-
Notifications
You must be signed in to change notification settings - Fork 22
/
conn_write.go
346 lines (264 loc) · 7.07 KB
/
conn_write.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// Copyright 2010 The go-pgsql Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package pgsql
import (
"encoding/binary"
"fmt"
"math"
"math/big"
"strconv"
"strings"
"time"
)
func (conn *Conn) flush() {
panicIfErr(conn.writer.Flush())
}
func (conn *Conn) write(b []byte) {
_, err := conn.writer.Write(b)
panicIfErr(err)
}
func (conn *Conn) writeByte(b byte) {
panicIfErr(conn.writer.WriteByte(b))
}
func (conn *Conn) writeFloat32(f float32) {
var buf [4]byte
b := buf[:]
binary.BigEndian.PutUint32(b, math.Float32bits(f))
conn.write(b)
}
func (conn *Conn) writeFloat64(f float64) {
var buf [8]byte
b := buf[:]
binary.BigEndian.PutUint64(b, math.Float64bits(f))
conn.write(b)
}
func (conn *Conn) writeFrontendMessageCode(code frontendMessageCode) {
panicIfErr(conn.writer.WriteByte(byte(code)))
}
func (conn *Conn) writeInt16(i int16) {
var buf [2]byte
b := buf[:]
binary.BigEndian.PutUint16(b, uint16(i))
conn.write(b)
}
func (conn *Conn) writeInt32(i int32) {
var buf [4]byte
b := buf[:]
binary.BigEndian.PutUint32(b, uint32(i))
conn.write(b)
}
func (conn *Conn) writeInt64(i int64) {
var buf [8]byte
b := buf[:]
binary.BigEndian.PutUint64(b, uint64(i))
conn.write(b)
}
func (conn *Conn) writeString(s string) {
_, err := conn.writer.WriteString(s)
panicIfErr(err)
}
func (conn *Conn) writeString0(s string) {
conn.writeString(s)
conn.writeByte(0)
}
func (conn *Conn) writeFlush() {
conn.writeFrontendMessageCode(_Flush)
conn.writeInt32(4)
conn.flush()
}
func (conn *Conn) writeBind(stmt *Statement) {
values := make([]string, len(stmt.params))
var paramValuesLen int
for i, param := range stmt.params {
value := param.value
if val, ok := value.(uint64); ok {
value = int64(val)
}
switch val := value.(type) {
case bool:
if val {
values[i] = "t"
} else {
values[i] = "f"
}
case byte:
values[i] = string([]byte{val})
case float32:
values[i] = strconv.FormatFloat(float64(val), 'f', -1, 32)
case float64:
values[i] = strconv.FormatFloat(val, 'f', -1, 64)
case int:
values[i] = strconv.Itoa(val)
case int16:
values[i] = strconv.Itoa(int(val))
case int32:
values[i] = strconv.Itoa(int(val))
case int64:
switch param.typ {
case Date:
values[i] = time.Unix(val, 0).UTC().Format("2006-01-02")
case Time, TimeTZ:
values[i] = time.Unix(val, 0).UTC().Format("15:04:05")
case Timestamp, TimestampTZ:
values[i] = time.Unix(val, 0).UTC().Format("2006-01-02 15:04:05")
default:
values[i] = strconv.FormatInt(val, 10)
}
case nil:
case *big.Rat:
if val.IsInt() {
values[i] = val.Num().String()
} else {
// FIXME: Find a better way to do this.
prec999 := val.FloatString(999)
trimmed := strings.TrimRight(prec999, "0")
sepIndex := strings.Index(trimmed, ".")
prec := len(trimmed) - sepIndex - 1
values[i] = val.FloatString(prec)
}
case string:
values[i] = val
case time.Time:
switch param.typ {
case Date:
values[i] = val.Format("2006-01-02")
case Time, TimeTZ:
values[i] = val.Format("15:04:05")
case Timestamp, TimestampTZ:
values[i] = val.Format("2006-01-02 15:04:05")
default:
panic("invalid use of time.Time")
}
default:
panic("unsupported parameter type")
}
paramValuesLen += len(values[i])
}
msgLen := int32(4 +
len(stmt.portalName) + 1 +
len(stmt.name) + 1 +
2 + 2 +
2 + len(stmt.params)*4 + paramValuesLen +
2 + 2)
conn.writeFrontendMessageCode(_Bind)
conn.writeInt32(msgLen)
conn.writeString0(stmt.portalName)
conn.writeString0(stmt.name)
conn.writeInt16(1)
conn.writeInt16(int16(textFormat))
conn.writeInt16(int16(len(stmt.params)))
for i, param := range stmt.params {
if param.value == nil {
conn.writeInt32(-1)
} else {
conn.writeInt32(int32(len(values[i])))
conn.writeString(values[i])
}
}
conn.writeInt16(1)
conn.writeInt16(int16(textFormat))
conn.writeFlush()
}
func (conn *Conn) writeClose(itemType byte, itemName string) {
msgLen := int32(4 + 1 + len(itemName) + 1)
conn.writeFrontendMessageCode(_Close)
conn.writeInt32(msgLen)
conn.writeByte(itemType)
conn.writeString0(itemName)
conn.flush()
}
func (conn *Conn) writeDescribe(stmt *Statement) {
msgLen := int32(4 + 1 + len(stmt.portalName) + 1)
conn.writeFrontendMessageCode(_Describe)
conn.writeInt32(msgLen)
conn.writeByte('P')
conn.writeString0(stmt.portalName)
conn.writeFlush()
}
func (conn *Conn) writeExecute(stmt *Statement) {
msgLen := int32(4 + len(stmt.portalName) + 1 + 4)
conn.writeFrontendMessageCode(_Execute)
conn.writeInt32(msgLen)
conn.writeString0(stmt.portalName)
conn.writeInt32(0)
conn.writeFlush()
}
func (conn *Conn) writeParse(stmt *Statement) {
if conn.LogLevel >= LogDebug {
defer conn.logExit(conn.logEnter("*Conn.writeParse"))
}
if conn.LogLevel >= LogCommand {
conn.log(LogCommand, fmt.Sprintf("stmt.ActualCommand: '%s'", stmt.ActualCommand()))
}
msgLen := int32(4 +
len(stmt.name) + 1 +
len(stmt.actualCommand) + 1 +
2 + len(stmt.params)*4)
conn.writeFrontendMessageCode(_Parse)
conn.writeInt32(msgLen)
conn.writeString0(stmt.name)
conn.writeString0(stmt.actualCommand)
conn.writeInt16(int16(len(stmt.params)))
for _, param := range stmt.params {
typ := param.typ
if typ == Char {
// FIXME: There seems to be something wrong with CHAR parameters.
// Had a query that correctly returned rows in psql, but didn't
// via go-pgsql statement. Changed param type from Char to Varchar
// and it worked. The corresponding field in the table was CHAR(32).
typ = Varchar
}
conn.writeInt32(int32(typ))
}
conn.writeFlush()
}
func (conn *Conn) writePasswordMessage(password string) {
if conn.LogLevel >= LogDebug {
defer conn.logExit(conn.logEnter("*Conn.writePasswordMessage"))
}
msgLen := int32(4 + len(password) + 1)
conn.writeFrontendMessageCode(_PasswordMessage)
conn.writeInt32(msgLen)
conn.writeString0(password)
conn.flush()
}
func (conn *Conn) writeQuery(command string) {
if conn.LogLevel >= LogDebug {
defer conn.logExit(conn.logEnter("*Conn.writeQuery"))
}
if conn.LogLevel >= LogCommand {
conn.log(LogCommand, fmt.Sprintf("command: '%s'", command))
}
conn.writeFrontendMessageCode(_Query)
conn.writeInt32(int32(4 + len(command) + 1))
conn.writeString0(command)
conn.flush()
}
func (conn *Conn) writeStartup() {
if conn.LogLevel >= LogDebug {
defer conn.logExit(conn.logEnter("*Conn.writeStartup"))
}
msglen := int32(4 + 4 +
len("user") + 1 + len(conn.params.User) + 1 +
len("database") + 1 + len(conn.params.Database) + 1 + 1)
conn.writeInt32(msglen)
// For now we only support protocol version 3.0.
conn.writeInt32(3 << 16)
conn.writeString0("user")
conn.writeString0(conn.params.User)
conn.writeString0("database")
conn.writeString0(conn.params.Database)
conn.writeByte(0)
conn.flush()
}
func (conn *Conn) writeSync() {
conn.writeFrontendMessageCode(_Sync)
conn.writeInt32(4)
conn.writeFlush()
}
func (conn *Conn) writeTerminate() {
conn.writeFrontendMessageCode(_Terminate)
conn.writeInt32(4)
conn.flush()
}