1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
9 #include <mysql/mysql.h>
11 #include <anna/core/tracing/Logger.hpp>
12 #include <anna/core/tracing/TraceMethod.hpp>
13 #include <anna/core/functions.hpp>
15 #include <anna/dbms.mysql/Database.hpp>
16 #include <anna/dbms.mysql/Connection.hpp>
17 #include <anna/dbms.mysql/ResultCode.hpp>
21 using namespace anna::dbms;
23 mysql::Connection::Connection(Database& database, const std::string& name, const char* user, const char* password) :
24 dbms::Connection(database, name, user, password),
25 a_mysqlDatabase(database),
29 void mysql::Connection::open()
30 throw(dbms::DatabaseException) {
33 string msg = asString();
34 msg += " | Has already been established";
35 Logger::warning(msg, ANNA_FILE_LOCATION);
40 if((a_mysql = mysql_init(NULL)) == NULL)
41 RuntimeException("Cannot initiate MySQL", ANNA_FILE_LOCATION);
43 const char* dbmsName = (a_mysqlDatabase.getType() == Database::Type::Remote) ? a_mysqlDatabase.getName().c_str() : NULL;
46 if(mysql_real_connect(a_mysql, a_mysqlDatabase.getHost(), a_user.c_str(), a_password.c_str(), dbmsName, 0, NULL, 0L) == NULL) {
47 ResultCode resultCode(a_mysql);
48 throw DatabaseException(resultCode, ANNA_FILE_LOCATION);
52 string msg("anna::dbms::mysql::Connection::open | ");
54 Logger::information(msg, ANNA_FILE_LOCATION);
56 } catch(DatabaseException& edbms) {
62 void mysql::Connection::close()
65 string msg("anna::dbms::mysql::Connection::close | ");
67 Logger::information(msg, ANNA_FILE_LOCATION);
74 string msg("anna::dbms::mysql::Connection::close | ");
76 Logger::information(msg, ANNA_FILE_LOCATION);
81 void mysql::Connection::do_commit()
82 throw(RuntimeException, dbms::DatabaseException) {
83 anna_dbms_mysql_check(mysql_commit(a_mysql), a_mysql);
86 void mysql::Connection::do_rollback()
89 anna_dbms_mysql_check(mysql_rollback(a_mysql), a_mysql);
90 } catch(Exception& ex) {
95 string mysql::Connection::asString() const
97 string result("dbms::mysql::Connection { ");
98 result += dbms::Connection::asString();
99 result += " | Context: ";
100 result += (a_mysql == NULL) ? "(null)" : functions::asHexString(anna_ptrnumber_cast(a_mysql));
101 return result += " }";