137 lines
No EOL
3.1 KiB
C
137 lines
No EOL
3.1 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
#include "util.h"
|
|
|
|
#include <string.h>
|
|
|
|
#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);
|
|
ctx = NULL;
|
|
}
|
|
|
|
klist_user_context *klist_user_context_init() {
|
|
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);
|
|
ctx = NULL;
|
|
}
|
|
|
|
klist_list_context *klist_list_context_init() {
|
|
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;
|
|
ctx->preset = NULL;
|
|
return ctx;
|
|
}
|
|
void klist_list_context_deinit(klist_list_context *ctx) {
|
|
if (ctx->name)
|
|
free(ctx->name);
|
|
if (ctx->desc)
|
|
free(ctx->desc);
|
|
if (ctx->preset)
|
|
free(ctx->preset);
|
|
if (ctx->stages) {
|
|
int i = 0;
|
|
for (; i < ctx->stages_len; i++)
|
|
free(ctx->stages[i]);
|
|
free(ctx->stages);
|
|
}
|
|
free(ctx);
|
|
ctx = NULL;
|
|
}
|
|
|
|
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->desc = NULL;
|
|
ctx->list = NULL;
|
|
ctx->stage = NULL;
|
|
return ctx;
|
|
}
|
|
void klist_task_context_deinit(klist_task_context *ctx) {
|
|
if (ctx->name)
|
|
free(ctx->name);
|
|
if (ctx->list)
|
|
free(ctx->list);
|
|
if (ctx->stage)
|
|
free(ctx->stage);
|
|
free(ctx);
|
|
ctx = NULL;
|
|
}
|
|
|
|
/*
|
|
* 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;
|
|
}
|
|
char *errmsg = NULL;
|
|
sqlite3_exec(ctx->db, klist_sql_get(INIT), NULL, NULL, &errmsg);
|
|
if (errmsg) {
|
|
fprintf(stderr, "Database initialization failed (%s), expect issues.\n",
|
|
sqlite3_errmsg(ctx->db));
|
|
sqlite3_free(errmsg);
|
|
}
|
|
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));
|
|
}
|
|
|
|
void klist_sql_seed(klist *ctx) {}
|
|
|
|
/*
|
|
* 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 *); |