Handle text with internal nuls

Fix insertion and selection (#657).
This commit is contained in:
gwenn
2020-03-25 19:20:05 +01:00
parent 8c574be1e3
commit f71ea05603
2 changed files with 41 additions and 17 deletions

View File

@@ -1,6 +1,5 @@
use std::ffi::CStr;
use std::iter::IntoIterator;
use std::os::raw::{c_char, c_int, c_void};
use std::os::raw::{c_int, c_void};
#[cfg(feature = "array")]
use std::rc::Rc;
use std::slice::from_raw_parts;
@@ -659,15 +658,18 @@ impl Statement<'_> {
}
ffi::SQLITE_TEXT => {
let s = unsafe {
// Quoting from "Using SQLite" book:
// To avoid problems, an application should first extract the desired type using a sqlite3_column_xxx() function,
// and then call the appropriate sqlite3_column_bytes() function.
let text = ffi::sqlite3_column_text(raw, col as c_int);
let len = ffi::sqlite3_column_bytes(raw, col as c_int);
assert!(
!text.is_null(),
"unexpected SQLITE_TEXT column type with NULL data"
);
CStr::from_ptr(text as *const c_char)
from_raw_parts(text as *const u8, len as usize)
};
let s = s.to_bytes();
ValueRef::Text(s)
}
ffi::SQLITE_BLOB => {
@@ -1089,4 +1091,32 @@ mod test {
let stmt = conn.prepare(";").unwrap();
assert_eq!(0, stmt.column_count());
}
#[test]
fn test_utf16_conversion() {
let db = Connection::open_in_memory().unwrap();
db.pragma_update(None, "encoding", &"UTF-16le").unwrap();
let encoding: String = db
.pragma_query_value(None, "encoding", |row| row.get(0))
.unwrap();
assert_eq!("UTF-16le", encoding);
db.execute_batch("CREATE TABLE foo(x TEXT)").unwrap();
let expected = "テスト";
db.execute("INSERT INTO foo(x) VALUES (?)", &[&expected])
.unwrap();
let actual: String = db
.query_row("SELECT x FROM foo", NO_PARAMS, |row| row.get(0))
.unwrap();
assert_eq!(expected, actual);
}
#[test]
fn test_nul_byte() {
let db = Connection::open_in_memory().unwrap();
let expected = "a\x00b";
let actual: String = db
.query_row("SELECT ?", &[&expected], |row| row.get(0))
.unwrap();
assert_eq!(expected, actual);
}
}