Skip to content
This repository was archived by the owner on Jul 11, 2021. It is now read-only.

Commit 667768f

Browse files
committed
move Statement to use NonNull instead of raw pointer
1 parent 6120e3d commit 667768f

File tree

2 files changed

+34
-34
lines changed

2 files changed

+34
-34
lines changed

redisql_lib/src/community_statement.rs

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ impl<'a> fmt::Display for MultiStatement {
3939
}
4040

4141
pub struct Statement {
42-
stmt: *mut ffi::sqlite3_stmt,
43-
//stmt: ptr::NonNull<ffi::sqlite3_stmt>,
42+
stmt: ptr::NonNull<ffi::sqlite3_stmt>,
4443
}
4544

4645
unsafe impl Send for Statement {}
@@ -49,7 +48,7 @@ unsafe impl Sync for Statement {}
4948
impl<'a> fmt::Display for Statement {
5049
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5150
let sql = unsafe {
52-
CStr::from_ptr(ffi::sqlite3_sql(self.stmt))
51+
CStr::from_ptr(ffi::sqlite3_sql(self.as_ptr()))
5352
.to_string_lossy()
5453
.into_owned()
5554
};
@@ -60,7 +59,7 @@ impl<'a> fmt::Display for Statement {
6059
impl<'a> Drop for Statement {
6160
fn drop(&mut self) {
6261
unsafe {
63-
ffi::sqlite3_finalize(self.stmt);
62+
ffi::sqlite3_finalize(self.as_ptr());
6463
};
6564
}
6665
}
@@ -92,7 +91,7 @@ pub fn generate_statements(
9291
match r {
9392
ffi::SQLITE_OK => {
9493
if !stmt.is_null() {
95-
let stmt = Statement { stmt };
94+
let stmt = Statement::from_ptr(stmt);
9695
stmts.push(stmt);
9796
}
9897
if unsafe { *next_query } == 0 {
@@ -112,21 +111,26 @@ pub fn generate_statements(
112111
}
113112

114113
impl Statement {
114+
fn from_ptr(stmt: *mut ffi::sqlite3_stmt) -> Self {
115+
Statement {
116+
stmt: ptr::NonNull::new(stmt).unwrap(),
117+
}
118+
}
115119
fn execute(
116120
&self,
117121
db: &RawConnection,
118122
) -> Result<Cursor, SQLite3Error> {
119-
match unsafe { ffi::sqlite3_step(self.stmt) } {
123+
match unsafe { ffi::sqlite3_step(self.as_ptr()) } {
120124
ffi::SQLITE_OK => Ok(Cursor::OKCursor {}),
121125
ffi::SQLITE_DONE => {
122126
let modified_rows =
123127
unsafe { ffi::sqlite3_changes(db.get_db()) };
124128
Ok(Cursor::DONECursor { modified_rows })
125129
}
126130
ffi::SQLITE_ROW => {
127-
let num_columns =
128-
unsafe { ffi::sqlite3_column_count(self.stmt) }
129-
as i32;
131+
let num_columns = unsafe {
132+
ffi::sqlite3_column_count(self.as_ptr())
133+
} as i32;
130134
Ok(Cursor::RowsCursor {
131135
stmt: self,
132136
num_columns,
@@ -138,10 +142,13 @@ impl Statement {
138142
}
139143
}
140144
fn get_last_error(&self) -> SQLite3Error {
141-
let db = unsafe { ffi::sqlite3_db_handle(self.stmt) };
145+
let db = unsafe { ffi::sqlite3_db_handle(self.as_ptr()) };
142146
let rc = RawConnection::from_db_handler(db);
143147
rc.get_last_error()
144148
}
149+
pub fn as_ptr(&self) -> *mut ffi::sqlite3_stmt {
150+
self.stmt.as_ptr()
151+
}
145152
}
146153

147154
impl<'a> StatementTrait<'a> for Statement {
@@ -169,15 +176,15 @@ impl<'a> StatementTrait<'a> for Statement {
169176
)
170177
};
171178
match r {
172-
ffi::SQLITE_OK => Ok(Statement { stmt }),
179+
ffi::SQLITE_OK => Ok(Statement::from_ptr(stmt)),
173180
_ => Err(conn.get_last_error()),
174181
}
175182
}
176183

177184
fn reset(&self) {
178185
unsafe {
179-
ffi::sqlite3_reset(self.stmt);
180-
ffi::sqlite3_clear_bindings(self.stmt);
186+
ffi::sqlite3_reset(self.as_ptr());
187+
ffi::sqlite3_clear_bindings(self.as_ptr());
181188
}
182189
}
183190

@@ -206,7 +213,7 @@ impl<'a> StatementTrait<'a> for Statement {
206213
}
207214
match unsafe {
208215
ffi::sqlite3_bind_text(
209-
self.stmt,
216+
self.as_ptr(),
210217
index,
211218
value.as_ptr() as *const c_char,
212219
value.len() as i32,
@@ -218,12 +225,8 @@ impl<'a> StatementTrait<'a> for Statement {
218225
}
219226
}
220227

221-
fn get_raw_stmt(&self) -> *mut ffi::sqlite3_stmt {
222-
self.stmt
223-
}
224-
225228
fn is_read_only(&self) -> bool {
226-
let v = unsafe { ffi::sqlite3_stmt_readonly(self.stmt) };
229+
let v = unsafe { ffi::sqlite3_stmt_readonly(self.as_ptr()) };
227230
v != 0
228231
}
229232
}
@@ -317,9 +320,6 @@ impl<'a> StatementTrait<'a> for MultiStatement {
317320
) -> Result<Self, SQLite3Error> {
318321
generate_statements(conn, query)
319322
}
320-
fn get_raw_stmt(&self) -> *mut ffi::sqlite3_stmt {
321-
self.stmts[0].stmt
322-
}
323323

324324
fn is_read_only(&self) -> bool {
325325
for stmt in &self.stmts {
@@ -396,11 +396,12 @@ fn get_parameter_name(
396396
stmt: &Statement,
397397
index: i32,
398398
) -> Result<Option<Parameters>, SQLite3Error> {
399-
let parameter_name_ptr =
400-
unsafe { ffi::sqlite3_bind_parameter_name(stmt.stmt, index) };
399+
let parameter_name_ptr = unsafe {
400+
ffi::sqlite3_bind_parameter_name(stmt.as_ptr(), index)
401+
};
401402
let index_parameter = unsafe {
402403
ffi::sqlite3_bind_parameter_index(
403-
stmt.stmt,
404+
stmt.as_ptr(),
404405
parameter_name_ptr,
405406
)
406407
};
@@ -414,7 +415,7 @@ fn get_parameters(
414415
stmt: &Statement,
415416
) -> Result<Vec<Parameters>, SQLite3Error> {
416417
let total_paramenters =
417-
unsafe { ffi::sqlite3_bind_parameter_count(stmt.stmt) }
418+
unsafe { ffi::sqlite3_bind_parameter_count(stmt.as_ptr()) }
418419
as usize;
419420
if total_paramenters == 0 {
420421
return Ok(vec![]);

redisql_lib/src/sqlite.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ pub trait StatementTrait<'a>: Sized {
168168
index: i32,
169169
value: &str,
170170
) -> Result<SQLiteOK, SQLite3Error>;
171-
fn get_raw_stmt(&self) -> *mut ffi::sqlite3_stmt;
172171
fn is_read_only(&self) -> bool {
173172
false
174173
}
@@ -198,23 +197,23 @@ pub enum Entity {
198197

199198
impl Entity {
200199
fn new(stmt: &Statement, i: i32) -> Entity {
201-
match get_entity_type(stmt.get_raw_stmt(), i) {
200+
match get_entity_type(stmt.as_ptr(), i) {
202201
EntityType::Integer => {
203202
let int = unsafe {
204-
ffi::sqlite3_column_int64(stmt.get_raw_stmt(), i)
203+
ffi::sqlite3_column_int64(stmt.as_ptr(), i)
205204
};
206205
Entity::Integer { int }
207206
}
208207
EntityType::Float => {
209208
let value = unsafe {
210-
ffi::sqlite3_column_double(stmt.get_raw_stmt(), i)
209+
ffi::sqlite3_column_double(stmt.as_ptr(), i)
211210
};
212211
Entity::Float { float: value }
213212
}
214213
EntityType::Text => {
215214
let value = unsafe {
216215
CStr::from_ptr(ffi::sqlite3_column_text(
217-
stmt.get_raw_stmt(),
216+
stmt.as_ptr(),
218217
i,
219218
)
220219
as *const c_char)
@@ -227,7 +226,7 @@ impl Entity {
227226
EntityType::Blob => {
228227
let value = unsafe {
229228
CStr::from_ptr(ffi::sqlite3_column_blob(
230-
stmt.get_raw_stmt(),
229+
stmt.as_ptr(),
231230
i,
232231
)
233232
as *const c_char)
@@ -323,7 +322,7 @@ impl<'a> From<Cursor<'a>> for QueryResult {
323322
for i in 0..num_columns {
324323
let name = unsafe {
325324
CStr::from_ptr(ffi::sqlite3_column_name(
326-
stmt.get_raw_stmt(),
325+
stmt.as_ptr(),
327326
i,
328327
))
329328
.to_string_lossy()
@@ -340,7 +339,7 @@ impl<'a> From<Cursor<'a>> for QueryResult {
340339
}
341340
unsafe {
342341
*previous_status =
343-
ffi::sqlite3_step(stmt.get_raw_stmt());
342+
ffi::sqlite3_step(stmt.as_ptr());
344343
};
345344

346345
result.push(row);

0 commit comments

Comments
 (0)