Db.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\DbConnection;
  12. use Closure;
  13. use Generator;
  14. use Hyperf\Context\ApplicationContext;
  15. use Hyperf\Database\Connection as Conn;
  16. use Hyperf\Database\ConnectionInterface;
  17. use Hyperf\Database\ConnectionResolverInterface;
  18. use Hyperf\Database\Query\Builder;
  19. use Hyperf\Database\Query\Expression;
  20. use Psr\Container\ContainerInterface;
  21. /**
  22. * DB Helper.
  23. * @method static Builder table(string $table)
  24. * @method static Expression raw($value)
  25. * @method static mixed selectOne(string $query, array $bindings = [], bool $useReadPdo = true)
  26. * @method static array select(string $query, array $bindings = [], bool $useReadPdo = true)
  27. * @method static Generator cursor(string $query, array $bindings = [], bool $useReadPdo = true)
  28. * @method static bool insert(string $query, array $bindings = [])
  29. * @method static int update(string $query, array $bindings = [])
  30. * @method static int delete(string $query, array $bindings = [])
  31. * @method static bool statement(string $query, array $bindings = [])
  32. * @method static int affectingStatement(string $query, array $bindings = [])
  33. * @method static bool unprepared(string $query)
  34. * @method static array prepareBindings(array $bindings)
  35. * @method static mixed transaction(Closure $callback, int $attempts = 1)
  36. * @method static void beginTransaction()
  37. * @method static void rollBack()
  38. * @method static void commit()
  39. * @method static int transactionLevel()
  40. * @method static array pretend(Closure $callback)
  41. * @method static ConnectionInterface connection(string $pool)
  42. */
  43. class Db
  44. {
  45. public function __construct(protected ContainerInterface $container)
  46. {
  47. }
  48. public function __call($name, $arguments)
  49. {
  50. if ($name === 'connection') {
  51. return $this->__connection(...$arguments);
  52. }
  53. return $this->__connection()->{$name}(...$arguments);
  54. }
  55. public static function __callStatic($name, $arguments)
  56. {
  57. $db = ApplicationContext::getContainer()->get(Db::class);
  58. if ($name === 'connection') {
  59. return $db->__connection(...$arguments);
  60. }
  61. return $db->__connection()->{$name}(...$arguments);
  62. }
  63. private function __connection(?string $name = null): ConnectionInterface
  64. {
  65. $resolver = $this->container->get(ConnectionResolverInterface::class);
  66. return $resolver->connection($name);
  67. }
  68. public static function beforeExecuting(Closure $closure): void
  69. {
  70. Conn::beforeExecuting($closure);
  71. }
  72. }