Simplify Sql.query_row_col function

Function `query_row` executes query and calls callback function to process
row returned. Function query_row_col() is special case, that provides
callback function, which returns value of one particular column of row,
ignoring others.

In all cases, that particular column was 0 (first and only column of
query result), since there is no point to select more than one column
with this function -- they are discarded anyway.

This commit removes that redundancy, removing column number argument of
query_row_col() function and adjusting call sites accordingly.
This commit is contained in:
Dmitry Bogatov
2019-09-12 05:01:06 +00:00
parent 500e80784a
commit cdfc7281d0
13 changed files with 3 additions and 39 deletions

View File

@@ -145,7 +145,6 @@ impl Chat {
LEFT JOIN contacts c ON c.id=cc.contact_id \
WHERE cc.chat_id=?;",
params![self.id as i32],
0,
)
.unwrap_or_else(|| "Err".into());
}
@@ -282,7 +281,6 @@ impl Chat {
context,
"SELECT contact_id FROM chats_contacts WHERE chat_id=?;",
params![self.id as i32],
0,
) {
to_id = id;
} else {
@@ -729,7 +727,6 @@ fn last_msg_in_chat_encrypted(context: &Context, sql: &Sql, chat_id: u32) -> boo
FROM msgs WHERE timestamp=(SELECT MAX(timestamp) FROM msgs WHERE chat_id=?) \
ORDER BY id DESC;",
params![chat_id as i32],
0,
);
if let Some(ref packed) = packed {
@@ -915,7 +912,6 @@ fn get_draft_msg_id(context: &Context, chat_id: u32) -> u32 {
context,
"SELECT id FROM msgs WHERE chat_id=? AND state=?;",
params![chat_id as i32, MessageState::OutDraft],
0,
)
.unwrap_or_default() as u32
}
@@ -1010,7 +1006,6 @@ pub fn get_msg_cnt(context: &Context, chat_id: u32) -> usize {
context,
"SELECT COUNT(*) FROM msgs WHERE chat_id=?;",
params![chat_id as i32],
0,
)
.unwrap_or_default() as usize
}
@@ -1025,7 +1020,6 @@ pub fn get_fresh_msg_cnt(context: &Context, chat_id: u32) -> usize {
AND hidden=0 \
AND chat_id=?;",
params![chat_id as i32],
0,
)
.unwrap_or_default() as usize
}
@@ -1794,7 +1788,6 @@ pub fn get_chat_contact_cnt(context: &Context, chat_id: u32) -> libc::c_int {
context,
"SELECT COUNT(*) FROM chats_contacts WHERE chat_id=?;",
params![chat_id as i32],
0,
)
.unwrap_or_default()
}
@@ -1808,7 +1801,6 @@ pub fn get_chat_cnt(context: &Context) -> usize {
context,
"SELECT COUNT(*) FROM chats WHERE id>9 AND blocked=0;",
params![],
0,
)
.unwrap_or_default() as usize
} else {

View File

@@ -305,7 +305,6 @@ pub fn dc_get_archived_cnt(context: &Context) -> u32 {
context,
"SELECT COUNT(*) FROM chats WHERE blocked=0 AND archived=1;",
params![],
0,
)
.unwrap_or_default()
}
@@ -323,7 +322,6 @@ fn get_last_deaddrop_fresh_msg(context: &Context) -> u32 {
AND c.blocked=2 \
ORDER BY m.timestamp DESC, m.id DESC;",
params![],
0,
)
.unwrap_or_default()
}

View File

@@ -272,7 +272,6 @@ impl Contact {
DC_CONTACT_ID_LAST_SPECIAL as i32,
DC_ORIGIN_MIN_CONTACT_LIST,
],
0
).unwrap_or_default()
}
@@ -542,7 +541,6 @@ impl Contact {
context,
"SELECT COUNT(*) FROM contacts WHERE id>? AND blocked!=0",
params![DC_CONTACT_ID_LAST_SPECIAL as i32],
0,
)
.unwrap_or_default() as usize
}
@@ -649,7 +647,6 @@ impl Contact {
context,
"SELECT COUNT(*) FROM chats_contacts WHERE contact_id=?;",
params![contact_id as i32],
0,
)
.unwrap_or_default();
@@ -660,7 +657,6 @@ impl Contact {
context,
"SELECT COUNT(*) FROM msgs WHERE from_id=? OR to_id=?;",
params![contact_id as i32, contact_id as i32],
0,
)
.unwrap_or_default()
} else {
@@ -848,7 +844,6 @@ impl Contact {
context,
"SELECT COUNT(*) FROM contacts WHERE id>?;",
params![DC_CONTACT_ID_LAST_SPECIAL as i32],
0,
)
.unwrap_or_default() as usize
}

View File

@@ -353,14 +353,12 @@ pub unsafe fn dc_get_info(context: &Context) -> *mut libc::c_char {
context,
"SELECT COUNT(*) FROM keypairs;",
rusqlite::NO_PARAMS,
0,
);
let pub_key_cnt: Option<isize> = context.sql.query_row_col(
context,
"SELECT COUNT(*) FROM acpeerstates;",
rusqlite::NO_PARAMS,
0,
);
let fingerprint_str = if let Some(key) = Key::from_self_public(context, &l2.addr, &context.sql)

