diff --git a/cpp/src/arrow/flight/sql/odbc/connection-options.md b/cpp/src/arrow/flight/sql/odbc/connection-options.md index 044368c506de..6c83e1ede9d2 100644 --- a/cpp/src/arrow/flight/sql/odbc/connection-options.md +++ b/cpp/src/arrow/flight/sql/odbc/connection-options.md @@ -58,7 +58,7 @@ useWideChar = true | `useEncryption` | Setting to determine if an SSL-encrypted connection should be used. | `true` | | `disableCertificateVerification` | Setting to determine if the driver should verify the host certificate against the trust store. | `false` | | `trustedCerts` | The full path of the .pem file containing certificates for the purpose of verifying the server. | `NONE` | -| `useSystemTrustStore` | Setting to determine whether to use a CA certificate from the system's trust store or from a specified .pem file. | `true` on Windows, `false` on macOS & Linux | +| `useSystemTrustStore` | Setting to determine whether to use a CA certificate from the system's trust store or from a specified .pem file. When `true`, `trustedCerts` is ignored. | `true` on Windows. On Linux, `true` when loaded from a DSN and `false` in a connection string. `false` on macOS. | | `stringColumnLength` | Maximum length of a string column. Some apps may require a lower length. | `NONE` | | `useWideChar` | Setting to determine if wide characters should be used. Important for Unicode applications. | `true` on Windows, `false` on macOS & Linux | | `chunkBufferCapacity` | Capacity of a chunk buffer. Larger values will improve throughput at the cost of increased memory usage. | 5 | diff --git a/cpp/src/arrow/flight/sql/odbc/odbc_impl/CMakeLists.txt b/cpp/src/arrow/flight/sql/odbc/odbc_impl/CMakeLists.txt index 77003fd3589c..cc14f5f00a03 100644 --- a/cpp/src/arrow/flight/sql/odbc/odbc_impl/CMakeLists.txt +++ b/cpp/src/arrow/flight/sql/odbc/odbc_impl/CMakeLists.txt @@ -177,8 +177,11 @@ target_link_libraries(arrow_odbc_spi_impl_cli arrow_odbc_spi_impl) # Use STATIC_LINK_LIBS when Flight tests require static linkage to avoid mixing static/shared Arrow if(APPLE) # macOS - always static + # arrow_odbc_spi_impl does not link the iODBC library on macOS in order to support + # static linking and avoid collisions, so unit tests link iODBC (${ODBCINST}) here to + # test ODBC API symbols. set(ODBC_SPI_IMPL_TEST_STATIC_LIBS arrow_odbc_spi_impl arrow_flight_testing_static - ${ARROW_TEST_STATIC_LINK_LIBS}) + ${ARROW_TEST_STATIC_LINK_LIBS} ${ODBCINST}) set(ODBC_SPI_IMPL_TEST_EXTRA_LIBS "") elseif(ARROW_FLIGHT_TEST_LINKAGE STREQUAL "static") set(ODBC_SPI_IMPL_TEST_STATIC_LIBS arrow_odbc_spi_impl arrow_flight_testing_static @@ -199,6 +202,7 @@ add_arrow_test(odbc_spi_impl_test accessors/string_array_accessor_test.cc accessors/time_array_accessor_test.cc accessors/timestamp_array_accessor_test.cc + config/configuration_test.cc flight_sql_connection_test.cc flight_sql_stream_chunk_buffer_test.cc parse_table_types_test.cc diff --git a/cpp/src/arrow/flight/sql/odbc/odbc_impl/config/configuration.cc b/cpp/src/arrow/flight/sql/odbc/odbc_impl/config/configuration.cc index d01b38184ab7..6ecc29b3077d 100644 --- a/cpp/src/arrow/flight/sql/odbc/odbc_impl/config/configuration.cc +++ b/cpp/src/arrow/flight/sql/odbc/odbc_impl/config/configuration.cc @@ -23,6 +23,7 @@ #include "arrow/flight/sql/odbc/odbc_impl/attribute_utils.h" #include "arrow/flight/sql/odbc/odbc_impl/util.h" #include "arrow/result.h" +#include "arrow/util/string.h" #include "arrow/util/utf8.h" #include @@ -69,19 +70,6 @@ std::string ReadDsnString(const std::string& dsn, const std::string_view& key, return result; } -void RemoveAllKnownKeys(std::vector& keys) { - // Remove all known DSN keys from the passed in set of keys, case insensitively. - keys.erase(std::remove_if(keys.begin(), keys.end(), - [&](auto& x) { - return std::find_if( - FlightSqlConnection::ALL_KEYS.begin(), - FlightSqlConnection::ALL_KEYS.end(), - [&](auto& s) { return boost::iequals(x, s); }) != - FlightSqlConnection::ALL_KEYS.end(); - }), - keys.end()); -} - std::vector ReadAllKeys(const std::string& dsn) { CONVERT_SQLWCHAR_STR(wdsn, dsn); @@ -172,10 +160,13 @@ void Configuration::LoadDsn(const std::string& dsn) { DEFAULT_DISABLE_CERT_VERIFICATION)); #endif // __APPLE__ - RemoveAllKnownKeys(customKeys); - for (auto key : customKeys) { - std::string_view key_sv(key); - Set(key, ReadDsnString(dsn, key_sv)); + // Read remaining keys in the DSN without overwriting keys that are read explicitly. + for (const auto& key : customKeys) { + if (arrow::internal::AsciiEqualsCaseInsensitive(key, FlightSqlConnection::DSN) || + arrow::internal::AsciiEqualsCaseInsensitive(key, FlightSqlConnection::DRIVER)) { + continue; + } + Emplace(key, ReadDsnString(dsn, key)); } } @@ -219,7 +210,7 @@ const Connection::ConnPropertyMap& Configuration::GetProperties() const { std::vector Configuration::GetCustomKeys() const { Connection::ConnPropertyMap copy_props(properties_); - for (auto& key : FlightSqlConnection::ALL_KEYS) { + for (auto& key : FlightSqlConnection::DSN_DIALOG_KEYS) { copy_props.erase(std::string(key)); } std::vector keys; diff --git a/cpp/src/arrow/flight/sql/odbc/odbc_impl/config/configuration.h b/cpp/src/arrow/flight/sql/odbc/odbc_impl/config/configuration.h index 66a267639976..da557005e6a1 100644 --- a/cpp/src/arrow/flight/sql/odbc/odbc_impl/config/configuration.h +++ b/cpp/src/arrow/flight/sql/odbc/odbc_impl/config/configuration.h @@ -62,6 +62,9 @@ class Configuration { */ const Connection::ConnPropertyMap& GetProperties() const; + /** + * Get the keys shown in "Advanced properties" table from Windows DSN Window. + */ std::vector GetCustomKeys() const; private: diff --git a/cpp/src/arrow/flight/sql/odbc/odbc_impl/config/configuration_test.cc b/cpp/src/arrow/flight/sql/odbc/odbc_impl/config/configuration_test.cc new file mode 100644 index 000000000000..b0e031ad3944 --- /dev/null +++ b/cpp/src/arrow/flight/sql/odbc/odbc_impl/config/configuration_test.cc @@ -0,0 +1,179 @@ +// 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. + +// flight_sql_connection.h needs to be included first due to conflicts with windows.h +#include "arrow/flight/sql/odbc/odbc_impl/flight_sql_connection.h" + +#include "arrow/flight/sql/odbc/odbc_impl/config/configuration.h" + +#include +#include +#include +#include + +#include "arrow/util/string.h" +#include "gtest/gtest.h" + +namespace arrow::flight::sql::odbc { +namespace config { + +namespace { + +bool ContainsKeyIgnoreCase(const std::vector& keys, + std::string_view key) { + for (const auto& candidate : keys) { + if (arrow::internal::AsciiEqualsCaseInsensitive(candidate, key)) { + return true; + } + } + return false; +} + +std::set ToSet(const std::vector& keys) { + return std::set(keys.begin(), keys.end()); +} + +// Known keys that do not have a dedicated field in the Windows DSN dialog. +const std::vector kKeysWithoutDialogField = { + FlightSqlConnection::STRING_COLUMN_LENGTH, FlightSqlConnection::USE_WIDE_CHAR, + FlightSqlConnection::CHUNK_BUFFER_CAPACITY, FlightSqlConnection::USER_ID}; + +std::vector KnownKeysWithoutDialogField() { + std::vector keys; + for (const auto& key : FlightSqlConnection::ALL_KEYS) { + if (!ContainsKeyIgnoreCase(FlightSqlConnection::DSN_DIALOG_KEYS, key)) { + keys.emplace_back(key); + } + } + return keys; +} + +void SetDialogKeys(Configuration& config) { + config.Set(FlightSqlConnection::DSN, "test dsn"); + config.Set(FlightSqlConnection::DRIVER, "test driver"); + config.Set(FlightSqlConnection::HOST, "localhost"); + config.Set(FlightSqlConnection::PORT, "32010"); + config.Set(FlightSqlConnection::TOKEN, "token"); + config.Set(FlightSqlConnection::UID, "user"); + config.Set(FlightSqlConnection::PWD, "password"); + config.Set(FlightSqlConnection::USE_ENCRYPTION, "true"); + config.Set(FlightSqlConnection::TRUSTED_CERTS, "/path/to/cert"); + config.Set(FlightSqlConnection::USE_SYSTEM_TRUST_STORE, "false"); + config.Set(FlightSqlConnection::DISABLE_CERTIFICATE_VERIFICATION, "false"); +} + +void SetKeysWithoutDialogField(Configuration& config) { + config.Set(FlightSqlConnection::STRING_COLUMN_LENGTH, "1024"); + config.Set(FlightSqlConnection::USE_WIDE_CHAR, "true"); + config.Set(FlightSqlConnection::CHUNK_BUFFER_CAPACITY, "10"); + config.Set(FlightSqlConnection::USER_ID, "user"); +} + +} // namespace + +TEST(DsnDialogKeysTest, DialogKeysAreKnownKeys) { + for (const auto& key : FlightSqlConnection::DSN_DIALOG_KEYS) { + EXPECT_TRUE(ContainsKeyIgnoreCase(FlightSqlConnection::ALL_KEYS, key)) + << key << " is in DSN_DIALOG_KEYS but not in ALL_KEYS"; + } +} + +TEST(DsnDialogKeysTest, KeysWithoutDialogFieldAreNotDialogKeys) { + for (const auto& key : kKeysWithoutDialogField) { + EXPECT_TRUE(ContainsKeyIgnoreCase(FlightSqlConnection::ALL_KEYS, key)) << key; + EXPECT_FALSE(ContainsKeyIgnoreCase(FlightSqlConnection::DSN_DIALOG_KEYS, key)) + << key << " has no field in the DSN dialog and must not be in DSN_DIALOG_KEYS"; + } +} + +// When a new key is added, put it in DSN_DIALOG_KEYS if it gets its own field +// in the Windows DSN dialog, or in kKeysWithoutDialogField when it +// is meant for "Advanced properties" table. +TEST(DsnDialogKeysTest, EveryKnownKeyIsClassified) { + for (const auto& key : FlightSqlConnection::ALL_KEYS) { + const bool in_dialog = + ContainsKeyIgnoreCase(FlightSqlConnection::DSN_DIALOG_KEYS, key); + const bool in_advanced = ContainsKeyIgnoreCase(kKeysWithoutDialogField, key); + EXPECT_NE(in_dialog, in_advanced) + << key << " must be in exactly one of DSN_DIALOG_KEYS and the advanced keys"; + } +} + +TEST(ConfigurationTest, TestHelpersSetEveryKnownKey) { + Configuration config; + SetDialogKeys(config); + SetKeysWithoutDialogField(config); + + for (const auto& key : FlightSqlConnection::ALL_KEYS) { + EXPECT_TRUE(config.IsSet(key)) << "Add a value for " << key << " to the test helpers"; + } +} + +TEST(ConfigurationTest, GetCustomKeysEmptyWhenOnlyDialogKeysSet) { + Configuration config; + SetDialogKeys(config); + + for (const auto& key : FlightSqlConnection::DSN_DIALOG_KEYS) { + EXPECT_TRUE(config.IsSet(key)) << "SetDialogKeys does not set " << key; + } + EXPECT_TRUE(config.GetCustomKeys().empty()); +} + +// Regression test for the DSN dialog silently dropping known keys that +// are meant for the "Advanced properties" table +TEST(ConfigurationTest, GetCustomKeysIncludesKeysWithoutDialogField) { + Configuration config; + SetDialogKeys(config); + SetKeysWithoutDialogField(config); + + const auto keys_without_dialog_field = KnownKeysWithoutDialogField(); + const std::set expected(keys_without_dialog_field.begin(), + keys_without_dialog_field.end()); + EXPECT_EQ(expected, ToSet(config.GetCustomKeys())); + EXPECT_FALSE(expected.empty()); +} + +TEST(ConfigurationTest, GetCustomKeysIncludesUnknownKeys) { + Configuration config; + config.Set(FlightSqlConnection::HOST, "localhost"); + config.Set(FlightSqlConnection::STRING_COLUMN_LENGTH, "1024"); + config.Set("MyCustomKey", "custom value"); + + const std::set expected = { + std::string(FlightSqlConnection::STRING_COLUMN_LENGTH), "MyCustomKey"}; + EXPECT_EQ(expected, ToSet(config.GetCustomKeys())); +} + +TEST(ConfigurationTest, GetCustomKeysExcludesDialogKeysCaseInsensitively) { + Configuration config; + SetDialogKeys(config); + SetKeysWithoutDialogField(config); + + Configuration upper_case_config; + for (const auto& [key, value] : config.GetProperties()) { + upper_case_config.Set(arrow::internal::AsciiToUpper(key), value); + } + + std::set expected; + for (const auto& key : KnownKeysWithoutDialogField()) { + expected.insert(arrow::internal::AsciiToUpper(key)); + } + EXPECT_EQ(expected, ToSet(upper_case_config.GetCustomKeys())); +} + +} // namespace config +} // namespace arrow::flight::sql::odbc diff --git a/cpp/src/arrow/flight/sql/odbc/odbc_impl/flight_sql_connection.cc b/cpp/src/arrow/flight/sql/odbc/odbc_impl/flight_sql_connection.cc index be1e605398d4..007a8ed09af1 100644 --- a/cpp/src/arrow/flight/sql/odbc/odbc_impl/flight_sql_connection.cc +++ b/cpp/src/arrow/flight/sql/odbc/odbc_impl/flight_sql_connection.cc @@ -67,6 +67,19 @@ const std::vector FlightSqlConnection::ALL_KEYS = { FlightSqlConnection::USE_WIDE_CHAR, FlightSqlConnection::CHUNK_BUFFER_CAPACITY}; +const std::vector FlightSqlConnection::DSN_DIALOG_KEYS = { + FlightSqlConnection::DSN, + FlightSqlConnection::DRIVER, + FlightSqlConnection::HOST, + FlightSqlConnection::PORT, + FlightSqlConnection::TOKEN, + FlightSqlConnection::UID, + FlightSqlConnection::PWD, + FlightSqlConnection::USE_ENCRYPTION, + FlightSqlConnection::TRUSTED_CERTS, + FlightSqlConnection::USE_SYSTEM_TRUST_STORE, + FlightSqlConnection::DISABLE_CERTIFICATE_VERIFICATION}; + namespace { #if _WIN32 || _WIN64 diff --git a/cpp/src/arrow/flight/sql/odbc/odbc_impl/flight_sql_connection.h b/cpp/src/arrow/flight/sql/odbc/odbc_impl/flight_sql_connection.h index b2b8ba2cd3bf..635c3f517351 100644 --- a/cpp/src/arrow/flight/sql/odbc/odbc_impl/flight_sql_connection.h +++ b/cpp/src/arrow/flight/sql/odbc/odbc_impl/flight_sql_connection.h @@ -52,7 +52,13 @@ class FlightSqlConnection : public Connection { void PopulateMetadataSettings(const Connection::ConnPropertyMap& conn_property_map); public: + /// \brief Known DSN keys supported by ODBC. static const std::vector ALL_KEYS; + /// \brief Keys that have a dedicated field in the Windows DSN configuration dialog. + /// + /// A subset of ALL_KEYS. + /// New key that gets a field in the Windows DSN UI must be added to DSN_DIALOG_KEYS. + static const std::vector DSN_DIALOG_KEYS; static constexpr std::string_view DSN = "dsn"; static constexpr std::string_view DRIVER = "driver"; static constexpr std::string_view HOST = "host"; diff --git a/cpp/src/arrow/flight/sql/odbc/tests/connection_test.cc b/cpp/src/arrow/flight/sql/odbc/tests/connection_test.cc index 05c0348fbe0c..e05ce742a485 100644 --- a/cpp/src/arrow/flight/sql/odbc/tests/connection_test.cc +++ b/cpp/src/arrow/flight/sql/odbc/tests/connection_test.cc @@ -23,6 +23,9 @@ #include #include +#include +#include + #include namespace arrow::flight::sql::odbc { @@ -284,6 +287,55 @@ TYPED_TEST(ConnectionHandleTest, TestSQLDriverConnectDsn) { << GetOdbcErrorMessage(SQL_HANDLE_DBC, this->conn); } +// StringColumnLength in the connection string should override the length reported for +// string columns. +TYPED_TEST(ConnectionHandleTest, TestSQLDriverConnectStringColumnLength) { + constexpr SQLULEN kStringColumnLength = 100; + std::string connect_str = this->GetConnectionString() + + ";StringColumnLength=" + std::to_string(kStringColumnLength); + ASSERT_OK_AND_ASSIGN(std::wstring wconnect_str, + arrow::util::UTF8ToWideString(connect_str)); + std::vector connect_str0(wconnect_str.begin(), wconnect_str.end()); + + SQLWCHAR out_str[kOdbcBufferSize] = {}; + SQLSMALLINT out_str_len; + ASSERT_EQ(SQL_SUCCESS, + SQLDriverConnect(this->conn, NULL, &connect_str0[0], + static_cast(connect_str0.size()), out_str, + kOdbcBufferSize, &out_str_len, SQL_DRIVER_NOPROMPT)) + << GetOdbcErrorMessage(SQL_HANDLE_DBC, this->conn); + + SQLHSTMT statement; + ASSERT_EQ(SQL_SUCCESS, SQLAllocHandle(SQL_HANDLE_STMT, this->conn, &statement)); + + ASSIGN_SQLWCHAR_ARR_AND_LEN(sql_query, + L"SELECT CAST('abc' AS VARCHAR(50)) AS varchar_str"); + ASSERT_EQ(SQL_SUCCESS, SQLExecDirect(statement, sql_query, sql_query_len)) + << GetOdbcErrorMessage(SQL_HANDLE_STMT, statement); + + constexpr SQLUSMALLINT kColumn = 1; + SQLWCHAR column_name[kOdbcBufferSize]; + SQLSMALLINT name_length = 0; + SQLSMALLINT data_type = 0; + SQLULEN column_size = 0; + SQLSMALLINT decimal_digits = 0; + SQLSMALLINT nullable = 0; + ASSERT_EQ(SQL_SUCCESS, + SQLDescribeCol(statement, kColumn, column_name, kOdbcBufferSize, &name_length, + &data_type, &column_size, &decimal_digits, &nullable)) + << GetOdbcErrorMessage(SQL_HANDLE_STMT, statement); + EXPECT_EQ(kStringColumnLength, column_size); + + SQLLEN length = 0; + ASSERT_EQ(SQL_SUCCESS, + SQLColAttribute(statement, kColumn, SQL_DESC_LENGTH, 0, 0, nullptr, &length)); + EXPECT_EQ(static_cast(kStringColumnLength), length); + + ASSERT_EQ(SQL_SUCCESS, SQLFreeHandle(SQL_HANDLE_STMT, statement)); + ASSERT_EQ(SQL_SUCCESS, SQLDisconnect(this->conn)) + << GetOdbcErrorMessage(SQL_HANDLE_DBC, this->conn); +} + TYPED_TEST(ConnectionHandleTest, TestSQLConnect) { // Connect string std::string connect_str = this->GetConnectionString(); @@ -605,4 +657,52 @@ TYPED_TEST(ConnectionTest, TestExplicitDescriptorsOutliveStatement) { EXPECT_EQ(SQL_SUCCESS, SQLFreeHandle(SQL_HANDLE_DESC, ard_descriptor)); } +// Every key in ALL_KEYS must survive LoadDsn, so a key added to ALL_KEYS without also +// being read by LoadDsn fails here. Exceptions are DSN and DRIVER keys. +TEST(DsnConfiguration, LoadDsnReadsAllKnownKeys) { + using config::Configuration; + + Connection::ConnPropertyMap properties = { + {std::string(FlightSqlConnection::DRIVER), "Apache Arrow Flight SQL ODBC Driver"}, + {std::string(FlightSqlConnection::HOST), "localhost"}, + {std::string(FlightSqlConnection::PORT), "32010"}, + {std::string(FlightSqlConnection::TOKEN), "token"}, + {std::string(FlightSqlConnection::UID), "user"}, + {std::string(FlightSqlConnection::USER_ID), "ole db user"}, + {std::string(FlightSqlConnection::PWD), "password"}, + {std::string(FlightSqlConnection::USE_ENCRYPTION), "false"}, + {std::string(FlightSqlConnection::TRUSTED_CERTS), "/path/to/cert"}, + {std::string(FlightSqlConnection::USE_SYSTEM_TRUST_STORE), "false"}, + {std::string(FlightSqlConnection::DISABLE_CERTIFICATE_VERIFICATION), "true"}, + {std::string(FlightSqlConnection::STRING_COLUMN_LENGTH), "2048"}, + {std::string(FlightSqlConnection::USE_WIDE_CHAR), "true"}, + {std::string(FlightSqlConnection::CHUNK_BUFFER_CAPACITY), "7"}, + }; + // Fails when a key is added to ALL_KEYS without a value above. + for (const auto& key : FlightSqlConnection::ALL_KEYS) { + if (key == FlightSqlConnection::DSN) { + continue; + } + EXPECT_EQ(1U, properties.count(key)) << "Add a value for " << key << " to this test"; + } + ASSERT_TRUE(WriteDSN(properties)); + + const std::string dsn(kTestDsn); + ASSERT_OK_AND_ASSIGN(std::wstring wdsn, arrow::util::UTF8ToWideString(dsn)); + + Configuration loaded; + loaded.LoadDsn(dsn); + + // Check before unregistering the DSN so a failed assertion does not leak it. + EXPECT_EQ(dsn, loaded.Get(FlightSqlConnection::DSN)); + for (const auto& [key, value] : properties) { + if (key == FlightSqlConnection::DRIVER) { + continue; + } + EXPECT_EQ(value, loaded.Get(key)) << key << " was not loaded from the DSN"; + } + + ASSERT_TRUE(UnregisterDsn(wdsn)); +} + } // namespace arrow::flight::sql::odbc