Skip to content

Missing literal, selected_as and using CAST for floats. #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions lib/ecto/adapters/sqlite3/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1203,7 +1203,6 @@ defmodule Ecto.Adapters.SQLite3.Connection do
quote_name(field)
end

# def expr({{:., _, [{:parent_as, _, [{:&, _, [idx]}]}, field]}, _, []}, _sources, query)
def expr({{:., _, [{:parent_as, _, [as]}, field]}, _, []}, _sources, query)
when is_atom(field) do
{ix, sources} = get_parent_sources_ix(query, as)
Expand Down Expand Up @@ -1288,6 +1287,14 @@ defmodule Ecto.Adapters.SQLite3.Connection do
|> parens_for_select
end

def expr({:literal, _, [literal]}, _sources, _query) do
quote_name(literal)
end

def expr({:selected_as, _, [name]}, _sources, _query) do
[quote_name(name)]
end

def expr({:datetime_add, _, [datetime, count, interval]}, sources, query) do
[
"CAST (",
Expand Down Expand Up @@ -1366,6 +1373,7 @@ defmodule Ecto.Adapters.SQLite3.Connection do
end
end

# TODO It technically is, its just a json array, so we *could* support it
def expr(list, _sources, query) when is_list(list) do
raise Ecto.QueryError,
query: query,
Expand Down Expand Up @@ -1404,8 +1412,7 @@ defmodule Ecto.Adapters.SQLite3.Connection do
end

def expr(literal, _sources, _query) when is_float(literal) do
# Unsure if SQLite3 supports float casting
["(0 + ", Float.to_string(literal), ?)]
["CAST(", Float.to_string(literal), " AS REAL)"]
end

def expr(expr, _sources, query) do
Expand Down
25 changes: 24 additions & 1 deletion test/ecto/adapters/sqlite3/connection_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,30 @@ defmodule Ecto.Adapters.SQLite3.ConnectionTest do
|> select([], true)
|> plan()

assert all(query) == ~s{SELECT 1 FROM "schema" AS s0 WHERE (s0."foo" = (0 + 123.0))}
assert all(query) ==
~s{SELECT 1 FROM "schema" AS s0 WHERE (s0."foo" = CAST(123.0 AS REAL))}

name = "y"

query =
"schema"
|> where(fragment("? = ?", literal(^name), "Main"))
|> select([], true)
|> plan()

assert all(query) == ~s|SELECT 1 FROM "schema" AS s0 WHERE ("y" = 'Main')|
end

test "selected_as" do
query =
from(s in "schema",
select: %{
y: selected_as(s.y, :y2)
}
)
|> plan()

assert all(query) == ~s|SELECT s0."y" AS "y2" FROM "schema" AS s0|
end

test "tagged type" do
Expand Down
48 changes: 48 additions & 0 deletions test/ecto/integration/crud_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -244,5 +244,53 @@ defmodule Ecto.Integration.CrudTest do

assert [_] = TestRepo.all(from(a in Account, as: :user, where: exists(subquery)))
end

test "can handle fragment literal" do
account1 = TestRepo.insert!(%Account{name: "Main"})

name = "name"
query = from(a in Account, where: fragment("? = ?", literal(^name), "Main"))

assert [account] = TestRepo.all(query)
assert account.id == account1.id
end

test "can handle selected_as" do
TestRepo.insert!(%Account{name: "Main"})
TestRepo.insert!(%Account{name: "Main"})
TestRepo.insert!(%Account{name: "Main2"})
TestRepo.insert!(%Account{name: "Main3"})

query =
from(a in Account,
select: %{
name: selected_as(a.name, :name2),
count: count()
},
group_by: selected_as(:name2)
)

assert [
%{name: "Main", count: 2},
%{name: "Main2", count: 1},
%{name: "Main3", count: 1}
] = TestRepo.all(query)
end

test "can handle floats" do
TestRepo.insert!(%Account{name: "Main"})

one = "1.0"
two = 2.0

query =
from(a in Account,
select: %{
sum: ^one + ^two
}
)

assert [%{sum: 3.0}] = TestRepo.all(query)
end
end
end