Move declare_vtab into Module trait

This commit is contained in:
gwenn 2018-06-20 20:29:55 +02:00
parent eaee342025
commit 861e8edb96
4 changed files with 21 additions and 18 deletions

View File

@ -6,7 +6,7 @@ use std::rc::Rc;
use ffi; use ffi;
use types::{ToSql, ToSqlOutput, Value}; use types::{ToSql, ToSqlOutput, Value};
use vtab::{self, declare_vtab, Context, IndexInfo, Module, VTab, VTabCursor, Values}; use vtab::{self, Context, IndexInfo, Module, VTab, VTabCursor, Values};
use {Connection, Error, Result}; use {Connection, Error, Result};
// http://sqlite.org/bindptr.html // http://sqlite.org/bindptr.html
@ -66,7 +66,10 @@ impl Module for ArrayModule {
let vtab = ArrayTab { let vtab = ArrayTab {
base: Default::default(), base: Default::default(),
}; };
try!(declare_vtab(db, "CREATE TABLE x(value,pointer hidden)")); try!(ArrayModule::declare_vtab(
db,
"CREATE TABLE x(value,pointer hidden)"
));
Ok(vtab) Ok(vtab)
} }
} }

View File

@ -10,8 +10,8 @@ use std::str;
use ffi; use ffi;
use types::Null; use types::Null;
use vtab::{ use vtab::{
declare_vtab, dequote, escape_double_quote, parse_boolean, Context, IndexInfo, Module, VTab, dequote, escape_double_quote, parse_boolean, Context, IndexInfo, Module, VTab, VTabCursor,
VTabCursor, Values, Values,
}; };
use {Connection, Error, Result}; use {Connection, Error, Result};
@ -230,7 +230,7 @@ impl Module for CSVModule {
schema = Some(sql); schema = Some(sql);
} }
try!(declare_vtab(db, &schema.unwrap())); try!(CSVModule::declare_vtab(db, &schema.unwrap()));
Ok(vtab) Ok(vtab)
} }
} }

View File

@ -64,6 +64,17 @@ pub trait Module {
aux: Option<&Self::Aux>, aux: Option<&Self::Aux>,
args: &[&[u8]], args: &[&[u8]],
) -> Result<Self::Table>; ) -> Result<Self::Table>;
/// Declare the schema of a virtual table.
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))
}
}
} }
/// Virtual table instance trait. /// Virtual table instance trait.
@ -352,17 +363,6 @@ impl InnerConnection {
} }
} }
/// Declare the schema of a virtual table.
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))
}
}
/// Escape double-quote (`"`) character occurences by doubling them (`""`). /// Escape double-quote (`"`) character occurences by doubling them (`""`).
pub fn escape_double_quote(identifier: &str) -> Cow<str> { pub fn escape_double_quote(identifier: &str) -> Cow<str> {
if identifier.contains('"') { if identifier.contains('"') {

View File

@ -5,7 +5,7 @@ use std::os::raw::{c_char, c_int, c_void};
use ffi; use ffi;
use types::Type; use types::Type;
use vtab::{self, declare_vtab, Context, IndexInfo, Module, VTab, VTabCursor, Values}; use vtab::{self, Context, IndexInfo, Module, VTab, VTabCursor, Values};
use {Connection, Error, Result}; use {Connection, Error, Result};
/// Register the "generate_series" module. /// Register the "generate_series" module.
@ -49,7 +49,7 @@ impl Module for Series {
let vtab = SeriesTab { let vtab = SeriesTab {
base: Default::default(), base: Default::default(),
}; };
try!(declare_vtab( try!(Series::declare_vtab(
db, db,
"CREATE TABLE x(value,start hidden,stop hidden,step hidden)" "CREATE TABLE x(value,start hidden,stop hidden,step hidden)"
)); ));