PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/result.js

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