execute.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. static fromPacket(packet, encoding) {
  76. const stmtId = packet.readInt32();
  77. const flags = packet.readInt8();
  78. const iterationCount = packet.readInt32();
  79. let i = packet.offset;
  80. while (i < packet.end - 1) {
  81. if((packet.buffer[i+1] === Types.VAR_STRING
  82. || packet.buffer[i+1] === Types.NULL
  83. || packet.buffer[i+1] === Types.DOUBLE
  84. || packet.buffer[i+1] === Types.TINY
  85. || packet.buffer[i+1] === Types.DATETIME
  86. || packet.buffer[i+1] === Types.JSON) && packet.buffer[i] === 1 && packet.buffer[i+2] === 0) {
  87. break;
  88. }
  89. else {
  90. packet.readInt8()
  91. }
  92. i++;
  93. }
  94. const types = [];
  95. for(let i = packet.offset + 1; i < packet.end - 1; i++) {
  96. if((packet.buffer[i] === Types.VAR_STRING
  97. || packet.buffer[i] === Types.NULL
  98. || packet.buffer[i] === Types.DOUBLE
  99. || packet.buffer[i] === Types.TINY
  100. || packet.buffer[i] === Types.DATETIME
  101. || packet.buffer[i] === Types.JSON) && packet.buffer[i + 1] === 0) {
  102. types.push(packet.buffer[i]);
  103. packet.skip(2);
  104. }
  105. }
  106. packet.skip(1);
  107. const values = [];
  108. for(let i = 0; i < types.length; i++) {
  109. if(types[i] === Types.VAR_STRING) {
  110. values.push(packet.readLengthCodedString(encoding))
  111. }
  112. else if(types[i] === Types.DOUBLE) {
  113. values.push(packet.readDouble())
  114. }
  115. else if(types[i] === Types.TINY) {
  116. values.push(packet.readInt8())
  117. }
  118. else if(types[i] === Types.DATETIME) {
  119. values.push(packet.readDateTime())
  120. }
  121. else if(types[i] === Types.JSON) {
  122. values.push(JSON.parse(packet.readLengthCodedString(encoding)))
  123. }
  124. if(types[i] === Types.NULL) {
  125. values.push(null)
  126. }
  127. }
  128. return { stmtId, flags, iterationCount, values };
  129. }
  130. toPacket() {
  131. // TODO: don't try to calculate packet length in advance, allocate some big buffer in advance (header + 256 bytes?)
  132. // and copy + reallocate if not enough
  133. // 0 + 4 - length, seqId
  134. // 4 + 1 - COM_EXECUTE
  135. // 5 + 4 - stmtId
  136. // 9 + 1 - flags
  137. // 10 + 4 - iteration-count (always 1)
  138. let length = 14;
  139. let parameters;
  140. if (this.parameters && this.parameters.length > 0) {
  141. length += Math.floor((this.parameters.length + 7) / 8);
  142. length += 1; // new-params-bound-flag
  143. length += 2 * this.parameters.length; // type byte for each parameter if new-params-bound-flag is set
  144. parameters = this.parameters.map(value =>
  145. toParameter(value, this.encoding, this.timezone)
  146. );
  147. length += parameters.reduce(
  148. (accumulator, parameter) => accumulator + parameter.length,
  149. 0
  150. );
  151. }
  152. const buffer = Buffer.allocUnsafe(length);
  153. const packet = new Packet(0, buffer, 0, length);
  154. packet.offset = 4;
  155. packet.writeInt8(CommandCodes.STMT_EXECUTE);
  156. packet.writeInt32(this.id);
  157. packet.writeInt8(CursorType.NO_CURSOR); // flags
  158. packet.writeInt32(1); // iteration-count, always 1
  159. if (parameters) {
  160. let bitmap = 0;
  161. let bitValue = 1;
  162. parameters.forEach(parameter => {
  163. if (parameter.type === Types.NULL) {
  164. bitmap += bitValue;
  165. }
  166. bitValue *= 2;
  167. if (bitValue === 256) {
  168. packet.writeInt8(bitmap);
  169. bitmap = 0;
  170. bitValue = 1;
  171. }
  172. });
  173. if (bitValue !== 1) {
  174. packet.writeInt8(bitmap);
  175. }
  176. // TODO: explain meaning of the flag
  177. // afaik, if set n*2 bytes with type of parameter are sent before parameters
  178. // if not, previous execution types are used (TODO prooflink)
  179. packet.writeInt8(1); // new-params-bound-flag
  180. // Write parameter types
  181. parameters.forEach(parameter => {
  182. packet.writeInt8(parameter.type); // field type
  183. packet.writeInt8(0); // parameter flag
  184. });
  185. // Write parameter values
  186. parameters.forEach(parameter => {
  187. if (parameter.type !== Types.NULL) {
  188. parameter.writer.call(packet, parameter.value);
  189. }
  190. });
  191. }
  192. return packet;
  193. }
  194. }
  195. module.exports = Execute;