prepare.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. 'use strict';
  2. const Packets = require('../packets/index.js');
  3. const Command = require('./command.js');
  4. const CloseStatement = require('./close_statement.js');
  5. const Execute = require('./execute.js');
  6. class PreparedStatementInfo {
  7. constructor(query, id, columns, parameters, connection) {
  8. this.query = query;
  9. this.id = id;
  10. this.columns = columns;
  11. this.parameters = parameters;
  12. this.rowParser = null;
  13. this._connection = connection;
  14. }
  15. close() {
  16. return this._connection.addCommand(new CloseStatement(this.id));
  17. }
  18. execute(parameters, callback) {
  19. if (typeof parameters === 'function') {
  20. callback = parameters;
  21. parameters = [];
  22. }
  23. return this._connection.addCommand(
  24. new Execute({ statement: this, values: parameters }, callback)
  25. );
  26. }
  27. }
  28. class Prepare extends Command {
  29. constructor(options, callback) {
  30. super();
  31. this.query = options.sql;
  32. this.onResult = callback;
  33. this.id = 0;
  34. this.fieldCount = 0;
  35. this.parameterCount = 0;
  36. this.fields = [];
  37. this.parameterDefinitions = [];
  38. this.options = options;
  39. }
  40. start(packet, connection) {
  41. const Connection = connection.constructor;
  42. this.key = Connection.statementKey(this.options);
  43. const statement = connection._statements.get(this.key);
  44. if (statement) {
  45. if (this.onResult) {
  46. this.onResult(null, statement);
  47. }
  48. return null;
  49. }
  50. const cmdPacket = new Packets.PrepareStatement(
  51. this.query,
  52. connection.config.charsetNumber
  53. );
  54. connection.writePacket(cmdPacket.toPacket(1));
  55. return Prepare.prototype.prepareHeader;
  56. }
  57. prepareHeader(packet, connection) {
  58. const header = new Packets.PreparedStatementHeader(packet);
  59. this.id = header.id;
  60. this.fieldCount = header.fieldCount;
  61. this.parameterCount = header.parameterCount;
  62. if (this.parameterCount > 0) {
  63. return Prepare.prototype.readParameter;
  64. } if (this.fieldCount > 0) {
  65. return Prepare.prototype.readField;
  66. }
  67. return this.prepareDone(connection);
  68. }
  69. readParameter(packet, connection) {
  70. const def = new Packets.ColumnDefinition(packet, connection.clientEncoding);
  71. this.parameterDefinitions.push(def);
  72. if (this.parameterDefinitions.length === this.parameterCount) {
  73. return Prepare.prototype.parametersEOF;
  74. }
  75. return this.readParameter;
  76. }
  77. readField(packet, connection) {
  78. const def = new Packets.ColumnDefinition(packet, connection.clientEncoding);
  79. this.fields.push(def);
  80. if (this.fields.length === this.fieldCount) {
  81. return Prepare.prototype.fieldsEOF;
  82. }
  83. return Prepare.prototype.readField;
  84. }
  85. parametersEOF(packet, connection) {
  86. if (!packet.isEOF()) {
  87. return connection.protocolError('Expected EOF packet after parameters');
  88. }
  89. if (this.fieldCount > 0) {
  90. return Prepare.prototype.readField;
  91. }
  92. return this.prepareDone(connection);
  93. }
  94. fieldsEOF(packet, connection) {
  95. if (!packet.isEOF()) {
  96. return connection.protocolError('Expected EOF packet after fields');
  97. }
  98. return this.prepareDone(connection);
  99. }
  100. prepareDone(connection) {
  101. const statement = new PreparedStatementInfo(
  102. this.query,
  103. this.id,
  104. this.fields,
  105. this.parameterDefinitions,
  106. connection
  107. );
  108. connection._statements.set(this.key, statement);
  109. if (this.onResult) {
  110. this.onResult(null, statement);
  111. }
  112. return null;
  113. }
  114. }
  115. module.exports = Prepare;