| 1 | <?php |
| 2 | /* |
| 3 | * $Id: Mssql.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_Mssql |
| 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_Mssql 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($this->conn->formatter->getSequenceName($seqName), true); |
| 46 | $seqcolName = $this->conn->quoteIdentifier($this->conn->getAttribute(Doctrine::ATTR_SEQCOL_NAME), true); |
| 47 | |
| 48 | |
| 49 | if ($this->checkSequence($sequenceName)) { |
| 50 | $query = 'SET IDENTITY_INSERT ' . $sequenceName . ' OFF ' |
| 51 | . 'INSERT INTO ' . $sequenceName . ' DEFAULT VALUES'; |
| 52 | } else { |
| 53 | $query = 'INSERT INTO ' . $sequenceName . ' (' . $seqcolName . ') VALUES (0)'; |
| 54 | } |
| 55 | |
| 56 | try { |
| 57 | |
| 58 | $this->conn->exec($query); |
| 59 | |
| 60 | } catch(Doctrine_Connection_Exception $e) { |
| 61 | if ($onDemand && $e->getPortableCode() == Doctrine::ERR_NOSUCHTABLE) { |
| 62 | // Since we are creating the sequence on demand |
| 63 | // we know the first id = 1 so initialize the |
| 64 | // sequence at 2 |
| 65 | try { |
| 66 | $result = $this->conn->export->createSequence($seqName, 2); |
| 67 | } catch(Doctrine_Exception $e) { |
| 68 | throw new Doctrine_Sequence_Exception('on demand sequence ' . $seqName . ' could not be created'); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * This could actually be a table that starts at 18.. oh well.. |
| 73 | * we will keep the fallback to return 1 in case we skip this.. which |
| 74 | * should really not happen.. otherwise the returned values is biased. |
| 75 | */ |
| 76 | if ($this->checkSequence($seqName)) { |
| 77 | return $this->lastInsertId($seqName); |
| 78 | } |
| 79 | |
| 80 | return 1; |
| 81 | } |
| 82 | throw $e; |
| 83 | } |
| 84 | |
| 85 | $value = $this->lastInsertId($sequenceName); |
| 86 | |
| 87 | if (is_numeric($value)) { |
| 88 | $query = 'DELETE FROM ' . $sequenceName . ' WHERE ' . $seqcolName . ' < ' . $value; |
| 89 | |
| 90 | try { |
| 91 | $this->conn->exec($query); |
| 92 | } catch (Doctrine_Connection_Exception $e) { |
| 93 | throw new Doctrine_Sequence_Exception('Could not delete previous sequence from ' . $sequenceName . |
| 94 | ' at ' . __FILE__ . ' in ' . __FUNCTION__ . ' with the message: ' . |
| 95 | $e->getMessage()); |
| 96 | } |
| 97 | } |
| 98 | return $value; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Checks if there's a sequence that exists. |
| 103 | * |
| 104 | * @param string $seqName The sequence name to verify. |
| 105 | * @return bool $tableExists The value if the table exists or not |
| 106 | * @access private |
| 107 | */ |
| 108 | public function checkSequence($seqName) |
| 109 | { |
| 110 | $query = 'SELECT COUNT(1) FROM ' . $seqName; |
| 111 | try { |
| 112 | $this->conn->execute($query); |
| 113 | } catch (Doctrine_Connection_Exception $e) { |
| 114 | if ($e->getPortableCode() == Doctrine::ERR_NOSUCHTABLE) { |
| 115 | return false; |
| 116 | } |
| 117 | } |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Returns the autoincrement ID if supported or $id or fetches the current |
| 123 | * ID in a sequence called: $table.(empty($field) ? '' : '_'.$field) |
| 124 | * |
| 125 | * @param string name of the table into which a new row was inserted |
| 126 | * @param string name of the field into which a new row was inserted |
| 127 | */ |
| 128 | public function lastInsertId($table = null, $field = null) |
| 129 | { |
| 130 | $serverInfo = $this->conn->getServerVersion(); |
| 131 | if (is_array($serverInfo) |
| 132 | && ! is_null($serverInfo['major']) |
| 133 | && $serverInfo['major'] >= 8) { |
| 134 | |
| 135 | $query = 'SELECT SCOPE_IDENTITY()'; |
| 136 | |
| 137 | } else { |
| 138 | $query = 'SELECT @@IDENTITY'; |
| 139 | } |
| 140 | |
| 141 | return $this->conn->fetchOne($query); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Returns the current id of a sequence |
| 146 | * |
| 147 | * @param string $seqName name of the sequence |
| 148 | * |
| 149 | * @return integer current id in the given sequence |
| 150 | */ |
| 151 | public function currId($seqName) |
| 152 | { |
| 153 | $this->warnings[] = 'database does not support getting current |
| 154 | sequence value, the sequence value was incremented'; |
| 155 | return $this->nextId($seqName); |
| 156 | } |
| 157 | } |