|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace SetBased\Stratum\MySql\Backend; |
|
5
|
|
|
|
|
6
|
|
|
use SetBased\Stratum\Backend\Config; |
|
7
|
|
|
use SetBased\Stratum\Backend\StratumStyle; |
|
8
|
|
|
use SetBased\Stratum\MySql\Exception\MySqlConnectFailedException; |
|
9
|
|
|
use SetBased\Stratum\MySql\Exception\MySqlDataLayerException; |
|
10
|
|
|
use SetBased\Stratum\MySql\MySqlDataLayer; |
|
11
|
|
|
use SetBased\Stratum\MySql\MySqlDefaultConnector; |
|
12
|
|
|
use SetBased\Stratum\MySql\MySqlMetaDataLayer; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Base class for commands which needs a connection to a MySQL instance. |
|
16
|
|
|
*/ |
|
17
|
|
|
class MySqlWorker |
|
18
|
|
|
{ |
|
19
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
20
|
|
|
/** |
|
21
|
|
|
* The meta data layer. |
|
22
|
|
|
* |
|
23
|
|
|
* @var MySqlMetaDataLayer |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $dl; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* The output object. |
|
29
|
|
|
* |
|
30
|
|
|
* @var StratumStyle |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $io; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* The settings from the PhpStratum configuration file. |
|
36
|
|
|
* |
|
37
|
|
|
* @var Config |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $settings; |
|
40
|
|
|
|
|
41
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
42
|
|
|
/** |
|
43
|
|
|
* Object constructor. |
|
44
|
|
|
* |
|
45
|
|
|
* @param Config $settings The settings from the PhpStratum configuration file. |
|
46
|
|
|
* @param StratumStyle $io The output object. |
|
47
|
|
|
*/ |
|
48
|
1 |
|
public function __construct(Config $settings, StratumStyle $io) |
|
49
|
|
|
{ |
|
50
|
1 |
|
$this->settings = $settings; |
|
51
|
1 |
|
$this->io = $io; |
|
52
|
1 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
55
|
|
|
/** |
|
56
|
|
|
* Disconnects from MySQL instance. |
|
57
|
|
|
*/ |
|
58
|
1 |
|
public function disconnect() |
|
59
|
|
|
{ |
|
60
|
1 |
|
if ($this->dl!==null) |
|
61
|
|
|
{ |
|
62
|
1 |
|
$this->dl->disconnect(); |
|
63
|
1 |
|
$this->dl = null; |
|
64
|
|
|
} |
|
65
|
1 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
68
|
|
|
/** |
|
69
|
|
|
* Connects to a MySQL instance. |
|
70
|
|
|
* |
|
71
|
|
|
* @throws MySqlConnectFailedException |
|
72
|
|
|
* @throws MySqlDataLayerException |
|
73
|
|
|
*/ |
|
74
|
1 |
|
protected function connect(): void |
|
75
|
|
|
{ |
|
76
|
1 |
|
$host = $this->settings->manString('database.host'); |
|
77
|
1 |
|
$user = $this->settings->manString('database.user'); |
|
78
|
1 |
|
$password = $this->settings->manString('database.password'); |
|
79
|
1 |
|
$database = $this->settings->manString('database.database'); |
|
80
|
1 |
|
$port = $this->settings->manInt('database.port', 3306); |
|
81
|
|
|
|
|
82
|
1 |
|
$connector = new MySqlDefaultConnector($host, $user, $password, $database, $port); |
|
83
|
1 |
|
$dataLayer = new MySqlDataLayer($connector); |
|
84
|
1 |
|
$dataLayer->connect(); |
|
85
|
|
|
|
|
86
|
1 |
|
$this->dl = new MySqlMetaDataLayer($dataLayer, $this->io); |
|
87
|
1 |
|
} |
|
88
|
|
|
|
|
89
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
//---------------------------------------------------------------------------------------------------------------------- |
|
93
|
|
|
|