getListViews = $getListViews; $this->resourceConnection = $resourceConnection; } /** * Write backup data to backup file. * * @param BackupInterface $backup */ public function execute(BackupInterface $backup): void { $views = $this->getListViews->execute(); foreach ($views as $view) { $backup->write($this->getViewHeader($view)); $backup->write($this->getDropViewSql($view)); $backup->write($this->getCreateView($view)); } } /** * Retrieve Database connection for Backup. * * @return AdapterInterface */ private function getConnection(): AdapterInterface { if (!$this->connection) { $this->connection = $this->resourceConnection->getConnection('backup'); } return $this->connection; } /** * Get CREATE VIEW query for the specific view. * * @param string $viewName * @return string */ private function getCreateView(string $viewName): string { $quotedViewName = $this->getConnection()->quoteIdentifier($viewName); $query = 'SHOW CREATE VIEW ' . $quotedViewName; $row = $this->getConnection()->fetchRow($query); $regExp = '/\sDEFINER\=\`([^`]*)\`\@\`([^`]*)\`/'; $sql = preg_replace($regExp, '', $row['Create View']); return $sql . ';' . "\n"; } /** * Prepare a header for View being dumped. * * @param string $viewName * @return string */ public function getViewHeader(string $viewName): string { $quotedViewName = $this->getConnection()->quoteIdentifier($viewName); return "\n--\n" . "-- Structure for view {$quotedViewName}\n" . "--\n\n"; } /** * Make sure that View being created is deleted if already exists. * * @param string $viewName * @return string */ public function getDropViewSql(string $viewName): string { $quotedViewName = $this->getConnection()->quoteIdentifier($viewName); return sprintf('DROP VIEW IF EXISTS %s;\n', $quotedViewName); } }