query.js 1007 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. var Utils = require("../../utils")
  2. , AbstractQuery = require('../abstract/query')
  3. module.exports = (function() {
  4. var Query = function(client, sequelize, callee, options) {
  5. this.client = client
  6. this.callee = callee
  7. this.sequelize = sequelize
  8. this.options = Utils._.extend({
  9. logging: console.log,
  10. plain: false,
  11. raw: false
  12. }, options || {})
  13. this.checkLoggingOption()
  14. }
  15. Utils.inherit(Query, AbstractQuery)
  16. Query.prototype.run = function(sql) {
  17. this.sql = sql
  18. if (this.options.logging !== false) {
  19. this.options.logging('Executing (' + this.options.uuid + '): ' + this.sql)
  20. }
  21. this.client.query(this.sql, function(err, results, fields) {
  22. this.emit('sql', this.sql)
  23. if (err) {
  24. err.sql = sql
  25. this.emit('error', err, this.callee)
  26. } else {
  27. this.emit('success', this.formatResults(results))
  28. }
  29. }.bind(this)).setMaxListeners(100)
  30. return this
  31. }
  32. return Query
  33. })()