2020-06-25 06:01:13 +00:00
|
|
|
package repository
|
2020-04-14 16:20:20 +00:00
|
|
|
|
|
|
|
import (
|
2023-08-22 12:49:02 +00:00
|
|
|
"context"
|
|
|
|
"database/sql"
|
2020-04-14 16:20:20 +00:00
|
|
|
"fmt"
|
2020-11-19 13:13:07 +00:00
|
|
|
|
2020-04-14 16:20:20 +00:00
|
|
|
"github.com/jinzhu/gorm"
|
2021-12-06 15:28:49 +00:00
|
|
|
|
2023-08-22 12:49:02 +00:00
|
|
|
"github.com/zitadel/logging"
|
|
|
|
|
2022-08-31 07:52:43 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/database"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2020-04-14 16:20:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type SearchRequest interface {
|
|
|
|
GetLimit() uint64
|
|
|
|
GetOffset() uint64
|
|
|
|
GetSortingColumn() ColumnKey
|
|
|
|
GetAsc() bool
|
|
|
|
GetQueries() []SearchQuery
|
|
|
|
}
|
|
|
|
|
|
|
|
type SearchQuery interface {
|
|
|
|
GetKey() ColumnKey
|
2021-03-01 07:48:50 +00:00
|
|
|
GetMethod() domain.SearchMethod
|
2020-04-14 16:20:20 +00:00
|
|
|
GetValue() interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ColumnKey interface {
|
|
|
|
ToColumnName() string
|
|
|
|
}
|
|
|
|
|
2020-07-30 12:37:55 +00:00
|
|
|
func PrepareSearchQuery(table string, request SearchRequest) func(db *gorm.DB, res interface{}) (uint64, error) {
|
|
|
|
return func(db *gorm.DB, res interface{}) (uint64, error) {
|
|
|
|
var count uint64 = 0
|
2020-04-14 16:20:20 +00:00
|
|
|
query := db.Table(table)
|
|
|
|
if column := request.GetSortingColumn(); column != nil {
|
|
|
|
order := "DESC"
|
|
|
|
if request.GetAsc() {
|
|
|
|
order = "ASC"
|
|
|
|
}
|
|
|
|
query = query.Order(fmt.Sprintf("%s %s", column.ToColumnName(), order))
|
|
|
|
}
|
2020-11-19 13:13:07 +00:00
|
|
|
|
2020-04-14 16:20:20 +00:00
|
|
|
for _, q := range request.GetQueries() {
|
|
|
|
var err error
|
|
|
|
query, err = SetQuery(query, q.GetKey(), q.GetValue(), q.GetMethod())
|
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return count, zerrors.ThrowInvalidArgument(err, "VIEW-KaGue", "query is invalid")
|
2020-04-14 16:20:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-22 12:49:02 +00:00
|
|
|
query = query.BeginTx(context.Background(), &sql.TxOptions{ReadOnly: true})
|
|
|
|
defer func() {
|
|
|
|
if err := query.Commit().Error; err != nil {
|
|
|
|
logging.OnError(err).Info("commit failed")
|
|
|
|
}
|
2024-01-10 23:02:50 +00:00
|
|
|
query.RollbackUnlessCommitted()
|
2023-08-22 12:49:02 +00:00
|
|
|
}()
|
|
|
|
|
2020-04-14 16:20:20 +00:00
|
|
|
query = query.Count(&count)
|
2021-12-06 15:28:49 +00:00
|
|
|
if res == nil {
|
|
|
|
return count, nil
|
|
|
|
}
|
2020-04-14 16:20:20 +00:00
|
|
|
if request.GetLimit() != 0 {
|
|
|
|
query = query.Limit(request.GetLimit())
|
|
|
|
}
|
|
|
|
query = query.Offset(request.GetOffset())
|
|
|
|
err := query.Find(res).Error
|
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return count, zerrors.ThrowInternal(err, "VIEW-muSDK", "unable to find result")
|
2020-04-14 16:20:20 +00:00
|
|
|
}
|
|
|
|
return count, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-01 07:48:50 +00:00
|
|
|
func SetQuery(query *gorm.DB, key ColumnKey, value interface{}, method domain.SearchMethod) (*gorm.DB, error) {
|
2020-04-14 16:20:20 +00:00
|
|
|
column := key.ToColumnName()
|
|
|
|
if column == "" {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "VIEW-7dz3w", "Column name missing")
|
2020-04-14 16:20:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch method {
|
2021-03-01 07:48:50 +00:00
|
|
|
case domain.SearchMethodEquals:
|
2020-04-14 16:20:20 +00:00
|
|
|
query = query.Where(""+column+" = ?", value)
|
2021-03-01 07:48:50 +00:00
|
|
|
case domain.SearchMethodEqualsIgnoreCase:
|
2020-04-14 16:20:20 +00:00
|
|
|
valueText, ok := value.(string)
|
|
|
|
if !ok {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "VIEW-idu8e", "Equal ignore case only possible for strings")
|
2020-04-14 16:20:20 +00:00
|
|
|
}
|
|
|
|
query = query.Where("LOWER("+column+") = LOWER(?)", valueText)
|
2021-03-01 07:48:50 +00:00
|
|
|
case domain.SearchMethodStartsWith:
|
2020-04-14 16:20:20 +00:00
|
|
|
valueText, ok := value.(string)
|
|
|
|
if !ok {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "VIEW-SLj7s", "Starts with only possible for strings")
|
2020-04-14 16:20:20 +00:00
|
|
|
}
|
2024-01-02 15:27:36 +00:00
|
|
|
valueText = database.EscapeLikeWildcards(valueText)
|
2020-04-14 16:20:20 +00:00
|
|
|
query = query.Where(column+" LIKE ?", valueText+"%")
|
2021-03-01 07:48:50 +00:00
|
|
|
case domain.SearchMethodStartsWithIgnoreCase:
|
2020-04-14 16:20:20 +00:00
|
|
|
valueText, ok := value.(string)
|
|
|
|
if !ok {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "VIEW-eidus", "Starts with ignore case only possible for strings")
|
2020-04-14 16:20:20 +00:00
|
|
|
}
|
2024-01-02 15:27:36 +00:00
|
|
|
valueText = database.EscapeLikeWildcards(valueText)
|
2020-04-14 16:20:20 +00:00
|
|
|
query = query.Where("LOWER("+column+") LIKE LOWER(?)", valueText+"%")
|
2021-03-01 07:48:50 +00:00
|
|
|
case domain.SearchMethodEndsWith:
|
2020-08-18 06:57:16 +00:00
|
|
|
valueText, ok := value.(string)
|
|
|
|
if !ok {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "VIEW-Hswd3", "Ends with only possible for strings")
|
2020-08-18 06:57:16 +00:00
|
|
|
}
|
2024-01-02 15:27:36 +00:00
|
|
|
valueText = database.EscapeLikeWildcards(valueText)
|
2020-08-18 06:57:16 +00:00
|
|
|
query = query.Where(column+" LIKE ?", "%"+valueText)
|
2021-03-01 07:48:50 +00:00
|
|
|
case domain.SearchMethodEndsWithIgnoreCase:
|
2020-08-18 06:57:16 +00:00
|
|
|
valueText, ok := value.(string)
|
|
|
|
if !ok {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "VIEW-dAG31", "Ends with ignore case only possible for strings")
|
2020-08-18 06:57:16 +00:00
|
|
|
}
|
2024-01-02 15:27:36 +00:00
|
|
|
valueText = database.EscapeLikeWildcards(valueText)
|
2020-08-18 06:57:16 +00:00
|
|
|
query = query.Where("LOWER("+column+") LIKE LOWER(?)", "%"+valueText)
|
2021-03-01 07:48:50 +00:00
|
|
|
case domain.SearchMethodContains:
|
2020-04-14 16:20:20 +00:00
|
|
|
valueText, ok := value.(string)
|
|
|
|
if !ok {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "VIEW-3ids", "Contains with only possible for strings")
|
2020-04-14 16:20:20 +00:00
|
|
|
}
|
2024-01-02 15:27:36 +00:00
|
|
|
valueText = database.EscapeLikeWildcards(valueText)
|
2020-04-14 16:20:20 +00:00
|
|
|
query = query.Where(column+" LIKE ?", "%"+valueText+"%")
|
2021-03-01 07:48:50 +00:00
|
|
|
case domain.SearchMethodContainsIgnoreCase:
|
2020-04-14 16:20:20 +00:00
|
|
|
valueText, ok := value.(string)
|
|
|
|
if !ok {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "VIEW-eid73", "Contains with ignore case only possible for strings")
|
2020-04-14 16:20:20 +00:00
|
|
|
}
|
2024-01-02 15:27:36 +00:00
|
|
|
valueText = database.EscapeLikeWildcards(valueText)
|
2020-04-14 16:20:20 +00:00
|
|
|
query = query.Where("LOWER("+column+") LIKE LOWER(?)", "%"+valueText+"%")
|
2021-03-01 07:48:50 +00:00
|
|
|
case domain.SearchMethodNotEquals:
|
2020-05-11 10:16:29 +00:00
|
|
|
query = query.Where(""+column+" <> ?", value)
|
2021-03-01 07:48:50 +00:00
|
|
|
case domain.SearchMethodGreaterThan:
|
2020-06-05 05:50:04 +00:00
|
|
|
query = query.Where(column+" > ?", value)
|
2021-03-01 07:48:50 +00:00
|
|
|
case domain.SearchMethodLessThan:
|
2020-06-05 05:50:04 +00:00
|
|
|
query = query.Where(column+" < ?", value)
|
2021-03-01 07:48:50 +00:00
|
|
|
case domain.SearchMethodIsOneOf:
|
2020-06-05 05:50:04 +00:00
|
|
|
query = query.Where(column+" IN (?)", value)
|
2021-03-01 07:48:50 +00:00
|
|
|
case domain.SearchMethodListContains:
|
2020-06-19 13:32:03 +00:00
|
|
|
valueText, ok := value.(string)
|
|
|
|
if !ok {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "VIEW-Psois", "list contains only possible for strings")
|
2020-06-19 13:32:03 +00:00
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
query = query.Where("? <@ "+column, database.TextArray[string]{valueText})
|
2020-04-14 16:20:20 +00:00
|
|
|
default:
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return query, nil
|
|
|
|
}
|