diff options
-rw-r--r-- | src/Components/Array2d.php | 10 | ||||
-rw-r--r-- | src/Components/ArrayObj.php | 22 | ||||
-rw-r--r-- | src/Components/IntoKeyword.php | 16 | ||||
-rw-r--r-- | src/Components/RenameOperation.php | 21 | ||||
-rw-r--r-- | tests/Components/Array2dTest.php | 9 | ||||
-rw-r--r-- | tests/Components/IntoKeywordTest.php | 12 | ||||
-rw-r--r-- | tests/Components/RenameOperationTest.php | 18 |
7 files changed, 100 insertions, 8 deletions
diff --git a/src/Components/Array2d.php b/src/Components/Array2d.php index c53c8be..c616fdb 100644 --- a/src/Components/Array2d.php +++ b/src/Components/Array2d.php @@ -118,4 +118,14 @@ class Array2d extends Component --$list->idx; return $ret; } + + /** + * @param ArrayObj[] $component The component to be built. + * + * @return string + */ + public static function build($component) + { + return ArrayObj::build($component); + } } diff --git a/src/Components/ArrayObj.php b/src/Components/ArrayObj.php index 8dde518..ef0f79f 100644 --- a/src/Components/ArrayObj.php +++ b/src/Components/ArrayObj.php @@ -133,20 +133,28 @@ class ArrayObj extends Component } /** - * @param ArrayObj $component The component to be built. + * @param ArrayObj|ArrayObj[] $component The component to be built. * * @return string */ public static function build($component) { - $values = array(); - if (!empty($component->raw)) { - $values = $component->raw; + if (is_array($component)) { + $values = array(); + foreach ($component as $c) { + $values[] = static::build($c); + } + return implode(', ', $values); } else { - foreach ($component->values as $value) { - $values[] = $value; + $values = array(); + if (!empty($component->raw)) { + $values = $component->raw; + } else { + foreach ($component->values as $value) { + $values[] = $value; + } } + return '(' . implode(', ', $values) . ')'; } - return '(' . implode(', ', $values) . ')'; } } diff --git a/src/Components/IntoKeyword.php b/src/Components/IntoKeyword.php index cb25481..9b52c7c 100644 --- a/src/Components/IntoKeyword.php +++ b/src/Components/IntoKeyword.php @@ -128,4 +128,20 @@ class IntoKeyword extends Component --$list->idx; return $ret; } + + /** + * @param IntoKeyword $component The component to be built. + * + * @return string + */ + public static function build($component) + { + if ($component->type === 'OUTFILE') { + return 'OUTFILE "' . $component->dest . '"'; + } else { + $columns = !empty($component->columns) ? + '(' . implode(', ', $component->columns) . ')' : ''; + return Expression::build($component->dest) . $columns; + } + } } diff --git a/src/Components/RenameOperation.php b/src/Components/RenameOperation.php index 7d115fb..cc9630c 100644 --- a/src/Components/RenameOperation.php +++ b/src/Components/RenameOperation.php @@ -44,7 +44,7 @@ class RenameOperation extends Component * @param TokensList $list The list of tokens that are being parsed. * @param array $options Parameters for parsing. * - * @return RenameOperation + * @return RenameOperation[] */ public static function parse(Parser $parser, TokensList $list, array $options = array()) { @@ -157,4 +157,23 @@ class RenameOperation extends Component --$list->idx; return $ret; } + + /** + * @param RenameOperation $component The component to be built. + * + * @return string + */ + public static function build($component) + { + if (is_array($component)) { + $values = array(); + foreach ($component as $c) { + $values[] = static::build($c); + } + return implode(', ', $values); + } else { + return Expression::build($component->old) . ' TO ' + . Expression::build($component->new); + } + } } diff --git a/tests/Components/Array2dTest.php b/tests/Components/Array2dTest.php index 0ceb345..b268d99 100644 --- a/tests/Components/Array2dTest.php +++ b/tests/Components/Array2dTest.php @@ -20,6 +20,15 @@ class Array2dTest extends TestCase ); } + public function testBuild() + { + $arrays = Array2d::parse(new Parser(), $this->getTokensList('(1, 2), (3, 4), (5, 6)')); + $this->assertEquals( + '(1, 2), (3, 4), (5, 6)', + Array2d::build($arrays) + ); + } + public function testParseErr1() { $parser = new Parser(); diff --git a/tests/Components/IntoKeywordTest.php b/tests/Components/IntoKeywordTest.php index ee30f09..dad7077 100644 --- a/tests/Components/IntoKeywordTest.php +++ b/tests/Components/IntoKeywordTest.php @@ -17,6 +17,18 @@ class IntoKeywordTest extends TestCase $this->assertEquals($component->dest, '/tmp/outfile.txt'); } + public function testBuild() + { + $component = IntoKeyword::parse(new Parser(), $this->getTokensList('tbl(col1, col2)')); + $this->assertEquals('tbl(col1, col2)', IntoKeyword::build($component)); + } + + public function testBuildOutfile() + { + $component = IntoKeyword::parse(new Parser(), $this->getTokensList('OUTFILE "/tmp/outfile.txt"')); + $this->assertEquals('OUTFILE "/tmp/outfile.txt"', IntoKeyword::build($component)); + } + public function testParseErr1() { $component = IntoKeyword::parse(new Parser(), $this->getTokensList('OUTFILE;')); diff --git a/tests/Components/RenameOperationTest.php b/tests/Components/RenameOperationTest.php new file mode 100644 index 0000000..b6a215c --- /dev/null +++ b/tests/Components/RenameOperationTest.php @@ -0,0 +1,18 @@ +<?php + +namespace SqlParser\Tests\Components; + +use SqlParser\Parser; +use SqlParser\Components\RenameOperation; + +use SqlParser\Tests\TestCase; + +class RenameOperationTest extends TestCase +{ + + public function testBuild() + { + $component = RenameOperation::parse(new Parser(), $this->getTokensList('a TO b, c TO d')); + $this->assertEquals(RenameOperation::build($component), 'a TO b, c TO d'); + } +} |