Skip to content

Commit

Permalink
Replaced PMA_RTN_parseOneParameter().
Browse files Browse the repository at this point in the history
Updated sql-parser library.

Signed-off-by: Dan Ungureanu <udan1107@gmail.com>
  • Loading branch information
Dan Ungureanu committed Jul 10, 2015
1 parent 1499963 commit 02f6c7f
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 13 deletions.
2 changes: 1 addition & 1 deletion libraries/rte/rte_routines.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ function PMA_RTN_getDataFromName($name, $type, $all = true)
}
$routine['DTD_IDENTIFIER'] = $dtd;
}
$returnparam = PMA_RTN_parseOneParameter($routine['DTD_IDENTIFIER']);
$returnparam = SqlParser\Utils\Routine::getReturnType($routine['DTD_IDENTIFIER']);
$retval['item_returntype'] = $returnparam[2];
$retval['item_returnlength'] = $returnparam[3];
$retval['item_returnopts_num'] = $returnparam[4];
Expand Down
4 changes: 3 additions & 1 deletion libraries/sql-parser/src/Fragments/DataTypeFragment.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ public static function parse(Parser $parser, TokensList $list, array $options =
} elseif ($state === 1) {
if (($token->type === Token::TYPE_OPERATOR) && ($token->value === '(')) {
$size = ArrayFragment::parse($parser, $list);
$ret->size = $size->array;
foreach ($size->tokens as $token) {
$ret->size[] = $token->token;
}
$ret->tokens = array_merge($ret->tokens, $size->tokens);
++$list->idx;
}
Expand Down
2 changes: 1 addition & 1 deletion libraries/sql-parser/src/Fragments/ParamDefFragment.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
if (($token->value === 'IN') || ($token->value === 'OUT') || ($token->value === 'INOUT')) {
$expr->inOut = $token->value;
++$list->idx;
} else if ($token->value === ')') {
} elseif ($token->value === ')') {
++$list->idx;
break;
} else {
Expand Down
6 changes: 3 additions & 3 deletions libraries/sql-parser/src/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public function lex()
// Another example is `parseComment`.

$tokens = new TokensList();
$lastToken = NULL;
$lastToken = null;

for ($this->last = 0, $lastIdx = 0; $this->last < $this->len; $lastIdx = ++$this->last) {
/** @var Token The new token. */
Expand All @@ -153,8 +153,8 @@ public function lex()
if ($this->delimiter !== $this->str[$this->last]) {
$this->error('Unexpected character.', $this->str[$this->last], $this->last);
}
} else if (($token->type === Token::TYPE_SYMBOL) && ($token->flags & Token::FLAG_SYMBOL_VARIABLE) &&
($lastToken !== NULL)) {
} elseif (($token->type === Token::TYPE_SYMBOL) && ($token->flags & Token::FLAG_SYMBOL_VARIABLE) &&
($lastToken !== null)) {
// Handles ```... FROM 'user'@'%' ...```.
if ((($lastToken->type === Token::TYPE_SYMBOL) && ($lastToken->flags & Token::FLAG_SYMBOL_BACKTICK)) ||
($lastToken->type === Token::TYPE_STRING)) {
Expand Down
8 changes: 6 additions & 2 deletions libraries/sql-parser/src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,20 @@ class Parser
* @param mixed $list
* @param bool $strict
*/
public function __construct($list, $strict = false)
public function __construct($list = null, $strict = false)
{
if ((is_string($list)) || ($list instanceof UtfString)) {
$lexer = new Lexer($list, $strict);
$this->list = $lexer->tokens;
} elseif ($list instanceof TokensList) {
$this->list = $list;
}

$this->strict = $strict;
$this->parse();

if ($list !== null) {
$this->parse();
}
}

/**
Expand Down
6 changes: 5 additions & 1 deletion libraries/sql-parser/src/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,17 @@ public function extract()
}
return $ret;
case Token::TYPE_STRING:
return mb_substr($this->token, 1, -1); // trims quotes
$quote = $this->token[0];
$str = str_replace($quote . $quote, $quote, $this->token);
return mb_substr($str, 1, -1); // trims quotes
case Token::TYPE_SYMBOL:
$str = $this->token;
if ((isset($str[0])) && ($str[0] === '@')) {
$str = mb_substr($str, 1);
}
if ((isset($str[0])) && (($str[0] === '`') || ($str[0] === '"') || ($str[0] === '\''))) {
$quote = $str[0];
$str = str_replace($quote . $quote, $quote, $str);
$str = mb_substr($str, 1, -1);
}
return $str;
Expand Down
3 changes: 1 addition & 2 deletions libraries/sql-parser/src/Utils/Misc.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static function getAliases($tree, $db)
}

foreach ($tree->expr as $expr) {
if ((empty($expr->column)) || (empty($expr->alias)) ){
if ((empty($expr->column)) || (empty($expr->alias))) {
continue;
}

Expand All @@ -87,5 +87,4 @@ public static function getAliases($tree, $db)

return $retval;
}

}
75 changes: 73 additions & 2 deletions libraries/sql-parser/src/Utils/Routine.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,89 @@

namespace SqlParser\Utils;

use SqlParser\Lexer;
use SqlParser\Parser;
use SqlParser\Statement;
use SqlParser\Fragments\DataTypeFragment;
use SqlParser\Fragments\ParamDefFragment;

class Routine
{

/**
* Parses a parameter of a routine.
*
* @param string $param Parameter's definition.
*
* @return array
*/
public function getReturnType($param)
{
$lexer = new Lexer($param);

// A dummy parser is used for error reporting.
$param = DataTypeFragment::parse(new Parser(), $lexer->tokens);

if ($param === null) {
return array('', '', '', '', '');
}

$options = array();
foreach ($param->options->options as $opt) {
$options[] = is_string($opt) ? $opt : $opt['value'];
}

return array(
'',
'',
$param->name,
implode(',', $param->size),
implode(' ', $options)
);
}

/**
* Parses a parameter of a routine.
*
* @param string $param Parameter's definition.
*
* @return array
*/
public function getParameter($param)
{
$lexer = new Lexer('(' . $param . ')');

// A dummy parser is used for error reporting.
$param = ParamDefFragment::parse(new Parser(), $lexer->tokens);

if (empty($param[0])) {
return array('', '', '', '', '');
}

$param = $param[0];

$options = array();
foreach ($param->type->options->options as $opt) {
$options[] = is_string($opt) ? $opt : $opt['value'];
}

return array(
empty($param->inOut) ? '' : $param->inOut,
$param->name,
$param->type->name,
implode(',', $param->type->size),
implode(' ', $options)
);
}

/**
* Gets the parameters of a routine from the parse tree.
*
* @param Statement $tree
*
* @return array
*/
public static function getParameters($tree)
public static function getParameters(Statement $tree)
{
$retval = array(
'num' => 0,
Expand Down Expand Up @@ -42,5 +114,4 @@ public static function getParameters($tree)

return $retval;
}

}

0 comments on commit 02f6c7f

Please sign in to comment.