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.DdlBuffer;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite;
|
||||
import io.ebeaninternal.dbmigration.migration.AlterColumn;
|
||||
import io.ebeaninternal.dbmigration.migration.Column;
|
||||
|
||||
/**
|
||||
@@ -134,10 +135,31 @@ public class DB2Ddl extends PlatformDdl {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected DdlAlterTable alterTable(DdlWrite writer, String tableName) {
|
||||
return writer.applyAlterTable(tableName, Db2AlterTableWrite::new);
|
||||
};
|
||||
@Override
|
||||
protected void alterColumnType(DdlWrite writer, AlterColumn alter) {
|
||||
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 {
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user