2727namespace arrow {
2828SQLRETURN SQLAllocHandle (SQLSMALLINT type, SQLHANDLE parent, SQLHANDLE* result) {
2929 // TODO: implement SQLAllocHandle by linking to `odbc_impl`
30- *result = 0 ;
30+ *result = nullptr ;
3131
3232 switch (type) {
3333 case SQL_HANDLE_ENV: {
@@ -43,7 +43,6 @@ SQLRETURN SQLAllocHandle(SQLSMALLINT type, SQLHANDLE parent, SQLHANDLE* result)
4343
4444 return SQL_SUCCESS;
4545 } catch (const std::bad_alloc&) {
46- // Will be caught in odbc abstration layer if using execute with diagnostics
4746 // allocating environment failed so cannot log diagnostic error here
4847 return SQL_ERROR;
4948 }
@@ -57,22 +56,17 @@ SQLRETURN SQLAllocHandle(SQLSMALLINT type, SQLHANDLE parent, SQLHANDLE* result)
5756
5857 ODBCEnvironment* environment = reinterpret_cast <ODBCEnvironment*>(parent);
5958
60- if (!environment) {
61- return SQL_INVALID_HANDLE;
62- }
63-
64- try {
59+ return ODBCEnvironment::ExecuteWithDiagnostics (environment, SQL_ERROR, [=]() {
6560 std::shared_ptr<ODBCConnection> conn = environment->CreateConnection ();
66- *result = reinterpret_cast <SQLHDBC>(conn.get ());
6761
68- return SQL_SUCCESS;
69- } catch ( const std::bad_alloc&) {
70- // Will be caught in odbc abstration layer if using execute with diagnostics
71- environment-> GetDiagnostics (). AddError ( driver::odbcabstraction::DriverException (
72- " A memory allocation error occurred. " , " HY001 " ));
62+ if (conn) {
63+ *result = reinterpret_cast <SQLHDBC>(conn. get ());
64+
65+ return SQL_SUCCESS;
66+ }
7367
7468 return SQL_ERROR;
75- }
69+ });
7670 }
7771
7872 case SQL_HANDLE_STMT: {
@@ -107,13 +101,11 @@ SQLRETURN SQLFreeHandle(SQLSMALLINT type, SQLHANDLE handle) {
107101
108102 ODBCConnection* conn = reinterpret_cast <ODBCConnection*>(handle);
109103
110- if (!conn) {
111- return SQL_INVALID_HANDLE;
112- }
113-
114- conn->releaseConnection ();
104+ return ODBCConnection::ExecuteWithDiagnostics (conn, SQL_ERROR, [=]() {
105+ conn->releaseConnection ();
115106
116- return SQL_SUCCESS;
107+ return SQL_SUCCESS;
108+ });
117109 }
118110
119111 case SQL_HANDLE_STMT:
@@ -131,119 +123,104 @@ SQLRETURN SQLFreeHandle(SQLSMALLINT type, SQLHANDLE handle) {
131123
132124SQLRETURN SQLGetEnvAttr (SQLHENV env, SQLINTEGER attr, SQLPOINTER valuePtr,
133125 SQLINTEGER bufferLen, SQLINTEGER* strLenPtr) {
126+ using driver::odbcabstraction::DriverException;
134127 using ODBC::ODBCEnvironment;
135128
136129 ODBCEnvironment* environment = reinterpret_cast <ODBCEnvironment*>(env);
137130
138- if (!environment) {
139- return SQL_INVALID_HANDLE;
140- }
131+ return ODBCEnvironment::ExecuteWithDiagnostics (environment, SQL_ERROR, [=]() {
132+ switch (attr) {
133+ case SQL_ATTR_ODBC_VERSION: {
134+ if (!valuePtr && !strLenPtr) {
135+ throw DriverException (" Invalid null pointer for attribute." , " HY000" );
136+ }
137+
138+ if (valuePtr) {
139+ SQLINTEGER* value = reinterpret_cast <SQLINTEGER*>(valuePtr);
140+ *value = static_cast <SQLSMALLINT>(environment->getODBCVersion ());
141+ }
142+
143+ if (strLenPtr) {
144+ *strLenPtr = sizeof (SQLINTEGER);
145+ }
141146
142- switch (attr) {
143- case SQL_ATTR_ODBC_VERSION: {
144- if (valuePtr) {
145- SQLINTEGER* value = reinterpret_cast <SQLINTEGER*>(valuePtr);
146- *value = static_cast <SQLSMALLINT>(environment->getODBCVersion ());
147- return SQL_SUCCESS;
148- } else if (strLenPtr) {
149- *strLenPtr = sizeof (SQLINTEGER);
150147 return SQL_SUCCESS;
151- } else {
152- environment->GetDiagnostics ().AddError (driver::odbcabstraction::DriverException (
153- " Invalid null pointer for attribute." , " HY000" ));
154- return SQL_ERROR;
155148 }
156- }
157149
158- case SQL_ATTR_OUTPUT_NTS: {
159- if (valuePtr) {
160- // output nts always returns SQL_TRUE
161- SQLINTEGER* value = reinterpret_cast <SQLINTEGER*>(valuePtr);
162- *value = SQL_TRUE;
163- return SQL_SUCCESS;
164- } else if (strLenPtr) {
165- *strLenPtr = sizeof (SQLINTEGER);
150+ case SQL_ATTR_OUTPUT_NTS: {
151+ if (!valuePtr && !strLenPtr) {
152+ throw DriverException (" Invalid null pointer for attribute." , " HY000" );
153+ }
154+
155+ if (valuePtr) {
156+ // output nts always returns SQL_TRUE
157+ SQLINTEGER* value = reinterpret_cast <SQLINTEGER*>(valuePtr);
158+ *value = SQL_TRUE;
159+ }
160+
161+ if (strLenPtr) {
162+ *strLenPtr = sizeof (SQLINTEGER);
163+ }
164+
166165 return SQL_SUCCESS;
167- } else {
168- environment->GetDiagnostics ().AddError (driver::odbcabstraction::DriverException (
169- " Invalid null pointer for attribute." , " HY000" ));
170- return SQL_ERROR;
171166 }
172- }
173167
174- case SQL_ATTR_CONNECTION_POOLING:
175- case SQL_ATTR_APP_ROW_DESC: {
176- environment->GetDiagnostics ().AddError (driver::odbcabstraction::DriverException (
177- " Optional feature not supported." , " HYC00" ));
178- return SQL_ERROR;
179- }
168+ case SQL_ATTR_CONNECTION_POOLING:
169+ case SQL_ATTR_APP_ROW_DESC: {
170+ throw DriverException (" Optional feature not supported." , " HYC00" );
171+ }
180172
181- default : {
182- environment->GetDiagnostics ().AddError (
183- driver::odbcabstraction::DriverException (" Invalid attribute" , " HYC00" ));
184- return SQL_ERROR;
173+ default : {
174+ throw DriverException (" Invalid attribute" , " HYC00" );
175+ }
185176 }
186- }
187-
188- return SQL_ERROR;
177+ });
189178}
190179
191180SQLRETURN SQLSetEnvAttr (SQLHENV env, SQLINTEGER attr, SQLPOINTER valuePtr,
192181 SQLINTEGER strLen) {
182+ using driver::odbcabstraction::DriverException;
193183 using ODBC::ODBCEnvironment;
194184
195185 ODBCEnvironment* environment = reinterpret_cast <ODBCEnvironment*>(env);
196186
197- if (!environment) {
198- return SQL_INVALID_HANDLE;
199- }
200-
201- if (!valuePtr) {
202- environment->GetDiagnostics ().AddError (driver::odbcabstraction::DriverException (
203- " Invalid null pointer for attribute." , " HY024" ));
204-
205- return SQL_ERROR;
206- }
187+ return ODBCEnvironment::ExecuteWithDiagnostics (environment, SQL_ERROR, [=]() {
188+ if (!valuePtr) {
189+ throw DriverException (" Invalid null pointer for attribute." , " HY024" );
190+ }
207191
208- switch (attr) {
209- case SQL_ATTR_ODBC_VERSION: {
210- SQLINTEGER version = static_cast <SQLINTEGER>(reinterpret_cast <intptr_t >(valuePtr));
211- if (version == SQL_OV_ODBC2 || version == SQL_OV_ODBC3) {
212- environment->setODBCVersion (version);
213- return SQL_SUCCESS;
214- } else {
215- environment->GetDiagnostics ().AddError (driver::odbcabstraction::DriverException (
216- " Invalid value for attribute" , " HY024" ));
217- return SQL_ERROR;
192+ switch (attr) {
193+ case SQL_ATTR_ODBC_VERSION: {
194+ SQLINTEGER version =
195+ static_cast <SQLINTEGER>(reinterpret_cast <intptr_t >(valuePtr));
196+ if (version == SQL_OV_ODBC2 || version == SQL_OV_ODBC3) {
197+ environment->setODBCVersion (version);
198+
199+ return SQL_SUCCESS;
200+ } else {
201+ throw DriverException (" Invalid value for attribute" , " HY024" );
202+ }
218203 }
219- }
220204
221- case SQL_ATTR_OUTPUT_NTS: {
222- // output nts can not be set to SQL_FALSE, is always SQL_TRUE
223- SQLINTEGER value = static_cast <SQLINTEGER>(reinterpret_cast <intptr_t >(valuePtr));
224- if (value == SQL_TRUE) {
225- return SQL_SUCCESS;
226- } else {
227- environment->GetDiagnostics ().AddError (driver::odbcabstraction::DriverException (
228- " Invalid value for attribute" , " HY024" ));
229- return SQL_ERROR;
205+ case SQL_ATTR_OUTPUT_NTS: {
206+ // output nts can not be set to SQL_FALSE, is always SQL_TRUE
207+ SQLINTEGER value = static_cast <SQLINTEGER>(reinterpret_cast <intptr_t >(valuePtr));
208+ if (value == SQL_TRUE) {
209+ return SQL_SUCCESS;
210+ } else {
211+ throw DriverException (" Invalid value for attribute" , " HY024" );
212+ }
230213 }
231- }
232214
233- case SQL_ATTR_CONNECTION_POOLING:
234- case SQL_ATTR_APP_ROW_DESC: {
235- environment->GetDiagnostics ().AddError (driver::odbcabstraction::DriverException (
236- " Optional feature not supported." , " HYC00" ));
237- return SQL_ERROR;
238- }
215+ case SQL_ATTR_CONNECTION_POOLING:
216+ case SQL_ATTR_APP_ROW_DESC: {
217+ throw DriverException (" Optional feature not supported." , " HYC00" );
218+ }
239219
240- default : {
241- environment->GetDiagnostics ().AddError (
242- driver::odbcabstraction::DriverException (" Invalid attribute" , " HY092" ));
243- return SQL_ERROR;
220+ default : {
221+ throw DriverException (" Invalid attribute" , " HY092" );
222+ }
244223 }
245- }
246-
247- return SQL_ERROR;
224+ });
248225}
249226} // namespace arrow
0 commit comments