Skip to content

Commit

Permalink
Fixed EmptyCondition-problem, some minor changes (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
Schubi1981 authored Apr 17, 2020
1 parent 84fd566 commit bc9fa30
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 29 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# SQLbuilder

[![Maven Central](https://maven-badges.herokuapp.com/maven-central/de.jaggl.sqlbuilder/sqlbuilder-core/badge.svg)](https://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22de.jaggl.sqlbuilder%22%20AND%20a%3A%22sqlbuilder-core%22)
[![Maven Central](https://img.shields.io/maven-metadata/v/http/central.maven.org/maven2/de/jaggl/sqlbuilder/sqlbuilder-core/maven-metadata.xml.svg)](https://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22de.jaggl.sqlbuilder%22%20AND%20a%3A%22sqlbuilder-core%22)
[![Release](https://github.com/de-jaggl/sqlbuilder/workflows/release/badge.svg)](https://github.com/de-jaggl/sqlbuilder/actions)
[![Nightly build](https://github.com/de-jaggl/sqlbuilder/workflows/nightly/badge.svg)](https://github.com/de-jaggl/sqlbuilder/actions)
[![javadoc](https://javadoc.io/badge2/de.jaggl.sqlbuilder/sqlbuilder-core/javadoc.svg?)](https://javadoc.io/doc/de.jaggl.sqlbuilder/sqlbuilder-core)
Expand All @@ -22,7 +22,7 @@ A Java-Library to build SQL-Statements
<dependency>
<groupId>de.jaggl.sqlbuilder</groupId>
<artifactId>sqlbuilder-core</artifactId>
<version>2.6.5</version>
<version>2.6.6</version>
</dependency>
```

Expand Down Expand Up @@ -83,7 +83,7 @@ DELETE FROM `persons` WHERE `persons`.`lastname` = 'Doe'

Create table:
```java
PERSONS.buildCreateTable().println()
Queries.createTable(PERSONS).println()
```
```sql
CREATE TABLE `persons` (`forename` VARCHAR(50) DEFAULT NULL, `lastname` VARCHAR(50) DEFAULT NULL)
Expand Down Expand Up @@ -118,9 +118,9 @@ CREATE TABLE `persons` (`forename` VARCHAR(50) DEFAULT NULL, `lastname` VARCHAR(

- Build queries with or without indentation

### Choose Dialect
### Choose dialect

By default the MySQL-Dialect is chosen. To change the Dialect, you can pass your wanted Dialect to the `print()` or `build()`-method. The known Dialects are collected in the Utility-Class `Dialects`. Simple Example for choose the known Sybase-Dialect:
By default the MySQL-dialect is chosen. To change the dialect, you can pass your wanted dialect to the `print()` or `build()`-method. The known dialects are collected in the Utility-Class `Dialects`. Simple example for choose the known Sybase-dialect:
```java
Queries.select()
.from(PERSONS)
Expand All @@ -131,7 +131,7 @@ This will output:
```sql
SELECT TOP 100 START AT 11 * FROM `persons`
```
It is also possible to glabally change the default-Dialect. To do so, set the system-property `sqlbuilder.defaultDialect` to the name of the Dialect you want.
It is also possible to globally change the default-Dialect. To do so, set the system-property `sqlbuilder.defaultDialect` to the name of the Dialect you want.

### Indentation

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>de.jaggl.sqlbuilder</groupId>
<artifactId>sqlbuilder-core</artifactId>
<version>2.6.5</version>
<version>2.6.6</version>

<packaging>jar</packaging>

Expand Down
9 changes: 9 additions & 0 deletions src/main/java/de/jaggl/sqlbuilder/conditions/Condition.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ public Condition append(Condition other, ConcatenationType concatenationType, Co
other.setConcatenation(concatenationType);
other.setType(conditionType);
CombinedCondition combined;
if (EmptyCondition.class.isAssignableFrom(getClass()))
{
return other;
}
if (!CombinedCondition.class.isAssignableFrom(getClass()))
{
combined = new CombinedCondition(this);
Expand All @@ -105,6 +109,11 @@ public static Condition plain(String plainCondition)
return new PlainCondition(plainCondition);
}

public static Condition emptyCondition()
{
return new EmptyCondition();
}

protected void addPlaceholderSqlTypes(List<Integer> sqlTypes)
{
if (sqlTypes != null)
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/de/jaggl/sqlbuilder/conditions/EmptyCondition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package de.jaggl.sqlbuilder.conditions;

import de.jaggl.sqlbuilder.domain.BuildingContext;
import de.jaggl.sqlbuilder.utils.Indentation;

public class EmptyCondition extends Condition
{
@Override
protected String doBuild(BuildingContext context, Indentation indentation)
{
throw new UnsupportedOperationException("the EmptyCondition is not meant to be build");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ default Condition nEq(String value)
return isNotEqualTo(value);
}

default Condition isIn(Collection<CharSequence> values)
default Condition isIn(Collection<String> values)
{
return new IsIn(this, values.stream().map(value -> (Object) value).collect(Collectors.toList()));
}
Expand All @@ -65,7 +65,7 @@ default Condition isIn(Placeholder placeholder)
return new IsIn(this, placeholder);
}

default Condition isNotIn(Collection<CharSequence> values)
default Condition isNotIn(Collection<String> values)
{
return new IsNotIn(this, values.stream().map(value -> (Object) value).collect(Collectors.toList()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import de.jaggl.sqlbuilder.columns.ColumnDefinition;
import de.jaggl.sqlbuilder.conditions.CombinedCondition;
import de.jaggl.sqlbuilder.conditions.Condition;
import de.jaggl.sqlbuilder.conditions.EmptyCondition;
import de.jaggl.sqlbuilder.domain.BuildingContext;
import de.jaggl.sqlbuilder.domain.ConditionType;
import de.jaggl.sqlbuilder.domain.Groupable;
Expand Down Expand Up @@ -273,7 +274,7 @@ protected void appendJoins(StringBuilder builder, List<Joinable> joins, Building
protected void appendConditions(String keyword, StringBuilder builder, Condition condition, ConditionType whereConditionType, BuildingContext context,
Indentation indentation)
{
if (condition != null)
if (condition != null && !EmptyCondition.class.isAssignableFrom(condition.getClass()))
{
builder.append(context.getDelimiter())
.append(indentation.getIndent())
Expand Down
12 changes: 0 additions & 12 deletions src/main/java/de/jaggl/sqlbuilder/schema/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
import de.jaggl.sqlbuilder.domain.BuildingContext;
import de.jaggl.sqlbuilder.domain.JoinableTable;
import de.jaggl.sqlbuilder.domain.Queryable;
import de.jaggl.sqlbuilder.queries.CreateTable;
import de.jaggl.sqlbuilder.queries.Queries;
import de.jaggl.sqlbuilder.queries.Select;
import de.jaggl.sqlbuilder.utils.BuilderUtils;
import de.jaggl.sqlbuilder.utils.Indentation;
Expand Down Expand Up @@ -268,16 +266,6 @@ <T extends Column> T addColumn(T column)
return column;
}

/**
* Creates a {@link CreateTable} for the current {@link Table}
*
* @return the created {@link CreateTable}
*/
public CreateTable buildCreateTable()
{
return Queries.createTable(this);
}

/**
* Creates a {@link Table} with the given name
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

import org.junit.jupiter.api.Test;

public class CombinedConditionTest
class CombinedConditionTest
{
@Test
public void testGetCopy()
void testGetCopy()
{
assertThat(CombinedCondition.getCopy(null)).isNull();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package de.jaggl.sqlbuilder.conditions;

import static org.assertj.core.api.Assertions.assertThatThrownBy;

import org.junit.jupiter.api.Test;

class EmptyConditionTest
{
@Test
void testDoBuild()
{
assertThatThrownBy(() -> new EmptyCondition().doBuild(null, null)).isInstanceOf(UnsupportedOperationException.class)
.hasMessage("the EmptyCondition is not meant to be build");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static de.jaggl.sqlbuilder.dialect.Dialects.MYSQL;
import static de.jaggl.sqlbuilder.dialect.Dialects.SYBASE;
import static de.jaggl.sqlbuilder.queries.Queries.createTable;
import static de.jaggl.sqlbuilder.utils.Indentation.enabled;
import static org.assertj.core.api.Assertions.assertThat;

Expand Down Expand Up @@ -32,7 +33,7 @@ class CreateTableTest
@Test
void testCreateTable()
{
var createTable = PERSONS.buildCreateTable();
var createTable = createTable(PERSONS);
createTable.println();
createTable.println(SYBASE, enabled());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.jaggl.sqlbuilder.queries;

import static de.jaggl.sqlbuilder.queries.Queries.createTable;
import static org.powermock.api.easymock.PowerMock.createStrictMock;
import static org.powermock.api.easymock.PowerMock.replayAll;
import static org.powermock.api.easymock.PowerMock.verifyAll;
Expand All @@ -22,7 +23,7 @@ void testQuery()
runnable.run();

replayAll();
TABLE.buildCreateTable().execute(queryExecutor);
createTable(TABLE).execute(queryExecutor);
verifyAll();
}
}
19 changes: 16 additions & 3 deletions src/test/java/de/jaggl/sqlbuilder/queries/SelectTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.jaggl.sqlbuilder.queries;

import static de.jaggl.sqlbuilder.conditions.Condition.emptyCondition;
import static de.jaggl.sqlbuilder.dialect.Dialects.MYSQL;
import static de.jaggl.sqlbuilder.dialect.Dialects.SYBASE;
import static de.jaggl.sqlbuilder.domain.LikeType.AFTER;
Expand All @@ -20,6 +21,8 @@

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -73,11 +76,15 @@ void testGetDialectByName()
@Test
void testBuildComplexSelect()
{
var subCondition = LASTNAME.isEqualTo("Schumacher")
.or(Condition.plain("IsNull(COL, '') != ''"));
var subCondition = Condition.emptyCondition();

subCondition = subCondition.and(LASTNAME.isEqualTo("Schumacher")
.or(Condition.plain("IsNull(COL, '') != ''")));

subCondition = subCondition.and(FORENAME.isNotNull());

List<String> names = Arrays.asList("Schubi", null, "Ronny");

var select = selectDistinct(FORENAME, LASTNAME, SIZE.as("Gr\\ö`ße"))
.select(Selectable.plain("IsNull(`COL`, '')").as("Color"), sum(AGE).as("ageSum"))
.from(select(count(FORENAME).as("foreCount")).from(PERSONS).as("sub"))
Expand All @@ -87,7 +94,7 @@ void testBuildComplexSelect()
.leftJoin(PERSONS.on(AGE.isEqualTo(FORENAME)))
.where(FORENAME.isNotEqualTo(LASTNAME)
.andNot(LASTNAME.isEqualTo("Sch'umach\\er"))
.and(NICKNAME.isIn("Schubi", null, "Ronny"))
.and(NICKNAME.isIn(names))
.andNot(Condition.plain("IsNull(`COL`, '') != ''"))
.and(FORENAME.isNotNull())
.or(LASTNAME.isEqualTo("Künzel"))
Expand Down Expand Up @@ -242,6 +249,12 @@ void testBuildComplexSelect()
assertThat(Select.copy(select).build(MYSQL)).isEqualTo(select.build(MYSQL));
}

@Test
void testWithEmptyCondition()
{
assertThat(select().from(PERSONS).where(emptyCondition()).build()).isEqualTo("SELECT * FROM `dba`.`persons`");
}

@Test
void testSimplestCopy()
{
Expand Down

0 comments on commit bc9fa30

Please sign in to comment.