Skip to content

Commit cbc4f36

Browse files
committed
remove flush when type is inferrable
1 parent 428475a commit cbc4f36

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

src/connection.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import crypto from 'crypto'
44
import Stream from 'stream'
55
import { performance } from 'perf_hooks'
66

7-
import { stringify, handleValue, arrayParser, arraySerializer } from './types.js'
7+
import { stringify, handleValue, arrayParser, arraySerializer, typesMapped } from './types.js'
88
import { Errors } from './errors.js'
99
import Result from './result.js'
1010
import Queue from './queue.js'
@@ -183,13 +183,13 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
183183

184184
return q.options.simple
185185
? b().Q().str(q.statement.string + b.N).end()
186-
: q.describeFirst
186+
: q.describeFirst
187187
? Buffer.concat([describe(q), Flush])
188-
: q.prepare
189-
? q.prepared
190-
? prepared(q)
191-
: Buffer.concat([describe(q), prepared(q)])
192-
: unnamed(q)
188+
: q.prepare
189+
? q.prepared
190+
? prepared(q)
191+
: Buffer.concat([describe(q), prepared(q)])
192+
: unnamed(q);
193193
}
194194

195195
function describe(q) {
@@ -230,11 +230,12 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
230230
q.onlyDescribe && (delete statements[q.signature])
231231
q.parameters = q.parameters || parameters
232232
q.prepared = q.prepare && q.signature in statements
233-
q.describeFirst = q.onlyDescribe || (parameters.length && !q.prepared)
233+
q.describeFirst = q.onlyDescribe || (!typesMapped(q.args, types) && !q.prepared);
234234
q.statement = q.prepared
235235
? statements[q.signature]
236236
: { string, types, name: q.prepare ? statementId + statementCount++ : '' }
237237

238+
console.log(`types: ${JSON.stringify(types)}`)
238239
typeof options.debug === 'function' && options.debug(id, string, parameters, types)
239240
}
240241

@@ -602,8 +603,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
602603
!query.statement.types[i] && (query.statement.types[i] = x.readUInt32BE(7 + i * 4))
603604

604605
query.prepare && (statements[query.signature] = query.statement)
605-
query.describeFirst && !query.onlyDescribe && (write(prepared(query)), query.describeFirst = false)
606-
}
606+
query.describeFirst && !query.onlyDescribe && write(prepared(query)), (query.describeFirst = false) }
607607

608608
function RowDescription(x) {
609609
if (result.command) {
@@ -951,7 +951,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
951951
function Execute(portal = '', rows = 0) {
952952
return Buffer.concat([
953953
b().E().str(portal + b.N).i32(rows).end(),
954-
Flush
954+
rows != 0 ? Flush : Sync
955955
])
956956
}
957957

src/types.js

+11
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,17 @@ export function handleValue(x, parameters, types, options) {
9393
))
9494
}
9595

96+
export function typesMapped(args, types) {
97+
if (types.length < args.length) return false;
98+
for(let i = 0; i < args.length; i++) {
99+
let val = args[i] instanceof Parameter ? args[i].value : args[i]
100+
if (types[i] === 0 && typeof val !== 'number') {
101+
return false;
102+
}
103+
}
104+
return true;
105+
}
106+
96107
const defaultHandlers = typeHandlers(types)
97108

98109
export function stringify(q, string, value, parameters, types, options) { // eslint-disable-line

tests/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1588,7 +1588,7 @@ t('Error contains query string', async() => [
15881588
])
15891589

15901590
t('Error contains query serialized parameters', async() => [
1591-
1,
1591+
'1',
15921592
(await sql`selec ${ 1 }`.catch(err => err.parameters[0]))
15931593
])
15941594

0 commit comments

Comments
 (0)