1 // ANNA - Anna is Not Nothingness Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // https://bitbucket.org/testillano/anna
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
17 // * Neither the name of Google Inc. nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 // Authors: eduardo.ramos.testillano@gmail.com
34 // cisco.tierra@gmail.com
39 #include <anna/core/core.hpp>
40 #include <anna/dbms/dbms.hpp>
41 #include <anna/app/app.hpp>
43 #include <anna/dbms.mysql/Database.hpp>
44 #include <anna/dbms.mysql/OracleTranslator.hpp>
46 class Select : public Application {
49 ~Select () { delete a_db; }
52 anna::dbms::Database* a_db;
54 void initialize () throw (RuntimeException);
55 void run () throw (RuntimeException);
60 int main (int argc, const char** argv)
62 CommandLine& commandLine (CommandLine::instantiate ());
66 commandLine.initialize (argv, argc);
67 commandLine.verify ();
69 Logger::setLevel (Logger::Debug);
70 Logger::initialize ("copy", new TraceWriter ("xselect.trace", 1024 * 1024));
74 catch (Exception& ex) {
75 cout << ex.asString () << endl;
82 Application ("xselect", "Copiador de la tabla ad_funcionalidades", "1.0"),
85 CommandLine& cl (CommandLine::instantiate ());
87 cl.add ("user", CommandLine::Argument::Mandatory, "Nombre del usuario");
88 cl.add ("password", CommandLine::Argument::Mandatory, "Clave del usuario");
89 cl.add ("host", CommandLine::Argument::Optional, "Nombre de la máquina donde se ubica el MySQL");
90 cl.add ("db", CommandLine::Argument::Optional, "Nombre de la base de datos");
94 * Las sentencias SQL usadas por este programana estaba originalmente escritas para Oracle,
95 * pero no hay que cambiar cada sentencia manualmente, s�lo hay que activar el traductor
96 * correspondiente y anna.dbms.mysql lo hace autom�ticamente.
98 void Select::initialize ()
99 throw (RuntimeException)
101 CommandLine& ccll = CommandLine::instantiate ();
102 const char* host = ccll.exists ("host") ? ccll.getValue ("host"): NULL;
104 a_db = new anna::dbms::mysql::Database (ccll.getValue ("db"), host);
105 a_db->setStatementTranslator (dbms::mysql::OracleTranslator::instantiate ());
109 throw (RuntimeException)
111 LOGMETHOD (TraceMethod tm ("Select", "run", ANNA_FILE_LOCATION));
113 CommandLine& cl (CommandLine::instantiate ());
118 dbms::Integer n (true);
119 dbms::String name (15, true);
120 dbms::Float zz (true);
121 dbms::TimeStamp tt (true);
123 dbms::ResultCode resultCode;
126 Connection* connection = a_db->createConnection ("xselect", cl.getValue ("user"), cl.getValue ("password"));
128 Guard guard (connection);
130 select = a_db->createStatement ("select", "select xx,yy,zz, tt from anna_db_test");
131 select->bindOutput ("XX", n);
132 select->bindOutput ("YY", name);
133 select->bindOutput ("zz", zz);
134 select->bindOutput ("tt", tt);
136 cout << endl << " --- Leyendo ---" << endl;
137 resultCode = connection->execute (select);
139 if (resultCode.successful () == true) {
140 while (select->fetch () == true) {
141 if (n.isNull () == true)
146 cout << " | YY: " << ((name.isNull () == true) ? "<null>": name);
149 if (zz.isNull () == true)
152 cout << zz.getValue ();
154 cout << " | TT: " << ((tt.isNull () == true) ? "<null>": tt.getCStringValue ());
160 cout << resultCode.asString () << endl << endl;
162 catch (dbms::DatabaseException& edb) {
163 throw RuntimeException (edb);