2016-02-27 01:00:19 +00:00
|
|
|
// Copyright © 2016 Abcum Ltd
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
2017-03-03 21:39:37 +00:00
|
|
|
"context"
|
|
|
|
|
2016-02-27 01:00:19 +00:00
|
|
|
"github.com/abcum/surreal/sql"
|
2017-11-16 20:53:39 +00:00
|
|
|
"github.com/abcum/surreal/util/data"
|
2016-09-06 13:33:02 +00:00
|
|
|
"github.com/abcum/surreal/util/keys"
|
2016-02-27 01:00:19 +00:00
|
|
|
)
|
|
|
|
|
2017-11-16 20:53:39 +00:00
|
|
|
func (e *executor) executeSelect(ctx context.Context, stm *sql.SelectStatement) ([]interface{}, error) {
|
2016-10-29 11:28:20 +00:00
|
|
|
|
2018-04-28 19:17:21 +00:00
|
|
|
ctx = context.WithValue(ctx, ctxKeyVersion, stm.Version)
|
|
|
|
|
2017-11-16 20:53:39 +00:00
|
|
|
var what sql.Exprs
|
|
|
|
|
|
|
|
for _, val := range stm.What {
|
|
|
|
w, err := e.fetch(ctx, val, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-10-29 11:28:20 +00:00
|
|
|
}
|
2017-11-16 20:53:39 +00:00
|
|
|
what = append(what, w)
|
2016-10-29 11:28:20 +00:00
|
|
|
}
|
2016-07-04 10:37:29 +00:00
|
|
|
|
2017-11-16 20:53:39 +00:00
|
|
|
i := newIterator(e, ctx, stm, false)
|
|
|
|
|
|
|
|
for _, w := range what {
|
|
|
|
|
|
|
|
switch what := w.(type) {
|
|
|
|
|
|
|
|
default:
|
|
|
|
key := &keys.Thing{KV: stm.KV, NS: stm.NS, DB: stm.DB}
|
|
|
|
i.processQuery(ctx, key, []interface{}{what})
|
|
|
|
|
|
|
|
case *sql.Table:
|
|
|
|
key := &keys.Table{KV: stm.KV, NS: stm.NS, DB: stm.DB, TB: what.TB}
|
|
|
|
i.processTable(ctx, key)
|
|
|
|
|
|
|
|
case *sql.Ident:
|
|
|
|
key := &keys.Table{KV: stm.KV, NS: stm.NS, DB: stm.DB, TB: what.ID}
|
|
|
|
i.processTable(ctx, key)
|
|
|
|
|
|
|
|
case *sql.Thing:
|
|
|
|
key := &keys.Thing{KV: stm.KV, NS: stm.NS, DB: stm.DB, TB: what.TB, ID: what.ID}
|
|
|
|
i.processThing(ctx, key)
|
|
|
|
|
|
|
|
case *sql.Model:
|
|
|
|
key := &keys.Thing{KV: stm.KV, NS: stm.NS, DB: stm.DB, TB: what.TB, ID: nil}
|
|
|
|
i.processModel(ctx, key, what)
|
|
|
|
|
|
|
|
case *sql.Batch:
|
|
|
|
key := &keys.Thing{KV: stm.KV, NS: stm.NS, DB: stm.DB, TB: what.TB, ID: nil}
|
|
|
|
i.processBatch(ctx, key, what)
|
|
|
|
|
|
|
|
// Result of subquery
|
|
|
|
case []interface{}:
|
|
|
|
key := &keys.Thing{KV: stm.KV, NS: stm.NS, DB: stm.DB}
|
|
|
|
i.processQuery(ctx, key, what)
|
|
|
|
|
|
|
|
// Result of subquery with LIMIT 1
|
|
|
|
case map[string]interface{}:
|
|
|
|
key := &keys.Thing{KV: stm.KV, NS: stm.NS, DB: stm.DB}
|
|
|
|
i.processQuery(ctx, key, []interface{}{what})
|
2016-07-04 10:37:29 +00:00
|
|
|
|
2016-09-06 13:33:02 +00:00
|
|
|
}
|
|
|
|
|
2017-11-16 20:53:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return i.Yield(ctx)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *executor) fetchSelect(ctx context.Context, stm *sql.SelectStatement, doc *data.Doc) (interface{}, error) {
|
|
|
|
|
2018-04-20 22:40:52 +00:00
|
|
|
ctx = dive(ctx)
|
|
|
|
|
2017-11-16 20:53:39 +00:00
|
|
|
if doc != nil {
|
|
|
|
vars := data.New()
|
2017-12-04 22:20:39 +00:00
|
|
|
vars.Set(doc.Data(), varKeyParent)
|
2017-11-16 20:53:39 +00:00
|
|
|
ctx = context.WithValue(ctx, ctxKeySubs, vars)
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := e.executeSelect(ctx, stm)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-11-26 13:45:48 +00:00
|
|
|
cnt, err := e.fetchOutputs(ctx, stm)
|
2017-11-16 20:53:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-11-26 13:45:48 +00:00
|
|
|
switch cnt {
|
2017-11-16 20:53:39 +00:00
|
|
|
case 1:
|
|
|
|
switch len(stm.Expr) {
|
|
|
|
case 1:
|
|
|
|
f := stm.Expr[0]
|
|
|
|
switch f.Expr.(type) {
|
|
|
|
default:
|
|
|
|
return data.Consume(out).Get(docKeyOne, f.Field).Data(), nil
|
|
|
|
case *sql.All:
|
|
|
|
return data.Consume(out).Get(docKeyOne).Data(), nil
|
2016-09-06 13:33:02 +00:00
|
|
|
}
|
2017-11-16 20:53:39 +00:00
|
|
|
default:
|
|
|
|
return data.Consume(out).Get(docKeyOne).Data(), nil
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
switch len(stm.Expr) {
|
|
|
|
case 1:
|
|
|
|
f := stm.Expr[0]
|
|
|
|
switch f.Expr.(type) {
|
|
|
|
default:
|
|
|
|
return data.Consume(out).Get(docKeyAll, f.Field).Data(), nil
|
|
|
|
case *sql.All:
|
|
|
|
return data.Consume(out).Get(docKeyAll).Data(), nil
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return data.Consume(out).Get(docKeyAll).Data(), nil
|
2016-09-06 13:33:02 +00:00
|
|
|
}
|
2016-07-04 10:37:29 +00:00
|
|
|
}
|
|
|
|
|
2017-11-16 20:53:39 +00:00
|
|
|
return out, err
|
2016-07-04 10:37:29 +00:00
|
|
|
|
2016-02-27 01:00:19 +00:00
|
|
|
}
|
2016-09-06 13:33:02 +00:00
|
|
|
|
2017-11-16 20:53:39 +00:00
|
|
|
func (d *document) runSelect(ctx context.Context, stm *sql.SelectStatement) (interface{}, error) {
|
|
|
|
|
|
|
|
var ok bool
|
|
|
|
var err error
|
2018-04-14 18:14:47 +00:00
|
|
|
var met = _SELECT
|
2017-11-16 20:53:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
if err = d.init(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = d.setup(ctx); err != nil {
|
2017-11-16 20:53:39 +00:00
|
|
|
return nil, err
|
2016-09-06 13:33:02 +00:00
|
|
|
}
|
|
|
|
|
2017-11-16 20:53:39 +00:00
|
|
|
if d.doc == nil && !d.val.Exi() {
|
|
|
|
return nil, nil
|
2016-09-14 21:24:22 +00:00
|
|
|
}
|
|
|
|
|
2018-04-14 18:14:47 +00:00
|
|
|
if ok, err = d.allow(ctx, met); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if ok == false {
|
|
|
|
return nil, nil
|
2017-11-16 20:53:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ok, err = d.check(ctx, stm.Cond); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if ok == false {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2016-09-06 13:33:02 +00:00
|
|
|
|
2017-11-16 20:53:39 +00:00
|
|
|
return d.yield(ctx, stm, sql.ILLEGAL)
|
2016-09-06 13:33:02 +00:00
|
|
|
|
|
|
|
}
|