Sequence.php 3.14 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
     *
64
     * @return \Doctrine\DBAL\Schema\Sequence
Benjamin Morel's avatar
Benjamin Morel committed
65
     */
66 67
    public function setAllocationSize($allocationSize)
    {
68 69 70 71 72
        if ($allocationSize > 0) {
            $this->allocationSize = (int) $allocationSize;
        } else {
            $this->allocationSize = 1;
        }
73 74

        return $this;
75 76
    }

Benjamin Morel's avatar
Benjamin Morel committed
77
    /**
78
     * @param int $initialValue
Benjamin Morel's avatar
Benjamin Morel committed
79
     *
80
     * @return \Doctrine\DBAL\Schema\Sequence
Benjamin Morel's avatar
Benjamin Morel committed
81
     */
82 83
    public function setInitialValue($initialValue)
    {
84 85 86 87 88
        if ($initialValue > 0) {
            $this->initialValue = (int) $initialValue;
        } else {
            $this->initialValue = 1;
        }
89 90 91 92 93

        return $this;
    }

    /**
94
     * @param int $cache
95 96 97 98 99 100 101 102
     *
     * @return \Doctrine\DBAL\Schema\Sequence
     */
    public function setCache($cache)
    {
        $this->cache = $cache;

        return $this;
103
    }
104

105
    /**
Benjamin Morel's avatar
Benjamin Morel committed
106
     * Checks if this sequence is an autoincrement sequence for a given table.
107 108 109 110
     *
     * This is used inside the comparator to not report sequences as missing,
     * when the "from" schema implicitly creates the sequences.
     *
111
     * @return bool
112 113 114
     */
    public function isAutoIncrementsFor(Table $table)
    {
Sergei Morozov's avatar
Sergei Morozov committed
115 116 117
        $primaryKey = $table->getPrimaryKey();

        if ($primaryKey === null) {
118 119 120
            return false;
        }

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

123
        if (count($pkColumns) !== 1) {
124 125 126 127 128
            return false;
        }

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

129
        if (! $column->getAutoincrement()) {
130 131 132 133 134
            return false;
        }

        $sequenceName      = $this->getShortestName($table->getNamespaceName());
        $tableName         = $table->getShortestName($table->getNamespaceName());
135
        $tableSequenceName = sprintf('%s_%s_seq', $tableName, $column->getShortestName($table->getNamespaceName()));
136 137 138 139

        return $tableSequenceName === $sequenceName;
    }

140
    /**
Benjamin Morel's avatar
Benjamin Morel committed
141
     * @return void
142 143 144 145 146 147
     */
    public function visit(Visitor $visitor)
    {
        $visitor->acceptSequence($this);
    }
}