mirror of
https://github.com/friendica/friendica
synced 2024-05-21 17:36:42 +02:00
66 lines
1.7 KiB
PHP
66 lines
1.7 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace Friendica\Database;
|
||
|
|
||
|
class Utils
|
||
|
{
|
||
|
/**
|
||
|
* Convert parameter array to an universal form
|
||
|
*
|
||
|
* @param array $args Parameter array
|
||
|
*
|
||
|
* @return array universalized parameter array
|
||
|
*/
|
||
|
public static function getParameters(array $args)
|
||
|
{
|
||
|
unset($args[0]);
|
||
|
|
||
|
// When the second function parameter is an array then use this as the parameter array
|
||
|
if ((count($args) > 0) && (is_array($args[1]))) {
|
||
|
return $args[1];
|
||
|
} else {
|
||
|
return $args;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @brief Replaces ANY_VALUE() function by MIN() function,
|
||
|
* if the database server does not support ANY_VALUE().
|
||
|
*
|
||
|
* Considerations for Standard SQL, or MySQL with ONLY_FULL_GROUP_BY (default since 5.7.5).
|
||
|
* ANY_VALUE() is available from MySQL 5.7.5 https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html
|
||
|
* A standard fall-back is to use MIN().
|
||
|
*
|
||
|
* @param string $sql An SQL string without the values
|
||
|
* @return string The input SQL string modified if necessary.
|
||
|
*/
|
||
|
public static function anyValueFallback($sql) {
|
||
|
$server_info = self::serverInfo();
|
||
|
if (version_compare($server_info, '5.7.5', '<') ||
|
||
|
(stripos($server_info, 'MariaDB') !== false)) {
|
||
|
$sql = str_ireplace('ANY_VALUE(', 'MIN(', $sql);
|
||
|
}
|
||
|
return $sql;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @brief beautifies the query - useful for "SHOW PROCESSLIST"
|
||
|
*
|
||
|
* This is safe when we bind the parameters later.
|
||
|
* The parameter values aren't part of the SQL.
|
||
|
*
|
||
|
* @param string $sql An SQL string without the values
|
||
|
* @return string The input SQL string modified if necessary.
|
||
|
*/
|
||
|
public static function cleanQuery($sql) {
|
||
|
$search = ["\t", "\n", "\r", " "];
|
||
|
$replace = [' ', ' ', ' ', ' '];
|
||
|
do {
|
||
|
$oldsql = $sql;
|
||
|
$sql = str_replace($search, $replace, $sql);
|
||
|
} while ($oldsql != $sql);
|
||
|
|
||
|
return $sql;
|
||
|
}
|
||
|
}
|