Compare commits

..

55 Commits

Author SHA1 Message Date
d64a31fd1a new version v2.6.3 (#3308) 2024-02-07 17:11:36 +08:00
6cdf044320 fix issue #2616 (#3307) 2024-02-07 16:47:41 +08:00
5307f0742e add # for cron pattern that can ignore seconds, which makes the cron pattern running in minimum minute like linux crontab pattern (#3306) 2024-02-07 15:38:24 +08:00
51326f3d02 fix #3245 (#3298) 2024-02-06 11:47:25 +08:00
e1fa99013a use iota to unify the enums definition (#3305) 2024-02-06 11:44:29 +08:00
72014689e4 fix issue #2643 (#3304) 2024-02-06 10:21:44 +08:00
2acdf4bb47 add field type detection for soft time field like created_at/updated_at/deleted_at to support unix timestamp or bool deleting table field (#3293) 2024-02-06 10:21:23 +08:00
85c5b7f19e fix issue #2594 (#3303) 2024-02-05 20:40:03 +08:00
42ec1a0076 fix issue #2572 (#3301) 2024-02-05 19:20:05 +08:00
553cc45e54 fix empty pwd path read from gfile.Pwd() which could cause internal error logging for gview.New (#3302) 2024-02-05 16:49:44 +08:00
7415ec32c8 fix issue #2503 (#3299) 2024-02-05 10:30:03 +08:00
14568562e3 fix issue #2552 (#3300) 2024-02-05 10:29:43 +08:00
cc79d23334 fix issue #2457 (#3297) 2024-02-02 10:57:24 +08:00
ef2b47d180 fix issue #3292 (#3294) 2024-02-01 16:02:36 +08:00
adc056f547 feat: support running commands via non-command line arguments (#3290) 2024-01-30 20:14:13 +08:00
8266a16b4e fix issue #3286 (#3289) 2024-01-30 20:03:58 +08:00
c4a51f4c2f bugfix:fix gconv map deep option not effective (#3287) 2024-01-29 19:52:04 +08:00
4ddc9c3909 add offline document link in readme.md (#3284) 2024-01-25 20:47:21 +08:00
0a2f8abb40 Update annotation for redis.toml (#3282) 2024-01-24 20:37:41 +08:00
81de5d3f25 fix workflow script for cli building and release (#3279) 2024-01-24 20:36:50 +08:00
e6d61e6e14 fix error in gerror.HasCode (#3277) 2024-01-24 20:36:32 +08:00
4fb739615b version v2.6.2 (#3276) 2024-01-22 21:53:10 +08:00
ba39323d30 improve converter feature for package gconv (#3211) 2024-01-22 21:52:54 +08:00
383937fe69 add example for ctx keys feature of package glog (#3273) 2024-01-22 21:07:40 +08:00
Bob
f919f90bf5 rename gitee issue template (#3274)
rename gitee ISSUE_TEMPLATE.MD to ISSUE_TEMPLATE
2024-01-22 21:07:04 +08:00
b4f6f06ab5 no printing internal middleware for ghttp.Server (#3271) 2024-01-22 21:05:40 +08:00
73fbca40ca add internal error logging and update comments for gclient.RequestVar (#3270) 2024-01-18 10:21:38 +08:00
e8d3fcac6b feat: upgrade gitee sync Hub Mirror Action (#3272) 2024-01-18 10:21:14 +08:00
f87182d5b8 fix workflow for gitee sync (#3269) 2024-01-17 20:27:41 +08:00
951f8921cd fix #3237 (#3267) 2024-01-17 15:35:48 +08:00
d26b7c5437 fix some wrong comment (#3265) 2024-01-16 14:08:07 +08:00
9407fda197 add example for exporting prometheus metrics using ghttp.Server (#3266) 2024-01-16 14:07:49 +08:00
905913f7be Fix gf gen service bug. Fix the issue of significant differences in the generated code every … (#3260) 2024-01-15 20:35:14 +08:00
4b8eaac73f fix issue #3232 (#3247) 2024-01-15 20:33:30 +08:00
ca242ff401 fix #3251 (#3254) 2024-01-11 22:15:22 +08:00
42e3c5f39a fix #3253 (#3255) 2024-01-11 22:14:22 +08:00
4f4d2c2f8e add MiddlewareNeverDoneCtx for package ghttp (#3250) 2024-01-06 13:03:49 +08:00
Gin
5a01798481 fix: memory leak when gcache.NewAdapterMemory with lru (#3241) 2024-01-03 20:03:19 +08:00
8af1eb693e Update README.MD (#3243) 2024-01-03 19:49:06 +08:00
335ccb32af Update Github issue template (#3234) 2024-01-02 20:42:14 +08:00
e1f2666499 add example for serve file (#3193) 2024-01-02 20:19:40 +08:00
22fcfdf755 add configuration support for logger of grpcx.Server (#3223) 2024-01-02 20:19:05 +08:00
1e21b61a19 fix typo flie -> file (#3228) 2024-01-02 20:17:54 +08:00
6abd8bd864 comments update for package gstr (#3233) 2024-01-02 20:16:51 +08:00
cy
4876ae0e2b fix:gz files are compressed repeatedly every time tick (#3236) 2024-01-02 20:10:57 +08:00
63bdf41d40 fix #3191 (#3194) 2023-12-28 20:18:29 +08:00
984cca8b82 feat: update dependent redoc js for swagger ui (#3217) 2023-12-28 20:13:21 +08:00
9f7ce42c74 enhance #3221 (#3224) 2023-12-28 20:07:07 +08:00
16a43bcb6c Add comment for Format method so that you can know Layout method. (#3230) 2023-12-26 16:19:59 +08:00
85d95e3e27 version v2.6.1 (#3222) 2023-12-21 21:12:51 +08:00
4cce8557e6 fix: #2924 (#3177) 2023-12-21 10:16:14 +08:00
d60d7674d7 fix issues for package contrib/drivers/dm (#3157) 2023-12-20 20:33:58 +08:00
7f9467d1f8 fix: #2938 (#3178) 2023-12-20 20:25:23 +08:00
d08e3ef838 fix: #2689 change sessionId in cookie (#3203) 2023-12-20 20:21:15 +08:00
645c5ff5b5 fix issue #3218 #3204 (#3220) 2023-12-19 21:58:12 +08:00
365 changed files with 8915 additions and 3629 deletions

View File

@ -1,38 +0,0 @@
<!-- Please answer these questions before submitting your issue. Thanks! -->
<!-- 为高效处理您的疑问如果觉得是BUG类问题请您务必提供可复现该问题的最小可运行代码否则issue可能会被延期处理 -->
<!-- 为高效处理您的疑问如果觉得是BUG类问题请您务必提供可复现该问题的最小可运行代码否则issue可能会被延期处理 -->
<!-- 为高效处理您的疑问如果觉得是BUG类问题请您务必提供可复现该问题的最小可运行代码否则issue可能会被延期处理 -->
<!-- 重要的事情说三遍! -->
### 1. What version of `Go` and system type/arch are you using?
<!--
Please paste the output of command `go version` from your terminal.
What expect to see is like: `go 1.12, linux/amd64`
-->
### 2. What version of `GoFrame` are you using?
<!-- You can find the GF version from your `go.mod`, or from the `version.go` in `GF` -->
### 3. Can this issue be re-produced with the latest release?
### 4. What did you do?
<!--
If possible, provide a copy of shortest codes for reproducing the error.
A complete runnable program is best.
-->
### 5. What did you expect to see?
### 6. What did you see instead?

41
.github/ISSUE_TEMPLATE/bug-report.md vendored Normal file
View File

@ -0,0 +1,41 @@
---
name: Bug report
about: Create a report to help us improve
title: ''
labels: bug
assignees: ''
---
<!-- Please answer these questions before submitting your issue. Thanks! -->
<!-- 为高效处理 Bug请您务必提供可复现该问题的最小可运行代码否则 issue 可能会被延期处理! -->
<!-- 为高效处理 Bug请您务必提供可复现该问题的最小可运行代码否则 issue 可能会被延期处理! -->
<!-- 为高效处理 Bug如请您务必提供可复现该问题的最小可运行代码否则 issue 可能会被延期处理! -->
<!-- 重要的事情说三遍! -->
**What version of `Go` and system type/arch are you using?**
<!--
Please paste the output of command `go version` from your terminal.
What expects to see is like: `go 1.12, linux/amd64`
-->
**What version of `GoFrame` are you using?**
<!-- You can find the GF version from your `go.mod`, or from the `version.go` in `GF` -->
**Can this bug be re-produced with the latest release?**
**What did you do?**
<!--
If possible, provide a copy of shortest codes for reproducing the error.
A complete runnable program is best.
-->
**What did you expect to see?**
**What did you see instead?**

View File

@ -0,0 +1,16 @@
---
name: Enhancement request
about: Enhance an idea for this project
title: ''
labels: enhancement
assignees: ''
---
**Package that You wish to enhance**
**Enhancement description**
**Additional**

View File

@ -0,0 +1,19 @@
---
name: Feature request
about: Suggest an idea for this project
title: ''
labels: feature
assignees: ''
---
**Is your feature request related to a problem? Please describe.**
**Describe the solution you'd like**
**Describe alternatives you've considered**
**Additional**

11
.github/ISSUE_TEMPLATE/question.md vendored Normal file
View File

@ -0,0 +1,11 @@
---
name: Question
about: I want to ask a question
title: ''
labels: question
assignees: ''
---
<!-- 请优先仔细阅读文档 -->
**What do you want to ask**

View File

@ -67,9 +67,13 @@ jobs:
- 3306:3306
# PostgreSQL backend server.
# docker run -d --name postgres -p 5432:5432 \
# -e POSTGRES_PASSWORD=12345678 -e POSTGRES_USER=postgres -e POSTGRES_DB=test \
# -v postgres:/Users/john/Temp/postgresql/data loads/postgres:13
# docker run -d --name postgres \
# -p 5432:5432 \
# -e POSTGRES_PASSWORD=12345678 \
# -e POSTGRES_USER=postgres \
# -e POSTGRES_DB=test \
# -v postgres:/Users/john/Temp/postgresql/data \
# loads/postgres:13
postgres:
image: loads/postgres:13
env:
@ -87,7 +91,8 @@ jobs:
--health-retries 5
# MSSQL backend server.
# docker run -d --name mssql -p 1433:1433 \
# docker run \
# -p 1433:1433 \
# -e ACCEPT_EULA=Y \
# -e SA_PASSWORD=LoremIpsum86 \
# -e MSSQL_DB=test \
@ -123,8 +128,13 @@ jobs:
- 9001:9001
# Polaris backend server.
# docker run -d --name polaris -p 8090:8090 -p 8091:8091 -p 8093:8093 -p 9090:9090 -p 9091:9091 loads/polaris-server-standalone:1.11.2
# docker run -d --name polaris -p 8090:8090 -p 8091:8091 -p 8093:8093 -p 9090:9090 -p 9091:9091 loads/polaris-standalone:v1.16.3
# docker run -d --name polaris \
# -p 8090:8090 -p 8091:8091 -p 8093:8093 -p 9090:9090 -p 9091:9091 \
# loads/polaris-server-standalone:1.11.2
#
# docker run -d --name polaris \
# -p 8090:8090 -p 8091:8091 -p 8093:8093 -p 9090:9090 -p 9091:9091 \
# loads/polaris-standalone:v1.16.3
polaris:
image: loads/polaris-standalone:v1.17.2
ports:
@ -134,7 +144,14 @@ jobs:
- 9090:9090
- 9091:9091
# Oracle 11g server
# Oracle 11g server.
# docker run \
# -e ORACLE_ALLOW_REMOTE=true \
# -e ORACLE_SID=XE \
# -e ORACLE_DB_USER_NAME=system \
# -e ORACLE_DB_PASSWORD=oracle \
# -p 1521:1521 \
# loads/oracle-xe-11g-r2:11.2.0
oracle-server:
image: loads/oracle-xe-11g-r2:11.2.0
env:
@ -146,7 +163,7 @@ jobs:
- 1521:1521
# dm8 server
# docker run -d --name dm -p 5236:5236 loads/dm:v8.1.2.128_ent_x86_64_ctm_pack4
# docker run -p 5236:5236 loads/dm:v8.1.2.128_ent_x86_64_ctm_pack4
dm-server:
image: loads/dm:v8.1.2.128_ent_x86_64_ctm_pack4
ports:

View File

@ -1,7 +1,7 @@
on:
push:
branches:
- main
- master
tags:
- "*"
@ -13,8 +13,8 @@ jobs:
steps:
- name: Checkout source code
uses: actions/checkout@v4
- name: Mirror Github to Gitee
uses: Yikun/hub-mirror-action@v1.2
- name: Mirror GitHub to Gitee
uses: Yikun/hub-mirror-action@v1.3
with:
src: github/gogf
dst: gitee/johng

View File

@ -34,7 +34,7 @@ jobs:
- name: Build CLI Binary For All Platform
run: |
cd cmd/gf
gf build main.go -n gf -a all -s all
gf build main.go -n gf -a all -s all -p temp
- name: Move Files Before Release
run: |

View File

@ -7,16 +7,16 @@
[![GoFrame CI](https://github.com/gogf/gf/actions/workflows/ci-main.yml/badge.svg)](https://github.com/gogf/gf/actions/workflows/ci-main.yml)
[![Go Report Card](https://goreportcard.com/badge/github.com/gogf/gf/v2)](https://goreportcard.com/report/github.com/gogf/gf/v2)
[![Code Coverage](https://codecov.io/gh/gogf/gf/branch/master/graph/badge.svg)](https://codecov.io/gh/gogf/gf)
[![Production Ready](https://img.shields.io/badge/production-ready-blue.svg)](https://github.com/gogf/gf)
[![Production Ready](https://img.shields.io/badge/production-ready-blue.svg?style=flat)](https://github.com/gogf/gf)
[![License](https://img.shields.io/github/license/gogf/gf.svg?style=flat)](https://github.com/gogf/gf)
[![Release](https://img.shields.io/github/v/release/gogf/gf)](https://github.com/gogf/gf/releases)
[![GitHub pull requests](https://img.shields.io/github/issues-pr/gogf/gf)](https://github.com/gogf/gf/pulls)
[![GitHub closed pull requests](https://img.shields.io/github/issues-pr-closed/gogf/gf)](https://github.com/gogf/gf/pulls?q=is%3Apr+is%3Aclosed)
[![GitHub issues](https://img.shields.io/github/issues/gogf/gf)](https://github.com/gogf/gf/issues)
[![GitHub closed issues](https://img.shields.io/github/issues-closed/gogf/gf)](https://github.com/gogf/gf/issues?q=is%3Aissue+is%3Aclosed)
![Stars](https://img.shields.io/github/stars/gogf/gf)
![Forks](https://img.shields.io/github/forks/gogf/gf)
[![Release](https://img.shields.io/github/v/release/gogf/gf?style=flat)](https://github.com/gogf/gf/releases)
[![GitHub pull requests](https://img.shields.io/github/issues-pr/gogf/gf?style=flat)](https://github.com/gogf/gf/pulls)
[![GitHub closed pull requests](https://img.shields.io/github/issues-pr-closed/gogf/gf?style=flat)](https://github.com/gogf/gf/pulls?q=is%3Apr+is%3Aclosed)
[![GitHub issues](https://img.shields.io/github/issues/gogf/gf?style=flat)](https://github.com/gogf/gf/issues)
[![GitHub closed issues](https://img.shields.io/github/issues-closed/gogf/gf?style=flat)](https://github.com/gogf/gf/issues?q=is%3Aissue+is%3Aclosed)
![Stars](https://img.shields.io/github/stars/gogf/gf?style=flat)
![Forks](https://img.shields.io/github/forks/gogf/gf?style=flat)
</div>
@ -62,6 +62,7 @@ golang version >= 1.18
# Documentation
- Chinese Official Site(中文官网): [https://goframe.org](https://goframe.org/display/gf)
- Offline Document(中文离线文档): [https://github.com/gogf/goframe.org-pdf](https://github.com/gogf/goframe.org-pdf)
- GoDoc API: [https://pkg.go.dev/github.com/gogf/gf/v2](https://pkg.go.dev/github.com/gogf/gf/v2)
# License

View File

@ -3,13 +3,13 @@ module github.com/gogf/gf/cmd/gf/v2
go 1.18
require (
github.com/gogf/gf/contrib/drivers/clickhouse/v2 v2.6.0
github.com/gogf/gf/contrib/drivers/mssql/v2 v2.6.0
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.6.0
github.com/gogf/gf/contrib/drivers/oracle/v2 v2.6.0
github.com/gogf/gf/contrib/drivers/pgsql/v2 v2.6.0
github.com/gogf/gf/contrib/drivers/sqlite/v2 v2.6.0
github.com/gogf/gf/v2 v2.6.0
github.com/gogf/gf/contrib/drivers/clickhouse/v2 v2.6.3
github.com/gogf/gf/contrib/drivers/mssql/v2 v2.6.3
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.6.3
github.com/gogf/gf/contrib/drivers/oracle/v2 v2.6.3
github.com/gogf/gf/contrib/drivers/pgsql/v2 v2.6.3
github.com/gogf/gf/contrib/drivers/sqlite/v2 v2.6.3
github.com/gogf/gf/v2 v2.6.3
github.com/gogf/selfupdate v0.0.0-20231215043001-5c48c528462f
github.com/olekukonko/tablewriter v0.0.5
golang.org/x/mod v0.9.0

View File

@ -44,8 +44,9 @@ type cBuild struct {
}
const (
cBuildBrief = `cross-building go project for lots of platforms`
cBuildEg = `
cBuildDefaultFile = "main.go"
cBuildBrief = `cross-building go project for lots of platforms`
cBuildEg = `
gf build main.go
gf build main.go --ps public,template
gf build main.go --cgo
@ -123,7 +124,7 @@ type cBuildInput struct {
Arch string `short:"a" name:"arch" brief:"output binary architecture, multiple arch separated with ','"`
System string `short:"s" name:"system" brief:"output binary system, multiple os separated with ','"`
Output string `short:"o" name:"output" brief:"output binary path, used when building single binary file"`
Path string `short:"p" name:"path" brief:"output binary directory path, default is './temp'" d:"./temp"`
Path string `short:"p" name:"path" brief:"output binary directory path, default is '.'" d:"."`
Extra string `short:"e" name:"extra" brief:"extra custom \"go build\" options"`
Mod string `short:"m" name:"mod" brief:"like \"-mod\" option of \"go build\", use \"-m none\" to disable go module"`
Cgo bool `short:"c" name:"cgo" brief:"enable or disable cgo feature, it's disabled in default" orphan:"true"`
@ -152,12 +153,13 @@ func (c cBuild) Index(ctx context.Context, in cBuildInput) (out *cBuildOutput, e
var (
parser = gcmd.ParserFromCtx(ctx)
file = parser.GetArg(2).String()
file = in.File
)
if len(file) < 1 {
if file == "" {
file = parser.GetArg(2).String()
// Check and use the main.go file.
if gfile.Exists("main.go") {
file = "main.go"
if gfile.Exists(cBuildDefaultFile) {
file = cBuildDefaultFile
} else {
mlog.Fatal("build file path is empty or main.go not found in current working directory")
}
@ -239,13 +241,7 @@ func (c cBuild) Index(ctx context.Context, in cBuildInput) (out *cBuildOutput, e
} else {
genv.MustSet("CGO_ENABLED", "0")
}
var (
cmd = ""
ext = ""
)
for system, item := range platformMap {
cmd = ""
ext = ""
if len(customSystems) > 0 && customSystems[0] != "all" && !gstr.InArray(customSystems, system) {
continue
}
@ -258,58 +254,22 @@ func (c cBuild) Index(ctx context.Context, in cBuildInput) (out *cBuildOutput, e
// For example:
// `gf build`
// `gf build -o main.exe`
if runtime.GOOS == "windows" {
ext = ".exe"
}
var outputPath string
if len(in.Output) > 0 {
outputPath = "-o " + in.Output
} else {
outputPath = "-o " + in.Name + ext
}
cmd = fmt.Sprintf(
`go build %s -ldflags "%s" %s %s`,
outputPath, ldFlags, in.Extra, file,
c.doBinaryBuild(
ctx, file,
in.Output, in.Path,
runtime.GOOS, runtime.GOARCH, in.Name, ldFlags, in.Extra,
in.ExitWhenError,
true,
)
} else {
// Cross-building, output the compiled binary to specified path.
if system == "windows" {
ext = ".exe"
}
genv.MustSet("GOOS", system)
genv.MustSet("GOARCH", arch)
var outputPath string
if len(in.Output) > 0 {
outputPath = "-o " + in.Output
} else {
outputPath = fmt.Sprintf(
"-o %s/%s/%s%s",
in.Path, system+"_"+arch, in.Name, ext,
)
}
cmd = fmt.Sprintf(
`go build %s -ldflags "%s" %s%s`,
outputPath, ldFlags, in.Extra, file,
c.doBinaryBuild(
ctx, file,
in.Output, in.Path,
system, arch, in.Name, ldFlags, in.Extra,
in.ExitWhenError,
false,
)
}
mlog.Debug(fmt.Sprintf("build for GOOS=%s GOARCH=%s", system, arch))
mlog.Debug(cmd)
// It's not necessary printing the complete command string.
cmdShow, _ := gregex.ReplaceString(`\s+(-ldflags ".+?")\s+`, " ", cmd)
mlog.Print(cmdShow)
if result, err := gproc.ShellExec(ctx, cmd); err != nil {
mlog.Printf(
"failed to build, os:%s, arch:%s, error:\n%s\n\n%s\n",
system, arch, gstr.Trim(result),
`you may use command option "--debug" to enable debug info and check the details`,
)
if in.ExitWhenError {
os.Exit(1)
}
} else {
mlog.Debug(gstr.Trim(result))
}
// single binary building.
if len(customSystems) == 0 && len(customArches) == 0 {
goto buildDone
@ -322,6 +282,68 @@ buildDone:
return
}
func (c cBuild) doBinaryBuild(
ctx context.Context,
filePath string,
outputPath, dirPath string,
system, arch, name, ldFlags, extra string,
exitWhenError bool,
singleBuild bool,
) {
var (
cmd string
ext string
)
// Cross-building, output the compiled binary to specified path.
if system == "windows" {
ext = ".exe"
}
genv.MustSet("GOOS", system)
genv.MustSet("GOARCH", arch)
if outputPath != "" {
outputPath = "-o " + outputPath
} else {
if dirPath == "" {
dirPath = "."
} else {
dirPath = gstr.TrimRight(dirPath, "/")
}
if singleBuild {
outputPath = fmt.Sprintf(
"-o %s/%s%s",
dirPath, name, ext,
)
} else {
outputPath = fmt.Sprintf(
"-o %s/%s/%s%s",
dirPath, system+"_"+arch, name, ext,
)
}
}
cmd = fmt.Sprintf(
`go build %s -ldflags "%s" %s%s`,
outputPath, ldFlags, extra, filePath,
)
mlog.Debug(fmt.Sprintf("build for GOOS=%s GOARCH=%s", system, arch))
mlog.Debug(cmd)
// It's not necessary printing the complete command string, filtering ldFlags.
cmdShow, _ := gregex.ReplaceString(`\s+(-ldflags ".+?")\s+`, " ", cmd)
mlog.Print(cmdShow)
if result, err := gproc.ShellExec(ctx, cmd); err != nil {
mlog.Printf(
"failed to build, os:%s, arch:%s, error:\n%s\n\n%s\n",
system, arch, gstr.Trim(result),
`you may use command option "--debug" to enable debug info and check the details`,
)
if exitWhenError {
os.Exit(1)
}
} else {
mlog.Debug(gstr.Trim(result))
}
}
// getBuildInVarStr retrieves and returns the custom build-in variables in configuration
// file as json.
func (c cBuild) getBuildInVarStr(ctx context.Context, in cBuildInput) string {

View File

@ -6,6 +6,33 @@
package cmd
import "context"
import (
"context"
"fmt"
var ctx = context.Background()
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/test/gtest"
)
var (
ctx = context.Background()
testDB gdb.DB
link = "mysql:root:12345678@tcp(127.0.0.1:3306)/test?loc=Local&parseTime=true"
)
func init() {
var err error
testDB, err = gdb.New(gdb.ConfigNode{
Link: link,
})
if err != nil {
panic(err)
}
}
func dropTableWithDb(db gdb.DB, table string) {
dropTableStmt := fmt.Sprintf("DROP TABLE IF EXISTS `%s`", table)
if _, err := db.Exec(ctx, dropTableStmt); err != nil {
gtest.Error(err)
}
}

View File

@ -0,0 +1,151 @@
// 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 cmd
import (
"testing"
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/os/gproc"
"github.com/gogf/gf/v2/test/gtest"
"github.com/gogf/gf/v2/text/gstr"
)
func Test_Build_Single(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
buildPath = gtest.DataPath(`build`, `single`)
pwd = gfile.Pwd()
binaryName = `t.test`
binaryPath = gtest.DataPath(`build`, `single`, binaryName)
f = cBuild{}
)
defer gfile.Chdir(pwd)
defer gfile.Remove(binaryPath)
err := gfile.Chdir(buildPath)
t.AssertNil(err)
t.Assert(gfile.Exists(binaryPath), false)
_, err = f.Index(ctx, cBuildInput{
File: cBuildDefaultFile,
Name: binaryName,
})
t.AssertNil(err)
t.Assert(gfile.Exists(binaryPath), true)
})
}
func Test_Build_Single_Output(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
buildPath = gtest.DataPath(`build`, `single`)
pwd = gfile.Pwd()
binaryName = `tt`
binaryDirPath = gtest.DataPath(`build`, `single`, `tt`)
binaryPath = gtest.DataPath(`build`, `single`, `tt`, binaryName)
f = cBuild{}
)
defer gfile.Chdir(pwd)
defer gfile.Remove(binaryDirPath)
err := gfile.Chdir(buildPath)
t.AssertNil(err)
t.Assert(gfile.Exists(binaryPath), false)
_, err = f.Index(ctx, cBuildInput{
Output: "./tt/tt",
})
t.AssertNil(err)
t.Assert(gfile.Exists(binaryPath), true)
})
}
func Test_Build_Single_Path(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
buildPath = gtest.DataPath(`build`, `single`)
pwd = gfile.Pwd()
dirName = "ttt"
binaryName = `main`
binaryDirPath = gtest.DataPath(`build`, `single`, dirName)
binaryPath = gtest.DataPath(`build`, `single`, dirName, binaryName)
f = cBuild{}
)
defer gfile.Chdir(pwd)
defer gfile.Remove(binaryDirPath)
err := gfile.Chdir(buildPath)
t.AssertNil(err)
t.Assert(gfile.Exists(binaryPath), false)
_, err = f.Index(ctx, cBuildInput{
Path: "ttt",
})
t.AssertNil(err)
t.Assert(gfile.Exists(binaryPath), true)
})
}
func Test_Build_Single_VarMap(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
buildPath = gtest.DataPath(`build`, `varmap`)
pwd = gfile.Pwd()
binaryName = `main`
binaryPath = gtest.DataPath(`build`, `varmap`, binaryName)
f = cBuild{}
)
defer gfile.Chdir(pwd)
defer gfile.Remove(binaryPath)
err := gfile.Chdir(buildPath)
t.AssertNil(err)
t.Assert(gfile.Exists(binaryPath), false)
_, err = f.Index(ctx, cBuildInput{
VarMap: map[string]interface{}{
"a": "1",
"b": "2",
},
})
t.AssertNil(err)
t.Assert(gfile.Exists(binaryPath), true)
result, err := gproc.ShellExec(ctx, binaryPath)
t.AssertNil(err)
t.Assert(gstr.Contains(result, `a: 1`), true)
t.Assert(gstr.Contains(result, `b: 2`), true)
})
}
func Test_Build_Multiple(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
buildPath = gtest.DataPath(`build`, `multiple`)
pwd = gfile.Pwd()
binaryDirPath = gtest.DataPath(`build`, `multiple`, `temp`)
binaryPathLinux = gtest.DataPath(`build`, `multiple`, `temp`, `v1.1`, `linux_amd64`, `ttt`)
binaryPathWindows = gtest.DataPath(`build`, `multiple`, `temp`, `v1.1`, `windows_amd64`, `ttt.exe`)
f = cBuild{}
)
defer gfile.Chdir(pwd)
defer gfile.Remove(binaryDirPath)
err := gfile.Chdir(buildPath)
t.AssertNil(err)
t.Assert(gfile.Exists(binaryPathLinux), false)
t.Assert(gfile.Exists(binaryPathWindows), false)
_, err = f.Index(ctx, cBuildInput{
File: "multiple.go",
Name: "ttt",
Version: "v1.1",
Arch: "amd64",
System: "linux, windows",
Path: "temp",
})
t.AssertNil(err)
t.Assert(gfile.Exists(binaryPathLinux), true)
t.Assert(gfile.Exists(binaryPathWindows), true)
})
}

View File

@ -7,13 +7,14 @@
package cmd
import (
"path/filepath"
"testing"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/genctrl"
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/test/gtest"
"github.com/gogf/gf/v2/util/guid"
"github.com/gogf/gf/v2/util/gutil"
"path/filepath"
"testing"
)
func Test_Gen_Ctrl_Default(t *testing.T) {

View File

@ -20,22 +20,11 @@ import (
"github.com/gogf/gf/v2/util/gutil"
)
func dropTableWithDb(db gdb.DB, table string) {
dropTableStmt := fmt.Sprintf("DROP TABLE IF EXISTS `%s`", table)
if _, err := db.Exec(ctx, dropTableStmt); err != nil {
gtest.Error(err)
}
}
func Test_Gen_Dao_Default(t *testing.T) {
link := "mysql:root:12345678@tcp(127.0.0.1:3306)/test?loc=Local&parseTime=true"
db, err := gdb.New(gdb.ConfigNode{
Link: link,
})
gtest.AssertNil(err)
gtest.C(t, func(t *gtest.T) {
var (
err error
db = testDB
table = "table_user"
sqlContent = fmt.Sprintf(
gtest.DataContent(`gendao`, `user.tpl.sql`),
@ -123,14 +112,10 @@ func Test_Gen_Dao_Default(t *testing.T) {
}
func Test_Gen_Dao_TypeMapping(t *testing.T) {
link := "mysql:root:12345678@tcp(127.0.0.1:3306)/test?loc=Local&parseTime=true"
db, err := gdb.New(gdb.ConfigNode{
Link: link,
})
gtest.AssertNil(err)
gtest.C(t, func(t *gtest.T) {
var (
err error
db = testDB
table = "table_user"
sqlContent = fmt.Sprintf(
gtest.DataContent(`gendao`, `user.tpl.sql`),
@ -225,3 +210,186 @@ func Test_Gen_Dao_TypeMapping(t *testing.T) {
}
})
}
func execSqlFile(db gdb.DB, filePath string, args ...any) error {
sqlContent := fmt.Sprintf(
gfile.GetContents(filePath),
args...,
)
array := gstr.SplitAndTrim(sqlContent, ";")
for _, v := range array {
if _, err := db.Exec(ctx, v); err != nil {
return err
}
}
return nil
}
func Test_Gen_Dao_Issue2572(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
err error
db = testDB
table1 = "user1"
table2 = "user2"
issueDirPath = gtest.DataPath(`issue`, `2572`)
)
t.AssertNil(execSqlFile(db, gtest.DataPath(`issue`, `2572`, `sql1.sql`)))
t.AssertNil(execSqlFile(db, gtest.DataPath(`issue`, `2572`, `sql2.sql`)))
defer dropTableWithDb(db, table1)
defer dropTableWithDb(db, table2)
var (
path = gfile.Temp(guid.S())
group = "test"
in = gendao.CGenDaoInput{
Path: path,
Link: link,
Tables: "",
TablesEx: "",
Group: group,
Prefix: "",
RemovePrefix: "",
JsonCase: "SnakeScreaming",
ImportPrefix: "",
DaoPath: "",
DoPath: "",
EntityPath: "",
TplDaoIndexPath: "",
TplDaoInternalPath: "",
TplDaoDoPath: "",
TplDaoEntityPath: "",
StdTime: false,
WithTime: false,
GJsonSupport: false,
OverwriteDao: false,
DescriptionTag: false,
NoJsonTag: false,
NoModelComment: false,
Clear: false,
TypeMapping: nil,
}
)
err = gutil.FillStructWithDefault(&in)
t.AssertNil(err)
err = gfile.Copy(issueDirPath, path)
t.AssertNil(err)
defer gfile.Remove(path)
pwd := gfile.Pwd()
err = gfile.Chdir(path)
t.AssertNil(err)
defer gfile.Chdir(pwd)
_, err = gendao.CGenDao{}.Dao(ctx, in)
t.AssertNil(err)
generatedFiles, err := gfile.ScanDir(path, "*.go", true)
t.AssertNil(err)
t.Assert(len(generatedFiles), 8)
for i, generatedFile := range generatedFiles {
generatedFiles[i] = gstr.TrimLeftStr(generatedFile, path)
}
t.Assert(gstr.InArray(generatedFiles, "/dao/internal/user_1.go"), true)
t.Assert(gstr.InArray(generatedFiles, "/dao/internal/user_2.go"), true)
t.Assert(gstr.InArray(generatedFiles, "/dao/user_1.go"), true)
t.Assert(gstr.InArray(generatedFiles, "/dao/user_2.go"), true)
t.Assert(gstr.InArray(generatedFiles, "/model/do/user_1.go"), true)
t.Assert(gstr.InArray(generatedFiles, "/model/do/user_2.go"), true)
t.Assert(gstr.InArray(generatedFiles, "/model/entity/user_1.go"), true)
t.Assert(gstr.InArray(generatedFiles, "/model/entity/user_2.go"), true)
})
}
func Test_Gen_Dao_Issue2616(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
err error
db = testDB
table1 = "user1"
table2 = "user2"
issueDirPath = gtest.DataPath(`issue`, `2616`)
)
t.AssertNil(execSqlFile(db, gtest.DataPath(`issue`, `2616`, `sql1.sql`)))
t.AssertNil(execSqlFile(db, gtest.DataPath(`issue`, `2616`, `sql2.sql`)))
defer dropTableWithDb(db, table1)
defer dropTableWithDb(db, table2)
var (
path = gfile.Temp(guid.S())
group = "test"
in = gendao.CGenDaoInput{
Path: path,
Link: link,
Tables: "",
TablesEx: "",
Group: group,
Prefix: "",
RemovePrefix: "",
JsonCase: "SnakeScreaming",
ImportPrefix: "",
DaoPath: "",
DoPath: "",
EntityPath: "",
TplDaoIndexPath: "",
TplDaoInternalPath: "",
TplDaoDoPath: "",
TplDaoEntityPath: "",
StdTime: false,
WithTime: false,
GJsonSupport: false,
OverwriteDao: false,
DescriptionTag: false,
NoJsonTag: false,
NoModelComment: false,
Clear: false,
TypeMapping: nil,
}
)
err = gutil.FillStructWithDefault(&in)
t.AssertNil(err)
err = gfile.Copy(issueDirPath, path)
t.AssertNil(err)
defer gfile.Remove(path)
pwd := gfile.Pwd()
err = gfile.Chdir(path)
t.AssertNil(err)
defer gfile.Chdir(pwd)
_, err = gendao.CGenDao{}.Dao(ctx, in)
t.AssertNil(err)
generatedFiles, err := gfile.ScanDir(path, "*.go", true)
t.AssertNil(err)
t.Assert(len(generatedFiles), 8)
for i, generatedFile := range generatedFiles {
generatedFiles[i] = gstr.TrimLeftStr(generatedFile, path)
}
t.Assert(gstr.InArray(generatedFiles, "/dao/internal/user_1.go"), true)
t.Assert(gstr.InArray(generatedFiles, "/dao/internal/user_2.go"), true)
t.Assert(gstr.InArray(generatedFiles, "/dao/user_1.go"), true)
t.Assert(gstr.InArray(generatedFiles, "/dao/user_2.go"), true)
t.Assert(gstr.InArray(generatedFiles, "/model/do/user_1.go"), true)
t.Assert(gstr.InArray(generatedFiles, "/model/do/user_2.go"), true)
t.Assert(gstr.InArray(generatedFiles, "/model/entity/user_1.go"), true)
t.Assert(gstr.InArray(generatedFiles, "/model/entity/user_2.go"), true)
// Key string to check if overwrite the dao files.
// dao user1 is not be overwritten as configured in config.yaml.
// dao user2 is to be overwritten as configured in config.yaml.
var (
keyStr = `// I am not overwritten.`
daoUser1Content = gfile.GetContents(path + "/dao/user_1.go")
daoUser2Content = gfile.GetContents(path + "/dao/user_2.go")
)
t.Assert(gstr.Contains(daoUser1Content, keyStr), true)
t.Assert(gstr.Contains(daoUser2Content, keyStr), false)
})
}

View File

@ -0,0 +1,70 @@
// 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 cmd
import (
"fmt"
"os"
"path/filepath"
"testing"
"github.com/gogf/gf/v2/os/gcmd"
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/test/gtest"
"github.com/gogf/gf/v2/text/gstr"
"github.com/gogf/gf/v2/util/guid"
)
func Test_Gen_Pbentity_NameCase(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
err error
db = testDB
table = "table_user"
sqlContent = fmt.Sprintf(
gtest.DataContent(`genpbentity`, `user.tpl.sql`),
table,
)
)
dropTableWithDb(db, table)
array := gstr.SplitAndTrim(sqlContent, ";")
for _, v := range array {
if _, err = db.Exec(ctx, v); err != nil {
t.AssertNil(err)
}
}
defer dropTableWithDb(db, table)
var path = gfile.Temp(guid.S())
err = gfile.Mkdir(path)
t.AssertNil(err)
defer gfile.Remove(path)
root, err := gcmd.NewFromObject(GF)
t.AssertNil(err)
err = root.AddObject(
Gen,
)
t.AssertNil(err)
os.Args = []string{"gf", "gen", "pbentity", "-l", link, "-p", path, "-package=unittest", "-nameCase=SnakeScreaming"}
err = root.RunWithError(ctx)
t.AssertNil(err)
files := []string{
filepath.FromSlash(path + "/table_user.proto"),
}
testPath := gtest.DataPath("genpbentity", "generated_user")
expectFiles := []string{
filepath.FromSlash(testPath + "/table_user.proto"),
}
// check files content
for i := range files {
t.Assert(gfile.GetContents(files[i]), gfile.GetContents(expectFiles[i]))
}
})
}

View File

@ -7,13 +7,14 @@
package cmd
import (
"path/filepath"
"testing"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/genservice"
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/test/gtest"
"github.com/gogf/gf/v2/util/guid"
"github.com/gogf/gf/v2/util/gutil"
"path/filepath"
"testing"
)
func Test_Gen_Service_Default(t *testing.T) {

View File

@ -201,7 +201,8 @@ type (
NoModelComment bool `name:"noModelComment" short:"m" brief:"{CGenDaoBriefNoModelComment}" orphan:"true"`
Clear bool `name:"clear" short:"a" brief:"{CGenDaoBriefClear}" orphan:"true"`
TypeMapping map[DBFieldTypeName]CustomAttributeType `name:"typeMapping" short:"y" brief:"{CGenDaoBriefTypeMapping}" orphan:"true"`
TypeMapping map[DBFieldTypeName]CustomAttributeType `name:"typeMapping" short:"y" brief:"{CGenDaoBriefTypeMapping}" orphan:"true"`
generatedFilePaths *CGenDaoInternalGeneratedFilePaths
}
CGenDaoOutput struct{}
@ -212,6 +213,13 @@ type (
NewTableNames []string
}
CGenDaoInternalGeneratedFilePaths struct {
DaoFilePaths []string
DaoInternalFilePaths []string
DoFilePaths []string
EntityFilePaths []string
}
DBFieldTypeName = string
CustomAttributeType struct {
Type string `brief:"custom attribute type name"`
@ -220,6 +228,12 @@ type (
)
func (c CGenDao) Dao(ctx context.Context, in CGenDaoInput) (out *CGenDaoOutput, err error) {
in.generatedFilePaths = &CGenDaoInternalGeneratedFilePaths{
DaoFilePaths: make([]string, 0),
DaoInternalFilePaths: make([]string, 0),
DoFilePaths: make([]string, 0),
EntityFilePaths: make([]string, 0),
}
if g.Cfg().Available(ctx) {
v := g.Cfg().MustGet(ctx, CGenDaoConfig)
if v.IsSlice() {
@ -333,6 +347,10 @@ func doGenDaoForArray(ctx context.Context, index int, in CGenDaoInput) {
TableNames: tableNames,
NewTableNames: newTableNames,
})
if in.Clear {
doClear(ctx, in)
}
}
func getImportPartContent(ctx context.Context, source string, isDo bool, appendImports []string) string {

View File

@ -10,19 +10,24 @@ import (
"context"
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/text/gstr"
"github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog"
"github.com/gogf/gf/cmd/gf/v2/internal/utility/utils"
)
func doClear(ctx context.Context, dirPath string, force bool) {
files, err := gfile.ScanDirFile(dirPath, "*.go", true)
func doClear(ctx context.Context, in CGenDaoInput) {
filePaths, err := gfile.ScanDirFile(in.Path, "*.go", true)
if err != nil {
mlog.Fatal(err)
}
for _, file := range files {
if force || utils.IsFileDoNotEdit(file) {
if err = gfile.Remove(file); err != nil {
var allGeneratedFilePaths = make([]string, 0)
allGeneratedFilePaths = append(allGeneratedFilePaths, in.generatedFilePaths.DaoFilePaths...)
allGeneratedFilePaths = append(allGeneratedFilePaths, in.generatedFilePaths.DaoInternalFilePaths...)
allGeneratedFilePaths = append(allGeneratedFilePaths, in.generatedFilePaths.EntityFilePaths...)
allGeneratedFilePaths = append(allGeneratedFilePaths, in.generatedFilePaths.DoFilePaths...)
for _, filePath := range filePaths {
if !gstr.InArray(allGeneratedFilePaths, filePath) {
if err = gfile.Remove(filePath); err != nil {
mlog.Print(err)
}
}

View File

@ -30,9 +30,6 @@ func generateDao(ctx context.Context, in CGenDaoInternalInput) {
dirPathDao = gfile.Join(in.Path, in.DaoPath)
dirPathDaoInternal = gfile.Join(dirPathDao, "internal")
)
if in.Clear {
doClear(ctx, dirPathDao, true)
}
for i := 0; i < len(in.TableNames); i++ {
generateDaoSingle(ctx, generateDaoSingleInput{
CGenDaoInternalInput: in,
@ -108,6 +105,11 @@ type generateDaoIndexInput struct {
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.
in.generatedFilePaths.DaoFilePaths = append(
in.generatedFilePaths.DaoFilePaths,
path,
)
if in.OverwriteDao || !gfile.Exists(path) {
indexContent := gstr.ReplaceByMap(
getTemplateFromPathOrDefault(in.TplDaoIndexPath, consts.TemplateGenDaoIndexContent),
@ -151,6 +153,10 @@ func generateDaoInternal(in generateDaoInternalInput) {
tplVarColumnNames: gstr.Trim(generateColumnNamesForDao(in.FieldMap, removeFieldPrefixArray)),
})
modelContent = replaceDefaultVar(in.CGenDaoInternalInput, modelContent)
in.generatedFilePaths.DaoInternalFilePaths = append(
in.generatedFilePaths.DaoInternalFilePaths,
path,
)
if err := gfile.PutContents(path, strings.TrimSpace(modelContent)); err != nil {
mlog.Fatalf("writing content to '%s' failed: %v", path, err)
} else {

View File

@ -24,9 +24,6 @@ import (
func generateDo(ctx context.Context, in CGenDaoInternalInput) {
var dirPathDo = filepath.FromSlash(gfile.Join(in.Path, in.DoPath))
if in.Clear {
doClear(ctx, dirPathDo, false)
}
in.NoJsonTag = true
in.DescriptionTag = false
in.NoModelComment = false
@ -66,6 +63,10 @@ func generateDo(ctx context.Context, in CGenDaoInternalInput) {
gstr.CaseCamel(newTableName),
structDefinition,
)
in.generatedFilePaths.DoFilePaths = append(
in.generatedFilePaths.DoFilePaths,
doFilePath,
)
err = gfile.PutContents(doFilePath, strings.TrimSpace(modelContent))
if err != nil {
mlog.Fatalf(`writing content to "%s" failed: %v`, doFilePath, err)

View File

@ -22,9 +22,6 @@ import (
func generateEntity(ctx context.Context, in CGenDaoInternalInput) {
var dirPathEntity = gfile.Join(in.Path, in.EntityPath)
if in.Clear {
doClear(ctx, dirPathEntity, false)
}
// Model content.
for i, tableName := range in.TableNames {
fieldMap, err := in.DB.TableFields(ctx, tableName)
@ -51,7 +48,10 @@ func generateEntity(ctx context.Context, in CGenDaoInternalInput) {
appendImports,
)
)
in.generatedFilePaths.EntityFilePaths = append(
in.generatedFilePaths.EntityFilePaths,
entityFilePath,
)
err = gfile.PutContents(entityFilePath, strings.TrimSpace(entityContent))
if err != nil {
mlog.Fatalf("writing content to '%s' failed: %v", entityFilePath, err)

View File

@ -40,7 +40,7 @@ type (
RemovePrefix string `name:"removePrefix" short:"r" brief:"{CGenPbEntityBriefRemovePrefix}"`
RemoveFieldPrefix string `name:"removeFieldPrefix" short:"rf" brief:"{CGenPbEntityBriefRemoveFieldPrefix}"`
NameCase string `name:"nameCase" short:"n" brief:"{CGenPbEntityBriefNameCase}" d:"Camel"`
JsonCase string `name:"jsonCase" short:"j" brief:"{CGenPbEntityBriefJsonCase}" d:"CamelLower"`
JsonCase string `name:"jsonCase" short:"j" brief:"{CGenPbEntityBriefJsonCase}" d:"none"`
Option string `name:"option" short:"o" brief:"{CGenPbEntityBriefOption}"`
}
CGenPbEntityOutput struct{}
@ -342,6 +342,7 @@ func generateMessageFieldForPbEntity(index int, field *gdb.TableField, in CGenPb
comment = gstr.Replace(comment, `\n`, " ")
comment, _ = gregex.ReplaceString(`\s{2,}`, ` `, comment)
if jsonTagName := formatCase(field.Name, in.JsonCase); jsonTagName != "" {
jsonTagStr = fmt.Sprintf(`[json_name = "%s"]`, jsonTagName)
// beautiful indent.
if index < 10 {
// 3 spaces
@ -378,32 +379,10 @@ func getTplPbEntityContent(tplEntityPath string) string {
// formatCase call gstr.Case* function to convert the s to specified case.
func formatCase(str, caseStr string) string {
switch gstr.ToLower(caseStr) {
case gstr.ToLower("Camel"):
return gstr.CaseCamel(str)
case gstr.ToLower("CamelLower"):
return gstr.CaseCamelLower(str)
case gstr.ToLower("Kebab"):
return gstr.CaseKebab(str)
case gstr.ToLower("KebabScreaming"):
return gstr.CaseKebabScreaming(str)
case gstr.ToLower("Snake"):
return gstr.CaseSnake(str)
case gstr.ToLower("SnakeFirstUpper"):
return gstr.CaseSnakeFirstUpper(str)
case gstr.ToLower("SnakeScreaming"):
return gstr.CaseSnakeScreaming(str)
case "none":
if caseStr == "none" {
return ""
}
return str
return gstr.CaseConvert(str, gstr.CaseTypeMatch(caseStr))
}
func sortFieldKeyForPbEntity(fieldMap map[string]*gdb.TableField) []string {

View File

@ -174,7 +174,7 @@ func (c CGenService) Service(ctx context.Context, in CGenServiceInput) (out *CGe
// Parse single logic package folder.
var (
// StructName => FunctionDefinitions
srcPkgInterfaceMap = make(map[string]*garray.StrArray)
srcPkgInterfaceMap = gmap.NewListMap()
srcImportedPackages = garray.NewSortedStrArray().SetUnique(true)
importAliasToPathMap = gmap.NewStrStrMap() // for conflict imports check. alias => import path(with `"`)
importPathToAliasMap = gmap.NewStrStrMap() // for conflict imports check. import path(with `"`) => alias

View File

@ -12,6 +12,7 @@ import (
"go/token"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/gmap"
"github.com/gogf/gf/v2/text/gregex"
"github.com/gogf/gf/v2/text/gstr"
)
@ -99,10 +100,9 @@ func (c CGenService) calculateCodeCommented(in CGenServiceInput, fileContent str
}
func (c CGenService) calculateInterfaceFunctions(
in CGenServiceInput, fileContent string, srcPkgInterfaceMap map[string]*garray.StrArray,
in CGenServiceInput, fileContent string, srcPkgInterfaceMap *gmap.ListMap,
) (err error) {
var (
ok bool
matches [][]string
srcPkgInterfaceFuncArray *garray.StrArray
)
@ -142,9 +142,11 @@ func (c CGenService) calculateInterfaceFunctions(
continue
}
structName = gstr.CaseCamel(structMatch[1])
if srcPkgInterfaceFuncArray, ok = srcPkgInterfaceMap[structName]; !ok {
srcPkgInterfaceMap[structName] = garray.NewStrArray()
srcPkgInterfaceFuncArray = srcPkgInterfaceMap[structName]
if !srcPkgInterfaceMap.Contains(structName) {
srcPkgInterfaceFuncArray = garray.NewStrArray()
srcPkgInterfaceMap.Set(structName, srcPkgInterfaceFuncArray)
} else {
srcPkgInterfaceFuncArray = srcPkgInterfaceMap.Get(structName).(*garray.StrArray)
}
srcPkgInterfaceFuncArray.Append(functionHead)
}
@ -165,8 +167,8 @@ func (c CGenService) calculateInterfaceFunctions(
continue
}
structName = gstr.CaseCamel(structMatch[1])
if srcPkgInterfaceFuncArray, ok = srcPkgInterfaceMap[structName]; !ok {
srcPkgInterfaceMap[structName] = garray.NewStrArray()
if !srcPkgInterfaceMap.Contains(structName) {
srcPkgInterfaceMap.Set(structName, garray.NewStrArray())
}
}
return nil

View File

@ -10,6 +10,7 @@ import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/gmap"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/text/gregex"
@ -23,7 +24,7 @@ import (
type generateServiceFilesInput struct {
CGenServiceInput
DstFilePath string // Absolute file path for generated service go file.
SrcStructFunctions map[string]*garray.StrArray
SrcStructFunctions *gmap.ListMap
SrcImportedPackages []string
SrcPackageName string
DstPackageName string
@ -46,7 +47,8 @@ func (c CGenService) generateServiceFile(in generateServiceFilesInput) (ok bool,
// Type definitions.
generatedContent += "type("
generatedContent += "\n"
for structName, funcArray := range in.SrcStructFunctions {
in.SrcStructFunctions.Iterator(func(key, value interface{}) bool {
structName, funcArray := key.(string), value.(*garray.StrArray)
allFuncArray.Append(funcArray.Slice()...)
// Add comments to a method.
for index, funcName := range funcArray.Slice() {
@ -60,7 +62,8 @@ func (c CGenService) generateServiceFile(in generateServiceFilesInput) (ok bool,
"{FuncDefinition}": funcArray.Join("\n\t"),
}))
generatedContent += "\n"
}
return true
})
generatedContent += ")"
generatedContent += "\n"
@ -70,17 +73,19 @@ func (c CGenService) generateServiceFile(in generateServiceFilesInput) (ok bool,
generatingInterfaceCheck string
)
// Variable definitions.
for structName := range in.SrcStructFunctions {
in.SrcStructFunctions.Iterator(func(key, value interface{}) bool {
structName := key.(string)
generatingInterfaceCheck = fmt.Sprintf(`[^\w\d]+%s.I%s[^\w\d]`, in.DstPackageName, structName)
if gregex.IsMatchString(generatingInterfaceCheck, generatedContent) {
continue
return true
}
variableContent += gstr.Trim(gstr.ReplaceByMap(consts.TemplateGenServiceContentVariable, g.MapStrStr{
"{StructName}": structName,
"{InterfaceName}": "I" + structName,
}))
variableContent += "\n"
}
return true
})
if variableContent != "" {
generatedContent += "var("
generatedContent += "\n"
@ -89,17 +94,19 @@ func (c CGenService) generateServiceFile(in generateServiceFilesInput) (ok bool,
generatedContent += "\n"
}
// Variable register function definitions.
for structName := range in.SrcStructFunctions {
in.SrcStructFunctions.Iterator(func(key, value interface{}) bool {
structName := key.(string)
generatingInterfaceCheck = fmt.Sprintf(`[^\w\d]+%s.I%s[^\w\d]`, in.DstPackageName, structName)
if gregex.IsMatchString(generatingInterfaceCheck, generatedContent) {
continue
return true
}
generatedContent += gstr.Trim(gstr.ReplaceByMap(consts.TemplateGenServiceContentRegister, g.MapStrStr{
"{StructName}": structName,
"{InterfaceName}": "I" + structName,
}))
generatedContent += "\n\n"
}
return true
})
// Replace empty braces that have new line.
generatedContent, _ = gregex.ReplaceString(`{[\s\t]+}`, `{}`, generatedContent)

View File

@ -0,0 +1,5 @@
package main
func main() {
}

View File

@ -0,0 +1,5 @@
package main
func main() {
}

View File

@ -0,0 +1,12 @@
module github.com/gogf/gf/cmd/gf/cmd/gf/testdata/vardump/v2
go 1.18
require github.com/gogf/gf/v2 v2.6.1
require (
go.opentelemetry.io/otel v1.14.0 // indirect
go.opentelemetry.io/otel/trace v1.14.0 // indirect
)
replace github.com/gogf/gf/v2 => ../../../../../../../

View File

@ -0,0 +1,27 @@
github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0=
github.com/clbanning/mxj/v2 v2.7.0 h1:WA/La7UGCanFe5NpHF0Q3DNtnCsVoxbPKuyBNHWRyME=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/grokify/html-strip-tags-go v0.0.1 h1:0fThFwLbW7P/kOiTBs03FsJSV9RM2M/Q/MOnCQxKMo0=
github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
go.opentelemetry.io/otel v1.14.0 h1:/79Huy8wbf5DnIPhemGB+zEPVwnN6fuQybr/SRXa6hM=
go.opentelemetry.io/otel v1.14.0/go.mod h1:o4buv+dJzx8rohcUeRmWUZhqupFvzWis188WlggnNeU=
go.opentelemetry.io/otel/sdk v1.14.0 h1:PDCppFRDq8A1jL9v6KMI6dYesaq+DFcDZvjsoGvxGzY=
go.opentelemetry.io/otel/trace v1.14.0 h1:wp2Mmvj41tDsyAJXiWDWpfNsOiIyd38fy85pyKcFq/M=
go.opentelemetry.io/otel/trace v1.14.0/go.mod h1:8avnQLK+CG77yNLUae4ea2JDQ6iT+gozhnZjy/rw9G8=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

View File

@ -0,0 +1,13 @@
package main
import (
"fmt"
"github.com/gogf/gf/v2/os/gbuild"
)
func main() {
for k, v := range gbuild.Data() {
fmt.Printf("%s: %v\n", k, v)
}
}

View File

@ -0,0 +1,21 @@
// ==========================================================================
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
// ==========================================================================
syntax = "proto3";
package unittest;
option go_package = "unittest";
import "google/protobuf/timestamp.proto";
message TableUser {
uint32 ID = 1; // User ID
string PASSPORT = 2; // User Passport
string PASSWORD = 3; // User Password
string NICKNAME = 4; // User Nickname
string SCORE = 5; // Total score amount.
google.protobuf.Timestamp CREATE_AT = 6; // Created Time
google.protobuf.Timestamp UPDATE_AT = 7; // Updated Time
}

View File

@ -0,0 +1,10 @@
CREATE TABLE `%s` (
`id` int unsigned NOT NULL AUTO_INCREMENT COMMENT 'User ID',
`passport` varchar(45) NOT NULL COMMENT 'User Passport',
`password` varchar(45) NOT NULL COMMENT 'User Password',
`nickname` varchar(45) NOT NULL COMMENT 'User Nickname',
`score` decimal(10,2) unsigned DEFAULT NULL COMMENT 'Total score amount.',
`create_at` datetime DEFAULT NULL COMMENT 'Created Time',
`update_at` datetime DEFAULT NULL COMMENT 'Updated Time',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

View File

@ -8,6 +8,7 @@ package article
import (
"context"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genservice/service"
)

View File

@ -0,0 +1,20 @@
gfcli:
gen:
dao:
- link: "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
tables: "user1"
descriptionTag: true
noModelComment: true
group: "sys"
clear: true
overwriteDao: true
- link: "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
tables: "user2"
descriptionTag: true
noModelComment: true
group: "book"
clear: true
overwriteDao: true

View File

@ -0,0 +1,85 @@
// ==========================================================================
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
// ==========================================================================
package internal
import (
"context"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/frame/g"
)
// User3Dao is the data access object for table user3.
type User3Dao struct {
table string // table is the underlying table name of the DAO.
group string // group is the database configuration group name of current DAO.
columns User3Columns // columns contains all the column names of Table for convenient usage.
}
// User3Columns defines and stores column names for table user3.
type User3Columns struct {
Id string // User ID
Passport string // User Passport
Password string // User Password
Nickname string // User Nickname
Score string // Total score amount.
CreateAt string // Created Time
UpdateAt string // Updated Time
}
// user3Columns holds the columns for table user3.
var user3Columns = User3Columns{
Id: "id",
Passport: "passport",
Password: "password",
Nickname: "nickname",
Score: "score",
CreateAt: "create_at",
UpdateAt: "update_at",
}
// NewUser3Dao creates and returns a new DAO object for table data access.
func NewUser3Dao() *User3Dao {
return &User3Dao{
group: "sys",
table: "user3",
columns: user3Columns,
}
}
// DB retrieves and returns the underlying raw database management object of current DAO.
func (dao *User3Dao) DB() gdb.DB {
return g.DB(dao.group)
}
// Table returns the table name of current dao.
func (dao *User3Dao) Table() string {
return dao.table
}
// Columns returns all column names of current dao.
func (dao *User3Dao) Columns() User3Columns {
return dao.columns
}
// Group returns the configuration group name of database of current dao.
func (dao *User3Dao) Group() string {
return dao.group
}
// Ctx creates and returns the Model for current DAO, It automatically sets the context for current operation.
func (dao *User3Dao) Ctx(ctx context.Context) *gdb.Model {
return dao.DB().Model(dao.table).Safe().Ctx(ctx)
}
// Transaction wraps the transaction logic using function f.
// It rollbacks the transaction and returns the error from function f if it returns non-nil error.
// It commits the transaction and returns nil if function f returns nil.
//
// Note that, you should not Commit or Rollback the transaction in function f
// as it is automatically handled by this function.
func (dao *User3Dao) Transaction(ctx context.Context, f func(ctx context.Context, tx gdb.TX) error) (err error) {
return dao.Ctx(ctx).Transaction(ctx, f)
}

View File

@ -0,0 +1,85 @@
// ==========================================================================
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
// ==========================================================================
package internal
import (
"context"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/frame/g"
)
// User4Dao is the data access object for table user4.
type User4Dao struct {
table string // table is the underlying table name of the DAO.
group string // group is the database configuration group name of current DAO.
columns User4Columns // columns contains all the column names of Table for convenient usage.
}
// User4Columns defines and stores column names for table user4.
type User4Columns struct {
Id string // User ID
Passport string // User Passport
Password string // User Password
Nickname string // User Nickname
Score string // Total score amount.
CreateAt string // Created Time
UpdateAt string // Updated Time
}
// user4Columns holds the columns for table user4.
var user4Columns = User4Columns{
Id: "id",
Passport: "passport",
Password: "password",
Nickname: "nickname",
Score: "score",
CreateAt: "create_at",
UpdateAt: "update_at",
}
// NewUser4Dao creates and returns a new DAO object for table data access.
func NewUser4Dao() *User4Dao {
return &User4Dao{
group: "book",
table: "user4",
columns: user4Columns,
}
}
// DB retrieves and returns the underlying raw database management object of current DAO.
func (dao *User4Dao) DB() gdb.DB {
return g.DB(dao.group)
}
// Table returns the table name of current dao.
func (dao *User4Dao) Table() string {
return dao.table
}
// Columns returns all column names of current dao.
func (dao *User4Dao) Columns() User4Columns {
return dao.columns
}
// Group returns the configuration group name of database of current dao.
func (dao *User4Dao) Group() string {
return dao.group
}
// Ctx creates and returns the Model for current DAO, It automatically sets the context for current operation.
func (dao *User4Dao) Ctx(ctx context.Context) *gdb.Model {
return dao.DB().Model(dao.table).Safe().Ctx(ctx)
}
// Transaction wraps the transaction logic using function f.
// It rollbacks the transaction and returns the error from function f if it returns non-nil error.
// It commits the transaction and returns nil if function f returns nil.
//
// Note that, you should not Commit or Rollback the transaction in function f
// as it is automatically handled by this function.
func (dao *User4Dao) Transaction(ctx context.Context, f func(ctx context.Context, tx gdb.TX) error) (err error) {
return dao.Ctx(ctx).Transaction(ctx, f)
}

View File

@ -0,0 +1,27 @@
// =================================================================================
// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
// =================================================================================
package dao
import (
"/internal"
)
// internalUser3Dao is internal type for wrapping internal DAO implements.
type internalUser3Dao = *internal.User3Dao
// user3Dao is the data access object for table user3.
// You can define custom methods on it to extend its functionality as you wish.
type user3Dao struct {
internalUser3Dao
}
var (
// User3 is globally public accessible object for table user3 operations.
User3 = user3Dao{
internal.NewUser3Dao(),
}
)
// Fill with you ideas below.

View File

@ -0,0 +1,27 @@
// =================================================================================
// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
// =================================================================================
package dao
import (
"/internal"
)
// internalUser4Dao is internal type for wrapping internal DAO implements.
type internalUser4Dao = *internal.User4Dao
// user4Dao is the data access object for table user4.
// You can define custom methods on it to extend its functionality as you wish.
type user4Dao struct {
internalUser4Dao
}
var (
// User4 is globally public accessible object for table user4 operations.
User4 = user4Dao{
internal.NewUser4Dao(),
}
)
// Fill with you ideas below.

View File

@ -0,0 +1,22 @@
// =================================================================================
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
// =================================================================================
package do
import (
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gtime"
)
// User1 is the golang structure of table user1 for DAO operations like Where/Data.
type User1 struct {
g.Meta `orm:"table:user1, do:true"`
Id interface{} // User ID
Passport interface{} // User Passport
Password interface{} // User Password
Nickname interface{} // User Nickname
Score interface{} // Total score amount.
CreateAt *gtime.Time // Created Time
UpdateAt *gtime.Time // Updated Time
}

View File

@ -0,0 +1,22 @@
// =================================================================================
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
// =================================================================================
package do
import (
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gtime"
)
// User2 is the golang structure of table user2 for DAO operations like Where/Data.
type User2 struct {
g.Meta `orm:"table:user2, do:true"`
Id interface{} // User ID
Passport interface{} // User Passport
Password interface{} // User Password
Nickname interface{} // User Nickname
Score interface{} // Total score amount.
CreateAt *gtime.Time // Created Time
UpdateAt *gtime.Time // Updated Time
}

View File

@ -0,0 +1,20 @@
// =================================================================================
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
// =================================================================================
package entity
import (
"github.com/gogf/gf/v2/os/gtime"
)
// User1 is the golang structure for table user1.
type User1 struct {
Id uint `json:"ID" description:"User ID"`
Passport string `json:"PASSPORT" description:"User Passport"`
Password string `json:"PASSWORD" description:"User Password"`
Nickname string `json:"NICKNAME" description:"User Nickname"`
Score float64 `json:"SCORE" description:"Total score amount."`
CreateAt *gtime.Time `json:"CREATE_AT" description:"Created Time"`
UpdateAt *gtime.Time `json:"UPDATE_AT" description:"Updated Time"`
}

View File

@ -0,0 +1,20 @@
// =================================================================================
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
// =================================================================================
package entity
import (
"github.com/gogf/gf/v2/os/gtime"
)
// User2 is the golang structure for table user2.
type User2 struct {
Id uint `json:"ID" description:"User ID"`
Passport string `json:"PASSPORT" description:"User Passport"`
Password string `json:"PASSWORD" description:"User Password"`
Nickname string `json:"NICKNAME" description:"User Nickname"`
Score float64 `json:"SCORE" description:"Total score amount."`
CreateAt *gtime.Time `json:"CREATE_AT" description:"Created Time"`
UpdateAt *gtime.Time `json:"UPDATE_AT" description:"Updated Time"`
}

View File

@ -0,0 +1,10 @@
CREATE TABLE `user1` (
`id` int unsigned NOT NULL AUTO_INCREMENT COMMENT 'User ID',
`passport` varchar(45) NOT NULL COMMENT 'User Passport',
`password` varchar(45) NOT NULL COMMENT 'User Password',
`nickname` varchar(45) NOT NULL COMMENT 'User Nickname',
`score` decimal(10,2) unsigned DEFAULT NULL COMMENT 'Total score amount.',
`create_at` datetime DEFAULT NULL COMMENT 'Created Time',
`update_at` datetime DEFAULT NULL COMMENT 'Updated Time',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

View File

@ -0,0 +1,10 @@
CREATE TABLE `user2` (
`id` int unsigned NOT NULL AUTO_INCREMENT COMMENT 'User ID',
`passport` varchar(45) NOT NULL COMMENT 'User Passport',
`password` varchar(45) NOT NULL COMMENT 'User Password',
`nickname` varchar(45) NOT NULL COMMENT 'User Nickname',
`score` decimal(10,2) unsigned DEFAULT NULL COMMENT 'Total score amount.',
`create_at` datetime DEFAULT NULL COMMENT 'Created Time',
`update_at` datetime DEFAULT NULL COMMENT 'Updated Time',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

View File

@ -0,0 +1,20 @@
gfcli:
gen:
dao:
- link: "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
tables: "user1"
descriptionTag: true
noModelComment: true
group: "sys"
clear: true
overwriteDao: false
- link: "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
tables: "user2"
descriptionTag: true
noModelComment: true
group: "book"
clear: true
overwriteDao: true

View File

@ -0,0 +1,85 @@
// ==========================================================================
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
// ==========================================================================
package internal
import (
"context"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/frame/g"
)
// User1Dao is the data access object for table user1.
type User1Dao struct {
table string // table is the underlying table name of the DAO.
group string // group is the database configuration group name of current DAO.
columns User1Columns // columns contains all the column names of Table for convenient usage.
}
// User1Columns defines and stores column names for table user1.
type User1Columns struct {
Id string // User ID
Passport string // User Passport
Password string // User Password
Nickname string // User Nickname
Score string // Total score amount.
CreateAt string // Created Time
UpdateAt string // Updated Time
}
// user1Columns holds the columns for table user1.
var user1Columns = User1Columns{
Id: "id",
Passport: "passport",
Password: "password",
Nickname: "nickname",
Score: "score",
CreateAt: "create_at",
UpdateAt: "update_at",
}
// NewUser1Dao creates and returns a new DAO object for table data access.
func NewUser1Dao() *User1Dao {
return &User1Dao{
group: "sys",
table: "user1",
columns: user1Columns,
}
}
// DB retrieves and returns the underlying raw database management object of current DAO.
func (dao *User1Dao) DB() gdb.DB {
return g.DB(dao.group)
}
// Table returns the table name of current dao.
func (dao *User1Dao) Table() string {
return dao.table
}
// Columns returns all column names of current dao.
func (dao *User1Dao) Columns() User1Columns {
return dao.columns
}
// Group returns the configuration group name of database of current dao.
func (dao *User1Dao) Group() string {
return dao.group
}
// Ctx creates and returns the Model for current DAO, It automatically sets the context for current operation.
func (dao *User1Dao) Ctx(ctx context.Context) *gdb.Model {
return dao.DB().Model(dao.table).Safe().Ctx(ctx)
}
// Transaction wraps the transaction logic using function f.
// It rollbacks the transaction and returns the error from function f if it returns non-nil error.
// It commits the transaction and returns nil if function f returns nil.
//
// Note that, you should not Commit or Rollback the transaction in function f
// as it is automatically handled by this function.
func (dao *User1Dao) Transaction(ctx context.Context, f func(ctx context.Context, tx gdb.TX) error) (err error) {
return dao.Ctx(ctx).Transaction(ctx, f)
}

View File

@ -0,0 +1,85 @@
// ==========================================================================
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
// ==========================================================================
package internal
import (
"context"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/frame/g"
)
// User2Dao is the data access object for table user2.
type User2Dao struct {
table string // table is the underlying table name of the DAO.
group string // group is the database configuration group name of current DAO.
columns User2Columns // columns contains all the column names of Table for convenient usage.
}
// User2Columns defines and stores column names for table user2.
type User2Columns struct {
Id string // User ID
Passport string // User Passport
Password string // User Password
Nickname string // User Nickname
Score string // Total score amount.
CreateAt string // Created Time
UpdateAt string // Updated Time
}
// user2Columns holds the columns for table user2.
var user2Columns = User2Columns{
Id: "id",
Passport: "passport",
Password: "password",
Nickname: "nickname",
Score: "score",
CreateAt: "create_at",
UpdateAt: "update_at",
}
// NewUser2Dao creates and returns a new DAO object for table data access.
func NewUser2Dao() *User2Dao {
return &User2Dao{
group: "sys",
table: "user2",
columns: user2Columns,
}
}
// DB retrieves and returns the underlying raw database management object of current DAO.
func (dao *User2Dao) DB() gdb.DB {
return g.DB(dao.group)
}
// Table returns the table name of current dao.
func (dao *User2Dao) Table() string {
return dao.table
}
// Columns returns all column names of current dao.
func (dao *User2Dao) Columns() User2Columns {
return dao.columns
}
// Group returns the configuration group name of database of current dao.
func (dao *User2Dao) Group() string {
return dao.group
}
// Ctx creates and returns the Model for current DAO, It automatically sets the context for current operation.
func (dao *User2Dao) Ctx(ctx context.Context) *gdb.Model {
return dao.DB().Model(dao.table).Safe().Ctx(ctx)
}
// Transaction wraps the transaction logic using function f.
// It rollbacks the transaction and returns the error from function f if it returns non-nil error.
// It commits the transaction and returns nil if function f returns nil.
//
// Note that, you should not Commit or Rollback the transaction in function f
// as it is automatically handled by this function.
func (dao *User2Dao) Transaction(ctx context.Context, f func(ctx context.Context, tx gdb.TX) error) (err error) {
return dao.Ctx(ctx).Transaction(ctx, f)
}

View File

@ -0,0 +1,85 @@
// ==========================================================================
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
// ==========================================================================
package internal
import (
"context"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/frame/g"
)
// User3Dao is the data access object for table user3.
type User3Dao struct {
table string // table is the underlying table name of the DAO.
group string // group is the database configuration group name of current DAO.
columns User3Columns // columns contains all the column names of Table for convenient usage.
}
// User3Columns defines and stores column names for table user3.
type User3Columns struct {
Id string // User ID
Passport string // User Passport
Password string // User Password
Nickname string // User Nickname
Score string // Total score amount.
CreateAt string // Created Time
UpdateAt string // Updated Time
}
// user3Columns holds the columns for table user3.
var user3Columns = User3Columns{
Id: "id",
Passport: "passport",
Password: "password",
Nickname: "nickname",
Score: "score",
CreateAt: "create_at",
UpdateAt: "update_at",
}
// NewUser3Dao creates and returns a new DAO object for table data access.
func NewUser3Dao() *User3Dao {
return &User3Dao{
group: "sys",
table: "user3",
columns: user3Columns,
}
}
// DB retrieves and returns the underlying raw database management object of current DAO.
func (dao *User3Dao) DB() gdb.DB {
return g.DB(dao.group)
}
// Table returns the table name of current dao.
func (dao *User3Dao) Table() string {
return dao.table
}
// Columns returns all column names of current dao.
func (dao *User3Dao) Columns() User3Columns {
return dao.columns
}
// Group returns the configuration group name of database of current dao.
func (dao *User3Dao) Group() string {
return dao.group
}
// Ctx creates and returns the Model for current DAO, It automatically sets the context for current operation.
func (dao *User3Dao) Ctx(ctx context.Context) *gdb.Model {
return dao.DB().Model(dao.table).Safe().Ctx(ctx)
}
// Transaction wraps the transaction logic using function f.
// It rollbacks the transaction and returns the error from function f if it returns non-nil error.
// It commits the transaction and returns nil if function f returns nil.
//
// Note that, you should not Commit or Rollback the transaction in function f
// as it is automatically handled by this function.
func (dao *User3Dao) Transaction(ctx context.Context, f func(ctx context.Context, tx gdb.TX) error) (err error) {
return dao.Ctx(ctx).Transaction(ctx, f)
}

View File

@ -0,0 +1,85 @@
// ==========================================================================
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
// ==========================================================================
package internal
import (
"context"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/frame/g"
)
// User4Dao is the data access object for table user4.
type User4Dao struct {
table string // table is the underlying table name of the DAO.
group string // group is the database configuration group name of current DAO.
columns User4Columns // columns contains all the column names of Table for convenient usage.
}
// User4Columns defines and stores column names for table user4.
type User4Columns struct {
Id string // User ID
Passport string // User Passport
Password string // User Password
Nickname string // User Nickname
Score string // Total score amount.
CreateAt string // Created Time
UpdateAt string // Updated Time
}
// user4Columns holds the columns for table user4.
var user4Columns = User4Columns{
Id: "id",
Passport: "passport",
Password: "password",
Nickname: "nickname",
Score: "score",
CreateAt: "create_at",
UpdateAt: "update_at",
}
// NewUser4Dao creates and returns a new DAO object for table data access.
func NewUser4Dao() *User4Dao {
return &User4Dao{
group: "book",
table: "user4",
columns: user4Columns,
}
}
// DB retrieves and returns the underlying raw database management object of current DAO.
func (dao *User4Dao) DB() gdb.DB {
return g.DB(dao.group)
}
// Table returns the table name of current dao.
func (dao *User4Dao) Table() string {
return dao.table
}
// Columns returns all column names of current dao.
func (dao *User4Dao) Columns() User4Columns {
return dao.columns
}
// Group returns the configuration group name of database of current dao.
func (dao *User4Dao) Group() string {
return dao.group
}
// Ctx creates and returns the Model for current DAO, It automatically sets the context for current operation.
func (dao *User4Dao) Ctx(ctx context.Context) *gdb.Model {
return dao.DB().Model(dao.table).Safe().Ctx(ctx)
}
// Transaction wraps the transaction logic using function f.
// It rollbacks the transaction and returns the error from function f if it returns non-nil error.
// It commits the transaction and returns nil if function f returns nil.
//
// Note that, you should not Commit or Rollback the transaction in function f
// as it is automatically handled by this function.
func (dao *User4Dao) Transaction(ctx context.Context, f func(ctx context.Context, tx gdb.TX) error) (err error) {
return dao.Ctx(ctx).Transaction(ctx, f)
}

View File

@ -0,0 +1,29 @@
// =================================================================================
// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
// =================================================================================
// I am not overwritten.
package dao
import (
"/internal"
)
// internalUser1Dao is internal type for wrapping internal DAO implements.
type internalUser1Dao = *internal.User1Dao
// user1Dao is the data access object for table user1.
// You can define custom methods on it to extend its functionality as you wish.
type user1Dao struct {
internalUser1Dao
}
var (
// User1 is globally public accessible object for table user1 operations.
User1 = user1Dao{
internal.NewUser1Dao(),
}
)
// Fill with you ideas below.

View File

@ -0,0 +1,29 @@
// =================================================================================
// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
// =================================================================================
// I am not overwritten.
package dao
import (
"/internal"
)
// internalUser2Dao is internal type for wrapping internal DAO implements.
type internalUser2Dao = *internal.User2Dao
// user2Dao is the data access object for table user2.
// You can define custom methods on it to extend its functionality as you wish.
type user2Dao struct {
internalUser2Dao
}
var (
// User2 is globally public accessible object for table user2 operations.
User2 = user2Dao{
internal.NewUser2Dao(),
}
)
// Fill with you ideas below.

View File

@ -0,0 +1,27 @@
// =================================================================================
// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
// =================================================================================
package dao
import (
"/internal"
)
// internalUser3Dao is internal type for wrapping internal DAO implements.
type internalUser3Dao = *internal.User3Dao
// user3Dao is the data access object for table user3.
// You can define custom methods on it to extend its functionality as you wish.
type user3Dao struct {
internalUser3Dao
}
var (
// User3 is globally public accessible object for table user3 operations.
User3 = user3Dao{
internal.NewUser3Dao(),
}
)
// Fill with you ideas below.

View File

@ -0,0 +1,27 @@
// =================================================================================
// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
// =================================================================================
package dao
import (
"/internal"
)
// internalUser4Dao is internal type for wrapping internal DAO implements.
type internalUser4Dao = *internal.User4Dao
// user4Dao is the data access object for table user4.
// You can define custom methods on it to extend its functionality as you wish.
type user4Dao struct {
internalUser4Dao
}
var (
// User4 is globally public accessible object for table user4 operations.
User4 = user4Dao{
internal.NewUser4Dao(),
}
)
// Fill with you ideas below.

View File

@ -0,0 +1,22 @@
// =================================================================================
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
// =================================================================================
package do
import (
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gtime"
)
// User1 is the golang structure of table user1 for DAO operations like Where/Data.
type User1 struct {
g.Meta `orm:"table:user1, do:true"`
Id interface{} // User ID
Passport interface{} // User Passport
Password interface{} // User Password
Nickname interface{} // User Nickname
Score interface{} // Total score amount.
CreateAt *gtime.Time // Created Time
UpdateAt *gtime.Time // Updated Time
}

View File

@ -0,0 +1,22 @@
// =================================================================================
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
// =================================================================================
package do
import (
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gtime"
)
// User2 is the golang structure of table user2 for DAO operations like Where/Data.
type User2 struct {
g.Meta `orm:"table:user2, do:true"`
Id interface{} // User ID
Passport interface{} // User Passport
Password interface{} // User Password
Nickname interface{} // User Nickname
Score interface{} // Total score amount.
CreateAt *gtime.Time // Created Time
UpdateAt *gtime.Time // Updated Time
}

View File

@ -0,0 +1,20 @@
// =================================================================================
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
// =================================================================================
package entity
import (
"github.com/gogf/gf/v2/os/gtime"
)
// User1 is the golang structure for table user1.
type User1 struct {
Id uint `json:"ID" description:"User ID"`
Passport string `json:"PASSPORT" description:"User Passport"`
Password string `json:"PASSWORD" description:"User Password"`
Nickname string `json:"NICKNAME" description:"User Nickname"`
Score float64 `json:"SCORE" description:"Total score amount."`
CreateAt *gtime.Time `json:"CREATE_AT" description:"Created Time"`
UpdateAt *gtime.Time `json:"UPDATE_AT" description:"Updated Time"`
}

View File

@ -0,0 +1,20 @@
// =================================================================================
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
// =================================================================================
package entity
import (
"github.com/gogf/gf/v2/os/gtime"
)
// User2 is the golang structure for table user2.
type User2 struct {
Id uint `json:"ID" description:"User ID"`
Passport string `json:"PASSPORT" description:"User Passport"`
Password string `json:"PASSWORD" description:"User Password"`
Nickname string `json:"NICKNAME" description:"User Nickname"`
Score float64 `json:"SCORE" description:"Total score amount."`
CreateAt *gtime.Time `json:"CREATE_AT" description:"Created Time"`
UpdateAt *gtime.Time `json:"UPDATE_AT" description:"Updated Time"`
}

View File

@ -0,0 +1,10 @@
CREATE TABLE `user1` (
`id` int unsigned NOT NULL AUTO_INCREMENT COMMENT 'User ID',
`passport` varchar(45) NOT NULL COMMENT 'User Passport',
`password` varchar(45) NOT NULL COMMENT 'User Password',
`nickname` varchar(45) NOT NULL COMMENT 'User Nickname',
`score` decimal(10,2) unsigned DEFAULT NULL COMMENT 'Total score amount.',
`create_at` datetime DEFAULT NULL COMMENT 'Created Time',
`update_at` datetime DEFAULT NULL COMMENT 'Updated Time',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

View File

@ -0,0 +1,10 @@
CREATE TABLE `user2` (
`id` int unsigned NOT NULL AUTO_INCREMENT COMMENT 'User ID',
`passport` varchar(45) NOT NULL COMMENT 'User Passport',
`password` varchar(45) NOT NULL COMMENT 'User Password',
`nickname` varchar(45) NOT NULL COMMENT 'User Nickname',
`score` decimal(10,2) unsigned DEFAULT NULL COMMENT 'Total score amount.',
`create_at` datetime DEFAULT NULL COMMENT 'Created Time',
`update_at` datetime DEFAULT NULL COMMENT 'Updated Time',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

View File

@ -9,6 +9,7 @@ package utils
import (
"context"
"fmt"
"golang.org/x/tools/imports"
"github.com/gogf/gf/cmd/gf/v2/internal/consts"

View File

@ -173,7 +173,11 @@ func (a *IntArray) InsertBefore(index int, values ...int) error {
a.mu.Lock()
defer a.mu.Unlock()
if index < 0 || index >= len(a.array) {
return gerror.NewCodef(gcode.CodeInvalidParameter, "index %d out of array range %d", index, len(a.array))
return gerror.NewCodef(
gcode.CodeInvalidParameter,
"index %d out of array range %d",
index, len(a.array),
)
}
rear := append([]int{}, a.array[index:]...)
a.array = append(a.array[0:index], values...)
@ -186,7 +190,11 @@ func (a *IntArray) InsertAfter(index int, values ...int) error {
a.mu.Lock()
defer a.mu.Unlock()
if index < 0 || index >= len(a.array) {
return gerror.NewCodef(gcode.CodeInvalidParameter, "index %d out of array range %d", index, len(a.array))
return gerror.NewCodef(
gcode.CodeInvalidParameter,
"index %d out of array range %d",
index, len(a.array),
)
}
rear := append([]int{}, a.array[index+1:]...)
a.array = append(a.array[0:index+1], values...)
@ -583,7 +591,11 @@ func (a *IntArray) Fill(startIndex int, num int, value int) error {
a.mu.Lock()
defer a.mu.Unlock()
if startIndex < 0 || startIndex > len(a.array) {
return gerror.NewCodef(gcode.CodeInvalidParameter, "index %d out of array range %d", startIndex, len(a.array))
return gerror.NewCodef(
gcode.CodeInvalidParameter,
"index %d out of array range %d",
startIndex, len(a.array),
)
}
for i := startIndex; i < startIndex+num; i++ {
if i > len(a.array)-1 {

View File

@ -75,7 +75,8 @@ func TestQueue_Close(t *testing.T) {
q1 := gqueue.New()
q1.Push(1)
q1.Push(2)
time.Sleep(time.Millisecond)
// wait sync to channel
time.Sleep(10 * time.Millisecond)
t.Assert(q1.Len(), 2)
q1.Close()
})
@ -83,7 +84,8 @@ func TestQueue_Close(t *testing.T) {
q1 := gqueue.New(2)
q1.Push(1)
q1.Push(2)
time.Sleep(time.Millisecond)
// wait sync to channel
time.Sleep(10 * time.Millisecond)
t.Assert(q1.Len(), 2)
q1.Close()
})

View File

@ -42,7 +42,7 @@ func ExampleIntSet_Add() {
fmt.Println(intSet.Slice())
fmt.Println(intSet.AddIfNotExist(1))
// Mya Output:
// May Output:
// [1 2 3]
// false
}
@ -56,7 +56,7 @@ func ExampleIntSet_AddIfNotExist() {
fmt.Println(intSet.Slice())
fmt.Println(intSet.AddIfNotExist(1))
// Mya Output:
// May Output:
// [1 2 3]
// false
}

View File

@ -42,13 +42,13 @@ func ExampleStrSet_Add() {
fmt.Println(strSet.Slice())
fmt.Println(strSet.AddIfNotExist("str"))
// Mya Output:
// May Output:
// [str str1 str2 str3]
// false
}
// AddIfNotExist checks whether item exists in the set,
// it adds the item to set and returns true if it does not exists in the set,
// it adds the item to set and returns true if it does not exist in the set,
// or else it does nothing and returns false.
func ExampleStrSet_AddIfNotExist() {
strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
@ -56,13 +56,13 @@ func ExampleStrSet_AddIfNotExist() {
fmt.Println(strSet.Slice())
fmt.Println(strSet.AddIfNotExist("str"))
// Mya Output:
// May Output:
// [str str1 str2 str3]
// false
}
// AddIfNotExistFunc checks whether item exists in the set,
// it adds the item to set and returns true if it does not exists in the set and function `f` returns true,
// it adds the item to set and returns true if it does not exist in the set and function `f` returns true,
// or else it does nothing and returns false.
// Note that, the function `f` is executed without writing lock.
func ExampleStrSet_AddIfNotExistFunc() {
@ -79,7 +79,7 @@ func ExampleStrSet_AddIfNotExistFunc() {
}
// AddIfNotExistFunc checks whether item exists in the set,
// it adds the item to set and returns true if it does not exists in the set and function `f` returns true,
// it adds the item to set and returns true if it does not exist in the set and function `f` returns true,
// or else it does nothing and returns false.
// Note that, the function `f` is executed without writing lock.
func ExampleStrSet_AddIfNotExistFuncLock() {

View File

@ -4,7 +4,7 @@ go 1.18
require (
github.com/apolloconfig/agollo/v4 v4.3.1
github.com/gogf/gf/v2 v2.6.0
github.com/gogf/gf/v2 v2.6.3
)
require (

View File

@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/config/consul/v2
go 1.19
require (
github.com/gogf/gf/v2 v2.6.0
github.com/gogf/gf/v2 v2.6.3
github.com/hashicorp/consul/api v1.24.0
github.com/hashicorp/go-cleanhttp v0.5.2
)

View File

@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/config/kubecm/v2
go 1.19
require (
github.com/gogf/gf/v2 v2.6.0
github.com/gogf/gf/v2 v2.6.3
k8s.io/api v0.27.4
k8s.io/apimachinery v0.27.4
k8s.io/client-go v0.27.4

View File

@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/config/nacos/v2
go 1.18
require (
github.com/gogf/gf/v2 v2.6.0
github.com/gogf/gf/v2 v2.6.3
github.com/nacos-group/nacos-sdk-go v1.1.4
)

View File

@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/config/polaris/v2
go 1.18
require (
github.com/gogf/gf/v2 v2.6.0
github.com/gogf/gf/v2 v2.6.3
github.com/polarismesh/polaris-go v1.5.5
)

View File

@ -9,25 +9,10 @@ package clickhouse
import (
"context"
"database/sql"
"database/sql/driver"
"errors"
"fmt"
"net/url"
"strings"
"time"
"github.com/ClickHouse/clickhouse-go/v2"
"github.com/google/uuid"
"github.com/shopspring/decimal"
"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/os/gctx"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/text/gregex"
"github.com/gogf/gf/v2/util/gutil"
)
// Driver is the driver for postgresql database.
@ -71,386 +56,9 @@ func (d *Driver) New(core *gdb.Core, node *gdb.ConfigNode) (gdb.DB, error) {
}, nil
}
// Open creates and returns an underlying sql.DB object for clickhouse.
func (d *Driver) Open(config *gdb.ConfigNode) (db *sql.DB, err error) {
source := config.Link
// clickhouse://username:password@host1:9000,host2:9000/database?dial_timeout=200ms&max_execution_time=60
if config.Link != "" {
// ============================================================================
// Deprecated from v2.2.0.
// ============================================================================
// Custom changing the schema in runtime.
if config.Name != "" {
source, _ = gregex.ReplaceString(replaceSchemaPattern, "@$1/"+config.Name, config.Link)
} else {
// If no schema, the link is matched for replacement
dbName, _ := gregex.MatchString(replaceSchemaPattern, config.Link)
if len(dbName) > 0 {
config.Name = dbName[len(dbName)-1]
}
}
} else {
if config.Pass != "" {
source = fmt.Sprintf(
"clickhouse://%s:%s@%s:%s/%s?debug=%t",
config.User, url.PathEscape(config.Pass),
config.Host, config.Port, config.Name, config.Debug,
)
} else {
source = fmt.Sprintf(
"clickhouse://%s@%s:%s/%s?debug=%t",
config.User, config.Host, config.Port, config.Name, config.Debug,
)
}
if config.Extra != "" {
source = fmt.Sprintf("%s&%s", source, config.Extra)
}
}
if db, err = sql.Open(driverName, source); err != nil {
err = gerror.WrapCodef(
gcode.CodeDbOperationError, err,
`sql.Open failed for driver "%s" by source "%s"`, driverName, source,
)
return nil, err
}
return
}
// Tables retrieves and returns the tables of current schema.
// It's mainly used in cli tool chain for automatically generating the models.
func (d *Driver) Tables(ctx context.Context, schema ...string) (tables []string, err error) {
var result gdb.Result
link, err := d.SlaveLink(schema...)
if err != nil {
return nil, err
}
query := fmt.Sprintf("select name from `system`.tables where database = '%s'", d.GetConfig().Name)
result, err = d.DoSelect(ctx, link, query)
if err != nil {
return
}
for _, m := range result {
tables = append(tables, m["name"].String())
}
return
}
// TableFields retrieves and returns the fields' information of specified table of current schema.
// Also see DriverMysql.TableFields.
func (d *Driver) TableFields(ctx context.Context, table string, schema ...string) (fields map[string]*gdb.TableField, err error) {
var (
result gdb.Result
link gdb.Link
usedSchema = gutil.GetOrDefaultStr(d.GetSchema(), schema...)
)
if link, err = d.SlaveLink(usedSchema); err != nil {
return nil, err
}
var (
columns = "name,position,default_expression,comment,type,is_in_partition_key,is_in_sorting_key,is_in_primary_key,is_in_sampling_key"
getColumnsSql = fmt.Sprintf(
"select %s from `system`.columns c where `table` = '%s'",
columns, table,
)
)
result, err = d.DoSelect(ctx, link, getColumnsSql)
if err != nil {
return nil, err
}
fields = make(map[string]*gdb.TableField)
for _, m := range result {
var (
isNull = false
fieldType = m["type"].String()
)
// in clickhouse , field type like is Nullable(int)
fieldsResult, _ := gregex.MatchString(`^Nullable\((.*?)\)`, fieldType)
if len(fieldsResult) == 2 {
isNull = true
fieldType = fieldsResult[1]
}
position := m["position"].Int()
if result[0]["position"].Int() != 0 {
position -= 1
}
fields[m["name"].String()] = &gdb.TableField{
Index: position,
Name: m["name"].String(),
Default: m["default_expression"].Val(),
Comment: m["comment"].String(),
// Key: m["Key"].String(),
Type: fieldType,
Null: isNull,
}
}
return fields, nil
}
// PingMaster pings the master node to check authentication or keeps the connection alive.
func (d *Driver) PingMaster() error {
conn, err := d.Master()
if err != nil {
return err
}
return d.ping(conn)
}
// PingSlave pings the slave node to check authentication or keeps the connection alive.
func (d *Driver) PingSlave() error {
conn, err := d.Slave()
if err != nil {
return err
}
return d.ping(conn)
}
// ping Returns the Clickhouse specific error.
func (d *Driver) ping(conn *sql.DB) error {
err := conn.Ping()
if exception, ok := err.(*clickhouse.Exception); ok {
return fmt.Errorf("[%d]%s", exception.Code, exception.Message)
}
return err
}
// DoFilter handles the sql before posts it to database.
func (d *Driver) DoFilter(
ctx context.Context, link gdb.Link, originSql string, args []interface{},
) (newSql string, newArgs []interface{}, err error) {
if len(args) == 0 {
return originSql, args, nil
}
// Convert placeholder char '?' to string "$x".
var index int
originSql, _ = gregex.ReplaceStringFunc(`\?`, originSql, func(s string) string {
index++
return fmt.Sprintf(`$%d`, index)
})
// Only SQL generated through the framework is processed.
if !d.getNeedParsedSqlFromCtx(ctx) {
return originSql, args, nil
}
// replace STD SQL to Clickhouse SQL grammar
modeRes, err := gregex.MatchString(filterTypePattern, strings.TrimSpace(originSql))
if err != nil {
return "", nil, err
}
if len(modeRes) == 0 {
return originSql, args, nil
}
// Only delete/ UPDATE statements require filter
switch strings.ToUpper(modeRes[0]) {
case "UPDATE":
// MySQL eg: UPDATE table_name SET field1=new-value1, field2=new-value2 [WHERE Clause]
// Clickhouse eg: ALTER TABLE [db.]table UPDATE column1 = expr1 [, ...] WHERE filter_expr
newSql, err = gregex.ReplaceStringFuncMatch(
updateFilterPattern, originSql,
func(s []string) string {
return fmt.Sprintf("ALTER TABLE %s UPDATE", s[1])
},
)
if err != nil {
return "", nil, err
}
return newSql, args, nil
case "DELETE":
// MySQL eg: DELETE FROM table_name [WHERE Clause]
// Clickhouse eg: ALTER TABLE [db.]table [ON CLUSTER cluster] DELETE WHERE filter_expr
newSql, err = gregex.ReplaceStringFuncMatch(
deleteFilterPattern, originSql,
func(s []string) string {
return fmt.Sprintf("ALTER TABLE %s DELETE", s[1])
},
)
if err != nil {
return "", nil, err
}
return newSql, args, nil
}
return originSql, args, nil
}
// DoCommit commits current sql and arguments to underlying sql driver.
func (d *Driver) DoCommit(ctx context.Context, in gdb.DoCommitInput) (out gdb.DoCommitOutput, err error) {
ctx = d.InjectIgnoreResult(ctx)
return d.Core.DoCommit(ctx, in)
}
// DoInsert inserts or updates data forF given table.
func (d *Driver) DoInsert(
ctx context.Context, link gdb.Link, table string, list gdb.List, option gdb.DoInsertOption,
) (result sql.Result, err error) {
var (
keys []string // Field names.
valueHolder = make([]string, 0)
)
// Handle the field names and placeholders.
for k := range list[0] {
keys = append(keys, k)
valueHolder = append(valueHolder, "?")
}
// Prepare the batch result pointer.
var (
charL, charR = d.Core.GetChars()
keysStr = charL + strings.Join(keys, charR+","+charL) + charR
holderStr = strings.Join(valueHolder, ",")
tx gdb.TX
stmt *gdb.Stmt
)
tx, err = d.Core.Begin(ctx)
if err != nil {
return
}
// It here uses defer to guarantee transaction be committed or roll-backed.
defer func() {
if err == nil {
_ = tx.Commit()
} else {
_ = tx.Rollback()
}
}()
stmt, err = tx.Prepare(fmt.Sprintf(
"INSERT INTO %s(%s) VALUES (%s)",
d.QuotePrefixTableName(table), keysStr,
holderStr,
))
if err != nil {
return
}
for i := 0; i < len(list); i++ {
// Values that will be committed to underlying database driver.
params := make([]interface{}, 0)
for _, k := range keys {
params = append(params, list[i][k])
}
// Prepare is allowed to execute only once in a transaction opened by clickhouse
result, err = stmt.ExecContext(ctx, params...)
if err != nil {
return
}
}
return
}
// ConvertValueForField converts value to the type of the record field.
func (d *Driver) ConvertValueForField(ctx context.Context, fieldType string, fieldValue interface{}) (interface{}, error) {
switch itemValue := fieldValue.(type) {
case time.Time:
// If the time is zero, it then updates it to nil,
// which will insert/update the value to database as "null".
if itemValue.IsZero() {
return nil, nil
}
case uuid.UUID:
return itemValue, nil
case *time.Time:
// If the time is zero, it then updates it to nil,
// which will insert/update the value to database as "null".
if itemValue == nil || itemValue.IsZero() {
return nil, nil
}
return itemValue, nil
case gtime.Time:
// If the time is zero, it then updates it to nil,
// which will insert/update the value to database as "null".
if itemValue.IsZero() {
return nil, nil
}
// for gtime type, needs to get time.Time
return itemValue.Time, nil
case *gtime.Time:
// for gtime type, needs to get time.Time
if itemValue != nil {
return itemValue.Time, nil
}
// If the time is zero, it then updates it to nil,
// which will insert/update the value to database as "null".
if itemValue == nil || itemValue.IsZero() {
return nil, nil
}
case decimal.Decimal:
return itemValue, nil
case *decimal.Decimal:
if itemValue != nil {
return *itemValue, nil
}
return nil, nil
default:
// if the other type implements valuer for the driver package
// the converted result is used
// otherwise the interface data is committed
valuer, ok := itemValue.(driver.Valuer)
if !ok {
return itemValue, nil
}
convertedValue, err := valuer.Value()
if err != nil {
return nil, err
}
return convertedValue, nil
}
return fieldValue, nil
}
// DoDelete does "DELETE FROM ... " statement for the table.
func (d *Driver) DoDelete(ctx context.Context, link gdb.Link, table string, condition string, args ...interface{}) (result sql.Result, err error) {
ctx = d.injectNeedParsedSql(ctx)
return d.Core.DoDelete(ctx, link, table, condition, args...)
}
// DoUpdate does "UPDATE ... " statement for the table.
func (d *Driver) DoUpdate(ctx context.Context, link gdb.Link, table string, data interface{}, condition string, args ...interface{}) (result sql.Result, err error) {
ctx = d.injectNeedParsedSql(ctx)
return d.Core.DoUpdate(ctx, link, table, data, condition, args...)
}
// InsertIgnore Other queries for modifying data parts are not supported: REPLACE, MERGE, UPSERT, INSERT UPDATE.
func (d *Driver) InsertIgnore(ctx context.Context, table string, data interface{}, batch ...int) (sql.Result, error) {
return nil, errUnsupportedInsertIgnore
}
// InsertAndGetId Other queries for modifying data parts are not supported: REPLACE, MERGE, UPSERT, INSERT UPDATE.
func (d *Driver) InsertAndGetId(ctx context.Context, table string, data interface{}, batch ...int) (int64, error) {
return 0, errUnsupportedInsertGetId
}
// Replace Other queries for modifying data parts are not supported: REPLACE, MERGE, UPSERT, INSERT UPDATE.
func (d *Driver) Replace(ctx context.Context, table string, data interface{}, batch ...int) (sql.Result, error) {
return nil, errUnsupportedReplace
}
// Begin starts and returns the transaction object.
func (d *Driver) Begin(ctx context.Context) (tx gdb.TX, err error) {
return nil, errUnsupportedBegin
}
// Transaction wraps the transaction logic using function `f`.
func (d *Driver) Transaction(ctx context.Context, f func(ctx context.Context, tx gdb.TX) error) error {
return errUnsupportedTransaction
}
func (d *Driver) injectNeedParsedSql(ctx context.Context) context.Context {
if ctx.Value(needParsedSqlInCtx) != nil {
return ctx
}
return context.WithValue(ctx, needParsedSqlInCtx, true)
}
func (d *Driver) getNeedParsedSqlFromCtx(ctx context.Context) bool {
if ctx.Value(needParsedSqlInCtx) != nil {
return true
}
return false
}

View File

@ -0,0 +1,85 @@
// 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 clickhouse
import (
"context"
"database/sql/driver"
"time"
"github.com/google/uuid"
"github.com/shopspring/decimal"
"github.com/gogf/gf/v2/os/gtime"
)
// ConvertValueForField converts value to the type of the record field.
func (d *Driver) ConvertValueForField(ctx context.Context, fieldType string, fieldValue interface{}) (interface{}, error) {
switch itemValue := fieldValue.(type) {
case time.Time:
// If the time is zero, it then updates it to nil,
// which will insert/update the value to database as "null".
if itemValue.IsZero() {
return nil, nil
}
case uuid.UUID:
return itemValue, nil
case *time.Time:
// If the time is zero, it then updates it to nil,
// which will insert/update the value to database as "null".
if itemValue == nil || itemValue.IsZero() {
return nil, nil
}
return itemValue, nil
case gtime.Time:
// If the time is zero, it then updates it to nil,
// which will insert/update the value to database as "null".
if itemValue.IsZero() {
return nil, nil
}
// for gtime type, needs to get time.Time
return itemValue.Time, nil
case *gtime.Time:
// for gtime type, needs to get time.Time
if itemValue != nil {
return itemValue.Time, nil
}
// If the time is zero, it then updates it to nil,
// which will insert/update the value to database as "null".
if itemValue == nil || itemValue.IsZero() {
return nil, nil
}
case decimal.Decimal:
return itemValue, nil
case *decimal.Decimal:
if itemValue != nil {
return *itemValue, nil
}
return nil, nil
default:
// if the other type implements valuer for the driver package
// the converted result is used
// otherwise the interface data is committed
valuer, ok := itemValue.(driver.Valuer)
if !ok {
return itemValue, nil
}
convertedValue, err := valuer.Value()
if err != nil {
return nil, err
}
return convertedValue, nil
}
return fieldValue, nil
}

View File

@ -0,0 +1,19 @@
// 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 clickhouse
import (
"context"
"github.com/gogf/gf/v2/database/gdb"
)
// DoCommit commits current sql and arguments to underlying sql driver.
func (d *Driver) DoCommit(ctx context.Context, in gdb.DoCommitInput) (out gdb.DoCommitOutput, err error) {
ctx = d.InjectIgnoreResult(ctx)
return d.Core.DoCommit(ctx, in)
}

View File

@ -0,0 +1,20 @@
// 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 clickhouse
import (
"context"
"database/sql"
"github.com/gogf/gf/v2/database/gdb"
)
// DoDelete does "DELETE FROM ... " statement for the table.
func (d *Driver) DoDelete(ctx context.Context, link gdb.Link, table string, condition string, args ...interface{}) (result sql.Result, err error) {
ctx = d.injectNeedParsedSql(ctx)
return d.Core.DoDelete(ctx, link, table, condition, args...)
}

View File

@ -0,0 +1,85 @@
// 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 clickhouse
import (
"context"
"fmt"
"strings"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/text/gregex"
)
// DoFilter handles the sql before posts it to database.
func (d *Driver) DoFilter(
ctx context.Context, link gdb.Link, originSql string, args []interface{},
) (newSql string, newArgs []interface{}, err error) {
if len(args) == 0 {
return originSql, args, nil
}
// Convert placeholder char '?' to string "$x".
var index int
originSql, _ = gregex.ReplaceStringFunc(`\?`, originSql, func(s string) string {
index++
return fmt.Sprintf(`$%d`, index)
})
// Only SQL generated through the framework is processed.
if !d.getNeedParsedSqlFromCtx(ctx) {
return originSql, args, nil
}
// replace STD SQL to Clickhouse SQL grammar
modeRes, err := gregex.MatchString(filterTypePattern, strings.TrimSpace(originSql))
if err != nil {
return "", nil, err
}
if len(modeRes) == 0 {
return originSql, args, nil
}
// Only delete/ UPDATE statements require filter
switch strings.ToUpper(modeRes[0]) {
case "UPDATE":
// MySQL eg: UPDATE table_name SET field1=new-value1, field2=new-value2 [WHERE Clause]
// Clickhouse eg: ALTER TABLE [db.]table UPDATE column1 = expr1 [, ...] WHERE filter_expr
newSql, err = gregex.ReplaceStringFuncMatch(
updateFilterPattern, originSql,
func(s []string) string {
return fmt.Sprintf("ALTER TABLE %s UPDATE", s[1])
},
)
if err != nil {
return "", nil, err
}
return newSql, args, nil
case "DELETE":
// MySQL eg: DELETE FROM table_name [WHERE Clause]
// Clickhouse eg: ALTER TABLE [db.]table [ON CLUSTER cluster] DELETE WHERE filter_expr
newSql, err = gregex.ReplaceStringFuncMatch(
deleteFilterPattern, originSql,
func(s []string) string {
return fmt.Sprintf("ALTER TABLE %s DELETE", s[1])
},
)
if err != nil {
return "", nil, err
}
return newSql, args, nil
}
return originSql, args, nil
}
func (d *Driver) getNeedParsedSqlFromCtx(ctx context.Context) bool {
if ctx.Value(needParsedSqlInCtx) != nil {
return true
}
return false
}

View File

@ -0,0 +1,72 @@
// 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 clickhouse
import (
"context"
"database/sql"
"fmt"
"strings"
"github.com/gogf/gf/v2/database/gdb"
)
// DoInsert inserts or updates data forF given table.
func (d *Driver) DoInsert(
ctx context.Context, link gdb.Link, table string, list gdb.List, option gdb.DoInsertOption,
) (result sql.Result, err error) {
var (
keys []string // Field names.
valueHolder = make([]string, 0)
)
// Handle the field names and placeholders.
for k := range list[0] {
keys = append(keys, k)
valueHolder = append(valueHolder, "?")
}
// Prepare the batch result pointer.
var (
charL, charR = d.Core.GetChars()
keysStr = charL + strings.Join(keys, charR+","+charL) + charR
holderStr = strings.Join(valueHolder, ",")
tx gdb.TX
stmt *gdb.Stmt
)
tx, err = d.Core.Begin(ctx)
if err != nil {
return
}
// It here uses defer to guarantee transaction be committed or roll-backed.
defer func() {
if err == nil {
_ = tx.Commit()
} else {
_ = tx.Rollback()
}
}()
stmt, err = tx.Prepare(fmt.Sprintf(
"INSERT INTO %s(%s) VALUES (%s)",
d.QuotePrefixTableName(table), keysStr,
holderStr,
))
if err != nil {
return
}
for i := 0; i < len(list); i++ {
// Values that will be committed to underlying database driver.
params := make([]interface{}, 0)
for _, k := range keys {
params = append(params, list[i][k])
}
// Prepare is allowed to execute only once in a transaction opened by clickhouse
result, err = stmt.ExecContext(ctx, params...)
if err != nil {
return
}
}
return
}

View File

@ -0,0 +1,20 @@
// 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 clickhouse
import (
"context"
"database/sql"
"github.com/gogf/gf/v2/database/gdb"
)
// DoUpdate does "UPDATE ... " statement for the table.
func (d *Driver) DoUpdate(ctx context.Context, link gdb.Link, table string, data interface{}, condition string, args ...interface{}) (result sql.Result, err error) {
ctx = d.injectNeedParsedSql(ctx)
return d.Core.DoUpdate(ctx, link, table, data, condition, args...)
}

View File

@ -0,0 +1,27 @@
// 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 clickhouse
import (
"context"
"database/sql"
)
// InsertIgnore Other queries for modifying data parts are not supported: REPLACE, MERGE, UPSERT, INSERT UPDATE.
func (d *Driver) InsertIgnore(ctx context.Context, table string, data interface{}, batch ...int) (sql.Result, error) {
return nil, errUnsupportedInsertIgnore
}
// InsertAndGetId Other queries for modifying data parts are not supported: REPLACE, MERGE, UPSERT, INSERT UPDATE.
func (d *Driver) InsertAndGetId(ctx context.Context, table string, data interface{}, batch ...int) (int64, error) {
return 0, errUnsupportedInsertGetId
}
// Replace Other queries for modifying data parts are not supported: REPLACE, MERGE, UPSERT, INSERT UPDATE.
func (d *Driver) Replace(ctx context.Context, table string, data interface{}, batch ...int) (sql.Result, error) {
return nil, errUnsupportedReplace
}

View File

@ -0,0 +1,63 @@
// 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 clickhouse
import (
"database/sql"
"fmt"
"net/url"
"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/text/gregex"
)
// Open creates and returns an underlying sql.DB object for clickhouse.
func (d *Driver) Open(config *gdb.ConfigNode) (db *sql.DB, err error) {
source := config.Link
// clickhouse://username:password@host1:9000,host2:9000/database?dial_timeout=200ms&max_execution_time=60
if config.Link != "" {
// ============================================================================
// Deprecated from v2.2.0.
// ============================================================================
// Custom changing the schema in runtime.
if config.Name != "" {
source, _ = gregex.ReplaceString(replaceSchemaPattern, "@$1/"+config.Name, config.Link)
} else {
// If no schema, the link is matched for replacement
dbName, _ := gregex.MatchString(replaceSchemaPattern, config.Link)
if len(dbName) > 0 {
config.Name = dbName[len(dbName)-1]
}
}
} else {
if config.Pass != "" {
source = fmt.Sprintf(
"clickhouse://%s:%s@%s:%s/%s?debug=%t",
config.User, url.PathEscape(config.Pass),
config.Host, config.Port, config.Name, config.Debug,
)
} else {
source = fmt.Sprintf(
"clickhouse://%s@%s:%s/%s?debug=%t",
config.User, config.Host, config.Port, config.Name, config.Debug,
)
}
if config.Extra != "" {
source = fmt.Sprintf("%s&%s", source, config.Extra)
}
}
if db, err = sql.Open(driverName, source); err != nil {
err = gerror.WrapCodef(
gcode.CodeDbOperationError, err,
`sql.Open failed for driver "%s" by source "%s"`, driverName, source,
)
return nil, err
}
return
}

View File

@ -0,0 +1,41 @@
// 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 clickhouse
import (
"database/sql"
"fmt"
"github.com/ClickHouse/clickhouse-go/v2"
)
// PingMaster pings the master node to check authentication or keeps the connection alive.
func (d *Driver) PingMaster() error {
conn, err := d.Master()
if err != nil {
return err
}
return d.ping(conn)
}
// PingSlave pings the slave node to check authentication or keeps the connection alive.
func (d *Driver) PingSlave() error {
conn, err := d.Slave()
if err != nil {
return err
}
return d.ping(conn)
}
// ping Returns the Clickhouse specific error.
func (d *Driver) ping(conn *sql.DB) error {
err := conn.Ping()
if exception, ok := err.(*clickhouse.Exception); ok {
return fmt.Errorf("[%d]%s", exception.Code, exception.Message)
}
return err
}

View File

@ -0,0 +1,70 @@
// 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 clickhouse
import (
"context"
"fmt"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/text/gregex"
"github.com/gogf/gf/v2/util/gutil"
)
const (
tableFieldsColumns = `name,position,default_expression,comment,type,is_in_partition_key,is_in_sorting_key,is_in_primary_key,is_in_sampling_key`
)
// TableFields retrieves and returns the fields' information of specified table of current schema.
// Also see DriverMysql.TableFields.
func (d *Driver) TableFields(ctx context.Context, table string, schema ...string) (fields map[string]*gdb.TableField, err error) {
var (
result gdb.Result
link gdb.Link
usedSchema = gutil.GetOrDefaultStr(d.GetSchema(), schema...)
)
if link, err = d.SlaveLink(usedSchema); err != nil {
return nil, err
}
var (
getColumnsSql = fmt.Sprintf(
"select %s from `system`.columns c where `table` = '%s'",
tableFieldsColumns, table,
)
)
result, err = d.DoSelect(ctx, link, getColumnsSql)
if err != nil {
return nil, err
}
fields = make(map[string]*gdb.TableField)
for _, m := range result {
var (
isNull = false
fieldType = m["type"].String()
)
// in clickhouse , field type like is Nullable(int)
fieldsResult, _ := gregex.MatchString(`^Nullable\((.*?)\)`, fieldType)
if len(fieldsResult) == 2 {
isNull = true
fieldType = fieldsResult[1]
}
position := m["position"].Int()
if result[0]["position"].Int() != 0 {
position -= 1
}
fields[m["name"].String()] = &gdb.TableField{
Index: position,
Name: m["name"].String(),
Default: m["default_expression"].Val(),
Comment: m["comment"].String(),
// Key: m["Key"].String(),
Type: fieldType,
Null: isNull,
}
}
return fields, nil
}

View File

@ -0,0 +1,36 @@
// 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 clickhouse
import (
"context"
"fmt"
"github.com/gogf/gf/v2/database/gdb"
)
const (
tablesSqlTmp = "select name from `system`.tables where database = '%s'"
)
// Tables retrieves and returns the tables of current schema.
// It's mainly used in cli tool chain for automatically generating the models.
func (d *Driver) Tables(ctx context.Context, schema ...string) (tables []string, err error) {
var result gdb.Result
link, err := d.SlaveLink(schema...)
if err != nil {
return nil, err
}
result, err = d.DoSelect(ctx, link, fmt.Sprintf(tablesSqlTmp, d.GetConfig().Name))
if err != nil {
return
}
for _, m := range result {
tables = append(tables, m["name"].String())
}
return
}

View File

@ -0,0 +1,23 @@
// 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 clickhouse
import (
"context"
"github.com/gogf/gf/v2/database/gdb"
)
// Begin starts and returns the transaction object.
func (d *Driver) Begin(ctx context.Context) (tx gdb.TX, err error) {
return nil, errUnsupportedBegin
}
// Transaction wraps the transaction logic using function `f`.
func (d *Driver) Transaction(ctx context.Context, f func(ctx context.Context, tx gdb.TX) error) error {
return errUnsupportedTransaction
}

View File

@ -4,7 +4,7 @@ go 1.18
require (
github.com/ClickHouse/clickhouse-go/v2 v2.0.15
github.com/gogf/gf/v2 v2.6.0
github.com/gogf/gf/v2 v2.6.3
github.com/google/uuid v1.3.0
github.com/shopspring/decimal v1.3.1
)

View File

@ -8,24 +8,10 @@
package dm
import (
"context"
"database/sql"
"fmt"
"net/url"
"strings"
"time"
_ "gitee.com/chunanyong/dm"
"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/text/gregex"
"github.com/gogf/gf/v2/text/gstr"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
type Driver struct {
@ -61,379 +47,7 @@ func (d *Driver) New(core *gdb.Core, node *gdb.ConfigNode) (gdb.DB, error) {
}, nil
}
// Open creates and returns an underlying sql.DB object for pgsql.
func (d *Driver) Open(config *gdb.ConfigNode) (db *sql.DB, err error) {
var (
source string
underlyingDriverName = "dm"
)
if config.Name == "" {
return nil, fmt.Errorf(
`dm.Open failed for driver "%s" without DB Name`, underlyingDriverName,
)
}
// Data Source Name of DM8:
// dm://userName:password@ip:port/dbname
source = fmt.Sprintf(
"dm://%s:%s@%s:%s/%s?charset=%s&schema=%s",
config.User, config.Pass, config.Host, config.Port, config.Name, config.Charset, config.Name,
)
// Demo of timezone setting:
// &loc=Asia/Shanghai
if config.Timezone != "" {
if strings.Contains(config.Timezone, "/") {
config.Timezone = url.QueryEscape(config.Timezone)
}
source = fmt.Sprintf("%s&loc%s", source, config.Timezone)
}
if config.Extra != "" {
source = fmt.Sprintf("%s&%s", source, config.Extra)
}
if db, err = sql.Open(underlyingDriverName, source); err != nil {
err = gerror.WrapCodef(
gcode.CodeDbOperationError, err,
`dm.Open failed for driver "%s" by source "%s"`, underlyingDriverName, source,
)
return nil, err
}
return
}
// GetChars returns the security char for this type of database.
func (d *Driver) GetChars() (charLeft string, charRight string) {
return quoteChar, quoteChar
}
// Tables retrieves and returns the tables of current schema.
// It's mainly used in cli tool chain for automatically generating the models.
func (d *Driver) Tables(ctx context.Context, schema ...string) (tables []string, err error) {
var result gdb.Result
// When schema is empty, return the default link
link, err := d.SlaveLink(schema...)
if err != nil {
return nil, err
}
// The link has been distinguished and no longer needs to judge the owner
result, err = d.DoSelect(
ctx, link, `SELECT * FROM ALL_TABLES`,
)
if err != nil {
return
}
for _, m := range result {
if v, ok := m["IOT_NAME"]; ok {
tables = append(tables, v.String())
}
}
return
}
// TableFields retrieves and returns the fields' information of specified table of current schema.
func (d *Driver) TableFields(ctx context.Context, table string, schema ...string) (fields map[string]*gdb.TableField, err error) {
var (
result gdb.Result
link gdb.Link
// When no schema is specified, the configuration item is returned by default
usedSchema = gutil.GetOrDefaultStr(d.GetSchema(), schema...)
)
// When usedSchema is empty, return the default link
if link, err = d.SlaveLink(usedSchema); err != nil {
return nil, err
}
// The link has been distinguished and no longer needs to judge the owner
result, err = d.DoSelect(
ctx, link,
fmt.Sprintf(
`SELECT * FROM ALL_TAB_COLUMNS WHERE Table_Name= '%s' AND OWNER = '%s'`,
strings.ToUpper(table),
strings.ToUpper(d.GetSchema()),
),
)
if err != nil {
return nil, err
}
fields = make(map[string]*gdb.TableField)
for i, m := range result {
// m[NULLABLE] returns "N" "Y"
// "N" means not null
// "Y" means could be null
var nullable bool
if m["NULLABLE"].String() != "N" {
nullable = true
}
fields[m["COLUMN_NAME"].String()] = &gdb.TableField{
Index: i,
Name: m["COLUMN_NAME"].String(),
Type: m["DATA_TYPE"].String(),
Null: nullable,
Default: m["DATA_DEFAULT"].Val(),
// Key: m["Key"].String(),
// Extra: m["Extra"].String(),
// Comment: m["Comment"].String(),
}
}
return fields, nil
}
// ConvertValueForField converts value to the type of the record field.
func (d *Driver) ConvertValueForField(ctx context.Context, fieldType string, fieldValue interface{}) (interface{}, error) {
switch itemValue := fieldValue.(type) {
// dm does not support time.Time, it so here converts it to time string that it supports.
case time.Time:
// If the time is zero, it then updates it to nil,
// which will insert/update the value to database as "null".
if itemValue.IsZero() {
return nil, nil
}
return gtime.New(itemValue).String(), nil
// dm does not support time.Time, it so here converts it to time string that it supports.
case *time.Time:
// If the time is zero, it then updates it to nil,
// which will insert/update the value to database as "null".
if itemValue == nil || itemValue.IsZero() {
return nil, nil
}
return gtime.New(itemValue).String(), nil
}
return fieldValue, nil
}
// DoFilter deals with the sql string before commits it to underlying sql driver.
func (d *Driver) DoFilter(ctx context.Context, link gdb.Link, sql string, args []interface{}) (newSql string, newArgs []interface{}, err error) {
// There should be no need to capitalize, because it has been done from field processing before
newSql, _ = gregex.ReplaceString(`["\n\t]`, "", sql)
newSql = gstr.ReplaceI(gstr.ReplaceI(newSql, "GROUP_CONCAT", "LISTAGG"), "SEPARATOR", ",")
// TODO The current approach is too rough. We should deal with the GROUP_CONCAT function and the parsing of the index field from within the select from match.
// GROUP_CONCAT DM does not approve; index cannot be used as a query column name, and security characters need to be added, such as "index"
l, r := d.GetChars()
newSql = gstr.ReplaceI(newSql, "INDEX", l+"INDEX"+r)
// TODO i tried to do but it never work
// array, err := gregex.MatchAllString(`SELECT (.*INDEX.*) FROM .*`, newSql)
// g.Dump("err:", err)
// g.Dump("array:", array)
// g.Dump("array:", array[0][1])
// newSql, err = gregex.ReplaceString(`SELECT (.*INDEX.*) FROM .*`, l+"INDEX"+r, newSql)
// g.Dump("err:", err)
// g.Dump("newSql:", newSql)
// re, err := regexp.Compile(`.*SELECT (.*INDEX.*) FROM .*`)
// newSql = re.ReplaceAllStringFunc(newSql, func(data string) string {
// fmt.Println("data:", data)
// return data
// })
return d.Core.DoFilter(
ctx,
link,
newSql,
args,
)
}
// TODO I originally wanted to only convert keywords in select
// 但是我发现 DoQuery 中会对 sql 会对 " " 达梦的安全字符 进行 / 转义,最后还是导致达梦无法正常解析
// However, I found that DoQuery() will perform / escape on sql with " " Dameng's safe characters, which ultimately caused Dameng to be unable to parse normally.
// But processing in DoFilter() is OK
// func (d *Driver) DoQuery(ctx context.Context, link gdb.Link, sql string, args ...interface{}) (gdb.Result, error) {
// l, r := d.GetChars()
// new := gstr.ReplaceI(sql, "INDEX", l+"INDEX"+r)
// g.Dump("new:", new)
// return d.Core.DoQuery(
// ctx,
// link,
// new,
// args,
// )
// }
// DoInsert inserts or updates data forF given table.
func (d *Driver) DoInsert(
ctx context.Context, link gdb.Link, table string, list gdb.List, option gdb.DoInsertOption,
) (result sql.Result, err error) {
switch option.InsertOption {
case gdb.InsertOptionReplace:
// TODO:: Should be Supported
return nil, gerror.NewCode(
gcode.CodeNotSupported, `Replace operation is not supported by dm driver`,
)
case gdb.InsertOptionSave:
// This syntax currently only supports design tables whose primary key is ID.
listLength := len(list)
if listLength == 0 {
return nil, gerror.NewCode(
gcode.CodeInvalidRequest, `Save operation list is empty by dm driver`,
)
}
var (
keysSort []string
charL, charR = d.GetChars()
)
// Column names need to be aligned in the syntax
for k := range list[0] {
keysSort = append(keysSort, k)
}
var char = struct {
charL string
charR string
valueCharL string
valueCharR string
duplicateKey string
keys []string
}{
charL: charL,
charR: charR,
valueCharL: "'",
valueCharR: "'",
// TODO:: Need to dynamically set the primary key of the table
duplicateKey: "ID",
keys: keysSort,
}
// insertKeys: Handle valid keys that need to be inserted and updated
// insertValues: Handle values that need to be inserted
// updateValues: Handle values that need to be updated
// queryValues: Handle only one insert with column name
insertKeys, insertValues, updateValues, queryValues := parseValue(list[0], char)
// unionValues: Handling values that need to be inserted and updated
unionValues := parseUnion(list[1:], char)
batchResult := new(gdb.SqlResult)
// parseSql():
// MERGE INTO {{table}} T1
// USING ( SELECT {{queryValues}} FROM DUAL
// {{unionValues}} ) T2
// ON (T1.{{duplicateKey}} = T2.{{duplicateKey}})
// WHEN NOT MATCHED THEN
// INSERT {{insertKeys}} VALUES {{insertValues}}
// WHEN MATCHED THEN
// UPDATE SET {{updateValues}}
sqlStr := parseSql(
insertKeys, insertValues, updateValues, queryValues, unionValues, table, char.duplicateKey,
)
r, err := d.DoExec(ctx, link, sqlStr)
if err != nil {
return r, err
}
if n, err := r.RowsAffected(); err != nil {
return r, err
} else {
batchResult.Result = r
batchResult.Affected += n
}
return batchResult, nil
}
return d.Core.DoInsert(ctx, link, table, list, option)
}
func parseValue(listOne gdb.Map, char struct {
charL string
charR string
valueCharL string
valueCharR string
duplicateKey string
keys []string
}) (insertKeys []string, insertValues []string, updateValues []string, queryValues []string) {
for _, column := range char.keys {
if listOne[column] == nil {
// remove unassigned struct object
continue
}
insertKeys = append(insertKeys, char.charL+column+char.charR)
insertValues = append(insertValues, "T2."+char.charL+column+char.charR)
if column != char.duplicateKey {
updateValues = append(
updateValues,
fmt.Sprintf(`T1.%s = T2.%s`, char.charL+column+char.charR, char.charL+column+char.charR),
)
}
saveValue := gconv.String(listOne[column])
queryValues = append(
queryValues,
fmt.Sprintf(
char.valueCharL+"%s"+char.valueCharR+" AS "+char.charL+"%s"+char.charR,
saveValue, column,
),
)
}
return
}
func parseUnion(list gdb.List, char struct {
charL string
charR string
valueCharL string
valueCharR string
duplicateKey string
keys []string
}) (unionValues []string) {
for _, mapper := range list {
var saveValue []string
for _, column := range char.keys {
if mapper[column] == nil {
continue
}
// va := reflect.ValueOf(mapper[column])
// ty := reflect.TypeOf(mapper[column])
// switch ty.Kind() {
// case reflect.String:
// saveValue = append(saveValue, char.valueCharL+va.String()+char.valueCharR)
// case reflect.Int:
// saveValue = append(saveValue, strconv.FormatInt(va.Int(), 10))
// case reflect.Int64:
// saveValue = append(saveValue, strconv.FormatInt(va.Int(), 10))
// default:
// // The fish has no chance getting here.
// // Nothing to do.
// }
saveValue = append(saveValue,
fmt.Sprintf(
char.valueCharL+"%s"+char.valueCharR,
gconv.String(mapper[column]),
))
}
unionValues = append(
unionValues,
fmt.Sprintf(`UNION ALL SELECT %s FROM DUAL`, strings.Join(saveValue, ",")),
)
}
return
}
func parseSql(
insertKeys, insertValues, updateValues, queryValues, unionValues []string, table, duplicateKey string,
) (sqlStr string) {
var (
queryValueStr = strings.Join(queryValues, ",")
unionValueStr = strings.Join(unionValues, " ")
insertKeyStr = strings.Join(insertKeys, ",")
insertValueStr = strings.Join(insertValues, ",")
updateValueStr = strings.Join(updateValues, ",")
pattern = gstr.Trim(`
MERGE INTO %s T1 USING (SELECT %s FROM DUAL %s) T2 ON %s
WHEN NOT MATCHED
THEN
INSERT(%s) VALUES (%s)
WHEN MATCHED
THEN
UPDATE SET %s;
COMMIT;
`)
)
return fmt.Sprintf(
pattern,
table, queryValueStr, unionValueStr,
fmt.Sprintf("(T1.%s = T2.%s)", duplicateKey, duplicateKey),
insertKeyStr, insertValueStr, updateValueStr,
)
}

View File

@ -0,0 +1,40 @@
// 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 dm
import (
"context"
"time"
"github.com/gogf/gf/v2/os/gtime"
)
// ConvertValueForField converts value to the type of the record field.
func (d *Driver) ConvertValueForField(ctx context.Context, fieldType string, fieldValue interface{}) (interface{}, error) {
switch itemValue := fieldValue.(type) {
// dm does not support time.Time, it so here converts it to time string that it supports.
case time.Time:
// If the time is zero, it then updates it to nil,
// which will insert/update the value to database as "null".
if itemValue.IsZero() {
return nil, nil
}
return gtime.New(itemValue).String(), nil
// dm does not support time.Time, it so here converts it to time string that it supports.
case *time.Time:
// If the time is zero, it then updates it to nil,
// which will insert/update the value to database as "null".
if itemValue == nil || itemValue.IsZero() {
return nil, nil
}
return gtime.New(itemValue).String(), nil
}
return fieldValue, nil
}

View File

@ -0,0 +1,59 @@
// 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 dm
import (
"context"
"strings"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/text/gregex"
"github.com/gogf/gf/v2/text/gstr"
)
// DoFilter deals with the sql string before commits it to underlying sql driver.
func (d *Driver) DoFilter(
ctx context.Context, link gdb.Link, sql string, args []interface{},
) (newSql string, newArgs []interface{}, err error) {
// There should be no need to capitalize, because it has been done from field processing before
newSql, _ = gregex.ReplaceString(`["\n\t]`, "", sql)
newSql = gstr.ReplaceI(gstr.ReplaceI(newSql, "GROUP_CONCAT", "LISTAGG"), "SEPARATOR", ",")
// TODO The current approach is too rough. We should deal with the GROUP_CONCAT function and the
// parsing of the index field from within the select from match.
// GROUP_CONCAT DM does not approve; index cannot be used as a query column name, and security characters need to be added, such as "index"
l, r := d.GetChars()
if strings.Contains(newSql, "INDEX") || strings.Contains(newSql, "index") {
if !(strings.Contains(newSql, "_INDEX") || strings.Contains(newSql, "_index")) {
newSql = gstr.ReplaceI(newSql, "INDEX", l+"INDEX"+r)
}
}
// TODO i tried to do but it never work
// array, err := gregex.MatchAllString(`SELECT (.*INDEX.*) FROM .*`, newSql)
// g.Dump("err:", err)
// g.Dump("array:", array)
// g.Dump("array:", array[0][1])
// newSql, err = gregex.ReplaceString(`SELECT (.*INDEX.*) FROM .*`, l+"INDEX"+r, newSql)
// g.Dump("err:", err)
// g.Dump("newSql:", newSql)
// re, err := regexp.Compile(`.*SELECT (.*INDEX.*) FROM .*`)
// newSql = re.ReplaceAllStringFunc(newSql, func(data string) string {
// fmt.Println("data:", data)
// return data
// })
return d.Core.DoFilter(
ctx,
link,
newSql,
args,
)
}

View File

@ -0,0 +1,207 @@
// 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 dm
import (
"context"
"database/sql"
"fmt"
"strings"
"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/text/gstr"
"github.com/gogf/gf/v2/util/gconv"
)
// DoInsert inserts or updates data forF given table.
func (d *Driver) DoInsert(
ctx context.Context, link gdb.Link, table string, list gdb.List, option gdb.DoInsertOption,
) (result sql.Result, err error) {
switch option.InsertOption {
case gdb.InsertOptionReplace:
// TODO:: Should be Supported
return nil, gerror.NewCode(
gcode.CodeNotSupported, `Replace operation is not supported by dm driver`,
)
case gdb.InsertOptionSave:
// This syntax currently only supports design tables whose primary key is ID.
listLength := len(list)
if listLength == 0 {
return nil, gerror.NewCode(
gcode.CodeInvalidRequest, `Save operation list is empty by dm driver`,
)
}
var (
keysSort []string
charL, charR = d.GetChars()
)
// Column names need to be aligned in the syntax
for k := range list[0] {
keysSort = append(keysSort, k)
}
var char = struct {
charL string
charR string
valueCharL string
valueCharR string
duplicateKey string
keys []string
}{
charL: charL,
charR: charR,
valueCharL: "'",
valueCharR: "'",
// TODO:: Need to dynamically set the primary key of the table
duplicateKey: "ID",
keys: keysSort,
}
// insertKeys: Handle valid keys that need to be inserted and updated
// insertValues: Handle values that need to be inserted
// updateValues: Handle values that need to be updated
// queryValues: Handle only one insert with column name
insertKeys, insertValues, updateValues, queryValues := parseValue(list[0], char)
// unionValues: Handling values that need to be inserted and updated
unionValues := parseUnion(list[1:], char)
batchResult := new(gdb.SqlResult)
// parseSql():
// MERGE INTO {{table}} T1
// USING ( SELECT {{queryValues}} FROM DUAL
// {{unionValues}} ) T2
// ON (T1.{{duplicateKey}} = T2.{{duplicateKey}})
// WHEN NOT MATCHED THEN
// INSERT {{insertKeys}} VALUES {{insertValues}}
// WHEN MATCHED THEN
// UPDATE SET {{updateValues}}
sqlStr := parseSql(
insertKeys, insertValues, updateValues, queryValues, unionValues, table, char.duplicateKey,
)
r, err := d.DoExec(ctx, link, sqlStr)
if err != nil {
return r, err
}
if n, err := r.RowsAffected(); err != nil {
return r, err
} else {
batchResult.Result = r
batchResult.Affected += n
}
return batchResult, nil
}
return d.Core.DoInsert(ctx, link, table, list, option)
}
func parseValue(listOne gdb.Map, char struct {
charL string
charR string
valueCharL string
valueCharR string
duplicateKey string
keys []string
}) (insertKeys []string, insertValues []string, updateValues []string, queryValues []string) {
for _, column := range char.keys {
if listOne[column] == nil {
// remove unassigned struct object
continue
}
insertKeys = append(insertKeys, char.charL+column+char.charR)
insertValues = append(insertValues, "T2."+char.charL+column+char.charR)
if column != char.duplicateKey {
updateValues = append(
updateValues,
fmt.Sprintf(`T1.%s = T2.%s`, char.charL+column+char.charR, char.charL+column+char.charR),
)
}
saveValue := gconv.String(listOne[column])
queryValues = append(
queryValues,
fmt.Sprintf(
char.valueCharL+"%s"+char.valueCharR+" AS "+char.charL+"%s"+char.charR,
saveValue, column,
),
)
}
return
}
func parseUnion(list gdb.List, char struct {
charL string
charR string
valueCharL string
valueCharR string
duplicateKey string
keys []string
}) (unionValues []string) {
for _, mapper := range list {
var saveValue []string
for _, column := range char.keys {
if mapper[column] == nil {
continue
}
// va := reflect.ValueOf(mapper[column])
// ty := reflect.TypeOf(mapper[column])
// switch ty.Kind() {
// case reflect.String:
// saveValue = append(saveValue, char.valueCharL+va.String()+char.valueCharR)
// case reflect.Int:
// saveValue = append(saveValue, strconv.FormatInt(va.Int(), 10))
// case reflect.Int64:
// saveValue = append(saveValue, strconv.FormatInt(va.Int(), 10))
// default:
// // The fish has no chance getting here.
// // Nothing to do.
// }
saveValue = append(saveValue,
fmt.Sprintf(
char.valueCharL+"%s"+char.valueCharR,
gconv.String(mapper[column]),
))
}
unionValues = append(
unionValues,
fmt.Sprintf(`UNION ALL SELECT %s FROM DUAL`, strings.Join(saveValue, ",")),
)
}
return
}
func parseSql(
insertKeys, insertValues, updateValues, queryValues, unionValues []string, table, duplicateKey string,
) (sqlStr string) {
var (
queryValueStr = strings.Join(queryValues, ",")
unionValueStr = strings.Join(unionValues, " ")
insertKeyStr = strings.Join(insertKeys, ",")
insertValueStr = strings.Join(insertValues, ",")
updateValueStr = strings.Join(updateValues, ",")
pattern = gstr.Trim(`
MERGE INTO %s T1 USING (SELECT %s FROM DUAL %s) T2 ON %s
WHEN NOT MATCHED
THEN
INSERT(%s) VALUES (%s)
WHEN MATCHED
THEN
UPDATE SET %s;
COMMIT;
`)
)
return fmt.Sprintf(
pattern,
table, queryValueStr, unionValueStr,
fmt.Sprintf("(T1.%s = T2.%s)", duplicateKey, duplicateKey),
insertKeyStr, insertValueStr, updateValueStr,
)
}

View File

@ -0,0 +1,23 @@
// 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 dm
// TODO I originally wanted to only convert keywords in select
// 但是我发现 DoQuery 中会对 sql 会对 " " 达梦的安全字符 进行 / 转义,最后还是导致达梦无法正常解析
// However, I found that DoQuery() will perform / escape on sql with " " Dameng's safe characters, which ultimately caused Dameng to be unable to parse normally.
// But processing in DoFilter() is OK
// func (d *Driver) DoQuery(ctx context.Context, link gdb.Link, sql string, args ...interface{}) (gdb.Result, error) {
// l, r := d.GetChars()
// new := gstr.ReplaceI(sql, "INDEX", l+"INDEX"+r)
// g.Dump("new:", new)
// return d.Core.DoQuery(
// ctx,
// link,
// new,
// args,
// )
// }

View File

@ -0,0 +1,65 @@
// 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 dm
import (
"database/sql"
"fmt"
"net/url"
"strings"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
)
// Open creates and returns an underlying sql.DB object for pgsql.
func (d *Driver) Open(config *gdb.ConfigNode) (db *sql.DB, err error) {
var (
source string
underlyingDriverName = "dm"
)
if config.Name == "" {
return nil, fmt.Errorf(
`dm.Open failed for driver "%s" without DB Name`, underlyingDriverName,
)
}
// Data Source Name of DM8:
// dm://userName:password@ip:port/dbname
// dm://userName:password@DW/dbname?DW=(192.168.1.1:5236,192.168.1.2:5236)
var domain string
if config.Port != "" {
domain = fmt.Sprintf("%s:%s", config.Host, config.Port)
} else {
domain = config.Host
}
source = fmt.Sprintf(
"dm://%s:%s@%s/%s?charset=%s&schema=%s",
config.User, config.Pass, domain, config.Name, config.Charset, config.Name,
)
// Demo of timezone setting:
// &loc=Asia/Shanghai
if config.Timezone != "" {
if strings.Contains(config.Timezone, "/") {
config.Timezone = url.QueryEscape(config.Timezone)
}
source = fmt.Sprintf("%s&loc%s", source, config.Timezone)
}
if config.Extra != "" {
source = fmt.Sprintf("%s&%s", source, config.Extra)
}
if db, err = sql.Open(underlyingDriverName, source); err != nil {
err = gerror.WrapCodef(
gcode.CodeDbOperationError, err,
`dm.Open failed for driver "%s" by source "%s"`, underlyingDriverName, source,
)
return nil, err
}
return
}

View File

@ -0,0 +1,70 @@
// 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 dm
import (
"context"
"fmt"
"strings"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/util/gutil"
)
const (
tableFieldsSqlTmp = `SELECT * FROM ALL_TAB_COLUMNS WHERE Table_Name= '%s' AND OWNER = '%s'`
)
// TableFields retrieves and returns the fields' information of specified table of current schema.
func (d *Driver) TableFields(
ctx context.Context, table string, schema ...string,
) (fields map[string]*gdb.TableField, err error) {
var (
result gdb.Result
link gdb.Link
// When no schema is specified, the configuration item is returned by default
usedSchema = gutil.GetOrDefaultStr(d.GetSchema(), schema...)
)
// When usedSchema is empty, return the default link
if link, err = d.SlaveLink(usedSchema); err != nil {
return nil, err
}
// The link has been distinguished and no longer needs to judge the owner
result, err = d.DoSelect(
ctx, link,
fmt.Sprintf(
tableFieldsSqlTmp,
strings.ToUpper(table),
strings.ToUpper(d.GetSchema()),
),
)
if err != nil {
return nil, err
}
fields = make(map[string]*gdb.TableField)
for i, m := range result {
// m[NULLABLE] returns "N" "Y"
// "N" means not null
// "Y" means could be null
var nullable bool
if m["NULLABLE"].String() != "N" {
nullable = true
}
fields[m["COLUMN_NAME"].String()] = &gdb.TableField{
Index: i,
Name: m["COLUMN_NAME"].String(),
Type: m["DATA_TYPE"].String(),
Null: nullable,
Default: m["DATA_DEFAULT"].Val(),
// Key: m["Key"].String(),
// Extra: m["Extra"].String(),
// Comment: m["Comment"].String(),
}
}
return fields, nil
}

View File

@ -0,0 +1,39 @@
// 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 dm
import (
"context"
"github.com/gogf/gf/v2/database/gdb"
)
const (
tablesSqlTmp = `SELECT * FROM ALL_TABLES`
)
// Tables retrieves and returns the tables of current schema.
// It's mainly used in cli tool chain for automatically generating the models.
func (d *Driver) Tables(ctx context.Context, schema ...string) (tables []string, err error) {
var result gdb.Result
// When schema is empty, return the default link
link, err := d.SlaveLink(schema...)
if err != nil {
return nil, err
}
// The link has been distinguished and no longer needs to judge the owner
result, err = d.DoSelect(ctx, link, tablesSqlTmp)
if err != nil {
return
}
for _, m := range result {
if v, ok := m["IOT_NAME"]; ok {
tables = append(tables, v.String())
}
}
return
}

View File

@ -27,13 +27,11 @@ func Test_DB_Ping(t *testing.T) {
}
func TestTables(t *testing.T) {
tables := []string{"A_tables", "A_tables2"}
for _, v := range tables {
createInitTable(v)
}
gtest.C(t, func(t *gtest.T) {
tables := []string{"A_tables", "A_tables2"}
for _, v := range tables {
createInitTable(v)
// createTable(v)
}
result, err := db.Tables(ctx)
gtest.Assert(err, nil)
@ -63,13 +61,31 @@ func TestTables(t *testing.T) {
})
}
func TestTableFields(t *testing.T) {
// The test scenario index of this test case (exact matching field) is a keyword in the Dameng database and cannot exist as a field name.
// If the data structure previously migrated from mysql has an index (completely matching field), it will also be allowed.
// However, when processing the index (completely matching field), the adapter will automatically add security character
// In principle, such problems will not occur if you directly use Dameng database initialization instead of migrating the data structure from mysql.
// If so, the adapter has also taken care of it.
func TestTablesFalse(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
tables := []string{"A_tables", "A_tables2"}
for _, v := range tables {
_, err := createTableFalse(v)
gtest.Assert(err, fmt.Errorf("createTableFalse"))
// createTable(v)
}
})
}
func TestTableFields(t *testing.T) {
tables := "A_tables"
createInitTable(tables)
gtest.C(t, func(t *gtest.T) {
tables := "A_tables"
var expect = map[string][]interface{}{
"ID": {"BIGINT", false},
"ACCOUNT_NAME": {"VARCHAR", false},
"PWD_RESET": {"TINYINT", false},
"ATTR_INDEX": {"INT", true},
"DELETED": {"INT", false},
"CREATED_TIME": {"TIMESTAMP", false},
}
@ -98,8 +114,9 @@ func TestTableFields(t *testing.T) {
}
func Test_DB_Query(t *testing.T) {
tableName := "A_tables"
createInitTable(tableName)
gtest.C(t, func(t *gtest.T) {
tableName := "A_tables"
// createTable(tableName)
_, err := db.Query(ctx, fmt.Sprintf("SELECT * from %s", tableName))
t.AssertNil(err)
@ -121,16 +138,18 @@ func Test_DB_Query(t *testing.T) {
}
func TestModelSave(t *testing.T) {
table := "A_tables"
createInitTable(table)
gtest.C(t, func(t *gtest.T) {
// createTable("A_tables")
data := []User{
{
ID: 100,
AccountName: "user_100",
AttrIndex: 100,
CreatedTime: time.Now(),
},
}
_, err := db.Model("A_tables").Data(data).Save()
_, err := db.Model(table).Data(data).Save()
gtest.Assert(err, nil)
data2 := []User{
@ -139,7 +158,7 @@ func TestModelSave(t *testing.T) {
AccountName: "user_101",
},
}
_, err = db.Model("A_tables").Data(&data2).Save()
_, err = db.Model(table).Data(&data2).Save()
gtest.Assert(err, nil)
data3 := []User{
@ -149,7 +168,7 @@ func TestModelSave(t *testing.T) {
PwdReset: 10,
},
}
_, err = db.Model("A_tables").Save(data3)
_, err = db.Model(table).Save(data3)
gtest.Assert(err, nil)
data4 := []User{
@ -159,63 +178,68 @@ func TestModelSave(t *testing.T) {
CreatedTime: time.Now(),
},
}
_, err = db.Model("A_tables").Save(&data4)
_, err = db.Model(table).Save(&data4)
gtest.Assert(err, nil)
// TODO:: Should be Supported 'Replace' Operation
// _, err = db.Schema(TestDbName).Replace(ctx, "DoInsert", data, 10)
// _, err = db.Schema(TestDBName).Replace(ctx, "DoInsert", data, 10)
// gtest.Assert(err, nil)
})
}
func TestModelInsert(t *testing.T) {
// g.Model.insert not lost default not null coloumn
table := "A_tables"
createInitTable(table)
gtest.C(t, func(t *gtest.T) {
// createTable("A_tables")
i := 200
data := User{
ID: int64(i),
AccountName: fmt.Sprintf(`A%dtwo`, i),
PwdReset: 0,
AttrIndex: 99,
// CreatedTime: time.Now(),
UpdatedTime: time.Now(),
}
// _, err := db.Schema(TestDbName).Model("A_tables").Data(data).Insert()
_, err := db.Model("A_tables").Insert(&data)
// _, err := db.Schema(TestDBName).Model(table).Data(data).Insert()
_, err := db.Model(table).Insert(&data)
gtest.Assert(err, nil)
})
gtest.C(t, func(t *gtest.T) {
// createTable("A_tables")
i := 201
data := User{
ID: int64(i),
AccountName: fmt.Sprintf(`A%dtwoONE`, i),
PwdReset: 1,
CreatedTime: time.Now(),
AttrIndex: 98,
// UpdatedTime: time.Now(),
}
// _, err := db.Schema(TestDbName).Model("A_tables").Data(data).Insert()
_, err := db.Model("A_tables").Data(&data).Insert()
// _, err := db.Schema(TestDBName).Model(table).Data(data).Insert()
_, err := db.Model(table).Data(&data).Insert()
gtest.Assert(err, nil)
})
}
func TestDBInsert(t *testing.T) {
table := "A_tables"
createInitTable("A_tables")
gtest.C(t, func(t *gtest.T) {
// createTable("A_tables")
i := 300
data := g.Map{
"ID": i,
"ACCOUNT_NAME": fmt.Sprintf(`A%dthress`, i),
"PWD_RESET": 3,
"ATTR_INDEX": 98,
}
_, err := db.Insert(ctx, "A_tables", &data)
_, err := db.Insert(ctx, table, &data)
gtest.Assert(err, nil)
})
}
func Test_DB_Exec(t *testing.T) {
createInitTable("A_tables")
gtest.C(t, func(t *gtest.T) {
_, err := db.Exec(ctx, "SELECT ? from dual", 1)
t.AssertNil(err)
@ -226,18 +250,18 @@ func Test_DB_Exec(t *testing.T) {
}
func Test_DB_Insert(t *testing.T) {
// table := createTable()
// defer dropTable(table)
table := "A_tables"
createInitTable(table)
gtest.C(t, func(t *gtest.T) {
// normal map
_, err := db.Insert(ctx, "A_tables", g.Map{
_, err := db.Insert(ctx, table, g.Map{
"ID": 1000,
"ACCOUNT_NAME": "map1",
"CREATED_TIME": gtime.Now(),
})
t.AssertNil(err)
result, err := db.Insert(ctx, "A_tables", g.Map{
result, err := db.Insert(ctx, table, g.Map{
"ID": "2000",
"ACCOUNT_NAME": "map2",
"CREATED_TIME": gtime.Now(),
@ -246,7 +270,7 @@ func Test_DB_Insert(t *testing.T) {
n, _ := result.RowsAffected()
t.Assert(n, 1)
result, err = db.Insert(ctx, "A_tables", g.Map{
result, err = db.Insert(ctx, table, g.Map{
"ID": 3000,
"ACCOUNT_NAME": "map3",
// "CREATED_TIME": gtime.Now(),
@ -256,7 +280,7 @@ func Test_DB_Insert(t *testing.T) {
t.Assert(n, 1)
// struct
result, err = db.Insert(ctx, "A_tables", User{
result, err = db.Insert(ctx, table, User{
ID: 4000,
AccountName: "struct_4",
// CreatedTime: timeStr,
@ -266,7 +290,7 @@ func Test_DB_Insert(t *testing.T) {
n, _ = result.RowsAffected()
t.Assert(n, 1)
ones, err := db.Model("A_tables").Where("ID", 4000).All()
ones, err := db.Model(table).Where("ID", 4000).All()
t.AssertNil(err)
t.Assert(ones[0]["ID"].Int(), 4000)
t.Assert(ones[0]["ACCOUNT_NAME"].String(), "struct_4")
@ -276,7 +300,7 @@ func Test_DB_Insert(t *testing.T) {
// *struct
timeStr := time.Now()
result, err = db.Insert(ctx, "A_tables", &User{
result, err = db.Insert(ctx, table, &User{
ID: 5000,
AccountName: "struct_5",
CreatedTime: timeStr,
@ -286,13 +310,13 @@ func Test_DB_Insert(t *testing.T) {
n, _ = result.RowsAffected()
t.Assert(n, 1)
one, err := db.Model("A_tables").Where("ID", 5000).One()
one, err := db.Model(table).Where("ID", 5000).One()
t.AssertNil(err)
t.Assert(one["ID"].Int(), 5000)
t.Assert(one["ACCOUNT_NAME"].String(), "struct_5")
// batch with Insert
r, err := db.Insert(ctx, "A_tables", g.Slice{
r, err := db.Insert(ctx, table, g.Slice{
g.Map{
"ID": 6000,
"ACCOUNT_NAME": "t6000",
@ -306,7 +330,7 @@ func Test_DB_Insert(t *testing.T) {
n, _ = r.RowsAffected()
t.Assert(n, 2)
one, err = db.Model("A_tables").Where("ID", 6000).One()
one, err = db.Model(table).Where("ID", 6000).One()
t.AssertNil(err)
t.Assert(one["ID"].Int(), 6000)
t.Assert(one["ACCOUNT_NAME"].String(), "t6000")
@ -314,8 +338,9 @@ func Test_DB_Insert(t *testing.T) {
}
func Test_DB_BatchInsert(t *testing.T) {
table := "A_tables"
createInitTable(table)
gtest.C(t, func(t *gtest.T) {
table := "A_tables"
r, err := db.Insert(ctx, table, g.List{
{
"ID": 400,
@ -334,9 +359,6 @@ func Test_DB_BatchInsert(t *testing.T) {
})
gtest.C(t, func(t *gtest.T) {
table := "A_tables"
// table := createTable()
// defer dropTable(table)
// []interface{}
r, err := db.Insert(ctx, table, g.Slice{
g.Map{
@ -357,9 +379,6 @@ func Test_DB_BatchInsert(t *testing.T) {
// batch insert map
gtest.C(t, func(t *gtest.T) {
table := "A_tables"
// table := createTable()
// defer dropTable(table)
result, err := db.Insert(ctx, table, g.Map{
"ID": 600,
"ACCOUNT_NAME": "600_batch_600",
@ -373,10 +392,9 @@ func Test_DB_BatchInsert(t *testing.T) {
func Test_DB_BatchInsert_Struct(t *testing.T) {
// batch insert struct
table := "A_tables"
createInitTable(table)
gtest.C(t, func(t *gtest.T) {
table := "A_tables"
// table := createTable()
// defer dropTable(table)
user := &User{
ID: 700,
AccountName: "BatchInsert_Struct_700",
@ -391,26 +409,25 @@ func Test_DB_BatchInsert_Struct(t *testing.T) {
func Test_DB_Update(t *testing.T) {
table := "A_tables"
// table := createInitTable()
createInitTable(table)
gtest.C(t, func(t *gtest.T) {
result, err := db.Update(ctx, table, "pwd_reset=7", "id=700")
result, err := db.Update(ctx, table, "pwd_reset=7", "id=7")
t.AssertNil(err)
n, _ := result.RowsAffected()
t.Assert(n, 1)
one, err := db.Model(table).Where("ID", 700).One()
one, err := db.Model(table).Where("ID", 7).One()
t.AssertNil(err)
t.Assert(one["ID"].Int(), 700)
t.Assert(one["ACCOUNT_NAME"].String(), "BatchInsert_Struct_700")
t.Assert(one["ID"].Int(), 7)
t.Assert(one["ACCOUNT_NAME"].String(), "name_7")
t.Assert(one["PWD_RESET"].String(), "7")
})
}
func Test_DB_GetAll(t *testing.T) {
table := "A_tables"
// table := createInitTable()
// defer dropTable(table)
createInitTable(table)
gtest.C(t, func(t *gtest.T) {
result, err := db.GetAll(ctx, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 1)
@ -459,41 +476,38 @@ func Test_DB_GetAll(t *testing.T) {
}
func Test_DB_GetOne(t *testing.T) {
// table := createInitTable()
table := "A_tables"
createInitTable(table)
gtest.C(t, func(t *gtest.T) {
record, err := db.GetOne(ctx, fmt.Sprintf("SELECT * FROM %s WHERE account_name=?", table), "struct_4")
record, err := db.GetOne(ctx, fmt.Sprintf("SELECT * FROM %s WHERE account_name=?", table), "name_4")
t.AssertNil(err)
t.Assert(record["ACCOUNT_NAME"].String(), "struct_4")
t.Assert(record["ACCOUNT_NAME"].String(), "name_4")
})
}
func Test_DB_GetValue(t *testing.T) {
table := "A_tables"
// table := createInitTable()
// defer dropTable(table)
createInitTable(table)
gtest.C(t, func(t *gtest.T) {
value, err := db.GetValue(ctx, fmt.Sprintf("SELECT id FROM %s WHERE account_name=?", table), "map2")
value, err := db.GetValue(ctx, fmt.Sprintf("SELECT id FROM %s WHERE account_name=?", table), "name_2")
t.AssertNil(err)
t.Assert(value.Int(), 2000)
t.Assert(value.Int(), 2)
})
}
func Test_DB_GetCount(t *testing.T) {
table := "A_tables"
// table := createInitTable()
// defer dropTable(table)
createInitTable(table)
gtest.C(t, func(t *gtest.T) {
count, err := db.GetCount(ctx, fmt.Sprintf("SELECT * FROM %s", table))
t.AssertNil(err)
t.Assert(count, 28)
t.Assert(count, 10)
})
}
func Test_DB_GetStruct(t *testing.T) {
table := "A_tables"
// table := createInitTable()
// defer dropTable(table)
createInitTable(table)
gtest.C(t, func(t *gtest.T) {
user := new(User)
err := db.GetScan(ctx, user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 3)
@ -502,33 +516,31 @@ func Test_DB_GetStruct(t *testing.T) {
})
gtest.C(t, func(t *gtest.T) {
user := new(User)
err := db.GetScan(ctx, user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 200)
err := db.GetScan(ctx, user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 2)
t.AssertNil(err)
t.Assert(user.AccountName, "A200two")
t.Assert(user.AccountName, "name_2")
})
}
func Test_DB_GetStructs(t *testing.T) {
table := "A_tables"
// table := createInitTable()
// defer dropTable(table)
createInitTable(table)
gtest.C(t, func(t *gtest.T) {
var users []User
err := db.GetScan(ctx, &users, fmt.Sprintf("SELECT * FROM %s WHERE id>?", table), 4000)
err := db.GetScan(ctx, &users, fmt.Sprintf("SELECT * FROM %s WHERE id>?", table), 4)
t.AssertNil(err)
t.Assert(users[0].ID, 5000)
t.Assert(users[1].ID, 6000)
t.Assert(users[2].ID, 6001)
t.Assert(users[0].AccountName, "struct_5")
t.Assert(users[1].AccountName, "t6000")
t.Assert(users[2].AccountName, "t6001")
t.Assert(users[0].ID, 5)
t.Assert(users[1].ID, 6)
t.Assert(users[2].ID, 7)
t.Assert(users[0].AccountName, "name_5")
t.Assert(users[1].AccountName, "name_6")
t.Assert(users[2].AccountName, "name_7")
})
}
func Test_DB_GetScan(t *testing.T) {
table := "A_tables"
// table := createInitTable()
// defer dropTable(table)
createInitTable(table)
gtest.C(t, func(t *gtest.T) {
user := new(User)
err := db.GetScan(ctx, user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 3)
@ -555,17 +567,17 @@ func Test_DB_GetScan(t *testing.T) {
}
func Test_DB_Delete(t *testing.T) {
// table := createInitTable()
// defer dropTable(table)
table := "A_tables"
createInitTable(table)
gtest.C(t, func(t *gtest.T) {
result, err := db.Delete(ctx, "A_tables", "id=32")
result, err := db.Delete(ctx, table, "id=32")
t.AssertNil(err)
n, _ := result.RowsAffected()
t.Assert(n, 0)
})
gtest.C(t, func(t *gtest.T) {
result, err := db.Model("A_tables").Where("id", 33).Delete()
result, err := db.Model(table).Where("id", 33).Delete()
t.AssertNil(err)
n, _ := result.RowsAffected()
t.Assert(n, 0)
@ -574,8 +586,7 @@ func Test_DB_Delete(t *testing.T) {
func Test_Empty_Slice_Argument(t *testing.T) {
table := "A_tables"
// table := createInitTable()
// defer dropTable(table)
createInitTable(table)
gtest.C(t, func(t *gtest.T) {
result, err := db.GetAll(ctx, fmt.Sprintf(`select * from %s where id in(?)`, table), g.Slice{})
t.AssertNil(err)

View File

@ -12,7 +12,6 @@ import (
"strings"
"time"
_ "gitee.com/chunanyong/dm"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/frame/g"
@ -21,38 +20,44 @@ import (
)
var (
db gdb.DB
dblink gdb.DB
dbErr gdb.DB
ctx context.Context
)
const (
db gdb.DB
dblink gdb.DB
dbErr gdb.DB
ctx context.Context
TableSize = 10
// TableName = "inf_group"
// TableNamePrefix = "t_"
// TestSchema = "SYSDBADP"
)
const (
TestDbIP = "127.0.0.1"
TestDbPort = "5236"
TestDbUser = "SYSDBA"
TestDbPass = "SYSDBA001"
TestDbName = "SYSDBA"
TestDbType = "dm"
TestDBHost = "127.0.0.1"
TestDBPort = "5236"
TestDBUser = "SYSDBA"
TestDBPass = "SYSDBA001"
TestDBName = "SYSDBA"
TestDBType = "dm"
TestCharset = "utf8"
)
type User struct {
ID int64 `orm:"id"`
AccountName string `orm:"account_name"`
PwdReset int64 `orm:"pwd_reset"`
AttrIndex int64 `orm:"attr_index"`
Enabled int64 `orm:"enabled"`
Deleted int64 `orm:"deleted"`
CreatedBy string `orm:"created_by"`
CreatedTime time.Time `orm:"created_time"`
UpdatedBy string `orm:"updated_by"`
UpdatedTime time.Time `orm:"updated_time"`
}
func init() {
node := gdb.ConfigNode{
Host: TestDbIP,
Port: TestDbPort,
User: TestDbUser,
Pass: TestDbPass,
Name: TestDbName,
Type: TestDbType,
Host: TestDBHost,
Port: TestDBPort,
User: TestDBUser,
Pass: TestDBPass,
Name: TestDBName,
Type: TestDBType,
Role: "master",
Charset: TestCharset,
Weight: 1,
@ -62,22 +67,23 @@ func init() {
UpdatedAt: "updated_time",
}
// todo
nodeLink := gdb.ConfigNode{
Type: TestDbType,
Name: TestDbName,
Type: TestDBType,
Name: TestDBName,
Link: fmt.Sprintf(
"dm:%s:%s@tcp(%s:%s)/%s?charset=%s",
TestDbUser, TestDbPass, TestDbIP, TestDbPort, TestDbName, TestCharset,
TestDBUser, TestDBPass, TestDBHost, TestDBPort, TestDBName, TestCharset,
),
}
nodeErr := gdb.ConfigNode{
Host: TestDbIP,
Port: TestDbPort,
User: TestDbUser,
Host: TestDBHost,
Port: TestDBPort,
User: TestDBUser,
Pass: "1234",
Name: TestDbName,
Type: TestDbType,
Name: TestDBName,
Type: TestDBType,
Role: "master",
Charset: TestCharset,
Weight: 1,
@ -107,6 +113,23 @@ func init() {
ctx = context.Background()
}
func dropTable(table string) {
count, err := db.GetCount(
ctx,
"SELECT COUNT(*) FROM all_tables WHERE owner = ? And table_name= ?", TestDBName, strings.ToUpper(table),
)
if err != nil {
gtest.Fatal(err)
}
if count == 0 {
return
}
if _, err := db.Exec(ctx, fmt.Sprintf("DROP TABLE %s", table)); err != nil {
gtest.Fatal(err)
}
}
func createTable(table ...string) (name string) {
if len(table) > 0 {
name = table[0]
@ -124,6 +147,7 @@ func createTable(table ...string) (name string) {
"PWD_RESET" TINYINT DEFAULT 0 NOT NULL,
"ENABLED" INT DEFAULT 1 NOT NULL,
"DELETED" INT DEFAULT 0 NOT NULL,
"ATTR_INDEX" INT DEFAULT 0 ,
"CREATED_BY" VARCHAR(32) DEFAULT '' NOT NULL,
"CREATED_TIME" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP() NOT NULL,
"UPDATED_BY" VARCHAR(32) DEFAULT '' NOT NULL,
@ -136,18 +160,6 @@ NOT CLUSTER PRIMARY KEY("ID")) STORAGE(ON "MAIN", CLUSTERBTR) ;
return
}
type User struct {
ID int64 `orm:"id"`
AccountName string `orm:"account_name"`
PwdReset int64 `orm:"pwd_reset"`
Enabled int64 `orm:"enabled"`
Deleted int64 `orm:"deleted"`
CreatedBy string `orm:"created_by"`
CreatedTime time.Time `orm:"created_time"`
UpdatedBy string `orm:"updated_by"`
UpdatedTime time.Time `orm:"updated_time"`
}
func createInitTable(table ...string) (name string) {
name = createTable(table...)
array := garray.New(true)
@ -156,10 +168,11 @@ func createInitTable(table ...string) (name string) {
"id": i,
"account_name": fmt.Sprintf(`name_%d`, i),
"pwd_reset": 0,
"attr_index": i,
"create_time": gtime.Now().String(),
})
}
result, err := db.Schema(TestDbName).Insert(context.Background(), name, array.Slice())
result, err := db.Schema(TestDBName).Insert(context.Background(), name, array.Slice())
gtest.Assert(err, nil)
n, e := result.RowsAffected()
@ -168,19 +181,34 @@ func createInitTable(table ...string) (name string) {
return
}
func dropTable(table string) {
count, err := db.GetCount(
ctx,
"SELECT COUNT(*) FROM USER_TABLES WHERE TABLE_NAME = ?", strings.ToUpper(table),
)
if err != nil {
gtest.Fatal(err)
func createTableFalse(table ...string) (name string, err error) {
if len(table) > 0 {
name = table[0]
} else {
name = fmt.Sprintf("random_%d", gtime.Timestamp())
}
if count == 0 {
return
}
if _, err := db.Exec(ctx, fmt.Sprintf("DROP TABLE %s", table)); err != nil {
gtest.Fatal(err)
dropTable(name)
if _, err := db.Exec(ctx, fmt.Sprintf(`
CREATE TABLE "%s"
(
"ID" BIGINT NOT NULL,
"ACCOUNT_NAME" VARCHAR(128) DEFAULT '' NOT NULL,
"PWD_RESET" TINYINT DEFAULT 0 NOT NULL,
"ENABLED" INT DEFAULT 1 NOT NULL,
"DELETED" INT DEFAULT 0 NOT NULL,
"INDEX" INT DEFAULT 0 ,
"ATTR_INDEX" INT DEFAULT 0 ,
"CREATED_BY" VARCHAR(32) DEFAULT '' NOT NULL,
"CREATED_TIME" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP() NOT NULL,
"UPDATED_BY" VARCHAR(32) DEFAULT '' NOT NULL,
"UPDATED_TIME" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP() NOT NULL,
NOT CLUSTER PRIMARY KEY("ID")) STORAGE(ON "MAIN", CLUSTERBTR) ;
`, name)); err != nil {
// gtest.Fatal(err)
return name, fmt.Errorf("createTableFalse")
}
return name, nil
}

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