Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / dbal / lib / Doctrine / DBAL / Schema / Sequence.php
1 <?php
2 /*
3  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14  *
15  * This software consists of voluntary contributions made by many individuals
16  * and is licensed under the MIT license. For more information, see
17  * <http://www.doctrine-project.org>.
18  */
19
20 namespace Doctrine\DBAL\Schema;
21
22 use Doctrine\DBAL\Schema\Visitor\Visitor;
23
24 /**
25  * Sequence Structure
26  *
27  * 
28  * @link    www.doctrine-project.org
29  * @since   2.0
30  * @version $Revision$
31  * @author  Benjamin Eberlei <kontakt@beberlei.de>
32  */
33 class Sequence extends AbstractAsset
34 {
35     /**
36      * @var int
37      */
38     protected $_allocationSize = 1;
39
40     /**
41      * @var int
42      */
43     protected $_initialValue = 1;
44
45     /**
46      *
47      * @param string $name
48      * @param int $allocationSize
49      * @param int $initialValue
50      */
51     public function __construct($name, $allocationSize=1, $initialValue=1)
52     {
53         $this->_setName($name);
54         $this->_allocationSize = (is_numeric($allocationSize))?$allocationSize:1;
55         $this->_initialValue = (is_numeric($initialValue))?$initialValue:1;
56     }
57
58     public function getAllocationSize()
59     {
60         return $this->_allocationSize;
61     }
62
63     public function getInitialValue()
64     {
65         return $this->_initialValue;
66     }
67
68     public function setAllocationSize($allocationSize)
69     {
70         $this->_allocationSize = (is_numeric($allocationSize))?$allocationSize:1;
71     }
72
73     public function setInitialValue($initialValue)
74     {
75         $this->_initialValue = (is_numeric($initialValue))?$initialValue:1;
76     }
77
78     /**
79      * Check if this sequence is an autoincrement sequence for a given table.
80      *
81      * This is used inside the comparator to not report sequences as missing,
82      * when the "from" schema implicitly creates the sequences.
83      *
84      * @param Table $table
85      *
86      * @return bool
87      */
88     public function isAutoIncrementsFor(Table $table)
89     {
90         if ( ! $table->hasPrimaryKey()) {
91             return false;
92         }
93
94         $pkColumns = $table->getPrimaryKey()->getColumns();
95
96         if (count($pkColumns) != 1) {
97             return false;
98         }
99
100         $column = $table->getColumn($pkColumns[0]);
101
102         if ( ! $column->getAutoincrement()) {
103             return false;
104         }
105
106         $sequenceName      = $this->getShortestName($table->getNamespaceName());
107         $tableName         = $table->getShortestName($table->getNamespaceName());
108         $tableSequenceName = sprintf('%s_%s_seq', $tableName, $pkColumns[0]);
109
110         return $tableSequenceName === $sequenceName;
111     }
112
113     /**
114      * @param Visitor $visitor
115      */
116     public function visit(Visitor $visitor)
117     {
118         $visitor->acceptSequence($this);
119     }
120 }