2017-05-02 02:01:30 +02:00
< ? php
/*
Microsoft SQL Server Driver for PHP - Unit Test Framework
Copyright ( c ) Microsoft Corporation . All rights reserved .
Description :
Common functions ( shared by all tests ) .
*/
2017-10-04 23:22:56 +02:00
require_once ( 'MsHelper.inc' );
require_once ( 'MsSetup.inc' );
2017-05-02 02:01:30 +02:00
2021-05-26 00:36:01 +02:00
$tvpIncPath = dirname ( __FILE__ ) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'inc' . DIRECTORY_SEPARATOR ;
require_once ( $tvpIncPath . 'test_tvp_data.php' );
2017-10-04 23:22:56 +02:00
$usingUTF8data = false ;
function isWindows ()
2017-05-02 02:01:30 +02:00
{
2017-10-04 23:22:56 +02:00
// This method returns TRUE when running in a Windows platform
2017-05-02 02:01:30 +02:00
// The possible values are WIN32, WINNT and Windows
return ( strtoupper ( substr ( PHP_OS , 0 , 3 )) === 'WIN' );
}
2017-10-04 23:22:56 +02:00
function useUTF8Data ()
2017-05-02 02:01:30 +02:00
{
2017-10-04 23:22:56 +02:00
global $usingUTF8data ;
return $usingUTF8data ;
2017-05-02 02:01:30 +02:00
}
2017-10-04 23:22:56 +02:00
function setUTF8Data ( $val )
2017-05-02 02:01:30 +02:00
{
2017-10-04 23:22:56 +02:00
global $usingUTF8data ;
$usingUTF8data = $val ;
2017-05-02 02:01:30 +02:00
}
2017-10-04 23:22:56 +02:00
function testMode ()
2017-05-02 02:01:30 +02:00
{
$testMode = getenv ( 'PHPT_EXEC' );
return ( $testMode ? true : false );
}
2017-10-04 23:22:56 +02:00
function traceMode ()
2017-05-02 02:01:30 +02:00
{
2017-10-04 23:22:56 +02:00
global $traceEnabled ;
return (( ! testMode () && $traceEnabled ) ? true : false );
2017-05-02 02:01:30 +02:00
}
2017-10-04 23:22:56 +02:00
function trace ( $msg )
2017-05-02 02:01:30 +02:00
{
2017-10-04 23:22:56 +02:00
if ( traceMode ()) {
2017-05-02 02:01:30 +02:00
echo $msg ;
}
}
2017-10-04 23:22:56 +02:00
/**
* This method prints the message when traceMode () is true
*
* @ param string $msg
* @ return void
*/
function traceData ( $sqlType , $data )
2017-05-02 02:01:30 +02:00
{
2017-10-04 23:22:56 +02:00
if ( traceMode ()) {
2017-05-02 02:01:30 +02:00
$msg = strtoupper ( " $sqlType : " );
echo " $msg\t " ;
2017-10-04 23:22:56 +02:00
if ( strlen ( $msg ) <= 7 ) {
2017-05-02 02:01:30 +02:00
echo " \t " ;
}
2017-10-04 23:22:56 +02:00
if ( strlen ( $msg ) <= 15 ) {
2017-05-02 02:01:30 +02:00
echo " \t " ;
}
2017-10-04 23:22:56 +02:00
echo " $data\n " ;
2017-05-02 02:01:30 +02:00
}
}
2017-10-04 23:22:56 +02:00
function isMarsSupported ()
2017-05-02 02:01:30 +02:00
{
2017-10-04 23:22:56 +02:00
global $marsMode ;
2017-05-02 02:01:30 +02:00
return ( $marsMode ? true : false );
}
2017-10-04 23:22:56 +02:00
function isDaasMode ()
2017-05-02 02:01:30 +02:00
{
2017-10-04 23:22:56 +02:00
global $daasMode ;
2017-05-02 02:01:30 +02:00
return ( $daasMode ? true : false );
}
2020-02-11 20:39:31 +01:00
function isLocaleDisabled ()
{
global $daasMode , $localeDisabled ;
return ( $daasMode || $localeDisabled );
}
2021-09-22 18:40:57 +02:00
function isServerHGSEnabled ()
{
$conn = connect ();
$tsql = " SELECT @@SERVERNAME " ;
$stmt = sqlsrv_query ( $conn , $tsql );
if ( sqlsrv_fetch ( $stmt )) {
$name = sqlsrv_get_field ( $stmt , 0 );
if ( strpos ( $name , 'HGS' ) != false ) {
return true ;
}
}
return false ;
}
2019-03-29 16:28:46 +01:00
function isSQLAzure ()
{
// 'SQL Azure' indicates SQL Database or SQL Data Warehouse
// For details, https://docs.microsoft.com/sql/t-sql/functions/serverproperty-transact-sql
$conn = connect ();
$tsql = " SELECT SERVERPROPERTY ('edition') " ;
$stmt = sqlsrv_query ( $conn , $tsql );
if ( sqlsrv_fetch ( $stmt )) {
$edition = sqlsrv_get_field ( $stmt , 0 );
if ( $edition === " SQL Azure " ) {
return true ;
} else {
return false ;
}
} else {
die ( " Could not fetch server property. " );
}
}
2019-01-08 00:36:59 +01:00
function isAzureDW ()
{
// Check if running Azure Data Warehouse
// For details, https://docs.microsoft.com/sql/t-sql/functions/serverproperty-transact-sql
$conn = connect ();
$tsql = " SELECT SERVERPROPERTY ('edition'), SERVERPROPERTY ('EngineEdition') " ;
$stmt = sqlsrv_query ( $conn , $tsql );
if ( sqlsrv_fetch ( $stmt )) {
$edition = sqlsrv_get_field ( $stmt , 0 );
$engEd = sqlsrv_get_field ( $stmt , 1 , SQLSRV_PHPTYPE_INT );
if ( $edition == " SQL Azure " && $engEd == 6 ) {
return true ;
} else {
return false ;
}
} else {
die ( " Could not fetch edition info. " );
}
}
2017-10-04 23:22:56 +02:00
function startTest ( $testName )
2017-05-02 02:01:30 +02:00
{
2017-10-04 23:22:56 +02:00
if ( traceMode ()) {
echo " Starting \" $testName\ " test ... \n\n " ;
2017-05-02 02:01:30 +02:00
}
}
2017-10-04 23:22:56 +02:00
/**
* This method signals the end of a test given its name
*
* @ return void
*/
function endTest ( $testName )
2017-05-02 02:01:30 +02:00
{
echo " Test \" $testName\ " completed successfully . \n " ;
}
2017-10-04 23:22:56 +02:00
function setup ()
2017-05-02 02:01:30 +02:00
{
set_time_limit ( 0 );
sqlsrv_configure ( 'LogSubsystems' , SQLSRV_LOG_SYSTEM_OFF );
sqlsrv_configure ( 'WarningsReturnAsErrors' , 1 );
}
2017-10-04 23:22:56 +02:00
function configure ( $param , $expected )
2017-05-02 02:01:30 +02:00
{
sqlsrv_configure ( $param , $expected );
$actual = sqlsrv_get_config ( $param );
2017-10-04 23:22:56 +02:00
if ( $actual == $expected ) {
trace ( " Set configuration parameter $param = $actual . \n " );
} else {
die ( " Failed to set configuration parameter $param = $expected . " );
2017-05-02 02:01:30 +02:00
}
}
2017-10-04 23:22:56 +02:00
function connect ( $options = array ())
2017-05-02 02:01:30 +02:00
{
2017-10-04 23:22:56 +02:00
include ( 'MsSetup.inc' );
2017-05-02 02:01:30 +02:00
2017-12-14 22:45:56 +01:00
if ( ! empty ( $options )) {
2017-05-02 02:01:30 +02:00
$connectionOptions = array_merge ( $connectionOptions , $options );
}
2017-10-04 23:22:56 +02:00
trace ( " Attempting connection to $server ... " );
2017-05-02 02:01:30 +02:00
$conn = sqlsrv_connect ( $server , $connectionOptions );
2017-10-04 23:22:56 +02:00
if ( $conn === false ) {
fatalError ( " Failed to connect to $server . " );
2017-05-02 02:01:30 +02:00
}
2017-10-04 23:22:56 +02:00
trace ( " successfully connected. \n \n " );
2017-05-02 02:01:30 +02:00
return ( $conn );
}
2017-10-04 23:22:56 +02:00
function getTempTableName ( $table = '' , $temporary = true )
2017-09-14 02:46:44 +02:00
{
2017-10-04 23:22:56 +02:00
// A temporary table name with the '#' prefix will be automatically
// dropped once the connection is closed. Otherwise, the caller
// should take care of dropping the temp table afterwards.
2017-09-14 02:46:44 +02:00
2017-10-04 23:22:56 +02:00
$someNumber = rand ( 0 , 1000 );
2017-09-14 02:46:44 +02:00
2017-10-04 23:22:56 +02:00
$prefix = '' ;
if ( $temporary ) {
$prefix = '#' ;
2017-09-15 00:04:55 +02:00
}
2017-09-14 02:46:44 +02:00
2017-10-04 23:22:56 +02:00
if ( strlen ( $table ) == 0 ) {
$table = 'php_test_table' ;
2017-09-14 02:46:44 +02:00
}
2017-09-15 00:04:55 +02:00
2017-07-25 20:06:02 +02:00
return $prefix . $table . '_' . $someNumber ;
2017-05-02 02:01:30 +02:00
}
2017-10-04 23:22:56 +02:00
function getTempProcName ( $proc = '' , $temporary = true )
2017-05-02 02:01:30 +02:00
{
2017-10-04 23:22:56 +02:00
// A temporary stored procedure name with the '#' prefix will be
// automatically dropped once the connection is closed. Otherwise,
// the caller should take care of dropping the temp procedure afterwards.
2017-07-25 20:06:02 +02:00
$someNumber = rand ( 0 , 1000 );
2017-05-02 02:01:30 +02:00
$prefix = '' ;
2017-10-04 23:22:56 +02:00
if ( $temporary ) {
2017-05-02 02:01:30 +02:00
$prefix = '#' ;
2017-10-04 23:22:56 +02:00
}
if ( strlen ( $proc ) == 0 ) {
$proc = 'php_test_proc' ;
}
2017-05-02 02:01:30 +02:00
2017-07-25 20:06:02 +02:00
return $prefix . $proc . '_' . $someNumber ;
2017-05-02 02:01:30 +02:00
}
2017-10-04 23:22:56 +02:00
function executeQuery ( $conn , $query )
2017-05-02 02:01:30 +02:00
{
$stmt = sqlsrv_query ( $conn , $query );
2017-10-04 23:22:56 +02:00
if ( $stmt === false ) {
fatalError ( " Query execution failed: $query " );
2017-05-02 02:01:30 +02:00
}
return ( $stmt );
}
2017-10-04 23:22:56 +02:00
function prepareQuery ( $conn , $query )
2017-05-02 02:01:30 +02:00
{
$stmt = sqlsrv_prepare ( $conn , $query );
2017-10-04 23:22:56 +02:00
if ( $stmt === false ) {
fatalError ( " Query preparation failed: $query " );
2017-05-02 02:01:30 +02:00
}
return ( $stmt );
}
2017-10-04 23:22:56 +02:00
function executeQueryEx ( $conn , $query , $modeDirect )
2017-05-02 02:01:30 +02:00
{
2017-10-04 23:22:56 +02:00
if ( $modeDirect ) { // direct execution
2017-05-02 02:01:30 +02:00
$stmt = sqlsrv_query ( $conn , $query );
2017-10-04 23:22:56 +02:00
} else {
$stmt = prepareQuery ( $conn , $query );
2017-05-02 02:01:30 +02:00
sqlsrv_execute ( $stmt );
}
return ( $stmt );
}
2017-10-04 23:22:56 +02:00
function createTableEx ( $conn , $tableName , $dataType )
2017-05-02 02:01:30 +02:00
{
$sql = " CREATE TABLE [ $tableName ] ( $dataType ) " ;
2017-10-04 23:22:56 +02:00
dropTable ( $conn , $tableName );
2017-05-02 02:01:30 +02:00
$stmt = sqlsrv_query ( $conn , $sql );
2017-10-04 23:22:56 +02:00
if ( $stmt === false ) {
fatalError ( " Failed to create test table: " . $sql );
2017-05-02 02:01:30 +02:00
}
sqlsrv_free_stmt ( $stmt );
}
2017-10-04 23:22:56 +02:00
function createTableIndex ( $conn , $tableName , $colIndex )
2017-05-02 02:01:30 +02:00
{
include 'MsSetup.inc' ;
2017-10-04 23:22:56 +02:00
createTableIndexEx ( $conn , $tableName , $tableIndex , $colIndex );
2017-05-02 02:01:30 +02:00
}
2017-10-04 23:22:56 +02:00
function createTableIndexEx ( $conn , $tableName , $tableIndex , $colIndex )
2017-05-02 02:01:30 +02:00
{
2017-10-04 23:22:56 +02:00
trace ( " Creating table index for $tableName ... " );
2017-05-02 02:01:30 +02:00
$sqlIndex = " CREATE CLUSTERED INDEX [ $tableIndex ] ON [ $tableName ]( $colIndex ) " ;
$stmt = sqlsrv_query ( $conn , $sqlIndex );
2017-10-04 23:22:56 +02:00
if ( $stmt === false ) {
fatalError ( " Failed to create clustered index for test table: " . $sqlIndex );
2017-05-02 02:01:30 +02:00
}
sqlsrv_free_stmt ( $stmt );
2017-10-04 23:22:56 +02:00
trace ( " completed successfully. \n " );
2017-05-02 02:01:30 +02:00
}
2017-10-04 23:22:56 +02:00
function createUniqueIndex ( $conn , $tableName , $colIndex )
2017-05-02 02:01:30 +02:00
{
include 'MsSetup.inc' ;
2017-10-04 23:22:56 +02:00
createUniqueIndexEx ( $conn , $tableName , $tableIndex , $colIndex );
2017-05-02 02:01:30 +02:00
}
2017-10-04 23:22:56 +02:00
function createUniqueIndexEx ( $conn , $tableName , $tableIndex , $colIndex )
2017-05-02 02:01:30 +02:00
{
2017-10-04 23:22:56 +02:00
trace ( " Creating unique table index for $tableName ... " );
2017-05-02 02:01:30 +02:00
$sqlIndex = " CREATE UNIQUE INDEX [ $tableIndex ] ON [ $tableName ]( $colIndex ) " ;
$stmt = sqlsrv_query ( $conn , $sqlIndex );
2017-10-04 23:22:56 +02:00
if ( $stmt === false ) {
fatalError ( " Failed to create unique index for test table: " . $sqlIndex );
2017-05-02 02:01:30 +02:00
}
sqlsrv_free_stmt ( $stmt );
2017-10-04 23:22:56 +02:00
trace ( " completed successfully. \n " );
2017-05-02 02:01:30 +02:00
}
2017-10-04 23:22:56 +02:00
function dropTable ( $conn , $tableName )
2017-05-02 02:01:30 +02:00
{
2017-12-08 20:50:44 +01:00
$stmt = sqlsrv_query ( $conn , " IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N' " . $tableName . " ') AND type in (N'U')) DROP TABLE [ $tableName ] " );
2017-10-04 23:22:56 +02:00
if ( $stmt === false ) {
} else {
2017-05-02 02:01:30 +02:00
sqlsrv_free_stmt ( $stmt );
}
}
2017-10-04 23:22:56 +02:00
function selectFromTable ( $conn , $tableName )
2017-05-02 02:01:30 +02:00
{
2017-10-04 23:22:56 +02:00
return ( selectFromTableEx ( $conn , $tableName , null ));
2017-05-02 02:01:30 +02:00
}
2017-10-04 23:22:56 +02:00
function selectFromTableEx ( $conn , $tableName , $cond )
2017-05-02 02:01:30 +02:00
{
2017-10-04 23:22:56 +02:00
if (( $cond != null ) && ( strlen ( $cond ) > 0 )) {
return ( selectQuery ( $conn , " SELECT * FROM [ $tableName ] WHERE $cond " ));
} else {
return ( selectQuery ( $conn , " SELECT * FROM [ $tableName ] " ));
2017-05-02 02:01:30 +02:00
}
}
2017-10-04 23:22:56 +02:00
function selectQuery ( $conn , $query )
2017-05-02 02:01:30 +02:00
{
2017-10-04 23:22:56 +02:00
return ( selectQueryEx ( $conn , $query , null ));
2017-05-02 02:01:30 +02:00
}
2017-10-04 23:22:56 +02:00
function selectQueryEx ( $conn , $query , $options )
2017-05-02 02:01:30 +02:00
{
2017-10-04 23:22:56 +02:00
if ( $options != null ) {
2017-05-02 02:01:30 +02:00
$stmt = sqlsrv_query ( $conn , $query , null , $options );
2017-10-04 23:22:56 +02:00
} else {
2017-05-02 02:01:30 +02:00
$stmt = sqlsrv_query ( $conn , $query );
}
2017-10-04 23:22:56 +02:00
if ( $stmt === false ) {
fatalError ( " Failed to query test table " );
}
2017-05-02 02:01:30 +02:00
$numFields = sqlsrv_num_fields ( $stmt );
2017-10-04 23:22:56 +02:00
if ( $numFields <= 0 ) {
2017-05-02 02:01:30 +02:00
die ( " Unexpected number of fields: " . $numFields );
}
return ( $stmt );
}
2017-10-04 23:22:56 +02:00
function rowCount ( $stmt )
2017-05-02 02:01:30 +02:00
{
$rowCount = 0 ;
2017-10-04 23:22:56 +02:00
while ( sqlsrv_fetch ( $stmt )) {
2017-05-02 02:01:30 +02:00
$rowCount ++ ;
}
return ( $rowCount );
}
2017-10-04 23:22:56 +02:00
function numRows ( $conn , $tableName )
2017-05-02 02:01:30 +02:00
{
$stmt = SelectFromTable ( $conn , $tableName );
2017-10-04 23:22:56 +02:00
$rowCount = rowCount ( $stmt );
2017-05-02 02:01:30 +02:00
sqlsrv_free_stmt ( $stmt );
return ( $rowCount );
}
2017-10-04 23:22:56 +02:00
function insertCheck ( $stmt )
2017-05-02 02:01:30 +02:00
{
2017-10-04 23:22:56 +02:00
if ( $stmt === false ) {
fatalError ( " Failed to insert row into test table " );
2017-05-02 02:01:30 +02:00
}
$numRows = sqlsrv_rows_affected ( $stmt );
sqlsrv_free_stmt ( $stmt );
2017-10-04 23:22:56 +02:00
if ( $numRows != 1 ) {
2017-05-02 02:01:30 +02:00
die ( " Unexpected row count at insert: " . $numRows );
}
return ( true );
}
2017-10-04 23:22:56 +02:00
function createProc ( $conn , $procName , $procArgs , $procCode )
2017-05-02 02:01:30 +02:00
{
2017-10-04 23:22:56 +02:00
dropProc ( $conn , $procName );
2017-05-02 02:01:30 +02:00
$stmt = sqlsrv_query ( $conn , " CREATE PROC [ $procName ] ( $procArgs ) AS BEGIN $procCode END " );
2017-10-04 23:22:56 +02:00
if ( $stmt === false ) {
2017-10-26 19:48:36 +02:00
fatalError ( " Failed to create test procedure " );
2017-05-02 02:01:30 +02:00
}
sqlsrv_free_stmt ( $stmt );
}
2017-10-04 23:22:56 +02:00
function dropProc ( $conn , $procName )
2017-05-02 02:01:30 +02:00
{
2017-11-29 01:40:49 +01:00
$stmt = sqlsrv_query ( $conn , " IF OBJECT_ID(' " . $procName . " ', 'P') IS NOT NULL DROP PROCEDURE [ $procName ] " );
2017-10-04 23:22:56 +02:00
if ( $stmt === false ) {
} else {
2017-05-02 02:01:30 +02:00
sqlsrv_free_stmt ( $stmt );
}
}
2017-10-04 23:22:56 +02:00
function callProc ( $conn , $procName , $procArgs , $procValues )
2017-05-02 02:01:30 +02:00
{
2017-10-04 23:22:56 +02:00
$stmt = callProcEx ( $conn , $procName , " " , $procArgs , $procValues );
2017-05-02 02:01:30 +02:00
sqlsrv_free_stmt ( $stmt );
}
2017-10-04 23:22:56 +02:00
function callProcEx ( $conn , $procName , $procPrefix , $procArgs , $procValues )
2017-05-02 02:01:30 +02:00
{
$stmt = sqlsrv_query ( $conn , " { $procPrefix CALL [ $procName ] ( $procArgs )} " , $procValues );
2017-10-04 23:22:56 +02:00
if ( $stmt === false ) {
2017-10-26 19:48:36 +02:00
fatalError ( " Failed to call test procedure " );
2017-05-02 02:01:30 +02:00
}
return ( $stmt );
}
2017-10-04 23:22:56 +02:00
function createFunc ( $conn , $funcName , $funcArgs , $retType , $funcCode )
2017-05-02 02:01:30 +02:00
{
2017-10-04 23:22:56 +02:00
dropFunc ( $conn , $funcName );
2017-05-02 02:01:30 +02:00
$stmt = sqlsrv_query ( $conn , " CREATE FUNCTION [ $funcName ] ( $funcArgs ) RETURNS $retType AS BEGIN $funcCode END " );
2017-10-04 23:22:56 +02:00
if ( $stmt === false ) {
fatalError ( " Failed to create test function " );
2017-05-02 02:01:30 +02:00
}
sqlsrv_free_stmt ( $stmt );
}
2017-10-04 23:22:56 +02:00
function dropFunc ( $conn , $funcName )
2017-05-02 02:01:30 +02:00
{
$stmt = sqlsrv_query ( $conn , " DROP FUNCTION [ $funcName ] " );
2017-10-04 23:22:56 +02:00
if ( $stmt === false ) {
} else {
2017-05-02 02:01:30 +02:00
sqlsrv_free_stmt ( $stmt );
}
}
2017-10-04 23:22:56 +02:00
function callFunc ( $conn , $funcName , $funcArgs , $funcValues )
2017-05-02 02:01:30 +02:00
{
$stmt = sqlsrv_query ( $conn , " { ? = CALL [ $funcName ]( $funcArgs )} " , $funcValues );
2017-10-04 23:22:56 +02:00
if ( $stmt === false ) {
fatalError ( " Failed to call test function " );
2017-05-02 02:01:30 +02:00
}
sqlsrv_free_stmt ( $stmt );
}
2017-10-24 18:22:30 +02:00
function fatalError ( $errorMsg , $print = true )
2017-05-02 02:01:30 +02:00
{
SetUTF8Data ( false );
2017-10-24 02:04:35 +02:00
if ( $print ) {
printErrors ();
} else {
handleErrors ();
}
2017-05-02 02:01:30 +02:00
die ( $errorMsg . " \n " );
}
2017-10-04 23:22:56 +02:00
function printErrors ( $message = " " )
2017-05-02 02:01:30 +02:00
{
2017-10-04 23:22:56 +02:00
if ( strlen ( $message ) > 0 ) {
2017-05-02 02:01:30 +02:00
echo $message . " \n " ;
}
$errors = sqlsrv_errors ( SQLSRV_ERR_ERRORS );
2018-05-29 19:04:05 +02:00
$count = 0 ;
if ( ! empty ( $errors )) {
$count = count ( $errors );
} else {
2017-05-02 02:01:30 +02:00
$errors = sqlsrv_errors ( SQLSRV_ERR_ALL );
2018-05-29 19:04:05 +02:00
if ( ! empty ( $errors )) {
$count = count ( $errors );
}
2017-05-02 02:01:30 +02:00
}
2018-05-29 19:04:05 +02:00
2017-10-04 23:22:56 +02:00
for ( $i = 0 ; $i < $count ; $i ++ ) {
2017-05-02 02:01:30 +02:00
echo $errors [ $i ][ 'message' ] . " \n " ;
}
}
2017-10-04 23:22:56 +02:00
function handleErrors ()
2017-05-02 02:01:30 +02:00
{
$errors = sqlsrv_errors ( SQLSRV_ERR_ERRORS );
2017-10-04 23:22:56 +02:00
$count = 0 ;
2018-05-29 19:04:05 +02:00
if ( ! empty ( $errors )) {
2017-05-02 02:01:30 +02:00
$count = count ( $errors );
2017-10-04 23:22:56 +02:00
} else {
$errors = sqlsrv_errors ( SQLSRV_ERR_ALL );
2018-05-29 19:04:05 +02:00
if ( ! empty ( $errors )) {
2017-10-04 23:22:56 +02:00
$count = count ( $errors );
2017-05-02 02:01:30 +02:00
}
}
2017-10-04 23:22:56 +02:00
for ( $i = 0 ; $i < $count ; $i ++ ) {
trace ( $errors [ $i ][ 'message' ] . " \n " );
}
2017-05-02 02:01:30 +02:00
}
2018-01-02 23:42:08 +01:00
function setUSAnsiLocale ()
2017-12-29 21:00:00 +01:00
{
2020-02-11 20:39:31 +01:00
// Do not run locale tests if locale disabled
if ( isLocaleDisabled ()) {
2018-11-08 01:37:11 +01:00
return ;
}
2020-02-11 20:39:31 +01:00
2017-12-29 21:00:00 +01:00
if ( ! isWindows ()) {
// macOS the locale names are different in Linux or macOS
$locale = strtoupper ( PHP_OS ) === 'LINUX' ? " en_US.ISO-8859-1 " : " en_US.ISO8859-1 " ;
setlocale ( LC_ALL , $locale );
}
}
2018-01-04 17:53:20 +01:00
function resetLocaleToDefault ()
{
2020-02-11 20:39:31 +01:00
// Do not run locale tests if locale disabled
if ( isLocaleDisabled ()) {
2018-11-08 01:37:11 +01:00
return ;
}
2018-01-04 17:53:20 +01:00
// Like setUSAnsiLocale() above, this method is only needed in non-Windows environment
if ( ! isWindows ()) {
setlocale ( LC_ALL , null );
}
}
2017-11-21 02:56:45 +01:00
// non-UTF8 locale support in ODBC 17 and above only
2017-12-19 23:35:28 +01:00
// if AE enabled, only supported in Windows (AE limitations)
2017-11-21 02:56:45 +01:00
function isLocaleSupported ()
{
2017-12-19 23:35:28 +01:00
if ( isWindows ()) {
return true ;
}
2020-02-11 20:39:31 +01:00
// Do not run locale tests if locale disabled
if ( isLocaleDisabled ()) {
2018-11-08 01:37:11 +01:00
return false ;
}
2018-06-26 22:41:07 +02:00
if ( AE\isDataEncrypted ()) {
2017-12-19 23:35:28 +01:00
return false ;
}
// now check ODBC version
2017-11-21 02:56:45 +01:00
$conn = AE\connect ();
$msodbcsql_ver = sqlsrv_client_info ( $conn )[ 'DriverVer' ];
if ( explode ( " . " , $msodbcsql_ver )[ 0 ] < 17 ) {
return false ;
}
2017-12-02 00:57:51 +01:00
return true ;
2017-11-21 02:56:45 +01:00
}
2017-11-29 01:40:49 +01:00
function verifyError ( $error , $state , $message )
{
if ( $error [ 'SQLSTATE' ] !== $state ) {
echo $error [ 'SQLSTATE' ] . PHP_EOL ;
fatalError ( " Unexpected SQL state \n " );
}
if ( strpos ( $error [ 'message' ], $message ) === false ) {
echo $error [ 'message' ] . PHP_EOL ;
fatalError ( " Unexpected error message \n " );
}
}
2021-05-26 00:36:01 +02:00
function getTodayDateAsString ( $conn )
{
$tsql = 'SELECT CONVERT (VARCHAR(20), GETDATE())' ;
$stmt = sqlsrv_query ( $conn , $tsql );
$result = sqlsrv_fetch ( $stmt , SQLSRV_FETCH_NUMERIC );
$today = '' ;
if ( $result ) {
$today = sqlsrv_get_field ( $stmt , 0 , SQLSRV_PHPTYPE_STRING ( SQLSRV_ENC_CHAR ));
} else {
echo " Failed to get today's date as string: " . PHP_EOL ;
print_r ( sqlsrv_errors ());
}
return $today ;
}
2017-11-21 02:56:45 +01:00
?>