2323import org .junit .jupiter .api .Test ;
2424
2525import org .springframework .data .annotation .Id ;
26+ import org .springframework .data .domain .PageRequest ;
27+ import org .springframework .data .domain .Sort ;
2628import org .springframework .data .jdbc .core .convert .JdbcCustomConversions ;
2729import org .springframework .data .jdbc .core .convert .JdbcTypeFactory ;
2830import org .springframework .data .jdbc .core .convert .MappingJdbcConverter ;
@@ -41,30 +43,76 @@ class StatementFactoryUnitTests {
4143 JdbcMappingContext mappingContext = new JdbcMappingContext ();
4244 MappingJdbcConverter converter ;
4345
46+ MapSqlParameterSource parameterSource = new MapSqlParameterSource ();
47+ StatementFactory statementFactory ;
48+
4449 @ BeforeEach
4550 void setUp () {
4651
4752 JdbcCustomConversions conversions = JdbcCustomConversions .of (JdbcH2Dialect .INSTANCE , List .of ());
4853 mappingContext .setSimpleTypeHolder (conversions .getSimpleTypeHolder ());
54+ mappingContext .setForceQuote (false );
4955 mappingContext .afterPropertiesSet ();
5056 converter = new MappingJdbcConverter (mappingContext , (identifier , path ) -> null , conversions ,
5157 JdbcTypeFactory .unsupported ());
58+ statementFactory = new StatementFactory (converter , JdbcH2Dialect .INSTANCE );
5259 }
5360
5461 @ Test // GH-2162
55- void statementFactoryConsidersQualifiedTableName () {
62+ void sliceConsiderSort () {
63+
64+ StatementFactory .SelectionBuilder selection = statementFactory .slice (User .class );
65+ selection .page (PageRequest .of (0 , 1 , Sort .by ("id" )));
66+
67+ String sql = selection .build (parameterSource );
68+ assertThat (sql ).contains ("SELECT user.ID" ).contains ("ORDER BY user.ID" );
69+ }
70+
71+ @ Test // GH-2162
72+ void selectConsiderSort () {
73+
74+ StatementFactory .SelectionBuilder selection = statementFactory .select (User .class );
75+ selection .page (PageRequest .of (0 , 1 , Sort .by ("id" )));
76+
77+ String sql = selection .build (parameterSource );
78+ assertThat (sql ).contains ("SELECT user.ID" ).contains ("ORDER BY user.ID" );
79+ }
80+
81+ @ Test // GH-2162
82+ void countDoesNotConsiderSort () {
5683
57- MapSqlParameterSource parameterSource = new MapSqlParameterSource ();
84+ StatementFactory .SelectionBuilder selection = statementFactory .count (User .class );
85+ selection .page (PageRequest .of (0 , 1 , Sort .by ("id" )));
86+
87+ String sql = selection .build (parameterSource );
88+ assertThat (sql ).isEqualTo ("SELECT COUNT(*) FROM user" );
89+ }
90+
91+ @ Test // GH-2162
92+ void existsDoesNotConsiderSort () {
93+
94+ StatementFactory .SelectionBuilder selection = statementFactory .exists (User .class );
95+ selection .page (PageRequest .of (0 , 1 , Sort .by ("id" )));
96+
97+ String sql = selection .build (parameterSource );
98+ assertThat (sql ).isEqualTo ("SELECT user.ID FROM user OFFSET 0 ROWS FETCH FIRST 1 ROWS ONLY" );
99+ }
100+
101+ @ Test // GH-2162
102+ void statementFactoryConsidersQualifiedTableName () {
58103
59- StatementFactory statementFactory = new StatementFactory (converter , JdbcH2Dialect .INSTANCE );
60104 StatementFactory .SelectionBuilder selection = statementFactory .select (Media .class );
61105
62106 String sql = selection .build (parameterSource );
63- assertThat (sql ).contains ("SELECT \" archive\" . \" media\" ." ).contains ("ROM \" archive\" . \" media\" " );
107+ assertThat (sql ).contains ("SELECT archive. media." ).contains ("ROM archive. media" );
64108 }
65109
66110 @ Table (schema = "archive" , name = "media" )
67111 public record Media (@ Id Long id , String objectType , Long objectId ) {
68112 }
69113
114+ @ Table (name = "user" )
115+ public record User (@ Id Long id , String objectType , Long objectId ) {
116+ }
117+
70118}
0 commit comments