Skip to content

Commit 29bdef6

Browse files
authored
Merge pull request #208 from pixelfederation/master
Optional $options parameter for the SQLParser
2 parents 1c6064d + fc3c9ff commit 29bdef6

26 files changed

+263
-186
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ vendor/
22
composer.lock
33
clover.xml
44
composer.phar
5+
.idea

src/PHPSQLParser/Options.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* @author mfris
4+
*
5+
*/
6+
7+
namespace PHPSQLParser;
8+
9+
/**
10+
*
11+
* @author mfris
12+
* @package PHPSQLParser
13+
*/
14+
final class Options
15+
{
16+
17+
/**
18+
* @var array
19+
*/
20+
private $options;
21+
22+
/**
23+
* @const string
24+
*/
25+
const CONSISTENT_SUB_TREES = 'consistent_sub_trees';
26+
27+
/**
28+
* Options constructor.
29+
*
30+
* @param array $options
31+
*/
32+
public function __construct(array $options)
33+
{
34+
$this->options = $options;
35+
}
36+
37+
/**
38+
* @return bool
39+
*/
40+
public function getConsistentSubtrees()
41+
{
42+
return (isset($this->options[self::CONSISTENT_SUB_TREES]) && $this->options[self::CONSISTENT_SUB_TREES]);
43+
}
44+
}

