Skip to content

Commit

Permalink
Merge pull request #52 from SpringQL/build/v0.15.0
Browse files Browse the repository at this point in the history
build: v0.15.0
  • Loading branch information
laysakura authored Jun 29, 2022
2 parents 0b5188d + 55c2982 commit 602e9e6
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 8 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ Also check the changes in springql-core: <https://github.com/SpringQL/SpringQL/b
<!-- markdownlint-disable MD024 -->
## [Unreleased]

## [v0.15.0] - 2022-06-29

### Added

- Following new APIs: ([#52](https://github.com/SpringQL/SpringQL-client-c/pull/52))
- `spring_source_row_builder`
- `spring_source_row_add_column_blob`
- `spring_source_row_build`

## [v0.14.0] - 2022-06-24

### Changed
Expand Down
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "springql-client-c"
version = "0.14.0"
version = "0.15.0"

authors = ["Sho Nakatani <lay.sakura@gmail.com>"]
license = "MIT OR Apache-2.0"
Expand All @@ -15,6 +15,6 @@ name = "springql_client"
cbindgen = "0.24"

[dependencies]
springql = "0.14.0"
springql = "0.15.0"

log = "0.4"
43 changes: 43 additions & 0 deletions springql.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ typedef void *SpringSinkRow;
*/
typedef void *SpringSourceRow;

/**
* Builder of SpringSourceRow
*/
typedef void *SpringSourceRowBuilder;

/**
* Returns default configuration.
*
Expand Down Expand Up @@ -195,6 +200,44 @@ enum SpringErrno spring_push(const SpringPipeline *pipeline,
*/
SpringSourceRow *spring_source_row_from_json(const char *json);

/**
* Start creating a source row using a builder.
*
* # Returns
*
* Pointer to the builder
*/
SpringSourceRowBuilder *spring_source_row_builder(void);

/**
* Add a BLOB column to the builder.
*
* # Parameters
*
* - `builder`: Pointer to the builder created via spring_source_row_builder().
* - `column_name`: Column name to add.
* - `v`: BLOB value to add. The byte sequence is copied internally.
* - `v_len`: `v`'s length.
*
* # Returns
*
* - `Ok`: on success.
* - `Sql`: `column_name` is already added to the builder.
*/
enum SpringErrno spring_source_row_add_column_blob(SpringSourceRowBuilder *builder,
const char *column_name,
const void *v,
int v_len);

/**
* Finish creating a source row using a builder.
*
* # Returns
*
* SpringSourceRow
*/
SpringSourceRow *spring_source_row_build(SpringSourceRowBuilder *builder);

/**
* Frees heap occupied by a `SpringSourceRow`.
*
Expand Down
62 changes: 61 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ pub mod spring_last_err;
mod spring_pipeline;
mod spring_sink_row;
mod spring_source_row;
mod spring_source_row_builder;

use std::{
ffi::{c_void, CStr},
os::raw::{c_char, c_float, c_int, c_long, c_short, c_uint},
panic::{catch_unwind, UnwindSafe},
ptr,
ptr, slice,
};

use crate::{
Expand All @@ -27,6 +28,7 @@ use crate::{
spring_pipeline::SpringPipeline,
spring_sink_row::SpringSinkRow,
spring_source_row::SpringSourceRow,
spring_source_row_builder::SpringSourceRowBuilder,
};
use ::springql::{error::SpringError, SpringPipeline as Pipeline};

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

/// Start creating a source row using a builder.
///
/// # Returns
///
/// Pointer to the builder
#[no_mangle]
pub unsafe extern "C" fn spring_source_row_builder() -> *mut SpringSourceRowBuilder {
SpringSourceRowBuilder::default().into_ptr()
}
/// Add a BLOB column to the builder.
///
/// # Parameters
///
/// - `builder`: Pointer to the builder created via spring_source_row_builder().
/// - `column_name`: Column name to add.
/// - `v`: BLOB value to add. The byte sequence is copied internally.
/// - `v_len`: `v`'s length.
///
/// # Returns
///
/// - `Ok`: on success.
/// - `Sql`: `column_name` is already added to the builder.
#[no_mangle]
pub unsafe extern "C" fn spring_source_row_add_column_blob(
builder: *mut SpringSourceRowBuilder,
column_name: *const c_char,
v: *const c_void,
v_len: c_int,
) -> SpringErrno {
let column_name = CStr::from_ptr(column_name).to_string_lossy().into_owned();

let v = v as *const u8;
let v = slice::from_raw_parts(v, v_len as usize);
let v = v.to_vec();

let rust_builder = (*builder).to_row_builder();
let res_rust_builder = with_catch(|| rust_builder.add_column(column_name, v));
match res_rust_builder {
Ok(rust_builder) => {
*builder = SpringSourceRowBuilder::new(rust_builder);
SpringErrno::Ok
}
Err(e) => e,
}
}
/// Finish creating a source row using a builder.
///
/// # Returns
///
/// SpringSourceRow
#[no_mangle]
pub unsafe extern "C" fn spring_source_row_build(
builder: *mut SpringSourceRowBuilder,
) -> *mut SpringSourceRow {
let rust_builder = (*builder).to_row_builder();
SpringSourceRow::new(rust_builder.build()).into_ptr()
}

/// Frees heap occupied by a `SpringSourceRow`.
///
/// # Returns
Expand Down
37 changes: 37 additions & 0 deletions src/spring_source_row_builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// 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.

use ::springql::SpringSourceRowBuilder as SourceRowBuilder;

use std::{ffi::c_void, mem};

/// Builder of SpringSourceRow
#[non_exhaustive]
#[repr(transparent)]
pub struct SpringSourceRowBuilder(*mut c_void);

impl SpringSourceRowBuilder {
pub fn new(underlying: SourceRowBuilder) -> Self {
SpringSourceRowBuilder(unsafe { mem::transmute(Box::new(underlying)) })
}

pub fn to_row_builder(&self) -> SourceRowBuilder {
unsafe { &*(self.0 as *const SourceRowBuilder) }.clone()
}

pub fn drop(ptr: *mut SpringSourceRowBuilder) {
let outer = unsafe { Box::from_raw(ptr) };
let inner = unsafe { Box::from_raw(outer.0) };
drop(inner);
drop(outer);
}

pub fn into_ptr(self) -> *mut SpringSourceRowBuilder {
Box::into_raw(Box::new(self))
}
}

impl Default for SpringSourceRowBuilder {
fn default() -> Self {
Self::new(SourceRowBuilder::default())
}
}

0 comments on commit 602e9e6

Please sign in to comment.