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-02-20 01:11:50 +00:00
|
|
|
"github.com/abcum/surreal/log"
|
2016-02-27 01:00:19 +00:00
|
|
|
"github.com/abcum/surreal/sql"
|
2016-09-06 13:33:02 +00:00
|
|
|
"github.com/abcum/surreal/util/item"
|
|
|
|
"github.com/abcum/surreal/util/keys"
|
2016-02-27 01:00:19 +00:00
|
|
|
)
|
|
|
|
|
2017-02-09 20:43:24 +00:00
|
|
|
func (e *executor) executeSelectStatement(ast *sql.SelectStatement) (out []interface{}, err error) {
|
2016-10-29 11:28:20 +00:00
|
|
|
|
2017-02-20 01:11:50 +00:00
|
|
|
log.WithPrefix("sql").WithFields(map[string]interface{}{
|
|
|
|
"ns": ast.NS,
|
|
|
|
"db": ast.DB,
|
|
|
|
}).Debugln(ast)
|
|
|
|
|
2016-10-29 11:28:20 +00:00
|
|
|
for k, w := range ast.What {
|
|
|
|
if what, ok := w.(*sql.Param); ok {
|
|
|
|
ast.What[k] = e.ctx.Get(what.ID).Data()
|
|
|
|
}
|
|
|
|
}
|
2016-07-04 10:37:29 +00:00
|
|
|
|
2016-09-06 13:33:02 +00:00
|
|
|
for _, w := range ast.What {
|
2016-07-04 10:37:29 +00:00
|
|
|
|
2016-09-06 13:33:02 +00:00
|
|
|
if what, ok := w.(*sql.Thing); ok {
|
|
|
|
key := &keys.Thing{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: what.TB, ID: what.ID}
|
2017-02-09 20:43:24 +00:00
|
|
|
kv, _ := e.txn.Get(0, key.Encode())
|
2017-02-09 11:16:59 +00:00
|
|
|
doc := item.New(kv, e.txn, key, e.ctx)
|
2016-09-06 13:33:02 +00:00
|
|
|
if ret, err := detect(doc, ast); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if ret != nil {
|
|
|
|
out = append(out, ret)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if what, ok := w.(*sql.Table); ok {
|
2017-02-09 11:16:59 +00:00
|
|
|
key := &keys.Table{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: what.TB}
|
2017-02-09 20:43:24 +00:00
|
|
|
kvs, _ := e.txn.GetL(0, key.Encode())
|
2016-09-06 13:33:02 +00:00
|
|
|
for _, kv := range kvs {
|
2017-02-09 20:43:24 +00:00
|
|
|
doc := item.New(kv, e.txn, nil, e.ctx)
|
2016-09-06 13:33:02 +00:00
|
|
|
if ret, err := detect(doc, ast); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if ret != nil {
|
|
|
|
out = append(out, ret)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-04 10:37:29 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
2016-02-27 01:00:19 +00:00
|
|
|
}
|
2016-09-06 13:33:02 +00:00
|
|
|
|
|
|
|
func detect(doc *item.Doc, ast *sql.SelectStatement) (out interface{}, err error) {
|
|
|
|
|
|
|
|
if !doc.Check(ast.Cond) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-09-14 21:24:22 +00:00
|
|
|
if !doc.Allow("SELECT") {
|
2016-10-18 12:49:46 +00:00
|
|
|
return
|
2016-09-14 21:24:22 +00:00
|
|
|
}
|
|
|
|
|
2016-09-06 13:33:02 +00:00
|
|
|
out = doc.Blaze(ast)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|