Skip to content

Commit ab159f4

Browse files
committed
Use emplace properly
1 parent 6883057 commit ab159f4

File tree

4 files changed

+181
-26
lines changed

4 files changed

+181
-26
lines changed

cpp/src/arrow/flight/sql/odbc/flight_sql/config/configuration.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ void Configuration::Set(const std::string_view& key, const std::string& value) {
160160
void Configuration::Emplace(const std::string_view& key, std::string&& value) {
161161
const std::string copy = boost::trim_copy(value);
162162
if (!copy.empty()) {
163-
this->properties[std::string(key)] = value;
163+
this->properties.emplace(
164+
std::make_pair(std::move(std::string(key)), std::move(value)));
164165
}
165166
}
166167

cpp/src/arrow/flight/sql/odbc/tests/connection_test.cc

Lines changed: 165 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -441,14 +441,11 @@ TEST(SQLConnect, TestSQLConnect) {
441441
// Connect string
442442
ASSERT_OK_AND_ASSIGN(std::string connect_str,
443443
arrow::internal::GetEnvVar(TEST_CONNECT_STR));
444-
ASSERT_OK_AND_ASSIGN(std::wstring wconnect_str,
445-
arrow::util::UTF8ToWideString(connect_str));
446-
std::vector<SQLWCHAR> connect_str0(wconnect_str.begin(), wconnect_str.end());
447444

448445
// Write connection string content into a DSN,
449446
// must succeed before continuing
450-
std::string uid, pwd;
451-
ASSERT_TRUE(writeDSN(connect_str, uid, pwd));
447+
std::string uid(""), pwd("");
448+
ASSERT_TRUE(writeDSN(connect_str));
452449

453450
std::string dsn(TEST_DSN);
454451
ASSERT_OK_AND_ASSIGN(std::wstring wdsn, arrow::util::UTF8ToWideString(dsn));
@@ -492,7 +489,7 @@ TEST(SQLConnect, TestSQLConnect) {
492489
EXPECT_TRUE(ret == SQL_SUCCESS);
493490
}
494491

495-
TEST(SQLConnect, TestSQLConnectWrongUid) {
492+
TEST(SQLConnect, TestSQLConnectInputUidPwd) {
496493
// ODBC Environment
497494
SQLHENV env;
498495
SQLHDBC conn;
@@ -514,17 +511,98 @@ TEST(SQLConnect, TestSQLConnectWrongUid) {
514511
// Connect string
515512
ASSERT_OK_AND_ASSIGN(std::string connect_str,
516513
arrow::internal::GetEnvVar(TEST_CONNECT_STR));
517-
ASSERT_OK_AND_ASSIGN(std::wstring wconnect_str,
518-
arrow::util::UTF8ToWideString(connect_str));
519-
std::vector<SQLWCHAR> connect_str0(wconnect_str.begin(), wconnect_str.end());
520514

521-
// Write connection string content into a DSN,
515+
// Retrieve valid uid and pwd
516+
Connection::ConnPropertyMap properties;
517+
ODBC::ODBCConnection::getPropertiesFromConnString(connect_str, properties);
518+
std::string uid_key("uid");
519+
std::string pwd_key("pwd");
520+
std::string uid = properties[uid_key];
521+
std::string pwd = properties[pwd_key];
522+
523+
// Write connection string content without uid and pwd into a DSN,
522524
// must succeed before continuing
523-
std::string uid, pwd;
524-
ASSERT_TRUE(writeDSN(connect_str, uid, pwd));
525+
properties.erase(uid_key);
526+
properties.erase(pwd_key);
527+
ASSERT_TRUE(writeDSN(properties));
525528

526-
// Make invalid uid
527-
uid += std::string("_invalid");
529+
std::string dsn(TEST_DSN);
530+
ASSERT_OK_AND_ASSIGN(std::wstring wdsn, arrow::util::UTF8ToWideString(dsn));
531+
ASSERT_OK_AND_ASSIGN(std::wstring wuid, arrow::util::UTF8ToWideString(uid));
532+
ASSERT_OK_AND_ASSIGN(std::wstring wpwd, arrow::util::UTF8ToWideString(pwd));
533+
std::vector<SQLWCHAR> dsn0(wdsn.begin(), wdsn.end());
534+
std::vector<SQLWCHAR> uid0(wuid.begin(), wuid.end());
535+
std::vector<SQLWCHAR> pwd0(wpwd.begin(), wpwd.end());
536+
537+
// Connecting to ODBC server.
538+
ret = SQLConnect(conn, dsn0.data(), static_cast<SQLSMALLINT>(dsn0.size()), uid0.data(),
539+
static_cast<SQLSMALLINT>(uid0.size()), pwd0.data(),
540+
static_cast<SQLSMALLINT>(pwd0.size()));
541+
542+
if (ret != SQL_SUCCESS) {
543+
std::cerr << GetOdbcErrorMessage(SQL_HANDLE_DBC, conn) << std::endl;
544+
}
545+
546+
EXPECT_TRUE(ret == SQL_SUCCESS);
547+
548+
// Remove DSN
549+
EXPECT_TRUE(UnregisterDsn(dsn));
550+
551+
// Disconnect from ODBC
552+
ret = SQLDisconnect(conn);
553+
554+
if (ret != SQL_SUCCESS) {
555+
std::cerr << GetOdbcErrorMessage(SQL_HANDLE_DBC, conn) << std::endl;
556+
}
557+
558+
EXPECT_TRUE(ret == SQL_SUCCESS);
559+
560+
// Free connection handle
561+
ret = SQLFreeHandle(SQL_HANDLE_DBC, conn);
562+
563+
EXPECT_TRUE(ret == SQL_SUCCESS);
564+
565+
// Free environment handle
566+
ret = SQLFreeHandle(SQL_HANDLE_ENV, env);
567+
568+
EXPECT_TRUE(ret == SQL_SUCCESS);
569+
}
570+
571+
TEST(SQLConnect, TestSQLConnectInvalidUid) {
572+
// ODBC Environment
573+
SQLHENV env;
574+
SQLHDBC conn;
575+
576+
// Allocate an environment handle
577+
SQLRETURN ret = SQLAllocEnv(&env);
578+
579+
EXPECT_TRUE(ret == SQL_SUCCESS);
580+
581+
ret = SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
582+
583+
EXPECT_TRUE(ret == SQL_SUCCESS);
584+
585+
// Allocate a connection using alloc handle
586+
ret = SQLAllocHandle(SQL_HANDLE_DBC, env, &conn);
587+
588+
EXPECT_TRUE(ret == SQL_SUCCESS);
589+
590+
// Connect string
591+
ASSERT_OK_AND_ASSIGN(std::string connect_str,
592+
arrow::internal::GetEnvVar(TEST_CONNECT_STR));
593+
594+
// Retrieve valid uid and pwd
595+
Connection::ConnPropertyMap properties;
596+
ODBC::ODBCConnection::getPropertiesFromConnString(connect_str, properties);
597+
std::string uid = properties[std::string("uid")];
598+
std::string pwd = properties[std::string("pwd")];
599+
600+
// Append invalid uid to connection string
601+
connect_str += std::string("uid=non_existent_id;");
602+
603+
// Write connection string content into a DSN,
604+
// must succeed before continuing
605+
ASSERT_TRUE(writeDSN(connect_str));
528606

529607
std::string dsn(TEST_DSN);
530608
ASSERT_OK_AND_ASSIGN(std::wstring wdsn, arrow::util::UTF8ToWideString(dsn));
@@ -539,6 +617,8 @@ TEST(SQLConnect, TestSQLConnectWrongUid) {
539617
static_cast<SQLSMALLINT>(uid0.size()), pwd0.data(),
540618
static_cast<SQLSMALLINT>(pwd0.size()));
541619

620+
// UID specified in DSN will take precedence,
621+
// so connection still fails despite passing valid uid in SQLConnect call
542622
EXPECT_TRUE(ret == SQL_ERROR);
543623

544624
// TODO uncomment this check when SQLGetDiagRec is implemented
@@ -558,6 +638,77 @@ TEST(SQLConnect, TestSQLConnectWrongUid) {
558638
EXPECT_TRUE(ret == SQL_SUCCESS);
559639
}
560640

641+
TEST(SQLConnect, TestSQLConnectDSNPrecedence) {
642+
// ODBC Environment
643+
SQLHENV env;
644+
SQLHDBC conn;
645+
646+
// Allocate an environment handle
647+
SQLRETURN ret = SQLAllocEnv(&env);
648+
649+
EXPECT_TRUE(ret == SQL_SUCCESS);
650+
651+
ret = SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
652+
653+
EXPECT_TRUE(ret == SQL_SUCCESS);
654+
655+
// Allocate a connection using alloc handle
656+
ret = SQLAllocHandle(SQL_HANDLE_DBC, env, &conn);
657+
658+
EXPECT_TRUE(ret == SQL_SUCCESS);
659+
660+
// Connect string
661+
ASSERT_OK_AND_ASSIGN(std::string connect_str,
662+
arrow::internal::GetEnvVar(TEST_CONNECT_STR));
663+
664+
// Write connection string content into a DSN,
665+
// must succeed before continuing
666+
667+
// Pass incorrect uid and password to SQLConnect, they will be ignored
668+
std::string uid("non_existent_id"), pwd("non_existent_password");
669+
ASSERT_TRUE(writeDSN(connect_str));
670+
671+
std::string dsn(TEST_DSN);
672+
ASSERT_OK_AND_ASSIGN(std::wstring wdsn, arrow::util::UTF8ToWideString(dsn));
673+
ASSERT_OK_AND_ASSIGN(std::wstring wuid, arrow::util::UTF8ToWideString(uid));
674+
ASSERT_OK_AND_ASSIGN(std::wstring wpwd, arrow::util::UTF8ToWideString(pwd));
675+
std::vector<SQLWCHAR> dsn0(wdsn.begin(), wdsn.end());
676+
std::vector<SQLWCHAR> uid0(wuid.begin(), wuid.end());
677+
std::vector<SQLWCHAR> pwd0(wpwd.begin(), wpwd.end());
678+
679+
// Connecting to ODBC server.
680+
ret = SQLConnect(conn, dsn0.data(), static_cast<SQLSMALLINT>(dsn0.size()), uid0.data(),
681+
static_cast<SQLSMALLINT>(uid0.size()), pwd0.data(),
682+
static_cast<SQLSMALLINT>(pwd0.size()));
683+
684+
if (ret != SQL_SUCCESS) {
685+
std::cerr << GetOdbcErrorMessage(SQL_HANDLE_DBC, conn) << std::endl;
686+
}
687+
688+
EXPECT_TRUE(ret == SQL_SUCCESS);
689+
690+
// Remove DSN
691+
EXPECT_TRUE(UnregisterDsn(dsn));
692+
693+
// Disconnect from ODBC
694+
ret = SQLDisconnect(conn);
695+
696+
if (ret != SQL_SUCCESS) {
697+
std::cerr << GetOdbcErrorMessage(SQL_HANDLE_DBC, conn) << std::endl;
698+
}
699+
700+
EXPECT_TRUE(ret == SQL_SUCCESS);
701+
702+
// Free connection handle
703+
ret = SQLFreeHandle(SQL_HANDLE_DBC, conn);
704+
705+
EXPECT_TRUE(ret == SQL_SUCCESS);
706+
707+
// Free environment handle
708+
ret = SQLFreeHandle(SQL_HANDLE_ENV, env);
709+
710+
EXPECT_TRUE(ret == SQL_SUCCESS);
711+
}
561712
} // namespace integration_tests
562713
} // namespace odbc
563714
} // namespace flight

cpp/src/arrow/flight/sql/odbc/tests/odbc_test_suite.cc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,26 +142,26 @@ std::string GetOdbcErrorMessage(SQLSMALLINT handle_type, SQLHANDLE handle) {
142142
return res;
143143
}
144144

145-
bool writeDSN(std::string connection_str, std::string& uid, std::string& pwd) {
145+
bool writeDSN(std::string connection_str) {
146+
Connection::ConnPropertyMap properties;
147+
148+
ODBC::ODBCConnection::getPropertiesFromConnString(connection_str, properties);
149+
return writeDSN(properties);
150+
}
151+
152+
bool writeDSN(Connection::ConnPropertyMap properties) {
146153
using driver::flight_sql::FlightSqlConnection;
147154
using driver::flight_sql::config::Configuration;
148155
using driver::odbcabstraction::Connection;
149156
using ODBC::ODBCConnection;
150157

151-
Connection::ConnPropertyMap properties;
152-
153-
ODBC::ODBCConnection::getPropertiesFromConnString(connection_str, properties);
154-
155158
Configuration config;
156159
config.Set(FlightSqlConnection::DSN, std::string(TEST_DSN));
157160

158161
for (const auto& [key, value] : properties) {
159162
config.Set(key, value);
160163
}
161164

162-
uid = config.Get(FlightSqlConnection::UID);
163-
pwd = config.Get(FlightSqlConnection::PWD);
164-
165165
std::string driver = config.Get(FlightSqlConnection::DRIVER);
166166

167167
return RegisterDsn(config, driver.c_str());

cpp/src/arrow/flight/sql/odbc/tests/odbc_test_suite.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,13 @@ void VerifyOdbcErrorState(SQLSMALLINT handle_type, SQLHANDLE handle,
7777

7878
/// \brief Write connection string into DSN
7979
/// \param[in] connection_str the connection string.
80-
/// \param[out] uid uid from connection string.
81-
/// \param[out] pwd pwd from connection string.
8280
/// \return true on success
83-
bool writeDSN(std::string connection_str, std::string& uid, std::string& pwd);
81+
bool writeDSN(std::string connection_str);
82+
83+
/// \brief Write properties map into DSN
84+
/// \param[in] properties map.
85+
/// \return true on success
86+
bool writeDSN(Connection::ConnPropertyMap properties);
8487
} // namespace integration_tests
8588
} // namespace odbc
8689
} // namespace flight

0 commit comments

Comments
 (0)