mirror of
https://github.com/isar/rusqlite.git
synced 2024-11-22 16:29:20 +08:00
Rename SqliteRow
-> Row
.
This commit is contained in:
parent
b932640181
commit
ea5cb41bbf
@ -6,6 +6,7 @@
|
||||
* `SqliteResult` is now `Result`
|
||||
* `SqliteStatement` is now `Statement`
|
||||
* `SqliteRows` is now `Rows`
|
||||
* `SqliteRow` is now `Row`
|
||||
The old, prefixed names are still exported should be considered deprecated.
|
||||
* Adds a variety of `..._named` methods for executing queries using named placeholder parameters.
|
||||
* Adds `backup` feature that exposes SQLite's online backup API.
|
||||
|
@ -56,7 +56,7 @@ fn main() {
|
||||
}
|
||||
```
|
||||
|
||||
### Design of SqliteRows and SqliteRow
|
||||
### Design of Rows and Row
|
||||
|
||||
To retrieve the result rows from a query, SQLite requires you to call
|
||||
[sqlite3_step()](https://www.sqlite.org/c3ref/step.html) on a prepared statement. You can only
|
||||
@ -67,7 +67,7 @@ satisfy the [Iterator](http://doc.rust-lang.org/std/iter/trait.Iterator.html) tr
|
||||
you cannot (as easily) loop over the rows, or use many of the helpful Iterator methods like `map`
|
||||
and `filter`.
|
||||
|
||||
Instead, Rusqlite's `SqliteRows` handle does conform to `Iterator`. It ensures safety by
|
||||
Instead, Rusqlite's `Rows` handle does conform to `Iterator`. It ensures safety by
|
||||
performing checks at runtime to ensure you do not try to retrieve the values of a "stale" row, and
|
||||
will panic if you do so. A specific example that will panic:
|
||||
|
||||
@ -88,7 +88,7 @@ fn bad_function_will_panic(conn: &Connection) -> Result<i64> {
|
||||
```
|
||||
|
||||
There are other, less obvious things that may result in a panic as well, such as calling
|
||||
`collect()` on a `SqliteRows` and then trying to use the collected rows.
|
||||
`collect()` on a `Rows` and then trying to use the collected rows.
|
||||
|
||||
Strongly consider using the method `query_map()` instead, if you can.
|
||||
`query_map()` returns an iterator over rows-mapped-to-some-type. This
|
||||
|
31
src/lib.rs
31
src/lib.rs
@ -379,7 +379,7 @@ impl Connection {
|
||||
/// Will return `Err` if `sql` cannot be converted to a C-compatible string or if the
|
||||
/// underlying SQLite call fails.
|
||||
pub fn query_row<T, F>(&self, sql: &str, params: &[&ToSql], f: F) -> Result<T>
|
||||
where F: FnOnce(SqliteRow) -> T
|
||||
where F: FnOnce(Row) -> T
|
||||
{
|
||||
let mut stmt = try!(self.prepare(sql));
|
||||
let mut rows = try!(stmt.query(params));
|
||||
@ -409,7 +409,7 @@ impl Connection {
|
||||
/// Will return `Err` if `sql` cannot be converted to a C-compatible string or if the
|
||||
/// underlying SQLite call fails.
|
||||
pub fn query_row_and_then<T, E, F>(&self, sql: &str, params: &[&ToSql], f: F) -> result::Result<T, E>
|
||||
where F: FnOnce(SqliteRow) -> result::Result<T, E>,
|
||||
where F: FnOnce(Row) -> result::Result<T, E>,
|
||||
E: convert::From<Error>
|
||||
{
|
||||
let mut stmt = try!(self.prepare(sql));
|
||||
@ -438,7 +438,7 @@ impl Connection {
|
||||
/// This method should be considered deprecated. Use `query_row` instead, which now
|
||||
/// does exactly the same thing.
|
||||
pub fn query_row_safe<T, F>(&self, sql: &str, params: &[&ToSql], f: F) -> Result<T>
|
||||
where F: FnOnce(SqliteRow) -> T
|
||||
where F: FnOnce(Row) -> T
|
||||
{
|
||||
self.query_row(sql, params, f)
|
||||
}
|
||||
@ -865,7 +865,7 @@ impl<'conn> Statement<'conn> {
|
||||
params: &[&ToSql],
|
||||
f: F)
|
||||
-> Result<MappedRows<'a, F>>
|
||||
where F: FnMut(&SqliteRow) -> T
|
||||
where F: FnMut(&Row) -> T
|
||||
{
|
||||
let row_iter = try!(self.query(params));
|
||||
|
||||
@ -890,7 +890,7 @@ impl<'conn> Statement<'conn> {
|
||||
f: F)
|
||||
-> Result<AndThenRows<'a, F>>
|
||||
where E: convert::From<Error>,
|
||||
F: FnMut(&SqliteRow) -> result::Result<T, E>
|
||||
F: FnMut(&Row) -> result::Result<T, E>
|
||||
{
|
||||
let row_iter = try!(self.query(params));
|
||||
|
||||
@ -968,7 +968,7 @@ pub struct MappedRows<'stmt, F> {
|
||||
map: F,
|
||||
}
|
||||
|
||||
impl<'stmt, T, F> Iterator for MappedRows<'stmt, F> where F: FnMut(&SqliteRow) -> T
|
||||
impl<'stmt, T, F> Iterator for MappedRows<'stmt, F> where F: FnMut(&Row) -> T
|
||||
{
|
||||
type Item = Result<T>;
|
||||
|
||||
@ -986,7 +986,7 @@ pub struct AndThenRows<'stmt, F> {
|
||||
|
||||
impl<'stmt, T, E, F> Iterator for AndThenRows<'stmt, F>
|
||||
where E: convert::From<Error>,
|
||||
F: FnMut(&SqliteRow) -> result::Result<T, E>
|
||||
F: FnMut(&Row) -> result::Result<T, E>
|
||||
{
|
||||
type Item = result::Result<T, E>;
|
||||
|
||||
@ -1051,7 +1051,7 @@ impl<'stmt> Rows<'stmt> {
|
||||
}
|
||||
}
|
||||
|
||||
fn get_expected_row(&mut self) -> Result<SqliteRow<'stmt>> {
|
||||
fn get_expected_row(&mut self) -> Result<Row<'stmt>> {
|
||||
match self.next() {
|
||||
Some(row) => row,
|
||||
None => {
|
||||
@ -1065,9 +1065,9 @@ impl<'stmt> Rows<'stmt> {
|
||||
}
|
||||
|
||||
impl<'stmt> Iterator for Rows<'stmt> {
|
||||
type Item = Result<SqliteRow<'stmt>>;
|
||||
type Item = Result<Row<'stmt>>;
|
||||
|
||||
fn next(&mut self) -> Option<Result<SqliteRow<'stmt>>> {
|
||||
fn next(&mut self) -> Option<Result<Row<'stmt>>> {
|
||||
if self.failed {
|
||||
return None;
|
||||
}
|
||||
@ -1075,7 +1075,7 @@ impl<'stmt> Iterator for Rows<'stmt> {
|
||||
ffi::SQLITE_ROW => {
|
||||
let current_row = self.current_row.get() + 1;
|
||||
self.current_row.set(current_row);
|
||||
Some(Ok(SqliteRow {
|
||||
Some(Ok(Row {
|
||||
stmt: self.stmt,
|
||||
current_row: self.current_row.clone(),
|
||||
row_idx: current_row,
|
||||
@ -1090,17 +1090,20 @@ impl<'stmt> Iterator for Rows<'stmt> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Old name for `Row`. `SqliteRow` is deprecated.
|
||||
pub type SqliteRow<'stmt> = Row<'stmt>;
|
||||
|
||||
/// A single result row of a query.
|
||||
pub struct SqliteRow<'stmt> {
|
||||
pub struct Row<'stmt> {
|
||||
stmt: &'stmt Statement<'stmt>,
|
||||
current_row: Rc<Cell<c_int>>,
|
||||
row_idx: c_int,
|
||||
}
|
||||
|
||||
impl<'stmt> SqliteRow<'stmt> {
|
||||
impl<'stmt> Row<'stmt> {
|
||||
/// Get the value of a particular column of the result row.
|
||||
///
|
||||
/// Note that `SqliteRow` can panic at runtime if you use it incorrectly. When you are
|
||||
/// Note that `Row` can panic at runtime if you use it incorrectly. When you are
|
||||
/// retrieving the rows of a query, a row becomes stale once you have requested the next row,
|
||||
/// and the values can no longer be retrieved. In general (when using looping over the rows,
|
||||
/// for example) this isn't an issue, but it means you cannot do something like this:
|
||||
|
@ -2,7 +2,7 @@ use libc::c_int;
|
||||
|
||||
use super::ffi;
|
||||
|
||||
use {Result, Error, Connection, Statement, Rows, SqliteRow, str_to_cstring};
|
||||
use {Result, Error, Connection, Statement, Rows, Row, str_to_cstring};
|
||||
use types::ToSql;
|
||||
|
||||
impl Connection {
|
||||
@ -42,7 +42,7 @@ impl Connection {
|
||||
params: &[(&str, &ToSql)],
|
||||
f: F)
|
||||
-> Result<T>
|
||||
where F: FnOnce(SqliteRow) -> T
|
||||
where F: FnOnce(Row) -> T
|
||||
{
|
||||
let mut stmt = try!(self.prepare(sql));
|
||||
let mut rows = try!(stmt.query_named(params));
|
||||
|
Loading…
Reference in New Issue
Block a user