diff --git a/src/Database/DatabaseStatement.php b/src/Database/DatabaseStatement.php index 7d3903be..d446d822 100644 --- a/src/Database/DatabaseStatement.php +++ b/src/Database/DatabaseStatement.php @@ -8,6 +8,7 @@ class DatabaseStatement { public $pdo; public $stmt; private $isQuery; + private $hasExecuted = false; public function __construct(PDOStatement $stmt, PDO $pdo, bool $isQuery) { $this->stmt = $stmt; @@ -21,13 +22,26 @@ class DatabaseStatement { } public function execute(array $params = []): bool { + if($this->hasExecuted) + return true; + $this->hasExecuted = true; + return count($params) ? $this->stmt->execute($params) : $this->stmt->execute(); } public function executeGetId(array $params = []): int { + if($this->hasExecuted) + return true; + $this->hasExecuted = true; + return $this->execute($params) ? $this->pdo->lastInsertId() : 0; } + public function reset(): DatabaseStatement { + $this->hasExecuted = false; + return $this; + } + public function fetch($default = []) { $out = $this->isQuery || $this->execute() ? $this->stmt->fetch(PDO::FETCH_ASSOC) : false; return $out ? $out : $default; @@ -42,4 +56,24 @@ class DatabaseStatement { $out = $this->isQuery || $this->execute() ? $this->stmt->fetchColumn($num) : false; return $out ? $out : $default; } + + public function fetchObject(string $className = 'stdClass', ?array $args = null, $default = null) { + $out = false; + + if($this->isQuery || $this->execute()) { + $out = $args === null ? $this->fetchObject($className) : $this->fetchObject($className, $args); + } + + return $out !== false ? $out : $default; + } + + public function fetchObjects(string $className = 'stdClass', ?array $args = null): array { + $objects = []; + + while(($object = $this->fetchObject($className, $args, false)) !== false) { + $objects[] = $object; + } + + return $objects; + } }