mysql.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // This file contains additional Squel commands for use with MySQL
  2. squel.flavours['mysql'] = function(_squel) {
  3. let cls = _squel.cls;
  4. // ON DUPLICATE KEY UPDATE ...
  5. cls.MysqlOnDuplicateKeyUpdateBlock = class extends cls.AbstractSetFieldBlock {
  6. onDupUpdate (field, value, options) {
  7. this._set(field, value, options);
  8. }
  9. _toParamString (options = {}) {
  10. let totalStr = "",
  11. totalValues = [];
  12. for (let i = 0; i < this._fields.length; ++i) {
  13. totalStr = _pad(totalStr, ', ');
  14. let field = this._fields[i];
  15. let value = this._values[0][i];
  16. let valueOptions = this._valueOptions[0][i];
  17. // e.g. if field is an expression such as: count = count + 1
  18. if (typeof value === 'undefined') {
  19. totalStr += field;
  20. } else {
  21. let ret = this._buildString(
  22. `${field} = ${this.options.parameterCharacter}`,
  23. [value],
  24. {
  25. buildParameterized: options.buildParameterized,
  26. formattingOptions: valueOptions,
  27. }
  28. );
  29. totalStr += ret.text;
  30. totalValues.push(...ret.values);
  31. }
  32. }
  33. return {
  34. text: !totalStr.length ? "" : `ON DUPLICATE KEY UPDATE ${totalStr}`,
  35. values: totalValues,
  36. };
  37. }
  38. }
  39. // INSERT query builder.
  40. cls.Insert = class extends cls.QueryBuilder {
  41. constructor (options, blocks = null) {
  42. blocks = blocks || [
  43. new cls.StringBlock(options, 'INSERT'),
  44. new cls.IntoTableBlock(options),
  45. new cls.InsertFieldValueBlock(options),
  46. new cls.InsertFieldsFromQueryBlock(options),
  47. new cls.MysqlOnDuplicateKeyUpdateBlock(options),
  48. ];
  49. super(options, blocks);
  50. }
  51. }
  52. };