Skip to content

remove duplicate execute_ddl, @impl true, table prefixes #103

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 5 commits into from
Feb 20, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ This is a breaking change in the sense that rebuilding the schema from scratch w

We kept `TEXT_DATETIME` to satisfy the old Ecto2 implementation to keep backwards compatibility.

- **breaking**: raise when table prefixes are used. [#103](https://github.com/elixir-sqlite/ecto_sqlite3/pull/103)

## v0.9.1 - 2022-12-21
- changed: Use `Connection.connect` instead of `Sqlite3.open`. [#96](https://github.com/elixir-sqlite/ecto_sqlite3/pull/96)
Expand Down
141 changes: 6 additions & 135 deletions lib/ecto/adapters/sqlite3/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ defmodule Ecto.Adapters.SQLite3.Connection do
DBConnection.execute(conn, cached, params, options)
end

@impl true
def execute(
conn,
%Exqlite.Query{statement: statement, ref: nil},
Expand All @@ -64,7 +63,6 @@ defmodule Ecto.Adapters.SQLite3.Connection do
execute(conn, statement, params, options)
end

@impl true
def execute(conn, sql, params, options) when is_binary(sql) or is_list(sql) do
query = Exqlite.Query.build(name: "", statement: IO.iodata_to_binary(sql))

Expand All @@ -75,7 +73,6 @@ defmodule Ecto.Adapters.SQLite3.Connection do
end
end

@impl true
def execute(conn, query, params, options) do
case DBConnection.execute(conn, query, params, options) do
{:ok, _} = ok -> ok
Expand Down Expand Up @@ -163,7 +160,6 @@ defmodule Ecto.Adapters.SQLite3.Connection do
raise ArgumentError, "locks are not supported by SQLite3"
end

@impl true
def all(query, as_prefix \\ []) do
sources = create_names(query, as_prefix)

Expand Down Expand Up @@ -233,7 +229,6 @@ defmodule Ecto.Adapters.SQLite3.Connection do
raise ArgumentError, "JOINS are not supported on DELETE statements by SQLite"
end

@impl true
def delete_all(query) do
sources = create_names(query, [])
cte = cte(query, sources)
Expand Down Expand Up @@ -261,7 +256,6 @@ defmodule Ecto.Adapters.SQLite3.Connection do
]
end

@impl true
def insert(prefix, table, header, rows, on_conflict, returning, _placeholders) do
fields = quote_names(header)

Expand Down Expand Up @@ -375,7 +369,6 @@ defmodule Ecto.Adapters.SQLite3.Connection do
raise ArgumentError, "SQLite3 adapter does not support keyword lists in :options"
end

@impl true
def execute_ddl({:create, %Table{} = table, columns}) do
{table, composite_pk_def} = composite_pk_definition(table, columns)
composite_fk_defs = composite_fk_definitions(table, columns)
Expand All @@ -395,7 +388,6 @@ defmodule Ecto.Adapters.SQLite3.Connection do
]
end

@impl true
def execute_ddl({:create_if_not_exists, %Table{} = table, columns}) do
{table, composite_pk_def} = composite_pk_definition(table, columns)
composite_fk_defs = composite_fk_definitions(table, columns)
Expand All @@ -415,7 +407,6 @@ defmodule Ecto.Adapters.SQLite3.Connection do
]
end

