aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/jackc/pgx/v5/pgproto3/error_response.go
blob: 6ef9bd0614acd6a731271a5a14e7f720f51ea778 (plain) (blame)
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
package pgproto3

import (
	"bytes"
	"encoding/json"
	"strconv"
)

type ErrorResponse struct {
	Severity            string
	SeverityUnlocalized string // only in 9.6 and greater
	Code                string
	Message             string
	Detail              string
	Hint                string
	Position            int32
	InternalPosition    int32
	InternalQuery       string
	Where               string
	SchemaName          string
	TableName           string
	ColumnName          string
	DataTypeName        string
	ConstraintName      string
	File                string
	Line                int32
	Routine             string

	UnknownFields map[byte]string
}

// Backend identifies this message as sendable by the PostgreSQL backend.
func (*ErrorResponse) Backend() {}

// Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message
// type identifier and 4 byte message length.
func (dst *ErrorResponse) Decode(src []byte) error {
	*dst = ErrorResponse{}

	buf := bytes.NewBuffer(src)

	for {
		k, err := buf.ReadByte()
		if err != nil {
			return err
		}
		if k == 0 {
			break
		}

		vb, err := buf.ReadBytes(0)
		if err != nil {
			return err
		}
		v := string(vb[:len(vb)-1])

		switch k {
		case 'S':
			dst.Severity = v
		case 'V':
			dst.SeverityUnlocalized = v
		case 'C':
			dst.Code = v
		case 'M':
			dst.Message = v
		case 'D':
			dst.Detail = v
		case 'H':
			dst.Hint = v
		case 'P':
			s := v
			n, _ := strconv.ParseInt(s, 10, 32)
			dst.Position = int32(n)
		case 'p':
			s := v
			n, _ := strconv.ParseInt(s, 10, 32)
			dst.InternalPosition = int32(n)
		case 'q':
			dst.InternalQuery = v
		case 'W':
			dst.Where = v
		case 's':
			dst.SchemaName = v
		case 't':
			dst.TableName = v
		case 'c':
			dst.ColumnName = v
		case 'd':
			dst.DataTypeName = v
		case 'n':
			dst.ConstraintName = v
		case 'F':
			dst.File = v
		case 'L':
			s := v
			n, _ := strconv.ParseInt(s, 10, 32)
			dst.Line = int32(n)
		case 'R':
			dst.Routine = v

		default:
			if dst.UnknownFields == nil {
				dst.UnknownFields = make(map[byte]string)
			}
			dst.UnknownFields[k] = v
		}
	}

	return nil
}

// Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length.
func (src *ErrorResponse) Encode(dst []byte) ([]byte, error) {
	dst, sp := beginMessage(dst, 'E')
	dst = src.appendFields(dst)
	return finishMessage(dst, sp)
}

func (src *ErrorResponse) appendFields(dst []byte) []byte {
	if src.Severity != "" {
		dst = append(dst, 'S')
		dst = append(dst, src.Severity...)
		dst = append(dst, 0)
	}
	if src.SeverityUnlocalized != "" {
		dst = append(dst, 'V')
		dst = append(dst, src.SeverityUnlocalized...)
		dst = append(dst, 0)
	}
	if src.Code != "" {
		dst = append(dst, 'C')
		dst = append(dst, src.Code...)
		dst = append(dst, 0)
	}
	if src.Message != "" {
		dst = append(dst, 'M')
		dst = append(dst, src.Message...)
		dst = append(dst, 0)
	}
	if src.Detail != "" {
		dst = append(dst, 'D')
		dst = append(dst, src.Detail...)
		dst = append(dst, 0)
	}
	if src.Hint != "" {
		dst = append(dst, 'H')
		dst = append(dst, src.Hint...)
		dst = append(dst, 0)
	}
	if src.Position != 0 {
		dst = append(dst, 'P')
		dst = append(dst, strconv.Itoa(int(src.Position))...)
		dst = append(dst, 0)
	}
	if src.InternalPosition != 0 {
		dst = append(dst, 'p')
		dst = append(dst, strconv.Itoa(int(src.InternalPosition))...)
		dst = append(dst, 0)
	}
	if src.InternalQuery != "" {
		dst = append(dst, 'q')
		dst = append(dst, src.InternalQuery...)
		dst = append(dst, 0)
	}
	if src.Where != "" {
		dst = append(dst, 'W')
		dst = append(dst, src.Where...)
		dst = append(dst, 0)
	}
	if src.SchemaName != "" {
		dst = append(dst, 's')
		dst = append(dst, src.SchemaName...)
		dst = append(dst, 0)
	}
	if src.TableName != "" {
		dst = append(dst, 't')
		dst = append(dst, src.TableName...)
		dst = append(dst, 0)
	}
	if src.ColumnName != "" {
		dst = append(dst, 'c')
		dst = append(dst, src.ColumnName...)
		dst = append(dst, 0)
	}
	if src.DataTypeName != "" {
		dst = append(dst, 'd')
		dst = append(dst, src.DataTypeName...)
		dst = append(dst, 0)
	}
	if src.ConstraintName != "" {
		dst = append(dst, 'n')
		dst = append(dst, src.ConstraintName...)
		dst = append(dst, 0)
	}
	if src.File != "" {
		dst = append(dst, 'F')
		dst = append(dst, src.File...)
		dst = append(dst, 0)
	}
	if src.Line != 0 {
		dst = append(dst, 'L')
		dst = append(dst, strconv.Itoa(int(src.Line))...)
		dst = append(dst, 0)
	}
	if src.Routine != "" {
		dst = append(dst, 'R')
		dst = append(dst, src.Routine...)
		dst = append(dst, 0)
	}

	for k, v := range src.UnknownFields {
		dst = append(dst, k)
		dst = append(dst, v...)
		dst = append(dst, 0)
	}

	dst = append(dst, 0)

	return dst
}

