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