2018-07-14 17:01:19 +08:00
|
|
|
//! Ensure Virtual tables can be declared outside `rusqlite` crate.
|
|
|
|
|
|
|
|
#[cfg(feature = "vtab")]
|
|
|
|
#[test]
|
2020-11-06 05:14:00 +08:00
|
|
|
fn test_dummy_module() -> rusqlite::Result<()> {
|
2018-09-16 15:49:23 +08:00
|
|
|
use rusqlite::types::ToSql;
|
2018-07-15 01:21:03 +08:00
|
|
|
use rusqlite::vtab::{
|
2018-07-17 02:17:53 +08:00
|
|
|
eponymous_only_module, sqlite3_vtab, sqlite3_vtab_cursor, Context, IndexInfo, VTab,
|
|
|
|
VTabConnection, VTabCursor, Values,
|
2018-07-15 01:21:03 +08:00
|
|
|
};
|
2018-07-15 22:15:55 +08:00
|
|
|
use rusqlite::{version_number, Connection, Result};
|
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;
|
|
|
|
|
2020-04-14 22:09:50 +08:00
|
|
|
let module = eponymous_only_module::<DummyTab>();
|
2018-07-14 17:01:19 +08:00
|
|
|
|
|
|
|
#[repr(C)]
|
2018-07-15 00:47:52 +08:00
|
|
|
struct DummyTab {
|
|
|
|
/// Base class. Must be first
|
2018-07-15 22:15:55 +08:00
|
|
|
base: sqlite3_vtab,
|
2018-07-15 00:47:52 +08:00
|
|
|
}
|
2018-07-14 17:01:19 +08:00
|
|
|
|
2020-06-01 15:48:49 +08:00
|
|
|
unsafe impl<'vtab> VTab<'vtab> for DummyTab {
|
2018-07-14 17:01:19 +08:00
|
|
|
type Aux = ();
|
2020-06-01 15:48:49 +08:00
|
|
|
type Cursor = DummyTabCursor<'vtab>;
|
2018-07-14 17:01:19 +08:00
|
|
|
|
|
|
|
fn connect(
|
|
|
|
_: &mut VTabConnection,
|
|
|
|
_aux: Option<&()>,
|
|
|
|
_args: &[&[u8]],
|
|
|
|
) -> Result<(String, DummyTab)> {
|
|
|
|
let vtab = DummyTab {
|
2018-07-15 22:15:55 +08:00
|
|
|
base: sqlite3_vtab::default(),
|
2018-07-14 17:01:19 +08:00
|
|
|
};
|
|
|
|
Ok(("CREATE TABLE x(value)".to_owned(), vtab))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn best_index(&self, info: &mut IndexInfo) -> Result<()> {
|
|
|
|
info.set_estimated_cost(1.);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-06-01 15:48:49 +08:00
|
|
|
fn open(&'vtab self) -> Result<DummyTabCursor<'vtab>> {
|
2018-07-14 17:01:19 +08:00
|
|
|
Ok(DummyTabCursor::default())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
#[repr(C)]
|
2020-06-01 15:48:49 +08:00
|
|
|
struct DummyTabCursor<'vtab> {
|
2018-07-14 17:01:19 +08:00
|
|
|
/// Base class. Must be first
|
2018-07-15 22:15:55 +08:00
|
|
|
base: sqlite3_vtab_cursor,
|
2018-07-14 17:01:19 +08:00
|
|
|
/// The rowid
|
|
|
|
row_id: i64,
|
2020-06-01 15:48:49 +08:00
|
|
|
phantom: PhantomData<&'vtab DummyTab>,
|
2018-07-14 17:01:19 +08:00
|
|
|
}
|
|
|
|
|
2020-06-01 15:48:49 +08:00
|
|
|
unsafe impl VTabCursor for DummyTabCursor<'_> {
|
2018-07-14 17:01:19 +08:00
|
|
|
fn filter(
|
|
|
|
&mut self,
|
|
|
|
_idx_num: c_int,
|
|
|
|
_idx_str: Option<&str>,
|
2018-12-08 04:57:04 +08:00
|
|
|
_args: &Values<'_>,
|
2018-07-14 17:01:19 +08:00
|
|
|
) -> Result<()> {
|
|
|
|
self.row_id = 1;
|
|
|
|
Ok(())
|
|
|
|
}
|
2018-08-17 00:29:46 +08:00
|
|
|
|
2018-07-14 17:01:19 +08:00
|
|
|
fn next(&mut self) -> Result<()> {
|
|
|
|
self.row_id += 1;
|
|
|
|
Ok(())
|
|
|
|
}
|
2018-08-17 00:29:46 +08:00
|
|
|
|
2018-07-14 17:01:19 +08:00
|
|
|
fn eof(&self) -> bool {
|
|
|
|
self.row_id > 1
|
|
|
|
}
|
2018-08-17 00:29:46 +08:00
|
|
|
|
2018-07-14 17:01:19 +08:00
|
|
|
fn column(&self, ctx: &mut Context, _: c_int) -> Result<()> {
|
|
|
|
ctx.set_result(&self.row_id)
|
|
|
|
}
|
2018-08-17 00:29:46 +08:00
|
|
|
|
2018-07-14 17:01:19 +08:00
|
|
|
fn rowid(&self) -> Result<i64> {
|
|
|
|
Ok(self.row_id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-06 05:14:00 +08:00
|
|
|
let db = Connection::open_in_memory()?;
|
2018-07-14 17:01:19 +08:00
|
|
|
|
2020-11-06 05:14:00 +08:00
|
|
|
db.create_module::<DummyTab>("dummy", &module, None)?;
|
2018-07-14 17:01:19 +08:00
|
|
|
|
2018-07-15 22:15:55 +08:00
|
|
|
let version = version_number();
|
2019-02-02 18:37:26 +08:00
|
|
|
if version < 3_008_012 {
|
2020-11-06 05:14:00 +08:00
|
|
|
return Ok(());
|
2018-07-14 17:28:43 +08:00
|
|
|
}
|
|
|
|
|
2020-11-06 05:14:00 +08:00
|
|
|
let mut s = db.prepare("SELECT * FROM dummy()")?;
|
2018-07-14 17:01:19 +08:00
|
|
|
|
2020-11-06 05:14:00 +08:00
|
|
|
let dummy = s.query_row(&[] as &[&dyn ToSql], |row| row.get::<_, i32>(0))?;
|
2018-07-14 17:01:19 +08:00
|
|
|
assert_eq!(1, dummy);
|
2020-11-06 05:14:00 +08:00
|
|
|
Ok(())
|
2018-07-14 17:01:19 +08:00
|
|
|
}
|