windows changes
[csql.git] / src / odbc / odbcEnv.cxx
blobb1e2cba8095605ae9b0b42244b860a46459ba119
1 /* Class CSqlOdbcEnv
2 Description: Environment Handle manager.
4 ODBC API's:
5 CSqlOdbcEnv::SQLAllocHandle();
6 CSqlOdbcEnv::SQLFreeHandle();
7 CSqlOdbcEnv::SQLSetEnvAttr();
9 Other Functions:
10 CSqlOdbcEnv::CSqlOdbcEnv();
13 #include "odbcCommon.h"
15 // Constructor
16 CSqlOdbcEnv::CSqlOdbcEnv( void ) :
17 handleType_( SQL_HANDLE_ENV ),
18 state_( E1 ),
19 err_( SQL_HANDLE_ENV ),
20 odbcVersion_( SQL_OV_ODBC3 )
23 SQLRETURN SQLAllocEnv(
24 SQLHENV *EnvironmentHandle)
26 return( CSqlOdbcEnv::SQLAllocHandle( (SQLHANDLE) SQL_NULL_HANDLE, EnvironmentHandle ) );
29 // Static function
30 SQLRETURN CSqlOdbcEnv::SQLAllocHandle(
31 SQLHANDLE inputHandle, // IN
32 SQLHANDLE *outputHandle ) // OUT
34 // Error in parameters
35 if( inputHandle != SQL_NULL_HANDLE )
37 *outputHandle = SQL_NULL_HENV;
38 return( SQL_ERROR );
41 // Allocate Environment.
42 *outputHandle = (SQLHANDLE*) new CSqlOdbcEnv;
43 if( *outputHandle == NULL )
45 globalError.set((int) ERROR_MEMALLOC );
46 globalError.printStr( SQL_OV_ODBC3 );
47 return( SQL_ERROR );
50 return( SQL_SUCCESS );
53 SQLRETURN SQLFreeEnv(
54 SQLHENV EnvironmentHandle)
56 return( CSqlOdbcEnv::SQLFreeHandle( EnvironmentHandle ) );
59 // Static function
60 SQLRETURN CSqlOdbcEnv::SQLFreeHandle(
61 SQLHANDLE inputHandle) // IN
63 CSqlOdbcEnv *inputEnv = (CSqlOdbcEnv*) inputHandle;
65 // Validate handle
66 if( isValidHandle( inputEnv, SQL_HANDLE_ENV ) != SQL_SUCCESS )
67 return( SQL_INVALID_HANDLE );
69 // Check whether we can proceed.
70 if( inputEnv->chkStateForSQLFreeHandle() != SQL_SUCCESS )
71 return( SQL_ERROR );
73 inputEnv->handleType_ = -1; // Make object invalid.
74 delete inputEnv; // Delete Environment.
76 return( SQL_SUCCESS );
79 SQLRETURN SQLTransact(
80 SQLHENV EnvironmentHandle,
81 SQLHDBC ConnectionHandle,
82 SQLUSMALLINT CompletionType)
84 // Call SQLEndTrans() on handle whichever is valid
85 if( isValidHandle( EnvironmentHandle, SQL_HANDLE_ENV ) == SQL_SUCCESS )
86 return( ((CSqlOdbcEnv*) EnvironmentHandle)->SQLEndTran( CompletionType ) );
88 if( isValidHandle( ConnectionHandle, SQL_HANDLE_DBC ) == SQL_SUCCESS )
89 return( ((CSqlOdbcDbc*) ConnectionHandle)->SQLEndTran( CompletionType ) );
91 return( SQL_INVALID_HANDLE );
94 SQLRETURN CSqlOdbcEnv::SQLEndTran(
95 SQLSMALLINT completionType) // IN
97 // Start with NO_ERR
98 err_.set( NO_ERR );
100 // Stop if odbcVersion not set.
101 if( odbcVersion_ == 0 )
103 err_.set( ERROR_FUNCSEQ );
104 return( SQL_ERROR );
107 // If no connection objects ?
108 if( dbcList_.size() == 0 )
109 return( SQL_SUCCESS );
111 // For each connect obj, call SQLEndTran
112 ListIterator iter = dbcList_.getIterator();
113 CSqlOdbcDbc *dbcElem = NULL;
114 while(iter.hasElement()) {
115 dbcElem = (CSqlOdbcDbc *) iter.nextElement();
116 if( dbcElem->chkStateForSQLEndTran() == SQL_SUCCESS ) {
117 if( dbcElem->SQLEndTran( completionType ) == SQL_ERROR )
118 return( SQL_ERROR );
120 else return ( SQL_ERROR );
122 return( SQL_SUCCESS );
125 SQLRETURN SQLSetEnvAttr(
126 SQLHENV inputEnv, // IN
127 SQLINTEGER Attribute, // IN
128 SQLPOINTER Value, // IN
129 SQLINTEGER StringLength) // IN
131 // Validate handle
132 if( isValidHandle( inputEnv, SQL_HANDLE_ENV ) != SQL_SUCCESS )
133 return( SQL_INVALID_HANDLE );
135 return( ((CSqlOdbcEnv*)inputEnv)->SQLSetEnvAttr( Attribute, Value, StringLength ) );
138 SQLRETURN CSqlOdbcEnv::SQLSetEnvAttr(
139 SQLINTEGER attribute, // IN
140 SQLPOINTER value, // IN
141 SQLINTEGER stringLength) // IN
143 // Start with NO_ERR
144 err_.set( NO_ERR );
146 // Check whether we can proceed.
147 if( chkStateForSQLSetEnvAttr() != SQL_SUCCESS )
148 return( SQL_ERROR );
150 switch( attribute )
152 case SQL_ATTR_ODBC_VERSION:
153 switch( (SQLINTEGER) value )
155 case SQL_OV_ODBC2:
156 case SQL_OV_ODBC3: break;
157 default:
158 err_.set( ERROR_OPTCHANGE );
159 return( SQL_ERROR );
161 odbcVersion_ = (SQLUINTEGER) value;
162 break;
164 case SQL_ATTR_OUTPUT_NTS:
165 if( (SQLINTEGER) value == SQL_TRUE )
166 return( SQL_SUCCESS );
167 else
169 err_.set( ERROR_OPTFEATURE_NOTIMP );
170 return( SQL_ERROR );
172 break;
174 default: err_.set( ERROR_OPTRANGE );
175 return( SQL_ERROR );
178 return( SQL_SUCCESS );
181 SQLRETURN SQLGetEnvAttr(
182 SQLHENV EnvironmentHandle,
183 SQLINTEGER Attribute,
184 SQLPOINTER Value,
185 SQLINTEGER BufferLength,
186 SQLINTEGER *StringLength)
188 // Validate handle
189 if( isValidHandle( EnvironmentHandle, SQL_HANDLE_ENV ) != SQL_SUCCESS )
190 return( SQL_INVALID_HANDLE );
192 return( ((CSqlOdbcEnv*)EnvironmentHandle)->SQLGetEnvAttr( Attribute, Value, BufferLength, StringLength ) );
195 SQLRETURN CSqlOdbcEnv::SQLGetEnvAttr(
196 SQLINTEGER attribute,
197 SQLPOINTER value,
198 SQLINTEGER bufferLength,
199 SQLINTEGER *stringLength)
201 // Start with NO_ERR
202 err_.set( NO_ERR );
204 // Can we proceed.? Always ( in all states)
206 switch( attribute )
208 case SQL_ATTR_ODBC_VERSION:
209 *((SQLUINTEGER*) value) = odbcVersion_;
210 break;
212 case SQL_ATTR_OUTPUT_NTS:
213 *((SQLUINTEGER*) value) = SQL_TRUE;
214 break;
216 default: err_.set( ERROR_OPTRANGE );
217 return( SQL_ERROR );
220 return( SQL_SUCCESS );