Files
gf/contrib/drivers/dm/dm_z_unit_basic_test.go

511 lines
13 KiB
Go
Raw Permalink Normal View History

2022-09-26 22:11:13 +08:00
// 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)
2022-09-26 22:11:13 +08:00
gtest.C(t, func(t *gtest.T) {
result, err := db.Tables(ctx)
gtest.AssertNil(err)
2022-09-26 22:11:13 +08:00
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]) {
2022-09-26 22:11:13 +08:00
find = true
break
}
}
gtest.AssertEQ(find, true)
}
result, err = dblink.Tables(ctx)
gtest.AssertNil(err)
2022-09-26 22:11:13 +08:00
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]) {
2022-09-26 22:11:13 +08:00
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)
}
})
}
2022-09-26 22:11:13 +08:00
func TestTableFields(t *testing.T) {
tables := "A_tables"
createInitTable(tables)
2022-09-26 22:11:13 +08:00
gtest.C(t, func(t *gtest.T) {
refactor: interface{} to any and reflect.Ptr to reflect.Pointer (#4395) This pull request standardizes the use of the Go 1.18+ `any` type alias instead of `interface{}` throughout the codebase. The change improves code readability and aligns with modern Go best practices. The update touches many files, including core data structures, code generation templates, logging utilities, and test data, ensuring consistency across all usages. **Type alias migration to `any`:** * Replaced all instances of `interface{}` with `any` in core data structures such as `garray` and in generated model structs (e.g., `TableUser`, `User1`, `User2`) to modernize type usage. [[1]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L31-R31) [[2]](diffhunk://#diff-6c19859cb32c7516ea95ddc8f8235460818eb2f24d2204308e0d9e1b19e7d90fL15-R19) [[3]](diffhunk://#diff-a15ba2f5e830b4833c47b902515a4f9e5a4f83a3707698f3229b307ec3776b41L15-R18) [[4]](diffhunk://#diff-52e0837e84d49221d1b810d88fdf78221f36cffcd664fb42f8aba49a79b974dcL15-R19) [[5]](diffhunk://#diff-11c3457d1a23a4ca6ecd00d6b856289774936b6a708384cf03aff164044e7546L15-R19) [[6]](diffhunk://#diff-2cff9cf8e6a0cc34087326d8c8149c3bbaf74c76fdbdf5a73daed13cc04249e1L15-R19) * Updated function signatures, method parameters, and return types from `interface{}` to `any` in various parts of the codebase, including code generation, service logic, and logging utilities (e.g., `mlog`). [[1]](diffhunk://#diff-175edfeea54490b8fe4e18ffcbea5835efaf8f0b8acf623359073987cae7eb76L48-R55) [[2]](diffhunk://#diff-2b1953fb78cf3593d8c2c7d911e95b65fd0b847c30ed0b4d167d16fe6d781235L54-R74) [[3]](diffhunk://#diff-e001b7a4b63603b9b14f00de78a4d570bb76c5f57d856a24643f071032e12356L66-R73) [[4]](diffhunk://#diff-5582954e8a9983988dc8854ad82067fb2ac6269b988e07357ad8db1dfec5f1a0L39-R41) [[5]](diffhunk://#diff-c5d51d56f487779a2b6207c7ad26c7a20bbadcc846ce094fe60ab4cabff58c51L107-R107) [[6]](diffhunk://#diff-f96e6a9fdb416eb1804ceaba1fe0ac637bff22c43837f8bb849c2366ce72d4a1L116-R121) [[7]](diffhunk://#diff-f94c83a1b08ae060d9346f4a6031fc4a7b9a0b894e02d9afaa09018b6598eac0L112-R112) [[8]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L36-R36) [[9]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L74-R74) [[10]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L96-R96) **Generated code and templates:** * Adjusted generated files and code generation templates to output `any` instead of `interface{}` for relevant struct fields and function signatures, ensuring that new code generation aligns with the updated convention. [[1]](diffhunk://#diff-6c19859cb32c7516ea95ddc8f8235460818eb2f24d2204308e0d9e1b19e7d90fL15-R19) [[2]](diffhunk://#diff-a15ba2f5e830b4833c47b902515a4f9e5a4f83a3707698f3229b307ec3776b41L15-R18) [[3]](diffhunk://#diff-52e0837e84d49221d1b810d88fdf78221f36cffcd664fb42f8aba49a79b974dcL15-R19) [[4]](diffhunk://#diff-11c3457d1a23a4ca6ecd00d6b856289774936b6a708384cf03aff164044e7546L15-R19) [[5]](diffhunk://#diff-2cff9cf8e6a0cc34087326d8c8149c3bbaf74c76fdbdf5a73daed13cc04249e1L15-R19) [[6]](diffhunk://#diff-175edfeea54490b8fe4e18ffcbea5835efaf8f0b8acf623359073987cae7eb76L48-R55) [[7]](diffhunk://#diff-e001b7a4b63603b9b14f00de78a4d570bb76c5f57d856a24643f071032e12356L66-R73) [[8]](diffhunk://#diff-5582954e8a9983988dc8854ad82067fb2ac6269b988e07357ad8db1dfec5f1a0L39-R41) **Container and utility updates:** * Refactored the `garray` container implementation and related constructors/methods to use `[]any` instead of `[]interface{}`, along with corresponding function signatures. [[1]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L31-R31) [[2]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L52-R52) [[3]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L62-R62) [[4]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L73-R86) [[5]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L96-R97) [[6]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L107-R114) [[7]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L124-R124) [[8]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L135-R143) [[9]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L167-R167) These changes collectively modernize the codebase and prepare it for future Go developments by using the idiomatic `any` type.
2025-08-28 16:53:19 +08:00
var expect = map[string][]any{
feat(contrib/drivers/dm): add `Replace/InsertIgnore` support and field type/length enhancements for dm database (#4541) This pull request introduces significant improvements to the DM database driver, especially around insert operations, and refines documentation and tests to reflect these changes. The main focus is enabling support for "replace" and "insert ignore" operations using DM's `MERGE` statement, improving type reporting for table fields, and updating documentation for clarity and accuracy. ### DM Driver Insert Operations * Added support for `Replace` and `InsertIgnore` operations in the DM driver by internally mapping them to DM's `MERGE` statement. This enables upsert and insert-ignore behavior for DM databases, improving compatibility with other drivers. * Implemented helper methods (`doMergeInsert`, `doInsertIgnore`, and `getPrimaryKeys`) to generate correct `MERGE` SQL statements and automatically detect primary keys when needed. [[1]](diffhunk://#diff-f51b30e3f0b0f1284b905385a89992efd0de2fe9ff8c5a4062344dfab17d428eL31-R94) [[2]](diffhunk://#diff-f51b30e3f0b0f1284b905385a89992efd0de2fe9ff8c5a4062344dfab17d428eL115-R212) * Updated the logic for building update values and SQL generation to ensure correct behavior for both upsert and insert-ignore cases. [[1]](diffhunk://#diff-f51b30e3f0b0f1284b905385a89992efd0de2fe9ff8c5a4062344dfab17d428eL61-R109) [[2]](diffhunk://#diff-f51b30e3f0b0f1284b905385a89992efd0de2fe9ff8c5a4062344dfab17d428eL89-R132) [[3]](diffhunk://#diff-f51b30e3f0b0f1284b905385a89992efd0de2fe9ff8c5a4062344dfab17d428eL100-R144) [[4]](diffhunk://#diff-f51b30e3f0b0f1284b905385a89992efd0de2fe9ff8c5a4062344dfab17d428eL115-R212) ### Table Field Type Reporting * Improved the DM driver's `TableFields` method to report column types with length/precision (e.g., `VARCHAR(128)` instead of just `VARCHAR`), aligning with expectations and other drivers. [[1]](diffhunk://#diff-40a365112421ae1967bd960f8acefcc91ddb8180865b78bc49cd090fbf4883daL26-R26) [[2]](diffhunk://#diff-40a365112421ae1967bd960f8acefcc91ddb8180865b78bc49cd090fbf4883daR88-R105) * Updated related unit tests to expect the new type format for DM table fields. ### Documentation Updates * Removed outdated or redundant documentation in both English and Chinese driver README files, and clarified supported features and limitations for DM and other drivers. [[1]](diffhunk://#diff-d49f5bc3a34b11a6ccb82cc54675b06a7dea5f0a943ae91c4ca0d28bd5003299L1) [[2]](diffhunk://#diff-d49f5bc3a34b11a6ccb82cc54675b06a7dea5f0a943ae91c4ca0d28bd5003299L47-R46) [[3]](diffhunk://#diff-d49f5bc3a34b11a6ccb82cc54675b06a7dea5f0a943ae91c4ca0d28bd5003299L119-L122) [[4]](diffhunk://#diff-05411a14e9c7ca235f7f436bfde732853aa93b364361fe80d65ac768f4e4d613L1-L126) ### Test Suite Enhancements * Refactored and restored unit tests for DM driver insert operations, including tests for `Save`, `Insert`, and the new `InsertIgnore` functionality to ensure correct behavior and compatibility. [[1]](diffhunk://#diff-2b1a59b8b2adaa1ca3074629374ab122929e4d4fbb4cc794b8e1db60ebf8d4c2L143-L245) [[2]](diffhunk://#diff-2b1a59b8b2adaa1ca3074629374ab122929e4d4fbb4cc794b8e1db60ebf8d4c2R512-R632) * Minor adjustments to DM test initialization for improved clarity. ### Core Insert Logic Minor Refactoring * Minor variable renaming for clarity in the core insert logic (`gdb_core.go`), improving code readability. [[1]](diffhunk://#diff-b1bbe5e3995261813e4e0ac6ffee8a37c236eaa2759f2bd82e211711695a70bcL449-R452) [[2]](diffhunk://#diff-b1bbe5e3995261813e4e0ac6ffee8a37c236eaa2759f2bd82e211711695a70bcL466-R474) --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-12-04 20:12:12 +08:00
"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},
2022-09-26 22:11:13 +08:00
}
res, err := db.TableFields(ctx, tables)
gtest.AssertNil(err)
2022-09-26 22:11:13 +08:00
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)
})
}
2022-09-26 22:11:13 +08:00
func Test_DB_Query(t *testing.T) {
tableName := "A_tables"
createInitTable(tableName)
2022-09-26 22:11:13 +08:00
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")
2022-09-26 22:11:13 +08:00
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)
2022-09-26 22:11:13 +08:00
gtest.C(t, func(t *gtest.T) {
timeNow := time.Now()
2022-09-26 22:11:13 +08:00
// normal map
_, err := db.Insert(ctx, table, g.Map{
2022-09-26 22:11:13 +08:00
"ID": 1000,
"ACCOUNT_NAME": "map1",
"CREATED_TIME": timeNow,
"UPDATED_TIME": timeNow,
2022-09-26 22:11:13 +08:00
})
t.AssertNil(err)
result, err := db.Insert(ctx, table, g.Map{
2022-09-26 22:11:13 +08:00
"ID": "2000",
"ACCOUNT_NAME": "map2",
"CREATED_TIME": timeNow,
"UPDATED_TIME": timeNow,
2022-09-26 22:11:13 +08:00
})
t.AssertNil(err)
n, _ := result.RowsAffected()
t.Assert(n, 1)
result, err = db.Insert(ctx, table, g.Map{
2022-09-26 22:11:13 +08:00
"ID": 3000,
"ACCOUNT_NAME": "map3",
"CREATED_TIME": timeNow,
"UPDATED_TIME": timeNow,
2022-09-26 22:11:13 +08:00
})
t.AssertNil(err)
n, _ = result.RowsAffected()
t.Assert(n, 1)
// struct
result, err = db.Insert(ctx, table, User{
2022-09-26 22:11:13 +08:00
ID: 4000,
AccountName: "struct_4",
CreatedTime: timeNow,
UpdatedTime: timeNow,
2022-09-26 22:11:13 +08:00
})
t.AssertNil(err)
n, _ = result.RowsAffected()
t.Assert(n, 1)
ones, err := db.Model(table).Where("ID", 4000).All()
2022-09-26 22:11:13 +08:00
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{
2022-09-26 22:11:13 +08:00
ID: 5000,
AccountName: "struct_5",
CreatedTime: timeNow,
UpdatedTime: timeNow,
2022-09-26 22:11:13 +08:00
})
t.AssertNil(err)
n, _ = result.RowsAffected()
t.Assert(n, 1)
one, err := db.Model(table).Where("ID", 5000).One()
2022-09-26 22:11:13 +08:00
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{
2022-09-26 22:11:13 +08:00
g.Map{
"ID": 6000,
"ACCOUNT_NAME": "t6000",
"CREATED_TIME": timeNow,
"UPDATED_TIME": timeNow,
2022-09-26 22:11:13 +08:00
},
g.Map{
"ID": 6001,
"ACCOUNT_NAME": "t6001",
"CREATED_TIME": timeNow,
"UPDATED_TIME": timeNow,
2022-09-26 22:11:13 +08:00
},
})
t.AssertNil(err)
n, _ = r.RowsAffected()
t.Assert(n, 2)
one, err = db.Model(table).Where("ID", 6000).One()
2022-09-26 22:11:13 +08:00
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)
2022-09-26 22:11:13 +08:00
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(),
2022-09-26 22:11:13 +08:00
},
{
"ID": 401,
"ACCOUNT_NAME": "list_401",
"CREATE_TIME": gtime.Now(),
"UPDATED_TIME": gtime.Now(),
2022-09-26 22:11:13 +08:00
},
}, 1)
t.AssertNil(err)
n, _ := r.RowsAffected()
t.Assert(n, 2)
})
gtest.C(t, func(t *gtest.T) {
refactor: interface{} to any and reflect.Ptr to reflect.Pointer (#4395) This pull request standardizes the use of the Go 1.18+ `any` type alias instead of `interface{}` throughout the codebase. The change improves code readability and aligns with modern Go best practices. The update touches many files, including core data structures, code generation templates, logging utilities, and test data, ensuring consistency across all usages. **Type alias migration to `any`:** * Replaced all instances of `interface{}` with `any` in core data structures such as `garray` and in generated model structs (e.g., `TableUser`, `User1`, `User2`) to modernize type usage. [[1]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L31-R31) [[2]](diffhunk://#diff-6c19859cb32c7516ea95ddc8f8235460818eb2f24d2204308e0d9e1b19e7d90fL15-R19) [[3]](diffhunk://#diff-a15ba2f5e830b4833c47b902515a4f9e5a4f83a3707698f3229b307ec3776b41L15-R18) [[4]](diffhunk://#diff-52e0837e84d49221d1b810d88fdf78221f36cffcd664fb42f8aba49a79b974dcL15-R19) [[5]](diffhunk://#diff-11c3457d1a23a4ca6ecd00d6b856289774936b6a708384cf03aff164044e7546L15-R19) [[6]](diffhunk://#diff-2cff9cf8e6a0cc34087326d8c8149c3bbaf74c76fdbdf5a73daed13cc04249e1L15-R19) * Updated function signatures, method parameters, and return types from `interface{}` to `any` in various parts of the codebase, including code generation, service logic, and logging utilities (e.g., `mlog`). [[1]](diffhunk://#diff-175edfeea54490b8fe4e18ffcbea5835efaf8f0b8acf623359073987cae7eb76L48-R55) [[2]](diffhunk://#diff-2b1953fb78cf3593d8c2c7d911e95b65fd0b847c30ed0b4d167d16fe6d781235L54-R74) [[3]](diffhunk://#diff-e001b7a4b63603b9b14f00de78a4d570bb76c5f57d856a24643f071032e12356L66-R73) [[4]](diffhunk://#diff-5582954e8a9983988dc8854ad82067fb2ac6269b988e07357ad8db1dfec5f1a0L39-R41) [[5]](diffhunk://#diff-c5d51d56f487779a2b6207c7ad26c7a20bbadcc846ce094fe60ab4cabff58c51L107-R107) [[6]](diffhunk://#diff-f96e6a9fdb416eb1804ceaba1fe0ac637bff22c43837f8bb849c2366ce72d4a1L116-R121) [[7]](diffhunk://#diff-f94c83a1b08ae060d9346f4a6031fc4a7b9a0b894e02d9afaa09018b6598eac0L112-R112) [[8]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L36-R36) [[9]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L74-R74) [[10]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L96-R96) **Generated code and templates:** * Adjusted generated files and code generation templates to output `any` instead of `interface{}` for relevant struct fields and function signatures, ensuring that new code generation aligns with the updated convention. [[1]](diffhunk://#diff-6c19859cb32c7516ea95ddc8f8235460818eb2f24d2204308e0d9e1b19e7d90fL15-R19) [[2]](diffhunk://#diff-a15ba2f5e830b4833c47b902515a4f9e5a4f83a3707698f3229b307ec3776b41L15-R18) [[3]](diffhunk://#diff-52e0837e84d49221d1b810d88fdf78221f36cffcd664fb42f8aba49a79b974dcL15-R19) [[4]](diffhunk://#diff-11c3457d1a23a4ca6ecd00d6b856289774936b6a708384cf03aff164044e7546L15-R19) [[5]](diffhunk://#diff-2cff9cf8e6a0cc34087326d8c8149c3bbaf74c76fdbdf5a73daed13cc04249e1L15-R19) [[6]](diffhunk://#diff-175edfeea54490b8fe4e18ffcbea5835efaf8f0b8acf623359073987cae7eb76L48-R55) [[7]](diffhunk://#diff-e001b7a4b63603b9b14f00de78a4d570bb76c5f57d856a24643f071032e12356L66-R73) [[8]](diffhunk://#diff-5582954e8a9983988dc8854ad82067fb2ac6269b988e07357ad8db1dfec5f1a0L39-R41) **Container and utility updates:** * Refactored the `garray` container implementation and related constructors/methods to use `[]any` instead of `[]interface{}`, along with corresponding function signatures. [[1]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L31-R31) [[2]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L52-R52) [[3]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L62-R62) [[4]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L73-R86) [[5]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L96-R97) [[6]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L107-R114) [[7]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L124-R124) [[8]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L135-R143) [[9]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L167-R167) These changes collectively modernize the codebase and prepare it for future Go developments by using the idiomatic `any` type.
2025-08-28 16:53:19 +08:00
// []any
2022-09-26 22:11:13 +08:00
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(),
2022-09-26 22:11:13 +08:00
},
g.Map{
"ID": 501,
"ACCOUNT_NAME": "501_batch_501",
"CREATE_TIME": gtime.Now(),
"UPDATED_TIME": gtime.Now(),
2022-09-26 22:11:13 +08:00
},
}, 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(),
2022-09-26 22:11:13 +08:00
})
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)
2022-09-26 22:11:13 +08:00
gtest.C(t, func(t *gtest.T) {
user := &User{
ID: 700,
AccountName: "BatchInsert_Struct_700",
CreatedTime: time.Now(),
UpdatedTime: time.Now(),
2022-09-26 22:11:13 +08:00
}
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)
2022-09-26 22:11:13 +08:00
gtest.C(t, func(t *gtest.T) {
result, err := db.Update(ctx, table, "pwd_reset=7", "id=7")
2022-09-26 22:11:13 +08:00
t.AssertNil(err)
n, _ := result.RowsAffected()
t.Assert(n, 1)
one, err := db.Model(table).Where("ID", 7).One()
2022-09-26 22:11:13 +08:00
t.AssertNil(err)
t.Assert(one["ID"].Int(), 7)
t.Assert(one["ACCOUNT_NAME"].String(), "name_7")
2022-09-26 22:11:13 +08:00
t.Assert(one["PWD_RESET"].String(), "7")
})
}
func Test_DB_GetAll(t *testing.T) {
table := "A_tables"
createInitTable(table)
2022-09-26 22:11:13 +08:00
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)
2022-09-26 22:11:13 +08:00
gtest.C(t, func(t *gtest.T) {
record, err := db.GetOne(ctx, fmt.Sprintf("SELECT * FROM %s WHERE account_name=?", table), "name_4")
2022-09-26 22:11:13 +08:00
t.AssertNil(err)
t.Assert(record["ACCOUNT_NAME"].String(), "name_4")
2022-09-26 22:11:13 +08:00
})
}
func Test_DB_GetValue(t *testing.T) {
table := "A_tables"
createInitTable(table)
2022-09-26 22:11:13 +08:00
gtest.C(t, func(t *gtest.T) {
value, err := db.GetValue(ctx, fmt.Sprintf("SELECT id FROM %s WHERE account_name=?", table), "name_2")
2022-09-26 22:11:13 +08:00
t.AssertNil(err)
t.Assert(value.Int(), 2)
2022-09-26 22:11:13 +08:00
})
}
func Test_DB_GetCount(t *testing.T) {
table := "A_tables"
createInitTable(table)
2022-09-26 22:11:13 +08:00
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)
2022-09-26 22:11:13 +08:00
})
}
func Test_DB_GetStruct(t *testing.T) {
table := "A_tables"
createInitTable(table)
2022-09-26 22:11:13 +08:00
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)
2022-09-26 22:11:13 +08:00
t.AssertNil(err)
t.Assert(user.AccountName, "name_2")
2022-09-26 22:11:13 +08:00
})
}
func Test_DB_GetStructs(t *testing.T) {
table := "A_tables"
createInitTable(table)
2022-09-26 22:11:13 +08:00
gtest.C(t, func(t *gtest.T) {
var users []User
err := db.GetScan(ctx, &users, fmt.Sprintf("SELECT * FROM %s WHERE id>?", table), 4)
2022-09-26 22:11:13 +08:00
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")
2022-09-26 22:11:13 +08:00
})
}
func Test_DB_GetScan(t *testing.T) {
table := "A_tables"
createInitTable(table)
2022-09-26 22:11:13 +08:00
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)
2022-09-26 22:11:13 +08:00
gtest.C(t, func(t *gtest.T) {
result, err := db.Delete(ctx, table, "id=32")
2022-09-26 22:11:13 +08:00
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()
2022-09-26 22:11:13 +08:00
t.AssertNil(err)
n, _ := result.RowsAffected()
t.Assert(n, 0)
})
}
func Test_Empty_Slice_Argument(t *testing.T) {
table := "A_tables"
createInitTable(table)
2022-09-26 22:11:13 +08:00
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)
})
}