f10889db2de85e8c872b91c34241f44b03e289ff
[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 {
1384 msiobj_release(&dbrec->hdr);
1385 return r;
1386 }
1387
1388 count = MSI_RecordGetFieldCount(dbrec);
1389 for (i = 1; i <= count; i++)
1390 {
1391 if (!MSI_RecordGetString(mergerec, i))
1392 break;
1393
1394 if (strcmpW( MSI_RecordGetString( dbrec, i ), MSI_RecordGetString( mergerec, i ) ))
1395 {
1396 r = ERROR_DATATYPE_MISMATCH;
1397 goto done;
1398 }
1399 }
1400
1401 msiobj_release(&dbrec->hdr);
1402 msiobj_release(&mergerec->hdr);
1403 dbrec = mergerec = NULL;
1404
1405 r = MSI_ViewGetColumnInfo(dbview, MSICOLINFO_TYPES, &dbrec);
1406 if (r != ERROR_SUCCESS)
1407 return r;
1408
1409 r = MSI_ViewGetColumnInfo(mergeview, MSICOLINFO_TYPES, &mergerec);
1410 if (r != ERROR_SUCCESS)
1411 {
1412 msiobj_release(&dbrec->hdr);
1413 return r;
1414 }
1415
1416 count = MSI_RecordGetFieldCount(dbrec);
1417 for (i = 1; i <= count; i++)
1418 {
1419 if (!MSI_RecordGetString(mergerec, i))
1420 break;
1421
1422 if (!merge_type_match(MSI_RecordGetString(dbrec, i),
1423 MSI_RecordGetString(mergerec, i)))
1424 {
1425 r = ERROR_DATATYPE_MISMATCH;
1426 break;
1427 }
1428 }
1429
1430 done:
1431 msiobj_release(&dbrec->hdr);
1432 msiobj_release(&mergerec->hdr);
1433
1434 return r;
1435 }
1436
1437 static UINT merge_verify_primary_keys(MSIDATABASE *db, MSIDATABASE *mergedb,
1438 LPCWSTR table)
1439 {
1440 MSIRECORD *dbrec, *mergerec = NULL;
1441 UINT r, i, count;
1442
1443 r = MSI_DatabaseGetPrimaryKeys(db, table, &dbrec);
1444 if (r != ERROR_SUCCESS)
1445 return r;
1446
1447 r = MSI_DatabaseGetPrimaryKeys(mergedb, table, &mergerec);
1448 if (r != ERROR_SUCCESS)
1449 goto done;
1450
1451 count = MSI_RecordGetFieldCount(dbrec);
1452 if (count != MSI_RecordGetFieldCount(mergerec))
1453 {
1454 r = ERROR_DATATYPE_MISMATCH;
1455 goto done;
1456 }
1457
1458 for (i = 1; i <= count; i++)
1459 {
1460 if (strcmpW( MSI_RecordGetString( dbrec, i ), MSI_RecordGetString( mergerec, i ) ))
1461 {
1462 r = ERROR_DATATYPE_MISMATCH;
1463 goto done;
1464 }
1465 }
1466
1467 done:
1468 msiobj_release(&dbrec->hdr);
1469 msiobj_release(&mergerec->hdr);
1470
1471 return r;
1472 }
1473
1474 static LPWSTR get_key_value(MSIQUERY *view, LPCWSTR key, MSIRECORD *rec)
1475 {
1476 MSIRECORD *colnames;
1477 LPWSTR str, val;
1478 UINT r, i = 0, sz = 0;
1479 int cmp;
1480
1481 r = MSI_ViewGetColumnInfo(view, MSICOLINFO_NAMES, &colnames);
1482 if (r != ERROR_SUCCESS)
1483 return NULL;
1484
1485 do
1486 {
1487 str = msi_dup_record_field(colnames, ++i);
1488 cmp = strcmpW( key, str );
1489 msi_free(str);
1490 } while (cmp);
1491
1492 msiobj_release(&colnames->hdr);
1493
1494 r = MSI_RecordGetStringW(rec, i, NULL, &sz);
1495 if (r != ERROR_SUCCESS)
1496 return NULL;
1497 sz++;
1498
1499 if (MSI_RecordGetString(rec, i)) /* check record field is a string */
1500 {
1501 /* quote string record fields */
1502 const WCHAR szQuote[] = {'\'', 0};
1503 sz += 2;
1504 val = msi_alloc(sz*sizeof(WCHAR));
1505 if (!val)
1506 return NULL;
1507
1508 lstrcpyW(val, szQuote);
1509 r = MSI_RecordGetStringW(rec, i, val+1, &sz);
1510 lstrcpyW(val+1+sz, szQuote);
1511 }
1512 else
1513 {
1514 /* do not quote integer record fields */
1515 val = msi_alloc(sz*sizeof(WCHAR));
1516 if (!val)
1517 return NULL;
1518
1519 r = MSI_RecordGetStringW(rec, i, val, &sz);
1520 }
1521
1522 if (r != ERROR_SUCCESS)
1523 {
1524 ERR("failed to get string!\n");
1525 msi_free(val);
1526 return NULL;
1527 }
1528
1529 return val;
1530 }
1531
1532 static LPWSTR create_diff_row_query(MSIDATABASE *merge, MSIQUERY *view,
1533 LPWSTR table, MSIRECORD *rec)
1534 {
1535 LPWSTR query = NULL, clause = NULL, val;
1536 LPCWSTR setptr, key;
1537 DWORD size, oldsize;
1538 MSIRECORD *keys;
1539 UINT r, i, count;
1540
1541 static const WCHAR keyset[] = {
1542 '`','%','s','`',' ','=',' ','%','s',' ','A','N','D',' ',0};
1543 static const WCHAR lastkeyset[] = {
1544 '`','%','s','`',' ','=',' ','%','s',' ',0};
1545 static const WCHAR fmt[] = {'S','E','L','E','C','T',' ','*',' ',
1546 'F','R','O','M',' ','`','%','s','`',' ',
1547 'W','H','E','R','E',' ','%','s',0};
1548
1549 r = MSI_DatabaseGetPrimaryKeys(merge, table, &keys);
1550 if (r != ERROR_SUCCESS)
1551 return NULL;
1552
1553 clause = msi_alloc_zero(sizeof(WCHAR));
1554 if (!clause)
1555 goto done;
1556
1557 size = 1;
1558 count = MSI_RecordGetFieldCount(keys);
1559 for (i = 1; i <= count; i++)
1560 {
1561 key = MSI_RecordGetString(keys, i);
1562 val = get_key_value(view, key, rec);
1563
1564 if (i == count)
1565 setptr = lastkeyset;
1566 else
1567 setptr = keyset;
1568
1569 oldsize = size;
1570 size += lstrlenW(setptr) + lstrlenW(key) + lstrlenW(val) - 4;
1571 clause = msi_realloc(clause, size * sizeof (WCHAR));
1572 if (!clause)
1573 {
1574 msi_free(val);
1575 goto done;
1576 }
1577
1578 sprintfW(clause + oldsize - 1, setptr, key, val);
1579 msi_free(val);
1580 }
1581
1582 size = lstrlenW(fmt) + lstrlenW(table) + lstrlenW(clause) + 1;
1583 query = msi_alloc(size * sizeof(WCHAR));
1584 if (!query)
1585 goto done;
1586
1587 sprintfW(query, fmt, table, clause);
1588
1589 done:
1590 msi_free(clause);
1591 msiobj_release(&keys->hdr);
1592 return query;
1593 }
1594
1595 static UINT merge_diff_row(MSIRECORD *rec, LPVOID param)
1596 {
1597 MERGEDATA *data = param;
1598 MERGETABLE *table = data->curtable;
1599 MERGEROW *mergerow;
1600 MSIQUERY *dbview = NULL;
1601 MSIRECORD *row = NULL;
1602 LPWSTR query = NULL;
1603 UINT r = ERROR_SUCCESS;
1604
1605 if (TABLE_Exists(data->db, table->name))
1606 {
1607 query = create_diff_row_query(data->merge, data->curview, table->name, rec);
1608 if (!query)
1609 return ERROR_OUTOFMEMORY;
1610
1611 r = MSI_DatabaseOpenViewW(data->db, query, &dbview);
1612 if (r != ERROR_SUCCESS)
1613 goto done;
1614
1615 r = MSI_ViewExecute(dbview, NULL);
1616 if (r != ERROR_SUCCESS)
1617 goto done;
1618
1619 r = MSI_ViewFetch(dbview, &row);
1620 if (r == ERROR_SUCCESS && !MSI_RecordsAreEqual(rec, row))
1621 {
1622 table->numconflicts++;
1623 goto done;
1624 }
1625 else if (r != ERROR_NO_MORE_ITEMS)
1626 goto done;
1627
1628 r = ERROR_SUCCESS;
1629 }
1630
1631 mergerow = msi_alloc(sizeof(MERGEROW));
1632 if (!mergerow)
1633 {
1634 r = ERROR_OUTOFMEMORY;
1635 goto done;
1636 }
1637
1638 mergerow->data = MSI_CloneRecord(rec);
1639 if (!mergerow->data)
1640 {
1641 r = ERROR_OUTOFMEMORY;
1642 msi_free(mergerow);
1643 goto done;
1644 }
1645
1646 list_add_tail(&table->rows, &mergerow->entry);
1647
1648 done:
1649 msi_free(query);
1650 msiobj_release(&row->hdr);
1651 msiobj_release(&dbview->hdr);
1652 return r;
1653 }
1654
1655 static UINT msi_get_table_labels(MSIDATABASE *db, LPCWSTR table, LPWSTR **labels, DWORD *numlabels)
1656 {
1657 UINT r, i, count;
1658 MSIRECORD *prec = NULL;
1659
1660 r = MSI_DatabaseGetPrimaryKeys(db, table, &prec);
1661 if (r != ERROR_SUCCESS)
1662 return r;
1663
1664 count = MSI_RecordGetFieldCount(prec);
1665 *numlabels = count + 1;
1666 *labels = msi_alloc((*numlabels)*sizeof(LPWSTR));
1667 if (!*labels)
1668 {
1669 r = ERROR_OUTOFMEMORY;
1670 goto end;
1671 }
1672
1673 (*labels)[0] = strdupW(table);
1674 for (i=1; i<=count; i++ )
1675 {
1676 (*labels)[i] = strdupW(MSI_RecordGetString(prec, i));
1677 }
1678
1679 end:
1680 msiobj_release( &prec->hdr );
1681 return r;
1682 }
1683
1684 static UINT msi_get_query_columns(MSIQUERY *query, LPWSTR **columns, DWORD *numcolumns)
1685 {
1686 UINT r, i, count;
1687 MSIRECORD *prec = NULL;
1688
1689 r = MSI_ViewGetColumnInfo(query, MSICOLINFO_NAMES, &prec);
1690 if (r != ERROR_SUCCESS)
1691 return r;
1692
1693 count = MSI_RecordGetFieldCount(prec);
1694 *columns = msi_alloc(count*sizeof(LPWSTR));
1695 if (!*columns)
1696 {
1697 r = ERROR_OUTOFMEMORY;
1698 goto end;
1699 }
1700
1701 for (i=1; i<=count; i++ )
1702 {
1703 (*columns)[i-1] = strdupW(MSI_RecordGetString(prec, i));
1704 }
1705
1706 *numcolumns = count;
1707
1708 end:
1709 msiobj_release( &prec->hdr );
1710 return r;
1711 }
1712
1713 static UINT msi_get_query_types(MSIQUERY *query, LPWSTR **types, DWORD *numtypes)
1714 {
1715 UINT r, i, count;
1716 MSIRECORD *prec = NULL;
1717
1718 r = MSI_ViewGetColumnInfo(query, MSICOLINFO_TYPES, &prec);
1719 if (r != ERROR_SUCCESS)
1720 return r;
1721
1722 count = MSI_RecordGetFieldCount(prec);
1723 *types = msi_alloc(count*sizeof(LPWSTR));
1724 if (!*types)
1725 {
1726 r = ERROR_OUTOFMEMORY;
1727 goto end;
1728 }
1729
1730 *numtypes = count;
1731 for (i=1; i<=count; i++ )
1732 {
1733 (*types)[i-1] = strdupW(MSI_RecordGetString(prec, i));
1734 }
1735
1736 end:
1737 msiobj_release( &prec->hdr );
1738 return r;
1739 }
1740
1741 static void merge_free_rows(MERGETABLE *table)
1742 {
1743 struct list *item, *cursor;
1744
1745 LIST_FOR_EACH_SAFE(item, cursor, &table->rows)
1746 {
1747 MERGEROW *row = LIST_ENTRY(item, MERGEROW, entry);
1748
1749 list_remove(&row->entry);
1750 msiobj_release(&row->data->hdr);
1751 msi_free(row);
1752 }
1753 }
1754
1755 static void free_merge_table(MERGETABLE *table)
1756 {
1757 UINT i;
1758
1759 if (table->labels != NULL)
1760 {
1761 for (i = 0; i < table->numlabels; i++)
1762 msi_free(table->labels[i]);
1763
1764 msi_free(table->labels);
1765 }
1766
1767 if (table->columns != NULL)
1768 {
1769 for (i = 0; i < table->numcolumns; i++)
1770 msi_free(table->columns[i]);
1771
1772 msi_free(table->columns);
1773 }
1774
1775 if (table->types != NULL)
1776 {
1777 for (i = 0; i < table->numtypes; i++)
1778 msi_free(table->types[i]);
1779
1780 msi_free(table->types);
1781 }
1782
1783 msi_free(table->name);
1784 merge_free_rows(table);
1785
1786 msi_free(table);
1787 }
1788
1789 static UINT msi_get_merge_table (MSIDATABASE *db, LPCWSTR name, MERGETABLE **ptable)
1790 {
1791 UINT r;
1792 MERGETABLE *table;
1793 MSIQUERY *mergeview = NULL;
1794
1795 static const WCHAR query[] = {'S','E','L','E','C','T',' ','*',' ',
1796 'F','R','O','M',' ','`','%','s','`',0};
1797
1798 table = msi_alloc_zero(sizeof(MERGETABLE));
1799 if (!table)
1800 {
1801 *ptable = NULL;
1802 return ERROR_OUTOFMEMORY;
1803 }
1804
1805 r = msi_get_table_labels(db, name, &table->labels, &table->numlabels);
1806 if (r != ERROR_SUCCESS)
1807 goto err;
1808
1809 r = MSI_OpenQuery(db, &mergeview, query, name);
1810 if (r != ERROR_SUCCESS)
1811 goto err;
1812
1813 r = msi_get_query_columns(mergeview, &table->columns, &table->numcolumns);
1814 if (r != ERROR_SUCCESS)
1815 goto err;
1816
1817 r = msi_get_query_types(mergeview, &table->types, &table->numtypes);
1818 if (r != ERROR_SUCCESS)
1819 goto err;
1820
1821 list_init(&table->rows);
1822
1823 table->name = strdupW(name);
1824 table->numconflicts = 0;
1825
1826 msiobj_release(&mergeview->hdr);
1827 *ptable = table;
1828 return ERROR_SUCCESS;
1829
1830 err:
1831 msiobj_release(&mergeview->hdr);
1832 free_merge_table(table);
1833 *ptable = NULL;
1834 return r;
1835 }
1836
1837 static UINT merge_diff_tables(MSIRECORD *rec, LPVOID param)
1838 {
1839 MERGEDATA *data = param;
1840 MERGETABLE *table;
1841 MSIQUERY *dbview = NULL;
1842 MSIQUERY *mergeview = NULL;
1843 LPCWSTR name;
1844 UINT r;
1845
1846 static const WCHAR query[] = {'S','E','L','E','C','T',' ','*',' ',
1847 'F','R','O','M',' ','`','%','s','`',0};
1848
1849 name = MSI_RecordGetString(rec, 1);
1850
1851 r = MSI_OpenQuery(data->merge, &mergeview, query, name);
1852 if (r != ERROR_SUCCESS)
1853 goto done;
1854
1855 if (TABLE_Exists(data->db, name))
1856 {
1857 r = MSI_OpenQuery(data->db, &dbview, query, name);
1858 if (r != ERROR_SUCCESS)
1859 goto done;
1860
1861 r = merge_verify_colnames(dbview, mergeview);
1862 if (r != ERROR_SUCCESS)
1863 goto done;
1864
1865 r = merge_verify_primary_keys(data->db, data->merge, name);
1866 if (r != ERROR_SUCCESS)
1867 goto done;
1868 }
1869
1870 r = msi_get_merge_table(data->merge, name, &table);
1871 if (r != ERROR_SUCCESS)
1872 goto done;
1873
1874 data->curtable = table;
1875 data->curview = mergeview;
1876 r = MSI_IterateRecords(mergeview, NULL, merge_diff_row, data);
1877 if (r != ERROR_SUCCESS)
1878 {
1879 free_merge_table(table);
1880 goto done;
1881 }
1882
1883 list_add_tail(data->tabledata, &table->entry);
1884
1885 done:
1886 msiobj_release(&dbview->hdr);
1887 msiobj_release(&mergeview->hdr);
1888 return r;
1889 }
1890
1891 static UINT gather_merge_data(MSIDATABASE *db, MSIDATABASE *merge,
1892 struct list *tabledata)
1893 {
1894 static const WCHAR query[] = {
1895 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1896 '`','_','T','a','b','l','e','s','`',0};
1897 MSIQUERY *view;
1898 MERGEDATA data;
1899 UINT r;
1900
1901 r = MSI_DatabaseOpenViewW(merge, query, &view);
1902 if (r != ERROR_SUCCESS)
1903 return r;
1904
1905 data.db = db;
1906 data.merge = merge;
1907 data.tabledata = tabledata;
1908 r = MSI_IterateRecords(view, NULL, merge_diff_tables, &data);
1909 msiobj_release(&view->hdr);
1910 return r;
1911 }
1912
1913 static UINT merge_table(MSIDATABASE *db, MERGETABLE *table)
1914 {
1915 UINT r;
1916 MERGEROW *row;
1917 MSIVIEW *tv;
1918
1919 if (!TABLE_Exists(db, table->name))
1920 {
1921 r = msi_add_table_to_db(db, table->columns, table->types,
1922 table->labels, table->numlabels, table->numcolumns);
1923 if (r != ERROR_SUCCESS)
1924 return ERROR_FUNCTION_FAILED;
1925 }
1926
1927 LIST_FOR_EACH_ENTRY(row, &table->rows, MERGEROW, entry)
1928 {
1929 r = TABLE_CreateView(db, table->name, &tv);
1930 if (r != ERROR_SUCCESS)
1931 return r;
1932
1933 r = tv->ops->insert_row(tv, row->data, -1, FALSE);
1934 tv->ops->delete(tv);
1935
1936 if (r != ERROR_SUCCESS)
1937 return r;
1938 }
1939
1940 return ERROR_SUCCESS;
1941 }
1942
1943 static UINT update_merge_errors(MSIDATABASE *db, LPCWSTR error,
1944 LPWSTR table, DWORD numconflicts)
1945 {
1946 UINT r;
1947 MSIQUERY *view;
1948
1949 static const WCHAR create[] = {
1950 'C','R','E','A','T','E',' ','T','A','B','L','E',' ',
1951 '`','%','s','`',' ','(','`','T','a','b','l','e','`',' ',
1952 'C','H','A','R','(','2','5','5',')',' ','N','O','T',' ',
1953 'N','U','L','L',',',' ','`','N','u','m','R','o','w','M','e','r','g','e',
1954 'C','o','n','f','l','i','c','t','s','`',' ','S','H','O','R','T',' ',
1955 'N','O','T',' ','N','U','L','L',' ','P','R','I','M','A','R','Y',' ',
1956 'K','E','Y',' ','`','T','a','b','l','e','`',')',0};
1957 static const WCHAR insert[] = {
1958 'I','N','S','E','R','T',' ','I','N','T','O',' ',
1959 '`','%','s','`',' ','(','`','T','a','b','l','e','`',',',' ',
1960 '`','N','u','m','R','o','w','M','e','r','g','e',
1961 'C','o','n','f','l','i','c','t','s','`',')',' ','V','A','L','U','E','S',
1962 ' ','(','\'','%','s','\'',',',' ','%','d',')',0};
1963
1964 if (!TABLE_Exists(db, error))
1965 {
1966 r = MSI_OpenQuery(db, &view, create, error);
1967 if (r != ERROR_SUCCESS)
1968 return r;
1969
1970 r = MSI_ViewExecute(view, NULL);
1971 msiobj_release(&view->hdr);
1972 if (r != ERROR_SUCCESS)
1973 return r;
1974 }
1975
1976 r = MSI_OpenQuery(db, &view, insert, error, table, numconflicts);
1977 if (r != ERROR_SUCCESS)
1978 return r;
1979
1980 r = MSI_ViewExecute(view, NULL);
1981 msiobj_release(&view->hdr);
1982 return r;
1983 }
1984
1985 UINT WINAPI MsiDatabaseMergeW(MSIHANDLE hDatabase, MSIHANDLE hDatabaseMerge,
1986 LPCWSTR szTableName)
1987 {
1988 struct list tabledata = LIST_INIT(tabledata);
1989 struct list *item, *cursor;
1990 MSIDATABASE *db, *merge;
1991 MERGETABLE *table;
1992 BOOL conflicts;
1993 UINT r;
1994
1995 TRACE("(%d, %d, %s)\n", hDatabase, hDatabaseMerge,
1996 debugstr_w(szTableName));
1997
1998 if (szTableName && !*szTableName)
1999 return ERROR_INVALID_TABLE;
2000
2001 db = msihandle2msiinfo(hDatabase, MSIHANDLETYPE_DATABASE);
2002 merge = msihandle2msiinfo(hDatabaseMerge, MSIHANDLETYPE_DATABASE);
2003 if (!db || !merge)
2004 {
2005 r = ERROR_INVALID_HANDLE;
2006 goto done;
2007 }
2008
2009 r = gather_merge_data(db, merge, &tabledata);
2010 if (r != ERROR_SUCCESS)
2011 goto done;
2012
2013 conflicts = FALSE;
2014 LIST_FOR_EACH_ENTRY(table, &tabledata, MERGETABLE, entry)
2015 {
2016 if (table->numconflicts)
2017 {
2018 conflicts = TRUE;
2019
2020 r = update_merge_errors(db, szTableName, table->name,
2021 table->numconflicts);
2022 if (r != ERROR_SUCCESS)
2023 break;
2024 }
2025 else
2026 {
2027 r = merge_table(db, table);
2028 if (r != ERROR_SUCCESS)
2029 break;
2030 }
2031 }
2032
2033 LIST_FOR_EACH_SAFE(item, cursor, &tabledata)
2034 {
2035 MERGETABLE *table = LIST_ENTRY(item, MERGETABLE, entry);
2036 list_remove(&table->entry);
2037 free_merge_table(table);
2038 }
2039
2040 if (conflicts)
2041 r = ERROR_FUNCTION_FAILED;
2042
2043 done:
2044 msiobj_release(&db->hdr);
2045 msiobj_release(&merge->hdr);
2046 return r;
2047 }
2048
2049 MSIDBSTATE WINAPI MsiGetDatabaseState( MSIHANDLE handle )
2050 {
2051 MSIDBSTATE ret = MSIDBSTATE_READ;
2052 MSIDATABASE *db;
2053
2054 TRACE("%d\n", handle);
2055
2056 db = msihandle2msiinfo( handle, MSIHANDLETYPE_DATABASE );
2057 if( !db )
2058 {
2059 IWineMsiRemoteDatabase *remote_database;
2060
2061 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( handle );
2062 if ( !remote_database )
2063 return MSIDBSTATE_ERROR;
2064
2065 IWineMsiRemoteDatabase_Release( remote_database );
2066 WARN("MsiGetDatabaseState not allowed during a custom action!\n");
2067
2068 return MSIDBSTATE_READ;
2069 }
2070
2071 if (db->mode != MSIDBOPEN_READONLY )
2072 ret = MSIDBSTATE_WRITE;
2073 msiobj_release( &db->hdr );
2074
2075 return ret;
2076 }
2077
2078 typedef struct _msi_remote_database_impl {
2079 IWineMsiRemoteDatabase IWineMsiRemoteDatabase_iface;
2080 MSIHANDLE database;
2081 LONG refs;
2082 } msi_remote_database_impl;
2083
2084 static inline msi_remote_database_impl *impl_from_IWineMsiRemoteDatabase( IWineMsiRemoteDatabase *iface )
2085 {
2086 return CONTAINING_RECORD(iface, msi_remote_database_impl, IWineMsiRemoteDatabase_iface);
2087 }
2088
2089 static HRESULT WINAPI mrd_QueryInterface( IWineMsiRemoteDatabase *iface,
2090 REFIID riid,LPVOID *ppobj)
2091 {
2092 if( IsEqualCLSID( riid, &IID_IUnknown ) ||
2093 IsEqualCLSID( riid, &IID_IWineMsiRemoteDatabase ) )
2094 {
2095 IWineMsiRemoteDatabase_AddRef( iface );
2096 *ppobj = iface;
2097 return S_OK;
2098 }
2099
2100 return E_NOINTERFACE;
2101 }
2102
2103 static ULONG WINAPI mrd_AddRef( IWineMsiRemoteDatabase *iface )
2104 {
2105 msi_remote_database_impl* This = impl_from_IWineMsiRemoteDatabase( iface );
2106
2107 return InterlockedIncrement( &This->refs );
2108 }
2109
2110 static ULONG WINAPI mrd_Release( IWineMsiRemoteDatabase *iface )
2111 {
2112 msi_remote_database_impl* This = impl_from_IWineMsiRemoteDatabase( iface );
2113 ULONG r;
2114
2115 r = InterlockedDecrement( &This->refs );
2116 if (r == 0)
2117 {
2118 MsiCloseHandle( This->database );
2119 msi_free( This );
2120 }
2121 return r;
2122 }
2123
2124 static HRESULT WINAPI mrd_IsTablePersistent( IWineMsiRemoteDatabase *iface,
2125 LPCWSTR table, MSICONDITION *persistent )
2126 {
2127 msi_remote_database_impl *This = impl_from_IWineMsiRemoteDatabase( iface );
2128 *persistent = MsiDatabaseIsTablePersistentW(This->database, table);
2129 return S_OK;
2130 }
2131
2132 static HRESULT WINAPI mrd_GetPrimaryKeys( IWineMsiRemoteDatabase *iface,
2133 LPCWSTR table, MSIHANDLE *keys )
2134 {
2135 msi_remote_database_impl *This = impl_from_IWineMsiRemoteDatabase( iface );
2136 UINT r = MsiDatabaseGetPrimaryKeysW(This->database, table, keys);
2137 return HRESULT_FROM_WIN32(r);
2138 }
2139
2140 static HRESULT WINAPI mrd_GetSummaryInformation( IWineMsiRemoteDatabase *iface,
2141 UINT updatecount, MSIHANDLE *suminfo )
2142 {
2143 msi_remote_database_impl *This = impl_from_IWineMsiRemoteDatabase( iface );
2144 UINT r = MsiGetSummaryInformationW(This->database, NULL, updatecount, suminfo);
2145 return HRESULT_FROM_WIN32(r);
2146 }
2147
2148 static HRESULT WINAPI mrd_OpenView( IWineMsiRemoteDatabase *iface,
2149 LPCWSTR query, MSIHANDLE *view )
2150 {
2151 msi_remote_database_impl *This = impl_from_IWineMsiRemoteDatabase( iface );
2152 UINT r = MsiDatabaseOpenViewW(This->database, query, view);
2153 return HRESULT_FROM_WIN32(r);
2154 }
2155
2156 static HRESULT WINAPI mrd_SetMsiHandle( IWineMsiRemoteDatabase *iface, MSIHANDLE handle )
2157 {
2158 msi_remote_database_impl* This = impl_from_IWineMsiRemoteDatabase( iface );
2159 This->database = handle;
2160 return S_OK;
2161 }
2162
2163 static const IWineMsiRemoteDatabaseVtbl msi_remote_database_vtbl =
2164 {
2165 mrd_QueryInterface,
2166 mrd_AddRef,
2167 mrd_Release,
2168 mrd_IsTablePersistent,
2169 mrd_GetPrimaryKeys,
2170 mrd_GetSummaryInformation,
2171 mrd_OpenView,
2172 mrd_SetMsiHandle,
2173 };
2174
2175 HRESULT create_msi_remote_database( IUnknown *pOuter, LPVOID *ppObj )
2176 {
2177 msi_remote_database_impl *This;
2178
2179 This = msi_alloc( sizeof *This );
2180 if (!This)
2181 return E_OUTOFMEMORY;
2182
2183 This->IWineMsiRemoteDatabase_iface.lpVtbl = &msi_remote_database_vtbl;
2184 This->database = 0;
2185 This->refs = 1;
2186
2187 *ppObj = This;
2188
2189 return S_OK;
2190 }