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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "wine/debug.h"
29 #include "wine/unicode.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(msi);
41 void MSI_CloseView( MSIOBJECTHDR *arg )
43 MSIQUERY *query = (MSIQUERY*) arg;
45 if( query->view && query->view->ops->delete )
46 query->view->ops->delete( query->view );
47 msiobj_release( &query->db->hdr );
50 UINT VIEW_find_column( MSIVIEW *table, LPWSTR name, UINT *n )
55 r = table->ops->get_dimensions( table, NULL, &count );
56 if( r != ERROR_SUCCESS )
59 for( i=1; i<=count; i++ )
64 r = table->ops->get_column_info( table, i, &col_name, NULL );
65 if( r != ERROR_SUCCESS )
67 x = lstrcmpW( name, col_name );
68 HeapFree( GetProcessHeap(), 0, col_name );
76 return ERROR_INVALID_PARAMETER;
79 UINT WINAPI MsiDatabaseOpenViewA(MSIHANDLE hdb,
80 LPCSTR szQuery, MSIHANDLE *phView)
85 TRACE("%ld %s %p\n", hdb, debugstr_a(szQuery), phView);
89 UINT len = MultiByteToWideChar( CP_ACP, 0, szQuery, -1, NULL, 0 );
90 szwQuery = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
92 return ERROR_FUNCTION_FAILED;
93 MultiByteToWideChar( CP_ACP, 0, szQuery, -1, szwQuery, len );
98 r = MsiDatabaseOpenViewW( hdb, szwQuery, phView);
100 HeapFree( GetProcessHeap(), 0, szwQuery );
104 UINT MSI_DatabaseOpenViewW(MSIDATABASE *db,
105 LPCWSTR szQuery, MSIQUERY **pView)
110 TRACE("%s %p\n", debugstr_w(szQuery), pView);
113 return ERROR_INVALID_PARAMETER;
115 /* pre allocate a handle to hold a pointer to the view */
116 query = alloc_msiobject( MSIHANDLETYPE_VIEW, sizeof (MSIQUERY),
119 return ERROR_FUNCTION_FAILED;
121 msiobj_addref( &db->hdr );
126 r = MSI_ParseSQL( db, szQuery, &query->view );
127 if( r == ERROR_SUCCESS )
129 msiobj_addref( &query->hdr );
133 msiobj_release( &query->hdr );
137 UINT MSI_OpenQuery( MSIDATABASE *db, MSIQUERY **view, LPCWSTR fmt, ... )
144 /* figure out how much space we need to allocate */
146 sz = strlenW(fmt) + 1;
156 case 's': /* a string */
157 sz += strlenW(va_arg(va,LPCWSTR));
160 case 'i': /* an integer -2147483648 seems to be longest */
162 (void)va_arg(va,int);
164 case '%': /* a single % - leave it alone */
167 FIXME("Unhandled character type %c\n",*p);
173 /* construct the string */
174 szQuery = HeapAlloc(GetProcessHeap(), 0, sz*sizeof(WCHAR));
176 vsnprintfW(szQuery, sz, fmt, va);
179 /* perform the query */
180 rc = MSI_DatabaseOpenViewW(db, szQuery, view);
181 HeapFree(GetProcessHeap(), 0, szQuery);
185 UINT MSI_IterateRecords( MSIQUERY *view, DWORD *count,
186 record_func func, LPVOID param )
188 MSIRECORD *rec = NULL;
189 UINT r, n = 0, max = 0;
191 r = MSI_ViewExecute( view, NULL );
192 if( r != ERROR_SUCCESS )
198 /* iterate a query */
199 for( n = 0; (max == 0) || (n < max); n++ )
201 r = MSI_ViewFetch( view, &rec );
202 if( r != ERROR_SUCCESS )
204 r = func( rec, param );
205 msiobj_release( &rec->hdr );
206 if( r != ERROR_SUCCESS )
210 MSI_ViewClose( view );
215 if( r == ERROR_NO_MORE_ITEMS )
221 UINT WINAPI MsiDatabaseOpenViewW(MSIHANDLE hdb,
222 LPCWSTR szQuery, MSIHANDLE *phView)
225 MSIQUERY *query = NULL;
228 TRACE("%s %p\n", debugstr_w(szQuery), phView);
230 db = msihandle2msiinfo( hdb, MSIHANDLETYPE_DATABASE );
232 return ERROR_INVALID_HANDLE;
234 ret = MSI_DatabaseOpenViewW( db, szQuery, &query );
235 if( ret == ERROR_SUCCESS )
237 *phView = alloc_msihandle( &query->hdr );
238 msiobj_release( &query->hdr );
240 msiobj_release( &db->hdr );
245 UINT MSI_ViewFetch(MSIQUERY *query, MSIRECORD **prec)
249 UINT row_count = 0, col_count = 0, i, ival, ret, type;
251 TRACE("%p %p\n", query, prec );
255 return ERROR_FUNCTION_FAILED;
257 ret = view->ops->get_dimensions( view, &row_count, &col_count );
261 return ERROR_INVALID_PARAMETER;
263 if( query->row >= row_count )
264 return ERROR_NO_MORE_ITEMS;
266 rec = MSI_CreateRecord( col_count );
268 return ERROR_FUNCTION_FAILED;
270 for( i=1; i<=col_count; i++ )
272 ret = view->ops->get_column_info( view, i, NULL, &type );
275 ERR("Error getting column type for %d\n", i );
278 if (( type != MSITYPE_BINARY) && (type != (MSITYPE_BINARY |
281 ret = view->ops->fetch_int( view, query->row, i, &ival );
284 ERR("Error fetching data for %d\n", i );
287 if( ! (type & MSITYPE_VALID ) )
288 ERR("Invalid type!\n");
290 /* check if it's nul (0) - if so, don't set anything */
294 if( type & MSITYPE_STRING )
298 sval = MSI_makestring( query->db, ival );
299 MSI_RecordSetStringW( rec, i, sval );
300 HeapFree( GetProcessHeap(), 0, sval );
304 if( (type & MSI_DATASIZEMASK) == 2 )
305 MSI_RecordSetInteger( rec, i, ival - (1<<15) );
307 MSI_RecordSetInteger( rec, i, ival - (1<<31) );
314 ret = view->ops->fetch_stream( view, query->row, i, &stm );
315 if( ( ret == ERROR_SUCCESS ) && stm )
317 MSI_RecordSetIStream( rec, i, stm );
318 IStream_Release( stm );
321 ERR("failed to get stream\n");
328 return ERROR_SUCCESS;
331 UINT WINAPI MsiViewFetch(MSIHANDLE hView, MSIHANDLE *record)
334 MSIRECORD *rec = NULL;
337 TRACE("%ld %p\n", hView, record);
339 query = msihandle2msiinfo( hView, MSIHANDLETYPE_VIEW );
341 return ERROR_INVALID_HANDLE;
342 ret = MSI_ViewFetch( query, &rec );
343 if( ret == ERROR_SUCCESS )
345 *record = alloc_msihandle( &rec->hdr );
346 msiobj_release( &rec->hdr );
348 msiobj_release( &query->hdr );
352 UINT MSI_ViewClose(MSIQUERY *query)
356 TRACE("%p\n", query );
360 return ERROR_FUNCTION_FAILED;
361 if( !view->ops->close )
362 return ERROR_FUNCTION_FAILED;
364 return view->ops->close( view );
367 UINT WINAPI MsiViewClose(MSIHANDLE hView)
372 TRACE("%ld\n", hView );
374 query = msihandle2msiinfo( hView, MSIHANDLETYPE_VIEW );
376 return ERROR_INVALID_HANDLE;
378 ret = MSI_ViewClose( query );
379 msiobj_release( &query->hdr );
383 UINT MSI_ViewExecute(MSIQUERY *query, MSIRECORD *rec )
387 TRACE("%p %p\n", query, rec);
391 return ERROR_FUNCTION_FAILED;
392 if( !view->ops->execute )
393 return ERROR_FUNCTION_FAILED;
396 return view->ops->execute( view, rec );
399 UINT WINAPI MsiViewExecute(MSIHANDLE hView, MSIHANDLE hRec)
402 MSIRECORD *rec = NULL;
405 TRACE("%ld %ld\n", hView, hRec);
407 query = msihandle2msiinfo( hView, MSIHANDLETYPE_VIEW );
409 return ERROR_INVALID_HANDLE;
413 rec = msihandle2msiinfo( hRec, MSIHANDLETYPE_RECORD );
416 ret = ERROR_INVALID_HANDLE;
421 msiobj_lock( &rec->hdr );
422 ret = MSI_ViewExecute( query, rec );
423 msiobj_unlock( &rec->hdr );
426 msiobj_release( &query->hdr );
428 msiobj_release( &rec->hdr );
433 UINT WINAPI MsiViewGetColumnInfo(MSIHANDLE hView, MSICOLINFO info, MSIHANDLE *hRec)
435 MSIVIEW *view = NULL;
436 MSIQUERY *query = NULL;
437 MSIRECORD *rec = NULL;
438 UINT r = ERROR_FUNCTION_FAILED, i, count = 0, type;
441 TRACE("%ld %d %p\n", hView, info, hRec);
443 query = msihandle2msiinfo( hView, MSIHANDLETYPE_VIEW );
445 return ERROR_INVALID_HANDLE;
451 if( !view->ops->get_dimensions )
454 r = view->ops->get_dimensions( view, NULL, &count );
459 r = ERROR_INVALID_PARAMETER;
463 rec = MSI_CreateRecord( count );
466 r = ERROR_FUNCTION_FAILED;
470 for( i=0; i<count; i++ )
473 r = view->ops->get_column_info( view, i+1, &name, &type );
474 if( r != ERROR_SUCCESS )
476 MSI_RecordSetStringW( rec, i+1, name );
477 HeapFree( GetProcessHeap(), 0, name );
480 *hRec = alloc_msihandle( &rec->hdr );
483 msiobj_release( &query->hdr );
485 msiobj_release( &rec->hdr );
490 UINT WINAPI MsiViewModify( MSIHANDLE hView, MSIMODIFY eModifyMode,
493 MSIVIEW *view = NULL;
494 MSIQUERY *query = NULL;
495 MSIRECORD *rec = NULL;
496 UINT r = ERROR_FUNCTION_FAILED;
498 TRACE("%ld %x %ld\n", hView, eModifyMode, hRecord);
500 query = msihandle2msiinfo( hView, MSIHANDLETYPE_VIEW );
502 return ERROR_INVALID_HANDLE;
508 if( !view->ops->modify )
511 rec = msihandle2msiinfo( hRecord, MSIHANDLETYPE_RECORD );
514 r = ERROR_INVALID_HANDLE;
518 r = view->ops->modify( view, eModifyMode, rec );
521 msiobj_release( &query->hdr );
523 msiobj_release( &rec->hdr );
528 UINT WINAPI MsiDatabaseApplyTransformA( MSIHANDLE hdb,
529 LPCSTR szTransformFile, int iErrorCond)
531 FIXME("%ld %s %d\n", hdb, debugstr_a(szTransformFile), iErrorCond);
532 return ERROR_CALL_NOT_IMPLEMENTED;
535 UINT WINAPI MsiDatabaseApplyTransformW( MSIHANDLE hdb,
536 LPCWSTR szTransformFile, int iErrorCond)
538 FIXME("%ld %s %d\n", hdb, debugstr_w(szTransformFile), iErrorCond);
539 return ERROR_CALL_NOT_IMPLEMENTED;
542 UINT WINAPI MsiDatabaseGenerateTransformA( MSIHANDLE hdb, MSIHANDLE hdbref,
543 LPCSTR szTransformFile, int iReserved1, int iReserved2 )
545 FIXME("%ld %ld %s %d %d\n", hdb, hdbref,
546 debugstr_a(szTransformFile), iReserved1, iReserved2);
547 return ERROR_CALL_NOT_IMPLEMENTED;
550 UINT WINAPI MsiDatabaseGenerateTransformW( MSIHANDLE hdb, MSIHANDLE hdbref,
551 LPCWSTR szTransformFile, int iReserved1, int iReserved2 )
553 FIXME("%ld %ld %s %d %d\n", hdb, hdbref,
554 debugstr_w(szTransformFile), iReserved1, iReserved2);
555 return ERROR_CALL_NOT_IMPLEMENTED;
558 UINT WINAPI MsiDatabaseCommit( MSIHANDLE hdb )
565 db = msihandle2msiinfo( hdb, MSIHANDLETYPE_DATABASE );
567 return ERROR_INVALID_HANDLE;
569 /* FIXME: lock the database */
571 r = MSI_CommitTables( db );
573 /* FIXME: unlock the database */
575 msiobj_release( &db->hdr );
580 struct msi_primary_key_record_info
586 static UINT msi_primary_key_iterator( MSIRECORD *rec, LPVOID param )
588 struct msi_primary_key_record_info *info = param;
592 type = MSI_RecordGetInteger( rec, 4 );
593 if( type & MSITYPE_KEY )
598 name = MSI_RecordGetString( rec, 3 );
599 MSI_RecordSetStringW( info->rec, info->n, name );
603 return ERROR_SUCCESS;
606 UINT MSI_DatabaseGetPrimaryKeys( MSIDATABASE *db,
607 LPCWSTR table, MSIRECORD **prec )
609 static const WCHAR sql[] = {
610 's','e','l','e','c','t',' ','*',' ',
611 'f','r','o','m',' ','`','_','C','o','l','u','m','n','s','`',' ',
612 'w','h','e','r','e',' ',
613 '`','T','a','b','l','e','`',' ','=',' ','\'','%','s','\'',0 };
614 struct msi_primary_key_record_info info;
615 MSIQUERY *query = NULL;
619 r = MSI_OpenQuery( db, &query, sql, table );
620 if( r != ERROR_SUCCESS )
625 /* count the number of primary key records */
628 r = MSI_IterateRecords( query, 0, msi_primary_key_iterator, &info );
629 if( r == ERROR_SUCCESS )
631 TRACE("Found %ld primary keys\n", info.n );
633 /* allocate a record and fill in the names of the tables */
634 info.rec = MSI_CreateRecord( info.n );
636 r = MSI_IterateRecords( query, 0, msi_primary_key_iterator, &info );
637 if( r == ERROR_SUCCESS )
640 msiobj_release( &info.rec->hdr );
642 msiobj_release( &query->hdr );
647 UINT WINAPI MsiDatabaseGetPrimaryKeysW( MSIHANDLE hdb,
648 LPCWSTR table, MSIHANDLE* phRec )
650 MSIRECORD *rec = NULL;
654 TRACE("%ld %s %p\n", hdb, debugstr_w(table), phRec);
656 db = msihandle2msiinfo( hdb, MSIHANDLETYPE_DATABASE );
658 return ERROR_INVALID_HANDLE;
660 r = MSI_DatabaseGetPrimaryKeys( db, table, &rec );
661 if( r == ERROR_SUCCESS )
663 *phRec = alloc_msihandle( &rec->hdr );
664 msiobj_release( &rec->hdr );
666 msiobj_release( &db->hdr );
671 UINT WINAPI MsiDatabaseGetPrimaryKeysA(MSIHANDLE hdb,
672 LPCSTR table, MSIHANDLE* phRec)
674 LPWSTR szwTable = NULL;
678 TRACE("%ld %s %p\n", hdb, debugstr_a(table), phRec);
682 len = MultiByteToWideChar( CP_ACP, 0, table, -1, NULL, 0 );
683 szwTable = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
684 MultiByteToWideChar( CP_ACP, 0, table, -1, szwTable, len );
686 r = MsiDatabaseGetPrimaryKeysW( hdb, szwTable, phRec );
687 HeapFree( GetProcessHeap(), 0, szwTable );
692 UINT WINAPI MsiDatabaseIsTablePersistentA(
693 MSIHANDLE hDatabase, LPSTR szTableName)
695 FIXME("%lx %s\n", hDatabase, debugstr_a(szTableName));
696 return ERROR_CALL_NOT_IMPLEMENTED;
699 UINT WINAPI MsiDatabaseIsTablePersistentW(
700 MSIHANDLE hDatabase, LPWSTR szTableName)
702 FIXME("%lx %s\n", hDatabase, debugstr_w(szTableName));
703 return ERROR_CALL_NOT_IMPLEMENTED;