Skip to content

Commit

Permalink
refactor(sys): impl taos_options
Browse files Browse the repository at this point in the history
  • Loading branch information
qevolg committed Dec 29, 2024
1 parent 71e772e commit e4d5f8d
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 45 deletions.
3 changes: 2 additions & 1 deletion taos-ws-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ edition = "2021"
publish = false

[lib]
name = "taos"
name = "taosws"
crate-type = ["cdylib", "staticlib"]

[dependencies]
anyhow = "1"
bytes = "1.1.0"
cargo_metadata = "0.18.1"
futures = { version = "0.3", features = ["executor"] }
once_cell = "1.20.2"
pretty_env_logger = "0.5.0"
serde_json = "1"
taos-error = { path = "../taos-error" }
Expand Down
2 changes: 1 addition & 1 deletion taos-ws-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn write_header(header: &str, dir: &str, cfg: &str) -> Result<(), Box<dyn Error>
.with_crate(crate_dir)
.with_config(config)
.generate()
.expect(&format!("Failed to generate {header}"))
.unwrap_or_else(|err| panic!("Failed to generate {header}, err: {err}"))
.write_to_file(bindings);

Ok(())
Expand Down
118 changes: 76 additions & 42 deletions taos-ws-sys/src/native/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
#![allow(unused_variables)]

use std::ffi::{c_char, c_int, c_void, CStr};
use std::ffi::{c_char, c_int, c_void, CStr, VaList};
use std::ptr::{null, null_mut};
use std::sync::RwLock;

use error::set_err_and_get_code;
use once_cell::sync::Lazy;
use taos_error::{Code, Error};
use taos_query::common::Ty;
use taos_query::TBuilder;
use taos_ws::{Taos, TaosBuilder};
use tracing::trace;

mod error;
pub mod error;
pub mod query;
pub mod sml;
pub mod stmt;
Expand All @@ -23,46 +26,6 @@ pub type TAOS_RES = c_void;

type Result<T> = std::result::Result<T, Error>;

#[repr(C)]
#[allow(non_camel_case_types)]
pub enum TSDB_OPTION {
TSDB_OPTION_LOCALE = 0,
TSDB_OPTION_CHARSET = 1,
TSDB_OPTION_TIMEZONE = 2,
TSDB_OPTION_CONFIGDIR = 3,
TSDB_OPTION_SHELL_ACTIVITY_TIMER = 4,
TSDB_OPTION_USE_ADAPTER = 5,
TSDB_OPTION_DRIVER = 6,
TSDB_MAX_OPTIONS = 7,
}

#[allow(clippy::just_underscores_and_digits)]
#[no_mangle]
pub unsafe extern "C" fn taos_options(option: TSDB_OPTION, arg: *const c_void, ...) -> c_int {
todo!()
}

#[repr(C)]
#[allow(non_camel_case_types)]
pub enum TSDB_OPTION_CONNECTION {
TSDB_OPTION_CONNECTION_CLEAR = -1,
TSDB_OPTION_CONNECTION_CHARSET = 0,
TSDB_OPTION_CONNECTION_TIMEZONE = 1,
TSDB_OPTION_CONNECTION_USER_IP = 2,
TSDB_OPTION_CONNECTION_USER_APP = 3,
TSDB_MAX_OPTIONS_CONNECTION = 4,
}

#[no_mangle]
pub unsafe extern "C" fn taos_options_connection(
taos: *mut TAOS,
option: TSDB_OPTION_CONNECTION,
arg: *const c_void,
...
) -> c_int {
todo!()
}

#[no_mangle]
pub extern "C" fn taos_connect(
ip: *const c_char,
Expand Down Expand Up @@ -156,6 +119,62 @@ pub extern "C" fn taos_close(taos: *mut TAOS) {
}
}

#[repr(C)]
#[allow(non_camel_case_types)]
pub enum TSDB_OPTION {
TSDB_OPTION_LOCALE = 0,
TSDB_OPTION_CHARSET = 1,
TSDB_OPTION_TIMEZONE = 2,
TSDB_OPTION_CONFIGDIR = 3,
TSDB_OPTION_SHELL_ACTIVITY_TIMER = 4,
TSDB_OPTION_USE_ADAPTER = 5,
TSDB_OPTION_DRIVER = 6,
TSDB_MAX_OPTIONS = 7,
}

