-
Notifications
You must be signed in to change notification settings - Fork 374
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature request - add ReturnDatesAsStrings option to statement level …
…for sqlsrv (#844) * Added ReturnDatesAsStrings option to the statement level * Added new tests for ReturnDatesAsStrings at statement level * Added more datetime types as per review
- Loading branch information
Showing
7 changed files
with
453 additions
and
5 deletions.
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
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
120 changes: 120 additions & 0 deletions
120
test/functional/sqlsrv/sqlsrv_statement_datetimes_as_nulls.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,120 @@ | ||
--TEST-- | ||
Test retrieving null datetime values with statement option ReturnDatesAsStrings as true | ||
--DESCRIPTION-- | ||
Test retrieving null datetime values with statement option ReturnDatesAsStrings as true, | ||
which is false by default. Whether retrieved as strings or date time objects should return | ||
NULLs. | ||
--SKIPIF-- | ||
<?php require('skipif_versions_old.inc'); ?> | ||
--FILE-- | ||
<?php | ||
require_once('MsCommon.inc'); | ||
|
||
function testFetch($conn, $query, $columns, $withBuffer = false) | ||
{ | ||
// The statement option ReturnDatesAsStrings set to true | ||
// Test different fetching | ||
if ($withBuffer){ | ||
$options = array('Scrollable' => 'buffered', 'ReturnDatesAsStrings' => true); | ||
} else { | ||
$options = array('ReturnDatesAsStrings' => true); | ||
} | ||
|
||
$size = count($columns); | ||
$stmt = sqlsrv_prepare($conn, $query, array(), $options); | ||
// Fetch by getting one field at a time | ||
sqlsrv_execute($stmt); | ||
if( sqlsrv_fetch( $stmt ) === false) { | ||
fatalError("Failed in retrieving data\n"); | ||
} | ||
for ($i = 0; $i < $size; $i++) { | ||
$field = sqlsrv_get_field($stmt, $i); // expect string | ||
if (!is_null($field)) { | ||
echo "Expected null for column $columns[$i] but got: "; | ||
var_dump($field); | ||
} | ||
} | ||
|
||
// Fetch row as an object | ||
sqlsrv_execute($stmt); | ||
$object = sqlsrv_fetch_object($stmt); | ||
|
||
$objArray = (array)$object; // turn the object into an associated array | ||
for ($i = 0; $i < $size; $i++) { | ||
$col = $columns[$i]; | ||
$val = $objArray[$col]; | ||
|
||
if (!is_null($val)) { | ||
echo "Expected null for column $columns[$i] but got: "; | ||
var_dump($val); | ||
} | ||
} | ||
} | ||
|
||
function createTestTable($conn, $tableName, $columns) | ||
{ | ||
// Create the test table of date and time columns | ||
$dataTypes = array('date', 'smalldatetime', 'datetime', 'datetime2', 'datetimeoffset', 'time'); | ||
|
||
$colMeta = array(new AE\ColumnMeta($dataTypes[0], $columns[0]), | ||
new AE\ColumnMeta($dataTypes[1], $columns[1]), | ||
new AE\ColumnMeta($dataTypes[2], $columns[2]), | ||
new AE\ColumnMeta($dataTypes[3], $columns[3]), | ||
new AE\ColumnMeta($dataTypes[4], $columns[4]), | ||
new AE\ColumnMeta($dataTypes[5], $columns[5])); | ||
AE\createTable($conn, $tableName, $colMeta); | ||
|
||
// Insert null values | ||
$inputData = array($colMeta[0]->colName => null, | ||
$colMeta[1]->colName => null, | ||
$colMeta[2]->colName => null, | ||
$colMeta[3]->colName => null, | ||
$colMeta[4]->colName => null, | ||
$colMeta[5]->colName => null); | ||
$stmt = AE\insertRow($conn, $tableName, $inputData); | ||
if (!$stmt) { | ||
fatalError("Failed to insert data.\n"); | ||
} | ||
sqlsrv_free_stmt($stmt); | ||
} | ||
|
||
function runTest($tableName, $columns, $dateAsString) | ||
{ | ||
// Connect | ||
$conn = connect(array('ReturnDatesAsStrings' => $dateAsString)); | ||
if (!$conn) { | ||
fatalError("Could not connect.\n"); | ||
} | ||
|
||
$query = "SELECT * FROM $tableName"; | ||
testFetch($conn, $query, $columns); | ||
testFetch($conn, $query, $columns, true); | ||
|
||
sqlsrv_close($conn); | ||
} | ||
|
||
set_time_limit(0); | ||
sqlsrv_configure('WarningsReturnAsErrors', 1); | ||
|
||
$tableName = "TestNullDateTime"; | ||
$columns = array('c1', 'c2', 'c3', 'c4', 'c5', 'c6'); | ||
|
||
// Connect | ||
$conn = connect(); | ||
if (!$conn) { | ||
fatalError("Could not connect.\n"); | ||
} | ||
|
||
createTestTable($conn, $tableName, $columns); | ||
|
||
runTest($tableName, $columns, true); | ||
runTest($tableName, $columns, false); | ||
|
||
dropTable($conn, $tableName); | ||
|
||
sqlsrv_close($conn); | ||
|
||
echo "Done\n"; | ||
?> | ||
--EXPECT-- | ||
Done |
Oops, something went wrong.