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 typedef struct tagMSISTREAM {
96 static const WCHAR szStringData[] = {
97 '_','S','t','r','i','n','g','D','a','t','a',0 };
98 static const WCHAR szStringPool[] = {
99 '_','S','t','r','i','n','g','P','o','o','l',0 };
101 /* information for default tables */
102 static WCHAR szTables[] = { '_','T','a','b','l','e','s',0 };
103 static WCHAR szTable[] = { 'T','a','b','l','e',0 };
104 static WCHAR szName[] = { 'N','a','m','e',0 };
105 static WCHAR szColumns[] = { '_','C','o','l','u','m','n','s',0 };
106 static WCHAR szNumber[] = { 'N','u','m','b','e','r',0 };
107 static WCHAR szType[] = { 'T','y','p','e',0 };
109 static const MSICOLUMNINFO _Columns_cols[4] = {
110 { szColumns, 1, szTable, MSITYPE_VALID | MSITYPE_STRING | MSITYPE_KEY | 64, 0, 0, 0, NULL },
111 { szColumns, 2, szNumber, MSITYPE_VALID | MSITYPE_KEY | 2, 2, 0, 0, NULL },
112 { szColumns, 3, szName, MSITYPE_VALID | MSITYPE_STRING | 64, 4, 0, 0, NULL },
113 { szColumns, 4, szType, MSITYPE_VALID | 2, 6, 0, 0, NULL },
116 static const MSICOLUMNINFO _Tables_cols[1] = {
117 { szTables, 1, szName, MSITYPE_VALID | MSITYPE_STRING | MSITYPE_KEY | 64, 0, 0, 0, NULL },
120 #define MAX_STREAM_NAME 0x1f
122 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name,
123 MSICOLUMNINFO **pcols, UINT *pcount );
124 static void table_calc_column_offsets( MSIDATABASE *db, MSICOLUMNINFO *colinfo,
126 static UINT get_tablecolumns( MSIDATABASE *db,
127 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz);
128 static void msi_free_colinfo( MSICOLUMNINFO *colinfo, UINT count );
130 static inline UINT bytes_per_column( MSIDATABASE *db, const MSICOLUMNINFO *col )
132 if( MSITYPE_IS_BINARY(col->type) )
135 if( col->type & MSITYPE_STRING )
136 return db->bytes_per_strref;
138 if( (col->type & 0xff) <= 2)
141 if( (col->type & 0xff) != 4 )
142 ERR("Invalid column size!\n");
147 static int utf2mime(int x)
149 if( (x>='0') && (x<='9') )
151 if( (x>='A') && (x<='Z') )
153 if( (x>='a') && (x<='z') )
162 static LPWSTR encode_streamname(BOOL bTable, LPCWSTR in)
164 DWORD count = MAX_STREAM_NAME;
169 count = lstrlenW( in )+2;
170 out = msi_alloc( count*sizeof(WCHAR) );
186 if( ( ch < 0x80 ) && ( utf2mime(ch) >= 0 ) )
188 ch = utf2mime(ch) + 0x4800;
190 if( next && (next<0x80) )
192 next = utf2mime(next);
203 ERR("Failed to encode stream name (%s)\n",debugstr_w(in));
208 static int mime2utf(int x)
215 return x - 10 - 26 + 'a';
216 if( x == (10+26+26) )
221 BOOL decode_streamname(LPCWSTR in, LPWSTR out)
226 while ( (ch = *in++) )
228 if( (ch >= 0x3800 ) && (ch < 0x4840 ) )
231 ch = mime2utf(ch-0x4800);
235 *out++ = mime2utf(ch&0x3f);
237 ch = mime2utf((ch>>6)&0x3f);
247 void enum_stream_names( IStorage *stg )
249 IEnumSTATSTG *stgenum = NULL;
255 r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
263 r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
264 if( FAILED( r ) || !count )
266 decode_streamname( stat.pwcsName, name );
267 TRACE("stream %2d -> %s %s\n", n,
268 debugstr_w(stat.pwcsName), debugstr_w(name) );
269 CoTaskMemFree( stat.pwcsName );
273 IEnumSTATSTG_Release( stgenum );
276 UINT read_stream_data( IStorage *stg, LPCWSTR stname, BOOL table,
277 BYTE **pdata, UINT *psz )
280 UINT ret = ERROR_FUNCTION_FAILED;
287 encname = encode_streamname(table, stname);
289 TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
291 r = IStorage_OpenStream(stg, encname, NULL,
292 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stm);
296 WARN("open stream failed r = %08x - empty table?\n", r);
300 r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
303 WARN("open stream failed r = %08x!\n", r);
307 if( stat.cbSize.QuadPart >> 32 )
313 sz = stat.cbSize.QuadPart;
314 data = msi_alloc( sz );
317 WARN("couldn't allocate memory r=%08x!\n", r);
318 ret = ERROR_NOT_ENOUGH_MEMORY;
322 r = IStream_Read(stm, data, sz, &count );
323 if( FAILED( r ) || ( count != sz ) )
326 WARN("read stream failed r = %08x!\n", r);
335 IStream_Release( stm );
340 static UINT find_open_stream( MSIDATABASE *db, LPCWSTR name, IStream **stm )
344 LIST_FOR_EACH_ENTRY( stream, &db->streams, MSISTREAM, entry )
349 r = IStream_Stat( stream->stm, &stat, 0 );
352 WARN("failed to stat stream r = %08x!\n", r);
356 if( !lstrcmpW( name, stat.pwcsName ) )
358 TRACE("found %s\n", debugstr_w(name));
360 CoTaskMemFree( stat.pwcsName );
361 return ERROR_SUCCESS;
364 CoTaskMemFree( stat.pwcsName );
367 return ERROR_FUNCTION_FAILED;
370 static UINT clone_open_stream( MSIDATABASE *db, LPCWSTR name, IStream **stm )
374 if (find_open_stream( db, name, &stream ) == ERROR_SUCCESS)
379 r = IStream_Clone( stream, stm );
382 WARN("failed to clone stream r = %08x!\n", r);
383 return ERROR_FUNCTION_FAILED;
387 r = IStream_Seek( *stm, pos, STREAM_SEEK_SET, NULL );
390 IStream_Release( *stm );
391 return ERROR_FUNCTION_FAILED;
394 return ERROR_SUCCESS;
397 return ERROR_FUNCTION_FAILED;
400 static UINT db_get_raw_stream( MSIDATABASE *db, LPCWSTR stname, IStream **stm )
405 encname = encode_streamname(FALSE, stname);
407 TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
409 if (clone_open_stream( db, encname, stm ) == ERROR_SUCCESS)
412 return ERROR_SUCCESS;
415 r = IStorage_OpenStream(db->storage, encname, NULL,
416 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, stm);
419 MSITRANSFORM *transform;
421 LIST_FOR_EACH_ENTRY( transform, &db->transforms, MSITRANSFORM, entry )
423 TRACE("looking for %s in transform storage\n", debugstr_w(stname) );
424 r = IStorage_OpenStream( transform->stg, encname, NULL,
425 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, stm );
437 stream = msi_alloc( sizeof(MSISTREAM) );
439 return ERROR_NOT_ENOUGH_MEMORY;
442 IStream_AddRef( *stm );
443 list_add_tail( &db->streams, &stream->entry );
446 return ERROR_SUCCESS;
449 UINT read_raw_stream_data( MSIDATABASE *db, LPCWSTR stname,
450 USHORT **pdata, UINT *psz )
453 UINT ret = ERROR_FUNCTION_FAILED;
459 r = db_get_raw_stream( db, stname, &stm );
460 if( r != ERROR_SUCCESS)
462 r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
465 WARN("open stream failed r = %08x!\n", r);
469 if( stat.cbSize.QuadPart >> 32 )
475 sz = stat.cbSize.QuadPart;
476 data = msi_alloc( sz );
479 WARN("couldn't allocate memory r=%08x!\n", r);
480 ret = ERROR_NOT_ENOUGH_MEMORY;
484 r = IStream_Read(stm, data, sz, &count );
485 if( FAILED( r ) || ( count != sz ) )
488 WARN("read stream failed r = %08x!\n", r);
497 IStream_Release( stm );
502 UINT write_stream_data( IStorage *stg, LPCWSTR stname,
503 LPCVOID data, UINT sz, BOOL bTable )
506 UINT ret = ERROR_FUNCTION_FAILED;
513 encname = encode_streamname(bTable, stname );
514 r = IStorage_OpenStream( stg, encname, NULL,
515 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, &stm);
518 r = IStorage_CreateStream( stg, encname,
519 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
524 WARN("open stream failed r = %08x\n", r);
529 r = IStream_SetSize( stm, size );
532 WARN("Failed to SetSize\n");
537 r = IStream_Seek( stm, pos, STREAM_SEEK_SET, NULL );
540 WARN("Failed to Seek\n");
546 r = IStream_Write(stm, data, sz, &count );
547 if( FAILED( r ) || ( count != sz ) )
549 WARN("Failed to Write\n");
557 IStream_Release( stm );
562 static void free_table( MSITABLE *table )
565 for( i=0; i<table->row_count; i++ )
566 msi_free( table->data[i] );
567 msi_free( table->data );
568 msi_free( table->data_persistent );
569 msi_free_colinfo( table->colinfo, table->col_count );
570 msi_free( table->colinfo );
574 static UINT msi_table_get_row_size( MSIDATABASE *db,const MSICOLUMNINFO *cols,
577 const MSICOLUMNINFO *last_col = &cols[count-1];
580 return last_col->offset + bytes_per_column( db, last_col );
583 /* add this table to the list of cached tables in the database */
584 static UINT read_table_from_storage( MSIDATABASE *db, MSITABLE *t, IStorage *stg )
586 BYTE *rawdata = NULL;
587 UINT rawsize = 0, i, j, row_size = 0;
589 TRACE("%s\n",debugstr_w(t->name));
591 row_size = msi_table_get_row_size( db, t->colinfo, t->col_count );
593 /* if we can't read the table, just assume that it's empty */
594 read_stream_data( stg, t->name, TRUE, &rawdata, &rawsize );
596 return ERROR_SUCCESS;
598 TRACE("Read %d bytes\n", rawsize );
600 if( rawsize % row_size )
602 WARN("Table size is invalid %d/%d\n", rawsize, row_size );
606 t->row_count = rawsize / row_size;
607 t->data = msi_alloc_zero( t->row_count * sizeof (USHORT*) );
610 t->data_persistent = msi_alloc_zero( t->row_count * sizeof(BOOL));
611 if ( !t->data_persistent )
614 /* transpose all the data */
615 TRACE("Transposing data from %d rows\n", t->row_count );
616 for( i=0; i<t->row_count; i++ )
618 t->data[i] = msi_alloc( row_size );
621 t->data_persistent[i] = TRUE;
623 for( j=0; j<t->col_count; j++ )
625 UINT ofs = t->colinfo[j].offset;
626 UINT n = bytes_per_column( db, &t->colinfo[j] );
629 if ( n != 2 && n != 3 && n != 4 )
631 ERR("oops - unknown column width %d\n", n);
635 for ( k = 0; k < n; k++ )
636 t->data[i][ofs + k] = rawdata[ofs*t->row_count + i * n + k];
641 return ERROR_SUCCESS;
644 return ERROR_FUNCTION_FAILED;
647 void free_cached_tables( MSIDATABASE *db )
649 while( !list_empty( &db->tables ) )
651 MSITABLE *t = LIST_ENTRY( list_head( &db->tables ), MSITABLE, entry );
653 list_remove( &t->entry );
658 static MSITABLE *find_cached_table( MSIDATABASE *db, LPCWSTR name )
662 LIST_FOR_EACH_ENTRY( t, &db->tables, MSITABLE, entry )
663 if( !lstrcmpW( name, t->name ) )
669 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name, MSICOLUMNINFO **pcols, UINT *pcount )
671 UINT r, column_count = 0;
672 MSICOLUMNINFO *columns;
674 /* get the number of columns in this table */
676 r = get_tablecolumns( db, name, NULL, &column_count );
677 if( r != ERROR_SUCCESS )
680 *pcount = column_count;
682 /* if there's no columns, there's no table */
683 if( column_count == 0 )
684 return ERROR_INVALID_PARAMETER;
686 TRACE("Table %s found\n", debugstr_w(name) );
688 columns = msi_alloc( column_count*sizeof (MSICOLUMNINFO) );
690 return ERROR_FUNCTION_FAILED;
692 r = get_tablecolumns( db, name, columns, &column_count );
693 if( r != ERROR_SUCCESS )
696 return ERROR_FUNCTION_FAILED;
704 UINT msi_create_table( MSIDATABASE *db, LPCWSTR name, column_info *col_info,
705 MSICONDITION persistent, MSITABLE **table_ret)
709 MSIRECORD *rec = NULL;
714 /* only add tables that don't exist already */
715 if( TABLE_Exists(db, name ) )
717 WARN("table %s exists\n", debugstr_w(name));
718 return ERROR_BAD_QUERY_SYNTAX;
721 table = msi_alloc( sizeof (MSITABLE) + lstrlenW(name)*sizeof (WCHAR) );
723 return ERROR_FUNCTION_FAILED;
725 table->ref_count = 1;
726 table->row_count = 0;
728 table->data_persistent = NULL;
729 table->colinfo = NULL;
730 table->col_count = 0;
731 table->persistent = persistent;
732 lstrcpyW( table->name, name );
734 for( col = col_info; col; col = col->next )
737 table->colinfo = msi_alloc( table->col_count * sizeof(MSICOLUMNINFO) );
741 return ERROR_FUNCTION_FAILED;
744 for( i = 0, col = col_info; col; i++, col = col->next )
746 table->colinfo[ i ].tablename = strdupW( col->table );
747 table->colinfo[ i ].number = i + 1;
748 table->colinfo[ i ].colname = strdupW( col->column );
749 table->colinfo[ i ].type = col->type;
750 table->colinfo[ i ].offset = 0;
751 table->colinfo[ i ].ref_count = 0;
752 table->colinfo[ i ].hash_table = NULL;
753 table->colinfo[ i ].temporary = col->temporary;
755 table_calc_column_offsets( db, table->colinfo, table->col_count);
757 r = TABLE_CreateView( db, szTables, &tv );
758 TRACE("CreateView returned %x\n", r);
765 r = tv->ops->execute( tv, 0 );
766 TRACE("tv execute returned %x\n", r);
770 rec = MSI_CreateRecord( 1 );
774 r = MSI_RecordSetStringW( rec, 1, name );
778 r = tv->ops->insert_row( tv, rec, -1, persistent == MSICONDITION_FALSE );
779 TRACE("insert_row returned %x\n", r);
783 tv->ops->delete( tv );
786 msiobj_release( &rec->hdr );
789 if( persistent != MSICONDITION_FALSE )
791 /* add each column to the _Columns table */
792 r = TABLE_CreateView( db, szColumns, &tv );
796 r = tv->ops->execute( tv, 0 );
797 TRACE("tv execute returned %x\n", r);
801 rec = MSI_CreateRecord( 4 );
805 r = MSI_RecordSetStringW( rec, 1, name );
810 * need to set the table, column number, col name and type
811 * for each column we enter in the table
814 for( col = col_info; col; col = col->next )
816 r = MSI_RecordSetInteger( rec, 2, nField );
820 r = MSI_RecordSetStringW( rec, 3, col->column );
824 r = MSI_RecordSetInteger( rec, 4, col->type );
828 r = tv->ops->insert_row( tv, rec, -1, FALSE );
840 msiobj_release( &rec->hdr );
841 /* FIXME: remove values from the string table on error */
843 tv->ops->delete( tv );
845 if (r == ERROR_SUCCESS)
847 list_add_head( &db->tables, &table->entry );
856 static UINT get_table( MSIDATABASE *db, LPCWSTR name, MSITABLE **table_ret )
861 /* first, see if the table is cached */
862 table = find_cached_table( db, name );
866 return ERROR_SUCCESS;
869 /* nonexistent tables should be interpreted as empty tables */
870 table = msi_alloc( sizeof (MSITABLE) + lstrlenW(name)*sizeof (WCHAR) );
872 return ERROR_FUNCTION_FAILED;
874 table->row_count = 0;
876 table->data_persistent = NULL;
877 table->colinfo = NULL;
878 table->col_count = 0;
879 table->persistent = MSICONDITION_TRUE;
880 lstrcpyW( table->name, name );
882 if ( !lstrcmpW(name, szTables) || !lstrcmpW(name, szColumns) )
883 table->persistent = MSICONDITION_NONE;
885 r = table_get_column_info( db, name, &table->colinfo, &table->col_count);
886 if (r != ERROR_SUCCESS)
888 free_table ( table );
892 r = read_table_from_storage( db, table, db->storage );
893 if( r != ERROR_SUCCESS )
899 list_add_head( &db->tables, &table->entry );
901 return ERROR_SUCCESS;
904 static UINT save_table( MSIDATABASE *db, const MSITABLE *t )
906 BYTE *rawdata = NULL, *p;
907 UINT rawsize, r, i, j, row_size;
909 /* Nothing to do for non-persistent tables */
910 if( t->persistent == MSICONDITION_FALSE )
911 return ERROR_SUCCESS;
913 TRACE("Saving %s\n", debugstr_w( t->name ) );
915 row_size = msi_table_get_row_size( db, t->colinfo, t->col_count );
917 rawsize = t->row_count * row_size;
918 rawdata = msi_alloc_zero( rawsize );
921 r = ERROR_NOT_ENOUGH_MEMORY;
927 for( i=0; i<t->col_count; i++ )
929 for( j=0; j<t->row_count; j++ )
931 UINT offset = t->colinfo[i].offset;
933 if (!t->data_persistent[j]) continue;
937 *p++ = t->data[j][offset];
938 *p++ = t->data[j][offset + 1];
939 if( 4 == bytes_per_column( db, &t->colinfo[i] ) )
941 *p++ = t->data[j][offset + 2];
942 *p++ = t->data[j][offset + 3];
947 TRACE("writing %d bytes\n", rawsize);
948 r = write_stream_data( db->storage, t->name, rawdata, rawsize, TRUE );
956 static void table_calc_column_offsets( MSIDATABASE *db, MSICOLUMNINFO *colinfo,
961 for( i=0; colinfo && (i<count); i++ )
963 assert( (i+1) == colinfo[ i ].number );
965 colinfo[i].offset = colinfo[ i - 1 ].offset
966 + bytes_per_column( db, &colinfo[ i - 1 ] );
968 colinfo[i].offset = 0;
969 TRACE("column %d is [%s] with type %08x ofs %d\n",
970 colinfo[i].number, debugstr_w(colinfo[i].colname),
971 colinfo[i].type, colinfo[i].offset);
975 static UINT get_defaulttablecolumns( MSIDATABASE *db, LPCWSTR name,
976 MSICOLUMNINFO *colinfo, UINT *sz)
978 const MSICOLUMNINFO *p;
981 TRACE("%s\n", debugstr_w(name));
983 if (!lstrcmpW( name, szTables ))
988 else if (!lstrcmpW( name, szColumns ))
994 return ERROR_FUNCTION_FAILED;
996 /* duplicate the string data so we can free it in msi_free_colinfo */
999 if (colinfo && (i < *sz) )
1002 colinfo[i].tablename = strdupW( p[i].tablename );
1003 colinfo[i].colname = strdupW( p[i].colname );
1005 if( colinfo && (i >= *sz) )
1008 table_calc_column_offsets( db, colinfo, n );
1010 return ERROR_SUCCESS;
1013 static void msi_free_colinfo( MSICOLUMNINFO *colinfo, UINT count )
1017 for( i=0; i<count; i++ )
1019 msi_free( colinfo[i].tablename );
1020 msi_free( colinfo[i].colname );
1021 msi_free( colinfo[i].hash_table );
1025 static LPWSTR msi_makestring( const MSIDATABASE *db, UINT stringid)
1027 return strdupW(msi_string_lookup_id( db->strings, stringid ));
1030 static UINT read_table_int(BYTE *const *data, UINT row, UINT col, UINT bytes)
1034 for (i = 0; i < bytes; i++)
1035 ret += (data[row][col + i] << i * 8);
1040 static UINT get_tablecolumns( MSIDATABASE *db,
1041 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz)
1043 UINT r, i, n=0, table_id, count, maxcount = *sz;
1044 MSITABLE *table = NULL;
1046 TRACE("%s\n", debugstr_w(szTableName));
1048 /* first check if there is a default table with that name */
1049 r = get_defaulttablecolumns( db, szTableName, colinfo, sz );
1050 if( ( r == ERROR_SUCCESS ) && *sz )
1053 r = get_table( db, szColumns, &table );
1054 if( r != ERROR_SUCCESS )
1056 ERR("couldn't load _Columns table\n");
1057 return ERROR_FUNCTION_FAILED;
1060 /* convert table and column names to IDs from the string table */
1061 r = msi_string2idW( db->strings, szTableName, &table_id );
1062 if( r != ERROR_SUCCESS )
1064 WARN("Couldn't find id for %s\n", debugstr_w(szTableName));
1068 TRACE("Table id is %d, row count is %d\n", table_id, table->row_count);
1070 /* Note: _Columns table doesn't have non-persistent data */
1072 /* if maxcount is non-zero, assume it's exactly right for this table */
1073 memset( colinfo, 0, maxcount*sizeof(*colinfo) );
1074 count = table->row_count;
1075 for( i=0; i<count; i++ )
1077 if( read_table_int(table->data, i, 0, db->bytes_per_strref) != table_id )
1081 UINT id = read_table_int(table->data, i, table->colinfo[2].offset, db->bytes_per_strref);
1082 UINT col = read_table_int(table->data, i, table->colinfo[1].offset, sizeof(USHORT)) - (1<<15);
1084 /* check the column number is in range */
1085 if (col<1 || col>maxcount)
1087 ERR("column %d out of range\n", col);
1091 /* check if this column was already set */
1092 if (colinfo[ col - 1 ].number)
1094 ERR("duplicate column %d\n", col);
1098 colinfo[ col - 1 ].tablename = msi_makestring( db, table_id );
1099 colinfo[ col - 1 ].number = col;
1100 colinfo[ col - 1 ].colname = msi_makestring( db, id );
1101 colinfo[ col - 1 ].type = read_table_int(table->data, i,
1102 table->colinfo[3].offset,
1103 sizeof(USHORT)) - (1<<15);
1104 colinfo[ col - 1 ].offset = 0;
1105 colinfo[ col - 1 ].ref_count = 0;
1106 colinfo[ col - 1 ].hash_table = NULL;
1111 TRACE("%s has %d columns\n", debugstr_w(szTableName), n);
1113 if (colinfo && n != maxcount)
1115 ERR("missing column in table %s\n", debugstr_w(szTableName));
1116 msi_free_colinfo(colinfo, maxcount );
1117 return ERROR_FUNCTION_FAILED;
1120 table_calc_column_offsets( db, colinfo, n );
1123 return ERROR_SUCCESS;
1126 static void msi_update_table_columns( MSIDATABASE *db, LPCWSTR name )
1130 UINT size, offset, old_count;
1133 /* We may free name in msi_free_colinfo. */
1134 tablename = strdupW( name );
1136 table = find_cached_table( db, tablename );
1137 old_count = table->col_count;
1138 msi_free_colinfo( table->colinfo, table->col_count );
1139 msi_free( table->colinfo );
1140 table->colinfo = NULL;
1142 table_get_column_info( db, tablename, &table->colinfo, &table->col_count );
1143 if (!table->col_count)
1146 size = msi_table_get_row_size( db, table->colinfo, table->col_count );
1147 offset = table->colinfo[table->col_count - 1].offset;
1149 for ( n = 0; n < table->row_count; n++ )
1151 table->data[n] = msi_realloc( table->data[n], size );
1152 if (old_count < table->col_count)
1153 memset( &table->data[n][offset], 0, size - offset );
1157 msi_free(tablename);
1160 /* try to find the table name in the _Tables table */
1161 BOOL TABLE_Exists( MSIDATABASE *db, LPCWSTR name )
1163 UINT r, table_id = 0, i, count;
1164 MSITABLE *table = NULL;
1166 static const WCHAR szStreams[] = {'_','S','t','r','e','a','m','s',0};
1167 static const WCHAR szStorages[] = {'_','S','t','o','r','a','g','e','s',0};
1169 if( !lstrcmpW( name, szTables ) || !lstrcmpW( name, szColumns ) ||
1170 !lstrcmpW( name, szStreams ) || !lstrcmpW( name, szStorages ) )
1173 r = msi_string2idW( db->strings, name, &table_id );
1174 if( r != ERROR_SUCCESS )
1176 TRACE("Couldn't find id for %s\n", debugstr_w(name));
1180 r = get_table( db, szTables, &table );
1181 if( r != ERROR_SUCCESS )
1183 ERR("table %s not available\n", debugstr_w(szTables));
1187 count = table->row_count;
1188 for( i=0; i<count; i++ )
1189 if( table->data[ i ][ 0 ] == table_id )
1195 /* below is the query interface to a table */
1197 typedef struct tagMSITABLEVIEW
1202 MSICOLUMNINFO *columns;
1203 MSIORDERINFO *order;
1209 static UINT TABLE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
1211 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1215 return ERROR_INVALID_PARAMETER;
1217 if( (col==0) || (col>tv->num_cols) )
1218 return ERROR_INVALID_PARAMETER;
1220 /* how many rows are there ? */
1221 if( row >= tv->table->row_count )
1222 return ERROR_NO_MORE_ITEMS;
1224 if( tv->columns[col-1].offset >= tv->row_size )
1226 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1227 ERR("%p %p\n", tv, tv->columns );
1228 return ERROR_FUNCTION_FAILED;
1232 row = tv->order->reorder[row];
1234 n = bytes_per_column( tv->db, &tv->columns[col-1] );
1235 if (n != 2 && n != 3 && n != 4)
1237 ERR("oops! what is %d bytes per column?\n", n );
1238 return ERROR_FUNCTION_FAILED;
1241 offset = tv->columns[col-1].offset;
1242 *val = read_table_int(tv->table->data, row, offset, n);
1244 /* TRACE("Data [%d][%d] = %d\n", row, col, *val ); */
1246 return ERROR_SUCCESS;
1249 static UINT msi_stream_name( const MSITABLEVIEW *tv, UINT row, LPWSTR *pstname )
1251 LPWSTR p, stname = NULL;
1252 UINT i, r, type, ival;
1255 MSIVIEW *view = (MSIVIEW *) tv;
1257 TRACE("%p %d\n", tv, row);
1259 len = lstrlenW( tv->name ) + 1;
1260 stname = msi_alloc( len*sizeof(WCHAR) );
1263 r = ERROR_OUTOFMEMORY;
1267 lstrcpyW( stname, tv->name );
1269 for ( i = 0; i < tv->num_cols; i++ )
1271 type = tv->columns[i].type;
1272 if ( type & MSITYPE_KEY )
1274 r = TABLE_fetch_int( view, row, i+1, &ival );
1275 if ( r != ERROR_SUCCESS )
1278 if ( tv->columns[i].type & MSITYPE_STRING )
1280 sval = msi_string_lookup_id( tv->db->strings, ival );
1283 r = ERROR_INVALID_PARAMETER;
1289 static const WCHAR fmt[] = { '%','d',0 };
1291 UINT n = bytes_per_column( tv->db, &tv->columns[i] );
1296 sprintfW( number, fmt, ival-0x8000 );
1299 sprintfW( number, fmt, ival^0x80000000 );
1302 ERR( "oops - unknown column width %d\n", n );
1303 r = ERROR_FUNCTION_FAILED;
1309 len += lstrlenW( szDot ) + lstrlenW( sval );
1310 p = msi_realloc ( stname, len*sizeof(WCHAR) );
1313 r = ERROR_OUTOFMEMORY;
1318 lstrcatW( stname, szDot );
1319 lstrcatW( stname, sval );
1326 return ERROR_SUCCESS;
1335 * We need a special case for streams, as we need to reference column with
1336 * the name of the stream in the same table, and the table name
1337 * which may not be available at higher levels of the query
1339 static UINT TABLE_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm )
1341 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1343 LPWSTR full_name = NULL;
1345 if( !view->ops->fetch_int )
1346 return ERROR_INVALID_PARAMETER;
1348 r = msi_stream_name( tv, row, &full_name );
1349 if ( r != ERROR_SUCCESS )
1351 ERR("fetching stream, error = %d\n", r);
1355 r = db_get_raw_stream( tv->db, full_name, stm );
1357 ERR("fetching stream %s, error = %d\n",debugstr_w(full_name), r);
1358 msi_free( full_name );
1363 static UINT TABLE_set_int( MSITABLEVIEW *tv, UINT row, UINT col, UINT val )
1368 return ERROR_INVALID_PARAMETER;
1370 if( (col==0) || (col>tv->num_cols) )
1371 return ERROR_INVALID_PARAMETER;
1373 if( row >= tv->table->row_count )
1374 return ERROR_INVALID_PARAMETER;
1376 if( tv->columns[col-1].offset >= tv->row_size )
1378 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1379 ERR("%p %p\n", tv, tv->columns );
1380 return ERROR_FUNCTION_FAILED;
1383 msi_free( tv->columns[col-1].hash_table );
1384 tv->columns[col-1].hash_table = NULL;
1386 n = bytes_per_column( tv->db, &tv->columns[col-1] );
1387 if ( n != 2 && n != 3 && n != 4 )
1389 ERR("oops! what is %d bytes per column?\n", n );
1390 return ERROR_FUNCTION_FAILED;
1393 offset = tv->columns[col-1].offset;
1394 for ( i = 0; i < n; i++ )
1395 tv->table->data[row][offset + i] = (val >> i * 8) & 0xff;
1397 return ERROR_SUCCESS;
1400 static UINT TABLE_get_row( struct tagMSIVIEW *view, UINT row, MSIRECORD **rec )
1402 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1405 return ERROR_INVALID_PARAMETER;
1408 row = tv->order->reorder[row];
1410 return msi_view_get_row(tv->db, view, row, rec);
1413 static UINT msi_addstreamW( MSIDATABASE *db, LPCWSTR name, IStream *data )
1416 MSIQUERY *query = NULL;
1417 MSIRECORD *rec = NULL;
1419 static const WCHAR insert[] = {
1420 'I','N','S','E','R','T',' ','I','N','T','O',' ',
1421 '`','_','S','t','r','e','a','m','s','`',' ',
1422 '(','`','N','a','m','e','`',',',
1423 '`','D','a','t','a','`',')',' ',
1424 'V','A','L','U','E','S',' ','(','?',',','?',')',0};
1426 TRACE("%p %s %p\n", db, debugstr_w(name), data);
1428 rec = MSI_CreateRecord( 2 );
1430 return ERROR_OUTOFMEMORY;
1432 r = MSI_RecordSetStringW( rec, 1, name );
1433 if ( r != ERROR_SUCCESS )
1436 r = MSI_RecordSetIStream( rec, 2, data );
1437 if ( r != ERROR_SUCCESS )
1440 r = MSI_DatabaseOpenViewW( db, insert, &query );
1441 if ( r != ERROR_SUCCESS )
1444 r = MSI_ViewExecute( query, rec );
1447 msiobj_release( &query->hdr );
1448 msiobj_release( &rec->hdr );
1453 static UINT get_table_value_from_record( MSITABLEVIEW *tv, MSIRECORD *rec, UINT iField, UINT *pvalue )
1455 MSICOLUMNINFO columninfo;
1458 if ( (iField <= 0) ||
1459 (iField > tv->num_cols) ||
1460 MSI_RecordIsNull( rec, iField ) )
1461 return ERROR_FUNCTION_FAILED;
1463 columninfo = tv->columns[ iField - 1 ];
1465 if ( MSITYPE_IS_BINARY(columninfo.type) )
1467 *pvalue = 1; /* refers to the first key column */
1469 else if ( columninfo.type & MSITYPE_STRING )
1471 LPCWSTR sval = MSI_RecordGetString( rec, iField );
1473 r = msi_string2idW(tv->db->strings, sval, pvalue);
1474 if (r != ERROR_SUCCESS)
1475 return ERROR_NOT_FOUND;
1477 else if ( 2 == bytes_per_column( tv->db, &columninfo ) )
1479 *pvalue = 0x8000 + MSI_RecordGetInteger( rec, iField );
1480 if ( *pvalue & 0xffff0000 )
1482 ERR("field %u value %d out of range\n", iField, *pvalue - 0x8000);
1483 return ERROR_FUNCTION_FAILED;
1488 INT ival = MSI_RecordGetInteger( rec, iField );
1489 *pvalue = ival ^ 0x80000000;
1492 return ERROR_SUCCESS;
1495 static UINT TABLE_set_row( struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask )
1497 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1498 UINT i, val, r = ERROR_SUCCESS;
1501 return ERROR_INVALID_PARAMETER;
1503 /* test if any of the mask bits are invalid */
1504 if ( mask >= (1<<tv->num_cols) )
1505 return ERROR_INVALID_PARAMETER;
1507 for ( i = 0; i < tv->num_cols; i++ )
1511 /* only update the fields specified in the mask */
1512 if ( !(mask&(1<<i)) )
1515 persistent = (tv->table->persistent != MSICONDITION_FALSE) &&
1516 (tv->table->data_persistent[row]);
1517 /* FIXME: should we allow updating keys? */
1520 if ( !MSI_RecordIsNull( rec, i + 1 ) )
1522 r = get_table_value_from_record (tv, rec, i + 1, &val);
1523 if ( MSITYPE_IS_BINARY(tv->columns[ i ].type) )
1528 if ( r != ERROR_SUCCESS )
1529 return ERROR_FUNCTION_FAILED;
1531 r = MSI_RecordGetIStream( rec, i + 1, &stm );
1532 if ( r != ERROR_SUCCESS )
1535 r = msi_stream_name( tv, row, &stname );
1536 if ( r != ERROR_SUCCESS )
1538 IStream_Release( stm );
1542 r = msi_addstreamW( tv->db, stname, stm );
1543 IStream_Release( stm );
1544 msi_free ( stname );
1546 if ( r != ERROR_SUCCESS )
1549 else if ( tv->columns[i].type & MSITYPE_STRING )
1553 if ( r != ERROR_SUCCESS )
1555 LPCWSTR sval = MSI_RecordGetString( rec, i + 1 );
1556 val = msi_addstringW( tv->db->strings, 0, sval, -1, 1,
1557 persistent ? StringPersistent : StringNonPersistent );
1562 TABLE_fetch_int(&tv->view, row, i + 1, &x);
1569 if ( r != ERROR_SUCCESS )
1570 return ERROR_FUNCTION_FAILED;
1574 r = TABLE_set_int( tv, row, i+1, val );
1575 if ( r != ERROR_SUCCESS )
1581 static UINT table_create_new_row( struct tagMSIVIEW *view, UINT *num, BOOL temporary )
1583 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1588 BOOL **data_persist_ptr;
1591 TRACE("%p %s\n", view, temporary ? "TRUE" : "FALSE");
1594 return ERROR_INVALID_PARAMETER;
1596 row = msi_alloc_zero( tv->row_size );
1598 return ERROR_NOT_ENOUGH_MEMORY;
1600 row_count = &tv->table->row_count;
1601 data_ptr = &tv->table->data;
1602 data_persist_ptr = &tv->table->data_persistent;
1604 *num = tv->table->row_count;
1606 sz = (*row_count + 1) * sizeof (BYTE*);
1608 p = msi_realloc( *data_ptr, sz );
1610 p = msi_alloc( sz );
1614 return ERROR_NOT_ENOUGH_MEMORY;
1617 sz = (*row_count + 1) * sizeof (BOOL);
1618 if( *data_persist_ptr )
1619 b = msi_realloc( *data_persist_ptr, sz );
1621 b = msi_alloc( sz );
1626 return ERROR_NOT_ENOUGH_MEMORY;
1630 (*data_ptr)[*row_count] = row;
1632 *data_persist_ptr = b;
1633 (*data_persist_ptr)[*row_count] = !temporary;
1637 return ERROR_SUCCESS;
1640 static UINT TABLE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
1642 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1644 TRACE("%p %p\n", tv, record);
1646 TRACE("There are %d columns\n", tv->num_cols );
1648 return ERROR_SUCCESS;
1651 static UINT TABLE_close( struct tagMSIVIEW *view )
1653 TRACE("%p\n", view );
1655 return ERROR_SUCCESS;
1658 static UINT TABLE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols)
1660 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1662 TRACE("%p %p %p\n", view, rows, cols );
1665 *cols = tv->num_cols;
1669 return ERROR_INVALID_PARAMETER;
1670 *rows = tv->table->row_count;
1673 return ERROR_SUCCESS;
1676 static UINT TABLE_get_column_info( struct tagMSIVIEW *view,
1677 UINT n, LPWSTR *name, UINT *type, BOOL *temporary,
1678 LPWSTR *table_name )
1680 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1682 TRACE("%p %d %p %p\n", tv, n, name, type );
1684 if( ( n == 0 ) || ( n > tv->num_cols ) )
1685 return ERROR_INVALID_PARAMETER;
1689 *name = strdupW( tv->columns[n-1].colname );
1691 return ERROR_FUNCTION_FAILED;
1696 *table_name = strdupW( tv->columns[n-1].tablename );
1698 return ERROR_FUNCTION_FAILED;
1702 *type = tv->columns[n-1].type;
1705 *temporary = tv->columns[n-1].temporary;
1707 return ERROR_SUCCESS;
1710 static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row );
1712 static UINT table_validate_new( MSITABLEVIEW *tv, MSIRECORD *rec )
1716 /* check there's no null values where they're not allowed */
1717 for( i = 0; i < tv->num_cols; i++ )
1719 if ( tv->columns[i].type & MSITYPE_NULLABLE )
1722 if ( MSITYPE_IS_BINARY(tv->columns[i].type) )
1723 TRACE("skipping binary column\n");
1724 else if ( tv->columns[i].type & MSITYPE_STRING )
1728 str = MSI_RecordGetString( rec, i+1 );
1729 if (str == NULL || str[0] == 0)
1730 return ERROR_INVALID_DATA;
1736 n = MSI_RecordGetInteger( rec, i+1 );
1737 if (n == MSI_NULL_INTEGER)
1738 return ERROR_INVALID_DATA;
1742 /* check there's no duplicate keys */
1743 r = msi_table_find_row( tv, rec, &row );
1744 if (r == ERROR_SUCCESS)
1745 return ERROR_FUNCTION_FAILED;
1747 return ERROR_SUCCESS;
1750 static UINT find_insert_index( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *pidx )
1752 UINT r, idx, j, ivalue, x;
1754 TRACE("%p %p %p\n", tv, rec, pidx);
1756 for (idx = 0; idx < tv->table->row_count; idx++)
1758 for (j = 0; j < tv->num_cols; j++ )
1760 r = get_table_value_from_record (tv, rec, j+1, &ivalue);
1761 if (r != ERROR_SUCCESS)
1764 r = TABLE_fetch_int(&tv->view, idx, j + 1, &x);
1765 if (r != ERROR_SUCCESS)
1770 else if (ivalue == x)
1773 TRACE("Found %d.\n", idx);
1775 return ERROR_SUCCESS;
1780 TRACE("Found %d.\n", idx);
1782 return ERROR_SUCCESS;
1785 static UINT TABLE_insert_row( struct tagMSIVIEW *view, MSIRECORD *rec, UINT row, BOOL temporary )
1787 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1790 TRACE("%p %p %s\n", tv, rec, temporary ? "TRUE" : "FALSE" );
1792 /* check that the key is unique - can we find a matching row? */
1793 r = table_validate_new( tv, rec );
1794 if( r != ERROR_SUCCESS )
1795 return ERROR_FUNCTION_FAILED;
1799 r = find_insert_index(tv, rec, &row);
1800 if( r != ERROR_SUCCESS )
1801 return ERROR_FUNCTION_FAILED;
1804 r = table_create_new_row( view, &row, temporary );
1805 TRACE("insert_row returned %08x\n", r);
1806 if( r != ERROR_SUCCESS )
1809 /* shift the rows to make room for the new row */
1810 for (i = tv->table->row_count - 1; i > row; i--)
1812 memmove(&(tv->table->data[i][0]),
1813 &(tv->table->data[i - 1][0]), tv->row_size);
1814 tv->table->data_persistent[i] = tv->table->data_persistent[i - 1];
1817 /* Re-set the persistence flag */
1818 tv->table->data_persistent[row] = !temporary;
1819 return TABLE_set_row( view, row, rec, (1<<tv->num_cols) - 1 );
1822 static UINT TABLE_delete_row( struct tagMSIVIEW *view, UINT row )
1824 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1825 UINT r, num_rows, num_cols, i;
1827 TRACE("%p %d\n", tv, row);
1830 return ERROR_INVALID_PARAMETER;
1832 r = TABLE_get_dimensions( view, &num_rows, &num_cols );
1833 if ( r != ERROR_SUCCESS )
1836 if ( row >= num_rows )
1837 return ERROR_FUNCTION_FAILED;
1839 num_rows = tv->table->row_count;
1840 tv->table->row_count--;
1842 /* reset the hash tables */
1843 for (i = 0; i < tv->num_cols; i++)
1845 msi_free( tv->columns[i].hash_table );
1846 tv->columns[i].hash_table = NULL;
1849 for (i = row + 1; i < num_rows; i++)
1851 memcpy(tv->table->data[i - 1], tv->table->data[i], tv->row_size);
1852 tv->table->data_persistent[i - 1] = tv->table->data_persistent[i];
1855 msi_free(tv->table->data[num_rows - 1]);
1857 return ERROR_SUCCESS;
1860 static UINT msi_table_update(struct tagMSIVIEW *view, MSIRECORD *rec, UINT row)
1862 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1865 /* FIXME: MsiViewFetch should set rec index 0 to some ID that
1866 * sets the fetched record apart from other records
1870 return ERROR_INVALID_PARAMETER;
1872 r = msi_table_find_row(tv, rec, &new_row);
1873 if (r != ERROR_SUCCESS)
1875 ERR("can't find row to modify\n");
1876 return ERROR_FUNCTION_FAILED;
1879 /* the row cannot be changed */
1880 if (row != new_row + 1)
1881 return ERROR_FUNCTION_FAILED;
1883 return TABLE_set_row(view, new_row, rec, (1 << tv->num_cols) - 1);
1886 static UINT msi_table_assign(struct tagMSIVIEW *view, MSIRECORD *rec)
1888 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1892 return ERROR_INVALID_PARAMETER;
1894 r = msi_table_find_row(tv, rec, &row);
1895 if (r == ERROR_SUCCESS)
1896 return TABLE_set_row(view, row, rec, (1 << tv->num_cols) - 1);
1898 return TABLE_insert_row( view, rec, -1, FALSE );
1901 static UINT modify_delete_row( struct tagMSIVIEW *view, MSIRECORD *rec )
1903 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1906 r = msi_table_find_row(tv, rec, &row);
1907 if (r != ERROR_SUCCESS)
1910 return TABLE_delete_row(view, row);
1913 static UINT msi_refresh_record( struct tagMSIVIEW *view, MSIRECORD *rec, UINT row )
1918 r = TABLE_get_row(view, row - 1, &curr);
1919 if (r != ERROR_SUCCESS)
1922 /* Close the original record */
1923 MSI_CloseRecord(&rec->hdr);
1925 count = MSI_RecordGetFieldCount(rec);
1926 for (i = 0; i < count; i++)
1927 MSI_RecordCopyField(curr, i + 1, rec, i + 1);
1929 msiobj_release(&curr->hdr);
1930 return ERROR_SUCCESS;
1933 static UINT TABLE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
1934 MSIRECORD *rec, UINT row)
1936 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1939 TRACE("%p %d %p\n", view, eModifyMode, rec );
1941 switch (eModifyMode)
1943 case MSIMODIFY_DELETE:
1944 r = modify_delete_row( view, rec );
1946 case MSIMODIFY_VALIDATE_NEW:
1947 r = table_validate_new( tv, rec );
1950 case MSIMODIFY_INSERT:
1951 r = table_validate_new( tv, rec );
1952 if (r != ERROR_SUCCESS)
1954 r = TABLE_insert_row( view, rec, -1, FALSE );
1957 case MSIMODIFY_INSERT_TEMPORARY:
1958 r = table_validate_new( tv, rec );
1959 if (r != ERROR_SUCCESS)
1961 r = TABLE_insert_row( view, rec, -1, TRUE );
1964 case MSIMODIFY_REFRESH:
1965 r = msi_refresh_record( view, rec, row );
1968 case MSIMODIFY_UPDATE:
1969 r = msi_table_update( view, rec, row );
1972 case MSIMODIFY_ASSIGN:
1973 r = msi_table_assign( view, rec );
1976 case MSIMODIFY_REPLACE:
1977 case MSIMODIFY_MERGE:
1978 case MSIMODIFY_VALIDATE:
1979 case MSIMODIFY_VALIDATE_FIELD:
1980 case MSIMODIFY_VALIDATE_DELETE:
1981 FIXME("%p %d %p - mode not implemented\n", view, eModifyMode, rec );
1982 r = ERROR_CALL_NOT_IMPLEMENTED;
1986 r = ERROR_INVALID_DATA;
1992 static UINT TABLE_delete( struct tagMSIVIEW *view )
1994 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1996 TRACE("%p\n", view );
2003 msi_free( tv->order->reorder );
2004 msi_free( tv->order );
2010 return ERROR_SUCCESS;
2013 static UINT TABLE_find_matching_rows( struct tagMSIVIEW *view, UINT col,
2014 UINT val, UINT *row, MSIITERHANDLE *handle )
2016 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
2017 const MSICOLUMNHASHENTRY *entry;
2019 TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
2022 return ERROR_INVALID_PARAMETER;
2024 if( (col==0) || (col > tv->num_cols) )
2025 return ERROR_INVALID_PARAMETER;
2027 if( !tv->columns[col-1].hash_table )
2030 UINT num_rows = tv->table->row_count;
2031 MSICOLUMNHASHENTRY **hash_table;
2032 MSICOLUMNHASHENTRY *new_entry;
2034 if( tv->columns[col-1].offset >= tv->row_size )
2036 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
2037 ERR("%p %p\n", tv, tv->columns );
2038 return ERROR_FUNCTION_FAILED;
2041 /* allocate contiguous memory for the table and its entries so we
2042 * don't have to do an expensive cleanup */
2043 hash_table = msi_alloc(MSITABLE_HASH_TABLE_SIZE * sizeof(MSICOLUMNHASHENTRY*) +
2044 num_rows * sizeof(MSICOLUMNHASHENTRY));
2046 return ERROR_OUTOFMEMORY;
2048 memset(hash_table, 0, MSITABLE_HASH_TABLE_SIZE * sizeof(MSICOLUMNHASHENTRY*));
2049 tv->columns[col-1].hash_table = hash_table;
2051 new_entry = (MSICOLUMNHASHENTRY *)(hash_table + MSITABLE_HASH_TABLE_SIZE);
2053 for (i = 0; i < num_rows; i++, new_entry++)
2057 if (view->ops->fetch_int( view, i, col, &row_value ) != ERROR_SUCCESS)
2060 new_entry->next = NULL;
2061 new_entry->value = row_value;
2063 if (hash_table[row_value % MSITABLE_HASH_TABLE_SIZE])
2065 MSICOLUMNHASHENTRY *prev_entry = hash_table[row_value % MSITABLE_HASH_TABLE_SIZE];
2066 while (prev_entry->next)
2067 prev_entry = prev_entry->next;
2068 prev_entry->next = new_entry;
2071 hash_table[row_value % MSITABLE_HASH_TABLE_SIZE] = new_entry;
2076 entry = tv->columns[col-1].hash_table[val % MSITABLE_HASH_TABLE_SIZE];
2078 entry = (*handle)->next;
2080 while (entry && entry->value != val)
2081 entry = entry->next;
2085 return ERROR_NO_MORE_ITEMS;
2089 return ERROR_SUCCESS;
2092 static UINT TABLE_add_ref(struct tagMSIVIEW *view)
2094 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
2097 TRACE("%p %d\n", view, tv->table->ref_count);
2099 for (i = 0; i < tv->table->col_count; i++)
2101 if (tv->table->colinfo[i].type & MSITYPE_TEMPORARY)
2102 InterlockedIncrement(&tv->table->colinfo[i].ref_count);
2105 return InterlockedIncrement(&tv->table->ref_count);
2108 static UINT TABLE_remove_column(struct tagMSIVIEW *view, LPCWSTR table, UINT number)
2110 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
2111 MSIRECORD *rec = NULL;
2112 MSIVIEW *columns = NULL;
2115 rec = MSI_CreateRecord(2);
2117 return ERROR_OUTOFMEMORY;
2119 MSI_RecordSetStringW(rec, 1, table);
2120 MSI_RecordSetInteger(rec, 2, number);
2122 r = TABLE_CreateView(tv->db, szColumns, &columns);
2123 if (r != ERROR_SUCCESS)
2126 r = msi_table_find_row((MSITABLEVIEW *)columns, rec, &row);
2127 if (r != ERROR_SUCCESS)
2130 r = TABLE_delete_row(columns, row);
2131 if (r != ERROR_SUCCESS)
2134 msi_update_table_columns(tv->db, table);
2137 msiobj_release(&rec->hdr);
2138 columns->ops->delete(columns);
2142 static UINT TABLE_release(struct tagMSIVIEW *view)
2144 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
2145 INT ref = tv->table->ref_count;
2148 TRACE("%p %d\n", view, ref);
2150 for (i = 0; i < tv->table->col_count; i++)
2152 if (tv->table->colinfo[i].type & MSITYPE_TEMPORARY)
2154 ref = InterlockedDecrement(&tv->table->colinfo[i].ref_count);
2157 r = TABLE_remove_column(view, tv->table->colinfo[i].tablename,
2158 tv->table->colinfo[i].number);
2159 if (r != ERROR_SUCCESS)
2165 ref = InterlockedDecrement(&tv->table->ref_count);
2168 if (!tv->table->row_count)
2170 list_remove(&tv->table->entry);
2171 free_table(tv->table);
2179 static UINT TABLE_add_column(struct tagMSIVIEW *view, LPCWSTR table, UINT number,
2180 LPCWSTR column, UINT type, BOOL hold)
2182 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
2187 rec = MSI_CreateRecord(4);
2189 return ERROR_OUTOFMEMORY;
2191 MSI_RecordSetStringW(rec, 1, table);
2192 MSI_RecordSetInteger(rec, 2, number);
2193 MSI_RecordSetStringW(rec, 3, column);
2194 MSI_RecordSetInteger(rec, 4, type);
2196 r = TABLE_insert_row(&tv->view, rec, -1, FALSE);
2197 if (r != ERROR_SUCCESS)
2200 msi_update_table_columns(tv->db, table);
2205 msitable = find_cached_table(tv->db, table);
2206 for (i = 0; i < msitable->col_count; i++)
2208 if (!lstrcmpW(msitable->colinfo[i].colname, column))
2210 InterlockedIncrement(&msitable->colinfo[i].ref_count);
2216 msiobj_release(&rec->hdr);
2220 static UINT order_add_column(struct tagMSIVIEW *view, MSIORDERINFO *order, LPCWSTR name)
2223 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
2225 r = TABLE_get_dimensions(view, NULL, &count);
2226 if (r != ERROR_SUCCESS)
2229 if (order->num_cols >= count)
2230 return ERROR_FUNCTION_FAILED;
2232 r = VIEW_find_column(view, name, tv->name, &n);
2233 if (r != ERROR_SUCCESS)
2236 order->cols[order->num_cols] = n;
2237 TRACE("Ordering by column %s (%d)\n", debugstr_w(name), n);
2241 return ERROR_SUCCESS;
2244 static UINT order_compare(struct tagMSIVIEW *view, MSIORDERINFO *order,
2245 UINT a, UINT b, UINT *swap)
2247 UINT r, i, a_val = 0, b_val = 0;
2250 for (i = 0; i < order->num_cols; i++)
2252 r = TABLE_fetch_int(view, a, order->cols[i], &a_val);
2253 if (r != ERROR_SUCCESS)
2256 r = TABLE_fetch_int(view, b, order->cols[i], &b_val);
2257 if (r != ERROR_SUCCESS)
2268 return ERROR_SUCCESS;
2271 static UINT order_mergesort(struct tagMSIVIEW *view, MSIORDERINFO *order,
2272 UINT left, UINT right)
2275 UINT swap = 0, center = (left + right) / 2;
2276 UINT *array = order->reorder;
2279 return ERROR_SUCCESS;
2281 /* sort the left half */
2282 r = order_mergesort(view, order, left, center);
2283 if (r != ERROR_SUCCESS)
2286 /* sort the right half */
2287 r = order_mergesort(view, order, center + 1, right);
2288 if (r != ERROR_SUCCESS)
2291 for (i = left, j = center + 1; (i <= center) && (j <= right); i++)
2293 r = order_compare(view, order, array[i], array[j], &swap);
2294 if (r != ERROR_SUCCESS)
2300 memmove(&array[i + 1], &array[i], (j - i) * sizeof(UINT));
2307 return ERROR_SUCCESS;
2310 static UINT order_verify(struct tagMSIVIEW *view, MSIORDERINFO *order, UINT num_rows)
2314 for (i = 1; i < num_rows; i++)
2316 r = order_compare(view, order, order->reorder[i - 1],
2317 order->reorder[i], &swap);
2318 if (r != ERROR_SUCCESS)
2324 ERR("Bad order! %d\n", i);
2325 return ERROR_FUNCTION_FAILED;
2328 return ERROR_SUCCESS;
2331 static UINT TABLE_sort(struct tagMSIVIEW *view, column_info *columns)
2333 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
2334 MSIORDERINFO *order;
2339 TRACE("sorting table %s\n", debugstr_w(tv->name));
2341 r = TABLE_get_dimensions(view, &rows, &cols);
2342 if (r != ERROR_SUCCESS)
2346 return ERROR_SUCCESS;
2348 order = msi_alloc_zero(sizeof(MSIORDERINFO) + sizeof(UINT) * cols);
2350 return ERROR_OUTOFMEMORY;
2352 for (ptr = columns; ptr; ptr = ptr->next)
2353 order_add_column(view, order, ptr->column);
2355 order->reorder = msi_alloc(rows * sizeof(UINT));
2356 if (!order->reorder)
2357 return ERROR_OUTOFMEMORY;
2359 for (i = 0; i < rows; i++)
2360 order->reorder[i] = i;
2362 r = order_mergesort(view, order, 0, rows - 1);
2363 if (r != ERROR_SUCCESS)
2366 r = order_verify(view, order, rows);
2367 if (r != ERROR_SUCCESS)
2372 return ERROR_SUCCESS;
2375 static UINT TABLE_drop(struct tagMSIVIEW *view)
2377 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
2378 MSIVIEW *tables = NULL;
2379 MSIRECORD *rec = NULL;
2383 TRACE("dropping table %s\n", debugstr_w(tv->name));
2385 for (i = tv->table->col_count - 1; i >= 0; i--)
2387 r = TABLE_remove_column(view, tv->table->colinfo[i].tablename,
2388 tv->table->colinfo[i].number);
2389 if (r != ERROR_SUCCESS)
2393 rec = MSI_CreateRecord(1);
2395 return ERROR_OUTOFMEMORY;
2397 MSI_RecordSetStringW(rec, 1, tv->name);
2399 r = TABLE_CreateView(tv->db, szTables, &tables);
2400 if (r != ERROR_SUCCESS)
2403 r = msi_table_find_row((MSITABLEVIEW *)tables, rec, &row);
2404 if (r != ERROR_SUCCESS)
2407 r = TABLE_delete_row(tables, row);
2408 if (r != ERROR_SUCCESS)
2411 list_remove(&tv->table->entry);
2412 free_table(tv->table);
2415 msiobj_release(&rec->hdr);
2416 tables->ops->delete(tables);
2421 static const MSIVIEWOPS table_ops =
2431 TABLE_get_dimensions,
2432 TABLE_get_column_info,
2435 TABLE_find_matching_rows,
2439 TABLE_remove_column,
2444 UINT TABLE_CreateView( MSIDATABASE *db, LPCWSTR name, MSIVIEW **view )
2449 static const WCHAR Streams[] = {'_','S','t','r','e','a','m','s',0};
2450 static const WCHAR Storages[] = {'_','S','t','o','r','a','g','e','s',0};
2452 TRACE("%p %s %p\n", db, debugstr_w(name), view );
2454 if ( !lstrcmpW( name, Streams ) )
2455 return STREAMS_CreateView( db, view );
2456 else if ( !lstrcmpW( name, Storages ) )
2457 return STORAGES_CreateView( db, view );
2459 sz = sizeof *tv + lstrlenW(name)*sizeof name[0] ;
2460 tv = msi_alloc_zero( sz );
2462 return ERROR_FUNCTION_FAILED;
2464 r = get_table( db, name, &tv->table );
2465 if( r != ERROR_SUCCESS )
2468 WARN("table not found\n");
2472 TRACE("table %p found with %d columns\n", tv->table, tv->table->col_count);
2474 /* fill the structure */
2475 tv->view.ops = &table_ops;
2477 tv->columns = tv->table->colinfo;
2478 tv->num_cols = tv->table->col_count;
2479 tv->row_size = msi_table_get_row_size( db, tv->table->colinfo, tv->table->col_count );
2481 TRACE("%s one row is %d bytes\n", debugstr_w(name), tv->row_size );
2483 *view = (MSIVIEW*) tv;
2484 lstrcpyW( tv->name, name );
2486 return ERROR_SUCCESS;
2489 UINT MSI_CommitTables( MSIDATABASE *db )
2492 MSITABLE *table = NULL;
2496 r = msi_save_string_table( db->strings, db->storage );
2497 if( r != ERROR_SUCCESS )
2499 WARN("failed to save string table r=%08x\n",r);
2503 LIST_FOR_EACH_ENTRY( table, &db->tables, MSITABLE, entry )
2505 r = save_table( db, table );
2506 if( r != ERROR_SUCCESS )
2508 WARN("failed to save table %s (r=%08x)\n",
2509 debugstr_w(table->name), r);
2514 /* force everything to reload next time */
2515 free_cached_tables( db );
2517 return ERROR_SUCCESS;
2520 MSICONDITION MSI_DatabaseIsTablePersistent( MSIDATABASE *db, LPCWSTR table )
2525 TRACE("%p %s\n", db, debugstr_w(table));
2528 return MSICONDITION_ERROR;
2530 r = get_table( db, table, &t );
2531 if (r != ERROR_SUCCESS)
2532 return MSICONDITION_NONE;
2534 return t->persistent;
2537 static UINT read_raw_int(const BYTE *data, UINT col, UINT bytes)
2541 for (i = 0; i < bytes; i++)
2542 ret += (data[col + i] << i * 8);
2547 static UINT msi_record_encoded_stream_name( const MSITABLEVIEW *tv, MSIRECORD *rec, LPWSTR *pstname )
2549 LPWSTR stname = NULL, sval, p;
2553 TRACE("%p %p\n", tv, rec);
2555 len = lstrlenW( tv->name ) + 1;
2556 stname = msi_alloc( len*sizeof(WCHAR) );
2559 r = ERROR_OUTOFMEMORY;
2563 lstrcpyW( stname, tv->name );
2565 for ( i = 0; i < tv->num_cols; i++ )
2567 if ( tv->columns[i].type & MSITYPE_KEY )
2569 sval = msi_dup_record_field( rec, i + 1 );
2572 r = ERROR_OUTOFMEMORY;
2576 len += lstrlenW( szDot ) + lstrlenW ( sval );
2577 p = msi_realloc ( stname, len*sizeof(WCHAR) );
2580 r = ERROR_OUTOFMEMORY;
2585 lstrcatW( stname, szDot );
2586 lstrcatW( stname, sval );
2594 *pstname = encode_streamname( FALSE, stname );
2597 return ERROR_SUCCESS;
2600 msi_free ( stname );
2605 static MSIRECORD *msi_get_transform_record( const MSITABLEVIEW *tv, const string_table *st,
2607 const BYTE *rawdata, UINT bytes_per_strref )
2609 UINT i, val, ofs = 0;
2611 MSICOLUMNINFO *columns = tv->columns;
2614 mask = rawdata[0] | (rawdata[1] << 8);
2617 rec = MSI_CreateRecord( tv->num_cols );
2622 for( i=0; i<tv->num_cols; i++ )
2624 if ( (mask&1) && (i>=(mask>>8)) )
2626 /* all keys must be present */
2627 if ( (~mask&1) && (~columns[i].type & MSITYPE_KEY) && ((1<<i) & ~mask) )
2630 if( MSITYPE_IS_BINARY(tv->columns[i].type) )
2633 IStream *stm = NULL;
2636 ofs += bytes_per_column( tv->db, &columns[i] );
2638 r = msi_record_encoded_stream_name( tv, rec, &encname );
2639 if ( r != ERROR_SUCCESS )
2642 r = IStorage_OpenStream( stg, encname, NULL,
2643 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stm );
2644 msi_free( encname );
2645 if ( r != ERROR_SUCCESS )
2648 MSI_RecordSetStream( rec, i+1, stm );
2649 TRACE(" field %d [%s]\n", i+1, debugstr_w(encname));
2651 else if( columns[i].type & MSITYPE_STRING )
2655 val = read_raw_int(rawdata, ofs, bytes_per_strref);
2656 sval = msi_string_lookup_id( st, val );
2657 MSI_RecordSetStringW( rec, i+1, sval );
2658 TRACE(" field %d [%s]\n", i+1, debugstr_w(sval));
2659 ofs += bytes_per_strref;
2663 UINT n = bytes_per_column( tv->db, &columns[i] );
2667 val = read_raw_int(rawdata, ofs, n);
2669 MSI_RecordSetInteger( rec, i+1, val-0x8000 );
2670 TRACE(" field %d [0x%04x]\n", i+1, val );
2673 val = read_raw_int(rawdata, ofs, n);
2675 MSI_RecordSetInteger( rec, i+1, val^0x80000000 );
2676 TRACE(" field %d [0x%08x]\n", i+1, val );
2679 ERR("oops - unknown column width %d\n", n);
2688 static void dump_record( MSIRECORD *rec )
2692 n = MSI_RecordGetFieldCount( rec );
2693 for( i=1; i<=n; i++ )
2695 LPCWSTR sval = MSI_RecordGetString( rec, i );
2697 if( MSI_RecordIsNull( rec, i ) )
2698 TRACE("row -> []\n");
2699 else if( (sval = MSI_RecordGetString( rec, i )) )
2700 TRACE("row -> [%s]\n", debugstr_w(sval));
2702 TRACE("row -> [0x%08x]\n", MSI_RecordGetInteger( rec, i ) );
2706 static void dump_table( const string_table *st, const USHORT *rawdata, UINT rawsize )
2711 for( i=0; i<(rawsize/2); i++ )
2713 sval = msi_string_lookup_id( st, rawdata[i] );
2714 MESSAGE(" %04x %s\n", rawdata[i], debugstr_w(sval) );
2718 static UINT* msi_record_to_row( const MSITABLEVIEW *tv, MSIRECORD *rec )
2723 data = msi_alloc( tv->num_cols *sizeof (UINT) );
2724 for( i=0; i<tv->num_cols; i++ )
2728 if ( ~tv->columns[i].type & MSITYPE_KEY )
2731 /* turn the transform column value into a row value */
2732 if ( ( tv->columns[i].type & MSITYPE_STRING ) &&
2733 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
2735 str = MSI_RecordGetString( rec, i+1 );
2736 r = msi_string2idW( tv->db->strings, str, &data[i] );
2738 /* if there's no matching string in the string table,
2739 these keys can't match any record, so fail now. */
2740 if( ERROR_SUCCESS != r )
2748 data[i] = MSI_RecordGetInteger( rec, i+1 );
2750 if (data[i] == MSI_NULL_INTEGER)
2752 else if ((tv->columns[i].type&0xff) == 2)
2755 data[i] += 0x80000000;
2761 static UINT msi_row_matches( MSITABLEVIEW *tv, UINT row, const UINT *data )
2763 UINT i, r, x, ret = ERROR_FUNCTION_FAILED;
2765 for( i=0; i<tv->num_cols; i++ )
2767 if ( ~tv->columns[i].type & MSITYPE_KEY )
2770 /* turn the transform column value into a row value */
2771 r = TABLE_fetch_int( &tv->view, row, i+1, &x );
2772 if ( r != ERROR_SUCCESS )
2774 ERR("TABLE_fetch_int shouldn't fail here\n");
2778 /* if this key matches, move to the next column */
2781 ret = ERROR_FUNCTION_FAILED;
2785 ret = ERROR_SUCCESS;
2791 static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row )
2793 UINT i, r = ERROR_FUNCTION_FAILED, *data;
2795 data = msi_record_to_row( tv, rec );
2798 for( i = 0; i < tv->table->row_count; i++ )
2800 r = msi_row_matches( tv, i, data );
2801 if( r == ERROR_SUCCESS )
2817 static UINT msi_table_load_transform( MSIDATABASE *db, IStorage *stg,
2818 string_table *st, TRANSFORMDATA *transform,
2819 UINT bytes_per_strref )
2822 BYTE *rawdata = NULL;
2823 MSITABLEVIEW *tv = NULL;
2824 UINT r, n, sz, i, mask;
2825 MSIRECORD *rec = NULL;
2831 return ERROR_SUCCESS;
2833 name = transform->name;
2836 TRACE("%p %p %p %s\n", db, stg, st, debugstr_w(name) );
2838 /* read the transform data */
2839 read_stream_data( stg, name, TRUE, &rawdata, &rawsize );
2842 TRACE("table %s empty\n", debugstr_w(name) );
2843 return ERROR_INVALID_TABLE;
2846 /* create a table view */
2847 r = TABLE_CreateView( db, name, (MSIVIEW**) &tv );
2848 if( r != ERROR_SUCCESS )
2851 r = tv->view.ops->execute( &tv->view, NULL );
2852 if( r != ERROR_SUCCESS )
2855 TRACE("name = %s columns = %u row_size = %u raw size = %u\n",
2856 debugstr_w(name), tv->num_cols, tv->row_size, rawsize );
2858 /* interpret the data */
2860 for( n=0; n < rawsize; )
2862 mask = rawdata[n] | (rawdata[n+1] << 8);
2867 * if the low bit is set, columns are continuous and
2868 * the number of columns is specified in the high byte
2871 for( i=0; i<tv->num_cols; i++ )
2873 if( (tv->columns[i].type & MSITYPE_STRING) &&
2874 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
2875 sz += bytes_per_strref;
2877 sz += bytes_per_column( tv->db, &tv->columns[i] );
2883 * If the low bit is not set, mask is a bitmask.
2884 * Excepting for key fields, which are always present,
2885 * each bit indicates that a field is present in the transform record.
2887 * mask == 0 is a special case ... only the keys will be present
2888 * and it means that this row should be deleted.
2891 for( i=0; i<tv->num_cols; i++ )
2893 if( (tv->columns[i].type & MSITYPE_KEY) || ((1<<i)&mask))
2895 if( (tv->columns[i].type & MSITYPE_STRING) &&
2896 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
2897 sz += bytes_per_strref;
2899 sz += bytes_per_column( tv->db, &tv->columns[i] );
2904 /* check we didn't run of the end of the table */
2905 if ( (n+sz) > rawsize )
2908 dump_table( st, (USHORT *)rawdata, rawsize );
2912 rec = msi_get_transform_record( tv, st, stg, &rawdata[n], bytes_per_strref );
2919 UINT number = MSI_NULL_INTEGER;
2921 TRACE("inserting record\n");
2923 if (!lstrcmpW(name, szColumns))
2925 MSI_RecordGetStringW( rec, 1, table, &sz );
2926 number = MSI_RecordGetInteger( rec, 2 );
2929 * Native msi seems writes nul into the Number (2nd) column of
2930 * the _Columns table, only when the columns are from a new table
2932 if ( number == MSI_NULL_INTEGER )
2934 /* reset the column number on a new table */
2935 if ( lstrcmpW(coltable, table) )
2938 lstrcpyW( coltable, table );
2941 /* fix nul column numbers */
2942 MSI_RecordSetInteger( rec, 2, ++colcol );
2946 r = TABLE_insert_row( &tv->view, rec, -1, FALSE );
2947 if (r != ERROR_SUCCESS)
2948 WARN("insert row failed\n");
2950 if ( number != MSI_NULL_INTEGER && !lstrcmpW(name, szColumns) )
2951 msi_update_table_columns( db, table );
2957 r = msi_table_find_row( tv, rec, &row );
2958 if (r != ERROR_SUCCESS)
2959 WARN("no matching row to transform\n");
2962 TRACE("modifying row [%d]:\n", row);
2963 TABLE_set_row( &tv->view, row, rec, mask );
2967 TRACE("deleting row [%d]:\n", row);
2968 TABLE_delete_row( &tv->view, row );
2971 if( TRACE_ON(msidb) ) dump_record( rec );
2972 msiobj_release( &rec->hdr );
2979 /* no need to free the table, it's associated with the database */
2980 msi_free( rawdata );
2982 tv->view.ops->delete( &tv->view );
2984 return ERROR_SUCCESS;
2988 * msi_table_apply_transform
2990 * Enumerate the table transforms in a transform storage and apply each one.
2992 UINT msi_table_apply_transform( MSIDATABASE *db, IStorage *stg )
2994 struct list transforms;
2995 IEnumSTATSTG *stgenum = NULL;
2996 TRANSFORMDATA *transform;
2997 TRANSFORMDATA *tables = NULL, *columns = NULL;
3000 string_table *strings;
3001 UINT ret = ERROR_FUNCTION_FAILED;
3002 UINT bytes_per_strref;
3004 TRACE("%p %p\n", db, stg );
3006 strings = msi_load_string_table( stg, &bytes_per_strref );
3010 r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
3014 list_init(&transforms);
3018 MSITABLEVIEW *tv = NULL;
3022 r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
3023 if ( FAILED( r ) || !count )
3026 decode_streamname( stat.pwcsName, name );
3027 CoTaskMemFree( stat.pwcsName );
3028 if ( name[0] != 0x4840 )
3031 if ( !lstrcmpW( name+1, szStringPool ) ||
3032 !lstrcmpW( name+1, szStringData ) )
3035 transform = msi_alloc_zero( sizeof(TRANSFORMDATA) );
3039 list_add_tail( &transforms, &transform->entry );
3041 transform->name = strdupW( name + 1 );
3043 if ( !lstrcmpW( transform->name, szTables ) )
3045 else if (!lstrcmpW( transform->name, szColumns ) )
3046 columns = transform;
3048 TRACE("transform contains stream %s\n", debugstr_w(name));
3050 /* load the table */
3051 r = TABLE_CreateView( db, transform->name, (MSIVIEW**) &tv );
3052 if( r != ERROR_SUCCESS )
3055 r = tv->view.ops->execute( &tv->view, NULL );
3056 if( r != ERROR_SUCCESS )
3058 tv->view.ops->delete( &tv->view );
3062 tv->view.ops->delete( &tv->view );
3066 * Apply _Tables and _Columns transforms first so that
3067 * the table metadata is correct, and empty tables exist.
3069 ret = msi_table_load_transform( db, stg, strings, tables, bytes_per_strref );
3070 if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
3073 ret = msi_table_load_transform( db, stg, strings, columns, bytes_per_strref );
3074 if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
3077 ret = ERROR_SUCCESS;
3079 while ( !list_empty( &transforms ) )
3081 transform = LIST_ENTRY( list_head( &transforms ), TRANSFORMDATA, entry );
3083 if ( lstrcmpW( transform->name, szColumns ) &&
3084 lstrcmpW( transform->name, szTables ) &&
3085 ret == ERROR_SUCCESS )
3087 ret = msi_table_load_transform( db, stg, strings, transform, bytes_per_strref );
3090 list_remove( &transform->entry );
3091 msi_free( transform->name );
3092 msi_free( transform );
3095 if ( ret == ERROR_SUCCESS )
3096 append_storage_to_db( db, stg );
3100 IEnumSTATSTG_Release( stgenum );
3102 msi_destroy_stringtable( strings );
3107 void append_storage_to_db( MSIDATABASE *db, IStorage *stg )
3111 t = msi_alloc( sizeof *t );
3113 IStorage_AddRef( stg );
3114 list_add_tail( &db->transforms, &t->entry );
3117 void msi_free_transforms( MSIDATABASE *db )
3119 while( !list_empty( &db->transforms ) )
3121 MSITRANSFORM *t = LIST_ENTRY( list_head( &db->transforms ),
3122 MSITRANSFORM, entry );
3123 list_remove( &t->entry );
3124 IStorage_Release( t->stg );
3129 void msi_free_streams( MSIDATABASE *db )
3131 while( !list_empty( &db->streams ) )
3133 MSISTREAM *s = LIST_ENTRY( list_head( &db->streams ),
3135 list_remove( &s->entry );
3136 IStream_Release( s->stm );