From 861e8edb9668b3ebfa4592973d7481d1cebb0125 Mon Sep 17 00:00:00 2001 From: gwenn Date: Wed, 20 Jun 2018 20:29:55 +0200 Subject: [PATCH] Move declare_vtab into Module trait --- src/vtab/array.rs | 7 +++++-- src/vtab/csvtab.rs | 6 +++--- src/vtab/mod.rs | 22 +++++++++++----------- src/vtab/series.rs | 4 ++-- 4 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/vtab/array.rs b/src/vtab/array.rs index 8d53a1b..677fb02 100644 --- a/src/vtab/array.rs +++ b/src/vtab/array.rs @@ -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) } } diff --git a/src/vtab/csvtab.rs b/src/vtab/csvtab.rs index ebdc290..98c9542 100644 --- a/src/vtab/csvtab.rs +++ b/src/vtab/csvtab.rs @@ -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) } } diff --git a/src/vtab/mod.rs b/src/vtab/mod.rs index 12b24a7..88d36be 100644 --- a/src/vtab/mod.rs +++ b/src/vtab/mod.rs @@ -64,6 +64,17 @@ pub trait Module { aux: Option<&Self::Aux>, args: &[&[u8]], ) -> Result; + + /// 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 { if identifier.contains('"') { diff --git a/src/vtab/series.rs b/src/vtab/series.rs index c088a40..a1de580 100644 --- a/src/vtab/series.rs +++ b/src/vtab/series.rs @@ -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)" ));