Skip to content
This repository has been archived by the owner on Mar 2, 2019. It is now read-only.

Join operators. #15

Merged
merged 2 commits into from
Jun 4, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -17,7 +17,23 @@ public SelectWhereBuilder where(String condition) {
}

public SelectJoinBuilder join(String table) {
return new SelectJoinBuilder(this, table);
return new SelectJoinBuilder(this, table, JoinType.JOIN);
}

public SelectJoinBuilder leftOuterJoin(String table) {
return new SelectJoinBuilder(this, table, JoinType.LEFT_OUTER_JOIN);
}

public SelectJoinBuilder crossJoin(String table) {
return new SelectJoinBuilder(this, table, JoinType.CROSS_JOIN);
}

public SelectJoinBuilder naturalJoin(String table) {
return new SelectJoinBuilder(this, table, JoinType.NATURAL_JOIN);
}

public SelectJoinBuilder naturalLeftOuterJoin(String table) {
return new SelectJoinBuilder(this, table, JoinType.NATURAL_LEFT_OUTER_JOIN);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

naturalLeftOuterJoin, naturalJoin, crossJoin, and leftOuterJoin in this file can be removed. They are no longer being used anywhere. This will probably bring the code coverage back up as well.

}

@Override
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/alexfu/sqlitequerybuilder/builder/JoinType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.alexfu.sqlitequerybuilder.builder;

public enum JoinType {
JOIN("JOIN"),
CROSS_JOIN("CROSS JOIN"),
LEFT_OUTER_JOIN("LEFT OUTER JOIN"),
NATURAL_JOIN("NATURAL JOIN"),
NATURAL_LEFT_OUTER_JOIN("NATURAL LEFT OUTER JOIN");

private final String sql;

JoinType(String sql) {
this.sql = sql;
}

public final String toSQL() {
return sql;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,23 @@ public SelectWhereBuilder where(String condition) {
}

public SelectJoinBuilder join(String table) {
return new SelectJoinBuilder(this, table);
return new SelectJoinBuilder(this, table, JoinType.JOIN);
}

public SelectJoinBuilder leftOuterJoin(String table) {
return new SelectJoinBuilder(this, table, JoinType.LEFT_OUTER_JOIN);
}

public SelectJoinBuilder crossJoin(String table) {
return new SelectJoinBuilder(this, table, JoinType.CROSS_JOIN);
}

public SelectJoinBuilder naturalJoin(String table) {
return new SelectJoinBuilder(this, table, JoinType.NATURAL_JOIN);
}

public SelectJoinBuilder naturalLeftOuterJoin(String table) {
return new SelectJoinBuilder(this, table, JoinType.NATURAL_LEFT_OUTER_JOIN);
}

public SelectOrderByBuilder orderBy(String column) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ public class SelectJoinBuilder extends SegmentBuilder {

private Builder prefix;
private String table;
private JoinType joinType;

public SelectJoinBuilder(Builder prefix, String table) {
public SelectJoinBuilder(Builder prefix, String table, JoinType joinType) {
this.prefix = prefix;
this.table = table;
this.joinType = joinType;
}

public JoinOnBuilder on(String condition) {
Expand All @@ -20,6 +22,6 @@ public JoinOnBuilder on(String condition) {

@Override
public String build() {
return join(" ", prefix.build(), "JOIN", table);
return join(" ", prefix.build(), joinType.toSQL(), table);
}
}
47 changes: 47 additions & 0 deletions src/test/java/com/alexfu/sqlitequerybuilder/SelectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import com.alexfu.sqlitequerybuilder.api.SQLiteQueryBuilder;
import com.alexfu.sqlitequerybuilder.api.SelectType;
import com.alexfu.sqlitequerybuilder.builder.JoinType;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -63,6 +64,52 @@ public void selectJoinTest() {
assertThat(query).isEqualTo( "SELECT * FROM mytable JOIN secondtable ON mytable.id = secondtable.id WHERE id = 1");
}

@Test
public void selectLeftOuterJoinTest() {
String query = SQLiteQueryBuilder
.select("*")
.from("mytable")
.leftOuterJoin("secondtable")
.on("mytable.id = secondtable.id")
.where("id = 1")
.build();

assertThat(query).isEqualTo( "SELECT * FROM mytable LEFT OUTER JOIN secondtable ON mytable.id = secondtable.id WHERE id = 1");
}

@Test
public void selectCrossJoinTest() {
String query = SQLiteQueryBuilder
.select("*")
.from("mytable")
.crossJoin("secondtable")
.build();

assertThat(query).isEqualTo( "SELECT * FROM mytable CROSS JOIN secondtable");
}

@Test
public void selectNaturalJoinTest() {
String query = SQLiteQueryBuilder
.select("*")
.from("mytable")
.naturalJoin("secondtable")
.build();

assertThat(query).isEqualTo( "SELECT * FROM mytable NATURAL JOIN secondtable");
}

@Test
public void selectNaturalLeftOuterJoinTest() {
String query = SQLiteQueryBuilder
.select("*")
.from("mytable")
.naturalLeftOuterJoin("secondtable")
.build();

assertThat(query).isEqualTo( "SELECT * FROM mytable NATURAL LEFT OUTER JOIN secondtable");
}

@Test
public void selectMultiJoinTest() {
String query = SQLiteQueryBuilder
Expand Down