2017-01-21 13:58:53 +01:00
|
|
|
<?php
|
|
|
|
|
2023-02-03 20:41:59 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2017-01-21 13:58:53 +01:00
|
|
|
namespace Engelsystem\Database;
|
|
|
|
|
2018-01-14 01:48:50 +01:00
|
|
|
use Illuminate\Database\Capsule\Manager as CapsuleManager;
|
2018-01-14 13:32:57 +01:00
|
|
|
use Illuminate\Database\Connection as DatabaseConnection;
|
2017-01-21 13:58:53 +01:00
|
|
|
use PDO;
|
|
|
|
|
2018-09-16 00:58:25 +02:00
|
|
|
/** @deprecated */
|
2017-01-21 13:58:53 +01:00
|
|
|
class Db
|
|
|
|
{
|
2022-12-15 19:50:56 +01:00
|
|
|
protected static CapsuleManager $dbManager;
|
2017-01-21 13:58:53 +01:00
|
|
|
|
|
|
|
/**
|
2018-01-14 01:48:50 +01:00
|
|
|
* Set the database connection manager
|
2017-01-21 13:58:53 +01:00
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public static function setDbManager(CapsuleManager $dbManager): void
|
2017-01-21 13:58:53 +01:00
|
|
|
{
|
2018-01-14 01:48:50 +01:00
|
|
|
self::$dbManager = $dbManager;
|
2017-01-21 13:58:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run a select query
|
|
|
|
*
|
2017-12-25 23:12:52 +01:00
|
|
|
* @return array[]
|
2017-01-21 13:58:53 +01:00
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public static function select(string $query, array $bindings = []): array
|
2017-01-21 13:58:53 +01:00
|
|
|
{
|
2018-01-14 13:32:57 +01:00
|
|
|
$return = self::connection()->select($query, $bindings);
|
|
|
|
|
2018-01-14 17:47:26 +01:00
|
|
|
// @TODO: Remove type casting
|
2018-01-14 13:32:57 +01:00
|
|
|
foreach ($return as $key => $value) {
|
2022-12-25 11:59:45 +01:00
|
|
|
$return[$key] = (array) $value;
|
2018-01-14 13:32:57 +01:00
|
|
|
}
|
2017-01-21 13:58:53 +01:00
|
|
|
|
2018-01-14 13:32:57 +01:00
|
|
|
return $return;
|
2017-01-21 13:58:53 +01:00
|
|
|
}
|
2017-08-30 00:07:01 +02:00
|
|
|
|
2017-07-28 20:11:09 +02:00
|
|
|
/**
|
|
|
|
* Run a select query and return only the first result or null if no result is found.
|
2017-08-30 00:07:01 +02:00
|
|
|
*
|
2018-08-29 18:08:45 +02:00
|
|
|
* @return array|null
|
2017-07-28 20:11:09 +02:00
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public static function selectOne(string $query, array $bindings = []): ?array
|
2017-07-28 20:11:09 +02:00
|
|
|
{
|
2018-01-14 13:32:57 +01:00
|
|
|
$result = self::connection()->selectOne($query, $bindings);
|
2017-08-30 00:07:01 +02:00
|
|
|
|
2018-01-14 17:47:26 +01:00
|
|
|
// @TODO: remove typecast
|
2022-12-25 11:59:45 +01:00
|
|
|
$result = (array) $result;
|
2018-08-29 18:08:45 +02:00
|
|
|
if (empty($result)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
2017-07-28 20:11:09 +02:00
|
|
|
}
|
2017-01-21 13:58:53 +01:00
|
|
|
|
|
|
|
/**
|
2017-07-19 21:28:26 +02:00
|
|
|
* Run an insert query
|
2017-01-21 13:58:53 +01:00
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public static function insert(string $query, array $bindings = []): bool
|
2017-01-21 13:58:53 +01:00
|
|
|
{
|
2018-01-14 13:32:57 +01:00
|
|
|
return self::connection()->insert($query, $bindings);
|
2017-01-21 13:58:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-07-19 21:28:26 +02:00
|
|
|
* Run an update query
|
2017-01-21 13:58:53 +01:00
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public static function update(string $query, array $bindings = []): int
|
2017-01-21 13:58:53 +01:00
|
|
|
{
|
2018-01-14 13:32:57 +01:00
|
|
|
return self::connection()->update($query, $bindings);
|
2017-01-21 13:58:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run a delete query
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public static function delete(string $query, array $bindings = []): int
|
2017-01-21 13:58:53 +01:00
|
|
|
{
|
2018-01-14 13:32:57 +01:00
|
|
|
return self::connection()->delete($query, $bindings);
|
2017-01-21 13:58:53 +01:00
|
|
|
}
|
|
|
|
|
2022-12-14 19:15:20 +01:00
|
|
|
public static function connection(): DatabaseConnection
|
2017-01-21 13:58:53 +01:00
|
|
|
{
|
2018-01-14 13:32:57 +01:00
|
|
|
return self::$dbManager->getConnection();
|
2017-01-21 13:58:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the PDO instance
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public static function getPdo(): PDO
|
2017-01-21 13:58:53 +01:00
|
|
|
{
|
2018-01-14 13:32:57 +01:00
|
|
|
return self::connection()->getPdo();
|
2017-01-21 13:58:53 +01:00
|
|
|
}
|
|
|
|
}
|