OmniSciDB  c07336695a
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 
73  switch (type) {
75  return "DATABASE";
76  case TableDBObjectType:
77  return "TABLE";
79  return "DASHBOARD";
80  case ViewDBObjectType:
81  return "VIEW";
82  default:
83  CHECK(false);
84  }
85  return "not possible";
86 }
87 
89  if (type.compare("DATABASE") == 0) {
90  return DatabaseDBObjectType;
91  } else if (type.compare("TABLE") == 0) {
92  return TableDBObjectType;
93  } else if (type.compare("DASHBOARD") == 0) {
94  return DashboardDBObjectType;
95  } else if (type.compare("VIEW") == 0) {
96  return ViewDBObjectType;
97  } else {
98  throw std::runtime_error("DB object type " + type + " is not supported.");
99  }
100 }
101 
102 DBObject::DBObject(const std::string& name, const DBObjectType& objectAndPermissionType)
103  : objectName_(name) {
104  objectType_ = objectAndPermissionType;
105  objectKey_.permissionType = objectAndPermissionType;
106  ownerId_ = 0;
107 }
108 
109 DBObject::DBObject(const int32_t id, const DBObjectType& objectAndPermissionType)
110  : objectName_("") {
111  objectType_ = objectAndPermissionType;
112  objectKey_.permissionType = objectAndPermissionType;
113  objectKey_.objectId = id;
114  ownerId_ = 0;
115 }
116 
118  : objectName_(object.objectName_), ownerId_(object.ownerId_) {
119  objectType_ = object.objectType_;
120  setObjectKey(object.objectKey_);
121  copyPrivileges(object);
122 }
123 
124 void DBObject::copyPrivileges(const DBObject& object) {
125  objectPrivs_ = object.objectPrivs_;
126 }
127 
129  objectPrivs_.privileges |= object.objectPrivs_.privileges;
130 }
131 
133  objectPrivs_.privileges &= ~(object.objectPrivs_.privileges);
134 }
135 
136 void DBObject::setPermissionType(const DBObjectType& permissionType) {
137  objectKey_.permissionType = permissionType;
138 }
139 void DBObject::setObjectType(const DBObjectType& objectType) {
140  objectType_ = objectType;
141 }
142 
143 std::vector<std::string> DBObject::toString() const {
144  std::vector<std::string> objectKey;
145  switch (objectKey_.permissionType) {
147  objectKey.push_back(std::to_string(objectKey_.permissionType));
148  objectKey.push_back(std::to_string(objectKey_.dbId));
149  objectKey.push_back(std::to_string(-1));
150  break;
151  case TableDBObjectType:
153  case ViewDBObjectType:
154  objectKey.push_back(std::to_string(objectKey_.permissionType));
155  objectKey.push_back(std::to_string(objectKey_.dbId));
156  objectKey.push_back(std::to_string(objectKey_.objectId));
157  break;
158  default: {
159  CHECK(false);
160  }
161  }
162  return objectKey;
163 }
164 
167  if (!getName().empty()) {
169  if (!Catalog_Namespace::SysCatalog::instance().getMetadataForDB(getName(), db)) {
170  throw std::runtime_error("Failure generating DB object key. Database " + getName() +
171  " does not exist.");
172  }
173  objectKey_.dbId = db.dbId;
174  ownerId_ = db.dbOwner;
175  } else {
176  objectKey_.dbId = 0; // very special case only used for initialisation of a role
177  }
178 }
179 
181  switch (objectType_) {
182  case DatabaseDBObjectType: {
183  loadKey();
184  break;
185  }
186  case ViewDBObjectType:
187  case TableDBObjectType: {
188  objectKey_.dbId = catalog.getCurrentDB().dbId;
189 
190  if (!getName().empty()) {
191  auto table =
192  catalog.getMetadataForTable(getName(), false /* do not populate fragments */);
193  if (!table) {
194  throw std::runtime_error("Failure generating DB object key. Table/View " +
195  getName() + " does not exist.");
196  }
197  objectKey_.objectId = table->tableId;
198  ownerId_ = table->userId;
199  } else {
200  ownerId_ = catalog.getCurrentDB().dbOwner;
201  }
202 
203  break;
204  }
205  case DashboardDBObjectType: {
206  objectKey_.dbId = catalog.getCurrentDB().dbId;
207 
208  if (objectKey_.objectId > 0) {
209  auto dashboard = catalog.getMetadataForDashboard(objectKey_.objectId);
210  if (!dashboard) {
211  throw std::runtime_error(
212  "Failure generating DB object key. Dashboard with ID " +
213  std::to_string(objectKey_.objectId) + " does not exist.");
214  }
215  objectName_ = dashboard->dashboardName;
216  ownerId_ = dashboard->userId;
217  } else {
218  ownerId_ = catalog.getCurrentDB().dbOwner;
219  }
220 
221  break;
222  }
223  default:
224  CHECK(false);
225  }
226 }
227 
228 DBObjectKey DBObjectKey::fromString(const std::vector<std::string>& key,
229  const DBObjectType& type) {
230  DBObjectKey objectKey;
231  switch (type) {
233  objectKey.permissionType = std::stoi(key[0]);
234  objectKey.dbId = std::stoi(key[1]);
235  break;
236  case TableDBObjectType:
237  case ViewDBObjectType:
239  objectKey.permissionType = std::stoi(key[0]);
240  objectKey.dbId = std::stoi(key[1]);
241  objectKey.objectId = std::stoi(key[2]);
242  break;
243  default:
244  CHECK(false);
245  }
246  return objectKey;
247 }
static const AccessPrivileges TRUNCATE_VIEW
Definition: DBObject.h:177
static const AccessPrivileges VIEW_SQL_EDITOR
Definition: DBObject.h:145
static const AccessPrivileges VIEW_DASHBOARD
Definition: DBObject.h:164
static const int32_t DROP_VIEW
Definition: DBObject.h:114
static const int32_t SELECT_FROM_VIEW
Definition: DBObject.h:115
class for a per-database catalog. also includes metadata for the current database and the current use...
Definition: Catalog.h:81
static const int32_t UPDATE_IN_VIEW
Definition: DBObject.h:117
static const AccessPrivileges ALL_DATABASE
Definition: DBObject.h:144
void copyPrivileges(const DBObject &object)
Definition: DBObject.cpp:124
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:158
DBObjectType
Definition: DBObject.h:42
static const int32_t CREATE_VIEW
Definition: DBObject.h:113
void updatePrivileges(const DBObject &object)
Definition: DBObject.cpp:128
static const AccessPrivileges TRUNCATE_TABLE
Definition: DBObject.h:157
static const AccessPrivileges ALL_TABLE_MIGRATE
Definition: DBObject.h:149
static const int32_t ALL
Definition: DBObject.h:78
static const AccessPrivileges INSERT_INTO_TABLE
Definition: DBObject.h:154
void revokePrivileges(const DBObject &object)
Definition: DBObject.cpp:132
static const AccessPrivileges CREATE_DASHBOARD
Definition: DBObject.h:163
void setObjectKey(const DBObjectKey &objectKey)
Definition: DBObject.h:200
int32_t objectId
Definition: DBObject.h:56
static const AccessPrivileges UPDATE_IN_VIEW
Definition: DBObject.h:175
static const int32_t ALTER_TABLE
Definition: DBObject.h:94
int32_t ownerId_
Definition: DBObject.h:220
static const AccessPrivileges SELECT_FROM_TABLE
Definition: DBObject.h:153
DBObject(const std::string &name, const DBObjectType &objectAndPermissionType)
Definition: DBObject.cpp:102
AccessPrivileges objectPrivs_
Definition: DBObject.h:219
std::string to_string(char const *&&v)
static const AccessPrivileges ALL_VIEW
Definition: DBObject.h:170
DBObjectType DBObjectTypeFromString(const std::string &type)
Definition: DBObject.cpp:88
DBObjectKey objectKey_
Definition: DBObject.h:218
This file contains the class specification and related data structures for Catalog.
static const AccessPrivileges INSERT_INTO_VIEW
Definition: DBObject.h:174
static const int32_t ALL_MIGRATE
Definition: DBObject.h:121
static DBObjectKey fromString(const std::vector< std::string > &key, const DBObjectType &type)
Definition: DBObject.cpp:228
static SysCatalog & instance()
Definition: SysCatalog.h:240
void setPermissionType(const DBObjectType &permissionType)
Definition: DBObject.cpp:136
std::string getName() const
Definition: DBObject.h:195
static const AccessPrivileges DROP_TABLE
Definition: DBObject.h:152
static const int32_t ALL_MIGRATE
Definition: DBObject.h:96
static const int32_t DELETE_FROM_TABLE
Definition: DBObject.h:92
static const AccessPrivileges ALL_DASHBOARD_MIGRATE
Definition: DBObject.h:161
static const int32_t TRUNCATE_TABLE
Definition: DBObject.h:93
static const AccessPrivileges DELETE_FROM_TABLE
Definition: DBObject.h:156
static const AccessPrivileges NONE
Definition: DBObject.h:141
std::string objectName_
Definition: DBObject.h:216
const DBMetadata & getCurrentDB() const
Definition: Catalog.h:176
static const int32_t ALL
Definition: DBObject.h:112
static const int32_t EDIT_DASHBOARD
Definition: DBObject.h:105
static const int32_t DELETE_DASHBOARD
Definition: DBObject.h:103
static const AccessPrivileges CREATE_TABLE
Definition: DBObject.h:151
static const int32_t INSERT_INTO_TABLE
Definition: DBObject.h:90
static const AccessPrivileges DELETE_FROM_VIEW
Definition: DBObject.h:176
void loadKey()
Definition: DBObject.cpp:165
void setObjectType(const DBObjectType &objectType)
Definition: DBObject.cpp:139
static const int32_t ALL_MIGRATE
Definition: DBObject.h:107
static const AccessPrivileges ALL_VIEW_MIGRATE
Definition: DBObject.h:169
static const AccessPrivileges SELECT_FROM_VIEW
Definition: DBObject.h:173
static const int32_t ACCESS
Definition: DBObject.h:82
int32_t dbId
Definition: DBObject.h:55
static const int32_t ALL
Definition: DBObject.h:86
static const AccessPrivileges ALL_DASHBOARD
Definition: DBObject.h:162
static const AccessPrivileges ACCESS
Definition: DBObject.h:146
static const int32_t VIEW_DASHBOARD
Definition: DBObject.h:104
static const AccessPrivileges ALL_TABLE
Definition: DBObject.h:150
static const int32_t VIEW_SQL_EDITOR
Definition: DBObject.h:81
static const int32_t DROP_TABLE
Definition: DBObject.h:88
static const int32_t INSERT_INTO_VIEW
Definition: DBObject.h:116
static const AccessPrivileges DROP_VIEW
Definition: DBObject.h:172
static const int32_t ALL
Definition: DBObject.h:101
#define CHECK(condition)
Definition: Logger.h:187
static const int32_t DELETE_FROM_VIEW
Definition: DBObject.h:118
static const AccessPrivileges CREATE_VIEW
Definition: DBObject.h:171
static const int32_t CREATE_TABLE
Definition: DBObject.h:87
const DashboardDescriptor * getMetadataForDashboard(const std::string &userId, const std::string &dashName) const
static const int32_t CREATE_DASHBOARD
Definition: DBObject.h:102
int32_t permissionType
Definition: DBObject.h:54
std::vector< std::string > toString() const
Definition: DBObject.cpp:143
static const AccessPrivileges DELETE_DASHBOARD
Definition: DBObject.h:166
static const int32_t SELECT_FROM_TABLE
Definition: DBObject.h:89
int64_t privileges
Definition: DBObject.h:126
static const AccessPrivileges EDIT_DASHBOARD
Definition: DBObject.h:165
static const AccessPrivileges UPDATE_IN_TABLE
Definition: DBObject.h:155
static const int32_t UPDATE_IN_TABLE
Definition: DBObject.h:91
static const int32_t TRUNCATE_VIEW
Definition: DBObject.h:119
DBObjectType objectType_
Definition: DBObject.h:217
std::string ObjectPermissionTypeToString(DBObjectType type)
Definition: DBObject.cpp:72