fix sql and statement creation, add more helper methods
This commit is contained in:
parent
e0610cee1c
commit
be20e4fab1
7 changed files with 144 additions and 58 deletions
|
@ -19,6 +19,8 @@ klist_user *klist_user_get_by_id(klist *, u_int);
|
||||||
klist_user *klist_user_get_by_local(klist *, u_int);
|
klist_user *klist_user_get_by_local(klist *, u_int);
|
||||||
klist_user *klist_user_get_by_discord(klist *, u_int);
|
klist_user *klist_user_get_by_discord(klist *, u_int);
|
||||||
klist_user *klist_user_get_by_google(klist *, u_int);
|
klist_user *klist_user_get_by_google(klist *, u_int);
|
||||||
|
void klist_user_save(klist *, klist_user *);
|
||||||
|
void klist_user_delete(klist *, klist_user *);
|
||||||
void klist_user_deinit(klist_user *);
|
void klist_user_deinit(klist_user *);
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -13,6 +13,7 @@ enum KLIST_SQL {
|
||||||
MOD_USER_LOCAL,
|
MOD_USER_LOCAL,
|
||||||
MOD_USER_DISCORD,
|
MOD_USER_DISCORD,
|
||||||
MOD_USER_GOOGLE,
|
MOD_USER_GOOGLE,
|
||||||
|
DEL_USER,
|
||||||
ADD_LIST,
|
ADD_LIST,
|
||||||
GET_LIST,
|
GET_LIST,
|
||||||
GET_LISTS,
|
GET_LISTS,
|
||||||
|
@ -32,4 +33,4 @@ enum KLIST_SQL {
|
||||||
_KLIST_SQL_COUNT
|
_KLIST_SQL_COUNT
|
||||||
};
|
};
|
||||||
|
|
||||||
char *klist_sql_get(const enum KLIST_SQL);
|
const char *klist_sql_get(const enum KLIST_SQL);
|
|
@ -28,10 +28,11 @@ void klist_deinit(klist *list);
|
||||||
|
|
||||||
enum klist_user_command {
|
enum klist_user_command {
|
||||||
USER_GET,
|
USER_GET,
|
||||||
|
USER_CREATE,
|
||||||
USER_DELETE
|
USER_DELETE
|
||||||
};
|
};
|
||||||
struct klist_user_context {
|
struct klist_user_context {
|
||||||
enum klist_command cmd;
|
enum klist_user_command cmd;
|
||||||
};
|
};
|
||||||
typedef struct klist_user_context klist_user_context;
|
typedef struct klist_user_context klist_user_context;
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "models.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
void print_help(char **argv);
|
void print_help(char **argv);
|
||||||
|
@ -18,16 +19,45 @@ int main(int argc, char **argv) {
|
||||||
|
|
||||||
if (!ctx->error) {
|
if (!ctx->error) {
|
||||||
setup(ctx, argc, argv);
|
setup(ctx, argc, argv);
|
||||||
|
klist_user *user;
|
||||||
switch (ctx->cmd) {
|
switch (ctx->cmd) {
|
||||||
case USER:
|
case USER:
|
||||||
switch (((klist_user_context *)ctx->cmd_ctx)->cmd) {
|
switch (((klist_user_context *)ctx->cmd_ctx)->cmd) {
|
||||||
case USER_GET:
|
case USER_GET:
|
||||||
|
user = klist_user_get_by_local(ctx, getuid());
|
||||||
|
if (user) {
|
||||||
|
fprintf(
|
||||||
|
stderr,
|
||||||
|
"User: %p\nID: %lu\nDiscord: %lu\nGoogle: %lu\n,",
|
||||||
|
user->name, user->id, user->discord_id, user->google_id
|
||||||
|
);
|
||||||
|
size_t lists_len, i = 0;
|
||||||
|
klist_list **lists = klist_list_get_all_by_user(ctx, user->id, &lists_len);
|
||||||
|
fprintf(stderr, "Lists: %lu\n", lists_len);
|
||||||
|
klist_user_deinit(user);
|
||||||
|
for (; i < lists_len; i++) klist_list_deinit(lists[i]);
|
||||||
|
} else fprintf(stderr, "No user for '%s' found.\n", getlogin());
|
||||||
|
break;
|
||||||
|
case USER_CREATE:
|
||||||
|
fprintf(
|
||||||
|
stderr,
|
||||||
|
(klist_assure_user(ctx, getuid(), getlogin()))
|
||||||
|
? "User created.\n"
|
||||||
|
: "User already exists.\n"
|
||||||
|
);
|
||||||
|
break;
|
||||||
case USER_DELETE:
|
case USER_DELETE:
|
||||||
default: ;
|
user = klist_user_get_by_local(ctx, getuid());
|
||||||
|
if (user) {
|
||||||
|
klist_user_delete(ctx, user);
|
||||||
|
klist_user_deinit(user);
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
case LIST:
|
case LIST:
|
||||||
switch (((klist_list_context *)ctx->cmd_ctx)->cmd) {
|
switch (((klist_list_context *)ctx->cmd_ctx)->cmd) {
|
||||||
case LIST_ADD:
|
case LIST_ADD:
|
||||||
|
|
||||||
case LIST_EDIT:
|
case LIST_EDIT:
|
||||||
case LIST_GET:
|
case LIST_GET:
|
||||||
case LIST_DELETE: ;
|
case LIST_DELETE: ;
|
||||||
|
@ -37,8 +67,7 @@ int main(int argc, char **argv) {
|
||||||
case TASK_ADD:
|
case TASK_ADD:
|
||||||
case TASK_EDIT:
|
case TASK_EDIT:
|
||||||
case TASK_GET:
|
case TASK_GET:
|
||||||
case TASK_DELETE:
|
case TASK_DELETE: ;
|
||||||
default: ;
|
|
||||||
}
|
}
|
||||||
default: ;
|
default: ;
|
||||||
}
|
}
|
||||||
|
@ -66,18 +95,22 @@ void setup(klist *ctx, int argc, char **argv) {
|
||||||
switch (ctx->cmd) {
|
switch (ctx->cmd) {
|
||||||
case USER:
|
case USER:
|
||||||
optind = 2;
|
optind = 2;
|
||||||
while ((opt = getopt(argc, argv, "dg")) != -1)
|
klist_user_context *user_ctx = klist_user_context_init(ctx);
|
||||||
|
while ((opt = getopt(argc, argv, "cdg")) != -1)
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
|
case 'c':
|
||||||
|
user_ctx->cmd = USER_CREATE;
|
||||||
|
break;
|
||||||
case 'd':
|
case 'd':
|
||||||
fprintf(stderr, "Deleting user %u\n", getuid());
|
user_ctx->cmd = USER_DELETE;
|
||||||
break;
|
break;
|
||||||
case 'g':
|
case 'g':
|
||||||
fprintf(stderr, "Printing user lists\n");
|
user_ctx->cmd = USER_GET;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
print_help(argv);
|
print_help(argv);
|
||||||
}
|
}
|
||||||
|
ctx->cmd_ctx = user_ctx;
|
||||||
break;
|
break;
|
||||||
case LIST:
|
case LIST:
|
||||||
if (argc < 3) {
|
if (argc < 3) {
|
||||||
|
@ -117,6 +150,7 @@ void setup(klist *ctx, int argc, char **argv) {
|
||||||
klist_list_context *task_ctx = malloc(sizeof(struct klist_task_context));
|
klist_list_context *task_ctx = malloc(sizeof(struct klist_task_context));
|
||||||
|
|
||||||
optind = 3;
|
optind = 3;
|
||||||
|
|
||||||
while ((opt = getopt(argc, argv, "aeds:")) != -1)
|
while ((opt = getopt(argc, argv, "aeds:")) != -1)
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
case 'a':
|
case 'a':
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
#include "models.h"
|
#include "models.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
@ -49,6 +50,26 @@ klist_user *klist_user_get_by_local(klist *ctx, u_int local_id) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void klist_user_save(klist *ctx, klist_user *user) {
|
||||||
|
sqlite3_stmt *stmt = ctx->stmts[ADD_USER];
|
||||||
|
int result = 0;
|
||||||
|
sqlite3_bind_text(stmt, 1, (char*)user->name, -1, SQLITE_STATIC);
|
||||||
|
sqlite3_bind_int(stmt, 2, user->local_id);
|
||||||
|
sqlite3_bind_int(stmt, 3, user->discord_id > 0 ? user->discord_id : -1);
|
||||||
|
sqlite3_bind_int(stmt, 4, user->google_id > 0 ? user->google_id : -1);
|
||||||
|
if ((result = sqlite3_step(stmt)) == SQLITE_ROW) {
|
||||||
|
user->id = sqlite3_column_int(stmt, 1);
|
||||||
|
sqlite3_reset(stmt);
|
||||||
|
} else fprintf(stderr, "failed to save user: %s - %d\n", sqlite3_errmsg(ctx->db), result);
|
||||||
|
}
|
||||||
|
|
||||||
|
void klist_user_delete(klist *ctx, klist_user *user) {
|
||||||
|
sqlite3_stmt *stmt = ctx->stmts[DEL_USER];
|
||||||
|
sqlite3_bind_int(stmt, 1, (int)user->id);
|
||||||
|
if (sqlite3_step(stmt) == SQLITE_ROW)
|
||||||
|
sqlite3_reset(stmt);
|
||||||
|
}
|
||||||
|
|
||||||
void klist_user_deinit(klist_user *user) {
|
void klist_user_deinit(klist_user *user) {
|
||||||
free(user->name);
|
free(user->name);
|
||||||
free(user);
|
free(user);
|
||||||
|
|
|
@ -1,45 +1,45 @@
|
||||||
#include "include/sql.h"
|
#include "include/sql.h"
|
||||||
|
|
||||||
|
|
||||||
char *klist_sql_get(const enum KLIST_SQL sql) {
|
const char *klist_sql_get(const enum KLIST_SQL sql) {
|
||||||
switch (sql) {
|
switch (sql) {
|
||||||
case INIT: return
|
case INIT: return
|
||||||
"create table if not exists users ("
|
"create table if not exists users (\n"
|
||||||
" id integer primary key,"
|
" id integer primary key,\n"
|
||||||
" name text not null,"
|
" name text not null,\n"
|
||||||
" local_id integer, -- if the thing is running locally"
|
" local_id integer, -- if the thing is running locally\n"
|
||||||
" discord_id integer, -- if logging in via web via discord"
|
" discord_id integer, -- if logging in via web via discord\n"
|
||||||
" google_id integer -- if logging in via web via google"
|
" google_id integer -- if logging in via web via google\n"
|
||||||
");"
|
");\n"
|
||||||
"create table if not exists user_lists ("
|
"create table if not exists user_lists (\n"
|
||||||
" id integer primary key,"
|
" id integer primary key,\n"
|
||||||
" user_id integer not null,"
|
" user_id integer not null,\n"
|
||||||
" list_id integer not null"
|
" list_id integer not null\n"
|
||||||
");"
|
");\n"
|
||||||
"create table if not exists lists ("
|
"create table if not exists lists (\n"
|
||||||
" id integer primary key,"
|
" id integer primary key,\n"
|
||||||
" name text not null,"
|
" name text not null,\n"
|
||||||
" desc text"
|
" desc text\n"
|
||||||
");"
|
");\n"
|
||||||
"create table if not exists task_stages ("
|
"create table if not exists task_stages (\n"
|
||||||
" id integer primary key,"
|
" id integer primary key,\n"
|
||||||
" name text not null,"
|
" name text not null,\n"
|
||||||
" desc text,"
|
" desc text,\n"
|
||||||
" list_id integer not null,"
|
" list_id integer not null,\n"
|
||||||
" foreign key (list_id) references lists(id)"
|
" foreign key (list_id) references lists(id)\n"
|
||||||
");"
|
");\n"
|
||||||
"-- this currently enables moving tasks _between lists_ (calculated of course)"
|
"-- this currently enables moving tasks _between lists_ (planned of course)\n"
|
||||||
"create table if not exists tasks ("
|
"create table if not exists tasks (\n"
|
||||||
" id integer primary key,"
|
" id integer primary key,\n"
|
||||||
" name text not null,"
|
" name text not null,\n"
|
||||||
" desc text,"
|
" desc text,\n"
|
||||||
" stage integer not null,"
|
" stage integer not null,\n"
|
||||||
" due date,"
|
" due date,\n"
|
||||||
" due_stage integer, -- more like target stage"
|
" due_stage integer, -- more like target stage\n"
|
||||||
" foreign key (stage) references task_stages(id),"
|
" foreign key (stage) references task_stages(id),\n"
|
||||||
" foreign key (due_stage) references task_stages(id)"
|
" foreign key (due_stage) references task_stages(id)\n"
|
||||||
")";
|
")";
|
||||||
case ADD_USER: return "insert into users values (?1, ?2, ?3, ?4)";
|
case ADD_USER: return "insert into users (name, local_id, discord_id, google_id) values (?1, ?2, ?3, ?4) returning id";
|
||||||
case GET_USER: return "select * from users where id = ?1";
|
case GET_USER: return "select * from users where id = ?1";
|
||||||
case GET_USER_BY_LOCAL: return "select * from users where local_id = ?1";
|
case GET_USER_BY_LOCAL: return "select * from users where local_id = ?1";
|
||||||
case GET_USER_BY_DISCORD: return "select * from users where discord_id = ?1";
|
case GET_USER_BY_DISCORD: return "select * from users where discord_id = ?1";
|
||||||
|
@ -49,23 +49,24 @@ char *klist_sql_get(const enum KLIST_SQL sql) {
|
||||||
case MOD_USER_LOCAL: return "update users set local_id = ?1 where id = ?2";;
|
case MOD_USER_LOCAL: return "update users set local_id = ?1 where id = ?2";;
|
||||||
case MOD_USER_DISCORD: return "update users set discord_id = ?1 where id = ?2";
|
case MOD_USER_DISCORD: return "update users set discord_id = ?1 where id = ?2";
|
||||||
case MOD_USER_GOOGLE: return "update users set google_id = ?1 where id = ?2";
|
case MOD_USER_GOOGLE: return "update users set google_id = ?1 where id = ?2";
|
||||||
case ADD_LIST: return "insert into lists values (?1, ?2)";
|
case DEL_USER: return "delete from users where id = ?1";
|
||||||
|
case ADD_LIST: return "insert into lists (name, desc) values (?1, ?2)";
|
||||||
case GET_LIST: return "select * from lists where id = ?1";
|
case GET_LIST: return "select * from lists where id = ?1";
|
||||||
case GET_LISTS: return "select * from lists";
|
case GET_LISTS: return "select * from lists";
|
||||||
case GET_LISTS_BY_USER: return "select * from lists where id = (select list_id from user_lists where user_id = ?1)";
|
case GET_LISTS_BY_USER: return "select * from lists where id = (select list_id from user_lists where user_id = ?1)";
|
||||||
case MOD_LIST_NAME: return "update lists set name = ?1 where id = ?2";
|
case MOD_LIST_NAME: return "update lists set name = ?1 where id = ?2";
|
||||||
case DEL_LIST: return "delete from lists where id = ?1";
|
case DEL_LIST: return "delete from lists where id = ?1";
|
||||||
case ADD_STAGE: return "insert into task_stages values (?1, ?2, ?3)";
|
case ADD_STAGE: return "insert into task_stages (name, desc, list_id) values (?1, ?2, ?3)";
|
||||||
case GET_STAGE: return "select * from task_stages where id = ?1";
|
case GET_STAGE: return "select * from task_stages where id = ?1";
|
||||||
case GET_STAGES_FOR_LIST: return "select * from task_stages";
|
case GET_STAGES_FOR_LIST: return "select * from task_stages";
|
||||||
case DEL_STAGE: return "delete from task_stages where id = ?1";
|
case DEL_STAGE: return "delete from task_stages where id = ?1";
|
||||||
case ADD_TASK: return "insert into tasks values (?1, ?2, ?3, ?4, ?5, ?6);";
|
case ADD_TASK: return "insert into tasks (name, desc, stage, due, due_stage) values (?1, ?2, ?3, ?4, ?5);";
|
||||||
case GET_TASK: return "select * from tasks where id = ?1";
|
case GET_TASK: return "select * from tasks where id = ?1";
|
||||||
case GET_TASKS: return "select * from tasks;";
|
case GET_TASKS: return "select * from tasks;";
|
||||||
case MOD_TASK_NAME: return "update tasks set name = ?1 where id = ?2";
|
case MOD_TASK_NAME: return "update tasks set name = ?1 where id = ?2";
|
||||||
case MOD_TASK_DESCRIPTION: return "update tasks set description = ?1 where id = ?2";
|
case MOD_TASK_DESCRIPTION: return "update tasks set desc = ?1 where id = ?2";
|
||||||
case DEL_TASK: return "delete from tasks where id = ?1";
|
case DEL_TASK: return "delete from tasks where id = ?1";
|
||||||
case _KLIST_SQL_COUNT: return ";";
|
case _KLIST_SQL_COUNT: return "";
|
||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,10 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "models.h"
|
||||||
#include "sql.h"
|
#include "sql.h"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -10,7 +14,7 @@
|
||||||
|
|
||||||
klist *klist_init(char *db) {
|
klist *klist_init(char *db) {
|
||||||
klist *ctx = malloc(sizeof(klist));
|
klist *ctx = malloc(sizeof(klist));
|
||||||
ctx->stmts = malloc(sizeof(sqlite3_stmt *) * _KLIST_SQL_COUNT);
|
ctx->stmts = malloc(_KLIST_SQL_COUNT * sizeof(sqlite3_stmt *));
|
||||||
ctx->cmd_ctx = NULL;
|
ctx->cmd_ctx = NULL;
|
||||||
ctx->error = 0;
|
ctx->error = 0;
|
||||||
|
|
||||||
|
@ -19,6 +23,9 @@ klist *klist_init(char *db) {
|
||||||
return ctx;
|
return ctx;
|
||||||
}
|
}
|
||||||
void klist_deinit(klist *ctx) {
|
void klist_deinit(klist *ctx) {
|
||||||
|
int i = 0;
|
||||||
|
for (; i < _KLIST_SQL_COUNT; i++)
|
||||||
|
sqlite3_finalize(ctx->stmts[i]);
|
||||||
sqlite3_close(ctx->db);
|
sqlite3_close(ctx->db);
|
||||||
if (ctx->cmd_ctx) free(ctx->cmd_ctx);
|
if (ctx->cmd_ctx) free(ctx->cmd_ctx);
|
||||||
free(ctx->stmts);
|
free(ctx->stmts);
|
||||||
|
@ -67,20 +74,39 @@ void klist_task_context_deinit(klist_task_context *ctx) {
|
||||||
*/
|
*/
|
||||||
|
|
||||||
void klist_sql_prepare(klist *ctx, char *db) {
|
void klist_sql_prepare(klist *ctx, char *db) {
|
||||||
ctx->stmts = malloc(sizeof(sqlite3_stmt *) * _KLIST_SQL_COUNT);
|
if (sqlite3_open(db ? db : ":memory:", &ctx->db) != SQLITE_OK) {
|
||||||
sqlite3_open(db ? db : ":memory:", &ctx->db);
|
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(ctx->db));
|
||||||
|
return;
|
||||||
|
}
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for (; i < _KLIST_SQL_COUNT; i++)
|
for (; i < _KLIST_SQL_COUNT; i++)
|
||||||
if (sqlite3_prepare(ctx->db, klist_sql_get(i), 0, &ctx->stmts[i], NULL) != SQLITE_OK)
|
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));
|
fprintf(stderr, "sqlite3_prepare: %s\n", sqlite3_errmsg(ctx->db));
|
||||||
|
char *err;
|
||||||
|
sqlite3_exec(ctx->db, klist_sql_get(INIT), NULL, NULL, &err);
|
||||||
|
if (err) {
|
||||||
|
fprintf(stderr, "Failed to initialize, expect errors: %s\n", err);
|
||||||
|
sqlite3_free(err);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* assuring things
|
* assuring things
|
||||||
*/
|
*/
|
||||||
|
|
||||||
bool klist_assure_user(klist *, __uid_t, char *);
|
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_list(klist *, char *);
|
||||||
void klist_assure_task(klist *, char *);
|
void klist_assure_task(klist *, char *);
|
Loading…
Add table
Reference in a new issue