Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
CWD
CwdDataDoctrineORMBundle
Commits
9626ffe6
Commit
9626ffe6
authored
Jan 21, 2019
by
Bernhard Schussek
Browse files
Overrode the CONCAT() function to support automatic string casting
parent
5f1184b8
Changes
2
Show whitespace changes
Inline
Side-by-side
Func/Postgresql/String/Concat.php
0 → 100644
View file @
9626ffe6
<?php
/*
* This file is part of the CWD Data Doctrine ORM Bundle
*
* (c) cwd.at GmbH <office@cwd.at>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare
(
strict_types
=
1
);
namespace
Cwd\DataDoctrineORMBundle\Func\Postgresql\String
;
use
Doctrine\ORM\Query\AST\Functions\FunctionNode
;
use
Doctrine\ORM\Query\Lexer
;
use
Doctrine\ORM\Query\Parser
;
use
Doctrine\ORM\Query\SqlWalker
;
/**
* Doctrine's native implementation uses PostgreSQL's "||" operator, which
* does not automatically cast non-string types (e.g. INT) to string.
*
* Use CONCAT() instead which casts automatically.
*/
class
Concat
extends
FunctionNode
{
private
$expressions
=
[];
/**
* {@inheritdoc}
*/
public
function
parse
(
Parser
$parser
)
{
$parser
->
match
(
Lexer
::
T_IDENTIFIER
);
$parser
->
match
(
Lexer
::
T_OPEN_PARENTHESIS
);
$this
->
expressions
[]
=
$parser
->
StringPrimary
();
$parser
->
match
(
Lexer
::
T_COMMA
);
$this
->
expressions
[]
=
$parser
->
StringPrimary
();
while
(
!
$parser
->
getLexer
()
->
isNextToken
(
Lexer
::
T_CLOSE_PARENTHESIS
))
{
$parser
->
match
(
Lexer
::
T_COMMA
);
$this
->
expressions
[]
=
$parser
->
StringPrimary
();
}
$parser
->
match
(
Lexer
::
T_CLOSE_PARENTHESIS
);
}
/**
* {@inheritdoc}
*/
public
function
getSql
(
SqlWalker
$sqlWalker
)
{
return
sprintf
(
'CONCAT(%s)'
,
implode
(
', '
,
array_map
(
function
(
$expr
)
use
(
$sqlWalker
)
{
return
$expr
->
dispatch
(
$sqlWalker
);
},
$this
->
expressions
))
);
}
}
OffsetStrategy/CompositeFieldsOffsetStrategy.php
View file @
9626ffe6
...
...
@@ -44,6 +44,9 @@ class CompositeFieldsOffsetStrategy implements OffsetStrategy
/**
* @var string[]
*
* @deprecated We're always casting to text now. Setting this field is not
* required anymore.
*/
private
$explicitCasts
;
...
...
@@ -78,13 +81,13 @@ class CompositeFieldsOffsetStrategy implements OffsetStrategy
sprintf
(
', \'%s\', '
,
$this
->
separator
),
array_map
(
function
(
$sortField
)
use
(
$alias
)
{
return
$this
->
generateFieldName
(
$sortField
,
$alias
);
return
$this
->
addAlias
(
$sortField
,
$alias
);
},
$this
->
fieldNames
)
)
)
:
$this
->
generateFieldName
(
$this
->
fieldNames
[
0
],
$alias
);
:
sprintf
(
'CAST(%s AS TEXT)'
,
$this
->
addAlias
(
$this
->
fieldNames
[
0
],
$alias
)
)
;
$qb
->
addSelect
(
sprintf
(
'%s as offset'
,
$offsetField
));
}
...
...
@@ -200,14 +203,6 @@ class CompositeFieldsOffsetStrategy implements OffsetStrategy
return
$this
->
defaultDirections
;
}
private
function
generateFieldName
(
$fieldName
,
$alias
)
{
return
in_array
(
$fieldName
,
$this
->
explicitCasts
,
true
)
?
sprintf
(
'CAST(%s AS TEXT)'
,
$this
->
addAlias
(
$fieldName
,
$alias
))
:
$this
->
addAlias
(
$fieldName
,
$alias
)
;
}
private
function
addAlias
(
string
$fieldName
,
string
$alias
):
string
{
if
(
false
!==
(
$pos
=
strpos
(
$fieldName
,
'.'
)))
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment