2018-09-18 22:42:38 +02:00
|
|
|
package clauses
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-02-13 19:31:18 +02:00
|
|
|
|
2018-09-18 22:42:38 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/collections"
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
2019-02-13 19:31:18 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values/types"
|
2018-09-18 22:42:38 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type LimitClause struct {
|
2018-10-25 03:30:05 +02:00
|
|
|
src core.SourceMap
|
|
|
|
dataSource collections.Iterable
|
2018-11-13 02:58:12 +02:00
|
|
|
count core.Expression
|
|
|
|
offset core.Expression
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewLimitClause(
|
|
|
|
src core.SourceMap,
|
2018-10-25 03:30:05 +02:00
|
|
|
dataSource collections.Iterable,
|
2018-11-13 02:58:12 +02:00
|
|
|
count core.Expression,
|
|
|
|
offset core.Expression,
|
2018-10-25 03:30:05 +02:00
|
|
|
) (collections.Iterable, error) {
|
|
|
|
if dataSource == nil {
|
|
|
|
return nil, core.Error(core.ErrMissedArgument, "dataSource source")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &LimitClause{src, dataSource, count, offset}, nil
|
|
|
|
}
|
|
|
|
|
2018-09-18 22:42:38 +02:00
|
|
|
func (clause *LimitClause) Iterate(ctx context.Context, scope *core.Scope) (collections.Iterator, error) {
|
|
|
|
src, err := clause.dataSource.Iterate(ctx, scope)
|
|
|
|
|
|
|
|
if err != nil {
|
2018-10-25 03:30:05 +02:00
|
|
|
return nil, core.SourceError(clause.src, err)
|
|
|
|
}
|
|
|
|
|
2018-11-13 02:58:12 +02:00
|
|
|
count, err := clause.count.Exec(ctx, scope)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, core.SourceError(clause.src, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
offset, err := clause.offset.Exec(ctx, scope)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, core.SourceError(clause.src, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
countInt, err := clause.parseValue(count)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
offsetInt, err := clause.parseValue(offset)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-03-29 16:48:51 +02:00
|
|
|
iterator, err := collections.NewLimitIterator(src, countInt, offsetInt)
|
2018-10-25 03:30:05 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, core.SourceError(clause.src, err)
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2018-10-25 03:30:05 +02:00
|
|
|
return iterator, nil
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
2018-11-13 02:58:12 +02:00
|
|
|
|
|
|
|
func (clause *LimitClause) parseValue(val core.Value) (int, error) {
|
2019-02-13 19:31:18 +02:00
|
|
|
if val.Type() == types.Int {
|
2018-11-13 02:58:12 +02:00
|
|
|
return val.Unwrap().(int), nil
|
|
|
|
}
|
|
|
|
|
2019-02-13 19:31:18 +02:00
|
|
|
if val.Type() == types.Float {
|
2018-11-13 02:58:12 +02:00
|
|
|
return int(val.Unwrap().(float64)), nil
|
|
|
|
}
|
|
|
|
|
2019-02-13 19:31:18 +02:00
|
|
|
return -1, core.TypeError(val.Type(), types.Int, types.Float)
|
2018-11-13 02:58:12 +02:00
|
|
|
}
|