OLD | NEW |
1 /** | 1 /** |
2 * \file database.h MSI database | 2 * \file database.h MSI database |
3 */ | 3 */ |
4 | 4 |
5 #include "database.h" | 5 #include "database.h" |
6 #include "msiquery.h" | 6 #include "msiquery.h" |
7 | 7 |
8 //------------------------------------------------------------------------------
----------- | 8 //------------------------------------------------------------------------------
----------- |
9 // Database | 9 // Database |
10 //------------------------------------------------------------------------------
----------- | 10 //------------------------------------------------------------------------------
----------- |
11 /** | 11 msi_handle Database::open_view( const wchar_t * query ) |
12 * \par Implementation Notes | |
13 * An MSI database handle is an overloaded type, used both for installation d
atabases and one opened outside an installation. | |
14 * Hence this base constructor initializes with that handle. | |
15 * | |
16 * \sa MSDN "Obtaining a Database Handle" | |
17 * http://msdn.microsoft.com/en-us/library/windows/desktop/aa370541(v=vs.85).
aspx | |
18 */ | |
19 Database::Database( MSIHANDLE handle ) | |
20 : handle( handle ) | |
21 { | 12 { |
22 } | 13 MSIHANDLE view_handle ; |
23 | 14 UINT x = MsiDatabaseOpenView( handle, query, & view_handle ) ; |
24 Database::~Database() | 15 if ( x == ERROR_BAD_QUERY_SYNTAX ) |
25 { | 16 { |
26 MsiCloseHandle( handle ) ; | 17 throw windows_api_error( "MsiDatabaseOpenView", "ERROR_BAD_QUERY_SYNTAX" ) ; |
| 18 } |
| 19 else if ( x == ERROR_INVALID_HANDLE ) |
| 20 { |
| 21 throw windows_api_error( "MsiDatabaseOpenView", "ERROR_INVALID_HANDLE" ) ; |
| 22 } |
| 23 return msi_handle( view_handle ) ; |
27 } | 24 } |
28 | 25 |
29 //------------------------------------------------------------------------------
----------- | 26 //------------------------------------------------------------------------------
----------- |
30 // Installation_Database | 27 // Installation_Database |
31 //------------------------------------------------------------------------------
----------- | 28 //------------------------------------------------------------------------------
----------- |
32 | 29 |
33 /** | 30 /** |
34 * Helper function for Installation_Database constructor. | 31 * Helper function for Installation_Database constructor. |
35 * | 32 * |
36 * \par Resource Allocator | 33 * \par Resource Allocator |
37 * Return value of this function, a handle, must be released in order to avoi
d a resource leak. | 34 * Return value of this function, a handle, must be released in order to avoid
a resource leak. |
38 * Passing it as an argument to the Database constructor is adequate. | 35 * Passing it as an argument to the Database constructor is adequate. |
39 */ | 36 */ |
40 MSIHANDLE get_active_database( Immediate_Session & session ) | 37 msi_handle get_active_database( Immediate_Session & session ) |
41 { | 38 { |
42 MSIHANDLE h = MsiGetActiveDatabase( session.handle ) ; | 39 MSIHANDLE h( MsiGetActiveDatabase( session.handle ) ) ; |
43 if ( h == 0 ) | 40 if ( h == 0 ) |
44 { | 41 { |
45 throw std::runtime_error( "Failed to retrieve active databases" ) ; | 42 throw windows_api_error( "MsiGetActiveDatabase", 0 ) ; |
46 } | 43 } |
47 return h ; | 44 return msi_handle( h ) ; |
48 } | 45 } |
49 | 46 |
50 /** | 47 /** |
51 * \par Implementation Notes | 48 * \par Implementation Notes |
52 * The only thing this constructor needs to do is to initialize the base clas
s. | 49 * The only thing this constructor needs to do is to initialize the base class
. |
53 */ | 50 */ |
54 Installation_Database::Installation_Database( Immediate_Session & session ) | 51 Installation_Database::Installation_Database( Immediate_Session & session ) |
55 : Database( get_active_database( session ) ) | 52 : Database( get_active_database( session ) ) |
56 { | 53 { |
57 // empty body | 54 // empty body |
58 } | 55 } ; |
| 56 |
| 57 //------------------------------------------------------------------------------
----------- |
| 58 // View |
| 59 //------------------------------------------------------------------------------
----------- |
| 60 /** |
| 61 * Implementation function for View::first(). |
| 62 */ |
| 63 void view_first_body( UINT x ) |
| 64 { |
| 65 if ( x != ERROR_SUCCESS ) |
| 66 { |
| 67 throw windows_api_error( "MsiViewExecute", x ) ; |
| 68 } |
| 69 } |
| 70 |
| 71 Record View::first() |
| 72 { |
| 73 view_first_body( MsiViewExecute( _handle, 0 ) ) ; |
| 74 return next() ; |
| 75 } |
| 76 |
| 77 Record View::first( Record & arguments ) |
| 78 { |
| 79 view_first_body( MsiViewExecute( _handle, arguments._handle ) ) ; |
| 80 return next() ; |
| 81 } |
| 82 |
| 83 Record View::next() |
| 84 { |
| 85 MSIHANDLE h ; |
| 86 UINT x = MsiViewFetch( _handle, & h ) ; |
| 87 if ( x == ERROR_NO_MORE_ITEMS ) |
| 88 { |
| 89 return Record( Record::null_t() ) ; |
| 90 } |
| 91 else if ( x == ERROR_SUCCESS ) |
| 92 { |
| 93 return Record( msi_handle( h ) ) ; |
| 94 } |
| 95 throw windows_api_error( "MsiViewFetch", x ) ; |
| 96 } |
| 97 |
OLD | NEW |