1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
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 //
11 #include <anna/core/core.hpp>
12 #include <anna/dbms/dbms.hpp>
13 #include <anna/app/app.hpp>
15 #include <anna/dbms.mysql/Database.hpp>
16 #include <anna/dbms.mysql/OracleTranslator.hpp>
18 class Insert : public Application {
21 ~Insert () { delete a_db; }
24 anna::dbms::Database* a_db;
26 void initialize () throw (RuntimeException);
27 void run () throw (RuntimeException);
32 int main (int argc, const char** argv)
34 CommandLine& commandLine (CommandLine::instantiate ());
38 commandLine.initialize (argv, argc);
39 commandLine.verify ();
41 Logger::setLevel (Logger::Debug);
42 Logger::initialize ("copy", new TraceWriter ("xinsert.trace", 1024 * 1024));
46 catch (Exception& ex) {
47 cout << ex.asString () << endl;
54 Application ("xinsert", "Copiador de la tabla ad_funcionalidades", "1.0"),
57 CommandLine& cl (CommandLine::instantiate ());
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");
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.
70 void Insert::initialize ()
71 throw (RuntimeException)
73 CommandLine& ccll = CommandLine::instantiate ();
74 const char* host = ccll.exists ("host") ? ccll.getValue ("host"): NULL;
76 a_db = new anna::dbms::mysql::Database (ccll.getValue ("db"), host);
77 a_db->setStatementTranslator (dbms::mysql::OracleTranslator::instantiate ());
81 throw (RuntimeException)
83 LOGMETHOD (TraceMethod tm ("Insert", "run", ANNA_FILE_LOCATION));
85 CommandLine& cl (CommandLine::instantiate ());
90 dbms::Integer n (true);
91 dbms::String name (15, true);
92 dbms::Float zz (true);
93 dbms::TimeStamp time (true);
95 dbms::ResultCode resultCode;
98 Connection* connection = a_db->createConnection ("xinsert", cl.getValue ("user"), cl.getValue ("password"));
101 Guard guard (connection);
104 create = a_db->createStatement (
105 "create", "create table anna_db_test (xx integer, yy varchar (15), zz float, tt datetime)"
108 resultCode = connection->execute (create);
110 catch (Exception& ex) {
113 cout << "Creando: " << resultCode.asString () << endl << endl;
115 Guard guard (connection);
118 * Observar que la sentencia indica los parámetros tal y como se haría en Oracle.
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);
129 time.setValue (anna::functions::second ());
130 cout << endl << " --- Insertando 1 ---" << endl;
131 resultCode = connection->execute (insert);
132 cout << resultCode.asString () << endl << endl;
138 cout << endl << " --- Insertando 2 (date=null)---" << endl;
139 resultCode = connection->execute (insert);
140 cout << resultCode.asString () << endl << endl;
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;
150 catch (dbms::DatabaseException& edb) {
151 throw RuntimeException (edb);