fix last prepares

This commit is contained in:
dignifiedquire
2019-07-10 18:31:41 +02:00
parent 9dda90dd5d
commit a0acfca255
5 changed files with 353 additions and 366 deletions

View File

@@ -1010,12 +1010,10 @@ unsafe fn export_backup(context: &Context, dir: *const libc::c_char) -> libc::c_
); );
} else { } else {
let dir_handle = dir_handle.unwrap(); let dir_handle = dir_handle.unwrap();
let mut stmt = dc_sqlite3_prepare(
context,
&sql,
"INSERT INTO backup_blobs (file_name, file_content) VALUES (?, ?);"
).expect("bad sql state");
sql.prepare(
"INSERT INTO backup_blobs (file_name, file_content) VALUES (?, ?);",
move |mut stmt| {
let mut processed_files_cnt = 0; let mut processed_files_cnt = 0;
for entry in dir_handle { for entry in dir_handle {
if entry.is_err() { if entry.is_err() {
@@ -1085,6 +1083,9 @@ unsafe fn export_backup(context: &Context, dir: *const libc::c_char) -> libc::c_
} }
} }
} }
Ok(())
}
).unwrap();
} }
} else { } else {
info!(context, 0, "Backup: No files to copy.",); info!(context, 0, "Backup: No files to copy.",);

View File

@@ -409,42 +409,25 @@ pub unsafe fn dc_save_locations(
return 0; return 0;
} }
let stmt_test = dc_sqlite3_prepare( context
context, .sql
&context.sql, .prepare2(
"SELECT id FROM locations WHERE timestamp=? AND from_id=?", "SELECT id FROM locations WHERE timestamp=? AND from_id=?",
);
let stmt_insert = dc_sqlite3_prepare(
context,
&context.sql,
"INSERT INTO locations\ "INSERT INTO locations\
(timestamp, from_id, chat_id, latitude, longitude, accuracy, independent) \ (timestamp, from_id, chat_id, latitude, longitude, accuracy, independent) \
VALUES (?,?,?,?,?,?,?);", VALUES (?,?,?,?,?,?,?);",
); |mut stmt_test, mut stmt_insert, conn| {
if stmt_test.is_none() || stmt_insert.is_none() {
return 0;
}
let mut stmt_test = stmt_test.unwrap();
let mut stmt_insert = stmt_insert.unwrap();
let mut newest_timestamp = 0; let mut newest_timestamp = 0;
let mut newest_location_id = 0; let mut newest_location_id = 0;
for i in 0..dc_array_get_cnt(locations) { for i in 0..dc_array_get_cnt(locations) {
// TODO: do I need to reset?
let location = dc_array_get_ptr(locations, i as size_t) as *mut dc_location_t; let location = dc_array_get_ptr(locations, i as size_t) as *mut dc_location_t;
let exists = stmt_test let exists =
.exists(params![(*location).timestamp, contact_id as i32]) stmt_test.exists(params![(*location).timestamp, contact_id as i32])?;
.unwrap_or_default();
if 0 != independent || !exists { if 0 != independent || !exists {
// TODO: do I need to reset? stmt_insert.execute(params![
if stmt_insert
.execute(params![
(*location).timestamp, (*location).timestamp,
contact_id as i32, contact_id as i32,
chat_id as i32, chat_id as i32,
@@ -452,17 +435,13 @@ pub unsafe fn dc_save_locations(
(*location).longitude, (*location).longitude,
(*location).accuracy, (*location).accuracy,
independent, independent,
]) ])?;
.is_err()
{
return 0;
}
if (*location).timestamp > newest_timestamp { if (*location).timestamp > newest_timestamp {
newest_timestamp = (*location).timestamp; newest_timestamp = (*location).timestamp;
newest_location_id = dc_sqlite3_get_rowid2( newest_location_id = get_rowid2(
context, context,
&context.sql, conn,
"locations", "locations",
"timestamp", "timestamp",
(*location).timestamp, (*location).timestamp,
@@ -472,8 +451,10 @@ pub unsafe fn dc_save_locations(
} }
} }
} }
Ok(newest_location_id)
newest_location_id },
)
.unwrap_or_default()
} }
pub unsafe fn dc_kml_parse( pub unsafe fn dc_kml_parse(
@@ -677,9 +658,7 @@ pub unsafe fn dc_job_do_DC_JOB_MAYBE_SEND_LOCATIONS(context: &Context, _job: *mu
} }
}, },
|rows| { |rows| {
let stmt_locations = dc_sqlite3_prepare( context.sql.prepare(
context,
&context.sql,
"SELECT id \ "SELECT id \
FROM locations \ FROM locations \
WHERE from_id=? \ WHERE from_id=? \
@@ -687,13 +666,7 @@ pub unsafe fn dc_job_do_DC_JOB_MAYBE_SEND_LOCATIONS(context: &Context, _job: *mu
AND timestamp>? \ AND timestamp>? \
AND independent=0 \ AND independent=0 \
ORDER BY timestamp;", ORDER BY timestamp;",
); |mut stmt_locations| {
if stmt_locations.is_none() {
// TODO: handle error
return Ok(());
}
let mut stmt_locations = stmt_locations.unwrap();
for (chat_id, locations_send_begin, locations_last_sent) in for (chat_id, locations_send_begin, locations_last_sent) in
rows.filter_map(|r| match r { rows.filter_map(|r| match r {
Ok(Some(v)) => Some(v), Ok(Some(v)) => Some(v),
@@ -727,6 +700,8 @@ pub unsafe fn dc_job_do_DC_JOB_MAYBE_SEND_LOCATIONS(context: &Context, _job: *mu
Ok(()) Ok(())
}, },
) )
},
)
.unwrap(); // TODO: Better error handling .unwrap(); // TODO: Better error handling
if 0 != continue_streaming { if 0 != continue_streaming {

View File

@@ -524,46 +524,39 @@ pub fn dc_update_msg_chat_id(context: &Context, msg_id: u32, chat_id: u32) -> bo
) )
} }
pub unsafe fn dc_markseen_msgs(context: &Context, msg_ids: *const u32, msg_cnt: usize) { pub fn dc_markseen_msgs(context: &Context, msg_ids: *const u32, msg_cnt: usize) -> bool {
if msg_ids.is_null() || msg_cnt <= 0 { if msg_ids.is_null() || msg_cnt <= 0 {
return; return false;
} }
let stmt = dc_sqlite3_prepare( context.sql.prepare(
context, &context.sql, "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",
"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" |mut stmt| {
);
if stmt.is_none() {
// TODO: error handling
return;
}
let mut stmt = stmt.unwrap();
let mut send_event = false; let mut send_event = false;
for i in 0..msg_cnt { for i in 0..msg_cnt {
// TODO: do I need to reset? // TODO: do I need to reset?
let id = unsafe { *msg_ids.offset(i as isize) };
if let Ok((curr_state, curr_blocked)) = stmt if let Ok((curr_state, curr_blocked)) = stmt
.query_row(params![*msg_ids.offset(i as isize) as i32], |row| { .query_row(params![id as i32], |row| {
Ok((row.get::<_, i32>(0)?, row.get::<_, i32>(1)?)) Ok((row.get::<_, i32>(0)?, row.get::<_, i32>(1)?))
}) })
{ {
if curr_blocked == 0 { if curr_blocked == 0 {
if curr_state == 10 || curr_state == 13 { if curr_state == 10 || curr_state == 13 {
dc_update_msg_state(context, *msg_ids.offset(i as isize), 16); dc_update_msg_state(context, id, 16);
info!(context, 0, "Seen message #{}.", *msg_ids.offset(i as isize),); info!(context, 0, "Seen message #{}.", id);
dc_job_add( unsafe { dc_job_add(
context, context,
130, 130,
*msg_ids.offset(i as isize) as libc::c_int, id as i32,
0 as *const libc::c_char, 0 as *const libc::c_char,
0, 0,
); ) };
send_event = true; send_event = true;
} }
} else if curr_state == 10 { } else if curr_state == 10 {
dc_update_msg_state(context, *msg_ids.offset(i as isize), 13); dc_update_msg_state(context, id, 13);
send_event = true; send_event = true;
} }
} }
@@ -572,6 +565,9 @@ pub unsafe fn dc_markseen_msgs(context: &Context, msg_ids: *const u32, msg_cnt:
if send_event { if send_event {
context.call_cb(Event::MSGS_CHANGED, 0, 0); context.call_cb(Event::MSGS_CHANGED, 0, 0);
} }
Ok(())
}
).is_ok()
} }
pub fn dc_update_msg_state(context: &Context, msg_id: uint32_t, state: libc::c_int) -> bool { pub fn dc_update_msg_state(context: &Context, msg_id: uint32_t, state: libc::c_int) -> bool {
@@ -592,26 +588,15 @@ pub fn dc_star_msgs(
if msg_ids.is_null() || msg_cnt <= 0 || star != 0 && star != 1 { if msg_ids.is_null() || msg_cnt <= 0 || star != 0 && star != 1 {
return false; return false;
} }
let stmt = dc_sqlite3_prepare( context
context, .sql
&context.sql, .prepare("UPDATE msgs SET starred=? WHERE id=?;", |mut stmt| {
"UPDATE msgs SET starred=? WHERE id=?;",
);
if stmt.is_none() {
return false;
}
let mut stmt = stmt.unwrap();
for i in 0..msg_cnt { for i in 0..msg_cnt {
if stmt stmt.execute(params![star, unsafe { *msg_ids.offset(i as isize) as i32 }])?;
.execute(params![star, unsafe { *msg_ids.offset(i as isize) as i32 }])
.is_err()
{
return false;
} }
} Ok(())
})
true .is_ok()
} }
pub unsafe fn dc_get_msg<'a>(context: &'a Context, msg_id: uint32_t) -> *mut dc_msg_t<'a> { pub unsafe fn dc_get_msg<'a>(context: &'a Context, msg_id: uint32_t) -> *mut dc_msg_t<'a> {

View File

@@ -489,24 +489,20 @@ pub unsafe fn dc_receive_imf(
} }
icnt = carray_count(mime_parser.parts) as size_t; icnt = carray_count(mime_parser.parts) as size_t;
let mut stmt = dc_sqlite3_prepare( context.sql.prepare(
context,
&context.sql,
"INSERT INTO msgs \ "INSERT INTO msgs \
(rfc724_mid, server_folder, server_uid, chat_id, from_id, to_id, timestamp, \ (rfc724_mid, server_folder, server_uid, chat_id, from_id, to_id, timestamp, \
timestamp_sent, timestamp_rcvd, type, state, msgrmsg, txt, txt_raw, param, \ timestamp_sent, timestamp_rcvd, type, state, msgrmsg, txt, txt_raw, param, \
bytes, hidden, mime_headers, mime_in_reply_to, mime_references) \ bytes, hidden, mime_headers, mime_in_reply_to, mime_references) \
VALUES (?,?,?,?,?,?, ?,?,?,?,?,?, ?,?,?,?,?,?, ?,?);" VALUES (?,?,?,?,?,?, ?,?,?,?,?,?, ?,?,?,?,?,?, ?,?);",
).unwrap(); |mut stmt| {
i = 0; let mut i = 0;
loop { loop {
if !(i < icnt) { if !(i < icnt) {
current_block = 2756754640271984560; current_block = 2756754640271984560;
break; break;
} }
let part: *mut dc_mimepart_t = let part = carray_get(mime_parser.parts, i as libc::c_uint) as *mut dc_mimepart_t;
carray_get(mime_parser.parts, i as libc::c_uint)
as *mut dc_mimepart_t;
if !(0 != (*part).is_meta) { if !(0 != (*part).is_meta) {
if !mime_parser.location_kml.is_null() if !mime_parser.location_kml.is_null()
&& icnt == 1 && icnt == 1
@@ -605,6 +601,9 @@ pub unsafe fn dc_receive_imf(
} }
i = i.wrapping_add(1) i = i.wrapping_add(1)
} }
Ok(())
}
).unwrap(); // TODO: better error handling
match current_block { match current_block {
16282941964262048061 => {} 16282941964262048061 => {}
_ => { _ => {

View File

@@ -60,6 +60,32 @@ impl SQLite {
self.connection.read().unwrap() self.connection.read().unwrap()
} }
pub fn prepare<G, H>(&self, sql: &str, g: G) -> Result<H>
where
G: FnOnce(Statement<'_>) -> Result<H>,
{
let conn_lock = self.connection.read().unwrap();
let conn = conn_lock.as_ref().expect("database closed");
let stmt = conn.prepare(sql)?;
let res = g(stmt)?;
Ok(res)
}
pub fn prepare2<G, H>(&self, sql1: &str, sql2: &str, g: G) -> Result<H>
where
G: FnOnce(Statement<'_>, Statement<'_>, &Connection) -> Result<H>,
{
let conn_lock = self.connection.read().unwrap();
let conn = conn_lock.as_ref().expect("database closed");
let stmt1 = conn.prepare(sql1)?;
let stmt2 = conn.prepare(sql2)?;
let res = g(stmt1, stmt2, conn)?;
Ok(res)
}
/// Prepares and executes the statement and maps a function over the resulting rows. /// Prepares and executes the statement and maps a function over the resulting rows.
/// Then executes the second function over the returned iterator and returns the /// Then executes the second function over the returned iterator and returns the
/// result of that function. /// result of that function.
@@ -883,16 +909,6 @@ pub fn dc_sqlite3_set_config(
1 1
} }
// TODO: Remove the option from the return type
pub fn dc_sqlite3_prepare<'a>(
_context: &Context,
_sql: &'a SQLite,
_querystr: &'a str,
) -> Option<Statement<'a>> {
// TODO: remove once it is not used anymore
unimplemented!()
}
pub fn dc_sqlite3_get_config( pub fn dc_sqlite3_get_config(
context: &Context, context: &Context,
sql: &SQLite, sql: &SQLite,
@@ -1047,8 +1063,22 @@ pub fn dc_sqlite3_get_rowid2(
field2: impl AsRef<str>, field2: impl AsRef<str>,
value2: i32, value2: i32,
) -> u32 { ) -> u32 {
// same as dc_sqlite3_get_rowid() with a key over two columns
if let Some(conn) = &*sql.connection.read().unwrap() { if let Some(conn) = &*sql.connection.read().unwrap() {
get_rowid2(context, conn, table, field, value, field2, value2)
} else {
0
}
}
pub fn get_rowid2(
context: &Context,
conn: &Connection,
table: impl AsRef<str>,
field: impl AsRef<str>,
value: i64,
field2: impl AsRef<str>,
value2: i32,
) -> u32 {
match conn.query_row( match conn.query_row(
&format!( &format!(
"SELECT id FROM ? WHERE {}=? AND {}=? ORDER BY id DESC", "SELECT id FROM ? WHERE {}=? AND {}=? ORDER BY id DESC",
@@ -1064,9 +1094,6 @@ pub fn dc_sqlite3_get_rowid2(
0 0
} }
} }
} else {
0
}
} }
pub fn dc_housekeeping(context: &Context) { pub fn dc_housekeeping(context: &Context) {