00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00030 #ifndef __VDBL_DATABASE_H
00031 #define __VDBL_DATABASE_H
00032
00033 #define HAVE_VTABLE 0
00034
00035 #include <algorithm>
00036 #include <map>
00037 #include <vdbl_config.h>
00038 #include <vdbl_types.h>
00039 #include <vdbl_table.h>
00040 #include <vdbl_user.h>
00041 #include <vdbl_view.h>
00042 #include <vdbl_stview.h>
00043 #if HAVE_VTABLE
00044 #include <vdbl_vtable.h>
00045 #endif
00046
00047 __VDBL_BEGIN_NAMESPACE
00048
00049 #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
00050 #pragma set woff 1209
00051 #endif
00052
00054
00059 class _VDBL_userflags
00060 {
00061 public:
00070 struct
00071 {
00072 bool all_tables_write;
00073 bool all_tables_read;
00074 bool all_tables_modify;
00075 bool all_tables_append;
00076 bool all_tables_delete;
00077 } table_privileges;
00078
00083 struct
00084 {
00085 bool all_views_commit_to_parent;
00086 } view_privileges;
00087
00088 public:
00092 _VDBL_userflags() : table_privileges(), view_privileges() {}
00096 _VDBL_userflags(const _VDBL_userflags& _a)
00097 : table_privileges(_a.table_privileges),
00098 view_privileges(_a.view_privileges) {}
00102 virtual ~_VDBL_userflags() {}
00103 };
00104
00106
00111 class _VDBL_aclentry
00112 {
00113 public:
00115
00118 bool read;
00119 bool write;
00120 bool append;
00121 bool drop;
00122 bool commit_to_parent;
00124
00125 public:
00129 _VDBL_aclentry(bool _r = false, bool _a = false, bool _w = false,
00130 bool _d = false, bool _ctp = false)
00131 : read(_r), write(_w), append(_a), drop(false),
00132 commit_to_parent(_ctp)
00133 {}
00134
00138 _VDBL_aclentry(const _VDBL_aclentry& _a) : read(_a.read),
00139 write(_a.write), append(_a.append), drop(_a.drop),
00140 commit_to_parent(_a.commit_to_parent)
00141 {}
00142
00146 virtual ~_VDBL_aclentry() {}
00147
00151 _VDBL_aclentry& operator=(const _VDBL_aclentry& __a)
00152 {
00153 read = __a.read;
00154 write = __a.write;
00155 append = __a.append;
00156 drop = __a.drop;
00157 commit_to_parent = __a.commit_to_parent;
00158 return *this;
00159 }
00160 };
00161
00163
00169 class _VDBL_acl
00170 {
00171 public:
00175 _VDBL_aclentry global;
00179 std::map<_VDBL_colid,_VDBL_aclentry> colwise;
00180
00181 public:
00185 _VDBL_acl(bool _gr = false, bool _ga = false, bool _gw = false,
00186 bool _gd = false, bool _gctp = false)
00187 : global(_gr,_ga,_gw,_gd,_gctp), colwise() {}
00188
00192 _VDBL_acl(const _VDBL_acl& _a) : global(_a.global), colwise(_a.colwise) {}
00193
00194
00198 virtual ~_VDBL_acl() {}
00199
00203 _VDBL_acl& operator=(const _VDBL_acl& __a)
00204 {
00205 global = __a.global;
00206 colwise = __a.colwise;
00207 return *this;
00208 }
00209 };
00210
00212
00217 class _VDBL_tableflags
00218 {
00219 public:
00220
00224 bool temporary;
00228 bool unrestricted;
00232 _VDBL_acl global;
00236 std::map<_VDBL_userid,_VDBL_acl> ACLs;
00237
00238 public:
00242 _VDBL_tableflags(bool _temp = false, bool _unrest = true) :
00243 temporary(_temp), unrestricted(_unrest), global(), ACLs()
00244 {}
00245
00249 _VDBL_tableflags(const _VDBL_acl& _gacl, bool _temp = false,
00250 bool _unrest = true) : temporary(_temp),
00251 unrestricted(_unrest), global(_gacl), ACLs()
00252 {}
00253
00257 _VDBL_tableflags(const _VDBL_tableflags& __t) :
00258 temporary(__t.temporary), unrestricted(__t.unrestricted),
00259 global(__t.global), ACLs(__t.ACLs)
00260 {}
00261
00265 virtual ~_VDBL_tableflags() {}
00266
00270 _VDBL_tableflags& operator=(const _VDBL_tableflags& __t)
00271 {
00272 temporary = __t.temporary;
00273 unrestricted = __t.unrestricted;
00274 global = __t.global;
00275 ACLs = __t.ACLs;
00276 return *this;
00277 }
00278 };
00279
00281
00286 class _VDBL_viewflags
00287 {
00288 public:
00292 _VDBL_acl global;
00296 std::map<_VDBL_userid,_VDBL_acl> ACLs;
00297
00298 public:
00302 _VDBL_viewflags() : global(), ACLs() {}
00303
00307 _VDBL_viewflags(const _VDBL_acl& _gacl) : global(_gacl), ACLs() {}
00308
00312 _VDBL_viewflags(const _VDBL_viewflags& __v) :
00313 global(__v.global), ACLs(__v.ACLs) {}
00317 virtual ~_VDBL_viewflags() {}
00318
00322 _VDBL_viewflags& operator=(const _VDBL_viewflags& __v)
00323 {
00324 global = __v.global;
00325 ACLs = __v.ACLs;
00326 return *this;
00327 }
00328 };
00329
00331
00336 class _VDBL_database
00337 {
00338 private:
00340 typedef std::pair<_VDBL_tableflags,_VDBL_table*> _D_table_entry;
00342 typedef std::map<std::string, _VDBL_tableid> _D_table_names;
00344 typedef std::map<_VDBL_tableid, _D_table_entry> _D_tables;
00345
00347 typedef triple<_VDBL_userid,_VDBL_userflags,_VDBL_user> _D_user_entry;
00349 typedef std::map<std::string, _D_user_entry> _D_users;
00350
00352 typedef std::pair<_VDBL_viewflags,_VDBL_view*> _D_view_entry;
00354 typedef std::map<std::string, _VDBL_viewid> _D_view_names;
00356 typedef std::map<_VDBL_viewid, _D_view_entry> _D_views;
00357
00359
00360 _VDBL_tableid _D_ti;
00361 _VDBL_userid _D_ui;
00362 _VDBL_viewid _D_vi;
00364
00368 _D_table_names _D_tn;
00372 _D_tables _D_t;
00376 _D_users _D_u;
00380 _D_view_names _D_vn;
00384 _D_views _D_v;
00385
00386 protected:
00388
00391 _VDBL_tableid get_tableid() { return ++_D_ti; }
00392 _VDBL_userid get_userid() { return ++_D_ui; }
00393 _VDBL_viewid get_viewid() { return ++_D_vi; }
00395
00396 public:
00397
00405 bool create_table(const std::string& _C_i, const _VDBL_userid& _C_u,
00406 const _VDBL_tableflags& __f = _VDBL_tableflags())
00407 {
00408
00409 _D_table_names::iterator __t(_D_tn.find(_C_i));
00410 if(__t != _D_tn.end())
00411 return false;
00412 _VDBL_table* _ntb = (_VDBL_table*) new _VDBL_standardtable;
00413 _VDBL_tableid _ti(get_tableid());
00414 _D_tn.insert(std::make_pair(_C_i,_ti));
00415 _D_t.insert(std::make_pair(_ti,std::make_pair(__f,_ntb)));
00416 return true;
00417 }
00418
00419 #if HAVE_VTABLE
00420
00428 bool create_table(const std::string& _C_i, const _VDBL_userid& _C_u,
00429 const _VDBL_tableflags& __f = _VDBL_tableflags(),
00430 const _VDBL_view& __v)
00431 {
00432
00433 _D_table_names::iterator __t(_D_tn.find(_C_i));
00434 if(__t != _D_tn.end())
00435 return false;
00436 _VDBL_table* _ntb = (_VDBL_table*) new _VDBL_viewtable(__v);
00437 _VDBL_tableid _ti(get_tableid());
00438 _D_tn.insert(std::make_pair(_C_i,_ti));
00439 _D_t.insert(std::make_pair(_ti,std::make_pair(__f,_ntb)));
00440 return true;
00441 }
00442 #endif
00443
00447 _VDBL_tableid get_tableid(const std::string& _C_i,
00448 const _VDBL_userid& _C_u) const
00449 {
00450
00451 _D_table_names::const_iterator __t(_D_tn.find(_C_i));
00452 if(__t == _D_tn.end())
00453 return _VDBL_tableid();
00454 else
00455 return (*__t).second;
00456 }
00457
00458 bool alter_table(const _VDBL_tableid& _ti, const _VDBL_userid& _C_u)
00459 { throw "alter_table: NYI"; }
00460
00461 bool alter_table(const std::string& _C_i, const _VDBL_userid& _C_u)
00462 { return alter_table(get_tableid(_C_i, _C_u), _C_u); }
00463
00470 bool drop_table(const _D_tables::iterator& __t,
00471 const _D_table_names::iterator& __tn,
00472 const _VDBL_userid& _C_u)
00473 {
00474 bool ret(true);
00475
00476 if(__t != _D_t.end())
00477 {
00478 _VDBL_table* _otb = (*__t).second.second;
00479 delete _otb;
00480 _D_t.erase(__t);
00481 }
00482 else
00483 ret = false;
00484 if(__tn != _D_tn.end())
00485 _D_tn.erase(__tn);
00486 else
00487 ret = false;
00488 return ret;
00489 }
00490
00495 bool drop_table(const _VDBL_tableid& _C_i, const _VDBL_userid& _C_u)
00496 {
00497 _D_table_names::iterator __t;
00498 for(__t = _D_tn.begin(); __t != _D_tn.end(); ++__t)
00499 if((*__t).second == _C_i)
00500 break;
00501 return drop_table(_D_t.find(_C_i), __t, _C_u);
00502 }
00503
00508 bool drop_table(const std::string& _C_i, const _VDBL_userid& _C_u)
00509 {
00510 _VDBL_tableid _ti(get_tableid(_C_i, _C_u));
00511 return drop_table(_D_t.find(_ti), _D_tn.find(_C_i), _C_u);
00512 }
00513
00517 bool has_table(const _VDBL_tableid& _C_i, const _VDBL_userid& _C_u) const
00518 {
00519 _D_tables::const_iterator __t(_D_t.find(_C_i));
00520 if(__t == _D_t.end())
00521 return false;
00522 else
00523 return true;
00524 }
00525
00529 bool has_table(const std::string& _C_i, const _VDBL_userid& _C_u) const
00530 { return has_table(get_tableid(_C_i, _C_u), _C_u); }
00531
00532 public:
00536 _VDBL_table* get_table(const _VDBL_tableid& _C_i, const _VDBL_userid& _C_u)
00537 const
00538 {
00539
00540 _D_tables::const_iterator __t(_D_t.find(_C_i));
00541 if(__t == _D_t.end())
00542 return NULL;
00543 return (*__t).second.second;
00544 }
00545
00549 _VDBL_table* get_table(const std::string& _C_i, const _VDBL_userid& _C_u) const
00550 { return get_table(get_tableid(_C_i, _C_u), _C_u); }
00551
00552 private:
00556 _VDBL_view* get_table_view(const _VDBL_tableid& _C_i,
00557 const _VDBL_context& __c, const _V_enum& __e) const
00558 {
00559 _D_tables::const_iterator __t(_D_t.find(_C_i));
00560 if(__t == _D_t.end())
00561 return NULL;
00562 _VDBL_view* __v = new
00563 _VDBL_standardview(_C_i, (*__t).second.second, __c, __e);
00564 return __v;
00565 }
00566
00570 _VDBL_view* get_table_view(const std::string& _C_i, const _VDBL_context& __c,
00571 const _V_enum& __e) const
00572 {
00573 _D_table_names::const_iterator _ti(_D_tn.find(_C_i));
00574 if(_ti == _D_tn.end())
00575 return NULL;
00576 return get_table_view((*_ti).second, __c, __e);
00577 }
00578
00585 template < template <class _T, class _A> class _SqCtr, class _Al >
00586 _VDBL_view* get_table_view(const _VDBL_tableid& _C_i,
00587 const _VDBL_context& __c, const _V_enum& __e,
00588 const _SqCtr<std::pair<_VDBL_tableid,_VDBL_rowid>, _Al>& _Ct) const
00589 {
00590 _D_tables::const_iterator __t(_D_t.find(_C_i));
00591 if(__t == _D_t.end())
00592 return NULL;
00593 std::vector<_VDBL_rowid> _r;
00594 typename _SqCtr<std::pair<_VDBL_tableid,_VDBL_rowid>, _Al>::const_iterator _a;
00595 _r.reserve(_Ct.size());
00596 for(_a = _Ct.begin(); _a != _Ct.end(); ++_a)
00597 if(_C_i == (*_a).first)
00598 _r.push_back((*_a).second);
00599 _VDBL_view* __v = new _VDBL_standardview(_C_i, (*__t).second.second,
00600 __c, __e, _r);
00601 return __v;
00602 }
00603
00610 template < template <class _T, class _A> class _SqCtr, class _Al >
00611 _VDBL_view* get_table_view(const std::string& _C_i, const _VDBL_context& __c,
00612 const _V_enum& __e,
00613 const _SqCtr<std::pair<_VDBL_tableid,_VDBL_rowid>, _Al>& _Ct) const
00614 {
00615 _D_table_names::const_iterator _ti(_D_tn.find(_C_i));
00616 if(_ti == _D_tn.end())
00617 return NULL;
00618 return get_table_view((*_ti).second, __c, __e, _Ct);
00619 }
00620
00621 public:
00622
00626 _VDBL_viewid get_viewid(const std::string& _C_i, const _VDBL_userid& _C_u) const
00627 {
00628 _D_view_names::const_iterator __v(_D_vn.find(_C_i));
00629 if(__v == _D_vn.end())
00630 return _VDBL_viewid();
00631 else
00632 return (*__v).second;
00633 }
00634
00640 bool create_view(const std::string& _C_i, const _VDBL_userid& _C_u,
00641 const _VDBL_context& __c,
00642 const std::string& _C_t, const _V_enum& __e)
00643 {
00644
00645 _D_view_names::iterator __v(_D_vn.find(_C_i));
00646 if(__v != _D_vn.end())
00647 return false;
00648 _VDBL_view* __vi = get_table_view(_C_t, __c, __e);
00649 if(__vi == NULL)
00650 return false;
00651 _VDBL_viewid _vid(get_viewid());
00652 _D_vn.insert(std::make_pair(_C_i,_vid));
00653 _D_v.insert(std::make_pair(_vid, std::make_pair(_VDBL_viewflags(),__vi)));
00654 return true;
00655 }
00656
00657 bool alter_view(const _VDBL_viewid& _C_i, const _VDBL_userid& _C_u)
00658 { throw "alter_view: NYI"; }
00659
00660 bool alter_view(const std::string& _C_i, const _VDBL_userid& _C_u)
00661 { return alter_view(get_viewid(_C_i, _C_u), _C_u); }
00662
00669 bool drop_view(const _D_views::iterator& __v,
00670 const _D_view_names::iterator& __vn,
00671 const _VDBL_userid& _C_u)
00672 {
00673 bool ret(true);
00674
00675 if(__v != _D_v.end())
00676 {
00677 _VDBL_view* _ovw = (*__v).second.second;
00678 delete _ovw;
00679 _D_v.erase(__v);
00680 }
00681 else
00682 ret = false;
00683 if(__vn != _D_vn.end())
00684 _D_vn.erase(__vn);
00685 else
00686 ret = false;
00687 return ret;
00688 }
00689
00694 bool drop_view(const _VDBL_viewid& _C_i, const _VDBL_userid& _C_u)
00695 {
00696 _D_view_names::iterator __v;
00697 for(__v = _D_vn.begin(); __v != _D_vn.end(); ++__v)
00698 if((*__v).second == _C_i)
00699 break;
00700 return drop_view(_D_v.find(_C_i), __v, _C_u);
00701 }
00702
00707 bool drop_view(const std::string& _C_i, const _VDBL_userid& _C_u)
00708 {
00709 _VDBL_viewid _vi(get_viewid(_C_i, _C_u));
00710 return drop_view(_D_v.find(_vi), _D_vn.find(_C_i), _C_u);
00711 }
00712
00716 bool has_view(const _VDBL_viewid& _C_i, const _VDBL_userid& _C_u) const
00717 {
00718 _D_views::const_iterator __v(_D_v.find(_C_i));
00719 if(__v == _D_v.end())
00720 return false;
00721 else
00722 return true;
00723 }
00724
00728 bool has_view(const std::string& _C_i, const _VDBL_userid& _C_u) const
00729 { return has_view(get_viewid(_C_i, _C_u), _C_u); }
00730
00734 _VDBL_view* get_view(const _VDBL_viewid& _C_i, const _VDBL_userid& _C_u) const
00735 {
00736
00737 _D_views::const_iterator __v(_D_v.find(_C_i));
00738 if(__v == _D_v.end())
00739 return NULL;
00740 return (*__v).second.second;
00741 }
00742
00746 _VDBL_view* get_view(const std::string& _C_i, const _VDBL_userid& _C_u) const
00747 { return get_view(get_viewid(_C_i,_C_u), _C_u); }
00748
00749 bool create_user(const std::string& _C_i) { throw "create_user: NYI"; }
00750 bool alter_user(const std::string& _C_i) { throw "alter_user: NYI"; }
00751 bool drop_user(const std::string& _C_i) { throw "drop_user: NYI"; }
00752 const _VDBL_userid& get_user(const std::string& _C_i) const
00753 { throw "get_user: NYI"; }
00754 bool has_user(const std::string& _C_i) const { throw "has_user: NYI"; }
00755
00759 _VDBL_database() : _D_ti(), _D_ui(), _D_vi(), _D_tn(),
00760 _D_t(), _D_u(), _D_vn(), _D_v() {}
00761
00765 _VDBL_database(const _VDBL_database& __t)
00766 : _D_ti(__t._D_ti), _D_ui(__t._D_ui), _D_vi(__t._D_vi),
00767 _D_tn(__t._D_tn), _D_t(__t._D_t), _D_u(__t._D_u),
00768 _D_vn(__t._D_vn), _D_v(__t._D_v)
00769 {
00770 for(_D_tables::iterator __x = _D_t.begin(); __x != _D_t.end(); ++__x)
00771 (*__x).second.second = new _VDBL_table(*(*__x).second.second);
00772 for(_D_views::iterator __x = _D_v.begin(); __x != _D_v.end(); ++__x)
00773 (*__x).second.second = new _VDBL_view(*(*__x).second.second);
00774 }
00775
00779 virtual ~_VDBL_database()
00780 {
00781 for(_D_tables::iterator __x = _D_t.begin(); __x != _D_t.end(); ++__x)
00782 delete (*__x).second.second;
00783 for(_D_views::iterator __x = _D_v.begin(); __x != _D_v.end(); ++__x)
00784 delete (*__x).second.second;
00785 }
00786
00787 friend class _VDBL_viewdbase;
00788 };
00789
00791
00796 class database : public _VDBL_database
00797 {
00798 private:
00799 typedef _VDBL_database _Base;
00800
00801 public:
00802 typedef _VDBL_table table;
00803 typedef _VDBL_view view;
00804 typedef _VDBL_user user;
00805
00806 public:
00814 bool create_table(const std::string& _C_i, const _VDBL_userid& _C_u,
00815 const _VDBL_tableflags& __f = _VDBL_tableflags())
00816 { return _Base::create_table(_C_i, _C_u, __f); }
00824 bool create_table(const char* _C_i, const _VDBL_userid& _C_u,
00825 const _VDBL_tableflags& __f = _VDBL_tableflags())
00826 { return create_table(std::string(_C_i), _C_u, __f); }
00827 #if HAVE_VTABLE
00828
00836 bool create_table(const char* _C_i, const _VDBL_userid& _C_u,
00837 const _VDBL_tableflags& __f = _VDBL_tableflags(),
00838 const _VDBL_view& __v)
00839 { return _Base::create_table(std::string(_C_i), _C_u, __f, __v); }
00840 #endif
00841 bool alter_table(const char* _C_i, const _VDBL_userid& _C_u)
00842 { return _Base::alter_table(std::string(_C_i), _C_u); }
00847 bool drop_table(const char* _C_i, const _VDBL_userid& _C_u)
00848 { return _Base::drop_table(std::string(_C_i), _C_u); }
00852 bool has_table(const char* _C_i, const _VDBL_userid& _C_u) const
00853 { return _Base::has_table(std::string(_C_i), _C_u); }
00859 bool create_view(const char* _C_i, const _VDBL_userid& _C_u,
00860 const _VDBL_context& __c,
00861 const std::string& _C_t, const _V_enum& __e)
00862 { return _Base::create_view(std::string(_C_i), _C_u, __c, _C_t, __e); }
00868 bool create_view(const std::string& _C_i, const _VDBL_userid& _C_u,
00869 const _VDBL_context& __c,
00870 const char* _C_t, const _V_enum& __e)
00871 { return _Base::create_view(_C_i, _C_u, __c, std::string(_C_t), __e); }
00877 bool create_view(const char* _C_i, const _VDBL_userid& _C_u,
00878 const _VDBL_context& __c,
00879 const char* _C_t, const _V_enum& __e)
00880 { return _Base::create_view(std::string(_C_i), _C_u, __c,
00881 std::string(_C_t), __e); }
00882 bool alter_view(const char* _C_i, const _VDBL_userid& _C_u)
00883 { return _Base::alter_view(std::string(_C_i), _C_u); }
00888 bool drop_view(const char* _C_i, const _VDBL_userid& _C_u)
00889 { return _Base::drop_view(std::string(_C_i), _C_u); }
00893 bool has_view(const char* _C_i, const _VDBL_userid& _C_u) const
00894 { return _Base::has_view(std::string(_C_i), _C_u); }
00898 viewbase* get_view(const char* _C_i, const _VDBL_userid& _C_u) const
00899 { return (viewbase*)_Base::get_view(std::string(_C_i), _C_u); }
00900 bool create_user(const char* _C_i)
00901 { return _Base::create_user(std::string(_C_i)); }
00902 bool alter_user(const char* _C_i)
00903 { return _Base::alter_user(std::string(_C_i)); }
00904 bool drop_user(const char* _C_i)
00905 { return _Base::drop_user(std::string(_C_i)); }
00906 const _VDBL_userid& get_user(const char* _C_i) const
00907 { return _Base::get_user(std::string(_C_i)); }
00908 bool has_user(const char* _C_i) const
00909 { return _Base::has_user(std::string(_C_i)); }
00913 table* get_table(const tableid& _C_i, const _VDBL_userid& _C_u) const
00914 { return (table*)_Base::get_table(_C_i,_C_u); }
00918 table* get_table(const std::string& _C_i, const _VDBL_userid& _C_u) const
00919 { return (table*)_Base::get_table(_C_i,_C_u); }
00923 table* get_table(const char* _C_i, const _VDBL_userid& _C_u) const
00924 { return get_table(std::string(_C_i),_C_u); }
00925 };
00926
00928
00932 typedef _VDBL_userflags userflags;
00934
00938 typedef _VDBL_viewflags viewflags;
00940
00944 typedef _VDBL_tableflags tableflags;
00945
00947
00951 typedef _VDBL_aclentry aclentry;
00953
00957 typedef _VDBL_acl acl;
00958
00959 #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
00960 #pragma reset woff 1209
00961 #endif
00962
00963 __VDBL_END_NAMESPACE
00964
00965 #endif
00966
00967
00968
00969