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 #define IS_INTMSIDBOPEN(x) (((ULONG_PTR)(x) >> 16) == 0)
56 typedef struct tagMSITRANSFORM
{
61 typedef struct tagMSISTREAM
{
66 static UINT
find_open_stream( MSIDATABASE
*db
, LPCWSTR name
, IStream
**stm
)
70 LIST_FOR_EACH_ENTRY( stream
, &db
->streams
, MSISTREAM
, entry
)
75 r
= IStream_Stat( stream
->stm
, &stat
, 0 );
78 WARN("failed to stat stream r = %08x!\n", r
);
82 if( !lstrcmpW( name
, stat
.pwcsName
) )
84 TRACE("found %s\n", debugstr_w(name
));
86 CoTaskMemFree( stat
.pwcsName
);
90 CoTaskMemFree( stat
.pwcsName
);
93 return ERROR_FUNCTION_FAILED
;
96 static UINT
clone_open_stream( MSIDATABASE
*db
, LPCWSTR name
, IStream
**stm
)
100 if (find_open_stream( db
, name
, &stream
) == ERROR_SUCCESS
)
105 r
= IStream_Clone( stream
, stm
);
108 WARN("failed to clone stream r = %08x!\n", r
);
109 return ERROR_FUNCTION_FAILED
;
113 r
= IStream_Seek( *stm
, pos
, STREAM_SEEK_SET
, NULL
);
116 IStream_Release( *stm
);
117 return ERROR_FUNCTION_FAILED
;
120 return ERROR_SUCCESS
;
123 return ERROR_FUNCTION_FAILED
;
126 UINT
db_get_raw_stream( MSIDATABASE
*db
, LPCWSTR stname
, IStream
**stm
)
129 WCHAR decoded
[MAX_STREAM_NAME_LEN
];
131 decode_streamname( stname
, decoded
);
132 TRACE("%s -> %s\n", debugstr_w(stname
), debugstr_w(decoded
));
134 if (clone_open_stream( db
, stname
, stm
) == ERROR_SUCCESS
)
135 return ERROR_SUCCESS
;
137 r
= IStorage_OpenStream( db
->storage
, stname
, NULL
,
138 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, stm
);
141 MSITRANSFORM
*transform
;
143 LIST_FOR_EACH_ENTRY( transform
, &db
->transforms
, MSITRANSFORM
, entry
)
145 r
= IStorage_OpenStream( transform
->stg
, stname
, NULL
,
146 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, stm
);
156 stream
= msi_alloc( sizeof(MSISTREAM
) );
158 return ERROR_NOT_ENOUGH_MEMORY
;
161 IStream_AddRef( *stm
);
162 list_add_tail( &db
->streams
, &stream
->entry
);
165 return SUCCEEDED(r
) ? ERROR_SUCCESS
: ERROR_FUNCTION_FAILED
;
168 static void free_transforms( MSIDATABASE
*db
)
170 while( !list_empty( &db
->transforms
) )
172 MSITRANSFORM
*t
= LIST_ENTRY( list_head( &db
->transforms
),
173 MSITRANSFORM
, entry
);
174 list_remove( &t
->entry
);
175 IStorage_Release( t
->stg
);
180 void db_destroy_stream( MSIDATABASE
*db
, LPCWSTR stname
)
182 MSISTREAM
*stream
, *stream2
;
184 LIST_FOR_EACH_ENTRY_SAFE( stream
, stream2
, &db
->streams
, MSISTREAM
, entry
)
189 r
= IStream_Stat( stream
->stm
, &stat
, 0 );
192 WARN("failed to stat stream r = %08x\n", r
);
196 if (!strcmpW( stname
, stat
.pwcsName
))
198 TRACE("destroying %s\n", debugstr_w(stname
));
200 list_remove( &stream
->entry
);
201 IStream_Release( stream
->stm
);
203 IStorage_DestroyElement( db
->storage
, stname
);
204 CoTaskMemFree( stat
.pwcsName
);
207 CoTaskMemFree( stat
.pwcsName
);
211 static void free_streams( MSIDATABASE
*db
)
213 while( !list_empty( &db
->streams
) )
215 MSISTREAM
*s
= LIST_ENTRY( list_head( &db
->streams
),
217 list_remove( &s
->entry
);
218 IStream_Release( s
->stm
);
223 void append_storage_to_db( MSIDATABASE
*db
, IStorage
*stg
)
227 t
= msi_alloc( sizeof *t
);
229 IStorage_AddRef( stg
);
230 list_add_head( &db
->transforms
, &t
->entry
);
232 /* the transform may add or replace streams */
236 static VOID
MSI_CloseDatabase( MSIOBJECTHDR
*arg
)
238 MSIDATABASE
*db
= (MSIDATABASE
*) arg
;
241 free_cached_tables( db
);
243 free_transforms( db
);
244 msi_destroy_stringtable( db
->strings
);
245 IStorage_Release( db
->storage
);
248 DeleteFileW( db
->deletefile
);
249 msi_free( db
->deletefile
);
253 DeleteFileW( db
->localfile
);
254 msi_free( db
->localfile
);
258 UINT
MSI_OpenDatabaseW(LPCWSTR szDBPath
, LPCWSTR szPersist
, MSIDATABASE
**pdb
)
260 IStorage
*stg
= NULL
;
262 MSIDATABASE
*db
= NULL
;
263 UINT ret
= ERROR_FUNCTION_FAILED
;
264 LPCWSTR szMode
, save_path
;
266 BOOL created
= FALSE
, patch
= FALSE
;
267 WCHAR path
[MAX_PATH
];
269 static const WCHAR szTables
[] = { '_','T','a','b','l','e','s',0 };
271 TRACE("%s %s\n",debugstr_w(szDBPath
),debugstr_w(szPersist
) );
274 return ERROR_INVALID_PARAMETER
;
276 if (szPersist
- MSIDBOPEN_PATCHFILE
>= MSIDBOPEN_READONLY
&&
277 szPersist
- MSIDBOPEN_PATCHFILE
<= MSIDBOPEN_CREATEDIRECT
)
279 TRACE("Database is a patch\n");
280 szPersist
-= MSIDBOPEN_PATCHFILE
;
284 save_path
= szDBPath
;
286 if( !IS_INTMSIDBOPEN(szPersist
) )
288 if (!CopyFileW( szDBPath
, szPersist
, FALSE
))
289 return ERROR_OPEN_FAILED
;
291 szDBPath
= szPersist
;
292 szPersist
= MSIDBOPEN_TRANSACT
;
296 if( szPersist
== MSIDBOPEN_READONLY
)
298 r
= StgOpenStorage( szDBPath
, NULL
,
299 STGM_DIRECT
|STGM_READ
|STGM_SHARE_DENY_WRITE
, NULL
, 0, &stg
);
301 else if( szPersist
== MSIDBOPEN_CREATE
|| szPersist
== MSIDBOPEN_CREATEDIRECT
)
303 /* FIXME: MSIDBOPEN_CREATE should case STGM_TRANSACTED flag to be
305 r
= StgCreateDocfile( szDBPath
,
306 STGM_CREATE
|STGM_DIRECT
|STGM_READWRITE
|STGM_SHARE_EXCLUSIVE
, 0, &stg
);
307 if( r
== ERROR_SUCCESS
)
309 IStorage_SetClass( stg
, patch
? &CLSID_MsiPatch
: &CLSID_MsiDatabase
);
310 /* create the _Tables stream */
311 r
= write_stream_data(stg
, szTables
, NULL
, 0, TRUE
);
313 r
= msi_init_string_table( stg
);
317 else if( szPersist
== MSIDBOPEN_TRANSACT
)
319 /* FIXME: MSIDBOPEN_TRANSACT should case STGM_TRANSACTED flag to be
321 r
= StgOpenStorage( szDBPath
, NULL
,
322 STGM_DIRECT
|STGM_READWRITE
|STGM_SHARE_EXCLUSIVE
, NULL
, 0, &stg
);
324 else if( szPersist
== MSIDBOPEN_DIRECT
)
326 r
= StgOpenStorage( szDBPath
, NULL
,
327 STGM_DIRECT
|STGM_READWRITE
|STGM_SHARE_EXCLUSIVE
, NULL
, 0, &stg
);
331 ERR("unknown flag %p\n",szPersist
);
332 return ERROR_INVALID_PARAMETER
;
335 if( FAILED( r
) || !stg
)
337 FIXME("open failed r = %08x for %s\n", r
, debugstr_w(szDBPath
));
338 return ERROR_FUNCTION_FAILED
;
341 r
= IStorage_Stat( stg
, &stat
, STATFLAG_NONAME
);
344 FIXME("Failed to stat storage\n");
348 if ( !IsEqualGUID( &stat
.clsid
, &CLSID_MsiDatabase
) &&
349 !IsEqualGUID( &stat
.clsid
, &CLSID_MsiPatch
) &&
350 !IsEqualGUID( &stat
.clsid
, &CLSID_MsiTransform
) )
352 ERR("storage GUID is not a MSI database GUID %s\n",
353 debugstr_guid(&stat
.clsid
) );
357 if ( patch
&& !IsEqualGUID( &stat
.clsid
, &CLSID_MsiPatch
) )
359 ERR("storage GUID is not the MSI patch GUID %s\n",
360 debugstr_guid(&stat
.clsid
) );
361 ret
= ERROR_OPEN_FAILED
;
365 db
= alloc_msiobject( MSIHANDLETYPE_DATABASE
, sizeof (MSIDATABASE
),
369 FIXME("Failed to allocate a handle\n");
373 if (!strchrW( save_path
, '\\' ))
375 GetCurrentDirectoryW( MAX_PATH
, path
);
376 lstrcatW( path
, szBackSlash
);
377 lstrcatW( path
, save_path
);
380 lstrcpyW( path
, save_path
);
382 db
->path
= strdupW( path
);
384 if( TRACE_ON( msi
) )
385 enum_stream_names( stg
);
390 db
->deletefile
= strdupW( szDBPath
);
391 list_init( &db
->tables
);
392 list_init( &db
->transforms
);
393 list_init( &db
->streams
);
395 db
->strings
= msi_load_string_table( stg
, &db
->bytes_per_strref
);
401 msiobj_addref( &db
->hdr
);
402 IStorage_AddRef( stg
);
407 msiobj_release( &db
->hdr
);
409 IStorage_Release( stg
);
414 UINT WINAPI
MsiOpenDatabaseW(LPCWSTR szDBPath
, LPCWSTR szPersist
, MSIHANDLE
*phDB
)
419 TRACE("%s %s %p\n",debugstr_w(szDBPath
),debugstr_w(szPersist
), phDB
);
421 ret
= MSI_OpenDatabaseW( szDBPath
, szPersist
, &db
);
422 if( ret
== ERROR_SUCCESS
)
424 *phDB
= alloc_msihandle( &db
->hdr
);
426 ret
= ERROR_NOT_ENOUGH_MEMORY
;
427 msiobj_release( &db
->hdr
);
433 UINT WINAPI
MsiOpenDatabaseA(LPCSTR szDBPath
, LPCSTR szPersist
, MSIHANDLE
*phDB
)
435 HRESULT r
= ERROR_FUNCTION_FAILED
;
436 LPWSTR szwDBPath
= NULL
, szwPersist
= NULL
;
438 TRACE("%s %s %p\n", debugstr_a(szDBPath
), debugstr_a(szPersist
), phDB
);
442 szwDBPath
= strdupAtoW( szDBPath
);
447 if( !IS_INTMSIDBOPEN(szPersist
) )
449 szwPersist
= strdupAtoW( szPersist
);
454 szwPersist
= (LPWSTR
)(DWORD_PTR
)szPersist
;
456 r
= MsiOpenDatabaseW( szwDBPath
, szwPersist
, phDB
);
459 if( !IS_INTMSIDBOPEN(szPersist
) )
460 msi_free( szwPersist
);
461 msi_free( szwDBPath
);
466 static LPWSTR
msi_read_text_archive(LPCWSTR path
)
471 DWORD read
, size
= 0;
473 file
= CreateFileW( path
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, NULL
);
474 if (file
== INVALID_HANDLE_VALUE
)
477 size
= GetFileSize( file
, NULL
);
478 data
= msi_alloc( size
+ 1 );
482 if (!ReadFile( file
, data
, size
, &read
, NULL
))
486 wdata
= strdupAtoW( data
);
494 static void msi_parse_line(LPWSTR
*line
, LPWSTR
**entries
, DWORD
*num_entries
)
496 LPWSTR ptr
= *line
, save
;
501 /* stay on this line */
502 while (*ptr
&& *ptr
!= '\n')
504 /* entries are separated by tabs */
511 *entries
= msi_alloc(count
* sizeof(LPWSTR
));
515 /* store pointers into the data */
516 for (i
= 0, ptr
= *line
; i
< count
; i
++)
518 while (*ptr
&& *ptr
== '\r') ptr
++;
521 while (*ptr
&& *ptr
!= '\t' && *ptr
!= '\n' && *ptr
!= '\r') ptr
++;
523 /* NULL-separate the data */
524 if (*ptr
== '\n' || *ptr
== '\r')
526 while (*ptr
== '\n' || *ptr
== '\r')
532 (*entries
)[i
] = save
;
535 /* move to the next line if there's more, else EOF */
539 *num_entries
= count
;
542 static LPWSTR
msi_build_createsql_prelude(LPWSTR table
)
547 static const WCHAR create_fmt
[] = {'C','R','E','A','T','E',' ','T','A','B','L','E',' ','`','%','s','`',' ','(',' ',0};
549 size
= sizeof(create_fmt
)/sizeof(create_fmt
[0]) + lstrlenW(table
) - 2;
550 prelude
= msi_alloc(size
* sizeof(WCHAR
));
554 sprintfW(prelude
, create_fmt
, table
);
558 static LPWSTR
msi_build_createsql_columns(LPWSTR
*columns_data
, LPWSTR
*types
, DWORD num_columns
)
562 DWORD sql_size
= 1, i
, len
;
563 WCHAR expanded
[128], *ptr
;
564 WCHAR size
[10], comma
[2], extra
[30];
566 static const WCHAR column_fmt
[] = {'`','%','s','`',' ','%','s','%','s','%','s','%','s',' ',0};
567 static const WCHAR size_fmt
[] = {'(','%','s',')',0};
568 static const WCHAR type_char
[] = {'C','H','A','R',0};
569 static const WCHAR type_int
[] = {'I','N','T',0};
570 static const WCHAR type_long
[] = {'L','O','N','G',0};
571 static const WCHAR type_object
[] = {'O','B','J','E','C','T',0};
572 static const WCHAR type_notnull
[] = {' ','N','O','T',' ','N','U','L','L',0};
573 static const WCHAR localizable
[] = {' ','L','O','C','A','L','I','Z','A','B','L','E',0};
575 columns
= msi_alloc_zero(sql_size
* sizeof(WCHAR
));
579 for (i
= 0; i
< num_columns
; i
++)
582 comma
[1] = size
[0] = extra
[0] = '\0';
584 if (i
== num_columns
- 1)
596 lstrcpyW(extra
, type_notnull
);
598 lstrcatW(extra
, localizable
);
600 sprintfW(size
, size_fmt
, ptr
);
603 lstrcpyW(extra
, type_notnull
);
606 sprintfW(size
, size_fmt
, ptr
);
609 lstrcpyW(extra
, type_notnull
);
617 WARN("invalid int width %u\n", len
);
623 lstrcpyW(extra
, type_notnull
);
628 ERR("Unknown type: %c\n", types
[i
][0]);
633 sprintfW(expanded
, column_fmt
, columns_data
[i
], type
, size
, extra
, comma
);
634 sql_size
+= lstrlenW(expanded
);
636 p
= msi_realloc(columns
, sql_size
* sizeof(WCHAR
));
644 lstrcatW(columns
, expanded
);
650 static LPWSTR
msi_build_createsql_postlude(LPWSTR
*primary_keys
, DWORD num_keys
)
652 LPWSTR postlude
, keys
, ptr
;
653 DWORD size
, key_size
, i
;
655 static const WCHAR key_fmt
[] = {'`','%','s','`',',',' ',0};
656 static const WCHAR postlude_fmt
[] = {'P','R','I','M','A','R','Y',' ','K','E','Y',' ','%','s',')',0};
658 for (i
= 0, size
= 1; i
< num_keys
; i
++)
659 size
+= lstrlenW(key_fmt
) + lstrlenW(primary_keys
[i
]) - 2;
661 keys
= msi_alloc(size
* sizeof(WCHAR
));
665 for (i
= 0, ptr
= keys
; i
< num_keys
; i
++)
667 key_size
= lstrlenW(key_fmt
) + lstrlenW(primary_keys
[i
]) -2;
668 sprintfW(ptr
, key_fmt
, primary_keys
[i
]);
672 /* remove final ', ' */
675 size
= lstrlenW(postlude_fmt
) + size
- 1;
676 postlude
= msi_alloc(size
* sizeof(WCHAR
));
680 sprintfW(postlude
, postlude_fmt
, keys
);
687 static UINT
msi_add_table_to_db(MSIDATABASE
*db
, LPWSTR
*columns
, LPWSTR
*types
, LPWSTR
*labels
, DWORD num_labels
, DWORD num_columns
)
689 UINT r
= ERROR_OUTOFMEMORY
;
692 LPWSTR create_sql
= NULL
;
693 LPWSTR prelude
, columns_sql
, postlude
;
695 prelude
= msi_build_createsql_prelude(labels
[0]);
696 columns_sql
= msi_build_createsql_columns(columns
, types
, num_columns
);
697 postlude
= msi_build_createsql_postlude(labels
+ 1, num_labels
- 1); /* skip over table name */
699 if (!prelude
|| !columns_sql
|| !postlude
)
702 size
= lstrlenW(prelude
) + lstrlenW(columns_sql
) + lstrlenW(postlude
) + 1;
703 create_sql
= msi_alloc(size
* sizeof(WCHAR
));
707 lstrcpyW(create_sql
, prelude
);
708 lstrcatW(create_sql
, columns_sql
);
709 lstrcatW(create_sql
, postlude
);
711 r
= MSI_DatabaseOpenViewW( db
, create_sql
, &view
);
712 if (r
!= ERROR_SUCCESS
)
715 r
= MSI_ViewExecute(view
, NULL
);
717 msiobj_release(&view
->hdr
);
721 msi_free(columns_sql
);
723 msi_free(create_sql
);
727 static LPWSTR
msi_import_stream_filename(LPCWSTR path
, LPCWSTR name
)
730 LPWSTR fullname
, ptr
;
732 len
= lstrlenW(path
) + lstrlenW(name
) + 1;
733 fullname
= msi_alloc(len
*sizeof(WCHAR
));
737 lstrcpyW( fullname
, path
);
739 /* chop off extension from path */
740 ptr
= strrchrW(fullname
, '.');
747 lstrcpyW( ptr
, name
);
751 static UINT
construct_record(DWORD num_columns
, LPWSTR
*types
,
752 LPWSTR
*data
, LPWSTR path
, MSIRECORD
**rec
)
756 *rec
= MSI_CreateRecord(num_columns
);
758 return ERROR_OUTOFMEMORY
;
760 for (i
= 0; i
< num_columns
; i
++)
764 case 'L': case 'l': case 'S': case 's':
765 MSI_RecordSetStringW(*rec
, i
+ 1, data
[i
]);
769 MSI_RecordSetInteger(*rec
, i
+ 1, atoiW(data
[i
]));
775 LPWSTR file
= msi_import_stream_filename(path
, data
[i
]);
777 return ERROR_FUNCTION_FAILED
;
779 r
= MSI_RecordSetStreamFromFileW(*rec
, i
+ 1, file
);
781 if (r
!= ERROR_SUCCESS
)
782 return ERROR_FUNCTION_FAILED
;
786 ERR("Unhandled column type: %c\n", types
[i
][0]);
787 msiobj_release(&(*rec
)->hdr
);
788 return ERROR_FUNCTION_FAILED
;
792 return ERROR_SUCCESS
;
795 static UINT
msi_add_records_to_table(MSIDATABASE
*db
, LPWSTR
*columns
, LPWSTR
*types
,
796 LPWSTR
*labels
, LPWSTR
**records
,
797 int num_columns
, int num_records
,
805 static const WCHAR select
[] = {
806 'S','E','L','E','C','T',' ','*',' ',
807 'F','R','O','M',' ','`','%','s','`',0
810 r
= MSI_OpenQuery(db
, &view
, select
, labels
[0]);
811 if (r
!= ERROR_SUCCESS
)
814 while (MSI_ViewFetch(view
, &rec
) != ERROR_NO_MORE_ITEMS
)
816 r
= MSI_ViewModify(view
, MSIMODIFY_DELETE
, rec
);
817 msiobj_release(&rec
->hdr
);
818 if (r
!= ERROR_SUCCESS
)
822 for (i
= 0; i
< num_records
; i
++)
824 r
= construct_record(num_columns
, types
, records
[i
], path
, &rec
);
825 if (r
!= ERROR_SUCCESS
)
828 r
= MSI_ViewModify(view
, MSIMODIFY_INSERT
, rec
);
829 if (r
!= ERROR_SUCCESS
)
831 msiobj_release(&rec
->hdr
);
835 msiobj_release(&rec
->hdr
);
839 msiobj_release(&view
->hdr
);
843 static UINT
MSI_DatabaseImport(MSIDATABASE
*db
, LPCWSTR folder
, LPCWSTR file
)
847 DWORD num_labels
, num_types
;
848 DWORD num_columns
, num_records
= 0;
849 LPWSTR
*columns
, *types
, *labels
;
850 LPWSTR path
, ptr
, data
;
851 LPWSTR
**records
= NULL
;
852 LPWSTR
**temp_records
;
854 static const WCHAR suminfo
[] =
855 {'_','S','u','m','m','a','r','y','I','n','f','o','r','m','a','t','i','o','n',0};
857 TRACE("%p %s %s\n", db
, debugstr_w(folder
), debugstr_w(file
) );
859 if( folder
== NULL
|| file
== NULL
)
860 return ERROR_INVALID_PARAMETER
;
862 len
= lstrlenW(folder
) + lstrlenW(szBackSlash
) + lstrlenW(file
) + 1;
863 path
= msi_alloc( len
* sizeof(WCHAR
) );
865 return ERROR_OUTOFMEMORY
;
867 lstrcpyW( path
, folder
);
868 lstrcatW( path
, szBackSlash
);
869 lstrcatW( path
, file
);
871 data
= msi_read_text_archive( path
);
874 msi_parse_line( &ptr
, &columns
, &num_columns
);
875 msi_parse_line( &ptr
, &types
, &num_types
);
876 msi_parse_line( &ptr
, &labels
, &num_labels
);
878 if (num_columns
!= num_types
)
880 r
= ERROR_FUNCTION_FAILED
;
884 records
= msi_alloc(sizeof(LPWSTR
*));
887 r
= ERROR_OUTOFMEMORY
;
891 /* read in the table records */
894 msi_parse_line( &ptr
, &records
[num_records
], NULL
);
897 temp_records
= msi_realloc(records
, (num_records
+ 1) * sizeof(LPWSTR
*));
900 r
= ERROR_OUTOFMEMORY
;
903 records
= temp_records
;
906 if (!strcmpW(labels
[0], suminfo
))
908 r
= msi_add_suminfo( db
, records
, num_records
, num_columns
);
909 if (r
!= ERROR_SUCCESS
)
911 r
= ERROR_FUNCTION_FAILED
;
917 if (!TABLE_Exists(db
, labels
[0]))
919 r
= msi_add_table_to_db( db
, columns
, types
, labels
, num_labels
, num_columns
);
920 if (r
!= ERROR_SUCCESS
)
922 r
= ERROR_FUNCTION_FAILED
;
927 r
= msi_add_records_to_table( db
, columns
, types
, labels
, records
, num_columns
, num_records
, path
);
937 for (i
= 0; i
< num_records
; i
++)
938 msi_free(records
[i
]);
945 UINT WINAPI
MsiDatabaseImportW(MSIHANDLE handle
, LPCWSTR szFolder
, LPCWSTR szFilename
)
950 TRACE("%x %s %s\n",handle
,debugstr_w(szFolder
), debugstr_w(szFilename
));
952 db
= msihandle2msiinfo( handle
, MSIHANDLETYPE_DATABASE
);
955 IWineMsiRemoteDatabase
*remote_database
;
957 remote_database
= (IWineMsiRemoteDatabase
*)msi_get_remote( handle
);
958 if ( !remote_database
)
959 return ERROR_INVALID_HANDLE
;
961 IWineMsiRemoteDatabase_Release( remote_database
);
962 WARN("MsiDatabaseImport not allowed during a custom action!\n");
964 return ERROR_SUCCESS
;
967 r
= MSI_DatabaseImport( db
, szFolder
, szFilename
);
968 msiobj_release( &db
->hdr
);
972 UINT WINAPI
MsiDatabaseImportA( MSIHANDLE handle
,
973 LPCSTR szFolder
, LPCSTR szFilename
)
975 LPWSTR path
= NULL
, file
= NULL
;
976 UINT r
= ERROR_OUTOFMEMORY
;
978 TRACE("%x %s %s\n", handle
, debugstr_a(szFolder
), debugstr_a(szFilename
));
982 path
= strdupAtoW( szFolder
);
989 file
= strdupAtoW( szFilename
);
994 r
= MsiDatabaseImportW( handle
, path
, file
);
1003 static UINT
msi_export_record( HANDLE handle
, MSIRECORD
*row
, UINT start
)
1005 UINT i
, count
, len
, r
= ERROR_SUCCESS
;
1011 buffer
= msi_alloc( len
);
1013 return ERROR_OUTOFMEMORY
;
1015 count
= MSI_RecordGetFieldCount( row
);
1016 for ( i
=start
; i
<=count
; i
++ )
1019 r
= MSI_RecordGetStringA( row
, i
, buffer
, &sz
);
1020 if (r
== ERROR_MORE_DATA
)
1022 char *p
= msi_realloc( buffer
, sz
+ 1 );
1029 r
= MSI_RecordGetStringA( row
, i
, buffer
, &sz
);
1030 if (r
!= ERROR_SUCCESS
)
1033 if (!WriteFile( handle
, buffer
, sz
, &sz
, NULL
))
1035 r
= ERROR_FUNCTION_FAILED
;
1039 sep
= (i
< count
) ? "\t" : "\r\n";
1040 if (!WriteFile( handle
, sep
, strlen(sep
), &sz
, NULL
))
1042 r
= ERROR_FUNCTION_FAILED
;
1050 static UINT
msi_export_row( MSIRECORD
*row
, void *arg
)
1052 return msi_export_record( arg
, row
, 1 );
1055 static UINT
msi_export_forcecodepage( HANDLE handle
)
1059 static const char data
[] = "\r\n\r\n0\t_ForceCodepage\r\n";
1061 FIXME("Read the codepage from the strings table!\n");
1063 sz
= lstrlenA(data
) + 1;
1064 if (!WriteFile(handle
, data
, sz
, &sz
, NULL
))
1065 return ERROR_FUNCTION_FAILED
;
1067 return ERROR_SUCCESS
;
1070 static UINT
MSI_DatabaseExport( MSIDATABASE
*db
, LPCWSTR table
,
1071 LPCWSTR folder
, LPCWSTR file
)
1073 static const WCHAR query
[] = {
1074 's','e','l','e','c','t',' ','*',' ','f','r','o','m',' ','%','s',0 };
1075 static const WCHAR forcecodepage
[] = {
1076 '_','F','o','r','c','e','C','o','d','e','p','a','g','e',0 };
1077 MSIRECORD
*rec
= NULL
;
1078 MSIQUERY
*view
= NULL
;
1083 TRACE("%p %s %s %s\n", db
, debugstr_w(table
),
1084 debugstr_w(folder
), debugstr_w(file
) );
1086 if( folder
== NULL
|| file
== NULL
)
1087 return ERROR_INVALID_PARAMETER
;
1089 len
= lstrlenW(folder
) + lstrlenW(file
) + 2;
1090 filename
= msi_alloc(len
* sizeof (WCHAR
));
1092 return ERROR_OUTOFMEMORY
;
1094 lstrcpyW( filename
, folder
);
1095 lstrcatW( filename
, szBackSlash
);
1096 lstrcatW( filename
, file
);
1098 handle
= CreateFileW( filename
, GENERIC_READ
| GENERIC_WRITE
, 0,
1099 NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1100 msi_free( filename
);
1101 if (handle
== INVALID_HANDLE_VALUE
)
1102 return ERROR_FUNCTION_FAILED
;
1104 if (!lstrcmpW( table
, forcecodepage
))
1106 r
= msi_export_forcecodepage( handle
);
1110 r
= MSI_OpenQuery( db
, &view
, query
, table
);
1111 if (r
== ERROR_SUCCESS
)
1113 /* write out row 1, the column names */
1114 r
= MSI_ViewGetColumnInfo(view
, MSICOLINFO_NAMES
, &rec
);
1115 if (r
== ERROR_SUCCESS
)
1117 msi_export_record( handle
, rec
, 1 );
1118 msiobj_release( &rec
->hdr
);
1121 /* write out row 2, the column types */
1122 r
= MSI_ViewGetColumnInfo(view
, MSICOLINFO_TYPES
, &rec
);
1123 if (r
== ERROR_SUCCESS
)
1125 msi_export_record( handle
, rec
, 1 );
1126 msiobj_release( &rec
->hdr
);
1129 /* write out row 3, the table name + keys */
1130 r
= MSI_DatabaseGetPrimaryKeys( db
, table
, &rec
);
1131 if (r
== ERROR_SUCCESS
)
1133 MSI_RecordSetStringW( rec
, 0, table
);
1134 msi_export_record( handle
, rec
, 0 );
1135 msiobj_release( &rec
->hdr
);
1138 /* write out row 4 onwards, the data */
1139 r
= MSI_IterateRecords( view
, 0, msi_export_row
, handle
);
1140 msiobj_release( &view
->hdr
);
1144 CloseHandle( handle
);
1148 /***********************************************************************
1149 * MsiExportDatabaseW [MSI.@]
1151 * Writes a file containing the table data as tab separated ASCII.
1153 * The format is as follows:
1155 * row1 : colname1 <tab> colname2 <tab> .... colnameN <cr> <lf>
1156 * row2 : coltype1 <tab> coltype2 <tab> .... coltypeN <cr> <lf>
1157 * row3 : tablename <tab> key1 <tab> key2 <tab> ... keyM <cr> <lf>
1159 * Followed by the data, starting at row 1 with one row per line
1161 * row4 : data <tab> data <tab> data <tab> ... data <cr> <lf>
1163 UINT WINAPI
MsiDatabaseExportW( MSIHANDLE handle
, LPCWSTR szTable
,
1164 LPCWSTR szFolder
, LPCWSTR szFilename
)
1169 TRACE("%x %s %s %s\n", handle
, debugstr_w(szTable
),
1170 debugstr_w(szFolder
), debugstr_w(szFilename
));
1172 db
= msihandle2msiinfo( handle
, MSIHANDLETYPE_DATABASE
);
1175 IWineMsiRemoteDatabase
*remote_database
;
1177 remote_database
= (IWineMsiRemoteDatabase
*)msi_get_remote( handle
);
1178 if ( !remote_database
)
1179 return ERROR_INVALID_HANDLE
;
1181 IWineMsiRemoteDatabase_Release( remote_database
);
1182 WARN("MsiDatabaseExport not allowed during a custom action!\n");
1184 return ERROR_SUCCESS
;
1187 r
= MSI_DatabaseExport( db
, szTable
, szFolder
, szFilename
);
1188 msiobj_release( &db
->hdr
);
1192 UINT WINAPI
MsiDatabaseExportA( MSIHANDLE handle
, LPCSTR szTable
,
1193 LPCSTR szFolder
, LPCSTR szFilename
)
1195 LPWSTR path
= NULL
, file
= NULL
, table
= NULL
;
1196 UINT r
= ERROR_OUTOFMEMORY
;
1198 TRACE("%x %s %s %s\n", handle
, debugstr_a(szTable
),
1199 debugstr_a(szFolder
), debugstr_a(szFilename
));
1203 table
= strdupAtoW( szTable
);
1210 path
= strdupAtoW( szFolder
);
1217 file
= strdupAtoW( szFilename
);
1222 r
= MsiDatabaseExportW( handle
, table
, path
, file
);
1232 UINT WINAPI
MsiDatabaseMergeA(MSIHANDLE hDatabase
, MSIHANDLE hDatabaseMerge
,
1238 TRACE("(%d, %d, %s)\n", hDatabase
, hDatabaseMerge
,
1239 debugstr_a(szTableName
));
1241 table
= strdupAtoW(szTableName
);
1242 r
= MsiDatabaseMergeW(hDatabase
, hDatabaseMerge
, table
);
1248 typedef struct _tagMERGETABLE
1262 typedef struct _tagMERGEROW
1268 typedef struct _tagMERGEDATA
1272 MERGETABLE
*curtable
;
1274 struct list
*tabledata
;
1277 static BOOL
merge_type_match(LPCWSTR type1
, LPCWSTR type2
)
1279 if (((type1
[0] == 'l') || (type1
[0] == 's')) &&
1280 ((type2
[0] == 'l') || (type2
[0] == 's')))
1283 if (((type1
[0] == 'L') || (type1
[0] == 'S')) &&
1284 ((type2
[0] == 'L') || (type2
[0] == 'S')))
1287 return !lstrcmpW(type1
, type2
);
1290 static UINT
merge_verify_colnames(MSIQUERY
*dbview
, MSIQUERY
*mergeview
)
1292 MSIRECORD
*dbrec
, *mergerec
;
1295 r
= MSI_ViewGetColumnInfo(dbview
, MSICOLINFO_NAMES
, &dbrec
);
1296 if (r
!= ERROR_SUCCESS
)
1299 r
= MSI_ViewGetColumnInfo(mergeview
, MSICOLINFO_NAMES
, &mergerec
);
1300 if (r
!= ERROR_SUCCESS
)
1303 count
= MSI_RecordGetFieldCount(dbrec
);
1304 for (i
= 1; i
<= count
; i
++)
1306 if (!MSI_RecordGetString(mergerec
, i
))
1309 if (lstrcmpW(MSI_RecordGetString(dbrec
, i
),
1310 MSI_RecordGetString(mergerec
, i
)))
1312 r
= ERROR_DATATYPE_MISMATCH
;
1317 msiobj_release(&dbrec
->hdr
);
1318 msiobj_release(&mergerec
->hdr
);
1319 dbrec
= mergerec
= NULL
;
1321 r
= MSI_ViewGetColumnInfo(dbview
, MSICOLINFO_TYPES
, &dbrec
);
1322 if (r
!= ERROR_SUCCESS
)
1325 r
= MSI_ViewGetColumnInfo(mergeview
, MSICOLINFO_TYPES
, &mergerec
);
1326 if (r
!= ERROR_SUCCESS
)
1329 count
= MSI_RecordGetFieldCount(dbrec
);
1330 for (i
= 1; i
<= count
; i
++)
1332 if (!MSI_RecordGetString(mergerec
, i
))
1335 if (!merge_type_match(MSI_RecordGetString(dbrec
, i
),
1336 MSI_RecordGetString(mergerec
, i
)))
1338 r
= ERROR_DATATYPE_MISMATCH
;
1344 msiobj_release(&dbrec
->hdr
);
1345 msiobj_release(&mergerec
->hdr
);
1350 static UINT
merge_verify_primary_keys(MSIDATABASE
*db
, MSIDATABASE
*mergedb
,
1353 MSIRECORD
*dbrec
, *mergerec
= NULL
;
1356 r
= MSI_DatabaseGetPrimaryKeys(db
, table
, &dbrec
);
1357 if (r
!= ERROR_SUCCESS
)
1360 r
= MSI_DatabaseGetPrimaryKeys(mergedb
, table
, &mergerec
);
1361 if (r
!= ERROR_SUCCESS
)
1364 count
= MSI_RecordGetFieldCount(dbrec
);
1365 if (count
!= MSI_RecordGetFieldCount(mergerec
))
1367 r
= ERROR_DATATYPE_MISMATCH
;
1371 for (i
= 1; i
<= count
; i
++)
1373 if (lstrcmpW(MSI_RecordGetString(dbrec
, i
),
1374 MSI_RecordGetString(mergerec
, i
)))
1376 r
= ERROR_DATATYPE_MISMATCH
;
1382 msiobj_release(&dbrec
->hdr
);
1383 msiobj_release(&mergerec
->hdr
);
1388 static LPWSTR
get_key_value(MSIQUERY
*view
, LPCWSTR key
, MSIRECORD
*rec
)
1390 MSIRECORD
*colnames
;
1392 UINT r
, i
= 0, sz
= 0;
1395 r
= MSI_ViewGetColumnInfo(view
, MSICOLINFO_NAMES
, &colnames
);
1396 if (r
!= ERROR_SUCCESS
)
1401 str
= msi_dup_record_field(colnames
, ++i
);
1402 cmp
= lstrcmpW(key
, str
);
1406 msiobj_release(&colnames
->hdr
);
1408 r
= MSI_RecordGetStringW(rec
, i
, NULL
, &sz
);
1409 if (r
!= ERROR_SUCCESS
)
1413 if (MSI_RecordGetString(rec
, i
)) /* check record field is a string */
1415 /* quote string record fields */
1416 const WCHAR szQuote
[] = {'\'', 0};
1418 val
= msi_alloc(sz
*sizeof(WCHAR
));
1422 lstrcpyW(val
, szQuote
);
1423 r
= MSI_RecordGetStringW(rec
, i
, val
+1, &sz
);
1424 lstrcpyW(val
+1+sz
, szQuote
);
1428 /* do not quote integer record fields */
1429 val
= msi_alloc(sz
*sizeof(WCHAR
));
1433 r
= MSI_RecordGetStringW(rec
, i
, val
, &sz
);
1436 if (r
!= ERROR_SUCCESS
)
1438 ERR("failed to get string!\n");
1446 static LPWSTR
create_diff_row_query(MSIDATABASE
*merge
, MSIQUERY
*view
,
1447 LPWSTR table
, MSIRECORD
*rec
)
1449 LPWSTR query
= NULL
, clause
= NULL
;
1450 LPWSTR ptr
= NULL
, val
;
1452 DWORD size
= 1, oldsize
;
1457 static const WCHAR keyset
[] = {
1458 '`','%','s','`',' ','=',' ','%','s',' ','A','N','D',' ',0};
1459 static const WCHAR lastkeyset
[] = {
1460 '`','%','s','`',' ','=',' ','%','s',' ',0};
1461 static const WCHAR fmt
[] = {'S','E','L','E','C','T',' ','*',' ',
1462 'F','R','O','M',' ','`','%','s','`',' ',
1463 'W','H','E','R','E',' ','%','s',0};
1465 r
= MSI_DatabaseGetPrimaryKeys(merge
, table
, &keys
);
1466 if (r
!= ERROR_SUCCESS
)
1469 clause
= msi_alloc_zero(size
* sizeof(WCHAR
));
1474 count
= MSI_RecordGetFieldCount(keys
);
1475 for (i
= 1; i
<= count
; i
++)
1477 key
= MSI_RecordGetString(keys
, i
);
1478 val
= get_key_value(view
, key
, rec
);
1481 setptr
= lastkeyset
;
1486 size
+= lstrlenW(setptr
) + lstrlenW(key
) + lstrlenW(val
) - 4;
1487 clause
= msi_realloc(clause
, size
* sizeof (WCHAR
));
1494 ptr
= clause
+ oldsize
- 1;
1495 sprintfW(ptr
, setptr
, key
, val
);
1499 size
= lstrlenW(fmt
) + lstrlenW(table
) + lstrlenW(clause
) + 1;
1500 query
= msi_alloc(size
* sizeof(WCHAR
));
1504 sprintfW(query
, fmt
, table
, clause
);
1508 msiobj_release(&keys
->hdr
);
1512 static UINT
merge_diff_row(MSIRECORD
*rec
, LPVOID param
)
1514 MERGEDATA
*data
= param
;
1515 MERGETABLE
*table
= data
->curtable
;
1517 MSIQUERY
*dbview
= NULL
;
1518 MSIRECORD
*row
= NULL
;
1519 LPWSTR query
= NULL
;
1520 UINT r
= ERROR_SUCCESS
;
1522 if (TABLE_Exists(data
->db
, table
->name
))
1524 query
= create_diff_row_query(data
->merge
, data
->curview
, table
->name
, rec
);
1526 return ERROR_OUTOFMEMORY
;
1528 r
= MSI_DatabaseOpenViewW(data
->db
, query
, &dbview
);
1529 if (r
!= ERROR_SUCCESS
)
1532 r
= MSI_ViewExecute(dbview
, NULL
);
1533 if (r
!= ERROR_SUCCESS
)
1536 r
= MSI_ViewFetch(dbview
, &row
);
1537 if (r
== ERROR_SUCCESS
&& !MSI_RecordsAreEqual(rec
, row
))
1539 table
->numconflicts
++;
1542 else if (r
!= ERROR_NO_MORE_ITEMS
)
1548 mergerow
= msi_alloc(sizeof(MERGEROW
));
1551 r
= ERROR_OUTOFMEMORY
;
1555 mergerow
->data
= MSI_CloneRecord(rec
);
1556 if (!mergerow
->data
)
1558 r
= ERROR_OUTOFMEMORY
;
1563 list_add_tail(&table
->rows
, &mergerow
->entry
);
1567 msiobj_release(&row
->hdr
);
1568 msiobj_release(&dbview
->hdr
);
1572 static UINT
msi_get_table_labels(MSIDATABASE
*db
, LPCWSTR table
, LPWSTR
**labels
, DWORD
*numlabels
)
1575 MSIRECORD
*prec
= NULL
;
1577 r
= MSI_DatabaseGetPrimaryKeys(db
, table
, &prec
);
1578 if (r
!= ERROR_SUCCESS
)
1581 count
= MSI_RecordGetFieldCount(prec
);
1582 *numlabels
= count
+ 1;
1583 *labels
= msi_alloc((*numlabels
)*sizeof(LPWSTR
));
1586 r
= ERROR_OUTOFMEMORY
;
1590 (*labels
)[0] = strdupW(table
);
1591 for (i
=1; i
<=count
; i
++ )
1593 (*labels
)[i
] = strdupW(MSI_RecordGetString(prec
, i
));
1597 msiobj_release( &prec
->hdr
);
1601 static UINT
msi_get_query_columns(MSIQUERY
*query
, LPWSTR
**columns
, DWORD
*numcolumns
)
1604 MSIRECORD
*prec
= NULL
;
1606 r
= MSI_ViewGetColumnInfo(query
, MSICOLINFO_NAMES
, &prec
);
1607 if (r
!= ERROR_SUCCESS
)
1610 count
= MSI_RecordGetFieldCount(prec
);
1611 *columns
= msi_alloc(count
*sizeof(LPWSTR
));
1614 r
= ERROR_OUTOFMEMORY
;
1618 for (i
=1; i
<=count
; i
++ )
1620 (*columns
)[i
-1] = strdupW(MSI_RecordGetString(prec
, i
));
1623 *numcolumns
= count
;
1626 msiobj_release( &prec
->hdr
);
1630 static UINT
msi_get_query_types(MSIQUERY
*query
, LPWSTR
**types
, DWORD
*numtypes
)
1633 MSIRECORD
*prec
= NULL
;
1635 r
= MSI_ViewGetColumnInfo(query
, MSICOLINFO_TYPES
, &prec
);
1636 if (r
!= ERROR_SUCCESS
)
1639 count
= MSI_RecordGetFieldCount(prec
);
1640 *types
= msi_alloc(count
*sizeof(LPWSTR
));
1643 r
= ERROR_OUTOFMEMORY
;
1648 for (i
=1; i
<=count
; i
++ )
1650 (*types
)[i
-1] = strdupW(MSI_RecordGetString(prec
, i
));
1654 msiobj_release( &prec
->hdr
);
1658 static void merge_free_rows(MERGETABLE
*table
)
1660 struct list
*item
, *cursor
;
1662 LIST_FOR_EACH_SAFE(item
, cursor
, &table
->rows
)
1664 MERGEROW
*row
= LIST_ENTRY(item
, MERGEROW
, entry
);
1666 list_remove(&row
->entry
);
1667 msiobj_release(&row
->data
->hdr
);
1672 static void free_merge_table(MERGETABLE
*table
)
1676 if (table
->labels
!= NULL
)
1678 for (i
= 0; i
< table
->numlabels
; i
++)
1679 msi_free(table
->labels
[i
]);
1681 msi_free(table
->labels
);
1684 if (table
->columns
!= NULL
)
1686 for (i
= 0; i
< table
->numcolumns
; i
++)
1687 msi_free(table
->columns
[i
]);
1689 msi_free(table
->columns
);
1692 if (table
->types
!= NULL
)
1694 for (i
= 0; i
< table
->numtypes
; i
++)
1695 msi_free(table
->types
[i
]);
1697 msi_free(table
->types
);
1700 msi_free(table
->name
);
1701 merge_free_rows(table
);
1706 static UINT
msi_get_merge_table (MSIDATABASE
*db
, LPCWSTR name
, MERGETABLE
**ptable
)
1710 MSIQUERY
*mergeview
= NULL
;
1712 static const WCHAR query
[] = {'S','E','L','E','C','T',' ','*',' ',
1713 'F','R','O','M',' ','`','%','s','`',0};
1715 table
= msi_alloc_zero(sizeof(MERGETABLE
));
1719 return ERROR_OUTOFMEMORY
;
1722 r
= msi_get_table_labels(db
, name
, &table
->labels
, &table
->numlabels
);
1723 if (r
!= ERROR_SUCCESS
)
1726 r
= MSI_OpenQuery(db
, &mergeview
, query
, name
);
1727 if (r
!= ERROR_SUCCESS
)
1730 r
= msi_get_query_columns(mergeview
, &table
->columns
, &table
->numcolumns
);
1731 if (r
!= ERROR_SUCCESS
)
1734 r
= msi_get_query_types(mergeview
, &table
->types
, &table
->numtypes
);
1735 if (r
!= ERROR_SUCCESS
)
1738 list_init(&table
->rows
);
1740 table
->name
= strdupW(name
);
1741 table
->numconflicts
= 0;
1743 msiobj_release(&mergeview
->hdr
);
1745 return ERROR_SUCCESS
;
1748 msiobj_release(&mergeview
->hdr
);
1749 free_merge_table(table
);
1754 static UINT
merge_diff_tables(MSIRECORD
*rec
, LPVOID param
)
1756 MERGEDATA
*data
= param
;
1758 MSIQUERY
*dbview
= NULL
;
1759 MSIQUERY
*mergeview
= NULL
;
1763 static const WCHAR query
[] = {'S','E','L','E','C','T',' ','*',' ',
1764 'F','R','O','M',' ','`','%','s','`',0};
1766 name
= MSI_RecordGetString(rec
, 1);
1768 r
= MSI_OpenQuery(data
->merge
, &mergeview
, query
, name
);
1769 if (r
!= ERROR_SUCCESS
)
1772 if (TABLE_Exists(data
->db
, name
))
1774 r
= MSI_OpenQuery(data
->db
, &dbview
, query
, name
);
1775 if (r
!= ERROR_SUCCESS
)
1778 r
= merge_verify_colnames(dbview
, mergeview
);
1779 if (r
!= ERROR_SUCCESS
)
1782 r
= merge_verify_primary_keys(data
->db
, data
->merge
, name
);
1783 if (r
!= ERROR_SUCCESS
)
1787 r
= msi_get_merge_table(data
->merge
, name
, &table
);
1788 if (r
!= ERROR_SUCCESS
)
1791 data
->curtable
= table
;
1792 data
->curview
= mergeview
;
1793 r
= MSI_IterateRecords(mergeview
, NULL
, merge_diff_row
, data
);
1794 if (r
!= ERROR_SUCCESS
)
1796 free_merge_table(table
);
1800 list_add_tail(data
->tabledata
, &table
->entry
);
1803 msiobj_release(&dbview
->hdr
);
1804 msiobj_release(&mergeview
->hdr
);
1808 static UINT
gather_merge_data(MSIDATABASE
*db
, MSIDATABASE
*merge
,
1809 struct list
*tabledata
)
1815 static const WCHAR query
[] = {'S','E','L','E','C','T',' ','*',' ',
1816 'F','R','O','M',' ','`','_','T','a','b','l','e','s','`',0};
1818 r
= MSI_DatabaseOpenViewW(merge
, query
, &view
);
1819 if (r
!= ERROR_SUCCESS
)
1824 data
.tabledata
= tabledata
;
1825 r
= MSI_IterateRecords(view
, NULL
, merge_diff_tables
, &data
);
1827 msiobj_release(&view
->hdr
);
1831 static UINT
merge_table(MSIDATABASE
*db
, MERGETABLE
*table
)
1837 if (!TABLE_Exists(db
, table
->name
))
1839 r
= msi_add_table_to_db(db
, table
->columns
, table
->types
,
1840 table
->labels
, table
->numlabels
, table
->numcolumns
);
1841 if (r
!= ERROR_SUCCESS
)
1842 return ERROR_FUNCTION_FAILED
;
1845 LIST_FOR_EACH_ENTRY(row
, &table
->rows
, MERGEROW
, entry
)
1847 r
= TABLE_CreateView(db
, table
->name
, &tv
);
1848 if (r
!= ERROR_SUCCESS
)
1851 r
= tv
->ops
->insert_row(tv
, row
->data
, -1, FALSE
);
1852 tv
->ops
->delete(tv
);
1854 if (r
!= ERROR_SUCCESS
)
1858 return ERROR_SUCCESS
;
1861 static UINT
update_merge_errors(MSIDATABASE
*db
, LPCWSTR error
,
1862 LPWSTR table
, DWORD numconflicts
)
1867 static const WCHAR create
[] = {
1868 'C','R','E','A','T','E',' ','T','A','B','L','E',' ',
1869 '`','%','s','`',' ','(','`','T','a','b','l','e','`',' ',
1870 'C','H','A','R','(','2','5','5',')',' ','N','O','T',' ',
1871 'N','U','L','L',',',' ','`','N','u','m','R','o','w','M','e','r','g','e',
1872 'C','o','n','f','l','i','c','t','s','`',' ','S','H','O','R','T',' ',
1873 'N','O','T',' ','N','U','L','L',' ','P','R','I','M','A','R','Y',' ',
1874 'K','E','Y',' ','`','T','a','b','l','e','`',')',0};
1875 static const WCHAR insert
[] = {
1876 'I','N','S','E','R','T',' ','I','N','T','O',' ',
1877 '`','%','s','`',' ','(','`','T','a','b','l','e','`',',',' ',
1878 '`','N','u','m','R','o','w','M','e','r','g','e',
1879 'C','o','n','f','l','i','c','t','s','`',')',' ','V','A','L','U','E','S',
1880 ' ','(','\'','%','s','\'',',',' ','%','d',')',0};
1882 if (!TABLE_Exists(db
, error
))
1884 r
= MSI_OpenQuery(db
, &view
, create
, error
);
1885 if (r
!= ERROR_SUCCESS
)
1888 r
= MSI_ViewExecute(view
, NULL
);
1889 msiobj_release(&view
->hdr
);
1890 if (r
!= ERROR_SUCCESS
)
1894 r
= MSI_OpenQuery(db
, &view
, insert
, error
, table
, numconflicts
);
1895 if (r
!= ERROR_SUCCESS
)
1898 r
= MSI_ViewExecute(view
, NULL
);
1899 msiobj_release(&view
->hdr
);
1903 UINT WINAPI
MsiDatabaseMergeW(MSIHANDLE hDatabase
, MSIHANDLE hDatabaseMerge
,
1904 LPCWSTR szTableName
)
1906 struct list tabledata
= LIST_INIT(tabledata
);
1907 struct list
*item
, *cursor
;
1908 MSIDATABASE
*db
, *merge
;
1913 TRACE("(%d, %d, %s)\n", hDatabase
, hDatabaseMerge
,
1914 debugstr_w(szTableName
));
1916 if (szTableName
&& !*szTableName
)
1917 return ERROR_INVALID_TABLE
;
1919 db
= msihandle2msiinfo(hDatabase
, MSIHANDLETYPE_DATABASE
);
1920 merge
= msihandle2msiinfo(hDatabaseMerge
, MSIHANDLETYPE_DATABASE
);
1923 r
= ERROR_INVALID_HANDLE
;
1927 r
= gather_merge_data(db
, merge
, &tabledata
);
1928 if (r
!= ERROR_SUCCESS
)
1932 LIST_FOR_EACH_ENTRY(table
, &tabledata
, MERGETABLE
, entry
)
1934 if (table
->numconflicts
)
1938 r
= update_merge_errors(db
, szTableName
, table
->name
,
1939 table
->numconflicts
);
1940 if (r
!= ERROR_SUCCESS
)
1945 r
= merge_table(db
, table
);
1946 if (r
!= ERROR_SUCCESS
)
1951 LIST_FOR_EACH_SAFE(item
, cursor
, &tabledata
)
1953 MERGETABLE
*table
= LIST_ENTRY(item
, MERGETABLE
, entry
);
1954 list_remove(&table
->entry
);
1955 free_merge_table(table
);
1959 r
= ERROR_FUNCTION_FAILED
;
1962 msiobj_release(&db
->hdr
);
1963 msiobj_release(&merge
->hdr
);
1967 MSIDBSTATE WINAPI
MsiGetDatabaseState( MSIHANDLE handle
)
1969 MSIDBSTATE ret
= MSIDBSTATE_READ
;
1972 TRACE("%d\n", handle
);
1974 db
= msihandle2msiinfo( handle
, MSIHANDLETYPE_DATABASE
);
1977 IWineMsiRemoteDatabase
*remote_database
;
1979 remote_database
= (IWineMsiRemoteDatabase
*)msi_get_remote( handle
);
1980 if ( !remote_database
)
1981 return MSIDBSTATE_ERROR
;
1983 IWineMsiRemoteDatabase_Release( remote_database
);
1984 WARN("MsiGetDatabaseState not allowed during a custom action!\n");
1986 return MSIDBSTATE_READ
;
1989 if (db
->mode
!= MSIDBOPEN_READONLY
)
1990 ret
= MSIDBSTATE_WRITE
;
1991 msiobj_release( &db
->hdr
);
1996 typedef struct _msi_remote_database_impl
{
1997 const IWineMsiRemoteDatabaseVtbl
*lpVtbl
;
2000 } msi_remote_database_impl
;
2002 static inline msi_remote_database_impl
* mrd_from_IWineMsiRemoteDatabase( IWineMsiRemoteDatabase
* iface
)
2004 return (msi_remote_database_impl
*)iface
;
2007 static HRESULT WINAPI
mrd_QueryInterface( IWineMsiRemoteDatabase
*iface
,
2008 REFIID riid
,LPVOID
*ppobj
)
2010 if( IsEqualCLSID( riid
, &IID_IUnknown
) ||
2011 IsEqualCLSID( riid
, &IID_IWineMsiRemoteDatabase
) )
2013 IUnknown_AddRef( iface
);
2018 return E_NOINTERFACE
;
2021 static ULONG WINAPI
mrd_AddRef( IWineMsiRemoteDatabase
*iface
)
2023 msi_remote_database_impl
* This
= mrd_from_IWineMsiRemoteDatabase( iface
);
2025 return InterlockedIncrement( &This
->refs
);
2028 static ULONG WINAPI
mrd_Release( IWineMsiRemoteDatabase
*iface
)
2030 msi_remote_database_impl
* This
= mrd_from_IWineMsiRemoteDatabase( iface
);
2033 r
= InterlockedDecrement( &This
->refs
);
2036 MsiCloseHandle( This
->database
);
2042 static HRESULT WINAPI
mrd_IsTablePersistent( IWineMsiRemoteDatabase
*iface
,
2043 LPCWSTR table
, MSICONDITION
*persistent
)
2045 msi_remote_database_impl
*This
= mrd_from_IWineMsiRemoteDatabase( iface
);
2046 *persistent
= MsiDatabaseIsTablePersistentW(This
->database
, table
);
2050 static HRESULT WINAPI
mrd_GetPrimaryKeys( IWineMsiRemoteDatabase
*iface
,
2051 LPCWSTR table
, MSIHANDLE
*keys
)
2053 msi_remote_database_impl
*This
= mrd_from_IWineMsiRemoteDatabase( iface
);
2054 UINT r
= MsiDatabaseGetPrimaryKeysW(This
->database
, table
, keys
);
2055 return HRESULT_FROM_WIN32(r
);
2058 static HRESULT WINAPI
mrd_GetSummaryInformation( IWineMsiRemoteDatabase
*iface
,
2059 UINT updatecount
, MSIHANDLE
*suminfo
)
2061 msi_remote_database_impl
*This
= mrd_from_IWineMsiRemoteDatabase( iface
);
2062 UINT r
= MsiGetSummaryInformationW(This
->database
, NULL
, updatecount
, suminfo
);
2063 return HRESULT_FROM_WIN32(r
);
2066 static HRESULT WINAPI
mrd_OpenView( IWineMsiRemoteDatabase
*iface
,
2067 LPCWSTR query
, MSIHANDLE
*view
)
2069 msi_remote_database_impl
*This
= mrd_from_IWineMsiRemoteDatabase( iface
);
2070 UINT r
= MsiDatabaseOpenViewW(This
->database
, query
, view
);
2071 return HRESULT_FROM_WIN32(r
);
2074 static HRESULT WINAPI
mrd_SetMsiHandle( IWineMsiRemoteDatabase
*iface
, MSIHANDLE handle
)
2076 msi_remote_database_impl
* This
= mrd_from_IWineMsiRemoteDatabase( iface
);
2077 This
->database
= handle
;
2081 static const IWineMsiRemoteDatabaseVtbl msi_remote_database_vtbl
=
2086 mrd_IsTablePersistent
,
2088 mrd_GetSummaryInformation
,
2093 HRESULT
create_msi_remote_database( IUnknown
*pOuter
, LPVOID
*ppObj
)
2095 msi_remote_database_impl
*This
;
2097 This
= msi_alloc( sizeof *This
);
2099 return E_OUTOFMEMORY
;
2101 This
->lpVtbl
= &msi_remote_database_vtbl
;