Update DB\Sqlite

This commit is contained in:
Visman 2021-12-29 17:14:32 +07:00
parent 768074c78e
commit bfecbd573c

View file

@ -335,6 +335,33 @@ class Sqlite
return false !== $this->db->exec($createQuery) ? $tmpTable : null;
}
/**
* Пересоздает таблицу из временной с помощью insert запроса
*/
protected function tmpToTable(array $schema, string $insertQuery): bool
{
if (! \preg_match('%^INSERT INTO "(.*?)".+FROM "(.*?)"%s', $insertQuery, $matches)) {
return false;
}
$tmpTable = $matches[1];
$table = $matches[2];
$result = false !== $this->db->exec($insertQuery);
$result = $result && $this->dropTable($table);
$result = $result && $this->renameTable($tmpTable, $table);
foreach ($schema as $key => $query) {
if ('TABLE' === $key) {
continue;
}
$result = $result && false !== $this->db->exec($query);
}
return $result;
}
/**
* Проверяет наличие таблицы в базе
*/
@ -532,19 +559,7 @@ class Sqlite
SELECT {$tmp}
FROM \"{$table}\"";
$result = $result && false !== $this->db->exec($query);
$result = $result && $this->dropTable($table);
$result = $result && $this->renameTable($tmpTable, $table);
foreach ($schema as $key => $query) {
if ('TABLE' === $key) {
continue;
}
$result = $result && false !== $this->db->exec($query);
}
return $result;
return $result && $this->tmpToTable($schema, $query);
}
/**
@ -591,19 +606,7 @@ class Sqlite
SELECT {$tmp}
FROM \"{$table}\"";
$result = $result && false !== $this->db->exec($query);
$result = $result && $this->dropTable($table);
$result = $result && $this->renameTable($tmpTable, $table);
foreach ($schema as $key => $query) {
if ('TABLE' === $key) {
continue;
}
$result = $result && false !== $this->db->exec($query);
}
return $result;
return $result && $this->tmpToTable($schema, $query);
} else {
$index = $table . '_' . $index;