compiler.js 3.0 KB

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