|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.IO; |
| 3 | +using System.Linq; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using DacFXToolLib.Common; |
| 6 | +using DacFXToolLib.Dab; |
| 7 | + |
| 8 | +namespace SqlProjectsPowerTools |
| 9 | +{ |
| 10 | + internal class SeedHandler |
| 11 | + { |
| 12 | + public async Task GenerateSeedScriptsAsync(Project project) |
| 13 | + { |
| 14 | + await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); |
| 15 | + |
| 16 | + try |
| 17 | + { |
| 18 | + if (await VSHelper.IsDebugModeAsync()) |
| 19 | + { |
| 20 | + VSHelper.ShowError("Cannot generate code while debugging"); |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + var options = new DataApiBuilderOptions(); |
| 25 | + |
| 26 | + var info = await ChooseDataBaseConnectionAsync(options); |
| 27 | + |
| 28 | + if (info.DatabaseModel == null) |
| 29 | + { |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + var dbInfo = await GetDatabaseInfoAsync(options); |
| 34 | + |
| 35 | + if (dbInfo == null) |
| 36 | + { |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + options.ProjectPath = project.FullPath; |
| 41 | + |
| 42 | + if (!await LoadDataBaseObjectsAsync(options)) |
| 43 | + { |
| 44 | + await VS.StatusBar.ClearAsync(); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + await GenerateScriptAsync(options, dbInfo.ConnectionString); |
| 49 | + } |
| 50 | + catch (AggregateException ae) |
| 51 | + { |
| 52 | + foreach (var innerException in ae.Flatten().InnerExceptions) |
| 53 | + { |
| 54 | + VSHelper.ShowError(innerException.Message); |
| 55 | + } |
| 56 | + } |
| 57 | + catch (Exception exception) |
| 58 | + { |
| 59 | + VSHelper.ShowError(exception.Message); |
| 60 | + } |
| 61 | + finally |
| 62 | + { |
| 63 | + await VS.StatusBar.ClearAsync(); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private static async Task GenerateScriptAsync(DataApiBuilderOptions options, string connectionString) |
| 68 | + { |
| 69 | + await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); |
| 70 | + |
| 71 | + await VS.StatusBar.ShowProgressAsync("Getting seed data", 1, 2); |
| 72 | + |
| 73 | + var revEngRunner = new ProcessLauncher(); |
| 74 | + |
| 75 | + var parts = options.Tables.FirstOrDefault()?.Name.Split('.'); |
| 76 | + |
| 77 | + if (parts == null || parts.Length != 2) |
| 78 | + { |
| 79 | + VSHelper.ShowError("No table selected."); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + var tableName = parts[1]; |
| 84 | + var schema = parts[0]; |
| 85 | + |
| 86 | + var result = await revEngRunner.CreateMergeScriptAsync(options.ProjectPath, connectionString, tableName, schema); |
| 87 | + |
| 88 | + await VS.StatusBar.ShowProgressAsync("Getting seed data", 2, 2); |
| 89 | + |
| 90 | + if (File.Exists(result)) |
| 91 | + { |
| 92 | + await VS.Documents.OpenAsync(result); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + private static async Task<List<TableModel>> GetTablesAsync(string connectionString) |
| 97 | + { |
| 98 | + var builder = new TableListBuilder(connectionString, DatabaseType.SQLServer, null); |
| 99 | + |
| 100 | + return await builder.GetTableDefinitionsAsync(); |
| 101 | + } |
| 102 | + |
| 103 | + private static async Task<bool> LoadDataBaseObjectsAsync(DataApiBuilderOptions options) |
| 104 | + { |
| 105 | + await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); |
| 106 | + |
| 107 | + IEnumerable<TableModel> predefinedTables = null; |
| 108 | + |
| 109 | + try |
| 110 | + { |
| 111 | + await VS.StatusBar.StartAnimationAsync(StatusAnimation.Build); |
| 112 | + |
| 113 | + predefinedTables = await GetTablesAsync(options.ConnectionString); |
| 114 | + |
| 115 | + predefinedTables = predefinedTables |
| 116 | + .Where(t => t.ObjectType == ObjectType.Table); |
| 117 | + } |
| 118 | + catch (InvalidOperationException ex) |
| 119 | + { |
| 120 | + VSHelper.ShowError($"{ex.Message}"); |
| 121 | + return false; |
| 122 | + } |
| 123 | + finally |
| 124 | + { |
| 125 | + await VS.StatusBar.EndAnimationAsync(StatusAnimation.Build); |
| 126 | + } |
| 127 | + |
| 128 | + var preselectedTables = new List<SerializationTableModel>(); |
| 129 | + |
| 130 | + await VS.StatusBar.ClearAsync(); |
| 131 | + |
| 132 | + var ptd = PackageManager.Package.GetView<IPickTablesDialog>() |
| 133 | + .AddTables(predefinedTables, new List<Schema>()) |
| 134 | + .PreselectTables(preselectedTables); |
| 135 | + |
| 136 | + var pickTablesResult = ptd.ShowAndAwaitUserResponse(true); |
| 137 | + |
| 138 | + options.Tables = pickTablesResult.Payload.Objects.ToList(); |
| 139 | + return pickTablesResult.ClosedByOK; |
| 140 | + } |
| 141 | + |
| 142 | + private async Task<(DatabaseConnectionModel DatabaseModel, int FileGenerationMode, bool ImportSettings)> ChooseDataBaseConnectionAsync(DataApiBuilderOptions options) |
| 143 | + { |
| 144 | + var vsDataHelper = new VsDataHelper(); |
| 145 | + var databaseList = await vsDataHelper.GetDataConnectionsAsync(); |
| 146 | + |
| 147 | + var psd = PackageManager.Package.GetView<IPickServerDatabaseDialog>(); |
| 148 | + |
| 149 | + if (databaseList != null && databaseList.Any()) |
| 150 | + { |
| 151 | + psd.PublishConnections(databaseList.Select(m => new DatabaseConnectionModel |
| 152 | + { |
| 153 | + ConnectionName = m.Value.ConnectionName, |
| 154 | + ConnectionString = m.Value.ConnectionString, |
| 155 | + DatabaseType = m.Value.DatabaseType, |
| 156 | + DataConnection = m.Value.DataConnection, |
| 157 | + })); |
| 158 | + } |
| 159 | + |
| 160 | + psd.PublishFileGenerationMode(GetCodeGenerationModes()); |
| 161 | + |
| 162 | + var pickDataSourceResult = psd.ShowAndAwaitUserResponse(true); |
| 163 | + if (!pickDataSourceResult.ClosedByOK) |
| 164 | + { |
| 165 | + return (null, 0, false); |
| 166 | + } |
| 167 | + |
| 168 | + if (pickDataSourceResult.Payload.Connection != null) |
| 169 | + { |
| 170 | + options.ConnectionString = pickDataSourceResult.Payload.Connection.ConnectionString; |
| 171 | + options.DatabaseType = pickDataSourceResult.Payload.Connection.DatabaseType; |
| 172 | + } |
| 173 | + |
| 174 | + return (pickDataSourceResult.Payload.Connection, |
| 175 | + pickDataSourceResult.Payload.CodeGenerationMode, |
| 176 | + pickDataSourceResult.Payload.GetDatabaseOptions); |
| 177 | + } |
| 178 | + |
| 179 | + private static async Task<DatabaseConnectionModel> GetDatabaseInfoAsync(DataApiBuilderOptions options) |
| 180 | + { |
| 181 | + await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); |
| 182 | + |
| 183 | + var dbInfo = new DatabaseConnectionModel(); |
| 184 | + |
| 185 | + if (!string.IsNullOrEmpty(options.ConnectionString)) |
| 186 | + { |
| 187 | + dbInfo.ConnectionString = options.ConnectionString; |
| 188 | + dbInfo.DatabaseType = options.DatabaseType; |
| 189 | + } |
| 190 | + |
| 191 | + if (dbInfo.DatabaseType == DatabaseType.Undefined) |
| 192 | + { |
| 193 | + VSHelper.ShowError($"Unsupported provider"); |
| 194 | + return null; |
| 195 | + } |
| 196 | + |
| 197 | + return dbInfo; |
| 198 | + } |
| 199 | + |
| 200 | + private static List<CodeGenerationItem> GetCodeGenerationModes() |
| 201 | + { |
| 202 | + var list = new List<CodeGenerationItem> |
| 203 | + { |
| 204 | + new CodeGenerationItem { Key = 0, Value = "Seed" }, |
| 205 | + }; |
| 206 | + return list; |
| 207 | + } |
| 208 | + } |
| 209 | +} |
0 commit comments