Skip to content

Commit

Permalink
Updaate
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Neto committed Jul 28, 2024
1 parent 623829f commit 4981e34
Showing 1 changed file with 33 additions and 10 deletions.
43 changes: 33 additions & 10 deletions install/mysqlRestore.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,33 +90,48 @@ function executeFile($filename) {
}
}

// Modificar comandos CREATE TABLE para CREATE TABLE IF NOT EXISTS
foreach ($createTableCommands as &$command) {
if (stripos($command, 'CREATE TABLE') !== false && stripos($command, 'IF NOT EXISTS') === false) {
$command = str_ireplace('CREATE TABLE', 'CREATE TABLE IF NOT EXISTS', $command);
}
}

// Executar DROP TABLE IF EXISTS separado de CREATE TABLE
foreach ($tables as $table) {
$dropTableCommand = 'DROP TABLE IF EXISTS `' . $table . '`;';
echo "Executing: $dropTableCommand\n"; // Imprimir o comando SQL
try {
if (!$global['mysqli']->query($dropTableCommand)) {
echo ('sqlDAL::executeFile ' . $filename . ' Error performing query \'<strong>' . $dropTableCommand . '\': ' . $global['mysqli']->error . '<br /><br />');
throw new Exception($global['mysqli']->error);
}
} catch (\Throwable $th) {
echo 'Error: '.$th->getMessage().PHP_EOL;
} catch (Exception $e) {
echo ('sqlDAL::executeFile ' . $filename . ' Error performing query \'<strong>' . $dropTableCommand . '\': ' . $e->getMessage() . '<br /><br />');
}
}

// Executar comandos de criação de tabela
foreach ($createTableCommands as $command) {
echo "Executing: $command\n"; // Imprimir o comando SQL
if (!$global['mysqli']->query($command)) {
echo ('sqlDAL::executeFile ' . $filename . ' Error performing query \'<strong>' . $command . '\': ' . $global['mysqli']->error . '<br /><br />');
try {
if (!$global['mysqli']->query($command)) {
throw new Exception($global['mysqli']->error);
}
} catch (Exception $e) {
echo ('sqlDAL::executeFile ' . $filename . ' Error performing query \'<strong>' . $command . '\': ' . $e->getMessage() . '<br /><br />');
}
}

// Adicionar LOCK TABLES para todas as tabelas identificadas
if (!empty($tables)) {
$lockTables = 'LOCK TABLES ' . implode(' WRITE, ', $tables) . ' WRITE;';
echo "Executing: $lockTables\n"; // Imprimir o comando SQL
if (!$global['mysqli']->query($lockTables)) {
echo ('sqlDAL::executeFile ' . $filename . ' Error performing query \'<strong>' . $lockTables . '\': ' . $global['mysqli']->error . '<br /><br />');
try {
if (!$global['mysqli']->query($lockTables)) {
throw new Exception($global['mysqli']->error);
}
} catch (Exception $e) {
echo ('sqlDAL::executeFile ' . $filename . ' Error performing query \'<strong>' . $lockTables . '\': ' . $e->getMessage() . '<br /><br />');
return;
}
}
Expand All @@ -125,12 +140,20 @@ function executeFile($filename) {
foreach ($commands as $command) {
if (!in_array($command, $createTableCommands)) {
echo "Executing: $command\n"; // Imprimir o comando SQL
if (!$global['mysqli']->query($command)) {
echo ('sqlDAL::executeFile ' . $filename . ' Error performing query \'<strong>' . $command . '\': ' . $global['mysqli']->error . '<br /><br />');
try {
if (!$global['mysqli']->query($command)) {
throw new Exception($global['mysqli']->error);
}
} catch (Exception $e) {
echo ('sqlDAL::executeFile ' . $filename . ' Error performing query \'<strong>' . $command . '\': ' . $e->getMessage() . '<br /><br />');
}
}
}

// Desbloquear as tabelas no final
$global['mysqli']->query('UNLOCK TABLES;');
try {
$global['mysqli']->query('UNLOCK TABLES;');
} catch (Exception $e) {
echo ('sqlDAL::executeFile ' . $filename . ' Error performing query \'<strong>UNLOCK TABLES</strong>\': ' . $e->getMessage() . '<br /><br />');
}
}

0 comments on commit 4981e34

Please sign in to comment.