| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* $Id: FormatterElement.php 148 2007-02-13 11:15:53Z mrook $
|
|
|
4 |
*
|
|
|
5 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
6 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
7 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
8 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
9 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
10 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
11 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
12 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
13 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
14 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
15 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
16 |
*
|
|
|
17 |
* This software consists of voluntary contributions made by many individuals
|
|
|
18 |
* and is licensed under the LGPL. For more information please see
|
|
|
19 |
* <http://phing.info>.
|
|
|
20 |
*/
|
|
|
21 |
|
|
|
22 |
require_once 'phing/system/io/PhingFile.php';
|
|
|
23 |
require_once 'phing/tasks/ext/pdo/PlainPDOResultFormatter.php';
|
|
|
24 |
require_once 'phing/tasks/ext/pdo/XMLPDOResultFormatter.php';
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
* A class to represent the nested <formatter> element for PDO SQL results.
|
|
|
28 |
*
|
|
|
29 |
* This class is inspired by the similarly-named class in the PHPUnit tasks.
|
|
|
30 |
*
|
|
|
31 |
* @author Hans Lellelid <hans@xmpl.org>
|
|
|
32 |
* @package phing.tasks.ext.pdo
|
|
|
33 |
* @since 2.3.0
|
|
|
34 |
*/
|
|
|
35 |
class PDOSQLExecFormatterElement
|
|
|
36 |
{
|
|
|
37 |
/**
|
|
|
38 |
* @var PDOResultFormatter
|
|
|
39 |
*/
|
|
|
40 |
private $formatter;
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* The type of the formatter (used for built-in formatter classes).
|
|
|
44 |
* @var string
|
|
|
45 |
*/
|
|
|
46 |
private $type = "";
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* Whether to use file (or write output to phing log).
|
|
|
50 |
* @var boolean
|
|
|
51 |
*/
|
|
|
52 |
private $useFile = true;
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
* Output file for formatter.
|
|
|
56 |
* @var PhingFile
|
|
|
57 |
*/
|
|
|
58 |
private $outfile;
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* Print header columns.
|
|
|
62 |
* @var boolean
|
|
|
63 |
*/
|
|
|
64 |
private $showheaders = true;
|
|
|
65 |
|
|
|
66 |
/**
|
|
|
67 |
* Whether to format XML output.
|
|
|
68 |
* @var boolean
|
|
|
69 |
*/
|
|
|
70 |
private $formatoutput = true;
|
|
|
71 |
|
|
|
72 |
/**
|
|
|
73 |
* Encoding for XML output.
|
|
|
74 |
* @var string
|
|
|
75 |
*/
|
|
|
76 |
private $encoding;
|
|
|
77 |
|
|
|
78 |
/**
|
|
|
79 |
* Column delimiter.
|
|
|
80 |
* Defaults to ','
|
|
|
81 |
* @var string
|
|
|
82 |
*/
|
|
|
83 |
private $coldelimiter = ",";
|
|
|
84 |
|
|
|
85 |
/**
|
|
|
86 |
* Row delimiter.
|
|
|
87 |
* Defaults to PHP_EOL.
|
|
|
88 |
* @var string
|
|
|
89 |
*/
|
|
|
90 |
private $rowdelimiter = PHP_EOL;
|
|
|
91 |
|
|
|
92 |
/**
|
|
|
93 |
* Append to an existing file or overwrite it?
|
|
|
94 |
* @var boolean
|
|
|
95 |
*/
|
|
|
96 |
private $append = false;
|
|
|
97 |
|
|
|
98 |
/**
|
|
|
99 |
* Parameters for a custom formatter.
|
|
|
100 |
* @var array Parameter[]
|
|
|
101 |
*/
|
|
|
102 |
private $formatterParams = array();
|
|
|
103 |
|
|
|
104 |
/**
|
|
|
105 |
* @var PDOSQLExecTask
|
|
|
106 |
*/
|
|
|
107 |
private $parentTask;
|
|
|
108 |
|
|
|
109 |
/**
|
|
|
110 |
* Construct a new PDOSQLExecFormatterElement with parent task.
|
|
|
111 |
* @param PDOSQLExecTask $parentTask
|
|
|
112 |
*/
|
|
|
113 |
public function __construct(PDOSQLExecTask $parentTask)
|
|
|
114 |
{
|
|
|
115 |
$this->parentTask = $parentTask;
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
/**
|
|
|
119 |
* Supports nested <param> element (for custom formatter classes).
|
|
|
120 |
* @return Parameter
|
|
|
121 |
*/
|
|
|
122 |
public function createParam() {
|
|
|
123 |
$num = array_push($this->parameters, new Parameter());
|
|
|
124 |
return $this->parameters[$num-1];
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
/**
|
|
|
128 |
* Gets a configured output writer.
|
|
|
129 |
* @return Writer
|
|
|
130 |
*/
|
|
|
131 |
private function getOutputWriter()
|
|
|
132 |
{
|
|
|
133 |
if ($this->useFile) {
|
|
|
134 |
$of = $this->getOutfile();
|
|
|
135 |
if (!$of) {
|
|
|
136 |
$of = new PhingFile($this->formatter->getPreferredOutfile());
|
|
|
137 |
}
|
|
|
138 |
return new FileWriter($of, $this->append);
|
|
|
139 |
} else {
|
|
|
140 |
return $this->getDefaultOutput();
|
|
|
141 |
}
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
/**
|
|
|
145 |
* Configures wrapped formatter class with any attributes on this element.
|
|
|
146 |
*/
|
|
|
147 |
public function prepare() {
|
|
|
148 |
|
|
|
149 |
if (!$this->formatter) {
|
|
|
150 |
throw new BuildException("No formatter specified (use type or classname attribute)", $this->getLocation());
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
$out = $this->getOutputWriter();
|
|
|
154 |
|
|
|
155 |
print "Setting output writer to: " . get_class($out) . "\n";
|
|
|
156 |
$this->formatter->setOutput($out);
|
|
|
157 |
|
|
|
158 |
if ($this->formatter instanceof PlainPDOResultFormatter) {
|
|
|
159 |
// set any options that apply to the plain formatter
|
|
|
160 |
$this->formatter->setShowheaders($this->showheaders);
|
|
|
161 |
$this->formatter->setRowdelim($this->rowdelimiter);
|
|
|
162 |
$this->formatter->setColdelim($this->coldelimiter);
|
|
|
163 |
} elseif ($this->formatter instanceof XMLPDOResultFormatter) {
|
|
|
164 |
// set any options that apply to the xml formatter
|
|
|
165 |
$this->formatter->setEncoding($this->encoding);
|
|
|
166 |
$this->formatter->setFormatOutput($this->formatoutput);
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
foreach($this->formatterParams as $param) {
|
|
|
170 |
$param = new Parameter();
|
|
|
171 |
$method = 'set' . $param->getName();
|
|
|
172 |
if (!method_exists($this->formatter, $param->getName())) {
|
|
|
173 |
throw new BuildException("Formatter " . get_class($this->formatter) . " does not have a $method method.", $this->getLocation());
|
|
|
174 |
}
|
|
|
175 |
call_user_func(array($this->formatter, $method), $param->getValue());
|
|
|
176 |
}
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
/**
|
|
|
180 |
* Sets the formatter type.
|
|
|
181 |
* @param string $type
|
|
|
182 |
*/
|
|
|
183 |
function setType($type) {
|
|
|
184 |
$this->type = $type;
|
|
|
185 |
if ($this->type == "xml") {
|
|
|
186 |
$this->formatter = new XMLPDOResultFormatter();
|
|
|
187 |
} elseif ($this->type == "plain") {
|
|
|
188 |
$this->formatter = new PlainPDOResultFormatter();
|
|
|
189 |
} else {
|
|
|
190 |
throw new BuildException("Formatter '" . $this->type . "' not implemented");
|
|
|
191 |
}
|
|
|
192 |
}
|
|
|
193 |
|
|
|
194 |
/**
|
|
|
195 |
* Set classname for a custom formatter (must extend PDOResultFormatter).
|
|
|
196 |
* @param string $className
|
|
|
197 |
*/
|
|
|
198 |
function setClassName($className) {
|
|
|
199 |
$classNameNoDot = Phing::import($className);
|
|
|
200 |
$this->formatter = new $classNameNoDot();
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
/**
|
|
|
204 |
* Set whether to write formatter results to file.
|
|
|
205 |
* @param boolean $useFile
|
|
|
206 |
*/
|
|
|
207 |
function setUseFile($useFile) {
|
|
|
208 |
$this->useFile = (boolean) $useFile;
|
|
|
209 |
}
|
|
|
210 |
|
|
|
211 |
/**
|
|
|
212 |
* Return whether to write formatter results to file.
|
|
|
213 |
* @return boolean
|
|
|
214 |
*/
|
|
|
215 |
function getUseFile() {
|
|
|
216 |
return $this->useFile;
|
|
|
217 |
}
|
|
|
218 |
|
|
|
219 |
/**
|
|
|
220 |
* Sets the output file for the formatter results.
|
|
|
221 |
* @param PhingFile $outFile
|
|
|
222 |
*/
|
|
|
223 |
function setOutfile(PhingFile $outfile) {
|
|
|
224 |
$this->outfile = $outfile;
|
|
|
225 |
}
|
|
|
226 |
|
|
|
227 |
/**
|
|
|
228 |
* Get the output file.
|
|
|
229 |
* @return PhingFile
|
|
|
230 |
*/
|
|
|
231 |
function getOutfile() {
|
|
|
232 |
return $this->outfile;
|
|
|
233 |
/*
|
|
|
234 |
} else {
|
|
|
235 |
return new PhingFile($this->formatter->getPreferredOutfile());
|
|
|
236 |
}*/
|
|
|
237 |
}
|
|
|
238 |
|
|
|
239 |
/**
|
|
|
240 |
* whether output should be appended to or overwrite
|
|
|
241 |
* an existing file. Defaults to false.
|
|
|
242 |
* @param boolean $append
|
|
|
243 |
*/
|
|
|
244 |
public function setAppend($append) {
|
|
|
245 |
$this->append = (boolean) $append;
|
|
|
246 |
}
|
|
|
247 |
|
|
|
248 |
/**
|
|
|
249 |
* Whether output should be appended to file.
|
|
|
250 |
* @return boolean
|
|
|
251 |
*/
|
|
|
252 |
public function getAppend() {
|
|
|
253 |
return $this->append;
|
|
|
254 |
}
|
|
|
255 |
|
|
|
256 |
/**
|
|
|
257 |
* Print headers for result sets from the
|
|
|
258 |
* statements; optional, default true.
|
|
|
259 |
* @param boolean $showheaders
|
|
|
260 |
*/
|
|
|
261 |
public function setShowheaders($showheaders) {
|
|
|
262 |
$this->showheaders = (boolean) $showheaders;
|
|
|
263 |
}
|
|
|
264 |
|
|
|
265 |
/**
|
|
|
266 |
* Sets the column delimiter.
|
|
|
267 |
* @param string $v
|
|
|
268 |
*/
|
|
|
269 |
public function setColdelim($v) {
|
|
|
270 |
$this->coldelimiter = $v;
|
|
|
271 |
}
|
|
|
272 |
|
|
|
273 |
/**
|
|
|
274 |
* Sets the row delimiter.
|
|
|
275 |
* @param string $v
|
|
|
276 |
*/
|
|
|
277 |
public function setRowdelim($v) {
|
|
|
278 |
$this->rowdelimiter = $v;
|
|
|
279 |
}
|
|
|
280 |
|
|
|
281 |
/**
|
|
|
282 |
* Set the DOM document encoding.
|
|
|
283 |
* @param string $v
|
|
|
284 |
*/
|
|
|
285 |
public function setEncoding($v) {
|
|
|
286 |
$this->encoding = $v;
|
|
|
287 |
}
|
|
|
288 |
|
|
|
289 |
/**
|
|
|
290 |
* @param boolean $v
|
|
|
291 |
*/
|
|
|
292 |
public function setFormatOutput($v) {
|
|
|
293 |
$this->formatOutput = (boolean) $v;
|
|
|
294 |
}
|
|
|
295 |
|
|
|
296 |
/**
|
|
|
297 |
* Gets a default output writer for this task.
|
|
|
298 |
* @return Writer
|
|
|
299 |
*/
|
|
|
300 |
private function getDefaultOutput()
|
|
|
301 |
{
|
|
|
302 |
return new LogWriter($this->parentTask);
|
|
|
303 |
}
|
|
|
304 |
|
|
|
305 |
/**
|
|
|
306 |
* Gets the formatter that has been configured based on this element.
|
|
|
307 |
* @return PDOResultFormatter
|
|
|
308 |
*/
|
|
|
309 |
function getFormatter() {
|
|
|
310 |
return $this->formatter;
|
|
|
311 |
}
|
|
|
312 |
}
|