This commit is contained in:
2023-11-30 17:02:20 +08:00
parent c352c46a0f
commit 501ace9e36
5 changed files with 43 additions and 44 deletions
+3 -6
View File
@@ -5,7 +5,6 @@ namespace Database;
use Co\Channel;
use Exception;
use Kiri\Di\LocalService;
use Swoole\Coroutine;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
@@ -25,7 +24,6 @@ class BackupCommand extends Command
public string $description = 'php kiri.php db:backup --database users --table u_user --data 1 /Users/admin/snowflake-bi/test.sql';
private LocalService $service;
public array $percentStatus = [];
@@ -35,7 +33,6 @@ class BackupCommand extends Command
*/
protected function configure()
{
$this->service = \Kiri::getDi()->get(LocalService::class);
$this->setName('db:backup')
->addOption('data', 'd', InputArgument::OPTIONAL)
->addArgument('path', InputArgument::REQUIRED, "save to path", null)
@@ -55,7 +52,7 @@ class BackupCommand extends Command
{
try {
/** @var Connection $data */
$data = $this->service->get($input->getOption('database'));
$data = \Kiri::getDi()->get(DatabasesProviders::class)->get($input->getOption('database'));
$table = $input->getOption('table');
if ($table !== null) {
@@ -159,7 +156,7 @@ class BackupCommand extends Command
});
/** @var Connection $database */
$database = \Kiri::service()->get($dbname);
$database = \Kiri::getDi()->get(DatabasesProviders::class)->get($dbname);
$total = $database->createCommand("SELECT COUNT(*) as total FROM " . $tableName)->one()['total'];
@@ -173,7 +170,7 @@ class BackupCommand extends Command
$wait->done();
});
/** @var Connection $database */
$database = \Kiri::service()->get($dbname);
$database = \Kiri::getDi()->get(DatabasesProviders::class)->get($dbname);
$data = $database->createCommand("SELECT * FROM $tableName LIMIT $offset,$size")->all();
if (is_bool($data) || count($data) < 1) {
+10 -16
View File
@@ -17,6 +17,7 @@ use ArrayAccess;
use Database\ActiveQuery;
use Database\Collection;
use Database\Connection;
use Database\DatabasesProviders;
use Database\ModelInterface;
use Database\Mysql\Columns;
use Database\Relation;
@@ -330,7 +331,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
*/
public function getConnection(): Connection
{
return Kiri::service()->get($this->connection);
return Kiri::getDi()->get(DatabasesProviders::class)->get($this->connection);
}
@@ -432,17 +433,14 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
private function insert(): bool|static
{
[$sql, $param] = SqlBuilder::builder(static::query())->insert($this->_attributes);
$connection = $this->getConnection();
$dbConnection = $connection->createCommand($sql, $param);
$lastId = $dbConnection->save();
$lastId = $this->getConnection()->createCommand($sql, $param)->save();
if ($lastId === false) {
return false;
} else {
if ($this->hasPrimary()) {
$this->_attributes[$this->getPrimary()] = $lastId;
}
return $this;
}
if ($this->hasPrimary()) {
$this->_attributes[$this->getPrimary()] = $lastId;
}
return $this;
}
@@ -463,14 +461,10 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
if ($generate === false) {
return false;
}
$connection = $this->getConnection();
$command = $connection->createCommand($generate, $query->attributes);
if ($command->save()) {
return $this->refresh()->afterSave($old, $change);
} else {
if (!$this->getConnection()->createCommand($generate, $query->attributes)->save()) {
return FALSE;
}
return $this->refresh()->afterSave($old, $change);
}
/**
@@ -592,7 +586,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
*/
public function getTable(): string
{
$connection = static::getConnection();
$connection = $this->getConnection();
$tablePrefix = $connection->tablePrefix;
if (empty($this->table)) {
throw new Exception('You need add static method `tableName` and return table name.');
+23 -9
View File
@@ -7,7 +7,6 @@ namespace Database;
use Exception;
use Kiri;
use Kiri\Abstracts\Providers;
use Kiri\Di\LocalService;
/**
* Class DatabasesProviders
@@ -18,22 +17,25 @@ class DatabasesProviders extends Providers
/**
* @param LocalService $application
* @var array
*/
protected array $connections = [];
/**
* @return void
* @throws
*/
public function onImport(LocalService $application): void
public function onImport(): void
{
$main = Kiri::getDi()->get(Kiri\Application::class);
$main->command(BackupCommand::class);
$main->command(ImplodeCommand::class);
$main->command(BackupCommand::class, ImplodeCommand::class);
$databases = \config('databases.connections', []);
if (empty($databases)) {
if (count($databases) < 1) {
return;
}
foreach ($databases as $key => $database) {
$application->set($key, Kiri::createObject($this->_settings($database)));
$this->set($key, $this->_settings($database));
}
}
@@ -45,7 +47,19 @@ class DatabasesProviders extends Providers
*/
public function get($name): Connection
{
return Kiri::service()->get($name);
return $this->connections[$name];
}
/**
* @param $key
* @param array $connection
* @return void
* @throws Exception
*/
protected function set($key, array $connection): void
{
$this->connections[$key] = Kiri::createObject($connection);
}
+6 -5
View File
@@ -49,7 +49,7 @@ class Db implements ISqlBuilder
{
$db = new Db();
if (is_string($dbname)) {
$dbname = Kiri::service()->get($dbname);
$dbname = \Kiri::getDi()->get(DatabasesProviders::class)->get($dbname);
}
$db->connection = $dbname;
return $db;
@@ -118,11 +118,11 @@ class Db implements ISqlBuilder
/**
* @param $table
*
* @param string $table
* @param string $database
* @return static
*/
public static function table($table): Db|static
public static function table(string $table, string $database = 'db'): Db|static
{
$connection = new Db();
$connection->connection = current(\config('databases.connections'));
@@ -264,10 +264,11 @@ class Db implements ISqlBuilder
return $connection;
}
$databases = \config('databases.connections', []);
$providers = \Kiri::getDi()->get(DatabasesProviders::class);
if (empty($databases) || !is_array($databases)) {
throw new Exception('Please configure the database link.');
}
return Kiri::service()->get($databases[$database]);
return $providers->get($databases[$database]);
}
+1 -8
View File
@@ -5,7 +5,6 @@ namespace Database;
use Co\Channel;
use Exception;
use Kiri\Di\LocalService;
use Swoole\Coroutine;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
@@ -32,11 +31,6 @@ class ImplodeCommand extends Command
public string $description = 'php kiri.php db:implode --database users /Users/admin/snowflake-bi/test.sql';
/**
* @var LocalService
*/
private LocalService $service;
/**
* @var Channel
@@ -51,7 +45,6 @@ class ImplodeCommand extends Command
*/
protected function configure()
{
$this->service = \Kiri::getDi()->get(LocalService::class);
$this->setName('db:implode')
->addArgument('path', InputArgument::REQUIRED, "save to path", null)
->addOption('database', 'db', InputArgument::OPTIONAL)
@@ -70,7 +63,7 @@ class ImplodeCommand extends Command
{
try {
/** @var Connection $data */
$data = $this->service->get($input->getOption('database'));
$data = \Kiri::getDi()->get(DatabasesProviders::class)->get($input->getOption('database'));
$path = $input->getArgument('path');
if (!str_starts_with($path, '/')) {