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

Commit

Permalink
Moves the tableExists boolean from the constructor to a new method. T…
Browse files Browse the repository at this point in the history
…hat way it follows the pattern that has been set out already.
  • Loading branch information
shadeven committed Apr 26, 2015
1 parent 1b56cdd commit ad9c5f3
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@
public interface CreateTable {

public CreateTable column(Column column);

public CreateTable ifNotExists();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ public static SelectBuilder select(SelectType type) {
}

public static CreateTableBuilder createTable(String table) {
return new CreateTableBuilder(table, false);
}

public static CreateTableBuilder createTable(String table, boolean tableExists) {
return new CreateTableBuilder(table, tableExists);
return new CreateTableBuilder(table);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ public class CreateTableBuilder extends SegmentBuilder implements CreateTable {

private final List<Column> definitions = new ArrayList<Column>();
private String table;
private boolean tableExists;
private String command = "CREATE TABLE";

public CreateTableBuilder(String table, boolean tableExists) {
public CreateTableBuilder(String table) {
this.table = table;
this.tableExists = tableExists;
}

@Override
Expand All @@ -30,13 +29,14 @@ public CreateTable column(Column column) {
return this;
}

@Override
public CreateTable ifNotExists() {
command = StringUtils.join(" ", command, "IF NOT EXISTS");
return this;
}

@Override
public String build() {
String command = "CREATE TABLE";
if (tableExists) {
command = StringUtils.join(" ", command, "IF NOT EXISTS");
}

return StringUtils.join(" ", command, table + "(" + StringUtils.join(",", definitions.toArray()) + ")");
}
}
14 changes: 1 addition & 13 deletions src/test/java/com/alexfu/sqlitequerybuilder/CreateTableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,9 @@ public final void createTableIfNotExists() {
Column column = new Column("column1", ColumnType.INTEGER, ColumnConstraint.PRIMARY_KEY);

// Act
String query = SQLiteQueryBuilder.createTable("myTable", true).column(column).toString();
String query = SQLiteQueryBuilder.createTable("myTable").ifNotExists().column(column).toString();

// Assert
assertThat(query).isEqualTo("CREATE TABLE IF NOT EXISTS myTable(column1 INTEGER PRIMARY KEY)");
}

@Test
public final void createTableWithoutIfNotExists() {
// Arrange
Column column = new Column("column1", ColumnType.INTEGER, ColumnConstraint.PRIMARY_KEY);

// Act
String query = SQLiteQueryBuilder.createTable("myTable", false).column(column).toString();

// Assert
assertThat(query).isEqualTo("CREATE TABLE myTable(column1 INTEGER PRIMARY KEY)");
}
}

0 comments on commit ad9c5f3

Please sign in to comment.