Skip to content

Commit c6c1263

Browse files
authored
feat: add support for copyfrom queries (#6)
copyfrom queries also included in runner pipline goimports new files for correct imports
1 parent 4402861 commit c6c1263

File tree

6 files changed

+64
-42
lines changed

6 files changed

+64
-42
lines changed

cmd/sqlc-restruct/separate_interface.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ var SeparateInterfaceCommand = &cli.Command{
5757
Usage: "The file name for the sqlc-generated Querier file.",
5858
Value: "querier.go",
5959
},
60+
&cli.StringSliceFlag{
61+
Name: "aditional-querier-files",
62+
Usage: "A list of other files that contains querier impl. (e.g. copyfrom.go)",
63+
Value: cli.NewStringSlice("copyfrom.go"),
64+
},
6065
},
6166
Action: func(c *cli.Context) error {
6267
iPkgName := c.String("iface-pkg-name")
@@ -77,16 +82,17 @@ var SeparateInterfaceCommand = &cli.Command{
7782
}
7883

7984
return separateinterface.Action(c.Context, separateinterface.ActionInput{
80-
IfacePkgName: iPkgName,
81-
IfacePkgURL: iPkgURL,
82-
IfaceDir: iDir,
83-
ModelsPkgName: mPkgName,
84-
ModelsPkgURL: mPkgURL,
85-
ModelsDir: mDir,
86-
ImplDir: c.String("impl-dir"),
87-
ImplSQLSuffix: c.String("impl-sql-suffix"),
88-
ModelsFileName: c.String("models-file-name"),
89-
QuerierFileName: c.String("querier-file-name"),
85+
IfacePkgName: iPkgName,
86+
IfacePkgURL: iPkgURL,
87+
IfaceDir: iDir,
88+
ModelsPkgName: mPkgName,
89+
ModelsPkgURL: mPkgURL,
90+
ModelsDir: mDir,
91+
ImplDir: c.String("impl-dir"),
92+
ImplSQLSuffix: c.String("impl-sql-suffix"),
93+
ModelsFileName: c.String("models-file-name"),
94+
QuerierFileName: c.String("querier-file-name"),
95+
AditionalQuerierFiles: c.StringSlice("aditional-querier-files"),
9096
})
9197
},
9298
}

example/infra/db/query.articles.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ SELECT * FROM articles WHERE user_id = $1 ORDER BY created_at DESC;
1717

1818
-- name: ArticleListOfUserInCategory :many
1919
SELECT * FROM articles WHERE user_id = $1 AND category_id = $2 ORDER BY created_at DESC;
20+
21+
-- name: ArticleInsertCopyFrom :copyfrom
22+
INSERT INTO articles(user_id, category_id, slug, title, body) VALUES ($1, $2, $3, $4, $5);

go.mod

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@ module github.com/mpyw/sqlc-restruct
33
go 1.20
44

55
require (
6-
github.com/jackc/pgx/v5 v5.4.1
76
github.com/urfave/cli/v2 v2.25.7
87
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df
8+
golang.org/x/tools v0.6.0
99
)
1010

1111
require (
1212
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
13-
github.com/jackc/pgpassfile v1.0.0 // indirect
14-
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
1513
github.com/russross/blackfriday/v2 v2.1.0 // indirect
1614
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
17-
golang.org/x/crypto v0.9.0 // indirect
18-
golang.org/x/text v0.9.0 // indirect
15+
golang.org/x/mod v0.11.0 // indirect
16+
golang.org/x/sys v0.8.0 // indirect
1917
)

go.sum

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,16 @@
11
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
22
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
3-
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4-
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
5-
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
6-
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
7-
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
8-
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
9-
github.com/jackc/pgx/v5 v5.4.1 h1:oKfB/FhuVtit1bBM3zNRRsZ925ZkMN3HXL+LgLUM9lE=
10-
github.com/jackc/pgx/v5 v5.4.1/go.mod h1:q6iHT8uDNXWiFNOlRqJzBTaSH3+2xCXkokxHZC5qWFY=
11-
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
12-
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
133
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
144
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
15-
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
16-
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
17-
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
18-
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
195
github.com/urfave/cli/v2 v2.25.7 h1:VAzn5oq403l5pHjc4OhD54+XGO9cdKVL/7lDjF+iKUs=
206
github.com/urfave/cli/v2 v2.25.7/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
217
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
228
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
23-
golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g=
24-
golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0=
259
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df h1:UA2aFVmmsIlefxMk29Dp2juaUSth8Pyn3Tq5Y5mJGME=
2610
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
27-
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
28-
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
29-
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
30-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
31-
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
11+
golang.org/x/mod v0.11.0 h1:bUO06HqtnRcc/7l71XBe4WcqTZ+3AH1J59zWDDwLKgU=
12+
golang.org/x/mod v0.11.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
13+
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
14+
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
15+
golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM=
16+
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=

