forked from cockroachdb/cockroach
/
v3.go
845 lines (765 loc) · 23.8 KB
/
v3.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
// Copyright 2015 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
// Author: Ben Darnell
// Author: Tamir Duberstein (tamird@gmail.com)
package pgwire
import (
"bufio"
"fmt"
"net"
"reflect"
"strconv"
"golang.org/x/net/context"
"github.com/cockroachdb/cockroach/sql"
"github.com/cockroachdb/cockroach/sql/parser"
"github.com/cockroachdb/cockroach/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/sql/sqlbase"
"github.com/cockroachdb/cockroach/util/log"
"github.com/cockroachdb/cockroach/util/tracing"
"github.com/cockroachdb/pq/oid"
"github.com/pkg/errors"
)
//go:generate stringer -type=clientMessageType
type clientMessageType byte
//go:generate stringer -type=serverMessageType
type serverMessageType byte
// http://www.postgresql.org/docs/9.4/static/protocol-message-formats.html
const (
clientMsgSimpleQuery clientMessageType = 'Q'
clientMsgParse clientMessageType = 'P'
clientMsgTerminate clientMessageType = 'X'
clientMsgDescribe clientMessageType = 'D'
clientMsgSync clientMessageType = 'S'
clientMsgClose clientMessageType = 'C'
clientMsgBind clientMessageType = 'B'
clientMsgExecute clientMessageType = 'E'
clientMsgFlush clientMessageType = 'H'
serverMsgAuth serverMessageType = 'R'
serverMsgCommandComplete serverMessageType = 'C'
serverMsgDataRow serverMessageType = 'D'
serverMsgErrorResponse serverMessageType = 'E'
serverMsgParseComplete serverMessageType = '1'
serverMsgReady serverMessageType = 'Z'
serverMsgRowDescription serverMessageType = 'T'
serverMsgEmptyQuery serverMessageType = 'I'
serverMsgParameterDescription serverMessageType = 't'
serverMsgBindComplete serverMessageType = '2'
serverMsgParameterStatus serverMessageType = 'S'
serverMsgNoData serverMessageType = 'n'
)
//go:generate stringer -type=serverErrFieldType
type serverErrFieldType byte
// http://www.postgresql.org/docs/current/static/protocol-error-fields.html
const (
serverErrFieldSeverity serverErrFieldType = 'S'
serverErrFieldSQLState serverErrFieldType = 'C'
serverErrFieldMsgPrimary serverErrFieldType = 'M'
serverErrFieldSrcFile serverErrFieldType = 'F'
serverErrFieldSrcLine serverErrFieldType = 'L'
serverErrFieldSrcFunction serverErrFieldType = 'R'
)
//go:generate stringer -type=prepareType
type prepareType byte
const (
prepareStatement prepareType = 'S'
preparePortal prepareType = 'P'
)
const (
authOK int32 = 0
)
// preparedStatementMeta is pgwire-specific metadata which is attached to each
// sql.PreparedStatement on a v3Conn's sql.Session.
type preparedStatementMeta struct {
inTypes []oid.Oid
}
// preparedPortalMeta is pgwire-specific metadata which is attached to each
// sql.PreparedPortal on a v3Conn's sql.Session.
type preparedPortalMeta struct {
outFormats []formatCode
}
type v3Conn struct {
conn net.Conn
rd *bufio.Reader
wr *bufio.Writer
executor *sql.Executor
readBuf readBuffer
writeBuf writeBuffer
tagBuf [64]byte
session *sql.Session
// The logic governing these guys is hairy, and is not sufficiently
// specified in documentation. Consult the sources before you modify:
// https://github.com/postgres/postgres/blob/master/src/backend/tcop/postgres.c
doingExtendedQueryMessage, ignoreTillSync bool
metrics *serverMetrics
}
func makeV3Conn(
conn net.Conn, executor *sql.Executor,
metrics *serverMetrics, sessionArgs sql.SessionArgs) v3Conn {
return v3Conn{
conn: conn,
rd: bufio.NewReader(conn),
wr: bufio.NewWriter(conn),
executor: executor,
writeBuf: writeBuffer{bytecount: metrics.bytesOutCount},
metrics: metrics,
session: sql.NewSession(sessionArgs, executor, conn.RemoteAddr()),
}
}
func (c *v3Conn) finish() {
// This is better than always flushing on error.
if err := c.wr.Flush(); err != nil {
log.Error(err)
}
_ = c.conn.Close()
c.session.Finish()
}
func parseOptions(data []byte) (sql.SessionArgs, error) {
args := sql.SessionArgs{}
buf := readBuffer{msg: data}
for {
key, err := buf.getString()
if err != nil {
return sql.SessionArgs{}, errors.Errorf("error reading option key: %s", err)
}
if len(key) == 0 {
break
}
value, err := buf.getString()
if err != nil {
return sql.SessionArgs{}, errors.Errorf("error reading option value: %s", err)
}
switch key {
case "database":
args.Database = value
case "user":
args.User = value
default:
if log.V(1) {
log.Warningf("unrecognized configuration parameter %q", key)
}
}
}
return args, nil
}
// statusReportParams is a static mapping from run-time parameters to their respective
// hard-coded values, each of which is to be returned as part of the status report
// during connection initialization.
var statusReportParams = map[string]string{
"client_encoding": "UTF8",
"DateStyle": "ISO",
// The latest version of the docs that was consulted during the development
// of this package. We specify this version to avoid having to support old
// code paths which various client tools fall back to if they can't
// determine that the server is new enough.
"server_version": "9.5.0",
}
func (c *v3Conn) serve(authenticationHook func(string, bool) error) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if authenticationHook != nil {
if err := authenticationHook(c.session.User, true /* public */); err != nil {
return c.sendInternalError(err.Error())
}
}
c.writeBuf.initMsg(serverMsgAuth)
c.writeBuf.putInt32(authOK)
if err := c.writeBuf.finishMsg(c.wr); err != nil {
return err
}
for key, value := range statusReportParams {
c.writeBuf.initMsg(serverMsgParameterStatus)
c.writeBuf.writeTerminatedString(key)
c.writeBuf.writeTerminatedString(value)
if err := c.writeBuf.finishMsg(c.wr); err != nil {
return err
}
}
if err := c.wr.Flush(); err != nil {
return err
}
for {
if !c.doingExtendedQueryMessage {
c.writeBuf.initMsg(serverMsgReady)
var txnStatus byte
switch c.session.TxnState.State {
case sql.Aborted, sql.RestartWait:
// We send status "InFailedTransaction" also for state RestartWait
// because GO's lib/pq freaks out if we invent a new status.
txnStatus = 'E'
case sql.Open:
txnStatus = 'T'
case sql.NoTxn:
// We're not in a txn (i.e. the last txn was committed).
txnStatus = 'I'
case sql.CommitWait:
// We need to lie to pgwire and claim that we're still
// in a txn. Otherwise drivers freak out.
// This state is not part of the Postgres protocol.
txnStatus = 'T'
}
if log.V(2) {
log.Infof("pgwire: %s: %q", serverMsgReady, txnStatus)
}
c.writeBuf.writeByte(txnStatus)
if err := c.writeBuf.finishMsg(c.wr); err != nil {
return err
}
// We only flush on every message if not doing an extended query.
// If we are, wait for an explicit Flush message. See:
// http://www.postgresql.org/docs/current/static/protocol-flow.html#PROTOCOL-FLOW-EXT-QUERY.
if err := c.wr.Flush(); err != nil {
return err
}
}
typ, n, err := c.readBuf.readTypedMsg(c.rd)
c.metrics.bytesInCount.Inc(int64(n))
if err != nil {
return err
}
// When an error occurs handling an extended query message, we have to ignore
// any messages until we get a sync.
if c.ignoreTillSync && typ != clientMsgSync {
if log.V(2) {
log.Infof("pgwire: ignoring %s till sync", typ)
}
continue
}
if log.V(2) {
log.Infof("pgwire: processing %s", typ)
}
switch typ {
case clientMsgSync:
c.doingExtendedQueryMessage = false
c.ignoreTillSync = false
case clientMsgSimpleQuery:
c.doingExtendedQueryMessage = false
err = c.handleSimpleQuery(ctx, &c.readBuf)
case clientMsgTerminate:
return nil
case clientMsgParse:
c.doingExtendedQueryMessage = true
err = c.handleParse(ctx, &c.readBuf)
case clientMsgDescribe:
c.doingExtendedQueryMessage = true
err = c.handleDescribe(&c.readBuf)
case clientMsgClose:
c.doingExtendedQueryMessage = true
err = c.handleClose(&c.readBuf)
case clientMsgBind:
c.doingExtendedQueryMessage = true
err = c.handleBind(&c.readBuf)
case clientMsgExecute:
c.doingExtendedQueryMessage = true
err = c.handleExecute(ctx, &c.readBuf)
case clientMsgFlush:
c.doingExtendedQueryMessage = true
err = c.wr.Flush()
default:
err = c.sendErrorWithCode(pgerror.CodeProtocolViolationError, sqlbase.MakeSrcCtx(0),
fmt.Sprintf("unrecognized client message type %s", typ))
}
if err != nil {
return err
}
}
}
func (c *v3Conn) handleSimpleQuery(ctx context.Context, buf *readBuffer) error {
query, err := buf.getString()
if err != nil {
return err
}
return c.executeStatements(ctx, query, nil, nil, true, 0)
}
func (c *v3Conn) handleParse(ctx context.Context, buf *readBuffer) error {
name, err := buf.getString()
if err != nil {
return err
}
// The unnamed prepared statement can be freely overwritten.
if name != "" {
if c.session.PreparedStatements.Exists(name) {
return c.sendInternalError(fmt.Sprintf("prepared statement %q already exists", name))
}
}
query, err := buf.getString()
if err != nil {
return err
}
// The client may provide type information for (some of) the
// placeholders. Read this first.
numQArgTypes, err := buf.getUint16()
if err != nil {
return err
}
inTypeHints := make([]oid.Oid, numQArgTypes)
for i := range inTypeHints {
typ, err := buf.getUint32()
if err != nil {
return err
}
inTypeHints[i] = oid.Oid(typ)
}
// Prepare the mapping of SQL placeholder names to
// types. Pre-populate it with the type hints received from the
// client, if any.
sqlTypeHints := make(parser.PlaceholderTypes)
for i, t := range inTypeHints {
if t == 0 {
continue
}
v, ok := oidToDatum[t]
if !ok {
return c.sendInternalError(fmt.Sprintf("unknown oid type: %v", t))
}
sqlTypeHints[fmt.Sprint(i+1)] = v
}
// Create the new PreparedStatement in the connection's Session.
stmt, err := c.session.PreparedStatements.New(ctx, c.executor, name, query, sqlTypeHints)
if err != nil {
return c.sendError(err)
}
// Convert the inferred SQL types back to an array of pgwire Oids.
inTypes := make([]oid.Oid, 0, len(stmt.SQLTypes))
for k, t := range stmt.SQLTypes {
i, err := strconv.Atoi(k)
if err != nil || i < 1 {
return c.sendInternalError(fmt.Sprintf("invalid placeholder name: $%s", k))
}
// Placeholder names are 1-indexed; the arrays in the protocol are
// 0-indexed.
i--
// Grow inTypes to be at least as large as i. Prepopulate all
// slots with the hints provided, if any.
for j := len(inTypes); j <= i; j++ {
inTypes = append(inTypes, 0)
if j < len(inTypeHints) {
inTypes[j] = inTypeHints[j]
}
}
// OID to Datum is not a 1-1 mapping (for example, int4 and int8
// both map to TypeInt), so we need to maintain the types sent by
// the client.
if inTypes[i] != 0 {
continue
}
id, ok := datumToOid[reflect.TypeOf(t)]
if !ok {
return c.sendInternalError(fmt.Sprintf("unknown datum type: %s", t.Type()))
}
inTypes[i] = id
}
for i, t := range inTypes {
if t == 0 {
return c.sendInternalError(
fmt.Sprintf("could not determine data type of placeholder $%d", i+1))
}
}
// Attach pgwire-specific metadata to the PreparedStatement.
stmt.ProtocolMeta = preparedStatementMeta{inTypes: inTypes}
c.writeBuf.initMsg(serverMsgParseComplete)
return c.writeBuf.finishMsg(c.wr)
}
func (c *v3Conn) handleDescribe(buf *readBuffer) error {
typ, err := buf.getPrepareType()
if err != nil {
return c.sendInternalError(err.Error())
}
name, err := buf.getString()
if err != nil {
return err
}
switch typ {
case prepareStatement:
stmt, ok := c.session.PreparedStatements.Get(name)
if !ok {
return c.sendInternalError(fmt.Sprintf("unknown prepared statement %q", name))
}
stmtMeta := stmt.ProtocolMeta.(preparedStatementMeta)
c.writeBuf.initMsg(serverMsgParameterDescription)
c.writeBuf.putInt16(int16(len(stmtMeta.inTypes)))
for _, t := range stmtMeta.inTypes {
c.writeBuf.putInt32(int32(t))
}
if err := c.writeBuf.finishMsg(c.wr); err != nil {
return err
}
return c.sendRowDescription(stmt.Columns, nil)
case preparePortal:
portal, ok := c.session.PreparedPortals.Get(name)
if !ok {
return c.sendInternalError(fmt.Sprintf("unknown portal %q", name))
}
portalMeta := portal.ProtocolMeta.(preparedPortalMeta)
return c.sendRowDescription(portal.Stmt.Columns, portalMeta.outFormats)
default:
return errors.Errorf("unknown describe type: %s", typ)
}
}
func (c *v3Conn) handleClose(buf *readBuffer) error {
typ, err := buf.getPrepareType()
if err != nil {
return c.sendInternalError(err.Error())
}
name, err := buf.getString()
if err != nil {
return err
}
switch typ {
case prepareStatement:
c.session.PreparedStatements.Delete(name)
case preparePortal:
c.session.PreparedPortals.Delete(name)
default:
return errors.Errorf("unknown close type: %s", typ)
}
return nil
}
func (c *v3Conn) handleBind(buf *readBuffer) error {
portalName, err := buf.getString()
if err != nil {
return err
}
// The unnamed portal can be freely overwritten.
if portalName != "" {
if c.session.PreparedPortals.Exists(portalName) {
return c.sendInternalError(fmt.Sprintf("portal %q already exists", portalName))
}
}
statementName, err := buf.getString()
if err != nil {
return err
}
stmt, ok := c.session.PreparedStatements.Get(statementName)
if !ok {
return c.sendInternalError(fmt.Sprintf("unknown prepared statement %q", statementName))
}
stmtMeta := stmt.ProtocolMeta.(preparedStatementMeta)
numQArgs := uint16(len(stmtMeta.inTypes))
qArgFormatCodes := make([]formatCode, numQArgs)
// From the docs on number of argument format codes to bind:
// This can be zero to indicate that there are no arguments or that the
// arguments all use the default format (text); or one, in which case the
// specified format code is applied to all arguments; or it can equal the
// actual number of arguments.
// http://www.postgresql.org/docs/current/static/protocol-message-formats.html
numQArgFormatCodes, err := buf.getUint16()
if err != nil {
return err
}
switch numQArgFormatCodes {
case 0:
case 1:
// `1` means read one code and apply it to every argument.
c, err := buf.getUint16()
if err != nil {
return err
}
fmtCode := formatCode(c)
for i := range qArgFormatCodes {
qArgFormatCodes[i] = fmtCode
}
case numQArgs:
// Read one format code for each argument and apply it to that argument.
for i := range qArgFormatCodes {
c, err := buf.getUint16()
if err != nil {
return err
}
qArgFormatCodes[i] = formatCode(c)
}
default:
return c.sendInternalError(fmt.Sprintf("wrong number of format codes specified: %d for %d arguments", numQArgFormatCodes, numQArgs))
}
numValues, err := buf.getUint16()
if err != nil {
return err
}
if numValues != numQArgs {
return c.sendInternalError(fmt.Sprintf("expected %d arguments, got %d", numQArgs, numValues))
}
qargs := parser.QueryArguments{}
for i, t := range stmtMeta.inTypes {
plen, err := buf.getUint32()
if err != nil {
return err
}
k := fmt.Sprint(i + 1)
if int32(plen) == -1 {
// The argument is a NULL value.
qargs[k] = parser.DNull
continue
}
b, err := buf.getBytes(int(plen))
if err != nil {
return err
}
d, err := decodeOidDatum(t, qArgFormatCodes[i], b)
if err != nil {
return c.sendInternalError(fmt.Sprintf("error in argument for $%d: %s", i+1, err))
}
qargs[k] = d
}
numColumns := uint16(len(stmt.Columns))
columnFormatCodes := make([]formatCode, numColumns)
// From the docs on number of result-column format codes to bind:
// This can be zero to indicate that there are no result columns or that
// the result columns should all use the default format (text); or one, in
// which case the specified format code is applied to all result columns
// (if any); or it can equal the actual number of result columns of the
// query.
// http://www.postgresql.org/docs/current/static/protocol-message-formats.html
numColumnFormatCodes, err := buf.getUint16()
if err != nil {
return err
}
switch numColumnFormatCodes {
case 0:
case 1:
// Read one code and apply it to every column.
c, err := buf.getUint16()
if err != nil {
return err
}
fmtCode := formatCode(c)
for i := range columnFormatCodes {
columnFormatCodes[i] = formatCode(fmtCode)
}
case numColumns:
// Read one format code for each column and apply it to that column.
for i := range columnFormatCodes {
c, err := buf.getUint16()
if err != nil {
return err
}
columnFormatCodes[i] = formatCode(c)
}
default:
return c.sendInternalError(fmt.Sprintf("expected 0, 1, or %d for number of format codes, got %d", numColumns, numColumnFormatCodes))
}
// Create the new PreparedPortal in the connection's Session.
portal := c.session.PreparedPortals.New(portalName, stmt, qargs)
// Attach pgwire-specific metadata to the PreparedPortal.
portal.ProtocolMeta = preparedPortalMeta{outFormats: columnFormatCodes}
c.writeBuf.initMsg(serverMsgBindComplete)
return c.writeBuf.finishMsg(c.wr)
}
func (c *v3Conn) handleExecute(ctx context.Context, buf *readBuffer) error {
portalName, err := buf.getString()
if err != nil {
return err
}
portal, ok := c.session.PreparedPortals.Get(portalName)
if !ok {
return c.sendInternalError(fmt.Sprintf("unknown portal %q", portalName))
}
limit, err := buf.getUint32()
if err != nil {
return err
}
stmt := portal.Stmt
portalMeta := portal.ProtocolMeta.(preparedPortalMeta)
pinfo := parser.PlaceholderInfo{
Types: stmt.SQLTypes,
Values: portal.Qargs,
}
return c.executeStatements(ctx, stmt.Query, &pinfo, portalMeta.outFormats, false, int(limit))
}
func (c *v3Conn) executeStatements(
ctx context.Context,
stmts string,
pinfo *parser.PlaceholderInfo,
formatCodes []formatCode,
sendDescription bool,
limit int,
) error {
tracing.AnnotateTrace()
results := c.executor.ExecuteStatements(ctx, c.session, stmts, pinfo)
tracing.AnnotateTrace()
if results.Empty {
// Skip executor and just send EmptyQueryResponse.
c.writeBuf.initMsg(serverMsgEmptyQuery)
return c.writeBuf.finishMsg(c.wr)
}
return c.sendResponse(results.ResultList, formatCodes, sendDescription, limit)
}
func (c *v3Conn) sendCommandComplete(tag []byte) error {
c.writeBuf.initMsg(serverMsgCommandComplete)
c.writeBuf.write(tag)
c.writeBuf.nullTerminate()
return c.writeBuf.finishMsg(c.wr)
}
func (c *v3Conn) sendError(err error) error {
if sqlErr, ok := err.(sqlbase.ErrorWithPGCode); ok {
return c.sendErrorWithCode(sqlErr.Code(), sqlErr.SrcContext(), err.Error())
}
return c.sendInternalError(err.Error())
}
// TODO(andrei): Figure out the correct codes to send for all the errors
// in this file and remove this function.
func (c *v3Conn) sendInternalError(errToSend string) error {
return c.sendErrorWithCode(pgerror.CodeInternalError, sqlbase.MakeSrcCtx(1), errToSend)
}
// errCode is a postgres error code, plus our extensions.
// See http://www.postgresql.org/docs/9.5/static/errcodes-appendix.html
func (c *v3Conn) sendErrorWithCode(errCode string, errCtx sqlbase.SrcCtx, errToSend string) error {
if c.doingExtendedQueryMessage {
c.ignoreTillSync = true
}
c.writeBuf.initMsg(serverMsgErrorResponse)
c.writeBuf.putErrFieldMsg(serverErrFieldSeverity)
c.writeBuf.writeTerminatedString("ERROR")
c.writeBuf.putErrFieldMsg(serverErrFieldSQLState)
c.writeBuf.writeTerminatedString(errCode)
c.writeBuf.putErrFieldMsg(serverErrFieldMsgPrimary)
c.writeBuf.writeTerminatedString(errToSend)
if errCtx.File != "" {
c.writeBuf.putErrFieldMsg(serverErrFieldSrcFile)
c.writeBuf.writeTerminatedString(errCtx.File)
}
if errCtx.Line > 0 {
c.writeBuf.putErrFieldMsg(serverErrFieldSrcLine)
c.writeBuf.writeTerminatedString(strconv.Itoa(errCtx.Line))
}
if errCtx.Function != "" {
c.writeBuf.putErrFieldMsg(serverErrFieldSrcFunction)
c.writeBuf.writeTerminatedString(errCtx.Function)
}
c.writeBuf.nullTerminate()
if err := c.writeBuf.finishMsg(c.wr); err != nil {
return err
}
return c.wr.Flush()
}
func (c *v3Conn) sendResponse(results sql.ResultList, formatCodes []formatCode, sendDescription bool, limit int) error {
if len(results) == 0 {
return c.sendCommandComplete(nil)
}
for _, result := range results {
if result.Err != nil {
if err := c.sendError(result.Err); err != nil {
return err
}
break
}
if limit != 0 && len(result.Rows) > limit {
if err := c.sendInternalError(fmt.Sprintf("execute row count limits not supported: %d of %d", limit, len(result.Rows))); err != nil {
return err
}
break
}
if result.PGTag == "INSERT" {
// From the postgres docs (49.5. Message Formats):
// `INSERT oid rows`... oid is the object ID of the inserted row if
// rows is 1 and the target table has OIDs; otherwise oid is 0.
result.PGTag = "INSERT 0"
}
tag := append(c.tagBuf[:0], result.PGTag...)
switch result.Type {
case parser.RowsAffected:
// Send CommandComplete.
tag = append(tag, ' ')
tag = strconv.AppendInt(tag, int64(result.RowsAffected), 10)
if err := c.sendCommandComplete(tag); err != nil {
return err
}
case parser.Rows:
if sendDescription {
if err := c.sendRowDescription(result.Columns, formatCodes); err != nil {
return err
}
}
// Send DataRows.
for _, row := range result.Rows {
c.writeBuf.initMsg(serverMsgDataRow)
c.writeBuf.putInt16(int16(len(row.Values)))
for i, col := range row.Values {
fmtCode := formatText
if formatCodes != nil {
fmtCode = formatCodes[i]
}
switch fmtCode {
case formatText:
c.writeBuf.writeTextDatum(col, c.session.Location)
case formatBinary:
c.writeBuf.writeBinaryDatum(col)
default:
c.writeBuf.setError(errors.Errorf("unsupported format code %s", fmtCode))
}
}
if err := c.writeBuf.finishMsg(c.wr); err != nil {
return err
}
}
// Send CommandComplete.
tag = append(tag, ' ')
tag = appendUint(tag, uint(len(result.Rows)))
if err := c.sendCommandComplete(tag); err != nil {
return err
}
case parser.Ack, parser.DDL:
if err := c.sendCommandComplete(tag); err != nil {
return err
}
default:
panic(fmt.Sprintf("unexpected result type %v", result.Type))
}
}
return nil
}
func (c *v3Conn) sendRowDescription(columns []sql.ResultColumn, formatCodes []formatCode) error {
if len(columns) == 0 {
c.writeBuf.initMsg(serverMsgNoData)
return c.writeBuf.finishMsg(c.wr)
}
c.writeBuf.initMsg(serverMsgRowDescription)
c.writeBuf.putInt16(int16(len(columns)))
for i, column := range columns {
if log.V(2) {
log.Infof("pgwire writing column %s of type: %T", column.Name, column.Typ)
}
c.writeBuf.writeTerminatedString(column.Name)
typ := typeForDatum(column.Typ)
c.writeBuf.putInt32(0) // Table OID (optional).
c.writeBuf.putInt16(0) // Column attribute ID (optional).
c.writeBuf.putInt32(int32(typ.oid))
c.writeBuf.putInt16(int16(typ.size))
c.writeBuf.putInt32(0) // Type modifier (none of our supported types have modifiers).
if formatCodes == nil {
c.writeBuf.putInt16(int16(formatText))
} else {
c.writeBuf.putInt16(int16(formatCodes[i]))
}
}
return c.writeBuf.finishMsg(c.wr)
}
func appendUint(in []byte, u uint) []byte {
var buf []byte
if cap(in)-len(in) >= 10 {
buf = in[len(in) : len(in)+10]
} else {
buf = make([]byte, 10)
}
i := len(buf)
for u >= 10 {
i--
q := u / 10
buf[i] = byte(u - q*10 + '0')
u = q
}
// u < 10
i--
buf[i] = byte(u + '0')
return append(in, buf[i:]...)
}