Skip to content

Commit

Permalink
Merge pull request #2 from gilgulim/master
Browse files Browse the repository at this point in the history
add allColumns criteria
  • Loading branch information
gchauvet committed May 23, 2018
2 parents 05e24ce + 35b49b6 commit ca9e1bd
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/main/java/io/zatarox/squiggle/SelectQuery.java
Expand Up @@ -37,6 +37,7 @@ public class SelectQuery implements Outputable, ValueSet {
private final List<Order> order = new ArrayList<Order>();

private boolean isDistinct = false;
private boolean isAllColumns = false;

public List<Table> listTables() {
LinkedHashSet<Table> tables = new LinkedHashSet<Table>();
Expand Down Expand Up @@ -74,6 +75,14 @@ public void setDistinct(boolean distinct) {
isDistinct = distinct;
}

public boolean isAllColumns() {
return isAllColumns;
}

public void setAllColumns(boolean allColumns) {
isAllColumns = allColumns;
}

public void addCriteria(Criteria criteria) {
this.criteria.add(criteria);
}
Expand Down Expand Up @@ -130,9 +139,13 @@ public void write(Output out) {
if (isDistinct) {
out.print(" DISTINCT");
}
out.println();

appendIndentedList(out, selection, ",");
if(isAllColumns) {
out.print(" *").println();
}else {
out.println();
appendIndentedList(out, selection, ",");
}

Set<Table> tables = findAllUsedTables();
if (!tables.isEmpty()) {
Expand Down
34 changes: 34 additions & 0 deletions src/test/java/io/zatarox/squiggle/SimplestSelectTest.java
Expand Up @@ -39,4 +39,38 @@ public void simpleSelect() {
+ "FROM people "
+ "ORDER BY people.age DESC"));
}

@Test
public void simpleSelectAllColumns() {
Table people = new Table("people");

SelectQuery select = new SelectQuery();
select.setAllColumns(true);
select.addColumn(people, "firstname");
select.addColumn(people, "lastname");

select.addOrder(people, "age", Order.DESCENDING);

assertThat(select.toString(), IsEqualIgnoringWhiteSpace.equalToIgnoringWhiteSpace(
"SELECT * "
+ "FROM people "
+ "ORDER BY people.age DESC"));
}

@Test
public void simpleSelectDistinct() {
Table people = new Table("people");

SelectQuery select = new SelectQuery();
select.setDistinct(true);
select.addColumn(people, "firstname");
select.addColumn(people, "lastname");

select.addOrder(people, "age", Order.DESCENDING);

assertThat(select.toString(), IsEqualIgnoringWhiteSpace.equalToIgnoringWhiteSpace(
"SELECT DISTINCT people.firstname , people.lastname "
+ "FROM people "
+ "ORDER BY people.age DESC"));
}
}

0 comments on commit ca9e1bd

Please sign in to comment.