Skip to content

Commit e38211a

Browse files
author
kamillecao
committed
support query with raw sql.
1 parent 2b54e48 commit e38211a

File tree

3 files changed

+30
-18
lines changed

3 files changed

+30
-18
lines changed

src/lib.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,14 @@ impl Client {
308308
inserter::Inserter::new(self, table)
309309
}
310310

311-
/// Starts a new SELECT/DDL query.
311+
/// Starts a new SELECT/DDL query with sql rendering
312312
pub fn query(&self, query: &str) -> query::Query {
313-
query::Query::new(self, query)
313+
query::Query::new(self, query, true)
314+
}
315+
316+
/// Starts a new SELECT/DDL query with a raw sql
317+
pub fn query_with_raw_sql(&self, query: &str) -> query::Query {
318+
query::Query::new(self, query, false)
314319
}
315320

316321
/// Starts a new WATCH query.

src/query.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ pub struct Query {
2626
}
2727

2828
impl Query {
29-
pub(crate) fn new(client: &Client, template: &str) -> Self {
29+
pub(crate) fn new(client: &Client, template: &str, need_render: bool) -> Self {
3030
Self {
3131
client: client.clone(),
32-
sql: SqlBuilder::new(template),
32+
sql: SqlBuilder::new_with_need_render(template, need_render),
3333
}
3434
}
3535

src/sql/mod.rs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,24 +47,31 @@ impl fmt::Display for SqlBuilder {
4747
}
4848

4949
impl SqlBuilder {
50+
#[cfg(test)]
5051
pub(crate) fn new(template: &str) -> Self {
52+
Self::new_with_need_render(template, true)
53+
}
54+
55+
pub(crate) fn new_with_need_render(template: &str, need_render: bool) -> Self {
5156
let mut parts = Vec::new();
5257
let mut rest = template;
53-
while let Some(idx) = rest.find('?') {
54-
if rest[idx + 1..].starts_with('?') {
55-
parts.push(Part::Text(rest[..idx + 1].to_string()));
56-
rest = &rest[idx + 2..];
57-
continue;
58-
} else if idx != 0 {
59-
parts.push(Part::Text(rest[..idx].to_string()));
60-
}
58+
if need_render {
59+
while let Some(idx) = rest.find('?') {
60+
if rest[idx + 1..].starts_with('?') {
61+
parts.push(Part::Text(rest[..idx + 1].to_string()));
62+
rest = &rest[idx + 2..];
63+
continue;
64+
} else if idx != 0 {
65+
parts.push(Part::Text(rest[..idx].to_string()));
66+
}
6167

62-
rest = &rest[idx + 1..];
63-
if let Some(restfields) = rest.strip_prefix("fields") {
64-
parts.push(Part::Fields);
65-
rest = restfields;
66-
} else {
67-
parts.push(Part::Arg);
68+
rest = &rest[idx + 1..];
69+
if let Some(restfields) = rest.strip_prefix("fields") {
70+
parts.push(Part::Fields);
71+
rest = restfields;
72+
} else {
73+
parts.push(Part::Arg);
74+
}
6875
}
6976
}
7077

0 commit comments

Comments
 (0)