text
stringlengths
0
357
void dc_sqlite3_close(dc_sqlite3_t* sql)
{
if (sql==NULL) {
return;
}
if (sql->cobj)
{
sqlite3_close(sql->cobj);
sql->cobj = NULL;
}
dc_log_info(sql->context, 0, "Database closed."); /* We log the information even if not real closing took place; this is to detect logic errors. */
}
int dc_sqlite3_is_open(const dc_sqlite3_t* sql)
{
if (sql==NULL || sql->cobj==NULL) {
return 0;
}
return 1;
}
int dc_sqlite3_table_exists(dc_sqlite3_t* sql, const char* name)
{
int ret = 0;
char* querystr = NULL;
sqlite3_stmt* stmt = NULL;
int sqlState = 0;
if ((querystr=sqlite3_mprintf("PRAGMA table_info(%s)", name))==NULL) { /* this statement cannot be used with binded variables */
dc_log_error(sql->context, 0, "dc_sqlite3_table_exists_(): Out of memory.");
goto cleanup;
}
if ((stmt=dc_sqlite3_prepare(sql, querystr))==NULL) {
goto cleanup;
}
sqlState = sqlite3_step(stmt);
if (sqlState==SQLITE_ROW) {
ret = 1; /* the table exists. Other states are SQLITE_DONE or SQLITE_ERROR in both cases we return 0. */
}
/* success - fall through to free allocated objects */
;
/* error/cleanup */
cleanup:
if (stmt) {
sqlite3_finalize(stmt);
}
if (querystr) {
sqlite3_free(querystr);
}
return ret;
}
/*******************************************************************************
* Handle configuration
******************************************************************************/
int dc_sqlite3_set_config(dc_sqlite3_t* sql, const char* key, const char* value)
{
int state = 0;
sqlite3_stmt* stmt = NULL;
if (key==NULL) {
dc_log_error(sql->context, 0, "dc_sqlite3_set_config(): Bad parameter.");
return 0;
}
if (!dc_sqlite3_is_open(sql)) {
dc_log_error(sql->context, 0, "dc_sqlite3_set_config(): Database not ready.");
return 0;
}
if (value)
{
/* insert/update key=value */
#define SELECT_v_FROM_config_k_STATEMENT "SELECT value FROM config WHERE keyname=?;"
stmt = dc_sqlite3_prepare(sql, SELECT_v_FROM_config_k_STATEMENT);
sqlite3_bind_text (stmt, 1, key, -1, SQLITE_STATIC);
state = sqlite3_step(stmt);
sqlite3_finalize(stmt);
if (state==SQLITE_DONE) {
stmt = dc_sqlite3_prepare(sql, "INSERT INTO config (keyname, value) VALUES (?, ?);");
sqlite3_bind_text (stmt, 1, key, -1, SQLITE_STATIC);
sqlite3_bind_text (stmt, 2, value, -1, SQLITE_STATIC);
state = sqlite3_step(stmt);
sqlite3_finalize(stmt);