#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); 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 nb_resp = malloc(sizeof(netbyte_store)); nb_init(nb_resp); PNL(); resp = rpc_call_resp_new(out_result,"None",1); resp->user = alloc_new_str(" "); resp->server = alloc_new_str(" "); resp->mask = alloc_new_str(" "); PNL(); rpc_call_resp_marsh(resp, &nb_resp); PNL(); nb_buf = (char *)nb_create(nb_resp); if (nb_buf) { ret = nb_buf; } nb_free(nb_resp); rpc_call_resp_free(resp); return ret; }