mirror of
https://github.com/fluencelabs/sqlite-wasm-connector
synced 2025-04-10 02:06:04 +00:00
Rename raw to ffi
This commit is contained in:
parent
f776e1daf6
commit
40e08d5b97
@ -1,5 +1,5 @@
|
|||||||
|
use ffi;
|
||||||
use libc::{c_char, c_int, c_void};
|
use libc::{c_char, c_int, c_void};
|
||||||
use raw;
|
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
@ -7,9 +7,9 @@ use {Result, Statement};
|
|||||||
|
|
||||||
/// A connection to a database.
|
/// A connection to a database.
|
||||||
pub struct Database<'l> {
|
pub struct Database<'l> {
|
||||||
raw: *mut raw::sqlite3,
|
raw: *mut ffi::sqlite3,
|
||||||
busy_callback: Option<Box<FnMut(usize) -> bool + 'l>>,
|
busy_callback: Option<Box<FnMut(usize) -> bool + 'l>>,
|
||||||
phantom: PhantomData<raw::sqlite3>,
|
phantom: PhantomData<ffi::sqlite3>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'l> Database<'l> {
|
impl<'l> Database<'l> {
|
||||||
@ -17,7 +17,7 @@ impl<'l> Database<'l> {
|
|||||||
pub fn open(path: &Path) -> Result<Database<'l>> {
|
pub fn open(path: &Path) -> Result<Database<'l>> {
|
||||||
let mut raw = 0 as *mut _;
|
let mut raw = 0 as *mut _;
|
||||||
unsafe {
|
unsafe {
|
||||||
success!(raw::sqlite3_open(path_to_c_str!(path), &mut raw));
|
success!(ffi::sqlite3_open(path_to_c_str!(path), &mut raw));
|
||||||
}
|
}
|
||||||
Ok(Database {
|
Ok(Database {
|
||||||
raw: raw,
|
raw: raw,
|
||||||
@ -30,7 +30,7 @@ impl<'l> Database<'l> {
|
|||||||
#[inline]
|
#[inline]
|
||||||
pub fn execute(&self, sql: &str) -> Result<()> {
|
pub fn execute(&self, sql: &str) -> Result<()> {
|
||||||
unsafe {
|
unsafe {
|
||||||
success!(self.raw, raw::sqlite3_exec(self.raw, str_to_c_str!(sql), None, 0 as *mut _,
|
success!(self.raw, ffi::sqlite3_exec(self.raw, str_to_c_str!(sql), None, 0 as *mut _,
|
||||||
0 as *mut _));
|
0 as *mut _));
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
@ -46,7 +46,7 @@ impl<'l> Database<'l> {
|
|||||||
{
|
{
|
||||||
unsafe {
|
unsafe {
|
||||||
let callback = Box::new(callback);
|
let callback = Box::new(callback);
|
||||||
success!(self.raw, raw::sqlite3_exec(self.raw, str_to_c_str!(sql),
|
success!(self.raw, ffi::sqlite3_exec(self.raw, str_to_c_str!(sql),
|
||||||
Some(process_callback::<F>),
|
Some(process_callback::<F>),
|
||||||
&*callback as *const F as *mut F as *mut _,
|
&*callback as *const F as *mut F as *mut _,
|
||||||
0 as *mut _));
|
0 as *mut _));
|
||||||
@ -71,7 +71,7 @@ impl<'l> Database<'l> {
|
|||||||
try!(self.remove_busy_handler());
|
try!(self.remove_busy_handler());
|
||||||
unsafe {
|
unsafe {
|
||||||
let callback = Box::new(callback);
|
let callback = Box::new(callback);
|
||||||
let result = raw::sqlite3_busy_handler(self.raw, Some(busy_callback::<F>),
|
let result = ffi::sqlite3_busy_handler(self.raw, Some(busy_callback::<F>),
|
||||||
&*callback as *const F as *mut F as *mut _);
|
&*callback as *const F as *mut F as *mut _);
|
||||||
self.busy_callback = Some(callback);
|
self.busy_callback = Some(callback);
|
||||||
success!(self.raw, result);
|
success!(self.raw, result);
|
||||||
@ -83,7 +83,7 @@ impl<'l> Database<'l> {
|
|||||||
/// rejected operations until a timeout expires.
|
/// rejected operations until a timeout expires.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn set_busy_timeout(&mut self, milliseconds: usize) -> Result<()> {
|
pub fn set_busy_timeout(&mut self, milliseconds: usize) -> Result<()> {
|
||||||
unsafe { success!(self.raw, raw::sqlite3_busy_timeout(self.raw, milliseconds as c_int)) };
|
unsafe { success!(self.raw, ffi::sqlite3_busy_timeout(self.raw, milliseconds as c_int)) };
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,7 +91,7 @@ impl<'l> Database<'l> {
|
|||||||
#[inline]
|
#[inline]
|
||||||
pub fn remove_busy_handler(&mut self) -> Result<()> {
|
pub fn remove_busy_handler(&mut self) -> Result<()> {
|
||||||
::std::mem::replace(&mut self.busy_callback, None);
|
::std::mem::replace(&mut self.busy_callback, None);
|
||||||
unsafe { success!(self.raw, raw::sqlite3_busy_handler(self.raw, None, 0 as *mut _)) };
|
unsafe { success!(self.raw, ffi::sqlite3_busy_handler(self.raw, None, 0 as *mut _)) };
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -101,7 +101,7 @@ impl<'l> Drop for Database<'l> {
|
|||||||
#[allow(unused_must_use)]
|
#[allow(unused_must_use)]
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
self.remove_busy_handler();
|
self.remove_busy_handler();
|
||||||
unsafe { raw::sqlite3_close(self.raw) };
|
unsafe { ffi::sqlite3_close(self.raw) };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
14
src/error.rs
14
src/error.rs
@ -1,4 +1,4 @@
|
|||||||
use raw;
|
use ffi;
|
||||||
use std::convert::{From, Into};
|
use std::convert::{From, Into};
|
||||||
use std::fmt::{self, Display, Formatter};
|
use std::fmt::{self, Display, Formatter};
|
||||||
|
|
||||||
@ -14,14 +14,14 @@ macro_rules! declare(
|
|||||||
/// An error kind.
|
/// An error kind.
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||||
pub enum ErrorKind {
|
pub enum ErrorKind {
|
||||||
$($left = raw::$right as isize,)*
|
$($left = ffi::$right as isize,)*
|
||||||
Unknown,
|
Unknown,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<isize> for ErrorKind {
|
impl From<isize> for ErrorKind {
|
||||||
fn from(code: isize) -> ErrorKind {
|
fn from(code: isize) -> ErrorKind {
|
||||||
match code as ::libc::c_int {
|
match code as ::libc::c_int {
|
||||||
$(raw::$right => ErrorKind::$left,)*
|
$(ffi::$right => ErrorKind::$left,)*
|
||||||
_ => ErrorKind::Unknown,
|
_ => ErrorKind::Unknown,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -95,13 +95,13 @@ impl Display for ErrorKind {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn last(raw: *mut raw::sqlite3) -> Option<Error> {
|
pub fn last(raw: *mut ffi::sqlite3) -> Option<Error> {
|
||||||
unsafe {
|
unsafe {
|
||||||
let code = raw::sqlite3_errcode(raw);
|
let code = ffi::sqlite3_errcode(raw);
|
||||||
if code == raw::SQLITE_OK {
|
if code == ffi::SQLITE_OK {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
let message = raw::sqlite3_errmsg(raw);
|
let message = ffi::sqlite3_errmsg(raw);
|
||||||
if message.is_null() {
|
if message.is_null() {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#![allow(unused_unsafe)]
|
#![allow(unused_unsafe)]
|
||||||
|
|
||||||
extern crate libc;
|
extern crate libc;
|
||||||
extern crate sqlite3_sys as raw;
|
extern crate sqlite3_sys as ffi;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
extern crate temporary;
|
extern crate temporary;
|
||||||
@ -22,13 +22,13 @@ macro_rules! failure(
|
|||||||
macro_rules! success(
|
macro_rules! success(
|
||||||
($database:expr, $result:expr) => (
|
($database:expr, $result:expr) => (
|
||||||
match $result {
|
match $result {
|
||||||
::raw::SQLITE_OK => {},
|
::ffi::SQLITE_OK => {},
|
||||||
code => failure!($database, code),
|
code => failure!($database, code),
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
($result:expr) => (
|
($result:expr) => (
|
||||||
match $result {
|
match $result {
|
||||||
::raw::SQLITE_OK => {},
|
::ffi::SQLITE_OK => {},
|
||||||
code => return Err(::Error::from(::ErrorKind::from(code as isize))),
|
code => return Err(::Error::from(::ErrorKind::from(code as isize))),
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
|
use ffi;
|
||||||
use libc::{c_double, c_int};
|
use libc::{c_double, c_int};
|
||||||
use raw;
|
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
use Result;
|
use Result;
|
||||||
|
|
||||||
/// A prepared statement.
|
/// A prepared statement.
|
||||||
pub struct Statement<'l> {
|
pub struct Statement<'l> {
|
||||||
raw: (*mut raw::sqlite3_stmt, *mut raw::sqlite3),
|
raw: (*mut ffi::sqlite3_stmt, *mut ffi::sqlite3),
|
||||||
phantom: PhantomData<(raw::sqlite3_stmt, &'l raw::sqlite3)>,
|
phantom: PhantomData<(ffi::sqlite3_stmt, &'l ffi::sqlite3)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A binding of a parameter of a prepared statement.
|
/// A binding of a parameter of a prepared statement.
|
||||||
@ -39,17 +39,17 @@ impl<'l> Statement<'l> {
|
|||||||
match *binding {
|
match *binding {
|
||||||
Binding::Float(i, value) => unsafe {
|
Binding::Float(i, value) => unsafe {
|
||||||
debug_assert!(i > 0, "the indexing starts from 1");
|
debug_assert!(i > 0, "the indexing starts from 1");
|
||||||
success!(self.raw.1, raw::sqlite3_bind_double(self.raw.0, i as c_int,
|
success!(self.raw.1, ffi::sqlite3_bind_double(self.raw.0, i as c_int,
|
||||||
value as c_double));
|
value as c_double));
|
||||||
},
|
},
|
||||||
Binding::Integer(i, value) => unsafe {
|
Binding::Integer(i, value) => unsafe {
|
||||||
debug_assert!(i > 0, "the indexing starts from 1");
|
debug_assert!(i > 0, "the indexing starts from 1");
|
||||||
success!(self.raw.1, raw::sqlite3_bind_int64(self.raw.0, i as c_int,
|
success!(self.raw.1, ffi::sqlite3_bind_int64(self.raw.0, i as c_int,
|
||||||
value as raw::sqlite3_int64));
|
value as ffi::sqlite3_int64));
|
||||||
},
|
},
|
||||||
Binding::Text(i, value) => unsafe {
|
Binding::Text(i, value) => unsafe {
|
||||||
debug_assert!(i > 0, "the indexing starts from 1");
|
debug_assert!(i > 0, "the indexing starts from 1");
|
||||||
success!(self.raw.1, raw::sqlite3_bind_text(self.raw.0, i as c_int,
|
success!(self.raw.1, ffi::sqlite3_bind_text(self.raw.0, i as c_int,
|
||||||
str_to_c_str!(value), -1, None));
|
str_to_c_str!(value), -1, None));
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -67,9 +67,9 @@ impl<'l> Statement<'l> {
|
|||||||
|
|
||||||
/// Evaluate the statement.
|
/// Evaluate the statement.
|
||||||
pub fn step(&mut self) -> Result<State> {
|
pub fn step(&mut self) -> Result<State> {
|
||||||
match unsafe { raw::sqlite3_step(self.raw.0) } {
|
match unsafe { ffi::sqlite3_step(self.raw.0) } {
|
||||||
raw::SQLITE_DONE => Ok(State::Done),
|
ffi::SQLITE_DONE => Ok(State::Done),
|
||||||
raw::SQLITE_ROW => Ok(State::Row),
|
ffi::SQLITE_ROW => Ok(State::Row),
|
||||||
code => failure!(self.raw.1, code),
|
code => failure!(self.raw.1, code),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -77,7 +77,7 @@ impl<'l> Statement<'l> {
|
|||||||
/// Reset the statement.
|
/// Reset the statement.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn reset(&mut self) -> Result<()> {
|
pub fn reset(&mut self) -> Result<()> {
|
||||||
unsafe { success!(self.raw.1, raw::sqlite3_reset(self.raw.0)) };
|
unsafe { success!(self.raw.1, ffi::sqlite3_reset(self.raw.0)) };
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -85,26 +85,26 @@ impl<'l> Statement<'l> {
|
|||||||
impl<'l> Drop for Statement<'l> {
|
impl<'l> Drop for Statement<'l> {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
unsafe { raw::sqlite3_finalize(self.raw.0) };
|
unsafe { ffi::sqlite3_finalize(self.raw.0) };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Value for f64 {
|
impl Value for f64 {
|
||||||
fn read(statement: &Statement, i: usize) -> Result<f64> {
|
fn read(statement: &Statement, i: usize) -> Result<f64> {
|
||||||
Ok(unsafe { raw::sqlite3_column_double(statement.raw.0, i as c_int) as f64 })
|
Ok(unsafe { ffi::sqlite3_column_double(statement.raw.0, i as c_int) as f64 })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Value for i64 {
|
impl Value for i64 {
|
||||||
fn read(statement: &Statement, i: usize) -> Result<i64> {
|
fn read(statement: &Statement, i: usize) -> Result<i64> {
|
||||||
Ok(unsafe { raw::sqlite3_column_int64(statement.raw.0, i as c_int) as i64 })
|
Ok(unsafe { ffi::sqlite3_column_int64(statement.raw.0, i as c_int) as i64 })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Value for String {
|
impl Value for String {
|
||||||
fn read(statement: &Statement, i: usize) -> Result<String> {
|
fn read(statement: &Statement, i: usize) -> Result<String> {
|
||||||
unsafe {
|
unsafe {
|
||||||
let pointer = raw::sqlite3_column_text(statement.raw.0, i as c_int);
|
let pointer = ffi::sqlite3_column_text(statement.raw.0, i as c_int);
|
||||||
if pointer.is_null() {
|
if pointer.is_null() {
|
||||||
raise!("cannot read a TEXT column");
|
raise!("cannot read a TEXT column");
|
||||||
}
|
}
|
||||||
@ -114,10 +114,10 @@ impl Value for String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn new<'l>(raw1: *mut raw::sqlite3, sql: &str) -> Result<Statement<'l>> {
|
pub fn new<'l>(raw1: *mut ffi::sqlite3, sql: &str) -> Result<Statement<'l>> {
|
||||||
let mut raw0 = 0 as *mut _;
|
let mut raw0 = 0 as *mut _;
|
||||||
unsafe {
|
unsafe {
|
||||||
success!(raw1, raw::sqlite3_prepare(raw1, str_to_c_str!(sql), -1, &mut raw0, 0 as *mut _));
|
success!(raw1, ffi::sqlite3_prepare(raw1, str_to_c_str!(sql), -1, &mut raw0, 0 as *mut _));
|
||||||
}
|
}
|
||||||
Ok(Statement { raw: (raw0, raw1), phantom: PhantomData })
|
Ok(Statement { raw: (raw0, raw1), phantom: PhantomData })
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user