| 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/PDOResultFormatter.php';
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* XML formatter for PDO results.
|
|
|
27 |
*
|
|
|
28 |
* This class reprsents the output of a query using a simple XML schema.
|
|
|
29 |
*
|
|
|
30 |
* <results>
|
|
|
31 |
* <row>
|
|
|
32 |
* <col name="id">value</col>
|
|
|
33 |
* <col name="name">value2</col>
|
|
|
34 |
* </row>
|
|
|
35 |
* <row>
|
|
|
36 |
* <col name="id">value</col>
|
|
|
37 |
* <col name="name">value2</col>
|
|
|
38 |
* </row>
|
|
|
39 |
* </results>
|
|
|
40 |
*
|
|
|
41 |
* The actual names of the colums will depend on the fetchmode that was used
|
|
|
42 |
* with PDO.
|
|
|
43 |
*
|
|
|
44 |
* @author Hans Lellelid <hans@xmpl.org>
|
|
|
45 |
* @package phing.tasks.ext.pdo
|
|
|
46 |
* @since 2.3.0
|
|
|
47 |
*/
|
|
|
48 |
class XMLPDOResultFormatter extends PDOResultFormatter {
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* The XML document being created.
|
|
|
52 |
* @var DOMDocument
|
|
|
53 |
*/
|
|
|
54 |
private $doc;
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* @var DOMElement
|
|
|
58 |
*/
|
|
|
59 |
private $rootNode;
|
|
|
60 |
|
|
|
61 |
/**
|
|
|
62 |
* XML document encoding
|
|
|
63 |
*
|
|
|
64 |
* @var string
|
|
|
65 |
*/
|
|
|
66 |
private $encoding;
|
|
|
67 |
|
|
|
68 |
/**
|
|
|
69 |
* @var boolean
|
|
|
70 |
*/
|
|
|
71 |
private $formatOutput = true;
|
|
|
72 |
|
|
|
73 |
/**
|
|
|
74 |
* Set the DOM document encoding.
|
|
|
75 |
* @param string $v
|
|
|
76 |
*/
|
|
|
77 |
public function setEncoding($v) {
|
|
|
78 |
$this->encoding = $v;
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
/**
|
|
|
82 |
* @param boolean $v
|
|
|
83 |
*/
|
|
|
84 |
public function setFormatOutput($v) {
|
|
|
85 |
$this->formatOutput = (boolean) $v;
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
public function initialize() {
|
|
|
89 |
$this->doc = new DOMDocument("1.0", $this->encoding);
|
|
|
90 |
$this->rootNode = $this->doc->createElement('results');
|
|
|
91 |
$this->doc->appendChild($this->rootNode);
|
|
|
92 |
$this->doc->formatOutput = $this->formatOutput;
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
/**
|
|
|
96 |
* Processes a specific row from PDO result set.
|
|
|
97 |
*
|
|
|
98 |
* @param array $row Row of PDO result set.
|
|
|
99 |
*/
|
|
|
100 |
public function processRow($row) {
|
|
|
101 |
|
|
|
102 |
$rowNode = $this->doc->createElement('row');
|
|
|
103 |
$this->rootNode->appendChild($rowNode);
|
|
|
104 |
|
|
|
105 |
foreach($row as $columnName => $columnValue) {
|
|
|
106 |
|
|
|
107 |
$colNode = $this->doc->createElement('column');
|
|
|
108 |
$colNode->setAttribute('name', $columnName);
|
|
|
109 |
|
|
|
110 |
if ($columnValue != null) {
|
|
|
111 |
$columnValue = trim($columnValue);
|
|
|
112 |
$colNode->nodeValue = $columnValue;
|
|
|
113 |
}
|
|
|
114 |
$rowNode->appendChild($colNode);
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
/**
|
|
|
120 |
* Gets a preferred filename for an output file.
|
|
|
121 |
*
|
|
|
122 |
* If no filename is specified, this is where the results will be placed
|
|
|
123 |
* (unless usefile=false).
|
|
|
124 |
*
|
|
|
125 |
* @return string
|
|
|
126 |
*/
|
|
|
127 |
public function getPreferredOutfile()
|
|
|
128 |
{
|
|
|
129 |
return new PhingFile('results.xml');
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
/**
|
|
|
133 |
* Write XML to file and free the DOM objects.
|
|
|
134 |
*/
|
|
|
135 |
public function close() {
|
|
|
136 |
$this->out->write($this->doc->saveXML());
|
|
|
137 |
$this->rootNode = null;
|
|
|
138 |
$this->doc = null;
|
|
|
139 |
parent::close();
|
|
|
140 |
}
|
|
|
141 |
}
|