modularize project

This commit is contained in:
wea_ondara
2022-11-23 14:21:11 +01:00
parent 915fc4a87b
commit 481280ed88
200 changed files with 177 additions and 101 deletions

28
mysql/pom.xml Normal file
View File

@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>jef</artifactId>
<groupId>jef</groupId>
<version>0.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mysql</artifactId>
<dependencies>
<dependency>
<groupId>jef</groupId>
<artifactId>core</artifactId>
<version>0.1</version>
<scope>compile</scope>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

View File

@@ -0,0 +1,19 @@
package jef.mysql;
import jef.Database;
import jef.DatabaseOptions;
import jef.MigrationException;
import jef.mysql.migration.MysqlMigrationApplier;
import java.sql.Connection;
public class MysqlDatabase extends Database {
public MysqlDatabase(Connection connection, DatabaseOptions options) {
super(connection, options);
}
@Override
public void migrate() throws MigrationException {
new MysqlMigrationApplier(connection, options).migrate();
}
}

View File

@@ -0,0 +1,76 @@
package jef.mysql.migration;
import jef.DatabaseOptions;
import jef.model.migration.Migration;
import jef.model.migration.operation.AddFieldOperation;
import jef.model.migration.operation.AddTableOperation;
import jef.model.migration.operation.AddUniqueKeyOperation;
import jef.model.migration.operation.MigrationOperation;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class MysqlMigrationApplier extends MigrationApplier {
public MysqlMigrationApplier(Connection connection, DatabaseOptions options) {
super(connection, options);
}
@Override
protected void insertMigrationLog(Migration m) throws SQLException {
try (var stmt = connection.prepareStatement("INSERT INTO `__jef_migration_log` (`name`, `version`) VALUES (?, ?)")) {//TODO configurable log table name
stmt.setString(1, m.getClass().getSimpleName());
stmt.setString(2, "0.1"); //TODO insert actual library version
stmt.executeUpdate();
}
}
@Override
protected boolean migrationsTableExists() throws SQLException {
try (var stmt = connection.prepareStatement("SHOW TABLES");
var res = stmt.executeQuery()) {
while (res.next()) {
if (res.getString(1).equals("__jef_migration_log")) {
return true;
}
}
}
return false;
}
@Override
protected void createMigrationsTable() throws SQLException {
var table = "__jef_migration_log";
var ops = List.of(
new AddTableOperation.Builder(table, List.of(
new AddFieldOperation.Builder(table, "name").notNull(true).sqlType("VARCHAR(255)"),
new AddFieldOperation.Builder(table, "version").notNull(true).sqlType("VARCHAR(255)")
)),
new AddUniqueKeyOperation.Builder("U_" + table + "_name", table, List.of("name"))
);
for (MigrationOperation.Builder<? extends MigrationOperation> e : ops) {
try (var stmt = MysqlMigrationOperationTranslator.translate(connection, e.build())) {
stmt.executeUpdate();
}
}
}
@Override
protected List<String> getAppliedMigrations() throws SQLException {
var ret = new ArrayList<String>();
if (!migrationsTableExists()) {
return ret;
}
try (var stmt = connection.prepareStatement("SELECT `name` FROM `__jef_migration_log`");
var res = stmt.executeQuery()) {
while (res.next()) {
if (res.getString(1).equals("__jef_migration_log")) {
ret.add(res.getString("name"));
}
}
}
return ret;
}
}

View File

