Skip to content

Commit 28581cf

Browse files
Loiccpg314
authored andcommitted
Implement LIKE
1 parent afaaf4c commit 28581cf

File tree

6 files changed

+10
-3
lines changed

6 files changed

+10
-3
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ assert_eq!(query.to_sql(true), "SELECT name, age FROM contacts");
157157
u.eq(1, 2),
158158
// Function, equivalent to {fn: "count", params: ["*"]}
159159
u.fn('count', ['*']),
160+
// Like operator
161+
u.like('text', u.string('%t%')),
160162
],
161163
// From expression (optional)
162164
from: 'a',

sqlsonnet/sql.pest

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ COMMENT = _{ "--" ~ (!"\n" ~ ANY)* ~ ("\n" | EOI) }
55
number = @{ ASCII_DIGIT+ }
66
string = { "'" ~ (!"'" ~ ANY)* ~ "'" }
77
// literal = @{ ASCII_ALPHANUMERIC+ | "*" }
8-
infix_op = { "+" | "-" | "*" | "/" | "=" | ">=" | "<=" | ">" | "<" | AND | OR | IN }
8+
infix_op = { "+" | "-" | "*" | "/" | "=" | ">=" | "<=" | ">" | "<" | AND | OR | IN | LIKE }
99
op_term = { !KEYWORD ~ infix_op ~ term }
1010
expr = { term ~ op_term* ~ as? }
1111
function = ${ identifier ~ "(" ~ exprs ~ ")" }
@@ -37,6 +37,7 @@ ASC = { ^"asc" }
3737
DESC = { ^"desc" }
3838
AS = { ^"as" }
3939
IN = { ^"in" }
40+
LIKE = { ^"like" }
4041
FROM = { ^"from" }
4142
GROUP_BY = { ^"group by" }
4243
ORDER_BY = { ^"order by" }

sqlsonnet/sqlsonnet.libsonnet

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
gt(a, b): [a, '>', b],
2121
lt(a, b): [a, '<', b],
2222
leq(a, b): [a, '<=', b],
23+
like(a, b): [a, 'LIKE', b],
2324
in_(a, b): [a, 'IN', b],
2425
not(expr): ['NOT', expr],
2526
// expr AS as, overriding existing aliases.

sqlsonnet/tests/data/example.jsonnet

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
u.fn('count', ['*']),
2222
// Negation
2323
u.not(u.neq(1, 2)),
24+
// Like operator
25+
u.like('text', u.string('%t%')),
2426
],
2527
// From expression (optional)
2628
// Table name

sqlsonnet/tests/data/example.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ SELECT
88
1 + 2,
99
1 = 2,
1010
count(*),
11-
NOT (1 != 2)
11+
NOT (1 != 2),
12+
text LIKE '%t%'
1213
FROM a
1314
SAMPLE 100
1415
JOIN b

sqlsonnet/tests/data/test.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ JOIN table7
3333
USING
3434
c
3535
WHERE
36-
(a = b) AND TRUE AND 1 = 1
36+
(a = b) AND TRUE AND 1 = 1 AND a LIKE '%a%'
3737
GROUP BY
3838
(a + b) AS c,
3939
d

0 commit comments

Comments
 (0)