2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2002-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
28 #include "wine/debug.h"
29 #include "wine/unicode.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(msi
);
41 static void MSI_CloseView( MSIOBJECTHDR
*arg
)
43 MSIQUERY
*query
= (MSIQUERY
*) arg
;
46 if( query
->view
&& query
->view
->ops
->delete )
47 query
->view
->ops
->delete( query
->view
);
48 msiobj_release( &query
->db
->hdr
);
50 LIST_FOR_EACH_SAFE( ptr
, t
, &query
->mem
)
56 UINT
VIEW_find_column( MSIVIEW
*table
, LPCWSTR name
, UINT
*n
)
61 r
= table
->ops
->get_dimensions( table
, NULL
, &count
);
62 if( r
!= ERROR_SUCCESS
)
65 for( i
=1; i
<=count
; i
++ )
70 r
= table
->ops
->get_column_info( table
, i
, &col_name
, NULL
);
71 if( r
!= ERROR_SUCCESS
)
73 x
= lstrcmpW( name
, col_name
);
82 return ERROR_INVALID_PARAMETER
;
85 UINT WINAPI
MsiDatabaseOpenViewA(MSIHANDLE hdb
,
86 LPCSTR szQuery
, MSIHANDLE
*phView
)
91 TRACE("%ld %s %p\n", hdb
, debugstr_a(szQuery
), phView
);
95 szwQuery
= strdupAtoW( szQuery
);
97 return ERROR_FUNCTION_FAILED
;
102 r
= MsiDatabaseOpenViewW( hdb
, szwQuery
, phView
);
104 msi_free( szwQuery
);
108 UINT
MSI_DatabaseOpenViewW(MSIDATABASE
*db
,
109 LPCWSTR szQuery
, MSIQUERY
**pView
)
114 TRACE("%s %p\n", debugstr_w(szQuery
), pView
);
117 return ERROR_INVALID_PARAMETER
;
119 /* pre allocate a handle to hold a pointer to the view */
120 query
= alloc_msiobject( MSIHANDLETYPE_VIEW
, sizeof (MSIQUERY
),
123 return ERROR_FUNCTION_FAILED
;
125 msiobj_addref( &db
->hdr
);
129 list_init( &query
->mem
);
131 r
= MSI_ParseSQL( db
, szQuery
, &query
->view
, &query
->mem
);
132 if( r
== ERROR_SUCCESS
)
134 msiobj_addref( &query
->hdr
);
138 msiobj_release( &query
->hdr
);
142 UINT
MSI_OpenQuery( MSIDATABASE
*db
, MSIQUERY
**view
, LPCWSTR fmt
, ... )
148 /* construct the string */
152 query
= msi_alloc( size
*sizeof(WCHAR
) );
154 res
= vsnprintfW(query
, size
, fmt
, va
);
156 if (res
== -1) size
*= 2;
157 else if (res
>= size
) size
= res
+ 1;
161 /* perform the query */
162 r
= MSI_DatabaseOpenViewW(db
, query
, view
);
167 UINT
MSI_IterateRecords( MSIQUERY
*view
, DWORD
*count
,
168 record_func func
, LPVOID param
)
170 MSIRECORD
*rec
= NULL
;
171 UINT r
, n
= 0, max
= 0;
173 r
= MSI_ViewExecute( view
, NULL
);
174 if( r
!= ERROR_SUCCESS
)
180 /* iterate a query */
181 for( n
= 0; (max
== 0) || (n
< max
); n
++ )
183 r
= MSI_ViewFetch( view
, &rec
);
184 if( r
!= ERROR_SUCCESS
)
187 r
= func( rec
, param
);
188 msiobj_release( &rec
->hdr
);
189 if( r
!= ERROR_SUCCESS
)
193 MSI_ViewClose( view
);
198 if( r
== ERROR_NO_MORE_ITEMS
)
204 /* return a single record from a query */
205 MSIRECORD
*MSI_QueryGetRecord( MSIDATABASE
*db
, LPCWSTR fmt
, ... )
207 MSIRECORD
*rec
= NULL
;
208 MSIQUERY
*view
= NULL
;
213 /* construct the string */
217 query
= msi_alloc( size
*sizeof(WCHAR
) );
219 res
= vsnprintfW(query
, size
, fmt
, va
);
221 if (res
== -1) size
*= 2;
222 else if (res
>= size
) size
= res
+ 1;
226 /* perform the query */
227 r
= MSI_DatabaseOpenViewW(db
, query
, &view
);
230 if( r
== ERROR_SUCCESS
)
232 MSI_ViewExecute( view
, NULL
);
233 MSI_ViewFetch( view
, &rec
);
234 MSI_ViewClose( view
);
235 msiobj_release( &view
->hdr
);
240 UINT WINAPI
MsiDatabaseOpenViewW(MSIHANDLE hdb
,
241 LPCWSTR szQuery
, MSIHANDLE
*phView
)
244 MSIQUERY
*query
= NULL
;
247 TRACE("%s %p\n", debugstr_w(szQuery
), phView
);
249 db
= msihandle2msiinfo( hdb
, MSIHANDLETYPE_DATABASE
);
251 return ERROR_INVALID_HANDLE
;
253 ret
= MSI_DatabaseOpenViewW( db
, szQuery
, &query
);
254 if( ret
== ERROR_SUCCESS
)
256 *phView
= alloc_msihandle( &query
->hdr
);
258 ret
= ERROR_NOT_ENOUGH_MEMORY
;
259 msiobj_release( &query
->hdr
);
261 msiobj_release( &db
->hdr
);
266 UINT
MSI_ViewFetch(MSIQUERY
*query
, MSIRECORD
**prec
)
270 UINT row_count
= 0, col_count
= 0, i
, ival
, ret
, type
;
272 TRACE("%p %p\n", query
, prec
);
276 return ERROR_FUNCTION_FAILED
;
278 ret
= view
->ops
->get_dimensions( view
, &row_count
, &col_count
);
282 return ERROR_INVALID_PARAMETER
;
284 if( query
->row
>= row_count
)
285 return ERROR_NO_MORE_ITEMS
;
287 rec
= MSI_CreateRecord( col_count
);
289 return ERROR_FUNCTION_FAILED
;
291 for( i
=1; i
<=col_count
; i
++ )
293 ret
= view
->ops
->get_column_info( view
, i
, NULL
, &type
);
296 ERR("Error getting column type for %d\n", i
);
299 if (!MSITYPE_IS_BINARY(type
))
301 ret
= view
->ops
->fetch_int( view
, query
->row
, i
, &ival
);
304 ERR("Error fetching data for %d\n", i
);
307 if( ! (type
& MSITYPE_VALID
) )
308 ERR("Invalid type!\n");
310 /* check if it's nul (0) - if so, don't set anything */
314 if( type
& MSITYPE_STRING
)
318 sval
= MSI_makestring( query
->db
, ival
);
319 MSI_RecordSetStringW( rec
, i
, sval
);
324 if( (type
& MSI_DATASIZEMASK
) == 2 )
325 MSI_RecordSetInteger( rec
, i
, ival
- (1<<15) );
327 MSI_RecordSetInteger( rec
, i
, ival
- (1<<31) );
334 ret
= view
->ops
->fetch_stream( view
, query
->row
, i
, &stm
);
335 if( ( ret
== ERROR_SUCCESS
) && stm
)
337 MSI_RecordSetIStream( rec
, i
, stm
);
338 IStream_Release( stm
);
341 ERR("failed to get stream\n");
348 return ERROR_SUCCESS
;
351 UINT WINAPI
MsiViewFetch(MSIHANDLE hView
, MSIHANDLE
*record
)
354 MSIRECORD
*rec
= NULL
;
357 TRACE("%ld %p\n", hView
, record
);
360 return ERROR_INVALID_PARAMETER
;
363 query
= msihandle2msiinfo( hView
, MSIHANDLETYPE_VIEW
);
365 return ERROR_INVALID_HANDLE
;
366 ret
= MSI_ViewFetch( query
, &rec
);
367 if( ret
== ERROR_SUCCESS
)
369 *record
= alloc_msihandle( &rec
->hdr
);
371 ret
= ERROR_NOT_ENOUGH_MEMORY
;
372 msiobj_release( &rec
->hdr
);
374 msiobj_release( &query
->hdr
);
378 UINT
MSI_ViewClose(MSIQUERY
*query
)
382 TRACE("%p\n", query
);
386 return ERROR_FUNCTION_FAILED
;
387 if( !view
->ops
->close
)
388 return ERROR_FUNCTION_FAILED
;
390 return view
->ops
->close( view
);
393 UINT WINAPI
MsiViewClose(MSIHANDLE hView
)
398 TRACE("%ld\n", hView
);
400 query
= msihandle2msiinfo( hView
, MSIHANDLETYPE_VIEW
);
402 return ERROR_INVALID_HANDLE
;
404 ret
= MSI_ViewClose( query
);
405 msiobj_release( &query
->hdr
);
409 UINT
MSI_ViewExecute(MSIQUERY
*query
, MSIRECORD
*rec
)
413 TRACE("%p %p\n", query
, rec
);
417 return ERROR_FUNCTION_FAILED
;
418 if( !view
->ops
->execute
)
419 return ERROR_FUNCTION_FAILED
;
422 return view
->ops
->execute( view
, rec
);
425 UINT WINAPI
MsiViewExecute(MSIHANDLE hView
, MSIHANDLE hRec
)
428 MSIRECORD
*rec
= NULL
;
431 TRACE("%ld %ld\n", hView
, hRec
);
433 query
= msihandle2msiinfo( hView
, MSIHANDLETYPE_VIEW
);
435 return ERROR_INVALID_HANDLE
;
439 rec
= msihandle2msiinfo( hRec
, MSIHANDLETYPE_RECORD
);
442 ret
= ERROR_INVALID_HANDLE
;
447 msiobj_lock( &rec
->hdr
);
448 ret
= MSI_ViewExecute( query
, rec
);
449 msiobj_unlock( &rec
->hdr
);
452 msiobj_release( &query
->hdr
);
454 msiobj_release( &rec
->hdr
);
459 static UINT
msi_set_record_type_string( MSIRECORD
*rec
, UINT field
, UINT type
)
461 static const WCHAR fmt
[] = { '%','d',0 };
464 if (MSITYPE_IS_BINARY(type
))
466 else if (type
& MSITYPE_LOCALIZABLE
)
468 else if (type
& MSITYPE_STRING
)
472 if (type
& MSITYPE_NULLABLE
)
475 sprintfW( &szType
[1], fmt
, (type
&0xff) );
477 TRACE("type %04x -> %s\n", type
, debugstr_w(szType
) );
479 return MSI_RecordSetStringW( rec
, field
, szType
);
482 UINT
MSI_ViewGetColumnInfo( MSIQUERY
*query
, MSICOLINFO info
, MSIRECORD
**prec
)
484 UINT r
= ERROR_FUNCTION_FAILED
, i
, count
= 0, type
;
486 MSIVIEW
*view
= query
->view
;
490 return ERROR_FUNCTION_FAILED
;
492 if( !view
->ops
->get_dimensions
)
493 return ERROR_FUNCTION_FAILED
;
495 r
= view
->ops
->get_dimensions( view
, NULL
, &count
);
496 if( r
!= ERROR_SUCCESS
)
499 return ERROR_INVALID_PARAMETER
;
501 rec
= MSI_CreateRecord( count
);
503 return ERROR_FUNCTION_FAILED
;
505 for( i
=0; i
<count
; i
++ )
508 r
= view
->ops
->get_column_info( view
, i
+1, &name
, &type
);
509 if( r
!= ERROR_SUCCESS
)
511 if (info
== MSICOLINFO_NAMES
)
512 MSI_RecordSetStringW( rec
, i
+1, name
);
514 msi_set_record_type_string( rec
, i
+1, type
);
519 return ERROR_SUCCESS
;
522 UINT WINAPI
MsiViewGetColumnInfo(MSIHANDLE hView
, MSICOLINFO info
, MSIHANDLE
*hRec
)
524 MSIQUERY
*query
= NULL
;
525 MSIRECORD
*rec
= NULL
;
528 TRACE("%ld %d %p\n", hView
, info
, hRec
);
531 return ERROR_INVALID_PARAMETER
;
533 if( info
!= MSICOLINFO_NAMES
&& info
!= MSICOLINFO_TYPES
)
534 return ERROR_INVALID_PARAMETER
;
536 query
= msihandle2msiinfo( hView
, MSIHANDLETYPE_VIEW
);
538 return ERROR_INVALID_HANDLE
;
540 r
= MSI_ViewGetColumnInfo( query
, info
, &rec
);
541 if ( r
== ERROR_SUCCESS
)
543 *hRec
= alloc_msihandle( &rec
->hdr
);
545 r
= ERROR_NOT_ENOUGH_MEMORY
;
546 msiobj_release( &rec
->hdr
);
549 msiobj_release( &query
->hdr
);
554 UINT WINAPI
MsiViewModify( MSIHANDLE hView
, MSIMODIFY eModifyMode
,
557 MSIVIEW
*view
= NULL
;
558 MSIQUERY
*query
= NULL
;
559 MSIRECORD
*rec
= NULL
;
560 UINT r
= ERROR_FUNCTION_FAILED
;
562 TRACE("%ld %x %ld\n", hView
, eModifyMode
, hRecord
);
564 query
= msihandle2msiinfo( hView
, MSIHANDLETYPE_VIEW
);
566 return ERROR_INVALID_HANDLE
;
572 if( !view
->ops
->modify
)
575 rec
= msihandle2msiinfo( hRecord
, MSIHANDLETYPE_RECORD
);
578 r
= ERROR_INVALID_HANDLE
;
582 r
= view
->ops
->modify( view
, eModifyMode
, rec
);
585 msiobj_release( &query
->hdr
);
587 msiobj_release( &rec
->hdr
);
592 MSIDBERROR WINAPI
MsiViewGetErrorW( MSIHANDLE handle
, LPWSTR szColumnNameBuffer
,
595 MSIQUERY
*query
= NULL
;
596 static const WCHAR szError
[] = { 0 };
597 MSIDBERROR r
= MSIDBERROR_NOERROR
;
600 FIXME("%ld %p %p - returns empty error string\n",
601 handle
, szColumnNameBuffer
, pcchBuf
);
604 return MSIDBERROR_INVALIDARG
;
606 query
= msihandle2msiinfo( handle
, MSIHANDLETYPE_VIEW
);
608 return MSIDBERROR_INVALIDARG
;
610 len
= lstrlenW( szError
);
611 if( szColumnNameBuffer
)
614 lstrcpyW( szColumnNameBuffer
, szError
);
616 r
= MSIDBERROR_MOREDATA
;
620 msiobj_release( &query
->hdr
);
624 MSIDBERROR WINAPI
MsiViewGetErrorA( MSIHANDLE handle
, LPSTR szColumnNameBuffer
,
627 static const CHAR szError
[] = { 0 };
628 MSIQUERY
*query
= NULL
;
629 MSIDBERROR r
= MSIDBERROR_NOERROR
;
632 FIXME("%ld %p %p - returns empty error string\n",
633 handle
, szColumnNameBuffer
, pcchBuf
);
636 return MSIDBERROR_INVALIDARG
;
638 query
= msihandle2msiinfo( handle
, MSIHANDLETYPE_VIEW
);
640 return MSIDBERROR_INVALIDARG
;
642 len
= lstrlenA( szError
);
643 if( szColumnNameBuffer
)
646 lstrcpyA( szColumnNameBuffer
, szError
);
648 r
= MSIDBERROR_MOREDATA
;
652 msiobj_release( &query
->hdr
);
656 MSIHANDLE WINAPI
MsiGetLastErrorRecord( void )
662 DEFINE_GUID( CLSID_MsiTransform
, 0x000c1082, 0x0000, 0x0000, 0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46);
664 UINT
MSI_DatabaseApplyTransformW( MSIDATABASE
*db
,
665 LPCWSTR szTransformFile
, int iErrorCond
)
668 IStorage
*stg
= NULL
;
670 TRACE("%p %s %d\n", db
, debugstr_w(szTransformFile
), iErrorCond
);
672 r
= StgOpenStorage( szTransformFile
, NULL
,
673 STGM_DIRECT
|STGM_READ
|STGM_SHARE_DENY_WRITE
, NULL
, 0, &stg
);
677 if( TRACE_ON( msi
) )
678 enum_stream_names( stg
);
680 r
= msi_table_apply_transform( db
, stg
);
682 IStorage_Release( stg
);
687 UINT WINAPI
MsiDatabaseApplyTransformW( MSIHANDLE hdb
,
688 LPCWSTR szTransformFile
, int iErrorCond
)
693 db
= msihandle2msiinfo( hdb
, MSIHANDLETYPE_DATABASE
);
695 return ERROR_INVALID_HANDLE
;
697 r
= MSI_DatabaseApplyTransformW( db
, szTransformFile
, iErrorCond
);
698 msiobj_release( &db
->hdr
);
702 UINT WINAPI
MsiDatabaseApplyTransformA( MSIHANDLE hdb
,
703 LPCSTR szTransformFile
, int iErrorCond
)
708 TRACE("%ld %s %d\n", hdb
, debugstr_a(szTransformFile
), iErrorCond
);
710 wstr
= strdupAtoW( szTransformFile
);
711 if( szTransformFile
&& !wstr
)
712 return ERROR_NOT_ENOUGH_MEMORY
;
714 ret
= MsiDatabaseApplyTransformW( hdb
, wstr
, iErrorCond
);
721 UINT WINAPI
MsiDatabaseGenerateTransformA( MSIHANDLE hdb
, MSIHANDLE hdbref
,
722 LPCSTR szTransformFile
, int iReserved1
, int iReserved2
)
724 FIXME("%ld %ld %s %d %d\n", hdb
, hdbref
,
725 debugstr_a(szTransformFile
), iReserved1
, iReserved2
);
726 return ERROR_CALL_NOT_IMPLEMENTED
;
729 UINT WINAPI
MsiDatabaseGenerateTransformW( MSIHANDLE hdb
, MSIHANDLE hdbref
,
730 LPCWSTR szTransformFile
, int iReserved1
, int iReserved2
)
732 FIXME("%ld %ld %s %d %d\n", hdb
, hdbref
,
733 debugstr_w(szTransformFile
), iReserved1
, iReserved2
);
734 return ERROR_CALL_NOT_IMPLEMENTED
;
737 UINT WINAPI
MsiDatabaseCommit( MSIHANDLE hdb
)
744 db
= msihandle2msiinfo( hdb
, MSIHANDLETYPE_DATABASE
);
746 return ERROR_INVALID_HANDLE
;
748 /* FIXME: lock the database */
750 r
= MSI_CommitTables( db
);
752 /* FIXME: unlock the database */
754 msiobj_release( &db
->hdr
);
759 struct msi_primary_key_record_info
765 static UINT
msi_primary_key_iterator( MSIRECORD
*rec
, LPVOID param
)
767 struct msi_primary_key_record_info
*info
= param
;
771 type
= MSI_RecordGetInteger( rec
, 4 );
772 if( type
& MSITYPE_KEY
)
777 name
= MSI_RecordGetString( rec
, 3 );
778 MSI_RecordSetStringW( info
->rec
, info
->n
, name
);
782 return ERROR_SUCCESS
;
785 UINT
MSI_DatabaseGetPrimaryKeys( MSIDATABASE
*db
,
786 LPCWSTR table
, MSIRECORD
**prec
)
788 static const WCHAR sql
[] = {
789 's','e','l','e','c','t',' ','*',' ',
790 'f','r','o','m',' ','`','_','C','o','l','u','m','n','s','`',' ',
791 'w','h','e','r','e',' ',
792 '`','T','a','b','l','e','`',' ','=',' ','\'','%','s','\'',0 };
793 struct msi_primary_key_record_info info
;
794 MSIQUERY
*query
= NULL
;
798 r
= MSI_OpenQuery( db
, &query
, sql
, table
);
799 if( r
!= ERROR_SUCCESS
)
804 /* count the number of primary key records */
807 r
= MSI_IterateRecords( query
, 0, msi_primary_key_iterator
, &info
);
808 if( r
== ERROR_SUCCESS
)
810 TRACE("Found %ld primary keys\n", info
.n
);
812 /* allocate a record and fill in the names of the tables */
813 info
.rec
= MSI_CreateRecord( info
.n
);
815 r
= MSI_IterateRecords( query
, 0, msi_primary_key_iterator
, &info
);
816 if( r
== ERROR_SUCCESS
)
819 msiobj_release( &info
.rec
->hdr
);
821 msiobj_release( &query
->hdr
);
826 UINT WINAPI
MsiDatabaseGetPrimaryKeysW( MSIHANDLE hdb
,
827 LPCWSTR table
, MSIHANDLE
* phRec
)
829 MSIRECORD
*rec
= NULL
;
833 TRACE("%ld %s %p\n", hdb
, debugstr_w(table
), phRec
);
835 db
= msihandle2msiinfo( hdb
, MSIHANDLETYPE_DATABASE
);
837 return ERROR_INVALID_HANDLE
;
839 r
= MSI_DatabaseGetPrimaryKeys( db
, table
, &rec
);
840 if( r
== ERROR_SUCCESS
)
842 *phRec
= alloc_msihandle( &rec
->hdr
);
844 r
= ERROR_NOT_ENOUGH_MEMORY
;
845 msiobj_release( &rec
->hdr
);
847 msiobj_release( &db
->hdr
);
852 UINT WINAPI
MsiDatabaseGetPrimaryKeysA(MSIHANDLE hdb
,
853 LPCSTR table
, MSIHANDLE
* phRec
)
855 LPWSTR szwTable
= NULL
;
858 TRACE("%ld %s %p\n", hdb
, debugstr_a(table
), phRec
);
862 szwTable
= strdupAtoW( table
);
864 return ERROR_OUTOFMEMORY
;
866 r
= MsiDatabaseGetPrimaryKeysW( hdb
, szwTable
, phRec
);
867 msi_free( szwTable
);
872 MSICONDITION WINAPI
MsiDatabaseIsTablePersistentA(
873 MSIHANDLE hDatabase
, LPCSTR szTableName
)
875 LPWSTR szwTableName
= NULL
;
878 TRACE("%lx %s\n", hDatabase
, debugstr_a(szTableName
));
882 szwTableName
= strdupAtoW( szTableName
);
884 return MSICONDITION_ERROR
;
886 r
= MsiDatabaseIsTablePersistentW( hDatabase
, szwTableName
);
887 msi_free( szwTableName
);
892 MSICONDITION WINAPI
MsiDatabaseIsTablePersistentW(
893 MSIHANDLE hDatabase
, LPCWSTR szTableName
)
895 FIXME("%lx %s\n", hDatabase
, debugstr_w(szTableName
));
896 return MSICONDITION_FALSE
;