refactor: Название методов
This commit is contained in:
parent
82f6dd1630
commit
9680409ba9
2 changed files with 31 additions and 32 deletions
|
|
@ -16,27 +16,27 @@ class Manager
|
||||||
$this->db = $db;
|
$this->db = $db;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function ExecuteAction($action/*: array*/, $db_file = "")
|
public function executeAction($action/*: array*/, $db_file = "")
|
||||||
{
|
{
|
||||||
switch($action["type"]) {
|
switch($action["type"]) {
|
||||||
case "dropTable":
|
case "dropTable":
|
||||||
$this->DropTableQuery($action["table_name"], true);
|
$this->dropTableQuery($action["table_name"], true);
|
||||||
break;
|
break;
|
||||||
case "createTable":
|
case "createTable":
|
||||||
$constraints = $action["constraints"] ?? null;
|
$constraints = $action["constraints"] ?? null;
|
||||||
$this->CreateTableQuery($action["table_name"], $action["fields"], $constraints);
|
$this->createTableQuery($action["table_name"], $action["fields"], $constraints);
|
||||||
break;
|
break;
|
||||||
case "addColumn":
|
case "addColumn":
|
||||||
$this->AddColumn($action["table_name"], $action["column_name"], $action["field"]);
|
$this->addColumn($action["table_name"], $action["column_name"], $action["field"]);
|
||||||
break;
|
break;
|
||||||
case "insert":
|
case "insert":
|
||||||
$this->db->insertQuery($action["table_name"], $action["values"]);
|
$this->db->insertQuery($action["table_name"], $action["values"]);
|
||||||
break;
|
break;
|
||||||
case "alterReference":
|
case "alterReference":
|
||||||
$this->AlterReference($action["table"], $action["column"], $action["refTable"], $action["refColumn"]);
|
$this->alterReference($action["table"], $action["column"], $action["refTable"], $action["refColumn"]);
|
||||||
break;
|
break;
|
||||||
case "renameColumn":
|
case "renameColumn":
|
||||||
$this->RenameColumn($action["table"], $action["old_name"], $action["new_name"]);
|
$this->renameColumn($action["table"], $action["old_name"], $action["new_name"]);
|
||||||
break;
|
break;
|
||||||
case "createView":
|
case "createView":
|
||||||
$this->recreateView($action["view"], $action["select"]);
|
$this->recreateView($action["view"], $action["select"]);
|
||||||
|
|
@ -66,7 +66,7 @@ class Manager
|
||||||
$this->db->query("CREATE VIEW ".$viewName." AS ".$selectStatement);
|
$this->db->query("CREATE VIEW ".$viewName." AS ".$selectStatement);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function DropTableQuery($table, $cascade=false)
|
public function dropTableQuery($table, $cascade=false)
|
||||||
{
|
{
|
||||||
$statement = "DROP TABLE IF EXISTS ".$table;
|
$statement = "DROP TABLE IF EXISTS ".$table;
|
||||||
if ($this->db->isPostgres()&&$cascade) {
|
if ($this->db->isPostgres()&&$cascade) {
|
||||||
|
|
@ -75,13 +75,13 @@ class Manager
|
||||||
$this->db->query($statement);
|
$this->db->query($statement);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function AlterReference($table, $column, $refTable, $refColumn)
|
public function alterReference($table, $column, $refTable, $refColumn)
|
||||||
{
|
{
|
||||||
$this->db->query("ALTER TABLE ".$table." ADD CONSTRAINT ".$table."_".$column."fk"." FOREIGN KEY (".$column.") REFERENCES ".$refTable." (".$refColumn.")");
|
$this->db->query("ALTER TABLE ".$table." ADD CONSTRAINT ".$table."_".$column."fk"." FOREIGN KEY (".$column.") REFERENCES ".$refTable." (".$refColumn.")");
|
||||||
}
|
}
|
||||||
|
|
||||||
//Извлечение информации о полях таблицы
|
//Извлечение информации о полях таблицы
|
||||||
public function TableInfo($table)
|
public function tableInfo($table)
|
||||||
{
|
{
|
||||||
$pg = $this->db->isPostgres();
|
$pg = $this->db->isPostgres();
|
||||||
if ($pg) {
|
if ($pg) {
|
||||||
|
|
@ -103,26 +103,26 @@ class Manager
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function RenameColumn($table, $old_name, $new_name)
|
public function renameColumn($table, $old_name, $new_name)
|
||||||
{
|
{
|
||||||
$pg = $this->db->isPostgres();
|
$pg = $this->db->isPostgres();
|
||||||
if ($pg) {
|
if ($pg) {
|
||||||
$this->db->query("ALTER TABLE ".$table." RENAME COLUMN ".$old_name." TO ".$new_name);
|
$this->db->query("ALTER TABLE ".$table." RENAME COLUMN ".$old_name." TO ".$new_name);
|
||||||
} else {
|
} else {
|
||||||
$tmp_table = "tmp_" . $table;
|
$tmp_table = "tmp_" . $table;
|
||||||
$this->DropTableQuery($tmp_table);
|
$this->dropTableQuery($tmp_table);
|
||||||
$table_info = $this->TableInfo($table);
|
$table_info = $this->tableInfo($table);
|
||||||
|
|
||||||
if (isset($table_info[$new_name])) {
|
if (isset($table_info[$new_name])) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$data/*: array*/ = $this->DumpTable($table);
|
$data/*: array*/ = $this->dumpTable($table);
|
||||||
|
|
||||||
$this->db->query("ALTER TABLE ".$table." RENAME TO ".$tmp_table.";");
|
$this->db->query("ALTER TABLE ".$table." RENAME TO ".$tmp_table.";");
|
||||||
$table_info[$new_name] = $table_info[$old_name];
|
$table_info[$new_name] = $table_info[$old_name];
|
||||||
unset($table_info[$old_name]);
|
unset($table_info[$old_name]);
|
||||||
$this->CreateTableQuery($table, $table_info, null);
|
$this->createTableQuery($table, $table_info, null);
|
||||||
|
|
||||||
foreach ($data as $row) {
|
foreach ($data as $row) {
|
||||||
$values = $row['values'];
|
$values = $row['values'];
|
||||||
|
|
@ -130,17 +130,17 @@ class Manager
|
||||||
unset($values[$old_name]);
|
unset($values[$old_name]);
|
||||||
$this->db->insertQuery($table, $values);
|
$this->db->insertQuery($table, $values);
|
||||||
}
|
}
|
||||||
$this->DropTableQuery($tmp_table);
|
$this->dropTableQuery($tmp_table);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Обновление ключа serial после ручной вставки
|
//Обновление ключа serial после ручной вставки
|
||||||
public function UpdateSerial($table, $column)
|
public function updateSerial($table, $column)
|
||||||
{
|
{
|
||||||
$this->db->query("SELECT setval(pg_get_serial_sequence('".$table."', '".$column."'), coalesce(max(".$column."),0) + 1, false) FROM ".$table);
|
$this->db->query("SELECT setval(pg_get_serial_sequence('".$table."', '".$column."'), coalesce(max(".$column."),0) + 1, false) FROM ".$table);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Column_Definition($name, $data, $pg)
|
public function columnDefinition($name, $data, $pg)
|
||||||
{
|
{
|
||||||
$constraint = isset($data['constraint']) ? " ".$data['constraint'] : "";
|
$constraint = isset($data['constraint']) ? " ".$data['constraint'] : "";
|
||||||
$references = "";
|
$references = "";
|
||||||
|
|
@ -161,11 +161,11 @@ class Manager
|
||||||
return $name." ".$type.$references.$constraint;
|
return $name." ".$type.$references.$constraint;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function AddColumn($table_name, $column_name, $field)
|
public function addColumn($table_name, $column_name, $field)
|
||||||
{
|
{
|
||||||
$pg = $this->db->isPostgres();
|
$pg = $this->db->isPostgres();
|
||||||
$q = "ALTER TABLE ".$table_name." ADD COLUMN ".
|
$q = "ALTER TABLE ".$table_name." ADD COLUMN ".
|
||||||
$this->Column_Definition($column_name, $field, $pg);
|
$this->columnDefinition($column_name, $field, $pg);
|
||||||
$this->db->query($q);
|
$this->db->query($q);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -178,7 +178,7 @@ class Manager
|
||||||
}
|
}
|
||||||
|
|
||||||
//CreateTableQuery('users',['id'=>['type'=>'integer','constraint'=>'PRIMARY KEY']])
|
//CreateTableQuery('users',['id'=>['type'=>'integer','constraint'=>'PRIMARY KEY']])
|
||||||
public function CreateTableQuery($table, $fields, $constraints)
|
public function createTableQuery($table, $fields, $constraints)
|
||||||
{
|
{
|
||||||
$pg = $this->db->isPostgres();
|
$pg = $this->db->isPostgres();
|
||||||
if ($constraints) {
|
if ($constraints) {
|
||||||
|
|
@ -191,26 +191,25 @@ class Manager
|
||||||
$statement = "CREATE TABLE $table (" . implode(
|
$statement = "CREATE TABLE $table (" . implode(
|
||||||
",",
|
",",
|
||||||
array_map(function ($name, $data) use ($pg) {
|
array_map(function ($name, $data) use ($pg) {
|
||||||
return $this->Column_Definition($name, $data, $pg);
|
return $this->columnDefinition($name, $data, $pg);
|
||||||
}, array_keys($fields), array_values($fields))
|
}, array_keys($fields), array_values($fields))
|
||||||
) . " " . $constraints . ")";
|
) . " " . $constraints . ")";
|
||||||
$this->db->query($statement);
|
$this->db->query($statement);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function DumpTable($table_name)
|
public function dumpTable($table_name)
|
||||||
{
|
{
|
||||||
$pg = $this->db->isPostgres();
|
$pg = $this->db->isPostgres();
|
||||||
|
|
||||||
$result/*: array*/ = [];
|
$result = [];
|
||||||
$data/*: array*/ = $this->db->fetchAllArray("SELECT * FROM ".$table_name.";");
|
$data = $this->db->fetchAllArray("SELECT * FROM ".$table_name.";");
|
||||||
|
|
||||||
if (!$pg) {
|
if (!$pg) {
|
||||||
$table_fields = $this->TableInfo($table_name);
|
$table_fields = $this->tableInfo($table_name);
|
||||||
foreach ($table_fields as $name => $value) {
|
foreach ($table_fields as $name => $value) {
|
||||||
$type = strtolower($value['type']);
|
$type = strtolower($value['type']);
|
||||||
if ($type == "boolean") {
|
if ($type == "boolean") {
|
||||||
foreach ($data as &$row) {
|
foreach ($data as &$row) {
|
||||||
$row/*: array*/ = $row;
|
|
||||||
if (isset($row[$name])) {
|
if (isset($row[$name])) {
|
||||||
$row[$name] = boolval($row[$name]);
|
$row[$name] = boolval($row[$name]);
|
||||||
}
|
}
|
||||||
|
|
@ -228,7 +227,7 @@ class Manager
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function GetAllTableNames()
|
public function getAllTableNames()
|
||||||
{
|
{
|
||||||
$result = [];
|
$result = [];
|
||||||
if ($this->db->isPostgres()) {
|
if ($this->db->isPostgres()) {
|
||||||
|
|
@ -243,12 +242,12 @@ class Manager
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function DumpInserts()
|
public function dumpInserts()
|
||||||
{
|
{
|
||||||
$table_names = $this->GetAllTableNames();
|
$table_names = $this->getAllTableNames();
|
||||||
$result = [];
|
$result = [];
|
||||||
foreach ($table_names as $table_name) {
|
foreach ($table_names as $table_name) {
|
||||||
$result = array_merge($result, $this->DumpTable($table_name));
|
$result = array_merge($result, $this->dumpTable($table_name));
|
||||||
}
|
}
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -80,7 +80,7 @@ class Document {
|
||||||
function save($filename)
|
function save($filename)
|
||||||
{
|
{
|
||||||
$doc = new XMLWriter();
|
$doc = new XMLWriter();
|
||||||
if (!$doc->openURI($filename)) {
|
if (!$doc->openUri($filename)) {
|
||||||
throw new Exception("unknown file " . $filename);
|
throw new Exception("unknown file " . $filename);
|
||||||
}
|
}
|
||||||
$doc->setIndent(false);
|
$doc->setIndent(false);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue