diff options
author | David LeGare <legare@google.com> | 2022-03-02 20:59:04 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2022-03-02 20:59:04 +0000 |
commit | 93c8cc61e4753e8db9b57fbc2e3e9274495e9809 (patch) | |
tree | ed3dae2c91a4d67fe43675ec1ee9eaf437cd8280 /src/session.rs | |
parent | 8a0e0cefcdd7fd8f911691e4422d8e58311cd132 (diff) | |
parent | 8e934d15c4f2316c5f177e0cf7306b089912719e (diff) | |
download | rusqlite-android13-qpr3-s4-release.tar.gz |
Update rusqlite to 0.27.0 am: 82c86ae98e am: 8386405f4f am: 8e934d15c4t_frc_odp_330442040t_frc_odp_330442000t_frc_ase_330444010android-13.0.0_r83android-13.0.0_r82android-13.0.0_r81android-13.0.0_r80android-13.0.0_r79android-13.0.0_r78android-13.0.0_r77android-13.0.0_r76android-13.0.0_r75android-13.0.0_r74android-13.0.0_r73android-13.0.0_r72android-13.0.0_r71android-13.0.0_r70android-13.0.0_r69android-13.0.0_r68android-13.0.0_r67android-13.0.0_r66android-13.0.0_r65android-13.0.0_r64android-13.0.0_r63android-13.0.0_r62android-13.0.0_r61android-13.0.0_r60android-13.0.0_r59android-13.0.0_r58android-13.0.0_r57android-13.0.0_r56android-13.0.0_r55android-13.0.0_r54android-13.0.0_r53android-13.0.0_r52android-13.0.0_r51android-13.0.0_r50android-13.0.0_r49android-13.0.0_r48android-13.0.0_r47android-13.0.0_r46android-13.0.0_r45android-13.0.0_r44android-13.0.0_r43android-13.0.0_r42android-13.0.0_r41android-13.0.0_r40android-13.0.0_r39android-13.0.0_r38android-13.0.0_r37android-13.0.0_r36android-13.0.0_r35android-13.0.0_r34android-13.0.0_r33android-13.0.0_r32android-13.0.0_r30android-13.0.0_r29android-13.0.0_r28android-13.0.0_r27android-13.0.0_r24android-13.0.0_r23android-13.0.0_r22android-13.0.0_r21android-13.0.0_r20android-13.0.0_r19android-13.0.0_r18android-13.0.0_r17android-13.0.0_r16aml_go_odp_330912000aml_go_ads_330915100aml_go_ads_330915000aml_go_ads_330913000android13-qpr3-s9-releaseandroid13-qpr3-s8-releaseandroid13-qpr3-s7-releaseandroid13-qpr3-s6-releaseandroid13-qpr3-s5-releaseandroid13-qpr3-s4-releaseandroid13-qpr3-s3-releaseandroid13-qpr3-s2-releaseandroid13-qpr3-s14-releaseandroid13-qpr3-s13-releaseandroid13-qpr3-s12-releaseandroid13-qpr3-s11-releaseandroid13-qpr3-s10-releaseandroid13-qpr3-s1-releaseandroid13-qpr3-releaseandroid13-qpr3-c-s8-releaseandroid13-qpr3-c-s7-releaseandroid13-qpr3-c-s6-releaseandroid13-qpr3-c-s5-releaseandroid13-qpr3-c-s4-releaseandroid13-qpr3-c-s3-releaseandroid13-qpr3-c-s2-releaseandroid13-qpr3-c-s12-releaseandroid13-qpr3-c-s11-releaseandroid13-qpr3-c-s10-releaseandroid13-qpr3-c-s1-releaseandroid13-qpr2-s9-releaseandroid13-qpr2-s8-releaseandroid13-qpr2-s7-releaseandroid13-qpr2-s6-releaseandroid13-qpr2-s5-releaseandroid13-qpr2-s3-releaseandroid13-qpr2-s2-releaseandroid13-qpr2-s12-releaseandroid13-qpr2-s11-releaseandroid13-qpr2-s10-releaseandroid13-qpr2-s1-releaseandroid13-qpr2-releaseandroid13-qpr2-b-s1-releaseandroid13-qpr1-s8-releaseandroid13-qpr1-s7-releaseandroid13-qpr1-s6-releaseandroid13-qpr1-s5-releaseandroid13-qpr1-s4-releaseandroid13-qpr1-s3-releaseandroid13-qpr1-s2-releaseandroid13-qpr1-s1-releaseandroid13-qpr1-releaseandroid13-mainline-go-adservices-releaseandroid13-frc-odp-releaseandroid13-devandroid13-d4-s2-releaseandroid13-d4-s1-releaseandroid13-d4-releaseandroid13-d3-s1-releaseandroid13-d2-release
Original change: https://android-review.googlesource.com/c/platform/external/rust/crates/rusqlite/+/2006011
Change-Id: Id37e832138a66e56e7ba634631e6517b66bd2ff0
Diffstat (limited to 'src/session.rs')
-rw-r--r-- | src/session.rs | 126 |
1 files changed, 58 insertions, 68 deletions
diff --git a/src/session.rs b/src/session.rs index 4e4c354..b02d306 100644 --- a/src/session.rs +++ b/src/session.rs @@ -1,4 +1,4 @@ -//! `feature = "session"` [Session Extension](https://sqlite.org/sessionintro.html) +//! [Session Extension](https://sqlite.org/sessionintro.html) #![allow(non_camel_case_types)] use std::ffi::CStr; @@ -11,7 +11,7 @@ use std::slice::{from_raw_parts, from_raw_parts_mut}; use fallible_streaming_iterator::FallibleStreamingIterator; -use crate::error::error_from_sqlite_code; +use crate::error::{check, error_from_sqlite_code}; use crate::ffi; use crate::hooks::Action; use crate::types::ValueRef; @@ -19,7 +19,7 @@ use crate::{errmsg_to_string, str_to_cstring, Connection, DatabaseName, Result}; // https://sqlite.org/session.html -/// `feature = "session"` An instance of this object is a session that can be +/// An instance of this object is a session that can be /// used to record changes to a database. pub struct Session<'conn> { phantom: PhantomData<&'conn Connection>, @@ -40,12 +40,12 @@ impl Session<'_> { db: &'conn Connection, name: DatabaseName<'_>, ) -> Result<Session<'conn>> { - let name = name.to_cstring()?; + let name = name.as_cstring()?; let db = db.db.borrow_mut().db; let mut s: *mut ffi::sqlite3_session = ptr::null_mut(); - check!(unsafe { ffi::sqlite3session_create(db, name.as_ptr(), &mut s) }); + check(unsafe { ffi::sqlite3session_create(db, name.as_ptr(), &mut s) })?; Ok(Session { phantom: PhantomData, @@ -109,15 +109,14 @@ impl Session<'_> { None }; let table = table.as_ref().map(|s| s.as_ptr()).unwrap_or(ptr::null()); - unsafe { check!(ffi::sqlite3session_attach(self.s, table)) }; - Ok(()) + check(unsafe { ffi::sqlite3session_attach(self.s, table) }) } /// Generate a Changeset pub fn changeset(&mut self) -> Result<Changeset> { let mut n = 0; let mut cs: *mut c_void = ptr::null_mut(); - check!(unsafe { ffi::sqlite3session_changeset(self.s, &mut n, &mut cs) }); + check(unsafe { ffi::sqlite3session_changeset(self.s, &mut n, &mut cs) })?; Ok(Changeset { cs, n }) } @@ -125,14 +124,13 @@ impl Session<'_> { #[inline] pub fn changeset_strm(&mut self, output: &mut dyn Write) -> Result<()> { let output_ref = &output; - check!(unsafe { + check(unsafe { ffi::sqlite3session_changeset_strm( self.s, Some(x_output), output_ref as *const &mut dyn Write as *mut c_void, ) - }); - Ok(()) + }) } /// Generate a Patchset @@ -140,7 +138,7 @@ impl Session<'_> { pub fn patchset(&mut self) -> Result<Changeset> { let mut n = 0; let mut ps: *mut c_void = ptr::null_mut(); - check!(unsafe { ffi::sqlite3session_patchset(self.s, &mut n, &mut ps) }); + check(unsafe { ffi::sqlite3session_patchset(self.s, &mut n, &mut ps) })?; // TODO Validate: same struct Ok(Changeset { cs: ps, n }) } @@ -149,19 +147,18 @@ impl Session<'_> { #[inline] pub fn patchset_strm(&mut self, output: &mut dyn Write) -> Result<()> { let output_ref = &output; - check!(unsafe { + check(unsafe { ffi::sqlite3session_patchset_strm( self.s, Some(x_output), output_ref as *const &mut dyn Write as *mut c_void, ) - }); - Ok(()) + }) } /// Load the difference between tables. pub fn diff(&mut self, from: DatabaseName<'_>, table: &str) -> Result<()> { - let from = from.to_cstring()?; + let from = from.as_cstring()?; let table = str_to_cstring(table)?; let table = table.as_ptr(); unsafe { @@ -223,23 +220,22 @@ impl Drop for Session<'_> { } } -/// `feature = "session"` Invert a changeset +/// Invert a changeset #[inline] pub fn invert_strm(input: &mut dyn Read, output: &mut dyn Write) -> Result<()> { let input_ref = &input; let output_ref = &output; - check!(unsafe { + check(unsafe { ffi::sqlite3changeset_invert_strm( Some(x_input), input_ref as *const &mut dyn Read as *mut c_void, Some(x_output), output_ref as *const &mut dyn Write as *mut c_void, ) - }); - Ok(()) + }) } -/// `feature = "session"` Combine two changesets +/// Combine two changesets #[inline] pub fn concat_strm( input_a: &mut dyn Read, @@ -249,7 +245,7 @@ pub fn concat_strm( let input_a_ref = &input_a; let input_b_ref = &input_b; let output_ref = &output; - check!(unsafe { + check(unsafe { ffi::sqlite3changeset_concat_strm( Some(x_input), input_a_ref as *const &mut dyn Read as *mut c_void, @@ -258,11 +254,10 @@ pub fn concat_strm( Some(x_output), output_ref as *const &mut dyn Write as *mut c_void, ) - }); - Ok(()) + }) } -/// `feature = "session"` Changeset or Patchset +/// Changeset or Patchset pub struct Changeset { cs: *mut c_void, n: c_int, @@ -274,9 +269,9 @@ impl Changeset { pub fn invert(&self) -> Result<Changeset> { let mut n = 0; let mut cs = ptr::null_mut(); - check!(unsafe { + check(unsafe { ffi::sqlite3changeset_invert(self.n, self.cs, &mut n, &mut cs as *mut *mut _) - }); + })?; Ok(Changeset { cs, n }) } @@ -284,7 +279,7 @@ impl Changeset { #[inline] pub fn iter(&self) -> Result<ChangesetIter<'_>> { let mut it = ptr::null_mut(); - check!(unsafe { ffi::sqlite3changeset_start(&mut it as *mut *mut _, self.n, self.cs) }); + check(unsafe { ffi::sqlite3changeset_start(&mut it as *mut *mut _, self.n, self.cs) })?; Ok(ChangesetIter { phantom: PhantomData, it, @@ -297,9 +292,9 @@ impl Changeset { pub fn concat(a: &Changeset, b: &Changeset) -> Result<Changeset> { let mut n = 0; let mut cs = ptr::null_mut(); - check!(unsafe { + check(unsafe { ffi::sqlite3changeset_concat(a.n, a.cs, b.n, b.cs, &mut n, &mut cs as *mut *mut _) - }); + })?; Ok(Changeset { cs, n }) } } @@ -313,7 +308,7 @@ impl Drop for Changeset { } } -/// `feature = "session"` Cursor for iterating over the elements of a changeset +/// Cursor for iterating over the elements of a changeset /// or patchset. pub struct ChangesetIter<'changeset> { phantom: PhantomData<&'changeset Changeset>, @@ -326,13 +321,13 @@ impl ChangesetIter<'_> { #[inline] pub fn start_strm<'input>(input: &&'input mut dyn Read) -> Result<ChangesetIter<'input>> { let mut it = ptr::null_mut(); - check!(unsafe { + check(unsafe { ffi::sqlite3changeset_start_strm( &mut it as *mut *mut _, Some(x_input), input as *const &mut dyn Read as *mut c_void, ) - }); + })?; Ok(ChangesetIter { phantom: PhantomData, it, @@ -367,7 +362,7 @@ impl FallibleStreamingIterator for ChangesetIter<'_> { } } -/// `feature = "session"` +/// Operation pub struct Operation<'item> { table_name: &'item str, number_of_columns: i32, @@ -410,7 +405,7 @@ impl Drop for ChangesetIter<'_> { } } -/// `feature = "session"` An item passed to a conflict-handler by +/// An item passed to a conflict-handler by /// [`Connection::apply`](crate::Connection::apply), or an item generated by /// [`ChangesetIter::next`](ChangesetIter::next). // TODO enum ? Delete, Insert, Update, ... @@ -427,11 +422,11 @@ impl ChangesetItem { pub fn conflict(&self, col: usize) -> Result<ValueRef<'_>> { unsafe { let mut p_value: *mut ffi::sqlite3_value = ptr::null_mut(); - check!(ffi::sqlite3changeset_conflict( + check(ffi::sqlite3changeset_conflict( self.it, col as i32, &mut p_value, - )); + ))?; Ok(ValueRef::from_value(p_value)) } } @@ -444,7 +439,7 @@ impl ChangesetItem { pub fn fk_conflicts(&self) -> Result<i32> { unsafe { let mut p_out = 0; - check!(ffi::sqlite3changeset_fk_conflicts(self.it, &mut p_out)); + check(ffi::sqlite3changeset_fk_conflicts(self.it, &mut p_out))?; Ok(p_out) } } @@ -457,7 +452,7 @@ impl ChangesetItem { pub fn new_value(&self, col: usize) -> Result<ValueRef<'_>> { unsafe { let mut p_value: *mut ffi::sqlite3_value = ptr::null_mut(); - check!(ffi::sqlite3changeset_new(self.it, col as i32, &mut p_value,)); + check(ffi::sqlite3changeset_new(self.it, col as i32, &mut p_value))?; Ok(ValueRef::from_value(p_value)) } } @@ -470,7 +465,7 @@ impl ChangesetItem { pub fn old_value(&self, col: usize) -> Result<ValueRef<'_>> { unsafe { let mut p_value: *mut ffi::sqlite3_value = ptr::null_mut(); - check!(ffi::sqlite3changeset_old(self.it, col as i32, &mut p_value,)); + check(ffi::sqlite3changeset_old(self.it, col as i32, &mut p_value))?; Ok(ValueRef::from_value(p_value)) } } @@ -483,13 +478,13 @@ impl ChangesetItem { let mut indirect = 0; let tab = unsafe { let mut pz_tab: *const c_char = ptr::null(); - check!(ffi::sqlite3changeset_op( + check(ffi::sqlite3changeset_op( self.it, &mut pz_tab, &mut number_of_columns, &mut code, - &mut indirect - )); + &mut indirect, + ))?; CStr::from_ptr(pz_tab) }; let table_name = tab.to_str()?; @@ -507,17 +502,17 @@ impl ChangesetItem { let mut number_of_columns = 0; unsafe { let mut pks: *mut c_uchar = ptr::null_mut(); - check!(ffi::sqlite3changeset_pk( + check(ffi::sqlite3changeset_pk( self.it, &mut pks, - &mut number_of_columns - )); + &mut number_of_columns, + ))?; Ok(from_raw_parts(pks, number_of_columns as usize)) } } } -/// `feature = "session"` Used to combine two or more changesets or +/// Used to combine two or more changesets or /// patchsets pub struct Changegroup { cg: *mut ffi::sqlite3_changegroup, @@ -528,29 +523,27 @@ impl Changegroup { #[inline] pub fn new() -> Result<Self> { let mut cg = ptr::null_mut(); - check!(unsafe { ffi::sqlite3changegroup_new(&mut cg) }); + check(unsafe { ffi::sqlite3changegroup_new(&mut cg) })?; Ok(Changegroup { cg }) } /// Add a changeset #[inline] pub fn add(&mut self, cs: &Changeset) -> Result<()> { - check!(unsafe { ffi::sqlite3changegroup_add(self.cg, cs.n, cs.cs) }); - Ok(()) + check(unsafe { ffi::sqlite3changegroup_add(self.cg, cs.n, cs.cs) }) } /// Add a changeset read from `input` to this change group. #[inline] pub fn add_stream(&mut self, input: &mut dyn Read) -> Result<()> { let input_ref = &input; - check!(unsafe { + check(unsafe { ffi::sqlite3changegroup_add_strm( self.cg, Some(x_input), input_ref as *const &mut dyn Read as *mut c_void, ) - }); - Ok(()) + }) } /// Obtain a composite Changeset @@ -558,7 +551,7 @@ impl Changegroup { pub fn output(&mut self) -> Result<Changeset> { let mut n = 0; let mut output: *mut c_void = ptr::null_mut(); - check!(unsafe { ffi::sqlite3changegroup_output(self.cg, &mut n, &mut output) }); + check(unsafe { ffi::sqlite3changegroup_output(self.cg, &mut n, &mut output) })?; Ok(Changeset { cs: output, n }) } @@ -566,14 +559,13 @@ impl Changegroup { #[inline] pub fn output_strm(&mut self, output: &mut dyn Write) -> Result<()> { let output_ref = &output; - check!(unsafe { + check(unsafe { ffi::sqlite3changegroup_output_strm( self.cg, Some(x_output), output_ref as *const &mut dyn Write as *mut c_void, ) - }); - Ok(()) + }) } } @@ -587,7 +579,7 @@ impl Drop for Changegroup { } impl Connection { - /// `feature = "session"` Apply a changeset to a database + /// Apply a changeset to a database pub fn apply<F, C>(&self, cs: &Changeset, filter: Option<F>, conflict: C) -> Result<()> where F: Fn(&str) -> bool + Send + RefUnwindSafe + 'static, @@ -597,7 +589,7 @@ impl Connection { let filtered = filter.is_some(); let tuple = &mut (filter, conflict); - check!(unsafe { + check(unsafe { if filtered { ffi::sqlite3changeset_apply( db, @@ -617,11 +609,10 @@ impl Connection { tuple as *mut (Option<F>, C) as *mut c_void, ) } - }); - Ok(()) + }) } - /// `feature = "session"` Apply a changeset to a database + /// Apply a changeset to a database pub fn apply_strm<F, C>( &self, input: &mut dyn Read, @@ -637,7 +628,7 @@ impl Connection { let filtered = filter.is_some(); let tuple = &mut (filter, conflict); - check!(unsafe { + check(unsafe { if filtered { ffi::sqlite3changeset_apply_strm( db, @@ -657,12 +648,11 @@ impl Connection { tuple as *mut (Option<F>, C) as *mut c_void, ) } - }); - Ok(()) + }) } } -/// `feature = "session"` Constants passed to the conflict handler +/// Constants passed to the conflict handler /// See [here](https://sqlite.org/session.html#SQLITE_CHANGESET_CONFLICT) for details. #[allow(missing_docs)] #[repr(i32)] @@ -690,7 +680,7 @@ impl From<i32> for ConflictType { } } -/// `feature = "session"` Constants returned by the conflict handler +/// Constants returned by the conflict handler /// See [here](https://sqlite.org/session.html#SQLITE_CHANGESET_ABORT) for details. #[allow(missing_docs)] #[repr(i32)] @@ -826,7 +816,7 @@ mod test { assert_eq!("foo", op.table_name()); assert_eq!(1, op.number_of_columns()); assert_eq!(Action::SQLITE_INSERT, op.code()); - assert_eq!(false, op.indirect()); + assert!(!op.indirect()); let pk = item.pk()?; assert_eq!(&[1], pk); |