From b701fab642450f1bcfcb72fa58ce51a4782775ff Mon Sep 17 00:00:00 2001 From: v-kaywon Date: Mon, 13 Mar 2017 17:54:58 -0700 Subject: [PATCH 1/4] implement throwing exception when ATTR_PERSISTENT is in the connection options; added tests --- source/pdo_sqlsrv/pdo_dbh.cpp | 6 ++- .../pdo_065_construct_persistent.phpt | 46 +++++++++++++++++++ .../pdo_065_construct_prefetch.phpt | 34 ++++++++++++++ 3 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 test/pdo_sqlsrv/pdo_065_construct_persistent.phpt create mode 100644 test/pdo_sqlsrv/pdo_065_construct_prefetch.phpt diff --git a/source/pdo_sqlsrv/pdo_dbh.cpp b/source/pdo_sqlsrv/pdo_dbh.cpp index 0d8e18be..6bea9c6e 100644 --- a/source/pdo_sqlsrv/pdo_dbh.cpp +++ b/source/pdo_sqlsrv/pdo_dbh.cpp @@ -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 ); diff --git a/test/pdo_sqlsrv/pdo_065_construct_persistent.phpt b/test/pdo_sqlsrv/pdo_065_construct_persistent.phpt new file mode 100644 index 00000000..361a9625 --- /dev/null +++ b/test/pdo_sqlsrv/pdo_065_construct_persistent.phpt @@ -0,0 +1,46 @@ +--TEST-- +Exception is thrown if the unsupported attribute ATTR_PERSISTENT is put into the connection options +--SKIPIF-- +--FILE-- + 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 diff --git a/test/pdo_sqlsrv/pdo_065_construct_prefetch.phpt b/test/pdo_sqlsrv/pdo_065_construct_prefetch.phpt new file mode 100644 index 00000000..c3a90cfc --- /dev/null +++ b/test/pdo_sqlsrv/pdo_065_construct_prefetch.phpt @@ -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-- + 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 From c9492ae487319e2156df9224e19b2e71996bd690 Mon Sep 17 00:00:00 2001 From: v-kaywon Date: Fri, 17 Mar 2017 16:46:02 -0700 Subject: [PATCH 2/4] fix memory leak caused by pecalloced persistent connection; fix test indentation --- source/pdo_sqlsrv/pdo_dbh.cpp | 1 + .../pdo_065_construct_persistent.phpt | 32 +++++++++---------- .../pdo_065_construct_prefetch.phpt | 18 +++++------ 3 files changed, 26 insertions(+), 25 deletions(-) diff --git a/source/pdo_sqlsrv/pdo_dbh.cpp b/source/pdo_sqlsrv/pdo_dbh.cpp index 6bea9c6e..5323058f 100644 --- a/source/pdo_sqlsrv/pdo_dbh.cpp +++ b/source/pdo_sqlsrv/pdo_dbh.cpp @@ -433,6 +433,7 @@ int pdo_sqlsrv_db_handle_factory(pdo_dbh_t *dbh, zval *driver_options TSRMLS_DC) } // 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 ) { + dbh->refcount--; throw pdo::PDOException(); } diff --git a/test/pdo_sqlsrv/pdo_065_construct_persistent.phpt b/test/pdo_sqlsrv/pdo_065_construct_persistent.phpt index 361a9625..0f0fc5a0 100644 --- a/test/pdo_sqlsrv/pdo_065_construct_persistent.phpt +++ b/test/pdo_sqlsrv/pdo_065_construct_persistent.phpt @@ -6,33 +6,33 @@ Exception is thrown if the unsupported attribute ATTR_PERSISTENT is put into the include 'pdo_tools.inc'; require_once("autonomous_setup.php"); $database = "tempdb"; -$dsn = "sqlsrv:Server = $serverName;Database = $database;"; +$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); + $attr = array(PDO::ATTR_PERSISTENT => true); $conn = new PDO( $dsn, $username, $password, $attr); - //free the statement and connection - $stmt=null; - $conn=null; + //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"; - } + 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); diff --git a/test/pdo_sqlsrv/pdo_065_construct_prefetch.phpt b/test/pdo_sqlsrv/pdo_065_construct_prefetch.phpt index c3a90cfc..ddd8f0c9 100644 --- a/test/pdo_sqlsrv/pdo_065_construct_prefetch.phpt +++ b/test/pdo_sqlsrv/pdo_065_construct_prefetch.phpt @@ -6,20 +6,20 @@ Exception is thrown for the unsupported connection attribute ATTR_PREFETCH only include 'pdo_tools.inc'; require_once("autonomous_setup.php"); $database = "tempdb"; -$dsn = "sqlsrv:Server = $serverName;Database = $database;"; +$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); + $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 "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; + 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"; From d289754362a9fa8453c00b89007209b1ec2ee62e Mon Sep 17 00:00:00 2001 From: v-kaywon Date: Mon, 20 Mar 2017 12:41:36 -0700 Subject: [PATCH 3/4] Update pdo_065_construct_prefetch.phpt --- test/pdo_sqlsrv/pdo_065_construct_prefetch.phpt | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/test/pdo_sqlsrv/pdo_065_construct_prefetch.phpt b/test/pdo_sqlsrv/pdo_065_construct_prefetch.phpt index ddd8f0c9..fbb68d79 100644 --- a/test/pdo_sqlsrv/pdo_065_construct_prefetch.phpt +++ b/test/pdo_sqlsrv/pdo_065_construct_prefetch.phpt @@ -9,16 +9,15 @@ $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"; + $conn=null; 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; + //free the connection $conn=null; } catch( PDOException $e ) { From 20198804bb3da468758b65e83baeb42d19a38109 Mon Sep 17 00:00:00 2001 From: v-kaywon Date: Mon, 20 Mar 2017 12:42:31 -0700 Subject: [PATCH 4/4] Update pdo_065_construct_persistent.phpt --- test/pdo_sqlsrv/pdo_065_construct_persistent.phpt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/pdo_sqlsrv/pdo_065_construct_persistent.phpt b/test/pdo_sqlsrv/pdo_065_construct_persistent.phpt index 0f0fc5a0..ca925d39 100644 --- a/test/pdo_sqlsrv/pdo_065_construct_persistent.phpt +++ b/test/pdo_sqlsrv/pdo_065_construct_persistent.phpt @@ -13,8 +13,7 @@ try{ $attr = array(PDO::ATTR_PERSISTENT => true); $conn = new PDO( $dsn, $username, $password, $attr); - //free the statement and connection - $stmt=null; + //free the connection $conn=null; } catch( PDOException $e ) { @@ -33,6 +32,9 @@ try{ if ($result['c1'] == 1 && $result['c2'] == 'column2') { echo "Test successfully"; } + //free the statement and connection + $stmt = null; + $conn = null; } catch( PDOException $e ) { var_dump( $e);