| | | 1 | | using Elsa.Persistence.VNext.Contracts; |
| | | 2 | | |
| | | 3 | | namespace Elsa.Persistence.VNext.Relational; |
| | | 4 | | |
| | 6 | 5 | | public class RelationalSchemaPlanner(IRelationalTypeMapper typeMapper) : IPersistenceSchemaPlanner<RelationalSchemaPlan> |
| | | 6 | | { |
| | | 7 | | public RelationalSchemaPlan Plan(PersistenceSchema schema) |
| | | 8 | | { |
| | 5 | 9 | | var tables = new List<RelationalTable>(); |
| | 5 | 10 | | var indexes = new List<RelationalIndex>(); |
| | | 11 | | |
| | 10 | 12 | | foreach (var table in schema.Tables) |
| | | 13 | | { |
| | 0 | 14 | | tables.Add(PlanTable(table)); |
| | 0 | 15 | | indexes.AddRange(table.Indexes.Select(index => PlanIndex(table, index))); |
| | | 16 | | } |
| | | 17 | | |
| | 20 | 18 | | foreach (var storageUnit in schema.StorageUnits) |
| | | 19 | | { |
| | 5 | 20 | | tables.Add(PlanTable(storageUnit)); |
| | 30 | 21 | | indexes.AddRange(storageUnit.Indexes.Select(index => PlanIndex(storageUnit, index))); |
| | | 22 | | } |
| | | 23 | | |
| | 5 | 24 | | return new RelationalSchemaPlan(tables, indexes); |
| | | 25 | | } |
| | | 26 | | |
| | | 27 | | private RelationalTable PlanTable(PersistenceTable table) |
| | | 28 | | { |
| | 0 | 29 | | var columns = table.Columns |
| | 0 | 30 | | .Select(column => new RelationalColumn(column.Name, typeMapper.Map(column), column.IsNullable)) |
| | 0 | 31 | | .ToList(); |
| | 0 | 32 | | var primaryKey = table.PrimaryKey is null ? null : new RelationalPrimaryKey(table.PrimaryKey.Name, table.Primary |
| | | 33 | | |
| | 0 | 34 | | return new RelationalTable(table.Name, table.Schema, columns, primaryKey); |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | private static RelationalIndex PlanIndex(PersistenceTable table, PersistenceIndex index) |
| | | 38 | | { |
| | 0 | 39 | | return new RelationalIndex(index.Name, table.Name, table.Schema, index.Columns, index.IsUnique); |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | private RelationalTable PlanTable(PersistenceStorageUnit storageUnit) |
| | | 43 | | { |
| | 5 | 44 | | var columns = storageUnit.Fields |
| | 5 | 45 | | .Select(field => |
| | 5 | 46 | | { |
| | 60 | 47 | | var column = new PersistenceColumn(field.Name, field.Type, field.IsNullable, field.Length); |
| | 60 | 48 | | return new RelationalColumn(field.Name, typeMapper.Map(column), field.IsNullable); |
| | 5 | 49 | | }) |
| | 5 | 50 | | .ToList(); |
| | 5 | 51 | | var primaryKey = storageUnit.Key is null ? null : new RelationalPrimaryKey(storageUnit.Key.Name, storageUnit.Key |
| | | 52 | | |
| | 5 | 53 | | return new RelationalTable(storageUnit.Name, storageUnit.Namespace, columns, primaryKey); |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | private static RelationalIndex PlanIndex(PersistenceStorageUnit storageUnit, PersistenceIndex index) |
| | | 57 | | { |
| | 25 | 58 | | return new RelationalIndex(index.Name, storageUnit.Name, storageUnit.Namespace, index.Columns, index.IsUnique); |
| | | 59 | | } |
| | | 60 | | } |