mirror of
https://github.com/darksaid98/oxmysql.git
synced 2026-12-01 22:21:51 +00:00
refactor(patches): remove redundant code
mysql2 calls some code, namely _resolveNamedPlaceholders, that is handled by oxmysql before the query is executed. Other changes: Calculate query execution time as part of the Query command. Check for undefined parameters during placeholder resolution.
This commit is contained in:
1 parent
4e29905179
commit
b7a17d9566
2 files changed
+270
-4
No files matched your search
@@ -1,3 +1,149 @@
|
||||
diff --git a/node_modules/mysql2/index.d.ts b/node_modules/mysql2/index.d.ts
|
||||
index a465b43..183dc97 100644
|
||||
--- a/node_modules/mysql2/index.d.ts
|
||||
+++ b/node_modules/mysql2/index.d.ts
|
||||
@@ -166,7 +166,6 @@ export interface ConnectionOptions extends mysql.ConnectionOptions {
|
||||
decimalNumbers?: boolean;
|
||||
isServer?: boolean;
|
||||
maxPreparedStatements?: number;
|
||||
- namedPlaceholders?: boolean;
|
||||
nestTables?: boolean | string;
|
||||
passwordSha1?: string;
|
||||
pool?: any;
|
||||
diff --git a/node_modules/mysql2/lib/commands/query.js b/node_modules/mysql2/lib/commands/query.js
|
||||
index e51da3c..a60811a 100644
|
||||
--- a/node_modules/mysql2/lib/commands/query.js
|
||||
+++ b/node_modules/mysql2/lib/commands/query.js
|
||||
@@ -1,6 +1,5 @@
|
||||
'use strict';
|
||||
|
||||
-const process = require('process');
|
||||
const Timers = require('timers');
|
||||
|
||||
const Readable = require('stream').Readable;
|
||||
@@ -19,7 +18,6 @@ class Query extends Command {
|
||||
this.sql = options.sql;
|
||||
this.values = options.values;
|
||||
this._queryOptions = options;
|
||||
- this.namedPlaceholders = options.namedPlaceholders || false;
|
||||
this.onResult = callback;
|
||||
this.timeout = options.timeout;
|
||||
this.queryTimeout = null;
|
||||
@@ -52,6 +50,7 @@ class Query extends Command {
|
||||
this._connection = connection;
|
||||
this.options = Object.assign({}, connection.config, this._queryOptions);
|
||||
this._setTimeout();
|
||||
+ this._time = process.hrtime()
|
||||
|
||||
const cmdPacket = new Packets.Query(
|
||||
this.sql,
|
||||
@@ -73,7 +72,7 @@ class Query extends Command {
|
||||
this.queryTimeout = null;
|
||||
}
|
||||
if (this.onResult) {
|
||||
- let rows, fields;
|
||||
+ let rows, fields, time;
|
||||
if (this._resultIndex === 0) {
|
||||
rows = this._rows[0];
|
||||
fields = this._fields[0];
|
||||
@@ -81,13 +80,14 @@ class Query extends Command {
|
||||
rows = this._rows;
|
||||
fields = this._fields;
|
||||
}
|
||||
+ if (this._time) time = process.hrtime(this._time)[1] / 1000000
|
||||
if (fields) {
|
||||
process.nextTick(() => {
|
||||
- this.onResult(null, rows, fields);
|
||||
+ this.onResult(null, rows, fields, time);
|
||||
});
|
||||
} else {
|
||||
process.nextTick(() => {
|
||||
- this.onResult(null, rows);
|
||||
+ this.onResult(null, rows, undefined, time);
|
||||
});
|
||||
}
|
||||
}
|
||||
diff --git a/node_modules/mysql2/lib/connection.js b/node_modules/mysql2/lib/connection.js
|
||||
index 47970e9..a0fec80 100644
|
||||
--- a/node_modules/mysql2/lib/connection.js
|
||||
+++ b/node_modules/mysql2/lib/connection.js
|
||||
@@ -27,8 +27,6 @@ const CharsetToEncoding = require('./constants/charset_encodings.js');
|
||||
|
||||
let _connectionId = 0;
|
||||
|
||||
-let convertNamedPlaceholders = null;
|
||||
-
|
||||
class Connection extends EventEmitter {
|
||||
constructor(opts) {
|
||||
super();
|
||||
@@ -495,7 +493,6 @@ class Connection extends EventEmitter {
|
||||
sql: sql,
|
||||
values: values
|
||||
};
|
||||
- this._resolveNamedPlaceholders(opts);
|
||||
return SqlString.format(
|
||||
opts.sql,
|
||||
opts.values,
|
||||
@@ -516,23 +513,6 @@ class Connection extends EventEmitter {
|
||||
return SqlString.raw(sql);
|
||||
}
|
||||
|
||||
- _resolveNamedPlaceholders(options) {
|
||||
- let unnamed;
|
||||
- if (this.config.namedPlaceholders || options.namedPlaceholders) {
|
||||
- if (Array.isArray(options.values)) {
|
||||
- // if an array is provided as the values, assume the conversion is not necessary.
|
||||
- // this allows the usage of unnamed placeholders even if the namedPlaceholders flag is enabled.
|
||||
- return
|
||||
- }
|
||||
- if (convertNamedPlaceholders === null) {
|
||||
- convertNamedPlaceholders = require('named-placeholders')();
|
||||
- }
|
||||
- unnamed = convertNamedPlaceholders(options.sql, options.values);
|
||||
- options.sql = unnamed[0];
|
||||
- options.values = unnamed[1];
|
||||
- }
|
||||
- }
|
||||
-
|
||||
query(sql, values, cb) {
|
||||
let cmdQuery;
|
||||
if (sql.constructor === Commands.Query) {
|
||||
@@ -540,7 +520,6 @@ class Connection extends EventEmitter {
|
||||
} else {
|
||||
cmdQuery = Connection.createQuery(sql, values, cb, this.config);
|
||||
}
|
||||
- this._resolveNamedPlaceholders(cmdQuery);
|
||||
const rawSql = this.format(cmdQuery.sql, cmdQuery.values !== undefined ? cmdQuery.values : []);
|
||||
cmdQuery.sql = rawSql;
|
||||
return this.addCommand(cmdQuery);
|
||||
@@ -608,26 +587,11 @@ class Connection extends EventEmitter {
|
||||
options.sql = sql;
|
||||
options.values = values;
|
||||
}
|
||||
- this._resolveNamedPlaceholders(options);
|
||||
// check for values containing undefined
|
||||
if (options.values) {
|
||||
- //If namedPlaceholder is not enabled and object is passed as bind parameters
|
||||
- if (!Array.isArray(options.values)) {
|
||||
- throw new TypeError(
|
||||
- 'Bind parameters must be array if namedPlaceholders parameter is not enabled'
|
||||
- );
|
||||
- }
|
||||
options.values.forEach(val => {
|
||||
- //If namedPlaceholder is not enabled and object is passed as bind parameters
|
||||
- if (!Array.isArray(options.values)) {
|
||||
- throw new TypeError(
|
||||
- 'Bind parameters must be array if namedPlaceholders parameter is not enabled'
|
||||
- );
|
||||
- }
|
||||
if (val === undefined) {
|
||||
- throw new TypeError(
|
||||
- 'Bind parameters must not contain undefined. To pass SQL NULL specify JS null'
|
||||
- );
|
||||
+ val = null
|
||||
}
|
||||
if (typeof val === 'function') {
|
||||
throw new TypeError(
|
||||
diff --git a/node_modules/mysql2/lib/connection_config.js b/node_modules/mysql2/lib/connection_config.js
|
||||
index 11ad01b..6fab74e 100644
|
||||
--- a/node_modules/mysql2/lib/connection_config.js
|
||||
@@ -45,3 +191,121 @@ index c612f66..02dbe22 100644
|
||||
|
||||
case 'number':
|
||||
type = Types.DOUBLE;
|
||||
diff --git a/node_modules/mysql2/promise.d.ts b/node_modules/mysql2/promise.d.ts
|
||||
index 6ab3d6f..68bc9cb 100644
|
||||
--- a/node_modules/mysql2/promise.d.ts
|
||||
+++ b/node_modules/mysql2/promise.d.ts
|
||||
@@ -25,41 +25,15 @@ export interface Connection extends EventEmitter {
|
||||
changeUser(options: ConnectionOptions): Promise<void>;
|
||||
|
||||
query<T extends RowDataPacket[][] | RowDataPacket[] | OkPacket | OkPacket[] | ResultSetHeader>(
|
||||
- sql: string
|
||||
- ): Promise<[T, FieldPacket[]]>;
|
||||
- query<T extends RowDataPacket[][] | RowDataPacket[] | OkPacket | OkPacket[] | ResultSetHeader>(
|
||||
- sql: string,
|
||||
- values: any | any[] | { [param: string]: any }
|
||||
- ): Promise<[T, FieldPacket[]]>;
|
||||
- query<T extends RowDataPacket[][] | RowDataPacket[] | OkPacket | OkPacket[] | ResultSetHeader>(
|
||||
- options: QueryOptions
|
||||
- ): Promise<[T, FieldPacket[]]>;
|
||||
- query<T extends RowDataPacket[][] | RowDataPacket[] | OkPacket | OkPacket[] | ResultSetHeader>(
|
||||
- options: QueryOptions,
|
||||
- values: any | any[] | { [param: string]: any }
|
||||
+ sql_or_options: string | QueryOptions,
|
||||
+ values?: any | any[] | { [param: string]: any },
|
||||
): Promise<[T, FieldPacket[]]>;
|
||||
|
||||
execute<
|
||||
T extends RowDataPacket[][] | RowDataPacket[] | OkPacket | OkPacket[] | ResultSetHeader
|
||||
>(
|
||||
- sql: string
|
||||
- ): Promise<[T, FieldPacket[]]>;
|
||||
- execute<
|
||||
- T extends RowDataPacket[][] | RowDataPacket[] | OkPacket | OkPacket[] | ResultSetHeader
|
||||
- >(
|
||||
- sql: string,
|
||||
- values: any | any[] | { [param: string]: any }
|
||||
- ): Promise<[T, FieldPacket[]]>;
|
||||
- execute<
|
||||
- T extends RowDataPacket[][] | RowDataPacket[] | OkPacket | OkPacket[] | ResultSetHeader
|
||||
- >(
|
||||
- options: QueryOptions
|
||||
- ): Promise<[T, FieldPacket[]]>;
|
||||
- execute<
|
||||
- T extends RowDataPacket[][] | RowDataPacket[] | OkPacket | OkPacket[] | ResultSetHeader
|
||||
- >(
|
||||
- options: QueryOptions,
|
||||
- values: any | any[] | { [param: string]: any }
|
||||
+ sql_or_options: string | QueryOptions,
|
||||
+ values?: any | any[] | { [param: string]: any },
|
||||
): Promise<[T, FieldPacket[]]>;
|
||||
|
||||
unprepare(sql: string): void;
|
||||
@@ -86,41 +60,15 @@ export interface PoolConnection extends Connection {
|
||||
|
||||
export interface Pool extends EventEmitter {
|
||||
query<T extends RowDataPacket[][] | RowDataPacket[] | OkPacket | OkPacket[] | ResultSetHeader>(
|
||||
- sql: string
|
||||
- ): Promise<[T, FieldPacket[]]>;
|
||||
- query<T extends RowDataPacket[][] | RowDataPacket[] | OkPacket | OkPacket[] | ResultSetHeader>(
|
||||
- sql: string,
|
||||
- values: any | any[] | { [param: string]: any }
|
||||
- ): Promise<[T, FieldPacket[]]>;
|
||||
- query<T extends RowDataPacket[][] | RowDataPacket[] | OkPacket | OkPacket[] | ResultSetHeader>(
|
||||
- options: QueryOptions
|
||||
- ): Promise<[T, FieldPacket[]]>;
|
||||
- query<T extends RowDataPacket[][] | RowDataPacket[] | OkPacket | OkPacket[] | ResultSetHeader>(
|
||||
- options: QueryOptions,
|
||||
- values: any | any[] | { [param: string]: any }
|
||||
+ sql_or_options: string | QueryOptions,
|
||||
+ values?: any | any[] | { [param: string]: any },
|
||||
): Promise<[T, FieldPacket[]]>;
|
||||
|
||||
execute<
|
||||
T extends RowDataPacket[][] | RowDataPacket[] | OkPacket | OkPacket[] | ResultSetHeader
|
||||
>(
|
||||
- sql: string
|
||||
- ): Promise<[T, FieldPacket[]]>;
|
||||
- execute<
|
||||
- T extends RowDataPacket[][] | RowDataPacket[] | OkPacket | OkPacket[] | ResultSetHeader
|
||||
- >(
|
||||
- sql: string,
|
||||
- values: any | any[] | { [param: string]: any }
|
||||
- ): Promise<[T, FieldPacket[]]>;
|
||||
- execute<
|
||||
- T extends RowDataPacket[][] | RowDataPacket[] | OkPacket | OkPacket[] | ResultSetHeader
|
||||
- >(
|
||||
- options: QueryOptions
|
||||
- ): Promise<[T, FieldPacket[]]>;
|
||||
- execute<
|
||||
- T extends RowDataPacket[][] | RowDataPacket[] | OkPacket | OkPacket[] | ResultSetHeader
|
||||
- >(
|
||||
- options: QueryOptions,
|
||||
- values: any | any[] | { [param: string]: any }
|
||||
+ sql_or_options: string | QueryOptions,
|
||||
+ values?: any | any[] | { [param: string]: any },
|
||||
): Promise<[T, FieldPacket[]]>;
|
||||
|
||||
getConnection(): Promise<PoolConnection>;
|
||||
diff --git a/node_modules/mysql2/promise.js b/node_modules/mysql2/promise.js
|
||||
index fc74cc0..fea1ccf 100644
|
||||
--- a/node_modules/mysql2/promise.js
|
||||
+++ b/node_modules/mysql2/promise.js
|
||||
@@ -4,7 +4,7 @@ const core = require('./index.js');
|
||||
const EventEmitter = require('events').EventEmitter;
|
||||
|
||||
function makeDoneCb(resolve, reject, localErr) {
|
||||
- return function (err, rows, fields) {
|
||||
+ return function (err, rows, fields, time) {
|
||||
if (err) {
|
||||
localErr.message = err.message;
|
||||
localErr.code = err.code;
|
||||
@@ -14,7 +14,7 @@ function makeDoneCb(resolve, reject, localErr) {
|
||||
localErr.sqlMessage = err.sqlMessage;
|
||||
reject(localErr);
|
||||
} else {
|
||||
- resolve([rows, fields]);
|
||||
+ resolve([rows, fields, time]);
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
diff --git a/node_modules/named-placeholders/index.js b/node_modules/named-placeholders/index.js
|
||||
index 527d723..728188b 100644
|
||||
index 527d723..53ef4cf 100644
|
||||
--- a/node_modules/named-placeholders/index.js
|
||||
+++ b/node_modules/named-placeholders/index.js
|
||||
@@ -3,7 +3,7 @@
|
||||
@@ -11,7 +11,7 @@ index 527d723..728188b 100644
|
||||
DQUOTE = 34,
|
||||
SQUOTE = 39,
|
||||
BSLASH = 92;
|
||||
@@ -94,6 +94,14 @@ function createCompiler(config) {
|
||||
@@ -94,15 +94,24 @@ function createCompiler(config) {
|
||||
if (typeof params == 'undefined')
|
||||
throw new Error('Named query contains placeholders, but parameters object is undefined');
|
||||
|
||||
@@ -25,8 +25,10 @@ index 527d723..728188b 100644
|
||||
+
|
||||
const tokens = tree[1];
|
||||
for (let i=0; i < tokens.length; ++i) {
|
||||
arr.push(params[tokens[i]]);
|
||||
@@ -102,7 +110,8 @@ function createCompiler(config) {
|
||||
- arr.push(params[tokens[i]]);
|
||||
+ arr.push(params[tokens[i]] === undefined ? null : params[tokens[i]]);
|
||||
}
|
||||
return [tree[0], arr];
|
||||
}
|
||||
|
||||
function noTailingSemicolon(s) {
|
||||
|
||||
Reference in new issue
Block a user