pkg/actions/separate-interface/runner.go

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ import (
1212
"path"
1313
"strings"
1414

15-
"github.com/mpyw/sqlc-restruct/pkg/internal/astutil"
1615
"golang.org/x/exp/slices"
16+
"golang.org/x/tools/imports"
17+
18+
"github.com/mpyw/sqlc-restruct/pkg/internal/astutil"
1719
)
1820

1921
type runner struct {
@@ -43,19 +45,32 @@ func (r *runner) Run() error {
4345
if newModelsContent, err = r.newModelsContent(); err != nil {
4446
return err
4547
}
48+
newModelsContent, err = imports.Process("", newModelsContent, nil)
49+
if err != nil {
50+
return err
51+
}
4652
continue
4753
}
4854
if filename == r.input.QuerierFileName {
4955
if newQuerierContent, err = r.newQuerierContent(); err != nil {
5056
return err
5157
}
58+
newQuerierContent, err = imports.Process("", newQuerierContent, nil)
59+
if err != nil {
60+
return err
61+
}
5262
continue
5363
}
54-
if strings.HasSuffix(filename, r.input.ImplSQLSuffix) {
64+
if r.isImplFile(filename) {
5565
var newQueriesContent []byte
5666
if newQueriesContent, err = r.newQueriesContent(filename); err != nil {
5767
return err
5868
}
69+
newQueriesContent, err = imports.Process("", newQueriesContent, nil)
70+
if err != nil {
71+
return err
72+
}
73+
5974
if newQueriesContents == nil {
6075
newQueriesContents = make(map[string][]byte)
6176
}
@@ -66,21 +81,21 @@ func (r *runner) Run() error {
6681

6782
if newModelsContent != nil {
6883
_ = os.Remove(path.Join(r.input.ModelsDir, r.input.ModelsFileName))
69-
if err := os.WriteFile(path.Join(r.input.ModelsDir, r.input.ModelsFileName), newModelsContent, 0644); err != nil {
84+
if err := os.WriteFile(path.Join(r.input.ModelsDir, r.input.ModelsFileName), newModelsContent, 0o644); err != nil {
7085
return fmt.Errorf("runner.Run() failed: %w", err)
7186
}
7287
_ = os.Remove(path.Join(r.input.ImplDir, r.input.ModelsFileName))
7388
}
7489
if newQuerierContent != nil {
7590
_ = os.Remove(path.Join(r.input.IfaceDir, r.input.QuerierFileName))
76-
if err := os.WriteFile(path.Join(r.input.IfaceDir, r.input.QuerierFileName), newQuerierContent, 0644); err != nil {
91+
if err := os.WriteFile(path.Join(r.input.IfaceDir, r.input.QuerierFileName), newQuerierContent, 0o644); err != nil {
7792
return fmt.Errorf("runner.Run() failed: %w", err)
7893
}
7994
_ = os.Remove(path.Join(r.input.ImplDir, r.input.QuerierFileName))
8095
}
8196
for filename, content := range newQueriesContents {
8297
_ = os.Remove(path.Join(r.input.ImplDir, filename))
83-
if err := os.WriteFile(path.Join(r.input.ImplDir, filename), content, 0644); err != nil {
98+
if err := os.WriteFile(path.Join(r.input.ImplDir, filename), content, 0o644); err != nil {
8499
return fmt.Errorf("runner.Run() failed: %w", err)
85100
}
86101
}
@@ -168,7 +183,7 @@ func (r *runner) newQuerierContent() ([]byte, error) {
168183
}
169184

170185
for _, dirEntry := range dirEntries {
171-
if !strings.HasSuffix(dirEntry.Name(), r.input.ImplSQLSuffix) {
186+
if !r.isImplFile(dirEntry.Name()) {
172187
continue
173188
}
174189

@@ -273,3 +288,16 @@ func (r *runner) newQueriesContent(filename string) ([]byte, error) {
273288
}
274289
return byt, nil
275290
}
291+
292+
func (r *runner) isImplFile(filename string) bool {
293+
return strings.HasSuffix(filename, r.input.ImplSQLSuffix) || inStrings(filename, r.input.AditionalQuerierFiles)
294+
}
295+
296+
func inStrings(s string, slice []string) bool {
297+
for _, v := range slice {
298+
if v == s {
299+
return true
300+
}
301+
}
302+
return false
303+
}

pkg/actions/separate-interface/separate_interface.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ type ActionInput struct {
2828
ModelsFileName string
2929
// QuerierFileName The file name for the sqlc-generated Querier file.
3030
QuerierFileName string
31+
// AditionalQuerierFiles A list of other files that contains querier impl.
32+
AditionalQuerierFiles []string
3133
}
3234

3335
func Action(_ context.Context, input ActionInput) error {

0 commit comments

Comments
 (0)