mirror of
https://gitee.com/johng/gf
synced 2026-06-11 03:41:44 +08:00
Compare commits
11 Commits
feat/cli-g
...
fix/contai
| Author | SHA1 | Date | |
|---|---|---|---|
| 4f6a216a56 | |||
| 9b5ebbc3d7 | |||
| 096c0b2b05 | |||
| be3606d6b5 | |||
| fb0e66a85c | |||
| d734f6ddd9 | |||
| cb0bf9e569 | |||
| 941bc1b15c | |||
| 3b9f2b893e | |||
| 95a20ea1e4 | |||
| 465324551a |
3
.gitignore
vendored
3
.gitignore
vendored
@ -25,5 +25,4 @@ node_modules
|
||||
output
|
||||
.example/
|
||||
.golangci.bck.yml
|
||||
*.exe
|
||||
.aiprompt.zh.md
|
||||
*.exe
|
||||
@ -1,6 +1,7 @@
|
||||
version: "2"
|
||||
run:
|
||||
concurrency: 4
|
||||
go: "1.25"
|
||||
modules-download-mode: readonly
|
||||
issues-exit-code: 2
|
||||
tests: false
|
||||
|
||||
@ -1,16 +1,5 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# Function to run sed in-place with OS-specific options
|
||||
sed_replace() {
|
||||
if [[ "$OSTYPE" == "darwin"* ]]; then
|
||||
# macOS - requires empty string after -i
|
||||
sed -i '' "$@"
|
||||
else
|
||||
# Linux/Windows Git Bash
|
||||
sed -i "$@"
|
||||
fi
|
||||
}
|
||||
|
||||
workdir=.
|
||||
echo "Prepare to tidy all go.mod files in the ${workdir} directory"
|
||||
|
||||
@ -38,9 +27,9 @@ for file in `find ${workdir} -name go.mod`; do
|
||||
|
||||
cd $goModPath
|
||||
# Remove indirect dependencies
|
||||
sed_replace '/\/\/ indirect/d' go.mod
|
||||
sed -i '/\/\/ indirect/d' go.mod
|
||||
go mod tidy
|
||||
# Remove toolchain line if exists
|
||||
sed_replace '/^toolchain/d' go.mod
|
||||
sed -i '' '/^toolchain/d' go.mod
|
||||
cd - > /dev/null
|
||||
done
|
||||
|
||||
@ -1,16 +1,19 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# Function to run sed in-place with OS-specific options
|
||||
sed_replace() {
|
||||
# Function to detect OS and set sed parameters
|
||||
setup_sed() {
|
||||
if [[ "$OSTYPE" == "darwin"* ]]; then
|
||||
# macOS - requires empty string after -i
|
||||
sed -i '' "$@"
|
||||
# macOS
|
||||
SED_INPLACE="sed -i ''"
|
||||
else
|
||||
# Linux/Windows Git Bash
|
||||
sed -i "$@"
|
||||
SED_INPLACE="sed -i"
|
||||
fi
|
||||
}
|
||||
|
||||
# Initialize sed command
|
||||
setup_sed
|
||||
|
||||
if [ $# -ne 2 ]; then
|
||||
echo "Parameter exception, please execute in the format of $0 [directory] [version number]"
|
||||
echo "PS:$0 ./ v2.4.0"
|
||||
@ -40,11 +43,10 @@ fi
|
||||
|
||||
if [[ true ]]; then
|
||||
# Use sed to replace the version number in version.go
|
||||
sed_replace 's/VERSION = ".*"/VERSION = "'${newVersion}'"/' version.go
|
||||
$SED_INPLACE 's/VERSION = ".*"/VERSION = "'${newVersion}'"/' version.go
|
||||
|
||||
# Use sed to replace the version number in README.MD
|
||||
sed_replace 's/version=[^"]*/version='${newVersion}'/' README.MD
|
||||
sed_replace 's/version=[^"]*/version='${newVersion}'/' README.zh_CN.MD
|
||||
$SED_INPLACE 's/version=[^"]*/version='${newVersion}'/' README.MD
|
||||
fi
|
||||
|
||||
if [ -f "go.work" ]; then
|
||||
@ -68,8 +70,6 @@ for file in `find ${workdir} -name go.mod`; do
|
||||
fi
|
||||
|
||||
cd $goModPath
|
||||
|
||||
# Add replace directive for local development.
|
||||
if [ $goModPath = "./cmd/gf" ]; then
|
||||
mv go.work go.work.version.bak
|
||||
go mod edit -replace github.com/gogf/gf/v2=../../
|
||||
@ -81,20 +81,20 @@ for file in `find ${workdir} -name go.mod`; do
|
||||
go mod edit -replace github.com/gogf/gf/contrib/drivers/sqlite/v2=../../contrib/drivers/sqlite
|
||||
fi
|
||||
# Remove indirect dependencies
|
||||
sed_replace '/\/\/ indirect/d' go.mod
|
||||
sed -i '/\/\/ indirect/d' go.mod
|
||||
go mod tidy
|
||||
# Remove toolchain line if exists
|
||||
sed_replace '/^toolchain/d' go.mod
|
||||
$SED_INPLACE '/^toolchain/d' go.mod
|
||||
|
||||
# Upgrading only GoFrame related libraries, sometimes even if a version number is specified,
|
||||
# Upgrading only GoFrame related libraries, sometimes even if a version number is specified,
|
||||
# it may not be possible to successfully upgrade. Please confirm before submitting the code
|
||||
go list -f "{{if and (not .Indirect) (not .Main)}}{{.Path}}@${newVersion}{{end}}" -m all | grep "^github.com/gogf/gf"
|
||||
go list -f "{{if and (not .Indirect) (not .Main)}}{{.Path}}@${newVersion}{{end}}" -m all | grep "^github.com/gogf/gf" | xargs -L1 go get -v
|
||||
go list -f "{{if and (not .Indirect) (not .Main)}}{{.Path}}@${newVersion}{{end}}" -m all | grep "^github.com/gogf/gf" | xargs -L1 go get -v
|
||||
# Remove indirect dependencies
|
||||
sed_replace '/\/\/ indirect/d' go.mod
|
||||
sed -i '/\/\/ indirect/d' go.mod
|
||||
go mod tidy
|
||||
# Remove toolchain line if exists
|
||||
sed_replace '/^toolchain/d' go.mod
|
||||
$SED_INPLACE '/^toolchain/d' go.mod
|
||||
if [ $goModPath = "./cmd/gf" ]; then
|
||||
go mod edit -dropreplace github.com/gogf/gf/v2
|
||||
go mod edit -dropreplace github.com/gogf/gf/contrib/drivers/clickhouse/v2
|
||||
|
||||
47
Makefile
47
Makefile
@ -1,27 +1,5 @@
|
||||
SHELL := /bin/bash
|
||||
|
||||
# commit changes with AI-generated commit message
|
||||
.PHONY: up
|
||||
up:
|
||||
@if git diff --quiet HEAD && git diff --cached --quiet && [ -z "$$(git ls-files --others --exclude-standard)" ]; then \
|
||||
echo "No changes to commit"; \
|
||||
exit 0; \
|
||||
fi
|
||||
@git add -A
|
||||
@echo "Analyzing changes and generating commit message via AI..."
|
||||
@set -e; \
|
||||
MSG=$$(git diff --cached --stat && echo "---" && git diff --cached | head -2000 | \
|
||||
claude -p "Analyze the git diff above and generate a concise commit message (single line, max 72 chars, lowercase, no quotes). Output only the commit message itself, nothing else." \
|
||||
--model haiku) || { echo "Error: Claude command failed"; exit 1; }; \
|
||||
COMMIT_MSG=$$(echo "$$MSG" | tail -1); \
|
||||
if [ -z "$$COMMIT_MSG" ]; then \
|
||||
echo "Error: Failed to generate commit message"; \
|
||||
exit 1; \
|
||||
fi; \
|
||||
echo "Commit: $$COMMIT_MSG"; \
|
||||
git commit -m "$$COMMIT_MSG" && \
|
||||
git push origin $$(git branch --show-current)
|
||||
|
||||
# execute "go mod tidy" on all folders that have go.mod file
|
||||
.PHONY: tidy
|
||||
tidy:
|
||||
@ -74,6 +52,31 @@ tag:
|
||||
git push origin $$newVersion; \
|
||||
echo "Tag $$newVersion created and pushed successfully!"
|
||||
|
||||
# update submodules
|
||||
.PHONY: subup
|
||||
subup:
|
||||
@set -e; \
|
||||
echo "Updating submodules..."; \
|
||||
git submodule init;\
|
||||
git submodule update;
|
||||
|
||||
# update and commit submodules
|
||||
.PHONY: subsync
|
||||
subsync: subup
|
||||
@set -e; \
|
||||
echo "";\
|
||||
cd examples; \
|
||||
echo "Checking for changes..."; \
|
||||
if git diff-index --quiet HEAD --; then \
|
||||
echo "No changes to commit"; \
|
||||
else \
|
||||
echo "Found changes, committing..."; \
|
||||
git add -A; \
|
||||
git commit -m "examples update"; \
|
||||
git push origin; \
|
||||
fi; \
|
||||
cd ..;
|
||||
|
||||
# manage docker services for local development
|
||||
# usage: make docker or make docker cmd=start svc=mysql
|
||||
.PHONY: docker
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
English | [简体中文](README.zh_CN.MD)
|
||||
|
||||
<div align=center>
|
||||
<img src="https://goframe.org/img/logo_full.png" width="300" alt="goframe logo"/>
|
||||
<img src="https://goframe.org/img/logo_full.png" width="300" alt="goframe gf logo"/>
|
||||
|
||||
[](https://pkg.go.dev/github.com/gogf/gf/v2)
|
||||
[](https://github.com/gogf/gf/actions/workflows/ci-main.yml)
|
||||
@ -19,7 +19,6 @@ English | [简体中文](README.zh_CN.MD)
|
||||
[](https://github.com/gogf/gf/issues?q=is%3Aissue+is%3Aclosed)
|
||||

|
||||

|
||||
[](https://deepwiki.com/gogf/gf)
|
||||
|
||||
</div>
|
||||
|
||||
@ -36,7 +35,7 @@ go get -u github.com/gogf/gf/v2
|
||||
- Official Site: [https://goframe.org](https://goframe.org)
|
||||
- Official Site(en): [https://goframe.org/en](https://goframe.org/en)
|
||||
- 国内镜像: [https://goframe.org.cn](https://goframe.org.cn)
|
||||
- Mirror Site: [https://pages.goframe.org](https://pages.goframe.org)
|
||||
- Mirror Site: [Github Pages](https://pages.goframe.org)
|
||||
- Mirror Site: [Offline Docs](https://github.com/gogf/goframe.org-pdf?tab=readme-ov-file#%E6%9C%80%E6%96%B0%E7%89%88%E6%9C%AC)
|
||||
- GoDoc API: [https://pkg.go.dev/github.com/gogf/gf/v2](https://pkg.go.dev/github.com/gogf/gf/v2)
|
||||
- Doc Source: [https://github.com/gogf/gf-site](https://github.com/gogf/gf-site)
|
||||
@ -46,7 +45,7 @@ go get -u github.com/gogf/gf/v2
|
||||
💖 [Thanks to all the contributors who made GoFrame possible](https://github.com/gogf/gf/graphs/contributors) 💖
|
||||
|
||||
<a href="https://github.com/gogf/gf/graphs/contributors">
|
||||
<img src="https://goframe.org/img/contributors.svg?version=v2.10.0" alt="goframe contributors"/>
|
||||
<img src="https://goframe.org/img/contributors.svg?version=v2.9.8" alt="goframe contributors"/>
|
||||
</a>
|
||||
|
||||
## License
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
[English](README.MD) | 简体中文
|
||||
|
||||
<div align=center>
|
||||
<img src="https://goframe.org/img/logo_full.png" width="300" alt="goframe logo"/>
|
||||
<img src="https://goframe.org/img/logo_full.png" width="300" alt="goframe gf logo"/>
|
||||
|
||||
[](https://pkg.go.dev/github.com/gogf/gf/v2)
|
||||
[](https://github.com/gogf/gf/actions/workflows/ci-main.yml)
|
||||
@ -19,11 +19,10 @@
|
||||
[](https://github.com/gogf/gf/issues?q=is%3Aissue+is%3Aclosed)
|
||||

|
||||

|
||||
[](https://deepwiki.com/gogf/gf)
|
||||
|
||||
</div>
|
||||
|
||||
一款强大的框架,为了更快、更轻松、更高效的项目开发。
|
||||
一个强大的框架,为了更快、更轻松、更高效的项目开发。
|
||||
|
||||
## 安装
|
||||
|
||||
@ -36,7 +35,7 @@ go get -u github.com/gogf/gf/v2
|
||||
- 官方网站: [https://goframe.org](https://goframe.org)
|
||||
- 官方网站(en): [https://goframe.org/en](https://goframe.org/en)
|
||||
- 国内镜像: [https://goframe.org.cn](https://goframe.org.cn)
|
||||
- 镜像网站: [https://pages.goframe.org](https://pages.goframe.org)
|
||||
- 镜像网站: [Github Pages](https://pages.goframe.org)
|
||||
- 镜像网站: [离线文档](https://github.com/gogf/goframe.org-pdf?tab=readme-ov-file#%E6%9C%80%E6%96%B0%E7%89%88%E6%9C%AC)
|
||||
- Go包文档: [https://pkg.go.dev/github.com/gogf/gf/v2](https://pkg.go.dev/github.com/gogf/gf/v2)
|
||||
- 文档源码: [https://github.com/gogf/gf-site](https://github.com/gogf/gf-site)
|
||||
@ -46,9 +45,9 @@ go get -u github.com/gogf/gf/v2
|
||||
💖 [感谢所有使 GoFrame 成为可能的贡献者](https://github.com/gogf/gf/graphs/contributors) 💖
|
||||
|
||||
<a href="https://github.com/gogf/gf/graphs/contributors">
|
||||
<img src="https://goframe.org/img/contributors.svg?version=v2.10.0" alt="goframe contributors"/>
|
||||
<img src="https://goframe.org/img/contributors.svg?version=v2.9.5" alt="goframe contributors"/>
|
||||
</a>
|
||||
|
||||
## 许可证
|
||||
|
||||
`GoFrame` 采用 [MIT License](LICENSE) 许可,100%开源和免费。
|
||||
`GoFrame` 采用 [MIT License](LICENSE) 许可,100% 免费和开源,永久保持。
|
||||
|
||||
@ -3,13 +3,13 @@ module github.com/gogf/gf/cmd/gf/v2
|
||||
go 1.23.0
|
||||
|
||||
require (
|
||||
github.com/gogf/gf/contrib/drivers/clickhouse/v2 v2.10.0
|
||||
github.com/gogf/gf/contrib/drivers/mssql/v2 v2.10.0
|
||||
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.10.0
|
||||
github.com/gogf/gf/contrib/drivers/oracle/v2 v2.10.0
|
||||
github.com/gogf/gf/contrib/drivers/pgsql/v2 v2.10.0
|
||||
github.com/gogf/gf/contrib/drivers/sqlite/v2 v2.10.0
|
||||
github.com/gogf/gf/v2 v2.10.0
|
||||
github.com/gogf/gf/contrib/drivers/clickhouse/v2 v2.9.8
|
||||
github.com/gogf/gf/contrib/drivers/mssql/v2 v2.9.8
|
||||
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.9.8
|
||||
github.com/gogf/gf/contrib/drivers/oracle/v2 v2.9.8
|
||||
github.com/gogf/gf/contrib/drivers/pgsql/v2 v2.9.8
|
||||
github.com/gogf/gf/contrib/drivers/sqlite/v2 v2.9.8
|
||||
github.com/gogf/gf/v2 v2.9.8
|
||||
github.com/gogf/selfupdate v0.0.0-20231215043001-5c48c528462f
|
||||
github.com/olekukonko/tablewriter v1.1.0
|
||||
github.com/schollz/progressbar/v3 v3.15.0
|
||||
|
||||
@ -46,20 +46,20 @@ github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiU
|
||||
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
|
||||
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
|
||||
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
|
||||
github.com/gogf/gf/contrib/drivers/clickhouse/v2 v2.10.0 h1:9PTchr92xIJej4tq5c+HOHSU7LGOHr3YfD7tuf23LW4=
|
||||
github.com/gogf/gf/contrib/drivers/clickhouse/v2 v2.10.0/go.mod h1:eKtLMs9uccxFvmoKOUCRQ/Se3nxhzEZwF0Ir13qbk5g=
|
||||
github.com/gogf/gf/contrib/drivers/mssql/v2 v2.10.0 h1:mBs6XpNM34IdZPZv4Kv3LA8yhP2UisbONMLfnQVFvKM=
|
||||
github.com/gogf/gf/contrib/drivers/mssql/v2 v2.10.0/go.mod h1:mChbF9FrmiYMSE2rG3zdxI/oSTwaHsR5KbINAgt3KcY=
|
||||
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.10.0 h1:UvqxwinkelKxwdwnKUfdy51/ls4RL7MCeJqAZOVAy0I=
|
||||
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.10.0/go.mod h1:6v7oGBF9wv59WERJIOJxXmLhkUcxwON3tPYW3AZ7wbY=
|
||||
github.com/gogf/gf/contrib/drivers/oracle/v2 v2.10.0 h1:MvhoMaz8YYj4WJuYzKGDdzJYiieiYiqp0vjoOshfOF4=
|
||||
github.com/gogf/gf/contrib/drivers/oracle/v2 v2.10.0/go.mod h1:vb2fx33RGhjhOaocOTEFvlEuBSGHss5S0lZ4sS3XK6E=
|
||||
github.com/gogf/gf/contrib/drivers/pgsql/v2 v2.10.0 h1:39+jbTenm7KBj4hO2C8ANAxVHpX/7OuRDs1VcGC9ylA=
|
||||
github.com/gogf/gf/contrib/drivers/pgsql/v2 v2.10.0/go.mod h1:B0s0fVzn0W220E8UTpSGzrrGKsop5KcB90twBeLCiz0=
|
||||
github.com/gogf/gf/contrib/drivers/sqlite/v2 v2.10.0 h1:OyAH7Ls2c9Un7CJiAq7G6eY1jWIICRkN8C5SyM94rnY=
|
||||
github.com/gogf/gf/contrib/drivers/sqlite/v2 v2.10.0/go.mod h1:fwhAMG0qZpeHbbP2JE78rJRfV7eBbu9jXkxTMM1lwyo=
|
||||
github.com/gogf/gf/v2 v2.10.0 h1:rzDROlyqGMe/eM6dCalSR8dZOuMIdLhmxKSH1DGhbFs=
|
||||
github.com/gogf/gf/v2 v2.10.0/go.mod h1:Svl1N+E8G/QshU2DUbh/3J/AJauqCgUnxHurXWR4Qx0=
|
||||
github.com/gogf/gf/contrib/drivers/clickhouse/v2 v2.9.8 h1:L72OB2HPuZSHtJ2ipBzI+62rGGDRdwYjequ1v+zctpg=
|
||||
github.com/gogf/gf/contrib/drivers/clickhouse/v2 v2.9.8/go.mod h1:D0UySg70Bd264F5AScYmz1Hl8vjzlUJ7YvqBJc5OFbo=
|
||||
github.com/gogf/gf/contrib/drivers/mssql/v2 v2.9.8 h1:DT5zHfo9/VkbJ+TF7kUasvv4dbU5uctoj+JGbrzgdYE=
|
||||
github.com/gogf/gf/contrib/drivers/mssql/v2 v2.9.8/go.mod h1:cDd91Zd8LxFF+xxOflRRqw0WTTCpAJ0nf0KKRA+nvTE=
|
||||
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.9.8 h1:XZ4Ya/50xpjf81+4genr33iJXR2dxJmqYKxGyXlLRqA=
|
||||
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.9.8/go.mod h1:wtm2NJb/L3CbDOmyUc7TsOpWHTCMakg1QRG7B/oKrRs=
|
||||
github.com/gogf/gf/contrib/drivers/oracle/v2 v2.9.8 h1:ZrqABJsUnhNDz8VAem1XXONBTywl6r+GHQH05i+4W1g=
|
||||
github.com/gogf/gf/contrib/drivers/oracle/v2 v2.9.8/go.mod h1:YTFyeVk2Rgu/JMUhFxkjYzWaBc+yZ6wAvY54XVZoNko=
|
||||
github.com/gogf/gf/contrib/drivers/pgsql/v2 v2.9.8 h1:Dc227FD1uf9nNBPFEjMEgIoAJbAgeYeNrOrjviDgPzY=
|
||||
github.com/gogf/gf/contrib/drivers/pgsql/v2 v2.9.8/go.mod h1:o3EpB4Ti3+x/axzRMJg2k7TrLiWZiSTxP0v64LBkk5k=
|
||||
github.com/gogf/gf/contrib/drivers/sqlite/v2 v2.9.8 h1:LHEhzsBfIo8xHvOUuLDQW1q7Qix1vnBabH/iivCRghs=
|
||||
github.com/gogf/gf/contrib/drivers/sqlite/v2 v2.9.8/go.mod h1:SX6dRONaJGafzCoMIrn8CkRM4fIvtmJRt/aYclUHy3Q=
|
||||
github.com/gogf/gf/v2 v2.9.8 h1:El0HwksTzeRk0DQV4Lh7S9DbsIwKInhHSHGcH7qJumM=
|
||||
github.com/gogf/gf/v2 v2.9.8/go.mod h1:Svl1N+E8G/QshU2DUbh/3J/AJauqCgUnxHurXWR4Qx0=
|
||||
github.com/gogf/selfupdate v0.0.0-20231215043001-5c48c528462f h1:7xfXR/BhG3JDqO1s45n65Oyx9t4E/UqDOXep6jXdLCM=
|
||||
github.com/gogf/selfupdate v0.0.0-20231215043001-5c48c528462f/go.mod h1:HnYoio6S7VaFJdryKcD/r9HgX+4QzYfr00XiXUo/xz0=
|
||||
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
|
||||
|
||||
@ -238,48 +238,3 @@ func Test_Gen_Service_PackagesFilter(t *testing.T) {
|
||||
t.Assert(files[0], dstFolder+filepath.FromSlash("/user.go"))
|
||||
})
|
||||
}
|
||||
|
||||
// https://github.com/gogf/gf/issues/4242
|
||||
// Test that versioned imports and aliased imports are correctly preserved.
|
||||
// The issue is that imports like "github.com/minio/minio-go/v7" were being
|
||||
// incorrectly handled because the package name (minio) differs from
|
||||
// the directory name (minio-go).
|
||||
func Test_Issue4242(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var (
|
||||
path = gfile.Temp(guid.S())
|
||||
dstFolder = path + filepath.FromSlash("/service")
|
||||
srvFolder = gtest.DataPath("issue", "4242", "logic")
|
||||
in = genservice.CGenServiceInput{
|
||||
SrcFolder: srvFolder,
|
||||
DstFolder: dstFolder,
|
||||
DstFileNameCase: "Snake",
|
||||
WatchFile: "",
|
||||
StPattern: "",
|
||||
Packages: nil,
|
||||
ImportPrefix: "",
|
||||
Clear: false,
|
||||
}
|
||||
)
|
||||
err := gutil.FillStructWithDefault(&in)
|
||||
t.AssertNil(err)
|
||||
|
||||
err = gfile.Mkdir(path)
|
||||
t.AssertNil(err)
|
||||
defer gfile.Remove(path)
|
||||
|
||||
_, err = genservice.CGenService{}.Service(ctx, in)
|
||||
t.AssertNil(err)
|
||||
|
||||
// Test versioned imports
|
||||
t.Assert(
|
||||
gfile.GetContents(dstFolder+filepath.FromSlash("/issue_4242.go")),
|
||||
gfile.GetContents(gtest.DataPath("issue", "4242", "service", "issue_4242.go")),
|
||||
)
|
||||
// Test aliased imports
|
||||
t.Assert(
|
||||
gfile.GetContents(dstFolder+filepath.FromSlash("/issue_4242_alias.go")),
|
||||
gfile.GetContents(gtest.DataPath("issue", "4242", "service", "issue_4242_alias.go")),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
@ -33,88 +33,65 @@ import (
|
||||
)
|
||||
|
||||
type (
|
||||
// CGenDao is the command handler struct for "gen dao" command.
|
||||
CGenDao struct{}
|
||||
|
||||
// CGenDaoInput defines all input parameters for the "gen dao" command.
|
||||
// It supports both command-line arguments and configuration file options.
|
||||
CGenDao struct{}
|
||||
CGenDaoInput struct {
|
||||
g.Meta `name:"dao" config:"{CGenDaoConfig}" usage:"{CGenDaoUsage}" brief:"{CGenDaoBrief}" eg:"{CGenDaoEg}" ad:"{CGenDaoAd}"`
|
||||
Path string `name:"path" short:"p" brief:"{CGenDaoBriefPath}" d:"internal"` // Base directory path for generated files.
|
||||
Link string `name:"link" short:"l" brief:"{CGenDaoBriefLink}"` // Database connection string (e.g., "mysql:root:pass@tcp(127.0.0.1:3306)/db").
|
||||
Tables string `name:"tables" short:"t" brief:"{CGenDaoBriefTables}"` // Comma-separated table names or wildcard patterns to include.
|
||||
TablesEx string `name:"tablesEx" short:"x" brief:"{CGenDaoBriefTablesEx}"` // Comma-separated table names or wildcard patterns to exclude.
|
||||
ShardingPattern []string `name:"shardingPattern" short:"sp" brief:"{CGenDaoBriefShardingPattern}"` // Patterns for sharding tables (e.g., "users_?" merges users_001, users_002 into one dao).
|
||||
Group string `name:"group" short:"g" brief:"{CGenDaoBriefGroup}" d:"default"` // Database configuration group name for ORM instance.
|
||||
Prefix string `name:"prefix" short:"f" brief:"{CGenDaoBriefPrefix}"` // Prefix to add to all generated table names.
|
||||
RemovePrefix string `name:"removePrefix" short:"r" brief:"{CGenDaoBriefRemovePrefix}"` // Comma-separated prefixes to remove from table names.
|
||||
RemoveFieldPrefix string `name:"removeFieldPrefix" short:"rf" brief:"{CGenDaoBriefRemoveFieldPrefix}"` // Comma-separated prefixes to remove from field names.
|
||||
JsonCase string `name:"jsonCase" short:"j" brief:"{CGenDaoBriefJsonCase}" d:"CamelLower"` // Naming convention for JSON tags (e.g., CamelLower, Snake).
|
||||
ImportPrefix string `name:"importPrefix" short:"i" brief:"{CGenDaoBriefImportPrefix}"` // Custom Go import path prefix for generated files.
|
||||
DaoPath string `name:"daoPath" short:"d" brief:"{CGenDaoBriefDaoPath}" d:"dao"` // Sub-directory under Path for dao files.
|
||||
TablePath string `name:"tablePath" short:"tp" brief:"{CGenDaoBriefTablePath}" d:"table"` // Sub-directory under Path for table field definition files.
|
||||
DoPath string `name:"doPath" short:"o" brief:"{CGenDaoBriefDoPath}" d:"model/do"` // Sub-directory under Path for DO (Data Object) files.
|
||||
EntityPath string `name:"entityPath" short:"e" brief:"{CGenDaoBriefEntityPath}" d:"model/entity"` // Sub-directory under Path for entity struct files.
|
||||
TplDaoTablePath string `name:"tplDaoTablePath" short:"t0" brief:"{CGenDaoBriefTplDaoTablePath}"` // Custom template file for dao table generation.
|
||||
TplDaoIndexPath string `name:"tplDaoIndexPath" short:"t1" brief:"{CGenDaoBriefTplDaoIndexPath}"` // Custom template file for dao index generation.
|
||||
TplDaoInternalPath string `name:"tplDaoInternalPath" short:"t2" brief:"{CGenDaoBriefTplDaoInternalPath}"` // Custom template file for dao internal generation.
|
||||
TplDaoDoPath string `name:"tplDaoDoPath" short:"t3" brief:"{CGenDaoBriefTplDaoDoPathPath}"` // Custom template file for DO generation.
|
||||
TplDaoEntityPath string `name:"tplDaoEntityPath" short:"t4" brief:"{CGenDaoBriefTplDaoEntityPath}"` // Custom template file for entity generation.
|
||||
StdTime bool `name:"stdTime" short:"s" brief:"{CGenDaoBriefStdTime}" orphan:"true"` // Use stdlib time.Time instead of gtime.Time for time fields.
|
||||
WithTime bool `name:"withTime" short:"w" brief:"{CGenDaoBriefWithTime}" orphan:"true"` // Add creation timestamp to generated file headers.
|
||||
GJsonSupport bool `name:"gJsonSupport" short:"n" brief:"{CGenDaoBriefGJsonSupport}" orphan:"true"` // Use *gjson.Json instead of string for JSON fields.
|
||||
OverwriteDao bool `name:"overwriteDao" short:"v" brief:"{CGenDaoBriefOverwriteDao}" orphan:"true"` // Overwrite existing dao files (both index and internal).
|
||||
DescriptionTag bool `name:"descriptionTag" short:"c" brief:"{CGenDaoBriefDescriptionTag}" orphan:"true"` // Add description struct tag with field comment.
|
||||
NoJsonTag bool `name:"noJsonTag" short:"k" brief:"{CGenDaoBriefNoJsonTag}" orphan:"true"` // Omit json struct tags from generated structs.
|
||||
NoModelComment bool `name:"noModelComment" short:"m" brief:"{CGenDaoBriefNoModelComment}" orphan:"true"` // Omit inline comments from generated struct fields.
|
||||
Clear bool `name:"clear" short:"a" brief:"{CGenDaoBriefClear}" orphan:"true"` // Delete generated files that no longer correspond to database tables.
|
||||
GenTable bool `name:"genTable" short:"gt" brief:"{CGenDaoBriefGenTable}" orphan:"true"` // Enable generation of table field definition files.
|
||||
SqlDir string `name:"sqlDir" short:"sd" brief:"{CGenDaoBriefSqlDir}"` // Directory of SQL DDL files for offline generation (no DB connection needed).
|
||||
SqlType string `name:"sqlType" short:"st" brief:"{CGenDaoBriefSqlType}" d:"mysql"` // SQL dialect when using SqlDir (mysql, pgsql, mssql, oracle, sqlite).
|
||||
Path string `name:"path" short:"p" brief:"{CGenDaoBriefPath}" d:"internal"`
|
||||
Link string `name:"link" short:"l" brief:"{CGenDaoBriefLink}"`
|
||||
Tables string `name:"tables" short:"t" brief:"{CGenDaoBriefTables}"`
|
||||
TablesEx string `name:"tablesEx" short:"x" brief:"{CGenDaoBriefTablesEx}"`
|
||||
ShardingPattern []string `name:"shardingPattern" short:"sp" brief:"{CGenDaoBriefShardingPattern}"`
|
||||
Group string `name:"group" short:"g" brief:"{CGenDaoBriefGroup}" d:"default"`
|
||||
Prefix string `name:"prefix" short:"f" brief:"{CGenDaoBriefPrefix}"`
|
||||
RemovePrefix string `name:"removePrefix" short:"r" brief:"{CGenDaoBriefRemovePrefix}"`
|
||||
RemoveFieldPrefix string `name:"removeFieldPrefix" short:"rf" brief:"{CGenDaoBriefRemoveFieldPrefix}"`
|
||||
JsonCase string `name:"jsonCase" short:"j" brief:"{CGenDaoBriefJsonCase}" d:"CamelLower"`
|
||||
ImportPrefix string `name:"importPrefix" short:"i" brief:"{CGenDaoBriefImportPrefix}"`
|
||||
DaoPath string `name:"daoPath" short:"d" brief:"{CGenDaoBriefDaoPath}" d:"dao"`
|
||||
TablePath string `name:"tablePath" short:"tp" brief:"{CGenDaoBriefTablePath}" d:"table"`
|
||||
DoPath string `name:"doPath" short:"o" brief:"{CGenDaoBriefDoPath}" d:"model/do"`
|
||||
EntityPath string `name:"entityPath" short:"e" brief:"{CGenDaoBriefEntityPath}" d:"model/entity"`
|
||||
TplDaoTablePath string `name:"tplDaoTablePath" short:"t0" brief:"{CGenDaoBriefTplDaoTablePath}"`
|
||||
TplDaoIndexPath string `name:"tplDaoIndexPath" short:"t1" brief:"{CGenDaoBriefTplDaoIndexPath}"`
|
||||
TplDaoInternalPath string `name:"tplDaoInternalPath" short:"t2" brief:"{CGenDaoBriefTplDaoInternalPath}"`
|
||||
TplDaoDoPath string `name:"tplDaoDoPath" short:"t3" brief:"{CGenDaoBriefTplDaoDoPathPath}"`
|
||||
TplDaoEntityPath string `name:"tplDaoEntityPath" short:"t4" brief:"{CGenDaoBriefTplDaoEntityPath}"`
|
||||
StdTime bool `name:"stdTime" short:"s" brief:"{CGenDaoBriefStdTime}" orphan:"true"`
|
||||
WithTime bool `name:"withTime" short:"w" brief:"{CGenDaoBriefWithTime}" orphan:"true"`
|
||||
GJsonSupport bool `name:"gJsonSupport" short:"n" brief:"{CGenDaoBriefGJsonSupport}" orphan:"true"`
|
||||
OverwriteDao bool `name:"overwriteDao" short:"v" brief:"{CGenDaoBriefOverwriteDao}" orphan:"true"`
|
||||
DescriptionTag bool `name:"descriptionTag" short:"c" brief:"{CGenDaoBriefDescriptionTag}" orphan:"true"`
|
||||
NoJsonTag bool `name:"noJsonTag" short:"k" brief:"{CGenDaoBriefNoJsonTag}" orphan:"true"`
|
||||
NoModelComment bool `name:"noModelComment" short:"m" brief:"{CGenDaoBriefNoModelComment}" orphan:"true"`
|
||||
Clear bool `name:"clear" short:"a" brief:"{CGenDaoBriefClear}" orphan:"true"`
|
||||
GenTable bool `name:"genTable" short:"gt" brief:"{CGenDaoBriefGenTable}" orphan:"true"`
|
||||
|
||||
// TypeMapping maps database field type names to custom Go types.
|
||||
// For example, mapping "decimal" to "float64" or "uuid" to "uuid.UUID".
|
||||
TypeMapping map[DBFieldTypeName]CustomAttributeType `name:"typeMapping" short:"y" brief:"{CGenDaoBriefTypeMapping}" orphan:"true"`
|
||||
// FieldMapping maps specific table.field combinations to custom Go types.
|
||||
// For example, mapping "user.balance" to "decimal.Decimal".
|
||||
TypeMapping map[DBFieldTypeName]CustomAttributeType `name:"typeMapping" short:"y" brief:"{CGenDaoBriefTypeMapping}" orphan:"true"`
|
||||
FieldMapping map[DBTableFieldName]CustomAttributeType `name:"fieldMapping" short:"fm" brief:"{CGenDaoBriefFieldMapping}" orphan:"true"`
|
||||
|
||||
// genItems tracks all generated file paths and directories for cleanup purposes.
|
||||
// internal usage purpose.
|
||||
genItems *CGenDaoInternalGenItems
|
||||
}
|
||||
|
||||
// CGenDaoOutput is the output of the "gen dao" command (currently empty).
|
||||
CGenDaoOutput struct{}
|
||||
|
||||
// CGenDaoInternalInput extends CGenDaoInput with runtime-resolved fields
|
||||
// used during the actual generation process.
|
||||
CGenDaoInternalInput struct {
|
||||
CGenDaoInput
|
||||
DB gdb.DB // Database connection instance (nil in SQL file mode).
|
||||
TableNames []string // Original table names from database or SQL files.
|
||||
NewTableNames []string // Processed table names after prefix removal and sharding.
|
||||
ShardingTableSet *gset.StrSet // Set of table names identified as sharding tables.
|
||||
// TableFieldsMap stores pre-parsed table fields from SQL files.
|
||||
// When this is set (SQL file mode), DB may be nil.
|
||||
TableFieldsMap map[string]map[string]*gdb.TableField
|
||||
DB gdb.DB
|
||||
TableNames []string
|
||||
NewTableNames []string
|
||||
ShardingTableSet *gset.StrSet
|
||||
}
|
||||
|
||||
// DBTableFieldName is the fully-qualified field name in "table.field" format.
|
||||
DBTableFieldName = string
|
||||
// DBFieldTypeName is the database column type name (e.g., "varchar", "decimal").
|
||||
DBFieldTypeName = string
|
||||
// CustomAttributeType defines a custom Go type mapping with its import path.
|
||||
DBTableFieldName = string
|
||||
DBFieldTypeName = string
|
||||
CustomAttributeType struct {
|
||||
Type string `brief:"custom attribute type name"` // Go type name (e.g., "decimal.Decimal").
|
||||
Import string `brief:"custom import for this type"` // Go import path (e.g., "github.com/shopspring/decimal").
|
||||
Type string `brief:"custom attribute type name"`
|
||||
Import string `brief:"custom import for this type"`
|
||||
}
|
||||
)
|
||||
|
||||
var (
|
||||
createdAt = gtime.Now() // Timestamp captured at program start, used in generated file headers.
|
||||
tplView = gview.New() // Shared template view instance for rendering all Go file templates.
|
||||
// defaultTypeMapping provides built-in type mappings from database types to Go types.
|
||||
// User-provided TypeMapping takes precedence over these defaults.
|
||||
createdAt = gtime.Now()
|
||||
tplView = gview.New()
|
||||
defaultTypeMapping = map[DBFieldTypeName]CustomAttributeType{
|
||||
"decimal": {
|
||||
Type: "float64",
|
||||
@ -134,8 +111,7 @@ var (
|
||||
},
|
||||
}
|
||||
|
||||
// twRenderer configures the tablewriter to render without borders or separators,
|
||||
// producing clean aligned text output for generated Go source code.
|
||||
// tablewriter Options
|
||||
twRenderer = tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{
|
||||
Borders: tw.Border{Top: tw.Off, Bottom: tw.Off, Left: tw.Off, Right: tw.Off},
|
||||
Settings: tw.Settings{
|
||||
@ -150,17 +126,9 @@ var (
|
||||
})
|
||||
)
|
||||
|
||||
// Dao is the main entry point for the "gen dao" command.
|
||||
// It dispatches to the appropriate generation mode based on input:
|
||||
// - SQL file mode (SqlDir is set): generates from DDL files without database connection.
|
||||
// - Link mode (Link is set): uses a direct database connection string.
|
||||
// - Config mode: reads database configuration from the application config file.
|
||||
func (c CGenDao) Dao(ctx context.Context, in CGenDaoInput) (out *CGenDaoOutput, err error) {
|
||||
in.genItems = newCGenDaoInternalGenItems()
|
||||
if in.SqlDir != "" {
|
||||
// SQL file mode: generate from SQL DDL files without database connection.
|
||||
doGenDaoFromSQLFiles(ctx, in)
|
||||
} else if in.Link != "" {
|
||||
if in.Link != "" {
|
||||
doGenDaoForArray(ctx, -1, in)
|
||||
} else if g.Cfg().Available(ctx) {
|
||||
v := g.Cfg().MustGet(ctx, CGenDaoConfig)
|
||||
@ -179,11 +147,7 @@ func (c CGenDao) Dao(ctx context.Context, in CGenDaoInput) (out *CGenDaoOutput,
|
||||
return
|
||||
}
|
||||
|
||||
// doGenDaoForArray implements the "gen dao" command for a single configuration entry.
|
||||
// When index >= 0, it reads configuration from the array at that index.
|
||||
// When index < 0, it uses the input as-is (for Link mode or single config mode).
|
||||
// It performs the full generation pipeline: connect to DB, resolve tables,
|
||||
// apply sharding patterns, and generate dao/table/do/entity files.
|
||||
// doGenDaoForArray implements the "gen dao" command for configuration array.
|
||||
func doGenDaoForArray(ctx context.Context, index int, in CGenDaoInput) {
|
||||
var (
|
||||
err error
|
||||
@ -368,10 +332,6 @@ func doGenDaoForArray(ctx context.Context, index int, in CGenDaoInput) {
|
||||
in.genItems.SetClear(in.Clear)
|
||||
}
|
||||
|
||||
// getImportPartContent analyzes the generated Go source code and builds the import block.
|
||||
// It automatically detects usage of gtime.Time, time.Time, and gjson.Json in the source,
|
||||
// and includes the corresponding import paths. Additional custom imports (from TypeMapping
|
||||
// or FieldMapping) are appended and their dependencies are resolved via "go get" if needed.
|
||||
func getImportPartContent(ctx context.Context, source string, isDo bool, appendImports []string) string {
|
||||
var packageImportsArray = garray.NewStrArray()
|
||||
if isDo {
|
||||
@ -425,9 +385,6 @@ func getImportPartContent(ctx context.Context, source string, isDo bool, appendI
|
||||
return packageImportsStr
|
||||
}
|
||||
|
||||
// assignDefaultVar sets the default template variables for datetime strings
|
||||
// used in generated file headers. The creation timestamp is only included
|
||||
// when WithTime is enabled in the input configuration.
|
||||
func assignDefaultVar(view *gview.View, in CGenDaoInternalInput) {
|
||||
var (
|
||||
tplCreatedAtDatetimeStr string
|
||||
@ -442,8 +399,6 @@ func assignDefaultVar(view *gview.View, in CGenDaoInternalInput) {
|
||||
})
|
||||
}
|
||||
|
||||
// sortFieldKeyForDao returns field names sorted by their Index in the TableField map.
|
||||
// This preserves the original column order as defined in the database table schema.
|
||||
func sortFieldKeyForDao(fieldMap map[string]*gdb.TableField) []string {
|
||||
names := make(map[int]string)
|
||||
for _, field := range fieldMap {
|
||||
@ -468,20 +423,6 @@ func sortFieldKeyForDao(fieldMap map[string]*gdb.TableField) []string {
|
||||
return result
|
||||
}
|
||||
|
||||
// getTableFields retrieves table fields either from the pre-parsed TableFieldsMap (SQL file mode)
|
||||
// or from the database connection. This abstracts the data source for generation functions.
|
||||
func getTableFields(ctx context.Context, in CGenDaoInternalInput, tableName string) (map[string]*gdb.TableField, error) {
|
||||
if in.TableFieldsMap != nil {
|
||||
if fields, ok := in.TableFieldsMap[tableName]; ok {
|
||||
return fields, nil
|
||||
}
|
||||
return nil, fmt.Errorf("table '%s' not found in SQL files", tableName)
|
||||
}
|
||||
return in.DB.TableFields(ctx, tableName)
|
||||
}
|
||||
|
||||
// getTemplateFromPathOrDefault returns the template content from the given file path.
|
||||
// If the file path is empty or the file has no content, it falls back to the default template.
|
||||
func getTemplateFromPathOrDefault(filePath string, def string) string {
|
||||
if filePath != "" {
|
||||
if contents := gfile.GetContents(filePath); contents != "" {
|
||||
@ -548,130 +489,3 @@ func filterTablesByPatterns(allTables []string, patterns []string) []string {
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// doGenDaoFromSQLFiles implements the "gen dao" command for SQL file mode.
|
||||
// It parses DDL SQL files to obtain table structures without requiring a database connection.
|
||||
func doGenDaoFromSQLFiles(ctx context.Context, in CGenDaoInput) {
|
||||
if dirRealPath := gfile.RealPath(in.Path); dirRealPath == "" {
|
||||
mlog.Fatalf(`path "%s" does not exist`, in.Path)
|
||||
}
|
||||
if dirRealPath := gfile.RealPath(in.SqlDir); dirRealPath == "" {
|
||||
mlog.Fatalf(`SQL directory "%s" does not exist`, in.SqlDir)
|
||||
}
|
||||
|
||||
dialect := SQLDialect(strings.ToLower(in.SqlType))
|
||||
tableNames, tableFieldsMap := ParseSQLFilesFromDir(in.SqlDir, dialect)
|
||||
|
||||
removePrefixArray := gstr.SplitAndTrim(in.RemovePrefix, ",")
|
||||
|
||||
// Table filtering by name patterns.
|
||||
if in.Tables != "" {
|
||||
inputTables := gstr.SplitAndTrim(in.Tables, ",")
|
||||
var hasPattern bool
|
||||
for _, t := range inputTables {
|
||||
if containsWildcard(t) {
|
||||
hasPattern = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if hasPattern {
|
||||
tableNames = filterTablesByPatterns(tableNames, inputTables)
|
||||
} else {
|
||||
tableNames = inputTables
|
||||
}
|
||||
}
|
||||
|
||||
// Table excluding.
|
||||
if in.TablesEx != "" {
|
||||
array := garray.NewStrArrayFrom(tableNames)
|
||||
for _, p := range gstr.SplitAndTrim(in.TablesEx, ",") {
|
||||
if containsWildcard(p) {
|
||||
regPattern := "^" + patternToRegex(p) + "$"
|
||||
for _, v := range array.Clone().Slice() {
|
||||
if gregex.IsMatchString(regPattern, v) {
|
||||
array.RemoveValue(v)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
array.RemoveValue(p)
|
||||
}
|
||||
}
|
||||
tableNames = array.Slice()
|
||||
}
|
||||
|
||||
// merge default typeMapping.
|
||||
if in.TypeMapping == nil {
|
||||
in.TypeMapping = defaultTypeMapping
|
||||
} else {
|
||||
for key, typeMapping := range defaultTypeMapping {
|
||||
if _, ok := in.TypeMapping[key]; !ok {
|
||||
in.TypeMapping[key] = typeMapping
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Process table names (prefix removal, sharding, etc.)
|
||||
var (
|
||||
newTableNames = make([]string, len(tableNames))
|
||||
shardingNewTableSet = gset.NewStrSet()
|
||||
)
|
||||
sortedShardingPatterns := make([]string, len(in.ShardingPattern))
|
||||
copy(sortedShardingPatterns, in.ShardingPattern)
|
||||
sort.Slice(sortedShardingPatterns, func(i, j int) bool {
|
||||
return len(sortedShardingPatterns[i]) > len(sortedShardingPatterns[j])
|
||||
})
|
||||
for i, tableName := range tableNames {
|
||||
newTableName := tableName
|
||||
for _, v := range removePrefixArray {
|
||||
newTableName = gstr.TrimLeftStr(newTableName, v, 1)
|
||||
}
|
||||
if len(sortedShardingPatterns) > 0 {
|
||||
for _, pattern := range sortedShardingPatterns {
|
||||
var (
|
||||
match []string
|
||||
regPattern = gstr.Replace(pattern, "?", `(.+)`)
|
||||
err error
|
||||
)
|
||||
match, err = gregex.MatchString(regPattern, newTableName)
|
||||
if err != nil {
|
||||
mlog.Fatalf(`invalid sharding pattern "%s": %+v`, pattern, err)
|
||||
}
|
||||
if len(match) < 2 {
|
||||
continue
|
||||
}
|
||||
newTableName = gstr.Replace(pattern, "?", "")
|
||||
newTableName = gstr.Trim(newTableName, `_.-`)
|
||||
if shardingNewTableSet.Contains(newTableName) {
|
||||
tableNames[i] = ""
|
||||
break
|
||||
}
|
||||
shardingNewTableSet.Add(in.Prefix + newTableName)
|
||||
break
|
||||
}
|
||||
}
|
||||
newTableName = in.Prefix + newTableName
|
||||
if tableNames[i] != "" {
|
||||
newTableNames[i] = newTableName
|
||||
}
|
||||
}
|
||||
tableNames = garray.NewStrArrayFrom(tableNames).FilterEmpty().Slice()
|
||||
newTableNames = garray.NewStrArrayFrom(newTableNames).FilterEmpty().Slice()
|
||||
in.genItems.Scale()
|
||||
|
||||
internalInput := CGenDaoInternalInput{
|
||||
CGenDaoInput: in,
|
||||
DB: nil,
|
||||
TableNames: tableNames,
|
||||
NewTableNames: newTableNames,
|
||||
ShardingTableSet: shardingNewTableSet,
|
||||
TableFieldsMap: tableFieldsMap,
|
||||
}
|
||||
|
||||
// Generate all files using the same flow as database mode.
|
||||
generateDao(ctx, internalInput)
|
||||
generateTable(ctx, internalInput)
|
||||
generateDo(ctx, internalInput)
|
||||
generateEntity(ctx, internalInput)
|
||||
|
||||
in.genItems.SetClear(in.Clear)
|
||||
}
|
||||
|
||||
@ -13,10 +13,6 @@ import (
|
||||
"github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog"
|
||||
)
|
||||
|
||||
// doClear performs cleanup of stale generated files across all generation items.
|
||||
// It collects all generated file paths from all items, then for each item with
|
||||
// Clear enabled, removes any .go files in its directories that are NOT in the
|
||||
// generated file list. This ensures files for dropped/removed tables are cleaned up.
|
||||
func doClear(items *CGenDaoInternalGenItems) {
|
||||
var allGeneratedFilePaths = make([]string, 0)
|
||||
for _, item := range items.Items {
|
||||
@ -33,10 +29,6 @@ func doClear(items *CGenDaoInternalGenItems) {
|
||||
}
|
||||
}
|
||||
|
||||
// doClearItem removes stale .go files for a single generation item.
|
||||
// It scans all storage directories for .go files and deletes any file
|
||||
// that is not in the allGeneratedFilePaths list (i.e., no longer corresponds
|
||||
// to an existing database table).
|
||||
func doClearItem(item CGenDaoInternalGenItem, allGeneratedFilePaths []string) {
|
||||
var generatedFilePaths = make([]string, 0)
|
||||
for _, dirPath := range item.StorageDirPaths {
|
||||
|
||||
@ -26,9 +26,6 @@ import (
|
||||
"github.com/gogf/gf/cmd/gf/v2/internal/utility/utils"
|
||||
)
|
||||
|
||||
// generateDao generates dao files (index + internal) for all tables in the input.
|
||||
// It creates the dao directory structure and iterates over each table to generate
|
||||
// individual dao files via generateDaoSingle.
|
||||
func generateDao(ctx context.Context, in CGenDaoInternalInput) {
|
||||
var (
|
||||
dirPathDao = gfile.Join(in.Path, in.DaoPath)
|
||||
@ -51,20 +48,21 @@ func generateDao(ctx context.Context, in CGenDaoInternalInput) {
|
||||
}
|
||||
}
|
||||
|
||||
// generateDaoSingleInput holds all parameters needed to generate dao files for a single table.
|
||||
type generateDaoSingleInput struct {
|
||||
CGenDaoInternalInput
|
||||
TableName string // Original table name as it exists in the database.
|
||||
NewTableName string // Processed table name after prefix removal and sharding.
|
||||
DirPathDao string // Directory path for the dao index files.
|
||||
DirPathDaoInternal string // Directory path for the dao internal implementation files.
|
||||
IsSharding bool // Whether this table is a sharding table (merged from multiple physical tables).
|
||||
// TableName specifies the table name of the table.
|
||||
TableName string
|
||||
// NewTableName specifies the prefix-stripped or custom edited name of the table.
|
||||
NewTableName string
|
||||
DirPathDao string
|
||||
DirPathDaoInternal string
|
||||
IsSharding bool
|
||||
}
|
||||
|
||||
// generateDaoSingle generates the dao and model content of given table.
|
||||
func generateDaoSingle(ctx context.Context, in generateDaoSingleInput) {
|
||||
// Generating table data preparing.
|
||||
fieldMap, err := getTableFields(ctx, in.CGenDaoInternalInput, in.TableName)
|
||||
fieldMap, err := in.DB.TableFields(ctx, in.TableName)
|
||||
if err != nil {
|
||||
mlog.Fatalf(`fetching tables fields failed for table "%s": %+v`, in.TableName, err)
|
||||
}
|
||||
@ -107,21 +105,14 @@ func generateDaoSingle(ctx context.Context, in generateDaoSingleInput) {
|
||||
})
|
||||
}
|
||||
|
||||
// generateDaoIndexInput holds parameters for generating the dao index file.
|
||||
// The index file provides the public API (exported struct and constructor)
|
||||
// for accessing the DAO, delegating to the internal implementation.
|
||||
type generateDaoIndexInput struct {
|
||||
generateDaoSingleInput
|
||||
TableNameCamelCase string // CamelCase version of the table name (e.g., "UserDetail").
|
||||
TableNameCamelLowerCase string // camelCase version of the table name (e.g., "userDetail").
|
||||
ImportPrefix string // Go import path prefix for the dao package.
|
||||
FileName string // Output file name (without extension).
|
||||
TableNameCamelCase string
|
||||
TableNameCamelLowerCase string
|
||||
ImportPrefix string
|
||||
FileName string
|
||||
}
|
||||
|
||||
// generateDaoIndex generates the dao index file for a single table.
|
||||
// The index file is the public-facing dao file that users import directly.
|
||||
// It will NOT overwrite an existing file unless OverwriteDao is enabled,
|
||||
// allowing users to customize the index file without losing changes.
|
||||
func generateDaoIndex(in generateDaoIndexInput) {
|
||||
path := filepath.FromSlash(gfile.Join(in.DirPathDao, in.FileName+".go"))
|
||||
// It should add path to result slice whenever it would generate the path file or not.
|
||||
@ -156,21 +147,15 @@ func generateDaoIndex(in generateDaoIndexInput) {
|
||||
}
|
||||
}
|
||||
|
||||
// generateDaoInternalInput holds parameters for generating the dao internal file.
|
||||
// The internal file contains the actual DAO implementation with column definitions
|
||||
// and is always overwritten on regeneration.
|
||||
type generateDaoInternalInput struct {
|
||||
generateDaoSingleInput
|
||||
TableNameCamelCase string // CamelCase version of the table name.
|
||||
TableNameCamelLowerCase string // camelCase version of the table name.
|
||||
ImportPrefix string // Go import path prefix for the dao package.
|
||||
FileName string // Output file name (without extension).
|
||||
FieldMap map[string]*gdb.TableField // Map of column name to field metadata.
|
||||
TableNameCamelCase string
|
||||
TableNameCamelLowerCase string
|
||||
ImportPrefix string
|
||||
FileName string
|
||||
FieldMap map[string]*gdb.TableField
|
||||
}
|
||||
|
||||
// generateDaoInternal generates the dao internal implementation file for a single table.
|
||||
// This file is always regenerated (overwritten) and contains the Columns struct definition
|
||||
// with column name constants and their string value assignments.
|
||||
func generateDaoInternal(in generateDaoInternalInput) {
|
||||
var (
|
||||
ctx = context.Background()
|
||||
|
||||
@ -22,10 +22,6 @@ import (
|
||||
"github.com/gogf/gf/cmd/gf/v2/internal/utility/utils"
|
||||
)
|
||||
|
||||
// generateDo generates DO (Data Object) files for all tables.
|
||||
// DO structs use "any" type for all scalar fields (replacing concrete types),
|
||||
// enabling flexible query building with the g.Meta `orm:"do:true"` tag.
|
||||
// Pointer, slice, and map types are preserved as-is.
|
||||
func generateDo(ctx context.Context, in CGenDaoInternalInput) {
|
||||
var dirPathDo = filepath.FromSlash(gfile.Join(in.Path, in.DoPath))
|
||||
in.genItems.AppendDirPath(dirPathDo)
|
||||
@ -34,7 +30,7 @@ func generateDo(ctx context.Context, in CGenDaoInternalInput) {
|
||||
in.NoModelComment = false
|
||||
// Model content.
|
||||
for i, tableName := range in.TableNames {
|
||||
fieldMap, err := getTableFields(ctx, in, tableName)
|
||||
fieldMap, err := in.DB.TableFields(ctx, tableName)
|
||||
if err != nil {
|
||||
mlog.Fatalf("fetching tables fields failed for table '%s':\n%v", tableName, err)
|
||||
}
|
||||
@ -79,9 +75,6 @@ func generateDo(ctx context.Context, in CGenDaoInternalInput) {
|
||||
}
|
||||
}
|
||||
|
||||
// generateDoContent renders the DO file content using the template engine.
|
||||
// It assembles template variables including package imports, struct definition,
|
||||
// and metadata, then parses the DO template to produce the final file content.
|
||||
func generateDoContent(
|
||||
ctx context.Context, in CGenDaoInternalInput, tableName, tableNameCamelCase, structDefine string,
|
||||
) string {
|
||||
|
||||
@ -20,15 +20,12 @@ import (
|
||||
"github.com/gogf/gf/cmd/gf/v2/internal/utility/utils"
|
||||
)
|
||||
|
||||
// generateEntity generates entity struct files for all tables.
|
||||
// Entity structs represent database table rows with concrete Go types,
|
||||
// including orm tags for field-to-column mapping and json tags for serialization.
|
||||
func generateEntity(ctx context.Context, in CGenDaoInternalInput) {
|
||||
var dirPathEntity = gfile.Join(in.Path, in.EntityPath)
|
||||
in.genItems.AppendDirPath(dirPathEntity)
|
||||
// Model content.
|
||||
for i, tableName := range in.TableNames {
|
||||
fieldMap, err := getTableFields(ctx, in, tableName)
|
||||
fieldMap, err := in.DB.TableFields(ctx, tableName)
|
||||
if err != nil {
|
||||
mlog.Fatalf("fetching tables fields failed for table '%s':\n%v", tableName, err)
|
||||
}
|
||||
@ -63,9 +60,6 @@ func generateEntity(ctx context.Context, in CGenDaoInternalInput) {
|
||||
}
|
||||
}
|
||||
|
||||
// generateEntityContent renders the entity file content using the template engine.
|
||||
// It assembles template variables and parses the entity template to produce
|
||||
// the final Go source file content with proper imports and struct definition.
|
||||
func generateEntityContent(
|
||||
ctx context.Context, in CGenDaoInternalInput, tableName, tableNameCamelCase, structDefine string, appendImports []string,
|
||||
) string {
|
||||
|
||||
@ -7,25 +7,17 @@
|
||||
package gendao
|
||||
|
||||
type (
|
||||
// CGenDaoInternalGenItems tracks generation state across multiple configuration entries.
|
||||
// Each configuration entry (e.g., different database links in the config array)
|
||||
// gets its own CGenDaoInternalGenItem via Scale(). The index field points to the
|
||||
// current active item.
|
||||
CGenDaoInternalGenItems struct {
|
||||
index int // Index of the current active generation item.
|
||||
Items []CGenDaoInternalGenItem // List of all generation items, one per config entry.
|
||||
index int
|
||||
Items []CGenDaoInternalGenItem
|
||||
}
|
||||
|
||||
// CGenDaoInternalGenItem tracks generated files and directories for a single
|
||||
// configuration entry. Used by the Clear feature to identify and remove stale files.
|
||||
CGenDaoInternalGenItem struct {
|
||||
Clear bool // Whether to clear stale files for this item.
|
||||
StorageDirPaths []string // Directories where generated files are stored (dao, do, entity, table).
|
||||
GeneratedFilePaths []string // All file paths generated in this run.
|
||||
Clear bool
|
||||
StorageDirPaths []string
|
||||
GeneratedFilePaths []string
|
||||
}
|
||||
)
|
||||
|
||||
// newCGenDaoInternalGenItems creates a new generation items tracker with an empty item list.
|
||||
func newCGenDaoInternalGenItems() *CGenDaoInternalGenItems {
|
||||
return &CGenDaoInternalGenItems{
|
||||
index: -1,
|
||||
@ -33,8 +25,6 @@ func newCGenDaoInternalGenItems() *CGenDaoInternalGenItems {
|
||||
}
|
||||
}
|
||||
|
||||
// Scale adds a new generation item and advances the index to it.
|
||||
// Must be called once per configuration entry before generating files.
|
||||
func (i *CGenDaoInternalGenItems) Scale() {
|
||||
i.Items = append(i.Items, CGenDaoInternalGenItem{
|
||||
StorageDirPaths: make([]string, 0),
|
||||
@ -44,12 +34,10 @@ func (i *CGenDaoInternalGenItems) Scale() {
|
||||
i.index++
|
||||
}
|
||||
|
||||
// SetClear enables or disables the clear (stale file removal) flag for the current item.
|
||||
func (i *CGenDaoInternalGenItems) SetClear(clear bool) {
|
||||
i.Items[i.index].Clear = clear
|
||||
}
|
||||
|
||||
// AppendDirPath records a directory path used for storing generated files in the current item.
|
||||
func (i *CGenDaoInternalGenItems) AppendDirPath(storageDirPath string) {
|
||||
i.Items[i.index].StorageDirPaths = append(
|
||||
i.Items[i.index].StorageDirPaths,
|
||||
@ -57,7 +45,6 @@ func (i *CGenDaoInternalGenItems) AppendDirPath(storageDirPath string) {
|
||||
)
|
||||
}
|
||||
|
||||
// AppendGeneratedFilePath records a file path that was generated in the current item.
|
||||
func (i *CGenDaoInternalGenItems) AppendGeneratedFilePath(generatedFilePath string) {
|
||||
i.Items[i.index].GeneratedFilePaths = append(
|
||||
i.Items[i.index].GeneratedFilePaths,
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@ -1,211 +0,0 @@
|
||||
// Copyright GoFrame gf 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 gendao
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
)
|
||||
|
||||
// MSSQLParser implements SQLParser for SQL Server (T-SQL) DDL.
|
||||
type MSSQLParser struct{}
|
||||
|
||||
// ParseCreateTable parses a single MSSQL CREATE TABLE statement.
|
||||
func (p *MSSQLParser) ParseCreateTable(stmt string) (string, map[string]*gdb.TableField, error) {
|
||||
body, _, ok := extractBodyAndTrailing(stmt)
|
||||
if !ok {
|
||||
return "", nil, nil
|
||||
}
|
||||
|
||||
parenIdx := strings.Index(stmt, "(")
|
||||
header := stmt[:parenIdx]
|
||||
tableName := extractTableName(header)
|
||||
if tableName == "" {
|
||||
return "", nil, fmt.Errorf("cannot extract table name from: %s", header)
|
||||
}
|
||||
|
||||
columnDefs := splitColumns(body)
|
||||
fields := make(map[string]*gdb.TableField)
|
||||
pkColumns := findPrimaryKeysFromConstraints(columnDefs)
|
||||
|
||||
fieldIndex := 0
|
||||
for _, def := range columnDefs {
|
||||
def = strings.TrimSpace(def)
|
||||
if def == "" {
|
||||
continue
|
||||
}
|
||||
firstWord := strings.ToUpper(strings.Fields(def)[0])
|
||||
if isConstraintKeyword(firstWord) {
|
||||
continue
|
||||
}
|
||||
|
||||
field, err := p.parseColumnDef(def, fieldIndex)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if field != nil {
|
||||
fields[field.Name] = field
|
||||
fieldIndex++
|
||||
}
|
||||
}
|
||||
|
||||
for _, pkCol := range pkColumns {
|
||||
if f, ok := fields[pkCol]; ok {
|
||||
f.Key = "PRI"
|
||||
}
|
||||
}
|
||||
|
||||
return tableName, fields, nil
|
||||
}
|
||||
|
||||
// ParseAlterTable parses MSSQL ALTER TABLE statements.
|
||||
func (p *MSSQLParser) ParseAlterTable(stmt string, tables map[string]map[string]*gdb.TableField) error {
|
||||
return parseAlterTableCommon(stmt, tables, p.parseColumnDef)
|
||||
}
|
||||
|
||||
// ParseComment parses EXEC sp_addextendedproperty to extract column comments.
|
||||
func (p *MSSQLParser) ParseComment(stmt string, tables map[string]map[string]*gdb.TableField) {
|
||||
upper := strings.ToUpper(strings.TrimSpace(stmt))
|
||||
if !strings.Contains(upper, "SP_ADDEXTENDEDPROPERTY") ||
|
||||
!strings.Contains(upper, "MS_DESCRIPTION") {
|
||||
return
|
||||
}
|
||||
|
||||
// Extract quoted string values
|
||||
var values []string
|
||||
inQuote := false
|
||||
var current strings.Builder
|
||||
for i := 0; i < len(stmt); i++ {
|
||||
ch := stmt[i]
|
||||
if ch == '\'' {
|
||||
if inQuote {
|
||||
if i+1 < len(stmt) && stmt[i+1] == '\'' {
|
||||
current.WriteByte('\'')
|
||||
i++
|
||||
continue
|
||||
}
|
||||
values = append(values, current.String())
|
||||
current.Reset()
|
||||
inQuote = false
|
||||
} else {
|
||||
inQuote = true
|
||||
}
|
||||
} else if inQuote {
|
||||
current.WriteByte(ch)
|
||||
}
|
||||
}
|
||||
|
||||
if len(values) < 8 {
|
||||
return
|
||||
}
|
||||
|
||||
var (
|
||||
comment string
|
||||
tableName string
|
||||
columnName string
|
||||
)
|
||||
|
||||
for i := 0; i < len(values)-1; i++ {
|
||||
switch strings.ToUpper(values[i]) {
|
||||
case "MS_DESCRIPTION":
|
||||
comment = values[i+1]
|
||||
case "TABLE":
|
||||
tableName = values[i+1]
|
||||
case "COLUMN":
|
||||
columnName = values[i+1]
|
||||
}
|
||||
}
|
||||
|
||||
if tableName != "" && columnName != "" && comment != "" {
|
||||
if fields, ok := tables[tableName]; ok {
|
||||
if field, ok := fields[columnName]; ok {
|
||||
field.Comment = comment
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// parseColumnDef parses a single MSSQL column definition string into a TableField.
|
||||
// It handles MSSQL-specific syntax including bracket-quoted identifiers and
|
||||
// type parameters like varchar(max).
|
||||
func (p *MSSQLParser) parseColumnDef(def string, index int) (*gdb.TableField, error) {
|
||||
tokens := mysqlTokenize(def)
|
||||
if len(tokens) < 2 {
|
||||
return nil, fmt.Errorf("invalid column definition: %s", def)
|
||||
}
|
||||
|
||||
field := &gdb.TableField{
|
||||
Index: index,
|
||||
Name: unquoteIdentifier(tokens[0]),
|
||||
Null: true,
|
||||
}
|
||||
|
||||
field.Type = tokens[1]
|
||||
|
||||
rest := ""
|
||||
if len(tokens) > 2 {
|
||||
rest = strings.Join(tokens[2:], " ")
|
||||
}
|
||||
if !strings.Contains(field.Type, "(") && strings.HasPrefix(strings.TrimSpace(rest), "(") {
|
||||
end := strings.Index(rest, ")")
|
||||
if end >= 0 {
|
||||
field.Type += rest[:end+1]
|
||||
rest = strings.TrimSpace(rest[end+1:])
|
||||
}
|
||||
}
|
||||
|
||||
p.parseColumnAttributes(field, rest)
|
||||
|
||||
return field, nil
|
||||
}
|
||||
|
||||
// parseColumnAttributes parses MSSQL column constraint keywords including
|
||||
// NOT NULL, NULL, PRIMARY KEY, UNIQUE, IDENTITY (auto-increment), and DEFAULT.
|
||||
func (p *MSSQLParser) parseColumnAttributes(field *gdb.TableField, attrs string) {
|
||||
words := strings.Fields(attrs)
|
||||
upperWords := strings.Fields(strings.ToUpper(attrs))
|
||||
|
||||
for i := 0; i < len(upperWords); i++ {
|
||||
switch upperWords[i] {
|
||||
case "NOT":
|
||||
if i+1 < len(upperWords) && upperWords[i+1] == "NULL" {
|
||||
field.Null = false
|
||||
i++
|
||||
}
|
||||
case "NULL":
|
||||
field.Null = true
|
||||
case "PRIMARY":
|
||||
if i+1 < len(upperWords) && upperWords[i+1] == "KEY" {
|
||||
field.Key = "PRI"
|
||||
i++
|
||||
}
|
||||
case "UNIQUE":
|
||||
if field.Key == "" {
|
||||
field.Key = "UNI"
|
||||
}
|
||||
case "IDENTITY":
|
||||
field.Extra = "auto_increment"
|
||||
if i+1 < len(words) && strings.HasPrefix(words[i+1], "(") {
|
||||
i++
|
||||
}
|
||||
default:
|
||||
if strings.HasPrefix(upperWords[i], "IDENTITY(") || strings.HasPrefix(upperWords[i], "IDENTITY (") {
|
||||
field.Extra = "auto_increment"
|
||||
}
|
||||
case "DEFAULT":
|
||||
if i+1 < len(words) {
|
||||
defaultVal, _ := extractDefaultValue("DEFAULT " + strings.Join(words[i+1:], " "))
|
||||
field.Default = defaultVal
|
||||
if defaultVal != nil {
|
||||
i++
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,72 +0,0 @@
|
||||
// Copyright GoFrame gf 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 gendao
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
)
|
||||
|
||||
func Test_MSSQL_CreateTable_Basic(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
parser := &MSSQLParser{}
|
||||
sql := `
|
||||
CREATE TABLE [dbo].[users] (
|
||||
[id] INT IDENTITY(1,1) NOT NULL,
|
||||
[name] NVARCHAR(100) NOT NULL,
|
||||
[email] NVARCHAR(200) NULL,
|
||||
[balance] DECIMAL(18,2) DEFAULT 0,
|
||||
[created_at] DATETIME2 NOT NULL DEFAULT GETDATE(),
|
||||
CONSTRAINT [PK_users] PRIMARY KEY CLUSTERED ([id])
|
||||
);
|
||||
EXEC sp_addextendedproperty 'MS_Description', 'User ID', 'SCHEMA', 'dbo', 'TABLE', 'users', 'COLUMN', 'id';
|
||||
EXEC sp_addextendedproperty 'MS_Description', 'User name', 'SCHEMA', 'dbo', 'TABLE', 'users', 'COLUMN', 'name';
|
||||
`
|
||||
tables := make(map[string]map[string]*gdb.TableField)
|
||||
err := processSQL(parser, sql, tables)
|
||||
t.AssertNil(err)
|
||||
|
||||
fields := tables["users"]
|
||||
t.Assert(len(fields), 5)
|
||||
|
||||
t.Assert(fields["id"].Extra, "auto_increment")
|
||||
t.Assert(fields["id"].Null, false)
|
||||
t.Assert(fields["id"].Key, "PRI")
|
||||
t.Assert(fields["id"].Comment, "User ID")
|
||||
|
||||
t.Assert(fields["name"].Comment, "User name")
|
||||
t.Assert(fields["name"].Null, false)
|
||||
|
||||
t.Assert(fields["email"].Null, true)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_MSSQL_AlterTable(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
parser := &MSSQLParser{}
|
||||
sql := `
|
||||
CREATE TABLE users (
|
||||
id INT IDENTITY(1,1) NOT NULL,
|
||||
name NVARCHAR(100) NOT NULL,
|
||||
CONSTRAINT PK_users PRIMARY KEY (id)
|
||||
);
|
||||
ALTER TABLE users ADD email NVARCHAR(200) NULL;
|
||||
ALTER TABLE users DROP COLUMN name;
|
||||
`
|
||||
tables := make(map[string]map[string]*gdb.TableField)
|
||||
err := processSQL(parser, sql, tables)
|
||||
t.AssertNil(err)
|
||||
|
||||
fields := tables["users"]
|
||||
t.Assert(len(fields), 2) // id, email
|
||||
_, ok := fields["name"]
|
||||
t.Assert(ok, false)
|
||||
t.Assert(fields["email"].Null, true)
|
||||
})
|
||||
}
|
||||
@ -1,199 +0,0 @@
|
||||
// Copyright GoFrame gf 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 gendao
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
)
|
||||
|
||||
// MySQLParser implements SQLParser for MySQL/MariaDB/TiDB DDL.
|
||||
type MySQLParser struct{}
|
||||
|
||||
// ParseCreateTable parses a single MySQL CREATE TABLE statement.
|
||||
func (p *MySQLParser) ParseCreateTable(stmt string) (string, map[string]*gdb.TableField, error) {
|
||||
body, trailing, ok := extractBodyAndTrailing(stmt)
|
||||
if !ok {
|
||||
return "", nil, nil
|
||||
}
|
||||
|
||||
parenIdx := strings.Index(stmt, "(")
|
||||
header := stmt[:parenIdx]
|
||||
tableName := extractTableName(header)
|
||||
if tableName == "" {
|
||||
return "", nil, fmt.Errorf("cannot extract table name from: %s", header)
|
||||
}
|
||||
|
||||
columnDefs := splitColumns(body)
|
||||
fields := make(map[string]*gdb.TableField)
|
||||
pkColumns := findPrimaryKeysFromConstraints(columnDefs)
|
||||
|
||||
fieldIndex := 0
|
||||
for _, def := range columnDefs {
|
||||
def = strings.TrimSpace(def)
|
||||
if def == "" {
|
||||
continue
|
||||
}
|
||||
firstWord := strings.ToUpper(strings.Fields(def)[0])
|
||||
if isConstraintKeyword(firstWord) {
|
||||
continue
|
||||
}
|
||||
|
||||
field, err := p.parseColumnDef(def, fieldIndex)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if field != nil {
|
||||
fields[field.Name] = field
|
||||
fieldIndex++
|
||||
}
|
||||
}
|
||||
|
||||
for _, pkCol := range pkColumns {
|
||||
if f, ok := fields[pkCol]; ok {
|
||||
f.Key = "PRI"
|
||||
}
|
||||
}
|
||||
|
||||
// Extract inline comments from trailing table options (not used for field generation)
|
||||
_ = trailing
|
||||
|
||||
return tableName, fields, nil
|
||||
}
|
||||
|
||||
// ParseAlterTable parses MySQL ALTER TABLE statements.
|
||||
func (p *MySQLParser) ParseAlterTable(stmt string, tables map[string]map[string]*gdb.TableField) error {
|
||||
return parseAlterTableCommon(stmt, tables, p.parseColumnDef)
|
||||
}
|
||||
|
||||
// ParseComment handles MySQL-style comments (inline COMMENT keyword is handled in parseColumnDef).
|
||||
func (p *MySQLParser) ParseComment(stmt string, tables map[string]map[string]*gdb.TableField) {
|
||||
// MySQL uses inline COMMENT 'xxx' in column definitions,
|
||||
// which is already handled by parseColumnDef. No separate COMMENT ON statement.
|
||||
}
|
||||
|
||||
// parseColumnDef parses a single MySQL column definition string into a TableField.
|
||||
// It extracts the column name, data type (including UNSIGNED modifier), and delegates
|
||||
// attribute parsing (NULL, DEFAULT, PRIMARY KEY, COMMENT, etc.) to parseColumnAttributes.
|
||||
func (p *MySQLParser) parseColumnDef(def string, index int) (*gdb.TableField, error) {
|
||||
tokens := mysqlTokenize(def)
|
||||
if len(tokens) < 2 {
|
||||
return nil, fmt.Errorf("invalid column definition: %s", def)
|
||||
}
|
||||
|
||||
field := &gdb.TableField{
|
||||
Index: index,
|
||||
Name: unquoteIdentifier(tokens[0]),
|
||||
Null: true,
|
||||
}
|
||||
|
||||
typeStr := tokens[1]
|
||||
rest := ""
|
||||
if len(tokens) > 2 {
|
||||
rest = strings.Join(tokens[2:], " ")
|
||||
}
|
||||
|
||||
// Check if rest starts with '(' meaning the type params are in rest
|
||||
if !strings.Contains(typeStr, "(") && strings.HasPrefix(strings.TrimSpace(rest), "(") {
|
||||
endParen := strings.Index(rest, ")")
|
||||
if endParen >= 0 {
|
||||
typeStr += rest[:endParen+1]
|
||||
rest = strings.TrimSpace(rest[endParen+1:])
|
||||
}
|
||||
}
|
||||
|
||||
field.Type = typeStr
|
||||
|
||||
// Handle UNSIGNED
|
||||
upperRest := strings.ToUpper(rest)
|
||||
if strings.HasPrefix(upperRest, "UNSIGNED") {
|
||||
field.Type += " unsigned"
|
||||
rest = strings.TrimSpace(rest[8:])
|
||||
}
|
||||
|
||||
p.parseColumnAttributes(field, rest)
|
||||
|
||||
return field, nil
|
||||
}
|
||||
|
||||
// parseColumnAttributes parses MySQL column constraint keywords from the attribute string
|
||||
// following the column type. It handles NOT NULL, NULL, PRIMARY KEY, UNIQUE, AUTO_INCREMENT,
|
||||
// DEFAULT, COMMENT, and ON UPDATE clauses.
|
||||
func (p *MySQLParser) parseColumnAttributes(field *gdb.TableField, attrs string) {
|
||||
words := strings.Fields(attrs)
|
||||
upperWords := strings.Fields(strings.ToUpper(attrs))
|
||||
|
||||
for i := 0; i < len(upperWords); i++ {
|
||||
switch upperWords[i] {
|
||||
case "NOT":
|
||||
if i+1 < len(upperWords) && upperWords[i+1] == "NULL" {
|
||||
field.Null = false
|
||||
i++
|
||||
}
|
||||
case "NULL":
|
||||
field.Null = true
|
||||
case "PRIMARY":
|
||||
if i+1 < len(upperWords) && upperWords[i+1] == "KEY" {
|
||||
field.Key = "PRI"
|
||||
i++
|
||||
}
|
||||
case "UNIQUE":
|
||||
if field.Key == "" {
|
||||
field.Key = "UNI"
|
||||
}
|
||||
if i+1 < len(upperWords) && upperWords[i+1] == "KEY" {
|
||||
i++
|
||||
}
|
||||
case "KEY":
|
||||
if field.Key == "" {
|
||||
field.Key = "MUL"
|
||||
}
|
||||
case "AUTO_INCREMENT":
|
||||
field.Extra = "auto_increment"
|
||||
case "DEFAULT":
|
||||
if i+1 < len(words) {
|
||||
defaultVal, _ := extractDefaultValue("DEFAULT " + strings.Join(words[i+1:], " "))
|
||||
field.Default = defaultVal
|
||||
if defaultVal != nil {
|
||||
if strings.HasPrefix(words[i+1], "'") {
|
||||
for j := i + 1; j < len(words); j++ {
|
||||
if strings.HasSuffix(words[j], "'") {
|
||||
i = j
|
||||
break
|
||||
}
|
||||
}
|
||||
} else {
|
||||
i++
|
||||
}
|
||||
}
|
||||
}
|
||||
case "COMMENT":
|
||||
if i+1 < len(words) {
|
||||
comment := strings.Join(words[i+1:], " ")
|
||||
comment = strings.TrimSpace(comment)
|
||||
if len(comment) >= 2 && comment[0] == '\'' && comment[len(comment)-1] == '\'' {
|
||||
comment = comment[1 : len(comment)-1]
|
||||
comment = strings.ReplaceAll(comment, "''", "'")
|
||||
}
|
||||
field.Comment = comment
|
||||
return
|
||||
}
|
||||
case "ON":
|
||||
if i+1 < len(upperWords) && upperWords[i+1] == "UPDATE" {
|
||||
if i+2 < len(upperWords) {
|
||||
if field.Extra != "" {
|
||||
field.Extra += ", "
|
||||
}
|
||||
field.Extra += "on update " + strings.ToLower(words[i+2])
|
||||
i += 2
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,300 +0,0 @@
|
||||
// Copyright GoFrame gf 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 gendao
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
)
|
||||
|
||||
func Test_MySQL_CreateTable_Basic(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
parser := &MySQLParser{}
|
||||
sql := `
|
||||
CREATE TABLE users (
|
||||
id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'User ID',
|
||||
name VARCHAR(100) NOT NULL DEFAULT '' COMMENT 'User name',
|
||||
email VARCHAR(200) NULL COMMENT 'Email address',
|
||||
age INT(11) DEFAULT 0,
|
||||
score DECIMAL(10,2) DEFAULT 0.00,
|
||||
status TINYINT(1) NOT NULL DEFAULT 1,
|
||||
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at DATETIME NULL ON UPDATE CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (id),
|
||||
UNIQUE KEY uk_email (email)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='User table';
|
||||
`
|
||||
tables := make(map[string]map[string]*gdb.TableField)
|
||||
err := processSQL(parser, sql, tables)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(tables), 1)
|
||||
|
||||
fields := tables["users"]
|
||||
t.Assert(len(fields), 8)
|
||||
|
||||
// Check id field
|
||||
t.Assert(fields["id"].Name, "id")
|
||||
t.Assert(fields["id"].Type, "BIGINT(20) unsigned")
|
||||
t.Assert(fields["id"].Null, false)
|
||||
t.Assert(fields["id"].Key, "PRI")
|
||||
t.Assert(fields["id"].Extra, "auto_increment")
|
||||
t.Assert(fields["id"].Comment, "User ID")
|
||||
t.Assert(fields["id"].Index, 0)
|
||||
|
||||
// Check name field
|
||||
t.Assert(fields["name"].Name, "name")
|
||||
t.Assert(fields["name"].Null, false)
|
||||
t.Assert(fields["name"].Comment, "User name")
|
||||
|
||||
// Check email field
|
||||
t.Assert(fields["email"].Null, true)
|
||||
|
||||
// Check created_at
|
||||
t.Assert(fields["created_at"].Null, false)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_MySQL_AlterTable_AddColumn(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
parser := &MySQLParser{}
|
||||
sql := `
|
||||
CREATE TABLE users (
|
||||
id INT NOT NULL AUTO_INCREMENT,
|
||||
name VARCHAR(100) NOT NULL,
|
||||
PRIMARY KEY (id)
|
||||
);
|
||||
ALTER TABLE users ADD COLUMN email VARCHAR(200) NULL COMMENT 'Email';
|
||||
ALTER TABLE users ADD COLUMN age INT DEFAULT 0;
|
||||
`
|
||||
tables := make(map[string]map[string]*gdb.TableField)
|
||||
err := processSQL(parser, sql, tables)
|
||||
t.AssertNil(err)
|
||||
|
||||
fields := tables["users"]
|
||||
t.Assert(len(fields), 4)
|
||||
t.Assert(fields["email"].Name, "email")
|
||||
t.Assert(fields["email"].Null, true)
|
||||
t.Assert(fields["email"].Comment, "Email")
|
||||
t.Assert(fields["age"].Name, "age")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_MySQL_AlterTable_DropColumn(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
parser := &MySQLParser{}
|
||||
sql := `
|
||||
CREATE TABLE users (
|
||||
id INT NOT NULL AUTO_INCREMENT,
|
||||
name VARCHAR(100),
|
||||
old_field VARCHAR(50),
|
||||
PRIMARY KEY (id)
|
||||
);
|
||||
ALTER TABLE users DROP COLUMN old_field;
|
||||
`
|
||||
tables := make(map[string]map[string]*gdb.TableField)
|
||||
err := processSQL(parser, sql, tables)
|
||||
t.AssertNil(err)
|
||||
|
||||
fields := tables["users"]
|
||||
t.Assert(len(fields), 2)
|
||||
_, ok := fields["old_field"]
|
||||
t.Assert(ok, false)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_MySQL_AlterTable_ModifyColumn(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
parser := &MySQLParser{}
|
||||
sql := `
|
||||
CREATE TABLE users (
|
||||
id INT NOT NULL AUTO_INCREMENT,
|
||||
name VARCHAR(100),
|
||||
PRIMARY KEY (id)
|
||||
);
|
||||
ALTER TABLE users MODIFY COLUMN name VARCHAR(200) NOT NULL COMMENT 'Full name';
|
||||
`
|
||||
tables := make(map[string]map[string]*gdb.TableField)
|
||||
err := processSQL(parser, sql, tables)
|
||||
t.AssertNil(err)
|
||||
|
||||
fields := tables["users"]
|
||||
t.Assert(fields["name"].Type, "VARCHAR(200)")
|
||||
t.Assert(fields["name"].Null, false)
|
||||
t.Assert(fields["name"].Comment, "Full name")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_MySQL_AlterTable_ChangeColumn(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
parser := &MySQLParser{}
|
||||
sql := `
|
||||
CREATE TABLE users (
|
||||
id INT NOT NULL AUTO_INCREMENT,
|
||||
old_name VARCHAR(100),
|
||||
PRIMARY KEY (id)
|
||||
);
|
||||
ALTER TABLE users CHANGE COLUMN old_name new_name VARCHAR(200) NOT NULL;
|
||||
`
|
||||
tables := make(map[string]map[string]*gdb.TableField)
|
||||
err := processSQL(parser, sql, tables)
|
||||
t.AssertNil(err)
|
||||
|
||||
fields := tables["users"]
|
||||
_, ok := fields["old_name"]
|
||||
t.Assert(ok, false)
|
||||
t.Assert(fields["new_name"].Name, "new_name")
|
||||
t.Assert(fields["new_name"].Type, "VARCHAR(200)")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_MySQL_AlterTable_AddPrimaryKey(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
parser := &MySQLParser{}
|
||||
sql := `
|
||||
CREATE TABLE users (
|
||||
id INT NOT NULL,
|
||||
name VARCHAR(100)
|
||||
);
|
||||
ALTER TABLE users ADD PRIMARY KEY (id);
|
||||
`
|
||||
tables := make(map[string]map[string]*gdb.TableField)
|
||||
err := processSQL(parser, sql, tables)
|
||||
t.AssertNil(err)
|
||||
|
||||
t.Assert(tables["users"]["id"].Key, "PRI")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_MySQL_DropTable(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
parser := &MySQLParser{}
|
||||
sql := `
|
||||
CREATE TABLE temp_log (id INT, msg TEXT);
|
||||
CREATE TABLE users (id INT, name VARCHAR(100));
|
||||
DROP TABLE IF EXISTS temp_log;
|
||||
`
|
||||
tables := make(map[string]map[string]*gdb.TableField)
|
||||
err := processSQL(parser, sql, tables)
|
||||
t.AssertNil(err)
|
||||
|
||||
t.Assert(len(tables), 1)
|
||||
_, ok := tables["temp_log"]
|
||||
t.Assert(ok, false)
|
||||
_, ok = tables["users"]
|
||||
t.Assert(ok, true)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_MySQL_MultipleMigrations(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
parser := &MySQLParser{}
|
||||
|
||||
// Simulate V1: initial schema
|
||||
tables := make(map[string]map[string]*gdb.TableField)
|
||||
err := processSQL(parser, `
|
||||
CREATE TABLE users (
|
||||
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
|
||||
name VARCHAR(50) NOT NULL,
|
||||
PRIMARY KEY (id)
|
||||
);
|
||||
`, tables)
|
||||
t.AssertNil(err)
|
||||
|
||||
// Simulate V2: add columns
|
||||
err = processSQL(parser, `
|
||||
ALTER TABLE users ADD COLUMN email VARCHAR(200) NULL;
|
||||
ALTER TABLE users ADD COLUMN phone VARCHAR(20) NULL;
|
||||
`, tables)
|
||||
t.AssertNil(err)
|
||||
|
||||
// Simulate V3: modify + drop
|
||||
err = processSQL(parser, `
|
||||
ALTER TABLE users MODIFY COLUMN name VARCHAR(100) NOT NULL COMMENT 'Full name';
|
||||
ALTER TABLE users DROP COLUMN phone;
|
||||
`, tables)
|
||||
t.AssertNil(err)
|
||||
|
||||
fields := tables["users"]
|
||||
t.Assert(len(fields), 3) // id, name, email
|
||||
t.Assert(fields["name"].Type, "VARCHAR(100)")
|
||||
t.Assert(fields["name"].Comment, "Full name")
|
||||
_, ok := fields["phone"]
|
||||
t.Assert(ok, false)
|
||||
t.Assert(fields["email"].Null, true)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_MySQL_FullMigrationScenario(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
parser := &MySQLParser{}
|
||||
tables := make(map[string]map[string]*gdb.TableField)
|
||||
|
||||
// V001: Initial tables
|
||||
err := processSQL(parser, `
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'Primary key',
|
||||
username VARCHAR(50) NOT NULL COMMENT 'Username',
|
||||
password VARCHAR(128) NOT NULL COMMENT 'Hashed password',
|
||||
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (id),
|
||||
UNIQUE KEY uk_username (username)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS orders (
|
||||
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
|
||||
user_id BIGINT UNSIGNED NOT NULL,
|
||||
amount DECIMAL(10,2) NOT NULL DEFAULT 0.00,
|
||||
PRIMARY KEY (id)
|
||||
);
|
||||
`, tables)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(tables), 2)
|
||||
|
||||
// V002: Add email, phone
|
||||
err = processSQL(parser, `
|
||||
ALTER TABLE users ADD COLUMN email VARCHAR(200) NULL COMMENT 'User email';
|
||||
ALTER TABLE users ADD COLUMN phone VARCHAR(20) NULL COMMENT 'Phone number';
|
||||
`, tables)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(tables["users"]), 6)
|
||||
|
||||
// V003: Modify, rename, drop
|
||||
err = processSQL(parser, `
|
||||
ALTER TABLE users MODIFY COLUMN username VARCHAR(100) NOT NULL COMMENT 'Login name';
|
||||
ALTER TABLE users CHANGE COLUMN phone mobile VARCHAR(20) NULL COMMENT 'Mobile number';
|
||||
ALTER TABLE users DROP COLUMN password;
|
||||
ALTER TABLE orders ADD COLUMN status TINYINT(1) NOT NULL DEFAULT 0 COMMENT 'Order status';
|
||||
`, tables)
|
||||
t.AssertNil(err)
|
||||
|
||||
userFields := tables["users"]
|
||||
t.Assert(len(userFields), 5) // id, username, email, mobile, created_at
|
||||
t.Assert(userFields["username"].Type, "VARCHAR(100)")
|
||||
t.Assert(userFields["username"].Comment, "Login name")
|
||||
_, ok := userFields["password"]
|
||||
t.Assert(ok, false)
|
||||
_, ok = userFields["phone"]
|
||||
t.Assert(ok, false)
|
||||
t.Assert(userFields["mobile"].Name, "mobile")
|
||||
t.Assert(userFields["mobile"].Comment, "Mobile number")
|
||||
|
||||
orderFields := tables["orders"]
|
||||
t.Assert(len(orderFields), 4)
|
||||
t.Assert(orderFields["status"].Default, "0")
|
||||
|
||||
// V004: Drop table
|
||||
err = processSQL(parser, `
|
||||
DROP TABLE IF EXISTS orders;
|
||||
`, tables)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(tables), 1)
|
||||
_, ok = tables["orders"]
|
||||
t.Assert(ok, false)
|
||||
})
|
||||
}
|
||||
@ -1,209 +0,0 @@
|
||||
// Copyright GoFrame gf 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 gendao
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
)
|
||||
|
||||
// OracleParser implements SQLParser for Oracle/DM DDL.
|
||||
type OracleParser struct{}
|
||||
|
||||
// ParseCreateTable parses a single Oracle CREATE TABLE statement.
|
||||
func (p *OracleParser) ParseCreateTable(stmt string) (string, map[string]*gdb.TableField, error) {
|
||||
body, _, ok := extractBodyAndTrailing(stmt)
|
||||
if !ok {
|
||||
return "", nil, nil
|
||||
}
|
||||
|
||||
parenIdx := strings.Index(stmt, "(")
|
||||
header := stmt[:parenIdx]
|
||||
tableName := extractTableName(header)
|
||||
if tableName == "" {
|
||||
return "", nil, fmt.Errorf("cannot extract table name from: %s", header)
|
||||
}
|
||||
|
||||
columnDefs := splitColumns(body)
|
||||
fields := make(map[string]*gdb.TableField)
|
||||
pkColumns := findPrimaryKeysFromConstraints(columnDefs)
|
||||
|
||||
fieldIndex := 0
|
||||
for _, def := range columnDefs {
|
||||
def = strings.TrimSpace(def)
|
||||
if def == "" {
|
||||
continue
|
||||
}
|
||||
firstWord := strings.ToUpper(strings.Fields(def)[0])
|
||||
if isConstraintKeyword(firstWord) {
|
||||
continue
|
||||
}
|
||||
|
||||
field, err := p.parseColumnDef(def, fieldIndex)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if field != nil {
|
||||
fields[field.Name] = field
|
||||
fieldIndex++
|
||||
}
|
||||
}
|
||||
|
||||
for _, pkCol := range pkColumns {
|
||||
if f, ok := fields[pkCol]; ok {
|
||||
f.Key = "PRI"
|
||||
}
|
||||
upperPk := strings.ToUpper(pkCol)
|
||||
if f, ok := fields[upperPk]; ok {
|
||||
f.Key = "PRI"
|
||||
}
|
||||
}
|
||||
|
||||
return tableName, fields, nil
|
||||
}
|
||||
|
||||
// ParseAlterTable parses Oracle ALTER TABLE statements.
|
||||
func (p *OracleParser) ParseAlterTable(stmt string, tables map[string]map[string]*gdb.TableField) error {
|
||||
return parseAlterTableCommon(stmt, tables, p.parseColumnDef)
|
||||
}
|
||||
|
||||
// ParseComment parses COMMENT ON COLUMN table.column IS 'comment'.
|
||||
func (p *OracleParser) ParseComment(stmt string, tables map[string]map[string]*gdb.TableField) {
|
||||
upper := strings.ToUpper(strings.TrimSpace(stmt))
|
||||
if !strings.HasPrefix(upper, "COMMENT ON COLUMN") {
|
||||
return
|
||||
}
|
||||
|
||||
rest := strings.TrimSpace(stmt[len("COMMENT ON COLUMN"):])
|
||||
isIdx := strings.Index(strings.ToUpper(rest), " IS ")
|
||||
if isIdx < 0 {
|
||||
return
|
||||
}
|
||||
ref := strings.TrimSpace(rest[:isIdx])
|
||||
comment := strings.TrimSpace(rest[isIdx+4:])
|
||||
|
||||
if len(comment) >= 2 && comment[0] == '\'' && comment[len(comment)-1] == '\'' {
|
||||
comment = comment[1 : len(comment)-1]
|
||||
comment = strings.ReplaceAll(comment, "''", "'")
|
||||
}
|
||||
|
||||
parts := strings.Split(ref, ".")
|
||||
var tableName, columnName string
|
||||
switch len(parts) {
|
||||
case 2:
|
||||
tableName = unquoteIdentifier(parts[0])
|
||||
columnName = unquoteIdentifier(parts[1])
|
||||
case 3:
|
||||
tableName = unquoteIdentifier(parts[1])
|
||||
columnName = unquoteIdentifier(parts[2])
|
||||
default:
|
||||
return
|
||||
}
|
||||
|
||||
if fields, ok := tables[tableName]; ok {
|
||||
if field, ok := fields[columnName]; ok {
|
||||
field.Comment = comment
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// parseColumnDef parses a single Oracle column definition string into a TableField.
|
||||
// It handles Oracle-specific types including TIMESTAMP WITH TIME ZONE and
|
||||
// TIMESTAMP WITH LOCAL TIME ZONE.
|
||||
func (p *OracleParser) parseColumnDef(def string, index int) (*gdb.TableField, error) {
|
||||
tokens := mysqlTokenize(def)
|
||||
if len(tokens) < 2 {
|
||||
return nil, fmt.Errorf("invalid column definition: %s", def)
|
||||
}
|
||||
|
||||
field := &gdb.TableField{
|
||||
Index: index,
|
||||
Name: unquoteIdentifier(tokens[0]),
|
||||
Null: true,
|
||||
}
|
||||
|
||||
field.Type = tokens[1]
|
||||
|
||||
rest := ""
|
||||
if len(tokens) > 2 {
|
||||
rest = strings.Join(tokens[2:], " ")
|
||||
}
|
||||
|
||||
if !strings.Contains(field.Type, "(") && strings.HasPrefix(strings.TrimSpace(rest), "(") {
|
||||
end := strings.Index(rest, ")")
|
||||
if end >= 0 {
|
||||
field.Type += rest[:end+1]
|
||||
rest = strings.TrimSpace(rest[end+1:])
|
||||
}
|
||||
}
|
||||
|
||||
// Handle TIMESTAMP WITH TIME ZONE / WITH LOCAL TIME ZONE
|
||||
upperType := strings.ToUpper(field.Type)
|
||||
upperRest := strings.ToUpper(rest)
|
||||
if upperType == "TIMESTAMP" {
|
||||
if strings.HasPrefix(upperRest, "WITH LOCAL TIME ZONE") {
|
||||
field.Type = "timestamp with local time zone"
|
||||
rest = strings.TrimSpace(rest[len("WITH LOCAL TIME ZONE"):])
|
||||
} else if strings.HasPrefix(upperRest, "WITH TIME ZONE") {
|
||||
field.Type = "timestamp with time zone"
|
||||
rest = strings.TrimSpace(rest[len("WITH TIME ZONE"):])
|
||||
}
|
||||
}
|
||||
|
||||
p.parseColumnAttributes(field, rest)
|
||||
|
||||
return field, nil
|
||||
}
|
||||
|
||||
// parseColumnAttributes parses Oracle column constraint keywords including
|
||||
// NOT NULL, NULL, PRIMARY KEY, UNIQUE, DEFAULT, and GENERATED ... AS IDENTITY.
|
||||
func (p *OracleParser) parseColumnAttributes(field *gdb.TableField, attrs string) {
|
||||
words := strings.Fields(attrs)
|
||||
upperWords := strings.Fields(strings.ToUpper(attrs))
|
||||
|
||||
for i := 0; i < len(upperWords); i++ {
|
||||
switch upperWords[i] {
|
||||
case "NOT":
|
||||
if i+1 < len(upperWords) && upperWords[i+1] == "NULL" {
|
||||
field.Null = false
|
||||
i++
|
||||
}
|
||||
case "NULL":
|
||||
field.Null = true
|
||||
case "PRIMARY":
|
||||
if i+1 < len(upperWords) && upperWords[i+1] == "KEY" {
|
||||
field.Key = "PRI"
|
||||
i++
|
||||
}
|
||||
case "UNIQUE":
|
||||
if field.Key == "" {
|
||||
field.Key = "UNI"
|
||||
}
|
||||
case "DEFAULT":
|
||||
if i+1 < len(words) {
|
||||
defaultVal, _ := extractDefaultValue("DEFAULT " + strings.Join(words[i+1:], " "))
|
||||
field.Default = defaultVal
|
||||
if defaultVal != nil {
|
||||
i++
|
||||
}
|
||||
}
|
||||
case "GENERATED":
|
||||
rest := strings.Join(upperWords[i:], " ")
|
||||
if strings.Contains(rest, "AS IDENTITY") {
|
||||
field.Extra = "auto_increment"
|
||||
for j := i + 1; j < len(upperWords); j++ {
|
||||
if upperWords[j] == "IDENTITY" {
|
||||
i = j
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,97 +0,0 @@
|
||||
// Copyright GoFrame gf 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 gendao
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
)
|
||||
|
||||
func Test_Oracle_CreateTable_Basic(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
parser := &OracleParser{}
|
||||
sql := `
|
||||
CREATE TABLE users (
|
||||
ID NUMBER(10) NOT NULL,
|
||||
NAME VARCHAR2(100) NOT NULL,
|
||||
EMAIL VARCHAR2(200),
|
||||
CREATED_AT TIMESTAMP WITH TIME ZONE DEFAULT SYSTIMESTAMP,
|
||||
CONSTRAINT PK_USERS PRIMARY KEY (ID)
|
||||
);
|
||||
COMMENT ON COLUMN users.ID IS 'User ID';
|
||||
COMMENT ON COLUMN users.NAME IS 'User name';
|
||||
`
|
||||
tables := make(map[string]map[string]*gdb.TableField)
|
||||
err := processSQL(parser, sql, tables)
|
||||
t.AssertNil(err)
|
||||
|
||||
fields := tables["users"]
|
||||
t.Assert(len(fields), 4)
|
||||
|
||||
t.Assert(fields["ID"].Key, "PRI")
|
||||
t.Assert(fields["ID"].Null, false)
|
||||
t.Assert(fields["ID"].Comment, "User ID")
|
||||
|
||||
t.Assert(fields["NAME"].Null, false)
|
||||
t.Assert(fields["NAME"].Comment, "User name")
|
||||
|
||||
t.Assert(fields["CREATED_AT"].Type, "timestamp with time zone")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Oracle_AlterTable(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
parser := &OracleParser{}
|
||||
sql := `
|
||||
CREATE TABLE users (
|
||||
ID NUMBER(10) NOT NULL,
|
||||
NAME VARCHAR2(100),
|
||||
CONSTRAINT PK_USERS PRIMARY KEY (ID)
|
||||
);
|
||||
ALTER TABLE users ADD EMAIL VARCHAR2(200);
|
||||
ALTER TABLE users MODIFY NAME VARCHAR2(200) NOT NULL;
|
||||
COMMENT ON COLUMN users.EMAIL IS 'Email address';
|
||||
`
|
||||
tables := make(map[string]map[string]*gdb.TableField)
|
||||
err := processSQL(parser, sql, tables)
|
||||
t.AssertNil(err)
|
||||
|
||||
fields := tables["users"]
|
||||
t.Assert(len(fields), 3)
|
||||
t.Assert(fields["EMAIL"].Comment, "Email address")
|
||||
t.Assert(fields["NAME"].Type, "VARCHAR2(200)")
|
||||
t.Assert(fields["NAME"].Null, false)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Oracle_AlterTable_DropColumn(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
parser := &OracleParser{}
|
||||
sql := `
|
||||
CREATE TABLE users (
|
||||
ID NUMBER(10) NOT NULL,
|
||||
NAME VARCHAR2(100) NOT NULL,
|
||||
OLD_COL VARCHAR2(50),
|
||||
EMAIL VARCHAR2(200),
|
||||
CONSTRAINT PK_USERS PRIMARY KEY (ID)
|
||||
);
|
||||
ALTER TABLE users DROP COLUMN OLD_COL;
|
||||
`
|
||||
tables := make(map[string]map[string]*gdb.TableField)
|
||||
err := processSQL(parser, sql, tables)
|
||||
t.AssertNil(err)
|
||||
|
||||
fields := tables["users"]
|
||||
t.Assert(len(fields), 3)
|
||||
_, ok := fields["OLD_COL"]
|
||||
t.Assert(ok, false)
|
||||
t.Assert(fields["NAME"].Name, "NAME")
|
||||
t.Assert(fields["EMAIL"].Name, "EMAIL")
|
||||
})
|
||||
}
|
||||
@ -1,268 +0,0 @@
|
||||
// Copyright GoFrame gf 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 gendao
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
)
|
||||
|
||||
// PgSQLParser implements SQLParser for PostgreSQL DDL.
|
||||
type PgSQLParser struct{}
|
||||
|
||||
// ParseCreateTable parses a single PostgreSQL CREATE TABLE statement.
|
||||
func (p *PgSQLParser) ParseCreateTable(stmt string) (string, map[string]*gdb.TableField, error) {
|
||||
body, _, ok := extractBodyAndTrailing(stmt)
|
||||
if !ok {
|
||||
return "", nil, nil
|
||||
}
|
||||
|
||||
parenIdx := strings.Index(stmt, "(")
|
||||
header := stmt[:parenIdx]
|
||||
tableName := extractTableName(header)
|
||||
if tableName == "" {
|
||||
return "", nil, fmt.Errorf("cannot extract table name from: %s", header)
|
||||
}
|
||||
|
||||
columnDefs := splitColumns(body)
|
||||
fields := make(map[string]*gdb.TableField)
|
||||
pkColumns := findPrimaryKeysFromConstraints(columnDefs)
|
||||
|
||||
fieldIndex := 0
|
||||
for _, def := range columnDefs {
|
||||
def = strings.TrimSpace(def)
|
||||
if def == "" {
|
||||
continue
|
||||
}
|
||||
firstWord := strings.ToUpper(strings.Fields(def)[0])
|
||||
if isConstraintKeyword(firstWord) {
|
||||
continue
|
||||
}
|
||||
|
||||
field, err := p.parseColumnDef(def, fieldIndex)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if field != nil {
|
||||
fields[field.Name] = field
|
||||
fieldIndex++
|
||||
}
|
||||
}
|
||||
|
||||
for _, pkCol := range pkColumns {
|
||||
if f, ok := fields[pkCol]; ok {
|
||||
f.Key = "PRI"
|
||||
}
|
||||
}
|
||||
|
||||
return tableName, fields, nil
|
||||
}
|
||||
|
||||
// ParseAlterTable parses PostgreSQL ALTER TABLE statements.
|
||||
func (p *PgSQLParser) ParseAlterTable(stmt string, tables map[string]map[string]*gdb.TableField) error {
|
||||
return parseAlterTableCommon(stmt, tables, p.parseColumnDef)
|
||||
}
|
||||
|
||||
// ParseComment parses COMMENT ON COLUMN schema.table.column IS 'comment' statements.
|
||||
func (p *PgSQLParser) ParseComment(stmt string, tables map[string]map[string]*gdb.TableField) {
|
||||
upper := strings.ToUpper(strings.TrimSpace(stmt))
|
||||
if !strings.HasPrefix(upper, "COMMENT ON COLUMN") {
|
||||
return
|
||||
}
|
||||
|
||||
rest := strings.TrimSpace(stmt[len("COMMENT ON COLUMN"):])
|
||||
isIdx := strings.Index(strings.ToUpper(rest), " IS ")
|
||||
if isIdx < 0 {
|
||||
return
|
||||
}
|
||||
ref := strings.TrimSpace(rest[:isIdx])
|
||||
comment := strings.TrimSpace(rest[isIdx+4:])
|
||||
|
||||
if len(comment) >= 2 && comment[0] == '\'' && comment[len(comment)-1] == '\'' {
|
||||
comment = comment[1 : len(comment)-1]
|
||||
comment = strings.ReplaceAll(comment, "''", "'")
|
||||
}
|
||||
|
||||
parts := strings.Split(ref, ".")
|
||||
var tableName, columnName string
|
||||
switch len(parts) {
|
||||
case 2:
|
||||
tableName = unquoteIdentifier(parts[0])
|
||||
columnName = unquoteIdentifier(parts[1])
|
||||
case 3:
|
||||
tableName = unquoteIdentifier(parts[1])
|
||||
columnName = unquoteIdentifier(parts[2])
|
||||
default:
|
||||
return
|
||||
}
|
||||
|
||||
if fields, ok := tables[tableName]; ok {
|
||||
if field, ok := fields[columnName]; ok {
|
||||
field.Comment = comment
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// parseColumnDef parses a single PostgreSQL column definition string into a TableField.
|
||||
// It handles PostgreSQL-specific types like SERIAL/BIGSERIAL (auto-increment shorthand),
|
||||
// CHARACTER VARYING, DOUBLE PRECISION, TIMESTAMP WITH TIME ZONE, and array types.
|
||||
func (p *PgSQLParser) parseColumnDef(def string, index int) (*gdb.TableField, error) {
|
||||
tokens := mysqlTokenize(def)
|
||||
if len(tokens) < 2 {
|
||||
return nil, fmt.Errorf("invalid column definition: %s", def)
|
||||
}
|
||||
|
||||
field := &gdb.TableField{
|
||||
Index: index,
|
||||
Name: unquoteIdentifier(tokens[0]),
|
||||
Null: true,
|
||||
}
|
||||
|
||||
// Handle SERIAL types
|
||||
typeToken := strings.ToUpper(tokens[1])
|
||||
switch typeToken {
|
||||
case "SERIAL":
|
||||
field.Type = "int"
|
||||
field.Extra = "auto_increment"
|
||||
field.Null = false
|
||||
case "BIGSERIAL":
|
||||
field.Type = "bigint"
|
||||
field.Extra = "auto_increment"
|
||||
field.Null = false
|
||||
case "SMALLSERIAL":
|
||||
field.Type = "smallint"
|
||||
field.Extra = "auto_increment"
|
||||
field.Null = false
|
||||
default:
|
||||
field.Type = tokens[1]
|
||||
}
|
||||
|
||||
rest := ""
|
||||
if len(tokens) > 2 {
|
||||
rest = strings.Join(tokens[2:], " ")
|
||||
}
|
||||
upperType := strings.ToUpper(field.Type)
|
||||
upperRest := strings.ToUpper(rest)
|
||||
|
||||
switch {
|
||||
case upperType == "CHARACTER" && strings.HasPrefix(upperRest, "VARYING"):
|
||||
rest = strings.TrimSpace(rest[len("VARYING"):])
|
||||
if strings.HasPrefix(rest, "(") {
|
||||
end := strings.Index(rest, ")")
|
||||
if end >= 0 {
|
||||
field.Type = "character varying" + rest[:end+1]
|
||||
rest = strings.TrimSpace(rest[end+1:])
|
||||
}
|
||||
} else {
|
||||
field.Type = "character varying"
|
||||
}
|
||||
case upperType == "DOUBLE" && strings.HasPrefix(upperRest, "PRECISION"):
|
||||
field.Type = "double precision"
|
||||
rest = strings.TrimSpace(rest[len("PRECISION"):])
|
||||
case (upperType == "TIMESTAMP" || upperType == "TIME") &&
|
||||
(strings.HasPrefix(upperRest, "WITH TIME ZONE") || strings.HasPrefix(upperRest, "WITHOUT TIME ZONE")):
|
||||
if strings.HasPrefix(upperRest, "WITH TIME ZONE") {
|
||||
if upperType == "TIMESTAMP" {
|
||||
field.Type = "timestamptz"
|
||||
} else {
|
||||
field.Type = "time with time zone"
|
||||
}
|
||||
rest = strings.TrimSpace(rest[len("WITH TIME ZONE"):])
|
||||
} else {
|
||||
field.Type = strings.ToLower(upperType)
|
||||
rest = strings.TrimSpace(rest[len("WITHOUT TIME ZONE"):])
|
||||
}
|
||||
case !strings.Contains(field.Type, "(") && strings.HasPrefix(strings.TrimSpace(rest), "("):
|
||||
end := strings.Index(rest, ")")
|
||||
if end >= 0 {
|
||||
field.Type += rest[:end+1]
|
||||
rest = strings.TrimSpace(rest[end+1:])
|
||||
}
|
||||
}
|
||||
|
||||
// Handle array types
|
||||
if strings.HasPrefix(rest, "[]") {
|
||||
field.Type += "[]"
|
||||
rest = strings.TrimSpace(rest[2:])
|
||||
} else if strings.HasPrefix(strings.ToUpper(rest), "ARRAY") {
|
||||
field.Type += "[]"
|
||||
rest = strings.TrimSpace(rest[5:])
|
||||
}
|
||||
|
||||
p.parseColumnAttributes(field, rest)
|
||||
|
||||
return field, nil
|
||||
}
|
||||
|
||||
// parseColumnAttributes parses PostgreSQL column constraint keywords including
|
||||
// NOT NULL, NULL, PRIMARY KEY, UNIQUE, DEFAULT, GENERATED ... AS IDENTITY, and REFERENCES.
|
||||
func (p *PgSQLParser) parseColumnAttributes(field *gdb.TableField, attrs string) {
|
||||
words := strings.Fields(attrs)
|
||||
upperWords := strings.Fields(strings.ToUpper(attrs))
|
||||
|
||||
for i := 0; i < len(upperWords); i++ {
|
||||
switch upperWords[i] {
|
||||
case "NOT":
|
||||
if i+1 < len(upperWords) && upperWords[i+1] == "NULL" {
|
||||
field.Null = false
|
||||
i++
|
||||
}
|
||||
case "NULL":
|
||||
field.Null = true
|
||||
case "PRIMARY":
|
||||
if i+1 < len(upperWords) && upperWords[i+1] == "KEY" {
|
||||
field.Key = "PRI"
|
||||
i++
|
||||
}
|
||||
case "UNIQUE":
|
||||
if field.Key == "" {
|
||||
field.Key = "UNI"
|
||||
}
|
||||
case "DEFAULT":
|
||||
if i+1 < len(words) {
|
||||
defaultVal, _ := extractDefaultValue("DEFAULT " + strings.Join(words[i+1:], " "))
|
||||
field.Default = defaultVal
|
||||
if defaultVal != nil {
|
||||
i++
|
||||
}
|
||||
}
|
||||
case "GENERATED":
|
||||
if containsSequence(upperWords[i:], "ALWAYS", "AS", "IDENTITY") ||
|
||||
containsSequence(upperWords[i:], "BY", "DEFAULT", "AS", "IDENTITY") {
|
||||
field.Extra = "auto_increment"
|
||||
for j := i + 1; j < len(upperWords); j++ {
|
||||
if upperWords[j] == "IDENTITY" {
|
||||
i = j
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
case "REFERENCES":
|
||||
for j := i + 1; j < len(upperWords); j++ {
|
||||
i = j
|
||||
if strings.Contains(words[j], ")") {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// containsSequence checks if words slice contains the given word sequence starting from index 1.
|
||||
func containsSequence(words []string, seq ...string) bool {
|
||||
if len(words) < len(seq)+1 {
|
||||
return false
|
||||
}
|
||||
for i, s := range seq {
|
||||
if words[i+1] != s {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
@ -1,232 +0,0 @@
|
||||
// Copyright GoFrame gf 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 gendao
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
)
|
||||
|
||||
func Test_PgSQL_CreateTable_Basic(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
parser := &PgSQLParser{}
|
||||
sql := `
|
||||
CREATE TABLE users (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
name VARCHAR(100) NOT NULL,
|
||||
email CHARACTER VARYING(200),
|
||||
score DOUBLE PRECISION DEFAULT 0.0,
|
||||
metadata JSONB,
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
|
||||
is_active BOOLEAN NOT NULL DEFAULT TRUE
|
||||
);
|
||||
COMMENT ON COLUMN users.name IS 'User full name';
|
||||
COMMENT ON COLUMN users.email IS 'Email address';
|
||||
`
|
||||
tables := make(map[string]map[string]*gdb.TableField)
|
||||
err := processSQL(parser, sql, tables)
|
||||
t.AssertNil(err)
|
||||
|
||||
fields := tables["users"]
|
||||
t.Assert(len(fields), 7)
|
||||
|
||||
// BIGSERIAL should be auto_increment bigint
|
||||
t.Assert(fields["id"].Type, "bigint")
|
||||
t.Assert(fields["id"].Extra, "auto_increment")
|
||||
t.Assert(fields["id"].Key, "PRI")
|
||||
|
||||
// CHARACTER VARYING
|
||||
t.AssertNE(fields["email"], nil)
|
||||
|
||||
// DOUBLE PRECISION
|
||||
t.Assert(fields["score"].Type, "double precision")
|
||||
|
||||
// JSONB
|
||||
t.Assert(fields["metadata"].Type, "JSONB")
|
||||
|
||||
// TIMESTAMP WITH TIME ZONE
|
||||
t.Assert(fields["created_at"].Type, "timestamptz")
|
||||
|
||||
// COMMENT ON COLUMN
|
||||
t.Assert(fields["name"].Comment, "User full name")
|
||||
t.Assert(fields["email"].Comment, "Email address")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_PgSQL_AlterTable_AddColumn(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
parser := &PgSQLParser{}
|
||||
sql := `
|
||||
CREATE TABLE users (
|
||||
id SERIAL PRIMARY KEY,
|
||||
name VARCHAR(100) NOT NULL
|
||||
);
|
||||
ALTER TABLE users ADD COLUMN email VARCHAR(200);
|
||||
COMMENT ON COLUMN users.email IS 'User email';
|
||||
`
|
||||
tables := make(map[string]map[string]*gdb.TableField)
|
||||
err := processSQL(parser, sql, tables)
|
||||
t.AssertNil(err)
|
||||
|
||||
fields := tables["users"]
|
||||
t.Assert(len(fields), 3)
|
||||
t.Assert(fields["email"].Name, "email")
|
||||
t.Assert(fields["email"].Comment, "User email")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_PgSQL_AlterTable_AlterColumnType(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
parser := &PgSQLParser{}
|
||||
sql := `
|
||||
CREATE TABLE users (
|
||||
id SERIAL PRIMARY KEY,
|
||||
name VARCHAR(100)
|
||||
);
|
||||
ALTER TABLE users ALTER COLUMN name TYPE VARCHAR(200);
|
||||
ALTER TABLE users ALTER COLUMN name SET NOT NULL;
|
||||
`
|
||||
tables := make(map[string]map[string]*gdb.TableField)
|
||||
err := processSQL(parser, sql, tables)
|
||||
t.AssertNil(err)
|
||||
|
||||
fields := tables["users"]
|
||||
t.Assert(fields["name"].Type, "VARCHAR(200)")
|
||||
t.Assert(fields["name"].Null, false)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_PgSQL_AlterTable_DropColumn(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
parser := &PgSQLParser{}
|
||||
sql := `
|
||||
CREATE TABLE users (
|
||||
id SERIAL PRIMARY KEY,
|
||||
name VARCHAR(100),
|
||||
old_col TEXT
|
||||
);
|
||||
ALTER TABLE users DROP COLUMN old_col;
|
||||
`
|
||||
tables := make(map[string]map[string]*gdb.TableField)
|
||||
err := processSQL(parser, sql, tables)
|
||||
t.AssertNil(err)
|
||||
|
||||
fields := tables["users"]
|
||||
t.Assert(len(fields), 2)
|
||||
_, ok := fields["old_col"]
|
||||
t.Assert(ok, false)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_PgSQL_AlterTable_RenameColumn(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
parser := &PgSQLParser{}
|
||||
sql := `
|
||||
CREATE TABLE users (
|
||||
id SERIAL PRIMARY KEY,
|
||||
old_name VARCHAR(100)
|
||||
);
|
||||
ALTER TABLE users RENAME COLUMN old_name TO new_name;
|
||||
`
|
||||
tables := make(map[string]map[string]*gdb.TableField)
|
||||
err := processSQL(parser, sql, tables)
|
||||
t.AssertNil(err)
|
||||
|
||||
fields := tables["users"]
|
||||
_, ok := fields["old_name"]
|
||||
t.Assert(ok, false)
|
||||
t.Assert(fields["new_name"].Name, "new_name")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_PgSQL_MultipleMigrations(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
parser := &PgSQLParser{}
|
||||
tables := make(map[string]map[string]*gdb.TableField)
|
||||
|
||||
// V1
|
||||
err := processSQL(parser, `
|
||||
CREATE TABLE products (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
name VARCHAR(100) NOT NULL,
|
||||
price NUMERIC(10,2) DEFAULT 0.00
|
||||
);
|
||||
`, tables)
|
||||
t.AssertNil(err)
|
||||
|
||||
// V2: add, alter, comment
|
||||
err = processSQL(parser, `
|
||||
ALTER TABLE products ADD COLUMN category VARCHAR(50);
|
||||
ALTER TABLE products ALTER COLUMN name TYPE VARCHAR(200);
|
||||
ALTER TABLE products ALTER COLUMN name SET NOT NULL;
|
||||
COMMENT ON COLUMN products.category IS 'Product category';
|
||||
`, tables)
|
||||
t.AssertNil(err)
|
||||
|
||||
// V3: rename, drop
|
||||
err = processSQL(parser, `
|
||||
ALTER TABLE products RENAME COLUMN category TO product_category;
|
||||
`, tables)
|
||||
t.AssertNil(err)
|
||||
|
||||
fields := tables["products"]
|
||||
t.Assert(len(fields), 4)
|
||||
t.Assert(fields["name"].Type, "VARCHAR(200)")
|
||||
t.Assert(fields["name"].Null, false)
|
||||
_, ok := fields["category"]
|
||||
t.Assert(ok, false)
|
||||
t.Assert(fields["product_category"].Name, "product_category")
|
||||
t.Assert(fields["product_category"].Comment, "Product category")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_PgSQL_FullMigrationScenario(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
parser := &PgSQLParser{}
|
||||
tables := make(map[string]map[string]*gdb.TableField)
|
||||
|
||||
// V001: Initial
|
||||
err := processSQL(parser, `
|
||||
CREATE TABLE users (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
name VARCHAR(100) NOT NULL,
|
||||
email VARCHAR(200) UNIQUE
|
||||
);
|
||||
COMMENT ON COLUMN users.name IS 'User name';
|
||||
`, tables)
|
||||
t.AssertNil(err)
|
||||
|
||||
// V002: Add, alter type, set not null
|
||||
err = processSQL(parser, `
|
||||
ALTER TABLE users ADD COLUMN avatar TEXT;
|
||||
ALTER TABLE users ALTER COLUMN name TYPE VARCHAR(200);
|
||||
ALTER TABLE users ALTER COLUMN email SET NOT NULL;
|
||||
COMMENT ON COLUMN users.avatar IS 'Avatar URL';
|
||||
`, tables)
|
||||
t.AssertNil(err)
|
||||
|
||||
fields := tables["users"]
|
||||
t.Assert(len(fields), 4)
|
||||
t.Assert(fields["name"].Type, "VARCHAR(200)")
|
||||
t.Assert(fields["email"].Null, false)
|
||||
t.Assert(fields["avatar"].Comment, "Avatar URL")
|
||||
|
||||
// V003: Rename column, drop not null
|
||||
err = processSQL(parser, `
|
||||
ALTER TABLE users RENAME COLUMN avatar TO profile_image;
|
||||
ALTER TABLE users ALTER COLUMN email DROP NOT NULL;
|
||||
`, tables)
|
||||
t.AssertNil(err)
|
||||
|
||||
_, ok := fields["avatar"]
|
||||
t.Assert(ok, false)
|
||||
t.Assert(fields["profile_image"].Name, "profile_image")
|
||||
t.Assert(fields["email"].Null, true)
|
||||
})
|
||||
}
|
||||
@ -1,159 +0,0 @@
|
||||
// Copyright GoFrame gf 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 gendao
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
)
|
||||
|
||||
// SQLiteParser implements SQLParser for SQLite DDL.
|
||||
type SQLiteParser struct{}
|
||||
|
||||
// ParseCreateTable parses a single SQLite CREATE TABLE statement.
|
||||
func (p *SQLiteParser) ParseCreateTable(stmt string) (string, map[string]*gdb.TableField, error) {
|
||||
body, _, ok := extractBodyAndTrailing(stmt)
|
||||
if !ok {
|
||||
return "", nil, nil
|
||||
}
|
||||
|
||||
parenIdx := strings.Index(stmt, "(")
|
||||
header := stmt[:parenIdx]
|
||||
tableName := extractTableName(header)
|
||||
if tableName == "" {
|
||||
return "", nil, fmt.Errorf("cannot extract table name from: %s", header)
|
||||
}
|
||||
|
||||
columnDefs := splitColumns(body)
|
||||
fields := make(map[string]*gdb.TableField)
|
||||
pkColumns := findPrimaryKeysFromConstraints(columnDefs)
|
||||
|
||||
fieldIndex := 0
|
||||
for _, def := range columnDefs {
|
||||
def = strings.TrimSpace(def)
|
||||
if def == "" {
|
||||
continue
|
||||
}
|
||||
firstWord := strings.ToUpper(strings.Fields(def)[0])
|
||||
if isConstraintKeyword(firstWord) {
|
||||
continue
|
||||
}
|
||||
|
||||
field, err := p.parseColumnDef(def, fieldIndex)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if field != nil {
|
||||
fields[field.Name] = field
|
||||
fieldIndex++
|
||||
}
|
||||
}
|
||||
|
||||
for _, pkCol := range pkColumns {
|
||||
if f, ok := fields[pkCol]; ok {
|
||||
f.Key = "PRI"
|
||||
}
|
||||
}
|
||||
|
||||
return tableName, fields, nil
|
||||
}
|
||||
|
||||
// ParseAlterTable parses SQLite ALTER TABLE statements.
|
||||
// Note: SQLite only supports ADD COLUMN and RENAME COLUMN in ALTER TABLE.
|
||||
func (p *SQLiteParser) ParseAlterTable(stmt string, tables map[string]map[string]*gdb.TableField) error {
|
||||
return parseAlterTableCommon(stmt, tables, p.parseColumnDef)
|
||||
}
|
||||
|
||||
// ParseComment is a no-op for SQLite as it doesn't support COMMENT ON statements.
|
||||
func (p *SQLiteParser) ParseComment(stmt string, tables map[string]map[string]*gdb.TableField) {
|
||||
// SQLite does not support comments on columns.
|
||||
}
|
||||
|
||||
// parseColumnDef parses a single SQLite column definition string into a TableField.
|
||||
// SQLite has flexible typing (type affinity), so columns may have no explicit type,
|
||||
// in which case "text" is used as the default type.
|
||||
func (p *SQLiteParser) parseColumnDef(def string, index int) (*gdb.TableField, error) {
|
||||
tokens := mysqlTokenize(def)
|
||||
if len(tokens) < 1 {
|
||||
return nil, fmt.Errorf("invalid column definition: %s", def)
|
||||
}
|
||||
|
||||
field := &gdb.TableField{
|
||||
Index: index,
|
||||
Name: unquoteIdentifier(tokens[0]),
|
||||
Null: true,
|
||||
}
|
||||
|
||||
if len(tokens) < 2 {
|
||||
field.Type = "text"
|
||||
return field, nil
|
||||
}
|
||||
|
||||
field.Type = tokens[1]
|
||||
|
||||
rest := ""
|
||||
if len(tokens) > 2 {
|
||||
rest = strings.Join(tokens[2:], " ")
|
||||
}
|
||||
|
||||
if !strings.Contains(field.Type, "(") && strings.HasPrefix(strings.TrimSpace(rest), "(") {
|
||||
end := strings.Index(rest, ")")
|
||||
if end >= 0 {
|
||||
field.Type += rest[:end+1]
|
||||
rest = strings.TrimSpace(rest[end+1:])
|
||||
}
|
||||
}
|
||||
|
||||
p.parseColumnAttributes(field, rest)
|
||||
|
||||
return field, nil
|
||||
}
|
||||
|
||||
// parseColumnAttributes parses SQLite column constraint keywords including
|
||||
// NOT NULL, NULL, PRIMARY KEY (with optional AUTOINCREMENT), UNIQUE, and DEFAULT.
|
||||
func (p *SQLiteParser) parseColumnAttributes(field *gdb.TableField, attrs string) {
|
||||
words := strings.Fields(attrs)
|
||||
upperWords := strings.Fields(strings.ToUpper(attrs))
|
||||
|
||||
for i := 0; i < len(upperWords); i++ {
|
||||
switch upperWords[i] {
|
||||
case "NOT":
|
||||
if i+1 < len(upperWords) && upperWords[i+1] == "NULL" {
|
||||
field.Null = false
|
||||
i++
|
||||
}
|
||||
case "NULL":
|
||||
field.Null = true
|
||||
case "PRIMARY":
|
||||
if i+1 < len(upperWords) && upperWords[i+1] == "KEY" {
|
||||
field.Key = "PRI"
|
||||
field.Null = false
|
||||
i++
|
||||
if i+1 < len(upperWords) && upperWords[i+1] == "AUTOINCREMENT" {
|
||||
field.Extra = "auto_increment"
|
||||
i++
|
||||
}
|
||||
}
|
||||
case "AUTOINCREMENT":
|
||||
field.Extra = "auto_increment"
|
||||
case "UNIQUE":
|
||||
if field.Key == "" {
|
||||
field.Key = "UNI"
|
||||
}
|
||||
case "DEFAULT":
|
||||
if i+1 < len(words) {
|
||||
defaultVal, _ := extractDefaultValue("DEFAULT " + strings.Join(words[i+1:], " "))
|
||||
field.Default = defaultVal
|
||||
if defaultVal != nil {
|
||||
i++
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,112 +0,0 @@
|
||||
// Copyright GoFrame gf 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 gendao
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
)
|
||||
|
||||
func Test_SQLite_CreateTable_Basic(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
parser := &SQLiteParser{}
|
||||
sql := `
|
||||
CREATE TABLE users (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL,
|
||||
email TEXT,
|
||||
age INTEGER DEFAULT 0,
|
||||
score REAL DEFAULT 0.0,
|
||||
is_active BOOLEAN NOT NULL DEFAULT 1
|
||||
);
|
||||
`
|
||||
tables := make(map[string]map[string]*gdb.TableField)
|
||||
err := processSQL(parser, sql, tables)
|
||||
t.AssertNil(err)
|
||||
|
||||
fields := tables["users"]
|
||||
t.Assert(len(fields), 6)
|
||||
|
||||
t.Assert(fields["id"].Key, "PRI")
|
||||
t.Assert(fields["id"].Extra, "auto_increment")
|
||||
t.Assert(fields["id"].Null, false)
|
||||
|
||||
t.Assert(fields["name"].Null, false)
|
||||
t.Assert(fields["email"].Null, true)
|
||||
t.Assert(fields["age"].Default, "0")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_SQLite_AlterTable_AddColumn(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
parser := &SQLiteParser{}
|
||||
sql := `
|
||||
CREATE TABLE users (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL
|
||||
);
|
||||
ALTER TABLE users ADD COLUMN email TEXT;
|
||||
ALTER TABLE users ADD COLUMN phone TEXT DEFAULT '';
|
||||
`
|
||||
tables := make(map[string]map[string]*gdb.TableField)
|
||||
err := processSQL(parser, sql, tables)
|
||||
t.AssertNil(err)
|
||||
|
||||
fields := tables["users"]
|
||||
t.Assert(len(fields), 4)
|
||||
t.Assert(fields["email"].Name, "email")
|
||||
t.Assert(fields["phone"].Name, "phone")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_SQLite_AlterTable_DropColumn(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
parser := &SQLiteParser{}
|
||||
sql := `
|
||||
CREATE TABLE users (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL,
|
||||
old_col TEXT,
|
||||
email TEXT
|
||||
);
|
||||
ALTER TABLE users DROP COLUMN old_col;
|
||||
`
|
||||
tables := make(map[string]map[string]*gdb.TableField)
|
||||
err := processSQL(parser, sql, tables)
|
||||
t.AssertNil(err)
|
||||
|
||||
fields := tables["users"]
|
||||
t.Assert(len(fields), 3)
|
||||
_, ok := fields["old_col"]
|
||||
t.Assert(ok, false)
|
||||
t.Assert(fields["name"].Name, "name")
|
||||
t.Assert(fields["email"].Name, "email")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_SQLite_RenameColumn(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
parser := &SQLiteParser{}
|
||||
sql := `
|
||||
CREATE TABLE users (
|
||||
id INTEGER PRIMARY KEY,
|
||||
old_name TEXT NOT NULL
|
||||
);
|
||||
ALTER TABLE users RENAME COLUMN old_name TO new_name;
|
||||
`
|
||||
tables := make(map[string]map[string]*gdb.TableField)
|
||||
err := processSQL(parser, sql, tables)
|
||||
t.AssertNil(err)
|
||||
|
||||
fields := tables["users"]
|
||||
_, ok := fields["old_name"]
|
||||
t.Assert(ok, false)
|
||||
t.Assert(fields["new_name"].Name, "new_name")
|
||||
})
|
||||
}
|
||||
@ -1,302 +0,0 @@
|
||||
// Copyright GoFrame gf 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 gendao
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
)
|
||||
|
||||
// ===========================
|
||||
// Common parser utilities tests
|
||||
// ===========================
|
||||
|
||||
func Test_splitSQLStatements(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
stmts := splitSQLStatements("CREATE TABLE t1 (id INT); ALTER TABLE t1 ADD COLUMN name VARCHAR(100);")
|
||||
t.Assert(len(stmts), 2)
|
||||
t.AssertIN("CREATE TABLE t1 (id INT)", stmts)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_splitSQLStatements_WithComments(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
sql := `
|
||||
-- This is a comment
|
||||
CREATE TABLE t1 (id INT);
|
||||
/* Block comment */
|
||||
ALTER TABLE t1 ADD COLUMN name VARCHAR(100);
|
||||
`
|
||||
stmts := splitSQLStatements(sql)
|
||||
t.Assert(len(stmts), 2)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_splitSQLStatements_WithQuotedSemicolon(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
sql := `CREATE TABLE t1 (id INT, name VARCHAR(100) DEFAULT 'a;b');`
|
||||
stmts := splitSQLStatements(sql)
|
||||
t.Assert(len(stmts), 1)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_classifyStatement(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
t.Assert(classifyStatement("CREATE TABLE users (id INT)"), SQLStatementCreateTable)
|
||||
t.Assert(classifyStatement("CREATE TEMPORARY TABLE tmp (id INT)"), SQLStatementCreateTable)
|
||||
t.Assert(classifyStatement("ALTER TABLE users ADD COLUMN email VARCHAR(100)"), SQLStatementAlterTable)
|
||||
t.Assert(classifyStatement("ALTER TABLE users RENAME TO customers"), SQLStatementRenameTable)
|
||||
t.Assert(classifyStatement("DROP TABLE IF EXISTS users"), SQLStatementDropTable)
|
||||
t.Assert(classifyStatement("RENAME TABLE old_name TO new_name"), SQLStatementRenameTable)
|
||||
t.Assert(classifyStatement("COMMENT ON COLUMN users.name IS 'User name'"), SQLStatementComment)
|
||||
t.Assert(classifyStatement("SELECT * FROM users"), SQLStatementUnknown)
|
||||
t.Assert(classifyStatement("INSERT INTO users VALUES (1)"), SQLStatementUnknown)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_unquoteIdentifier(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
t.Assert(unquoteIdentifier("`users`"), "users")
|
||||
t.Assert(unquoteIdentifier(`"users"`), "users")
|
||||
t.Assert(unquoteIdentifier("[users]"), "users")
|
||||
t.Assert(unquoteIdentifier("users"), "users")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_extractTableName(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
t.Assert(extractTableName("CREATE TABLE users"), "users")
|
||||
t.Assert(extractTableName("CREATE TABLE IF NOT EXISTS users"), "users")
|
||||
t.Assert(extractTableName("CREATE TABLE `users`"), "users")
|
||||
t.Assert(extractTableName("CREATE TABLE mydb.users"), "users")
|
||||
t.Assert(extractTableName("CREATE TEMPORARY TABLE temp_users"), "temp_users")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_applyDropTable(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
tables := map[string]map[string]*gdb.TableField{
|
||||
"users": {},
|
||||
"logs": {},
|
||||
}
|
||||
applyDropTable("DROP TABLE IF EXISTS users", tables)
|
||||
t.Assert(len(tables), 1)
|
||||
_, ok := tables["users"]
|
||||
t.Assert(ok, false)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_applyRenameTable_MySQL(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
tables := map[string]map[string]*gdb.TableField{
|
||||
"old_name": {"id": {Index: 0, Name: "id", Type: "int"}},
|
||||
}
|
||||
applyRenameTable("RENAME TABLE old_name TO new_name", tables)
|
||||
t.Assert(len(tables), 1)
|
||||
_, ok := tables["new_name"]
|
||||
t.Assert(ok, true)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_applyRenameTable_PgSQL(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
tables := map[string]map[string]*gdb.TableField{
|
||||
"old_name": {"id": {Index: 0, Name: "id", Type: "int"}},
|
||||
}
|
||||
applyRenameTable("ALTER TABLE old_name RENAME TO new_name", tables)
|
||||
t.Assert(len(tables), 1)
|
||||
_, ok := tables["new_name"]
|
||||
t.Assert(ok, true)
|
||||
})
|
||||
}
|
||||
|
||||
// ===========================
|
||||
// Abnormal/edge-case parsing tests
|
||||
// ===========================
|
||||
|
||||
func Test_processSQL_OnlyDMLStatements(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
parser := &MySQLParser{}
|
||||
sql := `
|
||||
INSERT INTO users (id, name) VALUES (1, 'Alice');
|
||||
INSERT INTO users (id, name) VALUES (2, 'Bob');
|
||||
DELETE FROM users WHERE id = 1;
|
||||
UPDATE users SET name = 'Charlie' WHERE id = 2;
|
||||
SELECT * FROM users;
|
||||
`
|
||||
tables := make(map[string]map[string]*gdb.TableField)
|
||||
err := processSQL(parser, sql, tables)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(tables), 0)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_processSQL_EmptySQL(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
parser := &MySQLParser{}
|
||||
tables := make(map[string]map[string]*gdb.TableField)
|
||||
|
||||
// Empty string
|
||||
err := processSQL(parser, "", tables)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(tables), 0)
|
||||
|
||||
// Only whitespace and newlines
|
||||
err = processSQL(parser, " \n\n \t ", tables)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(tables), 0)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_processSQL_OnlyComments(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
parser := &MySQLParser{}
|
||||
sql := `
|
||||
-- This is a line comment
|
||||
/* This is a block comment */
|
||||
-- Another comment
|
||||
`
|
||||
tables := make(map[string]map[string]*gdb.TableField)
|
||||
err := processSQL(parser, sql, tables)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(tables), 0)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_processSQL_AlterNonExistentTable(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
parser := &MySQLParser{}
|
||||
sql := `
|
||||
ALTER TABLE non_existent ADD COLUMN email VARCHAR(200);
|
||||
ALTER TABLE non_existent DROP COLUMN name;
|
||||
ALTER TABLE non_existent MODIFY COLUMN name VARCHAR(200);
|
||||
`
|
||||
tables := make(map[string]map[string]*gdb.TableField)
|
||||
err := processSQL(parser, sql, tables)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(tables), 0)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_processSQL_DropNonExistentTable(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
parser := &MySQLParser{}
|
||||
sql := `DROP TABLE IF EXISTS non_existent;`
|
||||
tables := make(map[string]map[string]*gdb.TableField)
|
||||
err := processSQL(parser, sql, tables)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(tables), 0)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_processSQL_MixedDDLAndDML(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
parser := &MySQLParser{}
|
||||
sql := `
|
||||
INSERT INTO logs (msg) VALUES ('starting migration');
|
||||
CREATE TABLE users (
|
||||
id INT NOT NULL AUTO_INCREMENT,
|
||||
name VARCHAR(100) NOT NULL,
|
||||
PRIMARY KEY (id)
|
||||
);
|
||||
INSERT INTO users (name) VALUES ('Alice');
|
||||
ALTER TABLE users ADD COLUMN email VARCHAR(200);
|
||||
UPDATE users SET email = 'alice@example.com' WHERE id = 1;
|
||||
DELETE FROM logs WHERE msg = 'starting migration';
|
||||
`
|
||||
tables := make(map[string]map[string]*gdb.TableField)
|
||||
err := processSQL(parser, sql, tables)
|
||||
t.AssertNil(err)
|
||||
// Only DDL statements should be processed; DML should be skipped.
|
||||
t.Assert(len(tables), 1)
|
||||
fields := tables["users"]
|
||||
t.Assert(len(fields), 3)
|
||||
t.Assert(fields["id"].Key, "PRI")
|
||||
t.Assert(fields["email"].Name, "email")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_processSQL_CommentOnNonExistentTable(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
parser := &PgSQLParser{}
|
||||
sql := `COMMENT ON COLUMN non_existent.col1 IS 'some comment';`
|
||||
tables := make(map[string]map[string]*gdb.TableField)
|
||||
err := processSQL(parser, sql, tables)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(tables), 0)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_processSQL_RenameNonExistentTable(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
parser := &MySQLParser{}
|
||||
sql := `RENAME TABLE non_existent TO new_name;`
|
||||
tables := make(map[string]map[string]*gdb.TableField)
|
||||
err := processSQL(parser, sql, tables)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(tables), 0)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_processSQL_DropColumnFromNonExistentTable(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
parser := &MySQLParser{}
|
||||
sql := `
|
||||
CREATE TABLE users (id INT, name VARCHAR(100), PRIMARY KEY (id));
|
||||
ALTER TABLE orders DROP COLUMN status;
|
||||
`
|
||||
tables := make(map[string]map[string]*gdb.TableField)
|
||||
err := processSQL(parser, sql, tables)
|
||||
t.AssertNil(err)
|
||||
// users table should still exist, orders ALTER should be silently ignored.
|
||||
t.Assert(len(tables), 1)
|
||||
t.Assert(len(tables["users"]), 2)
|
||||
})
|
||||
}
|
||||
|
||||
// ===========================
|
||||
// CheckLocalTypeForFieldType Tests
|
||||
// ===========================
|
||||
|
||||
func Test_CheckLocalTypeForFieldType(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
tests := []struct {
|
||||
fieldType string
|
||||
expected string
|
||||
}{
|
||||
{"int(10)", "int"},
|
||||
{"int(10) unsigned", "uint"},
|
||||
{"bigint(20)", "int64"},
|
||||
{"bigint(20) unsigned", "uint64"},
|
||||
{"tinyint(1)", "int"},
|
||||
{"varchar(100)", "string"},
|
||||
{"text", "string"},
|
||||
{"datetime", "datetime"},
|
||||
{"timestamp", "datetime"},
|
||||
{"timestamptz", "datetime"},
|
||||
{"date", "date"},
|
||||
{"time", "time"},
|
||||
{"json", "json"},
|
||||
{"jsonb", "jsonb"},
|
||||
{"float", "float64"},
|
||||
{"double", "float64"},
|
||||
{"decimal(10,2)", "string"},
|
||||
{"bool", "bool"},
|
||||
{"boolean", "bool"},
|
||||
{"blob", "[]byte"},
|
||||
{"binary(16)", "[]byte"},
|
||||
{"bit(1)", "bool"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
localType, err := gdb.CheckLocalTypeForFieldType(tt.fieldType)
|
||||
t.AssertNil(err)
|
||||
t.Assert(string(localType), tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
@ -20,20 +20,14 @@ import (
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
)
|
||||
|
||||
// generateStructDefinitionInput holds parameters for generating a Go struct definition
|
||||
// from database table fields.
|
||||
type generateStructDefinitionInput struct {
|
||||
CGenDaoInternalInput
|
||||
TableName string // Original database table name.
|
||||
StructName string // Go struct name (CamelCase of table name).
|
||||
FieldMap map[string]*gdb.TableField // Map of column name to field metadata.
|
||||
IsDo bool // Whether generating a DO struct (uses g.Meta orm tag).
|
||||
TableName string // Table name.
|
||||
StructName string // Struct name.
|
||||
FieldMap map[string]*gdb.TableField // Table field map.
|
||||
IsDo bool // Is generating DTO struct.
|
||||
}
|
||||
|
||||
// generateStructDefinition generates a complete Go struct definition string from table fields.
|
||||
// It returns the struct source code and a list of additional import paths needed
|
||||
// by custom type mappings. The fields are rendered in a table-aligned format
|
||||
// using tablewriter for consistent code formatting.
|
||||
func generateStructDefinition(ctx context.Context, in generateStructDefinitionInput) (string, []string) {
|
||||
var appendImports []string
|
||||
buffer := bytes.NewBuffer(nil)
|
||||
@ -65,10 +59,6 @@ func generateStructDefinition(ctx context.Context, in generateStructDefinitionIn
|
||||
return buffer.String(), appendImports
|
||||
}
|
||||
|
||||
// getTypeMappingInfo looks up a database field type in the type mapping configuration.
|
||||
// It handles exact matches first, then tries to extract the base type name from
|
||||
// parameterized types like "varchar(255)" or "numeric(10,2) unsigned".
|
||||
// Returns the mapped Go type name and its import path (if any).
|
||||
func getTypeMappingInfo(
|
||||
ctx context.Context, fieldType string, inTypeMapping map[DBFieldTypeName]CustomAttributeType,
|
||||
) (typeNameStr, importStr string) {
|
||||
@ -115,17 +105,9 @@ func generateStructFieldDefinition(
|
||||
}
|
||||
|
||||
if localTypeNameStr == "" {
|
||||
if in.DB != nil {
|
||||
localTypeName, err = in.DB.CheckLocalTypeForField(ctx, field.Type, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
} else {
|
||||
// SQL file mode: use standalone type checking without database connection.
|
||||
localTypeName, err = gdb.CheckLocalTypeForFieldType(field.Type)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
localTypeName, err = in.DB.CheckLocalTypeForField(ctx, field.Type, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
localTypeNameStr = string(localTypeName)
|
||||
switch localTypeName {
|
||||
@ -199,12 +181,11 @@ func generateStructFieldDefinition(
|
||||
return attrLines, appendImport
|
||||
}
|
||||
|
||||
// FieldNameCase defines the naming convention for converting field names to Go identifiers.
|
||||
type FieldNameCase string
|
||||
|
||||
const (
|
||||
FieldNameCaseCamel FieldNameCase = "CaseCamel" // PascalCase: "user_name" -> "UserName"
|
||||
FieldNameCaseCamelLower FieldNameCase = "CaseCamelLower" // camelCase: "user_name" -> "userName"
|
||||
FieldNameCaseCamel FieldNameCase = "CaseCamel"
|
||||
FieldNameCaseCamelLower FieldNameCase = "CaseCamelLower"
|
||||
)
|
||||
|
||||
// formatFieldName formats and returns a new field name that is used for golang codes generating.
|
||||
|
||||
@ -62,7 +62,7 @@ type generateTableSingleInput struct {
|
||||
// generateTableSingle generates dao files for a single table.
|
||||
func generateTableSingle(ctx context.Context, in generateTableSingleInput) {
|
||||
// Generating table data preparing.
|
||||
fieldMap, err := getTableFields(ctx, in.CGenDaoInternalInput, in.TableName)
|
||||
fieldMap, err := in.DB.TableFields(ctx, in.TableName)
|
||||
if err != nil {
|
||||
mlog.Fatalf(`fetching tables fields failed for table "%s": %+v`, in.TableName, err)
|
||||
}
|
||||
|
||||
@ -74,8 +74,6 @@ CONFIGURATION SUPPORT
|
||||
CGenDaoBriefTypeMapping = `custom local type mapping for generated struct attributes relevant to fields of table`
|
||||
CGenDaoBriefFieldMapping = `custom local type mapping for generated struct attributes relevant to specific fields of table`
|
||||
CGenDaoBriefShardingPattern = `sharding pattern for table name, e.g. "users_?" will be replace tables "users_001,users_002,..." to "users" dao`
|
||||
CGenDaoBriefSqlDir = `directory path of SQL DDL files for generating dao/do/entity without database connection`
|
||||
CGenDaoBriefSqlType = `SQL dialect type when using sqlDir, options: mysql|pgsql|mssql|oracle|sqlite, default is "mysql"`
|
||||
CGenDaoBriefGroup = `
|
||||
specifying the configuration group name of database for generated ORM instance,
|
||||
it's not necessary and the default value is "default"
|
||||
@ -97,23 +95,21 @@ generated json tag case for model struct, cases are as follows:
|
||||
CGenDaoBriefTplDaoDoPathPath = `template file path for dao do file`
|
||||
CGenDaoBriefTplDaoEntityPath = `template file path for dao entity file`
|
||||
|
||||
// Template variable names used by gview for rendering Go file templates.
|
||||
// These are passed to tplView.Assigns() and referenced in template files.
|
||||
tplVarTableName = `TplTableName` // Original database table name.
|
||||
tplVarTableNameCamelCase = `TplTableNameCamelCase` // PascalCase table name (e.g., "UserDetail").
|
||||
tplVarTableNameCamelLowerCase = `TplTableNameCamelLowerCase` // camelCase table name (e.g., "userDetail").
|
||||
tplVarTableSharding = `TplTableSharding` // Boolean: whether this is a sharding table.
|
||||
tplVarTableShardingPrefix = `TplTableShardingPrefix` // Sharding table name prefix (e.g., "user_").
|
||||
tplVarTableFields = `TplTableFields` // Generated table field definitions.
|
||||
tplVarPackageImports = `TplPackageImports` // Generated import block string.
|
||||
tplVarImportPrefix = `TplImportPrefix` // Go import path prefix for internal dao package.
|
||||
tplVarStructDefine = `TplStructDefine` // Generated struct definition string.
|
||||
tplVarColumnDefine = `TplColumnDefine` // Column struct field definitions for dao internal.
|
||||
tplVarColumnNames = `TplColumnNames` // Column name-to-string assignments for dao internal.
|
||||
tplVarGroupName = `TplGroupName` // Database configuration group name.
|
||||
tplVarDatetimeStr = `TplDatetimeStr` // Current datetime string for file headers.
|
||||
tplVarCreatedAtDatetimeStr = `TplCreatedAtDatetimeStr` // "Created at <datetime>" string (empty if WithTime is false).
|
||||
tplVarPackageName = `TplPackageName` // Go package name for the generated file.
|
||||
tplVarTableName = `TplTableName`
|
||||
tplVarTableNameCamelCase = `TplTableNameCamelCase`
|
||||
tplVarTableNameCamelLowerCase = `TplTableNameCamelLowerCase`
|
||||
tplVarTableSharding = `TplTableSharding`
|
||||
tplVarTableShardingPrefix = `TplTableShardingPrefix`
|
||||
tplVarTableFields = `TplTableFields`
|
||||
tplVarPackageImports = `TplPackageImports`
|
||||
tplVarImportPrefix = `TplImportPrefix`
|
||||
tplVarStructDefine = `TplStructDefine`
|
||||
tplVarColumnDefine = `TplColumnDefine`
|
||||
tplVarColumnNames = `TplColumnNames`
|
||||
tplVarGroupName = `TplGroupName`
|
||||
tplVarDatetimeStr = `TplDatetimeStr`
|
||||
tplVarCreatedAtDatetimeStr = `TplCreatedAtDatetimeStr`
|
||||
tplVarPackageName = `TplPackageName`
|
||||
)
|
||||
|
||||
func init() {
|
||||
@ -149,8 +145,6 @@ func init() {
|
||||
`CGenDaoBriefTypeMapping`: CGenDaoBriefTypeMapping,
|
||||
`CGenDaoBriefFieldMapping`: CGenDaoBriefFieldMapping,
|
||||
`CGenDaoBriefShardingPattern`: CGenDaoBriefShardingPattern,
|
||||
`CGenDaoBriefSqlDir`: CGenDaoBriefSqlDir,
|
||||
`CGenDaoBriefSqlType`: CGenDaoBriefSqlType,
|
||||
`CGenDaoBriefGroup`: CGenDaoBriefGroup,
|
||||
`CGenDaoBriefJsonCase`: CGenDaoBriefJsonCase,
|
||||
`CGenDaoBriefTplDaoIndexPath`: CGenDaoBriefTplDaoIndexPath,
|
||||
|
||||
@ -12,6 +12,7 @@ import (
|
||||
|
||||
"github.com/gogf/gf/v2/container/garray"
|
||||
"github.com/gogf/gf/v2/container/gmap"
|
||||
"github.com/gogf/gf/v2/os/gfile"
|
||||
"github.com/gogf/gf/v2/text/gregex"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
|
||||
@ -36,14 +37,21 @@ func (c CGenService) calculateImportedItems(
|
||||
}
|
||||
|
||||
for _, item := range pkgItems {
|
||||
// Skip anonymous imports
|
||||
if item.Alias == "_" {
|
||||
alias := item.Alias
|
||||
|
||||
// If the alias is _, it means that the package is not generated.
|
||||
if alias == "_" {
|
||||
mlog.Debugf(`ignore anonymous package: %s`, item.RawImport)
|
||||
continue
|
||||
}
|
||||
// Keep all imports, let gofmt clean up unused ones.
|
||||
// We cannot accurately infer package name from import path
|
||||
// (e.g., path "minio-go" but package name is "minio").
|
||||
// If the alias is empty, it will use the package name as the alias.
|
||||
if alias == "" {
|
||||
alias = gfile.Basename(gstr.Trim(item.Path, `"`))
|
||||
}
|
||||
if !gstr.Contains(allFuncParamType.String(), alias) {
|
||||
mlog.Debugf(`ignore unused package: %s`, item.RawImport)
|
||||
continue
|
||||
}
|
||||
srcImportedPackages.Add(item.RawImport)
|
||||
}
|
||||
return nil
|
||||
|
||||
@ -4,7 +4,7 @@ go 1.23.0
|
||||
|
||||
toolchain go1.24.6
|
||||
|
||||
require github.com/gogf/gf/v2 v2.10.0
|
||||
require github.com/gogf/gf/v2 v2.9.8
|
||||
|
||||
require (
|
||||
go.opentelemetry.io/otel v1.38.0 // indirect
|
||||
|
||||
@ -1,34 +0,0 @@
|
||||
package issue4242
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
|
||||
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/issue/4242/service"
|
||||
|
||||
"github.com/gogf/gf/contrib/drivers/mysql/v2"
|
||||
)
|
||||
|
||||
func init() {
|
||||
service.RegisterIssue4242(New())
|
||||
}
|
||||
|
||||
type sIssue4242 struct {
|
||||
}
|
||||
|
||||
func New() *sIssue4242 {
|
||||
return &sIssue4242{}
|
||||
}
|
||||
|
||||
// GetDriver tests versioned import path is preserved.
|
||||
func (s *sIssue4242) GetDriver(ctx context.Context) (d mysql.Driver, err error) {
|
||||
return mysql.Driver{}, nil
|
||||
}
|
||||
|
||||
// GetRequest tests another versioned import.
|
||||
func (s *sIssue4242) GetRequest(ctx context.Context) (*ghttp.Request, error) {
|
||||
g.Log().Info(ctx, "getting request")
|
||||
return nil, nil
|
||||
}
|
||||
@ -1,37 +0,0 @@
|
||||
package issue4242alias
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
// Anonymous import (should be skipped)
|
||||
_ "github.com/gogf/gf/v2/os/gres"
|
||||
|
||||
// Versioned import without alias
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
|
||||
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/issue/4242/service"
|
||||
|
||||
// Explicit alias import
|
||||
mysqlDriver "github.com/gogf/gf/contrib/drivers/mysql/v2"
|
||||
)
|
||||
|
||||
func init() {
|
||||
service.RegisterIssue4242Alias(New())
|
||||
}
|
||||
|
||||
type sIssue4242Alias struct {
|
||||
}
|
||||
|
||||
func New() *sIssue4242Alias {
|
||||
return &sIssue4242Alias{}
|
||||
}
|
||||
|
||||
// GetDriver tests explicit alias import.
|
||||
func (s *sIssue4242Alias) GetDriver(ctx context.Context) (d mysqlDriver.Driver, err error) {
|
||||
return mysqlDriver.Driver{}, nil
|
||||
}
|
||||
|
||||
// GetRequest tests versioned import.
|
||||
func (s *sIssue4242Alias) GetRequest(ctx context.Context) (*ghttp.Request, error) {
|
||||
return nil, nil
|
||||
}
|
||||
@ -1,10 +0,0 @@
|
||||
// ==========================================================================
|
||||
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
|
||||
// ==========================================================================
|
||||
|
||||
package logic
|
||||
|
||||
import (
|
||||
_ "github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/issue/4242/logic/issue4242"
|
||||
_ "github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/issue/4242/logic/issue4242alias"
|
||||
)
|
||||
@ -1,37 +0,0 @@
|
||||
// ================================================================================
|
||||
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
|
||||
// You can delete these comments if you wish manually maintain this interface file.
|
||||
// ================================================================================
|
||||
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/gogf/gf/contrib/drivers/mysql/v2"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
)
|
||||
|
||||
type (
|
||||
IIssue4242 interface {
|
||||
// GetDriver tests versioned import path is preserved.
|
||||
GetDriver(ctx context.Context) (d mysql.Driver, err error)
|
||||
// GetRequest tests another versioned import.
|
||||
GetRequest(ctx context.Context) (*ghttp.Request, error)
|
||||
}
|
||||
)
|
||||
|
||||
var (
|
||||
localIssue4242 IIssue4242
|
||||
)
|
||||
|
||||
func Issue4242() IIssue4242 {
|
||||
if localIssue4242 == nil {
|
||||
panic("implement not found for interface IIssue4242, forgot register?")
|
||||
}
|
||||
return localIssue4242
|
||||
}
|
||||
|
||||
func RegisterIssue4242(i IIssue4242) {
|
||||
localIssue4242 = i
|
||||
}
|
||||
@ -1,37 +0,0 @@
|
||||
// ================================================================================
|
||||
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
|
||||
// You can delete these comments if you wish manually maintain this interface file.
|
||||
// ================================================================================
|
||||
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
mysqlDriver "github.com/gogf/gf/contrib/drivers/mysql/v2"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
)
|
||||
|
||||
type (
|
||||
IIssue4242Alias interface {
|
||||
// GetDriver tests explicit alias import.
|
||||
GetDriver(ctx context.Context) (d mysqlDriver.Driver, err error)
|
||||
// GetRequest tests versioned import.
|
||||
GetRequest(ctx context.Context) (*ghttp.Request, error)
|
||||
}
|
||||
)
|
||||
|
||||
var (
|
||||
localIssue4242Alias IIssue4242Alias
|
||||
)
|
||||
|
||||
func Issue4242Alias() IIssue4242Alias {
|
||||
if localIssue4242Alias == nil {
|
||||
panic("implement not found for interface IIssue4242Alias, forgot register?")
|
||||
}
|
||||
return localIssue4242Alias
|
||||
}
|
||||
|
||||
func RegisterIssue4242Alias(i IIssue4242Alias) {
|
||||
localIssue4242Alias = i
|
||||
}
|
||||
@ -1,5 +1,3 @@
|
||||
module github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/issue/4387
|
||||
|
||||
go 1.23.0
|
||||
|
||||
toolchain go1.24.12
|
||||
go 1.20
|
||||
|
||||
@ -749,9 +749,7 @@ func (a *TArray[T]) String() string {
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
// DO NOT change this receiver to pointer type, as the TArray can be used as a var defined variable, like:
|
||||
// var a TArray[int]
|
||||
// Please refer to corresponding tests for more details.
|
||||
// Note that do not use pointer as its receiver here.
|
||||
func (a TArray[T]) MarshalJSON() ([]byte, error) {
|
||||
a.mu.RLock()
|
||||
defer a.mu.RUnlock()
|
||||
|
||||
@ -46,7 +46,7 @@ func NewSortedTArray[T comparable](comparator func(a, b T) int, safe ...bool) *S
|
||||
return NewSortedTArraySize(0, comparator, safe...)
|
||||
}
|
||||
|
||||
// NewSortedTArraySize create and returns a sorted array with given size and cap.
|
||||
// NewSortedTArraySize create and returns an sorted array with given size and cap.
|
||||
// The parameter `safe` is used to specify whether using array in concurrent-safety,
|
||||
// which is false in default.
|
||||
func NewSortedTArraySize[T comparable](cap int, comparator func(a, b T) int, safe ...bool) *SortedTArray[T] {
|
||||
@ -718,9 +718,7 @@ func (a *SortedTArray[T]) String() string {
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
// DO NOT change this receiver to pointer type, as the TArray can be used as a var defined variable, like:
|
||||
// var a SortedTArray[int]
|
||||
// Please refer to corresponding tests for more details.
|
||||
// Note that do not use pointer as its receiver here.
|
||||
func (a SortedTArray[T]) MarshalJSON() ([]byte, error) {
|
||||
a.mu.RLock()
|
||||
defer a.mu.RUnlock()
|
||||
|
||||
@ -17,17 +17,10 @@ import (
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
// NilChecker is a function that checks whether the given value is nil.
|
||||
type NilChecker[V any] func(V) bool
|
||||
|
||||
// KVMap wraps map type `map[K]V` and provides more map features.
|
||||
type KVMap[K comparable, V any] struct {
|
||||
mu rwmutex.RWMutex
|
||||
data map[K]V
|
||||
|
||||
// nilChecker is the custom nil checker function.
|
||||
// It uses empty.IsNil if it's nil.
|
||||
nilChecker NilChecker[V]
|
||||
}
|
||||
|
||||
// NewKVMap creates and returns an empty hash map.
|
||||
@ -36,13 +29,6 @@ func NewKVMap[K comparable, V any](safe ...bool) *KVMap[K, V] {
|
||||
return NewKVMapFrom(make(map[K]V), safe...)
|
||||
}
|
||||
|
||||
// NewKVMapWithChecker creates and returns an empty hash map with a custom nil checker.
|
||||
// The parameter `checker` is a function used to determine if a value is nil.
|
||||
// The parameter `safe` is used to specify whether to use the map in concurrent-safety mode, which is false by default.
|
||||
func NewKVMapWithChecker[K comparable, V any](checker NilChecker[V], safe ...bool) *KVMap[K, V] {
|
||||
return NewKVMapWithCheckerFrom(make(map[K]V), checker, safe...)
|
||||
}
|
||||
|
||||
// NewKVMapFrom creates and returns a hash map from given map `data`.
|
||||
// Note that, the param `data` map will be set as the underlying data map (no deep copy),
|
||||
// there might be some concurrent-safe issues when changing the map outside.
|
||||
@ -54,37 +40,6 @@ func NewKVMapFrom[K comparable, V any](data map[K]V, safe ...bool) *KVMap[K, V]
|
||||
return m
|
||||
}
|
||||
|
||||
// NewKVMapWithCheckerFrom creates and returns a hash map from given map `data` with a custom nil checker.
|
||||
// Note that, the param `data` map will be set as the underlying data map (no deep copy),
|
||||
// and there might be some concurrent-safe issues when changing the map outside.
|
||||
// The parameter `checker` is a function used to determine if a value is nil.
|
||||
// The parameter `safe` is used to specify whether to use the map in concurrent-safety mode, which is false by default.
|
||||
func NewKVMapWithCheckerFrom[K comparable, V any](data map[K]V, checker NilChecker[V], safe ...bool) *KVMap[K, V] {
|
||||
m := NewKVMapFrom[K, V](data, safe...)
|
||||
m.SetNilChecker(checker)
|
||||
return m
|
||||
}
|
||||
|
||||
// SetNilChecker registers a custom nil checker function for the map values.
|
||||
// This function is used to determine if a value should be considered as nil.
|
||||
// The nil checker function takes a value of type V and returns a boolean indicating
|
||||
// whether the value should be treated as nil.
|
||||
func (m *KVMap[K, V]) SetNilChecker(nilChecker NilChecker[V]) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.nilChecker = nilChecker
|
||||
}
|
||||
|
||||
// isNil checks whether the given value is nil.
|
||||
// It first checks if a custom nil checker function is registered and uses it if available,
|
||||
// otherwise it falls back to the default empty.IsNil function.
|
||||
func (m *KVMap[K, V]) isNil(v V) bool {
|
||||
if m.nilChecker != nil {
|
||||
return m.nilChecker(v)
|
||||
}
|
||||
return empty.IsNil(v)
|
||||
}
|
||||
|
||||
// Iterator iterates the hash map readonly with custom callback function `f`.
|
||||
// If `f` returns true, then it continues iterating; or false to stop.
|
||||
func (m *KVMap[K, V]) Iterator(f func(k K, v V) bool) {
|
||||
@ -245,12 +200,11 @@ func (m *KVMap[K, V]) Pops(size int) map[K]V {
|
||||
return newMap
|
||||
}
|
||||
|
||||
// doSetWithLockCheck sets value with given `value` if it does not exist,
|
||||
// and then returns this value and whether it exists.
|
||||
// doSetWithLockCheck checks whether value of the key exists with mutex.Lock,
|
||||
// if not exists, set value to the map with given `key`,
|
||||
// or else just return the existing value.
|
||||
//
|
||||
// It is a helper function for GetOrSet* functions.
|
||||
//
|
||||
// Note that, it does not add the value to the map if the given `value` is nil.
|
||||
// It returns value with given `key`.
|
||||
func (m *KVMap[K, V]) doSetWithLockCheck(key K, value V) (val V, ok bool) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
@ -262,9 +216,7 @@ func (m *KVMap[K, V]) doSetWithLockCheck(key K, value V) (val V, ok bool) {
|
||||
if v, ok := m.data[key]; ok {
|
||||
return v, true
|
||||
}
|
||||
if !m.isNil(value) {
|
||||
m.data[key] = value
|
||||
}
|
||||
m.data[key] = value
|
||||
return value, false
|
||||
}
|
||||
|
||||
@ -278,8 +230,6 @@ func (m *KVMap[K, V]) GetOrSet(key K, value V) V {
|
||||
// GetOrSetFunc returns the value by key,
|
||||
// or sets value with returned value of callback function `f` if it does not exist
|
||||
// and then returns this value.
|
||||
//
|
||||
// Note that, it does not add the value to the map if the returned value of `f` is nil.
|
||||
func (m *KVMap[K, V]) GetOrSetFunc(key K, f func() V) V {
|
||||
v, _ := m.doSetWithLockCheck(key, f())
|
||||
return v
|
||||
@ -291,8 +241,6 @@ func (m *KVMap[K, V]) GetOrSetFunc(key K, f func() V) V {
|
||||
//
|
||||
// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f`
|
||||
// with mutex.Lock of the hash map.
|
||||
//
|
||||
// Note that, it does not add the value to the map if the returned value of `f` is nil.
|
||||
func (m *KVMap[K, V]) GetOrSetFuncLock(key K, f func() V) V {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
@ -303,9 +251,7 @@ func (m *KVMap[K, V]) GetOrSetFuncLock(key K, f func() V) V {
|
||||
return v
|
||||
}
|
||||
value := f()
|
||||
if !m.isNil(value) {
|
||||
m.data[key] = value
|
||||
}
|
||||
m.data[key] = value
|
||||
return value
|
||||
}
|
||||
|
||||
@ -532,9 +478,6 @@ func (m *KVMap[K, V]) String() string {
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
// DO NOT change this receiver to pointer type, as the KVMap can be used as a var defined variable, like:
|
||||
// var m gmap.KVMap[int, string]
|
||||
// Please refer to corresponding tests for more details.
|
||||
func (m KVMap[K, V]) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(gconv.Map(m.Map()))
|
||||
}
|
||||
|
||||
@ -27,10 +27,9 @@ import (
|
||||
//
|
||||
// Reference: http://en.wikipedia.org/wiki/Associative_array
|
||||
type ListKVMap[K comparable, V any] struct {
|
||||
mu rwmutex.RWMutex
|
||||
data map[K]*glist.TElement[*gListKVMapNode[K, V]]
|
||||
list *glist.TList[*gListKVMapNode[K, V]]
|
||||
nilChecker NilChecker[V]
|
||||
mu rwmutex.RWMutex
|
||||
data map[K]*glist.TElement[*gListKVMapNode[K, V]]
|
||||
list *glist.TList[*gListKVMapNode[K, V]]
|
||||
}
|
||||
|
||||
type gListKVMapNode[K comparable, V any] struct {
|
||||
@ -50,16 +49,6 @@ func NewListKVMap[K comparable, V any](safe ...bool) *ListKVMap[K, V] {
|
||||
}
|
||||
}
|
||||
|
||||
// NewListKVMapWithChecker creates and returns a new ListKVMap instance with a custom nil checker.
|
||||
// The parameter `checker` is a function used to determine if a value is nil.
|
||||
// The parameter `safe` is used to specify whether using map in concurrent-safety,
|
||||
// which is false by default.
|
||||
func NewListKVMapWithChecker[K comparable, V any](checker NilChecker[V], safe ...bool) *ListKVMap[K, V] {
|
||||
m := NewListKVMap[K, V](safe...)
|
||||
m.SetNilChecker(checker)
|
||||
return m
|
||||
}
|
||||
|
||||
// NewListKVMapFrom returns a link map from given map `data`.
|
||||
// Note that, the param `data` map will be copied to the underlying data structure,
|
||||
// so changes to the original map will not affect the link map.
|
||||
@ -69,38 +58,6 @@ func NewListKVMapFrom[K comparable, V any](data map[K]V, safe ...bool) *ListKVMa
|
||||
return m
|
||||
}
|
||||
|
||||
// NewListKVMapWithCheckerFrom returns a link map from given map `data` with a custom nil checker.
|
||||
// Note that, the param `data` map will be copied to the underlying data structure,
|
||||
// so changes to the original map will not affect the link map.
|
||||
// The parameter `checker` is a function used to determine if a value is nil.
|
||||
// The parameter `safe` is used to specify whether using map in concurrent-safety,
|
||||
// which is false by default.
|
||||
func NewListKVMapWithCheckerFrom[K comparable, V any](data map[K]V, nilChecker NilChecker[V], safe ...bool) *ListKVMap[K, V] {
|
||||
m := NewListKVMapWithChecker[K, V](nilChecker, safe...)
|
||||
m.Sets(data)
|
||||
return m
|
||||
}
|
||||
|
||||
// SetNilChecker registers a custom nil checker function for the map values.
|
||||
// This function is used to determine if a value should be considered as nil.
|
||||
// The nil checker function takes a value of type V and returns a boolean indicating
|
||||
// whether the value should be treated as nil.
|
||||
func (m *ListKVMap[K, V]) SetNilChecker(nilChecker NilChecker[V]) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.nilChecker = nilChecker
|
||||
}
|
||||
|
||||
// isNil checks whether the given value is nil.
|
||||
// It first checks if a custom nil checker function is registered and uses it if available,
|
||||
// otherwise it falls back to the default empty.IsNil function.
|
||||
func (m *ListKVMap[K, V]) isNil(v V) bool {
|
||||
if m.nilChecker != nil {
|
||||
return m.nilChecker(v)
|
||||
}
|
||||
return empty.IsNil(v)
|
||||
}
|
||||
|
||||
// Iterator is alias of IteratorAsc.
|
||||
func (m *ListKVMap[K, V]) Iterator(f func(key K, value V) bool) {
|
||||
m.IteratorAsc(f)
|
||||
@ -325,9 +282,7 @@ func (m *ListKVMap[K, V]) doSetWithLockCheckWithoutLock(key K, value V) V {
|
||||
if e, ok := m.data[key]; ok {
|
||||
return e.Value.value
|
||||
}
|
||||
if !m.isNil(value) {
|
||||
m.data[key] = m.list.PushBack(&gListKVMapNode[K, V]{key, value})
|
||||
}
|
||||
m.data[key] = m.list.PushBack(&gListKVMapNode[K, V]{key, value})
|
||||
return value
|
||||
}
|
||||
|
||||
@ -370,9 +325,7 @@ func (m *ListKVMap[K, V]) GetOrSetFuncLock(key K, f func() V) V {
|
||||
return e.Value.value
|
||||
}
|
||||
value := f()
|
||||
if !m.isNil(value) {
|
||||
m.data[key] = m.list.PushBack(&gListKVMapNode[K, V]{key, value})
|
||||
}
|
||||
m.data[key] = m.list.PushBack(&gListKVMapNode[K, V]{key, value})
|
||||
return value
|
||||
}
|
||||
|
||||
@ -402,8 +355,6 @@ func (m *ListKVMap[K, V]) GetVarOrSetFuncLock(key K, f func() V) *gvar.Var {
|
||||
|
||||
// SetIfNotExist sets `value` to the map if the `key` does not exist, and then returns true.
|
||||
// It returns false if `key` exists, and `value` would be ignored.
|
||||
//
|
||||
// Note that it does not add the value to the map if `value` is nil.
|
||||
func (m *ListKVMap[K, V]) SetIfNotExist(key K, value V) bool {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
@ -415,16 +366,12 @@ func (m *ListKVMap[K, V]) SetIfNotExist(key K, value V) bool {
|
||||
if _, ok := m.data[key]; ok {
|
||||
return false
|
||||
}
|
||||
if !m.isNil(value) {
|
||||
m.data[key] = m.list.PushBack(&gListKVMapNode[K, V]{key, value})
|
||||
}
|
||||
m.data[key] = m.list.PushBack(&gListKVMapNode[K, V]{key, value})
|
||||
return true
|
||||
}
|
||||
|
||||
// SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true.
|
||||
// It returns false if `key` exists, and `value` would be ignored.
|
||||
//
|
||||
// Note that, it does not add the value to the map if the returned value of `f` is nil.
|
||||
func (m *ListKVMap[K, V]) SetIfNotExistFunc(key K, f func() V) bool {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
@ -437,9 +384,7 @@ func (m *ListKVMap[K, V]) SetIfNotExistFunc(key K, f func() V) bool {
|
||||
return false
|
||||
}
|
||||
value := f()
|
||||
if !m.isNil(value) {
|
||||
m.data[key] = m.list.PushBack(&gListKVMapNode[K, V]{key, value})
|
||||
}
|
||||
m.data[key] = m.list.PushBack(&gListKVMapNode[K, V]{key, value})
|
||||
return true
|
||||
}
|
||||
|
||||
@ -448,8 +393,6 @@ func (m *ListKVMap[K, V]) SetIfNotExistFunc(key K, f func() V) bool {
|
||||
//
|
||||
// SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that
|
||||
// it executes function `f` with mutex.Lock of the map.
|
||||
//
|
||||
// Note that, it does not add the value to the map if the returned value of `f` is nil.
|
||||
func (m *ListKVMap[K, V]) SetIfNotExistFuncLock(key K, f func() V) bool {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
@ -462,9 +405,7 @@ func (m *ListKVMap[K, V]) SetIfNotExistFuncLock(key K, f func() V) bool {
|
||||
return false
|
||||
}
|
||||
value := f()
|
||||
if !m.isNil(value) {
|
||||
m.data[key] = m.list.PushBack(&gListKVMapNode[K, V]{key, value})
|
||||
}
|
||||
m.data[key] = m.list.PushBack(&gListKVMapNode[K, V]{key, value})
|
||||
return true
|
||||
}
|
||||
|
||||
@ -615,9 +556,6 @@ func (m *ListKVMap[K, V]) String() string {
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
// DO NOT change this receiver to pointer type, as the ListKVMap can be used as a var defined variable, like:
|
||||
// var m gmap.ListKVMap[string]string
|
||||
// Please refer to corresponding tests for more details.
|
||||
func (m ListKVMap[K, V]) MarshalJSON() (jsonBytes []byte, err error) {
|
||||
if m.data == nil {
|
||||
return []byte("{}"), nil
|
||||
|
||||
@ -898,7 +898,7 @@ func Test_KVMap_GetOrSet_NilValue(t *testing.T) {
|
||||
v := m.GetOrSet("a", nil)
|
||||
t.Assert(v, nil)
|
||||
// nil interface value should not be stored
|
||||
t.Assert(m.Contains("a"), false)
|
||||
t.Assert(m.Contains("a"), true)
|
||||
})
|
||||
}
|
||||
|
||||
@ -910,7 +910,7 @@ func Test_KVMap_GetOrSetFunc_NilValue(t *testing.T) {
|
||||
v := m.GetOrSetFunc("a", func() any { return nil })
|
||||
t.Assert(v, nil)
|
||||
// nil interface value should not be stored
|
||||
t.Assert(m.Contains("a"), false)
|
||||
t.Assert(m.Contains("a"), true)
|
||||
})
|
||||
}
|
||||
|
||||
@ -929,7 +929,7 @@ func Test_KVMap_GetOrSetFuncLock_NilData(t *testing.T) {
|
||||
v := m.GetOrSetFuncLock("a", func() any { return nil })
|
||||
t.Assert(v, nil)
|
||||
// nil interface value should not be stored
|
||||
t.Assert(m.Contains("a"), false)
|
||||
t.Assert(m.Contains("a"), true)
|
||||
})
|
||||
}
|
||||
|
||||
@ -1637,69 +1637,3 @@ func Test_KVMap_Flip_String(t *testing.T) {
|
||||
t.Assert(m.Get("val2"), "key2")
|
||||
})
|
||||
}
|
||||
|
||||
// Test TypedNil with custom nil checker for pointers
|
||||
func Test_KVMap_TypedNil(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type Student struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
m1 := gmap.NewKVMap[int, *Student](true)
|
||||
for i := 0; i < 10; i++ {
|
||||
m1.GetOrSetFuncLock(i, func() *Student {
|
||||
if i%2 == 0 {
|
||||
return &Student{}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
t.Assert(m1.Size(), 5)
|
||||
|
||||
m2 := gmap.NewKVMap[int, *Student](true)
|
||||
m2.SetNilChecker(func(student *Student) bool {
|
||||
return student == nil
|
||||
})
|
||||
for i := 0; i < 10; i++ {
|
||||
m2.GetOrSetFuncLock(i, func() *Student {
|
||||
if i%2 == 0 {
|
||||
return &Student{}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
t.Assert(m2.Size(), 5)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_NewKVMapWithChecker_TypedNil(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type Student struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
m1 := gmap.NewKVMap[int, *Student](true)
|
||||
for i := 0; i < 10; i++ {
|
||||
m1.GetOrSetFuncLock(i, func() *Student {
|
||||
if i%2 == 0 {
|
||||
return &Student{}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
t.Assert(m1.Size(), 5)
|
||||
|
||||
m2 := gmap.NewKVMapWithChecker[int, *Student](func(student *Student) bool {
|
||||
return student == nil
|
||||
}, true)
|
||||
for i := 0; i < 10; i++ {
|
||||
m2.GetOrSetFuncLock(i, func() *Student {
|
||||
if i%2 == 0 {
|
||||
return &Student{}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
t.Assert(m2.Size(), 5)
|
||||
})
|
||||
}
|
||||
|
||||
@ -183,6 +183,77 @@ func Test_ListKVMap_SetIfNotExistFuncLock_MultipleKeys(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
// Test_ListKVMap_GetOrSetFuncLock_NilValue tests that nil values are handled correctly.
|
||||
func Test_ListKVMap_GetOrSetFuncLock_NilValue(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewListKVMap[string, *int](true)
|
||||
key := "nilKey"
|
||||
callCount := int32(0)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
goroutines := 50
|
||||
wg.Add(goroutines)
|
||||
|
||||
for i := 0; i < goroutines; i++ {
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
m.GetOrSetFuncLock(key, func() *int {
|
||||
atomic.AddInt32(&callCount, 1)
|
||||
return nil
|
||||
})
|
||||
}()
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
// Callback should be called once
|
||||
t.Assert(atomic.LoadInt32(&callCount), 1)
|
||||
// Typed nil pointer (*int)(nil) is stored because any(value) != nil for typed nil
|
||||
// This is a Go language feature: typed nil is not the same as interface nil
|
||||
t.Assert(m.Contains(key), true)
|
||||
t.Assert(m.Get(key), (*int)(nil))
|
||||
t.Assert(m.Size(), 1)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_ListKVMap_SetIfNotExistFuncLock_NilValue tests that nil values are handled correctly.
|
||||
func Test_ListKVMap_SetIfNotExistFuncLock_NilValue(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewListKVMap[string, *string](true)
|
||||
key := "nilKey"
|
||||
callCount := int32(0)
|
||||
successCount := int32(0)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
goroutines := 50
|
||||
wg.Add(goroutines)
|
||||
|
||||
for i := 0; i < goroutines; i++ {
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
success := m.SetIfNotExistFuncLock(key, func() *string {
|
||||
atomic.AddInt32(&callCount, 1)
|
||||
return nil
|
||||
})
|
||||
if success {
|
||||
atomic.AddInt32(&successCount, 1)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
// Callback should be called once
|
||||
t.Assert(atomic.LoadInt32(&callCount), 1)
|
||||
// Should report success once
|
||||
t.Assert(atomic.LoadInt32(&successCount), 1)
|
||||
// Typed nil pointer (*string)(nil) is stored because any(value) != nil for typed nil
|
||||
t.Assert(m.Contains(key), true)
|
||||
t.Assert(m.Get(key), (*string)(nil))
|
||||
t.Assert(m.Size(), 1)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_ListKVMap_GetOrSetFuncLock_ExistingKey tests behavior when key already exists.
|
||||
func Test_ListKVMap_GetOrSetFuncLock_ExistingKey(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
|
||||
@ -817,7 +817,7 @@ func Test_ListKVMap_GetOrSet_NilValue(t *testing.T) {
|
||||
v := m.GetOrSet("a", nil)
|
||||
t.Assert(v, nil)
|
||||
// nil interface value should not be stored
|
||||
t.Assert(m.Contains("a"), false)
|
||||
t.Assert(m.Contains("a"), true)
|
||||
})
|
||||
}
|
||||
|
||||
@ -1159,13 +1159,6 @@ func Test_ListKVMap_MarshalJSON_Error(t *testing.T) {
|
||||
t.AssertNil(err)
|
||||
t.Assert(string(b), `{"a":"1"}`)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var m gmap.ListKVMap[int, int]
|
||||
m.Set(1, 10)
|
||||
b, err := json.Marshal(m)
|
||||
t.AssertNil(err)
|
||||
t.Assert(string(b), `{"1":10}`)
|
||||
})
|
||||
}
|
||||
|
||||
// Test empty map operations
|
||||
@ -1299,7 +1292,7 @@ func Test_ListKVMap_GetOrSetFuncLock_NilData(t *testing.T) {
|
||||
v := m.GetOrSetFuncLock("a", func() any { return nil })
|
||||
t.Assert(v, nil)
|
||||
// nil interface value should not be stored
|
||||
t.Assert(m.Contains("a"), false)
|
||||
t.Assert(m.Contains("a"), true)
|
||||
})
|
||||
}
|
||||
|
||||
@ -1348,69 +1341,3 @@ func Test_ListKVMap_UnmarshalValue_NilData(t *testing.T) {
|
||||
t.Assert(m.Get("b"), "2")
|
||||
})
|
||||
}
|
||||
|
||||
// Test typed nil values
|
||||
func Test_ListKVMap_TypedNil(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type Student struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
m1 := gmap.NewListKVMap[int, *Student](true)
|
||||
for i := 0; i < 10; i++ {
|
||||
m1.GetOrSetFuncLock(i, func() *Student {
|
||||
if i%2 == 0 {
|
||||
return &Student{}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
t.Assert(m1.Size(), 5)
|
||||
|
||||
m2 := gmap.NewListKVMap[int, *Student](true)
|
||||
m2.SetNilChecker(func(student *Student) bool {
|
||||
return student == nil
|
||||
})
|
||||
for i := 0; i < 10; i++ {
|
||||
m2.GetOrSetFuncLock(i, func() *Student {
|
||||
if i%2 == 0 {
|
||||
return &Student{}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
t.Assert(m2.Size(), 5)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_NewListKVMapWithChecker_TypedNil(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type Student struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
m1 := gmap.NewListKVMap[int, *Student](true)
|
||||
for i := 0; i < 10; i++ {
|
||||
m1.GetOrSetFuncLock(i, func() *Student {
|
||||
if i%2 == 0 {
|
||||
return &Student{}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
t.Assert(m1.Size(), 5)
|
||||
|
||||
m2 := gmap.NewListKVMapWithChecker[int, *Student](func(student *Student) bool {
|
||||
return student == nil
|
||||
}, true)
|
||||
for i := 0; i < 10; i++ {
|
||||
m2.GetOrSetFuncLock(i, func() *Student {
|
||||
if i%2 == 0 {
|
||||
return &Student{}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
t.Assert(m2.Size(), 5)
|
||||
})
|
||||
}
|
||||
|
||||
@ -86,7 +86,7 @@ func (set *Set) AddIfNotExistFunc(item any, f func() bool) bool {
|
||||
}
|
||||
|
||||
// AddIfNotExistFuncLock checks whether item exists in the set,
|
||||
// it adds the item to set and returns true if it does not exists in the set and
|
||||
// it adds the item to set and returns true if it does not exist in the set and
|
||||
// function `f` returns true, or else it does nothing and returns false.
|
||||
//
|
||||
// Note that, if `item` is nil, it does nothing and returns false. The function `f`
|
||||
|
||||
@ -9,21 +9,16 @@ package gset
|
||||
import (
|
||||
"bytes"
|
||||
|
||||
"github.com/gogf/gf/v2/internal/empty"
|
||||
"github.com/gogf/gf/v2/internal/json"
|
||||
"github.com/gogf/gf/v2/internal/rwmutex"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
// NilChecker is a function that checks whether the given value is nil.
|
||||
type NilChecker[T any] func(T) bool
|
||||
|
||||
// TSet[T] is consisted of any items.
|
||||
// TSet is a generic set implementation that holds unique items of type T.
|
||||
type TSet[T comparable] struct {
|
||||
mu rwmutex.RWMutex
|
||||
data map[T]struct{}
|
||||
nilChecker NilChecker[T]
|
||||
mu rwmutex.RWMutex
|
||||
data map[T]struct{}
|
||||
}
|
||||
|
||||
// NewTSet creates and returns a new set, which contains un-repeated items.
|
||||
@ -35,15 +30,6 @@ func NewTSet[T comparable](safe ...bool) *TSet[T] {
|
||||
}
|
||||
}
|
||||
|
||||
// NewTSetWithChecker creates and returns a new set with a custom nil checker.
|
||||
// The parameter `nilChecker` is a function used to determine if a value is nil.
|
||||
// The parameter `safe` is used to specify whether using set in concurrent-safety mode.
|
||||
func NewTSetWithChecker[T comparable](checker NilChecker[T], safe ...bool) *TSet[T] {
|
||||
s := NewTSet[T](safe...)
|
||||
s.SetNilChecker(checker)
|
||||
return s
|
||||
}
|
||||
|
||||
// NewTSetFrom returns a new set from `items`.
|
||||
// `items` - A slice of type T.
|
||||
func NewTSetFrom[T comparable](items []T, safe ...bool) *TSet[T] {
|
||||
@ -57,36 +43,6 @@ func NewTSetFrom[T comparable](items []T, safe ...bool) *TSet[T] {
|
||||
}
|
||||
}
|
||||
|
||||
// NewTSetWithCheckerFrom returns a new set from `items` with a custom nil checker.
|
||||
// The parameter `items` is a slice of elements to be added to the set.
|
||||
// The parameter `checker` is a function used to determine if a value is nil.
|
||||
// The parameter `safe` is used to specify whether using set in concurrent-safety mode.
|
||||
func NewTSetWithCheckerFrom[T comparable](items []T, checker NilChecker[T], safe ...bool) *TSet[T] {
|
||||
set := NewTSetWithChecker[T](checker, safe...)
|
||||
set.Add(items...)
|
||||
return set
|
||||
}
|
||||
|
||||
// SetNilChecker registers a custom nil checker function for the set elements.
|
||||
// This function is used to determine if an element should be considered as nil.
|
||||
// The nil checker function takes an element of type T and returns a boolean indicating
|
||||
// whether the element should be treated as nil.
|
||||
func (set *TSet[T]) SetNilChecker(nilChecker NilChecker[T]) {
|
||||
set.mu.Lock()
|
||||
defer set.mu.Unlock()
|
||||
set.nilChecker = nilChecker
|
||||
}
|
||||
|
||||
// isNil checks whether the given value is nil.
|
||||
// It first checks if a custom nil checker function is registered and uses it if available,
|
||||
// otherwise it falls back to the default empty.IsNil function.
|
||||
func (set *TSet[T]) isNil(v T) bool {
|
||||
if set.nilChecker != nil {
|
||||
return set.nilChecker(v)
|
||||
}
|
||||
return empty.IsNil(v)
|
||||
}
|
||||
|
||||
// Iterator iterates the set readonly with given callback function `f`,
|
||||
// if `f` returns true then continue iterating; or false to stop.
|
||||
func (set *TSet[T]) Iterator(f func(v T) bool) {
|
||||
@ -110,14 +66,11 @@ func (set *TSet[T]) Add(items ...T) {
|
||||
}
|
||||
|
||||
// AddIfNotExist checks whether item exists in the set,
|
||||
// it adds the item to set and returns true if it does not exist in the set,
|
||||
// it adds the item to set and returns true if it does not exists in the set,
|
||||
// or else it does nothing and returns false.
|
||||
//
|
||||
// Note that, if `item` is nil, it does nothing and returns false.
|
||||
func (set *TSet[T]) AddIfNotExist(item T) bool {
|
||||
if set.isNil(item) {
|
||||
return false
|
||||
}
|
||||
if !set.Contains(item) {
|
||||
set.mu.Lock()
|
||||
defer set.mu.Unlock()
|
||||
@ -139,9 +92,6 @@ func (set *TSet[T]) AddIfNotExist(item T) bool {
|
||||
// Note that, if `item` is nil, it does nothing and returns false. The function `f`
|
||||
// is executed without writing lock.
|
||||
func (set *TSet[T]) AddIfNotExistFunc(item T, f func() bool) bool {
|
||||
if set.isNil(item) {
|
||||
return false
|
||||
}
|
||||
if !set.Contains(item) {
|
||||
if f() {
|
||||
set.mu.Lock()
|
||||
@ -159,15 +109,12 @@ func (set *TSet[T]) AddIfNotExistFunc(item T, f func() bool) bool {
|
||||
}
|
||||
|
||||
// AddIfNotExistFuncLock checks whether item exists in the set,
|
||||
// it adds the item to set and returns true if it does not exists in the set and
|
||||
// it adds the item to set and returns true if it does not exist in the set and
|
||||
// function `f` returns true, or else it does nothing and returns false.
|
||||
//
|
||||
// Note that, if `item` is nil, it does nothing and returns false. The function `f`
|
||||
// is executed within writing lock.
|
||||
func (set *TSet[T]) AddIfNotExistFuncLock(item T, f func() bool) bool {
|
||||
if set.isNil(item) {
|
||||
return false
|
||||
}
|
||||
if !set.Contains(item) {
|
||||
set.mu.Lock()
|
||||
defer set.mu.Unlock()
|
||||
|
||||
@ -419,6 +419,7 @@ func TestSet_AddIfNotExist(t *testing.T) {
|
||||
t.Assert(s.AddIfNotExist(2), true)
|
||||
t.Assert(s.Contains(2), true)
|
||||
t.Assert(s.AddIfNotExist(2), false)
|
||||
t.Assert(s.AddIfNotExist(nil), true)
|
||||
t.Assert(s.AddIfNotExist(nil), false)
|
||||
t.Assert(s.Contains(2), true)
|
||||
})
|
||||
@ -497,7 +498,18 @@ func TestSet_AddIfNotExistFuncLock(t *testing.T) {
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s := gset.New(true)
|
||||
t.Assert(s.AddIfNotExistFuncLock(nil, func() bool { return true }), false)
|
||||
t.Assert(
|
||||
s.AddIfNotExistFuncLock(nil, func() bool {
|
||||
return true
|
||||
}),
|
||||
true,
|
||||
)
|
||||
t.Assert(
|
||||
s.AddIfNotExistFuncLock(nil, func() bool {
|
||||
return true
|
||||
}),
|
||||
false,
|
||||
)
|
||||
s1 := gset.Set{}
|
||||
t.Assert(s1.AddIfNotExistFuncLock(1, func() bool { return true }), true)
|
||||
})
|
||||
|
||||
@ -591,42 +591,3 @@ func TestTSet_RLockFunc(t *testing.T) {
|
||||
t.Assert(sum, 6)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_TSet_TypedNil(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type Student struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
set := gset.NewTSet[*Student](true)
|
||||
var s *Student = nil
|
||||
exist := set.AddIfNotExist(s)
|
||||
t.Assert(exist, false)
|
||||
|
||||
set2 := gset.NewTSet[*Student](true)
|
||||
set2.SetNilChecker(func(student *Student) bool {
|
||||
return student == nil
|
||||
})
|
||||
exist2 := set2.AddIfNotExist(s)
|
||||
t.Assert(exist2, false)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_NewTSetWithChecker_TypedNil(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type Student struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
set := gset.NewTSet[*Student](true)
|
||||
var s *Student = nil
|
||||
exist := set.AddIfNotExist(s)
|
||||
t.Assert(exist, false)
|
||||
|
||||
set2 := gset.NewTSetWithChecker[*Student](func(student *Student) bool {
|
||||
return student == nil
|
||||
}, true)
|
||||
exist2 := set2.AddIfNotExist(s)
|
||||
t.Assert(exist2, false)
|
||||
})
|
||||
}
|
||||
|
||||
@ -12,22 +12,17 @@ import (
|
||||
"github.com/emirpasic/gods/v2/trees/avltree"
|
||||
|
||||
"github.com/gogf/gf/v2/container/gvar"
|
||||
"github.com/gogf/gf/v2/internal/empty"
|
||||
"github.com/gogf/gf/v2/internal/json"
|
||||
"github.com/gogf/gf/v2/internal/rwmutex"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
// NilChecker is a function that checks whether the given value is nil.
|
||||
type NilChecker[V any] func(V) bool
|
||||
|
||||
// AVLKVTree holds elements of the AVL tree.
|
||||
type AVLKVTree[K comparable, V any] struct {
|
||||
mu rwmutex.RWMutex
|
||||
comparator func(v1, v2 K) int
|
||||
tree *avltree.Tree[K, V]
|
||||
nilChecker NilChecker[V]
|
||||
}
|
||||
|
||||
// AVLKVTreeNode is a single element within the tree.
|
||||
@ -48,15 +43,6 @@ func NewAVLKVTree[K comparable, V any](comparator func(v1, v2 K) int, safe ...bo
|
||||
}
|
||||
}
|
||||
|
||||
// NewAVLKVTreeWithChecker instantiates an AVL tree with the custom key comparator and nil checker.
|
||||
// The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default.
|
||||
// The parameter `checker` is used to specify whether the given value is nil.
|
||||
func NewAVLKVTreeWithChecker[K comparable, V any](comparator func(v1, v2 K) int, checker NilChecker[V], safe ...bool) *AVLKVTree[K, V] {
|
||||
t := NewAVLKVTree[K, V](comparator, safe...)
|
||||
t.SetNilChecker(checker)
|
||||
return t
|
||||
}
|
||||
|
||||
// NewAVLKVTreeFrom instantiates an AVL tree with the custom key comparator and data map.
|
||||
//
|
||||
// The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default.
|
||||
@ -68,37 +54,6 @@ func NewAVLKVTreeFrom[K comparable, V any](comparator func(v1, v2 K) int, data m
|
||||
return tree
|
||||
}
|
||||
|
||||
// NewAVLKVTreeWithCheckerFrom instantiates an AVL tree with the custom key comparator, nil checker and data map.
|
||||
// The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default.
|
||||
// The parameter `checker` is used to specify whether the given value is nil.
|
||||
func NewAVLKVTreeWithCheckerFrom[K comparable, V any](comparator func(v1, v2 K) int, data map[K]V, checker NilChecker[V], safe ...bool) *AVLKVTree[K, V] {
|
||||
tree := NewAVLKVTreeWithChecker[K, V](comparator, checker, safe...)
|
||||
for k, v := range data {
|
||||
tree.doSet(k, v)
|
||||
}
|
||||
return tree
|
||||
}
|
||||
|
||||
// SetNilChecker registers a custom nil checker function for the map values.
|
||||
// This function is used to determine if a value should be considered as nil.
|
||||
// The nil checker function takes a value of type V and returns a boolean indicating
|
||||
// whether the value should be treated as nil.
|
||||
func (tree *AVLKVTree[K, V]) SetNilChecker(nilChecker NilChecker[V]) {
|
||||
tree.mu.Lock()
|
||||
defer tree.mu.Unlock()
|
||||
tree.nilChecker = nilChecker
|
||||
}
|
||||
|
||||
// isNil checks whether the given value is nil.
|
||||
// It first checks if a custom nil checker function is registered and uses it if available,
|
||||
// otherwise it falls back to the default empty.IsNil function.
|
||||
func (tree *AVLKVTree[K, V]) isNil(v V) bool {
|
||||
if tree.nilChecker != nil {
|
||||
return tree.nilChecker(v)
|
||||
}
|
||||
return empty.IsNil(v)
|
||||
}
|
||||
|
||||
// Clone clones and returns a new tree from current tree.
|
||||
func (tree *AVLKVTree[K, V]) Clone() *AVLKVTree[K, V] {
|
||||
if tree == nil {
|
||||
@ -563,9 +518,6 @@ func (tree *AVLKVTree[K, V]) Flip(comparator ...func(v1, v2 K) int) {
|
||||
//
|
||||
// It returns value with given `key`.
|
||||
func (tree *AVLKVTree[K, V]) doSet(key K, value V) V {
|
||||
if tree.isNil(value) {
|
||||
return value
|
||||
}
|
||||
tree.tree.Put(key, value)
|
||||
return value
|
||||
}
|
||||
|
||||
@ -12,7 +12,6 @@ import (
|
||||
"github.com/emirpasic/gods/v2/trees/btree"
|
||||
|
||||
"github.com/gogf/gf/v2/container/gvar"
|
||||
"github.com/gogf/gf/v2/internal/empty"
|
||||
"github.com/gogf/gf/v2/internal/json"
|
||||
"github.com/gogf/gf/v2/internal/rwmutex"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
@ -25,7 +24,6 @@ type BKVTree[K comparable, V any] struct {
|
||||
comparator func(v1, v2 K) int
|
||||
m int // order (maximum number of children)
|
||||
tree *btree.Tree[K, V]
|
||||
nilChecker NilChecker[V]
|
||||
}
|
||||
|
||||
// BKVTreeEntry represents the key-value pair contained within nodes.
|
||||
@ -47,15 +45,6 @@ func NewBKVTree[K comparable, V any](m int, comparator func(v1, v2 K) int, safe
|
||||
}
|
||||
}
|
||||
|
||||
// NewBKVTreeWithChecker instantiates a B-tree with `m` (maximum number of children), a custom key comparator and nil checker.
|
||||
// The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default.
|
||||
// The parameter `checker` is used to specify whether the given value is nil.
|
||||
func NewBKVTreeWithChecker[K comparable, V any](m int, comparator func(v1, v2 K) int, checker NilChecker[V], safe ...bool) *BKVTree[K, V] {
|
||||
t := NewBKVTree[K, V](m, comparator, safe...)
|
||||
t.SetNilChecker(checker)
|
||||
return t
|
||||
}
|
||||
|
||||
// NewBKVTreeFrom instantiates a B-tree with `m` (maximum number of children), a custom key comparator and data map.
|
||||
// The parameter `safe` is used to specify whether using tree in concurrent-safety,
|
||||
// which is false in default.
|
||||
@ -67,37 +56,6 @@ func NewBKVTreeFrom[K comparable, V any](m int, comparator func(v1, v2 K) int, d
|
||||
return tree
|
||||
}
|
||||
|
||||
// NewBKVTreeWithCheckerFrom instantiates a B-tree with `m` (maximum number of children), a custom key comparator, nil checker and data map.
|
||||
// The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default.
|
||||
// The parameter `checker` is used to specify whether the given value is nil.
|
||||
func NewBKVTreeWithCheckerFrom[K comparable, V any](m int, comparator func(v1, v2 K) int, data map[K]V, checker NilChecker[V], safe ...bool) *BKVTree[K, V] {
|
||||
tree := NewBKVTreeWithChecker[K, V](m, comparator, checker, safe...)
|
||||
for k, v := range data {
|
||||
tree.doSet(k, v)
|
||||
}
|
||||
return tree
|
||||
}
|
||||
|
||||
// SetNilChecker registers a custom nil checker function for the map values.
|
||||
// This function is used to determine if a value should be considered as nil.
|
||||
// The nil checker function takes a value of type V and returns a boolean indicating
|
||||
// whether the value should be treated as nil.
|
||||
func (tree *BKVTree[K, V]) SetNilChecker(nilChecker NilChecker[V]) {
|
||||
tree.mu.Lock()
|
||||
defer tree.mu.Unlock()
|
||||
tree.nilChecker = nilChecker
|
||||
}
|
||||
|
||||
// isNil checks whether the given value is nil.
|
||||
// It first checks if a custom nil checker function is registered and uses it if available,
|
||||
// otherwise it falls back to the default empty.IsNil function.
|
||||
func (tree *BKVTree[K, V]) isNil(v V) bool {
|
||||
if tree.nilChecker != nil {
|
||||
return tree.nilChecker(v)
|
||||
}
|
||||
return empty.IsNil(v)
|
||||
}
|
||||
|
||||
// Clone clones and returns a new tree from current tree.
|
||||
func (tree *BKVTree[K, V]) Clone() *BKVTree[K, V] {
|
||||
if tree == nil {
|
||||
@ -495,9 +453,6 @@ func (tree *BKVTree[K, V]) Right() *BKVTreeEntry[K, V] {
|
||||
//
|
||||
// It returns value with given `key`.
|
||||
func (tree *BKVTree[K, V]) doSet(key K, value V) V {
|
||||
if tree.isNil(value) {
|
||||
return value
|
||||
}
|
||||
tree.tree.Put(key, value)
|
||||
return value
|
||||
}
|
||||
|
||||
@ -12,7 +12,6 @@ import (
|
||||
"github.com/emirpasic/gods/v2/trees/redblacktree"
|
||||
|
||||
"github.com/gogf/gf/v2/container/gvar"
|
||||
"github.com/gogf/gf/v2/internal/empty"
|
||||
"github.com/gogf/gf/v2/internal/json"
|
||||
"github.com/gogf/gf/v2/internal/rwmutex"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
@ -25,7 +24,6 @@ type RedBlackKVTree[K comparable, V any] struct {
|
||||
mu rwmutex.RWMutex
|
||||
comparator func(v1, v2 K) int
|
||||
tree *redblacktree.Tree[K, V]
|
||||
nilChecker NilChecker[V]
|
||||
}
|
||||
|
||||
// RedBlackKVTreeNode is a single element within the tree.
|
||||
@ -43,15 +41,6 @@ func NewRedBlackKVTree[K comparable, V any](comparator func(v1, v2 K) int, safe
|
||||
return &tree
|
||||
}
|
||||
|
||||
// NewRedBlackKVTreeWithChecker instantiates a red-black tree with the custom key comparator and `nilChecker`.
|
||||
// The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default.
|
||||
// The parameter `checker` is used to specify whether the given value is nil.
|
||||
func NewRedBlackKVTreeWithChecker[K comparable, V any](comparator func(v1, v2 K) int, checker NilChecker[V], safe ...bool) *RedBlackKVTree[K, V] {
|
||||
t := NewRedBlackKVTree[K, V](comparator, safe...)
|
||||
t.SetNilChecker(checker)
|
||||
return t
|
||||
}
|
||||
|
||||
// NewRedBlackKVTreeFrom instantiates a red-black tree with the custom key comparator and `data` map.
|
||||
// The parameter `safe` is used to specify whether using tree in concurrent-safety,
|
||||
// which is false in default.
|
||||
@ -61,17 +50,6 @@ func NewRedBlackKVTreeFrom[K comparable, V any](comparator func(v1, v2 K) int, d
|
||||
return &tree
|
||||
}
|
||||
|
||||
// NewRedBlackKVTreeWithCheckerFrom instantiates a red-black tree with the custom key comparator, `data` map and `nilChecker`.
|
||||
// The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default.
|
||||
// The parameter `checker` is used to specify whether the given value is nil.
|
||||
func NewRedBlackKVTreeWithCheckerFrom[K comparable, V any](comparator func(v1, v2 K) int, data map[K]V, checker NilChecker[V], safe ...bool) *RedBlackKVTree[K, V] {
|
||||
t := NewRedBlackKVTreeWithChecker[K, V](comparator, checker, safe...)
|
||||
for k, v := range data {
|
||||
t.doSet(k, v)
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
// RedBlackKVTreeInit instantiates a red-black tree with the custom key comparator.
|
||||
// The parameter `safe` is used to specify whether using tree in concurrent-safety,
|
||||
// which is false in default.
|
||||
@ -97,26 +75,6 @@ func RedBlackKVTreeInitFrom[K comparable, V any](tree *RedBlackKVTree[K, V], com
|
||||
}
|
||||
}
|
||||
|
||||
// SetNilChecker registers a custom nil checker function for the map values.
|
||||
// This function is used to determine if a value should be considered as nil.
|
||||
// The nil checker function takes a value of type V and returns a boolean indicating
|
||||
// whether the value should be treated as nil.
|
||||
func (tree *RedBlackKVTree[K, V]) SetNilChecker(nilChecker NilChecker[V]) {
|
||||
tree.mu.Lock()
|
||||
defer tree.mu.Unlock()
|
||||
tree.nilChecker = nilChecker
|
||||
}
|
||||
|
||||
// isNil checks whether the given value is nil.
|
||||
// It first checks if a custom nil checker function is registered and uses it if available,
|
||||
// otherwise it falls back to the default empty.IsNil function.
|
||||
func (tree *RedBlackKVTree[K, V]) isNil(v V) bool {
|
||||
if tree.nilChecker != nil {
|
||||
return tree.nilChecker(v)
|
||||
}
|
||||
return empty.IsNil(v)
|
||||
}
|
||||
|
||||
// SetComparator sets/changes the comparator for sorting.
|
||||
func (tree *RedBlackKVTree[K, V]) SetComparator(comparator func(a, b K) int) {
|
||||
tree.comparator = comparator
|
||||
@ -634,9 +592,6 @@ func (tree *RedBlackKVTree[K, V]) UnmarshalValue(value any) (err error) {
|
||||
//
|
||||
// It returns value with given `key`.
|
||||
func (tree *RedBlackKVTree[K, V]) doSet(key K, value V) (ret V) {
|
||||
if tree.isNil(value) {
|
||||
return
|
||||
}
|
||||
tree.tree.Put(key, value)
|
||||
return value
|
||||
}
|
||||
|
||||
@ -1,215 +0,0 @@
|
||||
// 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 gtree_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/v2/container/gtree"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
"github.com/gogf/gf/v2/util/gutil"
|
||||
)
|
||||
|
||||
func Test_KVAVLTree_TypedNil(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type Student struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
avlTree := gtree.NewAVLKVTree[int, *Student](gutil.ComparatorTStr[int], true)
|
||||
for i := 0; i < 10; i++ {
|
||||
if i%2 == 0 {
|
||||
avlTree.Set(i, &Student{})
|
||||
} else {
|
||||
var s *Student = nil
|
||||
avlTree.Set(i, s)
|
||||
}
|
||||
}
|
||||
t.Assert(avlTree.Size(), 5)
|
||||
|
||||
avlTree2 := gtree.NewAVLKVTree[int, *Student](gutil.ComparatorTStr[int], true)
|
||||
avlTree2.SetNilChecker(func(student *Student) bool {
|
||||
return student == nil
|
||||
})
|
||||
for i := 0; i < 10; i++ {
|
||||
if i%2 == 0 {
|
||||
avlTree2.Set(i, &Student{})
|
||||
} else {
|
||||
var s *Student = nil
|
||||
avlTree2.Set(i, s)
|
||||
}
|
||||
}
|
||||
t.Assert(avlTree2.Size(), 5)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func Test_KVBTree_TypedNil(t *testing.T) {
|
||||
type Student struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
btree := gtree.NewBKVTree[int, *Student](100, gutil.ComparatorTStr[int], true)
|
||||
for i := 0; i < 10; i++ {
|
||||
if i%2 == 0 {
|
||||
btree.Set(i, &Student{})
|
||||
} else {
|
||||
var s *Student = nil
|
||||
btree.Set(i, s)
|
||||
}
|
||||
}
|
||||
t.Assert(btree.Size(), 5)
|
||||
|
||||
btree2 := gtree.NewBKVTree[int, *Student](100, gutil.ComparatorTStr[int], true)
|
||||
btree2.SetNilChecker(func(student *Student) bool {
|
||||
return student == nil
|
||||
})
|
||||
for i := 0; i < 10; i++ {
|
||||
if i%2 == 0 {
|
||||
btree2.Set(i, &Student{})
|
||||
} else {
|
||||
var s *Student = nil
|
||||
btree2.Set(i, s)
|
||||
}
|
||||
}
|
||||
t.Assert(btree2.Size(), 5)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func Test_KVRedBlackTree_TypedNil(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type Student struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
redBlackTree := gtree.NewRedBlackKVTree[int, *Student](gutil.ComparatorTStr[int], true)
|
||||
for i := 0; i < 10; i++ {
|
||||
if i%2 == 0 {
|
||||
redBlackTree.Set(i, &Student{})
|
||||
} else {
|
||||
var s *Student = nil
|
||||
redBlackTree.Set(i, s)
|
||||
}
|
||||
}
|
||||
t.Assert(redBlackTree.Size(), 5)
|
||||
|
||||
redBlackTree2 := gtree.NewRedBlackKVTree[int, *Student](gutil.ComparatorTStr[int], true)
|
||||
redBlackTree2.SetNilChecker(func(student *Student) bool {
|
||||
return student == nil
|
||||
})
|
||||
for i := 0; i < 10; i++ {
|
||||
if i%2 == 0 {
|
||||
redBlackTree2.Set(i, &Student{})
|
||||
} else {
|
||||
var s *Student = nil
|
||||
redBlackTree2.Set(i, s)
|
||||
}
|
||||
}
|
||||
t.Assert(redBlackTree2.Size(), 5)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_NewKVAVLTreeWithChecker_TypedNil(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type Student struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
avlTree := gtree.NewAVLKVTree[int, *Student](gutil.ComparatorTStr[int], true)
|
||||
for i := 0; i < 10; i++ {
|
||||
if i%2 == 0 {
|
||||
avlTree.Set(i, &Student{})
|
||||
} else {
|
||||
var s *Student = nil
|
||||
avlTree.Set(i, s)
|
||||
}
|
||||
}
|
||||
t.Assert(avlTree.Size(), 5)
|
||||
|
||||
avlTree2 := gtree.NewAVLKVTreeWithChecker[int, *Student](gutil.ComparatorTStr[int], func(student *Student) bool {
|
||||
return student == nil
|
||||
}, true)
|
||||
for i := 0; i < 10; i++ {
|
||||
if i%2 == 0 {
|
||||
avlTree2.Set(i, &Student{})
|
||||
} else {
|
||||
var s *Student = nil
|
||||
avlTree2.Set(i, s)
|
||||
}
|
||||
}
|
||||
t.Assert(avlTree2.Size(), 5)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func Test_NewKVBTreeWithChecker_TypedNil(t *testing.T) {
|
||||
type Student struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
btree := gtree.NewBKVTree[int, *Student](100, gutil.ComparatorTStr[int], true)
|
||||
for i := 0; i < 10; i++ {
|
||||
if i%2 == 0 {
|
||||
btree.Set(i, &Student{})
|
||||
} else {
|
||||
var s *Student = nil
|
||||
btree.Set(i, s)
|
||||
}
|
||||
}
|
||||
t.Assert(btree.Size(), 5)
|
||||
|
||||
btree2 := gtree.NewBKVTreeWithChecker[int, *Student](100, gutil.ComparatorTStr[int], func(student *Student) bool {
|
||||
return student == nil
|
||||
}, true)
|
||||
for i := 0; i < 10; i++ {
|
||||
if i%2 == 0 {
|
||||
btree2.Set(i, &Student{})
|
||||
} else {
|
||||
var s *Student = nil
|
||||
btree2.Set(i, s)
|
||||
}
|
||||
}
|
||||
t.Assert(btree2.Size(), 5)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func Test_NewRedBlackKVTreeWithChecker_TypedNil(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type Student struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
redBlackTree := gtree.NewRedBlackKVTree[int, *Student](gutil.ComparatorTStr[int], true)
|
||||
for i := 0; i < 10; i++ {
|
||||
if i%2 == 0 {
|
||||
redBlackTree.Set(i, &Student{})
|
||||
} else {
|
||||
var s *Student = nil
|
||||
redBlackTree.Set(i, s)
|
||||
}
|
||||
}
|
||||
t.Assert(redBlackTree.Size(), 5)
|
||||
|
||||
redBlackTree2 := gtree.NewRedBlackKVTreeWithChecker[int, *Student](gutil.ComparatorTStr[int], func(student *Student) bool {
|
||||
return student == nil
|
||||
}, true)
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
if i%2 == 0 {
|
||||
redBlackTree2.Set(i, &Student{})
|
||||
} else {
|
||||
var s *Student = nil
|
||||
redBlackTree2.Set(i, s)
|
||||
}
|
||||
}
|
||||
t.Assert(redBlackTree2.Size(), 5)
|
||||
})
|
||||
}
|
||||
@ -4,7 +4,7 @@ go 1.23.0
|
||||
|
||||
require (
|
||||
github.com/apolloconfig/agollo/v4 v4.3.1
|
||||
github.com/gogf/gf/v2 v2.10.0
|
||||
github.com/gogf/gf/v2 v2.9.8
|
||||
)
|
||||
|
||||
require (
|
||||
|
||||
@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/config/consul/v2
|
||||
go 1.23.0
|
||||
|
||||
require (
|
||||
github.com/gogf/gf/v2 v2.10.0
|
||||
github.com/gogf/gf/v2 v2.9.8
|
||||
github.com/hashicorp/consul/api v1.24.0
|
||||
github.com/hashicorp/go-cleanhttp v0.5.2
|
||||
)
|
||||
|
||||
@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/config/kubecm/v2
|
||||
go 1.24.0
|
||||
|
||||
require (
|
||||
github.com/gogf/gf/v2 v2.10.0
|
||||
github.com/gogf/gf/v2 v2.9.8
|
||||
k8s.io/api v0.33.4
|
||||
k8s.io/apimachinery v0.33.4
|
||||
k8s.io/client-go v0.33.4
|
||||
|
||||
@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/config/nacos/v2
|
||||
go 1.23.0
|
||||
|
||||
require (
|
||||
github.com/gogf/gf/v2 v2.10.0
|
||||
github.com/gogf/gf/v2 v2.9.8
|
||||
github.com/nacos-group/nacos-sdk-go/v2 v2.3.3
|
||||
)
|
||||
|
||||
|
||||
@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/config/polaris/v2
|
||||
go 1.23.0
|
||||
|
||||
require (
|
||||
github.com/gogf/gf/v2 v2.10.0
|
||||
github.com/gogf/gf/v2 v2.9.8
|
||||
github.com/polarismesh/polaris-go v1.6.1
|
||||
)
|
||||
|
||||
|
||||
@ -4,7 +4,7 @@ go 1.23.0
|
||||
|
||||
require (
|
||||
github.com/ClickHouse/clickhouse-go/v2 v2.0.15
|
||||
github.com/gogf/gf/v2 v2.10.0
|
||||
github.com/gogf/gf/v2 v2.9.8
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/shopspring/decimal v1.3.1
|
||||
)
|
||||
|
||||
@ -6,7 +6,7 @@ replace github.com/gogf/gf/v2 => ../../../
|
||||
|
||||
require (
|
||||
gitee.com/chunanyong/dm v1.8.12
|
||||
github.com/gogf/gf/v2 v2.10.0
|
||||
github.com/gogf/gf/v2 v2.9.8
|
||||
)
|
||||
|
||||
require (
|
||||
|
||||
@ -1,102 +0,0 @@
|
||||
// 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 gaussdb_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/os/glog"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
)
|
||||
|
||||
func Test_Ctx(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
db, err := gdb.Instance()
|
||||
t.AssertNil(err)
|
||||
|
||||
err1 := db.PingMaster()
|
||||
err2 := db.PingSlave()
|
||||
t.Assert(err1, nil)
|
||||
t.Assert(err2, nil)
|
||||
|
||||
newDb := db.Ctx(context.Background())
|
||||
t.AssertNE(newDb, nil)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Ctx_Query(t *testing.T) {
|
||||
db.GetLogger().(*glog.Logger).SetCtxKeys("SpanId", "TraceId")
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
db.SetDebug(true)
|
||||
defer db.SetDebug(false)
|
||||
ctx := context.WithValue(context.Background(), "TraceId", "12345678")
|
||||
ctx = context.WithValue(ctx, "SpanId", "0.1")
|
||||
db.Query(ctx, "select 1")
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
db.SetDebug(true)
|
||||
defer db.SetDebug(false)
|
||||
db.Query(ctx, "select 2")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Ctx_Model(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
db.GetLogger().(*glog.Logger).SetCtxKeys("SpanId", "TraceId")
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
db.SetDebug(true)
|
||||
defer db.SetDebug(false)
|
||||
ctx := context.WithValue(context.Background(), "TraceId", "12345678")
|
||||
ctx = context.WithValue(ctx, "SpanId", "0.1")
|
||||
db.Model(table).Ctx(ctx).All()
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
db.SetDebug(true)
|
||||
defer db.SetDebug(false)
|
||||
db.Model(table).All()
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Ctx_Transaction(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
db.GetLogger().(*glog.Logger).SetCtxKeys("SpanId", "TraceId")
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
db.SetDebug(true)
|
||||
defer db.SetDebug(false)
|
||||
ctx := context.WithValue(context.Background(), "TraceId", "tx_trace_123")
|
||||
ctx = context.WithValue(ctx, "SpanId", "0.2")
|
||||
|
||||
err := db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
_, err := tx.Model(table).Ctx(ctx).Where("id", 1).One()
|
||||
return err
|
||||
})
|
||||
t.AssertNil(err)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Ctx_Timeout(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*10)
|
||||
defer cancel()
|
||||
|
||||
// Wait for the context to expire
|
||||
time.Sleep(time.Millisecond * 50)
|
||||
|
||||
// Query with expired context should return error
|
||||
_, err := db.Model(table).Ctx(ctx).All()
|
||||
t.AssertNE(err, nil)
|
||||
})
|
||||
}
|
||||
@ -1,216 +0,0 @@
|
||||
// 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 gaussdb_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/v2/container/gvar"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
)
|
||||
|
||||
func Test_Model_Hook_Select(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := db.Model(table).Hook(gdb.HookHandler{
|
||||
Select: func(ctx context.Context, in *gdb.HookSelectInput) (result gdb.Result, err error) {
|
||||
result, err = in.Next(ctx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for i, record := range result {
|
||||
record["test"] = gvar.New(100 + record["id"].Int())
|
||||
result[i] = record
|
||||
}
|
||||
return
|
||||
},
|
||||
})
|
||||
all, err := m.Where("id > ?", 6).OrderAsc("id").All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(all), 4)
|
||||
t.Assert(all[0]["id"].Int(), 7)
|
||||
t.Assert(all[0]["test"].Int(), 107)
|
||||
t.Assert(all[1]["test"].Int(), 108)
|
||||
t.Assert(all[2]["test"].Int(), 109)
|
||||
t.Assert(all[3]["test"].Int(), 110)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Hook_Insert(t *testing.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := db.Model(table).Hook(gdb.HookHandler{
|
||||
Insert: func(ctx context.Context, in *gdb.HookInsertInput) (result sql.Result, err error) {
|
||||
for i, item := range in.Data {
|
||||
item["passport"] = fmt.Sprintf(`test_port_%d`, item["id"])
|
||||
item["nickname"] = fmt.Sprintf(`test_name_%d`, item["id"])
|
||||
item["password"] = fmt.Sprintf(`test_pass_%d`, item["id"])
|
||||
item["create_time"] = CreateTime
|
||||
in.Data[i] = item
|
||||
}
|
||||
return in.Next(ctx)
|
||||
},
|
||||
})
|
||||
_, err := m.Insert(g.Map{
|
||||
"id": 1,
|
||||
"nickname": "name_1",
|
||||
})
|
||||
t.AssertNil(err)
|
||||
one, err := m.One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["id"].Int(), 1)
|
||||
t.Assert(one["passport"], `test_port_1`)
|
||||
t.Assert(one["nickname"], `test_name_1`)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Hook_Update(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := db.Model(table).Hook(gdb.HookHandler{
|
||||
Update: func(ctx context.Context, in *gdb.HookUpdateInput) (result sql.Result, err error) {
|
||||
switch value := in.Data.(type) {
|
||||
case gdb.List:
|
||||
for i, data := range value {
|
||||
data["passport"] = `port`
|
||||
data["nickname"] = `name`
|
||||
value[i] = data
|
||||
}
|
||||
in.Data = value
|
||||
|
||||
case gdb.Map:
|
||||
value["passport"] = `port`
|
||||
value["nickname"] = `name`
|
||||
in.Data = value
|
||||
}
|
||||
return in.Next(ctx)
|
||||
},
|
||||
})
|
||||
_, err := m.Data(g.Map{
|
||||
"nickname": "name_1",
|
||||
}).WherePri(1).Update()
|
||||
t.AssertNil(err)
|
||||
|
||||
one, err := m.One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["id"].Int(), 1)
|
||||
t.Assert(one["passport"], `port`)
|
||||
t.Assert(one["nickname"], `name`)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Hook_Delete(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := db.Model(table).Hook(gdb.HookHandler{
|
||||
Delete: func(ctx context.Context, in *gdb.HookDeleteInput) (result sql.Result, err error) {
|
||||
return db.Model(table).Data(g.Map{
|
||||
"nickname": `deleted`,
|
||||
}).Where(in.Condition).Update()
|
||||
},
|
||||
})
|
||||
_, err := m.Where("1=1").Delete()
|
||||
t.AssertNil(err)
|
||||
|
||||
all, err := m.All()
|
||||
t.AssertNil(err)
|
||||
for _, item := range all {
|
||||
t.Assert(item["nickname"].String(), `deleted`)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Hook_Select_Count(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := db.Model(table).Hook(gdb.HookHandler{
|
||||
Select: func(ctx context.Context, in *gdb.HookSelectInput) (result gdb.Result, err error) {
|
||||
result, err = in.Next(ctx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
// Adding extra fields should not affect Count operations
|
||||
for i, record := range result {
|
||||
record["extra"] = gvar.New("extra_value")
|
||||
result[i] = record
|
||||
}
|
||||
return
|
||||
},
|
||||
})
|
||||
count, err := m.Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(count, TableSize)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Hook_Chain(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
// Normal chain: two hooks both modify data
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := db.Model(table).Hook(gdb.HookHandler{
|
||||
Select: func(ctx context.Context, in *gdb.HookSelectInput) (result gdb.Result, err error) {
|
||||
result, err = in.Next(ctx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for i, record := range result {
|
||||
record["hook1"] = gvar.New("value1")
|
||||
result[i] = record
|
||||
}
|
||||
return
|
||||
},
|
||||
}).Hook(gdb.HookHandler{
|
||||
Select: func(ctx context.Context, in *gdb.HookSelectInput) (result gdb.Result, err error) {
|
||||
result, err = in.Next(ctx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for i, record := range result {
|
||||
record["hook2"] = gvar.New("value2")
|
||||
result[i] = record
|
||||
}
|
||||
return
|
||||
},
|
||||
})
|
||||
all, err := m.Where("id", 1).All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(all), 1)
|
||||
t.Assert(all[0]["id"].Int(), 1)
|
||||
// The last Hook should take effect (Hook replaces previous one)
|
||||
t.Assert(all[0]["hook2"].String(), "value2")
|
||||
})
|
||||
|
||||
// Error chain: hook returns error
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := db.Model(table).Hook(gdb.HookHandler{
|
||||
Select: func(ctx context.Context, in *gdb.HookSelectInput) (result gdb.Result, err error) {
|
||||
return nil, gerror.New("hook error")
|
||||
},
|
||||
})
|
||||
_, err := m.Where("id", 1).All()
|
||||
t.AssertNE(err, nil)
|
||||
t.Assert(err.Error(), "hook error")
|
||||
})
|
||||
}
|
||||
@ -1,141 +0,0 @@
|
||||
// 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 gaussdb_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/v2/encoding/gjson"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
"github.com/gogf/gf/v2/util/gmeta"
|
||||
)
|
||||
|
||||
func Test_Model_Builder(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := db.Model(table)
|
||||
b := m.Builder()
|
||||
|
||||
all, err := m.Where(
|
||||
b.Where("id", g.Slice{1, 2, 3}).WhereOr("id", g.Slice{4, 5, 6}),
|
||||
).All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(all), 6)
|
||||
})
|
||||
|
||||
// Where And
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := db.Model(table)
|
||||
b := m.Builder()
|
||||
|
||||
all, err := m.Where(
|
||||
b.Where("id", g.Slice{1, 2, 3}).WhereOr("id", g.Slice{4, 5, 6}),
|
||||
).Where(
|
||||
b.Where("id", g.Slice{2, 3}).WhereOr("id", g.Slice{5, 6}),
|
||||
).Where(
|
||||
b.Where("id", g.Slice{3}).Where("id", g.Slice{1, 2, 3}),
|
||||
).All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(all), 1)
|
||||
})
|
||||
|
||||
// Where Or
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := db.Model(table)
|
||||
b := m.Builder()
|
||||
|
||||
all, err := m.WhereOr(
|
||||
b.Where("id", g.Slice{1, 2, 3}).WhereOr("id", g.Slice{4, 5, 6}),
|
||||
).WhereOr(
|
||||
b.Where("id", g.Slice{2, 3}).WhereOr("id", g.Slice{5, 6}),
|
||||
).WhereOr(
|
||||
b.Where("id", g.Slice{3}).Where("id", g.Slice{1, 2, 3}),
|
||||
).All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(all), 6)
|
||||
})
|
||||
|
||||
// Where with struct which has a field type of *gtime.Time
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := db.Model(table)
|
||||
b := m.Builder()
|
||||
|
||||
type Query struct {
|
||||
Id any
|
||||
Nickname *gtime.Time
|
||||
}
|
||||
|
||||
where, args := b.Where(&Query{Id: 1}).Build()
|
||||
t.Assert(where, `"id"=? AND "nickname" IS NULL`)
|
||||
t.Assert(args, []any{1})
|
||||
})
|
||||
|
||||
// Where with struct which has a field type of *gjson.Json
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := db.Model(table)
|
||||
b := m.Builder()
|
||||
|
||||
type Query struct {
|
||||
Id any
|
||||
Nickname *gjson.Json
|
||||
}
|
||||
|
||||
where, args := b.Where(&Query{Id: 1}).Build()
|
||||
t.Assert(where, `"id"=? AND "nickname" IS NULL`)
|
||||
t.Assert(args, []any{1})
|
||||
})
|
||||
|
||||
// Where with do struct which has a field type of *gtime.Time and generated by gf cli
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := db.Model(table)
|
||||
b := m.Builder()
|
||||
|
||||
type Query struct {
|
||||
gmeta.Meta `orm:"do:true"`
|
||||
Id any
|
||||
Nickname *gtime.Time
|
||||
}
|
||||
|
||||
where, args := b.Where(&Query{Id: 1}).Build()
|
||||
t.Assert(where, `"id"=?`)
|
||||
t.Assert(args, []any{1})
|
||||
})
|
||||
|
||||
// Where with do struct which has a field type of *gjson.Json and generated by gf cli
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := db.Model(table)
|
||||
b := m.Builder()
|
||||
|
||||
type Query struct {
|
||||
gmeta.Meta `orm:"do:true"`
|
||||
Id any
|
||||
Nickname *gjson.Json
|
||||
}
|
||||
|
||||
where, args := b.Where(&Query{Id: 1}).Build()
|
||||
t.Assert(where, `"id"=?`)
|
||||
t.Assert(args, []any{1})
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Safe_Builder(t *testing.T) {
|
||||
// test whether m.Builder() is chain safe
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
b := db.Model().Builder()
|
||||
b.Where("id", 1)
|
||||
_, args := b.Build()
|
||||
t.AssertNil(args)
|
||||
|
||||
b = b.Where("id", 1)
|
||||
_, args = b.Build()
|
||||
t.Assert(args, g.Slice{1})
|
||||
})
|
||||
}
|
||||
@ -1,410 +0,0 @@
|
||||
// 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 gaussdb_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
// createTableDO creates a table with nullable columns (no NOT NULL constraints)
|
||||
// suitable for DO (Data Object) partial insert tests.
|
||||
func createTableDO(table ...string) (name string) {
|
||||
if len(table) > 0 {
|
||||
name = table[0]
|
||||
} else {
|
||||
name = fmt.Sprintf(`%s_%d`, TablePrefix+"do_test", gtime.TimestampNano())
|
||||
}
|
||||
dropTable(name)
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE %s (
|
||||
id bigserial NOT NULL,
|
||||
passport varchar(45) DEFAULT '',
|
||||
password varchar(32) DEFAULT '',
|
||||
nickname varchar(45) DEFAULT '',
|
||||
create_time timestamp DEFAULT NULL,
|
||||
PRIMARY KEY (id)
|
||||
);`, name,
|
||||
)); err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func Test_Model_Insert_Data_DO(t *testing.T) {
|
||||
table := createTableDO()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type User struct {
|
||||
g.Meta `orm:"do:true"`
|
||||
Id any
|
||||
Passport any
|
||||
Password any
|
||||
Nickname any
|
||||
CreateTime any
|
||||
}
|
||||
data := User{
|
||||
Id: 1,
|
||||
Passport: "user_1",
|
||||
Password: "pass_1",
|
||||
}
|
||||
result, err := db.Model(table).Data(data).Insert()
|
||||
t.AssertNil(err)
|
||||
n, _ := result.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
|
||||
one, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one[`id`], 1)
|
||||
t.Assert(one[`passport`], `user_1`)
|
||||
t.Assert(one[`password`], `pass_1`)
|
||||
t.Assert(one[`nickname`], ``)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Insert_Data_List_DO(t *testing.T) {
|
||||
table := createTableDO()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type User struct {
|
||||
g.Meta `orm:"do:true"`
|
||||
Id any
|
||||
Passport any
|
||||
Password any
|
||||
Nickname any
|
||||
CreateTime any
|
||||
}
|
||||
data := g.Slice{
|
||||
User{
|
||||
Id: 1,
|
||||
Passport: "user_1",
|
||||
Password: "pass_1",
|
||||
},
|
||||
User{
|
||||
Id: 2,
|
||||
Passport: "user_2",
|
||||
Password: "pass_2",
|
||||
},
|
||||
}
|
||||
result, err := db.Model(table).Data(data).Insert()
|
||||
t.AssertNil(err)
|
||||
n, _ := result.RowsAffected()
|
||||
t.Assert(n, 2)
|
||||
|
||||
one, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one[`id`], 1)
|
||||
t.Assert(one[`passport`], `user_1`)
|
||||
t.Assert(one[`password`], `pass_1`)
|
||||
t.Assert(one[`nickname`], ``)
|
||||
|
||||
one, err = db.Model(table).WherePri(2).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one[`id`], 2)
|
||||
t.Assert(one[`passport`], `user_2`)
|
||||
t.Assert(one[`password`], `pass_2`)
|
||||
t.Assert(one[`nickname`], ``)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Update_Data_DO(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type User struct {
|
||||
g.Meta `orm:"do:true"`
|
||||
Id any
|
||||
Passport any
|
||||
Password any
|
||||
Nickname any
|
||||
CreateTime any
|
||||
}
|
||||
data := User{
|
||||
Id: 1,
|
||||
Passport: "user_100",
|
||||
Password: "pass_100",
|
||||
}
|
||||
_, err := db.Model(table).Data(data).WherePri(1).Update()
|
||||
t.AssertNil(err)
|
||||
|
||||
one, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one[`id`], 1)
|
||||
t.Assert(one[`passport`], `user_100`)
|
||||
t.Assert(one[`password`], `pass_100`)
|
||||
t.Assert(one[`nickname`], `name_1`)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Update_Pointer_Data_DO(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type NN string
|
||||
type Req struct {
|
||||
Id int
|
||||
Passport *string
|
||||
Password *string
|
||||
Nickname *NN
|
||||
}
|
||||
type UserDo struct {
|
||||
g.Meta `orm:"do:true"`
|
||||
Id any
|
||||
Passport any
|
||||
Password any
|
||||
Nickname any
|
||||
CreateTime any
|
||||
}
|
||||
var (
|
||||
nickname = NN("nickname_111")
|
||||
req = Req{
|
||||
Password: gconv.PtrString("12345678"),
|
||||
Nickname: &nickname,
|
||||
}
|
||||
data = UserDo{
|
||||
Passport: req.Passport,
|
||||
Password: req.Password,
|
||||
Nickname: req.Nickname,
|
||||
}
|
||||
)
|
||||
|
||||
_, err := db.Model(table).Data(data).WherePri(1).Update()
|
||||
t.AssertNil(err)
|
||||
|
||||
one, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one[`id`], 1)
|
||||
t.Assert(one[`password`], `12345678`)
|
||||
t.Assert(one[`nickname`], `nickname_111`)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Where_DO(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type User struct {
|
||||
g.Meta `orm:"do:true"`
|
||||
Id any
|
||||
Passport any
|
||||
Password any
|
||||
Nickname any
|
||||
CreateTime any
|
||||
}
|
||||
where := User{
|
||||
Id: 1,
|
||||
Passport: "user_1",
|
||||
Password: "pass_1",
|
||||
}
|
||||
one, err := db.Model(table).Where(where).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one[`id`], 1)
|
||||
t.Assert(one[`passport`], `user_1`)
|
||||
t.Assert(one[`password`], `pass_1`)
|
||||
t.Assert(one[`nickname`], `name_1`)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Insert_Data_ForDao(t *testing.T) {
|
||||
table := createTableDO()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type UserForDao struct {
|
||||
Id any
|
||||
Passport any
|
||||
Password any
|
||||
Nickname any
|
||||
CreateTime any
|
||||
}
|
||||
data := UserForDao{
|
||||
Id: 1,
|
||||
Passport: "user_1",
|
||||
Password: "pass_1",
|
||||
}
|
||||
result, err := db.Model(table).Data(data).Insert()
|
||||
t.AssertNil(err)
|
||||
n, _ := result.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
|
||||
one, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one[`id`], 1)
|
||||
t.Assert(one[`passport`], `user_1`)
|
||||
t.Assert(one[`password`], `pass_1`)
|
||||
t.Assert(one[`nickname`], ``)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Insert_Data_List_ForDao(t *testing.T) {
|
||||
table := createTableDO()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type UserForDao struct {
|
||||
Id any
|
||||
Passport any
|
||||
Password any
|
||||
Nickname any
|
||||
CreateTime any
|
||||
}
|
||||
data := g.Slice{
|
||||
UserForDao{
|
||||
Id: 1,
|
||||
Passport: "user_1",
|
||||
Password: "pass_1",
|
||||
},
|
||||
UserForDao{
|
||||
Id: 2,
|
||||
Passport: "user_2",
|
||||
Password: "pass_2",
|
||||
},
|
||||
}
|
||||
result, err := db.Model(table).Data(data).Insert()
|
||||
t.AssertNil(err)
|
||||
n, _ := result.RowsAffected()
|
||||
t.Assert(n, 2)
|
||||
|
||||
one, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one[`id`], 1)
|
||||
t.Assert(one[`passport`], `user_1`)
|
||||
t.Assert(one[`password`], `pass_1`)
|
||||
t.Assert(one[`nickname`], ``)
|
||||
|
||||
one, err = db.Model(table).WherePri(2).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one[`id`], 2)
|
||||
t.Assert(one[`passport`], `user_2`)
|
||||
t.Assert(one[`password`], `pass_2`)
|
||||
t.Assert(one[`nickname`], ``)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Update_Data_ForDao(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type UserForDao struct {
|
||||
Id any
|
||||
Passport any
|
||||
Password any
|
||||
Nickname any
|
||||
CreateTime any
|
||||
}
|
||||
data := UserForDao{
|
||||
Id: 1,
|
||||
Passport: "user_100",
|
||||
Password: "pass_100",
|
||||
}
|
||||
_, err := db.Model(table).Data(data).WherePri(1).Update()
|
||||
t.AssertNil(err)
|
||||
|
||||
one, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one[`id`], 1)
|
||||
t.Assert(one[`passport`], `user_100`)
|
||||
t.Assert(one[`password`], `pass_100`)
|
||||
t.Assert(one[`nickname`], `name_1`)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Where_ForDao(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type UserForDao struct {
|
||||
Id any
|
||||
Passport any
|
||||
Password any
|
||||
Nickname any
|
||||
CreateTime any
|
||||
}
|
||||
where := UserForDao{
|
||||
Id: 1,
|
||||
Passport: "user_1",
|
||||
Password: "pass_1",
|
||||
}
|
||||
one, err := db.Model(table).Where(where).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one[`id`], 1)
|
||||
t.Assert(one[`passport`], `user_1`)
|
||||
t.Assert(one[`password`], `pass_1`)
|
||||
t.Assert(one[`nickname`], `name_1`)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Where_FieldPrefix(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
array := gstr.SplitAndTrim(gtest.DataContent(`table_with_prefix.sql`), ";")
|
||||
for _, v := range array {
|
||||
if _, err := db.Exec(ctx, v); err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
}
|
||||
defer dropTable("instance")
|
||||
|
||||
type Instance struct {
|
||||
ID int `orm:"f_id"`
|
||||
Name string
|
||||
}
|
||||
|
||||
type InstanceDo struct {
|
||||
g.Meta `orm:"table:instance, do:true"`
|
||||
ID any `orm:"f_id"`
|
||||
}
|
||||
var instance *Instance
|
||||
err := db.Model("instance").Where(InstanceDo{
|
||||
ID: 1,
|
||||
}).Scan(&instance)
|
||||
t.AssertNil(err)
|
||||
t.AssertNE(instance, nil)
|
||||
t.Assert(instance.ID, 1)
|
||||
t.Assert(instance.Name, "john")
|
||||
})
|
||||
// With omitempty.
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
array := gstr.SplitAndTrim(gtest.DataContent(`table_with_prefix.sql`), ";")
|
||||
for _, v := range array {
|
||||
if _, err := db.Exec(ctx, v); err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
}
|
||||
defer dropTable("instance")
|
||||
|
||||
type Instance struct {
|
||||
ID int `orm:"f_id,omitempty"`
|
||||
Name string
|
||||
}
|
||||
|
||||
type InstanceDo struct {
|
||||
g.Meta `orm:"table:instance, do:true"`
|
||||
ID any `orm:"f_id,omitempty"`
|
||||
}
|
||||
var instance *Instance
|
||||
err := db.Model("instance").Where(InstanceDo{
|
||||
ID: 1,
|
||||
}).Scan(&instance)
|
||||
t.AssertNil(err)
|
||||
t.AssertNE(instance, nil)
|
||||
t.Assert(instance.ID, 1)
|
||||
t.Assert(instance.Name, "john")
|
||||
})
|
||||
}
|
||||
@ -1,177 +0,0 @@
|
||||
// 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 gaussdb_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
)
|
||||
|
||||
func Test_Model_LeftJoinOnField(t *testing.T) {
|
||||
var (
|
||||
table1 = "t_" + gtime.TimestampNanoStr() + "_table1"
|
||||
table2 = "t_" + gtime.TimestampNanoStr() + "_table2"
|
||||
)
|
||||
createInitTable(table1)
|
||||
defer dropTable(table1)
|
||||
createInitTable(table2)
|
||||
defer dropTable(table2)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
r, err := db.Model(table1).
|
||||
FieldsPrefix(table1, "*").
|
||||
LeftJoinOnField(table2, "id").
|
||||
WhereIn("id", g.Slice{1, 2}).
|
||||
Order("id asc").All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(r), 2)
|
||||
t.Assert(r[0]["id"], 1)
|
||||
t.Assert(r[1]["id"], 2)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_RightJoinOnField(t *testing.T) {
|
||||
var (
|
||||
table1 = "t_" + gtime.TimestampNanoStr() + "_table1"
|
||||
table2 = "t_" + gtime.TimestampNanoStr() + "_table2"
|
||||
)
|
||||
createInitTable(table1)
|
||||
defer dropTable(table1)
|
||||
createInitTable(table2)
|
||||
defer dropTable(table2)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
r, err := db.Model(table1).
|
||||
FieldsPrefix(table1, "*").
|
||||
RightJoinOnField(table2, "id").
|
||||
WhereIn("id", g.Slice{1, 2}).
|
||||
Order("id asc").All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(r), 2)
|
||||
t.Assert(r[0]["id"], 1)
|
||||
t.Assert(r[1]["id"], 2)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_InnerJoinOnField(t *testing.T) {
|
||||
var (
|
||||
table1 = "t_" + gtime.TimestampNanoStr() + "_table1"
|
||||
table2 = "t_" + gtime.TimestampNanoStr() + "_table2"
|
||||
)
|
||||
createInitTable(table1)
|
||||
defer dropTable(table1)
|
||||
createInitTable(table2)
|
||||
defer dropTable(table2)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
r, err := db.Model(table1).
|
||||
FieldsPrefix(table1, "*").
|
||||
InnerJoinOnField(table2, "id").
|
||||
WhereIn("id", g.Slice{1, 2}).
|
||||
Order("id asc").All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(r), 2)
|
||||
t.Assert(r[0]["id"], 1)
|
||||
t.Assert(r[1]["id"], 2)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_LeftJoinOnFields(t *testing.T) {
|
||||
var (
|
||||
table1 = "t_" + gtime.TimestampNanoStr() + "_table1"
|
||||
table2 = "t_" + gtime.TimestampNanoStr() + "_table2"
|
||||
)
|
||||
createInitTable(table1)
|
||||
defer dropTable(table1)
|
||||
createInitTable(table2)
|
||||
defer dropTable(table2)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
r, err := db.Model(table1).
|
||||
FieldsPrefix(table1, "*").
|
||||
LeftJoinOnFields(table2, "id", "=", "id").
|
||||
WhereIn("id", g.Slice{1, 2}).
|
||||
Order("id asc").All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(r), 2)
|
||||
t.Assert(r[0]["id"], 1)
|
||||
t.Assert(r[1]["id"], 2)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_RightJoinOnFields(t *testing.T) {
|
||||
var (
|
||||
table1 = "t_" + gtime.TimestampNanoStr() + "_table1"
|
||||
table2 = "t_" + gtime.TimestampNanoStr() + "_table2"
|
||||
)
|
||||
createInitTable(table1)
|
||||
defer dropTable(table1)
|
||||
createInitTable(table2)
|
||||
defer dropTable(table2)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
r, err := db.Model(table1).
|
||||
FieldsPrefix(table1, "*").
|
||||
RightJoinOnFields(table2, "id", "=", "id").
|
||||
WhereIn("id", g.Slice{1, 2}).
|
||||
Order("id asc").All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(r), 2)
|
||||
t.Assert(r[0]["id"], 1)
|
||||
t.Assert(r[1]["id"], 2)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_InnerJoinOnFields(t *testing.T) {
|
||||
var (
|
||||
table1 = "t_" + gtime.TimestampNanoStr() + "_table1"
|
||||
table2 = "t_" + gtime.TimestampNanoStr() + "_table2"
|
||||
)
|
||||
createInitTable(table1)
|
||||
defer dropTable(table1)
|
||||
createInitTable(table2)
|
||||
defer dropTable(table2)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
r, err := db.Model(table1).
|
||||
FieldsPrefix(table1, "*").
|
||||
InnerJoinOnFields(table2, "id", "=", "id").
|
||||
WhereIn("id", g.Slice{1, 2}).
|
||||
Order("id asc").All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(r), 2)
|
||||
t.Assert(r[0]["id"], 1)
|
||||
t.Assert(r[1]["id"], 2)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_FieldsPrefix(t *testing.T) {
|
||||
var (
|
||||
table1 = "t_" + gtime.TimestampNanoStr() + "_table1"
|
||||
table2 = "t_" + gtime.TimestampNanoStr() + "_table2"
|
||||
)
|
||||
createInitTable(table1)
|
||||
defer dropTable(table1)
|
||||
createInitTable(table2)
|
||||
defer dropTable(table2)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
r, err := db.Model(table1).
|
||||
FieldsPrefix(table1, "id").
|
||||
FieldsPrefix(table2, "nickname").
|
||||
LeftJoinOnField(table2, "id").
|
||||
WhereIn("id", g.Slice{1, 2}).
|
||||
Order("id asc").All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(r), 2)
|
||||
t.Assert(r[0]["id"], 1)
|
||||
t.Assert(r[0]["nickname"], "name_1")
|
||||
})
|
||||
}
|
||||
@ -1,477 +0,0 @@
|
||||
// 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 gaussdb_test
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/errors/gcode"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
func Test_Model_Embedded_Insert(t *testing.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type Base struct {
|
||||
Id int `json:"id"`
|
||||
CreateTime string `json:"create_time"`
|
||||
}
|
||||
type User struct {
|
||||
Base
|
||||
Passport string `json:"passport"`
|
||||
Password string `json:"password"`
|
||||
Nickname string `json:"nickname"`
|
||||
}
|
||||
result, err := db.Model(table).Data(User{
|
||||
Passport: "john-test",
|
||||
Password: "123456",
|
||||
Nickname: "John",
|
||||
Base: Base{
|
||||
Id: 100,
|
||||
CreateTime: gtime.Now().String(),
|
||||
},
|
||||
}).Insert()
|
||||
t.AssertNil(err)
|
||||
n, _ := result.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
value, err := db.Model(table).Fields("passport").Where("id=100").Value()
|
||||
t.AssertNil(err)
|
||||
t.Assert(value.String(), "john-test")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Embedded_MapToStruct(t *testing.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type Ids struct {
|
||||
Id int `json:"id"`
|
||||
}
|
||||
type Base struct {
|
||||
Ids
|
||||
CreateTime string `json:"create_time"`
|
||||
}
|
||||
type User struct {
|
||||
Base
|
||||
Passport string `json:"passport"`
|
||||
Password string `json:"password"`
|
||||
Nickname string `json:"nickname"`
|
||||
}
|
||||
data := g.Map{
|
||||
"id": 100,
|
||||
"passport": "t1",
|
||||
"password": "123456",
|
||||
"nickname": "T1",
|
||||
"create_time": gtime.Now().String(),
|
||||
}
|
||||
result, err := db.Model(table).Data(data).Insert()
|
||||
t.AssertNil(err)
|
||||
n, _ := result.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
|
||||
one, err := db.Model(table).Where("id=100").One()
|
||||
t.AssertNil(err)
|
||||
|
||||
user := new(User)
|
||||
|
||||
t.Assert(one.Struct(user), nil)
|
||||
t.Assert(user.Id, data["id"])
|
||||
t.Assert(user.Passport, data["passport"])
|
||||
t.Assert(user.Password, data["password"])
|
||||
t.Assert(user.Nickname, data["nickname"])
|
||||
t.Assert(user.CreateTime, data["create_time"])
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Struct_Pointer_Attribute(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
type User struct {
|
||||
Id *int
|
||||
Passport *string
|
||||
Password *string
|
||||
Nickname string
|
||||
}
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
one, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
user := new(User)
|
||||
err = one.Struct(user)
|
||||
t.AssertNil(err)
|
||||
t.Assert(*user.Id, 1)
|
||||
t.Assert(*user.Passport, "user_1")
|
||||
t.Assert(*user.Password, "pass_1")
|
||||
t.Assert(user.Nickname, "name_1")
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
user := new(User)
|
||||
err := db.Model(table).Scan(user, "id=1")
|
||||
t.AssertNil(err)
|
||||
t.Assert(*user.Id, 1)
|
||||
t.Assert(*user.Passport, "user_1")
|
||||
t.Assert(*user.Password, "pass_1")
|
||||
t.Assert(user.Nickname, "name_1")
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var user *User
|
||||
err := db.Model(table).Scan(&user, "id=1")
|
||||
t.AssertNil(err)
|
||||
t.Assert(*user.Id, 1)
|
||||
t.Assert(*user.Passport, "user_1")
|
||||
t.Assert(*user.Password, "pass_1")
|
||||
t.Assert(user.Nickname, "name_1")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Structs_Pointer_Attribute(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
type User struct {
|
||||
Id *int
|
||||
Passport *string
|
||||
Password *string
|
||||
Nickname string
|
||||
}
|
||||
// All
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
one, err := db.Model(table).All("id < 3")
|
||||
t.AssertNil(err)
|
||||
users := make([]User, 0)
|
||||
err = one.Structs(&users)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(users), 2)
|
||||
t.Assert(*users[0].Id, 1)
|
||||
t.Assert(*users[0].Passport, "user_1")
|
||||
t.Assert(*users[0].Password, "pass_1")
|
||||
t.Assert(users[0].Nickname, "name_1")
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
one, err := db.Model(table).All("id < 3")
|
||||
t.AssertNil(err)
|
||||
users := make([]*User, 0)
|
||||
err = one.Structs(&users)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(users), 2)
|
||||
t.Assert(*users[0].Id, 1)
|
||||
t.Assert(*users[0].Passport, "user_1")
|
||||
t.Assert(*users[0].Password, "pass_1")
|
||||
t.Assert(users[0].Nickname, "name_1")
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var users []User
|
||||
one, err := db.Model(table).All("id < 3")
|
||||
t.AssertNil(err)
|
||||
err = one.Structs(&users)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(users), 2)
|
||||
t.Assert(*users[0].Id, 1)
|
||||
t.Assert(*users[0].Passport, "user_1")
|
||||
t.Assert(*users[0].Password, "pass_1")
|
||||
t.Assert(users[0].Nickname, "name_1")
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var users []*User
|
||||
one, err := db.Model(table).All("id < 3")
|
||||
t.AssertNil(err)
|
||||
err = one.Structs(&users)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(users), 2)
|
||||
t.Assert(*users[0].Id, 1)
|
||||
t.Assert(*users[0].Passport, "user_1")
|
||||
t.Assert(*users[0].Password, "pass_1")
|
||||
t.Assert(users[0].Nickname, "name_1")
|
||||
})
|
||||
// Structs
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
users := make([]User, 0)
|
||||
err := db.Model(table).Scan(&users, "id < 3")
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(users), 2)
|
||||
t.Assert(*users[0].Id, 1)
|
||||
t.Assert(*users[0].Passport, "user_1")
|
||||
t.Assert(*users[0].Password, "pass_1")
|
||||
t.Assert(users[0].Nickname, "name_1")
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
users := make([]*User, 0)
|
||||
err := db.Model(table).Scan(&users, "id < 3")
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(users), 2)
|
||||
t.Assert(*users[0].Id, 1)
|
||||
t.Assert(*users[0].Passport, "user_1")
|
||||
t.Assert(*users[0].Password, "pass_1")
|
||||
t.Assert(users[0].Nickname, "name_1")
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var users []User
|
||||
err := db.Model(table).Scan(&users, "id < 3")
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(users), 2)
|
||||
t.Assert(*users[0].Id, 1)
|
||||
t.Assert(*users[0].Passport, "user_1")
|
||||
t.Assert(*users[0].Password, "pass_1")
|
||||
t.Assert(users[0].Nickname, "name_1")
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var users []*User
|
||||
err := db.Model(table).Scan(&users, "id < 3")
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(users), 2)
|
||||
t.Assert(*users[0].Id, 1)
|
||||
t.Assert(*users[0].Passport, "user_1")
|
||||
t.Assert(*users[0].Password, "pass_1")
|
||||
t.Assert(users[0].Nickname, "name_1")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Struct_Empty(t *testing.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
|
||||
type User struct {
|
||||
Id int
|
||||
Passport string
|
||||
Password string
|
||||
Nickname string
|
||||
}
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
user := new(User)
|
||||
err := db.Model(table).Where("id=100").Scan(user)
|
||||
t.Assert(err, sql.ErrNoRows)
|
||||
t.AssertNE(user, nil)
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
one, err := db.Model(table).Where("id=100").One()
|
||||
t.AssertNil(err)
|
||||
var user *User
|
||||
t.Assert(one.Struct(&user), nil)
|
||||
t.Assert(user, nil)
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var user *User
|
||||
err := db.Model(table).Where("id=100").Scan(&user)
|
||||
t.AssertNil(err)
|
||||
t.Assert(user, nil)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Structs_Empty(t *testing.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
|
||||
type User struct {
|
||||
Id int
|
||||
Passport string
|
||||
Password string
|
||||
Nickname string
|
||||
}
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
all, err := db.Model(table).Where("id>100").All()
|
||||
t.AssertNil(err)
|
||||
users := make([]User, 0)
|
||||
t.Assert(all.Structs(&users), nil)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
all, err := db.Model(table).Where("id>100").All()
|
||||
t.AssertNil(err)
|
||||
users := make([]User, 10)
|
||||
t.Assert(all.Structs(&users), sql.ErrNoRows)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
all, err := db.Model(table).Where("id>100").All()
|
||||
t.AssertNil(err)
|
||||
var users []User
|
||||
t.Assert(all.Structs(&users), nil)
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
all, err := db.Model(table).Where("id>100").All()
|
||||
t.AssertNil(err)
|
||||
users := make([]*User, 0)
|
||||
t.Assert(all.Structs(&users), nil)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
all, err := db.Model(table).Where("id>100").All()
|
||||
t.AssertNil(err)
|
||||
users := make([]*User, 10)
|
||||
t.Assert(all.Structs(&users), sql.ErrNoRows)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
all, err := db.Model(table).Where("id>100").All()
|
||||
t.AssertNil(err)
|
||||
var users []*User
|
||||
t.Assert(all.Structs(&users), nil)
|
||||
})
|
||||
}
|
||||
|
||||
type MyTime struct {
|
||||
gtime.Time
|
||||
}
|
||||
|
||||
type MyTimeSt struct {
|
||||
CreateTime MyTime
|
||||
}
|
||||
|
||||
func (st *MyTimeSt) UnmarshalValue(v any) error {
|
||||
m := gconv.Map(v)
|
||||
t, err := gtime.StrToTime(gconv.String(m["create_time"]))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
st.CreateTime = MyTime{*t}
|
||||
return nil
|
||||
}
|
||||
|
||||
func Test_Model_Scan_CustomType_Time(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
st := new(MyTimeSt)
|
||||
err := db.Model(table).Fields("create_time").Scan(st)
|
||||
t.AssertNil(err)
|
||||
t.Assert(st.CreateTime.String(), "2018-10-24 10:00:00")
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var stSlice []*MyTimeSt
|
||||
err := db.Model(table).Fields("create_time").Scan(&stSlice)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(stSlice), TableSize)
|
||||
t.Assert(stSlice[0].CreateTime.String(), "2018-10-24 10:00:00")
|
||||
t.Assert(stSlice[9].CreateTime.String(), "2018-10-24 10:00:00")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Scan_CustomType_String(t *testing.T) {
|
||||
type MyString string
|
||||
|
||||
type MyStringSt struct {
|
||||
Passport MyString
|
||||
}
|
||||
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
st := new(MyStringSt)
|
||||
err := db.Model(table).Fields("Passport").WherePri(1).Scan(st)
|
||||
t.AssertNil(err)
|
||||
t.Assert(st.Passport, "user_1")
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var sts []MyStringSt
|
||||
err := db.Model(table).Fields("Passport").Order("id asc").Scan(&sts)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(sts), TableSize)
|
||||
t.Assert(sts[0].Passport, "user_1")
|
||||
})
|
||||
}
|
||||
|
||||
type User struct {
|
||||
Id int
|
||||
Passport string
|
||||
Password string
|
||||
Nickname string
|
||||
CreateTime *gtime.Time
|
||||
}
|
||||
|
||||
func (user *User) UnmarshalValue(value any) error {
|
||||
if record, ok := value.(gdb.Record); ok {
|
||||
*user = User{
|
||||
Id: record["id"].Int(),
|
||||
Passport: record["passport"].String(),
|
||||
Password: "",
|
||||
Nickname: record["nickname"].String(),
|
||||
CreateTime: record["create_time"].GTime(),
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return gerror.NewCodef(gcode.CodeInvalidParameter, `unsupported value type for UnmarshalValue: %v`, reflect.TypeOf(value))
|
||||
}
|
||||
|
||||
func Test_Model_Scan_UnmarshalValue(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var users []*User
|
||||
err := db.Model(table).Order("id asc").Scan(&users)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(users), TableSize)
|
||||
t.Assert(users[0].Id, 1)
|
||||
t.Assert(users[0].Passport, "user_1")
|
||||
t.Assert(users[0].Password, "")
|
||||
t.Assert(users[0].Nickname, "name_1")
|
||||
t.Assert(users[0].CreateTime.String(), CreateTime)
|
||||
|
||||
t.Assert(users[9].Id, 10)
|
||||
t.Assert(users[9].Passport, "user_10")
|
||||
t.Assert(users[9].Password, "")
|
||||
t.Assert(users[9].Nickname, "name_10")
|
||||
t.Assert(users[9].CreateTime.String(), CreateTime)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Scan_Map(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var users []*User
|
||||
err := db.Model(table).Order("id asc").Scan(&users)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(users), TableSize)
|
||||
t.Assert(users[0].Id, 1)
|
||||
t.Assert(users[0].Passport, "user_1")
|
||||
t.Assert(users[0].Password, "")
|
||||
t.Assert(users[0].Nickname, "name_1")
|
||||
t.Assert(users[0].CreateTime.String(), CreateTime)
|
||||
|
||||
t.Assert(users[9].Id, 10)
|
||||
t.Assert(users[9].Passport, "user_10")
|
||||
t.Assert(users[9].Password, "")
|
||||
t.Assert(users[9].Nickname, "name_10")
|
||||
t.Assert(users[9].CreateTime.String(), CreateTime)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Scan_AutoFilteringByStructAttributes(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
type User struct {
|
||||
Id int
|
||||
Passport string
|
||||
}
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var user *User
|
||||
err := db.Model(table).OrderAsc("id").Scan(&user)
|
||||
t.AssertNil(err)
|
||||
t.Assert(user.Id, 1)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var users []User
|
||||
err := db.Model(table).OrderAsc("id").Scan(&users)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(users), TableSize)
|
||||
t.Assert(users[0].Id, 1)
|
||||
})
|
||||
}
|
||||
@ -1,66 +0,0 @@
|
||||
// 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 gaussdb_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
)
|
||||
|
||||
func Test_Model_SubQuery_Where(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
r, err := db.Model(table).Where(
|
||||
"id in ?",
|
||||
db.Model(table).Fields("id").Where("id", g.Slice{1, 3, 5}),
|
||||
).OrderAsc("id").All()
|
||||
t.AssertNil(err)
|
||||
|
||||
t.Assert(len(r), 3)
|
||||
t.Assert(r[0]["id"], 1)
|
||||
t.Assert(r[1]["id"], 3)
|
||||
t.Assert(r[2]["id"], 5)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_SubQuery_Having(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
r, err := db.Model(table).Where(
|
||||
"id in ?",
|
||||
db.Model(table).Fields("id").Where("id", g.Slice{1, 3, 5}),
|
||||
).Group("id").Having(
|
||||
"id > ?",
|
||||
db.Model(table).Fields("MAX(id)").Where("id", g.Slice{1, 3}),
|
||||
).OrderAsc("id").All()
|
||||
t.AssertNil(err)
|
||||
|
||||
t.Assert(len(r), 1)
|
||||
t.Assert(r[0]["id"], 5)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_SubQuery_Model(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
subQuery1 := db.Model(table).Where("id", g.Slice{1, 3, 5})
|
||||
subQuery2 := db.Model(table).Where("id", g.Slice{5, 7, 9})
|
||||
r, err := db.Model("? AS a, ? AS b", subQuery1, subQuery2).Fields("a.id").Where("a.id=b.id").OrderAsc("id").All()
|
||||
t.AssertNil(err)
|
||||
|
||||
t.Assert(len(r), 1)
|
||||
t.Assert(r[0]["id"], 5)
|
||||
})
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -1,146 +0,0 @@
|
||||
// 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 gaussdb_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
)
|
||||
|
||||
func Test_Union(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
r, err := db.Union(
|
||||
db.Model(table).Where("id", 1),
|
||||
db.Model(table).Where("id", 2),
|
||||
db.Model(table).WhereIn("id", g.Slice{1, 2, 3}).OrderDesc("id"),
|
||||
).OrderDesc("id").All()
|
||||
|
||||
t.AssertNil(err)
|
||||
|
||||
t.Assert(len(r), 3)
|
||||
t.Assert(r[0]["id"], 3)
|
||||
t.Assert(r[1]["id"], 2)
|
||||
t.Assert(r[2]["id"], 1)
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
r, err := db.Union(
|
||||
db.Model(table).Where("id", 1),
|
||||
db.Model(table).Where("id", 2),
|
||||
db.Model(table).WhereIn("id", g.Slice{1, 2, 3}).OrderDesc("id"),
|
||||
).OrderDesc("id").One()
|
||||
|
||||
t.AssertNil(err)
|
||||
|
||||
t.Assert(r["id"], 3)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_UnionAll(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
r, err := db.UnionAll(
|
||||
db.Model(table).Where("id", 1),
|
||||
db.Model(table).Where("id", 2),
|
||||
db.Model(table).WhereIn("id", g.Slice{1, 2, 3}).OrderDesc("id"),
|
||||
).OrderDesc("id").All()
|
||||
|
||||
t.AssertNil(err)
|
||||
|
||||
t.Assert(len(r), 5)
|
||||
t.Assert(r[0]["id"], 3)
|
||||
t.Assert(r[1]["id"], 2)
|
||||
t.Assert(r[2]["id"], 2)
|
||||
t.Assert(r[3]["id"], 1)
|
||||
t.Assert(r[4]["id"], 1)
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
r, err := db.UnionAll(
|
||||
db.Model(table).Where("id", 1),
|
||||
db.Model(table).Where("id", 2),
|
||||
db.Model(table).WhereIn("id", g.Slice{1, 2, 3}).OrderDesc("id"),
|
||||
).OrderDesc("id").One()
|
||||
|
||||
t.AssertNil(err)
|
||||
|
||||
t.Assert(r["id"], 3)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Union(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
r, err := db.Model(table).Union(
|
||||
db.Model(table).Where("id", 1),
|
||||
db.Model(table).Where("id", 2),
|
||||
db.Model(table).WhereIn("id", g.Slice{1, 2, 3}).OrderDesc("id"),
|
||||
).OrderDesc("id").All()
|
||||
|
||||
t.AssertNil(err)
|
||||
|
||||
t.Assert(len(r), 3)
|
||||
t.Assert(r[0]["id"], 3)
|
||||
t.Assert(r[1]["id"], 2)
|
||||
t.Assert(r[2]["id"], 1)
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
r, err := db.Model(table).Union(
|
||||
db.Model(table).Where("id", 1),
|
||||
db.Model(table).Where("id", 2),
|
||||
db.Model(table).WhereIn("id", g.Slice{1, 2, 3}).OrderDesc("id"),
|
||||
).OrderDesc("id").One()
|
||||
|
||||
t.AssertNil(err)
|
||||
|
||||
t.Assert(r["id"], 3)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_UnionAll(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
r, err := db.Model(table).UnionAll(
|
||||
db.Model(table).Where("id", 1),
|
||||
db.Model(table).Where("id", 2),
|
||||
db.Model(table).WhereIn("id", g.Slice{1, 2, 3}).OrderDesc("id"),
|
||||
).OrderDesc("id").All()
|
||||
|
||||
t.AssertNil(err)
|
||||
|
||||
t.Assert(len(r), 5)
|
||||
t.Assert(r[0]["id"], 3)
|
||||
t.Assert(r[1]["id"], 2)
|
||||
t.Assert(r[2]["id"], 2)
|
||||
t.Assert(r[3]["id"], 1)
|
||||
t.Assert(r[4]["id"], 1)
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
r, err := db.Model(table).UnionAll(
|
||||
db.Model(table).Where("id", 1),
|
||||
db.Model(table).Where("id", 2),
|
||||
db.Model(table).WhereIn("id", g.Slice{1, 2, 3}).OrderDesc("id"),
|
||||
).OrderDesc("id").One()
|
||||
|
||||
t.AssertNil(err)
|
||||
|
||||
t.Assert(r["id"], 3)
|
||||
})
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@ -239,6 +239,37 @@ func Test_Model_Exist(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Where(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
// map + slice parameter
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, err := db.Model(table).Where(g.Map{
|
||||
"id": g.Slice{1, 2, 3},
|
||||
"passport": g.Slice{"user_2", "user_3"},
|
||||
}).Where("id=? and nickname=?", g.Slice{3, "name_3"}).One()
|
||||
t.AssertNil(err)
|
||||
t.AssertGT(len(result), 0)
|
||||
t.Assert(result["id"].Int(), 3)
|
||||
})
|
||||
|
||||
// struct, automatic mapping and filtering.
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type User struct {
|
||||
Id int
|
||||
Nickname string
|
||||
}
|
||||
result, err := db.Model(table).Where(User{3, "name_3"}).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(result["id"].Int(), 3)
|
||||
|
||||
result, err = db.Model(table).Where(&User{3, "name_3"}).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(result["id"].Int(), 3)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Save(t *testing.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@ -7,7 +7,6 @@
|
||||
package gaussdb_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
@ -98,45 +97,3 @@ func Test_Raw_Update(t *testing.T) {
|
||||
t.Assert(n, int64(1))
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Raw_Where(t *testing.T) {
|
||||
table1 := createTable("test_raw_where_table1")
|
||||
table2 := createTable("test_raw_where_table2")
|
||||
defer dropTable(table1)
|
||||
defer dropTable(table2)
|
||||
|
||||
// https://github.com/gogf/gf/issues/3922
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expectSql := `SELECT * FROM "test_raw_where_table1" AS A WHERE NOT EXISTS (SELECT B.id FROM "test_raw_where_table2" AS B WHERE "B"."id"=A.id) LIMIT 1`
|
||||
sql, err := gdb.ToSQL(ctx, func(ctx context.Context) error {
|
||||
s := db.Model(table2).As("B").Ctx(ctx).Fields("B.id").Where("B.id", gdb.Raw("A.id"))
|
||||
m := db.Model(table1).As("A").Ctx(ctx).Where("NOT EXISTS ?", s).Limit(1)
|
||||
_, err := m.All()
|
||||
return err
|
||||
})
|
||||
t.AssertNil(err)
|
||||
t.Assert(expectSql, sql)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expectSql := `SELECT * FROM "test_raw_where_table1" AS A WHERE NOT EXISTS (SELECT B.id FROM "test_raw_where_table2" AS B WHERE B.id=A.id) LIMIT 1`
|
||||
sql, err := gdb.ToSQL(ctx, func(ctx context.Context) error {
|
||||
s := db.Model(table2).As("B").Ctx(ctx).Fields("B.id").Where(gdb.Raw("B.id=A.id"))
|
||||
m := db.Model(table1).As("A").Ctx(ctx).Where("NOT EXISTS ?", s).Limit(1)
|
||||
_, err := m.All()
|
||||
return err
|
||||
})
|
||||
t.AssertNil(err)
|
||||
t.Assert(expectSql, sql)
|
||||
})
|
||||
// https://github.com/gogf/gf/issues/3915
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expectSql := `SELECT * FROM "test_raw_where_table1" WHERE "passport" < "nickname"`
|
||||
sql, err := gdb.ToSQL(ctx, func(ctx context.Context) error {
|
||||
m := db.Model(table1).Ctx(ctx).WhereLT("passport", gdb.Raw(`"nickname"`))
|
||||
_, err := m.All()
|
||||
return err
|
||||
})
|
||||
t.AssertNil(err)
|
||||
t.Assert(expectSql, sql)
|
||||
})
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@ -4,7 +4,7 @@ go 1.23.0
|
||||
|
||||
require (
|
||||
gitee.com/opengauss/openGauss-connector-go-pq v1.0.7
|
||||
github.com/gogf/gf/v2 v2.10.0
|
||||
github.com/gogf/gf/v2 v2.9.8
|
||||
github.com/google/uuid v1.6.0
|
||||
)
|
||||
|
||||
|
||||
@ -1,6 +0,0 @@
|
||||
DROP TABLE IF EXISTS instance;
|
||||
CREATE TABLE instance (
|
||||
f_id SERIAL NOT NULL PRIMARY KEY,
|
||||
name varchar(255) DEFAULT ''
|
||||
);
|
||||
INSERT INTO instance VALUES (1, 'john');
|
||||
@ -1,30 +0,0 @@
|
||||
|
||||
CREATE TABLE table_a (
|
||||
id SERIAL PRIMARY KEY,
|
||||
alias varchar(255) DEFAULT ''
|
||||
);
|
||||
|
||||
INSERT INTO table_a VALUES (1, 'table_a_test1');
|
||||
INSERT INTO table_a VALUES (2, 'table_a_test2');
|
||||
|
||||
CREATE TABLE table_b (
|
||||
id SERIAL PRIMARY KEY,
|
||||
table_a_id integer NOT NULL,
|
||||
alias varchar(255) DEFAULT ''
|
||||
);
|
||||
|
||||
INSERT INTO table_b VALUES (10, 1, 'table_b_test1');
|
||||
INSERT INTO table_b VALUES (20, 2, 'table_b_test2');
|
||||
INSERT INTO table_b VALUES (30, 1, 'table_b_test3');
|
||||
INSERT INTO table_b VALUES (40, 2, 'table_b_test4');
|
||||
|
||||
CREATE TABLE table_c (
|
||||
id SERIAL PRIMARY KEY,
|
||||
table_b_id integer NOT NULL,
|
||||
alias varchar(255) DEFAULT ''
|
||||
);
|
||||
|
||||
INSERT INTO table_c VALUES (100, 10, 'table_c_test1');
|
||||
INSERT INTO table_c VALUES (200, 10, 'table_c_test2');
|
||||
INSERT INTO table_c VALUES (300, 20, 'table_c_test3');
|
||||
INSERT INTO table_c VALUES (400, 30, 'table_c_test4');
|
||||
@ -1,4 +0,0 @@
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
id SERIAL PRIMARY KEY,
|
||||
name varchar(45) NOT NULL
|
||||
);
|
||||
@ -1,4 +0,0 @@
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
uid SERIAL PRIMARY KEY,
|
||||
address varchar(45) NOT NULL
|
||||
);
|
||||
@ -1,5 +0,0 @@
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
id SERIAL PRIMARY KEY,
|
||||
uid integer NOT NULL,
|
||||
score integer NOT NULL
|
||||
);
|
||||
@ -3,8 +3,8 @@ module github.com/gogf/gf/contrib/drivers/mariadb/v2
|
||||
go 1.23.0
|
||||
|
||||
require (
|
||||
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.10.0
|
||||
github.com/gogf/gf/v2 v2.10.0
|
||||
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.9.8
|
||||
github.com/gogf/gf/v2 v2.9.8
|
||||
)
|
||||
|
||||
require (
|
||||
|
||||
@ -30,10 +30,9 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
db gdb.DB
|
||||
db2 gdb.DB
|
||||
dbInvalid gdb.DB
|
||||
ctx = context.TODO()
|
||||
db gdb.DB
|
||||
db2 gdb.DB
|
||||
ctx = context.TODO()
|
||||
)
|
||||
|
||||
func init() {
|
||||
@ -62,19 +61,6 @@ func init() {
|
||||
}
|
||||
db = db.Schema(TestSchema1)
|
||||
db2 = db.Schema(TestSchema2)
|
||||
|
||||
// Invalid db (wrong port for testing error handling).
|
||||
nodeInvalid := gdb.ConfigNode{
|
||||
Link: fmt.Sprintf("mariadb:root:%s@tcp(127.0.0.1:3317)/?loc=Local&parseTime=true", TestDbPass),
|
||||
TranTimeout: time.Second * 3,
|
||||
}
|
||||
gdb.AddConfigNode("nodeinvalid", nodeInvalid)
|
||||
if r, err := gdb.NewByGroup("nodeinvalid"); err != nil {
|
||||
gtest.Error(err)
|
||||
} else {
|
||||
dbInvalid = r
|
||||
}
|
||||
dbInvalid = dbInvalid.Schema(TestSchema1)
|
||||
}
|
||||
|
||||
func createTable(table ...string) string {
|
||||
|
||||
@ -1,337 +0,0 @@
|
||||
// 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 mariadb_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
)
|
||||
|
||||
// Test_Model_Batch_Insert tests batch insert with different batch sizes
|
||||
func Test_Model_Batch_Insert(t *testing.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Prepare data for batch insert
|
||||
data := g.Slice{}
|
||||
for i := 1; i <= 10; i++ {
|
||||
data = append(data, g.Map{
|
||||
"id": i,
|
||||
"passport": fmt.Sprintf("batch_user_%d", i),
|
||||
"password": fmt.Sprintf("batch_pass_%d", i),
|
||||
"nickname": fmt.Sprintf("batch_name_%d", i),
|
||||
})
|
||||
}
|
||||
|
||||
// Batch insert with batch size 3
|
||||
result, err := db.Model(table).Batch(3).Data(data).Insert()
|
||||
t.AssertNil(err)
|
||||
n, _ := result.RowsAffected()
|
||||
t.Assert(n, 10)
|
||||
|
||||
// Verify all records were inserted
|
||||
count, err := db.Model(table).Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(count, 10)
|
||||
|
||||
// Verify specific records
|
||||
one, err := db.Model(table).Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], "batch_user_1")
|
||||
|
||||
one, err = db.Model(table).Where("id", 10).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], "batch_user_10")
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Batch_Replace tests batch replace operation
|
||||
func Test_Model_Batch_Replace(t *testing.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Initial insert
|
||||
data := g.Slice{}
|
||||
for i := 1; i <= 5; i++ {
|
||||
data = append(data, g.Map{
|
||||
"id": i,
|
||||
"passport": fmt.Sprintf("original_%d", i),
|
||||
})
|
||||
}
|
||||
_, err := db.Model(table).Data(data).Insert()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Batch replace with overlapping ids
|
||||
replaceData := g.Slice{}
|
||||
for i := 3; i <= 8; i++ {
|
||||
replaceData = append(replaceData, g.Map{
|
||||
"id": i,
|
||||
"passport": fmt.Sprintf("replaced_%d", i),
|
||||
"nickname": fmt.Sprintf("new_name_%d", i),
|
||||
})
|
||||
}
|
||||
result, err := db.Model(table).Batch(2).Data(replaceData).Replace()
|
||||
t.AssertNil(err)
|
||||
n, _ := result.RowsAffected()
|
||||
t.AssertGT(n, 0)
|
||||
|
||||
// Verify replaced records
|
||||
one, err := db.Model(table).Where("id", 3).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], "replaced_3")
|
||||
t.Assert(one["nickname"], "new_name_3")
|
||||
|
||||
// Verify new records
|
||||
one, err = db.Model(table).Where("id", 8).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], "replaced_8")
|
||||
|
||||
// Verify total count
|
||||
count, err := db.Model(table).Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(count, 8) // ids 1-8
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Batch_Save tests batch save operation
|
||||
func Test_Model_Batch_Save(t *testing.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Initial data
|
||||
data := g.Slice{}
|
||||
for i := 1; i <= 5; i++ {
|
||||
data = append(data, g.Map{
|
||||
"id": i,
|
||||
"passport": fmt.Sprintf("save_user_%d", i),
|
||||
})
|
||||
}
|
||||
_, err := db.Model(table).Data(data).Insert()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Batch save with overlapping and new ids
|
||||
saveData := g.Slice{}
|
||||
for i := 3; i <= 8; i++ {
|
||||
saveData = append(saveData, g.Map{
|
||||
"id": i,
|
||||
"passport": fmt.Sprintf("saved_%d", i),
|
||||
"nickname": fmt.Sprintf("save_name_%d", i),
|
||||
})
|
||||
}
|
||||
result, err := db.Model(table).Batch(3).Data(saveData).Save()
|
||||
t.AssertNil(err)
|
||||
n, _ := result.RowsAffected()
|
||||
t.AssertGT(n, 0)
|
||||
|
||||
// Verify updated records
|
||||
one, err := db.Model(table).Where("id", 3).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], "saved_3")
|
||||
|
||||
// Verify inserted records
|
||||
one, err = db.Model(table).Where("id", 8).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], "saved_8")
|
||||
|
||||
// Verify total count
|
||||
count, err := db.Model(table).Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(count, 8)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Batch_LargeBatch tests batch operation with large dataset
|
||||
func Test_Model_Batch_LargeBatch(t *testing.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Prepare 1000+ records
|
||||
data := g.Slice{}
|
||||
totalRecords := 1500
|
||||
for i := 1; i <= totalRecords; i++ {
|
||||
data = append(data, g.Map{
|
||||
"id": i,
|
||||
"passport": fmt.Sprintf("large_user_%d", i),
|
||||
"nickname": fmt.Sprintf("large_name_%d", i),
|
||||
})
|
||||
}
|
||||
|
||||
// Batch insert with batch size 100
|
||||
result, err := db.Model(table).Batch(100).Data(data).Insert()
|
||||
t.AssertNil(err)
|
||||
n, _ := result.RowsAffected()
|
||||
t.Assert(n, totalRecords)
|
||||
|
||||
// Verify count
|
||||
count, err := db.Model(table).Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(count, totalRecords)
|
||||
|
||||
// Verify first and last records
|
||||
one, err := db.Model(table).Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], "large_user_1")
|
||||
|
||||
one, err = db.Model(table).Where("id", totalRecords).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], fmt.Sprintf("large_user_%d", totalRecords))
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Batch_EmptyBatch tests batch operation with empty data
|
||||
func Test_Model_Batch_EmptyBatch(t *testing.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Empty slice
|
||||
data := g.Slice{}
|
||||
|
||||
// Batch insert with empty data should return error
|
||||
_, err := db.Model(table).Batch(10).Data(data).Insert()
|
||||
t.AssertNE(err, nil)
|
||||
t.AssertIN(err.Error(), "data list cannot be empty")
|
||||
|
||||
// Verify no records inserted
|
||||
count, err := db.Model(table).Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(count, 0)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Batch_SingleRecord tests batch operation with single record
|
||||
func Test_Model_Batch_SingleRecord(t *testing.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Single record batch insert
|
||||
data := g.Slice{
|
||||
g.Map{
|
||||
"id": 1,
|
||||
"passport": "single_user",
|
||||
"nickname": "single_name",
|
||||
},
|
||||
}
|
||||
|
||||
result, err := db.Model(table).Batch(10).Data(data).Insert()
|
||||
t.AssertNil(err)
|
||||
n, _ := result.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
|
||||
// Verify the record
|
||||
one, err := db.Model(table).Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], "single_user")
|
||||
t.Assert(one["nickname"], "single_name")
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Batch_VsBatch tests performance comparison between different batch sizes
|
||||
func Test_Model_Batch_VsBatch(t *testing.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Prepare data
|
||||
data := g.Slice{}
|
||||
for i := 1; i <= 100; i++ {
|
||||
data = append(data, g.Map{
|
||||
"id": i,
|
||||
"passport": fmt.Sprintf("perf_user_%d", i),
|
||||
})
|
||||
}
|
||||
|
||||
// Test with batch size 1
|
||||
result, err := db.Model(table).Batch(1).Data(data).Insert()
|
||||
t.AssertNil(err)
|
||||
n, _ := result.RowsAffected()
|
||||
t.Assert(n, 100)
|
||||
|
||||
// Clean up
|
||||
_, err = db.Model(table).Where("1=1").Delete()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Test with batch size 10
|
||||
result, err = db.Model(table).Batch(10).Data(data).Insert()
|
||||
t.AssertNil(err)
|
||||
n, _ = result.RowsAffected()
|
||||
t.Assert(n, 100)
|
||||
|
||||
// Clean up
|
||||
_, err = db.Model(table).Where("1=1").Delete()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Test with batch size 50
|
||||
result, err = db.Model(table).Batch(50).Data(data).Insert()
|
||||
t.AssertNil(err)
|
||||
n, _ = result.RowsAffected()
|
||||
t.Assert(n, 100)
|
||||
|
||||
// All batch sizes should produce same result
|
||||
count, err := db.Model(table).Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(count, 100)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Batch_WithTransaction tests batch operation within transaction
|
||||
func Test_Model_Batch_WithTransaction(t *testing.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
data := g.Slice{}
|
||||
for i := 1; i <= 50; i++ {
|
||||
data = append(data, g.Map{
|
||||
"id": i,
|
||||
"passport": fmt.Sprintf("tx_batch_%d", i),
|
||||
})
|
||||
}
|
||||
|
||||
// Test commit
|
||||
err := db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
result, err := tx.Model(table).Batch(10).Data(data).Insert()
|
||||
t.AssertNil(err)
|
||||
n, _ := result.RowsAffected()
|
||||
t.Assert(n, 50)
|
||||
return nil
|
||||
})
|
||||
t.AssertNil(err)
|
||||
|
||||
// Verify commit
|
||||
count, err := db.Model(table).Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(count, 50)
|
||||
|
||||
// Clean up
|
||||
_, err = db.Model(table).Where("1=1").Delete()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Test rollback
|
||||
err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
_, err := tx.Model(table).Batch(10).Data(data).Insert()
|
||||
t.AssertNil(err)
|
||||
return fmt.Errorf("rollback test")
|
||||
})
|
||||
t.AssertNE(err, nil)
|
||||
|
||||
// Verify rollback - no records should exist
|
||||
count, err = db.Model(table).Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(count, 0)
|
||||
})
|
||||
}
|
||||
@ -1,300 +0,0 @@
|
||||
// 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 mariadb_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
)
|
||||
|
||||
// Test_Model_Cache_Basic tests basic cache functionality
|
||||
func Test_Model_Cache_Basic(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// First query - cache miss, result from DB
|
||||
one, err := db.Model(table).Cache(gdb.CacheOption{
|
||||
Duration: time.Second * 10,
|
||||
Name: "test_cache_basic",
|
||||
}).Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["id"], 1)
|
||||
t.Assert(one["passport"], "user_1")
|
||||
|
||||
// Update the record in DB
|
||||
_, err = db.Model(table).Data(g.Map{"passport": "updated_user"}).Where("id", 1).Update()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Second query - cache hit, still returns old cached value
|
||||
one, err = db.Model(table).Cache(gdb.CacheOption{
|
||||
Duration: time.Second * 10,
|
||||
Name: "test_cache_basic",
|
||||
}).Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], "user_1") // cached value, not "updated_user"
|
||||
|
||||
// Query without cache - returns updated value from DB
|
||||
one, err = db.Model(table).Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], "updated_user")
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Cache_TTL tests cache TTL expiration
|
||||
func Test_Model_Cache_TTL(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Cache with short TTL
|
||||
one, err := db.Model(table).Cache(gdb.CacheOption{
|
||||
Duration: time.Millisecond * 100, // 100ms TTL
|
||||
Name: "test_cache_ttl",
|
||||
}).Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], "user_1")
|
||||
|
||||
// Update record
|
||||
_, err = db.Model(table).Data(g.Map{"passport": "ttl_test"}).Where("id", 1).Update()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Immediate query - cache still valid
|
||||
one, err = db.Model(table).Cache(gdb.CacheOption{
|
||||
Duration: time.Millisecond * 100,
|
||||
Name: "test_cache_ttl",
|
||||
}).Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], "user_1") // cached value
|
||||
|
||||
// Wait for cache to expire
|
||||
time.Sleep(time.Millisecond * 150)
|
||||
|
||||
// Query after expiration - should get fresh data
|
||||
one, err = db.Model(table).Cache(gdb.CacheOption{
|
||||
Duration: time.Millisecond * 100,
|
||||
Name: "test_cache_ttl",
|
||||
}).Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], "ttl_test") // fresh value from DB
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Cache_Clear tests clearing cache with negative duration
|
||||
func Test_Model_Cache_Clear(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Set cache
|
||||
one, err := db.Model(table).Cache(gdb.CacheOption{
|
||||
Duration: time.Second * 60,
|
||||
Name: "test_cache_clear",
|
||||
}).Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], "user_1")
|
||||
|
||||
// Update record and clear cache
|
||||
_, err = db.Model(table).Cache(gdb.CacheOption{
|
||||
Duration: -1,
|
||||
Name: "test_cache_clear",
|
||||
}).Data(g.Map{"passport": "cleared"}).Where("id", 1).Update()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Query again - should get fresh data since cache was cleared
|
||||
one, err = db.Model(table).Cache(gdb.CacheOption{
|
||||
Duration: time.Second * 60,
|
||||
Name: "test_cache_clear",
|
||||
}).Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], "cleared")
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Cache_NoExpire tests cache with no expiration (Duration=0)
|
||||
func Test_Model_Cache_NoExpire(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Cache with no expiration
|
||||
one, err := db.Model(table).Cache(gdb.CacheOption{
|
||||
Duration: 0, // never expires
|
||||
Name: "test_cache_no_expire",
|
||||
}).Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], "user_1")
|
||||
|
||||
// Update record
|
||||
_, err = db.Model(table).Data(g.Map{"passport": "no_expire_test"}).Where("id", 1).Update()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Wait a bit
|
||||
time.Sleep(time.Millisecond * 100)
|
||||
|
||||
// Query - cache should still be valid
|
||||
one, err = db.Model(table).Cache(gdb.CacheOption{
|
||||
Duration: 0,
|
||||
Name: "test_cache_no_expire",
|
||||
}).Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], "user_1") // cached value persists
|
||||
|
||||
// Clear the cache with update operation
|
||||
_, err = db.Model(table).Cache(gdb.CacheOption{
|
||||
Duration: -1,
|
||||
Name: "test_cache_no_expire",
|
||||
}).Data(g.Map{"nickname": "cleared"}).Where("id", 1).Update()
|
||||
t.AssertNil(err)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Cache_Force tests Force option to cache nil results
|
||||
func Test_Model_Cache_Force(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
// Note: Removed Force cache test due to cache invalidation on INSERT
|
||||
// The test logic was flawed - INSERT operations clear cache, so cached nil
|
||||
// results would be invalidated before the second query
|
||||
}
|
||||
|
||||
// Test_Model_Cache_DisabledInTransaction tests cache is disabled in transactions
|
||||
func Test_Model_Cache_DisabledInTransaction(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
err := db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
// First query in transaction
|
||||
one, err := tx.Model(table).Cache(gdb.CacheOption{
|
||||
Duration: time.Second * 10,
|
||||
Name: "test_tx_cache",
|
||||
}).Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], "user_1")
|
||||
|
||||
// Update in transaction
|
||||
_, err = tx.Model(table).Data(g.Map{"passport": "tx_update"}).Where("id", 1).Update()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Second query - should see updated value (cache disabled in tx)
|
||||
one, err = tx.Model(table).Cache(gdb.CacheOption{
|
||||
Duration: time.Second * 10,
|
||||
Name: "test_tx_cache",
|
||||
}).Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], "tx_update") // not cached, fresh from DB
|
||||
|
||||
return nil
|
||||
})
|
||||
t.AssertNil(err)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_PageCache tests pagination cache
|
||||
func Test_Model_PageCache(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// First page query with cache
|
||||
all, err := db.Model(table).PageCache(
|
||||
gdb.CacheOption{Duration: time.Second * 10, Name: "test_page_count"},
|
||||
gdb.CacheOption{Duration: time.Second * 10, Name: "test_page_data"},
|
||||
).Page(1, 3).All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(all), 3)
|
||||
|
||||
// Insert new record
|
||||
_, err = db.Model(table).Data(g.Map{
|
||||
"id": 11,
|
||||
"passport": "user_11",
|
||||
}).Insert()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Query again - should return cached results
|
||||
all, err = db.Model(table).PageCache(
|
||||
gdb.CacheOption{Duration: time.Second * 10, Name: "test_page_count"},
|
||||
gdb.CacheOption{Duration: time.Second * 10, Name: "test_page_data"},
|
||||
).Page(1, 3).All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(all), 3) // cached results
|
||||
|
||||
// Clear page cache by updating with Duration=-1
|
||||
_, err = db.Model(table).Cache(gdb.CacheOption{
|
||||
Duration: -1,
|
||||
Name: "test_page_count",
|
||||
}).Data(g.Map{"nickname": "page_test"}).Where("id", 1).Update()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Query with fresh cache - should return updated count
|
||||
all, err = db.Model(table).PageCache(
|
||||
gdb.CacheOption{Duration: time.Second * 10, Name: "test_page_count"},
|
||||
gdb.CacheOption{Duration: time.Second * 10, Name: "test_page_data"},
|
||||
).Page(1, 3).All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(all), 3) // still 3 items per page
|
||||
|
||||
// Verify total count increased
|
||||
count, err := db.Model(table).Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(count, 11)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Cache_DifferentNames tests different cache names for same query
|
||||
func Test_Model_Cache_DifferentNames(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Cache with name1
|
||||
one, err := db.Model(table).Cache(gdb.CacheOption{
|
||||
Duration: time.Second * 10,
|
||||
Name: "cache_name1",
|
||||
}).Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], "user_1")
|
||||
|
||||
// Cache same query with name2
|
||||
one, err = db.Model(table).Cache(gdb.CacheOption{
|
||||
Duration: time.Second * 10,
|
||||
Name: "cache_name2",
|
||||
}).Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], "user_1")
|
||||
|
||||
// Update record and clear only cache_name1
|
||||
_, err = db.Model(table).Cache(gdb.CacheOption{
|
||||
Duration: -1,
|
||||
Name: "cache_name1",
|
||||
}).Data(g.Map{"passport": "diff_name"}).Where("id", 1).Update()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Query with cache_name1 - should get fresh data
|
||||
one, err = db.Model(table).Cache(gdb.CacheOption{
|
||||
Duration: time.Second * 10,
|
||||
Name: "cache_name1",
|
||||
}).Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], "diff_name")
|
||||
|
||||
// Query with cache_name2 - should still have cached old value
|
||||
one, err = db.Model(table).Cache(gdb.CacheOption{
|
||||
Duration: time.Second * 10,
|
||||
Name: "cache_name2",
|
||||
}).Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], "user_1") // still cached
|
||||
})
|
||||
}
|
||||
@ -1,338 +0,0 @@
|
||||
// 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 mariadb_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
)
|
||||
|
||||
// Test_Concurrent_Insert tests concurrent Insert operations
|
||||
func Test_Concurrent_Insert(t *testing.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var wg sync.WaitGroup
|
||||
concurrency := 10
|
||||
|
||||
wg.Add(concurrency)
|
||||
for i := 0; i < concurrency; i++ {
|
||||
go func(id int) {
|
||||
defer wg.Done()
|
||||
_, err := db.Model(table).Insert(g.Map{
|
||||
"passport": fmt.Sprintf("user_%d", id),
|
||||
"password": fmt.Sprintf("pass_%d", id),
|
||||
"nickname": fmt.Sprintf("name_%d", id),
|
||||
})
|
||||
t.AssertNil(err)
|
||||
}(i + 1)
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
// Verify all records inserted
|
||||
count, err := db.Model(table).Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(count, concurrency)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Concurrent_Update tests concurrent Update operations
|
||||
func Test_Concurrent_Update(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var wg sync.WaitGroup
|
||||
concurrency := 5
|
||||
|
||||
wg.Add(concurrency)
|
||||
for i := 0; i < concurrency; i++ {
|
||||
go func(id int) {
|
||||
defer wg.Done()
|
||||
_, err := db.Model(table).Data(g.Map{
|
||||
"nickname": fmt.Sprintf("updated_%d", id),
|
||||
}).Where("id", id+1).Update()
|
||||
t.AssertNil(err)
|
||||
}(i)
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
// Verify updates
|
||||
for i := 0; i < concurrency; i++ {
|
||||
one, err := db.Model(table).Where("id", i+1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["nickname"].String(), fmt.Sprintf("updated_%d", i))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Concurrent_Delete tests concurrent Delete operations
|
||||
func Test_Concurrent_Delete(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var wg sync.WaitGroup
|
||||
concurrency := 5
|
||||
|
||||
wg.Add(concurrency)
|
||||
for i := 0; i < concurrency; i++ {
|
||||
go func(id int) {
|
||||
defer wg.Done()
|
||||
_, err := db.Model(table).Where("id", id+1).Delete()
|
||||
t.AssertNil(err)
|
||||
}(i)
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
// Verify deletions
|
||||
count, err := db.Model(table).Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(count, TableSize-concurrency)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Concurrent_Query tests concurrent Query operations
|
||||
func Test_Concurrent_Query(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var wg sync.WaitGroup
|
||||
concurrency := 20
|
||||
|
||||
wg.Add(concurrency)
|
||||
for i := 0; i < concurrency; i++ {
|
||||
go func(id int) {
|
||||
defer wg.Done()
|
||||
result, err := db.Model(table).Where("id", (id%TableSize)+1).One()
|
||||
t.AssertNil(err)
|
||||
t.AssertNE(result, nil)
|
||||
}(i)
|
||||
}
|
||||
wg.Wait()
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Concurrent_Transaction tests concurrent transaction operations
|
||||
func Test_Concurrent_Transaction(t *testing.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var wg sync.WaitGroup
|
||||
concurrency := 10
|
||||
|
||||
wg.Add(concurrency)
|
||||
for i := 0; i < concurrency; i++ {
|
||||
go func(id int) {
|
||||
defer wg.Done()
|
||||
err := db.Transaction(ctx, func(ctx g.Ctx, tx gdb.TX) error {
|
||||
_, err := tx.Model(table).Insert(g.Map{
|
||||
"passport": fmt.Sprintf("user_%d", id),
|
||||
"password": fmt.Sprintf("pass_%d", id),
|
||||
"nickname": fmt.Sprintf("name_%d", id),
|
||||
})
|
||||
return err
|
||||
})
|
||||
t.AssertNil(err)
|
||||
}(i + 1)
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
// Verify all transactions committed
|
||||
count, err := db.Model(table).Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(count, concurrency)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Concurrent_Mixed_Operations tests mixed concurrent operations
|
||||
func Test_Concurrent_Mixed_Operations(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var wg sync.WaitGroup
|
||||
operations := 30
|
||||
|
||||
wg.Add(operations)
|
||||
for i := 0; i < operations; i++ {
|
||||
op := i % 3
|
||||
switch op {
|
||||
case 0: // Insert
|
||||
go func(id int) {
|
||||
defer wg.Done()
|
||||
_, _ = db.Model(table).Insert(g.Map{
|
||||
"passport": fmt.Sprintf("new_user_%d", id),
|
||||
"password": fmt.Sprintf("new_pass_%d", id),
|
||||
"nickname": fmt.Sprintf("new_name_%d", id),
|
||||
})
|
||||
}(i)
|
||||
case 1: // Update
|
||||
go func(id int) {
|
||||
defer wg.Done()
|
||||
targetId := (id % TableSize) + 1
|
||||
_, _ = db.Model(table).Data(g.Map{
|
||||
"nickname": fmt.Sprintf("concurrent_%d", id),
|
||||
}).Where("id", targetId).Update()
|
||||
}(i)
|
||||
case 2: // Query
|
||||
go func(id int) {
|
||||
defer wg.Done()
|
||||
targetId := (id % TableSize) + 1
|
||||
_, _ = db.Model(table).Where("id", targetId).One()
|
||||
}(i)
|
||||
}
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
// Verify database is still consistent
|
||||
count, err := db.Model(table).Count()
|
||||
t.AssertNil(err)
|
||||
t.AssertGT(count, TableSize)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Concurrent_Connection_Pool tests connection pool under load
|
||||
func Test_Concurrent_Connection_Pool(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var wg sync.WaitGroup
|
||||
concurrency := 50
|
||||
|
||||
wg.Add(concurrency)
|
||||
for i := 0; i < concurrency; i++ {
|
||||
go func(id int) {
|
||||
defer wg.Done()
|
||||
// Each goroutine performs multiple operations
|
||||
for j := 0; j < 5; j++ {
|
||||
_, err := db.Model(table).Where("id", (id%TableSize)+1).One()
|
||||
t.AssertNil(err)
|
||||
}
|
||||
}(i)
|
||||
}
|
||||
wg.Wait()
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Concurrent_Schema_Switch tests concurrent schema switching
|
||||
func Test_Concurrent_Schema_Switch(t *testing.T) {
|
||||
table1 := createTableWithDb(db, "test_schema_1")
|
||||
table2 := createTableWithDb(db2, "test_schema_2")
|
||||
defer dropTableWithDb(db, table1)
|
||||
defer dropTableWithDb(db2, table2)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var wg sync.WaitGroup
|
||||
concurrency := 10
|
||||
|
||||
wg.Add(concurrency * 2)
|
||||
for i := 0; i < concurrency; i++ {
|
||||
// Insert to schema1
|
||||
go func(id int) {
|
||||
defer wg.Done()
|
||||
_, err := db.Model(table1).Insert(g.Map{
|
||||
"passport": fmt.Sprintf("user_s1_%d", id),
|
||||
"password": fmt.Sprintf("pass_%d", id),
|
||||
"nickname": fmt.Sprintf("name_%d", id),
|
||||
})
|
||||
t.AssertNil(err)
|
||||
}(i)
|
||||
|
||||
// Insert to schema2
|
||||
go func(id int) {
|
||||
defer wg.Done()
|
||||
_, err := db2.Model(table2).Insert(g.Map{
|
||||
"passport": fmt.Sprintf("user_s2_%d", id),
|
||||
"password": fmt.Sprintf("pass_%d", id),
|
||||
"nickname": fmt.Sprintf("name_%d", id),
|
||||
})
|
||||
t.AssertNil(err)
|
||||
}(i)
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
// Verify both schemas
|
||||
count1, err := db.Model(table1).Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(count1, concurrency)
|
||||
|
||||
count2, err := db2.Model(table2).Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(count2, concurrency)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Concurrent_Model_Clone tests concurrent model cloning
|
||||
func Test_Concurrent_Model_Clone(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
baseModel := db.Model(table).Where("id>", 0)
|
||||
var wg sync.WaitGroup
|
||||
concurrency := 20
|
||||
|
||||
wg.Add(concurrency)
|
||||
for i := 0; i < concurrency; i++ {
|
||||
go func(id int) {
|
||||
defer wg.Done()
|
||||
// Clone model for each goroutine
|
||||
m := baseModel.Clone()
|
||||
result, err := m.Where("id<=", TableSize/2).All()
|
||||
t.AssertNil(err)
|
||||
t.AssertGT(len(result), 0)
|
||||
}(i)
|
||||
}
|
||||
wg.Wait()
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Concurrent_Batch_Insert tests concurrent batch insert operations
|
||||
func Test_Concurrent_Batch_Insert(t *testing.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var wg sync.WaitGroup
|
||||
concurrency := 5
|
||||
batchSize := 10
|
||||
|
||||
wg.Add(concurrency)
|
||||
for i := 0; i < concurrency; i++ {
|
||||
go func(batchId int) {
|
||||
defer wg.Done()
|
||||
batch := make([]g.Map, 0, batchSize)
|
||||
for j := 0; j < batchSize; j++ {
|
||||
id := batchId*batchSize + j
|
||||
batch = append(batch, g.Map{
|
||||
"passport": fmt.Sprintf("batch_user_%d", id),
|
||||
"password": fmt.Sprintf("pass_%d", id),
|
||||
"nickname": fmt.Sprintf("name_%d", id),
|
||||
})
|
||||
}
|
||||
_, err := db.Model(table).Data(batch).Insert()
|
||||
t.AssertNil(err)
|
||||
}(i)
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
// Verify all batch inserts
|
||||
count, err := db.Model(table).Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(count, concurrency*batchSize)
|
||||
})
|
||||
}
|
||||
@ -1,163 +0,0 @@
|
||||
// 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 mariadb_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/os/glog"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
)
|
||||
|
||||
func Test_Ctx(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
db, err := gdb.Instance()
|
||||
t.AssertNil(err)
|
||||
|
||||
err1 := db.PingMaster()
|
||||
err2 := db.PingSlave()
|
||||
t.Assert(err1, nil)
|
||||
t.Assert(err2, nil)
|
||||
|
||||
newDb := db.Ctx(context.Background())
|
||||
t.AssertNE(newDb, nil)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Ctx_Query(t *testing.T) {
|
||||
db.GetLogger().(*glog.Logger).SetCtxKeys("SpanId", "TraceId")
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
db.SetDebug(true)
|
||||
defer db.SetDebug(false)
|
||||
ctx := context.WithValue(context.Background(), "TraceId", "12345678")
|
||||
ctx = context.WithValue(ctx, "SpanId", "0.1")
|
||||
db.Query(ctx, "select 1")
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
db.SetDebug(true)
|
||||
defer db.SetDebug(false)
|
||||
db.Query(ctx, "select 2")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Ctx_Model(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
db.GetLogger().(*glog.Logger).SetCtxKeys("SpanId", "TraceId")
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
db.SetDebug(true)
|
||||
defer db.SetDebug(false)
|
||||
ctx := context.WithValue(context.Background(), "TraceId", "12345678")
|
||||
ctx = context.WithValue(ctx, "SpanId", "0.1")
|
||||
db.Model(table).Ctx(ctx).All()
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
db.SetDebug(true)
|
||||
defer db.SetDebug(false)
|
||||
db.Model(table).All()
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Ctx_Timeout tests context timeout behavior
|
||||
func Test_Ctx_Timeout(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Create a context with very short timeout
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Nanosecond)
|
||||
defer cancel()
|
||||
|
||||
// Wait for timeout
|
||||
time.Sleep(1 * time.Millisecond)
|
||||
|
||||
// Query should fail due to context timeout
|
||||
_, err := db.Model(table).Ctx(ctx).All()
|
||||
t.AssertNE(err, nil)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Ctx_Cancel tests context cancellation
|
||||
func Test_Ctx_Cancel(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
// Cancel immediately
|
||||
cancel()
|
||||
|
||||
// Query should fail due to cancelled context
|
||||
_, err := db.Model(table).Ctx(ctx).All()
|
||||
t.AssertNE(err, nil)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Ctx_Propagation_Transaction tests context propagation in transaction
|
||||
func Test_Ctx_Propagation_Transaction(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
db.GetLogger().(*glog.Logger).SetCtxKeys("TraceId")
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
db.SetDebug(true)
|
||||
defer db.SetDebug(false)
|
||||
|
||||
ctx := context.WithValue(context.Background(), "TraceId", "tx_trace_123")
|
||||
err := db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
// Context should propagate to transaction operations
|
||||
_, err := tx.Model(table).Ctx(ctx).Where("id", 1).One()
|
||||
return err
|
||||
})
|
||||
t.AssertNil(err)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Ctx_Multiple_Values tests context with multiple values
|
||||
func Test_Ctx_Multiple_Values(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
db.GetLogger().(*glog.Logger).SetCtxKeys("TraceId", "RequestId", "UserId")
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
db.SetDebug(true)
|
||||
defer db.SetDebug(false)
|
||||
|
||||
ctx := context.WithValue(context.Background(), "TraceId", "trace_001")
|
||||
ctx = context.WithValue(ctx, "RequestId", "req_002")
|
||||
ctx = context.WithValue(ctx, "UserId", "user_003")
|
||||
|
||||
db.Model(table).Ctx(ctx).Where("id", 1).One()
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Ctx_Nested_Operations tests context in nested operations
|
||||
func Test_Ctx_Nested_Operations(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
db.GetLogger().(*glog.Logger).SetCtxKeys("TraceId")
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
db.SetDebug(true)
|
||||
defer db.SetDebug(false)
|
||||
|
||||
ctx := context.WithValue(context.Background(), "TraceId", "nested_trace")
|
||||
|
||||
// Nested query operations should all have context
|
||||
result, err := db.Model(table).Ctx(ctx).Where("id>", 0).All()
|
||||
t.AssertNil(err)
|
||||
|
||||
if len(result) > 0 {
|
||||
// Another query using same context
|
||||
_, err = db.Model(table).Ctx(ctx).Where("id", result[0]["id"]).One()
|
||||
t.AssertNil(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
@ -1,489 +0,0 @@
|
||||
// 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 mariadb_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
)
|
||||
|
||||
// Test_Model_Insert_NilData tests Insert with nil data
|
||||
func Test_Model_Insert_NilData(t *testing.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
_, err := db.Model(table).Data(nil).Insert()
|
||||
t.AssertNE(err, nil)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Insert_EmptyMap tests Insert with empty map
|
||||
func Test_Model_Insert_EmptyMap(t *testing.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
_, err := db.Model(table).Data(g.Map{}).Insert()
|
||||
t.AssertNE(err, nil)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Insert_EmptySlice tests Insert with empty slice
|
||||
func Test_Model_Insert_EmptySlice(t *testing.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
_, err := db.Model(table).Data(g.Slice{}).Insert()
|
||||
t.AssertNE(err, nil)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Update_NilData tests Update with nil data
|
||||
func Test_Model_Update_NilData(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
_, err := db.Model(table).Data(nil).Where("id", 1).Update()
|
||||
t.AssertNE(err, nil)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Update_EmptyData tests Update with empty data
|
||||
func Test_Model_Update_EmptyData(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
_, err := db.Model(table).Data(g.Map{}).Where("id", 1).Update()
|
||||
t.AssertNE(err, nil)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Update_NoWhere tests Update without WHERE clause is rejected by framework
|
||||
func Test_Model_Update_NoWhere(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Framework safety check: Update without WHERE should return error
|
||||
_, err := db.Model(table).Data(g.Map{"nickname": "updated"}).Update()
|
||||
t.AssertNE(err, nil)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Delete_NoWhere tests Delete without WHERE clause is rejected by framework
|
||||
func Test_Model_Delete_NoWhere(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Framework safety check: Delete without WHERE should return error
|
||||
_, err := db.Model(table).Delete()
|
||||
t.AssertNE(err, nil)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Scan_NilPointer tests Scan with nil pointer
|
||||
func Test_Model_Scan_NilPointer(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
err := db.Model(table).Where("id", 1).Scan(nil)
|
||||
t.AssertNE(err, nil)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Scan_InvalidPointer tests Scan with invalid pointer type
|
||||
func Test_Model_Scan_InvalidPointer(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var str string
|
||||
err := db.Model(table).Where("id", 1).Scan(&str)
|
||||
t.AssertNE(err, nil)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Scan_EmptyResult tests Scan with empty result
|
||||
func Test_Model_Scan_EmptyResult(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
type User struct {
|
||||
Id int
|
||||
}
|
||||
|
||||
// Scan initialized struct with empty result returns sql.ErrNoRows
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var user User
|
||||
err := db.Model(table).Where("id > ?", 1000).Scan(&user)
|
||||
t.AssertNE(err, nil)
|
||||
})
|
||||
|
||||
// Scan nil pointer with empty result returns nil error
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var user *User
|
||||
err := db.Model(table).Where("id > ?", 1000).Scan(&user)
|
||||
t.AssertNil(err)
|
||||
t.Assert(user, nil)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Where_InvalidOperator tests Where with invalid operator
|
||||
func Test_Model_Where_InvalidOperator(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Invalid SQL should cause error at query time
|
||||
_, err := db.Model(table).Where("id INVALID_OP ?", 1).All()
|
||||
t.AssertNE(err, nil)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Where_EmptyString tests Where with empty string
|
||||
func Test_Model_Where_EmptyString(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, err := db.Model(table).Where("").All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(result), TableSize) // Empty WHERE returns all
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Fields_InvalidField tests Fields with non-existent field
|
||||
func Test_Model_Fields_InvalidField(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
_, err := db.Model(table).Fields("non_existent_field").All()
|
||||
t.AssertNE(err, nil)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Fields_Empty tests Fields with empty string
|
||||
// Regression test for #4697: Fields("") should handle empty string gracefully
|
||||
// https://github.com/gogf/gf/issues/4697
|
||||
func Test_Model_Fields_Empty(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, err := db.Model(table).Fields("").Limit(1).All()
|
||||
t.AssertNil(err)
|
||||
t.AssertLE(len(result), 1)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Order_InvalidSyntax tests Order with invalid syntax
|
||||
func Test_Model_Order_InvalidSyntax(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Invalid ORDER BY syntax
|
||||
_, err := db.Model(table).Order("id INVALID").All()
|
||||
t.AssertNE(err, nil)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Group_UnknownColumn tests Group with non-existent column
|
||||
func Test_Model_Group_UnknownColumn(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
_, err := db.Model(table).Group("non_existent_field").All()
|
||||
t.AssertNE(err, nil)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_TableNotExist tests querying non-existent table
|
||||
func Test_Model_TableNotExist(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
_, err := db.Model("non_existent_table_xyz").All()
|
||||
t.AssertNE(err, nil)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_InvalidTableName tests invalid table name
|
||||
func Test_Model_InvalidTableName(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Empty table name
|
||||
_, err := db.Model("").All()
|
||||
t.AssertNE(err, nil)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_SQLInjection_Where tests SQL injection prevention in Where
|
||||
func Test_Model_SQLInjection_Where(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Attempt SQL injection through string column parameter.
|
||||
// Using string column `nickname` instead of int column `id`,
|
||||
// because MySQL coerces "1 OR 1=1" to 1 for int columns.
|
||||
maliciousInput := "1 OR 1=1"
|
||||
result, err := db.Model(table).Where("nickname = ?", maliciousInput).All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(result), 0) // Should not return all records
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Attempt SQL injection with quotes, using string column to avoid
|
||||
// MySQL implicit int conversion (which would coerce "1'..." to 1)
|
||||
maliciousInput := "1'; DROP TABLE " + table + "; --"
|
||||
result, err := db.Model(table).Where("nickname = ?", maliciousInput).All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(result), 0)
|
||||
// Table should still exist
|
||||
count, err := db.Model(table).Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(count, TableSize)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_SQLInjection_Insert tests SQL injection prevention in Insert
|
||||
func Test_Model_SQLInjection_Insert(t *testing.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
maliciousData := g.Map{
|
||||
"id": 1,
|
||||
"passport": "'; DROP TABLE " + table + "; --",
|
||||
"password": "pwd",
|
||||
"nickname": "test",
|
||||
}
|
||||
_, err := db.Model(table).Data(maliciousData).Insert()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Verify data was inserted correctly and table still exists
|
||||
one, err := db.Model(table).Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
t.AssertNE(one, nil)
|
||||
t.Assert(one["passport"].String(), "'; DROP TABLE "+table+"; --")
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_SQLInjection_Update tests SQL injection prevention in Update
|
||||
func Test_Model_SQLInjection_Update(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Use shorter malicious string to fit in nickname column
|
||||
maliciousData := g.Map{
|
||||
"nickname": "'; DELETE FROM users; --",
|
||||
}
|
||||
_, err := db.Model(table).Data(maliciousData).Where("id", 1).Update()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Verify only one record was updated (parameterized query prevents injection)
|
||||
one, err := db.Model(table).Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["nickname"].String(), "'; DELETE FROM users; --")
|
||||
|
||||
// Other records should still exist (injection was prevented)
|
||||
count, err := db.Model(table).Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(count, TableSize)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Context_Cancelled tests query with cancelled context
|
||||
func Test_Model_Context_Cancelled(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel() // Cancel immediately
|
||||
|
||||
_, err := db.Model(table).Ctx(ctx).All()
|
||||
t.AssertNE(err, nil)
|
||||
t.Assert(gerror.Is(err, context.Canceled), true)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Value_EmptyResult tests Value with empty result
|
||||
func Test_Model_Value_EmptyResult(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
value, err := db.Model(table).Where("id > ?", 1000).Value()
|
||||
t.AssertNil(err)
|
||||
t.Assert(value.IsEmpty(), true)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Array_EmptyResult tests Array with empty result
|
||||
func Test_Model_Array_EmptyResult(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
array, err := db.Model(table).Where("id > ?", 1000).Array()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(array), 0)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Count_InvalidTable tests Count on invalid table
|
||||
func Test_Model_Count_InvalidTable(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
_, err := db.Model("non_existent_table").Count()
|
||||
t.AssertNE(err, nil)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Max_EmptyResult tests Max with empty result
|
||||
func Test_Model_Max_EmptyResult(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
value, err := db.Model(table).Where("id > ?", 1000).Max("id")
|
||||
t.AssertNil(err)
|
||||
t.Assert(value, 0) // Returns 0 for empty result
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Min_EmptyResult tests Min with empty result
|
||||
func Test_Model_Min_EmptyResult(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
value, err := db.Model(table).Where("id > ?", 1000).Min("id")
|
||||
t.AssertNil(err)
|
||||
t.Assert(value, 0) // Returns 0 for empty result
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Avg_EmptyResult tests Avg with empty result
|
||||
func Test_Model_Avg_EmptyResult(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
value, err := db.Model(table).Where("id > ?", 1000).Avg("id")
|
||||
t.AssertNil(err)
|
||||
t.Assert(value, 0) // Returns 0 for empty result
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Sum_EmptyResult tests Sum with empty result
|
||||
func Test_Model_Sum_EmptyResult(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
value, err := db.Model(table).Where("id > ?", 1000).Sum("id")
|
||||
t.AssertNil(err)
|
||||
t.Assert(value, 0) // Returns 0 for empty result
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_One_NilResult tests One returning nil
|
||||
func Test_Model_One_NilResult(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
one, err := db.Model(table).Where("id > ?", 1000).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one, nil)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_TX_Rollback_AfterError tests transaction rollback after error
|
||||
func Test_TX_Rollback_AfterError(t *testing.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
err := db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
// Insert valid record
|
||||
_, err := tx.Model(table).Data(g.Map{
|
||||
"id": 1,
|
||||
"passport": "pass1",
|
||||
"password": "pwd1",
|
||||
"nickname": "name1",
|
||||
}).Insert()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Insert duplicate id (should fail)
|
||||
_, err = tx.Model(table).Data(g.Map{
|
||||
"id": 1, // Duplicate
|
||||
"passport": "pass2",
|
||||
"password": "pwd2",
|
||||
"nickname": "name2",
|
||||
}).Insert()
|
||||
|
||||
return err // Return error to trigger rollback
|
||||
})
|
||||
|
||||
t.AssertNE(err, nil)
|
||||
|
||||
// Verify rollback - table should be empty
|
||||
count, err := db.Model(table).Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(count, 0)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Insert_DuplicateKey tests handling of duplicate key error
|
||||
func Test_Model_Insert_DuplicateKey(t *testing.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
data := g.Map{
|
||||
"id": 1,
|
||||
"passport": "pass",
|
||||
"password": "pwd",
|
||||
"nickname": "name",
|
||||
}
|
||||
|
||||
// First insert should succeed
|
||||
_, err := db.Model(table).Data(data).Insert()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Second insert with same id should fail
|
||||
_, err = db.Model(table).Data(data).Insert()
|
||||
t.AssertNE(err, nil)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_All_InvalidConnection tests query with invalid connection
|
||||
func Test_Model_All_InvalidConnection(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
if dbInvalid == nil {
|
||||
t.Skip("dbInvalid not configured")
|
||||
}
|
||||
_, err := dbInvalid.Model("test_table").All()
|
||||
t.AssertNE(err, nil)
|
||||
})
|
||||
}
|
||||
@ -1,229 +0,0 @@
|
||||
// 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 mariadb_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/v2/container/gvar"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
)
|
||||
|
||||
func Test_Model_Hook_Select(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := db.Model(table).Hook(gdb.HookHandler{
|
||||
Select: func(ctx context.Context, in *gdb.HookSelectInput) (result gdb.Result, err error) {
|
||||
result, err = in.Next(ctx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for i, record := range result {
|
||||
record["test"] = gvar.New(100 + record["id"].Int())
|
||||
result[i] = record
|
||||
}
|
||||
return
|
||||
},
|
||||
})
|
||||
all, err := m.Where(`id > 6`).OrderAsc(`id`).All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(all), 4)
|
||||
t.Assert(all[0]["id"].Int(), 7)
|
||||
t.Assert(all[0]["test"].Int(), 107)
|
||||
t.Assert(all[1]["test"].Int(), 108)
|
||||
t.Assert(all[2]["test"].Int(), 109)
|
||||
t.Assert(all[3]["test"].Int(), 110)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Hook_Insert(t *testing.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := db.Model(table).Hook(gdb.HookHandler{
|
||||
Insert: func(ctx context.Context, in *gdb.HookInsertInput) (result sql.Result, err error) {
|
||||
for i, item := range in.Data {
|
||||
item["passport"] = fmt.Sprintf(`test_port_%d`, item["id"])
|
||||
item["nickname"] = fmt.Sprintf(`test_name_%d`, item["id"])
|
||||
in.Data[i] = item
|
||||
}
|
||||
return in.Next(ctx)
|
||||
},
|
||||
})
|
||||
_, err := m.Insert(g.Map{
|
||||
"id": 1,
|
||||
"nickname": "name_1",
|
||||
})
|
||||
t.AssertNil(err)
|
||||
one, err := m.One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["id"].Int(), 1)
|
||||
t.Assert(one["passport"], `test_port_1`)
|
||||
t.Assert(one["nickname"], `test_name_1`)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Hook_Update(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := db.Model(table).Hook(gdb.HookHandler{
|
||||
Update: func(ctx context.Context, in *gdb.HookUpdateInput) (result sql.Result, err error) {
|
||||
switch value := in.Data.(type) {
|
||||
case gdb.List:
|
||||
for i, data := range value {
|
||||
data["passport"] = `port`
|
||||
data["nickname"] = `name`
|
||||
value[i] = data
|
||||
}
|
||||
in.Data = value
|
||||
|
||||
case gdb.Map:
|
||||
value["passport"] = `port`
|
||||
value["nickname"] = `name`
|
||||
in.Data = value
|
||||
}
|
||||
return in.Next(ctx)
|
||||
},
|
||||
})
|
||||
_, err := m.Data(g.Map{
|
||||
"nickname": "name_1",
|
||||
}).WherePri(1).Update()
|
||||
t.AssertNil(err)
|
||||
|
||||
one, err := m.One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["id"].Int(), 1)
|
||||
t.Assert(one["passport"], `port`)
|
||||
t.Assert(one["nickname"], `name`)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Hook_Delete(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := db.Model(table).Hook(gdb.HookHandler{
|
||||
Delete: func(ctx context.Context, in *gdb.HookDeleteInput) (result sql.Result, err error) {
|
||||
return db.Model(table).Data(g.Map{
|
||||
"nickname": `deleted`,
|
||||
}).Where(in.Condition).Update()
|
||||
},
|
||||
})
|
||||
_, err := m.Where(1).Delete()
|
||||
t.AssertNil(err)
|
||||
|
||||
all, err := m.All()
|
||||
t.AssertNil(err)
|
||||
for _, item := range all {
|
||||
t.Assert(item["nickname"].String(), `deleted`)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Hook_Multiple tests multiple hooks execution order
|
||||
func Test_Model_Hook_Multiple(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var execOrder []string
|
||||
|
||||
m := db.Model(table).Hook(gdb.HookHandler{
|
||||
Select: func(ctx context.Context, in *gdb.HookSelectInput) (result gdb.Result, err error) {
|
||||
execOrder = append(execOrder, "hook1_before")
|
||||
result, err = in.Next(ctx)
|
||||
execOrder = append(execOrder, "hook1_after")
|
||||
return
|
||||
},
|
||||
}).Hook(gdb.HookHandler{
|
||||
Select: func(ctx context.Context, in *gdb.HookSelectInput) (result gdb.Result, err error) {
|
||||
execOrder = append(execOrder, "hook2_before")
|
||||
result, err = in.Next(ctx)
|
||||
execOrder = append(execOrder, "hook2_after")
|
||||
return
|
||||
},
|
||||
})
|
||||
|
||||
_, err := m.Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Verify only the last registered hook executes (Hook is override, not chain)
|
||||
t.Assert(len(execOrder), 2)
|
||||
t.Assert(execOrder, g.Slice{"hook2_before", "hook2_after"})
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Hook_Error_Abort tests hook returning error aborts operation
|
||||
func Test_Model_Hook_Error_Abort(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := db.Model(table).Hook(gdb.HookHandler{
|
||||
Insert: func(ctx context.Context, in *gdb.HookInsertInput) (result sql.Result, err error) {
|
||||
// Return error to abort insert
|
||||
return nil, fmt.Errorf("hook aborted insert")
|
||||
},
|
||||
})
|
||||
|
||||
_, err := m.Insert(g.Map{
|
||||
"passport": "test_abort",
|
||||
"password": "pass",
|
||||
"nickname": "name",
|
||||
})
|
||||
t.AssertNE(err, nil)
|
||||
t.Assert(err.Error(), "hook aborted insert")
|
||||
|
||||
// Verify record was not inserted
|
||||
count, err := db.Model(table).Where("passport", "test_abort").Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(count, 0)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Hook_Modify_Data tests hook modifying data before insert
|
||||
func Test_Model_Hook_Modify_Data(t *testing.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := db.Model(table).Hook(gdb.HookHandler{
|
||||
Insert: func(ctx context.Context, in *gdb.HookInsertInput) (result sql.Result, err error) {
|
||||
// Modify all data items
|
||||
for i := range in.Data {
|
||||
in.Data[i]["password"] = "encrypted_" + fmt.Sprint(in.Data[i]["password"])
|
||||
in.Data[i]["nickname"] = "verified_" + fmt.Sprint(in.Data[i]["nickname"])
|
||||
}
|
||||
return in.Next(ctx)
|
||||
},
|
||||
})
|
||||
|
||||
_, err := m.Insert(g.Map{
|
||||
"passport": "test_user",
|
||||
"password": "plain123",
|
||||
"nickname": "john",
|
||||
})
|
||||
t.AssertNil(err)
|
||||
|
||||
// Verify data was modified by hook
|
||||
one, err := db.Model(table).Where("passport", "test_user").One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["password"].String(), "encrypted_plain123")
|
||||
t.Assert(one["nickname"].String(), "verified_john")
|
||||
})
|
||||
}
|
||||
@ -1,141 +0,0 @@
|
||||
// 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 mariadb_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/v2/encoding/gjson"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
"github.com/gogf/gf/v2/util/gmeta"
|
||||
)
|
||||
|
||||
func Test_Model_Builder(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := db.Model(table)
|
||||
b := m.Builder()
|
||||
|
||||
all, err := m.Where(
|
||||
b.Where("id", g.Slice{1, 2, 3}).WhereOr("id", g.Slice{4, 5, 6}),
|
||||
).All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(all), 6)
|
||||
})
|
||||
|
||||
// Where And
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := db.Model(table)
|
||||
b := m.Builder()
|
||||
|
||||
all, err := m.Where(
|
||||
b.Where("id", g.Slice{1, 2, 3}).WhereOr("id", g.Slice{4, 5, 6}),
|
||||
).Where(
|
||||
b.Where("id", g.Slice{2, 3}).WhereOr("id", g.Slice{5, 6}),
|
||||
).Where(
|
||||
b.Where("id", g.Slice{3}).Where("id", g.Slice{1, 2, 3}),
|
||||
).All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(all), 1)
|
||||
})
|
||||
|
||||
// Where Or
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := db.Model(table)
|
||||
b := m.Builder()
|
||||
|
||||
all, err := m.WhereOr(
|
||||
b.Where("id", g.Slice{1, 2, 3}).WhereOr("id", g.Slice{4, 5, 6}),
|
||||
).WhereOr(
|
||||
b.Where("id", g.Slice{2, 3}).WhereOr("id", g.Slice{5, 6}),
|
||||
).WhereOr(
|
||||
b.Where("id", g.Slice{3}).Where("id", g.Slice{1, 2, 3}),
|
||||
).All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(all), 6)
|
||||
})
|
||||
|
||||
// Where with struct which has a field type of *gtime.Time
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := db.Model(table)
|
||||
b := m.Builder()
|
||||
|
||||
type Query struct {
|
||||
Id any
|
||||
Nickname *gtime.Time
|
||||
}
|
||||
|
||||
where, args := b.Where(&Query{Id: 1}).Build()
|
||||
t.Assert(where, "`id`=? AND `nickname` IS NULL")
|
||||
t.Assert(args, []any{1})
|
||||
})
|
||||
|
||||
// Where with struct which has a field type of *gjson.Json
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := db.Model(table)
|
||||
b := m.Builder()
|
||||
|
||||
type Query struct {
|
||||
Id any
|
||||
Nickname *gjson.Json
|
||||
}
|
||||
|
||||
where, args := b.Where(&Query{Id: 1}).Build()
|
||||
t.Assert(where, "`id`=? AND `nickname` IS NULL")
|
||||
t.Assert(args, []any{1})
|
||||
})
|
||||
|
||||
// Where with do struct which has a field type of *gtime.Time and generated by gf cli
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := db.Model(table)
|
||||
b := m.Builder()
|
||||
|
||||
type Query struct {
|
||||
gmeta.Meta `orm:"do:true"`
|
||||
Id any
|
||||
Nickname *gtime.Time
|
||||
}
|
||||
|
||||
where, args := b.Where(&Query{Id: 1}).Build()
|
||||
t.Assert(where, "`id`=?")
|
||||
t.Assert(args, []any{1})
|
||||
})
|
||||
|
||||
// Where with do struct which has a field type of *gjson.Json and generated by gf cli
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := db.Model(table)
|
||||
b := m.Builder()
|
||||
|
||||
type Query struct {
|
||||
gmeta.Meta `orm:"do:true"`
|
||||
Id any
|
||||
Nickname *gjson.Json
|
||||
}
|
||||
|
||||
where, args := b.Where(&Query{Id: 1}).Build()
|
||||
t.Assert(where, "`id`=?")
|
||||
t.Assert(args, []any{1})
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Safe_Builder(t *testing.T) {
|
||||
// test whether m.Builder() is chain safe
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
b := db.Model().Builder()
|
||||
b.Where("id", 1)
|
||||
_, args := b.Build()
|
||||
t.AssertNil(args)
|
||||
|
||||
b = b.Where("id", 1)
|
||||
_, args = b.Build()
|
||||
t.Assert(args, g.Slice{1})
|
||||
})
|
||||
}
|
||||
@ -1,390 +0,0 @@
|
||||
// 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 mariadb_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
func Test_Model_Insert_Data_DO(t *testing.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type User struct {
|
||||
g.Meta `orm:"do:true"`
|
||||
Id any
|
||||
Passport any
|
||||
Password any
|
||||
Nickname any
|
||||
CreateTime any
|
||||
}
|
||||
data := User{
|
||||
Id: 1,
|
||||
Passport: "user_1",
|
||||
Password: "pass_1",
|
||||
}
|
||||
result, err := db.Model(table).Data(data).Insert()
|
||||
t.AssertNil(err)
|
||||
n, _ := result.LastInsertId()
|
||||
t.Assert(n, 1)
|
||||
|
||||
one, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one[`id`], `1`)
|
||||
t.Assert(one[`passport`], `user_1`)
|
||||
t.Assert(one[`password`], `pass_1`)
|
||||
t.Assert(one[`nickname`], ``)
|
||||
t.Assert(one[`create_time`], ``)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Insert_Data_List_DO(t *testing.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type User struct {
|
||||
g.Meta `orm:"do:true"`
|
||||
Id any
|
||||
Passport any
|
||||
Password any
|
||||
Nickname any
|
||||
CreateTime any
|
||||
}
|
||||
data := g.Slice{
|
||||
User{
|
||||
Id: 1,
|
||||
Passport: "user_1",
|
||||
Password: "pass_1",
|
||||
},
|
||||
User{
|
||||
Id: 2,
|
||||
Passport: "user_2",
|
||||
Password: "pass_2",
|
||||
},
|
||||
}
|
||||
result, err := db.Model(table).Data(data).Insert()
|
||||
t.AssertNil(err)
|
||||
n, _ := result.LastInsertId()
|
||||
t.Assert(n, 2)
|
||||
|
||||
one, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one[`id`], `1`)
|
||||
t.Assert(one[`passport`], `user_1`)
|
||||
t.Assert(one[`password`], `pass_1`)
|
||||
t.Assert(one[`nickname`], ``)
|
||||
t.Assert(one[`create_time`], ``)
|
||||
|
||||
one, err = db.Model(table).WherePri(2).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one[`id`], `2`)
|
||||
t.Assert(one[`passport`], `user_2`)
|
||||
t.Assert(one[`password`], `pass_2`)
|
||||
t.Assert(one[`nickname`], ``)
|
||||
t.Assert(one[`create_time`], ``)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Update_Data_DO(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type User struct {
|
||||
g.Meta `orm:"do:true"`
|
||||
Id any
|
||||
Passport any
|
||||
Password any
|
||||
Nickname any
|
||||
CreateTime any
|
||||
}
|
||||
data := User{
|
||||
Id: 1,
|
||||
Passport: "user_100",
|
||||
Password: "pass_100",
|
||||
}
|
||||
_, err := db.Model(table).Data(data).WherePri(1).Update()
|
||||
t.AssertNil(err)
|
||||
|
||||
one, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one[`id`], `1`)
|
||||
t.Assert(one[`passport`], `user_100`)
|
||||
t.Assert(one[`password`], `pass_100`)
|
||||
t.Assert(one[`nickname`], `name_1`)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Update_Pointer_Data_DO(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type NN string
|
||||
type Req struct {
|
||||
Id int
|
||||
Passport *string
|
||||
Password *string
|
||||
Nickname *NN
|
||||
}
|
||||
type UserDo struct {
|
||||
g.Meta `orm:"do:true"`
|
||||
Id any
|
||||
Passport any
|
||||
Password any
|
||||
Nickname any
|
||||
CreateTime any
|
||||
}
|
||||
var (
|
||||
nickname = NN("nickname_111")
|
||||
req = Req{
|
||||
Password: gconv.PtrString("12345678"),
|
||||
Nickname: &nickname,
|
||||
}
|
||||
data = UserDo{
|
||||
Passport: req.Passport,
|
||||
Password: req.Password,
|
||||
Nickname: req.Nickname,
|
||||
}
|
||||
)
|
||||
|
||||
_, err := db.Model(table).Data(data).WherePri(1).Update()
|
||||
t.AssertNil(err)
|
||||
|
||||
one, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one[`id`], `1`)
|
||||
t.Assert(one[`password`], `12345678`)
|
||||
t.Assert(one[`nickname`], `nickname_111`)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Where_DO(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type User struct {
|
||||
g.Meta `orm:"do:true"`
|
||||
Id any
|
||||
Passport any
|
||||
Password any
|
||||
Nickname any
|
||||
CreateTime any
|
||||
}
|
||||
where := User{
|
||||
Id: 1,
|
||||
Passport: "user_1",
|
||||
Password: "pass_1",
|
||||
}
|
||||
one, err := db.Model(table).Where(where).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one[`id`], `1`)
|
||||
t.Assert(one[`passport`], `user_1`)
|
||||
t.Assert(one[`password`], `pass_1`)
|
||||
t.Assert(one[`nickname`], `name_1`)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Insert_Data_ForDao(t *testing.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type UserForDao struct {
|
||||
Id any
|
||||
Passport any
|
||||
Password any
|
||||
Nickname any
|
||||
CreateTime any
|
||||
}
|
||||
data := UserForDao{
|
||||
Id: 1,
|
||||
Passport: "user_1",
|
||||
Password: "pass_1",
|
||||
}
|
||||
result, err := db.Model(table).Data(data).Insert()
|
||||
t.AssertNil(err)
|
||||
n, _ := result.LastInsertId()
|
||||
t.Assert(n, 1)
|
||||
|
||||
one, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one[`id`], `1`)
|
||||
t.Assert(one[`passport`], `user_1`)
|
||||
t.Assert(one[`password`], `pass_1`)
|
||||
t.Assert(one[`nickname`], ``)
|
||||
t.Assert(one[`create_time`], ``)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Insert_Data_List_ForDao(t *testing.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type UserForDao struct {
|
||||
Id any
|
||||
Passport any
|
||||
Password any
|
||||
Nickname any
|
||||
CreateTime any
|
||||
}
|
||||
data := g.Slice{
|
||||
UserForDao{
|
||||
Id: 1,
|
||||
Passport: "user_1",
|
||||
Password: "pass_1",
|
||||
},
|
||||
UserForDao{
|
||||
Id: 2,
|
||||
Passport: "user_2",
|
||||
Password: "pass_2",
|
||||
},
|
||||
}
|
||||
result, err := db.Model(table).Data(data).Insert()
|
||||
t.AssertNil(err)
|
||||
n, _ := result.LastInsertId()
|
||||
t.Assert(n, 2)
|
||||
|
||||
one, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one[`id`], `1`)
|
||||
t.Assert(one[`passport`], `user_1`)
|
||||
t.Assert(one[`password`], `pass_1`)
|
||||
t.Assert(one[`nickname`], ``)
|
||||
t.Assert(one[`create_time`], ``)
|
||||
|
||||
one, err = db.Model(table).WherePri(2).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one[`id`], `2`)
|
||||
t.Assert(one[`passport`], `user_2`)
|
||||
t.Assert(one[`password`], `pass_2`)
|
||||
t.Assert(one[`nickname`], ``)
|
||||
t.Assert(one[`create_time`], ``)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Update_Data_ForDao(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type UserForDao struct {
|
||||
Id any
|
||||
Passport any
|
||||
Password any
|
||||
Nickname any
|
||||
CreateTime any
|
||||
}
|
||||
data := UserForDao{
|
||||
Id: 1,
|
||||
Passport: "user_100",
|
||||
Password: "pass_100",
|
||||
}
|
||||
_, err := db.Model(table).Data(data).WherePri(1).Update()
|
||||
t.AssertNil(err)
|
||||
|
||||
one, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one[`id`], `1`)
|
||||
t.Assert(one[`passport`], `user_100`)
|
||||
t.Assert(one[`password`], `pass_100`)
|
||||
t.Assert(one[`nickname`], `name_1`)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Where_ForDao(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type UserForDao struct {
|
||||
Id any
|
||||
Passport any
|
||||
Password any
|
||||
Nickname any
|
||||
CreateTime any
|
||||
}
|
||||
where := UserForDao{
|
||||
Id: 1,
|
||||
Passport: "user_1",
|
||||
Password: "pass_1",
|
||||
}
|
||||
one, err := db.Model(table).Where(where).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one[`id`], `1`)
|
||||
t.Assert(one[`passport`], `user_1`)
|
||||
t.Assert(one[`password`], `pass_1`)
|
||||
t.Assert(one[`nickname`], `name_1`)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Where_FieldPrefix(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
array := gstr.SplitAndTrim(gtest.DataContent(`table_with_prefix.sql`), ";")
|
||||
for _, v := range array {
|
||||
if _, err := db.Exec(ctx, v); err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
}
|
||||
defer dropTable("instance")
|
||||
|
||||
type Instance struct {
|
||||
ID int `orm:"f_id"`
|
||||
Name string
|
||||
}
|
||||
|
||||
type InstanceDo struct {
|
||||
g.Meta `orm:"table:instance, do:true"`
|
||||
ID any `orm:"f_id"`
|
||||
}
|
||||
var instance *Instance
|
||||
err := db.Model("instance").Where(InstanceDo{
|
||||
ID: 1,
|
||||
}).Scan(&instance)
|
||||
t.AssertNil(err)
|
||||
t.AssertNE(instance, nil)
|
||||
t.Assert(instance.ID, 1)
|
||||
t.Assert(instance.Name, "john")
|
||||
})
|
||||
// With omitempty.
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
array := gstr.SplitAndTrim(gtest.DataContent(`table_with_prefix.sql`), ";")
|
||||
for _, v := range array {
|
||||
if _, err := db.Exec(ctx, v); err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
}
|
||||
defer dropTable("instance")
|
||||
|
||||
type Instance struct {
|
||||
ID int `orm:"f_id,omitempty"`
|
||||
Name string
|
||||
}
|
||||
|
||||
type InstanceDo struct {
|
||||
g.Meta `orm:"table:instance, do:true"`
|
||||
ID any `orm:"f_id,omitempty"`
|
||||
}
|
||||
var instance *Instance
|
||||
err := db.Model("instance").Where(InstanceDo{
|
||||
ID: 1,
|
||||
}).Scan(&instance)
|
||||
t.AssertNil(err)
|
||||
t.AssertNE(instance, nil)
|
||||
t.Assert(instance.ID, 1)
|
||||
t.Assert(instance.Name, "john")
|
||||
})
|
||||
}
|
||||
@ -1,513 +0,0 @@
|
||||
// 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 mariadb_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
)
|
||||
|
||||
func Test_Model_LeftJoinOnField(t *testing.T) {
|
||||
var (
|
||||
table1 = gtime.TimestampNanoStr() + "_table1"
|
||||
table2 = gtime.TimestampNanoStr() + "_table2"
|
||||
)
|
||||
createInitTable(table1)
|
||||
defer dropTable(table1)
|
||||
createInitTable(table2)
|
||||
defer dropTable(table2)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
r, err := db.Model(table1).
|
||||
FieldsPrefix(table1, "*").
|
||||
LeftJoinOnField(table2, "id").
|
||||
WhereIn("id", g.Slice{1, 2}).
|
||||
Order("id asc").All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(r), 2)
|
||||
t.Assert(r[0]["id"], "1")
|
||||
t.Assert(r[1]["id"], "2")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_RightJoinOnField(t *testing.T) {
|
||||
var (
|
||||
table1 = gtime.TimestampNanoStr() + "_table1"
|
||||
table2 = gtime.TimestampNanoStr() + "_table2"
|
||||
)
|
||||
createInitTable(table1)
|
||||
defer dropTable(table1)
|
||||
createInitTable(table2)
|
||||
defer dropTable(table2)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
r, err := db.Model(table1).
|
||||
FieldsPrefix(table1, "*").
|
||||
RightJoinOnField(table2, "id").
|
||||
WhereIn("id", g.Slice{1, 2}).
|
||||
Order("id asc").All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(r), 2)
|
||||
t.Assert(r[0]["id"], "1")
|
||||
t.Assert(r[1]["id"], "2")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_InnerJoinOnField(t *testing.T) {
|
||||
var (
|
||||
table1 = gtime.TimestampNanoStr() + "_table1"
|
||||
table2 = gtime.TimestampNanoStr() + "_table2"
|
||||
)
|
||||
createInitTable(table1)
|
||||
defer dropTable(table1)
|
||||
createInitTable(table2)
|
||||
defer dropTable(table2)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
r, err := db.Model(table1).
|
||||
FieldsPrefix(table1, "*").
|
||||
InnerJoinOnField(table2, "id").
|
||||
WhereIn("id", g.Slice{1, 2}).
|
||||
Order("id asc").All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(r), 2)
|
||||
t.Assert(r[0]["id"], "1")
|
||||
t.Assert(r[1]["id"], "2")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_LeftJoinOnFields(t *testing.T) {
|
||||
var (
|
||||
table1 = gtime.TimestampNanoStr() + "_table1"
|
||||
table2 = gtime.TimestampNanoStr() + "_table2"
|
||||
)
|
||||
createInitTable(table1)
|
||||
defer dropTable(table1)
|
||||
createInitTable(table2)
|
||||
defer dropTable(table2)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
r, err := db.Model(table1).
|
||||
FieldsPrefix(table1, "*").
|
||||
LeftJoinOnFields(table2, "id", "=", "id").
|
||||
WhereIn("id", g.Slice{1, 2}).
|
||||
Order("id asc").All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(r), 2)
|
||||
t.Assert(r[0]["id"], "1")
|
||||
t.Assert(r[1]["id"], "2")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_RightJoinOnFields(t *testing.T) {
|
||||
var (
|
||||
table1 = gtime.TimestampNanoStr() + "_table1"
|
||||
table2 = gtime.TimestampNanoStr() + "_table2"
|
||||
)
|
||||
createInitTable(table1)
|
||||
defer dropTable(table1)
|
||||
createInitTable(table2)
|
||||
defer dropTable(table2)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
r, err := db.Model(table1).
|
||||
FieldsPrefix(table1, "*").
|
||||
RightJoinOnFields(table2, "id", "=", "id").
|
||||
WhereIn("id", g.Slice{1, 2}).
|
||||
Order("id asc").All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(r), 2)
|
||||
t.Assert(r[0]["id"], "1")
|
||||
t.Assert(r[1]["id"], "2")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_InnerJoinOnFields(t *testing.T) {
|
||||
var (
|
||||
table1 = gtime.TimestampNanoStr() + "_table1"
|
||||
table2 = gtime.TimestampNanoStr() + "_table2"
|
||||
)
|
||||
createInitTable(table1)
|
||||
defer dropTable(table1)
|
||||
createInitTable(table2)
|
||||
defer dropTable(table2)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
r, err := db.Model(table1).
|
||||
FieldsPrefix(table1, "*").
|
||||
InnerJoinOnFields(table2, "id", "=", "id").
|
||||
WhereIn("id", g.Slice{1, 2}).
|
||||
Order("id asc").All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(r), 2)
|
||||
t.Assert(r[0]["id"], "1")
|
||||
t.Assert(r[1]["id"], "2")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_FieldsPrefix(t *testing.T) {
|
||||
var (
|
||||
table1 = gtime.TimestampNanoStr() + "_table1"
|
||||
table2 = gtime.TimestampNanoStr() + "_table2"
|
||||
)
|
||||
createInitTable(table1)
|
||||
defer dropTable(table1)
|
||||
createInitTable(table2)
|
||||
defer dropTable(table2)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
r, err := db.Model(table1).
|
||||
FieldsPrefix(table1, "id").
|
||||
FieldsPrefix(table2, "nickname").
|
||||
LeftJoinOnField(table2, "id").
|
||||
WhereIn("id", g.Slice{1, 2}).
|
||||
Order("id asc").All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(r), 2)
|
||||
t.Assert(r[0]["id"], "1")
|
||||
t.Assert(r[0]["nickname"], "name_1")
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Join_FiveTables tests complex join with 5+ tables
|
||||
func Test_Model_Join_FiveTables(t *testing.T) {
|
||||
var (
|
||||
table1 = gtime.TimestampNanoStr() + "_table1"
|
||||
table2 = gtime.TimestampNanoStr() + "_table2"
|
||||
table3 = gtime.TimestampNanoStr() + "_table3"
|
||||
table4 = gtime.TimestampNanoStr() + "_table4"
|
||||
table5 = gtime.TimestampNanoStr() + "_table5"
|
||||
)
|
||||
createInitTable(table1)
|
||||
defer dropTable(table1)
|
||||
createInitTable(table2)
|
||||
defer dropTable(table2)
|
||||
createInitTable(table3)
|
||||
defer dropTable(table3)
|
||||
createInitTable(table4)
|
||||
defer dropTable(table4)
|
||||
createInitTable(table5)
|
||||
defer dropTable(table5)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
r, err := db.Model(table1).As("t1").
|
||||
FieldsPrefix("t1", "id", "nickname").
|
||||
FieldsPrefix("t2", "passport").
|
||||
InnerJoin(table2+" AS t2", "t1.id = t2.id").
|
||||
InnerJoin(table3+" AS t3", "t2.id = t3.id").
|
||||
InnerJoin(table4+" AS t4", "t3.id = t4.id").
|
||||
InnerJoin(table5+" AS t5", "t4.id = t5.id").
|
||||
Where("t1.id IN(?)", g.Slice{1, 2, 3}).
|
||||
Order("t1.id asc").
|
||||
All()
|
||||
t.AssertNil(err)
|
||||
|
||||
t.Assert(len(r), 3)
|
||||
t.Assert(r[0]["id"], "1")
|
||||
t.Assert(r[0]["nickname"], "name_1")
|
||||
t.Assert(r[0]["passport"], "user_1")
|
||||
t.Assert(r[2]["id"], "3")
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// 6 tables with mixed join types
|
||||
table6 := gtime.TimestampNanoStr() + "_table6"
|
||||
createInitTable(table6)
|
||||
defer dropTable(table6)
|
||||
|
||||
r, err := db.Model(table1).As("t1").
|
||||
Fields("t1.id").
|
||||
InnerJoin(table2+" AS t2", "t1.id = t2.id").
|
||||
LeftJoin(table3+" AS t3", "t2.id = t3.id").
|
||||
InnerJoin(table4+" AS t4", "t3.id = t4.id").
|
||||
RightJoin(table5+" AS t5", "t4.id = t5.id").
|
||||
LeftJoin(table6+" AS t6", "t5.id = t6.id").
|
||||
Where("t1.id", 5).
|
||||
One()
|
||||
t.AssertNil(err)
|
||||
|
||||
t.Assert(r["id"], "5")
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Join_SelfJoin tests self-join scenarios
|
||||
func Test_Model_Join_SelfJoin(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Self-join to find pairs where a.id < b.id
|
||||
r, err := db.Model(table).As("a").
|
||||
Fields("a.id AS a_id", "b.id AS b_id").
|
||||
InnerJoin(table+" AS b", "a.id < b.id").
|
||||
Where("a.id", 1).
|
||||
Where("b.id <=", 3).
|
||||
Order("b.id asc").
|
||||
All()
|
||||
t.AssertNil(err)
|
||||
|
||||
t.Assert(len(r), 2)
|
||||
t.Assert(r[0]["a_id"], "1")
|
||||
t.Assert(r[0]["b_id"], "2")
|
||||
t.Assert(r[1]["b_id"], "3")
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Self-join with multiple conditions
|
||||
r, err := db.Model(table).As("a").
|
||||
Fields("a.id", "a.nickname", "b.nickname AS other_nickname").
|
||||
LeftJoin(table+" AS b", "a.id = b.id - 1").
|
||||
Where("a.id IN(?)", g.Slice{1, 2}).
|
||||
Order("a.id asc").
|
||||
All()
|
||||
t.AssertNil(err)
|
||||
|
||||
t.Assert(len(r), 2)
|
||||
t.Assert(r[0]["id"], "1")
|
||||
t.Assert(r[0]["nickname"], "name_1")
|
||||
t.Assert(r[0]["other_nickname"], "name_2")
|
||||
t.Assert(r[1]["id"], "2")
|
||||
t.Assert(r[1]["other_nickname"], "name_3")
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Join_LeftJoinNull tests LEFT JOIN NULL handling
|
||||
func Test_Model_Join_LeftJoinNull(t *testing.T) {
|
||||
var (
|
||||
table1 = gtime.TimestampNanoStr() + "_table1"
|
||||
table2 = gtime.TimestampNanoStr() + "_table2"
|
||||
)
|
||||
createInitTable(table1)
|
||||
defer dropTable(table1)
|
||||
|
||||
// Create table2 with only partial data
|
||||
createTable(table2)
|
||||
defer dropTable(table2)
|
||||
_, err := db.Insert(ctx, table2, g.List{
|
||||
{"id": 1, "passport": "user_1", "nickname": "name_1"},
|
||||
{"id": 2, "passport": "user_2", "nickname": "name_2"},
|
||||
})
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// LEFT JOIN - table1 has all records, table2 only has id 1,2
|
||||
r, err := db.Model(table1).As("t1").
|
||||
FieldsPrefix("t1", "id").
|
||||
FieldsPrefix("t2", "nickname").
|
||||
LeftJoin(table2+" AS t2", "t1.id = t2.id").
|
||||
Where("t1.id IN(?)", g.Slice{1, 2, 3}).
|
||||
Order("t1.id asc").
|
||||
All()
|
||||
t.AssertNil(err)
|
||||
|
||||
t.Assert(len(r), 3)
|
||||
t.Assert(r[0]["id"], "1")
|
||||
t.Assert(r[0]["nickname"], "name_1") // matched
|
||||
t.Assert(r[1]["id"], "2")
|
||||
t.Assert(r[1]["nickname"], "name_2") // matched
|
||||
t.Assert(r[2]["id"], "3")
|
||||
// r[2]["nickname"] should be NULL/empty from t2
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Find records where RIGHT table is NULL
|
||||
r, err := db.Model(table1).As("t1").
|
||||
FieldsPrefix("t1", "id", "nickname").
|
||||
LeftJoin(table2+" AS t2", "t1.id = t2.id").
|
||||
Where("t2.id IS NULL").
|
||||
Where("t1.id IN(?)", g.Slice{1, 2, 3, 4}).
|
||||
Order("t1.id asc").
|
||||
All()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Should return id 3,4 (not in table2)
|
||||
t.Assert(len(r), 2)
|
||||
t.Assert(r[0]["id"], "3")
|
||||
t.Assert(r[0]["nickname"], "name_3")
|
||||
t.Assert(r[1]["id"], "4")
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Join_RightJoinNull tests RIGHT JOIN NULL handling
|
||||
func Test_Model_Join_RightJoinNull(t *testing.T) {
|
||||
var (
|
||||
table1 = gtime.TimestampNanoStr() + "_table1"
|
||||
table2 = gtime.TimestampNanoStr() + "_table2"
|
||||
)
|
||||
// table1 has partial data
|
||||
createTable(table1)
|
||||
defer dropTable(table1)
|
||||
_, err := db.Insert(ctx, table1, g.List{
|
||||
{"id": 1, "passport": "user_1", "nickname": "name_1"},
|
||||
{"id": 2, "passport": "user_2", "nickname": "name_2"},
|
||||
})
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
|
||||
// table2 has all data
|
||||
createInitTable(table2)
|
||||
defer dropTable(table2)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// RIGHT JOIN - table1 only has id 1,2, table2 has all
|
||||
r, err := db.Model(table1).As("t1").
|
||||
FieldsPrefix("t2", "id").
|
||||
FieldsPrefix("t1", "nickname").
|
||||
RightJoin(table2+" AS t2", "t1.id = t2.id").
|
||||
Where("t2.id IN(?)", g.Slice{1, 2, 3}).
|
||||
Order("t2.id asc").
|
||||
All()
|
||||
t.AssertNil(err)
|
||||
|
||||
t.Assert(len(r), 3)
|
||||
t.Assert(r[0]["id"], "1")
|
||||
t.Assert(r[0]["nickname"], "name_1") // matched
|
||||
t.Assert(r[1]["id"], "2")
|
||||
t.Assert(r[1]["nickname"], "name_2") // matched
|
||||
t.Assert(r[2]["id"], "3")
|
||||
// r[2]["nickname"] should be NULL/empty from t1
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Find records where LEFT table is NULL
|
||||
r, err := db.Model(table1).As("t1").
|
||||
FieldsPrefix("t2", "id", "nickname").
|
||||
RightJoin(table2+" AS t2", "t1.id = t2.id").
|
||||
Where("t1.id IS NULL").
|
||||
Where("t2.id IN(?)", g.Slice{1, 2, 3, 4}).
|
||||
Order("t2.id asc").
|
||||
All()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Should return id 3,4 (not in table1)
|
||||
t.Assert(len(r), 2)
|
||||
t.Assert(r[0]["id"], "3")
|
||||
t.Assert(r[0]["nickname"], "name_3")
|
||||
t.Assert(r[1]["id"], "4")
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Join_OnVsWhere tests difference between ON and WHERE conditions
|
||||
func Test_Model_Join_OnVsWhere(t *testing.T) {
|
||||
var (
|
||||
table1 = gtime.TimestampNanoStr() + "_table1"
|
||||
table2 = gtime.TimestampNanoStr() + "_table2"
|
||||
)
|
||||
createInitTable(table1)
|
||||
defer dropTable(table1)
|
||||
createInitTable(table2)
|
||||
defer dropTable(table2)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// INNER JOIN: ON and WHERE behave the same
|
||||
r1, err := db.Model(table1).As("t1").
|
||||
Fields("t1.id").
|
||||
InnerJoin(table2+" AS t2", "t1.id = t2.id AND t2.id <= 3").
|
||||
Order("t1.id asc").
|
||||
All()
|
||||
t.AssertNil(err)
|
||||
|
||||
r2, err := db.Model(table1).As("t1").
|
||||
Fields("t1.id").
|
||||
InnerJoin(table2+" AS t2", "t1.id = t2.id").
|
||||
Where("t2.id <=", 3).
|
||||
Order("t1.id asc").
|
||||
All()
|
||||
t.AssertNil(err)
|
||||
|
||||
// For INNER JOIN, results should be identical
|
||||
t.Assert(len(r1), 3)
|
||||
t.Assert(len(r2), 3)
|
||||
t.Assert(r1[0]["id"], r2[0]["id"])
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// LEFT JOIN: ON filter in join condition vs WHERE filter after join
|
||||
// ON condition: filters t2 before join (keeps all t1 rows)
|
||||
r1, err := db.Model(table1).As("t1").
|
||||
FieldsPrefix("t1", "id").
|
||||
FieldsPrefix("t2", "nickname").
|
||||
LeftJoin(table2+" AS t2", "t1.id = t2.id AND t2.id <= 2").
|
||||
Where("t1.id <=", 4).
|
||||
Order("t1.id asc").
|
||||
All()
|
||||
t.AssertNil(err)
|
||||
|
||||
// WHERE condition: filters result after join (removes t1 rows where t2 is NULL)
|
||||
r2, err := db.Model(table1).As("t1").
|
||||
FieldsPrefix("t1", "id").
|
||||
FieldsPrefix("t2", "nickname").
|
||||
LeftJoin(table2+" AS t2", "t1.id = t2.id").
|
||||
Where("t1.id <=", 4).
|
||||
Where("t2.id <=", 2).
|
||||
Order("t1.id asc").
|
||||
All()
|
||||
t.AssertNil(err)
|
||||
|
||||
// r1: all t1 rows (1,2,3,4), t2 data only for id 1,2
|
||||
t.Assert(len(r1), 4)
|
||||
t.Assert(r1[0]["id"], "1")
|
||||
t.Assert(r1[0]["nickname"], "name_1")
|
||||
t.Assert(r1[2]["id"], "3")
|
||||
// r1[2]["nickname"] is NULL from t2
|
||||
|
||||
// r2: only rows where t2.id <= 2, so only id 1,2
|
||||
t.Assert(len(r2), 2)
|
||||
t.Assert(r2[0]["id"], "1")
|
||||
t.Assert(r2[1]["id"], "2")
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Join_ComplexConditions tests joins with complex ON conditions
|
||||
func Test_Model_Join_ComplexConditions(t *testing.T) {
|
||||
var (
|
||||
table1 = gtime.TimestampNanoStr() + "_table1"
|
||||
table2 = gtime.TimestampNanoStr() + "_table2"
|
||||
)
|
||||
createInitTable(table1)
|
||||
defer dropTable(table1)
|
||||
createInitTable(table2)
|
||||
defer dropTable(table2)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Multiple AND conditions in ON clause
|
||||
r, err := db.Model(table1).As("t1").
|
||||
Fields("t1.id", "t1.nickname").
|
||||
InnerJoin(
|
||||
table2+" AS t2",
|
||||
"t1.id = t2.id AND t1.nickname = t2.nickname AND t1.id BETWEEN 2 AND 4",
|
||||
).
|
||||
Order("t1.id asc").
|
||||
All()
|
||||
t.AssertNil(err)
|
||||
|
||||
t.Assert(len(r), 3)
|
||||
t.Assert(r[0]["id"], "2")
|
||||
t.Assert(r[2]["id"], "4")
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// OR conditions in ON clause (need to use Where for OR in join)
|
||||
r, err := db.Model(table1).As("t1").
|
||||
Fields("t1.id").
|
||||
InnerJoin(table2+" AS t2", "t1.id = t2.id").
|
||||
Where("t2.id = 1 OR t2.id = 5").
|
||||
Order("t1.id asc").
|
||||
All()
|
||||
t.AssertNil(err)
|
||||
|
||||
t.Assert(len(r), 2)
|
||||
t.Assert(r[0]["id"], "1")
|
||||
t.Assert(r[1]["id"], "5")
|
||||
})
|
||||
}
|
||||
@ -1,467 +0,0 @@
|
||||
// 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 mariadb_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
const (
|
||||
TestDbNameSh0 = "test_0"
|
||||
TestDbNameSh1 = "test_1"
|
||||
TestTableName = "user"
|
||||
)
|
||||
|
||||
type ShardingUser struct {
|
||||
Id int
|
||||
Name string
|
||||
}
|
||||
|
||||
// createShardingDatabase creates test databases and tables for sharding
|
||||
func createShardingDatabase(t *gtest.T) {
|
||||
// Create databases
|
||||
dbs := []string{TestDbNameSh0, TestDbNameSh1}
|
||||
for _, dbName := range dbs {
|
||||
sql := fmt.Sprintf("CREATE DATABASE IF NOT EXISTS `%s`", dbName)
|
||||
_, err := db.Exec(ctx, sql)
|
||||
t.AssertNil(err)
|
||||
|
||||
// Switch to the database
|
||||
sql = fmt.Sprintf("USE `%s`", dbName)
|
||||
_, err = db.Exec(ctx, sql)
|
||||
t.AssertNil(err)
|
||||
|
||||
// Create tables
|
||||
tables := []string{"user_0", "user_1", "user_2", "user_3"}
|
||||
for _, table := range tables {
|
||||
sql := fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
id int(11) NOT NULL,
|
||||
name varchar(255) NOT NULL,
|
||||
PRIMARY KEY (id)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
`, table)
|
||||
_, err := db.Exec(ctx, sql)
|
||||
t.AssertNil(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// dropShardingDatabase drops test databases
|
||||
func dropShardingDatabase(t *gtest.T) {
|
||||
dbs := []string{TestDbNameSh0, TestDbNameSh1}
|
||||
for _, dbName := range dbs {
|
||||
sql := fmt.Sprintf("DROP DATABASE IF EXISTS `%s`", dbName)
|
||||
_, err := db.Exec(ctx, sql)
|
||||
t.AssertNil(err)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_Sharding_Basic(t *testing.T) {
|
||||
return
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var (
|
||||
tablePrefix = "user_"
|
||||
schemaPrefix = "test_"
|
||||
)
|
||||
|
||||
// Create test databases and tables
|
||||
createShardingDatabase(t)
|
||||
defer dropShardingDatabase(t)
|
||||
|
||||
// Create sharding configuration
|
||||
shardingConfig := gdb.ShardingConfig{
|
||||
Table: gdb.ShardingTableConfig{
|
||||
Enable: true,
|
||||
Prefix: tablePrefix,
|
||||
Rule: &gdb.DefaultShardingRule{
|
||||
TableCount: 4,
|
||||
},
|
||||
},
|
||||
Schema: gdb.ShardingSchemaConfig{
|
||||
Enable: true,
|
||||
Prefix: schemaPrefix,
|
||||
Rule: &gdb.DefaultShardingRule{
|
||||
SchemaCount: 2,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// Prepare test data
|
||||
user := ShardingUser{
|
||||
Id: 1,
|
||||
Name: "John",
|
||||
}
|
||||
|
||||
model := db.Model(TestTableName).
|
||||
Sharding(shardingConfig).
|
||||
ShardingValue(user.Id).
|
||||
Safe()
|
||||
|
||||
// Test Insert
|
||||
_, err := model.Data(user).Insert()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Test Select
|
||||
var result ShardingUser
|
||||
err = model.Where("id", user.Id).Scan(&result)
|
||||
t.AssertNil(err)
|
||||
t.Assert(result.Id, user.Id)
|
||||
t.Assert(result.Name, user.Name)
|
||||
|
||||
// Test Update
|
||||
_, err = model.Data(g.Map{"name": "John Doe"}).
|
||||
Where("id", user.Id).
|
||||
Update()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Verify Update
|
||||
err = model.Where("id", user.Id).Scan(&result)
|
||||
t.AssertNil(err)
|
||||
t.Assert(result.Name, "John Doe")
|
||||
|
||||
// Test Delete
|
||||
_, err = model.Where("id", user.Id).Delete()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Verify Delete
|
||||
count, err := model.Where("id", user.Id).Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(count, 0)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Sharding_Error tests error cases
|
||||
func Test_Sharding_Error(t *testing.T) {
|
||||
return
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Create test databases and tables
|
||||
createShardingDatabase(t)
|
||||
defer dropShardingDatabase(t)
|
||||
|
||||
// Test missing sharding value
|
||||
model := db.Model(TestTableName).
|
||||
Sharding(gdb.ShardingConfig{
|
||||
Table: gdb.ShardingTableConfig{
|
||||
Enable: true,
|
||||
Prefix: "user_",
|
||||
Rule: &gdb.DefaultShardingRule{TableCount: 4},
|
||||
},
|
||||
}).Safe()
|
||||
|
||||
_, err := model.Insert(g.Map{"id": 1, "name": "test"})
|
||||
t.AssertNE(err, nil)
|
||||
t.Assert(err.Error(), "sharding value is required when sharding feature enabled")
|
||||
|
||||
// Test missing sharding rule
|
||||
model = db.Model(TestTableName).
|
||||
Sharding(gdb.ShardingConfig{
|
||||
Table: gdb.ShardingTableConfig{
|
||||
Enable: true,
|
||||
Prefix: "user_",
|
||||
},
|
||||
}).
|
||||
ShardingValue(1)
|
||||
|
||||
_, err = model.Insert(g.Map{"id": 1, "name": "test"})
|
||||
t.AssertNE(err, nil)
|
||||
t.Assert(err.Error(), "sharding rule is required when sharding feature enabled")
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Sharding_Complex tests complex sharding scenarios
|
||||
func Test_Sharding_Complex(t *testing.T) {
|
||||
return
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Create test databases and tables
|
||||
createShardingDatabase(t)
|
||||
defer dropShardingDatabase(t)
|
||||
|
||||
shardingConfig := gdb.ShardingConfig{
|
||||
Table: gdb.ShardingTableConfig{
|
||||
Enable: true,
|
||||
Prefix: "user_",
|
||||
Rule: &gdb.DefaultShardingRule{TableCount: 4},
|
||||
},
|
||||
Schema: gdb.ShardingSchemaConfig{
|
||||
Enable: true,
|
||||
Prefix: "test_",
|
||||
Rule: &gdb.DefaultShardingRule{SchemaCount: 2},
|
||||
},
|
||||
}
|
||||
|
||||
users := []ShardingUser{
|
||||
{Id: 1, Name: "User1"},
|
||||
{Id: 2, Name: "User2"},
|
||||
{Id: 3, Name: "User3"},
|
||||
}
|
||||
|
||||
for _, user := range users {
|
||||
model := db.Model(TestTableName).
|
||||
Sharding(shardingConfig).
|
||||
ShardingValue(user.Id).
|
||||
Safe()
|
||||
|
||||
_, err := model.Data(user).Insert()
|
||||
t.AssertNil(err)
|
||||
}
|
||||
|
||||
// Test batch query
|
||||
for _, user := range users {
|
||||
model := db.Model(TestTableName).
|
||||
Sharding(shardingConfig).
|
||||
ShardingValue(user.Id).
|
||||
Safe()
|
||||
|
||||
var result ShardingUser
|
||||
err := model.Where("id", user.Id).Scan(&result)
|
||||
t.AssertNil(err)
|
||||
t.Assert(result.Id, user.Id)
|
||||
t.Assert(result.Name, user.Name)
|
||||
}
|
||||
|
||||
// Clean up
|
||||
for _, user := range users {
|
||||
model := db.Model(TestTableName).
|
||||
Sharding(shardingConfig).
|
||||
ShardingValue(user.Id).
|
||||
Safe()
|
||||
|
||||
_, err := model.Where("id", user.Id).Delete()
|
||||
t.AssertNil(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Sharding_Table_Using_Hook(t *testing.T) {
|
||||
var (
|
||||
table1 = gtime.TimestampNanoStr() + "_table1"
|
||||
table2 = gtime.TimestampNanoStr() + "_table2"
|
||||
)
|
||||
createTable(table1)
|
||||
defer dropTable(table1)
|
||||
createTable(table2)
|
||||
defer dropTable(table2)
|
||||
|
||||
shardingModel := db.Model(table1).Hook(gdb.HookHandler{
|
||||
Select: func(ctx context.Context, in *gdb.HookSelectInput) (result gdb.Result, err error) {
|
||||
in.Table = table2
|
||||
return in.Next(ctx)
|
||||
},
|
||||
Insert: func(ctx context.Context, in *gdb.HookInsertInput) (result sql.Result, err error) {
|
||||
in.Table = table2
|
||||
return in.Next(ctx)
|
||||
},
|
||||
Update: func(ctx context.Context, in *gdb.HookUpdateInput) (result sql.Result, err error) {
|
||||
in.Table = table2
|
||||
return in.Next(ctx)
|
||||
},
|
||||
Delete: func(ctx context.Context, in *gdb.HookDeleteInput) (result sql.Result, err error) {
|
||||
in.Table = table2
|
||||
return in.Next(ctx)
|
||||
},
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
r, err := shardingModel.Insert(g.Map{
|
||||
"id": 1,
|
||||
"passport": fmt.Sprintf(`user_%d`, 1),
|
||||
"password": fmt.Sprintf(`pass_%d`, 1),
|
||||
"nickname": fmt.Sprintf(`name_%d`, 1),
|
||||
"create_time": gtime.NewFromStr(CreateTime).String(),
|
||||
})
|
||||
t.AssertNil(err)
|
||||
n, err := r.RowsAffected()
|
||||
t.AssertNil(err)
|
||||
t.Assert(n, 1)
|
||||
|
||||
var count int
|
||||
count, err = shardingModel.Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(count, 1)
|
||||
|
||||
count, err = db.Model(table1).Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(count, 0)
|
||||
|
||||
count, err = db.Model(table2).Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(count, 1)
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
r, err := shardingModel.Where(g.Map{
|
||||
"id": 1,
|
||||
}).Data(g.Map{
|
||||
"passport": fmt.Sprintf(`user_%d`, 2),
|
||||
"password": fmt.Sprintf(`pass_%d`, 2),
|
||||
"nickname": fmt.Sprintf(`name_%d`, 2),
|
||||
}).Update()
|
||||
t.AssertNil(err)
|
||||
n, err := r.RowsAffected()
|
||||
t.AssertNil(err)
|
||||
t.Assert(n, 1)
|
||||
|
||||
var (
|
||||
count int
|
||||
where = g.Map{"passport": fmt.Sprintf(`user_%d`, 2)}
|
||||
)
|
||||
count, err = shardingModel.Where(where).Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(count, 1)
|
||||
|
||||
count, err = db.Model(table1).Where(where).Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(count, 0)
|
||||
|
||||
count, err = db.Model(table2).Where(where).Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(count, 1)
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
r, err := shardingModel.Where(g.Map{
|
||||
"id": 1,
|
||||
}).Delete()
|
||||
t.AssertNil(err)
|
||||
n, err := r.RowsAffected()
|
||||
t.AssertNil(err)
|
||||
t.Assert(n, 1)
|
||||
|
||||
var count int
|
||||
count, err = shardingModel.Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(count, 0)
|
||||
|
||||
count, err = db.Model(table1).Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(count, 0)
|
||||
|
||||
count, err = db.Model(table2).Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(count, 0)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Sharding_Schema_Using_Hook(t *testing.T) {
|
||||
var (
|
||||
table = gtime.TimestampNanoStr() + "_table"
|
||||
)
|
||||
createTableWithDb(db, table)
|
||||
defer dropTableWithDb(db, table)
|
||||
createTableWithDb(db2, table)
|
||||
defer dropTableWithDb(db2, table)
|
||||
|
||||
shardingModel := db.Model(table).Hook(gdb.HookHandler{
|
||||
Select: func(ctx context.Context, in *gdb.HookSelectInput) (result gdb.Result, err error) {
|
||||
in.Table = table
|
||||
in.Schema = db2.GetSchema()
|
||||
return in.Next(ctx)
|
||||
},
|
||||
Insert: func(ctx context.Context, in *gdb.HookInsertInput) (result sql.Result, err error) {
|
||||
in.Table = table
|
||||
in.Schema = db2.GetSchema()
|
||||
return in.Next(ctx)
|
||||
},
|
||||
Update: func(ctx context.Context, in *gdb.HookUpdateInput) (result sql.Result, err error) {
|
||||
in.Table = table
|
||||
in.Schema = db2.GetSchema()
|
||||
return in.Next(ctx)
|
||||
},
|
||||
Delete: func(ctx context.Context, in *gdb.HookDeleteInput) (result sql.Result, err error) {
|
||||
in.Table = table
|
||||
in.Schema = db2.GetSchema()
|
||||
return in.Next(ctx)
|
||||
},
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
r, err := shardingModel.Insert(g.Map{
|
||||
"id": 1,
|
||||
"passport": fmt.Sprintf(`user_%d`, 1),
|
||||
"password": fmt.Sprintf(`pass_%d`, 1),
|
||||
"nickname": fmt.Sprintf(`name_%d`, 1),
|
||||
"create_time": gtime.NewFromStr(CreateTime).String(),
|
||||
})
|
||||
t.AssertNil(err)
|
||||
n, err := r.RowsAffected()
|
||||
t.AssertNil(err)
|
||||
t.Assert(n, 1)
|
||||
|
||||
var count int
|
||||
count, err = shardingModel.Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(count, 1)
|
||||
|
||||
count, err = db.Model(table).Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(count, 0)
|
||||
|
||||
count, err = db2.Model(table).Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(count, 1)
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
r, err := shardingModel.Where(g.Map{
|
||||
"id": 1,
|
||||
}).Data(g.Map{
|
||||
"passport": fmt.Sprintf(`user_%d`, 2),
|
||||
"password": fmt.Sprintf(`pass_%d`, 2),
|
||||
"nickname": fmt.Sprintf(`name_%d`, 2),
|
||||
}).Update()
|
||||
t.AssertNil(err)
|
||||
n, err := r.RowsAffected()
|
||||
t.AssertNil(err)
|
||||
t.Assert(n, 1)
|
||||
|
||||
var (
|
||||
count int
|
||||
where = g.Map{"passport": fmt.Sprintf(`user_%d`, 2)}
|
||||
)
|
||||
count, err = shardingModel.Where(where).Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(count, 1)
|
||||
|
||||
count, err = db.Model(table).Where(where).Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(count, 0)
|
||||
|
||||
count, err = db2.Model(table).Where(where).Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(count, 1)
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
r, err := shardingModel.Where(g.Map{
|
||||
"id": 1,
|
||||
}).Delete()
|
||||
t.AssertNil(err)
|
||||
n, err := r.RowsAffected()
|
||||
t.AssertNil(err)
|
||||
t.Assert(n, 1)
|
||||
|
||||
var count int
|
||||
count, err = shardingModel.Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(count, 0)
|
||||
|
||||
count, err = db.Model(table).Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(count, 0)
|
||||
|
||||
count, err = db2.Model(table).Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(count, 0)
|
||||
})
|
||||
}
|
||||
@ -1,482 +0,0 @@
|
||||
// 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 mariadb_test
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/errors/gcode"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
func Test_Model_Embedded_Insert(t *testing.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type Base struct {
|
||||
Id int `json:"id"`
|
||||
Uid int `json:"uid"`
|
||||
CreateTime string `json:"create_time"`
|
||||
}
|
||||
type User struct {
|
||||
Base
|
||||
Passport string `json:"passport"`
|
||||
Password string `json:"password"`
|
||||
Nickname string `json:"nickname"`
|
||||
}
|
||||
result, err := db.Model(table).Data(User{
|
||||
Passport: "john-test",
|
||||
Password: "123456",
|
||||
Nickname: "John",
|
||||
Base: Base{
|
||||
Id: 100,
|
||||
Uid: 100,
|
||||
CreateTime: gtime.Now().String(),
|
||||
},
|
||||
}).Insert()
|
||||
t.AssertNil(err)
|
||||
n, _ := result.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
value, err := db.Model(table).Fields("passport").Where("id=100").Value()
|
||||
t.AssertNil(err)
|
||||
t.Assert(value.String(), "john-test")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Embedded_MapToStruct(t *testing.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type Ids struct {
|
||||
Id int `json:"id"`
|
||||
Uid int `json:"uid"`
|
||||
}
|
||||
type Base struct {
|
||||
Ids
|
||||
CreateTime string `json:"create_time"`
|
||||
}
|
||||
type User struct {
|
||||
Base
|
||||
Passport string `json:"passport"`
|
||||
Password string `json:"password"`
|
||||
Nickname string `json:"nickname"`
|
||||
}
|
||||
data := g.Map{
|
||||
"id": 100,
|
||||
"uid": 101,
|
||||
"passport": "t1",
|
||||
"password": "123456",
|
||||
"nickname": "T1",
|
||||
"create_time": gtime.Now().String(),
|
||||
}
|
||||
result, err := db.Model(table).Data(data).Insert()
|
||||
t.AssertNil(err)
|
||||
n, _ := result.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
|
||||
one, err := db.Model(table).Where("id=100").One()
|
||||
t.AssertNil(err)
|
||||
|
||||
user := new(User)
|
||||
|
||||
t.Assert(one.Struct(user), nil)
|
||||
t.Assert(user.Id, data["id"])
|
||||
t.Assert(user.Passport, data["passport"])
|
||||
t.Assert(user.Password, data["password"])
|
||||
t.Assert(user.Nickname, data["nickname"])
|
||||
t.Assert(user.CreateTime, data["create_time"])
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Struct_Pointer_Attribute(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
type User struct {
|
||||
Id *int
|
||||
Passport *string
|
||||
Password *string
|
||||
Nickname string
|
||||
}
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
one, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
user := new(User)
|
||||
err = one.Struct(user)
|
||||
t.AssertNil(err)
|
||||
t.Assert(*user.Id, 1)
|
||||
t.Assert(*user.Passport, "user_1")
|
||||
t.Assert(*user.Password, "pass_1")
|
||||
t.Assert(user.Nickname, "name_1")
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
user := new(User)
|
||||
err := db.Model(table).Scan(user, "id=1")
|
||||
t.AssertNil(err)
|
||||
t.Assert(*user.Id, 1)
|
||||
t.Assert(*user.Passport, "user_1")
|
||||
t.Assert(*user.Password, "pass_1")
|
||||
t.Assert(user.Nickname, "name_1")
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var user *User
|
||||
err := db.Model(table).Scan(&user, "id=1")
|
||||
t.AssertNil(err)
|
||||
t.Assert(*user.Id, 1)
|
||||
t.Assert(*user.Passport, "user_1")
|
||||
t.Assert(*user.Password, "pass_1")
|
||||
t.Assert(user.Nickname, "name_1")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Structs_Pointer_Attribute(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
type User struct {
|
||||
Id *int
|
||||
Passport *string
|
||||
Password *string
|
||||
Nickname string
|
||||
}
|
||||
// All
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
one, err := db.Model(table).All("id < 3")
|
||||
t.AssertNil(err)
|
||||
users := make([]User, 0)
|
||||
err = one.Structs(&users)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(users), 2)
|
||||
t.Assert(*users[0].Id, 1)
|
||||
t.Assert(*users[0].Passport, "user_1")
|
||||
t.Assert(*users[0].Password, "pass_1")
|
||||
t.Assert(users[0].Nickname, "name_1")
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
one, err := db.Model(table).All("id < 3")
|
||||
t.AssertNil(err)
|
||||
users := make([]*User, 0)
|
||||
err = one.Structs(&users)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(users), 2)
|
||||
t.Assert(*users[0].Id, 1)
|
||||
t.Assert(*users[0].Passport, "user_1")
|
||||
t.Assert(*users[0].Password, "pass_1")
|
||||
t.Assert(users[0].Nickname, "name_1")
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var users []User
|
||||
one, err := db.Model(table).All("id < 3")
|
||||
t.AssertNil(err)
|
||||
err = one.Structs(&users)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(users), 2)
|
||||
t.Assert(*users[0].Id, 1)
|
||||
t.Assert(*users[0].Passport, "user_1")
|
||||
t.Assert(*users[0].Password, "pass_1")
|
||||
t.Assert(users[0].Nickname, "name_1")
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var users []*User
|
||||
one, err := db.Model(table).All("id < 3")
|
||||
t.AssertNil(err)
|
||||
err = one.Structs(&users)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(users), 2)
|
||||
t.Assert(*users[0].Id, 1)
|
||||
t.Assert(*users[0].Passport, "user_1")
|
||||
t.Assert(*users[0].Password, "pass_1")
|
||||
t.Assert(users[0].Nickname, "name_1")
|
||||
})
|
||||
// Structs
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
users := make([]User, 0)
|
||||
err := db.Model(table).Scan(&users, "id < 3")
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(users), 2)
|
||||
t.Assert(*users[0].Id, 1)
|
||||
t.Assert(*users[0].Passport, "user_1")
|
||||
t.Assert(*users[0].Password, "pass_1")
|
||||
t.Assert(users[0].Nickname, "name_1")
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
users := make([]*User, 0)
|
||||
err := db.Model(table).Scan(&users, "id < 3")
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(users), 2)
|
||||
t.Assert(*users[0].Id, 1)
|
||||
t.Assert(*users[0].Passport, "user_1")
|
||||
t.Assert(*users[0].Password, "pass_1")
|
||||
t.Assert(users[0].Nickname, "name_1")
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var users []User
|
||||
err := db.Model(table).Scan(&users, "id < 3")
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(users), 2)
|
||||
t.Assert(*users[0].Id, 1)
|
||||
t.Assert(*users[0].Passport, "user_1")
|
||||
t.Assert(*users[0].Password, "pass_1")
|
||||
t.Assert(users[0].Nickname, "name_1")
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var users []*User
|
||||
err := db.Model(table).Scan(&users, "id < 3")
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(users), 2)
|
||||
t.Assert(*users[0].Id, 1)
|
||||
t.Assert(*users[0].Passport, "user_1")
|
||||
t.Assert(*users[0].Password, "pass_1")
|
||||
t.Assert(users[0].Nickname, "name_1")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Struct_Empty(t *testing.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
|
||||
type User struct {
|
||||
Id int
|
||||
Passport string
|
||||
Password string
|
||||
Nickname string
|
||||
}
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
user := new(User)
|
||||
err := db.Model(table).Where("id=100").Scan(user)
|
||||
t.Assert(err, sql.ErrNoRows)
|
||||
t.AssertNE(user, nil)
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
one, err := db.Model(table).Where("id=100").One()
|
||||
t.AssertNil(err)
|
||||
var user *User
|
||||
t.Assert(one.Struct(&user), nil)
|
||||
t.Assert(user, nil)
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var user *User
|
||||
err := db.Model(table).Where("id=100").Scan(&user)
|
||||
t.AssertNil(err)
|
||||
t.Assert(user, nil)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Structs_Empty(t *testing.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
|
||||
type User struct {
|
||||
Id int
|
||||
Passport string
|
||||
Password string
|
||||
Nickname string
|
||||
}
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
all, err := db.Model(table).Where("id>100").All()
|
||||
t.AssertNil(err)
|
||||
users := make([]User, 0)
|
||||
t.Assert(all.Structs(&users), nil)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
all, err := db.Model(table).Where("id>100").All()
|
||||
t.AssertNil(err)
|
||||
users := make([]User, 10)
|
||||
t.Assert(all.Structs(&users), sql.ErrNoRows)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
all, err := db.Model(table).Where("id>100").All()
|
||||
t.AssertNil(err)
|
||||
var users []User
|
||||
t.Assert(all.Structs(&users), nil)
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
all, err := db.Model(table).Where("id>100").All()
|
||||
t.AssertNil(err)
|
||||
users := make([]*User, 0)
|
||||
t.Assert(all.Structs(&users), nil)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
all, err := db.Model(table).Where("id>100").All()
|
||||
t.AssertNil(err)
|
||||
users := make([]*User, 10)
|
||||
t.Assert(all.Structs(&users), sql.ErrNoRows)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
all, err := db.Model(table).Where("id>100").All()
|
||||
t.AssertNil(err)
|
||||
var users []*User
|
||||
t.Assert(all.Structs(&users), nil)
|
||||
})
|
||||
}
|
||||
|
||||
type MyTime struct {
|
||||
gtime.Time
|
||||
}
|
||||
|
||||
type MyTimeSt struct {
|
||||
CreateTime MyTime
|
||||
}
|
||||
|
||||
func (st *MyTimeSt) UnmarshalValue(v any) error {
|
||||
m := gconv.Map(v)
|
||||
t, err := gtime.StrToTime(gconv.String(m["create_time"]))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
st.CreateTime = MyTime{*t}
|
||||
return nil
|
||||
}
|
||||
|
||||
func Test_Model_Scan_CustomType_Time(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
st := new(MyTimeSt)
|
||||
err := db.Model(table).Fields("create_time").Scan(st)
|
||||
t.AssertNil(err)
|
||||
t.Assert(st.CreateTime.String(), "2018-10-24 10:00:00")
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var stSlice []*MyTimeSt
|
||||
err := db.Model(table).Fields("create_time").Scan(&stSlice)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(stSlice), TableSize)
|
||||
t.Assert(stSlice[0].CreateTime.String(), "2018-10-24 10:00:00")
|
||||
t.Assert(stSlice[9].CreateTime.String(), "2018-10-24 10:00:00")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Scan_CustomType_String(t *testing.T) {
|
||||
type MyString string
|
||||
|
||||
type MyStringSt struct {
|
||||
Passport MyString
|
||||
}
|
||||
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
st := new(MyStringSt)
|
||||
err := db.Model(table).Fields("Passport").WherePri(1).Scan(st)
|
||||
t.AssertNil(err)
|
||||
t.Assert(st.Passport, "user_1")
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var sts []MyStringSt
|
||||
err := db.Model(table).Fields("Passport").Order("id asc").Scan(&sts)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(sts), TableSize)
|
||||
t.Assert(sts[0].Passport, "user_1")
|
||||
})
|
||||
}
|
||||
|
||||
type User struct {
|
||||
Id int
|
||||
Passport string
|
||||
Password string
|
||||
Nickname string
|
||||
CreateTime *gtime.Time
|
||||
}
|
||||
|
||||
func (user *User) UnmarshalValue(value any) error {
|
||||
if record, ok := value.(gdb.Record); ok {
|
||||
*user = User{
|
||||
Id: record["id"].Int(),
|
||||
Passport: record["passport"].String(),
|
||||
Password: "",
|
||||
Nickname: record["nickname"].String(),
|
||||
CreateTime: record["create_time"].GTime(),
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return gerror.NewCodef(gcode.CodeInvalidParameter, `unsupported value type for UnmarshalValue: %v`, reflect.TypeOf(value))
|
||||
}
|
||||
|
||||
func Test_Model_Scan_UnmarshalValue(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var users []*User
|
||||
err := db.Model(table).Order("id asc").Scan(&users)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(users), TableSize)
|
||||
t.Assert(users[0].Id, 1)
|
||||
t.Assert(users[0].Passport, "user_1")
|
||||
t.Assert(users[0].Password, "")
|
||||
t.Assert(users[0].Nickname, "name_1")
|
||||
t.Assert(users[0].CreateTime.String(), CreateTime)
|
||||
|
||||
t.Assert(users[9].Id, 10)
|
||||
t.Assert(users[9].Passport, "user_10")
|
||||
t.Assert(users[9].Password, "")
|
||||
t.Assert(users[9].Nickname, "name_10")
|
||||
t.Assert(users[9].CreateTime.String(), CreateTime)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Scan_Map(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var users []*User
|
||||
err := db.Model(table).Order("id asc").Scan(&users)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(users), TableSize)
|
||||
t.Assert(users[0].Id, 1)
|
||||
t.Assert(users[0].Passport, "user_1")
|
||||
t.Assert(users[0].Password, "")
|
||||
t.Assert(users[0].Nickname, "name_1")
|
||||
t.Assert(users[0].CreateTime.String(), CreateTime)
|
||||
|
||||
t.Assert(users[9].Id, 10)
|
||||
t.Assert(users[9].Passport, "user_10")
|
||||
t.Assert(users[9].Password, "")
|
||||
t.Assert(users[9].Nickname, "name_10")
|
||||
t.Assert(users[9].CreateTime.String(), CreateTime)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Scan_AutoFilteringByStructAttributes(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
type User struct {
|
||||
Id int
|
||||
Passport string
|
||||
}
|
||||
// db.SetDebug(true)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var user *User
|
||||
err := db.Model(table).OrderAsc("id").Scan(&user)
|
||||
t.AssertNil(err)
|
||||
t.Assert(user.Id, 1)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var users []User
|
||||
err := db.Model(table).OrderAsc("id").Scan(&users)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(users), TableSize)
|
||||
t.Assert(users[0].Id, 1)
|
||||
})
|
||||
}
|
||||
@ -1,311 +0,0 @@
|
||||
// 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 mariadb_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
)
|
||||
|
||||
func Test_Model_SubQuery_Where(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
r, err := db.Model(table).Where(
|
||||
"id in ?",
|
||||
db.Model(table).Fields("id").Where("id", g.Slice{1, 3, 5}),
|
||||
).OrderAsc("id").All()
|
||||
t.AssertNil(err)
|
||||
|
||||
t.Assert(len(r), 3)
|
||||
t.Assert(r[0]["id"], 1)
|
||||
t.Assert(r[1]["id"], 3)
|
||||
t.Assert(r[2]["id"], 5)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_SubQuery_Having(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
r, err := db.Model(table).Where(
|
||||
"id in ?",
|
||||
db.Model(table).Fields("id").Where("id", g.Slice{1, 3, 5}),
|
||||
).Having(
|
||||
"id > ?",
|
||||
db.Model(table).Fields("MAX(id)").Where("id", g.Slice{1, 3}),
|
||||
).OrderAsc("id").All()
|
||||
t.AssertNil(err)
|
||||
|
||||
t.Assert(len(r), 1)
|
||||
t.Assert(r[0]["id"], 5)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_SubQuery_Model(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
subQuery1 := db.Model(table).Where("id", g.Slice{1, 3, 5})
|
||||
subQuery2 := db.Model(table).Where("id", g.Slice{5, 7, 9})
|
||||
r, err := db.Model("? AS a, ? AS b", subQuery1, subQuery2).Fields("a.id").Where("a.id=b.id").OrderAsc("id").All()
|
||||
t.AssertNil(err)
|
||||
|
||||
t.Assert(len(r), 1)
|
||||
t.Assert(r[0]["id"], 5)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_SubQuery_Correlated tests scalar subquery and correlated subquery with EXISTS
|
||||
func Test_Model_SubQuery_Correlated(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Scalar subquery: find users whose id is greater than average id
|
||||
subQuery := db.Model(table + " AS inner_table").Fields("AVG(id)")
|
||||
r, err := db.Model(table).Where(
|
||||
"id > (?)",
|
||||
subQuery,
|
||||
).OrderAsc("id").All()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Average of 1-10 is 5.5, so expect ids 6-10
|
||||
t.Assert(len(r), 5)
|
||||
t.Assert(r[0]["id"], 6)
|
||||
t.Assert(r[4]["id"], 10)
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Correlated subquery with EXISTS: find users with id matching their own id
|
||||
r, err := db.Model(table+" AS outer_table").
|
||||
Where(
|
||||
fmt.Sprintf("EXISTS (SELECT 1 FROM %s AS inner_table WHERE inner_table.id = outer_table.id AND inner_table.id <= ?)", table),
|
||||
3,
|
||||
).
|
||||
OrderAsc("id").
|
||||
All()
|
||||
t.AssertNil(err)
|
||||
|
||||
t.Assert(len(r), 3)
|
||||
t.Assert(r[0]["id"], 1)
|
||||
t.Assert(r[2]["id"], 3)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_SubQuery_From tests subquery in FROM clause
|
||||
func Test_Model_SubQuery_From(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Subquery in FROM clause
|
||||
subQuery := db.Model(table).Where("id <=", 5)
|
||||
r, err := db.Model("(?) AS sub", subQuery).
|
||||
Fields("sub.id", "sub.nickname").
|
||||
Where("sub.id >", 2).
|
||||
OrderAsc("id").
|
||||
All()
|
||||
t.AssertNil(err)
|
||||
|
||||
t.Assert(len(r), 3)
|
||||
t.Assert(r[0]["id"], 3)
|
||||
t.Assert(r[0]["nickname"], "name_3")
|
||||
t.Assert(r[2]["id"], 5)
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Multiple subqueries in FROM clause with JOIN
|
||||
subQuery1 := db.Model(table).Fields("id", "nickname").Where("id <=", 3)
|
||||
subQuery2 := db.Model(table).Fields("id", "passport").Where("id >=", 3)
|
||||
|
||||
r, err := db.Model("? AS a, ? AS b", subQuery1, subQuery2).
|
||||
Fields("a.id", "a.nickname", "b.passport").
|
||||
Where("a.id = b.id").
|
||||
OrderAsc("id").
|
||||
All()
|
||||
t.AssertNil(err)
|
||||
|
||||
t.Assert(len(r), 1)
|
||||
t.Assert(r[0]["id"], 3)
|
||||
t.Assert(r[0]["nickname"], "name_3")
|
||||
t.Assert(r[0]["passport"], "user_3")
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_SubQuery_Select tests subquery in SELECT clause
|
||||
func Test_Model_SubQuery_Select(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Subquery in SELECT clause for scalar value
|
||||
r, err := db.Model(table).
|
||||
Fields("id", "nickname", fmt.Sprintf("(SELECT MAX(id) FROM %s) AS max_id", table)).
|
||||
Where("id", 1).
|
||||
One()
|
||||
t.AssertNil(err)
|
||||
|
||||
t.Assert(r["id"], 1)
|
||||
t.Assert(r["nickname"], "name_1")
|
||||
t.Assert(r["max_id"], 10)
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Multiple subqueries in SELECT clause
|
||||
r, err := db.Model(table).
|
||||
Fields(
|
||||
"id",
|
||||
fmt.Sprintf("(SELECT MAX(id) FROM %s) AS max_id", table),
|
||||
fmt.Sprintf("(SELECT MIN(id) FROM %s) AS min_id", table),
|
||||
).
|
||||
Where("id", 5).
|
||||
One()
|
||||
t.AssertNil(err)
|
||||
|
||||
t.Assert(r["id"], 5)
|
||||
t.Assert(r["max_id"], 10)
|
||||
t.Assert(r["min_id"], 1)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_SubQuery_Nested tests multi-level nested subqueries (3+ levels)
|
||||
func Test_Model_SubQuery_Nested(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// 3-level nested subquery
|
||||
// Level 3: innermost - get ids <= 8
|
||||
level3 := db.Model(table).Fields("id").Where("id <=", 8)
|
||||
|
||||
// Level 2: middle - filter from level 3 where id >= 3
|
||||
level2 := db.Model("(?) AS l3", level3).Fields("l3.id").Where("l3.id >=", 3)
|
||||
|
||||
// Level 1: outermost - filter from level 2 where id <= 6
|
||||
r, err := db.Model(table).
|
||||
Where("id IN (?)", level2).
|
||||
Where("id <=", 6).
|
||||
OrderAsc("id").
|
||||
All()
|
||||
t.AssertNil(err)
|
||||
|
||||
t.Assert(len(r), 4)
|
||||
t.Assert(r[0]["id"], 3)
|
||||
t.Assert(r[3]["id"], 6)
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// 4-level nested subquery with aggregates
|
||||
// Level 4: get all ids
|
||||
level4 := db.Model(table).Fields("id")
|
||||
|
||||
// Level 3: get ids > 5 from level 4
|
||||
level3 := db.Model("(?) AS l4", level4).Fields("l4.id").Where("l4.id >", 5)
|
||||
|
||||
// Level 2: get MIN(id) from level 3
|
||||
level2 := db.Model("(?) AS l3", level3).Fields("MIN(l3.id)")
|
||||
|
||||
// Level 1: find records >= the minimum from level 2
|
||||
r, err := db.Model(table).
|
||||
Where("id >= (?)", level2).
|
||||
OrderAsc("id").
|
||||
All()
|
||||
t.AssertNil(err)
|
||||
|
||||
// MIN(id) from level 3 should be 6, so expect ids 6-10
|
||||
t.Assert(len(r), 5)
|
||||
t.Assert(r[0]["id"], 6)
|
||||
t.Assert(r[4]["id"], 10)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_SubQuery_WhereIn tests subquery with WHERE IN
|
||||
func Test_Model_SubQuery_WhereIn(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Simple WHERE IN with subquery
|
||||
subQuery := db.Model(table).Fields("id").Where("id IN(?)", g.Slice{2, 4, 6})
|
||||
r, err := db.Model(table).
|
||||
Where("id IN(?)", subQuery).
|
||||
OrderAsc("id").
|
||||
All()
|
||||
t.AssertNil(err)
|
||||
|
||||
t.Assert(len(r), 3)
|
||||
t.Assert(r[0]["id"], 2)
|
||||
t.Assert(r[1]["id"], 4)
|
||||
t.Assert(r[2]["id"], 6)
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Multiple WHERE IN subqueries combined
|
||||
subQuery1 := db.Model(table).Fields("id").Where("id <=", 5)
|
||||
subQuery2 := db.Model(table).Fields("id").Where("id >=", 3)
|
||||
|
||||
r, err := db.Model(table).
|
||||
Where("id IN(?)", subQuery1).
|
||||
Where("id IN(?)", subQuery2).
|
||||
OrderAsc("id").
|
||||
All()
|
||||
t.AssertNil(err)
|
||||
|
||||
t.Assert(len(r), 3)
|
||||
t.Assert(r[0]["id"], 3)
|
||||
t.Assert(r[2]["id"], 5)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_SubQuery_Complex tests complex subquery combinations
|
||||
func Test_Model_SubQuery_Complex(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Combine subquery in WHERE, FROM, and SELECT
|
||||
whereSubQuery := db.Model(table).Fields("AVG(id)")
|
||||
fromSubQuery := db.Model(table).Where("id <=", 7)
|
||||
|
||||
r, err := db.Model("(?) AS sub", fromSubQuery).
|
||||
Fields("sub.id", "sub.nickname").
|
||||
Where("sub.id > (?)", whereSubQuery).
|
||||
OrderAsc("id").
|
||||
All()
|
||||
t.AssertNil(err)
|
||||
|
||||
// AVG(1-10) = 5.5, filter sub.id > 5.5 from ids 1-7
|
||||
t.Assert(len(r), 2)
|
||||
t.Assert(r[0]["id"], 6)
|
||||
t.Assert(r[1]["id"], 7)
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Subquery with GROUP BY and HAVING
|
||||
subQuery := db.Model(table).
|
||||
Fields("id % 3 AS mod_group", "COUNT(*) AS cnt").
|
||||
Group("mod_group").
|
||||
Having("COUNT(*) >=", 3)
|
||||
|
||||
r, err := db.Model(table).
|
||||
Where("id % 3 IN(?)", db.Model("(?) AS sub", subQuery).Fields("sub.mod_group")).
|
||||
OrderAsc("id").
|
||||
All()
|
||||
t.AssertNil(err)
|
||||
|
||||
// id % 3: 0(3,6,9), 1(1,4,7,10), 2(2,5,8)
|
||||
// Groups with count >= 3: 0(3 items), 1(4 items), 2(3 items) - all qualify
|
||||
t.Assert(len(r), 10)
|
||||
})
|
||||
}
|
||||
@ -1,398 +0,0 @@
|
||||
// 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 mariadb_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
)
|
||||
|
||||
// Test_Model_OmitEmpty_Comprehensive tests OmitEmpty filtering for both data and where parameters
|
||||
func Test_Model_OmitEmpty_Comprehensive(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Test OmitEmpty with empty string in Data
|
||||
result, err := db.Model(table).OmitEmpty().Data(g.Map{
|
||||
"nickname": "", // empty string should be omitted
|
||||
"passport": "new_user", // non-empty should be kept
|
||||
}).Where("id", 1).Update()
|
||||
t.AssertNil(err)
|
||||
n, _ := result.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
|
||||
// Verify nickname was not updated (omitted)
|
||||
one, err := db.Model(table).Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["nickname"], "name_1") // original value preserved
|
||||
t.Assert(one["passport"], "new_user")
|
||||
|
||||
// Test OmitEmpty with empty slice in Where
|
||||
all, err := db.Model(table).OmitEmpty().Where(g.Map{
|
||||
"id": []int{}, // empty slice should be omitted
|
||||
"passport": "new_user",
|
||||
}).All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(all), 1)
|
||||
|
||||
// Without OmitEmpty, empty slice causes WHERE 0=1
|
||||
all, err = db.Model(table).Where(g.Map{
|
||||
"id": []int{},
|
||||
}).All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(all), 0) // no results due to WHERE 0=1
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_OmitEmptyWhere_Extended tests OmitEmpty filtering only for where parameters
|
||||
func Test_Model_OmitEmptyWhere_Extended(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// OmitEmptyWhere only affects Where, not Data
|
||||
result, err := db.Model(table).OmitEmptyWhere().Data(g.Map{
|
||||
"nickname": "", // empty string in Data should NOT be omitted (only Where is affected)
|
||||
}).Where(g.Map{
|
||||
"id": 1,
|
||||
"passport": "", // empty string in Where should be omitted
|
||||
}).Update()
|
||||
t.AssertNil(err)
|
||||
n, _ := result.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
|
||||
// Verify nickname was updated to empty (Data is not affected by OmitEmptyWhere)
|
||||
one, err := db.Model(table).Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["nickname"], "")
|
||||
|
||||
// Test with empty slice in Where
|
||||
all, err := db.Model(table).OmitEmptyWhere().Where(g.Map{
|
||||
"id": []int{}, // should be omitted
|
||||
}).Order("id").Limit(3).All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(all), 3) // returns results because empty condition was omitted
|
||||
|
||||
// Test with zero value in Where (zero is considered empty)
|
||||
all, err = db.Model(table).OmitEmptyWhere().Where(g.Map{
|
||||
"id": 0, // zero should be omitted
|
||||
}).Order("id").Limit(3).All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(all), 3)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_OmitEmptyData tests OmitEmpty filtering only for data parameters
|
||||
func Test_Model_OmitEmptyData(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// OmitEmptyData only affects Data, not Where
|
||||
result, err := db.Model(table).OmitEmptyData().Data(g.Map{
|
||||
"nickname": "", // empty string in Data should be omitted
|
||||
"passport": "test_user", // non-empty should be kept
|
||||
}).Where(g.Map{
|
||||
"id": 1,
|
||||
}).Update()
|
||||
t.AssertNil(err)
|
||||
n, _ := result.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
|
||||
// Verify nickname was not updated (omitted), passport was updated
|
||||
one, err := db.Model(table).Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["nickname"], "name_1")
|
||||
t.Assert(one["passport"], "test_user")
|
||||
|
||||
// Test Insert with OmitEmptyData
|
||||
result, err = db.Model(table).OmitEmptyData().Data(g.Map{
|
||||
"id": 100,
|
||||
"passport": "user_100",
|
||||
"nickname": "", // should be omitted
|
||||
"password": "pass_100",
|
||||
}).Insert()
|
||||
t.AssertNil(err)
|
||||
n, _ = result.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
|
||||
// Verify nickname is NULL (was omitted from INSERT)
|
||||
one, err = db.Model(table).Where("id", 100).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], "user_100")
|
||||
t.Assert(one["nickname"].IsNil(), true)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_OmitNil_Comprehensive tests OmitNil filtering for both data and where parameters
|
||||
func Test_Model_OmitNil_Comprehensive(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Test OmitNil with nil value in Data
|
||||
result, err := db.Model(table).OmitNil().Data(g.Map{
|
||||
"nickname": nil, // nil should be omitted
|
||||
"passport": "nil_test", // non-nil should be kept
|
||||
}).Where("id", 1).Update()
|
||||
t.AssertNil(err)
|
||||
n, _ := result.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
|
||||
// Verify nickname was not updated (omitted)
|
||||
one, err := db.Model(table).Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["nickname"], "name_1")
|
||||
t.Assert(one["passport"], "nil_test")
|
||||
|
||||
// Test OmitNil with nil in Where
|
||||
all, err := db.Model(table).OmitNil().Where(g.Map{
|
||||
"passport": nil, // nil should be omitted
|
||||
}).Order("id").Limit(5).All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(all), 5) // returns results because nil condition was omitted
|
||||
|
||||
// Without OmitNil, WHERE passport=NULL (which won't match anything)
|
||||
all, err = db.Model(table).Where(g.Map{
|
||||
"passport": nil,
|
||||
}).All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(all), 0) // NULL comparison doesn't match
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_OmitNilWhere tests OmitNil filtering only for where parameters
|
||||
func Test_Model_OmitNilWhere(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// OmitNilWhere only affects Where, not Data
|
||||
result, err := db.Model(table).OmitNilWhere().Data(g.Map{
|
||||
"nickname": nil, // nil in Data should NOT be omitted (only Where is affected)
|
||||
}).Where(g.Map{
|
||||
"id": 1,
|
||||
"passport": nil, // nil in Where should be omitted
|
||||
}).Update()
|
||||
t.AssertNil(err)
|
||||
n, _ := result.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
|
||||
// Verify nickname was set to NULL (Data is not affected by OmitNilWhere)
|
||||
one, err := db.Model(table).Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["nickname"].IsNil(), true)
|
||||
|
||||
// Test with nil in Where
|
||||
all, err := db.Model(table).OmitNilWhere().Where(g.Map{
|
||||
"passport": nil, // should be omitted
|
||||
}).Order("id").Limit(3).All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(all), 3) // returns results
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_OmitNilData tests OmitNil filtering only for data parameters
|
||||
func Test_Model_OmitNilData(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// OmitNilData only affects Data, not Where
|
||||
result, err := db.Model(table).OmitNilData().Data(g.Map{
|
||||
"nickname": nil, // nil in Data should be omitted
|
||||
"passport": "omitnil_test", // non-nil should be kept
|
||||
}).Where(g.Map{
|
||||
"id": 1,
|
||||
}).Update()
|
||||
t.AssertNil(err)
|
||||
n, _ := result.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
|
||||
// Verify nickname was not updated (omitted), passport was updated
|
||||
one, err := db.Model(table).Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["nickname"], "name_1")
|
||||
t.Assert(one["passport"], "omitnil_test")
|
||||
|
||||
// Test Insert with OmitNilData
|
||||
result, err = db.Model(table).OmitNilData().Data(g.Map{
|
||||
"id": 101,
|
||||
"passport": "user_101",
|
||||
"nickname": nil, // should be omitted
|
||||
"password": "pass_101",
|
||||
}).Insert()
|
||||
t.AssertNil(err)
|
||||
n, _ = result.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
|
||||
// Verify insert
|
||||
one, err = db.Model(table).Where("id", 101).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], "user_101")
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_OmitEmpty_WithStruct tests OmitEmpty with struct data
|
||||
func Test_Model_OmitEmpty_WithStruct(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
type User struct {
|
||||
Id int
|
||||
Passport string
|
||||
Nickname string
|
||||
Password string
|
||||
}
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Test OmitEmptyData with struct
|
||||
user := User{
|
||||
Passport: "struct_user",
|
||||
Nickname: "", // empty, should be omitted
|
||||
Password: "struct_pass",
|
||||
}
|
||||
result, err := db.Model(table).OmitEmptyData().Data(user).Where("id", 1).Update()
|
||||
t.AssertNil(err)
|
||||
n, _ := result.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
|
||||
// Verify nickname was not updated
|
||||
one, err := db.Model(table).Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["nickname"], "name_1")
|
||||
t.Assert(one["passport"], "struct_user")
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_OmitNil_WithPointerStruct tests OmitNil with pointer struct data
|
||||
func Test_Model_OmitNil_WithPointerStruct(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
type User struct {
|
||||
Id int
|
||||
Passport *string
|
||||
Nickname *string
|
||||
Password string
|
||||
}
|
||||
|
||||
// Note: Removed OmitNilData with pointer struct test due to framework limitations
|
||||
// Struct field nil pointer handling needs further investigation
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Test OmitNilData with Map (working as expected)
|
||||
sqlArray2, err := gdb.CatchSQL(ctx, func(ctx context.Context) error {
|
||||
_, err := db.Ctx(ctx).Model(table).OmitNilData().Data(g.Map{
|
||||
"passport": "map_user",
|
||||
"nickname": nil,
|
||||
"password": "map_pass",
|
||||
}).Where("id", 2).Update()
|
||||
return err
|
||||
})
|
||||
t.AssertNil(err)
|
||||
t.Logf("Map SQL: %v", sqlArray2)
|
||||
|
||||
one2, err := db.Model(table).Where("id", 2).One()
|
||||
t.AssertNil(err)
|
||||
t.Logf("Map result - nickname: %v, passport: %v", one2["nickname"], one2["passport"])
|
||||
t.Assert(one2["nickname"], "name_2") // should be preserved
|
||||
t.Assert(one2["passport"], "map_user")
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_OmitEmpty_ZeroValues tests OmitEmpty with various zero values
|
||||
func Test_Model_OmitEmpty_ZeroValues(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Test OmitEmptyData with various zero values
|
||||
result, err := db.Model(table).OmitEmptyData().Data(g.Map{
|
||||
"id": 0, // zero int, should be omitted
|
||||
"passport": "zero_test", // non-empty
|
||||
"nickname": "", // empty string, should be omitted
|
||||
}).Insert()
|
||||
t.AssertNil(err)
|
||||
n, _ := result.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
|
||||
// Verify the insert (id should be auto-generated since 0 was omitted)
|
||||
one, err := db.Model(table).Where("passport", "zero_test").One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], "zero_test")
|
||||
t.AssertNE(one["id"], 0) // auto-generated id
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_OmitEmpty_ComplexWhere tests OmitEmpty with complex where conditions
|
||||
func Test_Model_OmitEmpty_ComplexWhere(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Test OmitEmptyWhere with multiple conditions
|
||||
all, err := db.Model(table).OmitEmptyWhere().Where(g.Map{
|
||||
"id >": 0, // zero, should be omitted
|
||||
"passport": "", // empty string, should be omitted
|
||||
"nickname": "?", // placeholder, should NOT be omitted
|
||||
}).Order("id").Limit(3).All()
|
||||
t.AssertNil(err)
|
||||
// Should execute query with only the nickname condition
|
||||
|
||||
// Test with all empty conditions
|
||||
all, err = db.Model(table).OmitEmptyWhere().Where(g.Map{
|
||||
"passport": "",
|
||||
"nickname": "",
|
||||
}).Order("id").Limit(5).All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(all), 5) // all conditions omitted, returns all (limited to 5)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Omit_ChainedMethods tests Omit methods with other chained methods
|
||||
func Test_Model_Omit_ChainedMethods(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Test OmitEmpty with Fields and Order
|
||||
result, err := db.Model(table).
|
||||
OmitEmptyData().
|
||||
Fields("passport", "nickname").
|
||||
Data(g.Map{
|
||||
"passport": "chain_test",
|
||||
"nickname": "",
|
||||
}).
|
||||
Where("id", 1).
|
||||
Update()
|
||||
t.AssertNil(err)
|
||||
n, _ := result.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
|
||||
one, err := db.Model(table).Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], "chain_test")
|
||||
t.Assert(one["nickname"], "name_1") // not updated due to OmitEmptyData
|
||||
|
||||
// Test OmitNilWhere with multiple Where clauses
|
||||
all, err := db.Model(table).
|
||||
OmitNilWhere().
|
||||
Where("id>?", 5).
|
||||
Where(g.Map{
|
||||
"passport": nil, // should be omitted
|
||||
}).
|
||||
Order("id").
|
||||
All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(all), 5) // id 6-10
|
||||
})
|
||||
}
|
||||
@ -1,545 +0,0 @@
|
||||
// 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 mariadb_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
// Test_Model_AllAndCount_Basic tests basic AllAndCount functionality
|
||||
func Test_Model_AllAndCount_Basic(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, count, err := db.Model(table).AllAndCount(false)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(result), TableSize)
|
||||
t.Assert(count, TableSize)
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, count, err := db.Model(table).AllAndCount(true)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(result), TableSize)
|
||||
t.Assert(count, TableSize)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_AllAndCount_WithWhere tests AllAndCount with WHERE conditions
|
||||
func Test_Model_AllAndCount_WithWhere(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, count, err := db.Model(table).Where("id > ?", 5).AllAndCount(false)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(result), 5)
|
||||
t.Assert(count, 5)
|
||||
t.Assert(result[0]["id"], 6)
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, count, err := db.Model(table).Where("id", g.Slice{1, 2, 3}).AllAndCount(false)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(result), 3)
|
||||
t.Assert(count, 3)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_AllAndCount_WithPage tests AllAndCount with pagination
|
||||
func Test_Model_AllAndCount_WithPage(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, count, err := db.Model(table).Page(1, 3).Order("id").AllAndCount(false)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(result), 3)
|
||||
t.Assert(count, TableSize) // Count should be total, not page size
|
||||
t.Assert(result[0]["id"], 1)
|
||||
t.Assert(result[2]["id"], 3)
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, count, err := db.Model(table).Page(2, 3).Order("id").AllAndCount(false)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(result), 3)
|
||||
t.Assert(count, TableSize)
|
||||
t.Assert(result[0]["id"], 4)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_AllAndCount_WithFields tests AllAndCount with specific fields
|
||||
// Related: https://github.com/gogf/gf/issues/4698
|
||||
func Test_Model_AllAndCount_WithFields(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, count, err := db.Model(table).Fields("id, nickname").AllAndCount(false)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(result), TableSize)
|
||||
t.Assert(count, TableSize)
|
||||
t.Assert(len(result[0]), 2) // Only 2 fields
|
||||
})
|
||||
|
||||
// Regression test for #4698: AllAndCount(true) with multiple fields should work correctly
|
||||
// https://github.com/gogf/gf/issues/4698
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, count, err := db.Model(table).Fields("id, nickname").AllAndCount(true)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(result), TableSize)
|
||||
t.Assert(count, TableSize)
|
||||
t.Assert(len(result[0]), 2)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_AllAndCount_Empty tests AllAndCount with no results
|
||||
func Test_Model_AllAndCount_Empty(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, count, err := db.Model(table).Where("id > ?", 1000).AllAndCount(false)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(result), 0)
|
||||
t.Assert(count, 0)
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, count, err := db.Model(table).Where("id < ?", 0).AllAndCount(true)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(result), 0)
|
||||
t.Assert(count, 0)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_AllAndCount_WithCache tests AllAndCount with cache
|
||||
func Test_Model_AllAndCount_WithCache(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result1, count1, err := db.Model(table).PageCache(gdb.CacheOption{
|
||||
Duration: time.Second * 10,
|
||||
Force: false,
|
||||
}, gdb.CacheOption{
|
||||
Duration: time.Second * 10,
|
||||
Force: false,
|
||||
}).Page(1, 5).AllAndCount(false)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(result1), 5)
|
||||
t.Assert(count1, TableSize)
|
||||
|
||||
// Second call should use cache
|
||||
result2, count2, err := db.Model(table).PageCache(gdb.CacheOption{
|
||||
Duration: time.Second * 10,
|
||||
Force: false,
|
||||
}, gdb.CacheOption{
|
||||
Duration: time.Second * 10,
|
||||
Force: false,
|
||||
}).Page(1, 5).AllAndCount(false)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(result2), 5)
|
||||
t.Assert(count2, count1)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_AllAndCount_Distinct tests AllAndCount with DISTINCT
|
||||
func Test_Model_AllAndCount_Distinct(t *testing.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
|
||||
// Insert duplicate nicknames
|
||||
for i := 1; i <= 10; i++ {
|
||||
nickname := "name_" + gconv.String((i-1)/2) // Creates duplicates
|
||||
db.Model(table).Data(g.Map{
|
||||
"id": i,
|
||||
"passport": "pass_" + gconv.String(i),
|
||||
"password": "pwd",
|
||||
"nickname": nickname,
|
||||
}).Insert()
|
||||
}
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, count, err := db.Model(table).Fields("DISTINCT nickname").AllAndCount(true)
|
||||
t.AssertNil(err)
|
||||
t.Assert(count, 5) // 10 records / 2 = 5 distinct nicknames
|
||||
t.Assert(len(result), 5)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_ScanAndCount_Basic tests basic ScanAndCount functionality
|
||||
func Test_Model_ScanAndCount_Basic(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
type User struct {
|
||||
Id int
|
||||
Passport string
|
||||
Password string
|
||||
Nickname string
|
||||
}
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var users []User
|
||||
var count int
|
||||
err := db.Model(table).ScanAndCount(&users, &count, false)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(users), TableSize)
|
||||
t.Assert(count, TableSize)
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var users []User
|
||||
var count int
|
||||
err := db.Model(table).ScanAndCount(&users, &count, true)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(users), TableSize)
|
||||
t.Assert(count, TableSize)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_ScanAndCount_WithWhere tests ScanAndCount with WHERE conditions
|
||||
func Test_Model_ScanAndCount_WithWhere(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
type User struct {
|
||||
Id int
|
||||
Passport string
|
||||
Nickname string
|
||||
}
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var users []User
|
||||
var count int
|
||||
err := db.Model(table).Where("id <= ?", 5).ScanAndCount(&users, &count, false)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(users), 5)
|
||||
t.Assert(count, 5)
|
||||
t.Assert(users[0].Id, 1)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_ScanAndCount_WithPage tests ScanAndCount with pagination
|
||||
func Test_Model_ScanAndCount_WithPage(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
type User struct {
|
||||
Id int
|
||||
Nickname string
|
||||
}
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var users []User
|
||||
var count int
|
||||
err := db.Model(table).Page(2, 3).Order("id").ScanAndCount(&users, &count, false)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(users), 3)
|
||||
t.Assert(count, TableSize) // Total count, not page count
|
||||
t.Assert(users[0].Id, 4)
|
||||
t.Assert(users[2].Id, 6)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_ScanAndCount_Single tests ScanAndCount for single record
|
||||
func Test_Model_ScanAndCount_Single(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
type User struct {
|
||||
Id int
|
||||
Passport string
|
||||
}
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var user User
|
||||
var count int
|
||||
err := db.Model(table).Where("id", 1).ScanAndCount(&user, &count, false)
|
||||
t.AssertNil(err)
|
||||
t.Assert(user.Id, 1)
|
||||
t.Assert(count, 1)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_ScanAndCount_Empty tests ScanAndCount with no results
|
||||
func Test_Model_ScanAndCount_Empty(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
type User struct {
|
||||
Id int
|
||||
}
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var users []User
|
||||
var count int
|
||||
err := db.Model(table).Where("id > ?", 1000).ScanAndCount(&users, &count, false)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(users), 0)
|
||||
t.Assert(count, 0)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_ScanAndCount_WithFields tests ScanAndCount with specific fields
|
||||
func Test_Model_ScanAndCount_WithFields(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
type User struct {
|
||||
Id int
|
||||
Nickname string
|
||||
}
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var users []User
|
||||
var count int
|
||||
err := db.Model(table).Fields("id, nickname").ScanAndCount(&users, &count, false)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(users), TableSize)
|
||||
t.Assert(count, TableSize)
|
||||
t.Assert(users[0].Id > 0, true)
|
||||
t.AssertNE(users[0].Nickname, "")
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_ScanAndCount_WithCache tests ScanAndCount with cache
|
||||
func Test_Model_ScanAndCount_WithCache(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
type User struct {
|
||||
Id int
|
||||
}
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var users1 []User
|
||||
var count1 int
|
||||
err := db.Model(table).PageCache(gdb.CacheOption{
|
||||
Duration: time.Second * 10,
|
||||
Force: false,
|
||||
}, gdb.CacheOption{
|
||||
Duration: time.Second * 10,
|
||||
Force: false,
|
||||
}).Page(1, 5).ScanAndCount(&users1, &count1, false)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(users1), 5)
|
||||
t.Assert(count1, TableSize)
|
||||
|
||||
// Second call should use cache
|
||||
var users2 []User
|
||||
var count2 int
|
||||
err = db.Model(table).PageCache(gdb.CacheOption{
|
||||
Duration: time.Second * 10,
|
||||
Force: false,
|
||||
}, gdb.CacheOption{
|
||||
Duration: time.Second * 10,
|
||||
Force: false,
|
||||
}).Page(1, 5).ScanAndCount(&users2, &count2, false)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(users2), 5)
|
||||
t.Assert(count2, count1)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Chunk_Basic tests basic Chunk functionality
|
||||
func Test_Model_Chunk_Basic(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var (
|
||||
total int
|
||||
chunks int
|
||||
)
|
||||
db.Model(table).Order("id").Chunk(3, func(result gdb.Result, err error) bool {
|
||||
t.AssertNil(err)
|
||||
chunks++
|
||||
total += len(result)
|
||||
return true
|
||||
})
|
||||
t.Assert(chunks, 4) // 10 records / 3 = 4 chunks (3+3+3+1)
|
||||
t.Assert(total, TableSize)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Chunk_StopEarly tests Chunk with early stop
|
||||
func Test_Model_Chunk_StopEarly(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var chunks int
|
||||
db.Model(table).Order("id").Chunk(3, func(result gdb.Result, err error) bool {
|
||||
t.AssertNil(err)
|
||||
chunks++
|
||||
return chunks < 2 // Stop after 2nd chunk
|
||||
})
|
||||
t.Assert(chunks, 2)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Chunk_WithWhere tests Chunk with WHERE conditions
|
||||
func Test_Model_Chunk_WithWhere(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var (
|
||||
total int
|
||||
chunks int
|
||||
)
|
||||
db.Model(table).Where("id <= ?", 5).Order("id").Chunk(2, func(result gdb.Result, err error) bool {
|
||||
t.AssertNil(err)
|
||||
chunks++
|
||||
total += len(result)
|
||||
return true
|
||||
})
|
||||
t.Assert(chunks, 3) // 5 records / 2 = 3 chunks (2+2+1)
|
||||
t.Assert(total, 5)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Chunk_ErrorHandling tests Chunk error handling
|
||||
func Test_Model_Chunk_ErrorHandling(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var errorReceived bool
|
||||
db.Model("non_existent_table").Chunk(10, func(result gdb.Result, err error) bool {
|
||||
if err != nil {
|
||||
errorReceived = true
|
||||
return false
|
||||
}
|
||||
return true
|
||||
})
|
||||
t.Assert(errorReceived, true)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Chunk_Empty tests Chunk with no results
|
||||
func Test_Model_Chunk_Empty(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var chunks int
|
||||
db.Model(table).Where("id > ?", 1000).Chunk(10, func(result gdb.Result, err error) bool {
|
||||
chunks++
|
||||
return true
|
||||
})
|
||||
t.Assert(chunks, 0) // No chunks for empty result
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Page_Boundary tests Page with boundary values
|
||||
// Related: https://github.com/gogf/gf/issues/4699
|
||||
func Test_Model_Page_Boundary(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
// Page 0 should be treated as page 1
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, err := db.Model(table).Page(0, 3).Order("id").All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(result), 3)
|
||||
t.Assert(result[0]["id"], 1)
|
||||
})
|
||||
|
||||
// Negative page should be treated as page 1
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, err := db.Model(table).Page(-1, 3).Order("id").All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(result), 3)
|
||||
t.Assert(result[0]["id"], 1)
|
||||
})
|
||||
|
||||
// Size 0: framework treats limit=0 as "no limit", returns all records
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, err := db.Model(table).Page(1, 0).All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(result), TableSize)
|
||||
})
|
||||
|
||||
// Negative size: normalized to 0, same as Page(1, 0)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, err := db.Model(table).Page(1, -1).All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(result), TableSize)
|
||||
})
|
||||
|
||||
// Very large page number (beyond available data)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, err := db.Model(table).Page(100, 3).All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(result), 0)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Limit_Boundary tests Limit with boundary values
|
||||
// Related: https://github.com/gogf/gf/issues/4699
|
||||
func Test_Model_Limit_Boundary(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
// Limit 0: framework treats limit=0 as "no limit", returns all records
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, err := db.Model(table).Limit(0).All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(result), TableSize)
|
||||
})
|
||||
|
||||
// Negative limit: normalized to 0, same as Limit(0)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, err := db.Model(table).Limit(-1).All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(result), TableSize)
|
||||
})
|
||||
|
||||
// Limit larger than available data
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, err := db.Model(table).Limit(1000).All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(result), TableSize)
|
||||
})
|
||||
|
||||
// Limit(offset, size): offset=5 skips 5 rows, size=100 takes up to 100
|
||||
// With 10 rows total, skipping 5 returns remaining 5 rows
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, err := db.Model(table).Limit(5, 100).All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(result), TableSize-5)
|
||||
})
|
||||
|
||||
// Offset beyond data: returns empty result
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, err := db.Model(table).Limit(100, 5).All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(result), 0)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Model_Page_Limit_Combination tests Page and Limit used together
|
||||
func Test_Model_Page_Limit_Combination(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Page should override Limit
|
||||
result, err := db.Model(table).Limit(5).Page(1, 3).Order("id").All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(result), 3)
|
||||
t.Assert(result[0]["id"], 1)
|
||||
})
|
||||
}
|
||||
@ -1,905 +0,0 @@
|
||||
// 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 mariadb_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
func Test_Raw_Insert(t *testing.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
user := db.Model(table)
|
||||
result, err := user.Data(g.Map{
|
||||
"id": gdb.Raw("id+2"),
|
||||
"passport": "port_1",
|
||||
"password": "pass_1",
|
||||
"nickname": "name_1",
|
||||
"create_time": gdb.Raw("now()"),
|
||||
}).Insert()
|
||||
t.AssertNil(err)
|
||||
n, _ := result.LastInsertId()
|
||||
t.Assert(n, 2)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Raw_BatchInsert(t *testing.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
user := db.Model(table)
|
||||
result, err := user.Data(
|
||||
g.List{
|
||||
g.Map{
|
||||
"id": gdb.Raw("id+2"),
|
||||
"passport": "port_2",
|
||||
"password": "pass_2",
|
||||
"nickname": "name_2",
|
||||
"create_time": gdb.Raw("now()"),
|
||||
},
|
||||
g.Map{
|
||||
"id": gdb.Raw("id+4"),
|
||||
"passport": "port_4",
|
||||
"password": "pass_4",
|
||||
"nickname": "name_4",
|
||||
"create_time": gdb.Raw("now()"),
|
||||
},
|
||||
},
|
||||
).Insert()
|
||||
t.AssertNil(err)
|
||||
n, _ := result.LastInsertId()
|
||||
t.Assert(n, 4)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Raw_Update(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
user := db.Model(table)
|
||||
result, err := user.Data(g.Map{
|
||||
"id": gdb.Raw("id+100"),
|
||||
"create_time": gdb.Raw("now()"),
|
||||
}).Where("id", 1).Update()
|
||||
t.AssertNil(err)
|
||||
n, _ := result.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
user := db.Model(table)
|
||||
n, err := user.Where("id", 101).Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(n, 1)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Raw_Where(t *testing.T) {
|
||||
table1 := createTable("Test_Raw_Where_Table1")
|
||||
table2 := createTable("Test_Raw_Where_Table2")
|
||||
defer dropTable(table1)
|
||||
defer dropTable(table2)
|
||||
|
||||
// https://github.com/gogf/gf/issues/3922
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expectSql := "SELECT * FROM `Test_Raw_Where_Table1` AS A WHERE NOT EXISTS (SELECT B.id FROM `Test_Raw_Where_Table2` AS B WHERE `B`.`id`=A.id) LIMIT 1"
|
||||
sql, err := gdb.ToSQL(ctx, func(ctx context.Context) error {
|
||||
s := db.Model(table2).As("B").Ctx(ctx).Fields("B.id").Where("B.id", gdb.Raw("A.id"))
|
||||
m := db.Model(table1).As("A").Ctx(ctx).Where("NOT EXISTS ?", s).Limit(1)
|
||||
_, err := m.All()
|
||||
return err
|
||||
})
|
||||
t.AssertNil(err)
|
||||
t.Assert(expectSql, sql)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expectSql := "SELECT * FROM `Test_Raw_Where_Table1` AS A WHERE NOT EXISTS (SELECT B.id FROM `Test_Raw_Where_Table2` AS B WHERE B.id=A.id) LIMIT 1"
|
||||
sql, err := gdb.ToSQL(ctx, func(ctx context.Context) error {
|
||||
s := db.Model(table2).As("B").Ctx(ctx).Fields("B.id").Where(gdb.Raw("B.id=A.id"))
|
||||
m := db.Model(table1).As("A").Ctx(ctx).Where("NOT EXISTS ?", s).Limit(1)
|
||||
_, err := m.All()
|
||||
return err
|
||||
})
|
||||
t.AssertNil(err)
|
||||
t.Assert(expectSql, sql)
|
||||
})
|
||||
// https://github.com/gogf/gf/issues/3915
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expectSql := "SELECT * FROM `Test_Raw_Where_Table1` WHERE `passport` < `nickname`"
|
||||
sql, err := gdb.ToSQL(ctx, func(ctx context.Context) error {
|
||||
m := db.Model(table1).Ctx(ctx).WhereLT("passport", gdb.Raw("`nickname`"))
|
||||
_, err := m.All()
|
||||
return err
|
||||
})
|
||||
t.AssertNil(err)
|
||||
t.Assert(expectSql, sql)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_DataType_JSON_Insert tests JSON data insertion
|
||||
func Test_DataType_JSON_Insert(t *testing.T) {
|
||||
table := "test_json_" + gtime.TimestampMicroStr()
|
||||
_, err := db.Exec(ctx, "CREATE TABLE "+table+" (id INT PRIMARY KEY AUTO_INCREMENT, data JSON)")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Insert simple JSON object
|
||||
result, err := db.Model(table).Data(g.Map{
|
||||
"data": `{"name":"John","age":30}`,
|
||||
}).Insert()
|
||||
t.AssertNil(err)
|
||||
n, _ := result.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
|
||||
// Verify data
|
||||
one, err := db.Model(table).Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
expected := map[string]interface{}{"name": "John", "age": float64(30)}
|
||||
var actual map[string]interface{}
|
||||
err = json.Unmarshal([]byte(one["data"].String()), &actual)
|
||||
t.AssertNil(err)
|
||||
t.Assert(actual, expected)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_DataType_JSON_Extract tests JSON_EXTRACT function
|
||||
func Test_DataType_JSON_Extract(t *testing.T) {
|
||||
table := "test_json_extract_" + gtime.TimestampMicroStr()
|
||||
_, err := db.Exec(ctx, "CREATE TABLE "+table+" (id INT PRIMARY KEY AUTO_INCREMENT, data JSON)")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Insert test data
|
||||
_, err := db.Model(table).Data(g.Map{
|
||||
"data": `{"name":"Alice","age":25,"city":"Beijing"}`,
|
||||
}).Insert()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Extract name using JSON_EXTRACT
|
||||
one, err := db.Model(table).Fields("JSON_EXTRACT(data, '$.name') as name").Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["name"].String(), `"Alice"`)
|
||||
|
||||
// Extract age
|
||||
one, err = db.Model(table).Fields("JSON_EXTRACT(data, '$.age') as age").Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["age"].Int(), 25)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_DataType_JSON_Set tests JSON_SET function
|
||||
func Test_DataType_JSON_Set(t *testing.T) {
|
||||
table := "test_json_set_" + gtime.TimestampMicroStr()
|
||||
_, err := db.Exec(ctx, "CREATE TABLE "+table+" (id INT PRIMARY KEY AUTO_INCREMENT, data JSON)")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Insert initial data
|
||||
_, err := db.Model(table).Data(g.Map{
|
||||
"data": `{"name":"Bob"}`,
|
||||
}).Insert()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Update using JSON_SET
|
||||
_, err = db.Exec(ctx, fmt.Sprintf("UPDATE %s SET data = JSON_SET(data, '$.age', 30) WHERE id = 1", table))
|
||||
t.AssertNil(err)
|
||||
|
||||
// Verify updated data
|
||||
one, err := db.Model(table).Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
expected := map[string]interface{}{"name": "Bob", "age": float64(30)}
|
||||
var actual map[string]interface{}
|
||||
err = json.Unmarshal([]byte(one["data"].String()), &actual)
|
||||
t.AssertNil(err)
|
||||
t.Assert(actual, expected)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_DataType_JSON_Array tests JSON array operations
|
||||
func Test_DataType_JSON_Array(t *testing.T) {
|
||||
table := "test_json_array_" + gtime.TimestampMicroStr()
|
||||
_, err := db.Exec(ctx, "CREATE TABLE "+table+" (id INT PRIMARY KEY AUTO_INCREMENT, data JSON)")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Insert JSON array
|
||||
_, err := db.Model(table).Data(g.Map{
|
||||
"data": `["apple","banana","cherry"]`,
|
||||
}).Insert()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Extract array element
|
||||
one, err := db.Model(table).Fields("JSON_EXTRACT(data, '$[0]') as first").Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["first"].String(), `"apple"`)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_DataType_JSON_Null tests JSON NULL handling
|
||||
func Test_DataType_JSON_Null(t *testing.T) {
|
||||
table := "test_json_null_" + gtime.TimestampMicroStr()
|
||||
_, err := db.Exec(ctx, "CREATE TABLE "+table+" (id INT PRIMARY KEY AUTO_INCREMENT, data JSON)")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Insert NULL value
|
||||
_, err := db.Model(table).Data(g.Map{
|
||||
"data": nil,
|
||||
}).Insert()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Verify NULL
|
||||
one, err := db.Model(table).Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["data"].IsNil(), true)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_DataType_JSON_Complex tests complex nested JSON
|
||||
func Test_DataType_JSON_Complex(t *testing.T) {
|
||||
table := "test_json_complex_" + gtime.TimestampMicroStr()
|
||||
_, err := db.Exec(ctx, "CREATE TABLE "+table+" (id INT PRIMARY KEY AUTO_INCREMENT, data JSON)")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Insert complex nested JSON
|
||||
complexJSON := `{
|
||||
"user": {
|
||||
"name": "Charlie",
|
||||
"contacts": {
|
||||
"email": "charlie@example.com",
|
||||
"phone": "1234567890"
|
||||
},
|
||||
"tags": ["developer", "gopher"]
|
||||
}
|
||||
}`
|
||||
_, err := db.Model(table).Data(g.Map{
|
||||
"data": complexJSON,
|
||||
}).Insert()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Extract nested field
|
||||
one, err := db.Model(table).Fields("JSON_EXTRACT(data, '$.user.contacts.email') as email").Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["email"].String(), `"charlie@example.com"`)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_DataType_JSON_Query tests JSON query with WHERE clause
|
||||
func Test_DataType_JSON_Query(t *testing.T) {
|
||||
table := "test_json_query_" + gtime.TimestampMicroStr()
|
||||
_, err := db.Exec(ctx, "CREATE TABLE "+table+" (id INT PRIMARY KEY AUTO_INCREMENT, data JSON)")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Insert multiple JSON records
|
||||
_, err := db.Model(table).Data(g.List{
|
||||
g.Map{"data": `{"name":"David","age":20}`},
|
||||
g.Map{"data": `{"name":"Eve","age":30}`},
|
||||
g.Map{"data": `{"name":"Frank","age":25}`},
|
||||
}).Insert()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Query by JSON field value
|
||||
count, err := db.Model(table).Where("JSON_EXTRACT(data, '$.age') > ?", 25).Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(count, 1)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_DataType_JSON_Update tests updating JSON data
|
||||
func Test_DataType_JSON_Update(t *testing.T) {
|
||||
table := "test_json_update_" + gtime.TimestampMicroStr()
|
||||
_, err := db.Exec(ctx, "CREATE TABLE "+table+" (id INT PRIMARY KEY AUTO_INCREMENT, data JSON)")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Insert initial data
|
||||
_, err := db.Model(table).Data(g.Map{
|
||||
"data": `{"name":"Grace","age":28}`,
|
||||
}).Insert()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Update entire JSON
|
||||
_, err = db.Model(table).Data(g.Map{
|
||||
"data": `{"name":"Grace","age":29}`,
|
||||
}).Where("id", 1).Update()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Verify update
|
||||
one, err := db.Model(table).Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
expected := map[string]interface{}{"name": "Grace", "age": float64(29)}
|
||||
var actual map[string]interface{}
|
||||
err = json.Unmarshal([]byte(one["data"].String()), &actual)
|
||||
t.AssertNil(err)
|
||||
t.Assert(actual, expected)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_DataType_Binary_Small tests small binary data
|
||||
func Test_DataType_Binary_Small(t *testing.T) {
|
||||
table := "test_binary_small_" + gtime.TimestampMicroStr()
|
||||
_, err := db.Exec(ctx, "CREATE TABLE "+table+" (id INT PRIMARY KEY AUTO_INCREMENT, data BLOB)")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Insert small binary data
|
||||
binaryData := []byte{0x00, 0x01, 0x02, 0x03, 0xFF}
|
||||
_, err := db.Model(table).Data(g.Map{
|
||||
"data": binaryData,
|
||||
}).Insert()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Verify data
|
||||
one, err := db.Model(table).Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(bytes.Equal(one["data"].Bytes(), binaryData), true)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_DataType_Binary_Large tests large binary data (1MB+)
|
||||
func Test_DataType_Binary_Large(t *testing.T) {
|
||||
table := "test_binary_large_" + gtime.TimestampMicroStr()
|
||||
_, err := db.Exec(ctx, "CREATE TABLE "+table+" (id INT PRIMARY KEY AUTO_INCREMENT, data MEDIUMBLOB)")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Create 1MB binary data
|
||||
size := 1024 * 1024 // 1MB
|
||||
largeBinary := make([]byte, size)
|
||||
for i := 0; i < size; i++ {
|
||||
largeBinary[i] = byte(i % 256)
|
||||
}
|
||||
|
||||
// Insert large binary data
|
||||
_, err := db.Model(table).Data(g.Map{
|
||||
"data": largeBinary,
|
||||
}).Insert()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Verify data
|
||||
one, err := db.Model(table).Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(one["data"].Bytes()), size)
|
||||
t.Assert(bytes.Equal(one["data"].Bytes(), largeBinary), true)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_DataType_Binary_Integrity tests binary data integrity with checksum
|
||||
func Test_DataType_Binary_Integrity(t *testing.T) {
|
||||
table := "test_binary_integrity_" + gtime.TimestampMicroStr()
|
||||
_, err := db.Exec(ctx, "CREATE TABLE "+table+" (id INT PRIMARY KEY AUTO_INCREMENT, data BLOB, checksum VARCHAR(64))")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Create random binary data
|
||||
binaryData := []byte("Hello, World! This is a binary test data with special chars: \x00\xFF\xAB")
|
||||
|
||||
// Calculate SHA256 checksum
|
||||
hash := sha256.Sum256(binaryData)
|
||||
checksum := hex.EncodeToString(hash[:])
|
||||
|
||||
// Insert with checksum
|
||||
_, err := db.Model(table).Data(g.Map{
|
||||
"data": binaryData,
|
||||
"checksum": checksum,
|
||||
}).Insert()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Verify integrity
|
||||
one, err := db.Model(table).Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
|
||||
retrievedHash := sha256.Sum256(one["data"].Bytes())
|
||||
retrievedChecksum := hex.EncodeToString(retrievedHash[:])
|
||||
t.Assert(retrievedChecksum, checksum)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_DataType_Binary_Empty tests empty and NULL binary
|
||||
func Test_DataType_Binary_Empty(t *testing.T) {
|
||||
table := "test_binary_empty_" + gtime.TimestampMicroStr()
|
||||
_, err := db.Exec(ctx, "CREATE TABLE "+table+" (id INT PRIMARY KEY AUTO_INCREMENT, data BLOB)")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Insert empty binary
|
||||
_, err := db.Model(table).Data(g.Map{
|
||||
"data": []byte{},
|
||||
}).Insert()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Insert NULL
|
||||
_, err = db.Model(table).Data(g.Map{
|
||||
"data": nil,
|
||||
}).Insert()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Verify empty
|
||||
one, err := db.Model(table).Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(one["data"].Bytes()), 0)
|
||||
|
||||
// Verify NULL
|
||||
one, err = db.Model(table).Where("id", 2).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["data"].IsNil(), true)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_DataType_Decimal_HighPrecision tests high precision decimal (65,30)
|
||||
func Test_DataType_Decimal_HighPrecision(t *testing.T) {
|
||||
table := "test_decimal_precision_" + gtime.TimestampMicroStr()
|
||||
_, err := db.Exec(ctx, "CREATE TABLE "+table+" (id INT PRIMARY KEY AUTO_INCREMENT, amount DECIMAL(65,30))")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Insert high precision decimal
|
||||
value := "12345678901234567890123456789012345.123456789012345678901234567890"
|
||||
_, err := db.Model(table).Data(g.Map{
|
||||
"amount": value,
|
||||
}).Insert()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Verify precision
|
||||
one, err := db.Model(table).Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["amount"].String(), value)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_DataType_Decimal_Calculation tests decimal arithmetic
|
||||
func Test_DataType_Decimal_Calculation(t *testing.T) {
|
||||
table := "test_decimal_calc_" + gtime.TimestampMicroStr()
|
||||
_, err := db.Exec(ctx, "CREATE TABLE "+table+" (id INT PRIMARY KEY AUTO_INCREMENT, price DECIMAL(10,2), quantity DECIMAL(10,2))")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Insert test data
|
||||
_, err := db.Model(table).Data(g.Map{
|
||||
"price": "19.99",
|
||||
"quantity": "3.5",
|
||||
}).Insert()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Calculate total using SQL
|
||||
one, err := db.Model(table).Fields("price * quantity as total").Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["total"].String(), "69.9650")
|
||||
})
|
||||
}
|
||||
|
||||
// Test_DataType_Decimal_Boundary tests decimal boundary values
|
||||
func Test_DataType_Decimal_Boundary(t *testing.T) {
|
||||
table := "test_decimal_boundary_" + gtime.TimestampMicroStr()
|
||||
_, err := db.Exec(ctx, "CREATE TABLE "+table+" (id INT PRIMARY KEY AUTO_INCREMENT, value DECIMAL(10,2))")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Test max value (10 digits, 2 decimals: 99999999.99)
|
||||
_, err := db.Model(table).Data(g.Map{
|
||||
"value": "99999999.99",
|
||||
}).Insert()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Test min value
|
||||
_, err = db.Model(table).Data(g.Map{
|
||||
"value": "-99999999.99",
|
||||
}).Insert()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Test zero
|
||||
_, err = db.Model(table).Data(g.Map{
|
||||
"value": "0.00",
|
||||
}).Insert()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Verify all values
|
||||
all, err := db.Model(table).Order("id").All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(all), 3)
|
||||
t.Assert(all[0]["value"].String(), "99999999.99")
|
||||
t.Assert(all[1]["value"].String(), "-99999999.99")
|
||||
t.Assert(all[2]["value"].String(), "0.00")
|
||||
})
|
||||
}
|
||||
|
||||
// Test_DataType_Decimal_Null tests NULL decimal values
|
||||
func Test_DataType_Decimal_Null(t *testing.T) {
|
||||
table := "test_decimal_null_" + gtime.TimestampMicroStr()
|
||||
_, err := db.Exec(ctx, "CREATE TABLE "+table+" (id INT PRIMARY KEY AUTO_INCREMENT, value DECIMAL(10,2))")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Insert NULL
|
||||
_, err := db.Model(table).Data(g.Map{
|
||||
"value": nil,
|
||||
}).Insert()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Verify NULL
|
||||
one, err := db.Model(table).Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["value"].IsNil(), true)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_DataType_Datetime_Timezone tests datetime with timezone handling
|
||||
func Test_DataType_Datetime_Timezone(t *testing.T) {
|
||||
table := "test_datetime_tz_" + gtime.TimestampMicroStr()
|
||||
_, err := db.Exec(ctx, "CREATE TABLE "+table+" (id INT PRIMARY KEY AUTO_INCREMENT, created_at DATETIME)")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Insert datetime
|
||||
dt := "2024-01-15 12:30:45"
|
||||
_, err := db.Model(table).Data(g.Map{
|
||||
"created_at": dt,
|
||||
}).Insert()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Verify datetime
|
||||
one, err := db.Model(table).Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["created_at"].String(), dt)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_DataType_Datetime_Precision tests datetime with microsecond precision
|
||||
func Test_DataType_Datetime_Precision(t *testing.T) {
|
||||
table := "test_datetime_precision_" + gtime.TimestampMicroStr()
|
||||
_, err := db.Exec(ctx, "CREATE TABLE "+table+" (id INT PRIMARY KEY AUTO_INCREMENT, created_at DATETIME(6))")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Insert datetime with microseconds
|
||||
dt := "2024-01-15 12:30:45.123456"
|
||||
_, err := db.Model(table).Data(g.Map{
|
||||
"created_at": dt,
|
||||
}).Insert()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Verify precision (compare up to seconds, MySQL may format microseconds differently)
|
||||
one, err := db.Model(table).Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
expected := "2024-01-15 12:30:45"
|
||||
actual := one["created_at"].String()[:19] // Extract first 19 chars (YYYY-MM-DD HH:MM:SS)
|
||||
t.Assert(actual, expected)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_DataType_Datetime_Boundary tests datetime boundary values
|
||||
func Test_DataType_Datetime_Boundary(t *testing.T) {
|
||||
table := "test_datetime_boundary_" + gtime.TimestampMicroStr()
|
||||
_, err := db.Exec(ctx, "CREATE TABLE "+table+" (id INT PRIMARY KEY AUTO_INCREMENT, dt DATETIME)")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Test min datetime (MySQL supports 1000-01-01 00:00:00)
|
||||
_, err := db.Model(table).Data(g.Map{
|
||||
"dt": "1000-01-01 00:00:00",
|
||||
}).Insert()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Test max datetime
|
||||
_, err = db.Model(table).Data(g.Map{
|
||||
"dt": "9999-12-31 23:59:59",
|
||||
}).Insert()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Verify boundaries
|
||||
all, err := db.Model(table).Order("id").All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(all), 2)
|
||||
t.Assert(all[0]["dt"].String(), "1000-01-01 00:00:00")
|
||||
t.Assert(all[1]["dt"].String(), "9999-12-31 23:59:59")
|
||||
})
|
||||
}
|
||||
|
||||
// Test_DataType_Datetime_Null tests NULL datetime
|
||||
func Test_DataType_Datetime_Null(t *testing.T) {
|
||||
table := "test_datetime_null_" + gtime.TimestampMicroStr()
|
||||
_, err := db.Exec(ctx, "CREATE TABLE "+table+" (id INT PRIMARY KEY AUTO_INCREMENT, dt DATETIME)")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Insert NULL
|
||||
_, err := db.Model(table).Data(g.Map{
|
||||
"dt": nil,
|
||||
}).Insert()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Verify NULL
|
||||
one, err := db.Model(table).Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["dt"].IsNil(), true)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_DataType_Datetime_Update tests datetime updates
|
||||
func Test_DataType_Datetime_Update(t *testing.T) {
|
||||
table := "test_datetime_update_" + gtime.TimestampMicroStr()
|
||||
_, err := db.Exec(ctx, "CREATE TABLE "+table+" (id INT PRIMARY KEY AUTO_INCREMENT, dt DATETIME)")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Insert initial datetime
|
||||
dt1 := "2024-01-01 10:00:00"
|
||||
_, err := db.Model(table).Data(g.Map{
|
||||
"dt": dt1,
|
||||
}).Insert()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Update datetime
|
||||
dt2 := "2024-12-31 23:59:59"
|
||||
_, err = db.Model(table).Data(g.Map{
|
||||
"dt": dt2,
|
||||
}).Where("id", 1).Update()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Verify update
|
||||
one, err := db.Model(table).Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["dt"].String(), dt2)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_DataType_Enum_Valid tests valid ENUM values
|
||||
func Test_DataType_Enum_Valid(t *testing.T) {
|
||||
table := "test_enum_valid_" + gtime.TimestampMicroStr()
|
||||
_, err := db.Exec(ctx, "CREATE TABLE "+table+" (id INT PRIMARY KEY AUTO_INCREMENT, status ENUM('pending','approved','rejected'))")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Insert all valid values
|
||||
_, err := db.Model(table).Data(g.List{
|
||||
g.Map{"status": "pending"},
|
||||
g.Map{"status": "approved"},
|
||||
g.Map{"status": "rejected"},
|
||||
}).Insert()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Verify all values
|
||||
all, err := db.Model(table).Order("id").All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(all), 3)
|
||||
t.Assert(all[0]["status"].String(), "pending")
|
||||
t.Assert(all[1]["status"].String(), "approved")
|
||||
t.Assert(all[2]["status"].String(), "rejected")
|
||||
})
|
||||
}
|
||||
|
||||
// Test_DataType_Enum_Invalid tests invalid ENUM values (should fail or truncate)
|
||||
func Test_DataType_Enum_Invalid(t *testing.T) {
|
||||
table := "test_enum_invalid_" + gtime.TimestampMicroStr()
|
||||
_, err := db.Exec(ctx, "CREATE TABLE "+table+" (id INT PRIMARY KEY AUTO_INCREMENT, status ENUM('pending','approved','rejected'))")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Attempt to insert invalid value (should fail in strict mode)
|
||||
_, err := db.Model(table).Data(g.Map{
|
||||
"status": "invalid_status",
|
||||
}).Insert()
|
||||
// In strict SQL mode, this should produce an error
|
||||
// In non-strict mode, it might insert empty string
|
||||
t.AssertNE(err, nil)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_DataType_Set_Valid tests valid SET values
|
||||
func Test_DataType_Set_Valid(t *testing.T) {
|
||||
table := "test_set_valid_" + gtime.TimestampMicroStr()
|
||||
_, err := db.Exec(ctx, "CREATE TABLE "+table+" (id INT PRIMARY KEY AUTO_INCREMENT, permissions SET('read','write','execute'))")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Insert single value
|
||||
_, err := db.Model(table).Data(g.Map{
|
||||
"permissions": "read",
|
||||
}).Insert()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Insert multiple values
|
||||
_, err = db.Model(table).Data(g.Map{
|
||||
"permissions": "read,write",
|
||||
}).Insert()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Insert all values
|
||||
_, err = db.Model(table).Data(g.Map{
|
||||
"permissions": "read,write,execute",
|
||||
}).Insert()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Verify all values
|
||||
all, err := db.Model(table).Order("id").All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(all), 3)
|
||||
t.Assert(all[0]["permissions"].String(), "read")
|
||||
t.Assert(all[1]["permissions"].String(), "read,write")
|
||||
t.Assert(all[2]["permissions"].String(), "read,write,execute")
|
||||
})
|
||||
}
|
||||
|
||||
// Test_DataType_Set_Empty tests empty SET values
|
||||
func Test_DataType_Set_Empty(t *testing.T) {
|
||||
table := "test_set_empty_" + gtime.TimestampMicroStr()
|
||||
_, err := db.Exec(ctx, "CREATE TABLE "+table+" (id INT PRIMARY KEY AUTO_INCREMENT, permissions SET('read','write','execute'))")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Insert empty SET
|
||||
_, err := db.Model(table).Data(g.Map{
|
||||
"permissions": "",
|
||||
}).Insert()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Verify empty
|
||||
one, err := db.Model(table).Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["permissions"].String(), "")
|
||||
})
|
||||
}
|
||||
|
||||
// Test_DataType_Geometry_Point tests POINT geometry type
|
||||
func Test_DataType_Geometry_Point(t *testing.T) {
|
||||
table := "test_geo_point_" + gtime.TimestampMicroStr()
|
||||
_, err := db.Exec(ctx, "CREATE TABLE "+table+" (id INT PRIMARY KEY AUTO_INCREMENT, location POINT)")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Insert POINT using ST_GeomFromText
|
||||
_, err := db.Exec(ctx, fmt.Sprintf("INSERT INTO %s (location) VALUES (ST_GeomFromText('POINT(116.4074 39.9042)'))", table))
|
||||
t.AssertNil(err)
|
||||
|
||||
// Query POINT using ST_AsText
|
||||
one, err := db.Model(table).Fields("ST_AsText(location) as location_text").Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["location_text"].String(), "POINT(116.4074 39.9042)")
|
||||
})
|
||||
}
|
||||
|
||||
// Test_DataType_Geometry_Polygon tests POLYGON geometry type
|
||||
func Test_DataType_Geometry_Polygon(t *testing.T) {
|
||||
table := "test_geo_polygon_" + gtime.TimestampMicroStr()
|
||||
_, err := db.Exec(ctx, "CREATE TABLE "+table+" (id INT PRIMARY KEY AUTO_INCREMENT, area POLYGON)")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Insert POLYGON (rectangle)
|
||||
polygon := "POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))"
|
||||
_, err := db.Exec(ctx, fmt.Sprintf("INSERT INTO %s (area) VALUES (ST_GeomFromText('%s'))", table, polygon))
|
||||
t.AssertNil(err)
|
||||
|
||||
// Query POLYGON (normalize spaces for comparison)
|
||||
one, err := db.Model(table).Fields("ST_AsText(area) as area_text").Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
expected := "POLYGON((0 0,10 0,10 10,0 10,0 0))"
|
||||
actual := strings.ReplaceAll(one["area_text"].String(), ", ", ",") // Remove spaces after commas
|
||||
t.Assert(actual, expected)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_DataType_Geometry_Null tests NULL geometry values
|
||||
func Test_DataType_Geometry_Null(t *testing.T) {
|
||||
table := "test_geo_null_" + gtime.TimestampMicroStr()
|
||||
_, err := db.Exec(ctx, "CREATE TABLE "+table+" (id INT PRIMARY KEY AUTO_INCREMENT, location POINT)")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Insert NULL
|
||||
_, err := db.Model(table).Data(g.Map{
|
||||
"location": nil,
|
||||
}).Insert()
|
||||
t.AssertNil(err)
|
||||
|
||||
// Verify NULL
|
||||
one, err := db.Model(table).Where("id", 1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["location"].IsNil(), true)
|
||||
})
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user