-module(pgl@internal@decode). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/pgl/internal/decode.gleam"). -export([message/2]). -if(?OTP_RELEASE >= 27). -define(MODULEDOC(Str), -moduledoc(Str)). -define(DOC(Str), -doc(Str)). -else. -define(MODULEDOC(Str), -compile([])). -define(DOC(Str), -compile([])). -endif. ?MODULEDOC(false). -file("src/pgl/internal/decode.gleam", 44). ?DOC(false). -spec close_complete(bitstring()) -> {ok, pgl@internal:message()} | {error, pgl@internal:internal_error()}. close_complete(Payload) -> case Payload of <<>> -> {ok, close_complete}; _ -> _pipe = decoding_error, _pipe@1 = {protocol_error, _pipe, <<"Unexpected payload for CloseComplete"/utf8>>}, {error, _pipe@1} end. -file("src/pgl/internal/decode.gleam", 57). ?DOC(false). -spec empty_query_response(bitstring()) -> {ok, pgl@internal:message()} | {error, pgl@internal:internal_error()}. empty_query_response(Payload) -> case Payload of <<>> -> {ok, empty_query_response}; _ -> _pipe = decoding_error, _pipe@1 = {protocol_error, _pipe, <<"Unexpected payload for EmptyQueryResponse"/utf8>>}, {error, _pipe@1} end. -file("src/pgl/internal/decode.gleam", 72). ?DOC(false). -spec copy_done(bitstring()) -> {ok, pgl@internal:message()} | {error, pgl@internal:internal_error()}. copy_done(Payload) -> case Payload of <<>> -> {ok, copy_done}; _ -> _pipe = decoding_error, _pipe@1 = {protocol_error, _pipe, <<"Unexpected payload for CopyDone"/utf8>>}, {error, _pipe@1} end. -file("src/pgl/internal/decode.gleam", 85). ?DOC(false). -spec portal_suspended(bitstring()) -> {ok, pgl@internal:message()} | {error, pgl@internal:internal_error()}. portal_suspended(Payload) -> case Payload of <<>> -> {ok, portal_suspended}; _ -> _pipe = decoding_error, _pipe@1 = {protocol_error, _pipe, <<"Unexpected payload for PortalSuspended"/utf8>>}, {error, _pipe@1} end. -file("src/pgl/internal/decode.gleam", 100). ?DOC(false). -spec copy_data(bitstring()) -> {ok, pgl@internal:message()} | {error, pgl@internal:internal_error()}. copy_data(Data) -> {ok, {copy_data, Data}}. -file("src/pgl/internal/decode.gleam", 120). ?DOC(false). -spec data_row_values( bitstring(), integer(), list(gleam@option:option(bitstring())) ) -> {ok, list(gleam@option:option(bitstring()))} | {error, pgl@internal:internal_error()}. data_row_values(Payload, Columns, Acc) -> case Columns > 0 of false -> {ok, lists:reverse(Acc)}; true -> case Payload of <<-1:32/signed-integer, Rest/bitstring>> -> data_row_values(Rest, Columns - 1, [none | Acc]); <> -> data_row_values(Rest@1, Columns - 1, [{some, Value} | Acc]); _ -> _pipe = decoding_error, _pipe@1 = {protocol_error, _pipe, <<"Invalid data row"/utf8>>}, {error, _pipe@1} end end. -file("src/pgl/internal/decode.gleam", 104). ?DOC(false). -spec data_row(bitstring()) -> {ok, pgl@internal:message()} | {error, pgl@internal:internal_error()}. data_row(Payload) -> case Payload of <> -> _pipe = data_row_values(Rest, Columns, []), gleam@result:map(_pipe, fun(Field@0) -> {data_row, Field@0} end); _ -> _pipe@1 = decoding_error, _pipe@2 = {protocol_error, _pipe@1, <<"Unexpected payload for DataRow"/utf8>>}, {error, _pipe@2} end. -file("src/pgl/internal/decode.gleam", 145). ?DOC(false). -spec backend_key_data(bitstring()) -> {ok, pgl@internal:message()} | {error, pgl@internal:internal_error()}. backend_key_data(Payload) -> case Payload of <> -> {ok, {backend_key_data, Proc_id, Secret}}; _ -> _pipe = decoding_error, _pipe@1 = {protocol_error, _pipe, <<"Unexpected payload for BackendKeyData"/utf8>>}, {error, _pipe@1} end. -file("src/pgl/internal/decode.gleam", 161). ?DOC(false). -spec parameter_status(bitstring()) -> {ok, pgl@internal:message()} | {error, pgl@internal:internal_error()}. parameter_status(Payload) -> _pipe = Payload, _pipe@1 = gleam@bit_array:to_string(_pipe), _pipe@3 = gleam@result:map_error( _pipe@1, fun(_) -> _pipe@2 = decoding_error, {protocol_error, _pipe@2, <<"Unexpected payload for ParameterStatus"/utf8>>} end ), gleam@result:'try'( _pipe@3, fun(Str) -> case gleam@string:split(Str, <<"\x{0000}"/utf8>>) of [Name, Value, _] -> {ok, {parameter_status, Name, Value}}; _ -> _pipe@4 = decoding_error, _pipe@5 = {protocol_error, _pipe@4, <<"Unexpected payload for ParameterStatus"/utf8>>}, {error, _pipe@5} end end ). -file("src/pgl/internal/decode.gleam", 223). ?DOC(false). -spec sasl_methods_inner(bitstring()) -> {ok, list(binary())} | {error, pgl@internal:internal_error()}. sasl_methods_inner(Binary) -> case Binary of <<"SCRAM-SHA-256-PLUS"/utf8, 0, "SCRAM-SHA-256"/utf8, 0, _/bitstring>> -> {ok, [<<"SCRAM-SHA-256"/utf8>>]}; <<"SCRAM-SHA-256"/utf8, 0, _/bitstring>> -> {ok, [<<"SCRAM-SHA-256"/utf8>>]}; _ -> _pipe = method_not_implemented, _pipe@1 = {authentication_error, _pipe, <<"Supported methods: [SCRAM-SHA-256]"/utf8>>}, {error, _pipe@1} end. -file("src/pgl/internal/decode.gleam", 214). ?DOC(false). -spec sasl_methods(bitstring()) -> {ok, list(binary())} | {error, pgl@internal:internal_error()}. sasl_methods(Methods_bin) -> case erlang:byte_size(Methods_bin) of 0 -> {ok, []}; _ -> sasl_methods_inner(Methods_bin) end. -file("src/pgl/internal/decode.gleam", 184). ?DOC(false). -spec authentication(bitstring()) -> {ok, pgl@internal:message()} | {error, pgl@internal:internal_error()}. authentication(Payload) -> case Payload of <<0:32/integer>> -> {ok, authentication_ok}; <<2:32/integer>> -> {ok, authentication_kerberos_v5}; <<3:32/integer>> -> {ok, authentication_cleartext_password}; <<5:32/integer, Salt:32/bitstring>> -> {ok, {authentication_m_d5_password, Salt}}; <<6:32/integer>> -> {ok, authentication_s_c_m}; <<7:32/integer>> -> {ok, authentication_g_s_s}; <<8:32/integer, Data/bitstring>> -> {ok, {authentication_g_s_s_continue, Data}}; <<9:32/integer>> -> {ok, authentication_s_s_p_i}; <<10:32/integer, Methods_bin/bitstring>> -> _pipe = sasl_methods(Methods_bin), gleam@result:map( _pipe, fun(Methods) -> {authentication_s_a_s_l, Methods} end ); <<11:32/integer, Server_first/bitstring>> -> {ok, {authentication_s_a_s_l_continue, Server_first}}; <<12:32/integer, Server_final/bitstring>> -> {ok, {authentication_s_a_s_l_final, Server_final}}; _ -> _pipe@1 = decoding_error, _pipe@2 = {protocol_error, _pipe@1, <<"Unexpected authentication payload"/utf8>>}, {error, _pipe@2} end. -file("src/pgl/internal/decode.gleam", 237). ?DOC(false). -spec bind_complete(bitstring()) -> {ok, pgl@internal:message()} | {error, pgl@internal:internal_error()}. bind_complete(Payload) -> case Payload of <<>> -> {ok, bind_complete}; _ -> _pipe = decoding_error, _pipe@1 = {protocol_error, _pipe, <<"Unexpected payload for BindComplete"/utf8>>}, {error, _pipe@1} end. -file("src/pgl/internal/decode.gleam", 283). ?DOC(false). -spec num_rows_from_command(pgl@internal:command()) -> integer(). num_rows_from_command(Cmd) -> case Cmd of {select, Num} -> Num; {insert, Num@1} -> Num@1; {update, Num@2} -> Num@2; {delete, Num@3} -> Num@3; {fetch, Num@4} -> Num@4; {move, Num@5} -> Num@5; {copy, Num@6} -> Num@6; 'begin' -> 0; commit -> 0; rollback -> 0; {other, _} -> 0 end. -file("src/pgl/internal/decode.gleam", 321). ?DOC(false). -spec parse_num_rows( binary(), binary(), fun((integer()) -> pgl@internal:command()) ) -> {ok, pgl@internal:command()} | {error, pgl@internal:internal_error()}. parse_num_rows(Name, Num, Command) -> _pipe = gleam_stdlib:parse_int(Num), _pipe@2 = gleam@result:map_error(_pipe, fun(_) -> _pipe@1 = decoding_error, {protocol_error, _pipe@1, <<<<"Invalid "/utf8, Name/binary>>/binary, " row count"/utf8>>} end), gleam@result:map(_pipe@2, Command). -file("src/pgl/internal/decode.gleam", 299). ?DOC(false). -spec to_tag(binary()) -> {ok, pgl@internal:command()} | {error, pgl@internal:internal_error()}. to_tag(Value) -> case Value of <<"SELECT "/utf8, Num/binary>> -> parse_num_rows( <<"SELECT"/utf8>>, Num, fun(Field@0) -> {select, Field@0} end ); <<"INSERT 0 "/utf8, Num@1/binary>> -> parse_num_rows( <<"INSERT"/utf8>>, Num@1, fun(Field@0) -> {insert, Field@0} end ); <<"UPDATE "/utf8, Num@2/binary>> -> parse_num_rows( <<"UPDATE"/utf8>>, Num@2, fun(Field@0) -> {update, Field@0} end ); <<"DELETE "/utf8, Num@3/binary>> -> parse_num_rows( <<"DELETE"/utf8>>, Num@3, fun(Field@0) -> {delete, Field@0} end ); <<"FETCH "/utf8, Num@4/binary>> -> parse_num_rows( <<"FETCH"/utf8>>, Num@4, fun(Field@0) -> {fetch, Field@0} end ); <<"MOVE "/utf8, Num@5/binary>> -> parse_num_rows( <<"MOVE"/utf8>>, Num@5, fun(Field@0) -> {move, Field@0} end ); <<"COPY "/utf8, Num@6/binary>> -> parse_num_rows( <<"COPY"/utf8>>, Num@6, fun(Field@0) -> {copy, Field@0} end ); <<"BEGIN"/utf8>> -> {ok, 'begin'}; <<"COMMIT"/utf8>> -> {ok, commit}; <<"ROLLBACK"/utf8>> -> {ok, rollback}; Other -> {ok, {other, Other}} end. -file("src/pgl/internal/decode.gleam", 264). ?DOC(false). -spec command_complete(bitstring()) -> {ok, pgl@internal:message()} | {error, pgl@internal:internal_error()}. command_complete(Payload) -> Len = erlang:byte_size(Payload) - 1, _pipe = gleam_stdlib:bit_array_slice(Payload, 0, Len), _pipe@1 = gleam@result:'try'(_pipe, fun gleam@bit_array:to_string/1), _pipe@3 = gleam@result:map_error( _pipe@1, fun(_) -> _pipe@2 = decoding_error, {protocol_error, _pipe@2, <<"Unexpected payload for CommandComplete"/utf8>>} end ), _pipe@4 = gleam@result:'try'(_pipe@3, fun to_tag/1), gleam@result:map( _pipe@4, fun(Command) -> Rows = num_rows_from_command(Command), {command_complete, Command, Rows} end ). -file("src/pgl/internal/decode.gleam", 334). ?DOC(false). -spec ready_for_query(bitstring()) -> {ok, pgl@internal:message()} | {error, pgl@internal:internal_error()}. ready_for_query(Payload) -> case Payload of <<"I"/utf8>> -> {ok, {ready_for_query, idle}}; <<"T"/utf8>> -> {ok, {ready_for_query, transaction}}; <<"E"/utf8>> -> {ok, {ready_for_query, err}}; _ -> _pipe = message_error, _pipe@1 = {protocol_error, _pipe, <<"Unexpected payload for ReadyForQuery"/utf8>>}, {error, _pipe@1} end. -file("src/pgl/internal/decode.gleam", 373). ?DOC(false). -spec decode_string(bitstring()) -> {ok, {binary(), bitstring()}} | {error, pgl@internal:internal_error()}. decode_string(Bits) -> case pgl_ffi:binary_match(Bits, <<0>>) of {ok, {Start, _}} -> case erlang:split_binary(Bits, Start) of {<>, <<0, Rest/bitstring>>} -> _pipe = gleam@bit_array:to_string(Str), _pipe@1 = gleam@result:map( _pipe, fun(Str1) -> {Str1, Rest} end ), gleam@result:map_error( _pipe@1, fun(_) -> _pipe@2 = decoding_error, {protocol_error, _pipe@2, <<"Failed to parse binary"/utf8>>} end ); _ -> _pipe@3 = decoding_error, _pipe@4 = {protocol_error, _pipe@3, <<"Failed to parse binary"/utf8>>}, {error, _pipe@4} end; {error, _} -> _pipe@5 = decoding_error, _pipe@6 = {protocol_error, _pipe@5, <<"Failed to decode string"/utf8>>}, {error, _pipe@6} end. -file("src/pgl/internal/decode.gleam", 349). ?DOC(false). -spec error_and_notice_message_fields( bitstring(), gleam@dict:dict(bitstring(), binary()) ) -> {ok, gleam@dict:dict(bitstring(), binary())} | {error, pgl@internal:internal_error()}. error_and_notice_message_fields(Payload, Acc) -> case Payload of <<0>> -> {ok, Acc}; <> -> case decode_string(Rest) of {ok, {Field_string, Rest1}} -> Acc1 = gleam@dict:insert(Acc, Field, Field_string), error_and_notice_message_fields(Rest1, Acc1); {error, Err} -> {error, Err} end; _ -> _pipe = message_error, _pipe@1 = {protocol_error, _pipe, <<"Unexpected message format"/utf8>>}, {error, _pipe@1} end. -file("src/pgl/internal/decode.gleam", 250). ?DOC(false). -spec error_response(bitstring()) -> {ok, pgl@internal:message()} | {error, pgl@internal:internal_error()}. error_response(Payload) -> _pipe = error_and_notice_message_fields(Payload, maps:new()), gleam@result:map(_pipe, fun(Field@0) -> {error_response, Field@0} end). -file("src/pgl/internal/decode.gleam", 257). ?DOC(false). -spec notice_response(bitstring()) -> {ok, pgl@internal:message()} | {error, pgl@internal:internal_error()}. notice_response(Payload) -> _pipe = error_and_notice_message_fields(Payload, maps:new()), gleam@result:map(_pipe, fun(Field@0) -> {notice_response, Field@0} end). -file("src/pgl/internal/decode.gleam", 408). ?DOC(false). -spec no_data(bitstring()) -> {ok, pgl@internal:message()} | {error, pgl@internal:internal_error()}. no_data(Payload) -> case Payload of <<>> -> {ok, no_data}; _ -> _pipe = decoding_error, _pipe@1 = {protocol_error, _pipe, <<"Unexpected payload for NoData"/utf8>>}, {error, _pipe@1} end. -file("src/pgl/internal/decode.gleam", 491). ?DOC(false). -spec decode_format_code(integer()) -> {ok, pgl@internal:pg_sql_format()} | {error, pgl@internal:internal_error()}. decode_format_code(Code) -> case Code of 0 -> {ok, text}; 1 -> {ok, binary}; _ -> _pipe = decoding_error, _pipe@1 = {protocol_error, _pipe, <<"Unexpected format code"/utf8>>}, {error, _pipe@1} end. -file("src/pgl/internal/decode.gleam", 441). ?DOC(false). -spec row_description_fields( integer(), bitstring(), list(pgl@internal:row_description_field()) ) -> {ok, list(pgl@internal:row_description_field())} | {error, pgl@internal:internal_error()}. row_description_fields(Count, Binary, Acc) -> case {Count, Binary} of {0, <<>>} -> {ok, lists:reverse(Acc)}; {Count@1, Binary@1} -> _pipe = decode_string(Binary@1), gleam@result:'try'( _pipe, fun(Decoded) -> Name = erlang:element(1, Decoded), case erlang:element(2, Decoded) of <> -> _pipe@1 = decode_format_code(Format_code), gleam@result:'try'( _pipe@1, fun(Format) -> Field = {row_description_field, Name, Table_oid, Attr_number, Data_type_oid, Data_type_size, Type_modifier, Format}, row_description_fields( Count@1 - 1, Tail, [Field | Acc] ) end ); _ -> _pipe@2 = decoding_error, _pipe@3 = {protocol_error, _pipe@2, <<"Unexpected payload for RowDescriptionField"/utf8>>}, {error, _pipe@3} end end ) end. -file("src/pgl/internal/decode.gleam", 421). ?DOC(false). -spec row_description(bitstring()) -> {ok, pgl@internal:message()} | {error, pgl@internal:internal_error()}. row_description(Payload) -> case Payload of <> -> case row_description_fields(Count, Rest, []) of {ok, Fields} -> {ok, {row_description, Count, Fields}}; {error, Err} -> {error, Err} end; _ -> _pipe = decoding_error, _pipe@1 = {protocol_error, _pipe, <<"Unexpected payload for RowDescription"/utf8>>}, {error, _pipe@1} end. -file("src/pgl/internal/decode.gleam", 505). ?DOC(false). -spec parse_complete(bitstring()) -> {ok, pgl@internal:message()} | {error, pgl@internal:internal_error()}. parse_complete(Payload) -> case Payload of <<>> -> {ok, parse_complete}; _ -> _pipe = decoding_error, _pipe@1 = {protocol_error, _pipe, <<"Unexpected payload for ParseComplete"/utf8>>}, {error, _pipe@1} end. -file("src/pgl/internal/decode.gleam", 542). ?DOC(false). -spec parameter_data_types(bitstring(), list(integer())) -> {ok, list(integer())} | {error, pgl@internal:internal_error()}. parameter_data_types(Payload, Acc) -> case Payload of <<>> -> {ok, lists:reverse(Acc)}; <> -> parameter_data_types(Rest, [Oid | Acc]); _ -> _pipe = decoding_error, _pipe@1 = {protocol_error, _pipe, <<"ParameterDataTypes"/utf8>>}, {error, _pipe@1} end. -file("src/pgl/internal/decode.gleam", 518). ?DOC(false). -spec parameter_description(bitstring()) -> {ok, pgl@internal:message()} | {error, pgl@internal:internal_error()}. parameter_description(Payload) -> case Payload of <> -> gleam@result:'try'( parameter_data_types(Rest, []), fun(Data_types) -> case Count =:= erlang:length(Data_types) of true -> {ok, {parameter_description, Count, Data_types}}; _ -> _pipe = decoding_error, _pipe@1 = {protocol_error, _pipe, <<"ParameterDescription"/utf8>>}, {error, _pipe@1} end end ); _ -> _pipe@2 = decoding_error, _pipe@3 = {protocol_error, _pipe@2, <<"ParameterDescription"/utf8>>}, {error, _pipe@3} end. -file("src/pgl/internal/decode.gleam", 10). ?DOC(false). -spec message(bitstring(), bitstring()) -> {ok, pgl@internal:message()} | {error, pgl@internal:internal_error()}. message(Code, Payload) -> case Code of <<"1"/utf8>> -> parse_complete(Payload); <<"2"/utf8>> -> bind_complete(Payload); <<"3"/utf8>> -> close_complete(Payload); <<"C"/utf8>> -> command_complete(Payload); <<"D"/utf8>> -> data_row(Payload); <<"E"/utf8>> -> error_response(Payload); <<"I"/utf8>> -> empty_query_response(Payload); <<"K"/utf8>> -> backend_key_data(Payload); <<"N"/utf8>> -> notice_response(Payload); <<"R"/utf8>> -> authentication(Payload); <<"S"/utf8>> -> parameter_status(Payload); <<"T"/utf8>> -> row_description(Payload); <<"Z"/utf8>> -> ready_for_query(Payload); <<"c"/utf8>> -> copy_done(Payload); <<"d"/utf8>> -> copy_data(Payload); <<"n"/utf8>> -> no_data(Payload); <<"s"/utf8>> -> portal_suspended(Payload); <<"t"/utf8>> -> parameter_description(Payload); _ -> Message = <<"message type "/utf8, (begin _pipe = gleam@bit_array:to_string(Code), gleam@result:unwrap(_pipe, <<"unknown"/utf8>>) end)/binary>>, _pipe@1 = decoding_error, _pipe@2 = {protocol_error, _pipe@1, Message}, {error, _pipe@2} end.