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
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Column delimiter. |
51
|
|
|
* Defaults to ','. |
52
|
|
|
* |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
private $coldelimiter = ','; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Row delimiter. |
59
|
|
|
* Defaults to PHP_EOL. |
60
|
|
|
* |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
private $rowdelimiter = PHP_EOL; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Set the showheaders attribute. |
67
|
|
|
* |
68
|
|
|
* @param bool $v |
69
|
|
|
*/ |
70
|
1 |
|
public function setShowheaders($v) |
71
|
|
|
{ |
72
|
1 |
|
$this->showheaders = StringHelper::booleanValue($v); |
73
|
1 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Sets the column delimiter. |
77
|
|
|
* |
78
|
|
|
* @param string $v |
79
|
|
|
*/ |
80
|
1 |
|
public function setColdelim($v) |
81
|
|
|
{ |
82
|
1 |
|
$this->coldelimiter = $v; |
83
|
1 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Sets the row delimiter. |
87
|
|
|
* |
88
|
|
|
* @param string $v |
89
|
|
|
*/ |
90
|
1 |
|
public function setRowdelim($v) |
91
|
|
|
{ |
92
|
1 |
|
$this->rowdelimiter = $v; |
93
|
1 |
|
} |
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
|
|
|
$line = ''; |
103
|
|
|
|
104
|
|
|
if (!$this->colsprinted && $this->showheaders) { |
105
|
|
|
$first = true; |
106
|
|
|
foreach ($row as $fieldName => $ignore) { |
107
|
|
|
if ($first) { |
108
|
|
|
$first = false; |
109
|
|
|
} else { |
110
|
|
|
$line .= ','; |
111
|
|
|
} |
112
|
|
|
$line .= $fieldName; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$this->out->write($line); |
116
|
|
|
$this->out->write(PHP_EOL); |
117
|
|
|
|
118
|
|
|
$line = ''; |
119
|
|
|
$this->colsprinted = true; |
120
|
|
|
} // if show headers |
121
|
|
|
|
122
|
|
|
$first = true; |
123
|
|
|
foreach ($row as $columnValue) { |
124
|
|
|
if (null !== $columnValue) { |
125
|
|
|
$columnValue = trim($columnValue); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
if ($first) { |
129
|
|
|
$first = false; |
130
|
|
|
} else { |
131
|
|
|
$line .= $this->coldelimiter; |
132
|
|
|
} |
133
|
|
|
$line .= $columnValue; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$this->out->write($line); |
137
|
|
|
$this->out->write($this->rowdelimiter); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return File |
142
|
|
|
*/ |
143
|
|
|
public function getPreferredOutfile() |
144
|
|
|
{ |
145
|
|
|
return new File('results.txt'); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|