php-sqlsrv/test/functional/pdo_sqlsrv/pdo_ae_bindColumn_pdoparam_datetime_precision.phpt

167 lines
7.1 KiB
Plaintext
Raw Normal View History

2018-02-27 22:38:49 +01:00
--TEST--
2018-03-03 01:34:47 +01:00
Test for retrieving encrypted data from datetime types columns with different precisions using PDO::bindColumn
2018-02-27 22:38:49 +01:00
--DESCRIPTION--
2018-03-03 01:34:47 +01:00
Test conversion from datetime types column to output of PDO::PARAM types
2018-03-13 20:50:20 +01:00
With or without Always Encrypted, conversion works if:
2018-03-03 01:34:47 +01:00
1. From any datetime type column to PDO::PARAM_STR
2. From any datetime type column to PDO::PARAM_LOB
TODO: cannot insert into a datetime2(0) using the PDO_SQLSRV driver
returns operand type clash error between smalldatetime and datetime2(0)
to see error, uncomment 0 from the $precision array
2018-03-13 20:50:20 +01:00
documented in VSO 2693
2018-02-27 22:38:49 +01:00
--SKIPIF--
<?php require('skipif_mid-refactor.inc'); ?>
--FILE--
<?php
require_once("MsCommon_mid-refactor.inc");
require_once("AEData.inc");
2018-03-03 01:34:47 +01:00
function compareDate($dtout, $dtin, $dataType) {
if ($dataType == "datetimeoffset") {
$dtarr = explode(' ', $dtin);
if (strpos($dtout, $dtarr[0]) !== false && strpos($dtout, $dtarr[1]) !== false && strpos($dtout, $dtarr[2]) !== false) {
return true;
}
} else {
if (strpos($dtout, $dtin) !== false) {
return true;
}
}
return false;
}
2018-02-27 22:38:49 +01:00
$dataTypes = array("datetime2", "datetimeoffset", "time");
$precisions = array(0, 1, 2, 4, 7);
2018-02-27 22:38:49 +01:00
$inputValuesInit = array("datetime2" => array("0001-01-01 00:00:00", "9999-12-31 23:59:59"),
"datetimeoffset" => array("0001-01-01 00:00:00 -14:00", "9999-12-31 23:59:59 +14:00"),
"time" => array("00:00:00", "23:59:59"));
try {
$conn = connect("", array(), PDO::ERRMODE_SILENT);
foreach ($dataTypes as $dataType) {
2018-03-03 01:34:47 +01:00
foreach ($precisions as $m) {
// add $m number of decimal digits to the some input values
2018-02-27 22:38:49 +01:00
$inputValues[0] = $inputValuesInit[$dataType][0];
$inputValues[1] = $inputValuesInit[$dataType][1];
2018-03-03 01:34:47 +01:00
if ($m != 0) {
2018-02-27 22:38:49 +01:00
if ($dataType == "datetime2") {
2018-03-03 01:34:47 +01:00
$inputValues[1] .= "." . str_repeat("9", $m);
2018-02-27 22:38:49 +01:00
} else if ($dataType == "datetimeoffset") {
2018-03-03 01:34:47 +01:00
$dtoffsetPieces = explode(" ", $inputValues[1]);
$inputValues[1] = $dtoffsetPieces[0] . " " . $dtoffsetPieces[1] . "." . str_repeat("9", $m) . " " . $dtoffsetPieces[2];
2018-02-27 22:38:49 +01:00
} else if ($dataType == "time") {
2018-03-03 01:34:47 +01:00
$inputValues[0] .= "." . str_repeat("0", $m);
$inputValues[1] .= "." . str_repeat("9", $m);
2018-02-27 22:38:49 +01:00
}
}
2018-03-03 01:34:47 +01:00
$typeFull = "$dataType($m)";
echo "\nTesting $typeFull:\n";
2018-02-27 22:38:49 +01:00
2018-03-03 01:34:47 +01:00
//create and populate table containing datetime2(m), datetimeoffset(m) or time(m) columns
$tbname = "test_" . $dataType . $m;
$colMetaArr = array(new ColumnMeta($typeFull, "c_det"), new ColumnMeta($typeFull, "c_rand", null, "randomized"));
2018-02-27 22:38:49 +01:00
createTable($conn, $tbname, $colMetaArr);
insertRow($conn, $tbname, array("c_det" => $inputValues[0], "c_rand" => $inputValues[1]));
2018-03-03 01:34:47 +01:00
// fetch by specifying PDO::PARAM_ types with PDO:bindColumn
2018-02-27 22:38:49 +01:00
$query = "SELECT c_det, c_rand FROM $tbname";
foreach ($pdoParamTypes as $pdoParamType) {
$det = "";
$rand = "";
$stmt = $conn->prepare($query);
$stmt->execute();
$stmt->bindColumn('c_det', $det, constant($pdoParamType));
$stmt->bindColumn('c_rand', $rand, constant($pdoParamType));
$row = $stmt->fetch(PDO::FETCH_BOUND);
2018-03-03 01:34:47 +01:00
// check the case when fetching as PDO::PARAM_BOOL, PDO::PARAM_NULL or PDO::PARAM_INT
2018-03-13 20:50:20 +01:00
// with or without AE: should not work
2018-02-27 22:38:49 +01:00
if ($pdoParamType == "PDO::PARAM_BOOL" || $pdoParamType == "PDO::PARAM_NULL" || $pdoParamType == "PDO::PARAM_INT") {
if (!is_null($det) || !is_null($rand)) {
2018-03-03 01:34:47 +01:00
echo "Retrieving $typeFull data as $pdoParamType should not be supported\n";
2018-02-27 22:38:49 +01:00
}
2018-03-03 01:34:47 +01:00
// check the case when fetching as PDO::PARAM_STR or PDO::PARAM_LOB
// with or without AE: should work
2018-02-27 22:38:49 +01:00
} else {
2021-07-29 00:45:04 +02:00
if (PHP_VERSION_ID >= 80100 && $pdoParamType == "PDO::PARAM_LOB") {
// Starting with PHP 8.1 fetching as PDO::PARAM_LOB will return a resource obj
$det = fread($det, 8192);
$rand = fread($rand, 8192);
}
2018-03-03 01:34:47 +01:00
if (compareDate($det, $inputValues[0], $dataType) && compareDate($rand, $inputValues[1], $dataType)) {
echo "****Retrieving $typeFull as $pdoParamType is supported****\n";
} else {
echo "Retrieving $typeFull as $pdoParamType fails\n";
}
2018-02-27 22:38:49 +01:00
}
}
2018-03-03 01:34:47 +01:00
// cleanup
2018-02-27 22:38:49 +01:00
dropTable($conn, $tbname);
}
}
unset($stmt);
unset($conn);
} catch (PDOException $e) {
echo $e->getMessage();
}
?>
--EXPECT--
Testing datetime2(0):
****Retrieving datetime2(0) as PDO::PARAM_STR is supported****
****Retrieving datetime2(0) as PDO::PARAM_LOB is supported****
2018-02-27 22:38:49 +01:00
Testing datetime2(1):
2018-03-03 01:34:47 +01:00
****Retrieving datetime2(1) as PDO::PARAM_STR is supported****
****Retrieving datetime2(1) as PDO::PARAM_LOB is supported****
2018-02-27 22:38:49 +01:00
Testing datetime2(2):
2018-03-03 01:34:47 +01:00
****Retrieving datetime2(2) as PDO::PARAM_STR is supported****
****Retrieving datetime2(2) as PDO::PARAM_LOB is supported****
2018-02-27 22:38:49 +01:00
Testing datetime2(4):
2018-03-03 01:34:47 +01:00
****Retrieving datetime2(4) as PDO::PARAM_STR is supported****
****Retrieving datetime2(4) as PDO::PARAM_LOB is supported****
2018-02-27 22:38:49 +01:00
Testing datetime2(7):
2018-03-03 01:34:47 +01:00
****Retrieving datetime2(7) as PDO::PARAM_STR is supported****
****Retrieving datetime2(7) as PDO::PARAM_LOB is supported****
2018-02-27 22:38:49 +01:00
Testing datetimeoffset(0):
****Retrieving datetimeoffset(0) as PDO::PARAM_STR is supported****
****Retrieving datetimeoffset(0) as PDO::PARAM_LOB is supported****
2018-02-27 22:38:49 +01:00
Testing datetimeoffset(1):
2018-03-03 01:34:47 +01:00
****Retrieving datetimeoffset(1) as PDO::PARAM_STR is supported****
****Retrieving datetimeoffset(1) as PDO::PARAM_LOB is supported****
2018-02-27 22:38:49 +01:00
Testing datetimeoffset(2):
2018-03-03 01:34:47 +01:00
****Retrieving datetimeoffset(2) as PDO::PARAM_STR is supported****
****Retrieving datetimeoffset(2) as PDO::PARAM_LOB is supported****
2018-02-27 22:38:49 +01:00
Testing datetimeoffset(4):
2018-03-03 01:34:47 +01:00
****Retrieving datetimeoffset(4) as PDO::PARAM_STR is supported****
****Retrieving datetimeoffset(4) as PDO::PARAM_LOB is supported****
2018-02-27 22:38:49 +01:00
Testing datetimeoffset(7):
2018-03-03 01:34:47 +01:00
****Retrieving datetimeoffset(7) as PDO::PARAM_STR is supported****
****Retrieving datetimeoffset(7) as PDO::PARAM_LOB is supported****
2018-02-27 22:38:49 +01:00
Testing time(0):
****Retrieving time(0) as PDO::PARAM_STR is supported****
****Retrieving time(0) as PDO::PARAM_LOB is supported****
2018-02-27 22:38:49 +01:00
Testing time(1):
2018-03-03 01:34:47 +01:00
****Retrieving time(1) as PDO::PARAM_STR is supported****
****Retrieving time(1) as PDO::PARAM_LOB is supported****
2018-02-27 22:38:49 +01:00
Testing time(2):
2018-03-03 01:34:47 +01:00
****Retrieving time(2) as PDO::PARAM_STR is supported****
****Retrieving time(2) as PDO::PARAM_LOB is supported****
2018-02-27 22:38:49 +01:00
Testing time(4):
2018-03-03 01:34:47 +01:00
****Retrieving time(4) as PDO::PARAM_STR is supported****
****Retrieving time(4) as PDO::PARAM_LOB is supported****
2018-02-27 22:38:49 +01:00
Testing time(7):
2018-03-03 01:34:47 +01:00
****Retrieving time(7) as PDO::PARAM_STR is supported****
****Retrieving time(7) as PDO::PARAM_LOB is supported****