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

Commit 823f215

Browse files
committed
merge
2 parents 75ea042 + f86ac2e commit 823f215

File tree

4 files changed

+68
-38
lines changed

4 files changed

+68
-38
lines changed

redisql_lib/src/community_statement.rs

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

4141
pub struct Statement {
42-
stmt: *mut ffi::sqlite3_stmt,
42+
stmt: ptr::NonNull<ffi::sqlite3_stmt>,
4343
}
4444

4545
unsafe impl Send for Statement {}
@@ -48,7 +48,7 @@ unsafe impl Sync for Statement {}
4848
impl<'a> fmt::Display for Statement {
4949
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5050
let sql = unsafe {
51-
CStr::from_ptr(ffi::sqlite3_sql(self.stmt))
51+
CStr::from_ptr(ffi::sqlite3_sql(self.as_ptr()))
5252
.to_string_lossy()
5353
.into_owned()
5454
};
@@ -59,7 +59,7 @@ impl<'a> fmt::Display for Statement {
5959
impl<'a> Drop for Statement {
6060
fn drop(&mut self) {
6161
unsafe {
62-
ffi::sqlite3_finalize(self.stmt);
62+
ffi::sqlite3_finalize(self.as_ptr());
6363
};
6464
}
6565
}
@@ -87,10 +87,13 @@ pub fn generate_statements(
8787
&mut next_query,
8888
)
8989
};
90+
9091
match r {
9192
ffi::SQLITE_OK => {
92-
let stmt = Statement { stmt };
93-
stmts.push(stmt);
93+
if !stmt.is_null() {
94+
let stmt = Statement::from_ptr(stmt);
95+
stmts.push(stmt);
96+
}
9497
if unsafe { *next_query } == 0 {
9598
let (num_parameters, parameters) =
9699
count_parameters(&stmts)?;
@@ -100,29 +103,34 @@ pub fn generate_statements(
100103
number_parameters: num_parameters,
101104
_parameters: parameters,
102105
});
103-
}
106+
};
104107
}
105108
_ => return Err(conn.get_last_error()),
106109
}
107110
}
108111
}
109112

