sql-string.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. 'use strict';
  2. const dataTypes = require('./data-types');
  3. const { logger } = require('./utils/logger');
  4. function arrayToList(array, timeZone, dialect, format) {
  5. return array.reduce((sql, val, i) => {
  6. if (i !== 0) {
  7. sql += ', ';
  8. }
  9. if (Array.isArray(val)) {
  10. sql += `(${arrayToList(val, timeZone, dialect, format)})`;
  11. } else {
  12. sql += escape(val, timeZone, dialect, format);
  13. }
  14. return sql;
  15. }, '');
  16. }
  17. exports.arrayToList = arrayToList;
  18. function escape(val, timeZone, dialect, format) {
  19. let prependN = false;
  20. if (val === undefined || val === null) {
  21. return 'NULL';
  22. }
  23. switch (typeof val) {
  24. case 'boolean':
  25. // SQLite doesn't have true/false support. MySQL aliases true/false to 1/0
  26. // for us. Postgres actually has a boolean type with true/false literals,
  27. // but sequelize doesn't use it yet.
  28. if (dialect === 'sqlite' || dialect === 'mssql') {
  29. return +!!val;
  30. }
  31. return (!!val).toString();
  32. case 'number':
  33. return val.toString();
  34. case 'string':
  35. // In mssql, prepend N to all quoted vals which are originally a string (for
  36. // unicode compatibility)
  37. prependN = dialect === 'mssql';
  38. break;
  39. }
  40. if (val instanceof Date) {
  41. val = dataTypes[dialect].DATE.prototype.stringify(val, { timezone: timeZone });
  42. }
  43. if (Buffer.isBuffer(val)) {
  44. if (dataTypes[dialect].BLOB) {
  45. return dataTypes[dialect].BLOB.prototype.stringify(val);
  46. }
  47. return dataTypes.BLOB.prototype.stringify(val);
  48. }
  49. if (Array.isArray(val)) {
  50. const partialEscape = escVal => escape(escVal, timeZone, dialect, format);
  51. if (dialect === 'postgres' && !format) {
  52. return dataTypes.ARRAY.prototype.stringify(val, { escape: partialEscape });
  53. }
  54. return arrayToList(val, timeZone, dialect, format);
  55. }
  56. if (!val.replace) {
  57. throw new Error(`Invalid value ${logger.inspect(val)}`);
  58. }
  59. if (dialect === 'postgres' || dialect === 'sqlite' || dialect === 'mssql') {
  60. // http://www.postgresql.org/docs/8.2/static/sql-syntax-lexical.html#SQL-SYNTAX-STRINGS
  61. // http://stackoverflow.com/q/603572/130598
  62. val = val.replace(/'/g, "''");
  63. if (dialect === 'postgres') {
  64. // null character is not allowed in Postgres
  65. val = val.replace(/\0/g, '\\0');
  66. }
  67. } else {
  68. // eslint-disable-next-line no-control-regex
  69. val = val.replace(/[\0\n\r\b\t\\'"\x1a]/g, s => {
  70. switch (s) {
  71. case '\0': return '\\0';
  72. case '\n': return '\\n';
  73. case '\r': return '\\r';
  74. case '\b': return '\\b';
  75. case '\t': return '\\t';
  76. case '\x1a': return '\\Z';
  77. default: return `\\${s}`;
  78. }
  79. });
  80. }
  81. return `${(prependN ? "N'" : "'") + val}'`;
  82. }
  83. exports.escape = escape;
  84. function format(sql, values, timeZone, dialect) {
  85. values = [].concat(values);
  86. if (typeof sql !== 'string') {
  87. throw new Error(`Invalid SQL string provided: ${sql}`);
  88. }
  89. return sql.replace(/\?/g, match => {
  90. if (!values.length) {
  91. return match;
  92. }
  93. return escape(values.shift(), timeZone, dialect, true);
  94. });
  95. }
  96. exports.format = format;
  97. function formatNamedParameters(sql, values, timeZone, dialect) {
  98. return sql.replace(/:+(?!\d)(\w+)/g, (value, key) => {
  99. if ('postgres' === dialect && '::' === value.slice(0, 2)) {
  100. return value;
  101. }
  102. if (values[key] !== undefined) {
  103. return escape(values[key], timeZone, dialect, true);
  104. }
  105. throw new Error(`Named parameter "${value}" has no value in the given object.`);
  106. });
  107. }
  108. exports.formatNamedParameters = formatNamedParameters;