alinaliBQ commented on code in PR #47760:
URL: https://github.com/apache/arrow/pull/47760#discussion_r2446303776


##########
cpp/src/arrow/flight/sql/odbc/tests/connection_test.cc:
##########
@@ -0,0 +1,82 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+#include "arrow/flight/sql/odbc/tests/odbc_test_suite.h"
+
+#ifdef _WIN32
+#  include <windows.h>
+#endif

Review Comment:
   fixed



##########
cpp/src/arrow/flight/sql/odbc/tests/connection_test.cc:
##########
@@ -0,0 +1,82 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+#include "arrow/flight/sql/odbc/tests/odbc_test_suite.h"
+
+#ifdef _WIN32
+#  include <windows.h>
+#endif
+
+#include <sql.h>
+#include <sqltypes.h>
+#include <sqlucode.h>
+
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+
+namespace arrow::flight::sql::odbc {
+
+TEST(SQLGetEnvAttr, TestSQLGetEnvAttrODBCVersion) {
+  // ODBC Environment
+  SQLHENV env;
+
+  SQLINTEGER version;
+
+  // Allocate an environment handle
+  SQLRETURN return_env = SQLAllocEnv(&env);
+
+  EXPECT_EQ(SQL_SUCCESS, return_env);

Review Comment:
   fixed to use `ASSERT_EQ`



##########
cpp/src/arrow/flight/sql/odbc/tests/connection_test.cc:
##########
@@ -0,0 +1,82 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+#include "arrow/flight/sql/odbc/tests/odbc_test_suite.h"
+
+#ifdef _WIN32
+#  include <windows.h>
+#endif
+
+#include <sql.h>
+#include <sqltypes.h>
+#include <sqlucode.h>
+
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+
+namespace arrow::flight::sql::odbc {
+
+TEST(SQLGetEnvAttr, TestSQLGetEnvAttrODBCVersion) {
+  // ODBC Environment
+  SQLHENV env;
+
+  SQLINTEGER version;
+
+  // Allocate an environment handle
+  SQLRETURN return_env = SQLAllocEnv(&env);
+
+  EXPECT_EQ(SQL_SUCCESS, return_env);
+
+  SQLRETURN return_get = SQLGetEnvAttr(env, SQL_ATTR_ODBC_VERSION, &version, 
0, 0);
+
+  EXPECT_EQ(SQL_SUCCESS, return_get);
+
+  EXPECT_EQ(SQL_OV_ODBC2, version);
+}
+
+TEST(SQLSetEnvAttr, TestSQLSetEnvAttrODBCVersionValid) {
+  // ODBC Environment
+  SQLHENV env;
+
+  // Allocate an environment handle
+  SQLRETURN return_env = SQLAllocEnv(&env);
+
+  EXPECT_EQ(SQL_SUCCESS, return_env);
+
+  // Attempt to set to unsupported version
+  SQLRETURN return_set =
+      SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, 
reinterpret_cast<void*>(SQL_OV_ODBC2), 0);
+
+  EXPECT_EQ(SQL_SUCCESS, return_set);

Review Comment:
   yes `SQL_SUCCESS` is expected since the version is supposed to be supported. 
   
   Added check for `SQLGetEnvAttr`



##########
cpp/src/arrow/flight/sql/odbc/tests/connection_test.cc:
##########
@@ -0,0 +1,82 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+#include "arrow/flight/sql/odbc/tests/odbc_test_suite.h"
+
+#ifdef _WIN32
+#  include <windows.h>
+#endif
+
+#include <sql.h>
+#include <sqltypes.h>
+#include <sqlucode.h>
+
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+
+namespace arrow::flight::sql::odbc {
+
+TEST(SQLGetEnvAttr, TestSQLGetEnvAttrODBCVersion) {
+  // ODBC Environment
+  SQLHENV env;
+
+  SQLINTEGER version;
+
+  // Allocate an environment handle
+  SQLRETURN return_env = SQLAllocEnv(&env);
+
+  EXPECT_EQ(SQL_SUCCESS, return_env);
+
+  SQLRETURN return_get = SQLGetEnvAttr(env, SQL_ATTR_ODBC_VERSION, &version, 
0, 0);
+
+  EXPECT_EQ(SQL_SUCCESS, return_get);
+
+  EXPECT_EQ(SQL_OV_ODBC2, version);
+}
+
+TEST(SQLSetEnvAttr, TestSQLSetEnvAttrODBCVersionValid) {
+  // ODBC Environment
+  SQLHENV env;
+
+  // Allocate an environment handle
+  SQLRETURN return_env = SQLAllocEnv(&env);
+
+  EXPECT_EQ(SQL_SUCCESS, return_env);
+
+  // Attempt to set to unsupported version

Review Comment:
   fixed the comment, thanks good catch



##########
cpp/src/arrow/flight/sql/odbc/tests/connection_test.cc:
##########
@@ -0,0 +1,82 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+#include "arrow/flight/sql/odbc/tests/odbc_test_suite.h"
+
+#ifdef _WIN32
+#  include <windows.h>
+#endif
+
+#include <sql.h>
+#include <sqltypes.h>
+#include <sqlucode.h>
+
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+
+namespace arrow::flight::sql::odbc {
+
+TEST(SQLGetEnvAttr, TestSQLGetEnvAttrODBCVersion) {
+  // ODBC Environment

Review Comment:
   removed



##########
cpp/src/arrow/flight/sql/odbc/odbc_api.cc:
##########
@@ -100,16 +100,107 @@ SQLRETURN SQLGetEnvAttr(SQLHENV env, SQLINTEGER attr, 
SQLPOINTER value_ptr,
   ARROW_LOG(DEBUG) << "SQLGetEnvAttr called with env: " << env << ", attr: " 
<< attr
                    << ", value_ptr: " << value_ptr << ", buffer_length: " << 
buffer_length
                    << ", str_len_ptr: " << static_cast<const 
void*>(str_len_ptr);
-  // GH-46575 TODO: Implement SQLGetEnvAttr
-  return SQL_INVALID_HANDLE;
+
+  using driver::odbcabstraction::DriverException;
+  using ODBC::ODBCEnvironment;
+
+  ODBCEnvironment* environment = reinterpret_cast<ODBCEnvironment*>(env);
+
+  return ODBCEnvironment::ExecuteWithDiagnostics(environment, SQL_ERROR, [=]() 
{
+    switch (attr) {
+      case SQL_ATTR_ODBC_VERSION: {
+        if (!value_ptr && !str_len_ptr) {
+          throw DriverException("Invalid null pointer for attribute.", 
"HY000");
+        }

Review Comment:
   Added a test `DISABLED_TestSQLGetEnvAttrNullValuePointer` for this case. The 
test case is disabled because call to SQLGetEnvAttr is handled by the driver 
manager on Windows. And the Windows driver manager doesn't error out when null 
pointer is passed, even though it should. 
   
   Potentially this can be tested on mac/linux depending on the driver manager 
on these systems



##########
cpp/src/arrow/flight/sql/odbc/odbc_api.cc:
##########
@@ -100,16 +100,107 @@ SQLRETURN SQLGetEnvAttr(SQLHENV env, SQLINTEGER attr, 
SQLPOINTER value_ptr,
   ARROW_LOG(DEBUG) << "SQLGetEnvAttr called with env: " << env << ", attr: " 
<< attr
                    << ", value_ptr: " << value_ptr << ", buffer_length: " << 
buffer_length
                    << ", str_len_ptr: " << static_cast<const 
void*>(str_len_ptr);

Review Comment:
   We generally log the ODBC API calls for debugging so we know which ODBC APIs 
got passed to the driver, is it ok to keep these logs? 



##########
cpp/src/arrow/flight/sql/odbc/odbc_api.cc:
##########
@@ -100,16 +100,107 @@ SQLRETURN SQLGetEnvAttr(SQLHENV env, SQLINTEGER attr, 
SQLPOINTER value_ptr,
   ARROW_LOG(DEBUG) << "SQLGetEnvAttr called with env: " << env << ", attr: " 
<< attr
                    << ", value_ptr: " << value_ptr << ", buffer_length: " << 
buffer_length
                    << ", str_len_ptr: " << static_cast<const 
void*>(str_len_ptr);
-  // GH-46575 TODO: Implement SQLGetEnvAttr
-  return SQL_INVALID_HANDLE;
+
+  using driver::odbcabstraction::DriverException;
+  using ODBC::ODBCEnvironment;
+
+  ODBCEnvironment* environment = reinterpret_cast<ODBCEnvironment*>(env);
+
+  return ODBCEnvironment::ExecuteWithDiagnostics(environment, SQL_ERROR, [=]() 
{
+    switch (attr) {
+      case SQL_ATTR_ODBC_VERSION: {
+        if (!value_ptr && !str_len_ptr) {
+          throw DriverException("Invalid null pointer for attribute.", 
"HY000");
+        }

Review Comment:
   Probably not in this case. I think attribute needs to be checked first 
before the pointers, and ODBC needs to error out on attribute value first.
   In a case where attribute, `value_ptr` and `str_len_ptr` are all invalid, 
`HYC00` (error code for invalid attribute) should be returned first. So we put 
`value_ptr` and `str_len_ptr` check under the `switch` statement



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to