Skip to content

Commit

Permalink
Fix: Handle SubQuery in ref_clause.
Browse files Browse the repository at this point in the history
  • Loading branch information
Herz3h committed Oct 1, 2020
1 parent 940a466 commit e6bb45c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 16 deletions.
6 changes: 3 additions & 3 deletions src/PHPSQLParser/builders/FromBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <andre.rothe@phosco.info>
* @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;
Expand All @@ -48,7 +48,7 @@
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*
*/
class FromBuilder implements Builder {

Expand Down
16 changes: 11 additions & 5 deletions src/PHPSQLParser/builders/RefClauseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <andre.rothe@phosco.info>
* @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 <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*
*/
class RefClauseBuilder implements Builder {

Expand Down Expand Up @@ -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 '';
Expand All @@ -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');
Expand Down
8 changes: 4 additions & 4 deletions src/PHPSQLParser/builders/TableBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <andre.rothe@phosco.info>
* @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 <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*
*/
class TableBuilder implements Builder {

Expand Down
2 changes: 1 addition & 1 deletion src/PHPSQLParser/builders/WhereExpressionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
16 changes: 13 additions & 3 deletions tests/cases/creator/leftTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <andre.rothe@phosco.info>
* @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)
Expand All @@ -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;
}
}

0 comments on commit e6bb45c

Please sign in to comment.