You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by is...@apache.org on 2023/12/12 19:15:48 UTC

(ignite-3) branch main updated: IGNITE-20992 ODBC 3.0: Propagate username to connection_info (#2901)

This is an automated email from the ASF dual-hosted git repository.

isapego pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/ignite-3.git


The following commit(s) were added to refs/heads/main by this push:
     new d46beb2b1f IGNITE-20992 ODBC 3.0: Propagate username to connection_info (#2901)
d46beb2b1f is described below

commit d46beb2b1ff886a85b406a79a8a018c250f86aac
Author: Dmitriy Zabotlin <ar...@users.noreply.github.com>
AuthorDate: Tue Dec 12 21:15:42 2023 +0200

    IGNITE-20992 ODBC 3.0: Propagate username to connection_info (#2901)
    
    Co-authored-by: dzabotlin <dz...@gridgain.com>
---
 .../cpp/ignite/odbc/config/connection_info.cpp     | 11 ++++---
 .../cpp/ignite/odbc/config/connection_info.h       |  5 +++
 .../ignite/odbc/config/connection_info_test.cpp    | 36 ++++++++++++++++++++++
 .../platforms/cpp/ignite/odbc/sql_connection.cpp   |  1 +
 .../cpp/tests/odbc-test/connection_test.cpp        | 30 ++++++++++++++++++
 5 files changed, 79 insertions(+), 4 deletions(-)

diff --git a/modules/platforms/cpp/ignite/odbc/config/connection_info.cpp b/modules/platforms/cpp/ignite/odbc/config/connection_info.cpp
index 4366d1e015..f3e0e0b56b 100644
--- a/modules/platforms/cpp/ignite/odbc/config/connection_info.cpp
+++ b/modules/platforms/cpp/ignite/odbc/config/connection_info.cpp
@@ -572,8 +572,7 @@ const char *connection_info::info_type_to_string(info_type type) {
 
 #undef DBG_STR_CASE
 
-connection_info::connection_info(const configuration &config)
-    : config(config) {
+void connection_info::rebuild() {
     //
     //======================= String Params =======================
     //
@@ -809,8 +808,7 @@ connection_info::connection_info(const configuration &config)
 
 #ifdef SQL_USER_NAME
     // A character string with the name used in a particular database, which can be different from the login name.
-    // TODO: IGNITE-19722 Report username here.
-    m_str_params[SQL_USER_NAME] = "ignite";
+    m_str_params[SQL_USER_NAME] = config.get_auth_identity().get_value();
 #endif // SQL_USER_NAME
 
     //
@@ -2526,6 +2524,11 @@ connection_info::connection_info(const configuration &config)
 #endif // SQL_NULL_COLLATION
 }
 
+connection_info::connection_info(const configuration &config)
+    : config(config) {
+    rebuild();
+}
+
 std::string connection_info::get_formatted_project_version() {
     std::string_view project_ver = CMAKE_PROJECT_VERSION;
 
diff --git a/modules/platforms/cpp/ignite/odbc/config/connection_info.h b/modules/platforms/cpp/ignite/odbc/config/connection_info.h
index 6907c57540..54398286ab 100644
--- a/modules/platforms/cpp/ignite/odbc/config/connection_info.h
+++ b/modules/platforms/cpp/ignite/odbc/config/connection_info.h
@@ -74,6 +74,11 @@ public:
      */
     sql_result get_info(info_type type, void *buf, short buffer_len, short *result_len) const;
 
+    /**
+     * Rebuild connection_info from config.
+     */
+     void rebuild();
+
 private:
     /** Associative array of string m_parameters. */
     typedef std::map<info_type, std::string> string_info_map;
diff --git a/modules/platforms/cpp/ignite/odbc/config/connection_info_test.cpp b/modules/platforms/cpp/ignite/odbc/config/connection_info_test.cpp
index bd219c9ae9..f178b4fa90 100644
--- a/modules/platforms/cpp/ignite/odbc/config/connection_info_test.cpp
+++ b/modules/platforms/cpp/ignite/odbc/config/connection_info_test.cpp
@@ -219,3 +219,39 @@ TEST_F(connection_info_test, supported_info) {
     EXPECT_EQ(result, sql_result::AI_SUCCESS);
 #endif // SQL_QUOTED_IDENTIFIER_CASE
 }
+
+TEST_F(connection_info_test, default_username) {
+    char buffer[4096];
+    short res_len = 0;
+
+    configuration cfg;
+    connection_info info(cfg);
+
+    sql_result result;
+
+#ifdef SQL_USER_NAME
+    result = info.get_info(SQL_USER_NAME, buffer, sizeof(buffer), &res_len);
+    EXPECT_EQ(result, sql_result::AI_SUCCESS);
+    EXPECT_EQ(res_len, 0);
+#endif // SQL_USER_NAME
+}
+
+TEST_F(connection_info_test, username) {
+    char buffer[4096];
+    short res_len = 0;
+
+    std::string identity = "username-1";
+    std::string secret = "secret-1";
+
+    configuration cfg(identity, secret);
+    connection_info info(cfg);
+
+    sql_result result;
+
+#ifdef SQL_USER_NAME
+    result = info.get_info(SQL_USER_NAME, buffer, sizeof(buffer), &res_len);
+    EXPECT_EQ(result, sql_result::AI_SUCCESS);
+    EXPECT_EQ(res_len, identity.size());
+    EXPECT_EQ(std::string(buffer), identity);
+#endif // SQL_USER_NAME
+}
diff --git a/modules/platforms/cpp/ignite/odbc/sql_connection.cpp b/modules/platforms/cpp/ignite/odbc/sql_connection.cpp
index f132ff6f00..d0a94b2d69 100644
--- a/modules/platforms/cpp/ignite/odbc/sql_connection.cpp
+++ b/modules/platforms/cpp/ignite/odbc/sql_connection.cpp
@@ -126,6 +126,7 @@ void sql_connection::init_socket() {
 
 sql_result sql_connection::internal_establish(const configuration &cfg) {
     m_config = cfg;
+    m_info.rebuild();
 
     if (!m_config.get_address().is_set() || m_config.get_address().get_value().empty()) {
         add_status_record("No valid address to connect.");
diff --git a/modules/platforms/cpp/tests/odbc-test/connection_test.cpp b/modules/platforms/cpp/tests/odbc-test/connection_test.cpp
index 49f9a80597..1f9d59b45b 100644
--- a/modules/platforms/cpp/tests/odbc-test/connection_test.cpp
+++ b/modules/platforms/cpp/tests/odbc-test/connection_test.cpp
@@ -144,3 +144,33 @@ TEST_F(connection_test, odbc3_supported) {
         FAIL() << get_odbc_error_message(SQL_HANDLE_DBC, m_conn);
     }
 }
+
+TEST_F(connection_test, username) {
+    set_authentication_enabled(true);
+    EXPECT_NO_THROW(odbc_connect_throw(get_auth_connection_string()));
+
+    SQLCHAR buffer[ODBC_BUFFER_SIZE];
+    SQLSMALLINT resLen = 0;
+
+    SQLRETURN ret = SQLGetInfo(m_conn, SQL_USER_NAME, buffer, ODBC_BUFFER_SIZE, &resLen);
+
+    if (!SQL_SUCCEEDED(ret))
+        FAIL() << (get_odbc_error_message(SQL_HANDLE_DBC, m_conn));
+
+    EXPECT_EQ(CORRECT_USERNAME, std::string(reinterpret_cast<char*>(buffer)));
+}
+
+TEST_F(connection_test, username_no_auth) {
+    set_authentication_enabled(false);
+    EXPECT_NO_THROW(odbc_connect_throw(get_basic_connection_string()));
+
+    SQLCHAR buffer[ODBC_BUFFER_SIZE];
+    SQLSMALLINT resLen = 0;
+
+    SQLRETURN ret = SQLGetInfo(m_conn, SQL_USER_NAME, buffer, ODBC_BUFFER_SIZE, &resLen);
+
+    if (!SQL_SUCCEEDED(ret))
+        FAIL() << (get_odbc_error_message(SQL_HANDLE_DBC, m_conn));
+
+    EXPECT_EQ(std::string(""), std::string(reinterpret_cast<char*>(buffer)));
+}