@impl true
def execute_ddl({:drop, %Table{} = table}) do
[
[
Expand All @@ -425,12 +416,10 @@ defmodule Ecto.Adapters.SQLite3.Connection do
]
end

@impl true
def execute_ddl({:drop, %Table{} = table, _mode}) do
execute_ddl({:drop, table})
end

@impl true
def execute_ddl({:drop_if_exists, %Table{} = table}) do
[
[
Expand All @@ -440,12 +429,10 @@ defmodule Ecto.Adapters.SQLite3.Connection do
]
end

@impl true
def execute_ddl({:drop_if_exists, %Table{} = table, _mode}) do
execute_ddl({:drop_if_exists, table})
end

@impl true
def execute_ddl({:alter, %Table{} = table, changes}) do
Enum.map(changes, fn change ->
[
Expand All @@ -457,111 +444,6 @@ defmodule Ecto.Adapters.SQLite3.Connection do
end)
end

@impl true
def execute_ddl({:create, %Index{} = index}) do
fields = intersperse_map(index.columns, ", ", &index_expr/1)

[
[
"CREATE ",
if_do(index.unique, "UNIQUE "),
"INDEX ",
quote_name(index.name),
" ON ",
quote_table(index.prefix, index.table),
" (",
fields,
?),
if_do(index.where, [" WHERE ", to_string(index.where)])
]
]
end

@impl true
def execute_ddl({:create_if_not_exists, %Index{} = index}) do
fields = intersperse_map(index.columns, ", ", &index_expr/1)

[
[
"CREATE ",
if_do(index.unique, "UNIQUE "),
"INDEX IF NOT EXISTS ",
quote_name(index.name),
" ON ",
quote_table(index.prefix, index.table),
" (",
fields,
?),
if_do(index.where, [" WHERE ", to_string(index.where)])
]
]
end

@impl true
def execute_ddl({:drop, %Index{} = index}) do
[
[
"DROP INDEX ",
quote_table(index.prefix, index.name)
]
]
end

@impl true
def execute_ddl({:drop, %Index{} = index, _mode}) do
execute_ddl({:drop, index})
end

@impl true
def execute_ddl({:drop_if_exists, %Index{} = index}) do
[
[
"DROP INDEX IF EXISTS ",
quote_table(index.prefix, index.name)
]
]
end

@impl true
def execute_ddl({:drop_if_exists, %Index{} = index, _mode}) do
execute_ddl({:drop_if_exists, index})
end

@impl true
def execute_ddl({:rename, %Table{} = current_table, %Table{} = new_table}) do
[
[
"ALTER TABLE ",
quote_table(current_table.prefix, current_table.name),
" RENAME TO ",
quote_table(nil, new_table.name)
]
]
end

@impl true
def execute_ddl({:rename, %Table{} = current_table, old_col, new_col}) do
[
[
"ALTER TABLE ",
quote_table(current_table.prefix, current_table.name),
" RENAME COLUMN ",
quote_name(old_col),
" TO ",
quote_name(new_col)
]
]
end

@impl true
def execute_ddl(string) when is_binary(string), do: [string]

@impl true
def execute_ddl(keyword) when is_list(keyword) do
raise ArgumentError, "SQLite3 adapter does not support keyword lists in execute"
end

@impl true
def execute_ddl({:create, %Index{} = index}) do
fields = intersperse_map(index.columns, ", ", &index_expr/1)

Expand All @@ -574,16 +456,14 @@ defmodule Ecto.Adapters.SQLite3.Connection do
quote_name(index.name),
" ON ",
quote_table(index.prefix, index.table),
?\s,
?(,
" (",
fields,
?),
if_do(index.where, [" WHERE ", to_string(index.where)])
]
]
end

@impl true
def execute_ddl({:create_if_not_exists, %Index{} = index}) do
fields = intersperse_map(index.columns, ", ", &index_expr/1)

Expand All @@ -596,21 +476,18 @@ defmodule Ecto.Adapters.SQLite3.Connection do
quote_name(index.name),
" ON ",
quote_table(index.prefix, index.table),
?\s,
?(,
" (",
fields,
?),
if_do(index.where, [" WHERE ", to_string(index.where)])
]
]
end

@impl true
def execute_ddl({:create, %Constraint{}}) do
raise ArgumentError, "SQLite3 does not support ALTER TABLE ADD CONSTRAINT."
end

@impl true
def execute_ddl({:drop, %Index{} = index}) do
[
[
Expand All @@ -620,12 +497,10 @@ defmodule Ecto.Adapters.SQLite3.Connection do
]
end

@impl true
def execute_ddl({:drop, %Index{} = index, _mode}) do
execute_ddl({:drop, index})
end

@impl true
def execute_ddl({:drop_if_exists, %Index{} = index}) do
[
[
Expand All @@ -635,22 +510,18 @@ defmodule Ecto.Adapters.SQLite3.Connection do
]
end

@impl true
def execute_ddl({:drop_if_exists, %Index{} = index, _mode}) do
execute_ddl({:drop_if_exists, index})
end

@impl true
def execute_ddl({:drop, %Constraint{}, _mode}) do
raise ArgumentError, "SQLite3 does not support ALTER TABLE DROP CONSTRAINT."
end

@impl true
def execute_ddl({:drop_if_exists, %Constraint{}, _mode}) do
raise ArgumentError, "SQLite3 does not support ALTER TABLE DROP CONSTRAINT."
end

@impl true
def execute_ddl({:rename, %Table{} = current_table, %Table{} = new_table}) do
[
[
Expand All @@ -662,7 +533,6 @@ defmodule Ecto.Adapters.SQLite3.Connection do
]
end

@impl true
def execute_ddl({:rename, %Table{} = table, current_column, new_column}) do
[
[
Expand All @@ -676,10 +546,8 @@ defmodule Ecto.Adapters.SQLite3.Connection do
]
end

@impl true
def execute_ddl(string) when is_binary(string), do: [string]

@impl true
def execute_ddl(keyword) when is_list(keyword) do
raise ArgumentError, "SQLite3 adapter does not support keyword lists in execute"
end
Expand Down Expand Up @@ -1799,7 +1667,10 @@ defmodule Ecto.Adapters.SQLite3.Connection do
def quote_table(table), do: quote_entity(table)

defp quote_table(nil, name), do: quote_entity(name)
defp quote_table(prefix, name), do: [quote_entity(prefix), ?., quote_entity(name)]

defp quote_table(_prefix, _name) do
raise ArgumentError, "SQLite3 does not support table prefixes"
end

defp quote_entity(val) when is_atom(val) do
quote_entity(Atom.to_string(val))
Expand Down
Loading