|
5 | 5 | "io" |
6 | 6 | "io/ioutil" |
7 | 7 | "path/filepath" |
8 | | - "strings" |
9 | 8 |
|
10 | 9 | "github.com/davecgh/go-spew/spew" |
11 | 10 | "github.com/kyleconroy/sqlc/internal/dinosql" |
@@ -153,10 +152,14 @@ func (q *Query) parseNameAndCmd() error { |
153 | 152 | return fmt.Errorf("cannot parse name and cmd from null query") |
154 | 153 | } |
155 | 154 | _, comments := sqlparser.SplitMarginComments(q.SQL) |
156 | | - err := q.parseLeadingComment(comments.Leading) |
| 155 | + name, cmd, err := dinosql.ParseMetadata(comments.Leading, dinosql.CommentSyntaxStar) |
157 | 156 | if err != nil { |
158 | | - return fmt.Errorf("failed to parse leading comment %w", err) |
| 157 | + return err |
| 158 | + } else if name == "" || cmd == "" { |
| 159 | + return fmt.Errorf("failed to parse query leading comment") |
159 | 160 | } |
| 161 | + q.Name = name |
| 162 | + q.Cmd = cmd |
160 | 163 | return nil |
161 | 164 | } |
162 | 165 |
|
@@ -257,7 +260,7 @@ func parseFrom(from sqlparser.TableExprs, isLeftJoined bool) (FromTables, string |
257 | 260 | } |
258 | 261 | return right, leftMostTableName, nil |
259 | 262 | default: |
260 | | - return nil, "", fmt.Errorf("failed to parse table expr: %v", spew.Sdump(v)) |
| 263 | + return nil, "", fmt.Errorf("failed to parse table expr: %v", spew.Sdump(v)) |
261 | 264 | } |
262 | 265 | } |
263 | 266 | return tables, defaultTableName, nil |
@@ -303,7 +306,10 @@ func parseUpdate(node *sqlparser.Update, query string, s *Schema, settings dinos |
303 | 306 | DefaultTableName: defaultTable, |
304 | 307 | SchemaLookup: s, |
305 | 308 | } |
306 | | - parsedQuery.parseNameAndCmd() |
| 309 | + err = parsedQuery.parseNameAndCmd() |
| 310 | + if err != nil { |
| 311 | + return nil, err |
| 312 | + } |
307 | 313 |
|
308 | 314 | return &parsedQuery, nil |
309 | 315 | } |
@@ -356,7 +362,11 @@ func parseInsert(node *sqlparser.Insert, query string, s *Schema, settings dinos |
356 | 362 | DefaultTableName: tableName, |
357 | 363 | SchemaLookup: s, |
358 | 364 | } |
359 | | - parsedQuery.parseNameAndCmd() |
| 365 | + |
| 366 | + err := parsedQuery.parseNameAndCmd() |
| 367 | + if err != nil { |
| 368 | + return nil, err |
| 369 | + } |
360 | 370 | return parsedQuery, nil |
361 | 371 | } |
362 | 372 |
|
@@ -390,34 +400,6 @@ func parseDelete(node *sqlparser.Delete, query string, s *Schema, settings dinos |
390 | 400 | return parsedQuery, nil |
391 | 401 | } |
392 | 402 |
|
393 | | -func (q *Query) parseLeadingComment(comment string) error { |
394 | | - for _, line := range strings.Split(comment, "\n") { |
395 | | - if !strings.HasPrefix(line, "/* name:") { |
396 | | - continue |
397 | | - } |
398 | | - part := strings.Split(strings.TrimSpace(line), " ") |
399 | | - if len(part) == 3 { |
400 | | - return fmt.Errorf("missing query type [':one', ':many', ':exec', ':execrows']: %s", line) |
401 | | - } |
402 | | - if len(part) != 5 { |
403 | | - return fmt.Errorf("invalid query comment: %s", line) |
404 | | - } |
405 | | - queryName := part[2] |
406 | | - queryType := strings.TrimSpace(part[3]) |
407 | | - switch queryType { |
408 | | - case ":one", ":many", ":exec", ":execrows": |
409 | | - default: |
410 | | - return fmt.Errorf("invalid query type: %s", queryType) |
411 | | - } |
412 | | - // if err := validateQueryName(queryName); err != nil { |
413 | | - // return err |
414 | | - // } |
415 | | - q.Name = queryName |
416 | | - q.Cmd = queryType |
417 | | - } |
418 | | - return nil |
419 | | -} |
420 | | - |
421 | 403 | func parseSelectAliasExpr(exprs sqlparser.SelectExprs, s *Schema, tableAliasMap FromTables, defaultTable string) ([]Column, error) { |
422 | 404 | cols := []Column{} |
423 | 405 | for _, col := range exprs { |
|
0 commit comments