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 );
125 static inline UINT bytes_per_column( MSIDATABASE *db, const MSICOLUMNINFO *col )
127 if( MSITYPE_IS_BINARY(col->type) )
130 if( col->type & MSITYPE_STRING )
131 return db->bytes_per_strref;
133 if( (col->type & 0xff) <= 2)
136 if( (col->type & 0xff) != 4 )
137 ERR("Invalid column size!\n");
142 static int utf2mime(int x)
144 if( (x>='0') && (x<='9') )
146 if( (x>='A') && (x<='Z') )
148 if( (x>='a') && (x<='z') )
157 static LPWSTR encode_streamname(BOOL bTable, LPCWSTR in)
159 DWORD count = MAX_STREAM_NAME;
164 count = lstrlenW( in )+2;
165 out = msi_alloc( count*sizeof(WCHAR) );
181 if( ( ch < 0x80 ) && ( utf2mime(ch) >= 0 ) )
183 ch = utf2mime(ch) + 0x4800;
185 if( next && (next<0x80) )
187 next = utf2mime(next);
198 ERR("Failed to encode stream name (%s)\n",debugstr_w(in));
203 static int mime2utf(int x)
210 return x - 10 - 26 + 'a';
211 if( x == (10+26+26) )
216 BOOL decode_streamname(LPCWSTR in, LPWSTR out)
221 while ( (ch = *in++) )
223 if( (ch >= 0x3800 ) && (ch < 0x4840 ) )
226 ch = mime2utf(ch-0x4800);
230 *out++ = mime2utf(ch&0x3f);
232 ch = mime2utf((ch>>6)&0x3f);
242 void enum_stream_names( IStorage *stg )
244 IEnumSTATSTG *stgenum = NULL;
250 r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
258 r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
259 if( FAILED( r ) || !count )
261 decode_streamname( stat.pwcsName, name );
262 TRACE("stream %2d -> %s %s\n", n,
263 debugstr_w(stat.pwcsName), debugstr_w(name) );
264 CoTaskMemFree( stat.pwcsName );
268 IEnumSTATSTG_Release( stgenum );
271 UINT read_stream_data( IStorage *stg, LPCWSTR stname, BOOL table,
272 BYTE **pdata, UINT *psz )
275 UINT ret = ERROR_FUNCTION_FAILED;
282 encname = encode_streamname(table, stname);
284 TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
286 r = IStorage_OpenStream(stg, encname, NULL,
287 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stm);
291 WARN("open stream failed r = %08x - empty table?\n", r);
295 r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
298 WARN("open stream failed r = %08x!\n", r);
302 if( stat.cbSize.QuadPart >> 32 )
308 sz = stat.cbSize.QuadPart;
309 data = msi_alloc( sz );
312 WARN("couldn't allocate memory r=%08x!\n", r);
313 ret = ERROR_NOT_ENOUGH_MEMORY;
317 r = IStream_Read(stm, data, sz, &count );
318 if( FAILED( r ) || ( count != sz ) )
321 WARN("read stream failed r = %08x!\n", r);
330 IStream_Release( stm );
335 static UINT db_get_raw_stream( MSIDATABASE *db, LPCWSTR stname, IStream **stm )
340 encname = encode_streamname(FALSE, stname);
342 TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
344 r = IStorage_OpenStream(db->storage, encname, NULL,
345 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, stm);
348 MSITRANSFORM *transform;
350 LIST_FOR_EACH_ENTRY( transform, &db->transforms, MSITRANSFORM, entry )
352 TRACE("looking for %s in transform storage\n", debugstr_w(stname) );
353 r = IStorage_OpenStream( transform->stg, encname, NULL,
354 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, stm );
362 return SUCCEEDED(r) ? ERROR_SUCCESS : ERROR_FUNCTION_FAILED;
365 UINT read_raw_stream_data( MSIDATABASE *db, LPCWSTR stname,
366 USHORT **pdata, UINT *psz )
369 UINT ret = ERROR_FUNCTION_FAILED;
375 r = db_get_raw_stream( db, stname, &stm );
376 if( r != ERROR_SUCCESS)
378 r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
381 WARN("open stream failed r = %08x!\n", r);
385 if( stat.cbSize.QuadPart >> 32 )
391 sz = stat.cbSize.QuadPart;
392 data = msi_alloc( sz );
395 WARN("couldn't allocate memory r=%08x!\n", r);
396 ret = ERROR_NOT_ENOUGH_MEMORY;
400 r = IStream_Read(stm, data, sz, &count );
401 if( FAILED( r ) || ( count != sz ) )
404 WARN("read stream failed r = %08x!\n", r);
413 IStream_Release( stm );
418 UINT write_stream_data( IStorage *stg, LPCWSTR stname,
419 LPCVOID data, UINT sz, BOOL bTable )
422 UINT ret = ERROR_FUNCTION_FAILED;
429 encname = encode_streamname(bTable, stname );
430 r = IStorage_OpenStream( stg, encname, NULL,
431 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, &stm);
434 r = IStorage_CreateStream( stg, encname,
435 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
440 WARN("open stream failed r = %08x\n", r);
445 r = IStream_SetSize( stm, size );
448 WARN("Failed to SetSize\n");
453 r = IStream_Seek( stm, pos, STREAM_SEEK_SET, NULL );
456 WARN("Failed to Seek\n");
462 r = IStream_Write(stm, data, sz, &count );
463 if( FAILED( r ) || ( count != sz ) )
465 WARN("Failed to Write\n");
473 IStream_Release( stm );
478 static void free_table( MSITABLE *table )
481 for( i=0; i<table->row_count; i++ )
482 msi_free( table->data[i] );
483 msi_free( table->data );
484 msi_free( table->data_persistent );
485 msi_free_colinfo( table->colinfo, table->col_count );
486 msi_free( table->colinfo );
490 static UINT msi_table_get_row_size( MSIDATABASE *db,const MSICOLUMNINFO *cols,
493 const MSICOLUMNINFO *last_col = &cols[count-1];
496 return last_col->offset + bytes_per_column( db, last_col );
499 /* add this table to the list of cached tables in the database */
500 static UINT read_table_from_storage( MSIDATABASE *db, MSITABLE *t, IStorage *stg )
502 BYTE *rawdata = NULL;
503 UINT rawsize = 0, i, j, row_size = 0;
505 TRACE("%s\n",debugstr_w(t->name));
507 row_size = msi_table_get_row_size( db, t->colinfo, t->col_count );
509 /* if we can't read the table, just assume that it's empty */
510 read_stream_data( stg, t->name, TRUE, &rawdata, &rawsize );
512 return ERROR_SUCCESS;
514 TRACE("Read %d bytes\n", rawsize );
516 if( rawsize % row_size )
518 WARN("Table size is invalid %d/%d\n", rawsize, row_size );
522 t->row_count = rawsize / row_size;
523 t->data = msi_alloc_zero( t->row_count * sizeof (USHORT*) );
526 t->data_persistent = msi_alloc_zero( t->row_count * sizeof(BOOL));
527 if ( !t->data_persistent )
530 /* transpose all the data */
531 TRACE("Transposing data from %d rows\n", t->row_count );
532 for( i=0; i<t->row_count; i++ )
534 t->data[i] = msi_alloc( row_size );
537 t->data_persistent[i] = TRUE;
539 for( j=0; j<t->col_count; j++ )
541 UINT ofs = t->colinfo[j].offset;
542 UINT n = bytes_per_column( db, &t->colinfo[j] );
545 if ( n != 2 && n != 3 && n != 4 )
547 ERR("oops - unknown column width %d\n", n);
551 for ( k = 0; k < n; k++ )
552 t->data[i][ofs + k] = rawdata[ofs*t->row_count + i * n + k];
557 return ERROR_SUCCESS;
560 return ERROR_FUNCTION_FAILED;
563 void free_cached_tables( MSIDATABASE *db )
565 while( !list_empty( &db->tables ) )
567 MSITABLE *t = LIST_ENTRY( list_head( &db->tables ), MSITABLE, entry );
569 list_remove( &t->entry );
574 static MSITABLE *find_cached_table( MSIDATABASE *db, LPCWSTR name )
578 LIST_FOR_EACH_ENTRY( t, &db->tables, MSITABLE, entry )
579 if( !lstrcmpW( name, t->name ) )
585 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name, MSICOLUMNINFO **pcols, UINT *pcount )
587 UINT r, column_count = 0;
588 MSICOLUMNINFO *columns;
590 /* get the number of columns in this table */
592 r = get_tablecolumns( db, name, NULL, &column_count );
593 if( r != ERROR_SUCCESS )
596 *pcount = column_count;
598 /* if there's no columns, there's no table */
599 if( column_count == 0 )
600 return ERROR_INVALID_PARAMETER;
602 TRACE("Table %s found\n", debugstr_w(name) );
604 columns = msi_alloc( column_count*sizeof (MSICOLUMNINFO) );
606 return ERROR_FUNCTION_FAILED;
608 r = get_tablecolumns( db, name, columns, &column_count );
609 if( r != ERROR_SUCCESS )
612 return ERROR_FUNCTION_FAILED;
620 UINT msi_create_table( MSIDATABASE *db, LPCWSTR name, column_info *col_info,
621 MSICONDITION persistent, MSITABLE **table_ret)
625 MSIRECORD *rec = NULL;
630 /* only add tables that don't exist already */
631 if( TABLE_Exists(db, name ) )
633 WARN("table %s exists\n", debugstr_w(name));
634 return ERROR_BAD_QUERY_SYNTAX;
637 table = msi_alloc( sizeof (MSITABLE) + lstrlenW(name)*sizeof (WCHAR) );
639 return ERROR_FUNCTION_FAILED;
641 table->ref_count = 1;
642 table->row_count = 0;
644 table->data_persistent = NULL;
645 table->colinfo = NULL;
646 table->col_count = 0;
647 table->persistent = persistent;
648 lstrcpyW( table->name, name );
650 for( col = col_info; col; col = col->next )
653 table->colinfo = msi_alloc( table->col_count * sizeof(MSICOLUMNINFO) );
657 return ERROR_FUNCTION_FAILED;
660 for( i = 0, col = col_info; col; i++, col = col->next )
662 table->colinfo[ i ].tablename = strdupW( col->table );
663 table->colinfo[ i ].number = i + 1;
664 table->colinfo[ i ].colname = strdupW( col->column );
665 table->colinfo[ i ].type = col->type;
666 table->colinfo[ i ].offset = 0;
667 table->colinfo[ i ].ref_count = 0;
668 table->colinfo[ i ].hash_table = NULL;
669 table->colinfo[ i ].temporary = col->temporary;
671 table_calc_column_offsets( db, table->colinfo, table->col_count);
673 r = TABLE_CreateView( db, szTables, &tv );
674 TRACE("CreateView returned %x\n", r);
681 r = tv->ops->execute( tv, 0 );
682 TRACE("tv execute returned %x\n", r);
686 rec = MSI_CreateRecord( 1 );
690 r = MSI_RecordSetStringW( rec, 1, name );
694 r = tv->ops->insert_row( tv, rec, -1, 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 = tv->ops->insert_row( tv, rec, -1, FALSE );
756 msiobj_release( &rec->hdr );
757 /* FIXME: remove values from the string table on error */
759 tv->ops->delete( tv );
761 if (r == ERROR_SUCCESS)
763 list_add_head( &db->tables, &table->entry );
772 static UINT get_table( MSIDATABASE *db, LPCWSTR name, MSITABLE **table_ret )
777 /* first, see if the table is cached */
778 table = find_cached_table( db, name );
782 return ERROR_SUCCESS;
785 /* nonexistent tables should be interpreted as empty tables */
786 table = msi_alloc( sizeof (MSITABLE) + lstrlenW(name)*sizeof (WCHAR) );
788 return ERROR_FUNCTION_FAILED;
790 table->row_count = 0;
792 table->data_persistent = NULL;
793 table->colinfo = NULL;
794 table->col_count = 0;
795 table->persistent = MSICONDITION_TRUE;
796 lstrcpyW( table->name, name );
798 if ( !lstrcmpW(name, szTables) || !lstrcmpW(name, szColumns) )
799 table->persistent = MSICONDITION_NONE;
801 r = table_get_column_info( db, name, &table->colinfo, &table->col_count);
802 if (r != ERROR_SUCCESS)
804 free_table ( table );
808 r = read_table_from_storage( db, table, db->storage );
809 if( r != ERROR_SUCCESS )
815 list_add_head( &db->tables, &table->entry );
817 return ERROR_SUCCESS;
820 static UINT save_table( MSIDATABASE *db, const MSITABLE *t )
822 BYTE *rawdata = NULL, *p;
823 UINT rawsize, r, i, j, row_size;
825 /* Nothing to do for non-persistent tables */
826 if( t->persistent == MSICONDITION_FALSE )
827 return ERROR_SUCCESS;
829 TRACE("Saving %s\n", debugstr_w( t->name ) );
831 row_size = msi_table_get_row_size( db, t->colinfo, t->col_count );
833 rawsize = t->row_count * row_size;
834 rawdata = msi_alloc_zero( rawsize );
837 r = ERROR_NOT_ENOUGH_MEMORY;
843 for( i=0; i<t->col_count; i++ )
845 for( j=0; j<t->row_count; j++ )
847 UINT offset = t->colinfo[i].offset;
849 if (!t->data_persistent[j]) continue;
853 *p++ = t->data[j][offset];
854 *p++ = t->data[j][offset + 1];
855 if( 4 == bytes_per_column( db, &t->colinfo[i] ) )
857 *p++ = t->data[j][offset + 2];
858 *p++ = t->data[j][offset + 3];
863 TRACE("writing %d bytes\n", rawsize);
864 r = write_stream_data( db->storage, t->name, rawdata, rawsize, TRUE );
872 static void table_calc_column_offsets( MSIDATABASE *db, MSICOLUMNINFO *colinfo,
877 for( i=0; colinfo && (i<count); i++ )
879 assert( (i+1) == colinfo[ i ].number );
881 colinfo[i].offset = colinfo[ i - 1 ].offset
882 + bytes_per_column( db, &colinfo[ i - 1 ] );
884 colinfo[i].offset = 0;
885 TRACE("column %d is [%s] with type %08x ofs %d\n",
886 colinfo[i].number, debugstr_w(colinfo[i].colname),
887 colinfo[i].type, colinfo[i].offset);
891 static UINT get_defaulttablecolumns( MSIDATABASE *db, LPCWSTR name,
892 MSICOLUMNINFO *colinfo, UINT *sz)
894 const MSICOLUMNINFO *p;
897 TRACE("%s\n", debugstr_w(name));
899 if (!lstrcmpW( name, szTables ))
904 else if (!lstrcmpW( name, szColumns ))
910 return ERROR_FUNCTION_FAILED;
912 /* duplicate the string data so we can free it in msi_free_colinfo */
915 if (colinfo && (i < *sz) )
918 colinfo[i].tablename = strdupW( p[i].tablename );
919 colinfo[i].colname = strdupW( p[i].colname );
921 if( colinfo && (i >= *sz) )
924 table_calc_column_offsets( db, colinfo, n );
926 return ERROR_SUCCESS;
929 static void msi_free_colinfo( MSICOLUMNINFO *colinfo, UINT count )
933 for( i=0; i<count; i++ )
935 msi_free( colinfo[i].tablename );
936 msi_free( colinfo[i].colname );
937 msi_free( colinfo[i].hash_table );
941 static LPWSTR msi_makestring( const MSIDATABASE *db, UINT stringid)
943 return strdupW(msi_string_lookup_id( db->strings, stringid ));
946 static UINT read_table_int(BYTE *const *data, UINT row, UINT col, UINT bytes)
950 for (i = 0; i < bytes; i++)
951 ret += (data[row][col + i] << i * 8);
956 static UINT get_tablecolumns( MSIDATABASE *db,
957 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz)
959 UINT r, i, n=0, table_id, count, maxcount = *sz;
960 MSITABLE *table = NULL;
962 TRACE("%s\n", debugstr_w(szTableName));
964 /* first check if there is a default table with that name */
965 r = get_defaulttablecolumns( db, szTableName, colinfo, sz );
966 if( ( r == ERROR_SUCCESS ) && *sz )
969 r = get_table( db, szColumns, &table );
970 if( r != ERROR_SUCCESS )
972 ERR("couldn't load _Columns table\n");
973 return ERROR_FUNCTION_FAILED;
976 /* convert table and column names to IDs from the string table */
977 r = msi_string2idW( db->strings, szTableName, &table_id );
978 if( r != ERROR_SUCCESS )
980 WARN("Couldn't find id for %s\n", debugstr_w(szTableName));
984 TRACE("Table id is %d, row count is %d\n", table_id, table->row_count);
986 /* Note: _Columns table doesn't have non-persistent data */
988 /* if maxcount is non-zero, assume it's exactly right for this table */
989 memset( colinfo, 0, maxcount*sizeof(*colinfo) );
990 count = table->row_count;
991 for( i=0; i<count; i++ )
993 if( read_table_int(table->data, i, 0, db->bytes_per_strref) != table_id )
997 UINT id = read_table_int(table->data, i, table->colinfo[2].offset, db->bytes_per_strref);
998 UINT col = read_table_int(table->data, i, table->colinfo[1].offset, sizeof(USHORT)) - (1<<15);
1000 /* check the column number is in range */
1001 if (col<1 || col>maxcount)
1003 ERR("column %d out of range\n", col);
1007 /* check if this column was already set */
1008 if (colinfo[ col - 1 ].number)
1010 ERR("duplicate column %d\n", col);
1014 colinfo[ col - 1 ].tablename = msi_makestring( db, table_id );
1015 colinfo[ col - 1 ].number = col;
1016 colinfo[ col - 1 ].colname = msi_makestring( db, id );
1017 colinfo[ col - 1 ].type = read_table_int(table->data, i,
1018 table->colinfo[3].offset,
1019 sizeof(USHORT)) - (1<<15);
1020 colinfo[ col - 1 ].offset = 0;
1021 colinfo[ col - 1 ].ref_count = 0;
1022 colinfo[ col - 1 ].hash_table = NULL;
1027 TRACE("%s has %d columns\n", debugstr_w(szTableName), n);
1029 if (colinfo && n != maxcount)
1031 ERR("missing column in table %s\n", debugstr_w(szTableName));
1032 msi_free_colinfo(colinfo, maxcount );
1033 return ERROR_FUNCTION_FAILED;
1036 table_calc_column_offsets( db, colinfo, n );
1039 return ERROR_SUCCESS;
1042 static void msi_update_table_columns( MSIDATABASE *db, LPCWSTR name )
1045 UINT size, offset, old_count;
1048 table = find_cached_table( db, name );
1049 old_count = table->col_count;
1050 msi_free( table->colinfo );
1051 table->colinfo = NULL;
1053 table_get_column_info( db, name, &table->colinfo, &table->col_count );
1054 if (!table->col_count)
1057 size = msi_table_get_row_size( db, table->colinfo, table->col_count );
1058 offset = table->colinfo[table->col_count - 1].offset;
1060 for ( n = 0; n < table->row_count; n++ )
1062 table->data[n] = msi_realloc( table->data[n], size );
1063 if (old_count < table->col_count)
1064 memset( &table->data[n][offset], 0, size - offset );
1068 /* try to find the table name in the _Tables table */
1069 BOOL TABLE_Exists( MSIDATABASE *db, LPCWSTR name )
1071 UINT r, table_id = 0, i, count;
1072 MSITABLE *table = NULL;
1074 static const WCHAR szStreams[] = {'_','S','t','r','e','a','m','s',0};
1075 static const WCHAR szStorages[] = {'_','S','t','o','r','a','g','e','s',0};
1077 if( !lstrcmpW( name, szTables ) || !lstrcmpW( name, szColumns ) ||
1078 !lstrcmpW( name, szStreams ) || !lstrcmpW( name, szStorages ) )
1081 r = msi_string2idW( db->strings, name, &table_id );
1082 if( r != ERROR_SUCCESS )
1084 TRACE("Couldn't find id for %s\n", debugstr_w(name));
1088 r = get_table( db, szTables, &table );
1089 if( r != ERROR_SUCCESS )
1091 ERR("table %s not available\n", debugstr_w(szTables));
1095 count = table->row_count;
1096 for( i=0; i<count; i++ )
1097 if( table->data[ i ][ 0 ] == table_id )
1103 /* below is the query interface to a table */
1105 typedef struct tagMSITABLEVIEW
1110 MSICOLUMNINFO *columns;
1111 MSIORDERINFO *order;
1117 static UINT TABLE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
1119 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1123 return ERROR_INVALID_PARAMETER;
1125 if( (col==0) || (col>tv->num_cols) )
1126 return ERROR_INVALID_PARAMETER;
1128 /* how many rows are there ? */
1129 if( row >= tv->table->row_count )
1130 return ERROR_NO_MORE_ITEMS;
1132 if( tv->columns[col-1].offset >= tv->row_size )
1134 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1135 ERR("%p %p\n", tv, tv->columns );
1136 return ERROR_FUNCTION_FAILED;
1140 row = tv->order->reorder[row];
1142 n = bytes_per_column( tv->db, &tv->columns[col-1] );
1143 if (n != 2 && n != 3 && n != 4)
1145 ERR("oops! what is %d bytes per column?\n", n );
1146 return ERROR_FUNCTION_FAILED;
1149 offset = tv->columns[col-1].offset;
1150 *val = read_table_int(tv->table->data, row, offset, n);
1152 /* TRACE("Data [%d][%d] = %d\n", row, col, *val ); */
1154 return ERROR_SUCCESS;
1157 static UINT msi_stream_name( const MSITABLEVIEW *tv, UINT row, LPWSTR *pstname )
1159 LPWSTR p, stname = NULL;
1160 UINT i, r, type, ival;
1163 MSIVIEW *view = (MSIVIEW *) tv;
1165 TRACE("%p %d\n", tv, row);
1167 len = lstrlenW( tv->name ) + 1;
1168 stname = msi_alloc( len*sizeof(WCHAR) );
1171 r = ERROR_OUTOFMEMORY;
1175 lstrcpyW( stname, tv->name );
1177 for ( i = 0; i < tv->num_cols; i++ )
1179 type = tv->columns[i].type;
1180 if ( type & MSITYPE_KEY )
1182 r = TABLE_fetch_int( view, row, i+1, &ival );
1183 if ( r != ERROR_SUCCESS )
1186 if ( tv->columns[i].type & MSITYPE_STRING )
1188 sval = msi_string_lookup_id( tv->db->strings, ival );
1191 r = ERROR_INVALID_PARAMETER;
1197 static const WCHAR fmt[] = { '%','d',0 };
1199 UINT n = bytes_per_column( tv->db, &tv->columns[i] );
1204 sprintfW( number, fmt, ival^0x8000 );
1207 sprintfW( number, fmt, ival^0x80000000 );
1210 ERR( "oops - unknown column width %d\n", n );
1211 r = ERROR_FUNCTION_FAILED;
1217 len += lstrlenW( szDot ) + lstrlenW( sval );
1218 p = msi_realloc ( stname, len*sizeof(WCHAR) );
1221 r = ERROR_OUTOFMEMORY;
1226 lstrcatW( stname, szDot );
1227 lstrcatW( stname, sval );
1234 return ERROR_SUCCESS;
1243 * We need a special case for streams, as we need to reference column with
1244 * the name of the stream in the same table, and the table name
1245 * which may not be available at higher levels of the query
1247 static UINT TABLE_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm )
1249 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1251 LPWSTR full_name = NULL;
1253 if( !view->ops->fetch_int )
1254 return ERROR_INVALID_PARAMETER;
1256 r = msi_stream_name( tv, row, &full_name );
1257 if ( r != ERROR_SUCCESS )
1259 ERR("fetching stream, error = %d\n", r);
1263 r = db_get_raw_stream( tv->db, full_name, stm );
1265 ERR("fetching stream %s, error = %d\n",debugstr_w(full_name), r);
1266 msi_free( full_name );
1271 static UINT TABLE_set_int( MSITABLEVIEW *tv, UINT row, UINT col, UINT val )
1276 return ERROR_INVALID_PARAMETER;
1278 if( (col==0) || (col>tv->num_cols) )
1279 return ERROR_INVALID_PARAMETER;
1281 if( row >= tv->table->row_count )
1282 return ERROR_INVALID_PARAMETER;
1284 if( tv->columns[col-1].offset >= tv->row_size )
1286 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1287 ERR("%p %p\n", tv, tv->columns );
1288 return ERROR_FUNCTION_FAILED;
1291 msi_free( tv->columns[col-1].hash_table );
1292 tv->columns[col-1].hash_table = NULL;
1294 n = bytes_per_column( tv->db, &tv->columns[col-1] );
1295 if ( n != 2 && n != 3 && n != 4 )
1297 ERR("oops! what is %d bytes per column?\n", n );
1298 return ERROR_FUNCTION_FAILED;
1301 offset = tv->columns[col-1].offset;
1302 for ( i = 0; i < n; i++ )
1303 tv->table->data[row][offset + i] = (val >> i * 8) & 0xff;
1305 return ERROR_SUCCESS;
1308 static UINT TABLE_get_row( struct tagMSIVIEW *view, UINT row, MSIRECORD **rec )
1310 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1313 return ERROR_INVALID_PARAMETER;
1316 row = tv->order->reorder[row];
1318 return msi_view_get_row(tv->db, view, row, rec);
1321 static UINT msi_addstreamW( MSIDATABASE *db, LPCWSTR name, IStream *data )
1324 MSIQUERY *query = NULL;
1325 MSIRECORD *rec = NULL;
1327 static const WCHAR insert[] = {
1328 'I','N','S','E','R','T',' ','I','N','T','O',' ',
1329 '`','_','S','t','r','e','a','m','s','`',' ',
1330 '(','`','N','a','m','e','`',',',
1331 '`','D','a','t','a','`',')',' ',
1332 'V','A','L','U','E','S',' ','(','?',',','?',')',0};
1334 TRACE("%p %s %p\n", db, debugstr_w(name), data);
1336 rec = MSI_CreateRecord( 2 );
1338 return ERROR_OUTOFMEMORY;
1340 r = MSI_RecordSetStringW( rec, 1, name );
1341 if ( r != ERROR_SUCCESS )
1344 r = MSI_RecordSetIStream( rec, 2, data );
1345 if ( r != ERROR_SUCCESS )
1348 r = MSI_DatabaseOpenViewW( db, insert, &query );
1349 if ( r != ERROR_SUCCESS )
1352 r = MSI_ViewExecute( query, rec );
1355 msiobj_release( &query->hdr );
1356 msiobj_release( &rec->hdr );
1361 static UINT get_table_value_from_record( MSITABLEVIEW *tv, MSIRECORD *rec, UINT iField, UINT *pvalue )
1363 MSICOLUMNINFO columninfo;
1366 if ( (iField <= 0) ||
1367 (iField > tv->num_cols) ||
1368 MSI_RecordIsNull( rec, iField ) )
1369 return ERROR_FUNCTION_FAILED;
1371 columninfo = tv->columns[ iField - 1 ];
1373 if ( MSITYPE_IS_BINARY(columninfo.type) )
1375 *pvalue = 1; /* refers to the first key column */
1377 else if ( columninfo.type & MSITYPE_STRING )
1379 LPCWSTR sval = MSI_RecordGetString( rec, iField );
1381 r = msi_string2idW(tv->db->strings, sval, pvalue);
1382 if (r != ERROR_SUCCESS)
1383 return ERROR_NOT_FOUND;
1385 else if ( 2 == bytes_per_column( tv->db, &columninfo ) )
1387 *pvalue = 0x8000 + MSI_RecordGetInteger( rec, iField );
1388 if ( *pvalue & 0xffff0000 )
1390 ERR("field %u value %d out of range\n", iField, *pvalue - 0x8000);
1391 return ERROR_FUNCTION_FAILED;
1396 INT ival = MSI_RecordGetInteger( rec, iField );
1397 *pvalue = ival ^ 0x80000000;
1400 return ERROR_SUCCESS;
1403 static UINT TABLE_set_row( struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask )
1405 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1406 UINT i, val, r = ERROR_SUCCESS;
1409 return ERROR_INVALID_PARAMETER;
1411 /* test if any of the mask bits are invalid */
1412 if ( mask >= (1<<tv->num_cols) )
1413 return ERROR_INVALID_PARAMETER;
1415 for ( i = 0; i < tv->num_cols; i++ )
1419 /* only update the fields specified in the mask */
1420 if ( !(mask&(1<<i)) )
1423 persistent = (tv->table->persistent != MSICONDITION_FALSE) &&
1424 (tv->table->data_persistent[row]);
1425 /* FIXME: should we allow updating keys? */
1428 if ( !MSI_RecordIsNull( rec, i + 1 ) )
1430 r = get_table_value_from_record (tv, rec, i + 1, &val);
1431 if ( MSITYPE_IS_BINARY(tv->columns[ i ].type) )
1436 if ( r != ERROR_SUCCESS )
1437 return ERROR_FUNCTION_FAILED;
1439 r = MSI_RecordGetIStream( rec, i + 1, &stm );
1440 if ( r != ERROR_SUCCESS )
1443 r = msi_stream_name( tv, row, &stname );
1444 if ( r != ERROR_SUCCESS )
1446 IStream_Release( stm );
1450 r = msi_addstreamW( tv->db, stname, stm );
1451 IStream_Release( stm );
1452 msi_free ( stname );
1454 if ( r != ERROR_SUCCESS )
1457 else if ( tv->columns[i].type & MSITYPE_STRING )
1461 if ( r != ERROR_SUCCESS )
1463 LPCWSTR sval = MSI_RecordGetString( rec, i + 1 );
1464 val = msi_addstringW( tv->db->strings, 0, sval, -1, 1,
1465 persistent ? StringPersistent : StringNonPersistent );
1470 TABLE_fetch_int(&tv->view, row, i + 1, &x);
1477 if ( r != ERROR_SUCCESS )
1478 return ERROR_FUNCTION_FAILED;
1482 r = TABLE_set_int( tv, row, i+1, val );
1483 if ( r != ERROR_SUCCESS )
1489 static UINT table_create_new_row( struct tagMSIVIEW *view, UINT *num, BOOL temporary )
1491 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1496 BOOL **data_persist_ptr;
1499 TRACE("%p %s\n", view, temporary ? "TRUE" : "FALSE");
1502 return ERROR_INVALID_PARAMETER;
1504 row = msi_alloc_zero( tv->row_size );
1506 return ERROR_NOT_ENOUGH_MEMORY;
1508 row_count = &tv->table->row_count;
1509 data_ptr = &tv->table->data;
1510 data_persist_ptr = &tv->table->data_persistent;
1512 *num = tv->table->row_count;
1514 sz = (*row_count + 1) * sizeof (BYTE*);
1516 p = msi_realloc( *data_ptr, sz );
1518 p = msi_alloc( sz );
1522 return ERROR_NOT_ENOUGH_MEMORY;
1525 sz = (*row_count + 1) * sizeof (BOOL);
1526 if( *data_persist_ptr )
1527 b = msi_realloc( *data_persist_ptr, sz );
1529 b = msi_alloc( sz );
1534 return ERROR_NOT_ENOUGH_MEMORY;
1538 (*data_ptr)[*row_count] = row;
1540 *data_persist_ptr = b;
1541 (*data_persist_ptr)[*row_count] = !temporary;
1545 return ERROR_SUCCESS;
1548 static UINT TABLE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
1550 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1552 TRACE("%p %p\n", tv, record);
1554 TRACE("There are %d columns\n", tv->num_cols );
1556 return ERROR_SUCCESS;
1559 static UINT TABLE_close( struct tagMSIVIEW *view )
1561 TRACE("%p\n", view );
1563 return ERROR_SUCCESS;
1566 static UINT TABLE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols)
1568 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1570 TRACE("%p %p %p\n", view, rows, cols );
1573 *cols = tv->num_cols;
1577 return ERROR_INVALID_PARAMETER;
1578 *rows = tv->table->row_count;
1581 return ERROR_SUCCESS;
1584 static UINT TABLE_get_column_info( struct tagMSIVIEW *view,
1585 UINT n, LPWSTR *name, UINT *type, BOOL *temporary,
1586 LPWSTR *table_name )
1588 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1590 TRACE("%p %d %p %p\n", tv, n, name, type );
1592 if( ( n == 0 ) || ( n > tv->num_cols ) )
1593 return ERROR_INVALID_PARAMETER;
1597 *name = strdupW( tv->columns[n-1].colname );
1599 return ERROR_FUNCTION_FAILED;
1604 *table_name = strdupW( tv->columns[n-1].tablename );
1606 return ERROR_FUNCTION_FAILED;
1610 *type = tv->columns[n-1].type;
1613 *temporary = tv->columns[n-1].temporary;
1615 return ERROR_SUCCESS;
1618 static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row );
1620 static UINT table_validate_new( MSITABLEVIEW *tv, MSIRECORD *rec )
1624 /* check there's no null values where they're not allowed */
1625 for( i = 0; i < tv->num_cols; i++ )
1627 if ( tv->columns[i].type & MSITYPE_NULLABLE )
1630 if ( MSITYPE_IS_BINARY(tv->columns[i].type) )
1631 TRACE("skipping binary column\n");
1632 else if ( tv->columns[i].type & MSITYPE_STRING )
1636 str = MSI_RecordGetString( rec, i+1 );
1637 if (str == NULL || str[0] == 0)
1638 return ERROR_INVALID_DATA;
1644 n = MSI_RecordGetInteger( rec, i+1 );
1645 if (n == MSI_NULL_INTEGER)
1646 return ERROR_INVALID_DATA;
1650 /* check there's no duplicate keys */
1651 r = msi_table_find_row( tv, rec, &row );
1652 if (r == ERROR_SUCCESS)
1653 return ERROR_FUNCTION_FAILED;
1655 return ERROR_SUCCESS;
1658 static UINT find_insert_index( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *pidx )
1660 UINT r, idx, j, ivalue, x;
1662 TRACE("%p %p %p\n", tv, rec, pidx);
1664 for (idx = 0; idx < tv->table->row_count; idx++)
1666 for (j = 0; j < tv->num_cols; j++ )
1668 r = get_table_value_from_record (tv, rec, j+1, &ivalue);
1669 if (r != ERROR_SUCCESS)
1672 r = TABLE_fetch_int(&tv->view, idx, j + 1, &x);
1673 if (r != ERROR_SUCCESS)
1678 else if (ivalue == x)
1681 TRACE("Found %d.\n", idx);
1683 return ERROR_SUCCESS;
1688 TRACE("Found %d.\n", idx);
1690 return ERROR_SUCCESS;
1693 static UINT TABLE_insert_row( struct tagMSIVIEW *view, MSIRECORD *rec, UINT row, BOOL temporary )
1695 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1698 TRACE("%p %p %s\n", tv, rec, temporary ? "TRUE" : "FALSE" );
1700 /* check that the key is unique - can we find a matching row? */
1701 r = table_validate_new( tv, rec );
1702 if( r != ERROR_SUCCESS )
1703 return ERROR_FUNCTION_FAILED;
1707 r = find_insert_index(tv, rec, &row);
1708 if( r != ERROR_SUCCESS )
1709 return ERROR_FUNCTION_FAILED;
1712 r = table_create_new_row( view, &row, temporary );
1713 TRACE("insert_row returned %08x\n", r);
1714 if( r != ERROR_SUCCESS )
1717 /* shift the rows to make room for the new row */
1718 for (i = tv->table->row_count - 1; i > row; i--)
1720 memmove(&(tv->table->data[i][0]),
1721 &(tv->table->data[i - 1][0]), tv->row_size);
1722 tv->table->data_persistent[i] = tv->table->data_persistent[i - 1];
1725 /* Re-set the persistence flag */
1726 tv->table->data_persistent[row] = !temporary;
1727 return TABLE_set_row( view, row, rec, (1<<tv->num_cols) - 1 );
1730 static UINT TABLE_delete_row( struct tagMSIVIEW *view, UINT row )
1732 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1733 UINT r, num_rows, num_cols, i;
1735 TRACE("%p %d\n", tv, row);
1738 return ERROR_INVALID_PARAMETER;
1740 r = TABLE_get_dimensions( view, &num_rows, &num_cols );
1741 if ( r != ERROR_SUCCESS )
1744 if ( row >= num_rows )
1745 return ERROR_FUNCTION_FAILED;
1747 num_rows = tv->table->row_count;
1748 tv->table->row_count--;
1750 /* reset the hash tables */
1751 for (i = 0; i < tv->num_cols; i++)
1753 msi_free( tv->columns[i].hash_table );
1754 tv->columns[i].hash_table = NULL;
1757 if ( row == num_rows - 1 )
1758 return ERROR_SUCCESS;
1760 for (i = row + 1; i < num_rows; i++)
1762 memcpy(tv->table->data[i - 1], tv->table->data[i], tv->row_size);
1763 tv->table->data_persistent[i - 1] = tv->table->data_persistent[i];
1766 return ERROR_SUCCESS;
1769 static UINT msi_table_update(struct tagMSIVIEW *view, MSIRECORD *rec, UINT row)
1771 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1774 /* FIXME: MsiViewFetch should set rec index 0 to some ID that
1775 * sets the fetched record apart from other records
1779 return ERROR_INVALID_PARAMETER;
1781 r = msi_table_find_row(tv, rec, &new_row);
1782 if (r != ERROR_SUCCESS)
1784 ERR("can't find row to modify\n");
1785 return ERROR_FUNCTION_FAILED;
1788 /* the row cannot be changed */
1789 if (row != new_row + 1)
1790 return ERROR_FUNCTION_FAILED;
1792 return TABLE_set_row(view, new_row, rec, (1 << tv->num_cols) - 1);
1795 static UINT msi_table_assign(struct tagMSIVIEW *view, MSIRECORD *rec)
1797 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1801 return ERROR_INVALID_PARAMETER;
1803 r = msi_table_find_row(tv, rec, &row);
1804 if (r == ERROR_SUCCESS)
1805 return TABLE_set_row(view, row, rec, (1 << tv->num_cols) - 1);
1807 return TABLE_insert_row( view, rec, -1, FALSE );
1810 static UINT modify_delete_row( struct tagMSIVIEW *view, MSIRECORD *rec )
1812 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1815 r = msi_table_find_row(tv, rec, &row);
1816 if (r != ERROR_SUCCESS)
1819 return TABLE_delete_row(view, row);
1822 static UINT msi_refresh_record( struct tagMSIVIEW *view, MSIRECORD *rec, UINT row )
1827 r = TABLE_get_row(view, row - 1, &curr);
1828 if (r != ERROR_SUCCESS)
1831 count = MSI_RecordGetFieldCount(rec);
1832 for (i = 0; i < count; i++)
1833 MSI_RecordCopyField(curr, i + 1, rec, i + 1);
1835 msiobj_release(&curr->hdr);
1836 return ERROR_SUCCESS;
1839 static UINT TABLE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
1840 MSIRECORD *rec, UINT row)
1842 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1845 TRACE("%p %d %p\n", view, eModifyMode, rec );
1847 switch (eModifyMode)
1849 case MSIMODIFY_DELETE:
1850 r = modify_delete_row( view, rec );
1852 case MSIMODIFY_VALIDATE_NEW:
1853 r = table_validate_new( tv, rec );
1856 case MSIMODIFY_INSERT:
1857 r = table_validate_new( tv, rec );
1858 if (r != ERROR_SUCCESS)
1860 r = TABLE_insert_row( view, rec, -1, FALSE );
1863 case MSIMODIFY_INSERT_TEMPORARY:
1864 r = table_validate_new( tv, rec );
1865 if (r != ERROR_SUCCESS)
1867 r = TABLE_insert_row( view, rec, -1, TRUE );
1870 case MSIMODIFY_REFRESH:
1871 r = msi_refresh_record( view, rec, row );
1874 case MSIMODIFY_UPDATE:
1875 r = msi_table_update( view, rec, row );
1878 case MSIMODIFY_ASSIGN:
1879 r = msi_table_assign( view, rec );
1882 case MSIMODIFY_REPLACE:
1883 case MSIMODIFY_MERGE:
1884 case MSIMODIFY_VALIDATE:
1885 case MSIMODIFY_VALIDATE_FIELD:
1886 case MSIMODIFY_VALIDATE_DELETE:
1887 FIXME("%p %d %p - mode not implemented\n", view, eModifyMode, rec );
1888 r = ERROR_CALL_NOT_IMPLEMENTED;
1892 r = ERROR_INVALID_DATA;
1898 static UINT TABLE_delete( struct tagMSIVIEW *view )
1900 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1902 TRACE("%p\n", view );
1909 msi_free( tv->order->reorder );
1910 msi_free( tv->order );
1916 return ERROR_SUCCESS;
1919 static UINT TABLE_find_matching_rows( struct tagMSIVIEW *view, UINT col,
1920 UINT val, UINT *row, MSIITERHANDLE *handle )
1922 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1923 const MSICOLUMNHASHENTRY *entry;
1925 TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
1928 return ERROR_INVALID_PARAMETER;
1930 if( (col==0) || (col > tv->num_cols) )
1931 return ERROR_INVALID_PARAMETER;
1933 if( !tv->columns[col-1].hash_table )
1936 UINT num_rows = tv->table->row_count;
1937 MSICOLUMNHASHENTRY **hash_table;
1938 MSICOLUMNHASHENTRY *new_entry;
1940 if( tv->columns[col-1].offset >= tv->row_size )
1942 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1943 ERR("%p %p\n", tv, tv->columns );
1944 return ERROR_FUNCTION_FAILED;
1947 /* allocate contiguous memory for the table and its entries so we
1948 * don't have to do an expensive cleanup */
1949 hash_table = msi_alloc(MSITABLE_HASH_TABLE_SIZE * sizeof(MSICOLUMNHASHENTRY*) +
1950 num_rows * sizeof(MSICOLUMNHASHENTRY));
1952 return ERROR_OUTOFMEMORY;
1954 memset(hash_table, 0, MSITABLE_HASH_TABLE_SIZE * sizeof(MSICOLUMNHASHENTRY*));
1955 tv->columns[col-1].hash_table = hash_table;
1957 new_entry = (MSICOLUMNHASHENTRY *)(hash_table + MSITABLE_HASH_TABLE_SIZE);
1959 for (i = 0; i < num_rows; i++, new_entry++)
1963 if (view->ops->fetch_int( view, i, col, &row_value ) != ERROR_SUCCESS)
1966 new_entry->next = NULL;
1967 new_entry->value = row_value;
1969 if (hash_table[row_value % MSITABLE_HASH_TABLE_SIZE])
1971 MSICOLUMNHASHENTRY *prev_entry = hash_table[row_value % MSITABLE_HASH_TABLE_SIZE];
1972 while (prev_entry->next)
1973 prev_entry = prev_entry->next;
1974 prev_entry->next = new_entry;
1977 hash_table[row_value % MSITABLE_HASH_TABLE_SIZE] = new_entry;
1982 entry = tv->columns[col-1].hash_table[val % MSITABLE_HASH_TABLE_SIZE];
1984 entry = (*handle)->next;
1986 while (entry && entry->value != val)
1987 entry = entry->next;
1991 return ERROR_NO_MORE_ITEMS;
1995 return ERROR_SUCCESS;
1998 static UINT TABLE_add_ref(struct tagMSIVIEW *view)
2000 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
2003 TRACE("%p %d\n", view, tv->table->ref_count);
2005 for (i = 0; i < tv->table->col_count; i++)
2007 if (tv->table->colinfo[i].type & MSITYPE_TEMPORARY)
2008 InterlockedIncrement(&tv->table->colinfo[i].ref_count);
2011 return InterlockedIncrement(&tv->table->ref_count);
2014 static UINT TABLE_remove_column(struct tagMSIVIEW *view, LPCWSTR table, UINT number)
2016 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
2017 MSIRECORD *rec = NULL;
2018 MSIVIEW *columns = NULL;
2021 rec = MSI_CreateRecord(2);
2023 return ERROR_OUTOFMEMORY;
2025 MSI_RecordSetStringW(rec, 1, table);
2026 MSI_RecordSetInteger(rec, 2, number);
2028 r = TABLE_CreateView(tv->db, szColumns, &columns);
2029 if (r != ERROR_SUCCESS)
2032 r = msi_table_find_row((MSITABLEVIEW *)columns, rec, &row);
2033 if (r != ERROR_SUCCESS)
2036 r = TABLE_delete_row(columns, row);
2037 if (r != ERROR_SUCCESS)
2040 msi_update_table_columns(tv->db, table);
2043 msiobj_release(&rec->hdr);
2044 columns->ops->delete(columns);
2048 static UINT TABLE_release(struct tagMSIVIEW *view)
2050 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
2051 INT ref = tv->table->ref_count;
2054 TRACE("%p %d\n", view, ref);
2056 for (i = 0; i < tv->table->col_count; i++)
2058 if (tv->table->colinfo[i].type & MSITYPE_TEMPORARY)
2060 ref = InterlockedDecrement(&tv->table->colinfo[i].ref_count);
2063 r = TABLE_remove_column(view, tv->table->colinfo[i].tablename,
2064 tv->table->colinfo[i].number);
2065 if (r != ERROR_SUCCESS)
2071 ref = InterlockedDecrement(&tv->table->ref_count);
2074 if (!tv->table->row_count)
2076 list_remove(&tv->table->entry);
2077 free_table(tv->table);
2085 static UINT TABLE_add_column(struct tagMSIVIEW *view, LPCWSTR table, UINT number,
2086 LPCWSTR column, UINT type, BOOL hold)
2088 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
2093 rec = MSI_CreateRecord(4);
2095 return ERROR_OUTOFMEMORY;
2097 MSI_RecordSetStringW(rec, 1, table);
2098 MSI_RecordSetInteger(rec, 2, number);
2099 MSI_RecordSetStringW(rec, 3, column);
2100 MSI_RecordSetInteger(rec, 4, type);
2102 r = TABLE_insert_row(&tv->view, rec, -1, FALSE);
2103 if (r != ERROR_SUCCESS)
2106 msi_update_table_columns(tv->db, table);
2111 msitable = find_cached_table(tv->db, table);
2112 for (i = 0; i < msitable->col_count; i++)
2114 if (!lstrcmpW(msitable->colinfo[i].colname, column))
2116 InterlockedIncrement(&msitable->colinfo[i].ref_count);
2122 msiobj_release(&rec->hdr);
2126 static UINT order_add_column(struct tagMSIVIEW *view, MSIORDERINFO *order, LPCWSTR name)
2129 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
2131 r = TABLE_get_dimensions(view, NULL, &count);
2132 if (r != ERROR_SUCCESS)
2135 if (order->num_cols >= count)
2136 return ERROR_FUNCTION_FAILED;
2138 r = VIEW_find_column(view, name, tv->name, &n);
2139 if (r != ERROR_SUCCESS)
2142 order->cols[order->num_cols] = n;
2143 TRACE("Ordering by column %s (%d)\n", debugstr_w(name), n);
2147 return ERROR_SUCCESS;
2150 static UINT order_compare(struct tagMSIVIEW *view, MSIORDERINFO *order,
2151 UINT a, UINT b, UINT *swap)
2153 UINT r, i, a_val = 0, b_val = 0;
2156 for (i = 0; i < order->num_cols; i++)
2158 r = TABLE_fetch_int(view, a, order->cols[i], &a_val);
2159 if (r != ERROR_SUCCESS)
2162 r = TABLE_fetch_int(view, b, order->cols[i], &b_val);
2163 if (r != ERROR_SUCCESS)
2174 return ERROR_SUCCESS;
2177 static UINT order_mergesort(struct tagMSIVIEW *view, MSIORDERINFO *order,
2178 UINT left, UINT right)
2181 UINT swap = 0, center = (left + right) / 2;
2182 UINT *array = order->reorder;
2185 return ERROR_SUCCESS;
2187 /* sort the left half */
2188 r = order_mergesort(view, order, left, center);
2189 if (r != ERROR_SUCCESS)
2192 /* sort the right half */
2193 r = order_mergesort(view, order, center + 1, right);
2194 if (r != ERROR_SUCCESS)
2197 for (i = left, j = center + 1; (i <= center) && (j <= right); i++)
2199 r = order_compare(view, order, array[i], array[j], &swap);
2200 if (r != ERROR_SUCCESS)
2206 memmove(&array[i + 1], &array[i], (j - i) * sizeof(UINT));
2213 return ERROR_SUCCESS;
2216 static UINT order_verify(struct tagMSIVIEW *view, MSIORDERINFO *order, UINT num_rows)
2220 for (i = 1; i < num_rows; i++)
2222 r = order_compare(view, order, order->reorder[i - 1],
2223 order->reorder[i], &swap);
2224 if (r != ERROR_SUCCESS)
2230 ERR("Bad order! %d\n", i);
2231 return ERROR_FUNCTION_FAILED;
2234 return ERROR_SUCCESS;
2237 static UINT TABLE_sort(struct tagMSIVIEW *view, column_info *columns)
2239 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
2240 MSIORDERINFO *order;
2245 TRACE("sorting table %s\n", debugstr_w(tv->name));
2247 r = TABLE_get_dimensions(view, &rows, &cols);
2248 if (r != ERROR_SUCCESS)
2252 return ERROR_SUCCESS;
2254 order = msi_alloc_zero(sizeof(MSIORDERINFO) + sizeof(UINT) * cols);
2256 return ERROR_OUTOFMEMORY;
2258 for (ptr = columns; ptr; ptr = ptr->next)
2259 order_add_column(view, order, ptr->column);
2261 order->reorder = msi_alloc(rows * sizeof(UINT));
2262 if (!order->reorder)
2263 return ERROR_OUTOFMEMORY;
2265 for (i = 0; i < rows; i++)
2266 order->reorder[i] = i;
2268 r = order_mergesort(view, order, 0, rows - 1);
2269 if (r != ERROR_SUCCESS)
2272 r = order_verify(view, order, rows);
2273 if (r != ERROR_SUCCESS)
2278 return ERROR_SUCCESS;
2281 static UINT TABLE_drop(struct tagMSIVIEW *view)
2283 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
2284 MSIVIEW *tables = NULL;
2285 MSIRECORD *rec = NULL;
2289 TRACE("dropping table %s\n", debugstr_w(tv->name));
2291 for (i = tv->table->col_count - 1; i >= 0; i--)
2293 r = TABLE_remove_column(view, tv->table->colinfo[i].tablename,
2294 tv->table->colinfo[i].number);
2295 if (r != ERROR_SUCCESS)
2299 rec = MSI_CreateRecord(1);
2301 return ERROR_OUTOFMEMORY;
2303 MSI_RecordSetStringW(rec, 1, tv->name);
2305 r = TABLE_CreateView(tv->db, szTables, &tables);
2306 if (r != ERROR_SUCCESS)
2309 r = msi_table_find_row((MSITABLEVIEW *)tables, rec, &row);
2310 if (r != ERROR_SUCCESS)
2313 r = TABLE_delete_row(tables, row);
2314 if (r != ERROR_SUCCESS)
2317 list_remove(&tv->table->entry);
2318 free_table(tv->table);
2322 msiobj_release(&rec->hdr);
2323 tables->ops->delete(tables);
2328 static const MSIVIEWOPS table_ops =
2338 TABLE_get_dimensions,
2339 TABLE_get_column_info,
2342 TABLE_find_matching_rows,
2346 TABLE_remove_column,
2351 UINT TABLE_CreateView( MSIDATABASE *db, LPCWSTR name, MSIVIEW **view )
2356 static const WCHAR Streams[] = {'_','S','t','r','e','a','m','s',0};
2357 static const WCHAR Storages[] = {'_','S','t','o','r','a','g','e','s',0};
2359 TRACE("%p %s %p\n", db, debugstr_w(name), view );
2361 if ( !lstrcmpW( name, Streams ) )
2362 return STREAMS_CreateView( db, view );
2363 else if ( !lstrcmpW( name, Storages ) )
2364 return STORAGES_CreateView( db, view );
2366 sz = sizeof *tv + lstrlenW(name)*sizeof name[0] ;
2367 tv = msi_alloc_zero( sz );
2369 return ERROR_FUNCTION_FAILED;
2371 r = get_table( db, name, &tv->table );
2372 if( r != ERROR_SUCCESS )
2375 WARN("table not found\n");
2379 TRACE("table %p found with %d columns\n", tv->table, tv->table->col_count);
2381 /* fill the structure */
2382 tv->view.ops = &table_ops;
2384 tv->columns = tv->table->colinfo;
2385 tv->num_cols = tv->table->col_count;
2386 tv->row_size = msi_table_get_row_size( db, tv->table->colinfo, tv->table->col_count );
2388 TRACE("%s one row is %d bytes\n", debugstr_w(name), tv->row_size );
2390 *view = (MSIVIEW*) tv;
2391 lstrcpyW( tv->name, name );
2393 return ERROR_SUCCESS;
2396 UINT MSI_CommitTables( MSIDATABASE *db )
2399 MSITABLE *table = NULL;
2403 r = msi_save_string_table( db->strings, db->storage );
2404 if( r != ERROR_SUCCESS )
2406 WARN("failed to save string table r=%08x\n",r);
2410 LIST_FOR_EACH_ENTRY( table, &db->tables, MSITABLE, entry )
2412 r = save_table( db, table );
2413 if( r != ERROR_SUCCESS )
2415 WARN("failed to save table %s (r=%08x)\n",
2416 debugstr_w(table->name), r);
2421 /* force everything to reload next time */
2422 free_cached_tables( db );
2424 return ERROR_SUCCESS;
2427 MSICONDITION MSI_DatabaseIsTablePersistent( MSIDATABASE *db, LPCWSTR table )
2432 TRACE("%p %s\n", db, debugstr_w(table));
2435 return MSICONDITION_ERROR;
2437 r = get_table( db, table, &t );
2438 if (r != ERROR_SUCCESS)
2439 return MSICONDITION_NONE;
2441 return t->persistent;
2444 static UINT read_raw_int(const BYTE *data, UINT col, UINT bytes)
2448 for (i = 0; i < bytes; i++)
2449 ret += (data[col + i] << i * 8);
2454 static UINT msi_record_encoded_stream_name( const MSITABLEVIEW *tv, MSIRECORD *rec, LPWSTR *pstname )
2456 LPWSTR stname = NULL, sval, p;
2460 TRACE("%p %p\n", tv, rec);
2462 len = lstrlenW( tv->name ) + 1;
2463 stname = msi_alloc( len*sizeof(WCHAR) );
2466 r = ERROR_OUTOFMEMORY;
2470 lstrcpyW( stname, tv->name );
2472 for ( i = 0; i < tv->num_cols; i++ )
2474 if ( tv->columns[i].type & MSITYPE_KEY )
2476 sval = msi_dup_record_field( rec, i + 1 );
2479 r = ERROR_OUTOFMEMORY;
2483 len += lstrlenW( szDot ) + lstrlenW ( sval );
2484 p = msi_realloc ( stname, len*sizeof(WCHAR) );
2487 r = ERROR_OUTOFMEMORY;
2492 lstrcatW( stname, szDot );
2493 lstrcatW( stname, sval );
2501 *pstname = encode_streamname( FALSE, stname );
2504 return ERROR_SUCCESS;
2507 msi_free ( stname );
2512 static MSIRECORD *msi_get_transform_record( const MSITABLEVIEW *tv, const string_table *st,
2514 const BYTE *rawdata, UINT bytes_per_strref )
2516 UINT i, val, ofs = 0;
2518 MSICOLUMNINFO *columns = tv->columns;
2521 mask = rawdata[0] | (rawdata[1] << 8);
2524 rec = MSI_CreateRecord( tv->num_cols );
2529 for( i=0; i<tv->num_cols; i++ )
2531 if ( (mask&1) && (i>=(mask>>8)) )
2533 /* all keys must be present */
2534 if ( (~mask&1) && (~columns[i].type & MSITYPE_KEY) && ((1<<i) & ~mask) )
2537 if( MSITYPE_IS_BINARY(tv->columns[i].type) )
2540 IStream *stm = NULL;
2543 ofs += bytes_per_column( tv->db, &columns[i] );
2545 r = msi_record_encoded_stream_name( tv, rec, &encname );
2546 if ( r != ERROR_SUCCESS )
2549 r = IStorage_OpenStream( stg, encname, NULL,
2550 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stm );
2551 msi_free( encname );
2552 if ( r != ERROR_SUCCESS )
2555 MSI_RecordSetStream( rec, i+1, stm );
2556 TRACE(" field %d [%s]\n", i+1, debugstr_w(encname));
2558 else if( columns[i].type & MSITYPE_STRING )
2562 val = read_raw_int(rawdata, ofs, bytes_per_strref);
2563 sval = msi_string_lookup_id( st, val );
2564 MSI_RecordSetStringW( rec, i+1, sval );
2565 TRACE(" field %d [%s]\n", i+1, debugstr_w(sval));
2566 ofs += bytes_per_strref;
2570 UINT n = bytes_per_column( tv->db, &columns[i] );
2574 val = read_raw_int(rawdata, ofs, n);
2576 MSI_RecordSetInteger( rec, i+1, val^0x8000 );
2577 TRACE(" field %d [0x%04x]\n", i+1, val );
2580 val = read_raw_int(rawdata, ofs, n);
2582 MSI_RecordSetInteger( rec, i+1, val^0x80000000 );
2583 TRACE(" field %d [0x%08x]\n", i+1, val );
2586 ERR("oops - unknown column width %d\n", n);
2595 static void dump_record( MSIRECORD *rec )
2599 n = MSI_RecordGetFieldCount( rec );
2600 for( i=1; i<=n; i++ )
2602 LPCWSTR sval = MSI_RecordGetString( rec, i );
2604 if( MSI_RecordIsNull( rec, i ) )
2605 TRACE("row -> []\n");
2606 else if( (sval = MSI_RecordGetString( rec, i )) )
2607 TRACE("row -> [%s]\n", debugstr_w(sval));
2609 TRACE("row -> [0x%08x]\n", MSI_RecordGetInteger( rec, i ) );
2613 static void dump_table( const string_table *st, const USHORT *rawdata, UINT rawsize )
2618 for( i=0; i<(rawsize/2); i++ )
2620 sval = msi_string_lookup_id( st, rawdata[i] );
2621 MESSAGE(" %04x %s\n", rawdata[i], debugstr_w(sval) );
2625 static UINT* msi_record_to_row( const MSITABLEVIEW *tv, MSIRECORD *rec )
2630 data = msi_alloc( tv->num_cols *sizeof (UINT) );
2631 for( i=0; i<tv->num_cols; i++ )
2635 if ( ~tv->columns[i].type & MSITYPE_KEY )
2638 /* turn the transform column value into a row value */
2639 if ( ( tv->columns[i].type & MSITYPE_STRING ) &&
2640 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
2642 str = MSI_RecordGetString( rec, i+1 );
2643 r = msi_string2idW( tv->db->strings, str, &data[i] );
2645 /* if there's no matching string in the string table,
2646 these keys can't match any record, so fail now. */
2647 if( ERROR_SUCCESS != r )
2655 data[i] = MSI_RecordGetInteger( rec, i+1 );
2657 if (data[i] == MSI_NULL_INTEGER)
2659 else if ((tv->columns[i].type&0xff) == 2)
2662 data[i] += 0x80000000;
2668 static UINT msi_row_matches( MSITABLEVIEW *tv, UINT row, const UINT *data )
2670 UINT i, r, x, ret = ERROR_FUNCTION_FAILED;
2672 for( i=0; i<tv->num_cols; i++ )
2674 if ( ~tv->columns[i].type & MSITYPE_KEY )
2677 /* turn the transform column value into a row value */
2678 r = TABLE_fetch_int( &tv->view, row, i+1, &x );
2679 if ( r != ERROR_SUCCESS )
2681 ERR("TABLE_fetch_int shouldn't fail here\n");
2685 /* if this key matches, move to the next column */
2688 ret = ERROR_FUNCTION_FAILED;
2692 ret = ERROR_SUCCESS;
2698 static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row )
2700 UINT i, r = ERROR_FUNCTION_FAILED, *data;
2702 data = msi_record_to_row( tv, rec );
2705 for( i = 0; i < tv->table->row_count; i++ )
2707 r = msi_row_matches( tv, i, data );
2708 if( r == ERROR_SUCCESS )
2724 static UINT msi_table_load_transform( MSIDATABASE *db, IStorage *stg,
2725 string_table *st, TRANSFORMDATA *transform,
2726 UINT bytes_per_strref )
2729 BYTE *rawdata = NULL;
2730 MSITABLEVIEW *tv = NULL;
2731 UINT r, n, sz, i, mask;
2732 MSIRECORD *rec = NULL;
2738 return ERROR_SUCCESS;
2740 name = transform->name;
2743 TRACE("%p %p %p %s\n", db, stg, st, debugstr_w(name) );
2745 /* read the transform data */
2746 read_stream_data( stg, name, TRUE, &rawdata, &rawsize );
2749 TRACE("table %s empty\n", debugstr_w(name) );
2750 return ERROR_INVALID_TABLE;
2753 /* create a table view */
2754 r = TABLE_CreateView( db, name, (MSIVIEW**) &tv );
2755 if( r != ERROR_SUCCESS )
2758 r = tv->view.ops->execute( &tv->view, NULL );
2759 if( r != ERROR_SUCCESS )
2762 TRACE("name = %s columns = %u row_size = %u raw size = %u\n",
2763 debugstr_w(name), tv->num_cols, tv->row_size, rawsize );
2765 /* interpret the data */
2767 for( n=0; n < rawsize; )
2769 mask = rawdata[n] | (rawdata[n+1] << 8);
2774 * if the low bit is set, columns are continuous and
2775 * the number of columns is specified in the high byte
2778 for( i=0; i<tv->num_cols; i++ )
2780 if( (tv->columns[i].type & MSITYPE_STRING) &&
2781 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
2782 sz += bytes_per_strref;
2784 sz += bytes_per_column( tv->db, &tv->columns[i] );
2790 * If the low bit is not set, mask is a bitmask.
2791 * Excepting for key fields, which are always present,
2792 * each bit indicates that a field is present in the transform record.
2794 * mask == 0 is a special case ... only the keys will be present
2795 * and it means that this row should be deleted.
2798 for( i=0; i<tv->num_cols; i++ )
2800 if( (tv->columns[i].type & MSITYPE_KEY) || ((1<<i)&mask))
2802 if( (tv->columns[i].type & MSITYPE_STRING) &&
2803 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
2804 sz += bytes_per_strref;
2806 sz += bytes_per_column( tv->db, &tv->columns[i] );
2811 /* check we didn't run of the end of the table */
2812 if ( (n+sz) > rawsize )
2815 dump_table( st, (USHORT *)rawdata, rawsize );
2819 rec = msi_get_transform_record( tv, st, stg, &rawdata[n], bytes_per_strref );
2826 UINT number = MSI_NULL_INTEGER;
2828 TRACE("inserting record\n");
2830 if (!lstrcmpW(name, szColumns))
2832 MSI_RecordGetStringW( rec, 1, table, &sz );
2833 number = MSI_RecordGetInteger( rec, 2 );
2836 * Native msi seems writes nul into the Number (2nd) column of
2837 * the _Columns table, only when the columns are from a new table
2839 if ( number == MSI_NULL_INTEGER )
2841 /* reset the column number on a new table */
2842 if ( lstrcmpW(coltable, table) )
2845 lstrcpyW( coltable, table );
2848 /* fix nul column numbers */
2849 MSI_RecordSetInteger( rec, 2, ++colcol );
2853 r = TABLE_insert_row( &tv->view, rec, -1, FALSE );
2854 if (r != ERROR_SUCCESS)
2855 WARN("insert row failed\n");
2857 if ( number != MSI_NULL_INTEGER && !lstrcmpW(name, szColumns) )
2858 msi_update_table_columns( db, table );
2864 r = msi_table_find_row( tv, rec, &row );
2865 if (r != ERROR_SUCCESS)
2866 WARN("no matching row to transform\n");
2869 TRACE("modifying row [%d]:\n", row);
2870 TABLE_set_row( &tv->view, row, rec, mask );
2874 TRACE("deleting row [%d]:\n", row);
2875 TABLE_delete_row( &tv->view, row );
2878 if( TRACE_ON(msidb) ) dump_record( rec );
2879 msiobj_release( &rec->hdr );
2886 /* no need to free the table, it's associated with the database */
2887 msi_free( rawdata );
2889 tv->view.ops->delete( &tv->view );
2891 return ERROR_SUCCESS;
2895 * msi_table_apply_transform
2897 * Enumerate the table transforms in a transform storage and apply each one.
2899 UINT msi_table_apply_transform( MSIDATABASE *db, IStorage *stg )
2901 struct list transforms;
2902 IEnumSTATSTG *stgenum = NULL;
2903 TRANSFORMDATA *transform;
2904 TRANSFORMDATA *tables = NULL, *columns = NULL;
2907 string_table *strings;
2908 UINT ret = ERROR_FUNCTION_FAILED;
2909 UINT bytes_per_strref;
2911 TRACE("%p %p\n", db, stg );
2913 strings = msi_load_string_table( stg, &bytes_per_strref );
2917 r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
2921 list_init(&transforms);
2925 MSITABLEVIEW *tv = NULL;
2929 r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
2930 if ( FAILED( r ) || !count )
2933 decode_streamname( stat.pwcsName, name );
2934 CoTaskMemFree( stat.pwcsName );
2935 if ( name[0] != 0x4840 )
2938 if ( !lstrcmpW( name+1, szStringPool ) ||
2939 !lstrcmpW( name+1, szStringData ) )
2942 transform = msi_alloc_zero( sizeof(TRANSFORMDATA) );
2946 list_add_tail( &transforms, &transform->entry );
2948 transform->name = strdupW( name + 1 );
2950 if ( !lstrcmpW( transform->name, szTables ) )
2952 else if (!lstrcmpW( transform->name, szColumns ) )
2953 columns = transform;
2955 TRACE("transform contains stream %s\n", debugstr_w(name));
2957 /* load the table */
2958 r = TABLE_CreateView( db, transform->name, (MSIVIEW**) &tv );
2959 if( r != ERROR_SUCCESS )
2962 r = tv->view.ops->execute( &tv->view, NULL );
2963 if( r != ERROR_SUCCESS )
2965 tv->view.ops->delete( &tv->view );
2969 tv->view.ops->delete( &tv->view );
2973 * Apply _Tables and _Columns transforms first so that
2974 * the table metadata is correct, and empty tables exist.
2976 ret = msi_table_load_transform( db, stg, strings, tables, bytes_per_strref );
2977 if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
2980 ret = msi_table_load_transform( db, stg, strings, columns, bytes_per_strref );
2981 if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
2984 ret = ERROR_SUCCESS;
2986 while ( !list_empty( &transforms ) )
2988 transform = LIST_ENTRY( list_head( &transforms ), TRANSFORMDATA, entry );
2990 if ( lstrcmpW( transform->name, szColumns ) &&
2991 lstrcmpW( transform->name, szTables ) &&
2992 ret == ERROR_SUCCESS )
2994 ret = msi_table_load_transform( db, stg, strings, transform, bytes_per_strref );
2997 list_remove( &transform->entry );
2998 msi_free( transform->name );
2999 msi_free( transform );
3002 if ( ret == ERROR_SUCCESS )
3003 append_storage_to_db( db, stg );
3007 IEnumSTATSTG_Release( stgenum );
3009 msi_destroy_stringtable( strings );
3014 void append_storage_to_db( MSIDATABASE *db, IStorage *stg )
3018 t = msi_alloc( sizeof *t );
3020 IStorage_AddRef( stg );
3021 list_add_tail( &db->transforms, &t->entry );
3024 void msi_free_transforms( MSIDATABASE *db )
3026 while( !list_empty( &db->transforms ) )
3028 MSITRANSFORM *t = LIST_ENTRY( list_head( &db->transforms ),
3029 MSITRANSFORM, entry );
3030 list_remove( &t->entry );
3031 IStorage_Release( t->stg );