diff --git a/CHANGELOG.md b/CHANGELOG.md index 6826f11c0f66135de86575b9460a67f88793bb9d..90a7fc3f9923dde4745d330e340e81c95ea25f69 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +3.1.14 +------ +- Nouvelle ligne de commande 'run-sql-query' pour exécuter une requête SQL. +- Correction de ConsoleControllerFactory non migrée en ZF3. + 3.1.13 ------ - AjaxPopover : possibilité de désactiver l'interception du submit ajax en ajoutant la classe 'disable-ajax-submit' sur le formulaire. diff --git a/Module.php b/Module.php index 4df283a75f7e58b08c2323c590271c312049ea7f..c61cb9b30702f5667a9c14ef5a9fff86b159490c 100644 --- a/Module.php +++ b/Module.php @@ -238,6 +238,13 @@ class Module implements ['--path', "Requis. Chemin vers le script SQL à exécuter."], ['--logfile', "Facultatif. Chemin du fichier des logs d'exécution du script. Par défaut, il est généré."], ['--connection', "Facultatif. Identifiant de la connexion Doctrine. Par défaut : 'orm_default'."], + + // command + 'run-sql-query --sql= [--logfile=] [--connection=]' => "Exécuter une requête SQL", + // parameters + ['--sql', "Requis. Requête SQL à exécuter. Ex: \"begin DBMS_MVIEW.REFRESH('MV_RECHERCHE_THESE'); end;\"."], + ['--logfile', "Facultatif. Chemin du fichier des logs d'exécution. Par défaut, il est généré."], + ['--connection', "Facultatif. Identifiant de la connexion Doctrine. Par défaut : 'orm_default'."], ]; } } diff --git a/config/module.config.php b/config/module.config.php index 92de46836cf227f2d83b9dff47b380f1bff84bda..138d53e3980e34078eee66208fd7e13f80609f2a 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -251,7 +251,7 @@ return [ 'console' => [ 'router' => [ 'routes' => [ - 'run-sql' => [ + 'run-sql-script' => [ 'type' => Simple::class, 'options' => [ 'route' => 'run-sql-script --path= [--logfile=] [--connection=]', @@ -261,6 +261,16 @@ return [ ], ], ], + 'run-sql-query' => [ + 'type' => Simple::class, + 'options' => [ + 'route' => 'run-sql-query --sql= [--logfile=] [--connection=]', + 'defaults' => [ + 'controller' => ConsoleController::class, + 'action' => 'runSQLQuery', + ], + ], + ], ], ], 'view_manager' => [ diff --git a/src/UnicaenApp/Controller/ConsoleController.php b/src/UnicaenApp/Controller/ConsoleController.php index 1f10396e59a0badc6eeedb41771b365b5e4b1d01..83478656477a25f5192123338592fee7b825978f 100644 --- a/src/UnicaenApp/Controller/ConsoleController.php +++ b/src/UnicaenApp/Controller/ConsoleController.php @@ -4,6 +4,7 @@ namespace UnicaenApp\Controller; use Doctrine\DBAL\Connection; use Exception; +use Interop\Container\ContainerInterface; use UnicaenApp\Exception\RuntimeException; use UnicaenApp\Service\SQL\RunSQLServiceAwareTrait; use Zend\Log\Filter\Priority; @@ -11,60 +12,101 @@ use Zend\Log\Formatter\Simple; use Zend\Log\Logger; use Zend\Log\LoggerAwareTrait; use Zend\Log\Writer\Stream; -use Zend\Mvc\Controller\AbstractConsoleController; -use Zend\ServiceManager\ServiceLocatorAwareTrait; +use Zend\Mvc\Console\Controller\AbstractConsoleController; class ConsoleController extends AbstractConsoleController { - use ServiceLocatorAwareTrait; use LoggerAwareTrait; use RunSQLServiceAwareTrait; + /** + * @var ContainerInterface + */ + protected $container; + + /** + * @var Connection + */ + protected $connection; + + /** + * @var Logger + */ + protected $logger; + + /** + * @param ContainerInterface $container + * @return self + */ + public function setContainer(ContainerInterface $container): self + { + $this->container = $container; + return $this; + } + /** * @throws Exception */ - public function runSQLScriptAction() + public function runSQLQueryAction() { - $path = $this->params('path'); + $sql = $this->params('sql'); $connection = $this->params('connection', 'orm_default'); $logFilepath = $this->params('logfile'); - $serviceName = "doctrine.connection.$connection"; - $request = implode(' ', $this->getRequest()->getContent()); + $this->createLogger(); + $this->logger->info("### Exécution de commandes SQL ###"); + $this->logger->info(date_format(date_create(), 'd/m/Y H:i:s')); - $logger = $this->createLogger(); + $this->initConnection($connection); - $logger->info("### Exécution de scripts SQL ###"); - $logger->info(date_format(date_create(), 'd/m/Y H:i:s')); + $this->runSQLService->setLogger($this->logger); + $result = $this->runSQLService->runSQLQuery($sql, $this->connection, $logFilepath); - if (! $this->serviceLocator->has($serviceName)) { - throw new RuntimeException("Connection Doctrine introuvable : $serviceName"); + if ($result->isSuccess()) { + $this->logger->info("Exécution terminée avec succès."); + } else { + $this->logger->info("OUPS, UNE ERREUR EST SURVENUE !"); + } + + $this->logger->info("Durée : " . $result->getDurationInSec() . " sec"); + + if (! $result->isSuccess()) { + exit(1); } - /** @var Connection $conn */ - $conn = $this->serviceLocator->get($serviceName); + } - $logger->info("Connexion : '$serviceName'"); + /** + * @throws Exception + */ + public function runSQLScriptAction() + { + $path = $this->params('path'); + $connection = $this->params('connection', 'orm_default'); + $logFilepath = $this->params('logfile'); + + $this->createLogger(); + $this->logger->info("### Exécution de scripts SQL ###"); + $this->logger->info(date_format(date_create(), 'd/m/Y H:i:s')); + + $this->initConnection($connection); - $this->runSQLService->setLogger($logger); - $result = $this->runSQLService->runSQLScript($path, $conn, $logFilepath); + $this->runSQLService->setLogger($this->logger); + $result = $this->runSQLService->runSQLScript($path, $this->connection, $logFilepath); if ($result->isSuccess()) { - $logger->info("Exécution terminée avec succès."); + $this->logger->info("Exécution terminée avec succès."); } else { - $logger->info("OUPS, UNE ERREUR EST SURVENUE !"); + $this->logger->info("OUPS, UNE ERREUR EST SURVENUE !"); } - $logger->info("Durée : " . $result->getDurationInSec() . " sec"); + $this->logger->info("Durée : " . $result->getDurationInSec() . " sec"); if (! $result->isSuccess()) { exit(1); } } - /** - * @return Logger - */ private function createLogger() { $filter = new Priority(Logger::INFO); @@ -76,10 +118,17 @@ class ConsoleController extends AbstractConsoleController $writer->addFilter($filter); $writer->setFormatter($formatter); - /** @var Logger $logger */ - $logger = new Logger(); - $logger->addWriter($writer); + $this->logger = new Logger(); + $this->logger->addWriter($writer); + } + + protected function initConnection(string $name) + { + $serviceName = "doctrine.connection.$name"; + if (! $this->container->has($serviceName)) { + throw new RuntimeException("Connection Doctrine introuvable : $serviceName"); + } - return $logger; + $this->connection = $this->container->get($serviceName); } } diff --git a/src/UnicaenApp/Controller/ConsoleControllerFactory.php b/src/UnicaenApp/Controller/ConsoleControllerFactory.php index 6250d2036f199af285d14ec1cf016c0ca6fc13c3..67bc0d7735694c0593662787bf3ffe0cc07418a3 100644 --- a/src/UnicaenApp/Controller/ConsoleControllerFactory.php +++ b/src/UnicaenApp/Controller/ConsoleControllerFactory.php @@ -2,22 +2,26 @@ namespace UnicaenApp\Controller; +use Interop\Container\ContainerInterface; use UnicaenApp\Service\SQL\RunSQLService; use Zend\Log\Logger; use Zend\Log\LoggerInterface; use Zend\Log\Writer\Noop; -use Zend\Mvc\Controller\ControllerManager; +use Zend\ServiceManager\Factory\FactoryInterface; -class ConsoleControllerFactory +class ConsoleControllerFactory implements FactoryInterface { - public function __invoke(ControllerManager $sl) + /** + * @inheritDoc + */ + public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { - /** @var \UnicaenApp\Service\SQL\RunSQLService $runSQLService */ - $runSQLService = $sl->getServiceLocator()->get(RunSQLService::class); + /** @var RunSQLService $runSQLService */ + $runSQLService = $container->get(RunSQLService::class); $controller = new ConsoleController(); $controller->setLogger($this->createLogger()); - $controller->setServiceLocator($sl->getServiceLocator()); + $controller->setContainer($container); $controller->setRunSQLService($runSQLService); return $controller; @@ -28,7 +32,6 @@ class ConsoleControllerFactory */ private function createLogger() { - /** @var Logger $logger */ $logger = new Logger(); $logger->addWriter(new Noop()); diff --git a/src/UnicaenApp/Service/SQL/RunSQLProcess.php b/src/UnicaenApp/Service/SQL/RunSQLProcess.php index 464bbdaadc91978ec3a3db04520b661e4e2e42dc..06e7499dad760a2adf05d568be16448588e5fed7 100644 --- a/src/UnicaenApp/Service/SQL/RunSQLProcess.php +++ b/src/UnicaenApp/Service/SQL/RunSQLProcess.php @@ -3,7 +3,6 @@ namespace UnicaenApp\Service\SQL; use Doctrine\DBAL\Connection; -use Doctrine\DBAL\DBALException; use Exception; use UnicaenApp\Exception\RuntimeException; use Zend\Log\LoggerAwareTrait; @@ -47,7 +46,7 @@ class RunSQLProcess * @param string $scriptPath * @return self */ - public function setScriptPath($scriptPath) + public function setScriptPath(string $scriptPath) { $this->scriptPath = $scriptPath; @@ -101,12 +100,13 @@ class RunSQLProcess * @param string $query * @return RunSQLResult */ - public function executeQuery($query) + public function executeQuery(string $query) { $this->logger->info("+ Exécution d'une requête."); $this->queries = [$query]; $result = $this->executeQueries(); + $this->createLogFile(); return $result; } @@ -155,7 +155,7 @@ class RunSQLProcess $this->connection->executeQuery($query); $this->executedQueriesStack->stopQuery(); } - } catch (DBALException $e) { + } catch (\Doctrine\DBAL\Exception $e) { $result->setIsSuccess(false); $result->setException($e); @@ -180,10 +180,14 @@ class RunSQLProcess return $with . ' ' . str_replace(PHP_EOL, PHP_EOL . $with . ' ', $line); }; + $title = $this->scriptPath ? + "Log d'exécution du script SQL '{$this->scriptPath}'." : + "Log d'exécution d'une requête SQL."; + $lines = []; $lines[] = "----------------------------------------------------------------------------------------------"; $lines[] = "--"; - $lines[] = "-- Log d'exécution du script {$this->scriptPath}."; + $lines[] = "-- $title"; $lines[] = "--"; $lines[] = "-- " . date_create()->format('d/m/Y H:m:s'); $lines[] = "--"; @@ -249,7 +253,7 @@ class RunSQLProcess } $dir = sys_get_temp_dir(); - $scriptName = basename($this->scriptPath); + $scriptName = $this->scriptPath ? basename($this->scriptPath) : 'run-sql-query'; $filepathPattern = $dir . '/' . $scriptName . self::LOG_FILE_EXT_PATTERN; $filepathTemplate = $dir . '/' . $scriptName . self::LOG_FILE_EXT_TEMPLATE; diff --git a/src/UnicaenApp/Service/SQL/RunSQLService.php b/src/UnicaenApp/Service/SQL/RunSQLService.php index a930d41d5525b903bc75967e9be6894a2be03f9e..0d798cacc13de1010e0dc9f45b650d2d1c411332 100644 --- a/src/UnicaenApp/Service/SQL/RunSQLService.php +++ b/src/UnicaenApp/Service/SQL/RunSQLService.php @@ -41,13 +41,13 @@ class RunSQLService * / * </code> * - * @param string $path Chemin absolu d'un script SQL - * @param Connection $conn Connexion Doctrine à la base de données - * @param string $logFilepath Chemin du fichier de log à produire + * @param string $path Chemin absolu d'un script SQL + * @param Connection $conn Connexion Doctrine à la base de données + * @param string|null $logFilepath Chemin du fichier de log à produire * * @return RunSQLResult */ - public function runSQLScript($path, Connection $conn, $logFilepath = null) + public function runSQLScript(string $path, Connection $conn, string $logFilepath = null) { $process = new RunSQLProcess(); $process @@ -62,13 +62,13 @@ class RunSQLService /** * Exécute une requête. * - * @param string $query Requête à exécuter - * @param Connection $conn Connexion à la base de données - * @param string $logFilepath Chemin du fichier de log à produire + * @param string $query Requête à exécuter, ex: "begin DBMS_MVIEW.REFRESH('MV_RECHERCHE_THESE'); end;". + * @param Connection $conn Connexion à la base de données + * @param string|null $logFilepath Chemin du fichier de log à produire * * @return RunSQLResult */ - public function runSQLQuery($query, Connection $conn, $logFilepath = null) + public function runSQLQuery(string $query, Connection $conn, string $logFilepath = null) { $process = new RunSQLProcess(); $process