diff --git a/README.md b/README.md index f3aed2b..fd9f0cb 100644 --- a/README.md +++ b/README.md @@ -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) @@ -22,7 +22,7 @@ A Java-Library to build SQL-Statements de.jaggl.sqlbuilder sqlbuilder-core - 2.6.5 + 2.6.6 ``` @@ -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) @@ -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) @@ -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 diff --git a/pom.xml b/pom.xml index 8299219..365ca8d 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ de.jaggl.sqlbuilder sqlbuilder-core - 2.6.5 + 2.6.6 jar diff --git a/src/main/java/de/jaggl/sqlbuilder/conditions/Condition.java b/src/main/java/de/jaggl/sqlbuilder/conditions/Condition.java index ac4e2a4..6d5af7f 100644 --- a/src/main/java/de/jaggl/sqlbuilder/conditions/Condition.java +++ b/src/main/java/de/jaggl/sqlbuilder/conditions/Condition.java @@ -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); @@ -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 sqlTypes) { if (sqlTypes != null) diff --git a/src/main/java/de/jaggl/sqlbuilder/conditions/EmptyCondition.java b/src/main/java/de/jaggl/sqlbuilder/conditions/EmptyCondition.java new file mode 100644 index 0000000..4e7d8ce --- /dev/null +++ b/src/main/java/de/jaggl/sqlbuilder/conditions/EmptyCondition.java @@ -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"); + } +} diff --git a/src/main/java/de/jaggl/sqlbuilder/conditions/StringConditions.java b/src/main/java/de/jaggl/sqlbuilder/conditions/StringConditions.java index 2471df7..7784c21 100644 --- a/src/main/java/de/jaggl/sqlbuilder/conditions/StringConditions.java +++ b/src/main/java/de/jaggl/sqlbuilder/conditions/StringConditions.java @@ -50,7 +50,7 @@ default Condition nEq(String value) return isNotEqualTo(value); } - default Condition isIn(Collection values) + default Condition isIn(Collection values) { return new IsIn(this, values.stream().map(value -> (Object) value).collect(Collectors.toList())); } @@ -65,7 +65,7 @@ default Condition isIn(Placeholder placeholder) return new IsIn(this, placeholder); } - default Condition isNotIn(Collection values) + default Condition isNotIn(Collection values) { return new IsNotIn(this, values.stream().map(value -> (Object) value).collect(Collectors.toList())); } diff --git a/src/main/java/de/jaggl/sqlbuilder/dialect/DefaultDialect.java b/src/main/java/de/jaggl/sqlbuilder/dialect/DefaultDialect.java index 3fdd67a..77b11e6 100644 --- a/src/main/java/de/jaggl/sqlbuilder/dialect/DefaultDialect.java +++ b/src/main/java/de/jaggl/sqlbuilder/dialect/DefaultDialect.java @@ -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; @@ -273,7 +274,7 @@ protected void appendJoins(StringBuilder builder, List 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()) diff --git a/src/main/java/de/jaggl/sqlbuilder/schema/Table.java b/src/main/java/de/jaggl/sqlbuilder/schema/Table.java index 6e311b3..119fe9a 100644 --- a/src/main/java/de/jaggl/sqlbuilder/schema/Table.java +++ b/src/main/java/de/jaggl/sqlbuilder/schema/Table.java @@ -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; @@ -268,16 +266,6 @@ 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 * diff --git a/src/test/java/de/jaggl/sqlbuilder/conditions/CombinedConditionTest.java b/src/test/java/de/jaggl/sqlbuilder/conditions/CombinedConditionTest.java index f49aeaf..17c57f0 100644 --- a/src/test/java/de/jaggl/sqlbuilder/conditions/CombinedConditionTest.java +++ b/src/test/java/de/jaggl/sqlbuilder/conditions/CombinedConditionTest.java @@ -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(); } diff --git a/src/test/java/de/jaggl/sqlbuilder/conditions/EmptyConditionTest.java b/src/test/java/de/jaggl/sqlbuilder/conditions/EmptyConditionTest.java new file mode 100644 index 0000000..96561c3 --- /dev/null +++ b/src/test/java/de/jaggl/sqlbuilder/conditions/EmptyConditionTest.java @@ -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"); + } +} diff --git a/src/test/java/de/jaggl/sqlbuilder/queries/CreateTableTest.java b/src/test/java/de/jaggl/sqlbuilder/queries/CreateTableTest.java index 98d5292..c54a7a0 100644 --- a/src/test/java/de/jaggl/sqlbuilder/queries/CreateTableTest.java +++ b/src/test/java/de/jaggl/sqlbuilder/queries/CreateTableTest.java @@ -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; @@ -32,7 +33,7 @@ class CreateTableTest @Test void testCreateTable() { - var createTable = PERSONS.buildCreateTable(); + var createTable = createTable(PERSONS); createTable.println(); createTable.println(SYBASE, enabled()); diff --git a/src/test/java/de/jaggl/sqlbuilder/queries/ExecutableQueryTest.java b/src/test/java/de/jaggl/sqlbuilder/queries/ExecutableQueryTest.java index b1c7a74..707b6e3 100644 --- a/src/test/java/de/jaggl/sqlbuilder/queries/ExecutableQueryTest.java +++ b/src/test/java/de/jaggl/sqlbuilder/queries/ExecutableQueryTest.java @@ -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; @@ -22,7 +23,7 @@ void testQuery() runnable.run(); replayAll(); - TABLE.buildCreateTable().execute(queryExecutor); + createTable(TABLE).execute(queryExecutor); verifyAll(); } } diff --git a/src/test/java/de/jaggl/sqlbuilder/queries/SelectTest.java b/src/test/java/de/jaggl/sqlbuilder/queries/SelectTest.java index 8604c85..360261d 100644 --- a/src/test/java/de/jaggl/sqlbuilder/queries/SelectTest.java +++ b/src/test/java/de/jaggl/sqlbuilder/queries/SelectTest.java @@ -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; @@ -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; @@ -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 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")) @@ -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")) @@ -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() {