Skip to content

Commit 91b36f6

Browse files
committed
feat: add spring_column_{short, long, bool, float}()
1 parent 7886ad7 commit 91b36f6

2 files changed

Lines changed: 158 additions & 1 deletion

File tree

springql.h

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ typedef enum SpringErrno {
2626
Unavailable = -9,
2727
Sql = -10,
2828
InvalidConfig = -11,
29+
Null = -12,
2930
/**
3031
* Insufficient buffer size
3132
*/
@@ -140,6 +141,20 @@ SpringRow *spring_pop(const SpringPipeline *pipeline, const char *queue);
140141
*/
141142
enum SpringErrno spring_row_close(SpringRow *row);
142143

144+
/**
145+
* See: springql_core::api::spring_column_i16
146+
*
147+
* # Returns
148+
*
149+
* - `0`: if there are no recent errors.
150+
* - `< 0`: SpringErrno
151+
*
152+
* # Safety
153+
*
154+
* This function is unsafe because it uses raw pointer.
155+
*/
156+
enum SpringErrno spring_column_short(const SpringRow *row, uint16_t i_col, short *out);
157+
143158
/**
144159
* See: springql_core::api::spring_column_i32
145160
*
@@ -154,6 +169,20 @@ enum SpringErrno spring_row_close(SpringRow *row);
154169
*/
155170
enum SpringErrno spring_column_int(const SpringRow *row, uint16_t i_col, int *out);
156171

172+
/**
173+
* See: springql_core::api::spring_column_i64
174+
*
175+
* # Returns
176+
*
177+
* - `0`: if there are no recent errors.
178+
* - `< 0`: SpringErrno
179+
*
180+
* # Safety
181+
*
182+
* This function is unsafe because it uses raw pointer.
183+
*/
184+
enum SpringErrno spring_column_long(const SpringRow *row, uint16_t i_col, long *out);
185+
157186
/**
158187
* See: springql_core::api::spring_column_text
159188
*
@@ -171,6 +200,34 @@ enum SpringErrno spring_column_int(const SpringRow *row, uint16_t i_col, int *ou
171200
*/
172201
int spring_column_text(const SpringRow *row, uint16_t i_col, char *out, int out_len);
173202

203+
/**
204+
* See: springql_core::api::spring_column_bool
205+
*
206+
* # Returns
207+
*
208+
* - `0`: if there are no recent errors.
209+
* - `< 0`: SpringErrno
210+
*
211+
* # Safety
212+
*
213+
* This function is unsafe because it uses raw pointer.
214+
*/
215+
enum SpringErrno spring_column_bool(const SpringRow *row, uint16_t i_col, bool *out);
216+
217+
/**
218+
* See: springql_core::api::spring_column_f32
219+
*
220+
* # Returns
221+
*
222+
* - `0`: if there are no recent errors.
223+
* - `< 0`: SpringErrno
224+
*
225+
* # Safety
226+
*
227+
* This function is unsafe because it uses raw pointer.
228+
*/
229+
enum SpringErrno spring_column_float(const SpringRow *row, uint16_t i_col, float *out);
230+
174231
/**
175232
* Write the most recent error number into `errno` and message into a caller-provided buffer as a UTF-8
176233
* string, returning the number of bytes written.

src/lib.rs

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::{
66
convert::identity,
77
ffi::{c_void, CStr},
88
mem,
9-
os::raw::{c_char, c_int},
9+
os::raw::{c_char, c_float, c_int, c_long, c_short},
1010
panic::{catch_unwind, UnwindSafe},
1111
ptr,
1212
};
@@ -195,6 +195,31 @@ pub unsafe extern "C" fn spring_row_close(row: *mut SpringRow) -> SpringErrno {
195195
}
196196
}
197197

198+
/// See: springql_core::api::spring_column_i16
199+
///
200+
/// # Returns
201+
///
202+
/// - `0`: if there are no recent errors.
203+
/// - `< 0`: SpringErrno
204+
///
205+
/// # Safety
206+
///
207+
/// This function is unsafe because it uses raw pointer.
208+
#[no_mangle]
209+
pub unsafe extern "C" fn spring_column_short(
210+
row: *const SpringRow,
211+
i_col: u16,
212+
out: *mut c_short,
213+
) -> SpringErrno {
214+
let row = &*((*row).0 as *const springql_core::SpringRow);
215+
let i_col = i_col as usize;
216+
217+
with_catch(|| springql_core::spring_column_i16(row, i_col)).map_or_else(identity, |r| {
218+
*out = r;
219+
SpringErrno::Ok
220+
})
221+
}
222+
198223
/// See: springql_core::api::spring_column_i32
199224
///
200225
/// # Returns
@@ -220,6 +245,31 @@ pub unsafe extern "C" fn spring_column_int(
220245
})
221246
}
222247

248+
/// See: springql_core::api::spring_column_i64
249+
///
250+
/// # Returns
251+
///
252+
/// - `0`: if there are no recent errors.
253+
/// - `< 0`: SpringErrno
254+
///
255+
/// # Safety
256+
///
257+
/// This function is unsafe because it uses raw pointer.
258+
#[no_mangle]
259+
pub unsafe extern "C" fn spring_column_long(
260+
row: *const SpringRow,
261+
i_col: u16,
262+
out: *mut c_long,
263+
) -> SpringErrno {
264+
let row = &*((*row).0 as *const springql_core::SpringRow);
265+
let i_col = i_col as usize;
266+
267+
with_catch(|| springql_core::spring_column_i64(row, i_col)).map_or_else(identity, |r| {
268+
*out = r;
269+
SpringErrno::Ok
270+
})
271+
}
272+
223273
/// See: springql_core::api::spring_column_text
224274
///
225275
/// This returns UTF-8 string into `out`.
@@ -247,6 +297,56 @@ pub unsafe extern "C" fn spring_column_text(
247297
.map_or_else(|errno| errno as c_int, |text| strcpy(&text, out, out_len))
248298
}
249299

300+
/// See: springql_core::api::spring_column_bool
301+
///
302+
/// # Returns
303+
///
304+
/// - `0`: if there are no recent errors.
305+
/// - `< 0`: SpringErrno
306+
///
307+
/// # Safety
308+
///
309+
/// This function is unsafe because it uses raw pointer.
310+
#[no_mangle]
311+
pub unsafe extern "C" fn spring_column_bool(
312+
row: *const SpringRow,
313+
i_col: u16,
314+
out: *mut bool,
315+
) -> SpringErrno {
316+
let row = &*((*row).0 as *const springql_core::SpringRow);
317+
let i_col = i_col as usize;
318+
319+
with_catch(|| springql_core::spring_column_bool(row, i_col)).map_or_else(identity, |r| {
320+
*out = r;
321+
SpringErrno::Ok
322+
})
323+
}
324+
325+
/// See: springql_core::api::spring_column_f32
326+
///
327+
/// # Returns
328+
///
329+
/// - `0`: if there are no recent errors.
330+
/// - `< 0`: SpringErrno
331+
///
332+
/// # Safety
333+
///
334+
/// This function is unsafe because it uses raw pointer.
335+
#[no_mangle]
336+
pub unsafe extern "C" fn spring_column_float(
337+
row: *const SpringRow,
338+
i_col: u16,
339+
out: *mut c_float,
340+
) -> SpringErrno {
341+
let row = &*((*row).0 as *const springql_core::SpringRow);
342+
let i_col = i_col as usize;
343+
344+
with_catch(|| springql_core::spring_column_f32(row, i_col)).map_or_else(identity, |r| {
345+
*out = r;
346+
SpringErrno::Ok
347+
})
348+
}
349+
250350
fn with_catch<F, R>(f: F) -> Result<R, SpringErrno>
251351
where
252352
F: FnOnce() -> Result<R, SpringError> + UnwindSafe,

0 commit comments

Comments
 (0)