Skip to content

Commit

Permalink
Added new test cases for Issue 1310 (#1321)
Browse files Browse the repository at this point in the history
  • Loading branch information
yitam authored Oct 27, 2021
1 parent 8de0978 commit e3042e1
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 0 deletions.
88 changes: 88 additions & 0 deletions test/functional/pdo_sqlsrv/pdo_1310_null_varchar.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
--TEST--
GitHub issue 1310 - bind null field as varchar(max) if not binary
--DESCRIPTION--
The test shows null fields are no longer bound as char(1) if not binary such that it solves both issues 1310 and 1102.
--ENV--
PHPT_EXEC=true
--SKIPIF--
<?php require('skipif.inc'); ?>
--FILE--
<?php
require_once("MsSetup.inc");
require_once("MsCommon_mid-refactor.inc");

try {
$conn = connect();
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// Issue 1310
$query = "SELECT CAST(ISNULL(:K, -1) AS INT) AS K";
$k = null;

$stmt = $conn->prepare($query);
$stmt->bindParam(':K', $k, PDO::PARAM_NULL);
$stmt->execute();

$row = $stmt->fetchAll(PDO::FETCH_ASSOC);
var_dump($row);

$stmt->bindParam(':K', $k, PDO::PARAM_INT);
$stmt->execute();

$row = $stmt->fetchAll(PDO::FETCH_NUM);
var_dump($row);

// Issue 1102
$query = "DECLARE @d DATETIME = ISNULL(:K, GETDATE()); SELECT @d AS D;";
$k = null;

$stmt = $conn->prepare($query);
$stmt->bindParam(':K', $k, PDO::PARAM_NULL);
$stmt->execute();

$row = $stmt->fetchAll(PDO::FETCH_NUM);
var_dump($row);

$stmt->bindParam(':K', $k, PDO::PARAM_INT);
$stmt->execute();

$row = $stmt->fetchAll(PDO::FETCH_ASSOC);
var_dump($row);

echo "Done\n";
} catch (PdoException $e) {
echo $e->getMessage();
}

?>
--EXPECTREGEX--
array\(1\) {
\[0\]=>
array\(1\) {
\["K"\]=>
string\(2\) "-1"
}
}
array\(1\) {
\[0\]=>
array\(1\) {
\[0\]=>
string\(2\) "-1"
}
}
array\(1\) {
\[0\]=>
array\(1\) {
\[0\]=>
string\(23\) "20[0-9]{2}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:00.000"
}
}
array\(1\) {
\[0\]=>
array\(1\) {
\["D"\]=>
string\(23\) "20[0-9]{2}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:00.000"
}
}
Done

64 changes: 64 additions & 0 deletions test/functional/sqlsrv/srv_1310_null_varchar.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
--TEST--
GitHub issue 1310 - bind null field as varchar(max) if not binary
--DESCRIPTION--
The test shows null fields are no longer bound as char(1) if not binary such that it solves both issues 1310 and 1102.
--ENV--
PHPT_EXEC=true
--SKIPIF--
<?php require('skipif_versions_old.inc'); ?>
--FILE--
<?php
require_once('MsCommon.inc');

$conn = AE\connect();

// Issue 1310
$query = "SELECT CAST(ISNULL(?, -1) AS INT) AS K";
$k = null;
$params = array($k);

$stmt = sqlsrv_prepare($conn, $query, $params);
if (!$stmt) {
fatalError("Failed to prepare statement (1).");
}
if (!sqlsrv_execute($stmt)) {
fatalError("Failed to execute query (1).");
}

while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
var_dump($row);
}

sqlsrv_free_stmt($stmt);

// Issue 1102
$query = "DECLARE @d DATETIME = ISNULL(?, GETDATE()); SELECT @d AS D;";
$k = null;
$params = array($k, null, null, null);

$options = array('ReturnDatesAsStrings'=> true);
$stmt = sqlsrv_query($conn, $query, $params, $options);
if (!$stmt) {
fatalError("Failed to query statement (2).");
}

while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
var_dump($row);
}

sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);

echo "Done\n";

?>
--EXPECTREGEX--
array\(1\) {
\["K"\]=>
int\(-1\)
}
array\(1\) {
\["D"\]=>
string\(23\) "20[0-9]{2}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:00.000"
}
Done

0 comments on commit e3042e1

Please sign in to comment.