Modified AE fetch phptypes test to insert only one row at a time and loop through php types (#801)

* Modified AE fetch phptypes test to insert only one row at a time and loop through php types

* Fixed formatting
This commit is contained in:
Jenny Tam 2018-06-27 13:14:10 -07:00 committed by GitHub
parent 197489ab4f
commit aef3830479
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,16 +1,17 @@
--TEST-- --TEST--
Test insert data and fetch as all possible php types Test insert data and fetch as all possible php types
--DESCRIPTION--
Test insert data of most common column types and fetch them all as possible php types
--SKIPIF-- --SKIPIF--
<?php require('skipif_versions_old.inc'); ?> <?php require('skipif_versions_old.inc'); ?>
--FILE-- --FILE--
<?php <?php
require_once('MsCommon.inc'); require_once('MsCommon.inc');
require_once('tools.inc');
require_once('values.php'); require_once('values.php');
// Set up the columns and build the insert query. Each data type has an // Set up the columns and build the insert query. Each data type has an
// AE-encrypted and a non-encrypted column side by side in the table. // AE-encrypted and a non-encrypted column side by side in the table.
function FormulateSetupQuery($tableName, &$dataTypes, &$columns, &$insertQuery, $strsize, $strsize2) function formulateSetupQuery($tableName, &$dataTypes, &$columns, &$insertQuery)
{ {
$columns = array(); $columns = array();
$queryTypes = "("; $queryTypes = "(";
@ -45,20 +46,22 @@ $SQLSRV_PHPTYPE_CONST = array(SQLSRV_PHPTYPE_INT,
SQLSRV_PHPTYPE_STRING("UTF-8") SQLSRV_PHPTYPE_STRING("UTF-8")
); );
// Two sizes for the string types so we can test conversion from
// a shorter type to a longer type
$strsize = 256;
$strsize2 = 384;
$dataTypes = array ("binary($strsize)", "varbinary($strsize)", "varbinary(max)", "char($strsize)", // Two constants STRSIZE and LONG_STRSIZE for the string types so we can test
"varchar($strsize)", "varchar(max)", "nchar($strsize)", "nvarchar($strsize)", // conversion from a shorter type to a longer type
"nvarchar(max)", "datetime", "smalldatetime", "date", "time(5)", "datetimeoffset(5)", $strsize = STRSIZE;
"datetime2(5)", "decimal(28,4)", "numeric(32,4)", "float", "real", "bigint", "int", $strsize2 = LONG_STRSIZE;
"smallint", "tinyint", "bit",
"binary($strsize2)", "varbinary($strsize2)", "char($strsize2)", $dataTypes = array("binary($strsize)", "varbinary($strsize)", "varbinary(max)",
"varchar($strsize2)", "nchar($strsize2)", "nvarchar($strsize2)", "char($strsize)", "varchar($strsize)", "varchar(max)",
"time", "datetimeoffset", "datetime2", "decimal(32,4)", "numeric(36,4)" "nchar($strsize)", "nvarchar($strsize)", "nvarchar(max)",
); "datetime", "smalldatetime", "date", "time(5)", "datetimeoffset(5)",
"datetime2(5)", "decimal(28,4)", "numeric(32,4)", "float", "real",
"bigint", "int", "smallint", "tinyint", "bit",
"binary($strsize2)", "varbinary($strsize2)", "char($strsize2)",
"varchar($strsize2)", "nchar($strsize2)", "nvarchar($strsize2)",
"time", "datetimeoffset", "datetime2", "decimal(32,4)", "numeric(36,4)"
);
set_time_limit(0); set_time_limit(0);
sqlsrv_configure('WarningsReturnAsErrors', 1); sqlsrv_configure('WarningsReturnAsErrors', 1);
@ -74,16 +77,15 @@ $tableName = "type_conversion_table";
$columns = array(); $columns = array();
$insertQuery = ""; $insertQuery = "";
FormulateSetupQuery($tableName, $dataTypes, $columns, $insertQuery, $strsize, $strsize2); formulateSetupQuery($tableName, $dataTypes, $columns, $insertQuery);
$stmt = AE\createTable($conn, $tableName, $columns); $stmt = AE\createTable($conn, $tableName, $columns);
if (!$stmt) { if (!$stmt) {
fatalError("Failed to create table $tableName\n"); fatalError("Failed to create table $tableName\n");
} }
// The data we test against is in values.php // The data we test against is in values.php
for ($v = 0; $v < sizeof($values);++$v) for ($v = 0; $v < sizeof($values); ++$v) {
{
// Each value must be inserted twice because the AE and non-AE column are side by side. // Each value must be inserted twice because the AE and non-AE column are side by side.
$testValues = array(); $testValues = array();
for ($i=0; $i<sizeof($values[$v]); ++$i) { for ($i=0; $i<sizeof($values[$v]); ++$i) {
@ -92,101 +94,111 @@ for ($v = 0; $v < sizeof($values);++$v)
} }
// Insert the data using sqlsrv_prepare() // Insert the data using sqlsrv_prepare()
// Insert one set of data for each PHPTYPE
$stmt = sqlsrv_prepare($conn, $insertQuery, $testValues); $stmt = sqlsrv_prepare($conn, $insertQuery, $testValues);
if ($stmt == false) { if ($stmt == false) {
print_r(sqlsrv_errors()); print_r(sqlsrv_errors());
fatalError("sqlsrv_prepare failed\n"); fatalError("sqlsrv_prepare failed\n");
} }
for ($i = 0; $i < sizeof($SQLSRV_PHPTYPE_CONST); ++$i) { if (sqlsrv_execute($stmt) == false) {
if (sqlsrv_execute($stmt) == false) { print_r(sqlsrv_errors());
print_r(sqlsrv_errors()); fatalError("sqlsrv_execute failed\n");
fatalError("sqlsrv_execute failed\n");
}
} }
$selectQuery = "SELECT * FROM $tableName"; $selectQuery = "SELECT * FROM $tableName";
// Two select statements for selection using // Two select statements for selection using
// sqlsrv_get_field and sqlsrv_fetch_array // sqlsrv_get_field and sqlsrv_fetch_array
$stmt = sqlsrv_query($conn, $selectQuery); // Use sqlsrv_prepare() for the first one
// such that sqlsrv_execute() can be invoked for
// each PHP type
$stmt = sqlsrv_prepare($conn, $selectQuery);
if ($stmt == false) { if ($stmt == false) {
print_r(sqlsrv_errors()); print_r(sqlsrv_errors());
fatalError("First sqlsrv_prepare failed\n"); fatalError("SELECT using sqlsrv_prepare failed\n");
} }
$stmt2 = sqlsrv_query($conn, $selectQuery); $stmt2 = sqlsrv_query($conn, $selectQuery);
if ($stmt2 == false) { if ($stmt2 == false) {
print_r(sqlsrv_errors()); print_r(sqlsrv_errors());
fatalError("Second sqlsrv_prepare failed\n"); fatalError("SELECT using sqlsrv_query failed\n");
} }
$numFields = sqlsrv_num_fields($stmt); $numFields = sqlsrv_num_fields($stmt);
$i = 0;
$valueAE = null; $valueAE = null;
$valueFromArrayAE = null; $valueFromArrayAE = null;
while ($result = sqlsrv_fetch($stmt)) {
$dataArray = sqlsrv_fetch_array($stmt2, SQLSRV_FETCH_NUMERIC);
for ($j = 0; $j < $numFields; $j++) {
$value = sqlsrv_get_field($stmt, $j, $SQLSRV_PHPTYPE_CONST[$i]);
$valueFromArray = $dataArray[$j];
// PHPTYPE_STREAM returns a PHP resource, so check the type
if (is_resource($value)) $value = get_resource_type($value);
// For each type, the AE values come first and non-AE values second $dataArray = sqlsrv_fetch_array($stmt2, SQLSRV_FETCH_NUMERIC);
// So let's do the comparison every second field for ($i = 0; $i < sizeof($SQLSRV_PHPTYPE_CONST); ++$i) {
if ($j%2 == 0) { if (!sqlsrv_execute($stmt)) {
$valueAE = $value; fatalError("Execute failed for $SQLSRV_PHPTYPE_CONST[$i]\n");
$valueFromArrayAE = $valueFromArray; }
} elseif ($j%2 == 1) {
// If returning a DateTime PHP type from a date only SQL type, if ($result = sqlsrv_fetch($stmt)) {
// PHP adds the current timestamp to make a DateTime object, for ($j = 0; $j < $numFields; $j++) {
// and in this case the AE and non-AE times may be off by a $value = sqlsrv_get_field($stmt, $j, $SQLSRV_PHPTYPE_CONST[$i]);
// fraction of a second since they are retrieved at ever-so-slightly $valueFromArray = $dataArray[$j];
// different times. This not a test-failing discrepancy, so
// below the DateTime objects are made equal again for the next if // PHPTYPE_STREAM returns a PHP resource, so check the type
// block. if (is_resource($value)) {
if ($value instanceof DateTime) { $value = get_resource_type($value);
// date_diff returns a DateInterval object, and s is
// the difference in seconds. s should be zero because
// the difference should be just a fraction of a second.
$datediff = date_diff($value, $valueAE);
$diff = $datediff->s;
if ($diff == 0) {
$value = $valueAE;
}
} }
if ($valueAE != $value or $valueFromArrayAE != $valueFromArray) { // For each type, the AE values come first and non-AE values second
echo "Values do not match! PHPType $i Field $j\n"; // So let's do the comparison every second field
print_r($valueAE);echo "\n"; if ($j%2 == 0) {
print_r($value);echo "\n"; $valueAE = $value;
print_r($valueFromArrayAE);echo "\n"; $valueFromArrayAE = $valueFromArray;
print_r($valueFromArray);echo "\n"; } elseif ($j%2 == 1) {
print_r(sqlsrv_errors()); // If returning a DateTime PHP type from a date only SQL type,
fatalError("Test failed, values do not match.\n"); // PHP adds the current timestamp to make a DateTime object,
// and in this case the AE and non-AE times may be off by a
// fraction of a second since they are retrieved at ever-so-slightly
// different times. This not a test-failing discrepancy, so
// below the DateTime objects are made equal again for the next if
// block.
if ($value instanceof DateTime) {
// date_diff returns a DateInterval object, and s is
// the difference in seconds. s should be zero because
// the difference should be just a fraction of a second.
$datediff = date_diff($value, $valueAE);
$diff = $datediff->s;
if ($diff == 0) {
$value = $valueAE;
}
}
if ($valueAE != $value or $valueFromArrayAE != $valueFromArray) {
$index = floor($j / 2);
echo "Values do not match! PHPType $i Field $dataTypes[$index]\n";
print_r($valueAE);
echo "\n--------\n\n";
print_r($value);
echo "\n--------\n\n";
print_r($valueFromArrayAE);
echo "\n--------\n\n";
print_r($valueFromArray);
echo "\n--------\n\n";
print_r(sqlsrv_errors());
echo("Test failed, values do not match.\n");
}
} }
} }
} }
++$i;
} }
sqlsrv_free_stmt($stmt); sqlsrv_free_stmt($stmt);
sqlsrv_free_stmt($stmt2); sqlsrv_free_stmt($stmt2);
$deleteQuery = "DELETE FROM $tableName"; $deleteQuery = "TRUNCATE TABLE $tableName";
$stmt = sqlsrv_query($conn, $deleteQuery); $stmt = sqlsrv_query($conn, $deleteQuery);
if ($stmt == false) { if ($stmt == false) {
print_r(sqlsrv_errors()); print_r(sqlsrv_errors());
fatalError("Delete statement failed"); fatalError("Truncate statement failed");
} }
sqlsrv_free_stmt($stmt); sqlsrv_free_stmt($stmt);
} }