Query.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. var Sequence = require('./Sequence');
  2. var Util = require('util');
  3. var Packets = require('../packets');
  4. var ResultSet = require('../ResultSet');
  5. var ServerStatus = require('../constants/server_status');
  6. var fs = require('fs');
  7. module.exports = Query;
  8. Util.inherits(Query, Sequence);
  9. function Query(options, callback) {
  10. Sequence.call(this, callback);
  11. this.sql = options.sql;
  12. this.values = options.values;
  13. this.typeCast = (options.typeCast === undefined)
  14. ? true
  15. : options.typeCast;
  16. this.nestTables = options.nestTables || false;
  17. this._resultSet = null;
  18. this._results = [];
  19. this._fields = [];
  20. this._index = 0;
  21. this._loadError = null;
  22. }
  23. Query.prototype.start = function() {
  24. this.emit('packet', new Packets.ComQueryPacket(this.sql));
  25. };
  26. Query.prototype.determinePacket = function(firstByte, parser) {
  27. if (firstByte === 0) {
  28. // If we have a resultSet and got one eofPacket
  29. if (this._resultSet && this._resultSet.eofPackets.length === 1) {
  30. // Then this is a RowDataPacket with an empty string in the first column.
  31. // See: https://github.com/felixge/node-mysql/issues/222
  32. } else {
  33. return;
  34. }
  35. }
  36. if (firstByte === 255) {
  37. return;
  38. }
  39. // EofPacket's are 5 bytes in mysql >= 4.1
  40. // This is the only / best way to differentiate their firstByte from a 9
  41. // byte length coded binary.
  42. if (firstByte === 0xfe && parser.packetLength() < 9) {
  43. return Packets.EofPacket;
  44. }
  45. if (!this._resultSet) {
  46. return Packets.ResultSetHeaderPacket;
  47. }
  48. return (this._resultSet.eofPackets.length === 0)
  49. ? Packets.FieldPacket
  50. : Packets.RowDataPacket;
  51. };
  52. Query.prototype['OkPacket'] = function(packet) {
  53. // try...finally for exception safety
  54. try {
  55. if (!this._callback) {
  56. this.emit('result', packet, this._index);
  57. } else {
  58. this._results.push(packet);
  59. this._fields.push(undefined);
  60. }
  61. } finally {
  62. this._index++;
  63. this._handleFinalResultPacket(packet);
  64. }
  65. };
  66. Query.prototype['ErrorPacket'] = function(packet) {
  67. var err = this._packetToError(packet);
  68. var results = (this._results.length > 0)
  69. ? this._results
  70. : undefined;
  71. var fields = (this._fields.length > 0)
  72. ? this._fields
  73. : undefined;
  74. err.index = this._index;
  75. this.end(err, results, fields);
  76. };
  77. Query.prototype['ResultSetHeaderPacket'] = function(packet) {
  78. this._resultSet = new ResultSet(packet);
  79. // used by LOAD DATA LOCAL INFILE queries
  80. if (packet.fieldCount === null) {
  81. this._sendLocalDataFile(packet.extra);
  82. }
  83. };
  84. Query.prototype['FieldPacket'] = function(packet) {
  85. this._resultSet.fieldPackets.push(packet);
  86. };
  87. Query.prototype['EofPacket'] = function(packet) {
  88. this._resultSet.eofPackets.push(packet);
  89. if (this._resultSet.eofPackets.length === 1 && !this._callback) {
  90. this.emit('fields', this._resultSet.fieldPackets, this._index);
  91. }
  92. if (this._resultSet.eofPackets.length !== 2) {
  93. return;
  94. }
  95. if (this._callback) {
  96. this._results.push(this._resultSet.rows);
  97. this._fields.push(this._resultSet.fieldPackets);
  98. }
  99. this._index++;
  100. this._resultSet = null;
  101. this._handleFinalResultPacket(packet);
  102. };
  103. Query.prototype._handleFinalResultPacket = function(packet) {
  104. if (packet.serverStatus & ServerStatus.SERVER_MORE_RESULTS_EXISTS) {
  105. return;
  106. }
  107. var results = (this._results.length > 1)
  108. ? this._results
  109. : this._results[0];
  110. var fields = (this._fields.length > 1)
  111. ? this._fields
  112. : this._fields[0];
  113. this.end(this._loadError, results, fields);
  114. };
  115. Query.prototype['RowDataPacket'] = function(packet, parser, connection) {
  116. packet.parse(parser, this._resultSet.fieldPackets, this.typeCast, this.nestTables, connection);
  117. if (this._callback) {
  118. this._resultSet.rows.push(packet);
  119. } else {
  120. this.emit('result', packet, this._index);
  121. }
  122. };
  123. Query.prototype._sendLocalDataFile = function(path) {
  124. var self = this;
  125. fs.readFile(path, 'utf-8', function(err, data) {
  126. if (err) {
  127. self._loadError = err;
  128. } else {
  129. self.emit('packet', new Packets.LocalDataFilePacket(data));
  130. }
  131. self.emit('packet', new Packets.EmptyPacket());
  132. });
  133. };