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
)
2024-02-20 07:32:40 -06:00
func writeStarredSQL ( query SearchInQueryHistoryQuery , sqlStore db . DB , builder * db . SQLBuilder , isCount bool ) {
var sql bytes . Buffer
if isCount {
sql . WriteString ( ` COUNT( ` )
}
if query . OnlyStarred {
sql . WriteString ( sqlStore . GetDialect ( ) . BooleanStr ( true ) )
} else {
sql . WriteString ( ` CASE WHEN query_history_star.query_uid IS NULL THEN ` + sqlStore . GetDialect ( ) . BooleanStr ( false ) + ` ELSE ` + sqlStore . GetDialect ( ) . BooleanStr ( true ) + ` END ` )
}
if isCount {
sql . WriteString ( ` ) ` )
}
sql . WriteString ( ` AS starred FROM query_history ` )
2022-03-07 05:28:04 -06:00
if query . OnlyStarred {
2024-02-20 07:32:40 -06:00
sql . WriteString ( ` INNER ` )
2022-03-07 05:28:04 -06:00
} else {
2024-02-20 07:32:40 -06:00
sql . WriteString ( ` LEFT ` )
2022-03-07 05:28:04 -06:00
}
2024-02-20 07:32:40 -06:00
sql . WriteString ( ` JOIN query_history_star ON query_history_star.query_uid = query_history.uid ` )
builder . Write ( sql . String ( ) )
2022-03-07 05:28:04 -06:00
}
2022-10-19 08:02:15 -05:00
func writeFiltersSQL ( query SearchInQueryHistoryQuery , user * user . SignedInUser , sqlStore db . DB , builder * db . SQLBuilder ) {
2023-08-30 10:46:47 -05:00
params := [ ] any { 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 ) )
}