From 4eaa0a92fdc8d87c4ee088fa937e686529b73de7 Mon Sep 17 00:00:00 2001 From: Tejas Mehta Date: Wed, 22 Nov 2023 14:52:47 -0500 Subject: [PATCH] remove flush when type is inferrable --- src/connection.js | 21 ++++++++++----------- src/types.js | 11 +++++++++++ tests/index.js | 2 +- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/connection.js b/src/connection.js index e8e4881d..9b0af12b 100644 --- a/src/connection.js +++ b/src/connection.js @@ -4,7 +4,7 @@ import crypto from 'crypto' import Stream from 'stream' import { performance } from 'perf_hooks' -import { stringify, handleValue, arrayParser, arraySerializer } from './types.js' +import { stringify, handleValue, arrayParser, arraySerializer, typesMapped } from './types.js' import { Errors } from './errors.js' import Result from './result.js' import Queue from './queue.js' @@ -183,13 +183,13 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose return q.options.simple ? b().Q().str(q.statement.string + b.N).end() - : q.describeFirst + : q.describeFirst ? Buffer.concat([describe(q), Flush]) - : q.prepare - ? q.prepared - ? prepared(q) - : Buffer.concat([describe(q), prepared(q)]) - : unnamed(q) + : q.prepare + ? q.prepared + ? prepared(q) + : Buffer.concat([describe(q), prepared(q)]) + : unnamed(q); } function describe(q) { @@ -230,7 +230,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose q.onlyDescribe && (delete statements[q.signature]) q.parameters = q.parameters || parameters q.prepared = q.prepare && q.signature in statements - q.describeFirst = q.onlyDescribe || (parameters.length && !q.prepared) + q.describeFirst = q.onlyDescribe || (!typesMapped(q.args, types) && !q.prepared); q.statement = q.prepared ? statements[q.signature] : { string, types, name: q.prepare ? statementId + statementCount++ : '' } @@ -602,8 +602,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose !query.statement.types[i] && (query.statement.types[i] = x.readUInt32BE(7 + i * 4)) query.prepare && (statements[query.signature] = query.statement) - query.describeFirst && !query.onlyDescribe && (write(prepared(query)), query.describeFirst = false) - } + query.describeFirst && !query.onlyDescribe && write(prepared(query)), (query.describeFirst = false) } function RowDescription(x) { if (result.command) { @@ -951,7 +950,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose function Execute(portal = '', rows = 0) { return Buffer.concat([ b().E().str(portal + b.N).i32(rows).end(), - Flush + rows != 0 ? Flush : Sync ]) } diff --git a/src/types.js b/src/types.js index 7c7c2b93..79ceca1f 100644 --- a/src/types.js +++ b/src/types.js @@ -93,6 +93,17 @@ export function handleValue(x, parameters, types, options) { )) } +export function typesMapped(args, types) { + if (types.length < args.length) return false; + for(let i = 0; i < args.length; i++) { + let val = args[i] instanceof Parameter ? args[i].value : args[i] + if (types[i] === 0 && typeof val !== 'number') { + return false; + } + } + return true; +} + const defaultHandlers = typeHandlers(types) export function stringify(q, string, value, parameters, types, options) { // eslint-disable-line diff --git a/tests/index.js b/tests/index.js index 499b3fbd..fd72d4c4 100644 --- a/tests/index.js +++ b/tests/index.js @@ -1588,7 +1588,7 @@ t('Error contains query string', async() => [ ]) t('Error contains query serialized parameters', async() => [ - 1, + '1', (await sql`selec ${ 1 }`.catch(err => err.parameters[0])) ])