@@ -9,9 +9,10 @@ const matchRegexp = /^([A-Za-z]+)(?: (\d+))?(?: (\d+))?/
9
9
// passed as second argument to provided callback
10
10
class Result {
11
11
constructor ( rowMode , types ) {
12
- this . command = null
13
- this . rowCount = null
14
- this . oid = null
12
+ this . _command = undefined
13
+ this . _rowCount = undefined
14
+ this . _oid = undefined
15
+ this . _commandCompleteMsg = undefined
15
16
this . rows = [ ]
16
17
this . fields = [ ]
17
18
this . _parsers = undefined
@@ -26,25 +27,10 @@ class Result {
26
27
27
28
// adds a command complete message
28
29
addCommandComplete ( msg ) {
29
- let match
30
- if ( msg . text ) {
31
- // pure javascript
32
- match = matchRegexp . exec ( msg . text )
33
- } else {
34
- // native bindings
35
- match = matchRegexp . exec ( msg . command )
36
- }
37
- if ( match ) {
38
- this . command = match [ 1 ]
39
- if ( match [ 3 ] ) {
40
- // COMMAND OID ROWS
41
- this . oid = parseInt ( match [ 2 ] , 10 )
42
- this . rowCount = parseInt ( match [ 3 ] , 10 )
43
- } else if ( match [ 2 ] ) {
44
- // COMMAND ROWS
45
- this . rowCount = parseInt ( match [ 2 ] , 10 )
46
- }
47
- }
30
+ this . _commandCompleteMsg = msg
31
+ this . _command = undefined
32
+ this . _rowCount = undefined
33
+ this . _oid = undefined
48
34
}
49
35
50
36
_parseRowAsArray ( rowData ) {
@@ -104,6 +90,52 @@ class Result {
104
90
105
91
this . _prebuiltEmptyResultObject = { ...row }
106
92
}
93
+ get command ( ) {
94
+ this . _parseCommandCompleteIfNeeded ( )
95
+ return this . _command
96
+ }
97
+
98
+ get rowCount ( ) {
99
+ this . _parseCommandCompleteIfNeeded ( )
100
+ return this . _rowCount
101
+ }
102
+
103
+ get oid ( ) {
104
+ this . _parseCommandCompleteIfNeeded ( )
105
+ return this . _oid
106
+ }
107
+
108
+ _parseCommandCompleteIfNeeded ( ) {
109
+ if ( this . _command !== undefined || ! this . _commandCompleteMsg ) {
110
+ return
111
+ }
112
+ let match
113
+ const msg = this . _commandCompleteMsg
114
+ if ( msg . text ) {
115
+ match = matchRegexp . exec ( msg . text )
116
+ } else {
117
+ match = matchRegexp . exec ( msg . command )
118
+ }
119
+ if ( match ) {
120
+ this . _command = match [ 1 ]
121
+ if ( match [ 3 ] ) {
122
+ // COMMAND OID ROWS
123
+ this . _oid = parseInt ( match [ 2 ] , 10 )
124
+ this . _rowCount = parseInt ( match [ 3 ] , 10 )
125
+ } else if ( match [ 2 ] ) {
126
+ // COMMAND ROWS
127
+ this . _oid = null
128
+ this . _rowCount = parseInt ( match [ 2 ] , 10 )
129
+ } else {
130
+ this . _oid = null
131
+ this . _rowCount = null
132
+ }
133
+ } else {
134
+ this . _command = null
135
+ this . _oid = null
136
+ this . _rowCount = null
137
+ }
138
+ }
107
139
}
108
140
109
141
module . exports = Result
0 commit comments