2021-06-13 15:17:35 +08:00
|
|
|
//! CSV Virtual Table.
|
2018-07-15 16:19:18 +08:00
|
|
|
//!
|
2020-04-07 02:04:45 +08:00
|
|
|
//! Port of [csv](http://www.sqlite.org/cgi/src/finfo?name=ext/misc/csv.c) C
|
2020-11-07 19:32:41 +08:00
|
|
|
//! extension: `https://www.sqlite.org/csv.html`
|
2020-04-06 13:15:27 +08:00
|
|
|
//!
|
|
|
|
//! # Example
|
|
|
|
//!
|
|
|
|
//! ```rust,no_run
|
|
|
|
//! # use rusqlite::{Connection, Result};
|
|
|
|
//! fn example() -> Result<()> {
|
|
|
|
//! // Note: This should be done once (usually when opening the DB).
|
|
|
|
//! let db = Connection::open_in_memory()?;
|
|
|
|
//! rusqlite::vtab::csvtab::load_module(&db)?;
|
|
|
|
//! // Assum3e my_csv.csv
|
|
|
|
//! let schema = "
|
|
|
|
//! CREATE VIRTUAL TABLE my_csv_data
|
|
|
|
//! USING csv(filename = 'my_csv.csv')
|
|
|
|
//! ";
|
|
|
|
//! db.execute_batch(schema)?;
|
|
|
|
//! // Now the `my_csv_data` (virtual) table can be queried as normal...
|
|
|
|
//! Ok(())
|
|
|
|
//! }
|
|
|
|
//! ```
|
2016-02-09 01:06:11 +08:00
|
|
|
use std::fs::File;
|
2020-06-01 15:48:49 +08:00
|
|
|
use std::marker::PhantomData;
|
2018-07-15 00:47:52 +08:00
|
|
|
use std::os::raw::c_int;
|
2016-02-13 03:17:42 +08:00
|
|
|
use std::path::Path;
|
2016-02-11 03:30:08 +08:00
|
|
|
use std::str;
|
2016-02-09 01:06:11 +08:00
|
|
|
|
2018-10-31 03:11:35 +08:00
|
|
|
use crate::ffi;
|
|
|
|
use crate::types::Null;
|
|
|
|
use crate::vtab::{
|
2018-07-22 15:51:06 +08:00
|
|
|
dequote, escape_double_quote, parse_boolean, read_only_module, Context, CreateVTab, IndexInfo,
|
2022-03-18 02:58:02 +08:00
|
|
|
VTab, VTabConfig, VTabConnection, VTabCursor, Values,
|
2018-05-06 23:21:36 +08:00
|
|
|
};
|
2018-10-31 03:11:35 +08:00
|
|
|
use crate::{Connection, Error, Result};
|
2016-02-09 01:06:11 +08:00
|
|
|
|
2021-06-13 15:17:35 +08:00
|
|
|
/// Register the "csv" module.
|
2018-05-06 18:41:42 +08:00
|
|
|
/// ```sql
|
|
|
|
/// CREATE VIRTUAL TABLE vtab USING csv(
|
|
|
|
/// filename=FILENAME -- Name of file containing CSV content
|
|
|
|
/// [, schema=SCHEMA] -- Alternative CSV schema. 'CREATE TABLE x(col1 TEXT NOT NULL, col2 INT, ...);'
|
|
|
|
/// [, header=YES|NO] -- First row of CSV defines the names of columns if "yes". Default "no".
|
|
|
|
/// [, columns=N] -- Assume the CSV file contains N columns.
|
|
|
|
/// [, delimiter=C] -- CSV delimiter. Default ','.
|
|
|
|
/// [, quote=C] -- CSV quote. Default '"'. 0 means no quote.
|
|
|
|
/// );
|
|
|
|
/// ```
|
2016-02-09 01:06:11 +08:00
|
|
|
pub fn load_module(conn: &Connection) -> Result<()> {
|
|
|
|
let aux: Option<()> = None;
|
2021-03-26 04:06:46 +08:00
|
|
|
conn.create_module("csv", read_only_module::<CsvTab>(), aux)
|
2018-07-15 00:47:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// An instance of the CSV virtual table
|
|
|
|
#[repr(C)]
|
2021-03-26 04:06:46 +08:00
|
|
|
struct CsvTab {
|
2018-07-15 00:47:52 +08:00
|
|
|
/// 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,
|
|
|
|
}
|
2016-02-09 01:06:11 +08:00
|
|
|
|
2021-03-26 04:06:46 +08:00
|
|
|
impl CsvTab {
|
2020-04-07 05:43:06 +08:00
|
|
|
fn reader(&self) -> Result<csv::Reader<File>, csv::Error> {
|
2018-07-15 00:47:52 +08:00
|
|
|
csv::ReaderBuilder::new()
|
|
|
|
.has_headers(self.has_headers)
|
|
|
|
.delimiter(self.delimiter)
|
|
|
|
.quote(self.quote)
|
|
|
|
.from_path(&self.filename)
|
|
|
|
}
|
2018-05-06 18:41:42 +08:00
|
|
|
|
|
|
|
fn parameter(c_slice: &[u8]) -> Result<(&str, &str)> {
|
2018-10-31 03:11:35 +08:00
|
|
|
let arg = str::from_utf8(c_slice)?.trim();
|
2018-05-06 18:41:42 +08:00
|
|
|
let mut split = arg.split('=');
|
|
|
|
if let Some(key) = split.next() {
|
|
|
|
if let Some(value) = split.next() {
|
|
|
|
let param = key.trim();
|
|
|
|
let value = dequote(value);
|
|
|
|
return Ok((param, value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(Error::ModuleError(format!("illegal argument: '{}'", arg)))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_byte(arg: &str) -> Option<u8> {
|
|
|
|
if arg.len() == 1 {
|
|
|
|
arg.bytes().next()
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2016-02-09 01:06:11 +08:00
|
|
|
}
|
|
|
|
|
2021-03-26 04:06:46 +08:00
|
|
|
unsafe impl<'vtab> VTab<'vtab> for CsvTab {
|
2018-05-07 00:05:02 +08:00
|
|
|
type Aux = ();
|
2021-03-26 04:06:46 +08:00
|
|
|
type Cursor = CsvTabCursor<'vtab>;
|
2018-06-21 02:01:38 +08:00
|
|
|
|
2018-06-22 23:20:47 +08:00
|
|
|
fn connect(
|
2022-03-18 02:58:02 +08:00
|
|
|
db: &mut VTabConnection,
|
2018-06-22 23:20:47 +08:00
|
|
|
_aux: Option<&()>,
|
|
|
|
args: &[&[u8]],
|
2021-03-26 04:06:46 +08:00
|
|
|
) -> Result<(String, CsvTab)> {
|
2016-02-11 03:30:08 +08:00
|
|
|
if args.len() < 4 {
|
2016-04-02 23:16:17 +08:00
|
|
|
return Err(Error::ModuleError("no CSV file specified".to_owned()));
|
2016-02-11 01:07:58 +08:00
|
|
|
}
|
2018-05-06 18:41:42 +08:00
|
|
|
|
2021-03-26 04:06:46 +08:00
|
|
|
let mut vtab = CsvTab {
|
2018-06-29 03:07:05 +08:00
|
|
|
base: ffi::sqlite3_vtab::default(),
|
2018-05-06 18:41:42 +08:00
|
|
|
filename: "".to_owned(),
|
2016-02-13 03:17:42 +08:00
|
|
|
has_headers: false,
|
|
|
|
delimiter: b',',
|
|
|
|
quote: b'"',
|
2018-05-13 18:21:58 +08:00
|
|
|
offset_first_row: csv::Position::new(),
|
2016-02-13 03:17:42 +08:00
|
|
|
};
|
2018-05-06 18:41:42 +08:00
|
|
|
let mut schema = None;
|
|
|
|
let mut n_col = None;
|
2016-02-11 03:30:08 +08:00
|
|
|
|
2018-05-06 18:41:42 +08:00
|
|
|
let args = &args[3..];
|
2016-08-13 17:54:19 +08:00
|
|
|
for c_slice in args {
|
2021-03-26 04:06:46 +08:00
|
|
|
let (param, value) = CsvTab::parameter(c_slice)?;
|
2018-05-06 18:41:42 +08:00
|
|
|
match param {
|
|
|
|
"filename" => {
|
|
|
|
if !Path::new(value).exists() {
|
2018-05-06 23:21:36 +08:00
|
|
|
return Err(Error::ModuleError(format!(
|
|
|
|
"file '{}' does not exist",
|
|
|
|
value
|
|
|
|
)));
|
2018-05-06 18:41:42 +08:00
|
|
|
}
|
|
|
|
vtab.filename = value.to_owned();
|
2018-05-06 23:21:36 +08:00
|
|
|
}
|
2018-05-06 18:41:42 +08:00
|
|
|
"schema" => {
|
|
|
|
schema = Some(value.to_owned());
|
2018-05-06 23:21:36 +08:00
|
|
|
}
|
2018-05-06 18:41:42 +08:00
|
|
|
"columns" => {
|
|
|
|
if let Ok(n) = value.parse::<u16>() {
|
|
|
|
if n_col.is_some() {
|
2018-05-06 23:21:36 +08:00
|
|
|
return Err(Error::ModuleError(
|
|
|
|
"more than one 'columns' parameter".to_owned(),
|
|
|
|
));
|
2018-05-06 18:41:42 +08:00
|
|
|
} else if n == 0 {
|
2018-05-06 23:21:36 +08:00
|
|
|
return Err(Error::ModuleError(
|
|
|
|
"must have at least one column".to_owned(),
|
|
|
|
));
|
2018-05-06 18:41:42 +08:00
|
|
|
}
|
|
|
|
n_col = Some(n);
|
|
|
|
} else {
|
2018-05-06 23:21:36 +08:00
|
|
|
return Err(Error::ModuleError(format!(
|
|
|
|
"unrecognized argument to 'columns': {}",
|
|
|
|
value
|
|
|
|
)));
|
2018-05-06 18:41:42 +08:00
|
|
|
}
|
2018-05-06 23:21:36 +08:00
|
|
|
}
|
2018-05-06 18:41:42 +08:00
|
|
|
"header" => {
|
|
|
|
if let Some(b) = parse_boolean(value) {
|
|
|
|
vtab.has_headers = b;
|
|
|
|
} else {
|
2018-05-06 23:21:36 +08:00
|
|
|
return Err(Error::ModuleError(format!(
|
|
|
|
"unrecognized argument to 'header': {}",
|
|
|
|
value
|
|
|
|
)));
|
2018-05-06 18:41:42 +08:00
|
|
|
}
|
2018-05-06 23:21:36 +08:00
|
|
|
}
|
2018-05-06 18:41:42 +08:00
|
|
|
"delimiter" => {
|
2021-03-26 04:06:46 +08:00
|
|
|
if let Some(b) = CsvTab::parse_byte(value) {
|
2018-05-06 18:41:42 +08:00
|
|
|
vtab.delimiter = b;
|
|
|
|
} else {
|
2018-05-06 23:21:36 +08:00
|
|
|
return Err(Error::ModuleError(format!(
|
|
|
|
"unrecognized argument to 'delimiter': {}",
|
|
|
|
value
|
|
|
|
)));
|
2018-05-06 18:41:42 +08:00
|
|
|
}
|
2018-05-06 23:21:36 +08:00
|
|
|
}
|
2018-05-06 18:41:42 +08:00
|
|
|
"quote" => {
|
2021-03-26 04:06:46 +08:00
|
|
|
if let Some(b) = CsvTab::parse_byte(value) {
|
2018-05-06 18:41:42 +08:00
|
|
|
if b == b'0' {
|
|
|
|
vtab.quote = 0;
|
|
|
|
} else {
|
|
|
|
vtab.quote = b;
|
|
|
|
}
|
|
|
|
} else {
|
2018-05-06 23:21:36 +08:00
|
|
|
return Err(Error::ModuleError(format!(
|
|
|
|
"unrecognized argument to 'quote': {}",
|
|
|
|
value
|
|
|
|
)));
|
2018-05-06 18:41:42 +08:00
|
|
|
}
|
2018-05-06 23:21:36 +08:00
|
|
|
}
|
2018-05-06 18:41:42 +08:00
|
|
|
_ => {
|
2018-05-06 23:21:36 +08:00
|
|
|
return Err(Error::ModuleError(format!(
|
|
|
|
"unrecognized parameter '{}'",
|
|
|
|
param
|
|
|
|
)));
|
|
|
|
}
|
2016-02-11 03:30:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-06 18:41:42 +08:00
|
|
|
if vtab.filename.is_empty() {
|
|
|
|
return Err(Error::ModuleError("no CSV file specified".to_owned()));
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut cols: Vec<String> = Vec::new();
|
|
|
|
if vtab.has_headers || (n_col.is_none() && schema.is_none()) {
|
2018-10-31 03:11:35 +08:00
|
|
|
let mut reader = vtab.reader()?;
|
2018-05-06 18:41:42 +08:00
|
|
|
if vtab.has_headers {
|
2018-05-13 18:21:58 +08:00
|
|
|
{
|
2018-10-31 03:11:35 +08:00
|
|
|
let headers = reader.headers()?;
|
2018-05-13 18:21:58 +08:00
|
|
|
// headers ignored if cols is not empty
|
|
|
|
if n_col.is_none() && schema.is_none() {
|
|
|
|
cols = headers
|
|
|
|
.into_iter()
|
2021-06-17 01:22:31 +08:00
|
|
|
.map(|header| escape_double_quote(header).into_owned())
|
2018-05-13 18:21:58 +08:00
|
|
|
.collect();
|
|
|
|
}
|
2018-05-06 18:41:42 +08:00
|
|
|
}
|
2018-05-13 18:21:58 +08:00
|
|
|
vtab.offset_first_row = reader.position().clone();
|
2018-05-06 18:41:42 +08:00
|
|
|
} else {
|
2018-05-13 18:21:58 +08:00
|
|
|
let mut record = csv::ByteRecord::new();
|
2018-10-31 03:11:35 +08:00
|
|
|
if reader.read_byte_record(&mut record)? {
|
2018-05-14 01:16:12 +08:00
|
|
|
for (i, _) in record.iter().enumerate() {
|
|
|
|
cols.push(format!("c{}", i));
|
2018-05-13 18:21:58 +08:00
|
|
|
}
|
2018-05-06 18:41:42 +08:00
|
|
|
}
|
2016-02-11 03:30:08 +08:00
|
|
|
}
|
2018-05-15 01:23:17 +08:00
|
|
|
} else if let Some(n_col) = n_col {
|
|
|
|
for i in 0..n_col {
|
|
|
|
cols.push(format!("c{}", i));
|
|
|
|
}
|
2016-02-11 03:30:08 +08:00
|
|
|
}
|
|
|
|
|
2018-05-06 18:41:42 +08:00
|
|
|
if cols.is_empty() && schema.is_none() {
|
|
|
|
return Err(Error::ModuleError("no column specified".to_owned()));
|
2016-02-11 03:30:08 +08:00
|
|
|
}
|
|
|
|
|
2018-05-06 18:41:42 +08:00
|
|
|
if schema.is_none() {
|
|
|
|
let mut sql = String::from("CREATE TABLE x(");
|
|
|
|
for (i, col) in cols.iter().enumerate() {
|
|
|
|
sql.push('"');
|
|
|
|
sql.push_str(col);
|
|
|
|
sql.push_str("\" TEXT");
|
|
|
|
if i == cols.len() - 1 {
|
|
|
|
sql.push_str(");");
|
|
|
|
} else {
|
|
|
|
sql.push_str(", ");
|
|
|
|
}
|
2016-02-11 03:30:08 +08:00
|
|
|
}
|
2018-05-06 18:41:42 +08:00
|
|
|
schema = Some(sql);
|
2016-02-11 03:30:08 +08:00
|
|
|
}
|
2022-03-18 02:58:02 +08:00
|
|
|
db.config(VTabConfig::DirectOnly)?;
|
2019-10-13 19:08:33 +08:00
|
|
|
Ok((schema.unwrap(), vtab))
|
2016-02-09 01:06:11 +08:00
|
|
|
}
|
|
|
|
|
2018-05-06 18:41:42 +08:00
|
|
|
// Only a forward full table scan is supported.
|
|
|
|
fn best_index(&self, info: &mut IndexInfo) -> Result<()> {
|
2018-05-14 01:16:12 +08:00
|
|
|
info.set_estimated_cost(1_000_000.);
|
2016-08-14 15:44:37 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
2016-02-09 01:06:11 +08:00
|
|
|
|
2021-03-26 04:06:46 +08:00
|
|
|
fn open(&self) -> Result<CsvTabCursor<'_>> {
|
|
|
|
Ok(CsvTabCursor::new(self.reader()?))
|
2016-02-09 01:06:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-26 04:06:46 +08:00
|
|
|
impl CreateVTab<'_> for CsvTab {}
|
2018-07-22 15:51:06 +08:00
|
|
|
|
2016-08-13 17:54:19 +08:00
|
|
|
/// A cursor for the CSV virtual table
|
2016-02-09 01:06:11 +08:00
|
|
|
#[repr(C)]
|
2021-03-26 04:06:46 +08:00
|
|
|
struct CsvTabCursor<'vtab> {
|
2016-08-13 17:54:19 +08:00
|
|
|
/// Base class. Must be first
|
2016-02-09 01:06:11 +08:00
|
|
|
base: ffi::sqlite3_vtab_cursor,
|
2016-08-13 17:54:19 +08:00
|
|
|
/// The CSV reader object
|
2016-02-13 03:17:42 +08:00
|
|
|
reader: csv::Reader<File>,
|
2018-05-06 18:41:42 +08:00
|
|
|
/// Current cursor position used as rowid
|
2016-02-09 01:06:11 +08:00
|
|
|
row_number: usize,
|
2018-05-06 18:41:42 +08:00
|
|
|
/// Values of the current row
|
2018-05-13 18:21:58 +08:00
|
|
|
cols: csv::StringRecord,
|
2016-02-13 03:17:42 +08:00
|
|
|
eof: bool,
|
2021-03-26 04:06:46 +08:00
|
|
|
phantom: PhantomData<&'vtab CsvTab>,
|
2016-02-09 01:06:11 +08:00
|
|
|
}
|
|
|
|
|
2021-03-26 04:06:46 +08:00
|
|
|
impl CsvTabCursor<'_> {
|
|
|
|
fn new<'vtab>(reader: csv::Reader<File>) -> CsvTabCursor<'vtab> {
|
|
|
|
CsvTabCursor {
|
2018-06-29 03:07:05 +08:00
|
|
|
base: ffi::sqlite3_vtab_cursor::default(),
|
2018-05-06 14:45:56 +08:00
|
|
|
reader,
|
2016-02-09 01:06:11 +08:00
|
|
|
row_number: 0,
|
2018-05-13 18:21:58 +08:00
|
|
|
cols: csv::StringRecord::new(),
|
2016-02-13 03:17:42 +08:00
|
|
|
eof: false,
|
2020-06-01 15:48:49 +08:00
|
|
|
phantom: PhantomData,
|
2016-02-09 01:06:11 +08:00
|
|
|
}
|
|
|
|
}
|
2018-05-06 23:19:19 +08:00
|
|
|
|
2018-07-15 01:10:28 +08:00
|
|
|
/// Accessor to the associated virtual table.
|
2021-03-26 04:06:46 +08:00
|
|
|
fn vtab(&self) -> &CsvTab {
|
|
|
|
unsafe { &*(self.base.pVtab as *const CsvTab) }
|
2016-02-09 01:06:11 +08:00
|
|
|
}
|
2018-07-15 01:10:28 +08:00
|
|
|
}
|
|
|
|
|
2021-03-26 04:06:46 +08:00
|
|
|
unsafe impl VTabCursor for CsvTabCursor<'_> {
|
2018-05-06 18:41:42 +08:00
|
|
|
// Only a full table scan is supported. So `filter` simply rewinds to
|
|
|
|
// the beginning.
|
2018-12-08 04:57:04 +08:00
|
|
|
fn filter(
|
|
|
|
&mut self,
|
|
|
|
_idx_num: c_int,
|
|
|
|
_idx_str: Option<&str>,
|
|
|
|
_args: &Values<'_>,
|
|
|
|
) -> Result<()> {
|
2016-02-09 01:06:11 +08:00
|
|
|
{
|
2018-05-13 18:21:58 +08:00
|
|
|
let offset_first_row = self.vtab().offset_first_row.clone();
|
2018-10-31 03:11:35 +08:00
|
|
|
self.reader.seek(offset_first_row)?;
|
2016-02-09 01:06:11 +08:00
|
|
|
}
|
|
|
|
self.row_number = 0;
|
|
|
|
self.next()
|
|
|
|
}
|
2018-08-17 00:29:46 +08:00
|
|
|
|
2016-02-09 01:06:11 +08:00
|
|
|
fn next(&mut self) -> Result<()> {
|
2016-02-11 03:48:30 +08:00
|
|
|
{
|
2018-05-13 18:21:58 +08:00
|
|
|
self.eof = self.reader.is_done();
|
2016-02-13 03:17:42 +08:00
|
|
|
if self.eof {
|
2016-02-12 02:16:05 +08:00
|
|
|
return Ok(());
|
2016-02-11 03:48:30 +08:00
|
|
|
}
|
|
|
|
|
2018-10-31 03:11:35 +08:00
|
|
|
self.eof = !self.reader.read_record(&mut self.cols)?;
|
2016-02-09 01:06:11 +08:00
|
|
|
}
|
2016-02-11 03:48:30 +08:00
|
|
|
|
2016-08-13 19:55:30 +08:00
|
|
|
self.row_number += 1;
|
2016-02-09 01:06:11 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
2018-08-17 00:29:46 +08:00
|
|
|
|
2016-02-09 01:06:11 +08:00
|
|
|
fn eof(&self) -> bool {
|
2016-02-13 03:17:42 +08:00
|
|
|
self.eof
|
2016-02-09 01:06:11 +08:00
|
|
|
}
|
2018-08-17 00:29:46 +08:00
|
|
|
|
2017-03-09 03:35:07 +08:00
|
|
|
fn column(&self, ctx: &mut Context, col: c_int) -> Result<()> {
|
2016-02-13 03:17:42 +08:00
|
|
|
if col < 0 || col as usize >= self.cols.len() {
|
2018-05-06 23:21:36 +08:00
|
|
|
return Err(Error::ModuleError(format!(
|
|
|
|
"column index out of bounds: {}",
|
|
|
|
col
|
|
|
|
)));
|
2016-02-11 01:07:58 +08:00
|
|
|
}
|
2016-02-13 03:17:42 +08:00
|
|
|
if self.cols.is_empty() {
|
2018-06-10 18:16:54 +08:00
|
|
|
return ctx.set_result(&Null);
|
2016-02-11 01:07:58 +08:00
|
|
|
}
|
|
|
|
// TODO Affinity
|
2018-06-10 18:16:54 +08:00
|
|
|
ctx.set_result(&self.cols[col as usize].to_owned())
|
2016-02-09 01:06:11 +08:00
|
|
|
}
|
2018-08-17 00:29:46 +08:00
|
|
|
|
2016-02-09 01:06:11 +08:00
|
|
|
fn rowid(&self) -> Result<i64> {
|
|
|
|
Ok(self.row_number as i64)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<csv::Error> for Error {
|
2020-11-04 11:10:23 +08:00
|
|
|
#[cold]
|
2016-02-09 01:06:11 +08:00
|
|
|
fn from(err: csv::Error) -> Error {
|
2020-01-26 23:57:58 +08:00
|
|
|
Error::ModuleError(err.to_string())
|
2016-02-09 01:06:11 +08:00
|
|
|
}
|
|
|
|
}
|
2016-02-12 02:16:05 +08:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2018-10-31 03:11:35 +08:00
|
|
|
use crate::vtab::csvtab;
|
2020-11-03 17:32:46 +08:00
|
|
|
use crate::{Connection, Result};
|
2019-03-20 03:45:04 +08:00
|
|
|
use fallible_iterator::FallibleIterator;
|
2016-02-12 02:16:05 +08:00
|
|
|
|
|
|
|
#[test]
|
2020-11-06 05:14:00 +08:00
|
|
|
fn test_csv_module() -> Result<()> {
|
|
|
|
let db = Connection::open_in_memory()?;
|
|
|
|
csvtab::load_module(&db)?;
|
|
|
|
db.execute_batch("CREATE VIRTUAL TABLE vtab USING csv(filename='test.csv', header=yes)")?;
|
2016-02-12 02:16:05 +08:00
|
|
|
|
|
|
|
{
|
2020-11-06 05:14:00 +08:00
|
|
|
let mut s = db.prepare("SELECT rowid, * FROM vtab")?;
|
2016-02-13 03:17:42 +08:00
|
|
|
{
|
|
|
|
let headers = s.column_names();
|
|
|
|
assert_eq!(vec!["rowid", "colA", "colB", "colC"], headers);
|
|
|
|
}
|
2016-02-12 02:16:05 +08:00
|
|
|
|
2020-11-06 05:14:00 +08:00
|
|
|
let ids: Result<Vec<i32>> = s.query([])?.map(|row| row.get::<_, i32>(0)).collect();
|
|
|
|
let sum = ids?.iter().sum::<i32>();
|
2016-02-13 03:17:42 +08:00
|
|
|
assert_eq!(sum, 15);
|
2016-02-12 02:16:05 +08:00
|
|
|
}
|
2020-11-06 05:14:00 +08:00
|
|
|
db.execute_batch("DROP TABLE vtab")
|
2016-02-13 03:17:42 +08:00
|
|
|
}
|
2016-02-12 02:16:05 +08:00
|
|
|
|
2016-02-13 03:17:42 +08:00
|
|
|
#[test]
|
2020-11-06 05:14:00 +08:00
|
|
|
fn test_csv_cursor() -> Result<()> {
|
|
|
|
let db = Connection::open_in_memory()?;
|
|
|
|
csvtab::load_module(&db)?;
|
|
|
|
db.execute_batch("CREATE VIRTUAL TABLE vtab USING csv(filename='test.csv', header=yes)")?;
|
2016-02-13 03:17:42 +08:00
|
|
|
|
|
|
|
{
|
2020-11-06 05:14:00 +08:00
|
|
|
let mut s = db.prepare(
|
|
|
|
"SELECT v1.rowid, v1.* FROM vtab v1 NATURAL JOIN vtab v2 WHERE \
|
2018-06-10 18:16:54 +08:00
|
|
|
v1.rowid < v2.rowid",
|
2020-11-06 05:14:00 +08:00
|
|
|
)?;
|
2016-02-13 03:17:42 +08:00
|
|
|
|
2020-11-06 05:14:00 +08:00
|
|
|
let mut rows = s.query([])?;
|
|
|
|
let row = rows.next()?.unwrap();
|
2019-02-22 03:48:09 +08:00
|
|
|
assert_eq!(row.get_unwrap::<_, i32>(0), 2);
|
2016-02-13 03:17:42 +08:00
|
|
|
}
|
2020-11-06 05:14:00 +08:00
|
|
|
db.execute_batch("DROP TABLE vtab")
|
2016-02-12 02:16:05 +08:00
|
|
|
}
|
|
|
|
}
|