static PARAMS: Lazy<RwLock<String>> = Lazy::new(|| RwLock::new(String::new()));

// todo
#[allow(clippy::just_underscores_and_digits)]
#[no_mangle]
pub unsafe extern "C" fn taos_options(
option: TSDB_OPTION,
arg: *const c_void,
mut varargs: VaList,
) -> c_int {
let mut params = Vec::new();

loop {
let key_ptr: *const c_char = varargs.arg();
if key_ptr.is_null() {
break;
}

let key = match CStr::from_ptr(key_ptr).to_str() {
Ok(key) => key,
Err(_) => return set_err_and_get_code(Error::new(Code::INVALID_PARA, "Invalid key")),
};

let value_ptr: *const c_char = varargs.arg();
if value_ptr.is_null() {
break;
}

let value = match CStr::from_ptr(value_ptr).to_str() {
Ok(value) => value,
Err(_) => return set_err_and_get_code(Error::new(Code::INVALID_PARA, "Invalid value")),
};

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

trace!("dsn params: {:?}", params);

*PARAMS.write().unwrap() = params.join("&");

Code::SUCCESS.into()
}

#[no_mangle]
pub extern "C" fn taos_data_type(r#type: c_int) -> *const c_char {
match Ty::from_u8_option(r#type as _) {
Expand All @@ -164,6 +183,21 @@ pub extern "C" fn taos_data_type(r#type: c_int) -> *const c_char {
}
}

#[no_mangle]
pub extern "C" fn init_log() {
use std::sync::Once;

static ONCE_INIT: Once = Once::new();
ONCE_INIT.call_once(|| {
let mut builder = pretty_env_logger::formatted_timed_builder();
builder.format_timestamp_nanos();
builder.parse_filters("trace");
builder.init();
});

trace!("init log");
}

#[cfg(test)]
mod tests {
use std::ffi::CString;
Expand Down
23 changes: 22 additions & 1 deletion taos-ws-sys/src/native/stub.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(unused_variables)]

use std::ffi::{c_char, c_int, c_ulong, c_void};
use std::ffi::{c_char, c_int, c_ulong, c_void, VaList};

use super::query::{__taos_async_fn_t, TAOS_FIELD};
use super::stmt::TAOS_FIELD_E;
Expand All @@ -15,6 +15,27 @@ pub extern "C" fn taos_init() -> c_int {
#[no_mangle]
pub extern "C" fn taos_cleanup() {}

#[repr(C)]
#[allow(non_camel_case_types)]
pub enum TSDB_OPTION_CONNECTION {
TSDB_OPTION_CONNECTION_CLEAR = -1,
TSDB_OPTION_CONNECTION_CHARSET = 0,
TSDB_OPTION_CONNECTION_TIMEZONE = 1,
TSDB_OPTION_CONNECTION_USER_IP = 2,
TSDB_OPTION_CONNECTION_USER_APP = 3,
TSDB_MAX_OPTIONS_CONNECTION = 4,
}

#[no_mangle]
pub unsafe extern "C" fn taos_options_connection(
taos: *mut TAOS,
option: TSDB_OPTION_CONNECTION,
arg: *const c_void,
varargs: VaList,
) -> c_int {
todo!()
}

#[no_mangle]
pub extern "C" fn taos_connect_auth(
ip: *const c_char,
Expand Down
4 changes: 4 additions & 0 deletions taos-ws-sys/tests/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FLAGS=-I. -I../../target/debug/ -L. -L../../target/debug/ -ltaosws

test_native:
gcc -g test_native.c ${FLAGS} -o ../../target/test_native && ../../target/test_native
14 changes: 14 additions & 0 deletions taos-ws-sys/tests/test_native.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "taos.h"

int main()
{
init_log();

int code = taos_options(TSDB_OPTION_LOCALE, NULL, "group.id", "10s", "enable.auto.commit", "false", NULL);
if (code != 0)
{
return 1;
}

return 0;
}

0 comments on commit e4d5f8d

Please sign in to comment.