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
46 typedef struct tagMSICOLUMNHASHENTRY
48 struct tagMSICOLUMNHASHENTRY *next;
53 typedef struct tagMSICOLUMNINFO
62 MSICOLUMNHASHENTRY **hash_table;
65 typedef struct tagMSIORDERINFO
75 BOOL *data_persistent;
78 MSICOLUMNINFO *colinfo;
80 MSICONDITION persistent;
85 /* information for default tables */
86 static WCHAR szTables[] = { '_','T','a','b','l','e','s',0 };
87 static WCHAR szTable[] = { 'T','a','b','l','e',0 };
88 static WCHAR szName[] = { 'N','a','m','e',0 };
89 static WCHAR szColumns[] = { '_','C','o','l','u','m','n','s',0 };
90 static WCHAR szNumber[] = { 'N','u','m','b','e','r',0 };
91 static WCHAR szType[] = { 'T','y','p','e',0 };
93 static const MSICOLUMNINFO _Columns_cols[4] = {
94 { szColumns, 1, szTable, MSITYPE_VALID | MSITYPE_STRING | MSITYPE_KEY | 64, 0, 0, 0, NULL },
95 { szColumns, 2, szNumber, MSITYPE_VALID | MSITYPE_KEY | 2, 2, 0, 0, NULL },
96 { szColumns, 3, szName, MSITYPE_VALID | MSITYPE_STRING | 64, 4, 0, 0, NULL },
97 { szColumns, 4, szType, MSITYPE_VALID | 2, 6, 0, 0, NULL },
100 static const MSICOLUMNINFO _Tables_cols[1] = {
101 { szTables, 1, szName, MSITYPE_VALID | MSITYPE_STRING | MSITYPE_KEY | 64, 0, 0, 0, NULL },
104 #define MAX_STREAM_NAME 0x1f
106 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name,
107 MSICOLUMNINFO **pcols, UINT *pcount );
108 static void table_calc_column_offsets( MSIDATABASE *db, MSICOLUMNINFO *colinfo,
110 static UINT get_tablecolumns( MSIDATABASE *db,
111 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz);
112 static void msi_free_colinfo( MSICOLUMNINFO *colinfo, UINT count );
114 static inline UINT bytes_per_column( MSIDATABASE *db, const MSICOLUMNINFO *col, UINT bytes_per_strref )
116 if( MSITYPE_IS_BINARY(col->type) )
119 if( col->type & MSITYPE_STRING )
120 return bytes_per_strref;
122 if( (col->type & 0xff) <= 2)
125 if( (col->type & 0xff) != 4 )
126 ERR("Invalid column size!\n");
131 static int utf2mime(int x)
133 if( (x>='0') && (x<='9') )
135 if( (x>='A') && (x<='Z') )
137 if( (x>='a') && (x<='z') )
146 LPWSTR encode_streamname(BOOL bTable, LPCWSTR in)
148 DWORD count = MAX_STREAM_NAME;
153 count = lstrlenW( in )+2;
154 if (!(out = msi_alloc( count*sizeof(WCHAR) ))) return NULL;
170 if( ( ch < 0x80 ) && ( utf2mime(ch) >= 0 ) )
172 ch = utf2mime(ch) + 0x4800;
174 if( next && (next<0x80) )
176 next = utf2mime(next);
187 ERR("Failed to encode stream name (%s)\n",debugstr_w(in));
192 static int mime2utf(int x)
199 return x - 10 - 26 + 'a';
200 if( x == (10+26+26) )
205 BOOL decode_streamname(LPCWSTR in, LPWSTR out)
210 while ( (ch = *in++) )
212 if( (ch >= 0x3800 ) && (ch < 0x4840 ) )
215 ch = mime2utf(ch-0x4800);
219 *out++ = mime2utf(ch&0x3f);
221 ch = mime2utf((ch>>6)&0x3f);
231 void enum_stream_names( IStorage *stg )
233 IEnumSTATSTG *stgenum = NULL;
239 r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
247 r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
248 if( FAILED( r ) || !count )
250 decode_streamname( stat.pwcsName, name );
251 TRACE("stream %2d -> %s %s\n", n,
252 debugstr_w(stat.pwcsName), debugstr_w(name) );
253 CoTaskMemFree( stat.pwcsName );
257 IEnumSTATSTG_Release( stgenum );
260 UINT read_stream_data( IStorage *stg, LPCWSTR stname, BOOL table,
261 BYTE **pdata, UINT *psz )
264 UINT ret = ERROR_FUNCTION_FAILED;
271 encname = encode_streamname(table, stname);
273 TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
275 r = IStorage_OpenStream(stg, encname, NULL,
276 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stm);
280 WARN("open stream failed r = %08x - empty table?\n", r);
284 r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
287 WARN("open stream failed r = %08x!\n", r);
291 if( stat.cbSize.QuadPart >> 32 )
297 sz = stat.cbSize.QuadPart;
298 data = msi_alloc( sz );
301 WARN("couldn't allocate memory r=%08x!\n", r);
302 ret = ERROR_NOT_ENOUGH_MEMORY;
306 r = IStream_Read(stm, data, sz, &count );
307 if( FAILED( r ) || ( count != sz ) )
310 WARN("read stream failed r = %08x!\n", r);
319 IStream_Release( stm );
324 UINT write_stream_data( IStorage *stg, LPCWSTR stname,
325 LPCVOID data, UINT sz, BOOL bTable )
328 UINT ret = ERROR_FUNCTION_FAILED;
335 encname = encode_streamname(bTable, stname );
336 r = IStorage_OpenStream( stg, encname, NULL,
337 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, &stm);
340 r = IStorage_CreateStream( stg, encname,
341 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
346 WARN("open stream failed r = %08x\n", r);
351 r = IStream_SetSize( stm, size );
354 WARN("Failed to SetSize\n");
359 r = IStream_Seek( stm, pos, STREAM_SEEK_SET, NULL );
362 WARN("Failed to Seek\n");
368 r = IStream_Write(stm, data, sz, &count );
369 if( FAILED( r ) || ( count != sz ) )
371 WARN("Failed to Write\n");
379 IStream_Release( stm );
384 static void free_table( MSITABLE *table )
387 for( i=0; i<table->row_count; i++ )
388 msi_free( table->data[i] );
389 msi_free( table->data );
390 msi_free( table->data_persistent );
391 msi_free_colinfo( table->colinfo, table->col_count );
392 msi_free( table->colinfo );
396 static UINT msi_table_get_row_size( MSIDATABASE *db, const MSICOLUMNINFO *cols, UINT count, UINT bytes_per_strref )
398 const MSICOLUMNINFO *last_col;
403 if (bytes_per_strref != LONG_STR_BYTES)
406 for (i = 0; i < count; i++) size += bytes_per_column( db, &cols[i], bytes_per_strref );
409 last_col = &cols[count - 1];
410 return last_col->offset + bytes_per_column( db, last_col, bytes_per_strref );
413 /* add this table to the list of cached tables in the database */
414 static UINT read_table_from_storage( MSIDATABASE *db, MSITABLE *t, IStorage *stg )
416 BYTE *rawdata = NULL;
417 UINT rawsize = 0, i, j, row_size, row_size_mem;
419 TRACE("%s\n",debugstr_w(t->name));
421 row_size = msi_table_get_row_size( db, t->colinfo, t->col_count, db->bytes_per_strref );
422 row_size_mem = msi_table_get_row_size( db, t->colinfo, t->col_count, LONG_STR_BYTES );
424 /* if we can't read the table, just assume that it's empty */
425 read_stream_data( stg, t->name, TRUE, &rawdata, &rawsize );
427 return ERROR_SUCCESS;
429 TRACE("Read %d bytes\n", rawsize );
431 if( rawsize % row_size )
433 WARN("Table size is invalid %d/%d\n", rawsize, row_size );
437 t->row_count = rawsize / row_size;
438 t->data = msi_alloc_zero( t->row_count * sizeof (USHORT*) );
441 t->data_persistent = msi_alloc_zero( t->row_count * sizeof(BOOL));
442 if ( !t->data_persistent )
445 /* transpose all the data */
446 TRACE("Transposing data from %d rows\n", t->row_count );
447 for (i = 0; i < t->row_count; i++)
449 UINT ofs = 0, ofs_mem = 0;
451 t->data[i] = msi_alloc( row_size_mem );
454 t->data_persistent[i] = TRUE;
456 for (j = 0; j < t->col_count; j++)
458 UINT m = bytes_per_column( db, &t->colinfo[j], LONG_STR_BYTES );
459 UINT n = bytes_per_column( db, &t->colinfo[j], db->bytes_per_strref );
462 if ( n != 2 && n != 3 && n != 4 )
464 ERR("oops - unknown column width %d\n", n);
467 if (t->colinfo[j].type & MSITYPE_STRING && n < m)
469 for (k = 0; k < m; k++)
472 t->data[i][ofs_mem + k] = rawdata[ofs * t->row_count + i * n + k];
474 t->data[i][ofs_mem + k] = 0;
479 for (k = 0; k < n; k++)
480 t->data[i][ofs_mem + k] = rawdata[ofs * t->row_count + i * n + k];
488 return ERROR_SUCCESS;
491 return ERROR_FUNCTION_FAILED;
494 void free_cached_tables( MSIDATABASE *db )
496 while( !list_empty( &db->tables ) )
498 MSITABLE *t = LIST_ENTRY( list_head( &db->tables ), MSITABLE, entry );
500 list_remove( &t->entry );
505 static MSITABLE *find_cached_table( MSIDATABASE *db, LPCWSTR name )
509 LIST_FOR_EACH_ENTRY( t, &db->tables, MSITABLE, entry )
510 if( !strcmpW( name, t->name ) )
516 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name, MSICOLUMNINFO **pcols, UINT *pcount )
518 UINT r, column_count = 0;
519 MSICOLUMNINFO *columns;
521 /* get the number of columns in this table */
523 r = get_tablecolumns( db, name, NULL, &column_count );
524 if( r != ERROR_SUCCESS )
527 *pcount = column_count;
529 /* if there's no columns, there's no table */
530 if( column_count == 0 )
531 return ERROR_INVALID_PARAMETER;
533 TRACE("Table %s found\n", debugstr_w(name) );
535 columns = msi_alloc( column_count*sizeof (MSICOLUMNINFO) );
537 return ERROR_FUNCTION_FAILED;
539 r = get_tablecolumns( db, name, columns, &column_count );
540 if( r != ERROR_SUCCESS )
543 return ERROR_FUNCTION_FAILED;
551 UINT msi_create_table( MSIDATABASE *db, LPCWSTR name, column_info *col_info,
552 MSICONDITION persistent, MSITABLE **table_ret)
556 MSIRECORD *rec = NULL;
561 /* only add tables that don't exist already */
562 if( TABLE_Exists(db, name ) )
564 WARN("table %s exists\n", debugstr_w(name));
565 return ERROR_BAD_QUERY_SYNTAX;
568 table = msi_alloc( sizeof (MSITABLE) + lstrlenW(name)*sizeof (WCHAR) );
570 return ERROR_FUNCTION_FAILED;
572 table->ref_count = 1;
573 table->row_count = 0;
575 table->data_persistent = NULL;
576 table->colinfo = NULL;
577 table->col_count = 0;
578 table->persistent = persistent;
579 lstrcpyW( table->name, name );
581 for( col = col_info; col; col = col->next )
584 table->colinfo = msi_alloc( table->col_count * sizeof(MSICOLUMNINFO) );
588 return ERROR_FUNCTION_FAILED;
591 for( i = 0, col = col_info; col; i++, col = col->next )
593 table->colinfo[ i ].tablename = strdupW( col->table );
594 table->colinfo[ i ].number = i + 1;
595 table->colinfo[ i ].colname = strdupW( col->column );
596 table->colinfo[ i ].type = col->type;
597 table->colinfo[ i ].offset = 0;
598 table->colinfo[ i ].ref_count = 0;
599 table->colinfo[ i ].hash_table = NULL;
600 table->colinfo[ i ].temporary = col->temporary;
602 table_calc_column_offsets( db, table->colinfo, table->col_count);
604 r = TABLE_CreateView( db, szTables, &tv );
605 TRACE("CreateView returned %x\n", r);
612 r = tv->ops->execute( tv, 0 );
613 TRACE("tv execute returned %x\n", r);
617 rec = MSI_CreateRecord( 1 );
621 r = MSI_RecordSetStringW( rec, 1, name );
625 r = tv->ops->insert_row( tv, rec, -1, persistent == MSICONDITION_FALSE );
626 TRACE("insert_row returned %x\n", r);
630 tv->ops->delete( tv );
633 msiobj_release( &rec->hdr );
636 if( persistent != MSICONDITION_FALSE )
638 /* add each column to the _Columns table */
639 r = TABLE_CreateView( db, szColumns, &tv );
643 r = tv->ops->execute( tv, 0 );
644 TRACE("tv execute returned %x\n", r);
648 rec = MSI_CreateRecord( 4 );
652 r = MSI_RecordSetStringW( rec, 1, name );
657 * need to set the table, column number, col name and type
658 * for each column we enter in the table
661 for( col = col_info; col; col = col->next )
663 r = MSI_RecordSetInteger( rec, 2, nField );
667 r = MSI_RecordSetStringW( rec, 3, col->column );
671 r = MSI_RecordSetInteger( rec, 4, col->type );
675 r = tv->ops->insert_row( tv, rec, -1, FALSE );
687 msiobj_release( &rec->hdr );
688 /* FIXME: remove values from the string table on error */
690 tv->ops->delete( tv );
692 if (r == ERROR_SUCCESS)
694 list_add_head( &db->tables, &table->entry );
703 static UINT get_table( MSIDATABASE *db, LPCWSTR name, MSITABLE **table_ret )
708 /* first, see if the table is cached */
709 table = find_cached_table( db, name );
713 return ERROR_SUCCESS;
716 /* nonexistent tables should be interpreted as empty tables */
717 table = msi_alloc( sizeof (MSITABLE) + lstrlenW(name)*sizeof (WCHAR) );
719 return ERROR_FUNCTION_FAILED;
721 table->row_count = 0;
723 table->data_persistent = NULL;
724 table->colinfo = NULL;
725 table->col_count = 0;
726 table->persistent = MSICONDITION_TRUE;
727 lstrcpyW( table->name, name );
729 if ( !strcmpW( name, szTables ) || !strcmpW( name, szColumns ) )
730 table->persistent = MSICONDITION_NONE;
732 r = table_get_column_info( db, name, &table->colinfo, &table->col_count);
733 if (r != ERROR_SUCCESS)
735 free_table ( table );
739 r = read_table_from_storage( db, table, db->storage );
740 if( r != ERROR_SUCCESS )
746 list_add_head( &db->tables, &table->entry );
748 return ERROR_SUCCESS;
751 static UINT read_table_int(BYTE *const *data, UINT row, UINT col, UINT bytes)
755 for (i = 0; i < bytes; i++)
756 ret += data[row][col + i] << i * 8;
761 static UINT save_table( MSIDATABASE *db, const MSITABLE *t, UINT bytes_per_strref )
763 BYTE *rawdata = NULL;
764 UINT rawsize, i, j, row_size, row_count;
765 UINT r = ERROR_FUNCTION_FAILED;
767 /* Nothing to do for non-persistent tables */
768 if( t->persistent == MSICONDITION_FALSE )
769 return ERROR_SUCCESS;
771 TRACE("Saving %s\n", debugstr_w( t->name ) );
773 row_size = msi_table_get_row_size( db, t->colinfo, t->col_count, bytes_per_strref );
774 row_count = t->row_count;
775 for (i = 0; i < t->row_count; i++)
777 if (!t->data_persistent[i])
779 row_count = 1; /* yes, this is bizarre */
783 rawsize = row_count * row_size;
784 rawdata = msi_alloc_zero( rawsize );
787 r = ERROR_NOT_ENOUGH_MEMORY;
792 for (i = 0; i < t->row_count; i++)
794 UINT ofs = 0, ofs_mem = 0;
796 if (!t->data_persistent[i]) break;
798 for (j = 0; j < t->col_count; j++)
800 UINT m = bytes_per_column( db, &t->colinfo[j], LONG_STR_BYTES );
801 UINT n = bytes_per_column( db, &t->colinfo[j], bytes_per_strref );
804 if (n != 2 && n != 3 && n != 4)
806 ERR("oops - unknown column width %d\n", n);
809 if (t->colinfo[j].type & MSITYPE_STRING && n < m)
811 UINT id = read_table_int( t->data, i, ofs_mem, LONG_STR_BYTES );
812 if (id > 1 << bytes_per_strref * 8)
814 ERR("string id %u out of range\n", id);
818 for (k = 0; k < n; k++)
820 rawdata[ofs * row_count + i * n + k] = t->data[i][ofs_mem + k];
828 TRACE("writing %d bytes\n", rawsize);
829 r = write_stream_data( db->storage, t->name, rawdata, rawsize, TRUE );
836 static void table_calc_column_offsets( MSIDATABASE *db, MSICOLUMNINFO *colinfo, DWORD count )
840 for( i=0; colinfo && (i<count); i++ )
842 assert( (i+1) == colinfo[ i ].number );
844 colinfo[i].offset = colinfo[ i - 1 ].offset
845 + bytes_per_column( db, &colinfo[ i - 1 ], LONG_STR_BYTES );
847 colinfo[i].offset = 0;
848 TRACE("column %d is [%s] with type %08x ofs %d\n",
849 colinfo[i].number, debugstr_w(colinfo[i].colname),
850 colinfo[i].type, colinfo[i].offset);
854 static UINT get_defaulttablecolumns( MSIDATABASE *db, LPCWSTR name,
855 MSICOLUMNINFO *colinfo, UINT *sz)
857 const MSICOLUMNINFO *p;
860 TRACE("%s\n", debugstr_w(name));
862 if (!strcmpW( name, szTables ))
867 else if (!strcmpW( name, szColumns ))
873 return ERROR_FUNCTION_FAILED;
875 /* duplicate the string data so we can free it in msi_free_colinfo */
878 if (colinfo && (i < *sz) )
881 colinfo[i].tablename = strdupW( p[i].tablename );
882 colinfo[i].colname = strdupW( p[i].colname );
884 if( colinfo && (i >= *sz) )
887 table_calc_column_offsets( db, colinfo, n );
889 return ERROR_SUCCESS;
892 static void msi_free_colinfo( MSICOLUMNINFO *colinfo, UINT count )
896 for( i=0; i<count; i++ )
898 msi_free( colinfo[i].tablename );
899 msi_free( colinfo[i].colname );
900 msi_free( colinfo[i].hash_table );
904 static LPWSTR msi_makestring( const MSIDATABASE *db, UINT stringid)
906 return strdupW(msi_string_lookup_id( db->strings, stringid ));
909 static UINT get_tablecolumns( MSIDATABASE *db,
910 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz)
912 UINT r, i, n=0, table_id, count, maxcount = *sz;
913 MSITABLE *table = NULL;
915 TRACE("%s\n", debugstr_w(szTableName));
917 /* first check if there is a default table with that name */
918 r = get_defaulttablecolumns( db, szTableName, colinfo, sz );
919 if( ( r == ERROR_SUCCESS ) && *sz )
922 r = get_table( db, szColumns, &table );
923 if( r != ERROR_SUCCESS )
925 ERR("couldn't load _Columns table\n");
926 return ERROR_FUNCTION_FAILED;
929 /* convert table and column names to IDs from the string table */
930 r = msi_string2idW( db->strings, szTableName, &table_id );
931 if( r != ERROR_SUCCESS )
933 WARN("Couldn't find id for %s\n", debugstr_w(szTableName));
937 TRACE("Table id is %d, row count is %d\n", table_id, table->row_count);
939 /* Note: _Columns table doesn't have non-persistent data */
941 /* if maxcount is non-zero, assume it's exactly right for this table */
942 memset( colinfo, 0, maxcount*sizeof(*colinfo) );
943 count = table->row_count;
944 for( i=0; i<count; i++ )
946 if( read_table_int(table->data, i, 0, LONG_STR_BYTES) != table_id )
950 UINT id = read_table_int(table->data, i, table->colinfo[2].offset, LONG_STR_BYTES);
951 UINT col = read_table_int(table->data, i, table->colinfo[1].offset, sizeof(USHORT)) - (1<<15);
953 /* check the column number is in range */
954 if (col<1 || col>maxcount)
956 ERR("column %d out of range\n", col);
960 /* check if this column was already set */
961 if (colinfo[ col - 1 ].number)
963 ERR("duplicate column %d\n", col);
967 colinfo[ col - 1 ].tablename = msi_makestring( db, table_id );
968 colinfo[ col - 1 ].number = col;
969 colinfo[ col - 1 ].colname = msi_makestring( db, id );
970 colinfo[ col - 1 ].type = read_table_int(table->data, i,
971 table->colinfo[3].offset,
972 sizeof(USHORT)) - (1<<15);
973 colinfo[ col - 1 ].offset = 0;
974 colinfo[ col - 1 ].ref_count = 0;
975 colinfo[ col - 1 ].hash_table = NULL;
980 TRACE("%s has %d columns\n", debugstr_w(szTableName), n);
982 if (colinfo && n != maxcount)
984 ERR("missing column in table %s\n", debugstr_w(szTableName));
985 msi_free_colinfo(colinfo, maxcount );
986 return ERROR_FUNCTION_FAILED;
989 table_calc_column_offsets( db, colinfo, n );
992 return ERROR_SUCCESS;
995 static void msi_update_table_columns( MSIDATABASE *db, LPCWSTR name )
999 UINT size, offset, old_count;
1002 /* We may free name in msi_free_colinfo. */
1003 tablename = strdupW( name );
1005 table = find_cached_table( db, tablename );
1006 old_count = table->col_count;
1007 msi_free_colinfo( table->colinfo, table->col_count );
1008 msi_free( table->colinfo );
1009 table->colinfo = NULL;
1011 table_get_column_info( db, tablename, &table->colinfo, &table->col_count );
1012 if (!table->col_count)
1015 size = msi_table_get_row_size( db, table->colinfo, table->col_count, LONG_STR_BYTES );
1016 offset = table->colinfo[table->col_count - 1].offset;
1018 for ( n = 0; n < table->row_count; n++ )
1020 table->data[n] = msi_realloc( table->data[n], size );
1021 if (old_count < table->col_count)
1022 memset( &table->data[n][offset], 0, size - offset );
1026 msi_free(tablename);
1029 /* try to find the table name in the _Tables table */
1030 BOOL TABLE_Exists( MSIDATABASE *db, LPCWSTR name )
1032 UINT r, table_id, i;
1035 if( !strcmpW( name, szTables ) || !strcmpW( name, szColumns ) ||
1036 !strcmpW( name, szStreams ) || !strcmpW( name, szStorages ) )
1039 r = msi_string2idW( db->strings, name, &table_id );
1040 if( r != ERROR_SUCCESS )
1042 TRACE("Couldn't find id for %s\n", debugstr_w(name));
1046 r = get_table( db, szTables, &table );
1047 if( r != ERROR_SUCCESS )
1049 ERR("table %s not available\n", debugstr_w(szTables));
1053 for( i = 0; i < table->row_count; i++ )
1055 if( read_table_int( table->data, i, 0, LONG_STR_BYTES ) == table_id )
1062 /* below is the query interface to a table */
1064 typedef struct tagMSITABLEVIEW
1069 MSICOLUMNINFO *columns;
1070 MSIORDERINFO *order;
1076 static UINT TABLE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
1078 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1082 return ERROR_INVALID_PARAMETER;
1084 if( (col==0) || (col>tv->num_cols) )
1085 return ERROR_INVALID_PARAMETER;
1087 /* how many rows are there ? */
1088 if( row >= tv->table->row_count )
1089 return ERROR_NO_MORE_ITEMS;
1091 if( tv->columns[col-1].offset >= tv->row_size )
1093 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1094 ERR("%p %p\n", tv, tv->columns );
1095 return ERROR_FUNCTION_FAILED;
1099 row = tv->order->reorder[row];
1101 n = bytes_per_column( tv->db, &tv->columns[col - 1], LONG_STR_BYTES );
1102 if (n != 2 && n != 3 && n != 4)
1104 ERR("oops! what is %d bytes per column?\n", n );
1105 return ERROR_FUNCTION_FAILED;
1108 offset = tv->columns[col-1].offset;
1109 *val = read_table_int(tv->table->data, row, offset, n);
1111 /* TRACE("Data [%d][%d] = %d\n", row, col, *val ); */
1113 return ERROR_SUCCESS;
1116 static UINT msi_stream_name( const MSITABLEVIEW *tv, UINT row, LPWSTR *pstname )
1118 LPWSTR p, stname = NULL;
1119 UINT i, r, type, ival;
1122 MSIVIEW *view = (MSIVIEW *) tv;
1124 TRACE("%p %d\n", tv, row);
1126 len = lstrlenW( tv->name ) + 1;
1127 stname = msi_alloc( len*sizeof(WCHAR) );
1130 r = ERROR_OUTOFMEMORY;
1134 lstrcpyW( stname, tv->name );
1136 for ( i = 0; i < tv->num_cols; i++ )
1138 type = tv->columns[i].type;
1139 if ( type & MSITYPE_KEY )
1143 r = TABLE_fetch_int( view, row, i+1, &ival );
1144 if ( r != ERROR_SUCCESS )
1147 if ( tv->columns[i].type & MSITYPE_STRING )
1149 sval = msi_string_lookup_id( tv->db->strings, ival );
1152 r = ERROR_INVALID_PARAMETER;
1158 static const WCHAR fmt[] = { '%','d',0 };
1159 UINT n = bytes_per_column( tv->db, &tv->columns[i], LONG_STR_BYTES );
1164 sprintfW( number, fmt, ival-0x8000 );
1167 sprintfW( number, fmt, ival^0x80000000 );
1170 ERR( "oops - unknown column width %d\n", n );
1171 r = ERROR_FUNCTION_FAILED;
1177 len += lstrlenW( szDot ) + lstrlenW( sval );
1178 p = msi_realloc ( stname, len*sizeof(WCHAR) );
1181 r = ERROR_OUTOFMEMORY;
1186 lstrcatW( stname, szDot );
1187 lstrcatW( stname, sval );
1194 return ERROR_SUCCESS;
1203 * We need a special case for streams, as we need to reference column with
1204 * the name of the stream in the same table, and the table name
1205 * which may not be available at higher levels of the query
1207 static UINT TABLE_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm )
1209 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1211 LPWSTR encname, full_name = NULL;
1213 if( !view->ops->fetch_int )
1214 return ERROR_INVALID_PARAMETER;
1216 r = msi_stream_name( tv, row, &full_name );
1217 if ( r != ERROR_SUCCESS )
1219 ERR("fetching stream, error = %d\n", r);
1223 encname = encode_streamname( FALSE, full_name );
1224 r = msi_get_raw_stream( tv->db, encname, stm );
1226 ERR("fetching stream %s, error = %d\n",debugstr_w(full_name), r);
1228 msi_free( full_name );
1229 msi_free( encname );
1233 static UINT TABLE_set_int( MSITABLEVIEW *tv, UINT row, UINT col, UINT val )
1238 return ERROR_INVALID_PARAMETER;
1240 if( (col==0) || (col>tv->num_cols) )
1241 return ERROR_INVALID_PARAMETER;
1243 if( row >= tv->table->row_count )
1244 return ERROR_INVALID_PARAMETER;
1246 if( tv->columns[col-1].offset >= tv->row_size )
1248 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1249 ERR("%p %p\n", tv, tv->columns );
1250 return ERROR_FUNCTION_FAILED;
1253 msi_free( tv->columns[col-1].hash_table );
1254 tv->columns[col-1].hash_table = NULL;
1256 n = bytes_per_column( tv->db, &tv->columns[col - 1], LONG_STR_BYTES );
1257 if ( n != 2 && n != 3 && n != 4 )
1259 ERR("oops! what is %d bytes per column?\n", n );
1260 return ERROR_FUNCTION_FAILED;
1263 offset = tv->columns[col-1].offset;
1264 for ( i = 0; i < n; i++ )
1265 tv->table->data[row][offset + i] = (val >> i * 8) & 0xff;
1267 return ERROR_SUCCESS;
1270 static UINT TABLE_get_row( struct tagMSIVIEW *view, UINT row, MSIRECORD **rec )
1272 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1275 return ERROR_INVALID_PARAMETER;
1278 row = tv->order->reorder[row];
1280 return msi_view_get_row(tv->db, view, row, rec);
1283 static UINT msi_addstreamW( MSIDATABASE *db, LPCWSTR name, IStream *data )
1286 MSIQUERY *query = NULL;
1287 MSIRECORD *rec = NULL;
1289 static const WCHAR insert[] = {
1290 'I','N','S','E','R','T',' ','I','N','T','O',' ',
1291 '`','_','S','t','r','e','a','m','s','`',' ',
1292 '(','`','N','a','m','e','`',',',
1293 '`','D','a','t','a','`',')',' ',
1294 'V','A','L','U','E','S',' ','(','?',',','?',')',0};
1296 TRACE("%p %s %p\n", db, debugstr_w(name), data);
1298 rec = MSI_CreateRecord( 2 );
1300 return ERROR_OUTOFMEMORY;
1302 r = MSI_RecordSetStringW( rec, 1, name );
1303 if ( r != ERROR_SUCCESS )
1306 r = MSI_RecordSetIStream( rec, 2, data );
1307 if ( r != ERROR_SUCCESS )
1310 r = MSI_DatabaseOpenViewW( db, insert, &query );
1311 if ( r != ERROR_SUCCESS )
1314 r = MSI_ViewExecute( query, rec );
1317 msiobj_release( &query->hdr );
1318 msiobj_release( &rec->hdr );
1323 static UINT get_table_value_from_record( MSITABLEVIEW *tv, MSIRECORD *rec, UINT iField, UINT *pvalue )
1325 MSICOLUMNINFO columninfo;
1328 if ( (iField <= 0) ||
1329 (iField > tv->num_cols) ||
1330 MSI_RecordIsNull( rec, iField ) )
1331 return ERROR_FUNCTION_FAILED;
1333 columninfo = tv->columns[ iField - 1 ];
1335 if ( MSITYPE_IS_BINARY(columninfo.type) )
1337 *pvalue = 1; /* refers to the first key column */
1339 else if ( columninfo.type & MSITYPE_STRING )
1341 LPCWSTR sval = MSI_RecordGetString( rec, iField );
1344 r = msi_string2idW(tv->db->strings, sval, pvalue);
1345 if (r != ERROR_SUCCESS)
1346 return ERROR_NOT_FOUND;
1350 else if ( bytes_per_column( tv->db, &columninfo, LONG_STR_BYTES ) == 2 )
1352 *pvalue = 0x8000 + MSI_RecordGetInteger( rec, iField );
1353 if ( *pvalue & 0xffff0000 )
1355 ERR("field %u value %d out of range\n", iField, *pvalue - 0x8000);
1356 return ERROR_FUNCTION_FAILED;
1361 INT ival = MSI_RecordGetInteger( rec, iField );
1362 *pvalue = ival ^ 0x80000000;
1365 return ERROR_SUCCESS;
1368 static UINT TABLE_set_row( struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask )
1370 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1371 UINT i, val, r = ERROR_SUCCESS;
1374 return ERROR_INVALID_PARAMETER;
1376 /* test if any of the mask bits are invalid */
1377 if ( mask >= (1<<tv->num_cols) )
1378 return ERROR_INVALID_PARAMETER;
1380 for ( i = 0; i < tv->num_cols; i++ )
1384 /* only update the fields specified in the mask */
1385 if ( !(mask&(1<<i)) )
1388 persistent = (tv->table->persistent != MSICONDITION_FALSE) &&
1389 (tv->table->data_persistent[row]);
1390 /* FIXME: should we allow updating keys? */
1393 if ( !MSI_RecordIsNull( rec, i + 1 ) )
1395 r = get_table_value_from_record (tv, rec, i + 1, &val);
1396 if ( MSITYPE_IS_BINARY(tv->columns[ i ].type) )
1401 if ( r != ERROR_SUCCESS )
1402 return ERROR_FUNCTION_FAILED;
1404 r = MSI_RecordGetIStream( rec, i + 1, &stm );
1405 if ( r != ERROR_SUCCESS )
1408 r = msi_stream_name( tv, row, &stname );
1409 if ( r != ERROR_SUCCESS )
1411 IStream_Release( stm );
1415 r = msi_addstreamW( tv->db, stname, stm );
1416 IStream_Release( stm );
1417 msi_free ( stname );
1419 if ( r != ERROR_SUCCESS )
1422 else if ( tv->columns[i].type & MSITYPE_STRING )
1426 if ( r != ERROR_SUCCESS )
1428 LPCWSTR sval = MSI_RecordGetString( rec, i + 1 );
1429 val = msi_addstringW( tv->db->strings, sval, -1, 1,
1430 persistent ? StringPersistent : StringNonPersistent );
1434 TABLE_fetch_int(&tv->view, row, i + 1, &x);
1441 if ( r != ERROR_SUCCESS )
1442 return ERROR_FUNCTION_FAILED;
1446 r = TABLE_set_int( tv, row, i+1, val );
1447 if ( r != ERROR_SUCCESS )
1453 static UINT table_create_new_row( struct tagMSIVIEW *view, UINT *num, BOOL temporary )
1455 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1460 BOOL **data_persist_ptr;
1463 TRACE("%p %s\n", view, temporary ? "TRUE" : "FALSE");
1466 return ERROR_INVALID_PARAMETER;
1468 row = msi_alloc_zero( tv->row_size );
1470 return ERROR_NOT_ENOUGH_MEMORY;
1472 row_count = &tv->table->row_count;
1473 data_ptr = &tv->table->data;
1474 data_persist_ptr = &tv->table->data_persistent;
1476 *num = tv->table->row_count;
1478 sz = (*row_count + 1) * sizeof (BYTE*);
1480 p = msi_realloc( *data_ptr, sz );
1482 p = msi_alloc( sz );
1486 return ERROR_NOT_ENOUGH_MEMORY;
1489 sz = (*row_count + 1) * sizeof (BOOL);
1490 if( *data_persist_ptr )
1491 b = msi_realloc( *data_persist_ptr, sz );
1493 b = msi_alloc( sz );
1498 return ERROR_NOT_ENOUGH_MEMORY;
1502 (*data_ptr)[*row_count] = row;
1504 *data_persist_ptr = b;
1505 (*data_persist_ptr)[*row_count] = !temporary;
1509 return ERROR_SUCCESS;
1512 static UINT TABLE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
1514 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1516 TRACE("%p %p\n", tv, record);
1518 TRACE("There are %d columns\n", tv->num_cols );
1520 return ERROR_SUCCESS;
1523 static UINT TABLE_close( struct tagMSIVIEW *view )
1525 TRACE("%p\n", view );
1527 return ERROR_SUCCESS;
1530 static UINT TABLE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols)
1532 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1534 TRACE("%p %p %p\n", view, rows, cols );
1537 *cols = tv->num_cols;
1541 return ERROR_INVALID_PARAMETER;
1542 *rows = tv->table->row_count;
1545 return ERROR_SUCCESS;
1548 static UINT TABLE_get_column_info( struct tagMSIVIEW *view,
1549 UINT n, LPWSTR *name, UINT *type, BOOL *temporary,
1550 LPWSTR *table_name )
1552 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1554 TRACE("%p %d %p %p\n", tv, n, name, type );
1556 if( ( n == 0 ) || ( n > tv->num_cols ) )
1557 return ERROR_INVALID_PARAMETER;
1561 *name = strdupW( tv->columns[n-1].colname );
1563 return ERROR_FUNCTION_FAILED;
1568 *table_name = strdupW( tv->columns[n-1].tablename );
1570 return ERROR_FUNCTION_FAILED;
1574 *type = tv->columns[n-1].type;
1577 *temporary = tv->columns[n-1].temporary;
1579 return ERROR_SUCCESS;
1582 static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row, UINT *column );
1584 static UINT table_validate_new( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *column )
1588 /* check there's no null values where they're not allowed */
1589 for( i = 0; i < tv->num_cols; i++ )
1591 if ( tv->columns[i].type & MSITYPE_NULLABLE )
1594 if ( MSITYPE_IS_BINARY(tv->columns[i].type) )
1595 TRACE("skipping binary column\n");
1596 else if ( tv->columns[i].type & MSITYPE_STRING )
1600 str = MSI_RecordGetString( rec, i+1 );
1601 if (str == NULL || str[0] == 0)
1603 if (column) *column = i;
1604 return ERROR_INVALID_DATA;
1611 n = MSI_RecordGetInteger( rec, i+1 );
1612 if (n == MSI_NULL_INTEGER)
1614 if (column) *column = i;
1615 return ERROR_INVALID_DATA;
1620 /* check there's no duplicate keys */
1621 r = msi_table_find_row( tv, rec, &row, column );
1622 if (r == ERROR_SUCCESS)
1623 return ERROR_FUNCTION_FAILED;
1625 return ERROR_SUCCESS;
1628 static int compare_record( MSITABLEVIEW *tv, UINT row, MSIRECORD *rec )
1630 UINT r, i, ivalue, x;
1632 for (i = 0; i < tv->num_cols; i++ )
1634 if (!(tv->columns[i].type & MSITYPE_KEY)) continue;
1636 r = get_table_value_from_record( tv, rec, i + 1, &ivalue );
1637 if (r != ERROR_SUCCESS)
1640 r = TABLE_fetch_int( &tv->view, row, i + 1, &x );
1641 if (r != ERROR_SUCCESS)
1643 WARN("TABLE_fetch_int should not fail here %u\n", r);
1650 else if (ivalue == x)
1652 if (i < tv->num_cols - 1) continue;
1661 static int find_insert_index( MSITABLEVIEW *tv, MSIRECORD *rec )
1663 int idx, c, low = 0, high = tv->table->row_count - 1;
1665 TRACE("%p %p\n", tv, rec);
1669 idx = (low + high) / 2;
1670 c = compare_record( tv, idx, rec );
1678 TRACE("found %u\n", idx);
1682 TRACE("found %u\n", high + 1);
1686 static UINT TABLE_insert_row( struct tagMSIVIEW *view, MSIRECORD *rec, UINT row, BOOL temporary )
1688 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1691 TRACE("%p %p %s\n", tv, rec, temporary ? "TRUE" : "FALSE" );
1693 /* check that the key is unique - can we find a matching row? */
1694 r = table_validate_new( tv, rec, NULL );
1695 if( r != ERROR_SUCCESS )
1696 return ERROR_FUNCTION_FAILED;
1699 row = find_insert_index( tv, rec );
1701 r = table_create_new_row( view, &row, temporary );
1702 TRACE("insert_row returned %08x\n", r);
1703 if( r != ERROR_SUCCESS )
1706 /* shift the rows to make room for the new row */
1707 for (i = tv->table->row_count - 1; i > row; i--)
1709 memmove(&(tv->table->data[i][0]),
1710 &(tv->table->data[i - 1][0]), tv->row_size);
1711 tv->table->data_persistent[i] = tv->table->data_persistent[i - 1];
1714 /* Re-set the persistence flag */
1715 tv->table->data_persistent[row] = !temporary;
1716 return TABLE_set_row( view, row, rec, (1<<tv->num_cols) - 1 );
1719 static UINT TABLE_delete_row( struct tagMSIVIEW *view, UINT row )
1721 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1722 UINT r, num_rows, num_cols, i;
1724 TRACE("%p %d\n", tv, row);
1727 return ERROR_INVALID_PARAMETER;
1729 r = TABLE_get_dimensions( view, &num_rows, &num_cols );
1730 if ( r != ERROR_SUCCESS )
1733 if ( row >= num_rows )
1734 return ERROR_FUNCTION_FAILED;
1736 num_rows = tv->table->row_count;
1737 tv->table->row_count--;
1739 /* reset the hash tables */
1740 for (i = 0; i < tv->num_cols; i++)
1742 msi_free( tv->columns[i].hash_table );
1743 tv->columns[i].hash_table = NULL;
1746 for (i = row + 1; i < num_rows; i++)
1748 memcpy(tv->table->data[i - 1], tv->table->data[i], tv->row_size);
1749 tv->table->data_persistent[i - 1] = tv->table->data_persistent[i];
1752 msi_free(tv->table->data[num_rows - 1]);
1754 return ERROR_SUCCESS;
1757 static UINT msi_table_update(struct tagMSIVIEW *view, MSIRECORD *rec, UINT row)
1759 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1762 /* FIXME: MsiViewFetch should set rec index 0 to some ID that
1763 * sets the fetched record apart from other records
1767 return ERROR_INVALID_PARAMETER;
1769 r = msi_table_find_row(tv, rec, &new_row, NULL);
1770 if (r != ERROR_SUCCESS)
1772 ERR("can't find row to modify\n");
1773 return ERROR_FUNCTION_FAILED;
1776 /* the row cannot be changed */
1777 if (row != new_row + 1)
1778 return ERROR_FUNCTION_FAILED;
1781 new_row = tv->order->reorder[new_row];
1783 return TABLE_set_row(view, new_row, rec, (1 << tv->num_cols) - 1);
1786 static UINT msi_table_assign(struct tagMSIVIEW *view, MSIRECORD *rec)
1788 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1792 return ERROR_INVALID_PARAMETER;
1794 r = msi_table_find_row(tv, rec, &row, NULL);
1795 if (r == ERROR_SUCCESS)
1796 return TABLE_set_row(view, row, rec, (1 << tv->num_cols) - 1);
1798 return TABLE_insert_row( view, rec, -1, FALSE );
1801 static UINT modify_delete_row( struct tagMSIVIEW *view, MSIRECORD *rec )
1803 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1806 r = msi_table_find_row(tv, rec, &row, NULL);
1807 if (r != ERROR_SUCCESS)
1810 return TABLE_delete_row(view, row);
1813 static UINT msi_refresh_record( struct tagMSIVIEW *view, MSIRECORD *rec, UINT row )
1818 r = TABLE_get_row(view, row - 1, &curr);
1819 if (r != ERROR_SUCCESS)
1822 /* Close the original record */
1823 MSI_CloseRecord(&rec->hdr);
1825 count = MSI_RecordGetFieldCount(rec);
1826 for (i = 0; i < count; i++)
1827 MSI_RecordCopyField(curr, i + 1, rec, i + 1);
1829 msiobj_release(&curr->hdr);
1830 return ERROR_SUCCESS;
1833 static UINT TABLE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
1834 MSIRECORD *rec, UINT row)
1836 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1839 TRACE("%p %d %p\n", view, eModifyMode, rec );
1841 switch (eModifyMode)
1843 case MSIMODIFY_DELETE:
1844 r = modify_delete_row( view, rec );
1846 case MSIMODIFY_VALIDATE_NEW:
1847 r = table_validate_new( tv, rec, &column );
1848 if (r != ERROR_SUCCESS)
1850 tv->view.error = MSIDBERROR_DUPLICATEKEY;
1851 tv->view.error_column = tv->columns[column].colname;
1852 r = ERROR_INVALID_DATA;
1856 case MSIMODIFY_INSERT:
1857 r = table_validate_new( tv, rec, NULL );
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, NULL );
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, NULL);
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 (!strcmpW( 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, NULL);
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);
2321 msiobj_release(&rec->hdr);
2322 tables->ops->delete(tables);
2327 static const MSIVIEWOPS table_ops =
2337 TABLE_get_dimensions,
2338 TABLE_get_column_info,
2341 TABLE_find_matching_rows,
2345 TABLE_remove_column,
2350 UINT TABLE_CreateView( MSIDATABASE *db, LPCWSTR name, MSIVIEW **view )
2355 TRACE("%p %s %p\n", db, debugstr_w(name), view );
2357 if ( !strcmpW( name, szStreams ) )
2358 return STREAMS_CreateView( db, view );
2359 else if ( !strcmpW( name, szStorages ) )
2360 return STORAGES_CreateView( db, view );
2362 sz = sizeof *tv + lstrlenW(name)*sizeof name[0] ;
2363 tv = msi_alloc_zero( sz );
2365 return ERROR_FUNCTION_FAILED;
2367 r = get_table( db, name, &tv->table );
2368 if( r != ERROR_SUCCESS )
2371 WARN("table not found\n");
2375 TRACE("table %p found with %d columns\n", tv->table, tv->table->col_count);
2377 /* fill the structure */
2378 tv->view.ops = &table_ops;
2380 tv->columns = tv->table->colinfo;
2381 tv->num_cols = tv->table->col_count;
2382 tv->row_size = msi_table_get_row_size( db, tv->table->colinfo, tv->table->col_count, LONG_STR_BYTES );
2384 TRACE("%s one row is %d bytes\n", debugstr_w(name), tv->row_size );
2386 *view = (MSIVIEW*) tv;
2387 lstrcpyW( tv->name, name );
2389 return ERROR_SUCCESS;
2392 UINT MSI_CommitTables( MSIDATABASE *db )
2394 UINT r, bytes_per_strref;
2396 MSITABLE *table = NULL;
2400 r = msi_save_string_table( db->strings, db->storage, &bytes_per_strref );
2401 if( r != ERROR_SUCCESS )
2403 WARN("failed to save string table r=%08x\n",r);
2407 LIST_FOR_EACH_ENTRY( table, &db->tables, MSITABLE, entry )
2409 r = save_table( db, table, bytes_per_strref );
2410 if( r != ERROR_SUCCESS )
2412 WARN("failed to save table %s (r=%08x)\n",
2413 debugstr_w(table->name), r);
2418 /* force everything to reload next time */
2419 free_cached_tables( db );
2421 hr = IStorage_Commit( db->storage, 0 );
2424 WARN("failed to commit changes 0x%08x\n", hr);
2425 r = ERROR_FUNCTION_FAILED;
2430 MSICONDITION MSI_DatabaseIsTablePersistent( MSIDATABASE *db, LPCWSTR table )
2435 TRACE("%p %s\n", db, debugstr_w(table));
2438 return MSICONDITION_ERROR;
2440 r = get_table( db, table, &t );
2441 if (r != ERROR_SUCCESS)
2442 return MSICONDITION_NONE;
2444 return t->persistent;
2447 static UINT read_raw_int(const BYTE *data, UINT col, UINT bytes)
2451 for (i = 0; i < bytes; i++)
2452 ret += (data[col + i] << i * 8);
2457 static UINT msi_record_encoded_stream_name( const MSITABLEVIEW *tv, MSIRECORD *rec, LPWSTR *pstname )
2459 LPWSTR stname = NULL, sval, p;
2463 TRACE("%p %p\n", tv, rec);
2465 len = lstrlenW( tv->name ) + 1;
2466 stname = msi_alloc( len*sizeof(WCHAR) );
2469 r = ERROR_OUTOFMEMORY;
2473 lstrcpyW( stname, tv->name );
2475 for ( i = 0; i < tv->num_cols; i++ )
2477 if ( tv->columns[i].type & MSITYPE_KEY )
2479 sval = msi_dup_record_field( rec, i + 1 );
2482 r = ERROR_OUTOFMEMORY;
2486 len += lstrlenW( szDot ) + lstrlenW ( sval );
2487 p = msi_realloc ( stname, len*sizeof(WCHAR) );
2490 r = ERROR_OUTOFMEMORY;
2495 lstrcatW( stname, szDot );
2496 lstrcatW( stname, sval );
2504 *pstname = encode_streamname( FALSE, stname );
2507 return ERROR_SUCCESS;
2510 msi_free ( stname );
2515 static MSIRECORD *msi_get_transform_record( const MSITABLEVIEW *tv, const string_table *st,
2517 const BYTE *rawdata, UINT bytes_per_strref )
2519 UINT i, val, ofs = 0;
2521 MSICOLUMNINFO *columns = tv->columns;
2524 mask = rawdata[0] | (rawdata[1] << 8);
2527 rec = MSI_CreateRecord( tv->num_cols );
2532 for( i=0; i<tv->num_cols; i++ )
2534 if ( (mask&1) && (i>=(mask>>8)) )
2536 /* all keys must be present */
2537 if ( (~mask&1) && (~columns[i].type & MSITYPE_KEY) && ((1<<i) & ~mask) )
2540 if( MSITYPE_IS_BINARY(tv->columns[i].type) )
2543 IStream *stm = NULL;
2546 ofs += bytes_per_column( tv->db, &columns[i], bytes_per_strref );
2548 r = msi_record_encoded_stream_name( tv, rec, &encname );
2549 if ( r != ERROR_SUCCESS )
2552 r = IStorage_OpenStream( stg, encname, NULL,
2553 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stm );
2554 msi_free( encname );
2555 if ( r != ERROR_SUCCESS )
2558 MSI_RecordSetStream( rec, i+1, stm );
2559 TRACE(" field %d [%s]\n", i+1, debugstr_w(encname));
2561 else if( columns[i].type & MSITYPE_STRING )
2565 val = read_raw_int(rawdata, ofs, bytes_per_strref);
2566 sval = msi_string_lookup_id( st, val );
2567 MSI_RecordSetStringW( rec, i+1, sval );
2568 TRACE(" field %d [%s]\n", i+1, debugstr_w(sval));
2569 ofs += bytes_per_strref;
2573 UINT n = bytes_per_column( tv->db, &columns[i], bytes_per_strref );
2577 val = read_raw_int(rawdata, ofs, n);
2579 MSI_RecordSetInteger( rec, i+1, val-0x8000 );
2580 TRACE(" field %d [0x%04x]\n", i+1, val );
2583 val = read_raw_int(rawdata, ofs, n);
2585 MSI_RecordSetInteger( rec, i+1, val^0x80000000 );
2586 TRACE(" field %d [0x%08x]\n", i+1, val );
2589 ERR("oops - unknown column width %d\n", n);
2598 static void dump_record( MSIRECORD *rec )
2602 n = MSI_RecordGetFieldCount( rec );
2603 for( i=1; i<=n; i++ )
2607 if( MSI_RecordIsNull( rec, i ) )
2608 TRACE("row -> []\n");
2609 else if( (sval = MSI_RecordGetString( rec, i )) )
2610 TRACE("row -> [%s]\n", debugstr_w(sval));
2612 TRACE("row -> [0x%08x]\n", MSI_RecordGetInteger( rec, i ) );
2616 static void dump_table( const string_table *st, const USHORT *rawdata, UINT rawsize )
2621 for( i=0; i<(rawsize/2); i++ )
2623 sval = msi_string_lookup_id( st, rawdata[i] );
2624 MESSAGE(" %04x %s\n", rawdata[i], debugstr_w(sval) );
2628 static UINT* msi_record_to_row( const MSITABLEVIEW *tv, MSIRECORD *rec )
2633 data = msi_alloc( tv->num_cols *sizeof (UINT) );
2634 for( i=0; i<tv->num_cols; i++ )
2638 if ( ~tv->columns[i].type & MSITYPE_KEY )
2641 /* turn the transform column value into a row value */
2642 if ( ( tv->columns[i].type & MSITYPE_STRING ) &&
2643 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
2645 str = MSI_RecordGetString( rec, i+1 );
2648 r = msi_string2idW( tv->db->strings, str, &data[i] );
2650 /* if there's no matching string in the string table,
2651 these keys can't match any record, so fail now. */
2652 if (r != ERROR_SUCCESS)
2662 data[i] = MSI_RecordGetInteger( rec, i+1 );
2664 if (data[i] == MSI_NULL_INTEGER)
2666 else if ((tv->columns[i].type&0xff) == 2)
2669 data[i] += 0x80000000;
2675 static UINT msi_row_matches( MSITABLEVIEW *tv, UINT row, const UINT *data, UINT *column )
2677 UINT i, r, x, ret = ERROR_FUNCTION_FAILED;
2679 for( i=0; i<tv->num_cols; i++ )
2681 if ( ~tv->columns[i].type & MSITYPE_KEY )
2684 /* turn the transform column value into a row value */
2685 r = TABLE_fetch_int( &tv->view, row, i+1, &x );
2686 if ( r != ERROR_SUCCESS )
2688 ERR("TABLE_fetch_int shouldn't fail here\n");
2692 /* if this key matches, move to the next column */
2695 ret = ERROR_FUNCTION_FAILED;
2698 if (column) *column = i;
2699 ret = ERROR_SUCCESS;
2704 static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row, UINT *column )
2706 UINT i, r = ERROR_FUNCTION_FAILED, *data;
2708 data = msi_record_to_row( tv, rec );
2711 for( i = 0; i < tv->table->row_count; i++ )
2713 r = msi_row_matches( tv, i, data, column );
2714 if( r == ERROR_SUCCESS )
2730 static UINT msi_table_load_transform( MSIDATABASE *db, IStorage *stg,
2731 string_table *st, TRANSFORMDATA *transform,
2732 UINT bytes_per_strref )
2735 BYTE *rawdata = NULL;
2736 MSITABLEVIEW *tv = NULL;
2737 UINT r, n, sz, i, mask;
2738 MSIRECORD *rec = NULL;
2744 return ERROR_SUCCESS;
2746 name = transform->name;
2749 TRACE("%p %p %p %s\n", db, stg, st, debugstr_w(name) );
2751 /* read the transform data */
2752 read_stream_data( stg, name, TRUE, &rawdata, &rawsize );
2755 TRACE("table %s empty\n", debugstr_w(name) );
2756 return ERROR_INVALID_TABLE;
2759 /* create a table view */
2760 r = TABLE_CreateView( db, name, (MSIVIEW**) &tv );
2761 if( r != ERROR_SUCCESS )
2764 r = tv->view.ops->execute( &tv->view, NULL );
2765 if( r != ERROR_SUCCESS )
2768 TRACE("name = %s columns = %u row_size = %u raw size = %u\n",
2769 debugstr_w(name), tv->num_cols, tv->row_size, rawsize );
2771 /* interpret the data */
2773 for( n=0; n < rawsize; )
2775 mask = rawdata[n] | (rawdata[n+1] << 8);
2780 * if the low bit is set, columns are continuous and
2781 * the number of columns is specified in the high byte
2784 for( i=0; i<tv->num_cols; i++ )
2786 if( (tv->columns[i].type & MSITYPE_STRING) &&
2787 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
2788 sz += bytes_per_strref;
2790 sz += bytes_per_column( tv->db, &tv->columns[i], bytes_per_strref );
2796 * If the low bit is not set, mask is a bitmask.
2797 * Excepting for key fields, which are always present,
2798 * each bit indicates that a field is present in the transform record.
2800 * mask == 0 is a special case ... only the keys will be present
2801 * and it means that this row should be deleted.
2804 for( i=0; i<tv->num_cols; i++ )
2806 if( (tv->columns[i].type & MSITYPE_KEY) || ((1<<i)&mask))
2808 if( (tv->columns[i].type & MSITYPE_STRING) &&
2809 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
2810 sz += bytes_per_strref;
2812 sz += bytes_per_column( tv->db, &tv->columns[i], bytes_per_strref );
2817 /* check we didn't run of the end of the table */
2818 if ( (n+sz) > rawsize )
2821 dump_table( st, (USHORT *)rawdata, rawsize );
2825 rec = msi_get_transform_record( tv, st, stg, &rawdata[n], bytes_per_strref );
2830 UINT number = MSI_NULL_INTEGER;
2833 if (!strcmpW( name, szColumns ))
2835 MSI_RecordGetStringW( rec, 1, table, &sz );
2836 number = MSI_RecordGetInteger( rec, 2 );
2839 * Native msi seems writes nul into the Number (2nd) column of
2840 * the _Columns table, only when the columns are from a new table
2842 if ( number == MSI_NULL_INTEGER )
2844 /* reset the column number on a new table */
2845 if (strcmpW( coltable, table ))
2848 lstrcpyW( coltable, table );
2851 /* fix nul column numbers */
2852 MSI_RecordSetInteger( rec, 2, ++colcol );
2856 if (TRACE_ON(msidb)) dump_record( rec );
2858 r = msi_table_find_row( tv, rec, &row, NULL );
2859 if (r == ERROR_SUCCESS)
2863 TRACE("deleting row [%d]:\n", row);
2864 r = TABLE_delete_row( &tv->view, row );
2865 if (r != ERROR_SUCCESS)
2866 WARN("failed to delete row %u\n", r);
2870 TRACE("modifying full row [%d]:\n", row);
2871 r = TABLE_set_row( &tv->view, row, rec, (1 << tv->num_cols) - 1 );
2872 if (r != ERROR_SUCCESS)
2873 WARN("failed to modify row %u\n", r);
2877 TRACE("modifying masked row [%d]:\n", row);
2878 r = TABLE_set_row( &tv->view, row, rec, mask );
2879 if (r != ERROR_SUCCESS)
2880 WARN("failed to modify row %u\n", r);
2885 TRACE("inserting row\n");
2886 r = TABLE_insert_row( &tv->view, rec, -1, FALSE );
2887 if (r != ERROR_SUCCESS)
2888 WARN("failed to insert row %u\n", r);
2891 if (number != MSI_NULL_INTEGER && !strcmpW( name, szColumns ))
2892 msi_update_table_columns( db, table );
2894 msiobj_release( &rec->hdr );
2901 /* no need to free the table, it's associated with the database */
2902 msi_free( rawdata );
2904 tv->view.ops->delete( &tv->view );
2906 return ERROR_SUCCESS;
2910 * msi_table_apply_transform
2912 * Enumerate the table transforms in a transform storage and apply each one.
2914 UINT msi_table_apply_transform( MSIDATABASE *db, IStorage *stg )
2916 struct list transforms;
2917 IEnumSTATSTG *stgenum = NULL;
2918 TRANSFORMDATA *transform;
2919 TRANSFORMDATA *tables = NULL, *columns = NULL;
2922 string_table *strings;
2923 UINT ret = ERROR_FUNCTION_FAILED;
2924 UINT bytes_per_strref;
2926 TRACE("%p %p\n", db, stg );
2928 strings = msi_load_string_table( stg, &bytes_per_strref );
2932 r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
2936 list_init(&transforms);
2940 MSITABLEVIEW *tv = NULL;
2944 r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
2945 if ( FAILED( r ) || !count )
2948 decode_streamname( stat.pwcsName, name );
2949 CoTaskMemFree( stat.pwcsName );
2950 if ( name[0] != 0x4840 )
2953 if ( !strcmpW( name+1, szStringPool ) ||
2954 !strcmpW( name+1, szStringData ) )
2957 transform = msi_alloc_zero( sizeof(TRANSFORMDATA) );
2961 list_add_tail( &transforms, &transform->entry );
2963 transform->name = strdupW( name + 1 );
2965 if ( !strcmpW( transform->name, szTables ) )
2967 else if (!strcmpW( transform->name, szColumns ) )
2968 columns = transform;
2970 TRACE("transform contains stream %s\n", debugstr_w(name));
2972 /* load the table */
2973 r = TABLE_CreateView( db, transform->name, (MSIVIEW**) &tv );
2974 if( r != ERROR_SUCCESS )
2977 r = tv->view.ops->execute( &tv->view, NULL );
2978 if( r != ERROR_SUCCESS )
2980 tv->view.ops->delete( &tv->view );
2984 tv->view.ops->delete( &tv->view );
2988 * Apply _Tables and _Columns transforms first so that
2989 * the table metadata is correct, and empty tables exist.
2991 ret = msi_table_load_transform( db, stg, strings, tables, bytes_per_strref );
2992 if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
2995 ret = msi_table_load_transform( db, stg, strings, columns, bytes_per_strref );
2996 if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
2999 ret = ERROR_SUCCESS;
3001 while ( !list_empty( &transforms ) )
3003 transform = LIST_ENTRY( list_head( &transforms ), TRANSFORMDATA, entry );
3005 if ( strcmpW( transform->name, szColumns ) &&
3006 strcmpW( transform->name, szTables ) &&
3007 ret == ERROR_SUCCESS )
3009 ret = msi_table_load_transform( db, stg, strings, transform, bytes_per_strref );
3012 list_remove( &transform->entry );
3013 msi_free( transform->name );
3014 msi_free( transform );
3017 if ( ret == ERROR_SUCCESS )
3018 append_storage_to_db( db, stg );
3022 IEnumSTATSTG_Release( stgenum );
3024 msi_destroy_stringtable( strings );