prepare.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. var util = require('util');
  2. var Packets = require('../packets/index.js');
  3. var Command = require('./command.js');
  4. var CloseStatement = require('./close_statement.js')
  5. var Execute = require('./execute.js')
  6. function Prepare(options, callback)
  7. {
  8. Command.call(this);
  9. this.query = options.sql;
  10. this.onResult = callback;
  11. this.id = 0;
  12. this.fieldCount = 0;
  13. this.parameterCount = 0;
  14. this.fields = [];
  15. this.parameterDefinitions = [];
  16. this.options = options;
  17. }
  18. util.inherits(Prepare, Command);
  19. Prepare.prototype.start = function(packet, connection) {
  20. connection.writePacket(new Packets.PrepareStatement(this.query).toPacket(1));
  21. return Prepare.prototype.prepareHeader;
  22. };
  23. function PreparedStatementInfo(query, id, columns, parameters, connection) {
  24. this.query = query;
  25. this.id = id;
  26. this.columns = columns;
  27. this.parameters = parameters;
  28. this.rowParser = null;
  29. this._connection = connection
  30. }
  31. PreparedStatementInfo.prototype.close = function() {
  32. return this._connection.addCommand(new CloseStatement(this.id));
  33. };
  34. PreparedStatementInfo.prototype.execute = function(parameters, callback) {
  35. if (typeof parameters == 'function') {
  36. callback = parameters;
  37. parameters = [];
  38. }
  39. return this._connection.addCommand(new Execute({ statement: this, values: parameters}, callback));
  40. };
  41. Prepare.prototype.prepareHeader = function(packet, connection) {
  42. var header = new Packets.PreparedStatementHeader(packet);
  43. this.id = header.id;
  44. this.fieldCount = header.fieldCount;
  45. this.parameterCount = header.parameterCount;
  46. if (this.parameterCount > 0)
  47. return Prepare.prototype.readParameter;
  48. else if (this.fieldCount > 0)
  49. return Prepare.prototype.readField;
  50. else
  51. return this.prepareDone(connection);
  52. };
  53. Prepare.prototype.readParameter = function(packet) {
  54. var def = new Packets.ColumnDefinition(packet);
  55. this.parameterDefinitions.push(def);
  56. if (this.parameterDefinitions.length == this.parameterCount)
  57. return Prepare.prototype.parametersEOF;
  58. return this.readParameter;
  59. };
  60. Prepare.prototype.readField = function(packet, connection) {
  61. var def = new Packets.ColumnDefinition(packet);
  62. this.fields.push(def);
  63. if (this.fields.length == this.fieldCount)
  64. return Prepare.prototype.fieldsEOF;
  65. return Prepare.prototype.readField;
  66. };
  67. Prepare.prototype.parametersEOF = function(packet, connection) {
  68. if (!packet.isEOF())
  69. return connection.protocolError("Expected EOF packet after parameters");
  70. if (this.fieldCount > 0)
  71. return Prepare.prototype.readField;
  72. else
  73. return this.prepareDone(connection);
  74. };
  75. Prepare.prototype.fieldsEOF = function(packet, connection) {
  76. if (!packet.isEOF())
  77. return connection.protocolError("Expected EOF packet after fields");
  78. return this.prepareDone(connection);
  79. };
  80. Prepare.prototype.prepareDone = function(connection)
  81. {
  82. var self = this;
  83. if (this.onResult)
  84. process.nextTick(function() {
  85. self.onResult(
  86. null,
  87. new PreparedStatementInfo(
  88. self.query,
  89. self.id,
  90. self.fields,
  91. self.parameterDefinitions,
  92. connection
  93. )
  94. );
  95. });
  96. return null;
  97. };
  98. module.exports = Prepare;