Skip to content

Commit

Permalink
fixed escaping in sybase-dialect (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
Schubi1981 authored May 13, 2020
1 parent e13b46f commit 8116166
Show file tree
Hide file tree
Showing 27 changed files with 258 additions and 36 deletions.
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.7.0</version>
<version>2.7.1</version>

<packaging>jar</packaging>

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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<TimeColumn, TimeColumnBuilder, LocalDate>
{
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));
}
}
13 changes: 13 additions & 0 deletions src/main/java/de/jaggl/sqlbuilder/core/dialect/DefaultDialect.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -409,6 +416,12 @@ public DateTimeFormatter getDateTimeFormatter()
return DATE_TIME_FORMATTER;
}

@Override
public DateTimeFormatter getTimeFormatter()
{
return TIME_FORMATTER;
}

@Override
public Labels getLabels()
{
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/de/jaggl/sqlbuilder/core/dialect/Dialect.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/de/jaggl/sqlbuilder/core/queries/Insert.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/de/jaggl/sqlbuilder/core/schema/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 extends Column> T addColumn(T column)
{
if (columns == null)
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/de/jaggl/sqlbuilder/core/utils/BuilderUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

import org.junit.jupiter.api.Test;

import de.jaggl.sqlbuilder.core.conditions.CombinedCondition;

class CombinedConditionTest
{
@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

import org.junit.jupiter.api.Test;

import de.jaggl.sqlbuilder.core.conditions.EmptyCondition;

class EmptyConditionTest
{
@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

import org.junit.jupiter.api.Test;

import de.jaggl.sqlbuilder.core.domain.BuildingContext;

class BuildingContextTest
{
@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

import org.junit.jupiter.api.Test;

import de.jaggl.sqlbuilder.core.domain.DoubleSize;

class DoubleSizeTest
{
@Test
Expand Down
Loading

0 comments on commit 8116166

Please sign in to comment.