forked from asg017/sqlite-loadable-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharacters.rs
157 lines (141 loc) · 4.06 KB
/
characters.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//! cargo build --example series
//! sqlite3 :memory: '.read examples/test.sql'
use sqlite_loadable::prelude::*;
use sqlite_loadable::{
api, define_table_function,
table::{BestIndexError, ConstraintOperator, IndexInfo, VTab, VTabArguments, VTabCursor},
Result,
};
use std::mem;
static CREATE_SQL: &str = "CREATE TABLE x(value, input hidden)";
enum Columns {
Value,
Input,
}
fn column(index: i32) -> Option<Columns> {
match index {
0 => Some(Columns::Value),
1 => Some(Columns::Input),
_ => None,
}
}
#[repr(C)]
pub struct CharactersTable {
/// must be first
base: sqlite3_vtab,
}
impl<'vtab> VTab<'vtab> for CharactersTable {
type Aux = ();
type Cursor = CharactersCursor;
fn connect(
_db: *mut sqlite3,
_aux: Option<&Self::Aux>,
_args: VTabArguments,
) -> Result<(String, CharactersTable)> {
let vtab = CharactersTable { base: unsafe { mem::zeroed() } };
// TODO db.config(VTabConfig::Innocuous)?;
Ok((CREATE_SQL.to_owned(), vtab))
}
fn destroy(&self) -> Result<()> {
Ok(())
}
fn best_index(&self, mut info: IndexInfo) -> core::result::Result<(), BestIndexError> {
let mut has_input = false;
for mut constraint in info.constraints() {
match column(constraint.column_idx()) {
Some(Columns::Input) => {
if constraint.usable() && constraint.op() == Some(ConstraintOperator::EQ) {
constraint.set_omit(true);
constraint.set_argv_index(1);
has_input = true;
} else {
return Err(BestIndexError::Constraint);
}
}
_ => todo!(),
}
}
if !has_input {
return Err(BestIndexError::Error);
}
info.set_estimated_cost(100000.0);
info.set_estimated_rows(100000);
info.set_idxnum(1);
Ok(())
}
fn open(&mut self) -> Result<CharactersCursor> {
Ok(CharactersCursor::new())
}
}
#[repr(C)]
pub struct CharactersCursor {
/// Base class. Must be first
base: sqlite3_vtab_cursor,
input: Option<String>,
characters: Option<Vec<char>>,
idx: usize,
}
impl CharactersCursor {
fn new() -> CharactersCursor {
CharactersCursor {
base: unsafe { mem::zeroed() },
input: None,
characters: None,
idx: 0,
}
}
}
impl VTabCursor for CharactersCursor {
fn filter(
&mut self,
_idx_num: i32,
_idx_str: Option<&str>,
values: &[*mut sqlite3_value],
) -> Result<()> {
let input =
api::value_text(values.get(0).expect("1st input constraint is required"))?.to_owned();
self.characters = Some(input.chars().collect());
self.input = Some(input);
self.idx = 0;
Ok(())
}
fn next(&mut self) -> Result<()> {
self.idx += 1;
Ok(())
}
fn eof(&self) -> bool {
match &self.characters {
Some(chars) => chars.get(self.idx).is_none(),
None => true,
}
}
fn column(&self, context: *mut sqlite3_context, i: i32) -> Result<()> {
match column(i) {
Some(Columns::Value) => {
api::result_text(
context,
self.characters
.as_ref()
.unwrap()
.get(self.idx)
.unwrap()
.to_string()
.as_str(),
)?;
}
Some(Columns::Input) => {
api::result_text(context, self.input.as_ref().unwrap())?;
}
_ => (),
}
Ok(())
}
fn rowid(&self) -> Result<i64> {
Ok(self.idx as i64)
}
}
#[sqlite_entrypoint]
pub fn sqlite3_characters_init(db: *mut sqlite3) -> Result<()> {
define_table_function::<CharactersTable>(db, "characters", None)?;
Ok(())
}