Class Yiisoft\Db\Syntax\ColumnDefinitionParser
| Inheritance | Yiisoft\Db\Syntax\ColumnDefinitionParser |
|---|
Parses column definition string. For example, string(255) or int unsigned.
Psalm Types
| Name | Value |
|---|---|
| ExtraInfo | array{check?: string, collation?: string, comment?: string, defaultValueRaw?: string, extra?: string, notNull?: boolean, unique?: boolean, unsigned?: boolean} |
Public Methods
| Method | Description | Defined By |
|---|---|---|
| parse() | Parses column definition string. | Yiisoft\Db\Syntax\ColumnDefinitionParser |
Protected Methods
Method Details
| protected array enumInfo ( string $values ) | ||
| $values | string | |
protected function enumInfo(string $values): array
{
preg_match_all("/'((?:''|[^'])*)'/", $values, $matches);
$values = array_map(
static fn(string $value): string => str_replace("''", "'", $value),
$matches[1],
);
return ['values' => $values];
}
| protected array extraInfo ( string $extra ) | ||
| $extra | string | |
protected function extraInfo(string $extra): array
{
if (empty($extra)) {
return [];
}
$info = [];
$bracketsPattern = '(\(((?>[^()]+)|(?-2))*\))';
$defaultPattern = "/\\s*\\bDEFAULT\\s+('(?:[^']|'')*'|\"(?:[^\"]|\"\")*\"|[^(\\s]*$bracketsPattern?\\S*)/i";
$extra = $this->parseStringValue($extra, $defaultPattern, 'defaultValueRaw', $info);
$extra = $this->parseStringValue($extra, "/\\s*\\bCOMMENT\\s+'((?:[^']|'')*)'/i", 'comment', $info);
$extra = $this->parseStringValue($extra, "/\\s*\\bCHECK\\s+$bracketsPattern/i", 'check', $info);
$extra = $this->parseStringValue($extra, '/\s*\bCOLLATE\s+(\S+)/i', 'collation', $info);
$extra = $this->parseBoolValue($extra, '/\s*\bUNSIGNED\b/i', 'unsigned', $info);
$extra = $this->parseBoolValue($extra, '/\s*\bUNIQUE\b/i', 'unique', $info);
$extra = $this->parseBoolValue($extra, '/\s*\bNOT\s+NULL\b/i', 'notNull', $info);
if (empty($info['notNull'])) {
$extra = $this->parseBoolValue($extra, '/\s*\bNULL\b/i', 'notNull', $info);
if (!empty($info['notNull'])) {
$info['notNull'] = false;
}
}
/** @psalm-var ExtraInfo $info */
if (!empty($info['comment'])) {
$info['comment'] = str_replace("''", "'", $info['comment']);
}
if (!empty($info['check'])) {
$info['check'] = substr($info['check'], 1, -1);
}
if (!empty($extra)) {
$info['extra'] = $extra;
}
return $info;
}
Parses column definition string.
| public array parse ( string $definition ) | ||
| $definition | string |
The column definition string. For example, |
| return | array |
The column information. |
|---|---|---|
public function parse(string $definition): array
{
preg_match("/^(\w*)(?:\(((?:'[^']*'|[^)])+)\))?(\[[\d\[\]]*\])?\s*/", $definition, $matches);
$type = strtolower($matches[1]);
$info = ['type' => $type];
if (isset($matches[2]) && $matches[2] !== '') {
if ($type === 'enum') {
$info += $this->enumInfo($matches[2]);
} else {
$info += $this->sizeInfo($matches[2]);
}
}
if (isset($matches[3])) {
/** @psalm-var positive-int */
$info['dimension'] = substr_count($matches[3], '[');
}
$extra = substr($definition, strlen($matches[0]));
return $info + $this->extraInfo($extra);
}
| protected string parseBoolValue ( string $extra, string $pattern, string $name, array &$info ) | ||
| $extra | string | |
| $pattern | string | |
| $name | string | |
| $info | array | |
protected function parseBoolValue(string $extra, string $pattern, string $name, array &$info): string
{
if (empty($extra)) {
return '';
}
/** @psalm-suppress PossiblyNullArgument */
$extra = trim(preg_replace($pattern, '', $extra, 1, $count));
if ($count > 0) {
$info[$name] = true;
}
return $extra;
}
| protected string parseStringValue ( string $extra, string $pattern, string $name, array &$info ) | ||
| $extra | string | |
| $pattern | string | |
| $name | string | |
| $info | array | |
protected function parseStringValue(string $extra, string $pattern, string $name, array &$info): string
{
if (!empty($extra) && preg_match($pattern, $extra, $matches) === 1) {
$info[$name] = $matches[1];
return trim(str_replace($matches[0], '', $extra));
}
return $extra;
}
Signup or Login in order to comment.