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