Sequence.php 2.89 KB
Newer Older
1 2 3 4
<?php

namespace Doctrine\DBAL\Schema;

5
use Doctrine\DBAL\Schema\Visitor\Visitor;
6 7
use function count;
use function sprintf;
8

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

17
    /** @var int */
18 19
    protected $initialValue = 1;

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

    /**
24 25 26 27
     * @param string   $name
     * @param int      $allocationSize
     * @param int      $initialValue
     * @param int|null $cache
28
     */
29
    public function __construct($name, $allocationSize = 1, $initialValue = 1, $cache = null)
30 31
    {
        $this->_setName($name);
32 33
        $this->setAllocationSize($allocationSize);
        $this->setInitialValue($initialValue);
34
        $this->cache = $cache;
35 36
    }

Benjamin Morel's avatar
Benjamin Morel committed
37
    /**
38
     * @return int
Benjamin Morel's avatar
Benjamin Morel committed
39
     */
40 41
    public function getAllocationSize()
    {
42
        return $this->allocationSize;
43 44
    }

Benjamin Morel's avatar
Benjamin Morel committed
45
    /**
46
     * @return int
Benjamin Morel's avatar
Benjamin Morel committed
47
     */
48 49
    public function getInitialValue()
    {
50 51 52 53
        return $this->initialValue;
    }

    /**
54
     * @return int|null
55 56 57 58
     */
    public function getCache()
    {
        return $this->cache;
59
    }
60

Benjamin Morel's avatar
Benjamin Morel committed
61
    /**
62
     * @param int $allocationSize
Benjamin Morel's avatar
Benjamin Morel committed
63
     *
Grégoire Paris's avatar
Grégoire Paris committed
64
     * @return Sequence
Benjamin Morel's avatar
Benjamin Morel committed
65
     */
66 67
    public function setAllocationSize($allocationSize)
    {
Sergei Morozov's avatar
Sergei Morozov committed
68
        $this->allocationSize = (int) $allocationSize ?: 1;
69 70

        return $this;
71 72
    }

Benjamin Morel's avatar
Benjamin Morel committed
73
    /**
74
     * @param int $initialValue
Benjamin Morel's avatar
Benjamin Morel committed
75
     *
Grégoire Paris's avatar
Grégoire Paris committed
76
     * @return Sequence
Benjamin Morel's avatar
Benjamin Morel committed
77
     */
78 79
    public function setInitialValue($initialValue)
    {
Sergei Morozov's avatar
Sergei Morozov committed
80
        $this->initialValue = (int) $initialValue ?: 1;
81 82 83 84 85

        return $this;
    }

    /**
86
     * @param int $cache
87
     *
Grégoire Paris's avatar
Grégoire Paris committed
88
     * @return Sequence
89 90 91 92 93 94
     */
    public function setCache($cache)
    {
        $this->cache = $cache;

        return $this;
95
    }
96

97
    /**
Benjamin Morel's avatar
Benjamin Morel committed
98
     * Checks if this sequence is an autoincrement sequence for a given table.
99 100 101 102
     *
     * This is used inside the comparator to not report sequences as missing,
     * when the "from" schema implicitly creates the sequences.
     *
103
     * @return bool
104 105 106
     */
    public function isAutoIncrementsFor(Table $table)
    {
Sergei Morozov's avatar
Sergei Morozov committed
107 108 109
        $primaryKey = $table->getPrimaryKey();

        if ($primaryKey === null) {
110 111 112
            return false;
        }

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

115
        if (count($pkColumns) !== 1) {
116 117 118 119 120
            return false;
        }

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

121
        if (! $column->getAutoincrement()) {
122 123 124 125 126
            return false;
        }

        $sequenceName      = $this->getShortestName($table->getNamespaceName());
        $tableName         = $table->getShortestName($table->getNamespaceName());
127
        $tableSequenceName = sprintf('%s_%s_seq', $tableName, $column->getShortestName($table->getNamespaceName()));
128 129 130 131

        return $tableSequenceName === $sequenceName;
    }

132
    /**
Benjamin Morel's avatar
Benjamin Morel committed
133
     * @return void
134 135 136 137 138 139
     */
    public function visit(Visitor $visitor)
    {
        $visitor->acceptSequence($this);
    }
}