Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape column names using Dialect().Quote() #63

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions internal/queryset/generator/queryset.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ func genStructFieldInfos(s parser.ParsedStruct, types *types.Package) (ret []fie
return ret
}

func generateQuerySetConfigs(types *types.Package,
structs map[string]parser.ParsedStruct) querySetStructConfigSlice {
func generateQuerySetConfigs(
types *types.Package,
structs map[string]parser.ParsedStruct,
) querySetStructConfigSlice {

querySetStructConfigs := querySetStructConfigSlice{}

Expand Down Expand Up @@ -103,7 +105,10 @@ func generateQuerySetConfigs(types *types.Package,

// GenerateQuerySetsForStructs is an internal method to retrieve querysets
// generated code from parsed structs
func GenerateQuerySetsForStructs(types *types.Package, structs map[string]parser.ParsedStruct) (io.Reader, error) {
func GenerateQuerySetsForStructs(
types *types.Package,
structs map[string]parser.ParsedStruct,
) (io.Reader, error) {
querySetStructConfigs := generateQuerySetConfigs(types, structs)
if len(querySetStructConfigs) == 0 {
return nil, nil
Expand Down
22 changes: 11 additions & 11 deletions internal/queryset/generator/queryset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func testUserSelectWithSurnameFilter(t *testing.T, m sqlmock.Sqlmock, db *gorm.D
expUsers[0].Surname = &surname

req := "SELECT * FROM `users` " +
"WHERE `users`.`deleted_at` IS NULL AND ((user_surname = ?)) ORDER BY `users`.`id` ASC LIMIT 1"
"WHERE `users`.`deleted_at` IS NULL AND ((`user_surname` = ?)) ORDER BY `users`.`id` ASC LIMIT 1"
m.ExpectQuery(fixedFullRe(req)).
WillReturnRows(getRowsForUsers(expUsers))

Expand All @@ -241,28 +241,28 @@ type userQueryTestCase struct {
func testUserQueryFilters(t *testing.T, m sqlmock.Sqlmock, db *gorm.DB) {
cases := []userQueryTestCase{
{
q: "((name IN (?)))",
q: "((`name` IN (?)))",
args: []driver.Value{"a"},
qs: func(qs test.UserQuerySet) test.UserQuerySet {
return qs.NameIn("a")
},
},
{
q: "((name IN (?,?)))",
q: "((`name` IN (?,?)))",
args: []driver.Value{"a", "b"},
qs: func(qs test.UserQuerySet) test.UserQuerySet {
return qs.NameIn("a", "b")
},
},
{
q: "((name NOT IN (?)))",
q: "((`name` NOT IN (?)))",
args: []driver.Value{"a"},
qs: func(qs test.UserQuerySet) test.UserQuerySet {
return qs.NameNotIn("a")
},
},
{
q: "((name NOT IN (?,?)))",
q: "((`name` NOT IN (?,?)))",
args: []driver.Value{"a", "b"},
qs: func(qs test.UserQuerySet) test.UserQuerySet {
return qs.NameNotIn("a", "b")
Expand Down Expand Up @@ -321,7 +321,7 @@ func testUserCreateOneWithSurname(t *testing.T, m sqlmock.Sqlmock, db *gorm.DB)

func testUserUpdateByEmail(t *testing.T, m sqlmock.Sqlmock, db *gorm.DB) {
u := getUser()
req := "UPDATE `users` SET `name` = ? WHERE `users`.`deleted_at` IS NULL AND ((email = ?))"
req := "UPDATE `users` SET `name` = ? WHERE `users`.`deleted_at` IS NULL AND ((`email` = ?))"
m.ExpectExec(fixedFullRe(req)).
WithArgs(u.Name, u.Email).
WillReturnResult(sqlmock.NewResult(0, 1))
Expand All @@ -346,7 +346,7 @@ func testUserUpdateFieldsByPK(t *testing.T, m sqlmock.Sqlmock, db *gorm.DB) {

func testUserDeleteByEmail(t *testing.T, m sqlmock.Sqlmock, db *gorm.DB) {
u := getUser()
req := "UPDATE `users` SET `deleted_at`=? WHERE `users`.`deleted_at` IS NULL AND ((email = ?))"
req := "UPDATE `users` SET `deleted_at`=? WHERE `users`.`deleted_at` IS NULL AND ((`email` = ?))"
m.ExpectExec(fixedFullRe(req)).
WithArgs(sqlmock.AnyArg(), u.Email).
WillReturnResult(sqlmock.NewResult(0, 1))
Expand All @@ -370,7 +370,7 @@ func testUserDeleteByPK(t *testing.T, m sqlmock.Sqlmock, db *gorm.DB) {
func testUsersDeleteNum(t *testing.T, m sqlmock.Sqlmock, db *gorm.DB) {
usersNum := 2
users := getTestUsers(usersNum)
req := "UPDATE `users` SET `deleted_at`=? WHERE `users`.`deleted_at` IS NULL AND ((email IN (?,?)))"
req := "UPDATE `users` SET `deleted_at`=? WHERE `users`.`deleted_at` IS NULL AND ((`email` IN (?,?)))"
m.ExpectExec(fixedFullRe(req)).
WithArgs(sqlmock.AnyArg(), users[0].Email, users[1].Email).
WillReturnResult(sqlmock.NewResult(0, int64(usersNum)))
Expand All @@ -385,7 +385,7 @@ func testUsersDeleteNum(t *testing.T, m sqlmock.Sqlmock, db *gorm.DB) {
func testUsersDeleteNumUnscoped(t *testing.T, m sqlmock.Sqlmock, db *gorm.DB) {
usersNum := 2
users := getTestUsers(usersNum)
req := "DELETE FROM `users` WHERE (email IN (?,?))"
req := "DELETE FROM `users` WHERE (`email` IN (?,?))"
m.ExpectExec(fixedFullRe(req)).
WithArgs(users[0].Email, users[1].Email).
WillReturnResult(sqlmock.NewResult(0, int64(usersNum)))
Expand All @@ -400,7 +400,7 @@ func testUsersDeleteNumUnscoped(t *testing.T, m sqlmock.Sqlmock, db *gorm.DB) {
func testUsersUpdateNum(t *testing.T, m sqlmock.Sqlmock, db *gorm.DB) {
usersNum := 2
users := getTestUsers(usersNum)
req := "UPDATE `users` SET `name` = ? WHERE `users`.`deleted_at` IS NULL AND ((email IN (?,?)))"
req := "UPDATE `users` SET `name` = ? WHERE `users`.`deleted_at` IS NULL AND ((`email` IN (?,?)))"
m.ExpectExec(fixedFullRe(req)).
WithArgs(sqlmock.AnyArg(), users[0].Email, users[1].Email).
WillReturnResult(sqlmock.NewResult(0, int64(usersNum)))
Expand All @@ -416,7 +416,7 @@ func testUsersUpdateNum(t *testing.T, m sqlmock.Sqlmock, db *gorm.DB) {

func testUsersCount(t *testing.T, m sqlmock.Sqlmock, db *gorm.DB) {
expCount := 5
req := "SELECT count(*) FROM `users` WHERE `users`.`deleted_at` IS NULL AND ((name != ?))"
req := "SELECT count(*) FROM `users` WHERE `users`.`deleted_at` IS NULL AND ((`name` != ?))"
m.ExpectQuery(fixedFullRe(req)).WithArgs(driver.Value("")).
WillReturnRows(getRowWithFields([]driver.Value{expCount}))

Expand Down
Loading