Current section

Files

Jump to
cake src cake@internal@example@cake_read.erl
Raw

src/cake@internal@example@cake_read.erl

-module(cake@internal@example@cake_read).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([run_dummy_fragment/0, run_dummy_select/0, run_dummy_union_all/0, main/0]).
-spec drop_owners_table_if_exists() -> binary().
drop_owners_table_if_exists() ->
<<"DROP TABLE IF EXISTS owners;"/utf8>>.
-spec create_owners_table() -> binary().
create_owners_table() ->
<<"CREATE TABLE owners (
id int,
name text,
last_name text,
age int,
tags text[]
);"/utf8>>.
-spec insert_owners_rows() -> binary().
insert_owners_rows() ->
<<"INSERT INTO owners (id, name, last_name, age) VALUES
(1, 'Alice', 'Foo', 5),
(2, 'bob', 'BOB', 8),
(3, 'Charlie', 'Quux', 13)
;"/utf8>>.
-spec drop_cats_table_if_exists() -> binary().
drop_cats_table_if_exists() ->
<<"DROP TABLE IF EXISTS cats;"/utf8>>.
-spec create_cats_table() -> binary().
create_cats_table() ->
<<"CREATE TABLE cats (
name text,
age int,
is_wild boolean,
owner_id int
);"/utf8>>.
-spec insert_cats_rows() -> binary().
insert_cats_rows() ->
<<"INSERT INTO cats (name, age, is_wild, owner_id) VALUES
('Nubi', 4, TRUE, 1),
('Biffy', 10, NULL, 2),
('Ginny', 6, FALSE, 3),
('Karl', 8, TRUE, NULL),
('Clara', 3, TRUE, NULL)
;"/utf8>>.
-spec drop_dogs_table_if_exists() -> binary().
drop_dogs_table_if_exists() ->
<<"DROP TABLE IF EXISTS dogs;"/utf8>>.
-spec create_dogs_table() -> binary().
create_dogs_table() ->
<<"CREATE TABLE dogs (
name text,
age int,
is_trained boolean,
owner_id int
);"/utf8>>.
-spec insert_dogs_rows() -> binary().
insert_dogs_rows() ->
<<"INSERT INTO dogs (name, age, is_trained, owner_id) VALUES
('Fubi', 1, TRUE, 1),
('Diffy', 2, NULL, 2),
('Tinny', 3, FALSE, 3),
('Karl', 4, TRUE, NULL),
('Clara', 5, TRUE, NULL)
;"/utf8>>.
-spec run_on_postgres(
cake@internal@query:'query'(),
fun((gleam@dynamic:dynamic_()) -> {ok, PBE} |
{error, list(gleam@dynamic:decode_error())})
) -> {ok, list(PBE)} | {error, gleam@pgo:query_error()}.
run_on_postgres(Query, Query_decoder) ->
cake@adapter@postgres:with_connection(
fun(Conn) ->
_ = begin
_pipe = drop_owners_table_if_exists(),
cake@adapter@postgres:execute_raw_sql(_pipe, Conn)
end,
_ = begin
_pipe@1 = create_owners_table(),
cake@adapter@postgres:execute_raw_sql(_pipe@1, Conn)
end,
_ = begin
_pipe@2 = insert_owners_rows(),
cake@adapter@postgres:execute_raw_sql(_pipe@2, Conn)
end,
_ = begin
_pipe@3 = drop_cats_table_if_exists(),
cake@adapter@postgres:execute_raw_sql(_pipe@3, Conn)
end,
_ = begin
_pipe@4 = create_cats_table(),
cake@adapter@postgres:execute_raw_sql(_pipe@4, Conn)
end,
_ = begin
_pipe@5 = insert_cats_rows(),
cake@adapter@postgres:execute_raw_sql(_pipe@5, Conn)
end,
_ = begin
_pipe@6 = drop_dogs_table_if_exists(),
cake@adapter@postgres:execute_raw_sql(_pipe@6, Conn)
end,
_ = begin
_pipe@7 = create_dogs_table(),
cake@adapter@postgres:execute_raw_sql(_pipe@7, Conn)
end,
_ = begin
_pipe@8 = insert_dogs_rows(),
cake@adapter@postgres:execute_raw_sql(_pipe@8, Conn)
end,
_pipe@9 = Query,
cake@adapter@postgres:run_query(_pipe@9, Query_decoder, Conn)
end
).
-spec run_on_sqlite(
cake@internal@query:'query'(),
fun((gleam@dynamic:dynamic_()) -> {ok, PBH} |
{error, list(gleam@dynamic:decode_error())})
) -> {ok, list(PBH)} | {error, sqlight:error()}.
run_on_sqlite(Query, Query_decoder) ->
cake@adapter@sqlite:with_memory_connection(
fun(Conn) ->
_ = begin
_pipe = drop_owners_table_if_exists(),
cake@adapter@sqlite:execute_raw_sql(_pipe, Conn)
end,
_ = begin
_pipe@1 = create_owners_table(),
cake@adapter@sqlite:execute_raw_sql(_pipe@1, Conn)
end,
_ = begin
_pipe@2 = insert_owners_rows(),
cake@adapter@sqlite:execute_raw_sql(_pipe@2, Conn)
end,
_ = begin
_pipe@3 = drop_cats_table_if_exists(),
cake@adapter@sqlite:execute_raw_sql(_pipe@3, Conn)
end,
_ = begin
_pipe@4 = create_cats_table(),
cake@adapter@sqlite:execute_raw_sql(_pipe@4, Conn)
end,
_ = begin
_pipe@5 = insert_cats_rows(),
cake@adapter@sqlite:execute_raw_sql(_pipe@5, Conn)
end,
_ = begin
_pipe@6 = drop_dogs_table_if_exists(),
cake@adapter@sqlite:execute_raw_sql(_pipe@6, Conn)
end,
_ = begin
_pipe@7 = create_dogs_table(),
cake@adapter@sqlite:execute_raw_sql(_pipe@7, Conn)
end,
_ = begin
_pipe@8 = insert_dogs_rows(),
cake@adapter@sqlite:execute_raw_sql(_pipe@8, Conn)
end,
_pipe@9 = Query,
cake@adapter@sqlite:run_query(_pipe@9, Query_decoder, Conn)
end
).
-spec run_dummy_fragment() -> {ok, list({binary(), integer(), binary()})} |
{error, gleam@pgo:query_error()}.
run_dummy_fragment() ->
cake@internal@stdlib@iox:print_dashes(),
Cats_query = begin
_pipe = cake@query@from:table(<<"cats"/utf8>>),
cake@query@select:new_from(_pipe)
end,
Select_query = begin
_pipe@1 = Cats_query,
_pipe@4 = cake@query@select:where(
_pipe@1,
begin
_pipe@2 = cake@query@where:fragment_value(
cake@query@fragment:literal(<<"LOWER(cats.name)"/utf8>>)
),
_pipe@3 = cake@query@where:eq(
_pipe@2,
cake@query@where:fragment_value(
cake@query@fragment:prepared(
<<<<"LOWER("/utf8, (<<"$"/utf8>>)/binary>>/binary,
")"/utf8>>,
[cake@param:string(<<"Karl"/utf8>>)]
)
)
),
cake@internal@stdlib@iox:dbg(_pipe@3)
end
),
cake@query@select:to_query(_pipe@4)
end,
gleam_erlang_ffi:sleep(100),
Query_decoder = gleam@dynamic:tuple3(
fun gleam@dynamic:string/1,
fun gleam@dynamic:int/1,
fun gleam@dynamic:string/1
),
cake@internal@stdlib@iox:println(<<"SQLite"/utf8>>),
_ = begin
_pipe@5 = run_on_sqlite(Select_query, Query_decoder),
_pipe@6 = cake@internal@stdlib@iox:print_tap(
_pipe@5,
<<"Result: "/utf8>>
),
cake@internal@stdlib@iox:dbg(_pipe@6)
end,
gleam_erlang_ffi:sleep(100),
cake@internal@stdlib@iox:println(<<"Postgres"/utf8>>),
_ = begin
_pipe@7 = run_on_postgres(Select_query, Query_decoder),
_pipe@8 = cake@internal@stdlib@iox:print_tap(
_pipe@7,
<<"Result: "/utf8>>
),
cake@internal@stdlib@iox:dbg(_pipe@8)
end.
-spec run_dummy_select() -> nil.
run_dummy_select() ->
cake@internal@stdlib@iox:print_dashes(),
Cats_sub_query = cake@query@select:new_from(
cake@query@from:table(<<"cats"/utf8>>)
),
Dogs_sub_query = cake@query@select:new_from(
cake@query@from:table(<<"dogs"/utf8>>)
),
Cats_t = cake@internal@query:qualified_identifier(<<"cats"/utf8>>),
Owners_t = cake@internal@query:qualified_identifier(<<"owners"/utf8>>),
Where = cake@query@where:'or'(
[begin
_pipe = cake@query@where:col(Cats_t(<<"age"/utf8>>)),
cake@query@where:eq(_pipe, cake@query@where:int(10))
end,
begin
_pipe@1 = cake@query@where:col(Cats_t(<<"name"/utf8>>)),
cake@query@where:eq(
_pipe@1,
cake@query@where:string(<<"foo"/utf8>>)
)
end,
begin
_pipe@2 = cake@query@where:col(Cats_t(<<"name"/utf8>>)),
cake@query@where:eq(
_pipe@2,
cake@query@where:string(<<"foo"/utf8>>)
)
end,
begin
_pipe@3 = cake@query@where:col(Cats_t(<<"age"/utf8>>)),
cake@query@where:in(
_pipe@3,
[cake@query@where:int(1), cake@query@where:int(2)]
)
end]
),
Select_query = begin
_pipe@4 = Cats_sub_query,
_pipe@5 = cake@query@select:to_query(_pipe@4),
_pipe@6 = cake@query@from:sub_query(_pipe@5, <<"cats"/utf8>>),
_pipe@7 = cake@query@select:new_from(_pipe@6),
_pipe@9 = cake@query@select:select(
_pipe@7,
[cake@query@select:col(Cats_t(<<"name"/utf8>>)),
cake@query@select:col(Cats_t(<<"age"/utf8>>)),
begin
_pipe@8 = cake@query@select:col(Owners_t(<<"name"/utf8>>)),
cake@query@select:alias(_pipe@8, <<"owner_name"/utf8>>)
end]
),
_pipe@10 = cake@query@select:where(_pipe@9, Where),
_pipe@11 = cake@query@select:order_asc(
_pipe@10,
Cats_t(<<"name"/utf8>>)
),
_pipe@12 = cake@query@select:order_replace(
_pipe@11,
Cats_t(<<"age"/utf8>>),
asc
),
_pipe@13 = cake@query@select:limit(_pipe@12, 1),
_pipe@14 = cake@query@select:offset(_pipe@13, 0),
_pipe@18 = cake@query@select:joins(
_pipe@14,
[cake@query@join:inner(
cake@query@join:table(<<"owners"/utf8>>),
cake@query@where:'or'(
[begin
_pipe@15 = cake@query@where:col(
Owners_t(<<"id"/utf8>>)
),
cake@query@where:eq(
_pipe@15,
cake@query@where:col(
Cats_t(<<"owner_id"/utf8>>)
)
)
end,
begin
_pipe@16 = cake@query@where:col(
Owners_t(<<"id"/utf8>>)
),
cake@query@where:lt(
_pipe@16,
cake@query@where:int(20)
)
end,
begin
_pipe@17 = cake@query@where:col(
Owners_t(<<"id"/utf8>>)
),
cake@query@where:is_not_null(_pipe@17)
end]
),
<<"owners"/utf8>>
),
cake@query@join:cross(
cake@query@join:sub_query(
cake@query@select:to_query(Dogs_sub_query)
),
<<"dogs"/utf8>>
)]
),
_pipe@19 = cake@query@select:to_query(_pipe@18),
cake@internal@stdlib@iox:dbg(_pipe@19)
end,
gleam_erlang_ffi:sleep(100),
Query_decoder = gleam@dynamic:tuple3(
fun gleam@dynamic:string/1,
fun gleam@dynamic:int/1,
fun gleam@dynamic:string/1
),
cake@internal@stdlib@iox:println(<<"SQLite"/utf8>>),
_ = begin
_pipe@20 = run_on_sqlite(Select_query, Query_decoder),
_pipe@21 = cake@internal@stdlib@iox:print_tap(
_pipe@20,
<<"Result: "/utf8>>
),
cake@internal@stdlib@iox:dbg(_pipe@21)
end,
gleam_erlang_ffi:sleep(100),
cake@internal@stdlib@iox:println(<<"Postgres"/utf8>>),
_ = begin
_pipe@22 = run_on_postgres(Select_query, Query_decoder),
_pipe@23 = cake@internal@stdlib@iox:print_tap(
_pipe@22,
<<"Result: "/utf8>>
),
cake@internal@stdlib@iox:dbg(_pipe@23)
end,
gleam_erlang_ffi:sleep(100).
-spec run_dummy_union_all() -> nil.
run_dummy_union_all() ->
cake@internal@stdlib@iox:print_dashes(),
Select_query = begin
_pipe = cake@query@select:new_from(
cake@query@from:table(<<"cats"/utf8>>)
),
cake@query@select:select(
_pipe,
[cake@query@select:col(<<"name"/utf8>>),
cake@query@select:col(<<"age"/utf8>>)]
)
end,
Select_query_a = begin
_pipe@1 = Select_query,
cake@query@select:where(
_pipe@1,
cake@query@where:'or'(
[begin
_pipe@2 = cake@query@where:col(<<"age"/utf8>>),
cake@query@where:lte(_pipe@2, cake@query@where:int(4))
end,
begin
_pipe@3 = cake@query@where:col(<<"name"/utf8>>),
cake@query@where:like(_pipe@3, <<"%ara%"/utf8>>)
end]
)
)
end,
Where_b = cake@query@where:'not'(
begin
_pipe@4 = cake@query@where:col(<<"is_wild"/utf8>>),
cake@query@where:is_not_bool(_pipe@4, false)
end
),
Select_query_b = begin
_pipe@5 = Select_query,
_pipe@7 = cake@query@select:where(
_pipe@5,
begin
_pipe@6 = cake@query@where:col(<<"age"/utf8>>),
cake@query@where:gte(_pipe@6, cake@query@where:int(7))
end
),
_pipe@8 = cake@query@select:order_asc(_pipe@7, <<"name"/utf8>>),
cake@query@select:where(_pipe@8, Where_b)
end,
Union_query = begin
_pipe@9 = Select_query_a,
_pipe@10 = cake@query@combined:union_all(_pipe@9, Select_query_b),
_pipe@11 = cake@query@combined:limit(_pipe@10, 1),
_pipe@12 = cake@query@combined:order_replace(
_pipe@11,
<<"age"/utf8>>,
asc
),
_pipe@13 = cake@query@combined:to_query(_pipe@12),
cake@internal@stdlib@iox:dbg(_pipe@13)
end,
Query_decoder = gleam@dynamic:tuple2(
fun gleam@dynamic:string/1,
fun gleam@dynamic:int/1
),
gleam_erlang_ffi:sleep(100),
cake@internal@stdlib@iox:println(<<"SQLite"/utf8>>),
_ = begin
_pipe@14 = run_on_sqlite(Union_query, Query_decoder),
_pipe@15 = cake@internal@stdlib@iox:print_tap(
_pipe@14,
<<"Result: "/utf8>>
),
cake@internal@stdlib@iox:dbg(_pipe@15)
end,
gleam_erlang_ffi:sleep(100),
cake@internal@stdlib@iox:println(<<"Postgres"/utf8>>),
_ = begin
_pipe@16 = run_on_postgres(Union_query, Query_decoder),
_pipe@17 = cake@internal@stdlib@iox:print_tap(
_pipe@16,
<<"Result: "/utf8>>
),
cake@internal@stdlib@iox:dbg(_pipe@17)
end,
gleam_erlang_ffi:sleep(100).
-spec main() -> nil.
main() ->
gleam_erlang_ffi:sleep(100),
_ = run_dummy_fragment(),
gleam_erlang_ffi:sleep(100),
_ = run_dummy_select(),
gleam_erlang_ffi:sleep(100),
_ = run_dummy_union_all(),
gleam_erlang_ffi:sleep(100),
nil.