execute.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. 'use strict';
  2. const CursorType = require('../constants/cursor');
  3. const CommandCodes = require('../constants/commands');
  4. const Types = require('../constants/types');
  5. const Packet = require('../packets/packet');
  6. const CharsetToEncoding = require('../constants/charset_encodings.js');
  7. function isJSON(value) {
  8. return (
  9. Array.isArray(value) ||
  10. value.constructor === Object ||
  11. (typeof value.toJSON === 'function' && !Buffer.isBuffer(value))
  12. );
  13. }
  14. /**
  15. * Converts a value to an object describing type, String/Buffer representation and length
  16. * @param {*} value
  17. */
  18. function toParameter(value, encoding, timezone) {
  19. let type = Types.VAR_STRING;
  20. let length;
  21. let writer = function(value) {
  22. // eslint-disable-next-line no-invalid-this
  23. return Packet.prototype.writeLengthCodedString.call(this, value, encoding);
  24. };
  25. if (value !== null) {
  26. switch (typeof value) {
  27. case 'undefined':
  28. throw new TypeError('Bind parameters must not contain undefined');
  29. case 'number':
  30. type = Types.DOUBLE;
  31. length = 8;
  32. writer = Packet.prototype.writeDouble;
  33. break;
  34. case 'boolean':
  35. value = value | 0;
  36. type = Types.TINY;
  37. length = 1;
  38. writer = Packet.prototype.writeInt8;
  39. break;
  40. case 'object':
  41. if (Object.prototype.toString.call(value) === '[object Date]') {
  42. type = Types.DATETIME;
  43. length = 12;
  44. writer = function(value) {
  45. // eslint-disable-next-line no-invalid-this
  46. return Packet.prototype.writeDate.call(this, value, timezone);
  47. };
  48. } else if (isJSON(value)) {
  49. value = JSON.stringify(value);
  50. type = Types.JSON;
  51. } else if (Buffer.isBuffer(value)) {
  52. length = Packet.lengthCodedNumberLength(value.length) + value.length;
  53. writer = Packet.prototype.writeLengthCodedBuffer;
  54. }
  55. break;
  56. default:
  57. value = value.toString();
  58. }
  59. } else {
  60. value = '';
  61. type = Types.NULL;
  62. }
  63. if (!length) {
  64. length = Packet.lengthCodedStringLength(value, encoding);
  65. }
  66. return { value, type, length, writer };
  67. }
  68. class Execute {
  69. constructor(id, parameters, charsetNumber, timezone) {
  70. this.id = id;
  71. this.parameters = parameters;
  72. this.encoding = CharsetToEncoding[charsetNumber];
  73. this.timezone = timezone;
  74. }
  75. toPacket() {
  76. // TODO: don't try to calculate packet length in advance, allocate some big buffer in advance (header + 256 bytes?)
  77. // and copy + reallocate if not enough
  78. // 0 + 4 - length, seqId
  79. // 4 + 1 - COM_EXECUTE
  80. // 5 + 4 - stmtId
  81. // 9 + 1 - flags
  82. // 10 + 4 - iteration-count (always 1)
  83. let length = 14;
  84. let parameters;
  85. if (this.parameters && this.parameters.length > 0) {
  86. length += Math.floor((this.parameters.length + 7) / 8);
  87. length += 1; // new-params-bound-flag
  88. length += 2 * this.parameters.length; // type byte for each parameter if new-params-bound-flag is set
  89. parameters = this.parameters.map(value =>
  90. toParameter(value, this.encoding, this.timezone)
  91. );
  92. length += parameters.reduce(
  93. (accumulator, parameter) => accumulator + parameter.length,
  94. 0
  95. );
  96. }
  97. const buffer = Buffer.allocUnsafe(length);
  98. const packet = new Packet(0, buffer, 0, length);
  99. packet.offset = 4;
  100. packet.writeInt8(CommandCodes.STMT_EXECUTE);
  101. packet.writeInt32(this.id);
  102. packet.writeInt8(CursorType.NO_CURSOR); // flags
  103. packet.writeInt32(1); // iteration-count, always 1
  104. if (parameters) {
  105. let bitmap = 0;
  106. let bitValue = 1;
  107. parameters.forEach(parameter => {
  108. if (parameter.type === Types.NULL) {
  109. bitmap += bitValue;
  110. }
  111. bitValue *= 2;
  112. if (bitValue === 256) {
  113. packet.writeInt8(bitmap);
  114. bitmap = 0;
  115. bitValue = 1;
  116. }
  117. });
  118. if (bitValue !== 1) {
  119. packet.writeInt8(bitmap);
  120. }
  121. // TODO: explain meaning of the flag
  122. // afaik, if set n*2 bytes with type of parameter are sent before parameters
  123. // if not, previous execution types are used (TODO prooflink)
  124. packet.writeInt8(1); // new-params-bound-flag
  125. // Write parameter types
  126. parameters.forEach(parameter => {
  127. packet.writeInt8(parameter.type); // field type
  128. packet.writeInt8(0); // parameter flag
  129. });
  130. // Write parameter values
  131. parameters.forEach(parameter => {
  132. if (parameter.type !== Types.NULL) {
  133. parameter.writer.call(packet, parameter.value);
  134. }
  135. });
  136. }
  137. return packet;
  138. }
  139. }
  140. module.exports = Execute;