-module(cake). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([run_on_postgres/2, run_dummy_fragment/0, run_dummy_select/0, run_dummy_union_all/0, main/0, stacky/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, OKJ} | {error, list(gleam@dynamic:decode_error())}) ) -> {ok, list(OKJ)} | {error, gleam@pgo:query_error()}. run_on_postgres(Query, Query_decoder) -> cake@adapter@postgres_adapter:with_connection( fun(Conn) -> _ = begin _pipe = drop_owners_table_if_exists(), cake@adapter@postgres_adapter:execute(_pipe, Conn) end, _ = begin _pipe@1 = create_owners_table(), cake@adapter@postgres_adapter:execute(_pipe@1, Conn) end, _ = begin _pipe@2 = insert_owners_rows(), cake@adapter@postgres_adapter:execute(_pipe@2, Conn) end, _ = begin _pipe@3 = drop_cats_table_if_exists(), cake@adapter@postgres_adapter:execute(_pipe@3, Conn) end, _ = begin _pipe@4 = create_cats_table(), cake@adapter@postgres_adapter:execute(_pipe@4, Conn) end, _ = begin _pipe@5 = insert_cats_rows(), cake@adapter@postgres_adapter:execute(_pipe@5, Conn) end, _ = begin _pipe@6 = drop_dogs_table_if_exists(), cake@adapter@postgres_adapter:execute(_pipe@6, Conn) end, _ = begin _pipe@7 = create_dogs_table(), cake@adapter@postgres_adapter:execute(_pipe@7, Conn) end, _ = begin _pipe@8 = insert_dogs_rows(), cake@adapter@postgres_adapter:execute(_pipe@8, Conn) end, cake@adapter@postgres_adapter:run_query(Conn, Query, Query_decoder) end ). -spec run_on_sqlite( cake@internal@query:'query'(), fun((gleam@dynamic:dynamic_()) -> {ok, OKM} | {error, list(gleam@dynamic:decode_error())}) ) -> {ok, list(OKM)} | {error, sqlight:error()}. run_on_sqlite(Query, Query_decoder) -> cake@adapter@sqlite_adapter:with_memory_connection( fun(Conn) -> _ = begin _pipe = drop_owners_table_if_exists(), cake@adapter@sqlite_adapter:execute(_pipe, Conn) end, _ = begin _pipe@1 = create_owners_table(), cake@adapter@sqlite_adapter:execute(_pipe@1, Conn) end, _ = begin _pipe@2 = insert_owners_rows(), cake@adapter@sqlite_adapter:execute(_pipe@2, Conn) end, _ = begin _pipe@3 = drop_cats_table_if_exists(), cake@adapter@sqlite_adapter:execute(_pipe@3, Conn) end, _ = begin _pipe@4 = create_cats_table(), cake@adapter@sqlite_adapter:execute(_pipe@4, Conn) end, _ = begin _pipe@5 = insert_cats_rows(), cake@adapter@sqlite_adapter:execute(_pipe@5, Conn) end, _ = begin _pipe@6 = drop_dogs_table_if_exists(), cake@adapter@sqlite_adapter:execute(_pipe@6, Conn) end, _ = begin _pipe@7 = create_dogs_table(), cake@adapter@sqlite_adapter:execute(_pipe@7, Conn) end, _ = begin _pipe@8 = insert_dogs_rows(), cake@adapter@sqlite_adapter:execute(_pipe@8, Conn) end, cake@adapter@sqlite_adapter:run_query(Conn, Query, Query_decoder) end ). -spec run_dummy_fragment() -> {ok, list({binary(), integer(), binary()})} | {error, gleam@pgo:query_error()}. run_dummy_fragment() -> cake@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:col(<<"name"/utf8>>), _pipe@3 = cake@query@where:eq( _pipe@2, cake@query@where:fragment( cake@query@fragment:prepared( <<<<<<<<"LOWER("/utf8, (<<"$"/utf8>>)/binary>>/binary, ") OR name = LOWER("/utf8>>/binary, (<<"$"/utf8>>)/binary>>/binary, ")"/utf8>>, [cake@param:string(<<"Timmy"/utf8>>), cake@param:string(<<"Jimmy"/utf8>>)] ) ) ), cake@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@stdlib@iox:println(<<"SQLite"/utf8>>), _ = begin _pipe@5 = run_on_sqlite(Select_query, Query_decoder), _pipe@6 = cake@stdlib@iox:print_tap(_pipe@5, <<"Result: "/utf8>>), cake@stdlib@iox:dbg(_pipe@6) end, gleam_erlang_ffi:sleep(100), cake@stdlib@iox:println(<<"Postgres"/utf8>>), _ = begin _pipe@7 = run_on_postgres(Select_query, Query_decoder), _pipe@8 = cake@stdlib@iox:print_tap(_pipe@7, <<"Result: "/utf8>>), cake@stdlib@iox:dbg(_pipe@8) end. -spec run_dummy_select() -> nil. run_dummy_select() -> cake@stdlib@iox:print_dashes(), Cats_sub_query = begin _pipe = cake@query@from:table(<<"cats"/utf8>>), cake@query@select:new_from(_pipe) end, Dogs_sub_query = begin _pipe@1 = cake@query@from:table(<<"dogs"/utf8>>), cake@query@select:new_from(_pipe@1) end, 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@2 = cake@query@where:col(Cats_t(<<"age"/utf8>>)), cake@query@where:eq(_pipe@2, cake@query@where:int(10)) end, begin _pipe@3 = cake@query@where:col(Cats_t(<<"name"/utf8>>)), cake@query@where:eq( _pipe@3, cake@query@where:string(<<"foo"/utf8>>) ) end, begin _pipe@4 = cake@query@where:col(Cats_t(<<"name"/utf8>>)), cake@query@where:eq( _pipe@4, cake@query@where:string(<<"foo"/utf8>>) ) end, begin _pipe@5 = cake@query@where:col(Cats_t(<<"age"/utf8>>)), cake@query@where:in( _pipe@5, [cake@query@where:int(1), cake@query@where:int(2)] ) end] ), Select_query = begin _pipe@6 = Cats_sub_query, _pipe@7 = cake@query@select:to_query(_pipe@6), _pipe@8 = cake@query@from:sub_query(_pipe@7, <<"cats"/utf8>>), _pipe@9 = cake@query@select:new_from(_pipe@8), _pipe@11 = cake@query@select:select( _pipe@9, [cake@query@select:col(Cats_t(<<"name"/utf8>>)), cake@query@select:col(Cats_t(<<"age"/utf8>>)), begin _pipe@10 = cake@query@select:col(Owners_t(<<"name"/utf8>>)), cake@query@select:alias(_pipe@10, <<"owner_name"/utf8>>) end] ), _pipe@12 = cake@query@select:where(_pipe@11, Where), _pipe@13 = cake@query@select:order_asc( _pipe@12, Cats_t(<<"name"/utf8>>) ), _pipe@14 = cake@query@select:order_replace( _pipe@13, Cats_t(<<"age"/utf8>>), asc ), _pipe@15 = cake@query@select:set_limit(_pipe@14, 1), _pipe@16 = cake@query@select:set_limit_and_offset(_pipe@15, 1, 0), _pipe@20 = cake@query@select:joins( _pipe@16, [cake@query@join:inner( cake@query@join:table(<<"owners"/utf8>>), <<"owners"/utf8>>, cake@query@where:'or'( [begin _pipe@17 = cake@query@where:col( Owners_t(<<"id"/utf8>>) ), cake@query@where:eq( _pipe@17, cake@query@where:col( Cats_t(<<"owner_id"/utf8>>) ) ) end, begin _pipe@18 = cake@query@where:col( Owners_t(<<"id"/utf8>>) ), cake@query@where:lt( _pipe@18, cake@query@where:int(20) ) end, begin _pipe@19 = cake@query@where:col( Owners_t(<<"id"/utf8>>) ), cake@query@where:is_not_null(_pipe@19) end] ) ), cake@query@join:cross( cake@query@join:sub_query( cake@query@select:to_query(Dogs_sub_query) ), <<"dogs"/utf8>> )] ), _pipe@21 = cake@query@select:to_query(_pipe@20), cake@stdlib@iox:dbg(_pipe@21) 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@stdlib@iox:println(<<"SQLite"/utf8>>), _ = begin _pipe@22 = run_on_sqlite(Select_query, Query_decoder), _pipe@23 = cake@stdlib@iox:print_tap(_pipe@22, <<"Result: "/utf8>>), cake@stdlib@iox:dbg(_pipe@23) end, gleam_erlang_ffi:sleep(100), cake@stdlib@iox:println(<<"Postgres"/utf8>>), _ = begin _pipe@24 = run_on_postgres(Select_query, Query_decoder), _pipe@25 = cake@stdlib@iox:print_tap(_pipe@24, <<"Result: "/utf8>>), cake@stdlib@iox:dbg(_pipe@25) end, gleam_erlang_ffi:sleep(100). -spec run_dummy_union_all() -> nil. run_dummy_union_all() -> cake@stdlib@iox:print_dashes(), Select_query = begin _pipe = cake@query@from:table(<<"cats"/utf8>>), _pipe@1 = cake@query@select:new_from(_pipe), cake@query@select:select( _pipe@1, [cake@query@select:col(<<"name"/utf8>>), cake@query@select:col(<<"age"/utf8>>)] ) end, Select_query_a = begin _pipe@2 = Select_query, cake@query@select:where( _pipe@2, cake@query@where:'or'( [begin _pipe@3 = cake@query@where:col(<<"age"/utf8>>), cake@query@where:lte(_pipe@3, cake@query@where:int(4)) end, begin _pipe@4 = cake@query@where:col(<<"name"/utf8>>), cake@query@where:like(_pipe@4, <<"%ara%"/utf8>>) end] ) ) end, Where_b = cake@query@where:'not'( begin _pipe@5 = cake@query@where:col(<<"is_wild"/utf8>>), cake@query@where:is_not_bool(_pipe@5, false) end ), Select_query_b = begin _pipe@6 = Select_query, _pipe@8 = cake@query@select:where( _pipe@6, begin _pipe@7 = cake@query@where:col(<<"age"/utf8>>), cake@query@where:gte(_pipe@7, cake@query@where:int(7)) end ), _pipe@9 = cake@query@select:order_asc( _pipe@8, <<"will_be_removed"/utf8>> ), cake@query@select:where(_pipe@9, Where_b) end, Union_query = begin _pipe@10 = [Select_query_a, Select_query_b], _pipe@11 = cake@query@combined:union_all(_pipe@10), _pipe@12 = cake@query@combined:set_limit(_pipe@11, 1), _pipe@13 = cake@query@combined:order_replace( _pipe@12, <<"age"/utf8>>, asc ), _pipe@14 = cake@query@combined:to_query(_pipe@13), cake@stdlib@iox:dbg(_pipe@14) end, Query_decoder = gleam@dynamic:tuple2( fun gleam@dynamic:string/1, fun gleam@dynamic:int/1 ), gleam_erlang_ffi:sleep(100), cake@stdlib@iox:println(<<"SQLite"/utf8>>), _ = begin _pipe@15 = run_on_sqlite(Union_query, Query_decoder), _pipe@16 = cake@stdlib@iox:print_tap(_pipe@15, <<"Result: "/utf8>>), cake@stdlib@iox:dbg(_pipe@16) end, gleam_erlang_ffi:sleep(100), cake@stdlib@iox:println(<<"Postgres"/utf8>>), _ = begin _pipe@17 = run_on_postgres(Union_query, Query_decoder), _pipe@18 = cake@stdlib@iox:print_tap(_pipe@17, <<"Result: "/utf8>>), cake@stdlib@iox:dbg(_pipe@18) 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. -spec stacky() -> any(). stacky() -> cake_ffi:stacky().