View File

@@ -606,7 +606,7 @@ unsafe fn import_backup(context: &Context, backup_to_import: *const libc::c_char
let total_files_cnt = context
.sql
.query_row_col::<_, isize>(context, "SELECT COUNT(*) FROM backup_blobs;", params![], 0)
.query_row_col::<_, isize>(context, "SELECT COUNT(*) FROM backup_blobs;", params![])
.unwrap_or_default() as usize;
info!(
context,

View File

@@ -995,7 +995,6 @@ unsafe fn calc_timestamps(
context,
"SELECT MAX(timestamp) FROM msgs WHERE chat_id=? and from_id!=? AND timestamp>=?",
params![chat_id as i32, from_id as i32, *sort_timestamp],
0,
);
if let Some(last_msg_time) = last_msg_time {
if last_msg_time > 0 {

View File

@@ -186,7 +186,6 @@ impl Job {
context,
"SELECT chat_id FROM msgs WHERE id=?",
params![self.foreign_id as i32],
0,
)
.unwrap_or_default();
context.call_cb(
@@ -603,7 +602,6 @@ fn get_next_wakeup_time(context: &Context, thread: Thread) -> Duration {
context,
"SELECT MIN(desired_timestamp) FROM jobs WHERE thread=?;",
params![thread],
0,
)
.unwrap_or_default();

View File

@@ -146,7 +146,6 @@ impl Key {
context,
"SELECT public_key FROM keypairs WHERE addr=? AND is_default=1;",
&[addr],
0,
)
.and_then(|blob: Vec<u8>| Self::from_slice(&blob, KeyType::Public))
}
@@ -160,7 +159,6 @@ impl Key {
context,
"SELECT private_key FROM keypairs WHERE addr=? AND is_default=1;",
&[self_addr.as_ref()],
0,
)
.and_then(|blob: Vec<u8>| Self::from_slice(&blob, KeyType::Private))
}

View File

@@ -37,7 +37,6 @@ impl<'a> Keyring<'a> {
context,
"SELECT private_key FROM keypairs ORDER BY addr=? DESC, is_default DESC;",
&[self_addr.as_ref()],
0,
)
.and_then(|blob: Vec<u8>| Key::from_slice(&blob, KeyType::Private))
.map(|key| self.add_owned(key))

View File

@@ -177,7 +177,6 @@ pub unsafe fn dc_get_msg_info(context: &Context, msg_id: u32) -> *mut libc::c_ch
context,
"SELECT txt_raw FROM msgs WHERE id=?;",
params![msg_id as i32],
0,
);
if rawtxt.is_none() {
@@ -495,7 +494,6 @@ pub unsafe fn dc_get_mime_headers(context: &Context, msg_id: u32) -> *mut libc::
context,
"SELECT mime_headers FROM msgs WHERE id=?;",
params![msg_id as i32],
0,
);
if let Some(headers) = headers {
@@ -1012,7 +1010,6 @@ pub fn dc_msg_exists(context: &Context, msg_id: u32) -> bool {
context,
"SELECT chat_id FROM msgs WHERE id=?;",
params![msg_id],
0,
);
if let Some(chat_id) = chat_id {
@@ -1135,7 +1132,6 @@ pub unsafe fn dc_mdn_from_ext(
context,
"SELECT COUNT(*) FROM msgs_mdns WHERE msg_id=?;",
params![*ret_msg_id as i32],
0,
)
.unwrap_or_default();
/*

View File

@@ -758,7 +758,6 @@ pub fn handle_degrade_event(context: &Context, peerstate: &Peerstate) {
context,
"SELECT id FROM contacts WHERE addr=?;",
params![&peerstate.addr],
0,
)
.unwrap_or_default();
if contact_id > 0 {

View File

@@ -155,19 +155,13 @@ impl Sql {
.unwrap_or_default()
}
pub fn query_row_col<P, T>(
&self,
context: &Context,
query: &str,
params: P,
column: usize,
) -> Option<T>
pub fn query_row_col<P, T>(&self, context: &Context, query: &str, params: P) -> Option<T>
where
P: IntoIterator,
P::Item: rusqlite::ToSql,
T: rusqlite::types::FromSql,
{
match self.query_row(query, params, |row| row.get::<_, T>(column)) {
match self.query_row(query, params, |row| row.get::<_, T>(0)) {
Ok(res) => Some(res),
Err(Error::Sql(rusqlite::Error::QueryReturnedNoRows)) => None,
Err(Error::Sql(rusqlite::Error::InvalidColumnType(
@@ -242,7 +236,6 @@ impl Sql {
context,
"SELECT value FROM config WHERE keyname=?;",
params![key.as_ref()],
0,
)
}

View File

@@ -39,7 +39,6 @@ pub fn lookup(context: &Context, namespace: Namespace, foreign_id: u32) -> Optio
context,
"SELECT token FROM tokens WHERE namespc=? AND foreign_id=?;",
params![namespace, foreign_id as i32],
0,
)
}