result.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. 'use strict'
  2. var types = require('pg-types')
  3. var matchRegexp = /^([A-Za-z]+)(?: (\d+))?(?: (\d+))?/
  4. // result object returned from query
  5. // in the 'end' event and also
  6. // passed as second argument to provided callback
  7. class Result {
  8. constructor(rowMode, types) {
  9. this.command = null
  10. this.rowCount = null
  11. this.oid = null
  12. this.rows = []
  13. this.fields = []
  14. this._parsers = undefined
  15. this._types = types
  16. this.RowCtor = null
  17. this.rowAsArray = rowMode === 'array'
  18. if (this.rowAsArray) {
  19. this.parseRow = this._parseRowAsArray
  20. }
  21. this._prebuiltEmptyResultObject = null
  22. }
  23. // adds a command complete message
  24. addCommandComplete(msg) {
  25. var match
  26. if (msg.text) {
  27. // pure javascript
  28. match = matchRegexp.exec(msg.text)
  29. } else {
  30. // native bindings
  31. match = matchRegexp.exec(msg.command)
  32. }
  33. if (match) {
  34. this.command = match[1]
  35. if (match[3]) {
  36. // COMMMAND OID ROWS
  37. this.oid = parseInt(match[2], 10)
  38. this.rowCount = parseInt(match[3], 10)
  39. } else if (match[2]) {
  40. // COMMAND ROWS
  41. this.rowCount = parseInt(match[2], 10)
  42. }
  43. }
  44. }
  45. _parseRowAsArray(rowData) {
  46. var row = new Array(rowData.length)
  47. for (var i = 0, len = rowData.length; i < len; i++) {
  48. var rawValue = rowData[i]
  49. if (rawValue !== null) {
  50. row[i] = this._parsers[i](rawValue)
  51. } else {
  52. row[i] = null
  53. }
  54. }
  55. return row
  56. }
  57. parseRow(rowData) {
  58. var row = { ... this._prebuiltEmptyResultObject }
  59. for (var i = 0, len = rowData.length; i < len; i++) {
  60. var rawValue = rowData[i]
  61. var field = this.fields[i].name
  62. if (rawValue !== null) {
  63. row[field] = this._parsers[i](rawValue)
  64. }
  65. }
  66. return row
  67. }
  68. addRow(row) {
  69. this.rows.push(row)
  70. }
  71. addFields(fieldDescriptions) {
  72. // clears field definitions
  73. // multiple query statements in 1 action can result in multiple sets
  74. // of rowDescriptions...eg: 'select NOW(); select 1::int;'
  75. // you need to reset the fields
  76. this.fields = fieldDescriptions
  77. if (this.fields.length) {
  78. this._parsers = new Array(fieldDescriptions.length)
  79. }
  80. for (var i = 0; i < fieldDescriptions.length; i++) {
  81. var desc = fieldDescriptions[i]
  82. if (this._types) {
  83. this._parsers[i] = this._types.getTypeParser(desc.dataTypeID, desc.format || 'text')
  84. } else {
  85. this._parsers[i] = types.getTypeParser(desc.dataTypeID, desc.format || 'text')
  86. }
  87. }
  88. this._createPrebuiltEmptyResultObject()
  89. }
  90. _createPrebuiltEmptyResultObject() {
  91. var row = {}
  92. for (var i = 0; i < this.fields.length; i++) {
  93. row[this.fields[i].name] = null
  94. }
  95. this._prebuiltEmptyResultObject = { ... row }
  96. }
  97. }
  98. module.exports = Result