1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Starkerxp\DatabaseChecker\Factory; |
4
|
|
|
|
5
|
|
|
use Starkerxp\DatabaseChecker\Repository\MysqlRepository; |
6
|
|
|
use Starkerxp\DatabaseChecker\Structure\MysqlDatabaseTable; |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Transcris l'�tat de la base de donn�es en version objet afin de pouvoir y appliquer les traitements. |
11
|
|
|
* |
12
|
|
|
* @package Starkerxp\DatabaseChecker\Factory |
13
|
|
|
*/ |
14
|
|
|
class MysqlDatabaseFactory |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
protected $databaseName; |
18
|
|
|
protected $repositoryMysql; |
19
|
|
|
private $checkCollate = false; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* MysqlDatabaseFactory constructor. |
23
|
|
|
* |
24
|
|
|
* @param MysqlRepository $repositoryMysql |
25
|
|
|
* @param string $databaseName |
26
|
|
|
*/ |
27
|
|
|
public function __construct(MysqlRepository $repositoryMysql, $databaseName) |
28
|
|
|
{ |
29
|
|
|
$this->repositoryMysql = $repositoryMysql; |
30
|
|
|
$this->databaseName = $databaseName; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function enableCheckCollate() |
34
|
|
|
{ |
35
|
|
|
$this->checkCollate = true; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return MysqlDatabaseTable[] |
40
|
|
|
* |
41
|
|
|
* @throws \LogicException |
42
|
|
|
*/ |
43
|
|
|
public function generate() |
44
|
|
|
{ |
45
|
|
|
$export = []; |
46
|
|
|
$tables = $this->repositoryMysql->getTablesStructure($this->databaseName); |
47
|
|
|
foreach ($tables as $table) { |
48
|
|
|
$export[$table] = $this->getIndex($table); |
49
|
|
|
$export[$table]['columns'] = $this->getColumns($table); |
50
|
|
|
} |
51
|
|
|
$factoryJsonDatabase = new JsonDatabaseFactory(json_encode($export)); |
52
|
|
|
try { |
53
|
|
|
$export = $factoryJsonDatabase->generate(); |
54
|
|
|
} catch (\Exception $e) { |
55
|
|
|
throw new \LogicException('Un expected error with json.' . $e->getMessage()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $export; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
protected function getIndex($table) |
63
|
|
|
{ |
64
|
|
|
if (!$results = $this->repositoryMysql->fetchIndexStructure($this->databaseName, $table)) { |
65
|
|
|
return []; |
66
|
|
|
} |
67
|
|
|
$export = []; |
68
|
|
|
foreach ($results as $row) { |
69
|
|
|
if ($row['INDEX_NAME'] === 'PRIMARY') { |
70
|
|
|
$export['primary'] = array_filter(explode(',', $row['COLUMN_NAME'])); |
71
|
|
|
continue; |
72
|
|
|
} |
73
|
|
|
$key = !$row['NON_UNIQUE'] ? 'uniques' : 'indexes'; |
74
|
|
|
$export[$key][] = array_filter(['name' => $row['INDEX_NAME'], 'columns' => explode(',', $row['COLUMN_NAME'])]); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $export; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
protected function getColumns($table) |
81
|
|
|
{ |
82
|
|
|
$export = []; |
83
|
|
|
$results = $this->repositoryMysql->fetchColumnsStructure($this->databaseName, $table); |
84
|
|
|
foreach ($results as $row) { |
85
|
|
|
$type = $row['DATA_TYPE']; |
86
|
|
|
$length = str_replace([$type, '(', ')'], '', $row['COLUMN_TYPE']); |
87
|
|
|
if ($type === 'enum') { |
88
|
|
|
$type = $row['COLUMN_TYPE']; |
89
|
|
|
$length = null; |
90
|
|
|
} |
91
|
|
|
$export[$row['COLUMN_NAME']] = array_filter([ |
92
|
|
|
'type' => $type, |
93
|
|
|
'length' => $length, |
94
|
|
|
'nullable' => $row['IS_NULLABLE'] !== 'NO', |
95
|
|
|
'defaultValue' => $row['IS_NULLABLE'] !== 'NO' && empty($row['COLUMN_DEFAULT']) ? 'NULL' : $row['COLUMN_DEFAULT'], |
96
|
|
|
'extra' => $row['EXTRA'], |
97
|
|
|
]); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $export; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
} |
|
|
|
|
104
|
|
|
|
This check marks files that end in a newline character, i.e. an empy line.