sql_activate: Activate (uncomment) Part(s) of a SQL Template

Description Usage Arguments Details Tagged Comments Note See Also Examples

View source: R/sql_activate.R

Description

Activates (uncomments) Part of a SQL Template based on supplied data.

Usage

1
2
3
4
5
6
7
8
sql_activate(
  x,
  tags = if (!is.null(getOption("sql.tags"))) getOption("sql.tags") else "",
  block = TRUE,
  inline = TRUE
)

sql_uncomment(...)

Arguments

x

character or sql object; if character, it is first coerced to SQL

tags

character; tags that should be uncommenting. The default is getOption(sql.tags) or else "" – all comments are uncommented.

block

logical; uncomment block (/* ... */) comments. Default: TRUE

inline

logical; uncomment inline (– ...) comments. Default: TRUE

Details

sql_activate is not the same as sql_strip_comments() which completely removes comments from a SQL statement.

sql_activate removes comment delimiters so that comments that normally skipped will be evaluated by a SQL engine.

By default, all comments are uncommented, setting the global option sql.tag or passing an argument to tags will uncomment only tagged comments.

Tagged Comments

Tagged comments allow the user to implement additional functionality at run-time. This allows queries to be shared by multiple functions. For example, a query might be used in a bulk operation operating on all records or an iterative procedure where a subset of records are used.

Comments are uncommeted by tags. By default, the tag is "", namely all comments. A common case is to have a part of the where clause be for development. For example, –dev: AND user_id = 1 or /*dev: AND user_id = 1 */.

Comments can be tagged in the SQL code, by placing the tag as the first letter type character(s) of the query followed by a colon. For example:

inline: --tag_name:
block: /*tag_name:*/

A comment can only have one tag.

This interface may change in the future.

Note

Todo: Fixes white-space problems related to uncommenting.

See Also

sql
sql_render

Examples

1
2
3
4
5
  sql <- "SELECT  \n col a \n --, col b \n FROM table"
  sql_activate( sql )

  sql <- "SELECT col a /*, col b */ FROM table"
  sql_activate( sql )

decisionpatterns/sql.template documentation built on July 6, 2020, 8:41 a.m.