| 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 WixInternal.TestSupport |
| 4 | { |
| 5 | using System; |
| 6 | using System.Collections.Generic; |
| 7 | using System.IO; |
| 8 | using System.Linq; |
| 9 | using System.Text; |
| 10 | using WixToolset.Dtf.Compression.Cab; |
| 11 | using WixToolset.Dtf.WindowsInstaller; |
| 12 | |
| 13 | public class Query |
| 14 | { |
| 15 | public static string[] QueryDatabase(string path, string[] tables) |
| 16 | { |
| 17 | var results = new List<string>(); |
| 18 | var resultsByTable = QueryDatabaseByTable(path, tables); |
| 19 | var sortedTables = tables.ToList(); |
| 20 | sortedTables.Sort(); |
| 21 | foreach (var tableName in sortedTables) |
| 22 | { |
| 23 | var rows = resultsByTable[tableName]; |
| 24 | rows?.ForEach(r => results.Add($"{tableName}:{r}")); |
| 25 | } |
| 26 | return results.ToArray(); |
| 27 | } |
| 28 | |
| 29 | public static string[] QueryDatabase(Database db, string[] tables) |
| 30 | { |
| 31 | var results = new List<string>(); |
| 32 | var resultsByTable = QueryDatabaseByTable(db, tables); |
| 33 | var sortedTables = tables.ToList(); |
| 34 | sortedTables.Sort(); |
| 35 | foreach (var tableName in sortedTables) |
| 36 | { |
| 37 | var rows = resultsByTable[tableName]; |
| 38 | rows?.ForEach(r => results.Add($"{tableName}:{r}")); |
| 39 | } |
| 40 | return results.ToArray(); |
| 41 | } |
| 42 | |
| 43 | /// <summary> |
| 44 | /// Returns rows from requested tables formatted to facilitate testing. |
| 45 | /// If the table did not exist in the database, its list will be null. |
| 46 | /// </summary> |
| 47 | /// <param name="path"></param> |
| 48 | /// <param name="tables"></param> |
| 49 | /// <returns></returns> |
| 50 | public static Dictionary<string, List<string>> QueryDatabaseByTable(string path, string[] tables) |
| 51 | { |
| 52 | var results = new Dictionary<string, List<string>>(); |
| 53 | |
| 54 | if (tables?.Length > 0) |
| 55 | { |
| 56 | using (var db = new Database(path)) |
| 57 | { |
| 58 | results = QueryDatabaseByTable(db, tables); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | return results; |
| 63 | } |
| 64 | |
| 65 | /// <summary> |
| 66 | /// Returns rows from requested tables formatted to facilitate testing. |
| 67 | /// If the table did not exist in the database, its list will be null. |
| 68 | /// </summary> |
| 69 | /// <param name="db"></param> |
| 70 | /// <param name="tables"></param> |
| 71 | /// <returns></returns> |
| 72 | public static Dictionary<string, List<string>> QueryDatabaseByTable(Database db, string[] tables) |
| 73 | { |
| 74 | var results = new Dictionary<string, List<string>>(); |
| 75 | |
| 76 | if (tables?.Length > 0) |
| 77 | { |
| 78 | var sb = new StringBuilder(); |
| 79 | |
| 80 | foreach (var table in tables) |
| 81 | { |
| 82 | if (table == "_SummaryInformation") |
| 83 | { |
| 84 | var entries = new List<string>(); |
| 85 | results.Add(table, entries); |
| 86 | |
| 87 | entries.Add($"Title\t{db.SummaryInfo.Title}"); |
| 88 | entries.Add($"Subject\t{db.SummaryInfo.Subject}"); |
| 89 | entries.Add($"Author\t{db.SummaryInfo.Author}"); |
| 90 | entries.Add($"Keywords\t{db.SummaryInfo.Keywords}"); |
| 91 | entries.Add($"Comments\t{db.SummaryInfo.Comments}"); |
| 92 | entries.Add($"Template\t{db.SummaryInfo.Template}"); |
| 93 | entries.Add($"CodePage\t{db.SummaryInfo.CodePage}"); |
| 94 | entries.Add($"PageCount\t{db.SummaryInfo.PageCount}"); |
| 95 | entries.Add($"WordCount\t{db.SummaryInfo.WordCount}"); |
| 96 | entries.Add($"CharacterCount\t{db.SummaryInfo.CharacterCount}"); |
| 97 | entries.Add($"Security\t{db.SummaryInfo.Security}"); |
| 98 | |
| 99 | continue; |
| 100 | } |
| 101 | |
| 102 | if (!db.IsTablePersistent(table)) |
| 103 | { |
| 104 | results.Add(table, null); |
| 105 | continue; |
| 106 | } |
| 107 | |
| 108 | var rows = new List<string>(); |
| 109 | results.Add(table, rows); |
| 110 | |
| 111 | using (var view = db.OpenView("SELECT * FROM `{0}`", table)) |
| 112 | { |
| 113 | view.Execute(); |
| 114 | |
| 115 | Record record; |
| 116 | while ((record = view.Fetch()) != null) |
| 117 | { |
| 118 | sb.Clear(); |
| 119 | |
| 120 | using (record) |
| 121 | { |
| 122 | for (var i = 0; i < record.FieldCount; ++i) |
| 123 | { |
| 124 | if (i > 0) |
| 125 | { |
| 126 | sb.Append("\t"); |
| 127 | } |
| 128 | |
| 129 | sb.Append(record[i + 1]?.ToString()); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | rows.Add(sb.ToString()); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | rows.Sort(); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | return results; |
| 142 | } |
| 143 | |
| 144 | public static CabFileInfo[] GetCabinetFiles(string path) |
| 145 | { |
| 146 | var cab = new CabInfo(path); |
| 147 | |
| 148 | var result = cab.GetFiles(); |
| 149 | |
| 150 | return result.Select(c => c).ToArray(); |
| 151 | } |
| 152 | |
| 153 | public static void ExtractStream(string path, string streamName, string outputPath) |
| 154 | { |
| 155 | Directory.CreateDirectory(Path.GetDirectoryName(outputPath)); |
| 156 | |
| 157 | using (var db = new Database(path)) |
| 158 | using (var view = db.OpenView("SELECT `Data` FROM `_Streams` WHERE `Name` = '{0}'", streamName)) |
| 159 | { |
| 160 | view.Execute(); |
| 161 | |
| 162 | using (var record = view.Fetch()) |
| 163 | { |
| 164 | record.GetStream(1, outputPath); |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | public static void ExtractSubStorage(string path, string subStorageName, string outputPath) |
| 170 | { |
| 171 | Directory.CreateDirectory(Path.GetDirectoryName(outputPath)); |
| 172 | |
| 173 | using (var db = new Database(path)) |
| 174 | using (var view = db.OpenView("SELECT `Name`, `Data` FROM `_Storages` WHERE `Name` = '{0}'", subStorageName)) |
| 175 | { |
| 176 | view.Execute(); |
| 177 | |
| 178 | using (var record = view.Fetch()) |
| 179 | { |
| 180 | var name = record.GetString(1); |
| 181 | record.GetStream(2, outputPath); |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | public static string[] GetSubStorageNames(string path) |
| 187 | { |
| 188 | var result = new List<string>(); |
| 189 | |
| 190 | using (var db = new Database(path)) |
| 191 | using (var view = db.OpenView("SELECT `Name` FROM `_Storages`")) |
| 192 | { |
| 193 | view.Execute(); |
| 194 | |
| 195 | Record record; |
| 196 | while ((record = view.Fetch()) != null) |
| 197 | { |
| 198 | var name = record.GetString(1); |
| 199 | result.Add(name); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | result.Sort(); |
| 204 | return result.ToArray(); |
| 205 | } |
| 206 | } |
| 207 | } |