WORM 0.2
A C++ DAL/ORM code generation framework
|
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 "wsqltable.h" 00020 00021 namespace WSql { 00022 00071 WSqlTable::WSqlTable() 00072 { 00073 _type = NORMAL; 00074 _isValid = false; 00075 } 00081 WSqlTable::WSqlTable( const std::string name ) 00082 { 00083 _type = NORMAL; 00084 _name = name; 00085 _isValid = false; 00086 } 00087 00091 WSqlTable::WSqlTable( const WSqlTable &other ) 00092 { 00093 _type = other._type; 00094 _isValid = other._isValid; 00095 _name = other._name; 00096 _className = other._className; 00097 _columns = other._columns; 00098 _foreignKeys = other._foreignKeys; 00099 _referencedKeys = other._referencedKeys; 00100 } 00104 WSqlTable& WSqlTable::operator=( const WSqlTable& other ) 00105 { 00106 _type = other._type; 00107 _isValid = other._isValid; 00108 _name = other._name; 00109 _className = other._className; 00110 _columns = other._columns; 00111 _foreignKeys = other._foreignKeys; 00112 _referencedKeys = other._referencedKeys; 00113 return *this; 00114 } 00118 WSqlTable::~WSqlTable() 00119 { 00120 _columns.clear(); 00121 _foreignKeys.clear(); 00122 _referencedKeys.clear(); 00123 } 00139 bool WSqlTable::operator==( const WSqlTable &other ) const 00140 { 00141 if(_isValid != other._isValid) 00142 return false; 00143 if(_type != other._type) 00144 return false; 00145 if ( _columns.size() != other._columns.size() 00146 || _name.compare( other._name ) != 0 ) 00147 return false; 00148 00149 std::vector<WSqlColumn>::const_iterator it; 00150 std::vector<WSqlColumn>::const_iterator ito = other._columns.begin(); 00151 for ( it = _columns.begin();it != _columns.end();++it ) 00152 { 00153 if ( ito == other._columns.end() || *( it ) != *( ito ) ) 00154 return false; 00155 else 00156 ito++; 00157 } 00158 std::vector<WSqlForeignKey>::const_iterator fit; 00159 std::vector<WSqlForeignKey>::const_iterator fito = other._foreignKeys.begin(); 00160 for ( fit = _foreignKeys.begin(); fit != _foreignKeys.end(); ++fit ) 00161 { 00162 if ( fito == other._foreignKeys.end() || *( fit ) != *( fito ) ) 00163 return false; 00164 else 00165 fito++; 00166 } 00167 std::vector<WSqlReferencedKey>::const_iterator rfit; 00168 std::vector<WSqlReferencedKey>::const_iterator rfito = other._referencedKeys.begin(); 00169 for ( rfit = _referencedKeys.begin();rfit != _referencedKeys.end();++rfit ) 00170 { 00171 if ( rfito == other._referencedKeys.end() || *( rfit ) != *( rfito ) ) 00172 return false; 00173 else 00174 rfito++; 00175 } 00176 return true; 00177 } 00178 00183 const std::string WSqlTable::columnName( int index ) const 00184 { 00185 std::string strToReturn; 00186 int sz = _columns.size(); 00187 if ( !sz || index < 0 || index > sz - 1 ) 00188 return strToReturn; 00189 std::vector<WSqlColumn>::const_iterator it = _columns.begin(); 00190 it += index; 00191 return it->columnName(); 00192 } 00193 00198 WSqlColumn WSqlTable::column( int index ) const 00199 { 00200 WSqlColumn clmToReturn; 00201 int sz = _columns.size(); 00202 if ( !sz || index < 0 || index > sz - 1 ) 00203 return clmToReturn; 00204 std::vector<WSqlColumn>::const_iterator it = _columns.begin(); 00205 it += index; 00206 return *it; 00207 } 00208 00213 WSqlColumn WSqlTable::column( const std::string &fldname ) const 00214 { 00215 WSqlColumn clmToReturn; 00216 if ( _columns.empty() ) 00217 return clmToReturn; 00218 00219 std::vector<WSqlColumn>::const_iterator it; 00220 for ( it = _columns.begin(); it != _columns.end(); ++it ) 00221 { 00222 if ( it->columnName().compare( fldname ) == 0 ) 00223 { 00224 clmToReturn = *it; 00225 break; 00226 } 00227 } 00228 return clmToReturn; 00229 } 00230 00234 const std::vector<WSqlColumn>& WSqlTable::columns() const 00235 { 00236 return _columns; 00237 } 00241 std::vector<std::string> WSqlTable::columnNames() const 00242 { 00243 std::vector<std::string> vecToReturn; 00244 std::vector<WSqlColumn>::const_iterator it; 00245 for ( it = _columns.begin(); it != _columns.end(); ++it ) 00246 vecToReturn.push_back( it->columnName() ); 00247 return vecToReturn; 00248 } 00249 00250 int WSqlTable::count() const 00251 { 00252 return _columns.size(); 00253 } 00254 00255 int WSqlTable::indexOf( const std::string &columnname ) const 00256 { 00257 int intToReturn = -1; 00258 if ( _columns.empty() ) 00259 return intToReturn; 00260 int i = 0; 00261 std::vector<WSqlColumn>::const_iterator it; 00262 for ( it = _columns.begin(); it != _columns.end() ; ++it ) { 00263 if ( it->columnName().compare( columnname ) == 0 ) { 00264 intToReturn = i; 00265 break; 00266 } 00267 ++i; 00268 } 00269 return intToReturn; 00270 } 00271 00272 void WSqlTable::append( const WSqlColumn& column ) 00273 { 00274 _columns.push_back( column ); 00275 } 00276 00277 void WSqlTable::replace( int pos, const WSqlColumn& column ) 00278 { 00279 insert( pos, column, true ); 00280 } 00281 00282 void WSqlTable::insert( int pos, const WSqlColumn& column, bool replace ) 00283 { 00284 int sz = _columns.size(); 00285 if ( !sz || pos < 0 || pos > sz - 1 ) 00286 return; 00287 std::vector<WSqlColumn>::iterator it = _columns.begin(); 00288 it += pos; 00289 if ( replace ) 00290 _columns.erase( it ); 00291 _columns.insert( it, 1, column ); 00292 } 00293 00294 void WSqlTable::remove( int pos ) 00295 { 00296 int sz = _columns.size(); 00297 if ( !sz || pos < 0 || pos > sz - 1 ) 00298 return; 00299 std::vector<WSqlColumn>::iterator it = _columns.begin(); 00300 it += pos; 00301 _columns.erase( it ); 00302 } 00303 00304 bool WSqlTable::isEmpty() const 00305 { 00306 return _columns.empty(); 00307 } 00308 00309 WSqlForeignKey WSqlTable::foreignKey( const std::string columnname ) const 00310 { 00311 std::vector<WSqlForeignKey>::const_iterator it = _foreignKeys.begin(); 00312 for(;it != _foreignKeys.end(); ++it) 00313 { 00314 if (it->columnName().compare(columnname) == 0) 00315 { 00316 return *it; 00317 } 00318 } 00319 return WSqlForeignKey(); 00320 } 00321 00322 void WSqlTable::addForeignKey( const WSqlForeignKey& fk ) 00323 { 00324 _foreignKeys.push_back(fk); 00325 } 00326 00327 const std::vector< WSqlForeignKey >& WSqlTable::foreignKeys() const 00328 { 00329 return _foreignKeys; 00330 } 00331 00332 void WSqlTable::removeForeignKey( const WSqlForeignKey& fk ) 00333 { 00334 std::vector<WSqlForeignKey>::iterator it = _foreignKeys.begin(); 00335 for(;it != _foreignKeys.end(); ++it) 00336 { 00337 if (*(it) == fk) 00338 { 00339 _foreignKeys.erase(it); 00340 break; 00341 } 00342 } 00343 } 00344 00345 void WSqlTable::removeForeignKey( int pos ) 00346 { 00347 int sz = _foreignKeys.size(); 00348 if ( !sz || pos < 0 || pos > sz - 1 ) 00349 return; 00350 std::vector<WSqlForeignKey>::iterator it = _foreignKeys.begin(); 00351 it += pos; 00352 _foreignKeys.erase( it ); 00353 } 00354 00355 void WSqlTable::removeForeignKey( const std::string& keyname ) 00356 { 00357 if ( _foreignKeys.empty() ) 00358 return; 00359 00360 std::vector<WSqlForeignKey>::iterator it; 00361 for ( it = _foreignKeys.begin(); it != _foreignKeys.end(); ++it ) 00362 { 00363 if ( it->keyName().compare( keyname ) == 0 ) 00364 { 00365 _foreignKeys.erase(it); 00366 break; 00367 } 00368 } 00369 } 00370 00371 WSqlReferencedKey WSqlTable::referencedKey( const std::string columnname ) const 00372 { 00373 std::vector<WSqlReferencedKey>::const_iterator it = _referencedKeys.begin(); 00374 for(;it != _referencedKeys.end(); ++it) 00375 if (it->columnName().compare(columnname) == 0) 00376 return *it; 00377 return WSqlReferencedKey(WSqlForeignKey()); 00378 } 00379 00380 void WSqlTable::addReferencedKey( const WSqlReferencedKey& fk ) 00381 { 00382 _referencedKeys.push_back(fk); 00383 } 00384 00385 const std::vector< WSqlReferencedKey >& WSqlTable::referencedKeys() const 00386 { 00387 return _referencedKeys; 00388 } 00389 00390 void WSqlTable::removeReferencedKey( const WSqlReferencedKey& rk ) 00391 { 00392 std::vector<WSqlReferencedKey>::iterator it = _referencedKeys.begin(); 00393 for(;it != _referencedKeys.end(); ++it) 00394 { 00395 if (*(it) == rk) 00396 { 00397 _referencedKeys.erase(it); 00398 break; 00399 } 00400 } 00401 } 00402 00403 void WSqlTable::removeReferencedKey( int pos ) 00404 { 00405 int sz = _referencedKeys.size(); 00406 if ( !sz || pos < 0 || pos > sz - 1 ) 00407 return; 00408 std::vector<WSqlReferencedKey>::iterator it = _referencedKeys.begin(); 00409 it += pos; 00410 _referencedKeys.erase( it ); 00411 } 00412 00413 void WSqlTable::removeReferencedKey( const std::string& columnname ) 00414 { 00415 if ( _referencedKeys.empty() ) 00416 return; 00417 00418 std::vector<WSqlReferencedKey>::iterator it; 00419 for ( it = _referencedKeys.begin(); it != _referencedKeys.end(); ++it ) 00420 { 00421 if ( it->columnName().compare( columnname ) == 0 ) 00422 { 00423 _referencedKeys.erase(it); 00424 break; 00425 } 00426 } 00427 } 00428 00429 void WSqlTable::setName( const std::string& tablename ) 00430 { 00431 _name=tablename; 00432 _className=WSqlDataType::tableNameToClass(tablename); 00433 } 00434 00435 } // namespace WSql