bug fixing
This commit is contained in:
parent
cbbbf68db0
commit
445e883f55
4 changed files with 35 additions and 19 deletions
|
@ -65,15 +65,19 @@ int main(int argc, char **argv) {
|
||||||
fprintf(stderr, "Missing stages.\n");
|
fprintf(stderr, "Missing stages.\n");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
list = klist_list_init();
|
klist_assure_user(ctx, getuid(), getlogin());
|
||||||
list->name = (unsigned char *)list_ctx->name;
|
user = klist_user_get_by_local(ctx, getuid());
|
||||||
list->desc = (unsigned char *)list_ctx->desc;
|
list = klist_list_get_by_user_and_name(ctx, user->id, list_ctx->name);
|
||||||
|
if (!list) list = klist_list_init();
|
||||||
|
list->name = (unsigned char*)strdup(list_ctx->name);
|
||||||
|
if (list->desc)
|
||||||
|
list->desc = (unsigned char*)strdup(list_ctx->desc);
|
||||||
list->is_preset = false;
|
list->is_preset = false;
|
||||||
klist_list_save(ctx, list);
|
klist_list_save(ctx, list);
|
||||||
i = 0;
|
i = 0;
|
||||||
for (; i < list_ctx->stages_len; i++) {
|
for (; i < list_ctx->stages_len; i++) {
|
||||||
klist_stage *stage = klist_stage_init();
|
klist_stage *stage = klist_stage_init();
|
||||||
stage->name = (unsigned char *)list_ctx->stages[i];
|
stage->name = (unsigned char *)strdup(list_ctx->stages[i]);
|
||||||
stage->list_id = list->id;
|
stage->list_id = list->id;
|
||||||
klist_stage_save(ctx, stage);
|
klist_stage_save(ctx, stage);
|
||||||
klist_stage_deinit(stage);
|
klist_stage_deinit(stage);
|
||||||
|
@ -154,7 +158,7 @@ int main(int argc, char **argv) {
|
||||||
i = 0;
|
i = 0;
|
||||||
ssize_t stage_id = -1;
|
ssize_t stage_id = -1;
|
||||||
for (; i < stages_len; i++)
|
for (; i < stages_len; i++)
|
||||||
if (strcmp((char *)stages[i]->name, (char *)task_ctx->stage) == 0)
|
if (strcmp((char *)stages[i]->name, task_ctx->stage) == 0)
|
||||||
stage_id = stages[i]->id;
|
stage_id = stages[i]->id;
|
||||||
|
|
||||||
if (stage_id == -1) {
|
if (stage_id == -1) {
|
||||||
|
@ -298,7 +302,7 @@ void setup(klist *ctx, int argc, char **argv) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
optind = 3;
|
optind = 2;
|
||||||
while ((opt = getopt(argc, argv, "aden:p:s:")) != -1)
|
while ((opt = getopt(argc, argv, "aden:p:s:")) != -1)
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
case 'a':
|
case 'a':
|
||||||
|
@ -311,12 +315,18 @@ void setup(klist *ctx, int argc, char **argv) {
|
||||||
list_ctx->cmd = LIST_DELETE;
|
list_ctx->cmd = LIST_DELETE;
|
||||||
break;
|
break;
|
||||||
case 'p':
|
case 'p':
|
||||||
list_ctx->preset = malloc((strlen(optarg) + 1) * sizeof(char));
|
list_ctx->preset = strdup(optarg);
|
||||||
strcpy(list_ctx->preset, optarg);
|
|
||||||
break;
|
break;
|
||||||
case 'n':
|
case 'n':
|
||||||
list_ctx->name = malloc((strlen(optarg) + 1) * sizeof(char));
|
list_ctx->name = strdup(optarg);
|
||||||
strcpy(list_ctx->name, optarg);
|
break;
|
||||||
|
case 's':
|
||||||
|
char *stage = NULL;
|
||||||
|
while ((stage = strsep(&optarg, ","))) {
|
||||||
|
fprintf(stderr, "found stage %s\n", stage);
|
||||||
|
list_ctx->stages = realloc(list_ctx->stages, (++list_ctx->stages_len) * sizeof(char *));
|
||||||
|
list_ctx->stages[list_ctx->stages_len - 1] = strdup(stage);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
print_help(argv);
|
print_help(argv);
|
||||||
|
|
|
@ -42,7 +42,7 @@ klist_user *klist_user_get_by_local(klist *ctx, u_int local_id) {
|
||||||
sqlite3_clear_bindings(stmt);
|
sqlite3_clear_bindings(stmt);
|
||||||
return klist_user_init_from_sql(stmt);
|
return klist_user_init_from_sql(stmt);
|
||||||
}
|
}
|
||||||
fprintf(stderr, "failed to get user by local id %u\n", local_id);
|
fprintf(stderr, "failed to get user by local id %u: %s\n", local_id, sqlite3_errmsg(ctx->db));
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,7 +66,7 @@ void klist_user_save(klist *ctx, klist_user *user) {
|
||||||
fprintf(stderr, "failed to save logins for user: %s - %d\n",
|
fprintf(stderr, "failed to save logins for user: %s - %d\n",
|
||||||
sqlite3_errmsg(ctx->db), result);
|
sqlite3_errmsg(ctx->db), result);
|
||||||
sqlite3_clear_bindings(stmt);
|
sqlite3_clear_bindings(stmt);
|
||||||
}
|
} else fprintf(stderr, "no local uid saved for user %s", user->name);
|
||||||
}
|
}
|
||||||
|
|
||||||
void klist_user_delete(klist *ctx, klist_user *user) {
|
void klist_user_delete(klist *ctx, klist_user *user) {
|
||||||
|
@ -139,7 +139,8 @@ void klist_stage_save(klist *ctx, klist_stage *stage) {
|
||||||
int result = 0;
|
int result = 0;
|
||||||
sqlite3_bind_text(stmt, 1, (char *)stage->name, -1, SQLITE_STATIC);
|
sqlite3_bind_text(stmt, 1, (char *)stage->name, -1, SQLITE_STATIC);
|
||||||
sqlite3_bind_text(stmt, 2, (char *)stage->name, -1, SQLITE_STATIC);
|
sqlite3_bind_text(stmt, 2, (char *)stage->name, -1, SQLITE_STATIC);
|
||||||
if ((result = sqlite3_step(stmt)) == SQLITE_ROW) {
|
sqlite3_bind_int(stmt, 3, stage->list_id);
|
||||||
|
if ((result = sqlite3_step(stmt)) == SQLITE_ROW || result == SQLITE_DONE) {
|
||||||
stage->id = sqlite3_column_int(stmt, 0);
|
stage->id = sqlite3_column_int(stmt, 0);
|
||||||
sqlite3_clear_bindings(stmt);
|
sqlite3_clear_bindings(stmt);
|
||||||
} else
|
} else
|
||||||
|
@ -219,6 +220,7 @@ klist_list *klist_list_get_by_user_and_name(klist *ctx, u_int user_id,
|
||||||
sqlite3_clear_bindings(stmt);
|
sqlite3_clear_bindings(stmt);
|
||||||
return klist_list_init(stmt);
|
return klist_list_init(stmt);
|
||||||
}
|
}
|
||||||
|
fprintf(stderr, "failed to get lists for user\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -227,6 +229,7 @@ void klist_list_save(klist *ctx, klist_list *list) {
|
||||||
int result = 0;
|
int result = 0;
|
||||||
sqlite3_bind_text(stmt, 1, (char *)list->name, -1, SQLITE_STATIC);
|
sqlite3_bind_text(stmt, 1, (char *)list->name, -1, SQLITE_STATIC);
|
||||||
sqlite3_bind_text(stmt, 2, (char *)list->desc, -1, SQLITE_STATIC);
|
sqlite3_bind_text(stmt, 2, (char *)list->desc, -1, SQLITE_STATIC);
|
||||||
|
sqlite3_bind_int(stmt, 3, list->is_preset);
|
||||||
if ((result = sqlite3_step(stmt)) == SQLITE_ROW) {
|
if ((result = sqlite3_step(stmt)) == SQLITE_ROW) {
|
||||||
list->id = sqlite3_column_int(stmt, 0);
|
list->id = sqlite3_column_int(stmt, 0);
|
||||||
sqlite3_clear_bindings(stmt);
|
sqlite3_clear_bindings(stmt);
|
||||||
|
|
|
@ -56,8 +56,7 @@ const char *klist_sql_get(const enum KLIST_SQL sql) {
|
||||||
return "select users.*, user_logins.local_id from users inner join "
|
return "select users.*, user_logins.local_id from users inner join "
|
||||||
"user_logins on user_logins.user_id = users.id where users.id = ?1";
|
"user_logins on user_logins.user_id = users.id where users.id = ?1";
|
||||||
case GET_USER_BY_LOCAL:
|
case GET_USER_BY_LOCAL:
|
||||||
return "select * from users inner join user_logins on user_logins.user_id "
|
return "select * from users inner join user_logins on user_logins.user_id = users.id where user_logins.local_id = ?1";
|
||||||
"= users.id where user_logins.local_id = ?1";
|
|
||||||
case GET_USERS:
|
case GET_USERS:
|
||||||
return "select * from users";
|
return "select * from users";
|
||||||
case MOD_USER_NAME:
|
case MOD_USER_NAME:
|
||||||
|
@ -65,7 +64,7 @@ const char *klist_sql_get(const enum KLIST_SQL sql) {
|
||||||
case DEL_USER:
|
case DEL_USER:
|
||||||
return "delete from users where id = ?1";
|
return "delete from users where id = ?1";
|
||||||
case ADD_LIST:
|
case ADD_LIST:
|
||||||
return "insert into lists (name, desc, is_preset) values (?1, ?2, ?3)";
|
return "insert into lists (name, desc, is_preset) values (?1, ?2, ?3) on conflict (id) do update set name=excluded.name, desc=excluded.desc, is_preset=excluded.is_preset returning id";
|
||||||
case GET_LIST:
|
case GET_LIST:
|
||||||
return "select * from lists where id = ?1";
|
return "select * from lists where id = ?1";
|
||||||
case GET_LISTS:
|
case GET_LISTS:
|
||||||
|
@ -81,7 +80,7 @@ const char *klist_sql_get(const enum KLIST_SQL sql) {
|
||||||
case DEL_LIST:
|
case DEL_LIST:
|
||||||
return "delete from lists where id = ?1";
|
return "delete from lists where id = ?1";
|
||||||
case ADD_STAGE:
|
case ADD_STAGE:
|
||||||
return "insert into task_stages (name, desc, list_id) values (?1, ?2, ?3)";
|
return "insert into task_stages (name, desc, list_id) values (?1, ?2, ?3) on conflict (id) do update set name=excluded.name, desc=excluded.desc returning id";
|
||||||
case GET_STAGE:
|
case GET_STAGE:
|
||||||
return "select * from task_stages where id = ?1";
|
return "select * from task_stages where id = ?1";
|
||||||
case GET_STAGES_FOR_LIST:
|
case GET_STAGES_FOR_LIST:
|
||||||
|
@ -90,7 +89,7 @@ const char *klist_sql_get(const enum KLIST_SQL sql) {
|
||||||
return "delete from task_stages where id = ?1";
|
return "delete from task_stages where id = ?1";
|
||||||
case ADD_TASK:
|
case ADD_TASK:
|
||||||
return "insert into tasks (name, desc, stage, due, target_stage) values "
|
return "insert into tasks (name, desc, stage, due, target_stage) values "
|
||||||
"(?1, ?2, ?3, ?4, ?5);";
|
"(?1, ?2, ?3, ?4, ?5) on conflict (id) do update set name=excluded.name, desc=excluded.desc, stage=excluded.stage, due=excluded.due, target_stage=excluded.target_stage returning id";
|
||||||
case GET_TASK:
|
case GET_TASK:
|
||||||
return "select * from tasks where id = ?1";
|
return "select * from tasks where id = ?1";
|
||||||
case GET_TASKS:
|
case GET_TASKS:
|
||||||
|
|
|
@ -30,6 +30,7 @@ void klist_deinit(klist *ctx) {
|
||||||
// if (ctx->cmd_ctx) free(ctx->cmd_ctx);
|
// if (ctx->cmd_ctx) free(ctx->cmd_ctx);
|
||||||
free(ctx->stmts);
|
free(ctx->stmts);
|
||||||
free(ctx);
|
free(ctx);
|
||||||
|
ctx = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
klist_user_context *klist_user_context_init() {
|
klist_user_context *klist_user_context_init() {
|
||||||
|
@ -37,7 +38,8 @@ klist_user_context *klist_user_context_init() {
|
||||||
ctx->cmd = -1;
|
ctx->cmd = -1;
|
||||||
return ctx;
|
return ctx;
|
||||||
}
|
}
|
||||||
void klist_user_context_deinit(klist_user_context *ctx) { free(ctx); }
|
void klist_user_context_deinit(klist_user_context *ctx) { free(ctx);
|
||||||
|
ctx = NULL; }
|
||||||
|
|
||||||
klist_list_context *klist_list_context_init() {
|
klist_list_context *klist_list_context_init() {
|
||||||
klist_list_context *ctx = malloc(sizeof(klist_list_context));
|
klist_list_context *ctx = malloc(sizeof(klist_list_context));
|
||||||
|
@ -63,6 +65,7 @@ void klist_list_context_deinit(klist_list_context *ctx) {
|
||||||
free(ctx->stages);
|
free(ctx->stages);
|
||||||
}
|
}
|
||||||
free(ctx);
|
free(ctx);
|
||||||
|
ctx = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
klist_task_context *klist_task_context_init(klist *list) {
|
klist_task_context *klist_task_context_init(klist *list) {
|
||||||
|
@ -81,6 +84,7 @@ void klist_task_context_deinit(klist_task_context *ctx) {
|
||||||
if (ctx->stage)
|
if (ctx->stage)
|
||||||
free(ctx->stage);
|
free(ctx->stage);
|
||||||
free(ctx);
|
free(ctx);
|
||||||
|
ctx = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
Loading…
Add table
Reference in a new issue