-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Sqlite Collation Support #446
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mehcode
merged 7 commits into
launchbadge:master
from
agentsim:feature/430-sqlite-collation-support
Jul 4, 2020
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8388e5d
Sqlite Collation Support
agentsim 4e17454
Fix formatting
agentsim dba4040
Merge branch 'master' into feature/430-sqlite-collation-support
agentsim 747ab33
Address feedback
agentsim fe19dcc
Merge branch 'master' into feature/430-sqlite-collation-support
agentsim 40188c1
Merge branch 'master' into feature/430-sqlite-collation-support
agentsim 68d88ac
Merge branch 'master' into feature/430-sqlite-collation-support
mehcode File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
use std::cmp::Ordering; | ||
use std::ffi::CString; | ||
use std::os::raw::{c_char, c_int, c_void}; | ||
use std::panic::{catch_unwind, UnwindSafe}; | ||
use std::slice; | ||
|
||
use libsqlite3_sys::{sqlite3_create_collation_v2, SQLITE_OK, SQLITE_UTF8}; | ||
|
||
use crate::error::Error; | ||
use crate::sqlite::connection::handle::ConnectionHandle; | ||
use crate::sqlite::SqliteError; | ||
|
||
unsafe extern "C" fn free_boxed_value<T>(p: *mut c_void) { | ||
drop(Box::from_raw(p as *mut T)); | ||
} | ||
|
||
pub(crate) fn create_collation<F>( | ||
handle: &ConnectionHandle, | ||
name: &str, | ||
collation: F, | ||
) -> Result<(), Error> | ||
where | ||
F: Fn(&str, &str) -> Ordering + Send + Sync + UnwindSafe + 'static, | ||
{ | ||
unsafe extern "C" fn call_boxed_closure<C>( | ||
arg1: *mut c_void, | ||
arg2: c_int, | ||
arg3: *const c_void, | ||
arg4: c_int, | ||
arg5: *const c_void, | ||
) -> c_int | ||
where | ||
C: Fn(&str, &str) -> Ordering, | ||
{ | ||
let r = catch_unwind(|| { | ||
let boxed_f: *mut C = arg1 as *mut C; | ||
assert!(!boxed_f.is_null(), "Internal error - null function pointer"); | ||
let s1 = { | ||
let c_slice = slice::from_raw_parts(arg3 as *const u8, arg2 as usize); | ||
String::from_utf8_lossy(c_slice) | ||
agentsim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
let s2 = { | ||
let c_slice = slice::from_raw_parts(arg5 as *const u8, arg4 as usize); | ||
String::from_utf8_lossy(c_slice) | ||
}; | ||
(*boxed_f)(s1.as_ref(), s2.as_ref()) | ||
}); | ||
let t = match r { | ||
Err(_) => { | ||
return -1; // FIXME How ? | ||
agentsim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
Ok(r) => r, | ||
}; | ||
|
||
match t { | ||
Ordering::Less => -1, | ||
Ordering::Equal => 0, | ||
Ordering::Greater => 1, | ||
} | ||
} | ||
|
||
let boxed_f: *mut F = Box::into_raw(Box::new(collation)); | ||
let c_name = | ||
CString::new(name).map_err(|_| err_protocol!("Invalid collation name: {}", name))?; | ||
agentsim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let flags = SQLITE_UTF8; | ||
let r = unsafe { | ||
sqlite3_create_collation_v2( | ||
handle.as_ptr(), | ||
c_name.as_ptr(), | ||
flags, | ||
boxed_f as *mut c_void, | ||
Some(call_boxed_closure::<F>), | ||
Some(free_boxed_value::<F>), | ||
) | ||
}; | ||
|
||
if r == SQLITE_OK { | ||
Ok(()) | ||
} else { | ||
Err(Error::Database(Box::new(SqliteError::new(handle.as_ptr())))) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.