|
| 1 | + |
| 2 | +#' Helper method used to determine if a table identifier is that |
| 3 | +#' of a temporary table. |
| 4 | +#' |
| 5 | +#' Currently implemented only for select back-ends where |
| 6 | +#' we have a use for it (SQL Server, for example). Generic, in case |
| 7 | +#' we develop a broader use case. |
| 8 | +#' @param conn OdbcConnection |
| 9 | +#' @param name Table name |
| 10 | +#' @param ... additional parameters to methods |
| 11 | +#' @rdname isTempTable |
| 12 | +#' @export |
| 13 | +setGeneric( |
| 14 | + "isTempTable", |
| 15 | + valueClass = "logical", |
| 16 | + function(conn, name, ...) { |
| 17 | + standardGeneric("isTempTable") |
| 18 | + } |
| 19 | +) |
| 20 | + |
| 21 | +#' @rdname isTempTable |
| 22 | +setMethod( |
| 23 | + "isTempTable", |
| 24 | + c("OdbcConnection", "Id"), |
| 25 | + function(conn, name, ...) { |
| 26 | + isTempTable(conn, |
| 27 | + name = id_field(name, "table"), |
| 28 | + catalog_name = id_field(name, "catalog"), |
| 29 | + schema_name = id_field(name, "schema"), |
| 30 | + ...) |
| 31 | + } |
| 32 | +) |
| 33 | + |
| 34 | +#' @rdname isTempTable |
| 35 | +setMethod( |
| 36 | + "isTempTable", |
| 37 | + c("OdbcConnection", "SQL"), |
| 38 | + function(conn, name, ...) { |
| 39 | + isTempTable(conn, dbUnquoteIdentifier(conn, name)[[1]], ...) |
| 40 | + } |
| 41 | +) |
| 42 | + |
1 | 43 | # Oracle --------------------------------------------------------------------
|
2 | 44 |
|
3 | 45 | # Simple class prototype to avoid messages about unknown classes from setMethod
|
@@ -203,19 +245,89 @@ setMethod("sqlCreateTable", "DB2/AIX64",
|
203 | 245 |
|
204 | 246 | # Microsoft SQL Server ---------------------------------------------------------
|
205 | 247 |
|
206 |
| -# Simple class prototype to avoid messages about unknown classes from setMethod |
| 248 | +#' Simple class prototype to avoid messages about unknown classes from setMethod |
| 249 | +#' @rdname SQLServer |
| 250 | +#' @usage NULL |
207 | 251 | setClass("Microsoft SQL Server", where = class_cache)
|
208 | 252 |
|
209 |
| -# For SQL Server, conn@quote will return the quotation mark, however |
210 |
| -# both quotation marks as well as square bracket are used interchangeably for |
211 |
| -# delimited identifiers. See: |
212 |
| -# https://learn.microsoft.com/en-us/sql/relational-databases/databases/database-identifiers?view=sql-server-ver16 |
213 |
| -# Therefore strip the brackets first, and then call the DBI method that strips |
214 |
| -# the quotation marks. |
215 |
| -# TODO: the generic implementation in DBI should take a quote char as |
216 |
| -# parameter. |
| 253 | +#' SQL Server specific implementation. |
| 254 | +#' |
| 255 | +#' For SQL Server, `conn@quote` will return the quotation mark, however |
| 256 | +#' both quotation marks as well as square bracket are used interchangeably for |
| 257 | +#' delimited identifiers. See: |
| 258 | +#' \url{https://learn.microsoft.com/en-us/sql/relational-databases/databases/database-identifiers?view=sql-server-ver16} |
| 259 | +#' Therefore strip the brackets first, and then call the DBI method that strips |
| 260 | +#' the quotation marks. |
| 261 | +#' TODO: the generic implementation in DBI should take a quote char as |
| 262 | +#' parameter. |
| 263 | +#' |
| 264 | +#' @rdname SQLServer |
| 265 | +#' @docType methods |
| 266 | +#' @inheritParams DBI::dbUnquoteIdentifier |
| 267 | +#' @usage NULL |
217 | 268 | setMethod("dbUnquoteIdentifier", c("Microsoft SQL Server", "SQL"),
|
218 | 269 | function(conn, x, ...) {
|
219 | 270 | x <- gsub("(\\[)([^\\.]+?)(\\])", "\\2", x)
|
220 | 271 | callNextMethod( conn, x, ... )
|
221 | 272 | })
|
| 273 | + |
| 274 | +#' SQL Server specific implementation. |
| 275 | +#' |
| 276 | +#' Local temp tables are stored as |
| 277 | +#' \code{ [tempdb].[dbo].[#name]________(padding using underscores)[numeric identifier] } |
| 278 | +#' |
| 279 | +#' True if: |
| 280 | +#' - If catalog_name is supplied it must equal "temdb" or "%" ( wildcard ) |
| 281 | +#' - Name must start with "#" followd by a non-"#" character |
| 282 | +#' @rdname SQLServer |
| 283 | +#' @usage NULL |
| 284 | +setMethod("isTempTable", c("Microsoft SQL Server", "character"), |
| 285 | + function(conn, name, catalog_name = NULL, schema_name = NULL, ...) { |
| 286 | + if ( !is.null(catalog_name) && |
| 287 | + catalog_name != "%" && |
| 288 | + length(catalog_name ) > 0 && |
| 289 | + catalog_name != "tempdb" ) { |
| 290 | + return(FALSE) |
| 291 | + } |
| 292 | + |
| 293 | + if ( !grepl("^[#][^#]", name ) ) { |
| 294 | + return(FALSE) |
| 295 | + } |
| 296 | + return(TRUE) |
| 297 | +}) |
| 298 | + |
| 299 | +#' SQL server specific dbExistsTable implementation that accounts for |
| 300 | +#' local temp tables. |
| 301 | +#' |
| 302 | +#' If we can identify that the name is that of a local temp table |
| 303 | +#' then adjust the identifier and query appropriately. |
| 304 | +#' |
| 305 | +#' Note, the implementation here is such that it assumes the metadata attribute is |
| 306 | +#' set such that catalog functions accept wildcard entries. |
| 307 | +#' |
| 308 | +#' Driver note. OEM driver will return correctly for |
| 309 | +#' name, \code{catalog_name = "tempdb"} in some circumstances. For exmaple |
| 310 | +#' if the name has no underscores to beginwith. FreeTDS, will not index |
| 311 | +#' the table correctly unless name is adjusted ( allowed trailing wildcards to |
| 312 | +#' accomodate trailing underscores and postfix ). |
| 313 | +#' |
| 314 | +#' Therefore, in all cases query for \code{name___%}. |
| 315 | +#' @rdname SQLServer |
| 316 | +#' @docType methods |
| 317 | +#' @aliases dbExistsTable |
| 318 | +#' @inherit DBI::dbExistsTable |
| 319 | +#' @usage NULL |
| 320 | +setMethod( |
| 321 | + "dbExistsTable", c("Microsoft SQL Server", "character"), |
| 322 | + function(conn, name, ...) { |
| 323 | + stopifnot(length(name) == 1) |
| 324 | + if ( isTempTable( conn, name, ... ) ) |
| 325 | + { |
| 326 | + name <- paste0(name, "\\_\\_\\_%"); |
| 327 | + df <- odbcConnectionTables(conn, name, catalog_name = "tempdb", schema_name = "dbo") |
| 328 | + } |
| 329 | + else { |
| 330 | + df <- odbcConnectionTables(conn, name = name, ...) |
| 331 | + } |
| 332 | + NROW(df) > 0 |
| 333 | + }) |
0 commit comments