| 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. |
| 2 | |
| 3 | namespace WixToolset.Sql |
| 4 | { |
| 5 | #if TODO_CONSIDER_DECOMPILER |
| 6 | using System.Collections; |
| 7 | using WixToolset.Data; |
| 8 | using WixToolset.Extensibility; |
| 9 | using Sql = WixToolset.Extensions.Serialize.Sql; |
| 10 | using Wix = WixToolset.Data.Serialize; |
| 11 | |
| 12 | /// <summary> |
| 13 | /// The decompiler for the WiX Toolset SQL Server Extension. |
| 14 | /// </summary> |
| 15 | public sealed class SqlDecompiler : DecompilerExtension |
| 16 | { |
| 17 | /// <summary> |
| 18 | /// Creates a decompiler for SQL Extension. |
| 19 | /// </summary> |
| 20 | public SqlDecompiler() |
| 21 | { |
| 22 | this.TableDefinitions = SqlExtensionData.GetExtensionTableDefinitions(); |
| 23 | } |
| 24 | |
| 25 | /// <summary> |
| 26 | /// Get the extensions library to be removed. |
| 27 | /// </summary> |
| 28 | /// <param name="tableDefinitions">Table definitions for library.</param> |
| 29 | /// <returns>Library to remove from decompiled output.</returns> |
| 30 | public override Library GetLibraryToRemove(TableDefinitionCollection tableDefinitions) |
| 31 | { |
| 32 | return SqlExtensionData.GetExtensionLibrary(tableDefinitions); |
| 33 | } |
| 34 | |
| 35 | /// <summary> |
| 36 | /// Decompiles an extension table. |
| 37 | /// </summary> |
| 38 | /// <param name="table">The table to decompile.</param> |
| 39 | public override void DecompileTable(Table table) |
| 40 | { |
| 41 | switch (table.Name) |
| 42 | { |
| 43 | case "SqlDatabase": |
| 44 | this.DecompileSqlDatabaseTable(table); |
| 45 | break; |
| 46 | case "SqlFileSpec": |
| 47 | // handled in FinalizeSqlFileSpecTable |
| 48 | break; |
| 49 | case "SqlScript": |
| 50 | this.DecompileSqlScriptTable(table); |
| 51 | break; |
| 52 | case "SqlString": |
| 53 | this.DecompileSqlStringTable(table); |
| 54 | break; |
| 55 | default: |
| 56 | base.DecompileTable(table); |
| 57 | break; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /// <summary> |
| 62 | /// Finalize decompilation. |
| 63 | /// </summary> |
| 64 | /// <param name="tables">The collection of all tables.</param> |
| 65 | public override void Finish(TableIndexedCollection tables) |
| 66 | { |
| 67 | this.FinalizeSqlFileSpecTable(tables); |
| 68 | this.FinalizeSqlScriptAndSqlStringTables(tables); |
| 69 | } |
| 70 | |
| 71 | /// <summary> |
| 72 | /// Decompile the SqlDatabase table. |
| 73 | /// </summary> |
| 74 | /// <param name="table">The table to decompile.</param> |
| 75 | private void DecompileSqlDatabaseTable(Table table) |
| 76 | { |
| 77 | foreach (Row row in table.Rows) |
| 78 | { |
| 79 | Sql.SqlDatabase sqlDatabase = new Sql.SqlDatabase(); |
| 80 | |
| 81 | sqlDatabase.Id = (string)row[0]; |
| 82 | |
| 83 | if (null != row[1]) |
| 84 | { |
| 85 | sqlDatabase.Server = (string)row[1]; |
| 86 | } |
| 87 | |
| 88 | if (null != row[2]) |
| 89 | { |
| 90 | sqlDatabase.Instance = (string)row[2]; |
| 91 | } |
| 92 | |
| 93 | sqlDatabase.Database = (string)row[3]; |
| 94 | |
| 95 | if (null != row[5]) |
| 96 | { |
| 97 | sqlDatabase.User = (string)row[5]; |
| 98 | } |
| 99 | |
| 100 | // the FileSpec_ and FileSpec_Log columns will be handled in FinalizeSqlFileSpecTable |
| 101 | |
| 102 | if (null != row[8]) |
| 103 | { |
| 104 | int attributes = (int)row[8]; |
| 105 | |
| 106 | if (SqlCompiler.DbCreateOnInstall == (attributes & SqlCompiler.DbCreateOnInstall)) |
| 107 | { |
| 108 | sqlDatabase.CreateOnInstall = Sql.YesNoType.yes; |
| 109 | } |
| 110 | |
| 111 | if (SqlCompiler.DbDropOnUninstall == (attributes & SqlCompiler.DbDropOnUninstall)) |
| 112 | { |
| 113 | sqlDatabase.DropOnUninstall = Sql.YesNoType.yes; |
| 114 | } |
| 115 | |
| 116 | if (SqlCompiler.DbContinueOnError == (attributes & SqlCompiler.DbContinueOnError)) |
| 117 | { |
| 118 | sqlDatabase.ContinueOnError = Sql.YesNoType.yes; |
| 119 | } |
| 120 | |
| 121 | if (SqlCompiler.DbDropOnInstall == (attributes & SqlCompiler.DbDropOnInstall)) |
| 122 | { |
| 123 | sqlDatabase.DropOnInstall = Sql.YesNoType.yes; |
| 124 | } |
| 125 | |
| 126 | if (SqlCompiler.DbCreateOnUninstall == (attributes & SqlCompiler.DbCreateOnUninstall)) |
| 127 | { |
| 128 | sqlDatabase.CreateOnUninstall = Sql.YesNoType.yes; |
| 129 | } |
| 130 | |
| 131 | if (SqlCompiler.DbConfirmOverwrite == (attributes & SqlCompiler.DbConfirmOverwrite)) |
| 132 | { |
| 133 | sqlDatabase.ConfirmOverwrite = Sql.YesNoType.yes; |
| 134 | } |
| 135 | |
| 136 | if (SqlCompiler.DbCreateOnReinstall == (attributes & SqlCompiler.DbCreateOnReinstall)) |
| 137 | { |
| 138 | sqlDatabase.CreateOnReinstall = Sql.YesNoType.yes; |
| 139 | } |
| 140 | |
| 141 | if (SqlCompiler.DbDropOnReinstall == (attributes & SqlCompiler.DbDropOnReinstall)) |
| 142 | { |
| 143 | sqlDatabase.DropOnReinstall = Sql.YesNoType.yes; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | if (null != row[4]) |
| 148 | { |
| 149 | Wix.Component component = (Wix.Component)this.Core.GetIndexedElement("Component", (string)row[4]); |
| 150 | |
| 151 | if (null != component) |
| 152 | { |
| 153 | component.AddChild(sqlDatabase); |
| 154 | } |
| 155 | else |
| 156 | { |
| 157 | this.Core.OnMessage(WixWarnings.ExpectedForeignRow(row.SourceLineNumbers, table.Name, row.GetPrimaryKey(DecompilerConstants.PrimaryKeyDelimiter), "Component_", (string)row[4], "Component")); |
| 158 | } |
| 159 | } |
| 160 | else |
| 161 | { |
| 162 | this.Core.RootElement.AddChild(sqlDatabase); |
| 163 | } |
| 164 | this.Core.IndexElement(row, sqlDatabase); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | /// <summary> |
| 169 | /// Decompile the SqlScript table. |
| 170 | /// </summary> |
| 171 | /// <param name="table">The table to decompile.</param> |
| 172 | private void DecompileSqlScriptTable(Table table) |
| 173 | { |
| 174 | foreach (Row row in table.Rows) |
| 175 | { |
| 176 | Sql.SqlScript sqlScript = new Sql.SqlScript(); |
| 177 | |
| 178 | sqlScript.Id = (string)row[0]; |
| 179 | |
| 180 | // the Db_ and Component_ columns are handled in FinalizeSqlScriptAndSqlStringTables |
| 181 | |
| 182 | sqlScript.BinaryKey = (string)row[3]; |
| 183 | |
| 184 | if (null != row[4]) |
| 185 | { |
| 186 | sqlScript.User = (string)row[4]; |
| 187 | } |
| 188 | |
| 189 | int attributes = (int)row[5]; |
| 190 | |
| 191 | if (SqlCompiler.SqlContinueOnError == (attributes & SqlCompiler.SqlContinueOnError)) |
| 192 | { |
| 193 | sqlScript.ContinueOnError = Sql.YesNoType.yes; |
| 194 | } |
| 195 | |
| 196 | if (SqlCompiler.SqlExecuteOnInstall == (attributes & SqlCompiler.SqlExecuteOnInstall)) |
| 197 | { |
| 198 | sqlScript.ExecuteOnInstall = Sql.YesNoType.yes; |
| 199 | } |
| 200 | |
| 201 | if (SqlCompiler.SqlExecuteOnReinstall == (attributes & SqlCompiler.SqlExecuteOnReinstall)) |
| 202 | { |
| 203 | sqlScript.ExecuteOnReinstall = Sql.YesNoType.yes; |
| 204 | } |
| 205 | |
| 206 | if (SqlCompiler.SqlExecuteOnUninstall == (attributes & SqlCompiler.SqlExecuteOnUninstall)) |
| 207 | { |
| 208 | sqlScript.ExecuteOnUninstall = Sql.YesNoType.yes; |
| 209 | } |
| 210 | |
| 211 | if ((SqlCompiler.SqlRollback | SqlCompiler.SqlExecuteOnInstall) == (attributes & (SqlCompiler.SqlRollback | SqlCompiler.SqlExecuteOnInstall))) |
| 212 | { |
| 213 | sqlScript.RollbackOnInstall = Sql.YesNoType.yes; |
| 214 | } |
| 215 | |
| 216 | if ((SqlCompiler.SqlRollback | SqlCompiler.SqlExecuteOnReinstall) == (attributes & (SqlCompiler.SqlRollback | SqlCompiler.SqlExecuteOnReinstall))) |
| 217 | { |
| 218 | sqlScript.RollbackOnReinstall = Sql.YesNoType.yes; |
| 219 | } |
| 220 | |
| 221 | if ((SqlCompiler.SqlRollback | SqlCompiler.SqlExecuteOnUninstall) == (attributes & (SqlCompiler.SqlRollback | SqlCompiler.SqlExecuteOnUninstall))) |
| 222 | { |
| 223 | sqlScript.RollbackOnUninstall = Sql.YesNoType.yes; |
| 224 | } |
| 225 | |
| 226 | if (null != row[6]) |
| 227 | { |
| 228 | sqlScript.Sequence = (int)row[6]; |
| 229 | } |
| 230 | |
| 231 | this.Core.IndexElement(row, sqlScript); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | /// <summary> |
| 236 | /// Decompile the SqlString table. |
| 237 | /// </summary> |
| 238 | /// <param name="table">The table to decompile.</param> |
| 239 | private void DecompileSqlStringTable(Table table) |
| 240 | { |
| 241 | foreach (Row row in table.Rows) |
| 242 | { |
| 243 | Sql.SqlString sqlString = new Sql.SqlString(); |
| 244 | |
| 245 | sqlString.Id = (string)row[0]; |
| 246 | |
| 247 | // the Db_ and Component_ columns are handled in FinalizeSqlScriptAndSqlStringTables |
| 248 | |
| 249 | sqlString.SQL = (string)row[3]; |
| 250 | |
| 251 | if (null != row[4]) |
| 252 | { |
| 253 | sqlString.User = (string)row[4]; |
| 254 | } |
| 255 | |
| 256 | int attributes = (int)row[5]; |
| 257 | |
| 258 | if (SqlCompiler.SqlContinueOnError == (attributes & SqlCompiler.SqlContinueOnError)) |
| 259 | { |
| 260 | sqlString.ContinueOnError = Sql.YesNoType.yes; |
| 261 | } |
| 262 | |
| 263 | if (SqlCompiler.SqlExecuteOnInstall == (attributes & SqlCompiler.SqlExecuteOnInstall)) |
| 264 | { |
| 265 | sqlString.ExecuteOnInstall = Sql.YesNoType.yes; |
| 266 | } |
| 267 | |
| 268 | if (SqlCompiler.SqlExecuteOnReinstall == (attributes & SqlCompiler.SqlExecuteOnReinstall)) |
| 269 | { |
| 270 | sqlString.ExecuteOnReinstall = Sql.YesNoType.yes; |
| 271 | } |
| 272 | |
| 273 | if (SqlCompiler.SqlExecuteOnUninstall == (attributes & SqlCompiler.SqlExecuteOnUninstall)) |
| 274 | { |
| 275 | sqlString.ExecuteOnUninstall = Sql.YesNoType.yes; |
| 276 | } |
| 277 | |
| 278 | if ((SqlCompiler.SqlRollback | SqlCompiler.SqlExecuteOnInstall) == (attributes & (SqlCompiler.SqlRollback | SqlCompiler.SqlExecuteOnInstall))) |
| 279 | { |
| 280 | sqlString.RollbackOnInstall = Sql.YesNoType.yes; |
| 281 | } |
| 282 | |
| 283 | if ((SqlCompiler.SqlRollback | SqlCompiler.SqlExecuteOnReinstall) == (attributes & (SqlCompiler.SqlRollback | SqlCompiler.SqlExecuteOnReinstall))) |
| 284 | { |
| 285 | sqlString.RollbackOnReinstall = Sql.YesNoType.yes; |
| 286 | } |
| 287 | |
| 288 | if ((SqlCompiler.SqlRollback | SqlCompiler.SqlExecuteOnUninstall) == (attributes & (SqlCompiler.SqlRollback | SqlCompiler.SqlExecuteOnUninstall))) |
| 289 | { |
| 290 | sqlString.RollbackOnUninstall = Sql.YesNoType.yes; |
| 291 | } |
| 292 | |
| 293 | if (null != row[6]) |
| 294 | { |
| 295 | sqlString.Sequence = (int)row[6]; |
| 296 | } |
| 297 | |
| 298 | this.Core.IndexElement(row, sqlString); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | /// <summary> |
| 303 | /// Finalize the SqlFileSpec table. |
| 304 | /// </summary> |
| 305 | /// <param name="tables">The collection of all tables.</param> |
| 306 | /// <remarks> |
| 307 | /// Since rows of the SqlFileSpec table are represented by either |
| 308 | /// the SqlFileSpec or SqlLogFileSpec depending upon the context in |
| 309 | /// which they are used in the SqlDatabase table, decompilation of this |
| 310 | /// table must occur after the SqlDatbase parents are decompiled. |
| 311 | /// </remarks> |
| 312 | private void FinalizeSqlFileSpecTable(TableIndexedCollection tables) |
| 313 | { |
| 314 | Table sqlDatabaseTable = tables["SqlDatabase"]; |
| 315 | Table sqlFileSpecTable = tables["SqlFileSpec"]; |
| 316 | |
| 317 | if (null != sqlDatabaseTable && null != sqlFileSpecTable) |
| 318 | { |
| 319 | Hashtable sqlFileSpecRows = new Hashtable(); |
| 320 | |
| 321 | // index each SqlFileSpec row by its primary key |
| 322 | foreach (Row row in sqlFileSpecTable.Rows) |
| 323 | { |
| 324 | sqlFileSpecRows.Add(row[0], row); |
| 325 | } |
| 326 | |
| 327 | // create the necessary SqlFileSpec and SqlLogFileSpec elements for each row |
| 328 | foreach (Row row in sqlDatabaseTable.Rows) |
| 329 | { |
| 330 | Sql.SqlDatabase sqlDatabase = (Sql.SqlDatabase)this.Core.GetIndexedElement(row); |
| 331 | |
| 332 | if (null != row[6]) |
| 333 | { |
| 334 | Row sqlFileSpecRow = (Row)sqlFileSpecRows[row[6]]; |
| 335 | |
| 336 | if (null != sqlFileSpecRow) |
| 337 | { |
| 338 | Sql.SqlFileSpec sqlFileSpec = new Sql.SqlFileSpec(); |
| 339 | |
| 340 | sqlFileSpec.Id = (string)sqlFileSpecRow[0]; |
| 341 | |
| 342 | if (null != sqlFileSpecRow[1]) |
| 343 | { |
| 344 | sqlFileSpec.Name = (string)sqlFileSpecRow[1]; |
| 345 | } |
| 346 | |
| 347 | sqlFileSpec.Filename = (string)sqlFileSpecRow[2]; |
| 348 | |
| 349 | if (null != sqlFileSpecRow[3]) |
| 350 | { |
| 351 | sqlFileSpec.Size = (string)sqlFileSpecRow[3]; |
| 352 | } |
| 353 | |
| 354 | if (null != sqlFileSpecRow[4]) |
| 355 | { |
| 356 | sqlFileSpec.MaxSize = (string)sqlFileSpecRow[4]; |
| 357 | } |
| 358 | |
| 359 | if (null != sqlFileSpecRow[5]) |
| 360 | { |
| 361 | sqlFileSpec.GrowthSize = (string)sqlFileSpecRow[5]; |
| 362 | } |
| 363 | |
| 364 | sqlDatabase.AddChild(sqlFileSpec); |
| 365 | } |
| 366 | else |
| 367 | { |
| 368 | this.Core.OnMessage(WixWarnings.ExpectedForeignRow(row.SourceLineNumbers, sqlDatabaseTable.Name, row.GetPrimaryKey(DecompilerConstants.PrimaryKeyDelimiter), "FileSpec_", (string)row[6], "SqlFileSpec")); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | if (null != row[7]) |
| 373 | { |
| 374 | Row sqlFileSpecRow = (Row)sqlFileSpecRows[row[7]]; |
| 375 | |
| 376 | if (null != sqlFileSpecRow) |
| 377 | { |
| 378 | Sql.SqlLogFileSpec sqlLogFileSpec = new Sql.SqlLogFileSpec(); |
| 379 | |
| 380 | sqlLogFileSpec.Id = (string)sqlFileSpecRow[0]; |
| 381 | |
| 382 | if (null != sqlFileSpecRow[1]) |
| 383 | { |
| 384 | sqlLogFileSpec.Name = (string)sqlFileSpecRow[1]; |
| 385 | } |
| 386 | |
| 387 | sqlLogFileSpec.Filename = (string)sqlFileSpecRow[2]; |
| 388 | |
| 389 | if (null != sqlFileSpecRow[3]) |
| 390 | { |
| 391 | sqlLogFileSpec.Size = (string)sqlFileSpecRow[3]; |
| 392 | } |
| 393 | |
| 394 | if (null != sqlFileSpecRow[4]) |
| 395 | { |
| 396 | sqlLogFileSpec.MaxSize = (string)sqlFileSpecRow[4]; |
| 397 | } |
| 398 | |
| 399 | if (null != sqlFileSpecRow[5]) |
| 400 | { |
| 401 | sqlLogFileSpec.GrowthSize = (string)sqlFileSpecRow[5]; |
| 402 | } |
| 403 | |
| 404 | sqlDatabase.AddChild(sqlLogFileSpec); |
| 405 | } |
| 406 | else |
| 407 | { |
| 408 | this.Core.OnMessage(WixWarnings.ExpectedForeignRow(row.SourceLineNumbers, sqlDatabaseTable.Name, row.GetPrimaryKey(DecompilerConstants.PrimaryKeyDelimiter), "FileSpec_Log", (string)row[7], "SqlFileSpec")); |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | /// <summary> |
| 416 | /// Finalize the SqlScript table. |
| 417 | /// </summary> |
| 418 | /// <param name="tables">The collection of all tables.</param> |
| 419 | /// <remarks> |
| 420 | /// The SqlScript and SqlString tables contain a foreign key into the SqlDatabase |
| 421 | /// and Component tables. Depending upon the parent of the SqlDatabase |
| 422 | /// element, the SqlScript and SqlString elements are nested under either the |
| 423 | /// SqlDatabase or the Component element. |
| 424 | /// </remarks> |
| 425 | private void FinalizeSqlScriptAndSqlStringTables(TableIndexedCollection tables) |
| 426 | { |
| 427 | Table sqlDatabaseTable = tables["SqlDatabase"]; |
| 428 | Table sqlScriptTable = tables["SqlScript"]; |
| 429 | Table sqlStringTable = tables["SqlString"]; |
| 430 | |
| 431 | Hashtable sqlDatabaseRows = new Hashtable(); |
| 432 | |
| 433 | // index each SqlDatabase row by its primary key |
| 434 | if (null != sqlDatabaseTable) |
| 435 | { |
| 436 | foreach (Row row in sqlDatabaseTable.Rows) |
| 437 | { |
| 438 | sqlDatabaseRows.Add(row[0], row); |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | if (null != sqlScriptTable) |
| 443 | { |
| 444 | foreach (Row row in sqlScriptTable.Rows) |
| 445 | { |
| 446 | Sql.SqlScript sqlScript = (Sql.SqlScript)this.Core.GetIndexedElement(row); |
| 447 | |
| 448 | Row sqlDatabaseRow = (Row)sqlDatabaseRows[row[1]]; |
| 449 | string databaseComponent = (string)sqlDatabaseRow[4]; |
| 450 | |
| 451 | // determine if the SqlScript element should be nested under the database or another component |
| 452 | if (null != databaseComponent && databaseComponent == (string)row[2]) |
| 453 | { |
| 454 | Sql.SqlDatabase sqlDatabase = (Sql.SqlDatabase)this.Core.GetIndexedElement(sqlDatabaseRow); |
| 455 | |
| 456 | sqlDatabase.AddChild(sqlScript); |
| 457 | } |
| 458 | else // nest under the component of the SqlDatabase row |
| 459 | { |
| 460 | Wix.Component component = (Wix.Component)this.Core.GetIndexedElement("Component", (string)row[2]); |
| 461 | |
| 462 | // set the Database value |
| 463 | sqlScript.SqlDb = (string)row[1]; |
| 464 | |
| 465 | if (null != component) |
| 466 | { |
| 467 | component.AddChild(sqlScript); |
| 468 | } |
| 469 | else |
| 470 | { |
| 471 | this.Core.OnMessage(WixWarnings.ExpectedForeignRow(row.SourceLineNumbers, sqlScriptTable.Name, row.GetPrimaryKey(DecompilerConstants.PrimaryKeyDelimiter), "Component_", (string)row[2], "Component")); |
| 472 | } |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | if (null != sqlStringTable) |
| 478 | { |
| 479 | foreach (Row row in sqlStringTable.Rows) |
| 480 | { |
| 481 | Sql.SqlString sqlString = (Sql.SqlString)this.Core.GetIndexedElement(row); |
| 482 | |
| 483 | Row sqlDatabaseRow = (Row)sqlDatabaseRows[row[1]]; |
| 484 | string databaseComponent = (string)sqlDatabaseRow[4]; |
| 485 | |
| 486 | // determine if the SqlScript element should be nested under the database or another component |
| 487 | if (null != databaseComponent && databaseComponent == (string)row[2]) |
| 488 | { |
| 489 | Sql.SqlDatabase sqlDatabase = (Sql.SqlDatabase)this.Core.GetIndexedElement(sqlDatabaseRow); |
| 490 | |
| 491 | sqlDatabase.AddChild(sqlString); |
| 492 | } |
| 493 | else // nest under the component of the SqlDatabase row |
| 494 | { |
| 495 | Wix.Component component = (Wix.Component)this.Core.GetIndexedElement("Component", (string)row[2]); |
| 496 | |
| 497 | // set the Database value |
| 498 | sqlString.SqlDb = (string)row[1]; |
| 499 | |
| 500 | if (null != component) |
| 501 | { |
| 502 | component.AddChild(sqlString); |
| 503 | } |
| 504 | else |
| 505 | { |
| 506 | this.Core.OnMessage(WixWarnings.ExpectedForeignRow(row.SourceLineNumbers, sqlStringTable.Name, row.GetPrimaryKey(DecompilerConstants.PrimaryKeyDelimiter), "Component_", (string)row[2], "Component")); |
| 507 | } |
| 508 | } |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | #endif |
| 514 | } |