Repository navigation
Expand file tree
/
Copy pathvalue.rs
More file actions
135 lines (114 loc) · 3.91 KB
/
Copy pathvalue.rs
File metadata and controls
135 lines (114 loc) · 3.91 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
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;
pub struct SqliteValue<'c> {
pub(super) index: i32,
pub(super) statement: &'c 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> SqliteValue<'c> {
/// Returns true if the value should be intrepreted as NULL.
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 {
// unreachable: null statements do not have any values to type
return None;
}
};
// SQLITE_INTEGER, SQLITE_FLOAT, SQLITE_TEXT, SQLITE_BLOB, or SQLITE_NULL
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),
}
}
/// Returns the 32-bit INTEGER result.
pub(super) fn int(&self) -> i32 {
unsafe {
self.statement
.handle()
.map_or(0, |handle| sqlite3_column_int(handle, self.index))
}
}
/// Returns the 64-bit INTEGER result.
pub(super) fn int64(&self) -> i64 {
unsafe {
self.statement
.handle()
.map_or(0, |handle| sqlite3_column_int64(handle, self.index))
}
}
/// Returns the 64-bit, REAL result.
pub(super) fn double(&self) -> f64 {
unsafe {
self.statement
.handle()
.map_or(0.0, |handle| sqlite3_column_double(handle, self.index))
}
}
/// Returns the UTF-8 TEXT result.
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 {
// Returns the size of the result in bytes.
unsafe {
self.statement
.handle()
.map_or(0, |handle| sqlite3_column_bytes(handle, self.index)) as usize
}
}
/// Returns the BLOB result.
pub(super) fn blob(&self) -> &'c [u8] {
let ptr = unsafe {
if let Some(handle) = self.statement.handle() {
sqlite3_column_blob(handle, self.index)
} else {
// Null statements do not exist
return &[];
}
};
if ptr.is_null() {
// Empty BLOBs are received as null pointers
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,
})
}
}