Skip to content

Commit

Permalink
Merge pull request #16 from 2023-2-Design-Pattern/lsj/orderbyASC_DESC
Browse files Browse the repository at this point in the history
feat: asc, desc 키워드 추가, 내림차순 구현
  • Loading branch information
keylime7 authored Nov 19, 2023
2 parents d155679 + 5e7a741 commit 18461c7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/com/holub/database/ConcreteTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ public Table select(Selector where, Collection requestedColumns, Collection othe
resultTable = distinct(resultTable);
}
if(queryOptions.isOrderBy()) {
resultTable = orderBy(resultTable, queryOptions.getOrderByColumns());
resultTable = orderBy(resultTable, queryOptions.getOrderByColumns(), queryOptions.isOrderByASC());
}

return new UnmodifiableTable(resultTable);
Expand Down Expand Up @@ -639,7 +639,7 @@ private Table distinct(Table table) {
return resultTable;
}

private Table orderBy(Table table, List orderByColumns) {
private Table orderBy(Table table, List orderByColumns, boolean ascending) {
ArrayList<String> cols = new ArrayList<>();
for(int i = 0; i < table.rows().columnCount(); i++) {
cols.add(table.rows().columnName(i));
Expand Down Expand Up @@ -669,6 +669,9 @@ private Table orderBy(Table table, List orderByColumns) {
Comparable<Object> value1 = (Comparable<Object>) o1[index];
Comparable<Object> value2 = (Comparable<Object>) o2[index];
int result = value1.compareTo(value2);
if (!ascending) {
result = -result;
}
if (result != 0) {
return result;
}
Expand Down
10 changes: 10 additions & 0 deletions src/com/holub/database/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,8 @@ public boolean containsValue(Object v)
//additional token
DISTINCT = tokens.create( "'DISTINCT" ),
ORDER_BY = tokens.create( "'ORDER BY" ),
ASC = tokens.create( "'ASC" ),
DESC = tokens.create( "'DESC" ),

IDENTIFIER = tokens.create( "[a-zA-Z_0-9/\\\\:~]+" ); //{=Database.lastToken}

Expand Down Expand Up @@ -823,6 +825,14 @@ else if( in.matchAdvance(SELECT) != null )
{
queryOptions.setOrderBy();
queryOptions.setOrderByColumns(idList());

if ( in.matchAdvance(DESC) != null ) {
queryOptions.setOrderByASC(false);
} else if ( in.matchAdvance(ASC) != null ) {
queryOptions.setOrderByASC(true);
} else {
queryOptions.setOrderByASC(true);
}
}

Table result = doSelect(columns, into,
Expand Down
9 changes: 9 additions & 0 deletions src/com/holub/database/QueryOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class QueryOptions {
private boolean orderBy = false;

private List orderByColumns;
private boolean orderByASC = false;

public QueryOptions() {
}
Expand Down Expand Up @@ -34,4 +35,12 @@ public List getOrderByColumns() {
public void setOrderByColumns(List orderByColumns) {
this.orderByColumns = orderByColumns;
}

public boolean isOrderByASC() {
return orderByASC;
}

public void setOrderByASC(boolean bool) {
this.orderByASC = bool;
}
}

0 comments on commit 18461c7

Please sign in to comment.