qqq
This commit is contained in:
+1
-5
@@ -263,11 +263,7 @@ class ActiveQuery extends Component implements ISqlBuilder
|
|||||||
*/
|
*/
|
||||||
public function count(): int
|
public function count(): int
|
||||||
{
|
{
|
||||||
$data = $this->execute($this->builder->count(), $this->attributes)->one();
|
return $this->execute($this->builder->count(), $this->attributes)->one()['row_count'] ?? 0;
|
||||||
if ($data && is_array($data)) {
|
|
||||||
return (int)array_shift($data);
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+3
-2
@@ -11,6 +11,7 @@ declare(strict_types=1);
|
|||||||
namespace Database;
|
namespace Database;
|
||||||
|
|
||||||
|
|
||||||
|
use Closure;
|
||||||
use Database\Affair\BeginTransaction;
|
use Database\Affair\BeginTransaction;
|
||||||
use Database\Affair\Commit;
|
use Database\Affair\Commit;
|
||||||
use Database\Affair\Rollback;
|
use Database\Affair\Rollback;
|
||||||
@@ -96,9 +97,9 @@ class Connection extends Component
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $config
|
* @param array $config
|
||||||
* @return \Closure
|
* @return Closure
|
||||||
*/
|
*/
|
||||||
public function gender(array $config): \Closure
|
public function gender(array $config): Closure
|
||||||
{
|
{
|
||||||
return static function () use ($config) {
|
return static function () use ($config) {
|
||||||
$options = [
|
$options = [
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ class DatabasesProviders extends Providers
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
foreach ($databases as $key => $database) {
|
foreach ($databases as $key => $database) {
|
||||||
$application->set('db.' . $key, Kiri::createObject($this->_settings($database)));
|
$application->set($key, Kiri::createObject($this->_settings($database)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,7 +44,7 @@ class DatabasesProviders extends Providers
|
|||||||
*/
|
*/
|
||||||
public function get($name): Connection
|
public function get($name): Connection
|
||||||
{
|
{
|
||||||
return Kiri::service()->get('db.' . $name);
|
return Kiri::service()->get($name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -15,9 +15,11 @@ use Database\Affair\Commit;
|
|||||||
use Database\Affair\Rollback;
|
use Database\Affair\Rollback;
|
||||||
use Database\Traits\QueryTrait;
|
use Database\Traits\QueryTrait;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use Kiri;
|
||||||
use Kiri\Exception\ConfigException;
|
use Kiri\Exception\ConfigException;
|
||||||
use Psr\Container\ContainerExceptionInterface;
|
use Psr\Container\ContainerExceptionInterface;
|
||||||
use Psr\Container\NotFoundExceptionInterface;
|
use Psr\Container\NotFoundExceptionInterface;
|
||||||
|
use Throwable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Db
|
* Class Db
|
||||||
@@ -25,345 +27,223 @@ use Psr\Container\NotFoundExceptionInterface;
|
|||||||
*/
|
*/
|
||||||
class Db implements ISqlBuilder
|
class Db implements ISqlBuilder
|
||||||
{
|
{
|
||||||
use QueryTrait;
|
use QueryTrait;
|
||||||
|
|
||||||
|
|
||||||
private static bool $_inTransaction = false;
|
private static bool $_inTransaction = false;
|
||||||
|
|
||||||
/**
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public static function beginTransaction(): void
|
|
||||||
{
|
|
||||||
fire(new BeginTransaction());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Closure $closure
|
|
||||||
* @param mixed ...$params
|
|
||||||
* @return mixed
|
|
||||||
* @throws ContainerExceptionInterface
|
|
||||||
* @throws NotFoundExceptionInterface
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public static function Transaction(Closure $closure, ...$params): mixed
|
|
||||||
{
|
|
||||||
static::beginTransaction();
|
|
||||||
try {
|
|
||||||
$result = call_user_func($closure, ...$params);
|
|
||||||
} catch (\Throwable $throwable) {
|
|
||||||
error($throwable);
|
|
||||||
$result = addError($throwable->getMessage(), 'mysql');
|
|
||||||
} finally {
|
|
||||||
if ($result === false) {
|
|
||||||
static::rollback();
|
|
||||||
} else {
|
|
||||||
static::commit();
|
|
||||||
}
|
|
||||||
return $result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return void
|
* @var Connection|null
|
||||||
*/
|
*/
|
||||||
public static function commit(): void
|
private ?Connection $connection = null;
|
||||||
{
|
|
||||||
fire(new Commit());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return void
|
* @param string|Connection $dbname
|
||||||
|
* @return Db
|
||||||
|
* @throws \Exception
|
||||||
*/
|
*/
|
||||||
public static function rollback(): void
|
public static function connect(string|Connection $dbname): Db
|
||||||
{
|
|
||||||
fire(new Rollback());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $table
|
|
||||||
*
|
|
||||||
* @return static
|
|
||||||
*/
|
|
||||||
public static function table($table): Db|static
|
|
||||||
{
|
|
||||||
$connection = new Db();
|
|
||||||
$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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $column
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public static function increment(string $column): string
|
|
||||||
{
|
|
||||||
return '+ ' . $column;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $column
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public static function decrement(string $column): string
|
|
||||||
{
|
|
||||||
return '- ' . $column;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Connection|null $connection
|
|
||||||
* @return mixed
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function get(Connection $connection = NULL): mixed
|
|
||||||
{
|
|
||||||
$connection = static::getDefaultConnection($connection);
|
|
||||||
|
|
||||||
return $connection->createCommand(SqlBuilder::builder($this)->one())
|
|
||||||
->all();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $column
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public static function raw($column): string
|
|
||||||
{
|
|
||||||
return '`' . $column . '`';
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Connection|null $connection
|
|
||||||
* @return array|bool|null
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function find(Connection $connection = NULL): array|bool|null
|
|
||||||
{
|
{
|
||||||
$connection = static::getDefaultConnection($connection);
|
$db = new Db();
|
||||||
|
if (is_string($dbname)) {
|
||||||
|
$dbname = Kiri::service()->get($dbname);
|
||||||
|
}
|
||||||
|
$db->connection = $dbname;
|
||||||
|
return $db;
|
||||||
|
}
|
||||||
|
|
||||||
return $connection->createCommand(SqlBuilder::builder($this)->all())
|
/**
|
||||||
->one();
|
* @return void
|
||||||
}
|
* @throws Exception
|
||||||
|
*/
|
||||||
/**
|
public static function beginTransaction(): void
|
||||||
* @param Connection|NULL $connection
|
{
|
||||||
* @return bool|int
|
fire(new BeginTransaction());
|
||||||
* @throws Exception
|
}
|
||||||
*/
|
|
||||||
public function count(Connection $connection = NULL): bool|int
|
|
||||||
{
|
|
||||||
$connection = static::getDefaultConnection($connection);
|
|
||||||
|
|
||||||
return $connection->createCommand(SqlBuilder::builder($this)->count())
|
|
||||||
->exec();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Connection|NULL $connection
|
|
||||||
* @return bool|int
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function exists(Connection $connection = NULL): bool|int
|
|
||||||
{
|
|
||||||
$connection = static::getDefaultConnection($connection);
|
|
||||||
|
|
||||||
return $connection->createCommand(SqlBuilder::builder($this)->one())
|
|
||||||
->fetchColumn();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $sql
|
|
||||||
* @param array $attributes
|
|
||||||
* @param Connection|null $connection
|
|
||||||
* @return array|bool|int|string|null
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public static function findAllBySql(string $sql, array $attributes = [], Connection $connection = NULL): int|bool|array|string|null
|
|
||||||
{
|
|
||||||
$connection = static::getDefaultConnection($connection);
|
|
||||||
|
|
||||||
return $connection->createCommand($sql, $attributes)->all();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $sql
|
|
||||||
* @param array $attributes
|
|
||||||
* @param Connection|NULL $connection
|
|
||||||
* @return string|array|bool|int|null
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public static function findBySql(string $sql, array $attributes = [], Connection $connection = NULL): string|array|bool|int|null
|
|
||||||
{
|
|
||||||
$connection = static::getDefaultConnection($connection);
|
|
||||||
|
|
||||||
return $connection->createCommand($sql, $attributes)->one();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $field
|
|
||||||
* @return array|null
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function values(string $field): ?array
|
|
||||||
{
|
|
||||||
$data = $this->get();
|
|
||||||
if (empty($data) || empty($field)) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
$first = current($data);
|
|
||||||
if (!isset($first[$field])) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
return array_column($data, $field);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $field
|
|
||||||
* @return mixed
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function value($field): mixed
|
|
||||||
{
|
|
||||||
$data = $this->find();
|
|
||||||
if (!empty($field) && isset($data[$field])) {
|
|
||||||
return $data[$field];
|
|
||||||
}
|
|
||||||
return $data;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Connection|null $connection
|
|
||||||
* @return bool|int
|
|
||||||
* @throws ConfigException
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function delete(?Connection $connection = null): bool|int
|
|
||||||
{
|
|
||||||
$connection = static::getDefaultConnection($connection);
|
|
||||||
|
|
||||||
return $connection->createCommand($connection->getBuild()->builder($this))->delete();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $table
|
|
||||||
* @param null $connection
|
|
||||||
* @return bool|int
|
|
||||||
* @throws ConfigException
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public static function drop(string $table, $connection = null): bool|int
|
|
||||||
{
|
|
||||||
$connection = static::getDefaultConnection($connection);
|
|
||||||
|
|
||||||
$sprint = sprintf('DROP TABLE' . ' `%s`.`%s`', $connection->database, $table);
|
|
||||||
return $connection->createCommand($sprint)->delete();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $table
|
|
||||||
* @param null $connection
|
|
||||||
* @return bool|int
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public static function truncate(string $table, $connection = null): bool|int
|
|
||||||
{
|
|
||||||
$connection = static::getDefaultConnection($connection);
|
|
||||||
|
|
||||||
$sprint = sprintf('TRUNCATE `%s`.`%s`', $connection->database, $table);
|
|
||||||
return $connection->createCommand($sprint)->exec();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $table
|
|
||||||
* @param Connection|NULL $connection
|
|
||||||
* @return array|bool|null
|
|
||||||
* @throws ConfigException
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public static function showCreateSql(string $table, Connection $connection = NULL): array|bool|null
|
|
||||||
{
|
|
||||||
$connection = static::getDefaultConnection($connection);
|
|
||||||
|
|
||||||
$sprint = sprintf('SHOW CREATE TABLE `%s`.`%s`', $connection->database, $table);
|
|
||||||
return $connection->createCommand($sprint)->one();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $table
|
|
||||||
* @param Connection|NULL $connection
|
|
||||||
* @return array|bool|null
|
|
||||||
* @throws ConfigException
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public static function desc(string $table, Connection $connection = NULL): array|bool
|
|
||||||
{
|
|
||||||
$connection = static::getDefaultConnection($connection);
|
|
||||||
|
|
||||||
$sprint = sprintf('SHOW FULL FIELDS FROM `%s`.`%s`', $connection->database, $table);
|
|
||||||
return $connection->createCommand($sprint)->all();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $table
|
* @param Closure $closure
|
||||||
* @param Connection|NULL $connection
|
* @param mixed ...$params
|
||||||
* @return array|bool|null
|
* @return mixed
|
||||||
* @throws Exception
|
* @throws ContainerExceptionInterface
|
||||||
*/
|
* @throws NotFoundExceptionInterface
|
||||||
public static function show(string $table, Connection $connection = NULL): array|bool|null
|
* @throws Exception
|
||||||
{
|
*/
|
||||||
if ($table == '') {
|
public static function Transaction(Closure $closure, ...$params): mixed
|
||||||
return null;
|
{
|
||||||
}
|
static::beginTransaction();
|
||||||
$connection = static::getDefaultConnection($connection);
|
try {
|
||||||
|
$result = call_user_func($closure, ...$params);
|
||||||
$table = [' const TABLE = \'select * from %s where REFERENCED_TABLE_NAME=%s\';'];
|
} catch (Throwable $throwable) {
|
||||||
return $connection->createCommand((new Query())
|
error($throwable);
|
||||||
->select('*')
|
$result = addError($throwable->getMessage(), 'mysql');
|
||||||
->from('INFORMATION_SCHEMA.KEY_COLUMN_USAGE')
|
} finally {
|
||||||
->where(['REFERENCED_TABLE_NAME' => $table])
|
if ($result === false) {
|
||||||
->getSql())->one();
|
static::rollback();
|
||||||
}
|
} else {
|
||||||
|
static::commit();
|
||||||
|
}
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param null|Connection $connection
|
* @return void
|
||||||
* @param string $name
|
*/
|
||||||
* @return mixed
|
public static function commit(): void
|
||||||
* @throws Exception
|
{
|
||||||
*/
|
fire(new Commit());
|
||||||
public static function getDefaultConnection(?Connection $connection, string $name = 'db'): Connection
|
}
|
||||||
{
|
|
||||||
if ($connection instanceof Connection) {
|
|
||||||
return $connection;
|
/**
|
||||||
}
|
* @return void
|
||||||
$databases = \config('databases.connections', []);
|
*/
|
||||||
if (empty($databases) || !is_array($databases)) {
|
public static function rollback(): void
|
||||||
throw new Exception('Please configure the database link.');
|
{
|
||||||
}
|
fire(new Rollback());
|
||||||
return \Kiri::service()->get($databases[$name]);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $table
|
||||||
|
*
|
||||||
|
* @return static
|
||||||
|
*/
|
||||||
|
public static function table($table): Db|static
|
||||||
|
{
|
||||||
|
$connection = new Db();
|
||||||
|
$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
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function get(): array|bool
|
||||||
|
{
|
||||||
|
return $this->connection->createCommand(SqlBuilder::builder($this)->all())->all();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array|bool|null
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function first(): array|bool|null
|
||||||
|
{
|
||||||
|
return $this->connection->createCommand(SqlBuilder::builder($this)->all())->one();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool|int
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function count(): bool|int
|
||||||
|
{
|
||||||
|
return $this->connection->createCommand(SqlBuilder::builder($this)->count())->one()['row_count'];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool|int
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
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
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
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
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function one(string $sql, array $attributes = []): int|bool|array|string|null
|
||||||
|
{
|
||||||
|
return $this->connection->createCommand($sql, $attributes)->one();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool|int
|
||||||
|
* @throws ConfigException
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function delete(): bool|int
|
||||||
|
{
|
||||||
|
return $this->connection->createCommand(SqlBuilder::builder($this)->delete())->delete();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $table
|
||||||
|
* @return array|bool|null
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param null|Connection $connection
|
||||||
|
* @param string $name
|
||||||
|
* @return mixed
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public static function getDefaultConnection(?Connection $connection = null, string $name = 'db'): Connection
|
||||||
|
{
|
||||||
|
if ($connection instanceof Connection) {
|
||||||
|
return $connection;
|
||||||
|
}
|
||||||
|
$databases = \config('databases.connections', []);
|
||||||
|
if (empty($databases) || !is_array($databases)) {
|
||||||
|
throw new Exception('Please configure the database link.');
|
||||||
|
}
|
||||||
|
return Kiri::service()->get($databases[$name]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -229,7 +229,7 @@ class SqlBuilder extends Component
|
|||||||
*/
|
*/
|
||||||
public function count(): string
|
public function count(): string
|
||||||
{
|
{
|
||||||
return $this->_selectPrefix(['COUNT(*)']) . $this->_prefix();
|
return $this->_selectPrefix(['COUNT(*) as row_count']) . $this->_prefix();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user