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
30 #include "wine/debug.h"
36 #include "msiserver.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(msi
);
46 * An .msi file is a structured storage file.
47 * It contains a number of streams.
48 * A stream for each table in the database.
49 * Two streams for the string table in the database.
50 * Any binary data in a table is a reference to a stream.
53 #define IS_INTMSIDBOPEN(x) (((ULONG_PTR)(x) >> 16) == 0)
55 static void free_transforms( MSIDATABASE
*db
)
57 while( !list_empty( &db
->transforms
) )
59 MSITRANSFORM
*t
= LIST_ENTRY( list_head( &db
->transforms
), MSITRANSFORM
, entry
);
60 list_remove( &t
->entry
);
61 IStorage_Release( t
->stg
);
66 static void free_streams( MSIDATABASE
*db
)
69 for (i
= 0; i
< db
->num_streams
; i
++)
71 if (db
->streams
[i
].stream
) IStream_Release( db
->streams
[i
].stream
);
73 msi_free( db
->streams
);
76 void append_storage_to_db( MSIDATABASE
*db
, IStorage
*stg
)
80 t
= msi_alloc( sizeof *t
);
82 IStorage_AddRef( stg
);
83 list_add_head( &db
->transforms
, &t
->entry
);
86 static VOID
MSI_CloseDatabase( MSIOBJECTHDR
*arg
)
88 MSIDATABASE
*db
= (MSIDATABASE
*) arg
;
92 free_cached_tables( db
);
93 free_transforms( db
);
94 if (db
->strings
) msi_destroy_stringtable( db
->strings
);
95 IStorage_Release( db
->storage
);
98 DeleteFileW( db
->deletefile
);
99 msi_free( db
->deletefile
);
101 msi_free( db
->tempfolder
);
104 static HRESULT
db_initialize( IStorage
*stg
, const GUID
*clsid
)
108 hr
= IStorage_SetClass( stg
, clsid
);
111 WARN("failed to set class id %#lx\n", hr
);
115 /* create the _Tables stream */
116 hr
= write_stream_data( stg
, L
"_Tables", NULL
, 0, TRUE
);
119 WARN("failed to create _Tables stream %#lx\n", hr
);
123 hr
= msi_init_string_table( stg
);
126 WARN("failed to initialize string table %#lx\n", hr
);
130 hr
= IStorage_Commit( stg
, 0 );
133 WARN("failed to commit changes %#lx\n", hr
);
140 UINT
MSI_OpenDatabaseW(LPCWSTR szDBPath
, LPCWSTR szPersist
, MSIDATABASE
**pdb
)
142 IStorage
*stg
= NULL
;
144 MSIDATABASE
*db
= NULL
;
145 UINT ret
= ERROR_FUNCTION_FAILED
;
149 BOOL created
= FALSE
, patch
= FALSE
;
150 WCHAR path
[MAX_PATH
];
152 TRACE("%s %s\n",debugstr_w(szDBPath
),debugstr_w(szPersist
) );
155 return ERROR_INVALID_PARAMETER
;
157 save_path
= szDBPath
;
158 if ( IS_INTMSIDBOPEN(szPersist
) )
160 mode
= LOWORD(szPersist
);
164 if (!CopyFileW( szDBPath
, szPersist
, FALSE
))
165 return ERROR_OPEN_FAILED
;
167 szDBPath
= szPersist
;
168 mode
= MSI_OPEN_TRANSACT
;
172 if ((mode
& MSI_OPEN_PATCHFILE
) == MSI_OPEN_PATCHFILE
)
174 TRACE("Database is a patch\n");
175 mode
&= ~MSI_OPEN_PATCHFILE
;
179 if( mode
== MSI_OPEN_READONLY
)
181 r
= StgOpenStorage( szDBPath
, NULL
,
182 STGM_DIRECT
|STGM_READ
|STGM_SHARE_DENY_WRITE
, NULL
, 0, &stg
);
184 else if( mode
== MSI_OPEN_CREATE
)
186 r
= StgCreateDocfile( szDBPath
,
187 STGM_CREATE
|STGM_TRANSACTED
|STGM_READWRITE
|STGM_SHARE_EXCLUSIVE
, 0, &stg
);
190 r
= db_initialize( stg
, patch
? &CLSID_MsiPatch
: &CLSID_MsiDatabase
);
193 else if( mode
== MSI_OPEN_CREATEDIRECT
)
195 r
= StgCreateDocfile( szDBPath
,
196 STGM_CREATE
|STGM_DIRECT
|STGM_READWRITE
|STGM_SHARE_EXCLUSIVE
, 0, &stg
);
199 r
= db_initialize( stg
, patch
? &CLSID_MsiPatch
: &CLSID_MsiDatabase
);
202 else if( mode
== MSI_OPEN_TRANSACT
)
204 r
= StgOpenStorage( szDBPath
, NULL
,
205 STGM_TRANSACTED
|STGM_READWRITE
|STGM_SHARE_DENY_WRITE
, NULL
, 0, &stg
);
207 else if( mode
== MSI_OPEN_DIRECT
)
209 r
= StgOpenStorage( szDBPath
, NULL
,
210 STGM_DIRECT
|STGM_READWRITE
|STGM_SHARE_EXCLUSIVE
, NULL
, 0, &stg
);
214 ERR("unknown flag %x\n",mode
);
215 return ERROR_INVALID_PARAMETER
;
218 if( FAILED( r
) || !stg
)
220 WARN("open failed r = %#lx for %s\n", r
, debugstr_w(szDBPath
));
221 return ERROR_FUNCTION_FAILED
;
224 r
= IStorage_Stat( stg
, &stat
, STATFLAG_NONAME
);
227 FIXME("Failed to stat storage\n");
231 if ( !IsEqualGUID( &stat
.clsid
, &CLSID_MsiDatabase
) &&
232 !IsEqualGUID( &stat
.clsid
, &CLSID_MsiPatch
) &&
233 !IsEqualGUID( &stat
.clsid
, &CLSID_MsiTransform
) )
235 ERR("storage GUID is not a MSI database GUID %s\n",
236 debugstr_guid(&stat
.clsid
) );
240 if ( patch
&& !IsEqualGUID( &stat
.clsid
, &CLSID_MsiPatch
) )
242 ERR("storage GUID is not the MSI patch GUID %s\n",
243 debugstr_guid(&stat
.clsid
) );
244 ret
= ERROR_OPEN_FAILED
;
248 db
= alloc_msiobject( MSIHANDLETYPE_DATABASE
, sizeof (MSIDATABASE
),
252 FIXME("Failed to allocate a handle\n");
256 if (!wcschr( save_path
, '\\' ))
258 GetCurrentDirectoryW( MAX_PATH
, path
);
259 lstrcatW( path
, L
"\\" );
260 lstrcatW( path
, save_path
);
263 lstrcpyW( path
, save_path
);
265 db
->path
= strdupW( path
);
266 db
->media_transform_offset
= MSI_INITIAL_MEDIA_TRANSFORM_OFFSET
;
267 db
->media_transform_disk_id
= MSI_INITIAL_MEDIA_TRANSFORM_DISKID
;
269 if( TRACE_ON( msi
) )
270 enum_stream_names( stg
);
275 db
->deletefile
= strdupW( szDBPath
);
276 list_init( &db
->tables
);
277 list_init( &db
->transforms
);
279 db
->strings
= msi_load_string_table( stg
, &db
->bytes_per_strref
);
285 msiobj_addref( &db
->hdr
);
286 IStorage_AddRef( stg
);
291 msiobj_release( &db
->hdr
);
293 IStorage_Release( stg
);
298 UINT WINAPI
MsiOpenDatabaseW(LPCWSTR szDBPath
, LPCWSTR szPersist
, MSIHANDLE
*phDB
)
303 TRACE("%s %s %p\n",debugstr_w(szDBPath
),debugstr_w(szPersist
), phDB
);
305 ret
= MSI_OpenDatabaseW( szDBPath
, szPersist
, &db
);
306 if( ret
== ERROR_SUCCESS
)
308 *phDB
= alloc_msihandle( &db
->hdr
);
310 ret
= ERROR_NOT_ENOUGH_MEMORY
;
311 msiobj_release( &db
->hdr
);
317 UINT WINAPI
MsiOpenDatabaseA(LPCSTR szDBPath
, LPCSTR szPersist
, MSIHANDLE
*phDB
)
319 HRESULT r
= ERROR_FUNCTION_FAILED
;
320 LPWSTR szwDBPath
= NULL
, szwPersist
= NULL
;
322 TRACE("%s %s %p\n", debugstr_a(szDBPath
), debugstr_a(szPersist
), phDB
);
326 szwDBPath
= strdupAtoW( szDBPath
);
331 if( !IS_INTMSIDBOPEN(szPersist
) )
333 szwPersist
= strdupAtoW( szPersist
);
338 szwPersist
= (LPWSTR
)(DWORD_PTR
)szPersist
;
340 r
= MsiOpenDatabaseW( szwDBPath
, szwPersist
, phDB
);
343 if( !IS_INTMSIDBOPEN(szPersist
) )
344 msi_free( szwPersist
);
345 msi_free( szwDBPath
);
350 static LPWSTR
msi_read_text_archive(LPCWSTR path
, DWORD
*len
)
355 DWORD read
, size
= 0;
357 file
= CreateFileW( path
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, NULL
);
358 if (file
== INVALID_HANDLE_VALUE
)
361 size
= GetFileSize( file
, NULL
);
362 if (!(data
= msi_alloc( size
))) goto done
;
364 if (!ReadFile( file
, data
, size
, &read
, NULL
) || read
!= size
) goto done
;
366 while (!data
[size
- 1]) size
--;
367 *len
= MultiByteToWideChar( CP_ACP
, 0, data
, size
, NULL
, 0 );
368 if ((wdata
= msi_alloc( (*len
+ 1) * sizeof(WCHAR
) )))
370 MultiByteToWideChar( CP_ACP
, 0, data
, size
, wdata
, *len
);
380 static void msi_parse_line(LPWSTR
*line
, LPWSTR
**entries
, DWORD
*num_entries
, DWORD
*len
)
382 LPWSTR ptr
= *line
, save
;
383 DWORD i
, count
= 1, chars_left
= *len
;
387 /* stay on this line */
388 while (chars_left
&& *ptr
!= '\n')
390 /* entries are separated by tabs */
398 *entries
= msi_alloc(count
* sizeof(LPWSTR
));
402 /* store pointers into the data */
404 for (i
= 0, ptr
= *line
; i
< count
; i
++)
406 while (chars_left
&& *ptr
== '\r')
413 while (chars_left
&& *ptr
!= '\t' && *ptr
!= '\n' && *ptr
!= '\r')
415 if (!*ptr
) *ptr
= '\n'; /* convert embedded nulls to \n */
416 if (ptr
> *line
&& *ptr
== '\x19' && *(ptr
- 1) == '\x11')
425 /* NULL-separate the data */
426 if (*ptr
== '\n' || *ptr
== '\r')
428 while (chars_left
&& (*ptr
== '\n' || *ptr
== '\r'))
439 (*entries
)[i
] = save
;
442 /* move to the next line if there's more, else EOF */
446 *num_entries
= count
;
449 static LPWSTR
msi_build_createsql_prelude(LPWSTR table
)
454 size
= ARRAY_SIZE(L
"CREATE TABLE `%s` ( ") + lstrlenW(table
) - 2;
455 prelude
= msi_alloc(size
* sizeof(WCHAR
));
459 swprintf(prelude
, size
, L
"CREATE TABLE `%s` ( ", table
);
463 static LPWSTR
msi_build_createsql_columns(LPWSTR
*columns_data
, LPWSTR
*types
, DWORD num_columns
)
467 DWORD sql_size
= 1, i
, len
;
468 WCHAR expanded
[128], *ptr
;
469 WCHAR size
[10], comma
[2], extra
[30];
471 columns
= msi_alloc_zero(sql_size
* sizeof(WCHAR
));
475 for (i
= 0; i
< num_columns
; i
++)
478 comma
[1] = size
[0] = extra
[0] = '\0';
480 if (i
== num_columns
- 1)
486 len
= wcstol(ptr
, NULL
, 10);
492 lstrcpyW(extra
, L
" NOT NULL");
495 lstrcatW(extra
, L
" LOCALIZABLE");
497 swprintf(size
, ARRAY_SIZE(size
), L
"(%s)", ptr
);
500 lstrcpyW(extra
, L
" NOT NULL");
504 swprintf(size
, ARRAY_SIZE(size
), L
"(%s)", ptr
);
507 lstrcpyW(extra
, L
" NOT NULL");
516 WARN("invalid int width %lu\n", len
);
522 lstrcpyW(extra
, L
" NOT NULL");
528 ERR("Unknown type: %c\n", types
[i
][0]);
533 swprintf(expanded
, ARRAY_SIZE(expanded
), L
"`%s` %s%s%s%s ", columns_data
[i
], type
, size
, extra
, comma
);
534 sql_size
+= lstrlenW(expanded
);
536 p
= msi_realloc(columns
, sql_size
* sizeof(WCHAR
));
544 lstrcatW(columns
, expanded
);
550 static LPWSTR
msi_build_createsql_postlude(LPWSTR
*primary_keys
, DWORD num_keys
)
552 LPWSTR postlude
, keys
, ptr
;
555 for (i
= 0, size
= 1; i
< num_keys
; i
++)
556 size
+= lstrlenW(L
"`%s`, ") + lstrlenW(primary_keys
[i
]) - 2;
558 keys
= msi_alloc(size
* sizeof(WCHAR
));
562 for (i
= 0, ptr
= keys
; i
< num_keys
; i
++)
564 ptr
+= swprintf(ptr
, size
- (ptr
- keys
), L
"`%s`, ", primary_keys
[i
]);
567 /* remove final ', ' */
570 size
= lstrlenW(L
"PRIMARY KEY %s)") + size
- 1;
571 postlude
= msi_alloc(size
* sizeof(WCHAR
));
575 swprintf(postlude
, size
, L
"PRIMARY KEY %s)", keys
);
582 static UINT
msi_add_table_to_db(MSIDATABASE
*db
, LPWSTR
*columns
, LPWSTR
*types
, LPWSTR
*labels
, DWORD num_labels
, DWORD num_columns
)
584 UINT r
= ERROR_OUTOFMEMORY
;
587 LPWSTR create_sql
= NULL
;
588 LPWSTR prelude
, columns_sql
, postlude
;
590 prelude
= msi_build_createsql_prelude(labels
[0]);
591 columns_sql
= msi_build_createsql_columns(columns
, types
, num_columns
);
592 postlude
= msi_build_createsql_postlude(labels
+ 1, num_labels
- 1); /* skip over table name */
594 if (!prelude
|| !columns_sql
|| !postlude
)
597 size
= lstrlenW(prelude
) + lstrlenW(columns_sql
) + lstrlenW(postlude
) + 1;
598 create_sql
= msi_alloc(size
* sizeof(WCHAR
));
602 lstrcpyW(create_sql
, prelude
);
603 lstrcatW(create_sql
, columns_sql
);
604 lstrcatW(create_sql
, postlude
);
606 r
= MSI_DatabaseOpenViewW( db
, create_sql
, &view
);
607 if (r
!= ERROR_SUCCESS
)
610 r
= MSI_ViewExecute(view
, NULL
);
612 msiobj_release(&view
->hdr
);
616 msi_free(columns_sql
);
618 msi_free(create_sql
);
622 static LPWSTR
msi_import_stream_filename(LPCWSTR path
, LPCWSTR name
)
625 LPWSTR fullname
, ptr
;
627 len
= lstrlenW(path
) + lstrlenW(name
) + 1;
628 fullname
= msi_alloc(len
*sizeof(WCHAR
));
632 lstrcpyW( fullname
, path
);
634 /* chop off extension from path */
635 ptr
= wcsrchr(fullname
, '.');
642 lstrcpyW( ptr
, name
);
646 static UINT
construct_record(DWORD num_columns
, LPWSTR
*types
,
647 LPWSTR
*data
, LPWSTR path
, MSIRECORD
**rec
)
651 *rec
= MSI_CreateRecord(num_columns
);
653 return ERROR_OUTOFMEMORY
;
655 for (i
= 0; i
< num_columns
; i
++)
659 case 'L': case 'l': case 'S': case 's':
660 MSI_RecordSetStringW(*rec
, i
+ 1, data
[i
]);
664 MSI_RecordSetInteger(*rec
, i
+ 1, wcstol(data
[i
], NULL
, 10));
670 LPWSTR file
= msi_import_stream_filename(path
, data
[i
]);
672 return ERROR_FUNCTION_FAILED
;
674 r
= MSI_RecordSetStreamFromFileW(*rec
, i
+ 1, file
);
676 if (r
!= ERROR_SUCCESS
)
677 return ERROR_FUNCTION_FAILED
;
681 ERR("Unhandled column type: %c\n", types
[i
][0]);
682 msiobj_release(&(*rec
)->hdr
);
683 return ERROR_FUNCTION_FAILED
;
687 return ERROR_SUCCESS
;
690 static UINT
msi_add_records_to_table(MSIDATABASE
*db
, LPWSTR
*columns
, LPWSTR
*types
,
691 LPWSTR
*labels
, LPWSTR
**records
,
692 int num_columns
, int num_records
,
700 r
= MSI_OpenQuery(db
, &view
, L
"SELECT * FROM `%s`", labels
[0]);
701 if (r
!= ERROR_SUCCESS
)
704 while (MSI_ViewFetch(view
, &rec
) != ERROR_NO_MORE_ITEMS
)
706 r
= MSI_ViewModify(view
, MSIMODIFY_DELETE
, rec
);
707 msiobj_release(&rec
->hdr
);
708 if (r
!= ERROR_SUCCESS
)
712 for (i
= 0; i
< num_records
; i
++)
714 r
= construct_record(num_columns
, types
, records
[i
], path
, &rec
);
715 if (r
!= ERROR_SUCCESS
)
718 r
= MSI_ViewModify(view
, MSIMODIFY_INSERT
, rec
);
719 if (r
!= ERROR_SUCCESS
)
721 msiobj_release(&rec
->hdr
);
725 msiobj_release(&rec
->hdr
);
729 msiobj_release(&view
->hdr
);
733 static UINT
MSI_DatabaseImport(MSIDATABASE
*db
, LPCWSTR folder
, LPCWSTR file
)
736 DWORD len
, i
, num_labels
, num_types
, num_columns
, num_records
= 0;
737 WCHAR
**columns
, **types
, **labels
, *path
, *ptr
, *data
, ***records
= NULL
, ***temp_records
;
739 TRACE("%p %s %s\n", db
, debugstr_w(folder
), debugstr_w(file
) );
741 if (!folder
|| !file
)
742 return ERROR_INVALID_PARAMETER
;
744 len
= lstrlenW(folder
) + lstrlenW(L
"\\") + lstrlenW(file
) + 1;
745 path
= msi_alloc( len
* sizeof(WCHAR
) );
747 return ERROR_OUTOFMEMORY
;
749 lstrcpyW( path
, folder
);
750 lstrcatW( path
, L
"\\" );
751 lstrcatW( path
, file
);
753 data
= msi_read_text_archive( path
, &len
);
757 return ERROR_FUNCTION_FAILED
;
761 msi_parse_line( &ptr
, &columns
, &num_columns
, &len
);
762 msi_parse_line( &ptr
, &types
, &num_types
, &len
);
763 msi_parse_line( &ptr
, &labels
, &num_labels
, &len
);
765 if (num_columns
== 1 && !columns
[0][0] && num_labels
== 1 && !labels
[0][0] &&
766 num_types
== 2 && !wcscmp( types
[1], L
"_ForceCodepage" ))
768 r
= msi_set_string_table_codepage( db
->strings
, wcstol( types
[0], NULL
, 10 ) );
772 if (num_columns
!= num_types
)
774 r
= ERROR_FUNCTION_FAILED
;
778 records
= msi_alloc(sizeof(WCHAR
**));
781 r
= ERROR_OUTOFMEMORY
;
785 /* read in the table records */
788 msi_parse_line( &ptr
, &records
[num_records
], NULL
, &len
);
791 temp_records
= msi_realloc(records
, (num_records
+ 1) * sizeof(WCHAR
**));
794 r
= ERROR_OUTOFMEMORY
;
797 records
= temp_records
;
800 if (!wcscmp(labels
[0], L
"_SummaryInformation"))
802 r
= msi_add_suminfo( db
, records
, num_records
, num_columns
);
803 if (r
!= ERROR_SUCCESS
)
805 r
= ERROR_FUNCTION_FAILED
;
811 if (!TABLE_Exists(db
, labels
[0]))
813 r
= msi_add_table_to_db( db
, columns
, types
, labels
, num_labels
, num_columns
);
814 if (r
!= ERROR_SUCCESS
)
816 r
= ERROR_FUNCTION_FAILED
;
821 r
= msi_add_records_to_table( db
, columns
, types
, labels
, records
, num_columns
, num_records
, path
);
831 for (i
= 0; i
< num_records
; i
++)
832 msi_free(records
[i
]);
838 UINT WINAPI
MsiDatabaseImportW( MSIHANDLE handle
, const WCHAR
*szFolder
, const WCHAR
*szFilename
)
843 TRACE( "%lu %s %s\n", handle
, debugstr_w(szFolder
), debugstr_w(szFilename
) );
845 if (!(db
= msihandle2msiinfo(handle
, MSIHANDLETYPE_DATABASE
)))
846 return ERROR_INVALID_HANDLE
;
848 r
= MSI_DatabaseImport( db
, szFolder
, szFilename
);
849 msiobj_release( &db
->hdr
);
853 UINT WINAPI
MsiDatabaseImportA( MSIHANDLE handle
, const char *szFolder
, const char *szFilename
)
855 WCHAR
*path
= NULL
, *file
= NULL
;
856 UINT r
= ERROR_OUTOFMEMORY
;
858 TRACE( "%lu %s %s\n", handle
, debugstr_a(szFolder
), debugstr_a(szFilename
) );
862 path
= strdupAtoW( szFolder
);
869 file
= strdupAtoW( szFilename
);
874 r
= MsiDatabaseImportW( handle
, path
, file
);
883 static UINT
msi_export_field( HANDLE handle
, MSIRECORD
*row
, UINT field
)
890 buffer
= msi_alloc( sz
);
892 return ERROR_OUTOFMEMORY
;
894 r
= MSI_RecordGetStringA( row
, field
, buffer
, &sz
);
895 if (r
== ERROR_MORE_DATA
)
899 sz
++; /* leave room for NULL terminator */
900 tmp
= msi_realloc( buffer
, sz
);
904 return ERROR_OUTOFMEMORY
;
908 r
= MSI_RecordGetStringA( row
, field
, buffer
, &sz
);
909 if (r
!= ERROR_SUCCESS
)
915 else if (r
!= ERROR_SUCCESS
)
921 ret
= WriteFile( handle
, buffer
, sz
, &sz
, NULL
);
923 return ret
? ERROR_SUCCESS
: ERROR_FUNCTION_FAILED
;
926 static UINT
msi_export_stream( const WCHAR
*folder
, const WCHAR
*table
, MSIRECORD
*row
, UINT field
, UINT start
)
928 WCHAR stream
[MAX_STREAM_NAME_LEN
+ 1], *path
;
929 DWORD sz
, read_size
, write_size
;
934 sz
= ARRAY_SIZE( stream
);
935 r
= MSI_RecordGetStringW( row
, start
, stream
, &sz
);
936 if (r
!= ERROR_SUCCESS
)
939 len
= sz
+ lstrlenW( folder
) + lstrlenW( table
) + ARRAY_SIZE( L
"%s\\%s" ) + 1;
940 if (!(path
= msi_alloc( len
* sizeof(WCHAR
) )))
941 return ERROR_OUTOFMEMORY
;
943 len
= swprintf( path
, len
, L
"%s\\%s", folder
, table
);
944 if (!CreateDirectoryW( path
, NULL
) && GetLastError() != ERROR_ALREADY_EXISTS
)
947 return ERROR_FUNCTION_FAILED
;
951 lstrcpyW( path
+ len
, stream
);
952 file
= CreateFileW( path
, GENERIC_WRITE
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
953 NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
955 if (file
== INVALID_HANDLE_VALUE
)
956 return ERROR_FUNCTION_FAILED
;
958 read_size
= sizeof(buffer
);
959 while (read_size
== sizeof(buffer
))
961 r
= MSI_RecordReadStream( row
, field
, buffer
, &read_size
);
962 if (r
!= ERROR_SUCCESS
)
967 if (!WriteFile( file
, buffer
, read_size
, &write_size
, NULL
) || read_size
!= write_size
)
970 return ERROR_WRITE_FAULT
;
977 struct row_export_info
984 static UINT
msi_export_record( struct row_export_info
*row_export_info
, MSIRECORD
*row
, UINT start
)
986 HANDLE handle
= row_export_info
->handle
;
987 UINT i
, count
, r
= ERROR_SUCCESS
;
991 count
= MSI_RecordGetFieldCount( row
);
992 for (i
= start
; i
<= count
; i
++)
994 r
= msi_export_field( handle
, row
, i
);
995 if (r
== ERROR_INVALID_PARAMETER
)
997 r
= msi_export_stream( row_export_info
->folder
, row_export_info
->table
, row
, i
, start
);
998 if (r
!= ERROR_SUCCESS
)
1001 /* exporting a binary stream, repeat the "Name" field */
1002 r
= msi_export_field( handle
, row
, start
);
1003 if (r
!= ERROR_SUCCESS
)
1006 else if (r
!= ERROR_SUCCESS
)
1009 sep
= (i
< count
) ? "\t" : "\r\n";
1010 if (!WriteFile( handle
, sep
, strlen(sep
), &sz
, NULL
))
1011 return ERROR_FUNCTION_FAILED
;
1016 static UINT
msi_export_row( MSIRECORD
*row
, void *arg
)
1018 return msi_export_record( arg
, row
, 1 );
1021 static UINT
msi_export_forcecodepage( HANDLE handle
, UINT codepage
)
1023 static const char fmt
[] = "\r\n\r\n%u\t_ForceCodepage\r\n";
1024 char data
[sizeof(fmt
) + 10];
1025 DWORD sz
= sprintf( data
, fmt
, codepage
);
1027 if (!WriteFile(handle
, data
, sz
, &sz
, NULL
))
1028 return ERROR_FUNCTION_FAILED
;
1030 return ERROR_SUCCESS
;
1033 static UINT
msi_export_summaryinformation( MSIDATABASE
*db
, HANDLE handle
)
1035 static const char header
[] = "PropertyId\tValue\r\n"
1037 "_SummaryInformation\tPropertyId\r\n";
1038 DWORD sz
= ARRAY_SIZE(header
) - 1;
1040 if (!WriteFile(handle
, header
, sz
, &sz
, NULL
))
1041 return ERROR_WRITE_FAULT
;
1043 return msi_export_suminfo( db
, handle
);
1046 static UINT
MSI_DatabaseExport( MSIDATABASE
*db
, LPCWSTR table
, LPCWSTR folder
, LPCWSTR file
)
1048 MSIRECORD
*rec
= NULL
;
1049 MSIQUERY
*view
= NULL
;
1054 TRACE("%p %s %s %s\n", db
, debugstr_w(table
),
1055 debugstr_w(folder
), debugstr_w(file
) );
1057 if (!folder
|| !file
)
1058 return ERROR_INVALID_PARAMETER
;
1060 len
= lstrlenW(folder
) + lstrlenW(file
) + 2;
1061 filename
= msi_alloc(len
* sizeof (WCHAR
));
1063 return ERROR_OUTOFMEMORY
;
1065 lstrcpyW( filename
, folder
);
1066 lstrcatW( filename
, L
"\\" );
1067 lstrcatW( filename
, file
);
1069 handle
= CreateFileW( filename
, GENERIC_READ
| GENERIC_WRITE
, 0,
1070 NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1071 msi_free( filename
);
1072 if (handle
== INVALID_HANDLE_VALUE
)
1073 return ERROR_FUNCTION_FAILED
;
1075 if (!wcscmp( table
, L
"_ForceCodepage" ))
1077 UINT codepage
= msi_get_string_table_codepage( db
->strings
);
1078 r
= msi_export_forcecodepage( handle
, codepage
);
1082 if (!wcscmp( table
, L
"_SummaryInformation" ))
1084 r
= msi_export_summaryinformation( db
, handle
);
1088 r
= MSI_OpenQuery( db
, &view
, L
"SELECT * FROM %s", table
);
1089 if (r
== ERROR_SUCCESS
)
1091 struct row_export_info row_export_info
= { handle
, folder
, table
};
1093 /* write out row 1, the column names */
1094 r
= MSI_ViewGetColumnInfo(view
, MSICOLINFO_NAMES
, &rec
);
1095 if (r
== ERROR_SUCCESS
)
1097 msi_export_record( &row_export_info
, rec
, 1 );
1098 msiobj_release( &rec
->hdr
);
1101 /* write out row 2, the column types */
1102 r
= MSI_ViewGetColumnInfo(view
, MSICOLINFO_TYPES
, &rec
);
1103 if (r
== ERROR_SUCCESS
)
1105 msi_export_record( &row_export_info
, rec
, 1 );
1106 msiobj_release( &rec
->hdr
);
1109 /* write out row 3, the table name + keys */
1110 r
= MSI_DatabaseGetPrimaryKeys( db
, table
, &rec
);
1111 if (r
== ERROR_SUCCESS
)
1113 MSI_RecordSetStringW( rec
, 0, table
);
1114 msi_export_record( &row_export_info
, rec
, 0 );
1115 msiobj_release( &rec
->hdr
);
1118 /* write out row 4 onwards, the data */
1119 r
= MSI_IterateRecords( view
, 0, msi_export_row
, &row_export_info
);
1120 msiobj_release( &view
->hdr
);
1124 CloseHandle( handle
);
1128 /***********************************************************************
1129 * MsiExportDatabaseW [MSI.@]
1131 * Writes a file containing the table data as tab separated ASCII.
1133 * The format is as follows:
1135 * row1 : colname1 <tab> colname2 <tab> .... colnameN <cr> <lf>
1136 * row2 : coltype1 <tab> coltype2 <tab> .... coltypeN <cr> <lf>
1137 * row3 : tablename <tab> key1 <tab> key2 <tab> ... keyM <cr> <lf>
1139 * Followed by the data, starting at row 1 with one row per line
1141 * row4 : data <tab> data <tab> data <tab> ... data <cr> <lf>
1143 UINT WINAPI
MsiDatabaseExportW( MSIHANDLE handle
, const WCHAR
*szTable
, const WCHAR
*szFolder
, const WCHAR
*szFilename
)
1148 TRACE( "%lu %s %s %s\n", handle
, debugstr_w(szTable
), debugstr_w(szFolder
), debugstr_w(szFilename
) );
1150 if (!(db
= msihandle2msiinfo(handle
, MSIHANDLETYPE_DATABASE
)))
1151 return ERROR_INVALID_HANDLE
;
1153 r
= MSI_DatabaseExport( db
, szTable
, szFolder
, szFilename
);
1154 msiobj_release( &db
->hdr
);
1158 UINT WINAPI
MsiDatabaseExportA( MSIHANDLE handle
, const char *szTable
, const char *szFolder
, const char *szFilename
)
1160 WCHAR
*path
= NULL
, *file
= NULL
, *table
= NULL
;
1161 UINT r
= ERROR_OUTOFMEMORY
;
1163 TRACE( "%lu %s %s %s\n", handle
, debugstr_a(szTable
), debugstr_a(szFolder
), debugstr_a(szFilename
) );
1167 table
= strdupAtoW( szTable
);
1174 path
= strdupAtoW( szFolder
);
1181 file
= strdupAtoW( szFilename
);
1186 r
= MsiDatabaseExportW( handle
, table
, path
, file
);
1196 UINT WINAPI
MsiDatabaseMergeA( MSIHANDLE hDatabase
, MSIHANDLE hDatabaseMerge
, const char *szTableName
)
1201 TRACE("%lu, %lu, %s\n", hDatabase
, hDatabaseMerge
, debugstr_a(szTableName
) );
1203 table
= strdupAtoW(szTableName
);
1204 r
= MsiDatabaseMergeW(hDatabase
, hDatabaseMerge
, table
);
1210 typedef struct _tagMERGETABLE
1224 typedef struct _tagMERGEROW
1230 typedef struct _tagMERGEDATA
1234 MERGETABLE
*curtable
;
1236 struct list
*tabledata
;
1239 static BOOL
merge_type_match(LPCWSTR type1
, LPCWSTR type2
)
1241 if (((type1
[0] == 'l') || (type1
[0] == 's')) &&
1242 ((type2
[0] == 'l') || (type2
[0] == 's')))
1245 if (((type1
[0] == 'L') || (type1
[0] == 'S')) &&
1246 ((type2
[0] == 'L') || (type2
[0] == 'S')))
1249 return !wcscmp( type1
, type2
);
1252 static UINT
merge_verify_colnames(MSIQUERY
*dbview
, MSIQUERY
*mergeview
)
1254 MSIRECORD
*dbrec
, *mergerec
;
1257 r
= MSI_ViewGetColumnInfo(dbview
, MSICOLINFO_NAMES
, &dbrec
);
1258 if (r
!= ERROR_SUCCESS
)
1261 r
= MSI_ViewGetColumnInfo(mergeview
, MSICOLINFO_NAMES
, &mergerec
);
1262 if (r
!= ERROR_SUCCESS
)
1264 msiobj_release(&dbrec
->hdr
);
1268 count
= MSI_RecordGetFieldCount(dbrec
);
1269 for (i
= 1; i
<= count
; i
++)
1271 if (!MSI_RecordGetString(mergerec
, i
))
1274 if (wcscmp( MSI_RecordGetString( dbrec
, i
), MSI_RecordGetString( mergerec
, i
) ))
1276 r
= ERROR_DATATYPE_MISMATCH
;
1281 msiobj_release(&dbrec
->hdr
);
1282 msiobj_release(&mergerec
->hdr
);
1283 dbrec
= mergerec
= NULL
;
1285 r
= MSI_ViewGetColumnInfo(dbview
, MSICOLINFO_TYPES
, &dbrec
);
1286 if (r
!= ERROR_SUCCESS
)
1289 r
= MSI_ViewGetColumnInfo(mergeview
, MSICOLINFO_TYPES
, &mergerec
);
1290 if (r
!= ERROR_SUCCESS
)
1292 msiobj_release(&dbrec
->hdr
);
1296 count
= MSI_RecordGetFieldCount(dbrec
);
1297 for (i
= 1; i
<= count
; i
++)
1299 if (!MSI_RecordGetString(mergerec
, i
))
1302 if (!merge_type_match(MSI_RecordGetString(dbrec
, i
),
1303 MSI_RecordGetString(mergerec
, i
)))
1305 r
= ERROR_DATATYPE_MISMATCH
;
1311 msiobj_release(&dbrec
->hdr
);
1312 msiobj_release(&mergerec
->hdr
);
1317 static UINT
merge_verify_primary_keys(MSIDATABASE
*db
, MSIDATABASE
*mergedb
,
1320 MSIRECORD
*dbrec
, *mergerec
= NULL
;
1323 r
= MSI_DatabaseGetPrimaryKeys(db
, table
, &dbrec
);
1324 if (r
!= ERROR_SUCCESS
)
1327 r
= MSI_DatabaseGetPrimaryKeys(mergedb
, table
, &mergerec
);
1328 if (r
!= ERROR_SUCCESS
)
1331 count
= MSI_RecordGetFieldCount(dbrec
);
1332 if (count
!= MSI_RecordGetFieldCount(mergerec
))
1334 r
= ERROR_DATATYPE_MISMATCH
;
1338 for (i
= 1; i
<= count
; i
++)
1340 if (wcscmp( MSI_RecordGetString( dbrec
, i
), MSI_RecordGetString( mergerec
, i
) ))
1342 r
= ERROR_DATATYPE_MISMATCH
;
1348 msiobj_release(&dbrec
->hdr
);
1349 msiobj_release(&mergerec
->hdr
);
1354 static LPWSTR
get_key_value(MSIQUERY
*view
, LPCWSTR key
, MSIRECORD
*rec
)
1356 MSIRECORD
*colnames
;
1362 r
= MSI_ViewGetColumnInfo(view
, MSICOLINFO_NAMES
, &colnames
);
1363 if (r
!= ERROR_SUCCESS
)
1368 str
= msi_dup_record_field(colnames
, ++i
);
1369 cmp
= wcscmp( key
, str
);
1373 msiobj_release(&colnames
->hdr
);
1375 r
= MSI_RecordGetStringW(rec
, i
, NULL
, &sz
);
1376 if (r
!= ERROR_SUCCESS
)
1380 if (MSI_RecordGetString(rec
, i
)) /* check record field is a string */
1382 /* quote string record fields */
1384 val
= msi_alloc(sz
* sizeof(WCHAR
));
1388 lstrcpyW(val
, L
"'");
1389 r
= MSI_RecordGetStringW(rec
, i
, val
+ 1, &sz
);
1390 lstrcpyW(val
+ 1 + sz
, L
"'");
1394 /* do not quote integer record fields */
1395 val
= msi_alloc(sz
* sizeof(WCHAR
));
1399 r
= MSI_RecordGetStringW(rec
, i
, val
, &sz
);
1402 if (r
!= ERROR_SUCCESS
)
1404 ERR("failed to get string!\n");
1412 static LPWSTR
create_diff_row_query(MSIDATABASE
*merge
, MSIQUERY
*view
,
1413 LPWSTR table
, MSIRECORD
*rec
)
1415 LPWSTR query
= NULL
, clause
= NULL
, val
;
1416 LPCWSTR setptr
, key
;
1417 DWORD size
, oldsize
;
1421 r
= MSI_DatabaseGetPrimaryKeys(merge
, table
, &keys
);
1422 if (r
!= ERROR_SUCCESS
)
1425 clause
= msi_alloc_zero(sizeof(WCHAR
));
1430 count
= MSI_RecordGetFieldCount(keys
);
1431 for (i
= 1; i
<= count
; i
++)
1433 key
= MSI_RecordGetString(keys
, i
);
1434 val
= get_key_value(view
, key
, rec
);
1437 setptr
= L
"`%s` = %s ";
1439 setptr
= L
"`%s` = %s AND ";
1442 size
+= lstrlenW(setptr
) + lstrlenW(key
) + lstrlenW(val
) - 4;
1443 clause
= msi_realloc(clause
, size
* sizeof (WCHAR
));
1450 swprintf(clause
+ oldsize
- 1, size
- (oldsize
- 1), setptr
, key
, val
);
1454 size
= lstrlenW(L
"SELECT * FROM `%s` WHERE %s") + lstrlenW(table
) + lstrlenW(clause
) + 1;
1455 query
= msi_alloc(size
* sizeof(WCHAR
));
1459 swprintf(query
, size
, L
"SELECT * FROM `%s` WHERE %s", table
, clause
);
1463 msiobj_release(&keys
->hdr
);
1467 static UINT
merge_diff_row(MSIRECORD
*rec
, LPVOID param
)
1469 MERGEDATA
*data
= param
;
1470 MERGETABLE
*table
= data
->curtable
;
1472 MSIQUERY
*dbview
= NULL
;
1473 MSIRECORD
*row
= NULL
;
1474 LPWSTR query
= NULL
;
1475 UINT r
= ERROR_SUCCESS
;
1477 if (TABLE_Exists(data
->db
, table
->name
))
1479 query
= create_diff_row_query(data
->merge
, data
->curview
, table
->name
, rec
);
1481 return ERROR_OUTOFMEMORY
;
1483 r
= MSI_DatabaseOpenViewW(data
->db
, query
, &dbview
);
1484 if (r
!= ERROR_SUCCESS
)
1487 r
= MSI_ViewExecute(dbview
, NULL
);
1488 if (r
!= ERROR_SUCCESS
)
1491 r
= MSI_ViewFetch(dbview
, &row
);
1492 if (r
== ERROR_SUCCESS
&& !MSI_RecordsAreEqual(rec
, row
))
1494 table
->numconflicts
++;
1497 else if (r
!= ERROR_NO_MORE_ITEMS
)
1503 mergerow
= msi_alloc(sizeof(MERGEROW
));
1506 r
= ERROR_OUTOFMEMORY
;
1510 mergerow
->data
= MSI_CloneRecord(rec
);
1511 if (!mergerow
->data
)
1513 r
= ERROR_OUTOFMEMORY
;
1518 list_add_tail(&table
->rows
, &mergerow
->entry
);
1522 msiobj_release(&row
->hdr
);
1523 msiobj_release(&dbview
->hdr
);
1527 static UINT
msi_get_table_labels(MSIDATABASE
*db
, LPCWSTR table
, LPWSTR
**labels
, DWORD
*numlabels
)
1530 MSIRECORD
*prec
= NULL
;
1532 r
= MSI_DatabaseGetPrimaryKeys(db
, table
, &prec
);
1533 if (r
!= ERROR_SUCCESS
)
1536 count
= MSI_RecordGetFieldCount(prec
);
1537 *numlabels
= count
+ 1;
1538 *labels
= msi_alloc((*numlabels
)*sizeof(LPWSTR
));
1541 r
= ERROR_OUTOFMEMORY
;
1545 (*labels
)[0] = strdupW(table
);
1546 for (i
=1; i
<=count
; i
++ )
1548 (*labels
)[i
] = strdupW(MSI_RecordGetString(prec
, i
));
1552 msiobj_release( &prec
->hdr
);
1556 static UINT
msi_get_query_columns(MSIQUERY
*query
, LPWSTR
**columns
, DWORD
*numcolumns
)
1559 MSIRECORD
*prec
= NULL
;
1561 r
= MSI_ViewGetColumnInfo(query
, MSICOLINFO_NAMES
, &prec
);
1562 if (r
!= ERROR_SUCCESS
)
1565 count
= MSI_RecordGetFieldCount(prec
);
1566 *columns
= msi_alloc(count
*sizeof(LPWSTR
));
1569 r
= ERROR_OUTOFMEMORY
;
1573 for (i
=1; i
<=count
; i
++ )
1575 (*columns
)[i
-1] = strdupW(MSI_RecordGetString(prec
, i
));
1578 *numcolumns
= count
;
1581 msiobj_release( &prec
->hdr
);
1585 static UINT
msi_get_query_types(MSIQUERY
*query
, LPWSTR
**types
, DWORD
*numtypes
)
1588 MSIRECORD
*prec
= NULL
;
1590 r
= MSI_ViewGetColumnInfo(query
, MSICOLINFO_TYPES
, &prec
);
1591 if (r
!= ERROR_SUCCESS
)
1594 count
= MSI_RecordGetFieldCount(prec
);
1595 *types
= msi_alloc(count
*sizeof(LPWSTR
));
1598 r
= ERROR_OUTOFMEMORY
;
1603 for (i
=1; i
<=count
; i
++ )
1605 (*types
)[i
-1] = strdupW(MSI_RecordGetString(prec
, i
));
1609 msiobj_release( &prec
->hdr
);
1613 static void merge_free_rows(MERGETABLE
*table
)
1615 struct list
*item
, *cursor
;
1617 LIST_FOR_EACH_SAFE(item
, cursor
, &table
->rows
)
1619 MERGEROW
*row
= LIST_ENTRY(item
, MERGEROW
, entry
);
1621 list_remove(&row
->entry
);
1622 msiobj_release(&row
->data
->hdr
);
1627 static void free_merge_table(MERGETABLE
*table
)
1631 if (table
->labels
!= NULL
)
1633 for (i
= 0; i
< table
->numlabels
; i
++)
1634 msi_free(table
->labels
[i
]);
1636 msi_free(table
->labels
);
1639 if (table
->columns
!= NULL
)
1641 for (i
= 0; i
< table
->numcolumns
; i
++)
1642 msi_free(table
->columns
[i
]);
1644 msi_free(table
->columns
);
1647 if (table
->types
!= NULL
)
1649 for (i
= 0; i
< table
->numtypes
; i
++)
1650 msi_free(table
->types
[i
]);
1652 msi_free(table
->types
);
1655 msi_free(table
->name
);
1656 merge_free_rows(table
);
1661 static UINT
msi_get_merge_table (MSIDATABASE
*db
, LPCWSTR name
, MERGETABLE
**ptable
)
1665 MSIQUERY
*mergeview
= NULL
;
1667 table
= msi_alloc_zero(sizeof(MERGETABLE
));
1671 return ERROR_OUTOFMEMORY
;
1674 r
= msi_get_table_labels(db
, name
, &table
->labels
, &table
->numlabels
);
1675 if (r
!= ERROR_SUCCESS
)
1678 r
= MSI_OpenQuery(db
, &mergeview
, L
"SELECT * FROM `%s`", name
);
1679 if (r
!= ERROR_SUCCESS
)
1682 r
= msi_get_query_columns(mergeview
, &table
->columns
, &table
->numcolumns
);
1683 if (r
!= ERROR_SUCCESS
)
1686 r
= msi_get_query_types(mergeview
, &table
->types
, &table
->numtypes
);
1687 if (r
!= ERROR_SUCCESS
)
1690 list_init(&table
->rows
);
1692 table
->name
= strdupW(name
);
1693 table
->numconflicts
= 0;
1695 msiobj_release(&mergeview
->hdr
);
1697 return ERROR_SUCCESS
;
1700 msiobj_release(&mergeview
->hdr
);
1701 free_merge_table(table
);
1706 static UINT
merge_diff_tables(MSIRECORD
*rec
, LPVOID param
)
1708 MERGEDATA
*data
= param
;
1710 MSIQUERY
*dbview
= NULL
;
1711 MSIQUERY
*mergeview
= NULL
;
1715 name
= MSI_RecordGetString(rec
, 1);
1717 r
= MSI_OpenQuery(data
->merge
, &mergeview
, L
"SELECT * FROM `%s`", name
);
1718 if (r
!= ERROR_SUCCESS
)
1721 if (TABLE_Exists(data
->db
, name
))
1723 r
= MSI_OpenQuery(data
->db
, &dbview
, L
"SELECT * FROM `%s`", name
);
1724 if (r
!= ERROR_SUCCESS
)
1727 r
= merge_verify_colnames(dbview
, mergeview
);
1728 if (r
!= ERROR_SUCCESS
)
1731 r
= merge_verify_primary_keys(data
->db
, data
->merge
, name
);
1732 if (r
!= ERROR_SUCCESS
)
1736 r
= msi_get_merge_table(data
->merge
, name
, &table
);
1737 if (r
!= ERROR_SUCCESS
)
1740 data
->curtable
= table
;
1741 data
->curview
= mergeview
;
1742 r
= MSI_IterateRecords(mergeview
, NULL
, merge_diff_row
, data
);
1743 if (r
!= ERROR_SUCCESS
)
1745 free_merge_table(table
);
1749 list_add_tail(data
->tabledata
, &table
->entry
);
1752 msiobj_release(&dbview
->hdr
);
1753 msiobj_release(&mergeview
->hdr
);
1757 static UINT
gather_merge_data(MSIDATABASE
*db
, MSIDATABASE
*merge
,
1758 struct list
*tabledata
)
1764 r
= MSI_DatabaseOpenViewW(merge
, L
"SELECT * FROM `_Tables`", &view
);
1765 if (r
!= ERROR_SUCCESS
)
1770 data
.tabledata
= tabledata
;
1771 r
= MSI_IterateRecords(view
, NULL
, merge_diff_tables
, &data
);
1772 msiobj_release(&view
->hdr
);
1776 static UINT
merge_table(MSIDATABASE
*db
, MERGETABLE
*table
)
1782 if (!TABLE_Exists(db
, table
->name
))
1784 r
= msi_add_table_to_db(db
, table
->columns
, table
->types
,
1785 table
->labels
, table
->numlabels
, table
->numcolumns
);
1786 if (r
!= ERROR_SUCCESS
)
1787 return ERROR_FUNCTION_FAILED
;
1790 LIST_FOR_EACH_ENTRY(row
, &table
->rows
, MERGEROW
, entry
)
1792 r
= TABLE_CreateView(db
, table
->name
, &tv
);
1793 if (r
!= ERROR_SUCCESS
)
1796 r
= tv
->ops
->insert_row(tv
, row
->data
, -1, FALSE
);
1797 tv
->ops
->delete(tv
);
1799 if (r
!= ERROR_SUCCESS
)
1803 return ERROR_SUCCESS
;
1806 static UINT
update_merge_errors(MSIDATABASE
*db
, LPCWSTR error
,
1807 LPWSTR table
, DWORD numconflicts
)
1812 if (!TABLE_Exists(db
, error
))
1814 r
= MSI_OpenQuery(db
, &view
, L
"CREATE TABLE `%s` (`Table` CHAR(255) NOT NULL, `NumRowMergeConflicts` SHORT "
1815 "NOT NULL PRIMARY KEY `Table`)" , error
);
1816 if (r
!= ERROR_SUCCESS
)
1819 r
= MSI_ViewExecute(view
, NULL
);
1820 msiobj_release(&view
->hdr
);
1821 if (r
!= ERROR_SUCCESS
)
1825 r
= MSI_OpenQuery(db
, &view
, L
"INSERT INTO `%s` (`Table`, `NumRowMergeConflicts`) VALUES ('%s', %d)", error
,
1826 table
, numconflicts
);
1827 if (r
!= ERROR_SUCCESS
)
1830 r
= MSI_ViewExecute(view
, NULL
);
1831 msiobj_release(&view
->hdr
);
1835 UINT WINAPI
MsiDatabaseMergeW( MSIHANDLE hDatabase
, MSIHANDLE hDatabaseMerge
, const WCHAR
*szTableName
)
1837 struct list tabledata
= LIST_INIT(tabledata
);
1838 struct list
*item
, *cursor
;
1839 MSIDATABASE
*db
, *merge
;
1844 TRACE( "%lu, %lu, %s\n", hDatabase
, hDatabaseMerge
, debugstr_w(szTableName
) );
1846 if (szTableName
&& !*szTableName
)
1847 return ERROR_INVALID_TABLE
;
1849 db
= msihandle2msiinfo(hDatabase
, MSIHANDLETYPE_DATABASE
);
1850 merge
= msihandle2msiinfo(hDatabaseMerge
, MSIHANDLETYPE_DATABASE
);
1853 r
= ERROR_INVALID_HANDLE
;
1857 r
= gather_merge_data(db
, merge
, &tabledata
);
1858 if (r
!= ERROR_SUCCESS
)
1862 LIST_FOR_EACH_ENTRY(table
, &tabledata
, MERGETABLE
, entry
)
1864 if (table
->numconflicts
)
1868 r
= update_merge_errors(db
, szTableName
, table
->name
,
1869 table
->numconflicts
);
1870 if (r
!= ERROR_SUCCESS
)
1875 r
= merge_table(db
, table
);
1876 if (r
!= ERROR_SUCCESS
)
1881 LIST_FOR_EACH_SAFE(item
, cursor
, &tabledata
)
1883 MERGETABLE
*table
= LIST_ENTRY(item
, MERGETABLE
, entry
);
1884 list_remove(&table
->entry
);
1885 free_merge_table(table
);
1889 r
= ERROR_FUNCTION_FAILED
;
1892 msiobj_release(&db
->hdr
);
1893 msiobj_release(&merge
->hdr
);
1897 MSIDBSTATE WINAPI
MsiGetDatabaseState( MSIHANDLE handle
)
1899 MSIDBSTATE ret
= MSIDBSTATE_READ
;
1902 TRACE( "%lu\n", handle
);
1904 if (!(db
= msihandle2msiinfo( handle
, MSIHANDLETYPE_DATABASE
)))
1905 return MSIDBSTATE_ERROR
;
1907 if (db
->mode
!= MSI_OPEN_READONLY
)
1908 ret
= MSIDBSTATE_WRITE
;
1909 msiobj_release( &db
->hdr
);
1914 MSICONDITION __cdecl
s_remote_DatabaseIsTablePersistent(MSIHANDLE db
, LPCWSTR table
)
1916 return MsiDatabaseIsTablePersistentW(db
, table
);
1919 UINT __cdecl
s_remote_DatabaseGetPrimaryKeys(MSIHANDLE db
, LPCWSTR table
, struct wire_record
**rec
)
1922 UINT r
= MsiDatabaseGetPrimaryKeysW(db
, table
, &handle
);
1925 *rec
= marshal_record(handle
);
1926 MsiCloseHandle(handle
);
1930 UINT __cdecl
s_remote_DatabaseGetSummaryInformation(MSIHANDLE db
, UINT updatecount
, MSIHANDLE
*suminfo
)
1932 return MsiGetSummaryInformationW(db
, NULL
, updatecount
, suminfo
);
1935 UINT __cdecl
s_remote_DatabaseOpenView(MSIHANDLE db
, LPCWSTR query
, MSIHANDLE
*view
)
1937 return MsiDatabaseOpenViewW(db
, query
, view
);