lib/Doctrine/DBAL/Id/TableGenerator.php
public function nextValue($sequenceName)
    {
        if (isset($this->sequences[$sequenceName])) {
            $value = $this->sequences[$sequenceName]['value'];
            $this->sequences[$sequenceName]['value']++;
            if ($this->sequences[$sequenceName]['value'] >= $this->sequences[$sequenceName]['max']) {
                unset($this->sequences[$sequenceName]);
            }

            return $value;
        }

        $this->conn->beginTransaction();

        try {
            $platform = $this->conn->getDatabasePlatform();
            $sql      = 'SELECT sequence_value, sequence_increment_by'
                . ' FROM ' . $platform->appendLockHint($this->generatorTableName, LockMode::PESSIMISTIC_WRITE)
                . ' WHERE sequence_name = ? ' . $platform->getWriteLockSQL();
            $stmt     = $this->conn->executeQuery($sql, [$sequenceName]);
            $row      = $stmt->fetch(FetchMode::ASSOCIATIVE);

            if ($row !== false) {
                $row = array_change_key_case($row, CASE_LOWER);

                $value = $row['sequence_value'];
                $value++;

                if ($row['sequence_increment_by'] > 1) {
                    $this->sequences[$sequenceName] = [
                        'value' => $value,
                        'max' => $row['sequence_value'] + $row['sequence_increment_by'],
                    ];
                }

                $sql  = 'UPDATE ' . $this->generatorTableName . ' ' .
                       'SET sequence_value = sequence_value + sequence_increment_by ' .
                       'WHERE sequence_name = ? AND sequence_value = ?';
                $rows = $this->conn->executeUpdate($sql, [$sequenceName, $row['sequence_value']]);

                if ($rows !== 1) {
                    throw new DBALException('Race-condition detected while updating sequence. Aborting generation');
                }
            } else {
                $this->conn->insert(
                    $this->generatorTableName,
                    ['sequence_name' => $sequenceName, 'sequence_value' => 1, 'sequence_increment_by' => 1]
                );
                $value = 1;
            }

            $this->conn->commit();
        } catch (Throwable $e) {
            $this->conn->rollBack();
            throw new DBALException('Error occurred while generating ID with TableGenerator, aborted generation: ' . $e->getMessage(), 0, $e);
        }

        return $value;
    }
public function nextValue($sequenceName)
    {
        if (isset($this->sequences[$sequenceName])) {
            $value = $this->sequences[$sequenceName]['value'];
            $this->sequences[$sequenceName]['value']++;
            if ($this->sequences[$sequenceName]['value'] >= $this->sequences[$sequenceName]['max']) {
                unset($this->sequences[$sequenceName]);
            }

            return $value;
        }

        $this->conn->beginTransaction();

        try {
            $platform = $this->conn->getDatabasePlatform();
            $sql      = 'SELECT sequence_value, sequence_increment_by'
                . ' FROM ' . $platform->appendLockHint($this->generatorTableName, LockMode::PESSIMISTIC_WRITE)
                . ' WHERE sequence_name = ? ' . $platform->getWriteLockSQL();
            $stmt     = $this->conn->executeQuery($sql, [$sequenceName]);
            $row      = $stmt->fetch(FetchMode::ASSOCIATIVE);

            if ($row !== false) {
                $row = array_change_key_case($row, CASE_LOWER);

                $value = $row['sequence_value'];
                $value++;

                if ($row['sequence_increment_by'] > 1) {
                    $this->sequences[$sequenceName] = [
                        'value' => $value,
                        'max' => $row['sequence_value'] + $row['sequence_increment_by'],
                    ];
                }

                $sql  = 'UPDATE ' . $this->generatorTableName . ' ' .
                       'SET sequence_value = sequence_value + sequence_increment_by ' .
                       'WHERE sequence_name = ? AND sequence_value = ?';
                $rows = $this->conn->executeUpdate($sql, [$sequenceName, $row['sequence_value']]);

                if ($rows !== 1) {
                    throw new DBALException('Race-condition detected while updating sequence. Aborting generation');
                }
            } else {
                $this->conn->insert(
                    $this->generatorTableName,
                    ['sequence_name' => $sequenceName, 'sequence_value' => 1, 'sequence_increment_by' => 1]
                );
                $value = 1;
            }

            $this->conn->commit();
        } catch (Throwable $e) {
            $this->conn->rollBack();
            throw new DBALException('Error occurred while generating ID with TableGenerator, aborted generation: ' . $e->getMessage(), 0, $e);
        }

        return $value;
    }
public function nextValue ( $ sequenceName ) { if ( isset ( $ this -> sequences [ $ sequenceName ] ) ) { $ value = $ this -> sequences [ $ sequenceName ] [ 'value' ] ; $ this -> sequences [ $ sequenceName ] [ 'value' ] ++ ; if ( $ this -> sequences [ $ sequenceName ] [ 'value' ] >= $ this -> sequences [ $ sequenceName ] [ 'max' ] ) { unset ( $ this -> sequences [ $ sequenceName ] ) ; } return $ value ; } $ this -> conn -> beginTransaction ( ) ; try { $ platform = $ this -> conn -> getDatabasePlatform ( ) ; $ sql = 'SELECT sequence_value, sequence_increment_by' . ' FROM ' . $ platform -> appendLockHint ( $ this -> generatorTableName , LockMode :: PESSIMISTIC_WRITE ) . ' WHERE sequence_name = ? ' . $ platform -> getWriteLockSQL ( ) ; $ stmt = $ this -> conn -> executeQuery ( $ sql , [ $ sequenceName ] ) ; $ row = $ stmt -> fetch ( FetchMode :: ASSOCIATIVE ) ; if ( $ row !== false ) { $ row = array_change_key_case ( $ row , CASE_LOWER ) ; $ value = $ row [ 'sequence_value' ] ; $ value ++ ; if ( $ row [ 'sequence_increment_by' ] > 1 ) { $ this -> sequences [ $ sequenceName ] = [ 'value' => $ value , 'max' => $ row [ 'sequence_value' ] + $ row [ 'sequence_increment_by' ] , ] ; } $ sql = 'UPDATE ' . $ this -> generatorTableName . ' ' . 'SET sequence_value = sequence_value + sequence_increment_by ' . 'WHERE sequence_name = ? AND sequence_value = ?' ; $ rows = $ this -> conn -> executeUpdate ( $ sql , [ $ sequenceName , $ row [ 'sequence_value' ] ] ) ; if ( $ rows !== 1 ) { throw new DBALException ( 'Race-condition detected while updating sequence. Aborting generation' ) ; } } else { $ this -> conn -> insert ( $ this -> generatorTableName , [ 'sequence_name' => $ sequenceName , 'sequence_value' => 1 , 'sequence_increment_by' => 1 ] ) ; $ value = 1 ; } $ this -> conn -> commit ( ) ; } catch ( Throwable $ e ) { $ this -> conn -> rollBack ( ) ; throw new DBALException ( 'Error occurred while generating ID with TableGenerator, aborted generation: ' . $ e -> getMessage ( ) , 0 , $ e ) ; } return $ value ; }
Generates the next unused value for the given sequence name.

@param string $sequenceName

@return int

@throws DBALException
Generates the next unused value for the given sequence name .