Sequelize

declaration

Utils

Utils
    var Utils         = require("../../utils")
      , AbstractQuery = require('../abstract/query')
    
    
    module.exports = (function() {
      var Query = function(database, sequelize, callee, options) {
        this.database = database
        this.sequelize = sequelize
        this.callee = callee
        this.options = Utils._.extend({
          logging: console.log,
          plain: false,
          raw: false
        }, options || {})
    
        if(this.options.logging === true) {
          console.log('DEPRECATION WARNING: The logging-option should be either a function or false. Default: console.log')
          this.options.logging = console.log
        }
    
        if(this.options.logging == console.log) {
          // using just console.log will break in node < 0.6
          this.options.logging = function(s) { console.log(s) }
        }
      }
      Utils.inherit(Query, AbstractQuery)
    
      Query.prototype.run = function(sql) {
        var self = this
    
        this.sql = sql
    
        if(this.options.logging !== false) {
          this.options.logging('Executing: ' + this.sql)
        }
    
        var columnTypes = {};
        this.database.serialize(function() {
          var executeSql = function() {
            self.database[databaseMethod](self.sql, function(err, results) {
               //allow clients to listen to sql to do their own logging or whatnot
              self.emit('sql', self.sql)
              this.columnTypes = columnTypes;
              err ? onFailure.call(self, err) : onSuccess.call(self, results, this)
            })
          };
    
          var isInsertCommand = (self.sql.toLowerCase().indexOf('insert') == 0)
            , isUpdateCommand = (self.sql.toLowerCase().indexOf('update') == 0)
            , databaseMethod  = (isInsertCommand || isUpdateCommand) ? 'run' : 'all'
          if (databaseMethod === 'all' && /select\s.*?\sfrom\s+([^ ;]+)/i.test(self.sql)) {
            var tableName = RegExp.$1;
            if (tableName !== 'sqlite_master') {
              // get the column types
              self.database.all("PRAGMA table_info(" + tableName + ")", function(err, results) {
                if (!err) {
                  for (var i=0, l=results.length; i