mirror of
https://github.com/dg/dibi.git
synced 2025-08-05 05:37:39 +02:00
dibi: monostate implemented via __callStatic
This commit is contained in:
@@ -11,6 +11,29 @@ declare(strict_types=1);
|
|||||||
/**
|
/**
|
||||||
* This class is static container class for creating DB objects and
|
* This class is static container class for creating DB objects and
|
||||||
* store connections info.
|
* store connections info.
|
||||||
|
*
|
||||||
|
* @method void disconnect()
|
||||||
|
* @method Dibi\Result|int query(...$args)
|
||||||
|
* @method Dibi\Result|int nativeQuery(...$args)
|
||||||
|
* @method bool test(...$args)
|
||||||
|
* @method Dibi\DataSource dataSource(...$args)
|
||||||
|
* @method Dibi\Row|null fetch(...$args)
|
||||||
|
* @method array fetchAll(...$args)
|
||||||
|
* @method mixed fetchSingle(...$args)
|
||||||
|
* @method array fetchPairs(...$args)
|
||||||
|
* @method int getAffectedRows()
|
||||||
|
* @method int getInsertId(string $sequence = null)
|
||||||
|
* @method void begin(string $savepoint = null)
|
||||||
|
* @method void commit(string $savepoint = null)
|
||||||
|
* @method void rollback(string $savepoint = null)
|
||||||
|
* @method Dibi\Reflection\Database getDatabaseInfo()
|
||||||
|
* @method Dibi\Fluent command()
|
||||||
|
* @method Dibi\Fluent select(...$args)
|
||||||
|
* @method Dibi\Fluent update(string $table, array $args)
|
||||||
|
* @method Dibi\Fluent insert(string $table, array $args)
|
||||||
|
* @method Dibi\Fluent delete(string $table)
|
||||||
|
* @method Dibi\HashMap getSubstitutes()
|
||||||
|
* @method int loadFile(string $file)
|
||||||
*/
|
*/
|
||||||
class dibi
|
class dibi
|
||||||
{
|
{
|
||||||
@@ -73,15 +96,6 @@ class dibi
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Disconnects from database (doesn't destroy Connection object).
|
|
||||||
*/
|
|
||||||
public static function disconnect(): void
|
|
||||||
{
|
|
||||||
self::getConnection()->disconnect();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true when connection was established.
|
* Returns true when connection was established.
|
||||||
*/
|
*/
|
||||||
@@ -126,101 +140,11 @@ class dibi
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates and executes SQL query - Monostate for Dibi\Connection::query().
|
* Monostate for Dibi\Connection.
|
||||||
* @param mixed $args
|
|
||||||
* @return Dibi\Result|int result set or number of affected rows
|
|
||||||
* @throws Dibi\Exception
|
|
||||||
*/
|
*/
|
||||||
public static function query(...$args)
|
public static function __callStatic($name, $args)
|
||||||
{
|
{
|
||||||
return self::getConnection()->query($args);
|
return self::getConnection()->$name(...$args);
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Executes the SQL query - Monostate for Dibi\Connection::nativeQuery().
|
|
||||||
* @return Dibi\Result|int result set or number of affected rows
|
|
||||||
*/
|
|
||||||
public static function nativeQuery(string $sql)
|
|
||||||
{
|
|
||||||
return self::getConnection()->nativeQuery($sql);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Generates and prints SQL query - Monostate for Dibi\Connection::test().
|
|
||||||
* @param mixed $args
|
|
||||||
*/
|
|
||||||
public static function test(...$args): bool
|
|
||||||
{
|
|
||||||
return self::getConnection()->test($args);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Generates and returns SQL query as DataSource - Monostate for Dibi\Connection::test().
|
|
||||||
* @param mixed $args
|
|
||||||
*/
|
|
||||||
public static function dataSource(...$args): Dibi\DataSource
|
|
||||||
{
|
|
||||||
return self::getConnection()->dataSource($args);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Executes SQL query and fetch result - Monostate for Dibi\Connection::query() & fetch().
|
|
||||||
* @param mixed $args
|
|
||||||
* @throws Dibi\Exception
|
|
||||||
*/
|
|
||||||
public static function fetch(...$args): ?Dibi\Row
|
|
||||||
{
|
|
||||||
return self::getConnection()->query($args)->fetch();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Executes SQL query and fetch results - Monostate for Dibi\Connection::query() & fetchAll().
|
|
||||||
* @param mixed $args
|
|
||||||
* @return Dibi\Row[]
|
|
||||||
* @throws Dibi\Exception
|
|
||||||
*/
|
|
||||||
public static function fetchAll(...$args): array
|
|
||||||
{
|
|
||||||
return self::getConnection()->query($args)->fetchAll();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Executes SQL query and fetch first column - Monostate for Dibi\Connection::query() & fetchSingle().
|
|
||||||
* @param mixed $args
|
|
||||||
* @return mixed
|
|
||||||
* @throws Dibi\Exception
|
|
||||||
*/
|
|
||||||
public static function fetchSingle(...$args)
|
|
||||||
{
|
|
||||||
return self::getConnection()->query($args)->fetchSingle();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Executes SQL query and fetch pairs - Monostate for Dibi\Connection::query() & fetchPairs().
|
|
||||||
* @param mixed $args
|
|
||||||
* @throws Dibi\Exception
|
|
||||||
*/
|
|
||||||
public static function fetchPairs(...$args): array
|
|
||||||
{
|
|
||||||
return self::getConnection()->query($args)->fetchPairs();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the number of affected rows.
|
|
||||||
* Monostate for Dibi\Connection::getAffectedRows()
|
|
||||||
* @throws Dibi\Exception
|
|
||||||
*/
|
|
||||||
public static function getAffectedRows(): int
|
|
||||||
{
|
|
||||||
return self::getConnection()->getAffectedRows();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -234,17 +158,6 @@ class dibi
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
|
|
||||||
* Monostate for Dibi\Connection::getInsertId()
|
|
||||||
* @throws Dibi\Exception
|
|
||||||
*/
|
|
||||||
public static function getInsertId(string $sequence = null): int
|
|
||||||
{
|
|
||||||
return self::getConnection()->getInsertId($sequence);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @deprecated
|
* @deprecated
|
||||||
*/
|
*/
|
||||||
@@ -255,100 +168,6 @@ class dibi
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Begins a transaction - Monostate for Dibi\Connection::begin().
|
|
||||||
* @throws Dibi\Exception
|
|
||||||
*/
|
|
||||||
public static function begin(string $savepoint = null): void
|
|
||||||
{
|
|
||||||
self::getConnection()->begin($savepoint);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Commits statements in a transaction - Monostate for Dibi\Connection::commit($savepoint = null).
|
|
||||||
* @throws Dibi\Exception
|
|
||||||
*/
|
|
||||||
public static function commit(string $savepoint = null): void
|
|
||||||
{
|
|
||||||
self::getConnection()->commit($savepoint);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Rollback changes in a transaction - Monostate for Dibi\Connection::rollback().
|
|
||||||
* @throws Dibi\Exception
|
|
||||||
*/
|
|
||||||
public static function rollback(string $savepoint = null): void
|
|
||||||
{
|
|
||||||
self::getConnection()->rollback($savepoint);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets a information about the current database - Monostate for Dibi\Connection::getDatabaseInfo().
|
|
||||||
*/
|
|
||||||
public static function getDatabaseInfo(): Dibi\Reflection\Database
|
|
||||||
{
|
|
||||||
return self::getConnection()->getDatabaseInfo();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Import SQL dump from file - extreme fast!
|
|
||||||
* @return int count of sql commands
|
|
||||||
*/
|
|
||||||
public static function loadFile(string $file): int
|
|
||||||
{
|
|
||||||
return Dibi\Helpers::loadFromFile(self::getConnection(), $file);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/********************* fluent SQL builders ****************d*g**/
|
|
||||||
|
|
||||||
|
|
||||||
public static function command(): Dibi\Fluent
|
|
||||||
{
|
|
||||||
return self::getConnection()->command();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public static function select(...$args): Dibi\Fluent
|
|
||||||
{
|
|
||||||
return self::getConnection()->select(...$args);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public static function update(string $table, array $args): Dibi\Fluent
|
|
||||||
{
|
|
||||||
return self::getConnection()->update($table, $args);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public static function insert(string $table, array $args): Dibi\Fluent
|
|
||||||
{
|
|
||||||
return self::getConnection()->insert($table, $args);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public static function delete(string $table): Dibi\Fluent
|
|
||||||
{
|
|
||||||
return self::getConnection()->delete($table);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/********************* substitutions ****************d*g**/
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns substitution hashmap - Monostate for Dibi\Connection::getSubstitutes().
|
|
||||||
*/
|
|
||||||
public static function getSubstitutes(): Dibi\HashMap
|
|
||||||
{
|
|
||||||
return self::getConnection()->getSubstitutes();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/********************* misc tools ****************d*g**/
|
/********************* misc tools ****************d*g**/
|
||||||
|
|
||||||
|
|
||||||
|
18
tests/dibi/dibi.phpt
Normal file
18
tests/dibi/dibi.phpt
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider ../databases.ini
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
use Tester\Assert;
|
||||||
|
|
||||||
|
require __DIR__ . '/bootstrap.php';
|
||||||
|
|
||||||
|
dibi::connect($config);
|
||||||
|
dibi::loadFile(__DIR__ . "/data/$config[system].sql");
|
||||||
|
dibi::query('INSERT INTO products', [
|
||||||
|
'title' => 'Test product',
|
||||||
|
]);
|
||||||
|
Assert::same(1, dibi::getAffectedRows());
|
Reference in New Issue
Block a user