-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathColumnDefinitionParser.php
More file actions
73 lines (64 loc) · 1.89 KB
/
ColumnDefinitionParser.php
File metadata and controls
73 lines (64 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
declare(strict_types=1);
namespace Yiisoft\Db\Pgsql\Column;
use Yiisoft\Db\Syntax\AbstractColumnDefinitionParser;
use function preg_match;
use function preg_replace;
use function strlen;
use function substr;
/**
* Parses column definition string. For example, `string(255)` or `int unsigned`.
*/
final class ColumnDefinitionParser extends AbstractColumnDefinitionParser
{
private const TYPE_PATTERN = '/^(?:('
. 'time(?:stamp)?\s*(?:\((\d+)\))? with(?:out)? time zone'
. ')|('
. '(?:character|bit) varying'
. '|double precision'
. '|\w*'
. ")(?:\(((?:'[^']*'|[^)])+)\))?)(\[[\d\[\]]*\])?\s*/i";
protected function parseDefinition(string $definition): array
{
preg_match(self::TYPE_PATTERN, $definition, $matches);
/** @var string $type */
$type = $matches[3] ?? preg_replace('/\s*\(\d+\)/', '', $matches[1]);
return [
$type,
$matches[4] ?? $matches[2] ?? null,
$matches[5] ?? null,
substr($definition, strlen($matches[0])),
];
}
protected function parseTypeParams(string $type, string $params): array
{
return match ($type) {
'bit varying',
'bit',
'bpchar',
'char',
'character varying',
'character',
'decimal',
'double precision',
'float4',
'float8',
'int',
'interval',
'numeric',
'real',
'string',
'time with time zone',
'time without time zone',
'time',
'timestamp with time zone',
'timestamp without time zone',
'timestamp',
'timestamptz',
'timetz',
'varbit',
'varchar' => $this->parseSizeInfo($params),
default => [],
};
}
}