我试图更好地了解Elixir中的Ecto适配器。我已经开始尝试使用Ecto.Adapters.Postgres
作为基础构建我自己的适配器。这似乎是一个很好的选择,因为它是Phoenix使用的默认适配器。
我现在可以通过在项目的repo文件中更新以下行来在我自己的项目中使用我的适配器...
defmodule UsingTestAdapter.Repo do
use Ecto.Repo,
otp_app: :using_test_adapter,
adapter: TestAdapter # <------ this line
end
目前它具有与postgres适配器相同的功能。我一直在尝试编辑Ecto.Adapters.Postgres.Connection
中的一些功能,我已经意识到它们的效果并不像我预期的那样。
例如,insert
函数实际上并没有使用传递给Repo.insert
的参数。
为了使这一点更加清晰,想象一下我们有下表,Comments
......
| id | comment |
| -- | ------- |
现在Repo.insert(%Comments{comment: "hi"})
被召唤。
我想修改适配器,以便它忽略传入的“hi”值,而是插入“我是适配器的注释,我控制这个数据库。哈哈哈(邪恶的笑)”......
| id | comment |
| -- | ------------------------------------------------------------------ |
| 1 | I am the adapter and I control this database. Hahaha (evil laugh)" |
但是,insert
函数似乎并不实际将数据存储为参数。
我最初想到ecto适配器发生的事情是,当用户调用其中一个repo函数时,它调用了Ecto.Adapters.Postgres.Connection
模块中的相应函数。这似乎确实发生了,但其他步骤似乎在此之前发生。
如果有人对Repo.insert
(和任何其他Repo函数)被调用时调用的函数链有更好的理解,请在下面解释。
我有时间更深入地研究这个问题,觉得我现在有了更好的理解。
我将按顺序列出当用户在elixir应用程序中调用Repo.insert
时发生的步骤。
AppName.Repo.insert(%AppName.Comments{comment: "hi"})
defmodule AppName.Repo do
use Ecto.Repo, otp_app: :app_name, adapter: adapter_name
end
(这是凤凰应用程序的默认设置)
use Ecto.Repo
允许在模块中定义的所有函数在调用它的模块中使用。这意味着当我们调用AppName.Repo.insert
时,它会转到我们的模块,看到没有定义为insert的函数,看到use
marco,检查该模块,看到一个名为insert
的函数并调用该函数(这不完全是它的工作原理但是我觉得它解释得很好)。
def insert(struct, opts \\ []) do
Ecto.Repo.Schema.insert(__MODULE__, struct, opts)
end
# if a changeset was passed in
def insert(name, %Changeset{} = changeset, opts) when is_list(opts) do
do_insert(name, changeset, opts)
end
# if a struct was passed in
# This will be called in this example
def insert(name, %{__struct__: _} = struct, opts) when is_list(opts) do
do_insert(name, Ecto.Changeset.change(struct), opts)
end
此步骤确保以变更集的形式传递给do_insert
的数据。
do_insert(name, Ecto.Changeset.change(struct), opts)
因为它很长,所以不会粘贴整个功能。 Where function is defined
此函数执行大量数据操作并检查错误。如果一切顺利,最终会调用apply
函数
defp apply(changeset, adapter, action, args) do
case apply(adapter, action, args) do # <---- Kernel.apply/3
{:ok, values} ->
{:ok, values}
{:invalid, _} = constraints ->
constraints
{:error, :stale} ->
opts = List.last(args)
case Keyword.fetch(opts, :stale_error_field) do
{:ok, stale_error_field} when is_atom(stale_error_field) ->
stale_message = Keyword.get(opts, :stale_error_message, "is stale")
changeset = Changeset.add_error(changeset, stale_error_field, stale_message, [stale: true])
{:error, changeset}
_other ->
raise Ecto.StaleEntryError, struct: changeset.data, action: action
end
end
end
这个apply/4
函数用Kernel.apply/3
,module
和function name
调用arguments
函数。在我们的例子中,模块是AdapterName
,函数是:insert
。
这是我们的适配器发挥作用的地方:D(最后)。
上面的apply/3
函数调用将我们带到了我们创建的适配器。
defmodule AdapterName do
# Inherit all behaviour from Ecto.Adapters.SQL
use Ecto.Adapters.SQL, driver: :postgrex, migration_lock: "FOR UPDATE"
end
这个模块中没有定义插入函数,但是因为它是'使用'Ecto.Adapters.SQL
,让我们接下来看看这个模块。
defmodule Ecto.Adapters.SQL do
...
@conn __MODULE__.Connection
...
@impl true
def insert(adapter_meta, %{source: source, prefix: prefix}, params,
{kind, conflict_params, _} = on_conflict, returning, opts) do
{fields, values} = :lists.unzip(params)
sql = @conn.insert(prefix, source, fields, [fields], on_conflict, returning)
Ecto.Adapters.SQL.struct(adapter_meta, @conn, sql, :insert, source, [], values ++ conflict_params, kind, returning, opts)
end
...
end
@conn
被定义为module attribute,只是current calling module(MODULE)+。连接。
如第5点所述,调用模块是AdapterName
这意味着在insert
函数中,以下行...
@conn.insert(prefix, source, fields, [fields], on_conflict, returning)
是相同的
AdapterName.Connection.insert(prefix, source, fields, [fields], on_conflict, returning)
由于我们的adapter
与postgres adapter
一样,它将我们带到下一个功能。
def insert(prefix, table, header, rows, on_conflict, returning) do
values =
if header == [] do
[" VALUES " | intersperse_map(rows, ?,, fn _ -> "(DEFAULT)" end)]
else
[?\s, ?(, intersperse_map(header, ?,, "e_name/1), ") VALUES " | insert_all(rows, 1)]
end
["INSERT INTO ", quote_table(prefix, table), insert_as(on_conflict),
values, on_conflict(on_conflict, header) | returning(returning)]
end
为了在已经太长的答案中保存一些文字,我不会详细介绍。这个函数实际上并没有采用我们传入Repo.insert
的params(回到第一集)。
如果我们想编辑params,我们需要在AdapterName
模块中这样做。我们需要定义自己的insert
函数,以便它不再调用步骤6中定义的insert
函数。
为简单起见,我们将把步骤6中定义的insert
复制到AdapterName模块中。然后我们可以修改该函数来更新params,因为我们认为合适。
如果我们这样做,我们最终得到的功能就像......
def insert(adapter_meta, %{source: source, prefix: prefix}, params, on_conflict, returning, opts) do
Keyword.replace!(params, :comment, "I am the adapter and I control this database. Hahaha (evil laugh)") # <---- changing the comment like we wanted :D
{kind, conflict_params, _} = on_conflict
{fields, values} = :lists.unzip(params)
sql = @conn.insert(prefix, source, fields, [fields], on_conflict, returning)
Ecto.Adapters.SQL.struct(adapter_meta, @conn, sql, :insert, source, [], values ++ conflict_params, kind, returning, opts)
end
现在,这会插入我们原先想要的不同值。
希望有人觉得这很有帮助。