Skip to content

Commit 069ed71

Browse files
author
Jason Stiebs
committed
Missing literal, selected_as and using CAST for floats.
1 parent 3a0e8a5 commit 069ed71

File tree

3 files changed

+77
-4
lines changed

3 files changed

+77
-4
lines changed

lib/ecto/adapters/sqlite3/connection.ex

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,7 +1203,6 @@ defmodule Ecto.Adapters.SQLite3.Connection do
12031203
quote_name(field)
12041204
end
12051205

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

1290+
def expr({:literal, _, [literal]}, _sources, _query) do
1291+
quote_name(literal)
1292+
end
1293+
1294+
def expr({:selected_as, _, [name]}, _sources, _query) do
1295+
[quote_name(name)]
1296+
end
1297+
12911298
def expr({:datetime_add, _, [datetime, count, interval]}, sources, query) do
12921299
[
12931300
"CAST (",
@@ -1366,6 +1373,7 @@ defmodule Ecto.Adapters.SQLite3.Connection do
13661373
end
13671374
end
13681375

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

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

14111418
def expr(expr, _sources, query) do

test/ecto/adapters/sqlite3/connection_test.exs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,28 @@ defmodule Ecto.Adapters.SQLite3.ConnectionTest do
946946
|> select([], true)
947947
|> plan()
948948

949-
assert all(query) == ~s{SELECT 1 FROM "schema" AS s0 WHERE (s0."foo" = (0 + 123.0))}
949+
assert all(query) == ~s{SELECT 1 FROM "schema" AS s0 WHERE (s0."foo" = CAST(123.0 AS REAL))}
950+
951+
name = "y"
952+
query =
953+
"schema"
954+
|> where(fragment("? = ?", literal(^name), "Main"))
955+
|> select([], true)
956+
|> plan()
957+
958+
assert all(query) == ~s|SELECT 1 FROM "schema" AS s0 WHERE ("y" = 'Main')|
959+
end
960+
961+
test "selected_as" do
962+
query =
963+
from(s in "schema",
964+
select: %{
965+
y: selected_as(s.y, :y2),
966+
}
967+
)
968+
|> plan()
969+
970+
assert all(query) == ~s|SELECT s0."y" AS "y2" FROM "schema" AS s0|
950971
end
951972

952973
test "tagged type" do

test/ecto/integration/crud_test.exs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,5 +244,50 @@ defmodule Ecto.Integration.CrudTest do
244244

245245
assert [_] = TestRepo.all(from(a in Account, as: :user, where: exists(subquery)))
246246
end
247+
248+
test "can handle fragment literal" do
249+
account1 = TestRepo.insert!(%Account{name: "Main"})
250+
251+
name = "name"
252+
query =
253+
from(a in Account, where: fragment("? = ?", literal(^name), "Main"))
254+
255+
assert [account] = TestRepo.all(query)
256+
assert account.id == account1.id
257+
end
258+
259+
test "can handle selected_as" do
260+
TestRepo.insert!(%Account{name: "Main"})
261+
TestRepo.insert!(%Account{name: "Main"})
262+
TestRepo.insert!(%Account{name: "Main2"})
263+
TestRepo.insert!(%Account{name: "Main3"})
264+
265+
query =
266+
from(a in Account,
267+
select: %{
268+
name: selected_as(a.name, :name2),
269+
count: count()
270+
},
271+
group_by: selected_as(:name2)
272+
)
273+
274+
assert [%{name: "Main", count: 2}, %{name: "Main2", count: 1}, %{name: "Main3", count: 1}] = TestRepo.all(query)
275+
end
276+
277+
test "can handle floats" do
278+
TestRepo.insert!(%Account{name: "Main"})
279+
280+
one = "1.0"
281+
two = 2.0
282+
283+
query =
284+
from(a in Account,
285+
select: %{
286+
sum: ^one + ^two
287+
}
288+
)
289+
290+
assert [%{sum: 3.0}] = TestRepo.all(query)
291+
end
247292
end
248293
end

0 commit comments

Comments
 (0)