2017-01-21 13:58:53 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2018-01-14 01:48:50 +01:00
|
|
|
/** @var CapsuleManager */
|
|
|
|
protected static $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
|
|
|
|
*
|
|
|
|
* @param array $bindings
|
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) {
|
|
|
|
$return[$key] = (array)$value;
|
|
|
|
}
|
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
|
|
|
*
|
2017-07-28 20:11:09 +02:00
|
|
|
* @param array $bindings
|
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
|
2018-08-29 18:08:45 +02:00
|
|
|
$result = (array)$result;
|
|
|
|
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
|
|
|
*
|
|
|
|
* @param array $bindings
|
|
|
|
*/
|
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
|
|
|
*
|
|
|
|
* @param array $bindings
|
|
|
|
*/
|
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
|
|
|
|
*
|
|
|
|
* @param array $bindings
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
}
|