1
0
mirror of synced 2025-01-05 16:53:21 +03:00

Fixes for exporting sql to correct connections.

This commit is contained in:
Jonathan.Wage 2007-10-05 05:06:42 +00:00
parent d2d52a63ce
commit 956c5dfe91
2 changed files with 55 additions and 36 deletions

View File

@ -976,23 +976,10 @@ class Doctrine_Export extends Doctrine_Connection_Module
* @return void * @return void
*/ */
public function exportSchema($directory = null) public function exportSchema($directory = null)
{ {
$sql = $this->exportSql($directory); $models = Doctrine::loadModels($directory);
$this->conn->beginTransaction(); $this->exportClasses($models);
foreach ($sql as $query) {
try {
$this->conn->exec($query);
} catch (Doctrine_Connection_Exception $e) {
// we only want to silence table already exists errors
if ($e->getPortableCode() !== Doctrine::ERR_ALREADY_EXISTS) {
$this->conn->rollback();
throw $e;
}
}
}
$this->conn->commit();
} }
/** /**
* exportClasses * exportClasses
@ -1005,22 +992,38 @@ class Doctrine_Export extends Doctrine_Connection_Module
*/ */
public function exportClasses(array $classes) public function exportClasses(array $classes)
{ {
$sql = $this->exportClassesSql($classes); $connections = array();
foreach ($classes as $class) {
$this->conn->beginTransaction(); $record = new $class();
$connection = $record->getTable()->getConnection();
foreach ($sql as $query) { $connectionName = Doctrine_Manager::getInstance()->getConnectionName($connection);
try {
$this->conn->exec($query); if (!isset($connections[$connectionName])) {
} catch (Doctrine_Connection_Exception $e) { $connections[$connectionName] = array();
// we only want to silence table already exists errors }
if ($e->getPortableCode() !== Doctrine::ERR_ALREADY_EXISTS) {
$this->conn->rollback(); $connections[$connectionName] = array_merge($connections[$connectionName], $this->exportClassesSql(array($class)));
throw $e; }
foreach ($connections as $connectionName => $sql) {
$connection = Doctrine_Manager::getInstance()->getConnection($connectionName);
$connection->beginTransaction();
foreach ($sql as $query) {
try {
$connection->exec($query);
} catch (Doctrine_Connection_Exception $e) {
// we only want to silence table already exists errors
if ($e->getPortableCode() !== Doctrine::ERR_ALREADY_EXISTS) {
$connection->rollback();
throw $e;
}
} }
} }
$connection->commit();
} }
$this->conn->commit();
} }
/** /**
* exportClassesSql * exportClassesSql
@ -1050,6 +1053,7 @@ class Doctrine_Export extends Doctrine_Connection_Module
} else { } else {
$sql[] = $query; $sql[] = $query;
} }
if ($table->getAttribute(Doctrine::ATTR_EXPORT) & Doctrine::EXPORT_PLUGINS) { if ($table->getAttribute(Doctrine::ATTR_EXPORT) & Doctrine::EXPORT_PLUGINS) {
$sql = array_merge($sql, $this->exportPluginsSql($table)); $sql = array_merge($sql, $this->exportPluginsSql($table));
} }

View File

@ -134,6 +134,11 @@ END;
$i = 0; $i = 0;
if (isset($options['inheritance']['extends']) && !isset($options['override_parent'])) {
$ret[$i] = "\t\t\t\tparent::setTableDefinition();";
$i++;
}
if (isset($options['tableName']) && !empty($options['tableName'])) { if (isset($options['tableName']) && !empty($options['tableName'])) {
$ret[$i] = str_repeat(' ', 8) . '$this->setTableName(\''. $options['tableName'].'\');'; $ret[$i] = str_repeat(' ', 8) . '$this->setTableName(\''. $options['tableName'].'\');';
@ -188,7 +193,7 @@ END;
} }
if (!empty($ret)) { if (!empty($ret)) {
return "\n\t\tpublic function setTableDefinition()"."\n\t\t{\n\t\t\t\tparent::setTableDefinition();\n".implode("\n", $ret)."\n\t\t}"; return "\n\t\tpublic function setTableDefinition()"."\n\t\t{\n".implode("\n", $ret)."\n\t\t}";
} }
} }
public function buildSetUp(array $options, array $columns, array $relations) public function buildSetUp(array $options, array $columns, array $relations)
@ -197,6 +202,11 @@ END;
$i = 0; $i = 0;
if (isset($options['inheritance']['extends']) && !isset($options['override_parent'])) {
$ret[$i] = "\t\t\t\tparent::setUp();";
$i++;
}
foreach ($relations as $name => $relation) { foreach ($relations as $name => $relation) {
$alias = (isset($relation['alias']) && $relation['alias'] !== $name) ? ' as ' . $relation['alias'] : ''; $alias = (isset($relation['alias']) && $relation['alias'] !== $name) ? ' as ' . $relation['alias'] : '';
@ -253,7 +263,7 @@ END;
} }
if (!empty($ret)) { if (!empty($ret)) {
return "\n\t\tpublic function setUp()\n\t\t{\n\t\t\t\tparent::setUp();\n\t\t\t\t".implode("\n", $ret)."\n\t\t}"; return "\n\t\tpublic function setUp()\n\t\t{\n".implode("\n", $ret)."\n\t\t}";
} }
} }
@ -266,8 +276,13 @@ END;
$className = $options['className']; $className = $options['className'];
$extends = isset($options['inheritance']['extends']) ? $options['inheritance']['extends']:'Doctrine_Record'; $extends = isset($options['inheritance']['extends']) ? $options['inheritance']['extends']:'Doctrine_Record';
$definition = $this->buildTableDefinition($options, $columns, $relations); if (!isset($options['no_definition'])) {
$setUp = $this->buildSetUp($options, $columns, $relations); $definition = $this->buildTableDefinition($options, $columns, $relations);
$setUp = $this->buildSetUp($options, $columns, $relations);
} else {
$definition = null;
$setUp = null;
}
$content = sprintf(self::$tpl, $className, $content = sprintf(self::$tpl, $className,
$extends, $extends,
@ -298,7 +313,7 @@ END;
if ($this->generateBaseClasses()) { if ($this->generateBaseClasses()) {
if (!file_exists($options['fileName'])) { //if (!file_exists($options['fileName'])) {
$optionsBak = $options; $optionsBak = $options;
unset($options['tableName']); unset($options['tableName']);
@ -306,7 +321,7 @@ END;
$this->writeDefinition($options, array(), array()); $this->writeDefinition($options, array(), array());
$options = $optionsBak; $options = $optionsBak;
} //}
$generatedPath = $this->path . DIRECTORY_SEPARATOR . $this->baseClassesDirectory; $generatedPath = $this->path . DIRECTORY_SEPARATOR . $this->baseClassesDirectory;