Skip to content

Commit

Permalink
remove flush when type is inferrable
Browse files Browse the repository at this point in the history
  • Loading branch information
tmthecoder committed Nov 27, 2023
1 parent 428475a commit 4eaa0a9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
21 changes: 10 additions & 11 deletions src/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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++ : '' }
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
])
}

Expand Down
11 changes: 11 additions & 0 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]))
])

Expand Down

0 comments on commit 4eaa0a9

Please sign in to comment.