-
Notifications
You must be signed in to change notification settings - Fork 2
/
db.go
166 lines (133 loc) · 3.82 KB
/
db.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
165
166
package main
import (
"fmt"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/lxc/lxd/shared/generate/db"
"github.com/lxc/lxd/shared/generate/file"
"github.com/lxc/lxd/shared/generate/lex"
)
// Return a new db command.
func newDb() *cobra.Command {
cmd := &cobra.Command{
Use: "db [sub-command]",
Short: "Database-related code generation.",
RunE: func(cmd *cobra.Command, args []string) error {
return fmt.Errorf("Not implemented")
},
}
cmd.AddCommand(newDbSchema())
cmd.AddCommand(newDbMapper())
return cmd
}
func newDbSchema() *cobra.Command {
cmd := &cobra.Command{
Use: "schema",
Short: "Generate database schema by applying updates.",
RunE: func(cmd *cobra.Command, args []string) error {
return db.UpdateSchema()
},
}
return cmd
}
func newDbMapper() *cobra.Command {
cmd := &cobra.Command{
Use: "mapper [sub-command]",
Short: "Generate code mapping database rows to Go structs.",
RunE: func(cmd *cobra.Command, args []string) error {
return fmt.Errorf("Not implemented")
},
}
cmd.AddCommand(newDbMapperReset())
cmd.AddCommand(newDbMapperStmt())
cmd.AddCommand(newDbMapperMethod())
return cmd
}
func newDbMapperReset() *cobra.Command {
var target string
cmd := &cobra.Command{
Use: "reset",
Short: "Reset target source file.",
RunE: func(cmd *cobra.Command, args []string) error {
return file.Reset(target, db.Imports)
},
}
flags := cmd.Flags()
flags.StringVarP(&target, "target", "t", "-", "target source file to generate")
return cmd
}
func newDbMapperStmt() *cobra.Command {
var target string
var database string
var pkg string
var entity string
cmd := &cobra.Command{
Use: "stmt [kind]",
Args: cobra.MinimumNArgs(1),
Short: "Generate a particular database statement.",
RunE: func(cmd *cobra.Command, args []string) error {
kind := args[0]
if entity == "" {
return fmt.Errorf("No database entity given")
}
config, err := parseParams(args[1:])
if err != nil {
return err
}
stmt, err := db.NewStmt(database, pkg, entity, kind, config)
if err != nil {
return err
}
return file.Append(target, stmt)
},
}
flags := cmd.Flags()
flags.StringVarP(&target, "target", "t", "-", "target source file to generate")
flags.StringVarP(&database, "database", "d", "cluster", "target database")
flags.StringVarP(&pkg, "package", "p", "api", "Go package where the entity struct is declared")
flags.StringVarP(&entity, "entity", "e", "", "database entity to generate the statement for")
return cmd
}
func newDbMapperMethod() *cobra.Command {
var target string
var database string
var pkg string
var entity string
cmd := &cobra.Command{
Use: "method [kind] [param1=value1 ... paramN=valueN]",
Args: cobra.MinimumNArgs(1),
Short: "Generate a particular transaction method.",
RunE: func(cmd *cobra.Command, args []string) error {
kind := args[0]
if entity == "" {
return fmt.Errorf("No database entity given")
}
config, err := parseParams(args[1:])
if err != nil {
return err
}
method, err := db.NewMethod(database, pkg, entity, kind, config)
if err != nil {
return err
}
return file.Append(target, method)
},
}
flags := cmd.Flags()
flags.StringVarP(&target, "target", "t", "-", "target source file to generate")
flags.StringVarP(&database, "database", "d", "cluster", "target database")
flags.StringVarP(&pkg, "package", "p", "api", "Go package where the entity struct is declared")
flags.StringVarP(&entity, "entity", "e", "", "database entity to generate the method for")
return cmd
}
func parseParams(args []string) (map[string]string, error) {
config := map[string]string{}
for _, arg := range args {
key, value, err := lex.KeyValue(arg)
if err != nil {
return nil, errors.Wrap(err, "Invalid config parameter")
}
config[key] = value
}
return config, nil
}