1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
6
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
7
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
8
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
9
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
10
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
11
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
12
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
13
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
14
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
15
|
|
|
* |
16
|
|
|
* This software consists of voluntary contributions made by many individuals |
17
|
|
|
* and is licensed under the LGPL. For more information please see |
18
|
|
|
* <http://phing.info>. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace Phing\Task\System\Pdo; |
22
|
|
|
|
23
|
|
|
use DOMDocument; |
24
|
|
|
use DOMElement; |
25
|
|
|
use Phing\Io\File; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* XML formatter for PDO results. |
29
|
|
|
* |
30
|
|
|
* This class reprsents the output of a query using a simple XML schema. |
31
|
|
|
* |
32
|
|
|
* <results> |
33
|
|
|
* <row> |
34
|
|
|
* <col name="id">value</col> |
35
|
|
|
* <col name="name">value2</col> |
36
|
|
|
* </row> |
37
|
|
|
* <row> |
38
|
|
|
* <col name="id">value</col> |
39
|
|
|
* <col name="name">value2</col> |
40
|
|
|
* </row> |
41
|
|
|
* </results> |
42
|
|
|
* |
43
|
|
|
* The actual names of the colums will depend on the fetchmode that was used |
44
|
|
|
* with PDO. |
45
|
|
|
* |
46
|
|
|
* @author Hans Lellelid <[email protected]> |
47
|
|
|
* |
48
|
|
|
* @since 2.3.0 |
49
|
|
|
*/ |
50
|
|
|
class XMLPDOResultFormatter extends PDOResultFormatter |
51
|
|
|
{ |
52
|
|
|
/** |
53
|
|
|
* The XML document being created. |
54
|
|
|
* |
55
|
|
|
* @var DOMDocument |
56
|
|
|
*/ |
57
|
|
|
private $doc; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var DOMElement |
61
|
|
|
*/ |
62
|
|
|
private $rootNode; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* XML document encoding. |
66
|
|
|
* |
67
|
|
|
* @var string |
68
|
|
|
*/ |
69
|
|
|
private $encoding; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var bool |
73
|
|
|
*/ |
74
|
|
|
private $formatOutput = true; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Set the DOM document encoding. |
78
|
|
|
* |
79
|
|
|
* @param string $v |
80
|
|
|
*/ |
81
|
1 |
|
public function setEncoding($v) |
82
|
|
|
{ |
83
|
1 |
|
$this->encoding = $v; |
84
|
1 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param bool $v |
88
|
|
|
*/ |
89
|
1 |
|
public function setFormatOutput($v) |
90
|
|
|
{ |
91
|
1 |
|
$this->formatOutput = (bool) $v; |
92
|
1 |
|
} |
93
|
|
|
|
94
|
1 |
|
public function initialize() |
95
|
|
|
{ |
96
|
1 |
|
$this->doc = new DOMDocument('1.0', $this->encoding); |
97
|
1 |
|
$this->rootNode = $this->doc->createElement('results'); |
98
|
1 |
|
$this->doc->appendChild($this->rootNode); |
99
|
1 |
|
$this->doc->formatOutput = $this->formatOutput; |
100
|
1 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Processes a specific row from PDO result set. |
104
|
|
|
* |
105
|
|
|
* @param array $row row of PDO result set |
106
|
|
|
*/ |
107
|
|
|
public function processRow($row) |
108
|
|
|
{ |
109
|
|
|
$rowNode = $this->doc->createElement('row'); |
110
|
|
|
$this->rootNode->appendChild($rowNode); |
111
|
|
|
|
112
|
|
|
foreach ($row as $columnName => $columnValue) { |
113
|
|
|
$colNode = $this->doc->createElement('column'); |
114
|
|
|
$colNode->setAttribute('name', $columnName); |
115
|
|
|
|
116
|
|
|
if (null != $columnValue) { |
117
|
|
|
$columnValue = trim($columnValue); |
118
|
|
|
$colNode->nodeValue = $columnValue; |
119
|
|
|
} |
120
|
|
|
$rowNode->appendChild($colNode); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Gets a preferred filename for an output file. |
126
|
|
|
* |
127
|
|
|
* If no filename is specified, this is where the results will be placed |
128
|
|
|
* (unless usefile=false). |
129
|
|
|
* |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
|
|
public function getPreferredOutfile() |
133
|
|
|
{ |
134
|
|
|
return new File('results.xml'); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Write XML to file and free the DOM objects. |
139
|
|
|
*/ |
140
|
1 |
|
public function close() |
141
|
|
|
{ |
142
|
1 |
|
$this->out->write($this->doc->saveXML()); |
143
|
1 |
|
$this->rootNode = null; |
144
|
1 |
|
$this->doc = null; |
145
|
1 |
|
parent::close(); |
146
|
1 |
|
} |
147
|
|
|
} |
148
|
|
|
|