diff --git a/db/fetch.go b/db/fetch.go index fd476126..cd9b059c 100644 --- a/db/fetch.go +++ b/db/fetch.go @@ -339,12 +339,18 @@ func (e *executor) fetchPaths(ctx context.Context, doc *data.Doc, exprs ...sql.E func (e *executor) fetchThing(ctx context.Context, val *sql.Thing, doc *data.Doc) (interface{}, error) { + ver, err := e.fetchVersion(ctx, ctx.Value(ctxKeyVersion)) + if err != nil { + return nil, err + } + res, err := e.executeSelect(ctx, &sql.SelectStatement{ KV: cnf.Settings.DB.Base, NS: ctx.Value(ctxKeyNs).(string), DB: ctx.Value(ctxKeyDb).(string), Expr: []*sql.Field{{Expr: &sql.All{}}}, What: []sql.Expr{val}, + Version: sql.Expr(ver), Parallel: 1, }) @@ -362,12 +368,18 @@ func (e *executor) fetchThing(ctx context.Context, val *sql.Thing, doc *data.Doc func (e *executor) fetchArray(ctx context.Context, val []interface{}, doc *data.Doc) (interface{}, error) { + ver, err := e.fetchVersion(ctx, ctx.Value(ctxKeyVersion)) + if err != nil { + return nil, err + } + res, err := e.executeSelect(ctx, &sql.SelectStatement{ KV: cnf.Settings.DB.Base, NS: ctx.Value(ctxKeyNs).(string), DB: ctx.Value(ctxKeyDb).(string), Expr: []*sql.Field{{Expr: &sql.All{}}}, What: []sql.Expr{val}, + Version: sql.Expr(ver), Parallel: 1, }) @@ -442,6 +454,10 @@ func (e *executor) fetchStart(ctx context.Context, val sql.Expr) (int, error) { func (e *executor) fetchVersion(ctx context.Context, val sql.Expr) (int64, error) { + if v, ok := val.(int64); ok { + return v, nil + } + v, err := e.fetch(ctx, val, nil) if err != nil { return math.MaxInt64, err diff --git a/db/select.go b/db/select.go index 80b4c5b7..0c28dd0e 100644 --- a/db/select.go +++ b/db/select.go @@ -24,6 +24,8 @@ import ( func (e *executor) executeSelect(ctx context.Context, stm *sql.SelectStatement) ([]interface{}, error) { + ctx = context.WithValue(ctx, ctxKeyVersion, stm.Version) + var what sql.Exprs for _, val := range stm.What { diff --git a/db/vars.go b/db/vars.go index e8fc7b83..966fcf06 100644 --- a/db/vars.go +++ b/db/vars.go @@ -35,17 +35,18 @@ const ( ) const ( - ctxKeyId = "id" - ctxKeyNs = "ns" - ctxKeyDb = "db" - ctxKeyDive = "dive" - ctxKeyVars = "vars" - ctxKeySubs = "subs" - ctxKeySpec = "spec" - ctxKeyKeep = "keep" - ctxKeyAuth = "auth" - ctxKeyKind = "kind" - ctxKeyScope = "scope" + ctxKeyId = "id" + ctxKeyNs = "ns" + ctxKeyDb = "db" + ctxKeyDive = "dive" + ctxKeyVars = "vars" + ctxKeySubs = "subs" + ctxKeySpec = "spec" + ctxKeyKeep = "keep" + ctxKeyAuth = "auth" + ctxKeyKind = "kind" + ctxKeyScope = "scope" + ctxKeyVersion = "version" ) const (