@@ -707,6 +707,8 @@ Different value types are escaped differently, here is how:
707
707
* Arrays are turned into list, e.g. ` ['a', 'b'] ` turns into ` 'a', 'b' `
708
708
* Nested arrays are turned into grouped lists (for bulk inserts), e.g. `[[ 'a',
709
709
'b'] , [ 'c', 'd']] ` turns into ` ('a', 'b'), ('c', 'd')`
710
+ * Objects that have a ` toSqlString ` method will have ` .toSqlString() ` called
711
+ and the returned value is used as the raw SQL.
710
712
* Objects are turned into ` key = 'val' ` pairs for each enumerable property on
711
713
the object. If the property's value is a function, it is skipped; if the
712
714
property's value is an object, toString() is called on it and the returned
@@ -725,7 +727,14 @@ var query = connection.query('INSERT INTO posts SET ?', post, function (error, r
725
727
// Neat!
726
728
});
727
729
console .log (query .sql ); // INSERT INTO posts SET `id` = 1, `title` = 'Hello MySQL'
730
+ ```
731
+
732
+ And the ` toSqlString ` method allows you to form complex queries with functions:
728
733
734
+ ``` js
735
+ var CURRENT_TIMESTAMP = { toSqlString : function () { return ' CURRENT_TIMESTAMP()' ; } };
736
+ var sql = mysql .format (' UPDATE posts SET modified = ? WHERE id = ?' , [CURRENT_TIMESTAMP , 42 ]);
737
+ console .log (sql); // UPDATE posts SET modified = CURRENT_TIMESTAMP() WHERE id = 42
729
738
```
730
739
731
740
If you feel the need to escape queries by yourself, you can also use the escaping
0 commit comments