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, DWORD *len)
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 if (!(data = msi_alloc( size ))) goto done;
526 if (!ReadFile( file, data, size, &read, NULL ) || read != size) goto done;
528 while (!data[size - 1]) size--;
529 *len = MultiByteToWideChar( CP_ACP, 0, data, size, NULL, 0 );
530 if ((wdata = msi_alloc( (*len + 1) * sizeof(WCHAR) )))
532 MultiByteToWideChar( CP_ACP, 0, data, size, wdata, *len );
542 static void msi_parse_line(LPWSTR *line, LPWSTR **entries, DWORD *num_entries, DWORD *len)
544 LPWSTR ptr = *line, save;
545 DWORD i, count = 1, chars_left = *len;
549 /* stay on this line */
550 while (chars_left && *ptr != '\n')
552 /* entries are separated by tabs */
560 *entries = msi_alloc(count * sizeof(LPWSTR));
564 /* store pointers into the data */
566 for (i = 0, ptr = *line; i < count; i++)
568 while (chars_left && *ptr == '\r')
575 while (chars_left && *ptr != '\t' && *ptr != '\n' && *ptr != '\r')
577 if (!*ptr) *ptr = '\n'; /* convert embedded nulls to \n */
578 if (ptr > *line && *ptr == '\x19' && *(ptr - 1) == '\x11')
587 /* NULL-separate the data */
588 if (*ptr == '\n' || *ptr == '\r')
590 while (chars_left && (*ptr == '\n' || *ptr == '\r'))
601 (*entries)[i] = save;
604 /* move to the next line if there's more, else EOF */
608 *num_entries = count;
611 static LPWSTR msi_build_createsql_prelude(LPWSTR table)
616 static const WCHAR create_fmt[] = {'C','R','E','A','T','E',' ','T','A','B','L','E',' ','`','%','s','`',' ','(',' ',0};
618 size = sizeof(create_fmt)/sizeof(create_fmt[0]) + lstrlenW(table) - 2;
619 prelude = msi_alloc(size * sizeof(WCHAR));
623 sprintfW(prelude, create_fmt, table);
627 static LPWSTR msi_build_createsql_columns(LPWSTR *columns_data, LPWSTR *types, DWORD num_columns)
631 DWORD sql_size = 1, i, len;
632 WCHAR expanded[128], *ptr;
633 WCHAR size[10], comma[2], extra[30];
635 static const WCHAR column_fmt[] = {'`','%','s','`',' ','%','s','%','s','%','s','%','s',' ',0};
636 static const WCHAR size_fmt[] = {'(','%','s',')',0};
637 static const WCHAR type_char[] = {'C','H','A','R',0};
638 static const WCHAR type_int[] = {'I','N','T',0};
639 static const WCHAR type_long[] = {'L','O','N','G',0};
640 static const WCHAR type_object[] = {'O','B','J','E','C','T',0};
641 static const WCHAR type_notnull[] = {' ','N','O','T',' ','N','U','L','L',0};
642 static const WCHAR localizable[] = {' ','L','O','C','A','L','I','Z','A','B','L','E',0};
644 columns = msi_alloc_zero(sql_size * sizeof(WCHAR));
648 for (i = 0; i < num_columns; i++)
651 comma[1] = size[0] = extra[0] = '\0';
653 if (i == num_columns - 1)
665 lstrcpyW(extra, type_notnull);
667 lstrcatW(extra, localizable);
669 sprintfW(size, size_fmt, ptr);
672 lstrcpyW(extra, type_notnull);
675 sprintfW(size, size_fmt, ptr);
678 lstrcpyW(extra, type_notnull);
686 WARN("invalid int width %u\n", len);
692 lstrcpyW(extra, type_notnull);
697 ERR("Unknown type: %c\n", types[i][0]);
702 sprintfW(expanded, column_fmt, columns_data[i], type, size, extra, comma);
703 sql_size += lstrlenW(expanded);
705 p = msi_realloc(columns, sql_size * sizeof(WCHAR));
713 lstrcatW(columns, expanded);
719 static LPWSTR msi_build_createsql_postlude(LPWSTR *primary_keys, DWORD num_keys)
721 LPWSTR postlude, keys, ptr;
722 DWORD size, key_size, i;
724 static const WCHAR key_fmt[] = {'`','%','s','`',',',' ',0};
725 static const WCHAR postlude_fmt[] = {'P','R','I','M','A','R','Y',' ','K','E','Y',' ','%','s',')',0};
727 for (i = 0, size = 1; i < num_keys; i++)
728 size += lstrlenW(key_fmt) + lstrlenW(primary_keys[i]) - 2;
730 keys = msi_alloc(size * sizeof(WCHAR));
734 for (i = 0, ptr = keys; i < num_keys; i++)
736 key_size = lstrlenW(key_fmt) + lstrlenW(primary_keys[i]) -2;
737 sprintfW(ptr, key_fmt, primary_keys[i]);
741 /* remove final ', ' */
744 size = lstrlenW(postlude_fmt) + size - 1;
745 postlude = msi_alloc(size * sizeof(WCHAR));
749 sprintfW(postlude, postlude_fmt, keys);
756 static UINT msi_add_table_to_db(MSIDATABASE *db, LPWSTR *columns, LPWSTR *types, LPWSTR *labels, DWORD num_labels, DWORD num_columns)
758 UINT r = ERROR_OUTOFMEMORY;
761 LPWSTR create_sql = NULL;
762 LPWSTR prelude, columns_sql, postlude;
764 prelude = msi_build_createsql_prelude(labels[0]);
765 columns_sql = msi_build_createsql_columns(columns, types, num_columns);
766 postlude = msi_build_createsql_postlude(labels + 1, num_labels - 1); /* skip over table name */
768 if (!prelude || !columns_sql || !postlude)
771 size = lstrlenW(prelude) + lstrlenW(columns_sql) + lstrlenW(postlude) + 1;
772 create_sql = msi_alloc(size * sizeof(WCHAR));
776 lstrcpyW(create_sql, prelude);
777 lstrcatW(create_sql, columns_sql);
778 lstrcatW(create_sql, postlude);
780 r = MSI_DatabaseOpenViewW( db, create_sql, &view );
781 if (r != ERROR_SUCCESS)
784 r = MSI_ViewExecute(view, NULL);
786 msiobj_release(&view->hdr);
790 msi_free(columns_sql);
792 msi_free(create_sql);
796 static LPWSTR msi_import_stream_filename(LPCWSTR path, LPCWSTR name)
799 LPWSTR fullname, ptr;
801 len = lstrlenW(path) + lstrlenW(name) + 1;
802 fullname = msi_alloc(len*sizeof(WCHAR));
806 lstrcpyW( fullname, path );
808 /* chop off extension from path */
809 ptr = strrchrW(fullname, '.');
816 lstrcpyW( ptr, name );
820 static UINT construct_record(DWORD num_columns, LPWSTR *types,
821 LPWSTR *data, LPWSTR path, MSIRECORD **rec)
825 *rec = MSI_CreateRecord(num_columns);
827 return ERROR_OUTOFMEMORY;
829 for (i = 0; i < num_columns; i++)
833 case 'L': case 'l': case 'S': case 's':
834 MSI_RecordSetStringW(*rec, i + 1, data[i]);
838 MSI_RecordSetInteger(*rec, i + 1, atoiW(data[i]));
844 LPWSTR file = msi_import_stream_filename(path, data[i]);
846 return ERROR_FUNCTION_FAILED;
848 r = MSI_RecordSetStreamFromFileW(*rec, i + 1, file);
850 if (r != ERROR_SUCCESS)
851 return ERROR_FUNCTION_FAILED;
855 ERR("Unhandled column type: %c\n", types[i][0]);
856 msiobj_release(&(*rec)->hdr);
857 return ERROR_FUNCTION_FAILED;
861 return ERROR_SUCCESS;
864 static UINT msi_add_records_to_table(MSIDATABASE *db, LPWSTR *columns, LPWSTR *types,
865 LPWSTR *labels, LPWSTR **records,
866 int num_columns, int num_records,
874 static const WCHAR select[] = {
875 'S','E','L','E','C','T',' ','*',' ',
876 'F','R','O','M',' ','`','%','s','`',0
879 r = MSI_OpenQuery(db, &view, select, labels[0]);
880 if (r != ERROR_SUCCESS)
883 while (MSI_ViewFetch(view, &rec) != ERROR_NO_MORE_ITEMS)
885 r = MSI_ViewModify(view, MSIMODIFY_DELETE, rec);
886 msiobj_release(&rec->hdr);
887 if (r != ERROR_SUCCESS)
891 for (i = 0; i < num_records; i++)
893 r = construct_record(num_columns, types, records[i], path, &rec);
894 if (r != ERROR_SUCCESS)
897 r = MSI_ViewModify(view, MSIMODIFY_INSERT, rec);
898 if (r != ERROR_SUCCESS)
900 msiobj_release(&rec->hdr);
904 msiobj_release(&rec->hdr);
908 msiobj_release(&view->hdr);
912 static UINT MSI_DatabaseImport(MSIDATABASE *db, LPCWSTR folder, LPCWSTR file)
916 DWORD num_labels, num_types;
917 DWORD num_columns, num_records = 0;
918 LPWSTR *columns, *types, *labels;
919 LPWSTR path, ptr, data;
920 LPWSTR **records = NULL;
921 LPWSTR **temp_records;
923 static const WCHAR suminfo[] =
924 {'_','S','u','m','m','a','r','y','I','n','f','o','r','m','a','t','i','o','n',0};
925 static const WCHAR forcecodepage[] =
926 {'_','F','o','r','c','e','C','o','d','e','p','a','g','e',0};
928 TRACE("%p %s %s\n", db, debugstr_w(folder), debugstr_w(file) );
930 if( folder == NULL || file == NULL )
931 return ERROR_INVALID_PARAMETER;
933 len = lstrlenW(folder) + lstrlenW(szBackSlash) + lstrlenW(file) + 1;
934 path = msi_alloc( len * sizeof(WCHAR) );
936 return ERROR_OUTOFMEMORY;
938 lstrcpyW( path, folder );
939 lstrcatW( path, szBackSlash );
940 lstrcatW( path, file );
942 data = msi_read_text_archive( path, &len );
945 msi_parse_line( &ptr, &columns, &num_columns, &len );
946 msi_parse_line( &ptr, &types, &num_types, &len );
947 msi_parse_line( &ptr, &labels, &num_labels, &len );
949 if (num_columns == 1 && !columns[0][0] && num_labels == 1 && !labels[0][0] &&
950 num_types == 2 && !strcmpW( types[1], forcecodepage ))
952 r = msi_set_string_table_codepage( db->strings, atoiW( types[0] ) );
956 if (num_columns != num_types)
958 r = ERROR_FUNCTION_FAILED;
962 records = msi_alloc(sizeof(LPWSTR *));
965 r = ERROR_OUTOFMEMORY;
969 /* read in the table records */
972 msi_parse_line( &ptr, &records[num_records], NULL, &len );
975 temp_records = msi_realloc(records, (num_records + 1) * sizeof(LPWSTR *));
978 r = ERROR_OUTOFMEMORY;
981 records = temp_records;
984 if (!strcmpW(labels[0], suminfo))
986 r = msi_add_suminfo( db, records, num_records, num_columns );
987 if (r != ERROR_SUCCESS)
989 r = ERROR_FUNCTION_FAILED;
995 if (!TABLE_Exists(db, labels[0]))
997 r = msi_add_table_to_db( db, columns, types, labels, num_labels, num_columns );
998 if (r != ERROR_SUCCESS)
1000 r = ERROR_FUNCTION_FAILED;
1005 r = msi_add_records_to_table( db, columns, types, labels, records, num_columns, num_records, path );
1015 for (i = 0; i < num_records; i++)
1016 msi_free(records[i]);
1023 UINT WINAPI MsiDatabaseImportW(MSIHANDLE handle, LPCWSTR szFolder, LPCWSTR szFilename)
1028 TRACE("%x %s %s\n",handle,debugstr_w(szFolder), debugstr_w(szFilename));
1030 db = msihandle2msiinfo( handle, MSIHANDLETYPE_DATABASE );
1033 IWineMsiRemoteDatabase *remote_database;
1035 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( handle );
1036 if ( !remote_database )
1037 return ERROR_INVALID_HANDLE;
1039 IWineMsiRemoteDatabase_Release( remote_database );
1040 WARN("MsiDatabaseImport not allowed during a custom action!\n");
1042 return ERROR_SUCCESS;
1045 r = MSI_DatabaseImport( db, szFolder, szFilename );
1046 msiobj_release( &db->hdr );
1050 UINT WINAPI MsiDatabaseImportA( MSIHANDLE handle,
1051 LPCSTR szFolder, LPCSTR szFilename )
1053 LPWSTR path = NULL, file = NULL;
1054 UINT r = ERROR_OUTOFMEMORY;
1056 TRACE("%x %s %s\n", handle, debugstr_a(szFolder), debugstr_a(szFilename));
1060 path = strdupAtoW( szFolder );
1067 file = strdupAtoW( szFilename );
1072 r = MsiDatabaseImportW( handle, path, file );
1081 static UINT msi_export_record( HANDLE handle, MSIRECORD *row, UINT start )
1083 UINT i, count, len, r = ERROR_SUCCESS;
1089 buffer = msi_alloc( len );
1091 return ERROR_OUTOFMEMORY;
1093 count = MSI_RecordGetFieldCount( row );
1094 for ( i=start; i<=count; i++ )
1097 r = MSI_RecordGetStringA( row, i, buffer, &sz );
1098 if (r == ERROR_MORE_DATA)
1100 char *p = msi_realloc( buffer, sz + 1 );
1107 r = MSI_RecordGetStringA( row, i, buffer, &sz );
1108 if (r != ERROR_SUCCESS)
1111 if (!WriteFile( handle, buffer, sz, &sz, NULL ))
1113 r = ERROR_FUNCTION_FAILED;
1117 sep = (i < count) ? "\t" : "\r\n";
1118 if (!WriteFile( handle, sep, strlen(sep), &sz, NULL ))
1120 r = ERROR_FUNCTION_FAILED;
1128 static UINT msi_export_row( MSIRECORD *row, void *arg )
1130 return msi_export_record( arg, row, 1 );
1133 static UINT msi_export_forcecodepage( HANDLE handle, UINT codepage )
1135 static const char fmt[] = "\r\n\r\n%u\t_ForceCodepage\r\n";
1136 char data[sizeof(fmt) + 10];
1139 sprintf( data, fmt, codepage );
1141 sz = lstrlenA(data) + 1;
1142 if (!WriteFile(handle, data, sz, &sz, NULL))
1143 return ERROR_FUNCTION_FAILED;
1145 return ERROR_SUCCESS;
1148 static UINT MSI_DatabaseExport( MSIDATABASE *db, LPCWSTR table,
1149 LPCWSTR folder, LPCWSTR file )
1151 static const WCHAR query[] = {
1152 's','e','l','e','c','t',' ','*',' ','f','r','o','m',' ','%','s',0 };
1153 static const WCHAR forcecodepage[] = {
1154 '_','F','o','r','c','e','C','o','d','e','p','a','g','e',0 };
1155 MSIRECORD *rec = NULL;
1156 MSIQUERY *view = NULL;
1161 TRACE("%p %s %s %s\n", db, debugstr_w(table),
1162 debugstr_w(folder), debugstr_w(file) );
1164 if( folder == NULL || file == NULL )
1165 return ERROR_INVALID_PARAMETER;
1167 len = lstrlenW(folder) + lstrlenW(file) + 2;
1168 filename = msi_alloc(len * sizeof (WCHAR));
1170 return ERROR_OUTOFMEMORY;
1172 lstrcpyW( filename, folder );
1173 lstrcatW( filename, szBackSlash );
1174 lstrcatW( filename, file );
1176 handle = CreateFileW( filename, GENERIC_READ | GENERIC_WRITE, 0,
1177 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
1178 msi_free( filename );
1179 if (handle == INVALID_HANDLE_VALUE)
1180 return ERROR_FUNCTION_FAILED;
1182 if (!strcmpW( table, forcecodepage ))
1184 UINT codepage = msi_get_string_table_codepage( db->strings );
1185 r = msi_export_forcecodepage( handle, codepage );
1189 r = MSI_OpenQuery( db, &view, query, table );
1190 if (r == ERROR_SUCCESS)
1192 /* write out row 1, the column names */
1193 r = MSI_ViewGetColumnInfo(view, MSICOLINFO_NAMES, &rec);
1194 if (r == ERROR_SUCCESS)
1196 msi_export_record( handle, rec, 1 );
1197 msiobj_release( &rec->hdr );
1200 /* write out row 2, the column types */
1201 r = MSI_ViewGetColumnInfo(view, MSICOLINFO_TYPES, &rec);
1202 if (r == ERROR_SUCCESS)
1204 msi_export_record( handle, rec, 1 );
1205 msiobj_release( &rec->hdr );
1208 /* write out row 3, the table name + keys */
1209 r = MSI_DatabaseGetPrimaryKeys( db, table, &rec );
1210 if (r == ERROR_SUCCESS)
1212 MSI_RecordSetStringW( rec, 0, table );
1213 msi_export_record( handle, rec, 0 );
1214 msiobj_release( &rec->hdr );
1217 /* write out row 4 onwards, the data */
1218 r = MSI_IterateRecords( view, 0, msi_export_row, handle );
1219 msiobj_release( &view->hdr );
1223 CloseHandle( handle );
1227 /***********************************************************************
1228 * MsiExportDatabaseW [MSI.@]
1230 * Writes a file containing the table data as tab separated ASCII.
1232 * The format is as follows:
1234 * row1 : colname1 <tab> colname2 <tab> .... colnameN <cr> <lf>
1235 * row2 : coltype1 <tab> coltype2 <tab> .... coltypeN <cr> <lf>
1236 * row3 : tablename <tab> key1 <tab> key2 <tab> ... keyM <cr> <lf>
1238 * Followed by the data, starting at row 1 with one row per line
1240 * row4 : data <tab> data <tab> data <tab> ... data <cr> <lf>
1242 UINT WINAPI MsiDatabaseExportW( MSIHANDLE handle, LPCWSTR szTable,
1243 LPCWSTR szFolder, LPCWSTR szFilename )
1248 TRACE("%x %s %s %s\n", handle, debugstr_w(szTable),
1249 debugstr_w(szFolder), debugstr_w(szFilename));
1251 db = msihandle2msiinfo( handle, MSIHANDLETYPE_DATABASE );
1254 IWineMsiRemoteDatabase *remote_database;
1256 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( handle );
1257 if ( !remote_database )
1258 return ERROR_INVALID_HANDLE;
1260 IWineMsiRemoteDatabase_Release( remote_database );
1261 WARN("MsiDatabaseExport not allowed during a custom action!\n");
1263 return ERROR_SUCCESS;
1266 r = MSI_DatabaseExport( db, szTable, szFolder, szFilename );
1267 msiobj_release( &db->hdr );
1271 UINT WINAPI MsiDatabaseExportA( MSIHANDLE handle, LPCSTR szTable,
1272 LPCSTR szFolder, LPCSTR szFilename )
1274 LPWSTR path = NULL, file = NULL, table = NULL;
1275 UINT r = ERROR_OUTOFMEMORY;
1277 TRACE("%x %s %s %s\n", handle, debugstr_a(szTable),
1278 debugstr_a(szFolder), debugstr_a(szFilename));
1282 table = strdupAtoW( szTable );
1289 path = strdupAtoW( szFolder );
1296 file = strdupAtoW( szFilename );
1301 r = MsiDatabaseExportW( handle, table, path, file );
1311 UINT WINAPI MsiDatabaseMergeA(MSIHANDLE hDatabase, MSIHANDLE hDatabaseMerge,
1317 TRACE("(%d, %d, %s)\n", hDatabase, hDatabaseMerge,
1318 debugstr_a(szTableName));
1320 table = strdupAtoW(szTableName);
1321 r = MsiDatabaseMergeW(hDatabase, hDatabaseMerge, table);
1327 typedef struct _tagMERGETABLE
1341 typedef struct _tagMERGEROW
1347 typedef struct _tagMERGEDATA
1351 MERGETABLE *curtable;
1353 struct list *tabledata;
1356 static BOOL merge_type_match(LPCWSTR type1, LPCWSTR type2)
1358 if (((type1[0] == 'l') || (type1[0] == 's')) &&
1359 ((type2[0] == 'l') || (type2[0] == 's')))
1362 if (((type1[0] == 'L') || (type1[0] == 'S')) &&
1363 ((type2[0] == 'L') || (type2[0] == 'S')))
1366 return !strcmpW( type1, type2 );
1369 static UINT merge_verify_colnames(MSIQUERY *dbview, MSIQUERY *mergeview)
1371 MSIRECORD *dbrec, *mergerec;
1374 r = MSI_ViewGetColumnInfo(dbview, MSICOLINFO_NAMES, &dbrec);
1375 if (r != ERROR_SUCCESS)
1378 r = MSI_ViewGetColumnInfo(mergeview, MSICOLINFO_NAMES, &mergerec);
1379 if (r != ERROR_SUCCESS)
1382 count = MSI_RecordGetFieldCount(dbrec);
1383 for (i = 1; i <= count; i++)
1385 if (!MSI_RecordGetString(mergerec, i))
1388 if (strcmpW( MSI_RecordGetString( dbrec, i ), MSI_RecordGetString( mergerec, i ) ))
1390 r = ERROR_DATATYPE_MISMATCH;
1395 msiobj_release(&dbrec->hdr);
1396 msiobj_release(&mergerec->hdr);
1397 dbrec = mergerec = NULL;
1399 r = MSI_ViewGetColumnInfo(dbview, MSICOLINFO_TYPES, &dbrec);
1400 if (r != ERROR_SUCCESS)
1403 r = MSI_ViewGetColumnInfo(mergeview, MSICOLINFO_TYPES, &mergerec);
1404 if (r != ERROR_SUCCESS)
1407 count = MSI_RecordGetFieldCount(dbrec);
1408 for (i = 1; i <= count; i++)
1410 if (!MSI_RecordGetString(mergerec, i))
1413 if (!merge_type_match(MSI_RecordGetString(dbrec, i),
1414 MSI_RecordGetString(mergerec, i)))
1416 r = ERROR_DATATYPE_MISMATCH;
1422 msiobj_release(&dbrec->hdr);
1423 msiobj_release(&mergerec->hdr);
1428 static UINT merge_verify_primary_keys(MSIDATABASE *db, MSIDATABASE *mergedb,
1431 MSIRECORD *dbrec, *mergerec = NULL;
1434 r = MSI_DatabaseGetPrimaryKeys(db, table, &dbrec);
1435 if (r != ERROR_SUCCESS)
1438 r = MSI_DatabaseGetPrimaryKeys(mergedb, table, &mergerec);
1439 if (r != ERROR_SUCCESS)
1442 count = MSI_RecordGetFieldCount(dbrec);
1443 if (count != MSI_RecordGetFieldCount(mergerec))
1445 r = ERROR_DATATYPE_MISMATCH;
1449 for (i = 1; i <= count; i++)
1451 if (strcmpW( MSI_RecordGetString( dbrec, i ), MSI_RecordGetString( mergerec, i ) ))
1453 r = ERROR_DATATYPE_MISMATCH;
1459 msiobj_release(&dbrec->hdr);
1460 msiobj_release(&mergerec->hdr);
1465 static LPWSTR get_key_value(MSIQUERY *view, LPCWSTR key, MSIRECORD *rec)
1467 MSIRECORD *colnames;
1469 UINT r, i = 0, sz = 0;
1472 r = MSI_ViewGetColumnInfo(view, MSICOLINFO_NAMES, &colnames);
1473 if (r != ERROR_SUCCESS)
1478 str = msi_dup_record_field(colnames, ++i);
1479 cmp = strcmpW( key, str );
1483 msiobj_release(&colnames->hdr);
1485 r = MSI_RecordGetStringW(rec, i, NULL, &sz);
1486 if (r != ERROR_SUCCESS)
1490 if (MSI_RecordGetString(rec, i)) /* check record field is a string */
1492 /* quote string record fields */
1493 const WCHAR szQuote[] = {'\'', 0};
1495 val = msi_alloc(sz*sizeof(WCHAR));
1499 lstrcpyW(val, szQuote);
1500 r = MSI_RecordGetStringW(rec, i, val+1, &sz);
1501 lstrcpyW(val+1+sz, szQuote);
1505 /* do not quote integer record fields */
1506 val = msi_alloc(sz*sizeof(WCHAR));
1510 r = MSI_RecordGetStringW(rec, i, val, &sz);
1513 if (r != ERROR_SUCCESS)
1515 ERR("failed to get string!\n");
1523 static LPWSTR create_diff_row_query(MSIDATABASE *merge, MSIQUERY *view,
1524 LPWSTR table, MSIRECORD *rec)
1526 LPWSTR query = NULL, clause = NULL;
1527 LPWSTR ptr = NULL, val;
1529 DWORD size = 1, oldsize;
1534 static const WCHAR keyset[] = {
1535 '`','%','s','`',' ','=',' ','%','s',' ','A','N','D',' ',0};
1536 static const WCHAR lastkeyset[] = {
1537 '`','%','s','`',' ','=',' ','%','s',' ',0};
1538 static const WCHAR fmt[] = {'S','E','L','E','C','T',' ','*',' ',
1539 'F','R','O','M',' ','`','%','s','`',' ',
1540 'W','H','E','R','E',' ','%','s',0};
1542 r = MSI_DatabaseGetPrimaryKeys(merge, table, &keys);
1543 if (r != ERROR_SUCCESS)
1546 clause = msi_alloc_zero(size * sizeof(WCHAR));
1551 count = MSI_RecordGetFieldCount(keys);
1552 for (i = 1; i <= count; i++)
1554 key = MSI_RecordGetString(keys, i);
1555 val = get_key_value(view, key, rec);
1558 setptr = lastkeyset;
1563 size += lstrlenW(setptr) + lstrlenW(key) + lstrlenW(val) - 4;
1564 clause = msi_realloc(clause, size * sizeof (WCHAR));
1571 ptr = clause + oldsize - 1;
1572 sprintfW(ptr, setptr, key, val);
1576 size = lstrlenW(fmt) + lstrlenW(table) + lstrlenW(clause) + 1;
1577 query = msi_alloc(size * sizeof(WCHAR));
1581 sprintfW(query, fmt, table, clause);
1585 msiobj_release(&keys->hdr);
1589 static UINT merge_diff_row(MSIRECORD *rec, LPVOID param)
1591 MERGEDATA *data = param;
1592 MERGETABLE *table = data->curtable;
1594 MSIQUERY *dbview = NULL;
1595 MSIRECORD *row = NULL;
1596 LPWSTR query = NULL;
1597 UINT r = ERROR_SUCCESS;
1599 if (TABLE_Exists(data->db, table->name))
1601 query = create_diff_row_query(data->merge, data->curview, table->name, rec);
1603 return ERROR_OUTOFMEMORY;
1605 r = MSI_DatabaseOpenViewW(data->db, query, &dbview);
1606 if (r != ERROR_SUCCESS)
1609 r = MSI_ViewExecute(dbview, NULL);
1610 if (r != ERROR_SUCCESS)
1613 r = MSI_ViewFetch(dbview, &row);
1614 if (r == ERROR_SUCCESS && !MSI_RecordsAreEqual(rec, row))
1616 table->numconflicts++;
1619 else if (r != ERROR_NO_MORE_ITEMS)
1625 mergerow = msi_alloc(sizeof(MERGEROW));
1628 r = ERROR_OUTOFMEMORY;
1632 mergerow->data = MSI_CloneRecord(rec);
1633 if (!mergerow->data)
1635 r = ERROR_OUTOFMEMORY;
1640 list_add_tail(&table->rows, &mergerow->entry);
1644 msiobj_release(&row->hdr);
1645 msiobj_release(&dbview->hdr);
1649 static UINT msi_get_table_labels(MSIDATABASE *db, LPCWSTR table, LPWSTR **labels, DWORD *numlabels)
1652 MSIRECORD *prec = NULL;
1654 r = MSI_DatabaseGetPrimaryKeys(db, table, &prec);
1655 if (r != ERROR_SUCCESS)
1658 count = MSI_RecordGetFieldCount(prec);
1659 *numlabels = count + 1;
1660 *labels = msi_alloc((*numlabels)*sizeof(LPWSTR));
1663 r = ERROR_OUTOFMEMORY;
1667 (*labels)[0] = strdupW(table);
1668 for (i=1; i<=count; i++ )
1670 (*labels)[i] = strdupW(MSI_RecordGetString(prec, i));
1674 msiobj_release( &prec->hdr );
1678 static UINT msi_get_query_columns(MSIQUERY *query, LPWSTR **columns, DWORD *numcolumns)
1681 MSIRECORD *prec = NULL;
1683 r = MSI_ViewGetColumnInfo(query, MSICOLINFO_NAMES, &prec);
1684 if (r != ERROR_SUCCESS)
1687 count = MSI_RecordGetFieldCount(prec);
1688 *columns = msi_alloc(count*sizeof(LPWSTR));
1691 r = ERROR_OUTOFMEMORY;
1695 for (i=1; i<=count; i++ )
1697 (*columns)[i-1] = strdupW(MSI_RecordGetString(prec, i));
1700 *numcolumns = count;
1703 msiobj_release( &prec->hdr );
1707 static UINT msi_get_query_types(MSIQUERY *query, LPWSTR **types, DWORD *numtypes)
1710 MSIRECORD *prec = NULL;
1712 r = MSI_ViewGetColumnInfo(query, MSICOLINFO_TYPES, &prec);
1713 if (r != ERROR_SUCCESS)
1716 count = MSI_RecordGetFieldCount(prec);
1717 *types = msi_alloc(count*sizeof(LPWSTR));
1720 r = ERROR_OUTOFMEMORY;
1725 for (i=1; i<=count; i++ )
1727 (*types)[i-1] = strdupW(MSI_RecordGetString(prec, i));
1731 msiobj_release( &prec->hdr );
1735 static void merge_free_rows(MERGETABLE *table)
1737 struct list *item, *cursor;
1739 LIST_FOR_EACH_SAFE(item, cursor, &table->rows)
1741 MERGEROW *row = LIST_ENTRY(item, MERGEROW, entry);
1743 list_remove(&row->entry);
1744 msiobj_release(&row->data->hdr);
1749 static void free_merge_table(MERGETABLE *table)
1753 if (table->labels != NULL)
1755 for (i = 0; i < table->numlabels; i++)
1756 msi_free(table->labels[i]);
1758 msi_free(table->labels);
1761 if (table->columns != NULL)
1763 for (i = 0; i < table->numcolumns; i++)
1764 msi_free(table->columns[i]);
1766 msi_free(table->columns);
1769 if (table->types != NULL)
1771 for (i = 0; i < table->numtypes; i++)
1772 msi_free(table->types[i]);
1774 msi_free(table->types);
1777 msi_free(table->name);
1778 merge_free_rows(table);
1783 static UINT msi_get_merge_table (MSIDATABASE *db, LPCWSTR name, MERGETABLE **ptable)
1787 MSIQUERY *mergeview = NULL;
1789 static const WCHAR query[] = {'S','E','L','E','C','T',' ','*',' ',
1790 'F','R','O','M',' ','`','%','s','`',0};
1792 table = msi_alloc_zero(sizeof(MERGETABLE));
1796 return ERROR_OUTOFMEMORY;
1799 r = msi_get_table_labels(db, name, &table->labels, &table->numlabels);
1800 if (r != ERROR_SUCCESS)
1803 r = MSI_OpenQuery(db, &mergeview, query, name);
1804 if (r != ERROR_SUCCESS)
1807 r = msi_get_query_columns(mergeview, &table->columns, &table->numcolumns);
1808 if (r != ERROR_SUCCESS)
1811 r = msi_get_query_types(mergeview, &table->types, &table->numtypes);
1812 if (r != ERROR_SUCCESS)
1815 list_init(&table->rows);
1817 table->name = strdupW(name);
1818 table->numconflicts = 0;
1820 msiobj_release(&mergeview->hdr);
1822 return ERROR_SUCCESS;
1825 msiobj_release(&mergeview->hdr);
1826 free_merge_table(table);
1831 static UINT merge_diff_tables(MSIRECORD *rec, LPVOID param)
1833 MERGEDATA *data = param;
1835 MSIQUERY *dbview = NULL;
1836 MSIQUERY *mergeview = NULL;
1840 static const WCHAR query[] = {'S','E','L','E','C','T',' ','*',' ',
1841 'F','R','O','M',' ','`','%','s','`',0};
1843 name = MSI_RecordGetString(rec, 1);
1845 r = MSI_OpenQuery(data->merge, &mergeview, query, name);
1846 if (r != ERROR_SUCCESS)
1849 if (TABLE_Exists(data->db, name))
1851 r = MSI_OpenQuery(data->db, &dbview, query, name);
1852 if (r != ERROR_SUCCESS)
1855 r = merge_verify_colnames(dbview, mergeview);
1856 if (r != ERROR_SUCCESS)
1859 r = merge_verify_primary_keys(data->db, data->merge, name);
1860 if (r != ERROR_SUCCESS)
1864 r = msi_get_merge_table(data->merge, name, &table);
1865 if (r != ERROR_SUCCESS)
1868 data->curtable = table;
1869 data->curview = mergeview;
1870 r = MSI_IterateRecords(mergeview, NULL, merge_diff_row, data);
1871 if (r != ERROR_SUCCESS)
1873 free_merge_table(table);
1877 list_add_tail(data->tabledata, &table->entry);
1880 msiobj_release(&dbview->hdr);
1881 msiobj_release(&mergeview->hdr);
1885 static UINT gather_merge_data(MSIDATABASE *db, MSIDATABASE *merge,
1886 struct list *tabledata)
1892 static const WCHAR query[] = {'S','E','L','E','C','T',' ','*',' ',
1893 'F','R','O','M',' ','`','_','T','a','b','l','e','s','`',0};
1895 r = MSI_DatabaseOpenViewW(merge, query, &view);
1896 if (r != ERROR_SUCCESS)
1901 data.tabledata = tabledata;
1902 r = MSI_IterateRecords(view, NULL, merge_diff_tables, &data);
1904 msiobj_release(&view->hdr);
1908 static UINT merge_table(MSIDATABASE *db, MERGETABLE *table)
1914 if (!TABLE_Exists(db, table->name))
1916 r = msi_add_table_to_db(db, table->columns, table->types,
1917 table->labels, table->numlabels, table->numcolumns);
1918 if (r != ERROR_SUCCESS)
1919 return ERROR_FUNCTION_FAILED;
1922 LIST_FOR_EACH_ENTRY(row, &table->rows, MERGEROW, entry)
1924 r = TABLE_CreateView(db, table->name, &tv);
1925 if (r != ERROR_SUCCESS)
1928 r = tv->ops->insert_row(tv, row->data, -1, FALSE);
1929 tv->ops->delete(tv);
1931 if (r != ERROR_SUCCESS)
1935 return ERROR_SUCCESS;
1938 static UINT update_merge_errors(MSIDATABASE *db, LPCWSTR error,
1939 LPWSTR table, DWORD numconflicts)
1944 static const WCHAR create[] = {
1945 'C','R','E','A','T','E',' ','T','A','B','L','E',' ',
1946 '`','%','s','`',' ','(','`','T','a','b','l','e','`',' ',
1947 'C','H','A','R','(','2','5','5',')',' ','N','O','T',' ',
1948 'N','U','L','L',',',' ','`','N','u','m','R','o','w','M','e','r','g','e',
1949 'C','o','n','f','l','i','c','t','s','`',' ','S','H','O','R','T',' ',
1950 'N','O','T',' ','N','U','L','L',' ','P','R','I','M','A','R','Y',' ',
1951 'K','E','Y',' ','`','T','a','b','l','e','`',')',0};
1952 static const WCHAR insert[] = {
1953 'I','N','S','E','R','T',' ','I','N','T','O',' ',
1954 '`','%','s','`',' ','(','`','T','a','b','l','e','`',',',' ',
1955 '`','N','u','m','R','o','w','M','e','r','g','e',
1956 'C','o','n','f','l','i','c','t','s','`',')',' ','V','A','L','U','E','S',
1957 ' ','(','\'','%','s','\'',',',' ','%','d',')',0};
1959 if (!TABLE_Exists(db, error))
1961 r = MSI_OpenQuery(db, &view, create, error);
1962 if (r != ERROR_SUCCESS)
1965 r = MSI_ViewExecute(view, NULL);
1966 msiobj_release(&view->hdr);
1967 if (r != ERROR_SUCCESS)
1971 r = MSI_OpenQuery(db, &view, insert, error, table, numconflicts);
1972 if (r != ERROR_SUCCESS)
1975 r = MSI_ViewExecute(view, NULL);
1976 msiobj_release(&view->hdr);
1980 UINT WINAPI MsiDatabaseMergeW(MSIHANDLE hDatabase, MSIHANDLE hDatabaseMerge,
1981 LPCWSTR szTableName)
1983 struct list tabledata = LIST_INIT(tabledata);
1984 struct list *item, *cursor;
1985 MSIDATABASE *db, *merge;
1990 TRACE("(%d, %d, %s)\n", hDatabase, hDatabaseMerge,
1991 debugstr_w(szTableName));
1993 if (szTableName && !*szTableName)
1994 return ERROR_INVALID_TABLE;
1996 db = msihandle2msiinfo(hDatabase, MSIHANDLETYPE_DATABASE);
1997 merge = msihandle2msiinfo(hDatabaseMerge, MSIHANDLETYPE_DATABASE);
2000 r = ERROR_INVALID_HANDLE;
2004 r = gather_merge_data(db, merge, &tabledata);
2005 if (r != ERROR_SUCCESS)
2009 LIST_FOR_EACH_ENTRY(table, &tabledata, MERGETABLE, entry)
2011 if (table->numconflicts)
2015 r = update_merge_errors(db, szTableName, table->name,
2016 table->numconflicts);
2017 if (r != ERROR_SUCCESS)
2022 r = merge_table(db, table);
2023 if (r != ERROR_SUCCESS)
2028 LIST_FOR_EACH_SAFE(item, cursor, &tabledata)
2030 MERGETABLE *table = LIST_ENTRY(item, MERGETABLE, entry);
2031 list_remove(&table->entry);
2032 free_merge_table(table);
2036 r = ERROR_FUNCTION_FAILED;
2039 msiobj_release(&db->hdr);
2040 msiobj_release(&merge->hdr);
2044 MSIDBSTATE WINAPI MsiGetDatabaseState( MSIHANDLE handle )
2046 MSIDBSTATE ret = MSIDBSTATE_READ;
2049 TRACE("%d\n", handle);
2051 db = msihandle2msiinfo( handle, MSIHANDLETYPE_DATABASE );
2054 IWineMsiRemoteDatabase *remote_database;
2056 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( handle );
2057 if ( !remote_database )
2058 return MSIDBSTATE_ERROR;
2060 IWineMsiRemoteDatabase_Release( remote_database );
2061 WARN("MsiGetDatabaseState not allowed during a custom action!\n");
2063 return MSIDBSTATE_READ;
2066 if (db->mode != MSIDBOPEN_READONLY )
2067 ret = MSIDBSTATE_WRITE;
2068 msiobj_release( &db->hdr );
2073 typedef struct _msi_remote_database_impl {
2074 IWineMsiRemoteDatabase IWineMsiRemoteDatabase_iface;
2077 } msi_remote_database_impl;
2079 static inline msi_remote_database_impl *impl_from_IWineMsiRemoteDatabase( IWineMsiRemoteDatabase *iface )
2081 return CONTAINING_RECORD(iface, msi_remote_database_impl, IWineMsiRemoteDatabase_iface);
2084 static HRESULT WINAPI mrd_QueryInterface( IWineMsiRemoteDatabase *iface,
2085 REFIID riid,LPVOID *ppobj)
2087 if( IsEqualCLSID( riid, &IID_IUnknown ) ||
2088 IsEqualCLSID( riid, &IID_IWineMsiRemoteDatabase ) )
2090 IUnknown_AddRef( iface );
2095 return E_NOINTERFACE;
2098 static ULONG WINAPI mrd_AddRef( IWineMsiRemoteDatabase *iface )
2100 msi_remote_database_impl* This = impl_from_IWineMsiRemoteDatabase( iface );
2102 return InterlockedIncrement( &This->refs );
2105 static ULONG WINAPI mrd_Release( IWineMsiRemoteDatabase *iface )
2107 msi_remote_database_impl* This = impl_from_IWineMsiRemoteDatabase( iface );
2110 r = InterlockedDecrement( &This->refs );
2113 MsiCloseHandle( This->database );
2119 static HRESULT WINAPI mrd_IsTablePersistent( IWineMsiRemoteDatabase *iface,
2120 LPCWSTR table, MSICONDITION *persistent )
2122 msi_remote_database_impl *This = impl_from_IWineMsiRemoteDatabase( iface );
2123 *persistent = MsiDatabaseIsTablePersistentW(This->database, table);
2127 static HRESULT WINAPI mrd_GetPrimaryKeys( IWineMsiRemoteDatabase *iface,
2128 LPCWSTR table, MSIHANDLE *keys )
2130 msi_remote_database_impl *This = impl_from_IWineMsiRemoteDatabase( iface );
2131 UINT r = MsiDatabaseGetPrimaryKeysW(This->database, table, keys);
2132 return HRESULT_FROM_WIN32(r);
2135 static HRESULT WINAPI mrd_GetSummaryInformation( IWineMsiRemoteDatabase *iface,
2136 UINT updatecount, MSIHANDLE *suminfo )
2138 msi_remote_database_impl *This = impl_from_IWineMsiRemoteDatabase( iface );
2139 UINT r = MsiGetSummaryInformationW(This->database, NULL, updatecount, suminfo);
2140 return HRESULT_FROM_WIN32(r);
2143 static HRESULT WINAPI mrd_OpenView( IWineMsiRemoteDatabase *iface,
2144 LPCWSTR query, MSIHANDLE *view )
2146 msi_remote_database_impl *This = impl_from_IWineMsiRemoteDatabase( iface );
2147 UINT r = MsiDatabaseOpenViewW(This->database, query, view);
2148 return HRESULT_FROM_WIN32(r);
2151 static HRESULT WINAPI mrd_SetMsiHandle( IWineMsiRemoteDatabase *iface, MSIHANDLE handle )
2153 msi_remote_database_impl* This = impl_from_IWineMsiRemoteDatabase( iface );
2154 This->database = handle;
2158 static const IWineMsiRemoteDatabaseVtbl msi_remote_database_vtbl =
2163 mrd_IsTablePersistent,
2165 mrd_GetSummaryInformation,
2170 HRESULT create_msi_remote_database( IUnknown *pOuter, LPVOID *ppObj )
2172 msi_remote_database_impl *This;
2174 This = msi_alloc( sizeof *This );
2176 return E_OUTOFMEMORY;
2178 This->IWineMsiRemoteDatabase_iface.lpVtbl = &msi_remote_database_vtbl;