@@ -0,0 +1,149 @@
package jef.mysql.migration;
import jef.model.migration.operation.AddFieldOperation;
import jef.model.migration.operation.AddForeignKeyOperation;
import jef.model.migration.operation.AddIndexOperation;
import jef.model.migration.operation.AddKeyOperation;
import jef.model.migration.operation.AddPrimaryKeyOperation;
import jef.model.migration.operation.AddTableOperation;
import jef.model.migration.operation.AddUniqueKeyOperation;
import jef.model.migration.operation.DropConstraintOperation;
import jef.model.migration.operation.DropFieldOperation;
import jef.model.migration.operation.DropTableOperation;
import jef.model.migration.operation.MigrationOperation;
import jef.model.migration.operation.RenameFieldOperation;
import jef.model.migration.operation.RenameTableOperation;
import jef.model.migration.operation.UpdateFieldOperation;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
public class MysqlMigrationOperationTranslator {
private static final Map<Class<? extends MigrationOperation>, Mapper<MigrationOperation>> translators;
static {
var map = new HashMap<Class<? extends MigrationOperation>, Mapper<MigrationOperation>>();
map.put(AddFieldOperation.class, MysqlMigrationOperationTranslator::translateAddFieldOperation);
map.put(AddForeignKeyOperation.class, MysqlMigrationOperationTranslator::translateAddForeignKeyOperation);
map.put(AddIndexOperation.class, MysqlMigrationOperationTranslator::translateAddIndexOperation);
map.put(AddKeyOperation.class, MysqlMigrationOperationTranslator::translateAddKeyOperation);
map.put(AddPrimaryKeyOperation.class, MysqlMigrationOperationTranslator::translateAddPrimaryKeyOperation);
map.put(AddTableOperation.class, MysqlMigrationOperationTranslator::translateAddTableOperation);
map.put(AddUniqueKeyOperation.class, MysqlMigrationOperationTranslator::translateAddUniqueKeyOperation);
map.put(DropConstraintOperation.class, MysqlMigrationOperationTranslator::translateDropConstraintOperation);
map.put(DropFieldOperation.class, MysqlMigrationOperationTranslator::translateDropFieldOperation);
map.put(DropTableOperation.class, MysqlMigrationOperationTranslator::translateDropTableOperation);
map.put(RenameFieldOperation.class, MysqlMigrationOperationTranslator::translateRenameFieldOperation);
map.put(RenameTableOperation.class, MysqlMigrationOperationTranslator::translateRenameTableOperation);
map.put(UpdateFieldOperation.class, MysqlMigrationOperationTranslator::translateUpdateFieldOperation);
translators = Collections.unmodifiableMap(map);
}
public static PreparedStatement translate(Connection connection, MigrationOperation operation) throws SQLException {
return Optional.ofNullable(translators.get(operation.getClass())).orElseThrow().apply(connection, operation);
}
private static PreparedStatement translateAddFieldOperation(Connection connection, MigrationOperation operation) throws SQLException {
AddFieldOperation op = (AddFieldOperation) operation;
return connection.prepareStatement("ALTER TABLE `" + op.getTable() + "`"
+ " ADD COLUMN `" + op.getField() + "` "
+ op.getSqlType() + (op.isNotNull() ? " NOT NULL" : "") //TODO add after field specification
+ " LAST");
}
private static PreparedStatement translateAddForeignKeyOperation(Connection connection, MigrationOperation operation) throws SQLException {
AddForeignKeyOperation op = (AddForeignKeyOperation) operation;
return connection.prepareStatement("ALTER TABLE `" + op.getTable() + "`"
+ " ADD CONSTRAINT `" + op.getName() + "`"
+ " FOREIGN KEY (" + op.getFields().stream().map(e -> "`" + e + "`").collect(Collectors.joining(", ")) + ")"
+ " REFERENCES `" + op.getReferencedTable() + "`(" + op.getReferencedFields().stream()
.map(e -> "`" + e + "`")
.collect(Collectors.joining(", ")) + ")");
}
private static PreparedStatement translateAddIndexOperation(Connection connection, MigrationOperation operation) throws SQLException {
AddIndexOperation op = (AddIndexOperation) operation;
return connection.prepareStatement("ALTER TABLE `" + op.getTable() + "`"
+ " ADD CONSTRAINT `" + op.getName() + "`"
+ " INDEX (" + op.getFields().stream().map(e -> "`" + e + "`").collect(Collectors.joining(", ")) + ")");
}
private static PreparedStatement translateAddKeyOperation(Connection connection, MigrationOperation operation) throws SQLException {
AddKeyOperation op = (AddKeyOperation) operation;
return connection.prepareStatement("ALTER TABLE `" + op.getTable() + "`"
+ " ADD CONSTRAINT `" + op.getName() + "`"
+ " KEY (" + op.getFields().stream().map(e -> "`" + e + "`").collect(Collectors.joining(", ")) + ")");
}
private static PreparedStatement translateAddPrimaryKeyOperation(Connection connection, MigrationOperation operation) throws SQLException {
AddPrimaryKeyOperation op = (AddPrimaryKeyOperation) operation;
return connection.prepareStatement("ALTER TABLE `" + op.getTable() + "`"
+ " ADD " + (!op.getName().equals("PRIMARY") ? "CONSTRAINT `" + op.getName() + "`" : "")
+ " PRIMARY KEY (" + op.getFields().stream().map(e -> "`" + e + "`").collect(Collectors.joining(", ")) + ")");
// + " ADD CONSTRAINT `" + op.getName() + "`"
// + " PRIMARY KEY (" + op.getFields().stream().map(e -> "`" + e + "`").collect(Collectors.joining(", ")) + ")");
}
private static PreparedStatement translateAddTableOperation(Connection connection, MigrationOperation operation) throws SQLException {
AddTableOperation op = (AddTableOperation) operation;
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" : "");
}).collect(Collectors.joining(", "))
+ ")"); //TODO default collocation from database config or operation, field collation, primary key, constraints
}
private static PreparedStatement translateAddUniqueKeyOperation(Connection connection, MigrationOperation operation) throws SQLException {
AddUniqueKeyOperation op = (AddUniqueKeyOperation) operation;
return connection.prepareStatement("ALTER TABLE `" + op.getTable() + "`"
+ " ADD CONSTRAINT `" + op.getName() + "`"
+ " UNIQUE (" + op.getFields().stream().map(e -> "`" + e + "`").collect(Collectors.joining(", ")) + ")");
}
private static PreparedStatement translateDropConstraintOperation(Connection connection, MigrationOperation operation) throws SQLException {
DropConstraintOperation op = (DropConstraintOperation) operation;
return connection.prepareStatement("ALTER TABLE `" + op.getTable() + "`"
+ " DROP CONSTRAINT `" + op.getName() + "`");
}
private static PreparedStatement translateDropFieldOperation(Connection connection, MigrationOperation operation) throws SQLException {
DropFieldOperation op = (DropFieldOperation) operation;
return connection.prepareStatement("ALTER TABLE `" + op.getTable() + "`"
+ " DROP COLUMN `" + op.getField() + "`");
}
private static PreparedStatement translateDropTableOperation(Connection connection, MigrationOperation operation) throws SQLException {
DropTableOperation op = (DropTableOperation) operation;
return connection.prepareStatement("DROP TABLE `" + op.getTable() + "`");
}
private static PreparedStatement translateRenameFieldOperation(Connection connection, MigrationOperation operation) throws SQLException {
RenameFieldOperation op = (RenameFieldOperation) operation;
return connection.prepareStatement("ALTER TABLE `" + op.getTable() + "`"
+ " RENAME COLUMN `" + op.getOldName() + "` `" + op.getNewName() + "`");
}
private static PreparedStatement translateRenameTableOperation(Connection connection, MigrationOperation operation) throws SQLException {
RenameTableOperation op = (RenameTableOperation) operation;
return connection.prepareStatement("RENAME TABLE `" + op.getOldName() + "` `" + op.getNewName() + "`");
}
private static PreparedStatement translateUpdateFieldOperation(Connection connection, MigrationOperation operation) throws SQLException {
UpdateFieldOperation op = (UpdateFieldOperation) operation;
return connection.prepareStatement("ALTER TABLE `" + op.getTable() + "`"
+ " ALTER COLUMN `" + op.getField() + "` `" + op.getNewName() + "`"
+ op.getSqlType() + (op.isNotNull() ? " NOT NULL" : ""));
}
@FunctionalInterface
private interface Mapper<T extends MigrationOperation> {
PreparedStatement apply(Connection connection, T operation) throws SQLException;
}
}