First commit
[anna.git] / source / dbms.mysql / Connection.cpp
1 // ANNA - Anna is Not 'N' Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // https://bitbucket.org/testillano/anna
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
11 //     * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 //     * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
16 // distribution.
17 //     * Neither the name of Google Inc. nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
33 // Authors: eduardo.ramos.testillano@gmail.com
34 //          cisco.tierra@gmail.com
35
36
37 #include <mysql/mysql.h>
38
39 #include <anna/core/tracing/Logger.hpp>
40 #include <anna/core/tracing/TraceMethod.hpp>
41 #include <anna/core/functions.hpp>
42
43 #include <anna/dbms.mysql/Database.hpp>
44 #include <anna/dbms.mysql/Connection.hpp>
45 #include <anna/dbms.mysql/ResultCode.hpp>
46
47 using namespace std;
48 using namespace anna;
49 using namespace anna::dbms;
50
51 mysql::Connection::Connection(Database& database, const std::string& name, const char* user, const char* password) :
52   dbms::Connection(database, name, user, password),
53   a_mysqlDatabase(database),
54   a_mysql(NULL) {
55 }
56
57 void mysql::Connection::open()
58 throw(dbms::DatabaseException) {
59   if(a_mysql != NULL) {
60     LOGWARNING(
61       string msg = asString();
62       msg += " | Has already been established";
63       Logger::warning(msg, ANNA_FILE_LOCATION);
64     );
65     return;
66   }
67
68   if((a_mysql = mysql_init(NULL)) == NULL)
69     RuntimeException("Cannot initiate MySQL", ANNA_FILE_LOCATION);
70
71   const char* dbmsName = (a_mysqlDatabase.getType() == Database::Type::Remote) ? a_mysqlDatabase.getName().c_str() : NULL;
72
73   try {
74     if(mysql_real_connect(a_mysql, a_mysqlDatabase.getHost(), a_user.c_str(), a_password.c_str(), dbmsName, 0, NULL, 0L) == NULL) {
75       ResultCode resultCode(a_mysql);
76       throw DatabaseException(resultCode, ANNA_FILE_LOCATION);
77     }
78
79     LOGINFORMATION(
80       string msg("anna::dbms::mysql::Connection::open | ");
81       msg += asString();
82       Logger::information(msg, ANNA_FILE_LOCATION);
83     );
84   } catch(DatabaseException& edbms) {
85     close();
86     throw;
87   }
88 }
89
90 void mysql::Connection::close()
91 throw() {
92   LOGINFORMATION(
93     string msg("anna::dbms::mysql::Connection::close | ");
94     msg += asString();
95     Logger::information(msg, ANNA_FILE_LOCATION);
96   );
97
98   if(a_mysql != NULL) {
99     mysql_close(a_mysql);
100     a_mysql = NULL;
101     LOGINFORMATION(
102       string msg("anna::dbms::mysql::Connection::close | ");
103       msg += asString();
104       Logger::information(msg, ANNA_FILE_LOCATION);
105     );
106   }
107 }
108
109 void mysql::Connection::do_commit()
110 throw(RuntimeException, dbms::DatabaseException) {
111   anna_dbms_mysql_check(mysql_commit(a_mysql), a_mysql);
112 }
113
114 void mysql::Connection::do_rollback()
115 throw() {
116   try {
117     anna_dbms_mysql_check(mysql_rollback(a_mysql), a_mysql);
118   } catch(Exception& ex) {
119     ex.trace();
120   }
121 }
122
123 string mysql::Connection::asString() const
124 throw() {
125   string result("dbms::mysql::Connection { ");
126   result += dbms::Connection::asString();
127   result += " | Context: ";
128   result += (a_mysql == NULL) ? "(null)" : functions::asHexString(anna_ptrnumber_cast(a_mysql));
129   return result += " }";
130 }
131