X-Git-Url: https://git.teslayout.com/public/public/public/?a=blobdiff_plain;f=source%2Fdbms.mysql%2FConnection.cpp;fp=source%2Fdbms.mysql%2FConnection.cpp;h=aae802c4e94a1ab9b316b39305a86fffdfce7eba;hb=78be86969d2f26a9084b0c4af6ce43d5fa4ed3fd;hp=0000000000000000000000000000000000000000;hpb=a3b95648bd76140ef55e0b5941d423eee6c3856f;p=anna.git diff --git a/source/dbms.mysql/Connection.cpp b/source/dbms.mysql/Connection.cpp new file mode 100644 index 0000000..aae802c --- /dev/null +++ b/source/dbms.mysql/Connection.cpp @@ -0,0 +1,103 @@ +// ANNA - Anna is Not Nothingness Anymore // +// // +// (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo // +// // +// See project site at http://redmine.teslayout.com/projects/anna-suite // +// See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE // + + +#include + +#include +#include +#include + +#include +#include +#include + +using namespace std; +using namespace anna; +using namespace anna::dbms; + +mysql::Connection::Connection(Database& database, const std::string& name, const char* user, const char* password) : + dbms::Connection(database, name, user, password), + a_mysqlDatabase(database), + a_mysql(NULL) { +} + +void mysql::Connection::open() +throw(dbms::DatabaseException) { + if(a_mysql != NULL) { + LOGWARNING( + string msg = asString(); + msg += " | Has already been established"; + Logger::warning(msg, ANNA_FILE_LOCATION); + ); + return; + } + + if((a_mysql = mysql_init(NULL)) == NULL) + RuntimeException("Cannot initiate MySQL", ANNA_FILE_LOCATION); + + const char* dbmsName = (a_mysqlDatabase.getType() == Database::Type::Remote) ? a_mysqlDatabase.getName().c_str() : NULL; + + try { + if(mysql_real_connect(a_mysql, a_mysqlDatabase.getHost(), a_user.c_str(), a_password.c_str(), dbmsName, 0, NULL, 0L) == NULL) { + ResultCode resultCode(a_mysql); + throw DatabaseException(resultCode, ANNA_FILE_LOCATION); + } + + LOGINFORMATION( + string msg("anna::dbms::mysql::Connection::open | "); + msg += asString(); + Logger::information(msg, ANNA_FILE_LOCATION); + ); + } catch(DatabaseException& edbms) { + close(); + throw; + } +} + +void mysql::Connection::close() +throw() { + LOGINFORMATION( + string msg("anna::dbms::mysql::Connection::close | "); + msg += asString(); + Logger::information(msg, ANNA_FILE_LOCATION); + ); + + if(a_mysql != NULL) { + mysql_close(a_mysql); + a_mysql = NULL; + LOGINFORMATION( + string msg("anna::dbms::mysql::Connection::close | "); + msg += asString(); + Logger::information(msg, ANNA_FILE_LOCATION); + ); + } +} + +void mysql::Connection::do_commit() +throw(RuntimeException, dbms::DatabaseException) { + anna_dbms_mysql_check(mysql_commit(a_mysql), a_mysql); +} + +void mysql::Connection::do_rollback() +throw() { + try { + anna_dbms_mysql_check(mysql_rollback(a_mysql), a_mysql); + } catch(Exception& ex) { + ex.trace(); + } +} + +string mysql::Connection::asString() const +throw() { + string result("dbms::mysql::Connection { "); + result += dbms::Connection::asString(); + result += " | Context: "; + result += (a_mysql == NULL) ? "(null)" : functions::asHexString(anna_ptrnumber_cast(a_mysql)); + return result += " }"; +} +