Skip to content

Commit

Permalink
Allow cache statement to be per operation instead
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Apr 17, 2020
1 parent 62967b2 commit d8d468a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
4 changes: 4 additions & 0 deletions lib/ecto/adapters/myxql.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ defmodule Ecto.Adapters.MyXQL do
This option is only used for `mix ecto.load` and `mix ecto.dump`,
via the `mysql` command. For more information, please check
[MySQL docs](https://dev.mysql.com/doc/en/connecting.html)
* `:cache_statement` - how Ecto should cache INSERT/UPDATE/DELETE statements.
It can either be `:per_schema` (one cache key is used for each schema) or
`:per_operation` (one cache key is use for each operation). Note SELECTs
use a more complete cache mechanism that considers the query itself.
* `:socket_options` - Specifies socket configuration
* `:show_sensitive_data_on_connection_error` - show connection data and
configuration whenever there is an error attempting to connect to the
Expand Down
4 changes: 4 additions & 0 deletions lib/ecto/adapters/postgres.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ defmodule Ecto.Adapters.Postgres do
* `:ssl` - Set to true if ssl should be used (default: false)
* `:ssl_opts` - A list of ssl options, see Erlang's `ssl` docs
* `:parameters` - Keyword list of connection parameters
* `:cache_statement` - how Ecto should cache INSERT/UPDATE/DELETE statements.
It can either be `:per_schema` (one cache key is used for each schema) or
`:per_operation` (one cache key is use for each operation). Note SELECTs
use a more complete cache mechanism that considers the query itself.
* `:connect_timeout` - The timeout for establishing new connections (default: 5000)
* `:prepare` - How to prepare queries, either `:named` to use named queries
or `:unnamed` to force unnamed queries (default: `:named`)
Expand Down
14 changes: 10 additions & 4 deletions lib/ecto/adapters/sql.ex
Original file line number Diff line number Diff line change
Expand Up @@ -453,9 +453,10 @@ defmodule Ecto.Adapters.SQL do
telemetry_prefix = Keyword.fetch!(config, :telemetry_prefix)
telemetry = {config[:repo], log, telemetry_prefix ++ [:query]}

cache_statement = Keyword.get(config, :cache_statement, :per_schema)
config = adapter_config(config)
opts = Keyword.take(config, @pool_opts)
meta = %{telemetry: telemetry, sql: connection, opts: opts}
meta = %{telemetry: telemetry, sql: connection, opts: opts, cache_statement: cache_statement}
{:ok, connection.child_spec(config), meta}
end

Expand Down Expand Up @@ -489,15 +490,20 @@ defmodule Ecto.Adapters.SQL do

## Query

defp cache_statement(%{cache_statement: :per_schema}, operation, source),
do: "ecto_#{operation}_#{source}"

defp cache_statement(%{cache_statement: :per_operation}, operation, _source),
do: "ecto_#{operation}"

@doc false
def insert_all(adapter_meta, schema_meta, conn, header, rows, on_conflict, returning, opts) do
%{source: source, prefix: prefix} = schema_meta
{_, conflict_params, _} = on_conflict
{rows, params} = unzip_inserts(header, rows)
sql = conn.insert(prefix, source, header, rows, on_conflict, returning)

cache_statement = "ecto_insert_all_#{source}"
opts = [{:cache_statement, cache_statement} | opts]
opts = [{:cache_statement, cache_statement(adapter_meta, "insert_all", source)} | opts]

%{num_rows: num, rows: rows} =
query!(adapter_meta, sql, Enum.reverse(params) ++ conflict_params, opts)
Expand Down Expand Up @@ -626,7 +632,7 @@ defmodule Ecto.Adapters.SQL do

@doc false
def struct(adapter_meta, conn, sql, operation, source, params, values, on_conflict, returning, opts) do
cache_statement = "ecto_#{operation}_#{source}"
cache_statement = cache_statement(adapter_meta, operation, source)

case query(adapter_meta, sql, values, [cache_statement: cache_statement] ++ opts) do
{:ok, %{rows: nil, num_rows: 1}} ->
Expand Down
7 changes: 7 additions & 0 deletions lib/ecto/adapters/tds.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ defmodule Ecto.Adapters.Tds do
* `:pool` - The connection pool module, defaults to `DBConnection.ConnectionPool`
* `:ssl` - Set to true if ssl should be used (default: false)
* `:ssl_opts` - A list of ssl options, see Erlang's `ssl` docs
* `:cache_statement` - how Ecto should cache INSERT/UPDATE/DELETE statements.
It can either be `:per_schema` (one cache key is used for each schema) or
`:per_operation` (one cache key is use for each operation). Note SELECTs
use a more complete cache mechanism that considers the query itself.
* `:show_sensitive_data_on_connection_error` - show connection data and
configuration whenever there is an error attempting to connect to the
database
We also recommend developers to consult the `Tds.start_link/1` documentation
for a complete list of all supported options for driver.
Expand Down

0 comments on commit d8d468a

Please sign in to comment.