src/odbc/odbcCommon.cxx

Go to the documentation of this file.
00001 #include "odbcCommon.h"
00002 
00003 SQLRETURN SQLAllocHandle( SQLSMALLINT hdlType, 
00004                           SQLHANDLE inHdl, 
00005                           SQLHANDLE *outHdl )
00006 {
00007     switch( hdlType )
00008     {
00009         case SQL_HANDLE_ENV: 
00010                 return CSqlOdbcEnv::SQLAllocHandle( inHdl, outHdl );
00011         case SQL_HANDLE_DBC: 
00012                 return CSqlOdbcDbc::SQLAllocHandle( inHdl, outHdl );
00013         case SQL_HANDLE_STMT: 
00014                 return CSqlOdbcStmt::SQLAllocHandle( inHdl, outHdl );
00015     }
00016 
00017     globalError.set( ERROR_OPTRANGE );
00018     globalError.printStr( SQL_OV_ODBC3 );
00019     return( SQL_ERROR );
00020 }
00021 
00022 SQLRETURN SQLFreeHandle(
00023     SQLSMALLINT hdlType,
00024     SQLHANDLE   inHdl)
00025 {
00026     switch( hdlType )
00027     {
00028         case SQL_HANDLE_ENV: return CSqlOdbcEnv::SQLFreeHandle( inHdl );
00029         case SQL_HANDLE_DBC: return CSqlOdbcDbc::SQLFreeHandle( inHdl );
00030         case SQL_HANDLE_STMT: return CSqlOdbcStmt::SQLFreeHandle( inHdl );
00031     }
00032 
00033     globalError.set( ERROR_OPTRANGE );
00034     globalError.printStr( SQL_OV_ODBC3 );
00035     return( SQL_ERROR );
00036 }
00037 
00038 SQLRETURN SQLEndTran(
00039     SQLSMALLINT hdlType, 
00040     SQLHANDLE hdl,
00041     SQLSMALLINT compType)
00042 {
00043     if( isValidHandle( hdl, hdlType ) != SQL_SUCCESS )
00044         return( SQL_INVALID_HANDLE );
00045 
00046     switch( hdlType )
00047     {
00048         case SQL_HANDLE_ENV:
00049                 return ((CSqlOdbcEnv*) hdl)->SQLEndTran( compType );
00050         case SQL_HANDLE_DBC:
00051                 return ((CSqlOdbcDbc*) hdl)->SQLEndTran( compType );
00052     }
00053 
00054     globalError.set( ERROR_OPTRANGE );
00055     globalError.printStr( SQL_OV_ODBC3 );
00056     return( SQL_ERROR );
00057 }
00058 
00059 SQLRETURN isValidHandle( SQLHANDLE hdl,
00060     SQLSMALLINT hdlType)
00061 {
00062     if( hdlType == SQL_HANDLE_ENV || hdlType == SQL_HANDLE_DBC || hdlType == SQL_HANDLE_STMT )
00063 
00064         if( hdl != 0 )
00065             if( ((CSqlOdbcEnv*) hdl)->handleType_ == hdlType )
00066                 return( SQL_SUCCESS );
00067 
00068     globalError.set( ERROR_MEM_MGMNT );
00069     globalError.printStr( SQL_OV_ODBC3 );
00070     return( SQL_INVALID_HANDLE );
00071 }
00072 
00073 SQLRETURN isValidCType( SQLSMALLINT type )
00074 {
00075     switch( type )
00076     {
00077         case SQL_C_CHAR:
00078         case SQL_C_SLONG:
00079         case SQL_C_ULONG:
00080         case SQL_C_LONG:
00081         case SQL_C_FLOAT:
00082         case SQL_C_DOUBLE:
00083         case SQL_C_BIT:
00084         case SQL_C_SSHORT:
00085         case SQL_C_USHORT:
00086         case SQL_C_SHORT:
00087         case SQL_C_STINYINT:
00088         case SQL_C_UTINYINT:
00089         case SQL_C_TINYINT:
00090         case SQL_C_SBIGINT:
00091         case SQL_C_UBIGINT:
00092         case SQL_C_NUMERIC:
00093         case SQL_C_DATE:
00094         case SQL_C_TIME:
00095         case SQL_C_TIMESTAMP:
00096         case SQL_C_TYPE_DATE:
00097         case SQL_C_TYPE_TIME:
00098         case SQL_C_TYPE_TIMESTAMP:
00099         case SQL_C_BINARY: break;
00100         default: return( SQL_ERROR );
00101     }
00102     return( SQL_SUCCESS );
00103 }
00104 
00105 SQLRETURN isValidSQLType( SQLSMALLINT type )
00106 {
00107     switch( type )
00108     {
00109         case SQL_CHAR:
00110         case SQL_VARCHAR:
00111         case SQL_BIT:
00112         case SQL_SMALLINT:
00113         case SQL_INTEGER:
00114         case SQL_REAL:
00115         case SQL_DOUBLE:
00116         case SQL_FLOAT:
00117         case SQL_TINYINT:
00118         case SQL_BIGINT:
00119         case SQL_DECIMAL:
00120         case SQL_NUMERIC:
00121         case SQL_DATE:
00122         case SQL_TIME:
00123         case SQL_TIMESTAMP:
00124         case SQL_TYPE_DATE:
00125         case SQL_TYPE_TIME:
00126         case SQL_TYPE_TIMESTAMP:
00127         case SQL_BINARY:
00128         case SQL_VARBINARY: break;
00129         default: return( SQL_ERROR );
00130     }
00131     return( SQL_SUCCESS );
00132 }
00133 
00134 DataType getCSqlType( SQLSMALLINT type )
00135 {
00136     switch( type )
00137     {
00138         case SQL_C_CHAR:
00139             return typeString;
00140         case SQL_C_LONG:
00141         case SQL_C_SLONG:
00142         case SQL_C_ULONG:
00143             return typeLong;
00144         case SQL_C_FLOAT:
00145             return typeFloat;
00146         case SQL_C_DOUBLE:
00147             return typeDouble;
00148         case SQL_C_BIT:
00149             return typeUnknown;
00150         case SQL_C_SHORT:
00151         case SQL_C_SSHORT:
00152         case SQL_C_USHORT:
00153             return typeShort;
00154         case SQL_C_TINYINT:
00155         case SQL_C_STINYINT:
00156         case SQL_C_UTINYINT:
00157             return typeByteInt;
00158         case SQL_C_SBIGINT:
00159         case SQL_C_UBIGINT:
00160             return typeLongLong;
00161         case SQL_C_NUMERIC:
00162             return typeDecimal;
00163         case SQL_C_DATE:
00164         case SQL_C_TYPE_DATE:
00165             return typeDate;
00166         case SQL_C_TIME:
00167         case SQL_C_TYPE_TIME:
00168             return typeTime;
00169         case SQL_C_TIMESTAMP:
00170         case SQL_C_TYPE_TIMESTAMP: 
00171             return typeTimeStamp;
00172             break;
00173         case SQL_C_BINARY:
00174             return typeBinary;
00175         default: return( typeUnknown);
00176     }
00177 }
00178 
00179 SQLSMALLINT  getSQLType( DataType type )
00180 {
00181     switch(type)
00182     {
00183         case typeShort:
00184             return SQL_SMALLINT;
00185         case typeInt:
00186         case typeLong:
00187             return SQL_INTEGER;
00188         case typeLongLong:
00189             return SQL_BIGINT;
00190         case typeByteInt:
00191             return SQL_TINYINT;
00192 
00193         case typeFloat:
00194             return SQL_FLOAT;
00195         case typeDouble:
00196             return SQL_DOUBLE;
00197         case typeDecimal:
00198             return SQL_NUMERIC;
00199 
00200         case typeDate:
00201             return SQL_DATE;
00202         case typeTime:
00203             return SQL_TIME;
00204         case typeTimeStamp:
00205             return SQL_TIMESTAMP;
00206 
00207         case typeString:
00208             return SQL_CHAR;
00209         case typeBinary:
00210             return SQL_BINARY;
00211 
00212         // NOT SUPPORTED
00213         //case csqlSqlTlongDouble: might be equal typeDecimal
00214         //    return SQL_REAL;
00215         //case csqlSqlTvarString:
00216         //    return SQL_VARCHAR;
00217         //case csqlSqlTvarBinary:
00218         //    return SQL_BINARY;
00219         //case typeBit: 
00220         //    return SQL_BIT;
00221         default:
00222             return SQL_ERROR;
00223             break;
00224     }
00225 }
00226 
00227 SQLCHAR *getSQLTypeName( SQLSMALLINT type )
00228 {
00229     switch( type )
00230     {
00231         case SQL_CHAR:
00232             return (SQLCHAR *)"CHAR";
00233         case SQL_VARCHAR:
00234             return (SQLCHAR *)"VARCHAR";
00235         case SQL_BIT:
00236             return (SQLCHAR *)"BIT";
00237         case SQL_SMALLINT:
00238             return (SQLCHAR *)"SMALLINT";
00239         case SQL_INTEGER:
00240             return (SQLCHAR *)"INTEGER";
00241         case SQL_REAL:
00242             return (SQLCHAR *)"REAL";
00243         case SQL_DECIMAL:
00244         case SQL_NUMERIC:
00245             return (SQLCHAR *)"NUMERIC";
00246         case SQL_DOUBLE:
00247             return (SQLCHAR *)"DOUBLE";
00248         case SQL_FLOAT:
00249             return (SQLCHAR *)"FLOAT";
00250         case SQL_TINYINT:
00251             return (SQLCHAR *)"TINYINT";
00252         case SQL_BIGINT:
00253             return (SQLCHAR *)"BIGINT";
00254         case SQL_DATE:
00255         case SQL_TYPE_DATE:
00256             return (SQLCHAR *)"DATE";
00257         case SQL_TIME:
00258         case SQL_TYPE_TIME:
00259             return (SQLCHAR *)"TIME";
00260         case SQL_TIMESTAMP:
00261         case SQL_TYPE_TIMESTAMP: 
00262             return (SQLCHAR *)"TIMESTAMP";
00263             case SQL_BINARY:
00264             return (SQLCHAR *)"BINARY";
00265         default:
00266             break;
00267     }
00268 }
00269 
00270 void getInputBuffer(void **buffer,SQLSMALLINT type,SQLUINTEGER length)
00271 {
00272     switch(type)
00273     {
00274         case typeShort:
00275             *buffer = new short();
00276             break;
00277         case typeInt:
00278             *buffer= new int();
00279             break;
00280         case typeLong:
00281             *buffer= new long();
00282             break;
00283         case typeLongLong:
00284             *buffer = new long long();
00285             break;
00286         case typeByteInt:
00287             *buffer = new signed char();
00288             break;
00289 /*        case csqlSqlTbit:
00290             *buffer = new unsigned char ();
00291             break;*/
00292 
00293         case typeFloat:
00294             *buffer = new float();
00295             break;
00296         case typeDouble:
00297             *buffer = new double();
00298             break;
00299         case typeDecimal:
00300 //        case csqlSqlTlongDouble:
00301 //        case csqlSqlTnumeric:
00302             *buffer = new long double ();
00303             break;
00304 
00305         case typeString:
00306 //        case csqlSqlTvarString:
00307             *buffer = new char [length+1];
00308             break;
00309         case typeDate:
00310             *buffer = new Date();
00311             break;
00312         case typeTime:
00313             *buffer = new Time();
00314             break;
00315         case typeTimeStamp:
00316             *buffer = new TimeStamp();
00317             break;
00318         case typeBinary:
00319 //        case csqlSqlTvarBinary:                        
00320             *buffer = new unsigned char [length+1];
00321             break;
00322     }
00323 }
00324 
00325 // CSQL TODO - Need to review and finalize.
00326 //Copies data from the application onto the destination 
00327 //Called from SQLExecute
00328 //Destination here can be directly the database bound buffer or 
00329 //it may be ird
00330 void copyFromOdbc(AbsSqlStatement *stmt, int paramNo, SQLUINTEGER destLen,void *odbcData,SQLUINTEGER odbcLen,SQLSMALLINT type)
00331 {
00332     //No point in continuing further
00333     if(odbcData == NULL)
00334         return;
00335     switch( type )
00336     {
00337         case typeShort:
00338             //*(short*)destData = *(short*)odbcData;
00339             stmt->setShortParam(paramNo, *(short*)odbcData);
00340             break;
00341         case typeInt:
00342             //*(int*)destData = *(int*)odbcData;
00343             stmt->setIntParam(paramNo, *(int*)odbcData);
00344             break;
00345         case typeLong:
00346             //*(long*)destData = *(long*)odbcData;
00347             stmt->setLongParam(paramNo, *(long*)odbcData);
00348             break;
00349         case typeLongLong:
00350             //*(long long*)destData = *(long long*)odbcData;
00351             stmt->setLongLongParam(paramNo, *(long long *)odbcData);
00352             break;
00353         case typeByteInt: {
00354             //*(char*)destData = *(char*)odbcData;
00355             ByteInt bt(*(char*)odbcData);
00356              stmt->setByteIntParam(paramNo, bt);
00357             break;
00358         }
00359 /*        case csqlSqlTbit:
00360             *(unsigned char*)destData = *(unsigned char*)odbcData;
00361             break;*/
00362 
00363         case typeFloat:
00364             //*(float*)destData = *(float*)odbcData;
00365             stmt->setFloatParam(paramNo, *(float*)odbcData);
00366             break;
00367 
00368         case typeDouble:
00369             //*(double*)destData = *(double*)odbcData;
00370             stmt->setDoubleParam(paramNo, *(double*)odbcData);
00371             break;
00372 
00373 //        case csqlSqlTlongDouble:
00374 //        case csqlSqlTnumeric:
00375         case typeDecimal:
00376             //*(long double*)destData = *(long double*)odbcData;
00377             // TODO
00378             break;
00379 
00380         case typeString:
00381             stmt->setStringParam(paramNo, (char*) odbcData);
00382             /*if(odbcLen < destLen)
00383             {
00384                 strcpy( (char*)destData, (char*)odbcData);
00385                 *((char*)destData+odbcLen)='\0';
00386             }
00387             else
00388             {
00389                 strncpy((char*)destData,(char*)odbcData,(destLen-1));
00390                 *((char*)destData+destLen)='\0';
00391             }*/
00392             
00393             break;
00394         case typeDate: {
00395             Date dt;
00396             dt.set((*(SQL_DATE_STRUCT *)odbcData).year,
00397                    (*(SQL_DATE_STRUCT *)odbcData).month,
00398                    (*(SQL_DATE_STRUCT *)odbcData).day);
00399             stmt->setDateParam(paramNo, dt);
00400             break;
00401         }
00402         case typeTime: {
00403             Time tm;
00404             tm.set((*(SQL_TIME_STRUCT *)odbcData).hour,
00405                    (*(SQL_TIME_STRUCT *)odbcData).minute,
00406                    (*(SQL_TIME_STRUCT *)odbcData).second);
00407             stmt->setTimeParam(paramNo, tm);
00408             break;
00409         }
00410         case typeTimeStamp: {
00411             TimeStamp ts;
00412             ts.setDate((*(SQL_TIMESTAMP_STRUCT *)odbcData).year,
00413                        (*(SQL_TIMESTAMP_STRUCT *)odbcData).month,
00414                        (*(SQL_TIMESTAMP_STRUCT *)odbcData).day);
00415             ts.setTime((*(SQL_TIMESTAMP_STRUCT*)odbcData).hour,
00416                        (*(SQL_TIMESTAMP_STRUCT *)odbcData).minute,
00417                        (*(SQL_TIMESTAMP_STRUCT *)odbcData).second);
00418             stmt->setTimeStampParam(paramNo, ts);
00419             break;
00420         }
00421    }
00422     
00423 }
00424 
00425 //Copies data from the database onto the application buffer 
00426 //Called from SQLFetch
00427 //Destination here can be the actual application buffer or
00428 //it may be ipd_
00429 SQLINTEGER copyToOdbc(void *odbcData,SQLUINTEGER odbcLen,void *sourceData,SQLUINTEGER sourceLen,SQLSMALLINT type)
00430 {
00431     SQLINTEGER ind;
00432     if(odbcData == NULL || sourceData == NULL)
00433         return -1;
00434     switch( type )
00435     {
00436         case typeShort:
00437             *(short*)odbcData = *(short*)sourceData;
00438             break;
00439 
00440         case typeInt:
00441             *(int*)odbcData = *(int*)sourceData;
00442             break;
00443 
00444         case typeLong:
00445             *(long*)odbcData = *(long*)sourceData;
00446             break;
00447 
00448         case typeLongLong:
00449             *(long long*)odbcData = *(long long*)sourceData;
00450             break;
00451 
00452         case typeByteInt:
00453             *(char*)odbcData = *(char*)sourceData;
00454             break;
00455 
00456 //        case csqlSqlTbit:
00457 //            CSqlOdbcError::printDbg("copyToOdbc");
00458 //            *(unsigned char*)odbcData = *(unsigned char*)sourceData;
00459 //            break;
00460 
00461         case typeFloat:
00462             *(float*)odbcData = *(float*)sourceData;
00463             break;
00464         case typeDouble:
00465             *(double*)odbcData = *(double*)sourceData;
00466             break;
00467         case typeDecimal:
00468 
00469 //        case csqlSqlTlongDouble:
00470 //        case csqlSqlTnumeric:
00471             *(long double*)odbcData = *(long double*)sourceData;
00472             break;
00473         case typeString:
00474 //      case csqlSqlTvarString:
00475             if(sourceLen < odbcLen)
00476             {
00477                 strcpy((char*)odbcData,(char*)sourceData);
00478                 ind=strlen((char*)odbcData)+1;
00479             }
00480             else
00481             {
00482                 strncpy((char*)odbcData,(char*)sourceData ,odbcLen);
00483                 ind=odbcLen;
00484             }
00485             *((char *)odbcData+ind-1)='\0';
00486             break;
00487         case typeDate:
00488             (*(SQL_DATE_STRUCT *)odbcData).year = (*(Date *)sourceData).year();
00489             (*(SQL_DATE_STRUCT *)odbcData).month= (*(Date *)sourceData).month();
00490             (*(SQL_DATE_STRUCT *)odbcData).day = (*(Date *)sourceData).dayOfMonth();
00491             break;
00492         case typeTime:                        
00493             (*(SQL_TIME_STRUCT *)odbcData).hour = (*(Time *)sourceData).hours();
00494             (*(SQL_TIME_STRUCT *)odbcData).minute= (*(Time *)sourceData).minutes();
00495             (*(SQL_TIME_STRUCT *)odbcData).second = (*(Time *)sourceData).seconds();
00496             break;
00497         case typeTimeStamp:                        
00498             (*(SQL_TIMESTAMP_STRUCT *)odbcData).year= (*(TimeStamp *)sourceData).year();
00499             (*(SQL_TIMESTAMP_STRUCT *)odbcData).month= (*(TimeStamp *)sourceData).month();
00500             (*(SQL_TIMESTAMP_STRUCT *)odbcData).day= (*(TimeStamp *)sourceData).dayOfMonth();
00501             (*(SQL_TIMESTAMP_STRUCT *)odbcData).hour = (*(TimeStamp *)sourceData).hours();
00502             (*(SQL_TIMESTAMP_STRUCT *)odbcData).minute= (*(TimeStamp *)sourceData).minutes();
00503             (*(SQL_TIMESTAMP_STRUCT *)odbcData).second = (*(TimeStamp *)sourceData).seconds();
00504             break;
00505         case typeBinary:
00506 //        case csqlSqlTvarBinary:                        
00507 //            CSqlOdbcError::printDbg("copyToOdbc");
00508 //            memset((unsigned char*)odbcData,0,odbcLen);
00509             if(sourceLen < odbcLen)
00510             {
00511                 memcpy( (unsigned char*)odbcData , (unsigned char*)sourceData,sourceLen);
00512                 ind=sourceLen;
00513             }
00514             else
00515             {
00516                 memcpy( (unsigned char*)odbcData , (unsigned char*)sourceData,odbcLen);
00517                 ind=odbcLen;
00518             }
00519             break;
00520     }
00521     return ind;
00522 }

Generated on Mon Jun 9 22:37:14 2008 for csql by  doxygen 1.4.7