From bb57317f22b94970d78a97915763b0dbe1d7742d Mon Sep 17 00:00:00 2001 From: FreeArtMan Date: Sun, 29 Oct 2017 15:44:42 +0000 Subject: Make rpc and todo command working, with sqlite shitz --- cmd/cmd_todo.c | 181 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 179 insertions(+), 2 deletions(-) (limited to 'cmd/cmd_todo.c') diff --git a/cmd/cmd_todo.c b/cmd/cmd_todo.c index 2b3993b..50218b0 100644 --- a/cmd/cmd_todo.c +++ b/cmd/cmd_todo.c @@ -1,10 +1,105 @@ #include "cmd_todo.h" +static int add_todo(sqlite3 *db, char *user, char *todo) +{ + int rc; + + char sql_add_table[1024]; + snprintf(sql_add_table, 1024, "INSERT INTO todo(user,todo) VALUES('%s','%s');", user, todo); + printf("%s\n", sql_add_table); + + if ((rc = sqlite3_exec(db, sql_add_table, 0, 0, 0)) != SQLITE_OK) + { + printf("Cannot prepare statment: %s\n", sqlite3_errmsg(db)); + return -1; + } + + return 0; +} + +static int cb_list_todo_table(void *param1, int argc, char **argv, char **cname) +{ + int i; + + sds *out = (sds *)param1; + //sds local_out = sdsempty(); + + for (i=0; imethod, req->params, req->user, req->mask, req->server); + PRINT("(%s)-(%s)-(%s)-(%s)-(%s)\n", req->method, req->params, req->user, req->mask, req->server); + + if ((rc = sqlite3_open("todo.db", &db)) != SQLITE_OK) + { + printf("Cannot open todo database: %s\n", sqlite3_errmsg(db)); + sqlite3_close(db); + + return 1; + } + + //check if table excists + char *sql_check_table = "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='todo'"; + if ((rc = sqlite3_prepare_v2(db, sql_check_table, -1, &res, 0)) != SQLITE_OK) + { + printf("Cannot prepare statment: %s\n", sqlite3_errmsg(db)); + sqlite3_close(db); + return 1; + } + + rc = sqlite3_step(res); + if (rc == SQLITE_ROW) + { + int iret = sqlite3_column_int(res, 0); + //printf("%d\n", iret); + if (iret == 0) + { + table_todo_exists = 0; + } else + { + table_todo_exists = 1; + } + } + + //if table doesnt excists then create new one + if (table_todo_exists == 0) + { + char *sql_create_table = "CREATE TABLE todo(id INTEGER PRIMARY KEY AUTOINCREMENT, user TEXT, todo TEXT)"; + if ((rc = sqlite3_exec(db, sql_create_table, 0, 0, &err_msg)) != SQLITE_OK) + { + printf("Cant create table: %s\n", err_msg); + sqlite3_free(err_msg); + sqlite3_close(db); + } + } + + params = sdsnew(req->params); + tokens = sdssplitargs(params, &count); + if (strncmp(tokens[1],"add",3) == 0) + { + PRINT("ADD\n"); + if (count > 2) + { + sds sAdd = sdsempty(); + for (i=2;iuser, sAdd); + } + } else if (strncmp(tokens[1],"del",3) == 0) + { + PRINT("DEL\n"); + if (count > 2) + { + int id = atoi(tokens[2]); + del_todo(db, req->user, id); + } + + } else if (strncmp(tokens[1],"list",4) == 0) + { + PRINT("LIST\n"); + list_todo(db, req->user, &out_todo); + PRINT("%s\n",out_todo); + out_result = sdscatsds(out_result, out_todo); + out_result[sdslen(out_result)-1] = 0; //dirty hack mate + sdsfree(out_todo); + } else + { + + } + sqlite3_finalize(res); + sqlite3_close(db); //---------------------------------------------------------------------------- //prepare response @@ -51,7 +228,7 @@ void *cmd_todo(void *data) nb_init(nb_resp); PNL(); - resp = rpc_call_resp_new("Success","None",1); + resp = rpc_call_resp_new(out_result,"None",1); resp->user = alloc_new_str(" "); resp->server = alloc_new_str(" "); resp->mask = alloc_new_str(" "); -- cgit v1.2.3