Add no-deprecated to warnings due to dynamic exceptions.
[anna.git] / source / dbms.mysql / Connection.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
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 //
7
8
9 #include <mysql/mysql.h>
10
11 #include <anna/core/tracing/Logger.hpp>
12 #include <anna/core/tracing/TraceMethod.hpp>
13 #include <anna/core/functions.hpp>
14
15 #include <anna/dbms.mysql/Database.hpp>
16 #include <anna/dbms.mysql/Connection.hpp>
17 #include <anna/dbms.mysql/ResultCode.hpp>
18
19 using namespace std;
20 using namespace anna;
21 using namespace anna::dbms;
22
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),
26   a_mysql(NULL) {
27 }
28
29 void mysql::Connection::open()
30 throw(dbms::DatabaseException) {
31   if(a_mysql != NULL) {
32     LOGWARNING(
33       string msg = asString();
34       msg += " | Has already been established";
35       Logger::warning(msg, ANNA_FILE_LOCATION);
36     );
37     return;
38   }
39
40   if((a_mysql = mysql_init(NULL)) == NULL)
41     RuntimeException("Cannot initiate MySQL", ANNA_FILE_LOCATION);
42
43   const char* dbmsName = (a_mysqlDatabase.getType() == Database::Type::Remote) ? a_mysqlDatabase.getName().c_str() : NULL;
44
45   try {
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);
49     }
50
51     LOGINFORMATION(
52       string msg("anna::dbms::mysql::Connection::open | ");
53       msg += asString();
54       Logger::information(msg, ANNA_FILE_LOCATION);
55     );
56   } catch(DatabaseException& edbms) {
57     close();
58     throw;
59   }
60 }
61
62 void mysql::Connection::close()
63 throw() {
64   LOGINFORMATION(
65     string msg("anna::dbms::mysql::Connection::close | ");
66     msg += asString();
67     Logger::information(msg, ANNA_FILE_LOCATION);
68   );
69
70   if(a_mysql != NULL) {
71     mysql_close(a_mysql);
72     a_mysql = NULL;
73     LOGINFORMATION(
74       string msg("anna::dbms::mysql::Connection::close | ");
75       msg += asString();
76       Logger::information(msg, ANNA_FILE_LOCATION);
77     );
78   }
79 }
80
81 void mysql::Connection::do_commit()
82 throw(RuntimeException, dbms::DatabaseException) {
83   anna_dbms_mysql_check(mysql_commit(a_mysql), a_mysql);
84 }
85
86 void mysql::Connection::do_rollback()
87 throw() {
88   try {
89     anna_dbms_mysql_check(mysql_rollback(a_mysql), a_mysql);
90   } catch(Exception& ex) {
91     ex.trace();
92   }
93 }
94
95 string mysql::Connection::asString() const
96 throw() {
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 += " }";
102 }
103