Пример #1
0
 /**
  * @param array     $tokenData
  * @param           $type
  * @param string    $string
  * @param Tokenizer $tokenizer
  */
 protected static function getReservedString(array &$tokenData, $type, $string, Tokenizer $tokenizer)
 {
     $matches = [];
     $method = self::$regex[$type];
     if (empty($tokenData) && self::isReservedString($string, $matches, $tokenizer->{$method}(), $tokenizer->getRegexBoundaries())) {
         $tokenData = self::getStringTypeArray($type, $string, $matches);
     }
 }
Пример #2
0
 /**
  * @param Tokenizer $tokenizer
  * @param string    $string
  * @param array     $matches
  */
 public static function getNonReservedString(Tokenizer $tokenizer, $string, array &$matches)
 {
     if (!$tokenizer->getNextToken()) {
         $data = [];
         if (1 == \preg_match('/^(.*?)($|\\s|["\'`]|' . $tokenizer->getRegexBoundaries() . ')/', $string, $matches)) {
             $data = [Tokenizer::TOKEN_VALUE => $matches[1], Tokenizer::TOKEN_TYPE => Tokenizer::TOKEN_TYPE_WORD];
         }
         $tokenizer->setNextToken($data);
     }
 }
Пример #3
0
 /**
  * Returns a SQL string in a readable human-friendly format.
  *
  * @param string $sql
  *
  * @return string
  */
 public function format($sql)
 {
     $this->reset();
     $tab = "\t";
     $originalTokens = $this->tokenizer->tokenize((string) $sql);
     $tokens = WhiteSpace::removeTokenWhitespace($originalTokens);
     foreach ($tokens as $i => $token) {
         $queryValue = $token[Tokenizer::TOKEN_VALUE];
         $this->indentation->increaseSpecialIndent()->increaseBlockIndent();
         $addedNewline = $this->newLine->addNewLineBreak($tab);
         if ($this->comment->stringHasCommentToken($token)) {
             $this->formattedSql = $this->comment->writeCommentBlock($token, $tab, $queryValue);
             continue;
         }
         if ($this->parentheses->getInlineParentheses()) {
             if ($this->parentheses->stringIsClosingParentheses($token)) {
                 $this->parentheses->writeInlineParenthesesBlock($tab, $queryValue);
                 continue;
             }
             $this->newLine->writeNewLineForLongCommaInlineValues($token);
             $this->inlineCount += strlen($token[Tokenizer::TOKEN_VALUE]);
         }
         switch ($token) {
             case $this->parentheses->stringIsOpeningParentheses($token):
                 $tokens = $this->formatOpeningParenthesis($token, $i, $tokens, $originalTokens);
                 break;
             case $this->parentheses->stringIsClosingParentheses($token):
                 $this->indentation->decreaseIndentLevelUntilIndentTypeIsSpecial($this);
                 $this->newLine->addNewLineBeforeToken($addedNewline, $tab);
                 break;
             case $this->stringIsEndOfLimitClause($token):
                 $this->clauseLimit = false;
                 break;
             case $token[Tokenizer::TOKEN_VALUE] === ',' && false === $this->parentheses->getInlineParentheses():
                 $this->newLine->writeNewLineBecauseOfComma();
                 break;
             case Token::isTokenTypeReservedTopLevel($token):
                 $queryValue = $this->formatTokenTypeReservedTopLevel($addedNewline, $tab, $token, $queryValue);
                 break;
             case $this->newLine->isTokenTypeReservedNewLine($token):
                 $this->newLine->addNewLineBeforeToken($addedNewline, $tab);
                 if (WhiteSpace::tokenHasExtraWhiteSpaces($token)) {
                     $queryValue = preg_replace('/\\s+/', ' ', $queryValue);
                 }
                 break;
         }
         $this->formatBoundaryCharacterToken($token, $i, $tokens, $originalTokens);
         $this->formatWhiteSpaceToken($token, $queryValue);
         $this->formatDashToken($token, $i, $tokens);
     }
     return trim(str_replace(array("\t", " \n"), array($this->tab, "\n"), $this->formattedSql)) . "\n";
 }
Пример #4
0
    /**
     * @test
     */
    public function itShouldTokenizeFunction()
    {
        $sql = <<<SQL
SELECT customer_id, customer_name, COUNT(order_id) as total FROM customers GROUP BY customer_id, customer_name
HAVING COUNT(order_id) > 5 ORDER BY COUNT(order_id) DESC;
SQL;
        $result = $this->tokenizer->tokenize($sql);
        $functionFound = false;
        foreach ($result as $token) {
            if ('COUNT' === $token[Tokenizer::TOKEN_VALUE]) {
                $functionFound = true;
                break;
            }
        }
        $this->assertTrue($functionFound);
    }
Пример #5
0
 /**
  * @param Tokenizer $tokenizer
  * @param string    $string
  * @param array     $matches
  */
 public static function isBoundary(Tokenizer $tokenizer, $string, array &$matches)
 {
     if (!$tokenizer->getNextToken() && self::isBoundaryCharacter($string, $matches, $tokenizer->getRegexBoundaries())) {
         $tokenizer->setNextToken(self::getBoundaryCharacter($matches));
     }
 }
Пример #6
0
 /**
  * @param Tokenizer $tokenizer
  * @param string    $string
  */
 public static function isQuoted(Tokenizer $tokenizer, $string)
 {
     if (!$tokenizer->getNextToken() && self::isQuotedString($string)) {
         $tokenizer->setNextToken(self::getQuotedString($string));
     }
 }
Пример #7
0
 /**
  * @param Tokenizer $tokenizer
  * @param string    $string
  * @param array     $matches
  *
  * @return array
  */
 public static function isNumeral(Tokenizer $tokenizer, $string, array &$matches)
 {
     if (!$tokenizer->getNextToken() && self::isNumeralString($string, $matches, $tokenizer->getRegexBoundaries())) {
         $tokenizer->setNextToken(self::getNumeralString($matches));
     }
 }
Пример #8
0
 /**
  * @param Tokenizer $tokenizer
  * @param string    $string
  */
 public static function isComment(Tokenizer $tokenizer, $string)
 {
     if (!$tokenizer->getNextToken() && self::isCommentString($string)) {
         $tokenizer->setNextToken(self::getCommentString($string));
     }
 }
Пример #9
0
 /**
  * @param Tokenizer $tokenizer
  * @param string    $string
  *
  * @return array
  */
 public static function isUserDefinedVariable(Tokenizer $tokenizer, $string)
 {
     if (!$tokenizer->getNextToken() && self::isUserDefinedVariableString($string)) {
         $tokenizer->setNextToken(self::getUserDefinedVariableString($string));
     }
 }
Пример #10
0
 /**
  * @param Tokenizer $tokenizer
  * @param           string $string
  * @param array     $matches
  */
 public static function isWhiteSpace(Tokenizer $tokenizer, $string, array &$matches)
 {
     if (self::isWhiteSpaceString($string, $matches)) {
         $tokenizer->setNextToken(self::getWhiteSpaceString($matches));
     }
 }