|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2018 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | +// Author Ewout Prangsma |
| 21 | +// |
| 22 | + |
| 23 | +package driver |
| 24 | + |
| 25 | +import ( |
| 26 | + "context" |
| 27 | + "path" |
| 28 | +) |
| 29 | + |
| 30 | +type viewInfo struct { |
| 31 | + ID string `json:"id,omitempty"` |
| 32 | + Name string `json:"name,omitempty"` |
| 33 | + Type ViewType `json:"type,omitempty"` |
| 34 | +} |
| 35 | + |
| 36 | +type getViewResponse struct { |
| 37 | + Result []viewInfo `json:"result,omitempty"` |
| 38 | +} |
| 39 | + |
| 40 | +// View opens a connection to an existing view within the database. |
| 41 | +// If no collection with given name exists, an NotFoundError is returned. |
| 42 | +func (d *database) View(ctx context.Context, name string) (View, error) { |
| 43 | + escapedName := pathEscape(name) |
| 44 | + req, err := d.conn.NewRequest("GET", path.Join(d.relPath(), "_api/view", escapedName)) |
| 45 | + if err != nil { |
| 46 | + return nil, WithStack(err) |
| 47 | + } |
| 48 | + applyContextSettings(ctx, req) |
| 49 | + resp, err := d.conn.Do(ctx, req) |
| 50 | + if err != nil { |
| 51 | + return nil, WithStack(err) |
| 52 | + } |
| 53 | + if err := resp.CheckStatus(200); err != nil { |
| 54 | + return nil, WithStack(err) |
| 55 | + } |
| 56 | + var data viewInfo |
| 57 | + if err := resp.ParseBody("", &data); err != nil { |
| 58 | + return nil, WithStack(err) |
| 59 | + } |
| 60 | + view, err := newView(name, data.Type, d) |
| 61 | + if err != nil { |
| 62 | + return nil, WithStack(err) |
| 63 | + } |
| 64 | + return view, nil |
| 65 | +} |
| 66 | + |
| 67 | +// ViewExists returns true if a view with given name exists within the database. |
| 68 | +func (d *database) ViewExists(ctx context.Context, name string) (bool, error) { |
| 69 | + escapedName := pathEscape(name) |
| 70 | + req, err := d.conn.NewRequest("GET", path.Join(d.relPath(), "_api/view", escapedName)) |
| 71 | + if err != nil { |
| 72 | + return false, WithStack(err) |
| 73 | + } |
| 74 | + applyContextSettings(ctx, req) |
| 75 | + resp, err := d.conn.Do(ctx, req) |
| 76 | + if err != nil { |
| 77 | + return false, WithStack(err) |
| 78 | + } |
| 79 | + if err := resp.CheckStatus(200); err == nil { |
| 80 | + return true, nil |
| 81 | + } else if IsNotFound(err) { |
| 82 | + return false, nil |
| 83 | + } else { |
| 84 | + return false, WithStack(err) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +// Views returns a list of all views in the database. |
| 89 | +func (d *database) Views(ctx context.Context) ([]View, error) { |
| 90 | + req, err := d.conn.NewRequest("GET", path.Join(d.relPath(), "_api/view")) |
| 91 | + if err != nil { |
| 92 | + return nil, WithStack(err) |
| 93 | + } |
| 94 | + applyContextSettings(ctx, req) |
| 95 | + resp, err := d.conn.Do(ctx, req) |
| 96 | + if err != nil { |
| 97 | + return nil, WithStack(err) |
| 98 | + } |
| 99 | + if err := resp.CheckStatus(200); err != nil { |
| 100 | + return nil, WithStack(err) |
| 101 | + } |
| 102 | + var data getViewResponse |
| 103 | + if err := resp.ParseBody("", &data); err != nil { |
| 104 | + return nil, WithStack(err) |
| 105 | + } |
| 106 | + result := make([]View, 0, len(data.Result)) |
| 107 | + for _, info := range data.Result { |
| 108 | + view, err := newView(info.Name, info.Type, d) |
| 109 | + if err != nil { |
| 110 | + return nil, WithStack(err) |
| 111 | + } |
| 112 | + result = append(result, view) |
| 113 | + } |
| 114 | + return result, nil |
| 115 | +} |
| 116 | + |
| 117 | +// CreateArangoSearchView creates a new view of type ArangoSearch, |
| 118 | +// with given name and options, and opens a connection to it. |
| 119 | +// If a view with given name already exists within the database, a ConflictError is returned. |
| 120 | +func (d *database) CreateArangoSearchView(ctx context.Context, name string, options *ArangoSearchViewProperties) (ArangoSearchView, error) { |
| 121 | + input := struct { |
| 122 | + Name string `json:"name"` |
| 123 | + Type ViewType `json:"type"` |
| 124 | + ArangoSearchViewProperties // `json:"properties"` |
| 125 | + }{ |
| 126 | + Name: name, |
| 127 | + Type: ViewTypeArangoSearch, |
| 128 | + } |
| 129 | + if options != nil { |
| 130 | + input.ArangoSearchViewProperties = *options |
| 131 | + } |
| 132 | + req, err := d.conn.NewRequest("POST", path.Join(d.relPath(), "_api/view")) |
| 133 | + if err != nil { |
| 134 | + return nil, WithStack(err) |
| 135 | + } |
| 136 | + if _, err := req.SetBody(input); err != nil { |
| 137 | + return nil, WithStack(err) |
| 138 | + } |
| 139 | + applyContextSettings(ctx, req) |
| 140 | + resp, err := d.conn.Do(ctx, req) |
| 141 | + if err != nil { |
| 142 | + return nil, WithStack(err) |
| 143 | + } |
| 144 | + if err := resp.CheckStatus(201); err != nil { |
| 145 | + return nil, WithStack(err) |
| 146 | + } |
| 147 | + view, err := newView(name, input.Type, d) |
| 148 | + if err != nil { |
| 149 | + return nil, WithStack(err) |
| 150 | + } |
| 151 | + result, err := view.ArangoSearchView() |
| 152 | + if err != nil { |
| 153 | + return nil, WithStack(err) |
| 154 | + } |
| 155 | + |
| 156 | + return result, nil |
| 157 | +} |
0 commit comments