-module(ormlette@ir@sql). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([helper/1, to_sql/1, to_sql_drop/1, to_sql_drop_column/2]). -file("/Users/ashercohen/Desktop/gleam/ormlette/src/ormlette/ir/sql.gleam", 9). -spec helper(ormlette@ir@ir:column_ir()) -> binary(). helper(Column) -> case erlang:element(3, Column) of int -> <<"int"/utf8>>; bool -> <<"bool"/utf8>>; string -> <<"text"/utf8>>; foreign_key -> Fk_constraint = gleam@list:filter( erlang:element(4, Column), fun(Con) -> case Con of {foreign_key, _, _, _, _} -> true; _ -> false end end ), case gleam@list:first(Fk_constraint) of {ok, {foreign_key, Ref_table, Ref_column, _, _}} -> Col@1 = gleam@list:find( erlang:element(3, Ref_table), fun(Col) -> erlang:element(2, Col) =:= Ref_column end ), case Col@1 of {ok, Found_col} -> helper(Found_col); _ -> erlang:error(#{gleam_error => panic, message => <<"`panic` expression evaluated."/utf8>>, module => <<"ormlette/ir/sql"/utf8>>, function => <<"helper"/utf8>>, line => 28}) end; {error, _} -> erlang:error(#{gleam_error => panic, message => <<"`panic` expression evaluated."/utf8>>, module => <<"ormlette/ir/sql"/utf8>>, function => <<"helper"/utf8>>, line => 31}); _ -> erlang:error(#{gleam_error => panic, message => <<"`panic` expression evaluated."/utf8>>, module => <<"ormlette/ir/sql"/utf8>>, function => <<"helper"/utf8>>, line => 32}) end; serial -> <<"SERIAL"/utf8>> end. -file("/Users/ashercohen/Desktop/gleam/ormlette/src/ormlette/ir/sql.gleam", 61). -spec constraint_to_sql(ormlette@ir@ir:column_constraint()) -> binary(). constraint_to_sql(Constraint) -> case Constraint of primary_key -> <<"PRIMARY KEY"/utf8>>; nullable -> <<"NULL"/utf8>>; unique -> <<"UNIQUE"/utf8>>; {foreign_key, Ref_table, Ref_column, On_delete, On_update} -> <<<<<<<<<<<<"REFERENCES "/utf8, (erlang:element(2, Ref_table))/binary>>/binary, "("/utf8>>/binary, Ref_column/binary>>/binary, ") "/utf8>>/binary, (case On_delete of {some, Action} -> <<<<"ON DELETE "/utf8, Action/binary>>/binary, " "/utf8>>; none -> <<""/utf8>> end)/binary>>/binary, (case On_update of {some, Action@1} -> <<"ON UPDATE "/utf8, Action@1/binary>>; none -> <<""/utf8>> end)/binary>> end. -file("/Users/ashercohen/Desktop/gleam/ormlette/src/ormlette/ir/sql.gleam", 83). -spec dynamic_to_sql(gleam@dynamic:dynamic_()) -> binary(). dynamic_to_sql(Value) -> case gleam@dynamic:string(Value) of {ok, Str_val} -> <<<<"'"/utf8, Str_val/binary>>/binary, "'"/utf8>>; {error, _} -> case gleam@dynamic:int(Value) of {ok, Int_val} -> gleam@int:to_string(Int_val); _ -> erlang:error(#{gleam_error => panic, message => <<"`panic` expression evaluated."/utf8>>, module => <<"ormlette/ir/sql"/utf8>>, function => <<"dynamic_to_sql"/utf8>>, line => 89}) end end. -file("/Users/ashercohen/Desktop/gleam/ormlette/src/ormlette/ir/sql.gleam", 45). -spec column_to_sql(ormlette@ir@ir:column_ir()) -> binary(). column_to_sql(Column_ir) -> Constraints_sql = begin _pipe = erlang:element(4, Column_ir), _pipe@1 = gleam@list:map(_pipe, fun constraint_to_sql/1), gleam@string:join(_pipe@1, <<" "/utf8>>) end, Default_sql = case erlang:element(5, Column_ir) of none -> <<""/utf8>>; {some, Value} -> <<"DEFAULT "/utf8, (dynamic_to_sql(Value))/binary>> end, <<<<<<<<<<<<(erlang:element(2, Column_ir))/binary, " "/utf8>>/binary, (helper(Column_ir))/binary>>/binary, " "/utf8>>/binary, Constraints_sql/binary>>/binary, " "/utf8>>/binary, Default_sql/binary>>. -file("/Users/ashercohen/Desktop/gleam/ormlette/src/ormlette/ir/sql.gleam", 39). -spec to_sql(ormlette@ir@ir:table_ir()) -> binary(). to_sql(Table_ir) -> Columns_sql = begin _pipe = erlang:element(3, Table_ir), _pipe@1 = gleam@list:map(_pipe, fun column_to_sql/1), gleam@string:join(_pipe@1, <<", "/utf8>>) end, <<<<<<<<"CREATE TABLE "/utf8, (erlang:element(2, Table_ir))/binary>>/binary, " ("/utf8>>/binary, Columns_sql/binary>>/binary, ");"/utf8>>. -file("/Users/ashercohen/Desktop/gleam/ormlette/src/ormlette/ir/sql.gleam", 94). -spec to_sql_drop(ormlette@ir@ir:table_ir()) -> binary(). to_sql_drop(Table_ir) -> <<<<"DROP TABLE "/utf8, (erlang:element(2, Table_ir))/binary>>/binary, ";"/utf8>>. -file("/Users/ashercohen/Desktop/gleam/ormlette/src/ormlette/ir/sql.gleam", 98). -spec to_sql_drop_column(binary(), binary()) -> binary(). to_sql_drop_column(Table_name, Column_name) -> <<<<<<<<"ALTER TABLE "/utf8, Table_name/binary>>/binary, " DROP COLUMN "/utf8>>/binary, Column_name/binary>>/binary, ";"/utf8>>.