user32/tests: Fix some message sequences for Win9x.
[wine/multimedia.git] / dlls / msi / msiquery.c
blob0cfd05e8b1ddf313534080872c209332b052379a
1 /*
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
21 #include <stdarg.h>
23 #define COBJMACROS
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winerror.h"
28 #include "wine/debug.h"
29 #include "wine/unicode.h"
30 #include "msi.h"
31 #include "msiquery.h"
32 #include "objbase.h"
33 #include "objidl.h"
34 #include "msipriv.h"
35 #include "winnls.h"
37 #include "query.h"
38 #include "msiserver.h"
40 #include "initguid.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(msi);
44 static void MSI_CloseView( MSIOBJECTHDR *arg )
46 MSIQUERY *query = (MSIQUERY*) arg;
47 struct list *ptr, *t;
49 if( query->view && query->view->ops->delete )
50 query->view->ops->delete( query->view );
51 msiobj_release( &query->db->hdr );
53 LIST_FOR_EACH_SAFE( ptr, t, &query->mem )
55 msi_free( ptr );
59 UINT VIEW_find_column( MSIVIEW *table, LPCWSTR name, UINT *n )
61 LPWSTR col_name;
62 UINT i, count, r;
64 r = table->ops->get_dimensions( table, NULL, &count );
65 if( r != ERROR_SUCCESS )
66 return r;
68 for( i=1; i<=count; i++ )
70 INT x;
72 col_name = NULL;
73 r = table->ops->get_column_info( table, i, &col_name, NULL );
74 if( r != ERROR_SUCCESS )
75 return r;
76 x = lstrcmpW( name, col_name );
77 msi_free( col_name );
78 if( !x )
80 *n = i;
81 return ERROR_SUCCESS;
85 return ERROR_INVALID_PARAMETER;
88 UINT WINAPI MsiDatabaseOpenViewA(MSIHANDLE hdb,
89 LPCSTR szQuery, MSIHANDLE *phView)
91 UINT r;
92 LPWSTR szwQuery;
94 TRACE("%d %s %p\n", hdb, debugstr_a(szQuery), phView);
96 if( szQuery )
98 szwQuery = strdupAtoW( szQuery );
99 if( !szwQuery )
100 return ERROR_FUNCTION_FAILED;
102 else
103 szwQuery = NULL;
105 r = MsiDatabaseOpenViewW( hdb, szwQuery, phView);
107 msi_free( szwQuery );
108 return r;
111 UINT MSI_DatabaseOpenViewW(MSIDATABASE *db,
112 LPCWSTR szQuery, MSIQUERY **pView)
114 MSIQUERY *query;
115 UINT r;
117 TRACE("%s %p\n", debugstr_w(szQuery), pView);
119 if( !szQuery)
120 return ERROR_INVALID_PARAMETER;
122 /* pre allocate a handle to hold a pointer to the view */
123 query = alloc_msiobject( MSIHANDLETYPE_VIEW, sizeof (MSIQUERY),
124 MSI_CloseView );
125 if( !query )
126 return ERROR_FUNCTION_FAILED;
128 msiobj_addref( &db->hdr );
129 query->row = 0;
130 query->db = db;
131 query->view = NULL;
132 list_init( &query->mem );
134 r = MSI_ParseSQL( db, szQuery, &query->view, &query->mem );
135 if( r == ERROR_SUCCESS )
137 msiobj_addref( &query->hdr );
138 *pView = query;
141 msiobj_release( &query->hdr );
142 return r;
145 UINT MSI_OpenQuery( MSIDATABASE *db, MSIQUERY **view, LPCWSTR fmt, ... )
147 UINT r;
148 int size = 100, res;
149 LPWSTR query;
151 /* construct the string */
152 for (;;)
154 va_list va;
155 query = msi_alloc( size*sizeof(WCHAR) );
156 va_start(va, fmt);
157 res = vsnprintfW(query, size, fmt, va);
158 va_end(va);
159 if (res == -1) size *= 2;
160 else if (res >= size) size = res + 1;
161 else break;
162 msi_free( query );
164 /* perform the query */
165 r = MSI_DatabaseOpenViewW(db, query, view);
166 msi_free(query);
167 return r;
170 UINT MSI_IterateRecords( MSIQUERY *view, LPDWORD count,
171 record_func func, LPVOID param )
173 MSIRECORD *rec = NULL;
174 UINT r, n = 0, max = 0;
176 r = MSI_ViewExecute( view, NULL );
177 if( r != ERROR_SUCCESS )
178 return r;
180 if( count )
181 max = *count;
183 /* iterate a query */
184 for( n = 0; (max == 0) || (n < max); n++ )
186 r = MSI_ViewFetch( view, &rec );
187 if( r != ERROR_SUCCESS )
188 break;
189 if (func)
190 r = func( rec, param );
191 msiobj_release( &rec->hdr );
192 if( r != ERROR_SUCCESS )
193 break;
196 MSI_ViewClose( view );
198 if( count )
199 *count = n;
201 if( r == ERROR_NO_MORE_ITEMS )
202 r = ERROR_SUCCESS;
204 return r;
207 /* return a single record from a query */
208 MSIRECORD *MSI_QueryGetRecord( MSIDATABASE *db, LPCWSTR fmt, ... )
210 MSIRECORD *rec = NULL;
211 MSIQUERY *view = NULL;
212 UINT r;
213 int size = 100, res;
214 LPWSTR query;
216 /* construct the string */
217 for (;;)
219 va_list va;
220 query = msi_alloc( size*sizeof(WCHAR) );
221 va_start(va, fmt);
222 res = vsnprintfW(query, size, fmt, va);
223 va_end(va);
224 if (res == -1) size *= 2;
225 else if (res >= size) size = res + 1;
226 else break;
227 msi_free( query );
229 /* perform the query */
230 r = MSI_DatabaseOpenViewW(db, query, &view);
231 msi_free(query);
233 if( r == ERROR_SUCCESS )
235 MSI_ViewExecute( view, NULL );
236 MSI_ViewFetch( view, &rec );
237 MSI_ViewClose( view );
238 msiobj_release( &view->hdr );
240 return rec;
243 UINT WINAPI MsiDatabaseOpenViewW(MSIHANDLE hdb,
244 LPCWSTR szQuery, MSIHANDLE *phView)
246 MSIDATABASE *db;
247 MSIQUERY *query = NULL;
248 UINT ret;
250 TRACE("%s %p\n", debugstr_w(szQuery), phView);
252 db = msihandle2msiinfo( hdb, MSIHANDLETYPE_DATABASE );
253 if( !db )
255 HRESULT hr;
256 IWineMsiRemoteDatabase *remote_database;
258 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( hdb );
259 if ( !remote_database )
260 return ERROR_INVALID_HANDLE;
262 hr = IWineMsiRemoteDatabase_OpenView( remote_database, (BSTR)szQuery, phView );
263 IWineMsiRemoteDatabase_Release( remote_database );
265 if (FAILED(hr))
267 if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
268 return HRESULT_CODE(hr);
270 return ERROR_FUNCTION_FAILED;
273 return ERROR_SUCCESS;
276 ret = MSI_DatabaseOpenViewW( db, szQuery, &query );
277 if( ret == ERROR_SUCCESS )
279 *phView = alloc_msihandle( &query->hdr );
280 if (! *phView)
281 ret = ERROR_NOT_ENOUGH_MEMORY;
282 msiobj_release( &query->hdr );
284 msiobj_release( &db->hdr );
286 return ret;
289 UINT msi_view_get_row(MSIDATABASE *db, MSIVIEW *view, UINT row, MSIRECORD **rec)
291 UINT row_count = 0, col_count = 0, i, ival, ret, type;
293 TRACE("%p %p %d %p\n", db, view, row, rec);
295 ret = view->ops->get_dimensions(view, &row_count, &col_count);
296 if (ret)
297 return ret;
299 if (!col_count)
300 return ERROR_INVALID_PARAMETER;
302 if (row >= row_count)
303 return ERROR_NO_MORE_ITEMS;
305 *rec = MSI_CreateRecord(col_count);
306 if (!*rec)
307 return ERROR_FUNCTION_FAILED;
309 for (i = 1; i <= col_count; i++)
311 ret = view->ops->get_column_info(view, i, NULL, &type);
312 if (ret)
314 ERR("Error getting column type for %d\n", i);
315 continue;
318 if (MSITYPE_IS_BINARY(type))
320 IStream *stm = NULL;
322 ret = view->ops->fetch_stream(view, row, i, &stm);
323 if ((ret == ERROR_SUCCESS) && stm)
325 MSI_RecordSetIStream(*rec, i, stm);
326 IStream_Release(stm);
328 else
329 WARN("failed to get stream\n");
331 continue;
334 ret = view->ops->fetch_int(view, row, i, &ival);
335 if (ret)
337 ERR("Error fetching data for %d\n", i);
338 continue;
341 if (! (type & MSITYPE_VALID))
342 ERR("Invalid type!\n");
344 /* check if it's nul (0) - if so, don't set anything */
345 if (!ival)
346 continue;
348 if (type & MSITYPE_STRING)
350 LPCWSTR sval;
352 sval = msi_string_lookup_id(db->strings, ival);
353 MSI_RecordSetStringW(*rec, i, sval);
355 else
357 if ((type & MSI_DATASIZEMASK) == 2)
358 MSI_RecordSetInteger(*rec, i, ival - (1<<15));
359 else
360 MSI_RecordSetInteger(*rec, i, ival - (1<<31));
364 return ERROR_SUCCESS;
367 UINT MSI_ViewFetch(MSIQUERY *query, MSIRECORD **prec)
369 MSIVIEW *view;
370 UINT r;
372 TRACE("%p %p\n", query, prec );
374 view = query->view;
375 if( !view )
376 return ERROR_FUNCTION_FAILED;
378 r = msi_view_get_row(query->db, view, query->row, prec);
379 if (r == ERROR_SUCCESS)
381 query->row ++;
382 MSI_RecordSetInteger(*prec, 0, (int)query);
385 return r;
388 UINT WINAPI MsiViewFetch(MSIHANDLE hView, MSIHANDLE *record)
390 MSIQUERY *query;
391 MSIRECORD *rec = NULL;
392 UINT ret;
394 TRACE("%d %p\n", hView, record);
396 if( !record )
397 return ERROR_INVALID_PARAMETER;
398 *record = 0;
400 query = msihandle2msiinfo( hView, MSIHANDLETYPE_VIEW );
401 if( !query )
402 return ERROR_INVALID_HANDLE;
403 ret = MSI_ViewFetch( query, &rec );
404 if( ret == ERROR_SUCCESS )
406 *record = alloc_msihandle( &rec->hdr );
407 if (! *record)
408 ret = ERROR_NOT_ENOUGH_MEMORY;
409 msiobj_release( &rec->hdr );
411 msiobj_release( &query->hdr );
412 return ret;
415 UINT MSI_ViewClose(MSIQUERY *query)
417 MSIVIEW *view;
419 TRACE("%p\n", query );
421 view = query->view;
422 if( !view )
423 return ERROR_FUNCTION_FAILED;
424 if( !view->ops->close )
425 return ERROR_FUNCTION_FAILED;
427 return view->ops->close( view );
430 UINT WINAPI MsiViewClose(MSIHANDLE hView)
432 MSIQUERY *query;
433 UINT ret;
435 TRACE("%d\n", hView );
437 query = msihandle2msiinfo( hView, MSIHANDLETYPE_VIEW );
438 if( !query )
439 return ERROR_INVALID_HANDLE;
441 ret = MSI_ViewClose( query );
442 msiobj_release( &query->hdr );
443 return ret;
446 UINT MSI_ViewExecute(MSIQUERY *query, MSIRECORD *rec )
448 MSIVIEW *view;
450 TRACE("%p %p\n", query, rec);
452 view = query->view;
453 if( !view )
454 return ERROR_FUNCTION_FAILED;
455 if( !view->ops->execute )
456 return ERROR_FUNCTION_FAILED;
457 query->row = 0;
459 return view->ops->execute( view, rec );
462 UINT WINAPI MsiViewExecute(MSIHANDLE hView, MSIHANDLE hRec)
464 MSIQUERY *query;
465 MSIRECORD *rec = NULL;
466 UINT ret;
468 TRACE("%d %d\n", hView, hRec);
470 query = msihandle2msiinfo( hView, MSIHANDLETYPE_VIEW );
471 if( !query )
472 return ERROR_INVALID_HANDLE;
474 if( hRec )
476 rec = msihandle2msiinfo( hRec, MSIHANDLETYPE_RECORD );
477 if( !rec )
479 ret = ERROR_INVALID_HANDLE;
480 goto out;
484 msiobj_lock( &rec->hdr );
485 ret = MSI_ViewExecute( query, rec );
486 msiobj_unlock( &rec->hdr );
488 out:
489 msiobj_release( &query->hdr );
490 if( rec )
491 msiobj_release( &rec->hdr );
493 return ret;
496 static UINT msi_set_record_type_string( MSIRECORD *rec, UINT field, UINT type )
498 static const WCHAR fmt[] = { '%','d',0 };
499 WCHAR szType[0x10];
501 if (MSITYPE_IS_BINARY(type))
502 szType[0] = 'v';
503 else if (type & MSITYPE_LOCALIZABLE)
504 szType[0] = 'l';
505 else if (type & MSITYPE_STRING)
506 szType[0] = 's';
507 else
508 szType[0] = 'i';
509 if (type & MSITYPE_NULLABLE)
510 szType[0] &= ~0x20;
512 sprintfW( &szType[1], fmt, (type&0xff) );
514 TRACE("type %04x -> %s\n", type, debugstr_w(szType) );
516 return MSI_RecordSetStringW( rec, field, szType );
519 UINT MSI_ViewGetColumnInfo( MSIQUERY *query, MSICOLINFO info, MSIRECORD **prec )
521 UINT r = ERROR_FUNCTION_FAILED, i, count = 0, type;
522 MSIRECORD *rec;
523 MSIVIEW *view = query->view;
524 LPWSTR name;
526 if( !view )
527 return ERROR_FUNCTION_FAILED;
529 if( !view->ops->get_dimensions )
530 return ERROR_FUNCTION_FAILED;
532 r = view->ops->get_dimensions( view, NULL, &count );
533 if( r != ERROR_SUCCESS )
534 return r;
535 if( !count )
536 return ERROR_INVALID_PARAMETER;
538 rec = MSI_CreateRecord( count );
539 if( !rec )
540 return ERROR_FUNCTION_FAILED;
542 for( i=0; i<count; i++ )
544 name = NULL;
545 r = view->ops->get_column_info( view, i+1, &name, &type );
546 if( r != ERROR_SUCCESS )
547 continue;
548 if (info == MSICOLINFO_NAMES)
549 MSI_RecordSetStringW( rec, i+1, name );
550 else
551 msi_set_record_type_string( rec, i+1, type);
552 msi_free( name );
555 *prec = rec;
556 return ERROR_SUCCESS;
559 UINT WINAPI MsiViewGetColumnInfo(MSIHANDLE hView, MSICOLINFO info, MSIHANDLE *hRec)
561 MSIQUERY *query = NULL;
562 MSIRECORD *rec = NULL;
563 UINT r;
565 TRACE("%d %d %p\n", hView, info, hRec);
567 if( !hRec )
568 return ERROR_INVALID_PARAMETER;
570 if( info != MSICOLINFO_NAMES && info != MSICOLINFO_TYPES )
571 return ERROR_INVALID_PARAMETER;
573 query = msihandle2msiinfo( hView, MSIHANDLETYPE_VIEW );
574 if( !query )
575 return ERROR_INVALID_HANDLE;
577 r = MSI_ViewGetColumnInfo( query, info, &rec );
578 if ( r == ERROR_SUCCESS )
580 *hRec = alloc_msihandle( &rec->hdr );
581 if ( !*hRec )
582 r = ERROR_NOT_ENOUGH_MEMORY;
583 msiobj_release( &rec->hdr );
586 msiobj_release( &query->hdr );
588 return r;
591 UINT MSI_ViewModify( MSIQUERY *query, MSIMODIFY mode, MSIRECORD *rec )
593 MSIVIEW *view = NULL;
594 UINT r;
596 if ( !query || !rec )
597 return ERROR_INVALID_HANDLE;
599 view = query->view;
600 if ( !view || !view->ops->modify)
601 return ERROR_FUNCTION_FAILED;
603 if ( mode == MSIMODIFY_UPDATE && MSI_RecordGetInteger( rec, 0 ) != (int)query )
604 return ERROR_FUNCTION_FAILED;
606 r = view->ops->modify( view, mode, rec, query->row );
607 if (mode == MSIMODIFY_DELETE && r == ERROR_SUCCESS)
608 query->row--;
610 return r;
613 UINT WINAPI MsiViewModify( MSIHANDLE hView, MSIMODIFY eModifyMode,
614 MSIHANDLE hRecord)
616 MSIQUERY *query = NULL;
617 MSIRECORD *rec = NULL;
618 UINT r = ERROR_FUNCTION_FAILED;
620 TRACE("%d %x %d\n", hView, eModifyMode, hRecord);
622 query = msihandle2msiinfo( hView, MSIHANDLETYPE_VIEW );
623 if( !query )
624 return ERROR_INVALID_HANDLE;
626 rec = msihandle2msiinfo( hRecord, MSIHANDLETYPE_RECORD );
627 r = MSI_ViewModify( query, eModifyMode, rec );
629 msiobj_release( &query->hdr );
630 if( rec )
631 msiobj_release( &rec->hdr );
633 return r;
636 MSIDBERROR WINAPI MsiViewGetErrorW( MSIHANDLE handle, LPWSTR szColumnNameBuffer,
637 LPDWORD pcchBuf )
639 MSIQUERY *query = NULL;
640 static const WCHAR szError[] = { 0 };
641 MSIDBERROR r = MSIDBERROR_NOERROR;
642 DWORD len;
644 FIXME("%d %p %p - returns empty error string\n",
645 handle, szColumnNameBuffer, pcchBuf );
647 if( !pcchBuf )
648 return MSIDBERROR_INVALIDARG;
650 query = msihandle2msiinfo( handle, MSIHANDLETYPE_VIEW );
651 if( !query )
652 return MSIDBERROR_INVALIDARG;
654 len = strlenW( szError );
655 if( szColumnNameBuffer )
657 if( *pcchBuf > len )
658 lstrcpyW( szColumnNameBuffer, szError );
659 else
660 r = MSIDBERROR_MOREDATA;
662 *pcchBuf = len;
664 msiobj_release( &query->hdr );
665 return r;
668 MSIDBERROR WINAPI MsiViewGetErrorA( MSIHANDLE handle, LPSTR szColumnNameBuffer,
669 LPDWORD pcchBuf )
671 static const CHAR szError[] = { 0 };
672 MSIQUERY *query = NULL;
673 MSIDBERROR r = MSIDBERROR_NOERROR;
674 DWORD len;
676 FIXME("%d %p %p - returns empty error string\n",
677 handle, szColumnNameBuffer, pcchBuf );
679 if( !pcchBuf )
680 return MSIDBERROR_INVALIDARG;
682 query = msihandle2msiinfo( handle, MSIHANDLETYPE_VIEW );
683 if( !query )
684 return MSIDBERROR_INVALIDARG;
686 len = strlen( szError );
687 if( szColumnNameBuffer )
689 if( *pcchBuf > len )
690 lstrcpyA( szColumnNameBuffer, szError );
691 else
692 r = MSIDBERROR_MOREDATA;
694 *pcchBuf = len;
696 msiobj_release( &query->hdr );
697 return r;
700 MSIHANDLE WINAPI MsiGetLastErrorRecord( void )
702 FIXME("\n");
703 return 0;
706 DEFINE_GUID( CLSID_MsiTransform, 0x000c1082, 0x0000, 0x0000, 0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46);
708 UINT MSI_DatabaseApplyTransformW( MSIDATABASE *db,
709 LPCWSTR szTransformFile, int iErrorCond )
711 HRESULT r;
712 UINT ret = ERROR_FUNCTION_FAILED;
713 IStorage *stg = NULL;
714 STATSTG stat;
716 TRACE("%p %s %d\n", db, debugstr_w(szTransformFile), iErrorCond);
718 r = StgOpenStorage( szTransformFile, NULL,
719 STGM_DIRECT|STGM_READ|STGM_SHARE_DENY_WRITE, NULL, 0, &stg);
720 if ( FAILED(r) )
721 return ret;
723 r = IStorage_Stat( stg, &stat, STATFLAG_NONAME );
724 if ( FAILED( r ) )
725 goto end;
727 if ( !IsEqualGUID( &stat.clsid, &CLSID_MsiTransform ) )
728 goto end;
730 if( TRACE_ON( msi ) )
731 enum_stream_names( stg );
733 ret = msi_table_apply_transform( db, stg );
735 end:
736 IStorage_Release( stg );
738 return ret;
741 UINT WINAPI MsiDatabaseApplyTransformW( MSIHANDLE hdb,
742 LPCWSTR szTransformFile, int iErrorCond)
744 MSIDATABASE *db;
745 UINT r;
747 db = msihandle2msiinfo( hdb, MSIHANDLETYPE_DATABASE );
748 if( !db )
750 IWineMsiRemoteDatabase *remote_database;
752 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( hdb );
753 if ( !remote_database )
754 return ERROR_INVALID_HANDLE;
756 IWineMsiRemoteDatabase_Release( remote_database );
757 WARN("MsiDatabaseApplyTransform not allowed during a custom action!\n");
759 return ERROR_SUCCESS;
762 r = MSI_DatabaseApplyTransformW( db, szTransformFile, iErrorCond );
763 msiobj_release( &db->hdr );
764 return r;
767 UINT WINAPI MsiDatabaseApplyTransformA( MSIHANDLE hdb,
768 LPCSTR szTransformFile, int iErrorCond)
770 LPWSTR wstr;
771 UINT ret;
773 TRACE("%d %s %d\n", hdb, debugstr_a(szTransformFile), iErrorCond);
775 wstr = strdupAtoW( szTransformFile );
776 if( szTransformFile && !wstr )
777 return ERROR_NOT_ENOUGH_MEMORY;
779 ret = MsiDatabaseApplyTransformW( hdb, wstr, iErrorCond);
781 msi_free( wstr );
783 return ret;
786 UINT WINAPI MsiDatabaseGenerateTransformA( MSIHANDLE hdb, MSIHANDLE hdbref,
787 LPCSTR szTransformFile, int iReserved1, int iReserved2 )
789 FIXME("%d %d %s %d %d\n", hdb, hdbref,
790 debugstr_a(szTransformFile), iReserved1, iReserved2);
791 return ERROR_CALL_NOT_IMPLEMENTED;
794 UINT WINAPI MsiDatabaseGenerateTransformW( MSIHANDLE hdb, MSIHANDLE hdbref,
795 LPCWSTR szTransformFile, int iReserved1, int iReserved2 )
797 FIXME("%d %d %s %d %d\n", hdb, hdbref,
798 debugstr_w(szTransformFile), iReserved1, iReserved2);
799 return ERROR_CALL_NOT_IMPLEMENTED;
802 UINT WINAPI MsiDatabaseCommit( MSIHANDLE hdb )
804 MSIDATABASE *db;
805 UINT r;
807 TRACE("%d\n", hdb);
809 db = msihandle2msiinfo( hdb, MSIHANDLETYPE_DATABASE );
810 if( !db )
812 IWineMsiRemoteDatabase *remote_database;
814 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( hdb );
815 if ( !remote_database )
816 return ERROR_INVALID_HANDLE;
818 IWineMsiRemoteDatabase_Release( remote_database );
819 WARN("MsiDatabaseCommit not allowed during a custom action!\n");
821 return ERROR_SUCCESS;
824 /* FIXME: lock the database */
826 r = MSI_CommitTables( db );
827 if (r != ERROR_SUCCESS) ERR("Failed to commit tables!\n");
829 /* FIXME: unlock the database */
831 msiobj_release( &db->hdr );
833 if (r == ERROR_SUCCESS)
835 msi_free( db->deletefile );
836 db->deletefile = NULL;
839 return r;
842 struct msi_primary_key_record_info
844 DWORD n;
845 MSIRECORD *rec;
848 static UINT msi_primary_key_iterator( MSIRECORD *rec, LPVOID param )
850 struct msi_primary_key_record_info *info = param;
851 LPCWSTR name, table;
852 DWORD type;
854 type = MSI_RecordGetInteger( rec, 4 );
855 if( type & MSITYPE_KEY )
857 info->n++;
858 if( info->rec )
860 if ( info->n == 1 )
862 table = MSI_RecordGetString( rec, 1 );
863 MSI_RecordSetStringW( info->rec, 0, table);
866 name = MSI_RecordGetString( rec, 3 );
867 MSI_RecordSetStringW( info->rec, info->n, name );
871 return ERROR_SUCCESS;
874 UINT MSI_DatabaseGetPrimaryKeys( MSIDATABASE *db,
875 LPCWSTR table, MSIRECORD **prec )
877 static const WCHAR sql[] = {
878 's','e','l','e','c','t',' ','*',' ',
879 'f','r','o','m',' ','`','_','C','o','l','u','m','n','s','`',' ',
880 'w','h','e','r','e',' ',
881 '`','T','a','b','l','e','`',' ','=',' ','\'','%','s','\'',0 };
882 struct msi_primary_key_record_info info;
883 MSIQUERY *query = NULL;
884 UINT r;
886 r = MSI_OpenQuery( db, &query, sql, table );
887 if( r != ERROR_SUCCESS )
888 return r;
890 /* count the number of primary key records */
891 info.n = 0;
892 info.rec = 0;
893 r = MSI_IterateRecords( query, 0, msi_primary_key_iterator, &info );
894 if( r == ERROR_SUCCESS )
896 TRACE("Found %d primary keys\n", info.n );
898 /* allocate a record and fill in the names of the tables */
899 info.rec = MSI_CreateRecord( info.n );
900 info.n = 0;
901 r = MSI_IterateRecords( query, 0, msi_primary_key_iterator, &info );
902 if( r == ERROR_SUCCESS )
903 *prec = info.rec;
904 else
905 msiobj_release( &info.rec->hdr );
907 msiobj_release( &query->hdr );
909 return r;
912 UINT WINAPI MsiDatabaseGetPrimaryKeysW( MSIHANDLE hdb,
913 LPCWSTR table, MSIHANDLE* phRec )
915 MSIRECORD *rec = NULL;
916 MSIDATABASE *db;
917 UINT r;
919 TRACE("%d %s %p\n", hdb, debugstr_w(table), phRec);
921 db = msihandle2msiinfo( hdb, MSIHANDLETYPE_DATABASE );
922 if( !db )
924 HRESULT hr;
925 IWineMsiRemoteDatabase *remote_database;
927 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( hdb );
928 if ( !remote_database )
929 return ERROR_INVALID_HANDLE;
931 hr = IWineMsiRemoteDatabase_GetPrimaryKeys( remote_database, (BSTR)table, phRec );
932 IWineMsiRemoteDatabase_Release( remote_database );
934 if (FAILED(hr))
936 if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
937 return HRESULT_CODE(hr);
939 return ERROR_FUNCTION_FAILED;
942 return ERROR_SUCCESS;
945 r = MSI_DatabaseGetPrimaryKeys( db, table, &rec );
946 if( r == ERROR_SUCCESS )
948 *phRec = alloc_msihandle( &rec->hdr );
949 if (! *phRec)
950 r = ERROR_NOT_ENOUGH_MEMORY;
951 msiobj_release( &rec->hdr );
953 msiobj_release( &db->hdr );
955 return r;
958 UINT WINAPI MsiDatabaseGetPrimaryKeysA(MSIHANDLE hdb,
959 LPCSTR table, MSIHANDLE* phRec)
961 LPWSTR szwTable = NULL;
962 UINT r;
964 TRACE("%d %s %p\n", hdb, debugstr_a(table), phRec);
966 if( table )
968 szwTable = strdupAtoW( table );
969 if( !szwTable )
970 return ERROR_OUTOFMEMORY;
972 r = MsiDatabaseGetPrimaryKeysW( hdb, szwTable, phRec );
973 msi_free( szwTable );
975 return r;
978 MSICONDITION WINAPI MsiDatabaseIsTablePersistentA(
979 MSIHANDLE hDatabase, LPCSTR szTableName)
981 LPWSTR szwTableName = NULL;
982 MSICONDITION r;
984 TRACE("%x %s\n", hDatabase, debugstr_a(szTableName));
986 if( szTableName )
988 szwTableName = strdupAtoW( szTableName );
989 if( !szwTableName )
990 return MSICONDITION_ERROR;
992 r = MsiDatabaseIsTablePersistentW( hDatabase, szwTableName );
993 msi_free( szwTableName );
995 return r;
998 MSICONDITION WINAPI MsiDatabaseIsTablePersistentW(
999 MSIHANDLE hDatabase, LPCWSTR szTableName)
1001 MSIDATABASE *db;
1002 MSICONDITION r;
1004 TRACE("%x %s\n", hDatabase, debugstr_w(szTableName));
1006 db = msihandle2msiinfo( hDatabase, MSIHANDLETYPE_DATABASE );
1007 if( !db )
1009 HRESULT hr;
1010 MSICONDITION condition;
1011 IWineMsiRemoteDatabase *remote_database;
1013 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( hDatabase );
1014 if ( !remote_database )
1015 return MSICONDITION_ERROR;
1017 hr = IWineMsiRemoteDatabase_IsTablePersistent( remote_database,
1018 (BSTR)szTableName, &condition );
1019 IWineMsiRemoteDatabase_Release( remote_database );
1021 if (FAILED(hr))
1022 return MSICONDITION_ERROR;
1024 return condition;
1027 r = MSI_DatabaseIsTablePersistent( db, szTableName );
1029 msiobj_release( &db->hdr );
1031 return r;