@@ -23,11 +23,12 @@ import (
23
23
"bytes"
24
24
"encoding/base64"
25
25
"encoding/hex"
26
+ "fmt"
27
+ "strings"
26
28
"unicode"
27
29
28
30
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
29
31
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
30
- "github.com/cockroachdb/cockroach/pkg/sql/sessiondatapb"
31
32
"github.com/cockroachdb/errors"
32
33
"golang.org/x/text/language"
33
34
)
@@ -92,11 +93,9 @@ func LocaleNamesAreEqual(a, b string) bool {
92
93
// If the skipHexPrefix argument is set, the hexadecimal encoding does not
93
94
// prefix the output with "\x". This is suitable e.g. for the encode()
94
95
// built-in.
95
- func EncodeByteArrayToRawBytes (
96
- data string , be sessiondatapb.BytesEncodeFormat , skipHexPrefix bool ,
97
- ) string {
96
+ func EncodeByteArrayToRawBytes (data string , be BytesEncodeFormat , skipHexPrefix bool ) string {
98
97
switch be {
99
- case sessiondatapb . BytesEncodeHex :
98
+ case BytesEncodeHex :
100
99
head := 2
101
100
if skipHexPrefix {
102
101
head = 0
@@ -109,7 +108,7 @@ func EncodeByteArrayToRawBytes(
109
108
hex .Encode (res [head :], []byte (data ))
110
109
return string (res )
111
110
112
- case sessiondatapb . BytesEncodeEscape :
111
+ case BytesEncodeEscape :
113
112
// PostgreSQL does not allow all the escapes formats recognized by
114
113
// CockroachDB's scanner. It only recognizes octal and \\ for the
115
114
// backslash itself.
@@ -131,7 +130,7 @@ func EncodeByteArrayToRawBytes(
131
130
}
132
131
return string (res )
133
132
134
- case sessiondatapb . BytesEncodeBase64 :
133
+ case BytesEncodeBase64 :
135
134
return base64 .StdEncoding .EncodeToString ([]byte (data ))
136
135
137
136
default :
@@ -144,12 +143,12 @@ func EncodeByteArrayToRawBytes(
144
143
// When using the Hex format, the caller is responsible for skipping the
145
144
// "\x" prefix, if any. See DecodeRawBytesToByteArrayAuto() below for
146
145
// an alternative.
147
- func DecodeRawBytesToByteArray (data string , be sessiondatapb. BytesEncodeFormat ) ([]byte , error ) {
146
+ func DecodeRawBytesToByteArray (data string , be BytesEncodeFormat ) ([]byte , error ) {
148
147
switch be {
149
- case sessiondatapb . BytesEncodeHex :
148
+ case BytesEncodeHex :
150
149
return hex .DecodeString (data )
151
150
152
- case sessiondatapb . BytesEncodeEscape :
151
+ case BytesEncodeEscape :
153
152
// PostgreSQL does not allow all the escapes formats recognized by
154
153
// CockroachDB's scanner. It only recognizes octal and \\ for the
155
154
// backslash itself.
@@ -188,7 +187,7 @@ func DecodeRawBytesToByteArray(data string, be sessiondatapb.BytesEncodeFormat)
188
187
}
189
188
return res , nil
190
189
191
- case sessiondatapb . BytesEncodeBase64 :
190
+ case BytesEncodeBase64 :
192
191
return base64 .StdEncoding .DecodeString (data )
193
192
194
193
default :
@@ -201,7 +200,34 @@ func DecodeRawBytesToByteArray(data string, be sessiondatapb.BytesEncodeFormat)
201
200
// and escape.
202
201
func DecodeRawBytesToByteArrayAuto (data []byte ) ([]byte , error ) {
203
202
if len (data ) >= 2 && data [0 ] == '\\' && (data [1 ] == 'x' || data [1 ] == 'X' ) {
204
- return DecodeRawBytesToByteArray (string (data [2 :]), sessiondatapb .BytesEncodeHex )
203
+ return DecodeRawBytesToByteArray (string (data [2 :]), BytesEncodeHex )
204
+ }
205
+ return DecodeRawBytesToByteArray (string (data ), BytesEncodeEscape )
206
+ }
207
+
208
+ func (f BytesEncodeFormat ) String () string {
209
+ switch f {
210
+ case BytesEncodeHex :
211
+ return "hex"
212
+ case BytesEncodeEscape :
213
+ return "escape"
214
+ case BytesEncodeBase64 :
215
+ return "base64"
216
+ default :
217
+ return fmt .Sprintf ("invalid (%d)" , f )
218
+ }
219
+ }
220
+
221
+ // BytesEncodeFormatFromString converts a string into a BytesEncodeFormat.
222
+ func BytesEncodeFormatFromString (val string ) (_ BytesEncodeFormat , ok bool ) {
223
+ switch strings .ToUpper (val ) {
224
+ case "HEX" :
225
+ return BytesEncodeHex , true
226
+ case "ESCAPE" :
227
+ return BytesEncodeEscape , true
228
+ case "BASE64" :
229
+ return BytesEncodeBase64 , true
230
+ default :
231
+ return - 1 , false
205
232
}
206
- return DecodeRawBytesToByteArray (string (data ), sessiondatapb .BytesEncodeEscape )
207
233
}
0 commit comments