Repository navigation
Expand file tree
/
Copy pathvalue.rs
More file actions
97 lines (76 loc) · 2.78 KB
/
Copy pathvalue.rs
File metadata and controls
97 lines (76 loc) · 2.78 KB
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
use std::ffi::CStr;
use libsqlite3_sys::{
sqlite3_column_blob, sqlite3_column_bytes, sqlite3_column_double, sqlite3_column_int,
sqlite3_column_int64, sqlite3_column_text, sqlite3_column_type, SQLITE_BLOB, SQLITE_FLOAT,
SQLITE_INTEGER, SQLITE_NULL, SQLITE_TEXT,
};
use crate::sqlite::statement::SqliteStatement;
use crate::sqlite::types::SqliteType;
use crate::sqlite::SqliteConnection;
use core::slice;
pub struct SqliteResultValue<'c> {
pub(super) index: usize,
pub(super) statement: Option<usize>,
pub(super) connection: &'c SqliteConnection,
}
impl SqliteResultValue<'_> {
fn statement(&self) -> &SqliteStatement {
self.connection.statement(self.statement)
}
}
// https://www.sqlite.org/c3ref/column_blob.html
// https://www.sqlite.org/capi3ref.html#sqlite3_column_blob
// These routines return information about a single column of the current result row of a query.
impl<'c> SqliteResultValue<'c> {
pub(crate) fn r#type(&self) -> SqliteType {
#[allow(unsafe_code)]
let type_code =
unsafe { sqlite3_column_type(self.statement().handle(), self.index as i32) };
match type_code {
SQLITE_INTEGER => SqliteType::Integer,
SQLITE_FLOAT => SqliteType::Float,
SQLITE_BLOB => SqliteType::Blob,
SQLITE_NULL => SqliteType::Null,
SQLITE_TEXT => SqliteType::Text,
_ => unreachable!(),
}
}
pub(crate) fn int(&self) -> i32 {
#[allow(unsafe_code)]
unsafe {
sqlite3_column_int(self.statement().handle(), self.index as i32)
}
}
pub(crate) fn int64(&self) -> i64 {
#[allow(unsafe_code)]
unsafe {
sqlite3_column_int64(self.statement().handle(), self.index as i32)
}
}
pub(crate) fn double(&self) -> f64 {
#[allow(unsafe_code)]
unsafe {
sqlite3_column_double(self.statement().handle(), self.index as i32)
}
}
pub(crate) fn text(&self) -> crate::Result<&'c str> {
#[allow(unsafe_code)]
let raw = unsafe {
let ptr =
sqlite3_column_text(self.statement().handle(), self.index as i32) as *const i8;
debug_assert!(!ptr.is_null());
CStr::from_ptr(ptr)
};
raw.to_str().map_err(crate::Error::decode)
}
pub(crate) fn blob(&self) -> crate::Result<&'c [u8]> {
let index = self.index as i32;
#[allow(unsafe_code)]
let ptr = unsafe { sqlite3_column_blob(self.statement().handle(), index) };
#[allow(unsafe_code)]
let len = unsafe { sqlite3_column_bytes(self.statement().handle(), index) };
#[allow(unsafe_code)]
let raw = unsafe { slice::from_raw_parts(ptr as *const u8, len as usize) };
Ok(raw)
}
}