Skip to content

Commit e28d54d

Browse files
authored
Merge pull request #239 from maltepet/master
add the ability to parse reserved words in the WHERE clause
2 parents 6fca044 + f9200c0 commit e28d54d

3 files changed

Lines changed: 42 additions & 24 deletions

File tree

src/PHPSQLParser/builders/WhereBracketExpressionBuilder.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,25 @@
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
use PHPSQLParser\utils\ExpressionType;
4545

4646
/**
47-
* This class implements the builder for bracket expressions within the WHERE part.
47+
* This class implements the builder for bracket expressions within the WHERE part.
4848
* You can overwrite all functions to achieve another handling.
4949
*
5050
* @author André Rothe <andre.rothe@phosco.info>
5151
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
52-
*
52+
*
5353
*/
5454
class WhereBracketExpressionBuilder implements Builder {
5555

@@ -88,6 +88,11 @@ protected function buildUserVariable($parsed) {
8888
return $builder->build($parsed);
8989
}
9090

91+
protected function buildReserved($parsed) {
92+
$builder = new ReservedBuilder();
93+
return $builder->build($parsed);
94+
}
95+
9196
public function build(array $parsed) {
9297
if ($parsed['expr_type'] !== ExpressionType::BRACKET_EXPRESSION) {
9398
return "";
@@ -103,7 +108,8 @@ public function build(array $parsed) {
103108
$sql .= $this->buildWhereExpression($v);
104109
$sql .= $this->build($v);
105110
$sql .= $this->buildUserVariable($v);
106-
111+
$sql .= $this->buildReserved($v);
112+
107113
if ($len == strlen($sql)) {
108114
throw new UnableToCreateSQLException('WHERE expression subtree', $k, $v, 'expr_type');
109115
}

src/PHPSQLParser/builders/WhereBuilder.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 builder for the WHERE part.
46+
* This class implements the builder for the WHERE part.
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 WhereBuilder implements Builder {
5454

@@ -97,6 +97,11 @@ protected function buildUserVariable($parsed) {
9797
return $builder->build($parsed);
9898
}
9999

100+
protected function buildReserved($parsed) {
101+
$builder = new ReservedBuilder();
102+
return $builder->build($parsed);
103+
}
104+
100105
public function build(array $parsed) {
101106
$sql = "WHERE ";
102107
foreach ($parsed as $k => $v) {
@@ -111,7 +116,8 @@ public function build(array $parsed) {
111116
$sql .= $this->buildWhereExpression($v);
112117
$sql .= $this->buildWhereBracketExpression($v);
113118
$sql .= $this->buildUserVariable($v);
114-
119+
$sql .= $this->buildReserved($v);
120+
115121
if (strlen($sql) == $len) {
116122
throw new UnableToCreateSQLException('WHERE', $k, $v, 'expr_type');
117123
}

src/PHPSQLParser/builders/WhereExpressionBuilder.php

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,25 @@
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
use PHPSQLParser\utils\ExpressionType;
4545

4646
/**
47-
* This class implements the builder for expressions within the WHERE part.
47+
* This class implements the builder for expressions within the WHERE part.
4848
* You can overwrite all functions to achieve another handling.
4949
*
5050
* @author André Rothe <andre.rothe@phosco.info>
5151
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
52-
*
52+
*
5353
*/
5454
class WhereExpressionBuilder implements Builder {
5555

@@ -62,41 +62,46 @@ protected function buildConstant($parsed) {
6262
$builder = new ConstantBuilder();
6363
return $builder->build($parsed);
6464
}
65-
65+
6666
protected function buildOperator($parsed) {
6767
$builder = new OperatorBuilder();
6868
return $builder->build($parsed);
6969
}
70-
70+
7171
protected function buildFunction($parsed) {
7272
$builder = new FunctionBuilder();
7373
return $builder->build($parsed);
7474
}
75-
75+
7676
protected function buildInList($parsed) {
7777
$builder = new InListBuilder();
7878
return $builder->build($parsed);
7979
}
80-
80+
8181
protected function buildWhereExpression($parsed) {
8282
return $this->build($parsed);
8383
}
84-
84+
8585
protected function buildWhereBracketExpression($parsed) {
8686
$builder = new WhereBracketExpressionBuilder();
8787
return $builder->build($parsed);
8888
}
89-
89+
9090
protected function buildUserVariable($parsed) {
9191
$builder = new UserVariableBuilder();
9292
return $builder->build($parsed);
9393
}
94-
94+
9595
protected function buildSubQuery($parsed) {
9696
$builder = new SubQueryBuilder();
9797
return $builder->build($parsed);
9898
}
99-
99+
100+
protected function buildReserved($parsed) {
101+
$builder = new ReservedBuilder();
102+
return $builder->build($parsed);
103+
}
104+
100105
public function build(array $parsed) {
101106
if ($parsed['expr_type'] !== ExpressionType::EXPRESSION) {
102107
return "";
@@ -113,7 +118,8 @@ public function build(array $parsed) {
113118
$sql .= $this->buildWhereBracketExpression($v);
114119
$sql .= $this->buildUserVariable($v);
115120
$sql .= $this->buildSubQuery($v);
116-
121+
$sql .= $this->buildReserved($v);
122+
117123
if ($len == strlen($sql)) {
118124
throw new UnableToCreateSQLException('WHERE expression subtree', $k, $v, 'expr_type');
119125
}
@@ -124,6 +130,6 @@ public function build(array $parsed) {
124130
$sql = substr($sql, 0, -1);
125131
return $sql;
126132
}
127-
133+
128134
}
129135
?>

0 commit comments

Comments
 (0)