connection.ex (4222B)
1 defmodule Ecto.Adapters.SQL.Connection do 2 @moduledoc """ 3 Specifies the behaviour to be implemented by all SQL connections. 4 """ 5 6 @typedoc "The query name" 7 @type name :: String.t 8 9 @typedoc "The SQL statement" 10 @type statement :: String.t 11 12 @typedoc "The cached query which is a DBConnection Query" 13 @type cached :: map 14 15 @type connection :: DBConnection.conn() 16 @type params :: [term] 17 18 @doc """ 19 Receives options and returns `DBConnection` supervisor child 20 specification. 21 """ 22 @callback child_spec(options :: Keyword.t) :: {module, Keyword.t} 23 24 @doc """ 25 Prepares and executes the given query with `DBConnection`. 26 """ 27 @callback prepare_execute(connection, name, statement, params, options :: Keyword.t) :: 28 {:ok, cached, term} | {:error, Exception.t} 29 30 @doc """ 31 Executes a cached query. 32 """ 33 @callback execute(connection, cached, params, options :: Keyword.t) :: 34 {:ok, cached, term} | {:ok, term} | {:error | :reset, Exception.t} 35 36 @doc """ 37 Runs the given statement as a query. 38 """ 39 @callback query(connection, statement, params, options :: Keyword.t) :: 40 {:ok, term} | {:error, Exception.t} 41 42 @doc """ 43 Runs the given statement as a multi-result query. 44 """ 45 @callback query_many(connection, statement, params, options :: Keyword.t) :: 46 {:ok, term} | {:error, Exception.t} 47 48 @doc """ 49 Returns a stream that prepares and executes the given query with 50 `DBConnection`. 51 """ 52 @callback stream(connection, statement, params, options :: Keyword.t) :: 53 Enum.t 54 55 @doc """ 56 Receives the exception returned by `c:query/4`. 57 58 The constraints are in the keyword list and must return the 59 constraint type, like `:unique`, and the constraint name as 60 a string, for example: 61 62 [unique: "posts_title_index"] 63 64 Must return an empty list if the error does not come 65 from any constraint. 66 """ 67 @callback to_constraints(exception :: Exception.t, options :: Keyword.t) :: Keyword.t 68 69 ## Queries 70 71 @doc """ 72 Receives a query and must return a SELECT query. 73 """ 74 @callback all(query :: Ecto.Query.t) :: iodata 75 76 @doc """ 77 Receives a query and values to update and must return an UPDATE query. 78 """ 79 @callback update_all(query :: Ecto.Query.t) :: iodata 80 81 @doc """ 82 Receives a query and must return a DELETE query. 83 """ 84 @callback delete_all(query :: Ecto.Query.t) :: iodata 85 86 @doc """ 87 Returns an INSERT for the given `rows` in `table` returning 88 the given `returning`. 89 """ 90 @callback insert(prefix ::String.t, table :: String.t, 91 header :: [atom], rows :: [[atom | nil]], 92 on_conflict :: Ecto.Adapter.Schema.on_conflict, returning :: [atom], 93 placeholders :: [term]) :: iodata 94 95 @doc """ 96 Returns an UPDATE for the given `fields` in `table` filtered by 97 `filters` returning the given `returning`. 98 """ 99 @callback update(prefix :: String.t, table :: String.t, fields :: [atom], 100 filters :: [atom], returning :: [atom]) :: iodata 101 102 @doc """ 103 Returns a DELETE for the `filters` returning the given `returning`. 104 """ 105 @callback delete(prefix :: String.t, table :: String.t, 106 filters :: [atom], returning :: [atom]) :: iodata 107 108 @doc """ 109 Executes an EXPLAIN query or similar depending on the adapter to obtains statistics of the given query. 110 111 Receives the `connection`, `query`, `params` for the query, 112 and all `opts` including those related to the EXPLAIN statement and shared opts. 113 114 Must execute the explain query and return the result. 115 """ 116 @callback explain_query(connection, query :: String.t, params :: Keyword.t, opts :: Keyword.t) :: 117 {:ok, term} | {:error, Exception.t} 118 119 ## DDL 120 121 @doc """ 122 Receives a DDL command and returns a query that executes it. 123 """ 124 @callback execute_ddl(command :: Ecto.Adapter.Migration.command) :: String.t | [iodata] 125 126 @doc """ 127 Receives a query result and returns a list of logs. 128 """ 129 @callback ddl_logs(result :: term) :: [{Logger.level, Logger.message, Logger.metadata}] 130 131 @doc """ 132 Returns a queryable to check if the given `table` exists. 133 """ 134 @callback table_exists_query(table :: String.t) :: {iodata, [term]} 135 end