3
* $Id: Sqlite.php 4252 2008-04-19 07:37:53Z jwage $
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.
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>.
23
* Doctrine_Sequence_Sqlite
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
31
* @version $Revision: 4252 $
33
class Doctrine_Sequence_Sqlite extends Doctrine_Sequence
36
* Returns the next free id of a sequence
38
* @param string $seqName name of the sequence
39
* @param bool $onDemand when true missing sequences are automatic created
41
* @return integer next id in the given sequence
43
public function nextId($seqName, $onDemand = true)
45
$sequenceName = $this->conn->quoteIdentifier($this->conn->formatter->getSequenceName($seqName), true);
46
$seqcolName = $this->conn->quoteIdentifier($this->conn->getAttribute(Doctrine::ATTR_SEQCOL_NAME), true);
48
$query = 'INSERT INTO ' . $sequenceName . ' (' . $seqcolName . ') VALUES (NULL)';
52
$this->conn->exec($query);
54
} catch(Doctrine_Connection_Exception $e) {
55
if ($onDemand && $e->getPortableCode() == Doctrine::ERR_NOSUCHTABLE) {
56
// Since we are creating the sequence on demand
57
// we know the first id = 1 so initialize the
61
$result = $this->conn->export->createSequence($seqName, 2);
62
} catch(Doctrine_Exception $e) {
63
throw new Doctrine_Sequence_Exception('on demand sequence ' . $seqName . ' could not be created');
65
// First ID of a newly created sequence is 1
71
$value = $this->conn->getDbh()->lastInsertId();
73
if (is_numeric($value)) {
74
$query = 'DELETE FROM ' . $sequenceName . ' WHERE ' . $seqcolName . ' < ' . $value;
76
$this->conn->exec($query);
78
TODO: is the following needed ?
79
$this->warnings[] = 'nextID: could not delete previous sequence table values from '.$seq_name;
86
* Returns the autoincrement ID if supported or $id or fetches the current
87
* ID in a sequence called: $table.(empty($field) ? '' : '_'.$field)
89
* @param string name of the table into which a new row was inserted
90
* @param string name of the field into which a new row was inserted
91
* @return integer|boolean
93
public function lastInsertId($table = null, $field = null)
95
return $this->conn->getDbh()->lastInsertId();
99
* Returns the current id of a sequence
101
* @param string $seqName name of the sequence
103
* @return integer current id in the given sequence
105
public function currId($seqName)
107
$sequenceName = $this->conn->quoteIdentifier($this->conn->formatter->getSequenceName($seqName), true);
108
$seqcolName = $this->conn->quoteIdentifier($this->conn->getAttribute(Doctrine::ATTR_SEQCOL_NAME), true);
110
$query = 'SELECT MAX(' . $seqcolName . ') FROM ' . $sequenceName;
112
return (int) $this->conn->fetchOne($query);
b'\\ No newline at end of file'