-
Notifications
You must be signed in to change notification settings - Fork 0
/
struct.go
89 lines (83 loc) · 2.05 KB
/
struct.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
package cmds
import (
"encoding/json"
"fmt"
"github.com/mocheer/pluto/pkg/ds/ds_json"
"github.com/mocheer/pluto/pkg/ts"
"github.com/mocheer/pluto/pkg/ts/object"
"github.com/urfave/cli/v2"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
// Struct : nix struct schemeName
var Struct = &cli.Command{
Name: "struct",
Usage: "从数据库中读取库表模型转成struct",
Action: func(c *cli.Context) error {
db, err := openDB()
if err != nil {
return err
}
//
query := db.Raw(`SELECT
tb.tablename as tablename,
a.attname AS columnname,
t.typname AS type
FROM
pg_class as c,
pg_attribute as a,
pg_type as t,
(select tablename from pg_tables where schemaname = @schema) as tb
WHERE a.attnum > 0
and a.attrelid = c.oid
and a.atttypid = t.oid
and c.relname = tb.tablename
order by tablename`, map[string]any{"schema": "pipal"})
result := scanIntoMap(query)
fmt.Println(result)
return nil
},
}
func openDB() (db *gorm.DB, err error) {
data := ds_json.ReadGJSON("./assets/config/app.json")
if err != nil {
return nil, err
}
db, err = gorm.Open(postgres.Open(data.Get("DSN").String()), &gorm.Config{
AllowGlobalUpdate: false,
Logger: logger.Default.LogMode(logger.Silent),
})
return
}
func scanIntoMap(query *gorm.DB) []ts.Map[any] {
var result []ts.Map[any]
rows, err := query.Rows()
fmt.Println(err)
if err == nil {
for rows.Next() {
rowData := map[string]any{}
err = query.ScanRows(rows, &rowData)
//
if err == nil {
columnTypes, _ := rows.ColumnTypes()
for _, columnType := range columnTypes {
// gorm-postgresql 底层的 DataType 并没有识别JSON,然后转成[]byte,而是用的string
if columnType.DatabaseTypeName() == "JSON" {
name := columnType.Name()
if object.IsString(rowData[name]) {
var data any
err := json.Unmarshal([]byte(rowData[name].(string)), &data)
if err == nil {
rowData[name] = data
}
}
}
}
}
//
result = append(result, rowData)
}
}
return result
}