Packages

A delightful ORM (?) for Gleam! 🍳

Current section

Files

Jump to
ormlette src ormlette@schema@create.erl
Raw

src/ormlette@schema@create.erl

-module(ormlette@schema@create).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([int/1, text/1, serial/1, foreign_key/3, primary/1, nullable/1, unique/1, default/2, define_table/2, dbtype_to_string/1, format_column_sql/1]).
-export_type([db_type/0, column_type/0, column/0, reference_/0, table/0]).
-type db_type() :: integer | text | real | blob.
-type column_type() :: int | string | bool | foreign_key | serial.
-type column() :: {column,
binary(),
column_type(),
boolean(),
boolean(),
boolean(),
gleam@option:option(gleam@dynamic:dynamic_()),
gleam@option:option(reference_())}.
-type reference_() :: {reference,
table(),
binary(),
gleam@option:option(binary()),
gleam@option:option(binary())}.
-type table() :: {table, binary(), list(column())}.
-file("/Users/ashercohen/Desktop/gleam/ormlette/src/ormlette/schema/create.gleam", 50).
-spec int(binary()) -> column().
int(Name) ->
{column, Name, int, false, false, false, none, none}.
-file("/Users/ashercohen/Desktop/gleam/ormlette/src/ormlette/schema/create.gleam", 62).
-spec text(binary()) -> column().
text(Name) ->
{column, Name, string, false, false, false, none, none}.
-file("/Users/ashercohen/Desktop/gleam/ormlette/src/ormlette/schema/create.gleam", 74).
-spec serial(binary()) -> column().
serial(Name) ->
{column, Name, serial, true, false, true, none, none}.
-file("/Users/ashercohen/Desktop/gleam/ormlette/src/ormlette/schema/create.gleam", 87).
-spec foreign_key(column(), table(), binary()) -> column().
foreign_key(Col, References_table, References_column) ->
Column_exists = gleam@list:contains(
gleam@list:map(
erlang:element(3, References_table),
fun(Column) -> erlang:element(2, Column) end
),
References_column
),
case Column_exists of
true ->
{column,
erlang:element(2, Col),
foreign_key,
erlang:element(4, Col),
erlang:element(5, Col),
erlang:element(6, Col),
erlang:element(7, Col),
{some,
{reference, References_table, References_column, none, none}}};
false ->
erlang:error(#{gleam_error => panic,
message => <<"`panic` expression evaluated."/utf8>>,
module => <<"ormlette/schema/create"/utf8>>,
function => <<"foreign_key"/utf8>>,
line => 117})
end.
-file("/Users/ashercohen/Desktop/gleam/ormlette/src/ormlette/schema/create.gleam", 123).
-spec primary(column()) -> column().
primary(Col) ->
{column,
erlang:element(2, Col),
erlang:element(3, Col),
true,
erlang:element(5, Col),
erlang:element(6, Col),
erlang:element(7, Col),
erlang:element(8, Col)}.
-file("/Users/ashercohen/Desktop/gleam/ormlette/src/ormlette/schema/create.gleam", 135).
-spec nullable(column()) -> column().
nullable(Col) ->
{column,
erlang:element(2, Col),
erlang:element(3, Col),
erlang:element(4, Col),
true,
erlang:element(6, Col),
erlang:element(7, Col),
erlang:element(8, Col)}.
-file("/Users/ashercohen/Desktop/gleam/ormlette/src/ormlette/schema/create.gleam", 147).
-spec unique(column()) -> column().
unique(Col) ->
{column,
erlang:element(2, Col),
erlang:element(3, Col),
erlang:element(4, Col),
erlang:element(5, Col),
true,
erlang:element(7, Col),
erlang:element(8, Col)}.
-file("/Users/ashercohen/Desktop/gleam/ormlette/src/ormlette/schema/create.gleam", 159).
-spec default(column(), any()) -> column().
default(Col, Value) ->
{column,
erlang:element(2, Col),
erlang:element(3, Col),
erlang:element(4, Col),
erlang:element(5, Col),
erlang:element(6, Col),
{some, gleam_stdlib:identity(Value)},
erlang:element(8, Col)}.
-file("/Users/ashercohen/Desktop/gleam/ormlette/src/ormlette/schema/create.gleam", 177).
-spec define_table(binary(), list(column())) -> table().
define_table(Name, Columns) ->
{table, Name, Columns}.
-file("/Users/ashercohen/Desktop/gleam/ormlette/src/ormlette/schema/create.gleam", 240).
-spec dbtype_to_string(column_type()) -> binary().
dbtype_to_string(Dbtype) ->
case Dbtype of
int ->
<<"int"/utf8>>;
string ->
<<"text"/utf8>>;
bool ->
<<"bool"/utf8>>;
foreign_key ->
<<"int"/utf8>>;
serial ->
<<"int"/utf8>>
end.
-file("/Users/ashercohen/Desktop/gleam/ormlette/src/ormlette/schema/create.gleam", 182).
-spec format_column_sql(column()) -> binary().
format_column_sql(Column) ->
Primary_sql = case erlang:element(4, Column) of
true ->
<<"PRIMARY KEY"/utf8>>;
false ->
<<""/utf8>>
end,
Nullable_sql = case erlang:element(5, Column) of
true ->
<<"NULL"/utf8>>;
false ->
<<"NOT NULL"/utf8>>
end,
Unique_sql = case erlang:element(6, Column) of
true ->
<<"UNIQUE"/utf8>>;
false ->
<<""/utf8>>
end,
Default_sql = case erlang:element(7, Column) of
none ->
<<""/utf8>>;
{some, Val} ->
<<"DEFAULT "/utf8, ((case gleam@dynamic:string(Val) of
{ok, Value} ->
Value;
{error, _} ->
erlang:error(#{gleam_error => panic,
message => <<"`panic` expression evaluated."/utf8>>,
module => <<"ormlette/schema/create"/utf8>>,
function => <<"format_column_sql"/utf8>>,
line => 202})
end))/binary>>
end,
Foreign_key_sql = case erlang:element(8, Column) of
{some, Ref} ->
<<<<<<<<<<<<"REFERENCES "/utf8,
(erlang:element(2, erlang:element(2, Ref)))/binary>>/binary,
"("/utf8>>/binary,
(erlang:element(3, Ref))/binary>>/binary,
") "/utf8>>/binary,
(case erlang:element(4, Ref) of
{some, Action} ->
<<<<"ON DELETE "/utf8, Action/binary>>/binary,
" "/utf8>>;
none ->
<<""/utf8>>
end)/binary>>/binary,
(case erlang:element(5, Ref) of
{some, Action@1} ->
<<"ON UPDATE "/utf8, Action@1/binary>>;
none ->
<<""/utf8>>
end)/binary>>;
none ->
<<""/utf8>>
end,
<<<<<<<<<<<<<<<<<<<<<<<<(erlang:element(2, Column))/binary, " "/utf8>>/binary,
(dbtype_to_string(
erlang:element(3, Column)
))/binary>>/binary,
" "/utf8>>/binary,
Primary_sql/binary>>/binary,
" "/utf8>>/binary,
Nullable_sql/binary>>/binary,
" "/utf8>>/binary,
Unique_sql/binary>>/binary,
" "/utf8>>/binary,
Default_sql/binary>>/binary,
" "/utf8>>/binary,
Foreign_key_sql/binary>>.