user32: Use appropriate type for lresult.
[wine.git] / dlls / msi / msiquery.c
blob662b4fbb8ceb762807af6d67c7fe0d56c3e30b3f
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("%ld %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 ERR("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)
380 query->row ++;
382 return r;
385 UINT WINAPI MsiViewFetch(MSIHANDLE hView, MSIHANDLE *record)
387 MSIQUERY *query;
388 MSIRECORD *rec = NULL;
389 UINT ret;
391 TRACE("%ld %p\n", hView, record);
393 if( !record )
394 return ERROR_INVALID_PARAMETER;
395 *record = 0;
397 query = msihandle2msiinfo( hView, MSIHANDLETYPE_VIEW );
398 if( !query )
399 return ERROR_INVALID_HANDLE;
400 ret = MSI_ViewFetch( query, &rec );
401 if( ret == ERROR_SUCCESS )
403 *record = alloc_msihandle( &rec->hdr );
404 if (! *record)
405 ret = ERROR_NOT_ENOUGH_MEMORY;
406 msiobj_release( &rec->hdr );
408 msiobj_release( &query->hdr );
409 return ret;
412 UINT MSI_ViewClose(MSIQUERY *query)
414 MSIVIEW *view;
416 TRACE("%p\n", query );
418 view = query->view;
419 if( !view )
420 return ERROR_FUNCTION_FAILED;
421 if( !view->ops->close )
422 return ERROR_FUNCTION_FAILED;
424 return view->ops->close( view );
427 UINT WINAPI MsiViewClose(MSIHANDLE hView)
429 MSIQUERY *query;
430 UINT ret;
432 TRACE("%ld\n", hView );
434 query = msihandle2msiinfo( hView, MSIHANDLETYPE_VIEW );
435 if( !query )
436 return ERROR_INVALID_HANDLE;
438 ret = MSI_ViewClose( query );
439 msiobj_release( &query->hdr );
440 return ret;
443 UINT MSI_ViewExecute(MSIQUERY *query, MSIRECORD *rec )
445 MSIVIEW *view;
447 TRACE("%p %p\n", query, rec);
449 view = query->view;
450 if( !view )
451 return ERROR_FUNCTION_FAILED;
452 if( !view->ops->execute )
453 return ERROR_FUNCTION_FAILED;
454 query->row = 0;
456 return view->ops->execute( view, rec );
459 UINT WINAPI MsiViewExecute(MSIHANDLE hView, MSIHANDLE hRec)
461 MSIQUERY *query;
462 MSIRECORD *rec = NULL;
463 UINT ret;
465 TRACE("%ld %ld\n", hView, hRec);
467 query = msihandle2msiinfo( hView, MSIHANDLETYPE_VIEW );
468 if( !query )
469 return ERROR_INVALID_HANDLE;
471 if( hRec )
473 rec = msihandle2msiinfo( hRec, MSIHANDLETYPE_RECORD );
474 if( !rec )
476 ret = ERROR_INVALID_HANDLE;
477 goto out;
481 msiobj_lock( &rec->hdr );
482 ret = MSI_ViewExecute( query, rec );
483 msiobj_unlock( &rec->hdr );
485 out:
486 msiobj_release( &query->hdr );
487 if( rec )
488 msiobj_release( &rec->hdr );
490 return ret;
493 static UINT msi_set_record_type_string( MSIRECORD *rec, UINT field, UINT type )
495 static const WCHAR fmt[] = { '%','d',0 };
496 WCHAR szType[0x10];
498 if (MSITYPE_IS_BINARY(type))
499 szType[0] = 'v';
500 else if (type & MSITYPE_LOCALIZABLE)
501 szType[0] = 'l';
502 else if (type & MSITYPE_STRING)
503 szType[0] = 's';
504 else
505 szType[0] = 'i';
506 if (type & MSITYPE_NULLABLE)
507 szType[0] &= ~0x20;
509 sprintfW( &szType[1], fmt, (type&0xff) );
511 TRACE("type %04x -> %s\n", type, debugstr_w(szType) );
513 return MSI_RecordSetStringW( rec, field, szType );
516 UINT MSI_ViewGetColumnInfo( MSIQUERY *query, MSICOLINFO info, MSIRECORD **prec )
518 UINT r = ERROR_FUNCTION_FAILED, i, count = 0, type;
519 MSIRECORD *rec;
520 MSIVIEW *view = query->view;
521 LPWSTR name;
523 if( !view )
524 return ERROR_FUNCTION_FAILED;
526 if( !view->ops->get_dimensions )
527 return ERROR_FUNCTION_FAILED;
529 r = view->ops->get_dimensions( view, NULL, &count );
530 if( r != ERROR_SUCCESS )
531 return r;
532 if( !count )
533 return ERROR_INVALID_PARAMETER;
535 rec = MSI_CreateRecord( count );
536 if( !rec )
537 return ERROR_FUNCTION_FAILED;
539 for( i=0; i<count; i++ )
541 name = NULL;
542 r = view->ops->get_column_info( view, i+1, &name, &type );
543 if( r != ERROR_SUCCESS )
544 continue;
545 if (info == MSICOLINFO_NAMES)
546 MSI_RecordSetStringW( rec, i+1, name );
547 else
548 msi_set_record_type_string( rec, i+1, type);
549 msi_free( name );
552 *prec = rec;
553 return ERROR_SUCCESS;
556 UINT WINAPI MsiViewGetColumnInfo(MSIHANDLE hView, MSICOLINFO info, MSIHANDLE *hRec)
558 MSIQUERY *query = NULL;
559 MSIRECORD *rec = NULL;
560 UINT r;
562 TRACE("%ld %d %p\n", hView, info, hRec);
564 if( !hRec )
565 return ERROR_INVALID_PARAMETER;
567 if( info != MSICOLINFO_NAMES && info != MSICOLINFO_TYPES )
568 return ERROR_INVALID_PARAMETER;
570 query = msihandle2msiinfo( hView, MSIHANDLETYPE_VIEW );
571 if( !query )
572 return ERROR_INVALID_HANDLE;
574 r = MSI_ViewGetColumnInfo( query, info, &rec );
575 if ( r == ERROR_SUCCESS )
577 *hRec = alloc_msihandle( &rec->hdr );
578 if ( !*hRec )
579 r = ERROR_NOT_ENOUGH_MEMORY;
580 msiobj_release( &rec->hdr );
583 msiobj_release( &query->hdr );
585 return r;
588 UINT MSI_ViewModify( MSIQUERY *query, MSIMODIFY mode, MSIRECORD *rec )
590 MSIVIEW *view = NULL;
591 UINT r;
593 if ( !query || !rec )
594 return ERROR_INVALID_HANDLE;
596 view = query->view;
597 if ( !view || !view->ops->modify)
598 return ERROR_FUNCTION_FAILED;
600 r = view->ops->modify( view, mode, rec, query->row );
601 if (mode == MSIMODIFY_DELETE && r == ERROR_SUCCESS)
602 query->row--;
604 return r;
607 UINT WINAPI MsiViewModify( MSIHANDLE hView, MSIMODIFY eModifyMode,
608 MSIHANDLE hRecord)
610 MSIQUERY *query = NULL;
611 MSIRECORD *rec = NULL;
612 UINT r = ERROR_FUNCTION_FAILED;
614 TRACE("%ld %x %ld\n", hView, eModifyMode, hRecord);
616 query = msihandle2msiinfo( hView, MSIHANDLETYPE_VIEW );
617 if( !query )
618 return ERROR_INVALID_HANDLE;
620 rec = msihandle2msiinfo( hRecord, MSIHANDLETYPE_RECORD );
621 r = MSI_ViewModify( query, eModifyMode, rec );
623 msiobj_release( &query->hdr );
624 if( rec )
625 msiobj_release( &rec->hdr );
627 return r;
630 MSIDBERROR WINAPI MsiViewGetErrorW( MSIHANDLE handle, LPWSTR szColumnNameBuffer,
631 LPDWORD pcchBuf )
633 MSIQUERY *query = NULL;
634 static const WCHAR szError[] = { 0 };
635 MSIDBERROR r = MSIDBERROR_NOERROR;
636 int len;
638 FIXME("%ld %p %p - returns empty error string\n",
639 handle, szColumnNameBuffer, pcchBuf );
641 if( !pcchBuf )
642 return MSIDBERROR_INVALIDARG;
644 query = msihandle2msiinfo( handle, MSIHANDLETYPE_VIEW );
645 if( !query )
646 return MSIDBERROR_INVALIDARG;
648 len = lstrlenW( szError );
649 if( szColumnNameBuffer )
651 if( *pcchBuf > len )
652 lstrcpyW( szColumnNameBuffer, szError );
653 else
654 r = MSIDBERROR_MOREDATA;
656 *pcchBuf = len;
658 msiobj_release( &query->hdr );
659 return r;
662 MSIDBERROR WINAPI MsiViewGetErrorA( MSIHANDLE handle, LPSTR szColumnNameBuffer,
663 LPDWORD pcchBuf )
665 static const CHAR szError[] = { 0 };
666 MSIQUERY *query = NULL;
667 MSIDBERROR r = MSIDBERROR_NOERROR;
668 int len;
670 FIXME("%ld %p %p - returns empty error string\n",
671 handle, szColumnNameBuffer, pcchBuf );
673 if( !pcchBuf )
674 return MSIDBERROR_INVALIDARG;
676 query = msihandle2msiinfo( handle, MSIHANDLETYPE_VIEW );
677 if( !query )
678 return MSIDBERROR_INVALIDARG;
680 len = lstrlenA( szError );
681 if( szColumnNameBuffer )
683 if( *pcchBuf > len )
684 lstrcpyA( szColumnNameBuffer, szError );
685 else
686 r = MSIDBERROR_MOREDATA;
688 *pcchBuf = len;
690 msiobj_release( &query->hdr );
691 return r;
694 MSIHANDLE WINAPI MsiGetLastErrorRecord( void )
696 FIXME("\n");
697 return 0;
700 DEFINE_GUID( CLSID_MsiTransform, 0x000c1082, 0x0000, 0x0000, 0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46);
702 UINT MSI_DatabaseApplyTransformW( MSIDATABASE *db,
703 LPCWSTR szTransformFile, int iErrorCond )
705 HRESULT r;
706 UINT ret = ERROR_FUNCTION_FAILED;
707 IStorage *stg = NULL;
708 STATSTG stat;
710 TRACE("%p %s %d\n", db, debugstr_w(szTransformFile), iErrorCond);
712 r = StgOpenStorage( szTransformFile, NULL,
713 STGM_DIRECT|STGM_READ|STGM_SHARE_DENY_WRITE, NULL, 0, &stg);
714 if ( FAILED(r) )
715 return ret;
717 r = IStorage_Stat( stg, &stat, STATFLAG_NONAME );
718 if ( FAILED( r ) )
719 goto end;
721 if ( !IsEqualGUID( &stat.clsid, &CLSID_MsiTransform ) )
722 goto end;
724 if( TRACE_ON( msi ) )
725 enum_stream_names( stg );
727 ret = msi_table_apply_transform( db, stg );
729 end:
730 IStorage_Release( stg );
732 return ret;
735 UINT WINAPI MsiDatabaseApplyTransformW( MSIHANDLE hdb,
736 LPCWSTR szTransformFile, int iErrorCond)
738 MSIDATABASE *db;
739 UINT r;
741 db = msihandle2msiinfo( hdb, MSIHANDLETYPE_DATABASE );
742 if( !db )
744 IWineMsiRemoteDatabase *remote_database;
746 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( hdb );
747 if ( !remote_database )
748 return ERROR_INVALID_HANDLE;
750 IWineMsiRemoteDatabase_Release( remote_database );
751 WARN("MsiDatabaseApplyTransform not allowed during a custom action!\n");
753 return ERROR_SUCCESS;
756 r = MSI_DatabaseApplyTransformW( db, szTransformFile, iErrorCond );
757 msiobj_release( &db->hdr );
758 return r;
761 UINT WINAPI MsiDatabaseApplyTransformA( MSIHANDLE hdb,
762 LPCSTR szTransformFile, int iErrorCond)
764 LPWSTR wstr;
765 UINT ret;
767 TRACE("%ld %s %d\n", hdb, debugstr_a(szTransformFile), iErrorCond);
769 wstr = strdupAtoW( szTransformFile );
770 if( szTransformFile && !wstr )
771 return ERROR_NOT_ENOUGH_MEMORY;
773 ret = MsiDatabaseApplyTransformW( hdb, wstr, iErrorCond);
775 msi_free( wstr );
777 return ret;
780 UINT WINAPI MsiDatabaseGenerateTransformA( MSIHANDLE hdb, MSIHANDLE hdbref,
781 LPCSTR szTransformFile, int iReserved1, int iReserved2 )
783 FIXME("%ld %ld %s %d %d\n", hdb, hdbref,
784 debugstr_a(szTransformFile), iReserved1, iReserved2);
785 return ERROR_CALL_NOT_IMPLEMENTED;
788 UINT WINAPI MsiDatabaseGenerateTransformW( MSIHANDLE hdb, MSIHANDLE hdbref,
789 LPCWSTR szTransformFile, int iReserved1, int iReserved2 )
791 FIXME("%ld %ld %s %d %d\n", hdb, hdbref,
792 debugstr_w(szTransformFile), iReserved1, iReserved2);
793 return ERROR_CALL_NOT_IMPLEMENTED;
796 UINT WINAPI MsiDatabaseCommit( MSIHANDLE hdb )
798 MSIDATABASE *db;
799 UINT r;
801 TRACE("%ld\n", hdb);
803 db = msihandle2msiinfo( hdb, MSIHANDLETYPE_DATABASE );
804 if( !db )
806 IWineMsiRemoteDatabase *remote_database;
808 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( hdb );
809 if ( !remote_database )
810 return ERROR_INVALID_HANDLE;
812 IWineMsiRemoteDatabase_Release( remote_database );
813 WARN("MsiDatabaseCommit not allowed during a custom action!\n");
815 return ERROR_SUCCESS;
818 /* FIXME: lock the database */
820 r = MSI_CommitTables( db );
821 if (r != ERROR_SUCCESS) ERR("Failed to commit tables!\n");
823 /* FIXME: unlock the database */
825 msiobj_release( &db->hdr );
827 if (r == ERROR_SUCCESS)
829 msi_free( db->deletefile );
830 db->deletefile = NULL;
833 return r;
836 struct msi_primary_key_record_info
838 DWORD n;
839 MSIRECORD *rec;
842 static UINT msi_primary_key_iterator( MSIRECORD *rec, LPVOID param )
844 struct msi_primary_key_record_info *info = param;
845 LPCWSTR name;
846 DWORD type;
848 type = MSI_RecordGetInteger( rec, 4 );
849 if( type & MSITYPE_KEY )
851 info->n++;
852 if( info->rec )
854 name = MSI_RecordGetString( rec, 3 );
855 MSI_RecordSetStringW( info->rec, info->n, name );
859 return ERROR_SUCCESS;
862 UINT MSI_DatabaseGetPrimaryKeys( MSIDATABASE *db,
863 LPCWSTR table, MSIRECORD **prec )
865 static const WCHAR sql[] = {
866 's','e','l','e','c','t',' ','*',' ',
867 'f','r','o','m',' ','`','_','C','o','l','u','m','n','s','`',' ',
868 'w','h','e','r','e',' ',
869 '`','T','a','b','l','e','`',' ','=',' ','\'','%','s','\'',0 };
870 struct msi_primary_key_record_info info;
871 MSIQUERY *query = NULL;
872 MSIVIEW *view;
873 UINT r;
875 r = MSI_OpenQuery( db, &query, sql, table );
876 if( r != ERROR_SUCCESS )
877 return r;
879 view = query->view;
881 /* count the number of primary key records */
882 info.n = 0;
883 info.rec = 0;
884 r = MSI_IterateRecords( query, 0, msi_primary_key_iterator, &info );
885 if( r == ERROR_SUCCESS )
887 TRACE("Found %d primary keys\n", info.n );
889 /* allocate a record and fill in the names of the tables */
890 info.rec = MSI_CreateRecord( info.n );
891 info.n = 0;
892 r = MSI_IterateRecords( query, 0, msi_primary_key_iterator, &info );
893 if( r == ERROR_SUCCESS )
894 *prec = info.rec;
895 else
896 msiobj_release( &info.rec->hdr );
898 msiobj_release( &query->hdr );
900 return r;
903 UINT WINAPI MsiDatabaseGetPrimaryKeysW( MSIHANDLE hdb,
904 LPCWSTR table, MSIHANDLE* phRec )
906 MSIRECORD *rec = NULL;
907 MSIDATABASE *db;
908 UINT r;
910 TRACE("%ld %s %p\n", hdb, debugstr_w(table), phRec);
912 db = msihandle2msiinfo( hdb, MSIHANDLETYPE_DATABASE );
913 if( !db )
915 HRESULT hr;
916 IWineMsiRemoteDatabase *remote_database;
918 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( hdb );
919 if ( !remote_database )
920 return ERROR_INVALID_HANDLE;
922 hr = IWineMsiRemoteDatabase_GetPrimaryKeys( remote_database, (BSTR)table, phRec );
923 IWineMsiRemoteDatabase_Release( remote_database );
925 if (FAILED(hr))
927 if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
928 return HRESULT_CODE(hr);
930 return ERROR_FUNCTION_FAILED;
933 return ERROR_SUCCESS;
936 r = MSI_DatabaseGetPrimaryKeys( db, table, &rec );
937 if( r == ERROR_SUCCESS )
939 *phRec = alloc_msihandle( &rec->hdr );
940 if (! *phRec)
941 r = ERROR_NOT_ENOUGH_MEMORY;
942 msiobj_release( &rec->hdr );
944 msiobj_release( &db->hdr );
946 return r;
949 UINT WINAPI MsiDatabaseGetPrimaryKeysA(MSIHANDLE hdb,
950 LPCSTR table, MSIHANDLE* phRec)
952 LPWSTR szwTable = NULL;
953 UINT r;
955 TRACE("%ld %s %p\n", hdb, debugstr_a(table), phRec);
957 if( table )
959 szwTable = strdupAtoW( table );
960 if( !szwTable )
961 return ERROR_OUTOFMEMORY;
963 r = MsiDatabaseGetPrimaryKeysW( hdb, szwTable, phRec );
964 msi_free( szwTable );
966 return r;
969 MSICONDITION WINAPI MsiDatabaseIsTablePersistentA(
970 MSIHANDLE hDatabase, LPCSTR szTableName)
972 LPWSTR szwTableName = NULL;
973 MSICONDITION r;
975 TRACE("%lx %s\n", hDatabase, debugstr_a(szTableName));
977 if( szTableName )
979 szwTableName = strdupAtoW( szTableName );
980 if( !szwTableName )
981 return MSICONDITION_ERROR;
983 r = MsiDatabaseIsTablePersistentW( hDatabase, szwTableName );
984 msi_free( szwTableName );
986 return r;
989 MSICONDITION WINAPI MsiDatabaseIsTablePersistentW(
990 MSIHANDLE hDatabase, LPCWSTR szTableName)
992 MSIDATABASE *db;
993 MSICONDITION r;
995 TRACE("%lx %s\n", hDatabase, debugstr_w(szTableName));
997 db = msihandle2msiinfo( hDatabase, MSIHANDLETYPE_DATABASE );
998 if( !db )
1000 HRESULT hr;
1001 MSICONDITION condition;
1002 IWineMsiRemoteDatabase *remote_database;
1004 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( hDatabase );
1005 if ( !remote_database )
1006 return MSICONDITION_ERROR;
1008 hr = IWineMsiRemoteDatabase_IsTablePersistent( remote_database,
1009 (BSTR)szTableName, &condition );
1010 IWineMsiRemoteDatabase_Release( remote_database );
1012 if (FAILED(hr))
1013 return MSICONDITION_ERROR;
1015 return condition;
1018 r = MSI_DatabaseIsTablePersistent( db, szTableName );
1020 msiobj_release( &db->hdr );
1022 return r;