Skip to content

Commit

Permalink
Truncate sequence and trigger identifiers to the maximum of 30 chars …
Browse files Browse the repository at this point in the history
…too.
  • Loading branch information
yunosh committed Jan 22, 2014
1 parent 0073fb7 commit 636ddd6
Showing 1 changed file with 37 additions and 21 deletions.
58 changes: 37 additions & 21 deletions framework/Db/lib/Horde/Db/Adapter/Oracle/Schema.php
Expand Up @@ -331,14 +331,14 @@ public function dropTable($name)
try {
$this->execute(sprintf(
'DROP SEQUENCE %s',
$this->quoteColumnName($prefix . '_seq')
$this->quoteColumnName($this->_truncateTo30($prefix . '_seq'))
));
} catch (Horde_Db_Exception $e) {
}
try {
$this->execute(sprintf(
'DROP TRIGGER %s',
$this->quoteColumnName($prefix . '_trig')
$this->quoteColumnName($this->_truncateTo30($prefix . '_trig'))
));
} catch (Horde_Db_Exception $e) {
}
Expand Down Expand Up @@ -510,17 +510,18 @@ public function createAutoincrementTrigger($tableName, $columnName)
$this->execute('CREATE TABLE horde_db_autoincrement (id INTEGER)');
$this->execute('INSERT INTO horde_db_autoincrement (id) VALUES (0)');
}
$sequence = $this->_truncateTo30($id . '_seq');
$this->execute(sprintf(
'CREATE SEQUENCE %s_seq',
$id
'CREATE SEQUENCE %s',
$sequence
));
$this->execute(sprintf(
'CREATE OR REPLACE TRIGGER %s_trig BEFORE INSERT ON %s FOR EACH ROW DECLARE increment INTEGER; BEGIN SELECT %s_seq.NEXTVAL INTO :NEW.%s FROM dual; SELECT %s_seq.CURRVAL INTO increment FROM dual; UPDATE horde_db_autoincrement SET id = increment; END;',
$id,
'CREATE OR REPLACE TRIGGER %s BEFORE INSERT ON %s FOR EACH ROW DECLARE increment INTEGER; BEGIN SELECT %s.NEXTVAL INTO :NEW.%s FROM dual; SELECT %s.CURRVAL INTO increment FROM dual; UPDATE horde_db_autoincrement SET id = increment; END;',
$this->_truncateTo30($id . '_trig'),
$tableName,
$id,
$sequence,
$columnName,
$id
$sequence
));
}

Expand Down Expand Up @@ -610,19 +611,7 @@ public function removeIndex($tableName, $options = array())
public function indexName($tableName, $options = array())
{
$index = parent::indexName($tableName, $options);
if (strlen($index) > 30) {
$index = implode(
'_',
array_map(
function($t)
{
return substr($t, 0, 3);
},
explode('_', $index)
)
);
}
return substr($index, 0, 30);
return $this->_truncateTo30($index);
}

/**
Expand Down Expand Up @@ -713,4 +702,31 @@ protected function _clearTableCache($tableName)
parent::_clearTableCache($tableName);
$this->_cache->set('tables/primarykeys/' . $tableName, '');
}

/**
* Truncates an indentifier to 30 chars.
*
* To avoid collisions, the identifier is split up by underscores and the
* parts truncated to 3 characters first.
*
* @param string $name An identifier.
*
* @return string The truncated identifier.
*/
protected function _truncateTo30($name)
{
if (strlen($name) > 30) {
$name = implode(
'_',
array_map(
function($t)
{
return substr($t, 0, 3);
},
explode('_', $name)
)
);
}
return substr($name, 0, 30);
}
}

0 comments on commit 636ddd6

Please sign in to comment.