00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef rep_base_HPP
00012 #define rep_base_HPP
00013
00014 #include <iostream>
00015 #include <iomanip>
00016 #include "ocTypes.h"
00017 #include "ocString.h"
00018 #include "read_base.hpp"
00019 #include "cgiClass.h"
00020 #include "cgiTemplates.h"
00021
00022 using namespace std;
00023
00024
00025
00026
00027
00028
00029
00030
00031 class rep_base: public read_base
00032 {
00033 public:
00034 cgiTemplates reportParts;
00035 string head;
00036 string cachedSQL;
00037 ostream & webIO;
00038 bool opened;
00039 bool more_data;
00040 long row;
00041
00042 string topKey, endKey, rowKey, endRowKey, headingKey, dataKey;
00043
00044
00045
00046
00047 rep_base(ostream&sc):read_base(),webIO(sc),
00048 opened(false),more_data(false),row(0),
00049 topKey("top"), endKey("end"), rowKey("tr"), endRowKey("end_tr") ,headingKey("th"), dataKey("td")
00050 {
00051 }
00052
00053 virtual ~rep_base(){;}
00054
00055
00056 bool loadTemplates( string path )
00057 {
00058 bool ret = reportParts.load(path.c_str());
00059 if( ret == false )
00060 {
00061 webIO << "<!-- failed to load " << path << "-->";
00062 }
00063 return ret;
00064 }
00065
00066 ocString part( string id )
00067 {
00068 string & ret = reportParts.getParagraph(id);
00069 if( ret.length() == 0 ) webIO << "<!-- failed to find template item " << id << "-->";
00070 return ret;
00071 }
00072
00073 rep_base & copyTemplates( const cgiTemplates & in )
00074 {
00075 reportParts = in;
00076 return * this;
00077 }
00078
00079 virtual bool getData( string sql )
00080 {
00081 cachedSQL = sql;
00082 iGetData(sql);
00083 return opened;
00084 }
00085
00086 virtual bool iGetData( string sql )
00087 {
00088 opened = rs.open(sql);
00089 return opened;
00090 }
00091
00092 rep_base & emitTop( string Name )
00093 {
00094 ocString list_begin = reportParts.getParagraph(topKey);
00095 list_begin = list_begin.replace("$name$", Name.c_str() );
00096 list_begin = list_begin.replace("$title$", Name.c_str() );
00097 webIO << list_begin;
00098 }
00099 rep_base & emitEmptySet( void )
00100 {
00101 int cols = simulateHeader();
00102
00103 ocString Cols = "<td colspan='";
00104 Cols.append(cols);
00105 Cols+="' style='text-align: center' ";
00106 webIO << part(rowKey);
00107 ocString td = part(dataKey);
00108 webIO << td.replace( "$data$", "No Data" ).replace("<td",Cols);
00109 webIO << part(endRowKey);
00110 webIO << "<!-- " << cachedSQL << "-->" << endl;
00111 }
00112 int simulateHeader( void )
00113 {
00114 int cols = 0;
00115 webIO << part(rowKey);
00116 ocString chop = cachedSQL;
00117 chop.parse("select");
00118 chop = chop.parse("from");
00119 chop.parseInit();
00120 chop.parse(" as ");
00121 ocString col = chop.parse(",");
00122 while ( col.length() && !chop.endOfParse() && cols < 100 )
00123 {
00124 cols++;
00125 ocString tst = col;
00126 string colName;
00127 colName = tst.tokenParse("'");
00128 ocString th = part(headingKey);
00129 webIO << th.replace( "$data$", colName.c_str() );
00130 chop.parse(" as ");
00131 col = chop.parse(",");
00132 }
00133 webIO << part(endRowKey);
00134 return cols;
00135 }
00136 rep_base & emitHeadings( string pipedelimited = "" )
00137 {
00138 if( head.length() == 0 )
00139 {
00140 ocString headings;
00141 if( pipedelimited.length() )
00142 {
00143 headings = pipedelimited;
00144 }
00145 else if ( headings.length() )
00146 {
00147 headings.parseInit();
00148 }
00149 head = part(rowKey);
00150 ocString th = part(headingKey);
00151 while( ! headings.endOfParse() )
00152 {
00153 string heading = headings.parse("|");
00154 head += th.replace( "$data$", heading.c_str() );
00155 }
00156
00157 head += part(endRowKey);
00158 }
00159 webIO << head;
00160 return * this;
00161 }
00162
00163 rep_base & emitEnd( string extra = "" )
00164 {
00165 ocString list_end = part(endKey);
00166 webIO << list_end.replace("<!--extra-->",extra);
00167 return * this;
00168 }
00169
00170 void setColFormat( string fmt, int pos )
00171 {
00172 if( pos < rs.getFieldCount() )
00173 {
00174 rs.getField(pos).setFormatMask(fmt);
00175 }
00176 }
00177
00178 rep_base & emitHeader( void )
00179 {
00180 if( head.length() == 0 )
00181 {
00182 int fieldCount;
00183 ocString th = part(headingKey);
00184
00185 if( th.length() )
00186 {
00187 fieldCount = rs.getFieldCount();
00188 head = part(rowKey);
00189 for( int i=0; i < fieldCount; ++i )
00190 {
00191 head += getHeadCell(i, th );
00192 }
00193 head +=part(endRowKey);
00194 }
00195 }
00196 webIO << head;
00197 return * this;
00198 }
00199
00200
00201 virtual string getHeadCell( int iField, ocString & th )
00202 {
00203 return th.replace( "$data$", rs.getField(iField).getName().c_str() );
00204 }
00205
00206
00207 rep_base & emitData( void )
00208 {
00209 more_data = opened;
00210 int fieldCount;
00211 if( opened ) fieldCount = rs.getFieldCount();
00212 derived_commence_event();
00213 while( more_data )
00214 {
00215 derived_prerow_event();
00216 webIO << part(rowKey);
00217 ocString td = part(dataKey);
00218 for( int i=0; i < fieldCount; ++i )
00219 {
00220 sendField(i, td );
00221 }
00222 webIO << part(endRowKey);
00223 derived_postrow_event();
00224 more_data = rs.next();
00225 if( more_data ) row++;
00226 }
00227 derived_complete_event();
00228 return * this;
00229 }
00230
00231
00232 virtual void sendField( int iField, ocString & td )
00233 {
00234 webIO << part(dataKey).replace( "$data$", rs.getField(iField).format().c_str() );
00235 }
00236
00237
00238 virtual void derived_commence_event( void )
00239 {
00240 ;
00241 }
00242 virtual void derived_prerow_event( void )
00243 {
00244 ;
00245 }
00246 virtual void derived_postrow_event( void )
00247 {
00248 ;
00249 }
00250 virtual void derived_complete_event( void )
00251 {
00252 ;
00253 }
00254
00255 };
00256
00257
00258
00259
00260 #endif