Skip to content

Commit 54207db

Browse files
committed
only allow alias or table name in sqlc.embed()
1 parent f86fb4c commit 54207db

File tree

2 files changed

+7
-43
lines changed

2 files changed

+7
-43
lines changed

internal/sql/rewrite/embeds.go

Lines changed: 7 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package rewrite
22

33
import (
44
"fmt"
5-
"strings"
65

76
"github.com/kyleconroy/sqlc/internal/sql/ast"
87
"github.com/kyleconroy/sqlc/internal/sql/astutils"
@@ -50,29 +49,20 @@ func Embeds(raw *ast.RawStmt) (*ast.RawStmt, EmbedSet) {
5049
return false
5150
}
5251

53-
sw := &stringWalker{}
54-
astutils.Walk(sw, fun.Args)
55-
str := strings.Join(sw.Parts, ".")
56-
57-
tableName, err := parseTable(sw)
58-
if err != nil {
59-
return false
60-
}
52+
param, _ := flatten(fun.Args)
6153

6254
node := &ast.ColumnRef{
6355
Fields: &ast.List{
64-
Items: []ast.Node{},
56+
Items: []ast.Node{
57+
&ast.String{Str: param},
58+
&ast.A_Star{},
59+
},
6560
},
6661
}
6762

68-
for _, s := range sw.Parts {
69-
node.Fields.Items = append(node.Fields.Items, &ast.String{Str: s})
70-
}
71-
node.Fields.Items = append(node.Fields.Items, &ast.A_Star{})
72-
7363
embeds = append(embeds, &Embed{
74-
Table: tableName,
75-
param: str,
64+
Table: &ast.TableName{Name: param},
65+
param: param,
7666
Node: node,
7767
})
7868

@@ -99,27 +89,3 @@ func isEmbed(node ast.Node) bool {
9989
isValid := call.Func.Schema == "sqlc" && call.Func.Name == "embed"
10090
return isValid
10191
}
102-
103-
func parseTable(sw *stringWalker) (*ast.TableName, error) {
104-
parts := sw.Parts
105-
106-
switch len(parts) {
107-
case 1:
108-
return &ast.TableName{
109-
Name: parts[0],
110-
}, nil
111-
case 2:
112-
return &ast.TableName{
113-
Schema: parts[0],
114-
Name: parts[1],
115-
}, nil
116-
case 3:
117-
return &ast.TableName{
118-
Catalog: parts[0],
119-
Schema: parts[1],
120-
Name: parts[2],
121-
}, nil
122-
default:
123-
return nil, fmt.Errorf("invalid table name")
124-
}
125-
}

internal/sql/rewrite/parameters.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ func flatten(root ast.Node) (string, bool) {
1919

2020
type stringWalker struct {
2121
String string
22-
Parts []string
2322
IsConst bool
2423
}
2524

@@ -29,7 +28,6 @@ func (s *stringWalker) Visit(node ast.Node) astutils.Visitor {
2928
}
3029
if n, ok := node.(*ast.String); ok {
3130
s.String += n.Str
32-
s.Parts = append(s.Parts, n.Str)
3331
}
3432
return s
3533
}

0 commit comments

Comments
 (0)