Skip to content

Commit 09a7047

Browse files
committed
Finished builders for components.
1 parent a36dbf2 commit 09a7047

7 files changed

Lines changed: 100 additions & 8 deletions

File tree

src/Components/Array2d.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,14 @@ public static function parse(Parser $parser, TokensList $list, array $options =
118118
--$list->idx;
119119
return $ret;
120120
}
121+
122+
/**
123+
* @param ArrayObj[] $component The component to be built.
124+
*
125+
* @return string
126+
*/
127+
public static function build($component)
128+
{
129+
return ArrayObj::build($component);
130+
}
121131
}

src/Components/ArrayObj.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,20 +133,28 @@ public static function parse(Parser $parser, TokensList $list, array $options =
133133
}
134134

135135
/**
136-
* @param ArrayObj $component The component to be built.
136+
* @param ArrayObj|ArrayObj[] $component The component to be built.
137137
*
138138
* @return string
139139
*/
140140
public static function build($component)
141141
{
142-
$values = array();
143-
if (!empty($component->raw)) {
144-
$values = $component->raw;
142+
if (is_array($component)) {
143+
$values = array();
144+
foreach ($component as $c) {
145+
$values[] = static::build($c);
146+
}
147+
return implode(', ', $values);
145148
} else {
146-
foreach ($component->values as $value) {
147-
$values[] = $value;
149+
$values = array();
150+
if (!empty($component->raw)) {
151+
$values = $component->raw;
152+
} else {
153+
foreach ($component->values as $value) {
154+
$values[] = $value;
155+
}
148156
}
157+
return '(' . implode(', ', $values) . ')';
149158
}
150-
return '(' . implode(', ', $values) . ')';
151159
}
152160
}

src/Components/IntoKeyword.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,20 @@ public static function parse(Parser $parser, TokensList $list, array $options =
128128
--$list->idx;
129129
return $ret;
130130
}
131+
132+
/**
133+
* @param IntoKeyword $component The component to be built.
134+
*
135+
* @return string
136+
*/
137+
public static function build($component)
138+
{
139+
if ($component->type === 'OUTFILE') {
140+
return 'OUTFILE "' . $component->dest . '"';
141+
} else {
142+
$columns = !empty($component->columns) ?
143+
'(' . implode(', ', $component->columns) . ')' : '';
144+
return Expression::build($component->dest) . $columns;
145+
}
146+
}
131147
}

src/Components/RenameOperation.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class RenameOperation extends Component
4444
* @param TokensList $list The list of tokens that are being parsed.
4545
* @param array $options Parameters for parsing.
4646
*
47-
* @return RenameOperation
47+
* @return RenameOperation[]
4848
*/
4949
public static function parse(Parser $parser, TokensList $list, array $options = array())
5050
{
@@ -157,4 +157,23 @@ public static function parse(Parser $parser, TokensList $list, array $options =
157157
--$list->idx;
158158
return $ret;
159159
}
160+
161+
/**
162+
* @param RenameOperation $component The component to be built.
163+
*
164+
* @return string
165+
*/
166+
public static function build($component)
167+
{
168+
if (is_array($component)) {
169+
$values = array();
170+
foreach ($component as $c) {
171+
$values[] = static::build($c);
172+
}
173+
return implode(', ', $values);
174+
} else {
175+
return Expression::build($component->old) . ' TO '
176+
. Expression::build($component->new);
177+
}
178+
}
160179
}

tests/Components/Array2dTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ public function testParse()
2020
);
2121
}
2222

23+
public function testBuild()
24+
{
25+
$arrays = Array2d::parse(new Parser(), $this->getTokensList('(1, 2), (3, 4), (5, 6)'));
26+
$this->assertEquals(
27+
'(1, 2), (3, 4), (5, 6)',
28+
Array2d::build($arrays)
29+
);
30+
}
31+
2332
public function testParseErr1()
2433
{
2534
$parser = new Parser();

tests/Components/IntoKeywordTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@ public function testParse()
1717
$this->assertEquals($component->dest, '/tmp/outfile.txt');
1818
}
1919

20+
public function testBuild()
21+
{
22+
$component = IntoKeyword::parse(new Parser(), $this->getTokensList('tbl(col1, col2)'));
23+
$this->assertEquals('tbl(col1, col2)', IntoKeyword::build($component));
24+
}
25+
26+
public function testBuildOutfile()
27+
{
28+
$component = IntoKeyword::parse(new Parser(), $this->getTokensList('OUTFILE "/tmp/outfile.txt"'));
29+
$this->assertEquals('OUTFILE "/tmp/outfile.txt"', IntoKeyword::build($component));
30+
}
31+
2032
public function testParseErr1()
2133
{
2234
$component = IntoKeyword::parse(new Parser(), $this->getTokensList('OUTFILE;'));
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace SqlParser\Tests\Components;
4+
5+
use SqlParser\Parser;
6+
use SqlParser\Components\RenameOperation;
7+
8+
use SqlParser\Tests\TestCase;
9+
10+
class RenameOperationTest extends TestCase
11+
{
12+
13+
public function testBuild()
14+
{
15+
$component = RenameOperation::parse(new Parser(), $this->getTokensList('a TO b, c TO d'));
16+
$this->assertEquals(RenameOperation::build($component), 'a TO b, c TO d');
17+
}
18+
}

0 commit comments

Comments
 (0)