refactor: use params! macro for SQL query parameters
This commit is contained in:
24
src/db.rs
24
src/db.rs
@@ -84,7 +84,7 @@ pub fn insert_item(conn: &Connection, item: Item) -> Result<i64> {
|
|||||||
debug!("DB: Inserting item: {:?}", item);
|
debug!("DB: Inserting item: {:?}", item);
|
||||||
conn.execute(
|
conn.execute(
|
||||||
"INSERT INTO items (ts, size, compression, digest_type, digest_value) VALUES (?1, ?2, ?3, ?4, ?5)",
|
"INSERT INTO items (ts, size, compression, digest_type, digest_value) VALUES (?1, ?2, ?3, ?4, ?5)",
|
||||||
(item.ts, item.size, item.compression, item.digest_type, item.digest_value),
|
params![item.ts, item.size, item.compression, item.digest_type, item.digest_value],
|
||||||
)?;
|
)?;
|
||||||
Ok(conn.last_insert_rowid())
|
Ok(conn.last_insert_rowid())
|
||||||
}
|
}
|
||||||
@@ -93,20 +93,14 @@ pub fn update_item(conn: &Connection, item: Item) -> Result<()> {
|
|||||||
debug!("DB: Updating item: {:?}", item);
|
debug!("DB: Updating item: {:?}", item);
|
||||||
conn.execute(
|
conn.execute(
|
||||||
"UPDATE items SET size=?2, compression=?3, digest_type=?4, digest_value=?5 WHERE id=?1",
|
"UPDATE items SET size=?2, compression=?3, digest_type=?4, digest_value=?5 WHERE id=?1",
|
||||||
(
|
params![item.id, item.size, item.compression, item.digest_type, item.digest_value],
|
||||||
item.id,
|
|
||||||
item.size,
|
|
||||||
item.compression,
|
|
||||||
item.digest_type,
|
|
||||||
item.digest_value,
|
|
||||||
),
|
|
||||||
)?;
|
)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn delete_item(conn: &Connection, item: Item) -> Result<()> {
|
pub fn delete_item(conn: &Connection, item: Item) -> Result<()> {
|
||||||
debug!("DB: Deleting item: {:?}", item);
|
debug!("DB: Deleting item: {:?}", item);
|
||||||
conn.execute("DELETE FROM items WHERE id=?1", [item.id])?;
|
conn.execute("DELETE FROM items WHERE id=?1", params![item.id])?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -114,7 +108,7 @@ pub fn query_delete_meta(conn: &Connection, meta: Meta) -> Result<()> {
|
|||||||
debug!("DB: Deleting meta: {:?}", meta);
|
debug!("DB: Deleting meta: {:?}", meta);
|
||||||
conn.execute(
|
conn.execute(
|
||||||
"DELETE FROM metas WHERE id=?1 AND name=?2",
|
"DELETE FROM metas WHERE id=?1 AND name=?2",
|
||||||
(meta.id, meta.name),
|
params![meta.id, meta.name],
|
||||||
)?;
|
)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -124,7 +118,7 @@ pub fn query_upsert_meta(conn: &Connection, meta: Meta) -> Result<()> {
|
|||||||
conn.execute(
|
conn.execute(
|
||||||
"INSERT INTO metas (id, name, value) VALUES (?1, ?2, ?3)
|
"INSERT INTO metas (id, name, value) VALUES (?1, ?2, ?3)
|
||||||
ON CONFLICT(id, name) DO UPDATE SET value=?3",
|
ON CONFLICT(id, name) DO UPDATE SET value=?3",
|
||||||
(meta.id, meta.name, meta.value),
|
params![meta.id, meta.name, meta.value],
|
||||||
)?;
|
)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -143,14 +137,14 @@ pub fn insert_tag(conn: &Connection, tag: Tag) -> Result<()> {
|
|||||||
debug!("DB: Inserting tag: {:?}", tag);
|
debug!("DB: Inserting tag: {:?}", tag);
|
||||||
conn.execute(
|
conn.execute(
|
||||||
"INSERT INTO tags (id, name) VALUES (?1, ?2)",
|
"INSERT INTO tags (id, name) VALUES (?1, ?2)",
|
||||||
(tag.id, tag.name),
|
params![tag.id, tag.name],
|
||||||
)?;
|
)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn delete_item_tags(conn: &Connection, item: Item) -> Result<()> {
|
pub fn delete_item_tags(conn: &Connection, item: Item) -> Result<()> {
|
||||||
debug!("DB: Deleting all item tags: {:?}", item);
|
debug!("DB: Deleting all item tags: {:?}", item);
|
||||||
conn.execute("DELETE FROM tags WHERE id=?1", [item.id])?;
|
conn.execute("DELETE FROM tags WHERE id=?1", params![item.id])?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -221,7 +215,7 @@ pub fn query_tagged_items<'a>(conn: &'a Connection, tags: &'a Vec<String>) -> Re
|
|||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let tags_ptr = Rc::new(tags_values);
|
let tags_ptr = Rc::new(tags_values);
|
||||||
let mut rows = statement.query((&tags_ptr, &tags.len()))?;
|
let mut rows = statement.query(params![&tags_ptr, &tags.len()])?;
|
||||||
|
|
||||||
let mut items = Vec::new();
|
let mut items = Vec::new();
|
||||||
|
|
||||||
@@ -327,7 +321,7 @@ pub fn get_item_matching(
|
|||||||
|
|
||||||
let tags_ptr = Rc::new(tags_values);
|
let tags_ptr = Rc::new(tags_values);
|
||||||
|
|
||||||
let mut rows = statement.query((&tags_ptr, &tags.len()))?;
|
let mut rows = statement.query(params![&tags_ptr, &tags.len()])?;
|
||||||
|
|
||||||
match rows.next()? {
|
match rows.next()? {
|
||||||
Some(row) => Ok(Some(Item {
|
Some(row) => Ok(Some(Item {
|
||||||
|
|||||||
Reference in New Issue
Block a user