From e6bb45c6cbbe6ecb9340cea0f902eb9db142dcc9 Mon Sep 17 00:00:00 2001 From: Ahmed El Moden Date: Thu, 1 Oct 2020 13:12:41 +0200 Subject: [PATCH] Fix: Handle SubQuery in ref_clause. --- src/PHPSQLParser/builders/FromBuilder.php | 6 +++--- src/PHPSQLParser/builders/RefClauseBuilder.php | 16 +++++++++++----- src/PHPSQLParser/builders/TableBuilder.php | 8 ++++---- .../builders/WhereExpressionBuilder.php | 2 +- tests/cases/creator/leftTest.php | 16 +++++++++++++--- 5 files changed, 32 insertions(+), 16 deletions(-) diff --git a/src/PHPSQLParser/builders/FromBuilder.php b/src/PHPSQLParser/builders/FromBuilder.php index cffe8deb..1679ac97 100644 --- a/src/PHPSQLParser/builders/FromBuilder.php +++ b/src/PHPSQLParser/builders/FromBuilder.php @@ -31,12 +31,12 @@ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * + * * @author André Rothe * @copyright 2010-2014 Justin Swanhart and André Rothe * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) * @version SVN: $Id$ - * + * */ namespace PHPSQLParser\builders; @@ -48,7 +48,7 @@ * * @author André Rothe * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) - * + * */ class FromBuilder implements Builder { diff --git a/src/PHPSQLParser/builders/RefClauseBuilder.php b/src/PHPSQLParser/builders/RefClauseBuilder.php index df0deec7..a7965423 100644 --- a/src/PHPSQLParser/builders/RefClauseBuilder.php +++ b/src/PHPSQLParser/builders/RefClauseBuilder.php @@ -31,24 +31,24 @@ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * + * * @author André Rothe * @copyright 2010-2014 Justin Swanhart and André Rothe * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) * @version SVN: $Id$ - * + * */ namespace PHPSQLParser\builders; use PHPSQLParser\exceptions\UnableToCreateSQLException; /** - * This class implements the references clause within a JOIN. + * This class implements the references clause within a JOIN. * You can overwrite all functions to achieve another handling. * * @author André Rothe * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) - * + * */ class RefClauseBuilder implements Builder { @@ -86,7 +86,12 @@ protected function buildColumnList($parsed) { $builder = new ColumnListBuilder(); return $builder->build($parsed); } - + + protected function buildSubQuery($parsed) { + $builder = new SubQueryBuilder(); + return $builder->build($parsed); + } + public function build(array $parsed) { if ($parsed === false) { return ''; @@ -101,6 +106,7 @@ public function build(array $parsed) { $sql .= $this->buildBracketExpression($v); $sql .= $this->buildInList($v); $sql .= $this->buildColumnList($v); + $sql .= $this->buildSubQuery($v); if ($len == strlen($sql)) { throw new UnableToCreateSQLException('expression ref_clause', $k, $v, 'expr_type'); diff --git a/src/PHPSQLParser/builders/TableBuilder.php b/src/PHPSQLParser/builders/TableBuilder.php index 3ced04ab..e9282343 100644 --- a/src/PHPSQLParser/builders/TableBuilder.php +++ b/src/PHPSQLParser/builders/TableBuilder.php @@ -31,24 +31,24 @@ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * + * * @author André Rothe * @copyright 2010-2014 Justin Swanhart and André Rothe * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) * @version SVN: $Id$ - * + * */ namespace PHPSQLParser\builders; use PHPSQLParser\utils\ExpressionType; /** - * This class implements the builder for the table name and join options. + * This class implements the builder for the table name and join options. * You can overwrite all functions to achieve another handling. * * @author André Rothe * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) - * + * */ class TableBuilder implements Builder { diff --git a/src/PHPSQLParser/builders/WhereExpressionBuilder.php b/src/PHPSQLParser/builders/WhereExpressionBuilder.php index 610558b5..bf5b8868 100644 --- a/src/PHPSQLParser/builders/WhereExpressionBuilder.php +++ b/src/PHPSQLParser/builders/WhereExpressionBuilder.php @@ -119,7 +119,7 @@ public function build(array $parsed) { $sql .= $this->buildUserVariable($v); $sql .= $this->buildSubQuery($v); $sql .= $this->buildReserved($v); - + if ($len == strlen($sql)) { throw new UnableToCreateSQLException('WHERE expression subtree', $k, $v, 'expr_type'); } diff --git a/tests/cases/creator/leftTest.php b/tests/cases/creator/leftTest.php index ebe49bf6..7f6d553d 100644 --- a/tests/cases/creator/leftTest.php +++ b/tests/cases/creator/leftTest.php @@ -31,19 +31,19 @@ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * + * * @author André Rothe * @copyright 2010-2014 Justin Swanhart and André Rothe * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) * @version SVN: $Id$ - * + * */ namespace PHPSQLParser\Test\Creator; use PHPSQLParser\PHPSQLParser; use PHPSQLParser\PHPSQLCreator; class leftTest extends \PHPUnit_Framework_TestCase { - + public function testLeft() { $sql = 'SELECT * FROM (t1 LEFT JOIN t2 ON t1.a=t2.a) @@ -56,5 +56,15 @@ public function testLeft() { $this->assertSame($expected, $created, 'left joins and table-expression'); } + + public function testLeftIn() { + $sql = 'SELECT * + FROM (t1 LEFT JOIN t2 ON t1.a=t2.a) + LEFT JOIN t3 + ON t3.id IN (SELECT id FROM t4)'; + $parser = new PHPSQLParser($sql); + $creator = new PHPSQLCreator($parser->parsed); + $created = $creator->created; + } }