Skip to content

Commit

Permalink
fix: bad query split (#69)
Browse files Browse the repository at this point in the history
## Rationale
Close #68


## Detailed Changes


## Test Plan
CI
  • Loading branch information
jiacai2050 authored Jun 24, 2024
1 parent 5469532 commit 49a5745
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
5 changes: 5 additions & 0 deletions sqlness/examples/interceptor-case/simple/input.result
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,8 @@ INSERT INTO t (c) VALUES(1) , (2) , (3) , (4) ;
4
2;

-- Test case for https://github.com/CeresDB/sqlness/issues/68
INSERT INTO timestamp VALUES ('1900-1-1 00;00;00');

INSERT INTO timestamp VALUES ('1900-1-1 00;00;00');

3 changes: 3 additions & 0 deletions sqlness/examples/interceptor-case/simple/input.sql
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,6 @@ INSERT INTO t (c) VALUES
4
2
2;

-- Test case for https://github.com/CeresDB/sqlness/issues/68
INSERT INTO timestamp VALUES ('1900-1-1 00;00;00');
12 changes: 7 additions & 5 deletions sqlness/src/case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,14 @@ impl Query {

let sql = self.concat_query_lines();
// An intercetor may generate multiple SQLs, so we need to split them.
for sql in sql.split(QUERY_DELIMITER) {
for sql in sql.split(crate::interceptor::template::DELIMITER) {
if !sql.trim().is_empty() {
let mut result = db
.query(context.clone(), format!("{sql};"))
.await
.to_string();
let sql = if sql.ends_with(QUERY_DELIMITER) {
sql.to_string()
} else {
format!("{sql};")
};
let mut result = db.query(context.clone(), sql).await.to_string();
self.after_execute_intercept(&mut result).await;
self.write_result(writer, result)?;
}
Expand Down
3 changes: 2 additions & 1 deletion sqlness/src/interceptor/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::SqlnessError;
pub struct TemplateInterceptorFactory;

pub const PREFIX: &str = "TEMPLATE";
pub const DELIMITER: &str = "__sqlness_delimiter__";

/// Templated query, powered by [minijinja](https://github.com/mitsuhiko/minijinja).
/// The template syntax can be found [here](https://docs.rs/minijinja/latest/minijinja/syntax/index.html).
Expand Down Expand Up @@ -43,7 +44,7 @@ pub struct TemplateInterceptor {
}

fn sql_delimiter() -> std::result::Result<String, minijinja::Error> {
Ok(";".to_string())
Ok(DELIMITER.to_string())
}

impl Interceptor for TemplateInterceptor {
Expand Down

0 comments on commit 49a5745

Please sign in to comment.