diff options
Diffstat (limited to 'src/Database/Db.php')
-rw-r--r-- | src/Database/Db.php | 131 |
1 files changed, 27 insertions, 104 deletions
diff --git a/src/Database/Db.php b/src/Database/Db.php index 114bd8fc..f34d1564 100644 --- a/src/Database/Db.php +++ b/src/Database/Db.php @@ -2,68 +2,23 @@ namespace Engelsystem\Database; +use Illuminate\Database\Capsule\Manager as CapsuleManager; +use Illuminate\Database\Connection as DatabaseConnection; use PDO; -use PDOException; -use PDOStatement; class Db { - /** @var PDO */ - protected static $db; - - /** @var PDOStatement */ - protected static $stm = null; - - /** @var bool */ - protected static $lastStatus = true; - - /** - * Connect to database - * - * @param string $dsn - * @param string $username - * @param string $password - * @param array $options - * @return bool - */ - public static function connect($dsn, $username = null, $password = null, $options = []) - { - try { - self::$db = new PDO($dsn, $username, $password, $options); - } catch (PDOException $e) { - return false; - } - - return true; - } - - /** - * Run a prepared query - * - * @param string $query - * @param array $bindings - * @return PDOStatement - */ - public static function query($query, array $bindings = []) - { - self::$stm = self::$db->prepare($query); - self::$lastStatus = self::$stm->execute($bindings); - - return self::$stm; - } + /** @var CapsuleManager */ + protected static $dbManager; /** - * Run a sql query + * Set the database connection manager * - * @param string $query - * @return bool + * @param CapsuleManager $dbManager */ - public static function unprepared($query) + public static function setDbManager($dbManager) { - self::$stm = self::$db->query($query); - self::$lastStatus = (self::$stm instanceof PDOStatement); - - return self::$lastStatus; + self::$dbManager = $dbManager; } /** @@ -75,9 +30,14 @@ class Db */ public static function select($query, array $bindings = []) { - self::query($query, $bindings); + $return = self::connection()->select($query, $bindings); - return self::$stm->fetchAll(PDO::FETCH_ASSOC); + // @TODO: Remove type casting + foreach ($return as $key => $value) { + $return[$key] = (array)$value; + } + + return $return; } /** @@ -85,17 +45,14 @@ class Db * * @param string $query * @param array $bindings - * @return array|null + * @return array */ public static function selectOne($query, array $bindings = []) { - $result = self::select($query, $bindings); + $result = self::connection()->selectOne($query, $bindings); - if (empty($result)) { - return null; - } - - return array_shift($result); + // @TODO: remove typecast + return (array)$result; } /** @@ -103,13 +60,11 @@ class Db * * @param string $query * @param array $bindings - * @return int Row count + * @return bool */ public static function insert($query, array $bindings = []) { - self::query($query, $bindings); - - return self::$stm->rowCount(); + return self::connection()->insert($query, $bindings); } /** @@ -121,9 +76,7 @@ class Db */ public static function update($query, array $bindings = []) { - self::query($query, $bindings); - - return self::$stm->rowCount(); + return self::connection()->update($query, $bindings); } /** @@ -135,37 +88,15 @@ class Db */ public static function delete($query, array $bindings = []) { - self::query($query, $bindings); - - return self::$stm->rowCount(); + return self::connection()->delete($query, $bindings); } /** - * Run a single statement - * - * @param string $query - * @param array $bindings - * @return bool + * @return DatabaseConnection */ - public static function statement($query, array $bindings = []) + public static function connection() { - self::query($query, $bindings); - - return self::$lastStatus; - } - - /** - * Returns the last error - * - * @return array - */ - public static function getError() - { - if (!self::$stm instanceof PDOStatement) { - return [-1, null, null]; - } - - return self::$stm->errorInfo(); + return self::$dbManager->getConnection(); } /** @@ -175,14 +106,6 @@ class Db */ public static function getPdo() { - return self::$db; - } - - /** - * @return PDOStatement|false|null - */ - public static function getStm() - { - return self::$stm; + return self::connection()->getPdo(); } } |