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
|
|
|
|
* <http://www.phpdoctrine.com>.
|
|
|
|
*/
|
|
|
|
Doctrine::autoload('Doctrine_Connection_Module');
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* @link www.phpdoctrine.com
|
|
|
|
* @since 1.0
|
|
|
|
* @version $Revision$
|
|
|
|
*/
|
|
|
|
class Doctrine_Export extends Doctrine_Connection_Module
|
|
|
|
{
|
|
|
|
protected $valid_default_values = array(
|
|
|
|
'text' => '',
|
|
|
|
'boolean' => true,
|
|
|
|
'integer' => 0,
|
|
|
|
'decimal' => 0.0,
|
|
|
|
'float' => 0.0,
|
|
|
|
'timestamp' => '1970-01-01 00:00:00',
|
|
|
|
'time' => '00:00:00',
|
|
|
|
'date' => '1970-01-01',
|
|
|
|
'clob' => '',
|
|
|
|
'blob' => '',
|
|
|
|
'string' => ''
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* drop an existing database
|
|
|
|
* (this method is implemented by the drivers)
|
|
|
|
*
|
|
|
|
* @param string $name name of the database that should be dropped
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function dropDatabase($database)
|
|
|
|
{
|
|
|
|
$this->conn->execute($this->dropDatabaseSql($database));
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
/**
|
|
|
|
* drop an existing database
|
|
|
|
* (this method is implemented by the drivers)
|
|
|
|
*
|
|
|
|
* @param string $name name of the database that should be dropped
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function dropDatabaseSql($database)
|
|
|
|
{
|
|
|
|
throw new Doctrine_Export_Exception('Drop database not supported by this driver.');
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
/**
|
|
|
|
* dropTableSql
|
|
|
|
* drop an existing table
|
|
|
|
*
|
|
|
|
* @param string $table name of table that should be dropped from the database
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function dropTableSql($table)
|
|
|
|
{
|
|
|
|
return 'DROP TABLE ' . $this->conn->quoteIdentifier($table);
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
/**
|
|
|
|
* dropTable
|
|
|
|
* drop an existing table
|
|
|
|
*
|
|
|
|
* @param string $table name of table that should be dropped from the database
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function dropTable($table)
|
|
|
|
{
|
|
|
|
$this->conn->execute($this->dropTableSql($table));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 void
|
|
|
|
*/
|
|
|
|
public function dropIndex($table, $name)
|
|
|
|
{
|
|
|
|
return $this->conn->exec($this->dropIndexSql($table, $name));
|
|
|
|
}
|
2007-09-03 14:41:51 +04:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
/**
|
|
|
|
* dropIndexSql
|
|
|
|
*
|
|
|
|
* @param string $table name of table that should be used in method
|
|
|
|
* @param string $name name of the index to be dropped
|
|
|
|
* @return string SQL that is used for dropping an index
|
|
|
|
*/
|
2007-09-03 14:41:51 +04:00
|
|
|
public function dropIndexSql($table, $name)
|
2007-07-21 19:17:17 +04:00
|
|
|
{
|
|
|
|
$name = $this->conn->quoteIdentifier($this->conn->formatter->getIndexName($name));
|
2007-10-20 02:11:45 +04:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
return 'DROP INDEX ' . $name;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
/**
|
|
|
|
* drop existing constraint
|
|
|
|
*
|
|
|
|
* @param string $table name of table that should be used in method
|
|
|
|
* @param string $name name of the constraint to be dropped
|
|
|
|
* @param string $primary hint if the constraint is primary
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function dropConstraint($table, $name, $primary = false)
|
|
|
|
{
|
|
|
|
$table = $this->conn->quoteIdentifier($table);
|
2007-10-20 18:57:47 +04:00
|
|
|
$name = $this->conn->quoteIdentifier($name);
|
2007-10-20 02:11:45 +04:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
return $this->conn->exec('ALTER TABLE ' . $table . ' DROP CONSTRAINT ' . $name);
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-10-20 18:57:47 +04:00
|
|
|
/**
|
|
|
|
* drop existing foreign key
|
|
|
|
*
|
|
|
|
* @param string $table name of table that should be used in method
|
|
|
|
* @param string $name name of the foreign key to be dropped
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function dropForeignKey($table, $name)
|
|
|
|
{
|
|
|
|
return $this->dropConstraint($table, $name);
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
/**
|
|
|
|
* dropSequenceSql
|
|
|
|
* drop existing sequence
|
|
|
|
* (this method is implemented by the drivers)
|
|
|
|
*
|
|
|
|
* @throws Doctrine_Connection_Exception if something fails at database level
|
|
|
|
* @param string $sequenceName name of the sequence to be dropped
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function dropSequence($sequenceName)
|
|
|
|
{
|
|
|
|
$this->conn->exec($this->dropSequenceSql($sequenceName));
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
/**
|
|
|
|
* dropSequenceSql
|
|
|
|
* drop existing sequence
|
|
|
|
*
|
|
|
|
* @throws Doctrine_Connection_Exception if something fails at database level
|
|
|
|
* @param string $sequenceName name of the sequence to be dropped
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function dropSequenceSql($sequenceName)
|
|
|
|
{
|
|
|
|
throw new Doctrine_Export_Exception('Drop sequence not supported by this driver.');
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
/**
|
|
|
|
* create a new database
|
|
|
|
* (this method is implemented by the drivers)
|
|
|
|
*
|
|
|
|
* @param string $name name of the database that should be created
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function createDatabase($database)
|
|
|
|
{
|
|
|
|
$this->conn->execute($this->createDatabaseSql($database));
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
/**
|
|
|
|
* create a new database
|
|
|
|
* (this method is implemented by the drivers)
|
|
|
|
*
|
|
|
|
* @param string $name name of the database that should be created
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function createDatabaseSql($database)
|
|
|
|
{
|
|
|
|
throw new Doctrine_Export_Exception('Create database not supported by this driver.');
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
/**
|
|
|
|
* create a new table
|
|
|
|
*
|
|
|
|
* @param string $name Name of the database that should be created
|
|
|
|
* @param array $fields Associative array that contains the definition of each field of the new table
|
|
|
|
* The indexes of the array entries are the names of the fields of the table an
|
|
|
|
* the array entry values are associative arrays like those that are meant to be
|
|
|
|
* passed with the field definitions to get[Type]Declaration() functions.
|
|
|
|
* array(
|
|
|
|
* 'id' => array(
|
|
|
|
* 'type' => 'integer',
|
|
|
|
* 'unsigned' => 1
|
|
|
|
* 'notnull' => 1
|
|
|
|
* 'default' => 0
|
|
|
|
* ),
|
|
|
|
* 'name' => array(
|
|
|
|
* 'type' => 'text',
|
|
|
|
* 'length' => 12
|
|
|
|
* ),
|
|
|
|
* 'password' => array(
|
|
|
|
* 'type' => 'text',
|
|
|
|
* 'length' => 12
|
|
|
|
* )
|
|
|
|
* );
|
|
|
|
* @param array $options An associative array of table options:
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2007-08-30 01:51:49 +04:00
|
|
|
public function createTableSql($name, array $fields, array $options = array())
|
2007-07-21 19:17:17 +04:00
|
|
|
{
|
|
|
|
if ( ! $name) {
|
|
|
|
throw new Doctrine_Export_Exception('no valid table name specified');
|
|
|
|
}
|
2007-09-03 14:41:51 +04:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
if (empty($fields)) {
|
|
|
|
throw new Doctrine_Export_Exception('no fields specified for table ' . $name);
|
|
|
|
}
|
|
|
|
|
|
|
|
$queryFields = $this->getFieldDeclarationList($fields);
|
|
|
|
|
|
|
|
|
|
|
|
if (isset($options['primary']) && ! empty($options['primary'])) {
|
|
|
|
$queryFields .= ', PRIMARY KEY(' . implode(', ', array_values($options['primary'])) . ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($options['indexes']) && ! empty($options['indexes'])) {
|
|
|
|
foreach($options['indexes'] as $index => $definition) {
|
|
|
|
$queryFields .= ', ' . $this->getIndexDeclaration($index, $definition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-25 03:03:25 +04:00
|
|
|
$query = 'CREATE TABLE ' . $this->conn->quoteIdentifier($name, true) . ' (' . $queryFields;
|
2007-09-03 14:41:51 +04:00
|
|
|
|
2007-07-24 03:44:14 +04:00
|
|
|
$check = $this->getCheckDeclaration($fields);
|
2007-09-03 14:41:51 +04:00
|
|
|
|
2007-07-24 03:44:14 +04:00
|
|
|
if ( ! empty($check)) {
|
|
|
|
$query .= ', ' . $check;
|
|
|
|
}
|
|
|
|
|
|
|
|
$query .= ')';
|
|
|
|
|
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
|
|
|
|
$sql[] = $query;
|
|
|
|
|
2007-08-30 01:51:49 +04:00
|
|
|
if (isset($options['foreignKeys'])) {
|
2007-07-21 19:17:17 +04:00
|
|
|
|
|
|
|
foreach ((array) $options['foreignKeys'] as $k => $definition) {
|
|
|
|
if (is_array($definition)) {
|
|
|
|
$sql[] = $this->createForeignKeySql($name, $definition);
|
|
|
|
}
|
|
|
|
}
|
2007-09-03 14:41:51 +04:00
|
|
|
}
|
2007-07-21 19:17:17 +04:00
|
|
|
return $sql;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
/**
|
|
|
|
* create a new table
|
|
|
|
*
|
|
|
|
* @param string $name Name of the database that should be created
|
|
|
|
* @param array $fields Associative array that contains the definition of each field of the new table
|
|
|
|
* @param array $options An associative array of table options:
|
|
|
|
* @see Doctrine_Export::createTableSql()
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function createTable($name, array $fields, array $options = array())
|
|
|
|
{
|
2007-09-03 18:57:18 +04:00
|
|
|
$sql = (array) $this->createTableSql($name, $fields, $options);
|
2007-07-21 19:17:17 +04:00
|
|
|
|
|
|
|
foreach ($sql as $query) {
|
|
|
|
$this->conn->execute($query);
|
|
|
|
}
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
/**
|
|
|
|
* create sequence
|
|
|
|
*
|
|
|
|
* @throws Doctrine_Connection_Exception if something fails at database level
|
|
|
|
* @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',
|
2007-09-03 14:41:51 +04:00
|
|
|
* );
|
2007-07-21 19:17:17 +04:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function createSequence($seqName, $start = 1, array $options = array())
|
|
|
|
{
|
|
|
|
return $this->conn->execute($this->createSequenceSql($seqName, $start = 1, $options));
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
/**
|
|
|
|
* return RDBMS specific create sequence statement
|
|
|
|
* (this method is implemented by the drivers)
|
|
|
|
*
|
|
|
|
* @throws Doctrine_Connection_Exception if something fails at database level
|
|
|
|
* @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 string
|
|
|
|
*/
|
|
|
|
public function createSequenceSql($seqName, $start = 1, array $options = array())
|
|
|
|
{
|
|
|
|
throw new Doctrine_Export_Exception('Create sequence not supported by this driver.');
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
/**
|
|
|
|
* create a constraint on a table
|
|
|
|
*
|
|
|
|
* @param string $table name of the table on which the constraint is to be created
|
|
|
|
* @param string $name name of the constraint to be created
|
|
|
|
* @param array $definition associative array that defines properties of the constraint to be created.
|
|
|
|
* Currently, only one property named FIELDS is supported. This property
|
|
|
|
* is also an associative with the names of the constraint fields as array
|
|
|
|
* constraints. Each entry of this array is set to another type of associative
|
|
|
|
* array that specifies properties of the constraint that are specific to
|
|
|
|
* each field.
|
|
|
|
*
|
|
|
|
* Example
|
|
|
|
* array(
|
|
|
|
* 'fields' => array(
|
|
|
|
* 'user_name' => array(),
|
|
|
|
* 'last_login' => array()
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function createConstraint($table, $name, $definition)
|
|
|
|
{
|
2007-10-20 02:11:45 +04:00
|
|
|
$sql = $this->createConstraintSql($table, $name, $definition);
|
|
|
|
|
|
|
|
return $this->conn->exec($sql);
|
2007-07-21 19:17:17 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
/**
|
|
|
|
* create a constraint on a table
|
|
|
|
*
|
|
|
|
* @param string $table name of the table on which the constraint is to be created
|
|
|
|
* @param string $name name of the constraint to be created
|
|
|
|
* @param array $definition associative array that defines properties of the constraint to be created.
|
|
|
|
* Currently, only one property named FIELDS is supported. This property
|
|
|
|
* is also an associative with the names of the constraint fields as array
|
|
|
|
* constraints. Each entry of this array is set to another type of associative
|
|
|
|
* array that specifies properties of the constraint that are specific to
|
|
|
|
* each field.
|
|
|
|
*
|
|
|
|
* Example
|
|
|
|
* array(
|
|
|
|
* 'fields' => array(
|
|
|
|
* 'user_name' => array(),
|
|
|
|
* 'last_login' => array()
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function createConstraintSql($table, $name, $definition)
|
|
|
|
{
|
|
|
|
$table = $this->conn->quoteIdentifier($table);
|
|
|
|
$name = $this->conn->quoteIdentifier($this->conn->formatter->getIndexName($name));
|
|
|
|
$query = 'ALTER TABLE ' . $table . ' ADD CONSTRAINT ' . $name;
|
|
|
|
|
|
|
|
if (isset($definition['primary']) && $definition['primary']) {
|
|
|
|
$query .= ' PRIMARY KEY';
|
|
|
|
} elseif (isset($definition['unique']) && $definition['unique']) {
|
|
|
|
$query .= ' UNIQUE';
|
|
|
|
}
|
|
|
|
|
|
|
|
$fields = array();
|
|
|
|
foreach (array_keys($definition['fields']) as $field) {
|
|
|
|
$fields[] = $this->conn->quoteIdentifier($field, true);
|
|
|
|
}
|
|
|
|
$query .= ' ('. implode(', ', $fields) . ')';
|
|
|
|
|
|
|
|
return $query;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
/**
|
|
|
|
* Get the stucture of a field into an array
|
|
|
|
*
|
|
|
|
* @param string $table name of the table on which the index is to be created
|
|
|
|
* @param string $name name of the index to be created
|
|
|
|
* @param array $definition associative array that defines properties of the index to be created.
|
|
|
|
* Currently, only one property named FIELDS is supported. This property
|
|
|
|
* is also an associative with the names of the index fields as array
|
|
|
|
* indexes. Each entry of this array is set to another type of associative
|
|
|
|
* array that specifies properties of the index that are specific to
|
|
|
|
* each field.
|
|
|
|
*
|
|
|
|
* Currently, only the sorting property is supported. It should be used
|
|
|
|
* to define the sorting direction of the index. It may be set to either
|
|
|
|
* ascending or descending.
|
|
|
|
*
|
|
|
|
* Not all DBMS support index sorting direction configuration. The DBMS
|
|
|
|
* drivers of those that do not support it ignore this property. Use the
|
|
|
|
* function supports() to determine whether the DBMS driver can manage indexes.
|
|
|
|
*
|
|
|
|
* Example
|
|
|
|
* array(
|
|
|
|
* 'fields' => array(
|
|
|
|
* 'user_name' => array(
|
|
|
|
* 'sorting' => 'ascending'
|
|
|
|
* ),
|
|
|
|
* 'last_login' => array()
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function createIndex($table, $name, array $definition)
|
|
|
|
{
|
|
|
|
return $this->conn->execute($this->createIndexSql($table, $name, $definition));
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
/**
|
|
|
|
* Get the stucture of a field into an array
|
|
|
|
*
|
|
|
|
* @param string $table name of the table on which the index is to be created
|
|
|
|
* @param string $name name of the index to be created
|
|
|
|
* @param array $definition associative array that defines properties of the index to be created.
|
|
|
|
* @see Doctrine_Export::createIndex()
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function createIndexSql($table, $name, array $definition)
|
|
|
|
{
|
|
|
|
$table = $this->conn->quoteIdentifier($table);
|
|
|
|
$name = $this->conn->quoteIdentifier($name);
|
|
|
|
$type = '';
|
|
|
|
|
2007-09-03 18:57:18 +04:00
|
|
|
if (isset($definition['type'])) {
|
2007-07-21 19:17:17 +04:00
|
|
|
switch (strtolower($definition['type'])) {
|
|
|
|
case 'unique':
|
|
|
|
$type = strtoupper($definition['type']) . ' ';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Doctrine_Export_Exception('Unknown index type ' . $definition['type']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$query = 'CREATE ' . $type . 'INDEX ' . $name . ' ON ' . $table;
|
|
|
|
|
|
|
|
$fields = array();
|
|
|
|
foreach ($definition['fields'] as $field) {
|
|
|
|
$fields[] = $this->conn->quoteIdentifier($field);
|
|
|
|
}
|
|
|
|
$query .= ' (' . implode(', ', $fields) . ')';
|
|
|
|
|
|
|
|
return $query;
|
2007-10-16 21:05:52 +04:00
|
|
|
}
|
2007-07-21 19:17:17 +04:00
|
|
|
/**
|
|
|
|
* createForeignKeySql
|
|
|
|
*
|
|
|
|
* @param string $table name of the table on which the foreign key is to be created
|
|
|
|
* @param array $definition associative array that defines properties of the foreign key to be created.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function createForeignKeySql($table, array $definition)
|
|
|
|
{
|
|
|
|
$table = $this->conn->quoteIdentifier($table);
|
|
|
|
|
2007-10-17 23:09:48 +04:00
|
|
|
$query = 'ALTER TABLE ' . $table . ' ADD ' . $this->getForeignKeyDeclaration($definition);
|
2007-07-21 19:17:17 +04:00
|
|
|
|
|
|
|
return $query;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-10-16 21:05:52 +04:00
|
|
|
/**
|
|
|
|
* createForeignKey
|
|
|
|
*
|
|
|
|
* @param string $table name of the table on which the foreign key is to be created
|
|
|
|
* @param array $definition associative array that defines properties of the foreign key to be created.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function createForeignKey($table, array $definition)
|
|
|
|
{
|
2007-10-20 02:11:45 +04:00
|
|
|
$sql = $this->createForeignKeySql($table, $definition);
|
|
|
|
|
|
|
|
return $this->conn->execute($sql);
|
2007-10-16 21:05:52 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
/**
|
|
|
|
* alter an existing table
|
|
|
|
* (this method is implemented by the drivers)
|
|
|
|
*
|
|
|
|
* @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.
|
|
|
|
* @return void
|
|
|
|
*/
|
2007-09-19 23:33:00 +04:00
|
|
|
public function alterTable($name, array $changes, $check = false)
|
2007-07-21 19:17:17 +04:00
|
|
|
{
|
2007-09-19 23:33:00 +04:00
|
|
|
$sql = $this->alterTableSql($name, $changes, $check);
|
|
|
|
|
|
|
|
if (is_string($sql) && $sql) {
|
|
|
|
$this->conn->execute($sql);
|
|
|
|
}
|
2007-07-21 19:17:17 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
/**
|
|
|
|
* generates the sql for altering an existing table
|
|
|
|
* (this method is implemented by the drivers)
|
|
|
|
*
|
|
|
|
* @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 *
|
|
|
|
* @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.
|
|
|
|
* @see Doctrine_Export::alterTable()
|
|
|
|
* @return string
|
|
|
|
*/
|
2007-09-19 23:33:00 +04:00
|
|
|
public function alterTableSql($name, array $changes, $check = false)
|
2007-07-21 19:17:17 +04:00
|
|
|
{
|
|
|
|
throw new Doctrine_Export_Exception('Alter table not supported by this driver.');
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
/**
|
|
|
|
* Get declaration of a number of field in bulk
|
|
|
|
*
|
|
|
|
* @param array $fields a multidimensional associative array.
|
|
|
|
* The first dimension determines the field name, while the second
|
|
|
|
* dimension is keyed with the name of the properties
|
|
|
|
* of the field being declared as array indexes. Currently, the types
|
|
|
|
* of supported field properties are as follows:
|
|
|
|
*
|
|
|
|
* length
|
|
|
|
* Integer value that determines the maximum length of the text
|
|
|
|
* field. If this argument is missing the field should be
|
|
|
|
* declared to have the longest length allowed by the DBMS.
|
|
|
|
*
|
|
|
|
* default
|
|
|
|
* Text value to be used as default for this field.
|
|
|
|
*
|
|
|
|
* notnull
|
|
|
|
* Boolean flag that indicates whether this field is constrained
|
|
|
|
* to not be set to null.
|
|
|
|
* charset
|
|
|
|
* Text value with the default CHARACTER SET for this field.
|
|
|
|
* collation
|
|
|
|
* Text value with the default COLLATION for this field.
|
|
|
|
* unique
|
|
|
|
* unique constraint
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getFieldDeclarationList(array $fields)
|
|
|
|
{
|
|
|
|
foreach ($fields as $fieldName => $field) {
|
|
|
|
$query = $this->getDeclaration($fieldName, $field);
|
|
|
|
|
|
|
|
$queryFields[] = $query;
|
|
|
|
}
|
|
|
|
return implode(', ', $queryFields);
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
/**
|
|
|
|
* Obtain DBMS specific SQL code portion needed to declare a generic type
|
|
|
|
* field to be used in statements like CREATE TABLE.
|
|
|
|
*
|
|
|
|
* @param string $name name the field to be declared.
|
|
|
|
* @param array $field associative array with the name of the properties
|
|
|
|
* of the field being declared as array indexes. Currently, the types
|
|
|
|
* of supported field properties are as follows:
|
|
|
|
*
|
|
|
|
* length
|
|
|
|
* Integer value that determines the maximum length of the text
|
|
|
|
* field. If this argument is missing the field should be
|
|
|
|
* declared to have the longest length allowed by the DBMS.
|
|
|
|
*
|
|
|
|
* default
|
|
|
|
* Text value to be used as default for this field.
|
|
|
|
*
|
|
|
|
* notnull
|
|
|
|
* Boolean flag that indicates whether this field is constrained
|
|
|
|
* to not be set to null.
|
|
|
|
* charset
|
|
|
|
* Text value with the default CHARACTER SET for this field.
|
|
|
|
* collation
|
|
|
|
* Text value with the default COLLATION for this field.
|
|
|
|
* unique
|
|
|
|
* unique constraint
|
2007-09-03 14:41:51 +04:00
|
|
|
* check
|
2007-07-21 19:17:17 +04:00
|
|
|
* column check constraint
|
|
|
|
*
|
|
|
|
* @return string DBMS specific SQL code portion that should be used to
|
|
|
|
* declare the specified field.
|
|
|
|
*/
|
|
|
|
public function getDeclaration($name, array $field)
|
|
|
|
{
|
|
|
|
|
|
|
|
$default = $this->getDefaultFieldDeclaration($field);
|
|
|
|
|
|
|
|
$charset = (isset($field['charset']) && $field['charset']) ?
|
|
|
|
' ' . $this->getCharsetFieldDeclaration($field['charset']) : '';
|
|
|
|
|
|
|
|
$collation = (isset($field['collation']) && $field['collation']) ?
|
|
|
|
' ' . $this->getCollationFieldDeclaration($field['collation']) : '';
|
|
|
|
|
|
|
|
$notnull = (isset($field['notnull']) && $field['notnull']) ? ' NOT NULL' : '';
|
|
|
|
|
|
|
|
$unique = (isset($field['unique']) && $field['unique']) ?
|
|
|
|
' ' . $this->getUniqueFieldDeclaration() : '';
|
2007-09-03 14:41:51 +04:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
$check = (isset($field['check']) && $field['check']) ?
|
|
|
|
' ' . $field['check'] : '';
|
|
|
|
|
|
|
|
$method = 'get' . $field['type'] . 'Declaration';
|
|
|
|
|
|
|
|
if (method_exists($this->conn->dataDict, $method)) {
|
|
|
|
return $this->conn->dataDict->$method($name, $field);
|
|
|
|
} else {
|
|
|
|
$dec = $this->conn->dataDict->getNativeDeclaration($field);
|
|
|
|
}
|
|
|
|
return $this->conn->quoteIdentifier($name, true) . ' ' . $dec . $charset . $default . $notnull . $unique . $check . $collation;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
/**
|
|
|
|
* getDefaultDeclaration
|
|
|
|
* Obtain DBMS specific SQL code portion needed to set a default value
|
|
|
|
* declaration to be used in statements like CREATE TABLE.
|
|
|
|
*
|
|
|
|
* @param array $field field definition array
|
|
|
|
* @return string DBMS specific SQL code portion needed to set a default value
|
|
|
|
*/
|
|
|
|
public function getDefaultFieldDeclaration($field)
|
|
|
|
{
|
|
|
|
$default = '';
|
|
|
|
if (isset($field['default'])) {
|
|
|
|
if ($field['default'] === '') {
|
|
|
|
$field['default'] = empty($field['notnull'])
|
|
|
|
? null : $this->valid_default_values[$field['type']];
|
|
|
|
|
2007-09-03 14:41:51 +04:00
|
|
|
if ($field['default'] === '' &&
|
2007-07-21 19:17:17 +04:00
|
|
|
($this->conn->getAttribute(Doctrine::ATTR_PORTABILITY) & Doctrine::PORTABILITY_EMPTY_TO_NULL)) {
|
|
|
|
$field['default'] = null;
|
|
|
|
}
|
|
|
|
}
|
2007-09-03 14:41:51 +04:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
if ($field['type'] === 'boolean') {
|
2007-09-03 14:41:51 +04:00
|
|
|
$fields['default'] = $this->conn->convertBooleans($field['default']);
|
2007-07-21 19:17:17 +04:00
|
|
|
}
|
|
|
|
$default = ' DEFAULT ' . $this->conn->quote($field['default'], $field['type']);
|
|
|
|
}
|
|
|
|
return $default;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-24 02:50:07 +04:00
|
|
|
/**
|
|
|
|
* Obtain DBMS specific SQL code portion needed to set a CHECK constraint
|
|
|
|
* declaration to be used in statements like CREATE TABLE.
|
|
|
|
*
|
|
|
|
* @param array $definition check definition
|
|
|
|
* @return string DBMS specific SQL code portion needed to set a CHECK constraint
|
|
|
|
*/
|
|
|
|
public function getCheckDeclaration(array $definition)
|
|
|
|
{
|
|
|
|
$constraints = array();
|
|
|
|
foreach ($definition as $field => $def) {
|
|
|
|
if (is_string($def)) {
|
|
|
|
$constraints[] = 'CHECK (' . $def . ')';
|
2007-07-24 03:44:14 +04:00
|
|
|
} else {
|
|
|
|
if (isset($def['min'])) {
|
|
|
|
$constraints[] = 'CHECK (' . $field . ' >= ' . $def['min'] . ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($def['max'])) {
|
|
|
|
$constraints[] = 'CHECK (' . $field . ' <= ' . $def['max'] . ')';
|
|
|
|
}
|
2007-07-24 02:50:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-24 03:44:14 +04:00
|
|
|
return implode(', ', $constraints);
|
2007-07-24 02:50:07 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
/**
|
2007-09-03 14:41:51 +04:00
|
|
|
* Obtain DBMS specific SQL code portion needed to set an index
|
2007-07-21 19:17:17 +04:00
|
|
|
* declaration to be used in statements like CREATE TABLE.
|
|
|
|
*
|
2007-07-24 02:50:07 +04:00
|
|
|
* @param string $name name of the index
|
2007-07-21 19:17:17 +04:00
|
|
|
* @param array $definition index definition
|
|
|
|
* @return string DBMS specific SQL code portion needed to set an index
|
|
|
|
*/
|
|
|
|
public function getIndexDeclaration($name, array $definition)
|
|
|
|
{
|
|
|
|
$name = $this->conn->quoteIdentifier($name);
|
|
|
|
$type = '';
|
|
|
|
|
|
|
|
if (isset($definition['type'])) {
|
|
|
|
if (strtolower($definition['type']) == 'unique') {
|
|
|
|
$type = strtoupper($definition['type']) . ' ';
|
|
|
|
} else {
|
|
|
|
throw new Doctrine_Export_Exception('Unknown index type ' . $definition['type']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! isset($definition['fields']) || ! is_array($definition['fields'])) {
|
|
|
|
throw new Doctrine_Export_Exception('No index columns given.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$query = $type . 'INDEX ' . $name;
|
|
|
|
|
|
|
|
$query .= ' (' . $this->getIndexFieldDeclarationList($definition['fields']) . ')';
|
2007-09-03 14:41:51 +04:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
return $query;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
/**
|
|
|
|
* getIndexFieldDeclarationList
|
|
|
|
* Obtain DBMS specific SQL code portion needed to set an index
|
|
|
|
* declaration to be used in statements like CREATE TABLE.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getIndexFieldDeclarationList(array $fields)
|
|
|
|
{
|
2007-09-03 18:57:18 +04:00
|
|
|
$ret = array();
|
2007-07-21 19:17:17 +04:00
|
|
|
foreach ($fields as $field => $definition) {
|
2007-09-03 18:57:18 +04:00
|
|
|
if (is_array($definition)) {
|
2007-07-21 19:17:17 +04:00
|
|
|
$ret[] = $this->conn->quoteIdentifier($field);
|
|
|
|
} else {
|
|
|
|
$ret[] = $this->conn->quoteIdentifier($definition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return implode(', ', $ret);
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
/**
|
|
|
|
* A method to return the required SQL string that fits between CREATE ... TABLE
|
|
|
|
* to create the table as a temporary table.
|
|
|
|
*
|
|
|
|
* Should be overridden in driver classes to return the correct string for the
|
|
|
|
* specific database type.
|
|
|
|
*
|
|
|
|
* The default is to return the string "TEMPORARY" - this will result in a
|
|
|
|
* SQL error for any database that does not support temporary tables, or that
|
|
|
|
* requires a different SQL command from "CREATE TEMPORARY TABLE".
|
|
|
|
*
|
|
|
|
* @return string The string required to be placed between "CREATE" and "TABLE"
|
|
|
|
* to generate a temporary table, if possible.
|
|
|
|
*/
|
|
|
|
public function getTemporaryTableQuery()
|
|
|
|
{
|
|
|
|
return 'TEMPORARY';
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
/**
|
|
|
|
* getForeignKeyDeclaration
|
|
|
|
* Obtain DBMS specific SQL code portion needed to set the FOREIGN KEY constraint
|
|
|
|
* of a field declaration to be used in statements like CREATE TABLE.
|
|
|
|
*
|
|
|
|
* @param array $definition an associative array with the following structure:
|
|
|
|
* name optional constraint name
|
2007-09-03 14:41:51 +04:00
|
|
|
*
|
2007-07-21 19:17:17 +04:00
|
|
|
* local the local field(s)
|
|
|
|
*
|
|
|
|
* foreign the foreign reference field(s)
|
|
|
|
*
|
|
|
|
* foreignTable the name of the foreign table
|
|
|
|
*
|
|
|
|
* onDelete referential delete action
|
2007-09-03 14:41:51 +04:00
|
|
|
*
|
2007-07-21 19:17:17 +04:00
|
|
|
* onUpdate referential update action
|
|
|
|
*
|
|
|
|
* deferred deferred constraint checking
|
|
|
|
*
|
|
|
|
* The onDelete and onUpdate keys accept the following values:
|
|
|
|
*
|
2007-09-03 14:41:51 +04:00
|
|
|
* CASCADE: Delete or update the row from the parent table and automatically delete or
|
2007-07-21 19:17:17 +04:00
|
|
|
* update the matching rows in the child table. Both ON DELETE CASCADE and ON UPDATE CASCADE are supported.
|
|
|
|
* Between two tables, you should not define several ON UPDATE CASCADE clauses that act on the same column
|
|
|
|
* in the parent table or in the child table.
|
|
|
|
*
|
|
|
|
* SET NULL: Delete or update the row from the parent table and set the foreign key column or columns in the
|
2007-09-03 14:41:51 +04:00
|
|
|
* child table to NULL. This is valid only if the foreign key columns do not have the NOT NULL qualifier
|
2007-07-21 19:17:17 +04:00
|
|
|
* specified. Both ON DELETE SET NULL and ON UPDATE SET NULL clauses are supported.
|
|
|
|
*
|
2007-09-03 14:41:51 +04:00
|
|
|
* NO ACTION: In standard SQL, NO ACTION means no action in the sense that an attempt to delete or update a primary
|
2007-07-21 19:17:17 +04:00
|
|
|
* key value is not allowed to proceed if there is a related foreign key value in the referenced table.
|
|
|
|
*
|
|
|
|
* RESTRICT: Rejects the delete or update operation for the parent table. NO ACTION and RESTRICT are the same as
|
|
|
|
* omitting the ON DELETE or ON UPDATE clause.
|
|
|
|
*
|
|
|
|
* SET DEFAULT
|
|
|
|
*
|
|
|
|
* @return string DBMS specific SQL code portion needed to set the FOREIGN KEY constraint
|
|
|
|
* of a field declaration.
|
|
|
|
*/
|
|
|
|
public function getForeignKeyDeclaration(array $definition)
|
|
|
|
{
|
|
|
|
$sql = $this->getForeignKeyBaseDeclaration($definition);
|
|
|
|
$sql .= $this->getAdvancedForeignKeyOptions($definition);
|
|
|
|
|
|
|
|
return $sql;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
/**
|
|
|
|
* getAdvancedForeignKeyOptions
|
|
|
|
* Return the FOREIGN KEY query section dealing with non-standard options
|
|
|
|
* as MATCH, INITIALLY DEFERRED, ON UPDATE, ...
|
|
|
|
*
|
|
|
|
* @param array $definition foreign key definition
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getAdvancedForeignKeyOptions(array $definition)
|
|
|
|
{
|
|
|
|
$query = '';
|
|
|
|
if ( ! empty($definition['onUpdate'])) {
|
|
|
|
$query .= ' ON UPDATE ' . $this->getForeignKeyRefentialAction($definition['onUpdate']);
|
|
|
|
}
|
|
|
|
if ( ! empty($definition['onDelete'])) {
|
|
|
|
$query .= ' ON DELETE ' . $this->getForeignKeyRefentialAction($definition['onDelete']);
|
|
|
|
}
|
|
|
|
return $query;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
/**
|
|
|
|
* getForeignKeyReferentialAction
|
|
|
|
*
|
|
|
|
* returns given referential action in uppercase if valid, otherwise throws
|
|
|
|
* an exception
|
|
|
|
*
|
|
|
|
* @throws Doctrine_Exception_Exception if unknown referential action given
|
|
|
|
* @param string $action foreign key referential action
|
|
|
|
* @param string foreign key referential action in uppercase
|
|
|
|
*/
|
|
|
|
public function getForeignKeyReferentialAction($action)
|
|
|
|
{
|
2007-09-03 18:57:18 +04:00
|
|
|
$upper = strtoupper($action);
|
2007-07-21 19:17:17 +04:00
|
|
|
switch ($upper) {
|
|
|
|
case 'CASCADE':
|
|
|
|
case 'SET NULL':
|
|
|
|
case 'NO ACTION':
|
|
|
|
case 'RESTRICT':
|
|
|
|
case 'SET DEFAULT':
|
|
|
|
return $upper;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Doctrine_Export_Exception('Unknown foreign key referential action \'' . $upper . '\' given.');
|
|
|
|
}
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
/**
|
|
|
|
* getForeignKeyBaseDeclaration
|
|
|
|
* Obtain DBMS specific SQL code portion needed to set the FOREIGN KEY constraint
|
|
|
|
* of a field declaration to be used in statements like CREATE TABLE.
|
|
|
|
*
|
|
|
|
* @param array $definition
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getForeignKeyBaseDeclaration(array $definition)
|
|
|
|
{
|
2007-09-03 18:57:18 +04:00
|
|
|
$sql = '';
|
2007-07-21 19:17:17 +04:00
|
|
|
if (isset($definition['name'])) {
|
2007-10-17 23:09:48 +04:00
|
|
|
$sql .= ' CONSTRAINT ' . $this->conn->quoteIdentifier($definition['name']) . ' ';
|
2007-07-21 19:17:17 +04:00
|
|
|
}
|
|
|
|
$sql .= 'FOREIGN KEY (';
|
|
|
|
|
|
|
|
if ( ! isset($definition['local'])) {
|
|
|
|
throw new Doctrine_Export_Exception('Local reference field missing from definition.');
|
|
|
|
}
|
|
|
|
if ( ! isset($definition['foreign'])) {
|
|
|
|
throw new Doctrine_Export_Exception('Foreign reference field missing from definition.');
|
|
|
|
}
|
|
|
|
if ( ! isset($definition['foreignTable'])) {
|
|
|
|
throw new Doctrine_Export_Exception('Foreign reference table missing from definition.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! is_array($definition['local'])) {
|
|
|
|
$definition['local'] = array($definition['local']);
|
|
|
|
}
|
|
|
|
if ( ! is_array($definition['foreign'])) {
|
|
|
|
$definition['foreign'] = array($definition['foreign']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql .= implode(', ', array_map(array($this->conn, 'quoteIdentifier'), $definition['local']))
|
|
|
|
. ') REFERENCES '
|
2007-09-30 05:17:49 +04:00
|
|
|
. $this->conn->quoteIdentifier($definition['foreignTable']) . '('
|
2007-07-21 19:17:17 +04:00
|
|
|
. implode(', ', array_map(array($this->conn, 'quoteIdentifier'), $definition['foreign'])) . ')';
|
2007-09-03 14:41:51 +04:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
return $sql;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
/**
|
|
|
|
* Obtain DBMS specific SQL code portion needed to set the UNIQUE constraint
|
|
|
|
* of a field declaration to be used in statements like CREATE TABLE.
|
|
|
|
*
|
|
|
|
* @return string DBMS specific SQL code portion needed to set the UNIQUE constraint
|
|
|
|
* of a field declaration.
|
|
|
|
*/
|
|
|
|
public function getUniqueFieldDeclaration()
|
|
|
|
{
|
|
|
|
return 'UNIQUE';
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
/**
|
|
|
|
* Obtain DBMS specific SQL code portion needed to set the CHARACTER SET
|
|
|
|
* of a field declaration to be used in statements like CREATE TABLE.
|
|
|
|
*
|
|
|
|
* @param string $charset name of the charset
|
|
|
|
* @return string DBMS specific SQL code portion needed to set the CHARACTER SET
|
|
|
|
* of a field declaration.
|
|
|
|
*/
|
|
|
|
public function getCharsetFieldDeclaration($charset)
|
|
|
|
{
|
|
|
|
return '';
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
/**
|
|
|
|
* Obtain DBMS specific SQL code portion needed to set the COLLATION
|
|
|
|
* of a field declaration to be used in statements like CREATE TABLE.
|
|
|
|
*
|
|
|
|
* @param string $collation name of the collation
|
|
|
|
* @return string DBMS specific SQL code portion needed to set the COLLATION
|
|
|
|
* of a field declaration.
|
|
|
|
*/
|
|
|
|
public function getCollationFieldDeclaration($collation)
|
|
|
|
{
|
|
|
|
return '';
|
2007-09-03 14:41:51 +04:00
|
|
|
}
|
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
|
2007-07-21 19:17:17 +04:00
|
|
|
* method for exporting Doctrine_Record classes to a schema
|
|
|
|
*
|
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
|
|
|
|
* that extend Doctrine_Record and are not abstract classes
|
|
|
|
*
|
|
|
|
* @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);
|
2007-10-11 07:23:33 +04:00
|
|
|
} else {
|
2007-10-19 18:30:48 +04:00
|
|
|
$models = Doctrine::getLoadedModels();
|
2007-10-11 07:23:33 +04: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
|
|
|
|
* method for exporting Doctrine_Record classes to a schema
|
|
|
|
*
|
|
|
|
* @throws Doctrine_Connection_Exception if some error other than Doctrine::ERR_ALREADY_EXISTS
|
|
|
|
* occurred during the create table operation
|
|
|
|
* @param array $classes
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function exportClasses(array $classes)
|
|
|
|
{
|
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);
|
|
|
|
|
2007-10-21 10:23:59 +04:00
|
|
|
if ( ! isset($connections[$connectionName])) {
|
2007-10-05 09:06:42 +04:00
|
|
|
$connections[$connectionName] = array();
|
2007-10-09 03:55:25 +04:00
|
|
|
$connections[$connectionName]['creates'] = array();
|
|
|
|
$connections[$connectionName]['alters'] = array();
|
2007-10-05 09:06:42 +04:00
|
|
|
}
|
|
|
|
|
2007-10-09 03:55:25 +04:00
|
|
|
$sql = $this->exportClassesSql(array($class));
|
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) {
|
|
|
|
if (strstr($query, 'CREATE')) {
|
|
|
|
$connections[$connectionName]['creates'][] = $query;
|
|
|
|
// Unset the create from sql so we can have an array of everything else but creates
|
|
|
|
unset($sql[$key]);
|
|
|
|
}
|
|
|
|
}
|
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) {
|
2007-10-09 03:55:25 +04:00
|
|
|
$build[$connectionName] = array_merge($sql['creates'], $sql['alters']);
|
|
|
|
}
|
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);
|
|
|
|
|
|
|
|
$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();
|
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
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
* method for exporting Doctrine_Record classes to a schema
|
|
|
|
*
|
|
|
|
* @throws Doctrine_Connection_Exception if some error other than Doctrine::ERR_ALREADY_EXISTS
|
|
|
|
* occurred during the create table operation
|
|
|
|
* @param array $classes
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function exportClassesSql(array $classes)
|
|
|
|
{
|
2007-11-27 21:23:13 +03:00
|
|
|
$models = Doctrine::filterInvalidModels($classes);
|
2007-09-19 23:43:33 +04:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
$sql = array();
|
2007-09-19 23:43:33 +04:00
|
|
|
|
|
|
|
foreach ($models as $name) {
|
2007-09-01 20:11:58 +04:00
|
|
|
$record = new $name();
|
|
|
|
$table = $record->getTable();
|
2007-11-09 02:03:28 +03:00
|
|
|
|
|
|
|
$parents = $table->getOption('joinedParents');
|
|
|
|
|
|
|
|
foreach ($parents as $parent) {
|
|
|
|
$data = $table->getConnection()->getTable($parent)->getExportableFormat();
|
|
|
|
|
|
|
|
$query = $this->conn->export->createTableSql($data['tableName'], $data['columns'], $data['options']);
|
|
|
|
|
|
|
|
$sql = array_merge($sql, (array) $query);
|
|
|
|
}
|
|
|
|
|
2007-09-01 20:11:58 +04:00
|
|
|
$data = $table->getExportableFormat();
|
2007-09-03 14:41:51 +04:00
|
|
|
|
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;
|
|
|
|
}
|
2007-10-05 09:06:42 +04:00
|
|
|
|
2007-09-07 00:23:52 +04:00
|
|
|
if ($table->getAttribute(Doctrine::ATTR_EXPORT) & Doctrine::EXPORT_PLUGINS) {
|
2007-11-29 01:56:45 +03:00
|
|
|
$sql = array_merge($sql, $this->exportGeneratorsSql($table));
|
2007-09-07 00:23:52 +04:00
|
|
|
}
|
2007-07-21 19:17:17 +04:00
|
|
|
}
|
2007-09-19 23:43:33 +04:00
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
$sql = array_unique($sql);
|
2007-09-19 23:43:33 +04: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
|
2007-11-14 01:28:37 +03:00
|
|
|
*/
|
2007-11-29 01:56:45 +03:00
|
|
|
public function getAllGenerators(Doctrine_Table $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) {
|
2007-09-07 01:07:24 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
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();
|
2007-11-14 01:28:37 +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
|
2007-11-14 01:05:12 +03:00
|
|
|
*/
|
2007-11-29 01:56:45 +03:00
|
|
|
public function exportGeneratorsSql(Doctrine_Table $table)
|
2007-11-14 01:05:12 +03:00
|
|
|
{
|
|
|
|
$sql = array();
|
2007-11-29 01:56:45 +03:00
|
|
|
foreach ($this->getAllGenerators($table) as $name => $generator) {
|
|
|
|
$table = $generator->getTable();
|
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
|
|
|
|
* returns the sql for exporting Doctrine_Record classes to a schema
|
|
|
|
*
|
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
|
|
|
|
* that extend Doctrine_Record and are not abstract classes
|
|
|
|
*
|
|
|
|
* @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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
public function exportTable(Doctrine_Table $table)
|
|
|
|
{
|
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 {
|
|
|
|
$data = $table->getExportableFormat();
|
|
|
|
|
|
|
|
$this->conn->export->createTable($data['tableName'], $data['columns'], $data['options']);
|
|
|
|
} catch(Doctrine_Connection_Exception $e) {
|
|
|
|
// 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
|
|
|
}
|