mirror of
https://gitee.com/johng/gf
synced 2026-07-05 21:32:17 +08:00
refactor(contrib/clickhouse): optimization clickhouse (#4499)
1. close stmt 2. fix assert *gtime.Time
This commit is contained in:
@ -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
|
||||
}
|
||||
@ -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() {
|
||||
|
||||
@ -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
|
||||
@ -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
|
||||
@ -81,5 +80,4 @@ func (d *Driver) ConvertValueForField(ctx context.Context, fieldType string, fie
|
||||
}
|
||||
return convertedValue, nil
|
||||
}
|
||||
return fieldValue, nil
|
||||
}
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -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)
|
||||
@ -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 {
|
||||
|
||||
@ -303,6 +303,8 @@ func Test_DB_Tables(t *testing.T) {
|
||||
createTable(v)
|
||||
}
|
||||
|
||||
defer dropTable(tables...)
|
||||
|
||||
result, err := db.Tables(ctx)
|
||||
gtest.AssertNil(err)
|
||||
|
||||
|
||||
@ -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) {
|
||||
|
||||
@ -8,7 +8,6 @@ package clickhouse
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@ -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)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user