WORM 0.2
A C++ DAL/ORM code generation framework

src/sql/wsqlresult.cpp

Go to the documentation of this file.
00001 /*
00002  *    WORM - a DAL/ORM code generation framework
00003  *    Copyright (C) 2011  Erik Winn <erikwinnmail@yahoo.com>
00004  * 
00005  *    This program is free software: you can redistribute it and/or modify
00006  *    it under the terms of the GNU General Public License as published by
00007  *    the Free Software Foundation, either version 3 of the License, or
00008  *    (at your option) any later version.
00009  * 
00010  *    This program is distributed in the hope that it will be useful,
00011  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *    GNU General Public License for more details.
00014  * 
00015  *    You should have received a copy of the GNU General Public License
00016  *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017  */
00018 
00019 #include "wsqlresult.h"
00020 
00021 namespace WSql {
00022 
00067 WSqlResult::WSqlResult(const WSqlDriver *driver)
00068 {
00069     _driver = driver;
00070     _isCached=true;
00071     _isValid = false;  // this should be set by the driver after initializing.
00072     const_cur_record_it = _records.begin();
00073 }
00074 
00075 WSqlResult::WSqlResult( const WSqlResult& other )
00076 {
00077     _driver = other._driver;
00078     _isCached = other._isCached;
00079     _isValid = other._isValid;
00080     _records = other._records;
00081     const_cur_record_it = other.const_cur_record_it;
00082 }
00083 
00084 WSqlResult& WSqlResult::operator=( const WSql::WSqlResult& other )
00085 {
00086     _driver = other._driver;
00087     _isCached = other._isCached;
00088     _isValid = other._isValid;
00089     _records = other._records;
00090     const_cur_record_it = other.const_cur_record_it;
00091     return *this;
00092 }
00093 
00097 WSqlResult::~WSqlResult()
00098 {
00099     _records.clear();
00100 }
00101 
00152 WSqlRecord WSqlResult::current()
00153 {
00154     if(_records.end() != const_cur_record_it)
00155         return *const_cur_record_it;
00156     else
00157         return WSqlRecord(); 
00158 }
00159 WSqlRecord WSqlResult::fetch(int pos)
00160 {
00161     int sz = _records.size();
00162     if ( !sz || pos < 0 || pos +1 > sz || !seek(pos, false) )
00163         return WSqlRecord();
00164     return *const_cur_record_it;
00165 }
00166 WSqlRecord WSqlResult::fetchFirst()
00167 {
00168     if(_records.empty() || !first())
00169         return WSqlRecord();
00170     return *const_cur_record_it;
00171 }
00172 WSqlRecord WSqlResult::fetchLast()
00173 {
00174     if(_records.empty() || !last())
00175         return WSqlRecord();
00176     return *const_cur_record_it;
00177 }
00178 WSqlRecord WSqlResult::fetchNext()
00179 {
00180     if(_records.empty() || !next())
00181         return WSqlRecord();
00182     return *const_cur_record_it;
00183 }
00184 WSqlRecord WSqlResult::fetchPrevious()
00185 {
00186     if(_records.empty() || !previous())
00187         return WSqlRecord();
00188     return *const_cur_record_it;
00189 }
00190 bool WSqlResult::first()
00191 {
00192     const_cur_record_it = _records.begin();
00193     return _records.end() != const_cur_record_it;
00194 }
00195 bool WSqlResult::last()
00196 {
00197     if(_records.empty())
00198         return false;
00199     const_cur_record_it = _records.end();
00200     const_cur_record_it--;
00201     return true;
00202 }
00203 bool WSqlResult::next()
00204 {
00205     return seek(1,true); 
00206 }
00207 bool WSqlResult::previous()
00208 {
00209     return seek(-1, true);
00210 }
00211 
00213 bool WSqlResult::seek( int pos, bool relative )
00214 {
00215     long curpos = (const_cur_record_it - _records.begin());
00216     int max = _records.size() - 1;
00217     if(pos < 0 || pos > max)
00218         return false;
00219     if(relative)
00220     {
00221         if(curpos + pos > max)
00222             return false;
00223     }else{
00224         const_cur_record_it = _records.begin();
00225     }
00226     const_cur_record_it += pos;
00227     return true;
00228 }
00229 
00230 } //namespace WSql
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Defines