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