-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb_test.go
159 lines (152 loc) · 4.22 KB
/
db_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/jasonlabz/gentol/datasource"
"github.com/jasonlabz/gentol/gormx"
"github.com/jasonlabz/gentol/metadata"
"log"
"reflect"
"regexp"
"runtime"
"strings"
"testing"
"time"
)
func TestNewOperator(t *testing.T) {
dbInfo := &gormx.Config{
DBName: "master",
DBType: gormx.DBTypeGreenplum,
Host: "127.0.0.1",
Port: 8432,
User: "postgres",
Password: "halojeff",
Database: "lg_server",
}
err := gormx.InitConfig(dbInfo)
if err != nil {
panic(err)
}
ds, _ := datasource.GetDS(gormx.DBTypeGreenplum)
tableMap, err := ds.GetTablesUnderDB(context.Background(), dbInfo.DBName)
colMap, err := ds.GetColumns(context.Background(), dbInfo.DBName)
tableColMap, err := ds.GetColumnsUnderTable(context.Background(), dbInfo.DBName, "public", []string{"user"})
fmt.Print(tableMap)
fmt.Print(colMap)
fmt.Print(tableColMap)
}
func TestPostgresOperator(t *testing.T) {
dbInfo := &gormx.Config{
DBName: "master",
DBType: gormx.DBTypeGreenplum,
Host: "127.0.0.1",
Port: 8432,
User: "postgres",
Password: "halojeff",
Database: "lg_server",
}
err := gormx.InitConfig(dbInfo)
if err != nil {
panic(err)
}
db, err := gormx.GetDB(dbInfo.DBName)
if err != nil {
panic(err)
}
columnTypes, err := db.Migrator().ColumnTypes("test.user1")
indexes, err := db.Migrator().GetIndexes("user1")
if err != nil {
panic(err)
}
fmt.Println(indexes)
fmt.Println(columnTypes)
}
func TestDemo(t *testing.T) {
baseName := metadata.GetFuncNamePath(metadata.LoadTpl)
t1, filename, t2, ok := runtime.Caller(0)
fmt.Print(t1)
fmt.Print(t2)
fmt.Print(ok)
//fmt.Print(ok)
fmt.Print(baseName)
fmt.Print(filename)
inValues := []bool{true, true, false}
bytes, _ := json.Marshal(inValues)
inValues1 := []int32{1, 3, 5}
bytes1, _ := json.Marshal(inValues1)
res := string(bytes)
res1 := string(bytes1)
fmt.Print(res)
fmt.Print(res1)
}
func TestStruct(t *testing.T) {
user := struct{}{}
val := reflect.ValueOf(user)
fmt.Println(val.Type().PkgPath())
for i := 0; i < val.Type().NumField(); i++ {
fmt.Println("gorm:" + val.Type().Field(i).Tag.Get("gorm"))
column := ""
gormValList := strings.Split(val.Type().Field(i).Tag.Get("gorm"), ";")
for _, item := range gormValList {
if strings.Contains(item, "column") {
column = strings.Split(item, ":")[1]
}
}
fmt.Println("column:" + column)
fmt.Println("json:" + val.Type().Field(i).Tag.Get("json"))
}
}
func Values(value any) string {
switch value.(type) {
case int, int8, int16, int32, int64, bool, float32, float64:
return fmt.Sprintf("%v", value)
default:
return fmt.Sprintf("'%v'", value)
}
}
func TransInCondition[T any](prefix string, values []T) string {
res := make([]string, 0)
numbers := len(values) / 1000
for i := 0; i < numbers; i++ {
items := make([]string, 0)
for j := i * 1000; j < (i+1)*1000; j++ {
items = append(items, Values(values[j]))
}
res = append(res, fmt.Sprintf("%s (%s)", prefix, strings.Join(items, ",")))
}
items := make([]string, 0)
for i := numbers * 1000; i < numbers*1000+len(values)%1000; i++ {
items = append(items, Values(values[i]))
}
res = append(res, fmt.Sprintf("%s (%s)", prefix, strings.Join(items, ",")))
return strings.Join(res, " or ")
}
func TestTransInCondition(t *testing.T) {
inValues := []bool{true, true, false}
inValues0 := []int32{}
inValues1 := []float64{1.0, 3.0, 5.0}
inValues2 := []string{"这种短手", "sdasdsd", "sdasdsd", "sdasdsd", "sad"}
inValues3 := []time.Time{time.Now(), time.Now(), time.Now(), time.Now(), time.Now()}
condition := TransInCondition("name in", inValues)
condition1 := TransInCondition("name in", inValues0)
condition2 := TransInCondition("name in", inValues1)
condition3 := TransInCondition("name in", inValues2)
condition4 := TransInCondition("name in", inValues3)
fmt.Print(condition)
fmt.Print(condition1)
fmt.Print(condition2)
fmt.Print(condition3)
fmt.Print(condition4)
}
func TestListDir(t *testing.T) {
version := runtime.Version()
reg := regexp.MustCompile(`(\d+\.\d+\.*\d*)`)
if reg == nil {
log.Panicln("正则表达式解析失败")
}
versionStr := reg.FindString(version)
versionSlice := strings.Split(versionStr, ".")
fmt.Print(versionStr)
fmt.Print(versionSlice)
}