2018-07-20 02:06:13 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Friendica\Database;
|
|
|
|
|
2019-07-21 18:24:16 +00:00
|
|
|
use Friendica\BaseObject;
|
2018-07-22 16:53:46 +00:00
|
|
|
use mysqli;
|
2018-07-21 12:21:23 +00:00
|
|
|
use mysqli_result;
|
|
|
|
use mysqli_stmt;
|
2018-07-20 02:24:03 +00:00
|
|
|
use PDO;
|
|
|
|
use PDOStatement;
|
2018-07-20 02:06:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @class MySQL database class
|
|
|
|
*
|
|
|
|
* This class is for the low level database stuff that does driver specific things.
|
|
|
|
*/
|
2019-07-21 18:24:16 +00:00
|
|
|
class DBA extends BaseObject
|
2018-07-20 02:06:13 +00:00
|
|
|
{
|
2018-10-21 11:53:16 +00:00
|
|
|
/**
|
|
|
|
* Lowest possible date value
|
|
|
|
*/
|
2018-10-21 05:53:47 +00:00
|
|
|
const NULL_DATE = '0001-01-01';
|
2018-10-21 11:53:16 +00:00
|
|
|
/**
|
|
|
|
* Lowest possible datetime value
|
|
|
|
*/
|
2018-10-21 05:53:47 +00:00
|
|
|
const NULL_DATETIME = '0001-01-01 00:00:00';
|
|
|
|
|
2019-06-06 22:10:45 +00:00
|
|
|
public static function connect()
|
2019-04-13 18:46:58 +00:00
|
|
|
{
|
2019-07-21 18:24:16 +00:00
|
|
|
return self::getClass(Database::class)->connect();
|
2019-04-13 18:46:58 +00:00
|
|
|
}
|
|
|
|
|
2018-07-20 02:06:13 +00:00
|
|
|
/**
|
|
|
|
* Disconnects the current database connection
|
|
|
|
*/
|
|
|
|
public static function disconnect()
|
|
|
|
{
|
2019-07-21 18:24:16 +00:00
|
|
|
self::getClass(Database::class)->disconnect();
|
2018-07-20 02:06:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform a reconnect of an existing database connection
|
|
|
|
*/
|
2019-06-06 22:10:45 +00:00
|
|
|
public static function reconnect()
|
|
|
|
{
|
2019-07-21 18:24:16 +00:00
|
|
|
return self::getClass(Database::class)->reconnect();
|
2018-07-20 02:06:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the database object.
|
|
|
|
* @return PDO|mysqli
|
|
|
|
*/
|
2018-07-21 02:11:11 +00:00
|
|
|
public static function getConnection()
|
2018-07-20 02:06:13 +00:00
|
|
|
{
|
2019-07-21 18:24:16 +00:00
|
|
|
return self::getClass(Database::class)->getConnection();
|
2018-07-20 02:06:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns the MySQL server version string
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
2019-06-06 22:10:45 +00:00
|
|
|
public static function serverInfo()
|
|
|
|
{
|
2019-07-21 18:24:16 +00:00
|
|
|
return self::getClass(Database::class)->serverInfo();
|
2018-07-20 02:06:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns the selected database name
|
|
|
|
*
|
|
|
|
* @return string
|
2019-01-06 21:06:53 +00:00
|
|
|
* @throws \Exception
|
2018-07-20 02:06:13 +00:00
|
|
|
*/
|
2019-06-06 22:10:45 +00:00
|
|
|
public static function databaseName()
|
2019-05-21 05:34:41 +00:00
|
|
|
{
|
2019-07-21 18:24:16 +00:00
|
|
|
return self::getClass(Database::class)->databaseName();
|
2019-05-21 05:34:41 +00:00
|
|
|
}
|
|
|
|
|
2019-06-06 22:10:45 +00:00
|
|
|
public static function escape($str)
|
|
|
|
{
|
2019-07-21 18:24:16 +00:00
|
|
|
return self::getClass(Database::class)->escape($str);
|
2018-07-20 02:06:13 +00:00
|
|
|
}
|
|
|
|
|
2019-06-06 22:10:45 +00:00
|
|
|
public static function connected()
|
|
|
|
{
|
2019-07-21 18:24:16 +00:00
|
|
|
return self::getClass(Database::class)->connected();
|
2018-07-20 02:06:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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.
|
|
|
|
*/
|
2019-06-06 22:10:45 +00:00
|
|
|
public static function anyValueFallback($sql)
|
|
|
|
{
|
2019-07-21 18:24:16 +00:00
|
|
|
return self::getClass(Database::class)->anyValueFallback($sql);
|
2018-07-20 02:06:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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.
|
|
|
|
*/
|
2019-06-06 22:10:45 +00:00
|
|
|
public static function cleanQuery($sql)
|
|
|
|
{
|
2018-07-20 02:06:13 +00:00
|
|
|
$search = ["\t", "\n", "\r", " "];
|
|
|
|
$replace = [' ', ' ', ' ', ' '];
|
|
|
|
do {
|
|
|
|
$oldsql = $sql;
|
|
|
|
$sql = str_replace($search, $replace, $sql);
|
|
|
|
} while ($oldsql != $sql);
|
|
|
|
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Convert parameter array to an universal form
|
|
|
|
* @param array $args Parameter array
|
|
|
|
* @return array universalized parameter array
|
|
|
|
*/
|
2019-06-06 22:10:45 +00:00
|
|
|
public static function getParam($args)
|
|
|
|
{
|
2018-07-20 02:06:13 +00: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Executes a prepared statement that returns data
|
|
|
|
* @usage Example: $r = p("SELECT * FROM `item` WHERE `guid` = ?", $guid);
|
|
|
|
*
|
|
|
|
* Please only use it with complicated queries.
|
2018-08-23 13:51:58 +00:00
|
|
|
* For all regular queries please use DBA::select or DBA::exists
|
2018-07-20 02:06:13 +00:00
|
|
|
*
|
|
|
|
* @param string $sql SQL statement
|
|
|
|
* @return bool|object statement object or result object
|
2019-01-06 21:06:53 +00:00
|
|
|
* @throws \Exception
|
2018-07-20 02:06:13 +00:00
|
|
|
*/
|
2019-06-06 22:10:45 +00:00
|
|
|
public static function p($sql)
|
|
|
|
{
|
2018-07-20 02:06:13 +00:00
|
|
|
$params = self::getParam(func_get_args());
|
|
|
|
|
2019-07-21 18:24:16 +00:00
|
|
|
return self::getClass(Database::class)->p($sql, $params);
|
2018-07-20 02:06:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Executes a prepared statement like UPDATE or INSERT that doesn't return data
|
|
|
|
*
|
2018-08-23 13:51:58 +00:00
|
|
|
* Please use DBA::delete, DBA::insert, DBA::update, ... instead
|
2018-07-20 02:06:13 +00:00
|
|
|
*
|
|
|
|
* @param string $sql SQL statement
|
|
|
|
* @return boolean Was the query successfull? False is returned only if an error occurred
|
2019-01-06 21:06:53 +00:00
|
|
|
* @throws \Exception
|
2018-07-20 02:06:13 +00:00
|
|
|
*/
|
|
|
|
public static function e($sql) {
|
|
|
|
|
|
|
|
$params = self::getParam(func_get_args());
|
|
|
|
|
2019-07-21 18:24:16 +00:00
|
|
|
return self::getClass(Database::class)->e($sql, $params);
|
2018-07-20 02:06:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Check if data exists
|
|
|
|
*
|
2019-01-06 21:06:53 +00:00
|
|
|
* @param string $table Table name
|
|
|
|
* @param array $condition array of fields for condition
|
2018-07-20 02:06:13 +00:00
|
|
|
*
|
|
|
|
* @return boolean Are there rows for that condition?
|
2019-01-06 21:06:53 +00:00
|
|
|
* @throws \Exception
|
2018-07-20 02:06:13 +00:00
|
|
|
*/
|
2019-06-06 22:10:45 +00:00
|
|
|
public static function exists($table, $condition)
|
|
|
|
{
|
2019-07-21 18:24:16 +00:00
|
|
|
return self::getClass(Database::class)->exists($table, $condition);
|
2018-07-20 02:06:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches the first row
|
|
|
|
*
|
2018-08-23 13:51:58 +00:00
|
|
|
* Please use DBA::selectFirst or DBA::exists whenever this is possible.
|
2018-07-20 02:06:13 +00:00
|
|
|
*
|
|
|
|
* @brief Fetches the first row
|
|
|
|
* @param string $sql SQL statement
|
|
|
|
* @return array first row of query
|
2019-01-06 21:06:53 +00:00
|
|
|
* @throws \Exception
|
2018-07-20 02:06:13 +00:00
|
|
|
*/
|
2019-06-06 22:10:45 +00:00
|
|
|
public static function fetchFirst($sql)
|
|
|
|
{
|
2018-07-20 02:06:13 +00:00
|
|
|
$params = self::getParam(func_get_args());
|
|
|
|
|
2019-07-21 18:24:16 +00:00
|
|
|
return self::getClass(Database::class)->fetchFirst($sql, $params);
|
2018-07-20 02:06:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns the number of affected rows of the last statement
|
|
|
|
*
|
|
|
|
* @return int Number of rows
|
|
|
|
*/
|
2019-06-06 22:10:45 +00:00
|
|
|
public static function affectedRows()
|
|
|
|
{
|
2019-07-21 18:24:16 +00:00
|
|
|
return self::getClass(Database::class)->affectedRows();
|
2018-07-20 02:06:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns the number of columns of a statement
|
|
|
|
*
|
|
|
|
* @param object Statement object
|
|
|
|
* @return int Number of columns
|
|
|
|
*/
|
2019-06-06 22:10:45 +00:00
|
|
|
public static function columnCount($stmt)
|
|
|
|
{
|
2019-07-21 18:24:16 +00:00
|
|
|
return self::getClass(Database::class)->columnCount($stmt);
|
2018-07-20 02:06:13 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @brief Returns the number of rows of a statement
|
|
|
|
*
|
|
|
|
* @param PDOStatement|mysqli_result|mysqli_stmt Statement object
|
|
|
|
* @return int Number of rows
|
|
|
|
*/
|
2019-06-06 22:10:45 +00:00
|
|
|
public static function numRows($stmt)
|
|
|
|
{
|
2019-07-21 18:24:16 +00:00
|
|
|
return self::getClass(Database::class)->numRows($stmt);
|
2018-07-20 02:06:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Fetch a single row
|
|
|
|
*
|
|
|
|
* @param mixed $stmt statement object
|
|
|
|
* @return array current row
|
|
|
|
*/
|
2019-06-06 22:10:45 +00:00
|
|
|
public static function fetch($stmt)
|
2019-05-20 20:38:18 +00:00
|
|
|
{
|
2019-07-21 18:24:16 +00:00
|
|
|
return self::getClass(Database::class)->fetch($stmt);
|
2019-05-20 20:38:18 +00:00
|
|
|
}
|
|
|
|
|
2018-07-20 02:06:13 +00:00
|
|
|
/**
|
|
|
|
* @brief Insert a row into a table
|
|
|
|
*
|
2019-01-06 21:06:53 +00:00
|
|
|
* @param string $table Table name
|
|
|
|
* @param array $param parameter array
|
|
|
|
* @param bool $on_duplicate_update Do an update on a duplicate entry
|
2018-07-20 02:06:13 +00:00
|
|
|
*
|
2018-10-14 15:32:54 +00:00
|
|
|
* @return boolean was the insert successful?
|
2019-01-06 21:06:53 +00:00
|
|
|
* @throws \Exception
|
2018-07-20 02:06:13 +00:00
|
|
|
*/
|
2019-06-06 22:10:45 +00:00
|
|
|
public static function insert($table, $param, $on_duplicate_update = false)
|
|
|
|
{
|
2019-07-21 18:24:16 +00:00
|
|
|
return self::getClass(Database::class)->insert($table, $param, $on_duplicate_update);
|
2018-07-20 02:06:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Fetch the id of the last insert command
|
|
|
|
*
|
|
|
|
* @return integer Last inserted id
|
|
|
|
*/
|
2019-06-06 22:10:45 +00:00
|
|
|
public static function lastInsertId()
|
|
|
|
{
|
2019-07-21 18:24:16 +00:00
|
|
|
return self::getClass(Database::class)->lastInsertId();
|
2018-07-20 02:06:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Locks a table for exclusive write access
|
|
|
|
*
|
|
|
|
* This function can be extended in the future to accept a table array as well.
|
|
|
|
*
|
|
|
|
* @param string $table Table name
|
|
|
|
*
|
|
|
|
* @return boolean was the lock successful?
|
2019-01-06 21:06:53 +00:00
|
|
|
* @throws \Exception
|
2018-07-20 02:06:13 +00:00
|
|
|
*/
|
2019-06-06 22:10:45 +00:00
|
|
|
public static function lock($table)
|
|
|
|
{
|
2019-07-21 18:24:16 +00:00
|
|
|
return self::getClass(Database::class)->lock($table);
|
2018-07-20 02:06:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Unlocks all locked tables
|
|
|
|
*
|
|
|
|
* @return boolean was the unlock successful?
|
2019-01-06 21:06:53 +00:00
|
|
|
* @throws \Exception
|
2018-07-20 02:06:13 +00:00
|
|
|
*/
|
2019-06-06 22:10:45 +00:00
|
|
|
public static function unlock()
|
|
|
|
{
|
2019-07-21 18:24:16 +00:00
|
|
|
return self::getClass(Database::class)->unlock();
|
2018-07-20 02:06:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Starts a transaction
|
|
|
|
*
|
|
|
|
* @return boolean Was the command executed successfully?
|
|
|
|
*/
|
2019-06-06 22:10:45 +00:00
|
|
|
public static function transaction()
|
2018-07-20 02:06:13 +00:00
|
|
|
{
|
2019-07-21 18:24:16 +00:00
|
|
|
return self::getClass(Database::class)->transaction();
|
2018-07-20 02:06:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Does a commit
|
|
|
|
*
|
|
|
|
* @return boolean Was the command executed successfully?
|
|
|
|
*/
|
2019-06-06 22:10:45 +00:00
|
|
|
public static function commit()
|
|
|
|
{
|
2019-07-21 18:24:16 +00:00
|
|
|
return self::getClass(Database::class)->commit();
|
2018-07-20 02:06:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Does a rollback
|
|
|
|
*
|
|
|
|
* @return boolean Was the command executed successfully?
|
|
|
|
*/
|
2019-06-06 22:10:45 +00:00
|
|
|
public static function rollback()
|
|
|
|
{
|
2019-07-21 18:24:16 +00:00
|
|
|
return self::getClass(Database::class)->rollback();
|
2018-07-20 02:06:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Delete a row from a table
|
|
|
|
*
|
2019-01-06 21:06:53 +00:00
|
|
|
* @param string $table Table name
|
|
|
|
* @param array $conditions Field condition(s)
|
|
|
|
* @param array $options
|
|
|
|
* - cascade: If true we delete records in other tables that depend on the one we're deleting through
|
2018-07-20 02:06:13 +00:00
|
|
|
* relations (default: true)
|
|
|
|
*
|
2018-12-02 16:25:25 +00:00
|
|
|
* @return boolean was the delete successful?
|
2019-01-06 21:06:53 +00:00
|
|
|
* @throws \Exception
|
2018-07-20 02:06:13 +00:00
|
|
|
*/
|
2019-06-06 22:10:45 +00:00
|
|
|
public static function delete($table, array $conditions, array $options = [])
|
2018-07-20 02:06:13 +00:00
|
|
|
{
|
2019-07-21 18:24:16 +00:00
|
|
|
return self::getClass(Database::class)->delete($table, $conditions, $options);
|
2018-07-20 02:06:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Updates rows
|
|
|
|
*
|
|
|
|
* Updates rows in the database. When $old_fields is set to an array,
|
|
|
|
* 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!
|
|
|
|
*
|
2019-01-06 21:06:53 +00:00
|
|
|
* @param string $table Table name
|
|
|
|
* @param array $fields contains the fields that are updated
|
|
|
|
* @param array $condition condition array with the key values
|
2018-07-20 02:06:13 +00:00
|
|
|
* @param array|boolean $old_fields array with the old field values that are about to be replaced (true = update on duplicate)
|
|
|
|
*
|
|
|
|
* @return boolean was the update successfull?
|
2019-01-06 21:06:53 +00:00
|
|
|
* @throws \Exception
|
2018-07-20 02:06:13 +00:00
|
|
|
*/
|
2019-06-06 22:10:45 +00:00
|
|
|
public static function update($table, $fields, $condition, $old_fields = [])
|
|
|
|
{
|
2019-07-21 18:24:16 +00:00
|
|
|
return self::getClass(Database::class)->update($table, $fields, $condition, $old_fields);
|
2018-07-20 02:06:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve a single record from a table and returns it in an associative array
|
|
|
|
*
|
|
|
|
* @brief Retrieve a single record from a table
|
|
|
|
* @param string $table
|
|
|
|
* @param array $fields
|
|
|
|
* @param array $condition
|
|
|
|
* @param array $params
|
|
|
|
* @return bool|array
|
2019-01-06 21:06:53 +00:00
|
|
|
* @throws \Exception
|
|
|
|
* @see self::select
|
2018-07-20 02:06:13 +00:00
|
|
|
*/
|
|
|
|
public static function selectFirst($table, array $fields = [], array $condition = [], $params = [])
|
|
|
|
{
|
2019-07-21 18:24:16 +00:00
|
|
|
return self::getClass(Database::class)->selectFirst($table, $fields, $condition, $params);
|
2018-07-20 02:06:13 +00:00
|
|
|
}
|
|
|
|
|
2019-07-27 14:33:17 +00:00
|
|
|
/**
|
|
|
|
* @brief Select rows from a table and fills an array with the data
|
|
|
|
*
|
|
|
|
* @param string $table Table name
|
|
|
|
* @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
|
|
|
|
*
|
|
|
|
* @return array Data array
|
|
|
|
* @throws \Exception
|
|
|
|
* @see self::select
|
|
|
|
*/
|
2019-07-27 17:00:06 +00:00
|
|
|
public static function selectToArray(string $table, array $fields = [], array $condition = [], array $params = [])
|
2019-07-27 14:33:17 +00:00
|
|
|
{
|
2019-07-27 16:08:48 +00:00
|
|
|
return self::$database->selectToArray($table, $fields, $condition, $params);
|
2019-07-27 14:33:17 +00:00
|
|
|
}
|
|
|
|
|
2018-07-20 02:06:13 +00:00
|
|
|
/**
|
|
|
|
* @brief Select rows from a table
|
|
|
|
*
|
|
|
|
* @param string $table Table name
|
|
|
|
* @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
|
|
|
|
*
|
|
|
|
* @return boolean|object
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* $table = "item";
|
|
|
|
* $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 13:51:58 +00:00
|
|
|
* $data = DBA::select($table, $fields, $condition, $params);
|
2019-01-06 21:06:53 +00:00
|
|
|
* @throws \Exception
|
2018-07-20 02:06:13 +00:00
|
|
|
*/
|
|
|
|
public static function select($table, array $fields = [], array $condition = [], array $params = [])
|
|
|
|
{
|
2019-07-21 18:24:16 +00:00
|
|
|
return self::getClass(Database::class)->select($table, $fields, $condition, $params);
|
2018-07-20 02:06:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Counts the rows from a table satisfying the provided condition
|
|
|
|
*
|
2019-01-06 21:06:53 +00:00
|
|
|
* @param string $table Table name
|
|
|
|
* @param array $condition array of fields for condition
|
2018-07-20 02:06:13 +00:00
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* $table = "item";
|
|
|
|
*
|
|
|
|
* $condition = ["uid" => 1, "network" => 'dspr'];
|
|
|
|
* or:
|
|
|
|
* $condition = ["`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr'];
|
|
|
|
*
|
2018-08-23 13:51:58 +00:00
|
|
|
* $count = DBA::count($table, $condition);
|
2019-01-06 21:06:53 +00:00
|
|
|
* @throws \Exception
|
2018-07-20 02:06:13 +00:00
|
|
|
*/
|
|
|
|
public static function count($table, array $condition = [])
|
|
|
|
{
|
2019-07-21 18:24:16 +00:00
|
|
|
return self::getClass(Database::class)->count($table, $condition);
|
2018-07-20 02:06:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns the SQL condition string built from the provided condition array
|
|
|
|
*
|
|
|
|
* This function operates with two modes.
|
|
|
|
* - Supplied with a filed/value associative array, it builds simple strict
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
public static function buildCondition(array &$condition = [])
|
|
|
|
{
|
|
|
|
$condition_string = '';
|
|
|
|
if (count($condition) > 0) {
|
|
|
|
reset($condition);
|
|
|
|
$first_key = key($condition);
|
|
|
|
if (is_int($first_key)) {
|
|
|
|
$condition_string = " WHERE (" . array_shift($condition) . ")";
|
|
|
|
} else {
|
|
|
|
$new_values = [];
|
|
|
|
$condition_string = "";
|
|
|
|
foreach ($condition as $field => $value) {
|
|
|
|
if ($condition_string != "") {
|
|
|
|
$condition_string .= " AND ";
|
|
|
|
}
|
|
|
|
if (is_array($value)) {
|
|
|
|
/* Workaround for MySQL Bug #64791.
|
|
|
|
* Never mix data types inside any IN() condition.
|
|
|
|
* In case of mixed types, cast all as string.
|
2018-08-23 13:51:58 +00:00
|
|
|
* Logic needs to be consistent with DBA::p() data types.
|
2018-07-20 02:06:13 +00:00
|
|
|
*/
|
|
|
|
$is_int = false;
|
|
|
|
$is_alpha = false;
|
|
|
|
foreach ($value as $single_value) {
|
|
|
|
if (is_int($single_value)) {
|
|
|
|
$is_int = true;
|
|
|
|
} else {
|
|
|
|
$is_alpha = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($is_int && $is_alpha) {
|
|
|
|
foreach ($value as &$ref) {
|
|
|
|
if (is_int($ref)) {
|
|
|
|
$ref = (string)$ref;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unset($ref); //Prevent accidental re-use.
|
|
|
|
}
|
|
|
|
|
|
|
|
$new_values = array_merge($new_values, array_values($value));
|
|
|
|
$placeholders = substr(str_repeat("?, ", count($value)), 0, -2);
|
|
|
|
$condition_string .= "`" . $field . "` IN (" . $placeholders . ")";
|
2019-05-13 05:34:40 +00:00
|
|
|
} elseif (is_null($value)) {
|
|
|
|
$condition_string .= "`" . $field . "` IS NULL";
|
2018-07-20 02:06:13 +00:00
|
|
|
} else {
|
|
|
|
$new_values[$field] = $value;
|
|
|
|
$condition_string .= "`" . $field . "` = ?";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$condition_string = " WHERE (" . $condition_string . ")";
|
|
|
|
$condition = $new_values;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $condition_string;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns the SQL parameter string built from the provided parameter array
|
|
|
|
*
|
|
|
|
* @param array $params
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function buildParameter(array $params = [])
|
|
|
|
{
|
2019-05-19 00:50:14 +00:00
|
|
|
$groupby_string = '';
|
|
|
|
if (isset($params['group_by'])) {
|
|
|
|
$groupby_string = " GROUP BY ";
|
|
|
|
foreach ($params['group_by'] as $fields) {
|
|
|
|
$groupby_string .= "`" . $fields . "`, ";
|
|
|
|
}
|
|
|
|
$groupby_string = substr($groupby_string, 0, -2);
|
|
|
|
}
|
|
|
|
|
2018-07-20 02:06:13 +00:00
|
|
|
$order_string = '';
|
|
|
|
if (isset($params['order'])) {
|
|
|
|
$order_string = " ORDER BY ";
|
|
|
|
foreach ($params['order'] AS $fields => $order) {
|
2019-05-18 20:17:57 +00:00
|
|
|
if ($order === 'RAND()') {
|
|
|
|
$order_string .= "RAND(), ";
|
|
|
|
} elseif (!is_int($fields)) {
|
2018-07-20 02:06:13 +00:00
|
|
|
$order_string .= "`" . $fields . "` " . ($order ? "DESC" : "ASC") . ", ";
|
|
|
|
} else {
|
|
|
|
$order_string .= "`" . $order . "`, ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$order_string = substr($order_string, 0, -2);
|
|
|
|
}
|
|
|
|
|
|
|
|
$limit_string = '';
|
2019-03-20 04:33:26 +00:00
|
|
|
if (isset($params['limit']) && is_numeric($params['limit'])) {
|
2018-08-20 04:26:05 +00:00
|
|
|
$limit_string = " LIMIT " . intval($params['limit']);
|
2018-07-20 02:06:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($params['limit']) && is_array($params['limit'])) {
|
|
|
|
$limit_string = " LIMIT " . intval($params['limit'][0]) . ", " . intval($params['limit'][1]);
|
|
|
|
}
|
|
|
|
|
2019-05-19 03:13:06 +00:00
|
|
|
return $groupby_string . $order_string . $limit_string;
|
2018-07-20 02:06:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Fills an array with data from a query
|
|
|
|
*
|
|
|
|
* @param object $stmt statement object
|
2019-01-06 21:06:53 +00:00
|
|
|
* @param bool $do_close
|
2018-07-20 02:06:13 +00:00
|
|
|
* @return array Data array
|
|
|
|
*/
|
2019-06-06 22:10:45 +00:00
|
|
|
public static function toArray($stmt, $do_close = true)
|
|
|
|
{
|
2019-07-21 18:24:16 +00:00
|
|
|
return self::getClass(Database::class)->toArray($stmt, $do_close);
|
2018-07-20 02:06:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns the error number of the last query
|
|
|
|
*
|
|
|
|
* @return string Error number (0 if no error)
|
|
|
|
*/
|
2019-06-06 22:10:45 +00:00
|
|
|
public static function errorNo()
|
|
|
|
{
|
2019-07-21 18:24:16 +00:00
|
|
|
return self::getClass(Database::class)->errorNo();
|
2018-07-20 02:06:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns the error message of the last query
|
|
|
|
*
|
|
|
|
* @return string Error message ('' if no error)
|
|
|
|
*/
|
2019-06-06 22:10:45 +00:00
|
|
|
public static function errorMessage()
|
|
|
|
{
|
2019-07-21 18:24:16 +00:00
|
|
|
return self::getClass(Database::class)->errorMessage();
|
2018-07-20 02:06:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Closes the current statement
|
|
|
|
*
|
|
|
|
* @param object $stmt statement object
|
|
|
|
* @return boolean was the close successful?
|
|
|
|
*/
|
2019-06-06 22:10:45 +00:00
|
|
|
public static function close($stmt)
|
|
|
|
{
|
2019-07-21 18:24:16 +00:00
|
|
|
return self::getClass(Database::class)->close($stmt);
|
2018-07-20 02:06:13 +00:00
|
|
|
}
|
2018-07-21 12:21:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Return a list of database processes
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
* 'list' => List of processes, separated in their different states
|
|
|
|
* 'amount' => Number of concurrent database processes
|
2019-01-06 21:06:53 +00:00
|
|
|
* @throws \Exception
|
2018-07-21 12:21:23 +00:00
|
|
|
*/
|
|
|
|
public static function processlist()
|
|
|
|
{
|
2019-07-21 18:24:16 +00:00
|
|
|
return self::getClass(Database::class)->processlist();
|
2018-07-21 12:21:23 +00: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
|
|
|
|
*/
|
2018-07-21 12:46:04 +00:00
|
|
|
public static function isResult($array)
|
2018-07-21 12:21:23 +00:00
|
|
|
{
|
2019-07-21 18:24:16 +00:00
|
|
|
return self::getClass(Database::class)->isResult($array);
|
2018-07-21 12:21:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Escapes a whole array
|
|
|
|
*
|
|
|
|
* @param mixed $arr Array with values to be escaped
|
|
|
|
* @param boolean $add_quotation add quotation marks for string values
|
|
|
|
* @return void
|
|
|
|
*/
|
2018-07-21 12:48:29 +00:00
|
|
|
public static function escapeArray(&$arr, $add_quotation = false)
|
2018-07-21 12:21:23 +00:00
|
|
|
{
|
2019-07-21 18:24:16 +00:00
|
|
|
return self::getClass(Database::class)->escapeArray($arr, $add_quotation);
|
2018-07-21 12:21:23 +00:00
|
|
|
}
|
2018-07-20 02:06:13 +00:00
|
|
|
}
|