OmniSciDB  f632821e96
DBObject.cpp
Go to the documentation of this file.
1 /*
2  * File: DBObject.cpp
3  * Author: norair
4  *
5  * Created on May 16, 2017, 03:30 PM
6  */
7 
8 #include "DBObject.h"
9 #include "Catalog.h"
10 
12 
19 
40 
53 
71 
80 
82  switch (type) {
84  return "DATABASE";
85  case TableDBObjectType:
86  return "TABLE";
88  return "DASHBOARD";
89  case ViewDBObjectType:
90  return "VIEW";
91  case ServerDBObjectType:
92  return "SERVER";
93  default:
94  CHECK(false);
95  }
96  return "not possible";
97 }
98 
100  if (type.compare("DATABASE") == 0) {
101  return DatabaseDBObjectType;
102  } else if (type.compare("TABLE") == 0) {
103  return TableDBObjectType;
104  } else if (type.compare("DASHBOARD") == 0) {
105  return DashboardDBObjectType;
106  } else if (type.compare("VIEW") == 0) {
107  return ViewDBObjectType;
108  } else if (type.compare("SERVER") == 0) {
109  return ServerDBObjectType;
110  } else {
111  throw std::runtime_error("DB object type " + type + " is not supported.");
112  }
113 }
114 
115 DBObject::DBObject(const std::string& name, const DBObjectType& objectAndPermissionType)
116  : objectName_(name) {
117  objectType_ = objectAndPermissionType;
118  objectKey_.permissionType = objectAndPermissionType;
119  ownerId_ = 0;
120 }
121 
122 DBObject::DBObject(const int32_t id, const DBObjectType& objectAndPermissionType)
123  : objectName_("") {
124  objectType_ = objectAndPermissionType;
125  objectKey_.permissionType = objectAndPermissionType;
126  objectKey_.objectId = id;
127  ownerId_ = 0;
128 }
129 
131  : objectName_(object.objectName_), ownerId_(object.ownerId_) {
132  objectType_ = object.objectType_;
133  setObjectKey(object.objectKey_);
134  copyPrivileges(object);
135 }
136 
137 void DBObject::copyPrivileges(const DBObject& object) {
138  objectPrivs_ = object.objectPrivs_;
139 }
140 
142  objectPrivs_.privileges |= object.objectPrivs_.privileges;
143 }
144 
146  objectPrivs_.privileges &= ~(object.objectPrivs_.privileges);
147 }
148 
149 void DBObject::setPermissionType(const DBObjectType& permissionType) {
150  objectKey_.permissionType = permissionType;
151 }
152 void DBObject::setObjectType(const DBObjectType& objectType) {
153  objectType_ = objectType;
154 }
155 
156 std::vector<std::string> DBObject::toString() const {
157  std::vector<std::string> objectKey;
158  switch (objectKey_.permissionType) {
160  objectKey.push_back(std::to_string(objectKey_.permissionType));
161  objectKey.push_back(std::to_string(objectKey_.dbId));
162  objectKey.push_back(std::to_string(-1));
163  break;
164  case TableDBObjectType:
166  case ViewDBObjectType:
167  case ServerDBObjectType:
168  objectKey.push_back(std::to_string(objectKey_.permissionType));
169  objectKey.push_back(std::to_string(objectKey_.dbId));
170  objectKey.push_back(std::to_string(objectKey_.objectId));
171  break;
172  default: {
173  CHECK(false);
174  }
175  }
176  return objectKey;
177 }
178 
181  if (!getName().empty()) {
183  if (!Catalog_Namespace::SysCatalog::instance().getMetadataForDB(getName(), db)) {
184  throw std::runtime_error("Failure generating DB object key. Database " + getName() +
185  " does not exist.");
186  }
187  objectKey_.dbId = db.dbId;
188  ownerId_ = db.dbOwner;
189  } else {
190  objectKey_.dbId = 0; // very special case only used for initialisation of a role
191  }
192 }
193 
195  switch (objectType_) {
196  case DatabaseDBObjectType: {
197  loadKey();
198  break;
199  }
200  case ServerDBObjectType: {
201  objectKey_.dbId = catalog.getCurrentDB().dbId;
202 
203  if (!getName().empty()) {
204  auto server = catalog.getForeignServer(getName());
205  if (!server) {
206  throw std::runtime_error("Failure generating DB object key. Server " +
207  getName() + " does not exist.");
208  }
209  objectKey_.objectId = server->id;
210  ownerId_ = server->user_id;
211  } else {
212  ownerId_ = catalog.getCurrentDB().dbOwner;
213  }
214 
215  break;
216  }
217  case ViewDBObjectType:
218  case TableDBObjectType: {
219  objectKey_.dbId = catalog.getCurrentDB().dbId;
220 
221  if (!getName().empty()) {
222  auto table =
223  catalog.getMetadataForTable(getName(), false /* do not populate fragments */);
224  if (!table) {
225  throw std::runtime_error("Failure generating DB object key. Table/View " +
226  getName() + " does not exist.");
227  }
228  objectKey_.objectId = table->tableId;
229  ownerId_ = table->userId;
230  } else {
231  ownerId_ = catalog.getCurrentDB().dbOwner;
232  }
233 
234  break;
235  }
236  case DashboardDBObjectType: {
237  objectKey_.dbId = catalog.getCurrentDB().dbId;
238 
239  if (objectKey_.objectId > 0) {
240  auto dashboard = catalog.getMetadataForDashboard(objectKey_.objectId);
241  if (!dashboard) {
242  throw std::runtime_error(
243  "Failure generating DB object key. Dashboard with ID " +
244  std::to_string(objectKey_.objectId) + " does not exist.");
245  }
246  objectName_ = dashboard->dashboardName;
247  ownerId_ = dashboard->userId;
248  } else {
249  ownerId_ = catalog.getCurrentDB().dbOwner;
250  }
251 
252  break;
253  }
254  default:
255  CHECK(false);
256  }
257 }
258 
259 DBObjectKey DBObjectKey::fromString(const std::vector<std::string>& key,
260  const DBObjectType& type) {
261  DBObjectKey objectKey;
262  switch (type) {
264  objectKey.permissionType = std::stoi(key[0]);
265  objectKey.dbId = std::stoi(key[1]);
266  break;
267  case ServerDBObjectType:
268  case TableDBObjectType:
269  case ViewDBObjectType:
271  objectKey.permissionType = std::stoi(key[0]);
272  objectKey.dbId = std::stoi(key[1]);
273  objectKey.objectId = std::stoi(key[2]);
274  break;
275  default:
276  CHECK(false);
277  }
278  return objectKey;
279 }
static const AccessPrivileges TRUNCATE_VIEW
Definition: DBObject.h:185
static const AccessPrivileges VIEW_SQL_EDITOR
Definition: DBObject.h:153
static const AccessPrivileges VIEW_DASHBOARD
Definition: DBObject.h:172
static const int32_t DROP_VIEW
Definition: DBObject.h:115
static const int32_t ALTER_SERVER
Definition: DBObject.h:130
static const AccessPrivileges DROP_SERVER
Definition: DBObject.h:190
static const int32_t SELECT_FROM_VIEW
Definition: DBObject.h:116
class for a per-database catalog. also includes metadata for the current database and the current use...
Definition: Catalog.h:97
static const int32_t UPDATE_IN_VIEW
Definition: DBObject.h:118
static const AccessPrivileges ALL_DATABASE
Definition: DBObject.h:152
void copyPrivileges(const DBObject &object)
Definition: DBObject.cpp:137
const TableDescriptor * getMetadataForTable(const std::string &tableName, const bool populateFragmenter=true) const
Returns a pointer to a const TableDescriptor struct matching the provided tableName.
static const AccessPrivileges ALTER_TABLE
Definition: DBObject.h:166
DBObjectType
Definition: DBObject.h:42
static const int32_t CREATE_VIEW
Definition: DBObject.h:114
void updatePrivileges(const DBObject &object)
Definition: DBObject.cpp:141
static const AccessPrivileges TRUNCATE_TABLE
Definition: DBObject.h:165
static const AccessPrivileges ALL_TABLE_MIGRATE
Definition: DBObject.h:157
static const int32_t ALL
Definition: DBObject.h:79
static const AccessPrivileges INSERT_INTO_TABLE
Definition: DBObject.h:162
void revokePrivileges(const DBObject &object)
Definition: DBObject.cpp:145
static const AccessPrivileges CREATE_DASHBOARD
Definition: DBObject.h:171
void setObjectKey(const DBObjectKey &objectKey)
Definition: DBObject.h:215
int32_t objectId
Definition: DBObject.h:57
static const AccessPrivileges UPDATE_IN_VIEW
Definition: DBObject.h:183
static const int32_t ALTER_TABLE
Definition: DBObject.h:95
int32_t ownerId_
Definition: DBObject.h:235
static const AccessPrivileges SELECT_FROM_TABLE
Definition: DBObject.h:161
DBObject(const std::string &name, const DBObjectType &objectAndPermissionType)
Definition: DBObject.cpp:115
AccessPrivileges objectPrivs_
Definition: DBObject.h:234
std::string to_string(char const *&&v)
static const AccessPrivileges ALL_VIEW
Definition: DBObject.h:178
DBObjectType DBObjectTypeFromString(const std::string &type)
Definition: DBObject.cpp:99
static const AccessPrivileges ALTER_SERVER
Definition: DBObject.h:191
DBObjectKey objectKey_
Definition: DBObject.h:233
This file contains the class specification and related data structures for Catalog.
static const AccessPrivileges INSERT_INTO_VIEW
Definition: DBObject.h:182
static const int32_t ALL_MIGRATE
Definition: DBObject.h:122
static DBObjectKey fromString(const std::vector< std::string > &key, const DBObjectType &type)
Definition: DBObject.cpp:259
static SysCatalog & instance()
Definition: SysCatalog.h:286
void setPermissionType(const DBObjectType &permissionType)
Definition: DBObject.cpp:149
std::string getName() const
Definition: DBObject.h:209
static const AccessPrivileges DROP_TABLE
Definition: DBObject.h:160
static const int32_t ALL_MIGRATE
Definition: DBObject.h:97
static const int32_t DELETE_FROM_TABLE
Definition: DBObject.h:93
static const AccessPrivileges ALL_DASHBOARD_MIGRATE
Definition: DBObject.h:169
static const int32_t TRUNCATE_TABLE
Definition: DBObject.h:94
static const AccessPrivileges ALL_SERVER
Definition: DBObject.h:188
static const AccessPrivileges CREATE_SERVER
Definition: DBObject.h:189
static const AccessPrivileges DELETE_FROM_TABLE
Definition: DBObject.h:164
static const AccessPrivileges NONE
Definition: DBObject.h:149
std::string objectName_
Definition: DBObject.h:231
const DBMetadata & getCurrentDB() const
Definition: Catalog.h:208
static const int32_t ALL
Definition: DBObject.h:113
static const int32_t EDIT_DASHBOARD
Definition: DBObject.h:106
static const int32_t DELETE_DASHBOARD
Definition: DBObject.h:104
static const AccessPrivileges CREATE_TABLE
Definition: DBObject.h:159
static const int32_t INSERT_INTO_TABLE
Definition: DBObject.h:91
static const AccessPrivileges DELETE_FROM_VIEW
Definition: DBObject.h:184
static const int32_t CREATE_SERVER
Definition: DBObject.h:128
void loadKey()
Definition: DBObject.cpp:179
void setObjectType(const DBObjectType &objectType)
Definition: DBObject.cpp:152
static const int32_t ALL_MIGRATE
Definition: DBObject.h:108
static const AccessPrivileges ALL_VIEW_MIGRATE
Definition: DBObject.h:177
static const AccessPrivileges SELECT_FROM_VIEW
Definition: DBObject.h:181
static const int32_t ACCESS
Definition: DBObject.h:83
static const int32_t ALL
Definition: DBObject.h:127
int32_t dbId
Definition: DBObject.h:56
static const int32_t ALL
Definition: DBObject.h:87
static const AccessPrivileges ALL_DASHBOARD
Definition: DBObject.h:170
static const AccessPrivileges ACCESS
Definition: DBObject.h:154
static const int32_t VIEW_DASHBOARD
Definition: DBObject.h:105
static const AccessPrivileges ALL_TABLE
Definition: DBObject.h:158
static const int32_t VIEW_SQL_EDITOR
Definition: DBObject.h:82
static const int32_t DROP_TABLE
Definition: DBObject.h:89
const foreign_storage::ForeignServer * getForeignServer(const std::string &server_name) const
Definition: Catalog.cpp:2495
static const int32_t INSERT_INTO_VIEW
Definition: DBObject.h:117
static const AccessPrivileges DROP_VIEW
Definition: DBObject.h:180
static const int32_t ALL
Definition: DBObject.h:102
#define CHECK(condition)
Definition: Logger.h:197
static const int32_t DELETE_FROM_VIEW
Definition: DBObject.h:119
static const AccessPrivileges CREATE_VIEW
Definition: DBObject.h:179
static const int32_t CREATE_TABLE
Definition: DBObject.h:88
const DashboardDescriptor * getMetadataForDashboard(const std::string &userId, const std::string &dashName) const
static const int32_t CREATE_DASHBOARD
Definition: DBObject.h:103
int32_t permissionType
Definition: DBObject.h:55
std::vector< std::string > toString() const
Definition: DBObject.cpp:156
static const AccessPrivileges DELETE_DASHBOARD
Definition: DBObject.h:174
static const int32_t SELECT_FROM_TABLE
Definition: DBObject.h:90
int64_t privileges
Definition: DBObject.h:134
static const AccessPrivileges EDIT_DASHBOARD
Definition: DBObject.h:173
static const AccessPrivileges UPDATE_IN_TABLE
Definition: DBObject.h:163
static const int32_t UPDATE_IN_TABLE
Definition: DBObject.h:92
static const int32_t TRUNCATE_VIEW
Definition: DBObject.h:120
static const int32_t DROP_SERVER
Definition: DBObject.h:129
DBObjectType objectType_
Definition: DBObject.h:232
std::string ObjectPermissionTypeToString(DBObjectType type)
Definition: DBObject.cpp:81