-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabaseConnection.php
109 lines (95 loc) · 3.47 KB
/
DatabaseConnection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/**
* DatabaseConnection 可連線至資料庫取得資料庫相關資訊
* @author cwlin <[email protected]>
* @copyright 2015 Chien Wei Lin
* @link https://github.com/cwlin0416/phpMyDBCompare
* @version 1.0.0
*/
class DatabaseConnection {
public $host;
public $user;
public $dbname;
private $conn = null;
private $password;
function __construct($host, $dbname, $user, $password) {
$this->host = $host;
$this->user = $user;
$this->password = $password;
$this->dbname = $dbname;
$this->open();
}
function open() {
try {
$this->conn = new PDO("mysql:host=$this->host;dbname=$this->dbname", $this->user, $this->password);
$this->conn->query("SET NAMES UTF8");
} catch (Exception $exc) {
echo "DatabaseConnection($this->host, $this->dbname, $this->user): Connect error: \n";
echo $exc->getMessage() . "\n";
}
}
function isConnected() {
return !empty($this->conn);
}
function getTables() {
$tables = array();
$sql = "SHOW TABLE STATUS";
foreach ($this->conn->query($sql, PDO::FETCH_ASSOC) as $row) {
$row['Character_set_name'] = $this->getTableDefaultCharset($row['Name']);
$tables[$row['Name']] = $row;
}
return $tables;
}
function getTableDefaultCharset($tablename) {
$sql = "SELECT CCSA.character_set_name FROM information_schema.`TABLES` T,";
$sql .= " information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` CCSA";
$sql .= " WHERE CCSA.collation_name = T.table_collation";
$sql .= " AND T.table_schema = '$this->dbname'";
$sql .= " AND T.table_name = '$tablename';";
$stmt = $this->conn->query($sql, PDO::FETCH_ASSOC);
$row = $stmt->fetch();
return $row['character_set_name'];
}
function getTableColumns($tablename) {
$columns = array();
$sql = "SHOW FULL COLUMNS FROM $tablename";
foreach ($this->conn->query($sql, PDO::FETCH_ASSOC) as $row) {
$columns[$row['Field']] = $row;
}
return $columns;
}
function getTableIndexes($tablename) {
$indexes = array();
$sql = "SHOW INDEX FROM $tablename";
foreach ($this->conn->query($sql, PDO::FETCH_ASSOC) as $row) {
if (isset($indexes[$row['Key_name']])) {
// 若有重複的鍵名,代表為組合鍵,以 Composite_Keys 欄位註記以便比對
$indexes[$row['Key_name']]['Composite_Keys'][] = $row['Column_name'];
} else {
$indexes[$row['Key_name']] = $row;
$indexes[$row['Key_name']]['Composite_Keys'] = array($row['Column_name']);
}
}
return $indexes;
}
function getTableConstraints($tablename) {
$constraints = array();
$sql = "SELECT CONSTRAINT_NAME, TABLE_NAME, COLUMN_NAME, REFERENCED_TABLE_SCHEMA, REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME ";
$sql .= "FROM information_schema.`KEY_COLUMN_USAGE` ";
$sql .= "WHERE REFERENCED_TABLE_SCHEMA IS NOT NULL AND TABLE_SCHEMA = '$this->dbname' AND TABLE_NAME = '$tablename'";
foreach ($this->conn->query($sql, PDO::FETCH_ASSOC) as $row) {
$referential = $this->getTableConstraintReferential($tablename, $row['CONSTRAINT_NAME']);
$row['UPDATE_RULE'] = $referential['UPDATE_RULE'];
$row['DELETE_RULE'] = $referential['DELETE_RULE'];
$constraints[$row['CONSTRAINT_NAME']] = $row;
}
return $constraints;
}
function getTableConstraintReferential($tablename, $constraintname) {
$sql = "SELECT UPDATE_RULE, DELETE_RULE FROM information_schema.`REFERENTIAL_CONSTRAINTS` ";
$sql .= "WHERE CONSTRAINT_SCHEMA = '$this->dbname' AND TABLE_NAME='$tablename' AND CONSTRAINT_NAME='$constraintname'";
$stmt = $this->conn->query($sql);
$row = $stmt->fetch();
return $row;
}
}