Merge pull request #1262 from gwenn/parameters

Use ?NNN format instead of ?
This commit is contained in:
gwenn 2022-11-27 09:58:56 +01:00 committed by GitHub
commit 51a69b1b74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 85 additions and 85 deletions

View File

@ -134,7 +134,7 @@
//! // Insert another BLOB, this time using a parameter passed in from //! // Insert another BLOB, this time using a parameter passed in from
//! // rust (potentially with a dynamic size). //! // rust (potentially with a dynamic size).
//! db.execute( //! db.execute(
//! "INSERT INTO test_table (content) VALUES (?)", //! "INSERT INTO test_table (content) VALUES (?1)",
//! [ZeroBlob(64)], //! [ZeroBlob(64)],
//! )?; //! )?;
//! //!
@ -175,7 +175,7 @@
//! // Insert another blob, this time using a parameter passed in from //! // Insert another blob, this time using a parameter passed in from
//! // rust (potentially with a dynamic size). //! // rust (potentially with a dynamic size).
//! db.execute( //! db.execute(
//! "INSERT INTO test_table (content) VALUES (?)", //! "INSERT INTO test_table (content) VALUES (?1)",
//! [ZeroBlob(64)], //! [ZeroBlob(64)],
//! )?; //! )?;
//! //!

View File

