fix format
This commit is contained in:
parent
7163158baf
commit
2edd65d145
5 changed files with 407 additions and 388 deletions
|
|
@ -1,26 +1,29 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace ctiso\Database;
|
namespace ctiso\Database;
|
||||||
use ctiso\Database,
|
|
||||||
ctiso\Tools\SQLStatementExtractor,
|
use ctiso\Database;
|
||||||
ctiso\Path,
|
use ctiso\Tools\SQLStatementExtractor;
|
||||||
Exception;
|
use ctiso\Path;
|
||||||
|
use Exception;
|
||||||
|
|
||||||
class Manager
|
class Manager
|
||||||
{
|
{
|
||||||
public $db/*: Database*/;
|
public $db/*: Database*/;
|
||||||
|
|
||||||
function __construct(Database $db) {
|
public function __construct(Database $db)
|
||||||
|
{
|
||||||
$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 = isset($action["constraints"]) ? $action["constraints"] : NULL;
|
$constraints = isset($action["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":
|
||||||
|
|
@ -46,7 +49,7 @@ class Manager
|
||||||
}
|
}
|
||||||
|
|
||||||
$stmtList = SQLStatementExtractor::extractFile(Path::join(dirname($db_file), $file));
|
$stmtList = SQLStatementExtractor::extractFile(Path::join(dirname($db_file), $file));
|
||||||
foreach($stmtList as $stmt) {
|
foreach ($stmtList as $stmt) {
|
||||||
$this->db->executeQuery($stmt);
|
$this->db->executeQuery($stmt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -57,12 +60,14 @@ class Manager
|
||||||
}
|
}
|
||||||
|
|
||||||
//Дропает и создаёт SQL VIEW
|
//Дропает и создаёт SQL VIEW
|
||||||
public function recreateView($viewName, $selectStatement) {
|
public function recreateView($viewName, $selectStatement)
|
||||||
|
{
|
||||||
$this->db->query("DROP VIEW ".$viewName);
|
$this->db->query("DROP VIEW ".$viewName);
|
||||||
$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) {
|
||||||
$statement = $statement." CASCADE";
|
$statement = $statement." CASCADE";
|
||||||
|
|
@ -70,12 +75,14 @@ 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) {
|
||||||
throw new Exception("Not implemented for postgres");
|
throw new Exception("Not implemented for postgres");
|
||||||
|
|
@ -89,14 +96,15 @@ class Manager
|
||||||
$fields[$result["name"]] = [
|
$fields[$result["name"]] = [
|
||||||
"type"=> $result["type"],
|
"type"=> $result["type"],
|
||||||
"not_null"=> boolval($result["notnull"]),
|
"not_null"=> boolval($result["notnull"]),
|
||||||
"constraint"=> ((boolean) $result["pk"]) ? "PRIMARY KEY" : null
|
"constraint"=> ((bool) $result["pk"]) ? "PRIMARY KEY" : null
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
return $fields;
|
return $fields;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
|
|
@ -114,7 +122,7 @@ class Manager
|
||||||
$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'];
|
||||||
|
|
@ -127,36 +135,42 @@ class Manager
|
||||||
}
|
}
|
||||||
|
|
||||||
//Обновление ключа 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 Column_Definition($name, $data, $pg)
|
||||||
$constraint = isset($data['constraint'])?" ".$data['constraint']:"";
|
{
|
||||||
|
$constraint = isset($data['constraint']) ? " ".$data['constraint'] : "";
|
||||||
$references = "";
|
$references = "";
|
||||||
if (isset($data['references'])) {
|
if (isset($data['references'])) {
|
||||||
$references = " REFERENCES ".$data['references'];
|
$references = " REFERENCES ".$data['references'];
|
||||||
}
|
}
|
||||||
if (isset($data["not_null"]) && $data["not_null"])
|
if (isset($data["not_null"]) && $data["not_null"]) {
|
||||||
$constraint .=" NOT NULL";
|
$constraint .=" NOT NULL";
|
||||||
|
}
|
||||||
$type = $data['type'];
|
$type = $data['type'];
|
||||||
if (!$pg) {
|
if (!$pg) {
|
||||||
if (strtolower($type)=="serial")
|
if (strtolower($type)=="serial") {
|
||||||
$type = "integer";
|
$type = "integer";
|
||||||
|
}
|
||||||
//if (strtolower($type)=="boolean")
|
//if (strtolower($type)=="boolean")
|
||||||
// $type = "integer";
|
// $type = "integer";
|
||||||
}
|
}
|
||||||
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->Column_Definition($column_name, $field, $pg);
|
||||||
$this->db->query($q);
|
$this->db->query($q);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getConstraintDef($c/*: array*/) {
|
public function getConstraintDef($c/*: array*/)
|
||||||
|
{
|
||||||
if ($c['type'] == 'unique') {
|
if ($c['type'] == 'unique') {
|
||||||
return "UNIQUE(" . implode(", ", $c['fields']) . ")";
|
return "UNIQUE(" . implode(", ", $c['fields']) . ")";
|
||||||
}
|
}
|
||||||
|
|
@ -164,7 +178,8 @@ 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) {
|
||||||
if (is_array($constraints)) {
|
if (is_array($constraints)) {
|
||||||
|
|
@ -173,15 +188,17 @@ class Manager
|
||||||
$constraints = ", " . $constraints;
|
$constraints = ", " . $constraints;
|
||||||
}
|
}
|
||||||
|
|
||||||
$statement = "CREATE TABLE $table (" . implode(",",
|
$statement = "CREATE TABLE $table (" . implode(
|
||||||
array_map(function($name,$data) use ($pg) {
|
",",
|
||||||
return $this->Column_Definition($name,$data,$pg);
|
array_map(function ($name, $data) use ($pg) {
|
||||||
|
return $this->Column_Definition($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*/ = array();
|
$result/*: array*/ = array();
|
||||||
|
|
@ -211,7 +228,8 @@ class Manager
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function GetAllTableNames() {
|
public function GetAllTableNames()
|
||||||
|
{
|
||||||
$result = [];
|
$result = [];
|
||||||
if ($this->db->isPostgres()) {
|
if ($this->db->isPostgres()) {
|
||||||
$query = "SELECT table_name as name FROM information_schema.tables WHERE table_schema='public'";
|
$query = "SELECT table_name as name FROM information_schema.tables WHERE table_schema='public'";
|
||||||
|
|
@ -225,7 +243,8 @@ class Manager
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function DumpInserts() {
|
public function DumpInserts()
|
||||||
|
{
|
||||||
$table_names = $this->GetAllTableNames();
|
$table_names = $this->GetAllTableNames();
|
||||||
$result = array();
|
$result = array();
|
||||||
foreach ($table_names as $table_name) {
|
foreach ($table_names as $table_name) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue