simpleRDBTable.cc
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef simpleRDBTable_cc
00026 #define simpleRDBTable_cc
00027
00028 #include <rdbxx/simpleRDBTable.h>
00029
00030 template< class Type >
00031 simpleRDBTable< Type >::~simpleRDBTable( ) {
00032
00033 if ( rdb_column ) {
00034 delete [] rdb_column;
00035 rdb_column = NULL;
00036 }
00037
00038 }
00039
00040 template< class Type >
00041 size_t simpleRDBTable< Type >::count_num_args( const char* str[] ) {
00042
00043 size_t num = 0;
00044 for ( num = 0; NULL != str[ num ]; num++ )
00045 ;
00046 return num;
00047
00048 }
00049
00050 template< class Type >
00051 simpleRDBTable< Type >::simpleRDBTable( const string& fname,
00052 const int mode,
00053 const char* header[] )
00054 throw( Exception ) :
00055 RDB( fname, static_cast<ios_base::openmode>(mode) ){
00056
00057
00058
00059
00060
00061
00062 try {
00063
00064 size_t num_cols = count_num_args( header );
00065
00066 rdb_column = new RDBColumn*[ num_cols + 1 ];
00067 for( size_t ii = 0; ii < num_cols; ii++ ) {
00068 rdb_column[ ii ]= getColumn( header[ ii ] );
00069 }
00070 rdb_column[ num_cols ] = NULL;
00071
00072 rdb_header = header;
00073
00074 } catch( Exception& e ) {
00075
00076 throw( e );
00077
00078 } catch( ... ) {
00079
00080 throw( Exception( "simpleRDBTable: unexpected exception caught" ) );
00081
00082 }
00083 }
00084
00085 template< class Type >
00086 void simpleRDBTable< Type >::getData( RDBColumn* rdbcol, string& val )
00087 throw ( Exception ) {
00088
00089 if ( NULL == rdbcol )
00090 throw Exception( "simpleRDBTable<T>::getData( RDBColumn*, string& ) :"
00091 " The first argument must not be NULL\n" );
00092
00093 rdbcol->getData( val );
00094
00095 }
00096
00097 template< class Type >
00098 void simpleRDBTable< Type >::getData( RDBColumn* rdbcol, int& val )
00099 throw ( Exception ) {
00100
00101 if ( NULL == rdbcol )
00102 throw Exception( "simpleRDBTable<T>::getData( RDBColumn*, string& ) :"
00103 " The first argument must not be NULL\n" );
00104
00105 string tmp;
00106 rdbcol->getData( tmp );
00107
00108 try {
00109
00110 long result = suplib::str2l( tmp.c_str( ), 10 );
00111 val = (int) result;
00112
00113 } catch( ... ) {
00114
00115 throw;
00116
00117 }
00118
00119 }
00120
00121 template< class Type >
00122 void simpleRDBTable< Type >::getData( RDBColumn* rdbcol, long& val )
00123 throw ( Exception ) {
00124
00125 if ( NULL == rdbcol )
00126 throw Exception( "simpleRDBTable<T>::getData( RDBColumn*, string& ) : "
00127 "The first argument must not be NULL\n" );
00128
00129 string tmp;
00130 rdbcol->getData( tmp );
00131
00132 try {
00133
00134 val = suplib::str2l( tmp.c_str( ), 10 );
00135
00136 } catch( ... ) {
00137
00138 throw;
00139
00140 }
00141
00142 }
00143
00144 template< class Type >
00145 void simpleRDBTable< Type >::getData( RDBColumn* rdbcol, double& val )
00146 throw ( Exception ) {
00147
00148 if ( NULL == rdbcol )
00149 throw Exception( "simpleRDBTable<T>::getData( RDBColumn*, string& ) : "
00150 "The first argument must not be NULL\n" );
00151
00152 string tmp;
00153 rdbcol->getData( tmp );
00154
00155 try {
00156
00157 val = suplib::str2d( tmp.c_str( ) );
00158
00159 } catch( ... ) {
00160
00161 throw;
00162
00163 }
00164
00165 }
00166
00167 template< class Type >
00168 void simpleRDBTable< Type >::print( ostream& os ) const {
00169
00170 if ( rdb_header ) {
00171
00172 for( size_t num = 0; NULL != rdb_header[ num ]; num++ ) {
00173 os << rdb_header[ num ];
00174 if ( NULL != rdb_header[ num + 1 ] )
00175 os << '\t';
00176 else
00177 os << '\n';
00178 }
00179
00180 for( size_t num = 0; NULL != rdb_column[ num ]; num++ ) {
00181 os << rdb_column[ num ]->getDef( );
00182 if ( NULL != rdb_column[ num + 1 ] )
00183 os << '\t';
00184 }
00185
00186 }
00187
00188 }
00189
00190 template< class Type >
00191 Type* simpleRDBTable< Type >::readRow( )
00192 throw( Exception ) {
00193
00194 if ( ! _isptr )
00195 throw Exception( "The rdb table is not yet open for reading\n" );
00196
00197 try {
00198
00199 if ( read( ) )
00200 return new Type( rdb_header, rdb_column );
00201 else
00202 return (Type*) NULL;
00203
00204 } catch( ... ) {
00205
00206 throw;
00207
00208 }
00209
00210 }
00211
00212 #endif