From 5a395d2e95cefdd04d724168fe1f3fd52ea6f762 Mon Sep 17 00:00:00 2001 From: yitam Date: Wed, 5 Apr 2017 13:22:39 -0700 Subject: [PATCH] modified helper methods for temp tables/procs --- .../pdo_065_construct_persistent.phpt | 2 +- .../pdo_fetch_complex_transactions.phpt | 2 +- test/pdo_sqlsrv/pdo_tools.inc | 35 +++++++++++++------ .../sqlsrv_fetch_complex_transactions.phpt | 2 +- test/sqlsrv/sqlsrv_param_query_errors.phpt | 8 ++--- .../sqlsrv_param_query_invalid_inputs.phpt | 6 ++-- ...v_statement_query_timeout_transaction.phpt | 2 +- test/sqlsrv/tools.inc | 35 +++++++++++++------ 8 files changed, 59 insertions(+), 33 deletions(-) diff --git a/test/pdo_sqlsrv/pdo_065_construct_persistent.phpt b/test/pdo_sqlsrv/pdo_065_construct_persistent.phpt index ca925d39..fec3331a 100644 --- a/test/pdo_sqlsrv/pdo_065_construct_persistent.phpt +++ b/test/pdo_sqlsrv/pdo_065_construct_persistent.phpt @@ -22,7 +22,7 @@ catch( PDOException $e ) { } try{ echo "\nTesting new connection after exception thrown in previous connection...\n"; - $tableName1 = GetTempTableName('tab1'); + $tableName1 = GetTempTableName('tab1', false); $conn = new PDO( $dsn, $username, $password ); $sql = "CREATE TABLE $tableName1 (c1 int, c2 varchar(10))"; $stmt = $conn->query($sql); diff --git a/test/pdo_sqlsrv/pdo_fetch_complex_transactions.phpt b/test/pdo_sqlsrv/pdo_fetch_complex_transactions.phpt index 3a29ffa6..f74c5f9a 100644 --- a/test/pdo_sqlsrv/pdo_fetch_complex_transactions.phpt +++ b/test/pdo_sqlsrv/pdo_fetch_complex_transactions.phpt @@ -114,7 +114,7 @@ function RunTest() $conn2 = new PDO( "sqlsrv:server=$serverName;Database=$database", $username, $password); $conn2->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); - $tableName = GetTempTableName('testTransaction'); + $tableName = GetTempTableName('testTransaction', false); // ComplexTransaction() returns number of rows left in $tableName $numRows = ComplexTransaction($conn, $tableName); diff --git a/test/pdo_sqlsrv/pdo_tools.inc b/test/pdo_sqlsrv/pdo_tools.inc index b40ad406..add9c6d4 100644 --- a/test/pdo_sqlsrv/pdo_tools.inc +++ b/test/pdo_sqlsrv/pdo_tools.inc @@ -17,27 +17,40 @@ function EndTest($testName) echo "...Test '$testName' completed successfully.\n"; } -function GetTempTableName($table = '') +function GetTempTableName($table = '', $temporary = true) { // A temporary table name with the '#' prefix will be automatically - // dropped once the connection is closed - $random = mt_rand(1,1000); + // dropped once the connection is closed. Otherwise, the caller + // should take care of dropping the temp table afterwards. + $timestamp = round(microtime(true)*1000); + + $prefix = ''; + if ($temporary) + $prefix = '#'; + if (strlen($table) == 0) - return "#php_test_table" . '_' . $timestamp . '_' . $random; - else - return $table . $timestamp; + $table = 'php_test_table'; + + return $prefix . $table . '_' . $timestamp; } -function GetTempProcName($proc = '') +function GetTempProcName($proc = '', $temporary = true) { // A temporary stored procedure name with the '#' prefix will be - // automatically dropped once the connection is closed + // automatically dropped once the connection is closed. Otherwise, + // the caller should take care of dropping the temp procedure afterwards. + $timestamp = round(microtime(true)*1000); + + $prefix = ''; + if ($temporary) + $prefix = '#'; + if (strlen($proc) == 0) - return "#php_test_proc" . $timestamp; - else - return $proc . $timestamp; + $proc = 'php_test_proc'; + + return $prefix . $proc . '_' . $timestamp; } function CompareNumericData($actual, $expected) diff --git a/test/sqlsrv/sqlsrv_fetch_complex_transactions.phpt b/test/sqlsrv/sqlsrv_fetch_complex_transactions.phpt index e3e27ab8..696f3cd9 100644 --- a/test/sqlsrv/sqlsrv_fetch_complex_transactions.phpt +++ b/test/sqlsrv/sqlsrv_fetch_complex_transactions.phpt @@ -6,7 +6,7 @@ include 'tools.inc'; function ComplexTransaction($conn, $conn2) { - $tableName = GetTempTableName('testTransaction'); + $tableName = GetTempTableName('testTransaction', false); $stmt = sqlsrv_query($conn, "CREATE TABLE $tableName ([c1_int] int, [c2_real] real)"); sqlsrv_free_stmt($stmt); diff --git a/test/sqlsrv/sqlsrv_param_query_errors.phpt b/test/sqlsrv/sqlsrv_param_query_errors.phpt index 3ed6c050..43d2d95b 100644 --- a/test/sqlsrv/sqlsrv_param_query_errors.phpt +++ b/test/sqlsrv/sqlsrv_param_query_errors.phpt @@ -6,7 +6,7 @@ include 'tools.inc'; function ParamQueryError_PhpType_Mismatch($conn) { - $tableName = GetTempTableName(); + $tableName = GetTempTableName('PhpType_Mismatch'); $stmt = sqlsrv_query($conn, "CREATE TABLE $tableName ([c1_int] int, [c2_varchar_max] varchar(max))"); sqlsrv_free_stmt($stmt); @@ -30,7 +30,7 @@ function ParamQueryError_PhpType_Mismatch($conn) function ParamQueryError_Dir_Invalid($conn) { - $tableName = GetTempTableName(); + $tableName = GetTempTableName('Dir_Invalid'); $stmt = sqlsrv_query($conn, "CREATE TABLE $tableName ([c1_int] int, [c2_varchar_max] varchar(max))"); sqlsrv_free_stmt($stmt); @@ -46,7 +46,7 @@ function ParamQueryError_Dir_Invalid($conn) function ParamQueryError_PhpType_Encoding($conn) { - $tableName = GetTempTableName(); + $tableName = GetTempTableName('PhpType_Encoding'); $stmt = sqlsrv_query($conn, "CREATE TABLE $tableName ([c1_int] int, [c2_varchar_max] varchar(max))"); sqlsrv_free_stmt($stmt); @@ -57,7 +57,7 @@ function ParamQueryError_PhpType_Encoding($conn) function ParamQueryError_PhpType_Invalid($conn) { - $tableName = GetTempTableName(); + $tableName = GetTempTableName('PhpType_Invalid'); $stmt = sqlsrv_query($conn, "CREATE TABLE $tableName ([c1_int] int, [c2_varchar_max] varchar(max))"); sqlsrv_free_stmt($stmt); diff --git a/test/sqlsrv/sqlsrv_param_query_invalid_inputs.phpt b/test/sqlsrv/sqlsrv_param_query_invalid_inputs.phpt index 36d0654b..49545509 100644 --- a/test/sqlsrv/sqlsrv_param_query_invalid_inputs.phpt +++ b/test/sqlsrv/sqlsrv_param_query_invalid_inputs.phpt @@ -6,7 +6,7 @@ include 'tools.inc'; function ParamQueryError_MinMaxScale($conn) { - $tableName = GetTempTableName(); + $tableName = GetTempTableName('MinMaxScale'); $stmt = sqlsrv_query($conn, "CREATE TABLE $tableName ([c1_int] int, [c2_decimal] decimal(28,4), [c3_numeric] numeric(32,4))"); sqlsrv_free_stmt($stmt); @@ -20,7 +20,7 @@ function ParamQueryError_MinMaxScale($conn) function ParamQueryError_MinMaxSize($conn) { - $tableName = GetTempTableName(); + $tableName = GetTempTableName('MinMaxSize'); $stmt = sqlsrv_query($conn, "CREATE TABLE $tableName ([c1_int] int, [c2_varchar_max] varchar(max))"); sqlsrv_free_stmt($stmt); @@ -34,7 +34,7 @@ function ParamQueryError_MinMaxSize($conn) function ParamQueryError_MinMaxPrecision($conn) { - $tableName = GetTempTableName(); + $tableName = GetTempTableName('MinMaxPrecision'); $stmt = sqlsrv_query($conn, "CREATE TABLE $tableName ([c1_int] int, [c2_decimal] decimal(28,4), [c3_numeric] numeric(32,4))"); sqlsrv_free_stmt($stmt); diff --git a/test/sqlsrv/sqlsrv_statement_query_timeout_transaction.phpt b/test/sqlsrv/sqlsrv_statement_query_timeout_transaction.phpt index 9eacc6ac..4fdb8b28 100644 --- a/test/sqlsrv/sqlsrv_statement_query_timeout_transaction.phpt +++ b/test/sqlsrv/sqlsrv_statement_query_timeout_transaction.phpt @@ -6,7 +6,7 @@ include 'tools.inc'; function QueryTimeout($conn1, $conn2, $commit) { - $tableName = GetTempTableName('testQueryTimeout'); + $tableName = GetTempTableName('testQueryTimeout', false); $stmt = sqlsrv_query($conn1, "CREATE TABLE $tableName ([c1_int] int, [c2_tinyint] tinyint, [c3_smallint] smallint, [c4_bigint] bigint, [c5_bit] bit, [c6_float] float, [c7_real] real, [c8_decimal] decimal(28,4), [c9_numeric] numeric(32,4), [c10_money] money, [c11_smallmoney] smallmoney)"); sqlsrv_free_stmt($stmt); diff --git a/test/sqlsrv/tools.inc b/test/sqlsrv/tools.inc index c9fbcf5b..fba442f0 100644 --- a/test/sqlsrv/tools.inc +++ b/test/sqlsrv/tools.inc @@ -43,27 +43,40 @@ function EndTest($testName) echo "...Test '$testName' completed successfully.\n"; } -function GetTempTableName($table = '') +function GetTempTableName($table = '', $temporary = true) { // A temporary table name with the '#' prefix will be automatically - // dropped once the connection is closed - $random = mt_rand(1,1000); + // dropped once the connection is closed. Otherwise, the caller + // should take care of dropping the temp table afterwards. + $timestamp = round(microtime(true)*1000); + + $prefix = ''; + if ($temporary) + $prefix = '#'; + if (strlen($table) == 0) - return "#php_test_table" . '_' . $timestamp . '_' . $random; - else - return $table . $timestamp; + $table = 'php_test_table'; + + return $prefix . $table . '_' . $timestamp; } -function GetTempProcName($proc = '') +function GetTempProcName($proc = '', $temporary = true) { // A temporary stored procedure name with the '#' prefix will be - // automatically dropped once the connection is closed + // automatically dropped once the connection is closed. Otherwise, + // the caller should take care of dropping the temp procedure afterwards. + $timestamp = round(microtime(true)*1000); + + $prefix = ''; + if ($temporary) + $prefix = '#'; + if (strlen($proc) == 0) - return "#php_test_proc" . $timestamp; - else - return $proc . $timestamp; + $proc = 'php_test_proc'; + + return $prefix . $proc . '_' . $timestamp; } function FatalError($errorMsg)