Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/*
3
 *  $Id: Db2.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_Db2
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_Db2 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
     * @throws Doctrine_Sequence_Exception
43
     */
44
    public function nextId($seqName, $ondemand = true)
45
    {
46
        $sequenceName = $this->conn->quoteIdentifier($this->conn->formatter->getSequenceName($seqName), true);
47
        $query = 'SELECT NEXTVAL FOR ' . $sequenceName . ' AS VAL FROM SYSIBM.SYSDUMMY1';
48
 
49
        try {
50
            $result = $this->conn->fetchOne($query);
51
            $result = ($result) ? $result['VAL'] : null;
52
        } catch(Doctrine_Connection_Exception $e) {
53
            if ($onDemand && $e->getPortableCode() == Doctrine_Core::ERR_NOSUCHTABLE) {
54
                try {
55
                    $result = $this->conn->export->createSequence($seqName);
56
                } catch(Doctrine_Exception $e) {
57
                    throw new Doctrine_Sequence_Exception('on demand sequence ' . $seqName . ' could not be created');
58
                }
59
 
60
                return $this->nextId($seqName, false);
61
            } else {
62
                throw new Doctrine_Sequence_Exception('sequence ' .$seqName . ' does not exist');
63
            }
64
        }
65
        return $result;
66
    }
67
 
68
    /**
69
     * Return the most recent value from the specified sequence in the database.
70
     * This is supported only on RDBMS brands that support sequences
71
     * (e.g. Oracle, PostgreSQL, DB2).  Other RDBMS brands return null.
72
     *
73
     * @param string $sequenceName
74
     * @return integer
75
     * @throws Doctrine_Adapter_Db2_Exception
76
     */
77
    public function currId($sequenceName)
78
    {
79
        $sql = 'SELECT PREVVAL FOR '
80
             . $this->quoteIdentifier($this->conn->formatter->getSequenceName($sequenceName))
81
             . ' AS VAL FROM SYSIBM.SYSDUMMY1';
82
 
83
        $stmt   = $this->query($sql);
84
        $result = $stmt->fetchAll(Doctrine_Core::FETCH_ASSOC);
85
        if ($result) {
86
            return $result[0]['VAL'];
87
        } else {
88
            return null;
89
        }
90
    }
91
 
92
    /**
93
     * Gets the last ID generated automatically by an IDENTITY/AUTOINCREMENT column.
94
     *
95
     * As a convention, on RDBMS brands that support sequences
96
     * (e.g. Oracle, PostgreSQL, DB2), this method forms the name of a sequence
97
     * from the arguments and returns the last id generated by that sequence.
98
     * On RDBMS brands that support IDENTITY/AUTOINCREMENT columns, this method
99
     * returns the last value generated for such a column, and the table name
100
     * argument is disregarded.
101
     *
102
     * The IDENTITY_VAL_LOCAL() function gives the last generated identity value
103
     * in the current process, even if it was for a GENERATED column.
104
     *
105
     * @param string $tableName OPTIONAL
106
     * @param string $primaryKey OPTIONAL
107
     * @return integer
108
     * @throws Doctrine_Adapter_Db2_Exception
109
     */
110
    public function lastInsertId($tableName = null, $primaryKey = null)
111
    {
112
        $this->_connect();
113
 
114
        if ($tableName !== null) {
115
            $sequenceName = $tableName;
116
            if ($primaryKey) {
117
                $sequenceName .= "_$primaryKey";
118
            }
119
            $sequenceName .= '_seq';
120
            return $this->lastSequenceId($sequenceName);
121
        }
122
 
123
        $sql = 'SELECT IDENTITY_VAL_LOCAL() AS VAL FROM SYSIBM.SYSDUMMY1';
124
        $stmt = $this->query($sql);
125
        $result = $stmt->fetchAll(Doctrine_Core::FETCH_ASSOC);
126
        if ($result) {
127
            return $result[0]['VAL'];
128
        } else {
129
            return null;
130
        }
131
    }
132
}