Fix symlinks
[anna.git] / source / dbms / functions.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 <anna/core/functions.hpp>
10
11 #include <anna/dbms/Connection.hpp>
12 #include <anna/dbms/Database.hpp>
13 #include <anna/dbms/functions.hpp>
14 #include <anna/dbms/String.hpp>
15 #include <anna/dbms/Statement.hpp>
16
17 using namespace std;
18 using namespace anna;
19
20 /*static*/
21 void dbms::functions::verifyDataScheme(dbms::Connection& connection, const char* tableName, const char* requiredPatch, const char* columnID, const char* columnDate)
22 throw(RuntimeException) {
23   dbms::Database& database = connection.getDatabase();
24   dbms::Statement* statement = NULL;
25   dbms::String id(8);
26   string sql = anna::functions::asString(
27                  "select max(%s) from %s where %s in (select max(%s) from %s)",
28                  columnID, tableName, columnDate,
29                  columnDate, tableName
30                );
31
32   try {
33     statement = database.createStatement("dbms::functions::VerifyDataScheme", sql);
34     statement->bindOutput("max_id", id);
35     dbms::ResultCode resultCode = connection.execute(statement);
36
37     if(resultCode.successful() == false)
38       throw dbms::DatabaseException(resultCode, ANNA_FILE_LOCATION);
39
40     statement->fetch();
41     const string _id(id.getValue());
42     const string _required(requiredPatch);
43
44     if(_required != _id) {
45       std::string msg("DataScheme is out of date | Current patch: ");
46       msg += id;
47       msg += " | Required patch: ";
48       msg += requiredPatch;
49       throw RuntimeException(msg, ANNA_FILE_LOCATION);
50     }
51
52     database.releaseStatement(statement);
53   } catch(dbms::DatabaseException& edb) {
54     database.releaseStatement(statement);
55     throw RuntimeException(edb);
56   } catch(RuntimeException&) {
57     database.releaseStatement(statement);
58     throw;
59   }
60 }