Files
gf/contrib/drivers/dm/dm_z_unit_basic_test.go
John Guo 852c3dda62 feat(contrib/drivers/dm&pgsql&mssql&oracle): add Replace/LastInsertId features support for dm/pgsql/mssql/oracle (#4547)
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>
2025-12-09 15:46:41 +08:00

511 lines
13 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 (
"fmt"
"strings"
"testing"
"time"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/test/gtest"
)
func Test_DB_Ping(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
err1 := dblink.PingMaster()
err2 := dblink.PingSlave()
t.Assert(err1, nil)
t.Assert(err2, nil)
})
}
func TestTables(t *testing.T) {
tables := createInitTables(2)
gtest.C(t, func(t *gtest.T) {
result, err := db.Tables(ctx)
gtest.AssertNil(err)
for i := 0; i < len(tables); i++ {
find := false
for j := 0; j < len(result); j++ {
if strings.ToUpper(tables[i]) == strings.ToUpper(result[j]) {
find = true
break
}
}
gtest.AssertEQ(find, true)
}
result, err = dblink.Tables(ctx)
gtest.AssertNil(err)
for i := 0; i < len(tables); i++ {
find := false
for j := 0; j < len(result); j++ {
if strings.ToUpper(tables[i]) == strings.ToUpper(result[j]) {
find = true
break
}
}
gtest.AssertEQ(find, true)
}
})
}
// The test scenario index of this test case (exact matching field) is a keyword in the Dameng database and cannot exist as a field name.
// If the data structure previously migrated from mysql has an index (completely matching field), it will also be allowed.
// However, when processing the index (completely matching field), the adapter will automatically add security character
// In principle, such problems will not occur if you directly use Dameng database initialization instead of migrating the data structure from mysql.
// If so, the adapter has also taken care of it.
func TestTablesFalse(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
tables := []string{"A_tables", "A_tables2"}
for _, v := range tables {
_, err := createTableFalse(v)
gtest.Assert(err, fmt.Errorf("createTableFalse"))
// createTable(v)
}
})
}
func TestTableFields(t *testing.T) {
tables := "A_tables"
createInitTable(tables)
gtest.C(t, func(t *gtest.T) {
var expect = map[string][]any{
"ID": {"BIGINT(8)", false},
"ACCOUNT_NAME": {"VARCHAR(128)", false},
"PWD_RESET": {"TINYINT(1)", false},
"ATTR_INDEX": {"INT(4)", true},
"DELETED": {"INT(4)", false},
"CREATED_TIME": {"TIMESTAMP(8)", false},
}
res, err := db.TableFields(ctx, tables)
gtest.AssertNil(err)
for k, v := range expect {
_, ok := res[k]
gtest.AssertEQ(ok, true)
gtest.AssertEQ(res[k].Name, k)
gtest.Assert(res[k].Type, v[0])
gtest.Assert(res[k].Null, v[1])
}
})
gtest.C(t, func(t *gtest.T) {
_, err := db.TableFields(ctx, "t_user t_user2")
gtest.AssertNE(err, nil)
})
}
func TestTableFields_WithWrongPassword(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
// dbErr is configured with wrong password, so it should return an error
_, err := dbErr.TableFields(ctx, "Fields")
gtest.AssertNE(err, nil)
})
}
func Test_DB_Query(t *testing.T) {
tableName := "A_tables"
createInitTable(tableName)
gtest.C(t, func(t *gtest.T) {
// createTable(tableName)
_, err := db.Query(ctx, fmt.Sprintf("SELECT * from %s", tableName))
t.AssertNil(err)
resTwo := make([]User, 0)
err = db.Model(tableName).Scan(&resTwo)
t.AssertNil(err)
resThree := make([]User, 0)
model := db.Model(tableName)
model.Where("id", g.Slice{1, 2, 3, 4})
// model.Where("account_name like ?", "%"+"list"+"%")
model.Where("deleted", 0).Order("pwd_reset desc")
_, err = model.Count()
t.AssertNil(err)
err = model.Page(2, 2).Scan(&resThree)
t.AssertNil(err)
})
}
func Test_DB_Exec(t *testing.T) {
createInitTable("A_tables")
gtest.C(t, func(t *gtest.T) {
_, err := db.Exec(ctx, "SELECT ? from dual", 1)
t.AssertNil(err)
_, err = db.Exec(ctx, "ERROR")
t.AssertNE(err, nil)
})
}
func Test_DB_Insert(t *testing.T) {
table := "A_tables"
createInitTable(table)
gtest.C(t, func(t *gtest.T) {
timeNow := time.Now()
// normal map
_, err := db.Insert(ctx, table, g.Map{
"ID": 1000,
"ACCOUNT_NAME": "map1",
"CREATED_TIME": timeNow,
"UPDATED_TIME": timeNow,
})
t.AssertNil(err)
result, err := db.Insert(ctx, table, g.Map{
"ID": "2000",
"ACCOUNT_NAME": "map2",
"CREATED_TIME": timeNow,
"UPDATED_TIME": timeNow,
})
t.AssertNil(err)
n, _ := result.RowsAffected()
t.Assert(n, 1)
result, err = db.Insert(ctx, table, g.Map{
"ID": 3000,
"ACCOUNT_NAME": "map3",
"CREATED_TIME": timeNow,
"UPDATED_TIME": timeNow,
})
t.AssertNil(err)
n, _ = result.RowsAffected()
t.Assert(n, 1)
// struct
result, err = db.Insert(ctx, table, User{
ID: 4000,
AccountName: "struct_4",
CreatedTime: timeNow,
UpdatedTime: timeNow,
})
t.AssertNil(err)
n, _ = result.RowsAffected()
t.Assert(n, 1)
ones, err := db.Model(table).Where("ID", 4000).All()
t.AssertNil(err)
t.Assert(ones[0]["ID"].Int(), 4000)
t.Assert(ones[0]["ACCOUNT_NAME"].String(), "struct_4")
// TODO Question2
// this is DM bug.
// t.Assert(one["CREATED_TIME"].GTime().String(), timeStr)
// *struct
result, err = db.Insert(ctx, table, &User{
ID: 5000,
AccountName: "struct_5",
CreatedTime: timeNow,
UpdatedTime: timeNow,
})
t.AssertNil(err)
n, _ = result.RowsAffected()
t.Assert(n, 1)
one, err := db.Model(table).Where("ID", 5000).One()
t.AssertNil(err)
t.Assert(one["ID"].Int(), 5000)
t.Assert(one["ACCOUNT_NAME"].String(), "struct_5")
// batch with Insert
r, err := db.Insert(ctx, table, g.Slice{
g.Map{
"ID": 6000,
"ACCOUNT_NAME": "t6000",
"CREATED_TIME": timeNow,
"UPDATED_TIME": timeNow,
},
g.Map{
"ID": 6001,
"ACCOUNT_NAME": "t6001",
"CREATED_TIME": timeNow,
"UPDATED_TIME": timeNow,
},
})
t.AssertNil(err)
n, _ = r.RowsAffected()
t.Assert(n, 2)
one, err = db.Model(table).Where("ID", 6000).One()
t.AssertNil(err)
t.Assert(one["ID"].Int(), 6000)
t.Assert(one["ACCOUNT_NAME"].String(), "t6000")
})
}
func Test_DB_BatchInsert(t *testing.T) {
table := "A_tables"
createInitTable(table)
gtest.C(t, func(t *gtest.T) {
r, err := db.Insert(ctx, table, g.List{
{
"ID": 400,
"ACCOUNT_NAME": "list_400",
"CREATE_TIME": gtime.Now(),
"UPDATED_TIME": gtime.Now(),
},
{
"ID": 401,
"ACCOUNT_NAME": "list_401",
"CREATE_TIME": gtime.Now(),
"UPDATED_TIME": gtime.Now(),
},
}, 1)
t.AssertNil(err)
n, _ := r.RowsAffected()
t.Assert(n, 2)
})
gtest.C(t, func(t *gtest.T) {
// []any
r, err := db.Insert(ctx, table, g.Slice{
g.Map{
"ID": 500,
"ACCOUNT_NAME": "500_batch_500",
"CREATE_TIME": gtime.Now(),
"UPDATED_TIME": gtime.Now(),
},
g.Map{
"ID": 501,
"ACCOUNT_NAME": "501_batch_501",
"CREATE_TIME": gtime.Now(),
"UPDATED_TIME": gtime.Now(),
},
}, 1)
t.AssertNil(err)
n, _ := r.RowsAffected()
t.Assert(n, 2)
})
// batch insert map
gtest.C(t, func(t *gtest.T) {
result, err := db.Insert(ctx, table, g.Map{
"ID": 600,
"ACCOUNT_NAME": "600_batch_600",
"CREATE_TIME": gtime.Now(),
"UPDATED_TIME": gtime.Now(),
})
t.AssertNil(err)
n, _ := result.RowsAffected()
t.Assert(n, 1)
})
}
func Test_DB_BatchInsert_Struct(t *testing.T) {
// batch insert struct
table := "A_tables"
createInitTable(table)
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
user := &User{
ID: 700,
AccountName: "BatchInsert_Struct_700",
CreatedTime: time.Now(),
UpdatedTime: time.Now(),
}
result, err := db.Model(table).Insert(user)
t.AssertNil(err)
n, _ := result.RowsAffected()
t.Assert(n, 1)
})
}
func Test_DB_Update(t *testing.T) {
table := "A_tables"
createInitTable(table)
gtest.C(t, func(t *gtest.T) {
result, err := db.Update(ctx, table, "pwd_reset=7", "id=7")
t.AssertNil(err)
n, _ := result.RowsAffected()
t.Assert(n, 1)
one, err := db.Model(table).Where("ID", 7).One()
t.AssertNil(err)
t.Assert(one["ID"].Int(), 7)
t.Assert(one["ACCOUNT_NAME"].String(), "name_7")
t.Assert(one["PWD_RESET"].String(), "7")
})
}
func Test_DB_GetAll(t *testing.T) {
table := "A_tables"
createInitTable(table)
gtest.C(t, func(t *gtest.T) {
result, err := db.GetAll(ctx, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 1)
t.AssertNil(err)
t.Assert(len(result), 1)
t.Assert(result[0]["ID"].Int(), 1)
})
gtest.C(t, func(t *gtest.T) {
result, err := db.GetAll(ctx, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), g.Slice{1})
t.AssertNil(err)
t.Assert(len(result), 1)
t.Assert(result[0]["ID"].Int(), 1)
})
gtest.C(t, func(t *gtest.T) {
result, err := db.GetAll(ctx, fmt.Sprintf("SELECT * FROM %s WHERE id in(?)", table), g.Slice{1, 2, 3})
t.AssertNil(err)
t.Assert(len(result), 3)
t.Assert(result[0]["ID"].Int(), 1)
t.Assert(result[1]["ID"].Int(), 2)
t.Assert(result[2]["ID"].Int(), 3)
})
gtest.C(t, func(t *gtest.T) {
result, err := db.GetAll(ctx, fmt.Sprintf("SELECT * FROM %s WHERE id in(?,?,?)", table), g.Slice{1, 2, 3})
t.AssertNil(err)
t.Assert(len(result), 3)
t.Assert(result[0]["ID"].Int(), 1)
t.Assert(result[1]["ID"].Int(), 2)
t.Assert(result[2]["ID"].Int(), 3)
})
gtest.C(t, func(t *gtest.T) {
result, err := db.GetAll(ctx, fmt.Sprintf("SELECT * FROM %s WHERE id in(?,?,?)", table), g.Slice{1, 2, 3}...)
t.AssertNil(err)
t.Assert(len(result), 3)
t.Assert(result[0]["ID"].Int(), 1)
t.Assert(result[1]["ID"].Int(), 2)
t.Assert(result[2]["ID"].Int(), 3)
})
gtest.C(t, func(t *gtest.T) {
result, err := db.GetAll(ctx, fmt.Sprintf("SELECT * FROM %s WHERE id>=? AND id <=?", table), g.Slice{1, 3})
t.AssertNil(err)
t.Assert(len(result), 3)
t.Assert(result[0]["ID"].Int(), 1)
t.Assert(result[1]["ID"].Int(), 2)
t.Assert(result[2]["ID"].Int(), 3)
})
}
func Test_DB_GetOne(t *testing.T) {
table := "A_tables"
createInitTable(table)
gtest.C(t, func(t *gtest.T) {
record, err := db.GetOne(ctx, fmt.Sprintf("SELECT * FROM %s WHERE account_name=?", table), "name_4")
t.AssertNil(err)
t.Assert(record["ACCOUNT_NAME"].String(), "name_4")
})
}
func Test_DB_GetValue(t *testing.T) {
table := "A_tables"
createInitTable(table)
gtest.C(t, func(t *gtest.T) {
value, err := db.GetValue(ctx, fmt.Sprintf("SELECT id FROM %s WHERE account_name=?", table), "name_2")
t.AssertNil(err)
t.Assert(value.Int(), 2)
})
}
func Test_DB_GetCount(t *testing.T) {
table := "A_tables"
createInitTable(table)
gtest.C(t, func(t *gtest.T) {
count, err := db.GetCount(ctx, fmt.Sprintf("SELECT * FROM %s", table))
t.AssertNil(err)
t.Assert(count, 10)
})
}
func Test_DB_GetStruct(t *testing.T) {
table := "A_tables"
createInitTable(table)
gtest.C(t, func(t *gtest.T) {
user := new(User)
err := db.GetScan(ctx, user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 3)
t.AssertNil(err)
t.Assert(user.AccountName, "name_3")
})
gtest.C(t, func(t *gtest.T) {
user := new(User)
err := db.GetScan(ctx, user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 2)
t.AssertNil(err)
t.Assert(user.AccountName, "name_2")
})
}
func Test_DB_GetStructs(t *testing.T) {
table := "A_tables"
createInitTable(table)
gtest.C(t, func(t *gtest.T) {
var users []User
err := db.GetScan(ctx, &users, fmt.Sprintf("SELECT * FROM %s WHERE id>?", table), 4)
t.AssertNil(err)
t.Assert(users[0].ID, 5)
t.Assert(users[1].ID, 6)
t.Assert(users[2].ID, 7)
t.Assert(users[0].AccountName, "name_5")
t.Assert(users[1].AccountName, "name_6")
t.Assert(users[2].AccountName, "name_7")
})
}
func Test_DB_GetScan(t *testing.T) {
table := "A_tables"
createInitTable(table)
gtest.C(t, func(t *gtest.T) {
user := new(User)
err := db.GetScan(ctx, user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 3)
t.AssertNil(err)
t.Assert(user.AccountName, "name_3")
})
gtest.C(t, func(t *gtest.T) {
var user *User
err := db.GetScan(ctx, &user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 3)
t.AssertNil(err)
t.Assert(user.AccountName, "name_3")
})
gtest.C(t, func(t *gtest.T) {
var users []User
err := db.GetScan(ctx, &users, fmt.Sprintf("SELECT * FROM %s WHERE id<?", table), 4)
t.AssertNil(err)
t.Assert(users[0].ID, 1)
t.Assert(users[1].ID, 2)
t.Assert(users[2].ID, 3)
t.Assert(users[0].AccountName, "name_1")
t.Assert(users[1].AccountName, "name_2")
t.Assert(users[2].AccountName, "name_3")
})
}
func Test_DB_Delete(t *testing.T) {
table := "A_tables"
createInitTable(table)
gtest.C(t, func(t *gtest.T) {
result, err := db.Delete(ctx, table, "id=32")
t.AssertNil(err)
n, _ := result.RowsAffected()
t.Assert(n, 0)
})
gtest.C(t, func(t *gtest.T) {
result, err := db.Model(table).Where("id", 33).Delete()
t.AssertNil(err)
n, _ := result.RowsAffected()
t.Assert(n, 0)
})
}
func Test_Empty_Slice_Argument(t *testing.T) {
table := "A_tables"
createInitTable(table)
gtest.C(t, func(t *gtest.T) {
result, err := db.GetAll(ctx, fmt.Sprintf(`select * from %s where id in(?)`, table), g.Slice{})
t.AssertNil(err)
t.Assert(len(result), 0)
})
}