-
Notifications
You must be signed in to change notification settings - Fork 2
/
sql.rs
346 lines (308 loc) · 10 KB
/
sql.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
use crate::{prelude::ForensicResult, err::ForensicError};
use super::vfs::VirtualFile;
pub trait SqlDb {
fn list_tables(&self) -> ForensicResult<Vec<String>>;
fn prepare<'a>(&'a self, statement : &'a str) -> ForensicResult<Box<dyn SqlStatement + 'a>>;
/// Mounts a SQL reader from a sqlite file
fn from_file(&self, file: Box<dyn VirtualFile>) -> ForensicResult<Box<dyn SqlDb>>;
}
/// It allows decoupling the SQL database access library from the analysis library.
pub trait SqlStatement {
/// Return the number of columns.
fn column_count(&self) -> usize;
/// Return the name of a column. The first column has index 0.
fn column_name(&self, i: usize) -> Option<&str>;
/// Return column names.
fn column_names(&self) -> Vec<&str>;
/// Return the type of a column. The first column has index 0.
fn column_type(&self, i: usize) -> ColumnType;
/// Advance to the next state until there no more data is available (return=Ok(false)).
fn next(&mut self) -> ForensicResult<bool>;
/// Read a value from a column. The first column has index 0.
fn read(&self, i: usize) -> ForensicResult<ColumnValue>;
}
pub trait SqlValueInto: Sized {
fn into(value: &ColumnValue) -> ForensicResult<Self>;
fn into_owned(value: ColumnValue) -> ForensicResult<Self>;
}
pub enum ColumnType {
Binary,
Float,
Integer,
String,
Null,
}
pub enum ColumnValue {
Binary(Vec<u8>),
Float(f64),
Integer(i64),
String(String),
Null,
}
impl TryInto<String> for ColumnValue {
type Error = ForensicError;
fn try_into(self) -> Result<String, Self::Error> {
Ok(match self {
ColumnValue::String(v) => v.clone(),
ColumnValue::Binary(v) => format!("{:?}",v),
ColumnValue::Float(v) => format!("{:?}",v),
ColumnValue::Integer(v) => format!("{:?}",v),
ColumnValue::Null => String::new()
})
}
}
impl TryInto<i64> for ColumnValue {
type Error = ForensicError;
fn try_into(self) -> Result<i64, Self::Error> {
match self {
ColumnValue::Integer(v) => Ok(v),
_ => Err(ForensicError::CastError)
}
}
}
impl TryInto<usize> for ColumnValue {
type Error = ForensicError;
fn try_into(self) -> Result<usize, Self::Error> {
match self {
ColumnValue::Integer(v) => Ok(v as usize),
_ => Err(ForensicError::CastError)
}
}
}
impl TryInto<f64> for ColumnValue {
type Error = ForensicError;
fn try_into(self) -> Result<f64, Self::Error> {
match self {
ColumnValue::Float(v) => Ok(v),
_ => Err(ForensicError::CastError)
}
}
}
impl TryInto<Vec<u8>> for ColumnValue {
type Error = ForensicError;
fn try_into(self) -> Result<Vec<u8>, Self::Error> {
match self {
ColumnValue::Binary(v) => Ok(v),
_ => Err(ForensicError::CastError)
}
}
}
impl Into<ColumnValue> for f32 {
fn into(self) -> ColumnValue {
ColumnValue::Float(self as f64)
}
}
impl Into<ColumnValue> for f64 {
fn into(self) -> ColumnValue {
ColumnValue::Float(self)
}
}
impl Into<ColumnValue> for u64 {
fn into(self) -> ColumnValue {
ColumnValue::Integer(self as i64)
}
}
impl Into<ColumnValue> for i64 {
fn into(self) -> ColumnValue {
ColumnValue::Integer(self as i64)
}
}
impl Into<ColumnValue> for u32 {
fn into(self) -> ColumnValue {
ColumnValue::Integer(self as i64)
}
}
impl Into<ColumnValue> for i32 {
fn into(self) -> ColumnValue {
ColumnValue::Integer(self as i64)
}
}
impl Into<ColumnValue> for usize {
fn into(self) -> ColumnValue {
ColumnValue::Integer(self as i64)
}
}
impl Into<ColumnValue> for Vec<u8> {
fn into(self) -> ColumnValue {
ColumnValue::Binary(self)
}
}
impl Into<ColumnValue> for &Vec<u8> {
fn into(self) -> ColumnValue {
ColumnValue::Binary(self.clone())
}
}
impl Into<ColumnValue> for () {
fn into(self) -> ColumnValue {
ColumnValue::Null
}
}
impl Into<ColumnValue> for &[u8] {
fn into(self) -> ColumnValue {
let mut vc = Vec::with_capacity(self.len());
for v in self {
vc.push(*v);
}
ColumnValue::Binary(vc)
}
}
impl Into<ColumnValue> for &str {
fn into(self) -> ColumnValue {
ColumnValue::String(self.to_string())
}
}
impl Into<ColumnValue> for &String {
fn into(self) -> ColumnValue {
ColumnValue::String(self.to_string())
}
}
impl Into<ColumnValue> for String {
fn into(self) -> ColumnValue {
ColumnValue::String(self)
}
}
#[cfg(test)]
mod sql_tests {
extern crate sqlite;
use crate::prelude::{ForensicResult, ForensicError};
use self::sqlite::{Connection, Statement};
use super::{SqlStatement, ColumnValue, SqlDb};
struct SqliteWDB {
conn : Connection
}
impl SqliteWDB{
pub fn new(conn : Connection) -> SqliteWDB {
SqliteWDB {
conn
}
}
}
impl SqlDb for SqliteWDB {
fn prepare<'a>(&'a self, statement : &'a str) -> ForensicResult<Box<dyn SqlStatement +'a>> {
Ok(Box::new(SqliteStatement::new( &self.conn, statement)?))
}
fn from_file(&self, _file: Box<dyn crate::traits::vfs::VirtualFile>) -> ForensicResult<Box<dyn SqlDb>> {
match sqlite::open(":memory:") {
Ok(v) => Ok(Box::new(Self::new(v))),
Err(e) => Err(ForensicError::Other(e.to_string()))
}
}
fn list_tables(&self) -> ForensicResult<Vec<String>> {
let mut ret = Vec::with_capacity(32);
let mut sts = self.prepare(r#"SELECT
name
FROM
sqlite_schema
WHERE
type ='table' AND
name NOT LIKE 'sqlite_%';"#)?;
loop {
if !sts.next()? {
break;
}
let name : String = sts.read(0)?.try_into()?;
ret.push(name);
}
Ok(ret)
}
}
pub struct SqliteStatement<'conn> {
stmt: Statement<'conn>
}
impl<'conn> SqliteStatement<'conn>{
pub fn new(conn : &'conn Connection, statement : &str) -> ForensicResult<SqliteStatement<'conn>>{
Ok(Self { stmt : match conn.prepare(statement) {
Ok(st) => st,
Err(e) => return Err(ForensicError::Other(e.to_string()))
}
})
}
}
impl<'conn> SqlStatement for SqliteStatement<'conn>{
fn column_count(&self) -> usize {
self.stmt.column_count()
}
fn column_name(&self, i: usize) -> Option<&str> {
Some(self.stmt.column_name(i))
}
fn column_names(&self) -> Vec<&str> {
self.stmt.column_names()
}
fn column_type(&self, i: usize) -> super::ColumnType {
match self.stmt.column_type(i) {
sqlite::Type::Binary => super::ColumnType::Binary,
sqlite::Type::Float => super::ColumnType::Float,
sqlite::Type::Integer => super::ColumnType::Integer,
sqlite::Type::String => super::ColumnType::String,
sqlite::Type::Null => super::ColumnType::Null,
}
}
fn next(&mut self) -> ForensicResult<bool> {
match self.stmt.next() {
Ok(v) => Ok(match v {
sqlite::State::Row => true,
sqlite::State::Done => false,
}),
Err(e) => Err(ForensicError::Other(e.to_string())),
}
}
fn read(&self, i: usize) -> ForensicResult<ColumnValue> {
match self.stmt.column_type(i) {
sqlite::Type::Binary => match self.stmt.read(i) {
Ok(v) => Ok(ColumnValue::Binary(v)),
Err(e) => Err(ForensicError::Other(e.to_string())),
},
sqlite::Type::Float => match self.stmt.read(i) {
Ok(v) => Ok(ColumnValue::Float(v)),
Err(e) => Err(ForensicError::Other(e.to_string())),
},
sqlite::Type::Integer => match self.stmt.read(i) {
Ok(v) => Ok(ColumnValue::Integer(v)),
Err(e) => Err(ForensicError::Other(e.to_string())),
},
sqlite::Type::String => match self.stmt.read(i) {
Ok(v) => Ok(ColumnValue::String(v)),
Err(e) => Err(ForensicError::Other(e.to_string())),
},
sqlite::Type::Null => Ok(ColumnValue::Null),
}
}
}
fn prepare_db() -> Connection {
let connection = sqlite::open(":memory:").unwrap();
connection
.execute(
"
CREATE TABLE users (name TEXT, age INTEGER);
INSERT INTO users VALUES ('Alice', 42);
INSERT INTO users VALUES ('Bob', 69);
",
)
.unwrap();
connection
}
fn prepare_wrapper(connection :Connection) -> SqliteWDB{
SqliteWDB::new(connection)
}
#[test]
fn test_sqlite_wrapper() {
let conn = prepare_db();
let w_conn = prepare_wrapper(conn);
let mut statement = w_conn.prepare("SELECT name, age FROM users;").unwrap();
test_database_content(statement.as_mut()).expect("Should not return error");
}
fn test_database_content<'a>(statement : &mut dyn SqlStatement) -> ForensicResult<()> {
assert!(statement.next()?);
let name : String = statement.read(0)?.try_into()?;
let age : usize = statement.read(1)?.try_into()?;
assert_eq!("Alice", name);
assert_eq!(42, age);
assert!(statement.next()?);
let name : String = statement.read(0)?.try_into()?;
let age : usize = statement.read(1)?.try_into()?;
assert_eq!("Bob", name);
assert_eq!(69, age);
assert!(!statement.next()?);
Ok(())
}
}