From 98816bf0fd320bb7d6ace808a1dd6ecb2ff4bbf2 Mon Sep 17 00:00:00 2001 From: Anshu6250 Date: Wed, 12 Aug 2026 16:02:38 +0530 Subject: [PATCH 1/2] test --- .../odbc/bq_driver/odbc_driver_metadata.cc | 26 +++++-- .../odbc_driver_tests/catalog_test.cc | 67 +++++++++++++++++++ 2 files changed, 86 insertions(+), 7 deletions(-) diff --git a/google/cloud/odbc/bq_driver/odbc_driver_metadata.cc b/google/cloud/odbc/bq_driver/odbc_driver_metadata.cc index 3b6ae05f64..0626bd2d39 100644 --- a/google/cloud/odbc/bq_driver/odbc_driver_metadata.cc +++ b/google/cloud/odbc/bq_driver/odbc_driver_metadata.cc @@ -434,6 +434,25 @@ SQLRETURN SQLTablesInternal(SQLHSTMT stmt_handle, SQLCHAR* catalog_name, } SQLULEN metadata_id = *attr_status; + if (handle.GetConnectionHandle() == nullptr) { + LOG(ERROR) << "SQLTables:: Internal connection handle is null"; + return LogAndReturnCode(handle, + StatusRecord{SQLStates::k_HY013(), + "Internal connection handle is null"}); + } + ConnectionHandle& conn_handle = *(handle.GetConnectionHandle()); + std::string catalog_str; + if (catalog_name == nullptr || catalog_name_len == 0) { + SQLINTEGER catalog_len = 0; + SQLCHAR current_catalog[256] = {0}; + conn_handle.GetAttribute(SQL_ATTR_CURRENT_CATALOG, current_catalog, + sizeof(current_catalog), &catalog_len); + + catalog_str.assign(reinterpret_cast(current_catalog), catalog_len); + catalog_name = reinterpret_cast(catalog_str.data()); + catalog_name_len = static_cast(catalog_str.size()); + } + auto input_param_status = ValidateInputParameters( catalog_name, catalog_name_len, schema_name, schema_name_len, table_name, table_name_len, table_type_len, metadata_id); @@ -448,13 +467,6 @@ SQLRETURN SQLTablesInternal(SQLHSTMT stmt_handle, SQLCHAR* catalog_name, std::string table_filter = ToCharStr(table_name, kMatchAll); std::string table_type_filter = ToCharStr(table_type, kMatchAll); - if (handle.GetConnectionHandle() == nullptr) { - LOG(ERROR) << "SQLTables:: Internal connection handle is null"; - return LogAndReturnCode(handle, - StatusRecord{SQLStates::k_HY013(), - "Internal connection handle is null"}); - } - ConnectionHandle& conn_handle = *(handle.GetConnectionHandle()); if (!metadata_id && dataset_filter == kMatchAll) { auto const dsn = conn_handle.GetDsn(); if (dsn.filter_tables_on_default_dataset && !dsn.default_dataset.empty()) { diff --git a/google/cloud/odbc/integration_tests/odbc_driver_tests/catalog_test.cc b/google/cloud/odbc/integration_tests/odbc_driver_tests/catalog_test.cc index a272f2f362..5bcc16620a 100644 --- a/google/cloud/odbc/integration_tests/odbc_driver_tests/catalog_test.cc +++ b/google/cloud/odbc/integration_tests/odbc_driver_tests/catalog_test.cc @@ -2164,4 +2164,71 @@ TEST(SQLTables, Check_SQLTablesDescriptors) { EXPECT_EQ(Disconnect(conn), SQL_SUCCESS); } +TEST(CatalogTest, SQLTables_NullCatalogFiltersToCurrentProject) { + std::cout + << "[DEBUG] Starting integration test for SQLTables with NULL catalog..." + << std::endl; + + auto conn = std::make_shared(); + std::cout << "[DEBUG] Connecting to the data source..." << std::endl; + ASSERT_EQ(Connect(kDefaultConnectionString, conn), SQL_SUCCESS); + + SQLRETURN status = SQLSetStmtAttr(conn->hstmt, SQL_ATTR_METADATA_ID, + (SQLPOINTER)SQL_FALSE, 0); + CheckError(status, "SQLSetStmtAttr", conn); + + SQLCHAR current_catalog[256] = {0}; + SQLINTEGER catalog_len = 0; + SQLRETURN attr_status = + SQLGetConnectAttr(conn->hdbc, SQL_ATTR_CURRENT_CATALOG, current_catalog, + sizeof(current_catalog), &catalog_len); + + ASSERT_TRUE(SQL_SUCCEEDED(attr_status)) + << "Failed to get SQL_ATTR_CURRENT_CATALOG"; + std::string expected_catalog(reinterpret_cast(current_catalog)); + std::cout << "[DEBUG] Current Catalog (Project) from connection: " + << expected_catalog << std::endl; + + std::cout << "[DEBUG] Calling SQLTables with NULL catalog..." << std::endl; + SQLCHAR table_type[] = "TABLE,VIEW"; + SQLRETURN rc = SQLTables(conn->hstmt, NULL, 0, // Catalog (NULL) + NULL, 0, // Schema + NULL, 0, // Table name + table_type, SQL_NTS); // Table type + + ASSERT_TRUE(SQL_SUCCEEDED(rc)) << "SQLTables call failed."; + + SQLCHAR out_catalog[256] = {0}; + SQLLEN out_len = 0; + SQLBindCol(conn->hstmt, 1, SQL_C_CHAR, out_catalog, sizeof(out_catalog), + &out_len); + + int row_count = 0; + bool foreign_catalog_found = false; + + std::cout << "[DEBUG] Fetching rows..." << std::endl; + + while (SQLFetch(conn->hstmt) == SQL_SUCCESS) { + row_count++; + std::string fetched_catalog(reinterpret_cast(out_catalog)); + + if (fetched_catalog != expected_catalog) { + std::cout << "[DEBUG] ERROR: Found catalog '" << fetched_catalog + << "' which does not match current catalog '" + << expected_catalog << "'!" << std::endl; + foreign_catalog_found = true; + } + } + + std::cout << "[DEBUG] Total rows fetched: " << row_count << std::endl; + + EXPECT_FALSE(foreign_catalog_found) + << "SQLTables returned data for projects outside the configured DSN."; + EXPECT_GT(row_count, 0) + << "Expected to find at least one table/view in the default project."; + + EXPECT_EQ(Disconnect(conn), SQL_SUCCESS); + std::cout << "[DEBUG] Test completed successfully." << std::endl; +} + } // namespace google::cloud::odbc_tests From 6e37ef3043cc7769ba66e00b41a7e0a010ad9d7c Mon Sep 17 00:00:00 2001 From: Anshu6250 Date: Wed, 12 Aug 2026 18:42:22 +0530 Subject: [PATCH 2/2] minor: small changes --- .../odbc/bq_driver/odbc_driver_metadata.cc | 26 ++++++++++--------- .../odbc_driver_tests/catalog_test.cc | 16 ------------ 2 files changed, 14 insertions(+), 28 deletions(-) diff --git a/google/cloud/odbc/bq_driver/odbc_driver_metadata.cc b/google/cloud/odbc/bq_driver/odbc_driver_metadata.cc index 0626bd2d39..a82bbceacc 100644 --- a/google/cloud/odbc/bq_driver/odbc_driver_metadata.cc +++ b/google/cloud/odbc/bq_driver/odbc_driver_metadata.cc @@ -434,6 +434,15 @@ SQLRETURN SQLTablesInternal(SQLHSTMT stmt_handle, SQLCHAR* catalog_name, } SQLULEN metadata_id = *attr_status; + auto input_param_status = ValidateInputParameters( + catalog_name, catalog_name_len, schema_name, schema_name_len, table_name, + table_name_len, table_type_len, metadata_id); + if (!input_param_status.ok()) { + LOG(ERROR) << "SQLTables::ValidateInputParameters:: " + << input_param_status.message; + return LogAndReturnCode(handle, input_param_status); + } + if (handle.GetConnectionHandle() == nullptr) { LOG(ERROR) << "SQLTables:: Internal connection handle is null"; return LogAndReturnCode(handle, @@ -448,18 +457,11 @@ SQLRETURN SQLTablesInternal(SQLHSTMT stmt_handle, SQLCHAR* catalog_name, conn_handle.GetAttribute(SQL_ATTR_CURRENT_CATALOG, current_catalog, sizeof(current_catalog), &catalog_len); - catalog_str.assign(reinterpret_cast(current_catalog), catalog_len); - catalog_name = reinterpret_cast(catalog_str.data()); - catalog_name_len = static_cast(catalog_str.size()); - } - - auto input_param_status = ValidateInputParameters( - catalog_name, catalog_name_len, schema_name, schema_name_len, table_name, - table_name_len, table_type_len, metadata_id); - if (!input_param_status.ok()) { - LOG(ERROR) << "SQLTables::ValidateInputParameters:: " - << input_param_status.message; - return LogAndReturnCode(handle, input_param_status); + if (catalog_len > 0) { + catalog_str.assign(reinterpret_cast(current_catalog), catalog_len); + catalog_name = reinterpret_cast(catalog_str.data()); + catalog_name_len = static_cast(catalog_str.size()); + } } std::string project_filter = ToCharStr(catalog_name, kMatchAll); diff --git a/google/cloud/odbc/integration_tests/odbc_driver_tests/catalog_test.cc b/google/cloud/odbc/integration_tests/odbc_driver_tests/catalog_test.cc index 5bcc16620a..80a8bccdf7 100644 --- a/google/cloud/odbc/integration_tests/odbc_driver_tests/catalog_test.cc +++ b/google/cloud/odbc/integration_tests/odbc_driver_tests/catalog_test.cc @@ -2165,12 +2165,7 @@ TEST(SQLTables, Check_SQLTablesDescriptors) { } TEST(CatalogTest, SQLTables_NullCatalogFiltersToCurrentProject) { - std::cout - << "[DEBUG] Starting integration test for SQLTables with NULL catalog..." - << std::endl; - auto conn = std::make_shared(); - std::cout << "[DEBUG] Connecting to the data source..." << std::endl; ASSERT_EQ(Connect(kDefaultConnectionString, conn), SQL_SUCCESS); SQLRETURN status = SQLSetStmtAttr(conn->hstmt, SQL_ATTR_METADATA_ID, @@ -2186,10 +2181,7 @@ TEST(CatalogTest, SQLTables_NullCatalogFiltersToCurrentProject) { ASSERT_TRUE(SQL_SUCCEEDED(attr_status)) << "Failed to get SQL_ATTR_CURRENT_CATALOG"; std::string expected_catalog(reinterpret_cast(current_catalog)); - std::cout << "[DEBUG] Current Catalog (Project) from connection: " - << expected_catalog << std::endl; - std::cout << "[DEBUG] Calling SQLTables with NULL catalog..." << std::endl; SQLCHAR table_type[] = "TABLE,VIEW"; SQLRETURN rc = SQLTables(conn->hstmt, NULL, 0, // Catalog (NULL) NULL, 0, // Schema @@ -2206,29 +2198,21 @@ TEST(CatalogTest, SQLTables_NullCatalogFiltersToCurrentProject) { int row_count = 0; bool foreign_catalog_found = false; - std::cout << "[DEBUG] Fetching rows..." << std::endl; - while (SQLFetch(conn->hstmt) == SQL_SUCCESS) { row_count++; std::string fetched_catalog(reinterpret_cast(out_catalog)); if (fetched_catalog != expected_catalog) { - std::cout << "[DEBUG] ERROR: Found catalog '" << fetched_catalog - << "' which does not match current catalog '" - << expected_catalog << "'!" << std::endl; foreign_catalog_found = true; } } - std::cout << "[DEBUG] Total rows fetched: " << row_count << std::endl; - EXPECT_FALSE(foreign_catalog_found) << "SQLTables returned data for projects outside the configured DSN."; EXPECT_GT(row_count, 0) << "Expected to find at least one table/view in the default project."; EXPECT_EQ(Disconnect(conn), SQL_SUCCESS); - std::cout << "[DEBUG] Test completed successfully." << std::endl; } } // namespace google::cloud::odbc_tests