Skip to content

Loading…

DDC-450: Add TableGenerator Implementation #4952

Open
doctrinebot opened this Issue · 4 comments

1 participant

@doctrinebot

Jira issue originally created by user @beberlei:

The TableGenerator Id Generator is not yet implemented, here is some code i came up with:

class TableGenerator extends AbstractIdGenerator
{
    private $_tableName;
    private $_sequenceName;
    private $_allocationSize;
    private $_nextValue;
    private $_maxValue;

    public function **construct($tableName, $sequenceName = 'default', $allocationSize = 10)
    {
        $this->_tableName = $tableName;
        $this->_sequenceName = $sequenceName;
        $this->_allocationSize = $allocationSize;
    }

    public function generate(EntityManager $em, $entity)
    {
        if ($this->*maxValue === null || $this->_nextValue == $this->*maxValue) {
            // Allocate new values
            $conn = $em->getConnection();
            if ($conn->getTransactionNestingLevel() == 0) {

                // use select for update
                $sql = $conn->getDatabasePlatform()->getTableHiLoCurrentValSql($this->*tableName, $this->*sequenceName);
                $currentLevel = $conn->fetchColumn($sql);
                if ($currentLevel != null) {
                    $this->_nextValue = $currentLevel;
                    $this->*maxValue = $this->_nextValue <ins> $this->*allocationSize;

                    $updateSql = $conn->getDatabasePlatform()->getTableHiLoUpdateNextValSql(
                        $this->*tableName, $this->_sequenceName, $this->*allocationSize
                    );

                    if ($conn->executeUpdate($updateSql, array(1 => $currentLevel, 2 => $currentLevel</ins>1)) !== 1) {
                        // no affected rows, concurrency issue, throw exception
                    }
                } else {
                    // no current level returned, TableGenerator seems to be broken, throw exception
                }
            } else {
                // only table locks help here, implement this or throw exception?
                // or do we want to work with table locks exclusively?
            }
        }
        return $this->_nextValue<ins></ins>;
    }
}
@doctrinebot

Comment created by @guilhermeblanco:

Already merged into core.

@doctrinebot

Comment created by @beberlei:

But it is not enabled yet :-) Plus we need tests to verify this works in high concurrency enviroments and does not pass the same id twice.

Furthermore the DAtabase Platform Methods are completly missing. No implementations yet.

@doctrinebot

Comment created by @beberlei:

Schema-Tool support is also missing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.