Commit 45ceb8b0 authored by zYne's avatar zYne

--no commit message

--no commit message
parent 3c678fc5
...@@ -207,7 +207,20 @@ class Doctrine_Export extends Doctrine_Connection_Module ...@@ -207,7 +207,20 @@ class Doctrine_Export extends Doctrine_Connection_Module
$name = $this->conn->quoteIdentifier($name, true); $name = $this->conn->quoteIdentifier($name, true);
$query = 'CREATE TABLE ' . $name . ' (' . $queryFields . ')'; $query = 'CREATE TABLE ' . $name . ' (' . $queryFields . ')';
return $query; $sql[] = $query;
if (isset($options['foreignKeys'])) {
foreach ((array) $options['foreignKeys'] as $k => $definition) {
if (is_array($definition)) {
if ( ! isset($definition['table'])) {
$definition['table'] = $name;
}
$sql[] = $this->createForeignKeySql($definition['table'], $definition);
}
}
}
return $sql;
} }
/** /**
* create a new table * create a new table
...@@ -221,7 +234,11 @@ class Doctrine_Export extends Doctrine_Connection_Module ...@@ -221,7 +234,11 @@ class Doctrine_Export extends Doctrine_Connection_Module
*/ */
public function createTable($name, array $fields, array $options = array()) public function createTable($name, array $fields, array $options = array())
{ {
return $this->conn->execute($this->createTableSql($name, $fields, $options)); $sql = (array) $this->createTableSql($name, $fields, $options);
foreach ($sql as $query) {
$this->conn->execute($query);
}
} }
/** /**
* create sequence * create sequence
...@@ -282,6 +299,31 @@ class Doctrine_Export extends Doctrine_Connection_Module ...@@ -282,6 +299,31 @@ class Doctrine_Export extends Doctrine_Connection_Module
* @return void * @return void
*/ */
public function createConstraint($table, $name, $definition) public function createConstraint($table, $name, $definition)
{
return $this->conn->exec($this->createConstraintSql($table, $name, $definition));
}
/**
* create a constraint on a table
*
* @param string $table name of the table on which the constraint is to be created
* @param string $name name of the constraint to be created
* @param array $definition associative array that defines properties of the constraint to be created.
* Currently, only one property named FIELDS is supported. This property
* is also an associative with the names of the constraint fields as array
* constraints. Each entry of this array is set to another type of associative
* array that specifies properties of the constraint that are specific to
* each field.
*
* Example
* array(
* 'fields' => array(
* 'user_name' => array(),
* 'last_login' => array()
* )
* )
* @return void
*/
public function createConstraintSql($table, $name, $definition)
{ {
$table = $this->conn->quoteIdentifier($table); $table = $this->conn->quoteIdentifier($table);
$name = $this->conn->quoteIdentifier($this->conn->formatter->getIndexName($name)); $name = $this->conn->quoteIdentifier($this->conn->formatter->getIndexName($name));
...@@ -299,7 +341,7 @@ class Doctrine_Export extends Doctrine_Connection_Module ...@@ -299,7 +341,7 @@ class Doctrine_Export extends Doctrine_Connection_Module
} }
$query .= ' ('. implode(', ', $fields) . ')'; $query .= ' ('. implode(', ', $fields) . ')';
return $this->conn->exec($query); return $query;
} }
/** /**
* Get the stucture of a field into an array * Get the stucture of a field into an array
...@@ -877,16 +919,20 @@ class Doctrine_Export extends Doctrine_Connection_Module ...@@ -877,16 +919,20 @@ class Doctrine_Export extends Doctrine_Connection_Module
{ {
$sql = $this->exportSql($directory); $sql = $this->exportSql($directory);
$this->conn->beginTransaction();
foreach ($sql as $query) { foreach ($sql as $query) {
try { try {
$this->conn->exec($query); $this->conn->exec($query);
} catch (Doctrine_Connection_Exception $e) { } catch (Doctrine_Connection_Exception $e) {
// we only want to silence table already exists errors // we only want to silence table already exists errors
if($e->getPortableCode() !== Doctrine::ERR_ALREADY_EXISTS) { if($e->getPortableCode() !== Doctrine::ERR_ALREADY_EXISTS) {
$this->conn->rollback();
throw $e; throw $e;
} }
} }
} }
$this->conn->commit();
} }
/** /**
* exportClasses * exportClasses
...@@ -901,17 +947,20 @@ class Doctrine_Export extends Doctrine_Connection_Module ...@@ -901,17 +947,20 @@ class Doctrine_Export extends Doctrine_Connection_Module
{ {
$sql = $this->exportClassesSql($classes); $sql = $this->exportClassesSql($classes);
$this->conn->beginTransaction();
foreach ($sql as $query) { foreach ($sql as $query) {
try { try {
$this->conn->exec($query); $this->conn->exec($query);
} catch (Doctrine_Connection_Exception $e) { } catch (Doctrine_Connection_Exception $e) {
// we only want to silence table already exists errors // we only want to silence table already exists errors
if($e->getPortableCode() !== Doctrine::ERR_ALREADY_EXISTS) { if($e->getPortableCode() !== Doctrine::ERR_ALREADY_EXISTS) {
print $query."<br>"; $this->conn->rollback();
throw $e; throw $e;
} }
} }
} }
$this->conn->commit();
} }
/** /**
* exportClassesSql * exportClassesSql
......
...@@ -93,31 +93,30 @@ class Doctrine_Export_Oracle extends Doctrine_Export ...@@ -93,31 +93,30 @@ class Doctrine_Export_Oracle extends Doctrine_Export
*/ */
public function _makeAutoincrement($name, $table, $start = 1) public function _makeAutoincrement($name, $table, $start = 1)
{ {
$sql = array();
$table = strtoupper($table); $table = strtoupper($table);
$index_name = $table . '_AI_PK'; $indexName = $table . '_AI_PK';
$definition = array( $definition = array(
'primary' => true, 'primary' => true,
'fields' => array($name => true), 'fields' => array($name => true),
); );
$result = $this->createConstraint($table, $index_name, $definition);
$sql[] = $this->createConstraintSql($table, $indexName, $definition);
if (is_null($start)) { if (is_null($start)) {
$this->conn->beginTransaction();
$query = 'SELECT MAX(' . $this->conn->quoteIdentifier($name, true) . ') FROM ' . $this->conn->quoteIdentifier($table, true); $query = 'SELECT MAX(' . $this->conn->quoteIdentifier($name, true) . ') FROM ' . $this->conn->quoteIdentifier($table, true);
$start = $this->conn->fetchOne($query); $start = $this->conn->fetchOne($query);
++$start; ++$start;
$result = $this->createSequence($table, $start);
$this->conn->commit();
} else {
$result = $this->createSequence($table, $start);
} }
$sequence_name = $this->conn->formatter->getSequenceName($table); $sql[] = $this->createSequenceSql($table, $start);
$trigger_name = $this->conn->quoteIdentifier($table . '_AI_PK', true);
$sequenceName = $this->conn->formatter->getSequenceName($table);
$triggerName = $this->conn->quoteIdentifier($table . '_AI_PK', true);
$table = $this->conn->quoteIdentifier($table, true); $table = $this->conn->quoteIdentifier($table, true);
$name = $this->conn->quoteIdentifier($name, true); $name = $this->conn->quoteIdentifier($name, true);
$trigger_sql = 'CREATE TRIGGER '.$trigger_name.' $sql[] = 'CREATE TRIGGER ' . $triggerName . '
BEFORE INSERT BEFORE INSERT
ON '.$table.' ON '.$table.'
FOR EACH ROW FOR EACH ROW
...@@ -125,21 +124,21 @@ DECLARE ...@@ -125,21 +124,21 @@ DECLARE
last_Sequence NUMBER; last_Sequence NUMBER;
last_InsertID NUMBER; last_InsertID NUMBER;
BEGIN BEGIN
SELECT '.$sequence_name.'.NEXTVAL INTO :NEW.'.$name.' FROM DUAL; SELECT '.$sequenceName.'.NEXTVAL INTO :NEW.'.$name.' FROM DUAL;
IF (:NEW.'.$name.' IS NULL OR :NEW.'.$name.' = 0) THEN IF (:NEW.'.$name.' IS NULL OR :NEW.'.$name.' = 0) THEN
SELECT '.$sequence_name.'.NEXTVAL INTO :NEW.'.$name.' FROM DUAL; SELECT '.$sequenceName.'.NEXTVAL INTO :NEW.'.$name.' FROM DUAL;
ELSE ELSE
SELECT NVL(Last_Number, 0) INTO last_Sequence SELECT NVL(Last_Number, 0) INTO last_Sequence
FROM User_Sequences FROM User_Sequences
WHERE UPPER(Sequence_Name) = UPPER(\''.$sequence_name.'\'); WHERE UPPER(Sequence_Name) = UPPER(\''.$sequenceName.'\');
SELECT :NEW.id INTO last_InsertID FROM DUAL; SELECT :NEW.id INTO last_InsertID FROM DUAL;
WHILE (last_InsertID > last_Sequence) LOOP WHILE (last_InsertID > last_Sequence) LOOP
SELECT '.$sequence_name.'.NEXTVAL INTO last_Sequence FROM DUAL; SELECT ' . $sequenceName . '.NEXTVAL INTO last_Sequence FROM DUAL;
END LOOP; END LOOP;
END IF; END IF;
END; END;
'; ';
return $this->conn->exec($trigger_sql); return $sql;
} }
/** /**
* drop an existing autoincrement sequence + trigger * drop an existing autoincrement sequence + trigger
...@@ -246,17 +245,56 @@ END; ...@@ -246,17 +245,56 @@ END;
{ {
$this->conn->beginTransaction(); $this->conn->beginTransaction();
$result = parent::createTable($name, $fields, $options); foreach ($this->createTableSql($name, $fields, $options) as $sql) {
$this->conn->exec($sql);
foreach ($fields as $field_name => $field) {
if (isset($field['autoincrement']) && $field['autoincrement']) {
$result = $this->_makeAutoincrement($field_name, $name);
}
} }
$this->conn->commit(); $this->conn->commit();
}
return $result; /**
* create a new table
*
* @param string $name Name of the database that should be created
* @param array $fields Associative array that contains the definition of each field of the new table
* The indexes of the array entries are the names of the fields of the table an
* the array entry values are associative arrays like those that are meant to be
* passed with the field definitions to get[Type]Declaration() functions.
*
* Example
* array(
*
* 'id' => array(
* 'type' => 'integer',
* 'unsigned' => 1
* 'notnull' => 1
* 'default' => 0
* ),
* 'name' => array(
* 'type' => 'text',
* 'length' => 12
* ),
* 'password' => array(
* 'type' => 'text',
* 'length' => 12
* )
* );
* @param array $options An associative array of table options:
*
* @return void
*/
public function createTableSql($name, array $fields, array $options = array())
{
$sql = parent::createTableSql($name, $fields, $options);
foreach ($fields as $fieldName => $field) {
if (isset($field['autoincrement']) && $field['autoincrement'] ||
(isset($field['autoinc']) && $fields['autoinc'])) {
$sql = array_merge($sql, $this->_makeAutoincrement($fieldName, $name));
}
}
return $sql;
} }
/** /**
* drop an existing table * drop an existing table
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment