Skip to content

Commit 5d0d7b1

Browse files
rmestreaaannz
rmestre
authored andcommitted
Encode bytea fields
1 parent e8ad3ec commit 5d0d7b1

3 files changed

+77
-0
lines changed

dumper/dataWriter.go

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package dumper
77
import (
88
"bufio"
99
"database/sql"
10+
"encoding/hex"
1011
"encoding/json"
1112
"fmt"
1213
"strings"
@@ -380,6 +381,8 @@ func formatField(col sqlUtil.RowDataStructure) string {
380381
val = pq.QuoteLiteral(string(pq.FormatTimestamp(col.Value.(time.Time))))
381382
case "SQL":
382383
val = fmt.Sprintf(`(%s)`, col.Value)
384+
case "BYTEA":
385+
return fmt.Sprintf(`decode('%s', 'hex')`, hex.EncodeToString(col.Value.([]byte)))
383386
default:
384387
val = pq.QuoteLiteral(fmt.Sprintf("%s", col.Value))
385388
}

dumper/dataWriter_test.go

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// SPDX-FileCopyrightText: 2024 SUSE LLC
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
package dumper
6+
7+
import (
8+
"testing"
9+
"time"
10+
11+
"github.com/uyuni-project/inter-server-sync/sqlUtil"
12+
)
13+
14+
func TestFormatField(t *testing.T) {
15+
tests := []struct {
16+
col sqlUtil.RowDataStructure
17+
expectedVal string
18+
}{
19+
// Test case for NULL value
20+
{
21+
col: sqlUtil.RowDataStructure{},
22+
expectedVal: "null",
23+
},
24+
// Test case for NUMERIC column type
25+
{
26+
col: sqlUtil.RowDataStructure{ColumnType: "NUMERIC", Value: "10"},
27+
expectedVal: "10",
28+
},
29+
{
30+
col: sqlUtil.RowDataStructure{ColumnType: "NUMERIC", Value: "-10"},
31+
expectedVal: "-10",
32+
},
33+
// Test case for TIMESTAMPTZ and TIMESTAMP column types
34+
{
35+
col: sqlUtil.RowDataStructure{ColumnType: "TIMESTAMPTZ", Value: time.Date(1984, time.July, 9, 17, 20, 0, 0, time.UTC)},
36+
expectedVal: "'1984-07-09 17:20:00Z'",
37+
},
38+
{
39+
col: sqlUtil.RowDataStructure{ColumnType: "TIMESTAMPTZ", Value: time.Date(2019, time.May, 29, 13, 49, 0, 0, time.FixedZone("UTC-1", -3600))},
40+
expectedVal: "'2019-05-29 13:49:00-01:00'",
41+
},
42+
{
43+
col: sqlUtil.RowDataStructure{ColumnType: "TIMESTAMPTZ", Value: time.Date(2021, time.June, 25, 0, 56, 0, 0, time.FixedZone("UTC+1", 3600))},
44+
expectedVal: "'2021-06-25 00:56:00+01:00'",
45+
},
46+
// Test case for SQL column type
47+
{
48+
col: sqlUtil.RowDataStructure{ColumnType: "SQL", Value: "SELECT * FROM table"},
49+
expectedVal: "(SELECT * FROM table)",
50+
},
51+
// Test case for BYTEA column type
52+
{
53+
col: sqlUtil.RowDataStructure{ColumnType: "BYTEA", Value: []byte("hello")},
54+
expectedVal: "decode('68656c6c6f', 'hex')",
55+
},
56+
{
57+
col: sqlUtil.RowDataStructure{ColumnType: "BYTEA", Value: []byte("\"\\[\\e[0;32m\\]\\u@\\h:\\w\\$ \\[\\e[m\\]\ntest\"")},
58+
expectedVal: "decode('225c5b5c655b303b33326d5c5d5c75405c683a5c775c24205c5b5c655b6d5c5d0a7465737422', 'hex')",
59+
},
60+
// Test case for default column type
61+
{
62+
col: sqlUtil.RowDataStructure{ColumnType: "DEFAULT", Value: "default"},
63+
expectedVal: "'default'",
64+
},
65+
}
66+
67+
for _, test := range tests {
68+
result := formatField(test.col)
69+
if result != test.expectedVal {
70+
t.Errorf("formatField(%+v) = %s; expected %s", test.col, result, test.expectedVal)
71+
}
72+
}
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Encode bytea fields (bsc#219965)

0 commit comments

Comments
 (0)