1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- <?php
- declare(strict_types=1);
- /**
- * This file is part of Hyperf.
- *
- * @link https://www.hyperf.io
- * @document https://hyperf.wiki
- * @contact group@hyperf.io
- * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
- */
- namespace Hyperf\DbConnection;
- use Closure;
- use Generator;
- use Hyperf\Context\ApplicationContext;
- use Hyperf\Database\Connection as Conn;
- use Hyperf\Database\ConnectionInterface;
- use Hyperf\Database\ConnectionResolverInterface;
- use Hyperf\Database\Query\Builder;
- use Hyperf\Database\Query\Expression;
- use Psr\Container\ContainerInterface;
- /**
- * DB Helper.
- * @method static Builder table(string $table)
- * @method static Expression raw($value)
- * @method static mixed selectOne(string $query, array $bindings = [], bool $useReadPdo = true)
- * @method static array select(string $query, array $bindings = [], bool $useReadPdo = true)
- * @method static Generator cursor(string $query, array $bindings = [], bool $useReadPdo = true)
- * @method static bool insert(string $query, array $bindings = [])
- * @method static int update(string $query, array $bindings = [])
- * @method static int delete(string $query, array $bindings = [])
- * @method static bool statement(string $query, array $bindings = [])
- * @method static int affectingStatement(string $query, array $bindings = [])
- * @method static bool unprepared(string $query)
- * @method static array prepareBindings(array $bindings)
- * @method static mixed transaction(Closure $callback, int $attempts = 1)
- * @method static void beginTransaction()
- * @method static void rollBack()
- * @method static void commit()
- * @method static int transactionLevel()
- * @method static array pretend(Closure $callback)
- * @method static ConnectionInterface connection(string $pool)
- */
- class Db
- {
- public function __construct(protected ContainerInterface $container)
- {
- }
- public function __call($name, $arguments)
- {
- if ($name === 'connection') {
- return $this->__connection(...$arguments);
- }
- return $this->__connection()->{$name}(...$arguments);
- }
- public static function __callStatic($name, $arguments)
- {
- $db = ApplicationContext::getContainer()->get(Db::class);
- if ($name === 'connection') {
- return $db->__connection(...$arguments);
- }
- return $db->__connection()->{$name}(...$arguments);
- }
- private function __connection(?string $name = null): ConnectionInterface
- {
- $resolver = $this->container->get(ConnectionResolverInterface::class);
- return $resolver->connection($name);
- }
- public static function beforeExecuting(Closure $closure): void
- {
- Conn::beforeExecuting($closure);
- }
- }
|