database: integrated illuminate/database do Db class, removed unused methods
This commit is contained in:
parent
d4b36e9bad
commit
0e8cc2f0a7
|
@ -21,7 +21,7 @@ function EventConfig()
|
||||||
* @param int $event_end_date
|
* @param int $event_end_date
|
||||||
* @param int $teardown_end_date
|
* @param int $teardown_end_date
|
||||||
* @param string $event_welcome_msg
|
* @param string $event_welcome_msg
|
||||||
* @return int Rows updated
|
* @return bool
|
||||||
*/
|
*/
|
||||||
function EventConfig_update(
|
function EventConfig_update(
|
||||||
$event_name,
|
$event_name,
|
||||||
|
@ -54,7 +54,7 @@ function EventConfig_update(
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return DB::update('
|
return (bool)DB::update('
|
||||||
UPDATE `EventConfig` SET
|
UPDATE `EventConfig` SET
|
||||||
`event_name` = ?,
|
`event_name` = ?,
|
||||||
`buildup_start_date` = ?,
|
`buildup_start_date` = ?,
|
||||||
|
|
|
@ -58,5 +58,5 @@ function LogEntries_filter($keyword)
|
||||||
*/
|
*/
|
||||||
function LogEntries_clear_all()
|
function LogEntries_clear_all()
|
||||||
{
|
{
|
||||||
return DB::statement('TRUNCATE `LogEntries`');
|
return DB::connection()->statement('TRUNCATE `LogEntries`');
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,20 +3,14 @@
|
||||||
namespace Engelsystem\Database;
|
namespace Engelsystem\Database;
|
||||||
|
|
||||||
use Illuminate\Database\Capsule\Manager as CapsuleManager;
|
use Illuminate\Database\Capsule\Manager as CapsuleManager;
|
||||||
|
use Illuminate\Database\Connection as DatabaseConnection;
|
||||||
use PDO;
|
use PDO;
|
||||||
use PDOStatement;
|
|
||||||
|
|
||||||
class Db
|
class Db
|
||||||
{
|
{
|
||||||
/** @var CapsuleManager */
|
/** @var CapsuleManager */
|
||||||
protected static $dbManager;
|
protected static $dbManager;
|
||||||
|
|
||||||
/** @var PDOStatement */
|
|
||||||
protected static $stm = null;
|
|
||||||
|
|
||||||
/** @var bool */
|
|
||||||
protected static $lastStatus = true;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the database connection manager
|
* Set the database connection manager
|
||||||
*
|
*
|
||||||
|
@ -27,35 +21,6 @@ class Db
|
||||||
self::$dbManager = $dbManager;
|
self::$dbManager = $dbManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Run a prepared query
|
|
||||||
*
|
|
||||||
* @param string $query
|
|
||||||
* @param array $bindings
|
|
||||||
* @return PDOStatement
|
|
||||||
*/
|
|
||||||
public static function query($query, array $bindings = [])
|
|
||||||
{
|
|
||||||
self::$stm = self::getPdo()->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::getPdo()->query($query);
|
|
||||||
self::$lastStatus = (self::$stm instanceof PDOStatement);
|
|
||||||
|
|
||||||
return self::$lastStatus;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Run a select query
|
* Run a select query
|
||||||
*
|
*
|
||||||
|
@ -65,9 +30,13 @@ class Db
|
||||||
*/
|
*/
|
||||||
public static function select($query, array $bindings = [])
|
public static function select($query, array $bindings = [])
|
||||||
{
|
{
|
||||||
self::query($query, $bindings);
|
$return = self::connection()->select($query, $bindings);
|
||||||
|
|
||||||
return self::$stm->fetchAll(PDO::FETCH_ASSOC);
|
foreach ($return as $key => $value) {
|
||||||
|
$return[$key] = (array)$value;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -79,13 +48,13 @@ class Db
|
||||||
*/
|
*/
|
||||||
public static function selectOne($query, array $bindings = [])
|
public static function selectOne($query, array $bindings = [])
|
||||||
{
|
{
|
||||||
$result = self::select($query, $bindings);
|
$result = self::connection()->selectOne($query, $bindings);
|
||||||
|
|
||||||
if (empty($result)) {
|
if (empty($result)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return array_shift($result);
|
return (array)$result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -93,13 +62,11 @@ class Db
|
||||||
*
|
*
|
||||||
* @param string $query
|
* @param string $query
|
||||||
* @param array $bindings
|
* @param array $bindings
|
||||||
* @return int Row count
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public static function insert($query, array $bindings = [])
|
public static function insert($query, array $bindings = [])
|
||||||
{
|
{
|
||||||
self::query($query, $bindings);
|
return self::connection()->insert($query, $bindings);
|
||||||
|
|
||||||
return self::$stm->rowCount();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -111,9 +78,7 @@ class Db
|
||||||
*/
|
*/
|
||||||
public static function update($query, array $bindings = [])
|
public static function update($query, array $bindings = [])
|
||||||
{
|
{
|
||||||
self::query($query, $bindings);
|
return self::connection()->update($query, $bindings);
|
||||||
|
|
||||||
return self::$stm->rowCount();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -125,37 +90,15 @@ class Db
|
||||||
*/
|
*/
|
||||||
public static function delete($query, array $bindings = [])
|
public static function delete($query, array $bindings = [])
|
||||||
{
|
{
|
||||||
self::query($query, $bindings);
|
return self::connection()->delete($query, $bindings);
|
||||||
|
|
||||||
return self::$stm->rowCount();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Run a single statement
|
* @return DatabaseConnection
|
||||||
*
|
|
||||||
* @param string $query
|
|
||||||
* @param array $bindings
|
|
||||||
* @return bool
|
|
||||||
*/
|
*/
|
||||||
public static function statement($query, array $bindings = [])
|
public static function connection()
|
||||||
{
|
{
|
||||||
self::query($query, $bindings);
|
return self::$dbManager->getConnection();
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -165,14 +108,6 @@ class Db
|
||||||
*/
|
*/
|
||||||
public static function getPdo()
|
public static function getPdo()
|
||||||
{
|
{
|
||||||
return self::$dbManager->getConnection()->getPdo();
|
return self::connection()->getPdo();
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return PDOStatement|false|null
|
|
||||||
*/
|
|
||||||
public static function getStm()
|
|
||||||
{
|
|
||||||
return self::$stm;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,16 +6,14 @@ use Engelsystem\Database\Db;
|
||||||
use Illuminate\Database\Capsule\Manager as CapsuleManager;
|
use Illuminate\Database\Capsule\Manager as CapsuleManager;
|
||||||
use Illuminate\Database\Connection as DatabaseConnection;
|
use Illuminate\Database\Connection as DatabaseConnection;
|
||||||
use PDO;
|
use PDO;
|
||||||
use PDOStatement;
|
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use PHPUnit_Framework_MockObject_MockObject as MockObject;
|
use PHPUnit_Framework_MockObject_MockObject as MockObject;
|
||||||
use ReflectionObject;
|
|
||||||
use Throwable;
|
|
||||||
|
|
||||||
class DbTest extends TestCase
|
class DbTest extends TestCase
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @covers \Engelsystem\Database\Db::setDbManager()
|
* @covers \Engelsystem\Database\Db::setDbManager()
|
||||||
|
* @covers \Engelsystem\Database\Db::connection()
|
||||||
*/
|
*/
|
||||||
public function testSetDbManager()
|
public function testSetDbManager()
|
||||||
{
|
{
|
||||||
|
@ -43,27 +41,7 @@ class DbTest extends TestCase
|
||||||
|
|
||||||
Db::setDbManager($dbManager);
|
Db::setDbManager($dbManager);
|
||||||
$this->assertEquals($pdo, Db::getPdo());
|
$this->assertEquals($pdo, Db::getPdo());
|
||||||
}
|
$this->assertEquals($databaseConnection, Db::connection());
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers \Engelsystem\Database\Db::query()
|
|
||||||
*/
|
|
||||||
public function testQuery()
|
|
||||||
{
|
|
||||||
$stm = Db::query('SELECT * FROM test_data');
|
|
||||||
$this->assertEquals('00000', $stm->errorCode());
|
|
||||||
|
|
||||||
$stm = Db::query('SELECT * FROM test_data WHERE id = ?', [4]);
|
|
||||||
$this->assertEquals('00000', $stm->errorCode());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers \Engelsystem\Database\Db::unprepared()
|
|
||||||
*/
|
|
||||||
public function testUnprepared()
|
|
||||||
{
|
|
||||||
$return = Db::unprepared('SELECT * FROM test_data WHERE id = 3');
|
|
||||||
$this->assertTrue($return);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -99,11 +77,8 @@ class DbTest extends TestCase
|
||||||
*/
|
*/
|
||||||
public function testInsert()
|
public function testInsert()
|
||||||
{
|
{
|
||||||
$count = Db::insert("INSERT INTO test_data (id, data) VALUES (5, 'Some random text'), (6, 'another text')");
|
$result = Db::insert("INSERT INTO test_data (id, data) VALUES (5, 'Some random text'), (6, 'another text')");
|
||||||
$this->assertEquals(2, $count);
|
$this->assertTrue($result);
|
||||||
|
|
||||||
$count = Db::insert('INSERT INTO test_data(id, data) VALUES (:id, :alias)', ['id' => 7, 'alias' => 'Blafoo']);
|
|
||||||
$this->assertEquals(1, $count);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -130,42 +105,6 @@ class DbTest extends TestCase
|
||||||
$this->assertEquals(3, $count);
|
$this->assertEquals(3, $count);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers \Engelsystem\Database\Db::statement()
|
|
||||||
*/
|
|
||||||
public function testStatement()
|
|
||||||
{
|
|
||||||
$return = Db::statement('SELECT * FROM test_data WHERE id = 3');
|
|
||||||
$this->assertTrue($return);
|
|
||||||
|
|
||||||
$return = Db::statement('SELECT * FROM test_data WHERE id = ?', [2]);
|
|
||||||
$this->assertTrue($return);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers \Engelsystem\Database\Db::getError()
|
|
||||||
*/
|
|
||||||
public function testGetError()
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
Db::statement('foo');
|
|
||||||
} catch (Throwable $e) {
|
|
||||||
}
|
|
||||||
|
|
||||||
$error = Db::getError();
|
|
||||||
$this->assertTrue(is_array($error));
|
|
||||||
$this->assertEquals('near "foo": syntax error', $error[2]);
|
|
||||||
|
|
||||||
$db = new Db();
|
|
||||||
$refObject = new ReflectionObject($db);
|
|
||||||
$refProperty = $refObject->getProperty('stm');
|
|
||||||
$refProperty->setAccessible(true);
|
|
||||||
$refProperty->setValue(null, null);
|
|
||||||
|
|
||||||
$error = Db::getError();
|
|
||||||
$this->assertEquals([-1, null, null], $error);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers \Engelsystem\Database\Db::getPdo()
|
* @covers \Engelsystem\Database\Db::getPdo()
|
||||||
*/
|
*/
|
||||||
|
@ -175,15 +114,6 @@ class DbTest extends TestCase
|
||||||
$this->assertInstanceOf(PDO::class, $pdo);
|
$this->assertInstanceOf(PDO::class, $pdo);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers \Engelsystem\Database\Db::getStm()
|
|
||||||
*/
|
|
||||||
public function testGetStm()
|
|
||||||
{
|
|
||||||
$stm = Db::getStm();
|
|
||||||
$this->assertInstanceOf(PDOStatement::class, $stm);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Setup in memory database
|
* Setup in memory database
|
||||||
*/
|
*/
|
||||||
|
@ -196,14 +126,14 @@ class DbTest extends TestCase
|
||||||
|
|
||||||
Db::setDbManager($dbManager);
|
Db::setDbManager($dbManager);
|
||||||
Db::getPdo()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
Db::getPdo()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||||
Db::query(
|
Db::connection()->statement(
|
||||||
'
|
'
|
||||||
CREATE TABLE test_data(
|
CREATE TABLE test_data(
|
||||||
id INT PRIMARY KEY NOT NULL,
|
id INT PRIMARY KEY NOT NULL,
|
||||||
data TEXT NOT NULL
|
data TEXT NOT NULL
|
||||||
);
|
);
|
||||||
');
|
');
|
||||||
Db::query('CREATE UNIQUE INDEX test_data_id_uindex ON test_data (id);');
|
Db::connection()->statement('CREATE UNIQUE INDEX test_data_id_uindex ON test_data (id);');
|
||||||
Db::insert("
|
Db::insert("
|
||||||
INSERT INTO test_data (id, data)
|
INSERT INTO test_data (id, data)
|
||||||
VALUES
|
VALUES
|
||||||
|
|
Loading…
Reference in New Issue