-module(feather). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([main/0, default_config/0, connect/1, disconnect/1, optimize/1]). -export_type([journal_mode/0, sync_mode/0, temp_store/0, config/0]). -type journal_mode() :: journal_delete | journal_truncate | journal_persist | journal_memory | journal_wal | journal_off. -type sync_mode() :: sync_extra | sync_full | sync_normal | sync_off. -type temp_store() :: temp_store_default | temp_store_file | temp_store_memory. -type config() :: {config, binary(), journal_mode(), sync_mode(), temp_store(), gleam@option:option(integer()), gleam@option:option(integer())}. -spec main() -> nil. main() -> feather@migrate:main(). -spec default_config() -> config(). default_config() -> {config, <<"./sqlite.db"/utf8>>, journal_wal, sync_normal, temp_store_memory, none, none}. -spec connect(config()) -> {ok, sqlight:connection()} | {error, sqlight:error()}. connect(Config) -> gleam@result:'try'( sqlight:open(erlang:element(2, Config)), fun(Connection) -> Journal_mode = case erlang:element(3, Config) of journal_off -> <<"OFF"/utf8>>; journal_wal -> <<"WAL"/utf8>>; journal_delete -> <<"DELETE"/utf8>>; journal_memory -> <<"MEMORY"/utf8>>; journal_truncate -> <<"TRUNCATE"/utf8>>; journal_persist -> <<"PERSIST"/utf8>> end, Sync = case erlang:element(4, Config) of sync_off -> <<"OFF"/utf8>>; sync_full -> <<"FULL"/utf8>>; sync_extra -> <<"EXTRA"/utf8>>; sync_normal -> <<"NORMAL"/utf8>> end, Temp_store = case erlang:element(5, Config) of temp_store_file -> <<"FILE"/utf8>>; temp_store_memory -> <<"MEMORY"/utf8>>; temp_store_default -> <<"DEFAULT"/utf8>> end, gleam@result:'try'( sqlight:exec( <<<<"PRAGMA journal_mode = "/utf8, Journal_mode/binary>>/binary, ";"/utf8>>, Connection ), fun(_) -> gleam@result:'try'( sqlight:exec( <<<<"PRAGMA synchronous = "/utf8, Sync/binary>>/binary, ";"/utf8>>, Connection ), fun(_) -> gleam@result:'try'( sqlight:exec( <<<<"PRAGMA temp_store = "/utf8, Temp_store/binary>>/binary, ";"/utf8>>, Connection ), fun(_) -> gleam@result:'try'( begin _pipe = gleam@option:map( erlang:element(6, Config), fun gleam@int:to_string/1 ), _pipe@1 = gleam@option:map( _pipe, fun(Size) -> sqlight:exec( <<<<"PRAGMA mmap_size = "/utf8, Size/binary>>/binary, ";"/utf8>>, Connection ) end ), gleam@option:unwrap( _pipe@1, {ok, nil} ) end, fun(_) -> gleam@result:'try'( begin _pipe@2 = gleam@option:map( erlang:element( 7, Config ), fun gleam@int:to_string/1 ), _pipe@3 = gleam@option:map( _pipe@2, fun(Size@1) -> sqlight:exec( <<<<"PRAGMA page_size = "/utf8, Size@1/binary>>/binary, ";"/utf8>>, Connection ) end ), gleam@option:unwrap( _pipe@3, {ok, nil} ) end, fun(_) -> {ok, Connection} end ) end ) end ) end ) end ) end ). -spec disconnect(sqlight:connection()) -> {ok, nil} | {error, sqlight:error()}. disconnect(Connection) -> _ = sqlight:exec(<<"PRAGMA optimize;"/utf8>>, Connection), sqlight:close(Connection). -spec optimize(sqlight:connection()) -> {ok, nil} | {error, sqlight:error()}. optimize(Connection) -> sqlight:exec(<<"PRAGMA optimize;"/utf8>>, Connection).