00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00031 #ifndef __VDBL_TABLE_H
00032 #define __VDBL_TABLE_H
00033
00034 #include <algorithm>
00035 #include <map>
00036 #include <set>
00037 #include <vector>
00038 #include <string>
00039 #include <triple>
00040 #include <vdbl_config.h>
00041 #include <vdbl_row.h>
00042 #include <vdbl_col.h>
00043 #include <vdbl_index.h>
00044 #include <vdbl_context.h>
00045 #include <vdbl_view.h>
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
00055 static _VDBL_row ___empty_row_return;
00056
00058
00068 class _VDBL_table
00069 {
00070 private:
00071 friend class _VDBL_view;
00072
00073 public:
00078 typedef std::pair<std::string,_VDBL_col> _T_colspec;
00083 typedef std::pair<const std::string*,const _VDBL_col*> _T_ptrcolspec;
00084
00085 private:
00087
00088 _VDBL_colid _T_ci;
00089 _VDBL_rowid _T_ri;
00091
00092 unsigned int last_change;
00093
00094 protected:
00096
00099 _VDBL_colid get_colid() { return ++_T_ci; }
00100 _VDBL_rowid get_rowid() { return ++_T_ri; }
00102
00106 void made_change() { last_change++; }
00107
00108 public:
00116 virtual const std::type_info& get_colinfo(const std::string& _C_n,
00117 std::triple<bool,_VDBL_colid,_VDBL_colflags>& _r) const
00118 VDBL_PURE_VIRTUAL
00119
00120 public:
00124 unsigned int get_change_ctr() const { return last_change; }
00125
00126 public:
00132 class _col_iterator_base
00133 {
00134 private:
00135 typedef std::pair<std::string,_VDBL_colid> entry;
00136 public:
00137 typedef size_t size_type;
00138 typedef ptrdiff_t difference_type;
00139 typedef std::bidirectional_iterator_tag iterator_category;
00140
00141 protected:
00142 entry _CI_ci;
00143 const _VDBL_table* _CI_t;
00144
00145 public:
00146 _col_iterator_base(const _VDBL_table* __x, const entry& __c)
00147 : _CI_ci(__c), _CI_t(__x) { }
00148
00149 _col_iterator_base() : _CI_ci(), _CI_t(NULL) {}
00150
00151 void _D_incr() { _CI_ci = _CI_t->_next_col(_CI_ci); }
00152
00153 void _D_decr() { _CI_ci = _CI_t->_prev_col(_CI_ci); }
00154
00155
00156 bool operator==(const _col_iterator_base& __x) const
00157 { return _CI_t == __x._CI_t && _CI_ci == __x._CI_ci; }
00158
00159 bool operator!=(const _col_iterator_base& __x) const
00160 { return _CI_t != __x._CI_t || _CI_ci != __x._CI_ci; }
00161 };
00162
00168 template<typename _Tp, typename _Ref, typename _Ptr>
00169 struct _col_iterator : public _col_iterator_base
00170 {
00171 public:
00172 typedef _col_iterator<_Tp,_Tp&,_Tp*> iterator;
00173 typedef _col_iterator<_Tp,const _Tp&,const _Tp*> const_iterator;
00174 typedef _col_iterator<_Tp,_Ref,_Ptr> _Self;
00175
00176 typedef _Tp value_type;
00177 typedef _Ptr pointer;
00178 typedef _Ref reference;
00179
00180 _col_iterator(const _VDBL_table* __x,
00181 const std::pair<std::string,_VDBL_colid>& __c)
00182 : _col_iterator_base(__x,__c) { }
00183
00184 _col_iterator(const _VDBL_table* __x, const std::string& __s,
00185 const _VDBL_colid& __c)
00186 : _col_iterator_base(__x,std::make_pair(__s,__c)) { }
00187
00188 _col_iterator() : _col_iterator_base() { }
00189
00190 _col_iterator(const iterator& __x)
00191 : _col_iterator_base(__x._CI_t, __x._CI_ci) { }
00192
00193 reference operator*() const { return _CI_ci; }
00194
00195 pointer operator->() const { return &(operator*()); }
00196
00197 _Self& operator++() { this->_D_incr(); return *this; }
00198
00199 _Self operator++(int)
00200 { _Self __tmp = *this; this->_D_incr(); return __tmp; }
00201
00202 _Self& operator--() { this->_D_decr(); return *this; }
00203
00204 _Self operator--(int)
00205 { _Self __tmp = *this; this->_D_decr(); return __tmp; }
00206 };
00207
00211 typedef _col_iterator<std::pair<std::string,_VDBL_colid>,
00212 const std::pair<std::string,_VDBL_colid>&,
00213 const std::pair<std::string,_VDBL_colid>*>
00214 col_const_iterator;
00215
00221 virtual std::pair<std::string,_VDBL_colid> _next_col(
00222 const std::pair<std::string,_VDBL_colid>& _ci)
00223 const VDBL_PURE_VIRTUAL
00229 virtual std::pair<std::string,_VDBL_colid> _prev_col(
00230 const std::pair<std::string,_VDBL_colid>& _ci)
00231 const VDBL_PURE_VIRTUAL
00235 virtual col_const_iterator col_begin() const VDBL_PURE_VIRTUAL
00239 virtual col_const_iterator col_end() const VDBL_PURE_VIRTUAL
00240
00246 class _row_iterator_base
00247 {
00248 private:
00249 typedef _VDBL_rowid entry;
00250 public:
00251 typedef size_t size_type;
00252 typedef ptrdiff_t difference_type;
00253 typedef std::bidirectional_iterator_tag iterator_category;
00254
00255 protected:
00256 entry _CI_ci;
00257 const _VDBL_table* _CI_t;
00258
00259 public:
00260 _row_iterator_base(const _VDBL_table* __x, const entry& __c)
00261 : _CI_ci(__c), _CI_t(__x) { }
00262
00263 _row_iterator_base() : _CI_ci(), _CI_t(NULL) {}
00264
00265 void _D_incr() { _CI_ci = _CI_t->_next_row(_CI_ci); }
00266
00267 void _D_decr() { _CI_ci = _CI_t->_prev_row(_CI_ci); }
00268
00269
00270 bool operator==(const _row_iterator_base& __x) const
00271 { return _CI_t == __x._CI_t && _CI_ci == __x._CI_ci; }
00272
00273 bool operator!=(const _row_iterator_base& __x) const
00274 { return _CI_t != __x._CI_t || _CI_ci != __x._CI_ci; }
00275 };
00276
00282 template<typename _Tp, typename _Ref, typename _Ptr>
00283 struct _row_iterator : public _row_iterator_base
00284 {
00285 public:
00286 typedef _row_iterator<_Tp,_Tp&,_Tp*> iterator;
00287 typedef _row_iterator<_Tp,const _Tp&,const _Tp*> const_iterator;
00288 typedef _row_iterator<_Tp,_Ref,_Ptr> _Self;
00289
00290 typedef _Tp value_type;
00291 typedef _Ptr pointer;
00292 typedef _Ref reference;
00293
00294 _row_iterator(const _VDBL_table* __x, const _VDBL_rowid& __c)
00295 : _row_iterator_base(__x,__c) { }
00296
00297 _row_iterator() : _row_iterator_base() { }
00298
00299 _row_iterator(const iterator& __x)
00300 : _row_iterator_base(__x._CI_t, __x._CI_ci) { }
00301
00302 reference operator*() const { return _CI_ci; }
00303
00304 pointer operator->() const { return &(operator*()); }
00305
00306 _Self& operator++() { this->_D_incr(); return *this; }
00307
00308 _Self operator++(int)
00309 { _Self __tmp = *this; this->_D_incr(); return __tmp; }
00310
00311 _Self& operator--() { this->_D_decr(); return *this; }
00312
00313 _Self operator--(int)
00314 { _Self __tmp = *this; this->_D_decr(); return __tmp; }
00315 };
00316
00320 typedef _row_iterator<_VDBL_rowid, const _VDBL_rowid&, const _VDBL_rowid*>
00321 row_const_iterator;
00322
00328 virtual _VDBL_rowid _next_row(const _VDBL_rowid& _ci)
00329 const VDBL_PURE_VIRTUAL
00335 virtual _VDBL_rowid _prev_row(const _VDBL_rowid& _ci)
00336 const VDBL_PURE_VIRTUAL
00340 virtual row_const_iterator row_begin() const VDBL_PURE_VIRTUAL
00344 virtual row_const_iterator row_end() const VDBL_PURE_VIRTUAL
00348 virtual row_const_iterator* row_iterator_copy(const row_const_iterator&)
00349 const VDBL_PURE_VIRTUAL
00350
00351 public:
00355 _VDBL_table() : _T_ci(0), _T_ri(0), last_change(0) {}
00359 _VDBL_table(const _VDBL_table& __t) : _T_ci(__t._T_ci), _T_ri(__t._T_ri),
00360 last_change(0) {}
00361
00366 template <template <class __Tp, class __AllocTp> class __SequenceCtr,
00367 class Allocator1>
00368 _VDBL_table(const __SequenceCtr<std::triple<std::string, _VDBL_col,
00369 _VDBL_colflags>,Allocator1>& __cc) : _T_ci(0), _T_ri(0),
00370 last_change(0) {}
00371
00375 virtual ~_VDBL_table() {}
00376
00380 virtual _VDBL_table* new_copy() VDBL_PURE_VIRTUAL
00381
00385 virtual void destroy_copy(_VDBL_table* t) VDBL_PURE_VIRTUAL
00386
00392 virtual bool add_col(const std::string& _C_n, const _VDBL_col& __c,
00393 const _VDBL_colflags& __f) VDBL_PURE_VIRTUAL
00394
00400 virtual bool modify_col(const std::string& _C_n, const _VDBL_col& __c,
00401 const _VDBL_colflags& __f) VDBL_PURE_VIRTUAL
00402
00407 virtual bool modify_col(const std::string& _C_n, const _VDBL_col& __c)
00408 VDBL_PURE_VIRTUAL
00409
00414 virtual bool modify_col(const std::string& _C_n, const _VDBL_colflags& __f)
00415 VDBL_PURE_VIRTUAL
00416
00421 virtual bool drop_col(const std::string& _C_n) VDBL_PURE_VIRTUAL
00422
00427 virtual bool rename_col(const std::string& _C_old, const std::string& _C_new)
00428 VDBL_PURE_VIRTUAL
00429
00435 virtual bool insert(const std::vector<_T_ptrcolspec>& _row, _VDBL_rowid& _r)
00436 VDBL_PURE_VIRTUAL
00437
00442 virtual bool insert(const std::vector<_T_ptrcolspec>& _row) VDBL_PURE_VIRTUAL
00443
00451 template <template <class __Tp, class __AllocTp> class __SequenceCtr,
00452 class Allocator1>
00453 bool insert_row(const __SequenceCtr<_T_colspec,Allocator1>& _row)
00454 {
00455 typename __SequenceCtr<_T_colspec,Allocator1>::const_iterator __x;
00456 std::vector<_T_ptrcolspec> __v;
00457 __v.reserve(_row.size());
00458 for(__x = _row.begin(); __x != _row.end(); ++__x)
00459 __v.push_back(std::make_pair(&(*__x).first, &(*__x).second));
00460 return this->insert(__v);
00461 }
00462
00470 template <template <class __Tp1, class __AllocTp1> class __SequenceCtrOut,
00471 template <class __Tp2, class __AllocTp2> class __SequenceCtrIn,
00472 class AllocatorOut, class AllocatorIn>
00473 bool insert_row(const __SequenceCtrOut<__SequenceCtrIn<_T_colspec,AllocatorIn>,
00474 AllocatorOut>& _rows)
00475 {
00476 typedef typename __SequenceCtrOut<__SequenceCtrIn<_T_colspec,AllocatorIn>,
00477 AllocatorOut>::const_iterator _OutCIt;
00478 _OutCIt __x;
00479 bool ret = true;
00480
00481 for(__x = _rows.begin(); __x != _rows.end(); ++__x)
00482 if(!insert(*__x))
00483 ret = false;
00484 return ret;
00485 }
00486
00491 virtual bool update(const std::vector<_T_ptrcolspec>& _row, _VDBL_rowid& _r)
00492 VDBL_PURE_VIRTUAL
00493
00497 virtual bool remove(const _VDBL_rowid _ri) VDBL_PURE_VIRTUAL
00498
00502 virtual bool has_col(const std::string& _C_n) const VDBL_PURE_VIRTUAL
00503
00508 virtual const _VDBL_row& get_row(const _VDBL_rowid& _ri, bool& error) const
00509 VDBL_PURE_VIRTUAL
00510
00515 virtual const _VDBL_row* get_row_ptr(const _VDBL_rowid& _ri) const
00516 VDBL_PURE_VIRTUAL
00517
00522 virtual _VDBL_row& get_row(const _VDBL_rowid& _ri, bool& error)
00523 VDBL_PURE_VIRTUAL
00524
00529 virtual const _VDBL_col& get_def(const _VDBL_colid& _ci, bool& error) const
00530 VDBL_PURE_VIRTUAL
00531
00536 virtual _VDBL_col& get_def(const _VDBL_colid& _ci, bool& error)
00537 VDBL_PURE_VIRTUAL
00538
00542 virtual bool has_def(const _VDBL_colid& _ci) const
00543 VDBL_PURE_VIRTUAL
00544
00548 virtual bool has_col(const _VDBL_rowid& _ri, const _VDBL_colid& _ci) const
00549 VDBL_PURE_VIRTUAL
00550
00558 virtual bool retrieve(const _VDBL_rowid& _r, const _VDBL_colid& _c,
00559 const _VDBL_context* _ctx, _VDBL_alltype_base*& _val) const
00560 VDBL_PURE_VIRTUAL
00561
00562 virtual bool create_index(const std::string& _C_i, const index& i) VDBL_PURE_VIRTUAL
00563 virtual bool alter_index(const std::string& _C_i, const index& i) VDBL_PURE_VIRTUAL
00564 virtual bool drop_index(const std::string& _C_i) VDBL_PURE_VIRTUAL
00565 };
00566
00568
00573 class _VDBL_standardtable : public _VDBL_table
00574 {
00575 private:
00576 typedef _VDBL_table _Base;
00577
00578 typedef std::pair<_VDBL_colflags,_VDBL_col> _T_col_entry;
00579 typedef std::map<std::string, _VDBL_colid> _T_cols;
00580 typedef std::map<_VDBL_colid, _T_col_entry> _T_defaults;
00581 typedef std::map<std::string, index> _T_indices;
00582 typedef std::map<_VDBL_rowid,_VDBL_row> _T_rows;
00583
00584 friend class _VDBL_view;
00585
00586 public:
00591 typedef std::pair<std::string,_VDBL_col> _T_colspec;
00596 typedef std::pair<const std::string*,const _VDBL_col*> _T_ptrcolspec;
00597
00601 typedef _Base::col_const_iterator col_const_iterator;
00602
00603 private:
00605 _T_cols _T_c;
00607 _T_defaults _T_d;
00609 _T_indices _T_i;
00611 _T_rows _T_r;
00612
00613 public:
00614 const std::type_info& get_colinfo(const std::string& _C_n,
00615 std::triple<bool,_VDBL_colid,_VDBL_colflags>& _r) const
00616 {
00617 _T_cols::const_iterator _x;
00618 _x = _T_c.find(_C_n);
00619 if(_x != _T_c.end())
00620 {
00621 _T_defaults::const_iterator _y(_T_d.find((*_x).second));
00622 if(_y == _T_d.end())
00623 throw "_VDBL_table::get_colinfo: inconsistency - this can't happen!";
00624 _r = std::make_triple(true, (*_x).second, (*_y).second.first);
00625 return (*_y).second.second.return_type_id();
00626 }
00627 else
00628 {
00629 _r = std::make_triple(false, _VDBL_colid(), _VDBL_colflags());
00630 return typeid(void);
00631 }
00632 }
00633
00637 _VDBL_colid get_col_id(const std::string& _C_n) const
00638 {
00639 _T_cols::const_iterator _x;
00640 _x = _T_c.find(_C_n);
00641 if(_x != _T_c.end())
00642 return (*_x).second;
00643 else
00644 return _VDBL_colid();
00645 }
00646
00650 std::string get_col_name(const _VDBL_colid& _C_id) const
00651 {
00652 _T_cols::const_iterator _x, _e(_T_c.end());
00653 for(_x = _T_c.begin(); _x != _e; ++_x)
00654 if((*_x).second == _C_id)
00655 return (*_x).first;
00656 return std::string();
00657 }
00658
00659 public:
00663 _VDBL_standardtable() : _Base(), _T_c(), _T_d(), _T_i(), _T_r() {}
00667 _VDBL_standardtable(const _VDBL_standardtable& __t) : _Base(__t),
00668 _T_c(__t._T_c), _T_d(__t._T_d),
00669 _T_i(__t._T_i), _T_r(__t._T_r) {}
00670
00675 template <template <class __Tp, class __AllocTp> class __SequenceCtr,
00676 class Allocator1>
00677 _VDBL_standardtable(const __SequenceCtr<std::triple<std::string, _VDBL_col,
00678 _VDBL_colflags>,Allocator1>& __cc) : _Base(), _T_c(),
00679 _T_d(), _T_i(), _T_r()
00680 {
00681 typedef typename __SequenceCtr<std::triple<std::string, _VDBL_col,
00682 _VDBL_colflags>,Allocator1>::const_iterator _CIt;
00683
00684 for(_CIt __b = __cc.begin(); __b != __cc.end(); ++__b)
00685 {
00686 const std::string& _s((*__b).first);
00687 const _VDBL_col& _c((*__b).second);
00688 const _VDBL_colflags& _f((*__b).third);
00689 _T_cols::iterator __x(_T_c.find(_s));
00690 if(__x == _T_c.end())
00691 {
00692 _VDBL_colid _ci(get_colid());
00693 _T_c.insert(std::make_pair(_s,_ci));
00694 _T_d.insert(std::make_pair(_ci, std::make_pair(_c,_f)));
00695 }
00696 else
00697 {
00698 std::cerr << "Warning: Doubly defined column " << _s << " ignored!" <<
00699 std::endl;
00700 }
00701 }
00702 }
00703
00707 virtual ~_VDBL_standardtable() {}
00708
00712 virtual _VDBL_table* new_copy() { return new _VDBL_standardtable(*this); }
00713
00717 virtual void destroy_copy(_VDBL_table* t)
00718 { delete (_VDBL_standardtable*)t; }
00719
00720 bool add_col(const std::string& _C_n, const _VDBL_col& __c,
00721 const _VDBL_colflags& __f)
00722 {
00723 _T_cols::iterator __x = _T_c.find(_C_n);
00724 if(__x == _T_c.end())
00725 {
00726 _VDBL_colid _ci(get_colid());
00727 _T_c.insert(std::make_pair(_C_n,_ci));
00728 _T_d.insert(std::make_pair(_ci,std::make_pair(__f,__c)));
00729 made_change();
00730 return true;
00731 }
00732 else
00733 return false;
00734 }
00735
00736 bool modify_col(const std::string& _C_n, const _VDBL_col& __c,
00737 const _VDBL_colflags& __f)
00738 {
00739 _T_cols::iterator __x(_T_c.find(_C_n));
00740 if(__x != _T_c.end())
00741 {
00742 _VDBL_colid _ci = (*__x).second;
00743 _T_defaults::iterator __y(_T_d.find(_ci));
00744 if(__y == _T_d.end())
00745 throw "_VDBL_standardtable::modify_col: inconsistency - this can't happen!";
00746 if(!VDBL_CMP_TYPEID((*__y).second.second.return_type_id(),
00747 __c.return_type_id()))
00748
00749 return false;
00750 _T_d.erase(__y);
00751 _T_d.insert(std::make_pair(_ci,std::make_pair(__f,__c)));
00752 made_change();
00753 return true;
00754 }
00755 else
00756 return false;
00757 }
00758
00759 bool modify_col(const std::string& _C_n, const _VDBL_col& __c)
00760 {
00761 _T_cols::iterator __x(_T_c.find(_C_n));
00762 if(__x != _T_c.end())
00763 {
00764 _VDBL_colid _ci = (*__x).second;
00765 _T_defaults::iterator __y(_T_d.find(_ci));
00766 if(__y == _T_d.end())
00767 throw "_VDBL_standardtable::modify_col: inconsistency - this can't happen!";
00768 if(!VDBL_CMP_TYPEID((*__y).second.second.return_type_id(),
00769 __c.return_type_id()))
00770
00771 return false;
00772 _VDBL_colflags __f = (*__y).second.first;
00773 _T_d.erase(__y);
00774 _T_d.insert(std::make_pair(_ci,std::make_pair(__f,__c)));
00775 made_change();
00776 return true;
00777 }
00778 else
00779 return false;
00780 }
00781
00782 bool modify_col(const std::string& _C_n, const _VDBL_colflags& __f)
00783 {
00784 _T_cols::iterator __x(_T_c.find(_C_n));
00785 if(__x != _T_c.end())
00786 {
00787 _VDBL_colid _ci = (*__x).second;
00788 _T_defaults::iterator __y(_T_d.find(_ci));
00789 if(__y == _T_d.end())
00790 throw "_VDBL_standardtable::modify_col: inconsistency - this can't happen!";
00791 _VDBL_col __c = (*__y).second.second;
00792 _T_d.erase(__y);
00793 _T_d.insert(std::make_pair(_ci,std::make_pair(__f,__c)));
00794 made_change();
00795 return true;
00796 }
00797 else
00798 return false;
00799 }
00800
00801 bool drop_col(const std::string& _C_n)
00802 {
00803 _T_cols::iterator __x = _T_c.find(_C_n);
00804 if(__x != _T_c.end())
00805 {
00806 _VDBL_colid _ci = (*__x).second;
00807 _T_defaults::iterator __y(_T_d.find(_ci));
00808 if(__y == _T_d.end())
00809 throw "_VDBL_standardtable::drop_col: inconsistency - this can't happen!";
00810
00811 _T_d.erase(__y);
00812 for(_T_rows::iterator __r = _T_r.begin(); __r != _T_r.end(); ++__r)
00813 (*__r).second.drop(_ci);
00814 made_change();
00815 return true;
00816 }
00817 else
00818 return false;
00819 }
00820
00821 bool rename_col(const std::string& _C_old, const std::string& _C_new)
00822 {
00823 _T_cols::iterator __x = _T_c.find(_C_new);
00824 if(__x != _T_c.end())
00825 return false;
00826 __x = _T_c.find(_C_old);
00827 if(__x != _T_c.end())
00828 {
00829 _T_c.insert(std::make_pair(_C_new,(*__x).second));
00830 _T_c.erase(__x);
00831 made_change();
00832 return true;
00833 }
00834 else
00835 return false;
00836 }
00837
00838 bool insert(const std::vector<_T_ptrcolspec>& _row, _VDBL_rowid& _ri)
00839 {
00840 std::vector<_T_ptrcolspec>::const_iterator __x;
00841 bool ret = true;
00842 _VDBL_row _r;
00843 std::set<_VDBL_colid> _isdef;
00844
00845 for(__x = _row.begin(); __x != _row.end(); ++__x)
00846 {
00847 const _T_ptrcolspec& _c(*__x);
00848 std::triple<bool, _VDBL_colid, _VDBL_colflags> _ru;
00849 const std::type_info& _rt(get_colinfo(*_c.first, _ru));
00850 if(_isdef.find(_ru.second) != _isdef.end())
00851 {
00852 ret = false;
00853 break;
00854 }
00855 if(!_ru.first || !VDBL_CMP_TYPEID(_rt, _c.second->return_type_id()))
00856 {
00857 ret = false;
00858 break;
00859 }
00860 else
00861 {
00862 if(_ru.third.master_index)
00863 {
00864 #if _VDBL_DEBUG
00865 std::cout <<
00866 "We should check the constraints here in table::insert" <<
00867 std::endl;
00868 #endif
00869 }
00870 _isdef.insert(_ru.second);
00871 _r.insert(_ru.second,*_c.second);
00872 }
00873 }
00874
00875 if(ret)
00876 {
00877 for(_T_defaults::const_iterator __b = _T_d.begin(); __b != _T_d.end();
00878 ++__b)
00879 {
00880 const _T_col_entry& __de((*__b).second);
00881 if(!__de.first.has_default && _isdef.find((*__b).first) == _isdef.end())
00882
00883 {
00884 ret = false;
00885 break;
00886 }
00887 }
00888 }
00889 if(ret)
00890 {
00891 _ri = get_rowid();
00892 _T_r.insert(std::make_pair(_ri, _r));
00893 for(_T_indices::iterator i = _T_i.begin(); i != _T_i.end(); ++i)
00894 (*i).second.ins(_r, _ri);
00895 made_change();
00896 }
00897 return ret;
00898 }
00899
00900 bool insert(const std::vector<_T_ptrcolspec>& _row)
00901 {
00902 _VDBL_rowid dummy;
00903 return insert(_row,dummy);
00904 }
00905
00906 bool update(const std::vector<_T_ptrcolspec>& _row, _VDBL_rowid& _ri)
00907 {
00908 bool ret(true);
00909 _T_rows::iterator __xr = _T_r.find(_ri);
00910 if(__xr == _T_r.end())
00911 ret = false;
00912 else
00913 {
00914 std::vector<_T_ptrcolspec>::const_iterator __x;
00915 _VDBL_row _r((*__xr).second);
00916 std::set<_VDBL_colid> _isdef;
00917
00918 for(__x = _row.begin(); __x != _row.end(); ++__x)
00919 {
00920 const _T_ptrcolspec& _c(*__x);
00921 std::triple<bool, _VDBL_colid, _VDBL_colflags> _ru;
00922 const std::type_info& _rt(get_colinfo(*_c.first, _ru));
00923 if(_isdef.find(_ru.second) != _isdef.end())
00924 {
00925 ret = false;
00926 break;
00927 }
00928 if(!_ru.first || !VDBL_CMP_TYPEID(_rt, _c.second->return_type_id()))
00929 {
00930 ret = false;
00931 break;
00932 }
00933 else
00934 {
00935 if(_ru.third.master_index)
00936 {
00937 #if _VDBL_DEBUG
00938 std::cout <<
00939 "We should check the constraints here in table::insert" <<
00940 std::endl;
00941 #endif
00942 }
00943 _isdef.insert(_ru.second);
00944 _r.update(_ru.second,*_c.second);
00945 }
00946 }
00947 if(ret)
00948 {
00949 _T_r.erase(__xr);
00950 _T_r.insert(std::make_pair(_ri,_r));
00951 for(_T_indices::iterator i=_T_i.begin(); i != _T_i.end(); ++i)
00952 {
00953 (*i).second.del(_ri);
00954 (*i).second.ins(_r, _ri);
00955 }
00956 made_change();
00957 }
00958 }
00959 return ret;
00960 }
00961
00962 bool remove(const _VDBL_rowid _ri)
00963 {
00964 _T_rows::iterator __x = _T_r.find(_ri);
00965 if(__x == _T_r.end())
00966 return false;
00967 else
00968 {
00969 _T_r.erase(__x);
00970 for(_T_indices::iterator i=_T_i.begin(); i != _T_i.end(); ++i)
00971 (*i).second.del(_ri);
00972 made_change();
00973 return true;
00974 }
00975 }
00976
00977 bool has_col(const std::string& _C_n) const
00978 {
00979 _T_cols::const_iterator _x(_T_c.find(_C_n));
00980 return _x != _T_c.end();
00981 }
00982
00983 const _VDBL_row& get_row(const _VDBL_rowid& _ri, bool& error) const
00984 {
00985 _T_rows::const_iterator _x(_T_r.find(_ri));
00986 if(_x == _T_r.end())
00987 {
00988 error = true;
00989 return ___empty_row_return;
00990 }
00991 else
00992 {
00993 error = false;
00994 return (*_x).second;
00995 }
00996 }
00997
00998 const _VDBL_row* get_row_ptr(const _VDBL_rowid& _ri) const
00999 {
01000 _T_rows::const_iterator _x(_T_r.find(_ri));
01001 if(_x == _T_r.end())
01002 return NULL;
01003 else
01004 return &(*_x).second;
01005 }
01006
01007 _VDBL_row& get_row(const _VDBL_rowid& _ri, bool& error)
01008 {
01009 _T_rows::iterator _x(_T_r.find(_ri));
01010 if(_x == _T_r.end())
01011 {
01012 error = true;
01013 return ___empty_row_return;
01014 }
01015 else
01016 {
01017 error = false;
01018 return (*_x).second;
01019 }
01020 }
01021
01022 const _VDBL_col& get_def(const _VDBL_colid& _ci, bool& error) const
01023 {
01024 _T_defaults::const_iterator _x(_T_d.find(_ci));
01025 if(_x == _T_d.end() || !(*_x).second.first.has_default)
01026 {
01027 error = true;
01028 return ___empty_col_return;
01029 }
01030 else
01031 {
01032 error = false;
01033 return (*_x).second.second;
01034 }
01035 }
01036
01037 _VDBL_col& get_def(const _VDBL_colid& _ci, bool& error)
01038 {
01039 _T_defaults::iterator _x(_T_d.find(_ci));
01040 if(_x == _T_d.end() || !(*_x).second.first.has_default)
01041 {
01042 error = true;
01043 return ___empty_col_return;
01044 }
01045 else
01046 {
01047 error = false;
01048 return (*_x).second.second;
01049 }
01050 }
01051
01052 virtual bool has_def(const _VDBL_colid& _ci) const
01053 {
01054 _T_defaults::const_iterator _x(_T_d.find(_ci));
01055 if(_x == _T_d.end() || !(*_x).second.first.has_default)
01056 return false;
01057 else
01058 return true;
01059 }
01060
01061 virtual bool has_col(const _VDBL_rowid& _ri, const _VDBL_colid& _ci) const
01062 {
01063 bool error;
01064 const _VDBL_row& _r(get_row(_ri, error));
01065 if(error)
01066 return false;
01067 return _r.has_col(_ci);
01068 }
01069
01070 bool retrieve(const _VDBL_rowid& _r, const _VDBL_colid& _c,
01071 const _VDBL_context* _ctx, _VDBL_alltype_base*& _val) const
01072 {
01073 bool ret;
01074 const _VDBL_row& _row(get_row(_r, ret));
01075 if(ret)
01076 return false;
01077
01078 _VDBL_col _col(_row.get_col(_c, ret));
01079 if(ret)
01080 {
01081 _T_defaults::const_iterator _x(_T_d.find(_c));
01082 if(_x == _T_d.end())
01083 return false;
01084 _VDBL_col _col2((*_x).second.second);
01085 _col2.setcontext(_ctx, &_row);
01086 _col2.def_copy(_val);
01087 return true;
01088 }
01089 else
01090 {
01091 _col.setcontext(_ctx, &_row);
01092 _col.get_copy(_val);
01093 return true;
01094 }
01095 }
01096
01104 template <class _VALclass>
01105 bool retrieve(const _VDBL_rowid& _r, const _VDBL_colid& _c,
01106 const _VDBL_context* _ctx, _VALclass*& _val) const
01107 {
01108 bool ret;
01109 const _VDBL_row& _row(get_row(_r, ret));
01110 if(ret)
01111 return false;
01112
01113 _VDBL_col _col(_row.get_col(_c, ret));
01114 if(ret)
01115 {
01116 _T_defaults::const_iterator _x(_T_d.find(_c));
01117 if(_x == _T_d.end())
01118 return false;
01119 _VDBL_col _col2((*_x).second.second);
01120 _col2.setcontext(_ctx, &_row);
01121 _col2.def_copy(_val);
01122 return true;
01123 }
01124 else
01125 {
01126 _col.setcontext(_ctx, &_row);
01127 _col.get_copy(_val);
01128 return true;
01129 }
01130 }
01131
01132 void update_index(index& i)
01133 {
01134 i.clear();
01135 _T_rows::const_iterator __r, __e(_T_r.end());
01136 for(__r = _T_r.begin(); __r != __e; ++__r)
01137 i.ins((*__r).second, (*__r).first);
01138 }
01139 bool create_index(const std::string& _C_i, const index& i)
01140 { _T_indices::iterator _i = _T_i.find(_C_i);
01141 if(_i != _T_i.end())
01142 return false;
01143 else
01144 {
01145 _T_i.insert(std::make_pair(_C_i, i));
01146 update_index(_T_i[_C_i]);
01147 return true;
01148 }
01149 }
01150 bool alter_index(const std::string& _C_i, const index& i)
01151 { _T_indices::iterator _i = _T_i.find(_C_i);
01152 if(_i == _T_i.end())
01153 return false;
01154 else
01155 {
01156 _T_i.erase(_i);
01157 _T_i.insert(std::make_pair(_C_i, i));
01158 update_index(_T_i[_C_i]);
01159 return true;
01160 }
01161 }
01162 bool drop_index(const std::string& _C_i)
01163 { _T_indices::iterator _i = _T_i.find(_C_i);
01164 if(_i == _T_i.end())
01165 return false;
01166 else
01167 {
01168 _T_i.erase(_i);
01169 return true;
01170 }
01171 }
01172
01173 public:
01174 std::pair<std::string,_VDBL_colid> _next_col(
01175 const std::pair<std::string,_VDBL_colid>& _ci) const
01176 {
01177 _T_cols::const_iterator __x = _T_c.find(_ci.first);
01178 if(__x == _T_c.end())
01179 return std::make_pair("", _VDBL_colid());
01180 ++__x;
01181 if(__x == _T_c.end())
01182 return std::make_pair("", _VDBL_colid());
01183 else
01184 return *__x;
01185 }
01186
01187 virtual std::pair<std::string,_VDBL_colid> _prev_col(
01188 const std::pair<std::string,_VDBL_colid>& _ci) const
01189 {
01190 _T_cols::const_iterator __x = _T_c.find(_ci.first);
01191 if(__x == _T_c.end())
01192 return std::make_pair("", _VDBL_colid());
01193 --__x;
01194 if(__x == _T_c.end())
01195 return std::make_pair("", _VDBL_colid());
01196 else
01197 return *__x;
01198 }
01199
01200 col_const_iterator col_begin() const
01201 { if(_T_c.empty())
01202 return col_const_iterator(this, "", 0);
01203 else
01204 return col_const_iterator(this, *_T_c.begin());
01205 }
01206 col_const_iterator col_end() const
01207 { return col_const_iterator(this, "", 0); }
01208
01209 _VDBL_rowid _next_row(const _VDBL_rowid& _ci) const
01210 {
01211 _T_rows::const_iterator __x = _T_r.find(_ci);
01212 if(__x == _T_r.end())
01213 return _VDBL_rowid();
01214 ++__x;
01215 if(__x == _T_r.end())
01216 return _VDBL_rowid();
01217 else
01218 return (*__x).first;
01219 }
01220
01221 _VDBL_rowid _prev_row(const _VDBL_rowid& _ci) const
01222 {
01223 _T_rows::const_iterator __x = _T_r.find(_ci);
01224 if(__x == _T_r.end())
01225 return _VDBL_rowid();
01226 --__x;
01227 if(__x == _T_r.end())
01228 return _VDBL_rowid();
01229 else
01230 return (*__x).first;
01231 }
01232
01233 row_const_iterator row_begin() const
01234 { if(_T_r.empty())
01235 return row_const_iterator(this, 0);
01236 else
01237 return row_const_iterator(this, _T_r.begin()->first);
01238 }
01239 row_const_iterator row_end() const
01240 { return row_const_iterator(this, 0); }
01241
01242 row_const_iterator* row_iterator_copy(const row_const_iterator& r) const
01243 { return new row_const_iterator(r); }
01244 };
01245
01247
01251 class table : public _VDBL_table
01252 {
01253 public:
01254 typedef _VDBL_row row;
01255 typedef _VDBL_col col;
01256 typedef __VDBL_index index;
01257 typedef _VDBL_context context;
01258 typedef _VDBL_col def;
01259
01260 typedef _VDBL_table::_T_colspec col_spec;
01261 };
01262
01264
01268 class col_spec : public _VDBL_table::_T_colspec
01269 {
01270 private:
01271 typedef _VDBL_table::_T_colspec _Base;
01272
01273 public:
01275
01279 col_spec(const std::string& __s, const _VDBL_col& __c)
01280 : _Base(std::make_pair(__s,__c)) {}
01281
01282 col_spec(const char* __s, const _VDBL_col& __c)
01283 : _Base(std::make_pair(std::string(__s),__c)) {}
01285
01287
01291 template <class _CR>
01292 col_spec(const std::string& __s, const _CR& __c)
01293 : _Base(std::make_pair(__s,_VDBL_col(__c))) {}
01294
01295 template <class _CR>
01296 col_spec(const char* __s, const _CR& __c)
01297 : _Base(std::make_pair(std::string(__s),_VDBL_col(__c))) {}
01299
01303 col_spec(const col_spec& __c) : _Base(__c) {}
01304
01308 virtual ~col_spec() {}
01309 };
01310
01312
01317 class standard_table : public _VDBL_standardtable
01318 {
01319 private:
01320 typedef _VDBL_standardtable _Base;
01321 public:
01322 typedef _VDBL_colflags colflags;
01323 typedef __VDBL_index index;
01324 typedef _VDBL_context context;
01325 typedef _VDBL_col def;
01326
01330 standard_table() : _Base() {}
01334 standard_table(const standard_table& __t) : _Base(__t) {}
01335
01340 template <template <class __Tp, class __AllocTp> class __SequenceCtr,
01341 class Allocator1>
01342 standard_table(const __SequenceCtr<std::triple<std::string, _VDBL_col,
01343 _VDBL_colflags>,Allocator1>& __cc) : _Base(__cc) {}
01344
01348 virtual ~standard_table() {}
01349
01353 virtual standard_table* new_copy() { return new standard_table(*this); }
01354
01358 virtual void destroy_copy(standard_table* t) { delete (standard_table*)t; }
01359
01361
01366 template <class _CB>
01367 bool add_col(const char* _C_n, const _CB& __c, const _VDBL_colflags& __f)
01368 { return _Base::add_col(std::string(_C_n), _VDBL_col(__c), __f); }
01369
01370 template <class _CB>
01371 bool add_col(const std::string& _C_n, const _CB& __c,
01372 const _VDBL_colflags& __f)
01373 { return _Base::add_col(_C_n, _VDBL_col(__c), __f); }
01375
01381 bool insert(const std::vector<_T_ptrcolspec>& _row, rowid& _ri)
01382 { return _Base::insert(_row, _ri); }
01383
01388 bool insert(const std::vector<_T_ptrcolspec>& _row)
01389 { rowid dummy; return _Base::insert(_row, dummy); }
01390
01398 template <template <class __Tp, class __AllocTp> class __SequenceCtr,
01399 class Allocator1>
01400 bool insert_row(const __SequenceCtr<col_spec,Allocator1>& _row, rowid& _ri)
01401 {
01402 typename __SequenceCtr<col_spec,Allocator1>::const_iterator __x;
01403 std::vector<_T_ptrcolspec> __v;
01404 __v.reserve(_row.size());
01405 for(__x = _row.begin(); __x != _row.end(); ++__x)
01406 __v.push_back(std::make_pair(&(*__x).first, &(*__x).second));
01407 return this->insert(__v, _ri);
01408 }
01409
01416 template <template <class __Tp, class __AllocTp> class __SequenceCtr,
01417 class Allocator1>
01418 bool insert_row(const __SequenceCtr<col_spec,Allocator1>& _row)
01419 {
01420 _VDBL_colid dummy;
01421 return this->insert_row(_row, dummy);
01422 }
01423
01431 template <template <class __Tp1, class __AllocTp1> class __SequenceCtrOut,
01432 template <class __Tp2, class __AllocTp2> class __SequenceCtrIn,
01433 class AllocatorOut, class AllocatorIn>
01434 bool insert_row(const __SequenceCtrOut<__SequenceCtrIn<col_spec,AllocatorIn>,
01435 AllocatorOut>& _rows)
01436 {
01437 typedef typename __SequenceCtrOut<__SequenceCtrIn<col_spec,AllocatorIn>,
01438 AllocatorOut>::const_iterator _OutCIt;
01439 _OutCIt __x;
01440 bool ret = true;
01441
01442 for(__x = _rows.begin(); __x != _rows.end(); ++__x)
01443 if(!insert_row(*__x))
01444 ret = false;
01445 return ret;
01446 }
01447
01453 bool update(const std::vector<_T_ptrcolspec>& _row, _VDBL_rowid& _ri)
01454 {
01455 return _Base::update(_row, _ri);
01456 }
01457
01466 template <template <class __Tp, class __AllocTp> class __SequenceCtr,
01467 class Allocator1>
01468 bool update_row(const __SequenceCtr<col_spec,Allocator1>& _row, rowid& _ri)
01469 {
01470 typename __SequenceCtr<col_spec,Allocator1>::const_iterator __x;
01471 std::vector<_T_ptrcolspec> __v;
01472 __v.reserve(_row.size());
01473 for(__x = _row.begin(); __x != _row.end(); ++__x)
01474 __v.push_back(std::make_pair(&(*__x).first, &(*__x).second));
01475 return this->update(__v, _ri);
01476 }
01477
01482 const row& get_row(const rowid& _ri, bool& error) const
01483 { return *(row*)&_Base::get_row(_ri,error); }
01484
01489 const row* get_row_ptr(const rowid& _ri) const
01490 { return (const row*)_Base::get_row_ptr(_ri); }
01491
01496 row& get_row(const rowid& _ri, bool& error)
01497 { return *(row*)&_Base::get_row(_ri,error); }
01498
01500
01503 colid get_colid(const std::string& _C_n) const
01504 { return _Base::get_col_id(_C_n); }
01505
01506 colid get_colid(const char* _C_n) const
01507 { return _Base::get_col_id(std::string(_C_n)); }
01509
01513 std::string get_colname(const colid& _C_i) const
01514 { return _Base::get_col_name(_C_i); }
01515
01519 row_const_iterator row_begin() const { return _Base::row_begin(); }
01523 row_const_iterator row_end() const { return _Base::row_end(); }
01524 };
01525
01526 #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
01527 #pragma reset woff 1209
01528 #endif
01529
01530 __VDBL_END_NAMESPACE
01531
01532 #endif
01533
01534
01535
01536