/
limit.go
129 lines (106 loc) · 2.82 KB
/
limit.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
package spec
import (
"fmt"
"strings"
"github.com/iancoleman/strcase"
"github.com/anqiansong/sqlgen/internal/buffer"
"github.com/anqiansong/sqlgen/internal/parameter"
"github.com/anqiansong/sqlgen/internal/set"
)
const (
countField = "count"
offsetFiled = "offset"
)
func (l *Limit) IsValid() bool {
if l == nil {
return false
}
return l.Count > 0
}
// SQL returns the clause condition strings.
func (l *Limit) SQL() (string, error) {
sql, _, err := l.marshal()
return fmt.Sprintf("`%s`", sql), err
}
// ParameterStructure returns the parameter type structure.
func (l *Limit) ParameterStructure() (string, error) {
_, parameters, err := l.marshal()
if err != nil {
return "", err
}
var writer = buffer.New()
writer.Write(`// %s is a limit parameter structure.`, l.ParameterStructureName())
writer.Write(`type %s struct {`, l.ParameterStructureName())
for _, v := range parameters {
writer.Write("%s %s", v.Column, v.Type)
}
writer.Write(`}`)
return writer.String(), nil
}
// ParameterThirdImports returns the third package imports.
func (l *Limit) ParameterThirdImports() (string, error) {
_, parameters, err := l.marshal()
if err != nil {
return "", err
}
var thirdPkgSet = set.From()
for _, v := range parameters {
if len(v.ThirdPkg) == 0 {
continue
}
thirdPkgSet.Add(v.ThirdPkg)
}
return strings.Join(thirdPkgSet.String(), "\n"), nil
}
// Parameters returns the parameter variables.
func (l *Limit) Parameters(pkg string) (string, error) {
_, parameters, err := l.marshal()
if err != nil {
return "", err
}
var list []string
for _, v := range parameters {
list = append(list, fmt.Sprintf("%s.%s", pkg, v.Column))
}
return strings.Join(list, ", "), nil
}
// LimitParameter returns the parameter variables.
func (l *Limit) LimitParameter(pkg string) string {
return fmt.Sprintf("%s.%s", pkg, strcase.ToCamel(countField))
}
// OffsetParameter returns the parameter variables.
func (l *Limit) OffsetParameter(pkg string) string {
return fmt.Sprintf("%s.%s", pkg, strcase.ToCamel(offsetFiled))
}
// ParameterStructureName returns the parameter structure name.
func (l *Limit) ParameterStructureName() string {
if !l.IsValid() {
return ""
}
return strcase.ToCamel(fmt.Sprintf("%sLimitParameter", l.Comment.FuncName))
}
func (l *Limit) One() bool {
if l == nil {
return false
}
return l.Count == 1
}
func (l *Limit) Multiple() bool {
if l == nil {
return false
}
return l.Count > 1
}
func (l *Limit) marshal() (sql string, parameters parameter.Parameters, err error) {
parameters = parameter.Empty
if l == nil {
return
}
sql = fmt.Sprintf("limit %d", l.Count)
parameters = append(parameters, NewParameter(countField, "int", ""))
if l.Offset > 0 {
sql = fmt.Sprintf("limit %d, %d", l.Offset, l.Count)
parameters = append(parameters, NewParameter(offsetFiled, "int", ""))
}
return
}