|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. |
| 5 | + * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. |
| 6 | + */ |
| 7 | +package org.hibernate.dialect; |
| 8 | + |
| 9 | +import java.util.Locale; |
| 10 | + |
| 11 | +import org.hibernate.engine.spi.RowSelection; |
| 12 | +import org.junit.After; |
| 13 | +import org.junit.Before; |
| 14 | +import org.junit.Test; |
| 15 | + |
| 16 | +import org.hibernate.testing.TestForIssue; |
| 17 | +import org.hibernate.testing.junit4.BaseUnitTestCase; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | + |
| 21 | +/** |
| 22 | + * Tests the Limit/Offset handler for {@link SQLServer2012Dialect}. |
| 23 | + * |
| 24 | + * @author Chris Cranford |
| 25 | + */ |
| 26 | +public class SQLServer2012DialectTestCase extends BaseUnitTestCase { |
| 27 | + private SQLServer2012Dialect dialect; |
| 28 | + |
| 29 | + @Before |
| 30 | + public void setup() { |
| 31 | + dialect = new SQLServer2012Dialect(); |
| 32 | + } |
| 33 | + |
| 34 | + @After |
| 35 | + public void tearDown() { |
| 36 | + dialect = null; |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + @TestForIssue(jiraKey = "HHH-8768") |
| 41 | + public void testGetLimitStringMaxRowsOnly() { |
| 42 | + final String input = "select distinct f1 as f53245 from table846752 order by f234, f67 desc"; |
| 43 | + assertEquals( |
| 44 | + input + " offset 0 rows fetch next 10 rows only", |
| 45 | + dialect.getLimitHandler().processSql( input, toRowSelection( 0, 10 ) ).toLowerCase( Locale.ROOT ) |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + @TestForIssue(jiraKey = "HHH-8768") |
| 51 | + public void testGetLimitStringWithOffsetAndMaxRows() { |
| 52 | + final String input = "select distinct f1 as f53245 from table846752 order by f234, f67 desc"; |
| 53 | + assertEquals( |
| 54 | + input + " offset 5 rows fetch next 25 rows only", |
| 55 | + dialect.getLimitHandler().processSql( input, toRowSelection( 5, 25 ) ).toLowerCase( Locale.ROOT ) |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + @TestForIssue(jiraKey = "HHH-8768") |
| 61 | + public void testGetLimitStringMaxRowsOnlyNoOrderBy() { |
| 62 | + // this test defaults back to validating result matches that from SQLServer2005LimitHandler |
| 63 | + // See SQLServer2012LimitHandler for why this falls back |
| 64 | + final String input = "select f1 from table"; |
| 65 | + assertEquals( |
| 66 | + "select top(?) f1 from table", |
| 67 | + dialect.getLimitHandler().processSql( input, toRowSelection( 0, 10 ) ).toLowerCase( Locale.ROOT ) |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + @TestForIssue(jiraKey = "HHH-8768") |
| 73 | + public void testGetLimitStringWithOffsetAndMaxRowsNoOrderBy() { |
| 74 | + // this test defaults back to validating result matches that from SQLServer2005LimitHandler |
| 75 | + // See SQLServer2012LimitHandler for why this falls back |
| 76 | + final String input = "select f1 from table"; |
| 77 | + assertEquals( |
| 78 | + "with query as (select inner_query.*, row_number() over (order by current_timestamp) as __hibernate_row_nr__ " + |
| 79 | + "from ( select f1 as page0_ from table ) inner_query ) select page0_ from query where " + |
| 80 | + "__hibernate_row_nr__ >= ? and __hibernate_row_nr__ < ?", |
| 81 | + dialect.getLimitHandler().processSql( input, toRowSelection( 5, 10 ) ).toLowerCase( Locale.ROOT ) |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + private RowSelection toRowSelection(int firstRow, int maxRows) { |
| 86 | + final RowSelection selection = new RowSelection(); |
| 87 | + selection.setFirstRow( firstRow ); |
| 88 | + selection.setMaxRows( maxRows ); |
| 89 | + return selection; |
| 90 | + } |
| 91 | +} |
0 commit comments