@@ -70,6 +70,8 @@ Different value types are escaped differently, here is how:
7070* Arrays are turned into list, e.g. ` ['a', 'b'] ` turns into ` 'a', 'b' `
7171* Nested arrays are turned into grouped lists (for bulk inserts), e.g. `[[ 'a',
7272 'b'] , [ 'c', 'd']] ` turns into ` ('a', 'b'), ('c', 'd')`
73+ * Objects that have a ` toSqlString ` method will have ` .toSqlString() ` called
74+ and the returned value is used as the raw SQL.
7375* Objects are turned into ` key = 'val' ` pairs for each enumerable property on
7476 the object. If the property's value is a function, it is skipped; if the
7577 property's value is an object, toString() is called on it and the returned
@@ -87,6 +89,14 @@ var sql = SqlString.format('INSERT INTO posts SET ?', post);
8789console .log (sql); // INSERT INTO posts SET `id` = 1, `title` = 'Hello MySQL'
8890```
8991
92+ And the ` toSqlString ` method allows you to form complex queries with functions:
93+
94+ ``` js
95+ var CURRENT_TIMESTAMP = { toSqlString : function () { return ' CURRENT_TIMESTAMP()' ; } };
96+ var sql = SqlString .format (' UPDATE posts SET modified = ? WHERE id = ?' , [CURRENT_TIMESTAMP , 42 ]);
97+ console .log (sql); // UPDATE posts SET modified = CURRENT_TIMESTAMP() WHERE id = 42
98+ ```
99+
90100If you feel the need to escape queries by yourself, you can also use the escaping
91101function directly:
92102
0 commit comments