API inicial
This commit is contained in:
96
node_modules/mariadb/lib/misc/connection-information.js
generated
vendored
Normal file
96
node_modules/mariadb/lib/misc/connection-information.js
generated
vendored
Normal file
@@ -0,0 +1,96 @@
|
||||
'use strict';
|
||||
const Queue = require('denque');
|
||||
|
||||
const _addPacket = function (msg) {
|
||||
this.lastPackets.push(msg);
|
||||
while (this.lastPackets.size() > 32) this.lastPackets.shift();
|
||||
};
|
||||
|
||||
const _getLastPackets = function () {
|
||||
let output = '';
|
||||
let packet;
|
||||
while ((packet = this.lastPackets.shift())) {
|
||||
output += '\n' + packet;
|
||||
}
|
||||
return output;
|
||||
};
|
||||
|
||||
class ConnectionInformation {
|
||||
constructor() {
|
||||
this.threadId = -1;
|
||||
this.status = null;
|
||||
this.serverVersion = null;
|
||||
this.serverCapabilities = null;
|
||||
}
|
||||
|
||||
addPacket(msg) {}
|
||||
|
||||
getLastPackets() {
|
||||
return '';
|
||||
}
|
||||
|
||||
enableLogPacket() {
|
||||
this.lastPackets = new Queue();
|
||||
this.addPacket = _addPacket.bind(this);
|
||||
this.getLastPackets = _getLastPackets.bind(this);
|
||||
}
|
||||
|
||||
hasMinVersion(major, minor, patch) {
|
||||
if (!this.serverVersion)
|
||||
throw new Error('cannot know if server version until connection is established');
|
||||
|
||||
if (!major) throw new Error('a major version must be set');
|
||||
|
||||
if (!minor) minor = 0;
|
||||
if (!patch) patch = 0;
|
||||
|
||||
let ver = this.serverVersion;
|
||||
return (
|
||||
ver.major > major ||
|
||||
(ver.major === major && ver.minor > minor) ||
|
||||
(ver.major === major && ver.minor === minor && ver.patch >= patch)
|
||||
);
|
||||
}
|
||||
|
||||
isMariaDB() {
|
||||
if (!this.serverVersion)
|
||||
throw new Error('cannot know if server is MariaDB until connection is established');
|
||||
return this.serverVersion.mariaDb;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse raw info to set server major/minor/patch values
|
||||
* @param info
|
||||
*/
|
||||
static parseVersionString(info) {
|
||||
let car;
|
||||
let offset = 0;
|
||||
let type = 0;
|
||||
let val = 0;
|
||||
|
||||
for (; offset < info.serverVersion.raw.length; offset++) {
|
||||
car = info.serverVersion.raw.charCodeAt(offset);
|
||||
if (car < 48 || car > 57) {
|
||||
switch (type) {
|
||||
case 0:
|
||||
info.serverVersion.major = val;
|
||||
break;
|
||||
case 1:
|
||||
info.serverVersion.minor = val;
|
||||
break;
|
||||
case 2:
|
||||
info.serverVersion.patch = val;
|
||||
return;
|
||||
}
|
||||
type++;
|
||||
val = 0;
|
||||
} else {
|
||||
val = val * 10 + car - 48;
|
||||
}
|
||||
}
|
||||
//serverVersion finished by number like "5.5.57", assign patchVersion
|
||||
if (type === 2) info.serverVersion.patch = val;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ConnectionInformation;
|
||||
116
node_modules/mariadb/lib/misc/errors.js
generated
vendored
Normal file
116
node_modules/mariadb/lib/misc/errors.js
generated
vendored
Normal file
@@ -0,0 +1,116 @@
|
||||
'use strict';
|
||||
const ErrorCodes = require('../const/error-code');
|
||||
|
||||
class SqlError extends Error {
|
||||
constructor(msg, fatal, info, sqlState, errno, additionalStack, addHeader) {
|
||||
super(
|
||||
(addHeader === undefined || addHeader
|
||||
? '(conn=' +
|
||||
(info ? (info.threadId ? info.threadId : -1) : -1) +
|
||||
', no: ' +
|
||||
(errno ? errno : -1) +
|
||||
', SQLState: ' +
|
||||
(sqlState ? sqlState : 'HY000') +
|
||||
') '
|
||||
: '') + msg
|
||||
);
|
||||
this.fatal = fatal;
|
||||
this.errno = errno;
|
||||
this.sqlState = sqlState;
|
||||
if (errno > 45000 && errno < 46000) {
|
||||
//driver error
|
||||
this.code = errByNo[errno] || 'UNKNOWN';
|
||||
} else {
|
||||
this.code = ErrorCodes.codes[this.errno] || 'UNKNOWN';
|
||||
}
|
||||
if (additionalStack) {
|
||||
//adding caller stack, removing initial "Error:\n"
|
||||
this.stack +=
|
||||
'\n From event:\n' + additionalStack.substring(additionalStack.indexOf('\n') + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Error factory, so error get connection information.
|
||||
*
|
||||
* @param msg current error message
|
||||
* @param fatal is error fatal
|
||||
* @param info connection information
|
||||
* @param sqlState sql state
|
||||
* @param errno error number
|
||||
* @param additionalStack additional stack trace to see
|
||||
* @param addHeader add connection information
|
||||
* @returns {Error} the error
|
||||
*/
|
||||
module.exports.createError = function (
|
||||
msg,
|
||||
fatal,
|
||||
info,
|
||||
sqlState,
|
||||
errno,
|
||||
additionalStack,
|
||||
addHeader
|
||||
) {
|
||||
return new SqlError(msg, fatal, info, sqlState, errno, additionalStack, addHeader);
|
||||
};
|
||||
|
||||
/********************************************************************************
|
||||
* Driver specific errors
|
||||
********************************************************************************/
|
||||
|
||||
module.exports.ER_CONNECTION_ALREADY_CLOSED = 45001;
|
||||
module.exports.ER_ALREADY_CONNECTING = 45002;
|
||||
module.exports.ER_MYSQL_CHANGE_USER_BUG = 45003;
|
||||
module.exports.ER_CMD_NOT_EXECUTED_DESTROYED = 45004;
|
||||
module.exports.ER_NULL_CHAR_ESCAPEID = 45005;
|
||||
module.exports.ER_NULL_ESCAPEID = 45006;
|
||||
module.exports.ER_NOT_IMPLEMENTED_FORMAT = 45007;
|
||||
module.exports.ER_NODE_NOT_SUPPORTED_TLS = 45008;
|
||||
module.exports.ER_SOCKET_UNEXPECTED_CLOSE = 45009;
|
||||
module.exports.ER_UNEXPECTED_PACKET = 45011;
|
||||
module.exports.ER_CONNECTION_TIMEOUT = 45012;
|
||||
module.exports.ER_CMD_CONNECTION_CLOSED = 45013;
|
||||
module.exports.ER_CHANGE_USER_BAD_PACKET = 45014;
|
||||
module.exports.ER_PING_BAD_PACKET = 45015;
|
||||
module.exports.ER_MISSING_PARAMETER = 45016;
|
||||
module.exports.ER_PARAMETER_UNDEFINED = 45017;
|
||||
module.exports.ER_PLACEHOLDER_UNDEFINED = 45018;
|
||||
module.exports.ER_SOCKET = 45019;
|
||||
module.exports.ER_EOF_EXPECTED = 45020;
|
||||
module.exports.ER_LOCAL_INFILE_DISABLED = 45021;
|
||||
module.exports.ER_LOCAL_INFILE_NOT_READABLE = 45022;
|
||||
module.exports.ER_SERVER_SSL_DISABLED = 45023;
|
||||
module.exports.ER_AUTHENTICATION_BAD_PACKET = 45024;
|
||||
module.exports.ER_AUTHENTICATION_PLUGIN_NOT_SUPPORTED = 45025;
|
||||
module.exports.ER_SOCKET_TIMEOUT = 45026;
|
||||
module.exports.ER_POOL_ALREADY_CLOSED = 45027;
|
||||
module.exports.ER_GET_CONNECTION_TIMEOUT = 45028;
|
||||
module.exports.ER_SETTING_SESSION_ERROR = 45029;
|
||||
module.exports.ER_INITIAL_SQL_ERROR = 45030;
|
||||
module.exports.ER_BATCH_WITH_NO_VALUES = 45031;
|
||||
module.exports.ER_RESET_BAD_PACKET = 45032;
|
||||
module.exports.ER_WRONG_IANA_TIMEZONE = 45033;
|
||||
module.exports.ER_LOCAL_INFILE_WRONG_FILENAME = 45034;
|
||||
module.exports.ER_ADD_CONNECTION_CLOSED_POOL = 45035;
|
||||
module.exports.ER_WRONG_AUTO_TIMEZONE = 45036;
|
||||
module.exports.ER_CLOSING_POOL = 45037;
|
||||
module.exports.ER_TIMEOUT_NOT_SUPPORTED = 45038;
|
||||
module.exports.ER_INITIAL_TIMEOUT_ERROR = 45039;
|
||||
module.exports.ER_DUPLICATE_FIELD = 45040;
|
||||
module.exports.ER_CLIENT_OPTION_INCOMPATIBILITY = 45041;
|
||||
module.exports.ER_PING_TIMEOUT = 45042;
|
||||
module.exports.ER_BAD_PARAMETER_VALUE = 45043;
|
||||
module.exports.ER_CANNOT_RETRIEVE_RSA_KEY = 45044;
|
||||
module.exports.ER_MINIMUM_NODE_VERSION_REQUIRED = 45045;
|
||||
|
||||
const keys = Object.keys(module.exports);
|
||||
const errByNo = {};
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
const keyName = keys[i];
|
||||
if (keyName !== 'createError') {
|
||||
errByNo[module.exports[keyName]] = keyName;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.SqlError = SqlError;
|
||||
1025
node_modules/mariadb/lib/misc/parse.js
generated
vendored
Normal file
1025
node_modules/mariadb/lib/misc/parse.js
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
294
node_modules/mariadb/lib/misc/utils.js
generated
vendored
Normal file
294
node_modules/mariadb/lib/misc/utils.js
generated
vendored
Normal file
@@ -0,0 +1,294 @@
|
||||
'use strict';
|
||||
const Long = require('long');
|
||||
const hexArray = '0123456789ABCDEF'.split('');
|
||||
const Errors = require('../misc/errors');
|
||||
const CommonText = require('../cmd/common-text-cmd');
|
||||
const Iconv = require('iconv-lite');
|
||||
|
||||
/**
|
||||
* Write bytes/hexadecimal value of a byte array to a string.
|
||||
* String output example :
|
||||
* 38 00 00 00 03 63 72 65 61 74 65 20 74 61 62 6C 8....create tabl
|
||||
* 65 20 42 6C 6F 62 54 65 73 74 63 6C 6F 62 74 65 e BlobTestclobte
|
||||
* 73 74 32 20 28 73 74 72 6D 20 74 65 78 74 29 20 st2 (strm text)
|
||||
* 43 48 41 52 53 45 54 20 75 74 66 38 CHARSET utf8
|
||||
*/
|
||||
module.exports.log = function (opts, buf, off, end, header) {
|
||||
let out = [];
|
||||
if (!buf) return '';
|
||||
if (off === undefined || off === null) off = 0;
|
||||
if (end === undefined || end === null) end = buf.length;
|
||||
let asciiValue = new Array(16);
|
||||
asciiValue[8] = ' ';
|
||||
|
||||
let useHeader = header !== undefined;
|
||||
let offset = off || 0;
|
||||
const maxLgh = Math.min(useHeader ? opts.debugLen - header.length : opts.debugLen, end - offset);
|
||||
const isLimited = end - offset > maxLgh;
|
||||
let byteValue;
|
||||
let posHexa = 0;
|
||||
let pos = 0;
|
||||
|
||||
out.push(
|
||||
'+--------------------------------------------------+\n' +
|
||||
'| 0 1 2 3 4 5 6 7 8 9 a b c d e f |\n' +
|
||||
'+--------------------------------------------------+------------------+\n'
|
||||
);
|
||||
|
||||
if (useHeader) {
|
||||
while (pos < header.length) {
|
||||
if (posHexa === 0) out.push('| ');
|
||||
byteValue = header[pos++] & 0xff;
|
||||
out.push(hexArray[byteValue >>> 4], hexArray[byteValue & 0x0f], ' ');
|
||||
asciiValue[posHexa++] =
|
||||
byteValue > 31 && byteValue < 127 ? String.fromCharCode(byteValue) : '.';
|
||||
if (posHexa === 8) out.push(' ');
|
||||
}
|
||||
}
|
||||
|
||||
pos = offset;
|
||||
while (pos < maxLgh + offset) {
|
||||
if (posHexa === 0) out.push('| ');
|
||||
byteValue = buf[pos] & 0xff;
|
||||
|
||||
out.push(hexArray[byteValue >>> 4], hexArray[byteValue & 0x0f], ' ');
|
||||
|
||||
asciiValue[posHexa++] =
|
||||
byteValue > 31 && byteValue < 127 ? String.fromCharCode(byteValue) : '.';
|
||||
|
||||
if (posHexa === 8) out.push(' ');
|
||||
if (posHexa === 16) {
|
||||
out.push('| ', asciiValue.join(''), ' |\n');
|
||||
posHexa = 0;
|
||||
}
|
||||
pos++;
|
||||
}
|
||||
|
||||
let remaining = posHexa;
|
||||
if (remaining > 0) {
|
||||
if (remaining < 8) {
|
||||
for (; remaining < 8; remaining++) {
|
||||
out.push(' ');
|
||||
asciiValue[posHexa++] = ' ';
|
||||
}
|
||||
out.push(' ');
|
||||
}
|
||||
|
||||
for (; remaining < 16; remaining++) {
|
||||
out.push(' ');
|
||||
asciiValue[posHexa++] = ' ';
|
||||
}
|
||||
|
||||
out.push('| ', asciiValue.join(''), isLimited ? ' |...\n' : ' |\n');
|
||||
} else if (isLimited) {
|
||||
out[out.length - 1] = ' |...\n';
|
||||
}
|
||||
out.push('+--------------------------------------------------+------------------+\n');
|
||||
return out.join('');
|
||||
};
|
||||
|
||||
module.exports.escapeId = (opts, info, value) => {
|
||||
if (!value || value === '') {
|
||||
throw Errors.createError(
|
||||
'Cannot escape empty ID value',
|
||||
false,
|
||||
info,
|
||||
'0A000',
|
||||
Errors.ER_NULL_ESCAPEID
|
||||
);
|
||||
}
|
||||
if (value.includes('\u0000')) {
|
||||
throw Errors.createError(
|
||||
'Cannot escape ID with null character (u0000)',
|
||||
false,
|
||||
info,
|
||||
'0A000',
|
||||
Errors.ER_NULL_CHAR_ESCAPEID
|
||||
);
|
||||
}
|
||||
|
||||
// always return escaped value, event when there is no special characters
|
||||
// to permit working with reserved words
|
||||
if (value.match(/^`.+`$/g)) {
|
||||
// already escaped
|
||||
return value;
|
||||
}
|
||||
return '`' + value.replace(/`/g, '``') + '`';
|
||||
};
|
||||
|
||||
module.exports.escape = (opts, info, value) => {
|
||||
if (value === undefined || value === null) return 'NULL';
|
||||
|
||||
switch (typeof value) {
|
||||
case 'boolean':
|
||||
return value ? 'true' : 'false';
|
||||
case 'bigint':
|
||||
case 'number':
|
||||
return '' + value;
|
||||
case 'object':
|
||||
if (Object.prototype.toString.call(value) === '[object Date]') {
|
||||
return opts.tz
|
||||
? opts.tz === 'Etc/UTC'
|
||||
? CommonText.getUtcDate(value, opts)
|
||||
: CommonText.getTimezoneDate(value, opts)
|
||||
: CommonText.getLocalDate(value, opts);
|
||||
} else if (Buffer.isBuffer(value)) {
|
||||
let stValue;
|
||||
if (Buffer.isEncoding(opts.collation.charset)) {
|
||||
stValue = value.toString(opts.collation.charset, 0, value.length);
|
||||
} else {
|
||||
stValue = Iconv.decode(value, opts.collation.charset);
|
||||
}
|
||||
return "_binary'" + escapeString(stValue) + "'";
|
||||
} else if (typeof value.toSqlString === 'function') {
|
||||
return "'" + escapeString(String(value.toSqlString())) + "'";
|
||||
} else if (Long.isLong(value)) {
|
||||
return value.toString();
|
||||
} else if (Array.isArray(value)) {
|
||||
let out = opts.arrayParenthesis ? '(' : '';
|
||||
for (let i = 0; i < value.length; i++) {
|
||||
if (i !== 0) out += ',';
|
||||
out += this.escape(opts, info, value[i]);
|
||||
}
|
||||
if (opts.arrayParenthesis) out += ')';
|
||||
return out;
|
||||
} else {
|
||||
if (
|
||||
value.type != null &&
|
||||
[
|
||||
'Point',
|
||||
'LineString',
|
||||
'Polygon',
|
||||
'MultiPoint',
|
||||
'MultiLineString',
|
||||
'MultiPolygon',
|
||||
'GeometryCollection'
|
||||
].includes(value.type)
|
||||
) {
|
||||
//GeoJSON format.
|
||||
let prefix =
|
||||
info &&
|
||||
((info.isMariaDB() && info.hasMinVersion(10, 1, 4)) ||
|
||||
(!info.isMariaDB() && info.hasMinVersion(5, 7, 6)))
|
||||
? 'ST_'
|
||||
: '';
|
||||
switch (value.type) {
|
||||
case 'Point':
|
||||
return (
|
||||
prefix +
|
||||
"PointFromText('POINT(" +
|
||||
CommonText.geoPointToString(value.coordinates) +
|
||||
")')"
|
||||
);
|
||||
|
||||
case 'LineString':
|
||||
return (
|
||||
prefix +
|
||||
"LineFromText('LINESTRING(" +
|
||||
CommonText.geoArrayPointToString(value.coordinates) +
|
||||
")')"
|
||||
);
|
||||
|
||||
case 'Polygon':
|
||||
return (
|
||||
prefix +
|
||||
"PolygonFromText('POLYGON(" +
|
||||
CommonText.geoMultiArrayPointToString(value.coordinates) +
|
||||
")')"
|
||||
);
|
||||
|
||||
case 'MultiPoint':
|
||||
return (
|
||||
prefix +
|
||||
"MULTIPOINTFROMTEXT('MULTIPOINT(" +
|
||||
CommonText.geoArrayPointToString(value.coordinates) +
|
||||
")')"
|
||||
);
|
||||
|
||||
case 'MultiLineString':
|
||||
return (
|
||||
prefix +
|
||||
"MLineFromText('MULTILINESTRING(" +
|
||||
CommonText.geoMultiArrayPointToString(value.coordinates) +
|
||||
")')"
|
||||
);
|
||||
|
||||
case 'MultiPolygon':
|
||||
return (
|
||||
prefix +
|
||||
"MPolyFromText('MULTIPOLYGON(" +
|
||||
CommonText.geoMultiPolygonToString(value.coordinates) +
|
||||
")')"
|
||||
);
|
||||
|
||||
case 'GeometryCollection':
|
||||
return (
|
||||
prefix +
|
||||
"GeomCollFromText('GEOMETRYCOLLECTION(" +
|
||||
CommonText.geometricCollectionToString(value.geometries) +
|
||||
")')"
|
||||
);
|
||||
}
|
||||
} else {
|
||||
if (opts.permitSetMultiParamEntries) {
|
||||
let out = '';
|
||||
let first = true;
|
||||
for (let key in value) {
|
||||
const val = value[key];
|
||||
if (typeof val === 'function') continue;
|
||||
if (first) {
|
||||
first = false;
|
||||
} else {
|
||||
out += ',';
|
||||
}
|
||||
out += '`' + key + '`=';
|
||||
out += this.escape(opts, info, val);
|
||||
}
|
||||
if (out === '') return "'" + escapeString(JSON.stringify(value)) + "'";
|
||||
return out;
|
||||
} else {
|
||||
return "'" + escapeString(JSON.stringify(value)) + "'";
|
||||
}
|
||||
}
|
||||
}
|
||||
default:
|
||||
return "'" + escapeString(value) + "'";
|
||||
}
|
||||
};
|
||||
|
||||
// see https://mariadb.com/kb/en/library/string-literals/
|
||||
const LITTERAL_ESCAPE = {
|
||||
'\u0000': '\\0',
|
||||
"'": "\\'",
|
||||
'"': '\\"',
|
||||
'\b': '\\b',
|
||||
'\n': '\\n',
|
||||
'\r': '\\r',
|
||||
'\t': '\\t',
|
||||
'\u001A': '\\Z',
|
||||
'\\': '\\\\'
|
||||
};
|
||||
|
||||
const escapeString = (val) => {
|
||||
const pattern = /[\u0000'"\b\n\r\t\u001A\\]/g;
|
||||
|
||||
let offset = 0;
|
||||
let escaped = '';
|
||||
let match;
|
||||
|
||||
while ((match = pattern.exec(val))) {
|
||||
escaped += val.substring(offset, match.index);
|
||||
escaped += LITTERAL_ESCAPE[match[0]];
|
||||
offset = pattern.lastIndex;
|
||||
}
|
||||
|
||||
if (offset === 0) {
|
||||
return val;
|
||||
}
|
||||
|
||||
if (offset < val.length) {
|
||||
escaped += val.substring(offset);
|
||||
}
|
||||
|
||||
return escaped;
|
||||
};
|
||||
Reference in New Issue
Block a user