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