SQL databases often have custom quotation syntax for identifiers and strings
which make writing SQL queries error prone and cumbersome to do. glue_sql()
and
glue_data_sql()
are analogs to glue()
and glue_data()
which handle the
SQL quoting. glue_sql_collapse()
can be used to collapse DBI::SQL()
objects.
They automatically quote character results, quote identifiers if the glue
expression is surrounded by backticks '`
' and do not quote
non-characters such as numbers. If numeric data is stored in a character
column (which should be quoted) pass the data to glue_sql()
as a
character.
Returning the result with DBI::SQL()
will suppress quoting if desired for
a given value.
Note parameterized queries are generally the safest and most efficient way to pass user defined values in a query, however not every database driver supports them.
If you place a *
at the end of a glue expression the values will be
collapsed with commas, or if there are no values, produce NULL
.
This is useful for (e.g.) the
SQL IN Operator.
glue_sql(
...,
.con,
.sep = "",
.envir = parent.frame(),
.open = "{",
.close = "}",
.na = DBI::SQL("NULL"),
.null = character(),
.comment = "#",
.literal = FALSE,
.trim = TRUE
)glue_data_sql(
.x,
...,
.con,
.sep = "",
.envir = parent.frame(),
.open = "{",
.close = "}",
.na = DBI::SQL("NULL"),
.null = character(),
.comment = "#",
.literal = FALSE,
.trim = TRUE
)
A DBI::SQL()
object with the given query.
[expressions
]
Unnamed arguments are taken to be expression
string(s) to format. Multiple inputs are concatenated together before formatting.
Named arguments are taken to be temporary variables available for substitution.
For glue_data()
, elements in ...
override the values in .x
.
[DBIConnection
]: A DBI connection object obtained from
DBI::dbConnect()
.
[character(1)
: ‘""’]
Separator used to separate elements.
[environment
: parent.frame()
]
Environment to evaluate each expression in. Expressions are
evaluated from left to right. If .x
is an environment, the expressions are
evaluated in that environment and .envir
is ignored. If NULL
is passed, it is equivalent to emptyenv()
.
[character(1)
: ‘\{’]
The opening delimiter. Doubling the
full delimiter escapes it.
[character(1)
: ‘\}’]
The closing delimiter. Doubling the
full delimiter escapes it.
[character(1)
: DBI::SQL("NULL")
]
Value to replace
NA
values with. If NULL
missing values are propagated, that is an NA
result will cause NA
output. Otherwise the value is replaced by the
value of .na
.
[character(1)
: ‘character()’]
Value to replace
NULL values with. If character()
whole output is character()
. If
NULL
all NULL values are dropped (as in paste0()
). Otherwise the
value is replaced by the value of .null
.
[character(1)
: ‘#’]
Value to use as the comment
character.
[boolean(1)
: ‘FALSE’]
Whether to treat single or
double quotes, backticks, and comments as regular characters (vs. as
syntactic elements), when parsing the expression string. Setting .literal = TRUE
probably only makes sense in combination with a custom
.transformer
, as is the case with glue_col()
. Regard this argument
(especially, its name) as experimental.
[logical(1)
: ‘TRUE’]
Whether to trim the input
template with trim()
or not.
[listish
]
An environment, list, or data frame used to lookup values.
glue_sql_collapse()
to collapse DBI::SQL()
objects.