parse() public static method

public static parse ( Parser $parser, TokensList $list, array $options = [] ) : Condition[]
$parser SqlParser\Parser The parser that serves as context.
$list SqlParser\TokensList The list of tokens that are being parsed.
$options array Parameters for parsing.
return Condition[]
Esempio n. 1
0
 public function testParseBetween()
 {
     $component = Condition::parse(new Parser(), $this->getTokensList('(id BETWEEN 10 AND 20) OR (id BETWEEN 30 AND 40)'));
     $this->assertEquals($component[0]->expr, '(id BETWEEN 10 AND 20)');
     $this->assertEquals($component[1]->expr, 'OR');
     $this->assertEquals($component[2]->expr, '(id BETWEEN 30 AND 40)');
 }
Esempio n. 2
0
 /**
  * @param Parser     $parser The instance that requests parsing.
  * @param TokensList $list   The list of tokens to be parsed.
  *
  * @return void
  */
 public function parse(Parser $parser, TokensList $list)
 {
     ++$list->idx;
     // Skipping `DELETE`.
     // parse any options if provided
     $this->options = OptionsArray::parse($parser, $list, static::$OPTIONS);
     ++$list->idx;
     /**
      * The state of the parser.
      *
      * Below are the states of the parser.
      *
      *      0 ---------------------------------[ FROM ]----------------------------------> 2
      *      0 ------------------------------[ table[.*] ]--------------------------------> 1
      *      1 ---------------------------------[ FROM ]----------------------------------> 2
      *      2 --------------------------------[ USING ]----------------------------------> 3
      *      2 --------------------------------[ WHERE ]----------------------------------> 4
      *      2 --------------------------------[ ORDER ]----------------------------------> 5
      *      2 --------------------------------[ LIMIT ]----------------------------------> 6
      *
      * @var int $state
      */
     $state = 0;
     /**
      * If the query is multi-table or not
      *
      * @var bool $multiTable
      */
     $multiTable = false;
     for (; $list->idx < $list->count; ++$list->idx) {
         /**
          * Token parsed at this moment.
          *
          * @var Token $token
          */
         $token = $list->tokens[$list->idx];
         // End of statement.
         if ($token->type === Token::TYPE_DELIMITER) {
             break;
         }
         if ($state === 0) {
             if ($token->type === Token::TYPE_KEYWORD && $token->value !== 'FROM') {
                 $parser->error(__('Unexpected keyword.'), $token);
                 break;
             } elseif ($token->type === Token::TYPE_KEYWORD && $token->value === 'FROM') {
                 ++$list->idx;
                 // Skip 'FROM'
                 $this->from = ExpressionArray::parse($parser, $list);
                 $state = 2;
             } else {
                 $this->columns = ExpressionArray::parse($parser, $list);
                 $state = 1;
             }
         } elseif ($state === 1) {
             if ($token->type === Token::TYPE_KEYWORD && $token->value !== 'FROM') {
                 $parser->error(__('Unexpected keyword.'), $token);
                 break;
             } elseif ($token->type === Token::TYPE_KEYWORD && $token->value === 'FROM') {
                 ++$list->idx;
                 // Skip 'FROM'
                 $this->from = ExpressionArray::parse($parser, $list);
                 $state = 2;
             } else {
                 $parser->error(__('Unexpected token.'), $token);
                 break;
             }
         } elseif ($state === 2) {
             if ($token->type === Token::TYPE_KEYWORD && $token->value === 'USING') {
                 ++$list->idx;
                 // Skip 'USING'
                 $this->using = ExpressionArray::parse($parser, $list);
                 $state = 3;
                 $multiTable = true;
             } elseif ($token->type === Token::TYPE_KEYWORD && $token->value === 'WHERE') {
                 ++$list->idx;
                 // Skip 'WHERE'
                 $this->where = Condition::parse($parser, $list);
                 $state = 4;
             } elseif ($token->type === Token::TYPE_KEYWORD && $token->value === 'ORDER BY') {
                 ++$list->idx;
                 // Skip 'ORDER BY'
                 $this->order = OrderKeyword::parse($parser, $list);
                 $state = 5;
             } elseif ($token->type === Token::TYPE_KEYWORD && $token->value === 'LIMIT') {
                 ++$list->idx;
                 // Skip 'LIMIT'
                 $this->limit = Limit::parse($parser, $list);
                 $state = 6;
             } elseif ($token->type === Token::TYPE_KEYWORD) {
                 $parser->error(__('Unexpected keyword.'), $token);
                 break;
             }
         } elseif ($state === 3) {
             if ($token->type === Token::TYPE_KEYWORD && $token->value === 'WHERE') {
                 ++$list->idx;
                 // Skip 'WHERE'
                 $this->where = Condition::parse($parser, $list);
                 $state = 4;
             } elseif ($token->type === Token::TYPE_KEYWORD) {
                 $parser->error(__('Unexpected keyword.'), $token);
                 break;
             } else {
                 $parser->error(__('Unexpected token.'), $token);
                 break;
             }
         } elseif ($state === 4) {
             if ($multiTable === true && $token->type === Token::TYPE_KEYWORD) {
                 $parser->error(__('This type of clause is not valid in Multi-table queries.'), $token);
                 break;
             }
             if ($token->type === Token::TYPE_KEYWORD && $token->value === 'ORDER BY') {
                 ++$list->idx;
                 // Skip 'ORDER  BY'
                 $this->order = OrderKeyword::parse($parser, $list);
                 $state = 5;
             } elseif ($token->type === Token::TYPE_KEYWORD && $token->value === 'LIMIT') {
                 ++$list->idx;
                 // Skip 'LIMIT'
                 $this->limit = Limit::parse($parser, $list);
                 $state = 6;
             } elseif ($token->type === Token::TYPE_KEYWORD) {
                 $parser->error(__('Unexpected keyword.'), $token);
                 break;
             }
         } elseif ($state === 5) {
             if ($token->type === Token::TYPE_KEYWORD && $token->value === 'LIMIT') {
                 ++$list->idx;
                 // Skip 'LIMIT'
                 $this->limit = Limit::parse($parser, $list);
                 $state = 6;
             } elseif ($token->type === Token::TYPE_KEYWORD) {
                 $parser->error(__('Unexpected keyword.'), $token);
                 break;
             }
         }
     }
     if ($state >= 2) {
         foreach ($this->from as $from_expr) {
             $from_expr->database = $from_expr->table;
             $from_expr->table = $from_expr->column;
             $from_expr->column = null;
         }
     }
     --$list->idx;
 }
