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 );
427 msiobj_release( &query->hdr );
429 msiobj_release( &rec->hdr );
434 UINT WINAPI MsiViewGetColumnInfo(MSIHANDLE hView, MSICOLINFO info, MSIHANDLE *hRec)
436 MSIVIEW *view = NULL;
437 MSIQUERY *query = NULL;
438 MSIRECORD *rec = NULL;
439 UINT r = ERROR_FUNCTION_FAILED, i, count = 0, type;
442 TRACE("%ld %d %p\n", hView, info, hRec);
444 query = msihandle2msiinfo( hView, MSIHANDLETYPE_VIEW );
446 return ERROR_INVALID_HANDLE;
452 if( !view->ops->get_dimensions )
455 r = view->ops->get_dimensions( view, NULL, &count );
460 r = ERROR_INVALID_PARAMETER;
464 rec = MSI_CreateRecord( count );
467 r = ERROR_FUNCTION_FAILED;
471 for( i=0; i<count; i++ )
474 r = view->ops->get_column_info( view, i+1, &name, &type );
475 if( r != ERROR_SUCCESS )
477 MSI_RecordSetStringW( rec, i+1, name );
478 HeapFree( GetProcessHeap(), 0, name );
481 *hRec = alloc_msihandle( &rec->hdr );
485 msiobj_release( &query->hdr );
487 msiobj_release( &rec->hdr );
492 UINT WINAPI MsiViewModify( MSIHANDLE hView, MSIMODIFY eModifyMode,
495 MSIVIEW *view = NULL;
496 MSIQUERY *query = NULL;
497 MSIRECORD *rec = NULL;
498 UINT r = ERROR_FUNCTION_FAILED;
500 TRACE("%ld %x %ld\n", hView, eModifyMode, hRecord);
502 query = msihandle2msiinfo( hView, MSIHANDLETYPE_VIEW );
504 return ERROR_INVALID_HANDLE;
510 if( !view->ops->modify )
513 rec = msihandle2msiinfo( hRecord, MSIHANDLETYPE_RECORD );
516 r = ERROR_INVALID_HANDLE;
520 r = view->ops->modify( view, eModifyMode, rec );
524 msiobj_release( &query->hdr );
526 msiobj_release( &rec->hdr );
531 UINT WINAPI MsiDatabaseApplyTransformA( MSIHANDLE hdb,
532 LPCSTR szTransformFile, int iErrorCond)
534 FIXME("%ld %s %d\n", hdb, debugstr_a(szTransformFile), iErrorCond);
535 return ERROR_CALL_NOT_IMPLEMENTED;
538 UINT WINAPI MsiDatabaseApplyTransformW( MSIHANDLE hdb,
539 LPCWSTR szTransformFile, int iErrorCond)
541 FIXME("%ld %s %d\n", hdb, debugstr_w(szTransformFile), iErrorCond);
542 return ERROR_CALL_NOT_IMPLEMENTED;
545 UINT WINAPI MsiDatabaseGenerateTransformA( MSIHANDLE hdb, MSIHANDLE hdbref,
546 LPCSTR szTransformFile, int iReserved1, int iReserved2 )
548 FIXME("%ld %ld %s %d %d\n", hdb, hdbref,
549 debugstr_a(szTransformFile), iReserved1, iReserved2);
550 return ERROR_CALL_NOT_IMPLEMENTED;
553 UINT WINAPI MsiDatabaseGenerateTransformW( MSIHANDLE hdb, MSIHANDLE hdbref,
554 LPCWSTR szTransformFile, int iReserved1, int iReserved2 )
556 FIXME("%ld %ld %s %d %d\n", hdb, hdbref,
557 debugstr_w(szTransformFile), iReserved1, iReserved2);
558 return ERROR_CALL_NOT_IMPLEMENTED;
561 UINT WINAPI MsiDatabaseCommit( MSIHANDLE hdb )
568 db = msihandle2msiinfo( hdb, MSIHANDLETYPE_DATABASE );
570 return ERROR_INVALID_HANDLE;
572 /* FIXME: lock the database */
574 r = MSI_CommitTables( db );
576 /* FIXME: unlock the database */
578 msiobj_release( &db->hdr );
583 struct msi_primary_key_record_info
589 static UINT msi_primary_key_iterator( MSIRECORD *rec, LPVOID param )
591 struct msi_primary_key_record_info *info = param;
595 type = MSI_RecordGetInteger( rec, 4 );
596 if( type & MSITYPE_KEY )
601 name = MSI_RecordGetString( rec, 3 );
602 MSI_RecordSetStringW( info->rec, info->n, name );
606 return ERROR_SUCCESS;
609 UINT MSI_DatabaseGetPrimaryKeys( MSIDATABASE *db,
610 LPCWSTR table, MSIRECORD **prec )
612 static const WCHAR sql[] = {
613 's','e','l','e','c','t',' ','*',' ',
614 'f','r','o','m',' ','`','_','C','o','l','u','m','n','s','`',' ',
615 'w','h','e','r','e',' ',
616 '`','T','a','b','l','e','`',' ','=',' ','\'','%','s','\'',0 };
617 struct msi_primary_key_record_info info;
618 MSIQUERY *query = NULL;
622 r = MSI_OpenQuery( db, &query, sql, table );
623 if( r != ERROR_SUCCESS )
628 /* count the number of primary key records */
631 r = MSI_IterateRecords( query, 0, msi_primary_key_iterator, &info );
632 if( r == ERROR_SUCCESS )
634 TRACE("Found %ld primary keys\n", info.n );
636 /* allocate a record and fill in the names of the tables */
637 info.rec = MSI_CreateRecord( info.n );
639 r = MSI_IterateRecords( query, 0, msi_primary_key_iterator, &info );
640 if( r == ERROR_SUCCESS )
643 msiobj_release( &info.rec->hdr );
645 msiobj_release( &query->hdr );
650 UINT WINAPI MsiDatabaseGetPrimaryKeysW( MSIHANDLE hdb,
651 LPCWSTR table, MSIHANDLE* phRec )
653 MSIRECORD *rec = NULL;
657 TRACE("%ld %s %p\n", hdb, debugstr_w(table), phRec);
659 db = msihandle2msiinfo( hdb, MSIHANDLETYPE_DATABASE );
661 return ERROR_INVALID_HANDLE;
663 r = MSI_DatabaseGetPrimaryKeys( db, table, &rec );
664 if( r == ERROR_SUCCESS )
666 *phRec = alloc_msihandle( &rec->hdr );
667 msiobj_release( &rec->hdr );
669 msiobj_release( &db->hdr );
674 UINT WINAPI MsiDatabaseGetPrimaryKeysA(MSIHANDLE hdb,
675 LPCSTR table, MSIHANDLE* phRec)
677 LPWSTR szwTable = NULL;
681 TRACE("%ld %s %p\n", hdb, debugstr_a(table), phRec);
685 len = MultiByteToWideChar( CP_ACP, 0, table, -1, NULL, 0 );
686 szwTable = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
687 MultiByteToWideChar( CP_ACP, 0, table, -1, szwTable, len );
689 r = MsiDatabaseGetPrimaryKeysW( hdb, szwTable, phRec );
690 HeapFree( GetProcessHeap(), 0, szwTable );
695 UINT WINAPI MsiDatabaseIsTablePersistentA(
696 MSIHANDLE hDatabase, LPSTR szTableName)
698 FIXME("%lx %s\n", hDatabase, debugstr_a(szTableName));
699 return ERROR_CALL_NOT_IMPLEMENTED;
702 UINT WINAPI MsiDatabaseIsTablePersistentW(
703 MSIHANDLE hDatabase, LPWSTR szTableName)
705 FIXME("%lx %s\n", hDatabase, debugstr_w(szTableName));
706 return ERROR_CALL_NOT_IMPLEMENTED;