compiler.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _assign2 = require('lodash/assign');
  4. var _assign3 = _interopRequireDefault(_assign2);
  5. var _inherits = require('inherits');
  6. var _inherits2 = _interopRequireDefault(_inherits);
  7. var _compiler = require('../../../query/compiler');
  8. var _compiler2 = _interopRequireDefault(_compiler);
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. // MySQL Query Compiler
  11. // ------
  12. function QueryCompiler_MySQL(client, builder) {
  13. _compiler2.default.call(this, client, builder);
  14. }
  15. (0, _inherits2.default)(QueryCompiler_MySQL, _compiler2.default);
  16. (0, _assign3.default)(QueryCompiler_MySQL.prototype, {
  17. _emptyInsertValue: '() values ()',
  18. // Update method, including joins, wheres, order & limits.
  19. update: function update() {
  20. var join = this.join();
  21. var updates = this._prepUpdate(this.single.update);
  22. var where = this.where();
  23. var order = this.order();
  24. var limit = this.limit();
  25. return 'update ' + this.tableName + (join ? ' ' + join : '') + ' set ' + updates.join(', ') + (where ? ' ' + where : '') + (order ? ' ' + order : '') + (limit ? ' ' + limit : '');
  26. },
  27. forUpdate: function forUpdate() {
  28. return 'for update';
  29. },
  30. forShare: function forShare() {
  31. return 'lock in share mode';
  32. },
  33. // Compiles a `columnInfo` query.
  34. columnInfo: function columnInfo() {
  35. var column = this.single.columnInfo;
  36. return {
  37. sql: 'select * from information_schema.columns where table_name = ? and table_schema = ?',
  38. bindings: [this.single.table, this.client.database()],
  39. output: function output(resp) {
  40. var out = resp.reduce(function (columns, val) {
  41. columns[val.COLUMN_NAME] = {
  42. defaultValue: val.COLUMN_DEFAULT,
  43. type: val.DATA_TYPE,
  44. maxLength: val.CHARACTER_MAXIMUM_LENGTH,
  45. nullable: val.IS_NULLABLE === 'YES'
  46. };
  47. return columns;
  48. }, {});
  49. return column && out[column] || out;
  50. }
  51. };
  52. },
  53. limit: function limit() {
  54. var noLimit = !this.single.limit && this.single.limit !== 0;
  55. if (noLimit && !this.single.offset) return '';
  56. // Workaround for offset only.
  57. // see: http://stackoverflow.com/questions/255517/mysql-offset-infinite-rows
  58. var limit = this.single.offset && noLimit ? '18446744073709551615' : this.formatter.parameter(this.single.limit);
  59. return 'limit ' + limit;
  60. }
  61. });
  62. // Set the QueryBuilder & QueryCompiler on the client object,
  63. // in case anyone wants to modify things to suit their own purposes.
  64. exports.default = QueryCompiler_MySQL;
  65. module.exports = exports['default'];