-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.go
47 lines (40 loc) · 942 Bytes
/
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
package gmodel
import (
"bytes"
"reflect"
)
type TypeSchemaFields []ToSql
func (t TypeSchemaFields) ToSql(config ToSqlConfig) (string, []any) {
sqlBuf := &bytes.Buffer{}
pms := make([]any, 0)
for i, field := range t {
sql, fieldPms := field.ToSql(config)
if fieldPms != nil {
pms = append(pms, fieldPms...)
}
if i > 0 {
sqlBuf.WriteString(",")
}
sqlBuf.WriteString(sql)
}
return sqlBuf.String(), pms
}
func AllSchemaFields(schema any) ToSql {
rval := reflect.ValueOf(schema)
if rval.Kind() != reflect.Pointer {
panic("assert faild AllSchemaFields type")
}
rval = rval.Elem()
if rval.Kind() != reflect.Struct {
panic("assert type faild, schema must be a struct")
}
var num = rval.NumField()
var ret = make([]ToSql, 0, num)
for i := 0; i < num; i++ {
fieldval := rval.Field(i).Interface()
if field, ok := fieldval.(ToSql); ok {
ret = append(ret, field)
}
}
return TypeSchemaFields(ret)
}