Except for sql
, parameters are default values to be used when none are
supplied in sql
(i.e. when sql
is a tibble returned by read_sql()
).
prepare_sql(
sql,
quotesql = "yes",
values = parent.frame(),
execmethod = "get",
geometry = NA,
default.conn = default_conn()
)
A tibble containing 1 row per query with the following fields:
character. A name for this query
"yes" or "no". Should parameterized character values be quoted for this query?
"yes" or "no". Should this query be parameterized with values from R?
The method to
execute this query. One of "get" (DBI::dbGetQuery()
), "execute"
(DBI::dbExecute()
), "sendq" (DBI::dbSendQuery()
), "sends"
(DBI::dbSendStatement()
) or "spatial" (sf::st_read()
)
character. If execmethod
is "spatial", which is the
geometry column?
character. The name of the database connection to use for this query. Must be the name of a configured sqlhelper connection.
The sql query as entered
The value of file_name
The sql query to be executed, i.e. with interpolations and quoting in place
An optionally-named list or character vector containing sql
commands, or a tibble returned by read_sql()
"yes" or "no" - should interpolated characters be quoted by default? Anything that isn't "no" is treated as "yes".
An environment containing variables to interpolate into the
SQL. Pass any object that is not an environment (commonly-used options
include "no", NA, FALSE or NULL) if interpolation is to be skipped, or
another environment containing values to interpolate to avoid using
.GlobalEnv
.
One of "get", "execute", "sendq", "sends" or "spatial" -
which method should be used to execute the query? "get" means
DBI::dbGetQuery()
; "execute" means DBI::dbExecute()
; "sendq" means
DBI::dbSendQuery; "sends" means DBI::dbSendStatement()
; "spatial" means
sf::st_read()
.
If execmethod
is "spatial", which column contains the
geometry? Ignored if execmethod
is not "spatial".
Either the name of a sqlhelper connection, or a database
connection returned by DBI::dbConnect()
or pool::pool()
, or NA. This
connection is only used by glue::glue_sql()
to quote SQL interpolations;
prepare_sql()
does not execute any SQL code.
The default.conn
parameter may be used to supply a connection object that
is not a configured sqlhelper connection which can then be used to
interpolate quoted strings.
library(sqlhelper)
connect(
system.file("examples/sqlhelper_db_conf.yml",
package="sqlhelper"),
exclusive = TRUE
)
n <- 5
foo <- 'bar'
prepped <- prepare_sql(c("select {`foo`}", "select {n}"))
prepped
prepped$prepared_sql
Run the code above in your browser using DataLab