- Модуль: main
- Путь к файлу: ~/bitrix/modules/main/lib/db/oracleconnection.php
- Класс: BitrixMainDBOracleConnection
- Вызов: OracleConnection::createTable
public function createTable($tableName, $fields, $primary = array(), $autoincrement = array())
{
$sql = 'CREATE TABLE '.$this->getSqlHelper()->quote($tableName).' (';
$sqlFields = array();
foreach ($fields as $columnName => $field)
{
if (!($field instanceof ScalarField))
{
throw new ArgumentException(sprintf(
'Field `%s` should be an EntityScalarField instance', $columnName
));
}
$realColumnName = $field->getColumnName();
$sqlFields[] = $this->getSqlHelper()->quote($realColumnName)
. ' ' . $this->getSqlHelper()->getColumnTypeByField($field)
. ' ' . (in_array($columnName, $primary, true) ? 'NOT NULL' : 'NULL')
;
}
$sql .= join(', ', $sqlFields);
if (!empty($primary))
{
foreach ($primary as &$primaryColumn)
{
$realColumnName = $fields[$primaryColumn]->getColumnName();
$primaryColumn = $this->getSqlHelper()->quote($realColumnName);
}
$sql .= ', PRIMARY KEY('.join(', ', $primary).')';
}
$sql .= ')';
$this->query($sql);
// autoincrement field
if (!empty($autoincrement))
{
foreach ($autoincrement as $autoincrementColumn)
{
$autoincrementColumn = $fields[$autoincrementColumn]->getColumnName();
if ($autoincrementColumn == 'ID')
{
// old-school hack
$aiName = $tableName;
}
else
{
$aiName = $tableName.'_'.$autoincrementColumn;
}
$this->query('CREATE SEQUENCE '.$this->getSqlHelper()->quote('sq_'.$aiName));
$this->query('CREATE OR REPLACE TRIGGER '.$this->getSqlHelper()->quote($aiName.'_insert').'
BEFORE INSERT
ON '.$this->getSqlHelper()->quote($tableName).'
FOR EACH ROW
BEGIN
IF :NEW.'.$this->getSqlHelper()->quote($autoincrementColumn).' IS NULL THEN
SELECT '.$this->getSqlHelper()->quote('sq_'.$aiName).'.NEXTVAL
INTO :NEW.'.$this->getSqlHelper()->quote($autoincrementColumn).' FROM dual;
END IF;
END;'
);
}
}
}