From 7c96e7225770839da9a8675f42ce8b546828382b Mon Sep 17 00:00:00 2001 From: bim-g Date: Mon, 8 Jan 2024 14:44:19 +0200 Subject: [PATCH] [FIX] db query method to display usable data . --- src/Core/Orm/DB.php | 39 ++++++++++++------------- src/Core/Orm/Traits/QueryExecuter.php | 42 ++++++++++++++++----------- 2 files changed, 44 insertions(+), 37 deletions(-) diff --git a/src/Core/Orm/DB.php b/src/Core/Orm/DB.php index 2e056c2..1174f48 100644 --- a/src/Core/Orm/DB.php +++ b/src/Core/Orm/DB.php @@ -32,15 +32,15 @@ class DB extends DBConfig /** * @var string|null */ - private string $_error; + private string $request_error; /** * @var array */ - private array $_results; + private array $request_results; /** * @var int */ - private int $lastID; + private int $request_lastID; /** * @var PDO */ @@ -48,7 +48,7 @@ class DB extends DBConfig /** * @var int */ - private int $_count; + private int $result_count; /** * @var false|mixed */ @@ -106,11 +106,10 @@ private function select_option(string $table_name, string $action = null): DBSel */ private function initialisation() { - $this->_results = []; - $this->_count = 0; - $this->lastID = 0; - $this->_error = ''; -// self::$_instance = null; + $this->request_results = []; + $this->result_count = 0; + $this->request_lastID = -1; + $this->request_error = ''; } /** @@ -165,8 +164,8 @@ public function update(string $table): DBUpdate */ public function lastId(): int { - if ($this->queryResult && method_exists($this->queryResult, 'lastId')) $this->lastID = $this->queryResult->lastId(); - return $this->lastID; + if ($this->queryResult && method_exists($this->queryResult, 'lastId')) $this->request_lastID = $this->queryResult->lastId(); + return $this->request_lastID; } /** @@ -174,8 +173,8 @@ public function lastId(): int */ public function error(): string { - if (isset($this->queryResult)) $this->_error = $this->queryResult->error(); - return $this->_error; + if (isset($this->queryResult)) $this->request_error = $this->queryResult->error(); + return $this->request_error; } /** @@ -183,7 +182,7 @@ public function error(): string */ public function rowCount(): int { - return $this->queryResult->count() ?? $this->_count; + return $this->queryResult ? $this->queryResult->count() : $this->result_count; } /** @@ -253,7 +252,7 @@ protected function get_db_engine_table(string $engine = 'MyISAM'): array */ public function result(): array { - return $this->_results; + return $this->request_results; } /** @@ -263,11 +262,11 @@ public function result(): array */ public function query(string $sql, array $params = []): DB { - $q = $this->executeQuery($this->pdoObject, $sql, $params); - $this->_results = $q['result']; - $this->_count = $q['count']; - $this->_error = $q['error']; - $this->lastID = $q['lastID']; + $q = $this->executeQuery($this->pdoObject, $sql, $params, -1, true); + $this->request_results = $q['result']; + $this->result_count = $q['count']; + $this->request_error = $q['error']; + $this->request_lastID = $q['lastID']; return $this; } diff --git a/src/Core/Orm/Traits/QueryExecuter.php b/src/Core/Orm/Traits/QueryExecuter.php index 9ac6f6a..a739210 100644 --- a/src/Core/Orm/Traits/QueryExecuter.php +++ b/src/Core/Orm/Traits/QueryExecuter.php @@ -17,12 +17,15 @@ trait QueryExecuter { /** - * @param PDO $pdo - * @param string $sql - * @param array $params + * Execute sql request pass by the user + * @param PDO $pdo pdo object + * @param string $sql query string + * @param array $params for prepare request provide params value + * @param int $last_id in case of loop request provide id of the previous request + * @param bool $isQuery define if we use the query method directly * @return array */ - protected function executeQuery(PDO $pdo, string $sql, array $params = [], int $last_id = -1): array + protected function executeQuery(PDO $pdo, string $sql, array $params = [], int $last_id = -1,bool $isQuery = false): array { try { $data_result = [ @@ -32,7 +35,8 @@ protected function executeQuery(PDO $pdo, string $sql, array $params = [], int $ 'error' => "", ]; $sql_string = explode(' ', strtolower($sql)); - if ($sql_string[0] == 'select' && !$this->isCount) { + $fetchObject = $isQuery || (isset($this->isCount) && $this->isCount); + if ($sql_string[0] == 'select' && $fetchObject) { $pdo->setAttribute(PDO::ATTR_FETCH_TABLE_NAMES, true); } @@ -51,19 +55,23 @@ protected function executeQuery(PDO $pdo, string $sql, array $params = [], int $ switch ($sql_string[0]) { case 'select' : - if ($this->isCount) { - $count_result = $query->fetchAll(PDO::FETCH_OBJ); - if (isset($count_result[0]->{'.count'})) { - array_map(function ($item) { - if ($item->{'.count'}) { - $item->count = $item->{'.count'}; - unset($item->{'.count'}); - } - return $item; - }, $count_result); + if ($fetchObject) { + $fetch_result = $query->fetchAll(PDO::FETCH_OBJ); + if ( $isQuery ){ + $data_result['count'] = $query->columnCount(); + } else { + if (isset($fetch_result[0]->{'.count'})) { + array_map(function ($item) { + if ($item->{'.count'}) { + $item->count = $item->{'.count'}; + unset($item->{'.count'}); + } + return $item; + }, $fetch_result); + } + $data_result['count'] = $fetch_result[0]->count; } - $data_result['result'] = $count_result; - $data_result['count'] = $count_result[0]->count; + $data_result['result'] = $fetch_result; } else { $data_result['result'] = $query->fetchAll(PDO::FETCH_GROUP); $data_result['count'] = $query->columnCount();