Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,9 @@ public String getSelectStatement(String tableName, String columnNames, String wh
query.append(columnNames);
}

if (limit != null && offset != null && !useColumnForPartitioning) {
if (limit != null && offset != null && orderByClause != null && !useColumnForPartitioning) {
query.append(", ROW_NUMBER() OVER(ORDER BY ");
if (orderByClause != null && !orderByClause.isEmpty()) {
query.append(orderByClause);
} else {
// Add a default ORDER BY clause using the newid() function
query.append("newid()");
}
query.append(orderByClause);
query.append(" asc) rnum");
}
query.append(" FROM ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ public String getSelectStatement(String tableName, String columnNames, String wh
if (StringUtils.isEmpty(columnForPartitioning)) {
if (offset != null && limit != null && limit > 0) {
if (StringUtils.isEmpty(orderByClause)) {
// Add a default ORDER BY clause using the newid() function
query.append(" ORDER BY newid()");
throw new IllegalArgumentException("Order by clause cannot be null or empty when using row paging");
}

query.append(" OFFSET ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,6 @@ public void testNoTableName() throws Exception {
db.getSelectStatement("", "some(set),of(columns),that,might,contain,methods,a.*", "", "", null, null);
}

@Test
public void testPagingNoOrderBy() throws Exception {
String sql1 = db.getSelectStatement("database.tablename", "some(set),of(columns),that,might,contain,methods,a.*","",null,10L,0L);
String expected1 = "SELECT * FROM (SELECT TOP 10 some(set),of(columns),that,might,contain,methods,a.*, ROW_NUMBER() OVER(ORDER BY newid() asc) rnum "
+ "FROM database.tablename) A WHERE rnum > 0 AND rnum <= 10";
Assert.assertEquals(sql1,expected1);
}

@Test
public void testTOPQuery() throws Exception {
String sql = db.getSelectStatement("database.tablename", "some(set),of(columns),that,might,contain,methods,a.*", "", "", 100L, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,9 @@ public void testNoTableName() throws Exception {
db.getSelectStatement("", "some(set),of(columns),that,might,contain,methods,a.*","","",null,null);
}

@Test
@Test(expected = IllegalArgumentException.class)
public void testPagingNoOrderBy() throws Exception {
String sql1 = db.getSelectStatement("database.tablename", "some(set),of(columns),that,might,contain,methods,a.*","","",10L,0L);
String expected1 = "SELECT some(set),of(columns),that,might,contain,methods,a.* FROM database.tablename ORDER BY newid() OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY";
Assert.assertEquals(sql1,expected1);
db.getSelectStatement("database.tablename", "some(set),of(columns),that,might,contain,methods,a.*","","",10L,0L);
}

@Test
Expand Down