grafana/pkg/services/queryhistory/writers.go
Kristin Laemmert 05709ce411
chore: remove sqlstore & mockstore dependencies from (most) packages (#57087)
* chore: add alias for InitTestDB and Session

Adds an alias for the sqlstore InitTestDB and Session, and updates tests using these to reduce dependencies on the sqlstore.Store.

* next pass of removing sqlstore imports
* last little bit
* remove mockstore where possible
2022-10-19 09:02:15 -04:00

54 lines
2.1 KiB
Go

package queryhistory
import (
"bytes"
"strings"
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/services/user"
)
func writeStarredSQL(query SearchInQueryHistoryQuery, sqlStore db.DB, builder *db.SQLBuilder) {
if query.OnlyStarred {
builder.Write(sqlStore.GetDialect().BooleanStr(true) + ` AS starred
FROM query_history
INNER JOIN query_history_star ON query_history_star.query_uid = query_history.uid
`)
} else {
builder.Write(` CASE WHEN query_history_star.query_uid IS NULL THEN ` + sqlStore.GetDialect().BooleanStr(false) + ` ELSE ` + sqlStore.GetDialect().BooleanStr(true) + ` END AS starred
FROM query_history
LEFT JOIN query_history_star ON query_history_star.query_uid = query_history.uid
`)
}
}
func writeFiltersSQL(query SearchInQueryHistoryQuery, user *user.SignedInUser, sqlStore db.DB, builder *db.SQLBuilder) {
params := []interface{}{user.OrgID, user.UserID, query.From, query.To, "%" + query.SearchString + "%", "%" + query.SearchString + "%"}
var sql bytes.Buffer
sql.WriteString(" WHERE query_history.org_id = ? AND query_history.created_by = ? AND query_history.created_at >= ? AND query_history.created_at <= ? AND (query_history.queries " + sqlStore.GetDialect().LikeStr() + " ? OR query_history.comment " + sqlStore.GetDialect().LikeStr() + " ?) ")
if len(query.DatasourceUIDs) > 0 {
for _, uid := range query.DatasourceUIDs {
params = append(params, uid)
}
sql.WriteString(" AND query_history.datasource_uid IN (? " + strings.Repeat(",?", len(query.DatasourceUIDs)-1) + ") ")
}
builder.Write(sql.String(), params...)
}
func writeSortSQL(query SearchInQueryHistoryQuery, sqlStore db.DB, builder *db.SQLBuilder) {
if query.Sort == "time-asc" {
builder.Write(" ORDER BY created_at ASC ")
} else {
builder.Write(" ORDER BY created_at DESC ")
}
}
func writeLimitSQL(query SearchInQueryHistoryQuery, sqlStore db.DB, builder *db.SQLBuilder) {
builder.Write(" LIMIT ? ", query.Limit)
}
func writeOffsetSQL(query SearchInQueryHistoryQuery, sqlStore db.DB, builder *db.SQLBuilder) {
builder.Write(" OFFSET ? ", query.Limit*(query.Page-1))
}