src/odbc/odbcEnv.cxx

Go to the documentation of this file.
00001 /*  Class CSqlOdbcEnv
00002     Description: Environment Handle manager. 
00003 
00004     ODBC API's:
00005                 CSqlOdbcEnv::SQLAllocHandle();
00006                 CSqlOdbcEnv::SQLFreeHandle();
00007                 CSqlOdbcEnv::SQLSetEnvAttr();
00008 
00009     Other Functions:
00010                 CSqlOdbcEnv::CSqlOdbcEnv();
00011 */
00012 
00013 #include "odbcCommon.h"
00014 
00015 // Constructor
00016 CSqlOdbcEnv::CSqlOdbcEnv( void ) : 
00017     handleType_( SQL_HANDLE_ENV ), 
00018     state_( E1 ), 
00019     err_( SQL_HANDLE_ENV ),
00020     odbcVersion_( SQL_OV_ODBC3 )
00021 {}
00022 
00023 SQLRETURN SQLAllocEnv(
00024     SQLHENV *EnvironmentHandle)
00025 {
00026     return( CSqlOdbcEnv::SQLAllocHandle( (SQLHANDLE) SQL_NULL_HANDLE, EnvironmentHandle ) );
00027 }
00028 
00029 // Static function
00030 SQLRETURN CSqlOdbcEnv::SQLAllocHandle(
00031     SQLHANDLE   inputHandle,    // IN
00032     SQLHANDLE   *outputHandle ) // OUT
00033 {   
00034     // Error in parameters
00035     if( inputHandle != SQL_NULL_HANDLE )
00036     {
00037         *outputHandle = SQL_NULL_HENV;
00038         return( SQL_ERROR );
00039     }
00040 
00041     // Allocate Environment.
00042     *outputHandle = (SQLHANDLE*) new CSqlOdbcEnv;
00043     if( *outputHandle == NULL )
00044     {
00045         globalError.set((int) ERROR_MEMALLOC );
00046         globalError.printStr( SQL_OV_ODBC3 );
00047         return( SQL_ERROR );
00048     }
00049 
00050     return( SQL_SUCCESS );
00051 }
00052 
00053 SQLRETURN SQLFreeEnv(
00054     SQLHENV EnvironmentHandle)
00055 {
00056     return( CSqlOdbcEnv::SQLFreeHandle( EnvironmentHandle ) );
00057 }
00058 
00059 // Static function
00060 SQLRETURN CSqlOdbcEnv::SQLFreeHandle(
00061     SQLHANDLE inputHandle)      // IN
00062 {
00063     CSqlOdbcEnv *inputEnv = (CSqlOdbcEnv*) inputHandle;
00064 
00065     // Validate handle
00066     if( isValidHandle( inputEnv, SQL_HANDLE_ENV ) != SQL_SUCCESS )
00067         return( SQL_INVALID_HANDLE );
00068 
00069     // Check whether we can proceed.
00070     if( inputEnv->chkStateForSQLFreeHandle() != SQL_SUCCESS )
00071         return( SQL_ERROR );
00072         
00073     inputEnv->handleType_ = -1; // Make object invalid.
00074     delete inputEnv;            // Delete Environment.
00075 
00076     return( SQL_SUCCESS );
00077 }
00078 
00079 SQLRETURN SQLTransact(
00080     SQLHENV EnvironmentHandle,
00081     SQLHDBC ConnectionHandle, 
00082     SQLUSMALLINT CompletionType)
00083 {
00084     // Call SQLEndTrans() on handle whichever is valid 
00085     if( isValidHandle( EnvironmentHandle, SQL_HANDLE_ENV ) == SQL_SUCCESS )
00086         return( ((CSqlOdbcEnv*) EnvironmentHandle)->SQLEndTran( CompletionType ) );
00087 
00088     if( isValidHandle( ConnectionHandle, SQL_HANDLE_DBC ) == SQL_SUCCESS )
00089         return( ((CSqlOdbcDbc*) ConnectionHandle)->SQLEndTran( CompletionType ) );
00090 
00091     return( SQL_INVALID_HANDLE );
00092 }
00093 
00094 SQLRETURN CSqlOdbcEnv::SQLEndTran(
00095     SQLSMALLINT completionType) // IN
00096 {
00097     // Start with NO_ERR
00098     err_.set( NO_ERR );
00099 
00100     // Stop if odbcVersion not set.
00101     if( odbcVersion_ == 0 )
00102     {
00103         err_.set( ERROR_FUNCSEQ );
00104         return( SQL_ERROR );
00105     }
00106 
00107     // If no connection objects ?
00108     if( dbcList_.size() == 0 )
00109         return( SQL_SUCCESS );
00110 
00111     // For each connect obj, call SQLEndTran
00112     std::vector<CSqlOdbcDbc*>::iterator iter;
00113     iter = dbcList_.begin();
00114     while( iter != dbcList_.end() )
00115     {
00116         if( (*iter)->chkStateForSQLEndTran() == SQL_SUCCESS ) {
00117             if( (*iter)->SQLEndTran( completionType ) == SQL_ERROR )
00118                 return( SQL_ERROR ); 
00119         } 
00120         else return ( SQL_ERROR );
00121         iter++;
00122     }
00123 
00124     return( SQL_SUCCESS );
00125 }
00126 
00127 SQLRETURN SQLSetEnvAttr(
00128             SQLHENV    inputEnv,        // IN
00129             SQLINTEGER Attribute,       // IN
00130             SQLPOINTER Value,           // IN
00131             SQLINTEGER StringLength)    // IN
00132 {
00133    // Validate handle
00134    if( isValidHandle( inputEnv, SQL_HANDLE_ENV ) != SQL_SUCCESS )
00135         return( SQL_INVALID_HANDLE );
00136 
00137    return( ((CSqlOdbcEnv*)inputEnv)->SQLSetEnvAttr( Attribute, Value, StringLength ) );
00138 }
00139 
00140 SQLRETURN  CSqlOdbcEnv::SQLSetEnvAttr(
00141             SQLINTEGER attribute,       // IN
00142             SQLPOINTER value,           // IN
00143             SQLINTEGER stringLength)    // IN
00144 {
00145     // Start with NO_ERR
00146     err_.set( NO_ERR );
00147 
00148     // Check whether we can proceed.
00149     if( chkStateForSQLSetEnvAttr() != SQL_SUCCESS )
00150         return( SQL_ERROR );
00151         
00152     switch( attribute )
00153     {
00154         case SQL_ATTR_ODBC_VERSION:
00155             switch( (SQLINTEGER) value )
00156             {
00157                 case SQL_OV_ODBC2:
00158                 case SQL_OV_ODBC3: break;
00159                 default:
00160                         err_.set( ERROR_OPTCHANGE );
00161                         return( SQL_ERROR );
00162             }
00163             odbcVersion_ = (SQLUINTEGER) value;
00164             break;
00165 
00166         case SQL_ATTR_OUTPUT_NTS:
00167             if( (SQLINTEGER) value == SQL_TRUE )
00168                 return( SQL_SUCCESS );
00169             else
00170             {
00171                 err_.set( ERROR_OPTFEATURE_NOTIMP );
00172                 return( SQL_ERROR );
00173             }
00174             break;
00175 
00176         default: err_.set( ERROR_OPTRANGE );
00177                  return( SQL_ERROR );
00178     }
00179 
00180     return( SQL_SUCCESS );
00181 }
00182 
00183 SQLRETURN SQLGetEnvAttr(
00184     SQLHENV EnvironmentHandle,
00185     SQLINTEGER Attribute,
00186     SQLPOINTER Value,
00187     SQLINTEGER BufferLength, 
00188     SQLINTEGER *StringLength)
00189 {
00190    // Validate handle
00191    if( isValidHandle( EnvironmentHandle, SQL_HANDLE_ENV ) != SQL_SUCCESS )
00192         return( SQL_INVALID_HANDLE );
00193 
00194    return( ((CSqlOdbcEnv*)EnvironmentHandle)->SQLGetEnvAttr( Attribute, Value, BufferLength, StringLength ) );
00195 }
00196 
00197 SQLRETURN CSqlOdbcEnv::SQLGetEnvAttr(
00198     SQLINTEGER attribute,
00199     SQLPOINTER value,
00200     SQLINTEGER bufferLength, 
00201     SQLINTEGER *stringLength)
00202 {
00203     // Start with NO_ERR
00204     err_.set( NO_ERR );
00205 
00206     // Can we proceed.? Always ( in all states)
00207         
00208     switch( attribute )
00209     {
00210         case SQL_ATTR_ODBC_VERSION:
00211             *((SQLUINTEGER*) value) = odbcVersion_;
00212             break;
00213 
00214         case SQL_ATTR_OUTPUT_NTS:
00215             *((SQLUINTEGER*) value) = SQL_TRUE;
00216             break;
00217 
00218         default: err_.set( ERROR_OPTRANGE );
00219                  return( SQL_ERROR );
00220     }
00221 
00222     return( SQL_SUCCESS );
00223 }

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