diff --git a/src/vtab/csvtab.rs b/src/vtab/csvtab.rs index 812f996..5361491 100644 --- a/src/vtab/csvtab.rs +++ b/src/vtab/csvtab.rs @@ -6,6 +6,7 @@ use libc; use {Connection, Error, Result}; use ffi; +use types::Null; use vtab::declare_vtab; use self::csv::Reader; @@ -27,19 +28,25 @@ struct CSVTab { base: ffi::sqlite3_vtab, reader: csv::Reader, offset_first_row: u64, + cols: Vec, } impl CSVTab { fn create(db: *mut ffi::sqlite3, aux: *mut libc::c_void, - _argc: libc::c_int, + argc: libc::c_int, _argv: *const *const libc::c_char) -> Result { + if argc < 4 { + return Err(Error::ModuleError(format!("no CSV file specified"))); + } + //let filename = ; let reader = try!(csv::Reader::from_file("FIXME")); let vtab = CSVTab { base: Default::default(), reader: reader, offset_first_row: 0, + cols: vec![], // FIXME }; unimplemented!(); try!(declare_vtab(db, "CREATE TABLE x FIXME")); @@ -77,7 +84,7 @@ impl CSVTabCursor { fn filter(&mut self) -> Result<()> { { let vtab = self.vtab(); - vtab.reader.seek(vtab.offset_first_row); // FIXME Result ignore + try!(vtab.reader.seek(vtab.offset_first_row)); } self.row_number = 0; self.next() @@ -85,7 +92,7 @@ impl CSVTabCursor { fn next(&mut self) -> Result<()> { let vtab = self.vtab(); if vtab.reader.done() { - return Err(Error::SqliteFailure(ffi::Error::new(ffi::SQLITE_ERROR), None)); + return Err(Error::ModuleError(format!("eof"))); } unimplemented!(); // self.row_number = self.row_number + 1; @@ -95,10 +102,18 @@ impl CSVTabCursor { let vtab = self.vtab(); unsafe { (*vtab).reader.done() } } - fn column(&self, ctx: *mut ffi::sqlite3_context, _i: libc::c_int) -> Result<()> { + fn column(&self, ctx: *mut ffi::sqlite3_context, col: libc::c_int) -> Result<()> { + use functions::ToResult; let vtab = self.vtab(); - unimplemented!(); - // TODO.set_result(ctx); + if col < 0 || col as usize >= vtab.cols.len() { + return Err(Error::ModuleError(format!("column index out of bounds: {}", col))); + } + if vtab.cols.is_empty() { + unsafe { Null.set_result(ctx) }; + return Ok(()); + } + // TODO Affinity + unsafe { vtab.cols[col as usize].set_result(ctx) }; Ok(()) } fn rowid(&self) -> Result { diff --git a/src/vtab/mod.rs b/src/vtab/mod.rs index b069fe8..6b33959 100644 --- a/src/vtab/mod.rs +++ b/src/vtab/mod.rs @@ -172,6 +172,7 @@ unsafe extern "C" fn $destroy(vtab: *mut ffi::sqlite3_vtab) -> libc::c_int { unsafe extern "C" fn $open(vtab: *mut ffi::sqlite3_vtab, pp_cursor: *mut *mut ffi::sqlite3_vtab_cursor) -> libc::c_int { + use std::error::Error as StdError; use vtab::set_err_msg; let vt = vtab as *mut $vtab; match (*vt).open() { @@ -186,7 +187,8 @@ unsafe extern "C" fn $open(vtab: *mut ffi::sqlite3_vtab, } err.extended_code } - Err(_) => { + Err(err) => { + set_err_msg(vtab, err.description()); ffi::SQLITE_ERROR } } @@ -243,6 +245,7 @@ unsafe extern "C" fn $rowid(cursor: *mut ffi::sqlite3_vtab_cursor, pub unsafe fn cursor_error(cursor: *mut ffi::sqlite3_vtab_cursor, result: Result) -> libc::c_int { + use std::error::Error as StdError; match result { Ok(_) => ffi::SQLITE_OK, Err(Error::SqliteFailure(err, s)) => { @@ -251,8 +254,8 @@ pub unsafe fn cursor_error(cursor: *mut ffi::sqlite3_vtab_cursor, } err.extended_code } - Err(_) => { - // TODO errMsg + Err(err) => { + set_err_msg((*cursor).pVtab, err.description()); ffi::SQLITE_ERROR } }