|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
5
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
6
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
7
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
8
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
9
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
10
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
11
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
12
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
13
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
14
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
15
|
|
|
* |
|
16
|
|
|
* This software consists of voluntary contributions made by many individuals |
|
17
|
|
|
* and is licensed under the LGPL. For more information please see |
|
18
|
|
|
* <http://phing.info>. |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace Phing\Task\System\Pdo; |
|
22
|
|
|
|
|
23
|
|
|
use Exception; |
|
24
|
|
|
use PDO; |
|
25
|
|
|
use PDOException; |
|
26
|
|
|
use Phing\Exception\BuildException; |
|
27
|
|
|
use Phing\Project; |
|
28
|
|
|
use Phing\Task; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Handles PDO configuration needed by SQL type tasks. |
|
32
|
|
|
* |
|
33
|
|
|
* @author Hans Lellelid <[email protected]> (Phing) |
|
34
|
|
|
* @author Nick Chalko <[email protected]> (Ant) |
|
35
|
|
|
* @author Jeff Martin <[email protected]> (Ant) |
|
36
|
|
|
* @author Michael McCallum <[email protected]> (Ant) |
|
37
|
|
|
* @author Tim Stephenson <[email protected]> (Ant) |
|
38
|
|
|
*/ |
|
39
|
|
|
abstract class PDOTask extends Task |
|
40
|
|
|
{ |
|
41
|
|
|
/** |
|
42
|
|
|
* Autocommit flag. Default value is false. |
|
43
|
|
|
*/ |
|
44
|
|
|
private $autocommit = false; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* DB url. |
|
48
|
|
|
*/ |
|
49
|
|
|
private $url; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* User name. |
|
53
|
|
|
*/ |
|
54
|
|
|
private $userId; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Password. |
|
58
|
|
|
*/ |
|
59
|
|
|
private $password; |
|
60
|
|
|
private $failOnConnectionError = true; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Initialize the PDOTask |
|
64
|
|
|
* This method checks if the PDO classes are available and triggers |
|
65
|
|
|
* appropriate error if they cannot be found. This is not done in header |
|
66
|
|
|
* because we may want this class to be loaded w/o triggering an error. |
|
67
|
|
|
*/ |
|
68
|
11 |
|
public function init() |
|
69
|
|
|
{ |
|
70
|
11 |
|
if (!class_exists('PDO')) { |
|
71
|
|
|
throw new Exception('PDOTask depends on PDO feature being included in PHP.'); |
|
72
|
|
|
} |
|
73
|
11 |
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Sets the database connection URL; required. |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $url The url to set |
|
79
|
|
|
*/ |
|
80
|
17 |
|
public function setUrl($url): void |
|
81
|
|
|
{ |
|
82
|
17 |
|
$this->url = $url; |
|
83
|
17 |
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Sets the password; required. |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $password The password to set |
|
89
|
|
|
*/ |
|
90
|
1 |
|
public function setPassword($password): void |
|
91
|
|
|
{ |
|
92
|
1 |
|
$this->password = $password; |
|
93
|
1 |
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Auto commit flag for database connection; |
|
97
|
|
|
* optional, default false. |
|
98
|
|
|
* |
|
99
|
|
|
* @param bool $autocommit The autocommit to set |
|
100
|
|
|
*/ |
|
101
|
3 |
|
public function setAutocommit($autocommit): void |
|
102
|
|
|
{ |
|
103
|
3 |
|
$this->autocommit = $autocommit; |
|
104
|
3 |
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param bool $b |
|
108
|
|
|
*/ |
|
109
|
1 |
|
public function setFailOnConnectionError(bool $b): void |
|
110
|
|
|
{ |
|
111
|
1 |
|
$this->failOnConnectionError = $b; |
|
112
|
1 |
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Gets the autocommit. |
|
116
|
|
|
* |
|
117
|
|
|
* @return bool |
|
118
|
|
|
*/ |
|
119
|
13 |
|
public function isAutocommit() |
|
120
|
|
|
{ |
|
121
|
13 |
|
return $this->autocommit; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Gets the url. |
|
126
|
|
|
* |
|
127
|
|
|
* @return string |
|
128
|
|
|
*/ |
|
129
|
19 |
|
public function getUrl() |
|
130
|
|
|
{ |
|
131
|
19 |
|
return $this->url; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Gets the userId. |
|
136
|
|
|
* |
|
137
|
|
|
* @return string |
|
138
|
|
|
*/ |
|
139
|
16 |
|
public function getUserId() |
|
140
|
|
|
{ |
|
141
|
16 |
|
return $this->userId; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Set the user name for the connection; required. |
|
146
|
|
|
* |
|
147
|
|
|
* @param string $userId |
|
148
|
|
|
*/ |
|
149
|
2 |
|
public function setUserid($userId) |
|
150
|
|
|
{ |
|
151
|
2 |
|
$this->userId = $userId; |
|
152
|
2 |
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Gets the password. |
|
156
|
|
|
* |
|
157
|
|
|
* @return string |
|
158
|
|
|
*/ |
|
159
|
16 |
|
public function getPassword() |
|
160
|
|
|
{ |
|
161
|
16 |
|
return $this->password; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Creates a new Connection as using the driver, url, userid and password specified. |
|
166
|
|
|
* The calling method is responsible for closing the connection. |
|
167
|
|
|
* |
|
168
|
|
|
* @throws BuildException if the UserId/Password/Url is not set or there is no suitable driver |
|
169
|
|
|
* or the driver fails to load |
|
170
|
|
|
* |
|
171
|
|
|
* @return PDO|null the newly created connection |
|
172
|
|
|
*/ |
|
173
|
11 |
|
protected function getConnection(): ?PDO |
|
174
|
|
|
{ |
|
175
|
11 |
|
if (null === $this->url) { |
|
176
|
|
|
throw new BuildException('Url attribute must be set!', $this->getLocation()); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
try { |
|
180
|
11 |
|
$this->log('Connecting to ' . $this->getUrl(), Project::MSG_VERBOSE); |
|
181
|
|
|
|
|
182
|
11 |
|
$conn = null; |
|
|
|
|
|
|
183
|
|
|
try { |
|
184
|
11 |
|
$conn = new PDO( |
|
185
|
11 |
|
$this->getUrl(), |
|
186
|
11 |
|
$this->getUserId(), |
|
187
|
11 |
|
$this->getPassword(), |
|
188
|
|
|
[ |
|
189
|
11 |
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION |
|
190
|
|
|
] |
|
191
|
|
|
); |
|
192
|
1 |
|
} catch (PDOException $e) { |
|
193
|
1 |
|
if ($this->failOnConnectionError) { |
|
194
|
|
|
throw $e; |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
try { |
|
199
|
11 |
|
if ($conn instanceof PDO) { |
|
|
|
|
|
|
200
|
11 |
|
$conn->setAttribute(PDO::ATTR_AUTOCOMMIT, $this->autocommit); |
|
201
|
|
|
} |
|
202
|
|
|
} catch (PDOException $pe) { |
|
203
|
|
|
$this->log( |
|
204
|
|
|
'Unable to enable auto-commit for this database: ' . $pe->getMessage(), |
|
205
|
|
|
Project::MSG_VERBOSE |
|
206
|
|
|
); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
11 |
|
return $conn; |
|
210
|
|
|
} catch (PDOException $e) { |
|
211
|
|
|
throw new BuildException($e->getMessage(), $this->getLocation()); |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
} |
|
215
|
|
|
|