Skip to content

Commit b7ab973

Browse files
rmestrerjpmestre
rmestre
authored andcommitted
Encode bytea fields
1 parent 22da837 commit b7ab973

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-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

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

0 commit comments

Comments
 (0)