X-Git-Url: https://git.teslayout.com/public/public/public/?a=blobdiff_plain;f=example%2Fdbms.mysql%2FxInsert%2Fmain.cpp;fp=example%2Fdbms.mysql%2FxInsert%2Fmain.cpp;h=d52b9a1ae3a161ff8e5c3aa4da6a56fc5629f43a;hb=78be86969d2f26a9084b0c4af6ce43d5fa4ed3fd;hp=0000000000000000000000000000000000000000;hpb=a3b95648bd76140ef55e0b5941d423eee6c3856f;p=anna.git diff --git a/example/dbms.mysql/xInsert/main.cpp b/example/dbms.mysql/xInsert/main.cpp new file mode 100644 index 0000000..d52b9a1 --- /dev/null +++ b/example/dbms.mysql/xInsert/main.cpp @@ -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 + +#include +#include +#include + +#include +#include + +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); + } +} + + +