731427dde785807bebcf83292e29fda3ebb7a93e
[reactos.git] / reactos / dll / win32 / msi / database.c
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
3 *
4 * Copyright 2002,2003,2004,2005 Mike McCormack for CodeWeavers
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #define WIN32_NO_STATUS
22 #define _INC_WINDOWS
23 #define COM_NO_WINDOWS_H
24
25 //#include <stdarg.h>
26 #include <stdio.h>
27
28 #define COBJMACROS
29 #define NONAMELESSUNION
30
31 //#include "windef.h"
32 //#include "winbase.h"
33 //#include "winreg.h"
34 //#include "winnls.h"
35 #include <wine/debug.h>
36 #include <wine/unicode.h>
37 //#include "msi.h"
38 #include "msiquery.h"
39 //#include "msipriv.h"
40 //#include "objidl.h"
41 #include <objbase.h>
42 #include <msiserver.h>
43 #include "query.h"
44
45 //#include "initguid.h"
46
47 WINE_DEFAULT_DEBUG_CHANNEL(msi);
48
49 /*
50 * .MSI file format
51 *
52 * An .msi file is a structured storage file.
53 * It contains a number of streams.
54 * A stream for each table in the database.
55 * Two streams for the string table in the database.
56 * Any binary data in a table is a reference to a stream.
57 */
58
59 #define IS_INTMSIDBOPEN(x) (((ULONG_PTR)(x) >> 16) == 0)
60
61 typedef struct tagMSITRANSFORM {
62 struct list entry;
63 IStorage *stg;
64 } MSITRANSFORM;
65
66 typedef struct tagMSISTREAM {
67 struct list entry;
68 IStorage *stg;
69 IStream *stm;
70 } MSISTREAM;
71
72 static UINT find_open_stream( MSIDATABASE *db, IStorage *stg, LPCWSTR name, IStream **stm )
73 {
74 MSISTREAM *stream;
75
76 LIST_FOR_EACH_ENTRY( stream, &db->streams, MSISTREAM, entry )
77 {
78 HRESULT r;
79 STATSTG stat;
80
81 if (stream->stg != stg) continue;
82
83 r = IStream_Stat( stream->stm, &stat, 0 );
84 if( FAILED( r ) )
85 {
86 WARN("failed to stat stream r = %08x!\n", r);
87 continue;
88 }
89
90 if( !strcmpW( name, stat.pwcsName ) )
91 {
92 TRACE("found %s\n", debugstr_w(name));
93 *stm = stream->stm;
94 CoTaskMemFree( stat.pwcsName );
95 return ERROR_SUCCESS;
96 }
97
98 CoTaskMemFree( stat.pwcsName );
99 }
100
101 return ERROR_FUNCTION_FAILED;
102 }
103
104 UINT msi_clone_open_stream( MSIDATABASE *db, IStorage *stg, LPCWSTR name, IStream **stm )
105 {
106 IStream *stream;
107
108 if (find_open_stream( db, stg, name, &stream ) == ERROR_SUCCESS)
109 {
110 HRESULT r;
111 LARGE_INTEGER pos;
112
113 r = IStream_Clone( stream, stm );
114 if( FAILED( r ) )
115 {
116 WARN("failed to clone stream r = %08x!\n", r);
117 return ERROR_FUNCTION_FAILED;
118 }
119
120 pos.QuadPart = 0;
121 r = IStream_Seek( *stm, pos, STREAM_SEEK_SET, NULL );
122 if( FAILED( r ) )
123 {
124 IStream_Release( *stm );
125 return ERROR_FUNCTION_FAILED;
126 }
127
128 return ERROR_SUCCESS;
129 }
130
131 return ERROR_FUNCTION_FAILED;
132 }
133
134 UINT msi_get_raw_stream( MSIDATABASE *db, LPCWSTR stname, IStream **stm )
135 {
136 HRESULT r;
137 IStorage *stg;
138 WCHAR decoded[MAX_STREAM_NAME_LEN];
139
140 decode_streamname( stname, decoded );
141 TRACE("%s -> %s\n", debugstr_w(stname), debugstr_w(decoded));
142
143 if (msi_clone_open_stream( db, db->storage, stname, stm ) == ERROR_SUCCESS)
144 return ERROR_SUCCESS;
145
146 r = IStorage_OpenStream( db->storage, stname, NULL,
147 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, stm );
148 if( FAILED( r ) )
149 {
150 MSITRANSFORM *transform;
151
152 LIST_FOR_EACH_ENTRY( transform, &db->transforms, MSITRANSFORM, entry )
153 {
154 r = IStorage_OpenStream( transform->stg, stname, NULL,
155 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, stm );
156 if (SUCCEEDED(r))
157 {
158 stg = transform->stg;
159 break;
160 }
161 }
162 }
163 else stg = db->storage;
164
165 if( SUCCEEDED(r) )
166 {
167 MSISTREAM *stream;
168
169 if (!(stream = msi_alloc( sizeof(MSISTREAM) ))) return ERROR_NOT_ENOUGH_MEMORY;
170 stream->stg = stg;
171 IStorage_AddRef( stg );
172 stream->stm = *stm;
173 IStream_AddRef( *stm );
174 list_add_tail( &db->streams, &stream->entry );
175 }
176
177 return SUCCEEDED(r) ? ERROR_SUCCESS : ERROR_FUNCTION_FAILED;
178 }
179
180 static void free_transforms( MSIDATABASE *db )
181 {
182 while( !list_empty( &db->transforms ) )
183 {
184 MSITRANSFORM *t = LIST_ENTRY( list_head( &db->transforms ),
185 MSITRANSFORM, entry );
186 list_remove( &t->entry );
187 IStorage_Release( t->stg );
188 msi_free( t );
189 }
190 }
191
192 void msi_destroy_stream( MSIDATABASE *db, const WCHAR *stname )
193 {
194 MSISTREAM *stream, *stream2;
195
196 LIST_FOR_EACH_ENTRY_SAFE( stream, stream2, &db->streams, MSISTREAM, entry )
197 {
198 HRESULT r;
199 STATSTG stat;
200
201 r = IStream_Stat( stream->stm, &stat, 0 );
202 if (FAILED(r))
203 {
204 WARN("failed to stat stream r = %08x\n", r);
205 continue;
206 }
207
208 if (!strcmpW( stname, stat.pwcsName ))
209 {
210 TRACE("destroying %s\n", debugstr_w(stname));
211
212 list_remove( &stream->entry );
213 IStream_Release( stream->stm );
214 IStorage_Release( stream->stg );
215 IStorage_DestroyElement( stream->stg, stname );
216 msi_free( stream );
217 CoTaskMemFree( stat.pwcsName );
218 break;
219 }
220 CoTaskMemFree( stat.pwcsName );
221 }
222 }
223
224 static void free_streams( MSIDATABASE *db )
225 {
226 while( !list_empty( &db->streams ) )
227 {
228 MSISTREAM *s = LIST_ENTRY(list_head( &db->streams ), MSISTREAM, entry);
229 list_remove( &s->entry );
230 IStream_Release( s->stm );
231 IStorage_Release( s->stg );
232 msi_free( s );
233 }
234 }
235
236 void append_storage_to_db( MSIDATABASE *db, IStorage *stg )
237 {
238 MSITRANSFORM *t;
239
240 t = msi_alloc( sizeof *t );
241 t->stg = stg;
242 IStorage_AddRef( stg );
243 list_add_head( &db->transforms, &t->entry );
244
245 /* the transform may add or replace streams */
246 free_streams( db );
247 }
248
249 static VOID MSI_CloseDatabase( MSIOBJECTHDR *arg )
250 {
251 MSIDATABASE *db = (MSIDATABASE *) arg;
252
253 msi_free(db->path);
254 free_cached_tables( db );
255 free_streams( db );
256 free_transforms( db );
257 if (db->strings) msi_destroy_stringtable( db->strings );
258 IStorage_Release( db->storage );
259 if (db->deletefile)
260 {
261 DeleteFileW( db->deletefile );
262 msi_free( db->deletefile );
263 }
264 }
265
266 static HRESULT db_initialize( IStorage *stg, const GUID *clsid )
267 {
268 static const WCHAR szTables[] = { '_','T','a','b','l','e','s',0 };
269 HRESULT hr;
270
271 hr = IStorage_SetClass( stg, clsid );
272 if (FAILED( hr ))
273 {
274 WARN("failed to set class id 0x%08x\n", hr);
275 return hr;
276 }
277
278 /* create the _Tables stream */
279 hr = write_stream_data( stg, szTables, NULL, 0, TRUE );
280 if (FAILED( hr ))
281 {
282 WARN("failed to create _Tables stream 0x%08x\n", hr);
283 return hr;
284 }
285
286 hr = msi_init_string_table( stg );
287 if (FAILED( hr ))
288 {
289 WARN("failed to initialize string table 0x%08x\n", hr);
290 return hr;
291 }
292
293 hr = IStorage_Commit( stg, 0 );
294 if (FAILED( hr ))
295 {
296 WARN("failed to commit changes 0x%08x\n", hr);
297 return hr;
298 }
299
300 return S_OK;
301 }
302
303 UINT MSI_OpenDatabaseW(LPCWSTR szDBPath, LPCWSTR szPersist, MSIDATABASE **pdb)
304 {
305 IStorage *stg = NULL;
306 HRESULT r;
307 MSIDATABASE *db = NULL;
308 UINT ret = ERROR_FUNCTION_FAILED;
309 LPCWSTR szMode, save_path;
310 STATSTG stat;
311 BOOL created = FALSE, patch = FALSE;
312 WCHAR path[MAX_PATH];
313
314 TRACE("%s %s\n",debugstr_w(szDBPath),debugstr_w(szPersist) );
315
316 if( !pdb )
317 return ERROR_INVALID_PARAMETER;
318
319 if (szPersist - MSIDBOPEN_PATCHFILE >= MSIDBOPEN_READONLY &&
320 szPersist - MSIDBOPEN_PATCHFILE <= MSIDBOPEN_CREATEDIRECT)
321 {
322 TRACE("Database is a patch\n");
323 szPersist -= MSIDBOPEN_PATCHFILE;
324 patch = TRUE;
325 }
326
327 save_path = szDBPath;
328 szMode = szPersist;
329 if( !IS_INTMSIDBOPEN(szPersist) )
330 {
331 if (!CopyFileW( szDBPath, szPersist, FALSE ))
332 return ERROR_OPEN_FAILED;
333
334 szDBPath = szPersist;
335 szPersist = MSIDBOPEN_TRANSACT;
336 created = TRUE;
337 }
338
339 if( szPersist == MSIDBOPEN_READONLY )
340 {
341 r = StgOpenStorage( szDBPath, NULL,
342 STGM_DIRECT|STGM_READ|STGM_SHARE_DENY_WRITE, NULL, 0, &stg);
343 }
344 else if( szPersist == MSIDBOPEN_CREATE )
345 {
346 r = StgCreateDocfile( szDBPath,
347 STGM_CREATE|STGM_TRANSACTED|STGM_READWRITE|STGM_SHARE_EXCLUSIVE, 0, &stg );
348
349 if( SUCCEEDED(r) )
350 r = db_initialize( stg, patch ? &CLSID_MsiPatch : &CLSID_MsiDatabase );
351 created = TRUE;
352 }
353 else if( szPersist == MSIDBOPEN_CREATEDIRECT )
354 {
355 r = StgCreateDocfile( szDBPath,
356 STGM_CREATE|STGM_DIRECT|STGM_READWRITE|STGM_SHARE_EXCLUSIVE, 0, &stg );
357
358 if( SUCCEEDED(r) )
359 r = db_initialize( stg, patch ? &CLSID_MsiPatch : &CLSID_MsiDatabase );
360 created = TRUE;
361 }
362 else if( szPersist == MSIDBOPEN_TRANSACT )
363 {
364 r = StgOpenStorage( szDBPath, NULL,
365 STGM_TRANSACTED|STGM_READWRITE|STGM_SHARE_DENY_WRITE, NULL, 0, &stg);
366 }
367 else if( szPersist == MSIDBOPEN_DIRECT )
368 {
369 r = StgOpenStorage( szDBPath, NULL,
370 STGM_DIRECT|STGM_READWRITE|STGM_SHARE_EXCLUSIVE, NULL, 0, &stg);
371 }
372 else
373 {
374 ERR("unknown flag %p\n",szPersist);
375 return ERROR_INVALID_PARAMETER;
376 }
377
378 if( FAILED( r ) || !stg )
379 {
380 WARN("open failed r = %08x for %s\n", r, debugstr_w(szDBPath));
381 return ERROR_FUNCTION_FAILED;
382 }
383
384 r = IStorage_Stat( stg, &stat, STATFLAG_NONAME );
385 if( FAILED( r ) )
386 {
387 FIXME("Failed to stat storage\n");
388 goto end;
389 }
390
391 if ( !IsEqualGUID( &stat.clsid, &CLSID_MsiDatabase ) &&
392 !IsEqualGUID( &stat.clsid, &CLSID_MsiPatch ) &&
393 !IsEqualGUID( &stat.clsid, &CLSID_MsiTransform ) )
394 {
395 ERR("storage GUID is not a MSI database GUID %s\n",
396 debugstr_guid(&stat.clsid) );
397 goto end;
398 }
399
400 if ( patch && !IsEqualGUID( &stat.clsid, &CLSID_MsiPatch ) )
401 {
402 ERR("storage GUID is not the MSI patch GUID %s\n",
403 debugstr_guid(&stat.clsid) );
404 ret = ERROR_OPEN_FAILED;
405 goto end;
406 }
407
408 db = alloc_msiobject( MSIHANDLETYPE_DATABASE, sizeof (MSIDATABASE),
409 MSI_CloseDatabase );
410 if( !db )
411 {
412 FIXME("Failed to allocate a handle\n");
413 goto end;
414 }
415
416 if (!strchrW( save_path, '\\' ))
417 {
418 GetCurrentDirectoryW( MAX_PATH, path );
419 lstrcatW( path, szBackSlash );
420 lstrcatW( path, save_path );
421 }
422 else
423 lstrcpyW( path, save_path );
424
425 db->path = strdupW( path );
426 db->media_transform_offset = MSI_INITIAL_MEDIA_TRANSFORM_OFFSET;
427 db->media_transform_disk_id = MSI_INITIAL_MEDIA_TRANSFORM_DISKID;
428
429 if( TRACE_ON( msi ) )
430 enum_stream_names( stg );
431
432 db->storage = stg;
433 db->mode = szMode;
434 if (created)
435 db->deletefile = strdupW( szDBPath );
436 list_init( &db->tables );
437 list_init( &db->transforms );
438 list_init( &db->streams );
439
440 db->strings = msi_load_string_table( stg, &db->bytes_per_strref );
441 if( !db->strings )
442 goto end;
443
444 ret = ERROR_SUCCESS;
445
446 msiobj_addref( &db->hdr );
447 IStorage_AddRef( stg );
448 *pdb = db;
449
450 end:
451 if( db )
452 msiobj_release( &db->hdr );
453 if( stg )
454 IStorage_Release( stg );
455
456 return ret;
457 }
458
459 UINT WINAPI MsiOpenDatabaseW(LPCWSTR szDBPath, LPCWSTR szPersist, MSIHANDLE *phDB)
460 {
461 MSIDATABASE *db;
462 UINT ret;
463
464 TRACE("%s %s %p\n",debugstr_w(szDBPath),debugstr_w(szPersist), phDB);
465
466 ret = MSI_OpenDatabaseW( szDBPath, szPersist, &db );
467 if( ret == ERROR_SUCCESS )
468 {
469 *phDB = alloc_msihandle( &db->hdr );
470 if (! *phDB)
471 ret = ERROR_NOT_ENOUGH_MEMORY;
472 msiobj_release( &db->hdr );
473 }
474
475 return ret;
476 }
477
478 UINT WINAPI MsiOpenDatabaseA(LPCSTR szDBPath, LPCSTR szPersist, MSIHANDLE *phDB)
479 {
480 HRESULT r = ERROR_FUNCTION_FAILED;
481 LPWSTR szwDBPath = NULL, szwPersist = NULL;
482
483 TRACE("%s %s %p\n", debugstr_a(szDBPath), debugstr_a(szPersist), phDB);
484
485 if( szDBPath )
486 {
487 szwDBPath = strdupAtoW( szDBPath );
488 if( !szwDBPath )
489 goto end;
490 }
491
492 if( !IS_INTMSIDBOPEN(szPersist) )
493 {
494 szwPersist = strdupAtoW( szPersist );
495 if( !szwPersist )
496 goto end;
497 }
498 else
499 szwPersist = (LPWSTR)(DWORD_PTR)szPersist;
500
501 r = MsiOpenDatabaseW( szwDBPath, szwPersist, phDB );
502
503 end:
504 if( !IS_INTMSIDBOPEN(szPersist) )
505 msi_free( szwPersist );
506 msi_free( szwDBPath );
507
508 return r;
509 }
510
511 static LPWSTR msi_read_text_archive(LPCWSTR path, DWORD *len)
512 {
513 HANDLE file;
514 LPSTR data = NULL;
515 LPWSTR wdata = NULL;
516 DWORD read, size = 0;
517
518 file = CreateFileW( path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL );
519 if (file == INVALID_HANDLE_VALUE)
520 return NULL;
521
522 size = GetFileSize( file, NULL );
523 if (!(data = msi_alloc( size ))) goto done;
524
525 if (!ReadFile( file, data, size, &read, NULL ) || read != size) goto done;
526
527 while (!data[size - 1]) size--;
528 *len = MultiByteToWideChar( CP_ACP, 0, data, size, NULL, 0 );
529 if ((wdata = msi_alloc( (*len + 1) * sizeof(WCHAR) )))
530 {
531 MultiByteToWideChar( CP_ACP, 0, data, size, wdata, *len );
532 wdata[*len] = 0;
533 }
534
535 done:
536 CloseHandle( file );
537 msi_free( data );
538 return wdata;
539 }
540
541 static void msi_parse_line(LPWSTR *line, LPWSTR **entries, DWORD *num_entries, DWORD *len)
542 {
543 LPWSTR ptr = *line, save;
544 DWORD i, count = 1, chars_left = *len;
545
546 *entries = NULL;
547
548 /* stay on this line */
549 while (chars_left && *ptr != '\n')
550 {
551 /* entries are separated by tabs */
552 if (*ptr == '\t')
553 count++;
554
555 ptr++;
556 chars_left--;
557 }
558
559 *entries = msi_alloc(count * sizeof(LPWSTR));
560 if (!*entries)
561 return;
562
563 /* store pointers into the data */
564 chars_left = *len;
565 for (i = 0, ptr = *line; i < count; i++)
566 {
567 while (chars_left && *ptr == '\r')
568 {
569 ptr++;
570 chars_left--;
571 }
572 save = ptr;
573
574 while (chars_left && *ptr != '\t' && *ptr != '\n' && *ptr != '\r')
575 {
576 if (!*ptr) *ptr = '\n'; /* convert embedded nulls to \n */
577 if (ptr > *line && *ptr == '\x19' && *(ptr - 1) == '\x11')
578 {
579 *ptr = '\n';
580 *(ptr - 1) = '\r';
581 }
582 ptr++;
583 chars_left--;
584 }
585
586 /* NULL-separate the data */
587 if (*ptr == '\n' || *ptr == '\r')
588 {
589 while (chars_left && (*ptr == '\n' || *ptr == '\r'))
590 {
591 *(ptr++) = 0;
592 chars_left--;
593 }
594 }
595 else if (*ptr)
596 {
597 *(ptr++) = 0;
598 chars_left--;
599 }
600 (*entries)[i] = save;
601 }
602
603 /* move to the next line if there's more, else EOF */
604 *line = ptr;
605 *len = chars_left;
606 if (num_entries)
607 *num_entries = count;
608 }
609
610 static LPWSTR msi_build_createsql_prelude(LPWSTR table)
611 {
612 LPWSTR prelude;
613 DWORD size;
614
615 static const WCHAR create_fmt[] = {'C','R','E','A','T','E',' ','T','A','B','L','E',' ','`','%','s','`',' ','(',' ',0};
616
617 size = sizeof(create_fmt)/sizeof(create_fmt[0]) + lstrlenW(table) - 2;
618 prelude = msi_alloc(size * sizeof(WCHAR));
619 if (!prelude)
620 return NULL;
621
622 sprintfW(prelude, create_fmt, table);
623 return prelude;
624 }
625
626 static LPWSTR msi_build_createsql_columns(LPWSTR *columns_data, LPWSTR *types, DWORD num_columns)
627 {
628 LPWSTR columns, p;
629 LPCWSTR type;
630 DWORD sql_size = 1, i, len;
631 WCHAR expanded[128], *ptr;
632 WCHAR size[10], comma[2], extra[30];
633
634 static const WCHAR column_fmt[] = {'`','%','s','`',' ','%','s','%','s','%','s','%','s',' ',0};
635 static const WCHAR size_fmt[] = {'(','%','s',')',0};
636 static const WCHAR type_char[] = {'C','H','A','R',0};
637 static const WCHAR type_int[] = {'I','N','T',0};
638 static const WCHAR type_long[] = {'L','O','N','G',0};
639 static const WCHAR type_object[] = {'O','B','J','E','C','T',0};
640 static const WCHAR type_notnull[] = {' ','N','O','T',' ','N','U','L','L',0};
641 static const WCHAR localizable[] = {' ','L','O','C','A','L','I','Z','A','B','L','E',0};
642
643 columns = msi_alloc_zero(sql_size * sizeof(WCHAR));
644 if (!columns)
645 return NULL;
646
647 for (i = 0; i < num_columns; i++)
648 {
649 type = NULL;
650 comma[1] = size[0] = extra[0] = '\0';
651
652 if (i == num_columns - 1)
653 comma[0] = '\0';
654 else
655 comma[0] = ',';
656
657 ptr = &types[i][1];
658 len = atolW(ptr);
659 extra[0] = '\0';
660
661 switch (types[i][0])
662 {
663 case 'l':
664 lstrcpyW(extra, type_notnull);
665 /* fall through */
666 case 'L':
667 lstrcatW(extra, localizable);
668 type = type_char;
669 sprintfW(size, size_fmt, ptr);
670 break;
671 case 's':
672 lstrcpyW(extra, type_notnull);
673 /* fall through */
674 case 'S':
675 type = type_char;
676 sprintfW(size, size_fmt, ptr);
677 break;
678 case 'i':
679 lstrcpyW(extra, type_notnull);
680 /* fall through */
681 case 'I':
682 if (len <= 2)
683 type = type_int;
684 else if (len == 4)
685 type = type_long;
686 else
687 {
688 WARN("invalid int width %u\n", len);
689 msi_free(columns);
690 return NULL;
691 }
692 break;
693 case 'v':
694 lstrcpyW(extra, type_notnull);
695 /* fall through */
696 case 'V':
697 type = type_object;
698 break;
699 default:
700 ERR("Unknown type: %c\n", types[i][0]);
701 msi_free(columns);
702 return NULL;
703 }
704
705 sprintfW(expanded, column_fmt, columns_data[i], type, size, extra, comma);
706 sql_size += lstrlenW(expanded);
707
708 p = msi_realloc(columns, sql_size * sizeof(WCHAR));
709 if (!p)
710 {
711 msi_free(columns);
712 return NULL;
713 }
714 columns = p;
715
716 lstrcatW(columns, expanded);
717 }
718
719 return columns;
720 }
721
722 static LPWSTR msi_build_createsql_postlude(LPWSTR *primary_keys, DWORD num_keys)
723 {
724 LPWSTR postlude, keys, ptr;
725 DWORD size, key_size, i;
726
727 static const WCHAR key_fmt[] = {'`','%','s','`',',',' ',0};
728 static const WCHAR postlude_fmt[] = {'P','R','I','M','A','R','Y',' ','K','E','Y',' ','%','s',')',0};
729
730 for (i = 0, size = 1; i < num_keys; i++)
731 size += lstrlenW(key_fmt) + lstrlenW(primary_keys[i]) - 2;
732
733 keys = msi_alloc(size * sizeof(WCHAR));
734 if (!keys)
735 return NULL;
736
737 for (i = 0, ptr = keys; i < num_keys; i++)
738 {
739 key_size = lstrlenW(key_fmt) + lstrlenW(primary_keys[i]) -2;
740 sprintfW(ptr, key_fmt, primary_keys[i]);
741 ptr += key_size;
742 }
743
744 /* remove final ', ' */
745 *(ptr - 2) = '\0';
746
747 size = lstrlenW(postlude_fmt) + size - 1;
748 postlude = msi_alloc(size * sizeof(WCHAR));
749 if (!postlude)
750 goto done;
751
752 sprintfW(postlude, postlude_fmt, keys);
753
754 done:
755 msi_free(keys);
756 return postlude;
757 }
758
759 static UINT msi_add_table_to_db(MSIDATABASE *db, LPWSTR *columns, LPWSTR *types, LPWSTR *labels, DWORD num_labels, DWORD num_columns)
760 {
761 UINT r = ERROR_OUTOFMEMORY;
762 DWORD size;
763 MSIQUERY *view;
764 LPWSTR create_sql = NULL;
765 LPWSTR prelude, columns_sql, postlude;
766
767 prelude = msi_build_createsql_prelude(labels[0]);
768 columns_sql = msi_build_createsql_columns(columns, types, num_columns);
769 postlude = msi_build_createsql_postlude(labels + 1, num_labels - 1); /* skip over table name */
770
771 if (!prelude || !columns_sql || !postlude)
772 goto done;
773
774 size = lstrlenW(prelude) + lstrlenW(columns_sql) + lstrlenW(postlude) + 1;
775 create_sql = msi_alloc(size * sizeof(WCHAR));
776 if (!create_sql)
777 goto done;
778
779 lstrcpyW(create_sql, prelude);
780 lstrcatW(create_sql, columns_sql);
781 lstrcatW(create_sql, postlude);
782
783 r = MSI_DatabaseOpenViewW( db, create_sql, &view );
784 if (r != ERROR_SUCCESS)
785 goto done;
786
787 r = MSI_ViewExecute(view, NULL);
788 MSI_ViewClose(view);
789 msiobj_release(&view->hdr);
790
791 done:
792 msi_free(prelude);
793 msi_free(columns_sql);
794 msi_free(postlude);
795 msi_free(create_sql);
796 return r;
797 }
798
799 static LPWSTR msi_import_stream_filename(LPCWSTR path, LPCWSTR name)
800 {
801 DWORD len;
802 LPWSTR fullname, ptr;
803
804 len = lstrlenW(path) + lstrlenW(name) + 1;
805 fullname = msi_alloc(len*sizeof(WCHAR));
806 if (!fullname)
807 return NULL;
808
809 lstrcpyW( fullname, path );
810
811 /* chop off extension from path */
812 ptr = strrchrW(fullname, '.');
813 if (!ptr)
814 {
815 msi_free (fullname);
816 return NULL;
817 }
818 *ptr++ = '\\';
819 lstrcpyW( ptr, name );
820 return fullname;
821 }
822
823 static UINT construct_record(DWORD num_columns, LPWSTR *types,
824 LPWSTR *data, LPWSTR path, MSIRECORD **rec)
825 {
826 UINT i;
827
828 *rec = MSI_CreateRecord(num_columns);
829 if (!*rec)
830 return ERROR_OUTOFMEMORY;
831
832 for (i = 0; i < num_columns; i++)
833 {
834 switch (types[i][0])
835 {
836 case 'L': case 'l': case 'S': case 's':
837 MSI_RecordSetStringW(*rec, i + 1, data[i]);
838 break;
839 case 'I': case 'i':
840 if (*data[i])
841 MSI_RecordSetInteger(*rec, i + 1, atoiW(data[i]));
842 break;
843 case 'V': case 'v':
844 if (*data[i])
845 {
846 UINT r;
847 LPWSTR file = msi_import_stream_filename(path, data[i]);
848 if (!file)
849 return ERROR_FUNCTION_FAILED;
850
851 r = MSI_RecordSetStreamFromFileW(*rec, i + 1, file);
852 msi_free (file);
853 if (r != ERROR_SUCCESS)
854 return ERROR_FUNCTION_FAILED;
855 }
856 break;
857 default:
858 ERR("Unhandled column type: %c\n", types[i][0]);
859 msiobj_release(&(*rec)->hdr);
860 return ERROR_FUNCTION_FAILED;
861 }
862 }
863
864 return ERROR_SUCCESS;
865 }
866
867 static UINT msi_add_records_to_table(MSIDATABASE *db, LPWSTR *columns, LPWSTR *types,
868 LPWSTR *labels, LPWSTR **records,
869 int num_columns, int num_records,
870 LPWSTR path)
871 {
872 UINT r;
873 int i;
874 MSIQUERY *view;
875 MSIRECORD *rec;
876
877 static const WCHAR select[] = {
878 'S','E','L','E','C','T',' ','*',' ',
879 'F','R','O','M',' ','`','%','s','`',0
880 };
881
882 r = MSI_OpenQuery(db, &view, select, labels[0]);
883 if (r != ERROR_SUCCESS)
884 return r;
885
886 while (MSI_ViewFetch(view, &rec) != ERROR_NO_MORE_ITEMS)
887 {
888 r = MSI_ViewModify(view, MSIMODIFY_DELETE, rec);
889 msiobj_release(&rec->hdr);
890 if (r != ERROR_SUCCESS)
891 goto done;
892 }
893
894 for (i = 0; i < num_records; i++)
895 {
896 r = construct_record(num_columns, types, records[i], path, &rec);
897 if (r != ERROR_SUCCESS)
898 goto done;
899
900 r = MSI_ViewModify(view, MSIMODIFY_INSERT, rec);
901 if (r != ERROR_SUCCESS)
902 {
903 msiobj_release(&rec->hdr);
904 goto done;
905 }
906
907 msiobj_release(&rec->hdr);
908 }
909
910 done:
911 msiobj_release(&view->hdr);
912 return r;
913 }
914
915 static UINT MSI_DatabaseImport(MSIDATABASE *db, LPCWSTR folder, LPCWSTR file)
916 {
917 UINT r;
918 DWORD len, i;
919 DWORD num_labels, num_types;
920 DWORD num_columns, num_records = 0;
921 LPWSTR *columns, *types, *labels;
922 LPWSTR path, ptr, data;
923 LPWSTR **records = NULL;
924 LPWSTR **temp_records;
925
926 static const WCHAR suminfo[] =
927 {'_','S','u','m','m','a','r','y','I','n','f','o','r','m','a','t','i','o','n',0};
928 static const WCHAR forcecodepage[] =
929 {'_','F','o','r','c','e','C','o','d','e','p','a','g','e',0};
930
931 TRACE("%p %s %s\n", db, debugstr_w(folder), debugstr_w(file) );
932
933 if( folder == NULL || file == NULL )
934 return ERROR_INVALID_PARAMETER;
935
936 len = lstrlenW(folder) + lstrlenW(szBackSlash) + lstrlenW(file) + 1;
937 path = msi_alloc( len * sizeof(WCHAR) );
938 if (!path)
939 return ERROR_OUTOFMEMORY;
940
941 lstrcpyW( path, folder );
942 lstrcatW( path, szBackSlash );
943 lstrcatW( path, file );
944
945 data = msi_read_text_archive( path, &len );
946
947 ptr = data;
948 msi_parse_line( &ptr, &columns, &num_columns, &len );
949 msi_parse_line( &ptr, &types, &num_types, &len );
950 msi_parse_line( &ptr, &labels, &num_labels, &len );
951
952 if (num_columns == 1 && !columns[0][0] && num_labels == 1 && !labels[0][0] &&
953 num_types == 2 && !strcmpW( types[1], forcecodepage ))
954 {
955 r = msi_set_string_table_codepage( db->strings, atoiW( types[0] ) );
956 goto done;
957 }
958
959 if (num_columns != num_types)
960 {
961 r = ERROR_FUNCTION_FAILED;
962 goto done;
963 }
964
965 records = msi_alloc(sizeof(LPWSTR *));
966 if (!records)
967 {
968 r = ERROR_OUTOFMEMORY;
969 goto done;
970 }
971
972 /* read in the table records */
973 while (len)
974 {
975 msi_parse_line( &ptr, &records[num_records], NULL, &len );
976
977 num_records++;
978 temp_records = msi_realloc(records, (num_records + 1) * sizeof(LPWSTR *));
979 if (!temp_records)
980 {
981 r = ERROR_OUTOFMEMORY;
982 goto done;
983 }
984 records = temp_records;
985 }
986
987 if (!strcmpW(labels[0], suminfo))
988 {
989 r = msi_add_suminfo( db, records, num_records, num_columns );
990 if (r != ERROR_SUCCESS)
991 {
992 r = ERROR_FUNCTION_FAILED;
993 goto done;
994 }
995 }
996 else
997 {
998 if (!TABLE_Exists(db, labels[0]))
999 {
1000 r = msi_add_table_to_db( db, columns, types, labels, num_labels, num_columns );
1001 if (r != ERROR_SUCCESS)
1002 {
1003 r = ERROR_FUNCTION_FAILED;
1004 goto done;
1005 }
1006 }
1007
1008 r = msi_add_records_to_table( db, columns, types, labels, records, num_columns, num_records, path );
1009 }
1010
1011 done:
1012 msi_free(path);
1013 msi_free(data);
1014 msi_free(columns);
1015 msi_free(types);
1016 msi_free(labels);
1017
1018 for (i = 0; i < num_records; i++)
1019 msi_free(records[i]);
1020
1021 msi_free(records);
1022
1023 return r;
1024 }
1025
1026 UINT WINAPI MsiDatabaseImportW(MSIHANDLE handle, LPCWSTR szFolder, LPCWSTR szFilename)
1027 {
1028 MSIDATABASE *db;
1029 UINT r;
1030
1031 TRACE("%x %s %s\n",handle,debugstr_w(szFolder), debugstr_w(szFilename));
1032
1033 db = msihandle2msiinfo( handle, MSIHANDLETYPE_DATABASE );
1034 if( !db )
1035 {
1036 IWineMsiRemoteDatabase *remote_database;
1037
1038 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( handle );
1039 if ( !remote_database )
1040 return ERROR_INVALID_HANDLE;
1041
1042 IWineMsiRemoteDatabase_Release( remote_database );
1043 WARN("MsiDatabaseImport not allowed during a custom action!\n");
1044
1045 return ERROR_SUCCESS;
1046 }
1047
1048 r = MSI_DatabaseImport( db, szFolder, szFilename );
1049 msiobj_release( &db->hdr );
1050 return r;
1051 }
1052
1053 UINT WINAPI MsiDatabaseImportA( MSIHANDLE handle,
1054 LPCSTR szFolder, LPCSTR szFilename )
1055 {
1056 LPWSTR path = NULL, file = NULL;
1057 UINT r = ERROR_OUTOFMEMORY;
1058
1059 TRACE("%x %s %s\n", handle, debugstr_a(szFolder), debugstr_a(szFilename));
1060
1061 if( szFolder )
1062 {
1063 path = strdupAtoW( szFolder );
1064 if( !path )
1065 goto end;
1066 }
1067
1068 if( szFilename )
1069 {
1070 file = strdupAtoW( szFilename );
1071 if( !file )
1072 goto end;
1073 }
1074
1075 r = MsiDatabaseImportW( handle, path, file );
1076
1077 end:
1078 msi_free( path );
1079 msi_free( file );
1080
1081 return r;
1082 }
1083
1084 static UINT msi_export_record( HANDLE handle, MSIRECORD *row, UINT start )
1085 {
1086 UINT i, count, len, r = ERROR_SUCCESS;
1087 const char *sep;
1088 char *buffer;
1089 DWORD sz;
1090
1091 len = 0x100;
1092 buffer = msi_alloc( len );
1093 if ( !buffer )
1094 return ERROR_OUTOFMEMORY;
1095
1096 count = MSI_RecordGetFieldCount( row );
1097 for ( i=start; i<=count; i++ )
1098 {
1099 sz = len;
1100 r = MSI_RecordGetStringA( row, i, buffer, &sz );
1101 if (r == ERROR_MORE_DATA)
1102 {
1103 char *p = msi_realloc( buffer, sz + 1 );
1104 if (!p)
1105 break;
1106 len = sz + 1;
1107 buffer = p;
1108 }
1109 sz = len;
1110 r = MSI_RecordGetStringA( row, i, buffer, &sz );
1111 if (r != ERROR_SUCCESS)
1112 break;
1113
1114 if (!WriteFile( handle, buffer, sz, &sz, NULL ))
1115 {
1116 r = ERROR_FUNCTION_FAILED;
1117 break;
1118 }
1119
1120 sep = (i < count) ? "\t" : "\r\n";
1121 if (!WriteFile( handle, sep, strlen(sep), &sz, NULL ))
1122 {
1123 r = ERROR_FUNCTION_FAILED;
1124 break;
1125 }
1126 }
1127 msi_free( buffer );
1128 return r;
1129 }
1130
1131 static UINT msi_export_row( MSIRECORD *row, void *arg )
1132 {
1133 return msi_export_record( arg, row, 1 );
1134 }
1135
1136 static UINT msi_export_forcecodepage( HANDLE handle, UINT codepage )
1137 {
1138 static const char fmt[] = "\r\n\r\n%u\t_ForceCodepage\r\n";
1139 char data[sizeof(fmt) + 10];
1140 DWORD sz;
1141
1142 sprintf( data, fmt, codepage );
1143
1144 sz = lstrlenA(data) + 1;
1145 if (!WriteFile(handle, data, sz, &sz, NULL))
1146 return ERROR_FUNCTION_FAILED;
1147
1148 return ERROR_SUCCESS;
1149 }
1150
1151 static UINT MSI_DatabaseExport( MSIDATABASE *db, LPCWSTR table,
1152 LPCWSTR folder, LPCWSTR file )
1153 {
1154 static const WCHAR query[] = {
1155 's','e','l','e','c','t',' ','*',' ','f','r','o','m',' ','%','s',0 };
1156 static const WCHAR forcecodepage[] = {
1157 '_','F','o','r','c','e','C','o','d','e','p','a','g','e',0 };
1158 MSIRECORD *rec = NULL;
1159 MSIQUERY *view = NULL;
1160 LPWSTR filename;
1161 HANDLE handle;
1162 UINT len, r;
1163
1164 TRACE("%p %s %s %s\n", db, debugstr_w(table),
1165 debugstr_w(folder), debugstr_w(file) );
1166
1167 if( folder == NULL || file == NULL )
1168 return ERROR_INVALID_PARAMETER;
1169
1170 len = lstrlenW(folder) + lstrlenW(file) + 2;
1171 filename = msi_alloc(len * sizeof (WCHAR));
1172 if (!filename)
1173 return ERROR_OUTOFMEMORY;
1174
1175 lstrcpyW( filename, folder );
1176 lstrcatW( filename, szBackSlash );
1177 lstrcatW( filename, file );
1178
1179 handle = CreateFileW( filename, GENERIC_READ | GENERIC_WRITE, 0,
1180 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
1181 msi_free( filename );
1182 if (handle == INVALID_HANDLE_VALUE)
1183 return ERROR_FUNCTION_FAILED;
1184
1185 if (!strcmpW( table, forcecodepage ))
1186 {
1187 UINT codepage = msi_get_string_table_codepage( db->strings );
1188 r = msi_export_forcecodepage( handle, codepage );
1189 goto done;
1190 }
1191
1192 r = MSI_OpenQuery( db, &view, query, table );
1193 if (r == ERROR_SUCCESS)
1194 {
1195 /* write out row 1, the column names */
1196 r = MSI_ViewGetColumnInfo(view, MSICOLINFO_NAMES, &rec);
1197 if (r == ERROR_SUCCESS)
1198 {
1199 msi_export_record( handle, rec, 1 );
1200 msiobj_release( &rec->hdr );
1201 }
1202
1203 /* write out row 2, the column types */
1204 r = MSI_ViewGetColumnInfo(view, MSICOLINFO_TYPES, &rec);
1205 if (r == ERROR_SUCCESS)
1206 {
1207 msi_export_record( handle, rec, 1 );
1208 msiobj_release( &rec->hdr );
1209 }
1210
1211 /* write out row 3, the table name + keys */
1212 r = MSI_DatabaseGetPrimaryKeys( db, table, &rec );
1213 if (r == ERROR_SUCCESS)
1214 {
1215 MSI_RecordSetStringW( rec, 0, table );
1216 msi_export_record( handle, rec, 0 );
1217 msiobj_release( &rec->hdr );
1218 }
1219
1220 /* write out row 4 onwards, the data */
1221 r = MSI_IterateRecords( view, 0, msi_export_row, handle );
1222 msiobj_release( &view->hdr );
1223 }
1224
1225 done:
1226 CloseHandle( handle );
1227 return r;
1228 }
1229
1230 /***********************************************************************
1231 * MsiExportDatabaseW [MSI.@]
1232 *
1233 * Writes a file containing the table data as tab separated ASCII.
1234 *
1235 * The format is as follows:
1236 *
1237 * row1 : colname1 <tab> colname2 <tab> .... colnameN <cr> <lf>
1238 * row2 : coltype1 <tab> coltype2 <tab> .... coltypeN <cr> <lf>
1239 * row3 : tablename <tab> key1 <tab> key2 <tab> ... keyM <cr> <lf>
1240 *
1241 * Followed by the data, starting at row 1 with one row per line
1242 *
1243 * row4 : data <tab> data <tab> data <tab> ... data <cr> <lf>
1244 */
1245 UINT WINAPI MsiDatabaseExportW( MSIHANDLE handle, LPCWSTR szTable,
1246 LPCWSTR szFolder, LPCWSTR szFilename )
1247 {
1248 MSIDATABASE *db;
1249 UINT r;
1250
1251 TRACE("%x %s %s %s\n", handle, debugstr_w(szTable),
1252 debugstr_w(szFolder), debugstr_w(szFilename));
1253
1254 db = msihandle2msiinfo( handle, MSIHANDLETYPE_DATABASE );
1255 if( !db )
1256 {
1257 IWineMsiRemoteDatabase *remote_database;
1258
1259 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( handle );
1260 if ( !remote_database )
1261 return ERROR_INVALID_HANDLE;
1262
1263 IWineMsiRemoteDatabase_Release( remote_database );
1264 WARN("MsiDatabaseExport not allowed during a custom action!\n");
1265
1266 return ERROR_SUCCESS;
1267 }
1268
1269 r = MSI_DatabaseExport( db, szTable, szFolder, szFilename );
1270 msiobj_release( &db->hdr );
1271 return r;
1272 }
1273
1274 UINT WINAPI MsiDatabaseExportA( MSIHANDLE handle, LPCSTR szTable,
1275 LPCSTR szFolder, LPCSTR szFilename )
1276 {
1277 LPWSTR path = NULL, file = NULL, table = NULL;
1278 UINT r = ERROR_OUTOFMEMORY;
1279
1280 TRACE("%x %s %s %s\n", handle, debugstr_a(szTable),
1281 debugstr_a(szFolder), debugstr_a(szFilename));
1282
1283 if( szTable )
1284 {
1285 table = strdupAtoW( szTable );
1286 if( !table )
1287 goto end;
1288 }
1289
1290 if( szFolder )
1291 {
1292 path = strdupAtoW( szFolder );
1293 if( !path )
1294 goto end;
1295 }
1296
1297 if( szFilename )
1298 {
1299 file = strdupAtoW( szFilename );
1300 if( !file )
1301 goto end;
1302 }
1303
1304 r = MsiDatabaseExportW( handle, table, path, file );
1305
1306 end:
1307 msi_free( table );
1308 msi_free( path );
1309 msi_free( file );
1310
1311 return r;
1312 }
1313
1314 UINT WINAPI MsiDatabaseMergeA(MSIHANDLE hDatabase, MSIHANDLE hDatabaseMerge,
1315 LPCSTR szTableName)
1316 {
1317 UINT r;
1318 LPWSTR table;
1319
1320 TRACE("(%d, %d, %s)\n", hDatabase, hDatabaseMerge,
1321 debugstr_a(szTableName));
1322
1323 table = strdupAtoW(szTableName);
1324 r = MsiDatabaseMergeW(hDatabase, hDatabaseMerge, table);
1325
1326 msi_free(table);
1327 return r;
1328 }
1329
1330 typedef struct _tagMERGETABLE
1331 {
1332 struct list entry;
1333 struct list rows;
1334 LPWSTR name;
1335 DWORD numconflicts;
1336 LPWSTR *columns;
1337 DWORD numcolumns;
1338 LPWSTR *types;
1339 DWORD numtypes;
1340 LPWSTR *labels;
1341 DWORD numlabels;
1342 } MERGETABLE;
1343
1344 typedef struct _tagMERGEROW
1345 {
1346 struct list entry;
1347 MSIRECORD *data;
1348 } MERGEROW;
1349
1350 typedef struct _tagMERGEDATA
1351 {
1352 MSIDATABASE *db;
1353 MSIDATABASE *merge;
1354 MERGETABLE *curtable;
1355 MSIQUERY *curview;
1356 struct list *tabledata;
1357 } MERGEDATA;
1358
1359 static BOOL merge_type_match(LPCWSTR type1, LPCWSTR type2)
1360 {
1361 if (((type1[0] == 'l') || (type1[0] == 's')) &&
1362 ((type2[0] == 'l') || (type2[0] == 's')))
1363 return TRUE;
1364
1365 if (((type1[0] == 'L') || (type1[0] == 'S')) &&
1366 ((type2[0] == 'L') || (type2[0] == 'S')))
1367 return TRUE;
1368
1369 return !strcmpW( type1, type2 );
1370 }
1371
1372 static UINT merge_verify_colnames(MSIQUERY *dbview, MSIQUERY *mergeview)
1373 {
1374 MSIRECORD *dbrec, *mergerec;
1375 UINT r, i, count;
1376
1377 r = MSI_ViewGetColumnInfo(dbview, MSICOLINFO_NAMES, &dbrec);
1378 if (r != ERROR_SUCCESS)
1379 return r;
1380
1381 r = MSI_ViewGetColumnInfo(mergeview, MSICOLINFO_NAMES, &mergerec);
1382 if (r != ERROR_SUCCESS)
1383 return r;
1384
1385 count = MSI_RecordGetFieldCount(dbrec);
1386 for (i = 1; i <= count; i++)
1387 {
1388 if (!MSI_RecordGetString(mergerec, i))
1389 break;
1390
1391 if (strcmpW( MSI_RecordGetString( dbrec, i ), MSI_RecordGetString( mergerec, i ) ))
1392 {
1393 r = ERROR_DATATYPE_MISMATCH;
1394 goto done;
1395 }
1396 }
1397
1398 msiobj_release(&dbrec->hdr);
1399 msiobj_release(&mergerec->hdr);
1400 dbrec = mergerec = NULL;
1401
1402 r = MSI_ViewGetColumnInfo(dbview, MSICOLINFO_TYPES, &dbrec);
1403 if (r != ERROR_SUCCESS)
1404 return r;
1405
1406 r = MSI_ViewGetColumnInfo(mergeview, MSICOLINFO_TYPES, &mergerec);
1407 if (r != ERROR_SUCCESS)
1408 return r;
1409
1410 count = MSI_RecordGetFieldCount(dbrec);
1411 for (i = 1; i <= count; i++)
1412 {
1413 if (!MSI_RecordGetString(mergerec, i))
1414 break;
1415
1416 if (!merge_type_match(MSI_RecordGetString(dbrec, i),
1417 MSI_RecordGetString(mergerec, i)))
1418 {
1419 r = ERROR_DATATYPE_MISMATCH;
1420 break;
1421 }
1422 }
1423
1424 done:
1425 msiobj_release(&dbrec->hdr);
1426 msiobj_release(&mergerec->hdr);
1427
1428 return r;
1429 }
1430
1431 static UINT merge_verify_primary_keys(MSIDATABASE *db, MSIDATABASE *mergedb,
1432 LPCWSTR table)
1433 {
1434 MSIRECORD *dbrec, *mergerec = NULL;
1435 UINT r, i, count;
1436
1437 r = MSI_DatabaseGetPrimaryKeys(db, table, &dbrec);
1438 if (r != ERROR_SUCCESS)
1439 return r;
1440
1441 r = MSI_DatabaseGetPrimaryKeys(mergedb, table, &mergerec);
1442 if (r != ERROR_SUCCESS)
1443 goto done;
1444
1445 count = MSI_RecordGetFieldCount(dbrec);
1446 if (count != MSI_RecordGetFieldCount(mergerec))
1447 {
1448 r = ERROR_DATATYPE_MISMATCH;
1449 goto done;
1450 }
1451
1452 for (i = 1; i <= count; i++)
1453 {
1454 if (strcmpW( MSI_RecordGetString( dbrec, i ), MSI_RecordGetString( mergerec, i ) ))
1455 {
1456 r = ERROR_DATATYPE_MISMATCH;
1457 goto done;
1458 }
1459 }
1460
1461 done:
1462 msiobj_release(&dbrec->hdr);
1463 msiobj_release(&mergerec->hdr);
1464
1465 return r;
1466 }
1467
1468 static LPWSTR get_key_value(MSIQUERY *view, LPCWSTR key, MSIRECORD *rec)
1469 {
1470 MSIRECORD *colnames;
1471 LPWSTR str, val;
1472 UINT r, i = 0, sz = 0;
1473 int cmp;
1474
1475 r = MSI_ViewGetColumnInfo(view, MSICOLINFO_NAMES, &colnames);
1476 if (r != ERROR_SUCCESS)
1477 return NULL;
1478
1479 do
1480 {
1481 str = msi_dup_record_field(colnames, ++i);
1482 cmp = strcmpW( key, str );
1483 msi_free(str);
1484 } while (cmp);
1485
1486 msiobj_release(&colnames->hdr);
1487
1488 r = MSI_RecordGetStringW(rec, i, NULL, &sz);
1489 if (r != ERROR_SUCCESS)
1490 return NULL;
1491 sz++;
1492
1493 if (MSI_RecordGetString(rec, i)) /* check record field is a string */
1494 {
1495 /* quote string record fields */
1496 const WCHAR szQuote[] = {'\'', 0};
1497 sz += 2;
1498 val = msi_alloc(sz*sizeof(WCHAR));
1499 if (!val)
1500 return NULL;
1501
1502 lstrcpyW(val, szQuote);
1503 r = MSI_RecordGetStringW(rec, i, val+1, &sz);
1504 lstrcpyW(val+1+sz, szQuote);
1505 }
1506 else
1507 {
1508 /* do not quote integer record fields */
1509 val = msi_alloc(sz*sizeof(WCHAR));
1510 if (!val)
1511 return NULL;
1512
1513 r = MSI_RecordGetStringW(rec, i, val, &sz);
1514 }
1515
1516 if (r != ERROR_SUCCESS)
1517 {
1518 ERR("failed to get string!\n");
1519 msi_free(val);
1520 return NULL;
1521 }
1522
1523 return val;
1524 }
1525
1526 static LPWSTR create_diff_row_query(MSIDATABASE *merge, MSIQUERY *view,
1527 LPWSTR table, MSIRECORD *rec)
1528 {
1529 LPWSTR query = NULL, clause = NULL, val;
1530 LPCWSTR setptr, key;
1531 DWORD size, oldsize;
1532 MSIRECORD *keys;
1533 UINT r, i, count;
1534
1535 static const WCHAR keyset[] = {
1536 '`','%','s','`',' ','=',' ','%','s',' ','A','N','D',' ',0};
1537 static const WCHAR lastkeyset[] = {
1538 '`','%','s','`',' ','=',' ','%','s',' ',0};
1539 static const WCHAR fmt[] = {'S','E','L','E','C','T',' ','*',' ',
1540 'F','R','O','M',' ','`','%','s','`',' ',
1541 'W','H','E','R','E',' ','%','s',0};
1542
1543 r = MSI_DatabaseGetPrimaryKeys(merge, table, &keys);
1544 if (r != ERROR_SUCCESS)
1545 return NULL;
1546
1547 clause = msi_alloc_zero(sizeof(WCHAR));
1548 if (!clause)
1549 goto done;
1550
1551 size = 1;
1552 count = MSI_RecordGetFieldCount(keys);
1553 for (i = 1; i <= count; i++)
1554 {
1555 key = MSI_RecordGetString(keys, i);
1556 val = get_key_value(view, key, rec);
1557
1558 if (i == count)
1559 setptr = lastkeyset;
1560 else
1561 setptr = keyset;
1562
1563 oldsize = size;
1564 size += lstrlenW(setptr) + lstrlenW(key) + lstrlenW(val) - 4;
1565 clause = msi_realloc(clause, size * sizeof (WCHAR));
1566 if (!clause)
1567 {
1568 msi_free(val);
1569 goto done;
1570 }
1571
1572 sprintfW(clause + oldsize - 1, setptr, key, val);
1573 msi_free(val);
1574 }
1575
1576 size = lstrlenW(fmt) + lstrlenW(table) + lstrlenW(clause) + 1;
1577 query = msi_alloc(size * sizeof(WCHAR));
1578 if (!query)
1579 goto done;
1580
1581 sprintfW(query, fmt, table, clause);
1582
1583 done:
1584 msi_free(clause);
1585 msiobj_release(&keys->hdr);
1586 return query;
1587 }
1588
1589 static UINT merge_diff_row(MSIRECORD *rec, LPVOID param)
1590 {
1591 MERGEDATA *data = param;
1592 MERGETABLE *table = data->curtable;
1593 MERGEROW *mergerow;
1594 MSIQUERY *dbview = NULL;
1595 MSIRECORD *row = NULL;
1596 LPWSTR query = NULL;
1597 UINT r = ERROR_SUCCESS;
1598
1599 if (TABLE_Exists(data->db, table->name))
1600 {
1601 query = create_diff_row_query(data->merge, data->curview, table->name, rec);
1602 if (!query)
1603 return ERROR_OUTOFMEMORY;
1604
1605 r = MSI_DatabaseOpenViewW(data->db, query, &dbview);
1606 if (r != ERROR_SUCCESS)
1607 goto done;
1608
1609 r = MSI_ViewExecute(dbview, NULL);
1610 if (r != ERROR_SUCCESS)
1611 goto done;
1612
1613 r = MSI_ViewFetch(dbview, &row);
1614 if (r == ERROR_SUCCESS && !MSI_RecordsAreEqual(rec, row))
1615 {
1616 table->numconflicts++;
1617 goto done;
1618 }
1619 else if (r != ERROR_NO_MORE_ITEMS)
1620 goto done;
1621
1622 r = ERROR_SUCCESS;
1623 }
1624
1625 mergerow = msi_alloc(sizeof(MERGEROW));
1626 if (!mergerow)
1627 {
1628 r = ERROR_OUTOFMEMORY;
1629 goto done;
1630 }
1631
1632 mergerow->data = MSI_CloneRecord(rec);
1633 if (!mergerow->data)
1634 {
1635 r = ERROR_OUTOFMEMORY;
1636 msi_free(mergerow);
1637 goto done;
1638 }
1639
1640 list_add_tail(&table->rows, &mergerow->entry);
1641
1642 done:
1643 msi_free(query);
1644 msiobj_release(&row->hdr);
1645 msiobj_release(&dbview->hdr);
1646 return r;
1647 }
1648
1649 static UINT msi_get_table_labels(MSIDATABASE *db, LPCWSTR table, LPWSTR **labels, DWORD *numlabels)
1650 {
1651 UINT r, i, count;
1652 MSIRECORD *prec = NULL;
1653
1654 r = MSI_DatabaseGetPrimaryKeys(db, table, &prec);
1655 if (r != ERROR_SUCCESS)
1656 return r;
1657
1658 count = MSI_RecordGetFieldCount(prec);
1659 *numlabels = count + 1;
1660 *labels = msi_alloc((*numlabels)*sizeof(LPWSTR));
1661 if (!*labels)
1662 {
1663 r = ERROR_OUTOFMEMORY;
1664 goto end;
1665 }
1666
1667 (*labels)[0] = strdupW(table);
1668 for (i=1; i<=count; i++ )
1669 {
1670 (*labels)[i] = strdupW(MSI_RecordGetString(prec, i));
1671 }
1672
1673 end:
1674 msiobj_release( &prec->hdr );
1675 return r;
1676 }
1677
1678 static UINT msi_get_query_columns(MSIQUERY *query, LPWSTR **columns, DWORD *numcolumns)
1679 {
1680 UINT r, i, count;
1681 MSIRECORD *prec = NULL;
1682
1683 r = MSI_ViewGetColumnInfo(query, MSICOLINFO_NAMES, &prec);
1684 if (r != ERROR_SUCCESS)
1685 return r;
1686
1687 count = MSI_RecordGetFieldCount(prec);
1688 *columns = msi_alloc(count*sizeof(LPWSTR));
1689 if (!*columns)
1690 {
1691 r = ERROR_OUTOFMEMORY;
1692 goto end;
1693 }
1694
1695 for (i=1; i<=count; i++ )
1696 {
1697 (*columns)[i-1] = strdupW(MSI_RecordGetString(prec, i));
1698 }
1699
1700 *numcolumns = count;
1701
1702 end:
1703 msiobj_release( &prec->hdr );
1704 return r;
1705 }
1706
1707 static UINT msi_get_query_types(MSIQUERY *query, LPWSTR **types, DWORD *numtypes)
1708 {
1709 UINT r, i, count;
1710 MSIRECORD *prec = NULL;
1711
1712 r = MSI_ViewGetColumnInfo(query, MSICOLINFO_TYPES, &prec);
1713 if (r != ERROR_SUCCESS)
1714 return r;
1715
1716 count = MSI_RecordGetFieldCount(prec);
1717 *types = msi_alloc(count*sizeof(LPWSTR));
1718 if (!*types)
1719 {
1720 r = ERROR_OUTOFMEMORY;
1721 goto end;
1722 }
1723
1724 *numtypes = count;
1725 for (i=1; i<=count; i++ )
1726 {
1727 (*types)[i-1] = strdupW(MSI_RecordGetString(prec, i));
1728 }
1729
1730 end:
1731 msiobj_release( &prec->hdr );
1732 return r;
1733 }
1734
1735 static void merge_free_rows(MERGETABLE *table)
1736 {
1737 struct list *item, *cursor;
1738
1739 LIST_FOR_EACH_SAFE(item, cursor, &table->rows)
1740 {
1741 MERGEROW *row = LIST_ENTRY(item, MERGEROW, entry);
1742
1743 list_remove(&row->entry);
1744 msiobj_release(&row->data->hdr);
1745 msi_free(row);
1746 }
1747 }
1748
1749 static void free_merge_table(MERGETABLE *table)
1750 {
1751 UINT i;
1752
1753 if (table->labels != NULL)
1754 {
1755 for (i = 0; i < table->numlabels; i++)
1756 msi_free(table->labels[i]);
1757
1758 msi_free(table->labels);
1759 }
1760
1761 if (table->columns != NULL)
1762 {
1763 for (i = 0; i < table->numcolumns; i++)
1764 msi_free(table->columns[i]);
1765
1766 msi_free(table->columns);
1767 }
1768
1769 if (table->types != NULL)
1770 {
1771 for (i = 0; i < table->numtypes; i++)
1772 msi_free(table->types[i]);
1773
1774 msi_free(table->types);
1775 }
1776
1777 msi_free(table->name);
1778 merge_free_rows(table);
1779
1780 msi_free(table);
1781 }
1782
1783 static UINT msi_get_merge_table (MSIDATABASE *db, LPCWSTR name, MERGETABLE **ptable)
1784 {
1785 UINT r;
1786 MERGETABLE *table;
1787 MSIQUERY *mergeview = NULL;
1788
1789 static const WCHAR query[] = {'S','E','L','E','C','T',' ','*',' ',
1790 'F','R','O','M',' ','`','%','s','`',0};
1791
1792 table = msi_alloc_zero(sizeof(MERGETABLE));
1793 if (!table)
1794 {
1795 *ptable = NULL;
1796 return ERROR_OUTOFMEMORY;
1797 }
1798
1799 r = msi_get_table_labels(db, name, &table->labels, &table->numlabels);
1800 if (r != ERROR_SUCCESS)
1801 goto err;
1802
1803 r = MSI_OpenQuery(db, &mergeview, query, name);
1804 if (r != ERROR_SUCCESS)
1805 goto err;
1806
1807 r = msi_get_query_columns(mergeview, &table->columns, &table->numcolumns);
1808 if (r != ERROR_SUCCESS)
1809 goto err;
1810
1811 r = msi_get_query_types(mergeview, &table->types, &table->numtypes);
1812 if (r != ERROR_SUCCESS)
1813 goto err;
1814
1815 list_init(&table->rows);
1816
1817 table->name = strdupW(name);
1818 table->numconflicts = 0;
1819
1820 msiobj_release(&mergeview->hdr);
1821 *ptable = table;
1822 return ERROR_SUCCESS;
1823
1824 err:
1825 msiobj_release(&mergeview->hdr);
1826 free_merge_table(table);
1827 *ptable = NULL;
1828 return r;
1829 }
1830
1831 static UINT merge_diff_tables(MSIRECORD *rec, LPVOID param)
1832 {
1833 MERGEDATA *data = param;
1834 MERGETABLE *table;
1835 MSIQUERY *dbview = NULL;
1836 MSIQUERY *mergeview = NULL;
1837 LPCWSTR name;
1838 UINT r;
1839
1840 static const WCHAR query[] = {'S','E','L','E','C','T',' ','*',' ',
1841 'F','R','O','M',' ','`','%','s','`',0};
1842
1843 name = MSI_RecordGetString(rec, 1);
1844
1845 r = MSI_OpenQuery(data->merge, &mergeview, query, name);
1846 if (r != ERROR_SUCCESS)
1847 goto done;
1848
1849 if (TABLE_Exists(data->db, name))
1850 {
1851 r = MSI_OpenQuery(data->db, &dbview, query, name);
1852 if (r != ERROR_SUCCESS)
1853 goto done;
1854
1855 r = merge_verify_colnames(dbview, mergeview);
1856 if (r != ERROR_SUCCESS)
1857 goto done;
1858
1859 r = merge_verify_primary_keys(data->db, data->merge, name);
1860 if (r != ERROR_SUCCESS)
1861 goto done;
1862 }
1863
1864 r = msi_get_merge_table(data->merge, name, &table);
1865 if (r != ERROR_SUCCESS)
1866 goto done;
1867
1868 data->curtable = table;
1869 data->curview = mergeview;
1870 r = MSI_IterateRecords(mergeview, NULL, merge_diff_row, data);
1871 if (r != ERROR_SUCCESS)
1872 {
1873 free_merge_table(table);
1874 goto done;
1875 }
1876
1877 list_add_tail(data->tabledata, &table->entry);
1878
1879 done:
1880 msiobj_release(&dbview->hdr);
1881 msiobj_release(&mergeview->hdr);
1882 return r;
1883 }
1884
1885 static UINT gather_merge_data(MSIDATABASE *db, MSIDATABASE *merge,
1886 struct list *tabledata)
1887 {
1888 static const WCHAR query[] = {
1889 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1890 '`','_','T','a','b','l','e','s','`',0};
1891 MSIQUERY *view;
1892 MERGEDATA data;
1893 UINT r;
1894
1895 r = MSI_DatabaseOpenViewW(merge, query, &view);
1896 if (r != ERROR_SUCCESS)
1897 return r;
1898
1899 data.db = db;
1900 data.merge = merge;
1901 data.tabledata = tabledata;
1902 r = MSI_IterateRecords(view, NULL, merge_diff_tables, &data);
1903 msiobj_release(&view->hdr);
1904 return r;
1905 }
1906
1907 static UINT merge_table(MSIDATABASE *db, MERGETABLE *table)
1908 {
1909 UINT r;
1910 MERGEROW *row;
1911 MSIVIEW *tv;
1912
1913 if (!TABLE_Exists(db, table->name))
1914 {
1915 r = msi_add_table_to_db(db, table->columns, table->types,
1916 table->labels, table->numlabels, table->numcolumns);
1917 if (r != ERROR_SUCCESS)
1918 return ERROR_FUNCTION_FAILED;
1919 }
1920
1921 LIST_FOR_EACH_ENTRY(row, &table->rows, MERGEROW, entry)
1922 {
1923 r = TABLE_CreateView(db, table->name, &tv);
1924 if (r != ERROR_SUCCESS)
1925 return r;
1926
1927 r = tv->ops->insert_row(tv, row->data, -1, FALSE);
1928 tv->ops->delete(tv);
1929
1930 if (r != ERROR_SUCCESS)
1931 return r;
1932 }
1933
1934 return ERROR_SUCCESS;
1935 }
1936
1937 static UINT update_merge_errors(MSIDATABASE *db, LPCWSTR error,
1938 LPWSTR table, DWORD numconflicts)
1939 {
1940 UINT r;
1941 MSIQUERY *view;
1942
1943 static const WCHAR create[] = {
1944 'C','R','E','A','T','E',' ','T','A','B','L','E',' ',
1945 '`','%','s','`',' ','(','`','T','a','b','l','e','`',' ',
1946 'C','H','A','R','(','2','5','5',')',' ','N','O','T',' ',
1947 'N','U','L','L',',',' ','`','N','u','m','R','o','w','M','e','r','g','e',
1948 'C','o','n','f','l','i','c','t','s','`',' ','S','H','O','R','T',' ',
1949 'N','O','T',' ','N','U','L','L',' ','P','R','I','M','A','R','Y',' ',
1950 'K','E','Y',' ','`','T','a','b','l','e','`',')',0};
1951 static const WCHAR insert[] = {
1952 'I','N','S','E','R','T',' ','I','N','T','O',' ',
1953 '`','%','s','`',' ','(','`','T','a','b','l','e','`',',',' ',
1954 '`','N','u','m','R','o','w','M','e','r','g','e',
1955 'C','o','n','f','l','i','c','t','s','`',')',' ','V','A','L','U','E','S',
1956 ' ','(','\'','%','s','\'',',',' ','%','d',')',0};
1957
1958 if (!TABLE_Exists(db, error))
1959 {
1960 r = MSI_OpenQuery(db, &view, create, error);
1961 if (r != ERROR_SUCCESS)
1962 return r;
1963
1964 r = MSI_ViewExecute(view, NULL);
1965 msiobj_release(&view->hdr);
1966 if (r != ERROR_SUCCESS)
1967 return r;
1968 }
1969
1970 r = MSI_OpenQuery(db, &view, insert, error, table, numconflicts);
1971 if (r != ERROR_SUCCESS)
1972 return r;
1973
1974 r = MSI_ViewExecute(view, NULL);
1975 msiobj_release(&view->hdr);
1976 return r;
1977 }
1978
1979 UINT WINAPI MsiDatabaseMergeW(MSIHANDLE hDatabase, MSIHANDLE hDatabaseMerge,
1980 LPCWSTR szTableName)
1981 {
1982 struct list tabledata = LIST_INIT(tabledata);
1983 struct list *item, *cursor;
1984 MSIDATABASE *db, *merge;
1985 MERGETABLE *table;
1986 BOOL conflicts;
1987 UINT r;
1988
1989 TRACE("(%d, %d, %s)\n", hDatabase, hDatabaseMerge,
1990 debugstr_w(szTableName));
1991
1992 if (szTableName && !*szTableName)
1993 return ERROR_INVALID_TABLE;
1994
1995 db = msihandle2msiinfo(hDatabase, MSIHANDLETYPE_DATABASE);
1996 merge = msihandle2msiinfo(hDatabaseMerge, MSIHANDLETYPE_DATABASE);
1997 if (!db || !merge)
1998 {
1999 r = ERROR_INVALID_HANDLE;
2000 goto done;
2001 }
2002
2003 r = gather_merge_data(db, merge, &tabledata);
2004 if (r != ERROR_SUCCESS)
2005 goto done;
2006
2007 conflicts = FALSE;
2008 LIST_FOR_EACH_ENTRY(table, &tabledata, MERGETABLE, entry)
2009 {
2010 if (table->numconflicts)
2011 {
2012 conflicts = TRUE;
2013
2014 r = update_merge_errors(db, szTableName, table->name,
2015 table->numconflicts);
2016 if (r != ERROR_SUCCESS)
2017 break;
2018 }
2019 else
2020 {
2021 r = merge_table(db, table);
2022 if (r != ERROR_SUCCESS)
2023 break;
2024 }
2025 }
2026
2027 LIST_FOR_EACH_SAFE(item, cursor, &tabledata)
2028 {
2029 MERGETABLE *table = LIST_ENTRY(item, MERGETABLE, entry);
2030 list_remove(&table->entry);
2031 free_merge_table(table);
2032 }
2033
2034 if (conflicts)
2035 r = ERROR_FUNCTION_FAILED;
2036
2037 done:
2038 msiobj_release(&db->hdr);
2039 msiobj_release(&merge->hdr);
2040 return r;
2041 }
2042
2043 MSIDBSTATE WINAPI MsiGetDatabaseState( MSIHANDLE handle )
2044 {
2045 MSIDBSTATE ret = MSIDBSTATE_READ;
2046 MSIDATABASE *db;
2047
2048 TRACE("%d\n", handle);
2049
2050 db = msihandle2msiinfo( handle, MSIHANDLETYPE_DATABASE );
2051 if( !db )
2052 {
2053 IWineMsiRemoteDatabase *remote_database;
2054
2055 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( handle );
2056 if ( !remote_database )
2057 return MSIDBSTATE_ERROR;
2058
2059 IWineMsiRemoteDatabase_Release( remote_database );
2060 WARN("MsiGetDatabaseState not allowed during a custom action!\n");
2061
2062 return MSIDBSTATE_READ;
2063 }
2064
2065 if (db->mode != MSIDBOPEN_READONLY )
2066 ret = MSIDBSTATE_WRITE;
2067 msiobj_release( &db->hdr );
2068
2069 return ret;
2070 }
2071
2072 typedef struct _msi_remote_database_impl {
2073 IWineMsiRemoteDatabase IWineMsiRemoteDatabase_iface;
2074 MSIHANDLE database;
2075 LONG refs;
2076 } msi_remote_database_impl;
2077
2078 static inline msi_remote_database_impl *impl_from_IWineMsiRemoteDatabase( IWineMsiRemoteDatabase *iface )
2079 {
2080 return CONTAINING_RECORD(iface, msi_remote_database_impl, IWineMsiRemoteDatabase_iface);
2081 }
2082
2083 static HRESULT WINAPI mrd_QueryInterface( IWineMsiRemoteDatabase *iface,
2084 REFIID riid,LPVOID *ppobj)
2085 {
2086 if( IsEqualCLSID( riid, &IID_IUnknown ) ||
2087 IsEqualCLSID( riid, &IID_IWineMsiRemoteDatabase ) )
2088 {
2089 IWineMsiRemoteDatabase_AddRef( iface );
2090 *ppobj = iface;
2091 return S_OK;
2092 }
2093
2094 return E_NOINTERFACE;
2095 }
2096
2097 static ULONG WINAPI mrd_AddRef( IWineMsiRemoteDatabase *iface )
2098 {
2099 msi_remote_database_impl* This = impl_from_IWineMsiRemoteDatabase( iface );
2100
2101 return InterlockedIncrement( &This->refs );
2102 }
2103
2104 static ULONG WINAPI mrd_Release( IWineMsiRemoteDatabase *iface )
2105 {
2106 msi_remote_database_impl* This = impl_from_IWineMsiRemoteDatabase( iface );
2107 ULONG r;
2108
2109 r = InterlockedDecrement( &This->refs );
2110 if (r == 0)
2111 {
2112 MsiCloseHandle( This->database );
2113 msi_free( This );
2114 }
2115 return r;
2116 }
2117
2118 static HRESULT WINAPI mrd_IsTablePersistent( IWineMsiRemoteDatabase *iface,
2119 LPCWSTR table, MSICONDITION *persistent )
2120 {
2121 msi_remote_database_impl *This = impl_from_IWineMsiRemoteDatabase( iface );
2122 *persistent = MsiDatabaseIsTablePersistentW(This->database, table);
2123 return S_OK;
2124 }
2125
2126 static HRESULT WINAPI mrd_GetPrimaryKeys( IWineMsiRemoteDatabase *iface,
2127 LPCWSTR table, MSIHANDLE *keys )
2128 {
2129 msi_remote_database_impl *This = impl_from_IWineMsiRemoteDatabase( iface );
2130 UINT r = MsiDatabaseGetPrimaryKeysW(This->database, table, keys);
2131 return HRESULT_FROM_WIN32(r);
2132 }
2133
2134 static HRESULT WINAPI mrd_GetSummaryInformation( IWineMsiRemoteDatabase *iface,
2135 UINT updatecount, MSIHANDLE *suminfo )
2136 {
2137 msi_remote_database_impl *This = impl_from_IWineMsiRemoteDatabase( iface );
2138 UINT r = MsiGetSummaryInformationW(This->database, NULL, updatecount, suminfo);
2139 return HRESULT_FROM_WIN32(r);
2140 }
2141
2142 static HRESULT WINAPI mrd_OpenView( IWineMsiRemoteDatabase *iface,
2143 LPCWSTR query, MSIHANDLE *view )
2144 {
2145 msi_remote_database_impl *This = impl_from_IWineMsiRemoteDatabase( iface );
2146 UINT r = MsiDatabaseOpenViewW(This->database, query, view);
2147 return HRESULT_FROM_WIN32(r);
2148 }
2149
2150 static HRESULT WINAPI mrd_SetMsiHandle( IWineMsiRemoteDatabase *iface, MSIHANDLE handle )
2151 {
2152 msi_remote_database_impl* This = impl_from_IWineMsiRemoteDatabase( iface );
2153 This->database = handle;
2154 return S_OK;
2155 }
2156
2157 static const IWineMsiRemoteDatabaseVtbl msi_remote_database_vtbl =
2158 {
2159 mrd_QueryInterface,
2160 mrd_AddRef,
2161 mrd_Release,
2162 mrd_IsTablePersistent,
2163 mrd_GetPrimaryKeys,
2164 mrd_GetSummaryInformation,
2165 mrd_OpenView,
2166 mrd_SetMsiHandle,
2167 };
2168
2169 HRESULT create_msi_remote_database( IUnknown *pOuter, LPVOID *ppObj )
2170 {
2171 msi_remote_database_impl *This;
2172
2173 This = msi_alloc( sizeof *This );
2174 if (!This)
2175 return E_OUTOFMEMORY;
2176
2177 This->IWineMsiRemoteDatabase_iface.lpVtbl = &msi_remote_database_vtbl;
2178 This->database = 0;
2179 This->refs = 1;
2180
2181 *ppObj = This;
2182
2183 return S_OK;
2184 }