Files

262 lines
5.9 KiB
PHP
Raw Permalink Normal View History

2022-01-09 03:49:51 +08:00
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/4 0004
* Time: 15:40
*/
declare(strict_types=1);
namespace Database;
2023-04-10 17:13:24 +08:00
use Closure;
2023-05-26 09:20:29 +08:00
use Database\Affair\BeginTransaction;
2022-01-09 03:49:51 +08:00
use Database\Affair\Commit;
use Database\Affair\Rollback;
use Database\Traits\QueryTrait;
use Exception;
2023-12-13 14:47:23 +08:00
use Kiri;
2023-06-12 15:31:45 +08:00
use Throwable;
2022-01-09 03:49:51 +08:00
/**
* Class Db
* @package Database
*/
2023-12-13 10:05:42 +08:00
class Db extends QueryTrait implements ISqlBuilder
2022-01-09 03:49:51 +08:00
{
2023-06-12 15:31:45 +08:00
private static bool $_inTransaction = false;
2022-01-09 03:49:51 +08:00
2023-06-12 15:31:45 +08:00
/**
* @var Connection|null
*/
private ?Connection $connection = null;
/**
* @param string|Connection $dbname
* @return Db
2023-12-12 15:35:35 +08:00
* @throws
2023-06-12 15:31:45 +08:00
*/
public static function connect(string|Connection $dbname): Db
{
$db = new Db();
if (is_string($dbname)) {
2023-12-13 14:47:23 +08:00
$db->connection = Kiri::getDi()->get(DatabasesProviders::class)->get($dbname);
} else {
$db->connection = $dbname;
2023-06-12 15:31:45 +08:00
}
return $db;
}
/**
* @return void
2023-12-12 15:35:35 +08:00
* @throws
2023-06-12 15:31:45 +08:00
*/
public static function beginTransaction(): void
{
2023-05-26 09:20:29 +08:00
fire(new BeginTransaction());
2023-06-12 15:31:45 +08:00
}
2023-05-26 09:20:29 +08:00
/**
* @return void
2023-12-12 15:35:35 +08:00
* @throws
2023-05-26 09:20:29 +08:00
*/
2023-06-12 15:31:45 +08:00
public static function commit(): void
{
2023-05-26 09:20:29 +08:00
fire(new Commit());
2023-06-12 15:31:45 +08:00
}
2022-01-09 03:49:51 +08:00
2023-05-26 09:20:29 +08:00
/**
* @return void
2023-12-12 15:35:35 +08:00
* @throws
2023-05-26 09:20:29 +08:00
*/
2023-06-12 15:31:45 +08:00
public static function rollback(): void
{
2023-05-26 09:20:29 +08:00
fire(new Rollback());
2023-06-12 15:31:45 +08:00
}
/**
2023-11-30 17:02:20 +08:00
* @param string $table
* @param string $database
2023-06-12 15:31:45 +08:00
* @return static
*/
2023-11-30 17:02:20 +08:00
public static function table(string $table, string $database = 'db'): Db|static
2023-06-12 15:31:45 +08:00
{
2023-12-13 15:36:51 +08:00
return self::connect($database)->from($table);
2023-06-12 15:31:45 +08:00
}
/**
* @param string $column
* @param string $alias
* @return string
*/
public static function any_value(string $column, string $alias = ''): string
{
if (empty($alias)) {
$alias = $column . '_any_value';
}
return 'ANY_VALUE(' . $column . ') as ' . $alias;
}
/**
2023-12-13 14:47:23 +08:00
* @return int
2023-06-12 15:31:45 +08:00
*/
2023-12-13 14:47:23 +08:00
public function count(): int
2023-06-12 15:31:45 +08:00
{
2023-12-13 15:36:51 +08:00
return $this->connection->createCommand(SqlBuilder::builder($this)->count())->rowCount();
2023-06-12 15:31:45 +08:00
}
/**
2023-12-13 14:47:23 +08:00
* @return bool
2024-04-26 15:36:55 +08:00
* @throws Exception
2023-06-12 15:31:45 +08:00
*/
2023-12-13 14:47:23 +08:00
public function exists(): bool
2023-06-12 15:31:45 +08:00
{
2024-04-26 15:36:55 +08:00
return $this->connection->createCommand(SqlBuilder::builder($this->limit(1))->exists())->exists();
2023-06-12 15:31:45 +08:00
}
/**
* @param string $sql
* @param array $attributes
* @return array|bool|int|string|null
2023-12-12 15:35:35 +08:00
* @throws
2023-06-12 15:31:45 +08:00
*/
2023-12-13 15:36:51 +08:00
public function fetchAll(string $sql, array $attributes = []): int|bool|array|string|null
2023-06-12 15:31:45 +08:00
{
return $this->connection->createCommand($sql, $attributes)->all();
}
/**
* @param string $sql
* @param array $attributes
2023-12-13 14:47:23 +08:00
* @return array|null
2023-06-12 15:31:45 +08:00
*/
2023-12-13 15:36:51 +08:00
public function fetch(string $sql, array $attributes = []): ?array
2023-06-12 15:31:45 +08:00
{
return $this->connection->createCommand($sql, $attributes)->one();
}
/**
2023-12-13 14:47:23 +08:00
* @return bool
2023-06-12 15:31:45 +08:00
*/
2023-12-13 14:47:23 +08:00
public function delete(): bool
2023-06-12 15:31:45 +08:00
{
return $this->connection->createCommand(SqlBuilder::builder($this)->delete())->delete();
}
2024-04-29 21:49:08 +08:00
2024-04-29 21:55:19 +08:00
/**
* @return bool|array|null
*/
public function first(): bool|array|null
{
2024-04-29 21:55:31 +08:00
return $this->connection->createCommand(SqlBuilder::builder($this)->one())->one();
2024-04-29 21:55:19 +08:00
}
/**
* @return bool|array
*/
public function get(): bool|array
{
2024-04-29 21:55:31 +08:00
return $this->connection->createCommand(SqlBuilder::builder($this)->all())->all();
2024-04-29 21:55:19 +08:00
}
2024-04-29 21:49:08 +08:00
/**
* @param string $sql
* @return mixed
*/
public function exec(string $sql): mixed
{
return $this->connection->createCommand($sql)->exec();
}
/**
* @param string $sql
* @return array|bool|null
*/
public function query(string $sql): array|bool|null
{
return $this->connection->createCommand($sql)->one();
}
/**
* @param string $sql
* @return array|bool
*/
public function queryAll(string $sql): array|bool
{
return $this->connection->createCommand($sql)->all();
}
2023-06-12 15:31:45 +08:00
/**
* @param string $table
* @return array|bool|null
2023-12-12 15:35:35 +08:00
* @throws
2023-06-12 15:31:45 +08:00
*/
public static function show(string $table): array|bool|null
{
if ($table == '') {
return null;
}
$connection = static::getDefaultConnection();
$table = [' const TABLE = \'select * from %s where REFERENCED_TABLE_NAME=%s\';'];
return $connection->createCommand((new Query())
->from('INFORMATION_SCHEMA.KEY_COLUMN_USAGE')
->where(['REFERENCED_TABLE_NAME' => $table])
2023-12-13 14:47:23 +08:00
->build())->one();
2023-06-12 15:31:45 +08:00
}
2023-07-13 10:37:14 +08:00
/**
* @param string $table
* @param Connection|null $connection
* @param string $database
* @return array|null
2023-12-12 15:35:35 +08:00
* @throws
2023-07-13 10:37:14 +08:00
*/
public static function desc(string $table, ?Connection $connection = null, string $database = 'db'): ?array
{
$sql = SqlBuilder::builder(new Query())->columns($table);
$connection = self::getDefaultConnection($connection, $database);
return $connection->createCommand($sql)->all();
}
2023-06-12 15:31:45 +08:00
/**
* @param null|Connection $connection
2023-07-13 10:37:14 +08:00
* @param string $database
2023-06-12 15:31:45 +08:00
* @return mixed
2023-12-12 15:35:35 +08:00
* @throws
2023-06-12 15:31:45 +08:00
*/
2023-07-13 10:37:14 +08:00
public static function getDefaultConnection(?Connection $connection = null, string $database = 'db'): Connection
2023-05-26 09:20:29 +08:00
{
2023-06-12 15:31:45 +08:00
if ($connection instanceof Connection) {
return $connection;
}
$databases = \config('databases.connections', []);
2023-12-13 14:47:23 +08:00
$providers = Kiri::getDi()->get(DatabasesProviders::class);
2023-06-12 15:31:45 +08:00
if (empty($databases) || !is_array($databases)) {
throw new Exception('Please configure the database link.');
}
2023-11-30 17:02:20 +08:00
return $providers->get($databases[$database]);
2023-06-12 15:31:45 +08:00
}
2022-01-09 03:49:51 +08:00
}