Skip to content

Commit

Permalink
refactor example (#64)
Browse files Browse the repository at this point in the history
* refactor example

* Update PR template

* update Makefile

* fix CI
  • Loading branch information
jiacai2050 authored Mar 22, 2024
1 parent 3a661b2 commit 9157ab3
Show file tree
Hide file tree
Showing 24 changed files with 215 additions and 494 deletions.
30 changes: 3 additions & 27 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,7 @@
# Which issue does this PR close?
## Rationale

Closes #

# Rationale for this change

<!---
Why are you proposing this change? If this is already explained clearly in the issue, then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.
-->
## Detailed Changes

# What changes are included in this PR?

<!---
There is no need to duplicate the description in the issue here, but it is sometimes worth providing a summary of the individual changes in this PR to help reviewers understand the structure.
-->

# Are there any user-facing changes?

<!---
Please mention if:
- there are user-facing changes that need to update the documentation or configuration.
- this is a breaking change to public APIs
-->

# How does this change test

<!--
Please describe how you test this change (like by unit test case, integration test or some other ways) if this change has touched the code.
-->
## Test Plan
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
target
Cargo.lock
.DS_Store
.project
19 changes: 5 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 0 additions & 1 deletion sqlness/examples/basic.toml

This file was deleted.

53 changes: 0 additions & 53 deletions sqlness/examples/echo.rs

This file was deleted.

10 changes: 0 additions & 10 deletions sqlness/examples/echo/interceptor-env/env.result

This file was deleted.

5 changes: 0 additions & 5 deletions sqlness/examples/echo/interceptor-env/env.sql

This file was deleted.

21 changes: 0 additions & 21 deletions sqlness/examples/echo/interceptor-replace/replace.result

This file was deleted.

12 changes: 0 additions & 12 deletions sqlness/examples/echo/interceptor-replace/replace.sql

This file was deleted.

15 changes: 0 additions & 15 deletions sqlness/examples/interceptor-arg/simple/input.result

This file was deleted.

8 changes: 0 additions & 8 deletions sqlness/examples/interceptor-arg/simple/input.sql

This file was deleted.

121 changes: 121 additions & 0 deletions sqlness/examples/interceptor-case/simple/input.result
Original file line number Diff line number Diff line change
@@ -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<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;

-- 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;

71 changes: 71 additions & 0 deletions sqlness/examples/interceptor-case/simple/input.sql
Original file line number Diff line number Diff line change
@@ -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<y>\d{4})-(?P<m>\d{2})-(?P<d>\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;
Loading

0 comments on commit 9157ab3

Please sign in to comment.