Unverified Commit e6260dc6 authored by Michael Moravec's avatar Michael Moravec Committed by Marco Pivetta

Extract AbstractPlatform::TRIM_* constants into TrimMode class

parent 45b57743
# Upgrade to 2.7 # Upgrade to 2.7
## Doctrine\DBAL\Platforms\AbstractPlatform::TRIM_* constants deprecated
``Doctrine\DBAL\Platforms\AbstractPlatform::TRIM_*`` constants were moved into ``Doctrine\DBAL\Platforms\TrimMode`` class without the ``TRIM_`` prefix.
## Doctrine\DBAL\Connection::TRANSACTION_* constants deprecated ## Doctrine\DBAL\Connection::TRANSACTION_* constants deprecated
``Doctrine\DBAL\Connection::TRANSACTION_*`` were moved into ``Doctrine\DBAL\TransactionIsolationLevel`` class without the ``TRANSACTION_`` prefix. ``Doctrine\DBAL\Connection::TRANSACTION_*`` were moved into ``Doctrine\DBAL\TransactionIsolationLevel`` class without the ``TRANSACTION_`` prefix.
......
...@@ -111,23 +111,31 @@ abstract class AbstractPlatform ...@@ -111,23 +111,31 @@ abstract class AbstractPlatform
/** /**
* @var int * @var int
*
* @deprecated Use TrimMode::UNSPECIFIED.
*/ */
const TRIM_UNSPECIFIED = 0; public const TRIM_UNSPECIFIED = TrimMode::UNSPECIFIED;
/** /**
* @var int * @var int
*
* @deprecated Use TrimMode::LEADING.
*/ */
const TRIM_LEADING = 1; public const TRIM_LEADING = TrimMode::LEADING;
/** /**
* @var int * @var int
*
* @deprecated Use TrimMode::TRAILING.
*/ */
const TRIM_TRAILING = 2; public const TRIM_TRAILING = TrimMode::TRAILING;
/** /**
* @var int * @var int
*
* @deprecated Use TrimMode::BOTH.
*/ */
const TRIM_BOTH = 3; public const TRIM_BOTH = TrimMode::BOTH;
/** /**
* @var array|null * @var array|null
...@@ -766,34 +774,34 @@ abstract class AbstractPlatform ...@@ -766,34 +774,34 @@ abstract class AbstractPlatform
* Returns the SQL snippet to trim a string. * Returns the SQL snippet to trim a string.
* *
* @param string $str The expression to apply the trim to. * @param string $str The expression to apply the trim to.
* @param int $pos The position of the trim (leading/trailing/both). * @param int $mode The position of the trim (leading/trailing/both).
* @param string|boolean $char The char to trim, has to be quoted already. Defaults to space. * @param string|bool $char The char to trim, has to be quoted already. Defaults to space.
* *
* @return string * @return string
*/ */
public function getTrimExpression($str, $pos = self::TRIM_UNSPECIFIED, $char = false) public function getTrimExpression($str, $mode = TrimMode::UNSPECIFIED, $char = false)
{ {
$expression = ''; $expression = '';
switch ($pos) { switch ($mode) {
case self::TRIM_LEADING: case TrimMode::LEADING:
$expression = 'LEADING '; $expression = 'LEADING ';
break; break;
case self::TRIM_TRAILING: case TrimMode::TRAILING:
$expression = 'TRAILING '; $expression = 'TRAILING ';
break; break;
case self::TRIM_BOTH: case TrimMode::BOTH:
$expression = 'BOTH '; $expression = 'BOTH ';
break; break;
} }
if (false !== $char) { if ($char !== false) {
$expression .= $char . ' '; $expression .= $char . ' ';
} }
if ($pos || false !== $char) { if ($mode || $char !== false) {
$expression .= 'FROM '; $expression .= 'FROM ';
} }
......
...@@ -1103,13 +1103,13 @@ class SQLAnywherePlatform extends AbstractPlatform ...@@ -1103,13 +1103,13 @@ class SQLAnywherePlatform extends AbstractPlatform
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getTrimExpression($str, $pos = self::TRIM_UNSPECIFIED, $char = false) public function getTrimExpression($str, $pos = TrimMode::UNSPECIFIED, $char = false)
{ {
if ( ! $char) { if ( ! $char) {
switch ($pos) { switch ($pos) {
case self::TRIM_LEADING: case TrimMode::LEADING:
return $this->getLtrimExpression($str); return $this->getLtrimExpression($str);
case self::TRIM_TRAILING: case TrimMode::TRAILING:
return $this->getRtrimExpression($str); return $this->getRtrimExpression($str);
default: default:
return 'TRIM(' . $str . ')'; return 'TRIM(' . $str . ')';
...@@ -1119,9 +1119,9 @@ class SQLAnywherePlatform extends AbstractPlatform ...@@ -1119,9 +1119,9 @@ class SQLAnywherePlatform extends AbstractPlatform
$pattern = "'%[^' + $char + ']%'"; $pattern = "'%[^' + $char + ']%'";
switch ($pos) { switch ($pos) {
case self::TRIM_LEADING: case TrimMode::LEADING:
return 'SUBSTR(' . $str . ', PATINDEX(' . $pattern . ', ' . $str . '))'; return 'SUBSTR(' . $str . ', PATINDEX(' . $pattern . ', ' . $str . '))';
case self::TRIM_TRAILING: case TrimMode::TRAILING:
return 'REVERSE(SUBSTR(REVERSE(' . $str . '), PATINDEX(' . $pattern . ', REVERSE(' . $str . '))))'; return 'REVERSE(SUBSTR(REVERSE(' . $str . '), PATINDEX(' . $pattern . ', REVERSE(' . $str . '))))';
default: default:
return return
......
...@@ -1003,15 +1003,15 @@ class SQLServerPlatform extends AbstractPlatform ...@@ -1003,15 +1003,15 @@ class SQLServerPlatform extends AbstractPlatform
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getTrimExpression($str, $pos = self::TRIM_UNSPECIFIED, $char = false) public function getTrimExpression($str, $pos = TrimMode::UNSPECIFIED, $char = false)
{ {
if ( ! $char) { if ( ! $char) {
switch ($pos) { switch ($pos) {
case self::TRIM_LEADING: case TrimMode::LEADING:
$trimFn = 'LTRIM'; $trimFn = 'LTRIM';
break; break;
case self::TRIM_TRAILING: case TrimMode::TRAILING:
$trimFn = 'RTRIM'; $trimFn = 'RTRIM';
break; break;
...@@ -1033,11 +1033,11 @@ class SQLServerPlatform extends AbstractPlatform ...@@ -1033,11 +1033,11 @@ class SQLServerPlatform extends AbstractPlatform
*/ */
$pattern = "'%[^' + $char + ']%'"; $pattern = "'%[^' + $char + ']%'";
if ($pos == self::TRIM_LEADING) { if ($pos === TrimMode::LEADING) {
return 'stuff(' . $str . ', 1, patindex(' . $pattern . ', ' . $str . ') - 1, null)'; return 'stuff(' . $str . ', 1, patindex(' . $pattern . ', ' . $str . ') - 1, null)';
} }
if ($pos == self::TRIM_TRAILING) { if ($pos === TrimMode::TRAILING) {
return 'reverse(stuff(reverse(' . $str . '), 1, patindex(' . $pattern . ', reverse(' . $str . ')) - 1, null))'; return 'reverse(stuff(reverse(' . $str . '), 1, patindex(' . $pattern . ', reverse(' . $str . ')) - 1, null))';
} }
......
...@@ -80,16 +80,16 @@ class SqlitePlatform extends AbstractPlatform ...@@ -80,16 +80,16 @@ class SqlitePlatform extends AbstractPlatform
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getTrimExpression($str, $pos = self::TRIM_UNSPECIFIED, $char = false) public function getTrimExpression($str, $pos = TrimMode::UNSPECIFIED, $char = false)
{ {
$trimChar = ($char != false) ? (', ' . $char) : ''; $trimChar = ($char != false) ? (', ' . $char) : '';
switch ($pos) { switch ($pos) {
case self::TRIM_LEADING: case TrimMode::LEADING:
$trimFn = 'LTRIM'; $trimFn = 'LTRIM';
break; break;
case self::TRIM_TRAILING: case TrimMode::TRAILING:
$trimFn = 'RTRIM'; $trimFn = 'RTRIM';
break; break;
......
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Platforms;
final class TrimMode
{
public const UNSPECIFIED = 0;
public const LEADING = 1;
public const TRAILING = 2;
public const BOTH = 3;
private function __construct()
{
}
}
...@@ -4,6 +4,7 @@ namespace Doctrine\Tests\DBAL\Functional; ...@@ -4,6 +4,7 @@ namespace Doctrine\Tests\DBAL\Functional;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\TrimMode;
use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Type;
use PDO; use PDO;
...@@ -459,42 +460,42 @@ class DataAccessTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -459,42 +460,42 @@ class DataAccessTest extends \Doctrine\Tests\DbalFunctionalTestCase
public function getTrimExpressionData() public function getTrimExpressionData()
{ {
return array( return array(
array('test_string', AbstractPlatform::TRIM_UNSPECIFIED, false, 'foo'), ['test_string', TrimMode::UNSPECIFIED, false, 'foo'],
array('test_string', AbstractPlatform::TRIM_LEADING, false, 'foo'), ['test_string', TrimMode::LEADING, false, 'foo'],
array('test_string', AbstractPlatform::TRIM_TRAILING, false, 'foo'), ['test_string', TrimMode::TRAILING, false, 'foo'],
array('test_string', AbstractPlatform::TRIM_BOTH, false, 'foo'), ['test_string', TrimMode::BOTH, false, 'foo'],
array('test_string', AbstractPlatform::TRIM_UNSPECIFIED, "'f'", 'oo'), ['test_string', TrimMode::UNSPECIFIED, "'f'", 'oo'],
array('test_string', AbstractPlatform::TRIM_UNSPECIFIED, "'o'", 'f'), ['test_string', TrimMode::UNSPECIFIED, "'o'", 'f'],
array('test_string', AbstractPlatform::TRIM_UNSPECIFIED, "'.'", 'foo'), ['test_string', TrimMode::UNSPECIFIED, "'.'", 'foo'],
array('test_string', AbstractPlatform::TRIM_LEADING, "'f'", 'oo'), ['test_string', TrimMode::LEADING, "'f'", 'oo'],
array('test_string', AbstractPlatform::TRIM_LEADING, "'o'", 'foo'), ['test_string', TrimMode::LEADING, "'o'", 'foo'],
array('test_string', AbstractPlatform::TRIM_LEADING, "'.'", 'foo'), ['test_string', TrimMode::LEADING, "'.'", 'foo'],
array('test_string', AbstractPlatform::TRIM_TRAILING, "'f'", 'foo'), ['test_string', TrimMode::TRAILING, "'f'", 'foo'],
array('test_string', AbstractPlatform::TRIM_TRAILING, "'o'", 'f'), ['test_string', TrimMode::TRAILING, "'o'", 'f'],
array('test_string', AbstractPlatform::TRIM_TRAILING, "'.'", 'foo'), ['test_string', TrimMode::TRAILING, "'.'", 'foo'],
array('test_string', AbstractPlatform::TRIM_BOTH, "'f'", 'oo'), ['test_string', TrimMode::BOTH, "'f'", 'oo'],
array('test_string', AbstractPlatform::TRIM_BOTH, "'o'", 'f'), ['test_string', TrimMode::BOTH, "'o'", 'f'],
array('test_string', AbstractPlatform::TRIM_BOTH, "'.'", 'foo'), ['test_string', TrimMode::BOTH, "'.'", 'foo'],
array("' foo '", AbstractPlatform::TRIM_UNSPECIFIED, false, 'foo'), ["' foo '", TrimMode::UNSPECIFIED, false, 'foo'],
array("' foo '", AbstractPlatform::TRIM_LEADING, false, 'foo '), ["' foo '", TrimMode::LEADING, false, 'foo '],
array("' foo '", AbstractPlatform::TRIM_TRAILING, false, ' foo'), ["' foo '", TrimMode::TRAILING, false, ' foo'],
array("' foo '", AbstractPlatform::TRIM_BOTH, false, 'foo'), ["' foo '", TrimMode::BOTH, false, 'foo'],
array("' foo '", AbstractPlatform::TRIM_UNSPECIFIED, "'f'", ' foo '), ["' foo '", TrimMode::UNSPECIFIED, "'f'", ' foo '],
array("' foo '", AbstractPlatform::TRIM_UNSPECIFIED, "'o'", ' foo '), ["' foo '", TrimMode::UNSPECIFIED, "'o'", ' foo '],
array("' foo '", AbstractPlatform::TRIM_UNSPECIFIED, "'.'", ' foo '), ["' foo '", TrimMode::UNSPECIFIED, "'.'", ' foo '],
array("' foo '", AbstractPlatform::TRIM_UNSPECIFIED, "' '", 'foo'), ["' foo '", TrimMode::UNSPECIFIED, "' '", 'foo'],
array("' foo '", AbstractPlatform::TRIM_LEADING, "'f'", ' foo '), ["' foo '", TrimMode::LEADING, "'f'", ' foo '],
array("' foo '", AbstractPlatform::TRIM_LEADING, "'o'", ' foo '), ["' foo '", TrimMode::LEADING, "'o'", ' foo '],
array("' foo '", AbstractPlatform::TRIM_LEADING, "'.'", ' foo '), ["' foo '", TrimMode::LEADING, "'.'", ' foo '],
array("' foo '", AbstractPlatform::TRIM_LEADING, "' '", 'foo '), ["' foo '", TrimMode::LEADING, "' '", 'foo '],
array("' foo '", AbstractPlatform::TRIM_TRAILING, "'f'", ' foo '), ["' foo '", TrimMode::TRAILING, "'f'", ' foo '],
array("' foo '", AbstractPlatform::TRIM_TRAILING, "'o'", ' foo '), ["' foo '", TrimMode::TRAILING, "'o'", ' foo '],
array("' foo '", AbstractPlatform::TRIM_TRAILING, "'.'", ' foo '), ["' foo '", TrimMode::TRAILING, "'.'", ' foo '],
array("' foo '", AbstractPlatform::TRIM_TRAILING, "' '", ' foo'), ["' foo '", TrimMode::TRAILING, "' '", ' foo'],
array("' foo '", AbstractPlatform::TRIM_BOTH, "'f'", ' foo '), ["' foo '", TrimMode::BOTH, "'f'", ' foo '],
array("' foo '", AbstractPlatform::TRIM_BOTH, "'o'", ' foo '), ["' foo '", TrimMode::BOTH, "'o'", ' foo '],
array("' foo '", AbstractPlatform::TRIM_BOTH, "'.'", ' foo '), ["' foo '", TrimMode::BOTH, "'.'", ' foo '],
array("' foo '", AbstractPlatform::TRIM_BOTH, "' '", 'foo'), ["' foo '", TrimMode::BOTH, "' '", 'foo'],
); );
} }
......
...@@ -6,6 +6,7 @@ use Doctrine\DBAL\Connection; ...@@ -6,6 +6,7 @@ use Doctrine\DBAL\Connection;
use Doctrine\DBAL\LockMode; use Doctrine\DBAL\LockMode;
use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\SQLAnywherePlatform; use Doctrine\DBAL\Platforms\SQLAnywherePlatform;
use Doctrine\DBAL\Platforms\TrimMode;
use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\ColumnDiff; use Doctrine\DBAL\Schema\ColumnDiff;
use Doctrine\DBAL\Schema\Comparator; use Doctrine\DBAL\Schema\Comparator;
...@@ -550,11 +551,11 @@ class SQLAnywherePlatformTest extends AbstractPlatformTestCase ...@@ -550,11 +551,11 @@ class SQLAnywherePlatformTest extends AbstractPlatformTestCase
self::assertEquals('GLOBAL TEMPORARY', $this->_platform->getTemporaryTableSQL()); self::assertEquals('GLOBAL TEMPORARY', $this->_platform->getTemporaryTableSQL());
self::assertEquals( self::assertEquals(
'LTRIM(column)', 'LTRIM(column)',
$this->_platform->getTrimExpression('column', AbstractPlatform::TRIM_LEADING) $this->_platform->getTrimExpression('column', TrimMode::LEADING)
); );
self::assertEquals( self::assertEquals(
'RTRIM(column)', 'RTRIM(column)',
$this->_platform->getTrimExpression('column', AbstractPlatform::TRIM_TRAILING) $this->_platform->getTrimExpression('column', TrimMode::TRAILING)
); );
self::assertEquals( self::assertEquals(
'TRIM(column)', 'TRIM(column)',
...@@ -562,15 +563,15 @@ class SQLAnywherePlatformTest extends AbstractPlatformTestCase ...@@ -562,15 +563,15 @@ class SQLAnywherePlatformTest extends AbstractPlatformTestCase
); );
self::assertEquals( self::assertEquals(
'TRIM(column)', 'TRIM(column)',
$this->_platform->getTrimExpression('column', AbstractPlatform::TRIM_UNSPECIFIED) $this->_platform->getTrimExpression('column', TrimMode::UNSPECIFIED)
); );
self::assertEquals( self::assertEquals(
"SUBSTR(column, PATINDEX('%[^' + c + ']%', column))", "SUBSTR(column, PATINDEX('%[^' + c + ']%', column))",
$this->_platform->getTrimExpression('column', AbstractPlatform::TRIM_LEADING, 'c') $this->_platform->getTrimExpression('column', TrimMode::LEADING, 'c')
); );
self::assertEquals( self::assertEquals(
"REVERSE(SUBSTR(REVERSE(column), PATINDEX('%[^' + c + ']%', REVERSE(column))))", "REVERSE(SUBSTR(REVERSE(column), PATINDEX('%[^' + c + ']%', REVERSE(column))))",
$this->_platform->getTrimExpression('column', AbstractPlatform::TRIM_TRAILING, 'c') $this->_platform->getTrimExpression('column', TrimMode::TRAILING, 'c')
); );
self::assertEquals( self::assertEquals(
"REVERSE(SUBSTR(REVERSE(SUBSTR(column, PATINDEX('%[^' + c + ']%', column))), PATINDEX('%[^' + c + ']%', " . "REVERSE(SUBSTR(REVERSE(SUBSTR(column, PATINDEX('%[^' + c + ']%', column))), PATINDEX('%[^' + c + ']%', " .
...@@ -580,7 +581,7 @@ class SQLAnywherePlatformTest extends AbstractPlatformTestCase ...@@ -580,7 +581,7 @@ class SQLAnywherePlatformTest extends AbstractPlatformTestCase
self::assertEquals( self::assertEquals(
"REVERSE(SUBSTR(REVERSE(SUBSTR(column, PATINDEX('%[^' + c + ']%', column))), PATINDEX('%[^' + c + ']%', " . "REVERSE(SUBSTR(REVERSE(SUBSTR(column, PATINDEX('%[^' + c + ']%', column))), PATINDEX('%[^' + c + ']%', " .
"REVERSE(SUBSTR(column, PATINDEX('%[^' + c + ']%', column))))))", "REVERSE(SUBSTR(column, PATINDEX('%[^' + c + ']%', column))))))",
$this->_platform->getTrimExpression('column', AbstractPlatform::TRIM_UNSPECIFIED, 'c') $this->_platform->getTrimExpression('column', TrimMode::UNSPECIFIED, 'c')
); );
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment