Skip to content

Add from hints #88

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 1 commit into from
Aug 30, 2022
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ exqlite-*.tar
/tmp

*.db

# ADR files
.tool-versions
13 changes: 13 additions & 0 deletions integration_test/hints_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,17 @@ defmodule Ecto.Integration.HintsTest do

assert [%Post{id: 1}] = results
end

test "from hints" do
{:ok, _} = TestRepo.query("CREATE INDEX post_id_idx ON posts (id)")
TestRepo.insert!(%Post{id: 1})

results =
from(Post,
hints: ["INDEXED BY post_id_idx"]
)
|> TestRepo.all()

assert [%Post{id: 1}] = results
end
end
3 changes: 2 additions & 1 deletion lib/ecto/adapters/sqlite3/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -857,14 +857,15 @@ defmodule Ecto.Adapters.SQLite3.Connection do
end)
end

def from(%{from: %{source: source}} = query, sources) do
def from(%{from: %{source: source, hints: hints}} = query, sources) do
{from, name} = get_source(query, sources, 0, source)

[
" FROM ",
from,
" AS ",
name
| Enum.map(hints, &[?\s | &1])
]
end

Expand Down
7 changes: 4 additions & 3 deletions test/ecto/adapters/sqlite3/connection_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,15 @@ defmodule Ecto.Adapters.SQLite3.ConnectionTest do
assert all(query) == ~s{SELECT s0."x" FROM "schema" AS s0}
end

test "ignores from with hints" do
test "from with hints" do
query =
Schema
|> from(hints: ["USE INDEX FOO", "USE INDEX BAR"])
|> from(hints: ["INDEXED BY FOO", "INDEXED BY BAR"])
|> select([r], r.x)
|> plan()

assert all(query) == ~s{SELECT s0."x" FROM "schema" AS s0}
assert all(query) ==
~s{SELECT s0."x" FROM "schema" AS s0 INDEXED BY FOO INDEXED BY BAR}
end

test "from without schema" do
Expand Down