Skip to content

Commit

Permalink
format code
Browse files Browse the repository at this point in the history
Change-Id: I73fb244869c13863b31b6f0b01b1bf9366311931
  • Loading branch information
wangfenjin committed Jun 28, 2021
1 parent ea590c2 commit 1f3bcad
Show file tree
Hide file tree
Showing 21 changed files with 208 additions and 488 deletions.
1 change: 0 additions & 1 deletion libduckdb-sys/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ pub enum ErrorCode {
Unknown,
}


#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Error {
pub code: ErrorCode,
Expand Down
2 changes: 0 additions & 2 deletions libduckdb-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ pub const DuckDBSuccess: c_uint = duckdb_state_DuckDBSuccess;
pub use self::error::*;
mod error;



#[cfg(test)]
mod tests {
use super::*;
Expand Down
9 changes: 3 additions & 6 deletions src/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ impl Statement<'_> {
self.stmt
.column_name(col)
.ok_or(Error::InvalidColumnIndex(col))
.map(|slice| {
str::from_utf8(slice.to_bytes()).expect("Invalid UTF-8 sequence in column name")
})
.map(|slice| str::from_utf8(slice.to_bytes()).expect("Invalid UTF-8 sequence in column name"))
}

/// Returns the column index in the result set for a given column name.
Expand Down Expand Up @@ -138,9 +136,8 @@ impl Statement<'_> {
for i in 0..n {
let name = self.column_name_unwrap(i);
let slice = self.stmt.column_decltype(i);
let decl_type = slice.map(|s| {
str::from_utf8(s.to_bytes()).expect("Invalid UTF-8 sequence in column declaration")
});
let decl_type =
slice.map(|s| str::from_utf8(s.to_bytes()).expect("Invalid UTF-8 sequence in column declaration"));
cols.push(Column { name, decl_type });
}
cols
Expand Down
10 changes: 2 additions & 8 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,14 @@ mod test {
db.set_db_config(DbConfig::SQLITE_DBCONFIG_ENABLE_FKEY, opposite),
Ok(opposite)
);
assert_eq!(
db.db_config(DbConfig::SQLITE_DBCONFIG_ENABLE_FKEY),
Ok(opposite)
);
assert_eq!(db.db_config(DbConfig::SQLITE_DBCONFIG_ENABLE_FKEY), Ok(opposite));

