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
22/*
23Represents the Drop index ddl statement. Type DropIndex is
24a struct that contains fields mapping to each clause in the
25drop index statement, namely the keyspace and the index name.
26*/
27type DropIndex struct {
28	statementBase
29
30	keyspace *KeyspaceRef        `json:"keyspace"`
31	name     string              `json:"name"`
32	using    datastore.IndexType `json:"using"`
33}
34
35/*
36The function NewDropIndex returns a pointer to the
37DropIndex struct with the input argument values as fields.
38*/
39func NewDropIndex(keyspace *KeyspaceRef, name string, using datastore.IndexType) *DropIndex {
40	rv := &DropIndex{
41		keyspace: keyspace,
42		name:     name,
43		using:    using,
44	}
45
46	rv.stmt = rv
47	return rv
48}
49
50/*
51It calls the VisitDropIndex method by passing in the
52receiver and returns the interface. It is a visitor
53pattern.
54*/
55func (this *DropIndex) Accept(visitor Visitor) (interface{}, error) {
56	return visitor.VisitDropIndex(this)
57}
58
59/*
60Returns nil.
61*/
62func (this *DropIndex) Signature() value.Value {
63	return nil
64}
65
66/*
67Returns nil.
68*/
69func (this *DropIndex) Formalize() error {
70	return nil
71}
72
73/*
74Returns nil.
75*/
76func (this *DropIndex) MapExpressions(mapper expression.Mapper) error {
77	return nil
78}
79
80/*
81Returns all contained Expressions.
82*/
83func (this *DropIndex) Expressions() expression.Expressions {
84	return nil
85}
86
87/*
88Returns all required privileges.
89*/
90func (this *DropIndex) Privileges() (*auth.Privileges, errors.Error) {
91	privs := auth.NewPrivileges()
92	fullName := this.keyspace.FullName()
93	privs.Add(fullName, auth.PRIV_QUERY_DROP_INDEX)
94	return privs, nil
95}
96
97/*
98Return the keyspace.
99*/
100func (this *DropIndex) Keyspace() *KeyspaceRef {
101	return this.keyspace
102}
103
104/*
105Return the name of the index to be dropped.
106*/
107func (this *DropIndex) Name() string {
108	return this.name
109}
110
111/*
112Returns the index type string for the using clause.
113*/
114func (this *DropIndex) Using() datastore.IndexType {
115	return this.using
116}
117
118/*
119Marshals input receiver into byte array.
120*/
121func (this *DropIndex) MarshalJSON() ([]byte, error) {
122	r := map[string]interface{}{"type": "dropIndex"}
123	r["keyspaceRef"] = this.keyspace
124	r["name"] = this.name
125	r["using"] = this.using
126	return json.Marshal(r)
127}
128
129func (this *DropIndex) Type() string {
130	return "DROP_INDEX"
131}
132