mirror of
https://github.com/chatmail/core.git
synced 2026-05-25 01:36:31 +03:00
fix last prepares
This commit is contained in:
@@ -1010,12 +1010,10 @@ unsafe fn export_backup(context: &Context, dir: *const libc::c_char) -> libc::c_
|
||||
);
|
||||
} else {
|
||||
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;
|
||||
for entry in dir_handle {
|
||||
if entry.is_err() {
|
||||
@@ -1085,6 +1083,9 @@ unsafe fn export_backup(context: &Context, dir: *const libc::c_char) -> libc::c_
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
).unwrap();
|
||||
}
|
||||
} else {
|
||||
info!(context, 0, "Backup: No files to copy.",);
|
||||
|
||||
@@ -409,42 +409,25 @@ pub unsafe fn dc_save_locations(
|
||||
return 0;
|
||||
}
|
||||
|
||||
let stmt_test = dc_sqlite3_prepare(
|
||||
context,
|
||||
&context.sql,
|
||||
context
|
||||
.sql
|
||||
.prepare2(
|
||||
"SELECT id FROM locations WHERE timestamp=? AND from_id=?",
|
||||
);
|
||||
let stmt_insert = dc_sqlite3_prepare(
|
||||
context,
|
||||
&context.sql,
|
||||
"INSERT INTO locations\
|
||||
(timestamp, from_id, chat_id, latitude, longitude, accuracy, independent) \
|
||||
VALUES (?,?,?,?,?,?,?);",
|
||||
);
|
||||
|
||||
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();
|
||||
|
||||
|mut stmt_test, mut stmt_insert, conn| {
|
||||
let mut newest_timestamp = 0;
|
||||
let mut newest_location_id = 0;
|
||||
|
||||
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 exists = stmt_test
|
||||
.exists(params![(*location).timestamp, contact_id as i32])
|
||||
.unwrap_or_default();
|
||||
let exists =
|
||||
stmt_test.exists(params![(*location).timestamp, contact_id as i32])?;
|
||||
|
||||
if 0 != independent || !exists {
|
||||
// TODO: do I need to reset?
|
||||
if stmt_insert
|
||||
.execute(params![
|
||||
stmt_insert.execute(params![
|
||||
(*location).timestamp,
|
||||
contact_id as i32,
|
||||
chat_id as i32,
|
||||
@@ -452,17 +435,13 @@ pub unsafe fn dc_save_locations(
|
||||
(*location).longitude,
|
||||
(*location).accuracy,
|
||||
independent,
|
||||
])
|
||||
.is_err()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
])?;
|
||||
|
||||
if (*location).timestamp > newest_timestamp {
|
||||
newest_timestamp = (*location).timestamp;
|
||||
newest_location_id = dc_sqlite3_get_rowid2(
|
||||
newest_location_id = get_rowid2(
|
||||
context,
|
||||
&context.sql,
|
||||
conn,
|
||||
"locations",
|
||||
"timestamp",
|
||||
(*location).timestamp,
|
||||
@@ -472,8 +451,10 @@ pub unsafe fn dc_save_locations(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
newest_location_id
|
||||
Ok(newest_location_id)
|
||||
},
|
||||
)
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
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| {
|
||||
let stmt_locations = dc_sqlite3_prepare(
|
||||
context,
|
||||
&context.sql,
|
||||
context.sql.prepare(
|
||||
"SELECT id \
|
||||
FROM locations \
|
||||
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 independent=0 \
|
||||
ORDER BY timestamp;",
|
||||
);
|
||||
if stmt_locations.is_none() {
|
||||
// TODO: handle error
|
||||
return Ok(());
|
||||
}
|
||||
let mut stmt_locations = stmt_locations.unwrap();
|
||||
|
||||
|mut stmt_locations| {
|
||||
for (chat_id, locations_send_begin, locations_last_sent) in
|
||||
rows.filter_map(|r| match r {
|
||||
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(())
|
||||
},
|
||||
)
|
||||
},
|
||||
)
|
||||
.unwrap(); // TODO: Better error handling
|
||||
|
||||
if 0 != continue_streaming {
|
||||
|
||||
@@ -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 {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
let stmt = dc_sqlite3_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"
|
||||
);
|
||||
|
||||
if stmt.is_none() {
|
||||
// TODO: error handling
|
||||
return;
|
||||
}
|
||||
|
||||
let mut stmt = stmt.unwrap();
|
||||
context.sql.prepare(
|
||||
"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| {
|
||||
let mut send_event = false;
|
||||
|
||||
for i in 0..msg_cnt {
|
||||
// TODO: do I need to reset?
|
||||
let id = unsafe { *msg_ids.offset(i as isize) };
|
||||
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)?))
|
||||
})
|
||||
{
|
||||
if curr_blocked == 0 {
|
||||
if curr_state == 10 || curr_state == 13 {
|
||||
dc_update_msg_state(context, *msg_ids.offset(i as isize), 16);
|
||||
info!(context, 0, "Seen message #{}.", *msg_ids.offset(i as isize),);
|
||||
dc_update_msg_state(context, id, 16);
|
||||
info!(context, 0, "Seen message #{}.", id);
|
||||
|
||||
dc_job_add(
|
||||
unsafe { dc_job_add(
|
||||
context,
|
||||
130,
|
||||
*msg_ids.offset(i as isize) as libc::c_int,
|
||||
id as i32,
|
||||
0 as *const libc::c_char,
|
||||
0,
|
||||
);
|
||||
) };
|
||||
send_event = true;
|
||||
}
|
||||
} 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;
|
||||
}
|
||||
}
|
||||
@@ -572,6 +565,9 @@ pub unsafe fn dc_markseen_msgs(context: &Context, msg_ids: *const u32, msg_cnt:
|
||||
if send_event {
|
||||
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 {
|
||||
@@ -592,26 +588,15 @@ pub fn dc_star_msgs(
|
||||
if msg_ids.is_null() || msg_cnt <= 0 || star != 0 && star != 1 {
|
||||
return false;
|
||||
}
|
||||
let stmt = dc_sqlite3_prepare(
|
||||
context,
|
||||
&context.sql,
|
||||
"UPDATE msgs SET starred=? WHERE id=?;",
|
||||
);
|
||||
if stmt.is_none() {
|
||||
return false;
|
||||
}
|
||||
|
||||
let mut stmt = stmt.unwrap();
|
||||
context
|
||||
.sql
|
||||
.prepare("UPDATE msgs SET starred=? WHERE id=?;", |mut stmt| {
|
||||
for i in 0..msg_cnt {
|
||||
if stmt
|
||||
.execute(params![star, unsafe { *msg_ids.offset(i as isize) as i32 }])
|
||||
.is_err()
|
||||
{
|
||||
return false;
|
||||
stmt.execute(params![star, unsafe { *msg_ids.offset(i as isize) as i32 }])?;
|
||||
}
|
||||
}
|
||||
|
||||
true
|
||||
Ok(())
|
||||
})
|
||||
.is_ok()
|
||||
}
|
||||
|
||||
pub unsafe fn dc_get_msg<'a>(context: &'a Context, msg_id: uint32_t) -> *mut dc_msg_t<'a> {
|
||||
|
||||
@@ -489,24 +489,20 @@ pub unsafe fn dc_receive_imf(
|
||||
}
|
||||
icnt = carray_count(mime_parser.parts) as size_t;
|
||||
|
||||
let mut stmt = dc_sqlite3_prepare(
|
||||
context,
|
||||
&context.sql,
|
||||
context.sql.prepare(
|
||||
"INSERT INTO msgs \
|
||||
(rfc724_mid, server_folder, server_uid, chat_id, from_id, to_id, timestamp, \
|
||||
timestamp_sent, timestamp_rcvd, type, state, msgrmsg, txt, txt_raw, param, \
|
||||
bytes, hidden, mime_headers, mime_in_reply_to, mime_references) \
|
||||
VALUES (?,?,?,?,?,?, ?,?,?,?,?,?, ?,?,?,?,?,?, ?,?);"
|
||||
).unwrap();
|
||||
i = 0;
|
||||
VALUES (?,?,?,?,?,?, ?,?,?,?,?,?, ?,?,?,?,?,?, ?,?);",
|
||||
|mut stmt| {
|
||||
let mut i = 0;
|
||||
loop {
|
||||
if !(i < icnt) {
|
||||
current_block = 2756754640271984560;
|
||||
break;
|
||||
}
|
||||
let part: *mut dc_mimepart_t =
|
||||
carray_get(mime_parser.parts, i as libc::c_uint)
|
||||
as *mut dc_mimepart_t;
|
||||
let part = carray_get(mime_parser.parts, i as libc::c_uint) as *mut dc_mimepart_t;
|
||||
if !(0 != (*part).is_meta) {
|
||||
if !mime_parser.location_kml.is_null()
|
||||
&& icnt == 1
|
||||
@@ -605,6 +601,9 @@ pub unsafe fn dc_receive_imf(
|
||||
}
|
||||
i = i.wrapping_add(1)
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
).unwrap(); // TODO: better error handling
|
||||
match current_block {
|
||||
16282941964262048061 => {}
|
||||
_ => {
|
||||
|
||||
@@ -60,6 +60,32 @@ impl SQLite {
|
||||
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.
|
||||
/// Then executes the second function over the returned iterator and returns the
|
||||
/// result of that function.
|
||||
@@ -883,16 +909,6 @@ pub fn dc_sqlite3_set_config(
|
||||
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(
|
||||
context: &Context,
|
||||
sql: &SQLite,
|
||||
@@ -1047,8 +1063,22 @@ pub fn dc_sqlite3_get_rowid2(
|
||||
field2: impl AsRef<str>,
|
||||
value2: i32,
|
||||
) -> u32 {
|
||||
// same as dc_sqlite3_get_rowid() with a key over two columns
|
||||
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(
|
||||
&format!(
|
||||
"SELECT id FROM ? WHERE {}=? AND {}=? ORDER BY id DESC",
|
||||
@@ -1064,9 +1094,6 @@ pub fn dc_sqlite3_get_rowid2(
|
||||
0
|
||||
}
|
||||
}
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
|
||||
pub fn dc_housekeeping(context: &Context) {
|
||||
|
||||
Reference in New Issue
Block a user