Skip to content

Commit 9376252

Browse files
committed
Do not parse command complete info unless accessed
1 parent 01fadd9 commit 9376252

File tree

1 file changed

+54
-22
lines changed

1 file changed

+54
-22
lines changed

packages/pg/lib/result.js

Lines changed: 54 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ const matchRegexp = /^([A-Za-z]+)(?: (\d+))?(?: (\d+))?/
99
// passed as second argument to provided callback
1010
class Result {
1111
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
1516
this.rows = []
1617
this.fields = []
1718
this._parsers = undefined
@@ -26,25 +27,10 @@ class Result {
2627

2728
// adds a command complete message
2829
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
4834
}
4935

5036
_parseRowAsArray(rowData) {
@@ -104,6 +90,52 @@ class Result {
10490

10591
this._prebuiltEmptyResultObject = { ...row }
10692
}
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+
}
107139
}
108140

109141
module.exports = Result

0 commit comments

Comments
 (0)