mshtml: Added generic bind_to_tree node notification and use it to set frame's conten...
[wine/multimedia.git] / dlls / msi / database.c
blobadbdb9d46fc8709af28e2a8a758d114607cf924a
1 /*
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
21 #include <stdarg.h>
23 #define COBJMACROS
24 #define NONAMELESSUNION
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winreg.h"
29 #include "winnls.h"
30 #include "wine/debug.h"
31 #include "wine/unicode.h"
32 #include "msi.h"
33 #include "msiquery.h"
34 #include "msipriv.h"
35 #include "objidl.h"
36 #include "objbase.h"
37 #include "msiserver.h"
38 #include "query.h"
40 #include "initguid.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(msi);
45 * .MSI file format
47 * An .msi file is a structured storage file.
48 * It contains a number of streams.
49 * A stream for each table in the database.
50 * Two streams for the string table in the database.
51 * Any binary data in a table is a reference to a stream.
54 static VOID MSI_CloseDatabase( MSIOBJECTHDR *arg )
56 MSIDATABASE *db = (MSIDATABASE *) arg;
58 msi_free(db->path);
59 free_cached_tables( db );
60 msi_free_transforms( db );
61 msi_destroy_stringtable( db->strings );
62 IStorage_Release( db->storage );
63 if (db->deletefile)
65 DeleteFileW( db->deletefile );
66 msi_free( db->deletefile );
68 if (db->localfile)
70 DeleteFileW( db->localfile );
71 msi_free( db->localfile );
75 UINT MSI_OpenDatabaseW(LPCWSTR szDBPath, LPCWSTR szPersist, MSIDATABASE **pdb)
77 IStorage *stg = NULL;
78 HRESULT r;
79 MSIDATABASE *db = NULL;
80 UINT ret = ERROR_FUNCTION_FAILED;
81 LPCWSTR szMode, save_path;
82 STATSTG stat;
83 BOOL created = FALSE;
84 WCHAR path[MAX_PATH];
86 static const WCHAR szTables[] = { '_','T','a','b','l','e','s',0 };
88 TRACE("%s %s\n",debugstr_w(szDBPath),debugstr_w(szPersist) );
90 if( !pdb )
91 return ERROR_INVALID_PARAMETER;
93 if (szPersist - MSIDBOPEN_PATCHFILE >= MSIDBOPEN_READONLY &&
94 szPersist - MSIDBOPEN_PATCHFILE <= MSIDBOPEN_CREATEDIRECT)
96 TRACE("Database is a patch\n");
97 szPersist -= MSIDBOPEN_PATCHFILE;
100 save_path = szDBPath;
101 szMode = szPersist;
102 if( HIWORD( szPersist ) )
104 if (!CopyFileW( szDBPath, szPersist, FALSE ))
105 return ERROR_OPEN_FAILED;
107 szDBPath = szPersist;
108 szPersist = MSIDBOPEN_TRANSACT;
109 created = TRUE;
112 if( szPersist == MSIDBOPEN_READONLY )
114 r = StgOpenStorage( szDBPath, NULL,
115 STGM_DIRECT|STGM_READ|STGM_SHARE_DENY_WRITE, NULL, 0, &stg);
117 else if( szPersist == MSIDBOPEN_CREATE || szPersist == MSIDBOPEN_CREATEDIRECT )
119 /* FIXME: MSIDBOPEN_CREATE should case STGM_TRANSACTED flag to be
120 * used here: */
121 r = StgCreateDocfile( szDBPath,
122 STGM_CREATE|STGM_DIRECT|STGM_READWRITE|STGM_SHARE_EXCLUSIVE, 0, &stg);
123 if( r == ERROR_SUCCESS )
125 IStorage_SetClass( stg, &CLSID_MsiDatabase );
126 /* create the _Tables stream */
127 r = write_stream_data(stg, szTables, NULL, 0, TRUE);
128 if (SUCCEEDED(r))
129 r = msi_init_string_table( stg );
131 created = TRUE;
133 else if( szPersist == MSIDBOPEN_TRANSACT )
135 /* FIXME: MSIDBOPEN_TRANSACT should case STGM_TRANSACTED flag to be
136 * used here: */
137 r = StgOpenStorage( szDBPath, NULL,
138 STGM_DIRECT|STGM_READWRITE|STGM_SHARE_EXCLUSIVE, NULL, 0, &stg);
140 else if( szPersist == MSIDBOPEN_DIRECT )
142 r = StgOpenStorage( szDBPath, NULL,
143 STGM_DIRECT|STGM_READWRITE|STGM_SHARE_EXCLUSIVE, NULL, 0, &stg);
145 else
147 ERR("unknown flag %p\n",szPersist);
148 return ERROR_INVALID_PARAMETER;
151 if( FAILED( r ) || !stg )
153 FIXME("open failed r = %08x for %s\n", r, debugstr_w(szDBPath));
154 return ERROR_FUNCTION_FAILED;
157 r = IStorage_Stat( stg, &stat, STATFLAG_NONAME );
158 if( FAILED( r ) )
160 FIXME("Failed to stat storage\n");
161 goto end;
164 if ( !IsEqualGUID( &stat.clsid, &CLSID_MsiDatabase ) &&
165 !IsEqualGUID( &stat.clsid, &CLSID_MsiPatch ) &&
166 !IsEqualGUID( &stat.clsid, &CLSID_MsiTransform ) )
168 ERR("storage GUID is not a MSI database GUID %s\n",
169 debugstr_guid(&stat.clsid) );
170 goto end;
173 db = alloc_msiobject( MSIHANDLETYPE_DATABASE, sizeof (MSIDATABASE),
174 MSI_CloseDatabase );
175 if( !db )
177 FIXME("Failed to allocate a handle\n");
178 goto end;
181 if (!strchrW( save_path, '\\' ))
183 GetCurrentDirectoryW( MAX_PATH, path );
184 lstrcatW( path, szBackSlash );
185 lstrcatW( path, save_path );
187 else
188 lstrcpyW( path, save_path );
190 db->path = strdupW( path );
192 if( TRACE_ON( msi ) )
193 enum_stream_names( stg );
195 db->storage = stg;
196 db->mode = szMode;
197 if (created)
198 db->deletefile = strdupW( szDBPath );
199 list_init( &db->tables );
200 list_init( &db->transforms );
202 db->strings = msi_load_string_table( stg, &db->bytes_per_strref );
203 if( !db->strings )
204 goto end;
206 ret = ERROR_SUCCESS;
208 msiobj_addref( &db->hdr );
209 IStorage_AddRef( stg );
210 *pdb = db;
212 end:
213 if( db )
214 msiobj_release( &db->hdr );
215 if( stg )
216 IStorage_Release( stg );
218 return ret;
221 UINT WINAPI MsiOpenDatabaseW(LPCWSTR szDBPath, LPCWSTR szPersist, MSIHANDLE *phDB)
223 MSIDATABASE *db;
224 UINT ret;
226 TRACE("%s %s %p\n",debugstr_w(szDBPath),debugstr_w(szPersist), phDB);
228 ret = MSI_OpenDatabaseW( szDBPath, szPersist, &db );
229 if( ret == ERROR_SUCCESS )
231 *phDB = alloc_msihandle( &db->hdr );
232 if (! *phDB)
233 ret = ERROR_NOT_ENOUGH_MEMORY;
234 msiobj_release( &db->hdr );
237 return ret;
240 UINT WINAPI MsiOpenDatabaseA(LPCSTR szDBPath, LPCSTR szPersist, MSIHANDLE *phDB)
242 HRESULT r = ERROR_FUNCTION_FAILED;
243 LPWSTR szwDBPath = NULL, szwPersist = NULL;
245 TRACE("%s %s %p\n", debugstr_a(szDBPath), debugstr_a(szPersist), phDB);
247 if( szDBPath )
249 szwDBPath = strdupAtoW( szDBPath );
250 if( !szwDBPath )
251 goto end;
254 if( HIWORD(szPersist) )
256 szwPersist = strdupAtoW( szPersist );
257 if( !szwPersist )
258 goto end;
260 else
261 szwPersist = (LPWSTR)(DWORD_PTR)szPersist;
263 r = MsiOpenDatabaseW( szwDBPath, szwPersist, phDB );
265 end:
266 if( HIWORD(szPersist) )
267 msi_free( szwPersist );
268 msi_free( szwDBPath );
270 return r;
273 static LPWSTR msi_read_text_archive(LPCWSTR path)
275 HANDLE file;
276 LPSTR data = NULL;
277 LPWSTR wdata = NULL;
278 DWORD read, size = 0;
280 file = CreateFileW( path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL );
281 if (file == INVALID_HANDLE_VALUE)
282 return NULL;
284 size = GetFileSize( file, NULL );
285 data = msi_alloc( size + 1 );
286 if (!data)
287 goto done;
289 if (!ReadFile( file, data, size, &read, NULL ))
290 goto done;
292 data[size] = '\0';
293 wdata = strdupAtoW( data );
295 done:
296 CloseHandle( file );
297 msi_free( data );
298 return wdata;
301 static void msi_parse_line(LPWSTR *line, LPWSTR **entries, DWORD *num_entries)
303 LPWSTR ptr = *line, save;
304 DWORD i, count = 1;
306 *entries = NULL;
308 /* stay on this line */
309 while (*ptr && *ptr != '\n')
311 /* entries are separated by tabs */
312 if (*ptr == '\t')
313 count++;
315 ptr++;
318 *entries = msi_alloc(count * sizeof(LPWSTR));
319 if (!*entries)
320 return;
322 /* store pointers into the data */
323 for (i = 0, ptr = *line; i < count; i++)
325 while (*ptr && *ptr == '\r') ptr++;
326 save = ptr;
328 while (*ptr && *ptr != '\t' && *ptr != '\n' && *ptr != '\r') ptr++;
330 /* NULL-separate the data */
331 if (*ptr == '\n' || *ptr == '\r')
333 while (*ptr == '\n' || *ptr == '\r')
334 *(ptr++) = '\0';
336 else if (*ptr)
337 *ptr++ = '\0';
339 (*entries)[i] = save;
342 /* move to the next line if there's more, else EOF */
343 *line = ptr;
345 if (num_entries)
346 *num_entries = count;
349 static LPWSTR msi_build_createsql_prelude(LPWSTR table)
351 LPWSTR prelude;
352 DWORD size;
354 static const WCHAR create_fmt[] = {'C','R','E','A','T','E',' ','T','A','B','L','E',' ','`','%','s','`',' ','(',' ',0};
356 size = sizeof(create_fmt)/sizeof(create_fmt[0]) + lstrlenW(table) - 2;
357 prelude = msi_alloc(size * sizeof(WCHAR));
358 if (!prelude)
359 return NULL;
361 sprintfW(prelude, create_fmt, table);
362 return prelude;
365 static LPWSTR msi_build_createsql_columns(LPWSTR *columns_data, LPWSTR *types, DWORD num_columns)
367 LPWSTR columns, p;
368 LPCWSTR type;
369 DWORD sql_size = 1, i, len;
370 WCHAR expanded[128], *ptr;
371 WCHAR size[10], comma[2], extra[30];
373 static const WCHAR column_fmt[] = {'`','%','s','`',' ','%','s','%','s','%','s','%','s',' ',0};
374 static const WCHAR size_fmt[] = {'(','%','s',')',0};
375 static const WCHAR type_char[] = {'C','H','A','R',0};
376 static const WCHAR type_int[] = {'I','N','T',0};
377 static const WCHAR type_long[] = {'L','O','N','G',0};
378 static const WCHAR type_object[] = {'O','B','J','E','C','T',0};
379 static const WCHAR type_notnull[] = {' ','N','O','T',' ','N','U','L','L',0};
380 static const WCHAR localizable[] = {' ','L','O','C','A','L','I','Z','A','B','L','E',0};
382 columns = msi_alloc_zero(sql_size * sizeof(WCHAR));
383 if (!columns)
384 return NULL;
386 for (i = 0; i < num_columns; i++)
388 type = NULL;
389 comma[1] = size[0] = extra[0] = '\0';
391 if (i == num_columns - 1)
392 comma[0] = '\0';
393 else
394 comma[0] = ',';
396 ptr = &types[i][1];
397 len = atolW(ptr);
398 extra[0] = '\0';
400 switch (types[i][0])
402 case 'l':
403 lstrcpyW(extra, type_notnull);
404 case 'L':
405 lstrcatW(extra, localizable);
406 type = type_char;
407 sprintfW(size, size_fmt, ptr);
408 break;
409 case 's':
410 lstrcpyW(extra, type_notnull);
411 case 'S':
412 type = type_char;
413 sprintfW(size, size_fmt, ptr);
414 break;
415 case 'i':
416 lstrcpyW(extra, type_notnull);
417 case 'I':
418 if (len <= 2)
419 type = type_int;
420 else if (len == 4)
421 type = type_long;
422 else
424 WARN("invalid int width %u\n", len);
425 msi_free(columns);
426 return NULL;
428 break;
429 case 'v':
430 lstrcpyW(extra, type_notnull);
431 case 'V':
432 type = type_object;
433 break;
434 default:
435 ERR("Unknown type: %c\n", types[i][0]);
436 msi_free(columns);
437 return NULL;
440 sprintfW(expanded, column_fmt, columns_data[i], type, size, extra, comma);
441 sql_size += lstrlenW(expanded);
443 p = msi_realloc(columns, sql_size * sizeof(WCHAR));
444 if (!p)
446 msi_free(columns);
447 return NULL;
449 columns = p;
451 lstrcatW(columns, expanded);
454 return columns;
457 static LPWSTR msi_build_createsql_postlude(LPWSTR *primary_keys, DWORD num_keys)
459 LPWSTR postlude, keys, ptr;
460 DWORD size, key_size, i;
462 static const WCHAR key_fmt[] = {'`','%','s','`',',',' ',0};
463 static const WCHAR postlude_fmt[] = {'P','R','I','M','A','R','Y',' ','K','E','Y',' ','%','s',')',0};
465 for (i = 0, size = 1; i < num_keys; i++)
466 size += lstrlenW(key_fmt) + lstrlenW(primary_keys[i]) - 2;
468 keys = msi_alloc(size * sizeof(WCHAR));
469 if (!keys)
470 return NULL;
472 for (i = 0, ptr = keys; i < num_keys; i++)
474 key_size = lstrlenW(key_fmt) + lstrlenW(primary_keys[i]) -2;
475 sprintfW(ptr, key_fmt, primary_keys[i]);
476 ptr += key_size;
479 /* remove final ', ' */
480 *(ptr - 2) = '\0';
482 size = lstrlenW(postlude_fmt) + size - 1;
483 postlude = msi_alloc(size * sizeof(WCHAR));
484 if (!postlude)
485 goto done;
487 sprintfW(postlude, postlude_fmt, keys);
489 done:
490 msi_free(keys);
491 return postlude;
494 static UINT msi_add_table_to_db(MSIDATABASE *db, LPWSTR *columns, LPWSTR *types, LPWSTR *labels, DWORD num_labels, DWORD num_columns)
496 UINT r;
497 DWORD size;
498 MSIQUERY *view;
499 LPWSTR create_sql;
500 LPWSTR prelude, columns_sql, postlude;
502 prelude = msi_build_createsql_prelude(labels[0]);
503 columns_sql = msi_build_createsql_columns(columns, types, num_columns);
504 postlude = msi_build_createsql_postlude(labels + 1, num_labels - 1); /* skip over table name */
506 if (!prelude || !columns_sql || !postlude)
507 return ERROR_OUTOFMEMORY;
509 size = lstrlenW(prelude) + lstrlenW(columns_sql) + lstrlenW(postlude) + 1;
510 create_sql = msi_alloc(size * sizeof(WCHAR));
511 if (!create_sql)
512 return ERROR_OUTOFMEMORY;
514 lstrcpyW(create_sql, prelude);
515 lstrcatW(create_sql, columns_sql);
516 lstrcatW(create_sql, postlude);
518 msi_free(prelude);
519 msi_free(columns_sql);
520 msi_free(postlude);
522 r = MSI_DatabaseOpenViewW( db, create_sql, &view );
523 msi_free(create_sql);
525 if (r != ERROR_SUCCESS)
526 return r;
528 r = MSI_ViewExecute(view, NULL);
529 MSI_ViewClose(view);
530 msiobj_release(&view->hdr);
532 return r;
535 static LPWSTR msi_import_stream_filename(LPCWSTR path, LPCWSTR name)
537 DWORD len;
538 LPWSTR fullname, ptr;
540 len = lstrlenW(path) + lstrlenW(name) + 1;
541 fullname = msi_alloc(len*sizeof(WCHAR));
542 if (!fullname)
543 return NULL;
545 lstrcpyW( fullname, path );
547 /* chop off extension from path */
548 ptr = strrchrW(fullname, '.');
549 if (!ptr)
551 msi_free (fullname);
552 return NULL;
554 *ptr++ = '\\';
555 lstrcpyW( ptr, name );
556 return fullname;
559 static UINT construct_record(DWORD num_columns, LPWSTR *types,
560 LPWSTR *data, LPWSTR path, MSIRECORD **rec)
562 UINT i;
564 *rec = MSI_CreateRecord(num_columns);
565 if (!*rec)
566 return ERROR_OUTOFMEMORY;
568 for (i = 0; i < num_columns; i++)
570 switch (types[i][0])
572 case 'L': case 'l': case 'S': case 's':
573 MSI_RecordSetStringW(*rec, i + 1, data[i]);
574 break;
575 case 'I': case 'i':
576 if (*data[i])
577 MSI_RecordSetInteger(*rec, i + 1, atoiW(data[i]));
578 break;
579 case 'V': case 'v':
580 if (*data[i])
582 UINT r;
583 LPWSTR file = msi_import_stream_filename(path, data[i]);
584 if (!file)
585 return ERROR_FUNCTION_FAILED;
587 r = MSI_RecordSetStreamFromFileW(*rec, i + 1, file);
588 msi_free (file);
589 if (r != ERROR_SUCCESS)
590 return ERROR_FUNCTION_FAILED;
592 break;
593 default:
594 ERR("Unhandled column type: %c\n", types[i][0]);
595 msiobj_release(&(*rec)->hdr);
596 return ERROR_FUNCTION_FAILED;
600 return ERROR_SUCCESS;
603 static UINT msi_add_records_to_table(MSIDATABASE *db, LPWSTR *columns, LPWSTR *types,
604 LPWSTR *labels, LPWSTR **records,
605 int num_columns, int num_records,
606 LPWSTR path)
608 UINT r;
609 int i;
610 MSIQUERY *view;
611 MSIRECORD *rec;
613 static const WCHAR select[] = {
614 'S','E','L','E','C','T',' ','*',' ',
615 'F','R','O','M',' ','`','%','s','`',0
618 r = MSI_OpenQuery(db, &view, select, labels[0]);
619 if (r != ERROR_SUCCESS)
620 return r;
622 while (MSI_ViewFetch(view, &rec) != ERROR_NO_MORE_ITEMS)
624 r = MSI_ViewModify(view, MSIMODIFY_DELETE, rec);
625 if (r != ERROR_SUCCESS)
626 goto done;
629 for (i = 0; i < num_records; i++)
631 r = construct_record(num_columns, types, records[i], path, &rec);
632 if (r != ERROR_SUCCESS)
633 goto done;
635 r = MSI_ViewModify(view, MSIMODIFY_INSERT, rec);
636 if (r != ERROR_SUCCESS)
638 msiobj_release(&rec->hdr);
639 goto done;
642 msiobj_release(&rec->hdr);
645 done:
646 msiobj_release(&view->hdr);
647 return r;
650 static UINT MSI_DatabaseImport(MSIDATABASE *db, LPCWSTR folder, LPCWSTR file)
652 UINT r;
653 DWORD len, i;
654 DWORD num_labels, num_types;
655 DWORD num_columns, num_records = 0;
656 LPWSTR *columns, *types, *labels;
657 LPWSTR path, ptr, data;
658 LPWSTR **records = NULL;
659 LPWSTR **temp_records;
661 static const WCHAR suminfo[] =
662 {'_','S','u','m','m','a','r','y','I','n','f','o','r','m','a','t','i','o','n',0};
664 TRACE("%p %s %s\n", db, debugstr_w(folder), debugstr_w(file) );
666 if( folder == NULL || file == NULL )
667 return ERROR_INVALID_PARAMETER;
669 len = lstrlenW(folder) + lstrlenW(szBackSlash) + lstrlenW(file) + 1;
670 path = msi_alloc( len * sizeof(WCHAR) );
671 if (!path)
672 return ERROR_OUTOFMEMORY;
674 lstrcpyW( path, folder );
675 lstrcatW( path, szBackSlash );
676 lstrcatW( path, file );
678 data = msi_read_text_archive( path );
680 ptr = data;
681 msi_parse_line( &ptr, &columns, &num_columns );
682 msi_parse_line( &ptr, &types, &num_types );
683 msi_parse_line( &ptr, &labels, &num_labels );
685 if (num_columns != num_types)
687 r = ERROR_FUNCTION_FAILED;
688 goto done;
691 records = msi_alloc(sizeof(LPWSTR *));
692 if (!records)
694 r = ERROR_OUTOFMEMORY;
695 goto done;
698 /* read in the table records */
699 while (*ptr)
701 msi_parse_line( &ptr, &records[num_records], NULL );
703 num_records++;
704 temp_records = msi_realloc(records, (num_records + 1) * sizeof(LPWSTR *));
705 if (!temp_records)
707 r = ERROR_OUTOFMEMORY;
708 goto done;
710 records = temp_records;
713 if (!strcmpW(labels[0], suminfo))
715 r = msi_add_suminfo( db, records, num_records, num_columns );
716 if (r != ERROR_SUCCESS)
718 r = ERROR_FUNCTION_FAILED;
719 goto done;
722 else
724 if (!TABLE_Exists(db, labels[0]))
726 r = msi_add_table_to_db( db, columns, types, labels, num_labels, num_columns );
727 if (r != ERROR_SUCCESS)
729 r = ERROR_FUNCTION_FAILED;
730 goto done;
734 r = msi_add_records_to_table( db, columns, types, labels, records, num_columns, num_records, path );
737 done:
738 msi_free(path);
739 msi_free(data);
740 msi_free(columns);
741 msi_free(types);
742 msi_free(labels);
744 for (i = 0; i < num_records; i++)
745 msi_free(records[i]);
747 msi_free(records);
749 return r;
752 UINT WINAPI MsiDatabaseImportW(MSIHANDLE handle, LPCWSTR szFolder, LPCWSTR szFilename)
754 MSIDATABASE *db;
755 UINT r;
757 TRACE("%x %s %s\n",handle,debugstr_w(szFolder), debugstr_w(szFilename));
759 db = msihandle2msiinfo( handle, MSIHANDLETYPE_DATABASE );
760 if( !db )
762 IWineMsiRemoteDatabase *remote_database;
764 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( handle );
765 if ( !remote_database )
766 return ERROR_INVALID_HANDLE;
768 IWineMsiRemoteDatabase_Release( remote_database );
769 WARN("MsiDatabaseImport not allowed during a custom action!\n");
771 return ERROR_SUCCESS;
774 r = MSI_DatabaseImport( db, szFolder, szFilename );
775 msiobj_release( &db->hdr );
776 return r;
779 UINT WINAPI MsiDatabaseImportA( MSIHANDLE handle,
780 LPCSTR szFolder, LPCSTR szFilename )
782 LPWSTR path = NULL, file = NULL;
783 UINT r = ERROR_OUTOFMEMORY;
785 TRACE("%x %s %s\n", handle, debugstr_a(szFolder), debugstr_a(szFilename));
787 if( szFolder )
789 path = strdupAtoW( szFolder );
790 if( !path )
791 goto end;
794 if( szFilename )
796 file = strdupAtoW( szFilename );
797 if( !file )
798 goto end;
801 r = MsiDatabaseImportW( handle, path, file );
803 end:
804 msi_free( path );
805 msi_free( file );
807 return r;
810 static UINT msi_export_record( HANDLE handle, MSIRECORD *row, UINT start )
812 UINT i, count, len, r = ERROR_SUCCESS;
813 const char *sep;
814 char *buffer;
815 DWORD sz;
817 len = 0x100;
818 buffer = msi_alloc( len );
819 if ( !buffer )
820 return ERROR_OUTOFMEMORY;
822 count = MSI_RecordGetFieldCount( row );
823 for ( i=start; i<=count; i++ )
825 sz = len;
826 r = MSI_RecordGetStringA( row, i, buffer, &sz );
827 if (r == ERROR_MORE_DATA)
829 char *p = msi_realloc( buffer, sz + 1 );
830 if (!p)
831 break;
832 len = sz + 1;
833 buffer = p;
835 sz = len;
836 r = MSI_RecordGetStringA( row, i, buffer, &sz );
837 if (r != ERROR_SUCCESS)
838 break;
840 if (!WriteFile( handle, buffer, sz, &sz, NULL ))
842 r = ERROR_FUNCTION_FAILED;
843 break;
846 sep = (i < count) ? "\t" : "\r\n";
847 if (!WriteFile( handle, sep, strlen(sep), &sz, NULL ))
849 r = ERROR_FUNCTION_FAILED;
850 break;
853 msi_free( buffer );
854 return r;
857 static UINT msi_export_row( MSIRECORD *row, void *arg )
859 return msi_export_record( arg, row, 1 );
862 static UINT msi_export_forcecodepage( HANDLE handle )
864 DWORD sz;
866 static const char data[] = "\r\n\r\n0\t_ForceCodepage\r\n";
868 FIXME("Read the codepage from the strings table!\n");
870 sz = lstrlenA(data) + 1;
871 if (!WriteFile(handle, data, sz, &sz, NULL))
872 return ERROR_FUNCTION_FAILED;
874 return ERROR_SUCCESS;
877 static UINT MSI_DatabaseExport( MSIDATABASE *db, LPCWSTR table,
878 LPCWSTR folder, LPCWSTR file )
880 static const WCHAR query[] = {
881 's','e','l','e','c','t',' ','*',' ','f','r','o','m',' ','%','s',0 };
882 static const WCHAR forcecodepage[] = {
883 '_','F','o','r','c','e','C','o','d','e','p','a','g','e',0 };
884 MSIRECORD *rec = NULL;
885 MSIQUERY *view = NULL;
886 LPWSTR filename;
887 HANDLE handle;
888 UINT len, r;
890 TRACE("%p %s %s %s\n", db, debugstr_w(table),
891 debugstr_w(folder), debugstr_w(file) );
893 if( folder == NULL || file == NULL )
894 return ERROR_INVALID_PARAMETER;
896 len = lstrlenW(folder) + lstrlenW(file) + 2;
897 filename = msi_alloc(len * sizeof (WCHAR));
898 if (!filename)
899 return ERROR_OUTOFMEMORY;
901 lstrcpyW( filename, folder );
902 lstrcatW( filename, szBackSlash );
903 lstrcatW( filename, file );
905 handle = CreateFileW( filename, GENERIC_READ | GENERIC_WRITE, 0,
906 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
907 msi_free( filename );
908 if (handle == INVALID_HANDLE_VALUE)
909 return ERROR_FUNCTION_FAILED;
911 if (!lstrcmpW( table, forcecodepage ))
913 r = msi_export_forcecodepage( handle );
914 goto done;
917 r = MSI_OpenQuery( db, &view, query, table );
918 if (r == ERROR_SUCCESS)
920 /* write out row 1, the column names */
921 r = MSI_ViewGetColumnInfo(view, MSICOLINFO_NAMES, &rec);
922 if (r == ERROR_SUCCESS)
924 msi_export_record( handle, rec, 1 );
925 msiobj_release( &rec->hdr );
928 /* write out row 2, the column types */
929 r = MSI_ViewGetColumnInfo(view, MSICOLINFO_TYPES, &rec);
930 if (r == ERROR_SUCCESS)
932 msi_export_record( handle, rec, 1 );
933 msiobj_release( &rec->hdr );
936 /* write out row 3, the table name + keys */
937 r = MSI_DatabaseGetPrimaryKeys( db, table, &rec );
938 if (r == ERROR_SUCCESS)
940 MSI_RecordSetStringW( rec, 0, table );
941 msi_export_record( handle, rec, 0 );
942 msiobj_release( &rec->hdr );
945 /* write out row 4 onwards, the data */
946 r = MSI_IterateRecords( view, 0, msi_export_row, handle );
947 msiobj_release( &view->hdr );
950 done:
951 CloseHandle( handle );
952 return r;
955 /***********************************************************************
956 * MsiExportDatabaseW [MSI.@]
958 * Writes a file containing the table data as tab separated ASCII.
960 * The format is as follows:
962 * row1 : colname1 <tab> colname2 <tab> .... colnameN <cr> <lf>
963 * row2 : coltype1 <tab> coltype2 <tab> .... coltypeN <cr> <lf>
964 * row3 : tablename <tab> key1 <tab> key2 <tab> ... keyM <cr> <lf>
966 * Followed by the data, starting at row 1 with one row per line
968 * row4 : data <tab> data <tab> data <tab> ... data <cr> <lf>
970 UINT WINAPI MsiDatabaseExportW( MSIHANDLE handle, LPCWSTR szTable,
971 LPCWSTR szFolder, LPCWSTR szFilename )
973 MSIDATABASE *db;
974 UINT r;
976 TRACE("%x %s %s %s\n", handle, debugstr_w(szTable),
977 debugstr_w(szFolder), debugstr_w(szFilename));
979 db = msihandle2msiinfo( handle, MSIHANDLETYPE_DATABASE );
980 if( !db )
982 IWineMsiRemoteDatabase *remote_database;
984 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( handle );
985 if ( !remote_database )
986 return ERROR_INVALID_HANDLE;
988 IWineMsiRemoteDatabase_Release( remote_database );
989 WARN("MsiDatabaseExport not allowed during a custom action!\n");
991 return ERROR_SUCCESS;
994 r = MSI_DatabaseExport( db, szTable, szFolder, szFilename );
995 msiobj_release( &db->hdr );
996 return r;
999 UINT WINAPI MsiDatabaseExportA( MSIHANDLE handle, LPCSTR szTable,
1000 LPCSTR szFolder, LPCSTR szFilename )
1002 LPWSTR path = NULL, file = NULL, table = NULL;
1003 UINT r = ERROR_OUTOFMEMORY;
1005 TRACE("%x %s %s %s\n", handle, debugstr_a(szTable),
1006 debugstr_a(szFolder), debugstr_a(szFilename));
1008 if( szTable )
1010 table = strdupAtoW( szTable );
1011 if( !table )
1012 goto end;
1015 if( szFolder )
1017 path = strdupAtoW( szFolder );
1018 if( !path )
1019 goto end;
1022 if( szFilename )
1024 file = strdupAtoW( szFilename );
1025 if( !file )
1026 goto end;
1029 r = MsiDatabaseExportW( handle, table, path, file );
1031 end:
1032 msi_free( table );
1033 msi_free( path );
1034 msi_free( file );
1036 return r;
1039 UINT WINAPI MsiDatabaseMergeA(MSIHANDLE hDatabase, MSIHANDLE hDatabaseMerge,
1040 LPCSTR szTableName)
1042 UINT r;
1043 LPWSTR table;
1045 TRACE("(%d, %d, %s)\n", hDatabase, hDatabaseMerge,
1046 debugstr_a(szTableName));
1048 table = strdupAtoW(szTableName);
1049 r = MsiDatabaseMergeW(hDatabase, hDatabaseMerge, table);
1051 msi_free(table);
1052 return r;
1055 typedef struct _tagMERGETABLE
1057 struct list entry;
1058 struct list rows;
1059 LPWSTR name;
1060 DWORD numconflicts;
1061 LPWSTR *columns;
1062 DWORD numcolumns;
1063 LPWSTR *types;
1064 DWORD numtypes;
1065 LPWSTR *labels;
1066 DWORD numlabels;
1067 } MERGETABLE;
1069 typedef struct _tagMERGEROW
1071 struct list entry;
1072 MSIRECORD *data;
1073 } MERGEROW;
1075 typedef struct _tagMERGEDATA
1077 MSIDATABASE *db;
1078 MSIDATABASE *merge;
1079 MERGETABLE *curtable;
1080 MSIQUERY *curview;
1081 struct list *tabledata;
1082 } MERGEDATA;
1084 static BOOL merge_type_match(LPCWSTR type1, LPCWSTR type2)
1086 if (((type1[0] == 'l') || (type1[0] == 's')) &&
1087 ((type2[0] == 'l') || (type2[0] == 's')))
1088 return TRUE;
1090 if (((type1[0] == 'L') || (type1[0] == 'S')) &&
1091 ((type2[0] == 'L') || (type2[0] == 'S')))
1092 return TRUE;
1094 return !lstrcmpW(type1, type2);
1097 static UINT merge_verify_colnames(MSIQUERY *dbview, MSIQUERY *mergeview)
1099 MSIRECORD *dbrec, *mergerec;
1100 UINT r, i, count;
1102 r = MSI_ViewGetColumnInfo(dbview, MSICOLINFO_NAMES, &dbrec);
1103 if (r != ERROR_SUCCESS)
1104 return r;
1106 r = MSI_ViewGetColumnInfo(mergeview, MSICOLINFO_NAMES, &mergerec);
1107 if (r != ERROR_SUCCESS)
1108 return r;
1110 count = MSI_RecordGetFieldCount(dbrec);
1111 for (i = 1; i <= count; i++)
1113 if (!MSI_RecordGetString(mergerec, i))
1114 break;
1116 if (lstrcmpW(MSI_RecordGetString(dbrec, i),
1117 MSI_RecordGetString(mergerec, i)))
1119 r = ERROR_DATATYPE_MISMATCH;
1120 goto done;
1124 msiobj_release(&dbrec->hdr);
1125 msiobj_release(&mergerec->hdr);
1126 dbrec = mergerec = NULL;
1128 r = MSI_ViewGetColumnInfo(dbview, MSICOLINFO_TYPES, &dbrec);
1129 if (r != ERROR_SUCCESS)
1130 return r;
1132 r = MSI_ViewGetColumnInfo(mergeview, MSICOLINFO_TYPES, &mergerec);
1133 if (r != ERROR_SUCCESS)
1134 return r;
1136 count = MSI_RecordGetFieldCount(dbrec);
1137 for (i = 1; i <= count; i++)
1139 if (!MSI_RecordGetString(mergerec, i))
1140 break;
1142 if (!merge_type_match(MSI_RecordGetString(dbrec, i),
1143 MSI_RecordGetString(mergerec, i)))
1145 r = ERROR_DATATYPE_MISMATCH;
1146 break;
1150 done:
1151 msiobj_release(&dbrec->hdr);
1152 msiobj_release(&mergerec->hdr);
1154 return r;
1157 static UINT merge_verify_primary_keys(MSIDATABASE *db, MSIDATABASE *mergedb,
1158 LPCWSTR table)
1160 MSIRECORD *dbrec, *mergerec = NULL;
1161 UINT r, i, count;
1163 r = MSI_DatabaseGetPrimaryKeys(db, table, &dbrec);
1164 if (r != ERROR_SUCCESS)
1165 return r;
1167 r = MSI_DatabaseGetPrimaryKeys(mergedb, table, &mergerec);
1168 if (r != ERROR_SUCCESS)
1169 goto done;
1171 count = MSI_RecordGetFieldCount(dbrec);
1172 if (count != MSI_RecordGetFieldCount(mergerec))
1174 r = ERROR_DATATYPE_MISMATCH;
1175 goto done;
1178 for (i = 1; i <= count; i++)
1180 if (lstrcmpW(MSI_RecordGetString(dbrec, i),
1181 MSI_RecordGetString(mergerec, i)))
1183 r = ERROR_DATATYPE_MISMATCH;
1184 goto done;
1188 done:
1189 msiobj_release(&dbrec->hdr);
1190 msiobj_release(&mergerec->hdr);
1192 return r;
1195 static LPWSTR get_key_value(MSIQUERY *view, LPCWSTR key, MSIRECORD *rec)
1197 MSIRECORD *colnames;
1198 LPWSTR str, val;
1199 UINT r, i = 0, sz = 0;
1200 int cmp;
1202 r = MSI_ViewGetColumnInfo(view, MSICOLINFO_NAMES, &colnames);
1203 if (r != ERROR_SUCCESS)
1204 return NULL;
1208 str = msi_dup_record_field(colnames, ++i);
1209 cmp = lstrcmpW(key, str);
1210 msi_free(str);
1211 } while (cmp);
1213 msiobj_release(&colnames->hdr);
1215 r = MSI_RecordGetStringW(rec, i, NULL, &sz);
1216 if (r != ERROR_SUCCESS)
1217 return NULL;
1218 sz++;
1220 if (MSI_RecordGetString(rec, i)) /* check record field is a string */
1222 /* quote string record fields */
1223 const WCHAR szQuote[] = {'\'', 0};
1224 sz += 2;
1225 val = msi_alloc(sz*sizeof(WCHAR));
1226 if (!val)
1227 return NULL;
1229 lstrcpyW(val, szQuote);
1230 r = MSI_RecordGetStringW(rec, i, val+1, &sz);
1231 lstrcpyW(val+1+sz, szQuote);
1233 else
1235 /* do not quote integer record fields */
1236 val = msi_alloc(sz*sizeof(WCHAR));
1237 if (!val)
1238 return NULL;
1240 r = MSI_RecordGetStringW(rec, i, val, &sz);
1243 if (r != ERROR_SUCCESS)
1245 ERR("failed to get string!\n");
1246 msi_free(val);
1247 return NULL;
1250 return val;
1253 static LPWSTR create_diff_row_query(MSIDATABASE *merge, MSIQUERY *view,
1254 LPWSTR table, MSIRECORD *rec)
1256 LPWSTR query = NULL, clause = NULL;
1257 LPWSTR ptr = NULL, val;
1258 LPCWSTR setptr;
1259 DWORD size = 1, oldsize;
1260 LPCWSTR key;
1261 MSIRECORD *keys;
1262 UINT r, i, count;
1264 static const WCHAR keyset[] = {
1265 '`','%','s','`',' ','=',' ','%','s',' ','A','N','D',' ',0};
1266 static const WCHAR lastkeyset[] = {
1267 '`','%','s','`',' ','=',' ','%','s',' ',0};
1268 static const WCHAR fmt[] = {'S','E','L','E','C','T',' ','*',' ',
1269 'F','R','O','M',' ','`','%','s','`',' ',
1270 'W','H','E','R','E',' ','%','s',0};
1272 r = MSI_DatabaseGetPrimaryKeys(merge, table, &keys);
1273 if (r != ERROR_SUCCESS)
1274 return NULL;
1276 clause = msi_alloc_zero(size * sizeof(WCHAR));
1277 if (!clause)
1278 goto done;
1280 ptr = clause;
1281 count = MSI_RecordGetFieldCount(keys);
1282 for (i = 1; i <= count; i++)
1284 key = MSI_RecordGetString(keys, i);
1285 val = get_key_value(view, key, rec);
1287 if (i == count)
1288 setptr = lastkeyset;
1289 else
1290 setptr = keyset;
1292 oldsize = size;
1293 size += lstrlenW(setptr) + lstrlenW(key) + lstrlenW(val) - 4;
1294 clause = msi_realloc(clause, size * sizeof (WCHAR));
1295 if (!clause)
1297 msi_free(val);
1298 goto done;
1301 ptr = clause + oldsize - 1;
1302 sprintfW(ptr, setptr, key, val);
1303 msi_free(val);
1306 size = lstrlenW(fmt) + lstrlenW(table) + lstrlenW(clause) + 1;
1307 query = msi_alloc(size * sizeof(WCHAR));
1308 if (!query)
1309 goto done;
1311 sprintfW(query, fmt, table, clause);
1313 done:
1314 msi_free(clause);
1315 msiobj_release(&keys->hdr);
1316 return query;
1319 static UINT merge_diff_row(MSIRECORD *rec, LPVOID param)
1321 MERGEDATA *data = param;
1322 MERGETABLE *table = data->curtable;
1323 MERGEROW *mergerow;
1324 MSIQUERY *dbview = NULL;
1325 MSIRECORD *row = NULL;
1326 LPWSTR query = NULL;
1327 UINT r = ERROR_SUCCESS;
1329 if (TABLE_Exists(data->db, table->name))
1331 query = create_diff_row_query(data->merge, data->curview, table->name, rec);
1332 if (!query)
1333 return ERROR_OUTOFMEMORY;
1335 r = MSI_DatabaseOpenViewW(data->db, query, &dbview);
1336 if (r != ERROR_SUCCESS)
1337 goto done;
1339 r = MSI_ViewExecute(dbview, NULL);
1340 if (r != ERROR_SUCCESS)
1341 goto done;
1343 r = MSI_ViewFetch(dbview, &row);
1344 if (r == ERROR_SUCCESS && !MSI_RecordsAreEqual(rec, row))
1346 table->numconflicts++;
1347 goto done;
1349 else if (r != ERROR_NO_MORE_ITEMS)
1350 goto done;
1352 r = ERROR_SUCCESS;
1355 mergerow = msi_alloc(sizeof(MERGEROW));
1356 if (!mergerow)
1358 r = ERROR_OUTOFMEMORY;
1359 goto done;
1362 mergerow->data = MSI_CloneRecord(rec);
1363 if (!mergerow->data)
1365 r = ERROR_OUTOFMEMORY;
1366 msi_free(mergerow);
1367 goto done;
1370 list_add_tail(&table->rows, &mergerow->entry);
1372 done:
1373 msi_free(query);
1374 msiobj_release(&row->hdr);
1375 msiobj_release(&dbview->hdr);
1376 return r;
1379 static UINT msi_get_table_labels(MSIDATABASE *db, LPCWSTR table, LPWSTR **labels, DWORD *numlabels)
1381 UINT r, i, count;
1382 MSIRECORD *prec = NULL;
1384 r = MSI_DatabaseGetPrimaryKeys(db, table, &prec);
1385 if (r != ERROR_SUCCESS)
1386 return r;
1388 count = MSI_RecordGetFieldCount(prec);
1389 *numlabels = count + 1;
1390 *labels = msi_alloc((*numlabels)*sizeof(LPWSTR));
1391 if (!*labels)
1393 r = ERROR_OUTOFMEMORY;
1394 goto end;
1397 (*labels)[0] = strdupW(table);
1398 for (i=1; i<=count; i++ )
1400 (*labels)[i] = strdupW(MSI_RecordGetString(prec, i));
1403 end:
1404 msiobj_release( &prec->hdr );
1405 return r;
1408 static UINT msi_get_query_columns(MSIQUERY *query, LPWSTR **columns, DWORD *numcolumns)
1410 UINT r, i, count;
1411 MSIRECORD *prec = NULL;
1413 r = MSI_ViewGetColumnInfo(query, MSICOLINFO_NAMES, &prec);
1414 if (r != ERROR_SUCCESS)
1415 return r;
1417 count = MSI_RecordGetFieldCount(prec);
1418 *columns = msi_alloc(count*sizeof(LPWSTR));
1419 if (!*columns)
1421 r = ERROR_OUTOFMEMORY;
1422 goto end;
1425 for (i=1; i<=count; i++ )
1427 (*columns)[i-1] = strdupW(MSI_RecordGetString(prec, i));
1430 *numcolumns = count;
1432 end:
1433 msiobj_release( &prec->hdr );
1434 return r;
1437 static UINT msi_get_query_types(MSIQUERY *query, LPWSTR **types, DWORD *numtypes)
1439 UINT r, i, count;
1440 MSIRECORD *prec = NULL;
1442 r = MSI_ViewGetColumnInfo(query, MSICOLINFO_TYPES, &prec);
1443 if (r != ERROR_SUCCESS)
1444 return r;
1446 count = MSI_RecordGetFieldCount(prec);
1447 *types = msi_alloc(count*sizeof(LPWSTR));
1448 if (!*types)
1450 r = ERROR_OUTOFMEMORY;
1451 goto end;
1454 for (i=1; i<=count; i++ )
1456 (*types)[i-1] = strdupW(MSI_RecordGetString(prec, i));
1459 end:
1460 msiobj_release( &prec->hdr );
1461 return r;
1464 static void merge_free_rows(MERGETABLE *table)
1466 struct list *item, *cursor;
1468 LIST_FOR_EACH_SAFE(item, cursor, &table->rows)
1470 MERGEROW *row = LIST_ENTRY(item, MERGEROW, entry);
1472 list_remove(&row->entry);
1473 msiobj_release(&row->data->hdr);
1474 msi_free(row);
1478 static void free_merge_table(MERGETABLE *table)
1480 UINT i;
1482 if (table->labels != NULL)
1484 for (i = 0; i < table->numlabels; i++)
1485 msi_free(table->labels[i]);
1486 msi_free(table->labels);
1489 if (table->columns != NULL)
1491 for (i = 0; i < table->numcolumns; i++)
1492 msi_free(table->columns[i]);
1493 msi_free(table->columns);
1496 if (table->types != NULL)
1498 for (i = 0; i < table->numtypes; i++)
1499 msi_free(table->types[i]);
1500 msi_free(table->types);
1502 msi_free(table->name);
1503 merge_free_rows(table);
1505 msi_free(table);
1508 static UINT msi_get_merge_table (MSIDATABASE *db, LPCWSTR name, MERGETABLE **ptable)
1510 UINT r;
1511 MERGETABLE *table;
1512 MSIQUERY *mergeview = NULL;
1514 static const WCHAR query[] = {'S','E','L','E','C','T',' ','*',' ',
1515 'F','R','O','M',' ','`','%','s','`',0};
1517 table = msi_alloc_zero(sizeof(MERGETABLE));
1518 if (!table)
1520 *ptable = NULL;
1521 return ERROR_OUTOFMEMORY;
1524 r = msi_get_table_labels(db, name, &table->labels, &table->numlabels);
1525 if (r != ERROR_SUCCESS)
1526 goto err;
1528 r = MSI_OpenQuery(db, &mergeview, query, name);
1529 if (r != ERROR_SUCCESS)
1530 goto err;
1532 r = msi_get_query_columns(mergeview, &table->columns, &table->numcolumns);
1533 if (r != ERROR_SUCCESS)
1534 goto err;
1536 r = msi_get_query_types(mergeview, &table->types, &table->numtypes);
1537 if (r != ERROR_SUCCESS)
1538 goto err;
1540 list_init(&table->rows);
1542 table->name = strdupW(name);
1543 table->numconflicts = 0;
1545 msiobj_release(&mergeview->hdr);
1546 *ptable = table;
1547 return ERROR_SUCCESS;
1549 err:
1550 msiobj_release(&mergeview->hdr);
1551 free_merge_table(table);
1552 *ptable = NULL;
1553 return r;
1556 static UINT merge_diff_tables(MSIRECORD *rec, LPVOID param)
1558 MERGEDATA *data = param;
1559 MERGETABLE *table;
1560 MSIQUERY *dbview = NULL;
1561 MSIQUERY *mergeview = NULL;
1562 LPCWSTR name;
1563 UINT r;
1565 static const WCHAR query[] = {'S','E','L','E','C','T',' ','*',' ',
1566 'F','R','O','M',' ','`','%','s','`',0};
1568 name = MSI_RecordGetString(rec, 1);
1570 r = MSI_OpenQuery(data->merge, &mergeview, query, name);
1571 if (r != ERROR_SUCCESS)
1572 goto done;
1574 if (TABLE_Exists(data->db, name))
1576 r = MSI_OpenQuery(data->db, &dbview, query, name);
1577 if (r != ERROR_SUCCESS)
1578 goto done;
1580 r = merge_verify_colnames(dbview, mergeview);
1581 if (r != ERROR_SUCCESS)
1582 goto done;
1584 r = merge_verify_primary_keys(data->db, data->merge, name);
1585 if (r != ERROR_SUCCESS)
1586 goto done;
1589 r = msi_get_merge_table(data->merge, name, &table);
1590 if (r != ERROR_SUCCESS)
1591 goto done;
1593 data->curtable = table;
1594 data->curview = mergeview;
1595 r = MSI_IterateRecords(mergeview, NULL, merge_diff_row, data);
1596 if (r != ERROR_SUCCESS)
1598 free_merge_table(table);
1599 goto done;
1602 list_add_tail(data->tabledata, &table->entry);
1604 done:
1605 msiobj_release(&dbview->hdr);
1606 msiobj_release(&mergeview->hdr);
1607 return r;
1610 static UINT gather_merge_data(MSIDATABASE *db, MSIDATABASE *merge,
1611 struct list *tabledata)
1613 UINT r;
1614 MSIQUERY *view;
1615 MERGEDATA data;
1617 static const WCHAR query[] = {'S','E','L','E','C','T',' ','*',' ',
1618 'F','R','O','M',' ','`','_','T','a','b','l','e','s','`',0};
1620 r = MSI_DatabaseOpenViewW(merge, query, &view);
1621 if (r != ERROR_SUCCESS)
1622 return r;
1624 data.db = db;
1625 data.merge = merge;
1626 data.tabledata = tabledata;
1627 r = MSI_IterateRecords(view, NULL, merge_diff_tables, &data);
1629 msiobj_release(&view->hdr);
1630 return r;
1633 static UINT merge_table(MSIDATABASE *db, MERGETABLE *table)
1635 UINT r;
1636 MERGEROW *row;
1637 MSIVIEW *tv;
1639 if (!TABLE_Exists(db, table->name))
1641 r = msi_add_table_to_db(db, table->columns, table->types,
1642 table->labels, table->numlabels, table->numcolumns);
1643 if (r != ERROR_SUCCESS)
1644 return ERROR_FUNCTION_FAILED;
1647 LIST_FOR_EACH_ENTRY(row, &table->rows, MERGEROW, entry)
1649 r = TABLE_CreateView(db, table->name, &tv);
1650 if (r != ERROR_SUCCESS)
1651 return r;
1653 r = tv->ops->insert_row(tv, row->data, -1, FALSE);
1654 tv->ops->delete(tv);
1656 if (r != ERROR_SUCCESS)
1657 return r;
1660 return ERROR_SUCCESS;
1663 static UINT update_merge_errors(MSIDATABASE *db, LPCWSTR error,
1664 LPWSTR table, DWORD numconflicts)
1666 UINT r;
1667 MSIQUERY *view;
1669 static const WCHAR create[] = {
1670 'C','R','E','A','T','E',' ','T','A','B','L','E',' ',
1671 '`','%','s','`',' ','(','`','T','a','b','l','e','`',' ',
1672 'C','H','A','R','(','2','5','5',')',' ','N','O','T',' ',
1673 'N','U','L','L',',',' ','`','N','u','m','R','o','w','M','e','r','g','e',
1674 'C','o','n','f','l','i','c','t','s','`',' ','S','H','O','R','T',' ',
1675 'N','O','T',' ','N','U','L','L',' ','P','R','I','M','A','R','Y',' ',
1676 'K','E','Y',' ','`','T','a','b','l','e','`',')',0};
1677 static const WCHAR insert[] = {
1678 'I','N','S','E','R','T',' ','I','N','T','O',' ',
1679 '`','%','s','`',' ','(','`','T','a','b','l','e','`',',',' ',
1680 '`','N','u','m','R','o','w','M','e','r','g','e',
1681 'C','o','n','f','l','i','c','t','s','`',')',' ','V','A','L','U','E','S',
1682 ' ','(','\'','%','s','\'',',',' ','%','d',')',0};
1684 if (!TABLE_Exists(db, error))
1686 r = MSI_OpenQuery(db, &view, create, error);
1687 if (r != ERROR_SUCCESS)
1688 return r;
1690 r = MSI_ViewExecute(view, NULL);
1691 msiobj_release(&view->hdr);
1692 if (r != ERROR_SUCCESS)
1693 return r;
1696 r = MSI_OpenQuery(db, &view, insert, error, table, numconflicts);
1697 if (r != ERROR_SUCCESS)
1698 return r;
1700 r = MSI_ViewExecute(view, NULL);
1701 msiobj_release(&view->hdr);
1702 return r;
1705 UINT WINAPI MsiDatabaseMergeW(MSIHANDLE hDatabase, MSIHANDLE hDatabaseMerge,
1706 LPCWSTR szTableName)
1708 struct list tabledata = LIST_INIT(tabledata);
1709 struct list *item, *cursor;
1710 MSIDATABASE *db, *merge;
1711 MERGETABLE *table;
1712 BOOL conflicts;
1713 UINT r;
1715 TRACE("(%d, %d, %s)\n", hDatabase, hDatabaseMerge,
1716 debugstr_w(szTableName));
1718 if (szTableName && !*szTableName)
1719 return ERROR_INVALID_TABLE;
1721 db = msihandle2msiinfo(hDatabase, MSIHANDLETYPE_DATABASE);
1722 merge = msihandle2msiinfo(hDatabaseMerge, MSIHANDLETYPE_DATABASE);
1723 if (!db || !merge)
1725 r = ERROR_INVALID_HANDLE;
1726 goto done;
1729 r = gather_merge_data(db, merge, &tabledata);
1730 if (r != ERROR_SUCCESS)
1731 goto done;
1733 conflicts = FALSE;
1734 LIST_FOR_EACH_ENTRY(table, &tabledata, MERGETABLE, entry)
1736 if (table->numconflicts)
1738 conflicts = TRUE;
1740 r = update_merge_errors(db, szTableName, table->name,
1741 table->numconflicts);
1742 if (r != ERROR_SUCCESS)
1743 break;
1745 else
1747 r = merge_table(db, table);
1748 if (r != ERROR_SUCCESS)
1749 break;
1753 LIST_FOR_EACH_SAFE(item, cursor, &tabledata)
1755 MERGETABLE *table = LIST_ENTRY(item, MERGETABLE, entry);
1757 list_remove(&table->entry);
1758 free_merge_table(table);
1761 if (conflicts)
1762 r = ERROR_FUNCTION_FAILED;
1764 done:
1765 msiobj_release(&db->hdr);
1766 msiobj_release(&merge->hdr);
1767 return r;
1770 MSIDBSTATE WINAPI MsiGetDatabaseState( MSIHANDLE handle )
1772 MSIDBSTATE ret = MSIDBSTATE_READ;
1773 MSIDATABASE *db;
1775 TRACE("%d\n", handle);
1777 db = msihandle2msiinfo( handle, MSIHANDLETYPE_DATABASE );
1778 if( !db )
1780 IWineMsiRemoteDatabase *remote_database;
1782 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( handle );
1783 if ( !remote_database )
1784 return MSIDBSTATE_ERROR;
1786 IWineMsiRemoteDatabase_Release( remote_database );
1787 WARN("MsiGetDatabaseState not allowed during a custom action!\n");
1789 return MSIDBSTATE_READ;
1792 if (db->mode != MSIDBOPEN_READONLY )
1793 ret = MSIDBSTATE_WRITE;
1794 msiobj_release( &db->hdr );
1796 return ret;
1799 typedef struct _msi_remote_database_impl {
1800 const IWineMsiRemoteDatabaseVtbl *lpVtbl;
1801 MSIHANDLE database;
1802 LONG refs;
1803 } msi_remote_database_impl;
1805 static inline msi_remote_database_impl* mrd_from_IWineMsiRemoteDatabase( IWineMsiRemoteDatabase* iface )
1807 return (msi_remote_database_impl *)iface;
1810 static HRESULT WINAPI mrd_QueryInterface( IWineMsiRemoteDatabase *iface,
1811 REFIID riid,LPVOID *ppobj)
1813 if( IsEqualCLSID( riid, &IID_IUnknown ) ||
1814 IsEqualCLSID( riid, &IID_IWineMsiRemoteDatabase ) )
1816 IUnknown_AddRef( iface );
1817 *ppobj = iface;
1818 return S_OK;
1821 return E_NOINTERFACE;
1824 static ULONG WINAPI mrd_AddRef( IWineMsiRemoteDatabase *iface )
1826 msi_remote_database_impl* This = mrd_from_IWineMsiRemoteDatabase( iface );
1828 return InterlockedIncrement( &This->refs );
1831 static ULONG WINAPI mrd_Release( IWineMsiRemoteDatabase *iface )
1833 msi_remote_database_impl* This = mrd_from_IWineMsiRemoteDatabase( iface );
1834 ULONG r;
1836 r = InterlockedDecrement( &This->refs );
1837 if (r == 0)
1839 MsiCloseHandle( This->database );
1840 msi_free( This );
1842 return r;
1845 static HRESULT WINAPI mrd_IsTablePersistent( IWineMsiRemoteDatabase *iface,
1846 BSTR table, MSICONDITION *persistent )
1848 msi_remote_database_impl *This = mrd_from_IWineMsiRemoteDatabase( iface );
1849 *persistent = MsiDatabaseIsTablePersistentW(This->database, table);
1850 return S_OK;
1853 static HRESULT WINAPI mrd_GetPrimaryKeys( IWineMsiRemoteDatabase *iface,
1854 BSTR table, MSIHANDLE *keys )
1856 msi_remote_database_impl *This = mrd_from_IWineMsiRemoteDatabase( iface );
1857 UINT r = MsiDatabaseGetPrimaryKeysW(This->database, table, keys);
1858 return HRESULT_FROM_WIN32(r);
1861 static HRESULT WINAPI mrd_GetSummaryInformation( IWineMsiRemoteDatabase *iface,
1862 UINT updatecount, MSIHANDLE *suminfo )
1864 msi_remote_database_impl *This = mrd_from_IWineMsiRemoteDatabase( iface );
1865 UINT r = MsiGetSummaryInformationW(This->database, NULL, updatecount, suminfo);
1866 return HRESULT_FROM_WIN32(r);
1869 static HRESULT WINAPI mrd_OpenView( IWineMsiRemoteDatabase *iface,
1870 BSTR query, MSIHANDLE *view )
1872 msi_remote_database_impl *This = mrd_from_IWineMsiRemoteDatabase( iface );
1873 UINT r = MsiDatabaseOpenViewW(This->database, query, view);
1874 return HRESULT_FROM_WIN32(r);
1877 static HRESULT WINAPI mrd_SetMsiHandle( IWineMsiRemoteDatabase *iface, MSIHANDLE handle )
1879 msi_remote_database_impl* This = mrd_from_IWineMsiRemoteDatabase( iface );
1880 This->database = handle;
1881 return S_OK;
1884 static const IWineMsiRemoteDatabaseVtbl msi_remote_database_vtbl =
1886 mrd_QueryInterface,
1887 mrd_AddRef,
1888 mrd_Release,
1889 mrd_IsTablePersistent,
1890 mrd_GetPrimaryKeys,
1891 mrd_GetSummaryInformation,
1892 mrd_OpenView,
1893 mrd_SetMsiHandle,
1896 HRESULT create_msi_remote_database( IUnknown *pOuter, LPVOID *ppObj )
1898 msi_remote_database_impl *This;
1900 This = msi_alloc( sizeof *This );
1901 if (!This)
1902 return E_OUTOFMEMORY;
1904 This->lpVtbl = &msi_remote_database_vtbl;
1905 This->database = 0;
1906 This->refs = 1;
1908 *ppObj = This;
1910 return S_OK;