Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 20 additions & 2 deletions cpp/src/arrow/flight/sql/odbc/odbc_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,21 @@ SQLRETURN SQLSetConnectAttr(SQLHDBC conn, SQLINTEGER attr, SQLPOINTER valuePtr,
});
}

// Load properties from the given DSN. The properties loaded do _not_ overwrite existing
// entries in the properties.
void loadPropertiesFromDSN(const std::string& dsn,
Connection::ConnPropertyMap& properties) {
driver::flight_sql::config::Configuration config;
config.LoadDsn(dsn);
Connection::ConnPropertyMap dsnProperties = config.GetProperties();
for (auto& [key, value] : dsnProperties) {
auto propIter = properties.find(key);
if (propIter == properties.end()) {
properties.emplace(std::make_pair(std::move(key), std::move(value)));
}
}
}

SQLRETURN SQLDriverConnect(SQLHDBC conn, SQLHWND windowHandle,
SQLWCHAR* inConnectionString,
SQLSMALLINT inConnectionStringLen,
Expand Down Expand Up @@ -782,8 +797,11 @@ SQLRETURN SQLDriverConnect(SQLHDBC conn, SQLHWND windowHandle,
std::string connection_string =
ODBC::SqlWcharToString(inConnectionString, inConnectionStringLen);
Connection::ConnPropertyMap properties;
std::string dsn =
ODBCConnection::getPropertiesFromConnString(connection_string, properties);
std::string dsn = ODBCConnection::getDsnIfExists(connection_string);
if (!dsn.empty()) {
loadPropertiesFromDSN(dsn, properties);
}
ODBCConnection::getPropertiesFromConnString(connection_string, properties);

std::vector<std::string_view> missing_properties;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ class ODBCConnection : public ODBCHandle<ODBCConnection> {
inline bool IsOdbc2Connection() const { return m_is2xConnection; }

/// @return the DSN or empty string if Driver was used.
static std::string getPropertiesFromConnString(
static std::string getDsnIfExists(const std::string& connStr);

/// Read properties from connection string, but does not read values from DSN
static void getPropertiesFromConnString(
const std::string& connStr,
driver::odbcabstraction::Connection::ConnPropertyMap& properties);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,6 @@ namespace {
// built statically.
const boost::xpressive::sregex CONNECTION_STR_REGEX(
boost::xpressive::sregex::compile("([^=;]+)=({.+}|[^;]+|[^;])"));

// Load properties from the given DSN. The properties loaded do _not_ overwrite existing
// entries in the properties.
void loadPropertiesFromDSN(const std::string& dsn,
Connection::ConnPropertyMap& properties) {
driver::flight_sql::config::Configuration config;
config.LoadDsn(dsn);
Connection::ConnPropertyMap dsnProperties = config.GetProperties();
for (auto& [key, value] : dsnProperties) {
auto propIter = properties.find(key);
if (propIter == properties.end()) {
properties.emplace(std::make_pair(std::move(key), std::move(value)));
}
}
}

} // namespace

// Public
Expand Down Expand Up @@ -696,46 +680,64 @@ void ODBCConnection::dropDescriptor(ODBCDescriptor* desc) {

// Public Static
// ===================================================================================
std::string ODBCConnection::getPropertiesFromConnString(
const std::string& connStr, Connection::ConnPropertyMap& properties) {
std::string ODBCConnection::getDsnIfExists(const std::string& connStr) {
Comment thread
alinaliBQ marked this conversation as resolved.
const int groups[] = {1, 2}; // CONNECTION_STR_REGEX has two groups. key: 1, value: 2
boost::xpressive::sregex_token_iterator regexIter(connStr.begin(), connStr.end(),
CONNECTION_STR_REGEX, groups),
boost::xpressive::sregex_token_iterator regex_iter(connStr.begin(), connStr.end(),
CONNECTION_STR_REGEX, groups),
end;

bool isDsnFirst = false;
bool isDriverFirst = false;
std::string dsn;
for (auto it = regexIter; end != regexIter; ++regexIter) {
std::string key = *regexIter;
std::string value = *++regexIter;
bool dsn_first = false;
bool driver_first = false;

@alinaliBQ alinaliBQ Sep 18, 2025

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed variable name to fit arrow convention; but I didn't change variables everywhere though, as updating the variables are for a different ticket

std::string dsn("");
for (auto it = regex_iter; end != regex_iter; ++regex_iter) {
std::string key = *regex_iter;
std::string value = *++regex_iter;

// If the DSN shows up before driver key, load settings from the DSN.
Comment thread
alinaliBQ marked this conversation as resolved.
Outdated
// Only load values from the DSN once regardless of how many times the DSN
// key shows up.
if (boost::iequals(key, "DSN")) {
Comment thread
alinaliBQ marked this conversation as resolved.
Outdated
if (!isDriverFirst) {
if (!isDsnFirst) {
isDsnFirst = true;
loadPropertiesFromDSN(value, properties);
if (!driver_first) {
if (!dsn_first) {
dsn_first = true;
dsn.swap(value);
return dsn;
}
}
continue;
} else if (boost::iequals(key, "Driver")) {
if (!isDsnFirst) {
isDriverFirst = true;
if (!dsn_first) {
driver_first = true;
return dsn;
}
}

// Strip wrapping curly braces.
if (value.size() >= 2 && value[0] == '{' && value[value.size() - 1] == '}') {
value = value.substr(1, value.size() - 2);
}
}
return dsn;
}

void ODBCConnection::getPropertiesFromConnString(
const std::string& connStr, Connection::ConnPropertyMap& properties) {
const int groups[] = {1, 2}; // CONNECTION_STR_REGEX has two groups. key: 1, value: 2
boost::xpressive::sregex_token_iterator regex_iter(connStr.begin(), connStr.end(),
CONNECTION_STR_REGEX, groups),
end;

for (auto it = regex_iter; end != regex_iter; ++regex_iter) {
std::string key = *regex_iter;
std::string value = *++regex_iter;

// Strip wrapping curly braces.
if (value.size() >= 2 && value[0] == '{' && value[value.size() - 1] == '}') {
value = value.substr(1, value.size() - 2);
}

// Overwrite the existing value. Later copies of the key take precedence,
// including over entries in the DSN.
properties[key] = std::move(value);
}
return dsn;
}
Loading