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
|
|
|
private $caching = true; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Autocommit flag. Default value is false. |
45
|
|
|
*/ |
46
|
|
|
private $autocommit = false; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* DB url. |
50
|
|
|
*/ |
51
|
|
|
private $url; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* User name. |
55
|
|
|
*/ |
56
|
|
|
private $userId; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Password. |
60
|
|
|
*/ |
61
|
|
|
private $password; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Initialize the PDOTask |
65
|
|
|
* This method checks if the PDO classes are available and triggers |
66
|
|
|
* appropriate error if they cannot be found. This is not done in header |
67
|
|
|
* because we may want this class to be loaded w/o triggering an error. |
68
|
|
|
*/ |
69
|
9 |
|
public function init() |
70
|
|
|
{ |
71
|
9 |
|
if (!class_exists('PDO')) { |
72
|
|
|
throw new Exception('PDOTask depends on PDO feature being included in PHP.'); |
73
|
|
|
} |
74
|
9 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Caching loaders / driver. This is to avoid |
78
|
|
|
* getting an OutOfMemoryError when calling this task |
79
|
|
|
* multiple times in a row; default: true. |
80
|
|
|
* |
81
|
|
|
* @param bool $caching |
82
|
|
|
*/ |
83
|
3 |
|
public function setCaching($caching) |
84
|
|
|
{ |
85
|
3 |
|
$this->caching = $caching; |
86
|
3 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Sets the database connection URL; required. |
90
|
|
|
* |
91
|
|
|
* @param string $url The url to set |
92
|
|
|
*/ |
93
|
15 |
|
public function setUrl($url) |
94
|
|
|
{ |
95
|
15 |
|
$this->url = $url; |
96
|
15 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Sets the password; required. |
100
|
|
|
* |
101
|
|
|
* @param string $password The password to set |
102
|
|
|
*/ |
103
|
1 |
|
public function setPassword($password) |
104
|
|
|
{ |
105
|
1 |
|
$this->password = $password; |
106
|
1 |
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Auto commit flag for database connection; |
110
|
|
|
* optional, default false. |
111
|
|
|
* |
112
|
|
|
* @param bool $autocommit The autocommit to set |
113
|
|
|
*/ |
114
|
3 |
|
public function setAutocommit($autocommit) |
115
|
|
|
{ |
116
|
3 |
|
$this->autocommit = $autocommit; |
117
|
3 |
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return bool |
121
|
|
|
*/ |
122
|
|
|
public function isCaching() |
123
|
|
|
{ |
124
|
|
|
return $this->caching; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Gets the autocommit. |
129
|
|
|
* |
130
|
|
|
* @return bool |
131
|
|
|
*/ |
132
|
12 |
|
public function isAutocommit() |
133
|
|
|
{ |
134
|
12 |
|
return $this->autocommit; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Gets the url. |
139
|
|
|
* |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
17 |
|
public function getUrl() |
143
|
|
|
{ |
144
|
17 |
|
return $this->url; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Gets the userId. |
149
|
|
|
* |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
5 |
|
public function getUserId() |
153
|
|
|
{ |
154
|
5 |
|
return $this->userId; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Set the user name for the connection; required. |
159
|
|
|
* |
160
|
|
|
* @param string $userId |
161
|
|
|
*/ |
162
|
2 |
|
public function setUserid($userId) |
163
|
|
|
{ |
164
|
2 |
|
$this->userId = $userId; |
165
|
2 |
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Gets the password. |
169
|
|
|
* |
170
|
|
|
* @return string |
171
|
|
|
*/ |
172
|
5 |
|
public function getPassword() |
173
|
|
|
{ |
174
|
5 |
|
return $this->password; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Creates a new Connection as using the driver, url, userid and password specified. |
179
|
|
|
* The calling method is responsible for closing the connection. |
180
|
|
|
* |
181
|
|
|
* @throws BuildException if the UserId/Password/Url is not set or there is no suitable driver |
182
|
|
|
* or the driver fails to load |
183
|
|
|
* |
184
|
|
|
* @return PDO the newly created connection |
185
|
|
|
*/ |
186
|
9 |
|
protected function getConnection() |
187
|
|
|
{ |
188
|
9 |
|
if (null === $this->url) { |
189
|
|
|
throw new BuildException('Url attribute must be set!', $this->getLocation()); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
try { |
193
|
9 |
|
$this->log('Connecting to ' . $this->getUrl(), Project::MSG_VERBOSE); |
194
|
|
|
|
195
|
9 |
|
$user = null; |
196
|
9 |
|
$pass = null; |
197
|
|
|
|
198
|
9 |
|
if ($this->userId) { |
199
|
|
|
$user = $this->getUserId(); |
200
|
|
|
} |
201
|
|
|
|
202
|
9 |
|
if ($this->password) { |
203
|
|
|
$pass = $this->getPassword(); |
204
|
|
|
} |
205
|
|
|
|
206
|
9 |
|
$conn = new PDO($this->getUrl(), $user, $pass); |
207
|
9 |
|
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
208
|
|
|
|
209
|
|
|
try { |
210
|
9 |
|
$conn->setAttribute(PDO::ATTR_AUTOCOMMIT, $this->autocommit); |
211
|
|
|
} catch (PDOException $pe) { |
212
|
|
|
$this->log( |
213
|
|
|
'Unable to enable auto-commit for this database: ' . $pe->getMessage(), |
214
|
|
|
Project::MSG_VERBOSE |
215
|
|
|
); |
216
|
|
|
} |
217
|
|
|
|
218
|
9 |
|
return $conn; |
219
|
|
|
} catch (PDOException $e) { |
220
|
|
|
throw new BuildException($e->getMessage(), $this->getLocation()); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|