Skip to content

Commit 732e661

Browse files
committed
add sqlc.sort function
1 parent 67e865b commit 732e661

File tree

19 files changed

+1533
-1446
lines changed

19 files changed

+1533
-1446
lines changed

internal/analysis/analysis.pb.go

Lines changed: 210 additions & 189 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/cmd/shim.go

Lines changed: 68 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,20 @@ func pluginCatalog(c *catalog.Catalog) *plugin.Catalog {
6767
for _, typ := range s.Types {
6868
switch typ := typ.(type) {
6969
case *catalog.Enum:
70-
enums = append(enums, &plugin.Enum{
71-
Name: typ.Name,
72-
Comment: typ.Comment,
73-
Vals: typ.Vals,
74-
})
70+
enums = append(
71+
enums, &plugin.Enum{
72+
Name: typ.Name,
73+
Comment: typ.Comment,
74+
Vals: typ.Vals,
75+
},
76+
)
7577
case *catalog.CompositeType:
76-
cts = append(cts, &plugin.CompositeType{
77-
Name: typ.Name,
78-
Comment: typ.Comment,
79-
})
78+
cts = append(
79+
cts, &plugin.CompositeType{
80+
Name: typ.Name,
81+
Comment: typ.Comment,
82+
},
83+
)
8084
}
8185
}
8286
var tables []*plugin.Table
@@ -87,43 +91,49 @@ func pluginCatalog(c *catalog.Catalog) *plugin.Catalog {
8791
if c.Length != nil {
8892
l = *c.Length
8993
}
90-
columns = append(columns, &plugin.Column{
91-
Name: c.Name,
92-
Type: &plugin.Identifier{
93-
Catalog: c.Type.Catalog,
94-
Schema: c.Type.Schema,
95-
Name: c.Type.Name,
94+
columns = append(
95+
columns, &plugin.Column{
96+
Name: c.Name,
97+
Type: &plugin.Identifier{
98+
Catalog: c.Type.Catalog,
99+
Schema: c.Type.Schema,
100+
Name: c.Type.Name,
101+
},
102+
Comment: c.Comment,
103+
NotNull: c.IsNotNull,
104+
Unsigned: c.IsUnsigned,
105+
IsArray: c.IsArray,
106+
ArrayDims: int32(c.ArrayDims),
107+
Length: int32(l),
108+
Table: &plugin.Identifier{
109+
Catalog: t.Rel.Catalog,
110+
Schema: t.Rel.Schema,
111+
Name: t.Rel.Name,
112+
},
96113
},
97-
Comment: c.Comment,
98-
NotNull: c.IsNotNull,
99-
Unsigned: c.IsUnsigned,
100-
IsArray: c.IsArray,
101-
ArrayDims: int32(c.ArrayDims),
102-
Length: int32(l),
103-
Table: &plugin.Identifier{
114+
)
115+
}
116+
tables = append(
117+
tables, &plugin.Table{
118+
Rel: &plugin.Identifier{
104119
Catalog: t.Rel.Catalog,
105120
Schema: t.Rel.Schema,
106121
Name: t.Rel.Name,
107122
},
108-
})
109-
}
110-
tables = append(tables, &plugin.Table{
111-
Rel: &plugin.Identifier{
112-
Catalog: t.Rel.Catalog,
113-
Schema: t.Rel.Schema,
114-
Name: t.Rel.Name,
123+
Columns: columns,
124+
Comment: t.Comment,
115125
},
116-
Columns: columns,
117-
Comment: t.Comment,
118-
})
126+
)
119127
}
120-
schemas = append(schemas, &plugin.Schema{
121-
Comment: s.Comment,
122-
Name: s.Name,
123-
Tables: tables,
124-
Enums: enums,
125-
CompositeTypes: cts,
126-
})
128+
schemas = append(
129+
schemas, &plugin.Schema{
130+
Comment: s.Comment,
131+
Name: s.Name,
132+
Tables: tables,
133+
Enums: enums,
134+
CompositeTypes: cts,
135+
},
136+
)
127137
}
128138
return &plugin.Catalog{
129139
Name: c.Name,
@@ -152,16 +162,18 @@ func pluginQueries(r *compiler.Result) []*plugin.Query {
152162
Name: q.InsertIntoTable.Name,
153163
}
154164
}
155-
out = append(out, &plugin.Query{
156-
Name: q.Metadata.Name,
157-
Cmd: q.Metadata.Cmd,
158-
Text: q.SQL,
159-
Comments: q.Metadata.Comments,
160-
Columns: columns,
161-
Params: params,
162-
Filename: q.Metadata.Filename,
163-
InsertIntoTable: iit,
164-
})
165+
out = append(
166+
out, &plugin.Query{
167+
Name: q.Metadata.Name,
168+
Cmd: q.Metadata.Cmd,
169+
Text: q.SQL,
170+
Comments: q.Metadata.Comments,
171+
Columns: columns,
172+
Params: params,
173+
Filename: q.Metadata.Filename,
174+
InsertIntoTable: iit,
175+
},
176+
)
165177
}
166178
return out
167179
}
@@ -184,6 +196,13 @@ func pluginQueryColumn(c *compiler.Column) *plugin.Column {
184196
IsFuncCall: c.IsFuncCall,
185197
IsSqlcSlice: c.IsSqlcSlice,
186198
}
199+
if c.SqlcSortOpts != nil {
200+
out.SqlcSortOpts = &plugin.SqlcSortOpts{
201+
IsOrder: c.SqlcSortOpts.IsOrder,
202+
DefaultField: c.SqlcSortOpts.DefaultField,
203+
DefaultOrder: c.SqlcSortOpts.DefaultOrder,
204+
}
205+
}
187206

188207
if c.Type != nil {
189208
out.Type = &plugin.Identifier{

internal/codegen/golang/field.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ func (gf Field) HasSqlcSlice() bool {
2929
return gf.Column.IsSqlcSlice
3030
}
3131

32+
func (gf Field) HasSqlcSort() bool {
33+
return gf.Column.SqlcSortOpts != nil
34+
}
35+
36+
func (gf Field) NotNull() bool {
37+
return gf.Column.NotNull
38+
}
39+
40+
func (gf Field) SqlcSortOpts() *plugin.SqlcSortOpts {
41+
return gf.Column.SqlcSortOpts
42+
}
43+
3244
func TagsToString(tags map[string]string) string {
3345
if len(tags) == 0 {
3446
return ""
@@ -81,9 +93,11 @@ var camelPattern = regexp.MustCompile("[^A-Z][A-Z]+")
8193

8294
func toSnakeCase(s string) string {
8395
if !strings.ContainsRune(s, '_') {
84-
s = camelPattern.ReplaceAllStringFunc(s, func(x string) string {
85-
return x[:1] + "_" + x[1:]
86-
})
96+
s = camelPattern.ReplaceAllStringFunc(
97+
s, func(x string) string {
98+
return x[:1] + "_" + x[1:]
99+
},
100+
)
87101
}
88102
return strings.ToLower(s)
89103
}

0 commit comments

Comments
 (0)