2021-01-16 06:36:44 +00:00
|
|
|
package spec
|
|
|
|
|
2021-01-19 12:26:26 +00:00
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/sunboyy/repogen/internal/code"
|
|
|
|
)
|
2021-01-16 06:36:44 +00:00
|
|
|
|
|
|
|
// QueryMode one or many
|
|
|
|
type QueryMode string
|
|
|
|
|
|
|
|
// query mode constants
|
|
|
|
const (
|
|
|
|
QueryModeOne QueryMode = "ONE"
|
|
|
|
QueryModeMany QueryMode = "MANY"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RepositorySpec is a specification generated from the repository interface
|
|
|
|
type RepositorySpec struct {
|
|
|
|
InterfaceName string
|
|
|
|
Methods []MethodSpec
|
|
|
|
}
|
|
|
|
|
|
|
|
// MethodSpec is a method specification inside repository specification
|
|
|
|
type MethodSpec struct {
|
|
|
|
Name string
|
|
|
|
Params []code.Param
|
|
|
|
Returns []code.Type
|
|
|
|
Operation Operation
|
|
|
|
}
|
|
|
|
|
|
|
|
// Operation is an interface for any kind of operation
|
|
|
|
type Operation interface {
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindOperation is a method specification for find operations
|
|
|
|
type FindOperation struct {
|
|
|
|
Mode QueryMode
|
2021-01-17 03:29:50 +00:00
|
|
|
Query QuerySpec
|
2021-01-16 06:36:44 +00:00
|
|
|
}
|
|
|
|
|
2021-01-19 12:26:26 +00:00
|
|
|
// QuerySpec is a set of conditions of querying the database
|
2021-01-17 03:29:50 +00:00
|
|
|
type QuerySpec struct {
|
2021-01-19 12:35:54 +00:00
|
|
|
Operator Operator
|
2021-01-19 12:26:26 +00:00
|
|
|
Predicates []Predicate
|
2021-01-16 06:36:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NumberOfArguments returns number of arguments required to perform the query
|
2021-01-17 03:29:50 +00:00
|
|
|
func (q QuerySpec) NumberOfArguments() int {
|
2021-01-19 12:26:26 +00:00
|
|
|
return len(q.Predicates)
|
|
|
|
}
|
|
|
|
|
2021-01-19 12:35:54 +00:00
|
|
|
// Operator is a boolean operator for merging conditions
|
2021-01-19 12:26:26 +00:00
|
|
|
type Operator string
|
|
|
|
|
2021-01-19 12:35:54 +00:00
|
|
|
// boolean operator types
|
2021-01-19 12:26:26 +00:00
|
|
|
const (
|
2021-01-19 12:35:54 +00:00
|
|
|
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"
|
2021-01-19 12:26:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Predicate is a criteria for querying a field
|
|
|
|
type Predicate struct {
|
2021-01-19 12:35:54 +00:00
|
|
|
Field string
|
|
|
|
Comparator Comparator
|
2021-01-19 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type predicateToken []string
|
|
|
|
|
|
|
|
func (t predicateToken) ToPredicate() Predicate {
|
|
|
|
if len(t) > 1 && t[len(t)-1] == "Not" {
|
2021-01-19 12:35:54 +00:00
|
|
|
return Predicate{Field: strings.Join(t[:len(t)-1], ""), Comparator: ComparatorNot}
|
2021-01-19 12:26:26 +00:00
|
|
|
}
|
|
|
|
if len(t) > 2 && t[len(t)-2] == "Less" && t[len(t)-1] == "Than" {
|
2021-01-19 12:35:54 +00:00
|
|
|
return Predicate{Field: strings.Join(t[:len(t)-2], ""), Comparator: ComparatorLessThan}
|
2021-01-19 12:26:26 +00:00
|
|
|
}
|
|
|
|
if len(t) > 3 && t[len(t)-3] == "Less" && t[len(t)-2] == "Than" && t[len(t)-1] == "Equal" {
|
2021-01-19 12:35:54 +00:00
|
|
|
return Predicate{Field: strings.Join(t[:len(t)-3], ""), Comparator: ComparatorLessThanEqual}
|
2021-01-19 12:26:26 +00:00
|
|
|
}
|
|
|
|
if len(t) > 2 && t[len(t)-2] == "Greater" && t[len(t)-1] == "Than" {
|
2021-01-19 12:35:54 +00:00
|
|
|
return Predicate{Field: strings.Join(t[:len(t)-2], ""), Comparator: ComparatorGreaterThan}
|
2021-01-19 12:26:26 +00:00
|
|
|
}
|
|
|
|
if len(t) > 3 && t[len(t)-3] == "Greater" && t[len(t)-2] == "Than" && t[len(t)-1] == "Equal" {
|
2021-01-19 12:35:54 +00:00
|
|
|
return Predicate{Field: strings.Join(t[:len(t)-3], ""), Comparator: ComparatorGreaterThanEqual}
|
2021-01-19 12:26:26 +00:00
|
|
|
}
|
2021-01-19 12:35:54 +00:00
|
|
|
return Predicate{Field: strings.Join(t, ""), Comparator: ComparatorEqual}
|
2021-01-16 06:36:44 +00:00
|
|
|
}
|