2016-09-06 13:35:38 +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 item
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/imdario/mergo"
|
|
|
|
|
|
|
|
"github.com/abcum/surreal/sql"
|
|
|
|
"github.com/abcum/surreal/util/data"
|
|
|
|
)
|
|
|
|
|
2017-03-02 10:47:10 +00:00
|
|
|
func (this *Doc) Merge(data sql.Expr) (err error) {
|
2016-09-06 13:35:38 +00:00
|
|
|
|
2016-10-24 16:03:28 +00:00
|
|
|
if err = this.setFld(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-10-14 22:01:44 +00:00
|
|
|
if err = this.defFld(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2016-09-06 13:35:38 +00:00
|
|
|
|
2016-10-24 16:03:28 +00:00
|
|
|
if err = this.setFld(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-02 10:47:10 +00:00
|
|
|
switch expr := data.(type) {
|
|
|
|
case *sql.DataExpression:
|
|
|
|
this.mrgSet(expr)
|
|
|
|
case *sql.DiffExpression:
|
|
|
|
this.mrgDpm(expr)
|
|
|
|
case *sql.MergeExpression:
|
|
|
|
this.mrgAny(expr)
|
|
|
|
case *sql.ContentExpression:
|
|
|
|
this.mrgAll(expr)
|
2016-09-06 13:35:38 +00:00
|
|
|
}
|
|
|
|
|
2016-10-14 22:01:44 +00:00
|
|
|
if err = this.setFld(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = this.mrgFld(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = this.setFld(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2016-09-14 21:28:00 +00:00
|
|
|
|
|
|
|
return
|
2016-09-06 13:35:38 +00:00
|
|
|
|
2016-09-14 21:28:00 +00:00
|
|
|
}
|
2016-09-06 13:35:38 +00:00
|
|
|
|
2016-09-14 21:28:00 +00:00
|
|
|
func (this *Doc) setFld() (err error) {
|
2016-09-06 13:35:38 +00:00
|
|
|
|
2016-10-29 11:44:49 +00:00
|
|
|
this.current.Set(this.id, "id")
|
2016-10-29 11:46:22 +00:00
|
|
|
this.current.Set(this.key.ID, "meta.id")
|
|
|
|
this.current.Set(this.key.TB, "meta.tb")
|
2016-09-06 13:35:38 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Doc) defFld() (err error) {
|
|
|
|
|
|
|
|
for _, fld := range this.fields {
|
|
|
|
|
2016-10-24 16:03:28 +00:00
|
|
|
this.current.Walk(func(key string, val interface{}) error {
|
|
|
|
|
|
|
|
v := this.current.Valid(key)
|
|
|
|
e := this.current.Exists(key)
|
|
|
|
|
|
|
|
if fld.Default != nil && (!e || !v && fld.Notnull) {
|
|
|
|
switch val := fld.Default.(type) {
|
|
|
|
case sql.Void, *sql.Void:
|
|
|
|
this.current.Del(key)
|
|
|
|
case sql.Null, *sql.Null:
|
|
|
|
this.current.Set(nil, key)
|
|
|
|
default:
|
|
|
|
this.current.Set(fld.Default, key)
|
|
|
|
case sql.Ident:
|
|
|
|
this.current.Set(this.current.Get(val.ID).Data(), key)
|
|
|
|
case *sql.Ident:
|
|
|
|
this.current.Set(this.current.Get(val.ID).Data(), key)
|
|
|
|
}
|
2016-09-06 13:35:38 +00:00
|
|
|
}
|
2016-10-24 16:03:28 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
|
2017-03-02 10:47:10 +00:00
|
|
|
}, fld.Name.String())
|
2016-09-06 13:35:38 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Doc) mrgFld() (err error) {
|
|
|
|
|
|
|
|
for _, fld := range this.fields {
|
2016-10-24 16:03:28 +00:00
|
|
|
if err = this.each(fld); err != nil {
|
2016-09-06 13:35:38 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Doc) mrgAll(expr *sql.ContentExpression) {
|
|
|
|
|
2017-03-02 10:47:10 +00:00
|
|
|
switch val := expr.Data.(type) {
|
|
|
|
case sql.Object:
|
|
|
|
this.current = data.Consume(map[string]interface{}(val))
|
|
|
|
case map[string]interface{}:
|
|
|
|
this.current = data.Consume(map[string]interface{}(val))
|
|
|
|
case *sql.Param:
|
|
|
|
switch val := this.runtime.Get(val.ID).Data().(type) {
|
|
|
|
case sql.Object:
|
|
|
|
this.current = data.Consume(map[string]interface{}(val))
|
|
|
|
case map[string]interface{}:
|
|
|
|
this.current = data.Consume(map[string]interface{}(val))
|
|
|
|
}
|
|
|
|
}
|
2016-09-06 13:35:38 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Doc) mrgAny(expr *sql.MergeExpression) {
|
|
|
|
|
2016-09-14 20:46:23 +00:00
|
|
|
lhs, _ := this.current.Data().(map[string]interface{})
|
2017-03-02 10:47:10 +00:00
|
|
|
rhs, _ := expr.Data.(map[string]interface{})
|
2016-09-06 13:35:38 +00:00
|
|
|
|
|
|
|
err := mergo.MapWithOverwrite(&lhs, rhs)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-09-14 20:46:23 +00:00
|
|
|
this.current = data.Consume(lhs)
|
2016-09-06 13:35:38 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Doc) mrgDpm(expr *sql.DiffExpression) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-03-02 10:47:10 +00:00
|
|
|
func (this *Doc) mrgSet(expr *sql.DataExpression) {
|
|
|
|
|
|
|
|
for _, part := range expr.Data {
|
2016-09-06 13:35:38 +00:00
|
|
|
|
2017-03-02 10:47:10 +00:00
|
|
|
lhs := this.getMrgItemLHS(part.LHS)
|
|
|
|
rhs := this.getMrgItemRHS(part.RHS)
|
2016-09-06 13:35:38 +00:00
|
|
|
|
2017-03-02 10:47:10 +00:00
|
|
|
if part.Op == sql.EQ {
|
|
|
|
switch part.RHS.(type) {
|
|
|
|
default:
|
|
|
|
this.current.Set(rhs, lhs)
|
|
|
|
case *sql.Void:
|
|
|
|
this.current.Del(lhs)
|
|
|
|
}
|
2016-09-06 13:35:38 +00:00
|
|
|
}
|
|
|
|
|
2017-03-02 10:47:10 +00:00
|
|
|
if part.Op == sql.INC {
|
|
|
|
this.current.Inc(rhs, lhs)
|
|
|
|
}
|
|
|
|
|
|
|
|
if part.Op == sql.DEC {
|
|
|
|
this.current.Dec(rhs, lhs)
|
|
|
|
}
|
2016-09-06 13:35:38 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-10-29 11:28:20 +00:00
|
|
|
func (this *Doc) getMrgItemLHS(expr sql.Expr) string {
|
2016-09-06 13:35:38 +00:00
|
|
|
|
|
|
|
switch val := expr.(type) {
|
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
case sql.Ident:
|
|
|
|
return val.ID
|
|
|
|
case *sql.Ident:
|
|
|
|
return val.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-10-29 11:28:20 +00:00
|
|
|
func (this *Doc) getMrgItemRHS(expr sql.Expr) interface{} {
|
2016-09-06 13:35:38 +00:00
|
|
|
|
|
|
|
switch val := expr.(type) {
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
case time.Time:
|
|
|
|
return val
|
|
|
|
case bool, int64, float64, string:
|
|
|
|
return val
|
|
|
|
case []interface{}, map[string]interface{}:
|
|
|
|
return val
|
2016-09-14 21:28:00 +00:00
|
|
|
case *sql.Thing:
|
|
|
|
return val
|
2016-10-29 11:28:20 +00:00
|
|
|
case *sql.Param:
|
|
|
|
return this.runtime.Get(val.ID).Data()
|
2016-09-06 13:35:38 +00:00
|
|
|
case *sql.Ident:
|
2016-10-29 11:28:20 +00:00
|
|
|
return this.current.Get(val.ID).Data()
|
2016-09-06 13:35:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|