2007-03-07 13:55:35 +03:00
|
|
|
<?php
|
|
|
|
/*
|
2007-03-07 13:57:40 +03:00
|
|
|
* $Id$
|
2007-03-07 13:55:35 +03:00
|
|
|
*
|
|
|
|
* 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::autoload('Doctrine_Export');
|
|
|
|
/**
|
|
|
|
* Doctrine_Export_Frontbase
|
|
|
|
*
|
|
|
|
* @package Doctrine
|
|
|
|
* @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
|
|
|
|
* @category Object Relational Mapping
|
|
|
|
* @link www.phpdoctrine.com
|
|
|
|
* @since 1.0
|
2007-03-07 13:57:40 +03:00
|
|
|
* @version $Revision$
|
2007-03-07 13:55:35 +03:00
|
|
|
*/
|
|
|
|
class Doctrine_Export_Frontbase extends Doctrine_Export
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* create a new database
|
|
|
|
*
|
|
|
|
* @param string $name name of the database that should be created
|
2007-06-15 00:18:25 +04:00
|
|
|
* @return string
|
2007-03-07 13:55:35 +03:00
|
|
|
*/
|
2007-06-15 00:18:25 +04:00
|
|
|
public function createDatabaseSql($name)
|
2007-03-07 13:55:35 +03:00
|
|
|
{
|
|
|
|
$name = $this->conn->quoteIdentifier($name, true);
|
2007-06-15 00:18:25 +04:00
|
|
|
return 'CREATE DATABASE ' . $name;
|
2007-03-07 13:55:35 +03:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* drop an existing database
|
|
|
|
*
|
|
|
|
* @param string $name name of the database that should be dropped
|
2007-06-15 00:18:25 +04:00
|
|
|
* @return string
|
2007-03-07 13:55:35 +03:00
|
|
|
*/
|
2007-06-15 00:18:25 +04:00
|
|
|
public function dropDatabaseSql($name)
|
2007-03-07 13:55:35 +03:00
|
|
|
{
|
|
|
|
$name = $this->conn->quoteIdentifier($name, true);
|
2007-06-15 00:18:25 +04:00
|
|
|
return 'DELETE DATABASE ' . $name;
|
2007-03-07 13:55:35 +03:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* 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
|
2007-06-15 00:18:25 +04:00
|
|
|
* @return string
|
2007-03-07 13:55:35 +03:00
|
|
|
*/
|
2007-06-15 00:18:25 +04:00
|
|
|
public function dropTableSql($name)
|
2007-03-07 13:55:35 +03:00
|
|
|
{
|
|
|
|
$name = $this->conn->quoteIdentifier($name, true);
|
2007-06-15 00:18:25 +04:00
|
|
|
return 'DROP TABLE ' . $name . ' CASCADE';
|
2007-03-07 13:55:35 +03:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2007-09-19 23:33:00 +04:00
|
|
|
public function alterTable($name, array $changes, $check = false)
|
2007-03-07 13:55:35 +03:00
|
|
|
{
|
2007-09-03 18:57:18 +04:00
|
|
|
foreach ($changes as $changeName => $change) {
|
2007-03-07 13:55:35 +03:00
|
|
|
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 = '';
|
2007-09-03 18:57:18 +04:00
|
|
|
if ( ! empty($changes['name'])) {
|
2007-03-07 13:55:35 +03:00
|
|
|
$changeName = $this->conn->quoteIdentifier($changes['name'], true);
|
|
|
|
$query .= 'RENAME TO ' . $changeName;
|
|
|
|
}
|
|
|
|
|
2007-09-03 18:57:18 +04:00
|
|
|
if ( ! empty($changes['add']) && is_array($changes['add'])) {
|
2007-03-07 13:55:35 +03:00
|
|
|
foreach ($changes['add'] as $fieldName => $field) {
|
|
|
|
if ($query) {
|
|
|
|
$query.= ', ';
|
|
|
|
}
|
|
|
|
$query.= 'ADD ' . $this->conn->getDeclaration($field['type'], $fieldName, $field);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-03 18:57:18 +04:00
|
|
|
if ( ! empty($changes['remove']) && is_array($changes['remove'])) {
|
2007-03-07 13:55:35 +03:00
|
|
|
foreach ($changes['remove'] as $fieldName => $field) {
|
|
|
|
if ($query) {
|
|
|
|
$query.= ', ';
|
|
|
|
}
|
|
|
|
$fieldName = $this->conn->quoteIdentifier($fieldName, true);
|
|
|
|
$query.= 'DROP ' . $fieldName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$rename = array();
|
2007-09-03 18:57:18 +04:00
|
|
|
if ( ! empty($changes['rename']) && is_array($changes['rename'])) {
|
2007-03-07 13:55:35 +03:00
|
|
|
foreach ($changes['rename'] as $fieldName => $field) {
|
|
|
|
$rename[$field['name']] = $fieldName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-03 18:57:18 +04:00
|
|
|
if ( ! empty($changes['change']) && is_array($changes['change'])) {
|
2007-03-07 13:55:35 +03:00
|
|
|
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);
|
2007-05-18 13:35:00 +04:00
|
|
|
$query.= 'CHANGE ' . $oldFieldName . ' ' . $this->conn->getDeclaration($field['definition']['type'], $oldFieldName, $field['definition']);
|
2007-03-07 13:55:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-03 18:57:18 +04:00
|
|
|
if ( ! empty($rename) && is_array($rename)) {
|
2007-03-07 13:55:35 +03:00
|
|
|
foreach ($rename as $renamedFieldName => $renamed_field) {
|
|
|
|
if ($query) {
|
|
|
|
$query.= ', ';
|
|
|
|
}
|
|
|
|
$oldFieldName = $rename[$renamedFieldName];
|
|
|
|
$field = $changes['rename'][$oldFieldName];
|
|
|
|
$query.= 'CHANGE ' . $this->conn->getDeclaration($field['definition']['type'], $oldFieldName, $field['definition']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-03 18:57:18 +04:00
|
|
|
if ( ! $query) {
|
2007-03-07 13:55:35 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$name = $this->conn->quoteIdentifier($name, true);
|
2007-05-18 13:35:00 +04:00
|
|
|
return $this->conn->exec('ALTER TABLE ' . $name . ' ' . $query);
|
2007-03-07 13:55:35 +03:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* create sequence
|
|
|
|
*
|
|
|
|
* @param string $seqName name of the sequence to be created
|
|
|
|
* @param string $start start value of the sequence; default is 1
|
2007-06-14 19:01:58 +04:00
|
|
|
* @param array $options An associative array of table options:
|
|
|
|
* array(
|
|
|
|
* 'comment' => 'Foo',
|
|
|
|
* 'charset' => 'utf8',
|
|
|
|
* 'collate' => 'utf8_unicode_ci',
|
|
|
|
* );
|
|
|
|
* @return void
|
2007-03-07 13:55:35 +03:00
|
|
|
*/
|
2007-06-14 19:01:58 +04:00
|
|
|
public function createSequence($sequenceName, $start = 1, array $options = array())
|
2007-03-07 13:55:35 +03:00
|
|
|
{
|
2007-06-22 21:37:06 +04:00
|
|
|
$sequenceName = $this->conn->quoteIdentifier($this->conn->getSequenceName($sequenceName), true);
|
2007-03-07 13:55:35 +03:00
|
|
|
$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
|
2007-06-15 00:18:25 +04:00
|
|
|
* @return string
|
2007-03-07 13:55:35 +03:00
|
|
|
*/
|
2007-06-15 00:18:25 +04:00
|
|
|
public function dropSequenceSql($seqName)
|
2007-03-07 13:55:35 +03:00
|
|
|
{
|
|
|
|
$sequenceName = $this->conn->quoteIdentifier($this->conn->getSequenceName($seqName), true);
|
|
|
|
|
2007-06-15 00:18:25 +04:00
|
|
|
return 'DROP TABLE ' . $sequenceName . ' CASCADE';
|
2007-03-07 13:55:35 +03:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2007-06-15 00:18:25 +04:00
|
|
|
public function dropIndexSql($table, $name)
|
2007-03-07 13:55:35 +03:00
|
|
|
{
|
|
|
|
$table = $this->conn->quoteIdentifier($table, true);
|
|
|
|
$name = $this->conn->quoteIdentifier($this->conn->getIndexName($name), true);
|
|
|
|
|
2007-06-15 00:18:25 +04:00
|
|
|
return 'ALTER TABLE ' . $table . ' DROP INDEX ' . $name;
|
2007-03-07 13:55:35 +03:00
|
|
|
}
|
|
|
|
}
|