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