Revert "Remove mysql and oracle resources for anna-ericsson project"
[anna.git] / example / dbms.mysql / xInsert / 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 Insert : public Application {
19 public:
20    Insert ();
21    ~Insert () { 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    Insert testNull;
36
37    try {
38       commandLine.initialize (argv, argc);
39       commandLine.verify ();
40
41       Logger::setLevel (Logger::Debug); 
42       Logger::initialize ("copy", new TraceWriter ("xinsert.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 Insert::Insert () : 
54    Application ("xinsert", "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 Insert::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 Insert::run () 
81    throw (RuntimeException)
82 {
83    LOGMETHOD (TraceMethod tm ("Insert", "run", ANNA_FILE_LOCATION)); 
84
85    CommandLine& cl (CommandLine::instantiate ());
86    
87    Statement* create;
88    Statement* insert;
89   
90    dbms::Integer n (true);
91    dbms::String name (15, true);
92    dbms::Float zz (true);
93    dbms::TimeStamp time (true);
94
95    dbms::ResultCode resultCode;
96
97    try {      
98       Connection* connection = a_db->createConnection ("xinsert", cl.getValue ("user"), cl.getValue ("password"));
99
100       {
101          Guard guard (connection);
102
103          try {
104             create = a_db->createStatement (
105                "create", "create table anna_db_test (xx integer, yy varchar (15), zz float, tt datetime)"
106             );
107                         
108             resultCode = connection->execute (create);
109          }
110          catch (Exception& ex) {
111             ex.trace ();
112          }
113          cout << "Creando: " << resultCode.asString () << endl << endl;
114       }
115       Guard guard (connection);     
116
117       /*
118        * Observar que la sentencia indica los parámetros tal y como se haría en Oracle.
119        */
120       insert = a_db->createStatement ("select", "insert into anna_db_test (xx, yy, zz, tt) values (:x,:y,:z, :t)");
121       insert->bindInput ("XX", n);
122       insert->bindInput ("YY", name);
123       insert->bindInput ("zz", zz);
124       insert->bindInput ("tt", time);
125
126       n = 88;
127       name = "El 88";
128       zz = 0.88;
129       time.setValue (anna::functions::second ());
130       cout << endl << " --- Insertando 1 ---" << endl;
131       resultCode = connection->execute (insert);                       
132       cout << resultCode.asString () << endl << endl;  
133       
134       n = 1010;
135       name = "El 1010";
136       zz = 0.1010;
137       time.setNull (true);
138       cout << endl << " --- Insertando 2 (date=null)---" << endl;
139       resultCode = connection->execute (insert);                       
140       cout << resultCode.asString () << endl << endl;  
141       
142       n = 89;
143       name.setNull (true);
144       zz = 0.89;
145       time.setValue ((Second)(anna::functions::second () + 100));
146       cout << endl << " --- Insertando 2 (name=null)---" << endl;
147       resultCode = connection->execute (insert);                       
148       cout << resultCode.asString () << endl << endl;  
149    }
150    catch (dbms::DatabaseException& edb) {
151       throw RuntimeException (edb); 
152    }
153 }
154
155
156