From ea60f7e05464e6b34eec0dc11eba5b9bedaada88 Mon Sep 17 00:00:00 2001 From: daguang Date: Sat, 19 Feb 2022 17:49:53 +0800 Subject: [PATCH] improve clickhouse driver --- contrib/drivers/clickhouse/clickhouse.go | 10 ++-- contrib/drivers/clickhouse/clickhouse_test.go | 50 +++++++++++++++++++ database/gdb/gdb.go | 5 +- database/gdb/gdb_core.go | 14 +++--- 4 files changed, 67 insertions(+), 12 deletions(-) diff --git a/contrib/drivers/clickhouse/clickhouse.go b/contrib/drivers/clickhouse/clickhouse.go index 4dea4a2ed..a5fc94de8 100644 --- a/contrib/drivers/clickhouse/clickhouse.go +++ b/contrib/drivers/clickhouse/clickhouse.go @@ -221,15 +221,17 @@ func (d *Driver) DoDeleteSQL(ctx context.Context, link gdb.Link, table string, c return d.Core.DoExec(ctx, link, fmt.Sprintf("ALTER TABLE %s DELETE %s", table, condition), args...) } -func (d *Driver) DoInsert(ctx context.Context, link gdb.Link, table string, data gdb.List, option gdb.DoInsertOption) (result sql.Result, err error) { - return -} - +// DoCommit commits current sql and arguments to underlying sql driver. func (d *Driver) DoCommit(ctx context.Context, in gdb.DoCommitInput) (out gdb.DoCommitOutput, err error) { in.IsIgnoreResult = true return d.Core.DoCommit(ctx, in) } +func (d *Driver) DoInsert(ctx context.Context, link gdb.Link, table string, list gdb.List, option gdb.DoInsertOption) (result sql.Result, err error) { + option.IsIgnoreResult = true + return d.Core.DoInsert(ctx, link, table, list, option) +} + // InsertIgnore Other queries for modifying data parts are not supported: REPLACE, MERGE, UPSERT, INSERT UPDATE. func (d *Driver) InsertIgnore(ctx context.Context, table string, data interface{}, batch ...int) (sql.Result, error) { return nil, ErrUnsupportedInsertIgnore diff --git a/contrib/drivers/clickhouse/clickhouse_test.go b/contrib/drivers/clickhouse/clickhouse_test.go index 76274d6a9..8f867be83 100644 --- a/contrib/drivers/clickhouse/clickhouse_test.go +++ b/contrib/drivers/clickhouse/clickhouse_test.go @@ -9,6 +9,7 @@ import ( "github.com/gogf/gf/v2/database/gdb" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/test/gtest" + "github.com/gogf/gf/v2/util/gconv" "github.com/gogf/gf/v2/util/grand" ) @@ -104,6 +105,55 @@ func TestDriverClickhouse_Select(t *testing.T) { gtest.AssertEQ(len(data), 0) } +func TestDriver_InsertIgnore(t *testing.T) { + connect := InitClickhouse() + _, err := connect.InsertIgnore(context.Background(), "", nil) + gtest.AssertEQ(err, ErrUnsupportedInsertIgnore) +} + +func TestDriver_InsertAndGetId(t *testing.T) { + connect := InitClickhouse() + _, err := connect.InsertAndGetId(context.Background(), "", nil) + gtest.AssertEQ(err, ErrUnsupportedInsertGetId) +} + +func TestDriver_Replace(t *testing.T) { + connect := InitClickhouse() + _, err := connect.Replace(context.Background(), "", nil) + gtest.AssertEQ(err, ErrUnsupportedReplace) +} + +func TestDriverClickhouse_DoInsertOne(t *testing.T) { + connect := InitClickhouse() + gtest.AssertEQ(createClickhouseTable(connect), nil) + defer dropClickhouseTable(connect) + _, err := connect.Model("visits").Data(g.Map{ + "id": grand.Intn(999), + "duration": grand.Intn(999), + "url": grand.Intn(999), + "created": grand.Intn(999), + }).Insert() + gtest.AssertNil(err) +} + +func TestDriver_DoInsertMany(t *testing.T) { + connect := InitClickhouse() + gtest.AssertEQ(createClickhouseTable(connect), nil) + defer dropClickhouseTable(connect) + tx, err := connect.Begin(context.Background()) + for i := 0; i < 10; i++ { + _, err = tx.Model("visits").Data(g.Map{ + "id": grand.Intn(999), + "duration": float64(grand.Intn(999)), + "url": gconv.String(grand.Intn(999)), + "created": time.Now().Format("2006-01-02 15:04:05"), + }). + Save() + gtest.AssertNil(err) + } + gtest.AssertNil(tx.Commit()) +} + func TestDriverClickhouse_DoInsert(t *testing.T) { connect := InitClickhouse() gtest.AssertEQ(createClickhouseTable(connect), nil) diff --git a/database/gdb/gdb.go b/database/gdb/gdb.go index 85b8f0e7d..ecbd8cfab 100644 --- a/database/gdb/gdb.go +++ b/database/gdb/gdb.go @@ -242,8 +242,9 @@ type Sql struct { type DoInsertOption struct { OnDuplicateStr string OnDuplicateMap map[string]interface{} - InsertOption int // Insert operation. - BatchCount int // Batch count for batch inserting. + InsertOption int // Insert operation. + BatchCount int // Batch count for batch inserting. + IsIgnoreResult bool // IgnoreResult } // TableField is the struct for table field. diff --git a/database/gdb/gdb_core.go b/database/gdb/gdb_core.go index 9f4f1ca59..e92e1be94 100644 --- a/database/gdb/gdb_core.go +++ b/database/gdb/gdb_core.go @@ -450,12 +450,14 @@ func (c *Core) DoInsert(ctx context.Context, link Link, table string, list List, if err != nil { return stdSqlResult, err } - if affectedRows, err = stdSqlResult.RowsAffected(); err != nil { - err = gerror.WrapCode(gcode.CodeDbOperationError, err, `sql.Result.RowsAffected failed`) - return stdSqlResult, err - } else { - batchResult.Result = stdSqlResult - batchResult.Affected += affectedRows + if !option.IsIgnoreResult { + if affectedRows, err = stdSqlResult.RowsAffected(); err != nil { + err = gerror.WrapCode(gcode.CodeDbOperationError, err, `sql.Result.RowsAffected failed`) + return stdSqlResult, err + } else { + batchResult.Result = stdSqlResult + batchResult.Affected += affectedRows + } } params = params[:0] valueHolder = valueHolder[:0]