mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
This pull request introduces significant improvements to the handling of the `Replace` and `Save` operations for multiple database drivers, especially for MSSQL and PostgreSQL. The changes ensure that these operations now auto-detect primary keys when conflict columns are not explicitly provided, improving usability and aligning behavior across drivers. Additionally, the pull request updates related tests to reflect these enhancements and includes some minor documentation and code cleanup. **Key changes:** ### Enhanced Replace/Save Logic for Database Drivers * **MSSQL Driver:** - `Replace` and `Save` operations now auto-detect primary keys if `OnConflict` is not specified, using the `MERGE` statement for upsert functionality. If no primary key is found in the data, a detailed error is returned. [[1]](diffhunk://#diff-87815aa559a927e2de09bd05148f9841dfc06a1b5f3ecc5e3d5fcb80323a87f8L23-R61) [[2]](diffhunk://#diff-87815aa559a927e2de09bd05148f9841dfc06a1b5f3ecc5e3d5fcb80323a87f8L43-L59) - Updated tests to verify that `Replace` correctly updates or inserts records, and that missing conflict columns are properly handled. [[1]](diffhunk://#diff-bdbde9d7d6ee14c795343767b414740c4396f4dd3e97788b1f9d4e615405a42dL141-R151) [[2]](diffhunk://#diff-26338e93e473300b1313936eb0f6826546473793442f24715fa294b595f7a805L2661-R2707) * **PostgreSQL Driver:** - Similar to MSSQL, `Replace` and `Save` now auto-detect primary keys for conflict resolution if `OnConflict` is not set, and treat `Replace` as a `Save` operation. - Adjusted tests to ensure `Save` and `Replace` work as expected, including verifying data replacement and insertion. [[1]](diffhunk://#diff-c22703c37ebb6836c332f7cd2ada570577ba4564fe39886db02f7c2d0e7a2048L93-R93) [[2]](diffhunk://#diff-c22703c37ebb6836c332f7cd2ada570577ba4564fe39886db02f7c2d0e7a2048R102) [[3]](diffhunk://#diff-c22703c37ebb6836c332f7cd2ada570577ba4564fe39886db02f7c2d0e7a2048L110-R130) * **DM Driver:** - Improved conflict detection: now checks that at least one primary key exists in the provided data when `OnConflict` is not specified, and provides clearer error messages. - Refactored to use the core method for primary key detection and removed redundant code. ### Minor Improvements and Documentation * Added clarifying comments to `DoInsert` methods for ClickHouse, DM, MSSQL, Oracle, and PostgreSQL drivers, specifying that the input list must have at least one validated record. [[1]](diffhunk://#diff-f2e003895041ed3c52b91bb8c270696adc3528d77c39d2f7137af3396267444cR19) [[2]](diffhunk://#diff-f51b30e3f0b0f1284b905385a89992efd0de2fe9ff8c5a4062344dfab17d428eR23) [[3]](diffhunk://#diff-87815aa559a927e2de09bd05148f9841dfc06a1b5f3ecc5e3d5fcb80323a87f8L23-R61) [[4]](diffhunk://#diff-f61dac3fcfd5df4a3936cd8743499c8c0fc45f4f5d0f5398ed84a0cb1603202cR24) [[5]](diffhunk://#diff-c1dfed79aaa3a432057d2bd74d270e4b4094ebcf72984f1161d4972bea009410R16-R72) * Minor code and comment cleanups, including improved formatting and error handling. [[1]](diffhunk://#diff-f61dac3fcfd5df4a3936cd8743499c8c0fc45f4f5d0f5398ed84a0cb1603202cR37) [[2]](diffhunk://#diff-f61dac3fcfd5df4a3936cd8743499c8c0fc45f4f5d0f5398ed84a0cb1603202cL96-R98) [[3]](diffhunk://#diff-f61dac3fcfd5df4a3936cd8743499c8c0fc45f4f5d0f5398ed84a0cb1603202cL106-L116) [[4]](diffhunk://#diff-a17b44c76aaac53d1f164a2bb9440a5531659f4355e7ccfabdadff8dc8633c09L170-R171) [[5]](diffhunk://#diff-56189fa9ae1df51716b50d34d7fe56bfe67a330e8ac2c6b0de7b958db6817ed5R83-R98) ### Workflow and Documentation Updates * Updated example Docker commands in the CI workflow for consistency and clarity. [[1]](diffhunk://#diff-a1a3cb9bdeb5541d148091d973cf266aa3b317e6415a86630e816cbe27cf8b9cL57-R57) [[2]](diffhunk://#diff-a1a3cb9bdeb5541d148091d973cf266aa3b317e6415a86630e816cbe27cf8b9cL78-R78) [[3]](diffhunk://#diff-a1a3cb9bdeb5541d148091d973cf266aa3b317e6415a86630e816cbe27cf8b9cL92-R92) [[4]](diffhunk://#diff-a1a3cb9bdeb5541d148091d973cf266aa3b317e6415a86630e816cbe27cf8b9cL106-R106) [[5]](diffhunk://#diff-a1a3cb9bdeb5541d148091d973cf266aa3b317e6415a86630e816cbe27cf8b9cL153-R153) [[6]](diffhunk://#diff-a1a3cb9bdeb5541d148091d973cf266aa3b317e6415a86630e816cbe27cf8b9cL164-R164) * Removed outdated note about `Replace` support from the SQLite driver documentation. These changes improve the consistency, reliability, and developer experience when performing upsert operations across different database backends. --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Lance Add <1196661499@qq.com>
253 lines
6.1 KiB
Go
253 lines
6.1 KiB
Go
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
|
//
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
// If a copy of the MIT was not distributed with this file,
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
package dm_test
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/gogf/gf/v2/container/garray"
|
|
"github.com/gogf/gf/v2/database/gdb"
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"github.com/gogf/gf/v2/os/gtime"
|
|
"github.com/gogf/gf/v2/test/gtest"
|
|
)
|
|
|
|
var (
|
|
db gdb.DB
|
|
dblink gdb.DB
|
|
dbErr gdb.DB
|
|
ctx context.Context
|
|
TableSize = 10
|
|
)
|
|
|
|
const (
|
|
TestDBHost = "127.0.0.1"
|
|
TestDBPort = "5236"
|
|
TestDBUser = "SYSDBA"
|
|
TestDBPass = "SYSDBA001"
|
|
TestDBName = "SYSDBA"
|
|
TestDBType = "dm"
|
|
TestCharset = "utf8"
|
|
)
|
|
|
|
type User struct {
|
|
ID int64 `orm:"id"`
|
|
AccountName string `orm:"account_name"`
|
|
PwdReset int64 `orm:"pwd_reset"`
|
|
AttrIndex int64 `orm:"attr_index"`
|
|
Enabled int64 `orm:"enabled"`
|
|
Deleted int64 `orm:"deleted"`
|
|
CreatedBy string `orm:"created_by"`
|
|
CreatedTime time.Time `orm:"created_time"`
|
|
UpdatedBy string `orm:"updated_by"`
|
|
UpdatedTime time.Time `orm:"updated_time"`
|
|
}
|
|
|
|
func init() {
|
|
node := gdb.ConfigNode{
|
|
Host: TestDBHost,
|
|
Port: TestDBPort,
|
|
User: TestDBUser,
|
|
Pass: TestDBPass,
|
|
Name: TestDBName,
|
|
Type: TestDBType,
|
|
Role: "master",
|
|
Charset: TestCharset,
|
|
Weight: 1,
|
|
MaxIdleConnCount: 10,
|
|
MaxOpenConnCount: 10,
|
|
// CreatedAt: "created_time",
|
|
// UpdatedAt: "updated_time",
|
|
}
|
|
|
|
nodeLink := gdb.ConfigNode{
|
|
Type: TestDBType,
|
|
Name: TestDBName,
|
|
Link: fmt.Sprintf(
|
|
"dm:%s:%s@tcp(%s:%s)/%s?charset=%s",
|
|
TestDBUser, TestDBPass, TestDBHost, TestDBPort, TestDBName, TestCharset,
|
|
),
|
|
}
|
|
|
|
nodeErr := gdb.ConfigNode{
|
|
Host: TestDBHost,
|
|
Port: TestDBPort,
|
|
User: TestDBUser,
|
|
Pass: "1234",
|
|
Name: TestDBName,
|
|
Type: TestDBType,
|
|
Role: "master",
|
|
Charset: TestCharset,
|
|
Weight: 1,
|
|
}
|
|
|
|
gdb.AddConfigNode(gdb.DefaultGroupName, node)
|
|
if r, err := gdb.New(node); err != nil {
|
|
gtest.Fatal(err)
|
|
} else {
|
|
db = r
|
|
}
|
|
|
|
gdb.AddConfigNode("dblink", nodeLink)
|
|
if r, err := gdb.New(nodeLink); err != nil {
|
|
gtest.Fatal(err)
|
|
} else {
|
|
dblink = r
|
|
}
|
|
|
|
gdb.AddConfigNode("dbErr", nodeErr)
|
|
if r, err := gdb.New(nodeErr); err != nil {
|
|
gtest.Fatal(err)
|
|
} else {
|
|
dbErr = r
|
|
}
|
|
|
|
ctx = context.Background()
|
|
|
|
// db.SetDebug(true)
|
|
}
|
|
|
|
func dropTable(table string) {
|
|
count, err := db.GetCount(
|
|
ctx,
|
|
"SELECT COUNT(*) FROM all_tables WHERE owner = ? And table_name= ?", TestDBName, strings.ToUpper(table),
|
|
)
|
|
if err != nil {
|
|
gtest.Fatal(err)
|
|
}
|
|
|
|
if count == 0 {
|
|
return
|
|
}
|
|
if _, err := db.Exec(ctx, fmt.Sprintf("DROP TABLE %s", table)); err != nil {
|
|
gtest.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func createTable(table ...string) (name string) {
|
|
if len(table) > 0 {
|
|
name = table[0]
|
|
} else {
|
|
name = fmt.Sprintf("random_%d", gtime.Timestamp())
|
|
}
|
|
|
|
dropTable(name)
|
|
|
|
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
|
CREATE TABLE "%s"
|
|
(
|
|
"ID" BIGINT NOT NULL,
|
|
"ACCOUNT_NAME" VARCHAR(128) DEFAULT '' NOT NULL COMMENT 'Account Name',
|
|
"PWD_RESET" TINYINT DEFAULT 0 NOT NULL,
|
|
"ENABLED" INT DEFAULT 1 NOT NULL,
|
|
"DELETED" INT DEFAULT 0 NOT NULL,
|
|
"ATTR_INDEX" INT DEFAULT 0 ,
|
|
"CREATED_BY" VARCHAR(32) DEFAULT '' NOT NULL,
|
|
"CREATED_TIME" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP() NOT NULL,
|
|
"UPDATED_BY" VARCHAR(32) DEFAULT '' NOT NULL,
|
|
"UPDATED_TIME" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP() NOT NULL,
|
|
NOT CLUSTER PRIMARY KEY("ID")) STORAGE(ON "MAIN", CLUSTERBTR) ;
|
|
`, name)); err != nil {
|
|
gtest.Fatal(err)
|
|
}
|
|
return
|
|
}
|
|
|
|
func createInitTable(table ...string) (name string) {
|
|
name = createTable(table...)
|
|
array := garray.New(true)
|
|
for i := 1; i <= TableSize; i++ {
|
|
array.Append(g.Map{
|
|
"id": i,
|
|
"account_name": fmt.Sprintf(`name_%d`, i),
|
|
"pwd_reset": 0,
|
|
"attr_index": i,
|
|
"created_time": gtime.Now(),
|
|
})
|
|
}
|
|
result, err := db.Schema(TestDBName).Insert(context.Background(), name, array.Slice())
|
|
gtest.AssertNil(err)
|
|
|
|
n, e := result.RowsAffected()
|
|
gtest.Assert(e, nil)
|
|
gtest.Assert(n, TableSize)
|
|
return
|
|
}
|
|
|
|
func createTableFalse(table ...string) (name string, err error) {
|
|
if len(table) > 0 {
|
|
name = table[0]
|
|
} else {
|
|
name = fmt.Sprintf("random_%d", gtime.Timestamp())
|
|
}
|
|
|
|
dropTable(name)
|
|
|
|
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
|
CREATE TABLE "%s"
|
|
(
|
|
"ID" BIGINT NOT NULL,
|
|
"ACCOUNT_NAME" VARCHAR(128) DEFAULT '' NOT NULL,
|
|
"PWD_RESET" TINYINT DEFAULT 0 NOT NULL,
|
|
"ENABLED" INT DEFAULT 1 NOT NULL,
|
|
"DELETED" INT DEFAULT 0 NOT NULL,
|
|
"INDEX" INT DEFAULT 0 ,
|
|
"ATTR_INDEX" INT DEFAULT 0 ,
|
|
"CREATED_BY" VARCHAR(32) DEFAULT '' NOT NULL,
|
|
"CREATED_TIME" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP() NOT NULL,
|
|
"UPDATED_BY" VARCHAR(32) DEFAULT '' NOT NULL,
|
|
"UPDATED_TIME" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP() NOT NULL,
|
|
NOT CLUSTER PRIMARY KEY("ID")) STORAGE(ON "MAIN", CLUSTERBTR) ;
|
|
`, name)); err != nil {
|
|
// gtest.Fatal(err)
|
|
return name, fmt.Errorf("createTableFalse")
|
|
}
|
|
|
|
return name, nil
|
|
}
|
|
|
|
func createInitTables(len int) []string {
|
|
tables := make([]string, 0, len)
|
|
for range len {
|
|
tables = append(tables, createInitTable())
|
|
}
|
|
return tables
|
|
}
|
|
|
|
// createTableWithIdentity creates a table with IDENTITY column for LastInsertId testing
|
|
func createTableWithIdentity(table ...string) (name string) {
|
|
if len(table) > 0 {
|
|
name = table[0]
|
|
} else {
|
|
name = fmt.Sprintf("random_%d", gtime.Timestamp())
|
|
}
|
|
|
|
dropTable(name)
|
|
|
|
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
|
CREATE TABLE "%s"
|
|
(
|
|
"ID" BIGINT IDENTITY(1, 1) NOT NULL,
|
|
"ACCOUNT_NAME" VARCHAR(128) DEFAULT '' NOT NULL COMMENT 'Account Name',
|
|
"PWD_RESET" TINYINT DEFAULT 0 NOT NULL,
|
|
"ENABLED" INT DEFAULT 1 NOT NULL,
|
|
"DELETED" INT DEFAULT 0 NOT NULL,
|
|
"ATTR_INDEX" INT DEFAULT 0 ,
|
|
"CREATED_BY" VARCHAR(32) DEFAULT '' NOT NULL,
|
|
"CREATED_TIME" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP() NOT NULL,
|
|
"UPDATED_BY" VARCHAR(32) DEFAULT '' NOT NULL,
|
|
"UPDATED_TIME" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP() NOT NULL,
|
|
NOT CLUSTER PRIMARY KEY("ID")) STORAGE(ON "MAIN", CLUSTERBTR) ;
|
|
`, name)); err != nil {
|
|
gtest.Fatal(err)
|
|
}
|
|
return
|
|
}
|