-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathdbQuoteLiteral_DBIConnection.R
67 lines (57 loc) · 1.44 KB
/
dbQuoteLiteral_DBIConnection.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
#' @rdname hidden_aliases
#' @usage NULL
dbQuoteLiteral_DBIConnection <- function(conn, x, ...) {
# Switchpatching to avoid ambiguous S4 dispatch, so that our method
# is used only if no alternatives are available.
if (is(x, "SQL")) {
return(x)
}
if (is.factor(x)) {
return(dbQuoteString(conn, as.character(x)))
}
if (is.character(x)) {
return(dbQuoteString(conn, x))
}
if (inherits(x, "POSIXt")) {
return(dbQuoteString(
conn,
strftime(as.POSIXct(x), "%Y-%m-%d %H-%M-%S%z")
))
}
if (inherits(x, "Date")) {
return(dbQuoteString(conn, as.character(x)))
}
if (inherits(x, "difftime")) {
return(dbQuoteString(conn, format_hms(x)))
}
if (is.list(x)) {
blob_data <- vapply(
x,
function(x) {
if (is.null(x)) {
"NULL"
} else if (is.raw(x)) {
paste0("X'", paste(format(x), collapse = ""), "'")
} else {
stop("Lists must contain raw vectors or NULL", call. = FALSE)
}
},
character(1)
)
return(SQL(blob_data, names = names(x)))
}
if (is.double(x)) {
out <- sprintf("%.17e", x)
out[is.na(x)] <- "NULL"
return(SQL(out, names = names(x)))
}
if (is.logical(x)) {
x <- as.integer(x)
}
x <- as.character(x)
x[is.na(x)] <- "NULL"
SQL(x, names = names(x))
}
#' @rdname hidden_aliases
#' @export
setMethod("dbQuoteLiteral", signature("DBIConnection"), dbQuoteLiteral_DBIConnection)