| 1 |
lars |
1 |
<?php
|
|
|
2 |
/*
|
|
|
3 |
* $Id: Mssql.php 7490 2010-03-29 19:53:27Z jwage $
|
|
|
4 |
*
|
|
|
5 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
6 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
7 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
8 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
9 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
10 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
11 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
12 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
13 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
14 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
15 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
16 |
*
|
|
|
17 |
* This software consists of voluntary contributions made by many individuals
|
|
|
18 |
* and is licensed under the LGPL. For more information, see
|
|
|
19 |
* <http://www.doctrine-project.org>.
|
|
|
20 |
*/
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* Doctrine_Sequence_Mssql
|
|
|
24 |
*
|
|
|
25 |
* @package Doctrine
|
|
|
26 |
* @subpackage Sequence
|
|
|
27 |
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
|
|
28 |
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
29 |
* @link www.doctrine-project.org
|
|
|
30 |
* @since 1.0
|
|
|
31 |
* @version $Revision: 7490 $
|
|
|
32 |
*/
|
|
|
33 |
class Doctrine_Sequence_Mssql extends Doctrine_Sequence
|
|
|
34 |
{
|
|
|
35 |
/**
|
|
|
36 |
* Returns the next free id of a sequence
|
|
|
37 |
*
|
|
|
38 |
* @param string $seqName name of the sequence
|
|
|
39 |
* @param bool when true missing sequences are automatic created
|
|
|
40 |
*
|
|
|
41 |
* @return integer next id in the given sequence
|
|
|
42 |
*/
|
|
|
43 |
public function nextId($seqName, $onDemand = true)
|
|
|
44 |
{
|
|
|
45 |
$sequenceName = $this->conn->quoteIdentifier($this->conn->formatter->getSequenceName($seqName), true);
|
|
|
46 |
$seqcolName = $this->conn->quoteIdentifier($this->conn->getAttribute(Doctrine_Core::ATTR_SEQCOL_NAME), true);
|
|
|
47 |
|
|
|
48 |
|
|
|
49 |
if ($this->checkSequence($sequenceName)) {
|
|
|
50 |
$query = 'SET IDENTITY_INSERT ' . $sequenceName . ' OFF '
|
|
|
51 |
. 'INSERT INTO ' . $sequenceName . ' DEFAULT VALUES';
|
|
|
52 |
} else {
|
|
|
53 |
$query = 'INSERT INTO ' . $sequenceName . ' (' . $seqcolName . ') VALUES (0)';
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
try {
|
|
|
57 |
$this->conn->exec($query);
|
|
|
58 |
} catch(Doctrine_Connection_Exception $e) {
|
|
|
59 |
if ($onDemand && $e->getPortableCode() == Doctrine_Core::ERR_NOSUCHTABLE) {
|
|
|
60 |
// Since we are creating the sequence on demand
|
|
|
61 |
// we know the first id = 1 so initialize the
|
|
|
62 |
// sequence at 2
|
|
|
63 |
try {
|
|
|
64 |
$result = $this->conn->export->createSequence($seqName, 2);
|
|
|
65 |
} catch(Doctrine_Exception $e) {
|
|
|
66 |
throw new Doctrine_Sequence_Exception('on demand sequence ' . $seqName . ' could not be created');
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
/**
|
|
|
70 |
* This could actually be a table that starts at 18.. oh well..
|
|
|
71 |
* we will keep the fallback to return 1 in case we skip this.. which
|
|
|
72 |
* should really not happen.. otherwise the returned values is biased.
|
|
|
73 |
*/
|
|
|
74 |
if ($this->checkSequence($seqName)) {
|
|
|
75 |
return $this->lastInsertId($seqName);
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
return 1;
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
throw $e;
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
$value = $this->lastInsertId($sequenceName);
|
|
|
85 |
|
|
|
86 |
if (is_numeric($value)) {
|
|
|
87 |
$query = 'DELETE FROM ' . $sequenceName . ' WHERE ' . $seqcolName . ' < ' . $value;
|
|
|
88 |
|
|
|
89 |
try {
|
|
|
90 |
$this->conn->exec($query);
|
|
|
91 |
} catch (Doctrine_Connection_Exception $e) {
|
|
|
92 |
throw new Doctrine_Sequence_Exception(
|
|
|
93 |
'Could not delete previous sequence from ' .
|
|
|
94 |
$sequenceName . ' at ' . __FILE__ . ' in ' .
|
|
|
95 |
__FUNCTION__ . ' with the message: ' . $e->getMessage()
|
|
|
96 |
);
|
|
|
97 |
}
|
|
|
98 |
}
|
|
|
99 |
return $value;
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
/**
|
|
|
103 |
* Checks if there's a sequence that exists.
|
|
|
104 |
*
|
|
|
105 |
* @param string $seqName The sequence name to verify.
|
|
|
106 |
* @return bool $tableExists The value if the table exists or not
|
|
|
107 |
* @access private
|
|
|
108 |
*/
|
|
|
109 |
public function checkSequence($seqName)
|
|
|
110 |
{
|
|
|
111 |
$query = 'SELECT COUNT(1) FROM ' . $seqName;
|
|
|
112 |
try {
|
|
|
113 |
$this->conn->execute($query);
|
|
|
114 |
} catch (Doctrine_Connection_Exception $e) {
|
|
|
115 |
if ($e->getPortableCode() == Doctrine_Core::ERR_NOSUCHTABLE) {
|
|
|
116 |
return false;
|
|
|
117 |
}
|
|
|
118 |
}
|
|
|
119 |
return true;
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
/**
|
|
|
123 |
* Returns the autoincrement ID if supported or $id or fetches the current
|
|
|
124 |
* ID in a sequence called: $table.(empty($field) ? '' : '_'.$field)
|
|
|
125 |
*
|
|
|
126 |
* @param string name of the table into which a new row was inserted
|
|
|
127 |
* @param string name of the field into which a new row was inserted
|
|
|
128 |
*/
|
|
|
129 |
public function lastInsertId($table = null, $field = null)
|
|
|
130 |
{
|
|
|
131 |
$serverInfo = $this->conn->getServerVersion();
|
|
|
132 |
if (is_array($serverInfo)
|
|
|
133 |
&& ! is_null($serverInfo['major'])
|
|
|
134 |
&& $serverInfo['major'] >= 8) {
|
|
|
135 |
|
|
|
136 |
if (isset($table))
|
|
|
137 |
{
|
|
|
138 |
$query = 'SELECT IDENT_CURRENT(\'' . $this->conn->quoteIdentifier($table) . '\')';
|
|
|
139 |
} else {
|
|
|
140 |
$query = 'SELECT SCOPE_IDENTITY()';
|
|
|
141 |
}
|
|
|
142 |
} else {
|
|
|
143 |
$query = 'SELECT @@IDENTITY';
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
return (string) floor((float) $this->conn->fetchOne($query));
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
/**
|
|
|
150 |
* Returns the current id of a sequence
|
|
|
151 |
*
|
|
|
152 |
* @param string $seqName name of the sequence
|
|
|
153 |
*
|
|
|
154 |
* @return integer current id in the given sequence
|
|
|
155 |
*/
|
|
|
156 |
public function currId($seqName)
|
|
|
157 |
{
|
|
|
158 |
$this->warnings[] = 'database does not support getting current
|
|
|
159 |
sequence value, the sequence value was incremented';
|
|
|
160 |
return $this->nextId($seqName);
|
|
|
161 |
}
|
|
|
162 |
}
|