mirror of
https://github.com/darksaid98/oxmysql.git
synced 2026-12-01 22:21:51 +00:00
feat(rawExecute): "prepare" alternative without unpacking
See #179. Always return an array of row results (or null). Parameters are now optional (though preferred).
This commit is contained in:
1 parent
fe9c684bf0
commit
e8f1abcab7
4 files changed
+43
-13
No files matched your search
@@ -28,6 +28,7 @@ interface OxMySQL {
|
||||
update: <T = number | null> (query: Query, params?: Params | Callback<T>, cb?: Callback<T>) => Promise<T>
|
||||
insert: <T = number | null> (query: Query, params?: Params | Callback<T>, cb?: Callback<T>) => Promise<T>
|
||||
prepare: <T = any> (query: Query, params?: Params | Callback<T>, cb?: Callback<T>) => Promise<T>
|
||||
rawExecute: <T = Result | null> (query: Query, params?: Params | Callback<T>, cb?: Callback<T>) => Promise<T>
|
||||
transaction: (query: Transaction, params?: Params | Callback<boolean>, cb?: Callback<boolean>) => Promise<boolean>
|
||||
}
|
||||
|
||||
@@ -137,6 +138,11 @@ export const oxmysql: OxMySQL = {
|
||||
const result = await execute('prepare', query, params)
|
||||
return cb ? cb(result) : result
|
||||
},
|
||||
async rawExecute(query, params, cb) {
|
||||
[query, params, cb] = safeArgs(query, params, cb)
|
||||
const result = await execute('rawExecute', query, params)
|
||||
return cb ? cb(result) : result
|
||||
},
|
||||
async transaction(query, params, cb) {
|
||||
[query, params, cb] = safeArgs(query, params, cb, true)
|
||||
const result = await execute('transaction', query, params)
|
||||
|
||||
+13
-2
@@ -102,15 +102,26 @@ function MySQL.transaction.await(queries, parameters) end
|
||||
MySQL.Sync.transaction = MySQL.transaction.await
|
||||
|
||||
---@param query string
|
||||
---@param parameters table
|
||||
---@param parameters? table
|
||||
---@param cb? fun(result?: MySQLColumn | MySQLRow | MySQLQuery)
|
||||
function MySQL.prepare(query, parameters, cb) end
|
||||
|
||||
MySQL.Async.prepare = MySQL.prepare
|
||||
|
||||
---@param query string
|
||||
---@param parameters table
|
||||
---@param parameters? table
|
||||
---@return MySQLColumn | MySQLRow | MySQLQuery | nil result
|
||||
function MySQL.prepare.await(query, parameters) end
|
||||
|
||||
MySQL.Sync.prepare = MySQL.prepare.await
|
||||
|
||||
---@param query string
|
||||
---@param parameters? table
|
||||
---@param cb? fun(result?: MySQLQuery)
|
||||
function MySQL.rawExecute(query, parameters, cb) end
|
||||
|
||||
---@param query string
|
||||
---@param parameters? table
|
||||
---@return MySQLQuery? result
|
||||
function MySQL.rawExecute.await(query, parameters) end
|
||||
|
||||
+14
-11
@@ -12,7 +12,8 @@ export const rawExecute = (
|
||||
query: string,
|
||||
parameters: CFXParameters,
|
||||
cb?: CFXCallback,
|
||||
throwError?: boolean
|
||||
throwError?: boolean,
|
||||
unpack?: boolean
|
||||
) => {
|
||||
if (typeof query !== 'string')
|
||||
throw new Error(
|
||||
@@ -21,8 +22,7 @@ export const rawExecute = (
|
||||
|
||||
const type = executeType(query);
|
||||
const placeholders = query.split('?').length - 1;
|
||||
parameters = parseExecute(placeholders, parameters);
|
||||
if (parameters.length === 0) throw new Error(`Query received no parameters.`);
|
||||
parameters = parameters ? parseExecute(placeholders, parameters) : [];
|
||||
let response = [] as any;
|
||||
|
||||
scheduleTick();
|
||||
@@ -33,10 +33,13 @@ export const rawExecute = (
|
||||
pool.getConnection((err, connection) => {
|
||||
if (err) return reject(err.message);
|
||||
|
||||
parameters.forEach((values, index) => {
|
||||
const executionTime = process.hrtime();
|
||||
const parametersLength = parameters.length == 0 ? 1 : parameters.length;
|
||||
|
||||
if (placeholders > values.length) {
|
||||
for (let index = 0; index < parametersLength; index++) {
|
||||
const executionTime = process.hrtime();
|
||||
const values = parameters[index];
|
||||
|
||||
if (values && placeholders > values.length) {
|
||||
for (let i = values.length; i < placeholders; i++) {
|
||||
values[i] = null;
|
||||
}
|
||||
@@ -58,15 +61,15 @@ export const rawExecute = (
|
||||
|
||||
logQuery(invokingResource, query, process.hrtime(executionTime)[1] / 1e6, values as typeof parameters);
|
||||
|
||||
if (index === parameters.length - 1) {
|
||||
if (index === parametersLength - 1) {
|
||||
connection.release();
|
||||
|
||||
if (cb) {
|
||||
if (response.length === 1) {
|
||||
if (type === null) {
|
||||
if (response[0][0] && Object.keys(response[0][0]).length === 1)
|
||||
if (unpack && type === null) {
|
||||
if (response[0][0] && Object.keys(response[0][0]).length === 1) {
|
||||
resolve(Object.values(response[0][0])[0]);
|
||||
else resolve(response[0][0]);
|
||||
} else resolve(response[0][0]);
|
||||
} else {
|
||||
resolve(response[0]);
|
||||
}
|
||||
@@ -76,7 +79,7 @@ export const rawExecute = (
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
})
|
||||
.then(async (response) => {
|
||||
|
||||
@@ -71,6 +71,16 @@ MySQL.prepare = (
|
||||
cb: CFXCallback,
|
||||
invokingResource = GetInvokingResource(),
|
||||
throwError?: boolean
|
||||
) => {
|
||||
rawExecute(invokingResource, query, parameters, cb, throwError, true);
|
||||
};
|
||||
|
||||
MySQL.rawExecute = (
|
||||
query: string,
|
||||
parameters: CFXParameters,
|
||||
cb: CFXCallback,
|
||||
invokingResource = GetInvokingResource(),
|
||||
throwError?: boolean
|
||||
) => {
|
||||
rawExecute(invokingResource, query, parameters, cb, throwError);
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user