-
Notifications
You must be signed in to change notification settings - Fork 375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Made changes to output param handling code to convert doubles to ints, if necessary #761
Merged
+182
−28
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
218497c
Made changes to output param handling code to convert doubles to ints…
yitam 5b6b6f8
Merge branch 'dev' into vso2823
yitam 5bb3f1a
Modified the error message to indicate value out of range
yitam 5f6c399
A huge negative number is out of range too
yitam 2fc3afe
Added a new test for issue 707
yitam 06e9297
Use helper method isAEQualified instead
yitam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -491,7 +491,7 @@ void core_sqlsrv_bind_param( _Inout_ sqlsrv_stmt* stmt, _In_ SQLUSMALLINT param_ | |
ind_ptr = buffer_len; | ||
if( direction != SQL_PARAM_INPUT ){ | ||
// save the parameter so that 1) the buffer doesn't go away, and 2) we can set it to NULL if returned | ||
sqlsrv_output_param output_param( param_ref, static_cast<int>( param_num ), zval_was_bool ); | ||
sqlsrv_output_param output_param( param_ref, static_cast<int>( param_num ), zval_was_bool, php_out_type); | ||
save_output_param_for_later( stmt, output_param TSRMLS_CC ); | ||
} | ||
} | ||
|
@@ -503,7 +503,7 @@ void core_sqlsrv_bind_param( _Inout_ sqlsrv_stmt* stmt, _In_ SQLUSMALLINT param_ | |
ind_ptr = buffer_len; | ||
if( direction != SQL_PARAM_INPUT ){ | ||
// save the parameter so that 1) the buffer doesn't go away, and 2) we can set it to NULL if returned | ||
sqlsrv_output_param output_param( param_ref, static_cast<int>( param_num ), false ); | ||
sqlsrv_output_param output_param( param_ref, static_cast<int>( param_num ), zval_was_bool, php_out_type); | ||
save_output_param_for_later( stmt, output_param TSRMLS_CC ); | ||
} | ||
} | ||
|
@@ -2149,8 +2149,23 @@ void finalize_output_parameters( _Inout_ sqlsrv_stmt* stmt TSRMLS_DC ) | |
break; | ||
case IS_DOUBLE: | ||
// for a long or a float, simply check if NULL was returned and set the parameter to a PHP null if so | ||
if( stmt->param_ind_ptrs[ output_param->param_num ] == SQL_NULL_DATA ) { | ||
ZVAL_NULL( value_z ); | ||
if (stmt->param_ind_ptrs[output_param->param_num] == SQL_NULL_DATA) { | ||
ZVAL_NULL(value_z); | ||
} | ||
else if (output_param->php_out_type == SQLSRV_PHPTYPE_INT) { | ||
// first check if its value is out of range | ||
double dval = Z_DVAL_P(value_z); | ||
if (dval > INT_MAX || dval < INT_MIN) { | ||
CHECK_CUSTOM_ERROR(true, stmt, SQLSRV_ERROR_DOUBLE_CONVERSION_FAILED) { | ||
throw core::CoreException(); | ||
} | ||
} | ||
// if the output param is a boolean, still convert to | ||
// a long integer first to take care of rounding | ||
convert_to_long(value_z); | ||
if (output_param->is_bool) { | ||
convert_to_boolean(value_z); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this mean that if the value as a double is 0.1, the corresponding boolean is false? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes |
||
} | ||
} | ||
break; | ||
default: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
test/functional/pdo_sqlsrv/pdo_707_ae_output_param_decimals.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
--TEST-- | ||
GitHub issue 707 - binding decimals/numerics to integers or booleans with ColumnEncryption | ||
--DESCRIPTION-- | ||
Verifies that the double values will be rounded as integers or returned as booleans | ||
The key of this test is to connect with ColumnEncryption enabled, and the table columns | ||
do not need to be encrypted | ||
--ENV-- | ||
PHPT_EXEC=true | ||
--SKIPIF-- | ||
<?php require('skipif_unix.inc'); ?> | ||
--FILE-- | ||
<?php | ||
require_once("MsSetup.inc"); | ||
require_once("MsCommon_mid-refactor.inc"); | ||
|
||
$error = "Error converting a double (value out of range) to an integer"; | ||
|
||
function getOutputs($stmt, $outSql, $id, $pdoParamType, $inout = false) | ||
{ | ||
$dec = $num = 0; | ||
|
||
if ($inout) { | ||
$paramType = $pdoParamType | PDO::PARAM_INPUT_OUTPUT; | ||
} else { | ||
$paramType = $pdoParamType; | ||
} | ||
|
||
$stmt->bindParam(1, $id, PDO::PARAM_INT); | ||
$stmt->bindParam(2, $dec, $paramType, PDO::SQLSRV_PARAM_OUT_DEFAULT_SIZE); | ||
$stmt->bindParam(3, $num, $paramType, PDO::SQLSRV_PARAM_OUT_DEFAULT_SIZE); | ||
|
||
$stmt->execute(); | ||
|
||
if ($pdoParamType == PDO::PARAM_BOOL) { | ||
if (!$dec || !$num) { | ||
echo "The returned booleans ($dec, $num) were unexpected!\n"; | ||
} | ||
} else { | ||
if ($dec != 100 || $num != 200) { | ||
echo "The returned integers ($dec, $num) were unexpected!\n"; | ||
} | ||
} | ||
} | ||
|
||
function getOutputsWithException($stmt, $outSql, $id, $pdoParamType, $inout = false) | ||
{ | ||
global $error; | ||
|
||
try { | ||
getOutputs($stmt, $outSql, $id, $pdoParamType, $inout); | ||
} catch (PDOException $e) { | ||
$message = $e->getMessage(); | ||
$found = strpos($message, $error); | ||
if ($found === false) { | ||
echo "Exception message unexpected!\n"; | ||
} | ||
} | ||
} | ||
|
||
function getSmallNumbers($conn, $outSql) | ||
{ | ||
$stmt = $conn->prepare($outSql); | ||
getOutputs($stmt, $outSql, 1, PDO::PARAM_BOOL); | ||
getOutputs($stmt, $outSql, 1, PDO::PARAM_INT); | ||
|
||
getOutputs($stmt, $outSql, 1, PDO::PARAM_BOOL, true); | ||
getOutputs($stmt, $outSql, 1, PDO::PARAM_INT, true); | ||
|
||
unset($stmt); | ||
} | ||
|
||
function getHugeNumbers($conn, $outSql) | ||
{ | ||
// Expects an exception for each call | ||
$stmt = $conn->prepare($outSql); | ||
|
||
getOutputsWithException($stmt, $outSql, 2, PDO::PARAM_BOOL); | ||
getOutputsWithException($stmt, $outSql, 2, PDO::PARAM_INT); | ||
|
||
getOutputsWithException($stmt, $outSql, 2, PDO::PARAM_BOOL, true); | ||
getOutputsWithException($stmt, $outSql, 2, PDO::PARAM_INT, true); | ||
|
||
unset($stmt); | ||
} | ||
|
||
try { | ||
// Check eligibility | ||
$conn = new PDO( "sqlsrv:server = $server", $uid, $pwd ); | ||
if (!isAEQualified($conn)) { | ||
echo "Done\n"; | ||
return; | ||
} | ||
unset($conn); | ||
|
||
// Connection with column encryption enabled | ||
$connectionInfo = "ColumnEncryption = Enabled;"; | ||
$conn = new PDO("sqlsrv:server = $server; database=$databaseName; $connectionInfo", $uid, $pwd); | ||
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | ||
|
||
$tableName = "test_707_decimals"; | ||
$procName = "sp_test_707_decimals"; | ||
|
||
dropTable($conn, $tableName); | ||
dropProc($conn, $procName); | ||
|
||
// Create a test table | ||
$tsql = "CREATE TABLE $tableName (id int identity(1,1), c1_decimal decimal(19,4), c2_numeric numeric(20, 6))"; | ||
$stmt = $conn->query($tsql); | ||
unset($stmt); | ||
|
||
// Insert two rows | ||
$tsql = "INSERT INTO $tableName (c1_decimal, c2_numeric) VALUES (100.078, 200.034)"; | ||
$stmt = $conn->query($tsql); | ||
unset($stmt); | ||
|
||
$tsql = "INSERT INTO $tableName (c1_decimal, c2_numeric) VALUES (199999999999.0123, 999243876923.09887)"; | ||
$stmt = $conn->query($tsql); | ||
unset($stmt); | ||
|
||
// Create a stored procedure | ||
$procArgs = "@id int, @c_decimal decimal(19,4) OUTPUT, @c_numeric numeric(20, 6) OUTPUT"; | ||
$procCode = "SELECT @c_decimal = c1_decimal, @c_numeric = c2_numeric FROM $tableName WHERE id = @id"; | ||
createProc($conn, $procName, $procArgs, $procCode); | ||
|
||
// Read them back by calling the stored procedure | ||
$outSql = getCallProcSqlPlaceholders($procName, 3); | ||
getSmallNumbers($conn, $outSql); | ||
getHugeNumbers($conn, $outSql); | ||
|
||
dropProc($conn, $procName); | ||
dropTable($conn, $tableName); | ||
|
||
unset($conn); | ||
echo "Done\n"; | ||
} catch( PDOException $e ) { | ||
print_r( $e->getMessage() ); | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
Done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This error is not needed anymore?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it's an obsolete error message that is no longer needed