Skip to content

Commit 57cde61

Browse files
committed
completes delete_all/1 API
1 parent a82e18a commit 57cde61

File tree

2 files changed

+38
-25
lines changed

2 files changed

+38
-25
lines changed

lib/sqlite_ecto/query.ex

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ defmodule Sqlite.Ecto.Query do
5252
end
5353

5454
def delete_all(query) do
55+
if query.joins != [] do
56+
raise ArgumentError, "JOINS are not supported on DELETE statements by SQLite"
57+
end
58+
59+
sources = create_names(query, :delete)
60+
{table, _name, _model} = elem(sources, 0)
61+
where = where(query.wheres, sources)
62+
assemble ["DELETE FROM", quote_id(table), where]
5563
end
5664

5765
# XXX How do we handle inserting datetime values?
@@ -390,7 +398,7 @@ defmodule Sqlite.Ecto.Query do
390398
if stmt == :select do
391399
id = String.first(table) <> Integer.to_string(pos)
392400
else
393-
id = nil
401+
id = quote_id(table)
394402
end
395403
[{table, id, model} | create_names(sources, pos + 1, limit, stmt)]
396404
end
@@ -421,11 +429,7 @@ defmodule Sqlite.Ecto.Query do
421429

422430
defp expr({{:., _, [{:&, _, [idx]}, field]}, _, []}, sources) when is_atom(field) do
423431
{_, name, _} = elem(sources, idx)
424-
if name do
425-
"#{name}.#{quote_id(field)}"
426-
else
427-
quote_id(field)
428-
end
432+
"#{name}.#{quote_id(field)}"
429433
end
430434

431435
defp expr({:in, _, [left, right]}, sources) when is_list(right) do

test/sqlite_ecto_test.exs

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ defmodule Sqlite.Ecto.Test do
252252
test "distinct" do
253253
assert_raise ArgumentError, "DISTINCT with multiple columns is not supported by SQLite", fn ->
254254
query = Model |> distinct([r], r.x) |> select([r], {r.x, r.y}) |> normalize
255-
assert SQL.all(query)
255+
SQL.all(query)
256256
end
257257

258258
query = Model |> distinct([r], true) |> select([r], {r.x, r.y}) |> normalize
@@ -298,7 +298,7 @@ defmodule Sqlite.Ecto.Test do
298298
test "lock" do
299299
assert_raise ArgumentError, "locks are not supported by SQLite", fn ->
300300
query = Model |> lock("FOR SHARE NOWAIT") |> select([], 0) |> normalize
301-
assert SQL.all(query)
301+
SQL.all(query)
302302
end
303303
end
304304

@@ -370,15 +370,15 @@ defmodule Sqlite.Ecto.Test do
370370
test "tagged type" do
371371
assert_raise ArgumentError, "UUID is not supported by SQLite", fn ->
372372
query = Model |> select([], type(^<<0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15>>, :uuid)) |> normalize
373-
assert SQL.all(query)
373+
SQL.all(query)
374374
end
375375

376376
query = Model |> select([], type(^1, :float)) |> normalize
377377
assert SQL.all(query) == ~s{SELECT CAST ( ? AS NUMERIC ) FROM "model" AS m0}
378378

379379
assert_raise ArgumentError, "Array type is not supported by SQLite", fn ->
380380
query = Model |> select([], type(^[1,2,3], {:array, :integer})) |> normalize
381-
assert SQL.all(query)
381+
SQL.all(query)
382382
end
383383
end
384384

@@ -510,7 +510,7 @@ defmodule Sqlite.Ecto.Test do
510510
assert SQL.update_all(query, [x: 0]) == ~s{UPDATE "model" SET "x" = 0}
511511

512512
query = from(e in Model, where: e.x == 123) |> normalize
513-
assert SQL.update_all(query, [x: 0]) == ~s{UPDATE "model" SET "x" = 0 WHERE ( "x" = 123 )}
513+
assert SQL.update_all(query, [x: 0]) == ~s{UPDATE "model" SET "x" = 0 WHERE ( "model"."x" = 123 )}
514514

515515
query = Model |> Queryable.to_query |> normalize
516516
assert SQL.update_all(query, [x: 0, y: "123"]) == ~s{UPDATE "model" SET "x" = 0, "y" = '123'}
@@ -520,24 +520,33 @@ defmodule Sqlite.Ecto.Test do
520520

521521
assert_raise ArgumentError, "JOINS are not supported on UPDATE statements by SQLite", fn ->
522522
query = Model |> join(:inner, [p], q in Model2, p.x == q.z) |> normalize
523-
assert SQL.update_all(query, [x: 0])
523+
SQL.update_all(query, [x: 0])
524524
end
525525
end
526526

527-
# test "delete all" do
528-
# query = Model |> Queryable.to_query |> normalize
529-
# assert SQL.delete_all(query) == ~s{DELETE FROM "model" AS m0}
530-
#
531-
# query = from(e in Model, where: e.x == 123) |> normalize
532-
# assert SQL.delete_all(query) ==
533-
# ~s{DELETE FROM "model" AS m0 WHERE (m0."x" = 123)}
534-
#
527+
test "delete all" do
528+
query = Model |> Queryable.to_query |> normalize
529+
assert SQL.delete_all(query) == ~s{DELETE FROM "model"}
530+
531+
query = from(e in Model, where: e.x == 123) |> normalize
532+
assert SQL.delete_all(query) == ~s{DELETE FROM "model" WHERE ( "model"."x" = 123 )}
533+
534+
assert_raise ArgumentError, "JOINS are not supported on DELETE statements by SQLite", fn ->
535+
query = Model |> join(:inner, [p], q in Model2, p.x == q.z) |> normalize
536+
SQL.delete_all(query)
537+
end
538+
539+
# The assertions commented out below represent how joins *could* be
540+
# handled in SQLite to produce the same effect. Evenually, joins should
541+
# be converted to the below output. Until then, joins should raise
542+
# exceptions.
543+
535544
# query = Model |> join(:inner, [p], q in Model2, p.x == q.z) |> normalize
536-
# assert SQL.delete_all(query) ==
537-
# ~s{DELETE FROM "model" AS m0 USING "model2" AS m1 WHERE m0."x" = m1."z"}
545+
# #assert SQL.delete_all(query) == ~s{DELETE FROM "model" AS m0 USING "model2" AS m1 WHERE m0."x" = m1."z"}
546+
# assert SQL.delete_all(query) == ~s{DELETE FROM "model" WHERE "model"."x" IN ( SELECT m1."z" FROM "model2" AS m1 )}
538547
#
539548
# query = from(e in Model, where: e.x == 123, join: q in Model2, on: e.x == q.z) |> normalize
540-
# assert SQL.delete_all(query) ==
541-
# ~s{DELETE FROM "model" AS m0 USING "model2" AS m1 WHERE m0."x" = m1."z" AND (m0."x" = 123)}
542-
# end
549+
# #assert SQL.delete_all(query) == ~s{DELETE FROM "model" AS m0 USING "model2" AS m1 WHERE m0."x" = m1."z" AND (m0."x" = 123)}
550+
# assert SQL.delete_all(query) == ~s{DELETE FROM "model" WHERE "model"."x" IN ( SELECT m1."z" FROM "model2" AS m1 ) AND ( "model"."x" = 123 )}
551+
end
543552
end

0 commit comments

Comments
 (0)