Skip to content

Commit c2e92b4

Browse files
committed
Incorporate PR feedback
1 parent 3e5db60 commit c2e92b4

File tree

3 files changed

+29
-18
lines changed

3 files changed

+29
-18
lines changed

packages/pg-protocol/src/outbound-serializer.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ describe('serializer', () => {
110110
var expectedBuffer = new BufferList()
111111
.addCString('bang') // portal name
112112
.addCString('woo') // statement name
113+
.addInt16(4)
114+
.addInt16(0)
115+
.addInt16(0)
116+
.addInt16(0)
113117
.addInt16(0)
114118
.addInt16(4)
115119
.addInt32(1)

packages/pg-protocol/src/serializer.ts

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -101,31 +101,42 @@ const parse = (query: ParseOpts): Buffer => {
101101
return writer.flush(code.parse)
102102
}
103103

104+
type ValueMapper = (param: any, index: number) => any
105+
104106
type BindOpts = {
105107
portal?: string
106108
binary?: boolean
107109
statement?: string
108110
values?: any[]
109111
// optional map from JS value to postgres value per parameter
110-
valueMap?: (param: any) => any
112+
valueMapper?: ValueMapper
111113
}
112114

113115
const paramWriter = new Writer()
114116

115-
const writeValues = function (values: any[], valueMap?: (val: any) => any): void {
117+
// make this a const enum so typescript will inline the value
118+
const enum ParamType {
119+
STRING = 0,
120+
BINARY = 1,
121+
}
122+
123+
const writeValues = function (values: any[], valueMapper?: ValueMapper): void {
116124
for (let i = 0; i < values.length; i++) {
117-
var val = values[i]
118-
if (val === null || typeof val === 'undefined') {
119-
writer.addInt16(0)
125+
const mappedVal = valueMapper ? valueMapper(values[i], i) : values[i]
126+
if (mappedVal == null) {
127+
// add the param type (string) to the writer
128+
writer.addInt16(ParamType.STRING)
129+
// write -1 to the param writer to indicate null
120130
paramWriter.addInt32(-1)
121-
} else if (val instanceof Buffer) {
122-
writer.addInt16(1)
123-
const mappedVal = valueMap ? valueMap(val) : val
131+
} else if (mappedVal instanceof Buffer) {
132+
// add the param type (binary) to the writer
133+
writer.addInt16(ParamType.BINARY)
134+
// add the buffer to the param writer
124135
paramWriter.addInt32(mappedVal.length)
125136
paramWriter.add(mappedVal)
126137
} else {
127-
writer.addInt16(0)
128-
const mappedVal = valueMap ? valueMap(val) : val
138+
// add the param type (string) to the writer
139+
writer.addInt16(ParamType.STRING)
129140
paramWriter.addInt32(Buffer.byteLength(mappedVal))
130141
paramWriter.addString(mappedVal)
131142
}
@@ -143,17 +154,13 @@ const bind = (config: BindOpts = {}): Buffer => {
143154
writer.addCString(portal).addCString(statement)
144155
writer.addInt16(len)
145156

146-
writeValues(values, config.valueMap)
157+
writeValues(values, config.valueMapper)
147158

148159
writer.addInt16(len)
149160
writer.add(paramWriter.flush())
150161

151-
if (binary) {
152-
writer.addInt16(1) // format codes to use binary
153-
writer.addInt16(1)
154-
} else {
155-
writer.addInt16(0) // format codes to use text
156-
}
162+
// format code
163+
writer.addInt16(binary ? ParamType.BINARY : ParamType.STRING)
157164
return writer.flush(code.bind)
158165
}
159166

packages/pg/lib/query.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ class Query extends EventEmitter {
206206
statement: this.name,
207207
values: this.values,
208208
binary: this.binary,
209-
valueMap: utils.prepareValue,
209+
valueMapper: utils.prepareValue,
210210
})
211211
} catch (err) {
212212
this.handleError(err, connection)

0 commit comments

Comments
 (0)