Skip to content

Commit 8ed661c

Browse files
committed
refactor(sys): impl taos_fetch_row
1 parent 345f792 commit 8ed661c

File tree

4 files changed

+72
-42
lines changed

4 files changed

+72
-42
lines changed

taos-ws-sys/src/native/error.rs

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ pub unsafe extern "C" fn taos_errno(res: *mut TAOS_RES) -> c_int {
1313
return errno();
1414
}
1515

16-
match (res as *mut MaybeError<()>)
16+
match (res as *mut TaosMaybeError<()>)
1717
.as_ref()
18-
.and_then(MaybeError::errno)
18+
.and_then(TaosMaybeError::errno)
1919
{
2020
Some(errno) => format_errno(errno),
2121
_ => Code::SUCCESS.into(),
@@ -31,9 +31,9 @@ pub unsafe extern "C" fn taos_errstr(res: *mut TAOS_RES) -> *const c_char {
3131
return errstr();
3232
}
3333

34-
match (res as *mut MaybeError<()>)
34+
match (res as *mut TaosMaybeError<()>)
3535
.as_ref()
36-
.and_then(MaybeError::errstr)
36+
.and_then(TaosMaybeError::errstr)
3737
{
3838
Some(err) => err,
3939
_ => EMPTY.as_ptr(),
@@ -48,7 +48,7 @@ thread_local! {
4848
static ERRSTR: RefCell<[u8; MAX_ERRSTR_LEN]> = const { RefCell::new([0; MAX_ERRSTR_LEN]) };
4949
}
5050

51-
pub fn set_err_and_get_code(err: Error) -> i32 {
51+
pub fn set_err_and_get_code(err: TaosError) -> i32 {
5252
ERRNO.with(|errno| {
5353
let code: u32 = err.code.into();
5454
*errno.borrow_mut() = (code | 0x80000000) as i32;
@@ -81,13 +81,13 @@ pub fn format_errno(errno: i32) -> i32 {
8181
}
8282

8383
#[derive(Debug)]
84-
pub struct MaybeError<T> {
85-
err: Option<Error>,
84+
pub struct TaosMaybeError<T> {
85+
err: Option<TaosError>,
8686
data: *mut T,
8787
type_id: &'static str,
8888
}
8989

90-
impl<T> MaybeError<T> {
90+
impl<T> TaosMaybeError<T> {
9191
pub fn errno(&self) -> Option<i32> {
9292
self.err.as_ref().map(|err| err.code.into())
9393
}
@@ -105,7 +105,7 @@ impl<T> MaybeError<T> {
105105
}
106106
}
107107

108-
impl<T> Drop for MaybeError<T> {
108+
impl<T> Drop for TaosMaybeError<T> {
109109
fn drop(&mut self) {
110110
if !self.data.is_null() {
111111
trace!(self.type_id, "drop MaybeError");
@@ -114,7 +114,7 @@ impl<T> Drop for MaybeError<T> {
114114
}
115115
}
116116

117-
impl<T> From<T> for MaybeError<T> {
117+
impl<T> From<T> for TaosMaybeError<T> {
118118
fn from(value: T) -> Self {
119119
Self {
120120
err: None,
@@ -124,7 +124,7 @@ impl<T> From<T> for MaybeError<T> {
124124
}
125125
}
126126

127-
impl<T> From<Box<T>> for MaybeError<T> {
127+
impl<T> From<Box<T>> for TaosMaybeError<T> {
128128
fn from(value: Box<T>) -> Self {
129129
Self {
130130
err: None,
@@ -134,9 +134,9 @@ impl<T> From<Box<T>> for MaybeError<T> {
134134
}
135135
}
136136

137-
impl<T, E> From<Result<T, E>> for MaybeError<T>
137+
impl<T, E> From<Result<T, E>> for TaosMaybeError<T>
138138
where
139-
E: Into<Error>,
139+
E: Into<TaosError>,
140140
{
141141
fn from(value: Result<T, E>) -> Self {
142142
match value {
@@ -154,9 +154,9 @@ where
154154
}
155155
}
156156

157-
impl<T, E> From<Result<Box<T>, E>> for MaybeError<T>
157+
impl<T, E> From<Result<Box<T>, E>> for TaosMaybeError<T>
158158
where
159-
E: Into<Error>,
159+
E: Into<TaosError>,
160160
{
161161
fn from(value: Result<Box<T>, E>) -> Self {
162162
match value {
@@ -175,13 +175,13 @@ where
175175
}
176176

177177
#[derive(Debug)]
178-
pub struct Error {
178+
pub struct TaosError {
179179
code: Code,
180180
message: CString,
181181
source: Option<Box<dyn std::error::Error>>,
182182
}
183183

184-
impl Error {
184+
impl TaosError {
185185
pub fn new(code: Code, message: &str) -> Self {
186186
Self {
187187
code,
@@ -191,8 +191,8 @@ impl Error {
191191
}
192192
}
193193

194-
impl From<&Error> for Error {
195-
fn from(err: &Error) -> Self {
194+
impl From<&TaosError> for TaosError {
195+
fn from(err: &TaosError) -> Self {
196196
Self {
197197
code: err.code,
198198
message: err.message.clone(),
@@ -201,19 +201,19 @@ impl From<&Error> for Error {
201201
}
202202
}
203203

204-
impl std::fmt::Display for Error {
204+
impl std::fmt::Display for TaosError {
205205
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
206206
write!(f, "[{:#06X}] {}", self.code, self.message.to_str().unwrap())
207207
}
208208
}
209209

210-
impl std::error::Error for Error {
210+
impl std::error::Error for TaosError {
211211
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
212212
self.source.as_ref().map(|e| e.as_ref())
213213
}
214214
}
215215

216-
impl From<Box<dyn std::error::Error>> for Error {
216+
impl From<Box<dyn std::error::Error>> for TaosError {
217217
fn from(err: Box<dyn std::error::Error>) -> Self {
218218
Self {
219219
code: Code::FAILED,
@@ -223,7 +223,7 @@ impl From<Box<dyn std::error::Error>> for Error {
223223
}
224224
}
225225

226-
impl From<std::str::Utf8Error> for Error {
226+
impl From<std::str::Utf8Error> for TaosError {
227227
fn from(err: std::str::Utf8Error) -> Self {
228228
Self {
229229
code: Code::FAILED,
@@ -233,7 +233,7 @@ impl From<std::str::Utf8Error> for Error {
233233
}
234234
}
235235

236-
impl From<std::string::FromUtf8Error> for Error {
236+
impl From<std::string::FromUtf8Error> for TaosError {
237237
fn from(err: std::string::FromUtf8Error) -> Self {
238238
Self {
239239
code: Code::FAILED,
@@ -243,7 +243,7 @@ impl From<std::string::FromUtf8Error> for Error {
243243
}
244244
}
245245

246-
impl From<taos_query::DsnError> for Error {
246+
impl From<taos_query::DsnError> for TaosError {
247247
fn from(err: taos_query::DsnError) -> Self {
248248
use taos_ws::query::asyn::WS_ERROR_NO;
249249
Self {
@@ -254,7 +254,7 @@ impl From<taos_query::DsnError> for Error {
254254
}
255255
}
256256

257-
impl From<taos_query::common::SmlDataBuilderError> for Error {
257+
impl From<taos_query::common::SmlDataBuilderError> for TaosError {
258258
fn from(err: taos_query::common::SmlDataBuilderError) -> Self {
259259
Self {
260260
code: Code::FAILED,
@@ -264,7 +264,7 @@ impl From<taos_query::common::SmlDataBuilderError> for Error {
264264
}
265265
}
266266

267-
impl From<taos_ws::Error> for Error {
267+
impl From<taos_ws::Error> for TaosError {
268268
fn from(e: taos_ws::Error) -> Self {
269269
Self {
270270
code: e.errno(),
@@ -274,7 +274,7 @@ impl From<taos_ws::Error> for Error {
274274
}
275275
}
276276

277-
impl From<taos_ws::query::Error> for Error {
277+
impl From<taos_ws::query::Error> for TaosError {
278278
fn from(err: taos_ws::query::Error) -> Self {
279279
Self {
280280
code: err.errno(),
@@ -284,7 +284,7 @@ impl From<taos_ws::query::Error> for Error {
284284
}
285285
}
286286

287-
impl From<taos_error::Error> for Error {
287+
impl From<taos_error::Error> for TaosError {
288288
fn from(err: taos_error::Error) -> Self {
289289
Self {
290290
code: err.code(),
@@ -304,7 +304,7 @@ mod tests {
304304
#[test]
305305
fn test_set_err_and_get_code() {
306306
unsafe {
307-
let err = Error::new(Code::SUCCESS, "test error");
307+
let err = TaosError::new(Code::SUCCESS, "test error");
308308
let code = set_err_and_get_code(err);
309309
assert_eq!(code as u32, 0x80000000);
310310

taos-ws-sys/src/native/mod.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ use std::ptr;
55
use std::sync::RwLock;
66
use std::time::Duration;
77

8-
use error::{set_err_and_get_code, Error};
8+
use error::{set_err_and_get_code, TaosError};
99
use once_cell::sync::Lazy;
1010
use taos_error::Code;
1111
use taos_query::common::{Precision, Ty};
1212
use taos_query::TBuilder;
13+
use taos_ws::query::Error;
1314
use taos_ws::{Offset, Taos, TaosBuilder};
1415
use tracing::trace;
1516

@@ -27,7 +28,7 @@ pub type TAOS = c_void;
2728
#[allow(non_camel_case_types)]
2829
pub type TAOS_RES = c_void;
2930

30-
type TaosResult<T> = Result<T, Error>;
31+
type TaosResult<T> = Result<T, TaosError>;
3132

3233
#[no_mangle]
3334
pub unsafe extern "C" fn taos_connect(
@@ -97,7 +98,7 @@ unsafe fn connect(
9798
#[no_mangle]
9899
pub extern "C" fn taos_close(taos: *mut TAOS) {
99100
if taos.is_null() {
100-
set_err_and_get_code(Error::new(Code::INVALID_PARA, "taos is null"));
101+
set_err_and_get_code(TaosError::new(Code::INVALID_PARA, "taos is null"));
101102
return;
102103
}
103104

@@ -138,7 +139,9 @@ pub unsafe extern "C" fn taos_options(
138139

139140
let key = match CStr::from_ptr(key_ptr).to_str() {
140141
Ok(key) => key,
141-
Err(_) => return set_err_and_get_code(Error::new(Code::INVALID_PARA, "Invalid key")),
142+
Err(_) => {
143+
return set_err_and_get_code(TaosError::new(Code::INVALID_PARA, "Invalid key"))
144+
}
142145
};
143146

144147
let value_ptr: *const c_char = varargs.arg();
@@ -148,7 +151,9 @@ pub unsafe extern "C" fn taos_options(
148151

149152
let value = match CStr::from_ptr(value_ptr).to_str() {
150153
Ok(value) => value,
151-
Err(_) => return set_err_and_get_code(Error::new(Code::INVALID_PARA, "Invalid value")),
154+
Err(_) => {
155+
return set_err_and_get_code(TaosError::new(Code::INVALID_PARA, "Invalid value"))
156+
}
152157
};
153158

154159
params.push(format!("{key}={value}"));

taos-ws-sys/src/native/query/inner.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ use std::time::Duration;
55
use taos_error::Code;
66
use taos_query::common::{Precision, Ty};
77
use taos_query::{Fetchable, Queryable, RawBlock as Block};
8+
use taos_ws::query::Error;
89
use taos_ws::{Offset, Taos};
910
use tracing::trace;
1011

11-
use crate::native::error::Error;
12+
use crate::native::error::TaosError;
1213
use crate::native::query::{TAOS_FIELD, TAOS_ROW};
1314
use crate::native::{ResultSet, ResultSetOperations, TaosResult, TAOS};
1415

@@ -155,7 +156,7 @@ impl ResultSetOperations for QueryResultSet {
155156
pub unsafe fn query(taos: *mut TAOS, sql: *const c_char, req_id: u64) -> TaosResult<ResultSet> {
156157
let client = (taos as *mut Taos)
157158
.as_mut()
158-
.ok_or(Error::new(Code::INVALID_PARA, "client pointer is null"))?;
159+
.ok_or(TaosError::new(Code::INVALID_PARA, "client pointer is null"))?;
159160
let sql = CStr::from_ptr(sql).to_str()?;
160161
let rs = client.query_with_req_id(sql, req_id)?;
161162
Ok(ResultSet::Query(QueryResultSet::new(rs)))

taos-ws-sys/src/native/query/mod.rs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
use std::ffi::{c_char, c_int, c_void, CStr};
22
use std::ptr;
33

4+
use taos_error::Code;
45
use taos_query::common::Field;
56
use taos_query::util::generate_req_id;
67
use tracing::trace;
78

8-
use crate::native::error::MaybeError;
9-
use crate::native::{ResultSet, TAOS, TAOS_RES};
9+
use crate::native::error::{set_err_and_get_code, TaosError, TaosMaybeError};
10+
use crate::native::{ResultSet, ResultSetOperations, TAOS, TAOS_RES};
1011

1112
mod inner;
1213

@@ -59,14 +60,37 @@ pub unsafe extern "C" fn taos_query_with_reqid(
5960
reqId: i64,
6061
) -> *mut TAOS_RES {
6162
trace!(taos=?taos, req_id=reqId,"query sql={:?}", CStr::from_ptr(sql));
62-
let res: MaybeError<ResultSet> = inner::query(taos, sql, reqId as u64).into();
63+
let res: TaosMaybeError<ResultSet> = inner::query(taos, sql, reqId as u64).into();
6364
trace!(res=?res, "query done");
6465
Box::into_raw(Box::new(res)) as _
6566
}
6667

6768
#[no_mangle]
68-
pub extern "C" fn taos_fetch_row(res: *mut TAOS_RES) -> TAOS_ROW {
69-
todo!()
69+
pub unsafe extern "C" fn taos_fetch_row(res: *mut TAOS_RES) -> TAOS_ROW {
70+
trace!(res=?res, "fetch row");
71+
72+
fn handle_error(code: Code, msg: &str) -> TAOS_ROW {
73+
set_err_and_get_code(TaosError::new(code, msg));
74+
ptr::null_mut()
75+
}
76+
77+
let rs = match (res as *mut TaosMaybeError<ResultSet>).as_mut() {
78+
Some(rs) => rs,
79+
None => return handle_error(Code::INVALID_PARA, "Res is null"),
80+
};
81+
82+
let rs = match rs.deref_mut() {
83+
Some(rs) => rs,
84+
None => return handle_error(Code::INVALID_PARA, "Data is null"),
85+
};
86+
87+
match rs.fetch_row() {
88+
Ok(row) => {
89+
trace!(row=?row, "fetch row done");
90+
row
91+
}
92+
Err(err) => handle_error(err.errno(), &err.errstr()),
93+
}
7094
}
7195

7296
#[no_mangle]

0 commit comments

Comments
 (0)