2008-08-01 22:46:14 +04:00
|
|
|
<?php
|
2008-08-16 23:40:59 +04:00
|
|
|
/*
|
|
|
|
* $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
|
2009-05-05 21:20:55 +04:00
|
|
|
* <http://www.doctrine-project.org>.
|
2008-08-16 23:40:59 +04:00
|
|
|
*/
|
2008-08-01 22:46:14 +04:00
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
namespace Doctrine\DBAL\Platforms;
|
2008-09-12 12:51:56 +04:00
|
|
|
|
2009-12-06 12:47:54 +03:00
|
|
|
use Doctrine\DBAL\Schema\TableDiff;
|
|
|
|
|
2008-08-16 23:40:59 +04:00
|
|
|
/**
|
2009-05-05 21:20:55 +04:00
|
|
|
* OraclePlatform.
|
2008-08-16 23:40:59 +04:00
|
|
|
*
|
|
|
|
* @since 2.0
|
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
|
|
|
* @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
|
2009-11-03 11:58:03 +03:00
|
|
|
* @author Benjamin Eberlei <kontakt@beberlei.de>
|
2008-08-16 23:40:59 +04:00
|
|
|
*/
|
2009-01-22 22:38:10 +03:00
|
|
|
class OraclePlatform extends AbstractPlatform
|
2008-08-01 22:46:14 +04:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* return string to call a function to get a substring inside an SQL statement
|
|
|
|
*
|
|
|
|
* Note: Not SQL92, but common functionality.
|
|
|
|
*
|
|
|
|
* @param string $value an sql string literal or column name/alias
|
|
|
|
* @param integer $position where to start the substring portion
|
|
|
|
* @param integer $length the substring portion length
|
|
|
|
* @return string SQL substring function with given parameters
|
|
|
|
* @override
|
|
|
|
*/
|
|
|
|
public function getSubstringExpression($value, $position, $length = null)
|
|
|
|
{
|
2009-05-27 22:54:40 +04:00
|
|
|
if ($length !== null) {
|
2008-08-01 22:46:14 +04:00
|
|
|
return "SUBSTR($value, $position, $length)";
|
2009-05-27 22:54:40 +04:00
|
|
|
}
|
2008-08-01 22:46:14 +04:00
|
|
|
|
|
|
|
return "SUBSTR($value, $position)";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return string to call a variable with the current timestamp inside an SQL statement
|
|
|
|
* There are three special variables for current date and time:
|
|
|
|
* - CURRENT_TIMESTAMP (date and time, TIMESTAMP type)
|
|
|
|
* - CURRENT_DATE (date, DATE type)
|
|
|
|
* - CURRENT_TIME (time, TIME type)
|
|
|
|
*
|
|
|
|
* @return string to call a variable with the current timestamp
|
|
|
|
* @override
|
|
|
|
*/
|
|
|
|
public function getNowExpression($type = 'timestamp')
|
|
|
|
{
|
|
|
|
switch ($type) {
|
|
|
|
case 'date':
|
|
|
|
case 'time':
|
|
|
|
case 'timestamp':
|
|
|
|
default:
|
|
|
|
return 'TO_CHAR(CURRENT_TIMESTAMP, \'YYYY-MM-DD HH24:MI:SS\')';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-12 01:38:23 +03:00
|
|
|
/**
|
|
|
|
* returns the position of the first occurrence of substring $substr in string $str
|
|
|
|
*
|
|
|
|
* @param string $substr literal string to find
|
|
|
|
* @param string $str literal string
|
|
|
|
* @param int $pos position to start at, beginning of string by default
|
|
|
|
* @return integer
|
|
|
|
*/
|
|
|
|
public function getLocateExpression($str, $substr, $startPos = false)
|
|
|
|
{
|
|
|
|
if ($startPos == false) {
|
|
|
|
return 'INSTR('.$str.', '.$substr.')';
|
|
|
|
} else {
|
|
|
|
return 'INSTR('.$str.', '.$substr.', '.$startPos.')';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-01 22:46:14 +04:00
|
|
|
/**
|
|
|
|
* Returns global unique identifier
|
|
|
|
*
|
|
|
|
* @return string to get global unique identifier
|
|
|
|
* @override
|
|
|
|
*/
|
|
|
|
public function getGuidExpression()
|
|
|
|
{
|
|
|
|
return 'SYS_GUID()';
|
|
|
|
}
|
2009-05-30 13:37:56 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the SQL used to create a sequence that starts with a given value
|
|
|
|
* and increments by the given allocation size.
|
|
|
|
*
|
2009-11-30 22:02:05 +03:00
|
|
|
* Need to specifiy minvalue, since start with is hidden in the system and MINVALUE <= START WITH.
|
|
|
|
* Therefore we can use MINVALUE to be able to get a hint what START WITH was for later introspection
|
|
|
|
* in {@see listSequences()}
|
|
|
|
*
|
2009-12-02 22:07:38 +03:00
|
|
|
* @param \Doctrine\DBAL\Schema\Sequence $sequence
|
2010-02-20 01:33:54 +03:00
|
|
|
* @return string
|
2009-05-30 13:37:56 +04:00
|
|
|
*/
|
2010-03-05 19:35:00 +03:00
|
|
|
public function getCreateSequenceSQL(\Doctrine\DBAL\Schema\Sequence $sequence)
|
2009-05-05 21:20:55 +04:00
|
|
|
{
|
2009-12-02 22:07:38 +03:00
|
|
|
return 'CREATE SEQUENCE ' . $sequence->getName() .
|
|
|
|
' START WITH ' . $sequence->getInitialValue() .
|
|
|
|
' MINVALUE ' . $sequence->getInitialValue() .
|
|
|
|
' INCREMENT BY ' . $sequence->getAllocationSize();
|
2008-08-01 22:46:14 +04:00
|
|
|
}
|
2009-05-30 06:27:50 +04:00
|
|
|
|
2008-09-07 17:48:40 +04:00
|
|
|
/**
|
2009-05-05 21:20:55 +04:00
|
|
|
* {@inheritdoc}
|
2008-09-07 17:48:40 +04:00
|
|
|
*
|
2009-05-05 21:20:55 +04:00
|
|
|
* @param string $sequenceName
|
2008-09-07 17:48:40 +04:00
|
|
|
* @override
|
|
|
|
*/
|
2010-03-05 19:35:00 +03:00
|
|
|
public function getSequenceNextValSQL($sequenceName)
|
2008-09-07 17:48:40 +04:00
|
|
|
{
|
2009-08-11 14:51:38 +04:00
|
|
|
return 'SELECT ' . $sequenceName . '.nextval FROM DUAL';
|
2008-09-07 17:48:40 +04:00
|
|
|
}
|
2008-09-12 21:25:38 +04:00
|
|
|
|
|
|
|
/**
|
2009-05-05 21:20:55 +04:00
|
|
|
* {@inheritdoc}
|
2008-09-12 21:25:38 +04:00
|
|
|
*
|
2009-05-05 21:20:55 +04:00
|
|
|
* @param integer $level
|
2008-09-12 21:25:38 +04:00
|
|
|
* @override
|
|
|
|
*/
|
2010-03-05 19:35:00 +03:00
|
|
|
public function getSetTransactionIsolationSQL($level)
|
2008-09-12 21:25:38 +04:00
|
|
|
{
|
2010-03-05 19:35:00 +03:00
|
|
|
return 'SET TRANSACTION ISOLATION LEVEL ' . $this->_getTransactionIsolationLevelSQL($level);
|
2008-09-12 21:25:38 +04:00
|
|
|
}
|
2009-05-29 01:34:35 +04:00
|
|
|
|
2010-03-05 19:35:00 +03:00
|
|
|
protected function _getTransactionIsolationLevelSQL($level)
|
2008-09-12 21:25:38 +04:00
|
|
|
{
|
|
|
|
switch ($level) {
|
2009-07-14 02:59:36 +04:00
|
|
|
case \Doctrine\DBAL\Connection::TRANSACTION_READ_UNCOMMITTED:
|
|
|
|
return 'READ UNCOMMITTED';
|
|
|
|
case \Doctrine\DBAL\Connection::TRANSACTION_READ_COMMITTED:
|
2009-05-17 23:27:12 +04:00
|
|
|
return 'READ COMMITTED';
|
2009-07-14 02:59:36 +04:00
|
|
|
case \Doctrine\DBAL\Connection::TRANSACTION_REPEATABLE_READ:
|
|
|
|
case \Doctrine\DBAL\Connection::TRANSACTION_SERIALIZABLE:
|
2008-09-12 21:25:38 +04:00
|
|
|
return 'SERIALIZABLE';
|
|
|
|
default:
|
2010-03-05 19:35:00 +03:00
|
|
|
return parent::_getTransactionIsolationLevelSQL($level);
|
2008-09-12 21:25:38 +04:00
|
|
|
}
|
|
|
|
}
|
2009-09-05 06:23:24 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @override
|
|
|
|
*/
|
2010-03-05 19:35:00 +03:00
|
|
|
public function getBooleanTypeDeclarationSQL(array $field)
|
2009-09-05 06:23:24 +04:00
|
|
|
{
|
|
|
|
return 'NUMBER(1)';
|
|
|
|
}
|
2009-05-05 22:39:25 +04:00
|
|
|
|
2009-05-07 17:54:01 +04:00
|
|
|
/**
|
|
|
|
* @override
|
|
|
|
*/
|
2010-03-05 19:35:00 +03:00
|
|
|
public function getIntegerTypeDeclarationSQL(array $field)
|
2009-05-07 17:54:01 +04:00
|
|
|
{
|
|
|
|
return 'NUMBER(10)';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @override
|
|
|
|
*/
|
2010-03-05 19:35:00 +03:00
|
|
|
public function getBigIntTypeDeclarationSQL(array $field)
|
2009-05-07 17:54:01 +04:00
|
|
|
{
|
|
|
|
return 'NUMBER(20)';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @override
|
|
|
|
*/
|
2010-03-05 19:35:00 +03:00
|
|
|
public function getSmallIntTypeDeclarationSQL(array $field)
|
2009-05-07 17:54:01 +04:00
|
|
|
{
|
|
|
|
return 'NUMBER(5)';
|
|
|
|
}
|
|
|
|
|
2009-06-20 16:59:33 +04:00
|
|
|
/**
|
|
|
|
* @override
|
|
|
|
*/
|
2010-03-05 19:35:00 +03:00
|
|
|
public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration)
|
2009-06-20 16:59:33 +04:00
|
|
|
{
|
2009-08-28 21:25:28 +04:00
|
|
|
return 'TIMESTAMP(0) WITH TIME ZONE';
|
2009-06-20 16:59:33 +04:00
|
|
|
}
|
|
|
|
|
2009-11-03 19:56:05 +03:00
|
|
|
/**
|
|
|
|
* @override
|
|
|
|
*/
|
2010-03-05 19:35:00 +03:00
|
|
|
public function getDateTypeDeclarationSQL(array $fieldDeclaration)
|
2009-11-03 19:56:05 +03:00
|
|
|
{
|
|
|
|
return 'DATE';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @override
|
|
|
|
*/
|
2010-03-05 19:35:00 +03:00
|
|
|
public function getTimeTypeDeclarationSQL(array $fieldDeclaration)
|
2009-11-03 19:56:05 +03:00
|
|
|
{
|
|
|
|
return 'DATE';
|
|
|
|
}
|
|
|
|
|
2009-05-07 17:54:01 +04:00
|
|
|
/**
|
|
|
|
* @override
|
|
|
|
*/
|
2010-03-05 19:35:00 +03:00
|
|
|
protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef)
|
2009-05-07 17:54:01 +04:00
|
|
|
{
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the SQL snippet used to declare a VARCHAR column on the Oracle platform.
|
|
|
|
*
|
|
|
|
* @params array $field
|
|
|
|
* @override
|
|
|
|
*/
|
2010-03-05 19:35:00 +03:00
|
|
|
public function getVarcharTypeDeclarationSQL(array $field)
|
2009-05-07 17:54:01 +04:00
|
|
|
{
|
|
|
|
if ( ! isset($field['length'])) {
|
|
|
|
if (array_key_exists('default', $field)) {
|
|
|
|
$field['length'] = $this->getVarcharMaxLength();
|
|
|
|
} else {
|
|
|
|
$field['length'] = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$length = ($field['length'] <= $this->getVarcharMaxLength()) ? $field['length'] : false;
|
|
|
|
$fixed = (isset($field['fixed'])) ? $field['fixed'] : false;
|
|
|
|
|
|
|
|
return $fixed ? ($length ? 'CHAR(' . $length . ')' : 'CHAR(2000)')
|
|
|
|
: ($length ? 'VARCHAR2(' . $length . ')' : 'VARCHAR2(4000)');
|
|
|
|
}
|
2009-10-01 16:00:14 +04:00
|
|
|
|
|
|
|
/** @override */
|
2010-03-05 19:35:00 +03:00
|
|
|
public function getClobTypeDeclarationSQL(array $field)
|
2009-10-01 16:00:14 +04:00
|
|
|
{
|
|
|
|
return 'CLOB';
|
|
|
|
}
|
2009-05-07 17:54:01 +04:00
|
|
|
|
2010-03-05 19:35:00 +03:00
|
|
|
public function getListDatabasesSQL()
|
2009-05-30 06:27:50 +04:00
|
|
|
{
|
2009-11-30 22:02:05 +03:00
|
|
|
return 'SELECT username FROM all_users';
|
2009-05-30 06:27:50 +04:00
|
|
|
}
|
|
|
|
|
2010-03-05 19:35:00 +03:00
|
|
|
public function getListSequencesSQL($database)
|
2009-06-20 21:02:02 +04:00
|
|
|
{
|
2009-12-01 00:16:29 +03:00
|
|
|
return "SELECT sequence_name, min_value, increment_by FROM sys.all_sequences ".
|
|
|
|
"WHERE SEQUENCE_OWNER = '".strtoupper($database)."'";
|
2009-06-20 21:02:02 +04:00
|
|
|
}
|
|
|
|
|
2009-12-02 21:52:21 +03:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param string $table
|
|
|
|
* @param array $columns
|
|
|
|
* @param array $options
|
|
|
|
* @return array
|
|
|
|
*/
|
2010-03-05 19:35:00 +03:00
|
|
|
protected function _getCreateTableSQL($table, array $columns, array $options = array())
|
2009-05-30 06:27:50 +04:00
|
|
|
{
|
2009-08-21 22:13:22 +04:00
|
|
|
$indexes = isset($options['indexes']) ? $options['indexes'] : array();
|
2009-05-30 06:27:50 +04:00
|
|
|
$options['indexes'] = array();
|
2010-03-05 19:35:00 +03:00
|
|
|
$sql = parent::_getCreateTableSQL($table, $columns, $options);
|
2009-05-30 06:27:50 +04:00
|
|
|
|
|
|
|
foreach ($columns as $name => $column) {
|
|
|
|
if (isset($column['sequence'])) {
|
2010-03-05 19:35:00 +03:00
|
|
|
$sql[] = $this->getCreateSequenceSQL($column['sequence'], 1);
|
2009-05-30 06:27:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($column['autoincrement']) && $column['autoincrement'] ||
|
|
|
|
(isset($column['autoinc']) && $column['autoinc'])) {
|
|
|
|
$sql = array_merge($sql, $this->getCreateAutoincrementSql($name, $table));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($indexes) && ! empty($indexes)) {
|
2009-12-02 21:52:21 +03:00
|
|
|
foreach ($indexes as $indexName => $index) {
|
2010-03-05 19:35:00 +03:00
|
|
|
$sql[] = $this->getCreateIndexSQL($index, $table);
|
2009-05-30 06:27:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
2009-11-03 11:58:03 +03:00
|
|
|
/**
|
|
|
|
* @license New BSD License
|
|
|
|
* @link http://ezcomponents.org/docs/api/trunk/DatabaseSchema/ezcDbSchemaOracleReader.html
|
|
|
|
* @param string $table
|
|
|
|
* @return string
|
|
|
|
*/
|
2010-03-05 19:35:00 +03:00
|
|
|
public function getListTableIndexesSQL($table)
|
2009-05-30 06:27:50 +04:00
|
|
|
{
|
2009-11-30 22:02:05 +03:00
|
|
|
$table = strtoupper($table);
|
|
|
|
|
2009-11-03 11:58:03 +03:00
|
|
|
return "SELECT uind.index_name AS name, " .
|
|
|
|
" uind.index_type AS type, " .
|
|
|
|
" decode( uind.uniqueness, 'NONUNIQUE', 0, 'UNIQUE', 1 ) AS is_unique, " .
|
|
|
|
" uind_col.column_name AS column_name, " .
|
2009-11-30 22:02:05 +03:00
|
|
|
" uind_col.column_position AS column_pos, " .
|
|
|
|
" (SELECT ucon.constraint_type FROM user_constraints ucon WHERE ucon.constraint_name = uind.index_name) AS is_primary ".
|
2009-11-03 11:58:03 +03:00
|
|
|
"FROM user_indexes uind, user_ind_columns uind_col " .
|
2009-12-06 15:13:15 +03:00
|
|
|
"WHERE uind.index_name = uind_col.index_name AND uind_col.table_name = '$table' ORDER BY uind_col.column_position ASC";
|
2009-05-30 06:27:50 +04:00
|
|
|
}
|
|
|
|
|
2010-03-05 19:35:00 +03:00
|
|
|
public function getListTablesSQL()
|
2009-05-30 06:27:50 +04:00
|
|
|
{
|
|
|
|
return 'SELECT * FROM sys.user_tables';
|
|
|
|
}
|
|
|
|
|
2010-03-05 19:35:00 +03:00
|
|
|
public function getListViewsSQL($database)
|
2009-05-30 06:27:50 +04:00
|
|
|
{
|
2010-02-11 02:41:35 +03:00
|
|
|
return 'SELECT view_name, text FROM sys.user_views';
|
2009-05-30 06:27:50 +04:00
|
|
|
}
|
|
|
|
|
2010-03-05 19:35:00 +03:00
|
|
|
public function getCreateViewSQL($name, $sql)
|
2009-05-30 06:27:50 +04:00
|
|
|
{
|
|
|
|
return 'CREATE VIEW ' . $name . ' AS ' . $sql;
|
|
|
|
}
|
|
|
|
|
2010-03-05 19:35:00 +03:00
|
|
|
public function getDropViewSQL($name)
|
2009-05-30 06:27:50 +04:00
|
|
|
{
|
|
|
|
return 'DROP VIEW '. $name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCreateAutoincrementSql($name, $table, $start = 1)
|
|
|
|
{
|
|
|
|
$table = strtoupper($table);
|
|
|
|
$sql = array();
|
|
|
|
|
|
|
|
$indexName = $table . '_AI_PK';
|
|
|
|
$definition = array(
|
|
|
|
'primary' => true,
|
2009-10-07 02:38:34 +04:00
|
|
|
'columns' => array($name => true),
|
2009-05-30 06:27:50 +04:00
|
|
|
);
|
|
|
|
|
2009-12-03 01:28:38 +03:00
|
|
|
$idx = new \Doctrine\DBAL\Schema\Index($indexName, array($name), true, true);
|
|
|
|
|
2009-05-30 06:27:50 +04:00
|
|
|
$sql[] = 'DECLARE
|
|
|
|
constraints_Count NUMBER;
|
|
|
|
BEGIN
|
|
|
|
SELECT COUNT(CONSTRAINT_NAME) INTO constraints_Count FROM USER_CONSTRAINTS WHERE TABLE_NAME = \''.$table.'\' AND CONSTRAINT_TYPE = \'P\';
|
|
|
|
IF constraints_Count = 0 OR constraints_Count = \'\' THEN
|
2010-03-05 19:35:00 +03:00
|
|
|
EXECUTE IMMEDIATE \''.$this->getCreateConstraintSQL($idx, $table).'\';
|
2009-05-30 06:27:50 +04:00
|
|
|
END IF;
|
|
|
|
END;';
|
|
|
|
|
|
|
|
$sequenceName = $table . '_SEQ';
|
2009-12-02 22:07:38 +03:00
|
|
|
$sequence = new \Doctrine\DBAL\Schema\Sequence($sequenceName, $start);
|
2010-03-05 19:35:00 +03:00
|
|
|
$sql[] = $this->getCreateSequenceSQL($sequence);
|
2009-05-30 06:27:50 +04:00
|
|
|
|
2009-08-11 14:51:38 +04:00
|
|
|
$triggerName = $table . '_AI_PK';
|
2009-05-30 06:27:50 +04:00
|
|
|
$sql[] = 'CREATE TRIGGER ' . $triggerName . '
|
|
|
|
BEFORE INSERT
|
|
|
|
ON ' . $table . '
|
|
|
|
FOR EACH ROW
|
|
|
|
DECLARE
|
|
|
|
last_Sequence NUMBER;
|
|
|
|
last_InsertID NUMBER;
|
|
|
|
BEGIN
|
2009-08-11 14:51:38 +04:00
|
|
|
SELECT ' . $sequenceName . '.NEXTVAL INTO :NEW.' . $name . ' FROM DUAL;
|
2009-05-30 06:27:50 +04:00
|
|
|
IF (:NEW.' . $name . ' IS NULL OR :NEW.'.$name.' = 0) THEN
|
2009-08-11 14:51:38 +04:00
|
|
|
SELECT ' . $sequenceName . '.NEXTVAL INTO :NEW.' . $name . ' FROM DUAL;
|
2009-05-30 06:27:50 +04:00
|
|
|
ELSE
|
|
|
|
SELECT NVL(Last_Number, 0) INTO last_Sequence
|
|
|
|
FROM User_Sequences
|
|
|
|
WHERE Sequence_Name = \'' . $sequenceName . '\';
|
|
|
|
SELECT :NEW.' . $name . ' INTO last_InsertID FROM DUAL;
|
|
|
|
WHILE (last_InsertID > last_Sequence) LOOP
|
2009-08-11 14:51:38 +04:00
|
|
|
SELECT ' . $sequenceName . '.NEXTVAL INTO last_Sequence FROM DUAL;
|
2009-05-30 06:27:50 +04:00
|
|
|
END LOOP;
|
|
|
|
END IF;
|
|
|
|
END;';
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getDropAutoincrementSql($table)
|
|
|
|
{
|
|
|
|
$table = strtoupper($table);
|
|
|
|
$trigger = $table . '_AI_PK';
|
|
|
|
|
|
|
|
if ($trigger) {
|
|
|
|
$sql[] = 'DROP TRIGGER ' . $trigger;
|
2010-03-05 19:35:00 +03:00
|
|
|
$sql[] = $this->getDropSequenceSQL($table.'_SEQ');
|
2009-05-30 06:27:50 +04:00
|
|
|
|
|
|
|
$indexName = $table . '_AI_PK';
|
2010-03-05 19:35:00 +03:00
|
|
|
$sql[] = $this->getDropConstraintSQL($indexName, $table);
|
2009-05-30 06:27:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
2010-03-05 19:35:00 +03:00
|
|
|
public function getListTableForeignKeysSQL($table)
|
2009-11-30 22:02:05 +03:00
|
|
|
{
|
|
|
|
$table = strtoupper($table);
|
|
|
|
|
2010-02-07 15:36:30 +03:00
|
|
|
return "SELECT alc.constraint_name,
|
|
|
|
alc.DELETE_RULE,
|
|
|
|
alc.search_condition,
|
|
|
|
cols.column_name \"local_column\",
|
|
|
|
cols.position,
|
|
|
|
r_alc.table_name \"references_table\",
|
|
|
|
r_cols.column_name \"foreign_column\"
|
|
|
|
FROM all_cons_columns cols
|
|
|
|
LEFT JOIN all_constraints alc
|
|
|
|
ON alc.constraint_name = cols.constraint_name
|
|
|
|
AND alc.owner = cols.owner
|
|
|
|
LEFT JOIN all_constraints r_alc
|
|
|
|
ON alc.r_constraint_name = r_alc.constraint_name
|
|
|
|
AND alc.r_owner = r_alc.owner
|
|
|
|
LEFT JOIN all_cons_columns r_cols
|
|
|
|
ON r_alc.constraint_name = r_cols.constraint_name
|
|
|
|
AND r_alc.owner = r_cols.owner
|
|
|
|
AND cols.position = r_cols.position
|
|
|
|
WHERE alc.constraint_name = cols.constraint_name
|
|
|
|
AND alc.constraint_type = 'R'
|
|
|
|
AND alc.table_name = '".$table."'";
|
2009-11-30 22:02:05 +03:00
|
|
|
}
|
|
|
|
|
2010-03-05 19:35:00 +03:00
|
|
|
public function getListTableConstraintsSQL($table)
|
2009-05-30 06:27:50 +04:00
|
|
|
{
|
|
|
|
$table = strtoupper($table);
|
|
|
|
return 'SELECT * FROM user_constraints WHERE table_name = \'' . $table . '\'';
|
|
|
|
}
|
|
|
|
|
2010-03-05 19:35:00 +03:00
|
|
|
public function getListTableColumnsSQL($table)
|
2009-05-30 06:27:50 +04:00
|
|
|
{
|
|
|
|
$table = strtoupper($table);
|
|
|
|
return "SELECT * FROM all_tab_columns WHERE table_name = '" . $table . "' ORDER BY column_name";
|
|
|
|
}
|
|
|
|
|
2009-12-03 01:28:38 +03:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param \Doctrine\DBAL\Schema\Sequence $sequence
|
|
|
|
* @return string
|
|
|
|
*/
|
2010-03-05 19:35:00 +03:00
|
|
|
public function getDropSequenceSQL($sequence)
|
2009-05-30 06:27:50 +04:00
|
|
|
{
|
2009-12-03 01:28:38 +03:00
|
|
|
if ($sequence instanceof \Doctrine\DBAL\Schema\Sequence) {
|
|
|
|
$sequence = $sequence->getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'DROP SEQUENCE ' . $sequence;
|
2009-05-30 06:27:50 +04:00
|
|
|
}
|
|
|
|
|
2010-02-07 15:36:30 +03:00
|
|
|
/**
|
|
|
|
* @param ForeignKeyConstraint|string $foreignKey
|
|
|
|
* @param Table|string $table
|
|
|
|
* @return string
|
|
|
|
*/
|
2010-03-05 19:35:00 +03:00
|
|
|
public function getDropForeignKeySQL($foreignKey, $table)
|
2010-02-07 15:36:30 +03:00
|
|
|
{
|
|
|
|
if ($foreignKey instanceof \Doctrine\DBAL\Schema\ForeignKeyConstraint) {
|
|
|
|
$foreignKey = $foreignKey->getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($table instanceof \Doctrine\DBAL\Schema\Table) {
|
|
|
|
$table = $table->getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'ALTER TABLE ' . $table . ' DROP CONSTRAINT ' . $foreignKey;
|
|
|
|
}
|
|
|
|
|
2010-03-05 19:35:00 +03:00
|
|
|
public function getDropDatabaseSQL($database)
|
2009-05-30 06:27:50 +04:00
|
|
|
{
|
|
|
|
return 'DROP USER ' . $database . ' CASCADE';
|
|
|
|
}
|
|
|
|
|
2009-11-02 18:08:51 +03:00
|
|
|
/**
|
|
|
|
* Gets the sql statements for altering an existing table.
|
|
|
|
*
|
|
|
|
* The method returns an array of sql statements, since some platforms need several statements.
|
|
|
|
*
|
2009-12-06 12:47:54 +03:00
|
|
|
* @param string $diff->name name of the table that is intended to be changed.
|
2009-11-02 18:08:51 +03:00
|
|
|
* @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.
|
|
|
|
* @return array
|
|
|
|
*/
|
2010-03-05 19:35:00 +03:00
|
|
|
public function getAlterTableSQL(TableDiff $diff)
|
2009-05-30 06:27:50 +04:00
|
|
|
{
|
2009-12-06 16:00:49 +03:00
|
|
|
$sql = array();
|
2009-12-06 12:47:54 +03:00
|
|
|
|
|
|
|
$fields = array();
|
|
|
|
foreach ($diff->addedColumns AS $column) {
|
2010-03-05 19:35:00 +03:00
|
|
|
$fields[] = $this->getColumnDeclarationSQL($column->getName(), $column->toArray());
|
2009-05-30 06:27:50 +04:00
|
|
|
}
|
2009-12-06 12:47:54 +03:00
|
|
|
if (count($fields)) {
|
|
|
|
$sql[] = 'ALTER TABLE ' . $diff->name . ' ADD (' . implode(', ', $fields) . ')';
|
2009-05-30 06:27:50 +04:00
|
|
|
}
|
|
|
|
|
2009-12-06 12:47:54 +03:00
|
|
|
$fields = array();
|
|
|
|
foreach ($diff->changedColumns AS $columnDiff) {
|
|
|
|
$column = $columnDiff->column;
|
2010-03-05 19:35:00 +03:00
|
|
|
$fields[] = $column->getName(). ' ' . $this->getColumnDeclarationSQL('', $column->toArray());
|
2009-05-30 06:27:50 +04:00
|
|
|
}
|
2009-12-06 12:47:54 +03:00
|
|
|
if (count($fields)) {
|
|
|
|
$sql[] = 'ALTER TABLE ' . $diff->name . ' MODIFY (' . implode(', ', $fields) . ')';
|
2009-05-30 06:27:50 +04:00
|
|
|
}
|
|
|
|
|
2009-12-06 12:47:54 +03:00
|
|
|
foreach ($diff->renamedColumns AS $oldColumnName => $column) {
|
|
|
|
$sql[] = 'ALTER TABLE ' . $diff->name . ' RENAME COLUMN ' . $oldColumnName .' TO ' . $column->getName();
|
2009-05-30 06:27:50 +04:00
|
|
|
}
|
|
|
|
|
2009-12-06 12:47:54 +03:00
|
|
|
$fields = array();
|
|
|
|
foreach ($diff->removedColumns AS $column) {
|
|
|
|
$fields[] = $column->getName();
|
2009-05-30 06:27:50 +04:00
|
|
|
}
|
2009-12-06 12:47:54 +03:00
|
|
|
if (count($fields)) {
|
|
|
|
$sql[] = 'ALTER TABLE ' . $diff->name . ' DROP COLUMN ' . implode(', ', $fields);
|
2009-05-30 06:27:50 +04:00
|
|
|
}
|
|
|
|
|
2009-12-06 12:47:54 +03:00
|
|
|
if ($diff->newName !== false) {
|
|
|
|
$sql[] = 'ALTER TABLE ' . $diff->name . ' RENAME TO ' . $diff->newName;
|
2009-05-30 06:27:50 +04:00
|
|
|
}
|
|
|
|
|
2010-03-05 19:35:00 +03:00
|
|
|
$sql = array_merge($sql, $this->_getAlterTableIndexForeignKeySQL($diff));
|
2009-12-06 16:00:49 +03:00
|
|
|
|
2009-05-30 06:27:50 +04:00
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
2009-05-05 22:39:25 +04:00
|
|
|
/**
|
|
|
|
* Whether the platform prefers sequences for ID generation.
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function prefersSequences()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2009-05-27 22:54:40 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the platform name for this instance
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return 'oracle';
|
|
|
|
}
|
2009-07-10 21:53:48 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds an driver-specific LIMIT clause to the query
|
|
|
|
*
|
|
|
|
* @param string $query query to modify
|
|
|
|
* @param integer $limit limit the number of rows
|
|
|
|
* @param integer $offset start reading from given offset
|
|
|
|
* @return string the modified query
|
|
|
|
*/
|
2009-07-14 02:59:36 +04:00
|
|
|
public function modifyLimitQuery($query, $limit, $offset = null)
|
2009-07-10 21:53:48 +04:00
|
|
|
{
|
|
|
|
$limit = (int) $limit;
|
|
|
|
$offset = (int) $offset;
|
|
|
|
if (preg_match('/^\s*SELECT/i', $query)) {
|
|
|
|
if ( ! preg_match('/\sFROM\s/i', $query)) {
|
|
|
|
$query .= " FROM dual";
|
|
|
|
}
|
|
|
|
if ($limit > 0) {
|
|
|
|
$max = $offset + $limit;
|
2009-07-14 02:59:36 +04:00
|
|
|
$column = '*';
|
2009-07-10 21:53:48 +04:00
|
|
|
if ($offset > 0) {
|
|
|
|
$min = $offset + 1;
|
|
|
|
$query = 'SELECT b.'.$column.' FROM ('.
|
|
|
|
'SELECT a.*, ROWNUM AS doctrine_rownum FROM ('
|
|
|
|
. $query . ') a '.
|
|
|
|
') b '.
|
|
|
|
'WHERE doctrine_rownum BETWEEN ' . $min . ' AND ' . $max;
|
|
|
|
} else {
|
|
|
|
$query = 'SELECT a.'.$column.' FROM (' . $query .') a WHERE ROWNUM <= ' . $max;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $query;
|
|
|
|
}
|
2009-08-11 14:51:38 +04:00
|
|
|
|
2009-08-15 22:11:51 +04:00
|
|
|
/**
|
|
|
|
* Gets the character casing of a column in an SQL result set of this platform.
|
|
|
|
*
|
|
|
|
* Oracle returns all column names in SQL result sets in uppercase.
|
|
|
|
*
|
|
|
|
* @param string $column The column name for which to get the correct character casing.
|
|
|
|
* @return string The column name in the character casing used in SQL result sets.
|
|
|
|
*/
|
2010-03-05 19:35:00 +03:00
|
|
|
public function getSQLResultCasing($column)
|
2009-08-11 14:51:38 +04:00
|
|
|
{
|
|
|
|
return strtoupper($column);
|
|
|
|
}
|
2009-08-28 21:25:28 +04:00
|
|
|
|
2010-03-05 19:35:00 +03:00
|
|
|
public function getCreateTemporaryTableSnippetSQL()
|
2009-08-28 21:25:28 +04:00
|
|
|
{
|
|
|
|
return "CREATE GLOBAL TEMPORARY TABLE";
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getDateTimeFormatString()
|
|
|
|
{
|
|
|
|
return 'Y-m-d H:i:sP';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function fixSchemaElementName($schemaElementName)
|
|
|
|
{
|
|
|
|
if (strlen($schemaElementName) > 30) {
|
|
|
|
// Trim it
|
|
|
|
return substr($schemaElementName, 0, 30);
|
|
|
|
}
|
|
|
|
return $schemaElementName;
|
|
|
|
}
|
2009-11-30 22:02:05 +03:00
|
|
|
|
2009-12-11 01:39:28 +03:00
|
|
|
/**
|
|
|
|
* Maximum length of any given databse identifier, like tables or column names.
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getMaxIdentifierLength()
|
|
|
|
{
|
|
|
|
return 30;
|
|
|
|
}
|
|
|
|
|
2009-11-30 22:02:05 +03:00
|
|
|
/**
|
|
|
|
* Whether the platform supports sequences.
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function supportsSequences()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2009-12-02 21:52:21 +03:00
|
|
|
|
|
|
|
public function supportsForeignKeyOnUpdate()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2010-02-07 15:36:30 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2010-03-05 19:35:00 +03:00
|
|
|
public function getTruncateTableSQL($tableName, $cascade = false)
|
2010-02-07 15:36:30 +03:00
|
|
|
{
|
|
|
|
return 'TRUNCATE TABLE '.$tableName;
|
|
|
|
}
|
2009-11-03 19:56:05 +03:00
|
|
|
}
|