Revert "Remove mysql and oracle resources for anna-ericsson project"
[anna.git] / example / dbms.mysql / xInsert / main.cpp
diff --git a/example/dbms.mysql/xInsert/main.cpp b/example/dbms.mysql/xInsert/main.cpp
new file mode 100644 (file)
index 0000000..d52b9a1
--- /dev/null
@@ -0,0 +1,156 @@
+// ANNA - Anna is Not Nothingness Anymore                                                         //
+//                                                                                                //
+// (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
+//                                                                                                //
+// See project site at http://redmine.teslayout.com/projects/anna-suite                           //
+// See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
+
+
+#include <iostream>
+
+#include <anna/core/core.hpp>
+#include <anna/dbms/dbms.hpp>
+#include <anna/app/app.hpp>
+
+#include <anna/dbms.mysql/Database.hpp>
+#include <anna/dbms.mysql/OracleTranslator.hpp>
+
+class Insert : public Application {
+public:
+   Insert ();
+   ~Insert () { delete a_db; }
+   
+private:
+   anna::dbms::Database* a_db;
+
+   void initialize () throw (RuntimeException); 
+   void run () throw (RuntimeException);      
+};
+
+using namespace std;
+
+int main (int argc, const char** argv)
+{
+   CommandLine& commandLine (CommandLine::instantiate ());
+   Insert testNull;
+
+   try {
+      commandLine.initialize (argv, argc);
+      commandLine.verify ();
+
+      Logger::setLevel (Logger::Debug); 
+      Logger::initialize ("copy", new TraceWriter ("xinsert.trace", 1024 * 1024));
+      testNull.start ();
+   }
+   catch (Exception& ex) {
+      cout << ex.asString () << endl;
+   }
+   
+   return 0;
+}
+
+Insert::Insert () : 
+   Application ("xinsert", "Copiador de la tabla ad_funcionalidades", "1.0"),
+   a_db (NULL)
+{
+   CommandLine& cl (CommandLine::instantiate ());
+      
+   cl.add ("user", CommandLine::Argument::Mandatory, "Nombre del usuario");
+   cl.add ("password", CommandLine::Argument::Mandatory, "Clave del usuario");
+   cl.add ("host", CommandLine::Argument::Optional, "Nombre de la maquina donde se ubica el MySQL");
+   cl.add ("db", CommandLine::Argument::Optional, "Nombre de la base de datos");
+}
+
+/*
+ * Las sentencias SQL usadas por este programana estaba originalmente escritas para Oracle,
+ * pero no hay que cambiar cada sentencia manualmente, s�lo hay que activar el traductor
+ * correspondiente y anna.dbms.mysql lo hace autom�ticamente.
+ */
+void Insert::initialize ()
+   throw (RuntimeException)
+{
+   CommandLine& ccll = CommandLine::instantiate ();
+   const char* host = ccll.exists ("host") ? ccll.getValue ("host"): NULL;
+
+   a_db = new anna::dbms::mysql::Database (ccll.getValue ("db"), host);
+   a_db->setStatementTranslator (dbms::mysql::OracleTranslator::instantiate ());
+}
+
+void Insert::run () 
+   throw (RuntimeException)
+{
+   LOGMETHOD (TraceMethod tm ("Insert", "run", ANNA_FILE_LOCATION)); 
+
+   CommandLine& cl (CommandLine::instantiate ());
+   
+   Statement* create;
+   Statement* insert;
+  
+   dbms::Integer n (true);
+   dbms::String name (15, true);
+   dbms::Float zz (true);
+   dbms::TimeStamp time (true);
+
+   dbms::ResultCode resultCode;
+
+   try {      
+      Connection* connection = a_db->createConnection ("xinsert", cl.getValue ("user"), cl.getValue ("password"));
+
+      {
+         Guard guard (connection);
+
+         try {
+            create = a_db->createStatement (
+               "create", "create table anna_db_test (xx integer, yy varchar (15), zz float, tt datetime)"
+            );
+                        
+            resultCode = connection->execute (create);
+         }
+         catch (Exception& ex) {
+            ex.trace ();
+         }
+         cout << "Creando: " << resultCode.asString () << endl << endl;
+      }
+      Guard guard (connection);     
+
+      /*
+       * Observar que la sentencia indica los parámetros tal y como se haría en Oracle.
+       */
+      insert = a_db->createStatement ("select", "insert into anna_db_test (xx, yy, zz, tt) values (:x,:y,:z, :t)");
+      insert->bindInput ("XX", n);
+      insert->bindInput ("YY", name);
+      insert->bindInput ("zz", zz);
+      insert->bindInput ("tt", time);
+
+      n = 88;
+      name = "El 88";
+      zz = 0.88;
+      time.setValue (anna::functions::second ());
+      cout << endl << " --- Insertando 1 ---" << endl;
+      resultCode = connection->execute (insert);                       
+      cout << resultCode.asString () << endl << endl;  
+      
+      n = 1010;
+      name = "El 1010";
+      zz = 0.1010;
+      time.setNull (true);
+      cout << endl << " --- Insertando 2 (date=null)---" << endl;
+      resultCode = connection->execute (insert);                       
+      cout << resultCode.asString () << endl << endl;  
+      
+      n = 89;
+      name.setNull (true);
+      zz = 0.89;
+      time.setValue ((Second)(anna::functions::second () + 100));
+      cout << endl << " --- Insertando 2 (name=null)---" << endl;
+      resultCode = connection->execute (insert);                       
+      cout << resultCode.asString () << endl << endl;  
+   }
+   catch (dbms::DatabaseException& edb) {
+      throw RuntimeException (edb); 
+   }
+}
+
+
+