Skip to content

Add ltxtquery support #735

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ jobs:
otp-version: ${{matrix.pair.otp}}
elixir-version: ${{matrix.pair.elixir}}

- uses: actions/cache@v2
- uses: actions/cache@v4
with:
path: |
deps
Expand Down
37 changes: 37 additions & 0 deletions lib/postgrex/extensions/ltxtquery.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
defmodule Postgrex.Extensions.Ltxtquery do
@moduledoc false
import Postgrex.BinaryUtils, warn: false
use Postgrex.BinaryExtension, type: "ltxtquery"

@impl true
def init(opts), do: Keyword.get(opts, :decode_binary, :copy)

# ltxtquery binary formats are versioned
# https://github.com/postgres/postgres/blob/master/contrib/ltree/ltxtquery_io.c
@impl true
def encode(_state) do
quote location: :keep, generated: true do
bin when is_binary(bin) ->
version = 1
size = byte_size(bin) + 1
[<<size::int32(), version::int8()>> | bin]
end
end

@impl true
def decode(:reference) do
quote location: :keep do
<<len::int32(), bin::binary-size(len)>> ->
<<_version::int8(), ltxtquery::binary>> = bin
ltxtquery
end
end

def decode(:copy) do
quote location: :keep do
<<len::int32(), bin::binary-size(len)>> ->
<<_version::int8(), ltxtquery::binary>> = bin
:binary.copy(ltxtquery)
end
end
end
1 change: 1 addition & 0 deletions lib/postgrex/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ defmodule Postgrex.Utils do
Postgrex.Extensions.LineSegment,
Postgrex.Extensions.Lquery,
Postgrex.Extensions.Ltree,
Postgrex.Extensions.Ltxtquery,
Postgrex.Extensions.MACADDR,
Postgrex.Extensions.Multirange,
Postgrex.Extensions.Name,
Expand Down
12 changes: 12 additions & 0 deletions test/query_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,12 @@ defmodule QueryTest do
assert [[ltree]] == query("SELECT '#{ltree}'::ltree", [])
end

@tag min_pg_version: "13.0"
test "decode ltxtquery", context do
ltxtquery = "Europe% & Russia@* & !( Transportation & Test )"
assert [[ltxtquery]] == query("SELECT '#{ltxtquery}'::ltxtquery", [])
end

test "encode oid and its aliases", context do
# oid's range is 0 to 4294967295
assert [[0]] = query("select $1::oid;", [0])
Expand Down Expand Up @@ -1533,6 +1539,12 @@ defmodule QueryTest do
assert [[ltree]] == query("SELECT $1::ltree", [ltree])
end

@tag min_pg_version: "13.0"
test "encode ltxtquery", context do
ltxtquery = "Europe% & Russia@* & !( Transportation & Test )"
assert [[ltxtquery]] == query("SELECT $1::ltxtquery", [ltxtquery])
end

test "fail on encode arrays", context do
assert_raise ArgumentError, "nested lists must have lists with matching lengths", fn ->
query("SELECT $1::integer[]", [[[1], [1, 2]]])
Expand Down
Loading