Refactored export drivers
This commit is contained in:
parent
c1c66b6b3f
commit
61c793c2cf
@ -22,15 +22,15 @@ Doctrine::autoload('Doctrine_Export');
|
|||||||
/**
|
/**
|
||||||
* Doctrine_Export_Mysql
|
* Doctrine_Export_Mysql
|
||||||
*
|
*
|
||||||
* @package Doctrine
|
* @package Doctrine
|
||||||
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
||||||
* @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
|
* @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
|
||||||
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
||||||
* @category Object Relational Mapping
|
* @category Object Relational Mapping
|
||||||
* @link www.phpdoctrine.com
|
* @link www.phpdoctrine.com
|
||||||
* @since 1.0
|
* @since 1.0
|
||||||
* @version $Revision$
|
* @version $Revision$
|
||||||
*/
|
*/
|
||||||
class Doctrine_Export_Mysql extends Doctrine_Export {
|
class Doctrine_Export_Mysql extends Doctrine_Export {
|
||||||
/**
|
/**
|
||||||
* create a new database
|
* create a new database
|
||||||
@ -87,50 +87,50 @@ class Doctrine_Export_Mysql extends Doctrine_Export {
|
|||||||
* 'type' => 'innodb',
|
* 'type' => 'innodb',
|
||||||
* );
|
* );
|
||||||
*
|
*
|
||||||
* @return mixed MDB2_OK on success, a MDB2 error on failure
|
* @return void
|
||||||
* @access public
|
|
||||||
*/
|
*/
|
||||||
public function createTable($name, array $fields, array $options = array()) {
|
public function createTable($name, array $fields, array $options = array()) {
|
||||||
if (!$name) {
|
if( ! $name)
|
||||||
return $this->dbh->raiseError(MDB2_ERROR_CANNOT_CREATE, null, null,
|
throw new Doctrine_Export_Mysql_Exception('no valid table name specified');
|
||||||
'no valid table name specified', __FUNCTION__);
|
|
||||||
}
|
|
||||||
if(empty($fields))
|
if(empty($fields))
|
||||||
throw new Doctrine_Export_Exception('no fields specified for table "'.$name.'"');
|
throw new Doctrine_Export_Mysql_Exception('no fields specified for table "'.$name.'"');
|
||||||
|
|
||||||
$query_fields = $this->getFieldDeclarationList($fields);
|
$query_fields = $this->getFieldDeclarationList($fields);
|
||||||
|
|
||||||
if (!empty($options['primary'])) {
|
if( ! empty($options['primary']))
|
||||||
$query_fields.= ', PRIMARY KEY ('.implode(', ', array_keys($options['primary'])).')';
|
$query_fields.= ', PRIMARY KEY (' . implode(', ', array_keys($options['primary'])) . ')';
|
||||||
}
|
|
||||||
$name = $this->conn->quoteIdentifier($name, true);
|
|
||||||
$query = "CREATE TABLE $name ($query_fields)";
|
|
||||||
|
|
||||||
$options_strings = array();
|
$name = $this->conn->quoteIdentifier($name, true);
|
||||||
|
$query = 'CREATE TABLE ' . $name . ' (' . $query_fields . ')';
|
||||||
|
|
||||||
if (!empty($options['comment'])) {
|
$optionStrings = array();
|
||||||
$options_strings['comment'] = 'COMMENT = '.$this->dbh->quote($options['comment'], 'text');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!empty($options['charset'])) {
|
if( ! empty($options['comment']))
|
||||||
$options_strings['charset'] = 'DEFAULT CHARACTER SET '.$options['charset'];
|
$optionStrings['comment'] = 'COMMENT = '.$this->dbh->quote($options['comment'], 'text');
|
||||||
if (!empty($options['collate'])) {
|
|
||||||
$options_strings['charset'].= ' COLLATE '.$options['collate'];
|
|
||||||
|
if( ! empty($options['charset'])) {
|
||||||
|
$optionsSting['charset'] = 'DEFAULT CHARACTER SET '.$options['charset'];
|
||||||
|
if( ! empty($options['collate'])) {
|
||||||
|
$optionStrings['charset'].= ' COLLATE '.$options['collate'];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$type = false;
|
$type = false;
|
||||||
|
|
||||||
if (!empty($options['type'])) {
|
if (!empty($options['type'])) {
|
||||||
$type = $options['type'];
|
$type = $options['type'];
|
||||||
} elseif ($this->dbh->options['default_table_type']) {
|
} elseif ($this->dbh->options['default_table_type']) {
|
||||||
$type = $this->dbh->options['default_table_type'];
|
$type = $this->dbh->options['default_table_type'];
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($type) {
|
if ($type) {
|
||||||
$options_strings[] = "ENGINE = $type";
|
$optionStrings[] = "ENGINE = $type";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!empty($options_strings)) {
|
if (!empty($optionStrings)) {
|
||||||
$query.= ' '.implode(' ', $options_strings);
|
$query.= ' '.implode(' ', $optionStrings);
|
||||||
}
|
}
|
||||||
return $this->dbh->query($query);
|
return $this->dbh->query($query);
|
||||||
}
|
}
|
||||||
@ -218,13 +218,13 @@ class Doctrine_Export_Mysql extends Doctrine_Export {
|
|||||||
* )
|
* )
|
||||||
*
|
*
|
||||||
* @param boolean $check indicates whether the function should just check if the DBMS driver
|
* @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
|
* can perform the requested table alterations if the value is true or
|
||||||
* actually perform them otherwise.
|
* actually perform them otherwise.
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
public function alterTable($name, $changes, $check) {
|
public function alterTable($name, $changes, $check) {
|
||||||
foreach ($changes as $change_name => $change) {
|
foreach ($changes as $changeName => $change) {
|
||||||
switch ($change_name) {
|
switch ($changeName) {
|
||||||
case 'add':
|
case 'add':
|
||||||
case 'remove':
|
case 'remove':
|
||||||
case 'change':
|
case 'change':
|
||||||
@ -232,21 +232,21 @@ class Doctrine_Export_Mysql extends Doctrine_Export {
|
|||||||
case 'name':
|
case 'name':
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new Doctrine_Export_Exception('change type "'.$change_name.'" not yet supported');
|
throw new Doctrine_Export_Exception('change type "'.$changeName.'" not yet supported');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($check) {
|
if($check) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
$query = '';
|
$query = '';
|
||||||
if (!empty($changes['name'])) {
|
if( ! empty($changes['name'])) {
|
||||||
$change_name = $this->conn->quoteIdentifier($changes['name'], true);
|
$change_name = $this->conn->quoteIdentifier($changes['name'], true);
|
||||||
$query .= 'RENAME TO ' . $change_name;
|
$query .= 'RENAME TO ' . $change_name;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!empty($changes['add']) && is_array($changes['add'])) {
|
if( ! empty($changes['add']) && is_array($changes['add'])) {
|
||||||
foreach ($changes['add'] as $field_name => $field) {
|
foreach ($changes['add'] as $field_name => $field) {
|
||||||
if ($query) {
|
if ($query) {
|
||||||
$query.= ', ';
|
$query.= ', ';
|
||||||
@ -255,7 +255,7 @@ class Doctrine_Export_Mysql extends Doctrine_Export {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!empty($changes['remove']) && is_array($changes['remove'])) {
|
if( ! empty($changes['remove']) && is_array($changes['remove'])) {
|
||||||
foreach ($changes['remove'] as $field_name => $field) {
|
foreach ($changes['remove'] as $field_name => $field) {
|
||||||
if ($query) {
|
if ($query) {
|
||||||
$query.= ', ';
|
$query.= ', ';
|
||||||
@ -266,13 +266,13 @@ class Doctrine_Export_Mysql extends Doctrine_Export {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$rename = array();
|
$rename = array();
|
||||||
if (!empty($changes['rename']) && is_array($changes['rename'])) {
|
if( ! empty($changes['rename']) && is_array($changes['rename'])) {
|
||||||
foreach ($changes['rename'] as $field_name => $field) {
|
foreach ($changes['rename'] as $field_name => $field) {
|
||||||
$rename[$field['name']] = $field_name;
|
$rename[$field['name']] = $field_name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!empty($changes['change']) && is_array($changes['change'])) {
|
if( ! empty($changes['change']) && is_array($changes['change'])) {
|
||||||
foreach ($changes['change'] as $field_name => $field) {
|
foreach ($changes['change'] as $field_name => $field) {
|
||||||
if ($query) {
|
if ($query) {
|
||||||
$query.= ', ';
|
$query.= ', ';
|
||||||
@ -288,7 +288,7 @@ class Doctrine_Export_Mysql extends Doctrine_Export {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!empty($rename) && is_array($rename)) {
|
if( ! empty($rename) && is_array($rename)) {
|
||||||
foreach ($rename as $rename_name => $renamed_field) {
|
foreach ($rename as $rename_name => $renamed_field) {
|
||||||
if ($query) {
|
if ($query) {
|
||||||
$query.= ', ';
|
$query.= ', ';
|
||||||
@ -299,7 +299,7 @@ class Doctrine_Export_Mysql extends Doctrine_Export {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$query) {
|
if( ! $query) {
|
||||||
return MDB2_OK;
|
return MDB2_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -309,29 +309,34 @@ class Doctrine_Export_Mysql extends Doctrine_Export {
|
|||||||
/**
|
/**
|
||||||
* create sequence
|
* create sequence
|
||||||
*
|
*
|
||||||
* this method has been borrowed from PEAR MDB2 database abstraction layer
|
* @param string $sequenceName name of the sequence to be created
|
||||||
*
|
* @param string $seqcol_name the name of the sequence column
|
||||||
* @param string $seq_name name of the sequence to be created
|
* @param string $start start value of the sequence; default is 1
|
||||||
* @param string $start start value of the sequence; default is 1
|
* @return boolean
|
||||||
* @return mixed MDB2_OK on success, a MDB2 error on failure
|
|
||||||
*/
|
*/
|
||||||
public function createSequence($sequence_name, $seqcol_name, $start = 1) {
|
public function createSequence($sequenceName, $seqcol_name, $start = 1) {
|
||||||
$query = "CREATE TABLE $sequence_name ($seqcol_name INT NOT NULL AUTO_INCREMENT, PRIMARY KEY ($seqcol_name))";
|
$query = 'CREATE TABLE ' . $sequenceName
|
||||||
$query .= strlen($this->dbh->options['default_table_type']) ? ' TYPE = '.$this->dbh->options['default_table_type'] : '';
|
. ' (' . $seqcol_name . ' INT NOT NULL AUTO_INCREMENT, PRIMARY KEY ('
|
||||||
|
. $seqcol_name . '))'
|
||||||
|
. strlen($this->dbh->options['default_table_type']) ? ' TYPE = '
|
||||||
|
. $this->dbh->options['default_table_type'] : '';
|
||||||
|
|
||||||
$res = $this->dbh->query($query);
|
$res = $this->dbh->query($query);
|
||||||
|
|
||||||
if ($start == 1) {
|
if ($start == 1)
|
||||||
return MDB2_OK;
|
return true;
|
||||||
}
|
|
||||||
|
|
||||||
|
$query = 'INSERT INTO ' . $sequenceName
|
||||||
|
. ' (' . $seqcol_name . ') VALUES (' . ($start-1) . ')';
|
||||||
|
|
||||||
$query = "INSERT INTO $sequence_name ($seqcol_name) VALUES (".($start-1).')';
|
|
||||||
$res = $this->dbh->query($query);
|
$res = $this->dbh->query($query);
|
||||||
|
|
||||||
// Handle error
|
// Handle error
|
||||||
$result = $this->dbh->query("DROP TABLE $sequence_name");
|
try {
|
||||||
if (PEAR::isError($result)) {
|
$result = $this->dbh->query('DROP TABLE ' . $sequenceName);
|
||||||
return $this->dbh->raiseError($result, null, null,
|
} catch(Exception $e) {
|
||||||
'could not drop inconsistent sequence table', __FUNCTION__);
|
throw new Doctrine_Export_Mysql_Exception('could not drop inconsistent sequence table');
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->dbh->raiseError($res, null, null,
|
return $this->dbh->raiseError($res, null, null,
|
||||||
@ -340,8 +345,6 @@ class Doctrine_Export_Mysql extends Doctrine_Export {
|
|||||||
/**
|
/**
|
||||||
* Get the stucture of a field into an array
|
* Get the stucture of a field into an array
|
||||||
*
|
*
|
||||||
* this method has been borrowed from PEAR MDB2 database abstraction layer
|
|
||||||
*
|
|
||||||
* @author Leoncx
|
* @author Leoncx
|
||||||
* @param string $table name of the table on which the index is to be created
|
* @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 string $name name of the index to be created
|
||||||
@ -390,24 +393,25 @@ class Doctrine_Export_Mysql extends Doctrine_Export {
|
|||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* drop existing index
|
* drop existing index
|
||||||
* this method has been borrowed from PEAR MDB2 database abstraction layer
|
|
||||||
*
|
*
|
||||||
* @param string $table name of table that should be used in method
|
* @param string $table name of table that should be used in method
|
||||||
* @param string $name name of the index to be dropped
|
* @param string $name name of the index to be dropped
|
||||||
* @return mixed MDB2_OK on success, a MDB2 error on failure
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function dropIndex($table, $name) {
|
public function dropIndex($table, $name) {
|
||||||
$table = $this->conn->quoteIdentifier($table, true);
|
$table = $this->conn->quoteIdentifier($table, true);
|
||||||
$name = $this->conn->quoteIdentifier($this->dbh->getIndexName($name), true);
|
$name = $this->conn->quoteIdentifier($this->dbh->getIndexName($name), true);
|
||||||
return $this->dbh->query("DROP INDEX $name ON $table");
|
return $this->dbh->query('DROP INDEX ' . $name . ' ON ' . $table);
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* dropTable
|
* dropTable
|
||||||
*
|
*
|
||||||
* @param string $table name of table that should be dropped from the database
|
* @param string $table name of table that should be dropped from the database
|
||||||
* @throws PDOException
|
* @throws PDOException
|
||||||
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function dropTable($table) {
|
public function dropTable($table) {
|
||||||
|
$table = $this->conn->quoteIdentifier($table, true);
|
||||||
$this->dbh->query('DROP TABLE '.$table);
|
$this->dbh->query('DROP TABLE '.$table);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -41,10 +41,9 @@ class Doctrine_Export_Oracle extends Doctrine_Export {
|
|||||||
* @access public
|
* @access public
|
||||||
*/
|
*/
|
||||||
public function createDatabase($name) {
|
public function createDatabase($name) {
|
||||||
if (!$db->options['emulate_database']) {
|
if (!$db->options['emulate_database'])
|
||||||
return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
|
throw new Doctrine_Export_Oracle_Exception('database creation is only supported if the "emulate_database" option is enabled');
|
||||||
'database creation is only supported if the "emulate_database" option is enabled', __FUNCTION__);
|
|
||||||
}
|
|
||||||
|
|
||||||
$username = $db->options['database_name_prefix'].$name;
|
$username = $db->options['database_name_prefix'].$name;
|
||||||
$password = $db->dsn['password'] ? $db->dsn['password'] : $name;
|
$password = $db->dsn['password'] ? $db->dsn['password'] : $name;
|
||||||
@ -53,9 +52,7 @@ class Doctrine_Export_Oracle extends Doctrine_Export {
|
|||||||
|
|
||||||
$query = 'CREATE USER '.$username.' IDENTIFIED BY '.$password.$tablespace;
|
$query = 'CREATE USER '.$username.' IDENTIFIED BY '.$password.$tablespace;
|
||||||
$result = $db->standaloneQuery($query, null, true);
|
$result = $db->standaloneQuery($query, null, true);
|
||||||
if (PEAR::isError($result)) {
|
|
||||||
return $result;
|
|
||||||
}
|
|
||||||
$query = 'GRANT CREATE SESSION, CREATE TABLE, UNLIMITED TABLESPACE, CREATE SEQUENCE, CREATE TRIGGER TO '.$username;
|
$query = 'GRANT CREATE SESSION, CREATE TABLE, UNLIMITED TABLESPACE, CREATE SEQUENCE, CREATE TRIGGER TO '.$username;
|
||||||
$result = $db->standaloneQuery($query, null, true);
|
$result = $db->standaloneQuery($query, null, true);
|
||||||
if (PEAR::isError($result)) {
|
if (PEAR::isError($result)) {
|
||||||
@ -77,10 +74,10 @@ class Doctrine_Export_Oracle extends Doctrine_Export {
|
|||||||
* @access public
|
* @access public
|
||||||
*/
|
*/
|
||||||
public function dropDatabase($name) {
|
public function dropDatabase($name) {
|
||||||
if (!$db->options['emulate_database']) {
|
if (!$db->options['emulate_database'])
|
||||||
return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
|
throw new Doctrine_Export_Oracle_Exception('database dropping is only supported if the
|
||||||
'database dropping is only supported if the "emulate_database" option is enabled', __FUNCTION__);
|
"emulate_database" option is enabled');
|
||||||
}
|
|
||||||
|
|
||||||
$username = $db->options['database_name_prefix'].$name;
|
$username = $db->options['database_name_prefix'].$name;
|
||||||
return $db->standaloneQuery('DROP USER '.$username.' CASCADE', null, true);
|
return $db->standaloneQuery('DROP USER '.$username.' CASCADE', null, true);
|
||||||
@ -339,9 +336,7 @@ END;
|
|||||||
* @param boolean $check indicates whether the function should just check if the DBMS driver
|
* @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
|
* can perform the requested table alterations if the value is true or
|
||||||
* actually perform them otherwise.
|
* actually perform them otherwise.
|
||||||
* @access public
|
* @return void
|
||||||
*
|
|
||||||
* @return mixed MDB2_OK on success, a MDB2 error on failure
|
|
||||||
*/
|
*/
|
||||||
public function alterTable($name, $changes, $check) {
|
public function alterTable($name, $changes, $check) {
|
||||||
|
|
||||||
@ -423,7 +418,7 @@ END;
|
|||||||
* @param object $db database object that is extended by this class
|
* @param object $db database object that is extended by this class
|
||||||
* @param string $seq_name name of the sequence to be created
|
* @param string $seq_name name of the sequence to be created
|
||||||
* @param string $start start value of the sequence; default is 1
|
* @param string $start start value of the sequence; default is 1
|
||||||
* @return mixed MDB2_OK on success, a MDB2 error on failure
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function createSequence($seq_name, $start = 1) {
|
public function createSequence($seq_name, $start = 1) {
|
||||||
$sequence_name = $db->quoteIdentifier($db->getSequenceName($seq_name), true);
|
$sequence_name = $db->quoteIdentifier($db->getSequenceName($seq_name), true);
|
||||||
@ -436,7 +431,7 @@ END;
|
|||||||
*
|
*
|
||||||
* @param object $db database object that is extended by this class
|
* @param object $db database object that is extended by this class
|
||||||
* @param string $seq_name name of the sequence to be dropped
|
* @param string $seq_name name of the sequence to be dropped
|
||||||
* @return mixed MDB2_OK on success, a MDB2 error on failure
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function dropSequence($seq_name) {
|
public function dropSequence($seq_name) {
|
||||||
$sequence_name = $db->quoteIdentifier($db->getSequenceName($seq_name), true);
|
$sequence_name = $db->quoteIdentifier($db->getSequenceName($seq_name), true);
|
||||||
|
@ -143,7 +143,7 @@ class Doctrine_Export_Pgsql extends Doctrine_Export {
|
|||||||
* @throws PDOException
|
* @throws PDOException
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
function alterTable($name, $changes, $check) {
|
public function alterTable($name, $changes, $check) {
|
||||||
foreach ($changes as $change_name => $change) {
|
foreach ($changes as $change_name => $change) {
|
||||||
switch ($change_name) {
|
switch ($change_name) {
|
||||||
case 'add':
|
case 'add':
|
||||||
|
Loading…
Reference in New Issue
Block a user