1
0
mirror of https://github.com/uptrace/go-clickhouse.git synced 2025-06-08 23:26:11 +02:00

Merge pull request #46 from uptrace/fix/append-bytes

chore: use unhex to append bytes
This commit is contained in:
Vladimir Mihailenco 2022-09-03 09:41:05 +03:00 committed by GitHub
commit 0da7ce6381
2 changed files with 49 additions and 3 deletions

View File

@ -2,12 +2,11 @@ package chschema
import (
"database/sql/driver"
"encoding/hex"
"fmt"
"math"
"strconv"
"time"
"github.com/uptrace/go-clickhouse/ch/internal"
)
func Append(fmter Formatter, b []byte, v any) []byte {
@ -117,5 +116,13 @@ func AppendBytes(b []byte, bytes []byte) []byte {
if bytes == nil {
return AppendNull(b)
}
return AppendString(b, internal.String(bytes))
tmp := make([]byte, hex.EncodedLen(len(bytes)))
hex.Encode(tmp, bytes)
b = append(b, "unhex('"...)
b = append(b, tmp...)
b = append(b, "')"...)
return b
}

View File

@ -3,6 +3,7 @@ package ch_test
import (
"context"
"database/sql"
"encoding/hex"
"fmt"
"os"
"reflect"
@ -337,6 +338,44 @@ type EventColumnar struct {
CreatedAt []time.Time
}
func TestClickhouse(t *testing.T) {
ctx := context.Background()
db := chDB()
defer db.Close()
tests := []func(ctx context.Context, t *testing.T, db *ch.DB){
testWhereBytes,
}
for _, fn := range tests {
t.Run(funcName(fn), func(t *testing.T) {
fn(ctx, t, db)
})
}
}
func testWhereBytes(ctx context.Context, t *testing.T, db *ch.DB) {
type Data struct {
Bytes []byte
}
err := db.ResetModel(ctx, (*Data)(nil))
require.NoError(t, err)
src, _ := hex.DecodeString("5C00CC")
data := &Data{Bytes: src}
_, err = db.NewInsert().Model(data).Exec(context.Background())
require.NoError(t, err)
got := new(Data)
err = db.NewSelect().Model(got).
Where("bytes = ?", data.Bytes).
Scan(ctx)
require.NoError(t, err)
require.Equal(t, data.Bytes, got.Bytes)
}
func TestORM(t *testing.T) {
ctx := context.Background()