Skip to content

Commit

Permalink
Allow nul bytes in strings w/ SQLite
Browse files Browse the repository at this point in the history
Unlike PG, SQLite actually allows all valid UTF-8 strings in its data
type which is documented to be UTF-8. I find this especially ironic,
since PG does not interface over C, but basically follows C semantics,
while SQLite which we *only* access via C, does not.
  • Loading branch information
sgrif committed Feb 3, 2016
1 parent e9abd87 commit 22fe783
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
7 changes: 4 additions & 3 deletions diesel/src/connection/sqlite/sqlite_value.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
extern crate libsqlite3_sys as ffi;
extern crate libc;

use std::ffi::CStr;
use std::{slice, str};

use backend::Sqlite;
Expand All @@ -25,10 +24,12 @@ impl SqliteValue {
}
}

pub fn read_text(&self) -> Result<&str, str::Utf8Error> {
pub fn read_text(&self) -> &str {
unsafe {
let ptr = ffi::sqlite3_column_text(self.inner_statement, self.col_index);
CStr::from_ptr(ptr as *const libc::c_char).to_str()
let len = ffi::sqlite3_column_bytes(self.inner_statement, self.col_index);
let bytes = slice::from_raw_parts(ptr as *const u8, len as usize);
str::from_utf8_unchecked(bytes)
}
}

Expand Down
2 changes: 1 addition & 1 deletion diesel/src/types/impls/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use types::{self, FromSql};

impl FromSql<types::VarChar, Sqlite> for String {
fn from_sql(value: Option<&SqliteValue>) -> Result<Self, Box<Error>> {
let text = try!(not_none!(value).read_text());
let text = not_none!(value).read_text();
Ok(text.into())
}
}
Expand Down

0 comments on commit 22fe783

Please sign in to comment.