diff options
Diffstat (limited to 'src/WixToolset.Core.WindowsInstaller/Msi/Database.cs')
-rw-r--r-- | src/WixToolset.Core.WindowsInstaller/Msi/Database.cs | 65 |
1 files changed, 6 insertions, 59 deletions
diff --git a/src/WixToolset.Core.WindowsInstaller/Msi/Database.cs b/src/WixToolset.Core.WindowsInstaller/Msi/Database.cs index 801ebdde..ccb0e6cf 100644 --- a/src/WixToolset.Core.WindowsInstaller/Msi/Database.cs +++ b/src/WixToolset.Core.WindowsInstaller/Msi/Database.cs | |||
@@ -3,13 +3,11 @@ | |||
3 | namespace WixToolset.Msi | 3 | namespace WixToolset.Msi |
4 | { | 4 | { |
5 | using System; | 5 | using System; |
6 | using System.ComponentModel; | ||
7 | using System.Globalization; | 6 | using System.Globalization; |
8 | using System.IO; | 7 | using System.IO; |
9 | using System.Text; | ||
10 | using System.Threading; | 8 | using System.Threading; |
11 | using WixToolset.Data; | ||
12 | using WixToolset.Core.Native; | 9 | using WixToolset.Core.Native; |
10 | using WixToolset.Data; | ||
13 | 11 | ||
14 | /// <summary> | 12 | /// <summary> |
15 | /// Wrapper class for managing MSI API database handles. | 13 | /// Wrapper class for managing MSI API database handles. |
@@ -25,8 +23,7 @@ namespace WixToolset.Msi | |||
25 | /// <param name="type">Persist mode to use when opening the database.</param> | 23 | /// <param name="type">Persist mode to use when opening the database.</param> |
26 | public Database(string path, OpenDatabase type) | 24 | public Database(string path, OpenDatabase type) |
27 | { | 25 | { |
28 | uint handle = 0; | 26 | int error = MsiInterop.MsiOpenDatabase(path, new IntPtr((int)type), out var handle); |
29 | int error = MsiInterop.MsiOpenDatabase(path, new IntPtr((int)type), out handle); | ||
30 | if (0 != error) | 27 | if (0 != error) |
31 | { | 28 | { |
32 | throw new MsiException(error); | 29 | throw new MsiException(error); |
@@ -153,9 +150,9 @@ namespace WixToolset.Msi | |||
153 | /// <param name="fileName">Specifies the name of the exported table archive file.</param> | 150 | /// <param name="fileName">Specifies the name of the exported table archive file.</param> |
154 | public void Export(string tableName, string folderPath, string fileName) | 151 | public void Export(string tableName, string folderPath, string fileName) |
155 | { | 152 | { |
156 | if (null == folderPath || 0 == folderPath.Length) | 153 | if (String.IsNullOrEmpty(folderPath)) |
157 | { | 154 | { |
158 | folderPath = System.Environment.CurrentDirectory; | 155 | folderPath = Environment.CurrentDirectory; |
159 | } | 156 | } |
160 | 157 | ||
161 | int error = MsiInterop.MsiDatabaseExport(this.Handle, tableName, folderPath, fileName); | 158 | int error = MsiInterop.MsiDatabaseExport(this.Handle, tableName, folderPath, fileName); |
@@ -241,63 +238,13 @@ namespace WixToolset.Msi | |||
241 | /// primary key columns for a specified table.</returns> | 238 | /// primary key columns for a specified table.</returns> |
242 | public Record PrimaryKeys(string tableName) | 239 | public Record PrimaryKeys(string tableName) |
243 | { | 240 | { |
244 | uint recordHandle; | 241 | var error = MsiInterop.MsiDatabaseGetPrimaryKeys(this.Handle, tableName, out var recordHandle); |
245 | int error = MsiInterop.MsiDatabaseGetPrimaryKeys(this.Handle, tableName, out recordHandle); | 242 | if (error != 0) |
246 | if (0 != error) | ||
247 | { | 243 | { |
248 | throw new MsiException(error); | 244 | throw new MsiException(error); |
249 | } | 245 | } |
250 | 246 | ||
251 | return new Record(recordHandle); | 247 | return new Record(recordHandle); |
252 | } | 248 | } |
253 | |||
254 | /// <summary> | ||
255 | /// Imports a table into the database. | ||
256 | /// </summary> | ||
257 | /// <param name="codepage">Codepage of the database to import table to.</param> | ||
258 | /// <param name="table">Table to import into database.</param> | ||
259 | /// <param name="baseDirectory">The base directory where intermediate files are created.</param> | ||
260 | /// <param name="keepAddedColumns">Whether to keep columns added in a transform.</param> | ||
261 | public void ImportTable(int codepage, Table table, string baseDirectory, bool keepAddedColumns) | ||
262 | { | ||
263 | // write out the table to an IDT file | ||
264 | string idtPath = Path.Combine(baseDirectory, String.Concat(table.Name, ".idt")); | ||
265 | Encoding encoding; | ||
266 | |||
267 | // If UTF8 encoding, use the UTF8-specific constructor to avoid writing | ||
268 | // the byte order mark at the beginning of the file | ||
269 | if (Encoding.UTF8.CodePage == codepage) | ||
270 | { | ||
271 | encoding = new UTF8Encoding(false, true); | ||
272 | } | ||
273 | else | ||
274 | { | ||
275 | if (0 == codepage) | ||
276 | { | ||
277 | codepage = Encoding.ASCII.CodePage; | ||
278 | } | ||
279 | |||
280 | encoding = Encoding.GetEncoding(codepage, new EncoderExceptionFallback(), new DecoderExceptionFallback()); | ||
281 | } | ||
282 | |||
283 | using (StreamWriter idtWriter = new StreamWriter(idtPath, false, encoding)) | ||
284 | { | ||
285 | table.ToIdtDefinition(idtWriter, keepAddedColumns); | ||
286 | } | ||
287 | |||
288 | // try to import the table into the MSI | ||
289 | try | ||
290 | { | ||
291 | this.Import(idtPath); | ||
292 | } | ||
293 | catch (WixInvalidIdtException) | ||
294 | { | ||
295 | table.ValidateRows(); | ||
296 | |||
297 | // If ValidateRows finds anything it doesn't like, it throws. Otherwise, we'll | ||
298 | // throw WixInvalidIdtException here which is caught in light and turns off tidy. | ||
299 | throw new WixInvalidIdtException(idtPath, table.Name); | ||
300 | } | ||
301 | } | ||
302 | } | 249 | } |
303 | } | 250 | } |