@@ -3,10 +3,18 @@ var util = require('util');
3
3
4
4
var types = require ( __dirname + '/../types' ) ;
5
5
var utils = require ( __dirname + '/../utils' ) ;
6
+ var Result = require ( __dirname + '/../result' ) ;
6
7
7
8
//event emitter proxy
8
9
var NativeQuery = function ( text , values , callback ) {
9
- //TODO there are better ways to detect overloads
10
+ EventEmitter . call ( this ) ;
11
+
12
+ this . text = null ;
13
+ this . values = null ;
14
+ this . callback = null ;
15
+ this . name = null ;
16
+
17
+ //allow 'config object' as first parameter
10
18
if ( typeof text == 'object' ) {
11
19
this . text = text . text ;
12
20
this . values = text . values ;
@@ -26,17 +34,13 @@ var NativeQuery = function(text, values, callback) {
26
34
this . callback = values ;
27
35
}
28
36
}
29
- if ( this . callback ) {
30
- this . rows = [ ] ;
31
- }
37
+ this . result = new Result ( ) ;
32
38
//normalize values
33
39
if ( this . values ) {
34
40
for ( var i = 0 , len = this . values . length ; i < len ; i ++ ) {
35
41
this . values [ i ] = utils . prepareValue ( this . values [ i ] ) ;
36
42
}
37
43
}
38
-
39
- EventEmitter . call ( this ) ;
40
44
} ;
41
45
42
46
util . inherits ( NativeQuery , EventEmitter ) ;
@@ -55,9 +59,9 @@ var mapRowData = function(row) {
55
59
p . handleRow = function ( rowData ) {
56
60
var row = mapRowData ( rowData ) ;
57
61
if ( this . callback ) {
58
- this . rows . push ( row ) ;
62
+ this . result . addRow ( row ) ;
59
63
}
60
- this . emit ( 'row' , row ) ;
64
+ this . emit ( 'row' , row , this . result ) ;
61
65
} ;
62
66
63
67
p . handleError = function ( error ) {
@@ -71,8 +75,9 @@ p.handleError = function(error) {
71
75
72
76
p . handleReadyForQuery = function ( meta ) {
73
77
if ( this . callback ) {
74
- ( meta || { } ) . rows = this . rows ;
75
- this . callback ( null , meta ) ;
78
+ this . result . command = meta . command . split ( ' ' ) [ 0 ] ;
79
+ this . result . rowCount = parseInt ( meta . value ) ;
80
+ this . callback ( null , this . result ) ;
76
81
}
77
82
this . emit ( 'end' ) ;
78
83
} ;
0 commit comments