/
schema.go
102 lines (91 loc) · 2.93 KB
/
schema.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
package schema
import (
"fmt"
"github.com/getevo/evo/v2/lib/db/schema/table"
"gorm.io/gorm"
"gorm.io/gorm/schema"
"path/filepath"
"reflect"
)
var Models []Model
type Model struct {
Sample any `json:"sample"`
Value reflect.Value `json:"-"`
Type reflect.Type `json:"-"`
Kind reflect.Kind `json:"-"`
Table string `json:"table"`
Name string `json:"name"`
Package string `json:"package"`
PackagePath string `json:"package_path"`
PrimaryKey []string `json:"primary_key"`
Joins map[string][]string `json:"joins"`
Schema *schema.Schema `json:"-"`
Statement *gorm.Statement `json:"-"`
}
func (m Model) Join(joins ...*Model) ([]string, []string, error) {
var where []string
var tables = []string{m.Table}
for _, join := range joins {
tables = append(tables, join.Table)
if v, ok := m.Joins[join.Table]; ok {
where = append(where, quote(m.Table)+"."+quote(v[0])+" = "+quote(join.Table)+"."+quote(v[1]))
continue
}
if _, ok := join.Schema.FieldsByDBName[m.PrimaryKey[0]]; ok {
where = append(where, quote(m.Table)+"."+quote(m.PrimaryKey[0])+" = "+quote(join.Table)+"."+quote(m.PrimaryKey[0]))
continue
}
if _, ok := m.Schema.FieldsByDBName[join.PrimaryKey[0]]; ok {
where = append(where, quote(join.Table)+"."+quote(join.PrimaryKey[0])+" = "+quote(m.Table)+"."+quote(join.PrimaryKey[0]))
continue
}
return nil, nil, fmt.Errorf("unable to find relation between %s and %s", m.Name, join.Name)
}
return tables, where, nil
}
func quote(s string) string {
return "`" + s + "`"
}
var database = ""
func UseModel(db *gorm.DB, values ...any) {
migrations = append(migrations, values...)
if database == "" {
db.Raw("SELECT DATABASE();").Scan(&database)
}
for index, _ := range values {
ref := reflect.ValueOf(values[index])
if ref.Kind() != reflect.Struct {
return
}
var model = Model{
Sample: ref.Interface(),
Value: ref,
Type: reflect.TypeOf(ref.Interface()),
Kind: ref.Kind(),
PackagePath: ref.Type().PkgPath(),
Package: filepath.Base(ref.Type().PkgPath()),
}
model.Name = model.Package + "." + ref.Type().Name()
stmt := db.Model(values[index]).Statement
stmt.Parse(values[index])
model.Schema = stmt.Schema
model.PrimaryKey = stmt.Schema.PrimaryFieldDBNames
model.Statement = stmt
model.Table = stmt.Table
model.Joins = make(map[string][]string)
var constraints []table.Constraint
db.Where(table.Constraint{Database: database}).Find(&constraints)
for _, constraint := range constraints {
model.Joins[constraint.ReferencedTable] = []string{constraint.Column, constraint.ReferencedColumn}
}
Models = append(Models, model)
}
}
func Find(name string) *Model {
for idx, _ := range Models {
if Models[idx].Name == name || Models[idx].Table == name {
return &Models[idx]
}
}
return nil
}