Main Page   Modules   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

vdbl_table.h

Go to the documentation of this file.
00001 // Table implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2003 Hermann Schichl
00004 //
00005 // This file is part of the Vienna Database Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the Library GNU General Public License as published by the
00008 // Free Software Foundation; either version 2, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // Library GNU General Public License for more details.
00015 
00016 // As a special exception, you may use this file as part of a free software
00017 // library without restriction.  Specifically, if other files instantiate
00018 // templates or use macros or inline functions from this file, or you compile
00019 // this file and link it with other files to produce an executable, this
00020 // file does not by itself cause the resulting executable to be covered by
00021 // the Library GNU General Public License.  This exception does not however
00022 // invalidate any other reasons why the executable file might be covered by
00023 // the Library GNU General Public License.
00024 
00030 #ifndef __VDBL_TABLE_H
00031 #define __VDBL_TABLE_H
00032 
00033 #include <algorithm>
00034 #include <map>
00035 #include <set>
00036 #include <vector>
00037 #include <string>
00038 #include <vdbl_config.h>
00039 #include <vdbl_row.h>
00040 #include <vdbl_col.h>
00041 #include <vdbl_index.h>
00042 #include <vdbl_context.h>
00043 #include <vdbl_triple.h>
00044 #include <vdbl_view.h>
00045 
00046 __VDBL_BEGIN_NAMESPACE
00047 
00048 #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
00049 #pragma set woff 1209
00050 #endif
00051 
00053 
00054 static _VDBL_row ___empty_row_return;
00055 
00057 
00067 class _VDBL_table
00068 {
00069 private:
00070   friend class _VDBL_view;
00071 
00072 public:
00077   typedef std::pair<std::string,_VDBL_col> _T_colspec;
00082   typedef std::pair<const std::string*,const _VDBL_col*> _T_ptrcolspec;
00083 
00084 private:
00086 
00087   _VDBL_colid _T_ci;
00088   _VDBL_rowid _T_ri;
00090 
00091   unsigned int last_change;
00092 
00093 protected:
00095 
00098   _VDBL_colid get_colid() { return ++_T_ci; }
00099   _VDBL_rowid get_rowid() { return ++_T_ri; }
00101 
00105   void made_change() { last_change++; }
00106 
00107 public:
00115   virtual const std::type_info& get_colinfo(const std::string& _C_n,
00116                              triple<bool,_VDBL_colid,_VDBL_colflags>& _r) const
00117                                                         VDBL_PURE_VIRTUAL
00118 
00119 public:
00123   unsigned int get_change_ctr() const { return last_change; }
00124 
00125 public:
00131   class _col_iterator_base
00132   {
00133   private:
00134     typedef std::pair<std::string,_VDBL_colid> entry;
00135   public:
00136     typedef size_t size_type;
00137     typedef ptrdiff_t difference_type;
00138     typedef std::bidirectional_iterator_tag iterator_category;
00139 
00140   protected:
00141     entry _CI_ci;
00142     const _VDBL_table* _CI_t;
00143 
00144   public:
00145     _col_iterator_base(const _VDBL_table* __x, const entry& __c)
00146                 : _CI_ci(__c), _CI_t(__x) { }
00147 
00148     _col_iterator_base() : _CI_ci(), _CI_t(NULL) {}
00149 
00150     void _D_incr() { _CI_ci = _CI_t->_next_col(_CI_ci); }
00151 
00152     void _D_decr() { _CI_ci = _CI_t->_prev_col(_CI_ci); }
00153 
00154     
00155     bool operator==(const _col_iterator_base& __x) const
00156               { return _CI_t == __x._CI_t && _CI_ci == __x._CI_ci; }
00157 
00158     bool operator!=(const _col_iterator_base& __x) const
00159               { return _CI_t != __x._CI_t || _CI_ci != __x._CI_ci; }
00160   };
00161 
00167   template<typename _Tp, typename _Ref, typename _Ptr>
00168   struct _col_iterator : public _col_iterator_base
00169   {
00170   public:
00171     typedef _col_iterator<_Tp,_Tp&,_Tp*>             iterator;
00172     typedef _col_iterator<_Tp,const _Tp&,const _Tp*> const_iterator;
00173     typedef _col_iterator<_Tp,_Ref,_Ptr>             _Self;
00174 
00175     typedef _Tp value_type;
00176     typedef _Ptr pointer;
00177     typedef _Ref reference;
00178 
00179     _col_iterator(const _VDBL_table* __x,
00180                   const std::pair<std::string,_VDBL_colid>& __c)
00181                                 : _col_iterator_base(__x,__c) { }
00182 
00183     _col_iterator(const _VDBL_table* __x, const std::string& __s,
00184                   const _VDBL_colid& __c)
00185                          : _col_iterator_base(__x,std::make_pair(__s,__c)) { }
00186 
00187     _col_iterator() : _col_iterator_base() { }
00188 
00189     _col_iterator(const iterator& __x)
00190         : _col_iterator_base(__x._CI_t, __x._CI_ci) { }
00191 
00192     reference operator*() const { return _CI_ci; }
00193 
00194     pointer operator->() const { return &(operator*()); }
00195 
00196     _Self& operator++() { this->_D_incr(); return *this; }
00197 
00198     _Self operator++(int)
00199         { _Self __tmp = *this; this->_D_incr(); return __tmp; }
00200 
00201     _Self& operator--() { this->_D_decr(); return *this; }
00202 
00203     _Self operator--(int)
00204         { _Self __tmp = *this; this->_D_decr(); return __tmp; }
00205   };
00206 
00210   typedef _col_iterator<std::pair<std::string,_VDBL_colid>,
00211                         const std::pair<std::string,_VDBL_colid>&,
00212                         const std::pair<std::string,_VDBL_colid>*>
00213                                                 col_const_iterator;
00214 
00220   virtual std::pair<std::string,_VDBL_colid> _next_col(
00221         const std::pair<std::string,_VDBL_colid>& _ci)
00222                                         const VDBL_PURE_VIRTUAL
00228   virtual std::pair<std::string,_VDBL_colid> _prev_col(
00229         const std::pair<std::string,_VDBL_colid>& _ci)
00230                                         const VDBL_PURE_VIRTUAL
00234   virtual col_const_iterator col_begin() const VDBL_PURE_VIRTUAL
00238   virtual col_const_iterator col_end() const VDBL_PURE_VIRTUAL
00239 
00245   class _row_iterator_base
00246   {
00247   private:
00248     typedef _VDBL_rowid entry;
00249   public:
00250     typedef size_t size_type;
00251     typedef ptrdiff_t difference_type;
00252     typedef std::bidirectional_iterator_tag iterator_category;
00253 
00254   protected:
00255     entry _CI_ci;
00256     const _VDBL_table* _CI_t;
00257 
00258   public:
00259     _row_iterator_base(const _VDBL_table* __x, const entry& __c)
00260                 : _CI_ci(__c), _CI_t(__x) { }
00261 
00262     _row_iterator_base() : _CI_ci(), _CI_t(NULL) {}
00263 
00264     void _D_incr() { _CI_ci = _CI_t->_next_row(_CI_ci); }
00265 
00266     void _D_decr() { _CI_ci = _CI_t->_prev_row(_CI_ci); }
00267 
00268     
00269     bool operator==(const _row_iterator_base& __x) const
00270               { return _CI_t == __x._CI_t && _CI_ci == __x._CI_ci; }
00271 
00272     bool operator!=(const _row_iterator_base& __x) const
00273               { return _CI_t != __x._CI_t || _CI_ci != __x._CI_ci; }
00274   };
00275 
00281   template<typename _Tp, typename _Ref, typename _Ptr>
00282   struct _row_iterator : public _row_iterator_base
00283   {
00284   public:
00285     typedef _row_iterator<_Tp,_Tp&,_Tp*>             iterator;
00286     typedef _row_iterator<_Tp,const _Tp&,const _Tp*> const_iterator;
00287     typedef _row_iterator<_Tp,_Ref,_Ptr>             _Self;
00288 
00289     typedef _Tp value_type;
00290     typedef _Ptr pointer;
00291     typedef _Ref reference;
00292 
00293     _row_iterator(const _VDBL_table* __x, const _VDBL_rowid& __c)
00294                                 : _row_iterator_base(__x,__c) { }
00295 
00296     _row_iterator() : _row_iterator_base() { }
00297 
00298     _row_iterator(const iterator& __x)
00299         : _row_iterator_base(__x._CI_t, __x._CI_ci) { }
00300 
00301     reference operator*() const { return _CI_ci; }
00302 
00303     pointer operator->() const { return &(operator*()); }
00304 
00305     _Self& operator++() { this->_D_incr(); return *this; }
00306 
00307     _Self operator++(int)
00308         { _Self __tmp = *this; this->_D_incr(); return __tmp; }
00309 
00310     _Self& operator--() { this->_D_decr(); return *this; }
00311 
00312     _Self operator--(int)
00313         { _Self __tmp = *this; this->_D_decr(); return __tmp; }
00314   };
00315 
00319   typedef _row_iterator<_VDBL_rowid, const _VDBL_rowid&, const _VDBL_rowid*>
00320                                                 row_const_iterator;
00321 
00327   virtual _VDBL_rowid _next_row(const _VDBL_rowid& _ci)
00328                                         const VDBL_PURE_VIRTUAL
00334   virtual _VDBL_rowid _prev_row(const _VDBL_rowid& _ci)
00335                                         const VDBL_PURE_VIRTUAL
00339   virtual row_const_iterator row_begin() const VDBL_PURE_VIRTUAL
00343   virtual row_const_iterator row_end() const VDBL_PURE_VIRTUAL
00344 
00345 public:
00349   _VDBL_table() : _T_ci(0), _T_ri(0), last_change(0) {}
00353   _VDBL_table(const _VDBL_table& __t) : _T_ci(__t._T_ci), _T_ri(__t._T_ri),
00354                                         last_change(0) {}
00355 
00360   template <template <class __Tp, class __AllocTp> class __SequenceCtr,
00361             class Allocator1>
00362   _VDBL_table(const __SequenceCtr<triple<std::string, _VDBL_col,
00363               _VDBL_colflags>,Allocator1>& __cc) : _T_ci(0), _T_ri(0),
00364                                                    last_change(0) {}
00365 
00369   virtual ~_VDBL_table() {}
00370 
00376   virtual bool add_col(const std::string& _C_n, const _VDBL_col& __c,
00377                        const _VDBL_colflags& __f) VDBL_PURE_VIRTUAL
00378 
00384   virtual bool modify_col(const std::string& _C_n, const _VDBL_col& __c,
00385                           const _VDBL_colflags& __f) VDBL_PURE_VIRTUAL
00386 
00391   virtual bool modify_col(const std::string& _C_n, const _VDBL_col& __c)
00392         VDBL_PURE_VIRTUAL
00393 
00398   virtual bool modify_col(const std::string& _C_n, const _VDBL_colflags& __f)
00399         VDBL_PURE_VIRTUAL
00400 
00405   virtual bool drop_col(const std::string& _C_n) VDBL_PURE_VIRTUAL
00406 
00411   virtual bool rename_col(const std::string& _C_old, const std::string& _C_new)
00412         VDBL_PURE_VIRTUAL
00413 
00419   virtual bool insert(const std::vector<_T_ptrcolspec>& _row, _VDBL_rowid& _r)
00420         VDBL_PURE_VIRTUAL
00421 
00426   virtual bool insert(const std::vector<_T_ptrcolspec>& _row) VDBL_PURE_VIRTUAL
00427 
00435   template <template <class __Tp, class __AllocTp> class __SequenceCtr,
00436             class Allocator1>
00437   bool insert_row(const __SequenceCtr<_T_colspec,Allocator1>& _row)
00438   {
00439     typename __SequenceCtr<_T_colspec,Allocator1>::const_iterator __x;
00440     std::vector<_T_ptrcolspec> __v;
00441     __v.reserve(_row.size());
00442     for(__x = _row.begin(); __x != _row.end(); ++__x)
00443       __v.push_back(std::make_pair(&(*__x).first, &(*__x).second));
00444     return this->insert(__v);
00445   }
00446 
00454   template <template <class __Tp1, class __AllocTp1> class __SequenceCtrOut,
00455             template <class __Tp2, class __AllocTp2> class __SequenceCtrIn,
00456             class AllocatorOut, class AllocatorIn>
00457   bool insert_row(const __SequenceCtrOut<__SequenceCtrIn<_T_colspec,AllocatorIn>,
00458                         AllocatorOut>& _rows)
00459   {
00460     typedef typename __SequenceCtrOut<__SequenceCtrIn<_T_colspec,AllocatorIn>,
00461                                 AllocatorOut>::const_iterator _OutCIt;
00462     _OutCIt __x;
00463     bool ret = true;
00464     
00465     for(__x = _rows.begin(); __x != _rows.end(); ++__x)
00466       if(!insert(*__x))
00467         ret = false;
00468     return ret;
00469   }
00470 
00474   virtual bool remove(const _VDBL_rowid _ri) VDBL_PURE_VIRTUAL
00475 
00479   virtual bool has_col(const std::string& _C_n) const VDBL_PURE_VIRTUAL
00480 
00485   virtual const _VDBL_row& get_row(const _VDBL_rowid& _ri, bool& error) const
00486         VDBL_PURE_VIRTUAL
00487 
00492   virtual const _VDBL_row* get_row_ptr(const _VDBL_rowid& _ri) const
00493         VDBL_PURE_VIRTUAL
00494 
00499   virtual _VDBL_row& get_row(const _VDBL_rowid& _ri, bool& error)
00500         VDBL_PURE_VIRTUAL
00501 
00506   virtual const _VDBL_col& get_def(const _VDBL_colid& _ci, bool& error) const
00507         VDBL_PURE_VIRTUAL
00508 
00513   virtual _VDBL_col& get_def(const _VDBL_colid& _ci, bool& error)
00514         VDBL_PURE_VIRTUAL
00515 
00519   virtual bool has_def(const _VDBL_colid& _ci) const
00520         VDBL_PURE_VIRTUAL
00521 
00525   virtual bool has_col(const _VDBL_rowid& _ri, const _VDBL_colid& _ci) const
00526         VDBL_PURE_VIRTUAL
00527 
00535   virtual bool retrieve(const _VDBL_rowid& _r, const _VDBL_colid& _c,
00536                 const _VDBL_context& _ctx, _VDBL_alltype_base*& _val) const
00537         VDBL_PURE_VIRTUAL
00538 
00539   virtual bool create_index(const std::string& _C_i) VDBL_PURE_VIRTUAL
00540   virtual bool alter_index(const std::string& _C_i) VDBL_PURE_VIRTUAL
00541   virtual bool drop_index(const std::string& _C_i) VDBL_PURE_VIRTUAL
00542 };
00543 
00545 
00550 class _VDBL_standardtable : public _VDBL_table
00551 {
00552 private:
00553   typedef _VDBL_table _Base;
00554 
00555   typedef std::pair<_VDBL_colflags,_VDBL_col> _T_col_entry;
00556   typedef std::map<std::string, _VDBL_colid> _T_cols;
00557   typedef std::map<_VDBL_colid, _T_col_entry> _T_defaults;
00558   typedef std::map<_VDBL_colid,__VDBL_index> _T_indices;
00559   typedef std::map<_VDBL_rowid,_VDBL_row> _T_rows;
00560 
00561   friend class _VDBL_view;
00562 
00563 public:
00568   typedef std::pair<std::string,_VDBL_col> _T_colspec;
00573   typedef std::pair<const std::string*,const _VDBL_col*> _T_ptrcolspec;
00574   
00578   typedef _Base::col_const_iterator col_const_iterator;
00579 
00580 private:
00582   _T_cols _T_c;
00584   _T_defaults _T_d;
00586   _T_indices _T_i;
00588   _T_rows _T_r;
00589 
00590 public:
00591   const std::type_info& get_colinfo(const std::string& _C_n,
00592                              triple<bool,_VDBL_colid,_VDBL_colflags>& _r) const
00593   {
00594     _T_cols::const_iterator _x;
00595     _x = _T_c.find(_C_n);
00596     if(_x != _T_c.end())
00597     {
00598       _T_defaults::const_iterator _y(_T_d.find((*_x).second));
00599       if(_y == _T_d.end())
00600         throw "_VDBL_table::get_colinfo: inconsistency - this can't happen!";
00601       _r = make_triple(true, (*_x).second, (*_y).second.first);
00602       return (*_y).second.second.return_type_id();
00603     }
00604     else
00605     {
00606       _r = make_triple(false, _VDBL_colid(), _VDBL_colflags());
00607       return typeid(void);
00608     }
00609   }
00610 
00614   _VDBL_colid get_col_id(const std::string& _C_n) const
00615   {
00616     _T_cols::const_iterator _x;
00617     _x = _T_c.find(_C_n);
00618     if(_x != _T_c.end())
00619       return (*_x).second;
00620     else
00621       return _VDBL_colid();
00622   }
00623   
00624 public:
00628   _VDBL_standardtable() : _Base(), _T_c(), _T_d(), _T_i(), _T_r() {}
00632   _VDBL_standardtable(const _VDBL_standardtable& __t) : _Base(__t),
00633                                         _T_c(__t._T_c), _T_d(__t._T_d),
00634                                         _T_i(__t._T_i), _T_r(__t._T_r) {}
00635 
00640   template <template <class __Tp, class __AllocTp> class __SequenceCtr,
00641             class Allocator1>
00642   _VDBL_standardtable(const __SequenceCtr<triple<std::string, _VDBL_col,
00643               _VDBL_colflags>,Allocator1>& __cc) : _Base(), _T_c(),
00644                                                    _T_d(), _T_i(), _T_r()
00645   {
00646     typedef typename __SequenceCtr<triple<std::string, _VDBL_col,
00647                              _VDBL_colflags>,Allocator1>::const_iterator _CIt;
00648 
00649     for(_CIt __b = __cc.begin(); __b != __cc.end(); ++__b)
00650     {
00651       const std::string& _s((*__b).first);
00652       const _VDBL_col& _c((*__b).second);
00653       const _VDBL_colflags& _f((*__b).third);
00654       _T_cols::iterator __x(_T_c.find(_s));
00655       if(__x == _T_c.end())
00656       {
00657         _VDBL_colid _ci(get_colid());
00658         _T_c.insert(std::make_pair(_s,_ci));
00659         _T_d.insert(std::make_pair(_ci, std::make_pair(_c,_f)));
00660       }
00661       else
00662       {
00663         std::cerr << "Warning: Doubly defined column " << _s << " ignored!" <<
00664           std::endl;
00665       } 
00666     }
00667   }
00668 
00672   virtual ~_VDBL_standardtable() {}
00673 
00674   bool add_col(const std::string& _C_n, const _VDBL_col& __c,
00675                const _VDBL_colflags& __f)
00676   {
00677     _T_cols::iterator __x = _T_c.find(_C_n);
00678     if(__x == _T_c.end())
00679     {
00680       _VDBL_colid _ci(get_colid());
00681       _T_c.insert(std::make_pair(_C_n,_ci));
00682       _T_d.insert(std::make_pair(_ci,std::make_pair(__f,__c)));
00683       made_change();
00684       return true;
00685     }
00686     else
00687       return false;
00688   }
00689 
00690   bool modify_col(const std::string& _C_n, const _VDBL_col& __c,
00691                   const _VDBL_colflags& __f)
00692   {
00693     _T_cols::iterator __x(_T_c.find(_C_n));
00694     if(__x != _T_c.end())
00695     {
00696       _VDBL_colid _ci = (*__x).second;
00697       _T_defaults::iterator __y(_T_d.find(_ci));
00698       if(__y == _T_d.end())
00699         throw "_VDBL_standardtable::modify_col: inconsistency - this can't happen!";
00700       if(!VDBL_CMP_TYPEID((*__y).second.second.return_type_id(),
00701                           __c.return_type_id()))
00702         // must not change the basic type of the column
00703         return false;
00704       _T_d.erase(__y);
00705       _T_d.insert(std::make_pair(_ci,std::make_pair(__f,__c)));
00706       made_change();
00707       return true;
00708     }
00709     else
00710       return false;
00711   }
00712 
00713   bool modify_col(const std::string& _C_n, const _VDBL_col& __c)
00714   {
00715     _T_cols::iterator __x(_T_c.find(_C_n));
00716     if(__x != _T_c.end())
00717     {
00718       _VDBL_colid _ci = (*__x).second;
00719       _T_defaults::iterator __y(_T_d.find(_ci));
00720       if(__y == _T_d.end())
00721         throw "_VDBL_standardtable::modify_col: inconsistency - this can't happen!";
00722       if(!VDBL_CMP_TYPEID((*__y).second.second.return_type_id(),
00723                            __c.return_type_id()))
00724         // must not change the basic type of the column
00725         return false;
00726       _VDBL_colflags __f = (*__y).second.first;
00727       _T_d.erase(__y);
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_colflags& __f)
00737   {
00738     _T_cols::iterator __x(_T_c.find(_C_n));
00739     if(__x != _T_c.end())
00740     {
00741       _VDBL_colid _ci = (*__x).second;
00742       _T_defaults::iterator __y(_T_d.find(_ci));
00743       if(__y == _T_d.end())
00744         throw "_VDBL_standardtable::modify_col: inconsistency - this can't happen!";
00745       _VDBL_col __c = (*__y).second.second;
00746       _T_d.erase(__y);
00747       _T_d.insert(std::make_pair(_ci,std::make_pair(__f,__c)));
00748       made_change();
00749       return true;
00750     }
00751     else
00752       return false;
00753   }
00754 
00755   bool drop_col(const std::string& _C_n)
00756   {
00757     _T_cols::iterator __x = _T_c.find(_C_n);
00758     if(__x != _T_c.end())
00759     {
00760       _VDBL_colid _ci = (*__x).second;
00761       _T_defaults::iterator __y(_T_d.find(_ci));
00762       if(__y == _T_d.end())
00763         throw "_VDBL_standardtable::drop_col: inconsistency - this can't happen!";
00764       // now remove all occurrences of _C_n in the rows
00765       _T_d.erase(__y);
00766       for(_T_rows::iterator __r = _T_r.begin(); __r != _T_r.end(); ++__r)
00767         (*__r).second.drop(_ci);
00768       made_change();
00769       return true;
00770     }
00771     else
00772       return false;
00773   }
00774 
00775   bool rename_col(const std::string& _C_old, const std::string& _C_new)
00776   {
00777     _T_cols::iterator __x = _T_c.find(_C_new);
00778     if(__x != _T_c.end())
00779       return false;       // new column name exists already
00780     __x = _T_c.find(_C_old);
00781     if(__x != _T_c.end())
00782     {
00783       _T_c.insert(std::make_pair(_C_new,(*__x).second));
00784       _T_c.erase(__x);
00785       made_change();
00786       return true;
00787     }
00788     else
00789       return false;      // old column name does not exist
00790   }
00791 
00792   bool insert(const std::vector<_T_ptrcolspec>& _row, _VDBL_rowid& _ri)
00793   {
00794     std::vector<_T_ptrcolspec>::const_iterator __x;
00795     bool ret = true;
00796     _VDBL_row _r;
00797     std::set<_VDBL_colid> _isdef;
00798 
00799     for(__x = _row.begin(); __x != _row.end(); ++__x)
00800     {
00801       const _T_ptrcolspec& _c(*__x);
00802       triple<bool, _VDBL_colid, _VDBL_colflags> _ru;
00803       const std::type_info& _rt(get_colinfo(*_c.first, _ru));
00804       if(_isdef.find(_ru.second) != _isdef.end())
00805       { // column is doubly defined
00806         ret = false;
00807         break;
00808       }
00809       if(!_ru.first || !VDBL_CMP_TYPEID(_rt, _c.second->return_type_id()))
00810       { // value of the default col and the defined col are incompatible
00811         ret = false;
00812         break;
00813       }
00814       else
00815       {
00816         if(_ru.third.master_index)
00817         {
00818 #if _VDBL_DEBUG
00819           std::cout <<
00820                 "We should check the constraints here in table::insert" <<
00821                 std::endl;
00822 #endif
00823         }
00824         _isdef.insert(_ru.second);
00825         _r.insert(_ru.second,*_c.second);
00826       }
00827     }
00828     // now check whether all columns without default are actually defined
00829     if(ret)
00830     {
00831       for(_T_defaults::const_iterator __b = _T_d.begin(); __b != _T_d.end();
00832           ++__b)
00833       {
00834         const _T_col_entry& __de((*__b).second);
00835         if(!__de.first.has_default && _isdef.find((*__b).first) == _isdef.end())
00836         // this col must be defined but isn't
00837         {
00838           ret = false;
00839           break;
00840         }
00841       }
00842     }
00843     if(ret)
00844     {
00845       _ri = get_rowid();
00846       _T_r.insert(std::make_pair(_ri, _r));
00847       made_change();
00848     }
00849     return ret;
00850   }
00851 
00852   bool insert(const std::vector<_T_ptrcolspec>& _row)
00853   {
00854     _VDBL_rowid dummy;
00855     return insert(_row,dummy);
00856   }
00857 
00858   bool remove(const _VDBL_rowid _ri)
00859   {
00860     _T_rows::iterator __x = _T_r.find(_ri);
00861     if(__x == _T_r.end())
00862       return false;
00863     else
00864     {
00865       _T_r.erase(__x);
00866       made_change();
00867       return true;
00868     }
00869   }
00870 
00871   bool has_col(const std::string& _C_n) const
00872   {
00873     _T_cols::const_iterator _x(_T_c.find(_C_n));
00874     return _x != _T_c.end();
00875   }
00876 
00877   const _VDBL_row& get_row(const _VDBL_rowid& _ri, bool& error) const
00878   {
00879     _T_rows::const_iterator _x(_T_r.find(_ri));
00880     if(_x == _T_r.end())
00881     {
00882       error = true;
00883       return ___empty_row_return;
00884     }
00885     else
00886     {
00887       error = false;
00888       return (*_x).second;
00889     }
00890   }
00891 
00892   const _VDBL_row* get_row_ptr(const _VDBL_rowid& _ri) const
00893   {
00894     _T_rows::const_iterator _x(_T_r.find(_ri));
00895     if(_x == _T_r.end())
00896       return NULL;
00897     else
00898       return &(*_x).second;
00899   }
00900 
00901   _VDBL_row& get_row(const _VDBL_rowid& _ri, bool& error)
00902   {
00903     _T_rows::iterator _x(_T_r.find(_ri));
00904     if(_x == _T_r.end())
00905     {
00906       error = true;
00907       return ___empty_row_return;
00908     }
00909     else
00910     {
00911       error = false;
00912       return (*_x).second;
00913     }
00914   }
00915 
00916   const _VDBL_col& get_def(const _VDBL_colid& _ci, bool& error) const
00917   {
00918     _T_defaults::const_iterator _x(_T_d.find(_ci));
00919     if(_x == _T_d.end() || !(*_x).second.first.has_default)
00920     {
00921       error = true;
00922       return ___empty_col_return;
00923     }
00924     else
00925     {
00926       error = false;
00927       return (*_x).second.second;
00928     }
00929   }
00930 
00931   _VDBL_col& get_def(const _VDBL_colid& _ci, bool& error)
00932   {
00933     _T_defaults::iterator _x(_T_d.find(_ci));
00934     if(_x == _T_d.end() || !(*_x).second.first.has_default)
00935     {
00936       error = true;
00937       return ___empty_col_return;
00938     }
00939     else
00940     {
00941       error = false;
00942       return (*_x).second.second;
00943     }
00944   }
00945 
00946   virtual bool has_def(const _VDBL_colid& _ci) const
00947   {
00948     _T_defaults::const_iterator _x(_T_d.find(_ci));
00949     if(_x == _T_d.end() || !(*_x).second.first.has_default)
00950       return false;
00951     else
00952       return true;
00953   }
00954 
00955   virtual bool has_col(const _VDBL_rowid& _ri, const _VDBL_colid& _ci) const
00956   {
00957     bool error;
00958     const _VDBL_row& _r(get_row(_ri, error));
00959     if(error)
00960       return false;
00961     return _r.has_col(_ci);
00962   }
00963 
00964   bool retrieve(const _VDBL_rowid& _r, const _VDBL_colid& _c,
00965                 const _VDBL_context* _ctx, _VDBL_alltype_base*& _val) const
00966   {
00967     bool ret;
00968     const _VDBL_row& _row(get_row(_r, ret));
00969     if(ret)
00970       return false;
00971 
00972     _VDBL_col _col(_row.get_col(_c, ret));
00973     if(ret)
00974     {           // row does not contain this col -> get default
00975       _T_defaults::const_iterator _x(_T_d.find(_c));
00976       if(_x == _T_d.end())      // there is no default
00977         return false;
00978       _VDBL_col _col2((*_x).second.second);
00979       _col2.setcontext(_ctx, &_row);
00980       _col2.def_copy(_val);
00981       return true;
00982     }
00983     else
00984     {           // row contains this col
00985       _col.setcontext(_ctx, &_row);
00986       _col.get_copy(_val);
00987       return true;
00988     }
00989   }
00990 
00991   bool create_index(const std::string& _C_i) { throw "create_index: NYI"; }
00992   bool alter_index(const std::string& _C_i) { throw "alter_index: NYI"; }
00993   bool drop_index(const std::string& _C_i) { throw "drop_index: NYI"; }
00994 
00995 public:
00996   std::pair<std::string,_VDBL_colid> _next_col(
00997                         const std::pair<std::string,_VDBL_colid>& _ci) const
00998   {
00999     _T_cols::const_iterator __x = _T_c.find(_ci.first);
01000     if(__x == _T_c.end())
01001       return std::make_pair("", _VDBL_colid());
01002     ++__x;
01003     if(__x == _T_c.end())
01004       return std::make_pair("", _VDBL_colid());
01005     else
01006       return *__x;
01007   }
01008 
01009   virtual std::pair<std::string,_VDBL_colid> _prev_col(
01010                          const std::pair<std::string,_VDBL_colid>& _ci) const
01011   {
01012     _T_cols::const_iterator __x = _T_c.find(_ci.first);
01013     if(__x == _T_c.end())
01014       return std::make_pair("", _VDBL_colid());
01015     --__x;
01016     if(__x == _T_c.end())
01017       return std::make_pair("", _VDBL_colid());
01018     else
01019       return *__x;
01020   }
01021 
01022   col_const_iterator col_begin() const
01023         { if(_T_c.empty())
01024             return col_const_iterator(this, "", 0);
01025           else
01026             return col_const_iterator(this, *_T_c.begin());
01027         }
01028   col_const_iterator col_end() const
01029         { return col_const_iterator(this, "", 0); }
01030 
01031   _VDBL_rowid _next_row(const _VDBL_rowid& _ci) const
01032   {
01033     _T_rows::const_iterator __x = _T_r.find(_ci);
01034     if(__x == _T_r.end())
01035       return _VDBL_rowid();
01036     ++__x;
01037     if(__x == _T_r.end())
01038       return _VDBL_rowid();
01039     else
01040       return (*__x).first;
01041   }
01042 
01043   _VDBL_rowid _prev_row(const _VDBL_rowid& _ci) const
01044   {
01045     _T_rows::const_iterator __x = _T_r.find(_ci);
01046     if(__x == _T_r.end())
01047       return _VDBL_rowid();
01048     --__x;
01049     if(__x == _T_r.end())
01050       return _VDBL_rowid();
01051     else
01052       return (*__x).first;
01053   }
01054 
01055   row_const_iterator row_begin() const
01056         { if(_T_r.empty())
01057             return row_const_iterator(this, 0);
01058           else
01059             return row_const_iterator(this, _T_r.begin()->first);
01060         }
01061   row_const_iterator row_end() const
01062         { return row_const_iterator(this, 0); }
01063 };
01064 
01066 
01070 class table : public _VDBL_table
01071 {
01072 public:
01073   typedef _VDBL_row row;
01074   typedef _VDBL_col col;
01075   typedef __VDBL_index index;
01076   typedef _VDBL_context context;
01077   typedef _VDBL_col def;
01078 
01079   typedef _VDBL_table::_T_colspec col_spec;
01080 };
01081 
01083 
01087 class col_spec : public _VDBL_table::_T_colspec
01088 {
01089 private:
01090   typedef _VDBL_table::_T_colspec _Base;
01091 
01092 public:
01094 
01098   col_spec(const std::string& __s, const _VDBL_col& __c)
01099         : _Base(std::make_pair(__s,__c)) {}
01100   
01101   col_spec(const char* __s, const _VDBL_col& __c)
01102         : _Base(std::make_pair(std::string(__s),__c)) {}
01104   
01106 
01110   template <class _CR>
01111   col_spec(const std::string& __s, const _CR& __c)
01112         : _Base(std::make_pair(__s,_VDBL_col(__c))) {}
01113 
01114   template <class _CR>
01115   col_spec(const char* __s, const _CR& __c)
01116         : _Base(std::make_pair(std::string(__s),_VDBL_col(__c))) {}
01118 
01122   col_spec(const col_spec& __c) : _Base(__c) {}
01123 
01127   virtual ~col_spec() {}
01128 };
01129 
01131 
01136 class standard_table : public _VDBL_standardtable
01137 {
01138 private:
01139   typedef _VDBL_standardtable _Base;
01140 public:
01141   typedef _VDBL_colflags colflags;
01142   typedef __VDBL_index index;
01143   typedef _VDBL_context context;
01144   typedef _VDBL_col def;
01145 
01149   standard_table() : _Base() {}
01153   standard_table(const standard_table& __t) : _Base(__t) {}
01154 
01159   template <template <class __Tp, class __AllocTp> class __SequenceCtr,
01160             class Allocator1>
01161   standard_table(const __SequenceCtr<triple<std::string, _VDBL_col,
01162               _VDBL_colflags>,Allocator1>& __cc) : _Base(__c) {}
01163 
01167   virtual ~standard_table() {}
01168 
01170 
01175   template <class _CB>
01176   bool add_col(const char* _C_n, const _CB& __c, const _VDBL_colflags& __f)
01177   { return _Base::add_col(std::string(_C_n), _VDBL_col(__c), __f); }
01178 
01179   template <class _CB>
01180   bool add_col(const std::string& _C_n, const _CB& __c,
01181                const _VDBL_colflags& __f)
01182   { return _Base::add_col(_C_n, _VDBL_col(__c), __f); }
01184 
01190   bool insert(const std::vector<_T_ptrcolspec>& _row, rowid& _ri)
01191   { return _Base::insert(_row, _ri); }
01192 
01197   bool insert(const std::vector<_T_ptrcolspec>& _row)
01198   { rowid dummy; return _Base::insert(_row, dummy); }
01199 
01207   template <template <class __Tp, class __AllocTp> class __SequenceCtr,
01208             class Allocator1>
01209   bool insert_row(const __SequenceCtr<col_spec,Allocator1>& _row, rowid& _ri)
01210   {
01211     typename __SequenceCtr<col_spec,Allocator1>::const_iterator __x;
01212     std::vector<_T_ptrcolspec> __v;
01213     __v.reserve(_row.size());
01214     for(__x = _row.begin(); __x != _row.end(); ++__x)
01215       __v.push_back(std::make_pair(&(*__x).first, &(*__x).second));
01216     return this->insert(__v, _ri);
01217   }
01218 
01225   template <template <class __Tp, class __AllocTp> class __SequenceCtr,
01226             class Allocator1>
01227   bool insert_row(const __SequenceCtr<col_spec,Allocator1>& _row)
01228   {
01229     _VDBL_colid dummy;
01230     return this->insert_row(_row, dummy);
01231   }
01232 
01240   template <template <class __Tp1, class __AllocTp1> class __SequenceCtrOut,
01241             template <class __Tp2, class __AllocTp2> class __SequenceCtrIn,
01242             class AllocatorOut, class AllocatorIn>
01243   bool insert_row(const __SequenceCtrOut<__SequenceCtrIn<col_spec,AllocatorIn>,
01244                         AllocatorOut>& _rows)
01245   {
01246     typedef typename __SequenceCtrOut<__SequenceCtrIn<col_spec,AllocatorIn>,
01247                                 AllocatorOut>::const_iterator _OutCIt;
01248     _OutCIt __x;
01249     bool ret = true;
01250     
01251     for(__x = _rows.begin(); __x != _rows.end(); ++__x)
01252       if(!insert_row(*__x))
01253         ret = false;
01254     return ret;
01255   }
01256   
01261   const row& get_row(const rowid& _ri, bool& error) const
01262   { return *(row*)&_Base::get_row(_ri,error); }
01263 
01268   const row* get_row_ptr(const rowid& _ri) const
01269   { return (const row*)_Base::get_row_ptr(_ri); }
01270 
01275   row& get_row(const rowid& _ri, bool& error)
01276   { return *(row*)&_Base::get_row(_ri,error); }
01277 
01279 
01282   colid get_colid(const std::string& _C_n) const
01283   { return _Base::get_col_id(_C_n); }
01284 
01285   colid get_colid(const char* _C_n) const
01286   { return _Base::get_col_id(std::string(_C_n)); }
01288 };
01289 
01290 #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
01291 #pragma reset woff 1209
01292 #endif
01293 
01294 __VDBL_END_NAMESPACE
01295 
01296 #endif /* __VDBL_TABLE_H */
01297 
01298 // Local Variables:
01299 // mode:C++
01300 // End:

Generated on Tue Nov 4 01:29:11 2003 for Vienna Database Library by doxygen1.2.18