Skip to content

Commit 512fafc

Browse files
authored
Add FROM hints (#88)
1 parent a210374 commit 512fafc

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,6 @@ exqlite-*.tar
2929
/tmp
3030

3131
*.db
32+
33+
# ADR files
34+
.tool-versions

integration_test/hints_test.exs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,17 @@ defmodule Ecto.Integration.HintsTest do
2020

2121
assert [%Post{id: 1}] = results
2222
end
23+
24+
test "from hints" do
25+
{:ok, _} = TestRepo.query("CREATE INDEX post_id_idx ON posts (id)")
26+
TestRepo.insert!(%Post{id: 1})
27+
28+
results =
29+
from(Post,
30+
hints: ["INDEXED BY post_id_idx"]
31+
)
32+
|> TestRepo.all()
33+
34+
assert [%Post{id: 1}] = results
35+
end
2336
end

lib/ecto/adapters/sqlite3/connection.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -857,14 +857,15 @@ defmodule Ecto.Adapters.SQLite3.Connection do
857857
end)
858858
end
859859

860-
def from(%{from: %{source: source}} = query, sources) do
860+
def from(%{from: %{source: source, hints: hints}} = query, sources) do
861861
{from, name} = get_source(query, sources, 0, source)
862862

863863
[
864864
" FROM ",
865865
from,
866866
" AS ",
867867
name
868+
| Enum.map(hints, &[?\s | &1])
868869
]
869870
end
870871

test/ecto/adapters/sqlite3/connection_test.exs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,15 @@ defmodule Ecto.Adapters.SQLite3.ConnectionTest do
115115
assert all(query) == ~s{SELECT s0."x" FROM "schema" AS s0}
116116
end
117117

118-
test "ignores from with hints" do
118+
test "from with hints" do
119119
query =
120120
Schema
121-
|> from(hints: ["USE INDEX FOO", "USE INDEX BAR"])
121+
|> from(hints: ["INDEXED BY FOO", "INDEXED BY BAR"])
122122
|> select([r], r.x)
123123
|> plan()
124124

125-
assert all(query) == ~s{SELECT s0."x" FROM "schema" AS s0}
125+
assert all(query) ==
126+
~s{SELECT s0."x" FROM "schema" AS s0 INDEXED BY FOO INDEXED BY BAR}
126127
end
127128

128129
test "from without schema" do

0 commit comments

Comments
 (0)