More work on SQL support...

FossilOrigin-Name: d1c0825caf267daffe8dd04ad59a9b6f56026144fb891ac6879ddeb2988a38a2
This commit is contained in:
7u83@mail.ru 2015-04-30 12:10:59 +00:00
parent cdfc2c3f63
commit 7da8df5190
5 changed files with 56 additions and 6 deletions

View File

@ -27,7 +27,7 @@ int db_init()
{ {
const char * filename="ac.sqlite3"; const char * filename="ac.sqlite3";
cw_dbg(DBG_INFO,"Initializing Sqlite3 DB: %s",filename); cw_dbg(DBG_INFO,"Initializing Sqlite3 DB: %s, SQLite3 Version %s",filename,SQLITE_VERSION);
int rc = sqlite3_open(filename,&handle); int rc = sqlite3_open(filename,&handle);
if (rc != SQLITE_OK) if (rc != SQLITE_OK)
{ {
@ -51,7 +51,7 @@ int db_init()
static sqlite3_stmt * ping_stmt; static sqlite3_stmt * ping_stmt;
static sqlite3_stmt * put_wtp_prop_stmt; static sqlite3_stmt * put_wtp_prop_stmt;
static sqlite3_stmt * get_task_stmt; static sqlite3_stmt * get_tasks_stmt;
@ -76,16 +76,24 @@ int db_start()
rc = sqlite3_bind_text(ping_stmt,1,conf_acid,-1,SQLITE_STATIC); rc = sqlite3_bind_text(ping_stmt,1,conf_acid,-1,SQLITE_STATIC);
/* Prepare statement to update a WTP property */
sql = "INSERT OR REPLACE INTO wtpprops\ sql = "INSERT OR REPLACE INTO wtpprops\
(wtpid,rid,prop,val,upd)\ (wtpid,rid,prop,val,upd)\
VALUES (?,?,?,?,?)"; VALUES (?,?,?,?,?)";
/* Prepare statement to update a WTP property */
rc = sqlite3_prepare_v2(handle, sql,-1, &put_wtp_prop_stmt,0); rc = sqlite3_prepare_v2(handle, sql,-1, &put_wtp_prop_stmt,0);
if (rc) if (rc)
goto errX; goto errX;
sql = "SELECT * FROM wtpprops WHERE upd=0 AND wtpid=?";
rc = sqlite3_prepare_v2(handle, sql,-1, &get_tasks_stmt,0);
if (rc)
goto errX;
return 1; return 1;
@ -141,8 +149,34 @@ errX:
int db_get_tasks(const char * wtpid) int db_get_tasks(const char * wtpid)
{ {
sqlite3_reset(put_wtp_prop_stmt);
sqlite3_clear_bindings(put_wtp_prop_stmt);
if(sqlite3_bind_text(get_tasks_stmt,1,wtpid,-1,SQLITE_STATIC))
goto errX;
int rc;
rc = sqlite3_step(get_tasks_stmt);
if (rc == SQLITE_ROW) {
DBGX("Have a rowi %s",sqlite3_column_text(get_tasks_stmt,0));
DBGX("Have a rowi %s",sqlite3_column_text(get_tasks_stmt,1));
DBGX("Have a rowi %s",sqlite3_column_text(get_tasks_stmt,2));
DBGX("Have a rowi %s",sqlite3_column_text(get_tasks_stmt,3));
DBGX("Have a rowi %s",sqlite3_column_text(get_tasks_stmt,4));
}
DBGX("The SQL RC: %d\n",rc);
return 1;
errX:
if (rc) {
cw_log(LOG_ERR,"Can't update database with WTP props: %d - %s",
rc,sqlite3_errmsg(handle));
}
return 0;
} }

View File

@ -2,5 +2,7 @@ extern void db_ping();
extern int db_init(); extern int db_init();
int db_start(); int db_start();
void db_put_wtp_prop(const char *wtp_id,int rid, const char * prop,const char * val); void db_put_wtp_prop(const char *wtp_id,int rid, const char * prop,const char * val);
int db_get_tasks(const char * wtpid);

View File

@ -438,6 +438,8 @@ static void wtpman_run(void *arg)
if (errno != EAGAIN) if (errno != EAGAIN)
break; break;
} }
db_get_tasks(sock_addr2str(&conn->addr));
} }

View File

@ -14,7 +14,7 @@ static int mbag_bstr16str(void *item,char *dst)
if (utf8) { if (utf8) {
d += sprintf(d, "\"%.*s\"", bstr16_len(i->data), bstr16_data(i->data)); d += sprintf(d, "%.*s", bstr16_len(i->data), bstr16_data(i->data));
} else { } else {
d += sprintf(d, "\".x"); d += sprintf(d, "\".x");
d += cw_format_hex(d, bstr16_data(i->data), bstr16_len(i->data)); d += cw_format_hex(d, bstr16_data(i->data), bstr16_len(i->data));
@ -27,7 +27,7 @@ static int mbag_bstr16str(void *item,char *dst)
static int mbag_strstr(void *item,char *dst) static int mbag_strstr(void *item,char *dst)
{ {
mbag_item_t *i= item; mbag_item_t *i= item;
return sprintf(dst, "\"%s\"", i->data); return sprintf(dst, "%s", i->data);
} }

View File

@ -115,3 +115,15 @@ int cw_send_request(struct conn *conn,int msg_id)
return rc; return rc;
} }
// XXX find a better name for this function
int cw_send_request_2()
{
return 0;
}