Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions contrib/drivers/clickhouse/clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/gogf/gf/v2/os/gctx"
)

// Driver is the driver for postgresql database.
// Driver is the driver for clickhouse database.
type Driver struct {
*gdb.Core
}
Expand All @@ -29,12 +29,11 @@ var (
)

const (
updateFilterPattern = `(?i)UPDATE[\s]+?(\w+[\.]?\w+)[\s]+?SET`
deleteFilterPattern = `(?i)DELETE[\s]+?FROM[\s]+?(\w+[\.]?\w+)`
filterTypePattern = `(?i)^UPDATE|DELETE`
replaceSchemaPattern = `@(.+?)/([\w\.\-]+)+`
needParsedSqlInCtx gctx.StrKey = "NeedParsedSql"
driverName = "clickhouse"
updateFilterPattern = `(?i)UPDATE[\s]+?(\w+[\.]?\w+)[\s]+?SET`
deleteFilterPattern = `(?i)DELETE[\s]+?FROM[\s]+?(\w+[\.]?\w+)`
filterTypePattern = `(?i)^UPDATE|DELETE`
needParsedSqlInCtx gctx.StrKey = "NeedParsedSql"
driverName = "clickhouse"
)

func init() {
Expand Down
8 changes: 3 additions & 5 deletions contrib/drivers/clickhouse/clickhouse_convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func (d *Driver) ConvertValueForField(ctx context.Context, fieldType string, fie
if itemValue.IsZero() {
return nil, nil
}
return itemValue, nil

case uuid.UUID:
return itemValue, nil
Expand All @@ -48,15 +49,13 @@ func (d *Driver) ConvertValueForField(ctx context.Context, fieldType string, fie
return itemValue.Time, nil

case *gtime.Time:
// for gtime type, needs to get time.Time
if itemValue != nil {
return itemValue.Time, nil
}
// If the time is zero, it then updates it to nil,
// which will insert/update the value to database as "null".
if itemValue == nil || itemValue.IsZero() {
return nil, nil
}
// for gtime type, needs to get time.Time
return itemValue.Time, nil

case decimal.Decimal:
return itemValue, nil
Expand All @@ -81,5 +80,4 @@ func (d *Driver) ConvertValueForField(ctx context.Context, fieldType string, fie
}
return convertedValue, nil
}
return fieldValue, nil
}
8 changes: 3 additions & 5 deletions contrib/drivers/clickhouse/clickhouse_do_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,11 @@ func (d *Driver) DoFilter(
}
return newSql, args, nil

default:
return originSql, args, nil
}
return originSql, args, nil
}

func (d *Driver) getNeedParsedSqlFromCtx(ctx context.Context) bool {
if ctx.Value(needParsedSqlInCtx) != nil {
return true
}
return false
return ctx.Value(needParsedSqlInCtx) != nil
}
13 changes: 8 additions & 5 deletions contrib/drivers/clickhouse/clickhouse_do_insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ import (
func (d *Driver) DoInsert(
ctx context.Context, link gdb.Link, table string, list gdb.List, option gdb.DoInsertOption,
) (result sql.Result, err error) {
var (
keys []string // Field names.
valueHolder = make([]string, 0)
)
var keys, valueHolder []string

// Handle the field names and placeholders.
for k := range list[0] {
keys = append(keys, k)
Expand Down Expand Up @@ -56,7 +54,12 @@ func (d *Driver) DoInsert(
if err != nil {
return
}
for i := 0; i < len(list); i++ {

defer func() {
_ = stmt.Close()
}()

for i := range len(list) {
// Values that will be committed to underlying database driver.
params := make([]any, 0)
for _, k := range keys {
Expand Down
2 changes: 2 additions & 0 deletions contrib/drivers/clickhouse/clickhouse_z_unit_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,8 @@ func Test_DB_Tables(t *testing.T) {
createTable(v)
}

defer dropTable(tables...)

result, err := db.Tables(ctx)
gtest.AssertNil(err)

Expand Down
6 changes: 4 additions & 2 deletions contrib/drivers/clickhouse/clickhouse_z_unit_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ func createInitTable(table ...string) string {
return createInitTableWithDb(db, table...)
}

func dropTable(table string) {
dropTableWithDb(db, table)
func dropTable(tables ...string) {
for _, table := range tables {
dropTableWithDb(db, table)
}
}

func createTableWithDb(db gdb.DB, table ...string) (name string) {
Expand Down
9 changes: 4 additions & 5 deletions contrib/drivers/clickhouse/clickhouse_z_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package clickhouse

import (
"context"
"fmt"
"testing"
"time"

Expand Down Expand Up @@ -166,22 +165,22 @@ func createClickhouseExampleTable(connect gdb.DB) error {
}

func dropClickhouseTableVisits(conn gdb.DB) {
sqlStr := fmt.Sprintf("DROP TABLE IF EXISTS `visits`")
sqlStr := "DROP TABLE IF EXISTS `visits`"
_, _ = conn.Exec(context.Background(), sqlStr)
}

func dropClickhouseTableDim(conn gdb.DB) {
sqlStr := fmt.Sprintf("DROP TABLE IF EXISTS `dim`")
sqlStr := "DROP TABLE IF EXISTS `dim`"
_, _ = conn.Exec(context.Background(), sqlStr)
}

func dropClickhouseTableFact(conn gdb.DB) {
sqlStr := fmt.Sprintf("DROP TABLE IF EXISTS `fact`")
sqlStr := "DROP TABLE IF EXISTS `fact`"
_, _ = conn.Exec(context.Background(), sqlStr)
}

func dropClickhouseExampleTable(conn gdb.DB) {
sqlStr := fmt.Sprintf("DROP TABLE IF EXISTS `data_type`")
sqlStr := "DROP TABLE IF EXISTS `data_type`"
_, _ = conn.Exec(context.Background(), sqlStr)
}

Expand Down
Loading