forked from duckdb/duckdb-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappender.rs
222 lines (195 loc) · 6.51 KB
/
appender.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
use super::ffi;
use super::{AppenderParams, Connection, Result, ValueRef};
use std::ffi::c_void;
use std::fmt;
use std::iter::IntoIterator;
use std::os::raw::c_char;
use crate::error::result_from_duckdb_appender;
use crate::types::{ToSql, ToSqlOutput};
use crate::Error;
/// Appender for fast import data
pub struct Appender<'conn> {
conn: &'conn Connection,
app: ffi::duckdb_appender,
}
impl Appender<'_> {
/// Append multiple rows from Iterator
///
/// ## Example
///
/// ```rust,no_run
/// # use duckdb::{Connection, Result, params};
/// fn insert_rows(conn: &Connection) -> Result<()> {
/// let mut app = conn.appender("foo")?;
/// app.append_rows([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]])?;
/// Ok(())
/// }
/// ```
///
/// # Failure
///
/// Will return `Err` if append column count not the same with the table schema
#[inline]
pub fn append_rows<P, I>(&mut self, rows: I) -> Result<()>
where
I: IntoIterator<Item = P>,
P: AppenderParams,
{
for row in rows {
self.append_row(row)?;
}
Ok(())
}
/// Append one row
///
/// ## Example
///
/// ```rust,no_run
/// # use duckdb::{Connection, Result, params};
/// fn insert_row(conn: &Connection) -> Result<()> {
/// let mut app = conn.appender("foo")?;
/// app.append_row([1, 2])?;
/// Ok(())
/// }
/// ```
///
/// # Failure
///
/// Will return `Err` if append column count not the same with the table schema
#[inline]
pub fn append_row<P: AppenderParams>(&mut self, params: P) -> Result<()> {
let _ = unsafe { ffi::duckdb_appender_begin_row(self.app) };
params.__bind_in(self)?;
// NOTE: we only check end_row return value
let rc = unsafe { ffi::duckdb_appender_end_row(self.app) };
result_from_duckdb_appender(rc, self.app)
}
#[inline]
pub(crate) fn bind_parameters<P>(&mut self, params: P) -> Result<()>
where
P: IntoIterator,
P::Item: ToSql,
{
for p in params.into_iter() {
self.bind_parameter(&p)?;
}
Ok(())
}
fn bind_parameter<P: ?Sized + ToSql>(&self, param: &P) -> Result<()> {
let value = param.to_sql()?;
let ptr = self.app;
let value = match value {
ToSqlOutput::Borrowed(v) => v,
ToSqlOutput::Owned(ref v) => ValueRef::from(v),
};
// NOTE: we ignore the return value here
// because if anything failed, end_row will fail
// TODO: append more
let rc = match value {
ValueRef::Null => unsafe { ffi::duckdb_append_null(ptr) },
ValueRef::Boolean(i) => unsafe { ffi::duckdb_append_bool(ptr, i) },
ValueRef::TinyInt(i) => unsafe { ffi::duckdb_append_int8(ptr, i) },
ValueRef::SmallInt(i) => unsafe { ffi::duckdb_append_int16(ptr, i) },
ValueRef::Int(i) => unsafe { ffi::duckdb_append_int32(ptr, i) },
ValueRef::BigInt(i) => unsafe { ffi::duckdb_append_int64(ptr, i) },
// FIXME
ValueRef::HugeInt(i) => unsafe { ffi::duckdb_append_int64(ptr, i as i64) },
ValueRef::Float(r) => unsafe { ffi::duckdb_append_float(ptr, r) },
ValueRef::Double(r) => unsafe { ffi::duckdb_append_double(ptr, r) },
ValueRef::Text(s) => unsafe {
ffi::duckdb_append_varchar_length(ptr, s.as_ptr() as *const c_char, s.len() as u64)
},
ValueRef::Blob(b) => unsafe { ffi::duckdb_append_blob(ptr, b.as_ptr() as *const c_void, b.len() as u64) },
_ => unreachable!("not supported"),
};
if rc != 0 {
return Err(Error::AppendError);
}
Ok(())
}
#[inline]
pub(super) fn new(conn: &Connection, app: ffi::duckdb_appender) -> Appender<'_> {
Appender { conn, app }
}
/// Flush data into DB
#[inline]
pub fn flush(&mut self) {
unsafe {
ffi::duckdb_appender_flush(self.app);
}
}
}
impl Drop for Appender<'_> {
fn drop(&mut self) {
if !self.app.is_null() {
self.flush();
unsafe {
ffi::duckdb_appender_close(self.app);
ffi::duckdb_appender_destroy(&mut self.app);
}
}
}
}
impl fmt::Debug for Appender<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Appender").field("conn", self.conn).finish()
}
}
#[cfg(test)]
mod test {
use crate::{Connection, Result};
use std::convert::TryFrom;
#[test]
fn test_append_one_row() -> Result<()> {
let db = Connection::open_in_memory()?;
db.execute_batch("CREATE TABLE foo(x INTEGER)")?;
{
let mut app = db.appender("foo")?;
app.append_row([42])?;
}
let val = db.query_row("SELECT x FROM foo", [], |row| <(i32,)>::try_from(row))?;
assert_eq!(val, (42,));
Ok(())
}
#[test]
fn test_append_rows() -> Result<()> {
let db = Connection::open_in_memory()?;
db.execute_batch("CREATE TABLE foo(x INTEGER, y INTEGER)")?;
{
let mut app = db.appender("foo")?;
app.append_rows([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]])?;
}
let val = db.query_row("SELECT sum(x), sum(y) FROM foo", [], |row| <(i32, i32)>::try_from(row))?;
assert_eq!(val, (25, 30));
Ok(())
}
// Waiting https://github.com/duckdb/duckdb/pull/3405
#[cfg(feature = "uuid")]
#[test]
#[ignore = "not supported for now"]
fn test_append_uuid() -> Result<()> {
use uuid::Uuid;
let db = Connection::open_in_memory()?;
db.execute_batch("CREATE TABLE foo(x UUID)")?;
let id = Uuid::new_v4();
{
let mut app = db.appender("foo")?;
app.append_row([id])?;
}
let val = db.query_row("SELECT x FROM foo", [], |row| <(Uuid,)>::try_from(row))?;
assert_eq!(val, (id,));
Ok(())
}
#[test]
fn test_append_string_as_ts_row() -> Result<()> {
let db = Connection::open_in_memory()?;
db.execute_batch("CREATE TABLE foo(x TIMESTAMP)")?;
{
let mut app = db.appender("foo")?;
app.append_row(["2022-04-09 15:56:37.544"])?;
}
let val = db.query_row("SELECT x FROM foo", [], |row| <(String,)>::try_from(row))?;
assert_eq!(val, ("2022-04-09 15:56:37.544".to_string(),));
Ok(())
}
}