OmniSciDB  a987f07e93
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DBObject.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "DBObject.h"
18 #include "Catalog.h"
19 
21 
28 
49 
62 
80 
91 
93  switch (type) {
95  return "DATABASE";
96  case TableDBObjectType:
97  return "TABLE";
99  return "DASHBOARD";
100  case ViewDBObjectType:
101  return "VIEW";
102  case ServerDBObjectType:
103  return "SERVER";
104  default:
105  CHECK(false);
106  }
107  return "not possible";
108 }
109 
111  if (type.compare("DATABASE") == 0) {
112  return DatabaseDBObjectType;
113  } else if (type.compare("TABLE") == 0) {
114  return TableDBObjectType;
115  } else if (type.compare("DASHBOARD") == 0) {
116  return DashboardDBObjectType;
117  } else if (type.compare("VIEW") == 0) {
118  return ViewDBObjectType;
119  } else if (type.compare("SERVER") == 0) {
120  return ServerDBObjectType;
121  } else {
122  throw std::runtime_error("DB object type " + type + " is not supported.");
123  }
124 }
125 
126 DBObject::DBObject(const std::string& name, const DBObjectType& objectAndPermissionType)
127  : objectName_(name) {
128  objectType_ = objectAndPermissionType;
129  objectKey_.permissionType = objectAndPermissionType;
130  ownerId_ = 0;
131 }
132 
133 DBObject::DBObject(const int32_t id, const DBObjectType& objectAndPermissionType)
134  : objectName_("") {
135  objectType_ = objectAndPermissionType;
136  objectKey_.permissionType = objectAndPermissionType;
137  objectKey_.objectId = id;
138  ownerId_ = 0;
139 }
140 
142  : objectName_(object.objectName_), ownerId_(object.ownerId_) {
143  objectType_ = object.objectType_;
144  setObjectKey(object.objectKey_);
145  copyPrivileges(object);
146 }
147 
148 void DBObject::copyPrivileges(const DBObject& object) {
149  objectPrivs_ = object.objectPrivs_;
150 }
151 
153  objectPrivs_.privileges |= object.objectPrivs_.privileges;
154 }
155 
157  objectPrivs_.privileges &= ~(object.objectPrivs_.privileges);
158 }
159 
160 void DBObject::setPermissionType(const DBObjectType& permissionType) {
161  objectKey_.permissionType = permissionType;
162 }
163 void DBObject::setObjectType(const DBObjectType& objectType) {
164  objectType_ = objectType;
165 }
166 
167 std::vector<std::string> DBObject::toString() const {
168  std::vector<std::string> objectKey;
169  switch (objectKey_.permissionType) {
171  objectKey.push_back(std::to_string(objectKey_.permissionType));
172  objectKey.push_back(std::to_string(objectKey_.dbId));
173  objectKey.push_back(std::to_string(-1));
174  break;
175  case TableDBObjectType:
177  case ViewDBObjectType:
178  case ServerDBObjectType:
179  objectKey.push_back(std::to_string(objectKey_.permissionType));
180  objectKey.push_back(std::to_string(objectKey_.dbId));
181  objectKey.push_back(std::to_string(objectKey_.objectId));
182  break;
183  default: {
184  CHECK(false);
185  }
186  }
187  return objectKey;
188 }
189 
192  if (!getName().empty()) {
194  if (!Catalog_Namespace::SysCatalog::instance().getMetadataForDB(getName(), db)) {
195  throw std::runtime_error("Failure generating DB object key. Database " + getName() +
196  " does not exist.");
197  }
198  objectKey_.dbId = db.dbId;
199  ownerId_ = db.dbOwner;
200  objectName_ = db.dbName;
201  } else {
202  objectKey_.dbId = 0; // very special case only used for initialisation of a role
203  }
204 }
205 
207  switch (objectType_) {
208  case DatabaseDBObjectType: {
209  loadKey();
210  break;
211  }
212  case ServerDBObjectType: {
213  objectKey_.dbId = catalog.getCurrentDB().dbId;
214 
215  if (!getName().empty()) {
216  auto server = catalog.getForeignServer(getName());
217  if (!server) {
218  throw std::runtime_error("Failure generating DB object key. Server " +
219  getName() + " does not exist.");
220  }
221  objectKey_.objectId = server->id;
222  ownerId_ = server->user_id;
223  } else {
224  ownerId_ = catalog.getCurrentDB().dbOwner;
225  }
226 
227  break;
228  }
229  case ViewDBObjectType:
230  case TableDBObjectType: {
231  objectKey_.dbId = catalog.getCurrentDB().dbId;
232 
233  if (!getName().empty()) {
234  auto table =
235  catalog.getMetadataForTable(getName(), false /* do not populate fragments */);
236  if (!table) {
237  throw std::runtime_error("Failure generating DB object key. Table/View " +
238  getName() + " does not exist.");
239  }
240  objectKey_.objectId = table->tableId;
241  ownerId_ = table->userId;
242  } else {
243  ownerId_ = catalog.getCurrentDB().dbOwner;
244  }
245 
246  break;
247  }
248  case DashboardDBObjectType: {
249  objectKey_.dbId = catalog.getCurrentDB().dbId;
250 
251  if (objectKey_.objectId > 0) {
252  auto dashboard = catalog.getMetadataForDashboard(objectKey_.objectId);
253  if (!dashboard) {
254  throw std::runtime_error(
255  "Failure generating DB object key. Dashboard with ID " +
256  std::to_string(objectKey_.objectId) + " does not exist.");
257  }
258  objectName_ = dashboard->dashboardName;
259  ownerId_ = dashboard->userId;
260  } else {
261  ownerId_ = catalog.getCurrentDB().dbOwner;
262  }
263 
264  break;
265  }
266  default:
267  CHECK(false);
268  }
269 }
270 
271 DBObjectKey DBObjectKey::fromString(const std::vector<std::string>& key,
272  const DBObjectType& type) {
273  DBObjectKey objectKey;
274  switch (type) {
276  objectKey.permissionType = std::stoi(key[0]);
277  objectKey.dbId = std::stoi(key[1]);
278  break;
279  case ServerDBObjectType:
280  case TableDBObjectType:
281  case ViewDBObjectType:
283  objectKey.permissionType = std::stoi(key[0]);
284  objectKey.dbId = std::stoi(key[1]);
285  objectKey.objectId = std::stoi(key[2]);
286  break;
287  default:
288  CHECK(false);
289  }
290  return objectKey;
291 }
static const AccessPrivileges TRUNCATE_VIEW
Definition: DBObject.h:184
static const AccessPrivileges VIEW_SQL_EDITOR
Definition: DBObject.h:152
static const int32_t SERVER_USAGE
Definition: DBObject.h:129
static const AccessPrivileges VIEW_DASHBOARD
Definition: DBObject.h:171
static const int32_t DROP_VIEW
Definition: DBObject.h:113
static const int32_t ALTER_SERVER
Definition: DBObject.h:128
static const AccessPrivileges DROP_SERVER
Definition: DBObject.h:189
static const int32_t SELECT_FROM_VIEW
Definition: DBObject.h:114
class for a per-database catalog. also includes metadata for the current database and the current use...
Definition: Catalog.h:132
static const int32_t UPDATE_IN_VIEW
Definition: DBObject.h:116
static const AccessPrivileges ALL_DATABASE
Definition: DBObject.h:151
void copyPrivileges(const DBObject &object)
Definition: DBObject.cpp:148
static const AccessPrivileges ALTER_TABLE
Definition: DBObject.h:165
DBObjectType
Definition: DBObject.h:40
static const int32_t CREATE_VIEW
Definition: DBObject.h:112
void updatePrivileges(const DBObject &object)
Definition: DBObject.cpp:152
static const AccessPrivileges TRUNCATE_TABLE
Definition: DBObject.h:164
static const AccessPrivileges ALL_TABLE_MIGRATE
Definition: DBObject.h:156
const foreign_storage::ForeignServer * getForeignServer(const std::string &server_name) const
Definition: Catalog.cpp:3123
static const int32_t ALL
Definition: DBObject.h:77
static const AccessPrivileges INSERT_INTO_TABLE
Definition: DBObject.h:161
void revokePrivileges(const DBObject &object)
Definition: DBObject.cpp:156
static const AccessPrivileges CREATE_DASHBOARD
Definition: DBObject.h:170
static const AccessPrivileges SERVER_USAGE
Definition: DBObject.h:191
void setObjectKey(const DBObjectKey &objectKey)
Definition: DBObject.h:225
int32_t objectId
Definition: DBObject.h:55
static const AccessPrivileges UPDATE_IN_VIEW
Definition: DBObject.h:182
static const int32_t ALTER_TABLE
Definition: DBObject.h:93
int32_t ownerId_
Definition: DBObject.h:250
static const AccessPrivileges SELECT_FROM_TABLE
Definition: DBObject.h:160
DBObject(const std::string &name, const DBObjectType &objectAndPermissionType)
Definition: DBObject.cpp:126
AccessPrivileges objectPrivs_
Definition: DBObject.h:249
std::string to_string(char const *&&v)
static const AccessPrivileges ALL_VIEW
Definition: DBObject.h:177
DBObjectType DBObjectTypeFromString(const std::string &type)
Definition: DBObject.cpp:110
static const AccessPrivileges ALTER_SERVER
Definition: DBObject.h:190
DBObjectKey objectKey_
Definition: DBObject.h:248
This file contains the class specification and related data structures for Catalog.
static const AccessPrivileges INSERT_INTO_VIEW
Definition: DBObject.h:181
static const int32_t ALL_MIGRATE
Definition: DBObject.h:120
static DBObjectKey fromString(const std::vector< std::string > &key, const DBObjectType &type)
Definition: DBObject.cpp:271
static SysCatalog & instance()
Definition: SysCatalog.h:341
void setPermissionType(const DBObjectType &permissionType)
Definition: DBObject.cpp:160
const DBMetadata & getCurrentDB() const
Definition: Catalog.h:248
static const AccessPrivileges DROP_TABLE
Definition: DBObject.h:159
std::vector< std::string > toString() const
Definition: DBObject.cpp:167
static const int32_t ALL_MIGRATE
Definition: DBObject.h:95
static const int32_t DELETE_FROM_TABLE
Definition: DBObject.h:91
static const AccessPrivileges ALL_DASHBOARD_MIGRATE
Definition: DBObject.h:168
Class specification and related data structures for DBObject class.
static const int32_t TRUNCATE_TABLE
Definition: DBObject.h:92
const DashboardDescriptor * getMetadataForDashboard(const std::string &userId, const std::string &dashName) const
static const AccessPrivileges ALL_SERVER
Definition: DBObject.h:187
static const AccessPrivileges CREATE_SERVER
Definition: DBObject.h:188
static const AccessPrivileges DELETE_FROM_TABLE
Definition: DBObject.h:163
std::string getName() const
Definition: DBObject.h:219
static const AccessPrivileges NONE
Definition: DBObject.h:148
std::string objectName_
Definition: DBObject.h:246
static const int32_t ALL
Definition: DBObject.h:111
static const int32_t EDIT_DASHBOARD
Definition: DBObject.h:104
static const int32_t DELETE_DASHBOARD
Definition: DBObject.h:102
static const AccessPrivileges CREATE_TABLE
Definition: DBObject.h:158
static const int32_t INSERT_INTO_TABLE
Definition: DBObject.h:89
static const AccessPrivileges DELETE_FROM_VIEW
Definition: DBObject.h:183
std::string DBObjectTypeToString(DBObjectType type)
Definition: DBObject.cpp:92
static const int32_t CREATE_SERVER
Definition: DBObject.h:126
void loadKey()
Definition: DBObject.cpp:190
void setObjectType(const DBObjectType &objectType)
Definition: DBObject.cpp:163
static const int32_t ALL_MIGRATE
Definition: DBObject.h:106
static const AccessPrivileges ALL_VIEW_MIGRATE
Definition: DBObject.h:176
static const AccessPrivileges SELECT_FROM_VIEW
Definition: DBObject.h:180
static const int32_t ACCESS
Definition: DBObject.h:81
static const int32_t ALL
Definition: DBObject.h:125
int32_t dbId
Definition: DBObject.h:54
static const int32_t ALL
Definition: DBObject.h:85
static const AccessPrivileges ALL_DASHBOARD
Definition: DBObject.h:169
static const AccessPrivileges ACCESS
Definition: DBObject.h:153
static const int32_t VIEW_DASHBOARD
Definition: DBObject.h:103
static const AccessPrivileges ALL_TABLE
Definition: DBObject.h:157
static const int32_t VIEW_SQL_EDITOR
Definition: DBObject.h:80
static const int32_t DROP_TABLE
Definition: DBObject.h:87
static const int32_t INSERT_INTO_VIEW
Definition: DBObject.h:115
static const AccessPrivileges DROP_VIEW
Definition: DBObject.h:179
static const int32_t ALL
Definition: DBObject.h:100
#define CHECK(condition)
Definition: Logger.h:289
static const int32_t DELETE_FROM_VIEW
Definition: DBObject.h:117
static const AccessPrivileges CREATE_VIEW
Definition: DBObject.h:178
static const int32_t CREATE_TABLE
Definition: DBObject.h:86
static const int32_t CREATE_DASHBOARD
Definition: DBObject.h:101
int32_t permissionType
Definition: DBObject.h:53
static const AccessPrivileges DELETE_DASHBOARD
Definition: DBObject.h:173
static const int32_t SELECT_FROM_TABLE
Definition: DBObject.h:88
const TableDescriptor * getMetadataForTable(const std::string &tableName, const bool populateFragmenter=true) const
Returns a pointer to a const TableDescriptor struct matching the provided tableName.
int64_t privileges
Definition: DBObject.h:133
string name
Definition: setup.in.py:72
static const AccessPrivileges EDIT_DASHBOARD
Definition: DBObject.h:172
static const AccessPrivileges UPDATE_IN_TABLE
Definition: DBObject.h:162
static const int32_t UPDATE_IN_TABLE
Definition: DBObject.h:90
static const int32_t TRUNCATE_VIEW
Definition: DBObject.h:118
static const int32_t DROP_SERVER
Definition: DBObject.h:127
DBObjectType objectType_
Definition: DBObject.h:247