From 078d0dd80571164f03ed9bdd3ca15c8cd7c1aac2 Mon Sep 17 00:00:00 2001 From: Igor Barchenkov Date: Thu, 26 Jun 2025 10:07:42 +0300 Subject: [PATCH] Add Postgres GiST operators #17 --- lib/lexer.ex | 4 +-- lib/mix/tasks/sql.gen.parser.ex | 4 +-- test/adapters/postgres_test.exs | 48 +++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 4 deletions(-) diff --git a/lib/lexer.ex b/lib/lexer.ex index 36c36b6..463cc08 100644 --- a/lib/lexer.ex +++ b/lib/lexer.ex @@ -134,7 +134,7 @@ defmodule SQL.Lexer do acc = if type, do: insert_node(node(ident(type, data), line, column, data, opts), acc), else: acc lex(rest, binary, opts, line, column, nil, [], insert_node(node(type(b), line, column+1, [], opts), acc), n) end - def lex(<>, binary, opts, line, column, type, data, acc, n) when b in ~w[^-= |*= <=>] do + def lex(<>, binary, opts, line, column, type, data, acc, n) when b in ~w[^-= |*= <=> <-> >>= &<| <<| |>> |&> -|-] do node = node(String.to_atom(b), line, column+3, [], opts) if data == [] do lex(rest, binary, opts, line, column+3, type, data, insert_node(node, acc), n) @@ -142,7 +142,7 @@ defmodule SQL.Lexer do lex(rest, binary, opts, line, column+3, nil, [], insert_node(node, insert_node(node(ident(type, data), line, column, data, opts), acc)), n) end end - def lex(<>, binary, opts, line, column, type, data, acc, n) when b in ~w[:: <> != !< !> <= >= += -= *= /= %= &= ||] do + def lex(<>, binary, opts, line, column, type, data, acc, n) when b in ~w[:: <> != !< !> <= >= += -= *= /= %= &= || << &< && &> >> ~= @> <@ @@] do node = node(String.to_atom(b), line, column+2, [], opts) if data == [] do lex(rest, binary, opts, line, column+2, type, data, insert_node(node, acc), n) diff --git a/lib/mix/tasks/sql.gen.parser.ex b/lib/mix/tasks/sql.gen.parser.ex index 68453fd..61667e1 100644 --- a/lib/mix/tasks/sql.gen.parser.ex +++ b/lib/mix/tasks/sql.gen.parser.ex @@ -414,7 +414,7 @@ defmodule Mix.Tasks.Sql.Gen.Parser do acc = if type, do: insert_node(node(ident(type, data), line, column, data, opts), acc), else: acc lex(rest, binary, opts, line, column, nil, [], insert_node(node(type(b), line, column+1, [], opts), acc), n) end - def lex(<>, binary, opts, line, column, type, data, acc, n) when b in ~w[^-= |*= <=>] do + def lex(<>, binary, opts, line, column, type, data, acc, n) when b in ~w[^-= |*= <=> <-> >>= &<| <<| |>> |&> -|-] do node = node(String.to_atom(b), line, column+3, [], opts) if data == [] do lex(rest, binary, opts, line, column+3, type, data, insert_node(node, acc), n) @@ -422,7 +422,7 @@ defmodule Mix.Tasks.Sql.Gen.Parser do lex(rest, binary, opts, line, column+3, nil, [], insert_node(node, insert_node(node(ident(type, data), line, column, data, opts), acc)), n) end end - def lex(<>, binary, opts, line, column, type, data, acc, n) when b in ~w[:: <> != !< !> <= >= += -= *= /= %= &= ||] do + def lex(<>, binary, opts, line, column, type, data, acc, n) when b in ~w[:: <> != !< !> <= >= += -= *= /= %= &= || << &< && &> >> ~= @> <@ @@] do node = node(String.to_atom(b), line, column+2, [], opts) if data == [] do lex(rest, binary, opts, line, column+2, type, data, insert_node(node, acc), n) diff --git a/test/adapters/postgres_test.exs b/test/adapters/postgres_test.exs index 60cdb3d..3a262fd 100644 --- a/test/adapters/postgres_test.exs +++ b/test/adapters/postgres_test.exs @@ -263,6 +263,54 @@ defmodule SQL.Adapters.PostgresTest do assert "where id <= 1" == to_string(~SQL[where id <= 1]) assert "where id <= 1" == to_string(~SQL[where id<=1]) end + test "<<" do + assert "where id << 1" == to_string(~SQL[where id << 1]) + end + test "&<" do + assert "where id &< 1" == to_string(~SQL[where id &< 1]) + end + test "&&" do + assert "where id && 1" == to_string(~SQL[where id && 1]) + end + test "&>" do + assert "where id &> 1" == to_string(~SQL[where id &> 1]) + end + test ">>" do + assert "where id >> 1" == to_string(~SQL[where id >> 1]) + end + test "~=" do + assert "where id ~= 1" == to_string(~SQL[where id ~= 1]) + end + test "@>" do + assert "where id @> 1" == to_string(~SQL[where id @> 1]) + end + test "<@" do + assert "where id <@ 1" == to_string(~SQL[where id <@ 1]) + end + test "&<|" do + assert "where id &<| 1" == to_string(~SQL[where id &<| 1]) + end + test "<<|" do + assert "where id <<| 1" == to_string(~SQL[where id <<| 1]) + end + test "|>>" do + assert "where id |>> 1" == to_string(~SQL[where id |>> 1]) + end + test "|&>" do + assert "where id |&> 1" == to_string(~SQL[where id |&> 1]) + end + test "@@" do + assert "where id @@ 1" == to_string(~SQL[where id @@ 1]) + end + test "<->" do + assert "where id <-> 1" == to_string(~SQL[where id <-> 1]) + end + test ">>=" do + assert "where id <-> 1" == to_string(~SQL[where id <-> 1]) + end + test "-|-" do + assert "where id -|- 1" == to_string(~SQL[where id -|- 1]) + end test "between" do assert "where id between 1 and 2" == to_string(~SQL[where id between 1 and 2]) assert "where id not between 1 and 2" == to_string(~SQL[where id not between 1 and 2])