Skip to content

Commit 602e9e6

Browse files
authored
Merge pull request #52 from SpringQL/build/v0.15.0
build: v0.15.0
2 parents 0b5188d + 55c2982 commit 602e9e6

File tree

6 files changed

+157
-8
lines changed

6 files changed

+157
-8
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ Also check the changes in springql-core: <https://github.com/SpringQL/SpringQL/b
1313
<!-- markdownlint-disable MD024 -->
1414
## [Unreleased]
1515

16+
## [v0.15.0] - 2022-06-29
17+
18+
### Added
19+
20+
- Following new APIs: ([#52](https://github.com/SpringQL/SpringQL-client-c/pull/52))
21+
- `spring_source_row_builder`
22+
- `spring_source_row_add_column_blob`
23+
- `spring_source_row_build`
24+
1625
## [v0.14.0] - 2022-06-24
1726

1827
### Changed

Cargo.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "springql-client-c"
3-
version = "0.14.0"
3+
version = "0.15.0"
44

55
authors = ["Sho Nakatani <[email protected]>"]
66
license = "MIT OR Apache-2.0"
@@ -15,6 +15,6 @@ name = "springql_client"
1515
cbindgen = "0.24"
1616

1717
[dependencies]
18-
springql = "0.14.0"
18+
springql = "0.15.0"
1919

2020
log = "0.4"

springql.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ typedef void *SpringSinkRow;
5959
*/
6060
typedef void *SpringSourceRow;
6161

62+
/**
63+
* Builder of SpringSourceRow
64+
*/
65+
typedef void *SpringSourceRowBuilder;
66+
6267
/**
6368
* Returns default configuration.
6469
*
@@ -195,6 +200,44 @@ enum SpringErrno spring_push(const SpringPipeline *pipeline,
195200
*/
196201
SpringSourceRow *spring_source_row_from_json(const char *json);
197202

203+
/**
204+
* Start creating a source row using a builder.
205+
*
206+
* # Returns
207+
*
208+
* Pointer to the builder
209+
*/
210+
SpringSourceRowBuilder *spring_source_row_builder(void);
211+
212+
/**
213+
* Add a BLOB column to the builder.
214+
*
215+
* # Parameters
216+
*
217+
* - `builder`: Pointer to the builder created via spring_source_row_builder().
218+
* - `column_name`: Column name to add.
219+
* - `v`: BLOB value to add. The byte sequence is copied internally.
220+
* - `v_len`: `v`'s length.
221+
*
222+
* # Returns
223+
*
224+
* - `Ok`: on success.
225+
* - `Sql`: `column_name` is already added to the builder.
226+
*/
227+
enum SpringErrno spring_source_row_add_column_blob(SpringSourceRowBuilder *builder,
228+
const char *column_name,
229+
const void *v,
230+
int v_len);
231+
232+
/**
233+
* Finish creating a source row using a builder.
234+
*
235+
* # Returns
236+
*
237+
* SpringSourceRow
238+
*/
239+
SpringSourceRow *spring_source_row_build(SpringSourceRowBuilder *builder);
240+
198241
/**
199242
* Frees heap occupied by a `SpringSourceRow`.
200243
*

src/lib.rs

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ pub mod spring_last_err;
1111
mod spring_pipeline;
1212
mod spring_sink_row;
1313
mod spring_source_row;
14+
mod spring_source_row_builder;
1415

1516
use std::{
1617
ffi::{c_void, CStr},
1718
os::raw::{c_char, c_float, c_int, c_long, c_short, c_uint},
1819
panic::{catch_unwind, UnwindSafe},
19-
ptr,
20+
ptr, slice,
2021
};
2122

2223
use crate::{
@@ -27,6 +28,7 @@ use crate::{
2728
spring_pipeline::SpringPipeline,
2829
spring_sink_row::SpringSinkRow,
2930
spring_source_row::SpringSourceRow,
31+
spring_source_row_builder::SpringSourceRowBuilder,
3032
};
3133
use ::springql::{error::SpringError, SpringPipeline as Pipeline};
3234

@@ -256,6 +258,64 @@ pub unsafe extern "C" fn spring_source_row_from_json(json: *const c_char) -> *mu
256258
}
257259
}
258260

261+
/// Start creating a source row using a builder.
262+
///
263+
/// # Returns
264+
///
265+
/// Pointer to the builder
266+
#[no_mangle]
267+
pub unsafe extern "C" fn spring_source_row_builder() -> *mut SpringSourceRowBuilder {
268+
SpringSourceRowBuilder::default().into_ptr()
269+
}
270+
/// Add a BLOB column to the builder.
271+
///
272+
/// # Parameters
273+
///
274+
/// - `builder`: Pointer to the builder created via spring_source_row_builder().
275+
/// - `column_name`: Column name to add.
276+
/// - `v`: BLOB value to add. The byte sequence is copied internally.
277+
/// - `v_len`: `v`'s length.
278+
///
279+
/// # Returns
280+
///
281+
/// - `Ok`: on success.
282+
/// - `Sql`: `column_name` is already added to the builder.
283+
#[no_mangle]
284+
pub unsafe extern "C" fn spring_source_row_add_column_blob(
285+
builder: *mut SpringSourceRowBuilder,
286+
column_name: *const c_char,
287+
v: *const c_void,
288+
v_len: c_int,
289+
) -> SpringErrno {
290+
let column_name = CStr::from_ptr(column_name).to_string_lossy().into_owned();
291+
292+
let v = v as *const u8;
293+
let v = slice::from_raw_parts(v, v_len as usize);
294+
let v = v.to_vec();
295+
296+
let rust_builder = (*builder).to_row_builder();
297+
let res_rust_builder = with_catch(|| rust_builder.add_column(column_name, v));
298+
match res_rust_builder {
299+
Ok(rust_builder) => {
300+
*builder = SpringSourceRowBuilder::new(rust_builder);
301+
SpringErrno::Ok
302+
}
303+
Err(e) => e,
304+
}
305+
}
306+
/// Finish creating a source row using a builder.
307+
///
308+
/// # Returns
309+
///
310+
/// SpringSourceRow
311+
#[no_mangle]
312+
pub unsafe extern "C" fn spring_source_row_build(
313+
builder: *mut SpringSourceRowBuilder,
314+
) -> *mut SpringSourceRow {
315+
let rust_builder = (*builder).to_row_builder();
316+
SpringSourceRow::new(rust_builder.build()).into_ptr()
317+
}
318+
259319
/// Frees heap occupied by a `SpringSourceRow`.
260320
///
261321
/// # Returns

src/spring_source_row_builder.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// This file is part of https://github.com/SpringQL/SpringQL-client-c which is licensed under MIT OR Apache-2.0. See file LICENSE-MIT or LICENSE-APACHE for full license details.
2+
3+
use ::springql::SpringSourceRowBuilder as SourceRowBuilder;
4+
5+
use std::{ffi::c_void, mem};
6+
7+
/// Builder of SpringSourceRow
8+
#[non_exhaustive]
9+
#[repr(transparent)]
10+
pub struct SpringSourceRowBuilder(*mut c_void);
11+
12+
impl SpringSourceRowBuilder {
13+
pub fn new(underlying: SourceRowBuilder) -> Self {
14+
SpringSourceRowBuilder(unsafe { mem::transmute(Box::new(underlying)) })
15+
}
16+
17+
pub fn to_row_builder(&self) -> SourceRowBuilder {
18+
unsafe { &*(self.0 as *const SourceRowBuilder) }.clone()
19+
}
20+
21+
pub fn drop(ptr: *mut SpringSourceRowBuilder) {
22+
let outer = unsafe { Box::from_raw(ptr) };
23+
let inner = unsafe { Box::from_raw(outer.0) };
24+
drop(inner);
25+
drop(outer);
26+
}
27+
28+
pub fn into_ptr(self) -> *mut SpringSourceRowBuilder {
29+
Box::into_raw(Box::new(self))
30+
}
31+
}
32+
33+
impl Default for SpringSourceRowBuilder {
34+
fn default() -> Self {
35+
Self::new(SourceRowBuilder::default())
36+
}
37+
}

0 commit comments

Comments
 (0)