Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrif committed Jan 31, 2016
1 parent 483b50b commit 9b21e32
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions diesel/src/connection/sqlite/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ extern crate libsqlite3_sys as ffi;
extern crate libc;

use std::ffi::{CString, CStr};
use std::io::{stderr, Write};
use std::{ptr, str};

use result::*;
Expand Down Expand Up @@ -33,22 +34,20 @@ impl RawConnection {
pub fn exec(&self, query: &str) -> QueryResult<()> {
let mut err_msg = ptr::null_mut();
let query = try!(CString::new(query));
let callback_fn = None;
let callback_arg = ptr::null_mut();
unsafe {
ffi::sqlite3_exec(
self.internal_connection,
query.as_ptr(),
None,
ptr::null_mut(),
callback_fn,
callback_arg,
&mut err_msg,
);
}

if !err_msg.is_null() {
let msg = unsafe {
let bytes = CStr::from_ptr(err_msg).to_bytes();
str::from_utf8_unchecked(bytes).into()
};
unsafe { ffi::sqlite3_free(err_msg as *mut libc::c_void) };
let msg = convert_to_string_and_free(err_msg);
Err(DatabaseError(msg))
} else {
Ok(())
Expand All @@ -69,3 +68,12 @@ impl Drop for RawConnection {
}
}
}

fn convert_to_string_and_free(err_msg: *const libc::c_char) -> String {
let msg = unsafe {
let bytes = CStr::from_ptr(err_msg).to_bytes();
str::from_utf8_unchecked(bytes).into()
};
unsafe { ffi::sqlite3_free(err_msg as *mut libc::c_void) };
msg
}

0 comments on commit 9b21e32

Please sign in to comment.