88
99namespace Ibexa \Core \Search \Legacy \Content \Common \Gateway \CriterionHandler ;
1010
11- use Doctrine \DBAL \Connection ;
1211use Doctrine \DBAL \Query \QueryBuilder ;
1312use Ibexa \Contracts \Core \Repository \Values \Content \Query \Criterion ;
14- use Ibexa \Core \Persistence \TransformationProcessor ;
13+ use Ibexa \Core \Persistence \Legacy \ Content \ Gateway ;
1514use Ibexa \Core \Search \Legacy \Content \Common \Gateway \CriteriaConverter ;
1615use Ibexa \Core \Search \Legacy \Content \Common \Gateway \CriterionHandler ;
1716
17+ /**
18+ * @internal
19+ */
1820final class ContentName extends CriterionHandler
1921{
2022 private const CONTENTOBJECT_NAME_ALIAS = 'ezc_n ' ;
2123 private const CONTENTOBJECT_ALIAS = 'c ' ;
2224
23- private TransformationProcessor $ transformationProcessor ;
24-
25- public function __construct (
26- Connection $ connection ,
27- TransformationProcessor $ transformationProcessor
28- ) {
29- parent ::__construct ($ connection );
30-
31- $ this ->transformationProcessor = $ transformationProcessor ;
32- }
33-
3425 public function accept (Criterion $ criterion ): bool
3526 {
3627 return $ criterion instanceof Criterion \ContentName
@@ -42,6 +33,8 @@ public function accept(Criterion $criterion): bool
4233 * languages: array<string>,
4334 * useAlwaysAvailable: bool,
4435 * } $languageSettings
36+ *
37+ * @throws \Doctrine\DBAL\Exception
4538 */
4639 public function handle (
4740 CriteriaConverter $ converter ,
@@ -51,71 +44,33 @@ public function handle(
5144 ): string {
5245 $ subQuery = $ this ->connection ->createQueryBuilder ();
5346 $ subQuery
54- ->select ('contentobject_id ' )
55- ->distinct ()
56- ->from ('ezcontentobject_name ' , self ::CONTENTOBJECT_NAME_ALIAS )
57- ->innerJoin (
58- self ::CONTENTOBJECT_NAME_ALIAS ,
59- 'ezcontentobject ' ,
60- self ::CONTENTOBJECT_ALIAS ,
61- $ this ->getInnerJoinCondition ()
62- )
47+ ->select ('1 ' )
48+ ->from (Gateway::CONTENT_NAME_TABLE , self ::CONTENTOBJECT_NAME_ALIAS )
6349 ->andWhere (
50+ $ queryBuilder ->expr ()->eq (
51+ self ::CONTENTOBJECT_NAME_ALIAS . '.contentobject_id ' ,
52+ self ::CONTENTOBJECT_ALIAS . '.id '
53+ ),
54+ $ queryBuilder ->expr ()->eq (
55+ self ::CONTENTOBJECT_NAME_ALIAS . '.content_version ' ,
56+ self ::CONTENTOBJECT_ALIAS . '.current_version '
57+ ),
6458 $ queryBuilder ->expr ()->like (
6559 $ this ->toLowerCase (self ::CONTENTOBJECT_NAME_ALIAS . '.name ' ),
66- $ queryBuilder ->createNamedParameter (
67- $ this ->prepareValue ($ criterion )
60+ $ this ->toLowerCase (
61+ $ queryBuilder ->createNamedParameter (
62+ $ this ->prepareValue ($ criterion )
63+ )
6864 )
6965 )
7066 );
7167
72- if (!empty ($ languageSettings ['languages ' ])) {
73- $ this ->addLanguageConditionToSubQuery (
74- $ subQuery ,
75- $ queryBuilder ,
76- $ languageSettings ['languages ' ]
77- );
78- }
79-
80- return $ queryBuilder ->expr ()->in (
81- self ::CONTENTOBJECT_ALIAS . '.id ' ,
82- $ subQuery ->getSQL ()
83- );
84- }
85-
86- private function getInnerJoinCondition (): string
87- {
8868 return sprintf (
89- '(%s = %s AND %s = %s) ' ,
90- self ::CONTENTOBJECT_NAME_ALIAS . '.contentobject_id ' ,
91- self ::CONTENTOBJECT_ALIAS . '.id ' ,
92- self ::CONTENTOBJECT_NAME_ALIAS . '.content_version ' ,
93- self ::CONTENTOBJECT_ALIAS . '.current_version ' ,
69+ 'EXISTS (%s) ' ,
70+ $ subQuery ->getSQL ()
9471 );
9572 }
9673
97- /**
98- * @param array<string> $languages
99- */
100- private function addLanguageConditionToSubQuery (
101- QueryBuilder $ subQuery ,
102- QueryBuilder $ queryBuilder ,
103- array $ languages
104- ): void {
105- $ subQuery
106- ->andWhere (
107- $ queryBuilder ->expr ()->in (
108- $ this ->toLowerCase (self ::CONTENTOBJECT_NAME_ALIAS . '.content_translation ' ),
109- $ this ->toLowerCase (
110- $ queryBuilder ->createNamedParameter (
111- $ languages ,
112- Connection::PARAM_STR_ARRAY
113- )
114- ),
115- )
116- );
117- }
118-
11974 private function prepareValue (Criterion $ criterion ): string
12075 {
12176 /** @var string $value */
@@ -125,20 +80,17 @@ private function prepareValue(Criterion $criterion): string
12580 '* ' ,
12681 '% ' ,
12782 addcslashes (
128- $ this ->transformationProcessor ->transformByGroup (
129- $ value ,
130- 'lowercase '
131- ),
83+ $ value ,
13284 '%_ '
13385 )
13486 );
13587 }
13688
89+ /**
90+ * @throws \Doctrine\DBAL\Exception
91+ */
13792 private function toLowerCase (string $ value ): string
13893 {
139- return sprintf (
140- 'LOWER(%s) ' ,
141- $ value
142- );
94+ return $ this ->connection ->getDatabasePlatform ()->getLowerExpression ($ value );
14395 }
14496}
0 commit comments