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: Mysql.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_Mysql
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_Mysql 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($seqName, true);
46
        $seqcolName    = $this->conn->quoteIdentifier($this->conn->getAttribute(Doctrine_Core::ATTR_SEQCOL_NAME), true);
47
        $query         = 'INSERT INTO ' . $sequenceName . ' (' . $seqcolName . ') VALUES (NULL)';
48
 
49
        try {
50
            $this->conn->exec($query);
51
        } catch(Doctrine_Connection_Exception $e) {
52
            if ($onDemand && $e->getPortableCode() == Doctrine_Core::ERR_NOSUCHTABLE) {
53
                // Since we are creating the sequence on demand
54
                // we know the first id = 1 so initialize the
55
                // sequence at 2
56
                try {
57
                    $result = $this->conn->export->createSequence($seqName, 2);
58
                } catch(Doctrine_Exception $e) {
59
                    throw new Doctrine_Sequence_Exception('on demand sequence ' . $seqName . ' could not be created');
60
                }
61
 
62
                // First ID of a newly created sequence is 1
63
                return 1;
64
            } else {
65
                throw new Doctrine_Sequence_Exception('sequence ' .$seqName . ' does not exist');
66
            }
67
        }
68
 
69
        $value = $this->lastInsertId();
70
 
71
        if (is_numeric($value)) {
72
            $query = 'DELETE FROM ' . $sequenceName . ' WHERE ' . $seqcolName . ' < ' . $value;
73
            $this->conn->exec($query);
74
        }
75
 
76
        return $value;
77
    }
78
 
79
    /**
80
     * Returns the autoincrement ID if supported or $id or fetches the current
81
     * ID in a sequence called: $table.(empty($field) ? '' : '_'.$field)
82
     *
83
     * @param string  name of the table into which a new row was inserted
84
     * @param string  name of the field into which a new row was inserted
85
     * @return integer|boolean
86
     */
87
    public function lastInsertId($table = null, $field = null)
88
    {
89
        return $this->conn->getDbh()->lastInsertId();
90
    }
91
 
92
    /**
93
     * Returns the current id of a sequence
94
     *
95
     * @param string $seqName   name of the sequence
96
     *
97
     * @return integer          current id in the given sequence
98
     */
99
    public function currId($seqName)
100
    {
101
        $sequenceName   = $this->conn->quoteIdentifier($seqName, true);
102
        $seqcolName     = $this->conn->quoteIdentifier($this->conn->getAttribute(Doctrine_Core::ATTR_SEQCOL_NAME), true);
103
        $query          = 'SELECT MAX(' . $seqcolName . ') FROM ' . $sequenceName;
104
 
105
        return (int) $this->conn->fetchOne($query);
106
    }
107
}