Repository navigation
Expand file tree
/
Copy pathstatement.rs
More file actions
186 lines (147 loc) · 5.76 KB
/
Copy pathstatement.rs
File metadata and controls
186 lines (147 loc) · 5.76 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
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
use core::i32;
use core::ptr::{null_mut, NonNull};
use std::collections::HashMap;
use std::convert::TryInto;
use std::ffi::CStr;
use std::str;
use futures_core::future::BoxFuture;
use futures_core::stream::BoxStream;
use libsqlite3_sys::{
sqlite3_bind_parameter_count, sqlite3_column_count, sqlite3_column_decltype,
sqlite3_column_int64, sqlite3_column_name, sqlite3_column_type, sqlite3_finalize, sqlite3_step,
sqlite3_stmt, SQLITE_BLOB, SQLITE_DONE, SQLITE_FLOAT, SQLITE_INTEGER, SQLITE_NULL, SQLITE_ROW,
SQLITE_TEXT,
};
use crate::describe::{Column, Describe};
use crate::executor::Executor;
use crate::sqlite::arguments::SqliteValue;
use crate::sqlite::types::ValueKind;
use crate::sqlite::{Sqlite, SqliteArguments, SqliteConnection, SqliteRow, SqliteTypeInfo};
pub(super) enum Step {
// indicates that an operation has completed
Done,
// indicates that another row of output is available
Row,
}
pub(crate) struct Statement(pub(super) NonNull<sqlite3_stmt>);
// SAFE: See notes for the Send impl on [SqliteConnection].
#[allow(unsafe_code)]
unsafe impl Send for Statement {}
#[allow(unsafe_code)]
unsafe impl Sync for Statement {}
impl Statement {
pub(super) fn step(&mut self) -> crate::Result<Step> {
// <https://www.sqlite.org/c3ref/step.html>
#[allow(unsafe_code)]
let status = unsafe { sqlite3_step(self.0.as_ptr()) };
let step = match status {
SQLITE_DONE => Step::Done,
SQLITE_ROW => Step::Row,
_ => {
// TODO: Add handling of sqlite errors
// We need to bubble up as a [DatabaseError]
panic!("unexpected: {}", status);
}
};
Ok(step)
}
fn value_i64(&mut self, i: usize) -> SqliteValue {
// <https://www.sqlite.org/c3ref/bind_parameter_count.html>
#[allow(unsafe_code)]
let val = unsafe { sqlite3_column_int64(self.0.as_ptr(), i as _) };
SqliteValue::Int(val)
}
fn bind_parameter_count(&mut self) -> usize {
// <https://www.sqlite.org/c3ref/bind_parameter_count.html>
#[allow(unsafe_code)]
let count = unsafe { sqlite3_bind_parameter_count(self.0.as_ptr()) };
count as usize
}
pub(super) fn describe(&mut self) -> crate::Result<Describe<Sqlite>> {
let num_params = self.bind_parameter_count();
// All bind params are null in sqlite
let param_types = vec![SqliteTypeInfo::NULL; num_params].into_boxed_slice();
let num_columns = self.column_count();
let mut columns = Vec::with_capacity(num_columns);
for i in 0..num_columns {
let name = self.column_name(i);
let type_ = self.column_decltype(i);
columns.push(Column {
name: Some(name),
table_id: None,
type_info: type_,
});
}
Ok(Describe {
param_types,
result_columns: columns.into_boxed_slice(),
})
}
pub(super) fn column_decltype(&mut self, i: usize) -> SqliteTypeInfo {
// [from_utf8_unchecked]: UTF-8 is guaranteed by the SQLite3 API
// [sqlite3_column_decltype]: <https://www.sqlite.org/c3ref/column_decltype.html>
#[allow(unsafe_code)]
let name = unsafe {
let name_ptr = sqlite3_column_decltype(self.0.as_ptr(), i as _);
if name_ptr.is_null() {
""
} else {
str::from_utf8_unchecked(CStr::from_ptr(name_ptr).to_bytes())
}
};
match name {
"" => SqliteTypeInfo::NULL,
"text" => SqliteTypeInfo::new(ValueKind::Text),
"int" => SqliteTypeInfo::new(ValueKind::Int),
"double" => SqliteTypeInfo::new(ValueKind::Double),
"blob" => SqliteTypeInfo::new(ValueKind::Blob),
// TODO: What are the possible return values here?
_ => unreachable!("unexpected type name: {}", name),
}
}
pub(super) fn column_name(&mut self, i: usize) -> Box<str> {
// [from_utf8_unchecked]: UTF-8 is guaranteed by the SQLite3 API
// [sqlite3_column_name]: <https://www.sqlite.org/c3ref/column_name.html>
#[allow(unsafe_code)]
let name = unsafe {
str::from_utf8_unchecked(
CStr::from_ptr(sqlite3_column_name(self.0.as_ptr(), i as _)).to_bytes(),
)
};
name.into()
}
pub(super) fn column_names(&mut self) -> crate::Result<HashMap<Box<str>, usize>> {
let count = self.column_count();
let mut names = HashMap::with_capacity(count);
for i in 0..count {
names.insert(self.column_name(i), i);
}
Ok(names)
}
fn column_type(&mut self, i: usize) -> ValueKind {
// <https://www.sqlite.org/c3ref/column_count.html>
#[allow(unsafe_code)]
let typ = unsafe { sqlite3_column_type(self.0.as_ptr(), i as _) };
match typ {
SQLITE_INTEGER => ValueKind::Int,
SQLITE_BLOB => ValueKind::Blob,
SQLITE_NULL => ValueKind::Null,
SQLITE_TEXT => ValueKind::Text,
SQLITE_FLOAT => ValueKind::Double,
// TODO: What are the possible return values here?
_ => unreachable!("unexpected column type: {}", typ),
}
}
pub(super) fn column_count(&mut self) -> usize {
// <https://www.sqlite.org/c3ref/column_count.html>
#[allow(unsafe_code)]
let count = unsafe { sqlite3_column_count(self.0.as_ptr()) };
count as usize
}
pub(super) fn finalize(&mut self) -> crate::Result<()> {
// <https://www.sqlite.org/c3ref/finalize.html>
#[allow(unsafe_code)]
let status = unsafe { sqlite3_finalize(self.0.as_ptr()) };
Ok(())
}
}