implement throwing exception when ATTR_PERSISTENT is in the connection options; added tests

This commit is contained in:
v-kaywon 2017-03-13 17:54:58 -07:00
parent 9bd42e4060
commit b701fab642
3 changed files with 85 additions and 1 deletions

View file

@ -431,7 +431,11 @@ int pdo_sqlsrv_db_handle_factory(pdo_dbh_t *dbh, zval *driver_options TSRMLS_DC)
CHECK_CUSTOM_ERROR( driver_options && Z_TYPE_P( driver_options ) != IS_ARRAY, *g_henv_cp, SQLSRV_ERROR_CONN_OPTS_WRONG_TYPE ) {
throw core::CoreException();
}
// throws PDOException if the ATTR_PERSISTENT is in connection options
CHECK_CUSTOM_ERROR( dbh->is_persistent, *g_henv_cp, PDO_SQLSRV_ERROR_UNSUPPORTED_DBH_ATTR ) {
throw pdo::PDOException();
}
// Initialize the options array to be passed to the core layer
ALLOC_HASHTABLE( pdo_conn_options_ht );

View file

@ -0,0 +1,46 @@
--TEST--
Exception is thrown if the unsupported attribute ATTR_PERSISTENT is put into the connection options
--SKIPIF--
--FILE--
<?php
include 'pdo_tools.inc';
require_once("autonomous_setup.php");
$database = "tempdb";
$dsn = "sqlsrv:Server = $serverName;Database = $database;";
try{
echo "Testing a connection with ATTR_PERSISTENT...\n";
// setting PDO::ATTR_PERSISTENT in PDO constructor returns an exception
$attr = array(PDO::ATTR_PERSISTENT => true);
$conn = new PDO( $dsn, $username, $password, $attr);
//free the statement and connection
$stmt=null;
$conn=null;
}
catch( PDOException $e ) {
echo "Exception from unsupported attribute (ATTR_PERSISTENT) is caught\n";
//exit;
}
try{
echo "\nTesting new connection after exception thrown in previous connection...\n";
$tableName1 = GetTempTableName('tab1');
$conn = new PDO( $dsn, $username, $password );
$sql = "CREATE TABLE $tableName1 (c1 int, c2 varchar(10))";
$stmt = $conn->query($sql);
$ret = $conn->exec("INSERT INTO $tableName1 VALUES(1, 'column2')");
$stmt = $conn->query("SELECT * FROM $tableName1");
$result = $stmt->fetch(PDO::FETCH_ASSOC);
if ($result['c1'] == 1 && $result['c2'] == 'column2') {
echo "Test successfully";
}
}
catch( PDOException $e ) {
var_dump( $e);
}
?>
--EXPECT--
Testing a connection with ATTR_PERSISTENT...
Exception from unsupported attribute (ATTR_PERSISTENT) is caught
Testing new connection after exception thrown in previous connection...
Test successfully

View file

@ -0,0 +1,34 @@
--TEST--
Exception is thrown for the unsupported connection attribute ATTR_PREFETCH only if it is set after PDO::ERRMODE_EXCEPTION is turned on
--SKIPIF--
--FILE--
<?php
include 'pdo_tools.inc';
require_once("autonomous_setup.php");
$database = "tempdb";
$dsn = "sqlsrv:Server = $serverName;Database = $database;";
try{
echo "Testing a connection with ATTR_PREFETCH before ERRMODE_EXCEPTION...\n";
// setting PDO::ATTR_PERSISTENT in PDO constructor returns an exception
$attr = array(PDO::ATTR_PREFETCH => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
$conn = new PDO( $dsn, $username, $password, $attr);
echo "Error from supported attribute (ATTR_PREFETCH) is silented\n\n";
echo "Testing a connection with ATTR_PREFETCH after ERRMODE_EXCEPTION...\n";
$attr = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_PREFETCH => true);
$conn = new PDO( $dsn, $username, $password, $attr);
//free the statement and connection
$stmt=null;
$conn=null;
}
catch( PDOException $e ) {
echo "Exception from unsupported attribute (ATTR_PREFETCH) is caught\n";
//exit;
}
?>
--EXPECT--
Testing a connection with ATTR_PREFETCH before ERRMODE_EXCEPTION...
Error from supported attribute (ATTR_PREFETCH) is silented
Testing a connection with ATTR_PREFETCH after ERRMODE_EXCEPTION...
Exception from unsupported attribute (ATTR_PREFETCH) is caught