new: first database functions
This commit is contained in:
60
src/database/database.c
Normal file
60
src/database/database.c
Normal file
@ -0,0 +1,60 @@
|
||||
#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>
|
||||
|
||||
char *queries[] = {
|
||||
"CREATE TABLE IF NOT EXISTS packages (" \
|
||||
" name TEXT PRIMARY KEY NOT NULL," \
|
||||
" desc TEXT NOT NULL," \
|
||||
" version TEXT NOT NULL," \
|
||||
" size INT NOT NULL," \
|
||||
" depends TEXT NOT NULL);",
|
||||
|
||||
"INSERT INTO packages VALUES (?, ?, ?, ?, ?)",
|
||||
};
|
||||
|
||||
lm_database_t *lm_database_new(char *path){
|
||||
lm_database_t *db = malloc(sizeof(lm_database_t));
|
||||
char *err = NULL;
|
||||
bzero(db, sizeof(lm_database_t));
|
||||
|
||||
if(exists(path) && !can_read(path)){
|
||||
lm_error_set(LM_ERR_DbCantAccess);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(sqlite3_open(path, &db->sql)){
|
||||
pdebug(__func__, "(%s) failed to open databse: %s", path, sqlite3_errmsg(db->sql));
|
||||
lm_error_set(LM_ERR_DbSqlOpenFail);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(sqlite3_exec(db->sql, queries[QUERY_CREATE_TABLE], NULL, 0, &err) != SQLITE_OK){
|
||||
pdebug(__func__, "(%s) failed to create packages table: %s", path, err);
|
||||
lm_error_set(LM_ERR_DbSqlCreateFail);
|
||||
sqlite3_free(err);
|
||||
}
|
||||
|
||||
db->path = strdup(path);
|
||||
return db;
|
||||
}
|
||||
|
||||
void lm_database_free(lm_database_t *db){
|
||||
sqlite3_close(db->sql);
|
||||
free(db->path);
|
||||
|
||||
lm_pkg_t *cur = db->pkg, *prev = NULL;
|
||||
while(NULL != cur){
|
||||
prev = cur;
|
||||
cur = cur->next;
|
||||
lm_package_free(prev);
|
||||
}
|
||||
|
||||
free(db);
|
||||
}
|
53
src/database/package.c
Normal file
53
src/database/package.c
Normal file
@ -0,0 +1,53 @@
|
||||
#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)];
|
||||
sqlite3_stmt *insert = NULL;
|
||||
bool ret = false;
|
||||
|
||||
if(sqlite3_prepare(db->sql, queries[QUERY_INSERT_PACKAGE], strlen(queries[QUERY_INSERT_PACKAGE]), &insert, NULL) != SQLITE_OK){
|
||||
pdebug(__func__, "(%s) failed to prepare statement: %s", db->path, sqlite3_errmsg(db->sql));
|
||||
lm_error_set(LM_ERR_DbSqlPrepareFail);
|
||||
goto end;
|
||||
}
|
||||
|
||||
sqlite3_bind_text(insert, 1, pkg->name, strlen(pkg->name), SQLITE_STATIC);
|
||||
sqlite3_bind_text(insert, 2, pkg->desc, strlen(pkg->desc), SQLITE_STATIC);
|
||||
sqlite3_bind_text(insert, 3, pkg->version, strlen(pkg->version), SQLITE_STATIC);
|
||||
sqlite3_bind_int64(insert, 4, pkg->size);
|
||||
|
||||
if(!lm_package_depend_tostr(pkg, depends)){
|
||||
pdebug(__func__, "(%s) failed to convert depends to string: %s", db->path, lm_strerror());
|
||||
goto end;
|
||||
}
|
||||
sqlite3_bind_text(insert, 5, depends, strlen(depends), SQLITE_STATIC);
|
||||
|
||||
if(sqlite3_step(insert) != SQLITE_OK){
|
||||
pdebug(__func__, "(%s) failed to execute insert statement: %s", db->path, sqlite3_errmsg(db->sql));
|
||||
lm_error_set(LM_ERR_DbSqlInsertFail);
|
||||
goto end;
|
||||
}
|
||||
|
||||
ret = true;
|
||||
end:
|
||||
if(NULL != insert)
|
||||
sqlite3_finalize(insert);
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool lm_database_package_find(lm_database_t *db, lm_pkg_t *pkg){
|
||||
return true;
|
||||
}
|
Reference in New Issue
Block a user