-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement ENV interceptor (#56)
Signed-off-by: Ruihang Xia <[email protected]>
- Loading branch information
Showing
13 changed files
with
315 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// 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<dyn Display> { | ||
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
-- SQLNESS ENV SECRET | ||
select 23333 from data; | ||
|
||
select 23333 from data; | ||
|
||
-- SQLNESS ENV SECRET NONEXISTENT | ||
select 23333 from data; | ||
|
||
select 23333 from data; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
-- SQLNESS ENV SECRET | ||
select {{ SECRET }} from data; | ||
|
||
-- SQLNESS ENV SECRET NONEXISTENT | ||
select {{ SECRET }} from data; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
-- 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<y>\d{4})-(?P<m>\d{2})-(?P<d>\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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
-- SQLNESS REPLACE 00 | ||
SELECT 0; | ||
|
||
-- SQLNESS REPLACE 00 | ||
SELECT 00; | ||
|
||
-- SQLNESS REPLACE 0 1 | ||
SELECT 0; | ||
|
||
-- example of capture group replacement | ||
-- SQLNESS REPLACE (?P<y>\d{4})-(?P<m>\d{2})-(?P<d>\d{2}) $m/$d/$y | ||
2012-03-14, 2013-01-01 and 2014-07-05; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
-- 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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
-- 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// 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<dyn Display> { | ||
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.