Main Page   Class Hierarchy   File List  

pgTypeOids.h

00001 #ifndef PGTYPE_OIDS_H
00002 #define PGTYPE_OIDS_H
00003 #include "fmtTime.h"
00004 #include <ctype.h>
00005 /*
00006 Lost one day struggling with the BINARY CURSOR and gave up:
00007 - After reverse engineering postgres source (there is NO documentation on this!):
00008    For all types it's pretty clear what the BE gives back,
00009    BUT with the Timestamp value (double) it really wasn't clear what the value means.
00010 Postgres really should provide FE functions to turn the Timestamp into a tm struct.
00011 It should also provide an FE function to unpack the packed values of the
00012 Numeric and Decimal (synonomous) types.
00013 I have decided to give in and use the character return data, only to convert it to the native type.
00014 The major issue for me with this is that I KNOW that precision is going to be lost on the
00015 floating point and double values.  I have already seen this in the return string
00016 representation of the data.
00017 Also there is the performance issue of formatting to strings, converting to a native type, then
00018 re-formatting again for a web page (or XML stream.)
00019 
00020 David McCombs 12/14/2001
00021 NOTE!!!!:  NEED TO ADD TEXT FIELD TYPE HANDLER! DKMc 10/20/2002
00022 DONE! , Also implemented longlongField use. DKMc 7/30/03
00023 
00024 */
00025 /*
00026 For reference - this is the openFields enumeration
00027 enum fieldTypes
00028 {
00029   shortType, longType,  floatType, doubleType,
00030   stringType, currencyType, dateTimeType, boolType
00031 };
00032 
00033 */
00034 enum pgTypeOids
00035 {
00036   pgBooleanOid       =16, //BOOLOID        - probably char or short loot at size
00037   pgLongOid          =20, //INT8OID        -  long long
00038   pgShortOid         =21, //INT2OID        -  short
00039   pgIntegerOid       =23, //INT4OID        -  int
00040   pgTextOid          =25,  // TEXT               
00041   pgFloatOid         =700, //FLOAT4OID     - float
00042   pgDoubleOid        =701, //FLOAT8OID     - double
00043   pgCharOid          =1042, //BPCHAROID    - char *
00044   pgCharArrayOid     =1042, //BPCHAROID    - char *
00045   pgVarCharArrayOid  =1043, //VARCHAROID   - char *
00046   pgDateOid          =1082, //DATEOID      - Timestamp
00047   pgTimeOid          =1083, //TIMEOID      - Timestamp
00048   pgTimestampOid     =1184, //TIMESTAMPOID - Timestamp
00049   pgDecimalOid       =1700, //NUMERICOID   - packed decimal
00050   pgNumericOid       =1700 //NUMERICOID    - packed decimal
00051 };
00052 #define iDateFMT "%Y-%m-%d"
00053 #define iTimeFMT "%H:%M:%S"
00054 #define iDateTimeFMT "%Y-%m-%d %H:%M:%S"
00055 #define oDateFMT "%m-%d-%Y"
00056 #define oTimeFMT "%H:%M:%S"
00057 #define oDateTimeFMT "%m-%d-%Y %H:%M:%S"
00058 
00059 #ifdef JUST_PG_FIELD_INTERFACES
00060 basicField * createField( pgTypeOids oid, bool isNull, string value, string  name, bool isClient=false );
00061 basicField & setField( basicField & rBF, pgTypeOids oid, bool isNull, string value, bool isClient=false );
00062 #else
00063 basicField * createField( pgTypeOids oid, bool isNull, string value, string  name, bool isClient=false )
00064 {
00065   basicField * pBF = NULL;
00066   tm t;
00067   char chVal = '\0';
00068   bool bVal = false;
00069   short sVal = 0;
00070   long lVal = 0l;
00071   long long llVal = 0l;
00072   float fVal = 0.0;
00073   double dVal = 0.0;
00074   currency cVal;
00075   cVal.amount = 0.0;
00076   currentTime(&t);
00077 
00078   switch( oid )
00079   {
00080     case pgBooleanOid:
00081       bVal = false;
00082       chVal = toupper( value[0] );
00083       // T,Y,1 is true  F,N,0 is false
00084       if ( chVal == 'T' || chVal == 'Y' || chVal == '1' ) bVal = true;
00085       pBF = new boolField( bVal, isNull, true, name, "" );
00086       break;
00087 
00088     case pgShortOid:
00089       sVal = atoi( value.c_str());
00090       pBF = new shortField( sVal, isNull, true, name, "" );
00091       break;
00092 
00093     case pgIntegerOid:
00094       lVal = atol( value.c_str());
00095       pBF = new longField( lVal, isNull, true, name, "" );
00096       break;
00097     case pgLongOid:
00098       llVal = atoll( value.c_str());
00099       pBF = new longlongField( llVal, isNull, true, name, "" );
00100       break;
00101 
00102     case pgFloatOid:
00103       fVal = (float) atof( value.c_str());
00104       pBF = new floatField( fVal, isNull, true, name, "" );
00105       break;
00106 
00107     case pgDoubleOid:
00108       dVal = atof( value.c_str());
00109       pBF = new doubleField( dVal, isNull, true, name, "" );
00110       break;
00111 
00112     // case pgCharOid:
00113     case pgCharArrayOid:
00114     case pgVarCharArrayOid:
00115     case pgTextOid:
00116       pBF = new stringField( value, isNull, true, name, "" );
00117       break;
00118 
00119     case pgDateOid:
00120       if( value.length() == 0 )  isNull = true;
00121       else parseTime(value.c_str(), isClient?oDateFMT:iDateFMT, &t);
00122       pBF = new dateTimeField( t, isNull, true, name, "" );
00123       pBF->setFormatMask(string(oDateFMT));
00124       pBF->setSvrFormatMask(string(iDateFMT));
00125       break;
00126 
00127     case pgTimeOid:
00128       if( value.length() == 0 )  isNull = true;
00129       else parseTime(value.c_str(), isClient?oTimeFMT:iTimeFMT, &t);
00130       pBF = new dateTimeField( t, isNull, true, name, "" );
00131       pBF->setFormatMask(oTimeFMT);
00132       pBF->setSvrFormatMask(string(iTimeFMT));
00133       break;
00134 
00135     case pgTimestampOid:
00136       if( value.length() == 0 )  isNull = true;
00137       else parseTime(value.c_str(), isClient?oDateTimeFMT:iDateTimeFMT, &t);
00138       pBF = new dateTimeField( t, isNull, true, name, "" );
00139       pBF->setFormatMask(oDateTimeFMT);
00140       pBF->setSvrFormatMask(string(iDateTimeFMT));
00141       break;
00142 
00143     //case pgDecimalOid:
00144     case pgNumericOid:
00145       cVal.amount = atof( value.c_str());
00146       pBF = new currencyField( cVal, isNull, true, name, "" );
00147       break;
00148 
00149     default:
00150       // cerr << " Do not know about this type: " << oid << endl;
00151     break;
00152 
00153   }
00154   return pBF;
00155 };
00156 basicField & setField( basicField & rBF, pgTypeOids oid, bool isNull, string value, bool isClient=false )
00157 {
00158   tm t;
00159   bool bVal;
00160   char chVal;
00161   short sVal;
00162   long lVal;
00163   long long llVal = 0l;
00164   float fVal;
00165   double dVal;
00166   currency cVal;
00167 
00168   switch( oid )
00169   {
00170     case pgBooleanOid:
00171     {
00172       bVal = false;
00173       chVal = toupper( value[0] );
00174       // T,Y,1 is true  F,N,0 is false
00175       if ( chVal == 'T' || chVal == 'Y' || chVal == '1' ) bVal = true;
00176       boolField & rboolF = dynamic_cast<boolField&>(rBF);
00177       rboolF.set(bVal);
00178       rboolF.setNull(isNull);
00179       break;
00180     }
00181     case pgShortOid:
00182     {
00183       sVal = atoi( value.c_str());
00184       shortField & rshortF = dynamic_cast<shortField&>(rBF);
00185       rshortF.set(sVal);
00186       rshortF.setNull(isNull);
00187       break;
00188     }
00189     case pgIntegerOid:
00190     {
00191       lVal = atol( value.c_str());
00192       longField & rlongF = dynamic_cast<longField&>(rBF);
00193       rlongF.set(lVal);
00194       rlongF.setNull(isNull);
00195       break;
00196     }
00197     case pgLongOid:
00198     {
00199       llVal = atoll( value.c_str());
00200       longlongField & rlongF = dynamic_cast<longlongField&>(rBF);
00201       rlongF.set(llVal);
00202       rlongF.setNull(isNull);
00203       break;
00204     }
00205     case pgFloatOid:
00206     {
00207       fVal = (float) atof( value.c_str());
00208       floatField & rfloatF = dynamic_cast<floatField&>(rBF);
00209       rfloatF.set(fVal);
00210       rfloatF.setNull(isNull);
00211       break;
00212     }
00213     case pgDoubleOid:
00214     {
00215       dVal = atof( value.c_str());
00216       doubleField & rdoubleF = dynamic_cast<doubleField&>(rBF);
00217       rdoubleF.set(dVal);
00218       rdoubleF.setNull(isNull);
00219       break;
00220     }
00221     // case pgCharOid:
00222     case pgTextOid:
00223     case pgCharArrayOid:
00224     case pgVarCharArrayOid:
00225     {
00226       stringField & rstringF = dynamic_cast<stringField&>(rBF);
00227       rstringF.set( value);
00228       rstringF.setNull(isNull);
00229       break;
00230     }
00231     case pgDateOid:
00232     {
00233       parseTime(value.c_str(), isClient?oDateFMT:iDateFMT, &t);
00234       dateTimeField & rdateF = dynamic_cast<dateTimeField&>(rBF);
00235       rdateF.set(t);
00236       rdateF.setNull(isNull);
00237       break;
00238     }
00239     case pgTimeOid:
00240     {
00241       parseTime(value.c_str(), isClient?oTimeFMT:iTimeFMT, &t);
00242       dateTimeField & rtimeF = dynamic_cast<dateTimeField&>(rBF);
00243       rtimeF.set(t);
00244       rtimeF.setNull(isNull);
00245       break;
00246     }
00247     case pgTimestampOid:
00248     {
00249       parseTime(value.c_str(), isClient?oDateTimeFMT:iDateTimeFMT, &t);
00250       dateTimeField & rdatetimeBF = dynamic_cast<dateTimeField&>(rBF);
00251       rdatetimeBF.set(t);
00252       rdatetimeBF.setNull(isNull);
00253       break;
00254     }
00255     //case pgDecimalOid:
00256     case pgNumericOid:
00257     {
00258       cVal.amount = atof( value.c_str());
00259       currencyField & currencyF = dynamic_cast<currencyField&>(rBF);
00260       currencyF.set(cVal);
00261       currencyF.setNull(isNull);
00262       break;
00263     }
00264     default:
00265     break;
00266 
00267   }
00268   return rBF;
00269 };
00270 #endif
00271 #endif

Generated on Tue Jan 20 09:06:56 2004 for OpenTools by doxygen1.2.18