Skip to content

Commit 2e8e1a9

Browse files
authored
Missing literal, selected_as and using CAST for floats. (#100)
1 parent 3a0e8a5 commit 2e8e1a9

File tree

3 files changed

+82
-4
lines changed

3 files changed

+82
-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: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,30 @@ 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) ==
950+
~s{SELECT 1 FROM "schema" AS s0 WHERE (s0."foo" = CAST(123.0 AS REAL))}
951+
952+
name = "y"
953+
954+
query =
955+
"schema"
956+
|> where(fragment("? = ?", literal(^name), "Main"))
957+
|> select([], true)
958+
|> plan()
959+
960+
assert all(query) == ~s|SELECT 1 FROM "schema" AS s0 WHERE ("y" = 'Main')|
961+
end
962+
963+
test "selected_as" do
964+
query =
965+
from(s in "schema",
966+
select: %{
967+
y: selected_as(s.y, :y2)
968+
}
969+
)
970+
|> plan()
971+
972+
assert all(query) == ~s|SELECT s0."y" AS "y2" FROM "schema" AS s0|
950973
end
951974

952975
test "tagged type" do

test/ecto/integration/crud_test.exs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,5 +244,53 @@ 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 = from(a in Account, where: fragment("? = ?", literal(^name), "Main"))
253+
254+
assert [account] = TestRepo.all(query)
255+
assert account.id == account1.id
256+
end
257+
258+
test "can handle selected_as" do
259+
TestRepo.insert!(%Account{name: "Main"})
260+
TestRepo.insert!(%Account{name: "Main"})
261+
TestRepo.insert!(%Account{name: "Main2"})
262+
TestRepo.insert!(%Account{name: "Main3"})
263+
264+
query =
265+
from(a in Account,
266+
select: %{
267+
name: selected_as(a.name, :name2),
268+
count: count()
269+
},
270+
group_by: selected_as(:name2)
271+
)
272+
273+
assert [
274+
%{name: "Main", count: 2},
275+
%{name: "Main2", count: 1},
276+
%{name: "Main3", count: 1}
277+
] = TestRepo.all(query)
278+
end
279+
280+
test "can handle floats" do
281+
TestRepo.insert!(%Account{name: "Main"})
282+
283+
one = "1.0"
284+
two = 2.0
285+
286+
query =
287+
from(a in Account,
288+
select: %{
289+
sum: ^one + ^two
290+
}
291+
)
292+
293+
assert [%{sum: 3.0}] = TestRepo.all(query)
294+
end
247295
end
248296
end

0 commit comments

Comments
 (0)