1//  Copyright (c) 2014 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 AlterIndex struct {
23	statementBase
24
25	keyspace *KeyspaceRef        `json:"keyspace"`
26	name     string              `json:"name"`
27	using    datastore.IndexType `json:"using"`
28	with     value.Value         `json:"with"`
29}
30
31func NewAlterIndex(keyspace *KeyspaceRef, name string, using datastore.IndexType, with value.Value) *AlterIndex {
32	rv := &AlterIndex{
33		keyspace: keyspace,
34		name:     name,
35		using:    using,
36		with:     with,
37	}
38
39	rv.stmt = rv
40	return rv
41}
42
43func (this *AlterIndex) Accept(visitor Visitor) (interface{}, error) {
44	return visitor.VisitAlterIndex(this)
45}
46
47func (this *AlterIndex) Signature() value.Value {
48	return nil
49}
50
51func (this *AlterIndex) Formalize() error {
52	return nil
53}
54
55func (this *AlterIndex) MapExpressions(mapper expression.Mapper) error {
56	return nil
57}
58
59func (this *AlterIndex) Expressions() expression.Expressions {
60	return nil
61}
62
63/*
64Returns all required privileges.
65*/
66func (this *AlterIndex) Privileges() (*auth.Privileges, errors.Error) {
67	privs := auth.NewPrivileges()
68	fullName := this.keyspace.FullName()
69	privs.Add(fullName, auth.PRIV_QUERY_ALTER_INDEX)
70	return privs, nil
71}
72
73func (this *AlterIndex) Keyspace() *KeyspaceRef {
74	return this.keyspace
75}
76
77func (this *AlterIndex) Name() string {
78	return this.name
79}
80
81func (this *AlterIndex) Using() datastore.IndexType {
82	return this.using
83}
84
85func (this *AlterIndex) With() value.Value {
86	return this.with
87}
88
89func (this *AlterIndex) MarshalJSON() ([]byte, error) {
90	r := map[string]interface{}{"type": "alterIndex"}
91	r["keyspaceRef"] = this.keyspace
92	r["name"] = this.name
93	r["using"] = this.using
94	if this.with != nil {
95		r["with"] = this.with
96	}
97	return json.Marshal(r)
98}
99
100func (this *AlterIndex) Type() string {
101	return "ALTER_INDEX"
102}
103