SQL Templar is template standard for SQL. It uses mustache + SQL comments to implement a lightweight and powerful template solution for SQL.
sql.templar
provides a light weight, language-agnostic, implementation-agnostic
method for building parameterizaed queries. It allows some minimal logic.
sqltemplar is best demonstrated by some examples.
SELECT
, INSERT
, DELETE
, REPLACE
, ....SQL Templar works by de/activating individual lines from otherwise valid SQL statements.
There are many situations in which SQL queries vary only slightly from various aspects of a software application. These multiple queries are supported in various ways, one
Some basic examples showing SQL Templar abilities:
SELECT *
FROM CUSTOMER
WHERE CUSTOMER.AGE >= 21
SQL is unchanged.
{{expr}}
Values in {{expr}} are evaluated in the defined scope.
AGE=18
SELECT *
FROM CUSTOMER
WHERE CUSTOMER.AGE >={{AGE}}
The variable AGE
is substitued. If AGE
is undefined, an error is issued.
--! :
Apply line only when variable is evaluated in the defined scope.
AGE=18
SELECT *
FROM CUSTOMER
--! : WHERE CUSTOMER.AGE >= {{AGE}}
--! : AGE: WHERE CUSTOMER.AGE >= {{AGE}}
Special comments denoted by --!:
are activated when those
variables/expressions are defined. Otherwise, the SQL passes unaltered. No
error is thrown.
--!expr:
Only activation the line when the expr
is defined. The preceeding example
can also be written as this.
SELECT *
FROM CUSTOMER
--!AGE: WHERE CUSTOMER.AGE >= {{AGE}}
Conditional activation can be based on a different variable than what is used.
customer = TRUE
SELECT *
FROM CUSTOMER
--!customer: WHERE CUSTOMER.AGE >= 18
Use conditional activation with conditional
customer = TRUE
age = 18
SELECT *
FROM CUSTOMER
--!customer: WHERE CUSTOMER.AGE >= {{age}}
SQL Templar can be used for multiple tables by using conditional logic on both the
--! :
comment is uncommented if all whisker parameters are present--!logic
: SQL Templar has been ported to the following languages
The dplyr packages provides and automated way of building SQL -- but that SQL is generally generic and requires specific backend drivers.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.