2022-07-07 21:42:20 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). 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 pgsql_test
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2024-05-22 21:26:53 +08:00
|
|
|
"strings"
|
2022-07-07 21:42:20 +08:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
|
|
|
"github.com/gogf/gf/v2/os/gtime"
|
|
|
|
|
"github.com/gogf/gf/v2/test/gtest"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func Test_DB_Query(t *testing.T) {
|
|
|
|
|
table := createTable("name")
|
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
_, err := db.Query(ctx, fmt.Sprintf("select * from %s ", table))
|
|
|
|
|
t.AssertNil(err)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Test_DB_Exec(t *testing.T) {
|
|
|
|
|
table := createTable()
|
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
_, err := db.Exec(ctx, fmt.Sprintf("select * from %s ", table))
|
|
|
|
|
t.AssertNil(err)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Test_DB_Insert(t *testing.T) {
|
|
|
|
|
table := createTable()
|
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
_, err := db.Insert(ctx, table, g.Map{
|
|
|
|
|
"id": 1,
|
|
|
|
|
"passport": "t1",
|
|
|
|
|
"password": "25d55ad283aa400af464c76d713c07ad",
|
|
|
|
|
"nickname": "T1",
|
|
|
|
|
"create_time": gtime.Now().String(),
|
|
|
|
|
})
|
|
|
|
|
t.AssertNil(err)
|
|
|
|
|
answer, err := db.GetAll(ctx, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 1)
|
|
|
|
|
t.AssertNil(err)
|
|
|
|
|
t.Assert(len(answer), 1)
|
|
|
|
|
t.Assert(answer[0]["passport"], "t1")
|
|
|
|
|
t.Assert(answer[0]["password"], "25d55ad283aa400af464c76d713c07ad")
|
|
|
|
|
t.Assert(answer[0]["nickname"], "T1")
|
|
|
|
|
|
|
|
|
|
// normal map
|
|
|
|
|
result, err := db.Insert(ctx, table, g.Map{
|
|
|
|
|
"id": "2",
|
|
|
|
|
"passport": "t2",
|
|
|
|
|
"password": "25d55ad283aa400af464c76d713c07ad",
|
|
|
|
|
"nickname": "name_2",
|
|
|
|
|
"create_time": gtime.Now().String(),
|
|
|
|
|
})
|
|
|
|
|
t.AssertNil(err)
|
|
|
|
|
n, _ := result.RowsAffected()
|
|
|
|
|
t.Assert(n, 1)
|
|
|
|
|
answer, err = db.GetAll(ctx, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 2)
|
|
|
|
|
t.AssertNil(err)
|
|
|
|
|
t.Assert(len(answer), 1)
|
|
|
|
|
t.Assert(answer[0]["passport"], "t2")
|
|
|
|
|
t.Assert(answer[0]["password"], "25d55ad283aa400af464c76d713c07ad")
|
|
|
|
|
t.Assert(answer[0]["nickname"], "name_2")
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Test_DB_Save(t *testing.T) {
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
createTable("t_user")
|
|
|
|
|
defer dropTable("t_user")
|
|
|
|
|
|
|
|
|
|
i := 10
|
|
|
|
|
data := g.Map{
|
|
|
|
|
"id": i,
|
|
|
|
|
"passport": fmt.Sprintf(`t%d`, i),
|
|
|
|
|
"password": fmt.Sprintf(`p%d`, i),
|
|
|
|
|
"nickname": fmt.Sprintf(`T%d`, i),
|
|
|
|
|
"create_time": gtime.Now().String(),
|
|
|
|
|
}
|
|
|
|
|
_, err := db.Save(ctx, "t_user", data, 10)
|
|
|
|
|
gtest.AssertNE(err, nil)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Test_DB_Replace(t *testing.T) {
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
createTable("t_user")
|
|
|
|
|
defer dropTable("t_user")
|
|
|
|
|
|
2025-12-04 20:33:08 +08:00
|
|
|
// Insert initial record
|
2022-07-07 21:42:20 +08:00
|
|
|
i := 10
|
|
|
|
|
data := g.Map{
|
|
|
|
|
"id": i,
|
|
|
|
|
"passport": fmt.Sprintf(`t%d`, i),
|
|
|
|
|
"password": fmt.Sprintf(`p%d`, i),
|
|
|
|
|
"nickname": fmt.Sprintf(`T%d`, i),
|
|
|
|
|
"create_time": gtime.Now().String(),
|
|
|
|
|
}
|
2025-12-04 20:33:08 +08:00
|
|
|
_, err := db.Insert(ctx, "t_user", data)
|
|
|
|
|
gtest.AssertNil(err)
|
|
|
|
|
|
|
|
|
|
// Replace with new data
|
|
|
|
|
data2 := g.Map{
|
|
|
|
|
"id": i,
|
|
|
|
|
"passport": fmt.Sprintf(`t%d_new`, i),
|
|
|
|
|
"password": fmt.Sprintf(`p%d_new`, i),
|
|
|
|
|
"nickname": fmt.Sprintf(`T%d_new`, i),
|
|
|
|
|
"create_time": gtime.Now().String(),
|
|
|
|
|
}
|
|
|
|
|
_, err = db.Replace(ctx, "t_user", data2)
|
|
|
|
|
gtest.AssertNil(err)
|
|
|
|
|
|
|
|
|
|
// Verify the data was replaced
|
|
|
|
|
one, err := db.GetOne(ctx, fmt.Sprintf("SELECT * FROM t_user WHERE id=?"), i)
|
|
|
|
|
gtest.AssertNil(err)
|
|
|
|
|
gtest.Assert(one["passport"].String(), fmt.Sprintf(`t%d_new`, i))
|
|
|
|
|
gtest.Assert(one["password"].String(), fmt.Sprintf(`p%d_new`, i))
|
|
|
|
|
gtest.Assert(one["nickname"].String(), fmt.Sprintf(`T%d_new`, i))
|
2022-07-07 21:42:20 +08:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Test_DB_GetAll(t *testing.T) {
|
|
|
|
|
table := createInitTable()
|
|
|
|
|
defer dropTable(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 := createTable()
|
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
type User struct {
|
|
|
|
|
Id int
|
|
|
|
|
Passport string
|
|
|
|
|
Password string
|
|
|
|
|
Nickname string
|
|
|
|
|
CreateTime string
|
|
|
|
|
}
|
|
|
|
|
data := User{
|
|
|
|
|
Id: 1,
|
|
|
|
|
Passport: "user_1",
|
|
|
|
|
Password: "pass_1",
|
|
|
|
|
Nickname: "name_1",
|
|
|
|
|
CreateTime: "2020-10-10 12:00:01",
|
|
|
|
|
}
|
|
|
|
|
_, err := db.Insert(ctx, table, data)
|
|
|
|
|
t.AssertNil(err)
|
|
|
|
|
|
|
|
|
|
one, err := db.GetOne(ctx, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 1)
|
|
|
|
|
t.AssertNil(err)
|
|
|
|
|
t.Assert(one["passport"], data.Passport)
|
|
|
|
|
t.Assert(one["create_time"], data.CreateTime)
|
|
|
|
|
t.Assert(one["nickname"], data.Nickname)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Test_DB_GetValue(t *testing.T) {
|
|
|
|
|
table := createInitTable()
|
|
|
|
|
defer dropTable(table)
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
value, err := db.GetValue(ctx, fmt.Sprintf("SELECT id FROM %s WHERE passport=?", table), "user_3")
|
|
|
|
|
t.AssertNil(err)
|
|
|
|
|
t.Assert(value.Int(), 3)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Test_DB_GetCount(t *testing.T) {
|
|
|
|
|
table := createInitTable()
|
|
|
|
|
defer dropTable(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, TableSize)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Test_DB_GetArray(t *testing.T) {
|
|
|
|
|
table := createInitTable()
|
|
|
|
|
defer dropTable(table)
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
array, err := db.GetArray(ctx, fmt.Sprintf("SELECT password FROM %s", table))
|
|
|
|
|
t.AssertNil(err)
|
|
|
|
|
arrays := make([]string, 0)
|
|
|
|
|
for i := 1; i <= TableSize; i++ {
|
|
|
|
|
arrays = append(arrays, fmt.Sprintf(`pass_%d`, i))
|
|
|
|
|
}
|
|
|
|
|
t.Assert(array, arrays)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Test_DB_GetScan(t *testing.T) {
|
|
|
|
|
table := createInitTable()
|
|
|
|
|
defer dropTable(table)
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
type User struct {
|
|
|
|
|
Id int
|
|
|
|
|
Passport string
|
|
|
|
|
Password string
|
|
|
|
|
NickName string
|
|
|
|
|
CreateTime gtime.Time
|
|
|
|
|
}
|
|
|
|
|
user := new(User)
|
|
|
|
|
err := db.GetScan(ctx, user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 3)
|
|
|
|
|
t.AssertNil(err)
|
|
|
|
|
t.Assert(user.NickName, "name_3")
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Test_DB_Update(t *testing.T) {
|
|
|
|
|
table := createInitTable()
|
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
result, err := db.Update(ctx, table, "password='987654321'", "id=3")
|
|
|
|
|
t.AssertNil(err)
|
|
|
|
|
n, _ := result.RowsAffected()
|
|
|
|
|
t.Assert(n, 1)
|
|
|
|
|
|
|
|
|
|
one, err := db.Model(table).Where("id", 3).One()
|
|
|
|
|
t.AssertNil(err)
|
|
|
|
|
t.Assert(one["id"].Int(), 3)
|
|
|
|
|
t.Assert(one["passport"].String(), "user_3")
|
|
|
|
|
t.Assert(one["password"].String(), "987654321")
|
|
|
|
|
t.Assert(one["nickname"].String(), "name_3")
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Test_DB_Delete(t *testing.T) {
|
|
|
|
|
table := createInitTable()
|
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
result, err := db.Delete(ctx, table, "id>3")
|
|
|
|
|
t.AssertNil(err)
|
|
|
|
|
n, _ := result.RowsAffected()
|
|
|
|
|
t.Assert(n, 7)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Test_DB_Tables(t *testing.T) {
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
tables := []string{"t_user1", "pop", "haha"}
|
|
|
|
|
for _, v := range tables {
|
|
|
|
|
createTable(v)
|
|
|
|
|
}
|
|
|
|
|
result, err := db.Tables(ctx)
|
2024-12-13 11:09:07 +08:00
|
|
|
gtest.AssertNil(err)
|
2022-07-07 21:42:20 +08:00
|
|
|
for i := 0; i < len(tables); i++ {
|
|
|
|
|
find := false
|
|
|
|
|
for j := 0; j < len(result); j++ {
|
|
|
|
|
if tables[i] == result[j] {
|
|
|
|
|
find = true
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
gtest.AssertEQ(find, true)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Test_DB_TableFields(t *testing.T) {
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
table := createTable()
|
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
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{
|
2025-10-13 18:16:09 +08:00
|
|
|
// []string: Index Type Null Key Default Comment
|
|
|
|
|
// id is bigserial so the default is a pgsql function
|
2022-07-07 21:42:20 +08:00
|
|
|
"id": {0, "int8", false, "pri", fmt.Sprintf("nextval('%s_id_seq'::regclass)", table), ""},
|
|
|
|
|
"passport": {1, "varchar", false, "", nil, ""},
|
|
|
|
|
"password": {2, "varchar", false, "", nil, ""},
|
|
|
|
|
"nickname": {3, "varchar", false, "", nil, ""},
|
|
|
|
|
"create_time": {4, "timestamp", false, "", nil, ""},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res, err := db.TableFields(ctx, table)
|
2024-12-13 11:09:07 +08:00
|
|
|
gtest.AssertNil(err)
|
2022-07-07 21:42:20 +08:00
|
|
|
|
|
|
|
|
for k, v := range expect {
|
|
|
|
|
_, ok := res[k]
|
|
|
|
|
gtest.AssertEQ(ok, true)
|
|
|
|
|
|
|
|
|
|
gtest.AssertEQ(res[k].Index, v[0])
|
|
|
|
|
gtest.AssertEQ(res[k].Name, k)
|
|
|
|
|
gtest.AssertEQ(res[k].Type, v[1])
|
|
|
|
|
gtest.AssertEQ(res[k].Null, v[2])
|
|
|
|
|
gtest.AssertEQ(res[k].Key, v[3])
|
|
|
|
|
gtest.AssertEQ(res[k].Default, v[4])
|
|
|
|
|
gtest.AssertEQ(res[k].Comment, v[5])
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
2024-05-22 21:26:53 +08:00
|
|
|
|
|
|
|
|
func Test_NoFields_Error(t *testing.T) {
|
|
|
|
|
createSql := `CREATE TABLE IF NOT EXISTS %s (
|
|
|
|
|
id bigint PRIMARY KEY,
|
|
|
|
|
int_col INT);`
|
|
|
|
|
|
|
|
|
|
type Data struct {
|
|
|
|
|
Id int64
|
|
|
|
|
IntCol int64
|
|
|
|
|
}
|
|
|
|
|
// pgsql converts table names to lowercase
|
2025-11-19 18:03:52 +08:00
|
|
|
// mark: [c.oid = '%s'::regclass] is not case-sensitive
|
2024-05-22 21:26:53 +08:00
|
|
|
tableName := "Error_table"
|
|
|
|
|
_, err := db.Exec(ctx, fmt.Sprintf(createSql, tableName))
|
|
|
|
|
gtest.AssertNil(err)
|
|
|
|
|
defer dropTable(tableName)
|
|
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
var data = Data{
|
|
|
|
|
Id: 2,
|
|
|
|
|
IntCol: 2,
|
|
|
|
|
}
|
|
|
|
|
_, err = db.Model(tableName).Data(data).Insert()
|
2025-11-19 18:03:52 +08:00
|
|
|
t.AssertNE(err, nil)
|
2024-05-22 21:26:53 +08:00
|
|
|
|
|
|
|
|
// Insert a piece of test data using lowercase
|
|
|
|
|
_, err = db.Model(strings.ToLower(tableName)).Data(data).Insert()
|
|
|
|
|
t.AssertNil(err)
|
|
|
|
|
|
|
|
|
|
_, err = db.Model(tableName).Where("id", 1).Data(g.Map{
|
|
|
|
|
"int_col": 9999,
|
|
|
|
|
}).Update()
|
2025-11-19 18:03:52 +08:00
|
|
|
t.AssertNE(err, nil)
|
2024-05-22 21:26:53 +08:00
|
|
|
|
|
|
|
|
})
|
|
|
|
|
// The inserted field does not exist in the table
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
data := map[string]any{
|
|
|
|
|
"id1": 22,
|
|
|
|
|
"int_col_22": 11111,
|
|
|
|
|
}
|
|
|
|
|
_, err = db.Model(tableName).Data(data).Insert()
|
2025-11-19 18:03:52 +08:00
|
|
|
t.Assert(err, fmt.Errorf(`input data match no fields in table "%s"`, tableName))
|
2024-05-22 21:26:53 +08:00
|
|
|
|
|
|
|
|
lowerTableName := strings.ToLower(tableName)
|
|
|
|
|
_, err = db.Model(lowerTableName).Data(data).Insert()
|
|
|
|
|
t.Assert(err, fmt.Errorf(`input data match no fields in table "%s"`, lowerTableName))
|
|
|
|
|
|
|
|
|
|
_, err = db.Model(lowerTableName).Where("id", 1).Data(g.Map{
|
|
|
|
|
"int_col-2": 9999,
|
|
|
|
|
}).Update()
|
|
|
|
|
t.Assert(err, fmt.Errorf(`input data match no fields in table "%s"`, lowerTableName))
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
}
|
2024-10-16 07:32:33 +08:00
|
|
|
|
2025-10-13 18:16:09 +08:00
|
|
|
func Test_DB_TableFields_DuplicateConstraints(t *testing.T) {
|
|
|
|
|
// Test for the fix of duplicate field results with multiple constraints
|
|
|
|
|
// This test verifies that when a field has multiple constraints (e.g., both primary key and unique),
|
|
|
|
|
// the TableFields method correctly merges the results with proper priority (pri > uni > others)
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
tableName := "test_multi_constraint"
|
|
|
|
|
createSql := fmt.Sprintf(`
|
|
|
|
|
CREATE TABLE %s (
|
|
|
|
|
id bigserial NOT NULL PRIMARY KEY,
|
|
|
|
|
email varchar(100) NOT NULL UNIQUE,
|
|
|
|
|
username varchar(50) NOT NULL,
|
|
|
|
|
status int NOT NULL DEFAULT 1
|
|
|
|
|
)`, tableName)
|
|
|
|
|
|
|
|
|
|
_, err := db.Exec(ctx, createSql)
|
|
|
|
|
t.AssertNil(err)
|
|
|
|
|
defer dropTable(tableName)
|
|
|
|
|
|
|
|
|
|
// Get table fields
|
|
|
|
|
fields, err := db.TableFields(ctx, tableName)
|
|
|
|
|
t.AssertNil(err)
|
|
|
|
|
|
|
|
|
|
// Verify id field has primary key constraint
|
|
|
|
|
t.AssertNE(fields["id"], nil)
|
|
|
|
|
t.Assert(fields["id"].Key, "pri")
|
|
|
|
|
t.Assert(fields["id"].Name, "id")
|
|
|
|
|
t.Assert(fields["id"].Type, "int8")
|
|
|
|
|
|
|
|
|
|
// Verify email field has unique constraint
|
|
|
|
|
t.AssertNE(fields["email"], nil)
|
|
|
|
|
t.Assert(fields["email"].Key, "uni")
|
|
|
|
|
t.Assert(fields["email"].Name, "email")
|
|
|
|
|
t.Assert(fields["email"].Type, "varchar")
|
|
|
|
|
|
|
|
|
|
// Verify username field has no constraint
|
|
|
|
|
t.AssertNE(fields["username"], nil)
|
|
|
|
|
t.Assert(fields["username"].Key, "")
|
|
|
|
|
t.Assert(fields["username"].Name, "username")
|
|
|
|
|
|
|
|
|
|
// Verify status field has no constraint and has default value
|
|
|
|
|
t.AssertNE(fields["status"], nil)
|
|
|
|
|
t.Assert(fields["status"].Key, "")
|
|
|
|
|
t.Assert(fields["status"].Name, "status")
|
|
|
|
|
t.Assert(fields["status"].Default, 1)
|
|
|
|
|
|
|
|
|
|
// Verify field count is correct (no duplicates)
|
|
|
|
|
t.Assert(len(fields), 4)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Test table with composite constraints
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
tableName := "test_composite_constraint"
|
|
|
|
|
createSql := fmt.Sprintf(`
|
|
|
|
|
CREATE TABLE %s (
|
|
|
|
|
user_id bigint NOT NULL,
|
|
|
|
|
project_id bigint NOT NULL,
|
|
|
|
|
role varchar(50) NOT NULL,
|
|
|
|
|
PRIMARY KEY (user_id, project_id)
|
|
|
|
|
)`, tableName)
|
|
|
|
|
|
|
|
|
|
_, err := db.Exec(ctx, createSql)
|
|
|
|
|
t.AssertNil(err)
|
|
|
|
|
defer dropTable(tableName)
|
|
|
|
|
|
|
|
|
|
// Get table fields
|
|
|
|
|
fields, err := db.TableFields(ctx, tableName)
|
|
|
|
|
t.AssertNil(err)
|
|
|
|
|
|
|
|
|
|
// In PostgreSQL, composite primary keys may appear in query results
|
|
|
|
|
// The first field in the composite key should be marked as 'pri'
|
|
|
|
|
t.AssertNE(fields["user_id"], nil)
|
|
|
|
|
t.Assert(fields["user_id"].Name, "user_id")
|
|
|
|
|
|
|
|
|
|
t.AssertNE(fields["project_id"], nil)
|
|
|
|
|
t.Assert(fields["project_id"].Name, "project_id")
|
|
|
|
|
|
|
|
|
|
t.AssertNE(fields["role"], nil)
|
|
|
|
|
t.Assert(fields["role"].Name, "role")
|
|
|
|
|
t.Assert(fields["role"].Key, "")
|
|
|
|
|
|
|
|
|
|
// Verify field count is correct (no duplicates)
|
|
|
|
|
t.Assert(len(fields), 3)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-16 07:32:33 +08:00
|
|
|
func Test_DB_InsertIgnore(t *testing.T) {
|
|
|
|
|
table := createTable()
|
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
|
|
// Insert test record
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
_, err := db.Insert(ctx, table, g.Map{
|
|
|
|
|
"id": 1,
|
|
|
|
|
"passport": "t1",
|
|
|
|
|
"password": "25d55ad283aa400af464c76d713c07ad",
|
|
|
|
|
"nickname": "T1",
|
|
|
|
|
"create_time": gtime.Now().String(),
|
|
|
|
|
})
|
|
|
|
|
t.AssertNil(err)
|
|
|
|
|
|
|
|
|
|
answer, err := db.GetAll(ctx, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 1)
|
|
|
|
|
t.AssertNil(err)
|
|
|
|
|
t.Assert(len(answer), 1)
|
|
|
|
|
t.Assert(answer[0]["passport"], "t1")
|
|
|
|
|
t.Assert(answer[0]["password"], "25d55ad283aa400af464c76d713c07ad")
|
|
|
|
|
t.Assert(answer[0]["nickname"], "T1")
|
|
|
|
|
|
|
|
|
|
// Ignore Duplicate record
|
|
|
|
|
result, err := db.InsertIgnore(ctx, table, g.Map{
|
|
|
|
|
"id": 1,
|
|
|
|
|
"passport": "t1_duplicate",
|
|
|
|
|
"password": "duplicate_password",
|
|
|
|
|
"nickname": "Duplicate",
|
|
|
|
|
"create_time": gtime.Now().String(),
|
|
|
|
|
})
|
|
|
|
|
t.AssertNil(err)
|
|
|
|
|
|
|
|
|
|
n, _ := result.RowsAffected()
|
|
|
|
|
t.Assert(n, 0)
|
|
|
|
|
|
|
|
|
|
answer, err = db.GetAll(ctx, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 1)
|
|
|
|
|
t.AssertNil(err)
|
|
|
|
|
t.Assert(len(answer), 1)
|
|
|
|
|
t.Assert(answer[0]["passport"], "t1")
|
|
|
|
|
t.Assert(answer[0]["password"], "25d55ad283aa400af464c76d713c07ad")
|
|
|
|
|
t.Assert(answer[0]["nickname"], "T1")
|
|
|
|
|
|
|
|
|
|
// Insert Correct Record
|
|
|
|
|
result, err = db.Insert(ctx, table, g.Map{
|
|
|
|
|
"id": 2,
|
|
|
|
|
"passport": "t2",
|
|
|
|
|
"password": "25d55ad283aa400af464c76d713c07ad",
|
|
|
|
|
"nickname": "name_2",
|
|
|
|
|
"create_time": gtime.Now().String(),
|
|
|
|
|
})
|
|
|
|
|
t.AssertNil(err)
|
|
|
|
|
n, _ = result.RowsAffected()
|
|
|
|
|
t.Assert(n, 1)
|
|
|
|
|
|
|
|
|
|
answer, err = db.GetAll(ctx, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 2)
|
|
|
|
|
t.AssertNil(err)
|
|
|
|
|
t.Assert(len(answer), 1)
|
|
|
|
|
t.Assert(answer[0]["passport"], "t2")
|
|
|
|
|
t.Assert(answer[0]["password"], "25d55ad283aa400af464c76d713c07ad")
|
|
|
|
|
t.Assert(answer[0]["nickname"], "name_2")
|
|
|
|
|
|
|
|
|
|
// Insert Multiple Records Using g.Map Array
|
|
|
|
|
data := g.List{
|
|
|
|
|
{
|
|
|
|
|
"id": 3,
|
|
|
|
|
"passport": "t3",
|
|
|
|
|
"password": "25d55ad283aa400af464c76d713c07ad",
|
|
|
|
|
"nickname": "name_3",
|
|
|
|
|
"create_time": gtime.Now().String(),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"id": 4,
|
|
|
|
|
"passport": "t4",
|
|
|
|
|
"password": "25d55ad283aa400af464c76d713c07ad",
|
|
|
|
|
"nickname": "name_4",
|
|
|
|
|
"create_time": gtime.Now().String(),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"id": 1,
|
|
|
|
|
"passport": "t1_conflict",
|
|
|
|
|
"password": "conflict_password",
|
|
|
|
|
"nickname": "conflict_name",
|
|
|
|
|
"create_time": gtime.Now().String(),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"id": 2,
|
|
|
|
|
"passport": "t2_conflict",
|
|
|
|
|
"password": "conflict_password",
|
|
|
|
|
"nickname": "conflict_name",
|
|
|
|
|
"create_time": gtime.Now().String(),
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Insert Multiple Records with Ignore
|
|
|
|
|
result, err = db.InsertIgnore(ctx, table, data)
|
|
|
|
|
t.AssertNil(err)
|
|
|
|
|
|
|
|
|
|
n, _ = result.RowsAffected()
|
|
|
|
|
t.Assert(n, 2)
|
|
|
|
|
|
|
|
|
|
answer, err = db.GetAll(ctx, fmt.Sprintf("SELECT * FROM %s", table))
|
|
|
|
|
t.AssertNil(err)
|
|
|
|
|
t.Assert(len(answer), 4)
|
|
|
|
|
// Should have four records in total (ID 1, 2, 3, 4)
|
|
|
|
|
|
|
|
|
|
t.Assert(answer[0]["passport"], "t1")
|
|
|
|
|
t.Assert(answer[1]["passport"], "t2")
|
|
|
|
|
t.Assert(answer[2]["passport"], "t3")
|
|
|
|
|
t.Assert(answer[3]["passport"], "t4")
|
|
|
|
|
})
|
|
|
|
|
}
|