|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const Types = require('../constants/types.js'); |
| 4 | +const Charsets = require('../constants/charsets.js'); |
| 5 | +const helpers = require('../helpers'); |
| 6 | + |
| 7 | +const typeNames = []; |
| 8 | +for (const t in Types) { |
| 9 | + typeNames[Types[t]] = t; |
| 10 | +} |
| 11 | + |
| 12 | +function readField({ packet, type, charset, encoding, config, options }) { |
| 13 | + const supportBigNumbers = |
| 14 | + options.supportBigNumbers || config.supportBigNumbers; |
| 15 | + const bigNumberStrings = options.bigNumberStrings || config.bigNumberStrings; |
| 16 | + const timezone = options.timezone || config.timezone; |
| 17 | + const dateStrings = options.dateStrings || config.dateStrings; |
| 18 | + |
| 19 | + switch (type) { |
| 20 | + case Types.TINY: |
| 21 | + case Types.SHORT: |
| 22 | + case Types.LONG: |
| 23 | + case Types.INT24: |
| 24 | + case Types.YEAR: |
| 25 | + return packet.parseLengthCodedIntNoBigCheck(); |
| 26 | + case Types.LONGLONG: |
| 27 | + if (supportBigNumbers && bigNumberStrings) { |
| 28 | + return packet.parseLengthCodedIntString(); |
| 29 | + } |
| 30 | + return packet.parseLengthCodedInt(supportBigNumbers); |
| 31 | + case Types.FLOAT: |
| 32 | + case Types.DOUBLE: |
| 33 | + return packet.parseLengthCodedFloat(); |
| 34 | + case Types.NULL: |
| 35 | + case Types.DECIMAL: |
| 36 | + case Types.NEWDECIMAL: |
| 37 | + if (config.decimalNumbers) { |
| 38 | + return packet.parseLengthCodedFloat(); |
| 39 | + } |
| 40 | + return packet.readLengthCodedString("ascii"); |
| 41 | + case Types.DATE: |
| 42 | + if (helpers.typeMatch(type, dateStrings, Types)) { |
| 43 | + return packet.readLengthCodedString("ascii"); |
| 44 | + } |
| 45 | + return packet.parseDate(timezone); |
| 46 | + case Types.DATETIME: |
| 47 | + case Types.TIMESTAMP: |
| 48 | + if (helpers.typeMatch(type, dateStrings, Types)) { |
| 49 | + return packet.readLengthCodedString('ascii'); |
| 50 | + } |
| 51 | + return packet.parseDateTime(timezone); |
| 52 | + case Types.TIME: |
| 53 | + return packet.readLengthCodedString('ascii'); |
| 54 | + case Types.GEOMETRY: |
| 55 | + return packet.parseGeometryValue(); |
| 56 | + case Types.JSON: |
| 57 | + // Since for JSON columns mysql always returns charset 63 (BINARY), |
| 58 | + // we have to handle it according to JSON specs and use "utf8", |
| 59 | + // see https://github.com/sidorares/node-mysql2/issues/409 |
| 60 | + return JSON.parse(packet.readLengthCodedString('utf8')); |
| 61 | + default: |
| 62 | + if (charset === Charsets.BINARY) { |
| 63 | + return packet.readLengthCodedBuffer(); |
| 64 | + } |
| 65 | + return packet.readLengthCodedString(encoding); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +function compile(fields, options, config) { |
| 70 | + if ( |
| 71 | + typeof config.typeCast === 'function' && |
| 72 | + typeof options.typeCast !== 'function' |
| 73 | + ) { |
| 74 | + options.typeCast = config.typeCast; |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +function createTypecastField(field, packet) { |
| 79 | + return { |
| 80 | + type: typeNames[field.columnType], |
| 81 | + length: field.columnLength, |
| 82 | + db: field.schema, |
| 83 | + table: field.table, |
| 84 | + name: field.name, |
| 85 | + string: function(encoding = field.encoding) { |
| 86 | + if (field.columnType === Types.JSON && encoding === field.encoding) { |
| 87 | + // Since for JSON columns mysql always returns charset 63 (BINARY), |
| 88 | + // we have to handle it according to JSON specs and use "utf8", |
| 89 | + // see https://github.com/sidorares/node-mysql2/issues/1661 |
| 90 | + console.warn(`typeCast: JSON column "${field.name}" is interpreted as BINARY by default, recommended to manually set utf8 encoding: \`field.string("utf8")\``); |
| 91 | + } |
| 92 | + return packet.readLengthCodedString(encoding); |
| 93 | + }, |
| 94 | + buffer: function() { |
| 95 | + return packet.readLengthCodedBuffer(); |
| 96 | + }, |
| 97 | + geometry: function() { |
| 98 | + return packet.parseGeometryValue(); |
| 99 | + } |
| 100 | + }; |
| 101 | +} |
| 102 | + |
| 103 | +function getTextParser(fields, options, config) { |
| 104 | + return { |
| 105 | + next(packet, fields, options) { |
| 106 | + const result = options.rowsAsArray ? [] : {}; |
| 107 | + for (let i = 0; i < fields.length; i++) { |
| 108 | + const field = fields[i]; |
| 109 | + const typeCast = options.typeCast ? options.typeCast : config.typeCast; |
| 110 | + const next = () => { |
| 111 | + return readField({ |
| 112 | + packet, |
| 113 | + type: field.columnType, |
| 114 | + encoding: field.encoding, |
| 115 | + charset: field.characterSet, |
| 116 | + config, |
| 117 | + options |
| 118 | + }) |
| 119 | + } |
| 120 | + let value; |
| 121 | + if (typeof typeCast === 'function') { |
| 122 | + value = typeCast(createTypecastField(field, packet), next); |
| 123 | + } else { |
| 124 | + value = next(); |
| 125 | + } |
| 126 | + if (options.rowsAsArray) { |
| 127 | + result.push(value); |
| 128 | + } else if (options.nestTables) { |
| 129 | + if (!result[field.table]) { |
| 130 | + result[field.table] = {}; |
| 131 | + } |
| 132 | + result[field.table][field.name] = value; |
| 133 | + } else { |
| 134 | + result[field.name] = value; |
| 135 | + } |
| 136 | + } |
| 137 | + return result; |
| 138 | + } |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +module.exports = getTextParser; |
0 commit comments