1//  Copyright (c) 2016 Couchbase, Inc.
2//  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
3//  except in compliance with the License. You may obtain a copy of the License at
4//    http://www.apache.org/licenses/LICENSE-2.0
5//  Unless required by applicable law or agreed to in writing, software distributed under the
6//  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
7//  either express or implied. See the License for the specific language governing permissions
8//  and limitations under the License.
9
10package algebra
11
12import (
13	"encoding/json"
14
15	"github.com/couchbase/query/auth"
16	"github.com/couchbase/query/datastore"
17	"github.com/couchbase/query/errors"
18	"github.com/couchbase/query/expression"
19	"github.com/couchbase/query/value"
20)
21
22type InferKeyspace struct {
23	statementBase
24
25	keyspace *KeyspaceRef            `json:"keyspace"`
26	using    datastore.InferenceType `json:"using"`
27	with     value.Value             `json:"with"`
28}
29
30func NewInferKeyspace(keyspace *KeyspaceRef, using datastore.InferenceType,
31	with value.Value) *InferKeyspace {
32	rv := &InferKeyspace{
33		keyspace: keyspace,
34		using:    using,
35		with:     with,
36	}
37
38	rv.stmt = rv
39	return rv
40}
41
42func (this *InferKeyspace) Accept(visitor Visitor) (interface{}, error) {
43	return visitor.VisitInferKeyspace(this)
44}
45
46func (this *InferKeyspace) Signature() value.Value {
47	return nil
48}
49
50func (this *InferKeyspace) Formalize() error {
51	return nil
52}
53
54func (this *InferKeyspace) MapExpressions(mapper expression.Mapper) error {
55	return nil
56}
57
58func (this *InferKeyspace) Expressions() expression.Expressions {
59	return nil
60}
61
62/*
63Returns all required privileges.
64*/
65func (this *InferKeyspace) Privileges() (*auth.Privileges, errors.Error) {
66	privs, err := privilegesFromKeyspace(this.keyspace.Namespace(), this.keyspace.Keyspace())
67	if err != nil {
68		return privs, err
69	}
70
71	return privs, nil
72}
73
74func (this *InferKeyspace) Keyspace() *KeyspaceRef {
75	return this.keyspace
76}
77
78/*
79Returns the index type string for the using clause.
80*/
81func (this *InferKeyspace) Using() datastore.InferenceType {
82	return this.using
83}
84
85func (this *InferKeyspace) With() value.Value {
86	return this.with
87}
88
89func (this *InferKeyspace) MarshalJSON() ([]byte, error) {
90	r := map[string]interface{}{"type": "InferKeyspace"}
91	r["keyspaceRef"] = this.keyspace
92	r["using"] = this.using
93	r["with"] = this.with
94	return json.Marshal(r)
95}
96
97func (this *InferKeyspace) Type() string {
98	return "INFER"
99}
100