|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Starkerxp\DatabaseChecker\Repository; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use Starkerxp\DatabaseChecker\LoggerTrait; |
|
7
|
|
|
|
|
8
|
|
|
class MysqlRepository implements StructureInterface |
|
9
|
|
|
{ |
|
10
|
|
|
use LoggerTrait; |
|
11
|
|
|
/** |
|
12
|
|
|
* @var \PDO |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $pdo; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* MysqlRepository constructor. |
|
18
|
|
|
* |
|
19
|
|
|
* @param \PDO $pdo |
|
20
|
|
|
*/ |
|
21
|
|
|
public function __construct(\PDO $pdo) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->pdo = $pdo; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param $database |
|
28
|
|
|
* |
|
29
|
|
|
* @return array |
|
30
|
|
|
*/ |
|
31
|
|
|
public function getSchemaCollation($database) |
|
32
|
|
|
{ |
|
33
|
|
|
$sth = $this->pdo->prepare('SELECT default_collation_name FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME=:database'); |
|
34
|
|
|
$sth->bindParam(':database', $database); |
|
35
|
|
|
$sth->execute(); |
|
36
|
|
|
$results = $sth->fetchAll(\PDO::FETCH_ASSOC); |
|
37
|
|
|
|
|
38
|
|
|
return array_column($results, 'default_collation_name')[0]; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param $database |
|
43
|
|
|
* |
|
44
|
|
|
* @return array |
|
45
|
|
|
*/ |
|
46
|
|
|
public function getTablesStructure($database) |
|
47
|
|
|
{ |
|
48
|
|
|
$sth = $this->pdo->prepare('SELECT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA=:database GROUP BY TABLE_NAME ORDER BY TABLE_NAME'); |
|
49
|
|
|
$sth->bindParam(':database', $database); |
|
50
|
|
|
$sth->execute(); |
|
51
|
|
|
$results = $sth->fetchAll(\PDO::FETCH_ASSOC); |
|
52
|
|
|
|
|
53
|
|
|
return array_column($results, 'TABLE_NAME'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param $database |
|
58
|
|
|
* @param $table |
|
59
|
|
|
* |
|
60
|
|
|
* @return array |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getTablesCollation($database, $table) |
|
63
|
|
|
{ |
|
64
|
|
|
$sql = ' SELECT c.collation_name |
|
65
|
|
|
FROM information_schema.`TABLES` t |
|
66
|
|
|
INNER JOIN information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` c ON c.collation_name = t.table_collation |
|
67
|
|
|
WHERE |
|
68
|
|
|
t.table_schema = :database |
|
69
|
|
|
AND t.table_name = :table'; |
|
70
|
|
|
$sth = $this->pdo->prepare($sql); |
|
71
|
|
|
$sth->bindParam(':database', $database); |
|
72
|
|
|
$sth->bindParam(':table', $table); |
|
73
|
|
|
$sth->execute(); |
|
74
|
|
|
$results = $sth->fetchAll(\PDO::FETCH_ASSOC); |
|
75
|
|
|
|
|
76
|
|
|
return array_column($results, 'TABLE_NAME'); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param $database |
|
81
|
|
|
* @param $table |
|
82
|
|
|
* |
|
83
|
|
|
* @return array |
|
84
|
|
|
*/ |
|
85
|
|
|
public function fetchIndexStructure($database, $table) |
|
86
|
|
|
{ |
|
87
|
|
|
$sql = 'SELECT |
|
88
|
|
|
INDEX_NAME, |
|
89
|
|
|
GROUP_CONCAT(COLUMN_NAME) AS COLUMN_NAME, |
|
90
|
|
|
NON_UNIQUE, |
|
91
|
|
|
IF(INDEX_TYPE="FULLTEXT",0,1) as NON_FULLTEXT |
|
92
|
|
|
FROM |
|
93
|
|
|
information_schema.statistics |
|
94
|
|
|
WHERE |
|
95
|
|
|
TABLE_SCHEMA= :database AND |
|
96
|
|
|
TABLE_NAME= :myTable |
|
97
|
|
|
GROUP BY |
|
98
|
|
|
TABLE_NAME,INDEX_NAME |
|
99
|
|
|
ORDER BY |
|
100
|
|
|
TABLE_NAME,INDEX_NAME,COLUMN_NAME'; |
|
101
|
|
|
$sth = $this->pdo->prepare($sql); |
|
102
|
|
|
$sth->bindParam(':database', $database); |
|
103
|
|
|
$sth->bindParam(':myTable', $table); |
|
104
|
|
|
$sth->execute(); |
|
105
|
|
|
|
|
106
|
|
|
return $sth->fetchAll(\PDO::FETCH_ASSOC); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param $database |
|
111
|
|
|
* @param $table |
|
112
|
|
|
* |
|
113
|
|
|
* @return array |
|
114
|
|
|
*/ |
|
115
|
|
|
public function fetchColumnsStructure($database, $table) |
|
116
|
|
|
{ |
|
117
|
|
|
$sql = 'SELECT |
|
118
|
|
|
COLUMN_NAME, |
|
119
|
|
|
DATA_TYPE, |
|
120
|
|
|
COLUMN_TYPE, |
|
121
|
|
|
IS_NULLABLE, |
|
122
|
|
|
COLUMN_DEFAULT, |
|
123
|
|
|
EXTRA, |
|
124
|
|
|
COLLATION_NAME |
|
125
|
|
|
FROM INFORMATION_SCHEMA.COLUMNS |
|
126
|
|
|
WHERE |
|
127
|
|
|
TABLE_SCHEMA= :database AND |
|
128
|
|
|
TABLE_NAME= :myTable |
|
129
|
|
|
ORDER BY ORDINAL_POSITION'; |
|
130
|
|
|
$sth = $this->pdo->prepare($sql); |
|
131
|
|
|
$sth->bindParam(':database', $database); |
|
132
|
|
|
$sth->bindParam(':myTable', $table); |
|
133
|
|
|
$sth->execute(); |
|
134
|
|
|
|
|
135
|
|
|
return $sth->fetchAll(\PDO::FETCH_ASSOC); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
} |
|
139
|
|
|
|