28
28
import org .springframework .data .annotation .Id ;
29
29
import org .springframework .data .annotation .ReadOnlyProperty ;
30
30
import org .springframework .data .annotation .Version ;
31
+ import org .springframework .data .domain .Example ;
31
32
import org .springframework .data .domain .PageRequest ;
32
33
import org .springframework .data .domain .Pageable ;
33
34
import org .springframework .data .domain .Sort ;
46
47
import org .springframework .data .relational .core .mapping .RelationalMappingContext ;
47
48
import org .springframework .data .relational .core .mapping .RelationalPersistentEntity ;
48
49
import org .springframework .data .relational .core .mapping .RelationalPersistentProperty ;
50
+ import org .springframework .data .relational .core .query .Criteria ;
51
+ import org .springframework .data .relational .core .query .Query ;
49
52
import org .springframework .data .relational .core .sql .Aliased ;
50
53
import org .springframework .data .relational .core .sql .LockMode ;
51
54
import org .springframework .data .relational .core .sql .SqlIdentifier ;
52
55
import org .springframework .data .relational .core .sql .Table ;
56
+ import org .springframework .jdbc .core .namedparam .MapSqlParameterSource ;
53
57
54
58
/**
55
59
* Unit tests for the {@link SqlGenerator}.
64
68
* @author Myeonghyeon Lee
65
69
* @author Mikhail Polivakha
66
70
* @author Chirag Tailor
71
+ * @author Diego Krupitza
67
72
*/
68
73
class SqlGeneratorUnitTests {
69
74
@@ -250,7 +255,8 @@ void findAllSortedWithNullHandling_resolvesNullHandlingWhenDialectSupportsIt() {
250
255
251
256
SqlGenerator sqlGenerator = createSqlGenerator (DummyEntity .class , PostgresDialect .INSTANCE );
252
257
253
- String sql = sqlGenerator .getFindAll (Sort .by (new Sort .Order (Sort .Direction .ASC , "name" , Sort .NullHandling .NULLS_LAST )));
258
+ String sql = sqlGenerator
259
+ .getFindAll (Sort .by (new Sort .Order (Sort .Direction .ASC , "name" , Sort .NullHandling .NULLS_LAST )));
254
260
255
261
assertThat (sql ).contains ("ORDER BY \" dummy_entity\" .\" x_name\" ASC NULLS LAST" );
256
262
}
@@ -260,7 +266,8 @@ void findAllSortedWithNullHandling_ignoresNullHandlingWhenDialectDoesNotSupportI
260
266
261
267
SqlGenerator sqlGenerator = createSqlGenerator (DummyEntity .class , SqlServerDialect .INSTANCE );
262
268
263
- String sql = sqlGenerator .getFindAll (Sort .by (new Sort .Order (Sort .Direction .ASC , "name" , Sort .NullHandling .NULLS_LAST )));
269
+ String sql = sqlGenerator
270
+ .getFindAll (Sort .by (new Sort .Order (Sort .Direction .ASC , "name" , Sort .NullHandling .NULLS_LAST )));
264
271
265
272
assertThat (sql ).endsWith ("ORDER BY dummy_entity.x_name ASC" );
266
273
}
@@ -716,6 +723,25 @@ void columnForReferencedEntityWithoutId() {
716
723
SqlIdentifier .quoted ("child" ), SqlIdentifier .quoted ("CHILD_PARENT_OF_NO_ID_CHILD" ));
717
724
}
718
725
726
+ @ Test
727
+ void selectByQueryValidTest () {
728
+ final SqlGenerator sqlGenerator = createSqlGenerator (DummyEntity .class );
729
+
730
+ DummyEntity probe = new DummyEntity ();
731
+ probe .name = "Diego" ;
732
+
733
+ Criteria criteria = Criteria .where ("name" ).is (probe .name );
734
+ Query query = Query .query (criteria );
735
+
736
+ MapSqlParameterSource parameterSource = new MapSqlParameterSource ();
737
+
738
+ String generatedSQL = sqlGenerator .selectByQuery (query , parameterSource );
739
+ assertThat (generatedSQL ).isNotNull ().contains (":x_name" );
740
+
741
+ assertThat (parameterSource .getValues ()) //
742
+ .containsOnly (entry ("x_name" , probe .name ));
743
+ }
744
+
719
745
private SqlIdentifier getAlias (Object maybeAliased ) {
720
746
721
747
if (maybeAliased instanceof Aliased ) {
@@ -737,7 +763,8 @@ private PersistentPropertyPath<RelationalPersistentProperty> getPath(String path
737
763
@ SuppressWarnings ("unused" )
738
764
static class DummyEntity {
739
765
740
- @ Column ("id1" ) @ Id Long id ;
766
+ @ Column ("id1" )
767
+ @ Id Long id ;
741
768
String name ;
742
769
ReferencedEntity ref ;
743
770
Set <Element > elements ;
@@ -810,7 +837,8 @@ static class EntityWithQuotedColumnName {
810
837
811
838
// these column names behave like single double quote in the name since the get quoted and then doubling the double
812
839
// quote escapes it.
813
- @ Id @ Column ("test\" \" _@id" ) Long id ;
840
+ @ Id
841
+ @ Column ("test\" \" _@id" ) Long id ;
814
842
@ Column ("test\" \" _@123" ) String name ;
815
843
}
816
844
0 commit comments