libmp/src/database/package.c

198 lines
6.4 KiB
C

#include "../../include/database.h"
#include "../../include/package.h"
#include "../../include/error.h"
#include "../../include/util.h"
#include <sqlite3.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
bool lm_database_package_add(lm_database_t *db, lm_pkg_t *pkg){
if(NULL == db || NULL == pkg){
lm_error_set(LM_ERR_ArgNULL);
return false;
}
char depends[lm_package_depend_strlen(pkg)];
bool ret = false;
if(sqlite3_prepare(db->packages_db, queries[QUERY_INSERT_PACKAGE_SINGLE], strlen(queries[QUERY_INSERT_PACKAGE_SINGLE]), &db->packages_st, NULL) != SQLITE_OK){
pdebug(__func__, "failed to prepare statement for inserting %s: %s", pkg->name, sqlite3_errmsg(db->packages_db));
lm_error_set(LM_ERR_DbSqlPrepareFail);
goto end;
}
sqlite3_bind_text(db->packages_st, PACKAGES_COLUMN_NAME, pkg->name, strlen(pkg->name), SQLITE_STATIC);
sqlite3_bind_text(db->packages_st, PACKAGES_COLUMN_DESC, pkg->desc, strlen(pkg->desc), SQLITE_STATIC);
sqlite3_bind_text(db->packages_st, PACKAGES_COLUMN_VERSION, pkg->version, strlen(pkg->version), SQLITE_STATIC);
sqlite3_bind_int64(db->packages_st, PACKAGES_COLUMN_SIZE, pkg->size);
if(!lm_package_depend_tostr(pkg, depends)){
pdebug(__func__, "failed to convert depends to string for inserting %s: %s", pkg->name, lm_strerror());
goto end;
}
pdebug(__func__, "depend list for %s: %s", pkg->name, depends);
sqlite3_bind_text(db->packages_st, PACKAGES_COLUMN_DEPENDS, depends, strlen(depends), SQLITE_STATIC);
if(!lm_database_step_all(db->packages_st)){
pdebug(__func__, "failed to execute insert statement for inserting %s: %s", pkg->name, sqlite3_errmsg(db->packages_db));
lm_error_set(LM_ERR_DbSqlInsertFail);
goto end;
}
ret = true;
end:
if(NULL != db->packages_st){
sqlite3_finalize(db->packages_st);
db->packages_st = NULL;
}
return ret;
}
bool lm_database_package_find(lm_database_t *db, lm_pkg_t *pkg, char *name, char *version){
if(NULL == db || NULL == name){
lm_error_set(LM_ERR_ArgNULL);
return false;
}
bool ret = false;
char *query = NULL;
if(NULL == version)
query = queries[QUERY_SELECT_PACKAGE_SINGLE_1];
else
query = queries[QUERY_SELECT_PACKAGE_SINGLE_2];
// package pointer should already be free'd
// we are initing it just in case the caller didn't
if(NULL != pkg)
lm_package_init(pkg);
if(sqlite3_prepare(db->packages_db, query, strlen(query), &db->packages_st, NULL) != SQLITE_OK){
pdebug(__func__, "failed to prepare statement for finding %s: %s", name, sqlite3_errmsg(db->packages_db));
lm_error_set(LM_ERR_DbSqlPrepareFail);
goto end;
}
sqlite3_bind_text(db->packages_st, PACKAGES_COLUMN_NAME, name, strlen(name), SQLITE_STATIC);
if(NULL != version)
sqlite3_bind_text(db->packages_st, PACKAGES_COLUMN_VERSION, version, strlen(version), SQLITE_STATIC);
if(sqlite3_step(db->packages_st) != SQLITE_ROW){
pdebug(__func__, "got no rows for %s", name);
lm_error_set(LM_ERR_DbSqlNotFound);
goto end;
}
if(NULL == pkg){
ret = true;
goto end;
}
pkg->name = strdup((char*)sqlite3_column_text(db->packages_st, PACKAGES_COLUMN_NAME-1));
pkg->desc = strdup((char*)sqlite3_column_text(db->packages_st, PACKAGES_COLUMN_DESC-1));
pkg->version = strdup((char*)sqlite3_column_text(db->packages_st, PACKAGES_COLUMN_VERSION-1));
pkg->size = sqlite3_column_int64(db->packages_st, PACKAGES_COLUMN_SIZE-1);
char *depends = (char*)sqlite3_column_text(db->packages_st, PACKAGES_COLUMN_DEPENDS-1);
if(!lm_package_depend_fromstr(pkg, depends)){
pdebug(__func__, "failed to load depends for finding %s: %s", pkg->name, lm_strerror());
// error is set by the function
goto end;
}
ret = true;
end:
if(NULL != db->packages_st){
sqlite3_finalize(db->packages_st);
db->packages_st = NULL;
}
return ret;
}
bool lm_database_package_del(lm_database_t *db, lm_pkg_t *pkg){
if(NULL == db || NULL == pkg){
lm_error_set(LM_ERR_ArgNULL);
return false;
}
bool ret = false;
if(sqlite3_prepare(db->packages_db, queries[QUERY_DELETE_PACKAGE_SINGLE], strlen(queries[QUERY_DELETE_PACKAGE_SINGLE]), &db->packages_st, NULL) != SQLITE_OK){
pdebug(__func__, "failed to prepare statement for deleting %s: %s", pkg->name, sqlite3_errmsg(db->packages_db));
lm_error_set(LM_ERR_DbSqlPrepareFail);
goto end;
}
sqlite3_bind_text(db->packages_st, PACKAGES_COLUMN_NAME, pkg->name, strlen(pkg->name), SQLITE_STATIC);
if(sqlite3_step(db->packages_st) != SQLITE_DONE){
pdebug(__func__, "failed to execute delete statement for deleting %s: %s", pkg->name, sqlite3_errmsg(db->packages_db));
lm_error_set(LM_ERR_DbSqlDeleteFail);
goto end;
}
ret = true;
end:
if(NULL != db->packages_st){
sqlite3_finalize(db->packages_st);
db->packages_st = NULL;
}
return ret;
}
bool lm_database_package_next(lm_database_t *db, lm_pkg_t *pkg){
if(NULL == db || NULL == pkg){
lm_error_set(LM_ERR_ArgNULL);
return false;
}
if(NULL != db->packages_st)
lm_package_free(pkg);
else if(NULL == db->packages_st &&
sqlite3_prepare(db->packages_db, queries[QUERY_SELECT_PACKAGE_ALL], strlen(queries[QUERY_SELECT_PACKAGE_ALL]), &db->packages_st, NULL) != SQLITE_OK){
pdebug(__func__, "failed to prepare statement for selecting all: %s", sqlite3_errmsg(db->packages_db));
lm_error_set(LM_ERR_DbSqlPrepareFail);
return false;
}
lm_package_init(pkg);
if(sqlite3_step(db->packages_st) != SQLITE_ROW){
sqlite3_finalize(db->packages_st);
db->packages_st = NULL;
return false;
}
pkg->name = strdup((char*)sqlite3_column_text(db->packages_st, PACKAGES_COLUMN_NAME-1));
pkg->desc = strdup((char*)sqlite3_column_text(db->packages_st, PACKAGES_COLUMN_DESC-1));
pkg->version = strdup((char*)sqlite3_column_text(db->packages_st, PACKAGES_COLUMN_VERSION-1));
pkg->size = sqlite3_column_int64(db->packages_st, PACKAGES_COLUMN_SIZE-1);
char *depends = (char*)sqlite3_column_text(db->packages_st, PACKAGES_COLUMN_DEPENDS-1);
if(!lm_package_depend_fromstr(pkg, depends)){
pdebug(__func__, "failed to load depends for finding %s: %s", pkg->name, lm_strerror());
sqlite3_finalize(db->packages_st);
return false;
}
return true;
}
void lm_database_package_next_free(lm_database_t *db, lm_pkg_t *pkg){
if(NULL == db || NULL == pkg){
lm_error_set(LM_ERR_ArgNULL);
return;
}
if(NULL != db->packages_st){
sqlite3_finalize(db->packages_st);
db->packages_st = NULL;
}
if(NULL != pkg)
lm_package_free(pkg);
}