text
stringlengths
0
357
dc_log_event(smtp->context, DC_EVENT_SMTP_MESSAGE_SENT, 0,
"Message was sent to SMTP server");
success = 1;
cleanup:
return success;
}
```
Filename: dc_sqlite3.c
```c
#include <assert.h>
#include <dirent.h>
#include <sys/stat.h>
#include "dc_context.h"
#include "dc_apeerstate.h"
/* This class wraps around SQLite.
We use a single handle for the database connections, mainly because
we do not know from which threads the UI calls the dc_*() functions.
As the open the Database in serialized mode explicitly, in general, this is
safe. However, there are some points to keep in mind:
1. Reading can be done at the same time from several threads, however, only
one thread can write. If a seconds thread tries to write, this thread
is halted until the first has finished writing, at most the timespan set
by sqlite3_busy_timeout().
2. Transactions are possible using `BEGIN IMMEDIATE` (this causes the first
thread trying to write to block the others as described in 1.
Transaction cannot be nested, we recommend to use them only in the
top-level functions or not to use them.
3. Using sqlite3_last_insert_rowid() and sqlite3_changes() cause race conditions
(between the query and the call another thread may insert or update a row.
These functions MUST NOT be used;
dc_sqlite3_get_rowid() provides an alternative. */
void dc_sqlite3_log_error(dc_sqlite3_t* sql, const char* msg_format, ...)
{
char* msg = NULL;
va_list va;
if (sql==NULL || msg_format==NULL) {
return;
}
va_start(va, msg_format);
msg = sqlite3_vmprintf(msg_format, va);
dc_log_error(sql->context, 0, "%s SQLite says: %s",
msg? msg : "",
sql->cobj? sqlite3_errmsg(sql->cobj) : "SQLite object not set up.");
sqlite3_free(msg);
va_end(va);
}
sqlite3_stmt* dc_sqlite3_prepare(dc_sqlite3_t* sql, const char* querystr)
{
sqlite3_stmt* stmt = NULL;
if (sql==NULL || querystr==NULL || sql->cobj==NULL) {
return NULL;
}
if (sqlite3_prepare_v2(sql->cobj,
querystr, -1 /*read `querystr` up to the first null-byte*/,
&stmt,
NULL /*tail not interesting, we use only single statements*/) != SQLITE_OK)
{
dc_sqlite3_log_error(sql, "Query failed: %s", querystr);
return NULL;
}
/* success - the result must be freed using sqlite3_finalize() */
return stmt;
}
int dc_sqlite3_try_execute(dc_sqlite3_t* sql, const char* querystr)
{
// same as dc_sqlite3_execute() but does not pass error to ui
int success = 0;
sqlite3_stmt* stmt = NULL;
int sql_state = 0;
stmt = dc_sqlite3_prepare(sql, querystr);
if (stmt==NULL) {
goto cleanup;
}
sql_state = sqlite3_step(stmt);