src/PHPSQLParser/PHPSQLParser.php

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3333
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3434
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35-
*
35+
*
3636
* @author André Rothe <andre.rothe@phosco.info>
3737
* @copyright 2010-2014 Justin Swanhart and André Rothe
3838
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
@@ -56,34 +56,42 @@ class PHPSQLParser {
5656
public $parsed;
5757

5858
/**
59-
* Constructor. It simply calls the parse() function.
59+
* @var Options
60+
*/
61+
private $options;
62+
63+
/**
64+
* Constructor. It simply calls the parse() function.
6065
* Use the public variable $parsed to get the output.
61-
*
62-
* @param String $sql The SQL statement.
63-
* @param boolean $calcPositions True, if the output should contain [position], false otherwise.
66+
*
67+
* @param String|bool $sql The SQL statement.
68+
* @param bool $calcPositions True, if the output should contain [position], false otherwise.
69+
* @param array $options
6470
*/
65-
public function __construct($sql = false, $calcPositions = false) {
71+
public function __construct($sql = false, $calcPositions = false, array $options = []) {
72+
$this->options = new Options($options);
73+
6674
if ($sql) {
6775
$this->parse($sql, $calcPositions);
6876
}
6977
}
7078

7179
/**
72-
* It parses the given SQL statement and generates a detailled
73-
* output array for every part of the statement. The method can
74-
* also generate [position] fields within the output, which hold
75-
* the character position for every statement part. The calculation
80+
* It parses the given SQL statement and generates a detailled
81+
* output array for every part of the statement. The method can
82+
* also generate [position] fields within the output, which hold
83+
* the character position for every statement part. The calculation
7684
* of the positions needs some time, if you don't need positions in
7785
* your application, set the parameter to false.
78-
*
86+
*
7987
* @param String $sql The SQL statement.
8088
* @param boolean $calcPositions True, if the output should contain [position], false otherwise.
81-
*
89+
*
8290
* @return array An associative array with all meta information about the SQL statement.
8391
*/
8492
public function parse($sql, $calcPositions = false) {
8593

86-
$processor = new DefaultProcessor();
94+
$processor = new DefaultProcessor($this->options);
8795
$queries = $processor->process($sql);
8896

8997
// calc the positions of some important tokens
@@ -99,9 +107,9 @@ public function parse($sql, $calcPositions = false) {
99107

100108
/**
101109
* Add a custom function to the parser. no return value
102-
*
110+
*
103111
* @param String $token The name of the function to add
104-
*
112+
*
105113
* @return null
106114
*/
107115
public function addCustomFunction($token) {
@@ -110,9 +118,9 @@ public function addCustomFunction($token) {
110118

111119
/**
112120
* Remove a custom function from the parser. no return value
113-
*
121+
*
114122
* @param String $token The name of the function to remove
115-
*
123+
*
116124
* @return null
117125
*/
118126
public function removeCustomFunction($token) {
@@ -121,8 +129,8 @@ public function removeCustomFunction($token) {
121129

122130
/**
123131
* Returns the list of custom functions
124-
*
125-
* @return array Returns an array of all custom functions
132+
*
133+
* @return array Returns an array of all custom functions
126134
*/
127135
public function getCustomFunctions() {
128136
return PHPSQLParserConstants::getInstance()->getCustomFunctions();

src/PHPSQLParser/processors/AbstractProcessor.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,33 @@
4242
namespace PHPSQLParser\processors;
4343

4444
use PHPSQLParser\lexer\PHPSQLLexer;
45+
use PHPSQLParser\Options;
4546
use PHPSQLParser\utils\ExpressionType;
4647

4748
/**
4849
* This class contains some general functions for a processor.
49-
*
50+
*
5051
* @author André Rothe <andre.rothe@phosco.info>
5152
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
52-
*
53+
*
5354
*/
5455
abstract class AbstractProcessor {
5556

57+
/**
58+
* @var Options
59+
*/
60+
protected $options;
61+
62+
/**
63+
* AbstractProcessor constructor.
64+
*
65+
* @param Options $options
66+
*/
67+
public function __construct(Options $options)
68+
{
69+
$this->options = $options;
70+
}
71+
5672
/**
5773
* This function implements the main functionality of a processor class.
5874
* Always use default valuses for additional parameters within overridden functions.
@@ -78,9 +94,9 @@ public function splitSQLIntoTokens($sql) {
7894
* `a.b`
7995
* a.`b`
8096
* `a`.b
81-
* It is also possible to have escaped quoting characters
97+
* It is also possible to have escaped quoting characters
8298
* within an expression part:
83-
* `a``b` => a`b
99+
* `a``b` => a`b
84100
* And you can use whitespace between the parts:
85101
* a . `b` => [a,b]
86102
*/
@@ -278,11 +294,11 @@ protected function isBracketExpression($out) {
278294
protected function isSubQuery($out) {
279295
return (isset($out['expr_type']) && $out['expr_type'] === ExpressionType::SUBQUERY);
280296
}
281-
297+
282298
protected function isComment($out) {
283299
return (isset($out['expr_type']) && $out['expr_type'] === ExpressionType::COMMENT);
284300
}
285-
301+
286302
public function processComment($expression) {
287303
$result = array();
288304
$result['expr_type'] = ExpressionType::COMMENT;

src/PHPSQLParser/processors/BracketProcessor.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* BracketProcessor.php
44
*
55
* This file implements the processor for the parentheses around the statements.
6-
*
6+
*
77
* PHP version 5
88
*
99
* LICENSE:
@@ -44,15 +44,15 @@
4444

4545
/**
4646
* This class processes the parentheses around the statement.
47-
*
47+
*
4848
* @author André Rothe <andre.rothe@phosco.info>
4949
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
50-
*
50+
*
5151
*/
5252
class BracketProcessor extends AbstractProcessor {
5353

5454
protected function processTopLevel($sql) {
55-
$processor = new DefaultProcessor();
55+
$processor = new DefaultProcessor($this->options);
5656
return $processor->process($sql);
5757
}
5858

src/PHPSQLParser/processors/ColumnDefinitionProcessor.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@
4343
class ColumnDefinitionProcessor extends AbstractProcessor {
4444

4545
protected function processExpressionList($parsed) {
46-
$processor = new ExpressionListProcessor();
46+
$processor = new ExpressionListProcessor($this->options);
4747
$expr = $this->removeParenthesisFromStart($parsed);
4848
$expr = $this->splitSQLIntoTokens($expr);
4949
$expr = $this->removeComma($expr);
5050
return $processor->process($expr);
5151
}
5252

5353
protected function processReferenceDefinition($parsed) {
54-
$processor = new ReferenceDefinitionProcessor();
54+
$processor = new ReferenceDefinitionProcessor($this->options);
5555
return $processor->process($parsed);
5656
}
5757

@@ -394,8 +394,14 @@ public function process($tokens) {
394394
$parsed = $this->processExpressionList($trim);
395395

396396
$last = array_pop($expr);
397-
$last['sub_tree'] = array('expr_type' => ExpressionType::BRACKET_EXPRESSION, 'base_expr' => $trim,
398-
'sub_tree' => $parsed);
397+
$subTree = array('expr_type' => ExpressionType::BRACKET_EXPRESSION, 'base_expr' => $trim,
398+
'sub_tree' => $parsed);
399+
400+
if ($this->options->getConsistentSubtrees()) {
401+
$subTree = array($subTree);
402+
}
403+
404+
$last['sub_tree'] = $subTree;
399405
$expr[] = $last;
400406
$currCategory = $prevCategory;
401407
break;

src/PHPSQLParser/processors/CreateDefinitionProcessor.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,27 +47,27 @@
4747
*
4848
* @author André Rothe <andre.rothe@phosco.info>
4949
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
50-
*
50+
*
5151
*/
5252
class CreateDefinitionProcessor extends AbstractProcessor {
5353

5454
protected function processExpressionList($parsed) {
55-
$processor = new ExpressionListProcessor();
55+
$processor = new ExpressionListProcessor($this->options);
5656
return $processor->process($parsed);
5757
}
5858

5959
protected function processIndexColumnList($parsed) {
60-
$processor = new IndexColumnListProcessor();
60+
$processor = new IndexColumnListProcessor($this->options);
6161
return $processor->process($parsed);
6262
}
6363

6464
protected function processColumnDefinition($parsed) {
65-
$processor = new ColumnDefinitionProcessor();
65+
$processor = new ColumnDefinitionProcessor($this->options);
6666
return $processor->process($parsed);
6767
}
6868

6969
protected function processReferenceDefinition($parsed) {
70-
$processor = new ReferenceDefinitionProcessor();
70+
$processor = new ReferenceDefinitionProcessor($this->options);
7171
return $processor->process($parsed);
7272
}
7373

src/PHPSQLParser/processors/DefaultProcessor.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ protected function isUnion($tokens) {
5757
protected function processUnion($tokens) {
5858
// this is the highest level lexical analysis. This is the part of the
5959
// code which finds UNION and UNION ALL query parts
60-
$processor = new UnionProcessor();
60+
$processor = new UnionProcessor($this->options);
6161
return $processor->process($tokens);
6262
}
6363

6464
protected function processSQL($tokens) {
65-
$processor = new SQLProcessor();
65+
$processor = new SQLProcessor($this->options);
6666
return $processor->process($tokens);
6767
}
6868

0 commit comments

Comments
 (0)