1+ /*
2+ * Copyright © 2024 XDEV Software (https://xdev.software)
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
116package software .xdev .spring .data .eclipse .store .repository .query .antlr ;
217
18+ import java .time .LocalDate ;
19+ import java .time .format .DateTimeFormatter ;
320import java .util .Collection ;
21+ import java .util .List ;
422import java .util .Map ;
523import java .util .TreeMap ;
624import java .util .stream .Collectors ;
1432
1533import software .xdev .spring .data .eclipse .store .core .EntityListProvider ;
1634import software .xdev .spring .data .eclipse .store .repository .access .AccessHelper ;
35+ import software .xdev .spring .data .eclipse .store .repository .support .copier .working .WorkingCopier ;
1736
1837
1938public class HSqlQueryExecutor <T >
2039{
2140 private final SQLParser <T > parser ;
2241 private final EntityListProvider entityListProvider ;
2342 private final Class <T > domainClass ;
43+ private final WorkingCopier <T > copier ;
2444
25- public HSqlQueryExecutor (final Class <T > domainClass , final EntityListProvider entityListProvider )
45+ public HSqlQueryExecutor (
46+ final Class <T > domainClass ,
47+ final EntityListProvider entityListProvider ,
48+ final WorkingCopier <T > copier )
2649 {
2750 this .domainClass = domainClass ;
2851 this .parser = SQLParser .forPojoWithAttributes (domainClass , this .createAttributes (domainClass ));
2952 this .entityListProvider = entityListProvider ;
53+ this .copier = copier ;
3054 }
3155
32- public Object execute (final String sqlValue , final Object [] parameters )
56+ public List < T > execute (final String sqlValue , final Object [] parameters )
3357 {
3458 final IndexedCollection <T > entities = new ConcurrentIndexedCollection <>();
3559 entities .addAll (this .entityListProvider .getEntityProvider (this .domainClass ).toCollection ());
3660 final String sqlStringWithReplacedValues = this .replacePlaceholders (sqlValue , parameters );
3761 final ResultSet <T > retrieve = this .parser .retrieve (entities , sqlStringWithReplacedValues );
38- return retrieve .stream ().toList ();
62+ final List <T > results = retrieve .stream ().toList ();
63+ return this .copier .copy (results );
3964 }
4065
41- private String replacePlaceholders (String sqlValue , final Object [] parameters )
66+ private String replacePlaceholders (final String sqlValue , final Object [] parameters )
4267 {
68+ String stringWithReplacedValues = sqlValue ;
4369 // Replace positional placeholders with actual parameter values
4470 for (int i = 0 ; i < parameters .length ; i ++)
4571 {
4672 final String placeholder = "\\ ?" + (i + 1 );
4773 String value = parameters [i ].toString ();
48- if (parameters [i ] instanceof String )
49- {
50- value = "'" + value + "'" ;
51- }
5274 if (parameters [i ] instanceof final Collection collection )
5375 {
5476 value =
@@ -57,9 +79,13 @@ private String replacePlaceholders(String sqlValue, final Object[] parameters)
5779 .collect (Collectors .joining (", " , "(" , ")" ))
5880 .toString ();
5981 }
60- sqlValue = sqlValue .replaceAll (placeholder , value );
82+ if (parameters [i ] instanceof final LocalDate localDate )
83+ {
84+ value = localDate .format (DateTimeFormatter .ofPattern ("YYYY-MM-dd" ));
85+ }
86+ stringWithReplacedValues = stringWithReplacedValues .replaceAll (placeholder , value );
6187 }
62- return sqlValue ;
88+ return stringWithReplacedValues ;
6389 }
6490
6591 private <O > Map <String , ? extends Attribute <O , ?>> createAttributes (final Class <O > domainClass )
0 commit comments