Title: | 'Targets' Extension for 'SQL' Queries |
---|---|
Description: | Provides an extension for 'SQL' queries as separate file within 'targets' pipelines. The shorthand creates two targets, the query file and the query result. |
Authors: | David Ranzolin [aut, cre, cph] |
Maintainer: | David Ranzolin <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.2.1 |
Built: | 2025-01-30 02:36:16 UTC |
Source: | https://github.com/daranzolin/sqltargets |
Get or Set sqltargets Options
sqltargets_option_get(option_name) sqltargets_option_set(option_name, option_value)
sqltargets_option_get(option_name) sqltargets_option_set(option_name, option_value)
option_name |
Character. Option name. See Details. |
option_value |
Value to assign to option 'x'. |
Available Options
- ‘"sqltargets.template_engine"' - Either ’glue' or 'jinjar'. Determines how the query file should be parsed.
- '"sqltargets.glue_sql_opening_delimiter"' - character. Length 1. The opening delimiter passed to 'glue::glue_sql()'.
- '"sqltargets.glue_sql_closing_delimiter"' - character. Length 1. The closing delimiter passed to 'glue::glue_sql()'.
- '"sqltargets.jinja_block_open"' - character. Length 1. The opening delimiter passed to 'jinjar::jinjar_config()'.
- '"sqltargets.jinja_block_close"' - character. Length 1. The closing delimiter passed to 'jinjar::jinjar_config()'.
- '"sqltargets.jinja_variable_open"' - character. Length 1. The closing delimiter passed to 'jinjar::jinjar_config()'.
- '"sqltargets.jinja_variable_close"' - character. Length 1. The closing delimiter passed to 'jinjar::jinjar_config()'.
- '"sqltargets.jinja_comment_open"' - character. Length 1. The closing delimiter passed to 'jinjar::jinjar_config()'.
- '"sqltargets.jinja_comment_close"' - character. Length 1. The closing delimiter passed to 'jinjar::jinjar_config()'.
No return value, called for side effects
Shorthand to include a SQL query in a 'targets' pipeline.
tar_sql( name, path, params = list(), format = targets::tar_option_get("format"), tidy_eval = targets::tar_option_get("tidy_eval"), repository = targets::tar_option_get("repository"), iteration = targets::tar_option_get("iteration"), error = targets::tar_option_get("error"), memory = targets::tar_option_get("memory"), garbage_collection = targets::tar_option_get("garbage_collection"), deployment = targets::tar_option_get("deployment"), priority = targets::tar_option_get("priority"), resources = targets::tar_option_get("resources"), storage = targets::tar_option_get("storage"), retrieval = targets::tar_option_get("retrieval"), cue = targets::tar_option_get("cue") )
tar_sql( name, path, params = list(), format = targets::tar_option_get("format"), tidy_eval = targets::tar_option_get("tidy_eval"), repository = targets::tar_option_get("repository"), iteration = targets::tar_option_get("iteration"), error = targets::tar_option_get("error"), memory = targets::tar_option_get("memory"), garbage_collection = targets::tar_option_get("garbage_collection"), deployment = targets::tar_option_get("deployment"), priority = targets::tar_option_get("priority"), resources = targets::tar_option_get("resources"), storage = targets::tar_option_get("storage"), retrieval = targets::tar_option_get("retrieval"), cue = targets::tar_option_get("cue") )
name |
Symbol, name of the target. A target
name must be a valid name for a symbol in R, and it
must not start with a dot. Subsequent targets
can refer to this name symbolically to induce a dependency relationship:
e.g. |
path |
Character of length 1 to the single '*.sql' source file to be executed. Defaults to the working directory of the 'targets' pipeline. |
params |
Code, can be 'NULL'. 'params' evaluates to a named list of parameters that are passed to 'jinjar::render()'. The list is quoted (not evaluated until the target runs) so that upstream targets can serve as parameter values. |
format |
Optional storage format for the target's return value.
With the exception of |
tidy_eval |
Logical, whether to enable tidy evaluation
when interpreting |
repository |
Character of length 1, remote repository for target storage. Choices:
Note: if |
iteration |
Character of length 1, name of the iteration mode of the target. Choices:
|
error |
Character of length 1, what to do if the target stops and throws an error. Options:
|
memory |
Character of length 1, memory strategy.
If |
garbage_collection |
Logical, whether to run |
deployment |
Character of length 1, only relevant to
|
priority |
Numeric of length 1 between 0 and 1. Controls which
targets get deployed first when multiple competing targets are ready
simultaneously. Targets with priorities closer to 1 get built earlier
(and polled earlier in |
resources |
Object returned by |
storage |
Character of length 1, only relevant to
|
retrieval |
Character of length 1, only relevant to
|
cue |
An optional object from |
'tar_sql()' is an alternative to 'tar_target()' for SQL queries that depend on upstream targets. The SQL source files ('*.sql' files) should mention dependency targets with 'tar_load()' within SQL comments ('–'). (Do not use 'tar_load_raw()' or 'tar_read_raw()' for this.) Then, 'tar_sql()' defines a special kind of target. It 1. Finds all the 'tar_load()'/'tar_read()' dependencies in the query and inserts them into the target's command. This enforces the proper dependency relationships. (Do not use 'tar_load_raw()' or 'tar_read_raw()' for this.) 2. Sets 'format = "file"' (see 'tar_target()') so 'targets' watches the files at the returned paths and reruns the query if those files change. 3. Creates another upstream target to watch the query file for changes '<target name> ‘sqltargets_option_get("sqltargets.target_file_suffix")'’.
A data frame
targets::tar_dir({ # tar_dir() runs code from a temporary directory. # Unparameterized SQL query: lines <- c( "-- !preview conn=DBI::dbConnect(RSQLite::SQLite())", "-- targets::tar_load(data1)", "-- targets::tar_load(data2)", "select 1 AS my_col", "" ) # In tar_dir(), not part of the user's file space: writeLines(lines, "query.sql") # Include the query in a pipeline as follows. targets::tar_script({ library(tarchetypes) library(sqltargets) list( tar_sql(query, path = "query.sql") ) }, ask = FALSE) })
targets::tar_dir({ # tar_dir() runs code from a temporary directory. # Unparameterized SQL query: lines <- c( "-- !preview conn=DBI::dbConnect(RSQLite::SQLite())", "-- targets::tar_load(data1)", "-- targets::tar_load(data2)", "select 1 AS my_col", "" ) # In tar_dir(), not part of the user's file space: writeLines(lines, "query.sql") # Include the query in a pipeline as follows. targets::tar_script({ library(tarchetypes) library(sqltargets) list( tar_sql(query, path = "query.sql") ) }, ask = FALSE) })
List the target dependencies of one or more SQL queries.
tar_sql_deps(path)
tar_sql_deps(path)
path |
Character vector, path to one or more SQL queries. |
Character vector of the names of targets that are dependencies of the SQL query.
lines <- c( "-- !preview conn=DBI::dbConnect(RSQLite::SQLite())", "-- targets::tar_load(data1)", "-- targets::tar_read(data2)", "select 1 as my_col", "" ) query <- tempfile() writeLines(lines, query) tar_sql_deps(query)
lines <- c( "-- !preview conn=DBI::dbConnect(RSQLite::SQLite())", "-- targets::tar_load(data1)", "-- targets::tar_read(data2)", "select 1 as my_col", "" ) query <- tempfile() writeLines(lines, query) tar_sql_deps(query)
Shorthand to include a SQL query in a 'targets' pipeline.
tar_sql_raw( name, path = ".", params = params, format = format, error = targets::tar_option_get("error"), memory = targets::tar_option_get("memory"), garbage_collection = targets::tar_option_get("garbage_collection"), deployment = "main", priority = targets::tar_option_get("priority"), resources = targets::tar_option_get("resources"), retrieval = targets::tar_option_get("retrieval"), cue = targets::tar_option_get("cue"), params_nm = NULL )
tar_sql_raw( name, path = ".", params = params, format = format, error = targets::tar_option_get("error"), memory = targets::tar_option_get("memory"), garbage_collection = targets::tar_option_get("garbage_collection"), deployment = "main", priority = targets::tar_option_get("priority"), resources = targets::tar_option_get("resources"), retrieval = targets::tar_option_get("retrieval"), cue = targets::tar_option_get("cue"), params_nm = NULL )
name |
Character of length 1, name of the target. A target
name must be a valid name for a symbol in R, and it
must not start with a dot. Subsequent targets
can refer to this name symbolically to induce a dependency relationship:
e.g. |
path |
Character of length 1 to the single '*.sql' source file to be executed. Defaults to the working directory of the 'targets' pipeline. |
params |
Code, can be 'NULL'. 'params' evaluates to a named list of parameters that are passed to 'jinjar::render()'. The list is quoted (not evaluated until the target runs) so that upstream targets can serve as parameter values. |
format |
Optional storage format for the target's return value.
With the exception of |
error |
Character of length 1, what to do if the target stops and throws an error. Options:
|
memory |
Character of length 1, memory strategy.
If |
garbage_collection |
Logical, whether to run |
deployment |
Character of length 1, only relevant to
|
priority |
Numeric of length 1 between 0 and 1. Controls which
targets get deployed first when multiple competing targets are ready
simultaneously. Targets with priorities closer to 1 get built earlier
(and polled earlier in |
resources |
Object returned by |
retrieval |
Character of length 1, only relevant to
|
cue |
An optional object from |
params_nm |
Character of length 1, name of object passed to 'params'. |
'tar_sql()' is an alternative to 'tar_target()' for SQL queries that depend on upstream targets. The SQL source files ('*.sql' files) should mention dependency targets with 'tar_load()' within SQL comments ('–'). (Do not use 'tar_load_raw()' or 'tar_read_raw()' for this.) Then, 'tar_sql()' defines a special kind of target. It 1. Finds all the 'tar_load()'/'tar_read()' dependencies in the query and inserts them into the target's command. This enforces the proper dependency relationships. (Do not use 'tar_load_raw()' or 'tar_read_raw()' for this.) 2. Sets 'format = "file"' (see 'tar_target()') so 'targets' watches the files at the returned paths and reruns the query if those files change. 3. Creates another upstream target to watch the query file for changes '<target name> ‘sqltargets_option_get("sqltargets.target_file_suffix")'’.
A data frame
targets::tar_dir({ # tar_dir() runs code from a temporary directory. # Unparameterized SQL query: lines <- c( "-- !preview conn=DBI::dbConnect(RSQLite::SQLite())", "-- targets::tar_load(data1)", "-- targets::tar_load(data2)", "select 1 AS my_col", "" ) # In tar_dir(), not part of the user's file space: writeLines(lines, "query.sql") # Include the query in a pipeline as follows. targets::tar_script({ library(tarchetypes) library(sqltargets) list( tar_sql(query, path = "query.sql") ) }, ask = FALSE) })
targets::tar_dir({ # tar_dir() runs code from a temporary directory. # Unparameterized SQL query: lines <- c( "-- !preview conn=DBI::dbConnect(RSQLite::SQLite())", "-- targets::tar_load(data1)", "-- targets::tar_load(data2)", "select 1 AS my_col", "" ) # In tar_dir(), not part of the user's file space: writeLines(lines, "query.sql") # Include the query in a pipeline as follows. targets::tar_script({ library(tarchetypes) library(sqltargets) list( tar_sql(query, path = "query.sql") ) }, ask = FALSE) })