mirror of
https://github.com/fluencelabs/sqlite-wasm-connector
synced 2025-06-26 05:21:33 +00:00
Merge branch 'master' into optimize
This commit is contained in:
@ -1,10 +1,9 @@
|
||||
use sqlite3_connector as ffi;
|
||||
use crate::sqlite3_connector as ffi;
|
||||
use crate::{Result, Statement};
|
||||
|
||||
use std::marker::PhantomData;
|
||||
use std::path::Path;
|
||||
|
||||
use {Result, Statement};
|
||||
|
||||
/// A database connection.
|
||||
pub struct Connection {
|
||||
raw: ffi::Sqlite3DbHandle,
|
||||
@ -33,14 +32,14 @@ impl Connection {
|
||||
match result.ret_code {
|
||||
ffi::SQLITE_OK => {}
|
||||
code => {
|
||||
return match ::last_error(result.db_handle) {
|
||||
return match crate::last_error(result.db_handle) {
|
||||
Some(error) => {
|
||||
ffi::sqlite3_close(result.db_handle);
|
||||
Err(error)
|
||||
}
|
||||
_ => {
|
||||
ffi::sqlite3_close(result.db_handle);
|
||||
Err(::Error {
|
||||
Err(crate::Error {
|
||||
code: Some(code as isize),
|
||||
message: None,
|
||||
})
|
||||
@ -91,7 +90,7 @@ impl Connection {
|
||||
/// Create a prepared statement.
|
||||
#[inline]
|
||||
pub fn prepare<T: AsRef<str>>(&self, statement: T) -> Result<Statement> {
|
||||
::statement::new(self.raw, statement)
|
||||
crate::statement::new(self.raw, statement)
|
||||
}
|
||||
|
||||
/// Return the number of rows inserted, updated, or deleted by the most
|
||||
|
@ -1,6 +1,6 @@
|
||||
use sqlite3_connector as ffi;
|
||||
use statement::{State, Statement};
|
||||
use {Result, Value};
|
||||
use crate::sqlite3_connector as ffi;
|
||||
use crate::statement::{State, Statement};
|
||||
use crate::{Result, Value};
|
||||
|
||||
/// An iterator over rows.
|
||||
pub struct Cursor {
|
||||
|
@ -1,8 +1,6 @@
|
||||
#![allow(unused_variables)]
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
extern crate marine_rs_sdk;
|
||||
|
||||
use marine_rs_sdk::marine;
|
||||
|
||||
pub fn main() {}
|
||||
|
16
src/lib.rs
16
src/lib.rs
@ -4,7 +4,7 @@
|
||||
//!
|
||||
//! Open a connection, create a table, and insert some rows:
|
||||
//!
|
||||
//! ```
|
||||
//! ```ignore
|
||||
//! let connection = sqlite::open(":memory:").unwrap();
|
||||
//!
|
||||
//! connection
|
||||
@ -20,7 +20,7 @@
|
||||
//!
|
||||
//! Select some rows and process them one by one as plain text:
|
||||
//!
|
||||
//! ```
|
||||
//! ```ignore
|
||||
//! # let connection = sqlite::open(":memory:").unwrap();
|
||||
//! # connection
|
||||
//! # .execute(
|
||||
@ -44,7 +44,7 @@
|
||||
//! The same query using a prepared statement, which is much more efficient than
|
||||
//! the previous technique:
|
||||
//!
|
||||
//! ```
|
||||
//! ```ignore
|
||||
//! use sqlite::State;
|
||||
//! # let connection = sqlite::open(":memory:").unwrap();
|
||||
//! # connection
|
||||
@ -72,7 +72,7 @@
|
||||
//! The same query using a cursor, which is a wrapper around a prepared
|
||||
//! statement providing the concept of row and featuring all-at-once binding:
|
||||
//!
|
||||
//! ```
|
||||
//! ```ignore
|
||||
//! use sqlite::Value;
|
||||
//! # let connection = sqlite::open(":memory:").unwrap();
|
||||
//! # connection
|
||||
@ -108,9 +108,9 @@ use std::{error, fmt};
|
||||
|
||||
macro_rules! error(
|
||||
($connection:expr, $code:expr) => (
|
||||
match ::last_error($connection) {
|
||||
match crate::last_error($connection) {
|
||||
Some(error) => return Err(error),
|
||||
_ => return Err(::Error {
|
||||
_ => return Err(crate::Error {
|
||||
code: Some($code as isize),
|
||||
message: None,
|
||||
}),
|
||||
@ -121,7 +121,7 @@ macro_rules! error(
|
||||
macro_rules! ok_descr(
|
||||
($connection:expr, $result:expr) => (
|
||||
match $result.ret_code {
|
||||
::ffi::SQLITE_OK => {}
|
||||
crate::ffi::SQLITE_OK => {}
|
||||
code => error!($connection, code),
|
||||
}
|
||||
);
|
||||
@ -139,7 +139,7 @@ macro_rules! ok_descr(
|
||||
macro_rules! ok_raw(
|
||||
($connection:expr, $result:expr) => (
|
||||
match $result {
|
||||
::ffi::SQLITE_OK => {}
|
||||
crate::ffi::SQLITE_OK => {}
|
||||
code => error!($connection, code),
|
||||
}
|
||||
);
|
||||
|
@ -1,6 +1,4 @@
|
||||
extern crate marine_rs_sdk;
|
||||
|
||||
use self::marine_rs_sdk::marine;
|
||||
use marine_rs_sdk::marine;
|
||||
|
||||
pub(crate) type Sqlite3DbHandle = u32;
|
||||
pub(crate) type Sqlite3StmtHandle = u32;
|
||||
|
@ -1,7 +1,7 @@
|
||||
use sqlite3_connector as ffi;
|
||||
use std::marker::PhantomData;
|
||||
use crate::sqlite3_connector as ffi;
|
||||
use crate::{Cursor, Result, Type, Value};
|
||||
|
||||
use {Cursor, Result, Type, Value};
|
||||
use std::marker::PhantomData;
|
||||
|
||||
/// A prepared statement.
|
||||
pub struct Statement {
|
||||
@ -31,7 +31,7 @@ pub trait Readable: Sized {
|
||||
/// Read from a column.
|
||||
///
|
||||
/// The leftmost column has the index 0.
|
||||
fn read(&Statement, usize) -> Result<Self>;
|
||||
fn read(_: &Statement, _: usize) -> Result<Self>;
|
||||
}
|
||||
|
||||
impl Statement {
|
||||
@ -108,7 +108,7 @@ impl Statement {
|
||||
/// Upgrade to a cursor.
|
||||
#[inline]
|
||||
pub fn cursor(self) -> Cursor {
|
||||
::cursor::new(self)
|
||||
crate::cursor::new(self)
|
||||
}
|
||||
|
||||
/// Return the raw pointer.
|
||||
|
101
src/test.rs
101
src/test.rs
@ -1,8 +1,6 @@
|
||||
extern crate marine_rs_sdk;
|
||||
extern crate marine_sqlite_connector;
|
||||
|
||||
use marine_rs_sdk::marine;
|
||||
use marine_sqlite_connector::State;
|
||||
use marine_sqlite_connector::Value;
|
||||
|
||||
pub fn main() {}
|
||||
|
||||
@ -10,31 +8,6 @@ pub fn main() {}
|
||||
pub fn test1() {
|
||||
let connection = marine_sqlite_connector::open(":memory:").unwrap();
|
||||
|
||||
connection
|
||||
.execute(
|
||||
"
|
||||
CREATE TABLE users (name TEXT, age INTEGER);
|
||||
INSERT INTO users VALUES ('Alice', 42);
|
||||
INSERT INTO users VALUES ('Bob', 69);
|
||||
",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
connection
|
||||
.iterate("SELECT * FROM users WHERE age > 50", |pairs| {
|
||||
for &(column, value) in pairs.iter() {
|
||||
println!("{} = {}", column, value.unwrap());
|
||||
}
|
||||
true
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[marine]
|
||||
pub fn test2() {
|
||||
let connection = marine_sqlite_connector::open(":memory:").unwrap();
|
||||
|
||||
println!("connection id = {}\n", connection.as_raw());
|
||||
connection
|
||||
.execute(
|
||||
"
|
||||
@ -51,15 +24,13 @@ pub fn test2() {
|
||||
|
||||
statement.bind(1, 50).unwrap();
|
||||
|
||||
while let State::Row = statement.next().unwrap() {
|
||||
println!("name = {}", statement.read::<String>(0).unwrap());
|
||||
println!("age = {}", statement.read::<i64>(1).unwrap());
|
||||
}
|
||||
assert_eq!(statement.next().unwrap(), State::Row);
|
||||
assert_eq!(statement.read::<String>(0).unwrap(), "Bob");
|
||||
assert_eq!(statement.read::<i64>(1).unwrap(), 69);
|
||||
}
|
||||
#[marine]
|
||||
pub fn test3() {
|
||||
use marine_sqlite_connector::Value;
|
||||
|
||||
#[marine]
|
||||
pub fn test2() {
|
||||
let connection = marine_sqlite_connector::open(":memory:").unwrap();
|
||||
|
||||
connection
|
||||
@ -80,7 +51,63 @@ pub fn test3() {
|
||||
cursor.bind(&[Value::Integer(50)]).unwrap();
|
||||
|
||||
while let Some(row) = cursor.next().unwrap() {
|
||||
println!("name = {}", row[0].as_string().unwrap());
|
||||
println!("age = {}", row[1].as_integer().unwrap());
|
||||
assert_eq!(row[0].as_string().unwrap(), "Bob");
|
||||
assert_eq!(row[1].as_integer().unwrap(), 69);
|
||||
}
|
||||
}
|
||||
|
||||
#[marine]
|
||||
pub fn test3() {
|
||||
let connection = marine_sqlite_connector::open(":memory:").unwrap();
|
||||
|
||||
connection
|
||||
.execute(
|
||||
"
|
||||
CREATE TABLE test (number INTEGER, blob BLOB NOT NULL);
|
||||
",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let mut cursor = connection
|
||||
.prepare("INSERT OR REPLACE INTO test VALUES (?, ?)")
|
||||
.unwrap();
|
||||
|
||||
cursor.bind(1, &Value::Integer(50)).unwrap();
|
||||
cursor.bind(2, &Value::Binary(vec![1, 2, 3])).unwrap();
|
||||
|
||||
// check that blob is not null
|
||||
assert!(cursor.next().is_ok());
|
||||
}
|
||||
|
||||
#[marine]
|
||||
pub fn test4() {
|
||||
let connection = marine_sqlite_connector::open(":memory:").unwrap();
|
||||
|
||||
connection
|
||||
.execute(
|
||||
"
|
||||
CREATE TABLE test (number INTEGER, blob BLOB);
|
||||
",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let mut cursor = connection
|
||||
.prepare("INSERT OR REPLACE INTO test VALUES (?, ?)")
|
||||
.unwrap();
|
||||
|
||||
cursor.bind(1, &Value::Integer(50)).unwrap();
|
||||
cursor.bind(2, &Value::Binary(vec![1, 2, 3])).unwrap();
|
||||
|
||||
cursor.next().unwrap();
|
||||
|
||||
let mut cursor = connection
|
||||
.prepare("SELECT blob FROM test WHERE number = ?")
|
||||
.unwrap()
|
||||
.cursor();
|
||||
|
||||
cursor.bind(&[Value::Integer(50)]).unwrap();
|
||||
|
||||
while let Some(row) = cursor.next().unwrap() {
|
||||
assert_eq!(row[0].as_binary().unwrap().to_vec(), vec![1, 2, 3]);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user