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
use core::slice;
use std::ffi::CStr;
use std::str::from_utf8_unchecked;
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::Statement;
use crate::sqlite::type_info::SqliteType;
use crate::sqlite::{Sqlite, SqliteTypeInfo};
use crate::value::RawValue;
use crate::decode::Decode;
use crate::Result;
#[derive(Debug,Clone)]
pub struct SqliteValue<'c> {
pub(super) index: i32,
pub(super) statement: &'c Statement,
}
impl<'c> SqliteValue<'c> {
pub(super) fn is_null(&self) -> bool {
self.r#type().is_none()
}
fn r#type(&self) -> Option<SqliteType> {
let type_code = unsafe {
if let Some(handle) = self.statement.handle() {
sqlite3_column_type(handle, self.index)
} else {
return None;
}
};
match type_code {
SQLITE_INTEGER => Some(SqliteType::Integer),
SQLITE_FLOAT => Some(SqliteType::Float),
SQLITE_TEXT => Some(SqliteType::Text),
SQLITE_BLOB => Some(SqliteType::Blob),
SQLITE_NULL => None,
_ => unreachable!("received unexpected column type: {}", type_code),
}
}
pub(super) fn int(&self) -> i32 {
unsafe {
self.statement
.handle()
.map_or(0, |handle| sqlite3_column_int(handle, self.index))
}
}
pub(super) fn int64(&self) -> i64 {
unsafe {
self.statement
.handle()
.map_or(0, |handle| sqlite3_column_int64(handle, self.index))
}
}
pub(super) fn double(&self) -> f64 {
unsafe {
self.statement
.handle()
.map_or(0.0, |handle| sqlite3_column_double(handle, self.index))
}
}
pub(super) fn text(&self) -> Option<&'c str> {
unsafe {
self.statement.handle().and_then(|handle| {
let ptr = sqlite3_column_text(handle, self.index);
if ptr.is_null() {
None
} else {
Some(from_utf8_unchecked(CStr::from_ptr(ptr as _).to_bytes()))
}
})
}
}
fn bytes(&self) -> usize {
unsafe {
self.statement
.handle()
.map_or(0, |handle| sqlite3_column_bytes(handle, self.index)) as usize
}
}
pub(super) fn blob(&self) -> &'c [u8] {
let ptr = unsafe {
if let Some(handle) = self.statement.handle() {
sqlite3_column_blob(handle, self.index)
} else {
return &[];
}
};
if ptr.is_null() {
return &[];
}
unsafe { slice::from_raw_parts(ptr as *const u8, self.bytes()) }
}
}
impl<'c> RawValue<'c> for SqliteValue<'c> {
type Database = Sqlite;
fn type_info(&self) -> Option<SqliteTypeInfo> {
Some(SqliteTypeInfo {
r#type: self.r#type()?,
affinity: None,
})
}
fn try_to_json(&self) -> Result<serde_json::Value> {
let type_string = self.r#type();
if type_string.is_none() {
return Ok(serde_json::Value::Null);
}
let type_string = type_string.unwrap();
return match type_string {
SqliteType::Text => {
let r:crate::Result<String> = Decode::<'_,Sqlite>::decode(self.clone());
if r.is_err() {
return Err(r.err().unwrap());
}
Ok(serde_json::Value::from(r.unwrap()))
},
SqliteType::Boolean => {
let r:crate::Result<bool> = Decode::<'_,Sqlite>::decode(self.clone());
if r.is_err() {
return Err(r.err().unwrap());
}
Ok(serde_json::Value::from(r.unwrap()))
},
SqliteType::Integer => {
let r:crate::Result<i64> = Decode::<'_,Sqlite>::decode(self.clone());
if r.is_err() {
return Err(r.err().unwrap());
}
Ok(serde_json::Value::from(r.unwrap()))
},
SqliteType::Float => {
let r:crate::Result<f64> = Decode::<'_,Sqlite>::decode(self.clone());
if r.is_err() {
return Err(r.err().unwrap());
}
Ok(serde_json::Value::from(r.unwrap()))
},
SqliteType::Blob => {
unimplemented!()
}
}
}
}