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 "wsqldatabase.h" 00020 00021 #include "wsqldriver.h" 00022 #include "drivers/wsqlitedriver.h" 00023 #include "drivers/wmysqldriver.h" 00024 00025 namespace WSql { 00026 00093 WSqlDatabase::WSqlDatabase(const WSql::DriverType &type) 00094 { 00095 _driver=0; 00096 _driverType=type; 00097 _isValid=false; 00098 init(); 00099 } 00100 00104 WSqlDatabase::WSqlDatabase() 00105 { 00106 _driver=0; 00107 _isValid=false; 00108 } 00109 00118 WSqlDatabase::WSqlDatabase(const WSqlDatabase &other) 00119 { 00120 _databaseName = other._databaseName; 00121 _userName = other._userName; 00122 _password = other._password; 00123 _hostname = other._hostname; 00124 _port = other._port; 00125 _connectionOptions = other._connectionOptions; 00126 _driverType = other._driverType; 00127 init(); 00128 } 00129 00137 WSqlDatabase::~WSqlDatabase() 00138 { 00139 if(isValid()){ 00140 close(); 00141 } 00142 delete _driver; 00143 } 00144 00152 WSqlDatabase &WSqlDatabase::operator=(const WSqlDatabase &other) 00153 { 00154 if(_driver) 00155 delete _driver; 00156 _driver=0; 00157 00158 _databaseName = other._databaseName; 00159 _userName = other._userName; 00160 _password = other._password; 00161 _hostname = other._hostname; 00162 _port = other._port; 00163 _connectionOptions = other._connectionOptions; 00164 _driverType = other._driverType; 00165 init(); 00166 return *this; 00167 } 00168 00173 void WSqlDatabase::init() 00174 { 00175 if(_driver) 00176 delete _driver; 00177 00178 _isValid = initDriver(); 00179 } 00180 00185 bool WSqlDatabase::initDriver() 00186 { 00187 bool blnToReturn = false; 00188 std::string errmsg; 00189 00190 switch(_driverType) 00191 { 00192 case WSql::NONE: 00193 errmsg = "WSqlDatabase: initDriver failed - no driver type set!"; 00194 break; 00195 case WSql::WSQLITE : 00196 _driver = new WSqliteDriver(this); 00197 if(0 == _driver) 00198 errmsg = "WSqlDatabase: initDriver failed - memory allocation error!"; 00199 else 00200 blnToReturn = true; 00201 break; 00202 case WSql::WMYSQL : 00203 _driver = new WMysqlDriver(this); 00204 if(0 == _driver) 00205 errmsg = "WSqlDatabase: initDriver failed - memory allocation error!"; 00206 else 00207 blnToReturn = true; 00208 break; 00209 case WSql::WMONGO : 00210 case WSql::WBERKLEYDB : 00211 case WSql::WPSQL : 00212 default: 00213 errmsg = "WSqlDatabase: initDriver failed - driver unsupported!"; 00214 } 00215 if(!errmsg.empty()) 00216 _errorStack.push_back(WSqlError(errmsg,1, WSqlError::SYSTEM, WSqlError::WARNING)); 00217 return blnToReturn; 00218 } 00219 00230 bool WSqlDatabase::open() 00231 { 00232 return _driver->open(); 00233 } 00234 00245 bool WSqlDatabase::open(const std::string& username, const std::string& password) 00246 { 00247 setUserName(username); 00248 setPassword(password); 00249 return _driver->open(); 00250 } 00251 00256 void WSqlDatabase::close() 00257 { 00258 _driver->close(); 00259 } 00260 00265 bool WSqlDatabase::isOpen() const 00266 { 00267 return _driver->isOpen(); 00268 } 00269 00275 bool WSqlDatabase::hasError() const 00276 { 00278 return _driver->hasError(); 00279 } 00280 00281 00292 void WSqlDatabase::setDatabaseName(const std::string& name) 00293 { 00294 _databaseName = name; 00295 } 00296 00309 void WSqlDatabase::setUserName(const std::string& name) 00310 { 00311 _userName = name; 00312 } 00313 00326 void WSqlDatabase::setPassword(const std::string& password) 00327 { 00328 _password = password; 00329 } 00330 00342 void WSqlDatabase::setHostName(const std::string& hostname) 00343 { 00344 _hostname = hostname; 00345 } 00346 00359 void WSqlDatabase::setPort(int port) 00360 { 00361 _port = port; 00362 } 00363 00368 std::string WSqlDatabase::databaseName() const 00369 { 00370 return _databaseName; 00371 } 00372 00378 std::string WSqlDatabase::userName() const 00379 { 00380 return _userName; 00381 } 00382 00387 std::string WSqlDatabase::password() const 00388 { 00389 return _password; 00390 } 00391 00397 std::string WSqlDatabase::hostName() const 00398 { 00399 return _hostname; 00400 } 00401 00420 WSqlDriver* WSqlDatabase::driver() const 00421 { 00422 return _driver; 00423 } 00424 00430 WSqlError WSqlDatabase::error() const 00431 { 00432 return _driver->error(); 00433 } 00434 00442 // WSqlError WSqlDatabase::errors() const 00443 // { 00444 // return _driver->getError(); 00445 // } 00446 00447 00467 const std::vector<std::string>& WSqlDatabase::tableNames(WSql::TableType type) 00468 { 00469 if(_tableNames.empty()) 00470 _tableNames = _driver->tableNames(); 00471 return _tableNames; 00472 } 00473 00509 WSqlTable WSqlDatabase::tableMetaData( const std::string& tablename ) const 00510 { 00511 if(!isValid()) 00512 return WSqlTable(); 00513 return _driver->tableMetaData(tablename); 00514 } 00515 00532 void WSqlDatabase::initMetaData() 00533 { 00534 //load all tablenames 00535 tableNames(); 00536 //init metadata for all tables 00537 std::vector<std::string>::const_iterator it = _tableNames.begin(); 00538 for(;it != _tableNames.end();++it ) 00539 tableMetaData(*it); 00540 std::vector<WSqlTable>::const_iterator tbl_it = _driver->_tables.begin(); 00541 //add referenced keys 00542 for(;tbl_it != _driver->_tables.end();++tbl_it) 00543 { 00544 std::vector<WSqlForeignKey>::const_iterator fk_it = tbl_it->foreignKeys().begin(); 00545 for(;fk_it != tbl_it->foreignKeys().end();++fk_it) 00546 { 00547 WSqlTable *table = _driver->getTable( fk_it->referencedTableName()); 00548 if(table) 00549 { 00550 WSqlReferencedKey rfk(*fk_it); 00551 table->addReferencedKey(rfk); 00552 } 00553 } 00554 } 00555 } 00556 00557 00571 void WSqlDatabase::setConnectOptions(const std::string &options) 00572 { 00573 _connectionOptions = options; 00574 } 00575 00582 std::string WSqlDatabase::connectionOptions() const 00583 { 00584 return _connectionOptions; 00585 } 00586 00590 bool WSqlDatabase::isValid() const 00591 { 00592 return (_isValid && 0 != _driver && _driver->isValid()); 00593 } 00602 bool WSqlDatabase::query(const std::string& sql) 00603 { 00604 return _driver->query(sql); 00605 } 00640 WSqlResult * WSqlDatabase::result(bool iscached) 00641 { 00642 return _driver->result(iscached); 00643 } 00644 00645 00646 } //namespace WSql