Skip to content

Commit 51b9ad2

Browse files
committed
chore: small cleanups
1 parent 3bac027 commit 51b9ad2

1 file changed

Lines changed: 25 additions & 22 deletions

File tree

lib/sql_implementation.ex

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,13 @@ defmodule AshSqlite.SqlImplementation do
9090
Ecto.Query.dynamic(like(fragment("LOWER(?)", ^arg1), fragment("LOWER(?)", ^arg2)))
9191
end
9292

93-
if type != Ash.Type.Boolean do
94-
{:ok, inner_dyn, acc}
93+
# `like`/`ilike` produce SQLite's 0/1 integer result, so cast back to a
94+
# proper boolean when that's the expected output type. `type` typically
95+
# arrives as a `{Ash.Type.Boolean, constraints}` tuple, so match both forms.
96+
if boolean_type?(type) do
97+
{:ok, Ecto.Query.dynamic(type(^inner_dyn, :boolean)), acc}
9598
else
96-
{:ok, Ecto.Query.dynamic(type(^inner_dyn, ^type)), acc}
99+
{:ok, inner_dyn, acc}
97100
end
98101
end
99102

@@ -292,12 +295,13 @@ defmodule AshSqlite.SqlImplementation do
292295
defp plain_map?(value) when is_map(value) and not is_struct(value), do: true
293296
defp plain_map?(_), do: false
294297

295-
defp in_item_type(%Ash.Query.Ref{attribute: %{type: type} = attribute}) do
296-
{type, Map.get(attribute, :constraints, []) || []}
298+
defp in_item_type(left) do
299+
case Ash.Expr.determine_type(left) do
300+
{:ok, {type, constraints}} -> {type, constraints || []}
301+
_ -> {nil, []}
302+
end
297303
end
298304

299-
defp in_item_type(_), do: {nil, []}
300-
301305
defp in_left_bindings(bindings, item_type, constraints) do
302306
if ci_string_type?(item_type, constraints) do
303307
bindings
@@ -357,7 +361,7 @@ defmodule AshSqlite.SqlImplementation do
357361
|> Ash.Type.get_type()
358362
|> Ash.Type.storage_type(constraints)
359363

360-
adapter = sqlite_adapter!(query, bindings)
364+
adapter = sqlite_adapter(query, bindings)
361365

362366
Enum.map(values, fn value ->
363367
case Ecto.Type.adapter_dump(adapter, ecto_type, value) do
@@ -368,22 +372,21 @@ defmodule AshSqlite.SqlImplementation do
368372
end)
369373
end
370374

371-
defp sqlite_adapter!(query, bindings) do
372-
repo =
373-
bindings
374-
|> Map.fetch!(:resource)
375-
|> AshSql.dynamic_repo(__MODULE__, query)
376-
377-
case repo.__adapter__() do
378-
Ecto.Adapters.SQLite3 ->
379-
Ecto.Adapters.SQLite3
380-
381-
adapter ->
382-
raise ArgumentError,
383-
"expected #{inspect(repo)} to use sqlite adapter `Ecto.Adapters.SQLite3`, got: #{inspect(adapter)}"
384-
end
375+
# Every `AshSqlite.Repo` is compiled with `adapter: Ecto.Adapters.SQLite3`,
376+
# so we can extract the adapter without asserting on it.
377+
defp sqlite_adapter(query, bindings) do
378+
bindings
379+
|> Map.fetch!(:resource)
380+
|> AshSql.dynamic_repo(__MODULE__, query)
381+
|> then(& &1.__adapter__())
385382
end
386383

384+
defp boolean_type?(Ash.Type.Boolean), do: true
385+
defp boolean_type?({Ash.Type.Boolean, _}), do: true
386+
defp boolean_type?(:boolean), do: true
387+
defp boolean_type?({:boolean, _}), do: true
388+
defp boolean_type?(_), do: false
389+
387390
defp ci_string_type?({:parameterized, {inner_type, constraints}}, []) do
388391
parameterized_ci_string_type?(inner_type, constraints)
389392
end

0 commit comments

Comments
 (0)