-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.go
164 lines (141 loc) · 3.65 KB
/
generator.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
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
package yesql
import (
"context"
"database/sql"
"os"
"path/filepath"
stdTmpl "text/template"
"github.com/alimy/yesql/naming"
"github.com/alimy/yesql/template"
)
const (
_defaultSqlxPkgName = "github.com/jmoiron/sqlx"
)
var (
_ Generator = (*sqlGenerator)(nil)
)
type tmplCtx struct {
PkgName string
SqlxPkgName string
DefaultStructName string
AllQuery []*Query
DefaultQueryMap QueryMap
ScopeQuery ScopeQuery
YesqlVer string
}
type simplePrepareBuilder struct {
p PrepareContext
hooks []func(string) string
}
type simplePreparexBuilder[T, S any] struct {
p PreparexContext[T, S]
hooks []func(string) string
}
type sqlGenerator struct {
tmpl *stdTmpl.Template
}
func (s *simplePrepareBuilder) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error) {
return s.p.PrepareContext(ctx, query)
}
func (s *simplePrepareBuilder) QueryHook(query string) string {
for _, h := range s.hooks {
query = h(query)
}
return query
}
func (s *simplePreparexBuilder[T, S]) PreparexContext(ctx context.Context, query string) (T, error) {
return s.p.PreparexContext(ctx, query)
}
func (s *simplePreparexBuilder[T, S]) PrepareNamedContext(ctx context.Context, query string) (S, error) {
return s.p.PrepareNamedContext(ctx, query)
}
func (s *simplePreparexBuilder[T, S]) Rebind(query string) string {
return s.p.Rebind(query)
}
func (s *simplePreparexBuilder[T, S]) QueryHook(query string) string {
for _, h := range s.hooks {
query = h(query)
}
return query
}
func (t *tmplCtx) DefaultQueryMapNotEmpty() bool {
return len(t.DefaultQueryMap) != 0
}
func (t *tmplCtx) ScopeQueryNotEmpty() bool {
return len(t.ScopeQuery) != 0
}
func (s *sqlGenerator) Generate(dstPath string, pkgName string, query SQLQuery, opts ...option) (err error) {
opt := &generateOption{
goFileName: "yesql.go",
defaultStructName: "Yesql",
sqlxPkgName: _defaultSqlxPkgName,
}
for _, arg := range opts {
arg.apply(opt)
}
data := &tmplCtx{
PkgName: pkgName,
SqlxPkgName: opt.sqlxPkgName,
DefaultStructName: naming.Naming(opt.defaultStructName),
AllQuery: query.AllQuery(),
ScopeQuery: query.ListScope(),
YesqlVer: Version,
}
if len(data.PkgName) == 0 {
data.PkgName = "yesql"
}
data.DefaultQueryMap, err = query.ListQuery()
if err != nil {
return err
}
if filepath.Ext(opt.goFileName) != ".go" {
opt.goFileName += ".go"
}
filePath := filepath.Join(dstPath, opt.goFileName)
dirPath := filepath.Dir(filePath)
if err := os.MkdirAll(dirPath, 0755); err != nil {
return err
}
file, err := os.OpenFile(filePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer file.Close()
return s.tmpl.Execute(file, data)
}
// NewPrepareBuilder create a simple prepare builder instance
func NewPrepareBuilder(p PrepareContext, hooks ...func(string) string) PrepareBuilder {
obj := &simplePrepareBuilder{
p: p,
}
for _, h := range hooks {
if h != nil {
obj.hooks = append(obj.hooks, h)
}
}
return obj
}
// NewPreprarexBuilder[T, S] create a simple preparex builder instance
func NewPreparexBuilder[T, S any](p PreparexContext[T, S], hooks ...func(string) string) PreparexBuilder[T, S] {
obj := &simplePreparexBuilder[T, S]{
p: p,
}
for _, h := range hooks {
if h != nil {
obj.hooks = append(obj.hooks, h)
}
}
return obj
}
// NewSqlGenerator create a sql generator use std sql
func NewSqlGenerator() Generator {
return &sqlGenerator{
tmpl: template.NewSqlTemplate(),
}
}
// NewSqlxGenerator create a sqlx generator use sqlx
func NewSqlxGenerator() Generator {
return &sqlGenerator{
tmpl: template.NewSqlxTemplate(),
}
}