-
-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathKey.php
More file actions
104 lines (85 loc) · 2.51 KB
/
Key.php
File metadata and controls
104 lines (85 loc) · 2.51 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Components;
use PhpMyAdmin\SqlParser\Component;
use PhpMyAdmin\SqlParser\Context;
use function implode;
use function trim;
/**
* Parses the definition of a key.
*
* Used for parsing `CREATE TABLE` statement.
*/
final class Key implements Component
{
/**
* The name of this key.
*/
public string|null $name = null;
/**
* The key columns
*
* @var array{name?: string, length?: int, order?: string}[]
*/
public array $columns;
/**
* The type of this key.
*/
public string|null $type = null;
/**
* The expression if the Key is not using column names
*/
public string|null $expr = null;
/**
* The options of this key or null if none where found.
*/
public OptionsArray|null $options = null;
/**
* @param string|null $name the name of the key
* @param array<int, array<string, int|string>> $columns the columns covered by this key
* @param string|null $type the type of this key
* @param OptionsArray|null $options the options of this key
* @phpstan-param array{name?: string, length?: int, order?: string}[] $columns
*/
public function __construct(
string|null $name = null,
array $columns = [],
string|null $type = null,
OptionsArray|null $options = null,
) {
$this->name = $name;
$this->columns = $columns;
$this->type = $type;
$this->options = $options;
}
public function build(): string
{
$ret = $this->type . ' ';
if (! empty($this->name)) {
$ret .= Context::escape($this->name) . ' ';
}
if ($this->expr !== null) {
return $ret . '(' . $this->expr . ') ' . $this->options;
}
$columns = [];
foreach ($this->columns as $column) {
$tmp = '';
if (isset($column['name'])) {
$tmp .= Context::escape($column['name']);
}
if (isset($column['length'])) {
$tmp .= '(' . $column['length'] . ')';
}
if (isset($column['order'])) {
$tmp .= ' ' . $column['order'];
}
$columns[] = $tmp;
}
$ret .= '(' . implode(',', $columns) . ') ' . $this->options;
return trim($ret);
}
public function __toString(): string
{
return $this->build();
}
}