diff options
author | Igor Scheller <igor.scheller@igorshp.de> | 2018-01-14 13:32:57 +0100 |
---|---|---|
committer | Igor Scheller <igor.scheller@igorshp.de> | 2018-01-14 13:32:57 +0100 |
commit | 0e8cc2f0a73085170df45c6a40e8f3df06a6af51 (patch) | |
tree | a38b1c1d7da28f4c95b201ab32d370916e749832 | |
parent | d4b36e9bad4c9d64447f053ec19a9f600888b2fe (diff) |
database: integrated illuminate/database do Db class, removed unused methods
-rw-r--r-- | includes/model/EventConfig_model.php | 4 | ||||
-rw-r--r-- | includes/model/LogEntries_model.php | 2 | ||||
-rw-r--r-- | src/Database/Db.php | 99 | ||||
-rw-r--r-- | tests/Unit/Database/DbTest.php | 82 |
4 files changed, 26 insertions, 161 deletions
diff --git a/includes/model/EventConfig_model.php b/includes/model/EventConfig_model.php index 646d19c5..8a29482b 100644 --- a/includes/model/EventConfig_model.php +++ b/includes/model/EventConfig_model.php @@ -21,7 +21,7 @@ function EventConfig() * @param int $event_end_date * @param int $teardown_end_date * @param string $event_welcome_msg - * @return int Rows updated + * @return bool */ function EventConfig_update( $event_name, @@ -54,7 +54,7 @@ function EventConfig_update( ); } - return DB::update(' + return (bool)DB::update(' UPDATE `EventConfig` SET `event_name` = ?, `buildup_start_date` = ?, diff --git a/includes/model/LogEntries_model.php b/includes/model/LogEntries_model.php index f0ee6673..b16c598c 100644 --- a/includes/model/LogEntries_model.php +++ b/includes/model/LogEntries_model.php @@ -58,5 +58,5 @@ function LogEntries_filter($keyword) */ function LogEntries_clear_all() { - return DB::statement('TRUNCATE `LogEntries`'); + return DB::connection()->statement('TRUNCATE `LogEntries`'); } diff --git a/src/Database/Db.php b/src/Database/Db.php index aa9b1a27..426dd526 100644 --- a/src/Database/Db.php +++ b/src/Database/Db.php @@ -3,20 +3,14 @@ namespace Engelsystem\Database; use Illuminate\Database\Capsule\Manager as CapsuleManager; +use Illuminate\Database\Connection as DatabaseConnection; use PDO; -use PDOStatement; class Db { /** @var CapsuleManager */ protected static $dbManager; - /** @var PDOStatement */ - protected static $stm = null; - - /** @var bool */ - protected static $lastStatus = true; - /** * Set the database connection manager * @@ -28,35 +22,6 @@ class Db } /** - * 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 * * @param string $query @@ -65,9 +30,13 @@ class Db */ public static function select($query, array $bindings = []) { - self::query($query, $bindings); + $return = self::connection()->select($query, $bindings); + + foreach ($return as $key => $value) { + $return[$key] = (array)$value; + } - return self::$stm->fetchAll(PDO::FETCH_ASSOC); + return $return; } /** @@ -79,13 +48,13 @@ class Db */ 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); + return (array)$result; } /** @@ -93,13 +62,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); } /** @@ -111,9 +78,7 @@ class Db */ public static function update($query, array $bindings = []) { - self::query($query, $bindings); - - return self::$stm->rowCount(); + return self::connection()->update($query, $bindings); } /** @@ -125,37 +90,15 @@ class Db */ public static function delete($query, array $bindings = []) { - self::query($query, $bindings); - - return self::$stm->rowCount(); - } - - /** - * 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; + return self::connection()->delete($query, $bindings); } /** - * Returns the last error - * - * @return array + * @return DatabaseConnection */ - public static function getError() + public static function connection() { - if (!self::$stm instanceof PDOStatement) { - return [-1, null, null]; - } - - return self::$stm->errorInfo(); + return self::$dbManager->getConnection(); } /** @@ -165,14 +108,6 @@ class Db */ public static function getPdo() { - return self::$dbManager->getConnection()->getPdo(); - } - - /** - * @return PDOStatement|false|null - */ - public static function getStm() - { - return self::$stm; + return self::connection()->getPdo(); } } diff --git a/tests/Unit/Database/DbTest.php b/tests/Unit/Database/DbTest.php index 17829e77..ca6ac52c 100644 --- a/tests/Unit/Database/DbTest.php +++ b/tests/Unit/Database/DbTest.php @@ -6,16 +6,14 @@ use Engelsystem\Database\Db; use Illuminate\Database\Capsule\Manager as CapsuleManager; use Illuminate\Database\Connection as DatabaseConnection; use PDO; -use PDOStatement; use PHPUnit\Framework\TestCase; use PHPUnit_Framework_MockObject_MockObject as MockObject; -use ReflectionObject; -use Throwable; class DbTest extends TestCase { /** * @covers \Engelsystem\Database\Db::setDbManager() + * @covers \Engelsystem\Database\Db::connection() */ public function testSetDbManager() { @@ -43,27 +41,7 @@ class DbTest extends TestCase Db::setDbManager($dbManager); $this->assertEquals($pdo, Db::getPdo()); - } - - /** - * @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); + $this->assertEquals($databaseConnection, Db::connection()); } /** @@ -99,11 +77,8 @@ class DbTest extends TestCase */ public function testInsert() { - $count = Db::insert("INSERT INTO test_data (id, data) VALUES (5, 'Some random text'), (6, 'another text')"); - $this->assertEquals(2, $count); - - $count = Db::insert('INSERT INTO test_data(id, data) VALUES (:id, :alias)', ['id' => 7, 'alias' => 'Blafoo']); - $this->assertEquals(1, $count); + $result = Db::insert("INSERT INTO test_data (id, data) VALUES (5, 'Some random text'), (6, 'another text')"); + $this->assertTrue($result); } /** @@ -131,42 +106,6 @@ class DbTest extends TestCase } /** - * @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() */ public function testGetPdo() @@ -176,15 +115,6 @@ class DbTest extends TestCase } /** - * @covers \Engelsystem\Database\Db::getStm() - */ - public function testGetStm() - { - $stm = Db::getStm(); - $this->assertInstanceOf(PDOStatement::class, $stm); - } - - /** * Setup in memory database */ protected function setUp() @@ -196,14 +126,14 @@ class DbTest extends TestCase Db::setDbManager($dbManager); Db::getPdo()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - Db::query( + Db::connection()->statement( ' CREATE TABLE test_data( id INT PRIMARY KEY 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(" INSERT INTO test_data (id, data) VALUES |