let opposite = !db.db_config(DbConfig::SQLITE_DBCONFIG_ENABLE_TRIGGER)?;
assert_eq!(
db.set_db_config(DbConfig::SQLITE_DBCONFIG_ENABLE_TRIGGER, opposite),
Ok(opposite)
);
assert_eq!(
db.db_config(DbConfig::SQLITE_DBCONFIG_ENABLE_TRIGGER),
Ok(opposite)
);
assert_eq!(db.db_config(DbConfig::SQLITE_DBCONFIG_ENABLE_TRIGGER), Ok(opposite));
Ok(())
}
}
39 changes: 12 additions & 27 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::ffi;
use crate::types::FromSqlError;
use crate::types::Type;
use crate::{ffi};
use std::error;
use std::fmt;
use std::os::raw::c_int;
Expand Down Expand Up @@ -88,9 +88,7 @@ impl PartialEq for Error {
match (self, other) {
(Error::SqliteFailure(e1, s1), Error::SqliteFailure(e2, s2)) => e1 == e2 && s1 == s2,
(Error::SqliteSingleThreadedMode, Error::SqliteSingleThreadedMode) => true,
(Error::IntegralValueOutOfRange(i1, n1), Error::IntegralValueOutOfRange(i2, n2)) => {
i1 == i2 && n1 == n2
}
(Error::IntegralValueOutOfRange(i1, n1), Error::IntegralValueOutOfRange(i2, n2)) => i1 == i2 && n1 == n2,
(Error::Utf8Error(e1), Error::Utf8Error(e2)) => e1 == e2,
(Error::NulError(e1), Error::NulError(e2)) => e1 == e2,
(Error::InvalidParameterName(n1), Error::InvalidParameterName(n2)) => n1 == n2,
Expand All @@ -103,9 +101,7 @@ impl PartialEq for Error {
i1 == i2 && t1 == t2 && n1 == n2
}
(Error::StatementChangedRows(n1), Error::StatementChangedRows(n2)) => n1 == n2,
(Error::InvalidParameterCount(i1, n1), Error::InvalidParameterCount(i2, n2)) => {
i1 == i2 && n1 == n2
}
(Error::InvalidParameterCount(i1, n1), Error::InvalidParameterCount(i2, n2)) => i1 == i2 && n1 == n2,
(..) => false,
}
}
Expand Down Expand Up @@ -140,9 +136,7 @@ impl From<FromSqlError> for Error {
FromSqlError::InvalidUuidSize(_) => {
Error::FromSqlConversionFailure(UNKNOWN_COLUMN, Type::Blob, Box::new(err))
}
FromSqlError::Other(source) => {
Error::FromSqlConversionFailure(UNKNOWN_COLUMN, Type::Null, source)
}
FromSqlError::Other(source) => Error::FromSqlConversionFailure(UNKNOWN_COLUMN, Type::Null, source),
_ => Error::FromSqlConversionFailure(UNKNOWN_COLUMN, Type::Null, Box::new(err)),
}
}
Expand All @@ -153,17 +147,12 @@ impl fmt::Display for Error {
match *self {
Error::SqliteFailure(ref err, None) => err.fmt(f),
Error::SqliteFailure(_, Some(ref s)) => write!(f, "{}", s),
Error::SqliteSingleThreadedMode => write!(
f,
"SQLite was compiled or configured for single-threaded use only"
),
Error::SqliteSingleThreadedMode => {
write!(f, "SQLite was compiled or configured for single-threaded use only")
}
Error::FromSqlConversionFailure(i, ref t, ref err) => {
if i != UNKNOWN_COLUMN {
write!(
f,
"Conversion error from type {} at index: {}, {}",
t, i, err
)
write!(f, "Conversion error from type {} at index: {}, {}", t, i, err)
} else {
err.fmt(f)
}
Expand All @@ -185,11 +174,9 @@ impl fmt::Display for Error {
Error::QueryReturnedNoRows => write!(f, "Query returned no rows"),
Error::InvalidColumnIndex(i) => write!(f, "Invalid column index: {}", i),
Error::InvalidColumnName(ref name) => write!(f, "Invalid column name: {}", name),
Error::InvalidColumnType(i, ref name, ref t) => write!(
f,
"Invalid column type {} at index: {}, name: {}",
t, i, name
),
Error::InvalidColumnType(i, ref name, ref t) => {
write!(f, "Invalid column type {} at index: {}, name: {}", t, i, name)
}
Error::InvalidParameterCount(i1, n1) => write!(
f,
"Wrong number of parameters passed to query. Got {}, needed {}",
Expand Down Expand Up @@ -223,8 +210,7 @@ impl error::Error for Error {
| Error::StatementChangedRows(_)
| Error::InvalidQuery
| Error::MultipleStatement => None,
Error::FromSqlConversionFailure(_, _, ref err)
| Error::ToSqlConversionFailure(ref err) => Some(&**err),
Error::FromSqlConversionFailure(_, _, ref err) | Error::ToSqlConversionFailure(ref err) => Some(&**err),
}
}
}
Expand All @@ -240,4 +226,3 @@ pub fn error_from_duckdb_code(code: c_int, message: Option<String>) -> Error {
pub unsafe fn error_from_handle(_: *mut ffi::duckdb_connection, code: c_int) -> Error {
error_from_duckdb_code(code, None)
}

66 changes: 17 additions & 49 deletions src/inner_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ pub struct InnerConnection {
owned: bool,
}

impl Clone for InnerConnection {
fn clone(&self) -> Self {
unsafe { InnerConnection::new(self.db, false) }
}
}

impl InnerConnection {
#[allow(clippy::mutex_atomic)]
#[inline]
Expand All @@ -25,10 +31,7 @@ impl InnerConnection {
let r = ffi::duckdb_connect(db, &mut con);
if r != ffi::DuckDBSuccess {
ffi::duckdb_disconnect(&mut con);
let e = Error::SqliteFailure(
ffi::Error::new(r),
Some("connect error".to_owned()),
);
let e = Error::SqliteFailure(ffi::Error::new(r), Some("connect error".to_owned()));
// TODO: fix this
panic!("error {:?}", e);
}
Expand All @@ -41,11 +44,7 @@ impl InnerConnection {
}
}

pub fn open_with_flags(
c_path: &CStr,
_: OpenFlags,
_: Option<&CStr>,
) -> Result<InnerConnection> {
pub fn open_with_flags(c_path: &CStr, _: OpenFlags, _: Option<&CStr>) -> Result<InnerConnection> {
unsafe {
let mut db: ffi::duckdb_database = ptr::null_mut();
let r = if c_path.to_str().unwrap() == ":memory:" {
Expand All @@ -61,33 +60,10 @@ impl InnerConnection {
);
return Err(e);
}
let mut con: ffi::duckdb_connection = ptr::null_mut();
let r = ffi::duckdb_connect(db, &mut con);
if r != ffi::DuckDBSuccess {
ffi::duckdb_disconnect(&mut con);
ffi::duckdb_close(&mut db);
let e = Error::SqliteFailure(
ffi::Error::new(r),
Some("connect error".to_owned()),
);
// TODO: fix this
panic!("error {:?}", e);
}
Ok(InnerConnection {
db: db,
con: con,
// result: mem::zeroed(),
owned: true,
})
// Ok(InnerConnection::new(&mut db, true))
Ok(InnerConnection::new(db, true))
}
}

#[inline]
pub fn db(&self) -> ffi::duckdb_database {
self.db
}

#[inline]
pub fn decode_result(&mut self, code: c_int) -> Result<()> {
unsafe { InnerConnection::decode_result_raw(&mut self.db, code as c_uint) }
Expand All @@ -110,16 +86,16 @@ impl InnerConnection {
if self.con.is_null() {
return Ok(());
}
// let mut shared_handle = self.interrupt_lock.lock().unwrap();
// assert!(
// !shared_handle.is_null(),
// "Bug: Somehow interrupt_lock was cleared before the DB was closed"
// );
// let mut shared_handle = self.interrupt_lock.lock().unwrap();
// assert!(
// !shared_handle.is_null(),
// "Bug: Somehow interrupt_lock was cleared before the DB was closed"
// );
unsafe {
ffi::duckdb_disconnect(&mut self.con);
// Need to use _raw because _guard has a reference out, and
// decode_result takes &mut self.
// *shared_handle = ptr::null_mut();
// *shared_handle = ptr::null_mut();
self.con = ptr::null_mut();
if self.owned {
ffi::duckdb_close(&mut self.db);
Expand All @@ -145,17 +121,9 @@ impl InnerConnection {
pub fn prepare<'a>(&mut self, conn: &'a Connection, sql: &str) -> Result<Statement<'a>> {
let mut c_stmt: ffi::duckdb_prepared_statement = ptr::null_mut();
let c_str = CString::new(sql).unwrap();
let r = unsafe {
ffi::duckdb_prepare(
self.con,
c_str.as_ptr() as *const c_char,
&mut c_stmt,
)
};
let r = unsafe { ffi::duckdb_prepare(self.con, c_str.as_ptr() as *const c_char, &mut c_stmt) };
self.decode_result(r as c_int)?;
Ok(Statement::new(conn, unsafe {
RawStatement::new(c_stmt)
}))
Ok(Statement::new(conn, unsafe { RawStatement::new(c_stmt) }))
}

#[inline]
Expand Down
Loading

0 comments on commit 1f3bcad

Please sign in to comment.