lidavidm commented on code in PR #47788: URL: https://github.com/apache/arrow/pull/47788#discussion_r2443555001
########## cpp/src/arrow/flight/sql/odbc/tests/odbc_test_suite.h: ########## @@ -0,0 +1,237 @@ +// 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/testing/gtest_util.h" +#include "arrow/util/io_util.h" +#include "arrow/util/utf8.h" + +#include "arrow/flight/server_middleware.h" +#include "arrow/flight/sql/client.h" +#include "arrow/flight/sql/example/sqlite_server.h" +#include "arrow/flight/sql/odbc/odbc_impl/encoding_utils.h" + +#ifdef _WIN32 +# include <windows.h> +#endif + +#include <sql.h> +#include <sqltypes.h> +#include <sqlucode.h> + +#include <type_traits> + +#include "arrow/flight/sql/odbc/odbc_impl/odbc_connection.h" + +// For DSN registration +#include "arrow/flight/sql/odbc/odbc_impl/system_dsn.h" + +#define TEST_CONNECT_STR "ARROW_FLIGHT_SQL_ODBC_CONN" +#define TEST_DSN "Apache Arrow Flight SQL Test DSN" + +namespace arrow::flight::sql::odbc { + +class FlightSQLODBCRemoteTestBase : public ::testing::Test { + public: + /// \brief Allocate environment and connection handles + void AllocEnvConnHandles(SQLINTEGER odbc_ver = SQL_OV_ODBC3); + /// \brief Connect to Arrow Flight SQL server using connection string defined in + /// environment variable "ARROW_FLIGHT_SQL_ODBC_CONN", allocate statement handle. + /// Connects using ODBC Ver 3 by default + void Connect(SQLINTEGER odbc_ver = SQL_OV_ODBC3); + /// \brief Connect to Arrow Flight SQL server using connection string + void ConnectWithString(std::string connection_str); + /// \brief Disconnect from server + void Disconnect(); + /// \brief Get connection string from environment variable "ARROW_FLIGHT_SQL_ODBC_CONN" + std::string virtual GetConnectionString(); + /// \brief Get invalid connection string based on connection string defined in + /// environment variable "ARROW_FLIGHT_SQL_ODBC_CONN" + std::string virtual GetInvalidConnectionString(); + /// \brief Return a SQL query that selects all data types + std::wstring virtual GetQueryAllDataTypes(); + + /** ODBC Environment. */ + SQLHENV env = 0; + + /** ODBC Connect. */ + SQLHDBC conn = 0; + + /** ODBC Statement. */ + SQLHSTMT stmt = 0; + + protected: + void SetUp() override; +}; + +static constexpr std::string_view authorization_header = "authorization"; +static constexpr std::string_view bearer_prefix = "Bearer "; +static constexpr std::string_view test_token = "t0k3n"; + +std::string FindTokenInCallHeaders(const CallHeaders& incoming_headers); + +// A server middleware for validating incoming bearer header authentication. +class MockServerMiddleware : public ServerMiddleware { + public: + explicit MockServerMiddleware(const CallHeaders& incoming_headers, bool* is_valid) + : is_valid_(is_valid) { + incoming_headers_ = incoming_headers; + } + + void SendingHeaders(AddCallHeaders* outgoing_headers) override; + + void CallCompleted(const Status& status) override {} + + std::string name() const override { return "MockServerMiddleware"; } + + private: + CallHeaders incoming_headers_; + bool* is_valid_; +}; + +// Factory for base64 header authentication testing. +class MockServerMiddlewareFactory : public ServerMiddlewareFactory { + public: + MockServerMiddlewareFactory() : is_valid_(false) {} + + Status StartCall(const CallInfo& info, const ServerCallContext& context, + std::shared_ptr<ServerMiddleware>* middleware) override; + + private: + bool is_valid_; +}; + +class FlightSQLODBCMockTestBase : public FlightSQLODBCRemoteTestBase { + // Sets up a mock server for each test case + public: + /// \brief Get connection string for mock server + std::string GetConnectionString() override; + /// \brief Get invalid connection string for mock server + std::string GetInvalidConnectionString() override; + /// \brief Return a SQL query that selects all data types + std::wstring GetQueryAllDataTypes() override; + + /// \brief Run a SQL query to create default table for table test cases + void CreateTestTables(); + + /// \brief run a SQL query to create a table with all data types + void CreateTableAllDataType(); + /// \brief run a SQL query to create a table with unicode name + void CreateUnicodeTable(); + + int port; + + protected: + void SetUp() override; + + void TearDown() override; + + private: + std::shared_ptr<arrow::flight::sql::example::SQLiteFlightSqlServer> server_; +}; + +template <typename T> +class FlightSQLODBCTestBase : public T { + public: + using List = std::list<T>; +}; + +using TestTypes = + ::testing::Types<FlightSQLODBCMockTestBase, FlightSQLODBCRemoteTestBase>; +TYPED_TEST_SUITE(FlightSQLODBCTestBase, TestTypes); + +/** ODBC read buffer size. */ +enum { ODBC_BUFFER_SIZE = 1024 }; + +/// Compare ConnPropertyMap, key value is case-insensitive +bool CompareConnPropertyMap(Connection::ConnPropertyMap map1, + Connection::ConnPropertyMap map2); + +/// Get error message from ODBC driver using SQLGetDiagRec +std::string GetOdbcErrorMessage(SQLSMALLINT handle_type, SQLHANDLE handle); + +static constexpr std::string_view error_state_01004 = "01004"; +static constexpr std::string_view error_state_01S07 = "01S07"; +static constexpr std::string_view error_state_01S02 = "01S02"; +static constexpr std::string_view error_state_07009 = "07009"; +static constexpr std::string_view error_state_08003 = "08003"; +static constexpr std::string_view error_state_22002 = "22002"; +static constexpr std::string_view error_state_24000 = "24000"; +static constexpr std::string_view error_state_28000 = "28000"; +static constexpr std::string_view error_state_HY000 = "HY000"; +static constexpr std::string_view error_state_HY004 = "HY004"; +static constexpr std::string_view error_state_HY009 = "HY009"; +static constexpr std::string_view error_state_HY010 = "HY010"; +static constexpr std::string_view error_state_HY017 = "HY017"; +static constexpr std::string_view error_state_HY024 = "HY024"; +static constexpr std::string_view error_state_HY090 = "HY090"; +static constexpr std::string_view error_state_HY091 = "HY091"; +static constexpr std::string_view error_state_HY092 = "HY092"; +static constexpr std::string_view error_state_HY106 = "HY106"; +static constexpr std::string_view error_state_HY114 = "HY114"; +static constexpr std::string_view error_state_HY118 = "HY118"; +static constexpr std::string_view error_state_HYC00 = "HYC00"; +static constexpr std::string_view error_state_S1004 = "S1004"; Review Comment: ditto here ########## cpp/src/arrow/flight/sql/odbc/odbc_impl/system_dsn.h: ########## @@ -0,0 +1,68 @@ +// 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. + +// platform.h includes windows.h, so it needs to be included first +#include "arrow/flight/sql/odbc/odbc_impl/platform.h" + +#include "arrow/flight/sql/odbc/odbc_impl/config/configuration.h" + +namespace arrow::flight::sql::odbc { + +using config::Configuration; + +#if defined _WIN32 || defined _WIN64 Review Comment: nit: only need to test _WIN32. ########## cpp/src/arrow/flight/sql/odbc/tests/odbc_test_suite.h: ########## @@ -0,0 +1,237 @@ +// 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/testing/gtest_util.h" +#include "arrow/util/io_util.h" +#include "arrow/util/utf8.h" + +#include "arrow/flight/server_middleware.h" +#include "arrow/flight/sql/client.h" +#include "arrow/flight/sql/example/sqlite_server.h" +#include "arrow/flight/sql/odbc/odbc_impl/encoding_utils.h" + +#ifdef _WIN32 +# include <windows.h> +#endif + +#include <sql.h> +#include <sqltypes.h> +#include <sqlucode.h> + +#include <type_traits> + +#include "arrow/flight/sql/odbc/odbc_impl/odbc_connection.h" + +// For DSN registration +#include "arrow/flight/sql/odbc/odbc_impl/system_dsn.h" + +#define TEST_CONNECT_STR "ARROW_FLIGHT_SQL_ODBC_CONN" +#define TEST_DSN "Apache Arrow Flight SQL Test DSN" + +namespace arrow::flight::sql::odbc { + +class FlightSQLODBCRemoteTestBase : public ::testing::Test { + public: + /// \brief Allocate environment and connection handles + void AllocEnvConnHandles(SQLINTEGER odbc_ver = SQL_OV_ODBC3); + /// \brief Connect to Arrow Flight SQL server using connection string defined in + /// environment variable "ARROW_FLIGHT_SQL_ODBC_CONN", allocate statement handle. + /// Connects using ODBC Ver 3 by default + void Connect(SQLINTEGER odbc_ver = SQL_OV_ODBC3); + /// \brief Connect to Arrow Flight SQL server using connection string + void ConnectWithString(std::string connection_str); + /// \brief Disconnect from server + void Disconnect(); + /// \brief Get connection string from environment variable "ARROW_FLIGHT_SQL_ODBC_CONN" + std::string virtual GetConnectionString(); + /// \brief Get invalid connection string based on connection string defined in + /// environment variable "ARROW_FLIGHT_SQL_ODBC_CONN" + std::string virtual GetInvalidConnectionString(); + /// \brief Return a SQL query that selects all data types + std::wstring virtual GetQueryAllDataTypes(); + + /** ODBC Environment. */ + SQLHENV env = 0; + + /** ODBC Connect. */ + SQLHDBC conn = 0; + + /** ODBC Statement. */ + SQLHSTMT stmt = 0; + + protected: + void SetUp() override; +}; + +static constexpr std::string_view authorization_header = "authorization"; +static constexpr std::string_view bearer_prefix = "Bearer "; +static constexpr std::string_view test_token = "t0k3n"; Review Comment: nit: should be `kAuthorizationHeader` etc. https://google.github.io/styleguide/cppguide.html#Constant_Names ########## cpp/src/arrow/flight/sql/odbc/tests/odbc_test_suite.cc: ########## @@ -0,0 +1,466 @@ +// 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. + +// For DSN registration. flight_sql_connection.h needs to included first due to conflicts +// with windows.h +#include "arrow/flight/sql/odbc/odbc_impl/flight_sql_connection.h" + +#include "arrow/flight/sql/odbc/tests/odbc_test_suite.h" + +// For DSN registration +#include "arrow/flight/sql/odbc/odbc_impl/config/configuration.h" +#include "arrow/flight/sql/odbc/odbc_impl/encoding_utils.h" +#include "arrow/flight/sql/odbc/odbc_impl/odbc_connection.h" + +namespace arrow::flight::sql::odbc { + +void FlightSQLODBCRemoteTestBase::AllocEnvConnHandles(SQLINTEGER odbc_ver) { + // Allocate an environment handle + ASSERT_EQ(SQL_SUCCESS, SQLAllocEnv(&env)); + + ASSERT_EQ( + SQL_SUCCESS, + SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, + reinterpret_cast<SQLPOINTER>(static_cast<intptr_t>(odbc_ver)), 0)); + + // Allocate a connection using alloc handle + ASSERT_EQ(SQL_SUCCESS, SQLAllocHandle(SQL_HANDLE_DBC, env, &conn)); +} + +void FlightSQLODBCRemoteTestBase::Connect(SQLINTEGER odbc_ver) { + ASSERT_NO_FATAL_FAILURE(AllocEnvConnHandles(odbc_ver)); + std::string connect_str = GetConnectionString(); + ASSERT_NO_FATAL_FAILURE(ConnectWithString(connect_str)); +} + +void FlightSQLODBCRemoteTestBase::ConnectWithString(std::string connect_str) { + // Connect string + std::vector<SQLWCHAR> connect_str0(connect_str.begin(), connect_str.end()); + + SQLWCHAR out_str[ODBC_BUFFER_SIZE]; + SQLSMALLINT out_str_len; + + // Connecting to ODBC server. + ASSERT_EQ(SQL_SUCCESS, + SQLDriverConnect(conn, NULL, &connect_str0[0], + static_cast<SQLSMALLINT>(connect_str0.size()), out_str, + ODBC_BUFFER_SIZE, &out_str_len, SQL_DRIVER_NOPROMPT)) + << GetOdbcErrorMessage(SQL_HANDLE_DBC, conn); + + // Allocate a statement using alloc handle + ASSERT_EQ(SQL_SUCCESS, SQLAllocHandle(SQL_HANDLE_STMT, conn, &stmt)); +} + +void FlightSQLODBCRemoteTestBase::Disconnect() { + // Close statement + EXPECT_EQ(SQL_SUCCESS, SQLFreeHandle(SQL_HANDLE_STMT, stmt)); + + // Disconnect from ODBC + EXPECT_EQ(SQL_SUCCESS, SQLDisconnect(conn)) + << GetOdbcErrorMessage(SQL_HANDLE_DBC, conn); + + // Free connection handle + EXPECT_EQ(SQL_SUCCESS, SQLFreeHandle(SQL_HANDLE_DBC, conn)); + + // Free environment handle + EXPECT_EQ(SQL_SUCCESS, SQLFreeHandle(SQL_HANDLE_ENV, env)); +} + +std::string FlightSQLODBCRemoteTestBase::GetConnectionString() { + std::string connect_str = arrow::internal::GetEnvVar(TEST_CONNECT_STR).ValueOrDie(); + return connect_str; +} + +std::string FlightSQLODBCRemoteTestBase::GetInvalidConnectionString() { + std::string connect_str = GetConnectionString(); + // Append invalid uid to connection string + connect_str += std::string("uid=non_existent_id;"); + return connect_str; +} + +std::wstring FlightSQLODBCRemoteTestBase::GetQueryAllDataTypes() { + std::wstring wsql = + LR"( SELECT + -- Numeric types + -128 as stiny_int_min, 127 as stiny_int_max, + 0 as utiny_int_min, 255 as utiny_int_max, + + -32768 as ssmall_int_min, 32767 as ssmall_int_max, + 0 as usmall_int_min, 65535 as usmall_int_max, + + CAST(-2147483648 AS INTEGER) AS sinteger_min, + CAST(2147483647 AS INTEGER) AS sinteger_max, + CAST(0 AS BIGINT) AS uinteger_min, + CAST(4294967295 AS BIGINT) AS uinteger_max, + + CAST(-9223372036854775808 AS BIGINT) AS sbigint_min, + CAST(9223372036854775807 AS BIGINT) AS sbigint_max, + CAST(0 AS BIGINT) AS ubigint_min, + --Use string to represent unsigned big int due to lack of support from + --remote test server + '18446744073709551615' AS ubigint_max, + + CAST(-999999999 AS DECIMAL(38, 0)) AS decimal_negative, + CAST(999999999 AS DECIMAL(38, 0)) AS decimal_positive, + + CAST(-3.40282347E38 AS FLOAT) AS float_min, CAST(3.40282347E38 AS FLOAT) AS float_max, + + CAST(-1.7976931348623157E308 AS DOUBLE) AS double_min, + CAST(1.7976931348623157E308 AS DOUBLE) AS double_max, + + --Boolean + CAST(false AS BOOLEAN) AS bit_false, + CAST(true AS BOOLEAN) AS bit_true, + + --Character types + 'Z' AS c_char, '你' AS c_wchar, + + '你好' AS c_wvarchar, + + 'XYZ' AS c_varchar, + + --Date / timestamp + CAST(DATE '1400-01-01' AS DATE) AS date_min, + CAST(DATE '9999-12-31' AS DATE) AS date_max, + + CAST(TIMESTAMP '1400-01-01 00:00:00' AS TIMESTAMP) AS timestamp_min, + CAST(TIMESTAMP '9999-12-31 23:59:59' AS TIMESTAMP) AS timestamp_max; + )"; + return wsql; +} + +void FlightSQLODBCRemoteTestBase::SetUp() { + if (arrow::internal::GetEnvVar(TEST_CONNECT_STR).ValueOr("").empty()) { + GTEST_SKIP() << "Skipping FlightSQLODBCRemoteTestBase test: TEST_CONNECT_STR not set"; + } +} + +std::string FindTokenInCallHeaders(const CallHeaders& incoming_headers) { + // Lambda function to compare characters without case sensitivity. + auto char_compare = [](const char& char1, const char& char2) { + return (::toupper(char1) == ::toupper(char2)); + }; + + std::string bearer_token(""); + auto auth_header = incoming_headers.find(authorization_header); + if (auth_header != incoming_headers.end()) { + const std::string auth_val(auth_header->second); + if (auth_val.size() > bearer_prefix.length()) { + if (std::equal(auth_val.begin(), auth_val.begin() + bearer_prefix.length(), + bearer_prefix.begin(), char_compare)) { + bearer_token = auth_val.substr(bearer_prefix.length()); + } + } + } + return bearer_token; +} + +void MockServerMiddleware::SendingHeaders(AddCallHeaders* outgoing_headers) { + std::string bearer_token = FindTokenInCallHeaders(incoming_headers_); + *is_valid_ = (bearer_token == std::string(test_token)); +} + +Status MockServerMiddlewareFactory::StartCall( + const CallInfo& info, const ServerCallContext& context, + std::shared_ptr<ServerMiddleware>* middleware) { + std::string bearer_token = FindTokenInCallHeaders(context.incoming_headers()); + if (bearer_token == std::string(test_token)) { + *middleware = + std::make_shared<MockServerMiddleware>(context.incoming_headers(), &is_valid_); + } else { + return MakeFlightError(FlightStatusCode::Unauthenticated, + "Invalid token for mock server"); + } + + return Status::OK(); +} + +std::string FlightSQLODBCMockTestBase::GetConnectionString() { + std::string connect_str( + "driver={Apache Arrow Flight SQL ODBC Driver};HOST=localhost;port=" + + std::to_string(port) + ";token=" + std::string(test_token) + + ";useEncryption=false;"); + return connect_str; +} + +std::string FlightSQLODBCMockTestBase::GetInvalidConnectionString() { + std::string connect_str = GetConnectionString(); + // Append invalid token to connection string + connect_str += std::string("token=invalid_token;"); + return connect_str; +} + +std::wstring FlightSQLODBCMockTestBase::GetQueryAllDataTypes() { + std::wstring wsql = + LR"( SELECT + -- Numeric types + -128 AS stiny_int_min, 127 AS stiny_int_max, + 0 AS utiny_int_min, 255 AS utiny_int_max, + + -32768 AS ssmall_int_min, 32767 AS ssmall_int_max, + 0 AS usmall_int_min, 65535 AS usmall_int_max, + + CAST(-2147483648 AS INTEGER) AS sinteger_min, + CAST(2147483647 AS INTEGER) AS sinteger_max, + CAST(0 AS INTEGER) AS uinteger_min, + CAST(4294967295 AS INTEGER) AS uinteger_max, + + CAST(-9223372036854775808 AS INTEGER) AS sbigint_min, + CAST(9223372036854775807 AS INTEGER) AS sbigint_max, + CAST(0 AS INTEGER) AS ubigint_min, + -- stored as TEXT as SQLite doesn't support unsigned big int + '18446744073709551615' AS ubigint_max, + + CAST('-999999999' AS NUMERIC) AS decimal_negative, + CAST('999999999' AS NUMERIC) AS decimal_positive, + + CAST(-3.40282347E38 AS REAL) AS float_min, + CAST(3.40282347E38 AS REAL) AS float_max, + + CAST(-1.7976931348623157E308 AS REAL) AS double_min, + CAST(1.7976931348623157E308 AS REAL) AS double_max, + + -- Boolean + 0 AS bit_false, + 1 AS bit_true, + + -- Character types + 'Z' AS c_char, + '你' AS c_wchar, + '你好' AS c_wvarchar, + 'XYZ' AS c_varchar, + + DATE('1400-01-01') AS date_min, + DATE('9999-12-31') AS date_max, + + DATETIME('1400-01-01 00:00:00') AS timestamp_min, + DATETIME('9999-12-31 23:59:59') AS timestamp_max; + )"; + return wsql; +} + +void FlightSQLODBCMockTestBase::CreateTestTables() { + ASSERT_OK(server_->ExecuteSql(R"( + CREATE TABLE TestTable ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + keyName varchar(100), + value int); + + INSERT INTO TestTable (keyName, value) VALUES ('One', 1); + INSERT INTO TestTable (keyName, value) VALUES ('Two', 0); + INSERT INTO TestTable (keyName, value) VALUES ('Three', -1); + )")); +} + +void FlightSQLODBCMockTestBase::CreateTableAllDataType() { + // Limitation on mock SQLite server: + // Only int64, float64, binary, and utf8 Arrow Types are supported by + // SQLiteFlightSqlServer::Impl::DoGetTables + ASSERT_OK(server_->ExecuteSql(R"( + CREATE TABLE AllTypesTable( + bigint_col INTEGER PRIMARY KEY AUTOINCREMENT, + char_col varchar(100), + varbinary_col BLOB, + double_col REAL); + + INSERT INTO AllTypesTable ( + char_col, + varbinary_col, + double_col) VALUES ( + '1st Row', + X'31737420726F77', + 3.14159 + ); + )")); +} + +void FlightSQLODBCMockTestBase::CreateUnicodeTable() { + std::string unicode_sql = arrow::util::WideStringToUTF8( + LR"( + CREATE TABLE 数据( + 资料 varchar(100)); + + INSERT INTO 数据 (资料) VALUES ('第一行'); + INSERT INTO 数据 (资料) VALUES ('二行'); + INSERT INTO 数据 (资料) VALUES ('3rd Row'); + )") + .ValueOr(""); + ASSERT_OK(server_->ExecuteSql(unicode_sql)); +} + +void FlightSQLODBCMockTestBase::SetUp() { + ASSERT_OK_AND_ASSIGN(auto location, Location::ForGrpcTcp("0.0.0.0", 0)); + arrow::flight::FlightServerOptions options(location); + options.auth_handler = std::make_unique<NoOpAuthHandler>(); + options.middleware.push_back( + {"bearer-auth-server", std::make_shared<MockServerMiddlewareFactory>()}); + ASSERT_OK_AND_ASSIGN(server_, + arrow::flight::sql::example::SQLiteFlightSqlServer::Create()); + ASSERT_OK(server_->Init(options)); + + port = server_->port(); + ASSERT_OK_AND_ASSIGN(location, Location::ForGrpcTcp("localhost", port)); + ASSERT_OK_AND_ASSIGN(auto client, arrow::flight::FlightClient::Connect(location)); +} + +void FlightSQLODBCMockTestBase::TearDown() { ASSERT_OK(server_->Shutdown()); } + +bool CompareConnPropertyMap(Connection::ConnPropertyMap map1, + Connection::ConnPropertyMap map2) { + if (map1.size() != map2.size()) return false; + + for (const auto& [key, value] : map1) { + if (value != map2[key]) return false; + } + + return true; +} + +void VerifyOdbcErrorState(SQLSMALLINT handle_type, SQLHANDLE handle, + std::string_view expected_state) { + using ODBC::SqlWcharToString; + + SQLWCHAR sql_state[7] = {}; + SQLINTEGER native_code; + + SQLWCHAR message[ODBC_BUFFER_SIZE] = {}; + SQLSMALLINT real_len = 0; + + // On Windows, real_len is in bytes. On Linux, real_len is in chars. + // So, not using real_len + SQLGetDiagRec(handle_type, handle, 1, sql_state, &native_code, message, + ODBC_BUFFER_SIZE, &real_len); + + EXPECT_EQ(expected_state, SqlWcharToString(sql_state)); +} + +std::string GetOdbcErrorMessage(SQLSMALLINT handle_type, SQLHANDLE handle) { + using ODBC::SqlWcharToString; + + SQLWCHAR sql_state[7] = {}; + SQLINTEGER native_code; + + SQLWCHAR message[ODBC_BUFFER_SIZE] = {}; + SQLSMALLINT real_len = 0; + + // On Windows, real_len is in bytes. On Linux, real_len is in chars. + // So, not using real_len + SQLGetDiagRec(handle_type, handle, 1, sql_state, &native_code, message, + ODBC_BUFFER_SIZE, &real_len); + + std::string res = SqlWcharToString(sql_state); + + if (res.empty() || !message[0]) { + res = "Cannot find ODBC error message"; + } else { + res.append(": ").append(SqlWcharToString(message)); + } + + return res; +} + +// TODO: once RegisterDsn is implemented in Mac and Linux, the following can be +// re-enabled. +#if defined _WIN32 || defined _WIN64 Review Comment: nit: same here -- 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]
