Progressing work on SQL user interface.

FossilOrigin-Name: 44f3ad551f910061a585967beaedc164c371dbdc046ff6f97987eb26c8169832
This commit is contained in:
7u83@mail.ru 2015-04-29 10:23:12 +00:00
parent 74a47c97e1
commit f11f2b2cc5
1 changed files with 39 additions and 11 deletions

View File

@ -12,20 +12,27 @@ static sqlite3 *handle;
const char * init_tables = "\ const char * init_tables = "\
CREATE TABLE IF NOT EXISTS acs (acid TEXT PRIMARY KEY, acname TEXT, lastseen TIMESTAMP); \ CREATE TABLE IF NOT EXISTS acs (acid TEXT PRIMARY KEY, acname TEXT, lastseen TIMESTAMP); \
CREATE TABLE IF NOT EXISTS acips (acid TEXT,ip TEXT); \ CREATE TABLE IF NOT EXISTS acips (acid TEXT,ip TEXT); \
CREATE TABLE IF NOT EXISTS wtps (wtpid TEXT, wtp_name TEXT); \ CREATE TABLE IF NOT EXISTS wtps (wtpid TEXT PRIMARY KEY, wtp_name TEXT,lastseen TIMESTAMP); \
CREATE TABLE IF NOT EXISTS wtpprops (\
wtpid TEXT,\
rid INTEGER,\
prop TEXT,\
val TEXT,\
upd INTEGER,\
PRIMARY KEY(wtpid,rid,prop)\
);\
"; ";
int db_init() int db_init()
{ {
const char * filename="ac.sqlite3"; const char * filename="ac.sqlite3";
cw_dbg(DBG_INFO,"Init sqlite3 db: %s",filename); cw_dbg(DBG_INFO,"Initializing Sqlite3 DB: %s",filename);
int rc = sqlite3_open(filename,&handle); int rc = sqlite3_open(filename,&handle);
if (rc) if (rc != SQLITE_OK)
{ {
cw_log(LOG_ERR,"Error opening SQLite3 DB %s: %s",filename,sqlite3_errmsg(handle));
return 0; return 0;
} }
const char * cmd = init_tables; const char * cmd = init_tables;
@ -33,7 +40,7 @@ int db_init()
if (rc) if (rc)
{ {
const char *em = sqlite3_errmsg(handle); const char *em = sqlite3_errmsg(handle);
cw_log(LOG_ERR,"Error executing SQL \"%s\" - Error msg: %s",cmd, em); cw_log(LOG_ERR,"Error executing SQL \"%s\"\nSQL Error Message: %s",cmd, em);
return 0; return 0;
} }
@ -41,13 +48,18 @@ int db_init()
return 1; return 1;
} }
/** AC Ping statement */
static sqlite3_stmt * ping_stmt; static sqlite3_stmt * ping_stmt;
/** Put-WTP-Prop Statement */
static sqlite3_stmt * put_wtp_prop_stmt;
int db_start() int db_start()
{ {
cw_dbg(DBG_INFO,"Start sqlite3 db"); cw_dbg(DBG_INFO,"Starting Sqlite3 DB");
const char *sql="";
sqlite3_stmt *stmt; sqlite3_stmt *stmt;
int rc = sqlite3_prepare_v2(handle, "INSERT INTO acs (acid,acname) VALUES (?,?);",-1,&stmt,0); int rc = sqlite3_prepare_v2(handle, "INSERT INTO acs (acid,acname) VALUES (?,?);",-1,&stmt,0);
@ -63,17 +75,27 @@ int db_start()
rc = sqlite3_prepare_v2(handle, "UPDATE acs SET lastseen=datetime('now') WHERE acid=?;",-1,&ping_stmt,0); rc = sqlite3_prepare_v2(handle, "UPDATE acs SET lastseen=datetime('now') WHERE acid=?;",-1,&ping_stmt,0);
rc = sqlite3_bind_text(ping_stmt,1,conf_acid,-1,SQLITE_STATIC); rc = sqlite3_bind_text(ping_stmt,1,conf_acid,-1,SQLITE_STATIC);
// rc = sqlite3_prepare_v2(handle, "UPDATE acs SET lastseen=99 WHERE acid=? ;",-1,&ping_stmt,0);
sql = "INSERT OR REPLACE INTO wtpprops\
(wtpid,rid,prop,val,upd)\
VALUES (?,?,?,?,?)";
/* Prepare statement to update a WTP property */
rc = sqlite3_prepare_v2(handle, sql,-1, &put_wtp_prop_stmt,0);
if (rc)
goto errX;
return 1; return 1;
errX: errX:
cw_log(LOG_ERR,"Fatal: Can't start sqlite3 db, error %d",rc); cw_log(LOG_ERR,"Fatal: Can't start Sqlite3 DB, Error while executing '%s' - %d - %s",sql,rc,sqlite3_errmsg(handle));
return 0; return 0;
} }
void db_ping() void db_ping()
{ {
int rc = sqlite3_step(ping_stmt); int rc = sqlite3_step(ping_stmt);
@ -82,5 +104,11 @@ void db_ping()
} }
} }
void db_put_wtp_prop(const char *wtp_id,int rid, int upd, const char * prop)
{
int rc;
rc = sqlite3_bind_text(put_wtp_prop_stmt,1,wtp_id,-1,SQLITE_STATIC);
rc = sqlite3_bind_text(put_wtp_prop_stmt,2,wtp_id,-1,SQLITE_STATIC);
}