Fix bug in json to xml convert library
[anna.git] / source / dbms / Sentence.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/xml/Node.hpp>
10 #include <anna/xml/Attribute.hpp>
11
12 #include <anna/dbms/Database.hpp>
13 #include <anna/dbms/Connection.hpp>
14 #include <anna/dbms/Sentence.hpp>
15 #include <anna/dbms/Statement.hpp>
16
17 using namespace std;
18 using namespace anna;
19
20 const string& dbms::Sentence::getName() const
21 throw() {
22   static string empty;
23   return (a_dbStatement == NULL) ? empty : a_dbStatement->getName();
24 }
25
26 void dbms::Sentence::initialize(dbms::Database& database)
27 throw(RuntimeException) {
28   Guard guard(this, "anna::dbms::Sentence (initialize)");
29   a_dbStatement = do_initialize(database);
30 }
31
32 //-------------------------------------------------------------------------------------
33 // Ojo!! No activamos la seccion critica en este metodo porque debera estar
34 // activa externamente ... para recoger datos multiples, etc, etc.
35 //-------------------------------------------------------------------------------------
36 dbms::ResultCode dbms::Sentence::execute(dbms::Connection& connection, dbms::Statement* statement)
37 throw(RuntimeException) {
38   using namespace anna::dbms;
39   ResultCode result;
40
41   try {
42     result = connection.execute(statement);
43
44     if(result.successful() == false) {
45       if(a_mode == Mode::SilentWhenNotFound && result.notFound() == true)
46         return result;
47
48       throw DatabaseException(result, ANNA_FILE_LOCATION);
49     }
50   } catch(DatabaseException& edb) {
51     string msg("Sentence: ");
52     msg += getName();
53     msg += " | ";
54     msg += edb.getText();
55     throw RuntimeException(msg, ANNA_FILE_LOCATION);
56   }
57
58   return result;
59 }
60
61 bool dbms::Sentence::fetch()
62 throw(RuntimeException) {
63   bool result = false;
64
65   try {
66     result = a_dbStatement->fetch();
67   } catch(dbms::DatabaseException& edb) {
68     throw RuntimeException(edb);
69   }
70
71   return result;
72 }
73
74 string dbms::Sentence::asString() const
75 /*virtual*/
76 throw() {
77   string result("dbms::Sentence { Mode: ");
78   result += (a_mode == Mode::SilentWhenNotFound) ? "SilentWhenNotFound" : "ExceptionWhenNotFound";
79   result += " | ";
80
81   if(a_dbStatement != NULL)
82     result += a_dbStatement->asString();
83   else
84     result += "dbms::Statement: <null>";
85
86   return result += " }";
87 }
88
89 /*virtual*/
90 xml::Node* dbms::Sentence::asXML(xml::Node* parent) const
91 throw() {
92   xml::Node* result = parent->createChild("dbms.Sentence");
93   result->createAttribute("Mode", (a_mode == Mode::SilentWhenNotFound) ? "SilentWhenNotFound" : "ExceptionWhenNotFound");
94
95   if(a_dbStatement)
96     a_dbStatement->asXML(result);
97
98   return result;
99 }