mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-19 23:02:24 +00:00
Support for extra colum options for DB2
This commit is contained in:
1 parent
eb1ed0bdd0
commit
8f746196ab
6 files changed
+176
-4
No files matched your search
+77
@@ -0,0 +1,77 @@
|
|||||||
|
package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||||
|
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class parses the inline column options of a create-statement (see
|
||||||
|
* https://www.ibm.com/docs/en/db2/11.5?topic=statements-create-table#sdx-synid_frag-column-options) into separate pieces if you
|
||||||
|
* want to alter a column. The later one has to be done in separate statements: See
|
||||||
|
* https://www.ibm.com/docs/en/db2/11.5?topic=statements-alter-table#sdx-synid_frag-column-options
|
||||||
|
*
|
||||||
|
* <br>
|
||||||
|
* Note 1: This class parses only 'inline length', 'compact' and 'logged' options<br>
|
||||||
|
* Note 2: You cannot alter the compact or logged option on an existing column.
|
||||||
|
*
|
||||||
|
* @author Roland Praml, FOCONIS AG
|
||||||
|
*/
|
||||||
|
public class DB2ColumnOptionsParser {
|
||||||
|
private static final Pattern INLINE_LENGTH = Pattern.compile(".*( inline length \\d+).*", Pattern.CASE_INSENSITIVE);
|
||||||
|
private static final Pattern NOT_LOGGED = Pattern.compile(".*?(( not)? logged).*", Pattern.CASE_INSENSITIVE);
|
||||||
|
private static final Pattern NOT_COMPACT = Pattern.compile(".*?(( not)? compact).*", Pattern.CASE_INSENSITIVE);
|
||||||
|
|
||||||
|
private final String type;
|
||||||
|
private final String inlineLength;
|
||||||
|
private final boolean logged;
|
||||||
|
private final boolean compact;
|
||||||
|
private boolean extraOptions;
|
||||||
|
|
||||||
|
DB2ColumnOptionsParser(String def) {
|
||||||
|
Matcher m = INLINE_LENGTH.matcher(def);
|
||||||
|
if (m.matches()) {
|
||||||
|
def = def.replace(m.group(1), "");
|
||||||
|
inlineLength = m.group(1).trim();
|
||||||
|
} else {
|
||||||
|
inlineLength = null;
|
||||||
|
}
|
||||||
|
m = NOT_LOGGED.matcher(def);
|
||||||
|
if (m.matches()) {
|
||||||
|
extraOptions = true;
|
||||||
|
def = def.replace(m.group(1), "");
|
||||||
|
logged = m.group(2) == null;
|
||||||
|
} else {
|
||||||
|
logged = true; // default
|
||||||
|
}
|
||||||
|
|
||||||
|
m = NOT_COMPACT.matcher(def);
|
||||||
|
if (m.matches()) {
|
||||||
|
extraOptions = true;
|
||||||
|
def = def.replace(m.group(1), "");
|
||||||
|
compact = m.group(2) == null;
|
||||||
|
} else {
|
||||||
|
compact = false; // default
|
||||||
|
}
|
||||||
|
type = def.trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getInlineLength() {
|
||||||
|
return inlineLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isLogged() {
|
||||||
|
return logged;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isCompact() {
|
||||||
|
return compact;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasExtraOptions() {
|
||||||
|
return extraOptions;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+26
-4
@@ -8,6 +8,7 @@ import io.ebean.util.StringHelper;
|
|||||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlAlterTable;
|
import io.ebeaninternal.dbmigration.ddlgeneration.DdlAlterTable;
|
||||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlBuffer;
|
import io.ebeaninternal.dbmigration.ddlgeneration.DdlBuffer;
|
||||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite;
|
import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite;
|
||||||
|
import io.ebeaninternal.dbmigration.migration.AlterColumn;
|
||||||
import io.ebeaninternal.dbmigration.migration.Column;
|
import io.ebeaninternal.dbmigration.migration.Column;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -134,10 +135,31 @@ public class DB2Ddl extends PlatformDdl {
|
|||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected DdlAlterTable alterTable(DdlWrite writer, String tableName) {
|
protected void alterColumnType(DdlWrite writer, AlterColumn alter) {
|
||||||
return writer.applyAlterTable(tableName, Db2AlterTableWrite::new);
|
String type = convert(alter.getType());
|
||||||
};
|
DB2ColumnOptionsParser parser = new DB2ColumnOptionsParser(type);
|
||||||
|
alterTable(writer, alter.getTableName()).append(alterColumn, alter.getColumnName())
|
||||||
|
.append(columnSetType).append(parser.getType());
|
||||||
|
|
||||||
|
if (parser.getInlineLength() != null) {
|
||||||
|
alterTable(writer, alter.getTableName()).append(alterColumn, alter.getColumnName())
|
||||||
|
.append("set").appendWithSpace(parser.getInlineLength());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (parser.hasExtraOptions()) {
|
||||||
|
alterTable(writer, alter.getTableName()).raw("-- ignored options for ")
|
||||||
|
.append(alter.getTableName()).append(".").append(alter.getColumnName())
|
||||||
|
.append(": compact=").append(String.valueOf(parser.isCompact()))
|
||||||
|
.append(", logged=").append(String.valueOf(parser.isLogged()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected DdlAlterTable alterTable(DdlWrite writer, String tableName) {
|
||||||
|
return writer.applyAlterTable(tableName, Db2AlterTableWrite::new);
|
||||||
|
};
|
||||||
|
|
||||||
static class Db2AlterTableWrite extends BaseAlterTableWrite {
|
static class Db2AlterTableWrite extends BaseAlterTableWrite {
|
||||||
|
|
||||||
public Db2AlterTableWrite(String tableName) {
|
public Db2AlterTableWrite(String tableName) {
|
||||||
|
|||||||
+43
@@ -0,0 +1,43 @@
|
|||||||
|
package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||||
|
|
||||||
|
import org.assertj.core.api.SoftAssertions;
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
|
||||||
|
public class DB2ColumnOptionsParserTest {
|
||||||
|
|
||||||
|
private final SoftAssertions softly = new SoftAssertions();
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
void assertAll() {
|
||||||
|
softly.assertAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testParser() {
|
||||||
|
DB2ColumnOptionsParser p = new DB2ColumnOptionsParser("blob(64M) inline length 200 logged compact");
|
||||||
|
softly.assertThat(p.getType()).isEqualTo("blob(64M)");
|
||||||
|
softly.assertThat(p.getInlineLength()).isEqualTo("inline length 200");
|
||||||
|
softly.assertThat(p.isLogged()).isTrue();
|
||||||
|
softly.assertThat(p.isCompact()).isTrue();
|
||||||
|
|
||||||
|
p = new DB2ColumnOptionsParser("blob(64M) inline length 200 not logged not compact");
|
||||||
|
softly.assertThat(p.getType()).isEqualTo("blob(64M)");
|
||||||
|
softly.assertThat(p.getInlineLength()).isEqualTo("inline length 200");
|
||||||
|
softly.assertThat(p.isLogged()).isFalse();
|
||||||
|
softly.assertThat(p.isCompact()).isFalse();
|
||||||
|
|
||||||
|
p = new DB2ColumnOptionsParser("blob(64M) inline length 200");
|
||||||
|
softly.assertThat(p.getType()).isEqualTo("blob(64M)");
|
||||||
|
softly.assertThat(p.getInlineLength()).isEqualTo("inline length 200");
|
||||||
|
softly.assertThat(p.isLogged()).isTrue();
|
||||||
|
softly.assertThat(p.isCompact()).isFalse();
|
||||||
|
|
||||||
|
p = new DB2ColumnOptionsParser("blob(64M)");
|
||||||
|
softly.assertThat(p.getType()).isEqualTo("blob(64M)");
|
||||||
|
softly.assertThat(p.getInlineLength()).isNull();
|
||||||
|
softly.assertThat(p.isLogged()).isTrue();
|
||||||
|
softly.assertThat(p.isCompact()).isFalse();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package misc.migration.v1_0;
|
package misc.migration.v1_0;
|
||||||
|
|
||||||
import io.ebean.annotation.DbDefault;
|
import io.ebean.annotation.DbDefault;
|
||||||
|
import io.ebean.annotation.DbJson;
|
||||||
import io.ebean.annotation.EnumValue;
|
import io.ebean.annotation.EnumValue;
|
||||||
import io.ebean.annotation.Index;
|
import io.ebean.annotation.Index;
|
||||||
import io.ebean.annotation.NotNull;
|
import io.ebean.annotation.NotNull;
|
||||||
@@ -15,6 +16,7 @@ import javax.validation.constraints.Size;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.sql.Timestamp;
|
import java.sql.Timestamp;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name = "migtest_e_basic")
|
@Table(name = "migtest_e_basic")
|
||||||
@@ -50,6 +52,15 @@ public class EBasic {
|
|||||||
@Column(columnDefinition = "db2;blob(64M);")
|
@Column(columnDefinition = "db2;blob(64M);")
|
||||||
File descriptionFile;
|
File descriptionFile;
|
||||||
|
|
||||||
|
@DbJson
|
||||||
|
@Column(columnDefinition = "db2;clob(16K) inline length 500 compact;")
|
||||||
|
List<String> jsonList;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@DbDefault("X")
|
||||||
|
@Column(columnDefinition = "db2;clob(16K) inline length 500 not logged;")
|
||||||
|
String aLob;
|
||||||
|
|
||||||
Timestamp someDate;
|
Timestamp someDate;
|
||||||
|
|
||||||
boolean old_boolean;
|
boolean old_boolean;
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import io.ebean.annotation.*;
|
|||||||
import javax.persistence.*;
|
import javax.persistence.*;
|
||||||
import javax.validation.constraints.Size;
|
import javax.validation.constraints.Size;
|
||||||
import java.sql.Timestamp;
|
import java.sql.Timestamp;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name = "migtest_e_basic")
|
@Table(name = "migtest_e_basic")
|
||||||
@@ -58,6 +59,13 @@ public class EBasic {
|
|||||||
@Size(max=127)
|
@Size(max=127)
|
||||||
String description;
|
String description;
|
||||||
|
|
||||||
|
@DbJson
|
||||||
|
@Column(columnDefinition = "db2;clob(16K) inline length 500 compact;")
|
||||||
|
List<String> jsonList;
|
||||||
|
|
||||||
|
@Column(columnDefinition = "db2;clob(16K) inline length 500 not logged;", nullable = true)
|
||||||
|
String aLob;
|
||||||
|
|
||||||
//@NotNull
|
//@NotNull
|
||||||
//@DbDefault("2000-01-01T00:00:00") //- date time literals do not work for each platform yet
|
//@DbDefault("2000-01-01T00:00:00") //- date time literals do not work for each platform yet
|
||||||
//@DbDefault("now") //- now does not work for mariaDb
|
//@DbDefault("now") //- now does not work for mariaDb
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package misc.migration.v1_2;
|
package misc.migration.v1_2;
|
||||||
|
|
||||||
import io.ebean.annotation.DbDefault;
|
import io.ebean.annotation.DbDefault;
|
||||||
|
import io.ebean.annotation.DbJson;
|
||||||
import io.ebean.annotation.EnumValue;
|
import io.ebean.annotation.EnumValue;
|
||||||
import io.ebean.annotation.Index;
|
import io.ebean.annotation.Index;
|
||||||
import io.ebean.annotation.NotNull;
|
import io.ebean.annotation.NotNull;
|
||||||
@@ -15,6 +16,7 @@ import javax.validation.constraints.Size;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.sql.Timestamp;
|
import java.sql.Timestamp;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name = "migtest_e_basic")
|
@Table(name = "migtest_e_basic")
|
||||||
@@ -50,6 +52,15 @@ public class EBasic {
|
|||||||
@Column(columnDefinition = "db2;blob(64M);")
|
@Column(columnDefinition = "db2;blob(64M);")
|
||||||
File descriptionFile;
|
File descriptionFile;
|
||||||
|
|
||||||
|
@DbJson
|
||||||
|
@Column(columnDefinition = "db2;clob(16K) inline length 500 compact;")
|
||||||
|
List<String> jsonList;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@DbDefault("X")
|
||||||
|
@Column(columnDefinition = "db2;clob(16K) inline length 500 compact;")
|
||||||
|
String aLob;
|
||||||
|
|
||||||
Timestamp someDate;
|
Timestamp someDate;
|
||||||
|
|
||||||
boolean old_boolean;
|
boolean old_boolean;
|
||||||
|
|||||||
Reference in new issue
Block a user