common-binary-cmd.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. 'use strict';
  2. const ResultSet = require('./resultset');
  3. class CommonBinary extends ResultSet {
  4. constructor(resolve, reject, cmdOpts, connOpts, sql, values) {
  5. super(resolve, reject);
  6. this.configAssign(connOpts, cmdOpts);
  7. this.sql = sql;
  8. this.initialValues = values;
  9. }
  10. /**
  11. * Write (and escape) current parameter value to output writer
  12. *
  13. * @param out output writer
  14. * @param value current parameter
  15. * @param opts connection options
  16. * @param info connection information
  17. */
  18. writeParam(out, value, opts, info) {
  19. let flushed = false;
  20. switch (typeof value) {
  21. case 'boolean':
  22. flushed = out.writeInt8(0x00);
  23. flushed = out.writeInt8(value ? 0x01 : 0x00) || flushed;
  24. break;
  25. case 'bigint':
  26. case 'number':
  27. flushed = out.writeInt8(0x00);
  28. flushed = out.writeLengthStringAscii('' + value) || flushed;
  29. break;
  30. case 'object':
  31. if (Object.prototype.toString.call(value) === '[object Date]') {
  32. flushed = out.writeInt8(0x00);
  33. flushed = out.writeBinaryDate(value, opts) || flushed;
  34. } else if (Buffer.isBuffer(value)) {
  35. flushed = out.writeInt8(0x00);
  36. flushed = out.writeLengthEncodedBuffer(value) || flushed;
  37. } else if (typeof value.toSqlString === 'function') {
  38. flushed = out.writeInt8(0x00);
  39. flushed = out.writeLengthEncodedString(String(value.toSqlString())) || flushed;
  40. } else {
  41. if (
  42. value.type != null &&
  43. [
  44. 'Point',
  45. 'LineString',
  46. 'Polygon',
  47. 'MultiPoint',
  48. 'MultiLineString',
  49. 'MultiPolygon',
  50. 'GeometryCollection'
  51. ].includes(value.type)
  52. ) {
  53. const geoBuff = this.getBufferFromGeometryValue(value);
  54. if (geoBuff) {
  55. flushed = out.writeInt8(0x00); //Value follow
  56. flushed =
  57. out.writeLengthEncodedBuffer(Buffer.concat([Buffer.from([0, 0, 0, 0]), geoBuff])) ||
  58. flushed;
  59. } else {
  60. flushed = out.writeInt8(0x01); //NULL
  61. }
  62. } else {
  63. //TODO check if permitSetMultiParamEntries is needed !?
  64. flushed = out.writeInt8(0x00);
  65. flushed = out.writeLengthEncodedString(JSON.stringify(value)) || flushed;
  66. }
  67. }
  68. break;
  69. default:
  70. flushed = out.writeInt8(0x00);
  71. flushed = out.writeLengthEncodedString(value) || flushed;
  72. }
  73. return flushed;
  74. }
  75. getBufferFromGeometryValue(value, headerType) {
  76. let geoBuff;
  77. let pos;
  78. let type;
  79. if (!headerType) {
  80. switch (value.type) {
  81. case 'Point':
  82. geoBuff = Buffer.allocUnsafe(21);
  83. geoBuff.writeInt8(0x01, 0); //LITTLE ENDIAN
  84. geoBuff.writeInt32LE(1, 1); //wkbPoint
  85. if (
  86. value.coordinates &&
  87. Array.isArray(value.coordinates) &&
  88. value.coordinates.length >= 2 &&
  89. !isNaN(value.coordinates[0]) &&
  90. !isNaN(value.coordinates[1])
  91. ) {
  92. geoBuff.writeDoubleLE(value.coordinates[0], 5); //X
  93. geoBuff.writeDoubleLE(value.coordinates[1], 13); //Y
  94. return geoBuff;
  95. } else {
  96. return null;
  97. }
  98. case 'LineString':
  99. if (value.coordinates && Array.isArray(value.coordinates)) {
  100. const pointNumber = value.coordinates.length;
  101. geoBuff = Buffer.allocUnsafe(9 + 16 * pointNumber);
  102. geoBuff.writeInt8(0x01, 0); //LITTLE ENDIAN
  103. geoBuff.writeInt32LE(2, 1); //wkbLineString
  104. geoBuff.writeInt32LE(pointNumber, 5);
  105. for (let i = 0; i < pointNumber; i++) {
  106. if (
  107. value.coordinates[i] &&
  108. Array.isArray(value.coordinates[i]) &&
  109. value.coordinates[i].length >= 2 &&
  110. !isNaN(value.coordinates[i][0]) &&
  111. !isNaN(value.coordinates[i][1])
  112. ) {
  113. geoBuff.writeDoubleLE(value.coordinates[i][0], 9 + 16 * i); //X
  114. geoBuff.writeDoubleLE(value.coordinates[i][1], 17 + 16 * i); //Y
  115. } else {
  116. return null;
  117. }
  118. }
  119. return geoBuff;
  120. } else {
  121. return null;
  122. }
  123. case 'Polygon':
  124. if (value.coordinates && Array.isArray(value.coordinates)) {
  125. const numRings = value.coordinates.length;
  126. let size = 0;
  127. for (let i = 0; i < numRings; i++) {
  128. size += 4 + 16 * value.coordinates[i].length;
  129. }
  130. geoBuff = Buffer.allocUnsafe(9 + size);
  131. geoBuff.writeInt8(0x01, 0); //LITTLE ENDIAN
  132. geoBuff.writeInt32LE(3, 1); //wkbPolygon
  133. geoBuff.writeInt32LE(numRings, 5);
  134. pos = 9;
  135. for (let i = 0; i < numRings; i++) {
  136. const lineString = value.coordinates[i];
  137. if (lineString && Array.isArray(lineString)) {
  138. geoBuff.writeInt32LE(lineString.length, pos);
  139. pos += 4;
  140. for (let j = 0; j < lineString.length; j++) {
  141. if (
  142. lineString[j] &&
  143. Array.isArray(lineString[j]) &&
  144. lineString[j].length >= 2 &&
  145. !isNaN(lineString[j][0]) &&
  146. !isNaN(lineString[j][1])
  147. ) {
  148. geoBuff.writeDoubleLE(lineString[j][0], pos); //X
  149. geoBuff.writeDoubleLE(lineString[j][1], pos + 8); //Y
  150. pos += 16;
  151. } else {
  152. return null;
  153. }
  154. }
  155. }
  156. }
  157. return geoBuff;
  158. } else {
  159. return null;
  160. }
  161. case 'MultiPoint':
  162. type = 'MultiPoint';
  163. geoBuff = Buffer.allocUnsafe(9);
  164. geoBuff.writeInt8(0x01, 0); //LITTLE ENDIAN
  165. geoBuff.writeInt32LE(4, 1); //wkbMultiPoint
  166. break;
  167. case 'MultiLineString':
  168. type = 'MultiLineString';
  169. geoBuff = Buffer.allocUnsafe(9);
  170. geoBuff.writeInt8(0x01, 0); //LITTLE ENDIAN
  171. geoBuff.writeInt32LE(5, 1); //wkbMultiLineString
  172. break;
  173. case 'MultiPolygon':
  174. type = 'MultiPolygon';
  175. geoBuff = Buffer.allocUnsafe(9);
  176. geoBuff.writeInt8(0x01, 0); //LITTLE ENDIAN
  177. geoBuff.writeInt32LE(6, 1); //wkbMultiPolygon
  178. break;
  179. case 'GeometryCollection':
  180. geoBuff = Buffer.allocUnsafe(9);
  181. geoBuff.writeInt8(0x01, 0); //LITTLE ENDIAN
  182. geoBuff.writeInt32LE(7, 1); //wkbGeometryCollection
  183. if (value.geometries && Array.isArray(value.geometries)) {
  184. const coordinateLength = value.geometries.length;
  185. const subArrays = [geoBuff];
  186. for (let i = 0; i < coordinateLength; i++) {
  187. const tmpBuf = this.getBufferFromGeometryValue(value.geometries[i]);
  188. if (tmpBuf == null) break;
  189. subArrays.push(tmpBuf);
  190. }
  191. geoBuff.writeInt32LE(subArrays.length - 1, 5);
  192. return Buffer.concat(subArrays);
  193. } else {
  194. geoBuff.writeInt32LE(0, 5);
  195. return geoBuff;
  196. }
  197. default:
  198. return null;
  199. }
  200. if (value.coordinates && Array.isArray(value.coordinates)) {
  201. const coordinateLength = value.coordinates.length;
  202. const subArrays = [geoBuff];
  203. for (let i = 0; i < coordinateLength; i++) {
  204. const tmpBuf = this.getBufferFromGeometryValue(value.coordinates[i], type);
  205. if (tmpBuf == null) break;
  206. subArrays.push(tmpBuf);
  207. }
  208. geoBuff.writeInt32LE(subArrays.length - 1, 5);
  209. return Buffer.concat(subArrays);
  210. } else {
  211. geoBuff.writeInt32LE(0, 5);
  212. return geoBuff;
  213. }
  214. } else {
  215. switch (headerType) {
  216. case 'MultiPoint':
  217. if (
  218. value &&
  219. Array.isArray(value) &&
  220. value.length >= 2 &&
  221. !isNaN(value[0]) &&
  222. !isNaN(value[1])
  223. ) {
  224. geoBuff = Buffer.allocUnsafe(21);
  225. geoBuff.writeInt8(0x01, 0); //LITTLE ENDIAN
  226. geoBuff.writeInt32LE(1, 1); //wkbPoint
  227. geoBuff.writeDoubleLE(value[0], 5); //X
  228. geoBuff.writeDoubleLE(value[1], 13); //Y
  229. return geoBuff;
  230. }
  231. return null;
  232. case 'MultiLineString':
  233. if (value && Array.isArray(value)) {
  234. const pointNumber = value.length;
  235. geoBuff = Buffer.allocUnsafe(9 + 16 * pointNumber);
  236. geoBuff.writeInt8(0x01, 0); //LITTLE ENDIAN
  237. geoBuff.writeInt32LE(2, 1); //wkbLineString
  238. geoBuff.writeInt32LE(pointNumber, 5);
  239. for (let i = 0; i < pointNumber; i++) {
  240. if (
  241. value[i] &&
  242. Array.isArray(value[i]) &&
  243. value[i].length >= 2 &&
  244. !isNaN(value[i][0]) &&
  245. !isNaN(value[i][1])
  246. ) {
  247. geoBuff.writeDoubleLE(value[i][0], 9 + 16 * i); //X
  248. geoBuff.writeDoubleLE(value[i][1], 17 + 16 * i); //Y
  249. } else {
  250. return null;
  251. }
  252. }
  253. return geoBuff;
  254. }
  255. return null;
  256. case 'MultiPolygon':
  257. if (value && Array.isArray(value)) {
  258. const numRings = value.length;
  259. let size = 0;
  260. for (let i = 0; i < numRings; i++) {
  261. size += 4 + 16 * value[i].length;
  262. }
  263. geoBuff = Buffer.allocUnsafe(9 + size);
  264. geoBuff.writeInt8(0x01, 0); //LITTLE ENDIAN
  265. geoBuff.writeInt32LE(3, 1); //wkbPolygon
  266. geoBuff.writeInt32LE(numRings, 5);
  267. pos = 9;
  268. for (let i = 0; i < numRings; i++) {
  269. const lineString = value[i];
  270. if (lineString && Array.isArray(lineString)) {
  271. geoBuff.writeInt32LE(lineString.length, pos);
  272. pos += 4;
  273. for (let j = 0; j < lineString.length; j++) {
  274. if (
  275. lineString[j] &&
  276. Array.isArray(lineString[j]) &&
  277. lineString[j].length >= 2 &&
  278. !isNaN(lineString[j][0]) &&
  279. !isNaN(lineString[j][1])
  280. ) {
  281. geoBuff.writeDoubleLE(lineString[j][0], pos); //X
  282. geoBuff.writeDoubleLE(lineString[j][1], pos + 8); //Y
  283. pos += 16;
  284. } else {
  285. return null;
  286. }
  287. }
  288. }
  289. }
  290. return geoBuff;
  291. }
  292. return null;
  293. }
  294. return null;
  295. }
  296. }
  297. /**
  298. * Read text result-set row
  299. *
  300. * see: https://mariadb.com/kb/en/library/resultset-row/#text-resultset-row
  301. * data are created according to their type.
  302. *
  303. * @param columns columns metadata
  304. * @param packet current row packet
  305. * @param connOpts connection options
  306. * @returns {*} row data
  307. */
  308. parseRow(columns, packet, connOpts) {
  309. throw new Error('not implemented');
  310. }
  311. }
  312. module.exports = CommonBinary;