PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/result.js

https://github.com/lordsngrmkr/node-postgres
JavaScript | 102 lines | 74 code | 9 blank | 19 comment | 15 complexity | f7c710f33e40fee22b38b10aafbc4318 MD5 | raw file
  1. var types = require('pg-types');
  2. //result object returned from query
  3. //in the 'end' event and also
  4. //passed as second argument to provided callback
  5. var Result = function(rowMode) {
  6. this.command = null;
  7. this.rowCount = null;
  8. this.oid = null;
  9. this.rows = [];
  10. this.fields = [];
  11. this._parsers = [];
  12. this.RowCtor = null;
  13. this.rowAsArray = rowMode == "array";
  14. if(this.rowAsArray) {
  15. this.parseRow = this._parseRowAsArray;
  16. }
  17. };
  18. var matchRegexp = /([A-Za-z]+) ?(\d+ )?(\d+)?/;
  19. //adds a command complete message
  20. Result.prototype.addCommandComplete = function(msg) {
  21. var match;
  22. if(msg.text) {
  23. //pure javascript
  24. match = matchRegexp.exec(msg.text);
  25. } else {
  26. //native bindings
  27. match = matchRegexp.exec(msg.command);
  28. }
  29. if(match) {
  30. this.command = match[1];
  31. //match 3 will only be existing on insert commands
  32. if(match[3]) {
  33. //msg.value is from native bindings
  34. this.rowCount = parseInt(match[3] || msg.value, 10);
  35. this.oid = parseInt(match[2], 10);
  36. } else {
  37. this.rowCount = parseInt(match[2], 10);
  38. }
  39. }
  40. };
  41. Result.prototype._parseRowAsArray = function(rowData) {
  42. var row = [];
  43. for(var i = 0, len = rowData.length; i < len; i++) {
  44. var rawValue = rowData[i];
  45. if(rawValue !== null) {
  46. row.push(this._parsers[i](rawValue));
  47. } else {
  48. row.push(null);
  49. }
  50. }
  51. return row;
  52. };
  53. //rowData is an array of text or binary values
  54. //this turns the row into a JavaScript object
  55. Result.prototype.parseRow = function(rowData) {
  56. return new this.RowCtor(this._parsers, rowData);
  57. };
  58. Result.prototype.addRow = function(row) {
  59. this.rows.push(row);
  60. };
  61. var inlineParser = function(fieldName, i) {
  62. return "\nthis['" +
  63. //fields containing single quotes will break
  64. //the evaluated javascript unless they are escaped
  65. //see https://github.com/brianc/node-postgres/issues/507
  66. fieldName.replace("'", "\\'") +
  67. "'] = " +
  68. "rowData[" + i + "] == null ? null : parsers[" + i + "](rowData[" + i + "]);";
  69. };
  70. Result.prototype.addFields = function(fieldDescriptions) {
  71. //clears field definitions
  72. //multiple query statements in 1 action can result in multiple sets
  73. //of rowDescriptions...eg: 'select NOW(); select 1::int;'
  74. //you need to reset the fields
  75. if(this.fields.length) {
  76. this.fields = [];
  77. this._parsers = [];
  78. }
  79. var ctorBody = "";
  80. for(var i = 0; i < fieldDescriptions.length; i++) {
  81. var desc = fieldDescriptions[i];
  82. this.fields.push(desc);
  83. var parser = types.getTypeParser(desc.dataTypeID, desc.format || 'text');
  84. this._parsers.push(parser);
  85. //this is some craziness to compile the row result parsing
  86. //results in ~60% speedup on large query result sets
  87. ctorBody += inlineParser(desc.name, i);
  88. }
  89. if(!this.rowAsArray) {
  90. this.RowCtor = Function("parsers", "rowData", ctorBody);
  91. }
  92. };
  93. module.exports = Result;