00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "BaBar/BaBar.hh"
00019
00020
00021
00022
00023 extern "C" {
00024 }
00025
00026
00027
00028
00029 #include <algorithm>
00030 #include <iostream>
00031 #include <iomanip>
00032 #include <string>
00033 #include <vector>
00034
00035
00036
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
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
00073 RdbMySQLDumpTable( const std::string& appName, bool installErrLog = true ) ;
00074
00075
00076 virtual ~RdbMySQLDumpTable() {}
00077
00078 protected:
00079
00080 virtual int runApp () ;
00081
00082 private:
00083
00084
00085
00086
00087 SysCmdOpt<std::string> _optHost ;
00088 SysCmdOpt<unsigned int> _optPort ;
00089 SysCmdArg<std::string> _argDb ;
00090 SysCmdArg<std::string> _argTable ;
00091
00092 };
00093
00094
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
00113 RdbMySQLConn conn( _optHost.value(), "", "", _argDb.value(), _optPort.value() ) ;
00114
00115
00116 if ( ! conn.open() ) {
00117 cerr << "Cannot open connection" << endl ;
00118 return 1 ;
00119 }
00120
00121
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
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
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
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
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
00179
00180 int
00181 main ( int argc, char* argv[] )
00182 {
00183 RdbMySQLDumpTable app ( argv[0] ) ;
00184 return app.run ( argc, argv ) ;
00185 }