mirror of
https://github.com/isar/rusqlite.git
synced 2024-11-22 07:09:20 +08:00
Use most concise syntax for params
This commit is contained in:
parent
47134e9527
commit
afcdc2b43a
@ -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(())
|
||||
/// }
|
||||
|
12
src/lib.rs
12
src/lib.rs
@ -522,7 +522,7 @@ impl Connection {
|
||||
/// fn insert(conn: &Connection) -> Result<usize> {
|
||||
/// 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<?")?;
|
||||
|
@ -176,7 +176,7 @@ impl Params for [&dyn ToSql; 0] {
|
||||
// Note: Can't just return `Ok(())` — `Statement::bind_parameters`
|
||||
// checks that the right number of params were passed too.
|
||||
// TODO: we should have tests for `Error::InvalidParameterCount`...
|
||||
stmt.bind_parameters(crate::params![])
|
||||
stmt.bind_parameters(&[] as &[&dyn ToSql])
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -379,7 +379,7 @@ mod test {
|
||||
let db = Connection::open_in_memory()?;
|
||||
let mut table_info = db.prepare("SELECT * FROM pragma_table_info(?)")?;
|
||||
let mut columns = Vec::new();
|
||||
let mut rows = table_info.query(&["sqlite_master"])?;
|
||||
let mut rows = table_info.query(["sqlite_master"])?;
|
||||
|
||||
while let Some(row) = rows.next()? {
|
||||
let row = row;
|
||||
|
@ -791,7 +791,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"])?;
|
||||
|
||||
session.changeset()
|
||||
}
|
||||
@ -804,7 +804,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"])?;
|
||||
|
||||
let mut output = Vec::new();
|
||||
session.changeset_strm(&mut output)?;
|
||||
@ -864,7 +864,7 @@ mod test {
|
||||
)?;
|
||||
|
||||
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 = ?", ["bar"], |row| {
|
||||
row.get::<_, i32>(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(())
|
||||
|
@ -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<Vec<Person>> {
|
||||
/// 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<Vec<String>> {
|
||||
/// 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::<i32, _, _>(&[(":name", &"one")], |r| r.get(0))?
|
||||
stmt.query_row::<i32, _, _>(&[(":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<i32> = 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<i32> = 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(())
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
{
|
||||
|
@ -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))?;
|
||||
|
@ -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))?;
|
||||
|
||||
|
@ -37,7 +37,7 @@ mod test {
|
||||
}
|
||||
|
||||
fn get_url(db: &Connection, id: i64) -> Result<Url> {
|
||||
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]
|
||||
|
@ -18,7 +18,7 @@
|
||||
//! // 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 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?);
|
||||
//! }
|
||||
|
@ -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(())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user