From 0a2e6ad24256f805164895fe2b9f21ae4ddbd56a Mon Sep 17 00:00:00 2001 From: Jenny Tam Date: Tue, 28 Nov 2017 16:40:49 -0800 Subject: [PATCH] Refactoring sqlsrv tests with parameterized queries --- test/functional/sqlsrv/0020.phpt | 110 ++-- test/functional/sqlsrv/0060.phpt | 7 +- test/functional/sqlsrv/0061.phpt | 59 +-- test/functional/sqlsrv/0065.phpt | 241 ++++----- test/functional/sqlsrv/0066.phpt | 40 +- test/functional/sqlsrv/0067.phpt | 29 +- test/functional/sqlsrv/0068.phpt | 74 +-- test/functional/sqlsrv/0069.phpt | 99 ++-- test/functional/sqlsrv/0072.phpt | 20 +- test/functional/sqlsrv/0073.phpt | 20 +- test/functional/sqlsrv/0074.phpt | 107 ++-- test/functional/sqlsrv/0075.phpt | 34 +- test/functional/sqlsrv/0076.phpt | 56 +- test/functional/sqlsrv/0078.phpt | 501 +++++++++--------- test/functional/sqlsrv/0079.phpt | 32 +- test/functional/sqlsrv/MsCommon.inc | 23 +- test/functional/sqlsrv/MsHelper.inc | 42 +- test/functional/sqlsrv/fix_test_182741.phpt | 47 +- .../sqlsrv/sqlsrv_2008_dataTypes.phpt | 99 ++-- .../sqlsrv/sqlsrv_378_out_param_error.phpt | 32 +- .../sqlsrv/sqlsrv_bind_param_out_string.phpt | 57 +- .../sqlsrv/sqlsrv_bool_outparam.phpt | 7 +- .../sqlsrv_input_param_unknown_encoding.phpt | 90 ++-- .../sqlsrv/sqlsrv_param_input_variants.phpt | 127 +++-- .../sqlsrv_param_ints_with_deletes.phpt | 49 +- .../sqlsrv/sqlsrv_param_output_variants.phpt | 129 ++--- .../sqlsrv/sqlsrv_param_query_data_types.phpt | 73 +-- .../sqlsrv/sqlsrv_param_query_errors.phpt | 115 ++-- .../sqlsrv_param_query_invalid_inputs.phpt | 141 ++--- test/functional/sqlsrv/sqlsrv_query.phpt | 42 +- .../sqlsrv/sqlsrv_send_stream_data.phpt | 151 +++--- .../sqlsrv/sqlsrv_streams_empty_char.phpt | 136 ++--- .../sqlsrv/sqlsrv_streams_null_binary.phpt | 96 ++-- .../sqlsrv/sqlsrv_streams_null_nchar.phpt | 74 +-- .../sqlsrv/srv_036_transaction_commit.phpt | 71 ++- 35 files changed, 1562 insertions(+), 1468 deletions(-) diff --git a/test/functional/sqlsrv/0020.phpt b/test/functional/sqlsrv/0020.phpt index 44847a2c..e6e08096 100644 --- a/test/functional/sqlsrv/0020.phpt +++ b/test/functional/sqlsrv/0020.phpt @@ -1,28 +1,27 @@ --TEST-- reading streams of various types with a base64 decoding filter on top of them. --SKIPIF-- - + --FILE-- $data)); + } else { + $insertQuery = $params['insertQuery']; + $stmt = sqlsrv_query($conn, $insertQuery, array($data)); + } + + if ($stmt) { + do { $read = sqlsrv_send_stream_data($stmt); - if ($read === false) die(print_r(sqlsrv_errors(), true)); + if ($read === false) { + die(print_r(sqlsrv_errors(), true)); + } } while ($read); fclose($data) || die(print_r(error_get_last(), true)); sqlsrv_free_stmt($stmt) || die(print_r(sqlsrv_errors(), true)); - } else + } else { die(print_r(sqlsrv_errors(), true)); + } - return fopen($params['testImageURL'], "rb"); + return fopen($params['testImageURL'], "rb"); } -function PrepareParams(&$arr) { - $uname = php_uname(); - $phpgif = "\\php.gif"; - if (preg_match('/Win/',$uname)) - { +function prepareParams(&$arr) +{ + if (IsWindows()) { $phpgif = '\\php.gif'; - } - else // other than Windows - { + } else { $phpgif = '/php.gif'; } - $arr['tableName'] = $tblName = "dbo.B64TestTable"; + + // the test table always has a column of type 'nvarchar(MAX)' + $arr['tableName'] = $tblName = "B64TestTable"; $arr['columnName'] = $colName = "Base64Image"; $arr['fieldType'] = $fieldType = "nvarchar(MAX)"; - $arr['dropQuery'] = "IF OBJECT_ID(N'$tblName', N'U') IS NOT NULL DROP TABLE $tblName"; - $arr['createQuery'] = "CREATE TABLE $tblName ($colName $fieldType)"; $arr['insertQuery'] = "INSERT INTO $tblName ($colName) VALUES (?)"; $arr['selectQuery'] = "SELECT TOP 1 $colName FROM $tblName"; - // $arr['testImageURL'] = "http://static.php.net/www.php.net/images/php.gif"; - $arr['testImageURL'] = dirname( $_SERVER['PHP_SELF'] ).$phpgif; // use this when no http access - $arr['MIMEType'] = "image/gif"; -} - -function DropTestTable($conn, $params) { RunQuery($conn, $params['dropQuery']); } -function CreateTestTable($conn, $params) { RunQuery($conn, $params['createQuery']); } -function RunQuery($conn, $query) { - ($qStmt = sqlsrv_query($conn, $query)) && $qStmt && sqlsrv_free_stmt($qStmt) - || die(print_r(sqlsrv_errors(), true)); + $arr['testImageURL'] = dirname($_SERVER['PHP_SELF']) . $phpgif; // use this when no http access } ?> diff --git a/test/functional/sqlsrv/0060.phpt b/test/functional/sqlsrv/0060.phpt index afffaf24..5af735e8 100644 --- a/test/functional/sqlsrv/0060.phpt +++ b/test/functional/sqlsrv/0060.phpt @@ -1,7 +1,7 @@ --TEST-- binding parameters, including output parameters, using the simplified syntax. --SKIPIF-- - + --FILE-- + --FILE-- diff --git a/test/functional/sqlsrv/0065.phpt b/test/functional/sqlsrv/0065.phpt index f7042861..9031b796 100644 --- a/test/functional/sqlsrv/0065.phpt +++ b/test/functional/sqlsrv/0065.phpt @@ -1,46 +1,41 @@ --TEST-- inserting and retrieving UTF-8 text. --SKIPIF-- - + --FILE-- + --FILE-- + --FILE-- + --FILE-- ---EXPECTREGEX-- -sqlsrv_execute\(3\) failed -Array -\( - \[0\] => Array - \( - \[0\] => 42000 - \[SQLSTATE\] => 42000 - \[1\] => 257 - \[code\] => 257 - \[2\] => \[Microsoft\]\[ODBC Driver 1[1-9] for SQL Server\]\[SQL Server\]Implicit conversion from data type varchar\(max\) to varbinary\(max\) is not allowed\. Use the CONVERT function to run this query\. - \[message\] => \[Microsoft\]\[ODBC Driver 1[1-9] for SQL Server\]\[SQL Server\]Implicit conversion from data type varchar\(max\) to varbinary\(max\) is not allowed\. Use the CONVERT function to run this query\. - \) - - \[1\] => Array - \( - \[0\] => 42000 - \[SQLSTATE\] => 42000 - \[1\] => 8180 - \[code\] => 8180 - \[2\] => \[Microsoft\]\[ODBC Driver 1[1-9] for SQL Server\]\[SQL Server\]Statement\(s\) could not be prepared\. - \[message\] => \[Microsoft\]\[ODBC Driver 1[1-9] for SQL Server\]\[SQL Server\]Statement\(s\) could not be prepared\. - \) - -\) +--EXPECT-- +sqlsrv_execute failed +Done diff --git a/test/functional/sqlsrv/0069.phpt b/test/functional/sqlsrv/0069.phpt index 5ec3164a..d7a78e71 100644 --- a/test/functional/sqlsrv/0069.phpt +++ b/test/functional/sqlsrv/0069.phpt @@ -1,54 +1,77 @@ --TEST-- Variety of connection parameters. --SKIPIF-- - + --FILE-- + --FILE-- '.$guid."<\n"; - while( sqlsrv_next_result( $stmt ) != NULL ) { + while (sqlsrv_next_result($stmt) != null) { } echo 'New Guid: >'.$guid."<\n"; diff --git a/test/functional/sqlsrv/0073.phpt b/test/functional/sqlsrv/0073.phpt index 30fddf9b..56f60e5c 100644 --- a/test/functional/sqlsrv/0073.phpt +++ b/test/functional/sqlsrv/0073.phpt @@ -1,7 +1,7 @@ --TEST-- For output string parameter crash when output variable is set initially to null --SKIPIF-- - + --FILE-- '.$guid."<\n"; - while( sqlsrv_next_result( $stmt ) != NULL ) { + while (sqlsrv_next_result($stmt) != null) { } echo 'New Guid: >'.$guid."<\n"; diff --git a/test/functional/sqlsrv/0074.phpt b/test/functional/sqlsrv/0074.phpt index 9cc571bc..877d4262 100644 --- a/test/functional/sqlsrv/0074.phpt +++ b/test/functional/sqlsrv/0074.phpt @@ -1,44 +1,48 @@ --TEST-- output string parameters with rows affected return results before output parameter. --SKIPIF-- - + --FILE-- $id, 'Intro' => $intro)); + if ($stmt === false) { + die(print_r(sqlsrv_errors(), true)); + } } -$stmt = sqlsrv_query( $conn, "IF OBJECT_ID('Subjects', 'U') IS NOT NULL DROP TABLE Subjects" ); -$stmt = sqlsrv_query( $conn, "IF OBJECT_ID('sn_x_study', 'U') IS NOT NULL DROP TABLE sn_x_study" ); -$stmt = sqlsrv_query( $conn, "IF OBJECT_ID('Studies', 'U') IS NOT NULL DROP TABLE Studies" ); -$stmt = sqlsrv_query( $conn, "IF OBJECT_ID('sp_MakeSubject', 'P') IS NOT NULL DROP PROCEDURE sp_MakeSubject" ); +require_once('MsCommon.inc'); +$conn = AE\connect(); -$stmt = sqlsrv_query( $conn, "CREATE TABLE Subjects (StartTime datetime, sn nchar(32), extref nvarchar(50))" ); -if( $stmt === false ) { - die( print_r( sqlsrv_errors(), true )); -} -$stmt = sqlsrv_query( $conn, "CREATE TABLE sn_x_study (studyID int, sn nchar(32))" ); -if( $stmt === false ) { - die( print_r( sqlsrv_errors(), true )); -} -$stmt = sqlsrv_query( $conn, "CREATE TABLE Studies (studyID int, Intro nvarchar(max))" ); -if( $stmt === false ) { - die( print_r( sqlsrv_errors(), true )); -} -$stmt = sqlsrv_query( $conn, "INSERT INTO Studies (studyID, Intro) VALUES (1, 'Test class 1')" ); -if( $stmt === false ) { - die( print_r( sqlsrv_errors(), true )); -} -$stmt = sqlsrv_query( $conn, "INSERT INTO Studies (studyID, Intro) VALUES (2, 'Test class 2')" ); -if( $stmt === false ) { - die( print_r( sqlsrv_errors(), true )); -} -$stmt = sqlsrv_query( $conn, "INSERT INTO Studies (studyID, Intro) VALUES (3, 'Test class 3')" ); -if( $stmt === false ) { - die( print_r( sqlsrv_errors(), true )); +// drop the procedure if exists +$stmt = sqlsrv_query($conn, "IF OBJECT_ID('sp_MakeSubject', 'P') IS NOT NULL DROP PROCEDURE sp_MakeSubject"); + +// Create Table 'Subjects' but do not encrypt the first column because in the stored procedure +// we rely on the server to get the current date time. With Column Encryption, all input values +// have to be provided by the client +$columns = array(new AE\ColumnMeta('datetime', 'StartTime', null, true, true), + new AE\ColumnMeta('nchar(32)', 'sn'), + new AE\ColumnMeta('nvarchar(50)', 'extref')); +$stmt = AE\createTable($conn, 'Subjects', $columns); +unset($columns); + +// Create table 'sn_x_study' +$columns = array(new AE\ColumnMeta('int', 'studyID'), + new AE\ColumnMeta('nchar(32)', 'sn')); +$stmt = AE\createTable($conn, 'sn_x_study', $columns); +unset($columns); + +// Create table 'Studies' +$columns = array(new AE\ColumnMeta('int', 'studyID'), + new AE\ColumnMeta('nvarchar(max)', 'Intro')); +$stmt = AE\createTable($conn, 'Studies', $columns); +unset($columns); + +// Insert 3 rows into table 'Studies' +for ($i = 1; $i <= 3; $i++) { + insertIntoStudies($conn, $i); } $proc = << + --FILE-- --EXPECT-- diff --git a/test/functional/sqlsrv/0076.phpt b/test/functional/sqlsrv/0076.phpt index f03ff09c..756f2036 100644 --- a/test/functional/sqlsrv/0076.phpt +++ b/test/functional/sqlsrv/0076.phpt @@ -1,45 +1,47 @@ --TEST-- datetime server neutral to make sure it passes. --SKIPIF-- - + --FILE-- --EXPECTREGEX-- diff --git a/test/functional/sqlsrv/0078.phpt b/test/functional/sqlsrv/0078.phpt index 4ab4b2ac..66493ba8 100644 --- a/test/functional/sqlsrv/0078.phpt +++ b/test/functional/sqlsrv/0078.phpt @@ -1,257 +1,268 @@ --TEST-- Fix for output string parameters length prior to output being delivered --SKIPIF-- - + --FILE-- - $id, 'Intro' => $intro)); + } else { + $stmt = sqlsrv_query($conn, "INSERT INTO Studies (studyID, Intro) VALUES (" . $id . ", N'". $intro ."')"); + } + if ($stmt === false) { + fatalError("Failed to insert $id and $intro!\n"); } } -if( sqlsrv_errors() != NULL ) { - print_r( sqlsrv_errors() ); -} -echo "$introText\n"; + +sqlsrv_configure('WarningsReturnAsErrors', 0); +sqlsrv_configure('LogSeverity', SQLSRV_LOG_SEVERITY_ALL); + +require_once('MsCommon.inc'); +$conn = AE\connect(); +if ($conn === false) { + die(print_r(sqlsrv_errors(), true)); +} + +dropProc($conn, 'sp_MakeSubject78'); -$tsql_callSP = "{call sp_MakeSubject78(?,?,?,?)}"; -$introText="X"; +// Create Table 'Subjects' but do not encrypt the first column because in the stored procedure +// we rely on the server to get the current date time. With Column Encryption, all input values +// have to be provided by the client +$columns = array(new AE\ColumnMeta('datetime', 'StartTime', null, true, true), + new AE\ColumnMeta('nchar(32)', 'sn'), + new AE\ColumnMeta('nvarchar(50)', 'extref')); +$stmt = AE\createTable($conn, 'Subjects', $columns); +unset($columns); + +// Create table 'sn_x_study' +$columns = array(new AE\ColumnMeta('int', 'studyID'), + new AE\ColumnMeta('nchar(32)', 'sn')); +$stmt = AE\createTable($conn, 'sn_x_study', $columns); +unset($columns); + +// Create table 'Studies'. When AE is enabled, the sql type must match +// the column definition, but because this test wants to convert the +// output of column 'Intro' to nvarchar(256), we do not encrypt this second column +$columns = array(new AE\ColumnMeta('int', 'studyID'), + new AE\ColumnMeta('nchar(32)', 'Intro', null, true, true)); +$stmt = AE\createTable($conn, 'Studies', $columns); +unset($columns); + +// Insert 3 rows into table 'Studies' +insertIntoStudies($conn, 1, "Test class 1"); +insertIntoStudies($conn, 2, "12345678901234567890123456789012"); +insertIntoStudies($conn, 3, "Test class 3"); + +$proc = << --EXPECT-- 64 diff --git a/test/functional/sqlsrv/0079.phpt b/test/functional/sqlsrv/0079.phpt index 6b89db46..94ee6a18 100644 --- a/test/functional/sqlsrv/0079.phpt +++ b/test/functional/sqlsrv/0079.phpt @@ -1,7 +1,7 @@ --TEST-- Invalid UTF-16 coming from the server --SKIPIF-- - + --FILE-- diff --git a/test/functional/sqlsrv/MsHelper.inc b/test/functional/sqlsrv/MsHelper.inc index 4cb5cbfa..bf1ba21d 100644 --- a/test/functional/sqlsrv/MsHelper.inc +++ b/test/functional/sqlsrv/MsHelper.inc @@ -253,7 +253,7 @@ function getDefaultColname($dataType) */ function getInsertSqlComplete($tbname, $inputs) { - $colStr = "INSERT INTO $tbname ("; + $colStr = "INSERT INTO [$tbname] ("; $valStr = "VALUES ("; if (empty($inputs)) { echo "getInsertSqlComplete: inputs for inserting a row cannot be empty.\n"; @@ -282,7 +282,7 @@ function getInsertSqlComplete($tbname, $inputs) */ function getInsertSqlPlaceholders($tbname, $inputs) { - $colStr = "INSERT INTO $tbname ("; + $colStr = "INSERT INTO [$tbname] ("; $valStr = "VALUES ("; if (empty($inputs)) { echo "getInsertSqlPlaceholders: inputs for inserting a row cannot be empty.\n"; @@ -304,7 +304,7 @@ function getInsertSqlPlaceholders($tbname, $inputs) */ function getCallProcSqlPlaceholders($spname, $num) { - $callStr = "{CALL $spname ("; + $callStr = "{CALL [$spname] ("; $callStr .= getSeqPlaceholders($num) . ")} "; return $callStr; } @@ -396,13 +396,13 @@ function connect($options = array(), $disableCE = false) function createTable($conn, $tbname, $columnMetaArr) { require_once("MsCommon.inc"); - dropTable($conn, $tbname); + dropTable($conn, $tbname, true); $colDef = ""; foreach ($columnMetaArr as $meta) { $colDef = $colDef . $meta->getColDef() . ", "; } $colDef = rtrim($colDef, ", "); - $createSql = "CREATE TABLE $tbname ( $colDef )"; + $createSql = "CREATE TABLE [$tbname] ( $colDef )"; return sqlsrv_query($conn, $createSql); } @@ -554,6 +554,38 @@ function executeQueryEx($conn, $sql, $options) return $stmt; } +/** + * Similar to executeQuery() but with params for binding and + * whether this query is expected to fail + * @return resource sqlsrv statement upon success or false otherwise + */ +function executeQueryParams($conn, $sql, $params, $expectedToFail = false, $message = '') +{ + $res = true; + if (isColEncrypted()) { + $stmt = sqlsrv_prepare($conn, $sql, $params); + if ($stmt) { + $res = sqlsrv_execute($stmt); + } + } else { + $stmt = sqlsrv_query($conn, $sql, $params); + } + + if ($stmt === false || !$res ) { + if (! $expectedToFail) { + fatalError($message); + } else { + print_r(sqlsrv_errors()); + } + } else { + if ($expectedToFail) { + fatalError($message); + } + } + + return $stmt; +} + /** * Fetch all rows and all columns given a table name, and print them * @param resource $conn : connection resource diff --git a/test/functional/sqlsrv/fix_test_182741.phpt b/test/functional/sqlsrv/fix_test_182741.phpt index a0b2873c..90cf0e24 100644 --- a/test/functional/sqlsrv/fix_test_182741.phpt +++ b/test/functional/sqlsrv/fix_test_182741.phpt @@ -1,7 +1,7 @@ --TEST-- fix for 182741. --SKIPIF-- - + --FILE-- + --FILE-- true )); - -if (!$conn) { - fatalError("Could not connect"); -} - -$stmt = sqlsrv_query($conn, "IF OBJECT_ID('2008_date_types', 'U') IS NOT NULL DROP TABLE [2008_date_types]"); - -$stmt = sqlsrv_query($conn, "CREATE TABLE [2008_date_types] (id int, [c1_date] date, [c2_time] time, [c3_datetimeoffset] datetimeoffset, [c4_datetime2] datetime2)"); -if ($stmt === false) { - fatalError("Create table failed"); +$conn = AE\connect(array('ReturnDatesAsStrings' => true)); +$tableName = '2008_date_types'; +$columns = array(new AE\ColumnMeta('int', 'id'), + new AE\ColumnMeta('date', 'c1_date'), + new AE\ColumnMeta('time', 'c2_time'), + new AE\ColumnMeta('datetimeoffset', 'c3_datetimeoffset'), + new AE\ColumnMeta('datetime2', 'c4_datetime2')); +$stmt = AE\createTable($conn, $tableName, $columns); +if (!$stmt) { + fatalError("Failed to create table $tableName\n"); } // insert new date time types as strings (this works now) -$stmt = sqlsrv_query( - $conn, - "INSERT INTO [2008_date_types] (id, [c1_date], [c2_time], [c3_datetimeoffset], [c4_datetime2])" . - " VALUES (?, ?, ?, ?, ?)", - array( rand(0, 99999), - array( strftime('%Y-%m-%d'), SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STRING('utf-8'), SQLSRV_SQLTYPE_DATE ), - array( strftime('%H:%M:%S'), SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STRING('utf-8'), SQLSRV_SQLTYPE_TIME ), - array( date_format(date_create(), 'Y-m-d H:i:s.u P'), SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STRING('utf-8'), SQLSRV_SQLTYPE_DATETIMEOFFSET ), - array( date_format(date_create(), 'Y-m-d H:i:s.u'), SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STRING('utf-8'), SQLSRV_SQLTYPE_DATETIME2 )) +$insertSql = "INSERT INTO [$tableName] (id, [c1_date], [c2_time], [c3_datetimeoffset], [c4_datetime2]) VALUES (?, ?, ?, ?, ?)"; +$stmt = AE\executeQueryParams( + $conn, + $insertSql, + array(rand(0, 99999), + array(strftime('%Y-%m-%d'), SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STRING('utf-8'), SQLSRV_SQLTYPE_DATE), + array(strftime('%H:%M:%S'), SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STRING('utf-8'), SQLSRV_SQLTYPE_TIME), + array(date_format(date_create(), 'Y-m-d H:i:s.u P'), SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STRING('utf-8'), SQLSRV_SQLTYPE_DATETIMEOFFSET), + array(date_format(date_create(), 'Y-m-d H:i:s.u'), SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STRING('utf-8'), SQLSRV_SQLTYPE_DATETIME2)), + false, + "Insert 1 failed" ); -if ($stmt === false) { - fatalError("Insert 1 failed"); -} + // insert new date time types as DateTime objects (this works now) -$stmt = sqlsrv_query( +$stmt = AE\executeQueryParams( $conn, - "INSERT INTO [2008_date_types] (id, [c1_date], [c2_time], [c3_datetimeoffset], [c4_datetime2])" . - " VALUES (?, ?, ?, ?, ?)", - array( rand(0, 99999), - array( date_create(), SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_DATETIME, SQLSRV_SQLTYPE_DATE ), - array( date_create(), SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_DATETIME, SQLSRV_SQLTYPE_TIME ), - array( date_create(), SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_DATETIME, SQLSRV_SQLTYPE_DATETIMEOFFSET ), - array( date_create(), SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_DATETIME, SQLSRV_SQLTYPE_DATETIME2 )) + $insertSql, + array(rand(0, 99999), + array(date_create(), SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_DATETIME, SQLSRV_SQLTYPE_DATE), + array(date_create(), SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_DATETIME, SQLSRV_SQLTYPE_TIME), + array(date_create(), SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_DATETIME, SQLSRV_SQLTYPE_DATETIMEOFFSET), + array(date_create(), SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_DATETIME, SQLSRV_SQLTYPE_DATETIME2)), + false, + "Insert 2 failed" ); -if ($stmt === false) { - fatalError("Insert 2 failed"); -} + // insert new date time types as default DateTime objects with no type information (this works now) -$stmt = sqlsrv_query( +$stmt = AE\executeQueryParams( $conn, - "INSERT INTO [2008_date_types] (id, [c1_date], [c2_time], [c3_datetimeoffset], [c4_datetime2])" . - " VALUES (?, ?, ?, ?, ?)", - array( rand(0, 99999), date_create(), date_create(), date_create(), date_create()) + $insertSql, + array(rand(0, 99999), date_create(), date_create(), date_create(), date_create()), + false, + "Insert 3 failed" ); -if ($stmt === false) { - fatalError("Insert 3 failed"); -} + // insert new date time types as strings with no type information (this works) -$stmt = sqlsrv_query( +$stmt = AE\executeQueryParams( $conn, - "INSERT INTO [2008_date_types] (id, [c1_date], [c2_time], [c3_datetimeoffset], [c4_datetime2])" . - " VALUES (?, ?, ?, ?, ?)", - array( rand(0, 99999), strftime('%Y-%m-%d'), strftime('%H:%M:%S'), date_format(date_create(), 'Y-m-d H:i:s.u P'), date_format(date_create(), 'Y-m-d H:i:s.u P')) + $insertSql, + array(rand(0, 99999), strftime('%Y-%m-%d'), strftime('%H:%M:%S'), date_format(date_create(), 'Y-m-d H:i:s.u P'), date_format(date_create(), 'Y-m-d H:i:s.u P')), + false, + "Insert 4 failed" ); -if ($stmt === false) { - fatalError("Insert 4 failed"); -} // retrieve date time fields as strings (this works) -$stmt = sqlsrv_query($conn, "SELECT * FROM [2008_date_types]"); +$stmt = sqlsrv_query($conn, "SELECT * FROM [$tableName]"); while (sqlsrv_fetch($stmt)) { for ($i = 0; $i < sqlsrv_num_fields($stmt); ++$i) { $fld = sqlsrv_get_field($stmt, $i, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR)); @@ -84,7 +79,7 @@ while (sqlsrv_fetch($stmt)) { } // retrieve date time fields as default (should come back as DateTime objects) (this works now) -$stmt = sqlsrv_query($conn, "SELECT * FROM [2008_date_types]"); +$stmt = sqlsrv_query($conn, "SELECT * FROM [$tableName]"); if ($stmt === false) { fatalError("Select from table failed"); } @@ -93,7 +88,7 @@ while ($row = sqlsrv_fetch_array($stmt)) { } // retrieve date itme fields as DateTime objects -$stmt = sqlsrv_query($conn, "SELECT * FROM [2008_date_types]"); +$stmt = sqlsrv_query($conn, "SELECT * FROM [$tableName]"); while (sqlsrv_fetch($stmt)) { for ($i = 1; $i < sqlsrv_num_fields($stmt); ++$i) { $fld = sqlsrv_get_field($stmt, $i, SQLSRV_PHPTYPE_DATETIME); diff --git a/test/functional/sqlsrv/sqlsrv_378_out_param_error.phpt b/test/functional/sqlsrv/sqlsrv_378_out_param_error.phpt index 73787df4..ebb7334f 100644 --- a/test/functional/sqlsrv/sqlsrv_378_out_param_error.phpt +++ b/test/functional/sqlsrv/sqlsrv_378_out_param_error.phpt @@ -6,17 +6,17 @@ steps to reproduce the issue: 1- create a store procedure with print and output parameter 2- initialize output parameters to a different data type other than the type declared in sp. 3- set the WarningsReturnAsErrors to true -4 - call sp. +4- call sp. +--SKIPIF-- + --FILE-- + --FILE-- - --FILE-- 1)); diff --git a/test/functional/sqlsrv/sqlsrv_input_param_unknown_encoding.phpt b/test/functional/sqlsrv/sqlsrv_input_param_unknown_encoding.phpt index 65e90fc6..aa796ae9 100644 --- a/test/functional/sqlsrv/sqlsrv_input_param_unknown_encoding.phpt +++ b/test/functional/sqlsrv/sqlsrv_input_param_unknown_encoding.phpt @@ -1,50 +1,50 @@ --TEST-- test input param with unknown encoding --SKIPIF-- - + --FILE-- --EXPECTREGEX-- -(Warning|Notice)\: Use of undefined constant SQLSRV_ENC_UNKNOWN - assumed \'SQLSRV_ENC_UNKNOWN\' (\(this will throw an Error in a future version of PHP\) )?in .+(\/|\\)sqlsrv_input_param_unknown_encoding\.php on line 26 -Array -\( - \[0\] => Array - \( - \[0\] => IMSSP - \[SQLSTATE\] => IMSSP - \[1\] => -16 - \[code\] => -16 - \[2\] => An invalid PHP type for parameter 2 was specified\. - \[message\] => An invalid PHP type for parameter 2 was specified\. - \) +(Warning|Notice)\: Use of undefined constant SQLSRV_ENC_UNKNOWN - assumed \'SQLSRV_ENC_UNKNOWN\' (\(this will throw an Error in a future version of PHP\) )?in .+(\/|\\)sqlsrv_input_param_unknown_encoding\.php on line 24 -\) - -(Warning|Notice)\: Use of undefined constant SQLSRV_ENC_UNKNOWN - assumed \'SQLSRV_ENC_UNKNOWN\' (\(this will throw an Error in a future version of PHP\) )?in .+(\/|\\)sqlsrv_input_param_unknown_encoding\.php on line 33 -Array -\( - \[0\] => Array - \( - \[0\] => IMSSP - \[SQLSTATE\] => IMSSP - \[1\] => -16 - \[code\] => -16 - \[2\] => An invalid PHP type for parameter 2 was specified\. - \[message\] => An invalid PHP type for parameter 2 was specified\. - \) - -\) +(Warning|Notice)\: Use of undefined constant SQLSRV_ENC_UNKNOWN - assumed \'SQLSRV_ENC_UNKNOWN\' (\(this will throw an Error in a future version of PHP\) )?in .+(\/|\\)sqlsrv_input_param_unknown_encoding\.php on line 32 diff --git a/test/functional/sqlsrv/sqlsrv_param_input_variants.phpt b/test/functional/sqlsrv/sqlsrv_param_input_variants.phpt index 6c065f52..c1f0321e 100644 --- a/test/functional/sqlsrv/sqlsrv_param_input_variants.phpt +++ b/test/functional/sqlsrv/sqlsrv_param_input_variants.phpt @@ -13,27 +13,56 @@ hierarchyid geometry datetimeoffset User-defined types +--SKIPIF-- + --FILE-- c1_int, $row[$fld], $fetched, $fld++); - DoValuesMatched($obj->c2_tinyint, $row[$fld], $fetched, $fld++); - DoValuesMatched($obj->c3_smallint, $row[$fld], $fetched, $fld++); - DoValuesMatched($obj->c4_bigint, $row[$fld], $fetched, $fld++); - DoValuesMatched($obj->c5_bit, $row[$fld], $fetched, $fld++); - DoValuesMatched($obj->c6_float, $row[$fld], $fetched, $fld++); - DoValuesMatched($obj->c7_real, $row[$fld], $fetched, $fld++); - DoValuesMatched($obj->c8_decimal, $row[$fld], $fetched, $fld++); - DoValuesMatched($obj->c9_numeric, $row[$fld], $fetched, $fld++); - DoValuesMatched($obj->c10_money, $row[$fld], $fetched, $fld++); - DoValuesMatched($obj->c11_smallmoney, $row[$fld], $fetched, $fld++); - DoValuesMatched($obj->c12_char, $row[$fld], $fetched, $fld++); - DoValuesMatched($obj->c13_varchar, $row[$fld], $fetched, $fld++); - DoValuesMatched($obj->c14_uniqueidentifier, $row[$fld], $fetched, $fld++); - DoValuesMatched($obj->c15_datetime, $row[$fld], $fetched, $fld++); - DoValuesMatched($obj->c16_smalldatetime, $row[$fld], $fetched, $fld++); + doValuesMatched($obj->c1_int, $row[$fld], $fetched, $fld++); + doValuesMatched($obj->c2_tinyint, $row[$fld], $fetched, $fld++); + doValuesMatched($obj->c3_smallint, $row[$fld], $fetched, $fld++); + doValuesMatched($obj->c4_bigint, $row[$fld], $fetched, $fld++); + doValuesMatched($obj->c5_bit, $row[$fld], $fetched, $fld++); + doValuesMatched($obj->c6_float, $row[$fld], $fetched, $fld++); + doValuesMatched($obj->c7_real, $row[$fld], $fetched, $fld++); + doValuesMatched($obj->c8_decimal, $row[$fld], $fetched, $fld++); + doValuesMatched($obj->c9_numeric, $row[$fld], $fetched, $fld++); + doValuesMatched($obj->c10_money, $row[$fld], $fetched, $fld++); + doValuesMatched($obj->c11_smallmoney, $row[$fld], $fetched, $fld++); + doValuesMatched($obj->c12_char, $row[$fld], $fetched, $fld++); + doValuesMatched($obj->c13_varchar, $row[$fld], $fetched, $fld++); + doValuesMatched($obj->c14_uniqueidentifier, $row[$fld], $fetched, $fld++); + doValuesMatched($obj->c15_datetime, $row[$fld], $fetched, $fld++); + doValuesMatched($obj->c16_smalldatetime, $row[$fld], $fetched, $fld++); } // returns the number of rows fetched return $fetched; } -function DoValuesMatched($value1, $value2, $row, $col) +function doValuesMatched($value1, $value2, $row, $col) { $matched = false; if (is_null($value1) && is_null($value2)) { @@ -106,7 +135,7 @@ function DoValuesMatched($value1, $value2, $row, $col) } } -function GetInputData($index) +function getInputData($index) { switch ($index) { case 1: @@ -122,34 +151,29 @@ function GetInputData($index) } } -function RunTest() -{ - startTest("sqlsrv_param_input_variants"); - try { - setup(); - $conn = connect(); +try { + setup(); + $conn = connect(); - // Create a temp table that will be automatically dropped once the connection is closed - $tableName = GetTempTableName(); - CreateVariantTable($conn, $tableName); + // Create a temp table that will be automatically dropped once the connection is closed + $tableName = 'param_input_variants'; + createVariantTable($conn, $tableName); - // Insert data - $numRows = 4; - for ($i = 1; $i <= $numRows; $i++) { - InsertData($conn, $tableName, $i); - } - - FetchData($conn, $tableName, $numRows); - - sqlsrv_close($conn); - } catch (Exception $e) { - echo $e->getMessage(); + // Insert data + $numRows = 4; + for ($i = 1; $i <= $numRows; $i++) { + insertData($conn, $tableName, $i); } - echo "\nDone\n"; - endTest("sqlsrv_param_input_variants"); -} -RunTest(); + fetchData($conn, $tableName, $numRows); + + dropTable($conn, $tableName); + + sqlsrv_close($conn); +} catch (Exception $e) { + echo $e->getMessage(); +} +echo "\nDone\n"; ?> --EXPECT-- @@ -160,4 +184,3 @@ Comparing data in row 4 Number of rows fetched: 4 Done -Test "sqlsrv_param_input_variants" completed successfully. diff --git a/test/functional/sqlsrv/sqlsrv_param_ints_with_deletes.phpt b/test/functional/sqlsrv/sqlsrv_param_ints_with_deletes.phpt index 56a5b971..fc689c55 100644 --- a/test/functional/sqlsrv/sqlsrv_param_ints_with_deletes.phpt +++ b/test/functional/sqlsrv/sqlsrv_param_ints_with_deletes.phpt @@ -1,25 +1,28 @@ --TEST-- Test insertion with floats +--SKIPIF-- + --FILE-- getMessage(); - } - echo "\nDone\n"; - endTest("sqlsrv_statement_exec_param_ints"); +echo "\nTest begins...\n"; +try { + execData(true); + execData(false); +} catch (Exception $e) { + echo $e->getMessage(); } - -Repro(); +echo "\nDone\n"; +endTest("sqlsrv_statement_exec_param_ints"); ?> --EXPECT-- diff --git a/test/functional/sqlsrv/sqlsrv_param_output_variants.phpt b/test/functional/sqlsrv/sqlsrv_param_output_variants.phpt index 2ed475a6..76a1ccdd 100644 --- a/test/functional/sqlsrv/sqlsrv_param_output_variants.phpt +++ b/test/functional/sqlsrv/sqlsrv_param_output_variants.phpt @@ -1,15 +1,20 @@ --TEST-- Test parametrized insert and sql_variant as an output parameter. --DESCRIPTION-- -sql_variant is not supported for output parameters, this test checks the error handling in this case +Normally, sql_variant is not supported for output parameters, this test checks the error handling in this case. However, when Always Encrypted is enabled, we are able to bind output parameters with prepared +statements. +--SKIPIF-- + --FILE--  0) { - for ($i = 0; $i < $count; $i++) { - print($errors[$i]['message']."\n"); - } + if (AE\isColEncrypted()) { + fatalError("With AE this should not have failed!"); + } + + // Expect to fail only when AE is disabled + $expected = "Operand type clash: varchar(max) is incompatible with sql_variant"; + if (strpos($error['message'], $expected) === false) { + echo $error['message'] . PHP_EOL; + fatalError("Expected error: $expected\n"); } } -function RunTest() -{ - startTest("sqlsrv_param_output_variants"); - try { - setup(); +setup(); - // connect - $conn = connect(); +// connect +$conn = AE\connect(); - // Create a temp table that will be automatically dropped once the connection is closed - $tableName = GetTempTableName(); - CreateVariantTable($conn, $tableName); - echo "\n"; +// Create a test table that will be automatically dropped once the connection is closed +$tableName = GetTempTableName('test_output_variants', false); +createVariantTable($conn, $tableName); - TestOutputParam($conn, $tableName); - TestInputAndOutputParam($conn, $tableName); +testOutputParam($conn, $tableName); +testInputAndOutputParam($conn, $tableName); - sqlsrv_close($conn); - } catch (Exception $e) { - echo $e->getMessage(); - } - echo "\nDone\n"; - endTest("sqlsrv_param_output_variants"); -} - -RunTest(); +dropTable($conn, $tableName); +sqlsrv_close($conn); +print "Test completed successfully\n"; ?> ---EXPECTREGEX-- -  -\[Microsoft\]\[ODBC Driver 1[1-9] for SQL Server\]\[SQL Server\]Operand type clash: varchar\(max\) is incompatible with sql_variant - -\[Microsoft\]\[ODBC Driver 1[1-9] for SQL Server\]\[SQL Server\]Operand type clash: varchar\(max\) is incompatible with sql_variant - -Done -Test \"sqlsrv_param_output_variants\" completed successfully\. +--EXPECT-- +Test completed successfully \ No newline at end of file diff --git a/test/functional/sqlsrv/sqlsrv_param_query_data_types.phpt b/test/functional/sqlsrv/sqlsrv_param_query_data_types.phpt index 31552c14..6bb0dd27 100644 --- a/test/functional/sqlsrv/sqlsrv_param_query_data_types.phpt +++ b/test/functional/sqlsrv/sqlsrv_param_query_data_types.phpt @@ -1,58 +1,64 @@ --TEST-- Test insert various numeric data types and fetch them back as strings +--SKIPIF-- + --FILE-- getMessage(); - } - echo "\nDone\n"; - endTest("sqlsrv_param_query_data_types"); + sqlsrv_close($conn); +} catch (Exception $e) { + echo $e->getMessage(); } - -Repro(); +echo "\nDone\n"; ?> --EXPECT-- @@ -60,4 +66,3 @@ Repro(); Test begins... Done -Test "sqlsrv_param_query_data_types" completed successfully. diff --git a/test/functional/sqlsrv/sqlsrv_param_query_errors.phpt b/test/functional/sqlsrv/sqlsrv_param_query_errors.phpt index 02cec7ea..4ccec206 100644 --- a/test/functional/sqlsrv/sqlsrv_param_query_errors.phpt +++ b/test/functional/sqlsrv/sqlsrv_param_query_errors.phpt @@ -1,17 +1,35 @@ --TEST-- Insert with query params but with wrong parameters or types +--SKIPIF-- + --FILE-- getMessage(); - } - echo "\nDone\n"; - endTest("sqlsrv_param_query_errors"); + sqlsrv_close($conn); +} catch (Exception $e) { + echo $e->getMessage(); } - -RunTest(); - +echo "\nDone\n"; + ?> --EXPECT--  @@ -112,4 +116,3 @@ An invalid PHP type for parameter 2 was specified. An invalid PHP type for parameter 2 was specified. Done -Test "sqlsrv_param_query_errors" completed successfully. diff --git a/test/functional/sqlsrv/sqlsrv_param_query_invalid_inputs.phpt b/test/functional/sqlsrv/sqlsrv_param_query_invalid_inputs.phpt index a9460b75..31cc6037 100644 --- a/test/functional/sqlsrv/sqlsrv_param_query_invalid_inputs.phpt +++ b/test/functional/sqlsrv/sqlsrv_param_query_invalid_inputs.phpt @@ -1,82 +1,90 @@ --TEST-- Insert with query params but with various invalid inputs or boundaries +--SKIPIF-- + --FILE-- getMessage(); + $tableName = 'MinMaxScale'; + $columns = array(new AE\ColumnMeta('int', 'c1_int'), + new AE\ColumnMeta('decimal(28,4)', 'c2_decimal'), + new AE\ColumnMeta('numeric(32,4)', 'c3_numeric')); + $stmt = AE\createTable($conn, $tableName, $columns); + if (!$stmt) { + fatalError("Failed to create table $tableName\n"); } - echo "\nDone\n"; - endTest("sqlsrv_param_query_invalid_inputs"); + + $stmt = sqlsrv_query($conn, "INSERT INTO $tableName (c1_int, c2_decimal) VALUES (?, ?)", array(getFirstInputParam(), array(0.0, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR), SQLSRV_SQLTYPE_DECIMAL(28, 34)))); + printErrors(); + + $stmt = sqlsrv_query($conn, "INSERT INTO $tableName (c1_int, c3_numeric) VALUES (?, ?)", array(getFirstInputParam(), array(0.0, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR), SQLSRV_SQLTYPE_NUMERIC(32, -1)))); + printErrors(); + dropTable($conn, $tableName); } -RunTest(); +function MinMaxSize($conn) +{ + $tableName = 'MinMaxSize'; + $columns = array(new AE\ColumnMeta('int', 'c1_int'), + new AE\ColumnMeta('varchar(max)', 'c2_varchar_max')); + $stmt = AE\createTable($conn, $tableName, $columns); + if (!$stmt) { + fatalError("Failed to create table $tableName\n"); + } + + $stmt = sqlsrv_query($conn, "INSERT INTO $tableName (c1_int, c2_varchar_max) VALUES (?, ?)", array(getFirstInputParam(), array("Test Data", SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR), SQLSRV_SQLTYPE_VARCHAR(0)))); + printErrors(); + + $stmt = sqlsrv_query($conn, "INSERT INTO $tableName (c1_int, c2_varchar_max) VALUES (?, ?)", array(getFirstInputParam(), array("Test Data", SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR), SQLSRV_SQLTYPE_VARCHAR(9000)))); + printErrors(); + dropTable($conn, $tableName); +} + +function MinMaxPrecision($conn) +{ + $tableName = 'MinMaxPrecision'; + $columns = array(new AE\ColumnMeta('int', 'c1_int'), + new AE\ColumnMeta('decimal(28,4)', 'c2_decimal'), + new AE\ColumnMeta('numeric(32,4)', 'c3_numeric')); + $stmt = AE\createTable($conn, $tableName, $columns); + if (!$stmt) { + fatalError("Failed to create table $tableName\n"); + } + + $stmt = sqlsrv_query($conn, "INSERT INTO $tableName (c1_int, c3_numeric) VALUES (?, ?)", array(getFirstInputParam(), array(0.0, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR), SQLSRV_SQLTYPE_NUMERIC(40, 0)))); + printErrors(); + + $stmt = sqlsrv_query($conn, "INSERT INTO $tableName (c1_int, c2_decimal) VALUES (?, ?)", array(getFirstInputParam(), array(0.0, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR), SQLSRV_SQLTYPE_DECIMAL(-1, 0)))); + printErrors(); + dropTable($conn, $tableName); +} + +echo "\nTest begins...\n"; +try { + set_time_limit(0); + sqlsrv_configure('WarningsReturnAsErrors', 1); + + // connect + $conn = AE\connect(); + + MinMaxScale($conn); + MinMaxSize($conn); + MinMaxPrecision($conn); + + sqlsrv_close($conn); +} catch (Exception $e) { + echo $e->getMessage(); +} +echo "\nDone\n"; ?> --EXPECT-- @@ -90,4 +98,3 @@ An invalid size or precision for parameter 2 was specified. An invalid size or precision for parameter 2 was specified. Done -Test "sqlsrv_param_query_invalid_inputs" completed successfully. diff --git a/test/functional/sqlsrv/sqlsrv_query.phpt b/test/functional/sqlsrv/sqlsrv_query.phpt index 55f26fe4..5727b9b8 100644 --- a/test/functional/sqlsrv/sqlsrv_query.phpt +++ b/test/functional/sqlsrv/sqlsrv_query.phpt @@ -1,7 +1,7 @@ --TEST-- -sqlsrv_query test. Performs same tasks as 0006.phpt, using sqlsrv_query. +sqlsrv_query test. Performs same tasks as 0006.phpt, using sqlsrv_query. --SKIPIF-- - + --FILE-- + --FILE-- ---EXPECTF-- -Array -( - [0] => Array - ( - [0] => 22018 - [SQLSTATE] => 22018 - [1] => 0 - [code] => 0 - [2] => %SInvalid character value for cast specification - [message] => %SInvalid character value for cast specification - ) - -) +--EXPECT-- sqlsrv_query(2) failed. -Array -( - [0] => Array - ( - [0] => 42000 - [SQLSTATE] => 42000 - [1] => 257 - [code] => 257 - [2] => %SImplicit conversion from data type varchar(max) to varbinary(max) is not allowed. Use the CONVERT function to run this query. - [message] => %SImplicit conversion from data type varchar(max) to varbinary(max) is not allowed. Use the CONVERT function to run this query. - ) - -) sqlsrv_query(3) failed. 1 12.0 testtestte This is some text meant to test binding parameters to streams -array(1) { - [0]=> - array(6) { - [0]=> - string(5) "IMSSP" - ["SQLSTATE"]=> - string(5) "IMSSP" - [1]=> - int(-9) - ["code"]=> - int(-9) - [2]=> - string(59) "Parameter array 2 must have at least one value or variable." - ["message"]=> - string(59) "Parameter array 2 must have at least one value or variable." - } -} +Done \ No newline at end of file diff --git a/test/functional/sqlsrv/sqlsrv_streams_empty_char.phpt b/test/functional/sqlsrv/sqlsrv_streams_empty_char.phpt index 3ae7f1da..bfd726b6 100644 --- a/test/functional/sqlsrv/sqlsrv_streams_empty_char.phpt +++ b/test/functional/sqlsrv/sqlsrv_streams_empty_char.phpt @@ -1,54 +1,73 @@ --TEST-- Populate different test tables with character fields using empty stream data as inputs +--SKIPIF-- + --FILE--  0)); + if ($stmt) { + $res = sqlsrv_execute($stmt); + } + } else { + $stmt = sqlsrv_query($conn, $query, array($value, &$fname), array('SendStreamParamsAtExec' => 0)); + } + if ($stmt === false || !$res) { + fclose($fname); + fatalError("Failed in sendQueryStream for $value\n"); + } + + sqlsrv_send_stream_data($stmt); + sqlsrv_free_stmt($stmt); + fclose($fname); +} + +function char2Stream($conn, $fileName) +{ + $tableName = 'streams_empty_char'; // create a test table - $stmt = sqlsrv_query($conn, "CREATE TABLE $tableName ([c1_int] int, [c2_char] char(512), [c3_varchar] varchar(512), [c4_varchar_max] varchar(max), [c5_text] text)"); + $columns = array(new AE\ColumnMeta('int', 'c1_int'), + new AE\ColumnMeta('char(512)', 'c2_char'), + new AE\ColumnMeta('varchar(512)', 'c3_varchar'), + new AE\ColumnMeta('varchar(max)', 'c4_varchar_max'), + new AE\ColumnMeta('text', 'c5_text')); + $stmt = AE\createTable($conn, $tableName, $columns); + if (!$stmt) { + fatalError("Failed to create table $tableName\n"); + } sqlsrv_free_stmt($stmt); // insert data - $fname = fopen($fileName, "r"); - $stmt = sqlsrv_query($conn, "INSERT INTO $tableName (c1_int, c2_char) VALUES (?, ?)", array(1, &$fname), array('SendStreamParamsAtExec' => 0)); - sqlsrv_send_stream_data($stmt); - sqlsrv_free_stmt($stmt); - fclose($fname); + sendQueryStream($conn, "INSERT INTO $tableName (c1_int, c2_char) VALUES (?, ?)", 1, $fileName); + fetchData($conn, $tableName, 1); - FetchData($conn, $tableName, 1); + sendQueryStream($conn, "INSERT INTO $tableName (c1_int, c3_varchar) VALUES (?, ?)", 2, $fileName); + fetchData($conn, $tableName, 2); - $fname = fopen($fileName, "r"); - $stmt = sqlsrv_query($conn, "INSERT INTO $tableName (c1_int, c3_varchar) VALUES (?, ?)", array(2, &$fname), array('SendStreamParamsAtExec' => 0)); - sqlsrv_send_stream_data($stmt); - sqlsrv_free_stmt($stmt); - fclose($fname); + sendQueryStream($conn, "INSERT INTO $tableName (c1_int, c4_varchar_max) VALUES (?, ?)", 3, $fileName); + fetchData($conn, $tableName, 3); - FetchData($conn, $tableName, 2); - - $fname = fopen($fileName, "r"); - $stmt = sqlsrv_query($conn, "INSERT INTO $tableName (c1_int, c4_varchar_max) VALUES (?, ?)", array(3, &$fname), array('SendStreamParamsAtExec' => 0)); - sqlsrv_send_stream_data($stmt); - sqlsrv_free_stmt($stmt); - fclose($fname); - - FetchData($conn, $tableName, 3); - - $fname = fopen($fileName, "r"); - $stmt = sqlsrv_query($conn, "INSERT INTO $tableName (c1_int, c5_text) VALUES (?, ?)", array(4, &$fname), array('SendStreamParamsAtExec' => 0)); - sqlsrv_send_stream_data($stmt); - sqlsrv_free_stmt($stmt); - fclose($fname); - - FetchData($conn, $tableName, 4); + sendQueryStream($conn, "INSERT INTO $tableName (c1_int, c5_text) VALUES (?, ?)", 4, $fileName); + fetchData($conn, $tableName, 4); + + dropTable($conn, $tableName); } -function FetchData($conn, $tableName, $fld) +function fetchData($conn, $tableName, $fld) { - $stmt = sqlsrv_prepare($conn, "SELECT * FROM $tableName WHERE c1_int = $fld"); + if (AE\isColEncrypted()) { + // bind param when AE is enabled + $stmt = sqlsrv_prepare($conn, "SELECT * FROM $tableName WHERE c1_int = ?", array($fld)); + } else { + $stmt = sqlsrv_prepare($conn, "SELECT * FROM $tableName WHERE c1_int = $fld"); + } sqlsrv_execute($stmt); $result = sqlsrv_fetch($stmt); $stream = sqlsrv_get_field($stmt, $fld, SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY)); @@ -58,44 +77,32 @@ function FetchData($conn, $tableName, $fld) $result = sqlsrv_fetch($stmt); $value = sqlsrv_get_field($stmt, $fld, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_BINARY)); var_dump($value); + + sqlsrv_free_stmt($stmt); } -//-------------------------------------------------------------------- -// RunTest -// -//-------------------------------------------------------------------- -function RunTest() -{ - startTest("sqlsrv_streams_empty_char"); - echo "\nTest begins...\n"; - try { - set_time_limit(0); - sqlsrv_configure('WarningsReturnAsErrors', 1); +echo "\nTest begins...\n"; +try { + set_time_limit(0); + sqlsrv_configure('WarningsReturnAsErrors', 1); - // connect - $conn = connect(); - if (!$conn) { - fatalError("Could not connect.\n"); - } + // connect + $conn = AE\connect(); - // create an empty file - $fileName = "sqlsrv_streams_empty_char.dat"; - $fp = fopen($fileName, "wb"); - fclose($fp); + // create an empty file + $fileName = "sqlsrv_streams_empty_char.dat"; + $fp = fopen($fileName, "wb"); + fclose($fp); - EmptyStream_Char2Stream($conn, $fileName); + char2Stream($conn, $fileName); - // delete the file - unlink($fileName); - sqlsrv_close($conn); - } catch (Exception $e) { - echo $e->getMessage(); - } - echo "\nDone\n"; - endTest("sqlsrv_streams_empty_char"); + // delete the file + unlink($fileName); + sqlsrv_close($conn); +} catch (Exception $e) { + echo $e->getMessage(); } - -RunTest(); +echo "\nDone\n"; ?> --EXPECT-- @@ -111,4 +118,3 @@ bool(false) bool(false) Done -Test "sqlsrv_streams_empty_char" completed successfully. diff --git a/test/functional/sqlsrv/sqlsrv_streams_null_binary.phpt b/test/functional/sqlsrv/sqlsrv_streams_null_binary.phpt index 5e4662ce..4627ff1d 100644 --- a/test/functional/sqlsrv/sqlsrv_streams_null_binary.phpt +++ b/test/functional/sqlsrv/sqlsrv_streams_null_binary.phpt @@ -1,33 +1,57 @@ --TEST-- Populate different binary fields using null stream data as inputs. +--SKIPIF-- + --FILE-- getMessage(); + // connect + $conn = connect(); + if (!$conn) { + fatalError("Could not connect.\n"); } - echo "\nDone\n"; - endTest("sqlsrv_streams_null_binary"); -} -RunTest(); + // create a test table + $tableName = 'null_binary_stream'; + $stmt = sqlsrv_query($conn, "CREATE TABLE $tableName ([c1_int] int, [c2_varbinary] varbinary(512), [c3_varbinary_max] varbinary(max), [c4_image] image)"); + sqlsrv_free_stmt($stmt); + + nullBin2String($conn, $tableName); + nullPrepBin2String($conn, $tableName); + + dropTable($conn, $tableName); + + sqlsrv_close($conn); +} catch (Exception $e) { + echo $e->getMessage(); +} +echo "\nDone\n"; ?> --EXPECT-- @@ -84,4 +99,3 @@ NULL NULL Done -Test "sqlsrv_streams_null_binary" completed successfully. diff --git a/test/functional/sqlsrv/sqlsrv_streams_null_nchar.phpt b/test/functional/sqlsrv/sqlsrv_streams_null_nchar.phpt index 81556c32..9958b3a9 100644 --- a/test/functional/sqlsrv/sqlsrv_streams_null_nchar.phpt +++ b/test/functional/sqlsrv/sqlsrv_streams_null_nchar.phpt @@ -1,25 +1,50 @@ --TEST-- Populate different unicode character fields using null stream data as inputs +--SKIPIF-- + --FILE-- getMessage(); - } - echo "\nDone\n"; - endTest("sqlsrv_streams_null_nchar"); + sqlsrv_close($conn); +} catch (Exception $e) { + echo $e->getMessage(); } - -RunTest(); +echo "\nDone\n"; ?> --EXPECT-- @@ -71,4 +82,3 @@ NULL NULL Done -Test "sqlsrv_streams_null_nchar" completed successfully. diff --git a/test/functional/sqlsrv/srv_036_transaction_commit.phpt b/test/functional/sqlsrv/srv_036_transaction_commit.phpt index c418fd88..dafebe0b 100644 --- a/test/functional/sqlsrv/srv_036_transaction_commit.phpt +++ b/test/functional/sqlsrv/srv_036_transaction_commit.phpt @@ -1,39 +1,72 @@ --TEST-- Transaction operations: commit successful transactions --SKIPIF-- + --FILE-- = 0))"; -$stmt = sqlsrv_query($conn, $sql); - +// Do not encrypt the integer columns because of the operations required later +$columns = array(new AE\ColumnMeta('VARCHAR(10)', 'GroupId', 'primary key'), + new AE\ColumnMeta('INT', 'Accepted', null, null, true), + new AE\ColumnMeta('INT', 'Tentative', 'NOT NULL CHECK (Tentative >= 0)', null, true)); +$stmt = AE\createTable($conn, $tableName, $columns); +if (!$stmt) { + fatalError("Failed to create table $tableName\n"); +} +sqlsrv_free_stmt($stmt); // Set initial data -$sql = "INSERT INTO $tableName VALUES ('ID1','12','5'),('ID102','20','1')"; -$stmt = sqlsrv_query($conn, $sql) ?: die(print_r(sqlsrv_errors(), true)); +if (AE\isColEncrypted()) { + $stmt = sqlsrv_query($conn, + "INSERT INTO $tableName VALUES (?,?,?),(?,?,?)", + array(array('ID1', null, null, SQLSRV_SQLTYPE_VARCHAR(10)), + array(12, null, null, SQLSRV_SQLTYPE_INT), + array(5, null, null, SQLSRV_SQLTYPE_INT), + array('ID102', null, null, SQLSRV_SQLTYPE_VARCHAR(10)), + array(20, null, null, SQLSRV_SQLTYPE_INT), + array(1, null, null, SQLSRV_SQLTYPE_INT))); +} else { + $sql = "INSERT INTO $tableName VALUES ('ID1','12','5'),('ID102','20','1')"; + $stmt = sqlsrv_query($conn, $sql); +} +if (!$stmt) { + fatalError("Failed to insert data\n"); +} //Initiate transaction sqlsrv_begin_transaction($conn) ?: die(print_r(sqlsrv_errors(), true)); @@ -45,11 +78,11 @@ $params = array($count, $groupId); // Update Accepted column $sql = "UPDATE $tableName SET Accepted = (Accepted + ?) WHERE GroupId = ?"; -$stmt1 = sqlsrv_query($conn, $sql, $params) ?: die(print_r(sqlsrv_errors(), true)); +$stmt1 = runQuery($conn, $sql, $params); // Update Tentative column $sql = "UPDATE $tableName SET Tentative = (Tentative - ?) WHERE GroupId = ?"; -$stmt2 = sqlsrv_query($conn, $sql, $params); +$stmt2 = runQuery($conn, $sql, $params); // Commit the transactions if ($stmt1 && $stmt2) { @@ -60,7 +93,9 @@ if ($stmt1 && $stmt2) { echo "\nTransactions were rolled back.\n"; } -PrintContent($conn); +printContent($conn); + +dropTable($conn, $tableName); sqlsrv_free_stmt($stmt); sqlsrv_close($conn);