Pass BOOL values as real types (int/bool) instead of strings to SQL parameters. (#101961)
* Pass BOOL values as real types (int/bool) instead of strings to SQL parameters.
Fixes following integration tests when running with Spanner:
* TestIntegrationDataAccess
* GetDataSourcesByType/Get_prunable_data_sources
* TestIntegrationUserAuthToken:
* expires_correctly
* can_properly_rotate_tokens
* keeps_prev_token_valid_for_1_minute_after_it_is_confirmed
* Fix more places where "true" or "false" string was passed as query parameter instead of bool value.
* Removed unit test because it brought unwanted dependencies on xorm into multiple modules.
This commit is contained in:
@@ -319,7 +319,7 @@ func (s *UserAuthTokenService) rotateToken(ctx context.Context, token *auth.User
|
||||
now := getTime()
|
||||
var affected int64
|
||||
err = s.sqlStore.WithTransactionalDbSession(ctx, func(dbSession *db.Session) error {
|
||||
res, err := dbSession.Exec(sql, userAgent, clientIPStr, hashedToken, s.sqlStore.GetDialect().BooleanStr(false), now.Unix(), token.Id)
|
||||
res, err := dbSession.Exec(sql, userAgent, clientIPStr, hashedToken, s.sqlStore.GetDialect().BooleanValue(false), now.Unix(), token.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -627,7 +627,7 @@ func (d *dashboardStore) SoftDeleteDashboardsInFolders(ctx context.Context, orgI
|
||||
for _, folderUID := range folderUids {
|
||||
args = append(args, folderUID)
|
||||
}
|
||||
args = append(args, orgID, d.store.GetDialect().BooleanStr(false))
|
||||
args = append(args, orgID, d.store.GetDialect().BooleanValue(false))
|
||||
|
||||
_, err := sess.Exec(args...)
|
||||
return err
|
||||
@@ -674,14 +674,20 @@ func (d *dashboardStore) deleteDashboard(cmd *dashboards.DeleteDashboardCommand,
|
||||
|
||||
if dashboard.IsFolder {
|
||||
if !d.features.IsEnabledGlobally(featuremgmt.FlagDashboardRestore) {
|
||||
sqlStatements = append(sqlStatements, statement{SQL: "DELETE FROM dashboard WHERE org_id = ? AND folder_uid = ? AND is_folder = ? AND deleted IS NULL", args: []any{dashboard.OrgID, dashboard.UID, d.store.GetDialect().BooleanStr(false)}})
|
||||
sqlStatements = append(sqlStatements, statement{
|
||||
SQL: "DELETE FROM dashboard WHERE org_id = ? AND folder_uid = ? AND is_folder = ? AND deleted IS NULL",
|
||||
args: []any{dashboard.OrgID, dashboard.UID, d.store.GetDialect().BooleanValue(false)},
|
||||
})
|
||||
|
||||
if err := d.deleteChildrenDashboardAssociations(sess, &dashboard); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
// soft delete all dashboards in the folder
|
||||
sqlStatements = append(sqlStatements, statement{SQL: "UPDATE dashboard SET deleted = ? WHERE org_id = ? AND folder_uid = ? AND is_folder = ? ", args: []any{time.Now(), dashboard.OrgID, dashboard.UID, d.store.GetDialect().BooleanStr(false)}})
|
||||
sqlStatements = append(sqlStatements, statement{
|
||||
SQL: "UPDATE dashboard SET deleted = ? WHERE org_id = ? AND folder_uid = ? AND is_folder = ? ",
|
||||
args: []any{time.Now(), dashboard.OrgID, dashboard.UID, d.store.GetDialect().BooleanValue(false)},
|
||||
})
|
||||
}
|
||||
|
||||
// remove all access control permission with folder scope
|
||||
@@ -1083,7 +1089,7 @@ func (d *dashboardStore) CountDashboardsInFolders(
|
||||
}
|
||||
}
|
||||
s.WriteString(" AND org_id = ? AND is_folder = ? AND deleted IS NULL")
|
||||
args = append(args, req.OrgID, d.store.GetDialect().BooleanStr(false))
|
||||
args = append(args, req.OrgID, d.store.GetDialect().BooleanValue(false))
|
||||
sql := s.String()
|
||||
_, err := sess.SQL(sql, args...).Get(&count)
|
||||
return err
|
||||
|
||||
@@ -40,7 +40,7 @@ func (m *FolderUIDMigration) Exec(sess *xorm.Session, mgrtr *migrator.Migrator)
|
||||
WHERE d.is_folder = ?`
|
||||
}
|
||||
|
||||
r, err := sess.Exec(q, mgrtr.Dialect.BooleanStr(false))
|
||||
r, err := sess.Exec(q, mgrtr.Dialect.BooleanValue(false))
|
||||
if err != nil {
|
||||
mgrtr.Logger.Error("Failed to migrate dashboard folder_uid for dashboards", "error", err)
|
||||
return err
|
||||
@@ -68,7 +68,7 @@ func (m *FolderUIDMigration) Exec(sess *xorm.Session, mgrtr *migrator.Migrator)
|
||||
)
|
||||
WHERE is_folder = ?`
|
||||
}
|
||||
r, err = sess.Exec(q, mgrtr.Dialect.BooleanStr(true))
|
||||
r, err = sess.Exec(q, mgrtr.Dialect.BooleanValue(true))
|
||||
if err != nil {
|
||||
mgrtr.Logger.Error("Failed to migrate dashboard folder_uid for folders", "error", err)
|
||||
return err
|
||||
|
||||
@@ -138,7 +138,7 @@ func (ss *SqlStore) GetPrunableProvisionedDataSources(ctx context.Context) ([]*d
|
||||
|
||||
dataSources := make([]*datasources.DataSource, 0)
|
||||
return dataSources, ss.db.WithDbSession(ctx, func(sess *db.Session) error {
|
||||
return sess.Where(prunableQuery, ss.db.GetDialect().BooleanStr(true)).Asc("id").Find(&dataSources)
|
||||
return sess.Where(prunableQuery, ss.db.GetDialect().BooleanValue(true)).Asc("id").Find(&dataSources)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -569,7 +569,7 @@ func (ss *sqlStore) SearchOrgUsers(ctx context.Context, query *org.SearchOrgUser
|
||||
}
|
||||
|
||||
whereConditions = append(whereConditions, "u.is_service_account = ?")
|
||||
whereParams = append(whereParams, ss.dialect.BooleanStr(false))
|
||||
whereParams = append(whereParams, ss.dialect.BooleanValue(false))
|
||||
|
||||
if query.User == nil {
|
||||
ss.log.Warn("Query user not set for filtering.")
|
||||
|
||||
@@ -63,7 +63,7 @@ func (ss *SecretsStoreImpl) GetCurrentDataKey(ctx context.Context, label string)
|
||||
err := ss.db.WithDbSession(ctx, func(sess *db.Session) error {
|
||||
var err error
|
||||
exists, err = sess.Table(ss.table).
|
||||
Where("label = ? AND active = ?", label, ss.db.GetDialect().BooleanStr(true)).
|
||||
Where("label = ? AND active = ?", label, ss.db.GetDialect().BooleanValue(true)).
|
||||
Get(dataKey)
|
||||
return err
|
||||
})
|
||||
@@ -109,7 +109,7 @@ func (ss *SecretsStoreImpl) CreateDataKey(ctx context.Context, dataKey *secrets.
|
||||
func (ss *SecretsStoreImpl) DisableDataKeys(ctx context.Context) error {
|
||||
return ss.db.WithTransactionalDbSession(ctx, func(sess *db.Session) error {
|
||||
_, err := sess.Table(ss.table).
|
||||
Where("active = ?", ss.db.GetDialect().BooleanStr(true)).
|
||||
Where("active = ?", ss.db.GetDialect().BooleanValue(true)).
|
||||
UseBool("active").Update(&secrets.DataKey{Active: false})
|
||||
return err
|
||||
})
|
||||
|
||||
@@ -169,7 +169,7 @@ func (s *ServiceAccountsStoreImpl) DeleteServiceAccount(ctx context.Context, org
|
||||
func (s *ServiceAccountsStoreImpl) deleteServiceAccount(sess *db.Session, orgId, serviceAccountId int64) error {
|
||||
user := user.User{}
|
||||
has, err := sess.Where(`org_id = ? and id = ? and is_service_account = ?`,
|
||||
orgId, serviceAccountId, s.sqlStore.GetDialect().BooleanStr(true)).Get(&user)
|
||||
orgId, serviceAccountId, s.sqlStore.GetDialect().BooleanValue(true)).Get(&user)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -346,7 +346,7 @@ func (s *ServiceAccountsStoreImpl) SearchOrgServiceAccounts(ctx context.Context,
|
||||
whereConditions = append(
|
||||
whereConditions,
|
||||
"is_disabled = ?")
|
||||
whereParams = append(whereParams, s.sqlStore.GetDialect().BooleanStr(true))
|
||||
whereParams = append(whereParams, s.sqlStore.GetDialect().BooleanValue(true))
|
||||
case serviceaccounts.FilterOnlyExternal:
|
||||
whereConditions = append(
|
||||
whereConditions,
|
||||
|
||||
@@ -96,7 +96,7 @@ func (s *ServiceAccountsStoreImpl) RevokeServiceAccountToken(ctx context.Context
|
||||
rawSQL := "UPDATE api_key SET is_revoked = ? WHERE id=? and org_id=? and service_account_id=?"
|
||||
|
||||
return s.sqlStore.WithDbSession(ctx, func(sess *db.Session) error {
|
||||
result, err := sess.Exec(rawSQL, s.sqlStore.GetDialect().BooleanStr(true), tokenId, orgId, serviceAccountId)
|
||||
result, err := sess.Exec(rawSQL, s.sqlStore.GetDialect().BooleanValue(true), tokenId, orgId, serviceAccountId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -29,6 +29,10 @@ type Dialect interface {
|
||||
SupportEngine() bool
|
||||
LikeStr() string
|
||||
Default(col *Column) string
|
||||
// BooleanValue can be used as an argument in SELECT or INSERT statements. For constructing
|
||||
// raw SQL queries, please use BooleanStr instead.
|
||||
BooleanValue(bool) any
|
||||
// BooleanStr should only be used to construct SQL statements (strings). For arguments to queries, use BooleanValue instead.
|
||||
BooleanStr(bool) string
|
||||
DateTimeFunc(string) string
|
||||
BatchSize() int
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
|
||||
"github.com/VividCortex/mysqlerr"
|
||||
"github.com/go-sql-driver/mysql"
|
||||
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
@@ -35,6 +36,13 @@ func (db *MySQLDialect) AutoIncrStr() string {
|
||||
return "AUTO_INCREMENT"
|
||||
}
|
||||
|
||||
func (db *MySQLDialect) BooleanValue(value bool) interface{} {
|
||||
if value {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (db *MySQLDialect) BooleanStr(value bool) string {
|
||||
if value {
|
||||
return "1"
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/lib/pq"
|
||||
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
@@ -38,6 +39,10 @@ func (db *PostgresDialect) AutoIncrStr() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (db *PostgresDialect) BooleanValue(value bool) any {
|
||||
return value
|
||||
}
|
||||
|
||||
func (db *PostgresDialect) BooleanStr(value bool) string {
|
||||
return strconv.FormatBool(value)
|
||||
}
|
||||
|
||||
@@ -55,6 +55,11 @@ func (s *SpannerDialect) SQLType(col *Column) string {
|
||||
}
|
||||
|
||||
func (s *SpannerDialect) BatchSize() int { return 1000 }
|
||||
|
||||
func (s *SpannerDialect) BooleanValue(b bool) any {
|
||||
return b
|
||||
}
|
||||
|
||||
func (s *SpannerDialect) BooleanStr(b bool) string {
|
||||
if b {
|
||||
return "true"
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/mattn/go-sqlite3"
|
||||
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
@@ -32,6 +33,13 @@ func (db *SQLite3) AutoIncrStr() string {
|
||||
return "AUTOINCREMENT"
|
||||
}
|
||||
|
||||
func (db *SQLite3) BooleanValue(value bool) any {
|
||||
if value {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (db *SQLite3) BooleanStr(value bool) string {
|
||||
if value {
|
||||
return "1"
|
||||
|
||||
@@ -479,11 +479,11 @@ func populateSSOSettings(sqlStore db.DB, template models.SSOSettings, providers
|
||||
|
||||
func getSSOSettingsCountByDeleted(sqlStore db.DB) (deleted, notDeleted int64, err error) {
|
||||
err = sqlStore.WithDbSession(context.Background(), func(sess *db.Session) error {
|
||||
deleted, err = sess.Table("sso_setting").Where("is_deleted = ?", sqlStore.GetDialect().BooleanStr(true)).Count()
|
||||
deleted, err = sess.Table("sso_setting").Where("is_deleted = ?", sqlStore.GetDialect().BooleanValue(true)).Count()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
notDeleted, err = sess.Table("sso_setting").Where("is_deleted = ?", sqlStore.GetDialect().BooleanStr(false)).Count()
|
||||
notDeleted, err = sess.Table("sso_setting").Where("is_deleted = ?", sqlStore.GetDialect().BooleanValue(false)).Count()
|
||||
return err
|
||||
})
|
||||
|
||||
@@ -495,7 +495,7 @@ func getSSOSettingsByProvider(sqlStore db.DB, provider string, deleted bool) (*m
|
||||
var err error
|
||||
|
||||
err = sqlStore.WithDbSession(context.Background(), func(sess *db.Session) error {
|
||||
_, err = sess.Table("sso_setting").Where("provider = ? AND is_deleted = ?", provider, sqlStore.GetDialect().BooleanStr(deleted)).Get(&model)
|
||||
_, err = sess.Table("sso_setting").Where("provider = ? AND is_deleted = ?", provider, sqlStore.GetDialect().BooleanValue(deleted)).Get(&model)
|
||||
return err
|
||||
})
|
||||
|
||||
|
||||
@@ -166,8 +166,8 @@ func (ss *sqlStatsService) GetSystemStats(ctx context.Context, query *stats.GetS
|
||||
}
|
||||
// currently not supported when dashboards are in unified storage
|
||||
if !ss.features.IsEnabledGlobally(featuremgmt.FlagKubernetesClientDashboardsFolders) {
|
||||
sb.Write(`(SELECT SUM(LENGTH(data)) FROM `+dialect.Quote("dashboard")+` WHERE is_folder = ?) AS dashboard_bytes_total,`, dialect.BooleanStr(false))
|
||||
sb.Write(`(SELECT MAX(LENGTH(data)) FROM `+dialect.Quote("dashboard")+` WHERE is_folder = ?) AS dashboard_bytes_max,`, dialect.BooleanStr(false))
|
||||
sb.Write(`(SELECT SUM(LENGTH(data)) FROM `+dialect.Quote("dashboard")+` WHERE is_folder = ?) AS dashboard_bytes_total,`, dialect.BooleanValue(false))
|
||||
sb.Write(`(SELECT MAX(LENGTH(data)) FROM `+dialect.Quote("dashboard")+` WHERE is_folder = ?) AS dashboard_bytes_max,`, dialect.BooleanValue(false))
|
||||
}
|
||||
|
||||
sb.Write(ss.roleCounterSQL(ctx))
|
||||
|
||||
@@ -521,7 +521,7 @@ func (ss *xormStore) getTeamMembers(ctx context.Context, query *team.GetTeamMemb
|
||||
sess.Join("INNER", "team", "team.id=team_member.team_id")
|
||||
|
||||
// explicitly check for serviceaccounts
|
||||
sess.Where(fmt.Sprintf("%s.is_service_account=?", ss.db.GetDialect().Quote("user")), ss.db.GetDialect().BooleanStr(false))
|
||||
sess.Where(fmt.Sprintf("%s.is_service_account=?", ss.db.GetDialect().Quote("user")), ss.db.GetDialect().BooleanValue(false))
|
||||
|
||||
if acUserFilter != nil {
|
||||
sess.Where(acUserFilter.Where, acUserFilter.Args...)
|
||||
@@ -549,7 +549,7 @@ func (ss *xormStore) getTeamMembers(ctx context.Context, query *team.GetTeamMemb
|
||||
sess.Where("team_member.user_id=?", query.UserID)
|
||||
}
|
||||
if query.External {
|
||||
sess.Where("team_member.external=?", ss.db.GetDialect().BooleanStr(true))
|
||||
sess.Where("team_member.external=?", ss.db.GetDialect().BooleanValue(true))
|
||||
}
|
||||
sess.Cols(
|
||||
"team_member.org_id",
|
||||
|
||||
@@ -468,7 +468,7 @@ func (ss *sqlStore) Search(ctx context.Context, query *user.SearchUsersQuery) (*
|
||||
sess := dbSess.Table("user").Alias("u")
|
||||
|
||||
whereConditions = append(whereConditions, "u.is_service_account = ?")
|
||||
whereParams = append(whereParams, ss.dialect.BooleanStr(false))
|
||||
whereParams = append(whereParams, ss.dialect.BooleanValue(false))
|
||||
|
||||
// Join with only most recent auth module
|
||||
joinCondition := `(
|
||||
|
||||
Reference in New Issue
Block a user