#include #include #include "util.h" #include #include "models.h" #include "sql.h" /* * command parsing */ klist *klist_init(char *db) { klist *ctx = malloc(sizeof(klist)); ctx->stmts = malloc(_KLIST_SQL_COUNT * sizeof(sqlite3_stmt *)); ctx->cmd_ctx = NULL; ctx->error = 0; klist_sql_prepare(ctx, db); return ctx; } void klist_deinit(klist *ctx) { int i = 0; for (; i < _KLIST_SQL_COUNT; i++) sqlite3_finalize(ctx->stmts[i]); sqlite3_close(ctx->db); if (ctx->cmd_ctx) free(ctx->cmd_ctx); free(ctx->stmts); free(ctx); } klist_user_context *klist_user_context_init(klist *list) { klist_user_context *ctx = malloc(sizeof(klist_user_context)); ctx->cmd = -1; return ctx; } void klist_user_context_deinit(klist_user_context *ctx) { free(ctx); } klist_list_context *klist_list_context_init(klist *list) { klist_list_context *ctx = malloc(sizeof(klist_list_context)); ctx->cmd = -1; ctx->name = NULL; ctx->desc = NULL; ctx->stages = NULL; ctx->stages_len = 0; return ctx; } void klist_list_context_deinit(klist_list_context *ctx) { int i = 0; for (; i < ctx->stages_len; i++) free(ctx->stages[i]); free(ctx->stages); free(ctx); } klist_task_context *klist_task_context_init(klist *list) { klist_task_context *ctx = malloc(sizeof(klist_task_context)); ctx->cmd = -1; ctx->name = NULL; ctx->stage = NULL; return ctx; } void klist_task_context_deinit(klist_task_context *ctx) { free(ctx->name); free(ctx->stage); free(ctx); } /* * sql preparations */ void klist_sql_prepare(klist *ctx, char *db) { if (sqlite3_open(db ? db : ":memory:", &ctx->db) != SQLITE_OK) { fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(ctx->db)); return; } int i = 0; for (; i < _KLIST_SQL_COUNT; i++) if (sqlite3_prepare(ctx->db, klist_sql_get(i), -1, &ctx->stmts[i], NULL) != SQLITE_OK) fprintf(stderr, "sqlite3_prepare: %s\n", sqlite3_errmsg(ctx->db)); if (sqlite3_step(ctx->stmts[INIT]) != SQLITE_DONE) fprintf(stderr, "Database initialization failed (%s), expect issues.\n", sqlite3_errmsg(ctx->db)); } /* * assuring things */ bool klist_assure_user(klist *ctx, __uid_t id, char *name) { klist_user *user = klist_user_get_by_local(ctx, id); bool created = false; if (!user) { user = klist_user_init(); user->name = malloc((strlen(name) + 1) * sizeof(char)); strcpy((char *)user->name, name); user->local_id = id; klist_user_save(ctx, user); created = true; } klist_user_deinit(user); return created; } void klist_assure_list(klist *, char *); void klist_assure_task(klist *, char *);