/
dbi-connection.R
278 lines (252 loc) · 6.78 KB
/
dbi-connection.R
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#' @rdname OdbcConnection
#' @keywords internal
#' @export
setClass(
"OdbcConnection",
contains = "DBIConnection",
slots = list(
ptr = "externalptr",
quote = "character",
info = "ANY",
encoding = "character"
)
)
# TODO: show encoding, timezone, bigint mapping
#' @rdname OdbcConnection
#' @inheritParams methods::show
#' @export
setMethod("show", "OdbcConnection",
function(object) {
info <- dbGetInfo(object)
cat(
sep = "", "<OdbcConnection>",
if (nzchar(info[["servername"]])) {
paste0(
" ",
if (nzchar(info[["username"]])) paste0(info[["username"]], "@"),
info[["servername"]], "\n"
)
},
if (!dbIsValid(object)) {
" DISCONNECTED\n"
} else {
paste0(
collapse = "",
if (nzchar(info[["dbname"]])) {
paste0(" Database: ", info[["dbname"]], "\n")
},
if (nzchar(info[["dbms.name"]]) && nzchar(info[["db.version"]])) {
paste0(" ", info[["dbms.name"]], " ", "Version: ", info[["db.version"]], "\n")
},
NULL
)
}
)
}
)
#' @rdname OdbcConnection
#' @inheritParams DBI::dbIsValid
#' @export
setMethod("dbIsValid", "OdbcConnection",
function(dbObj, ...) {
connection_valid(dbObj@ptr)
}
)
#' @rdname OdbcConnection
#' @inheritParams DBI::dbDisconnect
#' @export
setMethod("dbDisconnect", "OdbcConnection",
function(conn, ...) {
if (!dbIsValid(conn)) {
warning("Connection already closed.", call. = FALSE)
}
on_connection_closed(conn)
connection_release(conn@ptr)
invisible(TRUE)
}
)
#' @rdname OdbcConnection
#' @inheritParams DBI::dbSendQuery
#' @param params Optional query parameters, passed on to [dbBind()]
#' @param immediate If `TRUE`, SQLExecDirect will be used instead of
#' SQLPrepare, and the `params` argument is ignored
#' @export
setMethod("dbSendQuery", c("OdbcConnection", "character"),
function(conn, statement, params = NULL, ..., immediate = FALSE) {
OdbcResult(
connection = conn,
statement = statement,
params = params,
immediate = immediate
)
}
)
#' @rdname OdbcConnection
#' @inheritParams DBI::dbExecute
#' @export
setMethod("dbExecute", c("OdbcConnection", "character"),
function(conn, statement, params = NULL, ..., immediate = is.null(params)) {
rs <- dbSendStatement(conn, statement, params = params, ..., immediate = immediate)
on.exit(dbClearResult(rs))
dbGetRowsAffected(rs)
}
)
#' @rdname OdbcConnection
#' @inheritParams DBI::dbSendStatement
#' @param params Query parameters to pass to [dbBind()].
#' See [dbBind()] for details.
#' @export
setMethod("dbSendStatement", c("OdbcConnection", "character"),
function(conn, statement, params = NULL, ..., immediate = FALSE) {
OdbcResult(
connection = conn,
statement = statement,
params = params,
immediate = immediate
)
}
)
#' @rdname OdbcConnection
#' @inheritParams DBI::dbDataType
#' @export
setMethod("dbDataType", "OdbcConnection",
function(dbObj, obj, ...) {
odbcDataType(dbObj, obj)
}
)
#' @rdname OdbcConnection
#' @inheritParams DBI::dbDataType
#' @export
setMethod("dbDataType", c("OdbcConnection", "data.frame"),
function(dbObj, obj, ...) {
odbc_data_type_df(dbObj, obj)
}
)
#' @rawNamespace exportMethods(dbQuoteString)
NULL
#' @rdname OdbcConnection
#' @inheritParams DBI::dbQuoteIdentifier
#' @export
setMethod("dbQuoteIdentifier", c("OdbcConnection", "character"),
function(conn, x, ...) {
if (length(x) == 0L) {
return(DBI::SQL(character()))
}
if (any(is.na(x))) {
cli::cli_abort("{.arg x} can't be {.code NA}.")
}
if (nzchar(conn@quote)) {
x <- gsub(conn@quote, paste0(conn@quote, conn@quote), x, fixed = TRUE)
}
nms <- names(x)
res <- DBI::SQL(paste(conn@quote, x, conn@quote, sep = ""))
names(res) <- nms
res
}
)
#' @rdname OdbcConnection
#' @inheritParams DBI::dbQuoteIdentifier
#' @export
setMethod("dbQuoteIdentifier", c("OdbcConnection", "SQL"),
getMethod("dbQuoteIdentifier", c("DBIConnection", "SQL"), asNamespace("DBI"))
)
#' List remote tables and fields for an ODBC connection
#'
#' `dbListTables()` provides names of remote tables accessible through this
#' connection; `dbListFields()` provides names of columns within a table.
#'
#' @inheritParams DBI::dbListTables
#' @param catalog_name,schema_name,table_name Catalog, schema, and table names.
#'
#' By default, `catalog_name`, `schema_name` and `table_name` will
#' automatically escape underscores to ensure that you match exactly one
#' table. If you want to search for multiple tables using wild cards, you
#' will need to use `odbcConnectionTables()` directly instead.
#'
#' @param table_type The type of the table to return, the default returns
#' all table types.
#' @returns A character vector of table or field names respectively.
#' @export
setMethod("dbListTables", "OdbcConnection",
function(conn,
catalog_name = NULL,
schema_name = NULL,
table_name = NULL,
table_type = NULL,
...) {
check_string(catalog_name, allow_null = TRUE)
check_string(schema_name, allow_null = TRUE)
check_string(table_name, allow_null = TRUE)
check_string(table_type, allow_null = TRUE)
tables <- odbcConnectionTables(
conn,
name = table_name,
catalog_name = catalog_name,
schema_name = schema_name,
table_type = table_type,
exact = TRUE
)
tables[["table_name"]]
}
)
#' @rdname OdbcConnection
#' @inheritParams DBI::dbGetInfo
#' @export
setMethod("dbGetInfo", "OdbcConnection",
function(dbObj, ...) {
connection_info(dbObj@ptr)
}
)
#' @rdname OdbcConnection
#' @inheritParams DBI::dbGetQuery
#' @inheritParams DBI::dbFetch
#' @export
setMethod("dbGetQuery", c("OdbcConnection", "character"),
function(conn,
statement,
n = -1,
params = NULL,
immediate = is.null(params),
...) {
rs <- dbSendQuery(
conn,
statement,
params = params,
immediate = immediate,
...
)
on.exit(dbClearResult(rs))
df <- dbFetch(rs, n = n, ...)
if (!dbHasCompleted(rs)) {
warning("Pending rows", call. = FALSE)
}
df
}
)
#' @rdname OdbcConnection
#' @inheritParams DBI::dbBegin
#' @export
setMethod("dbBegin", "OdbcConnection",
function(conn, ...) {
connection_begin(conn@ptr)
invisible(TRUE)
}
)
#' @rdname OdbcConnection
#' @inheritParams DBI::dbCommit
#' @export
setMethod("dbCommit", "OdbcConnection",
function(conn, ...) {
connection_commit(conn@ptr)
invisible(TRUE)
}
)
#' @rdname OdbcConnection
#' @inheritParams DBI::dbRollback
#' @export
setMethod("dbRollback", "OdbcConnection",
function(conn, ...) {
connection_rollback(conn@ptr)
invisible(TRUE)
}
)