|
1 | 1 | extern crate libsqlite3_sys as ffi; |
2 | 2 |
|
3 | | -use std::ffi::{CStr, CString, NulError}; |
| 3 | +use std::ffi::{CString, NulError}; |
4 | 4 | use std::io::{stderr, Write}; |
5 | 5 | use std::os::raw as libc; |
6 | 6 | use std::ptr::NonNull; |
7 | 7 | use std::{mem, ptr, slice, str}; |
8 | 8 |
|
9 | 9 | use super::functions::{build_sql_function_args, process_sql_function_result}; |
10 | 10 | use super::serialized_value::SerializedValue; |
| 11 | +use super::stmt::ensure_sqlite_ok; |
11 | 12 | use super::{Sqlite, SqliteAggregateFunction}; |
12 | 13 | use crate::deserialize::FromSqlRow; |
13 | 14 | use crate::result::Error::DatabaseError; |
@@ -62,27 +63,20 @@ impl RawConnection { |
62 | 63 | } |
63 | 64 |
|
64 | 65 | pub fn exec(&self, query: &str) -> QueryResult<()> { |
65 | | - let mut err_msg = ptr::null_mut(); |
66 | 66 | let query = CString::new(query)?; |
67 | 67 | let callback_fn = None; |
68 | 68 | let callback_arg = ptr::null_mut(); |
69 | | - unsafe { |
| 69 | + let result = unsafe { |
70 | 70 | ffi::sqlite3_exec( |
71 | 71 | self.internal_connection.as_ptr(), |
72 | 72 | query.as_ptr(), |
73 | 73 | callback_fn, |
74 | 74 | callback_arg, |
75 | | - &mut err_msg, |
76 | | - ); |
77 | | - } |
| 75 | + ptr::null_mut(), |
| 76 | + ) |
| 77 | + }; |
78 | 78 |
|
79 | | - if err_msg.is_null() { |
80 | | - Ok(()) |
81 | | - } else { |
82 | | - let msg = convert_to_string_and_free(err_msg); |
83 | | - let error_kind = DatabaseErrorKind::Unknown; |
84 | | - Err(DatabaseError(error_kind, Box::new(msg))) |
85 | | - } |
| 79 | + ensure_sqlite_ok(result, self.internal_connection.as_ptr()) |
86 | 80 | } |
87 | 81 |
|
88 | 82 | pub fn rows_affected_by_last_query(&self) -> usize { |
@@ -235,16 +229,6 @@ impl Drop for RawConnection { |
235 | 229 | } |
236 | 230 | } |
237 | 231 |
|
238 | | -fn convert_to_string_and_free(err_msg: *const libc::c_char) -> String { |
239 | | - let msg = unsafe { |
240 | | - let bytes = CStr::from_ptr(err_msg).to_bytes(); |
241 | | - // sqlite is documented to return utf8 strings here |
242 | | - str::from_utf8_unchecked(bytes).into() |
243 | | - }; |
244 | | - unsafe { ffi::sqlite3_free(err_msg as *mut libc::c_void) }; |
245 | | - msg |
246 | | -} |
247 | | - |
248 | 232 | enum SqliteCallbackError { |
249 | 233 | Abort(&'static str), |
250 | 234 | DieselError(crate::result::Error), |
|
0 commit comments