Esempio n. 1
0
 public function canParseTokens(SQLTokenIterator $tokens, &$checkFlags = 0)
 {
     $previousIndex = $tokens->getIndex();
     if (!$tokens->seekTokenNum(SqlToken::T_IN(), TokenIterator::NEXT, [SqlToken::T_NOT()])) {
         $tokens->seekIndex($previousIndex);
         return false;
     }
     $result = is_int($tokens->isTokenText('('));
     $tokens->seekIndex($previousIndex);
     return $result;
 }
Esempio n. 2
0
 public function canParseTokens(SQLTokenIterator $tokens)
 {
     $indexBefore = $tokens->getIndex();
     $tokens->seekIndex($tokens->getExclusiveTokenIndex());
     if (!in_array((int) (string) $tokens->getCurrentTokenNumber(), [T_STRING, SqlToken::T_DATABASE]) && !$tokens->getCurrentTokenNumber() instanceof Token) {
         $tokens->seekIndex($indexBefore);
         return false;
     }
     $paranthesisExist = is_int($tokens->isTokenText('('));
     $tokens->seekIndex($indexBefore);
     return $paranthesisExist;
 }
Esempio n. 3
0
 public function canParseTokens(SQLTokenIterator $tokens, &$skipChecks = 0)
 {
     $previousIndex = $tokens->getIndex();
     $result = is_int($tokens->isTokenNum(SqlToken::T_LIKE(), TokenIterator::NEXT, [SqlToken::T_NOT()])) || is_int($tokens->isTokenNum(SqlToken::T_LIKE(), TokenIterator::CURRENT, [SqlToken::T_NOT()]));
     $tokens->seekIndex($previousIndex);
     return $result;
 }
Esempio n. 4
0
 public function convertSqlToJob(SQLTokenIterator $tokens)
 {
     $parameterCondition = new ParameterConditionJob();
     try {
         $parameter = Parameter::getByValue(strtolower($tokens->getExclusiveTokenString()));
         if (is_null($parameter)) {
             throw new MalformedSqlException("Invalid parameter value given for parameter condition!");
         }
         $parameterCondition->setParameter($parameter);
         $tokens->seekIndex($tokens->getExclusiveTokenIndex());
     } catch (MalformedSqlException $exception) {
         throw new MalformedSqlException($exception->getMessage(), $tokens);
     } catch (\Exception $exception) {
         throw new ErrorException("Tried to parse parameter-condition when token-iterator does not point to valid parameter!");
     }
     switch ($parameter) {
         case Parameter::SEPARATOR:
             if (!$this->valueParser->canParseTokens($tokens)) {
                 throw new MalformedSqlException("Missing valid value after parameter-condition {$parameterCondition->getParameter()->getValue()}!", $tokens);
             }
             $parameterCondition->setValue($this->valueParser->convertSqlToJob($tokens));
             break;
     }
     return $parameterCondition;
 }
Esempio n. 5
0
 public function convertSqlToJob(SQLTokenIterator $tokens)
 {
     $parts = array();
     do {
         $tokens->seekIndex($tokens->getExclusiveTokenIndex());
         $part = $tokens->getCurrentTokenString();
         if ($part[0] === '`' && $part[strlen($part) - 1] === '`') {
             $part = substr($part, 1, strlen($part) - 2);
         }
         $parts[] = $part;
     } while ($tokens->seekTokenText(".") && !$tokens->isTokenText('*'));
     return TableSpecifier::factory(implode(".", $parts));
 }
