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