2017-01-21 13:58:53 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Engelsystem\Database;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run a sql query
|
|
|
|
*
|
|
|
|
* @param string $query
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function unprepared($query)
|
|
|
|
{
|
|
|
|
self::$stm = self::$db->query($query);
|
|
|
|
self::$lastStatus = (self::$stm instanceof PDOStatement);
|
|
|
|
|
|
|
|
return self::$lastStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run a select query
|
|
|
|
*
|
|
|
|
* @param string $query
|
|
|
|
* @param array $bindings
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function select($query, array $bindings = [])
|
|
|
|
{
|
|
|
|
self::query($query, $bindings);
|
|
|
|
|
|
|
|
return self::$stm->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
2017-07-23 11:46:54 +02:00
|
|
|
* @return int Row count
|
2017-01-21 13:58:53 +01:00
|
|
|
*/
|
|
|
|
public static function insert($query, array $bindings = [])
|
|
|
|
{
|
|
|
|
self::query($query, $bindings);
|
|
|
|
|
2017-07-23 11:46:54 +02:00
|
|
|
return self::$stm->rowCount();
|
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 = [])
|
|
|
|
{
|
|
|
|
self::query($query, $bindings);
|
|
|
|
|
2017-07-23 11:46:54 +02:00
|
|
|
return self::$stm->rowCount();
|
2017-01-21 13:58:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run a delete query
|
|
|
|
*
|
|
|
|
* @param string $query
|
|
|
|
* @param array $bindings
|
2017-07-19 21:28:26 +02:00
|
|
|
* @return int|bool
|
2017-01-21 13:58:53 +01:00
|
|
|
*/
|
|
|
|
public static function delete($query, array $bindings = [])
|
|
|
|
{
|
|
|
|
self::query($query, $bindings);
|
|
|
|
|
2017-07-19 21:28:26 +02:00
|
|
|
return (self::$lastStatus ? self::$stm->rowCount() : false);
|
2017-01-21 13:58:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run a single statement
|
|
|
|
*
|
|
|
|
* @param string $query
|
|
|
|
* @param array $bindings
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function statement($query, array $bindings = [])
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the PDO instance
|
|
|
|
*
|
|
|
|
* @return PDO
|
|
|
|
*/
|
|
|
|
public static function getPdo()
|
|
|
|
{
|
|
|
|
return self::$db;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return PDOStatement|false|null
|
|
|
|
*/
|
|
|
|
public static function getStm()
|
|
|
|
{
|
|
|
|
return self::$stm;
|
|
|
|
}
|
|
|
|
}
|