mirror of
https://github.com/friendica/friendica
synced 2025-05-06 15:44:11 +02:00
Split DBStructure & View to avoid DB-calls and dependencies for basic operations
- new "Definition" classes vor DB and Views - new "Writer" classes to create SQL definitions for DB and Views - DBStructure & View are responsible to execute DB-querys
This commit is contained in:
parent
a2c929d128
commit
a910fd8864
30 changed files with 898 additions and 608 deletions
301
src/Util/Writer/DbaDefinitionSqlWriter.php
Normal file
301
src/Util/Writer/DbaDefinitionSqlWriter.php
Normal file
|
@ -0,0 +1,301 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2022, the Friendica project
|
||||
*
|
||||
* @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/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Util\Writer;
|
||||
|
||||
use Exception;
|
||||
use Friendica\Database\Definition\DbaDefinition;
|
||||
|
||||
/**
|
||||
* SQL writer utility for the database definition
|
||||
*/
|
||||
class DbaDefinitionSqlWriter
|
||||
{
|
||||
/**
|
||||
* Creates a complete SQL definition bases on a give DBA Definition class
|
||||
*
|
||||
* @param DbaDefinition $definition The DBA definition class
|
||||
*
|
||||
* @return string The SQL definition as a string
|
||||
*
|
||||
* @throws Exception in case of parameter failures
|
||||
*/
|
||||
public static function create(DbaDefinition $definition): string
|
||||
{
|
||||
$sqlString = "-- ------------------------------------------\n";
|
||||
$sqlString .= "-- " . FRIENDICA_PLATFORM . " " . FRIENDICA_VERSION . " (" . FRIENDICA_CODENAME . ")\n";
|
||||
$sqlString .= "-- DB_UPDATE_VERSION " . DB_UPDATE_VERSION . "\n";
|
||||
$sqlString .= "-- ------------------------------------------\n\n\n";
|
||||
|
||||
foreach ($definition->getAll() as $tableName => $tableStructure) {
|
||||
$sqlString .= "--\n";
|
||||
$sqlString .= "-- TABLE $tableName\n";
|
||||
$sqlString .= "--\n";
|
||||
$sqlString .= static::createTable($tableName, $tableStructure);
|
||||
}
|
||||
|
||||
return $sqlString;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the SQL definition of one table
|
||||
*
|
||||
* @param string $tableName The table name
|
||||
* @param array $tableStructure The table structure
|
||||
*
|
||||
* @return string The SQL definition
|
||||
*
|
||||
* @throws Exception in cases of structure failures
|
||||
*/
|
||||
public static function createTable(string $tableName, array $tableStructure): string
|
||||
{
|
||||
$engine = '';
|
||||
$comment = '';
|
||||
$sql_rows = [];
|
||||
$primary_keys = [];
|
||||
$foreign_keys = [];
|
||||
|
||||
foreach ($tableStructure['fields'] as $fieldName => $field) {
|
||||
$sql_rows[] = '`' . static::escape($fieldName) . '` ' . self::FieldCommand($field);
|
||||
if (!empty($field['primary'])) {
|
||||
$primary_keys[] = $fieldName;
|
||||
}
|
||||
if (!empty($field['foreign'])) {
|
||||
$foreign_keys[$fieldName] = $field;
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($tableStructure['indexes'])) {
|
||||
foreach ($tableStructure['indexes'] as $indexName => $fieldNames) {
|
||||
$sql_index = self::createIndex($indexName, $fieldNames, '');
|
||||
if (!is_null($sql_index)) {
|
||||
$sql_rows[] = $sql_index;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($foreign_keys as $fieldName => $parameters) {
|
||||
$sql_rows[] = self::foreignCommand($fieldName, $parameters);
|
||||
}
|
||||
|
||||
if (isset($tableStructure['engine'])) {
|
||||
$engine = ' ENGINE=' . $tableStructure['engine'];
|
||||
}
|
||||
|
||||
if (isset($tableStructure['comment'])) {
|
||||
$comment = " COMMENT='" . static::escape($tableStructure['comment']) . "'";
|
||||
}
|
||||
|
||||
$sql = implode(",\n\t", $sql_rows);
|
||||
|
||||
$sql = sprintf("CREATE TABLE IF NOT EXISTS `%s` (\n\t", static::escape($tableName)) . $sql .
|
||||
"\n)" . $engine . " DEFAULT COLLATE utf8mb4_general_ci" . $comment;
|
||||
return $sql . ";\n\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* Standard escaping for SQL definitions
|
||||
*
|
||||
* @param string $sqlString the SQL string to escape
|
||||
*
|
||||
* @return string escaped SQL string
|
||||
*/
|
||||
public static function escape(string $sqlString): string
|
||||
{
|
||||
return str_replace("'", "\\'", $sqlString);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the SQL definition to add a foreign key
|
||||
*
|
||||
* @param string $keyName The foreign key name
|
||||
* @param array $parameters The given parameters of the foreign key
|
||||
*
|
||||
* @return string The SQL definition
|
||||
*/
|
||||
public static function addForeignKey(string $keyName, array $parameters): string
|
||||
{
|
||||
return sprintf("ADD %s", static::foreignCommand($keyName, $parameters));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the SQL definition to drop a foreign key
|
||||
*
|
||||
* @param string $keyName The foreign key name
|
||||
*
|
||||
* @return string The SQL definition
|
||||
*/
|
||||
public static function dropForeignKey(string $keyName): string
|
||||
{
|
||||
return sprintf("DROP FOREIGN KEY `%s`", $keyName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the SQL definition to drop an index
|
||||
*
|
||||
* @param string $indexName The index name
|
||||
*
|
||||
* @return string The SQL definition
|
||||
*/
|
||||
public static function dropIndex(string $indexName): string
|
||||
{
|
||||
return sprintf("DROP INDEX `%s`", static::escape($indexName));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the SQL definition to add a table field
|
||||
*
|
||||
* @param string $fieldName The table field name
|
||||
* @param array $parameters The parameters of the table field
|
||||
*
|
||||
* @return string The SQL definition
|
||||
*/
|
||||
public static function addTableField(string $fieldName, array $parameters): string
|
||||
{
|
||||
return sprintf("ADD `%s` %s", static::escape($fieldName), static::FieldCommand($parameters));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the SQL definition to modify a table field
|
||||
*
|
||||
* @param string $fieldName The table field name
|
||||
* @param array $parameters The paramters to modify
|
||||
*
|
||||
* @return string The SQL definition
|
||||
*/
|
||||
public static function modifyTableField(string $fieldName, array $parameters): string
|
||||
{
|
||||
return sprintf("MODIFY `%s` %s", static::escape($fieldName), self::FieldCommand($parameters, false));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns SQL statement for field
|
||||
*
|
||||
* @param array $parameters Parameters for SQL statement
|
||||
* @param boolean $create Whether to include PRIMARY KEY statement (unused)
|
||||
* @return string SQL statement part
|
||||
*/
|
||||
public static function FieldCommand(array $parameters, bool $create = true): string
|
||||
{
|
||||
$fieldstruct = $parameters['type'];
|
||||
|
||||
if (isset($parameters['Collation'])) {
|
||||
$fieldstruct .= ' COLLATE ' . $parameters['Collation'];
|
||||
}
|
||||
|
||||
if (isset($parameters['not null'])) {
|
||||
$fieldstruct .= ' NOT NULL';
|
||||
}
|
||||
|
||||
if (isset($parameters['default'])) {
|
||||
if (strpos(strtolower($parameters['type']), 'int') !== false) {
|
||||
$fieldstruct .= ' DEFAULT ' . $parameters['default'];
|
||||
} else {
|
||||
$fieldstruct .= " DEFAULT '" . $parameters['default'] . "'";
|
||||
}
|
||||
}
|
||||
if (isset($parameters['extra'])) {
|
||||
$fieldstruct .= ' ' . $parameters['extra'];
|
||||
}
|
||||
|
||||
if (isset($parameters['comment'])) {
|
||||
$fieldstruct .= " COMMENT '" . static::escape($parameters['comment']) . "'";
|
||||
}
|
||||
|
||||
/*if (($parameters['primary'] != '') && $create)
|
||||
$fieldstruct .= ' PRIMARY KEY';*/
|
||||
|
||||
return $fieldstruct;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the SQL definition to create an index
|
||||
*
|
||||
* @param string $indexName The index name
|
||||
* @param array $fieldNames The field names of this index
|
||||
* @param string $method The method to create the index (default is ADD)
|
||||
*
|
||||
* @return string The SQL definition
|
||||
* @throws Exception in cases the paramter contains invalid content
|
||||
*/
|
||||
public static function createIndex(string $indexName, array $fieldNames, string $method = 'ADD'): string
|
||||
{
|
||||
$method = strtoupper(trim($method));
|
||||
if ($method != '' && $method != 'ADD') {
|
||||
throw new Exception("Invalid parameter 'method' in self::createIndex(): '$method'");
|
||||
}
|
||||
|
||||
if (in_array($fieldNames[0], ['UNIQUE', 'FULLTEXT'])) {
|
||||
$index_type = array_shift($fieldNames);
|
||||
$method .= " " . $index_type;
|
||||
}
|
||||
|
||||
$names = "";
|
||||
foreach ($fieldNames as $fieldName) {
|
||||
if ($names != '') {
|
||||
$names .= ',';
|
||||
}
|
||||
|
||||
if (preg_match('|(.+)\((\d+)\)|', $fieldName, $matches)) {
|
||||
$names .= "`" . static::escape($matches[1]) . "`(" . intval($matches[2]) . ")";
|
||||
} else {
|
||||
$names .= "`" . static::escape($fieldName) . "`";
|
||||
}
|
||||
}
|
||||
|
||||
if ($indexName == 'PRIMARY') {
|
||||
return sprintf("%s PRIMARY KEY(%s)", $method, $names);
|
||||
}
|
||||
|
||||
|
||||
return sprintf("%s INDEX `%s` (%s)", $method, static::escape($indexName), $names);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the SQL definition for foreign keys
|
||||
*
|
||||
* @param string $foreignKeyName The foreign key name
|
||||
* @param array $parameters The parameters of the foreign key
|
||||
*
|
||||
* @return string The SQL definition
|
||||
*/
|
||||
public static function foreignCommand(string $foreignKeyName, array $parameters): string
|
||||
{
|
||||
$foreign_table = array_keys($parameters['foreign'])[0];
|
||||
$foreign_field = array_values($parameters['foreign'])[0];
|
||||
|
||||
$sql = "FOREIGN KEY (`" . $foreignKeyName . "`) REFERENCES `" . $foreign_table . "` (`" . $foreign_field . "`)";
|
||||
|
||||
if (!empty($parameters['foreign']['on update'])) {
|
||||
$sql .= " ON UPDATE " . strtoupper($parameters['foreign']['on update']);
|
||||
} else {
|
||||
$sql .= " ON UPDATE RESTRICT";
|
||||
}
|
||||
|
||||
if (!empty($parameters['foreign']['on delete'])) {
|
||||
$sql .= " ON DELETE " . strtoupper($parameters['foreign']['on delete']);
|
||||
} else {
|
||||
$sql .= " ON DELETE CASCADE";
|
||||
}
|
||||
|
||||
return $sql;
|
||||
}
|
||||
}
|
145
src/Util/Writer/DocWriter.php
Normal file
145
src/Util/Writer/DocWriter.php
Normal file
|
@ -0,0 +1,145 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2022, the Friendica project
|
||||
*
|
||||
* @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/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Util\Writer;
|
||||
|
||||
use Friendica\Core\Renderer;
|
||||
use Friendica\Database\Definition\DbaDefinition;
|
||||
use Friendica\Network\HTTPException\ServiceUnavailableException;
|
||||
|
||||
/**
|
||||
* Utility class to write content into the '/doc' directory
|
||||
*/
|
||||
class DocWriter
|
||||
{
|
||||
/**
|
||||
* Creates all database definitions as Markdown fields and create the mkdoc config file.
|
||||
*
|
||||
* @param DbaDefinition $definition The Database definition class
|
||||
* @param string $basePath The basepath of Friendica
|
||||
*
|
||||
* @return void
|
||||
* @throws ServiceUnavailableException in really unexpected cases!
|
||||
*/
|
||||
public static function writeDbDefinition(DbaDefinition $definition, string $basePath)
|
||||
{
|
||||
$tables = [];
|
||||
foreach ($definition->getAll() as $name => $definition) {
|
||||
$indexes = [
|
||||
[
|
||||
'name' => 'Name',
|
||||
'fields' => 'Fields',
|
||||
],
|
||||
[
|
||||
'name' => '-',
|
||||
'fields' => '-',
|
||||
]
|
||||
];
|
||||
|
||||
$lengths = ['name' => 4, 'fields' => 6];
|
||||
foreach ($definition['indexes'] as $key => $value) {
|
||||
$fieldlist = implode(', ', $value);
|
||||
$indexes[] = ['name' => $key, 'fields' => $fieldlist];
|
||||
$lengths['name'] = max($lengths['name'], strlen($key));
|
||||
$lengths['fields'] = max($lengths['fields'], strlen($fieldlist));
|
||||
}
|
||||
|
||||
array_walk_recursive($indexes, function (&$value, $key) use ($lengths) {
|
||||
$value = str_pad($value, $lengths[$key], $value === '-' ? '-' : ' ');
|
||||
});
|
||||
|
||||
$foreign = [];
|
||||
$fields = [
|
||||
[
|
||||
'name' => 'Field',
|
||||
'comment' => 'Description',
|
||||
'type' => 'Type',
|
||||
'null' => 'Null',
|
||||
'primary' => 'Key',
|
||||
'default' => 'Default',
|
||||
'extra' => 'Extra',
|
||||
],
|
||||
[
|
||||
'name' => '-',
|
||||
'comment' => '-',
|
||||
'type' => '-',
|
||||
'null' => '-',
|
||||
'primary' => '-',
|
||||
'default' => '-',
|
||||
'extra' => '-',
|
||||
]
|
||||
];
|
||||
$lengths = [
|
||||
'name' => 5,
|
||||
'comment' => 11,
|
||||
'type' => 4,
|
||||
'null' => 4,
|
||||
'primary' => 3,
|
||||
'default' => 7,
|
||||
'extra' => 5,
|
||||
];
|
||||
foreach ($definition['fields'] as $key => $value) {
|
||||
$field = [];
|
||||
$field['name'] = $key;
|
||||
$field['comment'] = $value['comment'] ?? '';
|
||||
$field['type'] = $value['type'];
|
||||
$field['null'] = ($value['not null'] ?? false) ? 'NO' : 'YES';
|
||||
$field['primary'] = ($value['primary'] ?? false) ? 'PRI' : '';
|
||||
$field['default'] = $value['default'] ?? 'NULL';
|
||||
$field['extra'] = $value['extra'] ?? '';
|
||||
|
||||
foreach ($field as $fieldName => $fieldvalue) {
|
||||
$lengths[$fieldName] = max($lengths[$fieldName] ?? 0, strlen($fieldvalue));
|
||||
}
|
||||
$fields[] = $field;
|
||||
|
||||
if (!empty($value['foreign'])) {
|
||||
$foreign[] = [
|
||||
'field' => $key,
|
||||
'targettable' => array_keys($value['foreign'])[0],
|
||||
'targetfield' => array_values($value['foreign'])[0]
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
array_walk_recursive($fields, function (&$value, $key) use ($lengths) {
|
||||
$value = str_pad($value, $lengths[$key], $value === '-' ? '-' : ' ');
|
||||
});
|
||||
|
||||
$tables[] = ['name' => $name, 'comment' => $definition['comment']];
|
||||
$content = Renderer::replaceMacros(Renderer::getMarkupTemplate('structure.tpl'), [
|
||||
'$name' => $name,
|
||||
'$comment' => $definition['comment'],
|
||||
'$fields' => $fields,
|
||||
'$indexes' => $indexes,
|
||||
'$foreign' => $foreign,
|
||||
]);
|
||||
$filename = $basePath . '/doc/database/db_' . $name . '.md';
|
||||
file_put_contents($filename, $content);
|
||||
}
|
||||
asort($tables);
|
||||
$content = Renderer::replaceMacros(Renderer::getMarkupTemplate('tables.tpl'), [
|
||||
'$tables' => $tables,
|
||||
]);
|
||||
$filename = $basePath . '/doc/database.md';
|
||||
file_put_contents($filename, $content);
|
||||
}
|
||||
}
|
66
src/Util/Writer/ViewDefinitionSqlWriter.php
Normal file
66
src/Util/Writer/ViewDefinitionSqlWriter.php
Normal file
|
@ -0,0 +1,66 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2022, the Friendica project
|
||||
*
|
||||
* @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/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Util\Writer;
|
||||
|
||||
use Friendica\Database\Definition\ViewDefinition;
|
||||
|
||||
class ViewDefinitionSqlWriter
|
||||
{
|
||||
public static function create(ViewDefinition $definition): string
|
||||
{
|
||||
$sqlString = '';
|
||||
|
||||
foreach ($definition->getAll() as $viewName => $viewStructure) {
|
||||
$sqlString .= "--\n";
|
||||
$sqlString .= "-- VIEW $viewName\n";
|
||||
$sqlString .= "--\n";
|
||||
$sqlString .= static::dropView($viewName);
|
||||
$sqlString .= static::createView($viewName, $viewStructure);
|
||||
}
|
||||
|
||||
return $sqlString;
|
||||
}
|
||||
|
||||
public static function dropView(string $viewName): string
|
||||
{
|
||||
return sprintf("DROP VIEW IF EXISTS `%s`", static::escape($viewName)) . ";\n";
|
||||
}
|
||||
|
||||
public static function createView(string $viewName, array $viewStructure): string
|
||||
{
|
||||
$sql_rows = [];
|
||||
foreach ($viewStructure['fields'] as $fieldname => $origin) {
|
||||
if (is_string($origin)) {
|
||||
$sql_rows[] = $origin . " AS `" . static::escape($fieldname) . "`";
|
||||
} elseif (is_array($origin) && (sizeof($origin) == 2)) {
|
||||
$sql_rows[] = "`" . static::escape($origin[0]) . "`.`" . static::escape($origin[1]) . "` AS `" . static::escape($fieldname) . "`";
|
||||
}
|
||||
}
|
||||
return sprintf("CREATE VIEW `%s` AS SELECT \n\t", static::escape($viewName)) .
|
||||
implode(",\n\t", $sql_rows) . "\n\t" . $viewStructure['query'] . ";\n\n";
|
||||
}
|
||||
|
||||
public static function escape(string $sqlString): string
|
||||
{
|
||||
return str_replace("'", "\\'", $sqlString);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue