@@ -9,13 +9,13 @@ import (
99 "sort"
1010 "strconv"
1111 "strings"
12- "unicode"
1312
1413 "github.com/davecgh/go-spew/spew"
1514 pg "github.com/lfittl/pg_query_go"
1615 nodes "github.com/lfittl/pg_query_go/nodes"
1716
1817 "github.com/kyleconroy/sqlc/internal/catalog"
18+ "github.com/kyleconroy/sqlc/internal/metadata"
1919 "github.com/kyleconroy/sqlc/internal/migrations"
2020 "github.com/kyleconroy/sqlc/internal/multierr"
2121 core "github.com/kyleconroy/sqlc/internal/pg"
@@ -210,67 +210,6 @@ func rangeVars(root nodes.Node) []nodes.RangeVar {
210210 return vars
211211}
212212
213- // A query name must be a valid Go identifier
214- //
215- // https://golang.org/ref/spec#Identifiers
216- func validateQueryName (name string ) error {
217- if len (name ) == 0 {
218- return fmt .Errorf ("invalid query name: %q" , name )
219- }
220- for i , c := range name {
221- isLetter := unicode .IsLetter (c ) || c == '_'
222- isDigit := unicode .IsDigit (c )
223- if i == 0 && ! isLetter {
224- return fmt .Errorf ("invalid query name %q" , name )
225- } else if ! (isLetter || isDigit ) {
226- return fmt .Errorf ("invalid query name %q" , name )
227- }
228- }
229- return nil
230- }
231-
232- type CommentSyntax int
233-
234- const (
235- CommentSyntaxDash CommentSyntax = iota
236- CommentSyntaxStar // Note: this is the only style supported by the MySQL sqlparser
237- CommentSyntaxHash
238- )
239-
240- func ParseMetadata (t string , commentStyle CommentSyntax ) (string , string , error ) {
241- for _ , line := range strings .Split (t , "\n " ) {
242- if commentStyle == CommentSyntaxDash && ! strings .HasPrefix (line , "-- name:" ) {
243- continue
244- }
245- if commentStyle == CommentSyntaxStar && ! strings .HasPrefix (line , "/* name:" ) {
246- continue
247- }
248- part := strings .Split (strings .TrimSpace (line ), " " )
249-
250- if commentStyle == CommentSyntaxStar {
251- part = part [:len (part )- 1 ] // removes the trailing "*/" element
252- }
253- if len (part ) == 2 {
254- return "" , "" , fmt .Errorf ("missing query type [':one', ':many', ':exec', ':execrows']: %s" , line )
255- }
256- if len (part ) != 4 {
257- return "" , "" , fmt .Errorf ("invalid query comment: %s" , line )
258- }
259- queryName := part [2 ]
260- queryType := strings .TrimSpace (part [3 ])
261- switch queryType {
262- case ":one" , ":many" , ":exec" , ":execrows" :
263- default :
264- return "" , "" , fmt .Errorf ("invalid query type: %s" , queryType )
265- }
266- if err := validateQueryName (queryName ); err != nil {
267- return "" , "" , err
268- }
269- return queryName , queryType , nil
270- }
271- return "" , "" , nil
272- }
273-
274213func validateCmd (n nodes.Node , name , cmd string ) error {
275214 // TODO: Convert cmd to an enum
276215 if ! (cmd == ":many" || cmd == ":one" ) {
@@ -331,7 +270,7 @@ func parseQuery(c core.Catalog, stmt nodes.Node, src string, rewriteParameters b
331270 if err := validate .FuncCall (& c , raw ); err != nil {
332271 return nil , err
333272 }
334- name , cmd , err := ParseMetadata (strings .TrimSpace (rawSQL ), CommentSyntaxDash )
273+ name , cmd , err := metadata . Parse (strings .TrimSpace (rawSQL ), metadata . CommentSyntaxDash )
335274 if err != nil {
336275 return nil , err
337276 }
0 commit comments