@ -17,13 +17,13 @@ impl Connection {
/// # use rusqlite::{Connection, Result}; /// # use rusqlite::{Connection, Result};
/// fn insert_new_people(conn: &Connection) -> Result<()> { /// fn insert_new_people(conn: &Connection) -> Result<()> {
/// { /// {
/// let mut stmt = conn.prepare_cached("INSERT INTO People (name) VALUES (?)")?; /// let mut stmt = conn.prepare_cached("INSERT INTO People (name) VALUES (?1)")?;
/// stmt.execute(["Joe Smith"])?; /// stmt.execute(["Joe Smith"])?;
/// } /// }
/// { /// {
/// // This will return the same underlying SQLite statement handle without /// // This will return the same underlying SQLite statement handle without
/// // having to prepare it again. /// // having to prepare it again.
/// let mut stmt = conn.prepare_cached("INSERT INTO People (name) VALUES (?)")?; /// let mut stmt = conn.prepare_cached("INSERT INTO People (name) VALUES (?1)")?;
/// stmt.execute(["Bob Jones"])?; /// stmt.execute(["Bob Jones"])?;
/// } /// }
/// Ok(()) /// Ok(())

View File

@ -534,7 +534,7 @@ impl Connection {
/// ```rust,no_run /// ```rust,no_run
/// # use rusqlite::{Connection}; /// # use rusqlite::{Connection};
/// fn update_rows(conn: &Connection) { /// fn update_rows(conn: &Connection) {
/// match conn.execute("UPDATE foo SET bar = 'baz' WHERE qux = ?", [1i32]) { /// match conn.execute("UPDATE foo SET bar = 'baz' WHERE qux = ?1", [1i32]) {
/// Ok(updated) => println!("{} rows were updated", updated), /// Ok(updated) => println!("{} rows were updated", updated),
/// Err(err) => println!("update failed: {}", err), /// Err(err) => println!("update failed: {}", err),
/// } /// }
@ -739,7 +739,7 @@ impl Connection {
/// ```rust,no_run /// ```rust,no_run
/// # use rusqlite::{Connection, Result}; /// # use rusqlite::{Connection, Result};
/// fn insert_new_people(conn: &Connection) -> Result<()> { /// fn insert_new_people(conn: &Connection) -> Result<()> {
/// let mut stmt = conn.prepare("INSERT INTO People (name) VALUES (?)")?; /// let mut stmt = conn.prepare("INSERT INTO People (name) VALUES (?1)")?;
/// stmt.execute(["Joe Smith"])?; /// stmt.execute(["Joe Smith"])?;
/// stmt.execute(["Bob Jones"])?; /// stmt.execute(["Bob Jones"])?;
/// Ok(()) /// Ok(())
@ -1406,8 +1406,8 @@ mod test {
let db = Connection::open_in_memory()?; let db = Connection::open_in_memory()?;
db.execute_batch("CREATE TABLE foo(x INTEGER)")?; db.execute_batch("CREATE TABLE foo(x INTEGER)")?;
assert_eq!(1, db.execute("INSERT INTO foo(x) VALUES (?)", [1i32])?); assert_eq!(1, db.execute("INSERT INTO foo(x) VALUES (?1)", [1i32])?);
assert_eq!(1, db.execute("INSERT INTO foo(x) VALUES (?)", [2i32])?); assert_eq!(1, db.execute("INSERT INTO foo(x) VALUES (?1)", [2i32])?);
assert_eq!(3i32, db.one_column::<i32>("SELECT SUM(x) FROM foo")?); assert_eq!(3i32, db.one_column::<i32>("SELECT SUM(x) FROM foo")?);
Ok(()) Ok(())
@ -1417,7 +1417,7 @@ mod test {
#[cfg(feature = "extra_check")] #[cfg(feature = "extra_check")]
fn test_execute_select() { fn test_execute_select() {
let db = checked_memory_handle(); let db = checked_memory_handle();
let err = db.execute("SELECT 1 WHERE 1 < ?", [1i32]).unwrap_err(); let err = db.execute("SELECT 1 WHERE 1 < ?1", [1i32]).unwrap_err();
assert_eq!( assert_eq!(
err, err,
Error::ExecuteReturnedResults, Error::ExecuteReturnedResults,
@ -1462,7 +1462,7 @@ mod test {
let db = Connection::open_in_memory()?; let db = Connection::open_in_memory()?;
db.execute_batch("CREATE TABLE foo(x INTEGER);")?; db.execute_batch("CREATE TABLE foo(x INTEGER);")?;
let mut insert_stmt = db.prepare("INSERT INTO foo(x) VALUES(?)")?; let mut insert_stmt = db.prepare("INSERT INTO foo(x) VALUES(?1)")?;
assert_eq!(insert_stmt.execute([1i32])?, 1); assert_eq!(insert_stmt.execute([1i32])?, 1);
assert_eq!(insert_stmt.execute([2i32])?, 1); assert_eq!(insert_stmt.execute([2i32])?, 1);
assert_eq!(insert_stmt.execute([3i32])?, 1); assert_eq!(insert_stmt.execute([3i32])?, 1);
@ -1471,7 +1471,7 @@ mod test {
assert_eq!(insert_stmt.execute(["goodbye"])?, 1); assert_eq!(insert_stmt.execute(["goodbye"])?, 1);
assert_eq!(insert_stmt.execute([types::Null])?, 1); assert_eq!(insert_stmt.execute([types::Null])?, 1);
let mut update_stmt = db.prepare("UPDATE foo SET x=? WHERE x<?")?; let mut update_stmt = db.prepare("UPDATE foo SET x=?1 WHERE x<?2")?;
assert_eq!(update_stmt.execute([3i32, 3i32])?, 2); assert_eq!(update_stmt.execute([3i32, 3i32])?, 2);
assert_eq!(update_stmt.execute([3i32, 3i32])?, 0); assert_eq!(update_stmt.execute([3i32, 3i32])?, 0);
assert_eq!(update_stmt.execute([8i32, 8i32])?, 3); assert_eq!(update_stmt.execute([8i32, 8i32])?, 3);
@ -1483,12 +1483,12 @@ mod test {
let db = Connection::open_in_memory()?; let db = Connection::open_in_memory()?;
db.execute_batch("CREATE TABLE foo(x INTEGER);")?; db.execute_batch("CREATE TABLE foo(x INTEGER);")?;
let mut insert_stmt = db.prepare("INSERT INTO foo(x) VALUES(?)")?; let mut insert_stmt = db.prepare("INSERT INTO foo(x) VALUES(?1)")?;
assert_eq!(insert_stmt.execute([1i32])?, 1); assert_eq!(insert_stmt.execute([1i32])?, 1);
assert_eq!(insert_stmt.execute([2i32])?, 1); assert_eq!(insert_stmt.execute([2i32])?, 1);
assert_eq!(insert_stmt.execute([3i32])?, 1); assert_eq!(insert_stmt.execute([3i32])?, 1);
let mut query = db.prepare("SELECT x FROM foo WHERE x < ? ORDER BY x DESC")?; let mut query = db.prepare("SELECT x FROM foo WHERE x < ?1 ORDER BY x DESC")?;
{ {
let mut rows = query.query([4i32])?; let mut rows = query.query([4i32])?;
let mut v = Vec::<i32>::new(); let mut v = Vec::<i32>::new();
@ -1753,7 +1753,7 @@ mod test {
let db = Connection::open_in_memory()?; let db = Connection::open_in_memory()?;
db.execute_batch("CREATE TABLE foo(i, x);")?; db.execute_batch("CREATE TABLE foo(i, x);")?;
let vals = ["foobar", "1234", "qwerty"]; let vals = ["foobar", "1234", "qwerty"];
let mut insert_stmt = db.prepare("INSERT INTO foo(i, x) VALUES(?, ?)")?; let mut insert_stmt = db.prepare("INSERT INTO foo(i, x) VALUES(?1, ?2)")?;
for (i, v) in vals.iter().enumerate() { for (i, v) in vals.iter().enumerate() {
let i_to_insert = i as i64; let i_to_insert = i as i64;
assert_eq!(insert_stmt.execute(params![i_to_insert, v])?, 1); assert_eq!(insert_stmt.execute(params![i_to_insert, v])?, 1);
@ -2019,7 +2019,7 @@ mod test {
let db = Connection::open_in_memory()?; let db = Connection::open_in_memory()?;
db.execute_batch("CREATE TABLE foo(x INTEGER);")?; db.execute_batch("CREATE TABLE foo(x INTEGER);")?;
let b: Box<dyn ToSql> = Box::new(5); let b: Box<dyn ToSql> = Box::new(5);
db.execute("INSERT INTO foo VALUES(?)", [b])?; db.execute("INSERT INTO foo VALUES(?1)", [b])?;
db.query_row("SELECT x FROM foo", [], |r| { db.query_row("SELECT x FROM foo", [], |r| {
assert_eq!(5, r.get_unwrap::<_, i32>(0)); assert_eq!(5, r.get_unwrap::<_, i32>(0));
Ok(()) Ok(())
@ -2031,10 +2031,10 @@ mod test {
let db = Connection::open_in_memory()?; let db = Connection::open_in_memory()?;
db.query_row( db.query_row(
"SELECT "SELECT
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10,
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?11, ?12, ?13, ?14, ?15, ?16, ?17, ?18, ?19, ?20,
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?21, ?22, ?23, ?24, ?25, ?26, ?27, ?28, ?29, ?30,
?, ?, ?, ?;", ?31, ?32, ?33, ?34;",
params![ params![
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,

View File

@ -70,7 +70,7 @@ use sealed::Sealed;
/// ```rust,no_run /// ```rust,no_run
/// # use rusqlite::{Connection, Result, params}; /// # use rusqlite::{Connection, Result, params};
/// fn update_rows(conn: &Connection) -> Result<()> { /// fn update_rows(conn: &Connection) -> Result<()> {
/// let mut stmt = conn.prepare("INSERT INTO test (a, b) VALUES (?, ?)")?; /// let mut stmt = conn.prepare("INSERT INTO test (a, b) VALUES (?1, ?2)")?;
/// ///
/// // Using a tuple: /// // Using a tuple:
/// stmt.execute((0, "foobar"))?; /// stmt.execute((0, "foobar"))?;
@ -357,7 +357,7 @@ impl_for_array_ref!(
/// fn query(conn: &Connection, ids: &BTreeSet<String>) -> Result<()> { /// fn query(conn: &Connection, ids: &BTreeSet<String>) -> Result<()> {
/// assert_eq!(ids.len(), 3, "Unrealistic sample code"); /// assert_eq!(ids.len(), 3, "Unrealistic sample code");
/// ///
/// let mut stmt = conn.prepare("SELECT * FROM users WHERE id IN (?, ?, ?)")?; /// let mut stmt = conn.prepare("SELECT * FROM users WHERE id IN (?1, ?2, ?3)")?;
/// let _rows = stmt.query(params_from_iter(ids.iter()))?; /// let _rows = stmt.query(params_from_iter(ids.iter()))?;
/// ///
/// // use _rows... /// // use _rows...

View File

@ -211,7 +211,7 @@ impl Connection {
/// (e.g. `integrity_check`). /// (e.g. `integrity_check`).
/// ///
/// Prefer [PRAGMA function](https://sqlite.org/pragma.html#pragfunc) introduced in SQLite 3.20: /// Prefer [PRAGMA function](https://sqlite.org/pragma.html#pragfunc) introduced in SQLite 3.20:
/// `SELECT * FROM pragma_table_info(?);` /// `SELECT * FROM pragma_table_info(?1);`
pub fn pragma<F, V>( pub fn pragma<F, V>(
&self, &self,
schema_name: Option<DatabaseName<'_>>, schema_name: Option<DatabaseName<'_>>,
@ -379,7 +379,7 @@ mod test {
#[cfg(feature = "modern_sqlite")] #[cfg(feature = "modern_sqlite")]
fn pragma_func() -> Result<()> { fn pragma_func() -> Result<()> {
let db = Connection::open_in_memory()?; let db = Connection::open_in_memory()?;
let mut table_info = db.prepare("SELECT * FROM pragma_table_info(?)")?; let mut table_info = db.prepare("SELECT * FROM pragma_table_info(?1)")?;
let mut columns = Vec::new(); let mut columns = Vec::new();
let mut rows = table_info.query(["sqlite_master"])?; let mut rows = table_info.query(["sqlite_master"])?;

View File

@ -779,7 +779,7 @@ mod test {
assert!(session.is_empty()); assert!(session.is_empty());
session.attach(None)?; session.attach(None)?;
db.execute("INSERT INTO foo (t) VALUES (?);", ["bar"])?; db.execute("INSERT INTO foo (t) VALUES (?1);", ["bar"])?;
session.changeset() session.changeset()
} }
@ -792,7 +792,7 @@ mod test {
assert!(session.is_empty()); assert!(session.is_empty());
session.attach(None)?; session.attach(None)?;
db.execute("INSERT INTO foo (t) VALUES (?);", ["bar"])?; db.execute("INSERT INTO foo (t) VALUES (?1);", ["bar"])?;
let mut output = Vec::new(); let mut output = Vec::new();
session.changeset_strm(&mut output)?; session.changeset_strm(&mut output)?;
@ -852,7 +852,7 @@ mod test {
)?; )?;
assert!(!CALLED.load(Ordering::Relaxed)); assert!(!CALLED.load(Ordering::Relaxed));
let check = db.query_row("SELECT 1 FROM foo WHERE t = ?", ["bar"], |row| { let check = db.query_row("SELECT 1 FROM foo WHERE t = ?1", ["bar"], |row| {
row.get::<_, i32>(0) row.get::<_, i32>(0)
})?; })?;
assert_eq!(1, check); assert_eq!(1, check);
@ -887,7 +887,7 @@ mod test {
|_conflict_type, _item| ConflictAction::SQLITE_CHANGESET_OMIT, |_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 = ?1", ["bar"], |row| {
row.get::<_, i32>(0) row.get::<_, i32>(0)
})?; })?;
assert_eq!(1, check); assert_eq!(1, check);
@ -903,7 +903,7 @@ mod test {
assert!(session.is_empty()); assert!(session.is_empty());
session.attach(None)?; session.attach(None)?;
db.execute("INSERT INTO foo (t) VALUES (?);", ["bar"])?; db.execute("INSERT INTO foo (t) VALUES (?1);", ["bar"])?;
assert!(!session.is_empty()); assert!(!session.is_empty());
Ok(()) Ok(())

View File

@ -33,7 +33,7 @@ impl Statement<'_> {
/// ```rust,no_run /// ```rust,no_run
/// # use rusqlite::{Connection, Result, params}; /// # use rusqlite::{Connection, Result, params};
/// fn update_rows(conn: &Connection) -> Result<()> { /// fn update_rows(conn: &Connection) -> Result<()> {
/// let mut stmt = conn.prepare("UPDATE foo SET bar = ? WHERE qux = ?")?; /// let mut stmt = conn.prepare("UPDATE foo SET bar = ?1 WHERE qux = ?2")?;
/// // For a single parameter, or a parameter where all the values have /// // For a single parameter, or a parameter where all the values have
/// // the same type, just passing an array is simplest. /// // the same type, just passing an array is simplest.
/// stmt.execute([2i32])?; /// stmt.execute([2i32])?;
@ -58,7 +58,7 @@ impl Statement<'_> {
/// fn store_file(conn: &Connection, path: &str, data: &[u8]) -> Result<()> { /// fn store_file(conn: &Connection, path: &str, data: &[u8]) -> Result<()> {
/// # // no need to do it for real. /// # // no need to do it for real.
/// # fn sha256(_: &[u8]) -> [u8; 32] { [0; 32] } /// # fn sha256(_: &[u8]) -> [u8; 32] { [0; 32] }
/// let query = "INSERT OR REPLACE INTO files(path, hash, data) VALUES (?, ?, ?)"; /// let query = "INSERT OR REPLACE INTO files(path, hash, data) VALUES (?1, ?2, ?3)";
/// let mut stmt = conn.prepare_cached(query)?; /// let mut stmt = conn.prepare_cached(query)?;
/// let hash: [u8; 32] = sha256(data); /// let hash: [u8; 32] = sha256(data);
/// // The easiest way to pass positional parameters of have several /// // The easiest way to pass positional parameters of have several
@ -193,7 +193,7 @@ impl Statement<'_> {
/// ```rust,no_run /// ```rust,no_run
/// # use rusqlite::{Connection, Result}; /// # use rusqlite::{Connection, Result};
/// fn query(conn: &Connection, name: &str) -> Result<()> { /// fn query(conn: &Connection, name: &str) -> Result<()> {
/// let mut stmt = conn.prepare("SELECT * FROM test where name = ?")?; /// let mut stmt = conn.prepare("SELECT * FROM test where name = ?1")?;
/// let mut rows = stmt.query(rusqlite::params![name])?; /// let mut rows = stmt.query(rusqlite::params![name])?;
/// while let Some(row) = rows.next()? { /// while let Some(row) = rows.next()? {
/// // ... /// // ...
@ -207,7 +207,7 @@ impl Statement<'_> {
/// ```rust,no_run /// ```rust,no_run
/// # use rusqlite::{Connection, Result}; /// # use rusqlite::{Connection, Result};
/// fn query(conn: &Connection, name: &str) -> Result<()> { /// fn query(conn: &Connection, name: &str) -> Result<()> {
/// let mut stmt = conn.prepare("SELECT * FROM test where name = ?")?; /// let mut stmt = conn.prepare("SELECT * FROM test where name = ?1")?;
/// let mut rows = stmt.query([name])?; /// let mut rows = stmt.query([name])?;
/// while let Some(row) = rows.next()? { /// while let Some(row) = rows.next()? {
/// // ... /// // ...
@ -398,7 +398,7 @@ impl Statement<'_> {
/// ```rust,no_run /// ```rust,no_run
/// # use rusqlite::{Connection, Result}; /// # use rusqlite::{Connection, Result};
/// fn get_names(conn: &Connection) -> Result<Vec<String>> { /// fn get_names(conn: &Connection) -> Result<Vec<String>> {
/// let mut stmt = conn.prepare("SELECT name FROM people WHERE id = ?")?; /// let mut stmt = conn.prepare("SELECT name FROM people WHERE id = ?1")?;
/// 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(); /// let mut persons = Vec::new();
@ -1267,7 +1267,7 @@ mod test {
fn test_insert() -> Result<()> { fn test_insert() -> Result<()> {
let db = Connection::open_in_memory()?; let db = Connection::open_in_memory()?;
db.execute_batch("CREATE TABLE foo(x INTEGER UNIQUE)")?; db.execute_batch("CREATE TABLE foo(x INTEGER UNIQUE)")?;
let mut stmt = db.prepare("INSERT OR IGNORE INTO foo (x) VALUES (?)")?; let mut stmt = db.prepare("INSERT OR IGNORE INTO foo (x) VALUES (?1)")?;
assert_eq!(stmt.insert([1i32])?, 1); assert_eq!(stmt.insert([1i32])?, 1);
assert_eq!(stmt.insert([2i32])?, 2); assert_eq!(stmt.insert([2i32])?, 2);
match stmt.insert([1i32]).unwrap_err() { match stmt.insert([1i32]).unwrap_err() {
@ -1307,7 +1307,7 @@ mod test {
INSERT INTO foo VALUES(2); INSERT INTO foo VALUES(2);
END;"; END;";
db.execute_batch(sql)?; db.execute_batch(sql)?;
let mut stmt = db.prepare("SELECT 1 FROM foo WHERE x = ?")?; let mut stmt = db.prepare("SELECT 1 FROM foo WHERE x = ?1")?;
assert!(stmt.exists([1i32])?); assert!(stmt.exists([1i32])?);
assert!(stmt.exists([2i32])?); assert!(stmt.exists([2i32])?);
assert!(!stmt.exists([0i32])?); assert!(!stmt.exists([0i32])?);
@ -1316,18 +1316,18 @@ mod test {
#[test] #[test]
fn test_tuple_params() -> Result<()> { fn test_tuple_params() -> Result<()> {
let db = Connection::open_in_memory()?; let db = Connection::open_in_memory()?;
let s = db.query_row("SELECT printf('[%s]', ?)", ("abc",), |r| { let s = db.query_row("SELECT printf('[%s]', ?1)", ("abc",), |r| {
r.get::<_, String>(0) r.get::<_, String>(0)
})?; })?;
assert_eq!(s, "[abc]"); assert_eq!(s, "[abc]");
let s = db.query_row( let s = db.query_row(
"SELECT printf('%d %s %d', ?, ?, ?)", "SELECT printf('%d %s %d', ?1, ?2, ?3)",
(1i32, "abc", 2i32), (1i32, "abc", 2i32),
|r| r.get::<_, String>(0), |r| r.get::<_, String>(0),
)?; )?;
assert_eq!(s, "1 abc 2"); assert_eq!(s, "1 abc 2");
let s = db.query_row( let s = db.query_row(
"SELECT printf('%d %s %d %d', ?, ?, ?, ?)", "SELECT printf('%d %s %d %d', ?1, ?2, ?3, ?4)",
(1, "abc", 2i32, 4i64), (1, "abc", 2i32, 4i64),
|r| r.get::<_, String>(0), |r| r.get::<_, String>(0),
)?; )?;
@ -1339,10 +1339,10 @@ mod test {
); );
let query = "SELECT printf( let query = "SELECT printf(
'%d %s | %d %s | %d %s | %d %s || %d %s | %d %s | %d %s | %d %s', '%d %s | %d %s | %d %s | %d %s || %d %s | %d %s | %d %s | %d %s',
?, ?, ?, ?, ?1, ?2, ?3, ?4,
?, ?, ?, ?, ?5, ?6, ?7, ?8,
?, ?, ?, ?, ?9, ?10, ?11, ?12,
?, ?, ?, ? ?13, ?14, ?15, ?16
)"; )";
let s = db.query_row(query, bigtup, |r| r.get::<_, String>(0))?; let s = db.query_row(query, bigtup, |r| r.get::<_, String>(0))?;
assert_eq!(s, "0 a | 1 b | 2 c | 3 d || 4 e | 5 f | 6 g | 7 h"); assert_eq!(s, "0 a | 1 b | 2 c | 3 d || 4 e | 5 f | 6 g | 7 h");
@ -1358,7 +1358,7 @@ mod test {
INSERT INTO foo VALUES(2, 4); INSERT INTO foo VALUES(2, 4);
END;"; END;";
db.execute_batch(sql)?; db.execute_batch(sql)?;
let mut stmt = db.prepare("SELECT y FROM foo WHERE x = ?")?; let mut stmt = db.prepare("SELECT y FROM foo WHERE x = ?1")?;
let y: Result<i64> = stmt.query_row([1i32], |r| r.get(0)); let y: Result<i64> = stmt.query_row([1i32], |r| r.get(0));
assert_eq!(3i64, y?); assert_eq!(3i64, y?);
Ok(()) Ok(())
@ -1395,7 +1395,7 @@ mod test {
#[test] #[test]
fn test_expanded_sql() -> Result<()> { fn test_expanded_sql() -> Result<()> {
let db = Connection::open_in_memory()?; let db = Connection::open_in_memory()?;
let stmt = db.prepare("SELECT ?")?; let stmt = db.prepare("SELECT ?1")?;
stmt.bind_parameter(&1, 1)?; stmt.bind_parameter(&1, 1)?;
assert_eq!(Some("SELECT 1".to_owned()), stmt.expanded_sql()); assert_eq!(Some("SELECT 1".to_owned()), stmt.expanded_sql());
Ok(()) Ok(())
@ -1497,7 +1497,7 @@ mod test {
assert_eq!("UTF-16le", encoding); assert_eq!("UTF-16le", encoding);
db.execute_batch("CREATE TABLE foo(x TEXT)")?; db.execute_batch("CREATE TABLE foo(x TEXT)")?;
let expected = "テスト"; let expected = "テスト";
db.execute("INSERT INTO foo(x) VALUES (?)", [&expected])?; db.execute("INSERT INTO foo(x) VALUES (?1)", [&expected])?;
let actual: String = db.one_column("SELECT x FROM foo")?; let actual: String = db.one_column("SELECT x FROM foo")?;
assert_eq!(expected, actual); assert_eq!(expected, actual);
Ok(()) Ok(())
@ -1507,7 +1507,7 @@ mod test {
fn test_nul_byte() -> Result<()> { fn test_nul_byte() -> Result<()> {
let db = Connection::open_in_memory()?; let db = Connection::open_in_memory()?;
let expected = "a\x00b"; let expected = "a\x00b";
let actual: String = db.query_row("SELECT ?", [expected], |row| row.get(0))?; let actual: String = db.query_row("SELECT ?1", [expected], |row| row.get(0))?;
assert_eq!(expected, actual); assert_eq!(expected, actual);
Ok(()) Ok(())
} }

View File

@ -144,13 +144,13 @@ mod test {
let mut db = Connection::open_in_memory()?; let mut db = Connection::open_in_memory()?;
db.trace(Some(tracer)); db.trace(Some(tracer));
{ {
let _ = db.query_row("SELECT ?", [1i32], |_| Ok(())); let _ = db.query_row("SELECT ?1", [1i32], |_| Ok(()));
let _ = db.query_row("SELECT ?", ["hello"], |_| Ok(())); let _ = db.query_row("SELECT ?1", ["hello"], |_| Ok(()));
} }
db.trace(None); db.trace(None);
{ {
let _ = db.query_row("SELECT ?", [2i32], |_| Ok(())); let _ = db.query_row("SELECT ?1", [2i32], |_| Ok(()));
let _ = db.query_row("SELECT ?", ["goodbye"], |_| Ok(())); let _ = db.query_row("SELECT ?1", ["goodbye"], |_| Ok(()));
} }
let traced_stmts = TRACED_STMTS.lock().unwrap(); let traced_stmts = TRACED_STMTS.lock().unwrap();

View File

@ -718,7 +718,7 @@ mod test {
} }
fn insert(x: i32, conn: &Connection) -> Result<usize> { fn insert(x: i32, conn: &Connection) -> Result<usize> {
conn.execute("INSERT INTO foo VALUES(?)", [x]) conn.execute("INSERT INTO foo VALUES(?1)", [x])
} }
fn assert_current_sum(x: i32, conn: &Connection) -> Result<()> { fn assert_current_sum(x: i32, conn: &Connection) -> Result<()> {

View File

@ -176,7 +176,7 @@ mod test {
fn test_naive_date() -> Result<()> { fn test_naive_date() -> Result<()> {
let db = checked_memory_handle()?; let db = checked_memory_handle()?;
let date = NaiveDate::from_ymd_opt(2016, 2, 23).unwrap(); let date = NaiveDate::from_ymd_opt(2016, 2, 23).unwrap();
db.execute("INSERT INTO foo (t) VALUES (?)", [date])?; db.execute("INSERT INTO foo (t) VALUES (?1)", [date])?;
let s: String = db.one_column("SELECT t FROM foo")?; let s: String = db.one_column("SELECT t FROM foo")?;
assert_eq!("2016-02-23", s); assert_eq!("2016-02-23", s);
@ -189,7 +189,7 @@ mod test {
fn test_naive_time() -> Result<()> { fn test_naive_time() -> Result<()> {
let db = checked_memory_handle()?; let db = checked_memory_handle()?;
let time = NaiveTime::from_hms_opt(23, 56, 4).unwrap(); let time = NaiveTime::from_hms_opt(23, 56, 4).unwrap();
db.execute("INSERT INTO foo (t) VALUES (?)", [time])?; db.execute("INSERT INTO foo (t) VALUES (?1)", [time])?;
let s: String = db.one_column("SELECT t FROM foo")?; let s: String = db.one_column("SELECT t FROM foo")?;
assert_eq!("23:56:04", s); assert_eq!("23:56:04", s);
@ -205,7 +205,7 @@ mod test {
let time = NaiveTime::from_hms_opt(23, 56, 4).unwrap(); let time = NaiveTime::from_hms_opt(23, 56, 4).unwrap();
let dt = NaiveDateTime::new(date, time); let dt = NaiveDateTime::new(date, time);
db.execute("INSERT INTO foo (t) VALUES (?)", [dt])?; db.execute("INSERT INTO foo (t) VALUES (?1)", [dt])?;
let s: String = db.one_column("SELECT t FROM foo")?; let s: String = db.one_column("SELECT t FROM foo")?;
assert_eq!("2016-02-23 23:56:04", s); assert_eq!("2016-02-23 23:56:04", s);
@ -226,7 +226,7 @@ mod test {
let dt = NaiveDateTime::new(date, time); let dt = NaiveDateTime::new(date, time);
let utc = Utc.from_utc_datetime(&dt); let utc = Utc.from_utc_datetime(&dt);
db.execute("INSERT INTO foo (t) VALUES (?)", [utc])?; db.execute("INSERT INTO foo (t) VALUES (?1)", [utc])?;
let s: String = db.one_column("SELECT t FROM foo")?; let s: String = db.one_column("SELECT t FROM foo")?;
assert_eq!("2016-02-23 23:56:04.789+00:00", s); assert_eq!("2016-02-23 23:56:04.789+00:00", s);
@ -253,7 +253,7 @@ mod test {
let dt = NaiveDateTime::new(date, time); let dt = NaiveDateTime::new(date, time);
let local = Local.from_local_datetime(&dt).single().unwrap(); let local = Local.from_local_datetime(&dt).single().unwrap();
db.execute("INSERT INTO foo (t) VALUES (?)", [local])?; db.execute("INSERT INTO foo (t) VALUES (?1)", [local])?;
// Stored string should be in UTC // Stored string should be in UTC
let s: String = db.one_column("SELECT t FROM foo")?; let s: String = db.one_column("SELECT t FROM foo")?;
@ -269,7 +269,7 @@ mod test {
let db = checked_memory_handle()?; let db = checked_memory_handle()?;
let time = DateTime::parse_from_rfc3339("2020-04-07T11:23:45+04:00").unwrap(); let time = DateTime::parse_from_rfc3339("2020-04-07T11:23:45+04:00").unwrap();
db.execute("INSERT INTO foo (t) VALUES (?)", [time])?; db.execute("INSERT INTO foo (t) VALUES (?1)", [time])?;
// Stored string should preserve timezone offset // Stored string should preserve timezone offset
let s: String = db.one_column("SELECT t FROM foo")?; let s: String = db.one_column("SELECT t FROM foo")?;
@ -298,7 +298,7 @@ mod test {
#[test] #[test]
fn test_naive_date_time_param() -> Result<()> { fn test_naive_date_time_param() -> Result<()> {
let db = checked_memory_handle()?; let db = checked_memory_handle()?;
let result: Result<bool> = db.query_row("SELECT 1 WHERE ? BETWEEN datetime('now', '-1 minute') AND datetime('now', '+1 minute')", [Utc::now().naive_utc()], |r| r.get(0)); let result: Result<bool> = db.query_row("SELECT 1 WHERE ?1 BETWEEN datetime('now', '-1 minute') AND datetime('now', '+1 minute')", [Utc::now().naive_utc()], |r| r.get(0));
result.unwrap(); result.unwrap();
Ok(()) Ok(())
} }
@ -306,7 +306,7 @@ mod test {
#[test] #[test]
fn test_date_time_param() -> Result<()> { fn test_date_time_param() -> Result<()> {
let db = checked_memory_handle()?; let db = checked_memory_handle()?;
let result: Result<bool> = db.query_row("SELECT 1 WHERE ? BETWEEN datetime('now', '-1 minute') AND datetime('now', '+1 minute')", [Utc::now()], |r| r.get(0)); let result: Result<bool> = db.query_row("SELECT 1 WHERE ?1 BETWEEN datetime('now', '-1 minute') AND datetime('now', '+1 minute')", [Utc::now()], |r| r.get(0));
result.unwrap(); result.unwrap();
Ok(()) Ok(())
} }

View File

@ -244,7 +244,7 @@ mod test {
{ {
for n in out_of_range { for n in out_of_range {
let err = db let err = db
.query_row("SELECT ?", [n], |r| r.get::<_, T>(0)) .query_row("SELECT ?1", [n], |r| r.get::<_, T>(0))
.unwrap_err(); .unwrap_err();
match err { match err {
Error::IntegralValueOutOfRange(_, value) => assert_eq!(*n, value), Error::IntegralValueOutOfRange(_, value) => assert_eq!(*n, value),
@ -254,7 +254,7 @@ mod test {
for n in in_range { for n in in_range {
assert_eq!( assert_eq!(
*n, *n,
db.query_row("SELECT ?", [n], |r| r.get::<_, T>(0)) db.query_row("SELECT ?1", [n], |r| r.get::<_, T>(0))
.unwrap() .unwrap()
.into() .into()
); );

View File

@ -102,7 +102,7 @@ mod value_ref;
/// # use rusqlite::types::{Null}; /// # use rusqlite::types::{Null};
/// ///
/// fn insert_null(conn: &Connection) -> Result<usize> { /// fn insert_null(conn: &Connection) -> Result<usize> {
/// conn.execute("INSERT INTO people (name) VALUES (?)", [Null]) /// conn.execute("INSERT INTO people (name) VALUES (?1)", [Null])
/// } /// }
/// ``` /// ```
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
@ -153,7 +153,7 @@ mod test {
let db = checked_memory_handle()?; let db = checked_memory_handle()?;
let v1234 = vec![1u8, 2, 3, 4]; let v1234 = vec![1u8, 2, 3, 4];
db.execute("INSERT INTO foo(b) VALUES (?)", [&v1234])?; db.execute("INSERT INTO foo(b) VALUES (?1)", [&v1234])?;
let v: Vec<u8> = db.one_column("SELECT b FROM foo")?; let v: Vec<u8> = db.one_column("SELECT b FROM foo")?;
assert_eq!(v, v1234); assert_eq!(v, v1234);
@ -165,7 +165,7 @@ mod test {
let db = checked_memory_handle()?; let db = checked_memory_handle()?;
let empty = vec![]; let empty = vec![];
db.execute("INSERT INTO foo(b) VALUES (?)", [&empty])?; db.execute("INSERT INTO foo(b) VALUES (?1)", [&empty])?;
let v: Vec<u8> = db.one_column("SELECT b FROM foo")?; let v: Vec<u8> = db.one_column("SELECT b FROM foo")?;
assert_eq!(v, empty); assert_eq!(v, empty);
@ -177,7 +177,7 @@ mod test {
let db = checked_memory_handle()?; let db = checked_memory_handle()?;
let s = "hello, world!"; let s = "hello, world!";
db.execute("INSERT INTO foo(t) VALUES (?)", [&s])?; db.execute("INSERT INTO foo(t) VALUES (?1)", [&s])?;
let from: String = db.one_column("SELECT t FROM foo")?; let from: String = db.one_column("SELECT t FROM foo")?;
assert_eq!(from, s); assert_eq!(from, s);
@ -189,7 +189,7 @@ mod test {
let db = checked_memory_handle()?; let db = checked_memory_handle()?;
let s = "hello, world!"; let s = "hello, world!";
db.execute("INSERT INTO foo(t) VALUES (?)", [s.to_owned()])?; db.execute("INSERT INTO foo(t) VALUES (?1)", [s.to_owned()])?;
let from: String = db.one_column("SELECT t FROM foo")?; let from: String = db.one_column("SELECT t FROM foo")?;
assert_eq!(from, s); assert_eq!(from, s);
@ -200,7 +200,7 @@ mod test {
fn test_value() -> Result<()> { fn test_value() -> Result<()> {
let db = checked_memory_handle()?; let db = checked_memory_handle()?;
db.execute("INSERT INTO foo(i) VALUES (?)", [Value::Integer(10)])?; db.execute("INSERT INTO foo(i) VALUES (?1)", [Value::Integer(10)])?;
assert_eq!(10i64, db.one_column::<i64>("SELECT i FROM foo")?); assert_eq!(10i64, db.one_column::<i64>("SELECT i FROM foo")?);
Ok(()) Ok(())
@ -213,8 +213,8 @@ mod test {
let s = Some("hello, world!"); let s = Some("hello, world!");
let b = Some(vec![1u8, 2, 3, 4]); let b = Some(vec![1u8, 2, 3, 4]);
db.execute("INSERT INTO foo(t) VALUES (?)", [&s])?; db.execute("INSERT INTO foo(t) VALUES (?1)", [&s])?;
db.execute("INSERT INTO foo(b) VALUES (?)", [&b])?; db.execute("INSERT INTO foo(b) VALUES (?1)", [&b])?;
let mut stmt = db.prepare("SELECT t, b FROM foo ORDER BY ROWID ASC")?; let mut stmt = db.prepare("SELECT t, b FROM foo ORDER BY ROWID ASC")?;
let mut rows = stmt.query([])?; let mut rows = stmt.query([])?;

View File

@ -41,7 +41,7 @@ mod test {
let json = r#"{"foo": 13, "bar": "baz"}"#; let json = r#"{"foo": 13, "bar": "baz"}"#;
let data: Value = serde_json::from_str(json).unwrap(); let data: Value = serde_json::from_str(json).unwrap();
db.execute( db.execute(
"INSERT INTO foo (t, b) VALUES (?, ?)", "INSERT INTO foo (t, b) VALUES (?1, ?2)",
[&data as &dyn ToSql, &json.as_bytes()], [&data as &dyn ToSql, &json.as_bytes()],
)?; )?;

View File

@ -91,7 +91,7 @@ mod test {
ts_vec.push(make_datetime(10_000_000_000, 0)); //November 20, 2286 ts_vec.push(make_datetime(10_000_000_000, 0)); //November 20, 2286
for ts in ts_vec { for ts in ts_vec {
db.execute("INSERT INTO foo(t) VALUES (?)", [ts])?; db.execute("INSERT INTO foo(t) VALUES (?1)", [ts])?;
let from: OffsetDateTime = db.one_column("SELECT t FROM foo")?; let from: OffsetDateTime = db.one_column("SELECT t FROM foo")?;
@ -143,7 +143,7 @@ mod test {
Ok(OffsetDateTime::parse("2013-10-07T04:23:19.120-04:00", &Rfc3339).unwrap()), Ok(OffsetDateTime::parse("2013-10-07T04:23:19.120-04:00", &Rfc3339).unwrap()),
), ),
] { ] {
let result: Result<OffsetDateTime> = db.query_row("SELECT ?", [s], |r| r.get(0)); let result: Result<OffsetDateTime> = db.query_row("SELECT ?1", [s], |r| r.get(0));
assert_eq!(result, t); assert_eq!(result, t);
} }
Ok(()) Ok(())
@ -160,7 +160,7 @@ mod test {
#[test] #[test]
fn test_param() -> Result<()> { fn test_param() -> Result<()> {
let db = Connection::open_in_memory()?; let db = Connection::open_in_memory()?;
let result: Result<bool> = db.query_row("SELECT 1 WHERE ? BETWEEN datetime('now', '-1 minute') AND datetime('now', '+1 minute')", [OffsetDateTime::now_utc()], |r| r.get(0)); let result: Result<bool> = db.query_row("SELECT 1 WHERE ?1 BETWEEN datetime('now', '-1 minute') AND datetime('now', '+1 minute')", [OffsetDateTime::now_utc()], |r| r.get(0));
result.unwrap(); result.unwrap();
Ok(()) Ok(())
} }

View File

@ -368,10 +368,10 @@ mod test {
db.execute( db.execute(
" "
INSERT INTO foo(i128, desc) VALUES INSERT INTO foo(i128, desc) VALUES
(?, 'zero'), (?1, 'zero'),
(?, 'neg one'), (?, 'neg two'), (?2, 'neg one'), (?3, 'neg two'),
(?, 'pos one'), (?, 'pos two'), (?4, 'pos one'), (?5, 'pos two'),
(?, 'min'), (?, 'max')", (?6, 'min'), (?7, 'max')",
[0i128, -1i128, -2i128, 1i128, 2i128, i128::MIN, i128::MAX], [0i128, -1i128, -2i128, 1i128, 2i128, i128::MIN, i128::MAX],
)?; )?;
@ -410,11 +410,11 @@ mod test {
let id = Uuid::new_v4(); let id = Uuid::new_v4();
db.execute( db.execute(
"INSERT INTO foo (id, label) VALUES (?, ?)", "INSERT INTO foo (id, label) VALUES (?1, ?2)",
params![id, "target"], params![id, "target"],
)?; )?;
let mut stmt = db.prepare("SELECT id, label FROM foo WHERE id = ?")?; let mut stmt = db.prepare("SELECT id, label FROM foo WHERE id = ?1")?;
let mut rows = stmt.query(params![id])?; let mut rows = stmt.query(params![id])?;
let row = rows.next()?.unwrap(); let row = rows.next()?.unwrap();

View File

@ -49,7 +49,7 @@ mod test {
let url2 = "http://www.example2.com/👌"; let url2 = "http://www.example2.com/👌";
db.execute( db.execute(
"INSERT INTO urls (i, v) VALUES (0, ?), (1, ?), (2, ?), (3, ?)", "INSERT INTO urls (i, v) VALUES (0, ?1), (1, ?2), (2, ?3), (3, ?4)",
// also insert a non-hex encoded url (which might be present if it was // also insert a non-hex encoded url (which might be present if it was
// inserted separately) // inserted separately)
params![url0, url1, url2, "illegal"], params![url0, url1, url2, "illegal"],

View File

@ -17,7 +17,7 @@
//! let v = [1i64, 2, 3, 4]; //! let v = [1i64, 2, 3, 4];
//! // Note: A `Rc<Vec<Value>>` must be used as the parameter. //! // Note: A `Rc<Vec<Value>>` must be used as the parameter.
//! let values = Rc::new(v.iter().copied().map(Value::from).collect::<Vec<Value>>()); //! let values = Rc::new(v.iter().copied().map(Value::from).collect::<Vec<Value>>());
//! let mut stmt = db.prepare("SELECT value from rarray(?);")?; //! let mut stmt = db.prepare("SELECT value from rarray(?1);")?;
//! let rows = stmt.query_map([values], |row| row.get::<_, i64>(0))?; //! let rows = stmt.query_map([values], |row| row.get::<_, i64>(0))?;
//! for value in rows { //! for value in rows {
//! println!("{}", value?); //! println!("{}", value?);
@ -206,7 +206,7 @@ mod test {
let values: Vec<Value> = v.into_iter().map(Value::from).collect(); let values: Vec<Value> = v.into_iter().map(Value::from).collect();
let ptr = Rc::new(values); let ptr = Rc::new(values);
{ {
let mut stmt = db.prepare("SELECT value from rarray(?);")?; let mut stmt = db.prepare("SELECT value from rarray(?1);")?;
let rows = stmt.query_map([&ptr], |row| row.get::<_, i64>(0))?; let rows = stmt.query_map([&ptr], |row| row.get::<_, i64>(0))?;
assert_eq!(2, Rc::strong_count(&ptr)); assert_eq!(2, Rc::strong_count(&ptr));

View File

@ -286,13 +286,13 @@ mod test {
let mut stmt = db.prepare("SELECT * FROM log;")?; let mut stmt = db.prepare("SELECT * FROM log;")?;
let mut rows = stmt.query([])?; let mut rows = stmt.query([])?;
while rows.next()?.is_some() {} while rows.next()?.is_some() {}
db.execute("DELETE FROM log WHERE a = ?", ["a1"])?; db.execute("DELETE FROM log WHERE a = ?1", ["a1"])?;
db.execute( db.execute(
"INSERT INTO log (a, b, c) VALUES (?, ?, ?)", "INSERT INTO log (a, b, c) VALUES (?1, ?2, ?3)",
["a", "b", "c"], ["a", "b", "c"],
)?; )?;
db.execute( db.execute(
"UPDATE log SET b = ?, c = ? WHERE a = ?", "UPDATE log SET b = ?1, c = ?2 WHERE a = ?3",
["bn", "cn", "a1"], ["bn", "cn", "a1"],
)?; )?;
Ok(()) Ok(())