| 1 |
use std::os::raw::{c_int, c_void}; |
| 2 |
#[cfg(feature = "array")] |
| 3 |
use std::rc::Rc; |
| 4 |
use std::slice::from_raw_parts; |
| 5 |
use std::{fmt, mem, ptr, str}; |
| 6 |
|
| 7 |
use super::ffi; |
| 8 |
use super::{len_as_c_int, str_for_sqlite}; |
| 9 |
use super::{ |
| 10 |
AndThenRows, Connection, Error, MappedRows, Params, RawStatement, Result, Row, Rows, ValueRef, |
| 11 |
}; |
| 12 |
use crate::types::{ToSql, ToSqlOutput}; |
| 13 |
#[cfg(feature = "array")] |
| 14 |
use crate::vtab::array::{free_array, ARRAY_TYPE}; |
| 15 |
|
| 16 |
/// A prepared statement. |
| 17 |
pub struct Statement<'conn> { |
| 18 |
conn: &'conn Connection, |
| 19 |
pub(crate) stmt: RawStatement, |
| 20 |
} |
| 21 |
|
| 22 |
impl Statement<'_> { |
| 23 |
/// Execute the prepared statement. |
| 24 |
/// |
| 25 |
/// On success, returns the number of rows that were changed or inserted or |
| 26 |
/// deleted (via `sqlite3_changes`). |
| 27 |
/// |
| 28 |
/// ## Example |
| 29 |
/// |
| 30 |
/// ### Use with positional parameters |
| 31 |
/// |
| 32 |
/// ```rust,no_run |
| 33 |
/// # use rusqlite::{Connection, Result, params}; |
| 34 |
/// fn update_rows(conn: &Connection) -> Result<()> { |
| 35 |
/// let mut stmt = conn.prepare("UPDATE foo SET bar = ?1 WHERE qux = ?2")?; |
| 36 |
/// // For a single parameter, or a parameter where all the values have |
| 37 |
/// // the same type, just passing an array is simplest. |
| 38 |
/// stmt.execute([2i32])?; |
| 39 |
/// // The `rusqlite::params!` macro is mostly useful when the parameters do not |
| 40 |
/// // all have the same type, or if there are more than 32 parameters |
| 41 |
/// // at once, but it can be used in other cases. |
| 42 |
/// stmt.execute(params![1i32])?; |
| 43 |
/// // However, it's not required, many cases are fine as: |
| 44 |
/// stmt.execute(&[&2i32])?; |
| 45 |
/// // Or even: |
| 46 |
/// stmt.execute([2i32])?; |
| 47 |
/// // If you really want to, this is an option as well. |
| 48 |
/// stmt.execute((2i32,))?; |
| 49 |
/// Ok(()) |
| 50 |
/// } |
| 51 |
/// ``` |
| 52 |
/// |
| 53 |
/// #### Heterogeneous positional parameters |
| 54 |
/// |
| 55 |
/// ``` |
| 56 |
/// use rusqlite::{Connection, Result}; |
| 57 |
/// fn store_file(conn: &Connection, path: &str, data: &[u8]) -> Result<()> { |
| 58 |
/// # // no need to do it for real. |
| 59 |
/// # fn sha256(_: &[u8]) -> [u8; 32] { [0; 32] } |
| 60 |
/// let query = "INSERT OR REPLACE INTO files(path, hash, data) VALUES (?1, ?2, ?3)"; |
| 61 |
/// let mut stmt = conn.prepare_cached(query)?; |
| 62 |
/// let hash: [u8; 32] = sha256(data); |
| 63 |
/// // The easiest way to pass positional parameters of have several |
| 64 |
/// // different types is by using a tuple. |
| 65 |
/// stmt.execute((path, hash, data))?; |
| 66 |
/// // Using the `params!` macro also works, and supports longer parameter lists: |
| 67 |
/// stmt.execute(rusqlite::params![path, hash, data])?; |
| 68 |
/// Ok(()) |
| 69 |
/// } |
| 70 |
/// # let c = Connection::open_in_memory().unwrap(); |
| 71 |
/// # c.execute_batch("CREATE TABLE files(path TEXT PRIMARY KEY, hash BLOB, data BLOB)").unwrap(); |
| 72 |
/// # store_file(&c, "foo/bar.txt", b"bibble").unwrap(); |
| 73 |
/// # store_file(&c, "foo/baz.txt", b"bobble").unwrap(); |
| 74 |
/// ``` |
| 75 |
/// |
| 76 |
/// ### Use with named parameters |
| 77 |
/// |
| 78 |
/// ```rust,no_run |
| 79 |
/// # use rusqlite::{Connection, Result, named_params}; |
| 80 |
/// fn insert(conn: &Connection) -> Result<()> { |
| 81 |
/// let mut stmt = conn.prepare("INSERT INTO test (key, value) VALUES (:key, :value)")?; |
| 82 |
/// // The `rusqlite::named_params!` macro (like `params!`) is useful for heterogeneous |
| 83 |
/// // sets of parameters (where all parameters are not the same type), or for queries |
| 84 |
/// // with many (more than 32) statically known parameters. |
| 85 |
/// stmt.execute(named_params! { ":key": "one", ":val": 2 })?; |
| 86 |
/// // However, named parameters can also be passed like: |
| 87 |
/// stmt.execute(&[(":key", "three"), (":val", "four")])?; |
| 88 |
/// // Or even: (note that a &T is required for the value type, currently) |
| 89 |
/// stmt.execute(&[(":key", &100), (":val", &200)])?; |
| 90 |
/// Ok(()) |
| 91 |
/// } |
| 92 |
/// ``` |
| 93 |
/// |
| 94 |
/// ### Use without parameters |
| 95 |
/// |
| 96 |
/// ```rust,no_run |
| 97 |
/// # use rusqlite::{Connection, Result, params}; |
| 98 |
/// fn delete_all(conn: &Connection) -> Result<()> { |
| 99 |
/// let mut stmt = conn.prepare("DELETE FROM users")?; |
| 100 |
/// stmt.execute([])?; |
| 101 |
/// Ok(()) |
| 102 |
/// } |
| 103 |
/// ``` |
| 104 |
/// |
| 105 |
/// # Failure |
| 106 |
/// |
| 107 |
/// Will return `Err` if binding parameters fails, the executed statement |
| 108 |
/// returns rows (in which case `query` should be used instead), or the |
| 109 |
/// underlying SQLite call fails. |
| 110 |
#[inline] |
| 111 |
pub fn execute<P: Params>(&mut self, params: P) -> Result<usize> { |
| 112 |
params.__bind_in(self)?; |
| 113 |
self.execute_with_bound_parameters() |
| 114 |
} |
| 115 |
|
| 116 |
/// Execute an INSERT and return the ROWID. |
| 117 |
/// |
| 118 |
/// # Note |
| 119 |
/// |
| 120 |
/// This function is a convenience wrapper around |
| 121 |
/// [`execute()`](Statement::execute) intended for queries that insert a |
| 122 |
/// single item. It is possible to misuse this function in a way that it |
| 123 |
/// cannot detect, such as by calling it on a statement which _updates_ |
| 124 |
/// a single item rather than inserting one. Please don't do that. |
| 125 |
/// |
| 126 |
/// # Failure |
| 127 |
/// |
| 128 |
/// Will return `Err` if no row is inserted or many rows are inserted. |
| 129 |
#[inline] |
| 130 |
pub fn insert<P: Params>(&mut self, params: P) -> Result<i64> { |
| 131 |
let changes = self.execute(params)?; |
| 132 |
match changes { |
| 133 |
1 => Ok(self.conn.last_insert_rowid()), |
| 134 |
_ => Err(Error::StatementChangedRows(changes)), |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
/// Execute the prepared statement, returning a handle to the resulting |
| 139 |
/// rows. |
| 140 |
/// |
| 141 |
/// Due to lifetime restrictions, the rows handle returned by `query` does |
| 142 |
/// not implement the `Iterator` trait. Consider using |
| 143 |
/// [`query_map`](Statement::query_map) or |
| 144 |
/// [`query_and_then`](Statement::query_and_then) instead, which do. |
| 145 |
/// |
| 146 |
/// ## Example |
| 147 |
/// |
| 148 |
/// ### Use without parameters |
| 149 |
/// |
| 150 |
/// ```rust,no_run |
| 151 |
/// # use rusqlite::{Connection, Result}; |
| 152 |
/// fn get_names(conn: &Connection) -> Result<Vec<String>> { |
| 153 |
/// let mut stmt = conn.prepare("SELECT name FROM people")?; |
| 154 |
/// let mut rows = stmt.query([])?; |
| 155 |
/// |
| 156 |
/// let mut names = Vec::new(); |
| 157 |
/// while let Some(row) = rows.next()? { |
| 158 |
/// names.push(row.get(0)?); |
| 159 |
/// } |
| 160 |
/// |
| 161 |
/// Ok(names) |
| 162 |
/// } |
| 163 |
/// ``` |
| 164 |
/// |
| 165 |
/// ### Use with positional parameters |
| 166 |
/// |
| 167 |
/// ```rust,no_run |
| 168 |
/// # use rusqlite::{Connection, Result}; |
| 169 |
/// fn query(conn: &Connection, name: &str) -> Result<()> { |
| 170 |
/// let mut stmt = conn.prepare("SELECT * FROM test where name = ?1")?; |
| 171 |
/// let mut rows = stmt.query(rusqlite::params![name])?; |
| 172 |
/// while let Some(row) = rows.next()? { |
| 173 |
/// // ... |
| 174 |
/// } |
| 175 |
/// Ok(()) |
| 176 |
/// } |
| 177 |
/// ``` |
| 178 |
/// |
| 179 |
/// Or, equivalently (but without the [`crate::params!`] macro). |
| 180 |
/// |
| 181 |
/// ```rust,no_run |
| 182 |
/// # use rusqlite::{Connection, Result}; |
| 183 |
/// fn query(conn: &Connection, name: &str) -> Result<()> { |
| 184 |
/// let mut stmt = conn.prepare("SELECT * FROM test where name = ?1")?; |
| 185 |
/// let mut rows = stmt.query([name])?; |
| 186 |
/// while let Some(row) = rows.next()? { |
| 187 |
/// // ... |
| 188 |
/// } |
| 189 |
/// Ok(()) |
| 190 |
/// } |
| 191 |
/// ``` |
| 192 |
/// |
| 193 |
/// ### Use with named parameters |
| 194 |
/// |
| 195 |
/// ```rust,no_run |
| 196 |
/// # use rusqlite::{Connection, Result}; |
| 197 |
/// fn query(conn: &Connection) -> Result<()> { |
| 198 |
/// let mut stmt = conn.prepare("SELECT * FROM test where name = :name")?; |
| 199 |
/// let mut rows = stmt.query(&[(":name", "one")])?; |
| 200 |
/// while let Some(row) = rows.next()? { |
| 201 |
/// // ... |
| 202 |
/// } |
| 203 |
/// Ok(()) |
| 204 |
/// } |
| 205 |
/// ``` |
| 206 |
/// |
| 207 |
/// Note, the `named_params!` macro is provided for syntactic convenience, |
| 208 |
/// and so the above example could also be written as: |
| 209 |
/// |
| 210 |
/// ```rust,no_run |
| 211 |
/// # use rusqlite::{Connection, Result, named_params}; |
| 212 |
/// fn query(conn: &Connection) -> Result<()> { |
| 213 |
/// let mut stmt = conn.prepare("SELECT * FROM test where name = :name")?; |
| 214 |
/// let mut rows = stmt.query(named_params! { ":name": "one" })?; |
| 215 |
/// while let Some(row) = rows.next()? { |
| 216 |
/// // ... |
| 217 |
/// } |
| 218 |
/// Ok(()) |
| 219 |
/// } |
| 220 |
/// ``` |
| 221 |
/// |
| 222 |
/// ## Failure |
| 223 |
/// |
| 224 |
/// Will return `Err` if binding parameters fails. |
| 225 |
#[inline] |
| 226 |
pub fn query<P: Params>(&mut self, params: P) -> Result<Rows<'_>> { |
| 227 |
params.__bind_in(self)?; |
| 228 |
Ok(Rows::new(self)) |
| 229 |
} |
| 230 |
|
| 231 |
/// Executes the prepared statement and maps a function over the resulting |
| 232 |
/// rows, returning an iterator over the mapped function results. |
| 233 |
/// |
| 234 |
/// `f` is used to transform the _streaming_ iterator into a _standard_ |
| 235 |
/// iterator. |
| 236 |
/// |
| 237 |
/// This is equivalent to `stmt.query(params)?.mapped(f)`. |
| 238 |
/// |
| 239 |
/// ## Example |
| 240 |
/// |
| 241 |
/// ### Use with positional params |
| 242 |
/// |
| 243 |
/// ```rust,no_run |
| 244 |
/// # use rusqlite::{Connection, Result}; |
| 245 |
/// fn get_names(conn: &Connection) -> Result<Vec<String>> { |
| 246 |
/// let mut stmt = conn.prepare("SELECT name FROM people")?; |
| 247 |
/// let rows = stmt.query_map([], |row| row.get(0))?; |
| 248 |
/// |
| 249 |
/// let mut names = Vec::new(); |
| 250 |
/// for name_result in rows { |
| 251 |
/// names.push(name_result?); |
| 252 |
/// } |
| 253 |
/// |
| 254 |
/// Ok(names) |
| 255 |
/// } |
| 256 |
/// ``` |
| 257 |
/// |
| 258 |
/// ### Use with named params |
| 259 |
/// |
| 260 |
/// ```rust,no_run |
| 261 |
/// # use rusqlite::{Connection, Result}; |
| 262 |
/// fn get_names(conn: &Connection) -> Result<Vec<String>> { |
| 263 |
/// let mut stmt = conn.prepare("SELECT name FROM people WHERE id = :id")?; |
| 264 |
/// let rows = stmt.query_map(&[(":id", &"one")], |row| row.get(0))?; |
| 265 |
/// |
| 266 |
/// let mut names = Vec::new(); |
| 267 |
/// for name_result in rows { |
| 268 |
/// names.push(name_result?); |
| 269 |
/// } |
| 270 |
/// |
| 271 |
/// Ok(names) |
| 272 |
/// } |
| 273 |
/// ``` |
| 274 |
/// ## Failure |
| 275 |
/// |
| 276 |
/// Will return `Err` if binding parameters fails. |
| 277 |
pub fn query_map<T, P, F>(&mut self, params: P, f: F) -> Result<MappedRows<'_, F>> |
| 278 |
where |
| 279 |
P: Params, |
| 280 |
F: FnMut(&Row<'_>) -> Result<T>, |
| 281 |
{ |
| 282 |
self.query(params).map(|rows| rows.mapped(f)) |
| 283 |
} |
| 284 |
|
| 285 |
/// Executes the prepared statement and maps a function over the resulting |
| 286 |
/// rows, where the function returns a `Result` with `Error` type |
| 287 |
/// implementing `std::convert::From<Error>` (so errors can be unified). |
| 288 |
/// |
| 289 |
/// This is equivalent to `stmt.query(params)?.and_then(f)`. |
| 290 |
/// |
| 291 |
/// ## Example |
| 292 |
/// |
| 293 |
/// ### Use with named params |
| 294 |
/// |
| 295 |
/// ```rust,no_run |
| 296 |
/// # use rusqlite::{Connection, Result}; |
| 297 |
/// struct Person { |
| 298 |
/// name: String, |
| 299 |
/// }; |
| 300 |
/// |
| 301 |
/// fn name_to_person(name: String) -> Result<Person> { |
| 302 |
/// // ... check for valid name |
| 303 |
/// Ok(Person { name }) |
| 304 |
/// } |
| 305 |
/// |
| 306 |
/// fn get_names(conn: &Connection) -> Result<Vec<Person>> { |
| 307 |
/// let mut stmt = conn.prepare("SELECT name FROM people WHERE id = :id")?; |
| 308 |
/// let rows = stmt.query_and_then(&[(":id", "one")], |row| name_to_person(row.get(0)?))?; |
| 309 |
/// |
| 310 |
/// let mut persons = Vec::new(); |
| 311 |
/// for person_result in rows { |
| 312 |
/// persons.push(person_result?); |
| 313 |
/// } |
| 314 |
/// |
| 315 |
/// Ok(persons) |
| 316 |
/// } |
| 317 |
/// ``` |
| 318 |
/// |
| 319 |
/// ### Use with positional params |
| 320 |
/// |
| 321 |
/// ```rust,no_run |
| 322 |
/// # use rusqlite::{Connection, Result}; |
| 323 |
/// fn get_names(conn: &Connection) -> Result<Vec<String>> { |
| 324 |
/// let mut stmt = conn.prepare("SELECT name FROM people WHERE id = ?1")?; |
| 325 |
/// let rows = stmt.query_and_then(["one"], |row| row.get::<_, String>(0))?; |
| 326 |
/// |
| 327 |
/// let mut persons = Vec::new(); |
| 328 |
/// for person_result in rows { |
| 329 |
/// persons.push(person_result?); |
| 330 |
/// } |
| 331 |
/// |
| 332 |
/// Ok(persons) |
| 333 |
/// } |
| 334 |
/// ``` |
| 335 |
/// |
| 336 |
/// # Failure |
| 337 |
/// |
| 338 |
/// Will return `Err` if binding parameters fails. |
| 339 |
#[inline] |
| 340 |
pub fn query_and_then<T, E, P, F>(&mut self, params: P, f: F) -> Result<AndThenRows<'_, F>> |
| 341 |
where |
| 342 |
P: Params, |
| 343 |
E: From<Error>, |
| 344 |
F: FnMut(&Row<'_>) -> Result<T, E>, |
| 345 |
{ |
| 346 |
self.query(params).map(|rows| rows.and_then(f)) |
| 347 |
} |
| 348 |
|
| 349 |
/// Return `true` if a query in the SQL statement it executes returns one |
| 350 |
/// or more rows and `false` if the SQL returns an empty set. |
| 351 |
#[inline] |
| 352 |
pub fn exists<P: Params>(&mut self, params: P) -> Result<bool> { |
| 353 |
let mut rows = self.query(params)?; |
| 354 |
let exists = rows.next()?.is_some(); |
| 355 |
Ok(exists) |
| 356 |
} |
| 357 |
|
| 358 |
/// Convenience method to execute a query that is expected to return a |
| 359 |
/// single row. |
| 360 |
/// |
| 361 |
/// If the query returns more than one row, all rows except the first are |
| 362 |
/// ignored. |
| 363 |
/// |
| 364 |
/// Returns `Err(QueryReturnedNoRows)` if no results are returned. If the |
| 365 |
/// query truly is optional, you can call |
| 366 |
/// [`.optional()`](crate::OptionalExtension::optional) on the result of |
| 367 |
/// this to get a `Result<Option<T>>` (requires that the trait |
| 368 |
/// `rusqlite::OptionalExtension` is imported). |
| 369 |
/// |
| 370 |
/// # Failure |
| 371 |
/// |
| 372 |
/// Will return `Err` if the underlying SQLite call fails. |
| 373 |
pub fn query_row<T, P, F>(&mut self, params: P, f: F) -> Result<T> |
| 374 |
where |
| 375 |
P: Params, |
| 376 |
F: FnOnce(&Row<'_>) -> Result<T>, |
| 377 |
{ |
| 378 |
let mut rows = self.query(params)?; |
| 379 |
|
| 380 |
rows.get_expected_row().and_then(f) |
| 381 |
} |
| 382 |
|
| 383 |
/// Consumes the statement. |
| 384 |
/// |
| 385 |
/// Functionally equivalent to the `Drop` implementation, but allows |
| 386 |
/// callers to see any errors that occur. |
| 387 |
/// |
| 388 |
/// # Failure |
| 389 |
/// |
| 390 |
/// Will return `Err` if the underlying SQLite call fails. |
| 391 |
#[inline] |
| 392 |
pub fn finalize(mut self) -> Result<()> { |
| 393 |
self.finalize_() |
| 394 |
} |
| 395 |
|
| 396 |
/// Return the (one-based) index of an SQL parameter given its name. |
| 397 |
/// |
| 398 |
/// Note that the initial ":" or "$" or "@" or "?" used to specify the |
| 399 |
/// parameter is included as part of the name. |
| 400 |
/// |
| 401 |
/// ```rust,no_run |
| 402 |
/// # use rusqlite::{Connection, Result}; |
| 403 |
/// fn example(conn: &Connection) -> Result<()> { |
| 404 |
/// let stmt = conn.prepare("SELECT * FROM test WHERE name = :example")?; |
| 405 |
/// let index = stmt.parameter_index(":example")?; |
| 406 |
/// assert_eq!(index, Some(1)); |
| 407 |
/// Ok(()) |
| 408 |
/// } |
| 409 |
/// ``` |
| 410 |
/// |
| 411 |
/// # Failure |
| 412 |
/// |
| 413 |
/// Will return Err if `name` is invalid. Will return Ok(None) if the name |
| 414 |
/// is valid but not a bound parameter of this statement. |
| 415 |
#[inline] |
| 416 |
pub fn parameter_index(&self, name: &str) -> Result<Option<usize>> { |
| 417 |
Ok(self.stmt.bind_parameter_index(name)) |
| 418 |
} |
| 419 |
|
| 420 |
/// Return the SQL parameter name given its (one-based) index (the inverse |
| 421 |
/// of [`Statement::parameter_index`]). |
| 422 |
/// |
| 423 |
/// ```rust,no_run |
| 424 |
/// # use rusqlite::{Connection, Result}; |
| 425 |
/// fn example(conn: &Connection) -> Result<()> { |
| 426 |
/// let stmt = conn.prepare("SELECT * FROM test WHERE name = :example")?; |
| 427 |
/// let index = stmt.parameter_name(1); |
| 428 |
/// assert_eq!(index, Some(":example")); |
| 429 |
/// Ok(()) |
| 430 |
/// } |
| 431 |
/// ``` |
| 432 |
/// |
| 433 |
/// # Failure |
| 434 |
/// |
| 435 |
/// Will return `None` if the column index is out of bounds or if the |
| 436 |
/// parameter is positional. |
| 437 |
/// |
| 438 |
/// # Panics |
| 439 |
/// |
| 440 |
/// Panics when parameter name is not valid UTF-8. |
| 441 |
#[inline] |
| 442 |
pub fn parameter_name(&self, index: usize) -> Option<&'_ str> { |
| 443 |
self.stmt.bind_parameter_name(index as i32).map(|name| { |
| 444 |
name.to_str() |
| 445 |
.expect("Invalid UTF-8 sequence in parameter name") |
| 446 |
}) |
| 447 |
} |
| 448 |
|
| 449 |
#[inline] |
| 450 |
pub(crate) fn bind_parameters<P>(&mut self, params: P) -> Result<()> |
| 451 |
where |
| 452 |
P: IntoIterator, |
| 453 |
P::Item: ToSql, |
| 454 |
{ |
| 455 |
let expected = self.stmt.bind_parameter_count(); |
| 456 |
let mut index = 0; |
| 457 |
for p in params { |
| 458 |
index += 1; // The leftmost SQL parameter has an index of 1. |
| 459 |
if index > expected { |
| 460 |
break; |
| 461 |
} |
| 462 |
self.bind_parameter(&p, index)?; |
| 463 |
} |
| 464 |
if index != expected { |
| 465 |
Err(Error::InvalidParameterCount(index, expected)) |
| 466 |
} else { |
| 467 |
Ok(()) |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
#[inline] |
| 472 |
pub(crate) fn ensure_parameter_count(&self, n: usize) -> Result<()> { |
| 473 |
let count = self.parameter_count(); |
| 474 |
if count != n { |
| 475 |
Err(Error::InvalidParameterCount(n, count)) |
| 476 |
} else { |
| 477 |
Ok(()) |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
#[inline] |
| 482 |
pub(crate) fn bind_parameters_named<T: ?Sized + ToSql>( |
| 483 |
&mut self, |
| 484 |
params: &[(&str, &T)], |
| 485 |
) -> Result<()> { |
| 486 |
for &(name, value) in params { |
| 487 |
if let Some(i) = self.parameter_index(name)? { |
| 488 |
let ts: &dyn ToSql = &value; |
| 489 |
self.bind_parameter(ts, i)?; |
| 490 |
} else { |
| 491 |
return Err(Error::InvalidParameterName(name.into())); |
| 492 |
} |
| 493 |
} |
| 494 |
Ok(()) |
| 495 |
} |
| 496 |
|
| 497 |
/// Return the number of parameters that can be bound to this statement. |
| 498 |
#[inline] |
| 499 |
pub fn parameter_count(&self) -> usize { |
| 500 |
self.stmt.bind_parameter_count() |
| 501 |
} |
| 502 |
|
| 503 |
/// Low level API to directly bind a parameter to a given index. |
| 504 |
/// |
| 505 |
/// Note that the index is one-based, that is, the first parameter index is |
| 506 |
/// 1 and not 0. This is consistent with the SQLite API and the values given |
| 507 |
/// to parameters bound as `?NNN`. |
| 508 |
/// |
| 509 |
/// The valid values for `one_based_col_index` begin at `1`, and end at |
| 510 |
/// [`Statement::parameter_count`], inclusive. |
| 511 |
/// |
| 512 |
/// # Caveats |
| 513 |
/// |
| 514 |
/// This should not generally be used, but is available for special cases |
| 515 |
/// such as: |
| 516 |
/// |
| 517 |
/// - binding parameters where a gap exists. |
| 518 |
/// - binding named and positional parameters in the same query. |
| 519 |
/// - separating parameter binding from query execution. |
| 520 |
/// |
| 521 |
/// In general, statements that have had *any* parameters bound this way |
| 522 |
/// should have *all* parameters bound this way, and be queried or executed |
| 523 |
/// by [`Statement::raw_query`] or [`Statement::raw_execute`], other usage |
| 524 |
/// is unsupported and will likely, probably in surprising ways. |
| 525 |
/// |
| 526 |
/// That is: Do not mix the "raw" statement functions with the rest of the |
| 527 |
/// API, or the results may be surprising, and may even change in future |
| 528 |
/// versions without comment. |
| 529 |
/// |
| 530 |
/// # Example |
| 531 |
/// |
| 532 |
/// ```rust,no_run |
| 533 |
/// # use rusqlite::{Connection, Result}; |
| 534 |
/// fn query(conn: &Connection) -> Result<()> { |
| 535 |
/// let mut stmt = conn.prepare("SELECT * FROM test WHERE name = :name AND value > ?2")?; |
| 536 |
/// let name_index = stmt.parameter_index(":name")?.expect("No such parameter"); |
| 537 |
/// stmt.raw_bind_parameter(name_index, "foo")?; |
| 538 |
/// stmt.raw_bind_parameter(2, 100)?; |
| 539 |
/// let mut rows = stmt.raw_query(); |
| 540 |
/// while let Some(row) = rows.next()? { |
| 541 |
/// // ... |
| 542 |
/// } |
| 543 |
/// Ok(()) |
| 544 |
/// } |
| 545 |
/// ``` |
| 546 |
#[inline] |
| 547 |
pub fn raw_bind_parameter<T: ToSql>( |
| 548 |
&mut self, |
| 549 |
one_based_col_index: usize, |
| 550 |
param: T, |
| 551 |
) -> Result<()> { |
| 552 |
// This is the same as `bind_parameter` but slightly more ergonomic and |
| 553 |
// correctly takes `&mut self`. |
| 554 |
self.bind_parameter(¶m, one_based_col_index) |
| 555 |
} |
| 556 |
|
| 557 |
/// Low level API to execute a statement given that all parameters were |
| 558 |
/// bound explicitly with the [`Statement::raw_bind_parameter`] API. |
| 559 |
/// |
| 560 |
/// # Caveats |
| 561 |
/// |
| 562 |
/// Any unbound parameters will have `NULL` as their value. |
| 563 |
/// |
| 564 |
/// This should not generally be used outside special cases, and |
| 565 |
/// functions in the [`Statement::execute`] family should be preferred. |
| 566 |
/// |
| 567 |
/// # Failure |
| 568 |
/// |
| 569 |
/// Will return `Err` if the executed statement returns rows (in which case |
| 570 |
/// `query` should be used instead), or the underlying SQLite call fails. |
| 571 |
#[inline] |
| 572 |
pub fn raw_execute(&mut self) -> Result<usize> { |
| 573 |
self.execute_with_bound_parameters() |
| 574 |
} |
| 575 |
|
| 576 |
/// Low level API to get `Rows` for this query given that all parameters |
| 577 |
/// were bound explicitly with the [`Statement::raw_bind_parameter`] API. |
| 578 |
/// |
| 579 |
/// # Caveats |
| 580 |
/// |
| 581 |
/// Any unbound parameters will have `NULL` as their value. |
| 582 |
/// |
| 583 |
/// This should not generally be used outside special cases, and |
| 584 |
/// functions in the [`Statement::query`] family should be preferred. |
| 585 |
/// |
| 586 |
/// Note that if the SQL does not return results, [`Statement::raw_execute`] |
| 587 |
/// should be used instead. |
| 588 |
#[inline] |
| 589 |
pub fn raw_query(&mut self) -> Rows<'_> { |
| 590 |
Rows::new(self) |
| 591 |
} |
| 592 |
|
| 593 |
// generic because many of these branches can constant fold away. |
| 594 |
fn bind_parameter<P: ?Sized + ToSql>(&self, param: &P, col: usize) -> Result<()> { |
| 595 |
let value = param.to_sql()?; |
| 596 |
|
| 597 |
let ptr = unsafe { self.stmt.ptr() }; |
| 598 |
let value = match value { |
| 599 |
ToSqlOutput::Borrowed(v) => v, |
| 600 |
ToSqlOutput::Owned(ref v) => ValueRef::from(v), |
| 601 |
|
| 602 |
#[cfg(feature = "blob")] |
| 603 |
ToSqlOutput::ZeroBlob(len) => { |
| 604 |
// TODO sqlite3_bind_zeroblob64 // 3.8.11 |
| 605 |
return self |
| 606 |
.conn |
| 607 |
.decode_result(unsafe { ffi::sqlite3_bind_zeroblob(ptr, col as c_int, len) }); |
| 608 |
} |
| 609 |
#[cfg(feature = "functions")] |
| 610 |
ToSqlOutput::Arg(_) => { |
| 611 |
return Err(err!(ffi::SQLITE_MISUSE, "Unsupported value \"{value:?}\"")); |
| 612 |
} |
| 613 |
#[cfg(feature = "array")] |
| 614 |
ToSqlOutput::Array(a) => { |
| 615 |
return self.conn.decode_result(unsafe { |
| 616 |
ffi::sqlite3_bind_pointer( |
| 617 |
ptr, |
| 618 |
col as c_int, |
| 619 |
Rc::into_raw(a) as *mut c_void, |
| 620 |
ARRAY_TYPE, |
| 621 |
Some(free_array), |
| 622 |
) |
| 623 |
}); |
| 624 |
} |
| 625 |
}; |
| 626 |
self.conn.decode_result(match value { |
| 627 |
ValueRef::Null => unsafe { ffi::sqlite3_bind_null(ptr, col as c_int) }, |
| 628 |
ValueRef::Integer(i) => unsafe { ffi::sqlite3_bind_int64(ptr, col as c_int, i) }, |
| 629 |
ValueRef::Real(r) => unsafe { ffi::sqlite3_bind_double(ptr, col as c_int, r) }, |
| 630 |
ValueRef::Text(s) => unsafe { |
| 631 |
let (c_str, len, destructor) = str_for_sqlite(s)?; |
| 632 |
// TODO sqlite3_bind_text64 // 3.8.7 |
| 633 |
ffi::sqlite3_bind_text(ptr, col as c_int, c_str, len, destructor) |
| 634 |
}, |
| 635 |
ValueRef::Blob(b) => unsafe { |
| 636 |
let length = len_as_c_int(b.len())?; |
| 637 |
if length == 0 { |
| 638 |
ffi::sqlite3_bind_zeroblob(ptr, col as c_int, 0) |
| 639 |
} else { |
| 640 |
// TODO sqlite3_bind_blob64 // 3.8.7 |
| 641 |
ffi::sqlite3_bind_blob( |
| 642 |
ptr, |
| 643 |
col as c_int, |
| 644 |
b.as_ptr().cast::<c_void>(), |
| 645 |
length, |
| 646 |
ffi::SQLITE_TRANSIENT(), |
| 647 |
) |
| 648 |
} |
| 649 |
}, |
| 650 |
}) |
| 651 |
} |
| 652 |
|
| 653 |
#[inline] |
| 654 |
fn execute_with_bound_parameters(&mut self) -> Result<usize> { |
| 655 |
self.check_update()?; |
| 656 |
let r = self.stmt.step(); |
| 657 |
let rr = self.stmt.reset(); |
| 658 |
match r { |
| 659 |
ffi::SQLITE_DONE => match rr { |
| 660 |
ffi::SQLITE_OK => Ok(self.conn.changes() as usize), |
| 661 |
_ => Err(self.conn.decode_result(rr).unwrap_err()), |
| 662 |
}, |
| 663 |
ffi::SQLITE_ROW => Err(Error::ExecuteReturnedResults), |
| 664 |
_ => Err(self.conn.decode_result(r).unwrap_err()), |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
#[inline] |
| 669 |
fn finalize_(&mut self) -> Result<()> { |
| 670 |
let mut stmt = unsafe { RawStatement::new(ptr::null_mut(), 0) }; |
| 671 |
mem::swap(&mut stmt, &mut self.stmt); |
| 672 |
self.conn.decode_result(stmt.finalize()) |
| 673 |
} |
| 674 |
|
| 675 |
#[cfg(feature = "extra_check")] |
| 676 |
#[inline] |
| 677 |
fn check_update(&self) -> Result<()> { |
| 678 |
// sqlite3_column_count works for DML but not for DDL (ie ALTER) |
| 679 |
if self.column_count() > 0 && self.stmt.readonly() { |
| 680 |
return Err(Error::ExecuteReturnedResults); |
| 681 |
} |
| 682 |
Ok(()) |
| 683 |
} |
| 684 |
|
| 685 |
#[cfg(not(feature = "extra_check"))] |
| 686 |
#[inline] |
| 687 |
#[expect(clippy::unnecessary_wraps)] |
| 688 |
fn check_update(&self) -> Result<()> { |
| 689 |
Ok(()) |
| 690 |
} |
| 691 |
|
| 692 |
/// Returns a string containing the SQL text of prepared statement with |
| 693 |
/// bound parameters expanded. |
| 694 |
pub fn expanded_sql(&self) -> Option<String> { |
| 695 |
self.stmt |
| 696 |
.expanded_sql() |
| 697 |
.map(|s| s.to_string_lossy().to_string()) |
| 698 |
} |
| 699 |
|
| 700 |
/// Get the value for one of the status counters for this statement. |
| 701 |
#[inline] |
| 702 |
pub fn get_status(&self, status: StatementStatus) -> i32 { |
| 703 |
self.stmt.get_status(status, false) |
| 704 |
} |
| 705 |
|
| 706 |
/// Reset the value of one of the status counters for this statement, |
| 707 |
#[inline] |
| 708 |
/// returning the value it had before resetting. |
| 709 |
pub fn reset_status(&self, status: StatementStatus) -> i32 { |
| 710 |
self.stmt.get_status(status, true) |
| 711 |
} |
| 712 |
|
| 713 |
/// Returns 1 if the prepared statement is an EXPLAIN statement, |
| 714 |
/// or 2 if the statement is an EXPLAIN QUERY PLAN, |
| 715 |
/// or 0 if it is an ordinary statement or a NULL pointer. |
| 716 |
#[inline] |
| 717 |
#[cfg(feature = "modern_sqlite")] // 3.28.0 |
| 718 |
#[cfg_attr(docsrs, doc(cfg(feature = "modern_sqlite")))] |
| 719 |
pub fn is_explain(&self) -> i32 { |
| 720 |
self.stmt.is_explain() |
| 721 |
} |
| 722 |
|
| 723 |
/// Returns true if the statement is read only. |
| 724 |
#[inline] |
| 725 |
pub fn readonly(&self) -> bool { |
| 726 |
self.stmt.readonly() |
| 727 |
} |
| 728 |
|
| 729 |
#[cfg(feature = "extra_check")] |
| 730 |
#[inline] |
| 731 |
pub(crate) fn check_no_tail(&self) -> Result<()> { |
| 732 |
if self.stmt.has_tail() { |
| 733 |
Err(Error::MultipleStatement) |
| 734 |
} else { |
| 735 |
Ok(()) |
| 736 |
} |
| 737 |
} |
| 738 |
|
| 739 |
#[cfg(not(feature = "extra_check"))] |
| 740 |
#[inline] |
| 741 |
#[expect(clippy::unnecessary_wraps)] |
| 742 |
pub(crate) fn check_no_tail(&self) -> Result<()> { |
| 743 |
Ok(()) |
| 744 |
} |
| 745 |
|
| 746 |
/// Safety: This is unsafe, because using `sqlite3_stmt` after the |
| 747 |
/// connection has closed is illegal, but `RawStatement` does not enforce |
| 748 |
/// this, as it loses our protective `'conn` lifetime bound. |
| 749 |
#[inline] |
| 750 |
pub(crate) unsafe fn into_raw(mut self) -> RawStatement { |
| 751 |
let mut stmt = RawStatement::new(ptr::null_mut(), 0); |
| 752 |
mem::swap(&mut stmt, &mut self.stmt); |
| 753 |
stmt |
| 754 |
} |
| 755 |
|
| 756 |
/// Reset all bindings |
| 757 |
pub fn clear_bindings(&mut self) { |
| 758 |
self.stmt.clear_bindings(); |
| 759 |
} |
| 760 |
} |
| 761 |
|
| 762 |
impl fmt::Debug for Statement<'_> { |
| 763 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 764 |
let sql = if self.stmt.is_null() { |
| 765 |
Ok("") |
| 766 |
} else { |
| 767 |
self.stmt.sql().unwrap().to_str() |
| 768 |
}; |
| 769 |
f.debug_struct("Statement") |
| 770 |
.field("conn", self.conn) |
| 771 |
.field("stmt", &self.stmt) |
| 772 |
.field("sql", &sql) |
| 773 |
.finish() |
| 774 |
} |
| 775 |
} |
| 776 |
|
| 777 |
impl Drop for Statement<'_> { |
| 778 |
#[expect(unused_must_use)] |
| 779 |
#[inline] |
| 780 |
fn drop(&mut self) { |
| 781 |
self.finalize_(); |
| 782 |
} |
| 783 |
} |
| 784 |
|
| 785 |
impl Statement<'_> { |
| 786 |
#[inline] |
| 787 |
pub(super) fn new(conn: &Connection, stmt: RawStatement) -> Statement<'_> { |
| 788 |
Statement { conn, stmt } |
| 789 |
} |
| 790 |
|
| 791 |
pub(super) fn value_ref(&self, col: usize) -> ValueRef<'_> { |
| 792 |
let raw = unsafe { self.stmt.ptr() }; |
| 793 |
|
| 794 |
match self.stmt.column_type(col) { |
| 795 |
ffi::SQLITE_NULL => ValueRef::Null, |
| 796 |
ffi::SQLITE_INTEGER => { |
| 797 |
ValueRef::Integer(unsafe { ffi::sqlite3_column_int64(raw, col as c_int) }) |
| 798 |
} |
| 799 |
ffi::SQLITE_FLOAT => { |
| 800 |
ValueRef::Real(unsafe { ffi::sqlite3_column_double(raw, col as c_int) }) |
| 801 |
} |
| 802 |
ffi::SQLITE_TEXT => { |
| 803 |
let s = unsafe { |
| 804 |
// Quoting from "Using SQLite" book: |
| 805 |
// To avoid problems, an application should first extract the desired type using |
| 806 |
// a sqlite3_column_xxx() function, and then call the |
| 807 |
// appropriate sqlite3_column_bytes() function. |
| 808 |
let text = ffi::sqlite3_column_text(raw, col as c_int); |
| 809 |
let len = ffi::sqlite3_column_bytes(raw, col as c_int); |
| 810 |
assert!( |
| 811 |
!text.is_null(), |
| 812 |
"unexpected SQLITE_TEXT column type with NULL data" |
| 813 |
); |
| 814 |
from_raw_parts(text.cast::<u8>(), len as usize) |
| 815 |
}; |
| 816 |
|
| 817 |
ValueRef::Text(s) |
| 818 |
} |
| 819 |
ffi::SQLITE_BLOB => { |
| 820 |
let (blob, len) = unsafe { |
| 821 |
( |
| 822 |
ffi::sqlite3_column_blob(raw, col as c_int), |
| 823 |
ffi::sqlite3_column_bytes(raw, col as c_int), |
| 824 |
) |
| 825 |
}; |
| 826 |
|
| 827 |
assert!( |
| 828 |
len >= 0, |
| 829 |
"unexpected negative return from sqlite3_column_bytes" |
| 830 |
); |
| 831 |
if len > 0 { |
| 832 |
assert!( |
| 833 |
!blob.is_null(), |
| 834 |
"unexpected SQLITE_BLOB column type with NULL data" |
| 835 |
); |
| 836 |
ValueRef::Blob(unsafe { from_raw_parts(blob.cast::<u8>(), len as usize) }) |
| 837 |
} else { |
| 838 |
// The return value from sqlite3_column_blob() for a zero-length BLOB |
| 839 |
// is a NULL pointer. |
| 840 |
ValueRef::Blob(&[]) |
| 841 |
} |
| 842 |
} |
| 843 |
_ => unreachable!("sqlite3_column_type returned invalid value"), |
| 844 |
} |
| 845 |
} |
| 846 |
|
| 847 |
#[inline] |
| 848 |
pub(super) fn step(&self) -> Result<bool> { |
| 849 |
match self.stmt.step() { |
| 850 |
ffi::SQLITE_ROW => Ok(true), |
| 851 |
ffi::SQLITE_DONE => Ok(false), |
| 852 |
code => Err(self.conn.decode_result(code).unwrap_err()), |
| 853 |
} |
| 854 |
} |
| 855 |
|
| 856 |
#[inline] |
| 857 |
pub(super) fn reset(&self) -> Result<()> { |
| 858 |
match self.stmt.reset() { |
| 859 |
ffi::SQLITE_OK => Ok(()), |
| 860 |
code => Err(self.conn.decode_result(code).unwrap_err()), |
| 861 |
} |
| 862 |
} |
| 863 |
} |
| 864 |
|
| 865 |
/// Prepared statement status counters. |
| 866 |
/// |
| 867 |
/// See `https://www.sqlite.org/c3ref/c_stmtstatus_counter.html` |
| 868 |
/// for explanations of each. |
| 869 |
/// |
| 870 |
/// Note that depending on your version of SQLite, all of these |
| 871 |
/// may not be available. |
| 872 |
#[repr(i32)] |
| 873 |
#[derive(Clone, Copy, PartialEq, Eq)] |
| 874 |
#[non_exhaustive] |
| 875 |
pub enum StatementStatus { |
| 876 |
/// Equivalent to `SQLITE_STMTSTATUS_FULLSCAN_STEP` |
| 877 |
FullscanStep = 1, |
| 878 |
/// Equivalent to `SQLITE_STMTSTATUS_SORT` |
| 879 |
Sort = 2, |
| 880 |
/// Equivalent to `SQLITE_STMTSTATUS_AUTOINDEX` |
| 881 |
AutoIndex = 3, |
| 882 |
/// Equivalent to `SQLITE_STMTSTATUS_VM_STEP` |
| 883 |
VmStep = 4, |
| 884 |
/// Equivalent to `SQLITE_STMTSTATUS_REPREPARE` (3.20.0) |
| 885 |
RePrepare = 5, |
| 886 |
/// Equivalent to `SQLITE_STMTSTATUS_RUN` (3.20.0) |
| 887 |
Run = 6, |
| 888 |
/// Equivalent to `SQLITE_STMTSTATUS_FILTER_MISS` |
| 889 |
FilterMiss = 7, |
| 890 |
/// Equivalent to `SQLITE_STMTSTATUS_FILTER_HIT` |
| 891 |
FilterHit = 8, |
| 892 |
/// Equivalent to `SQLITE_STMTSTATUS_MEMUSED` (3.20.0) |
| 893 |
MemUsed = 99, |
| 894 |
} |
| 895 |
|
| 896 |
#[cfg(test)] |
| 897 |
mod test { |
| 898 |
use crate::types::ToSql; |
| 899 |
use crate::{params_from_iter, Connection, Error, Result}; |
| 900 |
|
| 901 |
#[test] |
| 902 |
fn test_execute_named() -> Result<()> { |
| 903 |
let db = Connection::open_in_memory()?; |
| 904 |
db.execute_batch("CREATE TABLE foo(x INTEGER)")?; |
| 905 |
|
| 906 |
assert_eq!( |
| 907 |
db.execute("INSERT INTO foo(x) VALUES (:x)", &[(":x", &1i32)])?, |
| 908 |
1 |
| 909 |
); |
| 910 |
assert_eq!( |
| 911 |
db.execute("INSERT INTO foo(x) VALUES (:x)", &[(":x", &2i32)])?, |
| 912 |
1 |
| 913 |
); |
| 914 |
assert_eq!( |
| 915 |
db.execute( |
| 916 |
"INSERT INTO foo(x) VALUES (:x)", |
| 917 |
crate::named_params! {":x": 3i32} |
| 918 |
)?, |
| 919 |
1 |
| 920 |
); |
| 921 |
|
| 922 |
assert_eq!( |
| 923 |
6i32, |
| 924 |
db.query_row::<i32, _, _>( |
| 925 |
"SELECT SUM(x) FROM foo WHERE x > :x", |
| 926 |
&[(":x", &0i32)], |
| 927 |
|r| r.get(0) |
| 928 |
)? |
| 929 |
); |
| 930 |
assert_eq!( |
| 931 |
5i32, |
| 932 |
db.query_row::<i32, _, _>( |
| 933 |
"SELECT SUM(x) FROM foo WHERE x > :x", |
| 934 |
&[(":x", &1i32)], |
| 935 |
|r| r.get(0) |
| 936 |
)? |
| 937 |
); |
| 938 |
Ok(()) |
| 939 |
} |
| 940 |
|
| 941 |
#[test] |
| 942 |
fn test_stmt_execute_named() -> Result<()> { |
| 943 |
let db = Connection::open_in_memory()?; |
| 944 |
let sql = "CREATE TABLE test (id INTEGER PRIMARY KEY NOT NULL, name TEXT NOT NULL, flag \ |
| 945 |
INTEGER)"; |
| 946 |
db.execute_batch(sql)?; |
| 947 |
|
| 948 |
let mut stmt = db.prepare("INSERT INTO test (name) VALUES (:name)")?; |
| 949 |
stmt.execute(&[(":name", &"one")])?; |
| 950 |
|
| 951 |
let mut stmt = db.prepare("SELECT COUNT(*) FROM test WHERE name = :name")?; |
| 952 |
assert_eq!( |
| 953 |
1i32, |
| 954 |
stmt.query_row::<i32, _, _>(&[(":name", "one")], |r| r.get(0))? |
| 955 |
); |
| 956 |
Ok(()) |
| 957 |
} |
| 958 |
|
| 959 |
#[test] |
| 960 |
fn test_query_named() -> Result<()> { |
| 961 |
let db = Connection::open_in_memory()?; |
| 962 |
let sql = r#" |
| 963 |
CREATE TABLE test (id INTEGER PRIMARY KEY NOT NULL, name TEXT NOT NULL, flag INTEGER); |
| 964 |
INSERT INTO test(id, name) VALUES (1, "one"); |
| 965 |
"#; |
| 966 |
db.execute_batch(sql)?; |
| 967 |
|
| 968 |
let mut stmt = db.prepare("SELECT id FROM test where name = :name")?; |
| 969 |
let mut rows = stmt.query(&[(":name", "one")])?; |
| 970 |
let id: Result<i32> = rows.next()?.unwrap().get(0); |
| 971 |
assert_eq!(Ok(1), id); |
| 972 |
Ok(()) |
| 973 |
} |
| 974 |
|
| 975 |
#[test] |
| 976 |
fn test_query_map_named() -> Result<()> { |
| 977 |
let db = Connection::open_in_memory()?; |
| 978 |
let sql = r#" |
| 979 |
CREATE TABLE test (id INTEGER PRIMARY KEY NOT NULL, name TEXT NOT NULL, flag INTEGER); |
| 980 |
INSERT INTO test(id, name) VALUES (1, "one"); |
| 981 |
"#; |
| 982 |
db.execute_batch(sql)?; |
| 983 |
|
| 984 |
let mut stmt = db.prepare("SELECT id FROM test where name = :name")?; |
| 985 |
let mut rows = stmt.query_map(&[(":name", "one")], |row| { |
| 986 |
let id: Result<i32> = row.get(0); |
| 987 |
id.map(|i| 2 * i) |
| 988 |
})?; |
| 989 |
|
| 990 |
let doubled_id: i32 = rows.next().unwrap()?; |
| 991 |
assert_eq!(2, doubled_id); |
| 992 |
Ok(()) |
| 993 |
} |
| 994 |
|
| 995 |
#[test] |
| 996 |
fn test_query_and_then_by_name() -> Result<()> { |
| 997 |
let db = Connection::open_in_memory()?; |
| 998 |
let sql = r#" |
| 999 |
CREATE TABLE test (id INTEGER PRIMARY KEY NOT NULL, name TEXT NOT NULL, flag INTEGER); |
| 1000 |
INSERT INTO test(id, name) VALUES (1, "one"); |
| 1001 |
INSERT INTO test(id, name) VALUES (2, "one"); |
| 1002 |
"#; |
| 1003 |
db.execute_batch(sql)?; |
| 1004 |
|
| 1005 |
let mut stmt = db.prepare("SELECT id FROM test where name = :name ORDER BY id ASC")?; |
| 1006 |
let mut rows = stmt.query_and_then(&[(":name", "one")], |row| { |
| 1007 |
let id: i32 = row.get(0)?; |
| 1008 |
if id == 1 { |
| 1009 |
Ok(id) |
| 1010 |
} else { |
| 1011 |
Err(Error::SqliteSingleThreadedMode) |
| 1012 |
} |
| 1013 |
})?; |
| 1014 |
|
| 1015 |
// first row should be Ok |
| 1016 |
let doubled_id: i32 = rows.next().unwrap()?; |
| 1017 |
assert_eq!(1, doubled_id); |
| 1018 |
|
| 1019 |
// second row should be an `Err` |
| 1020 |
#[expect(clippy::match_wild_err_arm)] |
| 1021 |
match rows.next().unwrap() { |
| 1022 |
Ok(_) => panic!("invalid Ok"), |
| 1023 |
Err(Error::SqliteSingleThreadedMode) => (), |
| 1024 |
Err(_) => panic!("invalid Err"), |
| 1025 |
} |
| 1026 |
Ok(()) |
| 1027 |
} |
| 1028 |
|
| 1029 |
#[test] |
| 1030 |
fn test_unbound_parameters_are_null() -> Result<()> { |
| 1031 |
let db = Connection::open_in_memory()?; |
| 1032 |
let sql = "CREATE TABLE test (x TEXT, y TEXT)"; |
| 1033 |
db.execute_batch(sql)?; |
| 1034 |
|
| 1035 |
let mut stmt = db.prepare("INSERT INTO test (x, y) VALUES (:x, :y)")?; |
| 1036 |
stmt.execute(&[(":x", &"one")])?; |
| 1037 |
|
| 1038 |
let result: Option<String> = db.one_column("SELECT y FROM test WHERE x = 'one'")?; |
| 1039 |
assert!(result.is_none()); |
| 1040 |
Ok(()) |
| 1041 |
} |
| 1042 |
|
| 1043 |
#[test] |
| 1044 |
fn test_raw_binding() -> Result<()> { |
| 1045 |
let db = Connection::open_in_memory()?; |
| 1046 |
db.execute_batch("CREATE TABLE test (name TEXT, value INTEGER)")?; |
| 1047 |
{ |
| 1048 |
let mut stmt = db.prepare("INSERT INTO test (name, value) VALUES (:name, ?3)")?; |
| 1049 |
|
| 1050 |
let name_idx = stmt.parameter_index(":name")?.unwrap(); |
| 1051 |
stmt.raw_bind_parameter(name_idx, "example")?; |
| 1052 |
stmt.raw_bind_parameter(3, 50i32)?; |
| 1053 |
let n = stmt.raw_execute()?; |
| 1054 |
assert_eq!(n, 1); |
| 1055 |
} |
| 1056 |
|
| 1057 |
{ |
| 1058 |
let mut stmt = db.prepare("SELECT name, value FROM test WHERE value = ?2")?; |
| 1059 |
stmt.raw_bind_parameter(2, 50)?; |
| 1060 |
let mut rows = stmt.raw_query(); |
| 1061 |
{ |
| 1062 |
let row = rows.next()?.unwrap(); |
| 1063 |
let name: String = row.get(0)?; |
| 1064 |
assert_eq!(name, "example"); |
| 1065 |
let value: i32 = row.get(1)?; |
| 1066 |
assert_eq!(value, 50); |
| 1067 |
} |
| 1068 |
assert!(rows.next()?.is_none()); |
| 1069 |
} |
| 1070 |
|
| 1071 |
Ok(()) |
| 1072 |
} |
| 1073 |
|
| 1074 |
#[test] |
| 1075 |
fn test_unbound_parameters_are_reused() -> Result<()> { |
| 1076 |
let db = Connection::open_in_memory()?; |
| 1077 |
let sql = "CREATE TABLE test (x TEXT, y TEXT)"; |
| 1078 |
db.execute_batch(sql)?; |
| 1079 |
|
| 1080 |
let mut stmt = db.prepare("INSERT INTO test (x, y) VALUES (:x, :y)")?; |
| 1081 |
stmt.execute(&[(":x", "one")])?; |
| 1082 |
stmt.execute(&[(":y", "two")])?; |
| 1083 |
|
| 1084 |
let result: String = db.one_column("SELECT x FROM test WHERE y = 'two'")?; |
| 1085 |
assert_eq!(result, "one"); |
| 1086 |
Ok(()) |
| 1087 |
} |
| 1088 |
|
| 1089 |
#[test] |
| 1090 |
fn test_insert() -> Result<()> { |
| 1091 |
let db = Connection::open_in_memory()?; |
| 1092 |
db.execute_batch("CREATE TABLE foo(x INTEGER UNIQUE)")?; |
| 1093 |
let mut stmt = db.prepare("INSERT OR IGNORE INTO foo (x) VALUES (?1)")?; |
| 1094 |
assert_eq!(stmt.insert([1i32])?, 1); |
| 1095 |
assert_eq!(stmt.insert([2i32])?, 2); |
| 1096 |
match stmt.insert([1i32]).unwrap_err() { |
| 1097 |
Error::StatementChangedRows(0) => (), |
| 1098 |
err => panic!("Unexpected error {err}"), |
| 1099 |
} |
| 1100 |
let mut multi = db.prepare("INSERT INTO foo (x) SELECT 3 UNION ALL SELECT 4")?; |
| 1101 |
match multi.insert([]).unwrap_err() { |
| 1102 |
Error::StatementChangedRows(2) => (), |
| 1103 |
err => panic!("Unexpected error {err}"), |
| 1104 |
} |
| 1105 |
Ok(()) |
| 1106 |
} |
| 1107 |
|
| 1108 |
#[test] |
| 1109 |
fn test_insert_different_tables() -> Result<()> { |
| 1110 |
// Test for https://github.com/rusqlite/rusqlite/issues/171 |
| 1111 |
let db = Connection::open_in_memory()?; |
| 1112 |
db.execute_batch( |
| 1113 |
r" |
| 1114 |
CREATE TABLE foo(x INTEGER); |
| 1115 |
CREATE TABLE bar(x INTEGER); |
| 1116 |
", |
| 1117 |
)?; |
| 1118 |
|
| 1119 |
assert_eq!(db.prepare("INSERT INTO foo VALUES (10)")?.insert([])?, 1); |
| 1120 |
assert_eq!(db.prepare("INSERT INTO bar VALUES (10)")?.insert([])?, 1); |
| 1121 |
Ok(()) |
| 1122 |
} |
| 1123 |
|
| 1124 |
#[test] |
| 1125 |
fn test_exists() -> Result<()> { |
| 1126 |
let db = Connection::open_in_memory()?; |
| 1127 |
let sql = "BEGIN; |
| 1128 |
CREATE TABLE foo(x INTEGER); |
| 1129 |
INSERT INTO foo VALUES(1); |
| 1130 |
INSERT INTO foo VALUES(2); |
| 1131 |
END;"; |
| 1132 |
db.execute_batch(sql)?; |
| 1133 |
let mut stmt = db.prepare("SELECT 1 FROM foo WHERE x = ?1")?; |
| 1134 |
assert!(stmt.exists([1i32])?); |
| 1135 |
assert!(stmt.exists([2i32])?); |
| 1136 |
assert!(!stmt.exists([0i32])?); |
| 1137 |
Ok(()) |
| 1138 |
} |
| 1139 |
#[test] |
| 1140 |
fn test_tuple_params() -> Result<()> { |
| 1141 |
let db = Connection::open_in_memory()?; |
| 1142 |
let s = db.query_row("SELECT printf('[%s]', ?1)", ("abc",), |r| { |
| 1143 |
r.get::<_, String>(0) |
| 1144 |
})?; |
| 1145 |
assert_eq!(s, "[abc]"); |
| 1146 |
let s = db.query_row( |
| 1147 |
"SELECT printf('%d %s %d', ?1, ?2, ?3)", |
| 1148 |
(1i32, "abc", 2i32), |
| 1149 |
|r| r.get::<_, String>(0), |
| 1150 |
)?; |
| 1151 |
assert_eq!(s, "1 abc 2"); |
| 1152 |
let s = db.query_row( |
| 1153 |
"SELECT printf('%d %s %d %d', ?1, ?2, ?3, ?4)", |
| 1154 |
(1, "abc", 2i32, 4i64), |
| 1155 |
|r| r.get::<_, String>(0), |
| 1156 |
)?; |
| 1157 |
assert_eq!(s, "1 abc 2 4"); |
| 1158 |
#[rustfmt::skip] |
| 1159 |
let bigtup = ( |
| 1160 |
0, "a", 1, "b", 2, "c", 3, "d", |
| 1161 |
4, "e", 5, "f", 6, "g", 7, "h", |
| 1162 |
); |
| 1163 |
let query = "SELECT printf( |
| 1164 |
'%d %s | %d %s | %d %s | %d %s || %d %s | %d %s | %d %s | %d %s', |
| 1165 |
?1, ?2, ?3, ?4, |
| 1166 |
?5, ?6, ?7, ?8, |
| 1167 |
?9, ?10, ?11, ?12, |
| 1168 |
?13, ?14, ?15, ?16 |
| 1169 |
)"; |
| 1170 |
let s = db.query_row(query, bigtup, |r| r.get::<_, String>(0))?; |
| 1171 |
assert_eq!(s, "0 a | 1 b | 2 c | 3 d || 4 e | 5 f | 6 g | 7 h"); |
| 1172 |
Ok(()) |
| 1173 |
} |
| 1174 |
|
| 1175 |
#[test] |
| 1176 |
fn test_query_row() -> Result<()> { |
| 1177 |
let db = Connection::open_in_memory()?; |
| 1178 |
let sql = "BEGIN; |
| 1179 |
CREATE TABLE foo(x INTEGER, y INTEGER); |
| 1180 |
INSERT INTO foo VALUES(1, 3); |
| 1181 |
INSERT INTO foo VALUES(2, 4); |
| 1182 |
END;"; |
| 1183 |
db.execute_batch(sql)?; |
| 1184 |
let mut stmt = db.prepare("SELECT y FROM foo WHERE x = ?1")?; |
| 1185 |
let y: Result<i64> = stmt.query_row([1i32], |r| r.get(0)); |
| 1186 |
assert_eq!(3i64, y?); |
| 1187 |
Ok(()) |
| 1188 |
} |
| 1189 |
|
| 1190 |
#[test] |
| 1191 |
fn test_query_by_column_name() -> Result<()> { |
| 1192 |
let db = Connection::open_in_memory()?; |
| 1193 |
let sql = "BEGIN; |
| 1194 |
CREATE TABLE foo(x INTEGER, y INTEGER); |
| 1195 |
INSERT INTO foo VALUES(1, 3); |
| 1196 |
END;"; |
| 1197 |
db.execute_batch(sql)?; |
| 1198 |
let mut stmt = db.prepare("SELECT y FROM foo")?; |
| 1199 |
let y: Result<i64> = stmt.query_row([], |r| r.get("y")); |
| 1200 |
assert_eq!(3i64, y?); |
| 1201 |
Ok(()) |
| 1202 |
} |
| 1203 |
|
| 1204 |
#[test] |
| 1205 |
fn test_query_by_column_name_ignore_case() -> Result<()> { |
| 1206 |
let db = Connection::open_in_memory()?; |
| 1207 |
let sql = "BEGIN; |
| 1208 |
CREATE TABLE foo(x INTEGER, y INTEGER); |
| 1209 |
INSERT INTO foo VALUES(1, 3); |
| 1210 |
END;"; |
| 1211 |
db.execute_batch(sql)?; |
| 1212 |
let mut stmt = db.prepare("SELECT y as Y FROM foo")?; |
| 1213 |
let y: Result<i64> = stmt.query_row([], |r| r.get("y")); |
| 1214 |
assert_eq!(3i64, y?); |
| 1215 |
Ok(()) |
| 1216 |
} |
| 1217 |
|
| 1218 |
#[test] |
| 1219 |
fn test_expanded_sql() -> Result<()> { |
| 1220 |
let db = Connection::open_in_memory()?; |
| 1221 |
let stmt = db.prepare("SELECT ?1")?; |
| 1222 |
stmt.bind_parameter(&1, 1)?; |
| 1223 |
assert_eq!(Some("SELECT 1".to_owned()), stmt.expanded_sql()); |
| 1224 |
Ok(()) |
| 1225 |
} |
| 1226 |
|
| 1227 |
#[test] |
| 1228 |
fn test_bind_parameters() -> Result<()> { |
| 1229 |
let db = Connection::open_in_memory()?; |
| 1230 |
// dynamic slice: |
| 1231 |
db.query_row( |
| 1232 |
"SELECT ?1, ?2, ?3", |
| 1233 |
[&1u8 as &dyn ToSql, &"one", &Some("one")], |
| 1234 |
|row| row.get::<_, u8>(0), |
| 1235 |
)?; |
| 1236 |
// existing collection: |
| 1237 |
let data = vec![1, 2, 3]; |
| 1238 |
db.query_row("SELECT ?1, ?2, ?3", params_from_iter(&data), |row| { |
| 1239 |
row.get::<_, u8>(0) |
| 1240 |
})?; |
| 1241 |
db.query_row( |
| 1242 |
"SELECT ?1, ?2, ?3", |
| 1243 |
params_from_iter(data.as_slice()), |
| 1244 |
|row| row.get::<_, u8>(0), |
| 1245 |
)?; |
| 1246 |
db.query_row("SELECT ?1, ?2, ?3", params_from_iter(data), |row| { |
| 1247 |
row.get::<_, u8>(0) |
| 1248 |
})?; |
| 1249 |
|
| 1250 |
use std::collections::BTreeSet; |
| 1251 |
let data: BTreeSet<String> = ["one", "two", "three"] |
| 1252 |
.iter() |
| 1253 |
.map(|s| (*s).to_string()) |
| 1254 |
.collect(); |
| 1255 |
db.query_row("SELECT ?1, ?2, ?3", params_from_iter(&data), |row| { |
| 1256 |
row.get::<_, String>(0) |
| 1257 |
})?; |
| 1258 |
|
| 1259 |
let data = [0; 3]; |
| 1260 |
db.query_row("SELECT ?1, ?2, ?3", params_from_iter(&data), |row| { |
| 1261 |
row.get::<_, u8>(0) |
| 1262 |
})?; |
| 1263 |
db.query_row("SELECT ?1, ?2, ?3", params_from_iter(data.iter()), |row| { |
| 1264 |
row.get::<_, u8>(0) |
| 1265 |
})?; |
| 1266 |
Ok(()) |
| 1267 |
} |
| 1268 |
|
| 1269 |
#[test] |
| 1270 |
fn test_parameter_name() -> Result<()> { |
| 1271 |
let db = Connection::open_in_memory()?; |
| 1272 |
db.execute_batch("CREATE TABLE test (name TEXT, value INTEGER)")?; |
| 1273 |
let stmt = db.prepare("INSERT INTO test (name, value) VALUES (:name, ?3)")?; |
| 1274 |
assert_eq!(stmt.parameter_name(0), None); |
| 1275 |
assert_eq!(stmt.parameter_name(1), Some(":name")); |
| 1276 |
assert_eq!(stmt.parameter_name(2), None); |
| 1277 |
Ok(()) |
| 1278 |
} |
| 1279 |
|
| 1280 |
#[test] |
| 1281 |
fn test_empty_stmt() -> Result<()> { |
| 1282 |
let conn = Connection::open_in_memory()?; |
| 1283 |
let mut stmt = conn.prepare("")?; |
| 1284 |
assert_eq!(0, stmt.column_count()); |
| 1285 |
stmt.parameter_index("test")?; |
| 1286 |
let err = stmt.step().unwrap_err(); |
| 1287 |
assert_eq!(err.sqlite_error_code(), Some(crate::ErrorCode::ApiMisuse)); |
| 1288 |
// error msg is different with sqlcipher, so we use assert_ne: |
| 1289 |
assert_ne!(err.to_string(), "not an error".to_owned()); |
| 1290 |
stmt.reset()?; // SQLITE_OMIT_AUTORESET = false |
| 1291 |
stmt.execute([]).unwrap_err(); |
| 1292 |
Ok(()) |
| 1293 |
} |
| 1294 |
|
| 1295 |
#[test] |
| 1296 |
fn test_comment_stmt() -> Result<()> { |
| 1297 |
let conn = Connection::open_in_memory()?; |
| 1298 |
conn.prepare("/*SELECT 1;*/")?; |
| 1299 |
Ok(()) |
| 1300 |
} |
| 1301 |
|
| 1302 |
#[test] |
| 1303 |
fn test_comment_and_sql_stmt() -> Result<()> { |
| 1304 |
let conn = Connection::open_in_memory()?; |
| 1305 |
let stmt = conn.prepare("/*...*/ SELECT 1;")?; |
| 1306 |
assert_eq!(1, stmt.column_count()); |
| 1307 |
Ok(()) |
| 1308 |
} |
| 1309 |
|
| 1310 |
#[test] |
| 1311 |
fn test_semi_colon_stmt() -> Result<()> { |
| 1312 |
let conn = Connection::open_in_memory()?; |
| 1313 |
let stmt = conn.prepare(";")?; |
| 1314 |
assert_eq!(0, stmt.column_count()); |
| 1315 |
Ok(()) |
| 1316 |
} |
| 1317 |
|
| 1318 |
#[test] |
| 1319 |
fn test_utf16_conversion() -> Result<()> { |
| 1320 |
let db = Connection::open_in_memory()?; |
| 1321 |
db.pragma_update(None, "encoding", "UTF-16le")?; |
| 1322 |
let encoding: String = db.pragma_query_value(None, "encoding", |row| row.get(0))?; |
| 1323 |
assert_eq!("UTF-16le", encoding); |
| 1324 |
db.execute_batch("CREATE TABLE foo(x TEXT)")?; |
| 1325 |
let expected = "ใในใ"; |
| 1326 |
db.execute("INSERT INTO foo(x) VALUES (?1)", [&expected])?; |
| 1327 |
let actual: String = db.one_column("SELECT x FROM foo")?; |
| 1328 |
assert_eq!(expected, actual); |
| 1329 |
Ok(()) |
| 1330 |
} |
| 1331 |
|
| 1332 |
#[test] |
| 1333 |
fn test_nul_byte() -> Result<()> { |
| 1334 |
let db = Connection::open_in_memory()?; |
| 1335 |
let expected = "a\x00b"; |
| 1336 |
let actual: String = db.query_row("SELECT ?1", [expected], |row| row.get(0))?; |
| 1337 |
assert_eq!(expected, actual); |
| 1338 |
Ok(()) |
| 1339 |
} |
| 1340 |
|
| 1341 |
#[test] |
| 1342 |
#[cfg(feature = "modern_sqlite")] |
| 1343 |
fn is_explain() -> Result<()> { |
| 1344 |
let db = Connection::open_in_memory()?; |
| 1345 |
let stmt = db.prepare("SELECT 1;")?; |
| 1346 |
assert_eq!(0, stmt.is_explain()); |
| 1347 |
Ok(()) |
| 1348 |
} |
| 1349 |
|
| 1350 |
#[test] |
| 1351 |
fn readonly() -> Result<()> { |
| 1352 |
let db = Connection::open_in_memory()?; |
| 1353 |
let stmt = db.prepare("SELECT 1;")?; |
| 1354 |
assert!(stmt.readonly()); |
| 1355 |
Ok(()) |
| 1356 |
} |
| 1357 |
|
| 1358 |
#[test] |
| 1359 |
#[cfg(feature = "modern_sqlite")] // SQLite >= 3.38.0 |
| 1360 |
fn test_error_offset() -> Result<()> { |
| 1361 |
use crate::ffi::ErrorCode; |
| 1362 |
let db = Connection::open_in_memory()?; |
| 1363 |
let r = db.execute_batch("SELECT INVALID_FUNCTION;"); |
| 1364 |
match r.unwrap_err() { |
| 1365 |
Error::SqlInputError { error, offset, .. } => { |
| 1366 |
assert_eq!(error.code, ErrorCode::Unknown); |
| 1367 |
assert_eq!(offset, 7); |
| 1368 |
} |
| 1369 |
err => panic!("Unexpected error {err}"), |
| 1370 |
} |
| 1371 |
Ok(()) |
| 1372 |
} |
| 1373 |
} |