1
0
mirror of synced 2024-12-13 06:46:03 +03:00

Changed import/export methods to importSchema() and exportSchema()

This commit is contained in:
Jonathan.Wage 2007-08-16 22:42:35 +00:00
parent b4600960fd
commit 99ae59fd41
17 changed files with 448 additions and 59 deletions

View File

@ -408,26 +408,26 @@ final class Doctrine
} }
} }
/** /**
* imprt * importSchema
* method for importing existing schema to Doctrine_Record classes * method for importing existing schema to Doctrine_Record classes
* *
* @param string $directory * @param string $directory
* @param array $info * @param array $info
* @return boolean * @return boolean
*/ */
public static function imprt($directory, array $databases = array()) public static function importSchema($directory, array $databases = array())
{ {
return Doctrine_Manager::connection()->imprt->imprt($directory, $databases); return Doctrine_Manager::connection()->import->importSchema($directory, $databases);
} }
/** /**
* export * exportSchema
* method for exporting Doctrine_Record classes to a schema * method for exporting Doctrine_Record classes to a schema
* *
* @param string $directory * @param string $directory
*/ */
public static function export($directory = null) public static function exportSchema($directory = null)
{ {
return Doctrine_Manager::connection()->export->export($directory); return Doctrine_Manager::connection()->export->exportSchema($directory);
} }
/** /**
* exportSql * exportSql

View File

@ -958,7 +958,7 @@ class Doctrine_Export extends Doctrine_Connection_Module
return ''; return '';
} }
/** /**
* export * exportSchema
* method for exporting Doctrine_Record classes to a schema * method for exporting Doctrine_Record classes to a schema
* *
* if the directory parameter is given this method first iterates * if the directory parameter is given this method first iterates
@ -972,7 +972,7 @@ class Doctrine_Export extends Doctrine_Connection_Module
* @param string $directory optional directory parameter * @param string $directory optional directory parameter
* @return void * @return void
*/ */
public function export($directory = null) public function exportSchema($directory = null)
{ {
$sql = $this->exportSql($directory); $sql = $this->exportSql($directory);

View File

@ -36,6 +36,54 @@
* @version $Revision: 1838 $ * @version $Revision: 1838 $
* @author Nicolas Bérard-Nault <nicobn@gmail.com> * @author Nicolas Bérard-Nault <nicobn@gmail.com>
*/ */
class Doctrine_Export_Schema abstract class Doctrine_Export_Schema
{ {
/**
* build
*
* Build the schema string to be dumped to file
*
* @param string $array
* @return void
*/
abstract function build($array);
/**
* dump
*
* Dump the array to the schema file
*
* @param string $array
* @param string $schema
* @return void
*/
abstract function dump($array, $schema);
/**
* buildSchema
*
* Build schema array that can be dumped to file
*
* @param string $directory
* @return void
*/
public function buildSchema($directory)
{
// we need to figure out how we can build all the model information for the passed directory/directories
return array();
}
/**
* exportSchema
*
* @param string $schema
* @param string $directory
* @return void
*/
public function exportSchema($schema, $directory)
{
$array = $this->buildSchema($directory);
$this->dump($arr, $schema);
}
} }

View File

@ -31,4 +31,49 @@
*/ */
class Doctrine_Export_Schema_Xml extends Doctrine_Export_Schema class Doctrine_Export_Schema_Xml extends Doctrine_Export_Schema
{ {
/**
* build
*
* Build the schema xml string to be dumped to file
*
* @param string $array
* @return void
*/
public function build($array)
{
$xml = new SimpleXMLElement();
foreach ($array as $tableName => $fields) {
$table = $xml->addChild('table');
$name = $table->addChild('name', $tableName);
$declaration = $table->addChild('declaration');
foreach ($fields as $fieldName => $properties) {
$field = $declaration->addChild('field');
$field->addChild('name', $fieldName);
foreach ($properties as $key => $value) {
$field->addChild($key, $value);
}
}
}
return $xml->asXml();
}
/**
* dump
*
* Dump the array to the schema file
*
* @param string $array
* @param string $schema
* @return void
*/
public function dump($array, $schema)
{
$xml = $this->build($array);
file_put_contents($schema, $xml);
}
} }

View File

@ -31,4 +31,32 @@
*/ */
class Doctrine_Export_Schema_Yml extends Doctrine_Export_Schema class Doctrine_Export_Schema_Yml extends Doctrine_Export_Schema
{ {
/**
* build
*
* Build the schema yml string to be dumped to file
*
* @param string $array
* @return void
*/
public function build($array)
{
return var_dump($array);
}
/**
* dump
*
* Dump the array to the schema file
*
* @param string $arr
* @param string $schema
* @return void
*/
public function dump($arr, $schema)
{
$yml = $this->build($array);
file_put_contents($schema, $yml);
}
} }

View File

@ -175,7 +175,7 @@ class Doctrine_Import extends Doctrine_Connection_Module
return $this->conn->fetchColumn($this->sql['listViews']); return $this->conn->fetchColumn($this->sql['listViews']);
} }
/** /**
* imprt * importSchema
* *
* method for importing existing schema to Doctrine_Record classes * method for importing existing schema to Doctrine_Record classes
* *
@ -183,7 +183,7 @@ class Doctrine_Import extends Doctrine_Connection_Module
* @param array $databases * @param array $databases
* @return array the names of the imported classes * @return array the names of the imported classes
*/ */
public function imprt($directory, array $databases = array()) public function importSchema($directory, array $databases = array())
{ {
$builder = new Doctrine_Import_Builder(); $builder = new Doctrine_Import_Builder();
$builder->setTargetPath($directory); $builder->setTargetPath($directory);

View File

@ -40,15 +40,27 @@
abstract class Doctrine_Import_Schema abstract class Doctrine_Import_Schema
{ {
/** /**
* Import the schema and return it in an array * parse
*
* Function to do the actual parsing of the file
*
* @param string $schema
* @return void
* @author Jonathan H. Wage
*/
abstract function parse($schema);
/**
* Parse the schema and return it in an array
* *
* @param string $schema * @param string $schema
* @access public * @access public
*/ */
abstract function importSchema($schema); abstract function parseSchema($schema);
/** /**
* import * importSchema
* *
* A method to import a Schema and translate it into a Doctrine_Record object * A method to import a Schema and translate it into a Doctrine_Record object
* *
@ -57,18 +69,20 @@ abstract class Doctrine_Import_Schema
* be written * be written
* @access public * @access public
*/ */
public function imprt($schema, $directory) public function importSchema($schema, $directory)
{ {
$builder = new Doctrine_Import_Builder(); $builder = new Doctrine_Import_Builder();
$builder->setTargetPath($directory); $builder->setTargetPath($directory);
$arr = $this->importSchema($schema); $array = $this->parseSchema($schema);
foreach ($arr as $name => $columns) { foreach ($array as $name => $properties) {
$options['className'] = $name; $options['className'] = $properties['class'];
$options['fileName'] = $directory.DIRECTORY_SEPARATOR.$name.'.class.php'; $options['fileName'] = $directory.DIRECTORY_SEPARATOR.$properties['class'].'.class.php';
$columns = $properties['columns'];
$builder->buildRecord($options, $columns, array()); $builder->buildRecord($options, $columns, array());
} }
} }
} }

View File

@ -39,16 +39,13 @@
*/ */
class Doctrine_Import_Schema_Xml extends Doctrine_Import_Schema class Doctrine_Import_Schema_Xml extends Doctrine_Import_Schema
{ {
/** /**
* importSchema * parse
* *
* A method to import a XML Schema and translate it into a property array. * @param string $schema
* The function returns that property array. * @return void
* */
* @param string $schema Path to the file containing the XML schema public function parse($schema)
* @return array
*/
public function importSchema($schema)
{ {
if (!is_readable($schema)) { if (!is_readable($schema)) {
throw new Doctrine_Import_Exception('Could not read schema file '. $schema); throw new Doctrine_Import_Exception('Could not read schema file '. $schema);
@ -58,31 +55,50 @@ class Doctrine_Import_Schema_Xml extends Doctrine_Import_Schema
throw new Doctrine_Import_Exception('Schema file '. $schema . ' is empty'); throw new Doctrine_Import_Exception('Schema file '. $schema . ' is empty');
} }
$xmlObj = simplexml_load_string($xmlString); return simplexml_load_string($xmlString);
}
/**
* parseSchema
*
* A method to parse a XML Schema and translate it into a property array.
* The function returns that property array.
*
* @param string $schema Path to the file containing the XML schema
* @return array
*/
public function parseSchema($schema)
{
$xmlObj = $this->parse($schema);
// Go through all tables... // Go through all tables...
foreach ($xmlObj->table as $table) { foreach ($xmlObj->table as $table) {
// Go through all columns... // Go through all columns...
foreach ($table->declaration->column as $column) { foreach ($table->declaration->field as $field) {
$colDesc = array( $colDesc = array(
'name' => (string) $column->name, 'name' => (string) $field->name,
'type' => (string) $column->type, 'type' => (string) $field->type,
'ptype' => (string) $column->type, 'ptype' => (string) $field->type,
'length' => (int) $column->length, 'length' => (int) $field->length,
'fixed' => (int) $column->fixed, 'fixed' => (int) $field->fixed,
'unsigned' => (bool) $column->unsigned, 'unsigned' => (bool) $field->unsigned,
'primary' => (bool) (isset($column->primary) && $column->primary), 'primary' => (bool) (isset($field->primary) && $field->primary),
'default' => (string) $column->default, 'default' => (string) $field->default,
'notnull' => (bool) (isset($column->notnull) && $column->notnull), 'notnull' => (bool) (isset($field->notnull) && $field->notnull),
'autoinc' => (bool) (isset($column->autoincrement) && $column->autoincrement), 'autoinc' => (bool) (isset($field->autoincrement) && $field->autoincrement),
); );
$columns[(string) $column->name] = $colDesc; $columns[(string) $field->name] = $colDesc;
} }
$tables[(string) $table->name] = $columns; $class = $table->class ? (string) $table->class:(string) $table->name;
$tables[(string) $table->name]['name'] = (string) $table->name;
$tables[(string) $table->name]['class'] = (string) $class;
$tables[(string) $table->name]['columns'] = $columns;
} }
return $tables; return $tables;
} }
} }

View File

@ -39,26 +39,40 @@
*/ */
class Doctrine_Import_Schema_Yml extends Doctrine_Import_Schema class Doctrine_Import_Schema_Yml extends Doctrine_Import_Schema
{ {
/**
* parse
*
* @param string $schema
* @return void
*/
public function parse($schema)
{
if (!is_readable($schema)) {
throw new Doctrine_Import_Exception('Could not read schema file '. $schema);
}
return array();
}
/** /**
* importSchema * parseSchema
* *
* A method to import a Yml Schema and translate it into a property array. * A method to parse a Yml Schema and translate it into a property array.
* The function returns that property array. * The function returns that property array.
* *
* @param string $schema Path to the file containing the XML schema * @param string $schema Path to the file containing the XML schema
* @return array * @return array
*/ */
public function importSchema($schema) public function parseSchema($schema)
{ {
if (!is_readable($schema)) { $array = $this->parse($schema);
throw new Doctrine_Import_Exception('Could not read schema file '. $schema);
} $tables = array();
// Need to figure out best way to have yaml loading/dumping in Doctrine // Not working yet
// $yamlArr = YamlLoad($schema); /*
// Go through all tables... // Go through all tables...
foreach ($yamlArr['table'] as $table) { foreach ($array['table'] as $table) {
// Go through all columns... // Go through all columns...
foreach ($table['declaration']['field'] as $field) { foreach ($table['declaration']['field'] as $field) {
$colDesc = array( $colDesc = array(
@ -79,6 +93,7 @@ class Doctrine_Import_Schema_Yml extends Doctrine_Import_Schema
$tables[(string) $table['name']] = $columns; $tables[(string) $table['name']] = $columns;
} }
*/
return $tables; return $tables;
} }

View File

@ -88,7 +88,7 @@ class Doctrine_Export_Record_TestCase extends Doctrine_UnitTestCase
public function testExportModelFromDirectory() public function testExportModelFromDirectory()
{ {
Doctrine::export(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files2'); Doctrine::exportSchema(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files2');
$this->assertEqual($this->adapter->pop(), 'COMMIT'); $this->assertEqual($this->adapter->pop(), 'COMMIT');
$this->assertEqual($this->adapter->pop(), 'ALTER TABLE cms__category_languages ADD CONSTRAINT FOREIGN KEY (category_id) REFERENCES cms__category(id) ON DELETE CASCADE'); $this->assertEqual($this->adapter->pop(), 'ALTER TABLE cms__category_languages ADD CONSTRAINT FOREIGN KEY (category_id) REFERENCES cms__category(id) ON DELETE CASCADE');

View File

@ -0,0 +1,35 @@
<?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
* <http://www.phpdoctrine.com>.
*/
/**
* Doctrine_Export_Schema_Xml_TestCase
*
* @package Doctrine
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @category Object Relational Mapping
* @link www.phpdoctrine.com
* @since 1.0
* @version $Revision$
*/
class Doctrine_Export_Schema_Xml_TestCase extends Doctrine_UnitTestCase
{
}

View File

@ -0,0 +1,35 @@
<?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
* <http://www.phpdoctrine.com>.
*/
/**
* Doctrine_Export_Schema_Yml_TestCase
*
* @package Doctrine
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @category Object Relational Mapping
* @link www.phpdoctrine.com
* @since 1.0
* @version $Revision$
*/
class Doctrine_Export_Schema_Yml_TestCase extends Doctrine_UnitTestCase
{
}

View File

@ -0,0 +1,52 @@
<?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
* <http://www.phpdoctrine.com>.
*/
/**
* Doctrine_Import_Schema_Xml_TestCase
*
* @package Doctrine
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @category Object Relational Mapping
* @link www.phpdoctrine.com
* @since 1.0
* @version $Revision$
*/
class Doctrine_Import_Schema_Xml_TestCase extends Doctrine_UnitTestCase
{
public function testXmlImport()
{
$import = new Doctrine_Import_Schema_Xml();
$import->importSchema('schema.xml', 'classes');
if (!file_exists('classes/User.class.php')) {
$this->fail();
} else {
unlink('classes/User.class.php');
}
if (!file_exists('classes/Group.class.php')) {
$this->fail();
} else {
unlink('classes/Group.class.php');
}
}
}

View File

@ -0,0 +1,52 @@
<?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
* <http://www.phpdoctrine.com>.
*/
/**
* Doctrine_Import_Schema_Yml_TestCase
*
* @package Doctrine
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @category Object Relational Mapping
* @link www.phpdoctrine.com
* @since 1.0
* @version $Revision$
*/
class Doctrine_Import_Schema_Yml_TestCase extends Doctrine_UnitTestCase
{
public function testYmlImport()
{
$import = new Doctrine_Import_Schema_Yml();
$import->importSchema('schema.yml', 'classes');
if (!file_exists('classes/User.class.php')) {
$this->fail();
} else {
unlink('classes/User.class.php');
}
if (!file_exists('classes/Group.class.php')) {
$this->fail();
} else {
unlink('classes/Group.class.php');
}
}
}

View File

@ -326,6 +326,15 @@ $test->addTestCase(new Doctrine_Query_Cache_TestCase());
$test->addTestCase(new Doctrine_Cache_Apc_TestCase()); $test->addTestCase(new Doctrine_Cache_Apc_TestCase());
$test->addTestCase(new Doctrine_Query_SelectExpression_TestCase()); $test->addTestCase(new Doctrine_Query_SelectExpression_TestCase());
$test->addTestCase(new Doctrine_Import_Schema_Yml_TestCase());
$test->addTestCase(new Doctrine_Import_Schema_Xml_TestCase());
$test->addTestCase(new Doctrine_Export_Schema_Yml_TestCase());
$test->addTestCase(new Doctrine_Export_Schema_Xml_TestCase());
/** /**
$test->addTestCase(new Doctrine_Cache_Memcache_TestCase()); $test->addTestCase(new Doctrine_Cache_Memcache_TestCase());

40
tests/schema.xml Executable file
View File

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="ISO-8859-1" ?>
<tables>
<table>
<name>user</name>
<class>User</class>
<declaration>
<field>
<name>id</name>
<type>integer</type>
<notnull>true</notnull>
<autoincrement>true</autoincrement>
</field>
<field>
<name>username</name>
<type>string</type>
<length>20</length>
<notnull>true</notnull>
</field>
</declaration>
</table>
<table>
<name>group</name>
<class>Group</class>
<declaration>
<field>
<name>id</name>
<type>integer</type>
<notnull>true</notnull>
<autoincrement>true</autoincrement>
</field>
<field>
<name>name</name>
<type>string</type>
<length>20</length>
<notnull>true</notnull>
</field>
</declaration>
</table>
</tables>

0
tests/schema.yml Normal file
View File