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 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};
// http://sqlite.org/bindptr.html
@ -66,7 +66,10 @@ impl Module for ArrayModule {
let vtab = ArrayTab {
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)
}
}

View File

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

View File

@ -64,6 +64,17 @@ pub trait Module {
aux: Option<&Self::Aux>,
args: &[&[u8]],
) -> 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.
@ -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 (`""`).
pub fn escape_double_quote(identifier: &str) -> Cow<str> {
if identifier.contains('"') {

View File

@ -5,7 +5,7 @@ use std::os::raw::{c_char, c_int, c_void};
use ffi;
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};
/// Register the "generate_series" module.
@ -49,7 +49,7 @@ impl Module for Series {
let vtab = SeriesTab {
base: Default::default(),
};
try!(declare_vtab(
try!(Series::declare_vtab(
db,
"CREATE TABLE x(value,start hidden,stop hidden,step hidden)"
));