Skip to content

Commit

Permalink
Merge pull request #26 from SpringQL/feat/add-spring_column-apis
Browse files Browse the repository at this point in the history
feat: add spring_column_{short, long, bool, float}() and SpringErrno::Null
  • Loading branch information
laysakura authored May 9, 2022
2 parents 3ebbb93 + 91b36f6 commit 22c7de6
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 3 deletions.
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.5.0"
version = "0.7.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.21"

[dependencies]
springql-core = "0.5.0"
springql-core = "0.7.0"

log = "0.4"
57 changes: 57 additions & 0 deletions springql.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ typedef enum SpringErrno {
Unavailable = -9,
Sql = -10,
InvalidConfig = -11,
Null = -12,
/**
* Insufficient buffer size
*/
Expand Down Expand Up @@ -140,6 +141,20 @@ SpringRow *spring_pop(const SpringPipeline *pipeline, const char *queue);
*/
enum SpringErrno spring_row_close(SpringRow *row);

/**
* See: springql_core::api::spring_column_i16
*
* # Returns
*
* - `0`: if there are no recent errors.
* - `< 0`: SpringErrno
*
* # Safety
*
* This function is unsafe because it uses raw pointer.
*/
enum SpringErrno spring_column_short(const SpringRow *row, uint16_t i_col, short *out);

/**
* See: springql_core::api::spring_column_i32
*
Expand All @@ -154,6 +169,20 @@ enum SpringErrno spring_row_close(SpringRow *row);
*/
enum SpringErrno spring_column_int(const SpringRow *row, uint16_t i_col, int *out);

/**
* See: springql_core::api::spring_column_i64
*
* # Returns
*
* - `0`: if there are no recent errors.
* - `< 0`: SpringErrno
*
* # Safety
*
* This function is unsafe because it uses raw pointer.
*/
enum SpringErrno spring_column_long(const SpringRow *row, uint16_t i_col, long *out);

/**
* See: springql_core::api::spring_column_text
*
Expand All @@ -171,6 +200,34 @@ enum SpringErrno spring_column_int(const SpringRow *row, uint16_t i_col, int *ou
*/
int spring_column_text(const SpringRow *row, uint16_t i_col, char *out, int out_len);

/**
* See: springql_core::api::spring_column_bool
*
* # Returns
*
* - `0`: if there are no recent errors.
* - `< 0`: SpringErrno
*
* # Safety
*
* This function is unsafe because it uses raw pointer.
*/
enum SpringErrno spring_column_bool(const SpringRow *row, uint16_t i_col, bool *out);

/**
* See: springql_core::api::spring_column_f32
*
* # Returns
*
* - `0`: if there are no recent errors.
* - `< 0`: SpringErrno
*
* # Safety
*
* This function is unsafe because it uses raw pointer.
*/
enum SpringErrno spring_column_float(const SpringRow *row, uint16_t i_col, float *out);

/**
* Write the most recent error number into `errno` and message into a caller-provided buffer as a UTF-8
* string, returning the number of bytes written.
Expand Down
102 changes: 101 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
convert::identity,
ffi::{c_void, CStr},
mem,
os::raw::{c_char, c_int},
os::raw::{c_char, c_float, c_int, c_long, c_short},
panic::{catch_unwind, UnwindSafe},
ptr,
};
Expand Down Expand Up @@ -195,6 +195,31 @@ pub unsafe extern "C" fn spring_row_close(row: *mut SpringRow) -> SpringErrno {
}
}

/// See: springql_core::api::spring_column_i16
///
/// # Returns
///
/// - `0`: if there are no recent errors.
/// - `< 0`: SpringErrno
///
/// # Safety
///
/// This function is unsafe because it uses raw pointer.
#[no_mangle]
pub unsafe extern "C" fn spring_column_short(
row: *const SpringRow,
i_col: u16,
out: *mut c_short,
) -> SpringErrno {
let row = &*((*row).0 as *const springql_core::SpringRow);
let i_col = i_col as usize;

with_catch(|| springql_core::spring_column_i16(row, i_col)).map_or_else(identity, |r| {
*out = r;
SpringErrno::Ok
})
}

/// See: springql_core::api::spring_column_i32
///
/// # Returns
Expand All @@ -220,6 +245,31 @@ pub unsafe extern "C" fn spring_column_int(
})
}

/// See: springql_core::api::spring_column_i64
///
/// # Returns
///
/// - `0`: if there are no recent errors.
/// - `< 0`: SpringErrno
///
/// # Safety
///
/// This function is unsafe because it uses raw pointer.
#[no_mangle]
pub unsafe extern "C" fn spring_column_long(
row: *const SpringRow,
i_col: u16,
out: *mut c_long,
) -> SpringErrno {
let row = &*((*row).0 as *const springql_core::SpringRow);
let i_col = i_col as usize;

with_catch(|| springql_core::spring_column_i64(row, i_col)).map_or_else(identity, |r| {
*out = r;
SpringErrno::Ok
})
}

/// See: springql_core::api::spring_column_text
///
/// This returns UTF-8 string into `out`.
Expand Down Expand Up @@ -247,6 +297,56 @@ pub unsafe extern "C" fn spring_column_text(
.map_or_else(|errno| errno as c_int, |text| strcpy(&text, out, out_len))
}

/// See: springql_core::api::spring_column_bool
///
/// # Returns
///
/// - `0`: if there are no recent errors.
/// - `< 0`: SpringErrno
///
/// # Safety
///
/// This function is unsafe because it uses raw pointer.
#[no_mangle]
pub unsafe extern "C" fn spring_column_bool(
row: *const SpringRow,
i_col: u16,
out: *mut bool,
) -> SpringErrno {
let row = &*((*row).0 as *const springql_core::SpringRow);
let i_col = i_col as usize;

with_catch(|| springql_core::spring_column_bool(row, i_col)).map_or_else(identity, |r| {
*out = r;
SpringErrno::Ok
})
}

/// See: springql_core::api::spring_column_f32
///
/// # Returns
///
/// - `0`: if there are no recent errors.
/// - `< 0`: SpringErrno
///
/// # Safety
///
/// This function is unsafe because it uses raw pointer.
#[no_mangle]
pub unsafe extern "C" fn spring_column_float(
row: *const SpringRow,
i_col: u16,
out: *mut c_float,
) -> SpringErrno {
let row = &*((*row).0 as *const springql_core::SpringRow);
let i_col = i_col as usize;

with_catch(|| springql_core::spring_column_f32(row, i_col)).map_or_else(identity, |r| {
*out = r;
SpringErrno::Ok
})
}

fn with_catch<F, R>(f: F) -> Result<R, SpringErrno>
where
F: FnOnce() -> Result<R, SpringError> + UnwindSafe,
Expand Down
2 changes: 2 additions & 0 deletions src/spring_errno.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub enum SpringErrno {
Unavailable = -9,
Sql = -10,
InvalidConfig = -11,
Null = -12,

/// Insufficient buffer size
CInsufficient = -126,
Expand All @@ -45,6 +46,7 @@ impl From<&SpringError> for SpringErrno {
SpringError::Unavailable { .. } => SpringErrno::Unavailable,
SpringError::Sql(_) => SpringErrno::Sql,
SpringError::InvalidConfig { .. } => SpringErrno::InvalidConfig,
SpringError::Null { .. } => SpringErrno::Null,
}
}
}
Expand Down

0 comments on commit 22c7de6

Please sign in to comment.