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

namespace Doctrine\DBAL\Schema;

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

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

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

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

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

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

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

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

Benjamin Morel's avatar
Benjamin Morel committed
62
    /**
63
     * @param int $allocationSize
Benjamin Morel's avatar
Benjamin Morel committed
64
     *
65
     * @return \Doctrine\DBAL\Schema\Sequence
Benjamin Morel's avatar
Benjamin Morel committed
66
     */
67 68
    public function setAllocationSize($allocationSize)
    {
69
        $this->allocationSize = is_numeric($allocationSize) ? (int) $allocationSize : 1;
70 71

        return $this;
72 73
    }

Benjamin Morel's avatar
Benjamin Morel committed
74
    /**
75
     * @param int $initialValue
Benjamin Morel's avatar
Benjamin Morel committed
76
     *
77
     * @return \Doctrine\DBAL\Schema\Sequence
Benjamin Morel's avatar
Benjamin Morel committed
78
     */
79 80
    public function setInitialValue($initialValue)
    {
81
        $this->initialValue = is_numeric($initialValue) ? (int) $initialValue : 1;
82 83 84 85 86

        return $this;
    }

    /**
87
     * @param int $cache
88 89 90 91 92 93 94 95
     *
     * @return \Doctrine\DBAL\Schema\Sequence
     */
    public function setCache($cache)
    {
        $this->cache = $cache;

        return $this;
96
    }
97

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

        $pkColumns = $table->getPrimaryKey()->getColumns();

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

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

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

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

        return $tableSequenceName === $sequenceName;
    }

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