Revert "Remove mysql and oracle resources for anna-ericsson project"
[anna.git] / source / dbms.mysql / Connection.cpp
diff --git a/source/dbms.mysql/Connection.cpp b/source/dbms.mysql/Connection.cpp
new file mode 100644 (file)
index 0000000..aae802c
--- /dev/null
@@ -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 <mysql/mysql.h>
+
+#include <anna/core/tracing/Logger.hpp>
+#include <anna/core/tracing/TraceMethod.hpp>
+#include <anna/core/functions.hpp>
+
+#include <anna/dbms.mysql/Database.hpp>
+#include <anna/dbms.mysql/Connection.hpp>
+#include <anna/dbms.mysql/ResultCode.hpp>
+
+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 += " }";
+}
+