new SQL(queryopt, …bindingopt)
This is a tiny class to build SQL statement.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
query |
string |
<optional> |
SQL statement such like: "SELECT * FROM table WHERE id = ?" |
binding |
string |
<optional> <repeatable> |
value(s) to fulfill placeholder(s) of the statement |
- Source:
Example
var SQL = require("sql-statement");
var id = "123";
var sql = SQL("SELECT * FROM users WHERE id = ?", id);
console.log("SQL: " + sql); // => SQL: SELECT * FROM users WHERE id = '123'
var key = "group_id";
var grp = [45, 67, 89].join(", ");
sql.append("AND ?? IN (???)", key, grp);
console.log("SQL: " + sql); // => SQL: SELECT * FROM users WHERE id = '123' AND `group_id` IN (45, 67, 89)
Methods
append(query, …bindingopt) → {SQL}
This appends a piece of SQL statement at the end.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
query |
string | SQL | SQL statement piece such like: "WHERE id = ?" | |
binding |
string |
<optional> <repeatable> |
value(s) to fulfill placeholder(s) of the statement |
- Source:
Returns:
SQL object itself for method chaining
- Type
- SQL
bindings() → {Array}
This returns the array of values for placeholders.
- Source:
Returns:
values for placeholders
- Type
- Array
prepend(query, …bindingopt) → {SQL}
This inserts a piece of SQL statement at the beginning.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
query |
string | SQL | SQL statement piece such like: "WHERE id = ?" | |
binding |
string |
<optional> <repeatable> |
value(s) to fulfill placeholder(s) of the statement |
- Source:
Returns:
SQL object itself for method chaining
- Type
- SQL
query() → {string}
This returns the string of SQL statement unformatted.
- Source:
Returns:
SQL statement unformatted
- Type
- string
toString() → {string}
This returns the string of SQL statement which placeholders are fulfilled with values bound.
- Source:
Returns:
SQL statement formatted
- Type
- string