Esempio n. 3
0
 /**
  * @param Parser     $parser  The parser that serves as context.
  * @param TokensList $list    The list of tokens that are being parsed.
  * @param array      $options Parameters for parsing.
  *
  * @return JoinKeyword[]
  */
 public static function parse(Parser $parser, TokensList $list, array $options = array())
 {
     $ret = array();
     $expr = new JoinKeyword();
     /**
      * The state of the parser.
      *
      * Below are the states of the parser.
      *
      *      0 -----------------------[ JOIN ]----------------------> 1
      *
      *      1 -----------------------[ expr ]----------------------> 2
      *
      *      2 ------------------------[ ON ]-----------------------> 3
      *      2 -----------------------[ USING ]---------------------> 4
      *
      *      3 --------------------[ conditions ]-------------------> 0
      *
      *      4 ----------------------[ columns ]--------------------> 0
      *
      * @var int $state
      */
     $state = 0;
     // By design, the parser will parse first token after the keyword.
     // In this case, the keyword must be analyzed too, in order to determine
     // the type of this join.
     if ($list->idx > 0) {
         --$list->idx;
     }
     for (; $list->idx < $list->count; ++$list->idx) {
         /**
          * Token parsed at this moment.
          *
          * @var Token $token
          */
         $token = $list->tokens[$list->idx];
         // End of statement.
         if ($token->type === Token::TYPE_DELIMITER) {
             break;
         }
         // Skipping whitespaces and comments.
         if ($token->type === Token::TYPE_WHITESPACE || $token->type === Token::TYPE_COMMENT) {
             continue;
         }
         if ($state === 0) {
             if ($token->type === Token::TYPE_KEYWORD && !empty(static::$JOINS[$token->value])) {
                 $expr->type = static::$JOINS[$token->value];
                 $state = 1;
             } else {
                 break;
             }
         } elseif ($state === 1) {
             $expr->expr = Expression::parse($parser, $list, array('field' => 'table'));
             $state = 2;
         } elseif ($state === 2) {
             if ($token->type === Token::TYPE_KEYWORD) {
                 if ($token->value === 'ON') {
                     $state = 3;
                 } elseif ($token->value === 'USING') {
                     $state = 4;
                 }
             }
         } elseif ($state === 3) {
             $expr->on = Condition::parse($parser, $list);
             $ret[] = $expr;
             $expr = new JoinKeyword();
             $state = 0;
         } elseif ($state === 4) {
             $expr->using = ArrayObj::parse($parser, $list);
             $ret[] = $expr;
             $expr = new JoinKeyword();
             $state = 0;
         }
     }
     if (!empty($expr->type)) {
         $ret[] = $expr;
     }
     --$list->idx;
     return $ret;
 }
