Skip to content

Commit 84f55c4

Browse files
committed
Change smart column separator to @ because some shells interpret # as a comment
1 parent 235f82d commit 84f55c4

File tree

4 files changed

+72
-45
lines changed

4 files changed

+72
-45
lines changed

README.md

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
## Installation
66

77
```
8-
$ go install github.com/ngrash/sqlcup/cmd/sqlcup@v0.3.0
8+
$ go install github.com/ngrash/sqlcup/cmd/sqlcup@v0.4.0
99
```
1010

1111
## Usage
@@ -14,29 +14,50 @@ $ go install github.com/ngrash/sqlcup/cmd/[email protected]
1414
$ sqlcup -help
1515
sqlcup - generate SQL statements for sqlc (https://sqlc.dev)
1616
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:
36-
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:
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 argument given to sqlcup defines a database column and must
27+
be either a <plain-column> or a <smart-column>:
28+
29+
A <plain-column> must be of the form <name>:<type>[:<constraint>]. <name>,
30+
<type> and the optional <constraint> are used to generate a CREATE TABLE
31+
statement. In addition, <name> also appears in SQL queries. sqlcup never
32+
capitalizes those names. To use <tag> you need to define a <smart-column>.
33+
34+
A <smart-column> is a shortcut for common column definitions. It must be of
35+
the form <name>@<tag>@<tag>...
36+
37+
A <tag> adds either a data type or a constraint to a <smart-column>.
38+
39+
@id
40+
Make this column the primary key. Omitting column type and <name>
41+
for an @id column creates an INTEGER PRIMARY KEY named 'id'.
42+
43+
@text, @int, @float, @double, @datetime, @blob
44+
Set the column type.
45+
46+
@unique
47+
Add a UNIQUE constraint.
48+
49+
@null
50+
Omit NOT NULL constraint.
51+
52+
If any part of a <column> contains a space, it may be necessary to add
53+
quotes or escape those spaces, depending on the user's shell.
54+
55+
Example:
56+
sqlcup author/authors "id:INTEGER:PRIMARY KEY" "name:text:NOT NULL" bio:text
57+
sqlcup --order-by name user/users "id:INTEGER:PRIMARY KEY" name:text
58+
sqlcup author/authors @id name@text@unique bio@text@null
59+
60+
Options:
4061
-id-column string
4162
Name of the column that identifies a row (default "id")
4263
-no-exists-clause
@@ -47,6 +68,7 @@ Options:
4768
Limit output to 'schema' or 'queries'
4869
-order-by string
4970
Include ORDER BY in 'SELECT *' statement
71+
5072
```
5173

5274
## Example

cmd/sqlcup/main.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ import (
1212
"unicode"
1313
)
1414

15+
const (
16+
plainColumnSep = ":"
17+
smartColumnSep = "@"
18+
)
19+
1520
var (
1621
noExistsClauseFlag = flag.Bool("no-exists-clause", false, "Omit IF NOT EXISTS in CREATE TABLE statements")
1722
idColumnFlag = flag.String("id-column", "id", "Name of the column that identifies a row")
@@ -90,8 +95,8 @@ type scaffoldCommandArgs struct {
9095

9196
func parseColumnDefinition(s string) (column, error) {
9297
var (
93-
plainColumn = strings.Contains(s, ":")
94-
smartColumn = strings.Contains(s, "#")
98+
plainColumn = strings.Contains(s, plainColumnSep)
99+
smartColumn = strings.Contains(s, smartColumnSep)
95100
)
96101
if plainColumn && smartColumn {
97102
return column{}, fmt.Errorf("%w: invalid <column>: '%s' contains both plain and smart separators", errBadArgument, s)
@@ -105,7 +110,7 @@ func parseColumnDefinition(s string) (column, error) {
105110
}
106111

107112
func parseSmartColumnDefinition(s string) (column, error) {
108-
if s == "#id" {
113+
if s == "@id" {
109114
return column{
110115
ID: true,
111116
Name: "id",
@@ -114,7 +119,7 @@ func parseSmartColumnDefinition(s string) (column, error) {
114119
}, nil
115120
}
116121

117-
name, rest, _ := strings.Cut(s, "#")
122+
name, rest, _ := strings.Cut(s, smartColumnSep)
118123
if name == "" {
119124
return column{}, fmt.Errorf("%w: '%s', missing <name>", errInvalidSmartColumn, s)
120125
}
@@ -125,7 +130,7 @@ func parseSmartColumnDefinition(s string) (column, error) {
125130
null bool
126131
unique bool
127132
)
128-
tags := strings.Split(rest, "#")
133+
tags := strings.Split(rest, smartColumnSep)
129134
for _, tag := range tags {
130135
switch tag {
131136
case "id":
@@ -152,7 +157,7 @@ func parseSmartColumnDefinition(s string) (column, error) {
152157
}
153158
if id {
154159
if unique || null {
155-
return column{}, fmt.Errorf("%w: '%s', cannot combine #id with #unique or #null", errInvalidSmartColumn, s)
160+
return column{}, fmt.Errorf("%w: '%s', cannot combine @id with @unique or @null", errInvalidSmartColumn, s)
156161
}
157162
if colType == "" {
158163
colType = "INTEGER"

cmd/sqlcup/main_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,63 +9,63 @@ var smartColTests = map[string]struct {
99
col column
1010
err error
1111
}{
12-
"#id": {
12+
"@id": {
1313
col: column{
1414
Name: "id",
1515
Type: "INTEGER",
1616
Constraint: "PRIMARY KEY",
1717
ID: true,
1818
},
1919
},
20-
"col_id#id": {
20+
"col_id@id": {
2121
col: column{
2222
Name: "col_id",
2323
Type: "INTEGER",
2424
Constraint: "PRIMARY KEY",
2525
ID: true,
2626
},
2727
},
28-
"primary_key#text#id": {
28+
"primary_key@text@id": {
2929
col: column{
3030
Name: "primary_key",
3131
Type: "TEXT",
3232
Constraint: "NOT NULL PRIMARY KEY",
3333
ID: true,
3434
},
3535
},
36-
"col#text": {
36+
"col@text": {
3737
col: column{
3838
Name: "col",
3939
Type: "TEXT",
4040
Constraint: "NOT NULL",
4141
ID: false,
4242
},
4343
},
44-
"col#text#null": {
44+
"col@text@null": {
4545
col: column{
4646
Name: "col",
4747
Type: "TEXT",
4848
Constraint: "",
4949
ID: false,
5050
},
5151
},
52-
"col#text#unique": {
52+
"col@text@unique": {
5353
col: column{
5454
Name: "col",
5555
Type: "TEXT",
5656
Constraint: "NOT NULL UNIQUE",
5757
ID: false,
5858
},
5959
},
60-
"col#int": {
60+
"col@int": {
6161
col: column{
6262
Name: "col",
6363
Type: "INTEGER",
6464
Constraint: "NOT NULL",
6565
ID: false,
6666
},
6767
},
68-
"col#datetime": {
68+
"col@datetime": {
6969
col: column{
7070
Name: "col",
7171
Type: "DATETIME",

cmd/sqlcup/usage.txt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,21 @@ Description:
1818
capitalizes those names. To use <tag> you need to define a <smart-column>.
1919

2020
A <smart-column> is a shortcut for common column definitions. It must be of
21-
the form <name>#<tag>#<tag>...
21+
the form <name>@<tag>@<tag>...
2222

2323
A <tag> adds either a data type or a constraint to a <smart-column>.
2424

25-
#id
25+
@id
2626
Make this column the primary key. Omitting column type and <name>
27-
for an #id column creates an INTEGER PRIMARY KEY named 'id'.
27+
for an @id column creates an INTEGER PRIMARY KEY named 'id'.
2828

29-
#text, #int, #float, #double, #datetime, #blob
29+
@text, @int, @float, @double, @datetime, @blob
3030
Set the column type.
3131

32-
#unique
32+
@unique
3333
Add a UNIQUE constraint.
3434

35-
#null
35+
@null
3636
Omit NOT NULL constraint.
3737

3838
If any part of a <column> contains a space, it may be necessary to add
@@ -41,6 +41,6 @@ Description:
4141
Example:
4242
sqlcup author/authors "id:INTEGER:PRIMARY KEY" "name:text:NOT NULL" bio:text
4343
sqlcup --order-by name user/users "id:INTEGER:PRIMARY KEY" name:text
44-
sqlcup author/authors #id name#text#unique bio#text#null
44+
sqlcup author/authors @id name@text@unique bio@text@null
4545

4646
Options:

0 commit comments

Comments
 (0)