new SqlTemplate()
Class for replacing values in SQL strings and appending filters to the WHERE clause
- Source:
Extends
Methods
replace(str, values) → {string}
Replace tokens in a string with values from a provided object
Parameters:
Name | Type | Description |
---|---|---|
str |
string | String with tokens to be replaced |
values |
Object.<string, string> | Values token for replacement |
- Inherited From:
- Source:
Returns:
String with replacement value substitution
- Type
- string
sql(template, values, filtersopt) → {string}
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
template |
string | The SQL template with optional replacement tokens | |
values |
Object.<string, Object.<string, string>> | The replacement values | |
filters |
Object.<string, string> |
<optional> |
The filters to append to the WHERE clause (i.e. ) |
- Source:
Returns:
The SQL statement
- Type
- string
Example
var template = "SELECT * FROM bike WHERE ${where}";
var values = {
color: {value: "red"},
gear: {low: 10, high: 18}
};
var filters = {
color: "color = '${value}'",
gear: "gear BETWEEN ${low} AND ${high}"
};
var sqlTmpl = nyc.carto.SqlTemplate();
sqlTmpl.sql(template, values, filters);
//Returns "SELECT * FROM bike WHERE color = 'red' AND gear BETWEEN 10 AND 18"