|
| 1 | +package com.easy.query.core.migration; |
| 2 | + |
| 3 | +import com.easy.query.core.context.QueryRuntimeContext; |
| 4 | +import com.easy.query.core.exception.EasyQueryInvalidOperationException; |
| 5 | +import com.easy.query.core.inject.ServiceProvider; |
| 6 | +import com.easy.query.core.metadata.EntityMetadata; |
| 7 | +import com.easy.query.core.metadata.EntityMetadataManager; |
| 8 | +import com.easy.query.core.migration.data.TableMigrationData; |
| 9 | +import com.easy.query.core.util.EasyStringUtil; |
| 10 | + |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Objects; |
| 14 | + |
| 15 | +/** |
| 16 | + * create time 2025/1/11 14:29 |
| 17 | + * 文件说明 |
| 18 | + * |
| 19 | + * @author xuejiaming |
| 20 | + */ |
| 21 | +public class DefaultMigrationsSQLGenerator2 implements MigrationsSQLGenerator { |
| 22 | + private final EntityMetadataManager entityMetadataManager; |
| 23 | + private final DatabaseMigrationProvider databaseMigrationProvider; |
| 24 | + private final ServiceProvider serviceProvider; |
| 25 | + |
| 26 | + public DefaultMigrationsSQLGenerator2(EntityMetadataManager entityMetadataManager, DatabaseMigrationProvider databaseMigrationProvider, ServiceProvider serviceProvider) { |
| 27 | + this.entityMetadataManager = entityMetadataManager; |
| 28 | + this.databaseMigrationProvider = databaseMigrationProvider; |
| 29 | + this.serviceProvider = serviceProvider; |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public List<MigrationCommand> generateMigrationSQL(MigrationContext migrationContext) { |
| 34 | + ArrayList<MigrationCommand> migrationCommands = new ArrayList<>(); |
| 35 | +// if (!databaseMigrationProvider.databaseExists()) { |
| 36 | +// MigrationCommand databaseCommand = databaseMigrationProvider.createDatabaseCommand(); |
| 37 | +// if (databaseCommand != null) { |
| 38 | +// migrationCommands.add(databaseCommand); |
| 39 | +// } |
| 40 | +// } |
| 41 | + QueryRuntimeContext runtimeContext = serviceProvider.getService(QueryRuntimeContext.class); |
| 42 | + for (Class<?> entity : migrationContext.getEntities()) { |
| 43 | + TableMigrationData tableMigrationData=new TableMigrationData(); |
| 44 | + EntityMetadata entityMetadata = entityMetadataManager.getEntityMetadata(entity); |
| 45 | + EntityMigrationMetadata entityMigrationMetadata = databaseMigrationProvider.createEntityMigrationMetadata(entityMetadata); |
| 46 | + if (EasyStringUtil.isBlank(tableMigrationData.getTableName())) { |
| 47 | + throw new EasyQueryInvalidOperationException(String.format("table migration data key:[%s] not found table name.", tableMigrationData.getKey())); |
| 48 | + } |
| 49 | +// if (sql.length() > 0) { |
| 50 | +// sql.append(newLine); |
| 51 | +// } |
| 52 | +// String tableName = EasyToSQLUtil.getTableName(sqlKeyword, entityMetadata, entityMetadata.getTableName(), null, null); |
| 53 | +// String oldTableName = EasyStringUtil.isBlank(entityMetadata.getOldTableName()) ? null : EasyToSQLUtil.getTableName(sqlKeyword, entityMetadata, entityMetadata.getOldTableName(), null, null); |
| 54 | + boolean tableExists = databaseMigrationProvider.tableExists(tableMigrationData.getSchema(), tableMigrationData.getTableName()); |
| 55 | + if (!tableExists) { |
| 56 | + //如果新旧表名不一致 |
| 57 | + if (!Objects.equals(tableMigrationData.getTableName(), tableMigrationData.getOldTableName())) { |
| 58 | + //判断旧表是否存在 |
| 59 | + boolean oldTableExists = databaseMigrationProvider.tableExists(tableMigrationData.getSchema(), tableMigrationData.getOldTableName()); |
| 60 | + if (oldTableExists) { |
| 61 | + //存在就要修改表名 |
| 62 | + MigrationCommand migrationCommand = databaseMigrationProvider.renameTable(entityMigrationMetadata); |
| 63 | + if (migrationCommand != null) { |
| 64 | + migrationCommands.add(migrationCommand); |
| 65 | + } |
| 66 | + //新增修改删除表信息 |
| 67 | + List<MigrationCommand> columns = databaseMigrationProvider.syncTable(entityMigrationMetadata, false); |
| 68 | + if (columns != null) { |
| 69 | + migrationCommands.addAll(columns); |
| 70 | + } |
| 71 | + //判断是否要创建索引 |
| 72 | + } else { |
| 73 | + //表不存在就创建表 |
| 74 | + MigrationCommand migrationCommand = databaseMigrationProvider.createTable(entityMigrationMetadata); |
| 75 | + if (migrationCommand != null) { |
| 76 | + migrationCommands.add(migrationCommand); |
| 77 | + } |
| 78 | + //创建外键 |
| 79 | + List<MigrationCommand> tableForeignKeyCommands = databaseMigrationProvider.createTableForeignKey(entityMigrationMetadata, runtimeContext); |
| 80 | + migrationCommands.addAll(tableForeignKeyCommands); |
| 81 | + //创建索引 |
| 82 | + List<MigrationCommand> tableIndexCommands = databaseMigrationProvider.createTableIndex(entityMigrationMetadata); |
| 83 | + migrationCommands.addAll(tableIndexCommands); |
| 84 | + } |
| 85 | + } else { |
| 86 | + //表不存在就创建表 |
| 87 | + MigrationCommand migrationCommand = databaseMigrationProvider.createTable(entityMigrationMetadata); |
| 88 | + if (migrationCommand != null) { |
| 89 | + migrationCommands.add(migrationCommand); |
| 90 | + } |
| 91 | + //创建外键 |
| 92 | + List<MigrationCommand> tableForeignKeyCommands = databaseMigrationProvider.createTableForeignKey(entityMigrationMetadata, runtimeContext); |
| 93 | + migrationCommands.addAll(tableForeignKeyCommands); |
| 94 | + //创建索引 |
| 95 | + List<MigrationCommand> tableIndexCommands = databaseMigrationProvider.createTableIndex(entityMigrationMetadata); |
| 96 | + migrationCommands.addAll(tableIndexCommands); |
| 97 | + } |
| 98 | + } else { |
| 99 | + List<MigrationCommand> columns = databaseMigrationProvider.syncTable(entityMigrationMetadata, false); |
| 100 | + if (columns != null) { |
| 101 | + migrationCommands.addAll(columns); |
| 102 | + } |
| 103 | + //创建外键 |
| 104 | + List<MigrationCommand> tableForeignKeyCommands = databaseMigrationProvider.syncTableForeignKey(entityMigrationMetadata, runtimeContext, false); |
| 105 | + migrationCommands.addAll(tableForeignKeyCommands); |
| 106 | + //创建索引 |
| 107 | + List<MigrationCommand> tableIndexCommands = databaseMigrationProvider.syncTableIndex(entityMigrationMetadata, false); |
| 108 | + migrationCommands.addAll(tableIndexCommands); |
| 109 | + } |
| 110 | + |
| 111 | +// for (ColumnMetadata column : entityMetadata.getColumns()) { |
| 112 | +// |
| 113 | +// } |
| 114 | +// foreach (var tbcol in tb.ColumnsByPosition) |
| 115 | +// { |
| 116 | +// sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ").Append(tbcol.Attribute.DbType); |
| 117 | +// if (tbcol.Attribute.IsIdentity == true && tbcol.Attribute.DbType.IndexOf("AUTO_INCREMENT", StringComparison.CurrentCultureIgnoreCase) == -1) sb.Append(" AUTO_INCREMENT"); |
| 118 | +// if (string.IsNullOrEmpty(tbcol.Comment) == false) sb.Append(" COMMENT ").Append(_commonUtils.FormatSql("{0}", tbcol.Comment)); |
| 119 | +// sb.Append(","); |
| 120 | +// } |
| 121 | +// if (tb.Primarys.Any()) |
| 122 | +// { |
| 123 | +// sb.Append(" \r\n PRIMARY KEY ("); |
| 124 | +// foreach (var tbcol in tb.Primarys) sb.Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(", "); |
| 125 | +// sb.Remove(sb.Length - 2, 2).Append("),"); |
| 126 | +// } |
| 127 | + |
| 128 | + } |
| 129 | + return migrationCommands; |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public List<MigrationCommand> generateCreateTableMigrationSQL(MigrationContext migrationContext) { |
| 134 | + ArrayList<MigrationCommand> migrationCommands = new ArrayList<>(migrationContext.getEntities().size()); |
| 135 | + for (Class<?> entity : migrationContext.getEntities()) { |
| 136 | + EntityMetadata entityMetadata = entityMetadataManager.getEntityMetadata(entity); |
| 137 | + EntityMigrationMetadata entityMigrationMetadata = new EntityMigrationMetadata(entityMetadata); |
| 138 | + MigrationCommand migrationCommand = databaseMigrationProvider.createTable(entityMigrationMetadata); |
| 139 | + if (migrationCommand != null) { |
| 140 | + migrationCommands.add(migrationCommand); |
| 141 | + } |
| 142 | + List<MigrationCommand> tableIndexCommands = databaseMigrationProvider.createTableIndex(entityMigrationMetadata); |
| 143 | + migrationCommands.addAll(tableIndexCommands); |
| 144 | + } |
| 145 | + return migrationCommands; |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public List<MigrationCommand> generateDropTableMigrationSQL(MigrationContext migrationContext,boolean checkTableExists) { |
| 150 | + ArrayList<MigrationCommand> migrationCommands = new ArrayList<>(migrationContext.getEntities().size()); |
| 151 | + for (Class<?> entity : migrationContext.getEntities()) { |
| 152 | + if(checkTableExists){ |
| 153 | + if(!tableExists(entity)){ |
| 154 | + continue; |
| 155 | + } |
| 156 | + } |
| 157 | + EntityMetadata entityMetadata = entityMetadataManager.getEntityMetadata(entity); |
| 158 | + MigrationCommand migrationCommand = databaseMigrationProvider.dropTable(new EntityMigrationMetadata(entityMetadata)); |
| 159 | + if (migrationCommand != null) { |
| 160 | + migrationCommands.add(migrationCommand); |
| 161 | + } |
| 162 | + } |
| 163 | + return migrationCommands; |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public boolean tableExists(Class<?> entityType) { |
| 168 | + EntityMetadata entityMetadata = entityMetadataManager.getEntityMetadata(entityType); |
| 169 | + return databaseMigrationProvider.tableExists(entityMetadata.getSchemaOrNull(), entityMetadata.getTableName()); |
| 170 | + } |
| 171 | +} |
0 commit comments