# Source for file Mssql.php

Documentation is available at Mssql.php

1. <?php
2. /*
3. * $Id: Mssql.php 1934 2007-07-05 22:42:32Z zYne $
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.phpdoctrine.com>.
20. */
21. Doctrine::autoload('Doctrine_Sequence');
22. /**
23. * Doctrine_Sequence_Mssql
24. *
25. * @package Doctrine
26. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
27. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
28. * @category Object Relational Mapping
29. * @link www.phpdoctrine.com
30. * @since 1.0
31. * @version $Revision: 1934 $
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::ATTR_SEQCOL_NAME), true);
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. }
56. try {
58. $this->conn->exec($query);
60. } catch(Doctrine_Connection_Exception $e) {
61. if ($onDemand && $e->getPortableCode() == Doctrine::ERR_NOSUCHTABLE) {
62. // Since we are creating the sequence on demand
63. // we know the first id = 1 so initialize the
64. // sequence at 2
65. try {
66. $result = $this->conn->export->createSequence($seqName, 2);
67. } catch(Doctrine_Exception $e) {
68. throw new Doctrine_Sequence_Exception('on demand sequence ' . $seqName . ' could not be created');
69. }
71. /**
72. * This could actually be a table that starts at 18.. oh well..
73. * we will keep the fallback to return 1 in case we skip this.. which
74. * should really not happen.. otherwise the returned values is biased.
75. */
76. if ($this->checkSequence($seqName)) {
77. return $this->lastInsertId($seqName);
78. }
80. return 1;
81. }
82. throw $e;
83. }
85. $value = $this->lastInsertId($sequenceName);
87. if (is_numeric($value)) {
88. $query = 'DELETE FROM ' . $sequenceName . ' WHERE ' . $seqcolName . ' < ' . $value;
90. try {
91. $this->conn->exec($query);
92. } catch (Doctrine_Connection_Exception $e) {
93. throw new Doctrine_Sequence_Exception('Could not delete previous sequence from ' . $sequenceName .
94. ' at ' . __FILE__ . ' in ' . __FUNCTION__ . ' with the message: ' .
95. $e->getMessage());
96. }
97. }
98. return $value;
99. }
100. /**
101. * Checks if there's a sequence that exists.
102. *
103. * @param string $seqName The sequence name to verify.
104. * @return bool $tableExists The value if the table exists or not
105. * @access private
106. */
107. public function checkSequence($seqName)
108. {
109. $query = 'SELECT COUNT(1) FROM ' . $seqName;
110. try {
111. $this->conn->execute($query);
112. } catch (Doctrine_Connection_Exception $e) {
113. if ($e->getPortableCode() == Doctrine::ERR_NOSUCHTABLE) {
114. return false;
115. }
116. }
117. return true;
118. }
120. /**
121. * Returns the autoincrement ID if supported or $id or fetches the current
122. * ID in a sequence called: $table.(empty($field) ? '' : '_'.$field)
123. *
124. * @param string name of the table into which a new row was inserted
125. * @param string name of the field into which a new row was inserted
126. */
127. public function lastInsertId($table = null, $field = null)
128. {
129. $serverInfo = $this->conn->getServerVersion();
130. if (is_array($serverInfo)
131. && ! is_null($serverInfo['major'])
132. && $serverInfo['major'] >= 8) {
134. $query = 'SELECT SCOPE_IDENTITY()';
136. } else {
137. $query = 'SELECT @@IDENTITY';
138. }
140. return $this->conn->fetchOne($query);
141. }
142. /**
143. * Returns the current id of a sequence
144. *
145. * @param string $seqName name of the sequence
146. *
147. * @return integer current id in the given sequence
148. */
149. public function currId($seqName)
150. {
151. $this->warnings[] = 'database does not support getting current
152. sequence value, the sequence value was incremented';
153. return $this->nextId($seqName);
154. }
155. }