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