2019-02-28 13:38:25 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Friendica\Database;
|
|
|
|
|
2019-04-11 08:32:07 +02:00
|
|
|
use Friendica\Database\Driver\IDriver;
|
2019-04-10 23:49:41 +02:00
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
|
2019-02-28 13:38:25 +01:00
|
|
|
interface IDatabase
|
|
|
|
{
|
|
|
|
/**
|
2019-04-10 23:49:41 +02:00
|
|
|
* Sets the Logger of this database instance
|
|
|
|
* @param LoggerInterface $logger
|
2019-02-28 13:38:25 +01:00
|
|
|
*/
|
2019-04-10 23:49:41 +02:00
|
|
|
function setLogger(LoggerInterface $logger);
|
2019-02-28 13:38:25 +01:00
|
|
|
|
|
|
|
/**
|
2019-04-10 23:49:41 +02:00
|
|
|
* Returns the current connection of the database
|
2019-04-11 08:32:07 +02:00
|
|
|
*
|
|
|
|
* @return IDriver
|
2019-02-28 13:38:25 +01:00
|
|
|
*/
|
2019-04-11 08:32:07 +02:00
|
|
|
function getDriver();
|
2019-02-28 13:38:25 +01:00
|
|
|
|
|
|
|
/**
|
2019-04-10 23:49:41 +02:00
|
|
|
* Returns the selected database name
|
|
|
|
* @return string
|
2019-02-28 13:38:25 +01:00
|
|
|
*/
|
2019-04-10 23:49:41 +02:00
|
|
|
function getDatabaseName();
|
2019-02-28 13:38:25 +01:00
|
|
|
|
2019-04-12 08:38:03 +02:00
|
|
|
/**
|
|
|
|
* Returns the number of affected rows of the last statement
|
|
|
|
*
|
|
|
|
* @return int Number of rows
|
|
|
|
*/
|
|
|
|
function getAffectedRows();
|
|
|
|
|
2019-02-28 13:38:25 +01:00
|
|
|
/**
|
2019-04-10 23:49:41 +02:00
|
|
|
* Executes a prepared statement that returns data
|
|
|
|
*
|
|
|
|
* @usage Example: $r = p("SELECT * FROM `item` WHERE `guid` = ?", $guid);
|
|
|
|
*
|
|
|
|
* Please only use it with complicated queries.
|
|
|
|
* For all regular queries please use DBA::select or DBA::exists
|
|
|
|
*
|
2019-04-12 08:38:03 +02:00
|
|
|
* @param string $sql SQL statement
|
|
|
|
* @param array $params The parameters of the current SQL statement
|
2019-04-10 23:49:41 +02:00
|
|
|
*
|
|
|
|
* @return bool|object statement object or result object
|
2019-02-28 13:38:25 +01:00
|
|
|
*/
|
2019-04-12 08:38:03 +02:00
|
|
|
function prepared($sql, array $params = []);
|
2019-02-28 13:38:25 +01:00
|
|
|
|
|
|
|
/**
|
2019-04-10 23:49:41 +02:00
|
|
|
* Executes a prepared statement like UPDATE or INSERT that doesn't return data
|
|
|
|
*
|
|
|
|
* Please use DBA::delete, DBA::insert, DBA::update, ... instead
|
|
|
|
*
|
|
|
|
* @param string $sql SQL statement
|
2019-04-12 08:38:03 +02:00
|
|
|
* @param array $params The parameters of the current SQL statement
|
|
|
|
*
|
2019-04-10 23:49:41 +02:00
|
|
|
* @return boolean Was the query successfull? False is returned only if an error occurred
|
2019-02-28 13:38:25 +01:00
|
|
|
*/
|
2019-04-12 08:38:03 +02:00
|
|
|
function execute($sql, array $params = []);
|
2019-02-28 13:38:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if data exists
|
|
|
|
*
|
|
|
|
* @param string $table The table name
|
|
|
|
* @param array $condition An array of fields for condition
|
|
|
|
*
|
|
|
|
* @return bool Are there rows for that condition?
|
|
|
|
*/
|
|
|
|
function exists($table, array $condition);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Counts the rows from a table satisfying the provided condition
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* $table = 'item';
|
|
|
|
*
|
|
|
|
* $condition = ['uid' => 1, 'network' => 'dspr'];
|
|
|
|
* or:
|
|
|
|
* $condition = ['`uid` = ? AND `network` IN (?, ?)', 1, 'dfrn', 'dspr'];
|
|
|
|
*
|
|
|
|
* $count = IDatabase->count($table, $condition);
|
|
|
|
*
|
|
|
|
* @param string $table The table name
|
|
|
|
* @param array $condition An array of fields for condition
|
|
|
|
*
|
|
|
|
* @return int The counted rows
|
|
|
|
*/
|
|
|
|
function count($table, array $condition = []);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch a single row
|
|
|
|
* @param mixed $stmt A statement object
|
|
|
|
*
|
|
|
|
* @return array The current row
|
|
|
|
*/
|
|
|
|
function fetch($stmt);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts a transaction
|
|
|
|
*
|
|
|
|
* @return bool Was the command executed successfully?
|
|
|
|
*/
|
|
|
|
function transaction();
|
|
|
|
|
2019-04-10 23:49:41 +02:00
|
|
|
/**
|
|
|
|
* @brief Closes the current statement
|
|
|
|
*
|
|
|
|
* @param object $stmt statement object
|
|
|
|
*
|
|
|
|
* @return boolean was the close successful?
|
|
|
|
*/
|
|
|
|
function close($stmt);
|
|
|
|
|
2019-02-28 13:38:25 +01:00
|
|
|
/**
|
|
|
|
* Does a commit
|
|
|
|
*
|
|
|
|
* @return bool Was the command executed successfully?
|
|
|
|
*/
|
|
|
|
function commit();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Does a rollback
|
|
|
|
*
|
|
|
|
* @return bool Was the command executed successfully?
|
|
|
|
*/
|
|
|
|
function rollback();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert a row into a table
|
|
|
|
*
|
|
|
|
* @param string $table The table name
|
|
|
|
* @param array $param An array of fields for inserting
|
|
|
|
* @param bool $on_duplicate_update Do an update on a duplicate entry
|
|
|
|
*
|
|
|
|
* @return bool Was the insert successful?
|
|
|
|
*/
|
|
|
|
function insert($table, array $param, $on_duplicate_update = false);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a row from a table
|
|
|
|
*
|
|
|
|
* @param string $table The table name
|
|
|
|
* @param array $conditions An array of fields for condition
|
|
|
|
* @param bool $cascade If true we delete records in other tables that depend on the one we're deleting through
|
|
|
|
* relations (default: true)
|
|
|
|
*
|
|
|
|
* @return bool Was the delete successful?
|
|
|
|
*/
|
2019-04-10 23:49:41 +02:00
|
|
|
function delete($table, array $conditions, $cascade = true);
|
2019-02-28 13:38:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 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 exist.
|
|
|
|
*
|
|
|
|
* 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!
|
|
|
|
*
|
|
|
|
* @param string $table The table name
|
|
|
|
* @param array $fields An array of fields for updating
|
|
|
|
* @param array $condition An array of fields for condition
|
|
|
|
* @param array|bool $old_fields An array of old fields that are about to be replaced (true = update on duplicate)
|
|
|
|
*
|
|
|
|
* @return bool Was the update successful?
|
|
|
|
*/
|
|
|
|
function update($table, array $fields, array $condition, $old_fields = []);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Select rows from a table.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* $table = "item";
|
|
|
|
* $fields = ['id', 'uri', 'uid', 'network'];
|
|
|
|
*
|
|
|
|
* $condition = ['uid' => 1, 'network' => 'dspr'];
|
|
|
|
* or:
|
|
|
|
* $condition = ['`uid` = ? AND `network` IN (?, ?)', 1, 'dfrn', 'dspr');
|
|
|
|
*
|
|
|
|
* $params = ['order' => ['id', 'received' => true), 'limit' => 10];
|
|
|
|
*
|
|
|
|
* $data = IDatabase->select($table, $fields, $condition, $params);
|
|
|
|
*
|
|
|
|
* @param string $table The table name
|
|
|
|
* @param array $fields An array of fields for selecting, empty for all
|
|
|
|
* @param array $condition An array of fields for condition
|
|
|
|
* @param array $params An array of fields of several parameters
|
|
|
|
*
|
|
|
|
* @return bool|object The result object or "false" if nothing was found.
|
|
|
|
*/
|
|
|
|
function select($table, array $fields = [], array $condition = [], array $params = []);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve a single record from a table and returns it in an associative array
|
|
|
|
*
|
|
|
|
* @param string $table The table name
|
|
|
|
* @param array $fields An array of fields for selecting, empty for all
|
|
|
|
* @param array $condition An array of fields for condition
|
|
|
|
* @param array $params An array of fields of several parameters
|
|
|
|
*
|
|
|
|
* @return bool|array The result array or "false" if nothing was found.
|
|
|
|
*/
|
|
|
|
function selectFirst($table, array $fields = [], array $condition = [], array $params = []);
|
|
|
|
}
|