2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2002,2003,2004,2005 Mike McCormack for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #define NONAMELESSUNION
30 #include "wine/debug.h"
31 #include "wine/unicode.h"
37 #include "msiserver.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(msi
);
47 * An .msi file is a structured storage file.
48 * It contains a number of streams.
49 * A stream for each table in the database.
50 * Two streams for the string table in the database.
51 * Any binary data in a table is a reference to a stream.
54 static VOID
MSI_CloseDatabase( MSIOBJECTHDR
*arg
)
56 MSIDATABASE
*db
= (MSIDATABASE
*) arg
;
59 free_cached_tables( db
);
60 msi_free_transforms( db
);
61 msi_destroy_stringtable( db
->strings
);
62 IStorage_Release( db
->storage
);
65 DeleteFileW( db
->deletefile
);
66 msi_free( db
->deletefile
);
70 UINT
MSI_OpenDatabaseW(LPCWSTR szDBPath
, LPCWSTR szPersist
, MSIDATABASE
**pdb
)
74 MSIDATABASE
*db
= NULL
;
75 UINT ret
= ERROR_FUNCTION_FAILED
;
76 LPCWSTR szMode
, save_path
;
81 static const WCHAR szTables
[] = { '_','T','a','b','l','e','s',0 };
83 TRACE("%s %s\n",debugstr_w(szDBPath
),debugstr_w(szPersist
) );
86 return ERROR_INVALID_PARAMETER
;
88 if (szPersist
- MSIDBOPEN_PATCHFILE
>= MSIDBOPEN_READONLY
&&
89 szPersist
- MSIDBOPEN_PATCHFILE
<= MSIDBOPEN_CREATEDIRECT
)
91 TRACE("Database is a patch\n");
92 szPersist
-= MSIDBOPEN_PATCHFILE
;
97 if( HIWORD( szPersist
) )
99 if (!CopyFileW( szDBPath
, szPersist
, FALSE
))
100 return ERROR_OPEN_FAILED
;
102 szDBPath
= szPersist
;
103 szPersist
= MSIDBOPEN_TRANSACT
;
107 if( szPersist
== MSIDBOPEN_READONLY
)
109 r
= StgOpenStorage( szDBPath
, NULL
,
110 STGM_DIRECT
|STGM_READ
|STGM_SHARE_DENY_WRITE
, NULL
, 0, &stg
);
112 else if( szPersist
== MSIDBOPEN_CREATE
|| szPersist
== MSIDBOPEN_CREATEDIRECT
)
114 /* FIXME: MSIDBOPEN_CREATE should case STGM_TRANSACTED flag to be
116 r
= StgCreateDocfile( szDBPath
,
117 STGM_CREATE
|STGM_DIRECT
|STGM_READWRITE
|STGM_SHARE_EXCLUSIVE
, 0, &stg
);
118 if( r
== ERROR_SUCCESS
)
120 IStorage_SetClass( stg
, &CLSID_MsiDatabase
);
121 /* create the _Tables stream */
122 r
= write_stream_data(stg
, szTables
, NULL
, 0, TRUE
);
124 r
= msi_init_string_table( stg
);
128 else if( szPersist
== MSIDBOPEN_TRANSACT
)
130 /* FIXME: MSIDBOPEN_TRANSACT should case STGM_TRANSACTED flag to be
132 r
= StgOpenStorage( szDBPath
, NULL
,
133 STGM_DIRECT
|STGM_READWRITE
|STGM_SHARE_EXCLUSIVE
, NULL
, 0, &stg
);
135 else if( szPersist
== MSIDBOPEN_DIRECT
)
137 r
= StgOpenStorage( szDBPath
, NULL
,
138 STGM_DIRECT
|STGM_READWRITE
|STGM_SHARE_EXCLUSIVE
, NULL
, 0, &stg
);
142 ERR("unknown flag %p\n",szPersist
);
143 return ERROR_INVALID_PARAMETER
;
146 if( FAILED( r
) || !stg
)
148 FIXME("open failed r = %08x for %s\n", r
, debugstr_w(szDBPath
));
149 return ERROR_FUNCTION_FAILED
;
152 r
= IStorage_Stat( stg
, &stat
, STATFLAG_NONAME
);
155 FIXME("Failed to stat storage\n");
159 if ( !IsEqualGUID( &stat
.clsid
, &CLSID_MsiDatabase
) &&
160 !IsEqualGUID( &stat
.clsid
, &CLSID_MsiPatch
) &&
161 !IsEqualGUID( &stat
.clsid
, &CLSID_MsiTransform
) )
163 ERR("storage GUID is not a MSI database GUID %s\n",
164 debugstr_guid(&stat
.clsid
) );
168 db
= alloc_msiobject( MSIHANDLETYPE_DATABASE
, sizeof (MSIDATABASE
),
172 FIXME("Failed to allocate a handle\n");
176 if (!strchrW( save_path
, '\\' ))
178 GetCurrentDirectoryW( MAX_PATH
, path
);
179 lstrcatW( path
, szBackSlash
);
180 lstrcatW( path
, save_path
);
183 lstrcpyW( path
, save_path
);
185 db
->path
= strdupW( path
);
187 if( TRACE_ON( msi
) )
188 enum_stream_names( stg
);
193 db
->deletefile
= strdupW( szDBPath
);
195 db
->deletefile
= NULL
;
196 list_init( &db
->tables
);
197 list_init( &db
->transforms
);
199 db
->strings
= msi_load_string_table( stg
, &db
->bytes_per_strref
);
205 msiobj_addref( &db
->hdr
);
206 IStorage_AddRef( stg
);
211 msiobj_release( &db
->hdr
);
213 IStorage_Release( stg
);
218 UINT WINAPI
MsiOpenDatabaseW(LPCWSTR szDBPath
, LPCWSTR szPersist
, MSIHANDLE
*phDB
)
223 TRACE("%s %s %p\n",debugstr_w(szDBPath
),debugstr_w(szPersist
), phDB
);
225 ret
= MSI_OpenDatabaseW( szDBPath
, szPersist
, &db
);
226 if( ret
== ERROR_SUCCESS
)
228 *phDB
= alloc_msihandle( &db
->hdr
);
230 ret
= ERROR_NOT_ENOUGH_MEMORY
;
231 msiobj_release( &db
->hdr
);
237 UINT WINAPI
MsiOpenDatabaseA(LPCSTR szDBPath
, LPCSTR szPersist
, MSIHANDLE
*phDB
)
239 HRESULT r
= ERROR_FUNCTION_FAILED
;
240 LPWSTR szwDBPath
= NULL
, szwPersist
= NULL
;
242 TRACE("%s %s %p\n", debugstr_a(szDBPath
), debugstr_a(szPersist
), phDB
);
246 szwDBPath
= strdupAtoW( szDBPath
);
251 if( HIWORD(szPersist
) )
253 szwPersist
= strdupAtoW( szPersist
);
258 szwPersist
= (LPWSTR
)(DWORD_PTR
)szPersist
;
260 r
= MsiOpenDatabaseW( szwDBPath
, szwPersist
, phDB
);
263 if( HIWORD(szPersist
) )
264 msi_free( szwPersist
);
265 msi_free( szwDBPath
);
270 static LPWSTR
msi_read_text_archive(LPCWSTR path
)
275 DWORD read
, size
= 0;
277 file
= CreateFileW( path
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, NULL
);
278 if (file
== INVALID_HANDLE_VALUE
)
281 size
= GetFileSize( file
, NULL
);
282 data
= msi_alloc( size
+ 1 );
286 if (!ReadFile( file
, data
, size
, &read
, NULL
))
290 wdata
= strdupAtoW( data
);
298 static void msi_parse_line(LPWSTR
*line
, LPWSTR
**entries
, DWORD
*num_entries
)
300 LPWSTR ptr
= *line
, save
;
305 /* stay on this line */
306 while (*ptr
&& *ptr
!= '\n')
308 /* entries are separated by tabs */
315 *entries
= msi_alloc(count
* sizeof(LPWSTR
));
319 /* store pointers into the data */
320 for (i
= 0, ptr
= *line
; i
< count
; i
++)
322 while (*ptr
&& *ptr
== '\r') ptr
++;
325 while (*ptr
&& *ptr
!= '\t' && *ptr
!= '\n' && *ptr
!= '\r') ptr
++;
327 /* NULL-separate the data */
328 if (*ptr
== '\n' || *ptr
== '\r')
330 while (*ptr
== '\n' || *ptr
== '\r')
336 (*entries
)[i
] = save
;
339 /* move to the next line if there's more, else EOF */
343 *num_entries
= count
;
346 static LPWSTR
msi_build_createsql_prelude(LPWSTR table
)
351 static const WCHAR create_fmt
[] = {'C','R','E','A','T','E',' ','T','A','B','L','E',' ','`','%','s','`',' ','(',' ',0};
353 size
= sizeof(create_fmt
)/sizeof(create_fmt
[0]) + lstrlenW(table
) - 2;
354 prelude
= msi_alloc(size
* sizeof(WCHAR
));
358 sprintfW(prelude
, create_fmt
, table
);
362 static LPWSTR
msi_build_createsql_columns(LPWSTR
*columns_data
, LPWSTR
*types
, DWORD num_columns
)
366 DWORD sql_size
= 1, i
, len
;
367 WCHAR expanded
[128], *ptr
;
368 WCHAR size
[10], comma
[2], extra
[30];
370 static const WCHAR column_fmt
[] = {'`','%','s','`',' ','%','s','%','s','%','s','%','s',' ',0};
371 static const WCHAR size_fmt
[] = {'(','%','s',')',0};
372 static const WCHAR type_char
[] = {'C','H','A','R',0};
373 static const WCHAR type_int
[] = {'I','N','T',0};
374 static const WCHAR type_long
[] = {'L','O','N','G',0};
375 static const WCHAR type_object
[] = {'O','B','J','E','C','T',0};
376 static const WCHAR type_notnull
[] = {' ','N','O','T',' ','N','U','L','L',0};
377 static const WCHAR localizable
[] = {' ','L','O','C','A','L','I','Z','A','B','L','E',0};
379 columns
= msi_alloc_zero(sql_size
* sizeof(WCHAR
));
383 for (i
= 0; i
< num_columns
; i
++)
386 comma
[1] = size
[0] = extra
[0] = '\0';
388 if (i
== num_columns
- 1)
400 lstrcpyW(extra
, type_notnull
);
402 lstrcatW(extra
, localizable
);
404 sprintfW(size
, size_fmt
, ptr
);
407 lstrcpyW(extra
, type_notnull
);
410 sprintfW(size
, size_fmt
, ptr
);
413 lstrcpyW(extra
, type_notnull
);
421 WARN("invalid int width %u\n", len
);
427 lstrcpyW(extra
, type_notnull
);
432 ERR("Unknown type: %c\n", types
[i
][0]);
437 sprintfW(expanded
, column_fmt
, columns_data
[i
], type
, size
, extra
, comma
);
438 sql_size
+= lstrlenW(expanded
);
440 p
= msi_realloc(columns
, sql_size
* sizeof(WCHAR
));
448 lstrcatW(columns
, expanded
);
454 static LPWSTR
msi_build_createsql_postlude(LPWSTR
*primary_keys
, DWORD num_keys
)
456 LPWSTR postlude
, keys
, ptr
;
457 DWORD size
, key_size
, i
;
459 static const WCHAR key_fmt
[] = {'`','%','s','`',',',' ',0};
460 static const WCHAR postlude_fmt
[] = {'P','R','I','M','A','R','Y',' ','K','E','Y',' ','%','s',')',0};
462 for (i
= 0, size
= 1; i
< num_keys
; i
++)
463 size
+= lstrlenW(key_fmt
) + lstrlenW(primary_keys
[i
]) - 2;
465 keys
= msi_alloc(size
* sizeof(WCHAR
));
469 for (i
= 0, ptr
= keys
; i
< num_keys
; i
++)
471 key_size
= lstrlenW(key_fmt
) + lstrlenW(primary_keys
[i
]) -2;
472 sprintfW(ptr
, key_fmt
, primary_keys
[i
]);
476 /* remove final ', ' */
479 size
= lstrlenW(postlude_fmt
) + size
- 1;
480 postlude
= msi_alloc(size
* sizeof(WCHAR
));
484 sprintfW(postlude
, postlude_fmt
, keys
);
491 static UINT
msi_add_table_to_db(MSIDATABASE
*db
, LPWSTR
*columns
, LPWSTR
*types
, LPWSTR
*labels
, DWORD num_labels
, DWORD num_columns
)
497 LPWSTR prelude
, columns_sql
, postlude
;
499 prelude
= msi_build_createsql_prelude(labels
[0]);
500 columns_sql
= msi_build_createsql_columns(columns
, types
, num_columns
);
501 postlude
= msi_build_createsql_postlude(labels
+ 1, num_labels
- 1); /* skip over table name */
503 if (!prelude
|| !columns_sql
|| !postlude
)
504 return ERROR_OUTOFMEMORY
;
506 size
= lstrlenW(prelude
) + lstrlenW(columns_sql
) + lstrlenW(postlude
) + 1;
507 create_sql
= msi_alloc(size
* sizeof(WCHAR
));
509 return ERROR_OUTOFMEMORY
;
511 lstrcpyW(create_sql
, prelude
);
512 lstrcatW(create_sql
, columns_sql
);
513 lstrcatW(create_sql
, postlude
);
516 msi_free(columns_sql
);
519 r
= MSI_DatabaseOpenViewW( db
, create_sql
, &view
);
520 msi_free(create_sql
);
522 if (r
!= ERROR_SUCCESS
)
525 r
= MSI_ViewExecute(view
, NULL
);
527 msiobj_release(&view
->hdr
);
532 static LPWSTR
msi_import_stream_filename(LPCWSTR path
, LPCWSTR name
)
535 LPWSTR fullname
, ptr
;
537 len
= lstrlenW(path
) + lstrlenW(name
) + 1;
538 fullname
= msi_alloc(len
*sizeof(WCHAR
));
542 lstrcpyW( fullname
, path
);
544 /* chop off extension from path */
545 ptr
= strrchrW(fullname
, '.');
552 lstrcpyW( ptr
, name
);
556 static UINT
construct_record(DWORD num_columns
, LPWSTR
*types
,
557 LPWSTR
*data
, LPWSTR path
, MSIRECORD
**rec
)
561 *rec
= MSI_CreateRecord(num_columns
);
563 return ERROR_OUTOFMEMORY
;
565 for (i
= 0; i
< num_columns
; i
++)
569 case 'L': case 'l': case 'S': case 's':
570 MSI_RecordSetStringW(*rec
, i
+ 1, data
[i
]);
574 MSI_RecordSetInteger(*rec
, i
+ 1, atoiW(data
[i
]));
580 LPWSTR file
= msi_import_stream_filename(path
, data
[i
]);
582 return ERROR_FUNCTION_FAILED
;
584 r
= MSI_RecordSetStreamFromFileW(*rec
, i
+ 1, file
);
586 if (r
!= ERROR_SUCCESS
)
587 return ERROR_FUNCTION_FAILED
;
591 ERR("Unhandled column type: %c\n", types
[i
][0]);
592 msiobj_release(&(*rec
)->hdr
);
593 return ERROR_FUNCTION_FAILED
;
597 return ERROR_SUCCESS
;
600 static UINT
msi_add_records_to_table(MSIDATABASE
*db
, LPWSTR
*columns
, LPWSTR
*types
,
601 LPWSTR
*labels
, LPWSTR
**records
,
602 int num_columns
, int num_records
,
610 static const WCHAR select
[] = {
611 'S','E','L','E','C','T',' ','*',' ',
612 'F','R','O','M',' ','`','%','s','`',0
615 r
= MSI_OpenQuery(db
, &view
, select
, labels
[0]);
616 if (r
!= ERROR_SUCCESS
)
619 while (MSI_ViewFetch(view
, &rec
) != ERROR_NO_MORE_ITEMS
)
621 r
= MSI_ViewModify(view
, MSIMODIFY_DELETE
, rec
);
622 if (r
!= ERROR_SUCCESS
)
626 for (i
= 0; i
< num_records
; i
++)
628 r
= construct_record(num_columns
, types
, records
[i
], path
, &rec
);
629 if (r
!= ERROR_SUCCESS
)
632 r
= MSI_ViewModify(view
, MSIMODIFY_INSERT
, rec
);
633 if (r
!= ERROR_SUCCESS
)
635 msiobj_release(&rec
->hdr
);
639 msiobj_release(&rec
->hdr
);
643 msiobj_release(&view
->hdr
);
647 static UINT
MSI_DatabaseImport(MSIDATABASE
*db
, LPCWSTR folder
, LPCWSTR file
)
651 DWORD num_labels
, num_types
;
652 DWORD num_columns
, num_records
= 0;
653 LPWSTR
*columns
, *types
, *labels
;
654 LPWSTR path
, ptr
, data
;
655 LPWSTR
**records
= NULL
;
656 LPWSTR
**temp_records
;
658 static const WCHAR suminfo
[] =
659 {'_','S','u','m','m','a','r','y','I','n','f','o','r','m','a','t','i','o','n',0};
661 TRACE("%p %s %s\n", db
, debugstr_w(folder
), debugstr_w(file
) );
663 if( folder
== NULL
|| file
== NULL
)
664 return ERROR_INVALID_PARAMETER
;
666 len
= lstrlenW(folder
) + lstrlenW(szBackSlash
) + lstrlenW(file
) + 1;
667 path
= msi_alloc( len
* sizeof(WCHAR
) );
669 return ERROR_OUTOFMEMORY
;
671 lstrcpyW( path
, folder
);
672 lstrcatW( path
, szBackSlash
);
673 lstrcatW( path
, file
);
675 data
= msi_read_text_archive( path
);
678 msi_parse_line( &ptr
, &columns
, &num_columns
);
679 msi_parse_line( &ptr
, &types
, &num_types
);
680 msi_parse_line( &ptr
, &labels
, &num_labels
);
682 if (num_columns
!= num_types
)
684 r
= ERROR_FUNCTION_FAILED
;
688 records
= msi_alloc(sizeof(LPWSTR
*));
691 r
= ERROR_OUTOFMEMORY
;
695 /* read in the table records */
698 msi_parse_line( &ptr
, &records
[num_records
], NULL
);
701 temp_records
= msi_realloc(records
, (num_records
+ 1) * sizeof(LPWSTR
*));
704 r
= ERROR_OUTOFMEMORY
;
707 records
= temp_records
;
710 if (!strcmpW(labels
[0], suminfo
))
712 r
= msi_add_suminfo( db
, records
, num_records
, num_columns
);
713 if (r
!= ERROR_SUCCESS
)
715 r
= ERROR_FUNCTION_FAILED
;
721 if (!TABLE_Exists(db
, labels
[0]))
723 r
= msi_add_table_to_db( db
, columns
, types
, labels
, num_labels
, num_columns
);
724 if (r
!= ERROR_SUCCESS
)
726 r
= ERROR_FUNCTION_FAILED
;
731 r
= msi_add_records_to_table( db
, columns
, types
, labels
, records
, num_columns
, num_records
, path
);
741 for (i
= 0; i
< num_records
; i
++)
742 msi_free(records
[i
]);
749 UINT WINAPI
MsiDatabaseImportW(MSIHANDLE handle
, LPCWSTR szFolder
, LPCWSTR szFilename
)
754 TRACE("%x %s %s\n",handle
,debugstr_w(szFolder
), debugstr_w(szFilename
));
756 db
= msihandle2msiinfo( handle
, MSIHANDLETYPE_DATABASE
);
759 IWineMsiRemoteDatabase
*remote_database
;
761 remote_database
= (IWineMsiRemoteDatabase
*)msi_get_remote( handle
);
762 if ( !remote_database
)
763 return ERROR_INVALID_HANDLE
;
765 IWineMsiRemoteDatabase_Release( remote_database
);
766 WARN("MsiDatabaseImport not allowed during a custom action!\n");
768 return ERROR_SUCCESS
;
771 r
= MSI_DatabaseImport( db
, szFolder
, szFilename
);
772 msiobj_release( &db
->hdr
);
776 UINT WINAPI
MsiDatabaseImportA( MSIHANDLE handle
,
777 LPCSTR szFolder
, LPCSTR szFilename
)
779 LPWSTR path
= NULL
, file
= NULL
;
780 UINT r
= ERROR_OUTOFMEMORY
;
782 TRACE("%x %s %s\n", handle
, debugstr_a(szFolder
), debugstr_a(szFilename
));
786 path
= strdupAtoW( szFolder
);
793 file
= strdupAtoW( szFilename
);
798 r
= MsiDatabaseImportW( handle
, path
, file
);
807 static UINT
msi_export_record( HANDLE handle
, MSIRECORD
*row
, UINT start
)
809 UINT i
, count
, len
, r
= ERROR_SUCCESS
;
815 buffer
= msi_alloc( len
);
817 return ERROR_OUTOFMEMORY
;
819 count
= MSI_RecordGetFieldCount( row
);
820 for ( i
=start
; i
<=count
; i
++ )
823 r
= MSI_RecordGetStringA( row
, i
, buffer
, &sz
);
824 if (r
== ERROR_MORE_DATA
)
826 char *p
= msi_realloc( buffer
, sz
+ 1 );
833 r
= MSI_RecordGetStringA( row
, i
, buffer
, &sz
);
834 if (r
!= ERROR_SUCCESS
)
837 if (!WriteFile( handle
, buffer
, sz
, &sz
, NULL
))
839 r
= ERROR_FUNCTION_FAILED
;
843 sep
= (i
< count
) ? "\t" : "\r\n";
844 if (!WriteFile( handle
, sep
, strlen(sep
), &sz
, NULL
))
846 r
= ERROR_FUNCTION_FAILED
;
854 static UINT
msi_export_row( MSIRECORD
*row
, void *arg
)
856 return msi_export_record( arg
, row
, 1 );
859 static UINT
msi_export_forcecodepage( HANDLE handle
)
863 static const char data
[] = "\r\n\r\n0\t_ForceCodepage\r\n";
865 FIXME("Read the codepage from the strings table!\n");
867 sz
= lstrlenA(data
) + 1;
868 if (!WriteFile(handle
, data
, sz
, &sz
, NULL
))
869 return ERROR_FUNCTION_FAILED
;
871 return ERROR_SUCCESS
;
874 static UINT
MSI_DatabaseExport( MSIDATABASE
*db
, LPCWSTR table
,
875 LPCWSTR folder
, LPCWSTR file
)
877 static const WCHAR query
[] = {
878 's','e','l','e','c','t',' ','*',' ','f','r','o','m',' ','%','s',0 };
879 static const WCHAR forcecodepage
[] = {
880 '_','F','o','r','c','e','C','o','d','e','p','a','g','e',0 };
881 MSIRECORD
*rec
= NULL
;
882 MSIQUERY
*view
= NULL
;
887 TRACE("%p %s %s %s\n", db
, debugstr_w(table
),
888 debugstr_w(folder
), debugstr_w(file
) );
890 if( folder
== NULL
|| file
== NULL
)
891 return ERROR_INVALID_PARAMETER
;
893 len
= lstrlenW(folder
) + lstrlenW(file
) + 2;
894 filename
= msi_alloc(len
* sizeof (WCHAR
));
896 return ERROR_OUTOFMEMORY
;
898 lstrcpyW( filename
, folder
);
899 lstrcatW( filename
, szBackSlash
);
900 lstrcatW( filename
, file
);
902 handle
= CreateFileW( filename
, GENERIC_READ
| GENERIC_WRITE
, 0,
903 NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
904 msi_free( filename
);
905 if (handle
== INVALID_HANDLE_VALUE
)
906 return ERROR_FUNCTION_FAILED
;
908 if (!lstrcmpW( table
, forcecodepage
))
910 r
= msi_export_forcecodepage( handle
);
914 r
= MSI_OpenQuery( db
, &view
, query
, table
);
915 if (r
== ERROR_SUCCESS
)
917 /* write out row 1, the column names */
918 r
= MSI_ViewGetColumnInfo(view
, MSICOLINFO_NAMES
, &rec
);
919 if (r
== ERROR_SUCCESS
)
921 msi_export_record( handle
, rec
, 1 );
922 msiobj_release( &rec
->hdr
);
925 /* write out row 2, the column types */
926 r
= MSI_ViewGetColumnInfo(view
, MSICOLINFO_TYPES
, &rec
);
927 if (r
== ERROR_SUCCESS
)
929 msi_export_record( handle
, rec
, 1 );
930 msiobj_release( &rec
->hdr
);
933 /* write out row 3, the table name + keys */
934 r
= MSI_DatabaseGetPrimaryKeys( db
, table
, &rec
);
935 if (r
== ERROR_SUCCESS
)
937 MSI_RecordSetStringW( rec
, 0, table
);
938 msi_export_record( handle
, rec
, 0 );
939 msiobj_release( &rec
->hdr
);
942 /* write out row 4 onwards, the data */
943 r
= MSI_IterateRecords( view
, 0, msi_export_row
, handle
);
944 msiobj_release( &view
->hdr
);
948 CloseHandle( handle
);
952 /***********************************************************************
953 * MsiExportDatabaseW [MSI.@]
955 * Writes a file containing the table data as tab separated ASCII.
957 * The format is as follows:
959 * row1 : colname1 <tab> colname2 <tab> .... colnameN <cr> <lf>
960 * row2 : coltype1 <tab> coltype2 <tab> .... coltypeN <cr> <lf>
961 * row3 : tablename <tab> key1 <tab> key2 <tab> ... keyM <cr> <lf>
963 * Followed by the data, starting at row 1 with one row per line
965 * row4 : data <tab> data <tab> data <tab> ... data <cr> <lf>
967 UINT WINAPI
MsiDatabaseExportW( MSIHANDLE handle
, LPCWSTR szTable
,
968 LPCWSTR szFolder
, LPCWSTR szFilename
)
973 TRACE("%x %s %s %s\n", handle
, debugstr_w(szTable
),
974 debugstr_w(szFolder
), debugstr_w(szFilename
));
976 db
= msihandle2msiinfo( handle
, MSIHANDLETYPE_DATABASE
);
979 IWineMsiRemoteDatabase
*remote_database
;
981 remote_database
= (IWineMsiRemoteDatabase
*)msi_get_remote( handle
);
982 if ( !remote_database
)
983 return ERROR_INVALID_HANDLE
;
985 IWineMsiRemoteDatabase_Release( remote_database
);
986 WARN("MsiDatabaseExport not allowed during a custom action!\n");
988 return ERROR_SUCCESS
;
991 r
= MSI_DatabaseExport( db
, szTable
, szFolder
, szFilename
);
992 msiobj_release( &db
->hdr
);
996 UINT WINAPI
MsiDatabaseExportA( MSIHANDLE handle
, LPCSTR szTable
,
997 LPCSTR szFolder
, LPCSTR szFilename
)
999 LPWSTR path
= NULL
, file
= NULL
, table
= NULL
;
1000 UINT r
= ERROR_OUTOFMEMORY
;
1002 TRACE("%x %s %s %s\n", handle
, debugstr_a(szTable
),
1003 debugstr_a(szFolder
), debugstr_a(szFilename
));
1007 table
= strdupAtoW( szTable
);
1014 path
= strdupAtoW( szFolder
);
1021 file
= strdupAtoW( szFilename
);
1026 r
= MsiDatabaseExportW( handle
, table
, path
, file
);
1036 UINT WINAPI
MsiDatabaseMergeA(MSIHANDLE hDatabase
, MSIHANDLE hDatabaseMerge
,
1042 TRACE("(%d, %d, %s)\n", hDatabase
, hDatabaseMerge
,
1043 debugstr_a(szTableName
));
1045 table
= strdupAtoW(szTableName
);
1046 r
= MsiDatabaseMergeW(hDatabase
, hDatabaseMerge
, table
);
1052 typedef struct _tagMERGETABLE
1066 typedef struct _tagMERGEROW
1072 typedef struct _tagMERGEDATA
1076 MERGETABLE
*curtable
;
1078 struct list
*tabledata
;
1081 static UINT
merge_verify_colnames(MSIQUERY
*dbview
, MSIQUERY
*mergeview
)
1083 MSIRECORD
*dbrec
, *mergerec
;
1086 r
= MSI_ViewGetColumnInfo(dbview
, MSICOLINFO_NAMES
, &dbrec
);
1087 if (r
!= ERROR_SUCCESS
)
1090 r
= MSI_ViewGetColumnInfo(mergeview
, MSICOLINFO_NAMES
, &mergerec
);
1091 if (r
!= ERROR_SUCCESS
)
1094 count
= MSI_RecordGetFieldCount(dbrec
);
1095 for (i
= 1; i
<= count
; i
++)
1097 if (!MSI_RecordGetString(mergerec
, i
))
1100 if (lstrcmpW(MSI_RecordGetString(dbrec
, i
),
1101 MSI_RecordGetString(mergerec
, i
)))
1103 r
= ERROR_DATATYPE_MISMATCH
;
1108 msiobj_release(&dbrec
->hdr
);
1109 msiobj_release(&mergerec
->hdr
);
1110 dbrec
= mergerec
= NULL
;
1112 r
= MSI_ViewGetColumnInfo(dbview
, MSICOLINFO_TYPES
, &dbrec
);
1113 if (r
!= ERROR_SUCCESS
)
1116 r
= MSI_ViewGetColumnInfo(mergeview
, MSICOLINFO_TYPES
, &mergerec
);
1117 if (r
!= ERROR_SUCCESS
)
1120 count
= MSI_RecordGetFieldCount(dbrec
);
1121 for (i
= 1; i
<= count
; i
++)
1123 if (!MSI_RecordGetString(mergerec
, i
))
1126 if (lstrcmpW(MSI_RecordGetString(dbrec
, i
),
1127 MSI_RecordGetString(mergerec
, i
)))
1129 r
= ERROR_DATATYPE_MISMATCH
;
1135 msiobj_release(&dbrec
->hdr
);
1136 msiobj_release(&mergerec
->hdr
);
1141 static UINT
merge_verify_primary_keys(MSIDATABASE
*db
, MSIDATABASE
*mergedb
,
1144 MSIRECORD
*dbrec
, *mergerec
= NULL
;
1147 r
= MSI_DatabaseGetPrimaryKeys(db
, table
, &dbrec
);
1148 if (r
!= ERROR_SUCCESS
)
1151 r
= MSI_DatabaseGetPrimaryKeys(mergedb
, table
, &mergerec
);
1152 if (r
!= ERROR_SUCCESS
)
1155 count
= MSI_RecordGetFieldCount(dbrec
);
1156 if (count
!= MSI_RecordGetFieldCount(mergerec
))
1158 r
= ERROR_DATATYPE_MISMATCH
;
1162 for (i
= 1; i
<= count
; i
++)
1164 if (lstrcmpW(MSI_RecordGetString(dbrec
, i
),
1165 MSI_RecordGetString(mergerec
, i
)))
1167 r
= ERROR_DATATYPE_MISMATCH
;
1173 msiobj_release(&dbrec
->hdr
);
1174 msiobj_release(&mergerec
->hdr
);
1179 static LPWSTR
get_key_value(MSIQUERY
*view
, LPCWSTR key
, MSIRECORD
*rec
)
1181 MSIRECORD
*colnames
;
1183 UINT r
, i
= 0, sz
= 0;
1186 r
= MSI_ViewGetColumnInfo(view
, MSICOLINFO_NAMES
, &colnames
);
1187 if (r
!= ERROR_SUCCESS
)
1192 str
= msi_dup_record_field(colnames
, ++i
);
1193 cmp
= lstrcmpW(key
, str
);
1197 msiobj_release(&colnames
->hdr
);
1199 r
= MSI_RecordGetStringW(rec
, i
, NULL
, &sz
);
1200 if (r
!= ERROR_SUCCESS
)
1204 if (MSI_RecordGetString(rec
, i
)) /* check record field is a string */
1206 /* quote string record fields */
1207 const WCHAR szQuote
[] = {'\'', 0};
1209 val
= msi_alloc(sz
*sizeof(WCHAR
));
1213 lstrcpyW(val
, szQuote
);
1214 r
= MSI_RecordGetStringW(rec
, i
, val
+1, &sz
);
1215 lstrcpyW(val
+1+sz
, szQuote
);
1219 /* do not quote integer record fields */
1220 val
= msi_alloc(sz
*sizeof(WCHAR
));
1224 r
= MSI_RecordGetStringW(rec
, i
, val
, &sz
);
1227 if (r
!= ERROR_SUCCESS
)
1229 ERR("failed to get string!\n");
1237 static LPWSTR
create_diff_row_query(MSIDATABASE
*merge
, MSIQUERY
*view
,
1238 LPWSTR table
, MSIRECORD
*rec
)
1240 LPWSTR query
= NULL
, clause
= NULL
;
1241 LPWSTR ptr
= NULL
, val
;
1243 DWORD size
= 1, oldsize
;
1248 static const WCHAR keyset
[] = {
1249 '`','%','s','`',' ','=',' ','%','s',' ','A','N','D',' ',0};
1250 static const WCHAR lastkeyset
[] = {
1251 '`','%','s','`',' ','=',' ','%','s',' ',0};
1252 static const WCHAR fmt
[] = {'S','E','L','E','C','T',' ','*',' ',
1253 'F','R','O','M',' ','`','%','s','`',' ',
1254 'W','H','E','R','E',' ','%','s',0};
1256 r
= MSI_DatabaseGetPrimaryKeys(merge
, table
, &keys
);
1257 if (r
!= ERROR_SUCCESS
)
1260 clause
= msi_alloc_zero(size
* sizeof(WCHAR
));
1265 count
= MSI_RecordGetFieldCount(keys
);
1266 for (i
= 1; i
<= count
; i
++)
1268 key
= MSI_RecordGetString(keys
, i
);
1269 val
= get_key_value(view
, key
, rec
);
1272 setptr
= lastkeyset
;
1277 size
+= lstrlenW(setptr
) + lstrlenW(key
) + lstrlenW(val
) - 4;
1278 clause
= msi_realloc(clause
, size
* sizeof (WCHAR
));
1285 ptr
= clause
+ oldsize
- 1;
1286 sprintfW(ptr
, setptr
, key
, val
);
1290 size
= lstrlenW(fmt
) + lstrlenW(table
) + lstrlenW(clause
) + 1;
1291 query
= msi_alloc(size
* sizeof(WCHAR
));
1295 sprintfW(query
, fmt
, table
, clause
);
1299 msiobj_release(&keys
->hdr
);
1303 static UINT
merge_diff_row(MSIRECORD
*rec
, LPVOID param
)
1305 MERGEDATA
*data
= param
;
1306 MERGETABLE
*table
= data
->curtable
;
1308 MSIQUERY
*dbview
= NULL
;
1309 MSIRECORD
*row
= NULL
;
1310 LPWSTR query
= NULL
;
1311 UINT r
= ERROR_SUCCESS
;
1313 if (TABLE_Exists(data
->db
, table
->name
))
1315 query
= create_diff_row_query(data
->merge
, data
->curview
, table
->name
, rec
);
1317 return ERROR_OUTOFMEMORY
;
1319 r
= MSI_DatabaseOpenViewW(data
->db
, query
, &dbview
);
1320 if (r
!= ERROR_SUCCESS
)
1323 r
= MSI_ViewExecute(dbview
, NULL
);
1324 if (r
!= ERROR_SUCCESS
)
1327 r
= MSI_ViewFetch(dbview
, &row
);
1328 if (r
== ERROR_SUCCESS
&& !MSI_RecordsAreEqual(rec
, row
))
1330 table
->numconflicts
++;
1333 else if (r
!= ERROR_NO_MORE_ITEMS
)
1337 mergerow
= msi_alloc(sizeof(MERGEROW
));
1340 r
= ERROR_OUTOFMEMORY
;
1344 mergerow
->data
= MSI_CloneRecord(rec
);
1345 if (!mergerow
->data
)
1347 r
= ERROR_OUTOFMEMORY
;
1352 list_add_tail(&table
->rows
, &mergerow
->entry
);
1356 msiobj_release(&row
->hdr
);
1357 msiobj_release(&dbview
->hdr
);
1361 static UINT
msi_get_table_labels(MSIDATABASE
*db
, LPCWSTR table
, LPWSTR
**labels
, DWORD
*numlabels
)
1364 MSIRECORD
*prec
= NULL
;
1366 r
= MSI_DatabaseGetPrimaryKeys(db
, table
, &prec
);
1367 if (r
!= ERROR_SUCCESS
)
1370 count
= MSI_RecordGetFieldCount(prec
);
1371 *numlabels
= count
+ 1;
1372 *labels
= msi_alloc((*numlabels
)*sizeof(LPWSTR
));
1375 r
= ERROR_OUTOFMEMORY
;
1379 (*labels
)[0] = strdupW(table
);
1380 for (i
=1; i
<=count
; i
++ )
1382 (*labels
)[i
] = strdupW(MSI_RecordGetString(prec
, i
));
1386 msiobj_release( &prec
->hdr
);
1390 static UINT
msi_get_query_columns(MSIQUERY
*query
, LPWSTR
**columns
, DWORD
*numcolumns
)
1393 MSIRECORD
*prec
= NULL
;
1395 r
= MSI_ViewGetColumnInfo(query
, MSICOLINFO_NAMES
, &prec
);
1396 if (r
!= ERROR_SUCCESS
)
1399 count
= MSI_RecordGetFieldCount(prec
);
1400 *columns
= msi_alloc(count
*sizeof(LPWSTR
));
1403 r
= ERROR_OUTOFMEMORY
;
1407 for (i
=1; i
<=count
; i
++ )
1409 (*columns
)[i
-1] = strdupW(MSI_RecordGetString(prec
, i
));
1412 *numcolumns
= count
;
1415 msiobj_release( &prec
->hdr
);
1419 static UINT
msi_get_query_types(MSIQUERY
*query
, LPWSTR
**types
, DWORD
*numtypes
)
1422 MSIRECORD
*prec
= NULL
;
1424 r
= MSI_ViewGetColumnInfo(query
, MSICOLINFO_TYPES
, &prec
);
1425 if (r
!= ERROR_SUCCESS
)
1428 count
= MSI_RecordGetFieldCount(prec
);
1429 *types
= msi_alloc(count
*sizeof(LPWSTR
));
1432 r
= ERROR_OUTOFMEMORY
;
1436 for (i
=1; i
<=count
; i
++ )
1438 (*types
)[i
-1] = strdupW(MSI_RecordGetString(prec
, i
));
1442 msiobj_release( &prec
->hdr
);
1446 static void merge_free_rows(MERGETABLE
*table
)
1448 struct list
*item
, *cursor
;
1450 LIST_FOR_EACH_SAFE(item
, cursor
, &table
->rows
)
1452 MERGEROW
*row
= LIST_ENTRY(item
, MERGEROW
, entry
);
1454 list_remove(&row
->entry
);
1455 msiobj_release(&row
->data
->hdr
);
1460 static void free_merge_table(MERGETABLE
*table
)
1464 if (table
->labels
!= NULL
)
1466 for (i
= 0; i
< table
->numlabels
; i
++)
1467 msi_free(table
->labels
[i
]);
1468 msi_free(table
->labels
);
1471 if (table
->columns
!= NULL
)
1473 for (i
= 0; i
< table
->numcolumns
; i
++)
1474 msi_free(table
->columns
[i
]);
1475 msi_free(table
->columns
);
1478 if (table
->types
!= NULL
)
1480 for (i
= 0; i
< table
->numtypes
; i
++)
1481 msi_free(table
->types
[i
]);
1482 msi_free(table
->types
);
1484 msi_free(table
->name
);
1485 merge_free_rows(table
);
1490 static UINT
msi_get_merge_table (MSIDATABASE
*db
, LPCWSTR name
, MERGETABLE
**ptable
)
1494 MSIQUERY
*mergeview
= NULL
;
1496 static const WCHAR query
[] = {'S','E','L','E','C','T',' ','*',' ',
1497 'F','R','O','M',' ','`','%','s','`',0};
1499 table
= msi_alloc_zero(sizeof(MERGETABLE
));
1503 return ERROR_OUTOFMEMORY
;
1506 r
= msi_get_table_labels(db
, name
, &table
->labels
, &table
->numlabels
);
1507 if (r
!= ERROR_SUCCESS
)
1510 r
= MSI_OpenQuery(db
, &mergeview
, query
, name
);
1511 if (r
!= ERROR_SUCCESS
)
1514 r
= msi_get_query_columns(mergeview
, &table
->columns
, &table
->numcolumns
);
1515 if (r
!= ERROR_SUCCESS
)
1518 r
= msi_get_query_types(mergeview
, &table
->types
, &table
->numtypes
);
1519 if (r
!= ERROR_SUCCESS
)
1522 list_init(&table
->rows
);
1524 table
->name
= strdupW(name
);
1525 table
->numconflicts
= 0;
1527 msiobj_release(&mergeview
->hdr
);
1529 return ERROR_SUCCESS
;
1532 msiobj_release(&mergeview
->hdr
);
1533 free_merge_table(table
);
1538 static UINT
merge_diff_tables(MSIRECORD
*rec
, LPVOID param
)
1540 MERGEDATA
*data
= param
;
1542 MSIQUERY
*dbview
= NULL
;
1543 MSIQUERY
*mergeview
= NULL
;
1547 static const WCHAR query
[] = {'S','E','L','E','C','T',' ','*',' ',
1548 'F','R','O','M',' ','`','%','s','`',0};
1550 name
= MSI_RecordGetString(rec
, 1);
1552 r
= MSI_OpenQuery(data
->merge
, &mergeview
, query
, name
);
1553 if (r
!= ERROR_SUCCESS
)
1556 if (TABLE_Exists(data
->db
, name
))
1558 r
= MSI_OpenQuery(data
->db
, &dbview
, query
, name
);
1559 if (r
!= ERROR_SUCCESS
)
1562 r
= merge_verify_colnames(dbview
, mergeview
);
1563 if (r
!= ERROR_SUCCESS
)
1566 r
= merge_verify_primary_keys(data
->db
, data
->merge
, name
);
1567 if (r
!= ERROR_SUCCESS
)
1571 r
= msi_get_merge_table(data
->merge
, name
, &table
);
1572 if (r
!= ERROR_SUCCESS
)
1575 data
->curtable
= table
;
1576 data
->curview
= mergeview
;
1577 r
= MSI_IterateRecords(mergeview
, NULL
, merge_diff_row
, data
);
1578 if (r
!= ERROR_SUCCESS
)
1580 free_merge_table(table
);
1584 list_add_tail(data
->tabledata
, &table
->entry
);
1587 msiobj_release(&dbview
->hdr
);
1588 msiobj_release(&mergeview
->hdr
);
1592 static UINT
gather_merge_data(MSIDATABASE
*db
, MSIDATABASE
*merge
,
1593 struct list
*tabledata
)
1599 static const WCHAR query
[] = {'S','E','L','E','C','T',' ','*',' ',
1600 'F','R','O','M',' ','`','_','T','a','b','l','e','s','`',0};
1602 r
= MSI_DatabaseOpenViewW(merge
, query
, &view
);
1603 if (r
!= ERROR_SUCCESS
)
1608 data
.tabledata
= tabledata
;
1609 r
= MSI_IterateRecords(view
, NULL
, merge_diff_tables
, &data
);
1611 msiobj_release(&view
->hdr
);
1615 static UINT
merge_table(MSIDATABASE
*db
, MERGETABLE
*table
)
1621 if (!TABLE_Exists(db
, table
->name
))
1623 r
= msi_add_table_to_db(db
, table
->columns
, table
->types
,
1624 table
->labels
, table
->numlabels
, table
->numcolumns
);
1625 if (r
!= ERROR_SUCCESS
)
1626 return ERROR_FUNCTION_FAILED
;
1629 LIST_FOR_EACH_ENTRY(row
, &table
->rows
, MERGEROW
, entry
)
1631 r
= TABLE_CreateView(db
, table
->name
, &tv
);
1632 if (r
!= ERROR_SUCCESS
)
1635 r
= tv
->ops
->insert_row(tv
, row
->data
, -1, FALSE
);
1636 tv
->ops
->delete(tv
);
1638 if (r
!= ERROR_SUCCESS
)
1642 return ERROR_SUCCESS
;
1645 static UINT
update_merge_errors(MSIDATABASE
*db
, LPCWSTR error
,
1646 LPWSTR table
, DWORD numconflicts
)
1651 static const WCHAR create
[] = {
1652 'C','R','E','A','T','E',' ','T','A','B','L','E',' ',
1653 '`','%','s','`',' ','(','`','T','a','b','l','e','`',' ',
1654 'C','H','A','R','(','2','5','5',')',' ','N','O','T',' ',
1655 'N','U','L','L',',',' ','`','N','u','m','R','o','w','M','e','r','g','e',
1656 'C','o','n','f','l','i','c','t','s','`',' ','S','H','O','R','T',' ',
1657 'N','O','T',' ','N','U','L','L',' ','P','R','I','M','A','R','Y',' ',
1658 'K','E','Y',' ','`','T','a','b','l','e','`',')',0};
1659 static const WCHAR insert
[] = {
1660 'I','N','S','E','R','T',' ','I','N','T','O',' ',
1661 '`','%','s','`',' ','(','`','T','a','b','l','e','`',',',' ',
1662 '`','N','u','m','R','o','w','M','e','r','g','e',
1663 'C','o','n','f','l','i','c','t','s','`',')',' ','V','A','L','U','E','S',
1664 ' ','(','\'','%','s','\'',',',' ','%','d',')',0};
1666 if (!TABLE_Exists(db
, error
))
1668 r
= MSI_OpenQuery(db
, &view
, create
, error
);
1669 if (r
!= ERROR_SUCCESS
)
1672 r
= MSI_ViewExecute(view
, NULL
);
1673 msiobj_release(&view
->hdr
);
1674 if (r
!= ERROR_SUCCESS
)
1678 r
= MSI_OpenQuery(db
, &view
, insert
, error
, table
, numconflicts
);
1679 if (r
!= ERROR_SUCCESS
)
1682 r
= MSI_ViewExecute(view
, NULL
);
1683 msiobj_release(&view
->hdr
);
1687 UINT WINAPI
MsiDatabaseMergeW(MSIHANDLE hDatabase
, MSIHANDLE hDatabaseMerge
,
1688 LPCWSTR szTableName
)
1690 struct list tabledata
= LIST_INIT(tabledata
);
1691 struct list
*item
, *cursor
;
1692 MSIDATABASE
*db
, *merge
;
1697 TRACE("(%d, %d, %s)\n", hDatabase
, hDatabaseMerge
,
1698 debugstr_w(szTableName
));
1700 if (szTableName
&& !*szTableName
)
1701 return ERROR_INVALID_TABLE
;
1703 db
= msihandle2msiinfo(hDatabase
, MSIHANDLETYPE_DATABASE
);
1704 merge
= msihandle2msiinfo(hDatabaseMerge
, MSIHANDLETYPE_DATABASE
);
1707 r
= ERROR_INVALID_HANDLE
;
1711 r
= gather_merge_data(db
, merge
, &tabledata
);
1712 if (r
!= ERROR_SUCCESS
)
1716 LIST_FOR_EACH_ENTRY(table
, &tabledata
, MERGETABLE
, entry
)
1718 if (table
->numconflicts
)
1722 r
= update_merge_errors(db
, szTableName
, table
->name
,
1723 table
->numconflicts
);
1724 if (r
!= ERROR_SUCCESS
)
1729 r
= merge_table(db
, table
);
1730 if (r
!= ERROR_SUCCESS
)
1735 LIST_FOR_EACH_SAFE(item
, cursor
, &tabledata
)
1737 MERGETABLE
*table
= LIST_ENTRY(item
, MERGETABLE
, entry
);
1739 list_remove(&table
->entry
);
1740 free_merge_table(table
);
1744 r
= ERROR_FUNCTION_FAILED
;
1747 msiobj_release(&db
->hdr
);
1748 msiobj_release(&merge
->hdr
);
1752 MSIDBSTATE WINAPI
MsiGetDatabaseState( MSIHANDLE handle
)
1754 MSIDBSTATE ret
= MSIDBSTATE_READ
;
1757 TRACE("%d\n", handle
);
1759 db
= msihandle2msiinfo( handle
, MSIHANDLETYPE_DATABASE
);
1762 IWineMsiRemoteDatabase
*remote_database
;
1764 remote_database
= (IWineMsiRemoteDatabase
*)msi_get_remote( handle
);
1765 if ( !remote_database
)
1766 return MSIDBSTATE_ERROR
;
1768 IWineMsiRemoteDatabase_Release( remote_database
);
1769 WARN("MsiGetDatabaseState not allowed during a custom action!\n");
1771 return MSIDBSTATE_READ
;
1774 if (db
->mode
!= MSIDBOPEN_READONLY
)
1775 ret
= MSIDBSTATE_WRITE
;
1776 msiobj_release( &db
->hdr
);
1781 typedef struct _msi_remote_database_impl
{
1782 const IWineMsiRemoteDatabaseVtbl
*lpVtbl
;
1785 } msi_remote_database_impl
;
1787 static inline msi_remote_database_impl
* mrd_from_IWineMsiRemoteDatabase( IWineMsiRemoteDatabase
* iface
)
1789 return (msi_remote_database_impl
*)iface
;
1792 static HRESULT WINAPI
mrd_QueryInterface( IWineMsiRemoteDatabase
*iface
,
1793 REFIID riid
,LPVOID
*ppobj
)
1795 if( IsEqualCLSID( riid
, &IID_IUnknown
) ||
1796 IsEqualCLSID( riid
, &IID_IWineMsiRemoteDatabase
) )
1798 IUnknown_AddRef( iface
);
1803 return E_NOINTERFACE
;
1806 static ULONG WINAPI
mrd_AddRef( IWineMsiRemoteDatabase
*iface
)
1808 msi_remote_database_impl
* This
= mrd_from_IWineMsiRemoteDatabase( iface
);
1810 return InterlockedIncrement( &This
->refs
);
1813 static ULONG WINAPI
mrd_Release( IWineMsiRemoteDatabase
*iface
)
1815 msi_remote_database_impl
* This
= mrd_from_IWineMsiRemoteDatabase( iface
);
1818 r
= InterlockedDecrement( &This
->refs
);
1821 MsiCloseHandle( This
->database
);
1827 static HRESULT WINAPI
mrd_IsTablePersistent( IWineMsiRemoteDatabase
*iface
,
1828 BSTR table
, MSICONDITION
*persistent
)
1830 msi_remote_database_impl
*This
= mrd_from_IWineMsiRemoteDatabase( iface
);
1831 *persistent
= MsiDatabaseIsTablePersistentW(This
->database
, table
);
1835 static HRESULT WINAPI
mrd_GetPrimaryKeys( IWineMsiRemoteDatabase
*iface
,
1836 BSTR table
, MSIHANDLE
*keys
)
1838 msi_remote_database_impl
*This
= mrd_from_IWineMsiRemoteDatabase( iface
);
1839 UINT r
= MsiDatabaseGetPrimaryKeysW(This
->database
, table
, keys
);
1840 return HRESULT_FROM_WIN32(r
);
1843 static HRESULT WINAPI
mrd_GetSummaryInformation( IWineMsiRemoteDatabase
*iface
,
1844 UINT updatecount
, MSIHANDLE
*suminfo
)
1846 msi_remote_database_impl
*This
= mrd_from_IWineMsiRemoteDatabase( iface
);
1847 UINT r
= MsiGetSummaryInformationW(This
->database
, NULL
, updatecount
, suminfo
);
1848 return HRESULT_FROM_WIN32(r
);
1851 static HRESULT WINAPI
mrd_OpenView( IWineMsiRemoteDatabase
*iface
,
1852 BSTR query
, MSIHANDLE
*view
)
1854 msi_remote_database_impl
*This
= mrd_from_IWineMsiRemoteDatabase( iface
);
1855 UINT r
= MsiDatabaseOpenViewW(This
->database
, query
, view
);
1856 return HRESULT_FROM_WIN32(r
);
1859 static HRESULT WINAPI
mrd_SetMsiHandle( IWineMsiRemoteDatabase
*iface
, MSIHANDLE handle
)
1861 msi_remote_database_impl
* This
= mrd_from_IWineMsiRemoteDatabase( iface
);
1862 This
->database
= handle
;
1866 static const IWineMsiRemoteDatabaseVtbl msi_remote_database_vtbl
=
1871 mrd_IsTablePersistent
,
1873 mrd_GetSummaryInformation
,
1878 HRESULT
create_msi_remote_database( IUnknown
*pOuter
, LPVOID
*ppObj
)
1880 msi_remote_database_impl
*This
;
1882 This
= msi_alloc( sizeof *This
);
1884 return E_OUTOFMEMORY
;
1886 This
->lpVtbl
= &msi_remote_database_vtbl
;