1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\DBAL\Schema; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Schema\Visitor\Visitor; |
6
|
|
|
use function count; |
7
|
|
|
use function is_numeric; |
8
|
|
|
use function sprintf; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Sequence structure. |
12
|
|
|
* |
13
|
|
|
* @link www.doctrine-project.org |
14
|
|
|
*/ |
15
|
|
|
class Sequence extends AbstractAsset |
16
|
|
|
{ |
17
|
|
|
/** @var int */ |
18
|
|
|
protected $allocationSize = 1; |
19
|
|
|
|
20
|
|
|
/** @var int */ |
21
|
|
|
protected $initialValue = 1; |
22
|
|
|
|
23
|
|
|
/** @var int|null */ |
24
|
|
|
protected $cache = null; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string $name |
28
|
|
|
* @param int $allocationSize |
29
|
|
|
* @param int $initialValue |
30
|
|
|
* @param int|null $cache |
31
|
|
|
*/ |
32
|
780 |
|
public function __construct($name, $allocationSize = 1, $initialValue = 1, $cache = null) |
33
|
|
|
{ |
34
|
780 |
|
$this->_setName($name); |
35
|
780 |
|
$this->setAllocationSize($allocationSize); |
36
|
780 |
|
$this->setInitialValue($initialValue); |
37
|
780 |
|
$this->cache = $cache; |
38
|
780 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return int |
42
|
|
|
*/ |
43
|
429 |
|
public function getAllocationSize() |
44
|
|
|
{ |
45
|
429 |
|
return $this->allocationSize; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return int |
50
|
|
|
*/ |
51
|
410 |
|
public function getInitialValue() |
52
|
|
|
{ |
53
|
410 |
|
return $this->initialValue; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return int|null |
58
|
|
|
*/ |
59
|
305 |
|
public function getCache() |
60
|
|
|
{ |
61
|
305 |
|
return $this->cache; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param int $allocationSize |
66
|
|
|
* |
67
|
|
|
* @return \Doctrine\DBAL\Schema\Sequence |
68
|
|
|
*/ |
69
|
780 |
|
public function setAllocationSize($allocationSize) |
70
|
|
|
{ |
71
|
780 |
|
$this->allocationSize = is_numeric($allocationSize) ? (int) $allocationSize : 1; |
|
|
|
|
72
|
|
|
|
73
|
780 |
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param int $initialValue |
78
|
|
|
* |
79
|
|
|
* @return \Doctrine\DBAL\Schema\Sequence |
80
|
|
|
*/ |
81
|
780 |
|
public function setInitialValue($initialValue) |
82
|
|
|
{ |
83
|
780 |
|
$this->initialValue = is_numeric($initialValue) ? (int) $initialValue : 1; |
|
|
|
|
84
|
|
|
|
85
|
780 |
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param int $cache |
90
|
|
|
* |
91
|
|
|
* @return \Doctrine\DBAL\Schema\Sequence |
92
|
|
|
*/ |
93
|
|
|
public function setCache($cache) |
94
|
|
|
{ |
95
|
|
|
$this->cache = $cache; |
96
|
|
|
|
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Checks if this sequence is an autoincrement sequence for a given table. |
102
|
|
|
* |
103
|
|
|
* This is used inside the comparator to not report sequences as missing, |
104
|
|
|
* when the "from" schema implicitly creates the sequences. |
105
|
|
|
* |
106
|
|
|
* @return bool |
107
|
|
|
*/ |
108
|
76 |
|
public function isAutoIncrementsFor(Table $table) |
109
|
|
|
{ |
110
|
76 |
|
if (! $table->hasPrimaryKey()) { |
111
|
|
|
return false; |
112
|
|
|
} |
113
|
|
|
|
114
|
76 |
|
$pkColumns = $table->getPrimaryKey()->getColumns(); |
115
|
|
|
|
116
|
76 |
|
if (count($pkColumns) !== 1) { |
117
|
|
|
return false; |
118
|
|
|
} |
119
|
|
|
|
120
|
76 |
|
$column = $table->getColumn($pkColumns[0]); |
121
|
|
|
|
122
|
76 |
|
if (! $column->getAutoincrement()) { |
123
|
|
|
return false; |
124
|
|
|
} |
125
|
|
|
|
126
|
76 |
|
$sequenceName = $this->getShortestName($table->getNamespaceName()); |
127
|
76 |
|
$tableName = $table->getShortestName($table->getNamespaceName()); |
128
|
76 |
|
$tableSequenceName = sprintf('%s_%s_seq', $tableName, $column->getShortestName($table->getNamespaceName())); |
129
|
|
|
|
130
|
76 |
|
return $tableSequenceName === $sequenceName; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return void |
135
|
|
|
*/ |
136
|
76 |
|
public function visit(Visitor $visitor) |
137
|
|
|
{ |
138
|
76 |
|
$visitor->acceptSequence($this); |
139
|
76 |
|
} |
140
|
|
|
} |
141
|
|
|
|