source: node_modules/pg/lib/result.js@ 2d1ec46

main
Last change on this file since 2d1ec46 was 62b2964, checked in by Klimentina Efremova <klimentina08642@…>, 2 weeks ago

Project Handcraft Marketplace

  • Property mode set to 100644
File size: 2.8 KB
RevLine 
[62b2964]1'use strict'
2
3const types = require('pg-types')
4
5const matchRegexp = /^([A-Za-z]+)(?: (\d+))?(?: (\d+))?/
6
7// result object returned from query
8// in the 'end' event and also
9// passed as second argument to provided callback
10class Result {
11 constructor(rowMode, types) {
12 this.command = null
13 this.rowCount = null
14 this.oid = null
15 this.rows = []
16 this.fields = []
17 this._parsers = undefined
18 this._types = types
19 this.RowCtor = null
20 this.rowAsArray = rowMode === 'array'
21 if (this.rowAsArray) {
22 this.parseRow = this._parseRowAsArray
23 }
24 this._prebuiltEmptyResultObject = null
25 }
26
27 // adds a command complete message
28 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 }
48 }
49
50 _parseRowAsArray(rowData) {
51 const row = new Array(rowData.length)
52 for (let i = 0, len = rowData.length; i < len; i++) {
53 const rawValue = rowData[i]
54 if (rawValue !== null) {
55 row[i] = this._parsers[i](rawValue)
56 } else {
57 row[i] = null
58 }
59 }
60 return row
61 }
62
63 parseRow(rowData) {
64 const row = { ...this._prebuiltEmptyResultObject }
65 for (let i = 0, len = rowData.length; i < len; i++) {
66 const rawValue = rowData[i]
67 const field = this.fields[i].name
68 if (rawValue !== null) {
69 const v = this.fields[i].format === 'binary' ? Buffer.from(rawValue) : rawValue
70 row[field] = this._parsers[i](v)
71 } else {
72 row[field] = null
73 }
74 }
75 return row
76 }
77
78 addRow(row) {
79 this.rows.push(row)
80 }
81
82 addFields(fieldDescriptions) {
83 // clears field definitions
84 // multiple query statements in 1 action can result in multiple sets
85 // of rowDescriptions...eg: 'select NOW(); select 1::int;'
86 // you need to reset the fields
87 this.fields = fieldDescriptions
88 if (this.fields.length) {
89 this._parsers = new Array(fieldDescriptions.length)
90 }
91
92 const row = Object.create(null)
93
94 for (let i = 0; i < fieldDescriptions.length; i++) {
95 const desc = fieldDescriptions[i]
96 row[desc.name] = null
97
98 if (this._types) {
99 this._parsers[i] = this._types.getTypeParser(desc.dataTypeID, desc.format || 'text')
100 } else {
101 this._parsers[i] = types.getTypeParser(desc.dataTypeID, desc.format || 'text')
102 }
103 }
104
105 this._prebuiltEmptyResultObject = { ...row }
106 }
107}
108
109module.exports = Result
Note: See TracBrowser for help on using the repository browser.