Skip to content

Commit

Permalink
Fix/rawquery req params (#299)
Browse files Browse the repository at this point in the history
* internal/parser: add fullname to fix MySQL ambiguous cols

* internal/parse/x: adds parser tests
  • Loading branch information
scbizu authored Aug 7, 2023
1 parent 26be1c7 commit 8c5c671
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 25 deletions.
4 changes: 2 additions & 2 deletions e2e/mysql/gen_methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (req *GetUserReq) Params() []any {
func (req *GetUserReq) Condition() string {
var conditions []string
if req.Name != "" {
conditions = append(conditions, "name = ?")
conditions = append(conditions, "`name` = ?")
}
var query string
if len(conditions) > 0 {
Expand Down Expand Up @@ -200,7 +200,7 @@ func (req *UserJoinBlogReq) Params() []any {
func (req *UserJoinBlogReq) Condition() string {
var conditions []string
if req.Name != "" {
conditions = append(conditions, "name = ?")
conditions = append(conditions, "`u`.`name` = ?")
}
var query string
if len(conditions) > 0 {
Expand Down
8 changes: 4 additions & 4 deletions e2e/mysqlr/gen_methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (req *BlogReq) Params() []any {
func (req *BlogReq) Condition() string {
var conditions []string
if req.Id != 0 {
conditions = append(conditions, "id = ?")
conditions = append(conditions, "`b`.`id` = ?")
}
var query string
if len(conditions) > 0 {
Expand All @@ -81,7 +81,7 @@ func (req *BlogReq) Condition() string {
return query
}

const _BlogSQL = "SELECT `id`,`status` FROM `blogs` %s"
const _BlogSQL = "SELECT `b`.`id`,`b`.`status` FROM `blogs` AS `b` %s"

// Blog is a raw query handler generated function for `e2e/mysqlr/sqls/blog.sql`.
func (m *sqlMethods) Blog(ctx context.Context, req *BlogReq, opts ...RawQueryOptionHandler) ([]*BlogResp, error) {
Expand Down Expand Up @@ -133,7 +133,7 @@ func (req *BlogAggrReq) Params() []any {
func (req *BlogAggrReq) Condition() string {
var conditions []string
if req.Id != 0 {
conditions = append(conditions, "id = ?")
conditions = append(conditions, "`id` = ?")
}
var query string
if len(conditions) > 0 {
Expand Down Expand Up @@ -195,7 +195,7 @@ func (req *BlogFuncReq) Params() []any {
func (req *BlogFuncReq) Condition() string {
var conditions []string
if req.Id != 0 {
conditions = append(conditions, "id = ?")
conditions = append(conditions, "`id` = ?")
}
var query string
if len(conditions) > 0 {
Expand Down
8 changes: 4 additions & 4 deletions e2e/mysqlr/sqls/blog.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
SELECT
id ,
status
b.id,
b.status
FROM
blogs
blogs AS b
WHERE
id = 1
b.id = 1
LIMIT 1,2;
4 changes: 2 additions & 2 deletions internal/parser/shared/tpl/sql_method.gogo
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ func (req *{{$method.Name}}Req) Condition() string {
}
{{- else if (eq $field.Type "int64")}}
if req.{{$field.Name}} != 0 {
conditions = append(conditions, "{{$field.Raw}} = ?")
conditions = append(conditions, "{{$field.FullName}} = ?")
}
{{- else if (eq $field.Type "string")}}
if req.{{$field.Name}} != "" {
conditions = append(conditions, "{{$field.Raw}} = ?")
conditions = append(conditions, "{{$field.FullName}} = ?")
}
{{- end }}
{{- end }}
Expand Down
14 changes: 8 additions & 6 deletions internal/parser/x/query/sql_method.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ type SQLMethod struct {
}

type SQLMethodField struct {
Name string
Raw string
Type string
Name string
Raw string
Type string
FullName string
}

func NewSQL(objs map[string]generator.IObject) *SQL {
Expand Down Expand Up @@ -124,9 +125,10 @@ func (p *SQL) Read(path string) (*SQLMethod, error) {
}
default:
result.Fields = append(result.Fields, &SQLMethodField{
Name: strcase.ToCamel(name),
Raw: name,
Type: c.Type.String(),
FullName: strings.Split(c.Name, ":")[1],
Name: strcase.ToCamel(name),
Raw: name,
Type: c.Type.String(),
})
}
}
Expand Down
14 changes: 7 additions & 7 deletions internal/parser/x/query/tidb_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ LIMIT 0,10

const queryIn = `
SELECT
id
u.id
FROM
user
WHERE name IN ('me','him')
user AS u
WHERE u.name IN ('me','him')
`

const queryOneLimit = `
Expand Down Expand Up @@ -85,7 +85,7 @@ func TestTiDBParserParseQuery(t *testing.T) {
expect string
}{
{"query", query, "SELECT `id` FROM `user` %s"},
{"queryIn", queryIn, "SELECT `id` FROM `user` %s"},
{"queryIn", queryIn, "SELECT `u`.`id` FROM `user` AS `u` %s"},
{"queryOneLimit", queryOneLimit, "SELECT `id` FROM `user` %s"},
{"queryNoLimit", queryNoLimit, "SELECT `id` FROM `user` %s"},
{"queryWithTableJoin", queryWithTableJoin, "SELECT `u`.`id`,`b`.`id` FROM `user` AS `u` JOIN `blog` AS `b` ON `u`.`id`=`b`.`user_id` %s"},
Expand Down Expand Up @@ -124,12 +124,12 @@ func TestTiDBParserParseMetadata(t *testing.T) {
},
},
{"queryIn", queryIn, map[Table]*QueryMetadata{
{Name: "user"}: {
{Name: "user", Alias: "u"}: {
params: []*QueryField{
{Name: "col:`name`", Type: T_ARRAY_STRING},
{Name: "col:`u`.`name`", Type: T_ARRAY_STRING},
},
result: []*QueryField{
{Name: "`id`", Type: T_PLACEHOLDER},
{Name: "`u`.`id`", Type: T_PLACEHOLDER},
},
},
}},
Expand Down

0 comments on commit 8c5c671

Please sign in to comment.