mirror of
https://github.com/chatmail/core.git
synced 2026-04-26 01:46:34 +03:00
add sql.query_map
This commit is contained in:
@@ -670,38 +670,29 @@ pub unsafe fn dc_get_version_str() -> *mut libc::c_char {
|
||||
|
||||
pub fn dc_get_fresh_msgs(context: &Context) -> *mut dc_array_t {
|
||||
let show_deaddrop = 0;
|
||||
let ret = unsafe { dc_array_new(128 as size_t) };
|
||||
if ret.is_null() {
|
||||
return ret;
|
||||
}
|
||||
|
||||
let conn_lock = context.sql.get_conn();
|
||||
if let Some(ref mut stmt) = conn_lock.as_ref().and_then(|conn| {
|
||||
conn.prepare(
|
||||
context
|
||||
.sql
|
||||
.query_map(
|
||||
"SELECT m.id FROM msgs m LEFT JOIN contacts ct \
|
||||
ON m.from_id=ct.id LEFT JOIN chats c ON m.chat_id=c.id WHERE m.state=? \
|
||||
AND m.hidden=0 \
|
||||
AND m.chat_id>? \
|
||||
AND ct.blocked=0 \
|
||||
AND (c.blocked=0 OR c.blocked=?) ORDER BY m.timestamp DESC,m.id DESC;",
|
||||
)
|
||||
.ok()
|
||||
}) {
|
||||
match stmt.query_map(&[10, 9, if 0 != show_deaddrop { 2 } else { 0 }], |row| {
|
||||
row.get(0)
|
||||
}) {
|
||||
Ok(rows) => {
|
||||
for row in rows {
|
||||
if let Ok(id) = row {
|
||||
unsafe { dc_array_add_id(ret, id) };
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(_err) => {}
|
||||
}
|
||||
}
|
||||
&[10, 9, if 0 != show_deaddrop { 2 } else { 0 }],
|
||||
|row| row.get(0),
|
||||
|rows| {
|
||||
let ret = unsafe { dc_array_new(128 as size_t) };
|
||||
|
||||
ret
|
||||
for row in rows {
|
||||
let id = row?;
|
||||
unsafe { dc_array_add_id(ret, id) };
|
||||
}
|
||||
Ok(ret)
|
||||
},
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
pub fn dc_search_msgs(
|
||||
|
||||
@@ -59,6 +59,27 @@ impl SQLite {
|
||||
self.connection.read().unwrap()
|
||||
}
|
||||
|
||||
pub fn query_map<T, P, F, G, H>(
|
||||
&self,
|
||||
sql: &str,
|
||||
params: P,
|
||||
f: F,
|
||||
mut g: G,
|
||||
) -> rusqlite::Result<H>
|
||||
where
|
||||
P: IntoIterator,
|
||||
P::Item: rusqlite::ToSql,
|
||||
F: FnMut(&rusqlite::Row) -> rusqlite::Result<T>,
|
||||
G: FnMut(rusqlite::MappedRows<F>) -> rusqlite::Result<H>,
|
||||
{
|
||||
let conn_lock = self.connection.read().unwrap();
|
||||
let conn = conn_lock.as_ref().expect("database closed");
|
||||
|
||||
let mut stmt = conn.prepare(sql)?;
|
||||
let res = stmt.query_map(params, f)?;
|
||||
g(res)
|
||||
}
|
||||
|
||||
pub fn query_row<T, P, F>(&self, sql: &str, params: P, f: F) -> rusqlite::Result<T>
|
||||
where
|
||||
P: IntoIterator,
|
||||
|
||||
Reference in New Issue
Block a user