2018-07-19 22:06:13 -04:00
|
|
|
<?php
|
2020-02-09 09:45:36 -05:00
|
|
|
/**
|
2022-01-02 02:27:47 -05:00
|
|
|
* @copyright Copyright (C) 2010-2022, the Friendica project
|
2020-02-09 09:45:36 -05:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2018-07-19 22:06:13 -04:00
|
|
|
|
|
|
|
namespace Friendica\Database;
|
|
|
|
|
2019-12-15 17:28:01 -05:00
|
|
|
use Friendica\DI;
|
2018-07-22 12:53:46 -04:00
|
|
|
use mysqli;
|
2018-07-21 08:21:23 -04:00
|
|
|
use mysqli_result;
|
|
|
|
use mysqli_stmt;
|
2018-07-19 22:24:03 -04:00
|
|
|
use PDO;
|
|
|
|
use PDOStatement;
|
2018-07-19 22:06:13 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This class is for the low level database stuff that does driver specific things.
|
|
|
|
*/
|
2019-12-15 17:28:01 -05:00
|
|
|
class DBA
|
2018-07-19 22:06:13 -04:00
|
|
|
{
|
2018-10-21 07:53:16 -04:00
|
|
|
/**
|
|
|
|
* Lowest possible date value
|
|
|
|
*/
|
2018-10-21 01:53:47 -04:00
|
|
|
const NULL_DATE = '0001-01-01';
|
2018-10-21 07:53:16 -04:00
|
|
|
/**
|
|
|
|
* Lowest possible datetime value
|
|
|
|
*/
|
2018-10-21 01:53:47 -04:00
|
|
|
const NULL_DATETIME = '0001-01-01 00:00:00';
|
|
|
|
|
2022-06-20 02:06:45 -04:00
|
|
|
public static function connect(): bool
|
2019-04-13 14:46:58 -04:00
|
|
|
{
|
2019-12-15 17:28:01 -05:00
|
|
|
return DI::dba()->connect();
|
2019-04-13 14:46:58 -04:00
|
|
|
}
|
|
|
|
|
2018-07-19 22:06:13 -04:00
|
|
|
/**
|
|
|
|
* Disconnects the current database connection
|
|
|
|
*/
|
|
|
|
public static function disconnect()
|
|
|
|
{
|
2019-12-15 17:28:01 -05:00
|
|
|
DI::dba()->disconnect();
|
2018-07-19 22:06:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform a reconnect of an existing database connection
|
|
|
|
*/
|
2022-06-20 02:06:45 -04:00
|
|
|
public static function reconnect(): bool
|
2019-06-06 18:10:45 -04:00
|
|
|
{
|
2019-12-15 17:28:01 -05:00
|
|
|
return DI::dba()->reconnect();
|
2018-07-19 22:06:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the database object.
|
|
|
|
* @return PDO|mysqli
|
|
|
|
*/
|
2018-07-20 22:11:11 -04:00
|
|
|
public static function getConnection()
|
2018-07-19 22:06:13 -04:00
|
|
|
{
|
2019-12-15 17:28:01 -05:00
|
|
|
return DI::dba()->getConnection();
|
2018-07-19 22:06:13 -04:00
|
|
|
}
|
|
|
|
|
2020-11-07 04:02:02 -05:00
|
|
|
/**
|
|
|
|
* Return the database driver string
|
|
|
|
*
|
|
|
|
* @return string with either "pdo" or "mysqli"
|
|
|
|
*/
|
2022-06-21 07:04:05 -04:00
|
|
|
public static function getDriver(): string
|
2020-11-07 04:02:02 -05:00
|
|
|
{
|
|
|
|
return DI::dba()->getDriver();
|
|
|
|
}
|
|
|
|
|
2018-07-19 22:06:13 -04:00
|
|
|
/**
|
2020-01-18 15:33:20 -05:00
|
|
|
* Returns the MySQL server version string
|
2018-07-19 22:06:13 -04:00
|
|
|
*
|
|
|
|
* This function discriminate between the deprecated mysql API and the current
|
|
|
|
* object-oriented mysqli API. Example of returned string: 5.5.46-0+deb8u1
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2022-06-19 21:10:02 -04:00
|
|
|
public static function serverInfo(): string
|
2019-06-06 18:10:45 -04:00
|
|
|
{
|
2019-12-15 17:28:01 -05:00
|
|
|
return DI::dba()->serverInfo();
|
2018-07-19 22:06:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-18 15:33:20 -05:00
|
|
|
* Returns the selected database name
|
2018-07-19 22:06:13 -04:00
|
|
|
*
|
|
|
|
* @return string
|
2019-01-06 16:06:53 -05:00
|
|
|
* @throws \Exception
|
2018-07-19 22:06:13 -04:00
|
|
|
*/
|
2022-06-19 21:10:02 -04:00
|
|
|
public static function databaseName(): string
|
2019-05-21 01:34:41 -04:00
|
|
|
{
|
2019-12-15 17:28:01 -05:00
|
|
|
return DI::dba()->databaseName();
|
2019-05-21 01:34:41 -04:00
|
|
|
}
|
|
|
|
|
2020-01-18 15:33:20 -05:00
|
|
|
/**
|
|
|
|
* Escape all SQL unsafe data
|
|
|
|
*
|
|
|
|
* @param string $str
|
|
|
|
* @return string escaped string
|
|
|
|
*/
|
2022-06-19 21:10:02 -04:00
|
|
|
public static function escape(string $str): string
|
2019-06-06 18:10:45 -04:00
|
|
|
{
|
2019-12-15 17:28:01 -05:00
|
|
|
return DI::dba()->escape($str);
|
2018-07-19 22:06:13 -04:00
|
|
|
}
|
|
|
|
|
2020-01-18 15:33:20 -05:00
|
|
|
/**
|
|
|
|
* Checks if the database is connected
|
|
|
|
*
|
|
|
|
* @return boolean is the database connected?
|
|
|
|
*/
|
2022-06-19 21:10:02 -04:00
|
|
|
public static function connected(): bool
|
2019-06-06 18:10:45 -04:00
|
|
|
{
|
2019-12-15 17:28:01 -05:00
|
|
|
return DI::dba()->connected();
|
2018-07-19 22:06:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-18 15:33:20 -05:00
|
|
|
* Replaces ANY_VALUE() function by MIN() function,
|
|
|
|
* if the database server does not support ANY_VALUE().
|
2018-07-19 22:06:13 -04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2022-06-19 21:10:02 -04:00
|
|
|
public static function anyValueFallback(string $sql): string
|
2019-06-06 18:10:45 -04:00
|
|
|
{
|
2019-12-15 17:28:01 -05:00
|
|
|
return DI::dba()->anyValueFallback($sql);
|
2018-07-19 22:06:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-18 15:33:20 -05:00
|
|
|
* beautifies the query - useful for "SHOW PROCESSLIST"
|
2018-07-19 22:06:13 -04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2022-06-19 21:10:02 -04:00
|
|
|
public static function cleanQuery(string $sql): string
|
2019-06-06 18:10:45 -04:00
|
|
|
{
|
2018-07-19 22:06:13 -04:00
|
|
|
$search = ["\t", "\n", "\r", " "];
|
|
|
|
$replace = [' ', ' ', ' ', ' '];
|
|
|
|
do {
|
|
|
|
$oldsql = $sql;
|
|
|
|
$sql = str_replace($search, $replace, $sql);
|
|
|
|
} while ($oldsql != $sql);
|
|
|
|
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-18 15:33:20 -05:00
|
|
|
* Convert parameter array to an universal form
|
2018-07-19 22:06:13 -04:00
|
|
|
* @param array $args Parameter array
|
|
|
|
* @return array universalized parameter array
|
|
|
|
*/
|
2022-06-19 21:10:02 -04:00
|
|
|
public static function getParam(array $args): array
|
2019-06-06 18:10:45 -04:00
|
|
|
{
|
2018-07-19 22:06:13 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-18 15:33:20 -05:00
|
|
|
* Executes a prepared statement that returns data
|
2021-01-17 15:32:13 -05:00
|
|
|
* Example: $r = p("SELECT * FROM `post` WHERE `guid` = ?", $guid);
|
2018-07-19 22:06:13 -04:00
|
|
|
*
|
|
|
|
* Please only use it with complicated queries.
|
2018-08-23 09:51:58 -04:00
|
|
|
* For all regular queries please use DBA::select or DBA::exists
|
2018-07-19 22:06:13 -04:00
|
|
|
*
|
|
|
|
* @param string $sql SQL statement
|
|
|
|
* @return bool|object statement object or result object
|
2019-01-06 16:06:53 -05:00
|
|
|
* @throws \Exception
|
2018-07-19 22:06:13 -04:00
|
|
|
*/
|
2022-06-19 21:10:02 -04:00
|
|
|
public static function p(string $sql)
|
2019-06-06 18:10:45 -04:00
|
|
|
{
|
2018-07-19 22:06:13 -04:00
|
|
|
$params = self::getParam(func_get_args());
|
|
|
|
|
2019-12-15 17:28:01 -05:00
|
|
|
return DI::dba()->p($sql, $params);
|
2018-07-19 22:06:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-18 15:33:20 -05:00
|
|
|
* Executes a prepared statement like UPDATE or INSERT that doesn't return data
|
2018-07-19 22:06:13 -04:00
|
|
|
*
|
2018-08-23 09:51:58 -04:00
|
|
|
* Please use DBA::delete, DBA::insert, DBA::update, ... instead
|
2018-07-19 22:06:13 -04:00
|
|
|
*
|
|
|
|
* @param string $sql SQL statement
|
|
|
|
* @return boolean Was the query successfull? False is returned only if an error occurred
|
2019-01-06 16:06:53 -05:00
|
|
|
* @throws \Exception
|
2018-07-19 22:06:13 -04:00
|
|
|
*/
|
2022-06-19 21:10:02 -04:00
|
|
|
public static function e(string $sql): bool
|
|
|
|
{
|
2018-07-19 22:06:13 -04:00
|
|
|
$params = self::getParam(func_get_args());
|
|
|
|
|
2019-12-15 17:28:01 -05:00
|
|
|
return DI::dba()->e($sql, $params);
|
2018-07-19 22:06:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-18 15:33:20 -05:00
|
|
|
* Check if data exists
|
2018-07-19 22:06:13 -04:00
|
|
|
*
|
2022-11-24 19:03:34 -05:00
|
|
|
* @param string $table Table name in format schema.table (where schema is optional)
|
2022-06-19 21:10:02 -04:00
|
|
|
* @param array $condition Array of fields for condition
|
2018-07-19 22:06:13 -04:00
|
|
|
* @return boolean Are there rows for that condition?
|
2019-01-06 16:06:53 -05:00
|
|
|
* @throws \Exception
|
2018-07-19 22:06:13 -04:00
|
|
|
*/
|
2022-06-19 21:10:02 -04:00
|
|
|
public static function exists(string $table, array $condition): bool
|
2019-06-06 18:10:45 -04:00
|
|
|
{
|
2019-12-15 17:28:01 -05:00
|
|
|
return DI::dba()->exists($table, $condition);
|
2018-07-19 22:06:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches the first row
|
|
|
|
*
|
2018-08-23 09:51:58 -04:00
|
|
|
* Please use DBA::selectFirst or DBA::exists whenever this is possible.
|
2018-07-19 22:06:13 -04:00
|
|
|
*
|
|
|
|
* @param string $sql SQL statement
|
|
|
|
* @return array first row of query
|
2019-01-06 16:06:53 -05:00
|
|
|
* @throws \Exception
|
2018-07-19 22:06:13 -04:00
|
|
|
*/
|
2022-06-19 21:10:02 -04:00
|
|
|
public static function fetchFirst(string $sql)
|
2019-06-06 18:10:45 -04:00
|
|
|
{
|
2018-07-19 22:06:13 -04:00
|
|
|
$params = self::getParam(func_get_args());
|
|
|
|
|
2019-12-15 17:28:01 -05:00
|
|
|
return DI::dba()->fetchFirst($sql, $params);
|
2018-07-19 22:06:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-18 15:33:20 -05:00
|
|
|
* Returns the number of affected rows of the last statement
|
2018-07-19 22:06:13 -04:00
|
|
|
*
|
|
|
|
* @return int Number of rows
|
|
|
|
*/
|
2022-06-19 21:10:02 -04:00
|
|
|
public static function affectedRows(): int
|
2019-06-06 18:10:45 -04:00
|
|
|
{
|
2019-12-15 17:28:01 -05:00
|
|
|
return DI::dba()->affectedRows();
|
2018-07-19 22:06:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-18 15:33:20 -05:00
|
|
|
* Returns the number of columns of a statement
|
2018-07-19 22:06:13 -04:00
|
|
|
*
|
|
|
|
* @param object Statement object
|
|
|
|
* @return int Number of columns
|
|
|
|
*/
|
2022-06-19 21:10:02 -04:00
|
|
|
public static function columnCount($stmt): int
|
2019-06-06 18:10:45 -04:00
|
|
|
{
|
2019-12-15 17:28:01 -05:00
|
|
|
return DI::dba()->columnCount($stmt);
|
2018-07-19 22:06:13 -04:00
|
|
|
}
|
|
|
|
/**
|
2020-01-18 15:33:20 -05:00
|
|
|
* Returns the number of rows of a statement
|
2018-07-19 22:06:13 -04:00
|
|
|
*
|
|
|
|
* @param PDOStatement|mysqli_result|mysqli_stmt Statement object
|
|
|
|
* @return int Number of rows
|
|
|
|
*/
|
2022-06-19 21:10:02 -04:00
|
|
|
public static function numRows($stmt): int
|
2019-06-06 18:10:45 -04:00
|
|
|
{
|
2019-12-15 17:28:01 -05:00
|
|
|
return DI::dba()->numRows($stmt);
|
2018-07-19 22:06:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-18 15:33:20 -05:00
|
|
|
* Fetch a single row
|
2018-07-19 22:06:13 -04:00
|
|
|
*
|
|
|
|
* @param mixed $stmt statement object
|
|
|
|
* @return array current row
|
|
|
|
*/
|
2019-06-06 18:10:45 -04:00
|
|
|
public static function fetch($stmt)
|
2019-05-20 16:38:18 -04:00
|
|
|
{
|
2019-12-15 17:28:01 -05:00
|
|
|
return DI::dba()->fetch($stmt);
|
2019-05-20 16:38:18 -04:00
|
|
|
}
|
|
|
|
|
2018-07-19 22:06:13 -04:00
|
|
|
/**
|
2020-01-18 15:33:20 -05:00
|
|
|
* Insert a row into a table
|
2018-07-19 22:06:13 -04:00
|
|
|
*
|
2022-11-24 19:03:34 -05:00
|
|
|
* @param string $table Table name in format schema.table (where schema is optional)
|
2022-06-21 05:44:23 -04:00
|
|
|
* @param array $param parameter array
|
|
|
|
* @param int $duplicate_mode What to do on a duplicated entry
|
2018-10-14 11:32:54 -04:00
|
|
|
* @return boolean was the insert successful?
|
2019-01-06 16:06:53 -05:00
|
|
|
* @throws \Exception
|
2018-07-19 22:06:13 -04:00
|
|
|
*/
|
2022-06-21 05:44:23 -04:00
|
|
|
public static function insert(string $table, array $param, int $duplicate_mode = Database::INSERT_DEFAULT): bool
|
2019-06-06 18:10:45 -04:00
|
|
|
{
|
2020-11-19 14:34:48 -05:00
|
|
|
return DI::dba()->insert($table, $param, $duplicate_mode);
|
2018-07-19 22:06:13 -04:00
|
|
|
}
|
|
|
|
|
2020-08-26 16:16:57 -04:00
|
|
|
/**
|
2020-08-26 16:44:51 -04:00
|
|
|
* Inserts a row with the provided data in the provided table.
|
|
|
|
* If the data corresponds to an existing row through a UNIQUE or PRIMARY index constraints, it updates the row instead.
|
2020-08-26 16:16:57 -04:00
|
|
|
*
|
2022-11-24 19:03:34 -05:00
|
|
|
* @param string $table Table name in format schema.table (where schema is optional)
|
2022-06-21 05:44:23 -04:00
|
|
|
* @param array $param parameter array
|
2020-08-26 16:16:57 -04:00
|
|
|
* @return boolean was the insert successful?
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2022-06-21 05:44:23 -04:00
|
|
|
public static function replace(string $table, array $param): bool
|
2020-08-26 16:16:57 -04:00
|
|
|
{
|
|
|
|
return DI::dba()->replace($table, $param);
|
|
|
|
}
|
|
|
|
|
2018-07-19 22:06:13 -04:00
|
|
|
/**
|
2020-01-18 15:33:20 -05:00
|
|
|
* Fetch the id of the last insert command
|
2018-07-19 22:06:13 -04:00
|
|
|
*
|
|
|
|
* @return integer Last inserted id
|
|
|
|
*/
|
2022-06-19 21:10:02 -04:00
|
|
|
public static function lastInsertId(): int
|
2019-06-06 18:10:45 -04:00
|
|
|
{
|
2019-12-15 17:28:01 -05:00
|
|
|
return DI::dba()->lastInsertId();
|
2018-07-19 22:06:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-18 15:33:20 -05:00
|
|
|
* Locks a table for exclusive write access
|
2018-07-19 22:06:13 -04:00
|
|
|
*
|
|
|
|
* This function can be extended in the future to accept a table array as well.
|
|
|
|
*
|
2022-11-24 19:03:34 -05:00
|
|
|
* @param string $table Table name in format schema.table (where schema is optional)
|
2018-07-19 22:06:13 -04:00
|
|
|
* @return boolean was the lock successful?
|
2019-01-06 16:06:53 -05:00
|
|
|
* @throws \Exception
|
2018-07-19 22:06:13 -04:00
|
|
|
*/
|
2022-06-21 05:44:23 -04:00
|
|
|
public static function lock(string $table): bool
|
2019-06-06 18:10:45 -04:00
|
|
|
{
|
2019-12-15 17:28:01 -05:00
|
|
|
return DI::dba()->lock($table);
|
2018-07-19 22:06:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-18 15:33:20 -05:00
|
|
|
* Unlocks all locked tables
|
2018-07-19 22:06:13 -04:00
|
|
|
*
|
|
|
|
* @return boolean was the unlock successful?
|
2019-01-06 16:06:53 -05:00
|
|
|
* @throws \Exception
|
2018-07-19 22:06:13 -04:00
|
|
|
*/
|
2022-06-19 21:10:02 -04:00
|
|
|
public static function unlock(): bool
|
2019-06-06 18:10:45 -04:00
|
|
|
{
|
2019-12-15 17:28:01 -05:00
|
|
|
return DI::dba()->unlock();
|
2018-07-19 22:06:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-18 15:33:20 -05:00
|
|
|
* Starts a transaction
|
2018-07-19 22:06:13 -04:00
|
|
|
*
|
|
|
|
* @return boolean Was the command executed successfully?
|
|
|
|
*/
|
2022-06-19 21:10:02 -04:00
|
|
|
public static function transaction(): bool
|
2018-07-19 22:06:13 -04:00
|
|
|
{
|
2019-12-15 17:28:01 -05:00
|
|
|
return DI::dba()->transaction();
|
2018-07-19 22:06:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-18 15:33:20 -05:00
|
|
|
* Does a commit
|
2018-07-19 22:06:13 -04:00
|
|
|
*
|
|
|
|
* @return boolean Was the command executed successfully?
|
|
|
|
*/
|
2022-06-19 21:10:02 -04:00
|
|
|
public static function commit(): bool
|
2019-06-06 18:10:45 -04:00
|
|
|
{
|
2019-12-15 17:28:01 -05:00
|
|
|
return DI::dba()->commit();
|
2018-07-19 22:06:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-18 15:33:20 -05:00
|
|
|
* Does a rollback
|
2018-07-19 22:06:13 -04:00
|
|
|
*
|
|
|
|
* @return boolean Was the command executed successfully?
|
|
|
|
*/
|
2022-06-19 21:10:02 -04:00
|
|
|
public static function rollback(): bool
|
2019-06-06 18:10:45 -04:00
|
|
|
{
|
2019-12-15 17:28:01 -05:00
|
|
|
return DI::dba()->rollback();
|
2018-07-19 22:06:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-18 15:33:20 -05:00
|
|
|
* Delete a row from a table
|
2018-07-19 22:06:13 -04:00
|
|
|
*
|
2022-06-21 05:44:23 -04:00
|
|
|
* @param string $table Table name
|
|
|
|
* @param array $conditions Field condition(s)
|
2018-07-19 22:06:13 -04:00
|
|
|
*
|
2018-12-02 11:25:25 -05:00
|
|
|
* @return boolean was the delete successful?
|
2019-01-06 16:06:53 -05:00
|
|
|
* @throws \Exception
|
2018-07-19 22:06:13 -04:00
|
|
|
*/
|
2022-06-21 05:44:23 -04:00
|
|
|
public static function delete(string $table, array $conditions, array $options = []): bool
|
2018-07-19 22:06:13 -04:00
|
|
|
{
|
2019-12-15 17:28:01 -05:00
|
|
|
return DI::dba()->delete($table, $conditions, $options);
|
2018-07-19 22:06:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-19 01:05:23 -05:00
|
|
|
* Updates rows in the database.
|
|
|
|
*
|
|
|
|
* When $old_fields is set to an array,
|
2018-07-19 22:06:13 -04:00
|
|
|
* the system will only do an update if the fields in that array changed.
|
|
|
|
*
|
|
|
|
* Attention:
|
|
|
|
* Only the values in $old_fields are compared.
|
|
|
|
* This is an intentional behaviour.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* We include the timestamp field in $fields but not in $old_fields.
|
|
|
|
* Then the row will only get the new timestamp when the other fields had changed.
|
|
|
|
*
|
|
|
|
* When $old_fields is set to a boolean value the system will do this compare itself.
|
|
|
|
* When $old_fields is set to "true" the system will do an insert if the row doesn't exists.
|
|
|
|
*
|
|
|
|
* Attention:
|
|
|
|
* Only set $old_fields to a boolean value when you are sure that you will update a single row.
|
|
|
|
* When you set $old_fields to "true" then $fields must contain all relevant fields!
|
|
|
|
*
|
2022-11-24 19:03:34 -05:00
|
|
|
* @param string $table Table name in format schema.table (where schema is optional)
|
2019-01-06 16:06:53 -05:00
|
|
|
* @param array $fields contains the fields that are updated
|
|
|
|
* @param array $condition condition array with the key values
|
2021-09-05 14:37:42 -04:00
|
|
|
* @param array|boolean $old_fields array with the old field values that are about to be replaced (true = update on duplicate, false = don't update identical fields)
|
2022-06-01 18:11:03 -04:00
|
|
|
* @param array $params Parameters: "ignore" If set to "true" then the update is done with the ignore parameter
|
2018-07-19 22:06:13 -04:00
|
|
|
*
|
|
|
|
* @return boolean was the update successfull?
|
2019-01-06 16:06:53 -05:00
|
|
|
* @throws \Exception
|
2018-07-19 22:06:13 -04:00
|
|
|
*/
|
2022-06-21 05:44:23 -04:00
|
|
|
public static function update(string $table, array $fields, array $condition, $old_fields = [], array $params = []): bool
|
2019-06-06 18:10:45 -04:00
|
|
|
{
|
2022-06-01 18:11:03 -04:00
|
|
|
return DI::dba()->update($table, $fields, $condition, $old_fields, $params);
|
2018-07-19 22:06:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve a single record from a table and returns it in an associative array
|
|
|
|
*
|
2022-11-24 19:03:34 -05:00
|
|
|
* @param string|array $table Table name in format schema.table (where schema is optional)
|
2019-07-28 23:00:29 -04:00
|
|
|
* @param array $fields
|
|
|
|
* @param array $condition
|
|
|
|
* @param array $params
|
2018-07-19 22:06:13 -04:00
|
|
|
* @return bool|array
|
2019-01-06 16:06:53 -05:00
|
|
|
* @throws \Exception
|
|
|
|
* @see self::select
|
2018-07-19 22:06:13 -04:00
|
|
|
*/
|
2022-06-16 14:42:40 -04:00
|
|
|
public static function selectFirst($table, array $fields = [], array $condition = [], array $params = [])
|
2018-07-19 22:06:13 -04:00
|
|
|
{
|
2019-12-15 17:28:01 -05:00
|
|
|
return DI::dba()->selectFirst($table, $fields, $condition, $params);
|
2018-07-19 22:06:13 -04:00
|
|
|
}
|
|
|
|
|
2019-07-27 10:33:17 -04:00
|
|
|
/**
|
2020-01-18 15:33:20 -05:00
|
|
|
* Select rows from a table and fills an array with the data
|
2019-07-27 10:33:17 -04:00
|
|
|
*
|
2022-11-24 19:03:34 -05:00
|
|
|
* @param string $table Table name in format schema.table (where schema is optional)
|
2022-06-21 06:09:51 -04:00
|
|
|
* @param array $fields Array of selected fields, empty for all
|
|
|
|
* @param array $condition Array of fields for condition
|
|
|
|
* @param array $params Array of several parameters
|
2019-07-27 10:33:17 -04:00
|
|
|
*
|
|
|
|
* @return array Data array
|
|
|
|
* @throws \Exception
|
|
|
|
* @see self::select
|
|
|
|
*/
|
2022-06-21 06:09:51 -04:00
|
|
|
public static function selectToArray(string $table, array $fields = [], array $condition = [], array $params = [])
|
2019-07-27 10:33:17 -04:00
|
|
|
{
|
2019-12-15 17:28:01 -05:00
|
|
|
return DI::dba()->selectToArray($table, $fields, $condition, $params);
|
2019-07-27 10:33:17 -04:00
|
|
|
}
|
|
|
|
|
2018-07-19 22:06:13 -04:00
|
|
|
/**
|
2020-01-18 15:33:20 -05:00
|
|
|
* Select rows from a table
|
2018-07-19 22:06:13 -04:00
|
|
|
*
|
2022-11-24 19:03:34 -05:00
|
|
|
* @param string $table Table name in format schema.table (where schema is optional)
|
2022-06-21 05:44:23 -04:00
|
|
|
* @param array $fields Array of selected fields, empty for all
|
|
|
|
* @param array $condition Array of fields for condition
|
|
|
|
* @param array $params Array of several parameters
|
2018-07-19 22:06:13 -04:00
|
|
|
*
|
|
|
|
* @return boolean|object
|
|
|
|
*
|
|
|
|
* Example:
|
2021-02-06 08:42:21 -05:00
|
|
|
* $table = "post";
|
2018-07-19 22:06:13 -04:00
|
|
|
* $fields = array("id", "uri", "uid", "network");
|
|
|
|
*
|
|
|
|
* $condition = array("uid" => 1, "network" => 'dspr');
|
|
|
|
* or:
|
|
|
|
* $condition = array("`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr');
|
|
|
|
*
|
|
|
|
* $params = array("order" => array("id", "received" => true), "limit" => 10);
|
|
|
|
*
|
2018-08-23 09:51:58 -04:00
|
|
|
* $data = DBA::select($table, $fields, $condition, $params);
|
2019-01-06 16:06:53 -05:00
|
|
|
* @throws \Exception
|
2018-07-19 22:06:13 -04:00
|
|
|
*/
|
2022-06-21 05:44:23 -04:00
|
|
|
public static function select(string $table, array $fields = [], array $condition = [], array $params = [])
|
2018-07-19 22:06:13 -04:00
|
|
|
{
|
2019-12-15 17:28:01 -05:00
|
|
|
return DI::dba()->select($table, $fields, $condition, $params);
|
2018-07-19 22:06:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-18 15:33:20 -05:00
|
|
|
* Counts the rows from a table satisfying the provided condition
|
2018-07-19 22:06:13 -04:00
|
|
|
*
|
2022-11-24 19:03:34 -05:00
|
|
|
* @param string $table Table name in format schema.table (where schema is optional)
|
2022-06-21 05:44:23 -04:00
|
|
|
* @param array $condition array of fields for condition
|
|
|
|
* @param array $params Array of several parameters
|
2018-07-19 22:06:13 -04:00
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*
|
|
|
|
* Example:
|
2021-02-06 08:42:21 -05:00
|
|
|
* $table = "post";
|
2018-07-19 22:06:13 -04:00
|
|
|
*
|
|
|
|
* $condition = ["uid" => 1, "network" => 'dspr'];
|
|
|
|
* or:
|
|
|
|
* $condition = ["`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr'];
|
|
|
|
*
|
2018-08-23 09:51:58 -04:00
|
|
|
* $count = DBA::count($table, $condition);
|
2019-01-06 16:06:53 -05:00
|
|
|
* @throws \Exception
|
2018-07-19 22:06:13 -04:00
|
|
|
*/
|
2022-06-21 05:44:23 -04:00
|
|
|
public static function count(string $table, array $condition = [], array $params = []): int
|
2018-07-19 22:06:13 -04:00
|
|
|
{
|
2019-12-15 17:28:01 -05:00
|
|
|
return DI::dba()->count($table, $condition, $params);
|
2018-07-19 22:06:13 -04:00
|
|
|
}
|
|
|
|
|
2019-07-28 23:00:29 -04:00
|
|
|
/**
|
|
|
|
* Build the table query substring from one or more tables, with or without a schema.
|
|
|
|
*
|
|
|
|
* Expected formats:
|
|
|
|
* - table
|
|
|
|
* - [table1, table2, ...]
|
|
|
|
* - [schema1 => table1, schema2 => table2, table3, ...]
|
|
|
|
*
|
2022-06-21 05:44:23 -04:00
|
|
|
* @param array $tables Table names
|
2019-07-28 23:00:29 -04:00
|
|
|
* @return string
|
|
|
|
*/
|
2022-06-21 05:44:23 -04:00
|
|
|
public static function buildTableString(array $tables): string
|
2019-07-28 23:00:29 -04:00
|
|
|
{
|
2022-06-21 12:11:59 -04:00
|
|
|
// Quote each entry
|
|
|
|
return implode(',', array_map(['self', 'quoteIdentifier'], $tables));
|
2019-07-28 23:00:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-07-27 19:54:02 -04:00
|
|
|
* Escape an identifier (table or field name) optional with a schema like ((schema.)table.)field
|
2019-07-28 23:00:29 -04:00
|
|
|
*
|
2022-07-27 19:54:02 -04:00
|
|
|
* @param string $identifier Table, field name
|
2022-06-21 12:11:59 -04:00
|
|
|
* @return string Quotes table or field name
|
2019-07-28 23:00:29 -04:00
|
|
|
*/
|
2022-06-19 21:10:02 -04:00
|
|
|
public static function quoteIdentifier(string $identifier): string
|
2019-07-28 23:00:29 -04:00
|
|
|
{
|
2022-06-21 12:11:59 -04:00
|
|
|
return implode(
|
|
|
|
'.',
|
|
|
|
array_map(
|
|
|
|
function (string $identifier) { return '`' . str_replace('`', '``', $identifier) . '`'; },
|
|
|
|
explode('.', $identifier)
|
|
|
|
)
|
|
|
|
);
|
2019-07-28 23:00:29 -04:00
|
|
|
}
|
|
|
|
|
2018-07-19 22:06:13 -04:00
|
|
|
/**
|
2020-01-18 15:33:20 -05:00
|
|
|
* Returns the SQL condition string built from the provided condition array
|
2018-07-19 22:06:13 -04:00
|
|
|
*
|
|
|
|
* This function operates with two modes.
|
2020-07-29 11:30:54 -04:00
|
|
|
* - Supplied with a field/value associative array, it builds simple strict
|
2018-07-19 22:06:13 -04:00
|
|
|
* equality conditions linked by AND.
|
|
|
|
* - Supplied with a flat list, the first element is the condition string and
|
|
|
|
* the following arguments are the values to be interpolated
|
|
|
|
*
|
|
|
|
* $condition = ["uid" => 1, "network" => 'dspr'];
|
|
|
|
* or:
|
|
|
|
* $condition = ["`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr'];
|
|
|
|
*
|
|
|
|
* In either case, the provided array is left with the parameters only
|
|
|
|
*
|
|
|
|
* @param array $condition
|
|
|
|
* @return string
|
|
|
|
*/
|
2022-06-19 21:10:02 -04:00
|
|
|
public static function buildCondition(array &$condition = []): string
|
2018-07-19 22:06:13 -04:00
|
|
|
{
|
2020-01-05 17:22:07 -05:00
|
|
|
$condition = self::collapseCondition($condition);
|
|
|
|
|
2018-07-19 22:06:13 -04:00
|
|
|
$condition_string = '';
|
|
|
|
if (count($condition) > 0) {
|
2020-01-05 17:22:07 -05:00
|
|
|
$condition_string = " WHERE (" . array_shift($condition) . ")";
|
|
|
|
}
|
2018-07-19 22:06:13 -04:00
|
|
|
|
2020-01-05 17:22:07 -05:00
|
|
|
return $condition_string;
|
|
|
|
}
|
2018-07-19 22:06:13 -04:00
|
|
|
|
2020-01-05 17:22:07 -05:00
|
|
|
/**
|
|
|
|
* Collapse an associative array condition into a SQL string + parameters condition array.
|
|
|
|
*
|
|
|
|
* ['uid' => 1, 'network' => ['dspr', 'apub']]
|
|
|
|
*
|
|
|
|
* gets transformed into
|
|
|
|
*
|
|
|
|
* ["`uid` = ? AND `network` IN (?, ?)", 1, 'dspr', 'apub']
|
|
|
|
*
|
|
|
|
* @param array $condition
|
|
|
|
* @return array
|
|
|
|
*/
|
2022-06-19 21:10:02 -04:00
|
|
|
public static function collapseCondition(array $condition): array
|
2020-01-05 17:22:07 -05:00
|
|
|
{
|
|
|
|
// Ensures an always true condition is returned
|
|
|
|
if (count($condition) < 1) {
|
|
|
|
return ['1'];
|
|
|
|
}
|
|
|
|
|
|
|
|
reset($condition);
|
|
|
|
$first_key = key($condition);
|
|
|
|
|
|
|
|
if (is_int($first_key)) {
|
|
|
|
// Already collapsed
|
|
|
|
return $condition;
|
|
|
|
}
|
|
|
|
|
|
|
|
$values = [];
|
|
|
|
$condition_string = "";
|
|
|
|
foreach ($condition as $field => $value) {
|
|
|
|
if ($condition_string != "") {
|
|
|
|
$condition_string .= " AND ";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_array($value)) {
|
|
|
|
if (count($value)) {
|
|
|
|
/* Workaround for MySQL Bug #64791.
|
|
|
|
* Never mix data types inside any IN() condition.
|
|
|
|
* In case of mixed types, cast all as string.
|
|
|
|
* Logic needs to be consistent with DBA::p() data types.
|
|
|
|
*/
|
|
|
|
$is_int = false;
|
|
|
|
$is_alpha = false;
|
|
|
|
foreach ($value as $single_value) {
|
|
|
|
if (is_int($single_value)) {
|
|
|
|
$is_int = true;
|
2019-12-24 16:21:00 -05:00
|
|
|
} else {
|
2020-01-05 17:22:07 -05:00
|
|
|
$is_alpha = true;
|
2019-12-24 16:21:00 -05:00
|
|
|
}
|
2018-07-19 22:06:13 -04:00
|
|
|
}
|
2020-01-05 17:22:07 -05:00
|
|
|
|
|
|
|
if ($is_int && $is_alpha) {
|
|
|
|
foreach ($value as &$ref) {
|
|
|
|
if (is_int($ref)) {
|
|
|
|
$ref = (string)$ref;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unset($ref); //Prevent accidental re-use.
|
|
|
|
}
|
|
|
|
|
|
|
|
$values = array_merge($values, array_values($value));
|
|
|
|
$placeholders = substr(str_repeat("?, ", count($value)), 0, -2);
|
|
|
|
$condition_string .= self::quoteIdentifier($field) . " IN (" . $placeholders . ")";
|
|
|
|
} else {
|
|
|
|
// Empty value array isn't supported by IN and is logically equivalent to no match
|
|
|
|
$condition_string .= "FALSE";
|
2018-07-19 22:06:13 -04:00
|
|
|
}
|
2020-01-05 17:22:07 -05:00
|
|
|
} elseif (is_null($value)) {
|
|
|
|
$condition_string .= self::quoteIdentifier($field) . " IS NULL";
|
|
|
|
} else {
|
|
|
|
$values[$field] = $value;
|
|
|
|
$condition_string .= self::quoteIdentifier($field) . " = ?";
|
2018-07-19 22:06:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-05 17:22:07 -05:00
|
|
|
$condition = array_merge([$condition_string], array_values($values));
|
|
|
|
|
|
|
|
return $condition;
|
2018-07-19 22:06:13 -04:00
|
|
|
}
|
|
|
|
|
2020-07-29 11:30:54 -04:00
|
|
|
/**
|
|
|
|
* Merges the provided conditions into a single collapsed one
|
|
|
|
*
|
|
|
|
* @param array ...$conditions One or more condition arrays
|
|
|
|
* @return array A collapsed condition
|
|
|
|
* @see DBA::collapseCondition() for the condition array formats
|
|
|
|
*/
|
2022-06-19 21:10:02 -04:00
|
|
|
public static function mergeConditions(array ...$conditions): array
|
2020-07-29 11:30:54 -04:00
|
|
|
{
|
2020-09-06 16:28:08 -04:00
|
|
|
if (count($conditions) == 1) {
|
|
|
|
return current($conditions);
|
|
|
|
}
|
|
|
|
|
2020-07-29 11:30:54 -04:00
|
|
|
$conditionStrings = [];
|
|
|
|
$result = [];
|
|
|
|
|
|
|
|
foreach ($conditions as $key => $condition) {
|
2020-09-07 01:00:17 -04:00
|
|
|
if (!$condition) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-07-29 11:30:54 -04:00
|
|
|
$condition = self::collapseCondition($condition);
|
|
|
|
|
|
|
|
$conditionStrings[] = array_shift($condition);
|
|
|
|
// The result array holds the eventual parameter values
|
|
|
|
$result = array_merge($result, $condition);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($conditionStrings)) {
|
|
|
|
// We prepend the condition string at the end to form a collapsed condition array again
|
|
|
|
array_unshift($result, implode(' AND ', $conditionStrings));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2018-07-19 22:06:13 -04:00
|
|
|
/**
|
2020-01-18 15:33:20 -05:00
|
|
|
* Returns the SQL parameter string built from the provided parameter array
|
2018-07-19 22:06:13 -04:00
|
|
|
*
|
2020-07-01 22:54:14 -04:00
|
|
|
* Expected format for each key:
|
|
|
|
*
|
|
|
|
* group_by:
|
|
|
|
* - list of column names
|
|
|
|
*
|
|
|
|
* order:
|
|
|
|
* - numeric keyed column name => ASC
|
|
|
|
* - associative element with boolean value => DESC (true), ASC (false)
|
|
|
|
* - associative element with string value => 'ASC' or 'DESC' literally
|
|
|
|
*
|
|
|
|
* limit:
|
|
|
|
* - single numeric value => count
|
|
|
|
* - list with two numeric values => offset, count
|
|
|
|
*
|
2018-07-19 22:06:13 -04:00
|
|
|
* @param array $params
|
|
|
|
* @return string
|
|
|
|
*/
|
2022-06-19 21:10:02 -04:00
|
|
|
public static function buildParameter(array $params = []): string
|
2018-07-19 22:06:13 -04:00
|
|
|
{
|
2019-05-18 20:50:14 -04:00
|
|
|
$groupby_string = '';
|
2019-07-28 23:00:29 -04:00
|
|
|
if (!empty($params['group_by'])) {
|
|
|
|
$groupby_string = " GROUP BY " . implode(', ', array_map(['self', 'quoteIdentifier'], $params['group_by']));
|
2019-05-18 20:50:14 -04:00
|
|
|
}
|
|
|
|
|
2018-07-19 22:06:13 -04:00
|
|
|
$order_string = '';
|
|
|
|
if (isset($params['order'])) {
|
|
|
|
$order_string = " ORDER BY ";
|
2021-10-03 06:34:41 -04:00
|
|
|
foreach ($params['order'] as $fields => $order) {
|
2019-05-18 16:17:57 -04:00
|
|
|
if ($order === 'RAND()') {
|
|
|
|
$order_string .= "RAND(), ";
|
|
|
|
} elseif (!is_int($fields)) {
|
2020-07-01 22:54:14 -04:00
|
|
|
if ($order !== 'DESC' && $order !== 'ASC') {
|
|
|
|
$order = $order ? 'DESC' : 'ASC';
|
|
|
|
}
|
|
|
|
|
|
|
|
$order_string .= self::quoteIdentifier($fields) . " " . $order . ", ";
|
2018-07-19 22:06:13 -04:00
|
|
|
} else {
|
2019-07-28 23:00:29 -04:00
|
|
|
$order_string .= self::quoteIdentifier($order) . ", ";
|
2018-07-19 22:06:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$order_string = substr($order_string, 0, -2);
|
|
|
|
}
|
|
|
|
|
|
|
|
$limit_string = '';
|
2019-03-20 00:33:26 -04:00
|
|
|
if (isset($params['limit']) && is_numeric($params['limit'])) {
|
2018-08-20 00:26:05 -04:00
|
|
|
$limit_string = " LIMIT " . intval($params['limit']);
|
2018-07-19 22:06:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($params['limit']) && is_array($params['limit'])) {
|
|
|
|
$limit_string = " LIMIT " . intval($params['limit'][0]) . ", " . intval($params['limit'][1]);
|
|
|
|
}
|
|
|
|
|
2019-05-18 23:13:06 -04:00
|
|
|
return $groupby_string . $order_string . $limit_string;
|
2018-07-19 22:06:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-18 15:33:20 -05:00
|
|
|
* Fills an array with data from a query
|
2018-07-19 22:06:13 -04:00
|
|
|
*
|
2021-02-21 02:46:58 -05:00
|
|
|
* @param object $stmt statement object
|
|
|
|
* @param bool $do_close Close database connection after last row
|
|
|
|
* @param int $count maximum number of rows to be fetched
|
|
|
|
*
|
2018-07-19 22:06:13 -04:00
|
|
|
* @return array Data array
|
|
|
|
*/
|
2022-06-19 21:10:02 -04:00
|
|
|
public static function toArray($stmt, bool $do_close = true, int $count = 0): array
|
2019-06-06 18:10:45 -04:00
|
|
|
{
|
2021-02-21 02:46:58 -05:00
|
|
|
return DI::dba()->toArray($stmt, $do_close, $count);
|
2018-07-19 22:06:13 -04:00
|
|
|
}
|
|
|
|
|
2020-11-27 06:24:31 -05:00
|
|
|
/**
|
|
|
|
* Cast field types according to the table definition
|
|
|
|
*
|
|
|
|
* @param string $table
|
|
|
|
* @param array $fields
|
|
|
|
* @return array casted fields
|
|
|
|
*/
|
2022-06-16 14:33:10 -04:00
|
|
|
public static function castFields(string $table, array $fields): array
|
2020-11-27 06:24:31 -05:00
|
|
|
{
|
|
|
|
return DI::dba()->castFields($table, $fields);
|
|
|
|
}
|
|
|
|
|
2018-07-19 22:06:13 -04:00
|
|
|
/**
|
2020-01-18 15:33:20 -05:00
|
|
|
* Returns the error number of the last query
|
2018-07-19 22:06:13 -04:00
|
|
|
*
|
|
|
|
* @return string Error number (0 if no error)
|
|
|
|
*/
|
2022-06-16 14:33:10 -04:00
|
|
|
public static function errorNo(): int
|
2019-06-06 18:10:45 -04:00
|
|
|
{
|
2019-12-15 17:28:01 -05:00
|
|
|
return DI::dba()->errorNo();
|
2018-07-19 22:06:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-18 15:33:20 -05:00
|
|
|
* Returns the error message of the last query
|
2018-07-19 22:06:13 -04:00
|
|
|
*
|
|
|
|
* @return string Error message ('' if no error)
|
|
|
|
*/
|
2022-06-16 14:33:10 -04:00
|
|
|
public static function errorMessage(): string
|
2019-06-06 18:10:45 -04:00
|
|
|
{
|
2019-12-15 17:28:01 -05:00
|
|
|
return DI::dba()->errorMessage();
|
2018-07-19 22:06:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-18 15:33:20 -05:00
|
|
|
* Closes the current statement
|
2018-07-19 22:06:13 -04:00
|
|
|
*
|
|
|
|
* @param object $stmt statement object
|
|
|
|
* @return boolean was the close successful?
|
|
|
|
*/
|
2022-06-16 14:33:10 -04:00
|
|
|
public static function close($stmt): bool
|
2019-06-06 18:10:45 -04:00
|
|
|
{
|
2019-12-15 17:28:01 -05:00
|
|
|
return DI::dba()->close($stmt);
|
2018-07-19 22:06:13 -04:00
|
|
|
}
|
2018-07-21 08:21:23 -04:00
|
|
|
|
|
|
|
/**
|
2020-01-18 15:33:20 -05:00
|
|
|
* Return a list of database processes
|
2018-07-21 08:21:23 -04:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
* 'list' => List of processes, separated in their different states
|
|
|
|
* 'amount' => Number of concurrent database processes
|
2019-01-06 16:06:53 -05:00
|
|
|
* @throws \Exception
|
2018-07-21 08:21:23 -04:00
|
|
|
*/
|
2022-06-16 14:33:10 -04:00
|
|
|
public static function processlist(): array
|
2018-07-21 08:21:23 -04:00
|
|
|
{
|
2019-12-15 17:28:01 -05:00
|
|
|
return DI::dba()->processlist();
|
2018-07-21 08:21:23 -04:00
|
|
|
}
|
|
|
|
|
2020-04-29 02:28:06 -04:00
|
|
|
/**
|
|
|
|
* Fetch a database variable
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return string content
|
|
|
|
*/
|
|
|
|
public static function getVariable(string $name)
|
|
|
|
{
|
|
|
|
return DI::dba()->getVariable($name);
|
|
|
|
}
|
|
|
|
|
2018-07-21 08:21:23 -04:00
|
|
|
/**
|
|
|
|
* Checks if $array is a filled array with at least one entry.
|
|
|
|
*
|
|
|
|
* @param mixed $array A filled array with at least one entry
|
|
|
|
* @return boolean Whether $array is a filled array or an object with rows
|
|
|
|
*/
|
2022-06-19 21:10:02 -04:00
|
|
|
public static function isResult($array): bool
|
2018-07-21 08:21:23 -04:00
|
|
|
{
|
2019-12-15 17:28:01 -05:00
|
|
|
return DI::dba()->isResult($array);
|
2018-07-21 08:21:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-18 15:33:20 -05:00
|
|
|
* Escapes a whole array
|
2018-07-21 08:21:23 -04:00
|
|
|
*
|
|
|
|
* @param mixed $arr Array with values to be escaped
|
|
|
|
* @param boolean $add_quotation add quotation marks for string values
|
|
|
|
* @return void
|
|
|
|
*/
|
2022-06-19 21:10:02 -04:00
|
|
|
public static function escapeArray(&$arr, bool $add_quotation = false)
|
2018-07-21 08:21:23 -04:00
|
|
|
{
|
2019-12-15 17:28:01 -05:00
|
|
|
DI::dba()->escapeArray($arr, $add_quotation);
|
2018-07-21 08:21:23 -04:00
|
|
|
}
|
2018-07-19 22:06:13 -04:00
|
|
|
}
|