Skip to content

Commit 72047eb

Browse files
authored
Add Postgres GiST operators #17 (#18)
1 parent 524e010 commit 72047eb

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed

lib/lexer.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,15 @@ defmodule SQL.Lexer do
134134
acc = if type, do: insert_node(node(ident(type, data), line, column, data, opts), acc), else: acc
135135
lex(rest, binary, opts, line, column, nil, [], insert_node(node(type(b), line, column+1, [], opts), acc), n)
136136
end
137-
def lex(<<b::binary-size(3), rest::binary>>, binary, opts, line, column, type, data, acc, n) when b in ~w[^-= |*= <=>] do
137+
def lex(<<b::binary-size(3), rest::binary>>, binary, opts, line, column, type, data, acc, n) when b in ~w[^-= |*= <=> <-> >>= &<| <<| |>> |&> -|-] do
138138
node = node(String.to_atom(b), line, column+3, [], opts)
139139
if data == [] do
140140
lex(rest, binary, opts, line, column+3, type, data, insert_node(node, acc), n)
141141
else
142142
lex(rest, binary, opts, line, column+3, nil, [], insert_node(node, insert_node(node(ident(type, data), line, column, data, opts), acc)), n)
143143
end
144144
end
145-
def lex(<<b::binary-size(2), rest::binary>>, binary, opts, line, column, type, data, acc, n) when b in ~w[:: <> != !< !> <= >= += -= *= /= %= &= ||] do
145+
def lex(<<b::binary-size(2), rest::binary>>, binary, opts, line, column, type, data, acc, n) when b in ~w[:: <> != !< !> <= >= += -= *= /= %= &= || << &< && &> >> ~= @> <@ @@] do
146146
node = node(String.to_atom(b), line, column+2, [], opts)
147147
if data == [] do
148148
lex(rest, binary, opts, line, column+2, type, data, insert_node(node, acc), n)

lib/mix/tasks/sql.gen.parser.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,15 +414,15 @@ defmodule Mix.Tasks.Sql.Gen.Parser do
414414
acc = if type, do: insert_node(node(ident(type, data), line, column, data, opts), acc), else: acc
415415
lex(rest, binary, opts, line, column, nil, [], insert_node(node(type(b), line, column+1, [], opts), acc), n)
416416
end
417-
def lex(<<b::binary-size(3), rest::binary>>, binary, opts, line, column, type, data, acc, n) when b in ~w[^-= |*= <=>] do
417+
def lex(<<b::binary-size(3), rest::binary>>, binary, opts, line, column, type, data, acc, n) when b in ~w[^-= |*= <=> <-> >>= &<| <<| |>> |&> -|-] do
418418
node = node(String.to_atom(b), line, column+3, [], opts)
419419
if data == [] do
420420
lex(rest, binary, opts, line, column+3, type, data, insert_node(node, acc), n)
421421
else
422422
lex(rest, binary, opts, line, column+3, nil, [], insert_node(node, insert_node(node(ident(type, data), line, column, data, opts), acc)), n)
423423
end
424424
end
425-
def lex(<<b::binary-size(2), rest::binary>>, binary, opts, line, column, type, data, acc, n) when b in ~w[:: <> != !< !> <= >= += -= *= /= %= &= ||] do
425+
def lex(<<b::binary-size(2), rest::binary>>, binary, opts, line, column, type, data, acc, n) when b in ~w[:: <> != !< !> <= >= += -= *= /= %= &= || << &< && &> >> ~= @> <@ @@] do
426426
node = node(String.to_atom(b), line, column+2, [], opts)
427427
if data == [] do
428428
lex(rest, binary, opts, line, column+2, type, data, insert_node(node, acc), n)

test/adapters/postgres_test.exs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,54 @@ defmodule SQL.Adapters.PostgresTest do
263263
assert "where id <= 1" == to_string(~SQL[where id <= 1])
264264
assert "where id <= 1" == to_string(~SQL[where id<=1])
265265
end
266+
test "<<" do
267+
assert "where id << 1" == to_string(~SQL[where id << 1])
268+
end
269+
test "&<" do
270+
assert "where id &< 1" == to_string(~SQL[where id &< 1])
271+
end
272+
test "&&" do
273+
assert "where id && 1" == to_string(~SQL[where id && 1])
274+
end
275+
test "&>" do
276+
assert "where id &> 1" == to_string(~SQL[where id &> 1])
277+
end
278+
test ">>" do
279+
assert "where id >> 1" == to_string(~SQL[where id >> 1])
280+
end
281+
test "~=" do
282+
assert "where id ~= 1" == to_string(~SQL[where id ~= 1])
283+
end
284+
test "@>" do
285+
assert "where id @> 1" == to_string(~SQL[where id @> 1])
286+
end
287+
test "<@" do
288+
assert "where id <@ 1" == to_string(~SQL[where id <@ 1])
289+
end
290+
test "&<|" do
291+
assert "where id &<| 1" == to_string(~SQL[where id &<| 1])
292+
end
293+
test "<<|" do
294+
assert "where id <<| 1" == to_string(~SQL[where id <<| 1])
295+
end
296+
test "|>>" do
297+
assert "where id |>> 1" == to_string(~SQL[where id |>> 1])
298+
end
299+
test "|&>" do
300+
assert "where id |&> 1" == to_string(~SQL[where id |&> 1])
301+
end
302+
test "@@" do
303+
assert "where id @@ 1" == to_string(~SQL[where id @@ 1])
304+
end
305+
test "<->" do
306+
assert "where id <-> 1" == to_string(~SQL[where id <-> 1])
307+
end
308+
test ">>=" do
309+
assert "where id <-> 1" == to_string(~SQL[where id <-> 1])
310+
end
311+
test "-|-" do
312+
assert "where id -|- 1" == to_string(~SQL[where id -|- 1])
313+
end
266314
test "between" do
267315
assert "where id between 1 and 2" == to_string(~SQL[where id between 1 and 2])
268316
assert "where id not between 1 and 2" == to_string(~SQL[where id not between 1 and 2])

0 commit comments

Comments
 (0)