@@ -39,8 +39,7 @@ impl<'a> fmt::Display for MultiStatement {
3939}
4040
4141pub struct Statement {
42- stmt : * mut ffi:: sqlite3_stmt ,
43- //stmt: ptr::NonNull<ffi::sqlite3_stmt>,
42+ stmt : ptr:: NonNull < ffi:: sqlite3_stmt > ,
4443}
4544
4645unsafe impl Send for Statement { }
@@ -49,7 +48,7 @@ unsafe impl Sync for Statement {}
4948impl < ' 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 {
6059impl < ' 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
114113impl 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
147154impl < ' 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 ! [ ] ) ;
0 commit comments