Title: | Easier 'SQL' Integration |
---|---|
Description: | Execute files of 'SQL' and manage database connections. 'SQL' statements and queries may be interpolated with string literals. Execution of individual statements and queries may be controlled with keywords. Multiple connections may be defined with 'YAML' and accessed by name. |
Authors: | Matthew Roberts [aut, cre, cph] |
Maintainer: | Matthew Roberts <[email protected]> |
License: | GPL (>= 3) |
Version: | 0.2.1 |
Built: | 2025-01-23 03:55:26 UTC |
Source: | https://github.com/majerr/sqlhelper |
Provides example configurations for several databases and a range of options
config_examples(filename = NA)
config_examples(filename = NA)
filename |
A string. If supplied, examples are written to a file with this name. |
Irrespective of whether a filename is supplied, yaml configuration examples will be returned invisibly as a single string and printed if the session is interactive.
A yaml string of database configuration examples, invisibly.
config_examples() # write the examples to a temporary file called 'examples.yml' config_examples(file.path(tempdir(), "examples.yml"))
config_examples() # write the examples to a temporary file called 'examples.yml' config_examples(file.path(tempdir(), "examples.yml"))
Closes any open connections, reads config files as directed by
config_filename
and exclusive
, and creates new connections from the
descriptions in those files.
connect(config_filename = NA, exclusive = FALSE)
connect(config_filename = NA, exclusive = FALSE)
config_filename |
String. The full name and path of a configuration
file, or "site", or "user", or "example", or |
exclusive |
Logical. If |
If exclusive=FALSE
(the default), configuration files will be sought in the
directory returned by rappdirs::site_config_dir()
, the directory returned
by rappdirs::user_config_dir()
, and finally a file named by
config_filename
(if not NA
). If elements of those files conflict, later
files overwrite the elements of earlier files.
If exclusive=TRUE
, only 1 file, indicated by the
config_filename
parameter, will be read.
If config_filename = "site"
, a config file called
sqlhelper_db_conf.yml
will be sought in the directory returned by
rappdirs::site_config_dir()
If config_filename = "user"
, a config file called
sqlhelper_db_conf.yml
will be sought in the directory returned by
rappdirs::user_config_dir()
If config_filename
is not NULL
(but not "site" or "user"), it is
assumed to name a file.
A warning is raised if no valid configurations are found (e.g. connect()
is
called without arguments and no site- or user-wide files are present, or the
connections in those files are invalid)
vignette("connections")
explains how to write a
config file and how to access the created connections.
NULL
, invisibly
library(sqlhelper) example_filename <- system.file("examples", "sqlhelper_db_conf.yml", package = "sqlhelper") # Search for config files in rappdirs::site_config_dir(), # rappdirs::user_config_dir(), and read from example_filename connect(example_filename) # Read only the named example file connect(example_filename, exclusive=TRUE)
library(sqlhelper) example_filename <- system.file("examples", "sqlhelper_db_conf.yml", package = "sqlhelper") # Search for config files in rappdirs::site_config_dir(), # rappdirs::user_config_dir(), and read from example_filename connect(example_filename) # Read only the named example file connect(example_filename, exclusive=TRUE)
Provides information about created connections.
connection_info(name_str = ".*", exact = TRUE)
connection_info(name_str = ".*", exact = TRUE)
name_str |
A regular expression to be used to identify connection names to include. The default ('.*') returns all of them. |
exact |
TRUE or FALSE. Should |
Null, or a tibble with 1 row per identified connection and the following fields:
identifier (character)
a description of the connection, if found in the conf file (character)
is this connection valid and live? (logical)
the name of the driver function (character)
the string used to parameterize the connection (character)
is this a pool connection? (logical)
If no connection names matched name_str
, the tibble will be empty. If no
connections have been configured (e.g. connect()
has not been called),
NULL
is returned.
library(sqlhelper) connect( system.file( "examples/sqlhelper_db_conf.yml", package="sqlhelper" ), exclusive=TRUE ) connection_info() connection_info("pool_sqlite")
library(sqlhelper) connect( system.file( "examples/sqlhelper_db_conf.yml", package="sqlhelper" ), exclusive=TRUE ) connection_info() connection_info("pool_sqlite")
A convenience wrapper around live_connection()
and get_default_conn_name()
default_conn()
default_conn()
A database connection returned by DBI::dbConnect()
or
pool::dbPool()
library(sqlhelper) connect( system.file( "examples/sqlhelper_db_conf.yml", package="sqlhelper" ), exclusive=TRUE ) default_conn()
library(sqlhelper) connect( system.file( "examples/sqlhelper_db_conf.yml", package="sqlhelper" ), exclusive=TRUE ) default_conn()
Close all connections and remove them from the connections cache
disconnect()
disconnect()
NULL
, invisibly
library(sqlhelper) connect( system.file("examples", "sqlhelper_db_conf.yml", package="sqlhelper") ) disconnect()
library(sqlhelper) connect( system.file("examples", "sqlhelper_db_conf.yml", package="sqlhelper") ) disconnect()
Test whether a database is connected
is_connected(conn_name) not_connected(conn_name)
is_connected(conn_name) not_connected(conn_name)
conn_name |
Character. The name of a connection (run |
Logical, or NULL if conn_name
does not identify exactly 1
connection
library(sqlhelper) connect( system.file("examples/sqlhelper_db_conf.yml", package="sqlhelper") ) connection_info() is_connected("simple_sqlite") is_connected("foo") DBI::dbDisconnect(live_connection("simple_sqlite")) is_connected("simple_sqlite") not_connected("simple_sqlite") disconnect() is_connected("simple_sqlite") not_connected("simple_sqlite")
library(sqlhelper) connect( system.file("examples/sqlhelper_db_conf.yml", package="sqlhelper") ) connection_info() is_connected("simple_sqlite") is_connected("foo") DBI::dbDisconnect(live_connection("simple_sqlite")) is_connected("simple_sqlite") not_connected("simple_sqlite") disconnect() is_connected("simple_sqlite") not_connected("simple_sqlite")
Return the named connection or NULL
live_connection(conn_name)
live_connection(conn_name)
conn_name |
Chr. The name of the live connection you want (use connection_info to get names of available connections). |
A live connection to a database, or NULL, invisibly, if
conn_name
is not the name of a live connection
library(sqlhelper) connect( system.file("examples/sqlhelper_db_conf.yml", package="sqlhelper") ) connection_info() conn <- live_connection("simple_sqlite") conn DBI::dbDisconnect(conn) is.null(live_connection("simple_sqlite")) is.null(live_connection("foo"))
library(sqlhelper) connect( system.file("examples/sqlhelper_db_conf.yml", package="sqlhelper") ) connection_info() conn <- live_connection("simple_sqlite") conn DBI::dbDisconnect(conn) is.null(live_connection("simple_sqlite")) is.null(live_connection("foo"))
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() )
prepare_sql( sql, quotesql = "yes", values = parent.frame(), execmethod = "get", geometry = NA, default.conn = default_conn() )
sql |
An optionally-named list or character vector containing sql
commands, or a tibble returned by |
quotesql |
"yes" or "no" - should interpolated characters be quoted by default? Anything that isn't "no" is treated as "yes". |
values |
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
|
execmethod |
One of "get", "execute", "sendq", "sends" or "spatial" -
which method should be used to execute the query? "get" means
|
geometry |
If |
default.conn |
Either the name of a sqlhelper connection, or a database
connection returned by |
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.
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
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
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
Read a sql file and return it's contents as a tibble
read_sql(file_name, cascade = TRUE)
read_sql(file_name, cascade = TRUE)
file_name |
Full name and path of a file to read |
cascade |
Parameters for executing each query may be specified as
comments in the SQL file. If |
Multiple SQL queries in files should be terminated by semi-colons
(;
), as usual.
The values of qname
, quotesql
, interpolate
, execmethod
, geometry
,
and conn_name
in the output may be specified with comments immediately
preceding each query (see examples).
With the exception of qname
, the value of each interpreted comment is
cascaded to subsequent queries (assuming cascade=TRUE
). This means you
may set values once for the first query in the file and they will apply to
all the queries thereafter.
See run_queries()
for the implications of setting execution parameters.
See prepare_sql()
for the treatment of missing values in the output and
their defaults. The article vignette("execution")
has further examples of
using these parameters to control execution.
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 to be executed
The value of file_name
library(sqlhelper) fn <- system.file( "examples/read_sql_execution_params.SQL", package="sqlhelper" ) readLines( fn ) |> writeLines() sql_tibble <- read_sql(fn) sql_tibble sql_tibble$sql fn <- system.file( "examples/read_sql_comments.SQL", package="sqlhelper" ) readLines( fn ) |> writeLines() sql_tibble <- read_sql(fn) sql_tibble sql_tibble$sql
library(sqlhelper) fn <- system.file( "examples/read_sql_execution_params.SQL", package="sqlhelper" ) readLines( fn ) |> writeLines() sql_tibble <- read_sql(fn) sql_tibble sql_tibble$sql fn <- system.file( "examples/read_sql_comments.SQL", package="sqlhelper" ) readLines( fn ) |> writeLines() sql_tibble <- read_sql(fn) sql_tibble sql_tibble$sql
Accepts a character vector of SQL file names and attempts to execute the queries in each one.
run_files(filenames, ..., include_params = FALSE) runfiles(filenames, ..., include_params = FALSE)
run_files(filenames, ..., include_params = FALSE) runfiles(filenames, ..., include_params = FALSE)
filenames |
name, or vector of names, of file(s) to be executed |
... |
Arguments to be passed to |
include_params |
|
If no default connection is supplied via default.conn
and no
connections have been configured using connect()
, an attempt will be made
to configure connections via connect()
using the configuration search
path. If no database connections are available after this attempt, an error
will be raised. See vignette("connections")
for details about the
configuration search path.
run_files()
calls read_sql()
on each file, and prepare_sql()
on the
queries read from those files. Prepared queries are executed with run_queries()
. The
behaviour of those functions can be controlled by passing the relevant
parameters to run_files()
as the ...
argument.
run_files()
also enables control of the arguments accepted by run_queries()
on
a per-query basis, by interpreting comments in SQL files as described
for read_sql()
. Interpreted comments precede the sql query to which they
refer. Each interpretable comment must be on a line by itself and take the
form:
-- keyword = value
Keywords and possible values for interpretable comments are:
A name for this query
"yes" or "no" - should interpolated characters be quoted?
"yes" or "no" - should sql be interpolated?
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()
.
The name of a spatial column. Ignored if execmethod
is not 'spatial'
The name of a connection to execute this query against
All interpreted comments except qname
are cascaded within their file,
meaning that if you want to use the same values throughout, you need only set
them for the first query. See read_sql()
for details.
A list of results of sql queries found in files
Other SQL runners:
run_queries()
library(sqlhelper) config_filename <- system.file("examples/sqlhelper_db_conf.yml", package="sqlhelper") readLines( config_filename ) |> writeLines() connect( config_filename, exclusive=TRUE) DBI::dbWriteTable( default_conn(), "iris", iris) sf::st_write(spData::congruent, default_conn(), "congruent") sf::st_write(spData::incongruent, live_connection("pool_sqlite"), "incongruent") run_files_ex1 <- system.file("examples/run_files_ex1.sql", package="sqlhelper") readLines( run_files_ex1 ) |> writeLines() run_files_ex2 <- system.file("examples/run_files_ex2.sql", package="sqlhelper") readLines( run_files_ex2 ) |> writeLines() n_longest_petals <- 5 results <- run_files( c( run_files_ex1, run_files_ex2 ) ) names(results) results$how_many_irises results$n_longest_setosa_petal_lengths plot(results$get_congruent, border = "orange") plot(results$get_incongruent, border = "blue", add=TRUE)
library(sqlhelper) config_filename <- system.file("examples/sqlhelper_db_conf.yml", package="sqlhelper") readLines( config_filename ) |> writeLines() connect( config_filename, exclusive=TRUE) DBI::dbWriteTable( default_conn(), "iris", iris) sf::st_write(spData::congruent, default_conn(), "congruent") sf::st_write(spData::incongruent, live_connection("pool_sqlite"), "incongruent") run_files_ex1 <- system.file("examples/run_files_ex1.sql", package="sqlhelper") readLines( run_files_ex1 ) |> writeLines() run_files_ex2 <- system.file("examples/run_files_ex2.sql", package="sqlhelper") readLines( run_files_ex2 ) |> writeLines() n_longest_petals <- 5 results <- run_files( c( run_files_ex1, run_files_ex2 ) ) names(results) results$how_many_irises results$n_longest_setosa_petal_lengths plot(results$get_congruent, border = "orange") plot(results$get_incongruent, border = "blue", add=TRUE)
Accepts a character vector of SQL queries and attempts to execute each
run_queries(sql, ..., default.conn = default_conn(), include_params = FALSE) runqueries(sql, ..., default.conn = default_conn(), include_params = FALSE)
run_queries(sql, ..., default.conn = default_conn(), include_params = FALSE) runqueries(sql, ..., default.conn = default_conn(), include_params = FALSE)
sql |
An optionally-named list or character vector containing sql
strings, or a tibble returned by |
... |
Arguments to be passed to |
default.conn |
Either the name of a sqlhelper connection, or a database
connection returned by |
include_params |
|
If no default connection is supplied via default.conn
and no
connections have been configured using connect()
, an attempt will be made
to configure connections via connect()
using the configuration search
path. If no database connections are available after this attempt, an error
will be raised. See vignette("connections")
for details about the
configuration search path.
If include_params
is FALSE
and the sql
argument is a
vector, a list containing the results of each query; element names will be
taken from the sql
argument.
If the length of the sql
argument is 1 and is not named, the result of
that query is returned as-is (e.g. a data.frame), not as a 1-element list.
If include_params
is TRUE
, a tibble is returned 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", this should be the name of the geometry column.
character. The name of the database connection against which to execute this query. Must be the name of a configured sqlhelper connection.
The sql query to be executed
The value of file_name
The sql query to be executed, i.e. with interpolations and quoting in place
The result of the query
Other SQL runners:
run_files()
library(sqlhelper) readLines( system.file("examples/sqlhelper_db_conf.yml", package="sqlhelper") ) |> writeLines() connect( system.file("examples/sqlhelper_db_conf.yml", package="sqlhelper"), exclusive=TRUE) DBI::dbWriteTable( default_conn(), "iris", iris) n <- 5 run_queries( c(top_n = "select * from iris limit {n}", uniqs = "select distinct species as species from iris") ) ## use include_params to review the execution context run_queries( c(top_n = "select * from iris limit {n}", uniqs = "select distinct species as species from iris"), include_params = TRUE ) ## pass an env of interpolation values to the 'values' parameter ## result of a single, unnamed query is returned as an object, not a ## 1-element list e <- new.env() e$n <- 2 run_queries( "select * from iris limit {n}", values = e ) ## Use the execmethod parameter for statements run_queries("create table iris_setosa as select * from iris where species = 'setosa'", execmethod = 'execute') run_queries("select distinct species as species from iris_setosa")
library(sqlhelper) readLines( system.file("examples/sqlhelper_db_conf.yml", package="sqlhelper") ) |> writeLines() connect( system.file("examples/sqlhelper_db_conf.yml", package="sqlhelper"), exclusive=TRUE) DBI::dbWriteTable( default_conn(), "iris", iris) n <- 5 run_queries( c(top_n = "select * from iris limit {n}", uniqs = "select distinct species as species from iris") ) ## use include_params to review the execution context run_queries( c(top_n = "select * from iris limit {n}", uniqs = "select distinct species as species from iris"), include_params = TRUE ) ## pass an env of interpolation values to the 'values' parameter ## result of a single, unnamed query is returned as an object, not a ## 1-element list e <- new.env() e$n <- 2 run_queries( "select * from iris limit {n}", values = e ) ## Use the execmethod parameter for statements run_queries("create table iris_setosa as select * from iris where species = 'setosa'", execmethod = 'execute') run_queries("select distinct species as species from iris_setosa")
Set/get the name of the default connection to use
set_default_conn_name(conn_name) get_default_conn_name()
set_default_conn_name(conn_name) get_default_conn_name()
conn_name |
Character string. The name a connection |
get
returns the name of the default connection;
set
returns NULL
, invisibly.
library(sqlhelper) connect( system.file("examples/sqlhelper_db_conf.yml", package="sqlhelper"), exclusive = TRUE ) connection_info() get_default_conn_name() set_default_conn_name("pool_sqlite") connection_info() get_default_conn_name()
library(sqlhelper) connect( system.file("examples/sqlhelper_db_conf.yml", package="sqlhelper"), exclusive = TRUE ) connection_info() get_default_conn_name() set_default_conn_name("pool_sqlite") connection_info() get_default_conn_name()