Packages

An Elixir interface to the libpg_query postgres query parser. Converts a valid PostgreSQL query into a Protobuf based AST.

Current section

Files

Jump to
pg_query_ex lib pg_query proto pg_query_row_expr.ex
Raw

lib/pg_query/proto/pg_query_row_expr.ex

# Code generated by protox. Don't edit.
# credo:disable-for-this-file
defmodule PgQuery.RowExpr do
@moduledoc false
if function_exported?(Protox, :check_generator_version, 1) do
Protox.check_generator_version(1)
else
raise "This code was generated with protox 2 but the runtime is using an older version of protox."
end
@type t :: %__MODULE__{
location: integer(),
colnames: [PgQuery.Node.t()],
row_format: atom(),
row_typeid: non_neg_integer(),
args: [PgQuery.Node.t()],
xpr: PgQuery.Node.t() | nil,
__uf__: [{non_neg_integer(), Protox.Types.tag(), binary()}]
}
defstruct location: 0,
colnames: [],
row_format: :COERCION_FORM_UNDEFINED,
row_typeid: 0,
args: [],
xpr: nil,
__uf__: []
(
(
@spec encode(t()) :: {:ok, iodata(), non_neg_integer()} | {:error, any()}
def encode(msg) do
msg |> encode!() |> Tuple.insert_at(0, :ok)
rescue
e in [Protox.EncodingError, Protox.RequiredFieldsError] -> {:error, e}
end
@spec encode!(t()) :: {iodata(), non_neg_integer()} | no_return()
def encode!(msg) do
{_acc = [], _acc_size = 0}
|> encode_location(msg)
|> encode_colnames(msg)
|> encode_row_format(msg)
|> encode_row_typeid(msg)
|> encode_args(msg)
|> encode_xpr(msg)
|> encode_unknown_fields(msg)
end
)
defp encode_location({acc, acc_size}, msg) do
if msg.location == 0 do
{acc, acc_size}
else
{value_bytes, value_bytes_size} = Protox.Encode.encode_int32(msg.location)
{["0", value_bytes | acc], acc_size + 1 + value_bytes_size}
end
rescue
ArgumentError ->
reraise Protox.EncodingError.new(:location, "invalid field value"), __STACKTRACE__
end
defp encode_colnames({acc, acc_size}, msg) do
case msg.colnames do
[] ->
{acc, acc_size}
values ->
{value_bytes, value_size} =
(
{value_bytes, value_size} =
Enum.reduce(values, {_local_acc = [], _local_acc_size = 0}, fn value,
{local_acc,
local_acc_size} ->
{value_bytes, value_bytes_size} = Protox.Encode.encode_message(value)
{[value_bytes, "*" | local_acc], local_acc_size + 1 + value_bytes_size}
end)
{Enum.reverse(value_bytes), value_size}
)
{[value_bytes | acc], acc_size + value_size}
end
rescue
ArgumentError ->
reraise Protox.EncodingError.new(:colnames, "invalid field value"), __STACKTRACE__
end
defp encode_row_format({acc, acc_size}, msg) do
if msg.row_format == :COERCION_FORM_UNDEFINED do
{acc, acc_size}
else
{value_bytes, value_bytes_size} =
msg.row_format |> PgQuery.CoercionForm.encode() |> Protox.Encode.encode_enum()
{[" ", value_bytes | acc], acc_size + 1 + value_bytes_size}
end
rescue
ArgumentError ->
reraise Protox.EncodingError.new(:row_format, "invalid field value"), __STACKTRACE__
end
defp encode_row_typeid({acc, acc_size}, msg) do
if msg.row_typeid == 0 do
{acc, acc_size}
else
{value_bytes, value_bytes_size} = Protox.Encode.encode_uint32(msg.row_typeid)
{["\x18", value_bytes | acc], acc_size + 1 + value_bytes_size}
end
rescue
ArgumentError ->
reraise Protox.EncodingError.new(:row_typeid, "invalid field value"), __STACKTRACE__
end
defp encode_args({acc, acc_size}, msg) do
case msg.args do
[] ->
{acc, acc_size}
values ->
{value_bytes, value_size} =
(
{value_bytes, value_size} =
Enum.reduce(values, {_local_acc = [], _local_acc_size = 0}, fn value,
{local_acc,
local_acc_size} ->
{value_bytes, value_bytes_size} = Protox.Encode.encode_message(value)
{[value_bytes, "\x12" | local_acc], local_acc_size + 1 + value_bytes_size}
end)
{Enum.reverse(value_bytes), value_size}
)
{[value_bytes | acc], acc_size + value_size}
end
rescue
ArgumentError ->
reraise Protox.EncodingError.new(:args, "invalid field value"), __STACKTRACE__
end
defp encode_xpr({acc, acc_size}, msg) do
if msg.xpr == nil do
{acc, acc_size}
else
{value_bytes, value_bytes_size} = Protox.Encode.encode_message(msg.xpr)
{["\n", value_bytes | acc], acc_size + 1 + value_bytes_size}
end
rescue
ArgumentError ->
reraise Protox.EncodingError.new(:xpr, "invalid field value"), __STACKTRACE__
end
defp encode_unknown_fields({acc, acc_size}, msg) do
Enum.reduce(msg.__uf__, {acc, acc_size}, fn {tag, wire_type, bytes}, {acc, acc_size} ->
case wire_type do
0 ->
{key_bytes, key_size} = Protox.Encode.make_key_bytes(tag, :int32)
{[acc, <<key_bytes::binary, bytes::binary>>], acc_size + key_size + byte_size(bytes)}
1 ->
{key_bytes, key_size} = Protox.Encode.make_key_bytes(tag, :double)
{[acc, <<key_bytes::binary, bytes::binary>>], acc_size + key_size + byte_size(bytes)}
2 ->
{len_bytes, len_size} = bytes |> byte_size() |> Protox.Varint.encode()
{key_bytes, key_size} = Protox.Encode.make_key_bytes(tag, :packed)
{[acc, <<key_bytes::binary, len_bytes::binary, bytes::binary>>],
acc_size + key_size + len_size + byte_size(bytes)}
5 ->
{key_bytes, key_size} = Protox.Encode.make_key_bytes(tag, :float)
{[acc, <<key_bytes::binary, bytes::binary>>], acc_size + key_size + byte_size(bytes)}
end
end)
end
)
(
(
@spec decode(binary()) :: {:ok, t()} | {:error, any()}
def decode(bytes) do
{:ok, decode!(bytes)}
rescue
e in [Protox.DecodingError, Protox.IllegalTagError, Protox.RequiredFieldsError] ->
{:error, e}
end
(
@spec decode!(binary()) :: t() | no_return()
def decode!(bytes) do
parse_key_value(bytes, struct(PgQuery.RowExpr))
end
)
)
(
@spec parse_key_value(binary(), struct()) :: struct()
defp parse_key_value(<<>>, msg) do
msg
end
defp parse_key_value(bytes, msg) do
{field, rest} =
case bytes do
<<_::5, 3::3, _rest::binary>> ->
raise Protox.DecodingError.new(bytes, "invalid wire type 3")
<<_::5, 4::3, _rest::binary>> ->
raise Protox.DecodingError.new(bytes, "invalid wire type 4")
<<_::5, 6::3, _rest::binary>> ->
raise Protox.DecodingError.new(bytes, "invalid wire type 6")
<<_::5, 7::3, _rest::binary>> ->
raise Protox.DecodingError.new(bytes, "invalid wire type 7")
<<0::5, _::3, _rest::binary>> ->
raise %Protox.IllegalTagError{}
<<6::5, _wire_type::3, bytes::binary>> ->
{value, rest} = Protox.Decode.parse_int32(bytes)
{[location: value], rest}
<<5::5, _wire_type::3, bytes::binary>> ->
{len, bytes} = Protox.Varint.decode(bytes)
{delimited, rest} = Protox.Decode.parse_delimited(bytes, len)
{[colnames: msg.colnames ++ [PgQuery.Node.decode!(delimited)]], rest}
<<4::5, _wire_type::3, bytes::binary>> ->
{value, rest} = Protox.Decode.parse_enum(bytes, PgQuery.CoercionForm)
{[row_format: value], rest}
<<3::5, _wire_type::3, bytes::binary>> ->
{value, rest} = Protox.Decode.parse_uint32(bytes)
{[row_typeid: value], rest}
<<2::5, _wire_type::3, bytes::binary>> ->
{len, bytes} = Protox.Varint.decode(bytes)
{delimited, rest} = Protox.Decode.parse_delimited(bytes, len)
{[args: msg.args ++ [PgQuery.Node.decode!(delimited)]], rest}
<<1::5, _wire_type::3, bytes::binary>> ->
{len, bytes} = Protox.Varint.decode(bytes)
{delimited, rest} = Protox.Decode.parse_delimited(bytes, len)
{[xpr: Protox.MergeMessage.merge(msg.xpr, PgQuery.Node.decode!(delimited))], rest}
<<bytes::binary>> ->
{tag, wire_type, rest} = Protox.Decode.parse_key(bytes)
{value, rest} = Protox.Decode.parse_unknown(tag, wire_type, rest)
{[__uf__: msg.__uf__ ++ [value]], rest}
end
msg_updated = struct(msg, field)
parse_key_value(rest, msg_updated)
end
)
)
(
@spec unknown_fields(struct()) :: [{non_neg_integer(), Protox.Types.tag(), binary()}]
def unknown_fields(msg) do
msg.__uf__
end
@spec unknown_fields_name() :: :__uf__
def unknown_fields_name() do
:__uf__
end
@spec clear_unknown_fields(struct) :: struct
def clear_unknown_fields(msg) do
struct!(msg, __uf__: [])
end
)
(
@spec default(atom()) ::
{:ok, boolean() | integer() | String.t() | float()}
| {:error, :no_such_field | :no_default_value}
def default(:location) do
{:ok, 0}
end
def default(:colnames) do
{:error, :no_default_value}
end
def default(:row_format) do
{:ok, :COERCION_FORM_UNDEFINED}
end
def default(:row_typeid) do
{:ok, 0}
end
def default(:args) do
{:error, :no_default_value}
end
def default(:xpr) do
{:ok, nil}
end
def default(_) do
{:error, :no_such_field}
end
)
@spec schema() :: Protox.MessageSchema.t()
def schema() do
%{
__struct__: Protox.MessageSchema,
fields: %{
args: %{
__struct__: Protox.Field,
extender: nil,
kind: :unpacked,
label: :repeated,
name: :args,
tag: 2,
type: {:message, PgQuery.Node}
},
colnames: %{
__struct__: Protox.Field,
extender: nil,
kind: :unpacked,
label: :repeated,
name: :colnames,
tag: 5,
type: {:message, PgQuery.Node}
},
location: %{
__struct__: Protox.Field,
extender: nil,
kind: %{__struct__: Protox.Scalar, default_value: 0},
label: :optional,
name: :location,
tag: 6,
type: :int32
},
row_format: %{
__struct__: Protox.Field,
extender: nil,
kind: %{__struct__: Protox.Scalar, default_value: :COERCION_FORM_UNDEFINED},
label: :optional,
name: :row_format,
tag: 4,
type: {:enum, PgQuery.CoercionForm}
},
row_typeid: %{
__struct__: Protox.Field,
extender: nil,
kind: %{__struct__: Protox.Scalar, default_value: 0},
label: :optional,
name: :row_typeid,
tag: 3,
type: :uint32
},
xpr: %{
__struct__: Protox.Field,
extender: nil,
kind: %{__struct__: Protox.Scalar, default_value: nil},
label: :optional,
name: :xpr,
tag: 1,
type: {:message, PgQuery.Node}
}
},
file_options: nil,
name: PgQuery.RowExpr,
syntax: :proto3
}
end
end