test-custom-query-format.js 650 B

12345678910111213141516171819
  1. var Mysql = require('../../../');
  2. var common = require('../../common');
  3. var connection = common.createConnection();
  4. var assert = require('assert');
  5. connection.config.queryFormat = function (query, values, tz) {
  6. if (!values) return query;
  7. return query.replace(/\:(\w+)/g, function (txt, key) {
  8. if (values.hasOwnProperty(key)) {
  9. return this.escape(values[key]);
  10. }
  11. return txt;
  12. }.bind(this));
  13. };
  14. assert.equal(connection.format("SELECT :a1, :a2", { a1: 1, a2: 'two' }), "SELECT 1, 'two'");
  15. assert.equal(connection.format("SELECT :a1", []), "SELECT :a1");
  16. assert.equal(connection.format("SELECT :a1"), "SELECT :a1");