add field_at_index() sql func

This commit is contained in:
Damien Elmes 2020-03-17 13:41:56 +10:00
parent cffa52ff82
commit 7eab504126
3 changed files with 17 additions and 4 deletions

View file

@ -40,10 +40,10 @@ serde_repr = "0.1.5"
num_enum = "0.4.2" num_enum = "0.4.2"
[target.'cfg(target_vendor="apple")'.dependencies] [target.'cfg(target_vendor="apple")'.dependencies]
rusqlite = { version = "0.21.0", features = ["trace"] } rusqlite = { version = "0.21.0", features = ["trace", "functions"] }
[target.'cfg(not(target_vendor="apple"))'.dependencies] [target.'cfg(not(target_vendor="apple"))'.dependencies]
rusqlite = { version = "0.21.0", features = ["trace", "bundled"] } rusqlite = { version = "0.21.0", features = ["trace", "functions", "bundled"] }
[target.'cfg(linux)'.dependencies] [target.'cfg(linux)'.dependencies]
reqwest = { version = "0.10.1", features = ["json", "native-tls-vendored"] } reqwest = { version = "0.10.1", features = ["json", "native-tls-vendored"] }

View file

@ -216,7 +216,6 @@ fn write_note_type(ctx: &mut SearchContext, _notetype: &str) {
} }
// fixme: need note type manager // fixme: need note type manager
// fixme: need field_at_index()
fn write_single_field(ctx: &mut SearchContext, field: &str, val: &str) { fn write_single_field(ctx: &mut SearchContext, field: &str, val: &str) {
let _ = field; let _ = field;
let fields = vec![(0, 0)]; // fixme: get list of (ntid, ordinal) let fields = vec![(0, 0)]; // fixme: get list of (ntid, ordinal)
@ -240,7 +239,6 @@ fn write_single_field(ctx: &mut SearchContext, field: &str, val: &str) {
write!(ctx.sql, ")").unwrap(); write!(ctx.sql, ")").unwrap();
} }
// fixme: need field_at_index()
fn write_dupes(ctx: &mut SearchContext, ntid: &ObjID, text: &str) { fn write_dupes(ctx: &mut SearchContext, ntid: &ObjID, text: &str) {
let csum = field_checksum(text); let csum = field_checksum(text);
write!( write!(

View file

@ -36,11 +36,26 @@ fn open_or_create_collection_db(path: &Path) -> Result<Connection> {
db.pragma_update(None, "cache_size", &(-40 * 1024))?; db.pragma_update(None, "cache_size", &(-40 * 1024))?;
db.pragma_update(None, "legacy_file_format", &false)?; db.pragma_update(None, "legacy_file_format", &false)?;
db.pragma_update(None, "journal", &"wal")?; db.pragma_update(None, "journal", &"wal")?;
db.set_prepared_statement_cache_capacity(50); db.set_prepared_statement_cache_capacity(50);
add_field_index_function(&db)?;
Ok(db) Ok(db)
} }
/// Adds sql function field_at_index(flds, index)
/// to split provided fields and return field at zero-based index.
/// If out of range, returns empty string.
fn add_field_index_function(db: &Connection) -> Result<()> {
db.create_scalar_function("field_at_index", 2, true, |ctx| {
let mut fields = ctx.get_raw(0).as_str()?.split('\x1f');
let idx: u16 = ctx.get(1)?;
Ok(fields.nth(idx as usize).unwrap_or("").to_string())
})
.map_err(Into::into)
}
/// Fetch schema version from database. /// Fetch schema version from database.
/// Return (must_create, version) /// Return (must_create, version)
fn schema_version(db: &Connection) -> Result<(bool, u8)> { fn schema_version(db: &Connection) -> Result<(bool, u8)> {