From 098718acff1c693894055ad0f2383f15e88b8689 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= <715557344@qq.com> Date: Mon, 15 Jul 2024 18:01:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=98=AF=E5=90=A6=E4=B8=80?= =?UTF-8?q?=E6=AC=A1=E6=80=A7=E5=AF=BC=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 1 + src/Importer.php | 51 ++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/composer.json b/composer.json index 6e2bebf..f9bc8ca 100644 --- a/composer.json +++ b/composer.json @@ -19,6 +19,7 @@ }, "require": { "php": ">=8.1", + "ext-pdo": "*", "hyperf/database": "^3.0", "hyperf/db-connection": "^3.0" }, diff --git a/src/Importer.php b/src/Importer.php index 1d6d5e6..6ac3475 100644 --- a/src/Importer.php +++ b/src/Importer.php @@ -14,6 +14,7 @@ use Fan\SqlImport\Exception\NotFoundException; use Hyperf\Database\Connectors\ConnectionFactory; +use PDO; use Psr\Container\ContainerInterface; use Throwable; @@ -26,19 +27,48 @@ public function __construct(protected ContainerInterface $container) $this->factory = $this->container->get(ConnectionFactory::class); } - public function import(array $config, string $sql): Result + /** + * @param bool $once 是否一次性导入 + */ + public function import(array $config, string $sql, bool $once = true): Result { $connection = $this->factory->make($config); + $pdo = $connection->getPdo(); + if ($once) { + return new Result($this->importSql($pdo, $sql)); + } - $res = $connection->getPdo()->exec($sql); - if ($res === false) { - return new Result(false); + $sqls = explode("\n", $sql); + $sql = ''; + $result = new Result(true); + foreach ($sqls as $line) { + try { + if (str_starts_with($line, '--') || $line == '') { + continue; + } + + // Add this line to the current segment + $sql .= $line; + + // If it has a semicolon at the end, it's the end of the query + if (str_ends_with(trim($line), ';')) { + $pdo->exec($sql); + $sql = ''; + } + } catch (Throwable $exception) { + $result->isSuccess = false; + $result->failedSqls[] = new FailedSql($sql, $exception->getMessage()); + $sql = ''; + } } - return new Result(true); + return $result; } - public function importPath(array $config, string $path): Result + /** + * @param bool $once 是否一次性导入 + */ + public function importPath(array $config, string $path, bool $once = false): Result { // if file cannot be found throw errror if (! file_exists($path)) { @@ -46,6 +76,10 @@ public function importPath(array $config, string $path): Result } $pdo = $this->factory->make($config)->getPdo(); + if ($once) { + return new Result($this->importSql($pdo, file_get_contents($path))); + } + $fp = fopen($path, 'r'); $sql = ''; $result = new Result(true); @@ -72,4 +106,9 @@ public function importPath(array $config, string $path): Result fclose($fp); return $result; } + + protected function importSql(PDO $pdo, string $sql): bool + { + return $pdo->exec($sql) !== false; + } }