Skip to content

Commit 57d376c

Browse files
committed
Refactor loader
1 parent 829ffde commit 57d376c

File tree

3 files changed

+39
-40
lines changed

3 files changed

+39
-40
lines changed

loader.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,44 @@ import (
1010
"strings"
1111
)
1212

13+
func BuildFields(modelType reflect.Type) string {
14+
columns := GetFields(modelType)
15+
return strings.Join(columns, ",")
16+
}
17+
func GetFields(modelType reflect.Type) []string {
18+
m := modelType
19+
if m.Kind() == reflect.Ptr {
20+
m = m.Elem()
21+
}
22+
numField := m.NumField()
23+
columns := make([]string, 0)
24+
for idx := 0; idx < numField; idx++ {
25+
field := m.Field(idx)
26+
tag, _ := field.Tag.Lookup("gorm")
27+
if !strings.Contains(tag, IgnoreReadWrite) {
28+
if has := strings.Contains(tag, "column"); has {
29+
json := field.Name
30+
col := json
31+
str1 := strings.Split(tag, ";")
32+
num := len(str1)
33+
for i := 0; i < num; i++ {
34+
str2 := strings.Split(str1[i], ":")
35+
for j := 0; j < len(str2); j++ {
36+
if str2[j] == "column" {
37+
col = str2[j+1]
38+
columns = append(columns, col)
39+
}
40+
}
41+
}
42+
}
43+
}
44+
}
45+
return columns
46+
}
47+
func BuildQuery(table string, modelType reflect.Type) string {
48+
columns := GetFields(modelType)
49+
return "select " + strings.Join(columns, ",") + " from " + table + " "
50+
}
1351
func InitFields(modelType reflect.Type, db *sql.DB) (map[string]int, string, func(i int) string, string, error) {
1452
fieldsIndex, err := GetColumnIndexes(modelType)
1553
if err != nil {

sql_util.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010
)
1111

12+
const IgnoreReadWrite = "-"
1213
const txs = "tx"
1314

1415
type Executor interface {

util.go

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import (
1010
"time"
1111
)
1212

13-
const IgnoreReadWrite = "-"
14-
1513
type BatchStatement struct {
1614
Query string
1715
Values []interface{}
@@ -54,44 +52,6 @@ func BuildQueryBySchema(table string, schema *Schema) string {
5452
}
5553
return "select " + strings.Join(columns, ",") + " from " + table + " "
5654
}
57-
func BuildFields(modelType reflect.Type) string {
58-
columns := GetFields(modelType)
59-
return strings.Join(columns, ",")
60-
}
61-
func GetFields(modelType reflect.Type) []string {
62-
m := modelType
63-
if m.Kind() == reflect.Ptr {
64-
m = m.Elem()
65-
}
66-
numField := m.NumField()
67-
columns := make([]string, 0)
68-
for idx := 0; idx < numField; idx++ {
69-
field := m.Field(idx)
70-
tag, _ := field.Tag.Lookup("gorm")
71-
if !strings.Contains(tag, IgnoreReadWrite) {
72-
if has := strings.Contains(tag, "column"); has {
73-
json := field.Name
74-
col := json
75-
str1 := strings.Split(tag, ";")
76-
num := len(str1)
77-
for i := 0; i < num; i++ {
78-
str2 := strings.Split(str1[i], ":")
79-
for j := 0; j < len(str2); j++ {
80-
if str2[j] == "column" {
81-
col = str2[j+1]
82-
columns = append(columns, col)
83-
}
84-
}
85-
}
86-
}
87-
}
88-
}
89-
return columns
90-
}
91-
func BuildQuery(table string, modelType reflect.Type) string {
92-
columns := GetFields(modelType)
93-
return "select " + strings.Join(columns, ",") + " from " + table + " "
94-
}
9555
func CreateSchema(modelType reflect.Type) *Schema {
9656
m := modelType
9757
if m.Kind() == reflect.Ptr {

0 commit comments

Comments
 (0)