From 1cbb338acda9248c23e182d275f3f58f0e08f019 Mon Sep 17 00:00:00 2001 From: "choi.jh" Date: Fri, 11 Jul 2025 17:17:59 +0900 Subject: [PATCH] dbms_init...1 --- .../CLI/DBMigration/DBMigration.php | 26 +++---------- app/Controllers/CLI/DBMigration/SourceDB.php | 38 ++++++++++++++++--- 2 files changed, 37 insertions(+), 27 deletions(-) diff --git a/app/Controllers/CLI/DBMigration/DBMigration.php b/app/Controllers/CLI/DBMigration/DBMigration.php index ed3ea43..0b8c4ab 100644 --- a/app/Controllers/CLI/DBMigration/DBMigration.php +++ b/app/Controllers/CLI/DBMigration/DBMigration.php @@ -39,7 +39,7 @@ abstract class DBMigration extends BaseController /** * 공통 마이그레이션 처리 로직 */ - private function processMigration(MigrationProcessInterface $processor, array $rows, string $functionName): void + private function migration(MigrationProcessInterface $processor, array $rows, string $functionName): void { $this->getTargetDB()->transStart(); try { @@ -58,37 +58,21 @@ abstract class DBMigration extends BaseController final public function client(): void { - $this->processMigration( - new ClientProcess($this->getTargetDB()), - $this->getClient(), - __FUNCTION__ - ); + $this->migration(new ClientProcess($this->getTargetDB()), $this->getClient(), __FUNCTION__); } final public function line(): void { - $this->processMigration( - new LineProcess($this->getTargetDB()), - $this->getLine(), - __FUNCTION__ - ); + $this->migration(new LineProcess($this->getTargetDB()), $this->getLine(), __FUNCTION__); } final public function servercode(): void { - $this->processMigration( - new ServerCodeProcess($this->getTargetDB()), - $this->getServerCode(), - __FUNCTION__ - ); + $this->migration(new ServerCodeProcess($this->getTargetDB()), $this->getServerCode(), __FUNCTION__); } final public function switchcode(): void { - $this->processMigration( - new SwitchCodeProcess($this->getTargetDB()), - $this->getSwitchCode(), - __FUNCTION__ - ); + $this->migration(new SwitchCodeProcess($this->getTargetDB()), $this->getSwitchCode(), __FUNCTION__); } } diff --git a/app/Controllers/CLI/DBMigration/SourceDB.php b/app/Controllers/CLI/DBMigration/SourceDB.php index 1514074..9ca8bac 100644 --- a/app/Controllers/CLI/DBMigration/SourceDB.php +++ b/app/Controllers/CLI/DBMigration/SourceDB.php @@ -6,18 +6,32 @@ use CodeIgniter\Database\BaseConnection; use CodeIgniter\HTTP\RequestInterface; use CodeIgniter\HTTP\ResponseInterface; use Psr\Log\LoggerInterface; +use App\Config\SourceDatabase; class SourceDB extends DBMigration { private $_sourceDB = null; + + /** + * SQL 쿼리 상수 정의 + */ + private const SQL_GET_CLIENT = "SELECT * FROM clientdb"; + private const SQL_GET_LINE = "SELECT Line_code, line_case, Line_ip, line_client_name, line_gateway, line_id, line_pass, line_name, line_haveip, line_phone, line_mainip FROM linedb"; + private const SQL_GET_SERVER_CODE = "SELECT DISTINCT(server_code) FROM serverdb WHERE server_code NOT LIKE ?"; + private const SQL_GET_SWITCH_CODE = "SELECT DISTINCT(service_sw) FROM servicedb WHERE service_sw NOT LIKE ?"; + public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger) { parent::initController($request, $response, $logger); } + + /** + * 소스 데이터베이스 연결 반환 + */ final protected function getSourceDB(): BaseConnection { if ($this->_sourceDB === null) { - $primeidc = [ + $config = [ 'DSN' => '', 'hostname' => env('database.source.hostname', 'localhost'), 'username' => env('database.source.username', 'root'), @@ -38,26 +52,38 @@ class SourceDB extends DBMigration 'failover' => [], 'port' => 3306, ]; - $this->_sourceDB = \Config\Database::connect($primeidc); + $this->_sourceDB = \Config\Database::connect($config); } return $this->_sourceDB; } + + /** + * 공통 쿼리 실행 메서드 + */ + private function executeQuery(string $sql, array $params = []): array + { + return $this->getSourceDB() + ->query($sql, $params) + ->getResultArray(); + } + protected function getClient(): array { - return $this->getSourceDB()->query("SELECT * FROM clientdb")->getResultArray(); + return $this->executeQuery(self::SQL_GET_CLIENT); } + protected function getLine(): array { - return $this->getSourceDB()->query("SELECT Line_code,line_case,Line_ip,line_client_name,line_gateway,line_id,line_pass,line_name,line_haveip,line_phone,line_mainip from linedb;select Line_code,line_case,Line_ip,line_client_name,line_gateway,line_id,line_pass,line_name,line_haveip,line_phone,line_mainip FROM linedb")->getResultArray(); + return $this->executeQuery(self::SQL_GET_LINE); } protected function getServerCode(): array { - return $this->getSourceDB()->query("SELECT DISTINCT(server_code) FROM serverdb WHERE server_code NOT LIKE ('%일회성%')")->getResultArray(); + return $this->executeQuery(self::SQL_GET_SERVER_CODE, ['%일회성%']); } protected function getSwitchCode(): array { - return $this->getSourceDB()->query("SELECT DISTINCT(service_sw) FROM servicedb WHERE service_sw NOT LIKE ('%일회성%')")->getResultArray(); + return $this->executeQuery(self::SQL_GET_SWITCH_CODE, ['%일회성%']); } }