forked from r-dbi/odbc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver-sql-server.R
264 lines (240 loc) · 6.9 KB
/
driver-sql-server.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
# Microsoft SQL Server ---------------------------------------------------------
#' SQL Server
#'
#' Details of SQL Server methods for odbc and DBI generics.
#'
#' @rdname SQLServer
#' @export
setClass("Microsoft SQL Server", contains = "OdbcConnection")
#' @description
#' ## `dbUnquoteIdentifier()`
#'
#' `conn@quote` returns the quotation mark, but quotation marks and square
#' brackets can be used interchangeably for delimited identifiers.
#' (<https://learn.microsoft.com/en-us/sql/relational-databases/databases/database-identifiers>).
#' This function strips the brackets first and then calls the DBI method to
#' strip the quotation marks.
#' @rdname SQLServer
#' @docType methods
#' @usage NULL
#' @keywords internal
setMethod("dbUnquoteIdentifier", c("Microsoft SQL Server", "SQL"),
function(conn, x, ...) {
x <- gsub("(\\[)([^\\.]+?)(\\])", "\\2", x)
callNextMethod(conn, x, ...)
}
)
#' @description
#' ## `isTempTable()`
#'
#' Local temp tables are stored as
#' `[tempdb].[dbo].[#name]_____[numeric identifier]`, so `isTempTable()`
#' returns `TRUE` if `catalog_name` is `"tempdb"` or `"%"`, or the
#' name starts with `"#"`.
#' @rdname SQLServer
#' @usage NULL
setMethod("isTempTable", c("Microsoft SQL Server", "character"),
function(conn, name, catalog_name = NULL, schema_name = NULL, ...) {
if (!is.null(catalog_name) &&
catalog_name != "%" &&
length(catalog_name) > 0 &&
catalog_name != "tempdb") {
return(FALSE)
}
grepl("^[#][^#]", name)
}
)
#' @rdname SQLServer
#' @usage NULL
setMethod("isTempTable", c("Microsoft SQL Server", "SQL"),
function(conn, name, ...) {
isTempTable(conn, dbUnquoteIdentifier(conn, name)[[1]], ...)
}
)
#' @description
#' ## `dbExistsTable()`
#' The default implementation reports temporary tables as non-existent
#' since they live in a different catalog. This method provides a special
#' case for temporary tables, as identified by `isTempTable()`.
#' @rdname SQLServer
#' @docType methods
#' @usage NULL
setMethod("dbExistsTable", c("Microsoft SQL Server", "character"),
function(conn, name, ...) {
check_string(name)
if (isTempTable(conn, name, ...)) {
query <- paste0("SELECT OBJECT_ID('tempdb..", name, "')")
!is.na(dbGetQuery(conn, query)[[1]])
} else {
df <- odbcConnectionTables(conn, name = name, ...)
NROW(df) > 0
}
}
)
#' @description
#' ## `dbListTables()`
#' The default implementation reports temporary tables as non-existent
#' when a `catalog_name` isn't supplied since they live in a different catalog.
#' This method provides a special case for temporary tables.
#' @rdname SQLServer
#' @usage NULL
setMethod("dbListTables", "Microsoft SQL Server",
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)
res <- callNextMethod()
if (is.null(catalog_name) && is.null(schema_name)) {
res_temp <- callNextMethod(
conn = conn,
catalog_name = "tempdb",
schema_name = "dbo"
)
res <- c(res, res_temp)
}
res
}
)
#' @rdname SQLServer
#' @usage NULL
setMethod("dbExistsTable", c("Microsoft SQL Server", "Id"),
function(conn, name, ...) {
dbExistsTable(
conn,
name = id_field(name, "table"),
catalog_name = id_field(name, "catalog"),
schema_name = id_field(name, "schema")
)
}
)
#' @rdname SQLServer
#' @usage NULL
setMethod("dbExistsTable", c("Microsoft SQL Server", "SQL"),
function(conn, name, ...) {
dbExistsTable(conn, dbUnquoteIdentifier(conn, name)[[1]], ...)
}
)
#' @description
#' ## `odbcConnectionSchemas`
#'
#' Method for an internal function. Calls catalog-specific `sp_tables` to make
#' sure we list the schemas in the appropriate database/catalog.
#' @rdname SQLServer
#' @usage NULL
setMethod(
"odbcConnectionSchemas", "Microsoft SQL Server",
function(conn, catalog_name = NULL) {
if (is.null(catalog_name) || !nchar(catalog_name)) {
return(callNextMethod())
}
sproc <- paste(
dbQuoteIdentifier(conn, catalog_name), "dbo.sp_tables", sep = ".")
res <- dbGetQuery(conn, paste0(
"EXEC ", sproc, " ",
"@table_name = '', ",
"@table_owner = '%', ",
"@table_qualifier = ''"
))
res$TABLE_OWNER
}
)
#' @rdname SQLServer
#' @description
#' ## `sqlCreateTable()`
#'
#' Warns if `temporary = TRUE` but the `name` does not conform to temp table
#' naming conventions (i.e. it doesn't start with `#`).
#' @usage NULL
setMethod("sqlCreateTable", "Microsoft SQL Server",
function(con,
table,
fields,
row.names = NA,
temporary = FALSE,
...,
field.types = NULL) {
check_bool(temporary)
check_row.names(row.names)
check_field.types(field.types)
if (temporary && !isTempTable(con, table)) {
cli::cli_warn(
"{.arg temporary} is {.code TRUE}, but table name doesn't use # prefix."
)
}
temporary <- FALSE
callNextMethod()
}
)
#' @export
#' @rdname odbcDataType
#' @usage NULL
setMethod("odbcDataType", "Microsoft SQL Server",
function(con, obj, ...) {
switch_type(
obj,
factor = varchar(obj),
datetime = "DATETIME",
date = "DATE",
time = "TIME",
binary = varbinary(obj),
integer = "INT",
int64 = "BIGINT",
double = "FLOAT",
character = varchar(obj),
logical = "BIT",
list = varchar(obj),
stop("Unsupported type", call. = FALSE)
)
}
)
#' @description
#' ## `odbcConnectionColumns_()`
#'
#' If temp table, query the database for the
#' actual table name.
#' @rdname SQLServer
#' @usage NULL
setMethod("odbcConnectionColumns_", c("Microsoft SQL Server", "character"),
function(conn,
name,
...,
catalog_name = NULL,
schema_name = NULL,
column_name = NULL,
exact = FALSE) {
if (exact &&
isTempTable(conn, name, catalog_name, schema_name, column_name, exact)) {
catalog_name <- "tempdb"
schema_name <- "dbo"
query <- paste0("SELECT name FROM tempdb.sys.tables WHERE ",
"object_id = OBJECT_ID('tempdb..", name, "')")
name <- dbGetQuery(conn, query)[[1]]
}
callNextMethod(
conn = conn,
name = name,
...,
catalog_name = catalog_name,
schema_name = schema_name,
column_name = column_name,
exact = exact
)
}
)
#' @description
#' ## `odbcConnectionColumns_()`
#'
#' Copied over from odbc-connection to avoid S4 dispatch NOTEs.
#' @rdname SQLServer
#' @usage NULL
setMethod("odbcConnectionColumns_", c("Microsoft SQL Server", "SQL"),
function(conn, name, ...) {
odbcConnectionColumns_(conn, dbUnquoteIdentifier(conn, name)[[1]], ...)
}
)