Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 49 additions & 4 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
class Database extends QueryBuilder
{

public const ENTITY = 0;
public const ASSOC = 1;
public const ARRAY = 2;
public const OBJECT = 3;
public const LAZY = 4;

private PDO $_pdo;

private static PDO $_globalPDO;
Expand Down Expand Up @@ -52,6 +58,7 @@ class Database extends QueryBuilder
'writable' => true,
'deletable' => true,
'updatable' => true,
'return' => null,
];

private Result $_last;
Expand Down Expand Up @@ -107,12 +114,33 @@ final public function getPDO(): PDO
}
if(!isset($this->_pdo)){
try {
$this->_pdo = new PDO($this->_credentials['dsn'], $this->_credentials['username'], $this->_credentials['password'], [
$options = [
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_BOTH
]);
];
switch ($this->_credentials['return']) {
case null:
case self::ARRAY:
$options[PDO::ATTR_DEFAULT_FETCH_MODE] = PDO::FETCH_BOTH;
break;
case self::ASSOC:
$options[PDO::ATTR_DEFAULT_FETCH_MODE] = PDO::FETCH_ASSOC;
break;
case self::ENTITY:
$options[PDO::ATTR_DEFAULT_FETCH_MODE] = PDO::FETCH_CLASS;
break;
case self::OBJECT:
$options[PDO::ATTR_DEFAULT_FETCH_MODE] = PDO::FETCH_OBJ;
break;
case self::LAZY:
$options[PDO::ATTR_DEFAULT_FETCH_MODE] = PDO::FETCH_LAZY;
break;
default:
$options[PDO::ATTR_DEFAULT_FETCH_MODE] = PDO::FETCH_BOTH;
}

$this->_pdo = new PDO($this->_credentials['dsn'], $this->_credentials['username'], $this->_credentials['password'], $options);
if(!empty($this->_credentials['charset'])){
if(!empty($this->_credentials['collation'])) {
$this->_pdo->exec("SET NAMES '" . $this->_credentials['charset'] . "' COLLATE '" . $this->_credentials['collation'] . "'");
Expand Down Expand Up @@ -306,7 +334,24 @@ final public function query(string $sqlQuery, array $parameters = []): Result
$this->_errors[] = $errorCode . ' - ' . $errorInfo[2];
}
}
return $this->_last = new Result($stmt);
$this->_last = new Result($stmt);
if($this->_credentials['return'] === null){
return $this->_last;
}
switch ($this->_credentials['return']) {
case self::ASSOC:
return $this->_last->asAssoc();
case self::ARRAY:
return $this->_last->asArray();
case self::ENTITY:
return $this->_last->asEntity();
case self::OBJECT:
return $this->_last->asObject();
case self::LAZY:
return $this->_last->asLazy();
default:
return $this->_last;
}
} catch (\Exception $e) {
$message = $e->getMessage();
if(($this->_credentials['debug'] ?? false) === TRUE){
Expand Down
11 changes: 9 additions & 2 deletions src/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ public function query(): string
return $this->query;
}

public function toEntity(string $entityClass = Entity::class): array|object|null
/**
* @param string $entityClass
* @return array|object|null
*/
public function toEntity(string $entityClass = Entity::class)
{
if($this->num_rows === 0){
return null;
Expand Down Expand Up @@ -106,7 +110,10 @@ public function asArray(): self
return $this;
}

public function toObject(): object|array|null
/**
* @return object|array|null
*/
public function toObject()
{
if($this->num_rows === 0){
return null;
Expand Down