|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Maketok\DataMigration\Storage\Db; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Connection; |
|
6
|
|
|
use Doctrine\DBAL\Driver as DriverInterface; |
|
7
|
|
|
use Maketok\DataMigration\Action\ConfigInterface; |
|
8
|
|
|
|
|
9
|
|
|
abstract class AbstractDBALResource implements ResourceInterface |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var Connection |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $connection; |
|
15
|
|
|
/** |
|
16
|
|
|
* @var ConfigInterface |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $config; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param ConfigInterface $config |
|
22
|
|
|
*/ |
|
23
|
31 |
|
public function __construct(ConfigInterface $config) |
|
24
|
|
|
{ |
|
25
|
31 |
|
$this->config = $config; |
|
26
|
31 |
|
$this->open(); |
|
27
|
31 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* {@inheritdoc} |
|
31
|
|
|
*/ |
|
32
|
31 |
|
public function open() |
|
33
|
|
|
{ |
|
34
|
|
|
$params = [ |
|
35
|
31 |
|
'dbname' => $this->config['db_name'], |
|
36
|
31 |
|
'user' => $this->config['db_user'], |
|
37
|
31 |
|
'password' => $this->config['db_password'], |
|
38
|
31 |
|
'host' => $this->config['db_host'], |
|
39
|
31 |
|
'port' => $this->config['db_port'], |
|
40
|
|
|
]; |
|
41
|
31 |
|
if (isset($this->config['db_pdo'])) { |
|
42
|
13 |
|
$params['pdo'] = $this->config['db_pdo']; |
|
43
|
|
|
} |
|
44
|
31 |
|
if (isset($this->config['db_url'])) { |
|
45
|
|
|
$params['url'] = $this->config['db_url']; |
|
46
|
|
|
} |
|
47
|
31 |
|
$params['driverOptions'] = $this->getDriverOptions(); |
|
48
|
31 |
|
$this->connection = new Connection($params, $this->getDriver()); |
|
49
|
31 |
|
return $this->connection->connect(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return array |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function getDriverOptions() |
|
56
|
|
|
{ |
|
57
|
|
|
return []; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @return DriverInterface |
|
62
|
|
|
*/ |
|
63
|
|
|
abstract protected function getDriver(); |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* {@inheritdoc} |
|
67
|
|
|
*/ |
|
68
|
10 |
|
public function close() |
|
69
|
|
|
{ |
|
70
|
10 |
|
$this->connection->close(); |
|
71
|
10 |
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* GC opened resource |
|
75
|
|
|
*/ |
|
76
|
|
|
public function __destruct() |
|
77
|
|
|
{ |
|
78
|
|
|
if ($this->isActive()) { |
|
79
|
|
|
$this->close(); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* {@inheritdoc} |
|
85
|
|
|
*/ |
|
86
|
|
|
public function isActive() |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->connection->isConnected(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* {@inheritdoc} |
|
93
|
|
|
*/ |
|
94
|
2 |
|
public function startTransaction() |
|
95
|
|
|
{ |
|
96
|
2 |
|
$this->connection->beginTransaction(); |
|
97
|
2 |
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* {@inheritdoc} |
|
101
|
|
|
*/ |
|
102
|
2 |
|
public function commit() |
|
103
|
|
|
{ |
|
104
|
2 |
|
$this->connection->commit(); |
|
105
|
2 |
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* {@inheritdoc} |
|
109
|
|
|
*/ |
|
110
|
|
|
public function rollback() |
|
111
|
|
|
{ |
|
112
|
|
|
$this->connection->rollBack(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @return Connection |
|
117
|
|
|
*/ |
|
118
|
18 |
|
public function getConnection() |
|
119
|
|
|
{ |
|
120
|
18 |
|
return $this->connection; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|