Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,10 @@ protected void toStringGeneral(StringBuilder b) {
b.append("IF EXISTS ");
}
b.append(constraintName);
} else if (index != null) {
// The structured index is canonical. Legacy PK/UK/FK fields may also be populated for
// source compatibility, but cannot represent names, expressions, or index options.
b.append(index);
} else if (pkColumns != null) {
b.append("PRIMARY KEY (").append(PlainSelect.getStringList(pkColumns)).append(')');
} else if (ukColumns != null) {
Expand Down Expand Up @@ -1195,8 +1199,6 @@ protected void toStringGeneral(StringBuilder b) {
.append(PlainSelect.getStringList(fkSourceColumns))
.append(")");
referentialActions.forEach(b::append);
} else if (index != null) {
b.append(index);
}

if (getConstraints() != null && !getConstraints().isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public class CheckConstraint extends NamedConstraint {

private Boolean enforced;

public CheckConstraint() {
setKind(Kind.CHECK);
}

public Table getTable() {
return table;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ public enum Signedness {
SIGNED, UNSIGNED
}

public enum TypeModifier {
SIGNED, UNSIGNED, ZEROFILL
}

public enum NationalCharacterType {
CHAR, VARCHAR
}

private String dataType;
private List<String> argumentsStringList;
private String characterSet;
Expand All @@ -37,6 +45,8 @@ public enum Signedness {
private boolean zerofill;
private Integer precision;
private Integer scale;
private List<TypeModifier> typeModifiers;
private NationalCharacterType nationalCharacterType;

public ColDataType() {
// empty constructor
Expand Down Expand Up @@ -120,6 +130,58 @@ public void setZerofill(boolean zerofill) {
this.zerofill = zerofill;
}

/** Returns MySQL numeric modifiers in their original order, including repetitions. */
public List<TypeModifier> getTypeModifiers() {
return typeModifiers;
}

public void setTypeModifiers(List<TypeModifier> typeModifiers) {
this.typeModifiers = typeModifiers;
signedness = null;
zerofill = false;
if (typeModifiers != null) {
for (TypeModifier modifier : typeModifiers) {
updateEffectiveModifier(modifier);
}
}
}

public void addTypeModifier(TypeModifier typeModifier) {
if (typeModifiers == null) {
typeModifiers = new ArrayList<>();
}
typeModifiers.add(typeModifier);
updateEffectiveModifier(typeModifier);
}

private void updateEffectiveModifier(TypeModifier modifier) {
switch (modifier) {
case SIGNED:
signedness = Signedness.SIGNED;
break;
case UNSIGNED:
signedness = Signedness.UNSIGNED;
break;
case ZEROFILL:
zerofill = true;
break;
default:
break;
}
}

public NationalCharacterType getNationalCharacterType() {
return nationalCharacterType;
}

public void setNationalCharacterType(NationalCharacterType nationalCharacterType) {
this.nationalCharacterType = nationalCharacterType;
}

public boolean isNational() {
return nationalCharacterType != null;
}

/**
* The first numeric type parameter, e.g. {@code 255} for {@code VARCHAR(255)} or {@code 10} for
* {@code DECIMAL(10, 2)}. {@code MAX} is reported as {@link Integer#MAX_VALUE}. Returns
Expand Down Expand Up @@ -161,8 +223,10 @@ public String toString() {
+ (argumentsStringList != null
? " " + PlainSelect.getStringList(argumentsStringList, true, true)
: "")
+ (signedness != null ? " " + signedness : "")
+ (zerofill ? " ZEROFILL" : "")
+ (typeModifiers != null && !typeModifiers.isEmpty()
? " " + PlainSelect.getStringList(typeModifiers, false, false)
: (signedness != null ? " " + signedness : "")
+ (zerofill ? " ZEROFILL" : ""))
+ arraySpec.toString()
+ (characterSet != null ? " CHARACTER SET " + characterSet : "");
}
Expand Down Expand Up @@ -202,6 +266,16 @@ public ColDataType withZerofill(boolean zerofill) {
return this;
}

public ColDataType withTypeModifiers(List<TypeModifier> typeModifiers) {
setTypeModifiers(typeModifiers);
return this;
}

public ColDataType withNationalCharacterType(NationalCharacterType nationalCharacterType) {
setNationalCharacterType(nationalCharacterType);
return this;
}

public ColDataType withPrecision(Integer precision) {
this.setPrecision(precision);
return this;
Expand Down Expand Up @@ -254,7 +328,9 @@ public final boolean equals(Object o) {
&& Objects.equals(intervalQualifier, that.intervalQualifier)
&& Objects.equals(arrayData, that.arrayData)
&& signedness == that.signedness
&& zerofill == that.zerofill;
&& zerofill == that.zerofill
&& Objects.equals(typeModifiers, that.typeModifiers)
&& nationalCharacterType == that.nationalCharacterType;
}

@Override
Expand All @@ -266,6 +342,8 @@ public int hashCode() {
result = 31 * result + Objects.hashCode(arrayData);
result = 31 * result + Objects.hashCode(signedness);
result = 31 * result + Boolean.hashCode(zerofill);
result = 31 * result + Objects.hashCode(typeModifiers);
result = 31 * result + Objects.hashCode(nationalCharacterType);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@
/**
* Globally used definition class for columns.
*/
public class ColumnDefinition implements ImportColumn, Serializable {
public class ColumnDefinition implements ImportColumn, TableElement, Serializable {

private String columnName;
private ColDataType colDataType;
private List<String> columnSpecs;
private List<ColumnOption> columnOptions;

public ColumnDefinition() {}

Expand All @@ -46,6 +47,47 @@ public List<String> getColumnSpecs() {

public void setColumnSpecs(List<String> list) {
columnSpecs = list;
columnOptions = null;
}

/**
* Returns column options in source order, including structured references and MySQL
* {@code SERIAL DEFAULT VALUE}.
*/
public List<ColumnOption> getColumnOptions() {
return columnOptions;
}

public void setColumnOptions(List<ColumnOption> columnOptions) {
this.columnOptions = columnOptions;
}

public boolean isSerialDefaultValue() {
return columnOptions != null && columnOptions.stream()
.anyMatch(option -> option.getKind() == ColumnOption.Kind.SERIAL_DEFAULT_VALUE);
}

public ForeignKeyReference getForeignKeyReference() {
if (columnOptions == null) {
return null;
}
return columnOptions.stream()
.filter(option -> option.getKind() == ColumnOption.Kind.REFERENCE)
.map(ColumnOption::getForeignKeyReference)
.findFirst()
.orElse(null);
}

public ColumnDefinition withColumnOptions(List<ColumnOption> columnOptions) {
setColumnOptions(columnOptions);
return this;
}

public ColumnDefinition addColumnOptions(ColumnOption... columnOptions) {
List<ColumnOption> collection =
Optional.ofNullable(getColumnOptions()).orElseGet(ArrayList::new);
Collections.addAll(collection, columnOptions);
return withColumnOptions(collection);
}

public ColDataType getColDataType() {
Expand All @@ -71,9 +113,11 @@ public String toString() {

public String toStringDataTypeAndSpec() {
return (colDataType == null ? "" : colDataType)
+ (columnSpecs != null && !columnSpecs.isEmpty()
? " " + PlainSelect.getStringList(columnSpecs, false, false)
: "");
+ (columnOptions != null && !columnOptions.isEmpty()
? " " + PlainSelect.getStringList(columnOptions, false, false)
: columnSpecs != null && !columnSpecs.isEmpty()
? " " + PlainSelect.getStringList(columnSpecs, false, false)
: "");
}

public ColumnDefinition withColumnName(String columnName) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2026 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.statement.create.table;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import net.sf.jsqlparser.statement.select.PlainSelect;

/** A structured option following a column data type. */
public class ColumnOption implements Serializable {

public enum Kind {
SERIAL_DEFAULT_VALUE, REFERENCE, OTHER
}

private Kind kind = Kind.OTHER;
private List<String> tokens;
private ForeignKeyReference foreignKeyReference;

public static ColumnOption raw(List<String> tokens) {
ColumnOption option = new ColumnOption();
option.tokens = tokens;
return option;
}

public static ColumnOption raw(String... tokens) {
return raw(Arrays.asList(tokens));
}

public static ColumnOption serialDefaultValue() {
ColumnOption option = raw("SERIAL", "DEFAULT", "VALUE");
option.kind = Kind.SERIAL_DEFAULT_VALUE;
return option;
}

public static ColumnOption reference(ForeignKeyReference reference) {
ColumnOption option = new ColumnOption();
option.kind = Kind.REFERENCE;
option.foreignKeyReference = reference;
return option;
}

public Kind getKind() {
return kind;
}

public List<String> getTokens() {
return kind == Kind.REFERENCE ? Collections.singletonList(foreignKeyReference.toString())
: tokens;
}

public ForeignKeyReference getForeignKeyReference() {
return foreignKeyReference;
}

@Override
public String toString() {
return kind == Kind.REFERENCE ? foreignKeyReference.toString()
: PlainSelect.getStringList(tokens, false, false);
}
}
Loading
Loading