Skip to content

Commit

Permalink
implement throwing exception when ATTR_PERSISTENT is in the connectio…
Browse files Browse the repository at this point in the history
…n options; added tests
  • Loading branch information
yukiwongky committed Mar 14, 2017
1 parent 9bd42e4 commit b701fab
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
6 changes: 5 additions & 1 deletion source/pdo_sqlsrv/pdo_dbh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down
46 changes: 46 additions & 0 deletions test/pdo_sqlsrv/pdo_065_construct_persistent.phpt
Original file line number Diff line number Diff line change
@@ -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
34 changes: 34 additions & 0 deletions test/pdo_sqlsrv/pdo_065_construct_prefetch.phpt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit b701fab

Please sign in to comment.