| Type: | Package | 
| Title: | 'Targets' Extension for 'SQL' Queries | 
| Version: | 0.2.1 | 
| Maintainer: | David Ranzolin <daranzolin@gmail.com> | 
| 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. | 
| License: | MIT + file LICENSE | 
| Encoding: | UTF-8 | 
| Imports: | cli, DBI, fs, glue, jinjar, purrr, readr, rlang, stringr, tarchetypes, targets, withr | 
| URL: | https://github.com/daranzolin/sqltargets | 
| BugReports: | https://github.com/daranzolin/sqltargets/issues | 
| RoxygenNote: | 7.2.1 | 
| Suggests: | testthat (≥ 3.0.0), RSQLite (≥ 2.2.0) | 
| Config/testthat/edition: | 3 | 
| NeedsCompilation: | no | 
| Packaged: | 2024-10-02 00:03:22 UTC; dranzolin | 
| Author: | David Ranzolin [aut, cre, cph] | 
| Repository: | CRAN | 
| Date/Publication: | 2024-10-02 04:10:02 UTC | 
sqltargets package
Description
targets extension for SQL files
Details
See the README on GitHub
Get or Set sqltargets Options
Description
Get or Set sqltargets Options
Usage
sqltargets_option_get(option_name)
sqltargets_option_set(option_name, option_value)
Arguments
| option_name | Character. Option name. See Details. | 
| option_value | Value to assign to option 'x'. | 
Details
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()'.
Value
No return value, called for side effects
Target with a SQL query.
Description
Shorthand to include a SQL query in a 'targets' pipeline.
Usage
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")
)
Arguments
| 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  | 
Details
'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")'’.
Value
A data frame
Examples
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 SQL query dependencies.
Description
List the target dependencies of one or more SQL queries.
Usage
tar_sql_deps(path)
Arguments
| path | Character vector, path to one or more SQL queries. | 
Value
Character vector of the names of targets that are dependencies of the SQL query.
Examples
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)
Execute a SQL query.
Description
Internal function needed for 'tar_sql()'. Users should not invoke it directly.
Usage
tar_sql_exec(args, deps)
Arguments
| args | A named list of arguments to 'glue::glue_sql()'. | 
| deps | An unnamed list of target dependencies of the R Markdown report, automatically created by 'tar_sql_deps()'. | 
Value
a data frame.
Define upstream SQL file dep.
Description
Internal function needed for 'tar_sql()'. Users should not invoke it directly.
Usage
tar_sql_file(args)
Arguments
| args | Passed to 'fs::path_rel()'. | 
Value
A relative file path.
Target with a SQL query.
Description
Shorthand to include a SQL query in a 'targets' pipeline.
Usage
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
)
Arguments
| 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'. | 
Details
'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")'’.
Value
A data frame
Examples
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)
})