prepare.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. this.options.values
  54. );
  55. connection.writePacket(cmdPacket.toPacket(1));
  56. return Prepare.prototype.prepareHeader;
  57. }
  58. prepareHeader(packet, connection) {
  59. const header = new Packets.PreparedStatementHeader(packet);
  60. this.id = header.id;
  61. this.fieldCount = header.fieldCount;
  62. this.parameterCount = header.parameterCount;
  63. if (this.parameterCount > 0) {
  64. return Prepare.prototype.readParameter;
  65. } if (this.fieldCount > 0) {
  66. return Prepare.prototype.readField;
  67. }
  68. return this.prepareDone(connection);
  69. }
  70. readParameter(packet, connection) {
  71. const def = new Packets.ColumnDefinition(packet, connection.clientEncoding);
  72. this.parameterDefinitions.push(def);
  73. if (this.parameterDefinitions.length === this.parameterCount) {
  74. return Prepare.prototype.parametersEOF;
  75. }
  76. return this.readParameter;
  77. }
  78. readField(packet, connection) {
  79. const def = new Packets.ColumnDefinition(packet, connection.clientEncoding);
  80. this.fields.push(def);
  81. if (this.fields.length === this.fieldCount) {
  82. return Prepare.prototype.fieldsEOF;
  83. }
  84. return Prepare.prototype.readField;
  85. }
  86. parametersEOF(packet, connection) {
  87. if (!packet.isEOF()) {
  88. return connection.protocolError('Expected EOF packet after parameters');
  89. }
  90. if (this.fieldCount > 0) {
  91. return Prepare.prototype.readField;
  92. }
  93. return this.prepareDone(connection);
  94. }
  95. fieldsEOF(packet, connection) {
  96. if (!packet.isEOF()) {
  97. return connection.protocolError('Expected EOF packet after fields');
  98. }
  99. return this.prepareDone(connection);
  100. }
  101. prepareDone(connection) {
  102. const statement = new PreparedStatementInfo(
  103. this.query,
  104. this.id,
  105. this.fields,
  106. this.parameterDefinitions,
  107. connection
  108. );
  109. connection._statements.set(this.key, statement);
  110. if (this.onResult) {
  111. this.onResult(null, statement);
  112. }
  113. return null;
  114. }
  115. }
  116. module.exports = Prepare;