Refactor the internal sql interface somewhat

Experiment with refactoring the internal sql interface a bit.  My
original goal was to modify the schema and thus refactor to a state
where it would be sane to write tests for dc_sqlite_open() (and/or
however it ends up refactored) to assert schame changes before/after.
This commit is contained in:
Floris Bruynooghe
2019-06-08 21:49:38 +02:00
parent 7debe799d4
commit a6102d1039
27 changed files with 552 additions and 609 deletions

View File

@@ -57,14 +57,10 @@ pub unsafe fn dc_get_msg_info(context: &Context, msg_id: uint32_t) -> *mut libc:
let mut ret = String::new();
dc_msg_load_from_db(msg, context, msg_id);
dc_contact_load_from_db(
contact_from,
&context.sql.clone().read().unwrap(),
(*msg).from_id,
);
dc_contact_load_from_db(contact_from, &context.sql, (*msg).from_id);
stmt = dc_sqlite3_prepare(
context,
&context.sql.clone().read().unwrap(),
&context.sql,
b"SELECT txt_raw FROM msgs WHERE id=?;\x00" as *const u8 as *const libc::c_char,
);
sqlite3_bind_int(stmt, 1, msg_id as libc::c_int);
@@ -99,7 +95,7 @@ pub unsafe fn dc_get_msg_info(context: &Context, msg_id: uint32_t) -> *mut libc:
// device-internal message, no further details needed
stmt = dc_sqlite3_prepare(
context,
&context.sql.clone().read().unwrap(),
&context.sql,
b"SELECT contact_id, timestamp_sent FROM msgs_mdns WHERE msg_id=?;\x00" as *const u8
as *const libc::c_char,
);
@@ -111,7 +107,7 @@ pub unsafe fn dc_get_msg_info(context: &Context, msg_id: uint32_t) -> *mut libc:
let contact = dc_contact_new(context);
dc_contact_load_from_db(
contact,
&context.sql.clone().read().unwrap(),
&context.sql,
sqlite3_column_int64(stmt, 0) as uint32_t,
);
p = dc_contact_get_name_n_addr(contact);
@@ -444,7 +440,7 @@ pub unsafe fn dc_msg_load_from_db<'a>(
stmt =
dc_sqlite3_prepare(
context,
&context.sql.clone().read().unwrap(),
&context.sql,
b"SELECT m.id,rfc724_mid,m.mime_in_reply_to,m.server_folder,m.server_uid,m.move_state,m.chat_id, m.from_id,m.to_id,m.timestamp,m.timestamp_sent,m.timestamp_rcvd, m.type,m.state,m.msgrmsg,m.txt, m.param,m.starred,m.hidden,m.location_id, c.blocked FROM msgs m LEFT JOIN chats c ON c.id=m.chat_id WHERE m.id=?;\x00"
as *const u8 as *const libc::c_char);
sqlite3_bind_int(stmt, 1i32, id as libc::c_int);
@@ -546,7 +542,7 @@ pub unsafe fn dc_get_mime_headers(context: &Context, msg_id: uint32_t) -> *mut l
stmt = dc_sqlite3_prepare(
context,
&context.sql.clone().read().unwrap(),
&context.sql,
b"SELECT mime_headers FROM msgs WHERE id=?;\x00" as *const u8 as *const libc::c_char,
);
sqlite3_bind_int(stmt, 1i32, msg_id as libc::c_int);
@@ -586,7 +582,7 @@ pub unsafe fn dc_delete_msgs(context: &Context, msg_ids: *const uint32_t, msg_cn
pub unsafe fn dc_update_msg_chat_id(context: &Context, msg_id: uint32_t, chat_id: uint32_t) {
let stmt = dc_sqlite3_prepare(
context,
&context.sql.clone().read().unwrap(),
&context.sql,
b"UPDATE msgs SET chat_id=? WHERE id=?;\x00" as *const u8 as *const libc::c_char,
);
sqlite3_bind_int(stmt, 1i32, chat_id as libc::c_int);
@@ -603,7 +599,7 @@ pub unsafe fn dc_markseen_msgs(context: &Context, msg_ids: *const uint32_t, msg_
let mut stmt = 0 as *mut sqlite3_stmt;
if !(msg_ids.is_null() || msg_cnt <= 0i32) {
stmt =
dc_sqlite3_prepare(context, &context.sql.clone().read().unwrap(),
dc_sqlite3_prepare(context, &context.sql,
b"SELECT m.state, c.blocked FROM msgs m LEFT JOIN chats c ON c.id=m.chat_id WHERE m.id=? AND m.chat_id>9\x00"
as *const u8 as *const libc::c_char);
i = 0i32;
@@ -649,7 +645,7 @@ pub unsafe fn dc_markseen_msgs(context: &Context, msg_ids: *const uint32_t, msg_
pub unsafe fn dc_update_msg_state(context: &Context, msg_id: uint32_t, state: libc::c_int) {
let stmt = dc_sqlite3_prepare(
context,
&context.sql.clone().read().unwrap(),
&context.sql,
b"UPDATE msgs SET state=? WHERE id=?;\x00" as *const u8 as *const libc::c_char,
);
sqlite3_bind_int(stmt, 1i32, state);
@@ -669,7 +665,7 @@ pub unsafe fn dc_star_msgs(
}
let stmt = dc_sqlite3_prepare(
context,
&context.sql.clone().read().unwrap(),
&context.sql,
b"UPDATE msgs SET starred=? WHERE id=?;\x00" as *const u8 as *const libc::c_char,
);
let mut i: libc::c_int = 0i32;
@@ -1163,7 +1159,7 @@ pub unsafe fn dc_msg_save_param_to_disk(msg: *mut dc_msg_t) {
}
let stmt: *mut sqlite3_stmt = dc_sqlite3_prepare(
(*msg).context,
&mut (*msg).context.sql.clone().read().unwrap(),
&(*msg).context.sql,
b"UPDATE msgs SET param=? WHERE id=?;\x00" as *const u8 as *const libc::c_char,
);
sqlite3_bind_text(stmt, 1i32, (*(*msg).param).packed, -1i32, None);
@@ -1184,7 +1180,7 @@ pub unsafe fn dc_delete_msg_from_db(context: &Context, msg_id: uint32_t) {
if dc_msg_load_from_db(msg, context, msg_id) {
stmt = dc_sqlite3_prepare(
context,
&context.sql.clone().read().unwrap(),
&context.sql,
b"DELETE FROM msgs WHERE id=?;\x00" as *const u8 as *const libc::c_char,
);
sqlite3_bind_int(stmt, 1i32, (*msg).id as libc::c_int);
@@ -1192,7 +1188,7 @@ pub unsafe fn dc_delete_msg_from_db(context: &Context, msg_id: uint32_t) {
sqlite3_finalize(stmt);
stmt = dc_sqlite3_prepare(
context,
&context.sql.clone().read().unwrap(),
&context.sql,
b"DELETE FROM msgs_mdns WHERE msg_id=?;\x00" as *const u8 as *const libc::c_char,
);
sqlite3_bind_int(stmt, 1i32, (*msg).id as libc::c_int);
@@ -1218,7 +1214,7 @@ pub unsafe fn dc_msg_exists(context: &Context, msg_id: uint32_t) -> libc::c_int
if msg_id > 9 {
stmt = dc_sqlite3_prepare(
context,
&context.sql.clone().read().unwrap(),
&context.sql,
b"SELECT chat_id FROM msgs WHERE id=?;\x00" as *const u8 as *const libc::c_char,
);
sqlite3_bind_int(stmt, 1i32, msg_id as libc::c_int);
@@ -1244,7 +1240,7 @@ pub unsafe fn dc_update_msg_move_state(
// so that the state stay intact when parts are deleted
let stmt = dc_sqlite3_prepare(
context,
&context.sql.clone().read().unwrap(),
&context.sql,
b"UPDATE msgs SET move_state=? WHERE rfc724_mid=?;\x00" as *const u8 as *const libc::c_char,
);
sqlite3_bind_int(stmt, 1i32, state as libc::c_int);
@@ -1271,7 +1267,7 @@ pub unsafe fn dc_set_msg_failed(context: &Context, msg_id: uint32_t, error: *con
}
stmt = dc_sqlite3_prepare(
context,
&context.sql.clone().read().unwrap(),
&context.sql,
b"UPDATE msgs SET state=?, param=? WHERE id=?;\x00" as *const u8 as *const libc::c_char,
);
sqlite3_bind_int(stmt, 1i32, (*msg).state);
@@ -1314,7 +1310,7 @@ pub unsafe fn dc_mdn_from_ext(
stmt =
dc_sqlite3_prepare(
context,
&context.sql.clone().read().unwrap(),
&context.sql,
b"SELECT m.id, c.id, c.type, m.state FROM msgs m LEFT JOIN chats c ON m.chat_id=c.id WHERE rfc724_mid=? AND from_id=1 ORDER BY m.id;\x00"
as *const u8 as *const libc::c_char
);
@@ -1330,7 +1326,7 @@ pub unsafe fn dc_mdn_from_ext(
/* eg. already marked as MDNS_RCVD. however, it is importent, that the message ID is set above as this will allow the caller eg. to move the message away */
stmt = dc_sqlite3_prepare(
context,
&context.sql.clone().read().unwrap(),
&context.sql,
b"SELECT contact_id FROM msgs_mdns WHERE msg_id=? AND contact_id=?;\x00"
as *const u8 as *const libc::c_char,
);
@@ -1347,7 +1343,7 @@ pub unsafe fn dc_mdn_from_ext(
stmt =
dc_sqlite3_prepare(
context,
&context.sql.clone().read().unwrap(),
&context.sql,
b"INSERT INTO msgs_mdns (msg_id, contact_id, timestamp_sent) VALUES (?, ?, ?);\x00"
as *const u8 as
*const libc::c_char);
@@ -1366,7 +1362,7 @@ pub unsafe fn dc_mdn_from_ext(
/* send event about new state */
stmt = dc_sqlite3_prepare(
context,
&context.sql.clone().read().unwrap(),
&context.sql,
b"SELECT COUNT(*) FROM msgs_mdns WHERE msg_id=?;\x00" as *const u8
as *const libc::c_char,
);
@@ -1409,17 +1405,17 @@ pub unsafe fn dc_mdn_from_ext(
pub unsafe fn dc_get_real_msg_cnt(context: &Context) -> size_t {
let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt;
let mut ret: size_t = 0i32 as size_t;
if !(*&context.sql.clone().read().unwrap()).cobj.is_null() {
if (*&context.sql).is_open() {
stmt =
dc_sqlite3_prepare(
context,
&context.sql.clone().read().unwrap(),
&context.sql,
b"SELECT COUNT(*) FROM msgs m LEFT JOIN chats c ON c.id=m.chat_id WHERE m.id>9 AND m.chat_id>9 AND c.blocked=0;\x00"
as *const u8 as *const libc::c_char);
if sqlite3_step(stmt) != 100i32 {
dc_sqlite3_log_error(
context,
&context.sql.clone().read().unwrap(),
&context.sql,
b"dc_get_real_msg_cnt() failed.\x00" as *const u8 as *const libc::c_char,
);
} else {
@@ -1434,9 +1430,9 @@ pub unsafe fn dc_get_real_msg_cnt(context: &Context) -> size_t {
pub unsafe fn dc_get_deaddrop_msg_cnt(context: &Context) -> size_t {
let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt;
let mut ret: size_t = 0i32 as size_t;
if !context.sql.clone().read().unwrap().cobj.is_null() {
if context.sql.is_open() {
stmt =
dc_sqlite3_prepare(context, &context.sql.clone().read().unwrap(),
dc_sqlite3_prepare(context, &context.sql,
b"SELECT COUNT(*) FROM msgs m LEFT JOIN chats c ON c.id=m.chat_id WHERE c.blocked=2;\x00"
as *const u8 as *const libc::c_char);
if !(sqlite3_step(stmt) != 100i32) {
@@ -1452,10 +1448,10 @@ pub unsafe fn dc_rfc724_mid_cnt(context: &Context, rfc724_mid: *const libc::c_ch
/* check the number of messages with the same rfc724_mid */
let mut ret: libc::c_int = 0i32;
let mut stmt = 0 as *mut sqlite3_stmt;
if !context.sql.clone().read().unwrap().cobj.is_null() {
if context.sql.is_open() {
stmt = dc_sqlite3_prepare(
context,
&context.sql.clone().read().unwrap(),
&context.sql,
b"SELECT COUNT(*) FROM msgs WHERE rfc724_mid=?;\x00" as *const u8
as *const libc::c_char,
);
@@ -1480,7 +1476,7 @@ pub unsafe fn dc_rfc724_mid_exists(
if !(rfc724_mid.is_null() || *rfc724_mid.offset(0isize) as libc::c_int == 0i32) {
stmt = dc_sqlite3_prepare(
context,
&context.sql.clone().read().unwrap(),
&context.sql,
b"SELECT server_folder, server_uid, id FROM msgs WHERE rfc724_mid=?;\x00" as *const u8
as *const libc::c_char,
);
@@ -1515,7 +1511,7 @@ pub unsafe fn dc_update_server_uid(
) {
let stmt = dc_sqlite3_prepare(
context,
&context.sql.clone().read().unwrap(),
&context.sql,
b"UPDATE msgs SET server_folder=?, server_uid=? WHERE rfc724_mid=?;\x00" as *const u8
as *const libc::c_char,
);