# Source for file Db2.php

Documentation is available at Db2.php

1. <?php
2. /*
3. * $Id: Db2.php 1722 2007-06-17 17:50:05Z 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_Db2
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: 1722 $
32. */
33. class Doctrine_Sequence_Db2 extends Doctrine_Sequence
34. {
35. /**
36. * Return the most recent value from the specified sequence in the database.
37. * This is supported only on RDBMS brands that support sequences
38. * (e.g. Oracle, PostgreSQL, DB2). Other RDBMS brands return null.
39. *
40. * @param string $sequenceName
41. * @return integer
42. * @throws Doctrine_Adapter_Db2_Exception
43. */
44. public function lastSequenceId($sequenceName)
45. {
46. $sql = 'SELECT PREVVAL FOR '
47. . $this->quoteIdentifier($this->conn->formatter->getSequenceName($sequenceName))
48. . ' AS VAL FROM SYSIBM.SYSDUMMY1';
50. $stmt = $this->query($sql);
51. $result = $stmt->fetchAll(Doctrine::FETCH_ASSOC);
52. if ($result) {
53. return $result[0]['VAL'];
54. } else {
55. return null;
56. }
57. }
59. /**
60. * Generate a new value from the specified sequence in the database, and return it.
61. * This is supported only on RDBMS brands that support sequences
62. * (e.g. Oracle, PostgreSQL, DB2). Other RDBMS brands return null.
63. *
64. * @param string $sequenceName
65. * @return integer
66. * @throws Doctrine_Adapter_Db2_Exception
67. */
68. public function nextSequenceId($sequenceName)
69. {
70. $this->_connect();
71. $sql = 'SELECT NEXTVAL FOR '
72. . $this->quoteIdentifier($this->conn->formatter->getSequenceName($sequenceName))
73. . ' AS VAL FROM SYSIBM.SYSDUMMY1';
74. $stmt = $this->query($sql);
75. $result = $stmt->fetchAll(Doctrine::FETCH_ASSOC);
76. if ($result) {
77. return $result[0]['VAL'];
78. } else {
79. return null;
80. }
81. }
83. /**
84. * Gets the last ID generated automatically by an IDENTITY/AUTOINCREMENT column.
85. *
86. * As a convention, on RDBMS brands that support sequences
87. * (e.g. Oracle, PostgreSQL, DB2), this method forms the name of a sequence
88. * from the arguments and returns the last id generated by that sequence.
89. * On RDBMS brands that support IDENTITY/AUTOINCREMENT columns, this method
90. * returns the last value generated for such a column, and the table name
91. * argument is disregarded.
92. *
93. * The IDENTITY_VAL_LOCAL() function gives the last generated identity value
94. * in the current process, even if it was for a GENERATED column.
95. *
96. * @param string $tableName OPTIONAL
97. * @param string $primaryKey OPTIONAL
98. * @return integer
99. * @throws Doctrine_Adapter_Db2_Exception
100. */
101. public function lastInsertId($tableName = null, $primaryKey = null)
102. {
103. $this->_connect();
105. if ($tableName !== null) {
106. $sequenceName = $tableName;
107. if ($primaryKey) {
108. $sequenceName .= "_$primaryKey";
109. }
110. $sequenceName .= '_seq';
111. return $this->lastSequenceId($sequenceName);
112. }
114. $sql = 'SELECT IDENTITY_VAL_LOCAL() AS VAL FROM SYSIBM.SYSDUMMY1';
115. $stmt = $this->query($sql);
116. $result = $stmt->fetchAll(Doctrine::FETCH_ASSOC);
117. if ($result) {
118. return $result[0]['VAL'];
119. } else {
120. return null;
121. }
122. }
123. }