Skip to content

Commit cb31765

Browse files
GH-47719: [C++][FlightRPC] Extract SQLTables Implementation (#48021)
### Rationale for this change Addresses #47719 ### What changes are included in this PR? SQLTables enabled. Table tests added. ### Are these changes tested? Tested locally on MSVC. ### Are there any user-facing changes? No. * GitHub Issue: #47719 Lead-authored-by: Alina (Xi) Li <[email protected]> Co-authored-by: justing-bq <[email protected]> Co-authored-by: alinalibq <[email protected]> Signed-off-by: David Li <[email protected]>
1 parent 2a3c5db commit cb31765

File tree

5 files changed

+531
-2
lines changed

5 files changed

+531
-2
lines changed

cpp/src/arrow/flight/sql/odbc/odbc_api.cc

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,8 +1182,24 @@ SQLRETURN SQLTables(SQLHSTMT stmt, SQLWCHAR* catalog_name,
11821182
<< ", table_name_length: " << table_name_length
11831183
<< ", table_type: " << static_cast<const void*>(table_type)
11841184
<< ", table_type_length: " << table_type_length;
1185-
// GH-47719 TODO: Implement SQLTables
1186-
return SQL_INVALID_HANDLE;
1185+
1186+
using ODBC::ODBCStatement;
1187+
using ODBC::SqlWcharToString;
1188+
1189+
return ODBCStatement::ExecuteWithDiagnostics(stmt, SQL_ERROR, [=]() {
1190+
ODBCStatement* statement = reinterpret_cast<ODBCStatement*>(stmt);
1191+
1192+
std::string catalog = SqlWcharToString(catalog_name, catalog_name_length);
1193+
std::string schema = SqlWcharToString(schema_name, schema_name_length);
1194+
std::string table = SqlWcharToString(table_name, table_name_length);
1195+
std::string type = SqlWcharToString(table_type, table_type_length);
1196+
1197+
statement->GetTables(catalog_name ? &catalog : nullptr,
1198+
schema_name ? &schema : nullptr, table_name ? &table : nullptr,
1199+
table_type ? &type : nullptr);
1200+
1201+
return SQL_SUCCESS;
1202+
});
11871203
}
11881204

11891205
SQLRETURN SQLColumns(SQLHSTMT stmt, SQLWCHAR* catalog_name,

cpp/src/arrow/flight/sql/odbc/tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ add_arrow_test(flight_sql_odbc_test
4040
errors_test.cc
4141
statement_attr_test.cc
4242
statement_test.cc
43+
tables_test.cc
4344
# Enable Protobuf cleanup after test execution
4445
# GH-46889: move protobuf_test_util to a more common location
4546
../../../../engine/substrait/protobuf_test_util.cc

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,22 @@ bool WriteDSN(Connection::ConnPropertyMap properties) {
481481
}
482482
#endif
483483

484+
std::wstring GetStringColumnW(SQLHSTMT stmt, int col_id) {
485+
SQLWCHAR buf[1024];
486+
SQLLEN len_indicator = 0;
487+
488+
EXPECT_EQ(SQL_SUCCESS,
489+
SQLGetData(stmt, col_id, SQL_C_WCHAR, buf, sizeof(buf), &len_indicator));
490+
491+
if (len_indicator == SQL_NULL_DATA) {
492+
return L"";
493+
}
494+
495+
// indicator is in bytes, so convert to character count
496+
size_t char_count = static_cast<size_t>(len_indicator) / GetSqlWCharSize();
497+
return std::wstring(buf, buf + char_count);
498+
}
499+
484500
std::wstring ConvertToWString(const std::vector<SQLWCHAR>& str_val, SQLSMALLINT str_len) {
485501
std::wstring attr_str;
486502
if (str_len == 0) {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,12 @@ bool WriteDSN(std::string connection_str);
250250
/// \return true on success
251251
bool WriteDSN(Connection::ConnPropertyMap properties);
252252

253+
/// \brief Get wide string column.
254+
/// \param[in] stmt Statement.
255+
/// \param[in] col_id Column ID to check.
256+
/// \return wstring
257+
std::wstring GetStringColumnW(SQLHSTMT stmt, int col_id);
258+
253259
/// \brief Check wide char vector and convert into wstring
254260
/// \param[in] str_val Vector of SQLWCHAR.
255261
/// \param[in] str_len length of string, in bytes.

0 commit comments

Comments
 (0)