2021-02-06 11:05:47 +00:00
|
|
|
package spec
|
|
|
|
|
|
|
|
import (
|
2023-05-24 11:01:50 +00:00
|
|
|
"git.kmsign.com/royalcat/repogen/internal/code"
|
2021-02-06 11:05:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// QuerySpec is a set of conditions of querying the database
|
|
|
|
type QuerySpec struct {
|
|
|
|
Operator Operator
|
|
|
|
Predicates []Predicate
|
|
|
|
}
|
|
|
|
|
|
|
|
// NumberOfArguments returns number of arguments required to perform the query
|
|
|
|
func (q QuerySpec) NumberOfArguments() int {
|
|
|
|
var totalArgs int
|
|
|
|
for _, predicate := range q.Predicates {
|
|
|
|
totalArgs += predicate.Comparator.NumberOfArguments()
|
|
|
|
}
|
|
|
|
return totalArgs
|
|
|
|
}
|
|
|
|
|
|
|
|
// Operator is a boolean operator for merging conditions
|
|
|
|
type Operator string
|
|
|
|
|
|
|
|
// boolean operator types
|
|
|
|
const (
|
|
|
|
OperatorAnd Operator = "AND"
|
|
|
|
OperatorOr Operator = "OR"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Comparator is a comparison operator of the condition to query the data
|
|
|
|
type Comparator string
|
|
|
|
|
|
|
|
// comparator types
|
|
|
|
const (
|
|
|
|
ComparatorNot Comparator = "NOT"
|
|
|
|
ComparatorEqual Comparator = "EQUAL"
|
|
|
|
ComparatorLessThan Comparator = "LESS_THAN"
|
|
|
|
ComparatorLessThanEqual Comparator = "LESS_THAN_EQUAL"
|
|
|
|
ComparatorGreaterThan Comparator = "GREATER_THAN"
|
|
|
|
ComparatorGreaterThanEqual Comparator = "GREATER_THAN_EQUAL"
|
|
|
|
ComparatorBetween Comparator = "BETWEEN"
|
|
|
|
ComparatorIn Comparator = "IN"
|
2021-02-23 12:10:25 +00:00
|
|
|
ComparatorNotIn Comparator = "NOT_IN"
|
|
|
|
ComparatorTrue Comparator = "EQUAL_TRUE"
|
|
|
|
ComparatorFalse Comparator = "EQUAL_FALSE"
|
2022-11-12 09:09:59 +00:00
|
|
|
ComparatorExists Comparator = "EXISTS"
|
|
|
|
ComparatorNotExists Comparator = "NOT_EXISTS"
|
2021-02-06 11:05:47 +00:00
|
|
|
)
|
|
|
|
|
2022-11-09 04:21:11 +00:00
|
|
|
// ArgumentTypeFromFieldType returns a type of required argument from the given
|
|
|
|
// struct field type.
|
2021-02-06 11:05:47 +00:00
|
|
|
func (c Comparator) ArgumentTypeFromFieldType(t code.Type) code.Type {
|
2021-02-23 12:10:25 +00:00
|
|
|
switch c {
|
|
|
|
case ComparatorIn, ComparatorNotIn:
|
2021-02-06 11:05:47 +00:00
|
|
|
return code.ArrayType{ContainedType: t}
|
2021-02-23 12:10:25 +00:00
|
|
|
default:
|
|
|
|
return t
|
2021-02-06 11:05:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-09 04:21:11 +00:00
|
|
|
// NumberOfArguments returns the number of arguments required to perform the
|
|
|
|
// comparison.
|
2021-02-06 11:05:47 +00:00
|
|
|
func (c Comparator) NumberOfArguments() int {
|
2021-02-23 12:10:25 +00:00
|
|
|
switch c {
|
|
|
|
case ComparatorBetween:
|
2021-02-06 11:05:47 +00:00
|
|
|
return 2
|
2022-11-12 09:09:59 +00:00
|
|
|
case ComparatorTrue, ComparatorFalse, ComparatorExists, ComparatorNotExists:
|
2021-02-23 12:10:25 +00:00
|
|
|
return 0
|
|
|
|
default:
|
|
|
|
return 1
|
2021-02-06 11:05:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Predicate is a criteria for querying a field
|
|
|
|
type Predicate struct {
|
2021-03-03 14:01:32 +00:00
|
|
|
FieldReference FieldReference
|
|
|
|
Comparator Comparator
|
|
|
|
ParamIndex int
|
2021-02-06 11:05:47 +00:00
|
|
|
}
|
|
|
|
|
2021-03-03 14:01:32 +00:00
|
|
|
type queryParser struct {
|
|
|
|
fieldResolver fieldResolver
|
|
|
|
StructModel code.Struct
|
2021-02-06 11:05:47 +00:00
|
|
|
}
|
|
|
|
|
2022-11-12 09:09:59 +00:00
|
|
|
func (p queryParser) parseQuery(rawTokens []string, paramIndex int) (QuerySpec,
|
|
|
|
error) {
|
|
|
|
|
2021-02-14 04:48:09 +00:00
|
|
|
if len(rawTokens) == 0 {
|
2022-11-09 04:21:11 +00:00
|
|
|
return QuerySpec{}, ErrQueryRequired
|
2021-02-06 11:05:47 +00:00
|
|
|
}
|
|
|
|
|
2021-02-14 04:48:09 +00:00
|
|
|
tokens := rawTokens
|
2021-02-06 11:05:47 +00:00
|
|
|
if len(tokens) == 1 && tokens[0] == "All" {
|
|
|
|
return QuerySpec{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if tokens[0] == "By" {
|
|
|
|
tokens = tokens[1:]
|
|
|
|
}
|
|
|
|
|
2021-03-03 14:01:32 +00:00
|
|
|
if len(tokens) == 0 {
|
2021-02-14 04:48:09 +00:00
|
|
|
return QuerySpec{}, NewInvalidQueryError(rawTokens)
|
2021-02-06 11:05:47 +00:00
|
|
|
}
|
|
|
|
|
2021-03-03 14:01:32 +00:00
|
|
|
operator, predicateTokens, err := p.splitPredicateTokens(tokens)
|
|
|
|
if err != nil {
|
|
|
|
return QuerySpec{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
querySpec := QuerySpec{
|
|
|
|
Operator: operator,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, predicateToken := range predicateTokens {
|
|
|
|
predicate, err := p.parsePredicate(predicateToken, paramIndex)
|
|
|
|
if err != nil {
|
|
|
|
return QuerySpec{}, err
|
|
|
|
}
|
|
|
|
querySpec.Predicates = append(querySpec.Predicates, predicate)
|
|
|
|
paramIndex += predicate.Comparator.NumberOfArguments()
|
|
|
|
}
|
|
|
|
|
|
|
|
return querySpec, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p queryParser) splitPredicateTokens(tokens []string) (Operator, [][]string, error) {
|
2021-02-06 11:05:47 +00:00
|
|
|
var operator Operator
|
2021-03-03 14:01:32 +00:00
|
|
|
var predicateTokens [][]string
|
|
|
|
var aggregatedToken []string
|
|
|
|
|
2021-02-06 11:05:47 +00:00
|
|
|
for _, token := range tokens {
|
|
|
|
if token != "And" && token != "Or" {
|
|
|
|
aggregatedToken = append(aggregatedToken, token)
|
|
|
|
} else if len(aggregatedToken) == 0 {
|
2021-03-03 14:01:32 +00:00
|
|
|
return "", nil, NewInvalidQueryError(tokens)
|
2021-02-06 11:05:47 +00:00
|
|
|
} else if token == "And" && operator != OperatorOr {
|
|
|
|
operator = OperatorAnd
|
2021-03-03 14:01:32 +00:00
|
|
|
predicateTokens = append(predicateTokens, aggregatedToken)
|
|
|
|
aggregatedToken = nil
|
2021-02-06 11:05:47 +00:00
|
|
|
} else if token == "Or" && operator != OperatorAnd {
|
|
|
|
operator = OperatorOr
|
2021-03-03 14:01:32 +00:00
|
|
|
predicateTokens = append(predicateTokens, aggregatedToken)
|
|
|
|
aggregatedToken = nil
|
2021-02-06 11:05:47 +00:00
|
|
|
} else {
|
2021-03-03 14:01:32 +00:00
|
|
|
return "", nil, NewInvalidQueryError(tokens)
|
2021-02-06 11:05:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(aggregatedToken) == 0 {
|
2021-03-03 14:01:32 +00:00
|
|
|
return "", nil, NewInvalidQueryError(tokens)
|
|
|
|
}
|
|
|
|
predicateTokens = append(predicateTokens, aggregatedToken)
|
|
|
|
|
|
|
|
return operator, predicateTokens, nil
|
|
|
|
}
|
|
|
|
|
2022-11-12 09:09:59 +00:00
|
|
|
func (p queryParser) parsePredicate(t []string, paramIndex int) (Predicate,
|
|
|
|
error) {
|
|
|
|
|
2021-03-03 14:01:32 +00:00
|
|
|
if len(t) > 1 && t[len(t)-1] == "Not" {
|
|
|
|
return p.createPredicate(t[:len(t)-1], ComparatorNot, paramIndex)
|
|
|
|
}
|
|
|
|
if len(t) > 2 && t[len(t)-2] == "Less" && t[len(t)-1] == "Than" {
|
|
|
|
return p.createPredicate(t[:len(t)-2], ComparatorLessThan, paramIndex)
|
|
|
|
}
|
|
|
|
if len(t) > 3 && t[len(t)-3] == "Less" && t[len(t)-2] == "Than" && t[len(t)-1] == "Equal" {
|
|
|
|
return p.createPredicate(t[:len(t)-3], ComparatorLessThanEqual, paramIndex)
|
|
|
|
}
|
|
|
|
if len(t) > 2 && t[len(t)-2] == "Greater" && t[len(t)-1] == "Than" {
|
|
|
|
return p.createPredicate(t[:len(t)-2], ComparatorGreaterThan, paramIndex)
|
|
|
|
}
|
|
|
|
if len(t) > 3 && t[len(t)-3] == "Greater" && t[len(t)-2] == "Than" && t[len(t)-1] == "Equal" {
|
|
|
|
return p.createPredicate(t[:len(t)-3], ComparatorGreaterThanEqual, paramIndex)
|
|
|
|
}
|
|
|
|
if len(t) > 2 && t[len(t)-2] == "Not" && t[len(t)-1] == "In" {
|
|
|
|
return p.createPredicate(t[:len(t)-2], ComparatorNotIn, paramIndex)
|
|
|
|
}
|
2022-11-12 09:09:59 +00:00
|
|
|
if len(t) > 2 && t[len(t)-2] == "Not" && t[len(t)-1] == "Exists" {
|
|
|
|
return p.createPredicate(t[:len(t)-2], ComparatorNotExists, paramIndex)
|
|
|
|
}
|
2021-03-03 14:01:32 +00:00
|
|
|
if len(t) > 1 && t[len(t)-1] == "In" {
|
|
|
|
return p.createPredicate(t[:len(t)-1], ComparatorIn, paramIndex)
|
|
|
|
}
|
|
|
|
if len(t) > 1 && t[len(t)-1] == "Between" {
|
|
|
|
return p.createPredicate(t[:len(t)-1], ComparatorBetween, paramIndex)
|
|
|
|
}
|
|
|
|
if len(t) > 1 && t[len(t)-1] == "True" {
|
|
|
|
return p.createPredicate(t[:len(t)-1], ComparatorTrue, paramIndex)
|
|
|
|
}
|
|
|
|
if len(t) > 1 && t[len(t)-1] == "False" {
|
|
|
|
return p.createPredicate(t[:len(t)-1], ComparatorFalse, paramIndex)
|
|
|
|
}
|
2022-11-12 09:09:59 +00:00
|
|
|
if len(t) > 1 && t[len(t)-1] == "Exists" {
|
|
|
|
return p.createPredicate(t[:len(t)-1], ComparatorExists, paramIndex)
|
|
|
|
}
|
2021-03-03 14:01:32 +00:00
|
|
|
return p.createPredicate(t, ComparatorEqual, paramIndex)
|
|
|
|
}
|
|
|
|
|
2022-11-12 09:09:59 +00:00
|
|
|
func (p queryParser) createPredicate(t []string, comparator Comparator,
|
|
|
|
paramIndex int) (Predicate, error) {
|
|
|
|
|
2021-03-03 14:01:32 +00:00
|
|
|
fields, ok := p.fieldResolver.ResolveStructField(p.StructModel, t)
|
|
|
|
if !ok {
|
|
|
|
return Predicate{}, NewStructFieldNotFoundError(t)
|
2021-02-06 11:05:47 +00:00
|
|
|
}
|
|
|
|
|
2021-03-03 14:01:32 +00:00
|
|
|
return Predicate{
|
|
|
|
FieldReference: fields,
|
|
|
|
Comparator: comparator,
|
|
|
|
ParamIndex: paramIndex,
|
|
|
|
}, nil
|
2021-02-06 11:05:47 +00:00
|
|
|
}
|