Remove macros

This commit is contained in:
gwenn
2018-07-14 18:47:52 +02:00
parent b19d050e39
commit e2df03f474
8 changed files with 406 additions and 548 deletions

View File

@@ -2,16 +2,15 @@
//! Port of [csv](http://www.sqlite.org/cgi/src/finfo?name=ext/misc/csv.c) C extension.
extern crate csv;
use std::fs::File;
use std::os::raw::{c_char, c_int, c_void};
use std::os::raw::c_int;
use std::path::Path;
use std::result;
use std::str;
use error::error_from_sqlite_code;
use ffi;
use types::Null;
use vtab::{
dequote, escape_double_quote, parse_boolean, Context, IndexInfo, Module, VTab, VTabConnection,
dequote, escape_double_quote, parse_boolean, simple_module, Context, IndexInfo, Module, VTab, VTabConnection,
VTabCursor, Values,
};
use {Connection, Error, Result};
@@ -29,32 +28,36 @@ use {Connection, Error, Result};
/// ```
pub fn load_module(conn: &Connection) -> Result<()> {
let aux: Option<()> = None;
conn.create_module("csv", CSVModule(&CSV_MODULE), aux)
conn.create_module::<CSVTab>("csv", &CSV_MODULE, aux)
}
init_module!(
CSV_MODULE,
CSVModule,
CSVTab,
(),
CSVTabCursor,
csv_create,
csv_connect,
csv_best_index,
csv_disconnect,
csv_disconnect,
csv_open,
csv_close,
csv_filter,
csv_next,
csv_eof,
csv_column,
csv_rowid
);
lazy_static! {
static ref CSV_MODULE: Module<CSVTab> = simple_module::<CSVTab>();
}
struct CSVModule(&'static ffi::sqlite3_module);
/// An instance of the CSV virtual table
#[repr(C)]
struct CSVTab {
/// Base class. Must be first
base: ffi::sqlite3_vtab,
/// Name of the CSV file
filename: String,
has_headers: bool,
delimiter: u8,
quote: u8,
/// Offset to start of data
offset_first_row: csv::Position,
}
impl CSVTab {
fn reader(&self) -> result::Result<csv::Reader<File>, csv::Error> {
csv::ReaderBuilder::new()
.has_headers(self.has_headers)
.delimiter(self.delimiter)
.quote(self.quote)
.from_path(&self.filename)
}
impl CSVModule {
fn parameter(c_slice: &[u8]) -> Result<(&str, &str)> {
let arg = try!(str::from_utf8(c_slice)).trim();
let mut split = arg.split('=');
@@ -77,13 +80,9 @@ impl CSVModule {
}
}
impl Module for CSVModule {
impl VTab for CSVTab {
type Aux = ();
type Table = CSVTab;
fn as_ptr(&self) -> *const ffi::sqlite3_module {
self.0
}
type Cursor = CSVTabCursor;
fn connect(
_: &mut VTabConnection,
@@ -107,7 +106,7 @@ impl Module for CSVModule {
let args = &args[3..];
for c_slice in args {
let (param, value) = try!(CSVModule::parameter(c_slice));
let (param, value) = try!(CSVTab::parameter(c_slice));
match param {
"filename" => {
if !Path::new(value).exists() {
@@ -151,7 +150,7 @@ impl Module for CSVModule {
}
}
"delimiter" => {
if let Some(b) = CSVModule::parse_byte(value) {
if let Some(b) = CSVTab::parse_byte(value) {
vtab.delimiter = b;
} else {
return Err(Error::ModuleError(format!(
@@ -161,7 +160,7 @@ impl Module for CSVModule {
}
}
"quote" => {
if let Some(b) = CSVModule::parse_byte(value) {
if let Some(b) = CSVTab::parse_byte(value) {
if b == b'0' {
vtab.quote = 0;
} else {
@@ -237,34 +236,6 @@ impl Module for CSVModule {
Ok((schema.unwrap().to_owned(), vtab))
}
}
/// An instance of the CSV virtual table
#[repr(C)]
struct CSVTab {
/// Base class. Must be first
base: ffi::sqlite3_vtab,
/// Name of the CSV file
filename: String,
has_headers: bool,
delimiter: u8,
quote: u8,
/// Offset to start of data
offset_first_row: csv::Position,
}
impl CSVTab {
fn reader(&self) -> result::Result<csv::Reader<File>, csv::Error> {
csv::ReaderBuilder::new()
.has_headers(self.has_headers)
.delimiter(self.delimiter)
.quote(self.quote)
.from_path(&self.filename)
}
}
impl VTab for CSVTab {
type Cursor = CSVTabCursor;
// Only a forward full table scan is supported.
fn best_index(&self, info: &mut IndexInfo) -> Result<()> {