Skip to content

Commit d534d36

Browse files
authored
Handle SubQuery in ref_clause (#324)
* Fix: Tests. * Fix issue #322. * Fix: Handle SubQuery in ref_clause.
1 parent 63189f5 commit d534d36

5 files changed

Lines changed: 32 additions & 16 deletions

File tree

src/PHPSQLParser/builders/FromBuilder.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@
3131
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3232
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3333
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34-
*
34+
*
3535
* @author André Rothe <andre.rothe@phosco.info>
3636
* @copyright 2010-2014 Justin Swanhart and André Rothe
3737
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
3838
* @version SVN: $Id$
39-
*
39+
*
4040
*/
4141

4242
namespace PHPSQLParser\builders;
@@ -48,7 +48,7 @@
4848
*
4949
* @author André Rothe <andre.rothe@phosco.info>
5050
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
51-
*
51+
*
5252
*/
5353
class FromBuilder implements Builder {
5454

src/PHPSQLParser/builders/RefClauseBuilder.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,24 @@
3131
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3232
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3333
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34-
*
34+
*
3535
* @author André Rothe <andre.rothe@phosco.info>
3636
* @copyright 2010-2014 Justin Swanhart and André Rothe
3737
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
3838
* @version SVN: $Id$
39-
*
39+
*
4040
*/
4141

4242
namespace PHPSQLParser\builders;
4343
use PHPSQLParser\exceptions\UnableToCreateSQLException;
4444

4545
/**
46-
* This class implements the references clause within a JOIN.
46+
* This class implements the references clause within a JOIN.
4747
* You can overwrite all functions to achieve another handling.
4848
*
4949
* @author André Rothe <andre.rothe@phosco.info>
5050
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
51-
*
51+
*
5252
*/
5353
class RefClauseBuilder implements Builder {
5454

@@ -86,7 +86,12 @@ protected function buildColumnList($parsed) {
8686
$builder = new ColumnListBuilder();
8787
return $builder->build($parsed);
8888
}
89-
89+
90+
protected function buildSubQuery($parsed) {
91+
$builder = new SubQueryBuilder();
92+
return $builder->build($parsed);
93+
}
94+
9095
public function build(array $parsed) {
9196
if ($parsed === false) {
9297
return '';
@@ -101,6 +106,7 @@ public function build(array $parsed) {
101106
$sql .= $this->buildBracketExpression($v);
102107
$sql .= $this->buildInList($v);
103108
$sql .= $this->buildColumnList($v);
109+
$sql .= $this->buildSubQuery($v);
104110

105111
if ($len == strlen($sql)) {
106112
throw new UnableToCreateSQLException('expression ref_clause', $k, $v, 'expr_type');

src/PHPSQLParser/builders/TableBuilder.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,24 @@
3131
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3232
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3333
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34-
*
34+
*
3535
* @author André Rothe <andre.rothe@phosco.info>
3636
* @copyright 2010-2014 Justin Swanhart and André Rothe
3737
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
3838
* @version SVN: $Id$
39-
*
39+
*
4040
*/
4141

4242
namespace PHPSQLParser\builders;
4343
use PHPSQLParser\utils\ExpressionType;
4444

4545
/**
46-
* This class implements the builder for the table name and join options.
46+
* This class implements the builder for the table name and join options.
4747
* You can overwrite all functions to achieve another handling.
4848
*
4949
* @author André Rothe <andre.rothe@phosco.info>
5050
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
51-
*
51+
*
5252
*/
5353
class TableBuilder implements Builder {
5454

src/PHPSQLParser/builders/WhereExpressionBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public function build(array $parsed) {
119119
$sql .= $this->buildUserVariable($v);
120120
$sql .= $this->buildSubQuery($v);
121121
$sql .= $this->buildReserved($v);
122-
122+
123123
if ($len == strlen($sql)) {
124124
throw new UnableToCreateSQLException('WHERE expression subtree', $k, $v, 'expr_type');
125125
}

tests/cases/creator/leftTest.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,19 @@
3131
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3232
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3333
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34-
*
34+
*
3535
* @author André Rothe <andre.rothe@phosco.info>
3636
* @copyright 2010-2014 Justin Swanhart and André Rothe
3737
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
3838
* @version SVN: $Id$
39-
*
39+
*
4040
*/
4141
namespace PHPSQLParser\Test\Creator;
4242
use PHPSQLParser\PHPSQLParser;
4343
use PHPSQLParser\PHPSQLCreator;
4444

4545
class leftTest extends \PHPUnit_Framework_TestCase {
46-
46+
4747
public function testLeft() {
4848
$sql = 'SELECT *
4949
FROM (t1 LEFT JOIN t2 ON t1.a=t2.a)
@@ -56,5 +56,15 @@ public function testLeft() {
5656
$this->assertSame($expected, $created, 'left joins and table-expression');
5757

5858
}
59+
60+
public function testLeftIn() {
61+
$sql = 'SELECT *
62+
FROM (t1 LEFT JOIN t2 ON t1.a=t2.a)
63+
LEFT JOIN t3
64+
ON t3.id IN (SELECT id FROM t4)';
65+
$parser = new PHPSQLParser($sql);
66+
$creator = new PHPSQLCreator($parser->parsed);
67+
$created = $creator->created;
68+
}
5969
}
6070

0 commit comments

Comments
 (0)