Skip to content

Commit

Permalink
Update connection string keywords set function (#1484)
Browse files Browse the repository at this point in the history
* Update connection string keywords set function

* Update encrypt unit test
  • Loading branch information
absci authored Oct 5, 2023
1 parent bf2ca1f commit 9932512
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 42 deletions.
10 changes: 3 additions & 7 deletions source/pdo_sqlsrv/pdo_dbh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions source/shared/core_conn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 9 additions & 31 deletions source/sqlsrv/conn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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;
}
};
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ echo 'Test case 7' . PHP_EOL;
$connectionOptions = array('Encrypt' => 3);
$conn = sqlsrv_connect($server, $connectionOptions);
if ($conn !== false) {
echo 'Expect this to fail' . PHP_EOL;
sqlsrv_close($conn);
}

echo 'Done' . PHP_EOL;
Expand Down

0 comments on commit 9932512

Please sign in to comment.