added database value generation with identity generation

This commit is contained in:
wea_ondara
2022-11-27 08:10:52 +01:00
parent 3e581a9e4a
commit 195f3d0148
14 changed files with 118 additions and 15 deletions

View File

@@ -1,5 +1,6 @@
package jef.platform.mysql.migration;
import jef.model.annotations.Generated;
import jef.model.migration.operation.AddFieldOperation;
import jef.model.migration.operation.AddForeignKeyOperation;
import jef.model.migration.operation.AddIndexOperation;
@@ -59,6 +60,8 @@ public class MysqlMigrationOperationTranslator implements MigrationOperationTran
return connection.prepareStatement("ALTER TABLE `" + op.getTable() + "`"
+ " ADD COLUMN `" + op.getField() + "` "
+ op.getSqlType() + (op.isNotNull() ? " NOT NULL" : "") //TODO add after field specification
+ (op.getGenerated() == Generated.Type.IDENTITY ? "AUTO_INCREMENT " : "")
// + (op.getGenerated() == Generated.Type.COMPUTED ? "AUTO_INCREMENT " : "")//TODO
+ " LAST");
}
@@ -100,7 +103,10 @@ public class MysqlMigrationOperationTranslator implements MigrationOperationTran
return connection.prepareStatement("CREATE TABLE `" + op.getTable() + "` ("
+ op.getFields().stream().map(e -> {
var f = e.build();
return "`" + f.getField() + "` " + f.getSqlType() + (f.isNotNull() ? " NOT NULL" : "");
return "`" + f.getField() + "` " + f.getSqlType()
+ (f.isNotNull() ? " NOT NULL" : "")
+ (f.getGenerated() == Generated.Type.IDENTITY ? " AUTO_INCREMENT" : "");
// + (op.getGenerated() == Generated.Type.COMPUTED ? "AUTO_INCREMENT " : "")//TODO
}).collect(Collectors.joining(", "))
+ (op.getPrimaryKey() != null
? ", PRIMARY KEY (" + op.getPrimaryKey().build().getFields().stream().map(e -> "`" + e + "`").collect(Collectors.joining(", ")) + ")"