From 6e3182a07a5faea8ed1a3b420d0eec7399b8bca4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=B5=9C=EC=A4=80=ED=9D=A0?= Date: Fri, 4 Apr 2025 19:01:47 +0900 Subject: [PATCH] dbms_primeidc_init...1 --- extdbms/defense_index.php | 205 ++++++++++++------ .../Controllers/DBMS/DefenceController.php | 32 +++ .../Controllers/DBMS/NavigatorController.php | 2 +- extdbms/lib/Core/Controller.php | 28 ++- extdbms/lib/Core/Model.php | 116 ++++++---- extdbms/lib/Entities/DefenceEntity.php | 34 +++ extdbms/lib/Models/DefenceModel.php | 17 ++ extdbms/lib/Services/DefenceService.php | 38 ++++ extdbms/lib/Views/dbms/defence/mk.php | 138 ++++++++++++ 9 files changed, 492 insertions(+), 118 deletions(-) create mode 100644 extdbms/lib/Controllers/DBMS/DefenceController.php create mode 100644 extdbms/lib/Entities/DefenceEntity.php create mode 100644 extdbms/lib/Models/DefenceModel.php create mode 100644 extdbms/lib/Services/DefenceService.php create mode 100644 extdbms/lib/Views/dbms/defence/mk.php diff --git a/extdbms/defense_index.php b/extdbms/defense_index.php index c37a47c..3e93547 100644 --- a/extdbms/defense_index.php +++ b/extdbms/defense_index.php @@ -5,58 +5,136 @@ require_once 'function.php'; require_once 'lib.php'; ?> - -
- / - -
- +
+ / + +
+ - -
  • - - >수정 -
  • - - - + +?> \ No newline at end of file diff --git a/extdbms/lib/Controllers/DBMS/DefenceController.php b/extdbms/lib/Controllers/DBMS/DefenceController.php new file mode 100644 index 0000000..8d987c2 --- /dev/null +++ b/extdbms/lib/Controllers/DBMS/DefenceController.php @@ -0,0 +1,32 @@ +getView()->setPath('defence'); + } // + public function getDefenceService(): DefenceService + { + if ($this->_clientService === null) { + $this->_clientService = new DefenceService(); + } + return $this->_clientService; + } + + //방어 defense_index.php + //CLI 접속방법 : php index.php site/defence/mk/zone/존/parent/부모키/child/자식키 + //WEB 접속방법 : http://localhostsite/defence/mk/zone/존/parent/부모키/child/자식키 + public function mk(): string + { + $this->request = $this->getRequest(); + $this->entities = $this->getDefenceService()->getMKList($this->getRequest('zone')); + return $this->render(__FUNCTION__); + } +} //Class diff --git a/extdbms/lib/Controllers/DBMS/NavigatorController.php b/extdbms/lib/Controllers/DBMS/NavigatorController.php index e744952..b66bfb0 100644 --- a/extdbms/lib/Controllers/DBMS/NavigatorController.php +++ b/extdbms/lib/Controllers/DBMS/NavigatorController.php @@ -30,7 +30,7 @@ class NavigatorController extends BaseController { //전체 고객정보 $this->clients = $this->getClientService()->getEntities(); - $ip = $this->getRequest('ip'); + $ip = $this->getRequest('ip') ?? ''; //IP형식이 ipv4인지 확인 후 값가져오기 $services = []; if ($ip && $this->helper->isIPAddress($ip)) { diff --git a/extdbms/lib/Core/Controller.php b/extdbms/lib/Core/Controller.php index b8f4e8a..1f0a268 100644 --- a/extdbms/lib/Core/Controller.php +++ b/extdbms/lib/Core/Controller.php @@ -38,15 +38,27 @@ abstract class Controller } return array_key_exists($key, $this->_segments) ? $this->_segments[$key] : null; } - final public function getRequest(mixed $key = null, string $method = "GET"): mixed + final public function getRequest(mixed $key = null, mixed $method = null): mixed { - $requestDatas = $method === "POST" ? $_POST : $_GET; - $result = null; - if (!$key) - $result = $requestDatas; - else - $result = array_key_exists($key, $requestDatas) ? $requestDatas[$key] : null; - return $result; + if ($this->_request === null) { + $this->_request = match (strtoupper($method)) { + "POST" => $_POST, + "GET" => $_GET, + default => $_REQUEST, + }; + } + if ($key === null) { + return $this->_request; + } + return array_key_exists($key, $this->_request) ? $this->_request[$key] : null; + } + final public function getPost(mixed $key = null): mixed + { + return $this->getRequest($key, "POST"); + } + final public function getGet(mixed $key = null): mixed + { + return $this->getRequest($key, "GET"); } public function render(string $path) { diff --git a/extdbms/lib/Core/Model.php b/extdbms/lib/Core/Model.php index 961b340..45ec5d2 100644 --- a/extdbms/lib/Core/Model.php +++ b/extdbms/lib/Core/Model.php @@ -11,12 +11,12 @@ abstract class Model private $_db = null; private $_debug = false; private $_reset = true; - private $_querys = []; + private $_querys = ['SELECT' => 'SELECT *', 'JOIN' => [], 'ORDERBY' => [], 'LIMIT' => '']; private $_wheres = []; private $_lastQuery = ""; protected function __construct() { - $this->reset(); + $this->init(); } // abstract public function getTable(): string; abstract public function getPKField(): string; @@ -39,7 +39,7 @@ abstract class Model throw new \Exception("❌ DB 연결 실패: " . $e->getMessage()); } } - $this->reset(); + $this->init(); return $this->_db; } final public function setDebug($debug) @@ -50,48 +50,56 @@ abstract class Model { return $this->_debug; } - private function reset(): void + private function init(): void { $debug = $_ENV['DATABASE_QUERY_DEBUG'] ?? $_SERVER['DATABASE_QUERY_DEBUG'] ?? 'false'; if ($debug === "true") { $this->_debug = true; } + $this->_reset = true; + $this->_querys = ['SELECT' => 'SELECT *', 'JOIN' => [], 'ORDERBY' => [], 'LIMIT' => '']; $this->_wheres = []; - $this->_querys = ["SELECT" => "SELECT *", "JOIN" => "", "ORDERBY" => "", "LIMIT" => ""]; + $this->_lastQuery = ""; } final public function getLastQuery(): string { return $this->_lastQuery; } - private function makeValue(mixed $values): string|null + + //Where절관련 + private function getWhereValue(mixed $datas, $delimeter = ","): string { - if ($values === null) { - return $values; + $value = ""; + if ($datas === null) { + return "NULL"; } - $temps = []; - if (is_array($values)) { - foreach ($values as $value) { - $temps[] = is_string($value) ? "'{$value}'" : $value; + if (is_array($datas)) { + $temps = []; + foreach ($datas as $data) { + $data = trim($data); + $temps[] = is_string($data) ? "'{$data}'" : $data; } + $value = implode($delimeter, $temps); } else { - $temps[] = is_string($values) ? "'{$values}'" : $values; + $value = is_string($datas) ? "'{$datas}'" : $datas; } - return implode(",", $temps); + return $value; } - final public function makeWhere(mixed $columns, mixed $values = null, $delimeter = ","): string + private function getWhereColumn(mixed $columns, mixed $datas = null): string { $temps = []; if (is_array($columns)) { - foreach ($columns as $column => $value) { - $value = $this->makeValue($value); - $temps[] = sprintf("%s%s", $column, $value === null ? "" : "=" . $value); + //복합배열형태로 들어온 경우 + foreach ($columns as $column => $data) { + $value = $this->getWhereValue($data); + $temps[] = "{$column}={$value}"; } } else { - $value = $this->makeValue($values); - $temps[] = sprintf("%s%s", $columns, $value === null ? "" : "=" . $value); + $value = $this->getWhereValue($datas); + $temps[] = "{$columns}={$value}"; } // throw new \Exception("DATA:" . $columns . $value === null ? "NULL" : $value); - return implode($delimeter . " ", $temps); + return implode(" ", $temps); } final public function getWhere(): string { @@ -100,23 +108,14 @@ abstract class Model final public function where(mixed $columns, mixed $values = null, string $delimeter = "AND"): void { - $query = $this->makeWhere($columns, $values, $delimeter); + $query = $this->getWhereColumn($columns, $values); $this->_wheres[] = count($this->_wheres) ? $delimeter . " " . $query : $query; } final public function orWhere(mixed $columns, mixed $values = null, string $delimeter = "OR"): void { $this->where($columns, $values, $delimeter); } - final public function whereIn(string $column, array $values, string $delimeter = "AND", $range = "IN") - { - $query = " {$column} {$range} (" . $this->makeValue($values) . ")"; - $this->_wheres[] = count($this->_wheres) ? $delimeter . " " . $query : $query; - } - final public function whereNotIn(string $column, array $values, string $delimeter = "AND", $range = "NOT IN") - { - $this->whereIn($column, $values, $delimeter, $range); - } - final public function like(string $column, string $value, string $option = "both"): void + final public function whereLike(string $column, string $value, string $option = "both", string $delimeter = "AND"): void { switch ($option) { case 'before': @@ -129,7 +128,20 @@ abstract class Model $value = "%{$value}%"; break; } - $this->_wheres[] = "{$column} LIKE '{$value}'"; + $this->where("{$column} LIKE '{$value}'", null, $delimeter); + } + final public function orWhereLike(string $column, string $value, string $option = "both", string $delimeter = "OR"): void + { + $this->whereLike($column, $value, $option, $delimeter); + } + final public function whereIn(string $column, array $values, string $delimeter = "AND", $range = "IN") + { + $query = " {$column} {$range} (" . $this->getWhereValue($values) . ")"; + $this->_wheres[] = count($this->_wheres) ? $delimeter . " " . $query : $query; + } + final public function whereNotIn(string $column, array $values, string $delimeter = "AND", $range = "NOT IN") + { + $this->whereIn($column, $values, $delimeter, $range); } final public function execute(string $query): bool|PDOStatement @@ -141,7 +153,7 @@ abstract class Model $stmt = $this->getConnect()->prepare($query); $stmt->execute(); if ($this->_reset) { - $this->reset(); + $this->init(); } return $stmt; } @@ -149,12 +161,12 @@ abstract class Model //CURD문 final protected function create_process(mixed $columns, mixed $values = null): bool|PDOStatement { - $query = sprintf("INSERT INTO %s VALUES(%s) %s", $this->getTable(), $this->makeWhere($columns, $values), $this->getWhere()); + $query = sprintf("INSERT INTO %s VALUES(%s) %s", $this->getTable(), $this->getWhereColumn($columns, $values), $this->getWhere()); return $this->execute($query); } // final protected function modify_process(mixed $columns, mixed $values = null): bool|PDOStatement { - $query = sprintf("UPDATE %s SET %s %s", $this->getTable(), $this->makeWhere($columns, $values), $this->getWhere()); + $query = sprintf("UPDATE %s SET %s %s", $this->getTable(), $this->getWhereColumn($columns, $values), $this->getWhere()); return $this->execute($query); } // final protected function delete_process(): bool|PDOStatement @@ -164,16 +176,25 @@ abstract class Model } // public function select(mixed $columns = "*"): void { - $columns = is_array($columns) ? implode(",", $columns) : $columns; + $columns = is_array($columns) ? implode(",", $columns) : $columns; $this->_querys["SELECT"] = "SELECT {$columns}"; } - final public function join(string $table, mixed $match = null, $type = ""): void + //join_type : LEFT, RIGHT, INNER, OUTER + final public function join(string $table, mixed $match = null, $join_type = ""): void { - $this->_querys["JOIN"] = " {$type} JOIN {$table} ON {$match}"; + $this->_querys['JOIN'][] = " {$join_type} JOIN {$table} ON {$match}"; } - final public function orderBy(string $column, string $direction = "DESC"): void + final public function orderBy(mixed $columns, string $default_direction = ""): void { - $this->_querys["ORDERBY"] = " ORDER BY {$column} {$direction}"; + $orderBy = ""; + if (is_array($columns)) { + foreach ($columns as $column => $direction) { + $this->orderBy($column, $direction ?? $default_direction); + } + } else { + $columns = "{$columns} {$default_direction}"; + } + $this->_querys['ORDERBY'][] = $orderBy; } final public function limit(int $start, int $offset = 0): void { @@ -182,24 +203,27 @@ abstract class Model } //Result - private function makeResultQuery(string $head, string $tail = ""): string + private function getResultQuery(string $head, string $tail = ""): string { - return "{$head} FROM {$this->getTable()} {$this->_querys['JOIN']} {$this->getWhere()} {$tail}"; + $join = count($this->_querys['JOIN']) ? implode(",", $this->_querys['JOIN']) : ""; + $where = $this->getWhere(); + $orderby = count($this->_querys['ORDERBY']) ? implode(",", $this->_querys['ORDERBY']) : ""; + return "{$head} FROM {$this->getTable()} {$join} {$where} {$tail} {$orderby} {$this->_querys['LIMIT']}"; } public function getResult($mode = PDO::FETCH_ASSOC): mixed { - $stmt = $this->execute($this->makeResultQuery($this->_querys['SELECT'], "{$this->_querys['ORDERBY']} {$this->_querys['LIMIT']}")); + $stmt = $this->execute($this->getResultQuery($this->_querys['SELECT'])); return $stmt->fetch($mode); } public function getResults($mode = PDO::FETCH_ASSOC): mixed { - $stmt = $this->execute($this->makeResultQuery($this->_querys['SELECT'], "{$this->_querys['ORDERBY']} {$this->_querys['LIMIT']}")); + $stmt = $this->execute($this->getResultQuery($this->_querys['SELECT'])); return $stmt->fetchAll($mode); } final public function countAllResults(string $column = "*", $reset = true): int { $this->_reset = $reset; - $stmt = $this->execute($this->makeResultQuery("SELECT COUNT({$column})")); + $stmt = $this->execute($this->getResultQuery("SELECT COUNT({$column})")); $count = $stmt->fetchColumn(0); $this->_reset = true; return $count; diff --git a/extdbms/lib/Entities/DefenceEntity.php b/extdbms/lib/Entities/DefenceEntity.php new file mode 100644 index 0000000..44cef5d --- /dev/null +++ b/extdbms/lib/Entities/DefenceEntity.php @@ -0,0 +1,34 @@ +getTitle(); + } // + public function getParent(): string + { + return $this->parents; + } // + public function getChild(): string + { + return $this->child; + } // + public function getRealAddress(): string + { + return $this->real_address; + } // +} //Class diff --git a/extdbms/lib/Models/DefenceModel.php b/extdbms/lib/Models/DefenceModel.php new file mode 100644 index 0000000..2c1dd39 --- /dev/null +++ b/extdbms/lib/Models/DefenceModel.php @@ -0,0 +1,17 @@ +getClassName(); + } + public function getModelClass(): string + { + return Model::class; + } + public function getEntityClass(): string + { + return Entity::class; + } + + public function getMKList(mixed $zone = null): array + { + if ($zone !== null) { + $this->getModel()->setWhere(['zone' => $zone]); + } + return $this->getModel()->orderBy(['zone' => 'asc', 'parent' => 'asc', 'child' => 'asc']); + } +} diff --git a/extdbms/lib/Views/dbms/defence/mk.php b/extdbms/lib/Views/dbms/defence/mk.php new file mode 100644 index 0000000..b4dd078 --- /dev/null +++ b/extdbms/lib/Views/dbms/defence/mk.php @@ -0,0 +1,138 @@ + + +