Files
kiri-databases/Db.php
T

260 lines
6.2 KiB
PHP
Raw 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-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-11-30 17:02:20 +08:00
$dbname = \Kiri::getDi()->get(DatabasesProviders::class)->get($dbname);
2023-06-12 15:31:45 +08:00
}
$db->connection = $dbname;
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
}
/**
* @param Closure $closure
* @param mixed ...$params
* @return mixed
2023-12-12 15:35:35 +08:00
* @throws
2023-06-12 15:31:45 +08:00
*/
public static function Transaction(Closure $closure, ...$params): mixed
{
static::beginTransaction();
try {
$result = call_user_func($closure, ...$params);
} catch (Throwable $throwable) {
2023-07-31 23:08:59 +08:00
$result = trigger_print_error($throwable->getMessage(), 'mysql');
2023-06-12 15:31:45 +08:00
} finally {
if ($result === false) {
static::rollback();
} else {
static::commit();
}
return $result;
}
}
2023-04-10 17:13:24 +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-10-07 20:40:59 +08:00
$connection = new Db();
2023-06-12 15:31:45 +08:00
$connection->connection = current(\config('databases.connections'));
$connection->from($table);
return $connection;
}
/**
* @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;
}
/**
* @return array|bool
2023-12-12 15:35:35 +08:00
* @throws
2023-06-12 15:31:45 +08:00
*/
public function get(): array|bool
{
return $this->connection->createCommand(SqlBuilder::builder($this)->all())->all();
}
/**
* @return array|bool|null
2023-12-12 15:35:35 +08:00
* @throws
2023-06-12 15:31:45 +08:00
*/
public function first(): array|bool|null
{
return $this->connection->createCommand(SqlBuilder::builder($this)->all())->one();
}
/**
* @return bool|int
2023-12-12 15:35:35 +08:00
* @throws
2023-06-12 15:31:45 +08:00
*/
public function count(): bool|int
{
return $this->connection->createCommand(SqlBuilder::builder($this)->count())->one()['row_count'];
}
/**
* @return bool|int
2023-12-12 15:35:35 +08:00
* @throws
2023-06-12 15:31:45 +08:00
*/
public function exists(): bool|int
{
return $this->connection->createCommand(SqlBuilder::builder($this)->one())->fetchColumn();
}
/**
* @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
*/
public function query(string $sql, array $attributes = []): int|bool|array|string|null
{
return $this->connection->createCommand($sql, $attributes)->all();
}
/**
* @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
*/
public function one(string $sql, array $attributes = []): int|bool|array|string|null
{
return $this->connection->createCommand($sql, $attributes)->one();
}
/**
* @return bool|int
2023-12-12 15:35:35 +08:00
* @throws
2023-06-12 15:31:45 +08:00
*/
public function delete(): bool|int
{
return $this->connection->createCommand(SqlBuilder::builder($this)->delete())->delete();
}
/**
* @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())
->select('*')
->from('INFORMATION_SCHEMA.KEY_COLUMN_USAGE')
->where(['REFERENCED_TABLE_NAME' => $table])
->getSql())->one();
}
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-11-30 17:02:20 +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
}