Skip to content

Commit

Permalink
Merge pull request #164 from kivudesign/version_boss
Browse files Browse the repository at this point in the history
[FIX] db query method to display usable data .
  • Loading branch information
bim-g committed Jan 8, 2024
2 parents 2bb274d + 7c96e72 commit 70617ee
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 37 deletions.
39 changes: 19 additions & 20 deletions src/Core/Orm/DB.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,23 @@ 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
*/
private PDO $pdoObject;
/**
* @var int
*/
private int $_count;
private int $result_count;
/**
* @var false|mixed
*/
Expand Down Expand Up @@ -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 = '';
}

/**
Expand Down Expand Up @@ -165,25 +164,25 @@ 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;
}

/**
* @return string|null
*/
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;
}

/**
* @return int
*/
public function rowCount(): int
{
return $this->queryResult->count() ?? $this->_count;
return $this->queryResult ? $this->queryResult->count() : $this->result_count;
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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;
}

Expand Down
42 changes: 25 additions & 17 deletions src/Core/Orm/Traits/QueryExecuter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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);
}

Expand All @@ -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();
Expand Down

0 comments on commit 70617ee

Please sign in to comment.