MCPcopy
hub / github.com/jackc/pgx / Run

Method Run

pgproto3/example/pgfortune/server.go:28–73  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

26}
27
28func (p *PgFortuneBackend) Run() error {
29 defer p.Close()
30
31 err := p.handleStartup()
32 if err != nil {
33 return err
34 }
35
36 for {
37 msg, err := p.backend.Receive()
38 if err != nil {
39 return fmt.Errorf("error receiving message: %w", err)
40 }
41
42 switch msg.(type) {
43 case *pgproto3.Query:
44 response, err := p.responder()
45 if err != nil {
46 return fmt.Errorf("error generating query response: %w", err)
47 }
48
49 buf := mustEncode((&pgproto3.RowDescription{Fields: []pgproto3.FieldDescription{
50 {
51 Name: []byte("fortune"),
52 TableOID: 0,
53 TableAttributeNumber: 0,
54 DataTypeOID: 25,
55 DataTypeSize: -1,
56 TypeModifier: -1,
57 Format: 0,
58 },
59 }}).Encode(nil))
60 buf = mustEncode((&pgproto3.DataRow{Values: [][]byte{response}}).Encode(buf))
61 buf = mustEncode((&pgproto3.CommandComplete{CommandTag: []byte("SELECT 1")}).Encode(buf))
62 buf = mustEncode((&pgproto3.ReadyForQuery{TxStatus: 'I'}).Encode(buf))
63 _, err = p.conn.Write(buf)
64 if err != nil {
65 return fmt.Errorf("error writing query response: %w", err)
66 }
67 case *pgproto3.Terminate:
68 return nil
69 default:
70 return fmt.Errorf("received message other than Query from client: %#v", msg)
71 }
72 }
73}
74
75func (p *PgFortuneBackend) handleStartup() error {
76 startupMessage, err := p.backend.ReceiveStartupMessage()

Callers 15

mainFunction · 0.95
TestStartupMessageFunction · 0.45
TestCancelRequestDecodeFunction · 0.45
TestCancelRequestEncodeFunction · 0.45
TestJSONUnmarshalBindFunction · 0.45
TestConfigErrorFunction · 0.45

Calls 6

CloseMethod · 0.95
handleStartupMethod · 0.95
mustEncodeFunction · 0.70
EncodeMethod · 0.65
ReceiveMethod · 0.45
WriteMethod · 0.45

Tested by 15

TestStartupMessageFunction · 0.36
TestCancelRequestDecodeFunction · 0.36
TestCancelRequestEncodeFunction · 0.36
TestJSONUnmarshalBindFunction · 0.36
TestConfigErrorFunction · 0.36
TestConnectFunction · 0.36