Initial import from local backup (Documents-Playground/pakerpale)
This commit is contained in:
193
node_modules/mysql2/lib/parsers/binary_parser.js
generated
vendored
Normal file
193
node_modules/mysql2/lib/parsers/binary_parser.js
generated
vendored
Normal file
@@ -0,0 +1,193 @@
|
||||
'use strict';
|
||||
|
||||
const FieldFlags = require('../constants/field_flags.js');
|
||||
const Charsets = require('../constants/charsets.js');
|
||||
const Types = require('../constants/types.js');
|
||||
const helpers = require('../helpers');
|
||||
const genFunc = require('generate-function');
|
||||
const parserCache = require('./parser_cache.js');
|
||||
const typeNames = [];
|
||||
for (const t in Types) {
|
||||
typeNames[Types[t]] = t;
|
||||
}
|
||||
|
||||
function readCodeFor(field, config, options, fieldNum) {
|
||||
const supportBigNumbers =
|
||||
options.supportBigNumbers || config.supportBigNumbers;
|
||||
const bigNumberStrings = options.bigNumberStrings || config.bigNumberStrings;
|
||||
const unsigned = field.flags & FieldFlags.UNSIGNED;
|
||||
switch (field.columnType) {
|
||||
case Types.TINY:
|
||||
return unsigned ? 'packet.readInt8();' : 'packet.readSInt8();';
|
||||
case Types.SHORT:
|
||||
return unsigned ? 'packet.readInt16();' : 'packet.readSInt16();';
|
||||
case Types.LONG:
|
||||
case Types.INT24: // in binary protocol int24 is encoded in 4 bytes int32
|
||||
return unsigned ? 'packet.readInt32();' : 'packet.readSInt32();';
|
||||
case Types.YEAR:
|
||||
return 'packet.readInt16()';
|
||||
case Types.FLOAT:
|
||||
return 'packet.readFloat();';
|
||||
case Types.DOUBLE:
|
||||
return 'packet.readDouble();';
|
||||
case Types.NULL:
|
||||
return 'null;';
|
||||
case Types.DATE:
|
||||
case Types.DATETIME:
|
||||
case Types.TIMESTAMP:
|
||||
case Types.NEWDATE:
|
||||
if (config.dateStrings) {
|
||||
return 'packet.readDateTimeString(' + field.decimals + ');';
|
||||
}
|
||||
return 'packet.readDateTime();';
|
||||
case Types.TIME:
|
||||
return 'packet.readTimeString()';
|
||||
case Types.DECIMAL:
|
||||
case Types.NEWDECIMAL:
|
||||
if (config.decimalNumbers) {
|
||||
return 'packet.parseLengthCodedFloat();';
|
||||
}
|
||||
return 'packet.readLengthCodedString("ascii");';
|
||||
case Types.GEOMETRY:
|
||||
return 'packet.parseGeometryValue();';
|
||||
case Types.JSON:
|
||||
// Since for JSON columns mysql always returns charset 63 (BINARY),
|
||||
// we have to handle it according to JSON specs and use "utf8",
|
||||
// see https://github.com/sidorares/node-mysql2/issues/409
|
||||
return 'JSON.parse(packet.readLengthCodedString("utf8"));';
|
||||
case Types.LONGLONG:
|
||||
if (!supportBigNumbers) {
|
||||
return unsigned
|
||||
? 'packet.readInt64JSNumber();'
|
||||
: 'packet.readSInt64JSNumber();';
|
||||
} else {
|
||||
if (bigNumberStrings) {
|
||||
return unsigned
|
||||
? 'packet.readInt64String();'
|
||||
: 'packet.readSInt64String();';
|
||||
} else {
|
||||
return unsigned ? 'packet.readInt64();' : 'packet.readSInt64();';
|
||||
}
|
||||
}
|
||||
default:
|
||||
if (field.characterSet == Charsets.BINARY) {
|
||||
return 'packet.readLengthCodedBuffer();';
|
||||
} else {
|
||||
return (
|
||||
'packet.readLengthCodedString(CharsetToEncoding[fields[' +
|
||||
fieldNum +
|
||||
'].characterSet])'
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function compile(fields, options, config) {
|
||||
const parserFn = genFunc();
|
||||
let i = 0;
|
||||
const nullBitmapLength = Math.floor((fields.length + 7 + 2) / 8);
|
||||
|
||||
/* eslint-disable no-trailing-spaces */
|
||||
/* eslint-disable no-spaced-func */
|
||||
/* eslint-disable no-unexpected-multiline */
|
||||
|
||||
parserFn('(function(){')(
|
||||
'return function BinaryRow(packet, fields, options, CharsetToEncoding) {'
|
||||
);
|
||||
|
||||
if (options.rowsAsArray) {
|
||||
parserFn('const result = new Array(' + fields.length + ');');
|
||||
}
|
||||
|
||||
const resultTables = {};
|
||||
let resultTablesArray = [];
|
||||
|
||||
if (options.nestTables === true) {
|
||||
for (i = 0; i < fields.length; i++) {
|
||||
resultTables[fields[i].table] = 1;
|
||||
}
|
||||
resultTablesArray = Object.keys(resultTables);
|
||||
for (i = 0; i < resultTablesArray.length; i++) {
|
||||
parserFn('this[' + helpers.srcEscape(resultTablesArray[i]) + '] = {};');
|
||||
}
|
||||
}
|
||||
|
||||
parserFn('packet.readInt8();'); // status byte
|
||||
for (i = 0; i < nullBitmapLength; ++i) {
|
||||
parserFn('const nullBitmaskByte' + i + ' = packet.readInt8();');
|
||||
}
|
||||
|
||||
let lvalue = '';
|
||||
let currentFieldNullBit = 4;
|
||||
let nullByteIndex = 0;
|
||||
let fieldName = '';
|
||||
let tableName = '';
|
||||
|
||||
for (i = 0; i < fields.length; i++) {
|
||||
fieldName = helpers.srcEscape(fields[i].name);
|
||||
parserFn('// ' + fieldName + ': ' + typeNames[fields[i].columnType]);
|
||||
|
||||
if (typeof options.nestTables == 'string') {
|
||||
tableName = helpers.srcEscape(fields[i].table);
|
||||
lvalue =
|
||||
'this[' +
|
||||
helpers.srcEscape(
|
||||
fields[i].table + options.nestTables + fields[i].name
|
||||
) +
|
||||
']';
|
||||
} else if (options.nestTables === true) {
|
||||
tableName = helpers.srcEscape(fields[i].table);
|
||||
lvalue = 'this[' + tableName + '][' + fieldName + ']';
|
||||
} else if (options.rowsAsArray) {
|
||||
lvalue = 'result[' + i.toString(10) + ']';
|
||||
} else {
|
||||
lvalue = 'this[' + helpers.srcEscape(fields[i].name) + ']';
|
||||
}
|
||||
|
||||
// TODO: this used to be an optimisation ( if column marked as NOT_NULL don't include code to check null
|
||||
// bitmap at all, but it seems that we can't rely on this flag, see #178
|
||||
// TODO: benchmark performance difference
|
||||
//
|
||||
// if (fields[i].flags & FieldFlags.NOT_NULL) { // don't need to check null bitmap if field can't be null.
|
||||
// result.push(lvalue + ' = ' + readCodeFor(fields[i], config));
|
||||
// } else if (fields[i].columnType == Types.NULL) {
|
||||
// result.push(lvalue + ' = null;');
|
||||
// } else {
|
||||
parserFn(
|
||||
'if (nullBitmaskByte' + nullByteIndex + ' & ' + currentFieldNullBit + ')'
|
||||
);
|
||||
parserFn(lvalue + ' = null;');
|
||||
parserFn('else');
|
||||
parserFn(lvalue + ' = ' + readCodeFor(fields[i], config, options, i));
|
||||
// }
|
||||
currentFieldNullBit *= 2;
|
||||
if (currentFieldNullBit == 0x100) {
|
||||
currentFieldNullBit = 1;
|
||||
nullByteIndex++;
|
||||
}
|
||||
}
|
||||
|
||||
if (options.rowsAsArray) {
|
||||
parserFn('return result;');
|
||||
}
|
||||
|
||||
parserFn('};')('})()');
|
||||
|
||||
/* eslint-enable no-trailing-spaces */
|
||||
/* eslint-enable no-spaced-func */
|
||||
/* eslint-enable no-unexpected-multiline */
|
||||
|
||||
if (config.debug) {
|
||||
helpers.printDebugWithCode(
|
||||
'Compiled binary protocol row parser',
|
||||
parserFn.toString()
|
||||
);
|
||||
}
|
||||
return parserFn.toFunction();
|
||||
}
|
||||
|
||||
function getBinaryParser(fields, options, config) {
|
||||
return parserCache.getParser('binary', fields, options, config, compile);
|
||||
}
|
||||
|
||||
module.exports = getBinaryParser;
|
||||
55
node_modules/mysql2/lib/parsers/parser_cache.js
generated
vendored
Normal file
55
node_modules/mysql2/lib/parsers/parser_cache.js
generated
vendored
Normal file
@@ -0,0 +1,55 @@
|
||||
'use strict';
|
||||
|
||||
const LRU = require('lru-cache');
|
||||
|
||||
const parserCache = new LRU({
|
||||
max: 15000
|
||||
});
|
||||
|
||||
function keyFromFields(type, fields, options) {
|
||||
let res =
|
||||
type +
|
||||
'/' +
|
||||
typeof options.nestTables +
|
||||
'/' +
|
||||
options.nestTables +
|
||||
'/' +
|
||||
options.rowsAsArray +
|
||||
options.supportBigNumbers +
|
||||
'/' +
|
||||
options.bigNumberStrings +
|
||||
'/' +
|
||||
typeof options.typeCast;
|
||||
for (let i = 0; i < fields.length; ++i) {
|
||||
res +=
|
||||
'/' + fields[i].name + ':' + fields[i].columnType + ':' + fields[i].flags;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
function getParser(type, fields, options, config, compiler) {
|
||||
const key = keyFromFields(type, fields, options);
|
||||
let parser = parserCache.get(key);
|
||||
|
||||
if (parser) {
|
||||
return parser;
|
||||
}
|
||||
|
||||
parser = compiler(fields, options, config);
|
||||
parserCache.set(key, parser);
|
||||
return parser;
|
||||
}
|
||||
|
||||
function setMaxCache(max) {
|
||||
parserCache.max = max;
|
||||
}
|
||||
|
||||
function clearCache() {
|
||||
parserCache.reset();
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getParser: getParser,
|
||||
setMaxCache: setMaxCache,
|
||||
clearCache: clearCache
|
||||
};
|
||||
29
node_modules/mysql2/lib/parsers/string.js
generated
vendored
Normal file
29
node_modules/mysql2/lib/parsers/string.js
generated
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
'use strict';
|
||||
|
||||
const Iconv = require('iconv-lite');
|
||||
|
||||
exports.decode = function(buffer, encoding, options) {
|
||||
if (Buffer.isEncoding(encoding)) {
|
||||
return buffer.toString(encoding);
|
||||
}
|
||||
|
||||
const decoder = Iconv.getDecoder(encoding, options || {});
|
||||
|
||||
const res = decoder.write(buffer);
|
||||
const trail = decoder.end();
|
||||
|
||||
return trail ? res + trail : res;
|
||||
};
|
||||
|
||||
exports.encode = function(string, encoding, options) {
|
||||
if (Buffer.isEncoding(encoding)) {
|
||||
return Buffer.from(string, encoding);
|
||||
}
|
||||
|
||||
const encoder = Iconv.getEncoder(encoding, options || {});
|
||||
|
||||
const res = encoder.write(string);
|
||||
const trail = encoder.end();
|
||||
|
||||
return trail && trail.length > 0 ? Buffer.concat([res, trail]) : res;
|
||||
};
|
||||
203
node_modules/mysql2/lib/parsers/text_parser.js
generated
vendored
Normal file
203
node_modules/mysql2/lib/parsers/text_parser.js
generated
vendored
Normal file
@@ -0,0 +1,203 @@
|
||||
'use strict';
|
||||
|
||||
const Types = require('../constants/types.js');
|
||||
const Charsets = require('../constants/charsets.js');
|
||||
const helpers = require('../helpers');
|
||||
const genFunc = require('generate-function');
|
||||
const parserCache = require('./parser_cache.js');
|
||||
|
||||
const typeNames = [];
|
||||
for (const t in Types) {
|
||||
typeNames[Types[t]] = t;
|
||||
}
|
||||
|
||||
function readCodeFor(type, charset, encodingExpr, config, options) {
|
||||
const supportBigNumbers =
|
||||
options.supportBigNumbers || config.supportBigNumbers;
|
||||
const bigNumberStrings = options.bigNumberStrings || config.bigNumberStrings;
|
||||
|
||||
switch (type) {
|
||||
case Types.TINY:
|
||||
case Types.SHORT:
|
||||
case Types.LONG:
|
||||
case Types.INT24:
|
||||
case Types.YEAR:
|
||||
return 'packet.parseLengthCodedIntNoBigCheck()';
|
||||
case Types.LONGLONG:
|
||||
if (supportBigNumbers && bigNumberStrings) {
|
||||
return 'packet.parseLengthCodedIntString()';
|
||||
}
|
||||
return 'packet.parseLengthCodedInt(' + supportBigNumbers + ')';
|
||||
case Types.FLOAT:
|
||||
case Types.DOUBLE:
|
||||
return 'packet.parseLengthCodedFloat()';
|
||||
case Types.NULL:
|
||||
return 'packet.readLengthCodedNumber()';
|
||||
case Types.DECIMAL:
|
||||
case Types.NEWDECIMAL:
|
||||
if (config.decimalNumbers) {
|
||||
return 'packet.parseLengthCodedFloat()';
|
||||
}
|
||||
return 'packet.readLengthCodedString("ascii")';
|
||||
case Types.DATE:
|
||||
if (config.dateStrings) {
|
||||
return 'packet.readLengthCodedString("ascii")';
|
||||
}
|
||||
return 'packet.parseDate()';
|
||||
case Types.DATETIME:
|
||||
case Types.TIMESTAMP:
|
||||
if (config.dateStrings) {
|
||||
return 'packet.readLengthCodedString("ascii")';
|
||||
}
|
||||
return 'packet.parseDateTime()';
|
||||
case Types.TIME:
|
||||
return 'packet.readLengthCodedString("ascii")';
|
||||
case Types.GEOMETRY:
|
||||
return 'packet.parseGeometryValue()';
|
||||
case Types.JSON:
|
||||
// Since for JSON columns mysql always returns charset 63 (BINARY),
|
||||
// we have to handle it according to JSON specs and use "utf8",
|
||||
// see https://github.com/sidorares/node-mysql2/issues/409
|
||||
return 'JSON.parse(packet.readLengthCodedString("utf8"))';
|
||||
default:
|
||||
if (charset == Charsets.BINARY) {
|
||||
return 'packet.readLengthCodedBuffer()';
|
||||
} else {
|
||||
return 'packet.readLengthCodedString(' + encodingExpr + ')';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function compile(fields, options, config) {
|
||||
// node-mysql typeCast compatibility wrapper
|
||||
// see https://github.com/mysqljs/mysql/blob/96fdd0566b654436624e2375c7b6604b1f50f825/lib/protocol/packets/Field.js
|
||||
function wrap(field, type, packet, encoding) {
|
||||
return {
|
||||
type: type,
|
||||
length: field.columnLength,
|
||||
db: field.schema,
|
||||
table: field.table,
|
||||
name: field.name,
|
||||
string: function() {
|
||||
return packet.readLengthCodedString(encoding);
|
||||
},
|
||||
buffer: function() {
|
||||
return packet.readLengthCodedBuffer();
|
||||
},
|
||||
geometry: function() {
|
||||
return packet.parseGeometryValue();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// use global typeCast if current query doesn't specify one
|
||||
if (
|
||||
typeof config.typeCast === 'function' &&
|
||||
typeof options.typeCast !== 'function'
|
||||
) {
|
||||
options.typeCast = config.typeCast;
|
||||
}
|
||||
|
||||
const parserFn = genFunc();
|
||||
let i = 0;
|
||||
|
||||
/* eslint-disable no-trailing-spaces */
|
||||
/* eslint-disable no-spaced-func */
|
||||
/* eslint-disable no-unexpected-multiline */
|
||||
parserFn('(function () {')(
|
||||
'return function TextRow(packet, fields, options, CharsetToEncoding) {'
|
||||
);
|
||||
|
||||
if (options.rowsAsArray) {
|
||||
parserFn('const result = new Array(' + fields.length + ')');
|
||||
}
|
||||
|
||||
if (typeof options.typeCast === 'function') {
|
||||
parserFn('const wrap = ' + wrap.toString());
|
||||
}
|
||||
|
||||
const resultTables = {};
|
||||
let resultTablesArray = [];
|
||||
|
||||
if (options.nestTables === true) {
|
||||
for (i = 0; i < fields.length; i++) {
|
||||
resultTables[fields[i].table] = 1;
|
||||
}
|
||||
resultTablesArray = Object.keys(resultTables);
|
||||
for (i = 0; i < resultTablesArray.length; i++) {
|
||||
parserFn('this[' + helpers.srcEscape(resultTablesArray[i]) + '] = {};');
|
||||
}
|
||||
}
|
||||
|
||||
let lvalue = '';
|
||||
let fieldName = '';
|
||||
for (i = 0; i < fields.length; i++) {
|
||||
fieldName = helpers.srcEscape(fields[i].name);
|
||||
parserFn('// ' + fieldName + ': ' + typeNames[fields[i].columnType]);
|
||||
if (typeof options.nestTables == 'string') {
|
||||
lvalue =
|
||||
'this[' +
|
||||
helpers.srcEscape(
|
||||
fields[i].table + options.nestTables + fields[i].name
|
||||
) +
|
||||
']';
|
||||
} else if (options.nestTables === true) {
|
||||
lvalue =
|
||||
'this[' + helpers.srcEscape(fields[i].table) + '][' + fieldName + ']';
|
||||
} else if (options.rowsAsArray) {
|
||||
lvalue = 'result[' + i.toString(10) + ']';
|
||||
} else {
|
||||
lvalue = 'this[' + fieldName + ']';
|
||||
}
|
||||
const encodingExpr = 'CharsetToEncoding[fields[' + i + '].characterSet]';
|
||||
const readCode = readCodeFor(
|
||||
fields[i].columnType,
|
||||
fields[i].characterSet,
|
||||
encodingExpr,
|
||||
config,
|
||||
options
|
||||
);
|
||||
if (typeof options.typeCast === 'function') {
|
||||
parserFn(
|
||||
lvalue +
|
||||
' = options.typeCast(wrap(fields[' +
|
||||
i +
|
||||
'], ' +
|
||||
helpers.srcEscape(typeNames[fields[i].columnType]) +
|
||||
', packet, ' +
|
||||
encodingExpr +
|
||||
'), function() { return ' +
|
||||
readCode +
|
||||
';})'
|
||||
);
|
||||
} else if (options.typeCast === false) {
|
||||
parserFn(lvalue + ' = packet.readLengthCodedBuffer();');
|
||||
} else {
|
||||
parserFn(lvalue + ' = ' + readCode + ';');
|
||||
}
|
||||
}
|
||||
|
||||
if (options.rowsAsArray) {
|
||||
parserFn('return result;');
|
||||
}
|
||||
|
||||
parserFn('};')('})()');
|
||||
|
||||
/* eslint-enable no-trailing-spaces */
|
||||
/* eslint-enable no-spaced-func */
|
||||
/* eslint-enable no-unexpected-multiline */
|
||||
|
||||
if (config.debug) {
|
||||
helpers.printDebugWithCode(
|
||||
'Compiled text protocol row parser',
|
||||
parserFn.toString()
|
||||
);
|
||||
}
|
||||
return parserFn.toFunction();
|
||||
}
|
||||
|
||||
function getTextParser(fields, options, config) {
|
||||
return parserCache.getParser('text', fields, options, config, compile);
|
||||
}
|
||||
|
||||
module.exports = getTextParser;
|
||||
Reference in New Issue
Block a user