From 36107dd9bfd2df499c5cddfcf7d9ab9839fe63a6 Mon Sep 17 00:00:00 2001 From: Bruce Smith Date: Sun, 8 Sep 2024 17:50:06 -0400 Subject: [PATCH 1/7] added raw comments to plugin --- go.mod | 12 +- go.sum | 19 +- internal/cmd/shim.go | 18 +- internal/compiler/compile.go | 46 ++++- internal/compiler/parse.go | 16 +- internal/compiler/resolve.go | 1 - internal/engine/postgresql/convert.go | 12 +- internal/metadata/meta.go | 18 +- internal/plugin/codegen.pb.go | 279 +++++++++++++++----------- internal/source/code.go | 35 ++++ internal/sql/catalog/catalog.go | 17 +- internal/sql/catalog/schema.go | 18 +- internal/sql/catalog/table.go | 24 ++- internal/sql/catalog/types.go | 31 ++- protos/plugin/codegen.proto | 4 + 15 files changed, 357 insertions(+), 193 deletions(-) diff --git a/go.mod b/go.mod index fe705481ca..6e19713b73 100644 --- a/go.mod +++ b/go.mod @@ -1,8 +1,6 @@ module github.com/sqlc-dev/sqlc -go 1.22 - -toolchain go1.22.5 +go 1.23.0 require ( github.com/antlr4-go/antlr/v4 v4.13.1 @@ -14,11 +12,13 @@ require ( github.com/google/go-cmp v0.6.0 github.com/jackc/pgx/v4 v4.18.3 github.com/jackc/pgx/v5 v5.6.0 + github.com/jhump/protoreflect v1.17.0 github.com/jinzhu/inflection v1.0.0 github.com/lib/pq v1.10.9 github.com/pganalyze/pg_query_go/v5 v5.1.0 github.com/pingcap/tidb/pkg/parser v0.0.0-20231103154709-4f00ece106b1 github.com/riza-io/grpc-go v0.2.0 + github.com/ryboe/q v1.0.23 github.com/spf13/cobra v1.8.1 github.com/spf13/pflag v1.0.5 github.com/tetratelabs/wazero v1.8.0 @@ -33,8 +33,10 @@ require ( require ( filippo.io/edwards25519 v1.1.0 // indirect + github.com/bufbuild/protocompile v0.14.1 // indirect github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548 // indirect github.com/dustin/go-humanize v1.0.1 // indirect + github.com/golang/protobuf v1.5.4 // indirect github.com/google/uuid v1.6.0 // indirect github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect @@ -46,13 +48,15 @@ require ( github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect github.com/jackc/pgtype v1.14.0 // indirect github.com/jackc/puddle/v2 v2.2.1 // indirect + github.com/kr/pretty v0.3.1 // indirect + github.com/kr/text v0.2.0 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/ncruces/go-strftime v0.1.9 // indirect github.com/pingcap/errors v0.11.5-0.20210425183316-da1aaba5fb63 // indirect github.com/pingcap/failpoint v0.0.0-20220801062533-2eaa32854a6c // indirect github.com/pingcap/log v1.1.0 // indirect github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect - github.com/rogpeppe/go-internal v1.10.0 // indirect + github.com/rogpeppe/go-internal v1.12.0 // indirect github.com/stoewer/go-strcase v1.2.0 // indirect github.com/wasilibs/wazero-helpers v0.0.0-20240604052452-61d7981e9a38 // indirect github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect diff --git a/go.sum b/go.sum index 6631673ad5..36b508ce5f 100644 --- a/go.sum +++ b/go.sum @@ -5,12 +5,15 @@ github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0 github.com/antlr4-go/antlr/v4 v4.13.1 h1:SqQKkuVZ+zWkMMNkjy5FZe5mr5WURWnlpmOuzYWrPrQ= github.com/antlr4-go/antlr/v4 v4.13.1/go.mod h1:GKmUxMtwp6ZgGwZSva4eWPC5mS6vUAmOABFgjdkM7Nw= github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= +github.com/bufbuild/protocompile v0.14.1 h1:iA73zAf/fyljNjQKwYzUHD6AD4R8KMasmwa/FBatYVw= +github.com/bufbuild/protocompile v0.14.1/go.mod h1:ppVdAIhbr2H8asPk6k4pY7t9zB1OU5DoEw9xY/FUi1c= github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I= github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ= github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/cubicdaiya/gonp v1.0.4 h1:ky2uIAJh81WiLcGKBVD5R7KsM/36W6IqqTy6Bo6rGws= github.com/cubicdaiya/gonp v1.0.4/go.mod h1:iWGuP/7+JVTn02OWhRemVbMmG1DOUnmrGTYYACpOI0I= github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548 h1:iwZdTE0PVqJCos1vaoKsclOGD3ADKpshg3SRtYBbwso= @@ -30,6 +33,8 @@ github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/me github.com/gofrs/uuid v4.0.0+incompatible h1:1SD/1F5pU8p29ybwgQSwpQk+mwdRrXCYuPhW6m+TnJw= github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= +github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= github.com/google/cel-go v0.21.0 h1:cl6uW/gxN+Hy50tNYvI691+sXxioCnstFzLp2WO4GCI= github.com/google/cel-go v0.21.0/go.mod h1:rHUlWCcBKgyEk+eV03RPdZUekPp6YcJwV0FxuUksYxc= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= @@ -95,14 +100,16 @@ github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0f github.com/jackc/puddle v1.1.3/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk= github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= +github.com/jhump/protoreflect v1.17.0 h1:qOEr613fac2lOuTgWN4tPAtLL7fUSbuJL5X5XumQh94= +github.com/jhump/protoreflect v1.17.0/go.mod h1:h9+vUUL38jiBzck8ck+6G/aeMX8Z4QUY/NiJPwPNi+8= github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= -github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= -github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= @@ -135,6 +142,7 @@ github.com/pingcap/log v1.1.0 h1:ELiPxACz7vdo1qAvvaWJg1NrYFoY6gqAh/+Uo6aXdD8= github.com/pingcap/log v1.1.0/go.mod h1:DWQW5jICDR7UJh4HtxXSM20Churx4CQL0fwL/SoOSA4= github.com/pingcap/tidb/pkg/parser v0.0.0-20231103154709-4f00ece106b1 h1:SwGY3zMnK4wO85vvRIqrR3Yh6VpIC9pydG0QNOUPHCY= github.com/pingcap/tidb/pkg/parser v0.0.0-20231103154709-4f00ece106b1/go.mod h1:yRkiqLFwIqibYg2P7h4bclHjHcJiIFRLKhGRyBcKYus= +github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= @@ -145,12 +153,15 @@ github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qq github.com/riza-io/grpc-go v0.2.0 h1:2HxQKFVE7VuYstcJ8zqpN84VnAoJ4dCL6YFhJewNcHQ= github.com/riza-io/grpc-go v0.2.0/go.mod h1:2bDvR9KkKC3KhtlSHfR3dAXjUMT86kg4UfWFyVGWqi8= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= -github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= -github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= +github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= +github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8= +github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4= github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ= github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU= github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/ryboe/q v1.0.23 h1:IZ2ugkpRCkceZ9wds+2y7yF2TrUoVWGGkPVYJfVczeI= +github.com/ryboe/q v1.0.23/go.mod h1:km7Zxv4Bo5b7ND8WDRrg97XR3pircncCiFJEE9cZidQ= github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9NzErvs504Cn4c5DxATwIqPbtswREoFCre64PpcG4= diff --git a/internal/cmd/shim.go b/internal/cmd/shim.go index 654500429a..57f36c4765 100644 --- a/internal/cmd/shim.go +++ b/internal/cmd/shim.go @@ -68,14 +68,16 @@ func pluginCatalog(c *catalog.Catalog) *plugin.Catalog { switch typ := typ.(type) { case *catalog.Enum: enums = append(enums, &plugin.Enum{ - Name: typ.Name, - Comment: typ.Comment, - Vals: typ.Vals, + Name: typ.Name, + Comment: typ.Comment, + Vals: typ.Vals, + RawComments: typ.RawComments, }) case *catalog.CompositeType: cts = append(cts, &plugin.CompositeType{ - Name: typ.Name, - Comment: typ.Comment, + Name: typ.Name, + Comment: typ.Comment, + RawComments: typ.RawComments, }) } } @@ -113,8 +115,9 @@ func pluginCatalog(c *catalog.Catalog) *plugin.Catalog { Schema: t.Rel.Schema, Name: t.Rel.Name, }, - Columns: columns, - Comment: t.Comment, + Columns: columns, + Comment: t.Comment, + RawComments: t.RawComments, }) } schemas = append(schemas, &plugin.Schema{ @@ -123,6 +126,7 @@ func pluginCatalog(c *catalog.Catalog) *plugin.Catalog { Tables: tables, Enums: enums, CompositeTypes: cts, + RawComments: s.RawComments, }) } return &plugin.Catalog{ diff --git a/internal/compiler/compile.go b/internal/compiler/compile.go index 84fbb20a3c..36e6bf918f 100644 --- a/internal/compiler/compile.go +++ b/internal/compiler/compile.go @@ -37,15 +37,28 @@ func (c *Compiler) parseCatalog(schemas []string) error { merr.Add(filename, "", 0, err) continue } - contents := migrations.RemoveRollbackStatements(string(blob)) + src := string(blob) + contents := migrations.RemoveRollbackStatements(src) c.schema = append(c.schema, contents) stmts, err := c.parser.Parse(strings.NewReader(contents)) if err != nil { merr.Add(filename, contents, 0, err) continue } - for i := range stmts { - if err := c.catalog.Update(stmts[i], c); err != nil { + for i, stmt := range stmts { + // Lets just parse the metadata annotations and pass them in. + // Update will use them on Types we deem neccesary. + // Can either be metadata or just a raw []strings + _, rawSQL, err := getRaw(stmt.Raw, src) + if err != nil { + return err + } + RawComments, err := source.RawComments(rawSQL, c.parser.CommentSyntax()) + if err != nil { + return err + } + + if err := c.catalog.Update(stmts[i], RawComments, c); err != nil { merr.Add(filename, contents, stmts[i].Pos(), err) continue } @@ -99,7 +112,12 @@ func (c *Compiler) parseQueries(o opts.Parser) (*Result, error) { queryName := query.Metadata.Name if queryName != "" { if _, exists := set[queryName]; exists { - merr.Add(filename, src, stmt.Raw.Pos(), fmt.Errorf("duplicate query name: %s", queryName)) + merr.Add( + filename, + src, + stmt.Raw.Pos(), + fmt.Errorf("duplicate query name: %s", queryName), + ) continue } set[queryName] = struct{}{} @@ -111,10 +129,28 @@ func (c *Compiler) parseQueries(o opts.Parser) (*Result, error) { return nil, merr } if len(q) == 0 { - return nil, fmt.Errorf("no queries contained in paths %s", strings.Join(c.conf.Queries, ",")) + return nil, fmt.Errorf( + "no queries contained in paths %s", + strings.Join(c.conf.Queries, ","), + ) } return &Result{ Catalog: c.catalog, Queries: q, }, nil } + +func getRaw(stmt ast.Node, src string) (*ast.RawStmt, string, error) { + raw, ok := stmt.(*ast.RawStmt) + if !ok { + return nil, "", errors.New("node is not a statement") + } + rawSQL, err := source.Pluck(src, raw.StmtLocation, raw.StmtLen) + if err != nil { + return nil, "", err + } + if rawSQL == "" { + return nil, "", errors.New("missing semicolon at end of file") + } + return raw, rawSQL, nil +} diff --git a/internal/compiler/parse.go b/internal/compiler/parse.go index 022d23ea22..d5089a2f6e 100644 --- a/internal/compiler/parse.go +++ b/internal/compiler/parse.go @@ -2,7 +2,6 @@ package compiler import ( "context" - "errors" "fmt" "strings" @@ -28,20 +27,15 @@ func (c *Compiler) parseQuery(stmt ast.Node, src string, o opts.Parser) (*Query, } // rewrite queries to remove sqlc.* functions - - raw, ok := stmt.(*ast.RawStmt) - if !ok { - return nil, errors.New("node is not a statement") - } - rawSQL, err := source.Pluck(src, raw.StmtLocation, raw.StmtLen) + raw, rawSQL, err := getRaw(stmt, src) if err != nil { return nil, err } - if rawSQL == "" { - return nil, errors.New("missing semicolon at end of file") - } - name, cmd, err := metadata.ParseQueryNameAndType(rawSQL, metadata.CommentSyntax(c.parser.CommentSyntax())) + name, cmd, err := metadata.ParseQueryNameAndType( + rawSQL, + metadata.CommentSyntax(c.parser.CommentSyntax()), + ) if err != nil { return nil, err } diff --git a/internal/compiler/resolve.go b/internal/compiler/resolve.go index b1fbb1990e..5136a7a493 100644 --- a/internal/compiler/resolve.go +++ b/internal/compiler/resolve.go @@ -268,7 +268,6 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar, case *ast.BetweenExpr: if n == nil || n.Expr == nil || n.Left == nil || n.Right == nil { - fmt.Println("ast.BetweenExpr is nil") continue } diff --git a/internal/engine/postgresql/convert.go b/internal/engine/postgresql/convert.go index 0c77beecf9..dc29ff0740 100644 --- a/internal/engine/postgresql/convert.go +++ b/internal/engine/postgresql/convert.go @@ -237,7 +237,9 @@ func convertAlterDatabaseStmt(n *pg.AlterDatabaseStmt) *ast.AlterDatabaseStmt { } } -func convertAlterDefaultPrivilegesStmt(n *pg.AlterDefaultPrivilegesStmt) *ast.AlterDefaultPrivilegesStmt { +func convertAlterDefaultPrivilegesStmt( + n *pg.AlterDefaultPrivilegesStmt, +) *ast.AlterDefaultPrivilegesStmt { if n == nil { return nil } @@ -285,7 +287,9 @@ func convertAlterEventTrigStmt(n *pg.AlterEventTrigStmt) *ast.AlterEventTrigStmt } } -func convertAlterExtensionContentsStmt(n *pg.AlterExtensionContentsStmt) *ast.AlterExtensionContentsStmt { +func convertAlterExtensionContentsStmt( + n *pg.AlterExtensionContentsStmt, +) *ast.AlterExtensionContentsStmt { if n == nil { return nil } @@ -535,7 +539,9 @@ func convertAlterTableMoveAllStmt(n *pg.AlterTableMoveAllStmt) *ast.AlterTableMo } } -func convertAlterTableSpaceOptionsStmt(n *pg.AlterTableSpaceOptionsStmt) *ast.AlterTableSpaceOptionsStmt { +func convertAlterTableSpaceOptionsStmt( + n *pg.AlterTableSpaceOptionsStmt, +) *ast.AlterTableSpaceOptionsStmt { if n == nil { return nil } diff --git a/internal/metadata/meta.go b/internal/metadata/meta.go index 97ff36dbd2..40532786ff 100644 --- a/internal/metadata/meta.go +++ b/internal/metadata/meta.go @@ -17,7 +17,7 @@ type Metadata struct { Comments []string Params map[string]string Flags map[string]bool - + Raw []string Filename string } @@ -93,7 +93,10 @@ func ParseQueryNameAndType(t string, commentStyle CommentSyntax) (string, string part = part[:len(part)-1] // removes the trailing "*/" element } if len(part) == 3 { - return "", "", fmt.Errorf("missing query type [':one', ':many', ':exec', ':execrows', ':execlastid', ':execresult', ':copyfrom', 'batchexec', 'batchmany', 'batchone']: %s", line) + return "", "", fmt.Errorf( + "missing query type [':one', ':many', ':exec', ':execrows', ':execlastid', ':execresult', ':copyfrom', 'batchexec', 'batchmany', 'batchone']: %s", + line, + ) } if len(part) != 4 { return "", "", fmt.Errorf("invalid query comment: %s", line) @@ -101,7 +104,16 @@ func ParseQueryNameAndType(t string, commentStyle CommentSyntax) (string, string queryName := part[2] queryType := strings.TrimSpace(part[3]) switch queryType { - case CmdOne, CmdMany, CmdExec, CmdExecResult, CmdExecRows, CmdExecLastId, CmdCopyFrom, CmdBatchExec, CmdBatchMany, CmdBatchOne: + case CmdOne, + CmdMany, + CmdExec, + CmdExecResult, + CmdExecRows, + CmdExecLastId, + CmdCopyFrom, + CmdBatchExec, + CmdBatchMany, + CmdBatchOne: default: return "", "", fmt.Errorf("invalid query type: %s", queryType) } diff --git a/internal/plugin/codegen.pb.go b/internal/plugin/codegen.pb.go index 525ffc72ef..7a62b452ed 100644 --- a/internal/plugin/codegen.pb.go +++ b/internal/plugin/codegen.pb.go @@ -322,6 +322,7 @@ type Schema struct { Tables []*Table `protobuf:"bytes,3,rep,name=tables,proto3" json:"tables,omitempty"` Enums []*Enum `protobuf:"bytes,4,rep,name=enums,proto3" json:"enums,omitempty"` CompositeTypes []*CompositeType `protobuf:"bytes,5,rep,name=composite_types,json=compositeTypes,proto3" json:"composite_types,omitempty"` + RawComments []string `protobuf:"bytes,6,rep,name=raw_comments,json=rawComments,proto3" json:"raw_comments,omitempty"` } func (x *Schema) Reset() { @@ -391,13 +392,21 @@ func (x *Schema) GetCompositeTypes() []*CompositeType { return nil } +func (x *Schema) GetRawComments() []string { + if x != nil { + return x.RawComments + } + return nil +} + type CompositeType struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` - Comment string `protobuf:"bytes,2,opt,name=comment,proto3" json:"comment,omitempty"` + Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` + Comment string `protobuf:"bytes,2,opt,name=comment,proto3" json:"comment,omitempty"` + RawComments []string `protobuf:"bytes,3,rep,name=raw_comments,json=rawComments,proto3" json:"raw_comments,omitempty"` } func (x *CompositeType) Reset() { @@ -446,14 +455,22 @@ func (x *CompositeType) GetComment() string { return "" } +func (x *CompositeType) GetRawComments() []string { + if x != nil { + return x.RawComments + } + return nil +} + type Enum struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` - Vals []string `protobuf:"bytes,2,rep,name=vals,proto3" json:"vals,omitempty"` - Comment string `protobuf:"bytes,3,opt,name=comment,proto3" json:"comment,omitempty"` + Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` + Vals []string `protobuf:"bytes,2,rep,name=vals,proto3" json:"vals,omitempty"` + Comment string `protobuf:"bytes,3,opt,name=comment,proto3" json:"comment,omitempty"` + RawComments []string `protobuf:"bytes,4,rep,name=raw_comments,json=rawComments,proto3" json:"raw_comments,omitempty"` } func (x *Enum) Reset() { @@ -509,14 +526,22 @@ func (x *Enum) GetComment() string { return "" } +func (x *Enum) GetRawComments() []string { + if x != nil { + return x.RawComments + } + return nil +} + type Table struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Rel *Identifier `protobuf:"bytes,1,opt,name=rel,proto3" json:"rel,omitempty"` - Columns []*Column `protobuf:"bytes,2,rep,name=columns,proto3" json:"columns,omitempty"` - Comment string `protobuf:"bytes,3,opt,name=comment,proto3" json:"comment,omitempty"` + Rel *Identifier `protobuf:"bytes,1,opt,name=rel,proto3" json:"rel,omitempty"` + Columns []*Column `protobuf:"bytes,2,rep,name=columns,proto3" json:"columns,omitempty"` + Comment string `protobuf:"bytes,3,opt,name=comment,proto3" json:"comment,omitempty"` + RawComments []string `protobuf:"bytes,4,rep,name=raw_comments,json=rawComments,proto3" json:"raw_comments,omitempty"` } func (x *Table) Reset() { @@ -572,6 +597,13 @@ func (x *Table) GetComment() string { return "" } +func (x *Table) GetRawComments() []string { + if x != nil { + return x.RawComments + } + return nil +} + type Identifier struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -1242,7 +1274,7 @@ var file_plugin_codegen_proto_rawDesc = []byte{ 0x61, 0x6d, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x28, 0x0a, 0x07, 0x73, 0x63, 0x68, 0x65, 0x6d, 0x61, 0x73, 0x18, 0x04, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2e, 0x53, 0x63, 0x68, 0x65, 0x6d, 0x61, - 0x52, 0x07, 0x73, 0x63, 0x68, 0x65, 0x6d, 0x61, 0x73, 0x22, 0xc1, 0x01, 0x0a, 0x06, 0x53, 0x63, + 0x52, 0x07, 0x73, 0x63, 0x68, 0x65, 0x6d, 0x61, 0x73, 0x22, 0xe4, 0x01, 0x0a, 0x06, 0x53, 0x63, 0x68, 0x65, 0x6d, 0x61, 0x12, 0x18, 0x0a, 0x07, 0x63, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x63, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, @@ -1254,117 +1286,126 @@ var file_plugin_codegen_proto_rawDesc = []byte{ 0x0f, 0x63, 0x6f, 0x6d, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x65, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x73, 0x18, 0x05, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x15, 0x2e, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2e, 0x43, 0x6f, 0x6d, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x65, 0x54, 0x79, 0x70, 0x65, 0x52, 0x0e, 0x63, - 0x6f, 0x6d, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x65, 0x54, 0x79, 0x70, 0x65, 0x73, 0x22, 0x3d, 0x0a, - 0x0d, 0x43, 0x6f, 0x6d, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x65, 0x54, 0x79, 0x70, 0x65, 0x12, 0x12, - 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, - 0x6d, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x63, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x18, 0x02, 0x20, - 0x01, 0x28, 0x09, 0x52, 0x07, 0x63, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x22, 0x48, 0x0a, 0x04, - 0x45, 0x6e, 0x75, 0x6d, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, - 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x76, 0x61, 0x6c, 0x73, - 0x18, 0x02, 0x20, 0x03, 0x28, 0x09, 0x52, 0x04, 0x76, 0x61, 0x6c, 0x73, 0x12, 0x18, 0x0a, 0x07, - 0x63, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x63, - 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x22, 0x71, 0x0a, 0x05, 0x54, 0x61, 0x62, 0x6c, 0x65, 0x12, - 0x24, 0x0a, 0x03, 0x72, 0x65, 0x6c, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x12, 0x2e, 0x70, - 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2e, 0x49, 0x64, 0x65, 0x6e, 0x74, 0x69, 0x66, 0x69, 0x65, 0x72, - 0x52, 0x03, 0x72, 0x65, 0x6c, 0x12, 0x28, 0x0a, 0x07, 0x63, 0x6f, 0x6c, 0x75, 0x6d, 0x6e, 0x73, - 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2e, - 0x43, 0x6f, 0x6c, 0x75, 0x6d, 0x6e, 0x52, 0x07, 0x63, 0x6f, 0x6c, 0x75, 0x6d, 0x6e, 0x73, 0x12, - 0x18, 0x0a, 0x07, 0x63, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, - 0x52, 0x07, 0x63, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x22, 0x52, 0x0a, 0x0a, 0x49, 0x64, 0x65, - 0x6e, 0x74, 0x69, 0x66, 0x69, 0x65, 0x72, 0x12, 0x18, 0x0a, 0x07, 0x63, 0x61, 0x74, 0x61, 0x6c, - 0x6f, 0x67, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x63, 0x61, 0x74, 0x61, 0x6c, 0x6f, - 0x67, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x63, 0x68, 0x65, 0x6d, 0x61, 0x18, 0x02, 0x20, 0x01, 0x28, - 0x09, 0x52, 0x06, 0x73, 0x63, 0x68, 0x65, 0x6d, 0x61, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, - 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x22, 0x8e, 0x04, - 0x0a, 0x06, 0x43, 0x6f, 0x6c, 0x75, 0x6d, 0x6e, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, - 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x19, 0x0a, 0x08, - 0x6e, 0x6f, 0x74, 0x5f, 0x6e, 0x75, 0x6c, 0x6c, 0x18, 0x03, 0x20, 0x01, 0x28, 0x08, 0x52, 0x07, - 0x6e, 0x6f, 0x74, 0x4e, 0x75, 0x6c, 0x6c, 0x12, 0x19, 0x0a, 0x08, 0x69, 0x73, 0x5f, 0x61, 0x72, - 0x72, 0x61, 0x79, 0x18, 0x04, 0x20, 0x01, 0x28, 0x08, 0x52, 0x07, 0x69, 0x73, 0x41, 0x72, 0x72, - 0x61, 0x79, 0x12, 0x18, 0x0a, 0x07, 0x63, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x18, 0x05, 0x20, - 0x01, 0x28, 0x09, 0x52, 0x07, 0x63, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x12, 0x16, 0x0a, 0x06, - 0x6c, 0x65, 0x6e, 0x67, 0x74, 0x68, 0x18, 0x06, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x6c, 0x65, - 0x6e, 0x67, 0x74, 0x68, 0x12, 0x24, 0x0a, 0x0e, 0x69, 0x73, 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x64, - 0x5f, 0x70, 0x61, 0x72, 0x61, 0x6d, 0x18, 0x07, 0x20, 0x01, 0x28, 0x08, 0x52, 0x0c, 0x69, 0x73, - 0x4e, 0x61, 0x6d, 0x65, 0x64, 0x50, 0x61, 0x72, 0x61, 0x6d, 0x12, 0x20, 0x0a, 0x0c, 0x69, 0x73, - 0x5f, 0x66, 0x75, 0x6e, 0x63, 0x5f, 0x63, 0x61, 0x6c, 0x6c, 0x18, 0x08, 0x20, 0x01, 0x28, 0x08, - 0x52, 0x0a, 0x69, 0x73, 0x46, 0x75, 0x6e, 0x63, 0x43, 0x61, 0x6c, 0x6c, 0x12, 0x14, 0x0a, 0x05, - 0x73, 0x63, 0x6f, 0x70, 0x65, 0x18, 0x09, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x73, 0x63, 0x6f, - 0x70, 0x65, 0x12, 0x28, 0x0a, 0x05, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x18, 0x0a, 0x20, 0x01, 0x28, - 0x0b, 0x32, 0x12, 0x2e, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2e, 0x49, 0x64, 0x65, 0x6e, 0x74, - 0x69, 0x66, 0x69, 0x65, 0x72, 0x52, 0x05, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x12, 0x1f, 0x0a, 0x0b, - 0x74, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x61, 0x6c, 0x69, 0x61, 0x73, 0x18, 0x0b, 0x20, 0x01, 0x28, - 0x09, 0x52, 0x0a, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x41, 0x6c, 0x69, 0x61, 0x73, 0x12, 0x26, 0x0a, - 0x04, 0x74, 0x79, 0x70, 0x65, 0x18, 0x0c, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x12, 0x2e, 0x70, 0x6c, - 0x75, 0x67, 0x69, 0x6e, 0x2e, 0x49, 0x64, 0x65, 0x6e, 0x74, 0x69, 0x66, 0x69, 0x65, 0x72, 0x52, - 0x04, 0x74, 0x79, 0x70, 0x65, 0x12, 0x22, 0x0a, 0x0d, 0x69, 0x73, 0x5f, 0x73, 0x71, 0x6c, 0x63, - 0x5f, 0x73, 0x6c, 0x69, 0x63, 0x65, 0x18, 0x0d, 0x20, 0x01, 0x28, 0x08, 0x52, 0x0b, 0x69, 0x73, - 0x53, 0x71, 0x6c, 0x63, 0x53, 0x6c, 0x69, 0x63, 0x65, 0x12, 0x33, 0x0a, 0x0b, 0x65, 0x6d, 0x62, - 0x65, 0x64, 0x5f, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x18, 0x0e, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x12, + 0x6f, 0x6d, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x65, 0x54, 0x79, 0x70, 0x65, 0x73, 0x12, 0x21, 0x0a, + 0x0c, 0x72, 0x61, 0x77, 0x5f, 0x63, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x73, 0x18, 0x06, 0x20, + 0x03, 0x28, 0x09, 0x52, 0x0b, 0x72, 0x61, 0x77, 0x43, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x73, + 0x22, 0x60, 0x0a, 0x0d, 0x43, 0x6f, 0x6d, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x65, 0x54, 0x79, 0x70, + 0x65, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x63, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x74, + 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x63, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x12, + 0x21, 0x0a, 0x0c, 0x72, 0x61, 0x77, 0x5f, 0x63, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x73, 0x18, + 0x03, 0x20, 0x03, 0x28, 0x09, 0x52, 0x0b, 0x72, 0x61, 0x77, 0x43, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, + 0x74, 0x73, 0x22, 0x6b, 0x0a, 0x04, 0x45, 0x6e, 0x75, 0x6d, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, + 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x12, + 0x0a, 0x04, 0x76, 0x61, 0x6c, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28, 0x09, 0x52, 0x04, 0x76, 0x61, + 0x6c, 0x73, 0x12, 0x18, 0x0a, 0x07, 0x63, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x18, 0x03, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x07, 0x63, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x12, 0x21, 0x0a, 0x0c, + 0x72, 0x61, 0x77, 0x5f, 0x63, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x73, 0x18, 0x04, 0x20, 0x03, + 0x28, 0x09, 0x52, 0x0b, 0x72, 0x61, 0x77, 0x43, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x73, 0x22, + 0x94, 0x01, 0x0a, 0x05, 0x54, 0x61, 0x62, 0x6c, 0x65, 0x12, 0x24, 0x0a, 0x03, 0x72, 0x65, 0x6c, + 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x12, 0x2e, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2e, + 0x49, 0x64, 0x65, 0x6e, 0x74, 0x69, 0x66, 0x69, 0x65, 0x72, 0x52, 0x03, 0x72, 0x65, 0x6c, 0x12, + 0x28, 0x0a, 0x07, 0x63, 0x6f, 0x6c, 0x75, 0x6d, 0x6e, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, + 0x32, 0x0e, 0x2e, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2e, 0x43, 0x6f, 0x6c, 0x75, 0x6d, 0x6e, + 0x52, 0x07, 0x63, 0x6f, 0x6c, 0x75, 0x6d, 0x6e, 0x73, 0x12, 0x18, 0x0a, 0x07, 0x63, 0x6f, 0x6d, + 0x6d, 0x65, 0x6e, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x63, 0x6f, 0x6d, 0x6d, + 0x65, 0x6e, 0x74, 0x12, 0x21, 0x0a, 0x0c, 0x72, 0x61, 0x77, 0x5f, 0x63, 0x6f, 0x6d, 0x6d, 0x65, + 0x6e, 0x74, 0x73, 0x18, 0x04, 0x20, 0x03, 0x28, 0x09, 0x52, 0x0b, 0x72, 0x61, 0x77, 0x43, 0x6f, + 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x73, 0x22, 0x52, 0x0a, 0x0a, 0x49, 0x64, 0x65, 0x6e, 0x74, 0x69, + 0x66, 0x69, 0x65, 0x72, 0x12, 0x18, 0x0a, 0x07, 0x63, 0x61, 0x74, 0x61, 0x6c, 0x6f, 0x67, 0x18, + 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x63, 0x61, 0x74, 0x61, 0x6c, 0x6f, 0x67, 0x12, 0x16, + 0x0a, 0x06, 0x73, 0x63, 0x68, 0x65, 0x6d, 0x61, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, + 0x73, 0x63, 0x68, 0x65, 0x6d, 0x61, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x03, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x22, 0x8e, 0x04, 0x0a, 0x06, 0x43, + 0x6f, 0x6c, 0x75, 0x6d, 0x6e, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x19, 0x0a, 0x08, 0x6e, 0x6f, 0x74, + 0x5f, 0x6e, 0x75, 0x6c, 0x6c, 0x18, 0x03, 0x20, 0x01, 0x28, 0x08, 0x52, 0x07, 0x6e, 0x6f, 0x74, + 0x4e, 0x75, 0x6c, 0x6c, 0x12, 0x19, 0x0a, 0x08, 0x69, 0x73, 0x5f, 0x61, 0x72, 0x72, 0x61, 0x79, + 0x18, 0x04, 0x20, 0x01, 0x28, 0x08, 0x52, 0x07, 0x69, 0x73, 0x41, 0x72, 0x72, 0x61, 0x79, 0x12, + 0x18, 0x0a, 0x07, 0x63, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, + 0x52, 0x07, 0x63, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x12, 0x16, 0x0a, 0x06, 0x6c, 0x65, 0x6e, + 0x67, 0x74, 0x68, 0x18, 0x06, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x6c, 0x65, 0x6e, 0x67, 0x74, + 0x68, 0x12, 0x24, 0x0a, 0x0e, 0x69, 0x73, 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x64, 0x5f, 0x70, 0x61, + 0x72, 0x61, 0x6d, 0x18, 0x07, 0x20, 0x01, 0x28, 0x08, 0x52, 0x0c, 0x69, 0x73, 0x4e, 0x61, 0x6d, + 0x65, 0x64, 0x50, 0x61, 0x72, 0x61, 0x6d, 0x12, 0x20, 0x0a, 0x0c, 0x69, 0x73, 0x5f, 0x66, 0x75, + 0x6e, 0x63, 0x5f, 0x63, 0x61, 0x6c, 0x6c, 0x18, 0x08, 0x20, 0x01, 0x28, 0x08, 0x52, 0x0a, 0x69, + 0x73, 0x46, 0x75, 0x6e, 0x63, 0x43, 0x61, 0x6c, 0x6c, 0x12, 0x14, 0x0a, 0x05, 0x73, 0x63, 0x6f, + 0x70, 0x65, 0x18, 0x09, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x73, 0x63, 0x6f, 0x70, 0x65, 0x12, + 0x28, 0x0a, 0x05, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x18, 0x0a, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x12, 0x2e, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2e, 0x49, 0x64, 0x65, 0x6e, 0x74, 0x69, 0x66, 0x69, - 0x65, 0x72, 0x52, 0x0a, 0x65, 0x6d, 0x62, 0x65, 0x64, 0x54, 0x61, 0x62, 0x6c, 0x65, 0x12, 0x23, - 0x0a, 0x0d, 0x6f, 0x72, 0x69, 0x67, 0x69, 0x6e, 0x61, 0x6c, 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x18, - 0x0f, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0c, 0x6f, 0x72, 0x69, 0x67, 0x69, 0x6e, 0x61, 0x6c, 0x4e, - 0x61, 0x6d, 0x65, 0x12, 0x1a, 0x0a, 0x08, 0x75, 0x6e, 0x73, 0x69, 0x67, 0x6e, 0x65, 0x64, 0x18, - 0x10, 0x20, 0x01, 0x28, 0x08, 0x52, 0x08, 0x75, 0x6e, 0x73, 0x69, 0x67, 0x6e, 0x65, 0x64, 0x12, - 0x1d, 0x0a, 0x0a, 0x61, 0x72, 0x72, 0x61, 0x79, 0x5f, 0x64, 0x69, 0x6d, 0x73, 0x18, 0x11, 0x20, - 0x01, 0x28, 0x05, 0x52, 0x09, 0x61, 0x72, 0x72, 0x61, 0x79, 0x44, 0x69, 0x6d, 0x73, 0x22, 0x94, - 0x02, 0x0a, 0x05, 0x51, 0x75, 0x65, 0x72, 0x79, 0x12, 0x12, 0x0a, 0x04, 0x74, 0x65, 0x78, 0x74, - 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x74, 0x65, 0x78, 0x74, 0x12, 0x12, 0x0a, 0x04, - 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, - 0x12, 0x10, 0x0a, 0x03, 0x63, 0x6d, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x63, - 0x6d, 0x64, 0x12, 0x28, 0x0a, 0x07, 0x63, 0x6f, 0x6c, 0x75, 0x6d, 0x6e, 0x73, 0x18, 0x04, 0x20, - 0x03, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2e, 0x43, 0x6f, 0x6c, - 0x75, 0x6d, 0x6e, 0x52, 0x07, 0x63, 0x6f, 0x6c, 0x75, 0x6d, 0x6e, 0x73, 0x12, 0x2d, 0x0a, 0x06, - 0x70, 0x61, 0x72, 0x61, 0x6d, 0x73, 0x18, 0x05, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x11, 0x2e, 0x70, - 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2e, 0x50, 0x61, 0x72, 0x61, 0x6d, 0x65, 0x74, 0x65, 0x72, 0x52, - 0x0a, 0x70, 0x61, 0x72, 0x61, 0x6d, 0x65, 0x74, 0x65, 0x72, 0x73, 0x12, 0x1a, 0x0a, 0x08, 0x63, - 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x73, 0x18, 0x06, 0x20, 0x03, 0x28, 0x09, 0x52, 0x08, 0x63, - 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x73, 0x12, 0x1a, 0x0a, 0x08, 0x66, 0x69, 0x6c, 0x65, 0x6e, - 0x61, 0x6d, 0x65, 0x18, 0x07, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x66, 0x69, 0x6c, 0x65, 0x6e, - 0x61, 0x6d, 0x65, 0x12, 0x40, 0x0a, 0x11, 0x69, 0x6e, 0x73, 0x65, 0x72, 0x74, 0x5f, 0x69, 0x6e, - 0x74, 0x6f, 0x5f, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x18, 0x08, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x12, - 0x2e, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2e, 0x49, 0x64, 0x65, 0x6e, 0x74, 0x69, 0x66, 0x69, - 0x65, 0x72, 0x52, 0x11, 0x69, 0x6e, 0x73, 0x65, 0x72, 0x74, 0x5f, 0x69, 0x6e, 0x74, 0x6f, 0x5f, - 0x74, 0x61, 0x62, 0x6c, 0x65, 0x22, 0x4b, 0x0a, 0x09, 0x50, 0x61, 0x72, 0x61, 0x6d, 0x65, 0x74, - 0x65, 0x72, 0x12, 0x16, 0x0a, 0x06, 0x6e, 0x75, 0x6d, 0x62, 0x65, 0x72, 0x18, 0x01, 0x20, 0x01, - 0x28, 0x05, 0x52, 0x06, 0x6e, 0x75, 0x6d, 0x62, 0x65, 0x72, 0x12, 0x26, 0x0a, 0x06, 0x63, 0x6f, - 0x6c, 0x75, 0x6d, 0x6e, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x70, 0x6c, 0x75, - 0x67, 0x69, 0x6e, 0x2e, 0x43, 0x6f, 0x6c, 0x75, 0x6d, 0x6e, 0x52, 0x06, 0x63, 0x6f, 0x6c, 0x75, - 0x6d, 0x6e, 0x22, 0x87, 0x02, 0x0a, 0x0f, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, 0x65, 0x52, - 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x2c, 0x0a, 0x08, 0x73, 0x65, 0x74, 0x74, 0x69, 0x6e, - 0x67, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x10, 0x2e, 0x70, 0x6c, 0x75, 0x67, 0x69, - 0x6e, 0x2e, 0x53, 0x65, 0x74, 0x74, 0x69, 0x6e, 0x67, 0x73, 0x52, 0x08, 0x73, 0x65, 0x74, 0x74, - 0x69, 0x6e, 0x67, 0x73, 0x12, 0x29, 0x0a, 0x07, 0x63, 0x61, 0x74, 0x61, 0x6c, 0x6f, 0x67, 0x18, - 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0f, 0x2e, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2e, 0x43, - 0x61, 0x74, 0x61, 0x6c, 0x6f, 0x67, 0x52, 0x07, 0x63, 0x61, 0x74, 0x61, 0x6c, 0x6f, 0x67, 0x12, - 0x27, 0x0a, 0x07, 0x71, 0x75, 0x65, 0x72, 0x69, 0x65, 0x73, 0x18, 0x03, 0x20, 0x03, 0x28, 0x0b, - 0x32, 0x0d, 0x2e, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2e, 0x51, 0x75, 0x65, 0x72, 0x79, 0x52, - 0x07, 0x71, 0x75, 0x65, 0x72, 0x69, 0x65, 0x73, 0x12, 0x22, 0x0a, 0x0c, 0x73, 0x71, 0x6c, 0x63, - 0x5f, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0c, - 0x73, 0x71, 0x6c, 0x63, 0x5f, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x12, 0x26, 0x0a, 0x0e, - 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x5f, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0x05, - 0x20, 0x01, 0x28, 0x0c, 0x52, 0x0e, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x5f, 0x6f, 0x70, 0x74, - 0x69, 0x6f, 0x6e, 0x73, 0x12, 0x26, 0x0a, 0x0e, 0x67, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x5f, 0x6f, - 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0x06, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x0e, 0x67, 0x6c, - 0x6f, 0x62, 0x61, 0x6c, 0x5f, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x22, 0x36, 0x0a, 0x10, - 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, - 0x12, 0x22, 0x0a, 0x05, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, - 0x0c, 0x2e, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2e, 0x46, 0x69, 0x6c, 0x65, 0x52, 0x05, 0x66, - 0x69, 0x6c, 0x65, 0x73, 0x32, 0x4f, 0x0a, 0x0e, 0x43, 0x6f, 0x64, 0x65, 0x67, 0x65, 0x6e, 0x53, - 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x3d, 0x0a, 0x08, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, - 0x74, 0x65, 0x12, 0x17, 0x2e, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2e, 0x47, 0x65, 0x6e, 0x65, - 0x72, 0x61, 0x74, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x18, 0x2e, 0x70, 0x6c, - 0x75, 0x67, 0x69, 0x6e, 0x2e, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, 0x65, 0x52, 0x65, 0x73, - 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x7c, 0x0a, 0x0a, 0x63, 0x6f, 0x6d, 0x2e, 0x70, 0x6c, 0x75, - 0x67, 0x69, 0x6e, 0x42, 0x0c, 0x43, 0x6f, 0x64, 0x65, 0x67, 0x65, 0x6e, 0x50, 0x72, 0x6f, 0x74, - 0x6f, 0x50, 0x01, 0x5a, 0x28, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, - 0x73, 0x71, 0x6c, 0x63, 0x2d, 0x64, 0x65, 0x76, 0x2f, 0x73, 0x71, 0x6c, 0x63, 0x2f, 0x69, 0x6e, - 0x74, 0x65, 0x72, 0x6e, 0x61, 0x6c, 0x2f, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0xa2, 0x02, 0x03, - 0x50, 0x58, 0x58, 0xaa, 0x02, 0x06, 0x50, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0xca, 0x02, 0x06, 0x50, - 0x6c, 0x75, 0x67, 0x69, 0x6e, 0xe2, 0x02, 0x12, 0x50, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x5c, 0x47, - 0x50, 0x42, 0x4d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0xea, 0x02, 0x06, 0x50, 0x6c, 0x75, - 0x67, 0x69, 0x6e, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x65, 0x72, 0x52, 0x05, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x12, 0x1f, 0x0a, 0x0b, 0x74, 0x61, 0x62, + 0x6c, 0x65, 0x5f, 0x61, 0x6c, 0x69, 0x61, 0x73, 0x18, 0x0b, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0a, + 0x74, 0x61, 0x62, 0x6c, 0x65, 0x41, 0x6c, 0x69, 0x61, 0x73, 0x12, 0x26, 0x0a, 0x04, 0x74, 0x79, + 0x70, 0x65, 0x18, 0x0c, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x12, 0x2e, 0x70, 0x6c, 0x75, 0x67, 0x69, + 0x6e, 0x2e, 0x49, 0x64, 0x65, 0x6e, 0x74, 0x69, 0x66, 0x69, 0x65, 0x72, 0x52, 0x04, 0x74, 0x79, + 0x70, 0x65, 0x12, 0x22, 0x0a, 0x0d, 0x69, 0x73, 0x5f, 0x73, 0x71, 0x6c, 0x63, 0x5f, 0x73, 0x6c, + 0x69, 0x63, 0x65, 0x18, 0x0d, 0x20, 0x01, 0x28, 0x08, 0x52, 0x0b, 0x69, 0x73, 0x53, 0x71, 0x6c, + 0x63, 0x53, 0x6c, 0x69, 0x63, 0x65, 0x12, 0x33, 0x0a, 0x0b, 0x65, 0x6d, 0x62, 0x65, 0x64, 0x5f, + 0x74, 0x61, 0x62, 0x6c, 0x65, 0x18, 0x0e, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x12, 0x2e, 0x70, 0x6c, + 0x75, 0x67, 0x69, 0x6e, 0x2e, 0x49, 0x64, 0x65, 0x6e, 0x74, 0x69, 0x66, 0x69, 0x65, 0x72, 0x52, + 0x0a, 0x65, 0x6d, 0x62, 0x65, 0x64, 0x54, 0x61, 0x62, 0x6c, 0x65, 0x12, 0x23, 0x0a, 0x0d, 0x6f, + 0x72, 0x69, 0x67, 0x69, 0x6e, 0x61, 0x6c, 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x0f, 0x20, 0x01, + 0x28, 0x09, 0x52, 0x0c, 0x6f, 0x72, 0x69, 0x67, 0x69, 0x6e, 0x61, 0x6c, 0x4e, 0x61, 0x6d, 0x65, + 0x12, 0x1a, 0x0a, 0x08, 0x75, 0x6e, 0x73, 0x69, 0x67, 0x6e, 0x65, 0x64, 0x18, 0x10, 0x20, 0x01, + 0x28, 0x08, 0x52, 0x08, 0x75, 0x6e, 0x73, 0x69, 0x67, 0x6e, 0x65, 0x64, 0x12, 0x1d, 0x0a, 0x0a, + 0x61, 0x72, 0x72, 0x61, 0x79, 0x5f, 0x64, 0x69, 0x6d, 0x73, 0x18, 0x11, 0x20, 0x01, 0x28, 0x05, + 0x52, 0x09, 0x61, 0x72, 0x72, 0x61, 0x79, 0x44, 0x69, 0x6d, 0x73, 0x22, 0x94, 0x02, 0x0a, 0x05, + 0x51, 0x75, 0x65, 0x72, 0x79, 0x12, 0x12, 0x0a, 0x04, 0x74, 0x65, 0x78, 0x74, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x04, 0x74, 0x65, 0x78, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, + 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x10, 0x0a, + 0x03, 0x63, 0x6d, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x63, 0x6d, 0x64, 0x12, + 0x28, 0x0a, 0x07, 0x63, 0x6f, 0x6c, 0x75, 0x6d, 0x6e, 0x73, 0x18, 0x04, 0x20, 0x03, 0x28, 0x0b, + 0x32, 0x0e, 0x2e, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2e, 0x43, 0x6f, 0x6c, 0x75, 0x6d, 0x6e, + 0x52, 0x07, 0x63, 0x6f, 0x6c, 0x75, 0x6d, 0x6e, 0x73, 0x12, 0x2d, 0x0a, 0x06, 0x70, 0x61, 0x72, + 0x61, 0x6d, 0x73, 0x18, 0x05, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x11, 0x2e, 0x70, 0x6c, 0x75, 0x67, + 0x69, 0x6e, 0x2e, 0x50, 0x61, 0x72, 0x61, 0x6d, 0x65, 0x74, 0x65, 0x72, 0x52, 0x0a, 0x70, 0x61, + 0x72, 0x61, 0x6d, 0x65, 0x74, 0x65, 0x72, 0x73, 0x12, 0x1a, 0x0a, 0x08, 0x63, 0x6f, 0x6d, 0x6d, + 0x65, 0x6e, 0x74, 0x73, 0x18, 0x06, 0x20, 0x03, 0x28, 0x09, 0x52, 0x08, 0x63, 0x6f, 0x6d, 0x6d, + 0x65, 0x6e, 0x74, 0x73, 0x12, 0x1a, 0x0a, 0x08, 0x66, 0x69, 0x6c, 0x65, 0x6e, 0x61, 0x6d, 0x65, + 0x18, 0x07, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x66, 0x69, 0x6c, 0x65, 0x6e, 0x61, 0x6d, 0x65, + 0x12, 0x40, 0x0a, 0x11, 0x69, 0x6e, 0x73, 0x65, 0x72, 0x74, 0x5f, 0x69, 0x6e, 0x74, 0x6f, 0x5f, + 0x74, 0x61, 0x62, 0x6c, 0x65, 0x18, 0x08, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x12, 0x2e, 0x70, 0x6c, + 0x75, 0x67, 0x69, 0x6e, 0x2e, 0x49, 0x64, 0x65, 0x6e, 0x74, 0x69, 0x66, 0x69, 0x65, 0x72, 0x52, + 0x11, 0x69, 0x6e, 0x73, 0x65, 0x72, 0x74, 0x5f, 0x69, 0x6e, 0x74, 0x6f, 0x5f, 0x74, 0x61, 0x62, + 0x6c, 0x65, 0x22, 0x4b, 0x0a, 0x09, 0x50, 0x61, 0x72, 0x61, 0x6d, 0x65, 0x74, 0x65, 0x72, 0x12, + 0x16, 0x0a, 0x06, 0x6e, 0x75, 0x6d, 0x62, 0x65, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, + 0x06, 0x6e, 0x75, 0x6d, 0x62, 0x65, 0x72, 0x12, 0x26, 0x0a, 0x06, 0x63, 0x6f, 0x6c, 0x75, 0x6d, + 0x6e, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, + 0x2e, 0x43, 0x6f, 0x6c, 0x75, 0x6d, 0x6e, 0x52, 0x06, 0x63, 0x6f, 0x6c, 0x75, 0x6d, 0x6e, 0x22, + 0x87, 0x02, 0x0a, 0x0f, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, 0x65, 0x52, 0x65, 0x71, 0x75, + 0x65, 0x73, 0x74, 0x12, 0x2c, 0x0a, 0x08, 0x73, 0x65, 0x74, 0x74, 0x69, 0x6e, 0x67, 0x73, 0x18, + 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x10, 0x2e, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2e, 0x53, + 0x65, 0x74, 0x74, 0x69, 0x6e, 0x67, 0x73, 0x52, 0x08, 0x73, 0x65, 0x74, 0x74, 0x69, 0x6e, 0x67, + 0x73, 0x12, 0x29, 0x0a, 0x07, 0x63, 0x61, 0x74, 0x61, 0x6c, 0x6f, 0x67, 0x18, 0x02, 0x20, 0x01, + 0x28, 0x0b, 0x32, 0x0f, 0x2e, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2e, 0x43, 0x61, 0x74, 0x61, + 0x6c, 0x6f, 0x67, 0x52, 0x07, 0x63, 0x61, 0x74, 0x61, 0x6c, 0x6f, 0x67, 0x12, 0x27, 0x0a, 0x07, + 0x71, 0x75, 0x65, 0x72, 0x69, 0x65, 0x73, 0x18, 0x03, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0d, 0x2e, + 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2e, 0x51, 0x75, 0x65, 0x72, 0x79, 0x52, 0x07, 0x71, 0x75, + 0x65, 0x72, 0x69, 0x65, 0x73, 0x12, 0x22, 0x0a, 0x0c, 0x73, 0x71, 0x6c, 0x63, 0x5f, 0x76, 0x65, + 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0c, 0x73, 0x71, 0x6c, + 0x63, 0x5f, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x12, 0x26, 0x0a, 0x0e, 0x70, 0x6c, 0x75, + 0x67, 0x69, 0x6e, 0x5f, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0x05, 0x20, 0x01, 0x28, + 0x0c, 0x52, 0x0e, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x5f, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, + 0x73, 0x12, 0x26, 0x0a, 0x0e, 0x67, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x5f, 0x6f, 0x70, 0x74, 0x69, + 0x6f, 0x6e, 0x73, 0x18, 0x06, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x0e, 0x67, 0x6c, 0x6f, 0x62, 0x61, + 0x6c, 0x5f, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x22, 0x36, 0x0a, 0x10, 0x47, 0x65, 0x6e, + 0x65, 0x72, 0x61, 0x74, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x22, 0x0a, + 0x05, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x70, + 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2e, 0x46, 0x69, 0x6c, 0x65, 0x52, 0x05, 0x66, 0x69, 0x6c, 0x65, + 0x73, 0x32, 0x4f, 0x0a, 0x0e, 0x43, 0x6f, 0x64, 0x65, 0x67, 0x65, 0x6e, 0x53, 0x65, 0x72, 0x76, + 0x69, 0x63, 0x65, 0x12, 0x3d, 0x0a, 0x08, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, 0x65, 0x12, + 0x17, 0x2e, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2e, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, + 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x18, 0x2e, 0x70, 0x6c, 0x75, 0x67, 0x69, + 0x6e, 0x2e, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, + 0x73, 0x65, 0x42, 0x7c, 0x0a, 0x0a, 0x63, 0x6f, 0x6d, 0x2e, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, + 0x42, 0x0c, 0x43, 0x6f, 0x64, 0x65, 0x67, 0x65, 0x6e, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x50, 0x01, + 0x5a, 0x28, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x73, 0x71, 0x6c, + 0x63, 0x2d, 0x64, 0x65, 0x76, 0x2f, 0x73, 0x71, 0x6c, 0x63, 0x2f, 0x69, 0x6e, 0x74, 0x65, 0x72, + 0x6e, 0x61, 0x6c, 0x2f, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0xa2, 0x02, 0x03, 0x50, 0x58, 0x58, + 0xaa, 0x02, 0x06, 0x50, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0xca, 0x02, 0x06, 0x50, 0x6c, 0x75, 0x67, + 0x69, 0x6e, 0xe2, 0x02, 0x12, 0x50, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x5c, 0x47, 0x50, 0x42, 0x4d, + 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0xea, 0x02, 0x06, 0x50, 0x6c, 0x75, 0x67, 0x69, 0x6e, + 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( diff --git a/internal/source/code.go b/internal/source/code.go index 8b88a24136..0712268079 100644 --- a/internal/source/code.go +++ b/internal/source/code.go @@ -163,3 +163,38 @@ func CleanedComments(rawSQL string, cs CommentSyntax) ([]string, error) { } return comments, s.Err() } + +func RawComments(rawSQL string, cs CommentSyntax) ([]string, error) { + s := bufio.NewScanner(strings.NewReader(strings.TrimSpace(rawSQL))) + var comments []string + for s.Scan() { + line := s.Text() + var prefix string + if strings.HasPrefix(line, "--") { + if !cs.Dash { + continue + } + prefix = "--" + } + if strings.HasPrefix(line, "/*") { + if !cs.SlashStar { + continue + } + prefix = "/*" + } + if strings.HasPrefix(line, "#") { + if !cs.Hash { + continue + } + prefix = "#" + } + if prefix == "" { + continue + } + + // rest := line[len(prefix):] + // rest = strings.TrimSuffix(rest, "*/") + comments = append(comments, line) + } + return comments, s.Err() +} diff --git a/internal/sql/catalog/catalog.go b/internal/sql/catalog/catalog.go index 278ea8797d..1921e8cb7d 100644 --- a/internal/sql/catalog/catalog.go +++ b/internal/sql/catalog/catalog.go @@ -35,14 +35,14 @@ func New(defaultSchema string) *Catalog { func (c *Catalog) Build(stmts []ast.Statement) error { for i := range stmts { - if err := c.Update(stmts[i], nil); err != nil { + if err := c.Update(stmts[i], nil, nil); err != nil { return err } } return nil } -func (c *Catalog) Update(stmt ast.Statement, colGen columnGenerator) error { +func (c *Catalog) Update(stmt ast.Statement, rComments []string, colGen columnGenerator) error { if stmt.Raw == nil { return nil } @@ -80,10 +80,10 @@ func (c *Catalog) Update(stmt ast.Statement, colGen columnGenerator) error { err = c.commentOnView(n) case *ast.CompositeTypeStmt: - err = c.createCompositeType(n) + err = c.createCompositeType(n, rComments) case *ast.CreateEnumStmt: - err = c.createEnum(n) + err = c.createEnum(n, rComments) case *ast.CreateExtensionStmt: err = c.createExtension(n) @@ -92,13 +92,12 @@ func (c *Catalog) Update(stmt ast.Statement, colGen columnGenerator) error { err = c.createFunction(n) case *ast.CreateSchemaStmt: - err = c.createSchema(n) - + err = c.createSchema(n, rComments) case *ast.CreateTableStmt: - err = c.createTable(n) + err = c.createTable(n, rComments) case *ast.CreateTableAsStmt: - err = c.createTableAs(n, colGen) + err = c.createTableAs(n, rComments, colGen) case *ast.ViewStmt: err = c.createView(n, colGen) @@ -132,7 +131,7 @@ func (c *Catalog) Update(stmt ast.Statement, colGen columnGenerator) error { StmtLocation: stmt.Raw.StmtLocation, StmtLen: stmt.Raw.StmtLen, }, - }, colGen); err != nil { + }, nil, colGen); err != nil { return err } } diff --git a/internal/sql/catalog/schema.go b/internal/sql/catalog/schema.go index 72a32a6ff8..8efd10dc24 100644 --- a/internal/sql/catalog/schema.go +++ b/internal/sql/catalog/schema.go @@ -4,18 +4,20 @@ import ( "fmt" "strings" + "github.com/sqlc-dev/sqlc/internal/metadata" "github.com/sqlc-dev/sqlc/internal/sql/ast" "github.com/sqlc-dev/sqlc/internal/sql/sqlerr" ) // Schema describes how the data in a relational database may relate to other tables or other data models type Schema struct { - Name string - Tables []*Table - Types []Type - Funcs []*Function - - Comment string + Name string + Tables []*Table + Types []Type + Funcs []*Function + Metadata metadata.Metadata + Comment string + RawComments []string } func (s *Schema) getFunc(rel *ast.FuncName, tns []*ast.TypeName) (*Function, int, error) { @@ -95,7 +97,7 @@ func (c *Catalog) getSchema(name string) (*Schema, error) { return nil, sqlerr.SchemaNotFound(name) } -func (c *Catalog) createSchema(stmt *ast.CreateSchemaStmt) error { +func (c *Catalog) createSchema(stmt *ast.CreateSchemaStmt, rComments []string) error { if stmt.Name == nil { return fmt.Errorf("create schema: empty name") } @@ -109,7 +111,7 @@ func (c *Catalog) createSchema(stmt *ast.CreateSchemaStmt) error { return sqlerr.SchemaExists(*stmt.Name) } } - c.Schemas = append(c.Schemas, &Schema{Name: *stmt.Name}) + c.Schemas = append(c.Schemas, &Schema{Name: *stmt.Name, RawComments: rComments}) return nil } diff --git a/internal/sql/catalog/table.go b/internal/sql/catalog/table.go index dc30acfa1e..41db0df92c 100644 --- a/internal/sql/catalog/table.go +++ b/internal/sql/catalog/table.go @@ -13,9 +13,10 @@ import ( // A database table is a collection of related data held in a table format within a database. // It consists of columns and rows. type Table struct { - Rel *ast.TableName - Columns []*Column - Comment string + Rel *ast.TableName + Columns []*Column + Comment string + RawComments []string } func checkMissing(err error, missingOK bool) error { @@ -129,6 +130,8 @@ type Column struct { Comment string Length *int + RawComment string + linkedType bool } @@ -247,7 +250,7 @@ func (c *Catalog) alterTableSetSchema(stmt *ast.AlterTableSetSchemaStmt) error { return nil } -func (c *Catalog) createTable(stmt *ast.CreateTableStmt) error { +func (c *Catalog) createTable(stmt *ast.CreateTableStmt, rComments []string) error { ns := stmt.Name.Schema if ns == "" { ns = c.DefaultSchema @@ -263,7 +266,7 @@ func (c *Catalog) createTable(stmt *ast.CreateTableStmt) error { return sqlerr.RelationExists(stmt.Name.Name) } - tbl := Table{Rel: stmt.Name, Comment: stmt.Comment} + tbl := Table{Rel: stmt.Name, Comment: stmt.Comment, RawComments: rComments} coltype := make(map[string]ast.TypeName) // used to check for duplicate column names seen := make(map[string]bool) // used to check for duplicate column names for _, inheritTable := range stmt.Inherits { @@ -344,7 +347,7 @@ func (c *Catalog) defineColumn(table *ast.TableName, col *ast.ColumnDef) (*Colum Name: fmt.Sprintf("%s_%s", table.Name, col.Colname), } s := &ast.CreateEnumStmt{TypeName: &typeName, Vals: col.Vals} - if err := c.createEnum(s); err != nil { + if err := c.createEnum(s, nil); err != nil { return nil, err } tc.Type = typeName @@ -450,7 +453,11 @@ func (c *Catalog) renameTable(stmt *ast.RenameTableStmt) error { return nil } -func (c *Catalog) createTableAs(stmt *ast.CreateTableAsStmt, colGen columnGenerator) error { +func (c *Catalog) createTableAs( + stmt *ast.CreateTableAsStmt, + rComments []string, + colGen columnGenerator, +) error { cols, err := colGen.OutputColumns(stmt.Query) if err != nil { return err @@ -471,7 +478,8 @@ func (c *Catalog) createTableAs(stmt *ast.CreateTableAsStmt, colGen columnGenera Schema: schemaName, Name: *stmt.Into.Rel.Relname, }, - Columns: cols, + Columns: cols, + RawComments: rComments, } ns := tbl.Rel.Schema diff --git a/internal/sql/catalog/types.go b/internal/sql/catalog/types.go index 464472bcf2..7dfa2a5a92 100644 --- a/internal/sql/catalog/types.go +++ b/internal/sql/catalog/types.go @@ -15,9 +15,10 @@ type Type interface { } type Enum struct { - Name string - Vals []string - Comment string + Name string + Vals []string + Comment string + RawComments []string } func (e *Enum) SetComment(c string) { @@ -28,8 +29,9 @@ func (e *Enum) isType() { } type CompositeType struct { - Name string - Comment string + Name string + Comment string + RawComments []string } func (ct *CompositeType) isType() { @@ -62,7 +64,7 @@ func sameType(a, b *ast.TypeName) bool { return true } -func (c *Catalog) createEnum(stmt *ast.CreateEnumStmt) error { +func (c *Catalog) createEnum(stmt *ast.CreateEnumStmt, rComments []string) error { ns := stmt.TypeName.Schema if ns == "" { ns = c.DefaultSchema @@ -85,8 +87,9 @@ func (c *Catalog) createEnum(stmt *ast.CreateEnumStmt) error { return sqlerr.TypeExists(tbl.Name) } schema.Types = append(schema.Types, &Enum{ - Name: stmt.TypeName.Name, - Vals: stringSlice(stmt.Vals), + Name: stmt.TypeName.Name, + Vals: stringSlice(stmt.Vals), + RawComments: rComments, }) return nil } @@ -113,7 +116,7 @@ func (c *Catalog) getType(rel *ast.TypeName) (Type, int, error) { return s.getType(rel) } -func (c *Catalog) createCompositeType(stmt *ast.CompositeTypeStmt) error { +func (c *Catalog) createCompositeType(stmt *ast.CompositeTypeStmt, rComments []string) error { ns := stmt.TypeName.Schema if ns == "" { ns = c.DefaultSchema @@ -136,7 +139,8 @@ func (c *Catalog) createCompositeType(stmt *ast.CompositeTypeStmt) error { return sqlerr.TypeExists(tbl.Name) } schema.Types = append(schema.Types, &CompositeType{ - Name: stmt.TypeName.Name, + Name: stmt.TypeName.Name, + RawComments: rComments, }) return nil } @@ -228,7 +232,12 @@ func (c *Catalog) alterTypeAddValue(stmt *ast.AlterTypeAddValueStmt) error { } if !foundNeighbor { - return fmt.Errorf("enum %s unable to find existing neighbor value %s for new value %s", enum.Name, *stmt.NewValNeighbor, *stmt.NewValue) + return fmt.Errorf( + "enum %s unable to find existing neighbor value %s for new value %s", + enum.Name, + *stmt.NewValNeighbor, + *stmt.NewValue, + ) } } diff --git a/protos/plugin/codegen.proto b/protos/plugin/codegen.proto index e6faf19bad..3d68814e20 100644 --- a/protos/plugin/codegen.proto +++ b/protos/plugin/codegen.proto @@ -56,23 +56,27 @@ message Schema { repeated Table tables = 3; repeated Enum enums = 4; repeated CompositeType composite_types = 5; + repeated string raw_comments = 6; } message CompositeType { string name = 1; string comment = 2; + repeated string raw_comments = 3; } message Enum { string name = 1; repeated string vals = 2; string comment = 3; + repeated string raw_comments = 4; } message Table { Identifier rel = 1; repeated Column columns = 2; string comment = 3; + repeated string raw_comments = 4; } message Identifier { From 3cf2580201e91d72523edb0e2374faced5afef92 Mon Sep 17 00:00:00 2001 From: Bruce Smith Date: Sun, 8 Sep 2024 18:00:32 -0400 Subject: [PATCH 2/7] removed Raw from metadata --- internal/metadata/meta.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/metadata/meta.go b/internal/metadata/meta.go index 40532786ff..b646e33030 100644 --- a/internal/metadata/meta.go +++ b/internal/metadata/meta.go @@ -17,7 +17,6 @@ type Metadata struct { Comments []string Params map[string]string Flags map[string]bool - Raw []string Filename string } From 7fa83c5ba66dac94ac8346adaa7f9f8341fb1d39 Mon Sep 17 00:00:00 2001 From: Bruce Smith Date: Sun, 8 Sep 2024 18:02:07 -0400 Subject: [PATCH 3/7] cleaned up RawComments Parser --- internal/source/code.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/internal/source/code.go b/internal/source/code.go index 0712268079..978c226ecc 100644 --- a/internal/source/code.go +++ b/internal/source/code.go @@ -192,8 +192,6 @@ func RawComments(rawSQL string, cs CommentSyntax) ([]string, error) { continue } - // rest := line[len(prefix):] - // rest = strings.TrimSuffix(rest, "*/") comments = append(comments, line) } return comments, s.Err() From 7d6034a1ddc693a092a98f9555150946453eb585 Mon Sep 17 00:00:00 2001 From: Bruce Smith Date: Sun, 8 Sep 2024 18:03:15 -0400 Subject: [PATCH 4/7] cleaned up code comments from compiler --- internal/compiler/compile.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/internal/compiler/compile.go b/internal/compiler/compile.go index 36e6bf918f..2d9bb300b2 100644 --- a/internal/compiler/compile.go +++ b/internal/compiler/compile.go @@ -46,9 +46,6 @@ func (c *Compiler) parseCatalog(schemas []string) error { continue } for i, stmt := range stmts { - // Lets just parse the metadata annotations and pass them in. - // Update will use them on Types we deem neccesary. - // Can either be metadata or just a raw []strings _, rawSQL, err := getRaw(stmt.Raw, src) if err != nil { return err From 214bbab7eec49c34b354895d593ba70945370598 Mon Sep 17 00:00:00 2001 From: Bruce Smith Date: Wed, 18 Sep 2024 19:20:25 -0400 Subject: [PATCH 5/7] RawComments --- go.mod | 19 +++--- go.sum | 38 ++++++------ internal/cmd/shim.go | 14 +++-- internal/codegen/golang/gen.go | 25 ++++++-- internal/compiler/compile.go | 2 + internal/compiler/parse.go | 7 +++ internal/metadata/meta.go | 13 +++-- internal/plugin/codegen.pb.go | 102 ++++++++++++++++++++------------- internal/sql/catalog/table.go | 2 + protos/plugin/codegen.proto | 2 + 10 files changed, 142 insertions(+), 82 deletions(-) diff --git a/go.mod b/go.mod index 6e19713b73..abe87c1e59 100644 --- a/go.mod +++ b/go.mod @@ -6,13 +6,15 @@ require ( github.com/antlr4-go/antlr/v4 v4.13.1 github.com/cubicdaiya/gonp v1.0.4 github.com/davecgh/go-spew v1.1.1 + github.com/ettle/strcase v0.2.0 github.com/fatih/structtag v1.2.0 github.com/go-sql-driver/mysql v1.8.1 github.com/google/cel-go v0.21.0 github.com/google/go-cmp v0.6.0 github.com/jackc/pgx/v4 v4.18.3 github.com/jackc/pgx/v5 v5.6.0 - github.com/jhump/protoreflect v1.17.0 + github.com/jhump/protoreflect v1.17.1-0.20240913204751-8f5fd1dcb3c5 + github.com/jhump/protoreflect/v2 v2.0.0-beta.2 github.com/jinzhu/inflection v1.0.0 github.com/lib/pq v1.10.9 github.com/pganalyze/pg_query_go/v5 v5.1.0 @@ -25,7 +27,9 @@ require ( github.com/wasilibs/go-pgquery v0.0.0-20240606042535-c0843d6592cc github.com/xeipuuv/gojsonschema v1.2.0 golang.org/x/sync v0.8.0 - google.golang.org/grpc v1.66.0 + google.golang.org/genproto v0.0.0-20240903143218-8af14fe29dc1 + google.golang.org/genproto/googleapis/api v0.0.0-20240827150818-7e3bb234dfed + google.golang.org/grpc v1.66.2 google.golang.org/protobuf v1.34.2 gopkg.in/yaml.v3 v3.0.1 modernc.org/sqlite v1.32.0 @@ -64,13 +68,12 @@ require ( go.uber.org/atomic v1.11.0 // indirect go.uber.org/multierr v1.11.0 // indirect go.uber.org/zap v1.26.0 // indirect - golang.org/x/crypto v0.24.0 // indirect + golang.org/x/crypto v0.26.0 // indirect golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 // indirect - golang.org/x/net v0.26.0 // indirect - golang.org/x/sys v0.22.0 // indirect - golang.org/x/text v0.16.0 // indirect - google.golang.org/genproto/googleapis/api v0.0.0-20240604185151-ef581f913117 // indirect - google.golang.org/genproto/googleapis/rpc v0.0.0-20240604185151-ef581f913117 // indirect + golang.org/x/net v0.28.0 // indirect + golang.org/x/sys v0.24.0 // indirect + golang.org/x/text v0.17.0 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20240827150818-7e3bb234dfed // indirect gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6 // indirect modernc.org/libc v1.55.3 // indirect diff --git a/go.sum b/go.sum index 36b508ce5f..9e0ee21ba2 100644 --- a/go.sum +++ b/go.sum @@ -23,6 +23,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= +github.com/ettle/strcase v0.2.0 h1:fGNiVF21fHXpX1niBgk0aROov1LagYsOwV/xqKDKR/Q= +github.com/ettle/strcase v0.2.0/go.mod h1:DajmHElDSaX76ITe3/VHVyMin4LWSJN5Z909Wp+ED1A= github.com/fatih/structtag v1.2.0 h1:/OdNE99OxoI/PqaW/SuSK9uxxT3f/tcSZgon/ssNSx4= github.com/fatih/structtag v1.2.0/go.mod h1:mBJUNpUnHmRKrKlQQlmCrh5PuhftFbNv8Ys4/aAZl94= github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY= @@ -100,8 +102,10 @@ github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0f github.com/jackc/puddle v1.1.3/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk= github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= -github.com/jhump/protoreflect v1.17.0 h1:qOEr613fac2lOuTgWN4tPAtLL7fUSbuJL5X5XumQh94= -github.com/jhump/protoreflect v1.17.0/go.mod h1:h9+vUUL38jiBzck8ck+6G/aeMX8Z4QUY/NiJPwPNi+8= +github.com/jhump/protoreflect v1.17.1-0.20240913204751-8f5fd1dcb3c5 h1:OUsOWe/nhWohrzIjKP7Wk3Bt1lhDHn0w39uiT/zTWPM= +github.com/jhump/protoreflect v1.17.1-0.20240913204751-8f5fd1dcb3c5/go.mod h1:uUKhM0KLkqvoYeM5BSlLxkJ3Dja3r0N08ru0cacT99E= +github.com/jhump/protoreflect/v2 v2.0.0-beta.2 h1:qZU+rEZUOYTz1Bnhi3xbwn+VxdXkLVeEpAeZzVXLY88= +github.com/jhump/protoreflect/v2 v2.0.0-beta.2/go.mod h1:4tnOYkB/mq7QTyS3YKtVtNrJv4Psqout8HA1U+hZtgM= github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= @@ -232,8 +236,8 @@ golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPh golang.org/x/crypto v0.0.0-20201203163018-be400aefbc4c/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I= golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI= -golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM= +golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw= +golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54= golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 h1:vr/HnozRka3pE4EsMEg1lgkXJkTFJCVUX+S/ZT6wYzM= golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842/go.mod h1:XtvwrStGgqGPLc4cjQfWqZHG1YFdYs6swckp8vpsjnc= golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= @@ -246,8 +250,8 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ= -golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE= +golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE= +golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= @@ -264,8 +268,8 @@ golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= -golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg= +golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -273,8 +277,8 @@ golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= -golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= +golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc= +golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190425163242-31fd60d6bfdc/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= @@ -292,12 +296,14 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -google.golang.org/genproto/googleapis/api v0.0.0-20240604185151-ef581f913117 h1:+rdxYoE3E5htTEWIe15GlN6IfvbURM//Jt0mmkmm6ZU= -google.golang.org/genproto/googleapis/api v0.0.0-20240604185151-ef581f913117/go.mod h1:OimBR/bc1wPO9iV4NC2bpyjy3VnAwZh5EBPQdtaE5oo= -google.golang.org/genproto/googleapis/rpc v0.0.0-20240604185151-ef581f913117 h1:1GBuWVLM/KMVUv1t1En5Gs+gFZCNd360GGb4sSxtrhU= -google.golang.org/genproto/googleapis/rpc v0.0.0-20240604185151-ef581f913117/go.mod h1:EfXuqaE1J41VCDicxHzUDm+8rk+7ZdXzHV0IhO/I6s0= -google.golang.org/grpc v1.66.0 h1:DibZuoBznOxbDQxRINckZcUvnCEvrW9pcWIE2yF9r1c= -google.golang.org/grpc v1.66.0/go.mod h1:s3/l6xSSCURdVfAnL+TqCNMyTDAGN6+lZeVxnZR128Y= +google.golang.org/genproto v0.0.0-20240903143218-8af14fe29dc1 h1:BulPr26Jqjnd4eYDVe+YvyR7Yc2vJGkO5/0UxD0/jZU= +google.golang.org/genproto v0.0.0-20240903143218-8af14fe29dc1/go.mod h1:hL97c3SYopEHblzpxRL4lSs523++l8DYxGM1FQiYmb4= +google.golang.org/genproto/googleapis/api v0.0.0-20240827150818-7e3bb234dfed h1:3RgNmBoI9MZhsj3QxC+AP/qQhNwpCLOvYDYYsFrhFt0= +google.golang.org/genproto/googleapis/api v0.0.0-20240827150818-7e3bb234dfed/go.mod h1:OCdP9MfskevB/rbYvHTsXTtKC+3bHWajPdoKgjcYkfo= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240827150818-7e3bb234dfed h1:J6izYgfBXAI3xTKLgxzTmUltdYaLsuBxFCgDHWJ/eXg= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240827150818-7e3bb234dfed/go.mod h1:UqMtugtsSgubUsoxbuAoiCXvqvErP7Gf0so0mK9tHxU= +google.golang.org/grpc v1.66.2 h1:3QdXkuq3Bkh7w+ywLdLvM56cmGvQHUMZpiCzt6Rqaoo= +google.golang.org/grpc v1.66.2/go.mod h1:s3/l6xSSCURdVfAnL+TqCNMyTDAGN6+lZeVxnZR128Y= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= diff --git a/internal/cmd/shim.go b/internal/cmd/shim.go index 57f36c4765..e01587da86 100644 --- a/internal/cmd/shim.go +++ b/internal/cmd/shim.go @@ -96,12 +96,13 @@ func pluginCatalog(c *catalog.Catalog) *plugin.Catalog { Schema: c.Type.Schema, Name: c.Type.Name, }, - Comment: c.Comment, - NotNull: c.IsNotNull, - Unsigned: c.IsUnsigned, - IsArray: c.IsArray, - ArrayDims: int32(c.ArrayDims), - Length: int32(l), + Comment: c.Comment, + NotNull: c.IsNotNull, + Unsigned: c.IsUnsigned, + IsArray: c.IsArray, + ArrayDims: int32(c.ArrayDims), + PrimaryKey: c.PrimaryKey, + Length: int32(l), Table: &plugin.Identifier{ Catalog: t.Rel.Catalog, Schema: t.Rel.Schema, @@ -165,6 +166,7 @@ func pluginQueries(r *compiler.Result) []*plugin.Query { Params: params, Filename: q.Metadata.Filename, InsertIntoTable: iit, + RawComments: q.Metadata.RawComments, }) } return out diff --git a/internal/codegen/golang/gen.go b/internal/codegen/golang/gen.go index 5b7977f500..cbd8a59472 100644 --- a/internal/codegen/golang/gen.go +++ b/internal/codegen/golang/gen.go @@ -150,16 +150,28 @@ func validate(options *opts.Options, enums []Enum, structs []Struct, queries []Q } for _, query := range queries { if _, ok := enumNames[query.ConstantName]; ok { - return fmt.Errorf("query constant name conflicts with enum name: %s", query.ConstantName) + return fmt.Errorf( + "query constant name conflicts with enum name: %s", + query.ConstantName, + ) } if _, ok := structNames[query.ConstantName]; ok { - return fmt.Errorf("query constant name conflicts with struct name: %s", query.ConstantName) + return fmt.Errorf( + "query constant name conflicts with struct name: %s", + query.ConstantName, + ) } } return nil } -func generate(req *plugin.GenerateRequest, options *opts.Options, enums []Enum, structs []Struct, queries []Query) (*plugin.GenerateResponse, error) { +func generate( + req *plugin.GenerateRequest, + options *opts.Options, + enums []Enum, + structs []Struct, + queries []Query, +) (*plugin.GenerateResponse, error) { i := &importer{ Options: options, Queries: queries, @@ -189,8 +201,11 @@ func generate(req *plugin.GenerateRequest, options *opts.Options, enums []Enum, OmitSqlcVersion: options.OmitSqlcVersion, } - if tctx.UsesCopyFrom && !tctx.SQLDriver.IsPGX() && options.SqlDriver != opts.SQLDriverGoSQLDriverMySQL { - return nil, errors.New(":copyfrom is only supported by pgx and github.com/go-sql-driver/mysql") + if tctx.UsesCopyFrom && !tctx.SQLDriver.IsPGX() && + options.SqlDriver != opts.SQLDriverGoSQLDriverMySQL { + return nil, errors.New( + ":copyfrom is only supported by pgx and github.com/go-sql-driver/mysql", + ) } if tctx.UsesCopyFrom && options.SqlDriver == opts.SQLDriverGoSQLDriverMySQL { diff --git a/internal/compiler/compile.go b/internal/compiler/compile.go index 2d9bb300b2..d5bfcb51d6 100644 --- a/internal/compiler/compile.go +++ b/internal/compiler/compile.go @@ -8,6 +8,8 @@ import ( "path/filepath" "strings" + // "github.com/ryboe/q" + "github.com/sqlc-dev/sqlc/internal/migrations" "github.com/sqlc-dev/sqlc/internal/multierr" "github.com/sqlc-dev/sqlc/internal/opts" diff --git a/internal/compiler/parse.go b/internal/compiler/parse.go index d5089a2f6e..cbe4c1f703 100644 --- a/internal/compiler/parse.go +++ b/internal/compiler/parse.go @@ -53,6 +53,13 @@ func (c *Compiler) parseQuery(stmt ast.Node, src string, o opts.Parser) (*Query, Cmd: cmd, } + RawComments, err := source.RawComments(rawSQL, c.parser.CommentSyntax()) + if err != nil { + return nil, err + } + + md.RawComments = RawComments + // TODO eventually can use this for name and type/cmd parsing too cleanedComments, err := source.CleanedComments(rawSQL, c.parser.CommentSyntax()) if err != nil { diff --git a/internal/metadata/meta.go b/internal/metadata/meta.go index b646e33030..34f7a4dcf2 100644 --- a/internal/metadata/meta.go +++ b/internal/metadata/meta.go @@ -12,12 +12,13 @@ import ( type CommentSyntax source.CommentSyntax type Metadata struct { - Name string - Cmd string - Comments []string - Params map[string]string - Flags map[string]bool - Filename string + Name string + Cmd string + Comments []string + Params map[string]string + Flags map[string]bool + Filename string + RawComments []string } const ( diff --git a/internal/plugin/codegen.pb.go b/internal/plugin/codegen.pb.go index 7a62b452ed..5b19638e03 100644 --- a/internal/plugin/codegen.pb.go +++ b/internal/plugin/codegen.pb.go @@ -689,6 +689,7 @@ type Column struct { OriginalName string `protobuf:"bytes,15,opt,name=original_name,json=originalName,proto3" json:"original_name,omitempty"` Unsigned bool `protobuf:"varint,16,opt,name=unsigned,proto3" json:"unsigned,omitempty"` ArrayDims int32 `protobuf:"varint,17,opt,name=array_dims,json=arrayDims,proto3" json:"array_dims,omitempty"` + PrimaryKey bool `protobuf:"varint,18,opt,name=PrimaryKey,proto3" json:"PrimaryKey,omitempty"` } func (x *Column) Reset() { @@ -835,6 +836,13 @@ func (x *Column) GetArrayDims() int32 { return 0 } +func (x *Column) GetPrimaryKey() bool { + if x != nil { + return x.PrimaryKey + } + return false +} + type Query struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -848,6 +856,7 @@ type Query struct { Comments []string `protobuf:"bytes,6,rep,name=comments,proto3" json:"comments,omitempty"` Filename string `protobuf:"bytes,7,opt,name=filename,proto3" json:"filename,omitempty"` InsertIntoTable *Identifier `protobuf:"bytes,8,opt,name=insert_into_table,proto3" json:"insert_into_table,omitempty"` + RawComments []string `protobuf:"bytes,9,rep,name=raw_comments,proto3" json:"raw_comments,omitempty"` } func (x *Query) Reset() { @@ -938,6 +947,13 @@ func (x *Query) GetInsertIntoTable() *Identifier { return nil } +func (x *Query) GetRawComments() []string { + if x != nil { + return x.RawComments + } + return nil +} + type Parameter struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -1316,7 +1332,7 @@ var file_plugin_codegen_proto_rawDesc = []byte{ 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x63, 0x61, 0x74, 0x61, 0x6c, 0x6f, 0x67, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x63, 0x68, 0x65, 0x6d, 0x61, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x73, 0x63, 0x68, 0x65, 0x6d, 0x61, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x03, - 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x22, 0x8e, 0x04, 0x0a, 0x06, 0x43, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x22, 0xae, 0x04, 0x0a, 0x06, 0x43, 0x6f, 0x6c, 0x75, 0x6d, 0x6e, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x19, 0x0a, 0x08, 0x6e, 0x6f, 0x74, 0x5f, 0x6e, 0x75, 0x6c, 0x6c, 0x18, 0x03, 0x20, 0x01, 0x28, 0x08, 0x52, 0x07, 0x6e, 0x6f, 0x74, @@ -1349,7 +1365,9 @@ var file_plugin_codegen_proto_rawDesc = []byte{ 0x12, 0x1a, 0x0a, 0x08, 0x75, 0x6e, 0x73, 0x69, 0x67, 0x6e, 0x65, 0x64, 0x18, 0x10, 0x20, 0x01, 0x28, 0x08, 0x52, 0x08, 0x75, 0x6e, 0x73, 0x69, 0x67, 0x6e, 0x65, 0x64, 0x12, 0x1d, 0x0a, 0x0a, 0x61, 0x72, 0x72, 0x61, 0x79, 0x5f, 0x64, 0x69, 0x6d, 0x73, 0x18, 0x11, 0x20, 0x01, 0x28, 0x05, - 0x52, 0x09, 0x61, 0x72, 0x72, 0x61, 0x79, 0x44, 0x69, 0x6d, 0x73, 0x22, 0x94, 0x02, 0x0a, 0x05, + 0x52, 0x09, 0x61, 0x72, 0x72, 0x61, 0x79, 0x44, 0x69, 0x6d, 0x73, 0x12, 0x1e, 0x0a, 0x0a, 0x50, + 0x72, 0x69, 0x6d, 0x61, 0x72, 0x79, 0x4b, 0x65, 0x79, 0x18, 0x12, 0x20, 0x01, 0x28, 0x08, 0x52, + 0x0a, 0x50, 0x72, 0x69, 0x6d, 0x61, 0x72, 0x79, 0x4b, 0x65, 0x79, 0x22, 0xb8, 0x02, 0x0a, 0x05, 0x51, 0x75, 0x65, 0x72, 0x79, 0x12, 0x12, 0x0a, 0x04, 0x74, 0x65, 0x78, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x74, 0x65, 0x78, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x10, 0x0a, @@ -1367,45 +1385,47 @@ var file_plugin_codegen_proto_rawDesc = []byte{ 0x74, 0x61, 0x62, 0x6c, 0x65, 0x18, 0x08, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x12, 0x2e, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2e, 0x49, 0x64, 0x65, 0x6e, 0x74, 0x69, 0x66, 0x69, 0x65, 0x72, 0x52, 0x11, 0x69, 0x6e, 0x73, 0x65, 0x72, 0x74, 0x5f, 0x69, 0x6e, 0x74, 0x6f, 0x5f, 0x74, 0x61, 0x62, - 0x6c, 0x65, 0x22, 0x4b, 0x0a, 0x09, 0x50, 0x61, 0x72, 0x61, 0x6d, 0x65, 0x74, 0x65, 0x72, 0x12, - 0x16, 0x0a, 0x06, 0x6e, 0x75, 0x6d, 0x62, 0x65, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, - 0x06, 0x6e, 0x75, 0x6d, 0x62, 0x65, 0x72, 0x12, 0x26, 0x0a, 0x06, 0x63, 0x6f, 0x6c, 0x75, 0x6d, - 0x6e, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, - 0x2e, 0x43, 0x6f, 0x6c, 0x75, 0x6d, 0x6e, 0x52, 0x06, 0x63, 0x6f, 0x6c, 0x75, 0x6d, 0x6e, 0x22, - 0x87, 0x02, 0x0a, 0x0f, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, 0x65, 0x52, 0x65, 0x71, 0x75, - 0x65, 0x73, 0x74, 0x12, 0x2c, 0x0a, 0x08, 0x73, 0x65, 0x74, 0x74, 0x69, 0x6e, 0x67, 0x73, 0x18, - 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x10, 0x2e, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2e, 0x53, - 0x65, 0x74, 0x74, 0x69, 0x6e, 0x67, 0x73, 0x52, 0x08, 0x73, 0x65, 0x74, 0x74, 0x69, 0x6e, 0x67, - 0x73, 0x12, 0x29, 0x0a, 0x07, 0x63, 0x61, 0x74, 0x61, 0x6c, 0x6f, 0x67, 0x18, 0x02, 0x20, 0x01, - 0x28, 0x0b, 0x32, 0x0f, 0x2e, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2e, 0x43, 0x61, 0x74, 0x61, - 0x6c, 0x6f, 0x67, 0x52, 0x07, 0x63, 0x61, 0x74, 0x61, 0x6c, 0x6f, 0x67, 0x12, 0x27, 0x0a, 0x07, - 0x71, 0x75, 0x65, 0x72, 0x69, 0x65, 0x73, 0x18, 0x03, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0d, 0x2e, - 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2e, 0x51, 0x75, 0x65, 0x72, 0x79, 0x52, 0x07, 0x71, 0x75, - 0x65, 0x72, 0x69, 0x65, 0x73, 0x12, 0x22, 0x0a, 0x0c, 0x73, 0x71, 0x6c, 0x63, 0x5f, 0x76, 0x65, - 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0c, 0x73, 0x71, 0x6c, - 0x63, 0x5f, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x12, 0x26, 0x0a, 0x0e, 0x70, 0x6c, 0x75, - 0x67, 0x69, 0x6e, 0x5f, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0x05, 0x20, 0x01, 0x28, - 0x0c, 0x52, 0x0e, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x5f, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, - 0x73, 0x12, 0x26, 0x0a, 0x0e, 0x67, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x5f, 0x6f, 0x70, 0x74, 0x69, - 0x6f, 0x6e, 0x73, 0x18, 0x06, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x0e, 0x67, 0x6c, 0x6f, 0x62, 0x61, - 0x6c, 0x5f, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x22, 0x36, 0x0a, 0x10, 0x47, 0x65, 0x6e, - 0x65, 0x72, 0x61, 0x74, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x22, 0x0a, - 0x05, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x70, - 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2e, 0x46, 0x69, 0x6c, 0x65, 0x52, 0x05, 0x66, 0x69, 0x6c, 0x65, - 0x73, 0x32, 0x4f, 0x0a, 0x0e, 0x43, 0x6f, 0x64, 0x65, 0x67, 0x65, 0x6e, 0x53, 0x65, 0x72, 0x76, - 0x69, 0x63, 0x65, 0x12, 0x3d, 0x0a, 0x08, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, 0x65, 0x12, - 0x17, 0x2e, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2e, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, - 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x18, 0x2e, 0x70, 0x6c, 0x75, 0x67, 0x69, - 0x6e, 0x2e, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, - 0x73, 0x65, 0x42, 0x7c, 0x0a, 0x0a, 0x63, 0x6f, 0x6d, 0x2e, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, - 0x42, 0x0c, 0x43, 0x6f, 0x64, 0x65, 0x67, 0x65, 0x6e, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x50, 0x01, - 0x5a, 0x28, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x73, 0x71, 0x6c, - 0x63, 0x2d, 0x64, 0x65, 0x76, 0x2f, 0x73, 0x71, 0x6c, 0x63, 0x2f, 0x69, 0x6e, 0x74, 0x65, 0x72, - 0x6e, 0x61, 0x6c, 0x2f, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0xa2, 0x02, 0x03, 0x50, 0x58, 0x58, - 0xaa, 0x02, 0x06, 0x50, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0xca, 0x02, 0x06, 0x50, 0x6c, 0x75, 0x67, - 0x69, 0x6e, 0xe2, 0x02, 0x12, 0x50, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x5c, 0x47, 0x50, 0x42, 0x4d, - 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0xea, 0x02, 0x06, 0x50, 0x6c, 0x75, 0x67, 0x69, 0x6e, - 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x6c, 0x65, 0x12, 0x22, 0x0a, 0x0c, 0x72, 0x61, 0x77, 0x5f, 0x63, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, + 0x74, 0x73, 0x18, 0x09, 0x20, 0x03, 0x28, 0x09, 0x52, 0x0c, 0x72, 0x61, 0x77, 0x5f, 0x63, 0x6f, + 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x73, 0x22, 0x4b, 0x0a, 0x09, 0x50, 0x61, 0x72, 0x61, 0x6d, 0x65, + 0x74, 0x65, 0x72, 0x12, 0x16, 0x0a, 0x06, 0x6e, 0x75, 0x6d, 0x62, 0x65, 0x72, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x05, 0x52, 0x06, 0x6e, 0x75, 0x6d, 0x62, 0x65, 0x72, 0x12, 0x26, 0x0a, 0x06, 0x63, + 0x6f, 0x6c, 0x75, 0x6d, 0x6e, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x70, 0x6c, + 0x75, 0x67, 0x69, 0x6e, 0x2e, 0x43, 0x6f, 0x6c, 0x75, 0x6d, 0x6e, 0x52, 0x06, 0x63, 0x6f, 0x6c, + 0x75, 0x6d, 0x6e, 0x22, 0x87, 0x02, 0x0a, 0x0f, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, 0x65, + 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x2c, 0x0a, 0x08, 0x73, 0x65, 0x74, 0x74, 0x69, + 0x6e, 0x67, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x10, 0x2e, 0x70, 0x6c, 0x75, 0x67, + 0x69, 0x6e, 0x2e, 0x53, 0x65, 0x74, 0x74, 0x69, 0x6e, 0x67, 0x73, 0x52, 0x08, 0x73, 0x65, 0x74, + 0x74, 0x69, 0x6e, 0x67, 0x73, 0x12, 0x29, 0x0a, 0x07, 0x63, 0x61, 0x74, 0x61, 0x6c, 0x6f, 0x67, + 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0f, 0x2e, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2e, + 0x43, 0x61, 0x74, 0x61, 0x6c, 0x6f, 0x67, 0x52, 0x07, 0x63, 0x61, 0x74, 0x61, 0x6c, 0x6f, 0x67, + 0x12, 0x27, 0x0a, 0x07, 0x71, 0x75, 0x65, 0x72, 0x69, 0x65, 0x73, 0x18, 0x03, 0x20, 0x03, 0x28, + 0x0b, 0x32, 0x0d, 0x2e, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2e, 0x51, 0x75, 0x65, 0x72, 0x79, + 0x52, 0x07, 0x71, 0x75, 0x65, 0x72, 0x69, 0x65, 0x73, 0x12, 0x22, 0x0a, 0x0c, 0x73, 0x71, 0x6c, + 0x63, 0x5f, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x0c, 0x73, 0x71, 0x6c, 0x63, 0x5f, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x12, 0x26, 0x0a, + 0x0e, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x5f, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, + 0x05, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x0e, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x5f, 0x6f, 0x70, + 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x12, 0x26, 0x0a, 0x0e, 0x67, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x5f, + 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0x06, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x0e, 0x67, + 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x5f, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x22, 0x36, 0x0a, + 0x10, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, + 0x65, 0x12, 0x22, 0x0a, 0x05, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, + 0x32, 0x0c, 0x2e, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2e, 0x46, 0x69, 0x6c, 0x65, 0x52, 0x05, + 0x66, 0x69, 0x6c, 0x65, 0x73, 0x32, 0x4f, 0x0a, 0x0e, 0x43, 0x6f, 0x64, 0x65, 0x67, 0x65, 0x6e, + 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x3d, 0x0a, 0x08, 0x47, 0x65, 0x6e, 0x65, 0x72, + 0x61, 0x74, 0x65, 0x12, 0x17, 0x2e, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2e, 0x47, 0x65, 0x6e, + 0x65, 0x72, 0x61, 0x74, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x18, 0x2e, 0x70, + 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2e, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, 0x65, 0x52, 0x65, + 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x7c, 0x0a, 0x0a, 0x63, 0x6f, 0x6d, 0x2e, 0x70, 0x6c, + 0x75, 0x67, 0x69, 0x6e, 0x42, 0x0c, 0x43, 0x6f, 0x64, 0x65, 0x67, 0x65, 0x6e, 0x50, 0x72, 0x6f, + 0x74, 0x6f, 0x50, 0x01, 0x5a, 0x28, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, + 0x2f, 0x73, 0x71, 0x6c, 0x63, 0x2d, 0x64, 0x65, 0x76, 0x2f, 0x73, 0x71, 0x6c, 0x63, 0x2f, 0x69, + 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x6c, 0x2f, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0xa2, 0x02, + 0x03, 0x50, 0x58, 0x58, 0xaa, 0x02, 0x06, 0x50, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0xca, 0x02, 0x06, + 0x50, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0xe2, 0x02, 0x12, 0x50, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x5c, + 0x47, 0x50, 0x42, 0x4d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0xea, 0x02, 0x06, 0x50, 0x6c, + 0x75, 0x67, 0x69, 0x6e, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( diff --git a/internal/sql/catalog/table.go b/internal/sql/catalog/table.go index 41db0df92c..5a07e68d95 100644 --- a/internal/sql/catalog/table.go +++ b/internal/sql/catalog/table.go @@ -129,6 +129,7 @@ type Column struct { ArrayDims int Comment string Length *int + PrimaryKey bool RawComment string @@ -341,6 +342,7 @@ func (c *Catalog) defineColumn(table *ast.TableName, col *ast.ColumnDef) (*Colum ArrayDims: col.ArrayDims, Comment: col.Comment, Length: col.Length, + PrimaryKey: col.PrimaryKey, } if col.Vals != nil { typeName := ast.TypeName{ diff --git a/protos/plugin/codegen.proto b/protos/plugin/codegen.proto index 3d68814e20..2eaadcb9ca 100644 --- a/protos/plugin/codegen.proto +++ b/protos/plugin/codegen.proto @@ -104,6 +104,7 @@ message Column { string original_name = 15; bool unsigned = 16; int32 array_dims = 17; + bool PrimaryKey = 18; } message Query { @@ -115,6 +116,7 @@ message Query { repeated string comments = 6 [json_name = "comments"]; string filename = 7 [json_name = "filename"]; Identifier insert_into_table = 8 [json_name = "insert_into_table"]; + repeated string raw_comments = 9 [json_name = "raw_comments"]; } message Parameter { From 022ef7795a79f4d7b62d122ac218fec1428f192d Mon Sep 17 00:00:00 2001 From: Bruce Smith Date: Wed, 18 Sep 2024 19:22:58 -0400 Subject: [PATCH 6/7] added genproto --- cmd/sqlc-gen-proto/main.go | 1779 ++++++++++++++++++++++++++++++++++++ 1 file changed, 1779 insertions(+) create mode 100644 cmd/sqlc-gen-proto/main.go diff --git a/cmd/sqlc-gen-proto/main.go b/cmd/sqlc-gen-proto/main.go new file mode 100644 index 0000000000..b3c341ec99 --- /dev/null +++ b/cmd/sqlc-gen-proto/main.go @@ -0,0 +1,1779 @@ +package main + +import ( + "encoding/json" + "fmt" + "io" + "log" + "net/url" + "os" + "path/filepath" + "regexp" + "sort" + "strings" + + "github.com/ettle/strcase" + "github.com/sqlc-dev/sqlc/internal/codegen/sdk" + "github.com/sqlc-dev/sqlc/internal/plugin" + "google.golang.org/protobuf/proto" + + "github.com/bufbuild/protocompile/parser" + "github.com/bufbuild/protocompile/reporter" + "github.com/jhump/protoreflect/v2/protobuilder" + "google.golang.org/protobuf/reflect/protoreflect" + "google.golang.org/protobuf/types/known/anypb" + "google.golang.org/protobuf/types/known/structpb" + "google.golang.org/protobuf/types/known/timestamppb" + "google.golang.org/protobuf/types/known/wrapperspb" + + // "github.com/ryboe/q" + // "google.golang.org/genproto/googleapis/type/expr" + "google.golang.org/genproto/googleapis/api/annotations" + "google.golang.org/genproto/googleapis/type/decimal" + "google.golang.org/genproto/googleapis/type/money" + + "github.com/jhump/protoreflect/v2/protoprint" + + "google.golang.org/protobuf/types/descriptorpb" +) + +var ( + DEFAULT_OUTDIR = "./sqlcgen" + DEFAULT_USER_DEFINED_DIR = "./user_defined" + DEFAULT_DEFAULT_PACKAGE = "sqlcgen" + DEFAULT_ONE_OF_ID = "identifier" + SYNTAX_PROTO3 = "proto3" + DO_NOT_GENERATE = "DO_NOT_GENERATE" + + METHOD_NAMES = []string{"Create", "Get", "Update", "Delete", "List"} +) + +type options struct { + OutDir string `json:"out_dir,omitempty" yaml:"out_dir"` + UserDefinedDir string `json:"user_defined_dir,omitempty" yaml:"user_defined_dir"` + OneOfID string `json:"one_of_id,omitempty" yaml:"one_of_id"` + DefaultPackage string `json:"defaut_package,omitempty" yaml:"defaut_package"` +} + +func getGenRequest() (*plugin.GenerateRequest, error) { + var req plugin.GenerateRequest + reqBlob, err := io.ReadAll(os.Stdin) + if err != nil { + return nil, err + } + if err := proto.Unmarshal(reqBlob, &req); err != nil { + return nil, err + } + + return &req, nil +} + +func parseOptions(req *plugin.GenerateRequest) (*options, error) { + var options *options + if len(req.PluginOptions) == 0 { + return options, nil + } + if err := json.Unmarshal(req.PluginOptions, &options); err != nil { + return nil, err + } + if options.OutDir == "" { + options.OutDir = DEFAULT_OUTDIR + } + if options.UserDefinedDir == "" { + options.UserDefinedDir = DEFAULT_USER_DEFINED_DIR + } + if options.OneOfID == "" { + options.OneOfID = DEFAULT_ONE_OF_ID + } + if options.DefaultPackage == "" { + options.DefaultPackage = DEFAULT_DEFAULT_PACKAGE + } + + DEFAULT_OUTDIR = options.OutDir + DEFAULT_DEFAULT_PACKAGE = options.DefaultPackage + DEFAULT_ONE_OF_ID = options.OneOfID + DEFAULT_USER_DEFINED_DIR = options.UserDefinedDir + + return options, nil +} + +func main() { + fdm := make(map[fpath]*protobuilder.FileBuilder) + req, err := getGenRequest() + if err != nil { + log.Fatal(err) + } + opts, err := parseOptions(req) + if err != nil { + log.Fatal(err) + } + p := &Protos{ + files: fdm, + tables: make([]*table, 0), + enums: make([]*enum, 0), + queries: make([]*query, 0), + options: opts, + } + + if err := p.run(req); err != nil { + log.Fatal(err) + } +} + +// used to inform what kind of string to use in maps +type fpath string +type messagename string +type methodname string + +type Protofiles []*protobuilder.FileBuilder + +// Sort order map +var sortOrder = map[string]int{ + "enum.proto": 1, + "message.proto": 2, + "request_response.proto": 3, + "service.proto": 4, +} + +func (pf Protofiles) Len() int { + return len(pf) +} + +func (pf Protofiles) Swap(i, j int) { + pf[i], pf[j] = pf[j], pf[i] + +} + +func (pf Protofiles) Less(i, j int) bool { + filenameI := filepath.Base(pf[i].Path()) + filenameJ := filepath.Base(pf[j].Path()) + + orderI, okI := sortOrder[filenameI] + orderJ, okJ := sortOrder[filenameJ] + if okI && okJ { + return orderI < orderJ + } + + if okI { + return true + } + + if okJ { + return false + } + + return filenameI < filenameJ +} + +// map[ $outdir/$package/$filename] +// map["./sqlcgen/foo/bar/baz/v1/message.proto"] +type Protos struct { + queries []*query + tables []*table + enums []*enum + files map[fpath]*protobuilder.FileBuilder + options *options +} + +func handleSkip(s string, skips []string) bool { + for _, skip := range skips { + if s == skip { + return true + } + } + return false +} + +func copyAnnotations(t *table) error { + if t.a.ReqResp != nil { + t.a.ReqResp.a = &Annotations{ + Generate: t.a.Generate, + Package: t.a.Package, + OutDir: t.a.OutDir, + } + if err := setProps(t.a.ReqResp); err != nil { + return err + } + } + if t.a.Service != nil { + t.a.Service.a = &Annotations{ + Generate: t.a.Generate, + Package: t.a.Package, + OutDir: t.a.OutDir, + } + if err := setProps(t.a.Service); err != nil { + return err + } + } + return nil +} + +func toRequestName(method, msgName string) string { + return fmt.Sprintf("%s%s%s", method, msgName, "Request") + +} +func toResponseName(method, msgName string) string { + return fmt.Sprintf("%s%s%s", method, msgName, "Response") +} + +func toHttpRule(method string, t *table) *annotations.HttpRule { + var httpRule *annotations.HttpRule + identifier := t.a.PrimaryKey + + // POST, LIST + p := t.a.Service.Path.Path + // GET, UPDATE, DELETE + gp := fmt.Sprintf("%s/{%s}", p, identifier) + + switch method { + case "Create": + // Create are always POST with the path + // Ex: /v1/users -X POST + httpRule = &annotations.HttpRule{ + Pattern: &annotations.HttpRule_Post{ + Post: p, + }, + Body: "*", + } + case "Get": + // Get are always GET with path + primarykey, + // OneOf must have oneof the primary key. + // Connect cann't use a oneofs value. + // Ex: /v1/users/{uuid} + httpRule = &annotations.HttpRule{ + Pattern: &annotations.HttpRule_Get{ + Get: gp, + }, + } + case "Update": + // Update are always PUT with path + primarykey, + // OneOf must have oneof the primary key. + // Connect cann't use a oneofs value. + // Ex: /v1/users/{uuid} + httpRule = &annotations.HttpRule{ + Pattern: &annotations.HttpRule_Put{ + Put: gp, + }, + Body: "*", + } + case "Delete": + // Delete are always DELETE with path + primarykey, + // OneOf must have oneof the primary key. + // Connect cann't use a oneofs value. + // Ex: /v1/users/{uuid} + httpRule = &annotations.HttpRule{ + Pattern: &annotations.HttpRule_Delete{ + Delete: gp, + }, + } + case "List": + // List are always GET with path, + // OneOf must have oneof the primary key. + // Connect cann't use a oneofs value. + // Ex: /v1/users/{uuid} + httpRule = &annotations.HttpRule{ + Pattern: &annotations.HttpRule_Get{ + Get: p, + }, + } + } + + return httpRule +} + +func (p Protos) createServices( + mName string, + rrMap map[methodname]*protobuilder.MessageBuilder, + t *table, +) (err error) { + svcfb := p.getFD(t.a.Service.a) + sName := toPascal(t.a.Service.Name) + n := protoreflect.Name(*sName) + + sb := svcfb.GetService(n) + if sb == nil { + sb = protobuilder.NewService(n) + defer func() { + if iErr := svcfb.TryAddService(sb); err != nil { + err = iErr + } + }() + } + for _, method := range METHOD_NAMES { + req := rrMap[methodname(toRequestName(method, mName))] + resp := rrMap[methodname(toResponseName(method, mName))] + + reqRPC := protobuilder.RpcTypeMessage(req, false) + respRPC := protobuilder.RpcTypeMessage(resp, false) + methName := fmt.Sprintf("%s%s", method, mName) + + mb := protobuilder.NewMethod( + protoreflect.Name(methName), + reqRPC, + respRPC, + ) + + httpRule := toHttpRule(method, t) + + methodOptions := &descriptorpb.MethodOptions{} + proto.SetExtension(methodOptions, annotations.E_Http, httpRule) + mb.SetOptions(methodOptions) + + // mtdOpts := (*descriptorpb.MethodOptions{}(nil).ProtoReflect().Descriptor()) + // mb.Options.GetFeatures + // mb.SetOptions() + + if err := sb.TryAddMethod(mb); err != nil { + return err + } + } + + return nil +} + +func (p Protos) createRequestResponses( + messageb *protobuilder.MessageBuilder, + t *table, +) (map[methodname]*protobuilder.MessageBuilder, error) { + mName := string(messageb.Name()) + // Copy over Annotations Into New Pointer + // and Set Properties for ReqResp type. + // if err := copyAnnotations(t); err != nil { + // return nil, err + // } + + // Get a new FileDescriptor + rrfb := p.getFD(t.a.ReqResp.a) + + reqrespMap := make(map[methodname]*protobuilder.MessageBuilder) + for _, method := range METHOD_NAMES { + reqName := toRequestName(method, mName) + respName := toResponseName(method, mName) + + reqb := protobuilder.NewMessage(protoreflect.Name(reqName)) + respb := protobuilder.NewMessage(protoreflect.Name(respName)) + + // Add Annotedated Additional Fields + for aType, aField := range t.a.ReqResp.ReqFields { + at, err := p.convertType(aType) + if err != nil { + return nil, err + } + ab := protobuilder.NewField( + protoreflect.Name(aField), + at, + ) + if err := reqb.TryAddField(ab); err != nil { + return nil, err + } + + } + + // Add OneOf + var oneof *protobuilder.OneofBuilder + if len(*t.a.ReqResp.OneOf) > 0 { + oneof = protobuilder.NewOneof(protoreflect.Name(p.options.OneOfID)) + } + for _, ooField := range *t.a.ReqResp.OneOf { + ooName := protoreflect.Name(ooField) + oob := messageb.GetField(ooName) + if oob != nil { + fCopy := protobuilder.NewField(oob.Name(), oob.Type()) + if err := oneof.TryAddChoice(fCopy); err != nil { + return nil, err + } + } + } + + if method == "Get" || method == "Update" || method == "Delete" { + if err := reqb.TryAddOneOf(oneof); err != nil { + return nil, err + } + } + + // only for Create, Update, + if method == "Create" || method == "Update" { + fb := protobuilder.NewField( + protoreflect.Name(*toLowerSnake(mName)), + protobuilder.FieldTypeMessage(messageb), + ) + if err := reqb.TryAddField(fb); err != nil { + return nil, err + } + } + + if method == "List" { + psb := protobuilder.NewField( + protoreflect.Name("page_size"), + protobuilder.FieldTypeInt32(), + ) + if err := reqb.TryAddField(psb); err != nil { + return nil, err + } + ptb := protobuilder.NewField( + protoreflect.Name("page_token"), + protobuilder.FieldTypeString(), + ) + if err := reqb.TryAddField(ptb); err != nil { + return nil, err + } + } + + if err := rrfb.TryAddMessage(reqb); err != nil { + return nil, err + } + + skip := false + for m, enabled := range t.a.ReqResp.RespEmpty { + if strings.ToLower(method) == strings.ToLower(m) && enabled { + skip = true + continue + } + } + if skip || method != "Delete" { + fb := protobuilder.NewField( + protoreflect.Name(*toLowerSnake(mName)), + protobuilder.FieldTypeMessage(messageb), + ) + if method == "List" { + fb.SetRepeated() + npt := protobuilder.NewField( + protoreflect.Name("next_page_token"), + protobuilder.FieldTypeString(), + ) + if err := respb.TryAddField(npt); err != nil { + return nil, err + } + } + if err := respb.TryAddField(fb); err != nil { + return nil, err + } + } + + if err := rrfb.TryAddMessage(respb); err != nil { + return nil, err + } + + reqrespMap[methodname(reqName)] = reqb + reqrespMap[methodname(respName)] = respb + } + + return reqrespMap, nil +} + +func (p Protos) tableToMessage( + fileb *protobuilder.FileBuilder, + t *table, +) error { + mName := *toPascal(t.i.Rel.Name) + messageb := protobuilder.NewMessage(protoreflect.Name(mName)) + + for _, c := range t.i.Columns { + if handleSkip(c.Name, t.a.Skips) { + continue + } + if c.PrimaryKey { + t.a.PrimaryKey = c.Name + } + cName := protoreflect.Name(c.Name) + t, err := p.convertType(c) + if err != nil { + return err + } + + fieldb := protobuilder.NewField(cName, t) + if c.IsArray { + fieldb.SetRepeated() + } + + if err := messageb.TryAddField(fieldb); err != nil { + return err + } + } + + if err := fileb.TryAddMessage(messageb); err != nil { + return err + } + + // Copy over Annotations Into New Pointer + // and Set Properties for ReqResp type. + if err := copyAnnotations(t); err != nil { + return err + } + // Handle request_response + var rrMap map[methodname]*protobuilder.MessageBuilder + if t.a.ReqResp != nil { + rr, err := p.createRequestResponses(messageb, t) + if err != nil { + return err + } + rrMap = rr + } + + // Handle service + if t.a.Service != nil { + if err := p.createServices(mName, rrMap, t); err != nil { + return err + } + } + + return nil +} + +func (p Protos) queryToMessage( + messageb *protobuilder.MessageBuilder, + q *query, +) error { + for _, c := range q.i.Columns { + if handleSkip(c.Name, q.a.Skips) { + continue + } + // Append missing Columns from queries to Target + cName := protoreflect.Name(c.Name) + if messageb.GetField(cName) == nil { + t, err := p.convertType(c) + if err != nil { + return err + } + fieldb := protobuilder.NewField(cName, t) + if c.IsArray { + fieldb.SetRepeated() + } + if err := messageb.TryAddField(fieldb); err != nil { + return err + } + } + } + return nil +} + +// lgetFD will reutrn a new empty FD if one does not exist +func (p Protos) getFD(a *Annotations) *protobuilder.FileBuilder { + op := fpath(a.FullPath) + if p.files[op] == nil { + file := protobuilder.NewFile("") + file.SetSyntax(protoreflect.Proto3) + file.SetPath(a.FullPath) + file.SetPackageName(protoreflect.FullName(a.Package)) + p.files[op] = file + + } + return p.files[op] +} + +// Responsible for Constructing message.proto +func (p Protos) Messages() error { + for _, t := range p.tables { + fb := p.getFD(t.a) + + if err := p.tableToMessage(fb, t); err != nil { + return err + } + } + + return nil +} + +func (p Protos) Queries() error { + for _, q := range p.queries { + mb, err := p.GetMessage(protoreflect.Name(*toPascal(q.a.Target))) + if err != nil { + return err + } + if err := p.queryToMessage(mb, q); err != nil { + return err + } + } + + return nil +} + +func (p Protos) appendFieldsToMessage( + desc *descriptorpb.DescriptorProto, + b *protobuilder.MessageBuilder, +) error { + for _, uf := range desc.GetField() { + var fType *protobuilder.FieldType + if b.GetField(protoreflect.Name(*uf.Name)) != nil { + continue + } + if uf.Type != nil { + ft, err := p.convertType(uf.Type.String()) + if err != nil { + return err + } + fType = ft + } else { + if uf.TypeName != nil { + ft, err := p.convertType(userDefinedString(uf.TypeName)) + if err != nil { + return err + } + fType = ft + } + } + b.AddField(protobuilder.NewField( + protoreflect.Name(*uf.Name), + fType, + )) + } + + return nil +} + +func (p Protos) appendMethodsToService( + desc *descriptorpb.ServiceDescriptorProto, + b *protobuilder.ServiceBuilder, +) error { + for _, mm := range desc.GetMethod() { + if b.GetMethod(protoreflect.Name(*mm.Name)) != nil { + continue + } + itb, err := p.GetMessage(protoreflect.Name(*mm.InputType)) + if err != nil { + return err + } + otb, err := p.GetMessage(protoreflect.Name(*mm.OutputType)) + if err != nil { + return err + } + reqb := protobuilder.RpcTypeMessage(itb, false) + respb := protobuilder.RpcTypeMessage(otb, false) + + nmb := protobuilder.NewMethod(protoreflect.Name(*mm.Name), reqb, respb) + nmb.SetOptions(mm.Options) + + if err := b.TryAddMethod(nmb); err != nil { + return err + } + } + + return nil +} + +func (p Protos) appendValuesToEnum( + desc *descriptorpb.EnumDescriptorProto, + b *protobuilder.EnumBuilder, +) error { + for _, em := range desc.GetValue() { + if b.GetValue(protoreflect.Name(*em.Name)) != nil { + continue + } + evb := protobuilder.NewEnumValue(protoreflect.Name(*em.Name)) + if err := b.TryAddValue(evb); err != nil { + return err + } + } + + return nil +} + +// Always sorts enum.proto, message.proto, request_response.proto, service.proto +func (p Protos) GetFiles() []*protobuilder.FileBuilder { + var filesSlice Protofiles + + for _, f := range p.files { + filesSlice = append(filesSlice, f) + } + + sort.Sort(filesSlice) + + return filesSlice +} + +func (p Protos) UserDefined() error { + for _, file := range p.GetFiles() { + mp := fmt.Sprintf("%s/%s", p.options.UserDefinedDir, file.Path()) + + if _, err := os.Stat(mp); err != nil { + continue + } + mFile, err := os.Open(mp) + if err != nil { + return err + } + filename := filepath.Base(mp) + node, err := parser.Parse(filename, mFile, reporter.NewHandler(nil)) + if err != nil { + return err + } + result, err := parser.ResultFromAST(node, false, reporter.NewHandler(nil)) + if err != nil { + return err + } + udFile := result.FileDescriptorProto() + + // Enums + for _, ue := range udFile.GetEnumType() { + e := file.GetEnum(protoreflect.Name(*ue.Name)) + if e == nil { + neb := protobuilder.NewEnum(protoreflect.Name(*ue.Name)) + if err := p.appendValuesToEnum(ue, neb); err != nil { + return err + } + if err := file.TryAddEnum(neb); err != nil { + return err + } + continue + } + if err := p.appendValuesToEnum(ue, e); err != nil { + return err + } + } + + // Messaages + for _, um := range udFile.GetMessageType() { + m := file.GetMessage(protoreflect.Name(*um.Name)) + if m == nil { + nmb := protobuilder.NewMessage(protoreflect.Name(*um.Name)) + if err := p.appendFieldsToMessage(um, nmb); err != nil { + return err + } + if err := file.TryAddMessage(nmb); err != nil { + return err + } + continue + } + + if err := p.appendFieldsToMessage(um, m); err != nil { + return err + } + } + + // Services + for _, sm := range udFile.GetService() { + s := file.GetService(protoreflect.Name(*sm.Name)) + if s == nil { + nsb := protobuilder.NewService(protoreflect.Name(*sm.Name)) + if err := p.appendMethodsToService(sm, nsb); err != nil { + return err + } + if err := file.TryAddService(nsb); err != nil { + return err + } + // make new service + continue + } + if err := p.appendMethodsToService(sm, s); err != nil { + return err + } + + } + } + + return nil +} + +func (p Protos) GetMessage(name protoreflect.Name) (*protobuilder.MessageBuilder, error) { + for _, file := range p.files { + m := file.GetMessage(name) + if m == nil { + continue + } + return m, nil + } + + return nil, fmt.Errorf("%q: Message Not Found.", name) +} + +// Responsible for Creating enum.proto files +func (p Protos) Enums() error { + for _, enum := range p.enums { + pn := *toPascal(enum.i.Name) + fb := p.getFD(enum.a) + eName := protoreflect.Name(pn) + + eb := protobuilder.NewEnum(eName) + + vals := convertEnumValues(pn, enum.i.Vals) + for _, val := range vals { + vName := protoreflect.Name(val) + if err := eb.TryAddValue(protobuilder.NewEnumValue(vName)); err != nil { + return err + } + } + if err := fb.TryAddEnum(eb); err != nil { + return err + } + } + return nil +} + +func (p Protos) WriteFiles() error { + fdSlice := []protoreflect.FileDescriptor{} + printer := protoprint.Printer{} + for _, file := range p.files { + b, err := file.Build() + if err != nil { + return err + } + + fdSlice = append(fdSlice, b) + } + + if err := os.MkdirAll(p.options.OutDir, os.ModePerm); err != nil { + return err + } + if err := printer.PrintProtosToFileSystem(fdSlice, p.options.OutDir); err != nil { + return err + } + + return nil +} + +type Annotations struct { + Generate bool // All: Generate Protos. + Package string // All: Package Name. + // Replace map[string]PType // Tables -> Messages: Type Replacement + Skips []string // Tables -> Messages: Skip Field + ReqResp *ReqResp // Tables -> Messaes: Information for generating Request and Responses + Service *Service // Tables -> Messaes: Information for generating Services + Target string // Applies only to querys + FileName string // Override output filename + OutDir string // Override base output directory + + FullPath string // Generated from Package + FilenName + FullTypeName string // Generated from Package + FilenName + OutputPath string // Generated from OutDir + FullPath + PrimaryKey string +} + +// enumWrapper attaches parsed comment Annotations for plugin.Enum +type enum struct { + i *plugin.Enum + a *Annotations +} + +// queryWrapper attaches parsed comment Annotations to plugin.Query +type query struct { + i *plugin.Query + a *Annotations +} + +// tableWrapper attaches parsed comment Annotations to plugin.Table +type table struct { + i *plugin.Table + a *Annotations +} + +func wrapTable(i *plugin.Table) (*table, error) { + a, err := parseAnnotations(i.RawComments) + if err != nil { + return nil, err + } + x := &table{ + i: i, + a: a, + } + + if err := setProps(x); err != nil { + return nil, err + } + return x, nil +} + +func wrapQuery(i *plugin.Query) (*query, error) { + a, err := parseAnnotations(i.RawComments) + if err != nil { + return nil, err + } + x := &query{ + i: i, + a: a, + } + if err := setProps(x); err != nil { + return nil, err + } + return x, nil +} + +func wrapEnum(i *plugin.Enum) (*enum, error) { + a, err := parseAnnotations(i.RawComments) + if err != nil { + return nil, err + } + x := &enum{ + i: i, + a: a, + } + if err := setProps(x); err != nil { + return nil, err + } + return x, nil +} + +func (p *Protos) run(req *plugin.GenerateRequest) error { + + schemas := req.GetCatalog().GetSchemas() + queries := req.GetQueries() + + for _, schema := range schemas { + if schema.Name == "pg_catalog" || schema.Name == "information_schema" { + continue + } + // enums = append(enums, schema.GetEnums()...) + for _, table := range schema.GetTables() { + t, err := wrapTable(table) + if err != nil { + if err.Error() == DO_NOT_GENERATE { + continue + } + return err + } + p.tables = append(p.tables, t) + } + for _, enum := range schema.GetEnums() { + e, err := wrapEnum(enum) + if err != nil { + if err.Error() == DO_NOT_GENERATE { + continue + } + return err + } + p.enums = append(p.enums, e) + } + } + + for _, query := range queries { + q, err := wrapQuery(query) + if err != nil { + if err.Error() == DO_NOT_GENERATE { + continue + } + return err + } + p.queries = append(p.queries, q) + } + + if err := p.Enums(); err != nil { + return err + } + if err := p.Messages(); err != nil { + return err + } + if err := p.Queries(); err != nil { + return err + } + + if err := p.UserDefined(); err != nil { + return err + } + + if err := p.WriteFiles(); err != nil { + return err + } + + return nil +} + +func parseAnnotations(comments []string) (*Annotations, error) { + // replace := make(map[string]PType) + a := &Annotations{ + // Replace: replace, + } + + for _, line := range comments { + var prefix string + if strings.HasPrefix(line, "--") { + prefix = "--" + } + if strings.HasPrefix(line, "/*") { + prefix = "/*" + } + if strings.HasPrefix(line, "#") { + prefix = "#" + } + if prefix == "" { + continue + } + rest := line[len(prefix):] + if !strings.Contains(rest, ":") { + continue + } + for _, flagOpt := range []string{ + "generate", + "service", + } { + if !strings.HasPrefix(strings.TrimSpace(rest), flagOpt) { + continue + } + opt := fmt.Sprintf(" %s:", flagOpt) + + if !strings.HasPrefix(rest, opt) { + return nil, fmt.Errorf("invalid metadata: %s", line) + } + switch flagOpt { + case "generate": + a.Generate = true + case "service": + } + } + + for _, cmdOption := range []string{ + "package", + "replace", + "filename", + "target", + "skip", + "request_response", + "service", + } { + if !strings.HasPrefix(strings.TrimSpace(rest), cmdOption) { + continue + } + opt := fmt.Sprintf(" %s: ", cmdOption) + + if !strings.HasPrefix(rest, opt) { + return nil, fmt.Errorf("invalid metadata: %s", line) + } + + part := strings.Split(strings.TrimSpace(line), " ") + + switch cmdOption { + case "package": + if len(part) != 3 { + return nil, fmt.Errorf("-- package: ... takes exactly 1 argument") + } + packageName := part[2] + a.Package = packageName + case "target": + if len(part) != 3 { + return nil, fmt.Errorf( + "-- target: ... takes exactly 1 argument", + ) + } + a.Target = part[2] + case "skip": + if len(part) != 3 { + return nil, fmt.Errorf( + "-- skip: ... takes exactly 1 argument", + ) + } + skipField := part[2] + a.Skips = append(a.Skips, skipField) + case "request_response": + if len(part) < 3 { + return nil, fmt.Errorf( + "-- request_response: takes at minimum 2 argument", + ) + } + if a.ReqResp == nil { + es := []string{} + a.ReqResp = &ReqResp{ + OneOf: &es, + ReqFields: make(map[string]string), + RespEmpty: make(map[string]bool), + } + } + switch part[2] { + case "oneof": + if len(part) >= 5 { + *a.ReqResp.OneOf = append(*a.ReqResp.OneOf, part[3:]...) + } + if len(part) == 3 { + emptySlice := []string{} + a.ReqResp.OneOf = &emptySlice + } + case "req_field": + if len(part) != 5 { + return nil, fmt.Errorf( + "-- request_response: req_field takes exactly 2 arguments.", + ) + } + a.ReqResp.ReqFields = make(map[string]string) + a.ReqResp.ReqFields[part[3]] = part[4] + // a.ReqResp = rr + case "resp_empty": + if len(part) != 4 { + return nil, fmt.Errorf( + "-- request_response: resp_empty takes exactly 1 arguments.", + ) + } + a.ReqResp.RespEmpty = make(map[string]bool) + a.ReqResp.RespEmpty[part[3]] = true + } + case "service": + if len(part) != 4 { + return nil, fmt.Errorf( + "-- service: ... takes exactly 2 argument", + ) + } + name := part[2] + path := part[3] + p, err := url.Parse(path) + if err != nil { + return nil, err + } + a.Service = &Service{ + Path: p, + Name: name, + } + } + } + } + + return a, nil +} + +func toImportPath(pkg string, filename string) string { + return fmt.Sprintf("%s/%s", pkgToPath(pkg), filename) +} + +func pkgToPath(s string) string { + // validate against protected type names in importsl + return strings.ReplaceAll(s, ".", "/") +} + +func validatePackageName(s string) error { + tokens := strings.Split(s, ".") + for _, reserved := range []string{"enum", "message", "import", "syntax", "repeated"} { + if len(tokens) > 0 { + if tokens[0] == reserved { + return fmt.Errorf( + "%q: is a reserved word and cannot be used as first part in package: %q", + tokens[0], + s, + ) + } + } + } + + return nil +} + +func toPascal(s string) *string { + r := strings.ToLower(s) + r = strcase.ToPascal(s) + + return &r +} + +func toLowerSnake(s string) *string { + s = strings.ToLower(s) + s = strcase.ToSnake(s) + return &s +} + +func setCommonProps(a *Annotations) error { + if a.Package == "" { + a.Package = DEFAULT_DEFAULT_PACKAGE + } + + if err := validatePackageName(a.Package); err != nil { + return err + } + + if a.OutDir == "" { + a.OutDir = DEFAULT_OUTDIR + } + // Example Package: foo.bar.baz.v1 + // Example Filename: message.proto + // Path relative from root: foo/bar/baz/v1/message.proto + a.FullPath = fmt.Sprintf("%s/%s", pkgToPath(a.Package), a.FileName) + + // Example Package: foo.bar.baz.v1 + // Example Filename: message.proto + // Path relative from root: foo/bar/baz/v1/message.proto + // a.FullTypeName = fmt.Sprintf("%s.%s", a.Package, a.FileName) + + // Full Filesystem path to be written to + a.OutputPath = fmt.Sprintf("%s/%s", a.OutDir, a.FullPath) + + return nil +} + +func setProps(input interface{}) error { + switch i := input.(type) { + case *query: + if !i.a.Generate { + return fmt.Errorf(DO_NOT_GENERATE) + } + if i.a.Target == "" { + return fmt.Errorf( + "To append columns from Queries to protobufs you must declare a target: ", + ) + + } + if err := setCommonProps(i.a); err != nil { + return err + } + case *enum: + if !i.a.Generate { + return fmt.Errorf(DO_NOT_GENERATE) + } + if i.a.FileName == "" { + i.a.FileName = "enum.proto" + } + if err := setCommonProps(i.a); err != nil { + return err + } + case *table: + if !i.a.Generate { + return fmt.Errorf(DO_NOT_GENERATE) + } + if i.a.FileName == "" { + i.a.FileName = "message.proto" + } + if err := setCommonProps(i.a); err != nil { + return err + } + case *ReqResp: + if !i.a.Generate { + return fmt.Errorf(DO_NOT_GENERATE) + } + if i.a.FileName == "" { + i.a.FileName = "request_response.proto" + } + if err := setCommonProps(i.a); err != nil { + return err + } + case *Service: + if !i.a.Generate { + return fmt.Errorf(DO_NOT_GENERATE) + } + if i.a.FileName == "" { + i.a.FileName = "service.proto" + } + if err := setCommonProps(i.a); err != nil { + return err + } + } + + return nil +} + +type userDefinedString *string + +func (p *Protos) convertType(input interface{}) (*protobuilder.FieldType, error) { + var ct string + var notNull bool + + switch i := input.(type) { + case string: + ct = i + notNull = true + case userDefinedString: + ct = *i + case *plugin.Column: + s := sdk.DataType(i.Type) + notNull = i.NotNull || i.IsArray + ct = strings.ToLower(s) + } + + tAny := (*anypb.Any)(nil).ProtoReflect().Descriptor() + tI32 := (*wrapperspb.Int32Value)(nil).ProtoReflect().Descriptor() + tI64 := (*wrapperspb.Int64Value)(nil).ProtoReflect().Descriptor() + tFloat := (*wrapperspb.FloatValue)(nil).ProtoReflect().Descriptor() + tBytes := (*wrapperspb.BytesValue)(nil).ProtoReflect().Descriptor() + tBool := (*wrapperspb.BoolValue)(nil).ProtoReflect().Descriptor() + tString := (*wrapperspb.StringValue)(nil).ProtoReflect().Descriptor() + tDecimal := (*decimal.Decimal)(nil).ProtoReflect().Descriptor() + tMoney := (*money.Money)(nil).ProtoReflect().Descriptor() + tStruct := (*structpb.Struct)(nil).ProtoReflect().Descriptor() + tTimestamp := (*timestamppb.Timestamp)(nil).ProtoReflect().Descriptor() + + switch ct { + // Int32 + case "integer", + "int", + "int4", + "pg_catalog.int4", + "serial", + "serial4", + "pg_catalog.serial4", + "smallserial", + "smallint", "int2", "pg_catalog.int2", "serial2", + "pg_catalog.serial2", + WellKnownInt32Value: + if notNull { + return protobuilder.FieldTypeInt32(), nil + } + return protobuilder.FieldTypeImportedMessage( + tI32, + ), nil + + // Int64 + case "interval", + "pg_catalog.interval", + "bigint", + "int8", + "pg_catalog.int8", + "bigserial", + "serial8", + "pg_catalog.serial8", + "TYPE_INT64", + WellKnownInt64Value: + if notNull { + return protobuilder.FieldTypeInt64(), nil + } + return protobuilder.FieldTypeImportedMessage( + tI64, + ), nil + + // Float + case "real", + "float4", + "pg_catalog.float4", + "float", + "double precision", + "float8", + "pg_catalog.float8", + "TYPE_DOUBLE", + "TYPE_FLOAT", + WellKnownFloatValue: + if notNull { + return protobuilder.FieldTypeFloat(), nil + } + return protobuilder.FieldTypeImportedMessage( + tFloat, + ), nil + + case "numeric", "pg_catalog.numeric", WellKnownDecimal: + return protobuilder.FieldTypeImportedMessage( + tDecimal, + ), nil + + case "money", WellKnownMoney: + return protobuilder.FieldTypeImportedMessage( + tMoney, + ), nil + + case "boolean", "bool", "pg_catalog.bool", WellKnownBoolValue: + if notNull { + return protobuilder.FieldTypeBool(), nil + } + return protobuilder.FieldTypeImportedMessage( + tBool, + ), nil + + case "json", WellKnownStruct: + return protobuilder.FieldTypeImportedMessage( + tStruct, + ), nil + + case "uuid", "jsonb", "bytea", "blob", "pg_catalog.bytea", WellKnownBytesValue: + if notNull { + return protobuilder.FieldTypeBytes(), nil + } + return protobuilder.FieldTypeImportedMessage( + tBytes, + ), nil + + case "pg_catalog.timestamptz", + "date", + "timestamptz", + "pg_catalog.timestamp", + "pg_catalog.timetz", + "pg_catalog.time", + WellKnownTimestamp: + return protobuilder.FieldTypeImportedMessage( + tTimestamp, + ), nil + + case "citext", + "lquery", + "ltree", + "ltxtquery", + "name", + "inet", + "cidr", + "macaddr", + "macaddr8", + "pg_catalog.bpchar", + "pg_catalog.varchar", + "string", + "text", + WellKnownStringValue: + if notNull { + return protobuilder.FieldTypeString(), nil + } + return protobuilder.FieldTypeImportedMessage( + tString, + ), nil + + // All these PG Range Types Required FieldOptions + // Handle this Later + case "daterange": + // switch driver { + // case opts.SQLDriverPGXV4: + // return "pgtype.Daterange" + // case opts.SQLDriverPGXV5: + // return "pgtype.Range[pgtype.Date]" + // default: + // return "interface{}" + // } + + case "datemultirange": + // switch driver { + // case opts.SQLDriverPGXV5: + // return "pgtype.Multirange[pgtype.Range[pgtype.Date]]" + // default: + // return "interface{}" + // } + + case "tsrange": + // switch driver { + // case opts.SQLDriverPGXV4: + // return "pgtype.Tsrange" + // case opts.SQLDriverPGXV5: + // return "pgtype.Range[pgtype.Timestamp]" + // default: + // return "interface{}" + // } + + case "tsmultirange": + // switch driver { + // case opts.SQLDriverPGXV5: + // return "pgtype.Multirange[pgtype.Range[pgtype.Timestamp]]" + // default: + // return "interface{}" + // } + + case "tstzrange": + // switch driver { + // case opts.SQLDriverPGXV4: + // return "pgtype.Tstzrange" + // case opts.SQLDriverPGXV5: + // return "pgtype.Range[pgtype.Timestamptz]" + // default: + // return "interface{}" + // } + + case "tstzmultirange": + // switch driver { + // case opts.SQLDriverPGXV5: + // return "pgtype.Multirange[pgtype.Range[pgtype.Timestamptz]]" + // default: + // return "interface{}" + // } + + case "numrange": + // switch driver { + // case opts.SQLDriverPGXV4: + // return "pgtype.Numrange" + // case opts.SQLDriverPGXV5: + // return "pgtype.Range[pgtype.Numeric]" + // default: + // return "interface{}" + // } + + case "nummultirange": + // switch driver { + // case opts.SQLDriverPGXV5: + // return "pgtype.Multirange[pgtype.Range[pgtype.Numeric]]" + // default: + // return "interface{}" + // } + + case "int4range": + // switch driver { + // case opts.SQLDriverPGXV4: + // return "pgtype.Int4range" + // case opts.SQLDriverPGXV5: + // return "pgtype.Range[pgtype.Int4]" + // default: + // return "interface{}" + // } + + case "int4multirange": + // switch driver { + // case opts.SQLDriverPGXV5: + // return "pgtype.Multirange[pgtype.Range[pgtype.Int4]]" + // default: + // return "interface{}" + // } + + case "int8range": + // switch driver { + // case opts.SQLDriverPGXV4: + // return "pgtype.Int8range" + // case opts.SQLDriverPGXV5: + // return "pgtype.Range[pgtype.Int8]" + // default: + // return "interface{}" + // } + + case "int8multirange": + // switch driver { + // case opts.SQLDriverPGXV5: + // return "pgtype.Multirange[pgtype.Range[pgtype.Int8]]" + // default: + // return "interface{}" + // } + + case "hstore": + // if driver.IsPGX() { + // return "pgtype.Hstore" + // } + return protobuilder.FieldTypeImportedMessage( + tAny, + ), nil + + case "bit", "varbit", "pg_catalog.bit", "pg_catalog.varbit": + // if driver == opts.SQLDriverPGXV5 { + // return "pgtype.Bits" + // } + // if driver == opts.SQLDriverPGXV4 { + // return "pgtype.Varbit" + // } + + case "cid": + // if driver == opts.SQLDriverPGXV5 { + // return "pgtype.Uint32" + // } + // if driver == opts.SQLDriverPGXV4 { + // return "pgtype.CID" + // } + + case "oid": + // if driver == opts.SQLDriverPGXV5 { + // return "pgtype.Uint32" + // } + // if driver == opts.SQLDriverPGXV4 { + // return "pgtype.OID" + // } + + case "tid": + // if driver.IsPGX() { + // return "pgtype.TID" + // } + + case "xid": + // if driver == opts.SQLDriverPGXV5 { + // return "pgtype.Uint32" + // } + // if driver == opts.SQLDriverPGXV4 { + // return "pgtype.XID" + // } + + case "box": + // if driver.IsPGX() { + // return "pgtype.Box" + // } + + case "circle": + // if driver.IsPGX() { + // return "pgtype.Circle" + // } + + case "line": + // if driver.IsPGX() { + // return "pgtype.Line" + // } + + case "lseg": + // if driver.IsPGX() { + // return "pgtype.Lseg" + // } + + case "path": + // if driver.IsPGX() { + // return "pgtype.Path" + // } + + case "point": + // if driver.IsPGX() { + // return "pgtype.Point" + // } + + case "polygon": + // if driver.IsPGX() { + // return "pgtype.Polygon" + // } + + case "vector": + // if driver == opts.SQLDriverPGXV5 { + // if emitPointersForNull { + // return "*pgvector.Vector" + // } else { + // return "pgvector.Vector" + // } + // } + + case "void": + // A void value can only be scanned into an empty interface. + return protobuilder.FieldTypeImportedMessage( + tAny, + ), nil + + case "any", WellKnownAny: + return protobuilder.FieldTypeImportedMessage( + tAny, + ), nil + + // If we are here check for enum + default: + tKind, err := stringToKind(ct) + if err == nil { + return protobuilder.FieldTypeScalar(tKind), nil + } + + for _, f := range p.files { + // We have to handle for when UserDefined comes in with EnumType + // Ex: foo.bar.baz.v1.$EnumType + ct = filepath.Base(pkgToPath(ct)) + tName := protoreflect.Name(*toPascal(ct)) + // Check for Enums + eb := f.GetEnum(tName) + if eb != nil { + return protobuilder.FieldTypeEnum(eb), nil + } + // Check For Messages + mb := f.GetMessage(tName) + if mb != nil { + return protobuilder.FieldTypeMessage(mb), nil + } + } + } + + return nil, fmt.Errorf("%s: Type Conversion Not Implemented. Use --replace: or --skip:", ct) +} + +func stringToKind(typeString string) (protoreflect.Kind, error) { + switch strings.ToUpper(typeString) { + case "TYPE_DOUBLE": + return protoreflect.DoubleKind, nil + case "TYPE_FLOAT": + return protoreflect.FloatKind, nil + case "TYPE_INT64": + return protoreflect.Int64Kind, nil + case "TYPE_UINT64": + return protoreflect.Uint64Kind, nil + case "TYPE_INT32": + return protoreflect.Int32Kind, nil + case "TYPE_FIXED64": + return protoreflect.Fixed64Kind, nil + case "TYPE_FIXED32": + return protoreflect.Fixed32Kind, nil + case "TYPE_BOOL": + return protoreflect.BoolKind, nil + case "TYPE_STRING": + return protoreflect.StringKind, nil + case "TYPE_GROUP": + return protoreflect.GroupKind, nil + case "TYPE_MESSAGE": + return protoreflect.MessageKind, nil + case "TYPE_BYTES": + return protoreflect.BytesKind, nil + case "TYPE_UINT32": + return protoreflect.Uint32Kind, nil + case "TYPE_ENUM": + return protoreflect.EnumKind, nil + case "TYPE_SFIXED32": + return protoreflect.Sfixed32Kind, nil + case "TYPE_SFIXED64": + return protoreflect.Sfixed64Kind, nil + case "TYPE_SINT32": + return protoreflect.Sint32Kind, nil + case "TYPE_SINT64": + return protoreflect.Sint64Kind, nil + // Add more cases for different types as needed + default: + return protoreflect.Kind(0), fmt.Errorf("unknown type: %s", typeString) + } +} + +// Takes a EnumName and Values and PREFIXS them in the proto style +// Example: ResourceType_UNSPECIFIED +func convertEnumValues(n string, s []string) []string { + u := strcase.ToSNAKE(n) + x := []string{u + "_UNSPECIFIED"} + for _, z := range s { + y := strcase.ToSNAKE(z) + x = append(x, (u + "_" + y)) + } + return x +} + +type ReqResp struct { + OneOf *[]string + ReqFields map[string]string + RespEmpty map[string]bool + a *Annotations +} + +type Service struct { + Path *url.URL + Name string + a *Annotations +} + +type httpOptions struct { + Method string + Body string + Path *url.URL +} + +func parseDynamicPath(path string) []string { + // Split both the template and path into segments + pathParts := strings.Split(path, "/") + + // Create a map to store parameter values + var params []string + + // Regex to identify path parameters (enclosed in { }) + re := regexp.MustCompile(`^{([^}]+)}$`) + + for _, pathPart := range pathParts { + if matches := re.FindStringSubmatch(pathPart); len(matches) > 0 { + // If the path segment is a parameter (e.g., {org}), extract the name + paramName := matches[1] + params = append(params, paramName) + } + } + + return params +} + +const ( + wkprefix = "google.protobuf." + + // proto represents builtin types + protoDouble = "double" + protoFloat = "float" + protoInt32 = "int32" + protoInt64 = "int64" + protoUint32 = "uint32" + protoUint64 = "uint64" + protoSint32 = "sint32" + protoSint64 = "sint64" + protoFixed32 = "fixed32" + protoFixed64 = "fixed64" + protoSFixed32 = "sfixed32" + protoSFixed64 = "sfixed64" + protoBool = "bool" + protoString = "string" + protoBytes = "bytes" + + // well known represents google.proto. well known types + WellKnownAny = wkprefix + "Any" + WellKnownBoolValue = wkprefix + "BoolValue" + WellKnownBytesValue = wkprefix + "BytesValue" + WellKnownDecimal = wkprefix + "Decimal" + WellKnownDoubleValue = wkprefix + "DoubleValue" + WellKnownDuration = wkprefix + "Duration" + WellKnownEmpty = wkprefix + "Empty" + WellKnownEnum = wkprefix + "Enum" + WellKnownEnumValue = wkprefix + "EnumValue" + WellKnownField = wkprefix + "Field" + WellKnownFieldCardinality = wkprefix + "Field.Cardinality" + WellKnownFieldKind = wkprefix + "Field.Kind" + WellKnownFieldMask = wkprefix + "FieldMask" + WellKnownFloatValue = wkprefix + "FloatValue" + WellKnownInt32Value = wkprefix + "Int32Value" + WellKnownInt64Value = wkprefix + "Int64Value" + WellKnownListValue = wkprefix + "ListValue" + WellKnownMethod = wkprefix + "Method" + WellKnownMixin = wkprefix + "Mixin" + WellKnownMoney = wkprefix + "Money" + WellKnownNullValue = wkprefix + "NullValue" + WellKnownOption = wkprefix + "Option" + WellKnownSourceContext = wkprefix + "SourceContext" + WellKnownStringValue = wkprefix + "StringValue" + WellKnownStruct = wkprefix + "Struct" + WellKnownSyntax = wkprefix + "Syntax" + WellKnownTimestamp = wkprefix + "Timestamp" + WellKnownType = wkprefix + "Type" + WellKnownUInt32Value = wkprefix + "UInt32Value" + WellKnownUInt64Value = wkprefix + "UInt64Value" +) From a3309e3d3c7b6ee4c6d167bd86239542bac2a521 Mon Sep 17 00:00:00 2001 From: Bruce Smith Date: Wed, 18 Sep 2024 19:27:11 -0400 Subject: [PATCH 7/7] typo --- cmd/sqlc-gen-proto/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/sqlc-gen-proto/main.go b/cmd/sqlc-gen-proto/main.go index b3c341ec99..d5fa3932c0 100644 --- a/cmd/sqlc-gen-proto/main.go +++ b/cmd/sqlc-gen-proto/main.go @@ -52,7 +52,7 @@ type options struct { OutDir string `json:"out_dir,omitempty" yaml:"out_dir"` UserDefinedDir string `json:"user_defined_dir,omitempty" yaml:"user_defined_dir"` OneOfID string `json:"one_of_id,omitempty" yaml:"one_of_id"` - DefaultPackage string `json:"defaut_package,omitempty" yaml:"defaut_package"` + DefaultPackage string `json:"default_package,omitempty" yaml:"default_package"` } func getGenRequest() (*plugin.GenerateRequest, error) {