2016-01-11 03:56:04 +08:00
|
|
|
//! Create virtual tables.
|
|
|
|
//! (See http://sqlite.org/vtab.html)
|
2016-08-13 17:54:19 +08:00
|
|
|
use std::borrow::Cow::{self, Borrowed, Owned};
|
2016-01-24 01:34:09 +08:00
|
|
|
use std::ffi::CString;
|
2016-01-11 03:56:04 +08:00
|
|
|
use std::mem;
|
2016-02-09 01:06:11 +08:00
|
|
|
use std::ptr;
|
2016-01-24 01:34:09 +08:00
|
|
|
use libc;
|
2016-01-11 03:56:04 +08:00
|
|
|
|
2016-01-24 01:34:09 +08:00
|
|
|
use {Connection, Error, Result, InnerConnection, str_to_cstring};
|
|
|
|
use error::error_from_sqlite_code;
|
2016-01-11 03:56:04 +08:00
|
|
|
use ffi;
|
2016-08-13 23:46:49 +08:00
|
|
|
use functions::ToResult;
|
2016-01-11 03:56:04 +08:00
|
|
|
|
2016-01-24 01:34:09 +08:00
|
|
|
// let conn: Connection = ...;
|
|
|
|
// let mod: Module = ...; // VTab builder
|
|
|
|
// conn.create_module("module", mod);
|
|
|
|
//
|
|
|
|
// conn.execute("CREATE VIRTUAL TABLE foo USING module(...)");
|
|
|
|
// \-> Module::xcreate
|
|
|
|
// |-> let vtab: VTab = ...; // on the heap
|
|
|
|
// \-> conn.declare_vtab("CREATE TABLE foo (...)");
|
|
|
|
// conn = Connection::open(...);
|
|
|
|
// \-> Module::xconnect
|
|
|
|
// |-> let vtab: VTab = ...; // on the heap
|
|
|
|
// \-> conn.declare_vtab("CREATE TABLE foo (...)");
|
|
|
|
//
|
|
|
|
// conn.close();
|
|
|
|
// \-> vtab.xdisconnect
|
|
|
|
// conn.execute("DROP TABLE foo");
|
|
|
|
// \-> vtab.xDestroy
|
|
|
|
//
|
|
|
|
// let stmt = conn.prepare("SELECT ... FROM foo WHERE ...");
|
|
|
|
// \-> vtab.xbestindex
|
2016-02-09 01:06:11 +08:00
|
|
|
// stmt.query().next();
|
2016-01-24 01:34:09 +08:00
|
|
|
// \-> vtab.xopen
|
|
|
|
// |-> let cursor: Cursor = ...; // on the heap
|
|
|
|
// |-> cursor.xfilter or xnext
|
|
|
|
// |-> cursor.xeof
|
|
|
|
// \-> if not eof { cursor.column or xrowid } else { cursor.xclose }
|
|
|
|
//
|
2016-01-11 03:56:04 +08:00
|
|
|
|
2016-02-12 02:16:05 +08:00
|
|
|
/// Virtual table instance trait.
|
2016-02-11 01:15:46 +08:00
|
|
|
pub trait VTab<C: VTabCursor<Self>>: Sized {
|
2016-02-12 02:16:05 +08:00
|
|
|
/// Create a new instance of a virtual table in response to a CREATE VIRTUAL TABLE statement.
|
|
|
|
/// The `db` parameter is a pointer to the SQLite database connection that is executing the CREATE VIRTUAL TABLE statement.
|
2016-08-13 19:55:30 +08:00
|
|
|
fn create(db: *mut ffi::sqlite3, aux: *mut libc::c_void, args: &[&[u8]]) -> Result<Self>;
|
2016-02-12 02:16:05 +08:00
|
|
|
/// Determine the best way to access the virtual table.
|
2016-02-11 03:30:08 +08:00
|
|
|
fn best_index(&self, info: *mut ffi::sqlite3_index_info);
|
2016-02-12 02:16:05 +08:00
|
|
|
/// Create a new cursor used for accessing a virtual table.
|
2016-02-11 01:15:46 +08:00
|
|
|
fn open(&self) -> Result<C>;
|
|
|
|
}
|
|
|
|
|
2016-02-12 02:16:05 +08:00
|
|
|
/// Virtual table cursor trait.
|
2016-02-11 01:15:46 +08:00
|
|
|
pub trait VTabCursor<V: VTab<Self>>: Sized {
|
2016-02-12 02:16:05 +08:00
|
|
|
/// Accessor to the associated virtual table.
|
2016-02-11 01:15:46 +08:00
|
|
|
fn vtab(&self) -> &mut V;
|
2016-02-12 02:16:05 +08:00
|
|
|
/// Begin a search of a virtual table.
|
|
|
|
fn filter(&mut self,
|
|
|
|
idx_num: libc::c_int,
|
2016-08-13 19:55:30 +08:00
|
|
|
idx_str: Option<&str>,
|
|
|
|
args: &mut [*mut ffi::sqlite3_value])
|
2016-02-12 02:16:05 +08:00
|
|
|
-> Result<()>;
|
|
|
|
/// Advance cursor to the next row of a result set initiated by `filter`.
|
2016-02-11 01:15:46 +08:00
|
|
|
fn next(&mut self) -> Result<()>;
|
2016-02-12 02:16:05 +08:00
|
|
|
/// Must return `false` if the cursor currently points to a valid row of data, or `true` otherwise.
|
2016-02-11 01:15:46 +08:00
|
|
|
fn eof(&self) -> bool;
|
2016-02-12 02:16:05 +08:00
|
|
|
/// Find the value for the `i`-th column of the current row. `i` is zero-based so the first column is numbered 0.
|
|
|
|
/// May return its result back to SQLite using one of the specified `ctx`.
|
2016-08-13 23:46:49 +08:00
|
|
|
fn column(&self, ctx: &mut Context, i: libc::c_int) -> Result<()>;
|
2016-02-12 02:16:05 +08:00
|
|
|
/// Return the rowid of row that the cursor is currently pointing at.
|
2016-02-11 01:15:46 +08:00
|
|
|
fn rowid(&self) -> Result<i64>;
|
|
|
|
}
|
|
|
|
|
2016-08-13 23:46:49 +08:00
|
|
|
pub struct Context(*mut ffi::sqlite3_context);
|
|
|
|
|
|
|
|
impl Context {
|
|
|
|
pub fn set_result(&mut self, value: &ToResult) {
|
|
|
|
unsafe {
|
|
|
|
value.set_result(self.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-11 03:56:04 +08:00
|
|
|
impl Connection {
|
|
|
|
/// Register a virtual table implementation.
|
2016-01-24 01:34:09 +08:00
|
|
|
pub fn create_module<A>(&self,
|
|
|
|
module_name: &str,
|
|
|
|
module: *const ffi::sqlite3_module,
|
2016-02-09 01:06:11 +08:00
|
|
|
aux: Option<A>)
|
2016-01-24 01:34:09 +08:00
|
|
|
-> Result<()> {
|
2016-01-11 03:56:04 +08:00
|
|
|
self.db
|
|
|
|
.borrow_mut()
|
2016-01-24 01:34:09 +08:00
|
|
|
.create_module(module_name, module, aux)
|
2016-01-11 03:56:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-24 19:18:21 +08:00
|
|
|
impl InnerConnection {
|
|
|
|
fn create_module<A>(&mut self,
|
|
|
|
module_name: &str,
|
|
|
|
module: *const ffi::sqlite3_module,
|
2016-02-09 01:06:11 +08:00
|
|
|
aux: Option<A>)
|
2016-01-24 19:18:21 +08:00
|
|
|
-> Result<()> {
|
|
|
|
let c_name = try!(str_to_cstring(module_name));
|
2016-02-09 01:06:11 +08:00
|
|
|
let r = match aux {
|
|
|
|
Some(aux) => {
|
|
|
|
let boxed_aux: *mut A = Box::into_raw(Box::new(aux));
|
|
|
|
unsafe {
|
|
|
|
ffi::sqlite3_create_module_v2(self.db(),
|
|
|
|
c_name.as_ptr(),
|
|
|
|
module,
|
|
|
|
mem::transmute(boxed_aux),
|
2016-03-31 00:28:03 +08:00
|
|
|
Some(free_boxed_value::<A>))
|
2016-02-09 01:06:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
None => unsafe {
|
|
|
|
ffi::sqlite3_create_module_v2(self.db(),
|
|
|
|
c_name.as_ptr(),
|
|
|
|
module,
|
|
|
|
ptr::null_mut(),
|
|
|
|
None)
|
|
|
|
},
|
2016-01-24 19:18:21 +08:00
|
|
|
};
|
|
|
|
self.decode_result(r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-12 02:16:05 +08:00
|
|
|
/// Declare the schema of a virtual table.
|
2016-02-04 03:11:34 +08:00
|
|
|
pub fn declare_vtab(db: *mut ffi::sqlite3, sql: &str) -> Result<()> {
|
|
|
|
let c_sql = try!(CString::new(sql));
|
|
|
|
let rc = unsafe { ffi::sqlite3_declare_vtab(db, c_sql.as_ptr()) };
|
|
|
|
if rc == ffi::SQLITE_OK {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(error_from_sqlite_code(rc, None))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-12 02:16:05 +08:00
|
|
|
/// Escape double-quote (`"`) character occurences by doubling them (`""`).
|
2016-04-02 23:16:17 +08:00
|
|
|
pub fn escape_double_quote(identifier: &str) -> Cow<str> {
|
2016-02-12 02:16:05 +08:00
|
|
|
if identifier.contains('"') {
|
|
|
|
// escape quote by doubling them
|
2016-02-12 04:19:18 +08:00
|
|
|
Owned(identifier.replace("\"", "\"\""))
|
2016-02-12 02:16:05 +08:00
|
|
|
} else {
|
2016-02-12 04:19:18 +08:00
|
|
|
Borrowed(identifier)
|
2016-02-12 02:16:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-11 03:56:04 +08:00
|
|
|
// FIXME copy/paste from function.rs
|
|
|
|
unsafe extern "C" fn free_boxed_value<T>(p: *mut libc::c_void) {
|
|
|
|
let _: Box<T> = Box::from_raw(mem::transmute(p));
|
|
|
|
}
|
|
|
|
|
2016-02-03 04:16:10 +08:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! init_module {
|
|
|
|
($module_name: ident, $vtab: ident, $cursor: ty,
|
|
|
|
$create: ident, $best_index: ident, $destroy: ident,
|
|
|
|
$open: ident, $close: ident,
|
|
|
|
$filter: ident, $next: ident, $eof: ident,
|
|
|
|
$column: ident, $rowid: ident) => {
|
|
|
|
|
|
|
|
static $module_name: ffi::sqlite3_module = ffi::sqlite3_module {
|
2016-01-11 03:56:04 +08:00
|
|
|
iVersion: 1,
|
2016-02-03 04:16:10 +08:00
|
|
|
xCreate: Some($create),
|
|
|
|
xConnect: Some($create), /* A virtual table is eponymous if its xCreate method is the exact same function as the xConnect method */
|
|
|
|
xBestIndex: Some($best_index),
|
|
|
|
xDisconnect: Some($destroy),
|
|
|
|
xDestroy: Some($destroy),
|
|
|
|
xOpen: Some($open),
|
|
|
|
xClose: Some($close),
|
|
|
|
xFilter: Some($filter),
|
|
|
|
xNext: Some($next),
|
|
|
|
xEof: Some($eof),
|
|
|
|
xColumn: Some($column),
|
|
|
|
xRowid: Some($rowid),
|
2016-01-11 03:56:04 +08:00
|
|
|
xUpdate: None, // TODO
|
|
|
|
xBegin: None,
|
|
|
|
xSync: None,
|
|
|
|
xCommit: None,
|
|
|
|
xRollback: None,
|
|
|
|
xFindFunction: None,
|
|
|
|
xRename: None,
|
|
|
|
xSavepoint: None,
|
|
|
|
xRelease: None,
|
|
|
|
xRollbackTo: None,
|
|
|
|
};
|
|
|
|
|
2016-02-03 04:16:10 +08:00
|
|
|
unsafe extern "C" fn $create(db: *mut ffi::sqlite3,
|
2016-01-24 01:34:09 +08:00
|
|
|
aux: *mut libc::c_void,
|
|
|
|
argc: libc::c_int,
|
|
|
|
argv: *const *const libc::c_char,
|
|
|
|
pp_vtab: *mut *mut ffi::sqlite3_vtab,
|
|
|
|
err_msg: *mut *mut libc::c_char)
|
|
|
|
-> libc::c_int {
|
2016-02-09 01:06:11 +08:00
|
|
|
use std::error::Error as StdError;
|
2016-08-13 17:54:19 +08:00
|
|
|
use std::ffi::CStr;
|
2016-02-11 03:30:08 +08:00
|
|
|
use std::slice;
|
2016-02-09 01:06:11 +08:00
|
|
|
use vtab::mprintf;
|
2016-02-11 03:30:08 +08:00
|
|
|
let args = slice::from_raw_parts(argv, argc as usize);
|
2016-08-13 17:54:19 +08:00
|
|
|
let vec = args.iter().map(|cs| {
|
|
|
|
CStr::from_ptr(*cs).to_bytes()
|
|
|
|
}).collect::<Vec<_>>();
|
|
|
|
match $vtab::create(db, aux, &vec[..]) {
|
2016-01-24 01:34:09 +08:00
|
|
|
Ok(vtab) => {
|
2016-02-03 04:16:10 +08:00
|
|
|
let boxed_vtab: *mut $vtab = Box::into_raw(Box::new(vtab));
|
2016-01-24 01:34:09 +08:00
|
|
|
*pp_vtab = boxed_vtab as *mut ffi::sqlite3_vtab;
|
|
|
|
ffi::SQLITE_OK
|
2016-02-13 03:17:42 +08:00
|
|
|
},
|
2016-02-09 01:06:11 +08:00
|
|
|
Err(Error::SqliteFailure(err, s)) => {
|
|
|
|
if let Some(s) = s {
|
|
|
|
*err_msg = mprintf(&s);
|
2016-01-24 01:34:09 +08:00
|
|
|
}
|
2016-02-09 01:06:11 +08:00
|
|
|
err.extended_code
|
|
|
|
},
|
|
|
|
Err(err) => {
|
|
|
|
*err_msg = mprintf(err.description());
|
|
|
|
ffi::SQLITE_ERROR
|
2016-01-24 01:34:09 +08:00
|
|
|
}
|
|
|
|
}
|
2016-01-11 03:56:04 +08:00
|
|
|
}
|
2016-02-03 04:16:10 +08:00
|
|
|
unsafe extern "C" fn $best_index(vtab: *mut ffi::sqlite3_vtab,
|
|
|
|
info: *mut ffi::sqlite3_index_info)
|
|
|
|
-> libc::c_int {
|
|
|
|
let vtab = vtab as *mut $vtab;
|
|
|
|
(*vtab).best_index(info);
|
|
|
|
ffi::SQLITE_OK
|
|
|
|
}
|
|
|
|
unsafe extern "C" fn $destroy(vtab: *mut ffi::sqlite3_vtab) -> libc::c_int {
|
|
|
|
let vtab = vtab as *mut $vtab;
|
2016-04-02 23:16:17 +08:00
|
|
|
let _: Box<$vtab> = Box::from_raw(vtab);
|
2016-02-03 04:16:10 +08:00
|
|
|
ffi::SQLITE_OK
|
|
|
|
}
|
2016-01-24 01:34:09 +08:00
|
|
|
|
2016-02-03 04:16:10 +08:00
|
|
|
unsafe extern "C" fn $open(vtab: *mut ffi::sqlite3_vtab,
|
|
|
|
pp_cursor: *mut *mut ffi::sqlite3_vtab_cursor)
|
|
|
|
-> libc::c_int {
|
2016-02-11 01:07:58 +08:00
|
|
|
use std::error::Error as StdError;
|
2016-02-09 01:06:11 +08:00
|
|
|
use vtab::set_err_msg;
|
|
|
|
let vt = vtab as *mut $vtab;
|
|
|
|
match (*vt).open() {
|
|
|
|
Ok(cursor) => {
|
|
|
|
let boxed_cursor: *mut $cursor = Box::into_raw(Box::new(cursor));
|
|
|
|
*pp_cursor = boxed_cursor as *mut ffi::sqlite3_vtab_cursor;
|
|
|
|
ffi::SQLITE_OK
|
|
|
|
},
|
|
|
|
Err(Error::SqliteFailure(err, s)) => {
|
|
|
|
if let Some(err_msg) = s {
|
|
|
|
set_err_msg(vtab, &err_msg);
|
|
|
|
}
|
|
|
|
err.extended_code
|
2016-02-13 03:17:42 +08:00
|
|
|
},
|
2016-02-11 01:07:58 +08:00
|
|
|
Err(err) => {
|
|
|
|
set_err_msg(vtab, err.description());
|
2016-02-09 01:06:11 +08:00
|
|
|
ffi::SQLITE_ERROR
|
|
|
|
}
|
|
|
|
}
|
2016-02-03 04:16:10 +08:00
|
|
|
}
|
|
|
|
unsafe extern "C" fn $close(cursor: *mut ffi::sqlite3_vtab_cursor) -> libc::c_int {
|
2016-02-09 01:06:11 +08:00
|
|
|
let cr = cursor as *mut $cursor;
|
2016-04-02 23:16:17 +08:00
|
|
|
let _: Box<$cursor> = Box::from_raw(cr);
|
2016-02-03 04:16:10 +08:00
|
|
|
ffi::SQLITE_OK
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe extern "C" fn $filter(cursor: *mut ffi::sqlite3_vtab_cursor,
|
2016-02-12 02:16:05 +08:00
|
|
|
idx_num: libc::c_int,
|
|
|
|
idx_str: *const libc::c_char,
|
|
|
|
argc: libc::c_int,
|
|
|
|
argv: *mut *mut ffi::sqlite3_value)
|
2016-02-03 04:16:10 +08:00
|
|
|
-> libc::c_int {
|
2016-08-13 19:55:30 +08:00
|
|
|
use std::ffi::CStr;
|
2016-08-13 19:12:48 +08:00
|
|
|
use std::slice;
|
2016-08-13 19:55:30 +08:00
|
|
|
use std::str;
|
2016-02-09 01:06:11 +08:00
|
|
|
use vtab::cursor_error;
|
2016-08-13 19:55:30 +08:00
|
|
|
let idx_name = if idx_str.is_null() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
let c_slice = CStr::from_ptr(idx_str).to_bytes();
|
|
|
|
Some(str::from_utf8_unchecked(c_slice))
|
|
|
|
};
|
2016-08-13 19:12:48 +08:00
|
|
|
let mut args = slice::from_raw_parts_mut(argv, argc as usize);
|
2016-02-09 01:06:11 +08:00
|
|
|
let cr = cursor as *mut $cursor;
|
2016-08-13 19:55:30 +08:00
|
|
|
cursor_error(cursor, (*cr).filter(idx_num, idx_name, &mut args))
|
2016-02-03 04:16:10 +08:00
|
|
|
}
|
|
|
|
unsafe extern "C" fn $next(cursor: *mut ffi::sqlite3_vtab_cursor) -> libc::c_int {
|
2016-02-09 01:06:11 +08:00
|
|
|
use vtab::cursor_error;
|
|
|
|
let cr = cursor as *mut $cursor;
|
|
|
|
cursor_error(cursor, (*cr).next())
|
2016-02-03 04:16:10 +08:00
|
|
|
}
|
|
|
|
unsafe extern "C" fn $eof(cursor: *mut ffi::sqlite3_vtab_cursor) -> libc::c_int {
|
2016-02-09 01:06:11 +08:00
|
|
|
let cr = cursor as *mut $cursor;
|
|
|
|
(*cr).eof() as libc::c_int
|
2016-02-03 04:16:10 +08:00
|
|
|
}
|
|
|
|
unsafe extern "C" fn $column(cursor: *mut ffi::sqlite3_vtab_cursor,
|
|
|
|
ctx: *mut ffi::sqlite3_context,
|
|
|
|
i: libc::c_int)
|
|
|
|
-> libc::c_int {
|
2016-08-13 23:46:49 +08:00
|
|
|
use vtab::{result_error, Context};
|
2016-02-09 01:06:11 +08:00
|
|
|
let cr = cursor as *mut $cursor;
|
2016-08-13 23:46:49 +08:00
|
|
|
let mut ctxt = Context(ctx);
|
|
|
|
result_error(ctx, (*cr).column(&mut ctxt, i))
|
2016-02-03 04:16:10 +08:00
|
|
|
}
|
|
|
|
unsafe extern "C" fn $rowid(cursor: *mut ffi::sqlite3_vtab_cursor,
|
|
|
|
p_rowid: *mut ffi::sqlite3_int64)
|
|
|
|
-> libc::c_int {
|
2016-02-09 01:06:11 +08:00
|
|
|
use vtab::cursor_error;
|
|
|
|
let cr = cursor as *mut $cursor;
|
|
|
|
match (*cr).rowid() {
|
|
|
|
Ok(rowid) => {
|
|
|
|
*p_rowid = rowid;
|
|
|
|
ffi::SQLITE_OK
|
|
|
|
},
|
|
|
|
err => cursor_error(cursor, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-12 02:16:05 +08:00
|
|
|
/// Virtual table cursors can set an error message by assigning a string to `zErrMsg`.
|
2016-02-09 01:06:11 +08:00
|
|
|
pub unsafe fn cursor_error<T>(cursor: *mut ffi::sqlite3_vtab_cursor,
|
|
|
|
result: Result<T>)
|
|
|
|
-> libc::c_int {
|
2016-02-11 01:07:58 +08:00
|
|
|
use std::error::Error as StdError;
|
2016-02-09 01:06:11 +08:00
|
|
|
match result {
|
|
|
|
Ok(_) => ffi::SQLITE_OK,
|
|
|
|
Err(Error::SqliteFailure(err, s)) => {
|
|
|
|
if let Some(err_msg) = s {
|
|
|
|
set_err_msg((*cursor).pVtab, &err_msg);
|
|
|
|
}
|
|
|
|
err.extended_code
|
|
|
|
}
|
2016-02-11 01:07:58 +08:00
|
|
|
Err(err) => {
|
|
|
|
set_err_msg((*cursor).pVtab, err.description());
|
2016-02-09 01:06:11 +08:00
|
|
|
ffi::SQLITE_ERROR
|
|
|
|
}
|
|
|
|
}
|
2016-01-11 03:56:04 +08:00
|
|
|
}
|
2016-02-09 01:06:11 +08:00
|
|
|
|
2016-02-12 02:16:05 +08:00
|
|
|
/// Virtual tables methods can set an error message by assigning a string to `zErrMsg`.
|
2016-02-11 01:15:46 +08:00
|
|
|
pub unsafe fn set_err_msg(vtab: *mut ffi::sqlite3_vtab, err_msg: &str) {
|
2016-02-09 01:06:11 +08:00
|
|
|
if !(*vtab).zErrMsg.is_null() {
|
|
|
|
ffi::sqlite3_free((*vtab).zErrMsg as *mut libc::c_void);
|
2016-02-03 04:16:10 +08:00
|
|
|
}
|
2016-02-09 01:06:11 +08:00
|
|
|
(*vtab).zErrMsg = mprintf(err_msg);
|
2016-02-03 04:16:10 +08:00
|
|
|
}
|
|
|
|
|
2016-02-12 02:16:05 +08:00
|
|
|
/// To raise an error, the `column` method should use this method to set the error message and return the error code.
|
|
|
|
pub unsafe fn result_error<T>(ctx: *mut ffi::sqlite3_context, result: Result<T>) -> libc::c_int {
|
2016-02-09 01:06:11 +08:00
|
|
|
use std::error::Error as StdError;
|
2016-02-12 02:16:05 +08:00
|
|
|
match result {
|
|
|
|
Ok(_) => ffi::SQLITE_OK,
|
|
|
|
Err(Error::SqliteFailure(err, s)) => {
|
|
|
|
match err.extended_code {
|
|
|
|
ffi::SQLITE_TOOBIG => {
|
|
|
|
ffi::sqlite3_result_error_toobig(ctx);
|
|
|
|
}
|
|
|
|
ffi::SQLITE_NOMEM => {
|
|
|
|
ffi::sqlite3_result_error_nomem(ctx);
|
|
|
|
}
|
|
|
|
code => {
|
|
|
|
ffi::sqlite3_result_error_code(ctx, code);
|
|
|
|
if let Some(Ok(cstr)) = s.map(|s| str_to_cstring(&s)) {
|
|
|
|
ffi::sqlite3_result_error(ctx, cstr.as_ptr(), -1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2016-01-24 01:34:09 +08:00
|
|
|
err.extended_code
|
|
|
|
}
|
2016-02-12 02:16:05 +08:00
|
|
|
Err(err) => {
|
|
|
|
ffi::sqlite3_result_error_code(ctx, ffi::SQLITE_ERROR);
|
2016-01-24 01:34:09 +08:00
|
|
|
if let Ok(cstr) = str_to_cstring(err.description()) {
|
|
|
|
ffi::sqlite3_result_error(ctx, cstr.as_ptr(), -1);
|
|
|
|
}
|
2016-02-12 02:16:05 +08:00
|
|
|
ffi::SQLITE_ERROR
|
2016-01-24 01:34:09 +08:00
|
|
|
}
|
2016-01-11 03:56:04 +08:00
|
|
|
}
|
2016-01-24 01:34:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Space to hold this error message string must be obtained from an SQLite memory allocation function.
|
2016-02-04 03:11:34 +08:00
|
|
|
pub fn mprintf(err_msg: &str) -> *mut ::libc::c_char {
|
2016-01-24 01:34:09 +08:00
|
|
|
let c_format = CString::new("%s").unwrap();
|
|
|
|
let c_err = CString::new(err_msg).unwrap();
|
2016-01-24 19:18:21 +08:00
|
|
|
unsafe { ffi::sqlite3_mprintf(c_format.as_ptr(), c_err.as_ptr()) }
|
2016-01-11 03:56:04 +08:00
|
|
|
}
|
|
|
|
|
2016-02-09 01:06:11 +08:00
|
|
|
pub mod int_array;
|
2016-08-13 19:55:30 +08:00
|
|
|
#[cfg(feature = "csvtab")]
|
|
|
|
pub mod csvtab;
|