text stringlengths 0 357 |
|---|
free(sql); |
} |
int dc_sqlite3_open(dc_sqlite3_t* sql, const char* dbfile, int flags) |
{ |
if (dc_sqlite3_is_open(sql)) { |
return 0; // a cleanup would close the database |
} |
if (sql==NULL || dbfile==NULL) { |
goto cleanup; |
} |
if (sqlite3_threadsafe()==0) { |
dc_log_error(sql->context, 0, "Sqlite3 compiled thread-unsafe; this is not supported."); |
goto cleanup; |
} |
if (sql->cobj) { |
dc_log_error(sql->context, 0, "Cannot open, database \"%s\" already opened.", dbfile); |
goto cleanup; |
} |
// Force serialized mode (SQLITE_OPEN_FULLMUTEX) explicitly. |
// So, most of the explicit lock/unlocks on dc_sqlite3_t object are no longer needed. |
// However, locking is _also_ used for dc_context_t which _is_ still needed, so, we |
// should remove locks only if we're really sure. |
// |
// `PRAGMA cache_size` and `PRAGMA page_size`: As we save BLOBs in external |
// files, caching is not that important; we rely on the system defaults here |
// (normally 2 MB cache, 1 KB page size on sqlite < 3.12.0, 4 KB for newer |
// versions) |
if (sqlite3_open_v2(dbfile, &sql->cobj, |
SQLITE_OPEN_FULLMUTEX | ((flags&DC_OPEN_READONLY)? SQLITE_OPEN_READONLY : (SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE)), |
NULL) != SQLITE_OK) { |
dc_sqlite3_log_error(sql, "Cannot open database \"%s\".", dbfile); /* ususally, even for errors, the pointer is set up (if not, this is also checked by dc_sqlite3_log_error()) */ |
goto cleanup; |
} |
// let SQLite overwrite deleted content with zeros |
dc_sqlite3_execute(sql, "PRAGMA secure_delete=on;"); |
// Only one process can make changes to the database at one time. |
// busy_timeout defines, that if a seconds process wants write access, this second process will wait some milliseconds |
// and try over until it gets write access or the given timeout is elapsed. |
// If the second process does not get write access within the given timeout, sqlite3_step() will return the error SQLITE_BUSY. |
// (without a busy_timeout, sqlite3_step() would return SQLITE_BUSY at once) |
sqlite3_busy_timeout(sql->cobj, 10*1000); |
if (!(flags&DC_OPEN_READONLY)) |
{ |
int exists_before_update = 0; |
int dbversion_before_update = 0; |
/* Init tables to dbversion=0 */ |
if (!dc_sqlite3_table_exists(sql, "config")) |
{ |
dc_log_info(sql->context, 0, "First time init: creating tables in \"%s\".", dbfile); |
// the row with the type `INTEGER PRIMARY KEY` is an alias to the 64-bit-ROWID present in every table |
// we re-use this ID for our own purposes. |
// (the last inserted ROWID can be accessed using sqlite3_last_insert_rowid(), which, however, is |
// not recommended as not thread-safe, see above) |
dc_sqlite3_execute(sql, "CREATE TABLE config (id INTEGER PRIMARY KEY, keyname TEXT, value TEXT);"); |
dc_sqlite3_execute(sql, "CREATE INDEX config_index1 ON config (keyname);"); |
dc_sqlite3_execute(sql, "CREATE TABLE contacts (id INTEGER PRIMARY KEY AUTOINCREMENT," |
" name TEXT DEFAULT ''," |
" addr TEXT DEFAULT '' COLLATE NOCASE," |
" origin INTEGER DEFAULT 0," |
" blocked INTEGER DEFAULT 0," |
" last_seen INTEGER DEFAULT 0," /* last_seen is for future use */ |
" param TEXT DEFAULT '');"); /* param is for future use, eg. for the status */ |
dc_sqlite3_execute(sql, "CREATE INDEX contacts_index1 ON contacts (name COLLATE NOCASE);"); /* needed for query contacts */ |
dc_sqlite3_execute(sql, "CREATE INDEX contacts_index2 ON contacts (addr COLLATE NOCASE);"); /* needed for query and on receiving mails */ |
dc_sqlite3_execute(sql, "INSERT INTO contacts (id,name,origin) VALUES (1,'self',262144), (2,'device',262144), (3,'rsvd',262144), (4,'rsvd',262144), (5,'rsvd',262144), (6,'rsvd',262144), (7,'rsvd',262144), (8,'rsvd',262144), (9,'rsvd',262144);"); |
#if !defined(DC_ORIGIN_INTERNAL) || DC_ORIGIN_INTERNAL!=262144 |
#error |
#endif |
dc_sqlite3_execute(sql, "CREATE TABLE chats (id INTEGER PRIMARY KEY AUTOINCREMENT, " |
" type INTEGER DEFAULT 0," |
" name TEXT DEFAULT ''," |
" draft_timestamp INTEGER DEFAULT 0," |
" draft_txt TEXT DEFAULT ''," |
" blocked INTEGER DEFAULT 0," |
" grpid TEXT DEFAULT ''," /* contacts-global unique group-ID, see dc_chat.c for details */ |
" param TEXT DEFAULT '');"); |
dc_sqlite3_execute(sql, "CREATE INDEX chats_index1 ON chats (grpid);"); |
dc_sqlite3_execute(sql, "CREATE TABLE chats_contacts (chat_id INTEGER, contact_id INTEGER);"); |
dc_sqlite3_execute(sql, "CREATE INDEX chats_contacts_index1 ON chats_contacts (chat_id);"); |
dc_sqlite3_execute(sql, "INSERT INTO chats (id,type,name) VALUES (1,120,'deaddrop'), (2,120,'rsvd'), (3,120,'trash'), (4,120,'msgs_in_creation'), (5,120,'starred'), (6,120,'archivedlink'), (7,100,'rsvd'), (8,100,'rsvd'), (9,100,'rsvd');"); |
#if !defined(DC_CHAT_TYPE_SINGLE) || DC_CHAT_TYPE_SINGLE!=100 || DC_CHAT_TYPE_GROUP!=120 || \ |
DC_CHAT_ID_DEADDROP!=1 || DC_CHAT_ID_TRASH!=3 || \ |
DC_CHAT_ID_MSGS_IN_CREATION!=4 || DC_CHAT_ID_STARRED!=5 || DC_CHAT_ID_ARCHIVED_LINK!=6 || \ |
DC_CHAT_NOT_BLOCKED!=0 || DC_CHAT_MANUALLY_BLOCKED!=1 || DC_CHAT_DEADDROP_BLOCKED!=2 |
#error |
#endif |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.