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

Function Embeds

internal/sql/rewrite/embeds.go:38–77  ·  view source on GitHub ↗

Embeds rewrites `sqlc.embed(param)` to a `ast.ColumnRef` of form `param.*`. The compiler can make use of the returned `EmbedSet` while expanding the `param.*` column refs to produce the correct source edits.

(raw *ast.RawStmt)

Source from the content-addressed store, hash-verified

36// The compiler can make use of the returned `EmbedSet` while expanding the
37// `param.*` column refs to produce the correct source edits.
38func Embeds(raw *ast.RawStmt) (*ast.RawStmt, EmbedSet) {
39 var embeds []*Embed
40
41 node := astutils.Apply(raw, func(cr *astutils.Cursor) bool {
42 node := cr.Node()
43
44 switch {
45 case isEmbed(node):
46 fun := node.(*ast.FuncCall)
47
48 if len(fun.Args.Items) == 0 {
49 return false
50 }
51
52 param, _ := flatten(fun.Args)
53
54 node := &ast.ColumnRef{
55 Fields: &ast.List{
56 Items: []ast.Node{
57 &ast.String{Str: param},
58 &ast.A_Star{},
59 },
60 },
61 }
62
63 embeds = append(embeds, &Embed{
64 Table: &ast.TableName{Name: param},
65 param: param,
66 Node: node,
67 })
68
69 cr.Replace(node)
70 return false
71 default:
72 return true
73 }
74 }, nil)
75
76 return node.(*ast.RawStmt), embeds
77}
78
79func isEmbed(node ast.Node) bool {
80 call, ok := node.(*ast.FuncCall)

Callers 1

_analyzeQueryMethod · 0.92

Calls 5

ApplyFunction · 0.92
isEmbedFunction · 0.85
flattenFunction · 0.85
NodeMethod · 0.80
ReplaceMethod · 0.45

Tested by

no test coverage detected