/** * Parses a parameter of a routine. * * @param string $param Parameter's definition. * * @return array */ public static function getReturnType($param) { $lexer = new Lexer($param); // A dummy parser is used for error reporting. $type = DataType::parse(new Parser(), $lexer->list); if ($type === null) { return array('', '', '', '', ''); } $options = array(); foreach ($type->options->options as $opt) { $options[] = is_string($opt) ? $opt : $opt['value']; } return array('', '', $type->name, implode(',', $type->parameters), implode(' ', $options)); }
/** * @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 `CREATE`. // Parsing options. $this->options = OptionsArray::parse($parser, $list, static::$OPTIONS); ++$list->idx; // Skipping last option. // Parsing the field name. $this->name = Expression::parse($parser, $list, array('noAlias' => true, 'noBrackets' => true, 'skipColumn' => true)); if (!isset($this->name) || $this->name === '') { $parser->error(__('The name of the entity was expected.'), $list->tokens[$list->idx]); } else { ++$list->idx; // Skipping field. } if ($this->options->has('DATABASE')) { $this->entityOptions = OptionsArray::parse($parser, $list, static::$DB_OPTIONS); } elseif ($this->options->has('TABLE')) { $this->fields = CreateDefinition::parse($parser, $list); if (empty($this->fields)) { $parser->error(__('At least one column definition was expected.'), $list->tokens[$list->idx]); } ++$list->idx; $this->entityOptions = OptionsArray::parse($parser, $list, static::$TABLE_OPTIONS); /** * The field that is being filled (`partitionBy` or * `subpartitionBy`). * * @var string $field */ $field = null; /** * The number of brackets. `false` means no bracket was found * previously. At least one bracket is required to validate the * expression. * * @var int|bool $brackets */ $brackets = false; /* * Handles partitions. */ 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 comments. if ($token->type === Token::TYPE_COMMENT) { continue; } if ($token->type === Token::TYPE_KEYWORD && $token->value === 'PARTITION BY') { $field = 'partitionBy'; $brackets = false; } elseif ($token->type === Token::TYPE_KEYWORD && $token->value === 'SUBPARTITION BY') { $field = 'subpartitionBy'; $brackets = false; } elseif ($token->type === Token::TYPE_KEYWORD && $token->value === 'PARTITIONS') { $token = $list->getNextOfType(Token::TYPE_NUMBER); --$list->idx; // `getNextOfType` also advances one position. $this->partitionsNum = $token->value; } elseif ($token->type === Token::TYPE_KEYWORD && $token->value === 'SUBPARTITIONS') { $token = $list->getNextOfType(Token::TYPE_NUMBER); --$list->idx; // `getNextOfType` also advances one position. $this->subpartitionsNum = $token->value; } elseif (!empty($field)) { /* * Handling the content of `PARTITION BY` and `SUBPARTITION BY`. */ // Counting brackets. if ($token->type === Token::TYPE_OPERATOR && $token->value === '(') { // This is used instead of `++$brackets` because, // initially, `$brackets` is `false` cannot be // incremented. $brackets = $brackets + 1; } elseif ($token->type === Token::TYPE_OPERATOR && $token->value === ')') { --$brackets; } // Building the expression used for partitioning. $this->{$field} .= $token->type === Token::TYPE_WHITESPACE ? ' ' : $token->token; // Last bracket was read, the expression ended. // Comparing with `0` and not `false`, because `false` means // that no bracket was found and at least one must is // required. if ($brackets === 0) { $this->{$field} = trim($this->{$field}); $field = null; } } elseif ($token->type === Token::TYPE_OPERATOR && $token->value === '(') { if (!empty($this->partitionBy)) { $this->partitions = ArrayObj::parse($parser, $list, array('type' => 'SqlParser\\Components\\PartitionDefinition')); } break; } } } elseif ($this->options->has('PROCEDURE') || $this->options->has('FUNCTION')) { $this->parameters = ParameterDefinition::parse($parser, $list); if ($this->options->has('FUNCTION')) { $token = $list->getNextOfType(Token::TYPE_KEYWORD); if ($token->value !== 'RETURNS') { $parser->error(__('A "RETURNS" keyword was expected.'), $token); } else { ++$list->idx; $this->return = DataType::parse($parser, $list); } } ++$list->idx; $this->entityOptions = OptionsArray::parse($parser, $list, static::$FUNC_OPTIONS); ++$list->idx; for (; $list->idx < $list->count; ++$list->idx) { $token = $list->tokens[$list->idx]; $this->body[] = $token; } } elseif ($this->options->has('VIEW')) { $token = $list->getNext(); // Skipping whitespaces and comments. // Parsing columns list. if ($token->type === Token::TYPE_OPERATOR && $token->value === '(') { --$list->idx; // getNext() also goes forward one field. $this->fields = ArrayObj::parse($parser, $list); ++$list->idx; // Skipping last token from the array. $list->getNext(); } // Parsing the `AS` keyword. for (; $list->idx < $list->count; ++$list->idx) { $token = $list->tokens[$list->idx]; if ($token->type === Token::TYPE_DELIMITER) { break; } $this->body[] = $token; } } elseif ($this->options->has('TRIGGER')) { // Parsing the time and the event. $this->entityOptions = OptionsArray::parse($parser, $list, static::$TRIGGER_OPTIONS); ++$list->idx; $list->getNextOfTypeAndValue(Token::TYPE_KEYWORD, 'ON'); ++$list->idx; // Skipping `ON`. // Parsing the name of the table. $this->table = Expression::parse($parser, $list, array('noAlias' => true, 'noBrackets' => true, 'skipColumn' => true)); ++$list->idx; $list->getNextOfTypeAndValue(Token::TYPE_KEYWORD, 'FOR EACH ROW'); ++$list->idx; // Skipping `FOR EACH ROW`. for (; $list->idx < $list->count; ++$list->idx) { $token = $list->tokens[$list->idx]; $this->body[] = $token; } } else { for (; $list->idx < $list->count; ++$list->idx) { $token = $list->tokens[$list->idx]; if ($token->type === Token::TYPE_DELIMITER) { break; } $this->body[] = $token; } } }
/** * @param CreateDefinition|CreateDefinition[] $component The component to be built. * @param array $options Parameters for building. * * @return string */ public static function build($component, array $options = array()) { if (is_array($component)) { return "(\n " . implode(",\n ", $component) . "\n)"; } else { $tmp = ''; if ($component->isConstraint) { $tmp .= 'CONSTRAINT '; } if (isset($component->name) && $component->name !== '') { $tmp .= Context::escape($component->name) . ' '; } if (!empty($component->type)) { $tmp .= DataType::build($component->type, array('lowercase' => true)) . ' '; } if (!empty($component->key)) { $tmp .= $component->key . ' '; } if (!empty($component->references)) { $tmp .= 'REFERENCES ' . $component->references . ' '; } $tmp .= $component->options; return trim($tmp); } }
/** * @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 `CREATE`. // Parsing options. $this->options = OptionsArray::parse($parser, $list, static::$OPTIONS); ++$list->idx; // Skipping last option. // Parsing the field name. $this->name = Expression::parse($parser, $list, array('noAlias' => true, 'noBrackets' => true, 'skipColumn' => true)); ++$list->idx; // Skipping field. if ($this->options->has('DATABASE')) { $this->entityOptions = OptionsArray::parse($parser, $list, static::$DB_OPTIONS); } elseif ($this->options->has('TABLE')) { $this->fields = FieldDefinition::parse($parser, $list); ++$list->idx; $this->entityOptions = OptionsArray::parse($parser, $list, static::$TABLE_OPTIONS); } elseif ($this->options->has('PROCEDURE') || $this->options->has('FUNCTION')) { $this->parameters = ParameterDefinition::parse($parser, $list); if ($this->options->has('FUNCTION')) { $token = $list->getNextOfType(Token::TYPE_KEYWORD); if ($token->value !== 'RETURNS') { $parser->error('\'RETURNS\' keyword was expected.', $token); } else { ++$list->idx; $this->return = DataType::parse($parser, $list); } } ++$list->idx; $this->entityOptions = OptionsArray::parse($parser, $list, static::$FUNC_OPTIONS); ++$list->idx; for (; $list->idx < $list->count; ++$list->idx) { $token = $list->tokens[$list->idx]; $this->body[] = $token; } } else { if ($this->options->has('VIEW')) { $token = $list->getNext(); // Skipping whitespaces and comments. // Parsing columns list. if ($token->type === Token::TYPE_OPERATOR && $token->value === '(') { --$list->idx; // getNext() also goes forward one field. $this->fields = ArrayObj::parse($parser, $list); ++$list->idx; // Skipping last token from the array. $list->getNext(); } // Parsing the `AS` keyword. for (; $list->idx < $list->count; ++$list->idx) { $token = $list->tokens[$list->idx]; if ($token->type === Token::TYPE_DELIMITER) { break; } $this->body[] = $token; } } else { if ($this->options->has('TRIGGER')) { // Parsing the time and the event. $this->entityOptions = OptionsArray::parse($parser, $list, static::$TRIGGER_OPTIONS); ++$list->idx; $list->getNextOfTypeAndValue(Token::TYPE_KEYWORD, 'ON'); ++$list->idx; // Skipping `ON`. // Parsing the name of the table. $this->table = Expression::parse($parser, $list, array('noAlias' => true, 'noBrackets' => true, 'skipColumn' => true)); ++$list->idx; $list->getNextOfTypeAndValue(Token::TYPE_KEYWORD, 'FOR EACH ROW'); ++$list->idx; // Skipping `FOR EACH ROW`. for (; $list->idx < $list->count; ++$list->idx) { $token = $list->tokens[$list->idx]; $this->body[] = $token; } } } } }
/** * @param FieldDefinition|FieldDefinition[] $component The component to be built. * * @return string */ public static function build($component) { if (is_array($component)) { $ret = array(); foreach ($component as $c) { $ret[] = static::build($c); } return "(\n" . implode(",\n", $ret) . "\n)"; } else { $tmp = ''; if ($component->isConstraint) { $tmp .= 'CONSTRAINT '; } if (!empty($component->name)) { $tmp .= Context::escape($component->name) . ' '; } if (!empty($component->type)) { $tmp .= DataType::build($component->type) . ' '; } if (!empty($component->key)) { $tmp .= Key::build($component->key) . ' '; } if (!empty($component->references)) { $tmp .= 'REFERENCES ' . Reference::build($component->references) . ' '; } $tmp .= OptionsArray::build($component->options); return trim($tmp); } }
/** * @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 ParameterDefinition[] */ public static function parse(Parser $parser, TokensList $list, array $options = array()) { $ret = array(); $expr = new ParameterDefinition(); /** * The state of the parser. * * Below are the states of the parser. * * 0 -----------------------[ ( ]------------------------> 1 * * 1 ----------------[ IN / OUT / INOUT ]----------------> 1 * 1 ----------------------[ name ]----------------------> 2 * * 2 -------------------[ data type ]--------------------> 3 * * 3 ------------------------[ , ]-----------------------> 1 * 3 ------------------------[ ) ]-----------------------> (END) * * @var int $state */ $state = 0; 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_OPERATOR && $token->value === '(') { $state = 1; } continue; } elseif ($state === 1) { if ($token->value === 'IN' || $token->value === 'OUT' || $token->value === 'INOUT') { $expr->inOut = $token->value; ++$list->idx; } elseif ($token->value === ')') { ++$list->idx; break; } else { $expr->name = $token->value; $state = 2; } } elseif ($state === 2) { $expr->type = DataType::parse($parser, $list); $state = 3; } elseif ($state === 3) { $ret[] = $expr; $expr = new ParameterDefinition(); if ($token->value === ',') { $state = 1; } elseif ($token->value === ')') { ++$list->idx; break; } } } // Last iteration was not saved. if (isset($expr->name) && $expr->name !== '') { $ret[] = $expr; } --$list->idx; return $ret; }
/** * @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 CreateDefinition[] */ public static function parse(Parser $parser, TokensList $list, array $options = array()) { $ret = array(); $expr = new CreateDefinition(); /** * The state of the parser. * * Below are the states of the parser. * * 0 -----------------------[ ( ]------------------------> 1 * * 1 --------------------[ CONSTRAINT ]------------------> 1 * 1 -----------------------[ key ]----------------------> 2 * 1 -------------[ constraint / column name ]-----------> 2 * * 2 --------------------[ data type ]-------------------> 3 * * 3 ---------------------[ options ]--------------------> 4 * * 4 --------------------[ REFERENCES ]------------------> 4 * * 5 ------------------------[ , ]-----------------------> 1 * 5 ------------------------[ ) ]-----------------------> 6 (-1) * * @var int $state */ $state = 0; 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_OPERATOR && $token->value === '(') { $state = 1; } else { $parser->error(__('An opening bracket was expected.'), $token); break; } } elseif ($state === 1) { if ($token->type === Token::TYPE_KEYWORD && $token->value === 'CONSTRAINT') { $expr->isConstraint = true; } elseif ($token->type === Token::TYPE_KEYWORD && $token->flags & Token::FLAG_KEYWORD_KEY) { $expr->key = Key::parse($parser, $list); $state = 4; } else { $expr->name = $token->value; if (!$expr->isConstraint) { $state = 2; } } } elseif ($state === 2) { $expr->type = DataType::parse($parser, $list); $state = 3; } elseif ($state === 3) { $expr->options = OptionsArray::parse($parser, $list, static::$FIELD_OPTIONS); $state = 4; } elseif ($state === 4) { if ($token->type === Token::TYPE_KEYWORD && $token->value === 'REFERENCES') { ++$list->idx; // Skipping keyword 'REFERENCES'. $expr->references = Reference::parse($parser, $list); } else { --$list->idx; } $state = 5; } elseif ($state === 5) { if (!empty($expr->type) || !empty($expr->key)) { $ret[] = $expr; } $expr = new CreateDefinition(); if ($token->value === ',') { $state = 1; } elseif ($token->value === ')') { $state = 6; ++$list->idx; break; } else { $parser->error(__('A comma or a closing bracket was expected.'), $token); $state = 0; break; } } } // Last iteration was not saved. if (!empty($expr->type) || !empty($expr->key)) { $ret[] = $expr; } if ($state !== 0 && $state !== 6) { $parser->error(__('A closing bracket was expected.'), $list->tokens[$list->idx - 1]); } --$list->idx; return $ret; }
/** * @param ParameterDefinition[] $component The component to be built. * * @return string */ public static function build($component) { $ret = array(); foreach ($component as $c) { $tmp = ''; if (!empty($c->inOut)) { $tmp .= $c->inOut . ' '; } $ret[] = trim($tmp . Context::escape($c->name) . ' ' . DataType::build($c->type)); } return '(' . implode(', ', $ret) . ')'; }