Esempio n. 6
0
 public function convertSqlToJob(SQLTokenIterator $tokens)
 {
     $columnDefinition = new ColumnDefinitionJob();
     $tokens->seekTokens([T_STRING, T_CONSTANT_ENCAPSED_STRING]);
     if (!$tokens->isTokens([T_STRING, T_CONSTANT_ENCAPSED_STRING], TokenIterator::CURRENT)) {
         throw new MalformedSqlException("Missing name for column!", $tokens);
     }
     $name = $tokens->getCurrentTokenString();
     if ($name[0] === '`' && $name[strlen($name) - 1] === '`') {
         $name = substr($name, 1, strlen($name) - 2);
     }
     if ($name[0] === '"' && $name[strlen($name) - 1] === '"') {
         $name = substr($name, 1, strlen($name) - 2);
     }
     if ($name[0] === "'" && $name[strlen($name) - 1] === "'") {
         $name = substr($name, 1, strlen($name) - 2);
     }
     $columnDefinition->setName($name);
     # this makes sure that the next token is valid data-type
     $dataTypeString = strtoupper($tokens->getExclusiveTokenString());
     $dataType = DataType::factory($dataTypeString);
     $columnDefinition->setDataType($dataType);
     $tokens->seekIndex($tokens->getExclusiveTokenIndex());
     # data-type-length
     if ($tokens->seekTokenText('(')) {
         if ($dataType === DataType::ENUM() || $dataType === DataType::SET()) {
             do {
                 if (!$this->valueParser->canParseTokens($tokens)) {
                     throw new MalformedSqlException("Invalid value in ENUM!", $tokens);
                 }
                 $columnDefinition->addEnumValue($this->valueParser->convertSqlToJob($tokens));
             } while ($tokens->seekTokenText(','));
         } else {
             if (!$tokens->seekTokenNum(T_NUM_STRING)) {
                 throw new MalformedSqlException("Missing number for length of data-type!", $tokens);
             }
             $columnDefinition->setDataTypeLength((int) $tokens->getCurrentTokenString());
             if ($tokens->seekTokenText(',')) {
                 if (!$tokens->seekTokenNum(T_NUM_STRING)) {
                     throw new MalformedSqlException("Missing second number for length of data-type!", $tokens);
                 }
                 $columnDefinition->setDataTypeSecondLength((int) $tokens->getCurrentTokenString());
             }
         }
         if (!$tokens->seekTokenText(')')) {
             throw new MalformedSqlException("Missing end-parenthesis for length of data-type!", $tokens);
         }
     }
     while (true) {
         switch (true) {
             case $tokens->seekTokenNum(SqlToken::T_NOT()):
                 if (!$tokens->seekTokenNum(SqlToken::T_NULL())) {
                     throw new MalformedSqlException("Missing T_NULL after T_NOT in column-definition!", $tokens);
                 }
                 $columnDefinition->setIsNullable(false);
                 break;
             case $tokens->seekTokenNum(SqlToken::T_NULL()):
                 $columnDefinition->setIsNullable(true);
                 break;
             case $tokens->seekTokenNum(SqlToken::T_DEFAULT()):
                 if (!$this->valueParser->canParseTokens($tokens)) {
                     throw new MalformedSqlException("Missing valid default value for column definition!", $tokens);
                 }
                 $beforeIndex = $tokens->getIndex();
                 $defaultValue = $this->valueParser->convertSqlToJob($tokens);
                 $afterIndex = $tokens->getIndex();
                 $defaultSqlRepresentation = "";
                 for ($index = $beforeIndex + 1; $index <= $afterIndex; $index++) {
                     $defaultSqlRepresentation .= $tokens->getTokenArray()[$index][1];
                 }
                 $columnDefinition->setDefaultValue($defaultValue);
                 break;
             case $tokens->seekTokenNum(SqlToken::T_UNIQUE()):
                 $tokens->seekTokenNum(SqlToken::T_KEY());
                 $columnDefinition->setIsUnique(true);
                 break;
             case $tokens->seekTokenNum(SqlToken::T_PRIMARY()):
                 $tokens->seekTokenNum(SqlToken::T_KEY());
                 $columnDefinition->setIsPrimaryKey(true);
                 break;
             case $tokens->seekTokenNum(SqlToken::T_UNSIGNED()):
                 $columnDefinition->setIsUnsigned(true);
                 break;
             case $tokens->seekTokenNum(SqlToken::T_COMMENT()):
                 if (!$tokens->seekTokenNum(T_CONSTANT_ENCAPSED_STRING)) {
                     throw new MalformedSqlException("Missing encapsed string for comment-declaration column-definition!", $tokens);
                 }
                 $columnDefinition->setComment($tokens->getCurrentTokenString());
                 break;
             case $tokens->seekTokenNum(SqlToken::T_AUTO_INCREMENT()):
                 $columnDefinition->setAutoIncrement(true);
                 break;
             case $tokens->seekTokenNum(SqlToken::T_CHARACTER(), TokenIterator::NEXT, [SqlToken::T_DEFAULT()]):
                 if (!$tokens->seekTokenNum(SqlToken::T_SET())) {
                     throw new MalformedSqlException("MIssing SET after CHARACTER keyword!", $tokens);
                 }
             case $tokens->seekTokenNum(SqlToken::T_CHARSET(), TokenIterator::NEXT, [SqlToken::T_DEFAULT()]):
                 $tokens->seekTokenText('=');
                 if (!$tokens->seekTokenNum(T_CONSTANT_ENCAPSED_STRING) && !$tokens->seekTokenNum(T_STRING)) {
                     throw new MalformedSqlException("Missing string for CHARACTER SET!", $tokens);
                 }
                 $columnDefinition->setCharacterSet($tokens->getCurrentTokenString());
                 break;
             case $tokens->seekTokenNum(SqlToken::T_COLLATE()):
                 $tokens->seekTokenText('=');
                 if (!$tokens->seekTokenNum(T_CONSTANT_ENCAPSED_STRING) && !$tokens->seekTokenNum(T_STRING)) {
                     throw new MalformedSqlException("Missing string for COLLATE!", $tokens);
                 }
                 $columnDefinition->setCollate($tokens->getCurrentTokenString());
                 break;
             case $tokens->seekTokenNum(SqlToken::T_ON()):
                 switch (true) {
                     case $tokens->seekTokenNum(SqlToken::T_UPDATE()):
                         switch (true) {
                             case $this->valueParser->canParseTokens($tokens):
                                 $columnDefinition->setOnUpdate($this->valueParser->convertSqlToJob($tokens));
                                 break;
                             default:
                                 throw new MalformedSqlException("Invalid value for ON UPDATE!", $tokens);
                         }
                         break;
                     case $tokens->seekTokenNum(SqlToken::T_DELETE()):
                         switch (true) {
                             case $this->valueParser->canParseTokens($tokens):
                                 $columnDefinition->setOnDelete($this->valueParser->convertSqlToJob($tokens));
                                 break;
                             default:
                                 throw new MalformedSqlException("Invalid value for ON UPDATE!", $tokens);
                         }
                         break;
                     default:
                         throw new MalformedSqlException("Only UPDATE and DELETE allowed for ON trigger!", $tokens);
                 }
                 break;
             case is_int($tokens->isTokenText(')')):
             case is_int($tokens->isTokenText(',')):
                 break 2;
             default:
                 break 2;
         }
     }
     return $columnDefinition;
 }