Esempio n. 4
0
 /**
  *
  * @param Parser     $parser  The parser that serves as context.
  * @param TokensList $list    The list of tokens that are being parsed.
  *
  * @return Expression
  */
 public static function parse(Parser $parser, TokensList $list, array $options = array())
 {
     $ret = new CaseExpression();
     /**
      * State of parser
      *
      * @var int $parser
      */
     $state = 0;
     /**
      * Syntax type (type 0 or type 1)
      *
      * @var int $type
      */
     $type = 0;
     ++$list->idx;
     // Skip 'CASE'
     for (; $list->idx < $list->count; ++$list->idx) {
         /**
          * Token parsed at this moment.
          *
          * @var Token $token
          */
         $token = $list->tokens[$list->idx];
         // Skipping whitespaces and comments.
         if ($token->type === Token::TYPE_WHITESPACE || $token->type === Token::TYPE_COMMENT) {
             continue;
         }
         if ($state === 0) {
             if ($token->type === Token::TYPE_KEYWORD && $token->value === 'WHEN') {
                 ++$list->idx;
                 // Skip 'WHEN'
                 $new_condition = Condition::parse($parser, $list);
                 $type = 1;
                 $state = 1;
                 $ret->conditions[] = $new_condition;
             } elseif ($token->type === Token::TYPE_KEYWORD && $token->value === 'ELSE') {
                 ++$list->idx;
                 // Skip 'ELSE'
                 $ret->else_result = Expression::parse($parser, $list);
                 $state = 0;
                 // last clause of CASE expression
             } elseif ($token->type === Token::TYPE_KEYWORD && ($token->value === 'END' || $token->value === 'end')) {
                 $state = 3;
                 // end of CASE expression
                 ++$list->idx;
                 break;
             } elseif ($token->type === Token::TYPE_KEYWORD) {
                 $parser->error(__('Unexpected keyword.'), $token);
                 break;
             } else {
                 $ret->value = Expression::parse($parser, $list);
                 $type = 0;
                 $state = 1;
             }
         } elseif ($state === 1) {
             if ($type === 0) {
                 if ($token->type === Token::TYPE_KEYWORD && $token->value === 'WHEN') {
                     ++$list->idx;
                     // Skip 'WHEN'
                     $new_value = Expression::parse($parser, $list);
                     $state = 2;
                     $ret->compare_values[] = $new_value;
                 } elseif ($token->type === Token::TYPE_KEYWORD && $token->value === 'ELSE') {
                     ++$list->idx;
                     // Skip 'ELSE'
                     $ret->else_result = Expression::parse($parser, $list);
                     $state = 0;
                     // last clause of CASE expression
                 } elseif ($token->type === Token::TYPE_KEYWORD && ($token->value === 'END' || $token->value === 'end')) {
                     $state = 3;
                     // end of CASE expression
                     ++$list->idx;
                     break;
                 } elseif ($token->type === Token::TYPE_KEYWORD) {
                     $parser->error(__('Unexpected keyword.'), $token);
                     break;
                 }
             } else {
                 if ($token->type === Token::TYPE_KEYWORD && $token->value === 'THEN') {
                     ++$list->idx;
                     // Skip 'THEN'
                     $new_result = Expression::parse($parser, $list);
                     $state = 0;
                     $ret->results[] = $new_result;
                 } elseif ($token->type === Token::TYPE_KEYWORD) {
                     $parser->error(__('Unexpected keyword.'), $token);
                     break;
                 }
             }
         } elseif ($state === 2) {
             if ($type === 0) {
                 if ($token->type === Token::TYPE_KEYWORD && $token->value === 'THEN') {
                     ++$list->idx;
                     // Skip 'THEN'
                     $new_result = Expression::parse($parser, $list);
                     $ret->results[] = $new_result;
                     $state = 1;
                 } elseif ($token->type === Token::TYPE_KEYWORD) {
                     $parser->error(__('Unexpected keyword.'), $token);
                     break;
                 }
             }
         }
     }
     if ($state !== 3) {
         $parser->error(__('Unexpected end of CASE expression'), $list->tokens[$list->idx - 1]);
     }
     --$list->idx;
     return $ret;
 }