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 Phing\Io\File; |
24
|
|
|
use Phing\Util\StringHelper; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Plain text formatter for PDO results. |
28
|
|
|
* |
29
|
|
|
* @author Hans Lellelid <[email protected]> |
30
|
|
|
* |
31
|
|
|
* @since 2.3.0 |
32
|
|
|
*/ |
33
|
|
|
class PlainPDOResultFormatter extends PDOResultFormatter |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* Have column headers been printed? |
37
|
|
|
* |
38
|
|
|
* @var bool |
39
|
|
|
*/ |
40
|
|
|
private $colsprinted = false; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Whether to show headers. |
44
|
|
|
* |
45
|
|
|
* @var bool |
46
|
|
|
*/ |
47
|
|
|
private $showheaders = true; |
48
|
|
|
private $showtrailers = false; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Column delimiter. |
52
|
|
|
* Defaults to ','. |
53
|
|
|
* |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
private $coldelimiter = ','; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Row delimiter. |
60
|
|
|
* Defaults to PHP_EOL. |
61
|
|
|
* |
62
|
|
|
* @var string |
63
|
|
|
*/ |
64
|
|
|
private $rowdelimiter = PHP_EOL; |
65
|
|
|
private $statementcounter = 0; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Set the showheaders attribute. |
69
|
|
|
* |
70
|
|
|
* @param bool $v |
71
|
|
|
*/ |
72
|
2 |
|
public function setShowheaders($v) |
73
|
|
|
{ |
74
|
2 |
|
$this->showheaders = StringHelper::booleanValue($v); |
75
|
2 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Set the showtrailers attribute. |
79
|
|
|
* |
80
|
|
|
* @param bool $v |
81
|
|
|
*/ |
82
|
2 |
|
public function setShowtrailers($v) |
83
|
|
|
{ |
84
|
2 |
|
$this->showtrailers = StringHelper::booleanValue($v); |
85
|
2 |
|
} |
86
|
|
|
|
87
|
2 |
|
public function setStatementCounter($count) |
88
|
|
|
{ |
89
|
2 |
|
$this->statementcounter = $count; |
90
|
2 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Sets the column delimiter. |
94
|
|
|
* |
95
|
|
|
* @param string $v |
96
|
|
|
*/ |
97
|
2 |
|
public function setColdelim($v) |
98
|
|
|
{ |
99
|
2 |
|
$this->coldelimiter = $v; |
100
|
2 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Sets the row delimiter. |
104
|
|
|
* |
105
|
|
|
* @param string $v |
106
|
|
|
*/ |
107
|
2 |
|
public function setRowdelim($v) |
108
|
|
|
{ |
109
|
2 |
|
$this->rowdelimiter = $v; |
110
|
2 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Processes a specific row from PDO result set. |
114
|
|
|
* |
115
|
|
|
* @param array $row row of PDO result set |
116
|
|
|
*/ |
117
|
1 |
|
public function processRow($row) |
118
|
|
|
{ |
119
|
1 |
|
$line = ''; |
120
|
|
|
|
121
|
1 |
|
if ($this->showtrailers) { |
122
|
1 |
|
$this->out->write('# ' . $this->statementcounter . ' statement(s) successful executed.' . PHP_EOL); |
123
|
|
|
} |
124
|
|
|
|
125
|
1 |
|
if (!$this->colsprinted && $this->showheaders) { |
126
|
1 |
|
$first = true; |
127
|
1 |
|
foreach ($row as $fieldName => $ignore) { |
128
|
1 |
|
if ($first) { |
129
|
1 |
|
$first = false; |
130
|
|
|
} else { |
131
|
1 |
|
$line .= ','; |
132
|
|
|
} |
133
|
1 |
|
$line .= $fieldName; |
134
|
|
|
} |
135
|
|
|
|
136
|
1 |
|
$this->out->write($line); |
137
|
1 |
|
$this->out->write(PHP_EOL); |
138
|
|
|
|
139
|
1 |
|
$line = ''; |
140
|
1 |
|
$this->colsprinted = true; |
141
|
|
|
} // if show headers |
142
|
|
|
|
143
|
1 |
|
$first = true; |
144
|
1 |
|
foreach ($row as $columnValue) { |
145
|
1 |
|
if (null !== $columnValue) { |
146
|
1 |
|
$columnValue = trim($columnValue); |
147
|
|
|
} |
148
|
|
|
|
149
|
1 |
|
if ($first) { |
150
|
1 |
|
$first = false; |
151
|
|
|
} else { |
152
|
1 |
|
$line .= $this->coldelimiter; |
153
|
|
|
} |
154
|
1 |
|
$line .= $columnValue; |
155
|
|
|
} |
156
|
|
|
|
157
|
1 |
|
$this->out->write($line); |
158
|
1 |
|
$this->out->write($this->rowdelimiter); |
159
|
1 |
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return File |
163
|
|
|
*/ |
164
|
|
|
public function getPreferredOutfile() |
165
|
|
|
{ |
166
|
|
|
return new File('results.txt'); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|