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
|
|
|
/** @var string */ |
14
|
|
|
private $host; |
15
|
|
|
|
16
|
|
|
/** @var string */ |
17
|
|
|
private $port; |
18
|
|
|
|
19
|
|
|
/** @var string */ |
20
|
|
|
private $user; |
21
|
|
|
|
22
|
|
|
/** @var string */ |
23
|
|
|
private $password; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
private $databaseName; |
27
|
|
|
|
28
|
|
|
/** @var PDO */ |
29
|
|
|
private $currentConnection; |
30
|
|
|
|
31
|
|
|
/** @var string */ |
32
|
|
|
private $provisioningTable; |
33
|
|
|
|
34
|
|
|
/** @var string */ |
35
|
|
|
private $criteriaColumn; |
36
|
|
|
|
37
|
|
|
private $dsn; |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $host |
42
|
|
|
* @return $this |
43
|
|
|
*/ |
44
|
|
|
public function setHost($host) |
45
|
|
|
{ |
46
|
|
|
$this->host = $host; |
47
|
|
|
|
48
|
|
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $port |
55
|
|
|
* @return $this |
56
|
|
|
*/ |
57
|
|
|
public function setPort($port) |
58
|
|
|
{ |
59
|
|
|
$this->port = $port; |
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $user |
68
|
|
|
* @return $this |
69
|
|
|
*/ |
70
|
|
|
public function setUser($user) |
71
|
|
|
{ |
72
|
|
|
$this->user = $user; |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param string $password |
81
|
|
|
* @return $this |
82
|
|
|
*/ |
83
|
|
|
public function setPassword($password) |
84
|
|
|
{ |
85
|
|
|
$this->password = $password; |
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $databaseName |
94
|
|
|
* @return $this |
95
|
|
|
*/ |
96
|
|
|
public function setDatabaseName($databaseName) |
97
|
|
|
{ |
98
|
|
|
$this->databaseName = $databaseName; |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param bool $useMemoryStorage |
107
|
|
|
* @return $this |
108
|
|
|
*/ |
109
|
1 |
|
public function useSqlite($useMemoryStorage = false) |
110
|
|
|
{ |
111
|
1 |
|
$this->dsn = $useMemoryStorage ? 'sqlite::memory:' : 'sqlite:%s'; |
112
|
|
|
|
113
|
1 |
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return $this |
120
|
|
|
*/ |
121
|
1 |
|
public function useMysql() |
122
|
|
|
{ |
123
|
1 |
|
$this->dsn = 'mysql:host=%s;port=%d;dbname=%s'; |
124
|
|
|
|
125
|
1 |
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return PDO |
130
|
|
|
*/ |
131
|
1 |
|
public function getCurrentConnection() |
132
|
|
|
{ |
133
|
1 |
|
if (null === $this->currentConnection) { |
134
|
1 |
|
$this->currentConnection = new PDO( |
135
|
1 |
|
sprintf($this->dsn, $this->host, $this->port, $this->databaseName), |
136
|
1 |
|
$this->user, |
137
|
1 |
|
$this->password |
138
|
1 |
|
); |
139
|
1 |
|
$this->currentConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
140
|
1 |
|
$this->currentConnection->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); |
141
|
1 |
|
$this->currentConnection->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, true); |
142
|
1 |
|
} |
143
|
|
|
|
144
|
1 |
|
return $this->currentConnection; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
|
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param string $provisioningTable |
151
|
|
|
* @return $this |
152
|
|
|
*/ |
153
|
1 |
|
public function setProvisioningTable($provisioningTable) |
154
|
|
|
{ |
155
|
1 |
|
$this->provisioningTable = $provisioningTable; |
156
|
|
|
|
157
|
1 |
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
|
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param string $criteriaColumn |
164
|
|
|
* @return $this |
165
|
|
|
*/ |
166
|
1 |
|
public function setCriteriaColumn($criteriaColumn) |
167
|
|
|
{ |
168
|
1 |
|
$this->criteriaColumn = $criteriaColumn; |
169
|
|
|
|
170
|
1 |
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
|
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @return string |
177
|
|
|
*/ |
178
|
|
|
public function getProvisioningTable() |
179
|
|
|
{ |
180
|
|
|
return $this->provisioningTable; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
|
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @return string |
187
|
|
|
*/ |
188
|
|
|
public function getCriteriaColumn() |
189
|
|
|
{ |
190
|
|
|
return $this->criteriaColumn; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|