X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FDatabase%2FDBA.php;h=0c0fd0f80d30839cf94261d08dfee5a0f9a63334;hb=3f2b0b9422915529a0ea585aa4325b6d2f2f65cd;hp=e519a63cd6f99e468b753192ac949aca6d577796;hpb=6743de63f5d0d276d6ee6bfa0d065b7f289f48ac;p=friendica.git diff --git a/src/Database/DBA.php b/src/Database/DBA.php index e519a63cd6..0c0fd0f80d 100644 --- a/src/Database/DBA.php +++ b/src/Database/DBA.php @@ -1,6 +1,6 @@ connect(); @@ -77,7 +82,7 @@ class DBA * * @return string with either "pdo" or "mysqli" */ - public static function getDriver() + public static function getDriver(): string { return DI::dba()->getDriver(); } @@ -205,7 +210,7 @@ class DBA * Please use DBA::delete, DBA::insert, DBA::update, ... instead * * @param string $sql SQL statement - * @return boolean Was the query successfull? False is returned only if an error occurred + * @return boolean Was the query successful? False is returned only if an error occurred * @throws \Exception */ public static function e(string $sql): bool @@ -218,12 +223,12 @@ class DBA /** * Check if data exists * - * @param string|array $table Table name or array [schema => table] - * @param array $condition array of fields for condition + * @param string $table Table name in format schema.table (where schema is optional) + * @param array $condition Array of fields for condition * @return boolean Are there rows for that condition? * @throws \Exception */ - public static function exists($table, array $condition): bool + public static function exists(string $table, array $condition): bool { return DI::dba()->exists($table, $condition); } @@ -289,14 +294,13 @@ class DBA /** * Insert a row into a table * - * @param string|array $table Table name or array [schema => table] - * @param array $param parameter array - * @param int $duplicate_mode What to do on a duplicated entry - * + * @param string $table Table name in format schema.table (where schema is optional) + * @param array $param parameter array + * @param int $duplicate_mode What to do on a duplicated entry * @return boolean was the insert successful? * @throws \Exception */ - public static function insert($table, array $param, int $duplicate_mode = Database::INSERT_DEFAULT): bool + public static function insert(string $table, array $param, int $duplicate_mode = Database::INSERT_DEFAULT): bool { return DI::dba()->insert($table, $param, $duplicate_mode); } @@ -305,13 +309,12 @@ class DBA * 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. * - * @param string|array $table Table name or array [schema => table] - * @param array $param parameter array - * + * @param string $table Table name in format schema.table (where schema is optional) + * @param array $param parameter array * @return boolean was the insert successful? * @throws \Exception */ - public static function replace($table, array $param): bool + public static function replace(string $table, array $param): bool { return DI::dba()->replace($table, $param); } @@ -331,12 +334,11 @@ class DBA * * This function can be extended in the future to accept a table array as well. * - * @param string|array $table Table name or array [schema => table] - * + * @param string $table Table name in format schema.table (where schema is optional) * @return boolean was the lock successful? * @throws \Exception */ - public static function lock($table): bool + public static function lock(string $table): bool { return DI::dba()->lock($table); } @@ -385,13 +387,13 @@ class DBA /** * Delete a row from a table * - * @param string|array $table Table name - * @param array $conditions Field condition(s) + * @param string $table Table name + * @param array $conditions Field condition(s) * * @return boolean was the delete successful? * @throws \Exception */ - public static function delete($table, array $conditions, array $options = []): bool + public static function delete(string $table, array $conditions, array $options = []): bool { return DI::dba()->delete($table, $conditions, $options); } @@ -417,16 +419,16 @@ class DBA * 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|array $table Table name or array [schema => table] + * @param string $table Table name in format schema.table (where schema is optional) * @param array $fields contains the fields that are updated * @param array $condition condition array with the key values * @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) * @param array $params Parameters: "ignore" If set to "true" then the update is done with the ignore parameter * - * @return boolean was the update successfull? + * @return boolean was the update successful? * @throws \Exception */ - public static function update($table, array $fields, array $condition, $old_fields = [], array $params = []): bool + public static function update(string $table, array $fields, array $condition, $old_fields = [], array $params = []): bool { return DI::dba()->update($table, $fields, $condition, $old_fields, $params); } @@ -434,7 +436,7 @@ class DBA /** * Retrieve a single record from a table and returns it in an associative array * - * @param string|array $table Table name or array [schema => table] + * @param string|array $table Table name in format schema.table (where schema is optional) * @param array $fields * @param array $condition * @param array $params @@ -450,16 +452,16 @@ class DBA /** * Select rows from a table and fills an array with the data * - * @param string|array $table Table name or array [schema => table] - * @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 + * @param string $table Table name in format schema.table (where schema is optional) + * @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 */ - public static function selectToArray($table, array $fields = [], array $condition = [], array $params = []) + public static function selectToArray(string $table, array $fields = [], array $condition = [], array $params = []) { return DI::dba()->selectToArray($table, $fields, $condition, $params); } @@ -467,10 +469,10 @@ class DBA /** * Select rows from a table * - * @param string|array $table Table name or array [schema => table] - * @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 + * @param string $table Table name in format schema.table (where schema is optional) + * @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 * @@ -487,7 +489,7 @@ class DBA * $data = DBA::select($table, $fields, $condition, $params); * @throws \Exception */ - public static function select($table, array $fields = [], array $condition = [], array $params = []) + public static function select(string $table, array $fields = [], array $condition = [], array $params = []) { return DI::dba()->select($table, $fields, $condition, $params); } @@ -495,9 +497,9 @@ class DBA /** * Counts the rows from a table satisfying the provided condition * - * @param string|array $table Table name or array [schema => table] - * @param array $condition array of fields for condition - * @param array $params Array of several parameters + * @param string $table Table name in format schema.table (where schema is optional) + * @param array $condition array of fields for condition + * @param array $params Array of several parameters * * @return int * @@ -511,7 +513,7 @@ class DBA * $count = DBA::count($table, $condition); * @throws \Exception */ - public static function count($table, array $condition = [], array $params = []): int + public static function count(string $table, array $condition = [], array $params = []): int { return DI::dba()->count($table, $condition, $params); } @@ -520,41 +522,34 @@ class DBA * Build the table query substring from one or more tables, with or without a schema. * * Expected formats: - * - table + * - [table] * - [table1, table2, ...] * - [schema1 => table1, schema2 => table2, table3, ...] * - * @param string|array $tables + * @param array $tables Table names * @return string */ - public static function buildTableString($tables): string + public static function buildTableString(array $tables): string { - if (is_string($tables)) { - $tables = [$tables]; - } - - $quotedTables = []; - - foreach ($tables as $schema => $table) { - if (is_numeric($schema)) { - $quotedTables[] = self::quoteIdentifier($table); - } else { - $quotedTables[] = self::quoteIdentifier($schema) . '.' . self::quoteIdentifier($table); - } - } - - return implode(', ', $quotedTables); + // Quote each entry + return implode(',', array_map([self::class, 'quoteIdentifier'], $tables)); } /** - * Escape an identifier (table or field name) + * Escape an identifier (table or field name) optional with a schema like ((schema.)table.)field * - * @param $identifier - * @return string + * @param string $identifier Table, field name + * @return string Quotes table or field name */ public static function quoteIdentifier(string $identifier): string { - return '`' . str_replace('`', '``', $identifier) . '`'; + return implode( + '.', + array_map( + function (string $identifier) { return '`' . str_replace('`', '``', $identifier) . '`'; }, + explode('.', $identifier) + ) + ); } /** @@ -578,7 +573,7 @@ class DBA public static function buildCondition(array &$condition = []): string { $condition = self::collapseCondition($condition); - + $condition_string = ''; if (count($condition) > 0) { $condition_string = " WHERE (" . array_shift($condition) . ")"; @@ -727,7 +722,7 @@ class DBA { $groupby_string = ''; if (!empty($params['group_by'])) { - $groupby_string = " GROUP BY " . implode(', ', array_map(['self', 'quoteIdentifier'], $params['group_by'])); + $groupby_string = " GROUP BY " . implode(', ', array_map([self::class, 'quoteIdentifier'], $params['group_by'])); } $order_string = ''; @@ -831,6 +826,27 @@ class DBA return DI::dba()->processlist(); } + /** + * Optimizes tables + * + * @param string $table a given table + * + * @return bool True, if successfully optimized, otherwise false + * @throws \Exception + */ + public static function optimizeTable(string $table): bool + { + return DI::dba()->optimizeTable($table); + } + + /** + * Kill sleeping database processes + */ + public static function deleteSleepingProcesses() + { + DI::dba()->deleteSleepingProcesses(); + } + /** * Fetch a database variable *