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;
|
|
|
|
|
|
|
|
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
|
|
|
*
|
2018-01-14 01:48:50 +01:00
|
|
|
* @param CapsuleManager $dbManager
|
2017-01-21 13:58:53 +01:00
|
|
|
*/
|
2018-01-14 01:48:50 +01:00
|
|
|
public static function setDbManager($dbManager)
|
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 string $query
|
|
|
|
* @param array $bindings
|
2017-12-25 23:12:52 +01:00
|
|
|
* @return array[]
|
2017-01-21 13:58:53 +01:00
|
|
|
*/
|
|
|
|
public static function select($query, array $bindings = [])
|
|
|
|
{
|
2018-01-14 13:32:57 +01:00
|
|
|
$return = self::connection()->select($query, $bindings);
|
|
|
|
|
|
|
|
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 string $query
|
|
|
|
* @param array $bindings
|
|
|
|
* @return array|null
|
|
|
|
*/
|
|
|
|
public static function selectOne($query, array $bindings = [])
|
|
|
|
{
|
2018-01-14 13:32:57 +01:00
|
|
|
$result = self::connection()->selectOne($query, $bindings);
|
2017-08-30 00:07:01 +02:00
|
|
|
|
|
|
|
if (empty($result)) {
|
2017-07-28 20:11:09 +02:00
|
|
|
return null;
|
|
|
|
}
|
2017-08-30 00:07:01 +02:00
|
|
|
|
2018-01-14 13:32:57 +01:00
|
|
|
return (array)$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 string $query
|
|
|
|
* @param array $bindings
|
2018-01-14 13:32:57 +01:00
|
|
|
* @return bool
|
2017-01-21 13:58:53 +01:00
|
|
|
*/
|
|
|
|
public static function insert($query, array $bindings = [])
|
|
|
|
{
|
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 string $query
|
|
|
|
* @param array $bindings
|
2017-07-23 11:46:54 +02:00
|
|
|
* @return int
|
2017-01-21 13:58:53 +01:00
|
|
|
*/
|
|
|
|
public static function update($query, array $bindings = [])
|
|
|
|
{
|
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 string $query
|
|
|
|
* @param array $bindings
|
2017-07-28 18:50:00 +02:00
|
|
|
* @return int
|
2017-01-21 13:58:53 +01:00
|
|
|
*/
|
|
|
|
public static function delete($query, array $bindings = [])
|
|
|
|
{
|
2018-01-14 13:32:57 +01:00
|
|
|
return self::connection()->delete($query, $bindings);
|
2017-01-21 13:58:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-14 13:32:57 +01:00
|
|
|
* @return DatabaseConnection
|
2017-01-21 13:58:53 +01:00
|
|
|
*/
|
2018-01-14 13:32:57 +01:00
|
|
|
public static function connection()
|
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
|
|
|
|
*
|
|
|
|
* @return PDO
|
|
|
|
*/
|
|
|
|
public static function getPdo()
|
|
|
|
{
|
2018-01-14 13:32:57 +01:00
|
|
|
return self::connection()->getPdo();
|
2017-01-21 13:58:53 +01:00
|
|
|
}
|
|
|
|
}
|