query.js
1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
var Utils = require("../../utils")
, AbstractQuery = require('../abstract/query')
module.exports = (function() {
var Query = function(client, sequelize, callee, options) {
this.client = client
this.callee = callee
this.sequelize = sequelize
this.options = Utils._.extend({
logging: console.log,
plain: false,
raw: false
}, options || {})
this.checkLoggingOption()
}
Utils.inherit(Query, AbstractQuery)
Query.prototype.run = function(sql) {
this.sql = sql
if (this.options.logging !== false) {
this.options.logging('Executing: ' + this.sql)
}
var resultSet = [];
this.client.query(this.sql)
.on('result', function(results) {
results.on('row', function(row) {
resultSet.push(row);
})
.on('error', function(err) {
this.emit('error', err, this.callee)
})
.on('end', function(info) {
//console.log(info)
});
})
.on('error', function(err) {
console.log( stack )
//this.emit('error', err, this.callee)
})
.on('end', function() {
this.emit('sql', this.sql)
this.emit('success', this.formatResults(resultSet))
}.bind(this))
return this
}
return Query
})()