Skip to content

Commit

Permalink
Fixed Syntax of Inserts for Sybase (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
Schubi1981 authored Apr 27, 2020
1 parent e243860 commit 54f4ace
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ A Java-Library to build SQL-Statements
<dependency>
<groupId>de.jaggl.sqlbuilder</groupId>
<artifactId>sqlbuilder-core</artifactId>
<version>2.6.8</version>
<version>2.6.9</version>
</dependency>
```

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.8</version>
<version>2.6.9</version>

<packaging>jar</packaging>

Expand Down
57 changes: 57 additions & 0 deletions src/main/java/de/jaggl/sqlbuilder/dialect/SybaseDialect.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package de.jaggl.sqlbuilder.dialect;

import static de.jaggl.sqlbuilder.utils.BuilderUtils.columnApostrophe;

import java.util.Map;

import de.jaggl.sqlbuilder.columns.Column;
/**
* @author Martin Schumacher
*
* @since 2.0.0
*/
import de.jaggl.sqlbuilder.domain.BuildingContext;
import de.jaggl.sqlbuilder.domain.Limit;
import de.jaggl.sqlbuilder.domain.Valuable;
import de.jaggl.sqlbuilder.domain.ValuableColumn;
import de.jaggl.sqlbuilder.queries.Delete;
import de.jaggl.sqlbuilder.queries.Insert;
import de.jaggl.sqlbuilder.queries.Select;
import de.jaggl.sqlbuilder.utils.Indentation;

Expand Down Expand Up @@ -37,6 +45,55 @@ public String getName()
return "Sybase";
}

@Override
protected void appendInsertStatement(StringBuilder builder, Insert insert, BuildingContext context, Indentation indentation)
{
builder.append(context.getDialect().getLabels().getInsertInto()).append(" ").append(insert.getTable().getFullName(context));
builder.append(indentation.getDelimiter());
appendInsertColumns(builder, insert.getValues(), context, indentation.indent());
appendInsertValues(builder, insert.getValues(), context, indentation.indent());
}

protected void appendInsertColumns(StringBuilder builder, Map<Column, Valuable> values, BuildingContext context, Indentation indentation)
{
var counter = 0;
builder.append(indentation.getIndent()).append("(");
for (var entry : values.entrySet())
{
var column = entry.getKey();
builder.append(columnApostrophe(column.getName(), context));
if (++counter < values.size())
{
builder.append(", ");
}
}
builder.append(")").append(indentation.getDelimiter());
}

protected void appendInsertValues(StringBuilder builder, Map<Column, Valuable> values, BuildingContext context, Indentation indentation)
{
var counter = 0;
builder.append("VALUES").append(indentation.getDelimiter());
builder.append(indentation.getIndent()).append("(");
for (var entry : values.entrySet())
{
var value = entry.getValue();
if (ValuableColumn.class.isAssignableFrom(value.getClass()))
{
builder.append(columnApostrophe(((ValuableColumn) value).getName(), context));
}
else
{
builder.append(value.getValue(context, indentation));
}
if (++counter < values.size())
{
builder.append(", ");
}
}
builder.append(")");
}

@Override
protected void appendSelectStatement(StringBuilder builder, Select select, BuildingContext context, Indentation indentation)
{
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/de/jaggl/sqlbuilder/domain/ValuableColumn.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ public String getValue(BuildingContext context, Indentation indentation)
{
return column.getFullNameOrAlias(context);
}

public String getName()
{
return column.getName();
}
}
8 changes: 6 additions & 2 deletions src/test/java/de/jaggl/sqlbuilder/queries/InsertTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,12 @@ void testBuildInsert()
+ " `persons`.`numbers` = :numbers,\n" //
+ " `persons`.`lastname` = 'Schumacher'");

assertThat(insert.build(MYSQL)).isEqualTo(insert.build(SYBASE));
assertThat(insert.build(MYSQL, enabled())).isEqualTo(insert.build(SYBASE, enabled()));
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')");
assertThat(insert.build(SYBASE, enabled())).isEqualTo("INSERT INTO `persons`\n" //
+ " (`nickname`, `forename`, `birthday`, `deathday`, `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')");

assertThat(Insert.copy(insert).build(MYSQL)).isEqualTo(insert.build(MYSQL));
}
Expand Down

0 comments on commit 54f4ace

Please sign in to comment.