Remove warnings
[anna.git] / example / dbms.mysql / xSelect / main.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 <iostream>
10
11 #include <anna/core/core.hpp>
12 #include <anna/dbms/dbms.hpp>
13 #include <anna/app/app.hpp>
14
15 #include <anna/dbms.mysql/Database.hpp>
16 #include <anna/dbms.mysql/OracleTranslator.hpp>
17
18 class Select : public Application {
19 public:
20    Select ();
21    ~Select () { delete a_db; }
22    
23 private:
24    anna::dbms::Database* a_db;
25
26    void initialize () throw (RuntimeException); 
27    void run () throw (RuntimeException);      
28 };
29
30 using namespace std;
31
32 int main (int argc, const char** argv)
33 {
34    CommandLine& commandLine (CommandLine::instantiate ());
35    Select testNull;
36
37    try {
38       commandLine.initialize (argv, argc);
39       commandLine.verify ();
40
41       Logger::setLevel (Logger::Debug); 
42       Logger::initialize ("copy", new TraceWriter ("xselect.trace", 1024 * 1024));
43  
44       testNull.start ();
45    }
46    catch (Exception& ex) {
47       cout << ex.asString () << endl;
48    }
49    
50    return 0;
51 }
52
53 Select::Select () : 
54    Application ("xselect", "Copiador de la tabla ad_funcionalidades", "1.0"),
55    a_db (NULL)
56 {
57    CommandLine& cl (CommandLine::instantiate ());
58       
59    cl.add ("user", CommandLine::Argument::Mandatory, "Nombre del usuario");
60    cl.add ("password", CommandLine::Argument::Mandatory, "Clave del usuario");
61    cl.add ("host", CommandLine::Argument::Optional, "Nombre de la maquina donde se ubica el MySQL");
62    cl.add ("db", CommandLine::Argument::Optional, "Nombre de la base de datos");
63 }
64
65 /*
66  * Las sentencias SQL usadas por este programana estaba originalmente escritas para Oracle,
67  * pero no hay que cambiar cada sentencia manualmente, s�lo hay que activar el traductor
68  * correspondiente y anna.dbms.mysql lo hace autom�ticamente.
69  */
70 void Select::initialize ()
71    throw (RuntimeException)
72 {
73    CommandLine& ccll = CommandLine::instantiate ();
74    const char* host = ccll.exists ("host") ? ccll.getValue ("host"): NULL;
75
76    a_db = new anna::dbms::mysql::Database (ccll.getValue ("db"), host);
77    a_db->setStatementTranslator (dbms::mysql::OracleTranslator::instantiate ());
78 }
79
80 void Select::run () 
81    throw (RuntimeException)
82 {
83    LOGMETHOD (TraceMethod tm ("Select", "run", ANNA_FILE_LOCATION)); 
84
85    CommandLine& cl (CommandLine::instantiate ());
86    
87    Statement* select;
88   
89    dbms::Integer n (true);
90    dbms::String name (15, true);
91    dbms::Float zz (true);
92    dbms::TimeStamp tt (true);
93
94    dbms::ResultCode resultCode;
95
96    try {      
97       Connection* connection = a_db->createConnection ("xselect", cl.getValue ("user"), cl.getValue ("password"));
98
99       Guard guard (connection);     
100
101       select = a_db->createStatement ("select", "select xx,yy,zz, tt from anna_db_test");
102       select->bindOutput ("XX", n);
103       select->bindOutput ("YY", name);
104       select->bindOutput ("zz", zz);
105       select->bindOutput ("tt", tt);
106
107       cout << endl << " --- Leyendo ---" << endl;
108       resultCode = connection->execute (select);
109                         
110       if (resultCode.successful () == true) {
111          while (select->fetch () == true) {
112             if (n.isNull () == true)
113                cout << "<null>";
114             else
115                cout << n;
116
117             cout << " | YY: " << ((name.isNull () == true) ? "<null>": name);
118
119             cout << " | ZZ: ";
120             if (zz.isNull () == true)
121                cout << "<null>";
122             else
123                cout << zz.getValue ();
124
125             cout << " | TT: " << ((tt.isNull () == true) ? "<null>": tt.getCStringValue ());
126
127             cout << endl;
128          }         
129       }
130       else
131          cout << resultCode.asString () << endl << endl;  
132          }
133    catch (dbms::DatabaseException& edb) {
134       throw RuntimeException (edb); 
135    }
136 }
137
138
139