From 4975a17a31e2888384fdf754e812a7773c027998 Mon Sep 17 00:00:00 2001 From: Linden <65407488+thelindat@users.noreply.github.com> Date: Thu, 10 Feb 2022 16:57:28 +1100 Subject: [PATCH] fix(db/execute): Parse responses during prepare Prepare should return the most appropriate result type (scalar, single, query, update, etc.) --- src/database/rawExecute.ts | 17 +++++++---------- src/types/index.ts | 2 +- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/database/rawExecute.ts b/src/database/rawExecute.ts index de473f6..d396443 100644 --- a/src/database/rawExecute.ts +++ b/src/database/rawExecute.ts @@ -1,8 +1,9 @@ -import { QueryError } from 'mysql2'; +import { QueryError, ResultSetHeader, RowDataPacket } from 'mysql2'; import { pool } from '.'; import { scheduleTick } from '../config'; import { logQuery } from '../logger'; import { CFXCallback, CFXParameters, QueryResponse } from '../types'; +import { parseResponse } from '../utils/parseResponse'; import { parseExecute } from '../utils/parseExecute'; export const rawExecute = async ( @@ -24,26 +25,22 @@ export const rawExecute = async ( let result: QueryResponse; try { - const executionTime = process.hrtime(); - if (!parameters.every(Array.isArray)) parameters = [[...parameters]]; - const results = []; + const results = [] as RowDataPacket; + const executionTime = process.hrtime(); for (const params of parameters) { - results.push((await connection.execute(query, params))[0]); - + results.push(parseResponse(type, (await connection.execute(query, params))[0])); logQuery(invokingResource, query, process.hrtime(executionTime)[1] / 1e6, params as typeof parameters); } result = results; - //TODO any giggle if (results.length === 1) { if (type === 'execute') { - if ((results as any)[0][0] && Object.keys((results as any)[0][0]).length === 1) - result = Object.values((results as any)[0][0])[0] as any; - else result = (results as any)[0][0]; + if (results[0][0] && Object.keys(results[0][0]).length === 1) result = Object.values(results[0][0])[0] as any; + else result = results[0][0]; } else { result = results[0]; } diff --git a/src/types/index.ts b/src/types/index.ts index 9419ada..c093aa9 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -4,7 +4,7 @@ type SQLResponse = RowDataPacket[][] | RowDataPacket[] | OkPacket | OkPacket[] | export type QueryResponse = SQLResponse | SQLResponse[] | RowDataPacket; -export type QueryType = 'insert' | 'update' | 'scalar' | 'single' | null; +export type QueryType = 'execute' | 'insert' | 'update' | 'scalar' | 'single' | null; export type TransactionQuery = { query: string;