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;
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 )
52 HeapFree( GetProcessHeap(), 0, ptr );
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 );
74 HeapFree( GetProcessHeap(), 0, 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 HeapFree( GetProcessHeap(), 0, 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, ... )
149 /* figure out how much space we need to allocate */
151 sz = lstrlenW(fmt) + 1;
161 case 's': /* a string */
162 sz += lstrlenW(va_arg(va,LPCWSTR));
165 case 'i': /* an integer -2147483648 seems to be longest */
167 (void)va_arg(va,int);
169 case '%': /* a single % - leave it alone */
172 FIXME("Unhandled character type %c\n",*p);
178 /* construct the string */
179 szQuery = HeapAlloc(GetProcessHeap(), 0, sz*sizeof(WCHAR));
181 vsnprintfW(szQuery, sz, fmt, va);
184 /* perform the query */
185 rc = MSI_DatabaseOpenViewW(db, szQuery, view);
186 HeapFree(GetProcessHeap(), 0, szQuery);
190 UINT MSI_IterateRecords( MSIQUERY *view, DWORD *count,
191 record_func func, LPVOID param )
193 MSIRECORD *rec = NULL;
194 UINT r, n = 0, max = 0;
196 r = MSI_ViewExecute( view, NULL );
197 if( r != ERROR_SUCCESS )
203 /* iterate a query */
204 for( n = 0; (max == 0) || (n < max); n++ )
206 r = MSI_ViewFetch( view, &rec );
207 if( r != ERROR_SUCCESS )
209 r = func( rec, param );
210 msiobj_release( &rec->hdr );
211 if( r != ERROR_SUCCESS )
215 MSI_ViewClose( view );
220 if( r == ERROR_NO_MORE_ITEMS )
226 UINT WINAPI MsiDatabaseOpenViewW(MSIHANDLE hdb,
227 LPCWSTR szQuery, MSIHANDLE *phView)
230 MSIQUERY *query = NULL;
233 TRACE("%s %p\n", debugstr_w(szQuery), phView);
235 db = msihandle2msiinfo( hdb, MSIHANDLETYPE_DATABASE );
237 return ERROR_INVALID_HANDLE;
239 ret = MSI_DatabaseOpenViewW( db, szQuery, &query );
240 if( ret == ERROR_SUCCESS )
242 *phView = alloc_msihandle( &query->hdr );
243 msiobj_release( &query->hdr );
245 msiobj_release( &db->hdr );
250 UINT MSI_ViewFetch(MSIQUERY *query, MSIRECORD **prec)
254 UINT row_count = 0, col_count = 0, i, ival, ret, type;
256 TRACE("%p %p\n", query, prec );
260 return ERROR_FUNCTION_FAILED;
262 ret = view->ops->get_dimensions( view, &row_count, &col_count );
266 return ERROR_INVALID_PARAMETER;
268 if( query->row >= row_count )
269 return ERROR_NO_MORE_ITEMS;
271 rec = MSI_CreateRecord( col_count );
273 return ERROR_FUNCTION_FAILED;
275 for( i=1; i<=col_count; i++ )
277 ret = view->ops->get_column_info( view, i, NULL, &type );
280 ERR("Error getting column type for %d\n", i );
283 if (( type != MSITYPE_BINARY) && (type != (MSITYPE_BINARY |
286 ret = view->ops->fetch_int( view, query->row, i, &ival );
289 ERR("Error fetching data for %d\n", i );
292 if( ! (type & MSITYPE_VALID ) )
293 ERR("Invalid type!\n");
295 /* check if it's nul (0) - if so, don't set anything */
299 if( type & MSITYPE_STRING )
303 sval = MSI_makestring( query->db, ival );
304 MSI_RecordSetStringW( rec, i, sval );
305 HeapFree( GetProcessHeap(), 0, sval );
309 if( (type & MSI_DATASIZEMASK) == 2 )
310 MSI_RecordSetInteger( rec, i, ival - (1<<15) );
312 MSI_RecordSetInteger( rec, i, ival - (1<<31) );
319 ret = view->ops->fetch_stream( view, query->row, i, &stm );
320 if( ( ret == ERROR_SUCCESS ) && stm )
322 MSI_RecordSetIStream( rec, i, stm );
323 IStream_Release( stm );
326 ERR("failed to get stream\n");
333 return ERROR_SUCCESS;
336 UINT WINAPI MsiViewFetch(MSIHANDLE hView, MSIHANDLE *record)
339 MSIRECORD *rec = NULL;
342 TRACE("%ld %p\n", hView, record);
344 query = msihandle2msiinfo( hView, MSIHANDLETYPE_VIEW );
346 return ERROR_INVALID_HANDLE;
347 ret = MSI_ViewFetch( query, &rec );
348 if( ret == ERROR_SUCCESS )
350 *record = alloc_msihandle( &rec->hdr );
351 msiobj_release( &rec->hdr );
353 msiobj_release( &query->hdr );
357 UINT MSI_ViewClose(MSIQUERY *query)
361 TRACE("%p\n", query );
365 return ERROR_FUNCTION_FAILED;
366 if( !view->ops->close )
367 return ERROR_FUNCTION_FAILED;
369 return view->ops->close( view );
372 UINT WINAPI MsiViewClose(MSIHANDLE hView)
377 TRACE("%ld\n", hView );
379 query = msihandle2msiinfo( hView, MSIHANDLETYPE_VIEW );
381 return ERROR_INVALID_HANDLE;
383 ret = MSI_ViewClose( query );
384 msiobj_release( &query->hdr );
388 UINT MSI_ViewExecute(MSIQUERY *query, MSIRECORD *rec )
392 TRACE("%p %p\n", query, rec);
396 return ERROR_FUNCTION_FAILED;
397 if( !view->ops->execute )
398 return ERROR_FUNCTION_FAILED;
401 return view->ops->execute( view, rec );
404 UINT WINAPI MsiViewExecute(MSIHANDLE hView, MSIHANDLE hRec)
407 MSIRECORD *rec = NULL;
410 TRACE("%ld %ld\n", hView, hRec);
412 query = msihandle2msiinfo( hView, MSIHANDLETYPE_VIEW );
414 return ERROR_INVALID_HANDLE;
418 rec = msihandle2msiinfo( hRec, MSIHANDLETYPE_RECORD );
421 ret = ERROR_INVALID_HANDLE;
426 msiobj_lock( &rec->hdr );
427 ret = MSI_ViewExecute( query, rec );
428 msiobj_unlock( &rec->hdr );
431 msiobj_release( &query->hdr );
433 msiobj_release( &rec->hdr );
438 UINT WINAPI MsiViewGetColumnInfo(MSIHANDLE hView, MSICOLINFO info, MSIHANDLE *hRec)
440 MSIVIEW *view = NULL;
441 MSIQUERY *query = NULL;
442 MSIRECORD *rec = NULL;
443 UINT r = ERROR_FUNCTION_FAILED, i, count = 0, type;
446 TRACE("%ld %d %p\n", hView, info, hRec);
448 query = msihandle2msiinfo( hView, MSIHANDLETYPE_VIEW );
450 return ERROR_INVALID_HANDLE;
456 if( !view->ops->get_dimensions )
459 r = view->ops->get_dimensions( view, NULL, &count );
464 r = ERROR_INVALID_PARAMETER;
468 rec = MSI_CreateRecord( count );
471 r = ERROR_FUNCTION_FAILED;
475 for( i=0; i<count; i++ )
478 r = view->ops->get_column_info( view, i+1, &name, &type );
479 if( r != ERROR_SUCCESS )
481 MSI_RecordSetStringW( rec, i+1, name );
482 HeapFree( GetProcessHeap(), 0, name );
485 *hRec = alloc_msihandle( &rec->hdr );
488 msiobj_release( &query->hdr );
490 msiobj_release( &rec->hdr );
495 UINT WINAPI MsiViewModify( MSIHANDLE hView, MSIMODIFY eModifyMode,
498 MSIVIEW *view = NULL;
499 MSIQUERY *query = NULL;
500 MSIRECORD *rec = NULL;
501 UINT r = ERROR_FUNCTION_FAILED;
503 TRACE("%ld %x %ld\n", hView, eModifyMode, hRecord);
505 query = msihandle2msiinfo( hView, MSIHANDLETYPE_VIEW );
507 return ERROR_INVALID_HANDLE;
513 if( !view->ops->modify )
516 rec = msihandle2msiinfo( hRecord, MSIHANDLETYPE_RECORD );
519 r = ERROR_INVALID_HANDLE;
523 r = view->ops->modify( view, eModifyMode, rec );
526 msiobj_release( &query->hdr );
528 msiobj_release( &rec->hdr );
533 UINT WINAPI MsiDatabaseApplyTransformA( MSIHANDLE hdb,
534 LPCSTR szTransformFile, int iErrorCond)
536 FIXME("%ld %s %d\n", hdb, debugstr_a(szTransformFile), iErrorCond);
537 return ERROR_CALL_NOT_IMPLEMENTED;
540 UINT WINAPI MsiDatabaseApplyTransformW( MSIHANDLE hdb,
541 LPCWSTR szTransformFile, int iErrorCond)
543 FIXME("%ld %s %d\n", hdb, debugstr_w(szTransformFile), iErrorCond);
544 return ERROR_CALL_NOT_IMPLEMENTED;
547 UINT WINAPI MsiDatabaseGenerateTransformA( MSIHANDLE hdb, MSIHANDLE hdbref,
548 LPCSTR szTransformFile, int iReserved1, int iReserved2 )
550 FIXME("%ld %ld %s %d %d\n", hdb, hdbref,
551 debugstr_a(szTransformFile), iReserved1, iReserved2);
552 return ERROR_CALL_NOT_IMPLEMENTED;
555 UINT WINAPI MsiDatabaseGenerateTransformW( MSIHANDLE hdb, MSIHANDLE hdbref,
556 LPCWSTR szTransformFile, int iReserved1, int iReserved2 )
558 FIXME("%ld %ld %s %d %d\n", hdb, hdbref,
559 debugstr_w(szTransformFile), iReserved1, iReserved2);
560 return ERROR_CALL_NOT_IMPLEMENTED;
563 UINT WINAPI MsiDatabaseCommit( MSIHANDLE hdb )
570 db = msihandle2msiinfo( hdb, MSIHANDLETYPE_DATABASE );
572 return ERROR_INVALID_HANDLE;
574 /* FIXME: lock the database */
576 r = MSI_CommitTables( db );
578 /* FIXME: unlock the database */
580 msiobj_release( &db->hdr );
585 struct msi_primary_key_record_info
591 static UINT msi_primary_key_iterator( MSIRECORD *rec, LPVOID param )
593 struct msi_primary_key_record_info *info = param;
597 type = MSI_RecordGetInteger( rec, 4 );
598 if( type & MSITYPE_KEY )
603 name = MSI_RecordGetString( rec, 3 );
604 MSI_RecordSetStringW( info->rec, info->n, name );
608 return ERROR_SUCCESS;
611 UINT MSI_DatabaseGetPrimaryKeys( MSIDATABASE *db,
612 LPCWSTR table, MSIRECORD **prec )
614 static const WCHAR sql[] = {
615 's','e','l','e','c','t',' ','*',' ',
616 'f','r','o','m',' ','`','_','C','o','l','u','m','n','s','`',' ',
617 'w','h','e','r','e',' ',
618 '`','T','a','b','l','e','`',' ','=',' ','\'','%','s','\'',0 };
619 struct msi_primary_key_record_info info;
620 MSIQUERY *query = NULL;
624 r = MSI_OpenQuery( db, &query, sql, table );
625 if( r != ERROR_SUCCESS )
630 /* count the number of primary key records */
633 r = MSI_IterateRecords( query, 0, msi_primary_key_iterator, &info );
634 if( r == ERROR_SUCCESS )
636 TRACE("Found %ld primary keys\n", info.n );
638 /* allocate a record and fill in the names of the tables */
639 info.rec = MSI_CreateRecord( info.n );
641 r = MSI_IterateRecords( query, 0, msi_primary_key_iterator, &info );
642 if( r == ERROR_SUCCESS )
645 msiobj_release( &info.rec->hdr );
647 msiobj_release( &query->hdr );
652 UINT WINAPI MsiDatabaseGetPrimaryKeysW( MSIHANDLE hdb,
653 LPCWSTR table, MSIHANDLE* phRec )
655 MSIRECORD *rec = NULL;
659 TRACE("%ld %s %p\n", hdb, debugstr_w(table), phRec);
661 db = msihandle2msiinfo( hdb, MSIHANDLETYPE_DATABASE );
663 return ERROR_INVALID_HANDLE;
665 r = MSI_DatabaseGetPrimaryKeys( db, table, &rec );
666 if( r == ERROR_SUCCESS )
668 *phRec = alloc_msihandle( &rec->hdr );
669 msiobj_release( &rec->hdr );
671 msiobj_release( &db->hdr );
676 UINT WINAPI MsiDatabaseGetPrimaryKeysA(MSIHANDLE hdb,
677 LPCSTR table, MSIHANDLE* phRec)
679 LPWSTR szwTable = NULL;
682 TRACE("%ld %s %p\n", hdb, debugstr_a(table), phRec);
686 szwTable = strdupAtoW( table );
688 return ERROR_OUTOFMEMORY;
690 r = MsiDatabaseGetPrimaryKeysW( hdb, szwTable, phRec );
691 HeapFree( GetProcessHeap(), 0, szwTable );
696 UINT WINAPI MsiDatabaseIsTablePersistentA(
697 MSIHANDLE hDatabase, LPSTR szTableName)
699 FIXME("%lx %s\n", hDatabase, debugstr_a(szTableName));
700 return ERROR_CALL_NOT_IMPLEMENTED;
703 UINT WINAPI MsiDatabaseIsTablePersistentW(
704 MSIHANDLE hDatabase, LPWSTR szTableName)
706 FIXME("%lx %s\n", hDatabase, debugstr_w(szTableName));
707 return ERROR_CALL_NOT_IMPLEMENTED;