-module(sqlode@query_analyzer@context). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/sqlode/query_analyzer/context.gleam"). -export([analysis_error_to_string/1, new/1, find_column/3, find_column_in_tables/3]). -export_type([analysis_error/0, analyzer_context/0]). -if(?OTP_RELEASE >= 27). -define(MODULEDOC(Str), -moduledoc(Str)). -define(DOC(Str), -doc(Str)). -else. -define(MODULEDOC(Str), -compile([])). -define(DOC(Str), -compile([])). -endif. -type analysis_error() :: {table_not_found, binary(), binary()} | {column_not_found, binary(), binary(), binary()} | {parameter_type_not_inferred, binary(), integer()} | {unrecognized_cast_type, binary(), integer(), binary()} | {compound_column_count_mismatch, binary(), integer(), integer()} | {unsupported_expression, binary(), binary()}. -type analyzer_context() :: {analyzer_context, sqlode@naming:naming_context()}. -file("src/sqlode/query_analyzer/context.gleam", 20). -spec analysis_error_to_string(analysis_error()) -> binary(). analysis_error_to_string(Error) -> case Error of {table_not_found, Query_name, Table_name} -> <<<<<<<<"Query \""/utf8, Query_name/binary>>/binary, "\": table \""/utf8>>/binary, Table_name/binary>>/binary, "\" not found in schema"/utf8>>; {column_not_found, Query_name@1, Table_name@1, Column_name} -> <<<<<<<<<<<<"Query \""/utf8, Query_name@1/binary>>/binary, "\": column \""/utf8>>/binary, Column_name/binary>>/binary, "\" not found in table \""/utf8>>/binary, Table_name@1/binary>>/binary, "\""/utf8>>; {parameter_type_not_inferred, Query_name@2, Param_index} -> <<<<<<<<<<<<"Query \""/utf8, Query_name@2/binary>>/binary, "\": could not infer type for parameter $"/utf8>>/binary, (erlang:integer_to_binary(Param_index))/binary>>/binary, ". Use a type cast (e.g. $"/utf8>>/binary, (erlang:integer_to_binary(Param_index))/binary>>/binary, "::int) to specify the type"/utf8>>; {unrecognized_cast_type, Query_name@3, Param_index@1, Cast_type} -> <<<<<<<<<<"Query \""/utf8, Query_name@3/binary>>/binary, "\": unrecognized cast type \""/utf8>>/binary, Cast_type/binary>>/binary, "\" for parameter $"/utf8>>/binary, (erlang:integer_to_binary(Param_index@1))/binary>>; {compound_column_count_mismatch, Query_name@4, First_count, Branch_count} -> <<<<<<<<<<"Query \""/utf8, Query_name@4/binary>>/binary, "\": compound query branch has "/utf8>>/binary, (erlang:integer_to_binary(Branch_count))/binary>>/binary, " columns, but the first branch has "/utf8>>/binary, (erlang:integer_to_binary(First_count))/binary>>; {unsupported_expression, Query_name@5, Expression} -> <<<<<<<<"Query \""/utf8, Query_name@5/binary>>/binary, "\": unsupported expression \""/utf8>>/binary, Expression/binary>>/binary, "\", cannot infer result type. Use CAST to specify the type explicitly"/utf8>> end. -file("src/sqlode/query_analyzer/context.gleam", 71). -spec new(sqlode@naming:naming_context()) -> analyzer_context(). new(Naming_ctx) -> {analyzer_context, Naming_ctx}. -file("src/sqlode/query_analyzer/context.gleam", 75). -spec find_column(sqlode@model:catalog(), binary(), binary()) -> gleam@option:option(sqlode@model:column()). find_column(Catalog, Table_name, Column_name) -> case begin _pipe = erlang:element(2, Catalog), gleam@list:find( _pipe, fun(Table) -> erlang:element(2, Table) =:= sqlode@naming:normalize_identifier( Table_name ) end ) end of {ok, Table@1} -> _pipe@1 = erlang:element(3, Table@1), _pipe@2 = gleam@list:find( _pipe@1, fun(Column) -> erlang:element(2, Column) =:= sqlode@naming:normalize_identifier( Column_name ) end ), gleam@option:from_result(_pipe@2); {error, _} -> none end. -file("src/sqlode/query_analyzer/context.gleam", 98). ?DOC( " Search for a column across multiple tables, returning the column and the\n" " table name where it was found.\n" ). -spec find_column_in_tables(sqlode@model:catalog(), list(binary()), binary()) -> gleam@option:option({binary(), sqlode@model:column()}). find_column_in_tables(Catalog, Table_names, Column_name) -> _pipe = gleam@list:find_map( Table_names, fun(Name) -> case find_column(Catalog, Name, Column_name) of {some, Col} -> {ok, {Name, Col}}; none -> {error, nil} end end ), gleam@option:from_result(_pipe).