From 4978d0120e31f902abe3b8f4fe05ca353bc80623 Mon Sep 17 00:00:00 2001 From: gwenn Date: Sat, 17 Apr 2021 11:48:01 +0200 Subject: [PATCH] Upgrade rusqlite `NO_PARAMS` has been deprecated. For homogeneous parameters, `[value1, value2, ...]` can be used directly. For heterogeneous parameters, `params![value1, value2, ...]` is preferrred. --- Cargo.toml | 2 +- src/database/sqlite/initialization.md | 7 +++---- src/database/sqlite/insert_select.md | 15 +++++++-------- src/database/sqlite/transactions.md | 16 ++++++++-------- 4 files changed, 19 insertions(+), 21 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ab2a4c78..aa66f556 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -43,7 +43,7 @@ rayon = "1.0" regex = "1.0" reqwest = { version = "0.10", features = ["blocking", "json", "stream"] } ring = "0.16.11" -rusqlite = { version = "0.22", features = ["chrono"] } +rusqlite = { version = "0.25", features = ["chrono"] } same-file = "1.0" select = "0.4" semver = "0.9" diff --git a/src/database/sqlite/initialization.md b/src/database/sqlite/initialization.md index a27c536a..24fdd4a8 100644 --- a/src/database/sqlite/initialization.md +++ b/src/database/sqlite/initialization.md @@ -9,7 +9,6 @@ Use the `rusqlite` crate to open SQLite databases. See ```rust,edition2018,no_run use rusqlite::{Connection, Result}; -use rusqlite::NO_PARAMS; fn main() -> Result<()> { let conn = Connection::open("cats.db")?; @@ -19,7 +18,7 @@ fn main() -> Result<()> { id integer primary key, name text not null unique )", - NO_PARAMS, + [], )?; conn.execute( "create table if not exists cats ( @@ -27,7 +26,7 @@ fn main() -> Result<()> { name text not null, color_id integer not null references cat_colors(id) )", - NO_PARAMS, + [], )?; Ok(()) @@ -36,4 +35,4 @@ fn main() -> Result<()> { [`Connection::open`]: https://docs.rs/rusqlite/*/rusqlite/struct.Connection.html#method.open -[documentation]: https://github.com/jgallagher/rusqlite#user-content-notes-on-building-rusqlite-and-libsqlite3-sys +[documentation]: https://github.com/rusqlite/rusqlite#user-content-notes-on-building-rusqlite-and-libsqlite3-sys diff --git a/src/database/sqlite/insert_select.md b/src/database/sqlite/insert_select.md index a1d4bb13..8e7e3395 100644 --- a/src/database/sqlite/insert_select.md +++ b/src/database/sqlite/insert_select.md @@ -7,8 +7,7 @@ This recipe inserts data into `cat_colors` and `cats` tables using the [`execute ```rust,no_run -use rusqlite::NO_PARAMS; -use rusqlite::{Connection, Result}; +use rusqlite::{params, Connection, Result}; use std::collections::HashMap; #[derive(Debug)] @@ -26,25 +25,25 @@ fn main() -> Result<()> { for (color, catnames) in &cat_colors { conn.execute( - "INSERT INTO cat_colors (name) values (?1)", - &[&color.to_string()], + "INSERT INTO cat_colors (name) VALUES (?1)", + [color], )?; - let last_id: String = conn.last_insert_rowid().to_string(); + let last_id = conn.last_insert_rowid(); for cat in catnames { conn.execute( "INSERT INTO cats (name, color_id) values (?1, ?2)", - &[&cat.to_string(), &last_id], + params![cat, last_id], )?; } } let mut stmt = conn.prepare( - "SELECT c.name, cc.name from cats c + "SELECT c.name, cc.name FROM cats c INNER JOIN cat_colors cc ON cc.id = c.color_id;", )?; - let cats = stmt.query_map(NO_PARAMS, |row| { + let cats = stmt.query_map([], |row| { Ok(Cat { name: row.get(0)?, color: row.get(1)?, diff --git a/src/database/sqlite/transactions.md b/src/database/sqlite/transactions.md index 6eb1605c..3f80d8ea 100644 --- a/src/database/sqlite/transactions.md +++ b/src/database/sqlite/transactions.md @@ -13,7 +13,7 @@ a duplicate color is made, the transaction rolls back. ```rust,edition2018,no_run -use rusqlite::{Connection, Result, NO_PARAMS}; +use rusqlite::{Connection, Result}; fn main() -> Result<()> { let mut conn = Connection::open("cats.db")?; @@ -29,9 +29,9 @@ fn main() -> Result<()> { fn successful_tx(conn: &mut Connection) -> Result<()> { let tx = conn.transaction()?; - tx.execute("delete from cat_colors", NO_PARAMS)?; - tx.execute("insert into cat_colors (name) values (?1)", &[&"lavender"])?; - tx.execute("insert into cat_colors (name) values (?1)", &[&"blue"])?; + tx.execute("delete from cat_colors", [])?; + tx.execute("insert into cat_colors (name) values (?1)", ["lavender"])?; + tx.execute("insert into cat_colors (name) values (?1)", ["blue"])?; tx.commit() } @@ -39,10 +39,10 @@ fn successful_tx(conn: &mut Connection) -> Result<()> { fn rolled_back_tx(conn: &mut Connection) -> Result<()> { let tx = conn.transaction()?; - tx.execute("delete from cat_colors", NO_PARAMS)?; - tx.execute("insert into cat_colors (name) values (?1)", &[&"lavender"])?; - tx.execute("insert into cat_colors (name) values (?1)", &[&"blue"])?; - tx.execute("insert into cat_colors (name) values (?1)", &[&"lavender"])?; + tx.execute("delete from cat_colors", [])?; + tx.execute("insert into cat_colors (name) values (?1)", ["lavender"])?; + tx.execute("insert into cat_colors (name) values (?1)", ["blue"])?; + tx.execute("insert into cat_colors (name) values (?1)", ["lavender"])?; tx.commit() }