Skip to content

Commit 42c1401

Browse files
committed
Add -only flag to limit output to 'schema' or 'queries'
1 parent 3351fee commit 42c1401

File tree

2 files changed

+75
-45
lines changed

2 files changed

+75
-45
lines changed

README.md

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,38 @@ $ go install github.com/ngrash/sqlcup/cmd/sqlcup@v0.1.0
1313
```
1414
$ sqlcup -help
1515
sqlcup - generate SQL statements for sqlc (https://sqlc.dev)
16-
17-
Synopsis:
18-
sqlcup [options] <name> <column> ...
1916
20-
Description:
21-
sqlcup prints SQL statements to stdout. The <name> argument given to sqlcup
22-
must be of the form <singular>/<plural> where <singular> is the name of the
23-
Go struct and <plural> is the name of the database table.
24-
sqlcup capitalizes those names where required.
25-
26-
Each <column> arguments given to sqlcup defines a database column and must
27-
be of the form <name>:<type>[:<constraint>]. <name>, <type> and the
28-
optional <constraint> are used to generate a CREATE TABLE statement.
29-
In addition, <name> also appears in the SQL queries. sqlcup never
30-
capitalizes those names.
31-
32-
If any part of a <column> contains a space, it may be necessary to add
33-
quotes or escape those spaces, depending on the user's shell.
34-
35-
Example:
17+
Synopsis:
18+
sqlcup [options] <name> <column> ...
19+
20+
Description:
21+
sqlcup prints SQL statements to stdout. The <name> argument given to sqlcup
22+
must be of the form <singular>/<plural> where <singular> is the name of the
23+
Go struct and <plural> is the name of the database table.
24+
sqlcup capitalizes those names where required.
25+
26+
Each <column> arguments given to sqlcup defines a database column and must
27+
be of the form <name>:<type>[:<constraint>]. <name>, <type> and the
28+
optional <constraint> are used to generate a CREATE TABLE statement.
29+
In addition, <name> also appears in the SQL queries. sqlcup never
30+
capitalizes those names.
31+
32+
If any part of a <column> contains a space, it may be necessary to add
33+
quotes or escape those spaces, depending on the user's shell.
34+
35+
Example:
3636
sqlcup author/authors "id:INTEGER:PRIMARY KEY" "name:text:NOT NULL" bio:text
37-
sqlcup --order-by name user/users "id:INTEGER:PRIMARY KEY" name:text
38-
39-
Options:
37+
sqlcup --order-by name user/users "id:INTEGER:PRIMARY KEY" name:text
38+
39+
Options:
4040
-id-column string
4141
Name of the column that identifies a row (default "id")
4242
-no-exists-clause
4343
Omit IF NOT EXISTS in CREATE TABLE statements
4444
-no-returning-clause
4545
Omit 'RETURNING *' in UPDATE statement
46+
-only string
47+
Limit output to 'schema' or 'queries'
4648
-order-by string
4749
Include ORDER BY in 'SELECT *' statement
4850
```

cmd/sqlcup/main.go

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var (
1616
idColumnFlag = flag.String("id-column", "id", "Name of the column that identifies a row")
1717
orderByFlag = flag.String("order-by", "", "Include ORDER BY in 'SELECT *' statement")
1818
noReturningClauseFlag = flag.Bool("no-returning-clause", false, "Omit 'RETURNING *' in UPDATE statement")
19+
onlyFlag = flag.String("only", "", "Limit output to 'schema' or 'queries'")
1920
)
2021

2122
var errBadArgument = errors.New("bad argument")
@@ -79,6 +80,15 @@ type column struct {
7980
Constraint string
8081
}
8182

