Skip to content

Commit 2c0947a

Browse files
committed
fixed cursor related bugs:
1) last chunk won't emit when not aligned 2) err.query defined second time causing unwanted crash in some scenarios
1 parent f46906f commit 2c0947a

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

lib/backend.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ function Backend({
8888
}
8989
}
9090

91-
oncomplete()
91+
oncomplete(backend.query.result)
9292
}
9393

9494
/* c8 ignore next 3 */

lib/connection.js

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,22 @@ function Connection(options = {}) {
7373
})
7474
}
7575

76-
function oncomplete() {
77-
backend.query.cursor && socket.write(frontend.Close())
76+
function oncomplete(result) {
77+
if (backend.query.cursor) {
78+
if (result.length) {
79+
// emit final chunk to cursor handler
80+
new Promise(r => r(backend.query.cursor(
81+
backend.query.cursor.rows === 1 ? result[0] : result
82+
))).then(() => {
83+
socket.write(frontend.Close())
84+
}).catch(err => {
85+
rejectQuery(backend.query, err)
86+
socket.write(frontend.Close())
87+
})
88+
} else {
89+
socket.write(frontend.Close())
90+
}
91+
}
7892
}
7993

8094
function onparse() {
@@ -207,14 +221,18 @@ function Connection(options = {}) {
207221
if (query.origin) {
208222
err.stack += query.origin.replace(/.*\n/, '\n')
209223
}
210-
Object.defineProperty(err, 'query', {
211-
value: query.str,
212-
enumerable: !!options.debug
213-
})
214-
Object.defineProperty(err, 'parameters', {
215-
value: query.args,
216-
enumerable: !!options.debug
217-
})
224+
if (!err.query) {
225+
Object.defineProperty(err, 'query', {
226+
value: query.str,
227+
enumerable: !!options.debug
228+
})
229+
}
230+
if (!err.parameters) {
231+
Object.defineProperty(err, 'parameters', {
232+
value: query.args,
233+
enumerable: !!options.debug
234+
})
235+
}
218236
query.reject(err)
219237
}
220238

0 commit comments

Comments
 (0)