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

Commit

Permalink
Join operators.
Browse files Browse the repository at this point in the history
This adds LEFT OUTER JOIN, CROSS JOIN and the optional NATURAL operator.
  • Loading branch information
monxalo committed May 21, 2015
1 parent 222bac0 commit f74404b
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 4 deletions.
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);
}

@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

0 comments on commit f74404b

Please sign in to comment.