Skip to content

Commit

Permalink
Merge pull request #208 from pixelfederation/master
Browse files Browse the repository at this point in the history
Optional $options parameter for the SQLParser
  • Loading branch information
greenlion committed Jul 21, 2016
2 parents 1c6064d + fc3c9ff commit 29bdef6
Show file tree
Hide file tree
Showing 26 changed files with 263 additions and 186 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,3 +2,4 @@ vendor/
composer.lock
clover.xml
composer.phar
.idea
44 changes: 44 additions & 0 deletions src/PHPSQLParser/Options.php
@@ -0,0 +1,44 @@
<?php
/**
* @author mfris
*
*/

namespace PHPSQLParser;

/**
*
* @author mfris
* @package PHPSQLParser
*/
final class Options
{

/**
* @var array
*/
private $options;

/**
* @const string
*/
const CONSISTENT_SUB_TREES = 'consistent_sub_trees';

/**
* Options constructor.
*
* @param array $options
*/
public function __construct(array $options)
{
$this->options = $options;
}

/**
* @return bool
*/
public function getConsistentSubtrees()
{
return (isset($this->options[self::CONSISTENT_SUB_TREES]) && $this->options[self::CONSISTENT_SUB_TREES]);
}
}
46 changes: 27 additions & 19 deletions src/PHPSQLParser/PHPSQLParser.php
Expand Up @@ -32,7 +32,7 @@
* 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)
Expand All @@ -56,34 +56,42 @@ class PHPSQLParser {
public $parsed;

/**
* Constructor. It simply calls the parse() function.
* @var Options
*/
private $options;

/**
* Constructor. It simply calls the parse() function.
* Use the public variable $parsed to get the output.
*
* @param String $sql The SQL statement.
* @param boolean $calcPositions True, if the output should contain [position], false otherwise.
*
* @param String|bool $sql The SQL statement.
* @param bool $calcPositions True, if the output should contain [position], false otherwise.
* @param array $options
*/
public function __construct($sql = false, $calcPositions = false) {
public function __construct($sql = false, $calcPositions = false, array $options = []) {
$this->options = new Options($options);

if ($sql) {
$this->parse($sql, $calcPositions);
}
}

/**
* It parses the given SQL statement and generates a detailled
* output array for every part of the statement. The method can
* also generate [position] fields within the output, which hold
* the character position for every statement part. The calculation
* It parses the given SQL statement and generates a detailled
* output array for every part of the statement. The method can
* also generate [position] fields within the output, which hold
* the character position for every statement part. The calculation
* of the positions needs some time, if you don't need positions in
* your application, set the parameter to false.
*
*
* @param String $sql The SQL statement.
* @param boolean $calcPositions True, if the output should contain [position], false otherwise.
*
*
* @return array An associative array with all meta information about the SQL statement.
*/
public function parse($sql, $calcPositions = false) {

$processor = new DefaultProcessor();
$processor = new DefaultProcessor($this->options);
$queries = $processor->process($sql);

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

/**
* Add a custom function to the parser. no return value
*
*
* @param String $token The name of the function to add
*
*
* @return null
*/
public function addCustomFunction($token) {
Expand All @@ -110,9 +118,9 @@ public function addCustomFunction($token) {

/**
* Remove a custom function from the parser. no return value
*
*
* @param String $token The name of the function to remove
*
*
* @return null
*/
public function removeCustomFunction($token) {
Expand All @@ -121,8 +129,8 @@ public function removeCustomFunction($token) {

/**
* Returns the list of custom functions
*
* @return array Returns an array of all custom functions
*
* @return array Returns an array of all custom functions
*/
public function getCustomFunctions() {
return PHPSQLParserConstants::getInstance()->getCustomFunctions();
Expand Down
28 changes: 22 additions & 6 deletions src/PHPSQLParser/processors/AbstractProcessor.php
Expand Up @@ -42,17 +42,33 @@
namespace PHPSQLParser\processors;

use PHPSQLParser\lexer\PHPSQLLexer;
use PHPSQLParser\Options;
use PHPSQLParser\utils\ExpressionType;

/**
* This class contains some general functions for a processor.
*
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*
*/
abstract class AbstractProcessor {

/**
* @var Options
*/
protected $options;

/**
* AbstractProcessor constructor.
*
* @param Options $options
*/
public function __construct(Options $options)
{
$this->options = $options;
}

/**
* This function implements the main functionality of a processor class.
* Always use default valuses for additional parameters within overridden functions.
Expand All @@ -78,9 +94,9 @@ public function splitSQLIntoTokens($sql) {
* `a.b`
* a.`b`
* `a`.b
* It is also possible to have escaped quoting characters
* It is also possible to have escaped quoting characters
* within an expression part:
* `a``b` => a`b
* `a``b` => a`b
* And you can use whitespace between the parts:
* a . `b` => [a,b]
*/
Expand Down Expand Up @@ -278,11 +294,11 @@ protected function isBracketExpression($out) {
protected function isSubQuery($out) {
return (isset($out['expr_type']) && $out['expr_type'] === ExpressionType::SUBQUERY);
}

protected function isComment($out) {
return (isset($out['expr_type']) && $out['expr_type'] === ExpressionType::COMMENT);
}

public function processComment($expression) {
$result = array();
$result['expr_type'] = ExpressionType::COMMENT;
Expand Down
8 changes: 4 additions & 4 deletions src/PHPSQLParser/processors/BracketProcessor.php
Expand Up @@ -3,7 +3,7 @@
* BracketProcessor.php
*
* This file implements the processor for the parentheses around the statements.
*
*
* PHP version 5
*
* LICENSE:
Expand Down Expand Up @@ -44,15 +44,15 @@

/**
* This class processes the parentheses around the statement.
*
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*
*/
class BracketProcessor extends AbstractProcessor {

protected function processTopLevel($sql) {
$processor = new DefaultProcessor();
$processor = new DefaultProcessor($this->options);
return $processor->process($sql);
}

Expand Down
14 changes: 10 additions & 4 deletions src/PHPSQLParser/processors/ColumnDefinitionProcessor.php
Expand Up @@ -43,15 +43,15 @@
class ColumnDefinitionProcessor extends AbstractProcessor {

protected function processExpressionList($parsed) {
$processor = new ExpressionListProcessor();
$processor = new ExpressionListProcessor($this->options);
$expr = $this->removeParenthesisFromStart($parsed);
$expr = $this->splitSQLIntoTokens($expr);
$expr = $this->removeComma($expr);
return $processor->process($expr);
}

protected function processReferenceDefinition($parsed) {
$processor = new ReferenceDefinitionProcessor();
$processor = new ReferenceDefinitionProcessor($this->options);
return $processor->process($parsed);
}

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

$last = array_pop($expr);
$last['sub_tree'] = array('expr_type' => ExpressionType::BRACKET_EXPRESSION, 'base_expr' => $trim,
'sub_tree' => $parsed);
$subTree = array('expr_type' => ExpressionType::BRACKET_EXPRESSION, 'base_expr' => $trim,
'sub_tree' => $parsed);

if ($this->options->getConsistentSubtrees()) {
$subTree = array($subTree);
}

$last['sub_tree'] = $subTree;
$expr[] = $last;
$currCategory = $prevCategory;
break;
Expand Down
10 changes: 5 additions & 5 deletions src/PHPSQLParser/processors/CreateDefinitionProcessor.php
Expand Up @@ -47,27 +47,27 @@
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*
*/
class CreateDefinitionProcessor extends AbstractProcessor {

protected function processExpressionList($parsed) {
$processor = new ExpressionListProcessor();
$processor = new ExpressionListProcessor($this->options);
return $processor->process($parsed);
}

protected function processIndexColumnList($parsed) {
$processor = new IndexColumnListProcessor();
$processor = new IndexColumnListProcessor($this->options);
return $processor->process($parsed);
}

protected function processColumnDefinition($parsed) {
$processor = new ColumnDefinitionProcessor();
$processor = new ColumnDefinitionProcessor($this->options);
return $processor->process($parsed);
}

protected function processReferenceDefinition($parsed) {
$processor = new ReferenceDefinitionProcessor();
$processor = new ReferenceDefinitionProcessor($this->options);
return $processor->process($parsed);
}

Expand Down
4 changes: 2 additions & 2 deletions src/PHPSQLParser/processors/DefaultProcessor.php
Expand Up @@ -57,12 +57,12 @@ protected function isUnion($tokens) {
protected function processUnion($tokens) {
// this is the highest level lexical analysis. This is the part of the
// code which finds UNION and UNION ALL query parts
$processor = new UnionProcessor();
$processor = new UnionProcessor($this->options);
return $processor->process($tokens);
}

protected function processSQL($tokens) {
$processor = new SQLProcessor();
$processor = new SQLProcessor($this->options);
return $processor->process($tokens);
}

Expand Down

0 comments on commit 29bdef6

Please sign in to comment.