Quote filter fields

This commit is contained in:
Jamie Curnow 2024-09-11 15:22:10 +10:00
parent 9a2e5c92d5
commit e78dd069f1
No known key found for this signature in database
GPG Key ID: FFBB624C43388E9E
6 changed files with 12 additions and 11 deletions

View File

@ -122,12 +122,12 @@ func (s *testsuite) TestList() {
defer goleak.VerifyNone(s.T(), goleak.IgnoreAnyFunction("database/sql.(*DB).connectionOpener"))
s.mock.
ExpectQuery(regexp.QuoteMeta(`SELECT count(*) FROM "certificate_authority" WHERE name LIKE $1 AND "certificate_authority"."is_deleted" = $2`)).
ExpectQuery(regexp.QuoteMeta("SELECT count(*) FROM \"certificate_authority\" WHERE `certificate_authority`.`name` LIKE $1 AND \"certificate_authority\".\"is_deleted\" = $2")).
WithArgs("%test%", 0).
WillReturnRows(s.listCountRows)
s.mock.
ExpectQuery(regexp.QuoteMeta(`SELECT * FROM "certificate_authority" WHERE name LIKE $1 AND "certificate_authority"."is_deleted" = $2 ORDER BY name asc LIMIT $3`)).
ExpectQuery(regexp.QuoteMeta("SELECT * FROM \"certificate_authority\" WHERE `certificate_authority`.`name` LIKE $1 AND \"certificate_authority\".\"is_deleted\" = $2 ORDER BY name asc LIMIT $3")).
WithArgs("%test%", 0, 8).
WillReturnRows(s.listRows)

View File

@ -204,12 +204,12 @@ func (s *testsuite) TestList() {
defer goleak.VerifyNone(s.T(), goleak.IgnoreAnyFunction("database/sql.(*DB).connectionOpener"))
s.mock.
ExpectQuery(regexp.QuoteMeta(`SELECT count(*) FROM "dns_provider" WHERE acmesh_name LIKE $1 AND "dns_provider"."is_deleted" = $2`)).
ExpectQuery(regexp.QuoteMeta("SELECT count(*) FROM \"dns_provider\" WHERE `dns_provider`.`acmesh_name` LIKE $1 AND \"dns_provider\".\"is_deleted\" = $2")).
WithArgs("dns%", 0).
WillReturnRows(s.listCountRows)
s.mock.
ExpectQuery(regexp.QuoteMeta(`SELECT * FROM "dns_provider" WHERE acmesh_name LIKE $1 AND "dns_provider"."is_deleted" = $2 ORDER BY name asc LIMIT $3`)).
ExpectQuery(regexp.QuoteMeta("SELECT * FROM \"dns_provider\" WHERE `dns_provider`.`acmesh_name` LIKE $1 AND \"dns_provider\".\"is_deleted\" = $2 ORDER BY name asc LIMIT $3")).
WithArgs("dns%", 0, 8).
WillReturnRows(s.listRows)

View File

@ -307,12 +307,12 @@ func (s *testsuite) TestList() {
defer goleak.VerifyNone(s.T(), goleak.IgnoreAnyFunction("database/sql.(*DB).connectionOpener"))
s.mock.
ExpectQuery(regexp.QuoteMeta(`SELECT count(*) FROM "user" WHERE name LIKE $1 AND "user"."is_deleted" = $2`)).
ExpectQuery(regexp.QuoteMeta("SELECT count(*) FROM \"user\" WHERE `user`.`name` LIKE $1 AND \"user\".\"is_deleted\" = $2")).
WithArgs("%jon%", 0).
WillReturnRows(s.listCountRows)
s.mock.
ExpectQuery(regexp.QuoteMeta(`SELECT * FROM "user" WHERE name LIKE $1 AND "user"."is_deleted" = $2 ORDER BY name asc LIMIT $3`)).
ExpectQuery(regexp.QuoteMeta("SELECT * FROM \"user\" WHERE `user`.`name` LIKE $1 AND \"user\".\"is_deleted\" = $2 ORDER BY name asc LIMIT $3")).
WithArgs("%jon%", 0, 8).
WillReturnRows(s.listRows)

View File

@ -86,7 +86,6 @@ func List(pageInfo model.PageInfo, filters []model.Filter, expand []string) (ent
// DeleteAll will do just that, and should only be used for testing purposes.
func DeleteAll() error {
db := database.GetDB()
// nolint errcheck
result := db.Exec(fmt.Sprintf(`DELETE FROM %s WHERE is_system = ?`, database.QuoteTableName("user")), false)
return result.Error
}

View File

@ -6,6 +6,7 @@ import (
"regexp"
"strings"
"npm/internal/database"
"npm/internal/logger"
"npm/internal/model"
"npm/internal/util"
@ -33,8 +34,8 @@ func GetFilterMap(m interface{}, globalTablePrefix string) map[string]model.Filt
n := tableNameFunc.Func.Call([]reflect.Value{v})
if len(n) > 0 {
globalTablePrefix = fmt.Sprintf(
`"%s".`,
n[0].String(),
`%s.`,
database.QuoteTableName(n[0].String()),
)
}
}
@ -82,10 +83,10 @@ func GetFilterMap(m interface{}, globalTablePrefix string) map[string]model.Filt
}
// db can have many parts, we need to pull out the "column:value" part
f.Field = field.Name
f.Field = database.QuoteTableName(field.Name)
r := regexp.MustCompile(`(?:^|;)column:([^;|$]+)(?:$|;)`)
if matches := r.FindStringSubmatch(dbTag); len(matches) > 1 {
f.Field = fmt.Sprintf("%s%s", tablePrefix, matches[1])
f.Field = fmt.Sprintf("%s%s", tablePrefix, database.QuoteTableName(matches[1]))
}
}
filterMap[parts[0]] = f

View File

@ -14,6 +14,7 @@ if [[ -n "$INCOMPLETE_COMMENTS" ]]; then
# RESULT=1
fi
echo -e "${YELLOW}golangci-lint ...${RESET}"
if ! golangci-lint run -E goimports ./...; then
exit 1
fi