From 9157ab3d6415df8123a30ecca0a090b88fe45f1b Mon Sep 17 00:00:00 2001 From: Jiacai Liu Date: Fri, 22 Mar 2024 17:30:44 +0800 Subject: [PATCH] refactor example (#64) * refactor example * Update PR template * update Makefile * fix CI --- .github/pull_request_template.md | 30 +---- .gitignore | 2 + Makefile | 19 +-- sqlness/examples/basic.toml | 1 - sqlness/examples/echo.rs | 53 -------- .../examples/echo/interceptor-env/env.result | 10 -- sqlness/examples/echo/interceptor-env/env.sql | 5 - .../echo/interceptor-replace/replace.result | 21 --- .../echo/interceptor-replace/replace.sql | 12 -- .../interceptor-arg/simple/input.result | 15 --- .../examples/interceptor-arg/simple/input.sql | 8 -- .../interceptor-case/simple/input.result | 121 ++++++++++++++++++ .../interceptor-case/simple/input.sql | 71 ++++++++++ .../interceptor-env/simple/env.result | 22 ---- .../examples/interceptor-env/simple/env.sql | 15 --- .../interceptor-replace/simple/replace.result | 47 ------- .../interceptor-replace/simple/replace.sql | 28 ---- .../simple/sort_result.result | 33 ----- .../simple/sort_result.sql | 17 --- .../{interceptor_arg.rs => interceptor.rs} | 17 ++- sqlness/examples/interceptor_env.rs | 55 -------- sqlness/examples/interceptor_replace.rs | 53 -------- sqlness/examples/interceptor_sort_result.rs | 53 -------- sqlness/src/runner.rs | 1 + 24 files changed, 215 insertions(+), 494 deletions(-) delete mode 100644 sqlness/examples/basic.toml delete mode 100644 sqlness/examples/echo.rs delete mode 100644 sqlness/examples/echo/interceptor-env/env.result delete mode 100644 sqlness/examples/echo/interceptor-env/env.sql delete mode 100644 sqlness/examples/echo/interceptor-replace/replace.result delete mode 100644 sqlness/examples/echo/interceptor-replace/replace.sql delete mode 100644 sqlness/examples/interceptor-arg/simple/input.result delete mode 100644 sqlness/examples/interceptor-arg/simple/input.sql create mode 100644 sqlness/examples/interceptor-case/simple/input.result create mode 100644 sqlness/examples/interceptor-case/simple/input.sql delete mode 100644 sqlness/examples/interceptor-env/simple/env.result delete mode 100644 sqlness/examples/interceptor-env/simple/env.sql delete mode 100644 sqlness/examples/interceptor-replace/simple/replace.result delete mode 100644 sqlness/examples/interceptor-replace/simple/replace.sql delete mode 100644 sqlness/examples/interceptor-sort-result/simple/sort_result.result delete mode 100644 sqlness/examples/interceptor-sort-result/simple/sort_result.sql rename sqlness/examples/{interceptor_arg.rs => interceptor.rs} (73%) delete mode 100644 sqlness/examples/interceptor_env.rs delete mode 100644 sqlness/examples/interceptor_replace.rs delete mode 100644 sqlness/examples/interceptor_sort_result.rs diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index 37e5feb..3c2dd89 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -1,31 +1,7 @@ -# Which issue does this PR close? +## Rationale -Closes # -# Rationale for this change - - +## Detailed Changes -# What changes are included in this PR? - - -# Are there any user-facing changes? - - - -# How does this change test - - +## Test Plan diff --git a/.gitignore b/.gitignore index a9d37c5..c28dac5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ target Cargo.lock +.DS_Store +.project diff --git a/Makefile b/Makefile index 5e45779..4a440c6 100644 --- a/Makefile +++ b/Makefile @@ -25,24 +25,15 @@ cli-test: cd $(DIR)/sqlness-cli; cargo run -- -t mysql -c tests/mysql -i 127.0.0.1 -p 3306 -u root -P 1a2b3c -d public cd $(DIR)/sqlness-cli; cargo run -- -t postgresql -c tests/postgresql -i 127.0.0.1 -p 5432 -u postgres -P postgres -d postgres -example: good-example bad-example - -good-example: basic-example interceptor-arg-example interceptor-replace-example interceptor-sort-result-example interceptor-env-example - basic-example: cd $(DIR)/sqlness; cargo run --example basic +interceptor-example: + cd $(DIR)/sqlness; cargo run --example interceptor + bad-example: cd $(DIR)/sqlness; cargo run --example bad -interceptor-arg-example: - cd $(DIR)/sqlness; cargo run --example interceptor_arg +good-example: basic-example interceptor-example -interceptor-replace-example: - cd $(DIR)/sqlness; cargo run --example interceptor_replace - -interceptor-sort-result-example: - cd $(DIR)/sqlness; cargo run --example interceptor_sort_result - -interceptor-env-example: - cd $(DIR)/sqlness; cargo run --example interceptor_env +example: good-example bad-example diff --git a/sqlness/examples/basic.toml b/sqlness/examples/basic.toml deleted file mode 100644 index 1183cdf..00000000 --- a/sqlness/examples/basic.toml +++ /dev/null @@ -1 +0,0 @@ -case_dir = "examples/basic-case" diff --git a/sqlness/examples/echo.rs b/sqlness/examples/echo.rs deleted file mode 100644 index 10a3a49..00000000 --- a/sqlness/examples/echo.rs +++ /dev/null @@ -1,53 +0,0 @@ -// Copyright 2022 CeresDB Project Authors. Licensed under Apache-2.0. - -//! Shows how an REPLACE interceptor works. - -use std::{fmt::Display, path::Path}; - -use async_trait::async_trait; -use sqlness::{ConfigBuilder, Database, EnvController, QueryContext, Runner}; - -struct MyController; -struct MyDB; - -#[async_trait] -impl Database for MyDB { - async fn query(&self, _: QueryContext, query: String) -> Box { - return Box::new(query); - } -} - -impl MyDB { - fn new(_env: &str, _config: Option<&Path>) -> Self { - MyDB - } - - fn stop(self) {} -} - -#[async_trait] -impl EnvController for MyController { - type DB = MyDB; - - async fn start(&self, env: &str, config: Option<&Path>) -> Self::DB { - MyDB::new(env, config) - } - - async fn stop(&self, _env: &str, database: Self::DB) { - database.stop(); - } -} - -#[tokio::main] -async fn main() { - let env = MyController; - let config = ConfigBuilder::default() - .case_dir("examples/echo".to_string()) - .build() - .unwrap(); - let runner = Runner::new(config, env); - - println!("Run testcase..."); - - runner.run().await.unwrap(); -} diff --git a/sqlness/examples/echo/interceptor-env/env.result b/sqlness/examples/echo/interceptor-env/env.result deleted file mode 100644 index 596cb7f..00000000 --- a/sqlness/examples/echo/interceptor-env/env.result +++ /dev/null @@ -1,10 +0,0 @@ --- SQLNESS ENV SECRET -select 23333 from data; - -select 23333 from data; - --- SQLNESS ENV SECRET NONEXISTENT -select 23333 from data; - -select 23333 from data; - diff --git a/sqlness/examples/echo/interceptor-env/env.sql b/sqlness/examples/echo/interceptor-env/env.sql deleted file mode 100644 index 5160163..00000000 --- a/sqlness/examples/echo/interceptor-env/env.sql +++ /dev/null @@ -1,5 +0,0 @@ --- SQLNESS ENV SECRET -select {{ SECRET }} from data; - --- SQLNESS ENV SECRET NONEXISTENT -select {{ SECRET }} from data; \ No newline at end of file diff --git a/sqlness/examples/echo/interceptor-replace/replace.result b/sqlness/examples/echo/interceptor-replace/replace.result deleted file mode 100644 index 60d17d6..00000000 --- a/sqlness/examples/echo/interceptor-replace/replace.result +++ /dev/null @@ -1,21 +0,0 @@ --- SQLNESS REPLACE 00 -SELECT 0; - -SELECT 0; - --- SQLNESS REPLACE 00 -SELECT 00; - -SELECT ; - --- SQLNESS REPLACE 0 1 -SELECT 0; - -SELECT 1; - --- example of capture group replacement --- SQLNESS REPLACE (?P\d{4})-(?P\d{2})-(?P\d{2}) $m/$d/$y -2012-03-14, 2013-01-01 and 2014-07-05; - -03/14/2012, 01/01/2013 and 07/05/2014; - diff --git a/sqlness/examples/echo/interceptor-replace/replace.sql b/sqlness/examples/echo/interceptor-replace/replace.sql deleted file mode 100644 index f47f540..00000000 --- a/sqlness/examples/echo/interceptor-replace/replace.sql +++ /dev/null @@ -1,12 +0,0 @@ --- SQLNESS REPLACE 00 -SELECT 0; - --- SQLNESS REPLACE 00 -SELECT 00; - --- SQLNESS REPLACE 0 1 -SELECT 0; - --- example of capture group replacement --- SQLNESS REPLACE (?P\d{4})-(?P\d{2})-(?P\d{2}) $m/$d/$y -2012-03-14, 2013-01-01 and 2014-07-05; diff --git a/sqlness/examples/interceptor-arg/simple/input.result b/sqlness/examples/interceptor-arg/simple/input.result deleted file mode 100644 index 7d2fb3c..00000000 --- a/sqlness/examples/interceptor-arg/simple/input.result +++ /dev/null @@ -1,15 +0,0 @@ --- SQLNESS ARG bla=a addr=127.0.0.1 port=3306 -SELECT * FROM t; - -addr=127.0.0.1 -bla=a -port=3306 - --- SQLNESS 🤪ARG🤪 addr=127.0.0.1 port=3306 --- SQLNESS ARG port=3307 -SELECT * -FROM t -WHERE A=B; - -port=3307 - diff --git a/sqlness/examples/interceptor-arg/simple/input.sql b/sqlness/examples/interceptor-arg/simple/input.sql deleted file mode 100644 index 93dc0df..00000000 --- a/sqlness/examples/interceptor-arg/simple/input.sql +++ /dev/null @@ -1,8 +0,0 @@ --- SQLNESS ARG bla=a addr=127.0.0.1 port=3306 -SELECT * FROM t; - --- SQLNESS 🤪ARG🤪 addr=127.0.0.1 port=3306 --- SQLNESS ARG port=3307 -SELECT * -FROM t -WHERE A=B; diff --git a/sqlness/examples/interceptor-case/simple/input.result b/sqlness/examples/interceptor-case/simple/input.result new file mode 100644 index 00000000..e72d634 --- /dev/null +++ b/sqlness/examples/interceptor-case/simple/input.result @@ -0,0 +1,121 @@ +-- SQLNESS ARG bla=a addr=127.0.0.1 port=3306 +SELECT * FROM t; + +Args: addr=127.0.0.1, bla=a, port=3306 + +SELECT * FROM t; + +-- SQLNESS 🤪ARG🤪 addr=127.0.0.1 port=3306 +-- SQLNESS ARG port=3307 +SELECT * +FROM t +WHERE A=B; + +Args: port=3307 + +SELECT * +FROM t +WHERE A=B; + +-- multiple env in one line +-- SQLNESS ENV ENV1 ENV2 NONEXISTENT1 NONEXISTENT2 NONEXISTENT3 +SELECT $ENV1, $ENV2, $NONEXISTENT1 FROM t; + +SELECT value1, value2, $NONEXISTENT1 FROM t; + +-- multiple env in multiple lines +-- SQLNESS ENV ENV1 +-- SQLNESS ENV ENV2 +-- SQLNESS ENV NONEXISTENT1 +-- SQLNESS ENV NONEXISTENT2 +-- SQLNESS ENV NONEXISTENT3 +SELECT $ENV1, $ENV2, $NONEXISTENT1, FROM t; + +SELECT value1, value2, $NONEXISTENT1, FROM t; + +-- Undeclared env won't be rendered +-- SQLNESS ENV ENV2 +SELECT $ENV1, $ENV2, $NONEXISTENT1 FROM t; + +SELECT $ENV1, value2, $NONEXISTENT1 FROM t; + +-- SQLNESS REPLACE 00 +SELECT 0; + +SELECT 0; + +-- SQLNESS REPLACE 00 +SELECT 00; + +SELECT ; + +-- SQLNESS REPLACE 0 1 +SELECT 0; + +SELECT 1; + +-- example of capture group replacement +-- SQLNESS REPLACE (?P\d{4})-(?P\d{2})-(?P\d{2}) $m/$d/$y +2012-03-14, 2013-01-01 and 2014-07-05; + +03/14/2012, 01/01/2013 and 07/05/2014; + +-- SQLNESS TEMPLATE {"name": "test"} +SELECT * FROM table where name = "{{name}}"; + +SELECT * FROM table where name = "test"; + +-- SQLNESS TEMPLATE {"aggr": ["sum", "avg", "count"]} +{% for item in aggr %} +SELECT {{item}}(c) from t {%if not loop.last %} {{sql_delimiter()}} {% endif %} +{% endfor %} +; + +SELECT sum(c) from t ; + + SELECT avg(c) from t ; + + SELECT count(c) from t ; + +-- SQLNESS TEMPLATE +INSERT INTO t (c) VALUES +{% for num in range(1, 5) %} +({{ num }}) {%if not loop.last %} , {% endif %} +{% endfor %} +; + +INSERT INTO t (c) VALUES(1) , (2) , (3) , (4) ; + +-- SQLNESS SORT_RESULT +4 +3 +6 +1; + +1; +3 +4 +6 + +-- SQLNESS SORT_RESULT 1 1 +7 +1 +4 +2 +2; + +7 +1 +2 +4 +2; + +-- SQLNESS SORT_RESULT -1 +6 +2 +4; + +6 +2 +4; + diff --git a/sqlness/examples/interceptor-case/simple/input.sql b/sqlness/examples/interceptor-case/simple/input.sql new file mode 100644 index 00000000..85bced9 --- /dev/null +++ b/sqlness/examples/interceptor-case/simple/input.sql @@ -0,0 +1,71 @@ +-- SQLNESS ARG bla=a addr=127.0.0.1 port=3306 +SELECT * FROM t; + +-- SQLNESS 🤪ARG🤪 addr=127.0.0.1 port=3306 +-- SQLNESS ARG port=3307 +SELECT * +FROM t +WHERE A=B; + +-- multiple env in one line +-- SQLNESS ENV ENV1 ENV2 NONEXISTENT1 NONEXISTENT2 NONEXISTENT3 +SELECT $ENV1, $ENV2, $NONEXISTENT1 FROM t; + +-- multiple env in multiple lines +-- SQLNESS ENV ENV1 +-- SQLNESS ENV ENV2 +-- SQLNESS ENV NONEXISTENT1 +-- SQLNESS ENV NONEXISTENT2 +-- SQLNESS ENV NONEXISTENT3 +SELECT $ENV1, $ENV2, $NONEXISTENT1, FROM t; + +-- Undeclared env won't be rendered +-- SQLNESS ENV ENV2 +SELECT $ENV1, $ENV2, $NONEXISTENT1 FROM t; + +-- SQLNESS REPLACE 00 +SELECT 0; + +-- SQLNESS REPLACE 00 +SELECT 00; + +-- SQLNESS REPLACE 0 1 +SELECT 0; + +-- example of capture group replacement +-- SQLNESS REPLACE (?P\d{4})-(?P\d{2})-(?P\d{2}) $m/$d/$y +2012-03-14, 2013-01-01 and 2014-07-05; + +-- SQLNESS TEMPLATE {"name": "test"} +SELECT * FROM table where name = "{{name}}"; + +-- SQLNESS TEMPLATE {"aggr": ["sum", "avg", "count"]} +{% for item in aggr %} +SELECT {{item}}(c) from t {%if not loop.last %} {{sql_delimiter()}} {% endif %} +{% endfor %} +; + +-- SQLNESS TEMPLATE +INSERT INTO t (c) VALUES +{% for num in range(1, 5) %} +({{ num }}) {%if not loop.last %} , {% endif %} +{% endfor %} +; + +-- SQLNESS SORT_RESULT +4 +3 +6 +1; + +-- SQLNESS SORT_RESULT 1 1 +7 +1 +4 +2 +2; + +-- SQLNESS SORT_RESULT -1 +6 +2 +4; diff --git a/sqlness/examples/interceptor-env/simple/env.result b/sqlness/examples/interceptor-env/simple/env.result deleted file mode 100644 index 6a72536..00000000 --- a/sqlness/examples/interceptor-env/simple/env.result +++ /dev/null @@ -1,22 +0,0 @@ --- multiple env in one line --- SQLNESS ENV ENV1 ENV2 NONEXISTENT1 NONEXISTENT2 NONEXISTENT3 -SELECT $ENV1, $ENV2, $NONEXISTENT1 FROM t; - -SELECT value1, value2, $NONEXISTENT1 FROM t; - --- multiple env in multiple lines --- SQLNESS ENV ENV1 --- SQLNESS ENV ENV2 --- SQLNESS ENV NONEXISTENT1 --- SQLNESS ENV NONEXISTENT2 --- SQLNESS ENV NONEXISTENT3 -SELECT $ENV1, $ENV2, $NONEXISTENT1, FROM t; - -SELECT value1, value2, $NONEXISTENT1, FROM t; - --- Undeclared env won't be rendered --- SQLNESS ENV ENV2 -SELECT $ENV1, $ENV2, $NONEXISTENT1 FROM t; - -SELECT $ENV1, value2, $NONEXISTENT1 FROM t; - diff --git a/sqlness/examples/interceptor-env/simple/env.sql b/sqlness/examples/interceptor-env/simple/env.sql deleted file mode 100644 index e237480..00000000 --- a/sqlness/examples/interceptor-env/simple/env.sql +++ /dev/null @@ -1,15 +0,0 @@ --- multiple env in one line --- SQLNESS ENV ENV1 ENV2 NONEXISTENT1 NONEXISTENT2 NONEXISTENT3 -SELECT $ENV1, $ENV2, $NONEXISTENT1 FROM t; - --- multiple env in multiple lines --- SQLNESS ENV ENV1 --- SQLNESS ENV ENV2 --- SQLNESS ENV NONEXISTENT1 --- SQLNESS ENV NONEXISTENT2 --- SQLNESS ENV NONEXISTENT3 -SELECT $ENV1, $ENV2, $NONEXISTENT1, FROM t; - --- Undeclared env won't be rendered --- SQLNESS ENV ENV2 -SELECT $ENV1, $ENV2, $NONEXISTENT1 FROM t; diff --git a/sqlness/examples/interceptor-replace/simple/replace.result b/sqlness/examples/interceptor-replace/simple/replace.result deleted file mode 100644 index 138b705..00000000 --- a/sqlness/examples/interceptor-replace/simple/replace.result +++ /dev/null @@ -1,47 +0,0 @@ --- SQLNESS REPLACE 00 -SELECT 0; - -SELECT 0; - --- SQLNESS REPLACE 00 -SELECT 00; - -SELECT ; - --- SQLNESS REPLACE 0 1 -SELECT 0; - -SELECT 1; - --- example of capture group replacement --- SQLNESS REPLACE (?P\d{4})-(?P\d{2})-(?P\d{2}) $m/$d/$y -2012-03-14, 2013-01-01 and 2014-07-05; - -03/14/2012, 01/01/2013 and 07/05/2014; - --- SQLNESS TEMPLATE {"name": "test"} -SELECT * FROM table where name = "{{name}}"; - -SELECT * FROM table where name = "test"; - --- SQLNESS TEMPLATE {"aggr": ["sum", "avg", "count"]} -{% for item in aggr %} -SELECT {{item}}(c) from t {%if not loop.last %} {{sql_delimiter()}} {% endif %} -{% endfor %} -; - -SELECT sum(c) from t ; - - SELECT avg(c) from t ; - - SELECT count(c) from t ; - --- SQLNESS TEMPLATE -INSERT INTO t (c) VALUES -{% for num in range(1, 5) %} -({{ num }}) {%if not loop.last %} , {% endif %} -{% endfor %} -; - -INSERT INTO t (c) VALUES(1) , (2) , (3) , (4) ; - diff --git a/sqlness/examples/interceptor-replace/simple/replace.sql b/sqlness/examples/interceptor-replace/simple/replace.sql deleted file mode 100644 index 290c98d..00000000 --- a/sqlness/examples/interceptor-replace/simple/replace.sql +++ /dev/null @@ -1,28 +0,0 @@ --- SQLNESS REPLACE 00 -SELECT 0; - --- SQLNESS REPLACE 00 -SELECT 00; - --- SQLNESS REPLACE 0 1 -SELECT 0; - --- example of capture group replacement --- SQLNESS REPLACE (?P\d{4})-(?P\d{2})-(?P\d{2}) $m/$d/$y -2012-03-14, 2013-01-01 and 2014-07-05; - --- SQLNESS TEMPLATE {"name": "test"} -SELECT * FROM table where name = "{{name}}"; - --- SQLNESS TEMPLATE {"aggr": ["sum", "avg", "count"]} -{% for item in aggr %} -SELECT {{item}}(c) from t {%if not loop.last %} {{sql_delimiter()}} {% endif %} -{% endfor %} -; - --- SQLNESS TEMPLATE -INSERT INTO t (c) VALUES -{% for num in range(1, 5) %} -({{ num }}) {%if not loop.last %} , {% endif %} -{% endfor %} -; diff --git a/sqlness/examples/interceptor-sort-result/simple/sort_result.result b/sqlness/examples/interceptor-sort-result/simple/sort_result.result deleted file mode 100644 index 43920fc..00000000 --- a/sqlness/examples/interceptor-sort-result/simple/sort_result.result +++ /dev/null @@ -1,33 +0,0 @@ --- SQLNESS SORT_RESULT -4 -3 -6 -1; - -1; -3 -4 -6 - --- SQLNESS SORT_RESULT 1 1 -7 -1 -4 -2 -2; - -7 -1 -2 -4 -2; - --- SQLNESS SORT_RESULT -1 -6 -2 -4; - -6 -2 -4; - diff --git a/sqlness/examples/interceptor-sort-result/simple/sort_result.sql b/sqlness/examples/interceptor-sort-result/simple/sort_result.sql deleted file mode 100644 index 8d1c977..00000000 --- a/sqlness/examples/interceptor-sort-result/simple/sort_result.sql +++ /dev/null @@ -1,17 +0,0 @@ --- SQLNESS SORT_RESULT -4 -3 -6 -1; - --- SQLNESS SORT_RESULT 1 1 -7 -1 -4 -2 -2; - --- SQLNESS SORT_RESULT -1 -6 -2 -4; diff --git a/sqlness/examples/interceptor_arg.rs b/sqlness/examples/interceptor.rs similarity index 73% rename from sqlness/examples/interceptor_arg.rs rename to sqlness/examples/interceptor.rs index 03066e7..0978d95 100644 --- a/sqlness/examples/interceptor_arg.rs +++ b/sqlness/examples/interceptor.rs @@ -12,15 +12,20 @@ struct MyDB; #[async_trait] impl Database for MyDB { - async fn query(&self, ctx: QueryContext, _query: String) -> Box { + async fn query(&self, ctx: QueryContext, query: String) -> Box { let mut args = ctx.context.into_iter().collect::>(); + if args.is_empty() { + return Box::new(query); + } + args.sort(); - let result = args + let args = args .into_iter() .map(|(k, v)| format!("{k}={v}")) .collect::>() - .join("\n"); - return Box::new(result); + .join(", "); + let result = format!("Args: {args}\n\n{query}"); + Box::new(result) } } @@ -47,9 +52,11 @@ impl EnvController for MyController { #[tokio::main] async fn main() { + std::env::set_var("ENV1", "value1"); + std::env::set_var("ENV2", "value2"); let env = MyController; let config = ConfigBuilder::default() - .case_dir("examples/interceptor-arg".to_string()) + .case_dir("examples/interceptor-case".to_string()) .build() .unwrap(); let runner = Runner::new(config, env); diff --git a/sqlness/examples/interceptor_env.rs b/sqlness/examples/interceptor_env.rs deleted file mode 100644 index 1ae0ab5..00000000 --- a/sqlness/examples/interceptor_env.rs +++ /dev/null @@ -1,55 +0,0 @@ -// Copyright 2023 CeresDB Project Authors. Licensed under Apache-2.0. - -//! Shows how an SORT_RESULT interceptor works. - -use std::{fmt::Display, path::Path}; - -use async_trait::async_trait; -use sqlness::{ConfigBuilder, Database, EnvController, QueryContext, Runner}; - -struct MyController; -struct MyDB; - -#[async_trait] -impl Database for MyDB { - async fn query(&self, _: QueryContext, query: String) -> Box { - return Box::new(query); - } -} - -impl MyDB { - fn new(_env: &str, _config: Option<&Path>) -> Self { - MyDB - } - - fn stop(self) {} -} - -#[async_trait] -impl EnvController for MyController { - type DB = MyDB; - - async fn start(&self, env: &str, config: Option<&Path>) -> Self::DB { - MyDB::new(env, config) - } - - async fn stop(&self, _env: &str, database: Self::DB) { - database.stop(); - } -} - -#[tokio::main] -async fn main() { - std::env::set_var("ENV1", "value1"); - std::env::set_var("ENV2", "value2"); - let env = MyController; - let config = ConfigBuilder::default() - .case_dir("examples/interceptor-env".to_string()) - .build() - .unwrap(); - let runner = Runner::new(config, env); - - println!("Run testcase..."); - - runner.run().await.unwrap(); -} diff --git a/sqlness/examples/interceptor_replace.rs b/sqlness/examples/interceptor_replace.rs deleted file mode 100644 index 23f3dda..00000000 --- a/sqlness/examples/interceptor_replace.rs +++ /dev/null @@ -1,53 +0,0 @@ -// Copyright 2022 CeresDB Project Authors. Licensed under Apache-2.0. - -//! Shows how an REPLACE interceptor works. - -use std::{fmt::Display, path::Path}; - -use async_trait::async_trait; -use sqlness::{ConfigBuilder, Database, EnvController, QueryContext, Runner}; - -struct MyController; -struct MyDB; - -#[async_trait] -impl Database for MyDB { - async fn query(&self, _: QueryContext, query: String) -> Box { - return Box::new(query); - } -} - -impl MyDB { - fn new(_env: &str, _config: Option<&Path>) -> Self { - MyDB - } - - fn stop(self) {} -} - -#[async_trait] -impl EnvController for MyController { - type DB = MyDB; - - async fn start(&self, env: &str, config: Option<&Path>) -> Self::DB { - MyDB::new(env, config) - } - - async fn stop(&self, _env: &str, database: Self::DB) { - database.stop(); - } -} - -#[tokio::main] -async fn main() { - let env = MyController; - let config = ConfigBuilder::default() - .case_dir("examples/interceptor-replace".to_string()) - .build() - .unwrap(); - let runner = Runner::new(config, env); - - println!("Run testcase..."); - - runner.run().await.unwrap(); -} diff --git a/sqlness/examples/interceptor_sort_result.rs b/sqlness/examples/interceptor_sort_result.rs deleted file mode 100644 index aeeec19..00000000 --- a/sqlness/examples/interceptor_sort_result.rs +++ /dev/null @@ -1,53 +0,0 @@ -// Copyright 2023 CeresDB Project Authors. Licensed under Apache-2.0. - -//! Shows how an SORT_RESULT interceptor works. - -use std::{fmt::Display, path::Path}; - -use async_trait::async_trait; -use sqlness::{ConfigBuilder, Database, EnvController, QueryContext, Runner}; - -struct MyController; -struct MyDB; - -#[async_trait] -impl Database for MyDB { - async fn query(&self, _: QueryContext, query: String) -> Box { - return Box::new(query); - } -} - -impl MyDB { - fn new(_env: &str, _config: Option<&Path>) -> Self { - MyDB - } - - fn stop(self) {} -} - -#[async_trait] -impl EnvController for MyController { - type DB = MyDB; - - async fn start(&self, env: &str, config: Option<&Path>) -> Self::DB { - MyDB::new(env, config) - } - - async fn stop(&self, _env: &str, database: Self::DB) { - database.stop(); - } -} - -#[tokio::main] -async fn main() { - let env = MyController; - let config = ConfigBuilder::default() - .case_dir("examples/interceptor-sort-result".to_string()) - .build() - .unwrap(); - let runner = Runner::new(config, env); - - println!("Run testcase..."); - - runner.run().await.unwrap(); -} diff --git a/sqlness/src/runner.rs b/sqlness/src/runner.rs index 01d5e84..9e9066f 100644 --- a/sqlness/src/runner.rs +++ b/sqlness/src/runner.rs @@ -161,6 +161,7 @@ impl Runner { .create(true) .write(true) .read(true) + .truncate(false) .open(&result_path)?; // Read old result out for compare later