2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2002-2005 Mike McCormack for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
39 #include "wine/debug.h"
40 #include "wine/unicode.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(msidb
);
44 #define MSITABLE_HASH_TABLE_SIZE 37
46 typedef struct tagMSICOLUMNHASHENTRY
48 struct tagMSICOLUMNHASHENTRY
*next
;
53 typedef struct tagMSICOLUMNINFO
60 MSICOLUMNHASHENTRY
**hash_table
;
67 USHORT
**nonpersistent_data
;
68 UINT nonpersistent_row_count
;
70 MSICOLUMNINFO
*colinfo
;
75 typedef struct tagMSITRANSFORM
{
80 static const WCHAR szStringData
[] = {
81 '_','S','t','r','i','n','g','D','a','t','a',0 };
82 static const WCHAR szStringPool
[] = {
83 '_','S','t','r','i','n','g','P','o','o','l',0 };
85 /* information for default tables */
86 static const WCHAR szTables
[] = { '_','T','a','b','l','e','s',0 };
87 static const WCHAR szTable
[] = { 'T','a','b','l','e',0 };
88 static const WCHAR szName
[] = { 'N','a','m','e',0 };
89 static const WCHAR szColumns
[] = { '_','C','o','l','u','m','n','s',0 };
90 static const WCHAR szNumber
[] = { 'N','u','m','b','e','r',0 };
91 static const WCHAR szType
[] = { 'T','y','p','e',0 };
93 static const MSICOLUMNINFO _Columns_cols
[4] = {
94 { szColumns
, 1, szTable
, MSITYPE_VALID
| MSITYPE_STRING
| 64, 0 },
95 { szColumns
, 2, szNumber
, MSITYPE_VALID
| 2, 2 },
96 { szColumns
, 3, szName
, MSITYPE_VALID
| MSITYPE_STRING
| 64, 4 },
97 { szColumns
, 4, szType
, MSITYPE_VALID
| 2, 6 },
99 static const MSICOLUMNINFO _Tables_cols
[1] = {
100 { szTables
, 1, szName
, MSITYPE_VALID
| MSITYPE_STRING
| 64, 0 },
103 #define MAX_STREAM_NAME 0x1f
105 static UINT
table_get_column_info( MSIDATABASE
*db
, LPCWSTR name
,
106 MSICOLUMNINFO
**pcols
, UINT
*pcount
);
107 static void table_calc_column_offsets( MSICOLUMNINFO
*colinfo
, DWORD count
);
108 static UINT
get_tablecolumns( MSIDATABASE
*db
,
109 LPCWSTR szTableName
, MSICOLUMNINFO
*colinfo
, UINT
*sz
);
110 static void msi_free_colinfo( MSICOLUMNINFO
*colinfo
, UINT count
);
112 static inline UINT
bytes_per_column( const MSICOLUMNINFO
*col
)
114 if( col
->type
& MSITYPE_STRING
)
116 if( (col
->type
& 0xff) > 4 )
117 ERR("Invalid column size!\n");
118 return col
->type
& 0xff;
121 static int utf2mime(int x
)
123 if( (x
>='0') && (x
<='9') )
125 if( (x
>='A') && (x
<='Z') )
127 if( (x
>='a') && (x
<='z') )
136 static LPWSTR
encode_streamname(BOOL bTable
, LPCWSTR in
)
138 DWORD count
= MAX_STREAM_NAME
;
143 count
= lstrlenW( in
)+2;
144 out
= msi_alloc( count
*sizeof(WCHAR
) );
160 if( ( ch
< 0x80 ) && ( utf2mime(ch
) >= 0 ) )
162 ch
= utf2mime(ch
) + 0x4800;
164 if( next
&& (next
<0x80) )
166 next
= utf2mime(next
);
177 ERR("Failed to encode stream name (%s)\n",debugstr_w(in
));
182 static int mime2utf(int x
)
189 return x
- 10 - 26 + 'a';
190 if( x
== (10+26+26) )
195 static BOOL
decode_streamname(LPWSTR in
, LPWSTR out
)
200 while ( (ch
= *in
++) )
202 if( (ch
>= 0x3800 ) && (ch
< 0x4840 ) )
205 ch
= mime2utf(ch
-0x4800);
209 *out
++ = mime2utf(ch
&0x3f);
211 ch
= mime2utf((ch
>>6)&0x3f);
221 void enum_stream_names( IStorage
*stg
)
223 IEnumSTATSTG
*stgenum
= NULL
;
229 r
= IStorage_EnumElements( stg
, 0, NULL
, 0, &stgenum
);
237 r
= IEnumSTATSTG_Next( stgenum
, 1, &stat
, &count
);
238 if( FAILED( r
) || !count
)
240 decode_streamname( stat
.pwcsName
, name
);
241 TRACE("stream %2d -> %s %s\n", n
,
242 debugstr_w(stat
.pwcsName
), debugstr_w(name
) );
246 IEnumSTATSTG_Release( stgenum
);
249 UINT
read_stream_data( IStorage
*stg
, LPCWSTR stname
,
250 USHORT
**pdata
, UINT
*psz
)
253 UINT ret
= ERROR_FUNCTION_FAILED
;
260 encname
= encode_streamname(TRUE
, stname
);
262 TRACE("%s -> %s\n",debugstr_w(stname
),debugstr_w(encname
));
264 r
= IStorage_OpenStream(stg
, encname
, NULL
,
265 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, &stm
);
269 WARN("open stream failed r = %08x - empty table?\n", r
);
273 r
= IStream_Stat(stm
, &stat
, STATFLAG_NONAME
);
276 WARN("open stream failed r = %08x!\n", r
);
280 if( stat
.cbSize
.QuadPart
>> 32 )
286 sz
= stat
.cbSize
.QuadPart
;
287 data
= msi_alloc( sz
);
290 WARN("couldn't allocate memory r=%08x!\n", r
);
291 ret
= ERROR_NOT_ENOUGH_MEMORY
;
295 r
= IStream_Read(stm
, data
, sz
, &count
);
296 if( FAILED( r
) || ( count
!= sz
) )
299 WARN("read stream failed r = %08x!\n", r
);
308 IStream_Release( stm
);
313 UINT
db_get_raw_stream( MSIDATABASE
*db
, LPCWSTR stname
, IStream
**stm
)
318 encname
= encode_streamname(FALSE
, stname
);
320 TRACE("%s -> %s\n",debugstr_w(stname
),debugstr_w(encname
));
322 r
= IStorage_OpenStream(db
->storage
, encname
, NULL
,
323 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, stm
);
326 MSITRANSFORM
*transform
;
328 LIST_FOR_EACH_ENTRY( transform
, &db
->transforms
, MSITRANSFORM
, entry
)
330 TRACE("looking for %s in transform storage\n", debugstr_w(stname
) );
331 r
= IStorage_OpenStream( transform
->stg
, encname
, NULL
,
332 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, stm
);
340 return SUCCEEDED(r
) ? ERROR_SUCCESS
: ERROR_FUNCTION_FAILED
;
343 UINT
read_raw_stream_data( MSIDATABASE
*db
, LPCWSTR stname
,
344 USHORT
**pdata
, UINT
*psz
)
347 UINT ret
= ERROR_FUNCTION_FAILED
;
353 r
= db_get_raw_stream( db
, stname
, &stm
);
354 if( r
!= ERROR_SUCCESS
)
356 r
= IStream_Stat(stm
, &stat
, STATFLAG_NONAME
);
359 WARN("open stream failed r = %08x!\n", r
);
363 if( stat
.cbSize
.QuadPart
>> 32 )
369 sz
= stat
.cbSize
.QuadPart
;
370 data
= msi_alloc( sz
);
373 WARN("couldn't allocate memory r=%08x!\n", r
);
374 ret
= ERROR_NOT_ENOUGH_MEMORY
;
378 r
= IStream_Read(stm
, data
, sz
, &count
);
379 if( FAILED( r
) || ( count
!= sz
) )
382 WARN("read stream failed r = %08x!\n", r
);
391 IStream_Release( stm
);
396 UINT
write_stream_data( IStorage
*stg
, LPCWSTR stname
,
397 LPVOID data
, UINT sz
)
400 UINT ret
= ERROR_FUNCTION_FAILED
;
407 encname
= encode_streamname(TRUE
, stname
);
408 r
= IStorage_OpenStream( stg
, encname
, NULL
,
409 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, &stm
);
412 r
= IStorage_CreateStream( stg
, encname
,
413 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, 0, &stm
);
418 WARN("open stream failed r = %08x\n", r
);
423 r
= IStream_SetSize( stm
, size
);
426 WARN("Failed to SetSize\n");
431 r
= IStream_Seek( stm
, pos
, STREAM_SEEK_SET
, NULL
);
434 WARN("Failed to Seek\n");
440 r
= IStream_Write(stm
, data
, sz
, &count
);
441 if( FAILED( r
) || ( count
!= sz
) )
443 WARN("Failed to Write\n");
451 IStream_Release( stm
);
456 static void free_table( MSITABLE
*table
)
459 for( i
=0; i
<table
->row_count
; i
++ )
460 msi_free( table
->data
[i
] );
461 msi_free( table
->data
);
462 for( i
=0; i
<table
->nonpersistent_row_count
; i
++ )
463 msi_free( table
->nonpersistent_data
[i
] );
464 msi_free( table
->nonpersistent_data
);
465 if( (table
->colinfo
!= _Tables_cols
) &&
466 (table
->colinfo
!= _Columns_cols
) )
468 msi_free_colinfo( table
->colinfo
, table
->col_count
);
469 msi_free( table
->colinfo
);
474 static UINT
msi_table_get_row_size( const MSICOLUMNINFO
*cols
, UINT count
)
476 const MSICOLUMNINFO
*last_col
= &cols
[count
-1];
479 return last_col
->offset
+ bytes_per_column( last_col
);
482 /* add this table to the list of cached tables in the database */
483 static UINT
read_table_from_storage( MSITABLE
*t
, IStorage
*stg
)
485 USHORT
*rawdata
= NULL
;
486 UINT rawsize
= 0, i
, j
, row_size
= 0;
488 TRACE("%s\n",debugstr_w(t
->name
));
490 row_size
= msi_table_get_row_size( t
->colinfo
, t
->col_count
);
492 /* if we can't read the table, just assume that it's empty */
493 read_stream_data( stg
, t
->name
, &rawdata
, &rawsize
);
495 return ERROR_SUCCESS
;
497 TRACE("Read %d bytes\n", rawsize
);
499 if( rawsize
% row_size
)
501 WARN("Table size is invalid %d/%d\n", rawsize
, row_size
);
505 t
->row_count
= rawsize
/ row_size
;
506 t
->data
= msi_alloc_zero( t
->row_count
* sizeof (USHORT
*) );
510 /* transpose all the data */
511 TRACE("Transposing data from %d rows\n", t
->row_count
);
512 for( i
=0; i
<t
->row_count
; i
++ )
514 t
->data
[i
] = msi_alloc( row_size
);
518 for( j
=0; j
<t
->col_count
; j
++ )
520 UINT ofs
= t
->colinfo
[j
].offset
/2;
521 UINT n
= bytes_per_column( &t
->colinfo
[j
] );
526 t
->data
[i
][ofs
] = rawdata
[ofs
*t
->row_count
+ i
];
529 t
->data
[i
][ofs
] = rawdata
[ofs
*t
->row_count
+ i
*2 ];
530 t
->data
[i
][ofs
+1] = rawdata
[ofs
*t
->row_count
+ i
*2 + 1];
533 ERR("oops - unknown column width %d\n", n
);
540 return ERROR_SUCCESS
;
543 return ERROR_FUNCTION_FAILED
;
546 void free_cached_tables( MSIDATABASE
*db
)
548 while( !list_empty( &db
->tables
) )
550 MSITABLE
*t
= LIST_ENTRY( list_head( &db
->tables
), MSITABLE
, entry
);
552 list_remove( &t
->entry
);
557 static MSITABLE
*find_cached_table( MSIDATABASE
*db
, LPCWSTR name
)
561 LIST_FOR_EACH_ENTRY( t
, &db
->tables
, MSITABLE
, entry
)
562 if( !lstrcmpW( name
, t
->name
) )
568 static UINT
table_get_column_info( MSIDATABASE
*db
, LPCWSTR name
, MSICOLUMNINFO
**pcols
, UINT
*pcount
)
570 UINT r
, column_count
= 0;
571 MSICOLUMNINFO
*columns
;
573 /* get the number of columns in this table */
575 r
= get_tablecolumns( db
, name
, NULL
, &column_count
);
576 if( r
!= ERROR_SUCCESS
)
579 /* if there's no columns, there's no table */
580 if( column_count
== 0 )
581 return ERROR_INVALID_PARAMETER
;
583 TRACE("Table %s found\n", debugstr_w(name
) );
585 columns
= msi_alloc( column_count
*sizeof (MSICOLUMNINFO
) );
587 return ERROR_FUNCTION_FAILED
;
589 r
= get_tablecolumns( db
, name
, columns
, &column_count
);
590 if( r
!= ERROR_SUCCESS
)
593 return ERROR_FUNCTION_FAILED
;
597 *pcount
= column_count
;
602 UINT
msi_create_table( MSIDATABASE
*db
, LPCWSTR name
, column_info
*col_info
,
603 BOOL persistent
, MSITABLE
**table_ret
)
607 MSIRECORD
*rec
= NULL
;
612 /* only add tables that don't exist already */
613 if( TABLE_Exists(db
, name
) )
615 WARN("table %s exists\n", debugstr_w(name
));
616 return ERROR_BAD_QUERY_SYNTAX
;
619 table
= msi_alloc( sizeof (MSITABLE
) + lstrlenW(name
)*sizeof (WCHAR
) );
621 return ERROR_FUNCTION_FAILED
;
623 table
->row_count
= 0;
625 table
->nonpersistent_row_count
= 0;
626 table
->nonpersistent_data
= NULL
;
627 table
->colinfo
= NULL
;
628 table
->col_count
= 0;
629 lstrcpyW( table
->name
, name
);
631 for( col
= col_info
; col
; col
= col
->next
)
634 table
->colinfo
= msi_alloc( table
->col_count
* sizeof(MSICOLUMNINFO
) );
638 return ERROR_FUNCTION_FAILED
;
641 for( i
= 0, col
= col_info
; col
; i
++, col
= col
->next
)
643 table
->colinfo
[ i
].tablename
= strdupW( col
->table
);
644 table
->colinfo
[ i
].number
= i
+ 1;
645 table
->colinfo
[ i
].colname
= strdupW( col
->column
);
646 table
->colinfo
[ i
].type
= col
->type
;
647 table
->colinfo
[ i
].offset
= 0;
648 table
->colinfo
[ i
].hash_table
= NULL
;
650 table_calc_column_offsets( table
->colinfo
, table
->col_count
);
652 r
= TABLE_CreateView( db
, szTables
, &tv
);
653 TRACE("CreateView returned %x\n", r
);
660 r
= tv
->ops
->execute( tv
, 0 );
661 TRACE("tv execute returned %x\n", r
);
665 rec
= MSI_CreateRecord( 1 );
669 r
= MSI_RecordSetStringW( rec
, 1, name
);
673 r
= tv
->ops
->insert_row( tv
, rec
, FALSE
);
674 TRACE("insert_row returned %x\n", r
);
678 tv
->ops
->delete( tv
);
681 msiobj_release( &rec
->hdr
);
683 /* add each column to the _Columns table */
684 r
= TABLE_CreateView( db
, szColumns
, &tv
);
688 r
= tv
->ops
->execute( tv
, 0 );
689 TRACE("tv execute returned %x\n", r
);
693 rec
= MSI_CreateRecord( 4 );
697 r
= MSI_RecordSetStringW( rec
, 1, name
);
702 * need to set the table, column number, col name and type
703 * for each column we enter in the table
706 for( col
= col_info
; col
; col
= col
->next
)
708 r
= MSI_RecordSetInteger( rec
, 2, nField
);
712 r
= MSI_RecordSetStringW( rec
, 3, col
->column
);
716 r
= MSI_RecordSetInteger( rec
, 4, col
->type
);
720 r
= tv
->ops
->insert_row( tv
, rec
, !persistent
);
731 msiobj_release( &rec
->hdr
);
732 /* FIXME: remove values from the string table on error */
734 tv
->ops
->delete( tv
);
736 if (r
== ERROR_SUCCESS
)
738 list_add_head( &db
->tables
, &table
->entry
);
747 static UINT
get_table( MSIDATABASE
*db
, LPCWSTR name
, MSITABLE
**table_ret
)
752 /* first, see if the table is cached */
753 table
= find_cached_table( db
, name
);
757 return ERROR_SUCCESS
;
760 /* nonexistent tables should be interpreted as empty tables */
761 table
= msi_alloc( sizeof (MSITABLE
) + lstrlenW(name
)*sizeof (WCHAR
) );
763 return ERROR_FUNCTION_FAILED
;
765 table
->row_count
= 0;
767 table
->nonpersistent_row_count
= 0;
768 table
->nonpersistent_data
= NULL
;
769 table
->colinfo
= NULL
;
770 table
->col_count
= 0;
771 lstrcpyW( table
->name
, name
);
773 /* these two tables are special - we know the column types already */
774 if( !lstrcmpW( name
, szColumns
) )
776 table
->colinfo
= (MSICOLUMNINFO
*)_Columns_cols
;
777 table
->col_count
= sizeof(_Columns_cols
)/sizeof(_Columns_cols
[0]);
779 else if( !lstrcmpW( name
, szTables
) )
781 table
->colinfo
= (MSICOLUMNINFO
*)_Tables_cols
;
782 table
->col_count
= sizeof(_Tables_cols
)/sizeof(_Tables_cols
[0]);
786 r
= table_get_column_info( db
, name
, &table
->colinfo
, &table
->col_count
);
787 if (r
!= ERROR_SUCCESS
)
789 free_table ( table
);
794 r
= read_table_from_storage( table
, db
->storage
);
795 if( r
!= ERROR_SUCCESS
)
801 list_add_head( &db
->tables
, &table
->entry
);
803 return ERROR_SUCCESS
;
806 static UINT
save_table( MSIDATABASE
*db
, MSITABLE
*t
)
808 USHORT
*rawdata
= NULL
, *p
;
809 UINT rawsize
, r
, i
, j
, row_size
;
811 TRACE("Saving %s\n", debugstr_w( t
->name
) );
813 row_size
= msi_table_get_row_size( t
->colinfo
, t
->col_count
);
815 rawsize
= t
->row_count
* row_size
;
816 rawdata
= msi_alloc_zero( rawsize
);
819 r
= ERROR_NOT_ENOUGH_MEMORY
;
824 for( i
=0; i
<t
->col_count
; i
++ )
826 for( j
=0; j
<t
->row_count
; j
++ )
828 UINT offset
= t
->colinfo
[i
].offset
;
830 *p
++ = t
->data
[j
][offset
/2];
831 if( 4 == bytes_per_column( &t
->colinfo
[i
] ) )
832 *p
++ = t
->data
[j
][offset
/2+1];
836 TRACE("writing %d bytes\n", rawsize
);
837 r
= write_stream_data( db
->storage
, t
->name
, rawdata
, rawsize
);
845 static void table_calc_column_offsets( MSICOLUMNINFO
*colinfo
, DWORD count
)
849 for( i
=0; colinfo
&& (i
<count
); i
++ )
851 assert( (i
+1) == colinfo
[ i
].number
);
853 colinfo
[i
].offset
= colinfo
[ i
- 1 ].offset
854 + bytes_per_column( &colinfo
[ i
- 1 ] );
856 colinfo
[i
].offset
= 0;
857 TRACE("column %d is [%s] with type %08x ofs %d\n",
858 colinfo
[i
].number
, debugstr_w(colinfo
[i
].colname
),
859 colinfo
[i
].type
, colinfo
[i
].offset
);
863 static UINT
get_defaulttablecolumns( LPCWSTR name
, MSICOLUMNINFO
*colinfo
, UINT
*sz
)
865 const MSICOLUMNINFO
*p
;
868 TRACE("%s\n", debugstr_w(name
));
870 if (!lstrcmpW( name
, szTables
))
875 else if (!lstrcmpW( name
, szColumns
))
881 return ERROR_FUNCTION_FAILED
;
883 /* duplicate the string data so we can free it in msi_free_colinfo */
886 if (colinfo
&& (i
< *sz
) )
888 memcpy( &colinfo
[i
], &p
[i
], sizeof(MSICOLUMNINFO
) );
889 colinfo
[i
].tablename
= strdupW( p
[i
].tablename
);
890 colinfo
[i
].colname
= strdupW( p
[i
].colname
);
892 if( colinfo
&& (i
>= *sz
) )
895 table_calc_column_offsets( colinfo
, n
);
897 return ERROR_SUCCESS
;
900 static void msi_free_colinfo( MSICOLUMNINFO
*colinfo
, UINT count
)
904 for( i
=0; i
<count
; i
++ )
906 msi_free( (LPWSTR
) colinfo
[i
].tablename
);
907 msi_free( (LPWSTR
) colinfo
[i
].colname
);
908 msi_free( colinfo
[i
].hash_table
);
912 static LPWSTR
msi_makestring( MSIDATABASE
*db
, UINT stringid
)
914 return strdupW(msi_string_lookup_id( db
->strings
, stringid
));
917 static UINT
get_tablecolumns( MSIDATABASE
*db
,
918 LPCWSTR szTableName
, MSICOLUMNINFO
*colinfo
, UINT
*sz
)
920 UINT r
, i
, n
=0, table_id
, count
, maxcount
= *sz
;
921 MSITABLE
*table
= NULL
;
923 TRACE("%s\n", debugstr_w(szTableName
));
925 /* first check if there is a default table with that name */
926 r
= get_defaulttablecolumns( szTableName
, colinfo
, sz
);
927 if( ( r
== ERROR_SUCCESS
) && *sz
)
930 r
= get_table( db
, szColumns
, &table
);
931 if( r
!= ERROR_SUCCESS
)
933 ERR("couldn't load _Columns table\n");
934 return ERROR_FUNCTION_FAILED
;
937 /* convert table and column names to IDs from the string table */
938 r
= msi_string2idW( db
->strings
, szTableName
, &table_id
);
939 if( r
!= ERROR_SUCCESS
)
941 WARN("Couldn't find id for %s\n", debugstr_w(szTableName
));
945 TRACE("Table id is %d, row count is %d\n", table_id
, table
->row_count
);
947 /* Note: _Columns table doesn't have non-persistent data */
949 /* if maxcount is non-zero, assume it's exactly right for this table */
950 memset( colinfo
, 0, maxcount
*sizeof(*colinfo
) );
951 count
= table
->row_count
;
952 for( i
=0; i
<count
; i
++ )
954 if( table
->data
[ i
][ 0 ] != table_id
)
958 UINT id
= table
->data
[ i
] [ 2 ];
959 UINT col
= table
->data
[ i
][ 1 ] - (1<<15);
961 /* check the column number is in range */
962 if (col
<1 || col
>maxcount
)
964 ERR("column %d out of range\n", col
);
968 /* check if this column was already set */
969 if (colinfo
[ col
- 1 ].number
)
971 ERR("duplicate column %d\n", col
);
975 colinfo
[ col
- 1 ].tablename
= msi_makestring( db
, table_id
);
976 colinfo
[ col
- 1 ].number
= col
;
977 colinfo
[ col
- 1 ].colname
= msi_makestring( db
, id
);
978 colinfo
[ col
- 1 ].type
= table
->data
[ i
] [ 3 ] - (1<<15);
979 colinfo
[ col
- 1 ].offset
= 0;
980 colinfo
[ col
- 1 ].hash_table
= NULL
;
985 TRACE("%s has %d columns\n", debugstr_w(szTableName
), n
);
987 if (colinfo
&& n
!= maxcount
)
989 ERR("missing column in table %s\n", debugstr_w(szTableName
));
990 msi_free_colinfo(colinfo
, maxcount
);
991 return ERROR_FUNCTION_FAILED
;
994 table_calc_column_offsets( colinfo
, n
);
997 return ERROR_SUCCESS
;
1000 /* try to find the table name in the _Tables table */
1001 BOOL
TABLE_Exists( MSIDATABASE
*db
, LPCWSTR name
)
1003 UINT r
, table_id
= 0, i
, count
;
1004 MSITABLE
*table
= NULL
;
1006 if( !lstrcmpW( name
, szTables
) )
1008 if( !lstrcmpW( name
, szColumns
) )
1011 r
= msi_string2idW( db
->strings
, name
, &table_id
);
1012 if( r
!= ERROR_SUCCESS
)
1014 TRACE("Couldn't find id for %s\n", debugstr_w(name
));
1018 r
= get_table( db
, szTables
, &table
);
1019 if( r
!= ERROR_SUCCESS
)
1021 ERR("table %s not available\n", debugstr_w(szTables
));
1025 count
= table
->row_count
;
1026 for( i
=0; i
<count
; i
++ )
1027 if( table
->data
[ i
][ 0 ] == table_id
)
1033 count
= table
->nonpersistent_row_count
;
1034 for( i
=0; i
<count
; i
++ )
1035 if( table
->nonpersistent_data
[ i
][ 0 ] == table_id
)
1041 TRACE("Searched %d tables, but %d was not found\n", count
, table_id
);
1046 /* below is the query interface to a table */
1048 typedef struct tagMSITABLEVIEW
1053 MSICOLUMNINFO
*columns
;
1059 static UINT
TABLE_fetch_int( struct tagMSIVIEW
*view
, UINT row
, UINT col
, UINT
*val
)
1061 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1066 return ERROR_INVALID_PARAMETER
;
1068 if( (col
==0) || (col
>tv
->num_cols
) )
1069 return ERROR_INVALID_PARAMETER
;
1071 /* how many rows are there ? */
1072 if( row
>= tv
->table
->row_count
+ tv
->table
->nonpersistent_row_count
)
1073 return ERROR_NO_MORE_ITEMS
;
1075 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1077 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1078 ERR("%p %p\n", tv
, tv
->columns
);
1079 return ERROR_FUNCTION_FAILED
;
1082 if (row
>= tv
->table
->row_count
)
1084 row
-= tv
->table
->row_count
;
1085 data
= tv
->table
->nonpersistent_data
;
1088 data
= tv
->table
->data
;
1089 n
= bytes_per_column( &tv
->columns
[col
-1] );
1093 offset
= tv
->columns
[col
-1].offset
/2;
1094 *val
= data
[row
][offset
] +
1095 (data
[row
][offset
+ 1] << 16);
1098 offset
= tv
->columns
[col
-1].offset
/2;
1099 *val
= data
[row
][offset
];
1102 ERR("oops! what is %d bytes per column?\n", n
);
1103 return ERROR_FUNCTION_FAILED
;
1106 /* TRACE("Data [%d][%d] = %d\n", row, col, *val ); */
1108 return ERROR_SUCCESS
;
1112 * We need a special case for streams, as we need to reference column with
1113 * the name of the stream in the same table, and the table name
1114 * which may not be available at higher levels of the query
1116 static UINT
TABLE_fetch_stream( struct tagMSIVIEW
*view
, UINT row
, UINT col
, IStream
**stm
)
1118 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1119 UINT ival
= 0, refcol
= 0, r
;
1123 static const WCHAR szDot
[] = { '.', 0 };
1126 if( !view
->ops
->fetch_int
)
1127 return ERROR_INVALID_PARAMETER
;
1130 * The column marked with the type stream data seems to have a single number
1131 * which references the column containing the name of the stream data
1133 * Fetch the column to reference first.
1135 r
= view
->ops
->fetch_int( view
, row
, col
, &ival
);
1136 if( r
!= ERROR_SUCCESS
)
1139 /* check the column value is in range */
1140 if (ival
< 0 || ival
> tv
->num_cols
|| ival
== col
)
1142 ERR("bad column ref (%u) for stream\n", ival
);
1143 return ERROR_FUNCTION_FAILED
;
1146 if ( tv
->columns
[ival
- 1].type
& MSITYPE_STRING
)
1148 /* now get the column with the name of the stream */
1149 r
= view
->ops
->fetch_int( view
, row
, ival
, &refcol
);
1150 if ( r
!= ERROR_SUCCESS
)
1153 /* lookup the string value from the string table */
1154 sval
= msi_string_lookup_id( tv
->db
->strings
, refcol
);
1156 return ERROR_INVALID_PARAMETER
;
1160 static const WCHAR fmt
[] = { '%','d',0 };
1161 sprintfW( number
, fmt
, ival
);
1165 len
= lstrlenW( tv
->name
) + 2 + lstrlenW( sval
);
1166 full_name
= msi_alloc( len
*sizeof(WCHAR
) );
1167 lstrcpyW( full_name
, tv
->name
);
1168 lstrcatW( full_name
, szDot
);
1169 lstrcatW( full_name
, sval
);
1171 r
= db_get_raw_stream( tv
->db
, full_name
, stm
);
1173 ERR("fetching stream %s, error = %d\n",debugstr_w(full_name
), r
);
1174 msi_free( full_name
);
1179 static UINT
TABLE_set_int( MSITABLEVIEW
*tv
, UINT row
, UINT col
, UINT val
)
1185 return ERROR_INVALID_PARAMETER
;
1187 if( (col
==0) || (col
>tv
->num_cols
) )
1188 return ERROR_INVALID_PARAMETER
;
1190 if( row
>= tv
->table
->row_count
+ tv
->table
->nonpersistent_row_count
)
1191 return ERROR_INVALID_PARAMETER
;
1193 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1195 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1196 ERR("%p %p\n", tv
, tv
->columns
);
1197 return ERROR_FUNCTION_FAILED
;
1200 msi_free( tv
->columns
[col
-1].hash_table
);
1201 tv
->columns
[col
-1].hash_table
= NULL
;
1203 if (row
>= tv
->table
->row_count
)
1205 row
-= tv
->table
->row_count
;
1206 data
= tv
->table
->nonpersistent_data
;
1209 data
= tv
->table
->data
;
1210 n
= bytes_per_column( &tv
->columns
[col
-1] );
1214 offset
= tv
->columns
[col
-1].offset
/2;
1215 data
[row
][offset
] = val
& 0xffff;
1216 data
[row
][offset
+ 1] = (val
>>16)&0xffff;
1219 offset
= tv
->columns
[col
-1].offset
/2;
1220 data
[row
][offset
] = val
;
1223 ERR("oops! what is %d bytes per column?\n", n
);
1224 return ERROR_FUNCTION_FAILED
;
1226 return ERROR_SUCCESS
;
1229 static UINT
TABLE_set_row( struct tagMSIVIEW
*view
, UINT row
, MSIRECORD
*rec
, UINT mask
)
1231 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1232 UINT i
, val
, r
= ERROR_SUCCESS
;
1235 return ERROR_INVALID_PARAMETER
;
1237 /* test if any of the mask bits are invalid */
1238 if ( mask
>= (1<<tv
->num_cols
) )
1239 return ERROR_INVALID_PARAMETER
;
1241 for ( i
= 0; i
< tv
->num_cols
; i
++ )
1243 /* only update the fields specified in the mask */
1244 if ( !(mask
&(1<<i
)) )
1247 /* FIXME: should we allow updating keys? */
1250 if ( !MSI_RecordIsNull( rec
, i
+ 1 ) )
1252 if ( MSITYPE_IS_BINARY(tv
->columns
[ i
].type
) )
1254 val
= 1; /* refers to the first key column */
1256 else if ( tv
->columns
[i
].type
& MSITYPE_STRING
)
1258 LPCWSTR sval
= MSI_RecordGetString( rec
, i
+ 1 );
1259 val
= msi_addstringW( tv
->db
->strings
, 0, sval
, -1, 1, StringPersistent
);
1261 else if ( 2 == bytes_per_column( &tv
->columns
[ i
] ) )
1263 val
= 0x8000 + MSI_RecordGetInteger( rec
, i
+ 1 );
1264 if ( val
& 0xffff0000 )
1266 ERR("field %u value %d out of range\n", i
+1, val
- 0x8000 );
1267 return ERROR_FUNCTION_FAILED
;
1272 INT ival
= MSI_RecordGetInteger( rec
, i
+ 1 );
1273 val
= ival
^ 0x80000000;
1277 r
= TABLE_set_int( tv
, row
, i
+1, val
);
1278 if ( r
!= ERROR_SUCCESS
)
1284 static UINT
table_create_new_row( struct tagMSIVIEW
*view
, UINT
*num
, BOOL temporary
)
1286 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1292 TRACE("%p %s\n", view
, temporary
? "TRUE" : "FALSE");
1295 return ERROR_INVALID_PARAMETER
;
1297 row
= msi_alloc_zero( tv
->row_size
);
1299 return ERROR_NOT_ENOUGH_MEMORY
;
1303 row_count
= &tv
->table
->nonpersistent_row_count
;
1304 data_ptr
= &tv
->table
->nonpersistent_data
;
1305 *num
= tv
->table
->row_count
+ tv
->table
->nonpersistent_row_count
;
1309 row_count
= &tv
->table
->row_count
;
1310 data_ptr
= &tv
->table
->data
;
1311 *num
= tv
->table
->row_count
;
1314 sz
= (*row_count
+ 1) * sizeof (UINT
*);
1316 p
= msi_realloc( *data_ptr
, sz
);
1318 p
= msi_alloc( sz
);
1322 return ERROR_NOT_ENOUGH_MEMORY
;
1326 (*data_ptr
)[*row_count
] = row
;
1329 return ERROR_SUCCESS
;
1332 static UINT
TABLE_execute( struct tagMSIVIEW
*view
, MSIRECORD
*record
)
1334 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1336 TRACE("%p %p\n", tv
, record
);
1338 TRACE("There are %d columns\n", tv
->num_cols
);
1340 return ERROR_SUCCESS
;
1343 static UINT
TABLE_close( struct tagMSIVIEW
*view
)
1345 TRACE("%p\n", view
);
1347 return ERROR_SUCCESS
;
1350 static UINT
TABLE_get_dimensions( struct tagMSIVIEW
*view
, UINT
*rows
, UINT
*cols
)
1352 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1354 TRACE("%p %p %p\n", view
, rows
, cols
);
1357 *cols
= tv
->num_cols
;
1361 return ERROR_INVALID_PARAMETER
;
1362 *rows
= tv
->table
->row_count
+ tv
->table
->nonpersistent_row_count
;
1365 return ERROR_SUCCESS
;
1368 static UINT
TABLE_get_column_info( struct tagMSIVIEW
*view
,
1369 UINT n
, LPWSTR
*name
, UINT
*type
)
1371 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1373 TRACE("%p %d %p %p\n", tv
, n
, name
, type
);
1375 if( ( n
== 0 ) || ( n
> tv
->num_cols
) )
1376 return ERROR_INVALID_PARAMETER
;
1380 *name
= strdupW( tv
->columns
[n
-1].colname
);
1382 return ERROR_FUNCTION_FAILED
;
1385 *type
= tv
->columns
[n
-1].type
;
1387 return ERROR_SUCCESS
;
1390 static UINT
msi_table_find_row( MSITABLEVIEW
*tv
, MSIRECORD
*rec
, UINT
*row
);
1392 static UINT
table_validate_new( MSITABLEVIEW
*tv
, MSIRECORD
*rec
)
1396 /* check there's no null values where they're not allowed */
1397 for( i
= 0; i
< tv
->num_cols
; i
++ )
1399 if ( tv
->columns
[i
].type
& MSITYPE_NULLABLE
)
1402 if ( MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
1403 TRACE("skipping binary column\n");
1404 else if ( tv
->columns
[i
].type
& MSITYPE_STRING
)
1408 str
= MSI_RecordGetString( rec
, i
+1 );
1409 if (str
== NULL
|| str
[0] == 0)
1410 return ERROR_INVALID_DATA
;
1416 n
= MSI_RecordGetInteger( rec
, i
+1 );
1417 if (n
== MSI_NULL_INTEGER
)
1418 return ERROR_INVALID_DATA
;
1422 /* check there's no duplicate keys */
1423 r
= msi_table_find_row( tv
, rec
, &row
);
1424 if (r
== ERROR_SUCCESS
)
1425 return ERROR_INVALID_DATA
;
1427 return ERROR_SUCCESS
;
1430 static UINT
TABLE_insert_row( struct tagMSIVIEW
*view
, MSIRECORD
*rec
, BOOL temporary
)
1432 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1435 TRACE("%p %p %s\n", tv
, rec
, temporary
? "TRUE" : "FALSE" );
1437 /* check that the key is unique - can we find a matching row? */
1438 r
= table_validate_new( tv
, rec
);
1439 if( r
!= ERROR_SUCCESS
)
1440 return ERROR_FUNCTION_FAILED
;
1442 r
= table_create_new_row( view
, &row
, temporary
);
1443 TRACE("insert_row returned %08x\n", r
);
1444 if( r
!= ERROR_SUCCESS
)
1447 return TABLE_set_row( view
, row
, rec
, (1<<tv
->num_cols
) - 1 );
1450 static UINT
TABLE_modify( struct tagMSIVIEW
*view
, MSIMODIFY eModifyMode
,
1453 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1456 TRACE("%p %d %p\n", view
, eModifyMode
, rec
);
1458 switch (eModifyMode
)
1460 case MSIMODIFY_VALIDATE_NEW
:
1461 r
= table_validate_new( tv
, rec
);
1464 case MSIMODIFY_INSERT_TEMPORARY
:
1465 r
= table_validate_new( tv
, rec
);
1466 if (r
!= ERROR_SUCCESS
)
1468 r
= TABLE_insert_row( view
, rec
, TRUE
);
1471 case MSIMODIFY_REFRESH
:
1472 case MSIMODIFY_INSERT
:
1473 case MSIMODIFY_UPDATE
:
1474 case MSIMODIFY_ASSIGN
:
1475 case MSIMODIFY_REPLACE
:
1476 case MSIMODIFY_MERGE
:
1477 case MSIMODIFY_DELETE
:
1478 case MSIMODIFY_VALIDATE
:
1479 case MSIMODIFY_VALIDATE_FIELD
:
1480 case MSIMODIFY_VALIDATE_DELETE
:
1481 FIXME("%p %d %p - mode not implemented\n", view
, eModifyMode
, rec
);
1482 r
= ERROR_CALL_NOT_IMPLEMENTED
;
1486 r
= ERROR_INVALID_DATA
;
1492 static UINT
TABLE_delete( struct tagMSIVIEW
*view
)
1494 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1496 TRACE("%p\n", view
);
1504 return ERROR_SUCCESS
;
1507 static UINT
TABLE_find_matching_rows( struct tagMSIVIEW
*view
, UINT col
,
1508 UINT val
, UINT
*row
, MSIITERHANDLE
*handle
)
1510 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1511 const MSICOLUMNHASHENTRY
*entry
;
1513 TRACE("%p, %d, %u, %p\n", view
, col
, val
, *handle
);
1516 return ERROR_INVALID_PARAMETER
;
1518 if( (col
==0) || (col
> tv
->num_cols
) )
1519 return ERROR_INVALID_PARAMETER
;
1521 if( !tv
->columns
[col
-1].hash_table
)
1524 UINT num_rows
= tv
->table
->row_count
+ tv
->table
->nonpersistent_row_count
;
1525 MSICOLUMNHASHENTRY
**hash_table
;
1526 MSICOLUMNHASHENTRY
*new_entry
;
1528 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1530 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1531 ERR("%p %p\n", tv
, tv
->columns
);
1532 return ERROR_FUNCTION_FAILED
;
1535 /* allocate contiguous memory for the table and its entries so we
1536 * don't have to do an expensive cleanup */
1537 hash_table
= msi_alloc(MSITABLE_HASH_TABLE_SIZE
* sizeof(MSICOLUMNHASHENTRY
*) +
1538 num_rows
* sizeof(MSICOLUMNHASHENTRY
));
1540 return ERROR_OUTOFMEMORY
;
1542 memset(hash_table
, 0, MSITABLE_HASH_TABLE_SIZE
* sizeof(MSICOLUMNHASHENTRY
*));
1543 tv
->columns
[col
-1].hash_table
= hash_table
;
1545 new_entry
= (MSICOLUMNHASHENTRY
*)(hash_table
+ MSITABLE_HASH_TABLE_SIZE
);
1547 for (i
= 0; i
< num_rows
; i
++, new_entry
++)
1553 if( row
>= tv
->table
->row_count
)
1555 row
-= tv
->table
->row_count
;
1556 data
= tv
->table
->nonpersistent_data
;
1559 data
= tv
->table
->data
;
1560 n
= bytes_per_column( &tv
->columns
[col
-1] );
1564 offset
= tv
->columns
[col
-1].offset
/2;
1565 row_value
= data
[row
][offset
] +
1566 (data
[row
][offset
+ 1] << 16);
1569 offset
= tv
->columns
[col
-1].offset
/2;
1570 row_value
= data
[row
][offset
];
1573 ERR("oops! what is %d bytes per column?\n", n
);
1577 new_entry
->next
= NULL
;
1578 new_entry
->value
= row_value
;
1580 if (hash_table
[row_value
% MSITABLE_HASH_TABLE_SIZE
])
1582 MSICOLUMNHASHENTRY
*prev_entry
= hash_table
[row_value
% MSITABLE_HASH_TABLE_SIZE
];
1583 while (prev_entry
->next
)
1584 prev_entry
= prev_entry
->next
;
1585 prev_entry
->next
= new_entry
;
1588 hash_table
[row_value
% MSITABLE_HASH_TABLE_SIZE
] = new_entry
;
1593 entry
= tv
->columns
[col
-1].hash_table
[val
% MSITABLE_HASH_TABLE_SIZE
];
1595 entry
= (*handle
)->next
;
1597 while (entry
&& entry
->value
!= val
)
1598 entry
= entry
->next
;
1602 return ERROR_NO_MORE_ITEMS
;
1605 return ERROR_SUCCESS
;
1609 static const MSIVIEWOPS table_ops
=
1617 TABLE_get_dimensions
,
1618 TABLE_get_column_info
,
1621 TABLE_find_matching_rows
1624 UINT
TABLE_CreateView( MSIDATABASE
*db
, LPCWSTR name
, MSIVIEW
**view
)
1629 TRACE("%p %s %p\n", db
, debugstr_w(name
), view
);
1631 sz
= sizeof *tv
+ lstrlenW(name
)*sizeof name
[0] ;
1632 tv
= msi_alloc_zero( sz
);
1634 return ERROR_FUNCTION_FAILED
;
1636 r
= get_table( db
, name
, &tv
->table
);
1637 if( r
!= ERROR_SUCCESS
)
1640 WARN("table not found\n");
1644 TRACE("table %p found with %d columns\n", tv
->table
, tv
->table
->col_count
);
1646 /* fill the structure */
1647 tv
->view
.ops
= &table_ops
;
1649 tv
->columns
= tv
->table
->colinfo
;
1650 tv
->num_cols
= tv
->table
->col_count
;
1651 tv
->row_size
= msi_table_get_row_size( tv
->table
->colinfo
, tv
->table
->col_count
);
1653 TRACE("%s one row is %d bytes\n", debugstr_w(name
), tv
->row_size
);
1655 *view
= (MSIVIEW
*) tv
;
1656 lstrcpyW( tv
->name
, name
);
1658 return ERROR_SUCCESS
;
1661 UINT
MSI_CommitTables( MSIDATABASE
*db
)
1664 MSITABLE
*table
= NULL
;
1668 r
= msi_save_string_table( db
->strings
, db
->storage
);
1669 if( r
!= ERROR_SUCCESS
)
1671 WARN("failed to save string table r=%08x\n",r
);
1675 LIST_FOR_EACH_ENTRY( table
, &db
->tables
, MSITABLE
, entry
)
1677 r
= save_table( db
, table
);
1678 if( r
!= ERROR_SUCCESS
)
1680 WARN("failed to save table %s (r=%08x)\n",
1681 debugstr_w(table
->name
), r
);
1686 /* force everything to reload next time */
1687 free_cached_tables( db
);
1689 return ERROR_SUCCESS
;
1692 MSICONDITION
MSI_DatabaseIsTablePersistent( MSIDATABASE
*db
, LPCWSTR table
)
1695 return MSICONDITION_ERROR
;
1697 if (!TABLE_Exists( db
, table
))
1698 return MSICONDITION_NONE
;
1700 return MSICONDITION_FALSE
;
1703 static MSIRECORD
*msi_get_transform_record( MSITABLEVIEW
*tv
, string_table
*st
, USHORT
*rawdata
)
1705 UINT i
, val
, ofs
= 0;
1706 USHORT mask
= *rawdata
++;
1707 MSICOLUMNINFO
*columns
= tv
->columns
;
1710 rec
= MSI_CreateRecord( tv
->num_cols
);
1715 for( i
=0; i
<tv
->num_cols
; i
++ )
1717 UINT n
= bytes_per_column( &columns
[i
] );
1719 if ( (mask
&1) && (i
>=(mask
>>8)) )
1721 /* all keys must be present */
1722 if ( (~mask
&1) && (~columns
[i
].type
& MSITYPE_KEY
) && ((1<<i
) & ~mask
) )
1729 if( (columns
[i
].type
& MSITYPE_STRING
) &&
1730 ! MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
1732 LPCWSTR sval
= msi_string_lookup_id( st
, val
);
1733 MSI_RecordSetStringW( rec
, i
+1, sval
);
1734 TRACE(" field %d [%s]\n", i
+1, debugstr_w(sval
));
1739 MSI_RecordSetInteger( rec
, i
+1, val
^0x8000 );
1740 TRACE(" field %d [0x%04x]\n", i
+1, val
);
1744 val
= (rawdata
[ofs
] + (rawdata
[ofs
+ 1]<<16));
1746 MSI_RecordSetInteger( rec
, i
+1, val
^0x80000000 );
1747 TRACE(" field %d [0x%08x]\n", i
+1, val
);
1750 ERR("oops - unknown column width %d\n", n
);
1758 static void dump_record( MSIRECORD
*rec
)
1762 n
= MSI_RecordGetFieldCount( rec
);
1763 for( i
=1; i
<=n
; i
++ )
1765 LPCWSTR sval
= MSI_RecordGetString( rec
, i
);
1767 if( MSI_RecordIsNull( rec
, i
) )
1768 TRACE("row -> []\n");
1769 else if( (sval
= MSI_RecordGetString( rec
, i
)) )
1770 TRACE("row -> [%s]\n", debugstr_w(sval
));
1772 TRACE("row -> [0x%08x]\n", MSI_RecordGetInteger( rec
, i
) );
1776 static void dump_table( string_table
*st
, USHORT
*rawdata
, UINT rawsize
)
1781 for( i
=0; i
<(rawsize
/2); i
++ )
1783 sval
= msi_string_lookup_id( st
, rawdata
[i
] );
1784 MESSAGE(" %04x %s\n", rawdata
[i
], debugstr_w(sval
) );
1788 static UINT
* msi_record_to_row( MSITABLEVIEW
*tv
, MSIRECORD
*rec
)
1793 data
= msi_alloc( tv
->num_cols
*sizeof (UINT
) );
1794 for( i
=0; i
<tv
->num_cols
; i
++ )
1798 if ( ~tv
->columns
[i
].type
& MSITYPE_KEY
)
1801 /* turn the transform column value into a row value */
1802 if ( ( tv
->columns
[i
].type
& MSITYPE_STRING
) &&
1803 ! MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
1805 str
= MSI_RecordGetString( rec
, i
+1 );
1806 r
= msi_string2idW( tv
->db
->strings
, str
, &data
[i
] );
1808 /* if there's no matching string in the string table,
1809 these keys can't match any record, so fail now. */
1810 if( ERROR_SUCCESS
!= r
)
1818 data
[i
] = MSI_RecordGetInteger( rec
, i
+1 );
1819 if ((tv
->columns
[i
].type
&0xff) == 2)
1822 data
[i
] += 0x80000000;
1828 static UINT
msi_row_matches( MSITABLEVIEW
*tv
, UINT row
, UINT
*data
)
1830 UINT i
, r
, x
, ret
= ERROR_FUNCTION_FAILED
;
1832 for( i
=0; i
<tv
->num_cols
; i
++ )
1834 if ( ~tv
->columns
[i
].type
& MSITYPE_KEY
)
1837 /* turn the transform column value into a row value */
1838 r
= TABLE_fetch_int( &tv
->view
, row
, i
+1, &x
);
1839 if ( r
!= ERROR_SUCCESS
)
1841 ERR("TABLE_fetch_int shouldn't fail here\n");
1845 /* if this key matches, move to the next column */
1848 ret
= ERROR_FUNCTION_FAILED
;
1852 ret
= ERROR_SUCCESS
;
1858 static UINT
msi_table_find_row( MSITABLEVIEW
*tv
, MSIRECORD
*rec
, UINT
*row
)
1860 UINT i
, r
= ERROR_FUNCTION_FAILED
, *data
;
1862 data
= msi_record_to_row( tv
, rec
);
1865 for( i
= 0; i
< tv
->table
->row_count
+ tv
->table
->nonpersistent_row_count
; i
++ )
1867 r
= msi_row_matches( tv
, i
, data
);
1868 if( r
== ERROR_SUCCESS
)
1878 static UINT
msi_delete_row( MSITABLEVIEW
*tv
, UINT row
)
1882 for( i
=1; i
<=tv
->num_cols
; i
++ )
1883 TABLE_set_int( tv
, row
, i
, 0 );
1884 return ERROR_SUCCESS
;
1887 static UINT
msi_table_load_transform( MSIDATABASE
*db
, IStorage
*stg
,
1888 string_table
*st
, LPCWSTR name
)
1891 USHORT
*rawdata
= NULL
;
1892 MSITABLEVIEW
*tv
= NULL
;
1893 UINT r
, n
, sz
, i
, mask
;
1894 MSIRECORD
*rec
= NULL
;
1899 TRACE("%p %p %p %s\n", db
, stg
, st
, debugstr_w(name
) );
1901 /* read the transform data */
1902 read_stream_data( stg
, name
, &rawdata
, &rawsize
);
1905 TRACE("table %s empty\n", debugstr_w(name
) );
1906 return ERROR_INVALID_TABLE
;
1909 /* create a table view */
1910 r
= TABLE_CreateView( db
, name
, (MSIVIEW
**) &tv
);
1911 if( r
!= ERROR_SUCCESS
)
1914 r
= tv
->view
.ops
->execute( &tv
->view
, NULL
);
1915 if( r
!= ERROR_SUCCESS
)
1918 TRACE("name = %s columns = %u row_size = %u raw size = %u\n",
1919 debugstr_w(name
), tv
->num_cols
, tv
->row_size
, rawsize
);
1921 /* interpret the data */
1923 for( n
=0; n
< (rawsize
/2); )
1930 * if the low bit is set, columns are continuous and
1931 * the number of columns is specified in the high byte
1933 sz
= 2 + tv
->row_size
;
1938 * If the low bit is not set, rowdata[n] is a bitmask.
1939 * Excepting for key fields, which are always present,
1940 * each bit indicates that a field is present in the transform record.
1942 * rawdata[n] == 0 is a special case ... only the keys will be present
1943 * and it means that this row should be deleted.
1946 for( i
=0; i
<tv
->num_cols
; i
++ )
1948 if( (tv
->columns
[i
].type
& MSITYPE_KEY
) || ((1<<i
)&mask
))
1949 sz
+= bytes_per_column( &tv
->columns
[i
] );
1953 /* check we didn't run of the end of the table */
1954 if ( (n
+sz
) > rawsize
)
1957 dump_table( st
, rawdata
, rawsize
);
1961 rec
= msi_get_transform_record( tv
, st
, &rawdata
[n
] );
1966 TRACE("inserting record\n");
1969 * Native msi seems writes nul into the
1970 * Number (2nd) column of the _Columns table.
1971 * Not sure that it's deliberate...
1973 if (!lstrcmpW(name
, szColumns
))
1978 MSI_RecordGetStringW( rec
, 1, table
, &sz
);
1980 /* reset the column number on a new table */
1981 if ( lstrcmpW(coltable
, table
) )
1984 lstrcpyW( coltable
, table
);
1987 /* fix nul column numbers */
1988 MSI_RecordSetInteger( rec
, 2, ++colcol
);
1991 r
= TABLE_insert_row( &tv
->view
, rec
, FALSE
);
1992 if (r
!= ERROR_SUCCESS
)
1993 ERR("insert row failed\n");
1999 r
= msi_table_find_row( tv
, rec
, &row
);
2000 if (r
!= ERROR_SUCCESS
)
2001 ERR("no matching row to transform\n");
2004 TRACE("modifying row [%d]:\n", row
);
2005 TABLE_set_row( &tv
->view
, row
, rec
, mask
);
2009 TRACE("deleting row [%d]:\n", row
);
2010 msi_delete_row( tv
, row
);
2013 if( TRACE_ON(msidb
) ) dump_record( rec
);
2014 msiobj_release( &rec
->hdr
);
2021 /* no need to free the table, it's associated with the database */
2022 msi_free( rawdata
);
2024 tv
->view
.ops
->delete( &tv
->view
);
2026 return ERROR_SUCCESS
;
2030 * msi_table_apply_transform
2032 * Enumerate the table transforms in a transform storage and apply each one.
2034 UINT
msi_table_apply_transform( MSIDATABASE
*db
, IStorage
*stg
)
2036 IEnumSTATSTG
*stgenum
= NULL
;
2041 string_table
*strings
;
2042 UINT ret
= ERROR_FUNCTION_FAILED
;
2044 TRACE("%p %p\n", db
, stg
);
2046 strings
= msi_load_string_table( stg
);
2050 r
= IStorage_EnumElements( stg
, 0, NULL
, 0, &stgenum
);
2055 * Apply _Tables and _Columns transforms first so that
2056 * the table metadata is correct, and empty tables exist.
2058 ret
= msi_table_load_transform( db
, stg
, strings
, szTables
);
2059 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_INVALID_TABLE
)
2062 ret
= msi_table_load_transform( db
, stg
, strings
, szColumns
);
2063 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_INVALID_TABLE
)
2066 ret
= ERROR_SUCCESS
;
2068 while( r
== ERROR_SUCCESS
)
2071 r
= IEnumSTATSTG_Next( stgenum
, 1, &stat
, &count
);
2072 if( FAILED( r
) || !count
)
2075 decode_streamname( stat
.pwcsName
, name
);
2076 if ( name
[0] != 0x4840 )
2079 TRACE("transform contains stream %s\n", debugstr_w(name
));
2081 if ( !lstrcmpW( name
+1, szStringPool
) ||
2082 !lstrcmpW( name
+1, szStringData
) ||
2083 !lstrcmpW( name
+1, szColumns
) ||
2084 !lstrcmpW( name
+1, szTables
) )
2087 ret
= msi_table_load_transform( db
, stg
, strings
, name
+1 );
2090 if ( ret
== ERROR_SUCCESS
)
2091 append_storage_to_db( db
, stg
);
2095 IEnumSTATSTG_Release( stgenum
);
2097 msi_destroy_stringtable( strings
);
2102 void append_storage_to_db( MSIDATABASE
*db
, IStorage
*stg
)
2106 t
= msi_alloc( sizeof *t
);
2108 IStorage_AddRef( stg
);
2109 list_add_tail( &db
->transforms
, &t
->entry
);
2112 void msi_free_transforms( MSIDATABASE
*db
)
2114 while( !list_empty( &db
->transforms
) )
2116 MSITRANSFORM
*t
= LIST_ENTRY( list_head( &db
->transforms
),
2117 MSITRANSFORM
, entry
);
2118 list_remove( &t
->entry
);
2119 IStorage_Release( t
->stg
);