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
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
39 #include "wine/debug.h"
40 #include "wine/unicode.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
44 #define MSITABLE_HASH_TABLE_SIZE 37
45 #define LONG_STR_BYTES 3
47 typedef struct tagMSICOLUMNHASHENTRY
49 struct tagMSICOLUMNHASHENTRY *next;
54 typedef struct tagMSICOLUMNINFO
63 MSICOLUMNHASHENTRY **hash_table;
66 typedef struct tagMSIORDERINFO
76 BOOL *data_persistent;
79 MSICOLUMNINFO *colinfo;
81 MSICONDITION persistent;
86 typedef struct tagMSITRANSFORM {
91 static const WCHAR szStringData[] = {
92 '_','S','t','r','i','n','g','D','a','t','a',0 };
93 static const WCHAR szStringPool[] = {
94 '_','S','t','r','i','n','g','P','o','o','l',0 };
96 /* information for default tables */
97 static WCHAR szTables[] = { '_','T','a','b','l','e','s',0 };
98 static WCHAR szTable[] = { 'T','a','b','l','e',0 };
99 static WCHAR szName[] = { 'N','a','m','e',0 };
100 static WCHAR szColumns[] = { '_','C','o','l','u','m','n','s',0 };
101 static WCHAR szNumber[] = { 'N','u','m','b','e','r',0 };
102 static WCHAR szType[] = { 'T','y','p','e',0 };
104 static const MSICOLUMNINFO _Columns_cols[4] = {
105 { szColumns, 1, szTable, MSITYPE_VALID | MSITYPE_STRING | MSITYPE_KEY | 64, 0, 0, 0, NULL },
106 { szColumns, 2, szNumber, MSITYPE_VALID | MSITYPE_KEY | 2, 2, 0, 0, NULL },
107 { szColumns, 3, szName, MSITYPE_VALID | MSITYPE_STRING | 64, 4, 0, 0, NULL },
108 { szColumns, 4, szType, MSITYPE_VALID | 2, 6, 0, 0, NULL },
111 static const MSICOLUMNINFO _Tables_cols[1] = {
112 { szTables, 1, szName, MSITYPE_VALID | MSITYPE_STRING | MSITYPE_KEY | 64, 0, 0, 0, NULL },
115 #define MAX_STREAM_NAME 0x1f
117 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name,
118 MSICOLUMNINFO **pcols, UINT *pcount );
119 static void table_calc_column_offsets( MSIDATABASE *db, MSICOLUMNINFO *colinfo,
121 static UINT get_tablecolumns( MSIDATABASE *db,
122 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz);
123 static void msi_free_colinfo( MSICOLUMNINFO *colinfo, UINT count );
124 static UINT table_find_insert_idx (MSIVIEW *view, LPCWSTR name, INT *pidx);
126 static inline UINT bytes_per_column( MSIDATABASE *db, const MSICOLUMNINFO *col )
128 if( MSITYPE_IS_BINARY(col->type) )
130 if( col->type & MSITYPE_STRING )
131 return db->bytes_per_strref;
132 if( (col->type & 0xff) > 4 )
133 ERR("Invalid column size!\n");
134 return col->type & 0xff;
137 static int utf2mime(int x)
139 if( (x>='0') && (x<='9') )
141 if( (x>='A') && (x<='Z') )
143 if( (x>='a') && (x<='z') )
152 static LPWSTR encode_streamname(BOOL bTable, LPCWSTR in)
154 DWORD count = MAX_STREAM_NAME;
159 count = lstrlenW( in )+2;
160 out = msi_alloc( count*sizeof(WCHAR) );
176 if( ( ch < 0x80 ) && ( utf2mime(ch) >= 0 ) )
178 ch = utf2mime(ch) + 0x4800;
180 if( next && (next<0x80) )
182 next = utf2mime(next);
193 ERR("Failed to encode stream name (%s)\n",debugstr_w(in));
198 static int mime2utf(int x)
205 return x - 10 - 26 + 'a';
206 if( x == (10+26+26) )
211 BOOL decode_streamname(LPCWSTR in, LPWSTR out)
216 while ( (ch = *in++) )
218 if( (ch >= 0x3800 ) && (ch < 0x4840 ) )
221 ch = mime2utf(ch-0x4800);
225 *out++ = mime2utf(ch&0x3f);
227 ch = mime2utf((ch>>6)&0x3f);
237 void enum_stream_names( IStorage *stg )
239 IEnumSTATSTG *stgenum = NULL;
245 r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
253 r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
254 if( FAILED( r ) || !count )
256 decode_streamname( stat.pwcsName, name );
257 TRACE("stream %2d -> %s %s\n", n,
258 debugstr_w(stat.pwcsName), debugstr_w(name) );
259 CoTaskMemFree( stat.pwcsName );
263 IEnumSTATSTG_Release( stgenum );
266 UINT read_stream_data( IStorage *stg, LPCWSTR stname, BOOL table,
267 BYTE **pdata, UINT *psz )
270 UINT ret = ERROR_FUNCTION_FAILED;
277 encname = encode_streamname(table, stname);
279 TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
281 r = IStorage_OpenStream(stg, encname, NULL,
282 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stm);
286 WARN("open stream failed r = %08x - empty table?\n", r);
290 r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
293 WARN("open stream failed r = %08x!\n", r);
297 if( stat.cbSize.QuadPart >> 32 )
303 sz = stat.cbSize.QuadPart;
304 data = msi_alloc( sz );
307 WARN("couldn't allocate memory r=%08x!\n", r);
308 ret = ERROR_NOT_ENOUGH_MEMORY;
312 r = IStream_Read(stm, data, sz, &count );
313 if( FAILED( r ) || ( count != sz ) )
316 WARN("read stream failed r = %08x!\n", r);
325 IStream_Release( stm );
330 static UINT db_get_raw_stream( MSIDATABASE *db, LPCWSTR stname, IStream **stm )
335 encname = encode_streamname(FALSE, stname);
337 TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
339 r = IStorage_OpenStream(db->storage, encname, NULL,
340 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, stm);
343 MSITRANSFORM *transform;
345 LIST_FOR_EACH_ENTRY( transform, &db->transforms, MSITRANSFORM, entry )
347 TRACE("looking for %s in transform storage\n", debugstr_w(stname) );
348 r = IStorage_OpenStream( transform->stg, encname, NULL,
349 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, stm );
357 return SUCCEEDED(r) ? ERROR_SUCCESS : ERROR_FUNCTION_FAILED;
360 UINT read_raw_stream_data( MSIDATABASE *db, LPCWSTR stname,
361 USHORT **pdata, UINT *psz )
364 UINT ret = ERROR_FUNCTION_FAILED;
370 r = db_get_raw_stream( db, stname, &stm );
371 if( r != ERROR_SUCCESS)
373 r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
376 WARN("open stream failed r = %08x!\n", r);
380 if( stat.cbSize.QuadPart >> 32 )
386 sz = stat.cbSize.QuadPart;
387 data = msi_alloc( sz );
390 WARN("couldn't allocate memory r=%08x!\n", r);
391 ret = ERROR_NOT_ENOUGH_MEMORY;
395 r = IStream_Read(stm, data, sz, &count );
396 if( FAILED( r ) || ( count != sz ) )
399 WARN("read stream failed r = %08x!\n", r);
408 IStream_Release( stm );
413 UINT write_stream_data( IStorage *stg, LPCWSTR stname,
414 LPCVOID data, UINT sz, BOOL bTable )
417 UINT ret = ERROR_FUNCTION_FAILED;
424 encname = encode_streamname(bTable, stname );
425 r = IStorage_OpenStream( stg, encname, NULL,
426 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, &stm);
429 r = IStorage_CreateStream( stg, encname,
430 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
435 WARN("open stream failed r = %08x\n", r);
440 r = IStream_SetSize( stm, size );
443 WARN("Failed to SetSize\n");
448 r = IStream_Seek( stm, pos, STREAM_SEEK_SET, NULL );
451 WARN("Failed to Seek\n");
457 r = IStream_Write(stm, data, sz, &count );
458 if( FAILED( r ) || ( count != sz ) )
460 WARN("Failed to Write\n");
468 IStream_Release( stm );
473 static void free_table( MSITABLE *table )
476 for( i=0; i<table->row_count; i++ )
477 msi_free( table->data[i] );
478 msi_free( table->data );
479 msi_free( table->data_persistent );
480 msi_free_colinfo( table->colinfo, table->col_count );
481 msi_free( table->colinfo );
485 static UINT msi_table_get_row_size( MSIDATABASE *db,const MSICOLUMNINFO *cols,
488 const MSICOLUMNINFO *last_col = &cols[count-1];
491 return last_col->offset + bytes_per_column( db, last_col );
494 /* add this table to the list of cached tables in the database */
495 static UINT read_table_from_storage( MSIDATABASE *db, MSITABLE *t, IStorage *stg )
497 BYTE *rawdata = NULL;
498 UINT rawsize = 0, i, j, row_size = 0;
500 TRACE("%s\n",debugstr_w(t->name));
502 row_size = msi_table_get_row_size( db, t->colinfo, t->col_count );
504 /* if we can't read the table, just assume that it's empty */
505 read_stream_data( stg, t->name, TRUE, &rawdata, &rawsize );
507 return ERROR_SUCCESS;
509 TRACE("Read %d bytes\n", rawsize );
511 if( rawsize % row_size )
513 WARN("Table size is invalid %d/%d\n", rawsize, row_size );
517 t->row_count = rawsize / row_size;
518 t->data = msi_alloc_zero( t->row_count * sizeof (USHORT*) );
521 t->data_persistent = msi_alloc_zero( t->row_count * sizeof(BOOL));
522 if ( !t->data_persistent )
525 /* transpose all the data */
526 TRACE("Transposing data from %d rows\n", t->row_count );
527 for( i=0; i<t->row_count; i++ )
529 t->data[i] = msi_alloc( row_size );
532 t->data_persistent[i] = TRUE;
534 for( j=0; j<t->col_count; j++ )
536 UINT ofs = t->colinfo[j].offset;
537 UINT n = bytes_per_column( db, &t->colinfo[j] );
540 if ( n != 2 && n != 3 && n != 4 )
542 ERR("oops - unknown column width %d\n", n);
546 for ( k = 0; k < n; k++ )
547 t->data[i][ofs + k] = rawdata[ofs*t->row_count + i * n + k];
552 return ERROR_SUCCESS;
555 return ERROR_FUNCTION_FAILED;
558 void free_cached_tables( MSIDATABASE *db )
560 while( !list_empty( &db->tables ) )
562 MSITABLE *t = LIST_ENTRY( list_head( &db->tables ), MSITABLE, entry );
564 list_remove( &t->entry );
569 static MSITABLE *find_cached_table( MSIDATABASE *db, LPCWSTR name )
573 LIST_FOR_EACH_ENTRY( t, &db->tables, MSITABLE, entry )
574 if( !lstrcmpW( name, t->name ) )
580 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name, MSICOLUMNINFO **pcols, UINT *pcount )
582 UINT r, column_count = 0;
583 MSICOLUMNINFO *columns;
585 /* get the number of columns in this table */
587 r = get_tablecolumns( db, name, NULL, &column_count );
588 if( r != ERROR_SUCCESS )
591 *pcount = column_count;
593 /* if there's no columns, there's no table */
594 if( column_count == 0 )
595 return ERROR_INVALID_PARAMETER;
597 TRACE("Table %s found\n", debugstr_w(name) );
599 columns = msi_alloc( column_count*sizeof (MSICOLUMNINFO) );
601 return ERROR_FUNCTION_FAILED;
603 r = get_tablecolumns( db, name, columns, &column_count );
604 if( r != ERROR_SUCCESS )
607 return ERROR_FUNCTION_FAILED;
615 UINT msi_create_table( MSIDATABASE *db, LPCWSTR name, column_info *col_info,
616 MSICONDITION persistent, MSITABLE **table_ret)
620 MSIRECORD *rec = NULL;
626 /* only add tables that don't exist already */
627 if( TABLE_Exists(db, name ) )
629 WARN("table %s exists\n", debugstr_w(name));
630 return ERROR_BAD_QUERY_SYNTAX;
633 table = msi_alloc( sizeof (MSITABLE) + lstrlenW(name)*sizeof (WCHAR) );
635 return ERROR_FUNCTION_FAILED;
637 table->ref_count = 1;
638 table->row_count = 0;
640 table->data_persistent = NULL;
641 table->colinfo = NULL;
642 table->col_count = 0;
643 table->persistent = persistent;
644 lstrcpyW( table->name, name );
646 for( col = col_info; col; col = col->next )
649 table->colinfo = msi_alloc( table->col_count * sizeof(MSICOLUMNINFO) );
653 return ERROR_FUNCTION_FAILED;
656 for( i = 0, col = col_info; col; i++, col = col->next )
658 table->colinfo[ i ].tablename = strdupW( col->table );
659 table->colinfo[ i ].number = i + 1;
660 table->colinfo[ i ].colname = strdupW( col->column );
661 table->colinfo[ i ].type = col->type;
662 table->colinfo[ i ].offset = 0;
663 table->colinfo[ i ].ref_count = 0;
664 table->colinfo[ i ].hash_table = NULL;
665 table->colinfo[ i ].temporary = col->temporary;
667 table_calc_column_offsets( db, table->colinfo, table->col_count);
669 r = TABLE_CreateView( db, szTables, &tv );
670 TRACE("CreateView returned %x\n", r);
677 r = tv->ops->execute( tv, 0 );
678 TRACE("tv execute returned %x\n", r);
682 rec = MSI_CreateRecord( 1 );
686 r = MSI_RecordSetStringW( rec, 1, name );
690 r = table_find_insert_idx (tv, name, &idx);
691 if (r != ERROR_SUCCESS)
694 r = tv->ops->insert_row( tv, rec, idx, persistent == MSICONDITION_FALSE );
695 TRACE("insert_row returned %x\n", r);
699 tv->ops->delete( tv );
702 msiobj_release( &rec->hdr );
705 if( persistent != MSICONDITION_FALSE )
707 /* add each column to the _Columns table */
708 r = TABLE_CreateView( db, szColumns, &tv );
712 r = tv->ops->execute( tv, 0 );
713 TRACE("tv execute returned %x\n", r);
717 rec = MSI_CreateRecord( 4 );
721 r = MSI_RecordSetStringW( rec, 1, name );
726 * need to set the table, column number, col name and type
727 * for each column we enter in the table
730 for( col = col_info; col; col = col->next )
732 r = MSI_RecordSetInteger( rec, 2, nField );
736 r = MSI_RecordSetStringW( rec, 3, col->column );
740 r = MSI_RecordSetInteger( rec, 4, col->type );
744 r = table_find_insert_idx (tv, name, &idx);
745 if (r != ERROR_SUCCESS)
748 r = tv->ops->insert_row( tv, rec, idx, FALSE );
760 msiobj_release( &rec->hdr );
761 /* FIXME: remove values from the string table on error */
763 tv->ops->delete( tv );
765 if (r == ERROR_SUCCESS)
767 list_add_head( &db->tables, &table->entry );
776 static UINT get_table( MSIDATABASE *db, LPCWSTR name, MSITABLE **table_ret )
781 /* first, see if the table is cached */
782 table = find_cached_table( db, name );
786 return ERROR_SUCCESS;
789 /* nonexistent tables should be interpreted as empty tables */
790 table = msi_alloc( sizeof (MSITABLE) + lstrlenW(name)*sizeof (WCHAR) );
792 return ERROR_FUNCTION_FAILED;
794 table->row_count = 0;
796 table->data_persistent = NULL;
797 table->colinfo = NULL;
798 table->col_count = 0;
799 table->persistent = MSICONDITION_TRUE;
800 lstrcpyW( table->name, name );
802 if ( !lstrcmpW(name, szTables) || !lstrcmpW(name, szColumns) )
803 table->persistent = MSICONDITION_NONE;
805 r = table_get_column_info( db, name, &table->colinfo, &table->col_count);
806 if (r != ERROR_SUCCESS)
808 free_table ( table );
812 r = read_table_from_storage( db, table, db->storage );
813 if( r != ERROR_SUCCESS )
819 list_add_head( &db->tables, &table->entry );
821 return ERROR_SUCCESS;
824 static UINT save_table( MSIDATABASE *db, const MSITABLE *t )
826 BYTE *rawdata = NULL, *p;
827 UINT rawsize, r, i, j, row_size;
829 /* Nothing to do for non-persistent tables */
830 if( t->persistent == MSICONDITION_FALSE )
831 return ERROR_SUCCESS;
833 TRACE("Saving %s\n", debugstr_w( t->name ) );
835 row_size = msi_table_get_row_size( db, t->colinfo, t->col_count );
837 rawsize = t->row_count * row_size;
838 rawdata = msi_alloc_zero( rawsize );
841 r = ERROR_NOT_ENOUGH_MEMORY;
847 for( i=0; i<t->col_count; i++ )
849 for( j=0; j<t->row_count; j++ )
851 UINT offset = t->colinfo[i].offset;
853 if (!t->data_persistent[j]) continue;
857 *p++ = t->data[j][offset];
858 *p++ = t->data[j][offset + 1];
859 if( 4 == bytes_per_column( db, &t->colinfo[i] ) )
861 *p++ = t->data[j][offset + 2];
862 *p++ = t->data[j][offset + 3];
867 TRACE("writing %d bytes\n", rawsize);
868 r = write_stream_data( db->storage, t->name, rawdata, rawsize, TRUE );
876 static void table_calc_column_offsets( MSIDATABASE *db, MSICOLUMNINFO *colinfo,
881 for( i=0; colinfo && (i<count); i++ )
883 assert( (i+1) == colinfo[ i ].number );
885 colinfo[i].offset = colinfo[ i - 1 ].offset
886 + bytes_per_column( db, &colinfo[ i - 1 ] );
888 colinfo[i].offset = 0;
889 TRACE("column %d is [%s] with type %08x ofs %d\n",
890 colinfo[i].number, debugstr_w(colinfo[i].colname),
891 colinfo[i].type, colinfo[i].offset);
895 static UINT get_defaulttablecolumns( MSIDATABASE *db, LPCWSTR name,
896 MSICOLUMNINFO *colinfo, UINT *sz)
898 const MSICOLUMNINFO *p;
901 TRACE("%s\n", debugstr_w(name));
903 if (!lstrcmpW( name, szTables ))
908 else if (!lstrcmpW( name, szColumns ))
914 return ERROR_FUNCTION_FAILED;
916 /* duplicate the string data so we can free it in msi_free_colinfo */
919 if (colinfo && (i < *sz) )
922 colinfo[i].tablename = strdupW( p[i].tablename );
923 colinfo[i].colname = strdupW( p[i].colname );
925 if( colinfo && (i >= *sz) )
928 table_calc_column_offsets( db, colinfo, n );
930 return ERROR_SUCCESS;
933 static void msi_free_colinfo( MSICOLUMNINFO *colinfo, UINT count )
937 for( i=0; i<count; i++ )
939 msi_free( colinfo[i].tablename );
940 msi_free( colinfo[i].colname );
941 msi_free( colinfo[i].hash_table );
945 static LPWSTR msi_makestring( const MSIDATABASE *db, UINT stringid)
947 return strdupW(msi_string_lookup_id( db->strings, stringid ));
950 static UINT read_table_int(BYTE *const *data, UINT row, UINT col, UINT bytes)
954 for (i = 0; i < bytes; i++)
955 ret += (data[row][col + i] << i * 8);
960 static UINT get_tablecolumns( MSIDATABASE *db,
961 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz)
963 UINT r, i, n=0, table_id, count, maxcount = *sz;
964 MSITABLE *table = NULL;
966 TRACE("%s\n", debugstr_w(szTableName));
968 /* first check if there is a default table with that name */
969 r = get_defaulttablecolumns( db, szTableName, colinfo, sz );
970 if( ( r == ERROR_SUCCESS ) && *sz )
973 r = get_table( db, szColumns, &table );
974 if( r != ERROR_SUCCESS )
976 ERR("couldn't load _Columns table\n");
977 return ERROR_FUNCTION_FAILED;
980 /* convert table and column names to IDs from the string table */
981 r = msi_string2idW( db->strings, szTableName, &table_id );
982 if( r != ERROR_SUCCESS )
984 WARN("Couldn't find id for %s\n", debugstr_w(szTableName));
988 TRACE("Table id is %d, row count is %d\n", table_id, table->row_count);
990 /* Note: _Columns table doesn't have non-persistent data */
992 /* if maxcount is non-zero, assume it's exactly right for this table */
993 memset( colinfo, 0, maxcount*sizeof(*colinfo) );
994 count = table->row_count;
995 for( i=0; i<count; i++ )
997 if( read_table_int(table->data, i, 0, db->bytes_per_strref) != table_id )
1001 UINT id = read_table_int(table->data, i, table->colinfo[2].offset, db->bytes_per_strref);
1002 UINT col = read_table_int(table->data, i, table->colinfo[1].offset, sizeof(USHORT)) - (1<<15);
1004 /* check the column number is in range */
1005 if (col<1 || col>maxcount)
1007 ERR("column %d out of range\n", col);
1011 /* check if this column was already set */
1012 if (colinfo[ col - 1 ].number)
1014 ERR("duplicate column %d\n", col);
1018 colinfo[ col - 1 ].tablename = msi_makestring( db, table_id );
1019 colinfo[ col - 1 ].number = col;
1020 colinfo[ col - 1 ].colname = msi_makestring( db, id );
1021 colinfo[ col - 1 ].type = read_table_int(table->data, i,
1022 table->colinfo[3].offset,
1023 sizeof(USHORT)) - (1<<15);
1024 colinfo[ col - 1 ].offset = 0;
1025 colinfo[ col - 1 ].ref_count = 0;
1026 colinfo[ col - 1 ].hash_table = NULL;
1031 TRACE("%s has %d columns\n", debugstr_w(szTableName), n);
1033 if (colinfo && n != maxcount)
1035 ERR("missing column in table %s\n", debugstr_w(szTableName));
1036 msi_free_colinfo(colinfo, maxcount );
1037 return ERROR_FUNCTION_FAILED;
1040 table_calc_column_offsets( db, colinfo, n );
1043 return ERROR_SUCCESS;
1046 static void msi_update_table_columns( MSIDATABASE *db, LPCWSTR name )
1049 UINT size, offset, old_count;
1052 table = find_cached_table( db, name );
1053 old_count = table->col_count;
1054 msi_free( table->colinfo );
1055 table->colinfo = NULL;
1057 table_get_column_info( db, name, &table->colinfo, &table->col_count );
1058 if (!table->col_count)
1061 size = msi_table_get_row_size( db, table->colinfo, table->col_count );
1062 offset = table->colinfo[table->col_count - 1].offset;
1064 for ( n = 0; n < table->row_count; n++ )
1066 table->data[n] = msi_realloc( table->data[n], size );
1067 if (old_count < table->col_count)
1068 memset( &table->data[n][offset], 0, size - offset );
1072 /* try to find the table name in the _Tables table */
1073 BOOL TABLE_Exists( MSIDATABASE *db, LPCWSTR name )
1075 UINT r, table_id = 0, i, count;
1076 MSITABLE *table = NULL;
1078 static const WCHAR szStreams[] = {'_','S','t','r','e','a','m','s',0};
1079 static const WCHAR szStorages[] = {'_','S','t','o','r','a','g','e','s',0};
1081 if( !lstrcmpW( name, szTables ) || !lstrcmpW( name, szColumns ) ||
1082 !lstrcmpW( name, szStreams ) || !lstrcmpW( name, szStorages ) )
1085 r = msi_string2idW( db->strings, name, &table_id );
1086 if( r != ERROR_SUCCESS )
1088 TRACE("Couldn't find id for %s\n", debugstr_w(name));
1092 r = get_table( db, szTables, &table );
1093 if( r != ERROR_SUCCESS )
1095 ERR("table %s not available\n", debugstr_w(szTables));
1099 count = table->row_count;
1100 for( i=0; i<count; i++ )
1101 if( table->data[ i ][ 0 ] == table_id )
1107 /* below is the query interface to a table */
1109 typedef struct tagMSITABLEVIEW
1114 MSICOLUMNINFO *columns;
1115 MSIORDERINFO *order;
1121 static UINT TABLE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
1123 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1127 return ERROR_INVALID_PARAMETER;
1129 if( (col==0) || (col>tv->num_cols) )
1130 return ERROR_INVALID_PARAMETER;
1132 /* how many rows are there ? */
1133 if( row >= tv->table->row_count )
1134 return ERROR_NO_MORE_ITEMS;
1136 if( tv->columns[col-1].offset >= tv->row_size )
1138 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1139 ERR("%p %p\n", tv, tv->columns );
1140 return ERROR_FUNCTION_FAILED;
1144 row = tv->order->reorder[row];
1146 n = bytes_per_column( tv->db, &tv->columns[col-1] );
1147 if (n != 2 && n != 3 && n != 4)
1149 ERR("oops! what is %d bytes per column?\n", n );
1150 return ERROR_FUNCTION_FAILED;
1153 offset = tv->columns[col-1].offset;
1154 *val = read_table_int(tv->table->data, row, offset, n);
1156 /* TRACE("Data [%d][%d] = %d\n", row, col, *val ); */
1158 return ERROR_SUCCESS;
1161 static UINT msi_stream_name( const MSITABLEVIEW *tv, UINT row, LPWSTR *pstname )
1163 LPWSTR p, stname = NULL;
1164 UINT i, r, type, ival;
1167 MSIVIEW *view = (MSIVIEW *) tv;
1169 TRACE("%p %d\n", tv, row);
1171 len = lstrlenW( tv->name ) + 1;
1172 stname = msi_alloc( len*sizeof(WCHAR) );
1175 r = ERROR_OUTOFMEMORY;
1179 lstrcpyW( stname, tv->name );
1181 for ( i = 0; i < tv->num_cols; i++ )
1183 type = tv->columns[i].type;
1184 if ( type & MSITYPE_KEY )
1186 static const WCHAR szDot[] = { '.', 0 };
1188 r = TABLE_fetch_int( view, row, i+1, &ival );
1189 if ( r != ERROR_SUCCESS )
1192 if ( tv->columns[i].type & MSITYPE_STRING )
1194 sval = msi_string_lookup_id( tv->db->strings, ival );
1197 r = ERROR_INVALID_PARAMETER;
1203 static const WCHAR fmt[] = { '%','d',0 };
1205 UINT n = bytes_per_column( tv->db, &tv->columns[i] );
1210 sprintfW( number, fmt, ival^0x8000 );
1213 sprintfW( number, fmt, ival^0x80000000 );
1216 ERR( "oops - unknown column width %d\n", n );
1217 r = ERROR_FUNCTION_FAILED;
1223 len += lstrlenW( szDot ) + lstrlenW( sval );
1224 p = msi_realloc ( stname, len*sizeof(WCHAR) );
1227 r = ERROR_OUTOFMEMORY;
1232 lstrcatW( stname, szDot );
1233 lstrcatW( stname, sval );
1240 return ERROR_SUCCESS;
1249 * We need a special case for streams, as we need to reference column with
1250 * the name of the stream in the same table, and the table name
1251 * which may not be available at higher levels of the query
1253 static UINT TABLE_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm )
1255 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1257 LPWSTR full_name = NULL;
1259 if( !view->ops->fetch_int )
1260 return ERROR_INVALID_PARAMETER;
1262 r = msi_stream_name( tv, row, &full_name );
1263 if ( r != ERROR_SUCCESS )
1265 ERR("fetching stream, error = %d\n", r);
1269 r = db_get_raw_stream( tv->db, full_name, stm );
1271 ERR("fetching stream %s, error = %d\n",debugstr_w(full_name), r);
1272 msi_free( full_name );
1277 static UINT TABLE_set_int( MSITABLEVIEW *tv, UINT row, UINT col, UINT val )
1282 return ERROR_INVALID_PARAMETER;
1284 if( (col==0) || (col>tv->num_cols) )
1285 return ERROR_INVALID_PARAMETER;
1287 if( row >= tv->table->row_count )
1288 return ERROR_INVALID_PARAMETER;
1290 if( tv->columns[col-1].offset >= tv->row_size )
1292 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1293 ERR("%p %p\n", tv, tv->columns );
1294 return ERROR_FUNCTION_FAILED;
1297 msi_free( tv->columns[col-1].hash_table );
1298 tv->columns[col-1].hash_table = NULL;
1300 n = bytes_per_column( tv->db, &tv->columns[col-1] );
1301 if ( n != 2 && n != 3 && n != 4 )
1303 ERR("oops! what is %d bytes per column?\n", n );
1304 return ERROR_FUNCTION_FAILED;
1307 offset = tv->columns[col-1].offset;
1308 for ( i = 0; i < n; i++ )
1309 tv->table->data[row][offset + i] = (val >> i * 8) & 0xff;
1311 return ERROR_SUCCESS;
1314 static UINT TABLE_get_row( struct tagMSIVIEW *view, UINT row, MSIRECORD **rec )
1316 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1319 return ERROR_INVALID_PARAMETER;
1322 row = tv->order->reorder[row];
1324 return msi_view_get_row(tv->db, view, row, rec);
1327 static UINT msi_addstreamW( MSIDATABASE *db, LPCWSTR name, IStream *data )
1330 MSIQUERY *query = NULL;
1331 MSIRECORD *rec = NULL;
1333 static const WCHAR insert[] = {
1334 'I','N','S','E','R','T',' ','I','N','T','O',' ',
1335 '`','_','S','t','r','e','a','m','s','`',' ',
1336 '(','`','N','a','m','e','`',',',
1337 '`','D','a','t','a','`',')',' ',
1338 'V','A','L','U','E','S',' ','(','?',',','?',')',0};
1340 TRACE("%p %s %p\n", db, debugstr_w(name), data);
1342 rec = MSI_CreateRecord( 2 );
1344 return ERROR_OUTOFMEMORY;
1346 r = MSI_RecordSetStringW( rec, 1, name );
1347 if ( r != ERROR_SUCCESS )
1350 r = MSI_RecordSetIStream( rec, 2, data );
1351 if ( r != ERROR_SUCCESS )
1354 r = MSI_DatabaseOpenViewW( db, insert, &query );
1355 if ( r != ERROR_SUCCESS )
1358 r = MSI_ViewExecute( query, rec );
1361 msiobj_release( &query->hdr );
1362 msiobj_release( &rec->hdr );
1367 static UINT TABLE_set_row( struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask )
1369 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1370 UINT i, val, r = ERROR_SUCCESS;
1373 return ERROR_INVALID_PARAMETER;
1375 /* test if any of the mask bits are invalid */
1376 if ( mask >= (1<<tv->num_cols) )
1377 return ERROR_INVALID_PARAMETER;
1379 for ( i = 0; i < tv->num_cols; i++ )
1383 /* only update the fields specified in the mask */
1384 if ( !(mask&(1<<i)) )
1387 persistent = (tv->table->persistent != MSICONDITION_FALSE) &&
1388 (tv->table->data_persistent[row]);
1389 /* FIXME: should we allow updating keys? */
1392 if ( !MSI_RecordIsNull( rec, i + 1 ) )
1394 if ( MSITYPE_IS_BINARY(tv->columns[ i ].type) )
1399 r = MSI_RecordGetIStream( rec, i + 1, &stm );
1400 if ( r != ERROR_SUCCESS )
1403 r = msi_stream_name( tv, row, &stname );
1404 if ( r != ERROR_SUCCESS )
1406 IStream_Release( stm );
1410 r = msi_addstreamW( tv->db, stname, stm );
1411 IStream_Release( stm );
1412 msi_free ( stname );
1414 if ( r != ERROR_SUCCESS )
1417 val = 1; /* refers to the first key column */
1419 else if ( tv->columns[i].type & MSITYPE_STRING )
1421 LPCWSTR sval = MSI_RecordGetString( rec, i + 1 );
1424 r = msi_string2idW(tv->db->strings, sval, &ival);
1425 if (r == ERROR_SUCCESS)
1427 TABLE_fetch_int(&tv->view, row, i + 1, &x);
1432 val = msi_addstringW( tv->db->strings, 0, sval, -1, 1,
1433 persistent ? StringPersistent : StringNonPersistent );
1435 else if ( 2 == bytes_per_column( tv->db, &tv->columns[ i ] ) )
1437 val = 0x8000 + MSI_RecordGetInteger( rec, i + 1 );
1438 if ( val & 0xffff0000 )
1440 ERR("field %u value %d out of range\n", i+1, val - 0x8000 );
1441 return ERROR_FUNCTION_FAILED;
1446 INT ival = MSI_RecordGetInteger( rec, i + 1 );
1447 val = ival ^ 0x80000000;
1451 r = TABLE_set_int( tv, row, i+1, val );
1452 if ( r != ERROR_SUCCESS )
1458 static UINT table_create_new_row( struct tagMSIVIEW *view, UINT *num, BOOL temporary )
1460 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1465 BOOL **data_persist_ptr;
1468 TRACE("%p %s\n", view, temporary ? "TRUE" : "FALSE");
1471 return ERROR_INVALID_PARAMETER;
1473 row = msi_alloc_zero( tv->row_size );
1475 return ERROR_NOT_ENOUGH_MEMORY;
1477 row_count = &tv->table->row_count;
1478 data_ptr = &tv->table->data;
1479 data_persist_ptr = &tv->table->data_persistent;
1481 *num = tv->table->row_count;
1483 sz = (*row_count + 1) * sizeof (BYTE*);
1485 p = msi_realloc( *data_ptr, sz );
1487 p = msi_alloc( sz );
1491 return ERROR_NOT_ENOUGH_MEMORY;
1494 sz = (*row_count + 1) * sizeof (BOOL);
1495 if( *data_persist_ptr )
1496 b = msi_realloc( *data_persist_ptr, sz );
1498 b = msi_alloc( sz );
1503 return ERROR_NOT_ENOUGH_MEMORY;
1507 (*data_ptr)[*row_count] = row;
1509 *data_persist_ptr = b;
1510 (*data_persist_ptr)[*row_count] = !temporary;
1514 return ERROR_SUCCESS;
1517 static UINT TABLE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
1519 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1521 TRACE("%p %p\n", tv, record);
1523 TRACE("There are %d columns\n", tv->num_cols );
1525 return ERROR_SUCCESS;
1528 static UINT TABLE_close( struct tagMSIVIEW *view )
1530 TRACE("%p\n", view );
1532 return ERROR_SUCCESS;
1535 static UINT TABLE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols)
1537 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1539 TRACE("%p %p %p\n", view, rows, cols );
1542 *cols = tv->num_cols;
1546 return ERROR_INVALID_PARAMETER;
1547 *rows = tv->table->row_count;
1550 return ERROR_SUCCESS;
1553 static UINT TABLE_get_column_info( struct tagMSIVIEW *view,
1554 UINT n, LPWSTR *name, UINT *type, BOOL *temporary )
1556 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1558 TRACE("%p %d %p %p\n", tv, n, name, type );
1560 if( ( n == 0 ) || ( n > tv->num_cols ) )
1561 return ERROR_INVALID_PARAMETER;
1565 *name = strdupW( tv->columns[n-1].colname );
1567 return ERROR_FUNCTION_FAILED;
1571 *type = tv->columns[n-1].type;
1574 *temporary = tv->columns[n-1].temporary;
1576 return ERROR_SUCCESS;
1579 static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row );
1581 static UINT table_validate_new( MSITABLEVIEW *tv, MSIRECORD *rec )
1585 /* check there's no null values where they're not allowed */
1586 for( i = 0; i < tv->num_cols; i++ )
1588 if ( tv->columns[i].type & MSITYPE_NULLABLE )
1591 if ( MSITYPE_IS_BINARY(tv->columns[i].type) )
1592 TRACE("skipping binary column\n");
1593 else if ( tv->columns[i].type & MSITYPE_STRING )
1597 str = MSI_RecordGetString( rec, i+1 );
1598 if (str == NULL || str[0] == 0)
1599 return ERROR_INVALID_DATA;
1605 n = MSI_RecordGetInteger( rec, i+1 );
1606 if (n == MSI_NULL_INTEGER)
1607 return ERROR_INVALID_DATA;
1611 /* check there's no duplicate keys */
1612 r = msi_table_find_row( tv, rec, &row );
1613 if (r == ERROR_SUCCESS)
1614 return ERROR_FUNCTION_FAILED;
1616 return ERROR_SUCCESS;
1619 static UINT TABLE_insert_row( struct tagMSIVIEW *view, MSIRECORD *rec, UINT row, BOOL temporary )
1621 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1624 TRACE("%p %p %s\n", tv, rec, temporary ? "TRUE" : "FALSE" );
1626 /* check that the key is unique - can we find a matching row? */
1627 r = table_validate_new( tv, rec );
1628 if( r != ERROR_SUCCESS )
1629 return ERROR_FUNCTION_FAILED;
1631 r = table_create_new_row( view, &row, temporary );
1632 TRACE("insert_row returned %08x\n", r);
1633 if( r != ERROR_SUCCESS )
1636 /* shift the rows to make room for the new row */
1637 for (i = tv->table->row_count - 1; i > row; i--)
1639 memmove(&(tv->table->data[i][0]),
1640 &(tv->table->data[i - 1][0]), tv->row_size);
1641 tv->table->data_persistent[i] = tv->table->data_persistent[i - 1];
1644 /* Re-set the persistence flag */
1645 tv->table->data_persistent[row] = !temporary;
1646 return TABLE_set_row( view, row, rec, (1<<tv->num_cols) - 1 );
1649 static UINT TABLE_delete_row( struct tagMSIVIEW *view, UINT row )
1651 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1652 UINT r, num_rows, num_cols, i;
1654 TRACE("%p %d\n", tv, row);
1657 return ERROR_INVALID_PARAMETER;
1659 r = TABLE_get_dimensions( view, &num_rows, &num_cols );
1660 if ( r != ERROR_SUCCESS )
1663 if ( row >= num_rows )
1664 return ERROR_FUNCTION_FAILED;
1666 num_rows = tv->table->row_count;
1667 tv->table->row_count--;
1669 /* reset the hash tables */
1670 for (i = 0; i < tv->num_cols; i++)
1672 msi_free( tv->columns[i].hash_table );
1673 tv->columns[i].hash_table = NULL;
1676 if ( row == num_rows - 1 )
1677 return ERROR_SUCCESS;
1679 for (i = row + 1; i < num_rows; i++)
1681 memcpy(tv->table->data[i - 1], tv->table->data[i], tv->row_size);
1682 tv->table->data_persistent[i - 1] = tv->table->data_persistent[i];
1685 return ERROR_SUCCESS;
1688 static UINT msi_table_update(struct tagMSIVIEW *view, MSIRECORD *rec, UINT row)
1690 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1693 /* FIXME: MsiViewFetch should set rec index 0 to some ID that
1694 * sets the fetched record apart from other records
1698 return ERROR_INVALID_PARAMETER;
1700 r = msi_table_find_row(tv, rec, &new_row);
1701 if (r != ERROR_SUCCESS)
1703 ERR("can't find row to modify\n");
1704 return ERROR_FUNCTION_FAILED;
1707 /* the row cannot be changed */
1708 if (row != new_row + 1)
1709 return ERROR_FUNCTION_FAILED;
1711 return TABLE_set_row(view, new_row, rec, (1 << tv->num_cols) - 1);
1714 static UINT msi_table_assign(struct tagMSIVIEW *view, MSIRECORD *rec)
1716 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1720 return ERROR_INVALID_PARAMETER;
1722 r = msi_table_find_row(tv, rec, &row);
1723 if (r == ERROR_SUCCESS)
1724 return TABLE_set_row(view, row, rec, (1 << tv->num_cols) - 1);
1726 return TABLE_insert_row( view, rec, -1, FALSE );
1729 static UINT modify_delete_row( struct tagMSIVIEW *view, MSIRECORD *rec )
1731 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1734 r = msi_table_find_row(tv, rec, &row);
1735 if (r != ERROR_SUCCESS)
1738 return TABLE_delete_row(view, row);
1741 static UINT msi_refresh_record( struct tagMSIVIEW *view, MSIRECORD *rec, UINT row )
1746 r = TABLE_get_row(view, row - 1, &curr);
1747 if (r != ERROR_SUCCESS)
1750 count = MSI_RecordGetFieldCount(rec);
1751 for (i = 0; i < count; i++)
1752 MSI_RecordCopyField(curr, i + 1, rec, i + 1);
1754 msiobj_release(&curr->hdr);
1755 return ERROR_SUCCESS;
1758 static UINT TABLE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
1759 MSIRECORD *rec, UINT row)
1761 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1764 TRACE("%p %d %p\n", view, eModifyMode, rec );
1766 switch (eModifyMode)
1768 case MSIMODIFY_DELETE:
1769 r = modify_delete_row( view, rec );
1771 case MSIMODIFY_VALIDATE_NEW:
1772 r = table_validate_new( tv, rec );
1775 case MSIMODIFY_INSERT:
1776 r = table_validate_new( tv, rec );
1777 if (r != ERROR_SUCCESS)
1779 r = TABLE_insert_row( view, rec, -1, FALSE );
1782 case MSIMODIFY_INSERT_TEMPORARY:
1783 r = table_validate_new( tv, rec );
1784 if (r != ERROR_SUCCESS)
1786 r = TABLE_insert_row( view, rec, -1, TRUE );
1789 case MSIMODIFY_REFRESH:
1790 r = msi_refresh_record( view, rec, row );
1793 case MSIMODIFY_UPDATE:
1794 r = msi_table_update( view, rec, row );
1797 case MSIMODIFY_ASSIGN:
1798 r = msi_table_assign( view, rec );
1801 case MSIMODIFY_REPLACE:
1802 case MSIMODIFY_MERGE:
1803 case MSIMODIFY_VALIDATE:
1804 case MSIMODIFY_VALIDATE_FIELD:
1805 case MSIMODIFY_VALIDATE_DELETE:
1806 FIXME("%p %d %p - mode not implemented\n", view, eModifyMode, rec );
1807 r = ERROR_CALL_NOT_IMPLEMENTED;
1811 r = ERROR_INVALID_DATA;
1817 static UINT TABLE_delete( struct tagMSIVIEW *view )
1819 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1821 TRACE("%p\n", view );
1828 msi_free( tv->order->reorder );
1829 msi_free( tv->order );
1835 return ERROR_SUCCESS;
1838 static UINT TABLE_find_matching_rows( struct tagMSIVIEW *view, UINT col,
1839 UINT val, UINT *row, MSIITERHANDLE *handle )
1841 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1842 const MSICOLUMNHASHENTRY *entry;
1844 TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
1847 return ERROR_INVALID_PARAMETER;
1849 if( (col==0) || (col > tv->num_cols) )
1850 return ERROR_INVALID_PARAMETER;
1852 if( !tv->columns[col-1].hash_table )
1855 UINT num_rows = tv->table->row_count;
1856 MSICOLUMNHASHENTRY **hash_table;
1857 MSICOLUMNHASHENTRY *new_entry;
1859 if( tv->columns[col-1].offset >= tv->row_size )
1861 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1862 ERR("%p %p\n", tv, tv->columns );
1863 return ERROR_FUNCTION_FAILED;
1866 /* allocate contiguous memory for the table and its entries so we
1867 * don't have to do an expensive cleanup */
1868 hash_table = msi_alloc(MSITABLE_HASH_TABLE_SIZE * sizeof(MSICOLUMNHASHENTRY*) +
1869 num_rows * sizeof(MSICOLUMNHASHENTRY));
1871 return ERROR_OUTOFMEMORY;
1873 memset(hash_table, 0, MSITABLE_HASH_TABLE_SIZE * sizeof(MSICOLUMNHASHENTRY*));
1874 tv->columns[col-1].hash_table = hash_table;
1876 new_entry = (MSICOLUMNHASHENTRY *)(hash_table + MSITABLE_HASH_TABLE_SIZE);
1878 for (i = 0; i < num_rows; i++, new_entry++)
1882 if (view->ops->fetch_int( view, i, col, &row_value ) != ERROR_SUCCESS)
1885 new_entry->next = NULL;
1886 new_entry->value = row_value;
1888 if (hash_table[row_value % MSITABLE_HASH_TABLE_SIZE])
1890 MSICOLUMNHASHENTRY *prev_entry = hash_table[row_value % MSITABLE_HASH_TABLE_SIZE];
1891 while (prev_entry->next)
1892 prev_entry = prev_entry->next;
1893 prev_entry->next = new_entry;
1896 hash_table[row_value % MSITABLE_HASH_TABLE_SIZE] = new_entry;
1901 entry = tv->columns[col-1].hash_table[val % MSITABLE_HASH_TABLE_SIZE];
1903 entry = (*handle)->next;
1905 while (entry && entry->value != val)
1906 entry = entry->next;
1910 return ERROR_NO_MORE_ITEMS;
1914 return ERROR_SUCCESS;
1917 static UINT TABLE_add_ref(struct tagMSIVIEW *view)
1919 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1922 TRACE("%p %d\n", view, tv->table->ref_count);
1924 for (i = 0; i < tv->table->col_count; i++)
1926 if (tv->table->colinfo[i].type & MSITYPE_TEMPORARY)
1927 InterlockedIncrement(&tv->table->colinfo[i].ref_count);
1930 return InterlockedIncrement(&tv->table->ref_count);
1933 static UINT TABLE_remove_column(struct tagMSIVIEW *view, LPCWSTR table, UINT number)
1935 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1936 MSIRECORD *rec = NULL;
1937 MSIVIEW *columns = NULL;
1940 rec = MSI_CreateRecord(2);
1942 return ERROR_OUTOFMEMORY;
1944 MSI_RecordSetStringW(rec, 1, table);
1945 MSI_RecordSetInteger(rec, 2, number);
1947 r = TABLE_CreateView(tv->db, szColumns, &columns);
1948 if (r != ERROR_SUCCESS)
1951 r = msi_table_find_row((MSITABLEVIEW *)columns, rec, &row);
1952 if (r != ERROR_SUCCESS)
1955 r = TABLE_delete_row(columns, row);
1956 if (r != ERROR_SUCCESS)
1959 msi_update_table_columns(tv->db, table);
1962 msiobj_release(&rec->hdr);
1963 columns->ops->delete(columns);
1967 static UINT TABLE_release(struct tagMSIVIEW *view)
1969 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1970 INT ref = tv->table->ref_count;
1973 TRACE("%p %d\n", view, ref);
1975 for (i = 0; i < tv->table->col_count; i++)
1977 if (tv->table->colinfo[i].type & MSITYPE_TEMPORARY)
1979 ref = InterlockedDecrement(&tv->table->colinfo[i].ref_count);
1982 r = TABLE_remove_column(view, tv->table->colinfo[i].tablename,
1983 tv->table->colinfo[i].number);
1984 if (r != ERROR_SUCCESS)
1990 ref = InterlockedDecrement(&tv->table->ref_count);
1993 if (!tv->table->row_count)
1995 list_remove(&tv->table->entry);
1996 free_table(tv->table);
2004 static UINT TABLE_add_column(struct tagMSIVIEW *view, LPCWSTR table, UINT number,
2005 LPCWSTR column, UINT type, BOOL hold)
2007 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
2012 rec = MSI_CreateRecord(4);
2014 return ERROR_OUTOFMEMORY;
2016 MSI_RecordSetStringW(rec, 1, table);
2017 MSI_RecordSetInteger(rec, 2, number);
2018 MSI_RecordSetStringW(rec, 3, column);
2019 MSI_RecordSetInteger(rec, 4, type);
2021 r = TABLE_insert_row(&tv->view, rec, -1, FALSE);
2022 if (r != ERROR_SUCCESS)
2025 msi_update_table_columns(tv->db, table);
2030 msitable = find_cached_table(tv->db, table);
2031 for (i = 0; i < msitable->col_count; i++)
2033 if (!lstrcmpW(msitable->colinfo[i].colname, column))
2035 InterlockedIncrement(&msitable->colinfo[i].ref_count);
2041 msiobj_release(&rec->hdr);
2045 static UINT order_add_column(struct tagMSIVIEW *view, MSIORDERINFO *order, LPCWSTR name)
2049 r = TABLE_get_dimensions(view, NULL, &count);
2050 if (r != ERROR_SUCCESS)
2053 if (order->num_cols >= count)
2054 return ERROR_FUNCTION_FAILED;
2056 r = VIEW_find_column(view, name, &n);
2057 if (r != ERROR_SUCCESS)
2060 order->cols[order->num_cols] = n;
2061 TRACE("Ordering by column %s (%d)\n", debugstr_w(name), n);
2065 return ERROR_SUCCESS;
2068 static UINT order_compare(struct tagMSIVIEW *view, MSIORDERINFO *order,
2069 UINT a, UINT b, UINT *swap)
2071 UINT r, i, a_val = 0, b_val = 0;
2074 for (i = 0; i < order->num_cols; i++)
2076 r = TABLE_fetch_int(view, a, order->cols[i], &a_val);
2077 if (r != ERROR_SUCCESS)
2080 r = TABLE_fetch_int(view, b, order->cols[i], &b_val);
2081 if (r != ERROR_SUCCESS)
2092 return ERROR_SUCCESS;
2095 static UINT order_mergesort(struct tagMSIVIEW *view, MSIORDERINFO *order,
2096 UINT left, UINT right)
2099 UINT swap = 0, center = (left + right) / 2;
2100 UINT *array = order->reorder;
2103 return ERROR_SUCCESS;
2105 /* sort the left half */
2106 r = order_mergesort(view, order, left, center);
2107 if (r != ERROR_SUCCESS)
2110 /* sort the right half */
2111 r = order_mergesort(view, order, center + 1, right);
2112 if (r != ERROR_SUCCESS)
2115 for (i = left, j = center + 1; (i <= center) && (j <= right); i++)
2117 r = order_compare(view, order, array[i], array[j], &swap);
2118 if (r != ERROR_SUCCESS)
2124 memmove(&array[i + 1], &array[i], (j - i) * sizeof(UINT));
2131 return ERROR_SUCCESS;
2134 static UINT order_verify(struct tagMSIVIEW *view, MSIORDERINFO *order, UINT num_rows)
2138 for (i = 1; i < num_rows; i++)
2140 r = order_compare(view, order, order->reorder[i - 1],
2141 order->reorder[i], &swap);
2142 if (r != ERROR_SUCCESS)
2148 ERR("Bad order! %d\n", i);
2149 return ERROR_FUNCTION_FAILED;
2152 return ERROR_SUCCESS;
2155 static UINT TABLE_sort(struct tagMSIVIEW *view, column_info *columns)
2157 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
2158 MSIORDERINFO *order;
2163 TRACE("sorting table %s\n", debugstr_w(tv->name));
2165 r = TABLE_get_dimensions(view, &rows, &cols);
2166 if (r != ERROR_SUCCESS)
2170 return ERROR_SUCCESS;
2172 order = msi_alloc_zero(sizeof(MSIORDERINFO) + sizeof(UINT) * cols);
2174 return ERROR_OUTOFMEMORY;
2176 for (ptr = columns; ptr; ptr = ptr->next)
2177 order_add_column(view, order, ptr->column);
2179 order->reorder = msi_alloc(rows * sizeof(UINT));
2180 if (!order->reorder)
2181 return ERROR_OUTOFMEMORY;
2183 for (i = 0; i < rows; i++)
2184 order->reorder[i] = i;
2186 r = order_mergesort(view, order, 0, rows - 1);
2187 if (r != ERROR_SUCCESS)
2190 r = order_verify(view, order, rows);
2191 if (r != ERROR_SUCCESS)
2196 return ERROR_SUCCESS;
2199 static UINT TABLE_drop(struct tagMSIVIEW *view)
2201 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
2202 MSIVIEW *tables = NULL;
2203 MSIRECORD *rec = NULL;
2207 TRACE("dropping table %s\n", debugstr_w(tv->name));
2209 for (i = tv->table->col_count - 1; i >= 0; i--)
2211 r = TABLE_remove_column(view, tv->table->colinfo[i].tablename,
2212 tv->table->colinfo[i].number);
2213 if (r != ERROR_SUCCESS)
2217 rec = MSI_CreateRecord(1);
2219 return ERROR_OUTOFMEMORY;
2221 MSI_RecordSetStringW(rec, 1, tv->name);
2223 r = TABLE_CreateView(tv->db, szTables, &tables);
2224 if (r != ERROR_SUCCESS)
2227 r = msi_table_find_row((MSITABLEVIEW *)tables, rec, &row);
2228 if (r != ERROR_SUCCESS)
2231 r = TABLE_delete_row(tables, row);
2232 if (r != ERROR_SUCCESS)
2235 list_remove(&tv->table->entry);
2236 free_table(tv->table);
2240 msiobj_release(&rec->hdr);
2241 tables->ops->delete(tables);
2246 static const MSIVIEWOPS table_ops =
2256 TABLE_get_dimensions,
2257 TABLE_get_column_info,
2260 TABLE_find_matching_rows,
2264 TABLE_remove_column,
2269 UINT TABLE_CreateView( MSIDATABASE *db, LPCWSTR name, MSIVIEW **view )
2274 static const WCHAR Streams[] = {'_','S','t','r','e','a','m','s',0};
2275 static const WCHAR Storages[] = {'_','S','t','o','r','a','g','e','s',0};
2277 TRACE("%p %s %p\n", db, debugstr_w(name), view );
2279 if ( !lstrcmpW( name, Streams ) )
2280 return STREAMS_CreateView( db, view );
2281 else if ( !lstrcmpW( name, Storages ) )
2282 return STORAGES_CreateView( db, view );
2284 sz = sizeof *tv + lstrlenW(name)*sizeof name[0] ;
2285 tv = msi_alloc_zero( sz );
2287 return ERROR_FUNCTION_FAILED;
2289 r = get_table( db, name, &tv->table );
2290 if( r != ERROR_SUCCESS )
2293 WARN("table not found\n");
2297 TRACE("table %p found with %d columns\n", tv->table, tv->table->col_count);
2299 /* fill the structure */
2300 tv->view.ops = &table_ops;
2302 tv->columns = tv->table->colinfo;
2303 tv->num_cols = tv->table->col_count;
2304 tv->row_size = msi_table_get_row_size( db, tv->table->colinfo, tv->table->col_count );
2306 TRACE("%s one row is %d bytes\n", debugstr_w(name), tv->row_size );
2308 *view = (MSIVIEW*) tv;
2309 lstrcpyW( tv->name, name );
2311 return ERROR_SUCCESS;
2314 UINT MSI_CommitTables( MSIDATABASE *db )
2317 MSITABLE *table = NULL;
2321 r = msi_save_string_table( db->strings, db->storage );
2322 if( r != ERROR_SUCCESS )
2324 WARN("failed to save string table r=%08x\n",r);
2328 LIST_FOR_EACH_ENTRY( table, &db->tables, MSITABLE, entry )
2330 r = save_table( db, table );
2331 if( r != ERROR_SUCCESS )
2333 WARN("failed to save table %s (r=%08x)\n",
2334 debugstr_w(table->name), r);
2339 /* force everything to reload next time */
2340 free_cached_tables( db );
2342 return ERROR_SUCCESS;
2345 MSICONDITION MSI_DatabaseIsTablePersistent( MSIDATABASE *db, LPCWSTR table )
2350 TRACE("%p %s\n", db, debugstr_w(table));
2353 return MSICONDITION_ERROR;
2355 r = get_table( db, table, &t );
2356 if (r != ERROR_SUCCESS)
2357 return MSICONDITION_NONE;
2359 return t->persistent;
2362 static UINT read_raw_int(const BYTE *data, UINT col, UINT bytes)
2366 for (i = 0; i < bytes; i++)
2367 ret += (data[col + i] << i * 8);
2372 static UINT msi_record_encoded_stream_name( const MSITABLEVIEW *tv, MSIRECORD *rec, LPWSTR *pstname )
2374 static const WCHAR szDot[] = { '.', 0 };
2375 LPWSTR stname = NULL, sval, p;
2379 TRACE("%p %p\n", tv, rec);
2381 len = lstrlenW( tv->name ) + 1;
2382 stname = msi_alloc( len*sizeof(WCHAR) );
2385 r = ERROR_OUTOFMEMORY;
2389 lstrcpyW( stname, tv->name );
2391 for ( i = 0; i < tv->num_cols; i++ )
2393 if ( tv->columns[i].type & MSITYPE_KEY )
2395 sval = msi_dup_record_field( rec, i + 1 );
2398 r = ERROR_OUTOFMEMORY;
2402 len += lstrlenW( szDot ) + lstrlenW ( sval );
2403 p = msi_realloc ( stname, len*sizeof(WCHAR) );
2406 r = ERROR_OUTOFMEMORY;
2411 lstrcatW( stname, szDot );
2412 lstrcatW( stname, sval );
2420 *pstname = encode_streamname( FALSE, stname );
2423 return ERROR_SUCCESS;
2426 msi_free ( stname );
2431 static MSIRECORD *msi_get_transform_record( const MSITABLEVIEW *tv, const string_table *st,
2433 const BYTE *rawdata, UINT bytes_per_strref )
2435 UINT i, val, ofs = 0;
2437 MSICOLUMNINFO *columns = tv->columns;
2440 mask = rawdata[0] | (rawdata[1] << 8);
2443 rec = MSI_CreateRecord( tv->num_cols );
2448 for( i=0; i<tv->num_cols; i++ )
2450 if ( (mask&1) && (i>=(mask>>8)) )
2452 /* all keys must be present */
2453 if ( (~mask&1) && (~columns[i].type & MSITYPE_KEY) && ((1<<i) & ~mask) )
2456 if( MSITYPE_IS_BINARY(tv->columns[i].type) )
2459 IStream *stm = NULL;
2462 ofs += bytes_per_column( tv->db, &columns[i] );
2464 r = msi_record_encoded_stream_name( tv, rec, &encname );
2465 if ( r != ERROR_SUCCESS )
2468 r = IStorage_OpenStream( stg, encname, NULL,
2469 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stm );
2470 msi_free( encname );
2471 if ( r != ERROR_SUCCESS )
2474 MSI_RecordSetStream( rec, i+1, stm );
2475 TRACE(" field %d [%s]\n", i+1, debugstr_w(encname));
2477 else if( columns[i].type & MSITYPE_STRING )
2481 val = read_raw_int(rawdata, ofs, bytes_per_strref);
2482 sval = msi_string_lookup_id( st, val );
2483 MSI_RecordSetStringW( rec, i+1, sval );
2484 TRACE(" field %d [%s]\n", i+1, debugstr_w(sval));
2485 ofs += bytes_per_strref;
2489 UINT n = bytes_per_column( tv->db, &columns[i] );
2493 val = read_raw_int(rawdata, ofs, n);
2495 MSI_RecordSetInteger( rec, i+1, val^0x8000 );
2496 TRACE(" field %d [0x%04x]\n", i+1, val );
2499 val = read_raw_int(rawdata, ofs, n);
2501 MSI_RecordSetInteger( rec, i+1, val^0x80000000 );
2502 TRACE(" field %d [0x%08x]\n", i+1, val );
2505 ERR("oops - unknown column width %d\n", n);
2514 static void dump_record( MSIRECORD *rec )
2518 n = MSI_RecordGetFieldCount( rec );
2519 for( i=1; i<=n; i++ )
2521 LPCWSTR sval = MSI_RecordGetString( rec, i );
2523 if( MSI_RecordIsNull( rec, i ) )
2524 TRACE("row -> []\n");
2525 else if( (sval = MSI_RecordGetString( rec, i )) )
2526 TRACE("row -> [%s]\n", debugstr_w(sval));
2528 TRACE("row -> [0x%08x]\n", MSI_RecordGetInteger( rec, i ) );
2532 static void dump_table( const string_table *st, const USHORT *rawdata, UINT rawsize )
2537 for( i=0; i<(rawsize/2); i++ )
2539 sval = msi_string_lookup_id( st, rawdata[i] );
2540 MESSAGE(" %04x %s\n", rawdata[i], debugstr_w(sval) );
2544 static UINT* msi_record_to_row( const MSITABLEVIEW *tv, MSIRECORD *rec )
2549 data = msi_alloc( tv->num_cols *sizeof (UINT) );
2550 for( i=0; i<tv->num_cols; i++ )
2554 if ( ~tv->columns[i].type & MSITYPE_KEY )
2557 /* turn the transform column value into a row value */
2558 if ( ( tv->columns[i].type & MSITYPE_STRING ) &&
2559 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
2561 str = MSI_RecordGetString( rec, i+1 );
2562 r = msi_string2idW( tv->db->strings, str, &data[i] );
2564 /* if there's no matching string in the string table,
2565 these keys can't match any record, so fail now. */
2566 if( ERROR_SUCCESS != r )
2574 data[i] = MSI_RecordGetInteger( rec, i+1 );
2576 if (data[i] == MSI_NULL_INTEGER)
2578 else if ((tv->columns[i].type&0xff) == 2)
2581 data[i] += 0x80000000;
2587 static UINT msi_row_matches( MSITABLEVIEW *tv, UINT row, const UINT *data )
2589 UINT i, r, x, ret = ERROR_FUNCTION_FAILED;
2591 for( i=0; i<tv->num_cols; i++ )
2593 if ( ~tv->columns[i].type & MSITYPE_KEY )
2596 /* turn the transform column value into a row value */
2597 r = TABLE_fetch_int( &tv->view, row, i+1, &x );
2598 if ( r != ERROR_SUCCESS )
2600 ERR("TABLE_fetch_int shouldn't fail here\n");
2604 /* if this key matches, move to the next column */
2607 ret = ERROR_FUNCTION_FAILED;
2611 ret = ERROR_SUCCESS;
2617 static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row )
2619 UINT i, r = ERROR_FUNCTION_FAILED, *data;
2621 data = msi_record_to_row( tv, rec );
2624 for( i = 0; i < tv->table->row_count; i++ )
2626 r = msi_row_matches( tv, i, data );
2627 if( r == ERROR_SUCCESS )
2643 static UINT msi_table_load_transform( MSIDATABASE *db, IStorage *stg,
2644 string_table *st, TRANSFORMDATA *transform,
2645 UINT bytes_per_strref )
2648 BYTE *rawdata = NULL;
2649 MSITABLEVIEW *tv = NULL;
2650 UINT r, n, sz, i, mask;
2651 MSIRECORD *rec = NULL;
2657 return ERROR_SUCCESS;
2659 name = transform->name;
2662 TRACE("%p %p %p %s\n", db, stg, st, debugstr_w(name) );
2664 /* read the transform data */
2665 read_stream_data( stg, name, TRUE, &rawdata, &rawsize );
2668 TRACE("table %s empty\n", debugstr_w(name) );
2669 return ERROR_INVALID_TABLE;
2672 /* create a table view */
2673 r = TABLE_CreateView( db, name, (MSIVIEW**) &tv );
2674 if( r != ERROR_SUCCESS )
2677 r = tv->view.ops->execute( &tv->view, NULL );
2678 if( r != ERROR_SUCCESS )
2681 TRACE("name = %s columns = %u row_size = %u raw size = %u\n",
2682 debugstr_w(name), tv->num_cols, tv->row_size, rawsize );
2684 /* interpret the data */
2686 for( n=0; n < rawsize; )
2688 mask = rawdata[n] | (rawdata[n+1] << 8);
2693 * if the low bit is set, columns are continuous and
2694 * the number of columns is specified in the high byte
2697 for( i=0; i<tv->num_cols; i++ )
2699 if( (tv->columns[i].type & MSITYPE_STRING) &&
2700 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
2701 sz += bytes_per_strref;
2703 sz += bytes_per_column( tv->db, &tv->columns[i] );
2709 * If the low bit is not set, mask is a bitmask.
2710 * Excepting for key fields, which are always present,
2711 * each bit indicates that a field is present in the transform record.
2713 * mask == 0 is a special case ... only the keys will be present
2714 * and it means that this row should be deleted.
2717 for( i=0; i<tv->num_cols; i++ )
2719 if( (tv->columns[i].type & MSITYPE_KEY) || ((1<<i)&mask))
2721 if( (tv->columns[i].type & MSITYPE_STRING) &&
2722 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
2723 sz += bytes_per_strref;
2725 sz += bytes_per_column( tv->db, &tv->columns[i] );
2730 /* check we didn't run of the end of the table */
2731 if ( (n+sz) > rawsize )
2734 dump_table( st, (USHORT *)rawdata, rawsize );
2738 rec = msi_get_transform_record( tv, st, stg, &rawdata[n], bytes_per_strref );
2745 UINT number = MSI_NULL_INTEGER;
2747 TRACE("inserting record\n");
2749 if (!lstrcmpW(name, szColumns))
2751 MSI_RecordGetStringW( rec, 1, table, &sz );
2752 number = MSI_RecordGetInteger( rec, 2 );
2755 * Native msi seems writes nul into the Number (2nd) column of
2756 * the _Columns table, only when the columns are from a new table
2758 if ( number == MSI_NULL_INTEGER )
2760 /* reset the column number on a new table */
2761 if ( lstrcmpW(coltable, table) )
2764 lstrcpyW( coltable, table );
2767 /* fix nul column numbers */
2768 MSI_RecordSetInteger( rec, 2, ++colcol );
2772 r = TABLE_insert_row( &tv->view, rec, -1, FALSE );
2773 if (r != ERROR_SUCCESS)
2774 ERR("insert row failed\n");
2776 if ( number != MSI_NULL_INTEGER && !lstrcmpW(name, szColumns) )
2777 msi_update_table_columns( db, table );
2783 r = msi_table_find_row( tv, rec, &row );
2784 if (r != ERROR_SUCCESS)
2785 ERR("no matching row to transform\n");
2788 TRACE("modifying row [%d]:\n", row);
2789 TABLE_set_row( &tv->view, row, rec, mask );
2793 TRACE("deleting row [%d]:\n", row);
2794 TABLE_delete_row( &tv->view, row );
2797 if( TRACE_ON(msidb) ) dump_record( rec );
2798 msiobj_release( &rec->hdr );
2805 /* no need to free the table, it's associated with the database */
2806 msi_free( rawdata );
2808 tv->view.ops->delete( &tv->view );
2810 return ERROR_SUCCESS;
2814 * msi_table_apply_transform
2816 * Enumerate the table transforms in a transform storage and apply each one.
2818 UINT msi_table_apply_transform( MSIDATABASE *db, IStorage *stg )
2820 struct list transforms;
2821 IEnumSTATSTG *stgenum = NULL;
2822 TRANSFORMDATA *transform;
2823 TRANSFORMDATA *tables = NULL, *columns = NULL;
2826 string_table *strings;
2827 UINT ret = ERROR_FUNCTION_FAILED;
2828 UINT bytes_per_strref;
2830 TRACE("%p %p\n", db, stg );
2832 strings = msi_load_string_table( stg, &bytes_per_strref );
2836 r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
2840 list_init(&transforms);
2844 MSITABLEVIEW *tv = NULL;
2848 r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
2849 if ( FAILED( r ) || !count )
2852 decode_streamname( stat.pwcsName, name );
2853 CoTaskMemFree( stat.pwcsName );
2854 if ( name[0] != 0x4840 )
2857 if ( !lstrcmpW( name+1, szStringPool ) ||
2858 !lstrcmpW( name+1, szStringData ) )
2861 transform = msi_alloc_zero( sizeof(TRANSFORMDATA) );
2865 list_add_tail( &transforms, &transform->entry );
2867 transform->name = strdupW( name + 1 );
2869 if ( !lstrcmpW( transform->name, szTables ) )
2871 else if (!lstrcmpW( transform->name, szColumns ) )
2872 columns = transform;
2874 TRACE("transform contains stream %s\n", debugstr_w(name));
2876 /* load the table */
2877 r = TABLE_CreateView( db, transform->name, (MSIVIEW**) &tv );
2878 if( r != ERROR_SUCCESS )
2881 r = tv->view.ops->execute( &tv->view, NULL );
2882 if( r != ERROR_SUCCESS )
2884 tv->view.ops->delete( &tv->view );
2888 tv->view.ops->delete( &tv->view );
2892 * Apply _Tables and _Columns transforms first so that
2893 * the table metadata is correct, and empty tables exist.
2895 ret = msi_table_load_transform( db, stg, strings, tables, bytes_per_strref );
2896 if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
2899 ret = msi_table_load_transform( db, stg, strings, columns, bytes_per_strref );
2900 if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
2903 ret = ERROR_SUCCESS;
2905 while ( !list_empty( &transforms ) )
2907 transform = LIST_ENTRY( list_head( &transforms ), TRANSFORMDATA, entry );
2909 if ( lstrcmpW( transform->name, szColumns ) &&
2910 lstrcmpW( transform->name, szTables ) &&
2911 ret == ERROR_SUCCESS )
2913 ret = msi_table_load_transform( db, stg, strings, transform, bytes_per_strref );
2916 list_remove( &transform->entry );
2917 msi_free( transform->name );
2918 msi_free( transform );
2921 if ( ret == ERROR_SUCCESS )
2922 append_storage_to_db( db, stg );
2926 IEnumSTATSTG_Release( stgenum );
2928 msi_destroy_stringtable( strings );
2933 void append_storage_to_db( MSIDATABASE *db, IStorage *stg )
2937 t = msi_alloc( sizeof *t );
2939 IStorage_AddRef( stg );
2940 list_add_tail( &db->transforms, &t->entry );
2943 void msi_free_transforms( MSIDATABASE *db )
2945 while( !list_empty( &db->transforms ) )
2947 MSITRANSFORM *t = LIST_ENTRY( list_head( &db->transforms ),
2948 MSITRANSFORM, entry );
2949 list_remove( &t->entry );
2950 IStorage_Release( t->stg );
2955 static UINT table_find_insert_idx (MSIVIEW *view, LPCWSTR name, INT *pidx)
2957 UINT r, name_id, row_id;
2959 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
2961 TRACE ("%p %s\n", view, debugstr_w(name));
2963 r = msi_string2idW(tv->db->strings, name, &name_id);
2964 if (r != ERROR_SUCCESS)
2970 for( idx = 0; idx < tv->table->row_count; idx++ )
2972 r = TABLE_fetch_int( &tv->view, idx, 1, &row_id );
2973 if (row_id > name_id)
2978 return ERROR_SUCCESS;