This repository has been archived by the owner on May 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entity.template
168 lines (137 loc) · 4.44 KB
/
entity.template
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
160
161
162
163
164
165
166
167
168
/**
* Generation Details
* Opal Version: {{.Version}}
* Date: {{.Date}}
* Time: {{.Time}}
*/
package {{.Package}}
import (
. "github.com/twinj/opal"
"reflect"
)
// Compile time check of Model implementation
var _ Model = &{{.Model}}{}
// Compile time check of {{.DAOName}}DAO implementation
var _ {{.DAOName}}DAO = &{{.DAOName}}IDAO{}
// Constant ModelName used to identify type
const {{.Model}}Model ModelName = "{{.ImportName}}"
// The {{.Model}} Entity - deliberately private
var _{{.Model}} Entity
// The Global {{.Model}} access object
var {{.DAOName}} {{.DAOName}}DAO
// *************************************************** MODEL
func Raw{{.Model}}() *{{.Model}} {
o := new({{.Model}})
o.Entity = _{{.Model}}.New(o)
return o
}
func ({{.Model}}) ScanInto() (Model, []interface{}) {
o := Raw{{.Model}}()
return o, BindArgs(o)
}
func (o *{{.Model}}) Keys() []interface{} {
return []interface{}{ {{range $i, $e := .Keys}}{{if $i}}, {{end}}&o.{{.Name}}{{end}} }
}
func (o *{{.Model}}) Parameters() []interface{} {
return []interface{}{ {{range $i, $e := .Columns}}{{if $i}}, {{end}}&o.{{.Name}}{{end}} }
}
func ({{.Model}}) Gather(pModelMetadata *ModelMetadata) (ModelName, *Entity, func(*ModelIDAO)ModelDAO) {
pModelMetadata.AddTable(Table{ {{.Table}} })
{{range $i, $e := .Keys}}{{if $i}}{{/* Extra range args determines whether a newline is required at the end */}}
{{end}}pModelMetadata.AddKey({{printf "%q" .Name}}, {{.Index}}, Column{ {{.Tag}} }, {{.Kind}}){{end}}
{{range $i, $e := .Columns}}{{if $i}}
{{end}}pModelMetadata.AddColumn({{printf "%q" .Name}}, {{.Index}}, Column{ {{.Tag}} }, {{.Kind}}){{end}}
return {{.Model}}Model, &_{{.Model}}, new{{.DAOName}}DAO
}
// ************************************************* HELPERS
type {{.Model}}_ struct {
{{range $i, $e := .Keys}}{{if $i}}{{/* Extra range args determines whether a newline is required at the end */}}
{{end}}{{.Name}} interface{}{{end}}
{{range $i, $e := .Columns}}{{if $i}}
{{end}}{{.Name}} interface{}{{end}}
}
func (o *{{.Model}}_) Args() []interface{} {
return []interface{}{
{{range $i, $e := .Keys}}{{if $i}}{{/* Extra range args determines whether a newline is required at the end */}}
{{end}}o.{{.Name}},{{end}}
{{range $i, $e := .Columns}}{{if $i}}
{{end}}o.{{.Name}},{{end}}
}
}
func (o *{{.Model}}_) Scan(pModel *{{.Model}}) {
{{range $i, $e := .Keys}}{{if $i}}{{/* Extra range args determines whether a newline is required at the end */}}
{{end}}pModel.{{.Name}}.Scan(o.{{.Name}}){{end}}
{{range $i, $e := .Columns}}{{if $i}}
{{end}}pModel.{{.Name}}.Scan(o.{{.Name}}){{end}}
}
func (o {{.Model}}_) New() *{{.Model}} {
return o.Insert()
}
func (o {{.Model}}_) Create() *{{.Model}} {
return o.Insert()
}
func (o {{.Model}}_) Merge() *{{.Model}} {
return o.Save()
}
func (o {{.Model}}_) Update() *{{.Model}} {
return o.Save()
}
func (o {{.Model}}_) Insert() *{{.Model}} {
m := Raw{{.Model}}()
o.Scan(m)
m.Insert()
return m
}
func (o {{.Model}}_) Save() *{{.Model}} {
if {{range $i, $e := .Keys}}{{if $i}}&& {{end}}o.{{.Name}} != nil{{end}} {
m := {{.DAOName}}.Find({{range $i, $e := .Keys}}{{if $i}}, {{end}}o.{{.Name}}.({{.Primitive}}){{end}})
if m != nil {
o.Scan(m)
m.Save()
return m
}
}
return o.Insert()
}
// ***************************************************** DAO
type {{.DAOName}}DAO interface {
ModelDAO
All() []{{.Model}}
Find({{range $i, $e := .Keys}}{{if $i}},{{end}}{{.Primitive}}{{end}}) *{{.Model}}
Exec(Sql) ([]{{.Model}}, error)
}
type {{.DAOName}}IDAO struct {
*ModelIDAO
}
func new{{.DAOName}}DAO(pModelDAO *ModelIDAO) ModelDAO {
o := new({{.DAOName}}IDAO)
o.ModelIDAO = pModelDAO
{{.DAOName}} = o
return o
}
func (o {{.DAOName}}IDAO) All() []{{.Model}} {
return o.CastAll(o.FindAllModels())
}
func (o {{.DAOName}}IDAO) Find({{range $i, $e := .Keys}}{{if $i}}, {{end}}p{{printf "%d" $i}} {{.Primitive}}{{end}}) *{{.Model}} {
return o.Cast(o.FindModel({{range $i, $e := .Keys}}{{if $i}}, {{end}}New{{.TypeName}}(p{{printf "%d" $i}}){{end}}))
}
func (o {{.DAOName}}IDAO) Exec(pSql Sql) ([]{{.Model}}, error) {
rows, err := o.Gem().Query({{.Model}}Model, pSql)
if err != nil {
return nil, err
}
return o.CastAll(rows), nil
}
func (o {{.DAOName}}IDAO) CastAll(pModels []Model) []{{.Model}} {
list := make([]{{.Model}}, len(pModels))
for i, model := range pModels {
list[i] = *o.Cast(model)
}
return list
}
func ({{.DAOName}}IDAO) Cast(pModel Model) *{{.Model}} {
if pModel != nil {
return pModel.(*{{.Model}})
}
return nil
}