Skip to content

Commit 08a8f49

Browse files
committed
Address issues
1 parent ce7f0b1 commit 08a8f49

File tree

4 files changed

+47
-28
lines changed

4 files changed

+47
-28
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "colink"
3-
version = "0.3.7"
3+
version = "0.3.8"
44
edition = "2021"
55
description = "CoLink Rust SDK"
66
license = "MIT"
@@ -23,7 +23,7 @@ lapin = "2.1"
2323
prost = "0.10"
2424
rand = { version = "0.8", features = ["std_rng"] }
2525
rcgen = { version = "0.10", optional = true }
26-
rdbc2 = { version = "0.2.0", optional = true }
26+
rdbc2 = { version = "0.2.2", optional = true }
2727
redis = { version = "0.22", features = ["tokio-comp"] }
2828
reqwest = { version = "0.11", default-features = false, features = ["rustls-tls-native-roots"], optional = true }
2929
secp256k1 = { version = "0.25", features = ["rand-std"] }

src/extensions/instant_server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ impl InstantServer {
8181
.join(instant_server_id.clone());
8282
std::fs::create_dir_all(&working_dir).unwrap();
8383
let mut user_init_config_file =
84-
std::fs::File::create(&Path::new(&working_dir).join("user_init_config.toml")).unwrap();
84+
std::fs::File::create(Path::new(&working_dir).join("user_init_config.toml")).unwrap();
8585
user_init_config_file
8686
.write_all(user_init_config.as_bytes())
8787
.unwrap();

src/extensions/storage_macro/dbc.rs

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,27 @@ type Error = Box<dyn std::error::Error + Send + Sync + 'static>;
55

66
impl crate::application::CoLink {
77
#[async_recursion]
8-
async fn _search_and_generate_query_string(
8+
async fn _search_and_generate_query(
99
&self,
1010
string_before_dbc: &str,
1111
string_after_dbc: &str,
12-
) -> Result<String, Error> {
12+
) -> Result<(String, Vec<String>), Error> {
1313
let split_key_path: Vec<&str> = string_after_dbc.split(':').collect();
14-
for i in 0..split_key_path.len() {
15-
let current_key_path = format!(
16-
"{}:{}",
17-
string_before_dbc,
18-
split_key_path[0..i + 1].join(":")
19-
);
14+
for i in (0..split_key_path.len()).rev() {
15+
let current_key_path =
16+
format!("{}:{}", string_before_dbc, split_key_path[0..=i].join(":"));
2017
let payload = self.read_entry(current_key_path.as_str()).await;
21-
if payload.is_ok() {
22-
let query_string_raw = String::from_utf8(payload.unwrap())?;
23-
let count = query_string_raw.matches('?').count();
18+
if let Ok(payload) = payload {
19+
let query_string = String::from_utf8(payload)?;
20+
let count = query_string.matches('?').count();
2421
if count != split_key_path.len() - (i + 1) {
2522
return Err("Number of parameters does not match specified query string")?;
2623
}
27-
let mut query_string = query_string_raw;
28-
for j in 0..count {
29-
query_string = query_string.replacen('?', split_key_path[i + j + 1], 1);
30-
}
31-
return Ok(query_string);
32-
} else {
33-
continue;
24+
let params = split_key_path[(i + 1)..]
25+
.iter()
26+
.map(|x| x.to_string())
27+
.collect::<Vec<String>>();
28+
return Ok((query_string, params));
3429
}
3530
}
3631
Err("no query string found.")?
@@ -45,12 +40,13 @@ impl crate::application::CoLink {
4540
let url_key = format!("{}:url", string_before_dbc);
4641
let url = self.read_entry(url_key.as_str()).await?;
4742
let url_string = String::from_utf8(url)?;
48-
let query_string = self
49-
._search_and_generate_query_string(string_before_dbc, string_after_dbc)
43+
let (query_string, params) = self
44+
._search_and_generate_query(string_before_dbc, string_after_dbc)
5045
.await?;
46+
let params: Vec<&str> = params.iter().map(AsRef::as_ref).collect();
5147
let mut database = rdbc2::dbc::Database::new(url_string.as_str())?;
52-
let result = database.execute_query(query_string.as_str())?;
53-
let seraliased_result = serde_json::to_vec(&result)?;
54-
Ok(seraliased_result)
48+
let result = database.execute_query_with_params(query_string.as_str(), &params)?;
49+
let seralized_result = serde_json::to_vec(&result)?;
50+
Ok(seralized_result)
5551
}
5652
}

tests/test_storage_macro_dbc.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,40 @@ async fn test_storage_macro_dbc_mysql(
6060
let query_result = cl
6161
.read_entry("storage_macro_test:db:$dbc:query_users:'Alice':'20'")
6262
.await?;
63-
cl.read_entry("storage_macro_test:db:$dbc:cleanup").await?;
6463

6564
let stringified = String::from_utf8(query_result.clone())?;
6665
println!("{}", stringified);
6766
assert_eq!(
6867
stringified,
69-
r#"{"rows":[{"values":[{"Bytes":[65,108,105,99,101]},{"Int":20}],"columns":[{"name":"name","column_type":"VARCHAR"},{"name":"age","column_type":"INT"}]}],"affected_rows":0}"#
68+
r#"{"rows":[{"values":[{"Bytes":"QWxpY2U"},{"Int":20}],"columns":[{"name":"name","column_type":"VARCHAR"},{"name":"age","column_type":"INT"}]}],"affected_row_count":0}"#
7069
);
7170

7271
let deserialized: rdbc2::dbc::QueryResult = serde_json::from_slice(&query_result)?;
7372
assert_eq!(deserialized.rows.len(), 1);
7473

74+
// Test query string parsing order
75+
cl.create_entry(
76+
"storage_macro_test:db:query_users2",
77+
b"SELECT * FROM users WHERE name = ? AND age = ?" as &[u8],
78+
)
79+
.await?;
80+
81+
cl.create_entry(
82+
"storage_macro_test:db:query_users2:additional",
83+
b"SELECT * FROM users WHERE name = ?" as &[u8],
84+
)
85+
.await?;
86+
87+
let result = cl
88+
.read_entry("storage_macro_test:db:$dbc:query_users2:additional:'Alice'")
89+
.await?;
90+
91+
assert_eq!(
92+
String::from_utf8(result)?,
93+
r#"{"rows":[{"values":[{"Bytes":"QWxpY2U"},{"Int":20}],"columns":[{"name":"name","column_type":"VARCHAR"},{"name":"age","column_type":"INT"}]}],"affected_row_count":0}"#
94+
);
95+
96+
cl.read_entry("storage_macro_test:db:$dbc:cleanup").await?;
97+
7598
Ok(())
7699
}

0 commit comments

Comments
 (0)