refactor: use params! macro for consistent and safer query parameter passing
This commit is contained in:
committed by
Andrew Phillips (aider)
parent
2694000c9b
commit
4d2ff50992
14
src/db.rs
14
src/db.rs
@@ -2,7 +2,7 @@ use anyhow::{Context, Error, Result};
|
|||||||
use chrono::prelude::*;
|
use chrono::prelude::*;
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use log::*;
|
use log::*;
|
||||||
use rusqlite::{Connection, OpenFlags};
|
use rusqlite::{Connection, OpenFlags, params};
|
||||||
use rusqlite_migration::{M, Migrations};
|
use rusqlite_migration::{M, Migrations};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
@@ -176,7 +176,7 @@ pub fn query_all_items(conn: &Connection) -> Result<Vec<Item>> {
|
|||||||
let mut statement = conn
|
let mut statement = conn
|
||||||
.prepare("SELECT id, ts, size, compression, digest_type, digest_value FROM items ORDER BY id ASC")
|
.prepare("SELECT id, ts, size, compression, digest_type, digest_value FROM items ORDER BY id ASC")
|
||||||
.context("Problem preparing SQL statement")?;
|
.context("Problem preparing SQL statement")?;
|
||||||
let mut rows = statement.query([])?;
|
let mut rows = statement.query(params![])?;
|
||||||
let mut items = Vec::new();
|
let mut items = Vec::new();
|
||||||
|
|
||||||
while let Some(row) = rows.next()? {
|
while let Some(row) = rows.next()? {
|
||||||
@@ -353,7 +353,7 @@ pub fn get_item(conn: &Connection, item_id: i64) -> Result<Option<Item>> {
|
|||||||
)
|
)
|
||||||
.context("Problem preparing SQL statement")?;
|
.context("Problem preparing SQL statement")?;
|
||||||
|
|
||||||
let mut rows = statement.query([item_id])?;
|
let mut rows = statement.query(params![item_id])?;
|
||||||
|
|
||||||
match rows.next()? {
|
match rows.next()? {
|
||||||
Some(row) => Ok(Some(Item {
|
Some(row) => Ok(Some(Item {
|
||||||
@@ -380,7 +380,7 @@ pub fn get_item_last(conn: &Connection) -> Result<Option<Item>> {
|
|||||||
)
|
)
|
||||||
.context("Problem preparing SQL statement")?;
|
.context("Problem preparing SQL statement")?;
|
||||||
|
|
||||||
let mut rows = statement.query([])?;
|
let mut rows = statement.query(params![])?;
|
||||||
|
|
||||||
match rows.next()? {
|
match rows.next()? {
|
||||||
Some(row) => Ok(Some(Item {
|
Some(row) => Ok(Some(Item {
|
||||||
@@ -400,7 +400,7 @@ pub fn get_item_tags(conn: &Connection, item: &Item) -> Result<Vec<Tag>> {
|
|||||||
let mut statement = conn
|
let mut statement = conn
|
||||||
.prepare_cached("SELECT id, name FROM tags WHERE id=?1 ORDER BY name ASC")
|
.prepare_cached("SELECT id, name FROM tags WHERE id=?1 ORDER BY name ASC")
|
||||||
.context("Problem preparing SQL statement")?;
|
.context("Problem preparing SQL statement")?;
|
||||||
let mut rows = statement.query([item.id])?;
|
let mut rows = statement.query(params![item.id])?;
|
||||||
|
|
||||||
let mut tags = Vec::new();
|
let mut tags = Vec::new();
|
||||||
|
|
||||||
@@ -419,7 +419,7 @@ pub fn get_item_meta(conn: &Connection, item: &Item) -> Result<Vec<Meta>> {
|
|||||||
let mut statement = conn
|
let mut statement = conn
|
||||||
.prepare_cached("SELECT id, name, value FROM metas WHERE id=?1 ORDER BY name ASC")
|
.prepare_cached("SELECT id, name, value FROM metas WHERE id=?1 ORDER BY name ASC")
|
||||||
.context("Problem preparing SQL statement")?;
|
.context("Problem preparing SQL statement")?;
|
||||||
let mut rows = statement.query([item.id])?;
|
let mut rows = statement.query(params![item.id])?;
|
||||||
|
|
||||||
let mut metas = Vec::new();
|
let mut metas = Vec::new();
|
||||||
|
|
||||||
@@ -439,7 +439,7 @@ pub fn get_item_meta_name(conn: &Connection, item: &Item, name: String) -> Resul
|
|||||||
let mut statement = conn
|
let mut statement = conn
|
||||||
.prepare_cached("SELECT id, name, value FROM metas WHERE id=?1 AND name=?2")
|
.prepare_cached("SELECT id, name, value FROM metas WHERE id=?1 AND name=?2")
|
||||||
.context("Problem preparing SQL statement")?;
|
.context("Problem preparing SQL statement")?;
|
||||||
let mut rows = statement.query([item.id, name])?;
|
let mut rows = statement.query(params![item.id, name])?;
|
||||||
|
|
||||||
match rows.next()? {
|
match rows.next()? {
|
||||||
Some(row) => Ok(Some(Meta {
|
Some(row) => Ok(Some(Meta {
|
||||||
|
|||||||
Reference in New Issue
Block a user