Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

RdbMySQLDumpTable.cc

Go to the documentation of this file.
00001 //--------------------------------------------------------------------------
00002 // File and Version Information:
00003 //      $Id: RdbMySQLDumpTable.cc,v 1.3 2005/04/15 17:56:07 salnikov Exp $
00004 //
00005 // Description:
00006 //      Example application for the RdbMySQL package
00007 //
00008 // Environment:
00009 //      Software developed for the BaBar Detector at the SLAC B-Factory.
00010 //
00011 // Author List:
00012 //      Andy Salnikov
00013 //
00014 // Copyright Information:
00015 //      Copyright (C) 2005 SLAC
00016 //
00017 //------------------------------------------------------------------------
00018 #include "BaBar/BaBar.hh"
00019 
00020 //-------------
00021 // C Headers --
00022 //-------------
00023 extern "C" {
00024 }
00025 
00026 //---------------
00027 // C++ Headers --
00028 //---------------
00029 #include <algorithm>
00030 #include <iostream>
00031 #include <iomanip>
00032 #include <string>
00033 #include <vector>
00034 
00035 //-------------------------------
00036 // Collaborating Class Headers --
00037 //-------------------------------
00038 #include "ErrLogger/ErrLog.hh"
00039 #include "RdbMySQL/RdbMySQLQuery.hh"
00040 #include "RdbMySQL/RdbMySQLResult.hh"
00041 #include "RdbMySQL/RdbMySQLRow.hh"
00042 #include "RdbMySQL/RdbMySQLRowIter.hh"
00043 #include "RdbMySQL/RdbMySQLConn.hh"
00044 #include "SysUtils/SysApp.hh"
00045 #include "SysUtils/SysCmdArg.hh"
00046 #include "SysUtils/SysCmdOpt.hh"
00047 
00048 //-----------------------------------------------------------------------
00049 // Local Macros, Typedefs, Structures, Unions and Forward Declarations --
00050 //-----------------------------------------------------------------------
00051 
00052 namespace {
00053 
00054   template <typename Iter>
00055   void dashes ( Iter begin, Iter end ) {
00056     cout.fill('-') ;
00057     for ( ; begin != end ; ++ begin ) {
00058       cout << "+" << setw(*begin+2) << "" ;
00059     }
00060     cout << "+\n" ;
00061     cout.fill(' ') ;
00062   }
00063 
00064 }
00065 
00066 using namespace std ;
00067 
00068 class RdbMySQLDumpTable : public SysApp {
00069 
00070 public:
00071 
00072   // Constructors
00073   RdbMySQLDumpTable( const std::string& appName, bool installErrLog = true ) ;
00074 
00075   // Destructor
00076   virtual ~RdbMySQLDumpTable() {}
00077 
00078 protected:
00079 
00080   virtual int runApp () ;
00081 
00082 private:
00083 
00084   // Friends
00085 
00086   // Data members
00087   SysCmdOpt<std::string>  _optHost ;
00088   SysCmdOpt<unsigned int> _optPort ;
00089   SysCmdArg<std::string>  _argDb ;
00090   SysCmdArg<std::string>  _argTable ;
00091 
00092 };
00093 
00094 // Constructors
00095 RdbMySQLDumpTable::RdbMySQLDumpTable( const std::string& appName, bool installErrLog )
00096   : SysApp ( appName, installErrLog )
00097   , _optHost ( 's', "host", "string", "remote host name", "" )
00098   , _optPort ( 'p', "port", "number", "remote port number", 0 )
00099   , _argDb ( "database", "database name" ) 
00100   , _argTable ( "table", "table name" ) 
00101 {
00102   addOption ( _optHost ) ;
00103   addOption ( _optPort ) ;
00104   addArgument ( _argDb ) ;
00105   addArgument ( _argTable ) ;
00106 }
00107 
00108 int
00109 RdbMySQLDumpTable::runApp ()
00110 {
00111 
00112   // make a conection object
00113   RdbMySQLConn conn( _optHost.value(), "", "", _argDb.value(), _optPort.value() ) ;
00114 
00115   // open connection
00116   if ( ! conn.open() ) {
00117     cerr << "Cannot open connection" << endl ;
00118     return 1 ;
00119   }
00120 
00121   // make a query object
00122   RdbMySQLQuery query( conn ) ;
00123   
00124   const char* q = "SELECT * FROM ?" ;
00125   std::auto_ptr<RdbMySQLResult> res ( query.executePar( q, _argTable.value() ) ) ;
00126   if ( ! res.get() ) {
00127     ErrMsg(error) << "query failed: " << conn.error() << endmsg ;
00128     return 2 ;
00129   }
00130 
00131   // result header
00132   const RdbMySQLHeader& header = res->header() ;
00133   int nf = header.size() ;
00134   if ( ! nf ) {
00135     ErrMsg(error) << "empty header" << endmsg ;
00136     return 3 ;
00137   }
00138 
00139   // calculate max field sizes
00140   std::vector<int> sizes( nf, 0 ) ;
00141   for ( int i = 0 ; i < nf ; ++ i ) {
00142     const RdbMySQLField& field = header.field(i) ;
00143     sizes[i] = strlen(field.name()) ;
00144     if ( sizes[i] < field.max_length() ) {
00145       sizes[i] = field.max_length() ;
00146     }
00147   }
00148 
00149   // print header
00150   cout.setf( ios::left, ios::adjustfield ) ;
00151   ::dashes ( sizes.begin(), sizes.end() ) ;
00152   for ( int i = 0 ; i < nf ; ++ i ) {
00153     const RdbMySQLField& field = header.field(i) ;
00154     cout << "| " << setw(sizes[i]) << field.name() << " " ;
00155   }
00156   cout << "|\n" ;
00157   ::dashes ( sizes.begin(), sizes.end() ) ;
00158 
00159   // print all rows
00160   RdbMySQLRowIter iter ( *res ) ;
00161   while ( iter.next() ) {
00162     RdbMySQLRow row = iter.row() ;
00163     for ( int i = 0 ; i < nf ; ++ i ) {
00164       const char* str = row.at(i) ;
00165       if ( ! str ) str = "NULL" ;
00166       cout << "| " << setw(sizes[i]) << str << " " ;
00167     }
00168     cout << "|\n" ;
00169   }
00170 
00171   ::dashes ( sizes.begin(), sizes.end() ) ;
00172 
00173   return 0 ;
00174 }
00175 
00176 
00177 //
00178 //  Main function
00179 //
00180 int
00181 main ( int argc, char* argv[] ) 
00182 {
00183   RdbMySQLDumpTable app ( argv[0] ) ;
00184   return app.run ( argc, argv ) ;
00185 }

Generated on Mon Dec 5 18:22:13 2005 for CDB by doxygen1.3-rc3