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
62 MSICOLUMNHASHENTRY
**hash_table
;
65 typedef struct tagMSIORDERINFO
76 BYTE
**nonpersistent_data
;
77 UINT nonpersistent_row_count
;
79 MSICOLUMNINFO
*colinfo
;
86 typedef struct tagMSITRANSFORM
{
91 static const WCHAR szStringData
[] = {
92 '_','S','t','r','i','n','g','D','a','t','a',0 };
93 static const WCHAR szStringPool
[] = {
94 '_','S','t','r','i','n','g','P','o','o','l',0 };
96 /* information for default tables */
97 static WCHAR szTables
[] = { '_','T','a','b','l','e','s',0 };
98 static WCHAR szTable
[] = { 'T','a','b','l','e',0 };
99 static WCHAR szName
[] = { 'N','a','m','e',0 };
100 static WCHAR szColumns
[] = { '_','C','o','l','u','m','n','s',0 };
101 static WCHAR szNumber
[] = { 'N','u','m','b','e','r',0 };
102 static WCHAR szType
[] = { 'T','y','p','e',0 };
104 static const MSICOLUMNINFO _Columns_cols
[4] = {
105 { szColumns
, 1, szTable
, MSITYPE_VALID
| MSITYPE_STRING
| MSITYPE_KEY
| 64, 0, 0, NULL
},
106 { szColumns
, 2, szNumber
, MSITYPE_VALID
| MSITYPE_KEY
| 2, 2, 0, NULL
},
107 { szColumns
, 3, szName
, MSITYPE_VALID
| MSITYPE_STRING
| 64, 4, 0, NULL
},
108 { szColumns
, 4, szType
, MSITYPE_VALID
| 2, 6, 0, NULL
},
111 static const MSICOLUMNINFO _Tables_cols
[1] = {
112 { szTables
, 1, szName
, MSITYPE_VALID
| MSITYPE_STRING
| MSITYPE_KEY
| 64, 0, 0, NULL
},
115 #define MAX_STREAM_NAME 0x1f
117 static UINT
table_get_column_info( MSIDATABASE
*db
, LPCWSTR name
,
118 MSICOLUMNINFO
**pcols
, UINT
*pcount
);
119 static void table_calc_column_offsets( MSIDATABASE
*db
, MSICOLUMNINFO
*colinfo
,
121 static UINT
get_tablecolumns( MSIDATABASE
*db
,
122 LPCWSTR szTableName
, MSICOLUMNINFO
*colinfo
, UINT
*sz
);
123 static void msi_free_colinfo( MSICOLUMNINFO
*colinfo
, UINT count
);
125 static inline UINT
bytes_per_column( MSIDATABASE
*db
, const MSICOLUMNINFO
*col
)
127 if( MSITYPE_IS_BINARY(col
->type
) )
129 if( col
->type
& MSITYPE_STRING
)
130 return db
->bytes_per_strref
;
131 if( (col
->type
& 0xff) > 4 )
132 ERR("Invalid column size!\n");
133 return col
->type
& 0xff;
136 static int utf2mime(int x
)
138 if( (x
>='0') && (x
<='9') )
140 if( (x
>='A') && (x
<='Z') )
142 if( (x
>='a') && (x
<='z') )
151 LPWSTR
encode_streamname(BOOL bTable
, LPCWSTR in
)
153 DWORD count
= MAX_STREAM_NAME
;
158 count
= lstrlenW( in
)+2;
159 out
= msi_alloc( count
*sizeof(WCHAR
) );
175 if( ( ch
< 0x80 ) && ( utf2mime(ch
) >= 0 ) )
177 ch
= utf2mime(ch
) + 0x4800;
179 if( next
&& (next
<0x80) )
181 next
= utf2mime(next
);
192 ERR("Failed to encode stream name (%s)\n",debugstr_w(in
));
197 static int mime2utf(int x
)
204 return x
- 10 - 26 + 'a';
205 if( x
== (10+26+26) )
210 BOOL
decode_streamname(LPCWSTR in
, LPWSTR out
)
215 while ( (ch
= *in
++) )
217 if( (ch
>= 0x3800 ) && (ch
< 0x4840 ) )
220 ch
= mime2utf(ch
-0x4800);
224 *out
++ = mime2utf(ch
&0x3f);
226 ch
= mime2utf((ch
>>6)&0x3f);
236 void enum_stream_names( IStorage
*stg
)
238 IEnumSTATSTG
*stgenum
= NULL
;
244 r
= IStorage_EnumElements( stg
, 0, NULL
, 0, &stgenum
);
252 r
= IEnumSTATSTG_Next( stgenum
, 1, &stat
, &count
);
253 if( FAILED( r
) || !count
)
255 decode_streamname( stat
.pwcsName
, name
);
256 TRACE("stream %2d -> %s %s\n", n
,
257 debugstr_w(stat
.pwcsName
), debugstr_w(name
) );
258 CoTaskMemFree( stat
.pwcsName
);
262 IEnumSTATSTG_Release( stgenum
);
265 UINT
read_stream_data( IStorage
*stg
, LPCWSTR stname
, BOOL table
,
266 BYTE
**pdata
, UINT
*psz
)
269 UINT ret
= ERROR_FUNCTION_FAILED
;
276 encname
= encode_streamname(table
, stname
);
278 TRACE("%s -> %s\n",debugstr_w(stname
),debugstr_w(encname
));
280 r
= IStorage_OpenStream(stg
, encname
, NULL
,
281 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, &stm
);
285 WARN("open stream failed r = %08x - empty table?\n", r
);
289 r
= IStream_Stat(stm
, &stat
, STATFLAG_NONAME
);
292 WARN("open stream failed r = %08x!\n", r
);
296 if( stat
.cbSize
.QuadPart
>> 32 )
302 sz
= stat
.cbSize
.QuadPart
;
303 data
= msi_alloc( sz
);
306 WARN("couldn't allocate memory r=%08x!\n", r
);
307 ret
= ERROR_NOT_ENOUGH_MEMORY
;
311 r
= IStream_Read(stm
, data
, sz
, &count
);
312 if( FAILED( r
) || ( count
!= sz
) )
315 WARN("read stream failed r = %08x!\n", r
);
324 IStream_Release( stm
);
329 UINT
db_get_raw_stream( MSIDATABASE
*db
, LPCWSTR stname
, IStream
**stm
)
334 encname
= encode_streamname(FALSE
, stname
);
336 TRACE("%s -> %s\n",debugstr_w(stname
),debugstr_w(encname
));
338 r
= IStorage_OpenStream(db
->storage
, encname
, NULL
,
339 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, stm
);
342 MSITRANSFORM
*transform
;
344 LIST_FOR_EACH_ENTRY( transform
, &db
->transforms
, MSITRANSFORM
, entry
)
346 TRACE("looking for %s in transform storage\n", debugstr_w(stname
) );
347 r
= IStorage_OpenStream( transform
->stg
, encname
, NULL
,
348 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, stm
);
356 return SUCCEEDED(r
) ? ERROR_SUCCESS
: ERROR_FUNCTION_FAILED
;
359 UINT
read_raw_stream_data( MSIDATABASE
*db
, LPCWSTR stname
,
360 USHORT
**pdata
, UINT
*psz
)
363 UINT ret
= ERROR_FUNCTION_FAILED
;
369 r
= db_get_raw_stream( db
, stname
, &stm
);
370 if( r
!= ERROR_SUCCESS
)
372 r
= IStream_Stat(stm
, &stat
, STATFLAG_NONAME
);
375 WARN("open stream failed r = %08x!\n", r
);
379 if( stat
.cbSize
.QuadPart
>> 32 )
385 sz
= stat
.cbSize
.QuadPart
;
386 data
= msi_alloc( sz
);
389 WARN("couldn't allocate memory r=%08x!\n", r
);
390 ret
= ERROR_NOT_ENOUGH_MEMORY
;
394 r
= IStream_Read(stm
, data
, sz
, &count
);
395 if( FAILED( r
) || ( count
!= sz
) )
398 WARN("read stream failed r = %08x!\n", r
);
407 IStream_Release( stm
);
412 UINT
write_stream_data( IStorage
*stg
, LPCWSTR stname
,
413 LPCVOID data
, UINT sz
, BOOL bTable
)
416 UINT ret
= ERROR_FUNCTION_FAILED
;
423 encname
= encode_streamname(bTable
, stname
);
424 r
= IStorage_OpenStream( stg
, encname
, NULL
,
425 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, &stm
);
428 r
= IStorage_CreateStream( stg
, encname
,
429 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, 0, &stm
);
434 WARN("open stream failed r = %08x\n", r
);
439 r
= IStream_SetSize( stm
, size
);
442 WARN("Failed to SetSize\n");
447 r
= IStream_Seek( stm
, pos
, STREAM_SEEK_SET
, NULL
);
450 WARN("Failed to Seek\n");
456 r
= IStream_Write(stm
, data
, sz
, &count
);
457 if( FAILED( r
) || ( count
!= sz
) )
459 WARN("Failed to Write\n");
467 IStream_Release( stm
);
472 static void free_table( MSITABLE
*table
)
475 for( i
=0; i
<table
->row_count
; i
++ )
476 msi_free( table
->data
[i
] );
477 msi_free( table
->data
);
478 for( i
=0; i
<table
->nonpersistent_row_count
; i
++ )
479 msi_free( table
->nonpersistent_data
[i
] );
480 msi_free( table
->nonpersistent_data
);
481 msi_free_colinfo( table
->colinfo
, table
->col_count
);
482 msi_free( table
->colinfo
);
486 static UINT
msi_table_get_row_size( MSIDATABASE
*db
,const MSICOLUMNINFO
*cols
,
489 const MSICOLUMNINFO
*last_col
= &cols
[count
-1];
492 return last_col
->offset
+ bytes_per_column( db
, last_col
);
495 /* add this table to the list of cached tables in the database */
496 static UINT
read_table_from_storage( MSIDATABASE
*db
, MSITABLE
*t
, IStorage
*stg
)
498 BYTE
*rawdata
= NULL
;
499 UINT rawsize
= 0, i
, j
, row_size
= 0;
501 TRACE("%s\n",debugstr_w(t
->name
));
503 row_size
= msi_table_get_row_size( db
, t
->colinfo
, t
->col_count
);
505 /* if we can't read the table, just assume that it's empty */
506 read_stream_data( stg
, t
->name
, TRUE
, &rawdata
, &rawsize
);
508 return ERROR_SUCCESS
;
510 TRACE("Read %d bytes\n", rawsize
);
512 if( rawsize
% row_size
)
514 WARN("Table size is invalid %d/%d\n", rawsize
, row_size
);
518 t
->row_count
= rawsize
/ row_size
;
519 t
->data
= msi_alloc_zero( t
->row_count
* sizeof (USHORT
*) );
523 /* transpose all the data */
524 TRACE("Transposing data from %d rows\n", t
->row_count
);
525 for( i
=0; i
<t
->row_count
; i
++ )
527 t
->data
[i
] = msi_alloc( row_size
);
531 for( j
=0; j
<t
->col_count
; j
++ )
533 UINT ofs
= t
->colinfo
[j
].offset
;
534 UINT n
= bytes_per_column( db
, &t
->colinfo
[j
] );
537 if ( n
!= 2 && n
!= 3 && n
!= 4 )
539 ERR("oops - unknown column width %d\n", n
);
543 for ( k
= 0; k
< n
; k
++ )
544 t
->data
[i
][ofs
+ k
] = rawdata
[ofs
*t
->row_count
+ i
* n
+ k
];
549 return ERROR_SUCCESS
;
552 return ERROR_FUNCTION_FAILED
;
555 void free_cached_tables( MSIDATABASE
*db
)
557 while( !list_empty( &db
->tables
) )
559 MSITABLE
*t
= LIST_ENTRY( list_head( &db
->tables
), MSITABLE
, entry
);
561 list_remove( &t
->entry
);
566 static MSITABLE
*find_cached_table( MSIDATABASE
*db
, LPCWSTR name
)
570 LIST_FOR_EACH_ENTRY( t
, &db
->tables
, MSITABLE
, entry
)
571 if( !lstrcmpW( name
, t
->name
) )
577 static UINT
table_get_column_info( MSIDATABASE
*db
, LPCWSTR name
, MSICOLUMNINFO
**pcols
, UINT
*pcount
)
579 UINT r
, column_count
= 0;
580 MSICOLUMNINFO
*columns
;
582 /* get the number of columns in this table */
584 r
= get_tablecolumns( db
, name
, NULL
, &column_count
);
585 if( r
!= ERROR_SUCCESS
)
588 /* if there's no columns, there's no table */
589 if( column_count
== 0 )
590 return ERROR_INVALID_PARAMETER
;
592 TRACE("Table %s found\n", debugstr_w(name
) );
594 columns
= msi_alloc( column_count
*sizeof (MSICOLUMNINFO
) );
596 return ERROR_FUNCTION_FAILED
;
598 r
= get_tablecolumns( db
, name
, columns
, &column_count
);
599 if( r
!= ERROR_SUCCESS
)
602 return ERROR_FUNCTION_FAILED
;
606 *pcount
= column_count
;
611 UINT
msi_create_table( MSIDATABASE
*db
, LPCWSTR name
, column_info
*col_info
,
612 BOOL persistent
, MSITABLE
**table_ret
)
616 MSIRECORD
*rec
= NULL
;
621 /* only add tables that don't exist already */
622 if( TABLE_Exists(db
, name
) )
624 WARN("table %s exists\n", debugstr_w(name
));
625 return ERROR_BAD_QUERY_SYNTAX
;
628 table
= msi_alloc( sizeof (MSITABLE
) + lstrlenW(name
)*sizeof (WCHAR
) );
630 return ERROR_FUNCTION_FAILED
;
632 table
->ref_count
= 1;
633 table
->row_count
= 0;
635 table
->nonpersistent_row_count
= 0;
636 table
->nonpersistent_data
= NULL
;
637 table
->colinfo
= NULL
;
638 table
->col_count
= 0;
639 table
->persistent
= persistent
;
640 lstrcpyW( table
->name
, name
);
642 for( col
= col_info
; col
; col
= col
->next
)
645 table
->colinfo
= msi_alloc( table
->col_count
* sizeof(MSICOLUMNINFO
) );
649 return ERROR_FUNCTION_FAILED
;
652 for( i
= 0, col
= col_info
; col
; i
++, col
= col
->next
)
654 table
->colinfo
[ i
].tablename
= strdupW( col
->table
);
655 table
->colinfo
[ i
].number
= i
+ 1;
656 table
->colinfo
[ i
].colname
= strdupW( col
->column
);
657 table
->colinfo
[ i
].type
= col
->type
;
658 table
->colinfo
[ i
].offset
= 0;
659 table
->colinfo
[ i
].ref_count
= 0;
660 table
->colinfo
[ i
].hash_table
= NULL
;
662 table_calc_column_offsets( db
, table
->colinfo
, table
->col_count
);
664 r
= TABLE_CreateView( db
, szTables
, &tv
);
665 TRACE("CreateView returned %x\n", r
);
672 r
= tv
->ops
->execute( tv
, 0 );
673 TRACE("tv execute returned %x\n", r
);
677 rec
= MSI_CreateRecord( 1 );
681 r
= MSI_RecordSetStringW( rec
, 1, name
);
685 r
= tv
->ops
->insert_row( tv
, rec
, !persistent
);
686 TRACE("insert_row returned %x\n", r
);
690 tv
->ops
->delete( tv
);
693 msiobj_release( &rec
->hdr
);
698 /* add each column to the _Columns table */
699 r
= TABLE_CreateView( db
, szColumns
, &tv
);
703 r
= tv
->ops
->execute( tv
, 0 );
704 TRACE("tv execute returned %x\n", r
);
708 rec
= MSI_CreateRecord( 4 );
712 r
= MSI_RecordSetStringW( rec
, 1, name
);
717 * need to set the table, column number, col name and type
718 * for each column we enter in the table
721 for( col
= col_info
; col
; col
= col
->next
)
723 r
= MSI_RecordSetInteger( rec
, 2, nField
);
727 r
= MSI_RecordSetStringW( rec
, 3, col
->column
);
731 r
= MSI_RecordSetInteger( rec
, 4, col
->type
);
735 r
= tv
->ops
->insert_row( tv
, rec
, FALSE
);
747 msiobj_release( &rec
->hdr
);
748 /* FIXME: remove values from the string table on error */
750 tv
->ops
->delete( tv
);
752 if (r
== ERROR_SUCCESS
)
754 list_add_head( &db
->tables
, &table
->entry
);
763 static UINT
get_table( MSIDATABASE
*db
, LPCWSTR name
, MSITABLE
**table_ret
)
768 /* first, see if the table is cached */
769 table
= find_cached_table( db
, name
);
773 return ERROR_SUCCESS
;
776 /* nonexistent tables should be interpreted as empty tables */
777 table
= msi_alloc( sizeof (MSITABLE
) + lstrlenW(name
)*sizeof (WCHAR
) );
779 return ERROR_FUNCTION_FAILED
;
781 table
->row_count
= 0;
783 table
->nonpersistent_row_count
= 0;
784 table
->nonpersistent_data
= NULL
;
785 table
->colinfo
= NULL
;
786 table
->col_count
= 0;
787 table
->persistent
= TRUE
;
788 lstrcpyW( table
->name
, name
);
790 r
= table_get_column_info( db
, name
, &table
->colinfo
, &table
->col_count
);
791 if (r
!= ERROR_SUCCESS
)
793 free_table ( table
);
797 r
= read_table_from_storage( db
, table
, db
->storage
);
798 if( r
!= ERROR_SUCCESS
)
804 list_add_head( &db
->tables
, &table
->entry
);
806 return ERROR_SUCCESS
;
809 static UINT
save_table( MSIDATABASE
*db
, const MSITABLE
*t
)
811 BYTE
*rawdata
= NULL
, *p
;
812 UINT rawsize
, r
, i
, j
, row_size
;
814 /* Nothing to do for non-persistent tables */
816 return ERROR_SUCCESS
;
818 TRACE("Saving %s\n", debugstr_w( t
->name
) );
820 row_size
= msi_table_get_row_size( db
, t
->colinfo
, t
->col_count
);
822 rawsize
= t
->row_count
* row_size
;
823 rawdata
= msi_alloc_zero( rawsize
);
826 r
= ERROR_NOT_ENOUGH_MEMORY
;
831 for( i
=0; i
<t
->col_count
; i
++ )
833 for( j
=0; j
<t
->row_count
; j
++ )
835 UINT offset
= t
->colinfo
[i
].offset
;
837 *p
++ = t
->data
[j
][offset
];
838 *p
++ = t
->data
[j
][offset
+ 1];
839 if( 4 == bytes_per_column( db
, &t
->colinfo
[i
] ) )
841 *p
++ = t
->data
[j
][offset
+ 2];
842 *p
++ = t
->data
[j
][offset
+ 3];
847 TRACE("writing %d bytes\n", rawsize
);
848 r
= write_stream_data( db
->storage
, t
->name
, rawdata
, rawsize
, TRUE
);
856 static void table_calc_column_offsets( MSIDATABASE
*db
, MSICOLUMNINFO
*colinfo
,
861 for( i
=0; colinfo
&& (i
<count
); i
++ )
863 assert( (i
+1) == colinfo
[ i
].number
);
865 colinfo
[i
].offset
= colinfo
[ i
- 1 ].offset
866 + bytes_per_column( db
, &colinfo
[ i
- 1 ] );
868 colinfo
[i
].offset
= 0;
869 TRACE("column %d is [%s] with type %08x ofs %d\n",
870 colinfo
[i
].number
, debugstr_w(colinfo
[i
].colname
),
871 colinfo
[i
].type
, colinfo
[i
].offset
);
875 static UINT
get_defaulttablecolumns( MSIDATABASE
*db
, LPCWSTR name
,
876 MSICOLUMNINFO
*colinfo
, UINT
*sz
)
878 const MSICOLUMNINFO
*p
;
881 TRACE("%s\n", debugstr_w(name
));
883 if (!lstrcmpW( name
, szTables
))
888 else if (!lstrcmpW( name
, szColumns
))
894 return ERROR_FUNCTION_FAILED
;
896 /* duplicate the string data so we can free it in msi_free_colinfo */
899 if (colinfo
&& (i
< *sz
) )
902 colinfo
[i
].tablename
= strdupW( p
[i
].tablename
);
903 colinfo
[i
].colname
= strdupW( p
[i
].colname
);
905 if( colinfo
&& (i
>= *sz
) )
908 table_calc_column_offsets( db
, colinfo
, n
);
910 return ERROR_SUCCESS
;
913 static void msi_free_colinfo( MSICOLUMNINFO
*colinfo
, UINT count
)
917 for( i
=0; i
<count
; i
++ )
919 msi_free( colinfo
[i
].tablename
);
920 msi_free( colinfo
[i
].colname
);
921 msi_free( colinfo
[i
].hash_table
);
925 static LPWSTR
msi_makestring( const MSIDATABASE
*db
, UINT stringid
)
927 return strdupW(msi_string_lookup_id( db
->strings
, stringid
));
930 static UINT
read_table_int(BYTE
*const *data
, UINT row
, UINT col
, UINT bytes
)
934 for (i
= 0; i
< bytes
; i
++)
935 ret
+= (data
[row
][col
+ i
] << i
* 8);
940 static UINT
get_tablecolumns( MSIDATABASE
*db
,
941 LPCWSTR szTableName
, MSICOLUMNINFO
*colinfo
, UINT
*sz
)
943 UINT r
, i
, n
=0, table_id
, count
, maxcount
= *sz
;
944 MSITABLE
*table
= NULL
;
946 TRACE("%s\n", debugstr_w(szTableName
));
948 /* first check if there is a default table with that name */
949 r
= get_defaulttablecolumns( db
, szTableName
, colinfo
, sz
);
950 if( ( r
== ERROR_SUCCESS
) && *sz
)
953 r
= get_table( db
, szColumns
, &table
);
954 if( r
!= ERROR_SUCCESS
)
956 ERR("couldn't load _Columns table\n");
957 return ERROR_FUNCTION_FAILED
;
960 /* convert table and column names to IDs from the string table */
961 r
= msi_string2idW( db
->strings
, szTableName
, &table_id
);
962 if( r
!= ERROR_SUCCESS
)
964 WARN("Couldn't find id for %s\n", debugstr_w(szTableName
));
968 TRACE("Table id is %d, row count is %d\n", table_id
, table
->row_count
);
970 /* Note: _Columns table doesn't have non-persistent data */
972 /* if maxcount is non-zero, assume it's exactly right for this table */
973 memset( colinfo
, 0, maxcount
*sizeof(*colinfo
) );
974 count
= table
->row_count
;
975 for( i
=0; i
<count
; i
++ )
977 if( read_table_int(table
->data
, i
, 0, db
->bytes_per_strref
) != table_id
)
981 UINT id
= read_table_int(table
->data
, i
, table
->colinfo
[2].offset
, db
->bytes_per_strref
);
982 UINT col
= read_table_int(table
->data
, i
, table
->colinfo
[1].offset
, sizeof(USHORT
)) - (1<<15);
984 /* check the column number is in range */
985 if (col
<1 || col
>maxcount
)
987 ERR("column %d out of range\n", col
);
991 /* check if this column was already set */
992 if (colinfo
[ col
- 1 ].number
)
994 ERR("duplicate column %d\n", col
);
998 colinfo
[ col
- 1 ].tablename
= msi_makestring( db
, table_id
);
999 colinfo
[ col
- 1 ].number
= col
;
1000 colinfo
[ col
- 1 ].colname
= msi_makestring( db
, id
);
1001 colinfo
[ col
- 1 ].type
= read_table_int(table
->data
, i
,
1002 table
->colinfo
[3].offset
,
1003 sizeof(USHORT
)) - (1<<15);
1004 colinfo
[ col
- 1 ].offset
= 0;
1005 colinfo
[ col
- 1 ].ref_count
= 0;
1006 colinfo
[ col
- 1 ].hash_table
= NULL
;
1011 TRACE("%s has %d columns\n", debugstr_w(szTableName
), n
);
1013 if (colinfo
&& n
!= maxcount
)
1015 ERR("missing column in table %s\n", debugstr_w(szTableName
));
1016 msi_free_colinfo(colinfo
, maxcount
);
1017 return ERROR_FUNCTION_FAILED
;
1020 table_calc_column_offsets( db
, colinfo
, n
);
1023 return ERROR_SUCCESS
;
1026 static void msi_update_table_columns( MSIDATABASE
*db
, LPCWSTR name
)
1029 UINT size
, offset
, old_count
;
1032 table
= find_cached_table( db
, name
);
1033 old_count
= table
->col_count
;
1034 msi_free( table
->colinfo
);
1035 table_get_column_info( db
, name
, &table
->colinfo
, &table
->col_count
);
1037 size
= msi_table_get_row_size( db
, table
->colinfo
, table
->col_count
);
1038 offset
= table
->colinfo
[table
->col_count
- 1].offset
;
1040 for ( n
= 0; n
< table
->row_count
; n
++ )
1042 table
->data
[n
] = msi_realloc( table
->data
[n
], size
);
1043 if (old_count
< table
->col_count
)
1044 memset( &table
->data
[n
][offset
], 0, size
- offset
);
1048 /* try to find the table name in the _Tables table */
1049 BOOL
TABLE_Exists( MSIDATABASE
*db
, LPCWSTR name
)
1051 UINT r
, table_id
= 0, i
, count
;
1052 MSITABLE
*table
= NULL
;
1054 if( !lstrcmpW( name
, szTables
) )
1056 if( !lstrcmpW( name
, szColumns
) )
1059 r
= msi_string2idW( db
->strings
, name
, &table_id
);
1060 if( r
!= ERROR_SUCCESS
)
1062 TRACE("Couldn't find id for %s\n", debugstr_w(name
));
1066 r
= get_table( db
, szTables
, &table
);
1067 if( r
!= ERROR_SUCCESS
)
1069 ERR("table %s not available\n", debugstr_w(szTables
));
1073 count
= table
->row_count
;
1074 for( i
=0; i
<count
; i
++ )
1075 if( table
->data
[ i
][ 0 ] == table_id
)
1078 count
= table
->nonpersistent_row_count
;
1079 for( i
=0; i
<count
; i
++ )
1080 if( table
->nonpersistent_data
[ i
][ 0 ] == table_id
)
1086 /* below is the query interface to a table */
1088 typedef struct tagMSITABLEVIEW
1093 MSICOLUMNINFO
*columns
;
1094 MSIORDERINFO
*order
;
1100 static UINT
TABLE_fetch_int( struct tagMSIVIEW
*view
, UINT row
, UINT col
, UINT
*val
)
1102 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1107 return ERROR_INVALID_PARAMETER
;
1109 if( (col
==0) || (col
>tv
->num_cols
) )
1110 return ERROR_INVALID_PARAMETER
;
1112 /* how many rows are there ? */
1113 if( row
>= tv
->table
->row_count
+ tv
->table
->nonpersistent_row_count
)
1114 return ERROR_NO_MORE_ITEMS
;
1116 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1118 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1119 ERR("%p %p\n", tv
, tv
->columns
);
1120 return ERROR_FUNCTION_FAILED
;
1124 row
= tv
->order
->reorder
[row
];
1126 if (row
>= tv
->table
->row_count
)
1128 row
-= tv
->table
->row_count
;
1129 data
= tv
->table
->nonpersistent_data
;
1132 data
= tv
->table
->data
;
1134 n
= bytes_per_column( tv
->db
, &tv
->columns
[col
-1] );
1135 if (n
!= 2 && n
!= 3 && n
!= 4)
1137 ERR("oops! what is %d bytes per column?\n", n
);
1138 return ERROR_FUNCTION_FAILED
;
1141 offset
= tv
->columns
[col
-1].offset
;
1142 *val
= read_table_int(data
, row
, offset
, n
);
1144 /* TRACE("Data [%d][%d] = %d\n", row, col, *val ); */
1146 return ERROR_SUCCESS
;
1150 * We need a special case for streams, as we need to reference column with
1151 * the name of the stream in the same table, and the table name
1152 * which may not be available at higher levels of the query
1154 static UINT
TABLE_fetch_stream( struct tagMSIVIEW
*view
, UINT row
, UINT col
, IStream
**stm
)
1156 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1157 UINT ival
= 0, refcol
= 0, r
;
1161 static const WCHAR szDot
[] = { '.', 0 };
1164 if( !view
->ops
->fetch_int
)
1165 return ERROR_INVALID_PARAMETER
;
1168 * The column marked with the type stream data seems to have a single number
1169 * which references the column containing the name of the stream data
1171 * Fetch the column to reference first.
1173 r
= view
->ops
->fetch_int( view
, row
, col
, &ival
);
1174 if( r
!= ERROR_SUCCESS
)
1177 /* check the column value is in range */
1178 if (ival
> tv
->num_cols
|| ival
== col
)
1180 ERR("bad column ref (%u) for stream\n", ival
);
1181 return ERROR_FUNCTION_FAILED
;
1184 if ( tv
->columns
[ival
- 1].type
& MSITYPE_STRING
)
1186 /* now get the column with the name of the stream */
1187 r
= view
->ops
->fetch_int( view
, row
, ival
, &refcol
);
1188 if ( r
!= ERROR_SUCCESS
)
1191 /* lookup the string value from the string table */
1192 sval
= msi_string_lookup_id( tv
->db
->strings
, refcol
);
1194 return ERROR_INVALID_PARAMETER
;
1198 static const WCHAR fmt
[] = { '%','d',0 };
1199 sprintfW( number
, fmt
, ival
);
1203 len
= lstrlenW( tv
->name
) + 2 + lstrlenW( sval
);
1204 full_name
= msi_alloc( len
*sizeof(WCHAR
) );
1205 lstrcpyW( full_name
, tv
->name
);
1206 lstrcatW( full_name
, szDot
);
1207 lstrcatW( full_name
, sval
);
1209 r
= db_get_raw_stream( tv
->db
, full_name
, stm
);
1211 ERR("fetching stream %s, error = %d\n",debugstr_w(full_name
), r
);
1212 msi_free( full_name
);
1217 static UINT
TABLE_set_int( MSITABLEVIEW
*tv
, UINT row
, UINT col
, UINT val
)
1223 return ERROR_INVALID_PARAMETER
;
1225 if( (col
==0) || (col
>tv
->num_cols
) )
1226 return ERROR_INVALID_PARAMETER
;
1228 if( row
>= tv
->table
->row_count
+ tv
->table
->nonpersistent_row_count
)
1229 return ERROR_INVALID_PARAMETER
;
1231 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1233 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1234 ERR("%p %p\n", tv
, tv
->columns
);
1235 return ERROR_FUNCTION_FAILED
;
1238 msi_free( tv
->columns
[col
-1].hash_table
);
1239 tv
->columns
[col
-1].hash_table
= NULL
;
1241 if (row
>= tv
->table
->row_count
)
1243 row
-= tv
->table
->row_count
;
1244 data
= tv
->table
->nonpersistent_data
;
1247 data
= tv
->table
->data
;
1249 n
= bytes_per_column( tv
->db
, &tv
->columns
[col
-1] );
1250 if ( n
!= 2 && n
!= 3 && n
!= 4 )
1252 ERR("oops! what is %d bytes per column?\n", n
);
1253 return ERROR_FUNCTION_FAILED
;
1256 offset
= tv
->columns
[col
-1].offset
;
1257 for ( i
= 0; i
< n
; i
++ )
1258 data
[row
][offset
+ i
] = (val
>> i
* 8) & 0xff;
1260 return ERROR_SUCCESS
;
1263 static UINT
TABLE_get_row( struct tagMSIVIEW
*view
, UINT row
, MSIRECORD
**rec
)
1265 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1268 return ERROR_INVALID_PARAMETER
;
1271 row
= tv
->order
->reorder
[row
];
1273 return msi_view_get_row(tv
->db
, view
, row
, rec
);
1276 static UINT
TABLE_set_row( struct tagMSIVIEW
*view
, UINT row
, MSIRECORD
*rec
, UINT mask
)
1278 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1279 UINT i
, val
, r
= ERROR_SUCCESS
;
1282 return ERROR_INVALID_PARAMETER
;
1284 /* test if any of the mask bits are invalid */
1285 if ( mask
>= (1<<tv
->num_cols
) )
1286 return ERROR_INVALID_PARAMETER
;
1288 for ( i
= 0; i
< tv
->num_cols
; i
++ )
1292 /* only update the fields specified in the mask */
1293 if ( !(mask
&(1<<i
)) )
1296 /* if row >= tv->table->row_count then it is a non-persistent row */
1297 persistent
= tv
->table
->persistent
&& (row
< tv
->table
->row_count
);
1298 /* FIXME: should we allow updating keys? */
1301 if ( !MSI_RecordIsNull( rec
, i
+ 1 ) )
1303 if ( MSITYPE_IS_BINARY(tv
->columns
[ i
].type
) )
1305 val
= 1; /* refers to the first key column */
1307 else if ( tv
->columns
[i
].type
& MSITYPE_STRING
)
1309 LPCWSTR sval
= MSI_RecordGetString( rec
, i
+ 1 );
1312 r
= msi_string2idW(tv
->db
->strings
, sval
, &ival
);
1313 if (r
== ERROR_SUCCESS
)
1315 TABLE_fetch_int(&tv
->view
, row
, i
+ 1, &x
);
1320 val
= msi_addstringW( tv
->db
->strings
, 0, sval
, -1, 1,
1321 persistent
? StringPersistent
: StringNonPersistent
);
1323 else if ( 2 == bytes_per_column( tv
->db
, &tv
->columns
[ i
] ) )
1325 val
= 0x8000 + MSI_RecordGetInteger( rec
, i
+ 1 );
1326 if ( val
& 0xffff0000 )
1328 ERR("field %u value %d out of range\n", i
+1, val
- 0x8000 );
1329 return ERROR_FUNCTION_FAILED
;
1334 INT ival
= MSI_RecordGetInteger( rec
, i
+ 1 );
1335 val
= ival
^ 0x80000000;
1339 r
= TABLE_set_int( tv
, row
, i
+1, val
);
1340 if ( r
!= ERROR_SUCCESS
)
1346 static UINT
table_create_new_row( struct tagMSIVIEW
*view
, UINT
*num
, BOOL temporary
)
1348 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1354 TRACE("%p %s\n", view
, temporary
? "TRUE" : "FALSE");
1357 return ERROR_INVALID_PARAMETER
;
1359 row
= msi_alloc_zero( tv
->row_size
);
1361 return ERROR_NOT_ENOUGH_MEMORY
;
1365 row_count
= &tv
->table
->nonpersistent_row_count
;
1366 data_ptr
= &tv
->table
->nonpersistent_data
;
1367 *num
= tv
->table
->row_count
+ tv
->table
->nonpersistent_row_count
;
1371 row_count
= &tv
->table
->row_count
;
1372 data_ptr
= &tv
->table
->data
;
1373 *num
= tv
->table
->row_count
;
1376 sz
= (*row_count
+ 1) * sizeof (BYTE
*);
1378 p
= msi_realloc( *data_ptr
, sz
);
1380 p
= msi_alloc( sz
);
1384 return ERROR_NOT_ENOUGH_MEMORY
;
1388 (*data_ptr
)[*row_count
] = row
;
1391 return ERROR_SUCCESS
;
1394 static UINT
TABLE_execute( struct tagMSIVIEW
*view
, MSIRECORD
*record
)
1396 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1398 TRACE("%p %p\n", tv
, record
);
1400 TRACE("There are %d columns\n", tv
->num_cols
);
1402 return ERROR_SUCCESS
;
1405 static UINT
TABLE_close( struct tagMSIVIEW
*view
)
1407 TRACE("%p\n", view
);
1409 return ERROR_SUCCESS
;
1412 static UINT
TABLE_get_dimensions( struct tagMSIVIEW
*view
, UINT
*rows
, UINT
*cols
)
1414 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1416 TRACE("%p %p %p\n", view
, rows
, cols
);
1419 *cols
= tv
->num_cols
;
1423 return ERROR_INVALID_PARAMETER
;
1424 *rows
= tv
->table
->row_count
+ tv
->table
->nonpersistent_row_count
;
1427 return ERROR_SUCCESS
;
1430 static UINT
TABLE_get_column_info( struct tagMSIVIEW
*view
,
1431 UINT n
, LPWSTR
*name
, UINT
*type
)
1433 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1435 TRACE("%p %d %p %p\n", tv
, n
, name
, type
);
1437 if( ( n
== 0 ) || ( n
> tv
->num_cols
) )
1438 return ERROR_INVALID_PARAMETER
;
1442 *name
= strdupW( tv
->columns
[n
-1].colname
);
1444 return ERROR_FUNCTION_FAILED
;
1447 *type
= tv
->columns
[n
-1].type
;
1449 return ERROR_SUCCESS
;
1452 static UINT
msi_table_find_row( MSITABLEVIEW
*tv
, MSIRECORD
*rec
, UINT
*row
);
1454 static UINT
table_validate_new( MSITABLEVIEW
*tv
, MSIRECORD
*rec
)
1458 /* check there's no null values where they're not allowed */
1459 for( i
= 0; i
< tv
->num_cols
; i
++ )
1461 if ( tv
->columns
[i
].type
& MSITYPE_NULLABLE
)
1464 if ( MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
1465 TRACE("skipping binary column\n");
1466 else if ( tv
->columns
[i
].type
& MSITYPE_STRING
)
1470 str
= MSI_RecordGetString( rec
, i
+1 );
1471 if (str
== NULL
|| str
[0] == 0)
1472 return ERROR_INVALID_DATA
;
1478 n
= MSI_RecordGetInteger( rec
, i
+1 );
1479 if (n
== MSI_NULL_INTEGER
)
1480 return ERROR_INVALID_DATA
;
1484 /* check there's no duplicate keys */
1485 r
= msi_table_find_row( tv
, rec
, &row
);
1486 if (r
== ERROR_SUCCESS
)
1487 return ERROR_FUNCTION_FAILED
;
1489 return ERROR_SUCCESS
;
1492 static UINT
TABLE_insert_row( struct tagMSIVIEW
*view
, MSIRECORD
*rec
, BOOL temporary
)
1494 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1497 TRACE("%p %p %s\n", tv
, rec
, temporary
? "TRUE" : "FALSE" );
1499 /* check that the key is unique - can we find a matching row? */
1500 r
= table_validate_new( tv
, rec
);
1501 if( r
!= ERROR_SUCCESS
)
1502 return ERROR_FUNCTION_FAILED
;
1504 r
= table_create_new_row( view
, &row
, temporary
);
1505 TRACE("insert_row returned %08x\n", r
);
1506 if( r
!= ERROR_SUCCESS
)
1509 return TABLE_set_row( view
, row
, rec
, (1<<tv
->num_cols
) - 1 );
1512 static UINT
TABLE_delete_row( struct tagMSIVIEW
*view
, UINT row
)
1514 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1515 UINT r
, num_rows
, num_cols
, i
;
1518 TRACE("%p %d\n", tv
, row
);
1521 return ERROR_INVALID_PARAMETER
;
1523 r
= TABLE_get_dimensions( view
, &num_rows
, &num_cols
);
1524 if ( r
!= ERROR_SUCCESS
)
1527 if ( row
>= num_rows
)
1528 return ERROR_FUNCTION_FAILED
;
1530 if ( row
< tv
->table
->row_count
)
1532 num_rows
= tv
->table
->row_count
;
1533 tv
->table
->row_count
--;
1534 data
= tv
->table
->data
;
1538 num_rows
= tv
->table
->nonpersistent_row_count
;
1539 row
-= tv
->table
->row_count
;
1540 tv
->table
->nonpersistent_row_count
--;
1541 data
= tv
->table
->nonpersistent_data
;
1544 /* reset the hash tables */
1545 for (i
= 0; i
< tv
->num_cols
; i
++)
1547 msi_free( tv
->columns
[i
].hash_table
);
1548 tv
->columns
[i
].hash_table
= NULL
;
1551 if ( row
== num_rows
- 1 )
1552 return ERROR_SUCCESS
;
1554 for (i
= row
+ 1; i
< num_rows
; i
++)
1555 memcpy(data
[i
- 1], data
[i
], tv
->row_size
);
1557 return ERROR_SUCCESS
;
1560 static UINT
msi_table_update(struct tagMSIVIEW
*view
, MSIRECORD
*rec
, UINT row
)
1562 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1565 /* FIXME: MsiViewFetch should set rec index 0 to some ID that
1566 * sets the fetched record apart from other records
1570 return ERROR_INVALID_PARAMETER
;
1572 r
= msi_table_find_row(tv
, rec
, &new_row
);
1573 if (r
!= ERROR_SUCCESS
)
1575 ERR("can't find row to modify\n");
1576 return ERROR_FUNCTION_FAILED
;
1579 /* the row cannot be changed */
1580 if (row
!= new_row
+ 1)
1581 return ERROR_FUNCTION_FAILED
;
1583 return TABLE_set_row(view
, new_row
, rec
, (1 << tv
->num_cols
) - 1);
1586 static UINT
modify_delete_row( struct tagMSIVIEW
*view
, MSIRECORD
*rec
)
1588 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1591 r
= msi_table_find_row(tv
, rec
, &row
);
1592 if (r
!= ERROR_SUCCESS
)
1595 return TABLE_delete_row(view
, row
);
1598 static UINT
msi_refresh_record( struct tagMSIVIEW
*view
, MSIRECORD
*rec
, UINT row
)
1603 r
= TABLE_get_row(view
, row
- 1, &curr
);
1604 if (r
!= ERROR_SUCCESS
)
1607 count
= MSI_RecordGetFieldCount(rec
);
1608 for (i
= 0; i
< count
; i
++)
1609 MSI_RecordCopyField(curr
, i
+ 1, rec
, i
+ 1);
1611 msiobj_release(&curr
->hdr
);
1612 return ERROR_SUCCESS
;
1615 static UINT
TABLE_modify( struct tagMSIVIEW
*view
, MSIMODIFY eModifyMode
,
1616 MSIRECORD
*rec
, UINT row
)
1618 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1621 TRACE("%p %d %p\n", view
, eModifyMode
, rec
);
1623 switch (eModifyMode
)
1625 case MSIMODIFY_DELETE
:
1626 r
= modify_delete_row( view
, rec
);
1628 case MSIMODIFY_VALIDATE_NEW
:
1629 r
= table_validate_new( tv
, rec
);
1632 case MSIMODIFY_INSERT
:
1633 r
= table_validate_new( tv
, rec
);
1634 if (r
!= ERROR_SUCCESS
)
1636 r
= TABLE_insert_row( view
, rec
, FALSE
);
1639 case MSIMODIFY_INSERT_TEMPORARY
:
1640 r
= table_validate_new( tv
, rec
);
1641 if (r
!= ERROR_SUCCESS
)
1643 r
= TABLE_insert_row( view
, rec
, TRUE
);
1646 case MSIMODIFY_REFRESH
:
1647 r
= msi_refresh_record( view
, rec
, row
);
1650 case MSIMODIFY_UPDATE
:
1651 r
= msi_table_update( view
, rec
, row
);
1654 case MSIMODIFY_ASSIGN
:
1655 case MSIMODIFY_REPLACE
:
1656 case MSIMODIFY_MERGE
:
1657 case MSIMODIFY_VALIDATE
:
1658 case MSIMODIFY_VALIDATE_FIELD
:
1659 case MSIMODIFY_VALIDATE_DELETE
:
1660 FIXME("%p %d %p - mode not implemented\n", view
, eModifyMode
, rec
);
1661 r
= ERROR_CALL_NOT_IMPLEMENTED
;
1665 r
= ERROR_INVALID_DATA
;
1671 static UINT
TABLE_delete( struct tagMSIVIEW
*view
)
1673 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1675 TRACE("%p\n", view
);
1682 msi_free( tv
->order
->reorder
);
1683 msi_free( tv
->order
);
1689 return ERROR_SUCCESS
;
1692 static UINT
TABLE_find_matching_rows( struct tagMSIVIEW
*view
, UINT col
,
1693 UINT val
, UINT
*row
, MSIITERHANDLE
*handle
)
1695 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1696 const MSICOLUMNHASHENTRY
*entry
;
1698 TRACE("%p, %d, %u, %p\n", view
, col
, val
, *handle
);
1701 return ERROR_INVALID_PARAMETER
;
1703 if( (col
==0) || (col
> tv
->num_cols
) )
1704 return ERROR_INVALID_PARAMETER
;
1706 if( !tv
->columns
[col
-1].hash_table
)
1709 UINT num_rows
= tv
->table
->row_count
+ tv
->table
->nonpersistent_row_count
;
1710 MSICOLUMNHASHENTRY
**hash_table
;
1711 MSICOLUMNHASHENTRY
*new_entry
;
1713 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1715 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1716 ERR("%p %p\n", tv
, tv
->columns
);
1717 return ERROR_FUNCTION_FAILED
;
1720 /* allocate contiguous memory for the table and its entries so we
1721 * don't have to do an expensive cleanup */
1722 hash_table
= msi_alloc(MSITABLE_HASH_TABLE_SIZE
* sizeof(MSICOLUMNHASHENTRY
*) +
1723 num_rows
* sizeof(MSICOLUMNHASHENTRY
));
1725 return ERROR_OUTOFMEMORY
;
1727 memset(hash_table
, 0, MSITABLE_HASH_TABLE_SIZE
* sizeof(MSICOLUMNHASHENTRY
*));
1728 tv
->columns
[col
-1].hash_table
= hash_table
;
1730 new_entry
= (MSICOLUMNHASHENTRY
*)(hash_table
+ MSITABLE_HASH_TABLE_SIZE
);
1732 for (i
= 0; i
< num_rows
; i
++, new_entry
++)
1736 if (view
->ops
->fetch_int( view
, i
, col
, &row_value
) != ERROR_SUCCESS
)
1739 new_entry
->next
= NULL
;
1740 new_entry
->value
= row_value
;
1742 if (hash_table
[row_value
% MSITABLE_HASH_TABLE_SIZE
])
1744 MSICOLUMNHASHENTRY
*prev_entry
= hash_table
[row_value
% MSITABLE_HASH_TABLE_SIZE
];
1745 while (prev_entry
->next
)
1746 prev_entry
= prev_entry
->next
;
1747 prev_entry
->next
= new_entry
;
1750 hash_table
[row_value
% MSITABLE_HASH_TABLE_SIZE
] = new_entry
;
1755 entry
= tv
->columns
[col
-1].hash_table
[val
% MSITABLE_HASH_TABLE_SIZE
];
1757 entry
= (*handle
)->next
;
1759 while (entry
&& entry
->value
!= val
)
1760 entry
= entry
->next
;
1764 return ERROR_NO_MORE_ITEMS
;
1768 return ERROR_SUCCESS
;
1771 static UINT
TABLE_add_ref(struct tagMSIVIEW
*view
)
1773 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1776 TRACE("%p %d\n", view
, tv
->table
->ref_count
);
1778 for (i
= 0; i
< tv
->table
->col_count
; i
++)
1780 if (tv
->table
->colinfo
[i
].type
& MSITYPE_TEMPORARY
)
1781 InterlockedIncrement(&tv
->table
->colinfo
[i
].ref_count
);
1784 return InterlockedIncrement(&tv
->table
->ref_count
);
1787 static UINT
TABLE_remove_column(struct tagMSIVIEW
*view
, LPCWSTR table
, UINT number
)
1789 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1790 MSIRECORD
*rec
= NULL
;
1791 MSIVIEW
*columns
= NULL
;
1794 rec
= MSI_CreateRecord(2);
1796 return ERROR_OUTOFMEMORY
;
1798 MSI_RecordSetStringW(rec
, 1, table
);
1799 MSI_RecordSetInteger(rec
, 2, number
);
1801 r
= TABLE_CreateView(tv
->db
, szColumns
, &columns
);
1802 if (r
!= ERROR_SUCCESS
)
1805 r
= msi_table_find_row((MSITABLEVIEW
*)columns
, rec
, &row
);
1806 if (r
!= ERROR_SUCCESS
)
1809 r
= TABLE_delete_row(columns
, row
);
1810 if (r
!= ERROR_SUCCESS
)
1813 msi_update_table_columns(tv
->db
, table
);
1816 msiobj_release(&rec
->hdr
);
1817 if (columns
) columns
->ops
->delete(columns
);
1821 static UINT
TABLE_release(struct tagMSIVIEW
*view
)
1823 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1824 INT ref
= tv
->table
->ref_count
;
1827 TRACE("%p %d\n", view
, ref
);
1829 for (i
= 0; i
< tv
->table
->col_count
; i
++)
1831 if (tv
->table
->colinfo
[i
].type
& MSITYPE_TEMPORARY
)
1833 ref
= InterlockedDecrement(&tv
->table
->colinfo
[i
].ref_count
);
1836 r
= TABLE_remove_column(view
, tv
->table
->colinfo
[i
].tablename
,
1837 tv
->table
->colinfo
[i
].number
);
1838 if (r
!= ERROR_SUCCESS
)
1844 ref
= InterlockedDecrement(&tv
->table
->ref_count
);
1847 if (!tv
->table
->row_count
)
1849 list_remove(&tv
->table
->entry
);
1850 free_table(tv
->table
);
1858 static UINT
TABLE_add_column(struct tagMSIVIEW
*view
, LPCWSTR table
, UINT number
,
1859 LPCWSTR column
, UINT type
, BOOL hold
)
1861 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1866 rec
= MSI_CreateRecord(4);
1868 return ERROR_OUTOFMEMORY
;
1870 MSI_RecordSetStringW(rec
, 1, table
);
1871 MSI_RecordSetInteger(rec
, 2, number
);
1872 MSI_RecordSetStringW(rec
, 3, column
);
1873 MSI_RecordSetInteger(rec
, 4, type
);
1875 r
= TABLE_insert_row(&tv
->view
, rec
, FALSE
);
1876 if (r
!= ERROR_SUCCESS
)
1879 msi_update_table_columns(tv
->db
, table
);
1884 msitable
= find_cached_table(tv
->db
, table
);
1885 for (i
= 0; i
< msitable
->col_count
; i
++)
1887 if (!lstrcmpW(msitable
->colinfo
[i
].colname
, column
))
1889 InterlockedIncrement(&msitable
->colinfo
[i
].ref_count
);
1895 msiobj_release(&rec
->hdr
);
1899 static UINT
order_add_column(struct tagMSIVIEW
*view
, MSIORDERINFO
*order
, LPCWSTR name
)
1903 r
= TABLE_get_dimensions(view
, NULL
, &count
);
1904 if (r
!= ERROR_SUCCESS
)
1907 if (order
->num_cols
>= count
)
1908 return ERROR_FUNCTION_FAILED
;
1910 r
= VIEW_find_column(view
, name
, &n
);
1911 if (r
!= ERROR_SUCCESS
)
1914 order
->cols
[order
->num_cols
] = n
;
1915 TRACE("Ordering by column %s (%d)\n", debugstr_w(name
), n
);
1919 return ERROR_SUCCESS
;
1922 static UINT
order_compare(struct tagMSIVIEW
*view
, MSIORDERINFO
*order
,
1923 UINT a
, UINT b
, UINT
*swap
)
1925 UINT r
, i
, a_val
= 0, b_val
= 0;
1928 for (i
= 0; i
< order
->num_cols
; i
++)
1930 r
= TABLE_fetch_int(view
, a
, order
->cols
[i
], &a_val
);
1931 if (r
!= ERROR_SUCCESS
)
1934 r
= TABLE_fetch_int(view
, b
, order
->cols
[i
], &b_val
);
1935 if (r
!= ERROR_SUCCESS
)
1946 return ERROR_SUCCESS
;
1949 static UINT
order_mergesort(struct tagMSIVIEW
*view
, MSIORDERINFO
*order
,
1950 UINT left
, UINT right
)
1953 UINT swap
= 0, center
= (left
+ right
) / 2;
1954 UINT
*array
= order
->reorder
;
1957 return ERROR_SUCCESS
;
1959 /* sort the left half */
1960 r
= order_mergesort(view
, order
, left
, center
);
1961 if (r
!= ERROR_SUCCESS
)
1964 /* sort the right half */
1965 r
= order_mergesort(view
, order
, center
+ 1, right
);
1966 if (r
!= ERROR_SUCCESS
)
1969 for (i
= left
, j
= center
+ 1; (i
<= center
) && (j
<= right
); i
++)
1971 r
= order_compare(view
, order
, array
[i
], array
[j
], &swap
);
1972 if (r
!= ERROR_SUCCESS
)
1978 memmove(&array
[i
+ 1], &array
[i
], (j
- i
) * sizeof(UINT
));
1985 return ERROR_SUCCESS
;
1988 static UINT
order_verify(struct tagMSIVIEW
*view
, MSIORDERINFO
*order
, UINT num_rows
)
1992 for (i
= 1; i
< num_rows
; i
++)
1994 r
= order_compare(view
, order
, order
->reorder
[i
- 1],
1995 order
->reorder
[i
], &swap
);
1996 if (r
!= ERROR_SUCCESS
)
2002 ERR("Bad order! %d\n", i
);
2003 return ERROR_FUNCTION_FAILED
;
2006 return ERROR_SUCCESS
;
2009 static UINT
TABLE_sort(struct tagMSIVIEW
*view
, column_info
*columns
)
2011 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
2012 MSIORDERINFO
*order
;
2017 TRACE("sorting table %s\n", debugstr_w(tv
->name
));
2019 r
= TABLE_get_dimensions(view
, &rows
, &cols
);
2020 if (r
!= ERROR_SUCCESS
)
2024 return ERROR_SUCCESS
;
2026 order
= msi_alloc_zero(sizeof(MSIORDERINFO
) + sizeof(UINT
) * cols
);
2028 return ERROR_OUTOFMEMORY
;
2030 for (ptr
= columns
; ptr
; ptr
= ptr
->next
)
2031 order_add_column(view
, order
, ptr
->column
);
2033 order
->reorder
= msi_alloc(rows
* sizeof(UINT
));
2034 if (!order
->reorder
)
2035 return ERROR_OUTOFMEMORY
;
2037 for (i
= 0; i
< rows
; i
++)
2038 order
->reorder
[i
] = i
;
2040 r
= order_mergesort(view
, order
, 0, rows
- 1);
2041 if (r
!= ERROR_SUCCESS
)
2044 r
= order_verify(view
, order
, rows
);
2045 if (r
!= ERROR_SUCCESS
)
2050 return ERROR_SUCCESS
;
2053 static UINT
TABLE_drop(struct tagMSIVIEW
*view
)
2055 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
2056 MSIVIEW
*tables
= NULL
;
2057 MSIRECORD
*rec
= NULL
;
2060 TRACE("dropping table %s\n", debugstr_w(tv
->name
));
2062 for (i
= 0; i
< tv
->table
->col_count
; i
++)
2064 r
= TABLE_remove_column(view
, tv
->table
->colinfo
[i
].tablename
,
2065 tv
->table
->colinfo
[i
].number
);
2066 if (r
!= ERROR_SUCCESS
)
2070 rec
= MSI_CreateRecord(1);
2072 return ERROR_OUTOFMEMORY
;
2074 MSI_RecordSetStringW(rec
, 1, tv
->name
);
2076 r
= TABLE_CreateView(tv
->db
, szTables
, &tables
);
2077 if (r
!= ERROR_SUCCESS
)
2080 r
= msi_table_find_row((MSITABLEVIEW
*)tables
, rec
, &row
);
2081 if (r
!= ERROR_SUCCESS
)
2084 r
= TABLE_delete_row(tables
, row
);
2085 if (r
!= ERROR_SUCCESS
)
2088 list_remove(&tv
->table
->entry
);
2089 free_table(tv
->table
);
2093 msiobj_release(&rec
->hdr
);
2094 tables
->ops
->delete(tables
);
2099 static const MSIVIEWOPS table_ops
=
2109 TABLE_get_dimensions
,
2110 TABLE_get_column_info
,
2113 TABLE_find_matching_rows
,
2117 TABLE_remove_column
,
2122 UINT
TABLE_CreateView( MSIDATABASE
*db
, LPCWSTR name
, MSIVIEW
**view
)
2127 static const WCHAR Streams
[] = {'_','S','t','r','e','a','m','s',0};
2128 static const WCHAR Storages
[] = {'_','S','t','o','r','a','g','e','s',0};
2130 TRACE("%p %s %p\n", db
, debugstr_w(name
), view
);
2132 if ( !lstrcmpW( name
, Streams
) )
2133 return STREAMS_CreateView( db
, view
);
2134 else if ( !lstrcmpW( name
, Storages
) )
2135 return STORAGES_CreateView( db
, view
);
2137 sz
= sizeof *tv
+ lstrlenW(name
)*sizeof name
[0] ;
2138 tv
= msi_alloc_zero( sz
);
2140 return ERROR_FUNCTION_FAILED
;
2142 r
= get_table( db
, name
, &tv
->table
);
2143 if( r
!= ERROR_SUCCESS
)
2146 WARN("table not found\n");
2150 TRACE("table %p found with %d columns\n", tv
->table
, tv
->table
->col_count
);
2152 /* fill the structure */
2153 tv
->view
.ops
= &table_ops
;
2155 tv
->columns
= tv
->table
->colinfo
;
2156 tv
->num_cols
= tv
->table
->col_count
;
2157 tv
->row_size
= msi_table_get_row_size( db
, tv
->table
->colinfo
, tv
->table
->col_count
);
2159 TRACE("%s one row is %d bytes\n", debugstr_w(name
), tv
->row_size
);
2161 *view
= (MSIVIEW
*) tv
;
2162 lstrcpyW( tv
->name
, name
);
2164 return ERROR_SUCCESS
;
2167 UINT
MSI_CommitTables( MSIDATABASE
*db
)
2170 MSITABLE
*table
= NULL
;
2174 r
= msi_save_string_table( db
->strings
, db
->storage
);
2175 if( r
!= ERROR_SUCCESS
)
2177 WARN("failed to save string table r=%08x\n",r
);
2181 LIST_FOR_EACH_ENTRY( table
, &db
->tables
, MSITABLE
, entry
)
2183 r
= save_table( db
, table
);
2184 if( r
!= ERROR_SUCCESS
)
2186 WARN("failed to save table %s (r=%08x)\n",
2187 debugstr_w(table
->name
), r
);
2192 /* force everything to reload next time */
2193 free_cached_tables( db
);
2195 return ERROR_SUCCESS
;
2198 MSICONDITION
MSI_DatabaseIsTablePersistent( MSIDATABASE
*db
, LPCWSTR table
)
2203 TRACE("%p %s\n", db
, debugstr_w(table
));
2206 return MSICONDITION_ERROR
;
2208 r
= get_table( db
, table
, &t
);
2209 if (r
!= ERROR_SUCCESS
)
2210 return MSICONDITION_NONE
;
2213 return MSICONDITION_TRUE
;
2215 return MSICONDITION_FALSE
;
2218 static UINT
read_raw_int(const BYTE
*data
, UINT col
, UINT bytes
)
2222 for (i
= 0; i
< bytes
; i
++)
2223 ret
+= (data
[col
+ i
] << i
* 8);
2228 static MSIRECORD
*msi_get_transform_record( const MSITABLEVIEW
*tv
, const string_table
*st
,
2229 const BYTE
*rawdata
, UINT bytes_per_strref
)
2231 UINT i
, val
, ofs
= 0;
2233 MSICOLUMNINFO
*columns
= tv
->columns
;
2236 mask
= rawdata
[0] | (rawdata
[1] << 8);
2239 rec
= MSI_CreateRecord( tv
->num_cols
);
2244 for( i
=0; i
<tv
->num_cols
; i
++ )
2246 if ( (mask
&1) && (i
>=(mask
>>8)) )
2248 /* all keys must be present */
2249 if ( (~mask
&1) && (~columns
[i
].type
& MSITYPE_KEY
) && ((1<<i
) & ~mask
) )
2252 if( (columns
[i
].type
& MSITYPE_STRING
) &&
2253 ! MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
2257 val
= read_raw_int(rawdata
, ofs
, bytes_per_strref
);
2258 sval
= msi_string_lookup_id( st
, val
);
2259 MSI_RecordSetStringW( rec
, i
+1, sval
);
2260 TRACE(" field %d [%s]\n", i
+1, debugstr_w(sval
));
2261 ofs
+= bytes_per_strref
;
2265 UINT n
= bytes_per_column( tv
->db
, &columns
[i
] );
2269 val
= read_raw_int(rawdata
, ofs
, n
);
2271 MSI_RecordSetInteger( rec
, i
+1, val
^0x8000 );
2272 TRACE(" field %d [0x%04x]\n", i
+1, val
);
2275 val
= read_raw_int(rawdata
, ofs
, n
);
2277 MSI_RecordSetInteger( rec
, i
+1, val
^0x80000000 );
2278 TRACE(" field %d [0x%08x]\n", i
+1, val
);
2281 ERR("oops - unknown column width %d\n", n
);
2290 static void dump_record( MSIRECORD
*rec
)
2294 n
= MSI_RecordGetFieldCount( rec
);
2295 for( i
=1; i
<=n
; i
++ )
2297 LPCWSTR sval
= MSI_RecordGetString( rec
, i
);
2299 if( MSI_RecordIsNull( rec
, i
) )
2300 TRACE("row -> []\n");
2301 else if( (sval
= MSI_RecordGetString( rec
, i
)) )
2302 TRACE("row -> [%s]\n", debugstr_w(sval
));
2304 TRACE("row -> [0x%08x]\n", MSI_RecordGetInteger( rec
, i
) );
2308 static void dump_table( const string_table
*st
, const USHORT
*rawdata
, UINT rawsize
)
2313 for( i
=0; i
<(rawsize
/2); i
++ )
2315 sval
= msi_string_lookup_id( st
, rawdata
[i
] );
2316 MESSAGE(" %04x %s\n", rawdata
[i
], debugstr_w(sval
) );
2320 static UINT
* msi_record_to_row( const MSITABLEVIEW
*tv
, MSIRECORD
*rec
)
2325 data
= msi_alloc( tv
->num_cols
*sizeof (UINT
) );
2326 for( i
=0; i
<tv
->num_cols
; i
++ )
2330 if ( ~tv
->columns
[i
].type
& MSITYPE_KEY
)
2333 /* turn the transform column value into a row value */
2334 if ( ( tv
->columns
[i
].type
& MSITYPE_STRING
) &&
2335 ! MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
2337 str
= MSI_RecordGetString( rec
, i
+1 );
2338 r
= msi_string2idW( tv
->db
->strings
, str
, &data
[i
] );
2340 /* if there's no matching string in the string table,
2341 these keys can't match any record, so fail now. */
2342 if( ERROR_SUCCESS
!= r
)
2350 data
[i
] = MSI_RecordGetInteger( rec
, i
+1 );
2351 if ((tv
->columns
[i
].type
&0xff) == 2)
2354 data
[i
] += 0x80000000;
2360 static UINT
msi_row_matches( MSITABLEVIEW
*tv
, UINT row
, const UINT
*data
)
2362 UINT i
, r
, x
, ret
= ERROR_FUNCTION_FAILED
;
2364 for( i
=0; i
<tv
->num_cols
; i
++ )
2366 if ( ~tv
->columns
[i
].type
& MSITYPE_KEY
)
2369 /* turn the transform column value into a row value */
2370 r
= TABLE_fetch_int( &tv
->view
, row
, i
+1, &x
);
2371 if ( r
!= ERROR_SUCCESS
)
2373 ERR("TABLE_fetch_int shouldn't fail here\n");
2377 /* if this key matches, move to the next column */
2380 ret
= ERROR_FUNCTION_FAILED
;
2384 ret
= ERROR_SUCCESS
;
2390 static UINT
msi_table_find_row( MSITABLEVIEW
*tv
, MSIRECORD
*rec
, UINT
*row
)
2392 UINT i
, r
= ERROR_FUNCTION_FAILED
, *data
;
2394 data
= msi_record_to_row( tv
, rec
);
2397 for( i
= 0; i
< tv
->table
->row_count
+ tv
->table
->nonpersistent_row_count
; i
++ )
2399 r
= msi_row_matches( tv
, i
, data
);
2400 if( r
== ERROR_SUCCESS
)
2416 static UINT
msi_table_load_transform( MSIDATABASE
*db
, IStorage
*stg
,
2417 string_table
*st
, TRANSFORMDATA
*transform
,
2418 UINT bytes_per_strref
)
2421 BYTE
*rawdata
= NULL
;
2422 MSITABLEVIEW
*tv
= NULL
;
2423 UINT r
, n
, sz
, i
, mask
;
2424 MSIRECORD
*rec
= NULL
;
2430 return ERROR_SUCCESS
;
2432 name
= transform
->name
;
2435 TRACE("%p %p %p %s\n", db
, stg
, st
, debugstr_w(name
) );
2437 /* read the transform data */
2438 read_stream_data( stg
, name
, TRUE
, &rawdata
, &rawsize
);
2441 TRACE("table %s empty\n", debugstr_w(name
) );
2442 return ERROR_INVALID_TABLE
;
2445 /* create a table view */
2446 r
= TABLE_CreateView( db
, name
, (MSIVIEW
**) &tv
);
2447 if( r
!= ERROR_SUCCESS
)
2450 r
= tv
->view
.ops
->execute( &tv
->view
, NULL
);
2451 if( r
!= ERROR_SUCCESS
)
2454 TRACE("name = %s columns = %u row_size = %u raw size = %u\n",
2455 debugstr_w(name
), tv
->num_cols
, tv
->row_size
, rawsize
);
2457 /* interpret the data */
2459 for( n
=0; n
< rawsize
; )
2461 mask
= rawdata
[n
] | (rawdata
[n
+1] << 8);
2466 * if the low bit is set, columns are continuous and
2467 * the number of columns is specified in the high byte
2470 for( i
=0; i
<tv
->num_cols
; i
++ )
2472 if( (tv
->columns
[i
].type
& MSITYPE_STRING
) &&
2473 ! MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
2474 sz
+= bytes_per_strref
;
2476 sz
+= bytes_per_column( tv
->db
, &tv
->columns
[i
] );
2482 * If the low bit is not set, mask is a bitmask.
2483 * Excepting for key fields, which are always present,
2484 * each bit indicates that a field is present in the transform record.
2486 * mask == 0 is a special case ... only the keys will be present
2487 * and it means that this row should be deleted.
2490 for( i
=0; i
<tv
->num_cols
; i
++ )
2492 if( (tv
->columns
[i
].type
& MSITYPE_KEY
) || ((1<<i
)&mask
))
2494 if( (tv
->columns
[i
].type
& MSITYPE_STRING
) &&
2495 ! MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
2496 sz
+= bytes_per_strref
;
2498 sz
+= bytes_per_column( tv
->db
, &tv
->columns
[i
] );
2503 /* check we didn't run of the end of the table */
2504 if ( (n
+sz
) > rawsize
)
2507 dump_table( st
, (USHORT
*)rawdata
, rawsize
);
2511 rec
= msi_get_transform_record( tv
, st
, &rawdata
[n
], bytes_per_strref
);
2518 UINT number
= MSI_NULL_INTEGER
;
2520 TRACE("inserting record\n");
2522 if (!lstrcmpW(name
, szColumns
))
2524 MSI_RecordGetStringW( rec
, 1, table
, &sz
);
2525 number
= MSI_RecordGetInteger( rec
, 2 );
2528 * Native msi seems writes nul into the Number (2nd) column of
2529 * the _Columns table, only when the columns are from a new table
2531 if ( number
== MSI_NULL_INTEGER
)
2533 /* reset the column number on a new table */
2534 if ( lstrcmpW(coltable
, table
) )
2537 lstrcpyW( coltable
, table
);
2540 /* fix nul column numbers */
2541 MSI_RecordSetInteger( rec
, 2, ++colcol
);
2545 r
= TABLE_insert_row( &tv
->view
, rec
, FALSE
);
2546 if (r
!= ERROR_SUCCESS
)
2547 ERR("insert row failed\n");
2549 if ( number
!= MSI_NULL_INTEGER
&& !lstrcmpW(name
, szColumns
) )
2550 msi_update_table_columns( db
, table
);
2556 r
= msi_table_find_row( tv
, rec
, &row
);
2557 if (r
!= ERROR_SUCCESS
)
2558 ERR("no matching row to transform\n");
2561 TRACE("modifying row [%d]:\n", row
);
2562 TABLE_set_row( &tv
->view
, row
, rec
, mask
);
2566 TRACE("deleting row [%d]:\n", row
);
2567 TABLE_delete_row( &tv
->view
, row
);
2570 if( TRACE_ON(msidb
) ) dump_record( rec
);
2571 msiobj_release( &rec
->hdr
);
2578 /* no need to free the table, it's associated with the database */
2579 msi_free( rawdata
);
2581 tv
->view
.ops
->delete( &tv
->view
);
2583 return ERROR_SUCCESS
;
2587 * msi_table_apply_transform
2589 * Enumerate the table transforms in a transform storage and apply each one.
2591 UINT
msi_table_apply_transform( MSIDATABASE
*db
, IStorage
*stg
)
2593 struct list transforms
;
2594 IEnumSTATSTG
*stgenum
= NULL
;
2595 TRANSFORMDATA
*transform
;
2596 TRANSFORMDATA
*tables
= NULL
, *columns
= NULL
;
2599 string_table
*strings
;
2600 UINT ret
= ERROR_FUNCTION_FAILED
;
2601 UINT bytes_per_strref
;
2603 TRACE("%p %p\n", db
, stg
);
2605 strings
= msi_load_string_table( stg
, &bytes_per_strref
);
2609 r
= IStorage_EnumElements( stg
, 0, NULL
, 0, &stgenum
);
2613 list_init(&transforms
);
2617 MSITABLEVIEW
*tv
= NULL
;
2621 r
= IEnumSTATSTG_Next( stgenum
, 1, &stat
, &count
);
2622 if ( FAILED( r
) || !count
)
2625 decode_streamname( stat
.pwcsName
, name
);
2626 CoTaskMemFree( stat
.pwcsName
);
2627 if ( name
[0] != 0x4840 )
2630 if ( !lstrcmpW( name
+1, szStringPool
) ||
2631 !lstrcmpW( name
+1, szStringData
) )
2634 transform
= msi_alloc_zero( sizeof(TRANSFORMDATA
) );
2638 list_add_tail( &transforms
, &transform
->entry
);
2640 transform
->name
= strdupW( name
+ 1 );
2642 if ( !lstrcmpW( transform
->name
, szTables
) )
2644 else if (!lstrcmpW( transform
->name
, szColumns
) )
2645 columns
= transform
;
2647 TRACE("transform contains stream %s\n", debugstr_w(name
));
2649 /* load the table */
2650 r
= TABLE_CreateView( db
, transform
->name
, (MSIVIEW
**) &tv
);
2651 if( r
!= ERROR_SUCCESS
)
2654 r
= tv
->view
.ops
->execute( &tv
->view
, NULL
);
2655 if( r
!= ERROR_SUCCESS
)
2657 tv
->view
.ops
->delete( &tv
->view
);
2661 tv
->view
.ops
->delete( &tv
->view
);
2665 * Apply _Tables and _Columns transforms first so that
2666 * the table metadata is correct, and empty tables exist.
2668 ret
= msi_table_load_transform( db
, stg
, strings
, tables
, bytes_per_strref
);
2669 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_INVALID_TABLE
)
2672 ret
= msi_table_load_transform( db
, stg
, strings
, columns
, bytes_per_strref
);
2673 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_INVALID_TABLE
)
2676 ret
= ERROR_SUCCESS
;
2678 while ( !list_empty( &transforms
) )
2680 transform
= LIST_ENTRY( list_head( &transforms
), TRANSFORMDATA
, entry
);
2682 if ( lstrcmpW( transform
->name
, szColumns
) &&
2683 lstrcmpW( transform
->name
, szTables
) &&
2684 ret
== ERROR_SUCCESS
)
2686 ret
= msi_table_load_transform( db
, stg
, strings
, transform
, bytes_per_strref
);
2689 list_remove( &transform
->entry
);
2690 msi_free( transform
->name
);
2691 msi_free( transform
);
2694 if ( ret
== ERROR_SUCCESS
)
2695 append_storage_to_db( db
, stg
);
2699 IEnumSTATSTG_Release( stgenum
);
2701 msi_destroy_stringtable( strings
);
2706 void append_storage_to_db( MSIDATABASE
*db
, IStorage
*stg
)
2710 t
= msi_alloc( sizeof *t
);
2712 IStorage_AddRef( stg
);
2713 list_add_tail( &db
->transforms
, &t
->entry
);
2716 void msi_free_transforms( MSIDATABASE
*db
)
2718 while( !list_empty( &db
->transforms
) )
2720 MSITRANSFORM
*t
= LIST_ENTRY( list_head( &db
->transforms
),
2721 MSITRANSFORM
, entry
);
2722 list_remove( &t
->entry
);
2723 IStorage_Release( t
->stg
);