You've already forked go-clickhouse
mirror of
https://github.com/uptrace/go-clickhouse.git
synced 2025-06-14 23:44:59 +02:00
Merge pull request #46 from uptrace/fix/append-bytes
chore: use unhex to append bytes
This commit is contained in:
@ -2,12 +2,11 @@ package chschema
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql/driver"
|
"database/sql/driver"
|
||||||
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/uptrace/go-clickhouse/ch/internal"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func Append(fmter Formatter, b []byte, v any) []byte {
|
func Append(fmter Formatter, b []byte, v any) []byte {
|
||||||
@ -117,5 +116,13 @@ func AppendBytes(b []byte, bytes []byte) []byte {
|
|||||||
if bytes == nil {
|
if bytes == nil {
|
||||||
return AppendNull(b)
|
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
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package ch_test
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
@ -337,6 +338,44 @@ type EventColumnar struct {
|
|||||||
CreatedAt []time.Time
|
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) {
|
func TestORM(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user