110113
impl Statement {
114+
fn from_ptr(stmt: *mut ffi::sqlite3_stmt) -> Self {
115+
Statement {
116+
stmt: ptr::NonNull::new(stmt).unwrap(),
117+
}
118+
}
111119
fn execute(
112120
&self,
113121
db: &RawConnection,
114122
) -> Result<Cursor, SQLite3Error> {
115-
match unsafe { ffi::sqlite3_step(self.stmt) } {
123+
match unsafe { ffi::sqlite3_step(self.as_ptr()) } {
116124
ffi::SQLITE_OK => Ok(Cursor::OKCursor {}),
117125
ffi::SQLITE_DONE => {
118126
let modified_rows =
119127
unsafe { ffi::sqlite3_changes(db.get_db()) };
120128
Ok(Cursor::DONECursor { modified_rows })
121129
}
122130
ffi::SQLITE_ROW => {
123-
let num_columns =
124-
unsafe { ffi::sqlite3_column_count(self.stmt) }
125-
as i32;
131+
let num_columns = unsafe {
132+
ffi::sqlite3_column_count(self.as_ptr())
133+
} as i32;
126134
Ok(Cursor::RowsCursor {
127135
stmt: self,
128136
num_columns,
@@ -134,10 +142,13 @@ impl Statement {
134142
}
135143
}
136144
fn get_last_error(&self) -> SQLite3Error {
137-
let db = unsafe { ffi::sqlite3_db_handle(self.stmt) };
145+
let db = unsafe { ffi::sqlite3_db_handle(self.as_ptr()) };
138146
let rc = RawConnection::from_db_handler(db);
139147
rc.get_last_error()
140148
}
149+
pub fn as_ptr(&self) -> *mut ffi::sqlite3_stmt {
150+
self.stmt.as_ptr()
151+
}
141152
}
142153

143154
impl<'a> StatementTrait<'a> for Statement {
@@ -165,15 +176,15 @@ impl<'a> StatementTrait<'a> for Statement {
165176
)
166177
};
167178
match r {
168-
ffi::SQLITE_OK => Ok(Statement { stmt }),
179+
ffi::SQLITE_OK => Ok(Statement::from_ptr(stmt)),
169180
_ => Err(conn.get_last_error()),
170181
}
171182
}
172183

173184
fn reset(&self) {
174185
unsafe {
175-
ffi::sqlite3_reset(self.stmt);
176-
ffi::sqlite3_clear_bindings(self.stmt);
186+
ffi::sqlite3_reset(self.as_ptr());
187+
ffi::sqlite3_clear_bindings(self.as_ptr());
177188
}
178189
}
179190

@@ -202,7 +213,7 @@ impl<'a> StatementTrait<'a> for Statement {
202213
}
203214
match unsafe {
204215
ffi::sqlite3_bind_text(
205-
self.stmt,
216+
self.as_ptr(),
206217
index,
207218
value.as_ptr() as *const c_char,
208219
value.len() as i32,
@@ -214,19 +225,15 @@ impl<'a> StatementTrait<'a> for Statement {
214225
}
215226
}
216227

217-
fn get_raw_stmt(&self) -> *mut ffi::sqlite3_stmt {
218-
self.stmt
219-
}
220-
221228
fn is_read_only(&self) -> bool {
222-
let v = unsafe { ffi::sqlite3_stmt_readonly(self.stmt) };
229+
let v = unsafe { ffi::sqlite3_stmt_readonly(self.as_ptr()) };
223230
v != 0
224231
}
225232
}
226233

227234
impl<'a> StatementTrait<'a> for MultiStatement {
228235
fn reset(&self) {
229-
self.stmts.iter().map(|stmt| stmt.reset()).count();
236+
self.stmts.iter().map(StatementTrait::reset).count();
230237
}
231238
fn execute(&self) -> Result<Cursor, SQLite3Error> {
232239
let db = self.db.clone();
@@ -313,9 +320,6 @@ impl<'a> StatementTrait<'a> for MultiStatement {
313320
) -> Result<Self, SQLite3Error> {
314321
generate_statements(conn, query)
315322
}
316-
fn get_raw_stmt(&self) -> *mut ffi::sqlite3_stmt {
317-
self.stmts[0].stmt
318-
}
319323

320324
fn is_read_only(&self) -> bool {
321325
for stmt in &self.stmts {
@@ -392,11 +396,12 @@ fn get_parameter_name(
392396
stmt: &Statement,
393397
index: i32,
394398
) -> Result<Option<Parameters>, SQLite3Error> {
395-
let parameter_name_ptr =
396-
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+
};
397402
let index_parameter = unsafe {
398403
ffi::sqlite3_bind_parameter_index(
399-
stmt.stmt,
404+
stmt.as_ptr(),
400405
parameter_name_ptr,
401406
)
402407
};
@@ -410,7 +415,7 @@ fn get_parameters(
410415
stmt: &Statement,
411416
) -> Result<Vec<Parameters>, SQLite3Error> {
412417
let total_paramenters =
413-
unsafe { ffi::sqlite3_bind_parameter_count(stmt.stmt) }
418+
unsafe { ffi::sqlite3_bind_parameter_count(stmt.as_ptr()) }
414419
as usize;
415420
if total_paramenters == 0 {
416421
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);

src/commands.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -972,12 +972,12 @@ pub extern "C" fn MakeCopy(
972972
)
973973
.unwrap();
974974
STATISTICS.copy_err();
975-
return unsafe {
975+
unsafe {
976976
r::rm::ffi::RedisModule_ReplyWithError.unwrap()(
977977
context.as_ptr(),
978978
error.as_ptr(),
979979
)
980-
};
980+
}
981981
}
982982
}
983983
}

test/correctness/test.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,32 @@ def test_null_terminated(self):
860860
one = self.exec_naked("REDISQL.EXEC", "NULL", "SELECT 1" + b'\x00')
861861
self.assertEquals(one, [[1]])
862862

863+
class TestBlankAfterSemicolon(TestRediSQLWithExec):
864+
def test_whitespace_after_semicolon(self):
865+
with DB(self, "NULL"):
866+
one = self.exec_naked("REDISQL.EXEC", "NULL", "SELECT 1; ")
867+
self.assertEquals(one, [[1]])
868+
869+
def test_newline_after_semicolon(self):
870+
with DB(self, "NULL"):
871+
one = self.exec_naked("REDISQL.EXEC", "NULL", "SELECT 1;\n")
872+
self.assertEquals(one, [[1]])
873+
874+
def test_mix_after_semicolon(self):
875+
with DB(self, "NULL"):
876+
one = self.exec_naked("REDISQL.EXEC", "NULL", "SELECT 1; \n ")
877+
self.assertEquals(one, [[1]])
878+
879+
def test_whitespace_after_semicolon_then_query(self):
880+
with DB(self, "NULL"):
881+
one = self.exec_naked("REDISQL.EXEC", "NULL", "SELECT 1; SELECT 2;")
882+
self.assertEquals(one, [[2]])
883+
884+
def test_newline_after_semicolon_then_query(self):
885+
with DB(self, "NULL"):
886+
one = self.exec_naked("REDISQL.EXEC", "NULL", "SELECT 1;\nSELECT 2;")
887+
self.assertEquals(one, [[2]])
888+
863889
if __name__ == '__main__':
864890
unittest.main()
865891

0 commit comments

Comments
 (0)