Source for file Mysql.php
Documentation is available at Mysql.php
* $Id: Mysql.php 2288 2007-08-29 21:51:49Z Jonathan.Wage $
* 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>.
* @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
* @version $Revision: 2288 $
* @param string $name name of the database that should be created
return 'CREATE DATABASE ' .
$this->conn->quoteIdentifier($name, true);
* drop an existing database
* @param string $name name of the database that should be dropped
return 'DROP DATABASE ' .
$this->conn->quoteIdentifier($name);
* @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.
* @param array $options An associative array of table options:
* 'collate' => 'utf8_unicode_ci',
public function createTableSql($name, array $fields, array $options =
array())
// build indexes for all foreign key fields (needed in MySQL!!)
if (isset
($options['foreignKeys'])) {
foreach ($options['foreignKeys'] as $fk) {
if (isset
($options['indexes'])) {
foreach ($options['indexes'] as $definition) {
// Check if index already exists on the column
$found =
($local ==
$definition['fields']);
} else if (in_array($local, $definition['fields']) &&
count($definition['fields']) ===
1) {
// Index already exists on the column
if (isset
($options['primary']) &&
!empty($options['primary']) &&
in_array($local, $options['primary'])) {
// field is part of the PK and therefore already indexed
$options['indexes'][$local] =
array('fields' =>
array($local =>
array()));
if (isset
($options['indexes']) &&
! empty($options['indexes'])) {
foreach($options['indexes'] as $index =>
$definition) {
// attach all primary keys
if (isset
($options['primary']) &&
! empty($options['primary'])) {
$query =
'CREATE TABLE ' .
$this->conn->quoteIdentifier($name, true) .
' (' .
$queryFields .
')';
$optionStrings =
array();
if (isset
($options['comment'])) {
$optionStrings['comment'] =
'COMMENT = ' .
$this->dbh->quote($options['comment'], 'text');
if (isset
($options['charset'])) {
$optionStrings['charset'] =
'DEFAULT CHARACTER SET ' .
$options['charset'];
if (isset
($options['collate'])) {
$optionStrings['charset'] .=
' COLLATE ' .
$options['collate'];
// get the type of the table
if (isset
($options['type'])) {
$type =
$options['type'];
$type =
$this->conn->getAttribute(Doctrine::ATTR_DEFAULT_TABLE_TYPE);
$optionStrings[] =
'ENGINE = ' .
$type;
if (!empty($optionStrings)) {
$query.=
' '.
implode(' ', $optionStrings);
if (isset
($options['foreignKeys'])) {
foreach ((array)
$options['foreignKeys'] as $k =>
$definition) {
* 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:
* New name for the table.
* 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 Metabase parser.
* 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.
* 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.
* 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 Metabase parser.
* 'file_limit' => array(),
* 'time_limit' => array()
* @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.
foreach ($changes as $changeName =>
$change) {
if ( ! empty($changes['name'])) {
$change_name =
$this->conn->quoteIdentifier($changes['name']);
$query .=
'RENAME TO ' .
$change_name;
if ( ! empty($changes['add']) &&
is_array($changes['add'])) {
foreach ($changes['add'] as $fieldName =>
$field) {
$query.=
'ADD ' .
$this->getDeclaration($field['type'], $fieldName, $field);
if ( ! empty($changes['remove']) &&
is_array($changes['remove'])) {
foreach ($changes['remove'] as $fieldName =>
$field) {
$fieldName =
$this->conn->quoteIdentifier($fieldName);
$query .=
'DROP ' .
$fieldName;
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 (isset
($rename[$fieldName])) {
$oldFieldName =
$rename[$fieldName];
unset
($rename[$fieldName]);
$oldFieldName =
$fieldName;
$oldFieldName =
$this->conn->quoteIdentifier($oldFieldName, true);
$query .=
'CHANGE ' .
$oldFieldName .
' '
.
$this->getDeclaration($field['definition']['type'], $fieldName, $field['definition']);
if ( ! empty($rename) &&
is_array($rename)) {
foreach ($rename as $renameName =>
$renamedField) {
$field =
$changes['rename'][$renamedField];
$renamedField =
$this->conn->quoteIdentifier($renamedField, true);
$query .=
'CHANGE ' .
$renamedField .
' '
.
$this->getDeclaration($field['definition']['type'], $field['name'], $field['definition']);
$name =
$this->conn->quoteIdentifier($name, true);
return $this->conn->exec('ALTER TABLE ' .
$name .
' ' .
$query);
* @param string $sequenceName 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:
* 'collate' => 'utf8_unicode_ci',
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);
$optionsStrings =
array();
if (isset
($options['comment']) &&
! empty($options['comment'])) {
$optionsStrings['comment'] =
'COMMENT = ' .
$this->conn->quote($options['comment'], 'string');
if (isset
($options['charset']) &&
! empty($options['charset'])) {
$optionsStrings['charset'] =
'DEFAULT CHARACTER SET ' .
$options['charset'];
if (isset
($options['collate'])) {
$optionsStrings['collate'] .=
' COLLATE ' .
$options['collate'];
if (isset
($options['type'])) {
$type =
$options['type'];
$type =
$this->conn->default_table_type;
$optionsStrings[] =
'ENGINE = ' .
$type;
$query =
'CREATE TABLE ' .
$sequenceName
.
' (' .
$seqcolName .
' INT NOT NULL AUTO_INCREMENT, PRIMARY KEY ('
.
strlen($this->conn->default_table_type) ?
' TYPE = '
.
$this->conn->default_table_type :
'';
$query =
'INSERT INTO ' .
$sequenceName
.
' (' .
$seqcolName .
') VALUES (' .
($start -
1) .
')';
$result =
$this->conn->exec('DROP TABLE ' .
$sequenceName);
* 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
* 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.
* 'last_login' => array()
$name =
$this->conn->getIndexName($name);
if (isset
($definition['type'])) {
$query =
'CREATE ' .
$type .
'INDEX ' .
$name .
' ON ' .
$table;
* 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
if (isset
($field['default']) &&
$field['length'] <=
255) {
if ($field['default'] ===
'') {
$field['default'] =
empty($field['notnull'])
if ($field['default'] ===
''
&&
($conn->getAttribute(Doctrine::ATTR_PORTABILITY) & Doctrine::PORTABILITY_EMPTY_TO_NULL)
$default =
' DEFAULT ' .
$this->conn->quote($field['default'], $field['type']);
* Obtain DBMS specific SQL code portion needed to set an index
* declaration to be used in statements like CREATE TABLE.
* @param string $charset name of the index
* @param array $definition index definition
* @return string DBMS specific SQL code portion needed to set an index
$name =
$this->conn->quoteIdentifier($name);
if(isset
($definition['type'])) {
if ( ! isset
($definition['fields'])) {
if ( ! is_array($definition['fields'])) {
$definition['fields'] =
array($definition['fields']);
$query =
$type .
'INDEX ' .
$this->conn->formatter->getIndexName($name);
* getIndexFieldDeclarationList
* Obtain DBMS specific SQL code portion needed to set an index
* declaration to be used in statements like CREATE TABLE.
foreach ($fields as $fieldName =>
$field) {
$fieldString =
$fieldName;
if (isset
($field['length'])) {
$fieldString .=
'(' .
$field['length'] .
')';
if (isset
($field['sorting'])) {
$fieldString .=
' ' .
$sort;
$declFields[] =
$fieldString;
* getAdvancedForeignKeyOptions
* Return the FOREIGN KEY query section dealing with non-standard options
* as MATCH, INITIALLY DEFERRED, ON UPDATE, ...
* @param array $definition
if (!empty($definition['match'])) {
$query .=
' MATCH ' .
$definition['match'];
if (!empty($definition['onUpdate'])) {
if (!empty($definition['onDelete'])) {
* @param string $table name of table that should be used in method
* @param string $name name of the index to be dropped
$table =
$this->conn->quoteIdentifier($table, true);
$name =
$this->conn->quoteIdentifier($this->conn->formatter->getIndexName($name), true);
return 'DROP INDEX ' .
$name .
' ON ' .
$table;
* @param string $table name of table that should be dropped from the database
$table =
$this->conn->quoteIdentifier($table, true);
return 'DROP TABLE ' .
$table;