Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cpp/src/arrow/flight/sql/odbc/connection-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
6 changes: 5 additions & 1 deletion cpp/src/arrow/flight/sql/odbc/odbc_impl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
27 changes: 9 additions & 18 deletions cpp/src/arrow/flight/sql/odbc/odbc_impl/config/configuration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 <odbcinst.h>
Expand Down Expand Up @@ -69,19 +70,6 @@ std::string ReadDsnString(const std::string& dsn, const std::string_view& key,
return result;
}

void RemoveAllKnownKeys(std::vector<std::string>& 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<std::string> ReadAllKeys(const std::string& dsn) {
CONVERT_SQLWCHAR_STR(wdsn, dsn);

Expand Down Expand Up @@ -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));
}
}

Expand Down Expand Up @@ -219,7 +210,7 @@ const Connection::ConnPropertyMap& Configuration::GetProperties() const {

std::vector<std::string> 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<std::string> keys;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> GetCustomKeys() const;

private:
Expand Down
179 changes: 179 additions & 0 deletions cpp/src/arrow/flight/sql/odbc/odbc_impl/config/configuration_test.cc
Original file line number Diff line number Diff line change
@@ -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 <set>
#include <string>
#include <string_view>
#include <vector>

#include "arrow/util/string.h"
#include "gtest/gtest.h"

namespace arrow::flight::sql::odbc {
namespace config {

namespace {

bool ContainsKeyIgnoreCase(const std::vector<std::string_view>& keys,
std::string_view key) {
for (const auto& candidate : keys) {
if (arrow::internal::AsciiEqualsCaseInsensitive(candidate, key)) {
return true;
}
}
return false;
}

std::set<std::string> ToSet(const std::vector<std::string>& keys) {
return std::set<std::string>(keys.begin(), keys.end());
}

// Known keys that do not have a dedicated field in the Windows DSN dialog.
const std::vector<std::string_view> kKeysWithoutDialogField = {
FlightSqlConnection::STRING_COLUMN_LENGTH, FlightSqlConnection::USE_WIDE_CHAR,
FlightSqlConnection::CHUNK_BUFFER_CAPACITY, FlightSqlConnection::USER_ID};

std::vector<std::string> KnownKeysWithoutDialogField() {
std::vector<std::string> 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<std::string> 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<std::string> 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<std::string> 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
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,19 @@ const std::vector<std::string_view> FlightSqlConnection::ALL_KEYS = {
FlightSqlConnection::USE_WIDE_CHAR,
FlightSqlConnection::CHUNK_BUFFER_CAPACITY};

const std::vector<std::string_view> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string_view> 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<std::string_view> DSN_DIALOG_KEYS;
static constexpr std::string_view DSN = "dsn";
static constexpr std::string_view DRIVER = "driver";
static constexpr std::string_view HOST = "host";
Expand Down
Loading
Loading