|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Janisbiz\LightOrm\Dms\MySQL\Generator; |
|
4
|
|
|
|
|
5
|
|
|
use Janisbiz\LightOrm\Connection\ConnectionInterface; |
|
6
|
|
|
use Janisbiz\LightOrm\Generator\AbstractDmsFactory; |
|
7
|
|
|
use Janisbiz\LightOrm\Dms\MySQL\Generator\Dms\DmsColumn; |
|
8
|
|
|
use Janisbiz\LightOrm\Dms\MySQL\Generator\Dms\DmsDatabase; |
|
9
|
|
|
use Janisbiz\LightOrm\Dms\MySQL\Generator\Dms\DmsTable; |
|
10
|
|
|
use Janisbiz\LightOrm\Generator\Dms\DmsColumnInterface; |
|
11
|
|
|
use Janisbiz\LightOrm\Generator\Dms\DmsDatabaseInterface; |
|
12
|
|
|
use Janisbiz\LightOrm\Generator\Dms\DmsTableInterface; |
|
13
|
|
|
|
|
14
|
|
|
class DmsFactory extends AbstractDmsFactory |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @param string $databaseName |
|
18
|
|
|
* @param ConnectionInterface $connection |
|
19
|
|
|
* |
|
20
|
|
|
* @return DmsDatabase |
|
21
|
|
|
*/ |
|
22
|
|
|
public function createDmsDatabase(string $databaseName, ConnectionInterface $connection): DmsDatabaseInterface |
|
23
|
|
|
{ |
|
24
|
|
|
$tablesInDatabase = $connection->query('SHOW TABLES', \PDO::FETCH_CLASS, \stdClass::class); |
|
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
$tables = []; |
|
27
|
|
|
foreach ($tablesInDatabase as $tableInDatabase) { |
|
28
|
|
|
$tables[] = $this->createDmsTable($tableInDatabase->{\sprintf('Tables_in_%s', $databaseName)}, $connection); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
return new DmsDatabase($databaseName, $tables); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param string $tableName |
|
36
|
|
|
* @param ConnectionInterface $connection |
|
37
|
|
|
* |
|
38
|
|
|
* @return DmsTable |
|
39
|
|
|
*/ |
|
40
|
|
|
protected function createDmsTable(string $tableName, ConnectionInterface $connection): DmsTableInterface |
|
41
|
|
|
{ |
|
42
|
|
|
$columnsInTable = $connection->query( |
|
43
|
|
|
\sprintf('SHOW COLUMNS FROM %s', $tableName), |
|
44
|
|
|
\PDO::FETCH_CLASS, |
|
45
|
|
|
\stdClass::class |
|
46
|
|
|
); |
|
47
|
|
|
|
|
48
|
|
|
$columns = []; |
|
49
|
|
|
foreach ($columnsInTable as $columnInTable) { |
|
50
|
|
|
$columns[] = $this->createDmsColumn( |
|
51
|
|
|
$columnInTable->Field, |
|
52
|
|
|
$columnInTable->Type, |
|
53
|
|
|
$columnInTable->Null === 'YES', |
|
54
|
|
|
$columnInTable->Key, |
|
55
|
|
|
(string) $columnInTable->Default, |
|
56
|
|
|
$columnInTable->Extra |
|
57
|
|
|
); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return new DmsTable($tableName, $columns); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param string $name |
|
65
|
|
|
* @param string $type |
|
66
|
|
|
* @param bool $nullable |
|
67
|
|
|
* @param string $key |
|
68
|
|
|
* @param string $default |
|
69
|
|
|
* @param null|string $extra |
|
70
|
|
|
* |
|
71
|
|
|
* @return DmsColumn |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function createDmsColumn( |
|
74
|
|
|
string $name, |
|
75
|
|
|
string $type, |
|
76
|
|
|
bool $nullable, |
|
77
|
|
|
string $key, |
|
78
|
|
|
string $default, |
|
79
|
|
|
?string $extra |
|
80
|
|
|
): DmsColumnInterface { |
|
81
|
|
|
return new DmsColumn($name, $type, $nullable, $key, $default, $extra); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|