109 lines
3.5 KiB
PHP
Executable File
109 lines
3.5 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
use Symfony\Component\Console\Input\InputArgument;
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
use Illuminate\Database\Capsule\Manager as Capsule;
|
|
|
|
class DbImportCommand extends Command
|
|
{
|
|
protected static $defaultName = 'db:import';
|
|
protected static $defaultDescription = 'Importa um arquivo SQL para o banco de dados';
|
|
|
|
protected function configure(): void
|
|
{
|
|
$this
|
|
->addArgument('file', InputArgument::REQUIRED, 'O caminho do arquivo SQL a ser importado');
|
|
}
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
|
{
|
|
$file = $input->getArgument('file');
|
|
|
|
if (!file_exists($file)) {
|
|
$output->writeln("<error>Arquivo não encontrado: {$file}</error>");
|
|
return Command::FAILURE;
|
|
}
|
|
|
|
$output->writeln("<info>Importando arquivo: {$file}</info>");
|
|
|
|
try {
|
|
$sql = file_get_contents($file);
|
|
if (!$sql) {
|
|
$output->writeln("<error>Falha ao ler o arquivo SQL ou arquivo vazio.</error>");
|
|
return Command::FAILURE;
|
|
}
|
|
|
|
$connection = Capsule::connection();
|
|
$queries = $this->splitSqlQueries($sql);
|
|
$totalQueries = count($queries);
|
|
$successCount = 0;
|
|
|
|
$output->writeln("<comment>Total de queries encontradas: {$totalQueries}</comment>");
|
|
|
|
foreach ($queries as $index => $query) {
|
|
$query = trim($query);
|
|
if (empty($query)) {
|
|
continue;
|
|
}
|
|
|
|
try {
|
|
$connection->statement($query);
|
|
$successCount++;
|
|
$output->writeln("<info>Query " . ($index + 1) . "/{$totalQueries} executada com sucesso</info>");
|
|
} catch (\Exception $e) {
|
|
$output->writeln("<error>Erro na query " . ($index + 1) . ": " . $e->getMessage() . "</error>");
|
|
$output->writeln("<comment>Query: " . substr($query, 0, 100) . "...</comment>");
|
|
}
|
|
}
|
|
|
|
$output->writeln("<info>Importação concluída. {$successCount}/{$totalQueries} queries executadas com sucesso.</info>");
|
|
return Command::SUCCESS;
|
|
} catch (\Exception $e) {
|
|
$output->writeln("<error>Erro ao importar arquivo: " . $e->getMessage() . "</error>");
|
|
return Command::FAILURE;
|
|
}
|
|
}
|
|
|
|
private function splitSqlQueries(string $sql): array
|
|
{
|
|
$queries = [];
|
|
$current = '';
|
|
$inString = false;
|
|
$stringChar = '';
|
|
$length = strlen($sql);
|
|
|
|
for ($i = 0; $i < $length; $i++) {
|
|
$char = $sql[$i];
|
|
|
|
if (($char === "'" || $char === '"') && ($i === 0 || $sql[$i - 1] !== '\\')) {
|
|
if (!$inString) {
|
|
$inString = true;
|
|
$stringChar = $char;
|
|
} elseif ($char === $stringChar) {
|
|
$inString = false;
|
|
}
|
|
}
|
|
|
|
if (!$inString && $char === ';') {
|
|
$queries[] = trim($current);
|
|
$current = '';
|
|
} else {
|
|
$current .= $char;
|
|
}
|
|
}
|
|
|
|
$current = trim($current);
|
|
if (!empty($current)) {
|
|
$queries[] = $current;
|
|
}
|
|
|
|
return array_filter($queries, function($query) {
|
|
return !empty(trim($query));
|
|
});
|
|
}
|
|
}
|