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_VTABLE_H
00032 #define __VDBL_VTABLE_H
00033
00034 #include <vdbl_table.h>
00035 #include <vdbl_view.h>
00036
00037 __VDBL_BEGIN_NAMESPACE
00038
00039 #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
00040 #pragma set woff 1209
00041 #endif
00042
00047 class _VDBL_viewtable : public _VDBL_table
00048 {
00049 private:
00050 typedef _VDBL_table _Base;
00051
00052 typedef std::pair<_VDBL_colflags,_VDBL_col> _T_col_entry;
00053 typedef std::map<std::string, _VDBL_colid> _T_cols;
00054 typedef std::map<_VDBL_colid, _T_col_entry> _T_defaults;
00055 typedef std::map<_VDBL_colid,__VDBL_index> _T_indices;
00056 typedef std::map<_VDBL_rowid,_VDBL_row> _T_rows;
00057 typedef std::map<_VDBL_rowid,std::map<_VDBL_tableid,_VDBL_rowid> > _T_rowref;
00058 typedef std::map<_VDBL_colid,std::pair<_VDBL_colid,_VDBL_tableid> > _T_colref;
00059
00060 friend class _VDBL_view;
00061
00062 public:
00063 typedef std::pair<std::string,_VDBL_col> _T_colspec;
00064
00065 private:
00066 _VDBL_view* _T_v;
00067 unsigned int _T_v_lastchg;
00068
00069 _T_indices _T_i;
00070 _T_rows _T_r;
00071
00072 _T_colref _T_cr;
00073 _T_rowref _T_rr;
00074
00075 _T_rows _T_rowcache;
00076
00077 private:
00078 const std::type_info& get_colinfo(const std::string& _C_n,
00079 std::triple<bool,_VDBL_colid,_VDBL_colflags>& _r) const
00080 {
00081 const _VDBL_col* _x;
00082 return _T_v->get_colinfo(_C_n, _r);
00083 }
00084
00085 public:
00086 _VDBL_viewtable(_VDBL_view& _v) : _Base(), _T_v(&_v), _T_i(), _T_r(), _T_cr(),
00087 _T_rr(), _T_rowcache()
00088 {
00089 for(_VDBL_view::row_const_iterator __x = _T_v->rows_begin();
00090 __x != _T_v->rows_end(); ++__x)
00091 _T_rr.insert(get_rowid(), *__x);
00092 for(_VDBL_view::default_const_iterator __x = _T_v->defaults_begin();
00093 __x != _T_v->defaults_end(); ++__x)
00094 _T_cr.insert(get_colid(), *__x);
00095 _T_v_lastchg = _T_v->get_change_ctr();
00096 }
00097
00098 _VDBL_viewtable(const _VDBL_viewtable& __t) : _Base(__t), _T_v(__t._T_v),
00099 _T_v_lastchg(__t._T_v_lastchg),
00100 _T_i(__t._T_i), _T_r(__t._T_r),
00101 _T_cr(__t._T_cr), _T_rr(__t._T_rr)
00102 _T_rowcache() {}
00103
00104 virtual ~_VDBL_viewtable() {}
00105
00106 bool add_col(const std::string& _C_n, const _VDBL_col& __c,
00107 const _VDBL_colflags& __f)
00108 {
00109 return false;
00110 }
00111
00112 bool modify_col(const std::string& _C_n, const _VDBL_col& __c,
00113 const _VDBL_colflags& __f)
00114 {
00115 return false;
00116 }
00117
00118 bool modify_col(const std::string& _C_n, const _VDBL_col& __c)
00119 {
00120 return false;
00121 }
00122
00123 bool modify_col(const std::string& _C_n, const _VDBL_colflags& __f)
00124 {
00125 return false;
00126 }
00127
00128 bool drop_col(const std::string& _C_n)
00129 {
00130 return false;
00131 }
00132
00133 bool rename_col(const std::string& _C_old, const std::string& _C_new)
00134 {
00135 return false;
00136 }
00137
00138 bool insert(const std::vector<_T_colspec>& _row)
00139 {
00140 if(_T_v->view_type() == _VDBL_V_window)
00141 {
00142 bool ret = _T_v->insert(_row);
00143 if(ret)
00144 made_change();
00145 return ret;
00146 }
00147 else if(_T_v->view_type() == _VDBL_V_materialized)
00148 return false;
00149 else
00150 {
00151 std::vector<_T_colspec>::const_iterator __x;
00152 bool ret = true;
00153 _VDBL_row _r;
00154 std::set<_VDBL_colid> _isdef;
00155
00156 for(__x = _row.begin(); __x != _row.end(); ++__x)
00157 {
00158 _T_colspec& _c(*__x);
00159 std::triple<bool, _VDBL_colid, _VDBL_colflags> _ru;
00160 const std::type_info& _rt(get_colinfo(_c.first, _ru));
00161 if(_isdef.find(_ru.second) != _isdef.end())
00162 {
00163 ret = false;
00164 break;
00165 }
00166 if(!_ru.first || !VDBL_CMP_TYPEID(_rt, _c.second.return_type_id()))
00167 {
00168 ret = false;
00169 break;
00170 }
00171 else
00172 {
00173 if(_ru.third.is_master)
00174 {
00175 #if _VDBL_DEBUG
00176 std::cout <<
00177 "We should check the constraints here in table::insert" <<
00178 std::endl;
00179 #endif
00180 }
00181 _isdef.insert(_ru.second);
00182 _r.insert(_ru.second,_c.second);
00183 }
00184 }
00185
00186 if(ret)
00187 {
00188 for(_VDBL_view::defaults_const_iterator __b = _T_v->defaults_begin();
00189 __b != _T_v->defaults_end(); ++__b)
00190 {
00191 const _T_col_entry& __de((*__b).second);
00192 if(!__de.first.has_default &&
00193 _isdef.find((*__b).first) == _isdef.end())
00194
00195 {
00196 ret = false;
00197 break;
00198 }
00199 }
00200 }
00201 if(ret)
00202 {
00203 _T_r.insert(std::make_pair(get_rowid(), _r));
00204 made_change();
00205 }
00206 return ret;
00207 }
00208 }
00209
00210 bool remove(const _VDBL_rowid _ri)
00211 {
00213 if(_T_v->view_type() == _VDBL_V_window)
00214 {
00215 _T_rowref::iterator _v(_T_rr.find(_ri));
00216 if(_ri == _T_rr.end())
00217 return false;
00218 else
00219 {
00220 std::map<_VDBL_rowid,_VDBL_tableid>& _rowmap(_v.second);
00221 std::map<_VDBL_rowid,_VDBL_tableid>::iterator __r;
00222 bool ret = true;
00223 for(__r = _rowmap.begin(); __r != _rowmap.end(); ++__r)
00224 ret = _T_v->remove(*__r) && ret;
00225 made_change();
00226 return ret;
00227 }
00228 }
00229 else
00230 {
00231 _T_rows::iterator __x = _T_r.find(_ri);
00232 if(__x == _T_r.end())
00233 return false;
00234 else
00235 {
00236 _T_r.erase(__x);
00237 made_change();
00238 return true;
00239 }
00240 }
00241 }
00242
00243 bool has_col(const std::string& _C_n) const
00244 {
00245 return _T_v->has_col(_C_n);
00246 }
00247
00248 const _VDBL_row& get_row(const _VDBL_rowid& _ri, bool& error) const
00249 {
00250 _T_rows::const_iterator _x(_T_r.find(_ri));
00251 if(_x == _T_r.end())
00252 {
00253 if(_T_v->get_change_ctr() == _T_v_lastchg)
00254 {
00255 _x = _T_rowcache.find();
00256 if(_x != _T_rowcache.end())
00257 {
00258 error = false;
00259 return (*_x).second;
00260 }
00261 }
00262 else
00263 {
00264 _T_rowcache.erase(_T_rowcache.begin(), _T_rowcache.end());
00265 }
00266 _T_rowref::const_iterator _y(_T_rr.find(_ri));
00267 if(_y == _T_rr.end())
00268 {
00269 error = true;
00270 return ___empty_row_return;
00271 }
00272 const std::map<_VDBL_tableid,_VDBL_rowid>& _yr(_y.second);
00273
00274 _VDBL_row _rw;
00275 error = false;
00276 std::map<_VDBL_tableid,const _VDBL_row*> _fetched;
00277 std::map<_VDBL_tableid,const _VDBL_row*>::iterator _fit;
00278 for(_T_colref::const_iterator _cr = _T_cr.begin(); _cr != _T_cr.end();
00279 ++_cr)
00280 {
00281 const _VDBL_tableid& _ti((*_cr).second.second);
00282 if((_fit = _fetched.find(_ti)) == _fetched.end())
00283 {
00284 std::map<_VDBL_tableid,_VDBL_rowid>::const_iterator
00285 _rri(_yr.find(_ti));
00286 const _VDBL_row& _rrr(_T_v->get_row(*_rri));
00287 _fit = _fetched.insert(std::make_pair(_ti,&_rrr));
00288 }
00289 const _VDBL_col& _rrc((*_fit).second->get_col((*_cr).second.first,
00290 error));
00291 if(error)
00292 return ___empty_row_return;
00293 error = _rw.insert((*_cr).first,_rrc);
00294 if(error)
00295 return ___empty_row_return;
00296 }
00297 return _rw;
00298 }
00299 else
00300 {
00301 error = false;
00302 return (*_x).second;
00303 }
00304 }
00305
00306 const _VDBL_row* get_row_ptr(const _VDBL_rowid& _ri) const
00307 {
00308 _T_rows::const_iterator _x(_T_r.find(_ri));
00309 if(_x == _T_r.end())
00310 {
00311 if(_T_v->get_change_ctr() == _T_v_lastchg)
00312 {
00313 _x = _T_rowcache.find();
00314 if(_x != _T_rowcache.end())
00315 return &(*_x).second;
00316 }
00317 else
00318 {
00319 _T_rowcache.erase(_T_rowcache.begin(), _T_rowcache.end());
00320 }
00321 _T_rowref::const_iterator _y(_T_rr.find(_ri));
00322 if(_y == _T_rr.end())
00323 return NULL;
00324
00325 const std::map<_VDBL_tableid,_VDBL_rowid>& _yr(_y.second);
00326
00327 _VDBL_row _rw;
00328 bool error(false);
00329 std::map<_VDBL_tableid,const _VDBL_row*> _fetched;
00330 std::map<_VDBL_tableid,const _VDBL_row*>::iterator _fit;
00331 for(_T_colref::const_iterator _cr = _T_cr.begin(); _cr != _T_cr.end();
00332 ++_cr)
00333 {
00334 const _VDBL_tableid& _ti((*_cr).second.second);
00335 if((_fit = _fetched.find(_ti)) == _fetched.end())
00336 {
00337 std::map<_VDBL_tableid,_VDBL_rowid>::const_iterator
00338 _rri(_yr.find(_ti));
00339 const _VDBL_row& _rrr(_T_v->get_row(*_rri));
00340 _fit = _fetched.insert(std::make_pair(_ti,&_rrr));
00341 }
00342 const _VDBL_col& _rrc((*_fit).second->get_col((*_cr).second.first,
00343 error));
00344 if(error)
00345 return NULL;
00346 error = _rw.insert((*_cr).first,_rrc);
00347 if(error)
00348 return NULL;
00349 }
00350 return &_rw;
00351 }
00352 else
00353 return &(*_x).second;
00354 }
00355
00356 _VDBL_row& get_row(const _VDBL_rowid& _ri, bool& error)
00357 {
00358 _T_rows::iterator _x(_T_r.find(_ri));
00359 if(_x == _T_r.end())
00360 {
00361 if(_T_v->get_change_ctr() == _T_v_lastchg)
00362 {
00363 _x = _T_rowcache.find();
00364 if(_x != _T_rowcache.end())
00365 {
00366 error = false;
00367 return (*_x).second;
00368 }
00369 }
00370 else
00371 {
00372 _T_rowcache.erase(_T_rowcache.begin(), _T_rowcache.end());
00373 }
00374 _T_rowref::iterator _y(_T_rr.find(_ri));
00375 if(_y == _T_rr.end())
00376 {
00377 error = true;
00378 return ___empty_row_return;
00379 }
00380 std::map<_VDBL_tableid,_VDBL_rowid>& _yr(_y.second);
00381
00382 _VDBL_row _rw;
00383 error = false;
00384 std::map<_VDBL_tableid,_VDBL_row*> _fetched;
00385 std::map<_VDBL_tableid,_VDBL_row*>::iterator _fit;
00386 for(_T_colref::const_iterator _cr = _T_cr.begin(); _cr != _T_cr.end();
00387 ++_cr)
00388 {
00389 _VDBL_tableid& _ti((*_cr).second.second);
00390 if((_fit = _fetched.find(_ti)) == _fetched.end())
00391 {
00392 std::map<_VDBL_tableid,_VDBL_rowid>::iterator
00393 _rri(_yr.find(_ti));
00394 _VDBL_row& _rrr(_T_v->get_row(*_rri));
00395 _fit = _fetched.insert(std::make_pair(_ti,&_rrr));
00396 }
00397 _VDBL_col& _rrc((*_fit).second->get_col((*_cr).second.first,
00398 error));
00399 if(error)
00400 return ___empty_row_return;
00401 error = _rw.insert((*_cr).first,_rrc);
00402 if(error)
00403 return ___empty_row_return;
00404 }
00405 _x = _T_rowcache.insert(_ri,_rw);
00406 return (*_x).second;
00407 }
00408 else
00409 {
00410 error = false;
00411 return (*_x).second;
00412 }
00413 }
00414
00415 bool retrieve(const _VDBL_rowid& _r, const _VDBL_colid& _c,
00416 const _VDBL_context& _ctx, _VDBL_alltype_base*& _val) const
00417 {
00418 bool ret;
00419 const _VDBL_row& _row(get_row(_r, ret));
00420 if(ret)
00421 return false;
00422
00423 const _VDBL_col& _col(_row.get_col(_c, ret));
00424 if(ret)
00425 {
00426 _T_defaults::const_iterator _x(_T_d.find(_c));
00427 if(_x == _T_d.end())
00428 return false;
00429 _VDBL_col _col2((*_x).second.second);
00430 if(!VDBL_CMP_TYPEID(_col2.return_type_id(), typeid(_TR)))
00431 return false;
00432 _col2.setcontext(_ctx, &_row);
00433 _col2.def_copy(_val);
00434 return true;
00435 }
00436 else
00437 {
00438 if(!VDBL_CMP_TYPEID(_col.return_type_id(), typeid(_TR)))
00439 return false;
00440 _col.setcontext(_ctx, &_row);
00441 _col.get_copy(_val);
00442 return true;
00443 }
00444 }
00445
00446 bool create_index(const std::string& _C_i) { throw "create_index: NYI"; }
00447 bool alter_index(const std::string& _C_i) { throw "alter_index: NYI"; }
00448 bool drop_index(const std::string& _C_i) { throw "drop_index: NYI"; }
00449 };
00450
00451 class view_table : _VDBL_viewtable
00452 {
00453 public:
00454 typedef _VDBL_row row;
00455 typedef _VDBL_col col;
00456 typedef __VDBL_index index;
00457 typedef _VDBL_context context;
00458 typedef _VDBL_col def;
00459
00460 typedef _VDBL_table::_T_colspec col_spec;
00461 };
00462
00463 #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
00464 #pragma reset woff 1209
00465 #endif
00466
00467 __VDBL_END_NAMESPACE
00468
00469 #endif
00470
00471
00472
00473
00474