Sequence.php 2.43 KB
Newer Older
1 2
<?php

Michael Moravec's avatar
Michael Moravec committed
3 4
declare(strict_types=1);

5 6
namespace Doctrine\DBAL\Schema;

7
use Doctrine\DBAL\Schema\Visitor\Visitor;
8 9
use function count;
use function sprintf;
10

11
/**
Benjamin Morel's avatar
Benjamin Morel committed
12
 * Sequence structure.
13 14 15
 */
class Sequence extends AbstractAsset
{
16
    /** @var int */
17
    protected $allocationSize = 1;
18

19
    /** @var int */
20 21
    protected $initialValue = 1;

22
    /** @var int|null */
23
    protected $cache = null;
24

25
    public function __construct(string $name, int $allocationSize = 1, int $initialValue = 1, ?int $cache = null)
26 27
    {
        $this->_setName($name);
28 29
        $this->setAllocationSize($allocationSize);
        $this->setInitialValue($initialValue);
30
        $this->cache = $cache;
31 32
    }

33
    public function getAllocationSize() : int
34
    {
35
        return $this->allocationSize;
36 37
    }

38
    public function getInitialValue() : int
39
    {
40 41 42
        return $this->initialValue;
    }

43
    public function getCache() : ?int
44 45
    {
        return $this->cache;
46
    }
47

48
    public function setAllocationSize(int $allocationSize) : self
49
    {
50
        $this->allocationSize = $allocationSize;
51 52

        return $this;
53 54
    }

55
    public function setInitialValue(int $initialValue) : self
56
    {
57
        $this->initialValue = $initialValue;
58 59 60 61

        return $this;
    }

62
    public function setCache(int $cache) : self
63 64 65 66
    {
        $this->cache = $cache;

        return $this;
67
    }
68

69
    /**
Benjamin Morel's avatar
Benjamin Morel committed
70
     * Checks if this sequence is an autoincrement sequence for a given table.
71 72 73 74
     *
     * This is used inside the comparator to not report sequences as missing,
     * when the "from" schema implicitly creates the sequences.
     */
75
    public function isAutoIncrementsFor(Table $table) : bool
76
    {
Sergei Morozov's avatar
Sergei Morozov committed
77 78 79
        $primaryKey = $table->getPrimaryKey();

        if ($primaryKey === null) {
80 81 82
            return false;
        }

Sergei Morozov's avatar
Sergei Morozov committed
83
        $pkColumns = $primaryKey->getColumns();
84

85
        if (count($pkColumns) !== 1) {
86 87 88 89 90
            return false;
        }

        $column = $table->getColumn($pkColumns[0]);

91
        if (! $column->getAutoincrement()) {
92 93 94 95 96
            return false;
        }

        $sequenceName      = $this->getShortestName($table->getNamespaceName());
        $tableName         = $table->getShortestName($table->getNamespaceName());
97
        $tableSequenceName = sprintf('%s_%s_seq', $tableName, $column->getShortestName($table->getNamespaceName()));
98 99 100 101

        return $tableSequenceName === $sequenceName;
    }

102
    public function visit(Visitor $visitor) : void
103 104 105 106
    {
        $visitor->acceptSequence($this);
    }
}