mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Chore: Add function for detecting if the SQL driver supported CTEs (#64441)
* Add interface method for detecting if the SQL driver supported CTEs * Update nested folder store to call RecursiveQueriesAreSupported()
This commit is contained in:
committed by
GitHub
parent
fd6e97d52d
commit
4ee0be6fdf
@@ -2,13 +2,9 @@ package folderimpl
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/VividCortex/mysqlerr"
|
||||
"github.com/go-sql-driver/mysql"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/db"
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/infra/slugify"
|
||||
@@ -196,22 +192,25 @@ func (ss *sqlStore) GetParents(ctx context.Context, q folder.GetParentsQuery) ([
|
||||
SELECT * FROM RecQry;
|
||||
`
|
||||
|
||||
if err := ss.db.WithDbSession(ctx, func(sess *db.Session) error {
|
||||
err := sess.SQL(recQuery, q.UID, q.OrgID).Find(&folders)
|
||||
if err != nil {
|
||||
return folder.ErrDatabaseError.Errorf("failed to get folder parents: %w", err)
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
var driverErr *mysql.MySQLError
|
||||
if errors.As(err, &driverErr) {
|
||||
if driverErr.Number == mysqlerr.ER_PARSE_ERROR {
|
||||
ss.log.Debug("recursive CTE subquery is not supported; it fallbacks to the iterative implementation")
|
||||
return ss.getParentsMySQL(ctx, q)
|
||||
}
|
||||
}
|
||||
recursiveQueriesAreSupported, err := ss.db.RecursiveQueriesAreSupported()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
switch recursiveQueriesAreSupported {
|
||||
case true:
|
||||
if err := ss.db.WithDbSession(ctx, func(sess *db.Session) error {
|
||||
err := sess.SQL(recQuery, q.UID, q.OrgID).Find(&folders)
|
||||
if err != nil {
|
||||
return folder.ErrDatabaseError.Errorf("failed to get folder parents: %w", err)
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
default:
|
||||
ss.log.Debug("recursive CTE subquery is not supported; it fallbacks to the iterative implementation")
|
||||
return ss.getParentsMySQL(ctx, q)
|
||||
}
|
||||
|
||||
if len(folders) < 1 {
|
||||
// the query is expected to return at least the same folder
|
||||
|
||||
Reference in New Issue
Block a user