Skip to content

Commit df3aeca

Browse files
authored
Merge pull request #14 from wangfenjin/bind_varchar
bind varchar with length
2 parents 3a0d4e7 + cef1bf9 commit df3aeca

File tree

9 files changed

+32
-30
lines changed

9 files changed

+32
-30
lines changed

.github/workflows/rust.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ jobs:
3636
targets: ${{ matrix.target }}
3737
components: 'rustfmt, clippy'
3838
- run: cargo fmt --all -- --check
39-
- run: cargo clippy --all-targets --workspace --features bundled
39+
- run: cargo clippy --all-targets --workspace --features bundled -- -D warnings -A clippy::redundant-closure
4040
- name: Run cargo-tarpaulin
4141
uses: actions-rs/[email protected]
4242
with:

examples/appender.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,34 @@
11
extern crate duckdb;
22
use std::convert::TryFrom;
33

4+
use duckdb::DropBehavior;
45
use duckdb::{params, Connection, Result};
56

67
fn main() -> Result<()> {
7-
let db = Connection::open_in_memory()?;
8+
//let mut db = Connection::open("10m.db")?;
9+
let mut db = Connection::open_in_memory()?;
810

911
let create_table_sql = "
1012
create table IF NOT EXISTS test
1113
(
12-
id INTEGER not null primary key,
14+
id INTEGER not null, -- primary key,
1315
area CHAR(6),
1416
age TINYINT not null,
1517
active TINYINT not null
1618
);";
1719
db.execute_batch(create_table_sql)?;
1820

21+
let row_count = 10_000_000;
1922
{
20-
let mut app = db.appender("test")?;
23+
let mut tx = db.transaction()?;
24+
tx.set_drop_behavior(DropBehavior::Commit);
25+
let mut app = tx.appender("test")?;
2126
// use generator
2227
// for u in firstn(1_000_000) {
2328
// app.append_row(params![u.id, u.area, u.age, u.active])?;
2429
// }
2530

26-
for i in 0..1_000_000 {
31+
for i in 0..row_count {
2732
app.append_row(params![
2833
i,
2934
get_random_area_code(),
@@ -34,7 +39,7 @@ fn main() -> Result<()> {
3439
}
3540

3641
let val = db.query_row("SELECT count(1) FROM test", [], |row| <(u32,)>::try_from(row))?;
37-
assert_eq!(val, (1_000_000,));
42+
assert_eq!(val, (row_count,));
3843
Ok(())
3944
}
4045

src/appender.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use super::ffi;
22
use super::{AppenderParams, Connection, Result, ValueRef};
3-
use std::ffi::{c_void, CString};
3+
use std::ffi::c_void;
44
use std::fmt;
55
use std::iter::IntoIterator;
6+
use std::os::raw::c_char;
67

78
use crate::error::result_from_duckdb_code;
89
use crate::types::{ToSql, ToSqlOutput};
@@ -60,9 +61,9 @@ impl Appender<'_> {
6061
/// Will return `Err` if append column count not the same with the table schema
6162
#[inline]
6263
pub fn append_row<P: AppenderParams>(&mut self, params: P) -> Result<()> {
63-
let rc = unsafe { ffi::duckdb_appender_begin_row(self.app) };
64-
result_from_duckdb_code(rc, None)?;
64+
let _ = unsafe { ffi::duckdb_appender_begin_row(self.app) };
6565
params.__bind_in(self)?;
66+
// NOTE: we only check end_row return value
6667
let rc = unsafe { ffi::duckdb_appender_end_row(self.app) };
6768
result_from_duckdb_code(rc, None)
6869
}
@@ -87,8 +88,10 @@ impl Appender<'_> {
8788
ToSqlOutput::Borrowed(v) => v,
8889
ToSqlOutput::Owned(ref v) => ValueRef::from(v),
8990
};
91+
// NOTE: we ignore the return value here
92+
// because if anything failed, end_row will fail
9093
// TODO: append more
91-
let rc = match value {
94+
let _ = match value {
9295
ValueRef::Null => unsafe { ffi::duckdb_append_null(ptr) },
9396
ValueRef::Boolean(i) => unsafe { ffi::duckdb_append_bool(ptr, i) },
9497
ValueRef::TinyInt(i) => unsafe { ffi::duckdb_append_int8(ptr, i) },
@@ -102,13 +105,11 @@ impl Appender<'_> {
102105
ValueRef::Float(r) => unsafe { ffi::duckdb_append_float(ptr, r) },
103106
ValueRef::Double(r) => unsafe { ffi::duckdb_append_double(ptr, r) },
104107
ValueRef::Text(s) => unsafe {
105-
let c_str = CString::new(s).expect("can't convert into c_str");
106-
ffi::duckdb_append_varchar(ptr, c_str.as_ptr())
108+
ffi::duckdb_append_varchar_length(ptr, s.as_ptr() as *const c_char, s.len() as u64)
107109
},
108110
ValueRef::Blob(b) => unsafe { ffi::duckdb_append_blob(ptr, b.as_ptr() as *const c_void, b.len() as u64) },
109111
_ => unreachable!("not supported"),
110112
};
111-
result_from_duckdb_code(rc, Some("append error".to_owned()))?;
112113
Ok(())
113114
}
114115

src/column.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ impl Statement<'_> {
107107
for i in 0..n {
108108
// Note: `column_name` is only fallible if `i` is out of bounds,
109109
// which we've already checked.
110-
if name.eq_ignore_ascii_case(&self.stmt.column_name(i).unwrap()) {
110+
if name.eq_ignore_ascii_case(self.stmt.column_name(i).unwrap()) {
111111
return Ok(i);
112112
}
113113
}

src/error.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,13 +210,11 @@ impl error::Error for Error {
210210

211211
// These are public but not re-exported by lib.rs, so only visible within crate.
212212

213-
#[cold]
214213
#[inline]
215214
fn error_from_duckdb_code(code: c_uint, message: Option<String>) -> Result<()> {
216215
Err(Error::DuckDBFailure(ffi::Error::new(code as u32), message))
217216
}
218217

219-
#[cold]
220218
#[inline]
221219
pub fn result_from_duckdb_code(code: c_uint, message: Option<String>) -> Result<()> {
222220
if code == ffi::DuckDBSuccess {

src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,10 @@ pub enum DatabaseName<'a> {
169169

170170
impl<'a> fmt::Display for DatabaseName<'a> {
171171
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
172-
match self {
173-
&DatabaseName::Main => write!(f, "main"),
174-
&DatabaseName::Temp => write!(f, "temp"),
175-
&DatabaseName::Attached(s) => write!(f, "{}", s),
172+
match *self {
173+
DatabaseName::Main => write!(f, "main"),
174+
DatabaseName::Temp => write!(f, "temp"),
175+
DatabaseName::Attached(s) => write!(f, "{}", s),
176176
}
177177
}
178178
}
@@ -418,7 +418,7 @@ impl Connection {
418418
.query(params)?
419419
.get_expected_row()
420420
.map_err(E::from)
421-
.and_then(|r| f(&r))
421+
.and_then(|r| f(r))
422422
}
423423

424424
/// Prepare a SQL statement for execution.

src/pragma.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,7 @@ impl Connection {
169169
let mut stmt = self.prepare(&query)?;
170170
let mut rows = stmt.query([])?;
171171
while let Some(result_row) = rows.next()? {
172-
let row = result_row;
173-
f(&row)?;
172+
f(result_row)?;
174173
}
175174
Ok(())
176175
}
@@ -206,7 +205,7 @@ impl Connection {
206205
let mut rows = stmt.query([])?;
207206
while let Some(result_row) = rows.next()? {
208207
let row = result_row;
209-
f(&row)?;
208+
f(row)?;
210209
}
211210
Ok(())
212211
}

src/row.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ where
168168
self.rows
169169
.next()
170170
.transpose()
171-
.map(|row_result| row_result.and_then(|row| (map)(&row)))
171+
.map(|row_result| row_result.and_then(|row| (map)(row)))
172172
}
173173
}
174174

@@ -193,7 +193,7 @@ where
193193
self.rows
194194
.next()
195195
.transpose()
196-
.map(|row_result| row_result.map_err(E::from).and_then(|row| (map)(&row)))
196+
.map(|row_result| row_result.map_err(E::from).and_then(|row| (map)(row)))
197197
}
198198
}
199199

src/statement.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::ffi::c_void;
2-
use std::ffi::CString;
32
use std::iter::IntoIterator;
3+
use std::os::raw::c_char;
44
use std::{convert, fmt, str};
55

66
use super::ffi;
@@ -279,7 +279,7 @@ impl Statement<'_> {
279279
P: Params,
280280
F: FnOnce(&Row<'_>) -> Result<T>,
281281
{
282-
self.query(params)?.get_expected_row().and_then(|r| f(&r))
282+
self.query(params)?.get_expected_row().and_then(|r| f(r))
283283
}
284284

285285
/// Return the row count
@@ -426,8 +426,7 @@ impl Statement<'_> {
426426
ValueRef::Float(r) => unsafe { ffi::duckdb_bind_float(ptr, col as u64, r) },
427427
ValueRef::Double(r) => unsafe { ffi::duckdb_bind_double(ptr, col as u64, r) },
428428
ValueRef::Text(s) => unsafe {
429-
let c_str = CString::new(s).expect("can't convert into c_str");
430-
ffi::duckdb_bind_varchar(ptr, col as u64, c_str.as_ptr())
429+
ffi::duckdb_bind_varchar_length(ptr, col as u64, s.as_ptr() as *const c_char, s.len() as u64)
431430
},
432431
ValueRef::Blob(b) => unsafe {
433432
ffi::duckdb_bind_blob(ptr, col as u64, b.as_ptr() as *const c_void, b.len() as u64)

0 commit comments

Comments
 (0)