2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2002-2005 Mike McCormack for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
39 #include "wine/debug.h"
40 #include "wine/unicode.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(msidb
);
44 #define MSITABLE_HASH_TABLE_SIZE 37
45 #define LONG_STR_BYTES 3
47 typedef struct tagMSICOLUMNHASHENTRY
49 struct tagMSICOLUMNHASHENTRY
*next
;
54 typedef struct tagMSICOLUMNINFO
63 MSICOLUMNHASHENTRY
**hash_table
;
66 typedef struct tagMSIORDERINFO
77 BYTE
**nonpersistent_data
;
78 UINT nonpersistent_row_count
;
80 MSICOLUMNINFO
*colinfo
;
82 MSICONDITION persistent
;
87 typedef struct tagMSITRANSFORM
{
92 static const WCHAR szStringData
[] = {
93 '_','S','t','r','i','n','g','D','a','t','a',0 };
94 static const WCHAR szStringPool
[] = {
95 '_','S','t','r','i','n','g','P','o','o','l',0 };
97 /* information for default tables */
98 static WCHAR szTables
[] = { '_','T','a','b','l','e','s',0 };
99 static WCHAR szTable
[] = { 'T','a','b','l','e',0 };
100 static WCHAR szName
[] = { 'N','a','m','e',0 };
101 static WCHAR szColumns
[] = { '_','C','o','l','u','m','n','s',0 };
102 static WCHAR szNumber
[] = { 'N','u','m','b','e','r',0 };
103 static WCHAR szType
[] = { 'T','y','p','e',0 };
105 static const MSICOLUMNINFO _Columns_cols
[4] = {
106 { szColumns
, 1, szTable
, MSITYPE_VALID
| MSITYPE_STRING
| MSITYPE_KEY
| 64, 0, 0, 0, NULL
},
107 { szColumns
, 2, szNumber
, MSITYPE_VALID
| MSITYPE_KEY
| 2, 2, 0, 0, NULL
},
108 { szColumns
, 3, szName
, MSITYPE_VALID
| MSITYPE_STRING
| 64, 4, 0, 0, NULL
},
109 { szColumns
, 4, szType
, MSITYPE_VALID
| 2, 6, 0, 0, NULL
},
112 static const MSICOLUMNINFO _Tables_cols
[1] = {
113 { szTables
, 1, szName
, MSITYPE_VALID
| MSITYPE_STRING
| MSITYPE_KEY
| 64, 0, 0, 0, NULL
},
116 #define MAX_STREAM_NAME 0x1f
118 static UINT
table_get_column_info( MSIDATABASE
*db
, LPCWSTR name
,
119 MSICOLUMNINFO
**pcols
, UINT
*pcount
);
120 static void table_calc_column_offsets( MSIDATABASE
*db
, MSICOLUMNINFO
*colinfo
,
122 static UINT
get_tablecolumns( MSIDATABASE
*db
,
123 LPCWSTR szTableName
, MSICOLUMNINFO
*colinfo
, UINT
*sz
);
124 static void msi_free_colinfo( MSICOLUMNINFO
*colinfo
, UINT count
);
126 static inline UINT
bytes_per_column( MSIDATABASE
*db
, const MSICOLUMNINFO
*col
)
128 if( MSITYPE_IS_BINARY(col
->type
) )
130 if( col
->type
& MSITYPE_STRING
)
131 return db
->bytes_per_strref
;
132 if( (col
->type
& 0xff) > 4 )
133 ERR("Invalid column size!\n");
134 return col
->type
& 0xff;
137 static int utf2mime(int x
)
139 if( (x
>='0') && (x
<='9') )
141 if( (x
>='A') && (x
<='Z') )
143 if( (x
>='a') && (x
<='z') )
152 static LPWSTR
encode_streamname(BOOL bTable
, LPCWSTR in
)
154 DWORD count
= MAX_STREAM_NAME
;
159 count
= lstrlenW( in
)+2;
160 out
= msi_alloc( count
*sizeof(WCHAR
) );
176 if( ( ch
< 0x80 ) && ( utf2mime(ch
) >= 0 ) )
178 ch
= utf2mime(ch
) + 0x4800;
180 if( next
&& (next
<0x80) )
182 next
= utf2mime(next
);
193 ERR("Failed to encode stream name (%s)\n",debugstr_w(in
));
198 static int mime2utf(int x
)
205 return x
- 10 - 26 + 'a';
206 if( x
== (10+26+26) )
211 BOOL
decode_streamname(LPCWSTR in
, LPWSTR out
)
216 while ( (ch
= *in
++) )
218 if( (ch
>= 0x3800 ) && (ch
< 0x4840 ) )
221 ch
= mime2utf(ch
-0x4800);
225 *out
++ = mime2utf(ch
&0x3f);
227 ch
= mime2utf((ch
>>6)&0x3f);
237 void enum_stream_names( IStorage
*stg
)
239 IEnumSTATSTG
*stgenum
= NULL
;
245 r
= IStorage_EnumElements( stg
, 0, NULL
, 0, &stgenum
);
253 r
= IEnumSTATSTG_Next( stgenum
, 1, &stat
, &count
);
254 if( FAILED( r
) || !count
)
256 decode_streamname( stat
.pwcsName
, name
);
257 TRACE("stream %2d -> %s %s\n", n
,
258 debugstr_w(stat
.pwcsName
), debugstr_w(name
) );
259 CoTaskMemFree( stat
.pwcsName
);
263 IEnumSTATSTG_Release( stgenum
);
266 UINT
read_stream_data( IStorage
*stg
, LPCWSTR stname
, BOOL table
,
267 BYTE
**pdata
, UINT
*psz
)
270 UINT ret
= ERROR_FUNCTION_FAILED
;
277 encname
= encode_streamname(table
, stname
);
279 TRACE("%s -> %s\n",debugstr_w(stname
),debugstr_w(encname
));
281 r
= IStorage_OpenStream(stg
, encname
, NULL
,
282 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, &stm
);
286 WARN("open stream failed r = %08x - empty table?\n", r
);
290 r
= IStream_Stat(stm
, &stat
, STATFLAG_NONAME
);
293 WARN("open stream failed r = %08x!\n", r
);
297 if( stat
.cbSize
.QuadPart
>> 32 )
303 sz
= stat
.cbSize
.QuadPart
;
304 data
= msi_alloc( sz
);
307 WARN("couldn't allocate memory r=%08x!\n", r
);
308 ret
= ERROR_NOT_ENOUGH_MEMORY
;
312 r
= IStream_Read(stm
, data
, sz
, &count
);
313 if( FAILED( r
) || ( count
!= sz
) )
316 WARN("read stream failed r = %08x!\n", r
);
325 IStream_Release( stm
);
330 static UINT
db_get_raw_stream( MSIDATABASE
*db
, LPCWSTR stname
, IStream
**stm
)
335 encname
= encode_streamname(FALSE
, stname
);
337 TRACE("%s -> %s\n",debugstr_w(stname
),debugstr_w(encname
));
339 r
= IStorage_OpenStream(db
->storage
, encname
, NULL
,
340 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, stm
);
343 MSITRANSFORM
*transform
;
345 LIST_FOR_EACH_ENTRY( transform
, &db
->transforms
, MSITRANSFORM
, entry
)
347 TRACE("looking for %s in transform storage\n", debugstr_w(stname
) );
348 r
= IStorage_OpenStream( transform
->stg
, encname
, NULL
,
349 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, stm
);
357 return SUCCEEDED(r
) ? ERROR_SUCCESS
: ERROR_FUNCTION_FAILED
;
360 UINT
read_raw_stream_data( MSIDATABASE
*db
, LPCWSTR stname
,
361 USHORT
**pdata
, UINT
*psz
)
364 UINT ret
= ERROR_FUNCTION_FAILED
;
370 r
= db_get_raw_stream( db
, stname
, &stm
);
371 if( r
!= ERROR_SUCCESS
)
373 r
= IStream_Stat(stm
, &stat
, STATFLAG_NONAME
);
376 WARN("open stream failed r = %08x!\n", r
);
380 if( stat
.cbSize
.QuadPart
>> 32 )
386 sz
= stat
.cbSize
.QuadPart
;
387 data
= msi_alloc( sz
);
390 WARN("couldn't allocate memory r=%08x!\n", r
);
391 ret
= ERROR_NOT_ENOUGH_MEMORY
;
395 r
= IStream_Read(stm
, data
, sz
, &count
);
396 if( FAILED( r
) || ( count
!= sz
) )
399 WARN("read stream failed r = %08x!\n", r
);
408 IStream_Release( stm
);
413 UINT
write_stream_data( IStorage
*stg
, LPCWSTR stname
,
414 LPCVOID data
, UINT sz
, BOOL bTable
)
417 UINT ret
= ERROR_FUNCTION_FAILED
;
424 encname
= encode_streamname(bTable
, stname
);
425 r
= IStorage_OpenStream( stg
, encname
, NULL
,
426 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, &stm
);
429 r
= IStorage_CreateStream( stg
, encname
,
430 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, 0, &stm
);
435 WARN("open stream failed r = %08x\n", r
);
440 r
= IStream_SetSize( stm
, size
);
443 WARN("Failed to SetSize\n");
448 r
= IStream_Seek( stm
, pos
, STREAM_SEEK_SET
, NULL
);
451 WARN("Failed to Seek\n");
457 r
= IStream_Write(stm
, data
, sz
, &count
);
458 if( FAILED( r
) || ( count
!= sz
) )
460 WARN("Failed to Write\n");
468 IStream_Release( stm
);
473 static void free_table( MSITABLE
*table
)
476 for( i
=0; i
<table
->row_count
; i
++ )
477 msi_free( table
->data
[i
] );
478 msi_free( table
->data
);
479 for( i
=0; i
<table
->nonpersistent_row_count
; i
++ )
480 msi_free( table
->nonpersistent_data
[i
] );
481 msi_free( table
->nonpersistent_data
);
482 msi_free_colinfo( table
->colinfo
, table
->col_count
);
483 msi_free( table
->colinfo
);
487 static UINT
msi_table_get_row_size( MSIDATABASE
*db
,const MSICOLUMNINFO
*cols
,
490 const MSICOLUMNINFO
*last_col
= &cols
[count
-1];
493 return last_col
->offset
+ bytes_per_column( db
, last_col
);
496 /* add this table to the list of cached tables in the database */
497 static UINT
read_table_from_storage( MSIDATABASE
*db
, MSITABLE
*t
, IStorage
*stg
)
499 BYTE
*rawdata
= NULL
;
500 UINT rawsize
= 0, i
, j
, row_size
= 0;
502 TRACE("%s\n",debugstr_w(t
->name
));
504 row_size
= msi_table_get_row_size( db
, t
->colinfo
, t
->col_count
);
506 /* if we can't read the table, just assume that it's empty */
507 read_stream_data( stg
, t
->name
, TRUE
, &rawdata
, &rawsize
);
509 return ERROR_SUCCESS
;
511 TRACE("Read %d bytes\n", rawsize
);
513 if( rawsize
% row_size
)
515 WARN("Table size is invalid %d/%d\n", rawsize
, row_size
);
519 t
->row_count
= rawsize
/ row_size
;
520 t
->data
= msi_alloc_zero( t
->row_count
* sizeof (USHORT
*) );
524 /* transpose all the data */
525 TRACE("Transposing data from %d rows\n", t
->row_count
);
526 for( i
=0; i
<t
->row_count
; i
++ )
528 t
->data
[i
] = msi_alloc( row_size
);
532 for( j
=0; j
<t
->col_count
; j
++ )
534 UINT ofs
= t
->colinfo
[j
].offset
;
535 UINT n
= bytes_per_column( db
, &t
->colinfo
[j
] );
538 if ( n
!= 2 && n
!= 3 && n
!= 4 )
540 ERR("oops - unknown column width %d\n", n
);
544 for ( k
= 0; k
< n
; k
++ )
545 t
->data
[i
][ofs
+ k
] = rawdata
[ofs
*t
->row_count
+ i
* n
+ k
];
550 return ERROR_SUCCESS
;
553 return ERROR_FUNCTION_FAILED
;
556 void free_cached_tables( MSIDATABASE
*db
)
558 while( !list_empty( &db
->tables
) )
560 MSITABLE
*t
= LIST_ENTRY( list_head( &db
->tables
), MSITABLE
, entry
);
562 list_remove( &t
->entry
);
567 static MSITABLE
*find_cached_table( MSIDATABASE
*db
, LPCWSTR name
)
571 LIST_FOR_EACH_ENTRY( t
, &db
->tables
, MSITABLE
, entry
)
572 if( !lstrcmpW( name
, t
->name
) )
578 static UINT
table_get_column_info( MSIDATABASE
*db
, LPCWSTR name
, MSICOLUMNINFO
**pcols
, UINT
*pcount
)
580 UINT r
, column_count
= 0;
581 MSICOLUMNINFO
*columns
;
583 /* get the number of columns in this table */
585 r
= get_tablecolumns( db
, name
, NULL
, &column_count
);
586 if( r
!= ERROR_SUCCESS
)
589 *pcount
= column_count
;
591 /* if there's no columns, there's no table */
592 if( column_count
== 0 )
593 return ERROR_INVALID_PARAMETER
;
595 TRACE("Table %s found\n", debugstr_w(name
) );
597 columns
= msi_alloc( column_count
*sizeof (MSICOLUMNINFO
) );
599 return ERROR_FUNCTION_FAILED
;
601 r
= get_tablecolumns( db
, name
, columns
, &column_count
);
602 if( r
!= ERROR_SUCCESS
)
605 return ERROR_FUNCTION_FAILED
;
613 UINT
msi_create_table( MSIDATABASE
*db
, LPCWSTR name
, column_info
*col_info
,
614 MSICONDITION persistent
, MSITABLE
**table_ret
)
618 MSIRECORD
*rec
= NULL
;
623 /* only add tables that don't exist already */
624 if( TABLE_Exists(db
, name
) )
626 WARN("table %s exists\n", debugstr_w(name
));
627 return ERROR_BAD_QUERY_SYNTAX
;
630 table
= msi_alloc( sizeof (MSITABLE
) + lstrlenW(name
)*sizeof (WCHAR
) );
632 return ERROR_FUNCTION_FAILED
;
634 table
->ref_count
= 1;
635 table
->row_count
= 0;
637 table
->nonpersistent_row_count
= 0;
638 table
->nonpersistent_data
= NULL
;
639 table
->colinfo
= NULL
;
640 table
->col_count
= 0;
641 table
->persistent
= persistent
;
642 lstrcpyW( table
->name
, name
);
644 for( col
= col_info
; col
; col
= col
->next
)
647 table
->colinfo
= msi_alloc( table
->col_count
* sizeof(MSICOLUMNINFO
) );
651 return ERROR_FUNCTION_FAILED
;
654 for( i
= 0, col
= col_info
; col
; i
++, col
= col
->next
)
656 table
->colinfo
[ i
].tablename
= strdupW( col
->table
);
657 table
->colinfo
[ i
].number
= i
+ 1;
658 table
->colinfo
[ i
].colname
= strdupW( col
->column
);
659 table
->colinfo
[ i
].type
= col
->type
;
660 table
->colinfo
[ i
].offset
= 0;
661 table
->colinfo
[ i
].ref_count
= 0;
662 table
->colinfo
[ i
].hash_table
= NULL
;
663 table
->colinfo
[ i
].temporary
= col
->temporary
;
665 table_calc_column_offsets( db
, table
->colinfo
, table
->col_count
);
667 r
= TABLE_CreateView( db
, szTables
, &tv
);
668 TRACE("CreateView returned %x\n", r
);
675 r
= tv
->ops
->execute( tv
, 0 );
676 TRACE("tv execute returned %x\n", r
);
680 rec
= MSI_CreateRecord( 1 );
684 r
= MSI_RecordSetStringW( rec
, 1, name
);
688 r
= tv
->ops
->insert_row( tv
, rec
, -1, persistent
== MSICONDITION_FALSE
);
689 TRACE("insert_row returned %x\n", r
);
693 tv
->ops
->delete( tv
);
696 msiobj_release( &rec
->hdr
);
699 if( persistent
!= MSICONDITION_FALSE
)
701 /* add each column to the _Columns table */
702 r
= TABLE_CreateView( db
, szColumns
, &tv
);
706 r
= tv
->ops
->execute( tv
, 0 );
707 TRACE("tv execute returned %x\n", r
);
711 rec
= MSI_CreateRecord( 4 );
715 r
= MSI_RecordSetStringW( rec
, 1, name
);
720 * need to set the table, column number, col name and type
721 * for each column we enter in the table
724 for( col
= col_info
; col
; col
= col
->next
)
726 r
= MSI_RecordSetInteger( rec
, 2, nField
);
730 r
= MSI_RecordSetStringW( rec
, 3, col
->column
);
734 r
= MSI_RecordSetInteger( rec
, 4, col
->type
);
738 r
= tv
->ops
->insert_row( tv
, rec
, -1, FALSE
);
750 msiobj_release( &rec
->hdr
);
751 /* FIXME: remove values from the string table on error */
753 tv
->ops
->delete( tv
);
755 if (r
== ERROR_SUCCESS
)
757 list_add_head( &db
->tables
, &table
->entry
);
766 static UINT
get_table( MSIDATABASE
*db
, LPCWSTR name
, MSITABLE
**table_ret
)
771 /* first, see if the table is cached */
772 table
= find_cached_table( db
, name
);
776 return ERROR_SUCCESS
;
779 /* nonexistent tables should be interpreted as empty tables */
780 table
= msi_alloc( sizeof (MSITABLE
) + lstrlenW(name
)*sizeof (WCHAR
) );
782 return ERROR_FUNCTION_FAILED
;
784 table
->row_count
= 0;
786 table
->nonpersistent_row_count
= 0;
787 table
->nonpersistent_data
= NULL
;
788 table
->colinfo
= NULL
;
789 table
->col_count
= 0;
790 table
->persistent
= MSICONDITION_TRUE
;
791 lstrcpyW( table
->name
, name
);
793 if ( !lstrcmpW(name
, szTables
) || !lstrcmpW(name
, szColumns
) )
794 table
->persistent
= MSICONDITION_NONE
;
796 r
= table_get_column_info( db
, name
, &table
->colinfo
, &table
->col_count
);
797 if (r
!= ERROR_SUCCESS
)
799 free_table ( table
);
803 r
= read_table_from_storage( db
, table
, db
->storage
);
804 if( r
!= ERROR_SUCCESS
)
810 list_add_head( &db
->tables
, &table
->entry
);
812 return ERROR_SUCCESS
;
815 static UINT
save_table( MSIDATABASE
*db
, const MSITABLE
*t
)
817 BYTE
*rawdata
= NULL
, *p
;
818 UINT rawsize
, r
, i
, j
, row_size
;
820 /* Nothing to do for non-persistent tables */
821 if( t
->persistent
== MSICONDITION_FALSE
)
822 return ERROR_SUCCESS
;
824 TRACE("Saving %s\n", debugstr_w( t
->name
) );
826 row_size
= msi_table_get_row_size( db
, t
->colinfo
, t
->col_count
);
828 rawsize
= t
->row_count
* row_size
;
829 rawdata
= msi_alloc_zero( rawsize
);
832 r
= ERROR_NOT_ENOUGH_MEMORY
;
837 for( i
=0; i
<t
->col_count
; i
++ )
839 for( j
=0; j
<t
->row_count
; j
++ )
841 UINT offset
= t
->colinfo
[i
].offset
;
843 *p
++ = t
->data
[j
][offset
];
844 *p
++ = t
->data
[j
][offset
+ 1];
845 if( 4 == bytes_per_column( db
, &t
->colinfo
[i
] ) )
847 *p
++ = t
->data
[j
][offset
+ 2];
848 *p
++ = t
->data
[j
][offset
+ 3];
853 TRACE("writing %d bytes\n", rawsize
);
854 r
= write_stream_data( db
->storage
, t
->name
, rawdata
, rawsize
, TRUE
);
862 static void table_calc_column_offsets( MSIDATABASE
*db
, MSICOLUMNINFO
*colinfo
,
867 for( i
=0; colinfo
&& (i
<count
); i
++ )
869 assert( (i
+1) == colinfo
[ i
].number
);
871 colinfo
[i
].offset
= colinfo
[ i
- 1 ].offset
872 + bytes_per_column( db
, &colinfo
[ i
- 1 ] );
874 colinfo
[i
].offset
= 0;
875 TRACE("column %d is [%s] with type %08x ofs %d\n",
876 colinfo
[i
].number
, debugstr_w(colinfo
[i
].colname
),
877 colinfo
[i
].type
, colinfo
[i
].offset
);
881 static UINT
get_defaulttablecolumns( MSIDATABASE
*db
, LPCWSTR name
,
882 MSICOLUMNINFO
*colinfo
, UINT
*sz
)
884 const MSICOLUMNINFO
*p
;
887 TRACE("%s\n", debugstr_w(name
));
889 if (!lstrcmpW( name
, szTables
))
894 else if (!lstrcmpW( name
, szColumns
))
900 return ERROR_FUNCTION_FAILED
;
902 /* duplicate the string data so we can free it in msi_free_colinfo */
905 if (colinfo
&& (i
< *sz
) )
908 colinfo
[i
].tablename
= strdupW( p
[i
].tablename
);
909 colinfo
[i
].colname
= strdupW( p
[i
].colname
);
911 if( colinfo
&& (i
>= *sz
) )
914 table_calc_column_offsets( db
, colinfo
, n
);
916 return ERROR_SUCCESS
;
919 static void msi_free_colinfo( MSICOLUMNINFO
*colinfo
, UINT count
)
923 for( i
=0; i
<count
; i
++ )
925 msi_free( colinfo
[i
].tablename
);
926 msi_free( colinfo
[i
].colname
);
927 msi_free( colinfo
[i
].hash_table
);
931 static LPWSTR
msi_makestring( const MSIDATABASE
*db
, UINT stringid
)
933 return strdupW(msi_string_lookup_id( db
->strings
, stringid
));
936 static UINT
read_table_int(BYTE
*const *data
, UINT row
, UINT col
, UINT bytes
)
940 for (i
= 0; i
< bytes
; i
++)
941 ret
+= (data
[row
][col
+ i
] << i
* 8);
946 static UINT
get_tablecolumns( MSIDATABASE
*db
,
947 LPCWSTR szTableName
, MSICOLUMNINFO
*colinfo
, UINT
*sz
)
949 UINT r
, i
, n
=0, table_id
, count
, maxcount
= *sz
;
950 MSITABLE
*table
= NULL
;
952 TRACE("%s\n", debugstr_w(szTableName
));
954 /* first check if there is a default table with that name */
955 r
= get_defaulttablecolumns( db
, szTableName
, colinfo
, sz
);
956 if( ( r
== ERROR_SUCCESS
) && *sz
)
959 r
= get_table( db
, szColumns
, &table
);
960 if( r
!= ERROR_SUCCESS
)
962 ERR("couldn't load _Columns table\n");
963 return ERROR_FUNCTION_FAILED
;
966 /* convert table and column names to IDs from the string table */
967 r
= msi_string2idW( db
->strings
, szTableName
, &table_id
);
968 if( r
!= ERROR_SUCCESS
)
970 WARN("Couldn't find id for %s\n", debugstr_w(szTableName
));
974 TRACE("Table id is %d, row count is %d\n", table_id
, table
->row_count
);
976 /* Note: _Columns table doesn't have non-persistent data */
978 /* if maxcount is non-zero, assume it's exactly right for this table */
979 memset( colinfo
, 0, maxcount
*sizeof(*colinfo
) );
980 count
= table
->row_count
;
981 for( i
=0; i
<count
; i
++ )
983 if( read_table_int(table
->data
, i
, 0, db
->bytes_per_strref
) != table_id
)
987 UINT id
= read_table_int(table
->data
, i
, table
->colinfo
[2].offset
, db
->bytes_per_strref
);
988 UINT col
= read_table_int(table
->data
, i
, table
->colinfo
[1].offset
, sizeof(USHORT
)) - (1<<15);
990 /* check the column number is in range */
991 if (col
<1 || col
>maxcount
)
993 ERR("column %d out of range\n", col
);
997 /* check if this column was already set */
998 if (colinfo
[ col
- 1 ].number
)
1000 ERR("duplicate column %d\n", col
);
1004 colinfo
[ col
- 1 ].tablename
= msi_makestring( db
, table_id
);
1005 colinfo
[ col
- 1 ].number
= col
;
1006 colinfo
[ col
- 1 ].colname
= msi_makestring( db
, id
);
1007 colinfo
[ col
- 1 ].type
= read_table_int(table
->data
, i
,
1008 table
->colinfo
[3].offset
,
1009 sizeof(USHORT
)) - (1<<15);
1010 colinfo
[ col
- 1 ].offset
= 0;
1011 colinfo
[ col
- 1 ].ref_count
= 0;
1012 colinfo
[ col
- 1 ].hash_table
= NULL
;
1017 TRACE("%s has %d columns\n", debugstr_w(szTableName
), n
);
1019 if (colinfo
&& n
!= maxcount
)
1021 ERR("missing column in table %s\n", debugstr_w(szTableName
));
1022 msi_free_colinfo(colinfo
, maxcount
);
1023 return ERROR_FUNCTION_FAILED
;
1026 table_calc_column_offsets( db
, colinfo
, n
);
1029 return ERROR_SUCCESS
;
1032 static void msi_update_table_columns( MSIDATABASE
*db
, LPCWSTR name
)
1035 UINT size
, offset
, old_count
;
1038 table
= find_cached_table( db
, name
);
1039 old_count
= table
->col_count
;
1040 msi_free( table
->colinfo
);
1041 table_get_column_info( db
, name
, &table
->colinfo
, &table
->col_count
);
1043 if (!table
->col_count
)
1046 size
= msi_table_get_row_size( db
, table
->colinfo
, table
->col_count
);
1047 offset
= table
->colinfo
[table
->col_count
- 1].offset
;
1049 for ( n
= 0; n
< table
->row_count
; n
++ )
1051 table
->data
[n
] = msi_realloc( table
->data
[n
], size
);
1052 if (old_count
< table
->col_count
)
1053 memset( &table
->data
[n
][offset
], 0, size
- offset
);
1057 /* try to find the table name in the _Tables table */
1058 BOOL
TABLE_Exists( MSIDATABASE
*db
, LPCWSTR name
)
1060 UINT r
, table_id
= 0, i
, count
;
1061 MSITABLE
*table
= NULL
;
1063 static const WCHAR szStreams
[] = {'_','S','t','r','e','a','m','s',0};
1064 static const WCHAR szStorages
[] = {'_','S','t','o','r','a','g','e','s',0};
1066 if( !lstrcmpW( name
, szTables
) || !lstrcmpW( name
, szColumns
) ||
1067 !lstrcmpW( name
, szStreams
) || !lstrcmpW( name
, szStorages
) )
1070 r
= msi_string2idW( db
->strings
, name
, &table_id
);
1071 if( r
!= ERROR_SUCCESS
)
1073 TRACE("Couldn't find id for %s\n", debugstr_w(name
));
1077 r
= get_table( db
, szTables
, &table
);
1078 if( r
!= ERROR_SUCCESS
)
1080 ERR("table %s not available\n", debugstr_w(szTables
));
1084 count
= table
->row_count
;
1085 for( i
=0; i
<count
; i
++ )
1086 if( table
->data
[ i
][ 0 ] == table_id
)
1089 count
= table
->nonpersistent_row_count
;
1090 for( i
=0; i
<count
; i
++ )
1091 if( table
->nonpersistent_data
[ i
][ 0 ] == table_id
)
1097 /* below is the query interface to a table */
1099 typedef struct tagMSITABLEVIEW
1104 MSICOLUMNINFO
*columns
;
1105 MSIORDERINFO
*order
;
1111 static UINT
TABLE_fetch_int( struct tagMSIVIEW
*view
, UINT row
, UINT col
, UINT
*val
)
1113 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1118 return ERROR_INVALID_PARAMETER
;
1120 if( (col
==0) || (col
>tv
->num_cols
) )
1121 return ERROR_INVALID_PARAMETER
;
1123 /* how many rows are there ? */
1124 if( row
>= tv
->table
->row_count
+ tv
->table
->nonpersistent_row_count
)
1125 return ERROR_NO_MORE_ITEMS
;
1127 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1129 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1130 ERR("%p %p\n", tv
, tv
->columns
);
1131 return ERROR_FUNCTION_FAILED
;
1135 row
= tv
->order
->reorder
[row
];
1137 if (row
>= tv
->table
->row_count
)
1139 row
-= tv
->table
->row_count
;
1140 data
= tv
->table
->nonpersistent_data
;
1143 data
= tv
->table
->data
;
1145 n
= bytes_per_column( tv
->db
, &tv
->columns
[col
-1] );
1146 if (n
!= 2 && n
!= 3 && n
!= 4)
1148 ERR("oops! what is %d bytes per column?\n", n
);
1149 return ERROR_FUNCTION_FAILED
;
1152 offset
= tv
->columns
[col
-1].offset
;
1153 *val
= read_table_int(data
, row
, offset
, n
);
1155 /* TRACE("Data [%d][%d] = %d\n", row, col, *val ); */
1157 return ERROR_SUCCESS
;
1161 * We need a special case for streams, as we need to reference column with
1162 * the name of the stream in the same table, and the table name
1163 * which may not be available at higher levels of the query
1165 static UINT
TABLE_fetch_stream( struct tagMSIVIEW
*view
, UINT row
, UINT col
, IStream
**stm
)
1167 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1168 UINT ival
= 0, refcol
= 0, r
;
1172 static const WCHAR szDot
[] = { '.', 0 };
1175 if( !view
->ops
->fetch_int
)
1176 return ERROR_INVALID_PARAMETER
;
1179 * The column marked with the type stream data seems to have a single number
1180 * which references the column containing the name of the stream data
1182 * Fetch the column to reference first.
1184 r
= view
->ops
->fetch_int( view
, row
, col
, &ival
);
1185 if( r
!= ERROR_SUCCESS
)
1188 /* check the column value is in range */
1189 if (ival
> tv
->num_cols
|| ival
== col
)
1191 ERR("bad column ref (%u) for stream\n", ival
);
1192 return ERROR_FUNCTION_FAILED
;
1195 if ( tv
->columns
[ival
- 1].type
& MSITYPE_STRING
)
1197 /* now get the column with the name of the stream */
1198 r
= view
->ops
->fetch_int( view
, row
, ival
, &refcol
);
1199 if ( r
!= ERROR_SUCCESS
)
1202 /* lookup the string value from the string table */
1203 sval
= msi_string_lookup_id( tv
->db
->strings
, refcol
);
1205 return ERROR_INVALID_PARAMETER
;
1209 static const WCHAR fmt
[] = { '%','d',0 };
1210 sprintfW( number
, fmt
, ival
);
1214 len
= lstrlenW( tv
->name
) + 2 + lstrlenW( sval
);
1215 full_name
= msi_alloc( len
*sizeof(WCHAR
) );
1216 lstrcpyW( full_name
, tv
->name
);
1217 lstrcatW( full_name
, szDot
);
1218 lstrcatW( full_name
, sval
);
1220 r
= db_get_raw_stream( tv
->db
, full_name
, stm
);
1222 ERR("fetching stream %s, error = %d\n",debugstr_w(full_name
), r
);
1223 msi_free( full_name
);
1228 static UINT
TABLE_set_int( MSITABLEVIEW
*tv
, UINT row
, UINT col
, UINT val
)
1234 return ERROR_INVALID_PARAMETER
;
1236 if( (col
==0) || (col
>tv
->num_cols
) )
1237 return ERROR_INVALID_PARAMETER
;
1239 if( row
>= tv
->table
->row_count
+ tv
->table
->nonpersistent_row_count
)
1240 return ERROR_INVALID_PARAMETER
;
1242 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1244 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1245 ERR("%p %p\n", tv
, tv
->columns
);
1246 return ERROR_FUNCTION_FAILED
;
1249 msi_free( tv
->columns
[col
-1].hash_table
);
1250 tv
->columns
[col
-1].hash_table
= NULL
;
1252 if (row
>= tv
->table
->row_count
)
1254 row
-= tv
->table
->row_count
;
1255 data
= tv
->table
->nonpersistent_data
;
1258 data
= tv
->table
->data
;
1260 n
= bytes_per_column( tv
->db
, &tv
->columns
[col
-1] );
1261 if ( n
!= 2 && n
!= 3 && n
!= 4 )
1263 ERR("oops! what is %d bytes per column?\n", n
);
1264 return ERROR_FUNCTION_FAILED
;
1267 offset
= tv
->columns
[col
-1].offset
;
1268 for ( i
= 0; i
< n
; i
++ )
1269 data
[row
][offset
+ i
] = (val
>> i
* 8) & 0xff;
1271 return ERROR_SUCCESS
;
1274 static UINT
TABLE_get_row( struct tagMSIVIEW
*view
, UINT row
, MSIRECORD
**rec
)
1276 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1279 return ERROR_INVALID_PARAMETER
;
1282 row
= tv
->order
->reorder
[row
];
1284 return msi_view_get_row(tv
->db
, view
, row
, rec
);
1287 static UINT
TABLE_set_row( struct tagMSIVIEW
*view
, UINT row
, MSIRECORD
*rec
, UINT mask
)
1289 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1290 UINT i
, val
, r
= ERROR_SUCCESS
;
1293 return ERROR_INVALID_PARAMETER
;
1295 /* test if any of the mask bits are invalid */
1296 if ( mask
>= (1<<tv
->num_cols
) )
1297 return ERROR_INVALID_PARAMETER
;
1299 for ( i
= 0; i
< tv
->num_cols
; i
++ )
1303 /* only update the fields specified in the mask */
1304 if ( !(mask
&(1<<i
)) )
1307 /* if row >= tv->table->row_count then it is a non-persistent row */
1308 persistent
= (tv
->table
->persistent
!= MSICONDITION_FALSE
) &&
1309 (row
< tv
->table
->row_count
);
1310 /* FIXME: should we allow updating keys? */
1313 if ( !MSI_RecordIsNull( rec
, i
+ 1 ) )
1315 if ( MSITYPE_IS_BINARY(tv
->columns
[ i
].type
) )
1317 val
= 1; /* refers to the first key column */
1319 else if ( tv
->columns
[i
].type
& MSITYPE_STRING
)
1321 LPCWSTR sval
= MSI_RecordGetString( rec
, i
+ 1 );
1324 r
= msi_string2idW(tv
->db
->strings
, sval
, &ival
);
1325 if (r
== ERROR_SUCCESS
)
1327 TABLE_fetch_int(&tv
->view
, row
, i
+ 1, &x
);
1332 val
= msi_addstringW( tv
->db
->strings
, 0, sval
, -1, 1,
1333 persistent
? StringPersistent
: StringNonPersistent
);
1335 else if ( 2 == bytes_per_column( tv
->db
, &tv
->columns
[ i
] ) )
1337 val
= 0x8000 + MSI_RecordGetInteger( rec
, i
+ 1 );
1338 if ( val
& 0xffff0000 )
1340 ERR("field %u value %d out of range\n", i
+1, val
- 0x8000 );
1341 return ERROR_FUNCTION_FAILED
;
1346 INT ival
= MSI_RecordGetInteger( rec
, i
+ 1 );
1347 val
= ival
^ 0x80000000;
1351 r
= TABLE_set_int( tv
, row
, i
+1, val
);
1352 if ( r
!= ERROR_SUCCESS
)
1358 static UINT
table_create_new_row( struct tagMSIVIEW
*view
, UINT
*num
, BOOL temporary
)
1360 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1366 TRACE("%p %s\n", view
, temporary
? "TRUE" : "FALSE");
1369 return ERROR_INVALID_PARAMETER
;
1371 row
= msi_alloc_zero( tv
->row_size
);
1373 return ERROR_NOT_ENOUGH_MEMORY
;
1377 row_count
= &tv
->table
->nonpersistent_row_count
;
1378 data_ptr
= &tv
->table
->nonpersistent_data
;
1380 *num
= tv
->table
->row_count
+ tv
->table
->nonpersistent_row_count
;
1384 row_count
= &tv
->table
->row_count
;
1385 data_ptr
= &tv
->table
->data
;
1387 *num
= tv
->table
->row_count
;
1390 sz
= (*row_count
+ 1) * sizeof (BYTE
*);
1392 p
= msi_realloc( *data_ptr
, sz
);
1394 p
= msi_alloc( sz
);
1398 return ERROR_NOT_ENOUGH_MEMORY
;
1402 (*data_ptr
)[*row_count
] = row
;
1405 return ERROR_SUCCESS
;
1408 static UINT
TABLE_execute( struct tagMSIVIEW
*view
, MSIRECORD
*record
)
1410 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1412 TRACE("%p %p\n", tv
, record
);
1414 TRACE("There are %d columns\n", tv
->num_cols
);
1416 return ERROR_SUCCESS
;
1419 static UINT
TABLE_close( struct tagMSIVIEW
*view
)
1421 TRACE("%p\n", view
);
1423 return ERROR_SUCCESS
;
1426 static UINT
TABLE_get_dimensions( struct tagMSIVIEW
*view
, UINT
*rows
, UINT
*cols
)
1428 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1430 TRACE("%p %p %p\n", view
, rows
, cols
);
1433 *cols
= tv
->num_cols
;
1437 return ERROR_INVALID_PARAMETER
;
1438 *rows
= tv
->table
->row_count
+ tv
->table
->nonpersistent_row_count
;
1441 return ERROR_SUCCESS
;
1444 static UINT
TABLE_get_column_info( struct tagMSIVIEW
*view
,
1445 UINT n
, LPWSTR
*name
, UINT
*type
, BOOL
*temporary
)
1447 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1449 TRACE("%p %d %p %p\n", tv
, n
, name
, type
);
1451 if( ( n
== 0 ) || ( n
> tv
->num_cols
) )
1452 return ERROR_INVALID_PARAMETER
;
1456 *name
= strdupW( tv
->columns
[n
-1].colname
);
1458 return ERROR_FUNCTION_FAILED
;
1462 *type
= tv
->columns
[n
-1].type
;
1465 *temporary
= tv
->columns
[n
-1].temporary
;
1467 return ERROR_SUCCESS
;
1470 static UINT
msi_table_find_row( MSITABLEVIEW
*tv
, MSIRECORD
*rec
, UINT
*row
);
1472 static UINT
table_validate_new( MSITABLEVIEW
*tv
, MSIRECORD
*rec
)
1476 /* check there's no null values where they're not allowed */
1477 for( i
= 0; i
< tv
->num_cols
; i
++ )
1479 if ( tv
->columns
[i
].type
& MSITYPE_NULLABLE
)
1482 if ( MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
1483 TRACE("skipping binary column\n");
1484 else if ( tv
->columns
[i
].type
& MSITYPE_STRING
)
1488 str
= MSI_RecordGetString( rec
, i
+1 );
1489 if (str
== NULL
|| str
[0] == 0)
1490 return ERROR_INVALID_DATA
;
1496 n
= MSI_RecordGetInteger( rec
, i
+1 );
1497 if (n
== MSI_NULL_INTEGER
)
1498 return ERROR_INVALID_DATA
;
1502 /* check there's no duplicate keys */
1503 r
= msi_table_find_row( tv
, rec
, &row
);
1504 if (r
== ERROR_SUCCESS
)
1505 return ERROR_FUNCTION_FAILED
;
1507 return ERROR_SUCCESS
;
1510 static UINT
TABLE_insert_row( struct tagMSIVIEW
*view
, MSIRECORD
*rec
, UINT row
, BOOL temporary
)
1512 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1513 UINT i
, r
, idx
, size
;
1516 TRACE("%p %p %s\n", tv
, rec
, temporary
? "TRUE" : "FALSE" );
1518 /* check that the key is unique - can we find a matching row? */
1519 r
= table_validate_new( tv
, rec
);
1520 if( r
!= ERROR_SUCCESS
)
1521 return ERROR_FUNCTION_FAILED
;
1523 r
= table_create_new_row( view
, &row
, temporary
);
1524 TRACE("insert_row returned %08x\n", r
);
1525 if( r
!= ERROR_SUCCESS
)
1531 data
= tv
->table
->nonpersistent_data
;
1532 size
= tv
->table
->nonpersistent_row_count
;
1533 idx
-= tv
->table
->row_count
;
1537 data
= tv
->table
->data
;
1538 size
= tv
->table
->row_count
;
1541 /* shift the rows to make room for the new row */
1542 if( idx
!= size
- 1 )
1544 for (i
= 1; i
< size
- idx
; i
++)
1545 memmove(&(data
[size
- i
][0]),
1546 &(data
[size
- i
- 1][0]), tv
->row_size
);
1549 return TABLE_set_row( view
, row
, rec
, (1<<tv
->num_cols
) - 1 );
1552 static UINT
TABLE_delete_row( struct tagMSIVIEW
*view
, UINT row
)
1554 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1555 UINT r
, num_rows
, num_cols
, i
;
1558 TRACE("%p %d\n", tv
, row
);
1561 return ERROR_INVALID_PARAMETER
;
1563 r
= TABLE_get_dimensions( view
, &num_rows
, &num_cols
);
1564 if ( r
!= ERROR_SUCCESS
)
1567 if ( row
>= num_rows
)
1568 return ERROR_FUNCTION_FAILED
;
1570 if ( row
< tv
->table
->row_count
)
1572 num_rows
= tv
->table
->row_count
;
1573 tv
->table
->row_count
--;
1574 data
= tv
->table
->data
;
1578 num_rows
= tv
->table
->nonpersistent_row_count
;
1579 row
-= tv
->table
->row_count
;
1580 tv
->table
->nonpersistent_row_count
--;
1581 data
= tv
->table
->nonpersistent_data
;
1584 /* reset the hash tables */
1585 for (i
= 0; i
< tv
->num_cols
; i
++)
1587 msi_free( tv
->columns
[i
].hash_table
);
1588 tv
->columns
[i
].hash_table
= NULL
;
1591 if ( row
== num_rows
- 1 )
1592 return ERROR_SUCCESS
;
1594 for (i
= row
+ 1; i
< num_rows
; i
++)
1595 memcpy(data
[i
- 1], data
[i
], tv
->row_size
);
1597 return ERROR_SUCCESS
;
1600 static UINT
msi_table_update(struct tagMSIVIEW
*view
, MSIRECORD
*rec
, UINT row
)
1602 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1605 /* FIXME: MsiViewFetch should set rec index 0 to some ID that
1606 * sets the fetched record apart from other records
1610 return ERROR_INVALID_PARAMETER
;
1612 r
= msi_table_find_row(tv
, rec
, &new_row
);
1613 if (r
!= ERROR_SUCCESS
)
1615 ERR("can't find row to modify\n");
1616 return ERROR_FUNCTION_FAILED
;
1619 /* the row cannot be changed */
1620 if (row
!= new_row
+ 1)
1621 return ERROR_FUNCTION_FAILED
;
1623 return TABLE_set_row(view
, new_row
, rec
, (1 << tv
->num_cols
) - 1);
1626 static UINT
modify_delete_row( struct tagMSIVIEW
*view
, MSIRECORD
*rec
)
1628 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1631 r
= msi_table_find_row(tv
, rec
, &row
);
1632 if (r
!= ERROR_SUCCESS
)
1635 return TABLE_delete_row(view
, row
);
1638 static UINT
msi_refresh_record( struct tagMSIVIEW
*view
, MSIRECORD
*rec
, UINT row
)
1643 r
= TABLE_get_row(view
, row
- 1, &curr
);
1644 if (r
!= ERROR_SUCCESS
)
1647 count
= MSI_RecordGetFieldCount(rec
);
1648 for (i
= 0; i
< count
; i
++)
1649 MSI_RecordCopyField(curr
, i
+ 1, rec
, i
+ 1);
1651 msiobj_release(&curr
->hdr
);
1652 return ERROR_SUCCESS
;
1655 static UINT
TABLE_modify( struct tagMSIVIEW
*view
, MSIMODIFY eModifyMode
,
1656 MSIRECORD
*rec
, UINT row
)
1658 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1661 TRACE("%p %d %p\n", view
, eModifyMode
, rec
);
1663 switch (eModifyMode
)
1665 case MSIMODIFY_DELETE
:
1666 r
= modify_delete_row( view
, rec
);
1668 case MSIMODIFY_VALIDATE_NEW
:
1669 r
= table_validate_new( tv
, rec
);
1672 case MSIMODIFY_INSERT
:
1673 r
= table_validate_new( tv
, rec
);
1674 if (r
!= ERROR_SUCCESS
)
1676 r
= TABLE_insert_row( view
, rec
, -1, FALSE
);
1679 case MSIMODIFY_INSERT_TEMPORARY
:
1680 r
= table_validate_new( tv
, rec
);
1681 if (r
!= ERROR_SUCCESS
)
1683 r
= TABLE_insert_row( view
, rec
, -1, TRUE
);
1686 case MSIMODIFY_REFRESH
:
1687 r
= msi_refresh_record( view
, rec
, row
);
1690 case MSIMODIFY_UPDATE
:
1691 r
= msi_table_update( view
, rec
, row
);
1694 case MSIMODIFY_ASSIGN
:
1695 case MSIMODIFY_REPLACE
:
1696 case MSIMODIFY_MERGE
:
1697 case MSIMODIFY_VALIDATE
:
1698 case MSIMODIFY_VALIDATE_FIELD
:
1699 case MSIMODIFY_VALIDATE_DELETE
:
1700 FIXME("%p %d %p - mode not implemented\n", view
, eModifyMode
, rec
);
1701 r
= ERROR_CALL_NOT_IMPLEMENTED
;
1705 r
= ERROR_INVALID_DATA
;
1711 static UINT
TABLE_delete( struct tagMSIVIEW
*view
)
1713 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1715 TRACE("%p\n", view
);
1722 msi_free( tv
->order
->reorder
);
1723 msi_free( tv
->order
);
1729 return ERROR_SUCCESS
;
1732 static UINT
TABLE_find_matching_rows( struct tagMSIVIEW
*view
, UINT col
,
1733 UINT val
, UINT
*row
, MSIITERHANDLE
*handle
)
1735 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1736 const MSICOLUMNHASHENTRY
*entry
;
1738 TRACE("%p, %d, %u, %p\n", view
, col
, val
, *handle
);
1741 return ERROR_INVALID_PARAMETER
;
1743 if( (col
==0) || (col
> tv
->num_cols
) )
1744 return ERROR_INVALID_PARAMETER
;
1746 if( !tv
->columns
[col
-1].hash_table
)
1749 UINT num_rows
= tv
->table
->row_count
+ tv
->table
->nonpersistent_row_count
;
1750 MSICOLUMNHASHENTRY
**hash_table
;
1751 MSICOLUMNHASHENTRY
*new_entry
;
1753 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1755 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1756 ERR("%p %p\n", tv
, tv
->columns
);
1757 return ERROR_FUNCTION_FAILED
;
1760 /* allocate contiguous memory for the table and its entries so we
1761 * don't have to do an expensive cleanup */
1762 hash_table
= msi_alloc(MSITABLE_HASH_TABLE_SIZE
* sizeof(MSICOLUMNHASHENTRY
*) +
1763 num_rows
* sizeof(MSICOLUMNHASHENTRY
));
1765 return ERROR_OUTOFMEMORY
;
1767 memset(hash_table
, 0, MSITABLE_HASH_TABLE_SIZE
* sizeof(MSICOLUMNHASHENTRY
*));
1768 tv
->columns
[col
-1].hash_table
= hash_table
;
1770 new_entry
= (MSICOLUMNHASHENTRY
*)(hash_table
+ MSITABLE_HASH_TABLE_SIZE
);
1772 for (i
= 0; i
< num_rows
; i
++, new_entry
++)
1776 if (view
->ops
->fetch_int( view
, i
, col
, &row_value
) != ERROR_SUCCESS
)
1779 new_entry
->next
= NULL
;
1780 new_entry
->value
= row_value
;
1782 if (hash_table
[row_value
% MSITABLE_HASH_TABLE_SIZE
])
1784 MSICOLUMNHASHENTRY
*prev_entry
= hash_table
[row_value
% MSITABLE_HASH_TABLE_SIZE
];
1785 while (prev_entry
->next
)
1786 prev_entry
= prev_entry
->next
;
1787 prev_entry
->next
= new_entry
;
1790 hash_table
[row_value
% MSITABLE_HASH_TABLE_SIZE
] = new_entry
;
1795 entry
= tv
->columns
[col
-1].hash_table
[val
% MSITABLE_HASH_TABLE_SIZE
];
1797 entry
= (*handle
)->next
;
1799 while (entry
&& entry
->value
!= val
)
1800 entry
= entry
->next
;
1804 return ERROR_NO_MORE_ITEMS
;
1808 return ERROR_SUCCESS
;
1811 static UINT
TABLE_add_ref(struct tagMSIVIEW
*view
)
1813 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1816 TRACE("%p %d\n", view
, tv
->table
->ref_count
);
1818 for (i
= 0; i
< tv
->table
->col_count
; i
++)
1820 if (tv
->table
->colinfo
[i
].type
& MSITYPE_TEMPORARY
)
1821 InterlockedIncrement(&tv
->table
->colinfo
[i
].ref_count
);
1824 return InterlockedIncrement(&tv
->table
->ref_count
);
1827 static UINT
TABLE_remove_column(struct tagMSIVIEW
*view
, LPCWSTR table
, UINT number
)
1829 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1830 MSIRECORD
*rec
= NULL
;
1831 MSIVIEW
*columns
= NULL
;
1834 rec
= MSI_CreateRecord(2);
1836 return ERROR_OUTOFMEMORY
;
1838 MSI_RecordSetStringW(rec
, 1, table
);
1839 MSI_RecordSetInteger(rec
, 2, number
);
1841 r
= TABLE_CreateView(tv
->db
, szColumns
, &columns
);
1842 if (r
!= ERROR_SUCCESS
)
1845 r
= msi_table_find_row((MSITABLEVIEW
*)columns
, rec
, &row
);
1846 if (r
!= ERROR_SUCCESS
)
1849 r
= TABLE_delete_row(columns
, row
);
1850 if (r
!= ERROR_SUCCESS
)
1853 msi_update_table_columns(tv
->db
, table
);
1856 msiobj_release(&rec
->hdr
);
1857 columns
->ops
->delete(columns
);
1861 static UINT
TABLE_release(struct tagMSIVIEW
*view
)
1863 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1864 INT ref
= tv
->table
->ref_count
;
1867 TRACE("%p %d\n", view
, ref
);
1869 for (i
= 0; i
< tv
->table
->col_count
; i
++)
1871 if (tv
->table
->colinfo
[i
].type
& MSITYPE_TEMPORARY
)
1873 ref
= InterlockedDecrement(&tv
->table
->colinfo
[i
].ref_count
);
1876 r
= TABLE_remove_column(view
, tv
->table
->colinfo
[i
].tablename
,
1877 tv
->table
->colinfo
[i
].number
);
1878 if (r
!= ERROR_SUCCESS
)
1884 ref
= InterlockedDecrement(&tv
->table
->ref_count
);
1887 if (!tv
->table
->row_count
)
1889 list_remove(&tv
->table
->entry
);
1890 free_table(tv
->table
);
1898 static UINT
TABLE_add_column(struct tagMSIVIEW
*view
, LPCWSTR table
, UINT number
,
1899 LPCWSTR column
, UINT type
, BOOL hold
)
1901 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1906 rec
= MSI_CreateRecord(4);
1908 return ERROR_OUTOFMEMORY
;
1910 MSI_RecordSetStringW(rec
, 1, table
);
1911 MSI_RecordSetInteger(rec
, 2, number
);
1912 MSI_RecordSetStringW(rec
, 3, column
);
1913 MSI_RecordSetInteger(rec
, 4, type
);
1915 r
= TABLE_insert_row(&tv
->view
, rec
, -1, FALSE
);
1916 if (r
!= ERROR_SUCCESS
)
1919 msi_update_table_columns(tv
->db
, table
);
1924 msitable
= find_cached_table(tv
->db
, table
);
1925 for (i
= 0; i
< msitable
->col_count
; i
++)
1927 if (!lstrcmpW(msitable
->colinfo
[i
].colname
, column
))
1929 InterlockedIncrement(&msitable
->colinfo
[i
].ref_count
);
1935 msiobj_release(&rec
->hdr
);
1939 static UINT
order_add_column(struct tagMSIVIEW
*view
, MSIORDERINFO
*order
, LPCWSTR name
)
1943 r
= TABLE_get_dimensions(view
, NULL
, &count
);
1944 if (r
!= ERROR_SUCCESS
)
1947 if (order
->num_cols
>= count
)
1948 return ERROR_FUNCTION_FAILED
;
1950 r
= VIEW_find_column(view
, name
, &n
);
1951 if (r
!= ERROR_SUCCESS
)
1954 order
->cols
[order
->num_cols
] = n
;
1955 TRACE("Ordering by column %s (%d)\n", debugstr_w(name
), n
);
1959 return ERROR_SUCCESS
;
1962 static UINT
order_compare(struct tagMSIVIEW
*view
, MSIORDERINFO
*order
,
1963 UINT a
, UINT b
, UINT
*swap
)
1965 UINT r
, i
, a_val
= 0, b_val
= 0;
1968 for (i
= 0; i
< order
->num_cols
; i
++)
1970 r
= TABLE_fetch_int(view
, a
, order
->cols
[i
], &a_val
);
1971 if (r
!= ERROR_SUCCESS
)
1974 r
= TABLE_fetch_int(view
, b
, order
->cols
[i
], &b_val
);
1975 if (r
!= ERROR_SUCCESS
)
1986 return ERROR_SUCCESS
;
1989 static UINT
order_mergesort(struct tagMSIVIEW
*view
, MSIORDERINFO
*order
,
1990 UINT left
, UINT right
)
1993 UINT swap
= 0, center
= (left
+ right
) / 2;
1994 UINT
*array
= order
->reorder
;
1997 return ERROR_SUCCESS
;
1999 /* sort the left half */
2000 r
= order_mergesort(view
, order
, left
, center
);
2001 if (r
!= ERROR_SUCCESS
)
2004 /* sort the right half */
2005 r
= order_mergesort(view
, order
, center
+ 1, right
);
2006 if (r
!= ERROR_SUCCESS
)
2009 for (i
= left
, j
= center
+ 1; (i
<= center
) && (j
<= right
); i
++)
2011 r
= order_compare(view
, order
, array
[i
], array
[j
], &swap
);
2012 if (r
!= ERROR_SUCCESS
)
2018 memmove(&array
[i
+ 1], &array
[i
], (j
- i
) * sizeof(UINT
));
2025 return ERROR_SUCCESS
;
2028 static UINT
order_verify(struct tagMSIVIEW
*view
, MSIORDERINFO
*order
, UINT num_rows
)
2032 for (i
= 1; i
< num_rows
; i
++)
2034 r
= order_compare(view
, order
, order
->reorder
[i
- 1],
2035 order
->reorder
[i
], &swap
);
2036 if (r
!= ERROR_SUCCESS
)
2042 ERR("Bad order! %d\n", i
);
2043 return ERROR_FUNCTION_FAILED
;
2046 return ERROR_SUCCESS
;
2049 static UINT
TABLE_sort(struct tagMSIVIEW
*view
, column_info
*columns
)
2051 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
2052 MSIORDERINFO
*order
;
2057 TRACE("sorting table %s\n", debugstr_w(tv
->name
));
2059 r
= TABLE_get_dimensions(view
, &rows
, &cols
);
2060 if (r
!= ERROR_SUCCESS
)
2064 return ERROR_SUCCESS
;
2066 order
= msi_alloc_zero(sizeof(MSIORDERINFO
) + sizeof(UINT
) * cols
);
2068 return ERROR_OUTOFMEMORY
;
2070 for (ptr
= columns
; ptr
; ptr
= ptr
->next
)
2071 order_add_column(view
, order
, ptr
->column
);
2073 order
->reorder
= msi_alloc(rows
* sizeof(UINT
));
2074 if (!order
->reorder
)
2075 return ERROR_OUTOFMEMORY
;
2077 for (i
= 0; i
< rows
; i
++)
2078 order
->reorder
[i
] = i
;
2080 r
= order_mergesort(view
, order
, 0, rows
- 1);
2081 if (r
!= ERROR_SUCCESS
)
2084 r
= order_verify(view
, order
, rows
);
2085 if (r
!= ERROR_SUCCESS
)
2090 return ERROR_SUCCESS
;
2093 static UINT
TABLE_drop(struct tagMSIVIEW
*view
)
2095 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
2096 MSIVIEW
*tables
= NULL
;
2097 MSIRECORD
*rec
= NULL
;
2101 TRACE("dropping table %s\n", debugstr_w(tv
->name
));
2103 for (i
= tv
->table
->col_count
- 1; i
>= 0; i
--)
2105 r
= TABLE_remove_column(view
, tv
->table
->colinfo
[i
].tablename
,
2106 tv
->table
->colinfo
[i
].number
);
2107 if (r
!= ERROR_SUCCESS
)
2111 rec
= MSI_CreateRecord(1);
2113 return ERROR_OUTOFMEMORY
;
2115 MSI_RecordSetStringW(rec
, 1, tv
->name
);
2117 r
= TABLE_CreateView(tv
->db
, szTables
, &tables
);
2118 if (r
!= ERROR_SUCCESS
)
2121 r
= msi_table_find_row((MSITABLEVIEW
*)tables
, rec
, &row
);
2122 if (r
!= ERROR_SUCCESS
)
2125 r
= TABLE_delete_row(tables
, row
);
2126 if (r
!= ERROR_SUCCESS
)
2129 list_remove(&tv
->table
->entry
);
2130 free_table(tv
->table
);
2134 msiobj_release(&rec
->hdr
);
2135 tables
->ops
->delete(tables
);
2140 static const MSIVIEWOPS table_ops
=
2150 TABLE_get_dimensions
,
2151 TABLE_get_column_info
,
2154 TABLE_find_matching_rows
,
2158 TABLE_remove_column
,
2163 UINT
TABLE_CreateView( MSIDATABASE
*db
, LPCWSTR name
, MSIVIEW
**view
)
2168 static const WCHAR Streams
[] = {'_','S','t','r','e','a','m','s',0};
2169 static const WCHAR Storages
[] = {'_','S','t','o','r','a','g','e','s',0};
2171 TRACE("%p %s %p\n", db
, debugstr_w(name
), view
);
2173 if ( !lstrcmpW( name
, Streams
) )
2174 return STREAMS_CreateView( db
, view
);
2175 else if ( !lstrcmpW( name
, Storages
) )
2176 return STORAGES_CreateView( db
, view
);
2178 sz
= sizeof *tv
+ lstrlenW(name
)*sizeof name
[0] ;
2179 tv
= msi_alloc_zero( sz
);
2181 return ERROR_FUNCTION_FAILED
;
2183 r
= get_table( db
, name
, &tv
->table
);
2184 if( r
!= ERROR_SUCCESS
)
2187 WARN("table not found\n");
2191 TRACE("table %p found with %d columns\n", tv
->table
, tv
->table
->col_count
);
2193 /* fill the structure */
2194 tv
->view
.ops
= &table_ops
;
2196 tv
->columns
= tv
->table
->colinfo
;
2197 tv
->num_cols
= tv
->table
->col_count
;
2198 tv
->row_size
= msi_table_get_row_size( db
, tv
->table
->colinfo
, tv
->table
->col_count
);
2200 TRACE("%s one row is %d bytes\n", debugstr_w(name
), tv
->row_size
);
2202 *view
= (MSIVIEW
*) tv
;
2203 lstrcpyW( tv
->name
, name
);
2205 return ERROR_SUCCESS
;
2208 UINT
MSI_CommitTables( MSIDATABASE
*db
)
2211 MSITABLE
*table
= NULL
;
2215 r
= msi_save_string_table( db
->strings
, db
->storage
);
2216 if( r
!= ERROR_SUCCESS
)
2218 WARN("failed to save string table r=%08x\n",r
);
2222 LIST_FOR_EACH_ENTRY( table
, &db
->tables
, MSITABLE
, entry
)
2224 r
= save_table( db
, table
);
2225 if( r
!= ERROR_SUCCESS
)
2227 WARN("failed to save table %s (r=%08x)\n",
2228 debugstr_w(table
->name
), r
);
2233 /* force everything to reload next time */
2234 free_cached_tables( db
);
2236 return ERROR_SUCCESS
;
2239 MSICONDITION
MSI_DatabaseIsTablePersistent( MSIDATABASE
*db
, LPCWSTR table
)
2244 TRACE("%p %s\n", db
, debugstr_w(table
));
2247 return MSICONDITION_ERROR
;
2249 r
= get_table( db
, table
, &t
);
2250 if (r
!= ERROR_SUCCESS
)
2251 return MSICONDITION_NONE
;
2253 return t
->persistent
;
2256 static UINT
read_raw_int(const BYTE
*data
, UINT col
, UINT bytes
)
2260 for (i
= 0; i
< bytes
; i
++)
2261 ret
+= (data
[col
+ i
] << i
* 8);
2266 static MSIRECORD
*msi_get_transform_record( const MSITABLEVIEW
*tv
, const string_table
*st
,
2267 const BYTE
*rawdata
, UINT bytes_per_strref
)
2269 UINT i
, val
, ofs
= 0;
2271 MSICOLUMNINFO
*columns
= tv
->columns
;
2274 mask
= rawdata
[0] | (rawdata
[1] << 8);
2277 rec
= MSI_CreateRecord( tv
->num_cols
);
2282 for( i
=0; i
<tv
->num_cols
; i
++ )
2284 if ( (mask
&1) && (i
>=(mask
>>8)) )
2286 /* all keys must be present */
2287 if ( (~mask
&1) && (~columns
[i
].type
& MSITYPE_KEY
) && ((1<<i
) & ~mask
) )
2290 if( (columns
[i
].type
& MSITYPE_STRING
) &&
2291 ! MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
2295 val
= read_raw_int(rawdata
, ofs
, bytes_per_strref
);
2296 sval
= msi_string_lookup_id( st
, val
);
2297 MSI_RecordSetStringW( rec
, i
+1, sval
);
2298 TRACE(" field %d [%s]\n", i
+1, debugstr_w(sval
));
2299 ofs
+= bytes_per_strref
;
2303 UINT n
= bytes_per_column( tv
->db
, &columns
[i
] );
2307 val
= read_raw_int(rawdata
, ofs
, n
);
2309 MSI_RecordSetInteger( rec
, i
+1, val
^0x8000 );
2310 TRACE(" field %d [0x%04x]\n", i
+1, val
);
2313 val
= read_raw_int(rawdata
, ofs
, n
);
2315 MSI_RecordSetInteger( rec
, i
+1, val
^0x80000000 );
2316 TRACE(" field %d [0x%08x]\n", i
+1, val
);
2319 ERR("oops - unknown column width %d\n", n
);
2328 static void dump_record( MSIRECORD
*rec
)
2332 n
= MSI_RecordGetFieldCount( rec
);
2333 for( i
=1; i
<=n
; i
++ )
2335 LPCWSTR sval
= MSI_RecordGetString( rec
, i
);
2337 if( MSI_RecordIsNull( rec
, i
) )
2338 TRACE("row -> []\n");
2339 else if( (sval
= MSI_RecordGetString( rec
, i
)) )
2340 TRACE("row -> [%s]\n", debugstr_w(sval
));
2342 TRACE("row -> [0x%08x]\n", MSI_RecordGetInteger( rec
, i
) );
2346 static void dump_table( const string_table
*st
, const USHORT
*rawdata
, UINT rawsize
)
2351 for( i
=0; i
<(rawsize
/2); i
++ )
2353 sval
= msi_string_lookup_id( st
, rawdata
[i
] );
2354 MESSAGE(" %04x %s\n", rawdata
[i
], debugstr_w(sval
) );
2358 static UINT
* msi_record_to_row( const MSITABLEVIEW
*tv
, MSIRECORD
*rec
)
2363 data
= msi_alloc( tv
->num_cols
*sizeof (UINT
) );
2364 for( i
=0; i
<tv
->num_cols
; i
++ )
2368 if ( ~tv
->columns
[i
].type
& MSITYPE_KEY
)
2371 /* turn the transform column value into a row value */
2372 if ( ( tv
->columns
[i
].type
& MSITYPE_STRING
) &&
2373 ! MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
2375 str
= MSI_RecordGetString( rec
, i
+1 );
2376 r
= msi_string2idW( tv
->db
->strings
, str
, &data
[i
] );
2378 /* if there's no matching string in the string table,
2379 these keys can't match any record, so fail now. */
2380 if( ERROR_SUCCESS
!= r
)
2388 data
[i
] = MSI_RecordGetInteger( rec
, i
+1 );
2390 if (data
[i
] == MSI_NULL_INTEGER
)
2392 else if ((tv
->columns
[i
].type
&0xff) == 2)
2395 data
[i
] += 0x80000000;
2401 static UINT
msi_row_matches( MSITABLEVIEW
*tv
, UINT row
, const UINT
*data
)
2403 UINT i
, r
, x
, ret
= ERROR_FUNCTION_FAILED
;
2405 for( i
=0; i
<tv
->num_cols
; i
++ )
2407 if ( ~tv
->columns
[i
].type
& MSITYPE_KEY
)
2410 /* turn the transform column value into a row value */
2411 r
= TABLE_fetch_int( &tv
->view
, row
, i
+1, &x
);
2412 if ( r
!= ERROR_SUCCESS
)
2414 ERR("TABLE_fetch_int shouldn't fail here\n");
2418 /* if this key matches, move to the next column */
2421 ret
= ERROR_FUNCTION_FAILED
;
2425 ret
= ERROR_SUCCESS
;
2431 static UINT
msi_table_find_row( MSITABLEVIEW
*tv
, MSIRECORD
*rec
, UINT
*row
)
2433 UINT i
, r
= ERROR_FUNCTION_FAILED
, *data
;
2435 data
= msi_record_to_row( tv
, rec
);
2438 for( i
= 0; i
< tv
->table
->row_count
+ tv
->table
->nonpersistent_row_count
; i
++ )
2440 r
= msi_row_matches( tv
, i
, data
);
2441 if( r
== ERROR_SUCCESS
)
2457 static UINT
msi_table_load_transform( MSIDATABASE
*db
, IStorage
*stg
,
2458 string_table
*st
, TRANSFORMDATA
*transform
,
2459 UINT bytes_per_strref
)
2462 BYTE
*rawdata
= NULL
;
2463 MSITABLEVIEW
*tv
= NULL
;
2464 UINT r
, n
, sz
, i
, mask
;
2465 MSIRECORD
*rec
= NULL
;
2471 return ERROR_SUCCESS
;
2473 name
= transform
->name
;
2476 TRACE("%p %p %p %s\n", db
, stg
, st
, debugstr_w(name
) );
2478 /* read the transform data */
2479 read_stream_data( stg
, name
, TRUE
, &rawdata
, &rawsize
);
2482 TRACE("table %s empty\n", debugstr_w(name
) );
2483 return ERROR_INVALID_TABLE
;
2486 /* create a table view */
2487 r
= TABLE_CreateView( db
, name
, (MSIVIEW
**) &tv
);
2488 if( r
!= ERROR_SUCCESS
)
2491 r
= tv
->view
.ops
->execute( &tv
->view
, NULL
);
2492 if( r
!= ERROR_SUCCESS
)
2495 TRACE("name = %s columns = %u row_size = %u raw size = %u\n",
2496 debugstr_w(name
), tv
->num_cols
, tv
->row_size
, rawsize
);
2498 /* interpret the data */
2500 for( n
=0; n
< rawsize
; )
2502 mask
= rawdata
[n
] | (rawdata
[n
+1] << 8);
2507 * if the low bit is set, columns are continuous and
2508 * the number of columns is specified in the high byte
2511 for( i
=0; i
<tv
->num_cols
; i
++ )
2513 if( (tv
->columns
[i
].type
& MSITYPE_STRING
) &&
2514 ! MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
2515 sz
+= bytes_per_strref
;
2517 sz
+= bytes_per_column( tv
->db
, &tv
->columns
[i
] );
2523 * If the low bit is not set, mask is a bitmask.
2524 * Excepting for key fields, which are always present,
2525 * each bit indicates that a field is present in the transform record.
2527 * mask == 0 is a special case ... only the keys will be present
2528 * and it means that this row should be deleted.
2531 for( i
=0; i
<tv
->num_cols
; i
++ )
2533 if( (tv
->columns
[i
].type
& MSITYPE_KEY
) || ((1<<i
)&mask
))
2535 if( (tv
->columns
[i
].type
& MSITYPE_STRING
) &&
2536 ! MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
2537 sz
+= bytes_per_strref
;
2539 sz
+= bytes_per_column( tv
->db
, &tv
->columns
[i
] );
2544 /* check we didn't run of the end of the table */
2545 if ( (n
+sz
) > rawsize
)
2548 dump_table( st
, (USHORT
*)rawdata
, rawsize
);
2552 rec
= msi_get_transform_record( tv
, st
, &rawdata
[n
], bytes_per_strref
);
2559 UINT number
= MSI_NULL_INTEGER
;
2561 TRACE("inserting record\n");
2563 if (!lstrcmpW(name
, szColumns
))
2565 MSI_RecordGetStringW( rec
, 1, table
, &sz
);
2566 number
= MSI_RecordGetInteger( rec
, 2 );
2569 * Native msi seems writes nul into the Number (2nd) column of
2570 * the _Columns table, only when the columns are from a new table
2572 if ( number
== MSI_NULL_INTEGER
)
2574 /* reset the column number on a new table */
2575 if ( lstrcmpW(coltable
, table
) )
2578 lstrcpyW( coltable
, table
);
2581 /* fix nul column numbers */
2582 MSI_RecordSetInteger( rec
, 2, ++colcol
);
2586 r
= TABLE_insert_row( &tv
->view
, rec
, -1, FALSE
);
2587 if (r
!= ERROR_SUCCESS
)
2588 ERR("insert row failed\n");
2590 if ( number
!= MSI_NULL_INTEGER
&& !lstrcmpW(name
, szColumns
) )
2591 msi_update_table_columns( db
, table
);
2597 r
= msi_table_find_row( tv
, rec
, &row
);
2598 if (r
!= ERROR_SUCCESS
)
2599 ERR("no matching row to transform\n");
2602 TRACE("modifying row [%d]:\n", row
);
2603 TABLE_set_row( &tv
->view
, row
, rec
, mask
);
2607 TRACE("deleting row [%d]:\n", row
);
2608 TABLE_delete_row( &tv
->view
, row
);
2611 if( TRACE_ON(msidb
) ) dump_record( rec
);
2612 msiobj_release( &rec
->hdr
);
2619 /* no need to free the table, it's associated with the database */
2620 msi_free( rawdata
);
2622 tv
->view
.ops
->delete( &tv
->view
);
2624 return ERROR_SUCCESS
;
2628 * msi_table_apply_transform
2630 * Enumerate the table transforms in a transform storage and apply each one.
2632 UINT
msi_table_apply_transform( MSIDATABASE
*db
, IStorage
*stg
)
2634 struct list transforms
;
2635 IEnumSTATSTG
*stgenum
= NULL
;
2636 TRANSFORMDATA
*transform
;
2637 TRANSFORMDATA
*tables
= NULL
, *columns
= NULL
;
2640 string_table
*strings
;
2641 UINT ret
= ERROR_FUNCTION_FAILED
;
2642 UINT bytes_per_strref
;
2644 TRACE("%p %p\n", db
, stg
);
2646 strings
= msi_load_string_table( stg
, &bytes_per_strref
);
2650 r
= IStorage_EnumElements( stg
, 0, NULL
, 0, &stgenum
);
2654 list_init(&transforms
);
2658 MSITABLEVIEW
*tv
= NULL
;
2662 r
= IEnumSTATSTG_Next( stgenum
, 1, &stat
, &count
);
2663 if ( FAILED( r
) || !count
)
2666 decode_streamname( stat
.pwcsName
, name
);
2667 CoTaskMemFree( stat
.pwcsName
);
2668 if ( name
[0] != 0x4840 )
2671 if ( !lstrcmpW( name
+1, szStringPool
) ||
2672 !lstrcmpW( name
+1, szStringData
) )
2675 transform
= msi_alloc_zero( sizeof(TRANSFORMDATA
) );
2679 list_add_tail( &transforms
, &transform
->entry
);
2681 transform
->name
= strdupW( name
+ 1 );
2683 if ( !lstrcmpW( transform
->name
, szTables
) )
2685 else if (!lstrcmpW( transform
->name
, szColumns
) )
2686 columns
= transform
;
2688 TRACE("transform contains stream %s\n", debugstr_w(name
));
2690 /* load the table */
2691 r
= TABLE_CreateView( db
, transform
->name
, (MSIVIEW
**) &tv
);
2692 if( r
!= ERROR_SUCCESS
)
2695 r
= tv
->view
.ops
->execute( &tv
->view
, NULL
);
2696 if( r
!= ERROR_SUCCESS
)
2698 tv
->view
.ops
->delete( &tv
->view
);
2702 tv
->view
.ops
->delete( &tv
->view
);
2706 * Apply _Tables and _Columns transforms first so that
2707 * the table metadata is correct, and empty tables exist.
2709 ret
= msi_table_load_transform( db
, stg
, strings
, tables
, bytes_per_strref
);
2710 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_INVALID_TABLE
)
2713 ret
= msi_table_load_transform( db
, stg
, strings
, columns
, bytes_per_strref
);
2714 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_INVALID_TABLE
)
2717 ret
= ERROR_SUCCESS
;
2719 while ( !list_empty( &transforms
) )
2721 transform
= LIST_ENTRY( list_head( &transforms
), TRANSFORMDATA
, entry
);
2723 if ( lstrcmpW( transform
->name
, szColumns
) &&
2724 lstrcmpW( transform
->name
, szTables
) &&
2725 ret
== ERROR_SUCCESS
)
2727 ret
= msi_table_load_transform( db
, stg
, strings
, transform
, bytes_per_strref
);
2730 list_remove( &transform
->entry
);
2731 msi_free( transform
->name
);
2732 msi_free( transform
);
2735 if ( ret
== ERROR_SUCCESS
)
2736 append_storage_to_db( db
, stg
);
2740 IEnumSTATSTG_Release( stgenum
);
2742 msi_destroy_stringtable( strings
);
2747 void append_storage_to_db( MSIDATABASE
*db
, IStorage
*stg
)
2751 t
= msi_alloc( sizeof *t
);
2753 IStorage_AddRef( stg
);
2754 list_add_tail( &db
->transforms
, &t
->entry
);
2757 void msi_free_transforms( MSIDATABASE
*db
)
2759 while( !list_empty( &db
->transforms
) )
2761 MSITRANSFORM
*t
= LIST_ENTRY( list_head( &db
->transforms
),
2762 MSITRANSFORM
, entry
);
2763 list_remove( &t
->entry
);
2764 IStorage_Release( t
->stg
);