Skip to content

Commit

Permalink
Single dispatch for dbQuoteLiteral()
Browse files Browse the repository at this point in the history
  • Loading branch information
krlmlr committed Sep 15, 2021
1 parent 3ce24df commit 0712f29
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 114 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Imports:
bit64,
blob (>= 1.2.0),
DBI (>= 1.1.0),
hms (>= 0.5.0),
hms (>= 1.0.0),
lubridate,
methods,
Rcpp (>= 1.0.7),
Expand Down
129 changes: 45 additions & 84 deletions R/quote.R
Original file line number Diff line number Diff line change
Expand Up @@ -111,91 +111,52 @@ as_table <- function(catalog, schema, table) {
}

#' @export
#' @rdname quote
setMethod("dbQuoteLiteral", c("PqConnection", "logical"), function(conn, x, ...) {
ret <- as.character(x)
ret[is.na(ret)] <- "NULL"
SQL(ret, names = names(ret))
})

#' @export
#' @rdname quote
setMethod("dbQuoteLiteral", c("PqConnection", "integer"), function(conn, x, ...) {
ret <- paste0(as.character(x), "::int4")
ret[is.na(x)] <- "NULL"
SQL(ret, names = names(ret))
})

#' @export
#' @rdname quote
setMethod("dbQuoteLiteral", c("PqConnection", "numeric"), function(conn, x, ...) {
ret <- paste0(as.character(x), "::float8")
ret[is.na(x)] <- "NULL"
SQL(ret, names = names(ret))
})

#' @export
#' @rdname quote
setMethod("dbQuoteLiteral", c("PqConnection", "factor"), function(conn, x, ...) {
dbQuoteLiteral(conn, as.character(x))
})

#' @export
#' @rdname quote
setMethod("dbQuoteLiteral", c("PqConnection", "Date"), function(conn, x, ...) {
ret <- paste0("'", as.character(x), "'::date")
ret[is.na(x)] <- "NULL"
SQL(ret, names = names(ret))
})

#' @export
#' @rdname quote
setMethod("dbQuoteLiteral", c("PqConnection", "POSIXt"), function(conn, x, ...) {
ret <- paste0("'", as.character(lubridate::with_tz(x, conn@timezone)), "'::timestamp")
ret[is.na(x)] <- "NULL"
SQL(ret, names = names(ret))
})

#' @export
#' @rdname quote
setMethod("dbQuoteLiteral", c("PqConnection", "difftime"), function(conn, x, ...) {
# https://github.com/tidyverse/hms/issues/84
mode(x) <- "double"

ret <- paste0("'", as.character(hms::as_hms(x)), "'::interval")
ret[is.na(x)] <- "NULL"
SQL(ret, names = names(ret))
})

#' @export
#' @rdname quote
setMethod("dbQuoteLiteral", c("PqConnection", "list"), function(conn, x, ...) {
quote_blob(x)
})

#' @export
#' @rdname quote
#' @importFrom blob blob
setMethod("dbQuoteLiteral", c("PqConnection", "blob"), function(conn, x, ...) {
quote_blob(x)
})
#' @rdname quote
setMethod("dbQuoteLiteral", "PqConnection", function(conn, x, ...) {
if (is.factor(x)) {
x <- as.character(x)
}

quote_blob <- function(x) {
blob_data <- vcapply(
x,
function(x) {
if (is.null(x)) "NULL"
else if (is.raw(x)) paste0("E'\\\\x", paste(format(x), collapse = ""), "'")
else {
stop("Lists must contain raw vectors or NULL", call. = FALSE)
if (inherits(x, "Date")) {
ret <- paste0("'", as.character(x), "'::date")
ret[is.na(x)] <- "NULL"
SQL(ret, names = names(ret))
} else if (inherits(x, "POSIXt")) {
ret <- paste0("'", as.character(lubridate::with_tz(x, conn@timezone)), "'::timestamp")
ret[is.na(x)] <- "NULL"
SQL(ret, names = names(ret))
} else if (inherits(x, "difftime")) {
ret <- paste0("'", as.character(hms::as_hms(x)), "'::interval")
ret[is.na(x)] <- "NULL"
SQL(ret, names = names(ret))
} else if (is.logical(x)) {
ret <- as.character(x)
ret[is.na(ret)] <- "NULL"
SQL(ret, names = names(ret))
} else if (is.integer(x)) {
ret <- paste0(as.character(x), "::int4")
ret[is.na(x)] <- "NULL"
SQL(ret, names = names(ret))
} else if (is.numeric(x)) {
ret <- paste0(as.character(x), "::float8")
ret[is.na(x)] <- "NULL"
SQL(ret, names = names(ret))
} else if (is.list(x) || inherits(x, "blob")) {
blob_data <- vcapply(
x,
function(x) {
if (is.null(x)) "NULL"
else if (is.raw(x)) paste0("E'\\\\x", paste(format(x), collapse = ""), "'")
else {
stopc("Lists must contain raw vectors or NULL")
}
}
}
)
SQL(blob_data, names = names(x))
}

#' @export
#' @rdname quote
setMethod("dbQuoteLiteral", c("PqConnection", "character"), function(conn, x, ...) {
dbQuoteString(conn, x)
)
SQL(blob_data, names = names(x))
} else if (is.character(x)) {
dbQuoteString(conn, x)
} else {
stopc("Can't convert value of class ", class(x)[[1]], " to SQL.", call. = FALSE)
}
})
31 changes: 2 additions & 29 deletions man/quote.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0712f29

Please sign in to comment.