From 297115d2eed752172344d3d80d14591907c7dd45 Mon Sep 17 00:00:00 2001 From: absci Date: Thu, 28 Sep 2023 15:48:11 -0700 Subject: [PATCH] Update connection string keywords set function --- source/pdo_sqlsrv/pdo_dbh.cpp | 10 +++------ source/shared/core_conn.cpp | 4 +--- source/sqlsrv/conn.cpp | 40 ++++++++--------------------------- 3 files changed, 13 insertions(+), 41 deletions(-) diff --git a/source/pdo_sqlsrv/pdo_dbh.cpp b/source/pdo_sqlsrv/pdo_dbh.cpp index 5bbfee696..c9822491e 100644 --- a/source/pdo_sqlsrv/pdo_dbh.cpp +++ b/source/pdo_sqlsrv/pdo_dbh.cpp @@ -151,17 +151,13 @@ struct pdo_encrypt_set_func if (found != std::string::npos) val_str.erase(found + 1); - const char TRUE_VALUE_1[] = "true"; - const char TRUE_VALUE_2[] = "1"; - const char FALSE_VALUE_1[] = "false"; - const char FALSE_VALUE_2[] = "0"; transform(val_str.begin(), val_str.end(), val_str.begin(), ::tolower); - // For backward compatibility, convert true/1 to yes and false/0 to no + // For backward compatibility, convert true/1 to yes std::string attr; - if (!val_str.compare(TRUE_VALUE_1) || !val_str.compare(TRUE_VALUE_2)) { + if (!val_str.compare("true") || !val_str.compare("1") || !val_str.compare("yes")) { attr = "yes"; - } else if (!val_str.compare(FALSE_VALUE_1) || !val_str.compare(FALSE_VALUE_2)) { + } else if (!val_str.compare("false") || !val_str.compare("0") || !val_str.compare("no")) { attr = "no"; } else { // simply pass the attribute value to ODBC driver diff --git a/source/shared/core_conn.cpp b/source/shared/core_conn.cpp index c9f8b6f54..65b3c95fe 100644 --- a/source/shared/core_conn.cpp +++ b/source/shared/core_conn.cpp @@ -1147,10 +1147,8 @@ size_t core_str_zval_is_true(_Inout_ zval* value_z) if (found != std::string::npos) val_str.erase(found + 1); - const char TRUE_VALUE_1[] = "true"; - const char TRUE_VALUE_2[] = "1"; transform(val_str.begin(), val_str.end(), val_str.begin(), ::tolower); - if (!val_str.compare(TRUE_VALUE_1) || !val_str.compare(TRUE_VALUE_2)) { + if (!val_str.compare("true") || !val_str.compare("1") || !val_str.compare("yes")) { return 1; // true } return 0; // false diff --git a/source/sqlsrv/conn.cpp b/source/sqlsrv/conn.cpp index 27ef4b914..c70c78401 100644 --- a/source/sqlsrv/conn.cpp +++ b/source/sqlsrv/conn.cpp @@ -72,32 +72,6 @@ struct decimal_places_func } }; -struct srv_encrypt_set_func { - static void func(connection_option const* option, _In_ zval* value_z, _Inout_ sqlsrv_conn* conn, std::string& conn_str) - { - std::string attr; - - if (Z_TYPE_P(value_z) == IS_LONG) { - long val = Z_LVAL_P(value_z); - if (val == 1) { - attr = "yes"; - } else if (val == 0) { - attr = "no"; - } else { - attr = std::to_string(val); - } - } else if (Z_TYPE_P(value_z) == IS_TRUE || Z_TYPE_P(value_z) == IS_FALSE) { - attr = zend_is_true(value_z) ? "yes" : "no"; - } else { - attr = Z_STRVAL_P(value_z); - } - - char temp_str[MAX_CONN_VALSTRING_LEN]; - snprintf(temp_str, MAX_CONN_VALSTRING_LEN, "%s={%s};", option->odbc_name, attr.c_str()); - - conn_str += temp_str; - } -}; struct conn_char_set_func { @@ -133,18 +107,22 @@ struct bool_conn_str_func { static void func( _In_ connection_option const* option, _In_ zval* value, sqlsrv_conn* /*conn*/, _Out_ std::string& conn_str ) { - char temp_str[MAX_CONN_VALSTRING_LEN]; + std::string attr; if (Z_TYPE_P(value) != IS_STRING) { - convert_to_string(value); + convert_to_string_ex(value); } - const char *value_str = Z_STRVAL_P(value); + attr = Z_STRVAL_P(value); + transform(attr.begin(), attr.end(), attr.begin(), ::tolower); + + char temp_str[MAX_CONN_VALSTRING_LEN]; snprintf(temp_str, MAX_CONN_VALSTRING_LEN, "%s={%s};", option->odbc_name, - ((stricmp(value_str, "true") == 0 || stricmp(value_str, "1") == 0) ? "yes" : "no")); + ((!attr.compare("true") || !attr.compare("1") || !attr.compare("yes")) ? "yes" : "no")); + conn_str += temp_str; } }; @@ -459,7 +437,7 @@ const connection_option SS_CONN_OPTS[] = { ODBCConnOptions::Encrypt, sizeof( ODBCConnOptions::Encrypt ), CONN_ATTR_MIXED, - srv_encrypt_set_func::func + bool_conn_str_func::func }, { SSConnOptionNames::Failover_Partner,