From 313092dd75e522a9f65ec81e9e3eba42c289e6ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Sat, 30 Sep 2023 19:32:41 +0800 Subject: [PATCH] eee --- DatabasesProviders.php | 1 + ImplodeCommand.php | 97 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 ImplodeCommand.php diff --git a/DatabasesProviders.php b/DatabasesProviders.php index e2034b9..1faec0f 100644 --- a/DatabasesProviders.php +++ b/DatabasesProviders.php @@ -26,6 +26,7 @@ class DatabasesProviders extends Providers { $main = Kiri::getDi()->get(Kiri\Application::class); $main->command(BackupCommand::class); + $main->command(ImplodeCommand::class); $databases = \config('databases.connections', []); if (empty($databases)) { diff --git a/ImplodeCommand.php b/ImplodeCommand.php new file mode 100644 index 0000000..8511904 --- /dev/null +++ b/ImplodeCommand.php @@ -0,0 +1,97 @@ +service = \Kiri::getDi()->get(LocalService::class); + $this->setName('db:backup') + ->addOption('data', 'd', InputArgument::OPTIONAL) + ->addArgument('path', InputArgument::REQUIRED, "save to path", null) + ->addOption('database', 'db', InputArgument::OPTIONAL) + ->setDescription('php kiri.php db:backup --database users --table u_user --data 1 /Users/admin/snowflake-bi/test.sql'); + } + + + /** + * @param InputInterface $input + * @param OutputInterface $output + * @return int + * @throws Exception + */ + public function execute(InputInterface $input, OutputInterface $output): int + { + try { + /** @var Connection $data */ + $data = $this->service->get($input->getOption('database')); + + $path = $input->getArgument('path'); + if (!str_starts_with($path, '/')) { + $path = APP_PATH . $path; + } + + $waite = new Coroutine\WaitGroup(); + + + $stream = fopen($path, 'r'); + if (!$stream) { + return 0; + } + while (($line = fgets($stream)) !== false) { + $insert = html_entity_decode($line); + if (!str_starts_with(strtoupper($insert), 'INSERT INTO')) { + continue; + } + Coroutine::create(function () use ($waite, $insert, $data) { + Coroutine\defer(fn() => $waite->done()); + $exec = $data->createCommand($insert)->exec(); + exit('exec sql result: ' . $exec); + }); + } + fclose($stream); + + + $waite->wait(); + + $output->write('dump data success'); + } catch (\Throwable $throwable) { + $output->writeln(throwable($throwable)); + } finally { + return 1; + } + } + +}