sql.template: sql.template - lightweight SQL templating engine

Description References See Also Examples

Description

sql.template is a simple, mustache-based SQL templating library. It allows writing syntactically valid SQL that can execute as-is and simultaneously support injection of values programattically before sending the query for execution. It has three main functions

The package was designed so that the developers could share SQL code between developers from a shared or forked repository. It is very amenable to use with any of the R piping libraries.

References

http://en.wikipedia.org/wiki/Mustache_(template_system)
http://mustache.github
https://github.com/edwindj/whisker

See Also

sql
sqlutils: https://github.com/jbryer/sqlutils for an alternative approach

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
sql = "
SELECT
  *
FROM
  {{table}}
WHERE
  1 == 1
--fn: and FIRST_NAME = '{{first_name}}' /*ln: and LAST_NAME = '{{last_name}}'*/
"

replacement <- list( table = "pres", first_name = "Barack", last_name = "Obama" )

# COMPARE THE FOLLOWING:

sql_render( sql, replacement, render=FALSE, strip.comments = FALSE )
sql_render( sql, replacement )      # render
sql_render( sql, replacement, "fn" ) # render 'fn' tags
sql_render( sql, replacement, c("fn","ln") ) # render 'fn' tags

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