-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
190 lines (167 loc) · 5.68 KB
/
main.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright 2019 Job Stoit. All rights reserved.
// gqb is made for generating a query builder.
//
// The gqb command takes the path to the db.yml as argument.
// So for example:
// gqb -model ./models/qb.mdl.go db.yml
//
// The command takes the following flags as arguments.
// -migrate Specifies the output for a generated sql migration
//
// -db Directly inserts the configured structure into the database
// using the DB_DRIVER and DB_CONNECTION_STRING enviroment
// variables or the flags for this mode
//
// -dvr Set the driver for the db flag
//
// -cs Sets the connection string for the db flag
//
// -model Writes the configuration to NiseVoid/qb model(s) takes
// the output file(s) as argument
//
// -pkg Used by the model flag, sets the package name of the
// model file(s)
//
// Configuration yaml example.
// pkg: models # optional, default model
// driver: postgres # optional, default postgres
// tables:
// users:
// id: int, primary
// email: varchar, unique
// password: varhcar
// fist_name: varchar(50)
// last_name: varchar(100)
// role: role # foreign key (enum)
//
// posts:
// id: int, primary
// created_by: users.id # foreign key
// created_at: datetime, default(NOW)
// updated_at: datetime, default(NOW)
// title: varchar
// subtitle: varchar, nullable
// context: text
//
// post_images:
// id: int, primary
// posts_id: posts(id) # another foreign key
// img_url: varchar
//
// enums:
// role:
// - GENERAL_USER
// - MODERATOR
// - ADMIN
//
// The configuration has the following type constraints:
// primary sets the type as primary key in the table
// unique sets a UNIQUE constraint on the type
// nullable undoes the default NOT NULL constraint on a type
// default(%) set the default constraint and uses the parameter to set
// a default value
// constraint(%) sets one or more constraints, constraints can be seperated by ;
//
package main
import (
"bytes"
"database/sql"
"flag"
"fmt"
"io/ioutil"
"os"
_ "github.com/denisenkom/go-mssqldb"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
)
// Flags for the application
var (
migrateFlag = flag.String(`migrate`, ``, `Specifies the output for the SQL migration`)
dbFlag = flag.Bool(`db`, false, `Directly inserts the configured structur into the database`)
dvrFlag = flag.String(`dvr`, os.Getenv(`DB_DRIVER`), `Sets the driver for the db flag. (postgres by default)`)
csFlag = flag.String(`cs`, os.Getenv(`DB_CONNECTION_STRING`), `Sets the connection string for the db flag`)
modelFlag = flag.String(`model`, ``, `Sets the output for the Nisevoid/qb models`)
pkgFlag = flag.String(`pkg`, ``, `Used by the model flag, sets the package name of the model file(s)`)
)
var mdl Model
func main() {
flags()
var errs []error
success := make(map[string]string)
if len(mdl.Tables) == 0 {
flag.PrintDefaults()
return
}
if *modelFlag != `` {
if file, err := os.Create(*modelFlag); err == nil {
defer file.Close() // nolint: errcheck
// nolint: errcheck
file.WriteString(`// This file is a generated file by github.com/jobstoit/gqb. PLEASE DO NOT EDIT.
package ` + mdl.Pkg + `
import (
"database/sql"
"fmt"
"git.ultraware.nl/NiseVoid/qb"
"git.ultraware.nl/NiseVoid/qb/driver/autoqb"
"git.ultraware.nl/NiseVoid/qb/qbdb"
)`)
CreateQbModel(mdl, file)
success[`model`] = `written to ` + *modelFlag
} else {
errs = append(errs, err)
}
}
if *migrateFlag != `` {
if file, err := os.Create(*migrateFlag); err == nil {
defer file.Close() // nolint: errcheck
CreateMigration(mdl, file)
success[`migration`] = `written to ` + *migrateFlag
} else {
errs = append(errs, err)
}
}
if *dbFlag {
buff := new(bytes.Buffer)
CreateMigration(mdl, buff)
if db, err := sql.Open(mdl.Driver, *csFlag); err == nil {
defer db.Close() // nolint: errcheck
if _, err = db.Exec(buff.String()); err != nil {
errs = append(errs, err)
}
success[`db`] = `succesfully executed the migration in the database`
} else {
errs = append(errs, err)
}
}
if len(success) > 0 {
fmt.Printf("\n \x1b[34m0000 \n \x1b[34m0000 \x1b[32m000 \x1b[34m## ## ## ## ##### ##### ## ## ##### ##### ##### \n \x1b[34m0000 \x1b[32m000 \x1b[34m## ## ## #### ## ## ## ## ## ## ## ## ## ## ## ##\n \x1b[34m0000 \x1b[32m000 \x1b[34m## ## ## ## ##### ####### ## # ## ####### ##### #######\n \x1b[34m0\x1b[36m000 \x1b[32m0000 \x1b[34m## ## ## ## ## ## ## ## ## ## # ## ## ## ## ## ##\n \x1b[36m000000000 \x1b[32m0000 \x1b[34m###### ###### ##### ## ## ## ## ### ### ## ## ## ## #####\n \x1b[36m000000000\n\n \x1b[32m%s\x1b[0m\n", `succesfully generated`)
for key, val := range success {
fmt.Printf("%s: %s\n", key, val)
}
}
for _, err := range errs {
fmt.Println(err)
}
}
func flags() {
if !flag.Parsed() {
flag.Parse()
}
if bitz, err := ioutil.ReadFile(flag.Arg(0)); err == nil {
mdl = ReadConfig(bitz)
} else {
return
}
if *dvrFlag != `` {
mdl.Driver = *dvrFlag
}
if mdl.Driver == `` {
mdl.Driver = `postgres`
}
if *pkgFlag != `` {
mdl.Pkg = *pkgFlag
}
if mdl.Pkg == `` {
mdl.Pkg = `model`
}
}