1 // ANNA - Anna is Not Nothingness Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // http://redmine.teslayout.com/projects/anna-suite
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
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
17 // * Neither the name of the copyright holder 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.
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.
33 // Authors: eduardo.ramos.testillano@gmail.com
34 // cisco.tierra@gmail.com
37 #include <mysql/mysql.h>
39 #include <anna/core/tracing/Logger.hpp>
40 #include <anna/core/tracing/TraceMethod.hpp>
41 #include <anna/core/functions.hpp>
43 #include <anna/dbms.mysql/Database.hpp>
44 #include <anna/dbms.mysql/Connection.hpp>
45 #include <anna/dbms.mysql/ResultCode.hpp>
49 using namespace anna::dbms;
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),
57 void mysql::Connection::open()
58 throw(dbms::DatabaseException) {
61 string msg = asString();
62 msg += " | Has already been established";
63 Logger::warning(msg, ANNA_FILE_LOCATION);
68 if((a_mysql = mysql_init(NULL)) == NULL)
69 RuntimeException("Cannot initiate MySQL", ANNA_FILE_LOCATION);
71 const char* dbmsName = (a_mysqlDatabase.getType() == Database::Type::Remote) ? a_mysqlDatabase.getName().c_str() : NULL;
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);
80 string msg("anna::dbms::mysql::Connection::open | ");
82 Logger::information(msg, ANNA_FILE_LOCATION);
84 } catch(DatabaseException& edbms) {
90 void mysql::Connection::close()
93 string msg("anna::dbms::mysql::Connection::close | ");
95 Logger::information(msg, ANNA_FILE_LOCATION);
102 string msg("anna::dbms::mysql::Connection::close | ");
104 Logger::information(msg, ANNA_FILE_LOCATION);
109 void mysql::Connection::do_commit()
110 throw(RuntimeException, dbms::DatabaseException) {
111 anna_dbms_mysql_check(mysql_commit(a_mysql), a_mysql);
114 void mysql::Connection::do_rollback()
117 anna_dbms_mysql_check(mysql_rollback(a_mysql), a_mysql);
118 } catch(Exception& ex) {
123 string mysql::Connection::asString() const
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 += " }";