forked from brianc/node-postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-result.js
More file actions
81 lines (70 loc) · 1.98 KB
/
build-result.js
File metadata and controls
81 lines (70 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
'use strict'
class Result {
constructor(types, arrayMode) {
this._types = types
this._arrayMode = arrayMode
this.command = undefined
this.rowCount = undefined
this.fields = []
this.rows = []
this._prebuiltEmptyResultObject = null
this._parsers = []
}
consumeCommand(pq) {
this.command = pq.cmdStatus().split(' ')[0]
this.rowCount = parseInt(pq.cmdTuples(), 10)
}
consumeFields(pq) {
const nfields = pq.nfields()
this.fields = new Array(nfields)
const row = {}
this._parsers = new Array(nfields)
for (let x = 0; x < nfields; x++) {
const name = pq.fname(x)
row[name] = null
const typeId = pq.ftype(x)
this.fields[x] = {
name: name,
dataTypeID: typeId,
}
this._parsers[x] = this._types.getTypeParser(typeId)
}
this._prebuiltEmptyResultObject = { ...row }
}
consumeRows(pq) {
const tupleCount = pq.ntuples()
this.rows = new Array(tupleCount)
for (let i = 0; i < tupleCount; i++) {
this.rows[i] = this._arrayMode ? this.consumeRowAsArray(pq, i) : this.consumeRowAsObject(pq, i)
}
}
consumeRowAsObject(pq, rowIndex) {
const row = { ...this._prebuiltEmptyResultObject }
for (let j = 0; j < this.fields.length; j++) {
row[this.fields[j].name] = this.readValue(pq, rowIndex, j)
}
return row
}
consumeRowAsArray(pq, rowIndex) {
const row = new Array(this.fields.length)
for (let j = 0; j < this.fields.length; j++) {
row[j] = this.readValue(pq, rowIndex, j)
}
return row
}
readValue(pq, rowIndex, colIndex) {
const rawValue = pq.getvalue(rowIndex, colIndex)
if (rawValue === '' && pq.getisnull(rowIndex, colIndex)) {
return null
}
return this._parsers[colIndex](rawValue)
}
}
function buildResult(pq, types, arrayMode) {
const result = new Result(types, arrayMode)
result.consumeCommand(pq)
result.consumeFields(pq)
result.consumeRows(pq)
return result
}
module.exports = buildResult