mirror of
https://github.com/isar/rusqlite.git
synced 2024-11-23 09:09:19 +08:00
Merge branch 'master' into safe-tosql
This commit is contained in:
commit
0d685af2d5
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "rusqlite"
|
name = "rusqlite"
|
||||||
version = "0.7.2"
|
version = "0.7.3"
|
||||||
authors = ["John Gallagher <jgallagher@bignerdranch.com>"]
|
authors = ["John Gallagher <jgallagher@bignerdranch.com>"]
|
||||||
description = "Ergonomic wrapper for SQLite"
|
description = "Ergonomic wrapper for SQLite"
|
||||||
repository = "https://github.com/jgallagher/rusqlite"
|
repository = "https://github.com/jgallagher/rusqlite"
|
||||||
@ -24,7 +24,6 @@ time = "~0.1.0"
|
|||||||
bitflags = "0.7"
|
bitflags = "0.7"
|
||||||
lru-cache = "0.0.7"
|
lru-cache = "0.0.7"
|
||||||
libc = "~0.2"
|
libc = "~0.2"
|
||||||
clippy = {version = "~0.0.58", optional = true}
|
|
||||||
chrono = { version = "~0.2", optional = true }
|
chrono = { version = "~0.2", optional = true }
|
||||||
serde_json = { version = "0.6", optional = true }
|
serde_json = { version = "0.6", optional = true }
|
||||||
|
|
||||||
|
@ -5,6 +5,14 @@
|
|||||||
methods.
|
methods.
|
||||||
* BREAKING CHANGE: The `ToSql` trait has been redesigned. It can now be implemented without
|
* BREAKING CHANGE: The `ToSql` trait has been redesigned. It can now be implemented without
|
||||||
`unsafe`, and implementors can choose to return either borrowed or owned results.
|
`unsafe`, and implementors can choose to return either borrowed or owned results.
|
||||||
|
* Added `#[deprecated(since = "...", note = "...")]` flags (new in Rust 1.9 for libraries) to
|
||||||
|
all deprecated APIs.
|
||||||
|
|
||||||
|
# Version 0.7.3 (2016-06-01)
|
||||||
|
|
||||||
|
* Fixes an incorrect failure from the `insert()` convenience function when back-to-back inserts to
|
||||||
|
different tables both returned the same row ID
|
||||||
|
([#171](https://github.com/jgallagher/rusqlite/issues/171)).
|
||||||
|
|
||||||
# Version 0.7.2 (2016-05-19)
|
# Version 0.7.2 (2016-05-19)
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
environment:
|
environment:
|
||||||
TARGET: 1.8.0-x86_64-pc-windows-gnu
|
TARGET: 1.9.0-x86_64-pc-windows-gnu
|
||||||
MSYS2_BITS: 64
|
MSYS2_BITS: 64
|
||||||
install:
|
install:
|
||||||
- ps: Start-FileDownload "https://static.rust-lang.org/dist/rust-${env:TARGET}.exe"
|
- ps: Start-FileDownload "https://static.rust-lang.org/dist/rust-${env:TARGET}.exe"
|
||||||
|
@ -4,18 +4,20 @@ use types::ToSql;
|
|||||||
impl<'conn> Statement<'conn> {
|
impl<'conn> Statement<'conn> {
|
||||||
/// Execute an INSERT and return the ROWID.
|
/// Execute an INSERT and return the ROWID.
|
||||||
///
|
///
|
||||||
|
/// # Note
|
||||||
|
///
|
||||||
|
/// This function is a convenience wrapper around `execute()` intended for queries that
|
||||||
|
/// insert a single item. It is possible to misuse this function in a way that it cannot
|
||||||
|
/// detect, such as by calling it on a statement which _updates_ a single item rather than
|
||||||
|
/// inserting one. Please don't do that.
|
||||||
|
///
|
||||||
/// # Failure
|
/// # Failure
|
||||||
|
///
|
||||||
/// Will return `Err` if no row is inserted or many rows are inserted.
|
/// Will return `Err` if no row is inserted or many rows are inserted.
|
||||||
pub fn insert(&mut self, params: &[&ToSql]) -> Result<i64> {
|
pub fn insert(&mut self, params: &[&ToSql]) -> Result<i64> {
|
||||||
// Some non-insertion queries could still return 1 change (an UPDATE, for example), so
|
|
||||||
// to guard against that we can check that the connection's last_insert_rowid() changes
|
|
||||||
// after we execute the statement.
|
|
||||||
let prev_rowid = self.conn.last_insert_rowid();
|
|
||||||
let changes = try!(self.execute(params));
|
let changes = try!(self.execute(params));
|
||||||
let new_rowid = self.conn.last_insert_rowid();
|
|
||||||
match changes {
|
match changes {
|
||||||
1 if prev_rowid != new_rowid => Ok(new_rowid),
|
1 => Ok(self.conn.last_insert_rowid()),
|
||||||
1 if prev_rowid == new_rowid => Err(Error::StatementFailedToInsertRow),
|
|
||||||
_ => Err(Error::StatementChangedRows(changes)),
|
_ => Err(Error::StatementChangedRows(changes)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -57,18 +59,19 @@ mod test {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_insert_failures() {
|
fn test_insert_different_tables() {
|
||||||
|
// Test for https://github.com/jgallagher/rusqlite/issues/171
|
||||||
let db = Connection::open_in_memory().unwrap();
|
let db = Connection::open_in_memory().unwrap();
|
||||||
db.execute_batch("CREATE TABLE foo(x INTEGER UNIQUE)").unwrap();
|
db.execute_batch(r"
|
||||||
let mut insert = db.prepare("INSERT INTO foo (x) VALUES (?)").unwrap();
|
CREATE TABLE foo(x INTEGER);
|
||||||
let mut update = db.prepare("UPDATE foo SET x = ?").unwrap();
|
CREATE TABLE bar(x INTEGER);
|
||||||
|
")
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
assert_eq!(insert.insert(&[&1i32]).unwrap(), 1);
|
assert_eq!(db.prepare("INSERT INTO foo VALUES (10)").unwrap().insert(&[]).unwrap(),
|
||||||
|
1);
|
||||||
match update.insert(&[&2i32]) {
|
assert_eq!(db.prepare("INSERT INTO bar VALUES (10)").unwrap().insert(&[]).unwrap(),
|
||||||
Err(Error::StatementFailedToInsertRow) => (),
|
1);
|
||||||
r => panic!("Unexpected result {:?}", r),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
10
src/error.rs
10
src/error.rs
@ -6,6 +6,7 @@ use libc::c_int;
|
|||||||
use {ffi, errmsg_to_string};
|
use {ffi, errmsg_to_string};
|
||||||
|
|
||||||
/// Old name for `Error`. `SqliteError` is deprecated.
|
/// Old name for `Error`. `SqliteError` is deprecated.
|
||||||
|
#[deprecated(since = "0.6.0", note = "Use Error instead")]
|
||||||
pub type SqliteError = Error;
|
pub type SqliteError = Error;
|
||||||
|
|
||||||
/// Enum listing possible errors from rusqlite.
|
/// Enum listing possible errors from rusqlite.
|
||||||
@ -55,10 +56,6 @@ pub enum Error {
|
|||||||
/// Error when a query that was expected to insert one row did not insert any or insert many.
|
/// Error when a query that was expected to insert one row did not insert any or insert many.
|
||||||
StatementChangedRows(c_int),
|
StatementChangedRows(c_int),
|
||||||
|
|
||||||
/// Error when a query that was expected to insert a row did not change the connection's
|
|
||||||
/// last_insert_rowid().
|
|
||||||
StatementFailedToInsertRow,
|
|
||||||
|
|
||||||
/// Error returned by `functions::Context::get` when the function argument cannot be converted
|
/// Error returned by `functions::Context::get` when the function argument cannot be converted
|
||||||
/// to the requested type.
|
/// to the requested type.
|
||||||
#[cfg(feature = "functions")]
|
#[cfg(feature = "functions")]
|
||||||
@ -105,7 +102,6 @@ impl fmt::Display for Error {
|
|||||||
Error::InvalidColumnName(ref name) => write!(f, "Invalid column name: {}", name),
|
Error::InvalidColumnName(ref name) => write!(f, "Invalid column name: {}", name),
|
||||||
Error::InvalidColumnType => write!(f, "Invalid column type"),
|
Error::InvalidColumnType => write!(f, "Invalid column type"),
|
||||||
Error::StatementChangedRows(i) => write!(f, "Query changed {} rows", i),
|
Error::StatementChangedRows(i) => write!(f, "Query changed {} rows", i),
|
||||||
Error::StatementFailedToInsertRow => write!(f, "Statement failed to insert new row"),
|
|
||||||
|
|
||||||
#[cfg(feature = "functions")]
|
#[cfg(feature = "functions")]
|
||||||
Error::InvalidFunctionParameterType => write!(f, "Invalid function parameter type"),
|
Error::InvalidFunctionParameterType => write!(f, "Invalid function parameter type"),
|
||||||
@ -136,7 +132,6 @@ impl error::Error for Error {
|
|||||||
Error::InvalidColumnName(_) => "invalid column name",
|
Error::InvalidColumnName(_) => "invalid column name",
|
||||||
Error::InvalidColumnType => "invalid column type",
|
Error::InvalidColumnType => "invalid column type",
|
||||||
Error::StatementChangedRows(_) => "query inserted zero or more than one row",
|
Error::StatementChangedRows(_) => "query inserted zero or more than one row",
|
||||||
Error::StatementFailedToInsertRow => "statement failed to insert new row",
|
|
||||||
|
|
||||||
#[cfg(feature = "functions")]
|
#[cfg(feature = "functions")]
|
||||||
Error::InvalidFunctionParameterType => "invalid function parameter type",
|
Error::InvalidFunctionParameterType => "invalid function parameter type",
|
||||||
@ -161,8 +156,7 @@ impl error::Error for Error {
|
|||||||
Error::InvalidColumnName(_) |
|
Error::InvalidColumnName(_) |
|
||||||
Error::InvalidColumnType |
|
Error::InvalidColumnType |
|
||||||
Error::InvalidPath(_) |
|
Error::InvalidPath(_) |
|
||||||
Error::StatementChangedRows(_) |
|
Error::StatementChangedRows(_) => None,
|
||||||
Error::StatementFailedToInsertRow => None,
|
|
||||||
|
|
||||||
#[cfg(feature = "functions")]
|
#[cfg(feature = "functions")]
|
||||||
Error::InvalidFunctionParameterType => None,
|
Error::InvalidFunctionParameterType => None,
|
||||||
|
@ -59,8 +59,6 @@ use libc::{c_int, c_char, c_void};
|
|||||||
use ffi;
|
use ffi;
|
||||||
use ffi::sqlite3_context;
|
use ffi::sqlite3_context;
|
||||||
use ffi::sqlite3_value;
|
use ffi::sqlite3_value;
|
||||||
use ffi::sqlite3_value_type;
|
|
||||||
use ffi::sqlite3_value_numeric_type;
|
|
||||||
|
|
||||||
use types::{ToSql, ToSqlOutput, FromSql, ValueRef};
|
use types::{ToSql, ToSqlOutput, FromSql, ValueRef};
|
||||||
|
|
||||||
|
22
src/lib.rs
22
src/lib.rs
@ -50,8 +50,6 @@
|
|||||||
//! }
|
//! }
|
||||||
//! }
|
//! }
|
||||||
//! ```
|
//! ```
|
||||||
#![cfg_attr(feature="clippy", feature(plugin))]
|
|
||||||
#![cfg_attr(feature="clippy", plugin(clippy))]
|
|
||||||
|
|
||||||
extern crate libc;
|
extern crate libc;
|
||||||
extern crate libsqlite3_sys as ffi;
|
extern crate libsqlite3_sys as ffi;
|
||||||
@ -105,6 +103,7 @@ mod raw_statement;
|
|||||||
const STATEMENT_CACHE_DEFAULT_CAPACITY: usize = 16;
|
const STATEMENT_CACHE_DEFAULT_CAPACITY: usize = 16;
|
||||||
|
|
||||||
/// Old name for `Result`. `SqliteResult` is deprecated.
|
/// Old name for `Result`. `SqliteResult` is deprecated.
|
||||||
|
#[deprecated(since = "0.6.0", note = "Use Result instead")]
|
||||||
pub type SqliteResult<T> = Result<T>;
|
pub type SqliteResult<T> = Result<T>;
|
||||||
|
|
||||||
/// A typedef of the result returned by many methods.
|
/// A typedef of the result returned by many methods.
|
||||||
@ -151,6 +150,7 @@ impl<'a> DatabaseName<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Old name for `Connection`. `SqliteConnection` is deprecated.
|
/// Old name for `Connection`. `SqliteConnection` is deprecated.
|
||||||
|
#[deprecated(since = "0.6.0", note = "Use Connection instead")]
|
||||||
pub type SqliteConnection = Connection;
|
pub type SqliteConnection = Connection;
|
||||||
|
|
||||||
/// A connection to a SQLite database.
|
/// A connection to a SQLite database.
|
||||||
@ -367,6 +367,7 @@ impl Connection {
|
|||||||
///
|
///
|
||||||
/// This method should be considered deprecated. Use `query_row` instead, which now
|
/// This method should be considered deprecated. Use `query_row` instead, which now
|
||||||
/// does exactly the same thing.
|
/// does exactly the same thing.
|
||||||
|
#[deprecated(since = "0.1.0", note = "Use query_row instead")]
|
||||||
pub fn query_row_safe<T, F>(&self, sql: &str, params: &[&ToSql], f: F) -> Result<T>
|
pub fn query_row_safe<T, F>(&self, sql: &str, params: &[&ToSql], f: F) -> Result<T>
|
||||||
where F: FnOnce(Row) -> T
|
where F: FnOnce(Row) -> T
|
||||||
{
|
{
|
||||||
@ -507,6 +508,7 @@ struct InnerConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Old name for `OpenFlags`. `SqliteOpenFlags` is deprecated.
|
/// Old name for `OpenFlags`. `SqliteOpenFlags` is deprecated.
|
||||||
|
#[deprecated(since = "0.6.0", note = "Use OpenFlags instead")]
|
||||||
pub type SqliteOpenFlags = OpenFlags;
|
pub type SqliteOpenFlags = OpenFlags;
|
||||||
|
|
||||||
bitflags! {
|
bitflags! {
|
||||||
@ -678,6 +680,7 @@ impl Drop for InnerConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Old name for `Statement`. `SqliteStatement` is deprecated.
|
/// Old name for `Statement`. `SqliteStatement` is deprecated.
|
||||||
|
#[deprecated(since = "0.6.0", note = "Use Statement instead")]
|
||||||
pub type SqliteStatement<'conn> = Statement<'conn>;
|
pub type SqliteStatement<'conn> = Statement<'conn>;
|
||||||
|
|
||||||
/// A prepared statement.
|
/// A prepared statement.
|
||||||
@ -1013,6 +1016,7 @@ impl<'stmt, T, E, F> Iterator for AndThenRows<'stmt, F>
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Old name for `Rows`. `SqliteRows` is deprecated.
|
/// Old name for `Rows`. `SqliteRows` is deprecated.
|
||||||
|
#[deprecated(since = "0.6.0", note = "Use Rows instead")]
|
||||||
pub type SqliteRows<'stmt> = Rows<'stmt>;
|
pub type SqliteRows<'stmt> = Rows<'stmt>;
|
||||||
|
|
||||||
/// An handle for the resulting rows of a query.
|
/// An handle for the resulting rows of a query.
|
||||||
@ -1077,6 +1081,7 @@ impl<'stmt> Drop for Rows<'stmt> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Old name for `Row`. `SqliteRow` is deprecated.
|
/// Old name for `Row`. `SqliteRow` is deprecated.
|
||||||
|
#[deprecated(since = "0.6.0", note = "Use Row instead")]
|
||||||
pub type SqliteRow<'a, 'stmt> = Row<'a, 'stmt>;
|
pub type SqliteRow<'a, 'stmt> = Row<'a, 'stmt>;
|
||||||
|
|
||||||
/// A single result row of a query.
|
/// A single result row of a query.
|
||||||
@ -1168,14 +1173,17 @@ impl<'a> ValueRef<'a> {
|
|||||||
}
|
}
|
||||||
ffi::SQLITE_BLOB => {
|
ffi::SQLITE_BLOB => {
|
||||||
let blob = ffi::sqlite3_column_blob(raw, col);
|
let blob = ffi::sqlite3_column_blob(raw, col);
|
||||||
assert!(!blob.is_null(),
|
|
||||||
"unexpected SQLITE_BLOB column type with NULL data");
|
|
||||||
|
|
||||||
let len = ffi::sqlite3_column_bytes(raw, col);
|
let len = ffi::sqlite3_column_bytes(raw, col);
|
||||||
assert!(len >= 0,
|
assert!(len >= 0, "unexpected negative return from sqlite3_column_bytes");
|
||||||
"unexpected negative return from sqlite3_column_bytes");
|
if len > 0 {
|
||||||
|
assert!(!blob.is_null(), "unexpected SQLITE_BLOB column type with NULL data");
|
||||||
ValueRef::Blob(from_raw_parts(blob as *const u8, len as usize))
|
ValueRef::Blob(from_raw_parts(blob as *const u8, len as usize))
|
||||||
|
} else {
|
||||||
|
// The return value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
|
||||||
|
ValueRef::Blob(&[])
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
_ => unreachable!("sqlite3_column_type returned invalid value"),
|
_ => unreachable!("sqlite3_column_type returned invalid value"),
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
use {Result, Connection};
|
use {Result, Connection};
|
||||||
|
|
||||||
/// Old name for `LoadExtensionGuard`. `SqliteLoadExtensionGuard` is deprecated.
|
/// Old name for `LoadExtensionGuard`. `SqliteLoadExtensionGuard` is deprecated.
|
||||||
|
#[deprecated(since = "0.6.0", note = "Use LoadExtensionGuard instead")]
|
||||||
pub type SqliteLoadExtensionGuard<'conn> = LoadExtensionGuard<'conn>;
|
pub type SqliteLoadExtensionGuard<'conn> = LoadExtensionGuard<'conn>;
|
||||||
|
|
||||||
/// RAII guard temporarily enabling SQLite extensions to be loaded.
|
/// RAII guard temporarily enabling SQLite extensions to be loaded.
|
||||||
|
@ -2,6 +2,7 @@ use std::ops::Deref;
|
|||||||
use {Result, Connection};
|
use {Result, Connection};
|
||||||
|
|
||||||
/// Old name for `TransactionBehavior`. `SqliteTransactionBehavior` is deprecated.
|
/// Old name for `TransactionBehavior`. `SqliteTransactionBehavior` is deprecated.
|
||||||
|
#[deprecated(since = "0.6.0", note = "Use TransactionBehavior instead")]
|
||||||
pub type SqliteTransactionBehavior = TransactionBehavior;
|
pub type SqliteTransactionBehavior = TransactionBehavior;
|
||||||
|
|
||||||
/// Options for transaction behavior. See [BEGIN
|
/// Options for transaction behavior. See [BEGIN
|
||||||
@ -28,6 +29,7 @@ pub enum DropBehavior {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Old name for `Transaction`. `SqliteTransaction` is deprecated.
|
/// Old name for `Transaction`. `SqliteTransaction` is deprecated.
|
||||||
|
#[deprecated(since = "0.6.0", note = "Use Transaction instead")]
|
||||||
pub type SqliteTransaction<'conn> = Transaction<'conn>;
|
pub type SqliteTransaction<'conn> = Transaction<'conn>;
|
||||||
|
|
||||||
/// Represents a transaction on a database connection.
|
/// Represents a transaction on a database connection.
|
||||||
|
@ -112,6 +112,17 @@ mod test {
|
|||||||
assert_eq!(v, v1234);
|
assert_eq!(v, v1234);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_empty_blob() {
|
||||||
|
let db = checked_memory_handle();
|
||||||
|
|
||||||
|
let empty = vec![];
|
||||||
|
db.execute("INSERT INTO foo(b) VALUES (?)", &[&empty]).unwrap();
|
||||||
|
|
||||||
|
let v: Vec<u8> = db.query_row("SELECT b FROM foo", &[], |r| r.get(0)).unwrap();
|
||||||
|
assert_eq!(v, empty);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_str() {
|
fn test_str() {
|
||||||
let db = checked_memory_handle();
|
let db = checked_memory_handle();
|
||||||
|
Loading…
Reference in New Issue
Block a user