0)) { return (selectQuery($conn, "SELECT * FROM [$tableName] WHERE $cond")); } else { return (selectQuery($conn, "SELECT * FROM [$tableName]")); } } function selectQuery($conn, $query) { return (selectQueryEx($conn, $query, null)); } function selectQueryEx($conn, $query, $options) { if ($options != null) { $stmt = sqlsrv_query($conn, $query, null, $options); } else { $stmt = sqlsrv_query($conn, $query); } if ($stmt === false) { fatalError("Failed to query test table"); } $numFields = sqlsrv_num_fields($stmt); if ($numFields <= 0) { die("Unexpected number of fields: ".$numFields); } return ($stmt); } function rowCount($stmt) { $rowCount = 0; while (sqlsrv_fetch($stmt)) { $rowCount++; } return ($rowCount); } function numRows($conn, $tableName) { $stmt = SelectFromTable($conn, $tableName); $rowCount = rowCount($stmt); sqlsrv_free_stmt($stmt); return ($rowCount); } function insertCheck($stmt) { if ($stmt === false) { fatalError("Failed to insert row into test table"); } $numRows = sqlsrv_rows_affected($stmt); sqlsrv_free_stmt($stmt); if ($numRows != 1) { die("Unexpected row count at insert: ".$numRows); } return (true); } function createProc($conn, $procName, $procArgs, $procCode) { dropProc($conn, $procName); $stmt = sqlsrv_query($conn, "CREATE PROC [$procName] ($procArgs) AS BEGIN $procCode END"); if ($stmt === false) { fatalError("Failed to create test procedure"); } sqlsrv_free_stmt($stmt); } function dropProc($conn, $procName) { $stmt = sqlsrv_query($conn, "IF OBJECT_ID('". $procName ."', 'P') IS NOT NULL DROP PROCEDURE [$procName]"); if ($stmt === false) { } else { sqlsrv_free_stmt($stmt); } } function callProc($conn, $procName, $procArgs, $procValues) { $stmt = callProcEx($conn, $procName, "", $procArgs, $procValues); sqlsrv_free_stmt($stmt); } function callProcEx($conn, $procName, $procPrefix, $procArgs, $procValues) { $stmt = sqlsrv_query($conn, "{ $procPrefix CALL [$procName] ($procArgs)}", $procValues); if ($stmt === false) { fatalError("Failed to call test procedure"); } return ($stmt); } function createFunc($conn, $funcName, $funcArgs, $retType, $funcCode) { dropFunc($conn, $funcName); $stmt = sqlsrv_query($conn, "CREATE FUNCTION [$funcName] ($funcArgs) RETURNS $retType AS BEGIN $funcCode END"); if ($stmt === false) { fatalError("Failed to create test function"); } sqlsrv_free_stmt($stmt); } function dropFunc($conn, $funcName) { $stmt = sqlsrv_query($conn, "DROP FUNCTION [$funcName]"); if ($stmt === false) { } else { sqlsrv_free_stmt($stmt); } } function callFunc($conn, $funcName, $funcArgs, $funcValues) { $stmt = sqlsrv_query($conn, "{ ? = CALL [$funcName]($funcArgs)}", $funcValues); if ($stmt === false) { fatalError("Failed to call test function"); } sqlsrv_free_stmt($stmt); } function fatalError($errorMsg, $print = true) { SetUTF8Data(false); if ($print) { printErrors(); } else { handleErrors(); } die($errorMsg."\n"); } function printErrors($message = "") { if (strlen($message) > 0) { echo $message . "\n"; } $errors = sqlsrv_errors(SQLSRV_ERR_ERRORS); $count = 0; if (!empty($errors)) { $count = count($errors); } else { $errors = sqlsrv_errors(SQLSRV_ERR_ALL); if (!empty($errors)) { $count = count($errors); } } for ($i = 0; $i < $count; $i++) { echo $errors[$i]['message'] . "\n"; } } function handleErrors() { $errors = sqlsrv_errors(SQLSRV_ERR_ERRORS); $count = 0; if (!empty($errors)) { $count = count($errors); } else { $errors = sqlsrv_errors(SQLSRV_ERR_ALL); if (!empty($errors)) { $count = count($errors); } } for ($i = 0; $i < $count; $i++) { trace($errors[$i]['message']."\n"); } } function setUSAnsiLocale() { // Do not run locale tests if locale disabled if (isLocaleDisabled()) { return; } 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); } } function resetLocaleToDefault() { // Do not run locale tests if locale disabled if (isLocaleDisabled()) { return; } // Like setUSAnsiLocale() above, this method is only needed in non-Windows environment if (!isWindows()) { setlocale(LC_ALL, null); } } // non-UTF8 locale support in ODBC 17 and above only // if AE enabled, only supported in Windows (AE limitations) function isLocaleSupported() { if (isWindows()) { return true; } // Do not run locale tests if locale disabled if (isLocaleDisabled()) { return false; } if (AE\isDataEncrypted()) { return false; } // now check ODBC version $conn = AE\connect(); $msodbcsql_ver = sqlsrv_client_info($conn)['DriverVer']; if (explode(".", $msodbcsql_ver)[0] < 17) { return false; } return true; } 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"); } } 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; } ?>