|
0
|
1 |
<?php |
|
|
2 |
/* |
|
|
3 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
|
4 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
|
5 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
|
6 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
|
7 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
|
8 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
|
9 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
|
10 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
|
11 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
|
12 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
|
13 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
|
14 |
* |
|
|
15 |
* This software consists of voluntary contributions made by many individuals |
|
|
16 |
* and is licensed under the LGPL. For more information, see |
|
|
17 |
* <http://www.doctrine-project.org>. |
|
|
18 |
*/ |
|
|
19 |
|
|
|
20 |
namespace Doctrine\ORM\Query\AST\Functions; |
|
|
21 |
|
|
|
22 |
use Doctrine\ORM\Query\Lexer; |
|
|
23 |
use Doctrine\DBAL\Platforms\AbstractPlatform; |
|
|
24 |
|
|
|
25 |
/** |
|
|
26 |
* "TRIM" "(" [["LEADING" | "TRAILING" | "BOTH"] [char] "FROM"] StringPrimary ")" |
|
|
27 |
* |
|
|
28 |
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL |
|
|
29 |
* @link www.doctrine-project.org |
|
|
30 |
* @since 2.0 |
|
|
31 |
* @author Guilherme Blanco <guilhermeblanco@hotmail.com> |
|
|
32 |
* @author Jonathan Wage <jonwage@gmail.com> |
|
|
33 |
* @author Roman Borschel <roman@code-factory.org> |
|
|
34 |
* @author Benjamin Eberlei <kontakt@beberlei.de> |
|
|
35 |
*/ |
|
|
36 |
class TrimFunction extends FunctionNode |
|
|
37 |
{ |
|
|
38 |
public $leading; |
|
|
39 |
public $trailing; |
|
|
40 |
public $both; |
|
|
41 |
public $trimChar = false; |
|
|
42 |
public $stringPrimary; |
|
|
43 |
|
|
|
44 |
/** |
|
|
45 |
* @override |
|
|
46 |
*/ |
|
|
47 |
public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker) |
|
|
48 |
{ |
|
|
49 |
$pos = AbstractPlatform::TRIM_UNSPECIFIED; |
|
|
50 |
if ($this->leading) { |
|
|
51 |
$pos = AbstractPlatform::TRIM_LEADING; |
|
|
52 |
} else if ($this->trailing) { |
|
|
53 |
$pos = AbstractPlatform::TRIM_TRAILING; |
|
|
54 |
} else if ($this->both) { |
|
|
55 |
$pos = AbstractPlatform::TRIM_BOTH; |
|
|
56 |
} |
|
|
57 |
|
|
|
58 |
return $sqlWalker->getConnection()->getDatabasePlatform()->getTrimExpression( |
|
|
59 |
$sqlWalker->walkStringPrimary($this->stringPrimary), |
|
|
60 |
$pos, |
|
|
61 |
($this->trimChar != false) ? $sqlWalker->getConnection()->quote($this->trimChar) : false |
|
|
62 |
); |
|
|
63 |
} |
|
|
64 |
|
|
|
65 |
/** |
|
|
66 |
* @override |
|
|
67 |
*/ |
|
|
68 |
public function parse(\Doctrine\ORM\Query\Parser $parser) |
|
|
69 |
{ |
|
|
70 |
$lexer = $parser->getLexer(); |
|
|
71 |
|
|
|
72 |
$parser->match(Lexer::T_IDENTIFIER); |
|
|
73 |
$parser->match(Lexer::T_OPEN_PARENTHESIS); |
|
|
74 |
|
|
|
75 |
if (strcasecmp('leading', $lexer->lookahead['value']) === 0) { |
|
|
76 |
$parser->match(Lexer::T_LEADING); |
|
|
77 |
$this->leading = true; |
|
|
78 |
} else if (strcasecmp('trailing', $lexer->lookahead['value']) === 0) { |
|
|
79 |
$parser->match(Lexer::T_TRAILING); |
|
|
80 |
$this->trailing = true; |
|
|
81 |
} else if (strcasecmp('both', $lexer->lookahead['value']) === 0) { |
|
|
82 |
$parser->match(Lexer::T_BOTH); |
|
|
83 |
$this->both = true; |
|
|
84 |
} |
|
|
85 |
|
|
|
86 |
if ($lexer->isNextToken(Lexer::T_STRING)) { |
|
|
87 |
$parser->match(Lexer::T_STRING); |
|
|
88 |
$this->trimChar = $lexer->token['value']; |
|
|
89 |
} |
|
|
90 |
|
|
|
91 |
if ($this->leading || $this->trailing || $this->both || $this->trimChar) { |
|
|
92 |
$parser->match(Lexer::T_FROM); |
|
|
93 |
} |
|
|
94 |
|
|
|
95 |
$this->stringPrimary = $parser->StringPrimary(); |
|
|
96 |
|
|
|
97 |
$parser->match(Lexer::T_CLOSE_PARENTHESIS); |
|
|
98 |
} |
|
|
99 |
|
|
|
100 |
} |