/
connections.jl
141 lines (111 loc) · 4.4 KB
/
connections.jl
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
136
137
138
139
140
# DBInterface bridge.
"""
SQLConnection(conn; catalog)
Wrap a raw database connection object together with a [`SQLCatalog`](@ref)
object containing information about database tables.
"""
struct SQLConnection{RawConnType} <: DBInterface.Connection
raw::RawConnType
catalog::SQLCatalog
SQLConnection{RawConnType}(raw::RawConnType; catalog) where {RawConnType} =
new{RawConnType}(raw, catalog)
end
SQLConnection(raw::RawConnType; catalog) where {RawConnType} =
SQLConnection{RawConnType}(raw, catalog = catalog)
function Base.show(io::IO, conn::SQLConnection)
print(io, "SQLConnection(")
show(io, conn.raw)
print(io, ", catalog = ")
show(io, conn.catalog)
print(io, ')')
end
"""
SQLStatement(conn, raw; vars = Symbol[])
Wrap a prepared SQL statement.
"""
struct SQLStatement{RawConnType, RawStmtType} <: DBInterface.Statement
conn::SQLConnection{RawConnType}
raw::RawStmtType
vars::Vector{Symbol}
SQLStatement{RawConnType, RawStmtType}(conn::SQLConnection{RawConnType}, raw::RawStmtType; vars = Symbol[]) where {RawConnType, RawStmtType} =
new(conn, raw, vars)
end
SQLStatement(conn::SQLConnection{RawConnType}, raw::RawStmtType; vars = Symbol[]) where {RawConnType, RawStmtType} =
SQLStatement{RawConnType, RawStmtType}(conn, raw, vars = vars)
function Base.show(io::IO, stmt::SQLStatement)
print(io, "SQLStatement(")
show(io, stmt.conn)
print(io, ", ")
show(io, stmt.raw)
if !isempty(stmt.vars)
print(io, ", vars = ")
show(io, stmt.vars)
end
print(io, ')')
end
"""
Shorthand for [`SQLConnection`](@ref).
"""
const DB = SQLConnection
"""
DBInterface.connect(DB{RawConnType},
args...;
schema = nothing,
dialect = nothing,
cache = $default_cache_maxsize,
kws...)
Connect to the database server, call [`reflect`](@ref) to retrieve the
information about available tables and return a [`SQLConnection`](@ref) object.
Extra parameters `args` and `kws` are passed to the call:
DBInterface.connect(RawConnType, args...; kws...)
"""
function DBInterface.connect(::Type{SQLConnection{RawConnType}}, args...;
schema = nothing,
dialect = nothing,
cache = default_cache_maxsize,
kws...) where {RawConnType}
raw = DBInterface.connect(RawConnType, args...; kws...)
catalog = reflect(raw, schema = schema, dialect = dialect, cache = cache)
SQLConnection{RawConnType}(raw, catalog = catalog)
end
"""
DBInterface.prepare(conn::SQLConnection, sql::SQLNode)::SQLStatement
DBInterface.prepare(conn::SQLConnection, sql::SQLClause)::SQLStatement
Serialize the query node and return a prepared SQL statement.
"""
DBInterface.prepare(conn::SQLConnection, sql::Union{AbstractSQLNode, AbstractSQLClause}) =
DBInterface.prepare(conn, render(conn, sql))
"""
DBInterface.prepare(conn::SQLConnection, str::SQLString)::SQLStatement
Generate a prepared SQL statement.
"""
DBInterface.prepare(conn::SQLConnection, str::SQLString) =
SQLStatement(conn, DBInterface.prepare(conn.raw, str.raw), vars = str.vars)
DBInterface.prepare(conn::SQLConnection, str::AbstractString) =
DBInterface.prepare(conn.raw, str)
"""
DBInterface.execute(conn::SQLConnection, sql::SQLNode; params...)
DBInterface.execute(conn::SQLConnection, sql::SQLClause; params...)
Serialize and execute the query node.
"""
DBInterface.execute(conn::SQLConnection, sql::Union{AbstractSQLNode, AbstractSQLClause}; params...) =
DBInterface.execute(conn, sql, values(params))
"""
DBInterface.execute(conn::SQLConnection, sql::SQLNode, params)
DBInterface.execute(conn::SQLConnection, sql::SQLClause, params)
Serialize and execute the query node.
"""
DBInterface.execute(conn::SQLConnection, sql::Union{AbstractSQLNode, AbstractSQLClause}, params) =
DBInterface.execute(DBInterface.prepare(conn, sql), params)
DBInterface.close!(conn::SQLConnection) =
DBInterface.close!(conn.raw)
"""
DBInterface.execute(stmt::SQLStatement, params)
Execute the prepared SQL statement.
"""
DBInterface.execute(stmt::SQLStatement, params) =
DBInterface.execute(stmt.raw, pack(stmt.vars, params))
DBInterface.getconnection(stmt::SQLStatement) =
stmt.conn
DBInterface.close!(stmt::SQLStatement) =
DBInterface.close!(stmt.raw)