MCPcopy
hub / github.com/sqlc-dev/sqlc / rewriteTargetList

Function rewriteTargetList

internal/x/expander/expander.go:413–488  ·  view source on GitHub ↗

rewriteTargetList replaces * in a target list with explicit column references

(targets *ast.List, columns []string)

Source from the content-addressed store, hash-verified

411
412// rewriteTargetList replaces * in a target list with explicit column references
413func rewriteTargetList(targets *ast.List, columns []string) *ast.List {
414 if targets == nil {
415 return nil
416 }
417
418 starCount := countStarsInList(targets)
419 nonStarCount := countNonStarsInList(targets)
420
421 // Calculate how many columns each * expands to
422 // Total columns = (columns per star * number of stars) + non-star columns
423 // So: columns per star = (total - non-star) / stars
424 columnsPerStar := 0
425 if starCount > 0 {
426 columnsPerStar = (len(columns) - nonStarCount) / starCount
427 }
428
429 newItems := make([]ast.Node, 0, len(columns))
430 colIndex := 0
431
432 for _, target := range targets.Items {
433 resTarget, ok := target.(*ast.ResTarget)
434 if !ok {
435 newItems = append(newItems, target)
436 colIndex++
437 continue
438 }
439
440 if resTarget.Val == nil {
441 newItems = append(newItems, target)
442 colIndex++
443 continue
444 }
445
446 colRef, ok := resTarget.Val.(*ast.ColumnRef)
447 if !ok {
448 newItems = append(newItems, target)
449 colIndex++
450 continue
451 }
452
453 if colRef.Fields == nil {
454 newItems = append(newItems, target)
455 colIndex++
456 continue
457 }
458
459 // Check if this is a * (with or without table qualifier)
460 // and extract any table prefix
461 isStar := false
462 var tablePrefix []string
463 for _, field := range colRef.Fields.Items {
464 if _, ok := field.(*ast.A_Star); ok {
465 isStar = true
466 break
467 }
468 // Collect prefix parts (schema, table name)
469 if str, ok := field.(*ast.String); ok {
470 tablePrefix = append(tablePrefix, str.Str)

Callers 4

expandSelectStmtMethod · 0.85
expandInsertStmtMethod · 0.85
expandUpdateStmtMethod · 0.85
expandDeleteStmtMethod · 0.85

Calls 3

countStarsInListFunction · 0.85
countNonStarsInListFunction · 0.85

Tested by

no test coverage detected