Current section
Files
Jump to
Current section
Files
src/funsies@schema.erl
-module(funsies@schema).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([create_table/1, get_part_table/2, add_string_column/3, add_int_column/2, add_bool_column/2, add_serial_column/2, add_foreign_key_column/4, generate_create_table_sql/1, generate_drop_table_sql/1]).
-export_type([column/0, table/0]).
-type column() :: {string_column, binary(), integer()} |
{int_column, binary()} |
{bool_column, binary()} |
{foreign_key_column, binary(), binary(), binary(), binary()} |
{serial_column, binary()}.
-type table() :: {table, binary(), list(column())}.
-file("/Users/ashercohen/Desktop/ASHERSOPRO/funsies/src/funsies/schema.gleam", 38).
-spec create_table(binary()) -> table().
create_table(Name) ->
{table, Name, []}.
-file("/Users/ashercohen/Desktop/ASHERSOPRO/funsies/src/funsies/schema.gleam", 52).
-spec check_if_column(table(), binary()) -> {ok, column()} | {error, nil}.
check_if_column(Table, Column_name) ->
_pipe = erlang:element(3, Table),
gleam@list:find(_pipe, fun(Column) -> case Column of
{string_column, Name, _} ->
Name =:= Column_name;
{int_column, Name@1} ->
Name@1 =:= Column_name;
{bool_column, Name@2} ->
Name@2 =:= Column_name;
{foreign_key_column, Name@3, _, _, _} ->
Name@3 =:= Column_name;
{serial_column, Name@4} ->
Name@4 =:= Column_name
end end).
-file("/Users/ashercohen/Desktop/ASHERSOPRO/funsies/src/funsies/schema.gleam", 44).
-spec get_part_table(table(), list(binary())) -> table().
get_part_table(Table, Columns) ->
Filtered_columns = begin
_pipe = Columns,
gleam@list:filter_map(
_pipe,
fun(Column_name) -> check_if_column(Table, Column_name) end
)
end,
{table, erlang:element(2, Table), Filtered_columns}.
-file("/Users/ashercohen/Desktop/ASHERSOPRO/funsies/src/funsies/schema.gleam", 67).
-spec add_string_column(table(), binary(), integer()) -> table().
add_string_column(Table, Name, Size) ->
{table,
erlang:element(2, Table),
lists:append(erlang:element(3, Table), [{string_column, Name, Size}])}.
-file("/Users/ashercohen/Desktop/ASHERSOPRO/funsies/src/funsies/schema.gleam", 73).
-spec add_int_column(table(), binary()) -> table().
add_int_column(Table, Name) ->
{table,
erlang:element(2, Table),
lists:append(erlang:element(3, Table), [{int_column, Name}])}.
-file("/Users/ashercohen/Desktop/ASHERSOPRO/funsies/src/funsies/schema.gleam", 79).
-spec add_bool_column(table(), binary()) -> table().
add_bool_column(Table, Name) ->
{table,
erlang:element(2, Table),
lists:append(erlang:element(3, Table), [{bool_column, Name}])}.
-file("/Users/ashercohen/Desktop/ASHERSOPRO/funsies/src/funsies/schema.gleam", 127).
-spec add_serial_column(table(), binary()) -> table().
add_serial_column(Table, Name) ->
{table,
erlang:element(2, Table),
lists:append(erlang:element(3, Table), [{serial_column, Name}])}.
-file("/Users/ashercohen/Desktop/ASHERSOPRO/funsies/src/funsies/schema.gleam", 131).
-spec sql_type(column()) -> binary().
sql_type(Column) ->
case Column of
{string_column, _, Size} ->
<<<<"VARCHAR("/utf8, (gleam@int:to_string(Size))/binary>>/binary,
")"/utf8>>;
{int_column, _} ->
<<"INT"/utf8>>;
{bool_column, _} ->
<<"BOOLEAN"/utf8>>;
{foreign_key_column, _, _, _, Ref_type} ->
Ref_type;
{serial_column, _} ->
<<"SERIAL"/utf8>>
end.
-file("/Users/ashercohen/Desktop/ASHERSOPRO/funsies/src/funsies/schema.gleam", 85).
-spec add_foreign_key_column(table(), binary(), table(), binary()) -> {ok,
table()} |
{error, binary()}.
add_foreign_key_column(Table, Name, References_table, References_column) ->
Maybe_column_referenced = gleam@list:find(
erlang:element(3, References_table),
fun(Column) -> case Column of
{string_column, Ref_name, _} ->
Ref_name =:= References_column;
{int_column, Ref_name@1} ->
Ref_name@1 =:= References_column;
{bool_column, Ref_name@2} ->
Ref_name@2 =:= References_column;
{serial_column, Ref_name@3} ->
Ref_name@3 =:= References_column;
{foreign_key_column, Ref_name@4, _, _, _} ->
Ref_name@4 =:= References_column
end end
),
case Maybe_column_referenced of
{ok, Referenced_column} ->
Fk_column = {foreign_key_column,
Name,
erlang:element(2, References_table),
References_column,
sql_type(Referenced_column)},
{ok,
{table,
erlang:element(2, Table),
lists:append(erlang:element(3, Table), [Fk_column])}};
{error, _} ->
{error,
<<<<<<"Referenced column "/utf8, References_column/binary>>/binary,
" does not exist in table "/utf8>>/binary,
(erlang:element(2, References_table))/binary>>}
end.
-file("/Users/ashercohen/Desktop/ASHERSOPRO/funsies/src/funsies/schema.gleam", 143).
-spec generate_create_table_sql(table()) -> binary().
generate_create_table_sql(Table) ->
Columns_sql = begin
_pipe = erlang:element(3, Table),
_pipe@1 = gleam@list:map(
_pipe,
fun(Column) ->
Column_type = sql_type(Column),
case Column of
{string_column, Name, _} ->
<<<<Name/binary, " "/utf8>>/binary, Column_type/binary>>;
{int_column, Name@1} ->
<<<<Name@1/binary, " "/utf8>>/binary,
Column_type/binary>>;
{bool_column, Name@2} ->
<<<<Name@2/binary, " "/utf8>>/binary,
Column_type/binary>>;
{foreign_key_column, Name@3, Ref_table, Ref_column, _} ->
<<<<<<<<<<<<<<Name@3/binary, " "/utf8>>/binary,
Column_type/binary>>/binary,
" REFERENCES "/utf8>>/binary,
Ref_table/binary>>/binary,
"("/utf8>>/binary,
Ref_column/binary>>/binary,
")"/utf8>>;
{serial_column, Name@4} ->
<<<<Name@4/binary, " "/utf8>>/binary,
Column_type/binary>>
end
end
),
gleam@string:join(_pipe@1, <<", "/utf8>>)
end,
<<<<<<<<"CREATE TABLE "/utf8, (erlang:element(2, Table))/binary>>/binary,
" ("/utf8>>/binary,
Columns_sql/binary>>/binary,
");"/utf8>>.
-file("/Users/ashercohen/Desktop/ASHERSOPRO/funsies/src/funsies/schema.gleam", 170).
-spec generate_drop_table_sql(table()) -> binary().
generate_drop_table_sql(Table) ->
<<<<"DROP TABLE "/utf8, (erlang:element(2, Table))/binary>>/binary,
";"/utf8>>.