Author: romanb Date: 2008-09-12 10:45:56 +0100 (Fri, 12 Sep 2008) New Revision: 4922
Added: trunk/lib/Doctrine/ORM/Export/Frontbase.php trunk/lib/Doctrine/ORM/Export/Reporter.php trunk/lib/Doctrine/ORM/Export/Schema.php Removed: trunk/lib/Doctrine/Export/ Log: moved ORM export Added: trunk/lib/Doctrine/ORM/Export/Frontbase.php =================================================================== --- trunk/lib/Doctrine/ORM/Export/Frontbase.php (rev 0) +++ trunk/lib/Doctrine/ORM/Export/Frontbase.php 2008-09-12 09:45:56 UTC (rev 4922) @@ -0,0 +1,316 @@ +<?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.org>. + */ + +/** + * Doctrine_Export_Frontbase + * + * @package Doctrine + * @subpackage Export + * @author Konsta Vesterinen <[EMAIL PROTECTED]> + * @author Lukas Smith <[EMAIL PROTECTED]> (PEAR MDB2 library) + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @link www.phpdoctrine.org + * @since 1.0 + * @version $Revision$ + */ +class Doctrine_Export_Frontbase extends Doctrine_Export +{ + /** + * create a new database + * + * @param string $name name of the database that should be created + * @return string + */ + public function createDatabaseSql($name) + { + $name = $this->conn->quoteIdentifier($name, true); + return 'CREATE DATABASE ' . $name; + } + + /** + * drop an existing database + * + * @param string $name name of the database that should be dropped + * @return string + */ + public function dropDatabaseSql($name) + { + $name = $this->conn->quoteIdentifier($name, true); + return 'DELETE DATABASE ' . $name; + } + + /** + * drop an existing table + * + * @param object $this->conns database object that is extended by this class + * @param string $name name of the table that should be dropped + * @return string + */ + public function dropTableSql($name) + { + $name = $this->conn->quoteIdentifier($name, true); + return 'DROP TABLE ' . $name . ' CASCADE'; + } + + /** + * alter an existing table + * + * @param string $name name of the table that is intended to be changed. + * @param array $changes associative array that contains the details of each type + * of change that is intended to be performed. The types of + * changes that are currently supported are defined as follows: + * + * name + * + * New name for the table. + * + * add + * + * Associative array with the names of fields to be added as + * indexes of the array. The value of each entry of the array + * should be set to another associative array with the properties + * of the fields to be added. The properties of the fields should + * be the same as defined by the MDB2 parser. + * + * + * remove + * + * Associative array with the names of fields to be removed as indexes + * of the array. Currently the values assigned to each entry are ignored. + * An empty array should be used for future compatibility. + * + * rename + * + * Associative array with the names of fields to be renamed as indexes + * of the array. The value of each entry of the array should be set to + * another associative array with the entry named name with the new + * field name and the entry named Declaration that is expected to contain + * the portion of the field declaration already in DBMS specific SQL code + * as it is used in the CREATE TABLE statement. + * + * change + * + * Associative array with the names of the fields to be changed as indexes + * of the array. Keep in mind that if it is intended to change either the + * name of a field and any other properties, the change array entries + * should have the new names of the fields as array indexes. + * + * The value of each entry of the array should be set to another associative + * array with the properties of the fields to that are meant to be changed as + * array entries. These entries should be assigned to the new values of the + * respective properties. The properties of the fields should be the same + * as defined by the MDB2 parser. + * + * Example + * array( + * 'name' => 'userlist', + * 'add' => array( + * 'quota' => array( + * 'type' => 'integer', + * 'unsigned' => 1 + * ) + * ), + * 'remove' => array( + * 'file_limit' => array(), + * 'time_limit' => array() + * ), + * 'change' => array( + * 'name' => array( + * 'length' => '20', + * 'definition' => array( + * 'type' => 'text', + * 'length' => 20, + * ), + * ) + * ), + * 'rename' => array( + * 'sex' => array( + * 'name' => 'gender', + * 'definition' => array( + * 'type' => 'text', + * 'length' => 1, + * 'default' => 'M', + * ), + * ) + * ) + * ) + * + * @param boolean $check indicates whether the function should just check if the DBMS driver + * can perform the requested table alterations if the value is true or + * actually perform them otherwise. + * @access public + * + * @return boolean + */ + public function alterTable($name, array $changes, $check = false) + { + foreach ($changes as $changeName => $change) { + switch ($changeName) { + case 'add': + case 'remove': + case 'change': + case 'rename': + case 'name': + break; + default: + throw new Doctrine_Export_Exception('change type "'.$changeName.'" not yet supported'); + } + } + + if ($check) { + return true; + } + + $query = ''; + if ( ! empty($changes['name'])) { + $changeName = $this->conn->quoteIdentifier($changes['name'], true); + $query .= 'RENAME TO ' . $changeName; + } + + if ( ! empty($changes['add']) && is_array($changes['add'])) { + foreach ($changes['add'] as $fieldName => $field) { + if ($query) { + $query.= ', '; + } + $query.= 'ADD ' . $this->conn->getDeclaration($fieldName, $field); + } + } + + if ( ! empty($changes['remove']) && is_array($changes['remove'])) { + foreach ($changes['remove'] as $fieldName => $field) { + if ($query) { + $query.= ', '; + } + $fieldName = $this->conn->quoteIdentifier($fieldName, true); + $query.= 'DROP ' . $fieldName; + } + } + + $rename = array(); + if ( ! empty($changes['rename']) && is_array($changes['rename'])) { + foreach ($changes['rename'] as $fieldName => $field) { + $rename[$field['name']] = $fieldName; + } + } + + if ( ! empty($changes['change']) && is_array($changes['change'])) { + foreach ($changes['change'] as $fieldName => $field) { + if ($query) { + $query.= ', '; + } + if (isset($rename[$fieldName])) { + $oldFieldName = $rename[$fieldName]; + unset($rename[$fieldName]); + } else { + $oldFieldName = $fieldName; + } + $oldFieldName = $this->conn->quoteIdentifier($oldFieldName, true); + $query.= 'CHANGE ' . $oldFieldName . ' ' . $this->conn->getDeclaration($oldFieldName, $field['definition']); + } + } + + if ( ! empty($rename) && is_array($rename)) { + foreach ($rename as $renamedFieldName => $renamed_field) { + if ($query) { + $query.= ', '; + } + $oldFieldName = $rename[$renamedFieldName]; + $field = $changes['rename'][$oldFieldName]; + $query.= 'CHANGE ' . $this->conn->getDeclaration($oldFieldName, $field['definition']); + } + } + + if ( ! $query) { + return true; + } + + $name = $this->conn->quoteIdentifier($name, true); + return $this->conn->exec('ALTER TABLE ' . $name . ' ' . $query); + } + + /** + * create sequence + * + * @param string $seqName name of the sequence to be created + * @param string $start start value of the sequence; default is 1 + * @param array $options An associative array of table options: + * array( + * 'comment' => 'Foo', + * 'charset' => 'utf8', + * 'collate' => 'utf8_unicode_ci', + * ); + * @return void + */ + public function createSequence($sequenceName, $start = 1, array $options = array()) + { + $sequenceName = $this->conn->quoteIdentifier($this->conn->getSequenceName($sequenceName), true); + $seqcolName = $this->conn->quoteIdentifier($this->conn->getAttribute(Doctrine::ATTR_SEQCOL_NAME), true); + + $query = 'CREATE TABLE ' . $sequenceName . ' (' . $seqcolName . ' INTEGER DEFAULT UNIQUE, PRIMARY KEY(' . $seqcolName . '))'; + $res = $this->conn->exec($query); + $res = $this->conn->exec('SET UNIQUE = 1 FOR ' . $sequenceName); + + if ($start == 1) { + return true; + } + + try { + $this->conn->exec('INSERT INTO ' . $sequenceName . ' (' . $seqcolName . ') VALUES (' . ($start-1) . ')'); + } catch(Doctrine_Connection_Exception $e) { + // Handle error + try { + $this->conn->exec('DROP TABLE ' . $sequenceName); + } catch(Doctrine_Connection_Exception $e) { + throw new Doctrine_Export_Exception('could not drop inconsistent sequence table'); + } + + throw new Doctrine_Export_Exception('could not create sequence table'); + } + } + + /** + * drop existing sequence + * + * @param string $seqName name of the sequence to be dropped + * @return string + */ + public function dropSequenceSql($seqName) + { + $sequenceName = $this->conn->quoteIdentifier($this->conn->getSequenceName($seqName), true); + + return 'DROP TABLE ' . $sequenceName . ' CASCADE'; + } + + /** + * drop existing index + * + * @param string $table name of table that should be used in method + * @param string $name name of the index to be dropped + * @return boolean + */ + public function dropIndexSql($table, $name) + { + $table = $this->conn->quoteIdentifier($table, true); + $name = $this->conn->quoteIdentifier($this->conn->getIndexName($name), true); + + return 'ALTER TABLE ' . $table . ' DROP INDEX ' . $name; + } +} \ No newline at end of file Added: trunk/lib/Doctrine/ORM/Export/Reporter.php =================================================================== --- trunk/lib/Doctrine/ORM/Export/Reporter.php (rev 0) +++ trunk/lib/Doctrine/ORM/Export/Reporter.php 2008-09-12 09:45:56 UTC (rev 4922) @@ -0,0 +1,45 @@ +<?php +/* + * $Id: Reporter.php 3882 2008-02-22 18:11:35Z jwage $ + * + * 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.org>. + */ + +/** + * Doctrine_Export_Reporter + * + * @package Doctrine + * @subpackage Export + * @author Konsta Vesterinen <[EMAIL PROTECTED]> + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @link www.phpdoctrine.org + * @since 1.0 + * @version $Revision: 3882 $ + */ +class Doctrine_Export_Reporter implements IteratorAggregate { + protected $messages = array(); + + public function add($code, $message) { + $this->messages[] = array($code, $message); + } + public function pop() { + return array_pop($this->messages); + } + public function getIterator() { + return new ArrayIterator($this->messages); + } +} \ No newline at end of file Added: trunk/lib/Doctrine/ORM/Export/Schema.php =================================================================== --- trunk/lib/Doctrine/ORM/Export/Schema.php (rev 0) +++ trunk/lib/Doctrine/ORM/Export/Schema.php 2008-09-12 09:45:56 UTC (rev 4922) @@ -0,0 +1,131 @@ +<?php +/* + * $Id: Schema.php 1838 2007-06-26 00:58:21Z nicobn $ + * + * 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.org>. + */ + +/** + * Doctrine_Export_Schema + * + * Used for exporting a schema to a yaml file + * + * @package Doctrine + * @subpackage Export + * @link www.phpdoctrine.org + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @version $Revision: 1838 $ + * @author Nicolas Bérard-Nault <[EMAIL PROTECTED]> + * @author Jonathan H. Wage <[EMAIL PROTECTED]> + */ +class Doctrine_Export_Schema +{ + /** + * buildSchema + * + * Build schema array that can be dumped to file + * + * @param string $directory + * @return void + */ + public function buildSchema($directory = null, $models = array()) + { + if ($directory) { + $loadedModels = Doctrine::loadModels($directory); + } else { + $loadedModels = Doctrine::getLoadedModels(); + } + + $array = array(); + + $parent = new ReflectionClass('Doctrine_Entity'); + + $sql = array(); + $fks = array(); + + // we iterate trhough the diff of previously declared classes + // and currently declared classes + foreach ($loadedModels as $className) { + if ( ! empty($models) && !in_array($className, $models)) { + continue; + } + + $record = new $className(); + $recordTable = $record->getTable(); + + $data = $recordTable->getExportableFormat(); + + $table = array(); + + foreach ($data['columns'] AS $name => $column) { + $data['columns'][$name]['type'] = $column['type'] . '(' . $column['length'] . ')'; + unset($data['columns'][$name]['length']); + } + + $table['columns'] = $data['columns']; + + $relations = $recordTable->getRelations(); + foreach ($relations as $key => $relation) { + $relationData = $relation->toArray(); + + $relationKey = $relationData['alias']; + + if (isset($relationData['refTable']) && $relationData['refTable']) { + $table['relations'][$relationKey]['refClass'] = $relationData['refTable']->getComponentName(); + } + + if (isset($relationData['class']) && $relationData['class'] && $relation['class'] != $relationKey) { + $table['relations'][$relationKey]['class'] = $relationData['class']; + } + + $table['relations'][$relationKey]['local'] = $relationData['local']; + $table['relations'][$relationKey]['foreign'] = $relationData['foreign']; + + if ($relationData['type'] === Doctrine_Relation::ONE) { + $table['relations'][$relationKey]['type'] = 'one'; + } else if($relationData['type'] === Doctrine_Relation::MANY) { + $table['relations'][$relationKey]['type'] = 'many'; + } else { + $table['relations'][$relationKey]['type'] = 'one'; + } + } + + $array[$className] = $table; + } + + return $array; + } + + /** + * exportSchema + * + * @param string $schema + * @param string $directory + * @return string $string of data in the specified format + * @return void + */ + public function exportSchema($schema, $format = 'yml', $directory = null, $models = array()) + { + $array = $this->buildSchema($directory, $models); + + if (is_dir($schema)) { + $schema = $schema . DIRECTORY_SEPARATOR . 'schema.' . $format; + } + + return Doctrine_Parser::dump($array, $format, $schema); + } +} \ No newline at end of file --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "doctrine-svn" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.co.uk/group/doctrine-svn?hl=en-GB -~----------~----~----~----~------~----~------~--~---
