From afcdc2b43a3818a4839522346af71635e41c5793 Mon Sep 17 00:00:00 2001 From: gwenn Date: Tue, 19 Jan 2021 21:16:08 +0100 Subject: [PATCH] Use most concise syntax for params --- src/cache.rs | 4 ++-- src/lib.rs | 12 ++++++------ src/params.rs | 2 +- src/pragma.rs | 2 +- src/session.rs | 10 +++++----- src/statement.rs | 30 +++++++++++++++--------------- src/trace.rs | 4 ++-- src/types/chrono.rs | 10 +++++----- src/types/time.rs | 2 +- src/types/url.rs | 2 +- src/vtab/array.rs | 2 +- tests/vtab.rs | 3 +-- 12 files changed, 41 insertions(+), 42 deletions(-) diff --git a/src/cache.rs b/src/cache.rs index ea00019..0347174 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -18,13 +18,13 @@ impl Connection { /// fn insert_new_people(conn: &Connection) -> Result<()> { /// { /// let mut stmt = conn.prepare_cached("INSERT INTO People (name) VALUES (?)")?; - /// stmt.execute(&["Joe Smith"])?; + /// stmt.execute(["Joe Smith"])?; /// } /// { /// // This will return the same underlying SQLite statement handle without /// // having to prepare it again. /// let mut stmt = conn.prepare_cached("INSERT INTO People (name) VALUES (?)")?; - /// stmt.execute(&["Bob Jones"])?; + /// stmt.execute(["Bob Jones"])?; /// } /// Ok(()) /// } diff --git a/src/lib.rs b/src/lib.rs index d3114ce..4635c19 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -522,7 +522,7 @@ impl Connection { /// fn insert(conn: &Connection) -> Result { /// conn.execute( /// "INSERT INTO test (name) VALUES (:name)", - /// rusqlite::named_params!{ ":name": "one" }, + /// &[(":name", "one")], /// ) /// } /// ``` @@ -673,8 +673,8 @@ impl Connection { /// # use rusqlite::{Connection, Result}; /// fn insert_new_people(conn: &Connection) -> Result<()> { /// let mut stmt = conn.prepare("INSERT INTO People (name) VALUES (?)")?; - /// stmt.execute(&["Joe Smith"])?; - /// stmt.execute(&["Bob Jones"])?; + /// stmt.execute(["Joe Smith"])?; + /// stmt.execute(["Bob Jones"])?; /// Ok(()) /// } /// ``` @@ -1084,7 +1084,7 @@ mod test { tx1.query_row("SELECT x FROM foo LIMIT 1", [], |_| Ok(()))?; tx2.query_row("SELECT x FROM foo LIMIT 1", [], |_| Ok(()))?; - tx1.execute("INSERT INTO foo VALUES(?1)", &[&1])?; + tx1.execute("INSERT INTO foo VALUES(?1)", [1])?; let _ = tx2.execute("INSERT INTO foo VALUES(?1)", [2]); let _ = tx1.commit(); @@ -1317,8 +1317,8 @@ mod test { assert_eq!(insert_stmt.execute([2i32])?, 1); assert_eq!(insert_stmt.execute([3i32])?, 1); - assert_eq!(insert_stmt.execute(["hello".to_string()])?, 1); - assert_eq!(insert_stmt.execute(["goodbye".to_string()])?, 1); + assert_eq!(insert_stmt.execute(["hello"])?, 1); + assert_eq!(insert_stmt.execute(["goodbye"])?, 1); assert_eq!(insert_stmt.execute([types::Null])?, 1); let mut update_stmt = db.prepare("UPDATE foo SET x=? WHERE x(0) })?; assert_eq!(1, check); @@ -899,7 +899,7 @@ mod test { |_conflict_type, _item| ConflictAction::SQLITE_CHANGESET_OMIT, )?; - let check = db.query_row("SELECT 1 FROM foo WHERE t = ?", &["bar"], |row| { + let check = db.query_row("SELECT 1 FROM foo WHERE t = ?", ["bar"], |row| { row.get::<_, i32>(0) })?; assert_eq!(1, check); @@ -915,7 +915,7 @@ mod test { assert!(session.is_empty()); session.attach(None)?; - db.execute("INSERT INTO foo (t) VALUES (?);", &["bar"])?; + db.execute("INSERT INTO foo (t) VALUES (?);", ["bar"])?; assert!(!session.is_empty()); Ok(()) diff --git a/src/statement.rs b/src/statement.rs index b749e26..f808ce4 100644 --- a/src/statement.rs +++ b/src/statement.rs @@ -178,7 +178,7 @@ impl Statement<'_> { /// # use rusqlite::{Connection, Result}; /// fn query(conn: &Connection, name: &str) -> Result<()> { /// let mut stmt = conn.prepare("SELECT * FROM test where name = ?")?; - /// let mut rows = stmt.query(&[name])?; + /// let mut rows = stmt.query([name])?; /// while let Some(row) = rows.next()? { /// // ... /// } @@ -192,7 +192,7 @@ impl Statement<'_> { /// # use rusqlite::{Connection, Result}; /// fn query(conn: &Connection) -> Result<()> { /// let mut stmt = conn.prepare("SELECT * FROM test where name = :name")?; - /// let mut rows = stmt.query(&[(":name", &"one")])?; + /// let mut rows = stmt.query(&[(":name", "one")])?; /// while let Some(row) = rows.next()? { /// // ... /// } @@ -352,7 +352,7 @@ impl Statement<'_> { /// fn get_names(conn: &Connection) -> Result> { /// let mut stmt = conn.prepare("SELECT name FROM people WHERE id = :id")?; /// let rows = - /// stmt.query_and_then(&[(":id", &"one")], |row| name_to_person(row.get(0)?))?; + /// stmt.query_and_then(&[(":id", "one")], |row| name_to_person(row.get(0)?))?; /// /// let mut persons = Vec::new(); /// for person_result in rows { @@ -369,7 +369,7 @@ impl Statement<'_> { /// # use rusqlite::{Connection, Result}; /// fn get_names(conn: &Connection) -> Result> { /// let mut stmt = conn.prepare("SELECT name FROM people WHERE id = ?")?; - /// let rows = stmt.query_and_then(&["one"], |row| row.get::<_, String>(0))?; + /// let rows = stmt.query_and_then(["one"], |row| row.get::<_, String>(0))?; /// /// let mut persons = Vec::new(); /// for person_result in rows { @@ -999,7 +999,7 @@ mod test { ); assert_eq!( 1i32, - stmt.query_row::(&[(":name", &"one")], |r| r.get(0))? + stmt.query_row::(&[(":name", "one")], |r| r.get(0))? ); Ok(()) } @@ -1024,7 +1024,7 @@ mod test { // plain api { - let mut rows = stmt.query(&[(":name", &"one")])?; + let mut rows = stmt.query(&[(":name", "one")])?; let id: Result = rows.next()?.unwrap().get(0); assert_eq!(Ok(1), id); } @@ -1054,7 +1054,7 @@ mod test { } // plain api { - let mut rows = stmt.query_map(&[(":name", &"one")], |row| { + let mut rows = stmt.query_map(&[(":name", "one")], |row| { let id: Result = row.get(0); id.map(|i| 2 * i) })?; @@ -1111,7 +1111,7 @@ mod test { db.execute_batch(sql)?; let mut stmt = db.prepare("SELECT id FROM test where name = :name ORDER BY id ASC")?; - let mut rows = stmt.query_and_then(&[(":name", &"one")], |row| { + let mut rows = stmt.query_and_then(&[(":name", "one")], |row| { let id: i32 = row.get(0)?; if id == 1 { Ok(id) @@ -1188,8 +1188,8 @@ mod test { db.execute_batch(sql)?; let mut stmt = db.prepare("INSERT INTO test (x, y) VALUES (:x, :y)")?; - stmt.execute(&[(":x", &"one")])?; - stmt.execute(&[(":y", &"two")])?; + stmt.execute(&[(":x", "one")])?; + stmt.execute(&[(":y", "two")])?; let result: String = db.query_row("SELECT x FROM test WHERE y = 'two'", [], |row| row.get(0))?; @@ -1202,9 +1202,9 @@ mod test { let db = Connection::open_in_memory()?; db.execute_batch("CREATE TABLE foo(x INTEGER UNIQUE)")?; let mut stmt = db.prepare("INSERT OR IGNORE INTO foo (x) VALUES (?)")?; - assert_eq!(stmt.insert(&[&1i32])?, 1); - assert_eq!(stmt.insert(&[&2i32])?, 2); - match stmt.insert(&[&1i32]).unwrap_err() { + assert_eq!(stmt.insert([1i32])?, 1); + assert_eq!(stmt.insert([2i32])?, 2); + match stmt.insert([1i32]).unwrap_err() { Error::StatementChangedRows(0) => (), err => panic!("Unexpected error {}", err), } @@ -1243,8 +1243,8 @@ mod test { db.execute_batch(sql)?; let mut stmt = db.prepare("SELECT 1 FROM foo WHERE x = ?")?; assert!(stmt.exists([1i32])?); - assert!(stmt.exists(&[&2i32])?); - assert!(!stmt.exists([&0i32])?); + assert!(stmt.exists([2i32])?); + assert!(!stmt.exists([0i32])?); Ok(()) } diff --git a/src/trace.rs b/src/trace.rs index a7668dd..8e8fd3a 100644 --- a/src/trace.rs +++ b/src/trace.rs @@ -143,8 +143,8 @@ mod test { let mut db = Connection::open_in_memory()?; db.trace(Some(tracer)); { - let _ = db.query_row("SELECT ?", &[&1i32], |_| Ok(())); - let _ = db.query_row("SELECT ?", &["hello"], |_| Ok(())); + let _ = db.query_row("SELECT ?", [1i32], |_| Ok(())); + let _ = db.query_row("SELECT ?", ["hello"], |_| Ok(())); } db.trace(None); { diff --git a/src/types/chrono.rs b/src/types/chrono.rs index 7288786..78acdf7 100644 --- a/src/types/chrono.rs +++ b/src/types/chrono.rs @@ -147,7 +147,7 @@ mod test { fn test_naive_date() -> Result<()> { let db = checked_memory_handle()?; let date = NaiveDate::from_ymd(2016, 2, 23); - db.execute("INSERT INTO foo (t) VALUES (?)", &[&date])?; + db.execute("INSERT INTO foo (t) VALUES (?)", [date])?; let s: String = db.query_row("SELECT t FROM foo", [], |r| r.get(0))?; assert_eq!("2016-02-23", s); @@ -160,7 +160,7 @@ mod test { fn test_naive_time() -> Result<()> { let db = checked_memory_handle()?; let time = NaiveTime::from_hms(23, 56, 4); - db.execute("INSERT INTO foo (t) VALUES (?)", &[&time])?; + db.execute("INSERT INTO foo (t) VALUES (?)", [time])?; let s: String = db.query_row("SELECT t FROM foo", [], |r| r.get(0))?; assert_eq!("23:56:04", s); @@ -176,7 +176,7 @@ mod test { let time = NaiveTime::from_hms(23, 56, 4); let dt = NaiveDateTime::new(date, time); - db.execute("INSERT INTO foo (t) VALUES (?)", &[&dt])?; + db.execute("INSERT INTO foo (t) VALUES (?)", [dt])?; let s: String = db.query_row("SELECT t FROM foo", [], |r| r.get(0))?; assert_eq!("2016-02-23T23:56:04", s); @@ -197,7 +197,7 @@ mod test { let dt = NaiveDateTime::new(date, time); let utc = Utc.from_utc_datetime(&dt); - db.execute("INSERT INTO foo (t) VALUES (?)", &[&utc])?; + db.execute("INSERT INTO foo (t) VALUES (?)", [utc])?; let s: String = db.query_row("SELECT t FROM foo", [], |r| r.get(0))?; assert_eq!("2016-02-23T23:56:04.789+00:00", s); @@ -226,7 +226,7 @@ mod test { let dt = NaiveDateTime::new(date, time); let local = Local.from_local_datetime(&dt).single().unwrap(); - db.execute("INSERT INTO foo (t) VALUES (?)", &[&local])?; + db.execute("INSERT INTO foo (t) VALUES (?)", [local])?; // Stored string should be in UTC let s: String = db.query_row("SELECT t FROM foo", [], |r| r.get(0))?; diff --git a/src/types/time.rs b/src/types/time.rs index 14c92e1..54bdda5 100644 --- a/src/types/time.rs +++ b/src/types/time.rs @@ -60,7 +60,7 @@ mod test { ts_vec.push(make_datetime(10_000_000_000, 0)); //November 20, 2286 for ts in ts_vec { - db.execute("INSERT INTO foo(t) VALUES (?)", &[&ts])?; + db.execute("INSERT INTO foo(t) VALUES (?)", [ts])?; let from: OffsetDateTime = db.query_row("SELECT t FROM foo", [], |r| r.get(0))?; diff --git a/src/types/url.rs b/src/types/url.rs index 8991385..fea8500 100644 --- a/src/types/url.rs +++ b/src/types/url.rs @@ -37,7 +37,7 @@ mod test { } fn get_url(db: &Connection, id: i64) -> Result { - db.query_row("SELECT v FROM urls WHERE i = ?", params![id], |r| r.get(0)) + db.query_row("SELECT v FROM urls WHERE i = ?", [id], |r| r.get(0)) } #[test] diff --git a/src/vtab/array.rs b/src/vtab/array.rs index bd3197a..713604c 100644 --- a/src/vtab/array.rs +++ b/src/vtab/array.rs @@ -18,7 +18,7 @@ //! // Note: A `Rc>` must be used as the parameter. //! let values = Rc::new(v.iter().copied().map(Value::from).collect::>()); //! let mut stmt = db.prepare("SELECT value from rarray(?);")?; -//! let rows = stmt.query_map(params![values], |row| row.get::<_, i64>(0))?; +//! let rows = stmt.query_map([values], |row| row.get::<_, i64>(0))?; //! for value in rows { //! println!("{}", value?); //! } diff --git a/tests/vtab.rs b/tests/vtab.rs index 1446c28..5c5bdef 100644 --- a/tests/vtab.rs +++ b/tests/vtab.rs @@ -3,7 +3,6 @@ #[cfg(feature = "vtab")] #[test] fn test_dummy_module() -> rusqlite::Result<()> { - use rusqlite::types::ToSql; use rusqlite::vtab::{ eponymous_only_module, sqlite3_vtab, sqlite3_vtab_cursor, Context, IndexInfo, VTab, VTabConnection, VTabCursor, Values, @@ -95,7 +94,7 @@ fn test_dummy_module() -> rusqlite::Result<()> { let mut s = db.prepare("SELECT * FROM dummy()")?; - let dummy = s.query_row(&[] as &[&dyn ToSql], |row| row.get::<_, i32>(0))?; + let dummy = s.query_row([], |row| row.get::<_, i32>(0))?; assert_eq!(1, dummy); Ok(()) }