-module(glitr@convert@sql). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([select/2, insert/3, update/4]). -file("/home/billuc/Code/glitr/glitr_convert_sql/src/glitr/convert/sql.gleam", 64). -spec select(binary(), glitr@convert:converter(any())) -> binary(). select(Table, Converter) -> case begin _pipe = Converter, glitr@convert:type_def(_pipe) end of {object, Fields} -> Field_names = gleam@list:map( Fields, fun(F) -> erlang:element(1, F) end ), <<<<<<<<"SELECT "/utf8, (begin _pipe@1 = Field_names, gleam@string:join(_pipe@1, <<", "/utf8>>) end)/binary>>/binary, " FROM "/utf8>>/binary, Table/binary>>/binary, ";"/utf8>>; _ -> gleam@io:println_error(<<"Cannot select non object values"/utf8>>), <<""/utf8>> end. -file("/home/billuc/Code/glitr/glitr_convert_sql/src/glitr/convert/sql.gleam", 129). -spec to_sql_value(glitr@convert:glitr_value()) -> binary(). to_sql_value(Value) -> case Value of {bool_value, true} -> <<"1"/utf8>>; {bool_value, false} -> <<"0"/utf8>>; {float_value, V} -> gleam@float:to_string(V); {int_value, V@1} -> gleam@int:to_string(V@1); null_value -> <<"NULL"/utf8>>; {string_value, V@2} -> <<<<"'"/utf8, V@2/binary>>/binary, "'"/utf8>>; _ -> _pipe = glitr@convert@json:encode_value(Value), gleam@json:to_string(_pipe) end. -file("/home/billuc/Code/glitr/glitr_convert_sql/src/glitr/convert/sql.gleam", 78). -spec to_insert_values(glitr@convert:glitr_value(), list(binary())) -> binary(). to_insert_values(Value, Field_names) -> case Value of {object_value, Fields} -> _pipe = Field_names, _pipe@2 = gleam@list:map( _pipe, fun(F) -> _pipe@1 = gleam@list:key_find(Fields, F), gleam@result:map(_pipe@1, fun to_sql_value/1) end ), _pipe@3 = gleam@result:all(_pipe@2), _pipe@5 = gleam@result:map( _pipe@3, fun(Values) -> <<<<"("/utf8, (begin _pipe@4 = Values, gleam@string:join(_pipe@4, <<", "/utf8>>) end)/binary>>/binary, ")"/utf8>> end ), gleam@result:lazy_unwrap( _pipe@5, fun() -> gleam@io:println_error( <<"There was an error getting some values"/utf8>> ), <<""/utf8>> end ); _ -> gleam@io:println_error(<<"Cannot insert non object values"/utf8>>), <<""/utf8>> end. -file("/home/billuc/Code/glitr/glitr_convert_sql/src/glitr/convert/sql.gleam", 12). -spec insert(binary(), glitr@convert:converter(HBA), list(HBA)) -> binary(). insert(Table, Converter, Values) -> case begin _pipe = Converter, glitr@convert:type_def(_pipe) end of {object, Fields} -> Field_names = gleam@list:map( Fields, fun(F) -> erlang:element(1, F) end ), <<<<<<<<<<<<"INSERT INTO "/utf8, Table/binary>>/binary, " ("/utf8>>/binary, (begin _pipe@1 = Field_names, gleam@string:join(_pipe@1, <<", "/utf8>>) end)/binary>>/binary, ") VALUES "/utf8>>/binary, (begin _pipe@4 = gleam@list:map(Values, fun(V) -> _pipe@2 = V, _pipe@3 = (glitr@convert:encode(Converter))( _pipe@2 ), to_insert_values(_pipe@3, Field_names) end), gleam@string:join(_pipe@4, <<", "/utf8>>) end)/binary>>/binary, ";"/utf8>>; _ -> gleam@io:println_error(<<"Cannot insert non object values"/utf8>>), <<""/utf8>> end. -file("/home/billuc/Code/glitr/glitr_convert_sql/src/glitr/convert/sql.gleam", 97). -spec to_update_values(glitr@convert:glitr_value(), binary()) -> binary(). to_update_values(Value, Col_to_ignore) -> case Value of {object_value, Fields} -> _pipe = Fields, _pipe@1 = gleam@list:filter_map( _pipe, fun(V) -> gleam@bool:guard( erlang:element(1, V) =:= Col_to_ignore, {error, nil}, fun() -> {ok, <<<<(erlang:element(1, V))/binary, "="/utf8>>/binary, (to_sql_value(erlang:element(2, V)))/binary>>} end ) end ), gleam@string:join(_pipe@1, <<", "/utf8>>); _ -> gleam@io:println_error(<<"Cannot update non object values"/utf8>>), <<""/utf8>> end. -file("/home/billuc/Code/glitr/glitr_convert_sql/src/glitr/convert/sql.gleam", 114). -spec to_condition(glitr@convert:glitr_value(), binary()) -> binary(). to_condition(Value, Selector_col) -> case Value of {object_value, Fields} -> _pipe = Fields, _pipe@1 = gleam@list:key_find(_pipe, Selector_col), _pipe@2 = gleam@result:map( _pipe@1, fun(Val) -> <<<>); _ -> gleam@io:println_error(<<"Cannot update non object values"/utf8>>), <<""/utf8>> end. -file("/home/billuc/Code/glitr/glitr_convert_sql/src/glitr/convert/sql.gleam", 39). -spec update(binary(), glitr@convert:converter(HBD), HBD, binary()) -> binary(). update(Table, Converter, Value, Selector_col) -> case begin _pipe = Converter, glitr@convert:type_def(_pipe) end of {object, _} -> Glitr_value = begin _pipe@1 = Value, (glitr@convert:encode(Converter))(_pipe@1) end, <<<<<<<<<<<<"UPDATE "/utf8, Table/binary>>/binary, " SET "/utf8>>/binary, (begin _pipe@2 = Glitr_value, to_update_values(_pipe@2, Selector_col) end)/binary>>/binary, " WHERE "/utf8>>/binary, (to_condition(Glitr_value, Selector_col))/binary>>/binary, ";"/utf8>>; _ -> gleam@io:println_error(<<"Cannot update non object values"/utf8>>), <<""/utf8>> end.