@@ -3,10 +3,18 @@ var util = require('util');
33
44var types = require ( __dirname + '/../types' ) ;
55var utils = require ( __dirname + '/../utils' ) ;
6+ var Result = require ( __dirname + '/../result' ) ;
67
78//event emitter proxy
89var 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
1018 if ( typeof text == 'object' ) {
1119 this . text = text . text ;
1220 this . values = text . values ;
@@ -26,17 +34,13 @@ var NativeQuery = function(text, values, callback) {
2634 this . callback = values ;
2735 }
2836 }
29- if ( this . callback ) {
30- this . rows = [ ] ;
31- }
37+ this . result = new Result ( ) ;
3238 //normalize values
3339 if ( this . values ) {
3440 for ( var i = 0 , len = this . values . length ; i < len ; i ++ ) {
3541 this . values [ i ] = utils . prepareValue ( this . values [ i ] ) ;
3642 }
3743 }
38-
39- EventEmitter . call ( this ) ;
4044} ;
4145
4246util . inherits ( NativeQuery , EventEmitter ) ;
@@ -55,9 +59,9 @@ var mapRowData = function(row) {
5559p . handleRow = function ( rowData ) {
5660 var row = mapRowData ( rowData ) ;
5761 if ( this . callback ) {
58- this . rows . push ( row ) ;
62+ this . result . addRow ( row ) ;
5963 }
60- this . emit ( 'row' , row ) ;
64+ this . emit ( 'row' , row , this . result ) ;
6165} ;
6266
6367p . handleError = function ( error ) {
@@ -71,8 +75,9 @@ p.handleError = function(error) {
7175
7276p . handleReadyForQuery = function ( meta ) {
7377 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 ) ;
7681 }
7782 this . emit ( 'end' ) ;
7883} ;
0 commit comments