2007-07-21 19:17:17 +04:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many individuals
|
|
|
|
* and is licensed under the LGPL. For more information, see
|
2008-01-23 01:52:53 +03:00
|
|
|
* <http://www.phpdoctrine.org>.
|
2007-07-21 19:17:17 +04:00
|
|
|
*/
|
2008-05-30 16:09:24 +04:00
|
|
|
|
|
|
|
#namespace Doctrine::DBAL::Export;
|
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
/**
|
|
|
|
* Doctrine_Export
|
|
|
|
*
|
|
|
|
* @package Doctrine
|
2007-10-04 01:43:22 +04:00
|
|
|
* @subpackage Export
|
2007-07-21 19:17:17 +04:00
|
|
|
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
|
|
|
* @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
|
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
2008-02-22 21:11:35 +03:00
|
|
|
* @link www.phpdoctrine.org
|
2007-07-21 19:17:17 +04:00
|
|
|
* @since 1.0
|
|
|
|
* @version $Revision$
|
2008-07-27 23:38:56 +04:00
|
|
|
* @todo Rename to ExportManager. Subclasses: MySqlExportManager, PgSqlExportManager etc.
|
2007-07-21 19:17:17 +04:00
|
|
|
*/
|
|
|
|
class Doctrine_Export extends Doctrine_Connection_Module
|
|
|
|
{
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
/**
|
2007-08-17 02:42:35 +04:00
|
|
|
* exportSchema
|
2008-05-14 01:20:34 +04:00
|
|
|
* method for exporting Doctrine_Entity classes to a schema
|
2007-07-21 19:17:17 +04:00
|
|
|
*
|
2007-09-03 14:41:51 +04:00
|
|
|
* if the directory parameter is given this method first iterates
|
2007-07-21 19:17:17 +04:00
|
|
|
* recursively trhough the given directory in order to find any model classes
|
|
|
|
*
|
|
|
|
* Then it iterates through all declared classes and creates tables for the ones
|
2008-05-14 01:20:34 +04:00
|
|
|
* that extend Doctrine_Entity and are not abstract classes
|
2007-07-21 19:17:17 +04:00
|
|
|
*
|
|
|
|
* @throws Doctrine_Connection_Exception if some error other than Doctrine::ERR_ALREADY_EXISTS
|
|
|
|
* occurred during the create table operation
|
|
|
|
* @param string $directory optional directory parameter
|
|
|
|
* @return void
|
|
|
|
*/
|
2007-08-17 02:42:35 +04:00
|
|
|
public function exportSchema($directory = null)
|
2007-10-11 07:23:33 +04:00
|
|
|
{
|
|
|
|
if ($directory !== null) {
|
2007-10-19 18:30:48 +04:00
|
|
|
$models = Doctrine::loadModels($directory);
|
2008-02-19 23:12:46 +03:00
|
|
|
} else {
|
|
|
|
$models = Doctrine::getLoadedModels();
|
2007-10-11 07:23:33 +04:00
|
|
|
}
|
2008-01-10 11:29:20 +03:00
|
|
|
|
2007-10-05 09:06:42 +04:00
|
|
|
$this->exportClasses($models);
|
2007-07-21 19:17:17 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
/**
|
|
|
|
* exportClasses
|
2008-01-23 00:42:17 +03:00
|
|
|
*
|
|
|
|
* FIXME: This method is a big huge hack. The sql needs to be executed in the correct order. I have some stupid logic to
|
|
|
|
* make sure they are in the right order.
|
|
|
|
*
|
2008-05-14 01:20:34 +04:00
|
|
|
* method for exporting Doctrine_Entity classes to a schema
|
2007-07-21 19:17:17 +04:00
|
|
|
*
|
|
|
|
* @throws Doctrine_Connection_Exception if some error other than Doctrine::ERR_ALREADY_EXISTS
|
|
|
|
* occurred during the create table operation
|
|
|
|
* @param array $classes
|
|
|
|
* @return void
|
2008-08-01 22:46:14 +04:00
|
|
|
* @todo ORM stuff
|
2007-07-21 19:17:17 +04:00
|
|
|
*/
|
|
|
|
public function exportClasses(array $classes)
|
2008-02-04 00:29:57 +03:00
|
|
|
{
|
2007-10-05 09:06:42 +04:00
|
|
|
$connections = array();
|
|
|
|
foreach ($classes as $class) {
|
|
|
|
$record = new $class();
|
|
|
|
$connection = $record->getTable()->getConnection();
|
|
|
|
$connectionName = Doctrine_Manager::getInstance()->getConnectionName($connection);
|
2008-01-10 11:29:20 +03:00
|
|
|
|
2007-10-21 10:23:59 +04:00
|
|
|
if ( ! isset($connections[$connectionName])) {
|
2008-02-13 07:13:55 +03:00
|
|
|
$connections[$connectionName] = array(
|
|
|
|
'create_tables' => array(),
|
|
|
|
'create_sequences' => array(),
|
|
|
|
'alters' => array()
|
|
|
|
);
|
2007-10-05 09:06:42 +04:00
|
|
|
}
|
2008-01-10 11:29:20 +03:00
|
|
|
|
2007-10-09 03:55:25 +04:00
|
|
|
$sql = $this->exportClassesSql(array($class));
|
2008-02-04 00:29:57 +03:00
|
|
|
|
2007-11-08 21:52:08 +03:00
|
|
|
// Build array of all the creates
|
|
|
|
// We need these to happen first
|
|
|
|
foreach ($sql as $key => $query) {
|
2008-01-23 00:42:17 +03:00
|
|
|
if (strstr($query, 'CREATE TABLE')) {
|
|
|
|
$connections[$connectionName]['create_tables'][] = $query;
|
|
|
|
|
|
|
|
unset($sql[$key]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strstr($query, 'CREATE SEQUENCE')) {
|
|
|
|
$connections[$connectionName]['create_sequences'][] = $query;
|
|
|
|
|
2007-11-08 21:52:08 +03:00
|
|
|
unset($sql[$key]);
|
|
|
|
}
|
|
|
|
}
|
2008-01-10 11:29:20 +03:00
|
|
|
|
2007-10-09 03:55:25 +04:00
|
|
|
$connections[$connectionName]['alters'] = array_merge($connections[$connectionName]['alters'], $sql);
|
2007-10-05 09:06:42 +04:00
|
|
|
}
|
2007-11-08 21:52:08 +03:00
|
|
|
|
2007-10-09 03:55:25 +04:00
|
|
|
// Loop over all the sql again to merge the creates and alters in to the same array, but so that the alters are at the bottom
|
|
|
|
$build = array();
|
2007-10-05 09:06:42 +04:00
|
|
|
foreach ($connections as $connectionName => $sql) {
|
2008-01-23 00:42:17 +03:00
|
|
|
$build[$connectionName] = array_merge($sql['create_tables'], $sql['create_sequences'], $sql['alters']);
|
2007-10-09 03:55:25 +04:00
|
|
|
}
|
2007-11-08 21:52:08 +03:00
|
|
|
|
2007-10-09 03:55:25 +04:00
|
|
|
foreach ($build as $connectionName => $sql) {
|
2007-10-05 09:06:42 +04:00
|
|
|
$connection = Doctrine_Manager::getInstance()->getConnection($connectionName);
|
2008-01-10 11:29:20 +03:00
|
|
|
|
2007-10-05 09:06:42 +04:00
|
|
|
$connection->beginTransaction();
|
2008-01-10 11:29:20 +03:00
|
|
|
|
2007-10-05 09:06:42 +04:00
|
|
|
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();
|
2007-10-17 03:33:14 +04:00
|
|
|
throw new Doctrine_Export_Exception($e->getMessage() . '. Failing Query: ' . $query);
|
2007-10-05 09:06:42 +04:00
|
|
|
}
|
2007-07-21 19:17:17 +04:00
|
|
|
}
|
|
|
|
}
|
2008-01-10 11:29:20 +03:00
|
|
|
|
2007-10-05 09:06:42 +04:00
|
|
|
$connection->commit();
|
2007-07-21 19:17:17 +04:00
|
|
|
}
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
/**
|
|
|
|
* exportClassesSql
|
2008-05-14 01:20:34 +04:00
|
|
|
* method for exporting Doctrine_Entity classes to a schema
|
2007-07-21 19:17:17 +04:00
|
|
|
*
|
|
|
|
* @throws Doctrine_Connection_Exception if some error other than Doctrine::ERR_ALREADY_EXISTS
|
|
|
|
* occurred during the create table operation
|
|
|
|
* @param array $classes
|
|
|
|
* @return void
|
2008-02-04 00:29:57 +03:00
|
|
|
* @todo package:orm
|
2007-07-21 19:17:17 +04:00
|
|
|
*/
|
|
|
|
public function exportClassesSql(array $classes)
|
|
|
|
{
|
2007-11-27 21:23:13 +03:00
|
|
|
$models = Doctrine::filterInvalidModels($classes);
|
2008-01-10 11:29:20 +03:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
$sql = array();
|
2008-02-04 00:29:57 +03:00
|
|
|
$finishedClasses = array();
|
|
|
|
|
2007-09-19 23:43:33 +04:00
|
|
|
foreach ($models as $name) {
|
2008-02-04 00:29:57 +03:00
|
|
|
if (in_array($name, $finishedClasses)) {
|
|
|
|
continue;
|
2007-11-09 02:03:28 +03:00
|
|
|
}
|
2008-02-04 00:29:57 +03:00
|
|
|
|
|
|
|
$classMetadata = $this->conn->getClassMetadata($name);
|
|
|
|
|
2008-05-01 13:41:47 +04:00
|
|
|
// In Class Table Inheritance we have to make sure that ALL tables of parent classes
|
|
|
|
// are exported, too as soon as ONE table is exported, because the data of one class is stored
|
2008-02-04 00:29:57 +03:00
|
|
|
// across many tables.
|
2008-03-23 14:30:29 +03:00
|
|
|
if ($classMetadata->getInheritanceType() == Doctrine::INHERITANCE_TYPE_JOINED) {
|
2008-03-26 14:10:45 +03:00
|
|
|
$parents = $classMetadata->getParentClasses();
|
2008-02-04 00:29:57 +03:00
|
|
|
foreach ($parents as $parent) {
|
|
|
|
$data = $classMetadata->getConnection()->getClassMetadata($parent)->getExportableFormat();
|
|
|
|
$query = $this->conn->export->createTableSql($data['tableName'], $data['columns'], $data['options']);
|
|
|
|
$sql = array_merge($sql, (array) $query);
|
|
|
|
$finishedClasses[] = $parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = $classMetadata->getExportableFormat();
|
2007-09-01 20:11:58 +04:00
|
|
|
$query = $this->conn->export->createTableSql($data['tableName'], $data['columns'], $data['options']);
|
|
|
|
|
|
|
|
if (is_array($query)) {
|
|
|
|
$sql = array_merge($sql, $query);
|
|
|
|
} else {
|
|
|
|
$sql[] = $query;
|
|
|
|
}
|
2008-01-10 11:29:20 +03:00
|
|
|
|
2008-02-04 00:29:57 +03:00
|
|
|
if ($classMetadata->getAttribute(Doctrine::ATTR_EXPORT) & Doctrine::EXPORT_PLUGINS) {
|
|
|
|
$sql = array_merge($sql, $this->exportGeneratorsSql($classMetadata));
|
2007-09-07 00:23:52 +04:00
|
|
|
}
|
2007-07-21 19:17:17 +04:00
|
|
|
}
|
2008-01-10 11:29:20 +03:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
$sql = array_unique($sql);
|
2008-01-10 11:29:20 +03:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
rsort($sql);
|
|
|
|
|
|
|
|
return $sql;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-09-07 00:23:52 +04:00
|
|
|
/**
|
2007-11-29 01:56:45 +03:00
|
|
|
* fetches all generators recursively for given table
|
2007-09-07 00:23:52 +04:00
|
|
|
*
|
2007-11-29 01:56:45 +03:00
|
|
|
* @param Doctrine_Table $table table object to retrieve the generators from
|
|
|
|
* @return array an array of Doctrine_Record_Generator objects
|
2008-02-04 00:29:57 +03:00
|
|
|
* @todo package:orm
|
2007-11-14 01:28:37 +03:00
|
|
|
*/
|
2008-02-04 00:29:57 +03:00
|
|
|
public function getAllGenerators(Doctrine_ClassMetadata $table)
|
2007-09-07 00:23:52 +04:00
|
|
|
{
|
2007-11-29 01:56:45 +03:00
|
|
|
$generators = array();
|
2007-09-07 00:23:52 +04:00
|
|
|
|
2007-11-29 01:56:45 +03:00
|
|
|
foreach ($table->getGenerators() as $name => $generator) {
|
|
|
|
if ($generator === null) {
|
2008-01-10 11:29:20 +03:00
|
|
|
continue;
|
2007-09-07 01:07:24 +04:00
|
|
|
}
|
|
|
|
|
2007-11-29 01:56:45 +03:00
|
|
|
$generators[] = $generator;
|
2007-11-14 01:05:12 +03:00
|
|
|
|
2007-11-29 01:56:45 +03:00
|
|
|
$generatorTable = $generator->getTable();
|
2008-01-10 11:29:20 +03:00
|
|
|
|
2007-11-29 01:56:45 +03:00
|
|
|
if ($generatorTable instanceof Doctrine_Table) {
|
|
|
|
$generators = array_merge($generators, $this->getAllGenerators($generatorTable));
|
2007-11-14 01:28:37 +03:00
|
|
|
}
|
2007-11-14 01:05:12 +03:00
|
|
|
}
|
|
|
|
|
2007-11-29 01:56:45 +03:00
|
|
|
return $generators;
|
2007-11-14 01:05:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-11-29 01:56:45 +03:00
|
|
|
* exportGeneratorsSql
|
2007-11-14 01:05:12 +03:00
|
|
|
* exports plugin tables for given table
|
|
|
|
*
|
2007-11-29 01:56:45 +03:00
|
|
|
* @param Doctrine_Table $table the table in which the generators belong to
|
|
|
|
* @return array an array of sql strings
|
2008-02-04 00:29:57 +03:00
|
|
|
* @todo package:orm
|
2007-11-14 01:05:12 +03:00
|
|
|
*/
|
2008-02-04 00:29:57 +03:00
|
|
|
public function exportGeneratorsSql(Doctrine_ClassMetadata $class)
|
2007-11-14 01:05:12 +03:00
|
|
|
{
|
|
|
|
$sql = array();
|
2008-02-04 00:29:57 +03:00
|
|
|
foreach ($this->getAllGenerators($class) as $name => $generator) {
|
2007-11-29 01:56:45 +03:00
|
|
|
$table = $generator->getTable();
|
2008-01-10 11:29:20 +03:00
|
|
|
|
2007-09-25 02:27:41 +04:00
|
|
|
// Make sure plugin has a valid table
|
|
|
|
if ($table instanceof Doctrine_Table) {
|
|
|
|
$data = $table->getExportableFormat();
|
|
|
|
$query = $this->conn->export->createTableSql($data['tableName'], $data['columns'], $data['options']);
|
|
|
|
$sql = array_merge($sql, (array) $query);
|
|
|
|
}
|
2007-09-07 00:23:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return $sql;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
/**
|
|
|
|
* exportSql
|
2008-05-14 01:20:34 +04:00
|
|
|
* returns the sql for exporting Doctrine_Entity classes to a schema
|
2007-07-21 19:17:17 +04:00
|
|
|
*
|
2007-09-03 14:41:51 +04:00
|
|
|
* if the directory parameter is given this method first iterates
|
2007-07-21 19:17:17 +04:00
|
|
|
* recursively trhough the given directory in order to find any model classes
|
|
|
|
*
|
|
|
|
* Then it iterates through all declared classes and creates tables for the ones
|
2008-05-14 01:20:34 +04:00
|
|
|
* that extend Doctrine_Entity and are not abstract classes
|
2007-07-21 19:17:17 +04:00
|
|
|
*
|
|
|
|
* @throws Doctrine_Connection_Exception if some error other than Doctrine::ERR_ALREADY_EXISTS
|
|
|
|
* occurred during the create table operation
|
|
|
|
* @param string $directory optional directory parameter
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function exportSql($directory = null)
|
|
|
|
{
|
2007-10-16 06:37:31 +04:00
|
|
|
if ($directory !== null) {
|
2007-10-19 18:30:48 +04:00
|
|
|
$models = Doctrine::loadModels($directory);
|
2007-10-16 06:37:31 +04:00
|
|
|
} else {
|
2007-10-19 18:30:48 +04:00
|
|
|
$models = Doctrine::getLoadedModels();
|
2007-10-16 06:37:31 +04:00
|
|
|
}
|
2008-01-10 11:29:20 +03:00
|
|
|
|
2007-09-19 23:43:33 +04:00
|
|
|
return $this->exportClassesSql($models);
|
2007-07-21 19:17:17 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
/**
|
|
|
|
* exportTable
|
|
|
|
* exports given table into database based on column and option definitions
|
|
|
|
*
|
|
|
|
* @throws Doctrine_Connection_Exception if some error other than Doctrine::ERR_ALREADY_EXISTS
|
|
|
|
* occurred during the create table operation
|
|
|
|
* @return boolean whether or not the export operation was successful
|
|
|
|
* false if table already existed in the database
|
2008-08-01 22:46:14 +04:00
|
|
|
* @todo ORM stuff
|
2007-07-21 19:17:17 +04:00
|
|
|
*/
|
2008-02-10 17:32:05 +03:00
|
|
|
public function exportTable(Doctrine_ClassMetadata $metadata)
|
2007-07-21 19:17:17 +04:00
|
|
|
{
|
2007-09-03 18:57:18 +04:00
|
|
|
/**
|
|
|
|
TODO: maybe there should be portability option for the following check
|
2007-07-21 19:17:17 +04:00
|
|
|
if ( ! Doctrine::isValidClassname($table->getOption('declaringClass')->getName())) {
|
|
|
|
throw new Doctrine_Export_Exception('Class name not valid.');
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
try {
|
2008-02-10 17:32:05 +03:00
|
|
|
$data = $metadata->getExportableFormat();
|
2007-07-21 19:17:17 +04:00
|
|
|
|
|
|
|
$this->conn->export->createTable($data['tableName'], $data['columns'], $data['options']);
|
2008-02-10 17:32:05 +03:00
|
|
|
} catch (Doctrine_Connection_Exception $e) {
|
2007-07-21 19:17:17 +04:00
|
|
|
// we only want to silence table already exists errors
|
2007-09-03 18:57:18 +04:00
|
|
|
if ($e->getPortableCode() !== Doctrine::ERR_ALREADY_EXISTS) {
|
2007-07-21 19:17:17 +04:00
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-11-09 02:03:28 +03:00
|
|
|
}
|