Revert "Remove mysql and oracle resources for anna-ericsson project"
[anna.git] / source / dbms / functions.cpp
diff --git a/source/dbms/functions.cpp b/source/dbms/functions.cpp
new file mode 100644 (file)
index 0000000..97a4e88
--- /dev/null
@@ -0,0 +1,60 @@
+// 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 <anna/core/functions.hpp>
+
+#include <anna/dbms/Connection.hpp>
+#include <anna/dbms/Database.hpp>
+#include <anna/dbms/functions.hpp>
+#include <anna/dbms/String.hpp>
+#include <anna/dbms/Statement.hpp>
+
+using namespace std;
+using namespace anna;
+
+/*static*/
+void dbms::functions::verifyDataScheme(dbms::Connection& connection, const char* tableName, const char* requiredPatch, const char* columnID, const char* columnDate)
+throw(RuntimeException) {
+  dbms::Database& database = connection.getDatabase();
+  dbms::Statement* statement = NULL;
+  dbms::String id(8);
+  string sql = anna::functions::asString(
+                 "select max(%s) from %s where %s in (select max(%s) from %s)",
+                 columnID, tableName, columnDate,
+                 columnDate, tableName
+               );
+
+  try {
+    statement = database.createStatement("dbms::functions::VerifyDataScheme", sql);
+    statement->bindOutput("max_id", id);
+    dbms::ResultCode resultCode = connection.execute(statement);
+
+    if(resultCode.successful() == false)
+      throw dbms::DatabaseException(resultCode, ANNA_FILE_LOCATION);
+
+    statement->fetch();
+    const string _id(id.getValue());
+    const string _required(requiredPatch);
+
+    if(_required != _id) {
+      std::string msg("DataScheme is out of date | Current patch: ");
+      msg += id;
+      msg += " | Required patch: ";
+      msg += requiredPatch;
+      throw RuntimeException(msg, ANNA_FILE_LOCATION);
+    }
+
+    database.releaseStatement(statement);
+  } catch(dbms::DatabaseException& edb) {
+    database.releaseStatement(statement);
+    throw RuntimeException(edb);
+  } catch(RuntimeException&) {
+    database.releaseStatement(statement);
+    throw;
+  }
+}