diff options
Diffstat (limited to 'src/internal/WixBuildTools.TestSupport/Query.cs')
-rw-r--r-- | src/internal/WixBuildTools.TestSupport/Query.cs | 172 |
1 files changed, 172 insertions, 0 deletions
diff --git a/src/internal/WixBuildTools.TestSupport/Query.cs b/src/internal/WixBuildTools.TestSupport/Query.cs new file mode 100644 index 00000000..101a8890 --- /dev/null +++ b/src/internal/WixBuildTools.TestSupport/Query.cs | |||
@@ -0,0 +1,172 @@ | |||
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 WixBuildTools.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 | /// <summary> | ||
30 | /// Returns rows from requested tables formatted to facilitate testing. | ||
31 | /// If the table did not exist in the database, its list will be null. | ||
32 | /// </summary> | ||
33 | /// <param name="path"></param> | ||
34 | /// <param name="tables"></param> | ||
35 | /// <returns></returns> | ||
36 | public static Dictionary<string, List<string>> QueryDatabaseByTable(string path, string[] tables) | ||
37 | { | ||
38 | var results = new Dictionary<string, List<string>>(); | ||
39 | |||
40 | if (tables?.Length > 0) | ||
41 | { | ||
42 | var sb = new StringBuilder(); | ||
43 | using (var db = new Database(path)) | ||
44 | { | ||
45 | foreach (var table in tables) | ||
46 | { | ||
47 | if (table == "_SummaryInformation") | ||
48 | { | ||
49 | var entries = new List<string>(); | ||
50 | results.Add(table, entries); | ||
51 | |||
52 | entries.Add($"Title\t{db.SummaryInfo.Title}"); | ||
53 | entries.Add($"Subject\t{db.SummaryInfo.Subject}"); | ||
54 | entries.Add($"Author\t{db.SummaryInfo.Author}"); | ||
55 | entries.Add($"Keywords\t{db.SummaryInfo.Keywords}"); | ||
56 | entries.Add($"Comments\t{db.SummaryInfo.Comments}"); | ||
57 | entries.Add($"Template\t{db.SummaryInfo.Template}"); | ||
58 | entries.Add($"CodePage\t{db.SummaryInfo.CodePage}"); | ||
59 | entries.Add($"PageCount\t{db.SummaryInfo.PageCount}"); | ||
60 | entries.Add($"WordCount\t{db.SummaryInfo.WordCount}"); | ||
61 | entries.Add($"CharacterCount\t{db.SummaryInfo.CharacterCount}"); | ||
62 | entries.Add($"Security\t{db.SummaryInfo.Security}"); | ||
63 | |||
64 | continue; | ||
65 | } | ||
66 | |||
67 | if (!db.IsTablePersistent(table)) | ||
68 | { | ||
69 | results.Add(table, null); | ||
70 | continue; | ||
71 | } | ||
72 | |||
73 | var rows = new List<string>(); | ||
74 | results.Add(table, rows); | ||
75 | |||
76 | using (var view = db.OpenView("SELECT * FROM `{0}`", table)) | ||
77 | { | ||
78 | view.Execute(); | ||
79 | |||
80 | Record record; | ||
81 | while ((record = view.Fetch()) != null) | ||
82 | { | ||
83 | sb.Clear(); | ||
84 | |||
85 | using (record) | ||
86 | { | ||
87 | for (var i = 0; i < record.FieldCount; ++i) | ||
88 | { | ||
89 | if (i > 0) | ||
90 | { | ||
91 | sb.Append("\t"); | ||
92 | } | ||
93 | |||
94 | sb.Append(record[i + 1]?.ToString()); | ||
95 | } | ||
96 | } | ||
97 | |||
98 | rows.Add(sb.ToString()); | ||
99 | } | ||
100 | } | ||
101 | rows.Sort(); | ||
102 | } | ||
103 | } | ||
104 | } | ||
105 | |||
106 | return results; | ||
107 | } | ||
108 | |||
109 | public static CabFileInfo[] GetCabinetFiles(string path) | ||
110 | { | ||
111 | var cab = new CabInfo(path); | ||
112 | |||
113 | var result = cab.GetFiles(); | ||
114 | |||
115 | return result.Select(c => c).ToArray(); | ||
116 | } | ||
117 | |||
118 | public static void ExtractStream(string path, string streamName, string outputPath) | ||
119 | { | ||
120 | Directory.CreateDirectory(Path.GetDirectoryName(outputPath)); | ||
121 | |||
122 | using (var db = new Database(path)) | ||
123 | using (var view = db.OpenView("SELECT `Data` FROM `_Streams` WHERE `Name` = '{0}'", streamName)) | ||
124 | { | ||
125 | view.Execute(); | ||
126 | |||
127 | using (var record = view.Fetch()) | ||
128 | { | ||
129 | record.GetStream(1, outputPath); | ||
130 | } | ||
131 | } | ||
132 | } | ||
133 | |||
134 | public static void ExtractSubStorage(string path, string subStorageName, string outputPath) | ||
135 | { | ||
136 | Directory.CreateDirectory(Path.GetDirectoryName(outputPath)); | ||
137 | |||
138 | using (var db = new Database(path)) | ||
139 | using (var view = db.OpenView("SELECT `Name`, `Data` FROM `_Storages` WHERE `Name` = '{0}'", subStorageName)) | ||
140 | { | ||
141 | view.Execute(); | ||
142 | |||
143 | using (var record = view.Fetch()) | ||
144 | { | ||
145 | var name = record.GetString(1); | ||
146 | record.GetStream(2, outputPath); | ||
147 | } | ||
148 | } | ||
149 | } | ||
150 | |||
151 | public static string[] GetSubStorageNames(string path) | ||
152 | { | ||
153 | var result = new List<string>(); | ||
154 | |||
155 | using (var db = new Database(path)) | ||
156 | using (var view = db.OpenView("SELECT `Name` FROM `_Storages`")) | ||
157 | { | ||
158 | view.Execute(); | ||
159 | |||
160 | Record record; | ||
161 | while ((record = view.Fetch()) != null) | ||
162 | { | ||
163 | var name = record.GetString(1); | ||
164 | result.Add(name); | ||
165 | } | ||
166 | } | ||
167 | |||
168 | result.Sort(); | ||
169 | return result.ToArray(); | ||
170 | } | ||
171 | } | ||
172 | } | ||