2022-03-07 05:28:04 -06:00
package queryhistory
import (
"bytes"
"strings"
2022-10-19 08:02:15 -05:00
"github.com/grafana/grafana/pkg/infra/db"
2022-08-10 04:56:48 -05:00
"github.com/grafana/grafana/pkg/services/user"
2022-03-07 05:28:04 -06:00
)
2022-10-19 08:02:15 -05:00
func writeStarredSQL ( query SearchInQueryHistoryQuery , sqlStore db . DB , builder * db . SQLBuilder ) {
2022-03-07 05:28:04 -06:00
if query . OnlyStarred {
2022-10-14 14:33:06 -05:00
builder . Write ( sqlStore . GetDialect ( ) . BooleanStr ( true ) + ` AS starred
2022-03-07 05:28:04 -06:00
FROM query_history
INNER JOIN query_history_star ON query_history_star . query_uid = query_history . uid
` )
} else {
2022-10-14 14:33:06 -05:00
builder . Write ( ` CASE WHEN query_history_star.query_uid IS NULL THEN ` + sqlStore . GetDialect ( ) . BooleanStr ( false ) + ` ELSE ` + sqlStore . GetDialect ( ) . BooleanStr ( true ) + ` END AS starred
2022-03-07 05:28:04 -06:00
FROM query_history
LEFT JOIN query_history_star ON query_history_star . query_uid = query_history . uid
` )
}
}
2022-10-19 08:02:15 -05:00
func writeFiltersSQL ( query SearchInQueryHistoryQuery , user * user . SignedInUser , sqlStore db . DB , builder * db . SQLBuilder ) {
2022-08-11 06:28:55 -05:00
params := [ ] interface { } { user . OrgID , user . UserID , query . From , query . To , "%" + query . SearchString + "%" , "%" + query . SearchString + "%" }
2022-03-07 05:28:04 -06:00
var sql bytes . Buffer
2022-10-14 14:33:06 -05:00
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 ( ) + " ?) " )
2022-04-29 02:55:33 -05:00
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 ) + ") " )
}
2022-03-07 05:28:04 -06:00
builder . Write ( sql . String ( ) , params ... )
}
2022-10-19 08:02:15 -05:00
func writeSortSQL ( query SearchInQueryHistoryQuery , sqlStore db . DB , builder * db . SQLBuilder ) {
2022-03-07 05:28:04 -06:00
if query . Sort == "time-asc" {
builder . Write ( " ORDER BY created_at ASC " )
} else {
builder . Write ( " ORDER BY created_at DESC " )
}
}
2022-10-19 08:02:15 -05:00
func writeLimitSQL ( query SearchInQueryHistoryQuery , sqlStore db . DB , builder * db . SQLBuilder ) {
2022-03-07 05:28:04 -06:00
builder . Write ( " LIMIT ? " , query . Limit )
}
2022-10-19 08:02:15 -05:00
func writeOffsetSQL ( query SearchInQueryHistoryQuery , sqlStore db . DB , builder * db . SQLBuilder ) {
2022-03-07 05:28:04 -06:00
builder . Write ( " OFFSET ? " , query . Limit * ( query . Page - 1 ) )
}