diff --git a/pom.xml b/pom.xml index 40699f5..46c42b4 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ de.jaggl.sqlbuilder sqlbuilder-core - 2.7.0 + 2.7.1 jar diff --git a/src/main/java/de/jaggl/sqlbuilder/core/columns/datetime/TimeColumn.java b/src/main/java/de/jaggl/sqlbuilder/core/columns/datetime/TimeColumn.java new file mode 100644 index 0000000..15448e9 --- /dev/null +++ b/src/main/java/de/jaggl/sqlbuilder/core/columns/datetime/TimeColumn.java @@ -0,0 +1,161 @@ +package de.jaggl.sqlbuilder.core.columns.datetime; + +import static de.jaggl.sqlbuilder.core.conditions.GenericCondition.GenericConditionType.IS_BETWEEN; +import static de.jaggl.sqlbuilder.core.conditions.GenericCondition.GenericConditionType.IS_EQUAL_TO; +import static de.jaggl.sqlbuilder.core.conditions.GenericCondition.GenericConditionType.IS_GREATER_THAN; +import static de.jaggl.sqlbuilder.core.conditions.GenericCondition.GenericConditionType.IS_GREATER_THAN_OR_EQUAL_TO; +import static de.jaggl.sqlbuilder.core.conditions.GenericCondition.GenericConditionType.IS_LESS_THAN; +import static de.jaggl.sqlbuilder.core.conditions.GenericCondition.GenericConditionType.IS_LESS_THAN_OR_EQUAL_TO; +import static de.jaggl.sqlbuilder.core.conditions.GenericCondition.GenericConditionType.IS_NOT_EQUAL_TO; +import static de.jaggl.sqlbuilder.core.conditions.GenericCondition.GenericConditionType.IS_NOT_NULL; +import static de.jaggl.sqlbuilder.core.conditions.GenericCondition.GenericConditionType.IS_NULL; +import static java.sql.Types.DATE; + +import java.time.LocalTime; + +import de.jaggl.sqlbuilder.core.columns.Column; +import de.jaggl.sqlbuilder.core.columns.ColumnDefinition; +import de.jaggl.sqlbuilder.core.conditions.Condition; +import de.jaggl.sqlbuilder.core.conditions.GenericCondition; +import de.jaggl.sqlbuilder.core.domain.Placeholder; +import de.jaggl.sqlbuilder.core.schema.Table; +import lombok.ToString; + +/** + * @author Martin Schumacher + * + * @since 2.0.0 + */ +@ToString(callSuper = true) +public class TimeColumn extends Column +{ + public TimeColumn(Table table, String name, String alias, ColumnDefinition columnDefinition) + { + super(table, name, alias, columnDefinition, DATE); + } + + public TimeColumn as(String alias) + { + return new TimeColumn(table, name, alias, columnDefinition); + } + + public Condition isEqualTo(LocalTime value) + { + return value == null ? new GenericCondition(IS_NULL, this) : new GenericCondition(IS_EQUAL_TO, this, value); + } + + public Condition eq(LocalTime value) + { + return isEqualTo(value); + } + + public Condition isNotEqualTo(LocalTime value) + { + return value == null ? new GenericCondition(IS_NOT_NULL, this) : new GenericCondition(IS_NOT_EQUAL_TO, this, value); + } + + public Condition nEq(LocalTime value) + { + return isNotEqualTo(value); + } + + public Condition isAfter(LocalTime value) + { + return new GenericCondition(IS_GREATER_THAN, this, value); + } + + public Condition isAfter(Column otherColumn) + { + return new GenericCondition(IS_GREATER_THAN, this, otherColumn); + } + + public Condition isAfter(Placeholder placeholder) + { + return new GenericCondition(IS_GREATER_THAN, this, placeholder); + } + + public Condition isAfterOrEqualTo(LocalTime value) + { + return new GenericCondition(IS_GREATER_THAN_OR_EQUAL_TO, this, value); + } + + public Condition isAfterOrEqualTo(Column otherColumn) + { + return new GenericCondition(IS_GREATER_THAN_OR_EQUAL_TO, this, otherColumn); + } + + public Condition isAfterOrEqualTo(Placeholder placeholder) + { + return new GenericCondition(IS_GREATER_THAN_OR_EQUAL_TO, this, placeholder); + } + + public Condition isBefore(LocalTime value) + { + return new GenericCondition(IS_LESS_THAN, this, value); + } + + public Condition isBefore(Column otherColumn) + { + return new GenericCondition(IS_LESS_THAN, this, otherColumn); + } + + public Condition isBefore(Placeholder placeholder) + { + return new GenericCondition(IS_LESS_THAN, this, placeholder); + } + + public Condition isBeforeOrEqualTo(LocalTime value) + { + return new GenericCondition(IS_LESS_THAN_OR_EQUAL_TO, this, value); + } + + public Condition isBeforeOrEqualTo(Column otherColumn) + { + return new GenericCondition(IS_LESS_THAN_OR_EQUAL_TO, this, otherColumn); + } + + public Condition isBeforeOrEqualTo(Placeholder placeholder) + { + return new GenericCondition(IS_LESS_THAN_OR_EQUAL_TO, this, placeholder); + } + + public Condition isBetween(LocalTime value1, LocalTime value2) + { + return new GenericCondition(IS_BETWEEN, this, value1, value2); + } + + public Condition isBetween(Column otherColumn1, Column otherColumn2) + { + return new GenericCondition(IS_BETWEEN, this, otherColumn1, otherColumn2); + } + + public Condition isBetween(LocalTime value, Column otherColumn) + { + return new GenericCondition(IS_BETWEEN, this, value, otherColumn); + } + + public Condition isBetween(Column otherColumn, LocalTime value) + { + return new GenericCondition(IS_BETWEEN, this, otherColumn, value); + } + + public Condition isBetween(Column otherColumn, Placeholder placeholder) + { + return new GenericCondition(IS_BETWEEN, this, otherColumn, placeholder); + } + + public Condition isBetween(LocalTime value, Placeholder placeholder) + { + return new GenericCondition(IS_BETWEEN, this, value, placeholder); + } + + public Condition isBetween(Placeholder placeholder, LocalTime value) + { + return new GenericCondition(IS_BETWEEN, this, placeholder, value); + } + + public Condition isBetween(Placeholder placeholder, Column otherColumn) + { + return new GenericCondition(IS_BETWEEN, this, placeholder, otherColumn); + } +} diff --git a/src/main/java/de/jaggl/sqlbuilder/core/columns/datetime/TimeColumnBuilder.java b/src/main/java/de/jaggl/sqlbuilder/core/columns/datetime/TimeColumnBuilder.java new file mode 100644 index 0000000..75f7ced --- /dev/null +++ b/src/main/java/de/jaggl/sqlbuilder/core/columns/datetime/TimeColumnBuilder.java @@ -0,0 +1,26 @@ +package de.jaggl.sqlbuilder.core.columns.datetime; + +import java.time.LocalDate; + +import de.jaggl.sqlbuilder.core.columns.ColumnBuilder; +import de.jaggl.sqlbuilder.core.columns.ColumnDefinition; +import de.jaggl.sqlbuilder.core.schema.Table; + +/** + * @author Martin Schumacher + * + * @since 2.0.0 + */ +public class TimeColumnBuilder extends ColumnBuilder +{ + public TimeColumnBuilder(Table table, String name) + { + super(table, name); + } + + @Override + protected TimeColumn getColumnInstance() + { + return new TimeColumn(table, name, null, new ColumnDefinition("TIME", null, isNullable, isDefaultNull, false, false, defaultValue)); + } +} diff --git a/src/main/java/de/jaggl/sqlbuilder/core/dialect/DefaultDialect.java b/src/main/java/de/jaggl/sqlbuilder/core/dialect/DefaultDialect.java index 3583441..5126bf9 100644 --- a/src/main/java/de/jaggl/sqlbuilder/core/dialect/DefaultDialect.java +++ b/src/main/java/de/jaggl/sqlbuilder/core/dialect/DefaultDialect.java @@ -42,9 +42,16 @@ public abstract class DefaultDialect implements Dialect { static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ISO_DATE; static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.nnnnnn"); + static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss.nnnnnn"); private Labels labels = DefaultLabels.getInstance(); + @Override + public String escape(String value, char apostrophe) + { + return value.replace("\\", "\\\\").replace(Character.toString(apostrophe), "\\" + apostrophe); + } + @Override public final String build(Insert insert, Indentation indentation) { @@ -409,6 +416,12 @@ public DateTimeFormatter getDateTimeFormatter() return DATE_TIME_FORMATTER; } + @Override + public DateTimeFormatter getTimeFormatter() + { + return TIME_FORMATTER; + } + @Override public Labels getLabels() { diff --git a/src/main/java/de/jaggl/sqlbuilder/core/dialect/Dialect.java b/src/main/java/de/jaggl/sqlbuilder/core/dialect/Dialect.java index 974ee42..351db1a 100644 --- a/src/main/java/de/jaggl/sqlbuilder/core/dialect/Dialect.java +++ b/src/main/java/de/jaggl/sqlbuilder/core/dialect/Dialect.java @@ -34,8 +34,12 @@ public interface Dialect DateTimeFormatter getDateTimeFormatter(); + DateTimeFormatter getTimeFormatter(); + Labels getLabels(); + String escape(String value, char apostrophe); + public static void register(Dialect dialect) { Dialects.register(dialect); diff --git a/src/main/java/de/jaggl/sqlbuilder/core/dialect/SybaseDialect.java b/src/main/java/de/jaggl/sqlbuilder/core/dialect/SybaseDialect.java index 5e512af..8a2aaa8 100644 --- a/src/main/java/de/jaggl/sqlbuilder/core/dialect/SybaseDialect.java +++ b/src/main/java/de/jaggl/sqlbuilder/core/dialect/SybaseDialect.java @@ -40,6 +40,12 @@ public String getName() return "Sybase"; } + @Override + public String escape(String value, char apostrophe) + { + return value.replace(String.valueOf(apostrophe), String.valueOf(apostrophe) + String.valueOf(apostrophe)); + } + @Override protected void appendInsertStatement(StringBuilder builder, Insert insert, BuildingContext context, Indentation indentation) { diff --git a/src/main/java/de/jaggl/sqlbuilder/core/queries/Insert.java b/src/main/java/de/jaggl/sqlbuilder/core/queries/Insert.java index 6004f37..00226b3 100644 --- a/src/main/java/de/jaggl/sqlbuilder/core/queries/Insert.java +++ b/src/main/java/de/jaggl/sqlbuilder/core/queries/Insert.java @@ -4,12 +4,14 @@ import java.time.LocalDate; import java.time.LocalDateTime; +import java.time.LocalTime; import java.util.LinkedHashMap; import java.util.Map; import de.jaggl.sqlbuilder.core.columns.Column; import de.jaggl.sqlbuilder.core.columns.datetime.DateColumn; import de.jaggl.sqlbuilder.core.columns.datetime.DateTimeColumn; +import de.jaggl.sqlbuilder.core.columns.datetime.TimeColumn; import de.jaggl.sqlbuilder.core.columns.number.NumberColumn; import de.jaggl.sqlbuilder.core.columns.string.StringColumn; import de.jaggl.sqlbuilder.core.dialect.Dialect; @@ -96,6 +98,11 @@ public Insert set(DateTimeColumn column, LocalDateTime value) return addValue(column, new PlainValuable(value)); } + public Insert set(TimeColumn column, LocalTime value) + { + return addValue(column, new PlainValuable(value)); + } + private Insert addValue(Column column, Valuable value) { values.put(column, value); diff --git a/src/main/java/de/jaggl/sqlbuilder/core/schema/Table.java b/src/main/java/de/jaggl/sqlbuilder/core/schema/Table.java index 9be9be9..c2832c0 100644 --- a/src/main/java/de/jaggl/sqlbuilder/core/schema/Table.java +++ b/src/main/java/de/jaggl/sqlbuilder/core/schema/Table.java @@ -10,6 +10,8 @@ import de.jaggl.sqlbuilder.core.columns.datetime.DateColumnBuilder; import de.jaggl.sqlbuilder.core.columns.datetime.DateTimeColumn; import de.jaggl.sqlbuilder.core.columns.datetime.DateTimeColumnBuilder; +import de.jaggl.sqlbuilder.core.columns.datetime.TimeColumn; +import de.jaggl.sqlbuilder.core.columns.datetime.TimeColumnBuilder; import de.jaggl.sqlbuilder.core.columns.number.doubletype.DecimalColumn; import de.jaggl.sqlbuilder.core.columns.number.doubletype.DecimalColumnBuilder; import de.jaggl.sqlbuilder.core.columns.number.doubletype.DoubleColumn; @@ -256,6 +258,17 @@ public DateTimeColumnBuilder dateTimeColumn(String columnName) return new DateTimeColumnBuilder(this, columnName); } + /** + * Creates a {@link TimeColumnBuilder} to build a {@link TimeColumn} with the given name for the current {@link Table} + * + * @param columnName the name for the {@link TimeColumn} to be build + * @return the {@link TimeColumnBuilder} to build a {@link TimeColumn} from + */ + public TimeColumnBuilder timeColumn(String columnName) + { + return new TimeColumnBuilder(this, columnName); + } + T addColumn(T column) { if (columns == null) diff --git a/src/main/java/de/jaggl/sqlbuilder/core/utils/BuilderUtils.java b/src/main/java/de/jaggl/sqlbuilder/core/utils/BuilderUtils.java index a44ecae..174ae02 100644 --- a/src/main/java/de/jaggl/sqlbuilder/core/utils/BuilderUtils.java +++ b/src/main/java/de/jaggl/sqlbuilder/core/utils/BuilderUtils.java @@ -6,6 +6,7 @@ import java.time.LocalDate; import java.time.LocalDateTime; +import java.time.LocalTime; import de.jaggl.sqlbuilder.core.columns.Column; import de.jaggl.sqlbuilder.core.domain.BuildingContext; @@ -63,22 +64,26 @@ private static String getValued(Object value, String prefix, String postfix, Bui { return valueApostrophe(prefix + context.getDialect().getDateTimeFormatter().format((LocalDateTime) value) + postfix, context); } + if (LocalTime.class.isAssignableFrom(value.getClass())) + { + return valueApostrophe(prefix + context.getDialect().getTimeFormatter().format((LocalTime) value) + postfix, context); + } return valueApostrophe(prefix + String.valueOf(value) + postfix, context); } public static String columnApostrophe(String value, BuildingContext context) { - return apostrophe(value, context.getDialect().getLabels().getColumnApostrophe()); + return apostrophe(value, context.getDialect().getLabels().getColumnApostrophe(), context); } public static String valueApostrophe(String value, BuildingContext context) { - return apostrophe(value, context.getDialect().getLabels().getValueApostrophe()); + return apostrophe(value, context.getDialect().getLabels().getValueApostrophe(), context); } - private static String apostrophe(String value, char apostrophe) + private static String apostrophe(String value, char apostrophe, BuildingContext context) { - return apostrophe + value.replaceAll("\\\\", "\\\\\\\\").replaceAll(Character.toString(apostrophe), "\\\\" + apostrophe) + apostrophe; + return apostrophe + context.getDialect().escape(value, apostrophe) + apostrophe; } public static String buildLikePart(Object value, LikeType likeType, BuildingContext context, Indentation indentation) diff --git a/src/test/java/de/jaggl/sqlbuilder/core/conditions/CombinedConditionTest.java b/src/test/java/de/jaggl/sqlbuilder/core/conditions/CombinedConditionTest.java index e6addf5..037a9b4 100644 --- a/src/test/java/de/jaggl/sqlbuilder/core/conditions/CombinedConditionTest.java +++ b/src/test/java/de/jaggl/sqlbuilder/core/conditions/CombinedConditionTest.java @@ -4,8 +4,6 @@ import org.junit.jupiter.api.Test; -import de.jaggl.sqlbuilder.core.conditions.CombinedCondition; - class CombinedConditionTest { @Test diff --git a/src/test/java/de/jaggl/sqlbuilder/core/conditions/EmptyConditionTest.java b/src/test/java/de/jaggl/sqlbuilder/core/conditions/EmptyConditionTest.java index d516001..bfdb0ee 100644 --- a/src/test/java/de/jaggl/sqlbuilder/core/conditions/EmptyConditionTest.java +++ b/src/test/java/de/jaggl/sqlbuilder/core/conditions/EmptyConditionTest.java @@ -4,8 +4,6 @@ import org.junit.jupiter.api.Test; -import de.jaggl.sqlbuilder.core.conditions.EmptyCondition; - class EmptyConditionTest { @Test diff --git a/src/test/java/de/jaggl/sqlbuilder/core/dialect/DialectBuilderTestSupport.java b/src/test/java/de/jaggl/sqlbuilder/core/dialect/DialectBuilderTestSupport.java index 481c8b9..9b75a80 100644 --- a/src/test/java/de/jaggl/sqlbuilder/core/dialect/DialectBuilderTestSupport.java +++ b/src/test/java/de/jaggl/sqlbuilder/core/dialect/DialectBuilderTestSupport.java @@ -3,7 +3,6 @@ import static lombok.AccessLevel.PRIVATE; import de.jaggl.sqlbuilder.core.columns.ColumnDefinition; -import de.jaggl.sqlbuilder.core.dialect.DefaultDialect; import de.jaggl.sqlbuilder.core.domain.BuildingContext; import de.jaggl.sqlbuilder.core.utils.Indentation; import lombok.NoArgsConstructor; diff --git a/src/test/java/de/jaggl/sqlbuilder/core/dialect/DialectTest.java b/src/test/java/de/jaggl/sqlbuilder/core/dialect/DialectTest.java index 9530f11..5435029 100644 --- a/src/test/java/de/jaggl/sqlbuilder/core/dialect/DialectTest.java +++ b/src/test/java/de/jaggl/sqlbuilder/core/dialect/DialectTest.java @@ -7,7 +7,6 @@ import org.junit.jupiter.api.Test; -import de.jaggl.sqlbuilder.core.dialect.Dialect; import de.jaggl.sqlbuilder.core.domain.exceptions.UnknownDialectException; class DialectTest diff --git a/src/test/java/de/jaggl/sqlbuilder/core/domain/BuildingContextTest.java b/src/test/java/de/jaggl/sqlbuilder/core/domain/BuildingContextTest.java index eabd066..0c559bb 100644 --- a/src/test/java/de/jaggl/sqlbuilder/core/domain/BuildingContextTest.java +++ b/src/test/java/de/jaggl/sqlbuilder/core/domain/BuildingContextTest.java @@ -7,8 +7,6 @@ import org.junit.jupiter.api.Test; -import de.jaggl.sqlbuilder.core.domain.BuildingContext; - class BuildingContextTest { @Test diff --git a/src/test/java/de/jaggl/sqlbuilder/core/domain/DoubleSizeTest.java b/src/test/java/de/jaggl/sqlbuilder/core/domain/DoubleSizeTest.java index d596374..4aa2250 100644 --- a/src/test/java/de/jaggl/sqlbuilder/core/domain/DoubleSizeTest.java +++ b/src/test/java/de/jaggl/sqlbuilder/core/domain/DoubleSizeTest.java @@ -4,8 +4,6 @@ import org.junit.jupiter.api.Test; -import de.jaggl.sqlbuilder.core.domain.DoubleSize; - class DoubleSizeTest { @Test diff --git a/src/test/java/de/jaggl/sqlbuilder/core/domain/IntSizeTest.java b/src/test/java/de/jaggl/sqlbuilder/core/domain/IntSizeTest.java index 1bbb400..7cd2a5a 100644 --- a/src/test/java/de/jaggl/sqlbuilder/core/domain/IntSizeTest.java +++ b/src/test/java/de/jaggl/sqlbuilder/core/domain/IntSizeTest.java @@ -4,8 +4,6 @@ import org.junit.jupiter.api.Test; -import de.jaggl.sqlbuilder.core.domain.IntSize; - class IntSizeTest { @Test diff --git a/src/test/java/de/jaggl/sqlbuilder/core/domain/PlaceholderTest.java b/src/test/java/de/jaggl/sqlbuilder/core/domain/PlaceholderTest.java index 40189f7..c736c40 100644 --- a/src/test/java/de/jaggl/sqlbuilder/core/domain/PlaceholderTest.java +++ b/src/test/java/de/jaggl/sqlbuilder/core/domain/PlaceholderTest.java @@ -6,7 +6,6 @@ import org.junit.jupiter.api.Test; import de.jaggl.sqlbuilder.core.columns.string.VarCharColumn; -import de.jaggl.sqlbuilder.core.domain.Plain; import de.jaggl.sqlbuilder.core.schema.Table; class PlaceholderTest diff --git a/src/test/java/de/jaggl/sqlbuilder/core/domain/ValuableTest.java b/src/test/java/de/jaggl/sqlbuilder/core/domain/ValuableTest.java index 0720411..6fd0cef 100644 --- a/src/test/java/de/jaggl/sqlbuilder/core/domain/ValuableTest.java +++ b/src/test/java/de/jaggl/sqlbuilder/core/domain/ValuableTest.java @@ -4,9 +4,6 @@ import org.junit.jupiter.api.Test; -import de.jaggl.sqlbuilder.core.domain.Plain; -import de.jaggl.sqlbuilder.core.domain.Valuable; - class ValuableTest { @Test diff --git a/src/test/java/de/jaggl/sqlbuilder/core/functions/NowTest.java b/src/test/java/de/jaggl/sqlbuilder/core/functions/NowTest.java index 78142b1..699f685 100644 --- a/src/test/java/de/jaggl/sqlbuilder/core/functions/NowTest.java +++ b/src/test/java/de/jaggl/sqlbuilder/core/functions/NowTest.java @@ -12,7 +12,6 @@ import org.junit.jupiter.api.Test; -import de.jaggl.sqlbuilder.core.functions.Function; import de.jaggl.sqlbuilder.core.schema.Table; import de.jaggl.sqlbuilder.core.testsupport.OtherColumnTestSupport; diff --git a/src/test/java/de/jaggl/sqlbuilder/core/queries/CreateTableTest.java b/src/test/java/de/jaggl/sqlbuilder/core/queries/CreateTableTest.java index 2c2d903..5f0a8a5 100644 --- a/src/test/java/de/jaggl/sqlbuilder/core/queries/CreateTableTest.java +++ b/src/test/java/de/jaggl/sqlbuilder/core/queries/CreateTableTest.java @@ -13,7 +13,6 @@ import de.jaggl.sqlbuilder.core.columns.number.doubletype.DoubleColumn; import de.jaggl.sqlbuilder.core.columns.number.integer.IntColumn; import de.jaggl.sqlbuilder.core.columns.string.VarCharColumn; -import de.jaggl.sqlbuilder.core.queries.CreateTable; import de.jaggl.sqlbuilder.core.schema.Schema; import de.jaggl.sqlbuilder.core.schema.Table; diff --git a/src/test/java/de/jaggl/sqlbuilder/core/queries/DeleteTest.java b/src/test/java/de/jaggl/sqlbuilder/core/queries/DeleteTest.java index cac8fa5..5d895ba 100644 --- a/src/test/java/de/jaggl/sqlbuilder/core/queries/DeleteTest.java +++ b/src/test/java/de/jaggl/sqlbuilder/core/queries/DeleteTest.java @@ -14,7 +14,6 @@ import de.jaggl.sqlbuilder.core.columns.number.integer.IntColumn; import de.jaggl.sqlbuilder.core.columns.string.VarCharColumn; -import de.jaggl.sqlbuilder.core.queries.Delete; import de.jaggl.sqlbuilder.core.schema.Table; class DeleteTest diff --git a/src/test/java/de/jaggl/sqlbuilder/core/queries/InsertTest.java b/src/test/java/de/jaggl/sqlbuilder/core/queries/InsertTest.java index 4e641fe..99d44bd 100644 --- a/src/test/java/de/jaggl/sqlbuilder/core/queries/InsertTest.java +++ b/src/test/java/de/jaggl/sqlbuilder/core/queries/InsertTest.java @@ -9,17 +9,18 @@ import java.time.LocalDate; import java.time.LocalDateTime; +import java.time.LocalTime; import org.junit.jupiter.api.Test; import de.jaggl.sqlbuilder.core.columns.datetime.DateColumn; import de.jaggl.sqlbuilder.core.columns.datetime.DateTimeColumn; +import de.jaggl.sqlbuilder.core.columns.datetime.TimeColumn; import de.jaggl.sqlbuilder.core.columns.number.doubletype.DoubleColumn; import de.jaggl.sqlbuilder.core.columns.number.integer.BigIntColumn; import de.jaggl.sqlbuilder.core.columns.number.integer.IntColumn; import de.jaggl.sqlbuilder.core.columns.string.VarCharColumn; import de.jaggl.sqlbuilder.core.domain.Placeholder; -import de.jaggl.sqlbuilder.core.queries.Insert; import de.jaggl.sqlbuilder.core.schema.Table; class InsertTest @@ -34,6 +35,7 @@ class InsertTest public static final IntColumn COUNT = PERSONS.intColumn("count").build(); public static final DateColumn BIRTHDAY = PERSONS.dateColumn("birthday").build(); public static final DateColumn DEATHDAY = PERSONS.dateColumn("deathday").build(); + public static final TimeColumn LUNCH = PERSONS.timeColumn("lunch").build(); public static final DateTimeColumn LAST_UPDATE = PERSONS.dateTimeColumn("lastUpdate").build(); public static final BigIntColumn NUMBERS = PERSONS.bigIntColumn("numbers").build(); @@ -45,6 +47,7 @@ void testBuildInsert() .set(FORENAME, "Martin") .set(BIRTHDAY, now()) .set(DEATHDAY, LocalDate.of(2020, 4, 24)) + .set(LUNCH, LocalTime.of(14, 46)) .set(LAST_UPDATE, LocalDateTime.of(2020, 4, 24, 13, 53)) .set(COUNT, Integer.valueOf(5)) .set(AGE, 38) @@ -56,7 +59,7 @@ void testBuildInsert() insert.println(SYBASE, enabled()); assertThat(insert.build()) - .isEqualTo("INSERT INTO `persons` SET `persons`.`nickname` = `persons`.`forename`, `persons`.`forename` = 'Martin', `persons`.`birthday` = NOW(), `persons`.`deathday` = '2020-04-24', `persons`.`lastUpdate` = '2020-04-24 13:53:00.000000', `persons`.`count` = 5, `persons`.`age` = 38, `persons`.`size` = 175.89, `persons`.`numbers` = :numbers, `persons`.`lastname` = 'Schumacher'"); + .isEqualTo("INSERT INTO `persons` SET `persons`.`nickname` = `persons`.`forename`, `persons`.`forename` = 'Martin', `persons`.`birthday` = NOW(), `persons`.`deathday` = '2020-04-24', `persons`.`lunch` = '14:46:00.000000', `persons`.`lastUpdate` = '2020-04-24 13:53:00.000000', `persons`.`count` = 5, `persons`.`age` = 38, `persons`.`size` = 175.89, `persons`.`numbers` = :numbers, `persons`.`lastname` = 'Schumacher'"); assertThat(insert.build(enabled())) .isEqualTo("INSERT INTO\n" // @@ -66,6 +69,7 @@ void testBuildInsert() + " `persons`.`forename` = 'Martin',\n" // + " `persons`.`birthday` = NOW(),\n" // + " `persons`.`deathday` = '2020-04-24',\n" // + + " `persons`.`lunch` = '14:46:00.000000',\n" // + " `persons`.`lastUpdate` = '2020-04-24 13:53:00.000000',\n" // + " `persons`.`count` = 5,\n" // + " `persons`.`age` = 38,\n" // @@ -74,11 +78,11 @@ void testBuildInsert() + " `persons`.`lastname` = 'Schumacher'"); assertThat(insert.build(SYBASE)) - .isEqualTo("INSERT INTO `persons` (`nickname`, `forename`, `birthday`, `deathday`, `lastUpdate`, `count`, `age`, `size`, `numbers`, `lastname`) VALUES (`forename`, 'Martin', NOW(), '2020-04-24', '2020-04-24 13:53:00.000000', 5, 38, 175.89, :numbers, 'Schumacher')"); + .isEqualTo("INSERT INTO `persons` (`nickname`, `forename`, `birthday`, `deathday`, `lunch`, `lastUpdate`, `count`, `age`, `size`, `numbers`, `lastname`) VALUES (`forename`, 'Martin', NOW(), '2020-04-24', '14:46:00.000000', '2020-04-24 13:53:00.000000', 5, 38, 175.89, :numbers, 'Schumacher')"); assertThat(insert.build(SYBASE, enabled())).isEqualTo("INSERT INTO `persons`\n" // - + " (`nickname`, `forename`, `birthday`, `deathday`, `lastUpdate`, `count`, `age`, `size`, `numbers`, `lastname`)\n" // + + " (`nickname`, `forename`, `birthday`, `deathday`, `lunch`, `lastUpdate`, `count`, `age`, `size`, `numbers`, `lastname`)\n" // + "VALUES\n" // - + " (`forename`, 'Martin', NOW(), '2020-04-24', '2020-04-24 13:53:00.000000', 5, 38, 175.89, :numbers, 'Schumacher')"); + + " (`forename`, 'Martin', NOW(), '2020-04-24', '14:46:00.000000', '2020-04-24 13:53:00.000000', 5, 38, 175.89, :numbers, 'Schumacher')"); assertThat(Insert.copy(insert).build(MYSQL)).isEqualTo(insert.build(MYSQL)); } diff --git a/src/test/java/de/jaggl/sqlbuilder/core/queries/QueryTest.java b/src/test/java/de/jaggl/sqlbuilder/core/queries/QueryTest.java index 5b2ce5f..1bbb2b6 100644 --- a/src/test/java/de/jaggl/sqlbuilder/core/queries/QueryTest.java +++ b/src/test/java/de/jaggl/sqlbuilder/core/queries/QueryTest.java @@ -64,6 +64,7 @@ public void testPrintAndPrintln() query.println(System.out, "MYSQL", enabled()); } + @SuppressWarnings("resource") @Test public void testPrepare() throws SQLException { @@ -81,6 +82,7 @@ public void testPrepare() throws SQLException verifyAll(); } + @SuppressWarnings("resource") @Test public void testPrepareWithDialect() throws SQLException { @@ -98,6 +100,7 @@ public void testPrepareWithDialect() throws SQLException verifyAll(); } + @SuppressWarnings("resource") @Test public void testPrepareWithDialectName() throws SQLException { diff --git a/src/test/java/de/jaggl/sqlbuilder/core/queries/SelectTest.java b/src/test/java/de/jaggl/sqlbuilder/core/queries/SelectTest.java index 44aa14c..7ae7475 100644 --- a/src/test/java/de/jaggl/sqlbuilder/core/queries/SelectTest.java +++ b/src/test/java/de/jaggl/sqlbuilder/core/queries/SelectTest.java @@ -35,7 +35,6 @@ import de.jaggl.sqlbuilder.core.conditions.Condition; import de.jaggl.sqlbuilder.core.domain.Queryable; import de.jaggl.sqlbuilder.core.domain.Selectable; -import de.jaggl.sqlbuilder.core.queries.Select; import de.jaggl.sqlbuilder.core.schema.Schema; import de.jaggl.sqlbuilder.core.schema.Table; @@ -126,7 +125,7 @@ void testBuildComplexSelect() .isEqualTo("SELECT DISTINCT `dba`.`persons`.`forename`, `dba`.`persons`.`lastname`, `dba`.`persons`.`size` AS `Gr\\\\ö\\`ße`, IsNull(`COL`, '') AS `Color`, SUM(`dba`.`persons`.`age`) AS `ageSum` FROM (SELECT COUNT(`dba`.`persons`.`forename`) AS `foreCount` FROM `dba`.`persons`) AS `sub` LEFT OUTER JOIN `dba`.`persons` AS `q` ON (`dba`.`persons`.`forename` = `dba`.`persons`.`nickname` AND `dba`.`persons`.`age` > `dba`.`persons`.`size`) LEFT JOIN `dba`.`persons` ON `dba`.`persons`.`age` = `dba`.`persons`.`forename` WHERE (`dba`.`persons`.`forename` != `dba`.`persons`.`lastname` AND NOT `dba`.`persons`.`lastname` = 'Sch\\'umach\\\\er' AND `dba`.`persons`.`nickname` IN ('Schubi', NULL, 'Ronny') AND NOT IsNull(`COL`, '') != '' AND `dba`.`persons`.`forename` IS NOT NULL OR `dba`.`persons`.`lastname` = 'Künzel' AND `dba`.`persons`.`age` = 12 OR NOT `dba`.`persons`.`size` BETWEEN 40.1234 AND 50.9876 OR `dba`.`persons`.`age` < NULL AND `dba`.`persons`.`lastname` NOT LIKE 'Nils%' AND `dba`.`persons`.`happening` = '2019-02-22 21:11:00.000000' AND MIN(`dba`.`persons`.`age`) >= 50 AND (`dba`.`persons`.`lastname` = 'Schumacher' OR IsNull(COL, '') != '' AND `dba`.`persons`.`forename` IS NOT NULL) AND ((`dba`.`persons`.`age` = `dba`.`persons`.`forename` AND `dba`.`persons`.`forename` IS NOT NULL) OR (`dba`.`persons`.`forename` IS NULL AND `dba`.`persons`.`age` = 12)) AND (`dba`.`persons`.`birthday` <= '2009-07-31' OR `dba`.`persons`.`birthday` LIKE '2019-05-%')) GROUP BY `dba`.`persons`.`lastname`, `dba`.`persons`.`forename`, foreCount HAVING (SUM(`dba`.`persons`.`age`) > 20 AND `dba`.`persons`.`size` BETWEEN 120 AND 150) ORDER BY `dba`.`persons`.`lastname` ASC, `dba`.`persons`.`forename` DESC LIMIT 10, 100"); assertThat(select.build(SYBASE)) - .isEqualTo("SELECT TOP 100 START AT 11 DISTINCT `dba`.`persons`.`forename`, `dba`.`persons`.`lastname`, `dba`.`persons`.`size` AS `Gr\\\\ö\\`ße`, IsNull(`COL`, '') AS `Color`, SUM(`dba`.`persons`.`age`) AS `ageSum` FROM (SELECT COUNT(`dba`.`persons`.`forename`) AS `foreCount` FROM `dba`.`persons`) AS `sub` LEFT OUTER JOIN `dba`.`persons` AS `q` ON (`dba`.`persons`.`forename` = `dba`.`persons`.`nickname` AND `dba`.`persons`.`age` > `dba`.`persons`.`size`) LEFT JOIN `dba`.`persons` ON `dba`.`persons`.`age` = `dba`.`persons`.`forename` WHERE (`dba`.`persons`.`forename` != `dba`.`persons`.`lastname` AND NOT `dba`.`persons`.`lastname` = 'Sch\\'umach\\\\er' AND `dba`.`persons`.`nickname` IN ('Schubi', NULL, 'Ronny') AND NOT IsNull(`COL`, '') != '' AND `dba`.`persons`.`forename` IS NOT NULL OR `dba`.`persons`.`lastname` = 'Künzel' AND `dba`.`persons`.`age` = 12 OR NOT `dba`.`persons`.`size` BETWEEN 40.1234 AND 50.9876 OR `dba`.`persons`.`age` < NULL AND `dba`.`persons`.`lastname` NOT LIKE 'Nils%' AND `dba`.`persons`.`happening` = '2019-02-22 21:11:00.000000' AND MIN(`dba`.`persons`.`age`) >= 50 AND (`dba`.`persons`.`lastname` = 'Schumacher' OR IsNull(COL, '') != '' AND `dba`.`persons`.`forename` IS NOT NULL) AND ((`dba`.`persons`.`age` = `dba`.`persons`.`forename` AND `dba`.`persons`.`forename` IS NOT NULL) OR (`dba`.`persons`.`forename` IS NULL AND `dba`.`persons`.`age` = 12)) AND (`dba`.`persons`.`birthday` <= '2009-07-31' OR `dba`.`persons`.`birthday` LIKE '2019-05-%')) GROUP BY `dba`.`persons`.`lastname`, `dba`.`persons`.`forename`, foreCount HAVING (SUM(`dba`.`persons`.`age`) > 20 AND `dba`.`persons`.`size` BETWEEN 120 AND 150) ORDER BY `dba`.`persons`.`lastname` ASC, `dba`.`persons`.`forename` DESC"); + .isEqualTo("SELECT TOP 100 START AT 11 DISTINCT `dba`.`persons`.`forename`, `dba`.`persons`.`lastname`, `dba`.`persons`.`size` AS `Gr\\ö``ße`, IsNull(`COL`, '') AS `Color`, SUM(`dba`.`persons`.`age`) AS `ageSum` FROM (SELECT COUNT(`dba`.`persons`.`forename`) AS `foreCount` FROM `dba`.`persons`) AS `sub` LEFT OUTER JOIN `dba`.`persons` AS `q` ON (`dba`.`persons`.`forename` = `dba`.`persons`.`nickname` AND `dba`.`persons`.`age` > `dba`.`persons`.`size`) LEFT JOIN `dba`.`persons` ON `dba`.`persons`.`age` = `dba`.`persons`.`forename` WHERE (`dba`.`persons`.`forename` != `dba`.`persons`.`lastname` AND NOT `dba`.`persons`.`lastname` = 'Sch''umach\\er' AND `dba`.`persons`.`nickname` IN ('Schubi', NULL, 'Ronny') AND NOT IsNull(`COL`, '') != '' AND `dba`.`persons`.`forename` IS NOT NULL OR `dba`.`persons`.`lastname` = 'Künzel' AND `dba`.`persons`.`age` = 12 OR NOT `dba`.`persons`.`size` BETWEEN 40.1234 AND 50.9876 OR `dba`.`persons`.`age` < NULL AND `dba`.`persons`.`lastname` NOT LIKE 'Nils%' AND `dba`.`persons`.`happening` = '2019-02-22 21:11:00.000000' AND MIN(`dba`.`persons`.`age`) >= 50 AND (`dba`.`persons`.`lastname` = 'Schumacher' OR IsNull(COL, '') != '' AND `dba`.`persons`.`forename` IS NOT NULL) AND ((`dba`.`persons`.`age` = `dba`.`persons`.`forename` AND `dba`.`persons`.`forename` IS NOT NULL) OR (`dba`.`persons`.`forename` IS NULL AND `dba`.`persons`.`age` = 12)) AND (`dba`.`persons`.`birthday` <= '2009-07-31' OR `dba`.`persons`.`birthday` LIKE '2019-05-%')) GROUP BY `dba`.`persons`.`lastname`, `dba`.`persons`.`forename`, foreCount HAVING (SUM(`dba`.`persons`.`age`) > 20 AND `dba`.`persons`.`size` BETWEEN 120 AND 150) ORDER BY `dba`.`persons`.`lastname` ASC, `dba`.`persons`.`forename` DESC"); assertThat(select.build(MYSQL, enabled())) .isEqualTo("SELECT DISTINCT\n" // @@ -201,13 +200,13 @@ void testBuildComplexSelect() + "LIMIT 10, 100"); assertThat(select.build(SYBASE)) - .isEqualTo("SELECT TOP 100 START AT 11 DISTINCT `dba`.`persons`.`forename`, `dba`.`persons`.`lastname`, `dba`.`persons`.`size` AS `Gr\\\\ö\\`ße`, IsNull(`COL`, '') AS `Color`, SUM(`dba`.`persons`.`age`) AS `ageSum` FROM (SELECT COUNT(`dba`.`persons`.`forename`) AS `foreCount` FROM `dba`.`persons`) AS `sub` LEFT OUTER JOIN `dba`.`persons` AS `q` ON (`dba`.`persons`.`forename` = `dba`.`persons`.`nickname` AND `dba`.`persons`.`age` > `dba`.`persons`.`size`) LEFT JOIN `dba`.`persons` ON `dba`.`persons`.`age` = `dba`.`persons`.`forename` WHERE (`dba`.`persons`.`forename` != `dba`.`persons`.`lastname` AND NOT `dba`.`persons`.`lastname` = 'Sch\\'umach\\\\er' AND `dba`.`persons`.`nickname` IN ('Schubi', NULL, 'Ronny') AND NOT IsNull(`COL`, '') != '' AND `dba`.`persons`.`forename` IS NOT NULL OR `dba`.`persons`.`lastname` = 'Künzel' AND `dba`.`persons`.`age` = 12 OR NOT `dba`.`persons`.`size` BETWEEN 40.1234 AND 50.9876 OR `dba`.`persons`.`age` < NULL AND `dba`.`persons`.`lastname` NOT LIKE 'Nils%' AND `dba`.`persons`.`happening` = '2019-02-22 21:11:00.000000' AND MIN(`dba`.`persons`.`age`) >= 50 AND (`dba`.`persons`.`lastname` = 'Schumacher' OR IsNull(COL, '') != '' AND `dba`.`persons`.`forename` IS NOT NULL) AND ((`dba`.`persons`.`age` = `dba`.`persons`.`forename` AND `dba`.`persons`.`forename` IS NOT NULL) OR (`dba`.`persons`.`forename` IS NULL AND `dba`.`persons`.`age` = 12)) AND (`dba`.`persons`.`birthday` <= '2009-07-31' OR `dba`.`persons`.`birthday` LIKE '2019-05-%')) GROUP BY `dba`.`persons`.`lastname`, `dba`.`persons`.`forename`, foreCount HAVING (SUM(`dba`.`persons`.`age`) > 20 AND `dba`.`persons`.`size` BETWEEN 120 AND 150) ORDER BY `dba`.`persons`.`lastname` ASC, `dba`.`persons`.`forename` DESC"); + .isEqualTo("SELECT TOP 100 START AT 11 DISTINCT `dba`.`persons`.`forename`, `dba`.`persons`.`lastname`, `dba`.`persons`.`size` AS `Gr\\ö``ße`, IsNull(`COL`, '') AS `Color`, SUM(`dba`.`persons`.`age`) AS `ageSum` FROM (SELECT COUNT(`dba`.`persons`.`forename`) AS `foreCount` FROM `dba`.`persons`) AS `sub` LEFT OUTER JOIN `dba`.`persons` AS `q` ON (`dba`.`persons`.`forename` = `dba`.`persons`.`nickname` AND `dba`.`persons`.`age` > `dba`.`persons`.`size`) LEFT JOIN `dba`.`persons` ON `dba`.`persons`.`age` = `dba`.`persons`.`forename` WHERE (`dba`.`persons`.`forename` != `dba`.`persons`.`lastname` AND NOT `dba`.`persons`.`lastname` = 'Sch''umach\\er' AND `dba`.`persons`.`nickname` IN ('Schubi', NULL, 'Ronny') AND NOT IsNull(`COL`, '') != '' AND `dba`.`persons`.`forename` IS NOT NULL OR `dba`.`persons`.`lastname` = 'Künzel' AND `dba`.`persons`.`age` = 12 OR NOT `dba`.`persons`.`size` BETWEEN 40.1234 AND 50.9876 OR `dba`.`persons`.`age` < NULL AND `dba`.`persons`.`lastname` NOT LIKE 'Nils%' AND `dba`.`persons`.`happening` = '2019-02-22 21:11:00.000000' AND MIN(`dba`.`persons`.`age`) >= 50 AND (`dba`.`persons`.`lastname` = 'Schumacher' OR IsNull(COL, '') != '' AND `dba`.`persons`.`forename` IS NOT NULL) AND ((`dba`.`persons`.`age` = `dba`.`persons`.`forename` AND `dba`.`persons`.`forename` IS NOT NULL) OR (`dba`.`persons`.`forename` IS NULL AND `dba`.`persons`.`age` = 12)) AND (`dba`.`persons`.`birthday` <= '2009-07-31' OR `dba`.`persons`.`birthday` LIKE '2019-05-%')) GROUP BY `dba`.`persons`.`lastname`, `dba`.`persons`.`forename`, foreCount HAVING (SUM(`dba`.`persons`.`age`) > 20 AND `dba`.`persons`.`size` BETWEEN 120 AND 150) ORDER BY `dba`.`persons`.`lastname` ASC, `dba`.`persons`.`forename` DESC"); assertThat(select.build(SYBASE, enabled())) .isEqualTo("SELECT TOP 100 START AT 11 DISTINCT\n" // + " `dba`.`persons`.`forename`,\n" // + " `dba`.`persons`.`lastname`,\n" // - + " `dba`.`persons`.`size` AS `Gr\\\\ö\\`ße`,\n" // + + " `dba`.`persons`.`size` AS `Gr\\ö``ße`,\n" // + " IsNull(`COL`, '') AS `Color`,\n" // + " SUM(`dba`.`persons`.`age`) AS `ageSum`\n" // + "FROM\n" // @@ -226,7 +225,7 @@ void testBuildComplexSelect() + "WHERE\n" // + "(\n" // + " `dba`.`persons`.`forename` != `dba`.`persons`.`lastname`\n" // - + " AND NOT `dba`.`persons`.`lastname` = 'Sch\\'umach\\\\er'\n" // + + " AND NOT `dba`.`persons`.`lastname` = 'Sch''umach\\er'\n" // + " AND `dba`.`persons`.`nickname` IN ('Schubi', NULL, 'Ronny')\n" // + " AND NOT IsNull(`COL`, '') != ''\n" // + " AND `dba`.`persons`.`forename` IS NOT NULL\n" // diff --git a/src/test/java/de/jaggl/sqlbuilder/core/queries/UpdateTest.java b/src/test/java/de/jaggl/sqlbuilder/core/queries/UpdateTest.java index d5a6676..4bef858 100644 --- a/src/test/java/de/jaggl/sqlbuilder/core/queries/UpdateTest.java +++ b/src/test/java/de/jaggl/sqlbuilder/core/queries/UpdateTest.java @@ -21,7 +21,6 @@ import de.jaggl.sqlbuilder.core.columns.number.doubletype.DoubleColumn; import de.jaggl.sqlbuilder.core.columns.number.integer.IntColumn; import de.jaggl.sqlbuilder.core.columns.string.VarCharColumn; -import de.jaggl.sqlbuilder.core.queries.Update; import de.jaggl.sqlbuilder.core.schema.Table; class UpdateTest diff --git a/src/test/java/de/jaggl/sqlbuilder/core/schema/TableTest.java b/src/test/java/de/jaggl/sqlbuilder/core/schema/TableTest.java index 3eb24fa..372cff5 100644 --- a/src/test/java/de/jaggl/sqlbuilder/core/schema/TableTest.java +++ b/src/test/java/de/jaggl/sqlbuilder/core/schema/TableTest.java @@ -20,7 +20,6 @@ import de.jaggl.sqlbuilder.core.columns.string.TextColumnBuilder; import de.jaggl.sqlbuilder.core.columns.string.VarCharColumnBuilder; import de.jaggl.sqlbuilder.core.domain.BuildingContext; -import de.jaggl.sqlbuilder.core.schema.Table; class TableTest { diff --git a/src/test/java/de/jaggl/sqlbuilder/core/utils/BuilderUtilsTest.java b/src/test/java/de/jaggl/sqlbuilder/core/utils/BuilderUtilsTest.java index fd373d1..831110f 100644 --- a/src/test/java/de/jaggl/sqlbuilder/core/utils/BuilderUtilsTest.java +++ b/src/test/java/de/jaggl/sqlbuilder/core/utils/BuilderUtilsTest.java @@ -9,6 +9,7 @@ import java.time.LocalDate; import java.time.LocalDateTime; +import java.time.LocalTime; import org.junit.jupiter.api.Test; @@ -35,6 +36,7 @@ void testGetValued() assertThat(getValued(Float.valueOf(55.8f), context, disabled())).isEqualTo("55.8"); assertThat(getValued(LocalDate.of(2019, 12, 27), context, disabled())).isEqualTo("'2019-12-27'"); assertThat(getValued(LocalDateTime.of(2019, 12, 27, 19, 38, 5, 234567), context, disabled())).isEqualTo("'2019-12-27 19:38:05.234567'"); + assertThat(getValued(LocalTime.of(19, 38, 5, 234567), context, disabled())).isEqualTo("'19:38:05.234567'"); assertThat(getValued("anyValue", context, disabled())).isEqualTo("'anyValue'"); } }