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
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