83+
type outputMode uint8
84+
85+
const (
86+
outputSchema outputMode = 1 << iota
87+
outputQueries
88+
89+
outputAll = outputSchema | outputQueries
90+
)
91+
8292
type scaffoldCommandArgs struct {
8393
Table string
8494
SingularEntity string
@@ -91,12 +101,14 @@ type scaffoldCommandArgs struct {
91101
NoExistsClause bool
92102
OrderBy string
93103
NoReturningClause bool
104+
Output outputMode
94105
}
95106

96107
func parseScaffoldCommandArgs(args []string) (*scaffoldCommandArgs, error) {
97108
if len(args) == 0 {
98109
return nil, fmt.Errorf("%w: missing <name> and <column>", errBadArgument)
99110
}
111+
100112
tableParts := strings.Split(args[0], "/")
101113
if len(tableParts) != 2 || len(tableParts[0]) == 0 || len(tableParts[1]) == 0 {
102114
return nil, fmt.Errorf("%w: invalid <name>: '%s', expected '<singular>/<plural>'", errBadArgument, tableParts)
@@ -110,6 +122,17 @@ func parseScaffoldCommandArgs(args []string) (*scaffoldCommandArgs, error) {
110122
NoReturningClause: *noReturningClauseFlag,
111123
OrderBy: *orderByFlag,
112124
}
125+
switch *onlyFlag {
126+
case "schema":
127+
sca.Output = sca.Output | outputSchema
128+
case "queries":
129+
sca.Output = sca.Output | outputQueries
130+
case "":
131+
sca.Output = sca.Output | outputAll
132+
default:
133+
return nil, fmt.Errorf("%w: '-only %s', expected 'schema' or 'queries'", errBadArgument, *onlyFlag)
134+
}
135+
113136
for _, arg := range args[1:] {
114137
parts := strings.Split(arg, ":")
115138
if len(parts) < 2 || len(parts) > 3 {
@@ -142,36 +165,41 @@ func parseScaffoldCommandArgs(args []string) (*scaffoldCommandArgs, error) {
142165
func scaffoldCommand(args *scaffoldCommandArgs) error {
143166
b := &strings.Builder{}
144167

145-
b.WriteString("#############################################\n")
146-
b.WriteString("# Add the following to your SQL schema file #\n")
147-
b.WriteString("#############################################\n\n")
148-
writeSchema(b, args)
149-
b.WriteString("\n\n")
150-
151-
b.WriteString("##############################################\n")
152-
b.WriteString("# Add the following to your SQL queries file #\n")
153-
b.WriteString("##############################################\n\n")
154-
if args.IDColumn != nil {
155-
writeGetQuery(b, args)
168+
if args.Output&outputAll == outputAll {
169+
b.WriteString("#############################################\n")
170+
b.WriteString("# Add the following to your SQL schema file #\n")
171+
b.WriteString("#############################################\n\n")
172+
}
173+
if args.Output&outputSchema != 0 {
174+
writeSchema(b, args)
156175
b.WriteString("\n\n")
157176
}
177+
if args.Output&outputAll == outputAll {
178+
b.WriteString("##############################################\n")
179+
b.WriteString("# Add the following to your SQL queries file #\n")
180+
b.WriteString("##############################################\n\n")
181+
}
182+
if args.Output&outputQueries != 0 {
183+
if args.IDColumn != nil {
184+
writeGetQuery(b, args)
185+
b.WriteString("\n\n")
186+
}
158187

159-
writeListQuery(b, args)
160-
b.WriteString("\n\n")
161-
162-
writeCreateQuery(b, args)
163-
b.WriteString("\n")
188+
writeListQuery(b, args)
189+
b.WriteString("\n\n")
164190

165-
if args.IDColumn != nil {
191+
writeCreateQuery(b, args)
166192
b.WriteString("\n")
167-
writeDeleteQuery(b, args)
168-
b.WriteString("\n\n")
169-
writeUpdateQuery(b, args)
170-
b.WriteString("\n\n")
171-
}
172193

194+
if args.IDColumn != nil {
195+
b.WriteString("\n")
196+
writeDeleteQuery(b, args)
197+
b.WriteString("\n\n")
198+
writeUpdateQuery(b, args)
199+
b.WriteString("\n\n")
200+
}
201+
}
173202
fmt.Print(b)
174-
175203
return nil
176204
}
177205

0 commit comments

Comments
 (0)