// MarshalJSON implements encoding/json.Marshaler.
func (src ErrorResponse) MarshalJSON() ([]byte, error) {
	return json.Marshal(struct {
		Type                string
		Severity            string
		SeverityUnlocalized string // only in 9.6 and greater
		Code                string
		Message             string
		Detail              string
		Hint                string
		Position            int32
		InternalPosition    int32
		InternalQuery       string
		Where               string
		SchemaName          string
		TableName           string
		ColumnName          string
		DataTypeName        string
		ConstraintName      string
		File                string
		Line                int32
		Routine             string

		UnknownFields map[byte]string
	}{
		Type:                "ErrorResponse",
		Severity:            src.Severity,
		SeverityUnlocalized: src.SeverityUnlocalized,
		Code:                src.Code,
		Message:             src.Message,
		Detail:              src.Detail,
		Hint:                src.Hint,
		Position:            src.Position,
		InternalPosition:    src.InternalPosition,
		InternalQuery:       src.InternalQuery,
		Where:               src.Where,
		SchemaName:          src.SchemaName,
		TableName:           src.TableName,
		ColumnName:          src.ColumnName,
		DataTypeName:        src.DataTypeName,
		ConstraintName:      src.ConstraintName,
		File:                src.File,
		Line:                src.Line,
		Routine:             src.Routine,
		UnknownFields:       src.UnknownFields,
	})
}

// UnmarshalJSON implements encoding/json.Unmarshaler.
func (dst *ErrorResponse) UnmarshalJSON(data []byte) error {
	// Ignore null, like in the main JSON package.
	if string(data) == "null" {
		return nil
	}

	var msg struct {
		Type                string
		Severity            string
		SeverityUnlocalized string // only in 9.6 and greater
		Code                string
		Message             string
		Detail              string
		Hint                string
		Position            int32
		InternalPosition    int32
		InternalQuery       string
		Where               string
		SchemaName          string
		TableName           string
		ColumnName          string
		DataTypeName        string
		ConstraintName      string
		File                string
		Line                int32
		Routine             string

		UnknownFields map[byte]string
	}
	if err := json.Unmarshal(data, &msg); err != nil {
		return err
	}

	dst.Severity = msg.Severity
	dst.SeverityUnlocalized = msg.SeverityUnlocalized
	dst.Code = msg.Code
	dst.Message = msg.Message
	dst.Detail = msg.Detail
	dst.Hint = msg.Hint
	dst.Position = msg.Position
	dst.InternalPosition = msg.InternalPosition
	dst.InternalQuery = msg.InternalQuery
	dst.Where = msg.Where
	dst.SchemaName = msg.SchemaName
	dst.TableName = msg.TableName
	dst.ColumnName = msg.ColumnName
	dst.DataTypeName = msg.DataTypeName
	dst.ConstraintName = msg.ConstraintName
	dst.File = msg.File
	dst.Line = msg.Line
	dst.Routine = msg.Routine

	dst.UnknownFields = msg.UnknownFields

	return nil
}