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