mirror of
https://github.com/fluencelabs/sqlite-wasm-connector
synced 2025-04-25 00:32:14 +00:00
Make execute and statement take immutable self
This commit is contained in:
parent
c5c87066e9
commit
0e6ee0c10d
@ -19,7 +19,7 @@ use std::path::PathBuf;
|
|||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let path = setup();
|
let path = setup();
|
||||||
let mut database = sqlite::open(&path).unwrap();
|
let database = sqlite::open(&path).unwrap();
|
||||||
|
|
||||||
database.execute(r#"
|
database.execute(r#"
|
||||||
CREATE TABLE `users` (id INTEGER, name VARCHAR(255));
|
CREATE TABLE `users` (id INTEGER, name VARCHAR(255));
|
||||||
|
@ -4,7 +4,7 @@ use std::path::PathBuf;
|
|||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let path = setup();
|
let path = setup();
|
||||||
let mut database = sqlite::open(&path).unwrap();
|
let database = sqlite::open(&path).unwrap();
|
||||||
|
|
||||||
database.execute(r#"
|
database.execute(r#"
|
||||||
CREATE TABLE `users` (id INTEGER, name VARCHAR(255));
|
CREATE TABLE `users` (id INTEGER, name VARCHAR(255));
|
||||||
|
@ -25,8 +25,8 @@ impl Database {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Execute an SQL statement.
|
/// Execute an SQL statement.
|
||||||
pub fn execute<'l>(&mut self, sql: &str,
|
pub fn execute<'l>(&self, sql: &str, callback: Option<&mut ExecuteCallback<'l>>)
|
||||||
callback: Option<&mut ExecuteCallback<'l>>) -> Result<()> {
|
-> Result<()> {
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
match callback {
|
match callback {
|
||||||
@ -49,7 +49,7 @@ impl Database {
|
|||||||
|
|
||||||
/// Create a prepared statement.
|
/// Create a prepared statement.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn statement<'l>(&'l mut self, sql: &str) -> Result<Statement<'l>> {
|
pub fn statement<'l>(&'l self, sql: &str) -> Result<Statement<'l>> {
|
||||||
::statement::new(self, sql)
|
::statement::new(self, sql)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -62,7 +62,7 @@ impl Drop for Database {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn as_raw(database: &mut Database) -> *mut raw::sqlite3 {
|
pub fn as_raw(database: &Database) -> *mut raw::sqlite3 {
|
||||||
database.raw
|
database.raw
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -13,7 +13,7 @@ pub struct Error {
|
|||||||
|
|
||||||
impl Error {
|
impl Error {
|
||||||
/// Return the last occurred error if any.
|
/// Return the last occurred error if any.
|
||||||
pub fn last(database: &mut Database) -> Option<Error> {
|
pub fn last(database: &Database) -> Option<Error> {
|
||||||
unsafe {
|
unsafe {
|
||||||
let code = raw::sqlite3_errcode(::database::as_raw(database));
|
let code = raw::sqlite3_errcode(::database::as_raw(database));
|
||||||
if code == raw::SQLITE_OK {
|
if code == raw::SQLITE_OK {
|
||||||
|
@ -101,7 +101,7 @@ impl Value for String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn new<'l>(database: &'l mut Database, sql: &str) -> Result<Statement<'l>> {
|
pub fn new<'l>(database: &'l Database, sql: &str) -> Result<Statement<'l>> {
|
||||||
let mut raw = 0 as *mut _;
|
let mut raw = 0 as *mut _;
|
||||||
unsafe {
|
unsafe {
|
||||||
success!(database, raw::sqlite3_prepare(::database::as_raw(database), str_to_c_str!(sql),
|
success!(database, raw::sqlite3_prepare(::database::as_raw(database), str_to_c_str!(sql),
|
||||||
|
@ -18,7 +18,7 @@ fn workflow() {
|
|||||||
);
|
);
|
||||||
|
|
||||||
let (path, _directory) = setup();
|
let (path, _directory) = setup();
|
||||||
let mut database = ok!(sqlite::open(&path));
|
let database = ok!(sqlite::open(&path));
|
||||||
|
|
||||||
let sql = r#"CREATE TABLE `users` (id INTEGER, name VARCHAR(255), age REAL);"#;
|
let sql = r#"CREATE TABLE `users` (id INTEGER, name VARCHAR(255), age REAL);"#;
|
||||||
ok!(database.execute(sql, None));
|
ok!(database.execute(sql, None));
|
||||||
@ -58,7 +58,7 @@ fn workflow() {
|
|||||||
#[test]
|
#[test]
|
||||||
fn failure() {
|
fn failure() {
|
||||||
let (path, _directory) = setup();
|
let (path, _directory) = setup();
|
||||||
let mut database = ok!(sqlite::open(&path));
|
let database = ok!(sqlite::open(&path));
|
||||||
match database.execute(":)", None) {
|
match database.execute(":)", None) {
|
||||||
Err(error) => assert_eq!(error.message, Some(String::from(r#"unrecognized token: ":""#))),
|
Err(error) => assert_eq!(error.message, Some(String::from(r#"unrecognized token: ":""#))),
|
||||||
_ => assert!(false),
|
_ => assert!(false),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user