execute.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. var util = require('util');
  2. var Command = require('./command.js');
  3. var Query = require('./query.js');
  4. var Packets = require('../packets/index.js');
  5. var compileParser = require('../compile_binary_parser.js');
  6. function Execute(options, callback)
  7. {
  8. Command.call(this);
  9. this.statement = options.statement;
  10. this.sql = options.sql;
  11. this.values = options.values;
  12. this.onResult = callback;
  13. this.parameters = options.values;
  14. this.insertId = 0;
  15. this._rows = [];
  16. this._fields = [];
  17. this._result = [];
  18. this._fieldCount = 0;
  19. this._rowParser = null;
  20. this.options = options;
  21. this._resultIndex = 0;
  22. this._localStream = null;
  23. this._streamFactory = options.infileStreamFactory;
  24. }
  25. util.inherits(Execute, Command);
  26. Execute.prototype.buildParserFromFields = function(fields, connection) {
  27. var parserKey = connection.keyFromFields(fields, this.options);
  28. var parser = connection.binaryProtocolParsers[parserKey];
  29. if (!parser) {
  30. parser = compileParser(fields, this.options, connection.config);
  31. connection.binaryProtocolParsers[parserKey] = parser;
  32. }
  33. return parser;
  34. }
  35. Execute.prototype.start = function(packet, connection) {
  36. var executePacket = new Packets.Execute(this.statement.id, this.parameters);
  37. connection.writePacket(executePacket.toPacket(1));
  38. return Execute.prototype.resultsetHeader;
  39. };
  40. Execute.prototype.done = Query.prototype.done;
  41. Execute.prototype.doneInsert = Query.prototype.doneInsert;
  42. Execute.prototype.resultsetHeader = Query.prototype.resultsetHeader;
  43. Execute.prototype._findOrCreateReadStream = Query.prototype._findOrCreateReadStream
  44. Execute.prototype._streamLocalInfile = Query.prototype._streamLocalInfile;
  45. Execute.prototype.row = Query.prototype.row;
  46. Execute.prototype.stream = Query.prototype.stream;
  47. Execute.prototype.readField = function(packet, connection) {
  48. var def, fields;
  49. // disabling for now, but would be great to find reliable way to parse fields only once
  50. // fields reported by prepare can be empty at all or just incorrect - see #169
  51. //
  52. // perfomance optimisation: if we already have this field parsed in statement header, use one from header
  53. //var field = this.statement.columns.length == this._fieldCount ?
  54. // this.statement.columns[this._receivedFieldsCount] : new Packets.ColumnDefinition(packet);
  55. var field = new Packets.ColumnDefinition(packet);
  56. this._receivedFieldsCount++;
  57. this._fields[this._resultIndex].push(field);
  58. if (this._receivedFieldsCount == this._fieldCount) {
  59. fields = this._fields[this._resultIndex];
  60. this.emit('fields', fields, this._resultIndex);
  61. return Execute.prototype.fieldsEOF;
  62. }
  63. return Execute.prototype.readField;
  64. };
  65. Execute.prototype.fieldsEOF = function(packet, connection) {
  66. // check EOF
  67. if (!packet.isEOF())
  68. return connection.protocolError("Expected EOF packet");
  69. this._rowParser = this.buildParserFromFields(this._fields[this._resultIndex], connection)
  70. return Execute.prototype.row;
  71. };
  72. module.exports = Execute;