1 |
<?php
|
2 |
/*
|
3 |
* $Id: Pgsql.php 2963 2007-10-21 06:23:59Z Jonathan.Wage $
|
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.org>.
|
20 |
*/
|
21 |
Doctrine::autoload('Doctrine_Sequence');
|
22 |
/**
|
23 |
* Doctrine_Sequence_Pgsql
|
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.phpdoctrine.org
|
30 |
* @since 1.0
|
31 |
* @version $Revision: 2963 $
|
32 |
*/
|
33 |
class Doctrine_Sequence_Pgsql 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 onDemand 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 |
|
47 |
$query = "SELECT NEXTVAL('" . $sequenceName . "')";
|
48 |
try {
|
49 |
$result = (int) $this->conn->fetchOne($query);
|
50 |
} catch(Doctrine_Connection_Exception $e) {
|
51 |
if ($onDemand && $e->getPortableCode() == Doctrine::ERR_NOSUCHTABLE) {
|
52 |
|
53 |
try {
|
54 |
$result = $this->conn->export->createSequence($seqName);
|
55 |
} catch(Doctrine_Exception $e) {
|
56 |
throw new Doctrine_Sequence_Exception('on demand sequence ' . $seqName . ' could not be created');
|
57 |
}
|
58 |
return $this->nextId($seqName, false);
|
59 |
}
|
60 |
}
|
61 |
return $result;
|
62 |
}
|
63 |
|
64 |
/**
|
65 |
* lastInsertId
|
66 |
*
|
67 |
* Returns the autoincrement ID if supported or $id or fetches the current
|
68 |
* ID in a sequence called: $table.(empty($field) ? '' : '_'.$field)
|
69 |
*
|
70 |
* @param string name of the table into which a new row was inserted
|
71 |
* @param string name of the field into which a new row was inserted
|
72 |
* @return integer the autoincremented id
|
73 |
*/
|
74 |
public function lastInsertId($table = null, $field = null)
|
75 |
{
|
76 |
$seqName = $table . (empty($field) ? '' : '_' . $field);
|
77 |
$sequenceName = $this->conn->quoteIdentifier($this->conn->formatter->getSequenceName($seqName), true);
|
78 |
|
79 |
return (int) $this->conn->fetchOne("SELECT CURRVAL('" . $sequenceName . "')");
|
80 |
}
|
81 |
|
82 |
/**
|
83 |
* Returns the current id of a sequence
|
84 |
*
|
85 |
* @param string $seqName name of the sequence
|
86 |
*
|
87 |
* @return integer current id in the given sequence
|
88 |
*/
|
89 |
public function currId($seqName)
|
90 |
{
|
91 |
$sequenceName = $this->conn->quoteIdentifier($this->conn->formatter->getSequenceName($seqName), true);
|
92 |
return (int) $this->conn->fetchOne('SELECT last_value FROM ' . $sequenceName);
|
93 |
}
|
94 |
} |