text
stringlengths
0
357
if (sql_state != SQLITE_DONE && sql_state != SQLITE_ROW) {
dc_log_warning(sql->context, 0, "Try-execute for \"%s\" failed: %s",
querystr, sqlite3_errmsg(sql->cobj));
goto cleanup;
}
success = 1;
cleanup:
sqlite3_finalize(stmt);
return success;
}
int dc_sqlite3_execute(dc_sqlite3_t* sql, const char* querystr)
{
int success = 0;
sqlite3_stmt* stmt = NULL;
int sqlState = 0;
stmt = dc_sqlite3_prepare(sql, querystr);
if (stmt==NULL) {
goto cleanup;
}
sqlState = sqlite3_step(stmt);
if (sqlState != SQLITE_DONE && sqlState != SQLITE_ROW) {
dc_sqlite3_log_error(sql, "Cannot execute \"%s\".", querystr);
goto cleanup;
}
success = 1;
cleanup:
sqlite3_finalize(stmt);
return success;
}
uint32_t dc_sqlite3_get_rowid(dc_sqlite3_t* sql, const char* table, const char* field, const char* value)
{
// alternative to sqlite3_last_insert_rowid() which MUST NOT be used due to race conditions, see comment above.
// the ORDER BY ensures, this function always returns the most recent id,
// eg. if a Message-ID is splitted into different messages.
uint32_t id = 0;
char* q3 = sqlite3_mprintf("SELECT id FROM %s WHERE %s=%Q ORDER BY id DESC;", table, field, value);
sqlite3_stmt* stmt = dc_sqlite3_prepare(sql, q3);
if (SQLITE_ROW==sqlite3_step(stmt)) {
id = sqlite3_column_int(stmt, 0);
}
sqlite3_finalize(stmt);
sqlite3_free(q3);
return id;
}
uint32_t dc_sqlite3_get_rowid2(dc_sqlite3_t* sql, const char* table,
const char* field, uint64_t value,
const char* field2, uint32_t value2)
{
// same as dc_sqlite3_get_rowid() with a key over two columns
uint32_t id = 0;
// see https://www.sqlite.org/printf.html for sqlite-printf modifiers
char* q3 = sqlite3_mprintf(
"SELECT id FROM %s WHERE %s=%lli AND %s=%i ORDER BY id DESC;",
table, field, value, field2, value2);
sqlite3_stmt* stmt = dc_sqlite3_prepare(sql, q3);
if (SQLITE_ROW==sqlite3_step(stmt)) {
id = sqlite3_column_int(stmt, 0);
}
sqlite3_finalize(stmt);
sqlite3_free(q3);
return id;
}
dc_sqlite3_t* dc_sqlite3_new(dc_context_t* context)
{
dc_sqlite3_t* sql = NULL;
if ((sql=calloc(1, sizeof(dc_sqlite3_t)))==NULL) {
exit(24); /* cannot allocate little memory, unrecoverable error */
}
sql->context = context;
return sql;
}
void dc_sqlite3_unref(dc_sqlite3_t* sql)
{
if (sql==NULL) {
return;
}
if (sql->cobj) {
dc_sqlite3_close(sql);
}