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
25 #define NONAMELESSUNION
31 #include "wine/debug.h"
32 #include "wine/unicode.h"
38 #include "msiserver.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(msi);
48 * An .msi file is a structured storage file.
49 * It contains a number of streams.
50 * A stream for each table in the database.
51 * Two streams for the string table in the database.
52 * Any binary data in a table is a reference to a stream.
55 #define IS_INTMSIDBOPEN(x) (((ULONG_PTR)(x) >> 16) == 0)
57 typedef struct tagMSITRANSFORM {
62 typedef struct tagMSISTREAM {
68 static UINT find_open_stream( MSIDATABASE *db, IStorage *stg, LPCWSTR name, IStream **stm )
72 LIST_FOR_EACH_ENTRY( stream, &db->streams, MSISTREAM, entry )
77 if (stream->stg != stg) continue;
79 r = IStream_Stat( stream->stm, &stat, 0 );
82 WARN("failed to stat stream r = %08x!\n", r);
86 if( !strcmpW( name, stat.pwcsName ) )
88 TRACE("found %s\n", debugstr_w(name));
90 CoTaskMemFree( stat.pwcsName );
94 CoTaskMemFree( stat.pwcsName );
97 return ERROR_FUNCTION_FAILED;
100 UINT msi_clone_open_stream( MSIDATABASE *db, IStorage *stg, LPCWSTR name, IStream **stm )
104 if (find_open_stream( db, stg, name, &stream ) == ERROR_SUCCESS)
109 r = IStream_Clone( stream, stm );
112 WARN("failed to clone stream r = %08x!\n", r);
113 return ERROR_FUNCTION_FAILED;
117 r = IStream_Seek( *stm, pos, STREAM_SEEK_SET, NULL );
120 IStream_Release( *stm );
121 return ERROR_FUNCTION_FAILED;
124 return ERROR_SUCCESS;
127 return ERROR_FUNCTION_FAILED;
130 UINT msi_get_raw_stream( MSIDATABASE *db, LPCWSTR stname, IStream **stm )
134 WCHAR decoded[MAX_STREAM_NAME_LEN];
136 decode_streamname( stname, decoded );
137 TRACE("%s -> %s\n", debugstr_w(stname), debugstr_w(decoded));
139 if (msi_clone_open_stream( db, db->storage, stname, stm ) == ERROR_SUCCESS)
140 return ERROR_SUCCESS;
142 r = IStorage_OpenStream( db->storage, stname, NULL,
143 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, stm );
146 MSITRANSFORM *transform;
148 LIST_FOR_EACH_ENTRY( transform, &db->transforms, MSITRANSFORM, entry )
150 r = IStorage_OpenStream( transform->stg, stname, NULL,
151 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, stm );
154 stg = transform->stg;
159 else stg = db->storage;
165 if (!(stream = msi_alloc( sizeof(MSISTREAM) ))) return ERROR_NOT_ENOUGH_MEMORY;
167 IStream_AddRef( stg );
169 IStream_AddRef( *stm );
170 list_add_tail( &db->streams, &stream->entry );
173 return SUCCEEDED(r) ? ERROR_SUCCESS : ERROR_FUNCTION_FAILED;
176 static void free_transforms( MSIDATABASE *db )
178 while( !list_empty( &db->transforms ) )
180 MSITRANSFORM *t = LIST_ENTRY( list_head( &db->transforms ),
181 MSITRANSFORM, entry );
182 list_remove( &t->entry );
183 IStorage_Release( t->stg );
188 void msi_destroy_stream( MSIDATABASE *db, const WCHAR *stname )
190 MSISTREAM *stream, *stream2;
192 LIST_FOR_EACH_ENTRY_SAFE( stream, stream2, &db->streams, MSISTREAM, entry )
197 r = IStream_Stat( stream->stm, &stat, 0 );
200 WARN("failed to stat stream r = %08x\n", r);
204 if (!strcmpW( stname, stat.pwcsName ))
206 TRACE("destroying %s\n", debugstr_w(stname));
208 list_remove( &stream->entry );
209 IStream_Release( stream->stm );
210 IStream_Release( stream->stg );
211 IStorage_DestroyElement( stream->stg, stname );
213 CoTaskMemFree( stat.pwcsName );
216 CoTaskMemFree( stat.pwcsName );
220 static void free_streams( MSIDATABASE *db )
222 while( !list_empty( &db->streams ) )
224 MSISTREAM *s = LIST_ENTRY(list_head( &db->streams ), MSISTREAM, entry);
225 list_remove( &s->entry );
226 IStream_Release( s->stm );
227 IStream_Release( s->stg );
232 void append_storage_to_db( MSIDATABASE *db, IStorage *stg )
236 t = msi_alloc( sizeof *t );
238 IStorage_AddRef( stg );
239 list_add_head( &db->transforms, &t->entry );
241 /* the transform may add or replace streams */
245 static VOID MSI_CloseDatabase( MSIOBJECTHDR *arg )
247 MSIDATABASE *db = (MSIDATABASE *) arg;
250 free_cached_tables( db );
252 free_transforms( db );
253 if (db->strings) msi_destroy_stringtable( db->strings );
254 IStorage_Release( db->storage );
257 DeleteFileW( db->deletefile );
258 msi_free( db->deletefile );
262 DeleteFileW( db->localfile );
263 msi_free( db->localfile );
267 static HRESULT db_initialize( IStorage *stg, const GUID *clsid )
269 static const WCHAR szTables[] = { '_','T','a','b','l','e','s',0 };
272 hr = IStorage_SetClass( stg, clsid );
275 WARN("failed to set class id 0x%08x\n", hr);
279 /* create the _Tables stream */
280 hr = write_stream_data( stg, szTables, NULL, 0, TRUE );
283 WARN("failed to create _Tables stream 0x%08x\n", hr);
287 hr = msi_init_string_table( stg );
290 WARN("failed to initialize string table 0x%08x\n", hr);
294 hr = IStorage_Commit( stg, 0 );
297 WARN("failed to commit changes 0x%08x\n", hr);
304 UINT MSI_OpenDatabaseW(LPCWSTR szDBPath, LPCWSTR szPersist, MSIDATABASE **pdb)
306 IStorage *stg = NULL;
308 MSIDATABASE *db = NULL;
309 UINT ret = ERROR_FUNCTION_FAILED;
310 LPCWSTR szMode, save_path;
312 BOOL created = FALSE, patch = FALSE;
313 WCHAR path[MAX_PATH];
315 TRACE("%s %s\n",debugstr_w(szDBPath),debugstr_w(szPersist) );
318 return ERROR_INVALID_PARAMETER;
320 if (szPersist - MSIDBOPEN_PATCHFILE >= MSIDBOPEN_READONLY &&
321 szPersist - MSIDBOPEN_PATCHFILE <= MSIDBOPEN_CREATEDIRECT)
323 TRACE("Database is a patch\n");
324 szPersist -= MSIDBOPEN_PATCHFILE;
328 save_path = szDBPath;
330 if( !IS_INTMSIDBOPEN(szPersist) )
332 if (!CopyFileW( szDBPath, szPersist, FALSE ))
333 return ERROR_OPEN_FAILED;
335 szDBPath = szPersist;
336 szPersist = MSIDBOPEN_TRANSACT;
340 if( szPersist == MSIDBOPEN_READONLY )
342 r = StgOpenStorage( szDBPath, NULL,
343 STGM_DIRECT|STGM_READ|STGM_SHARE_DENY_WRITE, NULL, 0, &stg);
345 else if( szPersist == MSIDBOPEN_CREATE )
347 r = StgCreateDocfile( szDBPath,
348 STGM_CREATE|STGM_TRANSACTED|STGM_READWRITE|STGM_SHARE_EXCLUSIVE, 0, &stg );
351 r = db_initialize( stg, patch ? &CLSID_MsiPatch : &CLSID_MsiDatabase );
354 else if( szPersist == MSIDBOPEN_CREATEDIRECT )
356 r = StgCreateDocfile( szDBPath,
357 STGM_CREATE|STGM_DIRECT|STGM_READWRITE|STGM_SHARE_EXCLUSIVE, 0, &stg );
360 r = db_initialize( stg, patch ? &CLSID_MsiPatch : &CLSID_MsiDatabase );
363 else if( szPersist == MSIDBOPEN_TRANSACT )
365 r = StgOpenStorage( szDBPath, NULL,
366 STGM_TRANSACTED|STGM_READWRITE|STGM_SHARE_EXCLUSIVE, NULL, 0, &stg);
368 else if( szPersist == MSIDBOPEN_DIRECT )
370 r = StgOpenStorage( szDBPath, NULL,
371 STGM_DIRECT|STGM_READWRITE|STGM_SHARE_EXCLUSIVE, NULL, 0, &stg);
375 ERR("unknown flag %p\n",szPersist);
376 return ERROR_INVALID_PARAMETER;
379 if( FAILED( r ) || !stg )
381 FIXME("open failed r = %08x for %s\n", r, debugstr_w(szDBPath));
382 return ERROR_FUNCTION_FAILED;
385 r = IStorage_Stat( stg, &stat, STATFLAG_NONAME );
388 FIXME("Failed to stat storage\n");
392 if ( !IsEqualGUID( &stat.clsid, &CLSID_MsiDatabase ) &&
393 !IsEqualGUID( &stat.clsid, &CLSID_MsiPatch ) &&
394 !IsEqualGUID( &stat.clsid, &CLSID_MsiTransform ) )
396 ERR("storage GUID is not a MSI database GUID %s\n",
397 debugstr_guid(&stat.clsid) );
401 if ( patch && !IsEqualGUID( &stat.clsid, &CLSID_MsiPatch ) )
403 ERR("storage GUID is not the MSI patch GUID %s\n",
404 debugstr_guid(&stat.clsid) );
405 ret = ERROR_OPEN_FAILED;
409 db = alloc_msiobject( MSIHANDLETYPE_DATABASE, sizeof (MSIDATABASE),
413 FIXME("Failed to allocate a handle\n");
417 if (!strchrW( save_path, '\\' ))
419 GetCurrentDirectoryW( MAX_PATH, path );
420 lstrcatW( path, szBackSlash );
421 lstrcatW( path, save_path );
424 lstrcpyW( path, save_path );
426 db->path = strdupW( path );
427 db->media_transform_offset = MSI_INITIAL_MEDIA_TRANSFORM_OFFSET;
428 db->media_transform_disk_id = MSI_INITIAL_MEDIA_TRANSFORM_DISKID;
430 if( TRACE_ON( msi ) )
431 enum_stream_names( stg );
436 db->deletefile = strdupW( szDBPath );
437 list_init( &db->tables );
438 list_init( &db->transforms );
439 list_init( &db->streams );
441 db->strings = msi_load_string_table( stg, &db->bytes_per_strref );
447 msiobj_addref( &db->hdr );
448 IStorage_AddRef( stg );
453 msiobj_release( &db->hdr );
455 IStorage_Release( stg );
460 UINT WINAPI MsiOpenDatabaseW(LPCWSTR szDBPath, LPCWSTR szPersist, MSIHANDLE *phDB)
465 TRACE("%s %s %p\n",debugstr_w(szDBPath),debugstr_w(szPersist), phDB);
467 ret = MSI_OpenDatabaseW( szDBPath, szPersist, &db );
468 if( ret == ERROR_SUCCESS )
470 *phDB = alloc_msihandle( &db->hdr );
472 ret = ERROR_NOT_ENOUGH_MEMORY;
473 msiobj_release( &db->hdr );
479 UINT WINAPI MsiOpenDatabaseA(LPCSTR szDBPath, LPCSTR szPersist, MSIHANDLE *phDB)
481 HRESULT r = ERROR_FUNCTION_FAILED;
482 LPWSTR szwDBPath = NULL, szwPersist = NULL;
484 TRACE("%s %s %p\n", debugstr_a(szDBPath), debugstr_a(szPersist), phDB);
488 szwDBPath = strdupAtoW( szDBPath );
493 if( !IS_INTMSIDBOPEN(szPersist) )
495 szwPersist = strdupAtoW( szPersist );
500 szwPersist = (LPWSTR)(DWORD_PTR)szPersist;
502 r = MsiOpenDatabaseW( szwDBPath, szwPersist, phDB );
505 if( !IS_INTMSIDBOPEN(szPersist) )
506 msi_free( szwPersist );
507 msi_free( szwDBPath );
512 static LPWSTR msi_read_text_archive(LPCWSTR path)
517 DWORD read, size = 0;
519 file = CreateFileW( path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL );
520 if (file == INVALID_HANDLE_VALUE)
523 size = GetFileSize( file, NULL );
524 data = msi_alloc( size + 1 );
528 if (!ReadFile( file, data, size, &read, NULL ))
532 wdata = strdupAtoW( data );
540 static void msi_parse_line(LPWSTR *line, LPWSTR **entries, DWORD *num_entries)
542 LPWSTR ptr = *line, save;
547 /* stay on this line */
548 while (*ptr && *ptr != '\n')
550 /* entries are separated by tabs */
557 *entries = msi_alloc(count * sizeof(LPWSTR));
561 /* store pointers into the data */
562 for (i = 0, ptr = *line; i < count; i++)
564 while (*ptr && *ptr == '\r') ptr++;
567 while (*ptr && *ptr != '\t' && *ptr != '\n' && *ptr != '\r') ptr++;
569 /* NULL-separate the data */
570 if (*ptr == '\n' || *ptr == '\r')
572 while (*ptr == '\n' || *ptr == '\r')
578 (*entries)[i] = save;
581 /* move to the next line if there's more, else EOF */
585 *num_entries = count;
588 static LPWSTR msi_build_createsql_prelude(LPWSTR table)
593 static const WCHAR create_fmt[] = {'C','R','E','A','T','E',' ','T','A','B','L','E',' ','`','%','s','`',' ','(',' ',0};
595 size = sizeof(create_fmt)/sizeof(create_fmt[0]) + lstrlenW(table) - 2;
596 prelude = msi_alloc(size * sizeof(WCHAR));
600 sprintfW(prelude, create_fmt, table);
604 static LPWSTR msi_build_createsql_columns(LPWSTR *columns_data, LPWSTR *types, DWORD num_columns)
608 DWORD sql_size = 1, i, len;
609 WCHAR expanded[128], *ptr;
610 WCHAR size[10], comma[2], extra[30];
612 static const WCHAR column_fmt[] = {'`','%','s','`',' ','%','s','%','s','%','s','%','s',' ',0};
613 static const WCHAR size_fmt[] = {'(','%','s',')',0};
614 static const WCHAR type_char[] = {'C','H','A','R',0};
615 static const WCHAR type_int[] = {'I','N','T',0};
616 static const WCHAR type_long[] = {'L','O','N','G',0};
617 static const WCHAR type_object[] = {'O','B','J','E','C','T',0};
618 static const WCHAR type_notnull[] = {' ','N','O','T',' ','N','U','L','L',0};
619 static const WCHAR localizable[] = {' ','L','O','C','A','L','I','Z','A','B','L','E',0};
621 columns = msi_alloc_zero(sql_size * sizeof(WCHAR));
625 for (i = 0; i < num_columns; i++)
628 comma[1] = size[0] = extra[0] = '\0';
630 if (i == num_columns - 1)
642 lstrcpyW(extra, type_notnull);
644 lstrcatW(extra, localizable);
646 sprintfW(size, size_fmt, ptr);
649 lstrcpyW(extra, type_notnull);
652 sprintfW(size, size_fmt, ptr);
655 lstrcpyW(extra, type_notnull);
663 WARN("invalid int width %u\n", len);
669 lstrcpyW(extra, type_notnull);
674 ERR("Unknown type: %c\n", types[i][0]);
679 sprintfW(expanded, column_fmt, columns_data[i], type, size, extra, comma);
680 sql_size += lstrlenW(expanded);
682 p = msi_realloc(columns, sql_size * sizeof(WCHAR));
690 lstrcatW(columns, expanded);
696 static LPWSTR msi_build_createsql_postlude(LPWSTR *primary_keys, DWORD num_keys)
698 LPWSTR postlude, keys, ptr;
699 DWORD size, key_size, i;
701 static const WCHAR key_fmt[] = {'`','%','s','`',',',' ',0};
702 static const WCHAR postlude_fmt[] = {'P','R','I','M','A','R','Y',' ','K','E','Y',' ','%','s',')',0};
704 for (i = 0, size = 1; i < num_keys; i++)
705 size += lstrlenW(key_fmt) + lstrlenW(primary_keys[i]) - 2;
707 keys = msi_alloc(size * sizeof(WCHAR));
711 for (i = 0, ptr = keys; i < num_keys; i++)
713 key_size = lstrlenW(key_fmt) + lstrlenW(primary_keys[i]) -2;
714 sprintfW(ptr, key_fmt, primary_keys[i]);
718 /* remove final ', ' */
721 size = lstrlenW(postlude_fmt) + size - 1;
722 postlude = msi_alloc(size * sizeof(WCHAR));
726 sprintfW(postlude, postlude_fmt, keys);
733 static UINT msi_add_table_to_db(MSIDATABASE *db, LPWSTR *columns, LPWSTR *types, LPWSTR *labels, DWORD num_labels, DWORD num_columns)
735 UINT r = ERROR_OUTOFMEMORY;
738 LPWSTR create_sql = NULL;
739 LPWSTR prelude, columns_sql, postlude;
741 prelude = msi_build_createsql_prelude(labels[0]);
742 columns_sql = msi_build_createsql_columns(columns, types, num_columns);
743 postlude = msi_build_createsql_postlude(labels + 1, num_labels - 1); /* skip over table name */
745 if (!prelude || !columns_sql || !postlude)
748 size = lstrlenW(prelude) + lstrlenW(columns_sql) + lstrlenW(postlude) + 1;
749 create_sql = msi_alloc(size * sizeof(WCHAR));
753 lstrcpyW(create_sql, prelude);
754 lstrcatW(create_sql, columns_sql);
755 lstrcatW(create_sql, postlude);
757 r = MSI_DatabaseOpenViewW( db, create_sql, &view );
758 if (r != ERROR_SUCCESS)
761 r = MSI_ViewExecute(view, NULL);
763 msiobj_release(&view->hdr);
767 msi_free(columns_sql);
769 msi_free(create_sql);
773 static LPWSTR msi_import_stream_filename(LPCWSTR path, LPCWSTR name)
776 LPWSTR fullname, ptr;
778 len = lstrlenW(path) + lstrlenW(name) + 1;
779 fullname = msi_alloc(len*sizeof(WCHAR));
783 lstrcpyW( fullname, path );
785 /* chop off extension from path */
786 ptr = strrchrW(fullname, '.');
793 lstrcpyW( ptr, name );
797 static UINT construct_record(DWORD num_columns, LPWSTR *types,
798 LPWSTR *data, LPWSTR path, MSIRECORD **rec)
802 *rec = MSI_CreateRecord(num_columns);
804 return ERROR_OUTOFMEMORY;
806 for (i = 0; i < num_columns; i++)
810 case 'L': case 'l': case 'S': case 's':
811 MSI_RecordSetStringW(*rec, i + 1, data[i]);
815 MSI_RecordSetInteger(*rec, i + 1, atoiW(data[i]));
821 LPWSTR file = msi_import_stream_filename(path, data[i]);
823 return ERROR_FUNCTION_FAILED;
825 r = MSI_RecordSetStreamFromFileW(*rec, i + 1, file);
827 if (r != ERROR_SUCCESS)
828 return ERROR_FUNCTION_FAILED;
832 ERR("Unhandled column type: %c\n", types[i][0]);
833 msiobj_release(&(*rec)->hdr);
834 return ERROR_FUNCTION_FAILED;
838 return ERROR_SUCCESS;
841 static UINT msi_add_records_to_table(MSIDATABASE *db, LPWSTR *columns, LPWSTR *types,
842 LPWSTR *labels, LPWSTR **records,
843 int num_columns, int num_records,
851 static const WCHAR select[] = {
852 'S','E','L','E','C','T',' ','*',' ',
853 'F','R','O','M',' ','`','%','s','`',0
856 r = MSI_OpenQuery(db, &view, select, labels[0]);
857 if (r != ERROR_SUCCESS)
860 while (MSI_ViewFetch(view, &rec) != ERROR_NO_MORE_ITEMS)
862 r = MSI_ViewModify(view, MSIMODIFY_DELETE, rec);
863 msiobj_release(&rec->hdr);
864 if (r != ERROR_SUCCESS)
868 for (i = 0; i < num_records; i++)
870 r = construct_record(num_columns, types, records[i], path, &rec);
871 if (r != ERROR_SUCCESS)
874 r = MSI_ViewModify(view, MSIMODIFY_INSERT, rec);
875 if (r != ERROR_SUCCESS)
877 msiobj_release(&rec->hdr);
881 msiobj_release(&rec->hdr);
885 msiobj_release(&view->hdr);
889 static UINT MSI_DatabaseImport(MSIDATABASE *db, LPCWSTR folder, LPCWSTR file)
893 DWORD num_labels, num_types;
894 DWORD num_columns, num_records = 0;
895 LPWSTR *columns, *types, *labels;
896 LPWSTR path, ptr, data;
897 LPWSTR **records = NULL;
898 LPWSTR **temp_records;
900 static const WCHAR suminfo[] =
901 {'_','S','u','m','m','a','r','y','I','n','f','o','r','m','a','t','i','o','n',0};
902 static const WCHAR forcecodepage[] =
903 {'_','F','o','r','c','e','C','o','d','e','p','a','g','e',0};
905 TRACE("%p %s %s\n", db, debugstr_w(folder), debugstr_w(file) );
907 if( folder == NULL || file == NULL )
908 return ERROR_INVALID_PARAMETER;
910 len = lstrlenW(folder) + lstrlenW(szBackSlash) + lstrlenW(file) + 1;
911 path = msi_alloc( len * sizeof(WCHAR) );
913 return ERROR_OUTOFMEMORY;
915 lstrcpyW( path, folder );
916 lstrcatW( path, szBackSlash );
917 lstrcatW( path, file );
919 data = msi_read_text_archive( path );
922 msi_parse_line( &ptr, &columns, &num_columns );
923 msi_parse_line( &ptr, &types, &num_types );
924 msi_parse_line( &ptr, &labels, &num_labels );
926 if (num_columns == 1 && !columns[0][0] && num_labels == 1 && !labels[0][0] &&
927 num_types == 2 && !strcmpW( types[1], forcecodepage ))
929 r = msi_set_string_table_codepage( db->strings, atoiW( types[0] ) );
933 if (num_columns != num_types)
935 r = ERROR_FUNCTION_FAILED;
939 records = msi_alloc(sizeof(LPWSTR *));
942 r = ERROR_OUTOFMEMORY;
946 /* read in the table records */
949 msi_parse_line( &ptr, &records[num_records], NULL );
952 temp_records = msi_realloc(records, (num_records + 1) * sizeof(LPWSTR *));
955 r = ERROR_OUTOFMEMORY;
958 records = temp_records;
961 if (!strcmpW(labels[0], suminfo))
963 r = msi_add_suminfo( db, records, num_records, num_columns );
964 if (r != ERROR_SUCCESS)
966 r = ERROR_FUNCTION_FAILED;
972 if (!TABLE_Exists(db, labels[0]))
974 r = msi_add_table_to_db( db, columns, types, labels, num_labels, num_columns );
975 if (r != ERROR_SUCCESS)
977 r = ERROR_FUNCTION_FAILED;
982 r = msi_add_records_to_table( db, columns, types, labels, records, num_columns, num_records, path );
992 for (i = 0; i < num_records; i++)
993 msi_free(records[i]);
1000 UINT WINAPI MsiDatabaseImportW(MSIHANDLE handle, LPCWSTR szFolder, LPCWSTR szFilename)
1005 TRACE("%x %s %s\n",handle,debugstr_w(szFolder), debugstr_w(szFilename));
1007 db = msihandle2msiinfo( handle, MSIHANDLETYPE_DATABASE );
1010 IWineMsiRemoteDatabase *remote_database;
1012 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( handle );
1013 if ( !remote_database )
1014 return ERROR_INVALID_HANDLE;
1016 IWineMsiRemoteDatabase_Release( remote_database );
1017 WARN("MsiDatabaseImport not allowed during a custom action!\n");
1019 return ERROR_SUCCESS;
1022 r = MSI_DatabaseImport( db, szFolder, szFilename );
1023 msiobj_release( &db->hdr );
1027 UINT WINAPI MsiDatabaseImportA( MSIHANDLE handle,
1028 LPCSTR szFolder, LPCSTR szFilename )
1030 LPWSTR path = NULL, file = NULL;
1031 UINT r = ERROR_OUTOFMEMORY;
1033 TRACE("%x %s %s\n", handle, debugstr_a(szFolder), debugstr_a(szFilename));
1037 path = strdupAtoW( szFolder );
1044 file = strdupAtoW( szFilename );
1049 r = MsiDatabaseImportW( handle, path, file );
1058 static UINT msi_export_record( HANDLE handle, MSIRECORD *row, UINT start )
1060 UINT i, count, len, r = ERROR_SUCCESS;
1066 buffer = msi_alloc( len );
1068 return ERROR_OUTOFMEMORY;
1070 count = MSI_RecordGetFieldCount( row );
1071 for ( i=start; i<=count; i++ )
1074 r = MSI_RecordGetStringA( row, i, buffer, &sz );
1075 if (r == ERROR_MORE_DATA)
1077 char *p = msi_realloc( buffer, sz + 1 );
1084 r = MSI_RecordGetStringA( row, i, buffer, &sz );
1085 if (r != ERROR_SUCCESS)
1088 if (!WriteFile( handle, buffer, sz, &sz, NULL ))
1090 r = ERROR_FUNCTION_FAILED;
1094 sep = (i < count) ? "\t" : "\r\n";
1095 if (!WriteFile( handle, sep, strlen(sep), &sz, NULL ))
1097 r = ERROR_FUNCTION_FAILED;
1105 static UINT msi_export_row( MSIRECORD *row, void *arg )
1107 return msi_export_record( arg, row, 1 );
1110 static UINT msi_export_forcecodepage( HANDLE handle, UINT codepage )
1112 static const char fmt[] = "\r\n\r\n%u\t_ForceCodepage\r\n";
1113 char data[sizeof(fmt) + 10];
1116 sprintf( data, fmt, codepage );
1118 sz = lstrlenA(data) + 1;
1119 if (!WriteFile(handle, data, sz, &sz, NULL))
1120 return ERROR_FUNCTION_FAILED;
1122 return ERROR_SUCCESS;
1125 static UINT MSI_DatabaseExport( MSIDATABASE *db, LPCWSTR table,
1126 LPCWSTR folder, LPCWSTR file )
1128 static const WCHAR query[] = {
1129 's','e','l','e','c','t',' ','*',' ','f','r','o','m',' ','%','s',0 };
1130 static const WCHAR forcecodepage[] = {
1131 '_','F','o','r','c','e','C','o','d','e','p','a','g','e',0 };
1132 MSIRECORD *rec = NULL;
1133 MSIQUERY *view = NULL;
1138 TRACE("%p %s %s %s\n", db, debugstr_w(table),
1139 debugstr_w(folder), debugstr_w(file) );
1141 if( folder == NULL || file == NULL )
1142 return ERROR_INVALID_PARAMETER;
1144 len = lstrlenW(folder) + lstrlenW(file) + 2;
1145 filename = msi_alloc(len * sizeof (WCHAR));
1147 return ERROR_OUTOFMEMORY;
1149 lstrcpyW( filename, folder );
1150 lstrcatW( filename, szBackSlash );
1151 lstrcatW( filename, file );
1153 handle = CreateFileW( filename, GENERIC_READ | GENERIC_WRITE, 0,
1154 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
1155 msi_free( filename );
1156 if (handle == INVALID_HANDLE_VALUE)
1157 return ERROR_FUNCTION_FAILED;
1159 if (!strcmpW( table, forcecodepage ))
1161 UINT codepage = msi_get_string_table_codepage( db->strings );
1162 r = msi_export_forcecodepage( handle, codepage );
1166 r = MSI_OpenQuery( db, &view, query, table );
1167 if (r == ERROR_SUCCESS)
1169 /* write out row 1, the column names */
1170 r = MSI_ViewGetColumnInfo(view, MSICOLINFO_NAMES, &rec);
1171 if (r == ERROR_SUCCESS)
1173 msi_export_record( handle, rec, 1 );
1174 msiobj_release( &rec->hdr );
1177 /* write out row 2, the column types */
1178 r = MSI_ViewGetColumnInfo(view, MSICOLINFO_TYPES, &rec);
1179 if (r == ERROR_SUCCESS)
1181 msi_export_record( handle, rec, 1 );
1182 msiobj_release( &rec->hdr );
1185 /* write out row 3, the table name + keys */
1186 r = MSI_DatabaseGetPrimaryKeys( db, table, &rec );
1187 if (r == ERROR_SUCCESS)
1189 MSI_RecordSetStringW( rec, 0, table );
1190 msi_export_record( handle, rec, 0 );
1191 msiobj_release( &rec->hdr );
1194 /* write out row 4 onwards, the data */
1195 r = MSI_IterateRecords( view, 0, msi_export_row, handle );
1196 msiobj_release( &view->hdr );
1200 CloseHandle( handle );
1204 /***********************************************************************
1205 * MsiExportDatabaseW [MSI.@]
1207 * Writes a file containing the table data as tab separated ASCII.
1209 * The format is as follows:
1211 * row1 : colname1 <tab> colname2 <tab> .... colnameN <cr> <lf>
1212 * row2 : coltype1 <tab> coltype2 <tab> .... coltypeN <cr> <lf>
1213 * row3 : tablename <tab> key1 <tab> key2 <tab> ... keyM <cr> <lf>
1215 * Followed by the data, starting at row 1 with one row per line
1217 * row4 : data <tab> data <tab> data <tab> ... data <cr> <lf>
1219 UINT WINAPI MsiDatabaseExportW( MSIHANDLE handle, LPCWSTR szTable,
1220 LPCWSTR szFolder, LPCWSTR szFilename )
1225 TRACE("%x %s %s %s\n", handle, debugstr_w(szTable),
1226 debugstr_w(szFolder), debugstr_w(szFilename));
1228 db = msihandle2msiinfo( handle, MSIHANDLETYPE_DATABASE );
1231 IWineMsiRemoteDatabase *remote_database;
1233 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( handle );
1234 if ( !remote_database )
1235 return ERROR_INVALID_HANDLE;
1237 IWineMsiRemoteDatabase_Release( remote_database );
1238 WARN("MsiDatabaseExport not allowed during a custom action!\n");
1240 return ERROR_SUCCESS;
1243 r = MSI_DatabaseExport( db, szTable, szFolder, szFilename );
1244 msiobj_release( &db->hdr );
1248 UINT WINAPI MsiDatabaseExportA( MSIHANDLE handle, LPCSTR szTable,
1249 LPCSTR szFolder, LPCSTR szFilename )
1251 LPWSTR path = NULL, file = NULL, table = NULL;
1252 UINT r = ERROR_OUTOFMEMORY;
1254 TRACE("%x %s %s %s\n", handle, debugstr_a(szTable),
1255 debugstr_a(szFolder), debugstr_a(szFilename));
1259 table = strdupAtoW( szTable );
1266 path = strdupAtoW( szFolder );
1273 file = strdupAtoW( szFilename );
1278 r = MsiDatabaseExportW( handle, table, path, file );
1288 UINT WINAPI MsiDatabaseMergeA(MSIHANDLE hDatabase, MSIHANDLE hDatabaseMerge,
1294 TRACE("(%d, %d, %s)\n", hDatabase, hDatabaseMerge,
1295 debugstr_a(szTableName));
1297 table = strdupAtoW(szTableName);
1298 r = MsiDatabaseMergeW(hDatabase, hDatabaseMerge, table);
1304 typedef struct _tagMERGETABLE
1318 typedef struct _tagMERGEROW
1324 typedef struct _tagMERGEDATA
1328 MERGETABLE *curtable;
1330 struct list *tabledata;
1333 static BOOL merge_type_match(LPCWSTR type1, LPCWSTR type2)
1335 if (((type1[0] == 'l') || (type1[0] == 's')) &&
1336 ((type2[0] == 'l') || (type2[0] == 's')))
1339 if (((type1[0] == 'L') || (type1[0] == 'S')) &&
1340 ((type2[0] == 'L') || (type2[0] == 'S')))
1343 return !strcmpW( type1, type2 );
1346 static UINT merge_verify_colnames(MSIQUERY *dbview, MSIQUERY *mergeview)
1348 MSIRECORD *dbrec, *mergerec;
1351 r = MSI_ViewGetColumnInfo(dbview, MSICOLINFO_NAMES, &dbrec);
1352 if (r != ERROR_SUCCESS)
1355 r = MSI_ViewGetColumnInfo(mergeview, MSICOLINFO_NAMES, &mergerec);
1356 if (r != ERROR_SUCCESS)
1359 count = MSI_RecordGetFieldCount(dbrec);
1360 for (i = 1; i <= count; i++)
1362 if (!MSI_RecordGetString(mergerec, i))
1365 if (strcmpW( MSI_RecordGetString( dbrec, i ), MSI_RecordGetString( mergerec, i ) ))
1367 r = ERROR_DATATYPE_MISMATCH;
1372 msiobj_release(&dbrec->hdr);
1373 msiobj_release(&mergerec->hdr);
1374 dbrec = mergerec = NULL;
1376 r = MSI_ViewGetColumnInfo(dbview, MSICOLINFO_TYPES, &dbrec);
1377 if (r != ERROR_SUCCESS)
1380 r = MSI_ViewGetColumnInfo(mergeview, MSICOLINFO_TYPES, &mergerec);
1381 if (r != ERROR_SUCCESS)
1384 count = MSI_RecordGetFieldCount(dbrec);
1385 for (i = 1; i <= count; i++)
1387 if (!MSI_RecordGetString(mergerec, i))
1390 if (!merge_type_match(MSI_RecordGetString(dbrec, i),
1391 MSI_RecordGetString(mergerec, i)))
1393 r = ERROR_DATATYPE_MISMATCH;
1399 msiobj_release(&dbrec->hdr);
1400 msiobj_release(&mergerec->hdr);
1405 static UINT merge_verify_primary_keys(MSIDATABASE *db, MSIDATABASE *mergedb,
1408 MSIRECORD *dbrec, *mergerec = NULL;
1411 r = MSI_DatabaseGetPrimaryKeys(db, table, &dbrec);
1412 if (r != ERROR_SUCCESS)
1415 r = MSI_DatabaseGetPrimaryKeys(mergedb, table, &mergerec);
1416 if (r != ERROR_SUCCESS)
1419 count = MSI_RecordGetFieldCount(dbrec);
1420 if (count != MSI_RecordGetFieldCount(mergerec))
1422 r = ERROR_DATATYPE_MISMATCH;
1426 for (i = 1; i <= count; i++)
1428 if (strcmpW( MSI_RecordGetString( dbrec, i ), MSI_RecordGetString( mergerec, i ) ))
1430 r = ERROR_DATATYPE_MISMATCH;
1436 msiobj_release(&dbrec->hdr);
1437 msiobj_release(&mergerec->hdr);
1442 static LPWSTR get_key_value(MSIQUERY *view, LPCWSTR key, MSIRECORD *rec)
1444 MSIRECORD *colnames;
1446 UINT r, i = 0, sz = 0;
1449 r = MSI_ViewGetColumnInfo(view, MSICOLINFO_NAMES, &colnames);
1450 if (r != ERROR_SUCCESS)
1455 str = msi_dup_record_field(colnames, ++i);
1456 cmp = strcmpW( key, str );
1460 msiobj_release(&colnames->hdr);
1462 r = MSI_RecordGetStringW(rec, i, NULL, &sz);
1463 if (r != ERROR_SUCCESS)
1467 if (MSI_RecordGetString(rec, i)) /* check record field is a string */
1469 /* quote string record fields */
1470 const WCHAR szQuote[] = {'\'', 0};
1472 val = msi_alloc(sz*sizeof(WCHAR));
1476 lstrcpyW(val, szQuote);
1477 r = MSI_RecordGetStringW(rec, i, val+1, &sz);
1478 lstrcpyW(val+1+sz, szQuote);
1482 /* do not quote integer record fields */
1483 val = msi_alloc(sz*sizeof(WCHAR));
1487 r = MSI_RecordGetStringW(rec, i, val, &sz);
1490 if (r != ERROR_SUCCESS)
1492 ERR("failed to get string!\n");
1500 static LPWSTR create_diff_row_query(MSIDATABASE *merge, MSIQUERY *view,
1501 LPWSTR table, MSIRECORD *rec)
1503 LPWSTR query = NULL, clause = NULL;
1504 LPWSTR ptr = NULL, val;
1506 DWORD size = 1, oldsize;
1511 static const WCHAR keyset[] = {
1512 '`','%','s','`',' ','=',' ','%','s',' ','A','N','D',' ',0};
1513 static const WCHAR lastkeyset[] = {
1514 '`','%','s','`',' ','=',' ','%','s',' ',0};
1515 static const WCHAR fmt[] = {'S','E','L','E','C','T',' ','*',' ',
1516 'F','R','O','M',' ','`','%','s','`',' ',
1517 'W','H','E','R','E',' ','%','s',0};
1519 r = MSI_DatabaseGetPrimaryKeys(merge, table, &keys);
1520 if (r != ERROR_SUCCESS)
1523 clause = msi_alloc_zero(size * sizeof(WCHAR));
1528 count = MSI_RecordGetFieldCount(keys);
1529 for (i = 1; i <= count; i++)
1531 key = MSI_RecordGetString(keys, i);
1532 val = get_key_value(view, key, rec);
1535 setptr = lastkeyset;
1540 size += lstrlenW(setptr) + lstrlenW(key) + lstrlenW(val) - 4;
1541 clause = msi_realloc(clause, size * sizeof (WCHAR));
1548 ptr = clause + oldsize - 1;
1549 sprintfW(ptr, setptr, key, val);
1553 size = lstrlenW(fmt) + lstrlenW(table) + lstrlenW(clause) + 1;
1554 query = msi_alloc(size * sizeof(WCHAR));
1558 sprintfW(query, fmt, table, clause);
1562 msiobj_release(&keys->hdr);
1566 static UINT merge_diff_row(MSIRECORD *rec, LPVOID param)
1568 MERGEDATA *data = param;
1569 MERGETABLE *table = data->curtable;
1571 MSIQUERY *dbview = NULL;
1572 MSIRECORD *row = NULL;
1573 LPWSTR query = NULL;
1574 UINT r = ERROR_SUCCESS;
1576 if (TABLE_Exists(data->db, table->name))
1578 query = create_diff_row_query(data->merge, data->curview, table->name, rec);
1580 return ERROR_OUTOFMEMORY;
1582 r = MSI_DatabaseOpenViewW(data->db, query, &dbview);
1583 if (r != ERROR_SUCCESS)
1586 r = MSI_ViewExecute(dbview, NULL);
1587 if (r != ERROR_SUCCESS)
1590 r = MSI_ViewFetch(dbview, &row);
1591 if (r == ERROR_SUCCESS && !MSI_RecordsAreEqual(rec, row))
1593 table->numconflicts++;
1596 else if (r != ERROR_NO_MORE_ITEMS)
1602 mergerow = msi_alloc(sizeof(MERGEROW));
1605 r = ERROR_OUTOFMEMORY;
1609 mergerow->data = MSI_CloneRecord(rec);
1610 if (!mergerow->data)
1612 r = ERROR_OUTOFMEMORY;
1617 list_add_tail(&table->rows, &mergerow->entry);
1621 msiobj_release(&row->hdr);
1622 msiobj_release(&dbview->hdr);
1626 static UINT msi_get_table_labels(MSIDATABASE *db, LPCWSTR table, LPWSTR **labels, DWORD *numlabels)
1629 MSIRECORD *prec = NULL;
1631 r = MSI_DatabaseGetPrimaryKeys(db, table, &prec);
1632 if (r != ERROR_SUCCESS)
1635 count = MSI_RecordGetFieldCount(prec);
1636 *numlabels = count + 1;
1637 *labels = msi_alloc((*numlabels)*sizeof(LPWSTR));
1640 r = ERROR_OUTOFMEMORY;
1644 (*labels)[0] = strdupW(table);
1645 for (i=1; i<=count; i++ )
1647 (*labels)[i] = strdupW(MSI_RecordGetString(prec, i));
1651 msiobj_release( &prec->hdr );
1655 static UINT msi_get_query_columns(MSIQUERY *query, LPWSTR **columns, DWORD *numcolumns)
1658 MSIRECORD *prec = NULL;
1660 r = MSI_ViewGetColumnInfo(query, MSICOLINFO_NAMES, &prec);
1661 if (r != ERROR_SUCCESS)
1664 count = MSI_RecordGetFieldCount(prec);
1665 *columns = msi_alloc(count*sizeof(LPWSTR));
1668 r = ERROR_OUTOFMEMORY;
1672 for (i=1; i<=count; i++ )
1674 (*columns)[i-1] = strdupW(MSI_RecordGetString(prec, i));
1677 *numcolumns = count;
1680 msiobj_release( &prec->hdr );
1684 static UINT msi_get_query_types(MSIQUERY *query, LPWSTR **types, DWORD *numtypes)
1687 MSIRECORD *prec = NULL;
1689 r = MSI_ViewGetColumnInfo(query, MSICOLINFO_TYPES, &prec);
1690 if (r != ERROR_SUCCESS)
1693 count = MSI_RecordGetFieldCount(prec);
1694 *types = msi_alloc(count*sizeof(LPWSTR));
1697 r = ERROR_OUTOFMEMORY;
1702 for (i=1; i<=count; i++ )
1704 (*types)[i-1] = strdupW(MSI_RecordGetString(prec, i));
1708 msiobj_release( &prec->hdr );
1712 static void merge_free_rows(MERGETABLE *table)
1714 struct list *item, *cursor;
1716 LIST_FOR_EACH_SAFE(item, cursor, &table->rows)
1718 MERGEROW *row = LIST_ENTRY(item, MERGEROW, entry);
1720 list_remove(&row->entry);
1721 msiobj_release(&row->data->hdr);
1726 static void free_merge_table(MERGETABLE *table)
1730 if (table->labels != NULL)
1732 for (i = 0; i < table->numlabels; i++)
1733 msi_free(table->labels[i]);
1735 msi_free(table->labels);
1738 if (table->columns != NULL)
1740 for (i = 0; i < table->numcolumns; i++)
1741 msi_free(table->columns[i]);
1743 msi_free(table->columns);
1746 if (table->types != NULL)
1748 for (i = 0; i < table->numtypes; i++)
1749 msi_free(table->types[i]);
1751 msi_free(table->types);
1754 msi_free(table->name);
1755 merge_free_rows(table);
1760 static UINT msi_get_merge_table (MSIDATABASE *db, LPCWSTR name, MERGETABLE **ptable)
1764 MSIQUERY *mergeview = NULL;
1766 static const WCHAR query[] = {'S','E','L','E','C','T',' ','*',' ',
1767 'F','R','O','M',' ','`','%','s','`',0};
1769 table = msi_alloc_zero(sizeof(MERGETABLE));
1773 return ERROR_OUTOFMEMORY;
1776 r = msi_get_table_labels(db, name, &table->labels, &table->numlabels);
1777 if (r != ERROR_SUCCESS)
1780 r = MSI_OpenQuery(db, &mergeview, query, name);
1781 if (r != ERROR_SUCCESS)
1784 r = msi_get_query_columns(mergeview, &table->columns, &table->numcolumns);
1785 if (r != ERROR_SUCCESS)
1788 r = msi_get_query_types(mergeview, &table->types, &table->numtypes);
1789 if (r != ERROR_SUCCESS)
1792 list_init(&table->rows);
1794 table->name = strdupW(name);
1795 table->numconflicts = 0;
1797 msiobj_release(&mergeview->hdr);
1799 return ERROR_SUCCESS;
1802 msiobj_release(&mergeview->hdr);
1803 free_merge_table(table);
1808 static UINT merge_diff_tables(MSIRECORD *rec, LPVOID param)
1810 MERGEDATA *data = param;
1812 MSIQUERY *dbview = NULL;
1813 MSIQUERY *mergeview = NULL;
1817 static const WCHAR query[] = {'S','E','L','E','C','T',' ','*',' ',
1818 'F','R','O','M',' ','`','%','s','`',0};
1820 name = MSI_RecordGetString(rec, 1);
1822 r = MSI_OpenQuery(data->merge, &mergeview, query, name);
1823 if (r != ERROR_SUCCESS)
1826 if (TABLE_Exists(data->db, name))
1828 r = MSI_OpenQuery(data->db, &dbview, query, name);
1829 if (r != ERROR_SUCCESS)
1832 r = merge_verify_colnames(dbview, mergeview);
1833 if (r != ERROR_SUCCESS)
1836 r = merge_verify_primary_keys(data->db, data->merge, name);
1837 if (r != ERROR_SUCCESS)
1841 r = msi_get_merge_table(data->merge, name, &table);
1842 if (r != ERROR_SUCCESS)
1845 data->curtable = table;
1846 data->curview = mergeview;
1847 r = MSI_IterateRecords(mergeview, NULL, merge_diff_row, data);
1848 if (r != ERROR_SUCCESS)
1850 free_merge_table(table);
1854 list_add_tail(data->tabledata, &table->entry);
1857 msiobj_release(&dbview->hdr);
1858 msiobj_release(&mergeview->hdr);
1862 static UINT gather_merge_data(MSIDATABASE *db, MSIDATABASE *merge,
1863 struct list *tabledata)
1869 static const WCHAR query[] = {'S','E','L','E','C','T',' ','*',' ',
1870 'F','R','O','M',' ','`','_','T','a','b','l','e','s','`',0};
1872 r = MSI_DatabaseOpenViewW(merge, query, &view);
1873 if (r != ERROR_SUCCESS)
1878 data.tabledata = tabledata;
1879 r = MSI_IterateRecords(view, NULL, merge_diff_tables, &data);
1881 msiobj_release(&view->hdr);
1885 static UINT merge_table(MSIDATABASE *db, MERGETABLE *table)
1891 if (!TABLE_Exists(db, table->name))
1893 r = msi_add_table_to_db(db, table->columns, table->types,
1894 table->labels, table->numlabels, table->numcolumns);
1895 if (r != ERROR_SUCCESS)
1896 return ERROR_FUNCTION_FAILED;
1899 LIST_FOR_EACH_ENTRY(row, &table->rows, MERGEROW, entry)
1901 r = TABLE_CreateView(db, table->name, &tv);
1902 if (r != ERROR_SUCCESS)
1905 r = tv->ops->insert_row(tv, row->data, -1, FALSE);
1906 tv->ops->delete(tv);
1908 if (r != ERROR_SUCCESS)
1912 return ERROR_SUCCESS;
1915 static UINT update_merge_errors(MSIDATABASE *db, LPCWSTR error,
1916 LPWSTR table, DWORD numconflicts)
1921 static const WCHAR create[] = {
1922 'C','R','E','A','T','E',' ','T','A','B','L','E',' ',
1923 '`','%','s','`',' ','(','`','T','a','b','l','e','`',' ',
1924 'C','H','A','R','(','2','5','5',')',' ','N','O','T',' ',
1925 'N','U','L','L',',',' ','`','N','u','m','R','o','w','M','e','r','g','e',
1926 'C','o','n','f','l','i','c','t','s','`',' ','S','H','O','R','T',' ',
1927 'N','O','T',' ','N','U','L','L',' ','P','R','I','M','A','R','Y',' ',
1928 'K','E','Y',' ','`','T','a','b','l','e','`',')',0};
1929 static const WCHAR insert[] = {
1930 'I','N','S','E','R','T',' ','I','N','T','O',' ',
1931 '`','%','s','`',' ','(','`','T','a','b','l','e','`',',',' ',
1932 '`','N','u','m','R','o','w','M','e','r','g','e',
1933 'C','o','n','f','l','i','c','t','s','`',')',' ','V','A','L','U','E','S',
1934 ' ','(','\'','%','s','\'',',',' ','%','d',')',0};
1936 if (!TABLE_Exists(db, error))
1938 r = MSI_OpenQuery(db, &view, create, error);
1939 if (r != ERROR_SUCCESS)
1942 r = MSI_ViewExecute(view, NULL);
1943 msiobj_release(&view->hdr);
1944 if (r != ERROR_SUCCESS)
1948 r = MSI_OpenQuery(db, &view, insert, error, table, numconflicts);
1949 if (r != ERROR_SUCCESS)
1952 r = MSI_ViewExecute(view, NULL);
1953 msiobj_release(&view->hdr);
1957 UINT WINAPI MsiDatabaseMergeW(MSIHANDLE hDatabase, MSIHANDLE hDatabaseMerge,
1958 LPCWSTR szTableName)
1960 struct list tabledata = LIST_INIT(tabledata);
1961 struct list *item, *cursor;
1962 MSIDATABASE *db, *merge;
1967 TRACE("(%d, %d, %s)\n", hDatabase, hDatabaseMerge,
1968 debugstr_w(szTableName));
1970 if (szTableName && !*szTableName)
1971 return ERROR_INVALID_TABLE;
1973 db = msihandle2msiinfo(hDatabase, MSIHANDLETYPE_DATABASE);
1974 merge = msihandle2msiinfo(hDatabaseMerge, MSIHANDLETYPE_DATABASE);
1977 r = ERROR_INVALID_HANDLE;
1981 r = gather_merge_data(db, merge, &tabledata);
1982 if (r != ERROR_SUCCESS)
1986 LIST_FOR_EACH_ENTRY(table, &tabledata, MERGETABLE, entry)
1988 if (table->numconflicts)
1992 r = update_merge_errors(db, szTableName, table->name,
1993 table->numconflicts);
1994 if (r != ERROR_SUCCESS)
1999 r = merge_table(db, table);
2000 if (r != ERROR_SUCCESS)
2005 LIST_FOR_EACH_SAFE(item, cursor, &tabledata)
2007 MERGETABLE *table = LIST_ENTRY(item, MERGETABLE, entry);
2008 list_remove(&table->entry);
2009 free_merge_table(table);
2013 r = ERROR_FUNCTION_FAILED;
2016 msiobj_release(&db->hdr);
2017 msiobj_release(&merge->hdr);
2021 MSIDBSTATE WINAPI MsiGetDatabaseState( MSIHANDLE handle )
2023 MSIDBSTATE ret = MSIDBSTATE_READ;
2026 TRACE("%d\n", handle);
2028 db = msihandle2msiinfo( handle, MSIHANDLETYPE_DATABASE );
2031 IWineMsiRemoteDatabase *remote_database;
2033 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( handle );
2034 if ( !remote_database )
2035 return MSIDBSTATE_ERROR;
2037 IWineMsiRemoteDatabase_Release( remote_database );
2038 WARN("MsiGetDatabaseState not allowed during a custom action!\n");
2040 return MSIDBSTATE_READ;
2043 if (db->mode != MSIDBOPEN_READONLY )
2044 ret = MSIDBSTATE_WRITE;
2045 msiobj_release( &db->hdr );
2050 typedef struct _msi_remote_database_impl {
2051 IWineMsiRemoteDatabase IWineMsiRemoteDatabase_iface;
2054 } msi_remote_database_impl;
2056 static inline msi_remote_database_impl *impl_from_IWineMsiRemoteDatabase( IWineMsiRemoteDatabase *iface )
2058 return CONTAINING_RECORD(iface, msi_remote_database_impl, IWineMsiRemoteDatabase_iface);
2061 static HRESULT WINAPI mrd_QueryInterface( IWineMsiRemoteDatabase *iface,
2062 REFIID riid,LPVOID *ppobj)
2064 if( IsEqualCLSID( riid, &IID_IUnknown ) ||
2065 IsEqualCLSID( riid, &IID_IWineMsiRemoteDatabase ) )
2067 IUnknown_AddRef( iface );
2072 return E_NOINTERFACE;
2075 static ULONG WINAPI mrd_AddRef( IWineMsiRemoteDatabase *iface )
2077 msi_remote_database_impl* This = impl_from_IWineMsiRemoteDatabase( iface );
2079 return InterlockedIncrement( &This->refs );
2082 static ULONG WINAPI mrd_Release( IWineMsiRemoteDatabase *iface )
2084 msi_remote_database_impl* This = impl_from_IWineMsiRemoteDatabase( iface );
2087 r = InterlockedDecrement( &This->refs );
2090 MsiCloseHandle( This->database );
2096 static HRESULT WINAPI mrd_IsTablePersistent( IWineMsiRemoteDatabase *iface,
2097 LPCWSTR table, MSICONDITION *persistent )
2099 msi_remote_database_impl *This = impl_from_IWineMsiRemoteDatabase( iface );
2100 *persistent = MsiDatabaseIsTablePersistentW(This->database, table);
2104 static HRESULT WINAPI mrd_GetPrimaryKeys( IWineMsiRemoteDatabase *iface,
2105 LPCWSTR table, MSIHANDLE *keys )
2107 msi_remote_database_impl *This = impl_from_IWineMsiRemoteDatabase( iface );
2108 UINT r = MsiDatabaseGetPrimaryKeysW(This->database, table, keys);
2109 return HRESULT_FROM_WIN32(r);
2112 static HRESULT WINAPI mrd_GetSummaryInformation( IWineMsiRemoteDatabase *iface,
2113 UINT updatecount, MSIHANDLE *suminfo )
2115 msi_remote_database_impl *This = impl_from_IWineMsiRemoteDatabase( iface );
2116 UINT r = MsiGetSummaryInformationW(This->database, NULL, updatecount, suminfo);
2117 return HRESULT_FROM_WIN32(r);
2120 static HRESULT WINAPI mrd_OpenView( IWineMsiRemoteDatabase *iface,
2121 LPCWSTR query, MSIHANDLE *view )
2123 msi_remote_database_impl *This = impl_from_IWineMsiRemoteDatabase( iface );
2124 UINT r = MsiDatabaseOpenViewW(This->database, query, view);
2125 return HRESULT_FROM_WIN32(r);
2128 static HRESULT WINAPI mrd_SetMsiHandle( IWineMsiRemoteDatabase *iface, MSIHANDLE handle )
2130 msi_remote_database_impl* This = impl_from_IWineMsiRemoteDatabase( iface );
2131 This->database = handle;
2135 static const IWineMsiRemoteDatabaseVtbl msi_remote_database_vtbl =
2140 mrd_IsTablePersistent,
2142 mrd_GetSummaryInformation,
2147 HRESULT create_msi_remote_database( IUnknown *pOuter, LPVOID *ppObj )
2149 msi_remote_database_impl *This;
2151 This = msi_alloc( sizeof *This );
2153 return E_OUTOFMEMORY;
2155 This->IWineMsiRemoteDatabase_iface.lpVtbl = &msi_remote_database_vtbl;