2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2002,2003,2004,2005 Mike McCormack for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 WINE_DEFAULT_DEBUG_CHANNEL(msi
);
30 * An .msi file is a structured storage file.
31 * It contains a number of streams.
32 * A stream for each table in the database.
33 * Two streams for the string table in the database.
34 * Any binary data in a table is a reference to a stream.
37 #define IS_INTMSIDBOPEN(x) (((ULONG_PTR)(x) >> 16) == 0)
39 static void free_transforms( MSIDATABASE
*db
)
41 while( !list_empty( &db
->transforms
) )
43 MSITRANSFORM
*t
= LIST_ENTRY( list_head( &db
->transforms
), MSITRANSFORM
, entry
);
44 list_remove( &t
->entry
);
45 IStorage_Release( t
->stg
);
50 static void free_streams( MSIDATABASE
*db
)
53 for (i
= 0; i
< db
->num_streams
; i
++)
55 if (db
->streams
[i
].stream
) IStream_Release( db
->streams
[i
].stream
);
57 msi_free( db
->streams
);
60 void append_storage_to_db( MSIDATABASE
*db
, IStorage
*stg
)
64 t
= msi_alloc( sizeof *t
);
66 IStorage_AddRef( stg
);
67 list_add_head( &db
->transforms
, &t
->entry
);
70 static VOID
MSI_CloseDatabase( MSIOBJECTHDR
*arg
)
72 MSIDATABASE
*db
= (MSIDATABASE
*) arg
;
76 free_cached_tables( db
);
77 free_transforms( db
);
78 if (db
->strings
) msi_destroy_stringtable( db
->strings
);
79 IStorage_Release( db
->storage
);
82 DeleteFileW( db
->deletefile
);
83 msi_free( db
->deletefile
);
85 msi_free( db
->tempfolder
);
88 static HRESULT
db_initialize( IStorage
*stg
, const GUID
*clsid
)
90 static const WCHAR szTables
[] = { '_','T','a','b','l','e','s',0 };
93 hr
= IStorage_SetClass( stg
, clsid
);
96 WARN("failed to set class id 0x%08x\n", hr
);
100 /* create the _Tables stream */
101 hr
= write_stream_data( stg
, szTables
, NULL
, 0, TRUE
);
104 WARN("failed to create _Tables stream 0x%08x\n", hr
);
108 hr
= msi_init_string_table( stg
);
111 WARN("failed to initialize string table 0x%08x\n", hr
);
115 hr
= IStorage_Commit( stg
, 0 );
118 WARN("failed to commit changes 0x%08x\n", hr
);
125 UINT
MSI_OpenDatabaseW(LPCWSTR szDBPath
, LPCWSTR szPersist
, MSIDATABASE
**pdb
)
127 IStorage
*stg
= NULL
;
129 MSIDATABASE
*db
= NULL
;
130 UINT ret
= ERROR_FUNCTION_FAILED
;
131 LPCWSTR szMode
, save_path
;
133 BOOL created
= FALSE
, patch
= FALSE
;
134 WCHAR path
[MAX_PATH
];
136 TRACE("%s %s\n",debugstr_w(szDBPath
),debugstr_w(szPersist
) );
139 return ERROR_INVALID_PARAMETER
;
141 if (szPersist
- MSIDBOPEN_PATCHFILE
<= MSIDBOPEN_CREATEDIRECT
)
143 TRACE("Database is a patch\n");
144 szPersist
-= MSIDBOPEN_PATCHFILE
;
148 save_path
= szDBPath
;
150 if( !IS_INTMSIDBOPEN(szPersist
) )
152 if (!CopyFileW( szDBPath
, szPersist
, FALSE
))
153 return ERROR_OPEN_FAILED
;
155 szDBPath
= szPersist
;
156 szPersist
= MSIDBOPEN_TRANSACT
;
160 if( szPersist
== MSIDBOPEN_READONLY
)
162 r
= StgOpenStorage( szDBPath
, NULL
,
163 STGM_DIRECT
|STGM_READ
|STGM_SHARE_DENY_WRITE
, NULL
, 0, &stg
);
165 else if( szPersist
== MSIDBOPEN_CREATE
)
167 r
= StgCreateDocfile( szDBPath
,
168 STGM_CREATE
|STGM_TRANSACTED
|STGM_READWRITE
|STGM_SHARE_EXCLUSIVE
, 0, &stg
);
171 r
= db_initialize( stg
, patch
? &CLSID_MsiPatch
: &CLSID_MsiDatabase
);
174 else if( szPersist
== MSIDBOPEN_CREATEDIRECT
)
176 r
= StgCreateDocfile( szDBPath
,
177 STGM_CREATE
|STGM_DIRECT
|STGM_READWRITE
|STGM_SHARE_EXCLUSIVE
, 0, &stg
);
180 r
= db_initialize( stg
, patch
? &CLSID_MsiPatch
: &CLSID_MsiDatabase
);
183 else if( szPersist
== MSIDBOPEN_TRANSACT
)
185 r
= StgOpenStorage( szDBPath
, NULL
,
186 STGM_TRANSACTED
|STGM_READWRITE
|STGM_SHARE_DENY_WRITE
, NULL
, 0, &stg
);
188 else if( szPersist
== MSIDBOPEN_DIRECT
)
190 r
= StgOpenStorage( szDBPath
, NULL
,
191 STGM_DIRECT
|STGM_READWRITE
|STGM_SHARE_EXCLUSIVE
, NULL
, 0, &stg
);
195 ERR("unknown flag %p\n",szPersist
);
196 return ERROR_INVALID_PARAMETER
;
199 if( FAILED( r
) || !stg
)
201 WARN("open failed r = %08x for %s\n", r
, debugstr_w(szDBPath
));
202 return ERROR_FUNCTION_FAILED
;
205 r
= IStorage_Stat( stg
, &stat
, STATFLAG_NONAME
);
208 FIXME("Failed to stat storage\n");
212 if ( !IsEqualGUID( &stat
.clsid
, &CLSID_MsiDatabase
) &&
213 !IsEqualGUID( &stat
.clsid
, &CLSID_MsiPatch
) &&
214 !IsEqualGUID( &stat
.clsid
, &CLSID_MsiTransform
) )
216 ERR("storage GUID is not a MSI database GUID %s\n",
217 debugstr_guid(&stat
.clsid
) );
221 if ( patch
&& !IsEqualGUID( &stat
.clsid
, &CLSID_MsiPatch
) )
223 ERR("storage GUID is not the MSI patch GUID %s\n",
224 debugstr_guid(&stat
.clsid
) );
225 ret
= ERROR_OPEN_FAILED
;
229 db
= alloc_msiobject( MSIHANDLETYPE_DATABASE
, sizeof (MSIDATABASE
),
233 FIXME("Failed to allocate a handle\n");
237 if (!strchrW( save_path
, '\\' ))
239 GetCurrentDirectoryW( MAX_PATH
, path
);
240 lstrcatW( path
, szBackSlash
);
241 lstrcatW( path
, save_path
);
244 lstrcpyW( path
, save_path
);
246 db
->path
= strdupW( path
);
247 db
->media_transform_offset
= MSI_INITIAL_MEDIA_TRANSFORM_OFFSET
;
248 db
->media_transform_disk_id
= MSI_INITIAL_MEDIA_TRANSFORM_DISKID
;
250 if( TRACE_ON( msi
) )
251 enum_stream_names( stg
);
256 db
->deletefile
= strdupW( szDBPath
);
257 list_init( &db
->tables
);
258 list_init( &db
->transforms
);
260 db
->strings
= msi_load_string_table( stg
, &db
->bytes_per_strref
);
266 msiobj_addref( &db
->hdr
);
267 IStorage_AddRef( stg
);
272 msiobj_release( &db
->hdr
);
274 IStorage_Release( stg
);
279 UINT WINAPI
MsiOpenDatabaseW(LPCWSTR szDBPath
, LPCWSTR szPersist
, MSIHANDLE
*phDB
)
284 TRACE("%s %s %p\n",debugstr_w(szDBPath
),debugstr_w(szPersist
), phDB
);
286 ret
= MSI_OpenDatabaseW( szDBPath
, szPersist
, &db
);
287 if( ret
== ERROR_SUCCESS
)
289 *phDB
= alloc_msihandle( &db
->hdr
);
291 ret
= ERROR_NOT_ENOUGH_MEMORY
;
292 msiobj_release( &db
->hdr
);
298 UINT WINAPI
MsiOpenDatabaseA(LPCSTR szDBPath
, LPCSTR szPersist
, MSIHANDLE
*phDB
)
300 HRESULT r
= ERROR_FUNCTION_FAILED
;
301 LPWSTR szwDBPath
= NULL
, szwPersist
= NULL
;
303 TRACE("%s %s %p\n", debugstr_a(szDBPath
), debugstr_a(szPersist
), phDB
);
307 szwDBPath
= strdupAtoW( szDBPath
);
312 if( !IS_INTMSIDBOPEN(szPersist
) )
314 szwPersist
= strdupAtoW( szPersist
);
319 szwPersist
= (LPWSTR
)(DWORD_PTR
)szPersist
;
321 r
= MsiOpenDatabaseW( szwDBPath
, szwPersist
, phDB
);
324 if( !IS_INTMSIDBOPEN(szPersist
) )
325 msi_free( szwPersist
);
326 msi_free( szwDBPath
);
331 static LPWSTR
msi_read_text_archive(LPCWSTR path
, DWORD
*len
)
336 DWORD read
, size
= 0;
338 file
= CreateFileW( path
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, NULL
);
339 if (file
== INVALID_HANDLE_VALUE
)
342 size
= GetFileSize( file
, NULL
);
343 if (!(data
= msi_alloc( size
))) goto done
;
345 if (!ReadFile( file
, data
, size
, &read
, NULL
) || read
!= size
) goto done
;
347 while (!data
[size
- 1]) size
--;
348 *len
= MultiByteToWideChar( CP_ACP
, 0, data
, size
, NULL
, 0 );
349 if ((wdata
= msi_alloc( (*len
+ 1) * sizeof(WCHAR
) )))
351 MultiByteToWideChar( CP_ACP
, 0, data
, size
, wdata
, *len
);
361 static void msi_parse_line(LPWSTR
*line
, LPWSTR
**entries
, DWORD
*num_entries
, DWORD
*len
)
363 LPWSTR ptr
= *line
, save
;
364 DWORD i
, count
= 1, chars_left
= *len
;
368 /* stay on this line */
369 while (chars_left
&& *ptr
!= '\n')
371 /* entries are separated by tabs */
379 *entries
= msi_alloc(count
* sizeof(LPWSTR
));
383 /* store pointers into the data */
385 for (i
= 0, ptr
= *line
; i
< count
; i
++)
387 while (chars_left
&& *ptr
== '\r')
394 while (chars_left
&& *ptr
!= '\t' && *ptr
!= '\n' && *ptr
!= '\r')
396 if (!*ptr
) *ptr
= '\n'; /* convert embedded nulls to \n */
397 if (ptr
> *line
&& *ptr
== '\x19' && *(ptr
- 1) == '\x11')
406 /* NULL-separate the data */
407 if (*ptr
== '\n' || *ptr
== '\r')
409 while (chars_left
&& (*ptr
== '\n' || *ptr
== '\r'))
420 (*entries
)[i
] = save
;
423 /* move to the next line if there's more, else EOF */
427 *num_entries
= count
;
430 static LPWSTR
msi_build_createsql_prelude(LPWSTR table
)
435 static const WCHAR create_fmt
[] = {'C','R','E','A','T','E',' ','T','A','B','L','E',' ','`','%','s','`',' ','(',' ',0};
437 size
= sizeof(create_fmt
)/sizeof(create_fmt
[0]) + lstrlenW(table
) - 2;
438 prelude
= msi_alloc(size
* sizeof(WCHAR
));
442 sprintfW(prelude
, create_fmt
, table
);
446 static LPWSTR
msi_build_createsql_columns(LPWSTR
*columns_data
, LPWSTR
*types
, DWORD num_columns
)
450 DWORD sql_size
= 1, i
, len
;
451 WCHAR expanded
[128], *ptr
;
452 WCHAR size
[10], comma
[2], extra
[30];
454 static const WCHAR column_fmt
[] = {'`','%','s','`',' ','%','s','%','s','%','s','%','s',' ',0};
455 static const WCHAR size_fmt
[] = {'(','%','s',')',0};
456 static const WCHAR type_char
[] = {'C','H','A','R',0};
457 static const WCHAR type_int
[] = {'I','N','T',0};
458 static const WCHAR type_long
[] = {'L','O','N','G',0};
459 static const WCHAR type_object
[] = {'O','B','J','E','C','T',0};
460 static const WCHAR type_notnull
[] = {' ','N','O','T',' ','N','U','L','L',0};
461 static const WCHAR localizable
[] = {' ','L','O','C','A','L','I','Z','A','B','L','E',0};
463 columns
= msi_alloc_zero(sql_size
* sizeof(WCHAR
));
467 for (i
= 0; i
< num_columns
; i
++)
470 comma
[1] = size
[0] = extra
[0] = '\0';
472 if (i
== num_columns
- 1)
484 lstrcpyW(extra
, type_notnull
);
487 lstrcatW(extra
, localizable
);
489 sprintfW(size
, size_fmt
, ptr
);
492 lstrcpyW(extra
, type_notnull
);
496 sprintfW(size
, size_fmt
, ptr
);
499 lstrcpyW(extra
, type_notnull
);
508 WARN("invalid int width %u\n", len
);
514 lstrcpyW(extra
, type_notnull
);
520 ERR("Unknown type: %c\n", types
[i
][0]);
525 sprintfW(expanded
, column_fmt
, columns_data
[i
], type
, size
, extra
, comma
);
526 sql_size
+= lstrlenW(expanded
);
528 p
= msi_realloc(columns
, sql_size
* sizeof(WCHAR
));
536 lstrcatW(columns
, expanded
);
542 static LPWSTR
msi_build_createsql_postlude(LPWSTR
*primary_keys
, DWORD num_keys
)
544 LPWSTR postlude
, keys
, ptr
;
545 DWORD size
, key_size
, i
;
547 static const WCHAR key_fmt
[] = {'`','%','s','`',',',' ',0};
548 static const WCHAR postlude_fmt
[] = {'P','R','I','M','A','R','Y',' ','K','E','Y',' ','%','s',')',0};
550 for (i
= 0, size
= 1; i
< num_keys
; i
++)
551 size
+= lstrlenW(key_fmt
) + lstrlenW(primary_keys
[i
]) - 2;
553 keys
= msi_alloc(size
* sizeof(WCHAR
));
557 for (i
= 0, ptr
= keys
; i
< num_keys
; i
++)
559 key_size
= lstrlenW(key_fmt
) + lstrlenW(primary_keys
[i
]) -2;
560 sprintfW(ptr
, key_fmt
, primary_keys
[i
]);
564 /* remove final ', ' */
567 size
= lstrlenW(postlude_fmt
) + size
- 1;
568 postlude
= msi_alloc(size
* sizeof(WCHAR
));
572 sprintfW(postlude
, postlude_fmt
, keys
);
579 static UINT
msi_add_table_to_db(MSIDATABASE
*db
, LPWSTR
*columns
, LPWSTR
*types
, LPWSTR
*labels
, DWORD num_labels
, DWORD num_columns
)
581 UINT r
= ERROR_OUTOFMEMORY
;
584 LPWSTR create_sql
= NULL
;
585 LPWSTR prelude
, columns_sql
, postlude
;
587 prelude
= msi_build_createsql_prelude(labels
[0]);
588 columns_sql
= msi_build_createsql_columns(columns
, types
, num_columns
);
589 postlude
= msi_build_createsql_postlude(labels
+ 1, num_labels
- 1); /* skip over table name */
591 if (!prelude
|| !columns_sql
|| !postlude
)
594 size
= lstrlenW(prelude
) + lstrlenW(columns_sql
) + lstrlenW(postlude
) + 1;
595 create_sql
= msi_alloc(size
* sizeof(WCHAR
));
599 lstrcpyW(create_sql
, prelude
);
600 lstrcatW(create_sql
, columns_sql
);
601 lstrcatW(create_sql
, postlude
);
603 r
= MSI_DatabaseOpenViewW( db
, create_sql
, &view
);
604 if (r
!= ERROR_SUCCESS
)
607 r
= MSI_ViewExecute(view
, NULL
);
609 msiobj_release(&view
->hdr
);
613 msi_free(columns_sql
);
615 msi_free(create_sql
);
619 static LPWSTR
msi_import_stream_filename(LPCWSTR path
, LPCWSTR name
)
622 LPWSTR fullname
, ptr
;
624 len
= lstrlenW(path
) + lstrlenW(name
) + 1;
625 fullname
= msi_alloc(len
*sizeof(WCHAR
));
629 lstrcpyW( fullname
, path
);
631 /* chop off extension from path */
632 ptr
= strrchrW(fullname
, '.');
639 lstrcpyW( ptr
, name
);
643 static UINT
construct_record(DWORD num_columns
, LPWSTR
*types
,
644 LPWSTR
*data
, LPWSTR path
, MSIRECORD
**rec
)
648 *rec
= MSI_CreateRecord(num_columns
);
650 return ERROR_OUTOFMEMORY
;
652 for (i
= 0; i
< num_columns
; i
++)
656 case 'L': case 'l': case 'S': case 's':
657 MSI_RecordSetStringW(*rec
, i
+ 1, data
[i
]);
661 MSI_RecordSetInteger(*rec
, i
+ 1, atoiW(data
[i
]));
667 LPWSTR file
= msi_import_stream_filename(path
, data
[i
]);
669 return ERROR_FUNCTION_FAILED
;
671 r
= MSI_RecordSetStreamFromFileW(*rec
, i
+ 1, file
);
673 if (r
!= ERROR_SUCCESS
)
674 return ERROR_FUNCTION_FAILED
;
678 ERR("Unhandled column type: %c\n", types
[i
][0]);
679 msiobj_release(&(*rec
)->hdr
);
680 return ERROR_FUNCTION_FAILED
;
684 return ERROR_SUCCESS
;
687 static UINT
msi_add_records_to_table(MSIDATABASE
*db
, LPWSTR
*columns
, LPWSTR
*types
,
688 LPWSTR
*labels
, LPWSTR
**records
,
689 int num_columns
, int num_records
,
697 static const WCHAR select
[] = {
698 'S','E','L','E','C','T',' ','*',' ',
699 'F','R','O','M',' ','`','%','s','`',0
702 r
= MSI_OpenQuery(db
, &view
, select
, labels
[0]);
703 if (r
!= ERROR_SUCCESS
)
706 while (MSI_ViewFetch(view
, &rec
) != ERROR_NO_MORE_ITEMS
)
708 r
= MSI_ViewModify(view
, MSIMODIFY_DELETE
, rec
);
709 msiobj_release(&rec
->hdr
);
710 if (r
!= ERROR_SUCCESS
)
714 for (i
= 0; i
< num_records
; i
++)
716 r
= construct_record(num_columns
, types
, records
[i
], path
, &rec
);
717 if (r
!= ERROR_SUCCESS
)
720 r
= MSI_ViewModify(view
, MSIMODIFY_INSERT
, rec
);
721 if (r
!= ERROR_SUCCESS
)
723 msiobj_release(&rec
->hdr
);
727 msiobj_release(&rec
->hdr
);
731 msiobj_release(&view
->hdr
);
735 static UINT
MSI_DatabaseImport(MSIDATABASE
*db
, LPCWSTR folder
, LPCWSTR file
)
739 DWORD num_labels
, num_types
;
740 DWORD num_columns
, num_records
= 0;
741 LPWSTR
*columns
, *types
, *labels
;
742 LPWSTR path
, ptr
, data
;
743 LPWSTR
**records
= NULL
;
744 LPWSTR
**temp_records
;
746 static const WCHAR suminfo
[] =
747 {'_','S','u','m','m','a','r','y','I','n','f','o','r','m','a','t','i','o','n',0};
748 static const WCHAR forcecodepage
[] =
749 {'_','F','o','r','c','e','C','o','d','e','p','a','g','e',0};
751 TRACE("%p %s %s\n", db
, debugstr_w(folder
), debugstr_w(file
) );
753 if( folder
== NULL
|| file
== NULL
)
754 return ERROR_INVALID_PARAMETER
;
756 len
= lstrlenW(folder
) + lstrlenW(szBackSlash
) + lstrlenW(file
) + 1;
757 path
= msi_alloc( len
* sizeof(WCHAR
) );
759 return ERROR_OUTOFMEMORY
;
761 lstrcpyW( path
, folder
);
762 lstrcatW( path
, szBackSlash
);
763 lstrcatW( path
, file
);
765 data
= msi_read_text_archive( path
, &len
);
768 msi_parse_line( &ptr
, &columns
, &num_columns
, &len
);
769 msi_parse_line( &ptr
, &types
, &num_types
, &len
);
770 msi_parse_line( &ptr
, &labels
, &num_labels
, &len
);
772 if (num_columns
== 1 && !columns
[0][0] && num_labels
== 1 && !labels
[0][0] &&
773 num_types
== 2 && !strcmpW( types
[1], forcecodepage
))
775 r
= msi_set_string_table_codepage( db
->strings
, atoiW( types
[0] ) );
779 if (num_columns
!= num_types
)
781 r
= ERROR_FUNCTION_FAILED
;
785 records
= msi_alloc(sizeof(LPWSTR
*));
788 r
= ERROR_OUTOFMEMORY
;
792 /* read in the table records */
795 msi_parse_line( &ptr
, &records
[num_records
], NULL
, &len
);
798 temp_records
= msi_realloc(records
, (num_records
+ 1) * sizeof(LPWSTR
*));
801 r
= ERROR_OUTOFMEMORY
;
804 records
= temp_records
;
807 if (!strcmpW(labels
[0], suminfo
))
809 r
= msi_add_suminfo( db
, records
, num_records
, num_columns
);
810 if (r
!= ERROR_SUCCESS
)
812 r
= ERROR_FUNCTION_FAILED
;
818 if (!TABLE_Exists(db
, labels
[0]))
820 r
= msi_add_table_to_db( db
, columns
, types
, labels
, num_labels
, num_columns
);
821 if (r
!= ERROR_SUCCESS
)
823 r
= ERROR_FUNCTION_FAILED
;
828 r
= msi_add_records_to_table( db
, columns
, types
, labels
, records
, num_columns
, num_records
, path
);
838 for (i
= 0; i
< num_records
; i
++)
839 msi_free(records
[i
]);
846 UINT WINAPI
MsiDatabaseImportW(MSIHANDLE handle
, LPCWSTR szFolder
, LPCWSTR szFilename
)
851 TRACE("%x %s %s\n",handle
,debugstr_w(szFolder
), debugstr_w(szFilename
));
853 db
= msihandle2msiinfo( handle
, MSIHANDLETYPE_DATABASE
);
856 IWineMsiRemoteDatabase
*remote_database
;
858 remote_database
= (IWineMsiRemoteDatabase
*)msi_get_remote( handle
);
859 if ( !remote_database
)
860 return ERROR_INVALID_HANDLE
;
862 IWineMsiRemoteDatabase_Release( remote_database
);
863 WARN("MsiDatabaseImport not allowed during a custom action!\n");
865 return ERROR_SUCCESS
;
868 r
= MSI_DatabaseImport( db
, szFolder
, szFilename
);
869 msiobj_release( &db
->hdr
);
873 UINT WINAPI
MsiDatabaseImportA( MSIHANDLE handle
,
874 LPCSTR szFolder
, LPCSTR szFilename
)
876 LPWSTR path
= NULL
, file
= NULL
;
877 UINT r
= ERROR_OUTOFMEMORY
;
879 TRACE("%x %s %s\n", handle
, debugstr_a(szFolder
), debugstr_a(szFilename
));
883 path
= strdupAtoW( szFolder
);
890 file
= strdupAtoW( szFilename
);
895 r
= MsiDatabaseImportW( handle
, path
, file
);
904 static UINT
msi_export_record( HANDLE handle
, MSIRECORD
*row
, UINT start
)
906 UINT i
, count
, len
, r
= ERROR_SUCCESS
;
912 buffer
= msi_alloc( len
);
914 return ERROR_OUTOFMEMORY
;
916 count
= MSI_RecordGetFieldCount( row
);
917 for ( i
=start
; i
<=count
; i
++ )
920 r
= MSI_RecordGetStringA( row
, i
, buffer
, &sz
);
921 if (r
== ERROR_MORE_DATA
)
923 char *p
= msi_realloc( buffer
, sz
+ 1 );
930 r
= MSI_RecordGetStringA( row
, i
, buffer
, &sz
);
931 if (r
!= ERROR_SUCCESS
)
934 if (!WriteFile( handle
, buffer
, sz
, &sz
, NULL
))
936 r
= ERROR_FUNCTION_FAILED
;
940 sep
= (i
< count
) ? "\t" : "\r\n";
941 if (!WriteFile( handle
, sep
, strlen(sep
), &sz
, NULL
))
943 r
= ERROR_FUNCTION_FAILED
;
951 static UINT
msi_export_row( MSIRECORD
*row
, void *arg
)
953 return msi_export_record( arg
, row
, 1 );
956 static UINT
msi_export_forcecodepage( HANDLE handle
, UINT codepage
)
958 static const char fmt
[] = "\r\n\r\n%u\t_ForceCodepage\r\n";
959 char data
[sizeof(fmt
) + 10];
962 sprintf( data
, fmt
, codepage
);
964 sz
= lstrlenA(data
) + 1;
965 if (!WriteFile(handle
, data
, sz
, &sz
, NULL
))
966 return ERROR_FUNCTION_FAILED
;
968 return ERROR_SUCCESS
;
971 static UINT
MSI_DatabaseExport( MSIDATABASE
*db
, LPCWSTR table
,
972 LPCWSTR folder
, LPCWSTR file
)
974 static const WCHAR query
[] = {
975 's','e','l','e','c','t',' ','*',' ','f','r','o','m',' ','%','s',0 };
976 static const WCHAR forcecodepage
[] = {
977 '_','F','o','r','c','e','C','o','d','e','p','a','g','e',0 };
978 MSIRECORD
*rec
= NULL
;
979 MSIQUERY
*view
= NULL
;
984 TRACE("%p %s %s %s\n", db
, debugstr_w(table
),
985 debugstr_w(folder
), debugstr_w(file
) );
987 if( folder
== NULL
|| file
== NULL
)
988 return ERROR_INVALID_PARAMETER
;
990 len
= lstrlenW(folder
) + lstrlenW(file
) + 2;
991 filename
= msi_alloc(len
* sizeof (WCHAR
));
993 return ERROR_OUTOFMEMORY
;
995 lstrcpyW( filename
, folder
);
996 lstrcatW( filename
, szBackSlash
);
997 lstrcatW( filename
, file
);
999 handle
= CreateFileW( filename
, GENERIC_READ
| GENERIC_WRITE
, 0,
1000 NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1001 msi_free( filename
);
1002 if (handle
== INVALID_HANDLE_VALUE
)
1003 return ERROR_FUNCTION_FAILED
;
1005 if (!strcmpW( table
, forcecodepage
))
1007 UINT codepage
= msi_get_string_table_codepage( db
->strings
);
1008 r
= msi_export_forcecodepage( handle
, codepage
);
1012 r
= MSI_OpenQuery( db
, &view
, query
, table
);
1013 if (r
== ERROR_SUCCESS
)
1015 /* write out row 1, the column names */
1016 r
= MSI_ViewGetColumnInfo(view
, MSICOLINFO_NAMES
, &rec
);
1017 if (r
== ERROR_SUCCESS
)
1019 msi_export_record( handle
, rec
, 1 );
1020 msiobj_release( &rec
->hdr
);
1023 /* write out row 2, the column types */
1024 r
= MSI_ViewGetColumnInfo(view
, MSICOLINFO_TYPES
, &rec
);
1025 if (r
== ERROR_SUCCESS
)
1027 msi_export_record( handle
, rec
, 1 );
1028 msiobj_release( &rec
->hdr
);
1031 /* write out row 3, the table name + keys */
1032 r
= MSI_DatabaseGetPrimaryKeys( db
, table
, &rec
);
1033 if (r
== ERROR_SUCCESS
)
1035 MSI_RecordSetStringW( rec
, 0, table
);
1036 msi_export_record( handle
, rec
, 0 );
1037 msiobj_release( &rec
->hdr
);
1040 /* write out row 4 onwards, the data */
1041 r
= MSI_IterateRecords( view
, 0, msi_export_row
, handle
);
1042 msiobj_release( &view
->hdr
);
1046 CloseHandle( handle
);
1050 /***********************************************************************
1051 * MsiExportDatabaseW [MSI.@]
1053 * Writes a file containing the table data as tab separated ASCII.
1055 * The format is as follows:
1057 * row1 : colname1 <tab> colname2 <tab> .... colnameN <cr> <lf>
1058 * row2 : coltype1 <tab> coltype2 <tab> .... coltypeN <cr> <lf>
1059 * row3 : tablename <tab> key1 <tab> key2 <tab> ... keyM <cr> <lf>
1061 * Followed by the data, starting at row 1 with one row per line
1063 * row4 : data <tab> data <tab> data <tab> ... data <cr> <lf>
1065 UINT WINAPI
MsiDatabaseExportW( MSIHANDLE handle
, LPCWSTR szTable
,
1066 LPCWSTR szFolder
, LPCWSTR szFilename
)
1071 TRACE("%x %s %s %s\n", handle
, debugstr_w(szTable
),
1072 debugstr_w(szFolder
), debugstr_w(szFilename
));
1074 db
= msihandle2msiinfo( handle
, MSIHANDLETYPE_DATABASE
);
1077 IWineMsiRemoteDatabase
*remote_database
;
1079 remote_database
= (IWineMsiRemoteDatabase
*)msi_get_remote( handle
);
1080 if ( !remote_database
)
1081 return ERROR_INVALID_HANDLE
;
1083 IWineMsiRemoteDatabase_Release( remote_database
);
1084 WARN("MsiDatabaseExport not allowed during a custom action!\n");
1086 return ERROR_SUCCESS
;
1089 r
= MSI_DatabaseExport( db
, szTable
, szFolder
, szFilename
);
1090 msiobj_release( &db
->hdr
);
1094 UINT WINAPI
MsiDatabaseExportA( MSIHANDLE handle
, LPCSTR szTable
,
1095 LPCSTR szFolder
, LPCSTR szFilename
)
1097 LPWSTR path
= NULL
, file
= NULL
, table
= NULL
;
1098 UINT r
= ERROR_OUTOFMEMORY
;
1100 TRACE("%x %s %s %s\n", handle
, debugstr_a(szTable
),
1101 debugstr_a(szFolder
), debugstr_a(szFilename
));
1105 table
= strdupAtoW( szTable
);
1112 path
= strdupAtoW( szFolder
);
1119 file
= strdupAtoW( szFilename
);
1124 r
= MsiDatabaseExportW( handle
, table
, path
, file
);
1134 UINT WINAPI
MsiDatabaseMergeA(MSIHANDLE hDatabase
, MSIHANDLE hDatabaseMerge
,
1140 TRACE("(%d, %d, %s)\n", hDatabase
, hDatabaseMerge
,
1141 debugstr_a(szTableName
));
1143 table
= strdupAtoW(szTableName
);
1144 r
= MsiDatabaseMergeW(hDatabase
, hDatabaseMerge
, table
);
1150 typedef struct _tagMERGETABLE
1164 typedef struct _tagMERGEROW
1170 typedef struct _tagMERGEDATA
1174 MERGETABLE
*curtable
;
1176 struct list
*tabledata
;
1179 static BOOL
merge_type_match(LPCWSTR type1
, LPCWSTR type2
)
1181 if (((type1
[0] == 'l') || (type1
[0] == 's')) &&
1182 ((type2
[0] == 'l') || (type2
[0] == 's')))
1185 if (((type1
[0] == 'L') || (type1
[0] == 'S')) &&
1186 ((type2
[0] == 'L') || (type2
[0] == 'S')))
1189 return !strcmpW( type1
, type2
);
1192 static UINT
merge_verify_colnames(MSIQUERY
*dbview
, MSIQUERY
*mergeview
)
1194 MSIRECORD
*dbrec
, *mergerec
;
1197 r
= MSI_ViewGetColumnInfo(dbview
, MSICOLINFO_NAMES
, &dbrec
);
1198 if (r
!= ERROR_SUCCESS
)
1201 r
= MSI_ViewGetColumnInfo(mergeview
, MSICOLINFO_NAMES
, &mergerec
);
1202 if (r
!= ERROR_SUCCESS
)
1204 msiobj_release(&dbrec
->hdr
);
1208 count
= MSI_RecordGetFieldCount(dbrec
);
1209 for (i
= 1; i
<= count
; i
++)
1211 if (!MSI_RecordGetString(mergerec
, i
))
1214 if (strcmpW( MSI_RecordGetString( dbrec
, i
), MSI_RecordGetString( mergerec
, i
) ))
1216 r
= ERROR_DATATYPE_MISMATCH
;
1221 msiobj_release(&dbrec
->hdr
);
1222 msiobj_release(&mergerec
->hdr
);
1223 dbrec
= mergerec
= NULL
;
1225 r
= MSI_ViewGetColumnInfo(dbview
, MSICOLINFO_TYPES
, &dbrec
);
1226 if (r
!= ERROR_SUCCESS
)
1229 r
= MSI_ViewGetColumnInfo(mergeview
, MSICOLINFO_TYPES
, &mergerec
);
1230 if (r
!= ERROR_SUCCESS
)
1232 msiobj_release(&dbrec
->hdr
);
1236 count
= MSI_RecordGetFieldCount(dbrec
);
1237 for (i
= 1; i
<= count
; i
++)
1239 if (!MSI_RecordGetString(mergerec
, i
))
1242 if (!merge_type_match(MSI_RecordGetString(dbrec
, i
),
1243 MSI_RecordGetString(mergerec
, i
)))
1245 r
= ERROR_DATATYPE_MISMATCH
;
1251 msiobj_release(&dbrec
->hdr
);
1252 msiobj_release(&mergerec
->hdr
);
1257 static UINT
merge_verify_primary_keys(MSIDATABASE
*db
, MSIDATABASE
*mergedb
,
1260 MSIRECORD
*dbrec
, *mergerec
= NULL
;
1263 r
= MSI_DatabaseGetPrimaryKeys(db
, table
, &dbrec
);
1264 if (r
!= ERROR_SUCCESS
)
1267 r
= MSI_DatabaseGetPrimaryKeys(mergedb
, table
, &mergerec
);
1268 if (r
!= ERROR_SUCCESS
)
1271 count
= MSI_RecordGetFieldCount(dbrec
);
1272 if (count
!= MSI_RecordGetFieldCount(mergerec
))
1274 r
= ERROR_DATATYPE_MISMATCH
;
1278 for (i
= 1; i
<= count
; i
++)
1280 if (strcmpW( MSI_RecordGetString( dbrec
, i
), MSI_RecordGetString( mergerec
, i
) ))
1282 r
= ERROR_DATATYPE_MISMATCH
;
1288 msiobj_release(&dbrec
->hdr
);
1289 msiobj_release(&mergerec
->hdr
);
1294 static LPWSTR
get_key_value(MSIQUERY
*view
, LPCWSTR key
, MSIRECORD
*rec
)
1296 MSIRECORD
*colnames
;
1298 UINT r
, i
= 0, sz
= 0;
1301 r
= MSI_ViewGetColumnInfo(view
, MSICOLINFO_NAMES
, &colnames
);
1302 if (r
!= ERROR_SUCCESS
)
1307 str
= msi_dup_record_field(colnames
, ++i
);
1308 cmp
= strcmpW( key
, str
);
1312 msiobj_release(&colnames
->hdr
);
1314 r
= MSI_RecordGetStringW(rec
, i
, NULL
, &sz
);
1315 if (r
!= ERROR_SUCCESS
)
1319 if (MSI_RecordGetString(rec
, i
)) /* check record field is a string */
1321 /* quote string record fields */
1322 const WCHAR szQuote
[] = {'\'', 0};
1324 val
= msi_alloc(sz
*sizeof(WCHAR
));
1328 lstrcpyW(val
, szQuote
);
1329 r
= MSI_RecordGetStringW(rec
, i
, val
+1, &sz
);
1330 lstrcpyW(val
+1+sz
, szQuote
);
1334 /* do not quote integer record fields */
1335 val
= msi_alloc(sz
*sizeof(WCHAR
));
1339 r
= MSI_RecordGetStringW(rec
, i
, val
, &sz
);
1342 if (r
!= ERROR_SUCCESS
)
1344 ERR("failed to get string!\n");
1352 static LPWSTR
create_diff_row_query(MSIDATABASE
*merge
, MSIQUERY
*view
,
1353 LPWSTR table
, MSIRECORD
*rec
)
1355 LPWSTR query
= NULL
, clause
= NULL
, val
;
1356 LPCWSTR setptr
, key
;
1357 DWORD size
, oldsize
;
1361 static const WCHAR keyset
[] = {
1362 '`','%','s','`',' ','=',' ','%','s',' ','A','N','D',' ',0};
1363 static const WCHAR lastkeyset
[] = {
1364 '`','%','s','`',' ','=',' ','%','s',' ',0};
1365 static const WCHAR fmt
[] = {'S','E','L','E','C','T',' ','*',' ',
1366 'F','R','O','M',' ','`','%','s','`',' ',
1367 'W','H','E','R','E',' ','%','s',0};
1369 r
= MSI_DatabaseGetPrimaryKeys(merge
, table
, &keys
);
1370 if (r
!= ERROR_SUCCESS
)
1373 clause
= msi_alloc_zero(sizeof(WCHAR
));
1378 count
= MSI_RecordGetFieldCount(keys
);
1379 for (i
= 1; i
<= count
; i
++)
1381 key
= MSI_RecordGetString(keys
, i
);
1382 val
= get_key_value(view
, key
, rec
);
1385 setptr
= lastkeyset
;
1390 size
+= lstrlenW(setptr
) + lstrlenW(key
) + lstrlenW(val
) - 4;
1391 clause
= msi_realloc(clause
, size
* sizeof (WCHAR
));
1398 sprintfW(clause
+ oldsize
- 1, setptr
, key
, val
);
1402 size
= lstrlenW(fmt
) + lstrlenW(table
) + lstrlenW(clause
) + 1;
1403 query
= msi_alloc(size
* sizeof(WCHAR
));
1407 sprintfW(query
, fmt
, table
, clause
);
1411 msiobj_release(&keys
->hdr
);
1415 static UINT
merge_diff_row(MSIRECORD
*rec
, LPVOID param
)
1417 MERGEDATA
*data
= param
;
1418 MERGETABLE
*table
= data
->curtable
;
1420 MSIQUERY
*dbview
= NULL
;
1421 MSIRECORD
*row
= NULL
;
1422 LPWSTR query
= NULL
;
1423 UINT r
= ERROR_SUCCESS
;
1425 if (TABLE_Exists(data
->db
, table
->name
))
1427 query
= create_diff_row_query(data
->merge
, data
->curview
, table
->name
, rec
);
1429 return ERROR_OUTOFMEMORY
;
1431 r
= MSI_DatabaseOpenViewW(data
->db
, query
, &dbview
);
1432 if (r
!= ERROR_SUCCESS
)
1435 r
= MSI_ViewExecute(dbview
, NULL
);
1436 if (r
!= ERROR_SUCCESS
)
1439 r
= MSI_ViewFetch(dbview
, &row
);
1440 if (r
== ERROR_SUCCESS
&& !MSI_RecordsAreEqual(rec
, row
))
1442 table
->numconflicts
++;
1445 else if (r
!= ERROR_NO_MORE_ITEMS
)
1451 mergerow
= msi_alloc(sizeof(MERGEROW
));
1454 r
= ERROR_OUTOFMEMORY
;
1458 mergerow
->data
= MSI_CloneRecord(rec
);
1459 if (!mergerow
->data
)
1461 r
= ERROR_OUTOFMEMORY
;
1466 list_add_tail(&table
->rows
, &mergerow
->entry
);
1470 msiobj_release(&row
->hdr
);
1471 msiobj_release(&dbview
->hdr
);
1475 static UINT
msi_get_table_labels(MSIDATABASE
*db
, LPCWSTR table
, LPWSTR
**labels
, DWORD
*numlabels
)
1478 MSIRECORD
*prec
= NULL
;
1480 r
= MSI_DatabaseGetPrimaryKeys(db
, table
, &prec
);
1481 if (r
!= ERROR_SUCCESS
)
1484 count
= MSI_RecordGetFieldCount(prec
);
1485 *numlabels
= count
+ 1;
1486 *labels
= msi_alloc((*numlabels
)*sizeof(LPWSTR
));
1489 r
= ERROR_OUTOFMEMORY
;
1493 (*labels
)[0] = strdupW(table
);
1494 for (i
=1; i
<=count
; i
++ )
1496 (*labels
)[i
] = strdupW(MSI_RecordGetString(prec
, i
));
1500 msiobj_release( &prec
->hdr
);
1504 static UINT
msi_get_query_columns(MSIQUERY
*query
, LPWSTR
**columns
, DWORD
*numcolumns
)
1507 MSIRECORD
*prec
= NULL
;
1509 r
= MSI_ViewGetColumnInfo(query
, MSICOLINFO_NAMES
, &prec
);
1510 if (r
!= ERROR_SUCCESS
)
1513 count
= MSI_RecordGetFieldCount(prec
);
1514 *columns
= msi_alloc(count
*sizeof(LPWSTR
));
1517 r
= ERROR_OUTOFMEMORY
;
1521 for (i
=1; i
<=count
; i
++ )
1523 (*columns
)[i
-1] = strdupW(MSI_RecordGetString(prec
, i
));
1526 *numcolumns
= count
;
1529 msiobj_release( &prec
->hdr
);
1533 static UINT
msi_get_query_types(MSIQUERY
*query
, LPWSTR
**types
, DWORD
*numtypes
)
1536 MSIRECORD
*prec
= NULL
;
1538 r
= MSI_ViewGetColumnInfo(query
, MSICOLINFO_TYPES
, &prec
);
1539 if (r
!= ERROR_SUCCESS
)
1542 count
= MSI_RecordGetFieldCount(prec
);
1543 *types
= msi_alloc(count
*sizeof(LPWSTR
));
1546 r
= ERROR_OUTOFMEMORY
;
1551 for (i
=1; i
<=count
; i
++ )
1553 (*types
)[i
-1] = strdupW(MSI_RecordGetString(prec
, i
));
1557 msiobj_release( &prec
->hdr
);
1561 static void merge_free_rows(MERGETABLE
*table
)
1563 struct list
*item
, *cursor
;
1565 LIST_FOR_EACH_SAFE(item
, cursor
, &table
->rows
)
1567 MERGEROW
*row
= LIST_ENTRY(item
, MERGEROW
, entry
);
1569 list_remove(&row
->entry
);
1570 msiobj_release(&row
->data
->hdr
);
1575 static void free_merge_table(MERGETABLE
*table
)
1579 if (table
->labels
!= NULL
)
1581 for (i
= 0; i
< table
->numlabels
; i
++)
1582 msi_free(table
->labels
[i
]);
1584 msi_free(table
->labels
);
1587 if (table
->columns
!= NULL
)
1589 for (i
= 0; i
< table
->numcolumns
; i
++)
1590 msi_free(table
->columns
[i
]);
1592 msi_free(table
->columns
);
1595 if (table
->types
!= NULL
)
1597 for (i
= 0; i
< table
->numtypes
; i
++)
1598 msi_free(table
->types
[i
]);
1600 msi_free(table
->types
);
1603 msi_free(table
->name
);
1604 merge_free_rows(table
);
1609 static UINT
msi_get_merge_table (MSIDATABASE
*db
, LPCWSTR name
, MERGETABLE
**ptable
)
1613 MSIQUERY
*mergeview
= NULL
;
1615 static const WCHAR query
[] = {'S','E','L','E','C','T',' ','*',' ',
1616 'F','R','O','M',' ','`','%','s','`',0};
1618 table
= msi_alloc_zero(sizeof(MERGETABLE
));
1622 return ERROR_OUTOFMEMORY
;
1625 r
= msi_get_table_labels(db
, name
, &table
->labels
, &table
->numlabels
);
1626 if (r
!= ERROR_SUCCESS
)
1629 r
= MSI_OpenQuery(db
, &mergeview
, query
, name
);
1630 if (r
!= ERROR_SUCCESS
)
1633 r
= msi_get_query_columns(mergeview
, &table
->columns
, &table
->numcolumns
);
1634 if (r
!= ERROR_SUCCESS
)
1637 r
= msi_get_query_types(mergeview
, &table
->types
, &table
->numtypes
);
1638 if (r
!= ERROR_SUCCESS
)
1641 list_init(&table
->rows
);
1643 table
->name
= strdupW(name
);
1644 table
->numconflicts
= 0;
1646 msiobj_release(&mergeview
->hdr
);
1648 return ERROR_SUCCESS
;
1651 msiobj_release(&mergeview
->hdr
);
1652 free_merge_table(table
);
1657 static UINT
merge_diff_tables(MSIRECORD
*rec
, LPVOID param
)
1659 MERGEDATA
*data
= param
;
1661 MSIQUERY
*dbview
= NULL
;
1662 MSIQUERY
*mergeview
= NULL
;
1666 static const WCHAR query
[] = {'S','E','L','E','C','T',' ','*',' ',
1667 'F','R','O','M',' ','`','%','s','`',0};
1669 name
= MSI_RecordGetString(rec
, 1);
1671 r
= MSI_OpenQuery(data
->merge
, &mergeview
, query
, name
);
1672 if (r
!= ERROR_SUCCESS
)
1675 if (TABLE_Exists(data
->db
, name
))
1677 r
= MSI_OpenQuery(data
->db
, &dbview
, query
, name
);
1678 if (r
!= ERROR_SUCCESS
)
1681 r
= merge_verify_colnames(dbview
, mergeview
);
1682 if (r
!= ERROR_SUCCESS
)
1685 r
= merge_verify_primary_keys(data
->db
, data
->merge
, name
);
1686 if (r
!= ERROR_SUCCESS
)
1690 r
= msi_get_merge_table(data
->merge
, name
, &table
);
1691 if (r
!= ERROR_SUCCESS
)
1694 data
->curtable
= table
;
1695 data
->curview
= mergeview
;
1696 r
= MSI_IterateRecords(mergeview
, NULL
, merge_diff_row
, data
);
1697 if (r
!= ERROR_SUCCESS
)
1699 free_merge_table(table
);
1703 list_add_tail(data
->tabledata
, &table
->entry
);
1706 msiobj_release(&dbview
->hdr
);
1707 msiobj_release(&mergeview
->hdr
);
1711 static UINT
gather_merge_data(MSIDATABASE
*db
, MSIDATABASE
*merge
,
1712 struct list
*tabledata
)
1714 static const WCHAR query
[] = {
1715 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1716 '`','_','T','a','b','l','e','s','`',0};
1721 r
= MSI_DatabaseOpenViewW(merge
, query
, &view
);
1722 if (r
!= ERROR_SUCCESS
)
1727 data
.tabledata
= tabledata
;
1728 r
= MSI_IterateRecords(view
, NULL
, merge_diff_tables
, &data
);
1729 msiobj_release(&view
->hdr
);
1733 static UINT
merge_table(MSIDATABASE
*db
, MERGETABLE
*table
)
1739 if (!TABLE_Exists(db
, table
->name
))
1741 r
= msi_add_table_to_db(db
, table
->columns
, table
->types
,
1742 table
->labels
, table
->numlabels
, table
->numcolumns
);
1743 if (r
!= ERROR_SUCCESS
)
1744 return ERROR_FUNCTION_FAILED
;
1747 LIST_FOR_EACH_ENTRY(row
, &table
->rows
, MERGEROW
, entry
)
1749 r
= TABLE_CreateView(db
, table
->name
, &tv
);
1750 if (r
!= ERROR_SUCCESS
)
1753 r
= tv
->ops
->insert_row(tv
, row
->data
, -1, FALSE
);
1754 tv
->ops
->delete(tv
);
1756 if (r
!= ERROR_SUCCESS
)
1760 return ERROR_SUCCESS
;
1763 static UINT
update_merge_errors(MSIDATABASE
*db
, LPCWSTR error
,
1764 LPWSTR table
, DWORD numconflicts
)
1769 static const WCHAR create
[] = {
1770 'C','R','E','A','T','E',' ','T','A','B','L','E',' ',
1771 '`','%','s','`',' ','(','`','T','a','b','l','e','`',' ',
1772 'C','H','A','R','(','2','5','5',')',' ','N','O','T',' ',
1773 'N','U','L','L',',',' ','`','N','u','m','R','o','w','M','e','r','g','e',
1774 'C','o','n','f','l','i','c','t','s','`',' ','S','H','O','R','T',' ',
1775 'N','O','T',' ','N','U','L','L',' ','P','R','I','M','A','R','Y',' ',
1776 'K','E','Y',' ','`','T','a','b','l','e','`',')',0};
1777 static const WCHAR insert
[] = {
1778 'I','N','S','E','R','T',' ','I','N','T','O',' ',
1779 '`','%','s','`',' ','(','`','T','a','b','l','e','`',',',' ',
1780 '`','N','u','m','R','o','w','M','e','r','g','e',
1781 'C','o','n','f','l','i','c','t','s','`',')',' ','V','A','L','U','E','S',
1782 ' ','(','\'','%','s','\'',',',' ','%','d',')',0};
1784 if (!TABLE_Exists(db
, error
))
1786 r
= MSI_OpenQuery(db
, &view
, create
, error
);
1787 if (r
!= ERROR_SUCCESS
)
1790 r
= MSI_ViewExecute(view
, NULL
);
1791 msiobj_release(&view
->hdr
);
1792 if (r
!= ERROR_SUCCESS
)
1796 r
= MSI_OpenQuery(db
, &view
, insert
, error
, table
, numconflicts
);
1797 if (r
!= ERROR_SUCCESS
)
1800 r
= MSI_ViewExecute(view
, NULL
);
1801 msiobj_release(&view
->hdr
);
1805 UINT WINAPI
MsiDatabaseMergeW(MSIHANDLE hDatabase
, MSIHANDLE hDatabaseMerge
,
1806 LPCWSTR szTableName
)
1808 struct list tabledata
= LIST_INIT(tabledata
);
1809 struct list
*item
, *cursor
;
1810 MSIDATABASE
*db
, *merge
;
1815 TRACE("(%d, %d, %s)\n", hDatabase
, hDatabaseMerge
,
1816 debugstr_w(szTableName
));
1818 if (szTableName
&& !*szTableName
)
1819 return ERROR_INVALID_TABLE
;
1821 db
= msihandle2msiinfo(hDatabase
, MSIHANDLETYPE_DATABASE
);
1822 merge
= msihandle2msiinfo(hDatabaseMerge
, MSIHANDLETYPE_DATABASE
);
1825 r
= ERROR_INVALID_HANDLE
;
1829 r
= gather_merge_data(db
, merge
, &tabledata
);
1830 if (r
!= ERROR_SUCCESS
)
1834 LIST_FOR_EACH_ENTRY(table
, &tabledata
, MERGETABLE
, entry
)
1836 if (table
->numconflicts
)
1840 r
= update_merge_errors(db
, szTableName
, table
->name
,
1841 table
->numconflicts
);
1842 if (r
!= ERROR_SUCCESS
)
1847 r
= merge_table(db
, table
);
1848 if (r
!= ERROR_SUCCESS
)
1853 LIST_FOR_EACH_SAFE(item
, cursor
, &tabledata
)
1855 MERGETABLE
*table
= LIST_ENTRY(item
, MERGETABLE
, entry
);
1856 list_remove(&table
->entry
);
1857 free_merge_table(table
);
1861 r
= ERROR_FUNCTION_FAILED
;
1864 msiobj_release(&db
->hdr
);
1865 msiobj_release(&merge
->hdr
);
1869 MSIDBSTATE WINAPI
MsiGetDatabaseState( MSIHANDLE handle
)
1871 MSIDBSTATE ret
= MSIDBSTATE_READ
;
1874 TRACE("%d\n", handle
);
1876 db
= msihandle2msiinfo( handle
, MSIHANDLETYPE_DATABASE
);
1879 IWineMsiRemoteDatabase
*remote_database
;
1881 remote_database
= (IWineMsiRemoteDatabase
*)msi_get_remote( handle
);
1882 if ( !remote_database
)
1883 return MSIDBSTATE_ERROR
;
1885 IWineMsiRemoteDatabase_Release( remote_database
);
1886 WARN("MsiGetDatabaseState not allowed during a custom action!\n");
1888 return MSIDBSTATE_READ
;
1891 if (db
->mode
!= MSIDBOPEN_READONLY
)
1892 ret
= MSIDBSTATE_WRITE
;
1893 msiobj_release( &db
->hdr
);
1898 typedef struct _msi_remote_database_impl
{
1899 IWineMsiRemoteDatabase IWineMsiRemoteDatabase_iface
;
1902 } msi_remote_database_impl
;
1904 static inline msi_remote_database_impl
*impl_from_IWineMsiRemoteDatabase( IWineMsiRemoteDatabase
*iface
)
1906 return CONTAINING_RECORD(iface
, msi_remote_database_impl
, IWineMsiRemoteDatabase_iface
);
1909 static HRESULT WINAPI
mrd_QueryInterface( IWineMsiRemoteDatabase
*iface
,
1910 REFIID riid
,LPVOID
*ppobj
)
1912 if( IsEqualCLSID( riid
, &IID_IUnknown
) ||
1913 IsEqualCLSID( riid
, &IID_IWineMsiRemoteDatabase
) )
1915 IWineMsiRemoteDatabase_AddRef( iface
);
1920 return E_NOINTERFACE
;
1923 static ULONG WINAPI
mrd_AddRef( IWineMsiRemoteDatabase
*iface
)
1925 msi_remote_database_impl
* This
= impl_from_IWineMsiRemoteDatabase( iface
);
1927 return InterlockedIncrement( &This
->refs
);
1930 static ULONG WINAPI
mrd_Release( IWineMsiRemoteDatabase
*iface
)
1932 msi_remote_database_impl
* This
= impl_from_IWineMsiRemoteDatabase( iface
);
1935 r
= InterlockedDecrement( &This
->refs
);
1938 MsiCloseHandle( This
->database
);
1944 static HRESULT WINAPI
mrd_IsTablePersistent( IWineMsiRemoteDatabase
*iface
,
1945 LPCWSTR table
, MSICONDITION
*persistent
)
1947 msi_remote_database_impl
*This
= impl_from_IWineMsiRemoteDatabase( iface
);
1948 *persistent
= MsiDatabaseIsTablePersistentW(This
->database
, table
);
1952 static HRESULT WINAPI
mrd_GetPrimaryKeys( IWineMsiRemoteDatabase
*iface
,
1953 LPCWSTR table
, MSIHANDLE
*keys
)
1955 msi_remote_database_impl
*This
= impl_from_IWineMsiRemoteDatabase( iface
);
1956 UINT r
= MsiDatabaseGetPrimaryKeysW(This
->database
, table
, keys
);
1957 return HRESULT_FROM_WIN32(r
);
1960 static HRESULT WINAPI
mrd_GetSummaryInformation( IWineMsiRemoteDatabase
*iface
,
1961 UINT updatecount
, MSIHANDLE
*suminfo
)
1963 msi_remote_database_impl
*This
= impl_from_IWineMsiRemoteDatabase( iface
);
1964 UINT r
= MsiGetSummaryInformationW(This
->database
, NULL
, updatecount
, suminfo
);
1965 return HRESULT_FROM_WIN32(r
);
1968 static HRESULT WINAPI
mrd_OpenView( IWineMsiRemoteDatabase
*iface
,
1969 LPCWSTR query
, MSIHANDLE
*view
)
1971 msi_remote_database_impl
*This
= impl_from_IWineMsiRemoteDatabase( iface
);
1972 UINT r
= MsiDatabaseOpenViewW(This
->database
, query
, view
);
1973 return HRESULT_FROM_WIN32(r
);
1976 static HRESULT WINAPI
mrd_SetMsiHandle( IWineMsiRemoteDatabase
*iface
, MSIHANDLE handle
)
1978 msi_remote_database_impl
* This
= impl_from_IWineMsiRemoteDatabase( iface
);
1979 This
->database
= handle
;
1983 static const IWineMsiRemoteDatabaseVtbl msi_remote_database_vtbl
=
1988 mrd_IsTablePersistent
,
1990 mrd_GetSummaryInformation
,
1995 HRESULT
create_msi_remote_database( IUnknown
*pOuter
, LPVOID
*ppObj
)
1997 msi_remote_database_impl
*This
;
1999 This
= msi_alloc( sizeof *This
);
2001 return E_OUTOFMEMORY
;
2003 This
->IWineMsiRemoteDatabase_iface
.lpVtbl
= &msi_remote_database_vtbl
;
2007 *ppObj
= &This
->IWineMsiRemoteDatabase_iface
;