From fae9558041d15212281d994e358102e21bc5f92e Mon Sep 17 00:00:00 2001 From: rijkvp Date: Wed, 15 May 2024 11:52:27 +0200 Subject: [PATCH] Implement appender for date/time types --- src/appender/mod.rs | 22 +++++++++++++++------- src/types/value_ref.rs | 12 ++++++++++++ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/appender/mod.rs b/src/appender/mod.rs index 65b6db0f..cd4529be 100644 --- a/src/appender/mod.rs +++ b/src/appender/mod.rs @@ -118,15 +118,23 @@ impl Appender<'_> { ffi::duckdb_append_varchar_length(ptr, s.as_ptr() as *const c_char, s.len() as u64) }, ValueRef::Timestamp(u, i) => unsafe { - let micros = match u { - TimeUnit::Second => i * 1_000_000, - TimeUnit::Millisecond => i * 1_000, - TimeUnit::Microsecond => i, - TimeUnit::Nanosecond => i / 1_000, - }; - ffi::duckdb_append_timestamp(ptr, ffi::duckdb_timestamp { micros }) + ffi::duckdb_append_timestamp(ptr, ffi::duckdb_timestamp { micros: u.to_micros(i) }) }, ValueRef::Blob(b) => unsafe { ffi::duckdb_append_blob(ptr, b.as_ptr() as *const c_void, b.len() as u64) }, + ValueRef::Date32(d) => unsafe { ffi::duckdb_append_date(ptr, ffi::duckdb_date { days: d }) }, + ValueRef::Time64(u, v) => unsafe { + ffi::duckdb_append_time(ptr, ffi::duckdb_time { micros: u.to_micros(v) }) + }, + ValueRef::Interval { months, days, nanos } => unsafe { + ffi::duckdb_append_interval( + ptr, + ffi::duckdb_interval { + months, + days, + micros: nanos / 1000, + }, + ) + }, _ => unreachable!("not supported"), }; if rc != 0 { diff --git a/src/types/value_ref.rs b/src/types/value_ref.rs index d520f8f1..f4b0493d 100644 --- a/src/types/value_ref.rs +++ b/src/types/value_ref.rs @@ -21,6 +21,18 @@ pub enum TimeUnit { Nanosecond, } +impl TimeUnit { + /// Convert a number of `TimeUnit` to microseconds. + pub fn to_micros(&self, value: i64) -> i64 { + match self { + TimeUnit::Second => value * 1_000_000, + TimeUnit::Millisecond => value * 1000, + TimeUnit::Microsecond => value, + TimeUnit::Nanosecond => value / 1000, + } + } +} + /// A non-owning [static type value](https://duckdb.org/docs/sql/data_types/overview). Typically the /// memory backing this value is owned by SQLite. ///