1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tworzenieweb\SqlProvisioner\Database; |
4
|
|
|
|
5
|
|
|
use PDO; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @author Luke Adamczewski |
9
|
|
|
* @package Tworzenieweb\SqlProvisioner\Database |
10
|
|
|
*/ |
11
|
|
|
class Connection |
12
|
|
|
{ |
13
|
|
|
const DSN = 'mysql:host=%s;port=%d;dbname=%s'; |
14
|
|
|
|
15
|
|
|
/** @var string */ |
16
|
|
|
private $host; |
17
|
|
|
|
18
|
|
|
/** @var string */ |
19
|
|
|
private $port; |
20
|
|
|
|
21
|
|
|
/** @var string */ |
22
|
|
|
private $user; |
23
|
|
|
|
24
|
|
|
/** @var string */ |
25
|
|
|
private $password; |
26
|
|
|
|
27
|
|
|
/** @var string */ |
28
|
|
|
private $databaseName; |
29
|
|
|
|
30
|
|
|
/** @var PDO */ |
31
|
|
|
private $currentConnection; |
32
|
|
|
|
33
|
|
|
/** @var string */ |
34
|
|
|
private $provisioningTable; |
35
|
|
|
|
36
|
|
|
/** @var string */ |
37
|
|
|
private $criteriaColumn; |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $host |
43
|
|
|
*/ |
44
|
|
|
public function setHost($host) |
45
|
|
|
{ |
46
|
|
|
$this->host = $host; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $port |
53
|
|
|
*/ |
54
|
|
|
public function setPort($port) |
55
|
|
|
{ |
56
|
|
|
$this->port = $port; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $user |
63
|
|
|
*/ |
64
|
|
|
public function setUser($user) |
65
|
|
|
{ |
66
|
|
|
$this->user = $user; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $password |
73
|
|
|
*/ |
74
|
|
|
public function setPassword($password) |
75
|
|
|
{ |
76
|
|
|
$this->password = $password; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $databaseName |
83
|
|
|
*/ |
84
|
|
|
public function setDatabaseName($databaseName) |
85
|
|
|
{ |
86
|
|
|
$this->databaseName = $databaseName; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return PDO |
93
|
|
|
*/ |
94
|
|
|
public function getCurrentConnection() |
95
|
|
|
{ |
96
|
|
|
if (null === $this->currentConnection) { |
97
|
|
|
$this->currentConnection = new PDO( |
98
|
|
|
sprintf(self::DSN, $this->host, $this->port, $this->databaseName), $this->user, $this->password |
99
|
|
|
); |
100
|
|
|
$this->currentConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
101
|
|
|
$this->currentConnection->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); |
102
|
|
|
$this->currentConnection->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, true); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $this->currentConnection; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param string $provisioningTable |
112
|
|
|
*/ |
113
|
|
|
public function setProvisioningTable($provisioningTable) |
114
|
|
|
{ |
115
|
|
|
$this->provisioningTable = $provisioningTable; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $criteriaColumn |
122
|
|
|
*/ |
123
|
|
|
public function setCriteriaColumn($criteriaColumn) |
124
|
|
|
{ |
125
|
|
|
$this->criteriaColumn = $criteriaColumn; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
|
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
|
|
public function getProvisioningTable() |
134
|
|
|
{ |
135
|
|
|
return $this->provisioningTable; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
|
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
public function getCriteriaColumn() |
144
|
|
|
{ |
145
|
|
|
return $this->criteriaColumn; |
146
|
|
|
} |
147
|
|
|
} |