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
## 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_*`` were moved into ``Doctrine\DBAL\TransactionIsolationLevel`` class without the ``TRANSACTION_`` prefix.
......
......@@ -111,23 +111,31 @@ abstract class AbstractPlatform
/**
* @var int
*
* @deprecated Use TrimMode::UNSPECIFIED.
*/
const TRIM_UNSPECIFIED = 0;
public const TRIM_UNSPECIFIED = TrimMode::UNSPECIFIED;
/**
* @var int
*
* @deprecated Use TrimMode::LEADING.
*/
const TRIM_LEADING = 1;
public const TRIM_LEADING = TrimMode::LEADING;
/**
* @var int
*
* @deprecated Use TrimMode::TRAILING.
*/
const TRIM_TRAILING = 2;
public const TRIM_TRAILING = TrimMode::TRAILING;
/**
* @var int
*
* @deprecated Use TrimMode::BOTH.
*/
const TRIM_BOTH = 3;
public const TRIM_BOTH = TrimMode::BOTH;
/**
* @var array|null
......@@ -765,35 +773,35 @@ abstract class AbstractPlatform
/**
* Returns the SQL snippet to trim a string.
*
* @param string $str The expression to apply the trim to.
* @param int $pos 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 $str The expression to apply the trim to.
* @param int $mode The position of the trim (leading/trailing/both).
* @param string|bool $char The char to trim, has to be quoted already. Defaults to space.
*
* @return string
*/
public function getTrimExpression($str, $pos = self::TRIM_UNSPECIFIED, $char = false)
public function getTrimExpression($str, $mode = TrimMode::UNSPECIFIED, $char = false)
{
$expression = '';
switch ($pos) {
case self::TRIM_LEADING:
switch ($mode) {
case TrimMode::LEADING:
$expression = 'LEADING ';
break;
case self::TRIM_TRAILING:
case TrimMode::TRAILING:
$expression = 'TRAILING ';
break;
case self::TRIM_BOTH:
case TrimMode::BOTH:
$expression = 'BOTH ';
break;
}
if (false !== $char) {
if ($char !== false) {
$expression .= $char . ' ';
}
if ($pos || false !== $char) {
if ($mode || $char !== false) {
$expression .= 'FROM ';
}
......
......@@ -1103,13 +1103,13 @@ class SQLAnywherePlatform extends AbstractPlatform
/**
* {@inheritdoc}
*/
public function getTrimExpression($str, $pos = self::TRIM_UNSPECIFIED, $char = false)
public function getTrimExpression($str, $pos = TrimMode::UNSPECIFIED, $char = false)
{
if ( ! $char) {
switch ($pos) {
case self::TRIM_LEADING:
case TrimMode::LEADING:
return $this->getLtrimExpression($str);
case self::TRIM_TRAILING:
case TrimMode::TRAILING:
return $this->getRtrimExpression($str);
default:
return 'TRIM(' . $str . ')';
......@@ -1119,9 +1119,9 @@ class SQLAnywherePlatform extends AbstractPlatform
$pattern = "'%[^' + $char + ']%'";
switch ($pos) {
case self::TRIM_LEADING:
case TrimMode::LEADING:
return 'SUBSTR(' . $str . ', PATINDEX(' . $pattern . ', ' . $str . '))';
case self::TRIM_TRAILING:
case TrimMode::TRAILING:
return 'REVERSE(SUBSTR(REVERSE(' . $str . '), PATINDEX(' . $pattern . ', REVERSE(' . $str . '))))';
default:
return
......
......@@ -1003,15 +1003,15 @@ class SQLServerPlatform extends AbstractPlatform
/**
* {@inheritDoc}
*/
public function getTrimExpression($str, $pos = self::TRIM_UNSPECIFIED, $char = false)
public function getTrimExpression($str, $pos = TrimMode::UNSPECIFIED, $char = false)
{
if ( ! $char) {
switch ($pos) {
case self::TRIM_LEADING:
case TrimMode::LEADING:
$trimFn = 'LTRIM';
break;
case self::TRIM_TRAILING:
case TrimMode::TRAILING:
$trimFn = 'RTRIM';
break;
......@@ -1033,11 +1033,11 @@ class SQLServerPlatform extends AbstractPlatform
*/
$pattern = "'%[^' + $char + ']%'";
if ($pos == self::TRIM_LEADING) {
if ($pos === TrimMode::LEADING) {
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))';
}
......
......@@ -80,16 +80,16 @@ class SqlitePlatform extends AbstractPlatform
/**
* {@inheritDoc}
*/
public function getTrimExpression($str, $pos = self::TRIM_UNSPECIFIED, $char = false)
public function getTrimExpression($str, $pos = TrimMode::UNSPECIFIED, $char = false)
{
$trimChar = ($char != false) ? (', ' . $char) : '';
switch ($pos) {
case self::TRIM_LEADING:
case TrimMode::LEADING:
$trimFn = 'LTRIM';
break;
case self::TRIM_TRAILING:
case TrimMode::TRAILING:
$trimFn = 'RTRIM';
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;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\TrimMode;
use Doctrine\DBAL\Types\Type;
use PDO;
......@@ -459,42 +460,42 @@ class DataAccessTest extends \Doctrine\Tests\DbalFunctionalTestCase
public function getTrimExpressionData()
{
return array(
array('test_string', AbstractPlatform::TRIM_UNSPECIFIED, false, 'foo'),
array('test_string', AbstractPlatform::TRIM_LEADING, false, 'foo'),
array('test_string', AbstractPlatform::TRIM_TRAILING, false, 'foo'),
array('test_string', AbstractPlatform::TRIM_BOTH, false, 'foo'),
array('test_string', AbstractPlatform::TRIM_UNSPECIFIED, "'f'", 'oo'),
array('test_string', AbstractPlatform::TRIM_UNSPECIFIED, "'o'", 'f'),
array('test_string', AbstractPlatform::TRIM_UNSPECIFIED, "'.'", 'foo'),
array('test_string', AbstractPlatform::TRIM_LEADING, "'f'", 'oo'),
array('test_string', AbstractPlatform::TRIM_LEADING, "'o'", 'foo'),
array('test_string', AbstractPlatform::TRIM_LEADING, "'.'", 'foo'),
array('test_string', AbstractPlatform::TRIM_TRAILING, "'f'", 'foo'),
array('test_string', AbstractPlatform::TRIM_TRAILING, "'o'", 'f'),
array('test_string', AbstractPlatform::TRIM_TRAILING, "'.'", 'foo'),
array('test_string', AbstractPlatform::TRIM_BOTH, "'f'", 'oo'),
array('test_string', AbstractPlatform::TRIM_BOTH, "'o'", 'f'),
array('test_string', AbstractPlatform::TRIM_BOTH, "'.'", 'foo'),
array("' foo '", AbstractPlatform::TRIM_UNSPECIFIED, false, 'foo'),
array("' foo '", AbstractPlatform::TRIM_LEADING, false, 'foo '),
array("' foo '", AbstractPlatform::TRIM_TRAILING, false, ' foo'),
array("' foo '", AbstractPlatform::TRIM_BOTH, false, 'foo'),
array("' foo '", AbstractPlatform::TRIM_UNSPECIFIED, "'f'", ' foo '),
array("' foo '", AbstractPlatform::TRIM_UNSPECIFIED, "'o'", ' foo '),
array("' foo '", AbstractPlatform::TRIM_UNSPECIFIED, "'.'", ' foo '),
array("' foo '", AbstractPlatform::TRIM_UNSPECIFIED, "' '", 'foo'),
array("' foo '", AbstractPlatform::TRIM_LEADING, "'f'", ' foo '),
array("' foo '", AbstractPlatform::TRIM_LEADING, "'o'", ' foo '),
array("' foo '", AbstractPlatform::TRIM_LEADING, "'.'", ' foo '),
array("' foo '", AbstractPlatform::TRIM_LEADING, "' '", 'foo '),
array("' foo '", AbstractPlatform::TRIM_TRAILING, "'f'", ' foo '),
array("' foo '", AbstractPlatform::TRIM_TRAILING, "'o'", ' foo '),
array("' foo '", AbstractPlatform::TRIM_TRAILING, "'.'", ' foo '),
array("' foo '", AbstractPlatform::TRIM_TRAILING, "' '", ' foo'),
array("' foo '", AbstractPlatform::TRIM_BOTH, "'f'", ' foo '),
array("' foo '", AbstractPlatform::TRIM_BOTH, "'o'", ' foo '),
array("' foo '", AbstractPlatform::TRIM_BOTH, "'.'", ' foo '),
array("' foo '", AbstractPlatform::TRIM_BOTH, "' '", 'foo'),
['test_string', TrimMode::UNSPECIFIED, false, 'foo'],
['test_string', TrimMode::LEADING, false, 'foo'],
['test_string', TrimMode::TRAILING, false, 'foo'],
['test_string', TrimMode::BOTH, false, 'foo'],
['test_string', TrimMode::UNSPECIFIED, "'f'", 'oo'],
['test_string', TrimMode::UNSPECIFIED, "'o'", 'f'],
['test_string', TrimMode::UNSPECIFIED, "'.'", 'foo'],
['test_string', TrimMode::LEADING, "'f'", 'oo'],
['test_string', TrimMode::LEADING, "'o'", 'foo'],
['test_string', TrimMode::LEADING, "'.'", 'foo'],
['test_string', TrimMode::TRAILING, "'f'", 'foo'],
['test_string', TrimMode::TRAILING, "'o'", 'f'],
['test_string', TrimMode::TRAILING, "'.'", 'foo'],
['test_string', TrimMode::BOTH, "'f'", 'oo'],
['test_string', TrimMode::BOTH, "'o'", 'f'],
['test_string', TrimMode::BOTH, "'.'", 'foo'],
["' foo '", TrimMode::UNSPECIFIED, false, 'foo'],
["' foo '", TrimMode::LEADING, false, 'foo '],
["' foo '", TrimMode::TRAILING, false, ' foo'],
["' foo '", TrimMode::BOTH, false, 'foo'],
["' foo '", TrimMode::UNSPECIFIED, "'f'", ' foo '],
["' foo '", TrimMode::UNSPECIFIED, "'o'", ' foo '],
["' foo '", TrimMode::UNSPECIFIED, "'.'", ' foo '],
["' foo '", TrimMode::UNSPECIFIED, "' '", 'foo'],
["' foo '", TrimMode::LEADING, "'f'", ' foo '],
["' foo '", TrimMode::LEADING, "'o'", ' foo '],
["' foo '", TrimMode::LEADING, "'.'", ' foo '],
["' foo '", TrimMode::LEADING, "' '", 'foo '],
["' foo '", TrimMode::TRAILING, "'f'", ' foo '],
["' foo '", TrimMode::TRAILING, "'o'", ' foo '],
["' foo '", TrimMode::TRAILING, "'.'", ' foo '],
["' foo '", TrimMode::TRAILING, "' '", ' foo'],
["' foo '", TrimMode::BOTH, "'f'", ' foo '],
["' foo '", TrimMode::BOTH, "'o'", ' foo '],
["' foo '", TrimMode::BOTH, "'.'", ' foo '],
["' foo '", TrimMode::BOTH, "' '", 'foo'],
);
}
......
......@@ -6,6 +6,7 @@ use Doctrine\DBAL\Connection;
use Doctrine\DBAL\LockMode;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\SQLAnywherePlatform;
use Doctrine\DBAL\Platforms\TrimMode;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\ColumnDiff;
use Doctrine\DBAL\Schema\Comparator;
......@@ -550,11 +551,11 @@ class SQLAnywherePlatformTest extends AbstractPlatformTestCase
self::assertEquals('GLOBAL TEMPORARY', $this->_platform->getTemporaryTableSQL());
self::assertEquals(
'LTRIM(column)',
$this->_platform->getTrimExpression('column', AbstractPlatform::TRIM_LEADING)
$this->_platform->getTrimExpression('column', TrimMode::LEADING)
);
self::assertEquals(
'RTRIM(column)',
$this->_platform->getTrimExpression('column', AbstractPlatform::TRIM_TRAILING)
$this->_platform->getTrimExpression('column', TrimMode::TRAILING)
);
self::assertEquals(
'TRIM(column)',
......@@ -562,15 +563,15 @@ class SQLAnywherePlatformTest extends AbstractPlatformTestCase
);
self::assertEquals(
'TRIM(column)',
$this->_platform->getTrimExpression('column', AbstractPlatform::TRIM_UNSPECIFIED)
$this->_platform->getTrimExpression('column', TrimMode::UNSPECIFIED)
);
self::assertEquals(
"SUBSTR(column, PATINDEX('%[^' + c + ']%', column))",
$this->_platform->getTrimExpression('column', AbstractPlatform::TRIM_LEADING, 'c')
$this->_platform->getTrimExpression('column', TrimMode::LEADING, 'c')
);
self::assertEquals(
"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(
"REVERSE(SUBSTR(REVERSE(SUBSTR(column, PATINDEX('%[^' + c + ']%', column))), PATINDEX('%[^' + c + ']%', " .
......@@ -580,7 +581,7 @@ class SQLAnywherePlatformTest extends AbstractPlatformTestCase
self::assertEquals(
"REVERSE(SUBSTR(REVERSE(SUBSTR(column, PATINDEX('%[^' + c + ']%', column))), PATINDEX('%[^' + c + ']%', " .
"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