From: Michael Date: Fri, 24 Apr 2020 08:48:34 +0000 (+0000) Subject: DBView is View X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=afa712b8112e8e827112b3438f65e781da95b91a;p=friendica.git DBView is View --- diff --git a/src/Database/DBStructure.php b/src/Database/DBStructure.php index 372e3c496f..1433a5346a 100644 --- a/src/Database/DBStructure.php +++ b/src/Database/DBStructure.php @@ -113,7 +113,7 @@ class DBStructure echo "\n"; } - DBView::printStructure($basePath); + View::printStructure($basePath); } /** @@ -596,7 +596,7 @@ class DBStructure } } - DBView::create($verbose, $action); + View::create($verbose, $action); if ($action && !$install) { DI::config()->set('system', 'maintenance', 0); diff --git a/src/Database/DBView.php b/src/Database/DBView.php deleted file mode 100644 index cd1d22e1e1..0000000000 --- a/src/Database/DBView.php +++ /dev/null @@ -1,142 +0,0 @@ -. - * - */ - -namespace Friendica\Database; - -use Exception; -use Friendica\Core\Hook; -use Friendica\Core\Logger; -use Friendica\DI; -use Friendica\Util\DateTimeFormat; -use phpDocumentor\Reflection\Types\Boolean; - -require_once __DIR__ . '/../../include/dba.php'; - -class DBView -{ - /** - * view definition loaded from config/dbview.config.php - * - * @var array - */ - private static $definition = []; - - /** - * Loads the database structure definition from the config/dbview.config.php file. - * On first pass, defines DB_UPDATE_VERSION constant. - * - * @see static/dbview.config.php - * @param boolean $with_addons_structure Whether to tack on addons additional tables - * @param string $basePath The base path of this application - * @return array - * @throws Exception - */ - public static function definition($basePath = '', $with_addons_structure = true) - { - if (!self::$definition) { - if (empty($basePath)) { - $basePath = DI::app()->getBasePath(); - } - - $filename = $basePath . '/static/dbview.config.php'; - - if (!is_readable($filename)) { - throw new Exception('Missing database view config file static/dbview.config.php'); - } - - $definition = require $filename; - - if (!$definition) { - throw new Exception('Corrupted database view config file static/dbview.config.php'); - } - - self::$definition = $definition; - } else { - $definition = self::$definition; - } - - if ($with_addons_structure) { - Hook::callAll('dbview_definition', $definition); - } - - return $definition; - } - - public static function create(bool $verbose, bool $action) - { - $definition = self::definition(); - - foreach ($definition as $name => $structure) { - self::createview($name, $structure, $verbose, $action); - } - } - - public static function printStructure($basePath) - { - $database = self::definition($basePath, false); - - foreach ($database AS $name => $structure) { - echo "--\n"; - echo "-- VIEW $name\n"; - echo "--\n"; - self::createView($name, $structure, true, false); - - echo "\n"; - } - } - - private static function createview($name, $structure, $verbose, $action) - { - $r = true; - - $sql_rows = []; - foreach ($structure["fields"] AS $fieldname => $origin) { - if (is_string($origin)) { - $sql_rows[] = $origin . " AS `" . DBA::escape($fieldname) . "`"; - } elseif (is_array($origin) && (sizeof($origin) == 2)) { - $sql_rows[] = "`" . DBA::escape($origin[0]) . "`.`" . DBA::escape($origin[1]) . "` AS `" . DBA::escape($fieldname) . "`"; - } - } - - $sql = sprintf("DROP VIEW IF EXISTS `%s`", DBA::escape($name)); - - if ($verbose) { - echo $sql . ";\n"; - } - - if ($action) { - DBA::e($sql); - } - - $sql = sprintf("CREATE VIEW `%s` AS SELECT \n\t", DBA::escape($name)) . - implode(",\n\t", $sql_rows) . "\n\t" . $structure['query']; - - if ($verbose) { - echo $sql . ";\n"; - } - - if ($action) { - $r = DBA::e($sql); - } - - return $r; - } -} diff --git a/src/Database/View.php b/src/Database/View.php new file mode 100644 index 0000000000..fa4c502a23 --- /dev/null +++ b/src/Database/View.php @@ -0,0 +1,139 @@ +. + * + */ + +namespace Friendica\Database; + +use Exception; +use Friendica\Core\Hook; +use Friendica\DI; + +require_once __DIR__ . '/../../include/dba.php'; + +class View +{ + /** + * view definition loaded from config/dbview.config.php + * + * @var array + */ + private static $definition = []; + + /** + * Loads the database structure definition from the config/dbview.config.php file. + * On first pass, defines DB_UPDATE_VERSION constant. + * + * @see static/dbview.config.php + * @param boolean $with_addons_structure Whether to tack on addons additional tables + * @param string $basePath The base path of this application + * @return array + * @throws Exception + */ + public static function definition($basePath = '', $with_addons_structure = true) + { + if (!self::$definition) { + if (empty($basePath)) { + $basePath = DI::app()->getBasePath(); + } + + $filename = $basePath . '/static/dbview.config.php'; + + if (!is_readable($filename)) { + throw new Exception('Missing database view config file static/dbview.config.php'); + } + + $definition = require $filename; + + if (!$definition) { + throw new Exception('Corrupted database view config file static/dbview.config.php'); + } + + self::$definition = $definition; + } else { + $definition = self::$definition; + } + + if ($with_addons_structure) { + Hook::callAll('dbview_definition', $definition); + } + + return $definition; + } + + public static function create(bool $verbose, bool $action) + { + $definition = self::definition(); + + foreach ($definition as $name => $structure) { + self::createview($name, $structure, $verbose, $action); + } + } + + public static function printStructure($basePath) + { + $database = self::definition($basePath, false); + + foreach ($database AS $name => $structure) { + echo "--\n"; + echo "-- VIEW $name\n"; + echo "--\n"; + self::createView($name, $structure, true, false); + + echo "\n"; + } + } + + private static function createview($name, $structure, $verbose, $action) + { + $r = true; + + $sql_rows = []; + foreach ($structure["fields"] AS $fieldname => $origin) { + if (is_string($origin)) { + $sql_rows[] = $origin . " AS `" . DBA::escape($fieldname) . "`"; + } elseif (is_array($origin) && (sizeof($origin) == 2)) { + $sql_rows[] = "`" . DBA::escape($origin[0]) . "`.`" . DBA::escape($origin[1]) . "` AS `" . DBA::escape($fieldname) . "`"; + } + } + + $sql = sprintf("DROP VIEW IF EXISTS `%s`", DBA::escape($name)); + + if ($verbose) { + echo $sql . ";\n"; + } + + if ($action) { + DBA::e($sql); + } + + $sql = sprintf("CREATE VIEW `%s` AS SELECT \n\t", DBA::escape($name)) . + implode(",\n\t", $sql_rows) . "\n\t" . $structure['query']; + + if ($verbose) { + echo $sql . ";\n"; + } + + if ($action) { + $r = DBA::e($sql); + } + + return $r; + } +}