1
1
var Result = require ( './pg' ) . Result
2
2
var prepare = require ( './pg' ) . prepareValue
3
+ var EventEmitter = require ( 'events' ) . EventEmitter ;
4
+ var util = require ( 'util' ) ;
5
+
6
+ function Cursor ( text , values ) {
7
+ EventEmitter . call ( this ) ;
3
8
4
- var Cursor = function ( text , values ) {
5
9
this . text = text
6
10
this . values = values ? values . map ( prepare ) : null
7
11
this . connection = null
@@ -12,6 +16,8 @@ var Cursor = function(text, values) {
12
16
this . _rows = null
13
17
}
14
18
19
+ util . inherits ( Cursor , EventEmitter )
20
+
15
21
Cursor . prototype . submit = function ( connection ) {
16
22
this . connection = connection
17
23
@@ -58,6 +64,7 @@ Cursor.prototype.handleRowDescription = function(msg) {
58
64
59
65
Cursor . prototype . handleDataRow = function ( msg ) {
60
66
var row = this . _result . parseRow ( msg . fields )
67
+ this . emit ( 'row' , row , this . _result )
61
68
this . _rows . push ( row )
62
69
}
63
70
@@ -87,6 +94,7 @@ Cursor.prototype.handlePortalSuspended = function() {
87
94
88
95
Cursor . prototype . handleReadyForQuery = function ( ) {
89
96
this . _sendRows ( )
97
+ this . emit ( 'end' , this . _result )
90
98
this . state = 'done'
91
99
}
92
100
@@ -107,6 +115,7 @@ Cursor.prototype.handleError = function(msg) {
107
115
for ( var i = 0 ; i < this . _queue . length ; i ++ ) {
108
116
this . _queue . pop ( ) [ 1 ] ( msg )
109
117
}
118
+ this . emit ( 'error' , msg )
110
119
//call sync to keep this connection from hanging
111
120
this . connection . sync ( )
112
121
}
0 commit comments