Current section
Files
Jump to
Current section
Files
src/ormlette@ir@sql.erl
-module(ormlette@ir@sql).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([helper/1, to_sql/1]).
-file("/Users/ashercohen/Desktop/gleam/ormlette/src/ormlette/ir/sql.gleam", 12).
-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 ->
Hi = case gleam@list:first(
gleam@list:filter(
erlang:element(4, Column),
fun(Con) -> case Con of
{foreign_key, _, _, _, _} ->
true;
_ ->
false
end end
)
) of
{ok, V} ->
case V of
{foreign_key, References_table, Reference_column, _, _} ->
Col@1 = gleam@list:find(
erlang:element(3, References_table),
fun(Col) ->
erlang:element(2, Col) =:= Reference_column
end
),
case Col@1 of
{ok, Found_col} ->
helper(Found_col);
{error, _} ->
erlang:error(#{gleam_error => panic,
message => <<"`panic` expression evaluated."/utf8>>,
module => <<"ormlette/ir/sql"/utf8>>,
function => <<"helper"/utf8>>,
line => 37})
end;
_ ->
erlang:error(#{gleam_error => panic,
message => <<"`panic` expression evaluated."/utf8>>,
module => <<"ormlette/ir/sql"/utf8>>,
function => <<"helper"/utf8>>,
line => 40})
end;
{error, _} ->
erlang:error(#{gleam_error => panic,
message => <<"`panic` expression evaluated."/utf8>>,
module => <<"ormlette/ir/sql"/utf8>>,
function => <<"helper"/utf8>>,
line => 43})
end;
serial ->
<<"SERIAL"/utf8>>
end.
-file("/Users/ashercohen/Desktop/gleam/ormlette/src/ormlette/ir/sql.gleam", 82).
-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", 105).
-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);
{error, _} ->
erlang:error(#{gleam_error => panic,
message => <<"`panic` expression evaluated."/utf8>>,
module => <<"ormlette/ir/sql"/utf8>>,
function => <<"dynamic_to_sql"/utf8>>,
line => 111})
end
end.
-file("/Users/ashercohen/Desktop/gleam/ormlette/src/ormlette/ir/sql.gleam", 61).
-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", 51).
-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>>.