Skip to content

Commit

Permalink
fixed createTable (create a key for autoIncrementColumn)
Browse files Browse the repository at this point in the history
  • Loading branch information
Schubi1981 committed Jan 12, 2020
1 parent 7e8b822 commit 2a93089
Show file tree
Hide file tree
Showing 15 changed files with 73 additions and 3 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.6.1</version>
<version>2.6.2</version>

<packaging>jar</packaging>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
import de.jaggl.sqlbuilder.columns.Column;
import de.jaggl.sqlbuilder.domain.Placeholder;

/**
* @author Martin Schumacher
*
* @since 2.1.0
*/
public interface DateTimeConditions extends LikeConditions
{
Function<ZonedDateTime, Temporal> getDateConversion();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
import de.jaggl.sqlbuilder.columns.Column;
import de.jaggl.sqlbuilder.domain.Placeholder;

/**
* @author Martin Schumacher
*
* @since 2.1.0
*/
public interface EqualityConditions
{
default Condition isEqualTo(Column otherColumn)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
import de.jaggl.sqlbuilder.domain.LikeType;
import de.jaggl.sqlbuilder.domain.Placeholder;

/**
* @author Martin Schumacher
*
* @since 2.1.0
*/
public interface LikeConditions
{
default Condition isLike(CharSequence value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
import static de.jaggl.sqlbuilder.conditions.GenericCondition.GenericConditionType.IS_NOT_NULL;
import static de.jaggl.sqlbuilder.conditions.GenericCondition.GenericConditionType.IS_NULL;

/**
* @author Martin Schumacher
*
* @since 2.1.0
*/
public interface NullableConditions
{
default Condition isNull()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
import de.jaggl.sqlbuilder.columns.Column;
import de.jaggl.sqlbuilder.domain.Placeholder;

/**
* @author Martin Schumacher
*
* @since 2.1.0
*/
public interface NumberConditions
{
default Condition isEqualTo(Number value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
import de.jaggl.sqlbuilder.domain.Placeholder;
import de.jaggl.sqlbuilder.utils.ArrayUtils;

/**
* @author Martin Schumacher
*
* @since 2.1.0
*/
public interface StringConditions extends LikeConditions
{
default Condition isEqualTo(String value)
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/de/jaggl/sqlbuilder/dialect/DefaultDialect.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@ public String build(CreateTable createTable, Indentation indentation)
builder.append(buildColumnDefinition(column.getColumnDefinition(), context, indentation));
isFirst = false;
}
createTable.getTable()
.getColumns()
.stream()
.filter(col -> col.getColumnDefinition().isAutoIncrement())
.findFirst()
.ifPresent(column -> builder.append(",")
.append(indentation.getDelimiter())
.append(indentation.indent().getIndent())
.append("PRIMARY KEY (" + BuilderUtils.columnApostrophe(column.getName(), context) + ")"));
if (indentation.isEnabled())
{
builder.append(indentation.getDelimiter());
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/de/jaggl/sqlbuilder/dialect/SybaseDialect.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
import de.jaggl.sqlbuilder.queries.Select;
import de.jaggl.sqlbuilder.utils.Indentation;

/**
* @author Martin Schumacher
*
* @since 2.0.0
*/
public class SybaseDialect extends DefaultDialect
{
private static final SybaseDialect instance;
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/de/jaggl/sqlbuilder/domain/Placeholder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

import de.jaggl.sqlbuilder.columns.Column;

/**
* @author Martin Schumacher
*
* @since 2.5.0
*/
public class Placeholder extends PlainValuable
{
private Placeholder(String value)
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/de/jaggl/sqlbuilder/domain/Plain.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
import lombok.AllArgsConstructor;
import lombok.Getter;

/**
* @author Martin Schumacher
*
* @since 2.6.0
*/
@AllArgsConstructor
@Getter
public class Plain
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/de/jaggl/sqlbuilder/domain/SqlTypeSupplier.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package de.jaggl.sqlbuilder.domain;

/**
* @author Martin Schumacher
*
* @since 2.4.0
*/
public interface SqlTypeSupplier
{
int getSqlType();
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/de/jaggl/sqlbuilder/queries/CreateTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
import de.jaggl.sqlbuilder.utils.Indentation;
import lombok.Getter;

/**
* @author Martin Schumacher
*
* @since 2.4.0
*/
@Getter
public class CreateTable implements ExecutableQuery
{
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/de/jaggl/sqlbuilder/queries/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
import de.jaggl.sqlbuilder.dialect.Dialect;
import de.jaggl.sqlbuilder.utils.Indentation;

/**
* @author Martin Schumacher
*
* @since 2.3.0
*/
public interface Query
{
String build(Dialect dialect, Indentation indentation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void testCreateTable()
createTable.println(SYBASE, enabled());

assertThat(createTable.build(MYSQL))
.isEqualTo("CREATE TABLE `dba`.`persons` (`forename` VARCHAR(50), `lastname` VARCHAR(50) DEFAULT NULL, `nickname` VARCHAR(30) DEFAULT 'Schubi', `age` INT(5) UNSIGNED NOT NULL AUTO_INCREMENT, `size` DOUBLE(2,2) UNSIGNED NOT NULL DEFAULT 55.8, `birthday` DATE NOT NULL, `happening` DATETIME NOT NULL)");
.isEqualTo("CREATE TABLE `dba`.`persons` (`forename` VARCHAR(50), `lastname` VARCHAR(50) DEFAULT NULL, `nickname` VARCHAR(30) DEFAULT 'Schubi', `age` INT(5) UNSIGNED NOT NULL AUTO_INCREMENT, `size` DOUBLE(2,2) UNSIGNED NOT NULL DEFAULT 55.8, `birthday` DATE NOT NULL, `happening` DATETIME NOT NULL, PRIMARY KEY (`age`))");

assertThat(createTable.build(MYSQL, enabled()))
.isEqualTo("CREATE TABLE `dba`.`persons`\n" //
Expand All @@ -48,7 +48,8 @@ void testCreateTable()
+ " `age` INT(5) UNSIGNED NOT NULL AUTO_INCREMENT,\n" //
+ " `size` DOUBLE(2,2) UNSIGNED NOT NULL DEFAULT 55.8,\n" //
+ " `birthday` DATE NOT NULL,\n" //
+ " `happening` DATETIME NOT NULL\n" //
+ " `happening` DATETIME NOT NULL,\n" //
+ " PRIMARY KEY (`age`)\n" //
+ ")");

assertThat(createTable.build(MYSQL)).isEqualTo(createTable.build(SYBASE));
Expand Down

0 comments on commit 2a93089

Please sign in to comment.