2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2002-2005 Mike McCormack for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
39 #include "wine/debug.h"
40 #include "wine/unicode.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(msidb
);
44 #define MSITABLE_HASH_TABLE_SIZE 37
45 #define LONG_STR_BYTES 3
47 typedef struct tagMSICOLUMNHASHENTRY
49 struct tagMSICOLUMNHASHENTRY
*next
;
54 typedef struct tagMSICOLUMNINFO
62 MSICOLUMNHASHENTRY
**hash_table
;
69 BYTE
**nonpersistent_data
;
70 UINT nonpersistent_row_count
;
72 MSICOLUMNINFO
*colinfo
;
79 typedef struct tagMSITRANSFORM
{
84 static const WCHAR szStringData
[] = {
85 '_','S','t','r','i','n','g','D','a','t','a',0 };
86 static const WCHAR szStringPool
[] = {
87 '_','S','t','r','i','n','g','P','o','o','l',0 };
89 /* information for default tables */
90 static const WCHAR szTables
[] = { '_','T','a','b','l','e','s',0 };
91 static const WCHAR szTable
[] = { 'T','a','b','l','e',0 };
92 static const WCHAR szName
[] = { 'N','a','m','e',0 };
93 static const WCHAR szColumns
[] = { '_','C','o','l','u','m','n','s',0 };
94 static const WCHAR szNumber
[] = { 'N','u','m','b','e','r',0 };
95 static const WCHAR szType
[] = { 'T','y','p','e',0 };
97 /* These tables are written into (the .hash_table part).
98 * Do not mark them const.
100 static MSICOLUMNINFO _Columns_cols
[4] = {
101 { szColumns
, 1, szTable
, MSITYPE_VALID
| MSITYPE_STRING
| MSITYPE_KEY
| 64, 0 },
102 { szColumns
, 2, szNumber
, MSITYPE_VALID
| MSITYPE_KEY
| 2, 2 },
103 { szColumns
, 3, szName
, MSITYPE_VALID
| MSITYPE_STRING
| 64, 4 },
104 { szColumns
, 4, szType
, MSITYPE_VALID
| 2, 6 },
106 static MSICOLUMNINFO _Tables_cols
[1] = {
107 { szTables
, 1, szName
, MSITYPE_VALID
| MSITYPE_STRING
| 64, 0 },
110 #define MAX_STREAM_NAME 0x1f
112 static UINT
table_get_column_info( MSIDATABASE
*db
, LPCWSTR name
,
113 MSICOLUMNINFO
**pcols
, UINT
*pcount
);
114 static void table_calc_column_offsets( MSICOLUMNINFO
*colinfo
, DWORD count
);
115 static UINT
get_tablecolumns( MSIDATABASE
*db
,
116 LPCWSTR szTableName
, MSICOLUMNINFO
*colinfo
, UINT
*sz
);
117 static void msi_free_colinfo( MSICOLUMNINFO
*colinfo
, UINT count
);
120 void msi_table_set_strref(UINT bytes_per_strref
)
122 _Columns_cols
[0].offset
= 0;
123 _Columns_cols
[1].offset
= bytes_per_strref
;
124 _Columns_cols
[2].offset
= _Columns_cols
[1].offset
+ sizeof(USHORT
);
125 _Columns_cols
[3].offset
= _Columns_cols
[2].offset
+ bytes_per_strref
;
128 static inline UINT
bytes_per_column( const MSICOLUMNINFO
*col
)
130 if( MSITYPE_IS_BINARY(col
->type
) )
132 if( col
->type
& MSITYPE_STRING
)
133 return _Columns_cols
[1].offset
;
134 if( (col
->type
& 0xff) > 4 )
135 ERR("Invalid column size!\n");
136 return col
->type
& 0xff;
139 static int utf2mime(int x
)
141 if( (x
>='0') && (x
<='9') )
143 if( (x
>='A') && (x
<='Z') )
145 if( (x
>='a') && (x
<='z') )
154 LPWSTR
encode_streamname(BOOL bTable
, LPCWSTR in
)
156 DWORD count
= MAX_STREAM_NAME
;
161 count
= lstrlenW( in
)+2;
162 out
= msi_alloc( count
*sizeof(WCHAR
) );
178 if( ( ch
< 0x80 ) && ( utf2mime(ch
) >= 0 ) )
180 ch
= utf2mime(ch
) + 0x4800;
182 if( next
&& (next
<0x80) )
184 next
= utf2mime(next
);
195 ERR("Failed to encode stream name (%s)\n",debugstr_w(in
));
200 static int mime2utf(int x
)
207 return x
- 10 - 26 + 'a';
208 if( x
== (10+26+26) )
213 BOOL
decode_streamname(LPCWSTR in
, LPWSTR out
)
218 while ( (ch
= *in
++) )
220 if( (ch
>= 0x3800 ) && (ch
< 0x4840 ) )
223 ch
= mime2utf(ch
-0x4800);
227 *out
++ = mime2utf(ch
&0x3f);
229 ch
= mime2utf((ch
>>6)&0x3f);
239 void enum_stream_names( IStorage
*stg
)
241 IEnumSTATSTG
*stgenum
= NULL
;
247 r
= IStorage_EnumElements( stg
, 0, NULL
, 0, &stgenum
);
255 r
= IEnumSTATSTG_Next( stgenum
, 1, &stat
, &count
);
256 if( FAILED( r
) || !count
)
258 decode_streamname( stat
.pwcsName
, name
);
259 TRACE("stream %2d -> %s %s\n", n
,
260 debugstr_w(stat
.pwcsName
), debugstr_w(name
) );
264 IEnumSTATSTG_Release( stgenum
);
267 UINT
read_stream_data( IStorage
*stg
, LPCWSTR stname
, BOOL table
,
268 BYTE
**pdata
, UINT
*psz
)
271 UINT ret
= ERROR_FUNCTION_FAILED
;
278 encname
= encode_streamname(table
, stname
);
280 TRACE("%s -> %s\n",debugstr_w(stname
),debugstr_w(encname
));
282 r
= IStorage_OpenStream(stg
, encname
, NULL
,
283 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, &stm
);
287 WARN("open stream failed r = %08x - empty table?\n", r
);
291 r
= IStream_Stat(stm
, &stat
, STATFLAG_NONAME
);
294 WARN("open stream failed r = %08x!\n", r
);
298 if( stat
.cbSize
.QuadPart
>> 32 )
304 sz
= stat
.cbSize
.QuadPart
;
305 data
= msi_alloc( sz
);
308 WARN("couldn't allocate memory r=%08x!\n", r
);
309 ret
= ERROR_NOT_ENOUGH_MEMORY
;
313 r
= IStream_Read(stm
, data
, sz
, &count
);
314 if( FAILED( r
) || ( count
!= sz
) )
317 WARN("read stream failed r = %08x!\n", r
);
326 IStream_Release( stm
);
331 UINT
db_get_raw_stream( MSIDATABASE
*db
, LPCWSTR stname
, IStream
**stm
)
336 encname
= encode_streamname(FALSE
, stname
);
338 TRACE("%s -> %s\n",debugstr_w(stname
),debugstr_w(encname
));
340 r
= IStorage_OpenStream(db
->storage
, encname
, NULL
,
341 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, stm
);
344 MSITRANSFORM
*transform
;
346 LIST_FOR_EACH_ENTRY( transform
, &db
->transforms
, MSITRANSFORM
, entry
)
348 TRACE("looking for %s in transform storage\n", debugstr_w(stname
) );
349 r
= IStorage_OpenStream( transform
->stg
, encname
, NULL
,
350 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, stm
);
358 return SUCCEEDED(r
) ? ERROR_SUCCESS
: ERROR_FUNCTION_FAILED
;
361 UINT
read_raw_stream_data( MSIDATABASE
*db
, LPCWSTR stname
,
362 USHORT
**pdata
, UINT
*psz
)
365 UINT ret
= ERROR_FUNCTION_FAILED
;
371 r
= db_get_raw_stream( db
, stname
, &stm
);
372 if( r
!= ERROR_SUCCESS
)
374 r
= IStream_Stat(stm
, &stat
, STATFLAG_NONAME
);
377 WARN("open stream failed r = %08x!\n", r
);
381 if( stat
.cbSize
.QuadPart
>> 32 )
387 sz
= stat
.cbSize
.QuadPart
;
388 data
= msi_alloc( sz
);
391 WARN("couldn't allocate memory r=%08x!\n", r
);
392 ret
= ERROR_NOT_ENOUGH_MEMORY
;
396 r
= IStream_Read(stm
, data
, sz
, &count
);
397 if( FAILED( r
) || ( count
!= sz
) )
400 WARN("read stream failed r = %08x!\n", r
);
409 IStream_Release( stm
);
414 UINT
write_stream_data( IStorage
*stg
, LPCWSTR stname
,
415 LPCVOID data
, UINT sz
, BOOL bTable
)
418 UINT ret
= ERROR_FUNCTION_FAILED
;
425 encname
= encode_streamname(bTable
, stname
);
426 r
= IStorage_OpenStream( stg
, encname
, NULL
,
427 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, &stm
);
430 r
= IStorage_CreateStream( stg
, encname
,
431 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, 0, &stm
);
436 WARN("open stream failed r = %08x\n", r
);
441 r
= IStream_SetSize( stm
, size
);
444 WARN("Failed to SetSize\n");
449 r
= IStream_Seek( stm
, pos
, STREAM_SEEK_SET
, NULL
);
452 WARN("Failed to Seek\n");
458 r
= IStream_Write(stm
, data
, sz
, &count
);
459 if( FAILED( r
) || ( count
!= sz
) )
461 WARN("Failed to Write\n");
469 IStream_Release( stm
);
474 static void free_table( MSITABLE
*table
)
477 for( i
=0; i
<table
->row_count
; i
++ )
478 msi_free( table
->data
[i
] );
479 msi_free( table
->data
);
480 for( i
=0; i
<table
->nonpersistent_row_count
; i
++ )
481 msi_free( table
->nonpersistent_data
[i
] );
482 msi_free( table
->nonpersistent_data
);
483 if( (table
->colinfo
!= _Tables_cols
) &&
484 (table
->colinfo
!= _Columns_cols
) )
486 msi_free_colinfo( table
->colinfo
, table
->col_count
);
487 msi_free( table
->colinfo
);
492 static UINT
msi_table_get_row_size( const MSICOLUMNINFO
*cols
, UINT count
)
494 const MSICOLUMNINFO
*last_col
= &cols
[count
-1];
497 return last_col
->offset
+ bytes_per_column( last_col
);
500 /* add this table to the list of cached tables in the database */
501 static UINT
read_table_from_storage( MSITABLE
*t
, IStorage
*stg
)
503 BYTE
*rawdata
= NULL
;
504 UINT rawsize
= 0, i
, j
, row_size
= 0;
506 TRACE("%s\n",debugstr_w(t
->name
));
508 row_size
= msi_table_get_row_size( t
->colinfo
, t
->col_count
);
510 /* if we can't read the table, just assume that it's empty */
511 read_stream_data( stg
, t
->name
, TRUE
, &rawdata
, &rawsize
);
513 return ERROR_SUCCESS
;
515 TRACE("Read %d bytes\n", rawsize
);
517 if( rawsize
% row_size
)
519 WARN("Table size is invalid %d/%d\n", rawsize
, row_size
);
523 t
->row_count
= rawsize
/ row_size
;
524 t
->data
= msi_alloc_zero( t
->row_count
* sizeof (USHORT
*) );
528 /* transpose all the data */
529 TRACE("Transposing data from %d rows\n", t
->row_count
);
530 for( i
=0; i
<t
->row_count
; i
++ )
532 t
->data
[i
] = msi_alloc( row_size
);
536 for( j
=0; j
<t
->col_count
; j
++ )
538 UINT ofs
= t
->colinfo
[j
].offset
;
539 UINT n
= bytes_per_column( &t
->colinfo
[j
] );
542 if ( n
!= 2 && n
!= 3 && n
!= 4 )
544 ERR("oops - unknown column width %d\n", n
);
548 for ( k
= 0; k
< n
; k
++ )
549 t
->data
[i
][ofs
+ k
] = rawdata
[ofs
*t
->row_count
+ i
* n
+ k
];
554 return ERROR_SUCCESS
;
557 return ERROR_FUNCTION_FAILED
;
560 void free_cached_tables( MSIDATABASE
*db
)
562 while( !list_empty( &db
->tables
) )
564 MSITABLE
*t
= LIST_ENTRY( list_head( &db
->tables
), MSITABLE
, entry
);
566 list_remove( &t
->entry
);
571 static MSITABLE
*find_cached_table( MSIDATABASE
*db
, LPCWSTR name
)
575 LIST_FOR_EACH_ENTRY( t
, &db
->tables
, MSITABLE
, entry
)
576 if( !lstrcmpW( name
, t
->name
) )
582 static UINT
table_get_column_info( MSIDATABASE
*db
, LPCWSTR name
, MSICOLUMNINFO
**pcols
, UINT
*pcount
)
584 UINT r
, column_count
= 0;
585 MSICOLUMNINFO
*columns
;
587 /* get the number of columns in this table */
589 r
= get_tablecolumns( db
, name
, NULL
, &column_count
);
590 if( r
!= ERROR_SUCCESS
)
593 /* if there's no columns, there's no table */
594 if( column_count
== 0 )
595 return ERROR_INVALID_PARAMETER
;
597 TRACE("Table %s found\n", debugstr_w(name
) );
599 columns
= msi_alloc( column_count
*sizeof (MSICOLUMNINFO
) );
601 return ERROR_FUNCTION_FAILED
;
603 r
= get_tablecolumns( db
, name
, columns
, &column_count
);
604 if( r
!= ERROR_SUCCESS
)
607 return ERROR_FUNCTION_FAILED
;
611 *pcount
= column_count
;
616 UINT
msi_create_table( MSIDATABASE
*db
, LPCWSTR name
, column_info
*col_info
,
617 BOOL persistent
, MSITABLE
**table_ret
)
621 MSIRECORD
*rec
= NULL
;
626 /* only add tables that don't exist already */
627 if( TABLE_Exists(db
, name
) )
629 WARN("table %s exists\n", debugstr_w(name
));
630 return ERROR_BAD_QUERY_SYNTAX
;
633 table
= msi_alloc( sizeof (MSITABLE
) + lstrlenW(name
)*sizeof (WCHAR
) );
635 return ERROR_FUNCTION_FAILED
;
637 table
->ref_count
= 1;
638 table
->row_count
= 0;
640 table
->nonpersistent_row_count
= 0;
641 table
->nonpersistent_data
= NULL
;
642 table
->colinfo
= NULL
;
643 table
->col_count
= 0;
644 table
->persistent
= persistent
;
645 lstrcpyW( table
->name
, name
);
647 for( col
= col_info
; col
; col
= col
->next
)
650 table
->colinfo
= msi_alloc( table
->col_count
* sizeof(MSICOLUMNINFO
) );
654 return ERROR_FUNCTION_FAILED
;
657 for( i
= 0, col
= col_info
; col
; i
++, col
= col
->next
)
659 table
->colinfo
[ i
].tablename
= strdupW( col
->table
);
660 table
->colinfo
[ i
].number
= i
+ 1;
661 table
->colinfo
[ i
].colname
= strdupW( col
->column
);
662 table
->colinfo
[ i
].type
= col
->type
;
663 table
->colinfo
[ i
].offset
= 0;
664 table
->colinfo
[ i
].ref_count
= 0;
665 table
->colinfo
[ i
].hash_table
= NULL
;
667 table_calc_column_offsets( table
->colinfo
, table
->col_count
);
669 r
= TABLE_CreateView( db
, szTables
, &tv
);
670 TRACE("CreateView returned %x\n", r
);
677 r
= tv
->ops
->execute( tv
, 0 );
678 TRACE("tv execute returned %x\n", r
);
682 rec
= MSI_CreateRecord( 1 );
686 r
= MSI_RecordSetStringW( rec
, 1, name
);
690 r
= tv
->ops
->insert_row( tv
, rec
, !persistent
);
691 TRACE("insert_row returned %x\n", r
);
695 tv
->ops
->delete( tv
);
698 msiobj_release( &rec
->hdr
);
703 /* add each column to the _Columns table */
704 r
= TABLE_CreateView( db
, szColumns
, &tv
);
708 r
= tv
->ops
->execute( tv
, 0 );
709 TRACE("tv execute returned %x\n", r
);
713 rec
= MSI_CreateRecord( 4 );
717 r
= MSI_RecordSetStringW( rec
, 1, name
);
722 * need to set the table, column number, col name and type
723 * for each column we enter in the table
726 for( col
= col_info
; col
; col
= col
->next
)
728 r
= MSI_RecordSetInteger( rec
, 2, nField
);
732 r
= MSI_RecordSetStringW( rec
, 3, col
->column
);
736 r
= MSI_RecordSetInteger( rec
, 4, col
->type
);
740 r
= tv
->ops
->insert_row( tv
, rec
, FALSE
);
752 msiobj_release( &rec
->hdr
);
753 /* FIXME: remove values from the string table on error */
755 tv
->ops
->delete( tv
);
757 if (r
== ERROR_SUCCESS
)
759 list_add_head( &db
->tables
, &table
->entry
);
768 static UINT
get_table( MSIDATABASE
*db
, LPCWSTR name
, MSITABLE
**table_ret
)
773 /* first, see if the table is cached */
774 table
= find_cached_table( db
, name
);
778 return ERROR_SUCCESS
;
781 /* nonexistent tables should be interpreted as empty tables */
782 table
= msi_alloc( sizeof (MSITABLE
) + lstrlenW(name
)*sizeof (WCHAR
) );
784 return ERROR_FUNCTION_FAILED
;
786 table
->row_count
= 0;
788 table
->nonpersistent_row_count
= 0;
789 table
->nonpersistent_data
= NULL
;
790 table
->colinfo
= NULL
;
791 table
->col_count
= 0;
792 table
->persistent
= TRUE
;
793 lstrcpyW( table
->name
, name
);
795 /* these two tables are special - we know the column types already */
796 if( !lstrcmpW( name
, szColumns
) )
798 table
->colinfo
= _Columns_cols
;
799 table
->col_count
= sizeof(_Columns_cols
)/sizeof(_Columns_cols
[0]);
801 else if( !lstrcmpW( name
, szTables
) )
803 table
->colinfo
= _Tables_cols
;
804 table
->col_count
= sizeof(_Tables_cols
)/sizeof(_Tables_cols
[0]);
808 r
= table_get_column_info( db
, name
, &table
->colinfo
, &table
->col_count
);
809 if (r
!= ERROR_SUCCESS
)
811 free_table ( table
);
816 r
= read_table_from_storage( table
, db
->storage
);
817 if( r
!= ERROR_SUCCESS
)
823 list_add_head( &db
->tables
, &table
->entry
);
825 return ERROR_SUCCESS
;
828 static UINT
save_table( MSIDATABASE
*db
, const MSITABLE
*t
)
830 BYTE
*rawdata
= NULL
, *p
;
831 UINT rawsize
, r
, i
, j
, row_size
;
833 /* Nothing to do for non-persistent tables */
835 return ERROR_SUCCESS
;
837 TRACE("Saving %s\n", debugstr_w( t
->name
) );
839 row_size
= msi_table_get_row_size( t
->colinfo
, t
->col_count
);
841 rawsize
= t
->row_count
* row_size
;
842 rawdata
= msi_alloc_zero( rawsize
);
845 r
= ERROR_NOT_ENOUGH_MEMORY
;
850 for( i
=0; i
<t
->col_count
; i
++ )
852 for( j
=0; j
<t
->row_count
; j
++ )
854 UINT offset
= t
->colinfo
[i
].offset
;
856 *p
++ = t
->data
[j
][offset
];
857 *p
++ = t
->data
[j
][offset
+ 1];
858 if( 4 == bytes_per_column( &t
->colinfo
[i
] ) )
860 *p
++ = t
->data
[j
][offset
+ 2];
861 *p
++ = t
->data
[j
][offset
+ 3];
866 TRACE("writing %d bytes\n", rawsize
);
867 r
= write_stream_data( db
->storage
, t
->name
, rawdata
, rawsize
, TRUE
);
875 static void table_calc_column_offsets( MSICOLUMNINFO
*colinfo
, DWORD count
)
879 for( i
=0; colinfo
&& (i
<count
); i
++ )
881 assert( (i
+1) == colinfo
[ i
].number
);
883 colinfo
[i
].offset
= colinfo
[ i
- 1 ].offset
884 + bytes_per_column( &colinfo
[ i
- 1 ] );
886 colinfo
[i
].offset
= 0;
887 TRACE("column %d is [%s] with type %08x ofs %d\n",
888 colinfo
[i
].number
, debugstr_w(colinfo
[i
].colname
),
889 colinfo
[i
].type
, colinfo
[i
].offset
);
893 static UINT
get_defaulttablecolumns( LPCWSTR name
, MSICOLUMNINFO
*colinfo
, UINT
*sz
)
895 const MSICOLUMNINFO
*p
;
898 TRACE("%s\n", debugstr_w(name
));
900 if (!lstrcmpW( name
, szTables
))
905 else if (!lstrcmpW( name
, szColumns
))
911 return ERROR_FUNCTION_FAILED
;
913 /* duplicate the string data so we can free it in msi_free_colinfo */
916 if (colinfo
&& (i
< *sz
) )
918 memcpy( &colinfo
[i
], &p
[i
], sizeof(MSICOLUMNINFO
) );
919 colinfo
[i
].tablename
= strdupW( p
[i
].tablename
);
920 colinfo
[i
].colname
= strdupW( p
[i
].colname
);
922 if( colinfo
&& (i
>= *sz
) )
925 table_calc_column_offsets( colinfo
, n
);
927 return ERROR_SUCCESS
;
930 static void msi_free_colinfo( MSICOLUMNINFO
*colinfo
, UINT count
)
934 for( i
=0; i
<count
; i
++ )
936 msi_free( (LPWSTR
) colinfo
[i
].tablename
);
937 msi_free( (LPWSTR
) colinfo
[i
].colname
);
938 msi_free( colinfo
[i
].hash_table
);
942 static LPWSTR
msi_makestring( const MSIDATABASE
*db
, UINT stringid
)
944 return strdupW(msi_string_lookup_id( db
->strings
, stringid
));
947 static UINT
read_table_int(BYTE
*const *data
, UINT row
, UINT col
, UINT bytes
)
951 for (i
= 0; i
< bytes
; i
++)
952 ret
+= (data
[row
][col
+ i
] << i
* 8);
957 static UINT
get_tablecolumns( MSIDATABASE
*db
,
958 LPCWSTR szTableName
, MSICOLUMNINFO
*colinfo
, UINT
*sz
)
960 UINT r
, i
, n
=0, table_id
, count
, maxcount
= *sz
;
961 MSITABLE
*table
= NULL
;
963 TRACE("%s\n", debugstr_w(szTableName
));
965 /* first check if there is a default table with that name */
966 r
= get_defaulttablecolumns( szTableName
, colinfo
, sz
);
967 if( ( r
== ERROR_SUCCESS
) && *sz
)
970 r
= get_table( db
, szColumns
, &table
);
971 if( r
!= ERROR_SUCCESS
)
973 ERR("couldn't load _Columns table\n");
974 return ERROR_FUNCTION_FAILED
;
977 /* convert table and column names to IDs from the string table */
978 r
= msi_string2idW( db
->strings
, szTableName
, &table_id
);
979 if( r
!= ERROR_SUCCESS
)
981 WARN("Couldn't find id for %s\n", debugstr_w(szTableName
));
985 TRACE("Table id is %d, row count is %d\n", table_id
, table
->row_count
);
987 /* Note: _Columns table doesn't have non-persistent data */
989 /* if maxcount is non-zero, assume it's exactly right for this table */
990 memset( colinfo
, 0, maxcount
*sizeof(*colinfo
) );
991 count
= table
->row_count
;
992 for( i
=0; i
<count
; i
++ )
994 if( read_table_int(table
->data
, i
, 0, db
->bytes_per_strref
) != table_id
)
998 UINT id
= read_table_int(table
->data
, i
, _Columns_cols
[2].offset
, db
->bytes_per_strref
);
999 UINT col
= read_table_int(table
->data
, i
, _Columns_cols
[1].offset
, sizeof(USHORT
)) - (1<<15);
1001 /* check the column number is in range */
1002 if (col
<1 || col
>maxcount
)
1004 ERR("column %d out of range\n", col
);
1008 /* check if this column was already set */
1009 if (colinfo
[ col
- 1 ].number
)
1011 ERR("duplicate column %d\n", col
);
1015 colinfo
[ col
- 1 ].tablename
= msi_makestring( db
, table_id
);
1016 colinfo
[ col
- 1 ].number
= col
;
1017 colinfo
[ col
- 1 ].colname
= msi_makestring( db
, id
);
1018 colinfo
[ col
- 1 ].type
= read_table_int(table
->data
, i
, _Columns_cols
[3].offset
, sizeof(USHORT
)) - (1<<15);
1019 colinfo
[ col
- 1 ].offset
= 0;
1020 colinfo
[ col
- 1 ].ref_count
= 0;
1021 colinfo
[ col
- 1 ].hash_table
= NULL
;
1026 TRACE("%s has %d columns\n", debugstr_w(szTableName
), n
);
1028 if (colinfo
&& n
!= maxcount
)
1030 ERR("missing column in table %s\n", debugstr_w(szTableName
));
1031 msi_free_colinfo(colinfo
, maxcount
);
1032 return ERROR_FUNCTION_FAILED
;
1035 table_calc_column_offsets( colinfo
, n
);
1038 return ERROR_SUCCESS
;
1041 static void msi_update_table_columns( MSIDATABASE
*db
, LPCWSTR name
)
1047 table
= find_cached_table( db
, name
);
1048 msi_free( table
->colinfo
);
1049 table_get_column_info( db
, name
, &table
->colinfo
, &table
->col_count
);
1051 size
= msi_table_get_row_size( table
->colinfo
, table
->col_count
);
1052 offset
= table
->colinfo
[table
->col_count
- 1].offset
;
1054 for ( n
= 0; n
< table
->row_count
; n
++ )
1056 table
->data
[n
] = msi_realloc( table
->data
[n
], size
);
1057 table
->data
[n
][offset
] = (BYTE
)MSI_NULL_INTEGER
;
1061 /* try to find the table name in the _Tables table */
1062 BOOL
TABLE_Exists( MSIDATABASE
*db
, LPCWSTR name
)
1064 UINT r
, table_id
= 0, i
, count
;
1065 MSITABLE
*table
= NULL
;
1067 if( !lstrcmpW( name
, szTables
) )
1069 if( !lstrcmpW( name
, szColumns
) )
1072 r
= msi_string2idW( db
->strings
, name
, &table_id
);
1073 if( r
!= ERROR_SUCCESS
)
1075 TRACE("Couldn't find id for %s\n", debugstr_w(name
));
1079 r
= get_table( db
, szTables
, &table
);
1080 if( r
!= ERROR_SUCCESS
)
1082 ERR("table %s not available\n", debugstr_w(szTables
));
1086 count
= table
->row_count
;
1087 for( i
=0; i
<count
; i
++ )
1088 if( table
->data
[ i
][ 0 ] == table_id
)
1094 count
= table
->nonpersistent_row_count
;
1095 for( i
=0; i
<count
; i
++ )
1096 if( table
->nonpersistent_data
[ i
][ 0 ] == table_id
)
1102 TRACE("Searched %d tables, but %d was not found\n", count
, table_id
);
1107 /* below is the query interface to a table */
1109 typedef struct tagMSITABLEVIEW
1114 MSICOLUMNINFO
*columns
;
1120 static UINT
TABLE_fetch_int( struct tagMSIVIEW
*view
, UINT row
, UINT col
, UINT
*val
)
1122 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1127 return ERROR_INVALID_PARAMETER
;
1129 if( (col
==0) || (col
>tv
->num_cols
) )
1130 return ERROR_INVALID_PARAMETER
;
1132 /* how many rows are there ? */
1133 if( row
>= tv
->table
->row_count
+ tv
->table
->nonpersistent_row_count
)
1134 return ERROR_NO_MORE_ITEMS
;
1136 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1138 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1139 ERR("%p %p\n", tv
, tv
->columns
);
1140 return ERROR_FUNCTION_FAILED
;
1143 if (row
>= tv
->table
->row_count
)
1145 row
-= tv
->table
->row_count
;
1146 data
= tv
->table
->nonpersistent_data
;
1149 data
= tv
->table
->data
;
1151 n
= bytes_per_column( &tv
->columns
[col
-1] );
1152 if (n
!= 2 && n
!= 3 && n
!= 4)
1154 ERR("oops! what is %d bytes per column?\n", n
);
1155 return ERROR_FUNCTION_FAILED
;
1158 offset
= tv
->columns
[col
-1].offset
;
1159 *val
= read_table_int(data
, row
, offset
, n
);
1161 /* TRACE("Data [%d][%d] = %d\n", row, col, *val ); */
1163 return ERROR_SUCCESS
;
1167 * We need a special case for streams, as we need to reference column with
1168 * the name of the stream in the same table, and the table name
1169 * which may not be available at higher levels of the query
1171 static UINT
TABLE_fetch_stream( struct tagMSIVIEW
*view
, UINT row
, UINT col
, IStream
**stm
)
1173 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1174 UINT ival
= 0, refcol
= 0, r
;
1178 static const WCHAR szDot
[] = { '.', 0 };
1181 if( !view
->ops
->fetch_int
)
1182 return ERROR_INVALID_PARAMETER
;
1185 * The column marked with the type stream data seems to have a single number
1186 * which references the column containing the name of the stream data
1188 * Fetch the column to reference first.
1190 r
= view
->ops
->fetch_int( view
, row
, col
, &ival
);
1191 if( r
!= ERROR_SUCCESS
)
1194 /* check the column value is in range */
1195 if (ival
< 0 || ival
> tv
->num_cols
|| ival
== col
)
1197 ERR("bad column ref (%u) for stream\n", ival
);
1198 return ERROR_FUNCTION_FAILED
;
1201 if ( tv
->columns
[ival
- 1].type
& MSITYPE_STRING
)
1203 /* now get the column with the name of the stream */
1204 r
= view
->ops
->fetch_int( view
, row
, ival
, &refcol
);
1205 if ( r
!= ERROR_SUCCESS
)
1208 /* lookup the string value from the string table */
1209 sval
= msi_string_lookup_id( tv
->db
->strings
, refcol
);
1211 return ERROR_INVALID_PARAMETER
;
1215 static const WCHAR fmt
[] = { '%','d',0 };
1216 sprintfW( number
, fmt
, ival
);
1220 len
= lstrlenW( tv
->name
) + 2 + lstrlenW( sval
);
1221 full_name
= msi_alloc( len
*sizeof(WCHAR
) );
1222 lstrcpyW( full_name
, tv
->name
);
1223 lstrcatW( full_name
, szDot
);
1224 lstrcatW( full_name
, sval
);
1226 r
= db_get_raw_stream( tv
->db
, full_name
, stm
);
1228 ERR("fetching stream %s, error = %d\n",debugstr_w(full_name
), r
);
1229 msi_free( full_name
);
1234 static UINT
TABLE_set_int( MSITABLEVIEW
*tv
, UINT row
, UINT col
, UINT val
)
1240 return ERROR_INVALID_PARAMETER
;
1242 if( (col
==0) || (col
>tv
->num_cols
) )
1243 return ERROR_INVALID_PARAMETER
;
1245 if( row
>= tv
->table
->row_count
+ tv
->table
->nonpersistent_row_count
)
1246 return ERROR_INVALID_PARAMETER
;
1248 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1250 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1251 ERR("%p %p\n", tv
, tv
->columns
);
1252 return ERROR_FUNCTION_FAILED
;
1255 msi_free( tv
->columns
[col
-1].hash_table
);
1256 tv
->columns
[col
-1].hash_table
= NULL
;
1258 if (row
>= tv
->table
->row_count
)
1260 row
-= tv
->table
->row_count
;
1261 data
= tv
->table
->nonpersistent_data
;
1264 data
= tv
->table
->data
;
1266 n
= bytes_per_column( &tv
->columns
[col
-1] );
1267 if ( n
!= 2 && n
!= 3 && n
!= 4 )
1269 ERR("oops! what is %d bytes per column?\n", n
);
1270 return ERROR_FUNCTION_FAILED
;
1273 offset
= tv
->columns
[col
-1].offset
;
1274 for ( i
= 0; i
< n
; i
++ )
1275 data
[row
][offset
+ i
] = (val
>> i
* 8) & 0xff;
1277 return ERROR_SUCCESS
;
1280 static UINT
TABLE_get_row( struct tagMSIVIEW
*view
, UINT row
, MSIRECORD
**rec
)
1282 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1285 return ERROR_INVALID_PARAMETER
;
1287 return msi_view_get_row(tv
->db
, view
, row
, rec
);
1290 static UINT
TABLE_set_row( struct tagMSIVIEW
*view
, UINT row
, MSIRECORD
*rec
, UINT mask
)
1292 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1293 UINT i
, val
, r
= ERROR_SUCCESS
;
1296 return ERROR_INVALID_PARAMETER
;
1298 /* test if any of the mask bits are invalid */
1299 if ( mask
>= (1<<tv
->num_cols
) )
1300 return ERROR_INVALID_PARAMETER
;
1302 for ( i
= 0; i
< tv
->num_cols
; i
++ )
1306 /* only update the fields specified in the mask */
1307 if ( !(mask
&(1<<i
)) )
1310 /* if row >= tv->table->row_count then it is a non-persistent row */
1311 persistent
= tv
->table
->persistent
&& (row
< tv
->table
->row_count
);
1312 /* FIXME: should we allow updating keys? */
1315 if ( !MSI_RecordIsNull( rec
, i
+ 1 ) )
1317 if ( MSITYPE_IS_BINARY(tv
->columns
[ i
].type
) )
1319 val
= 1; /* refers to the first key column */
1321 else if ( tv
->columns
[i
].type
& MSITYPE_STRING
)
1323 LPCWSTR sval
= MSI_RecordGetString( rec
, i
+ 1 );
1324 val
= msi_addstringW( tv
->db
->strings
, 0, sval
, -1, 1,
1325 persistent
? StringPersistent
: StringNonPersistent
);
1327 else if ( 2 == bytes_per_column( &tv
->columns
[ i
] ) )
1329 val
= 0x8000 + MSI_RecordGetInteger( rec
, i
+ 1 );
1330 if ( val
& 0xffff0000 )
1332 ERR("field %u value %d out of range\n", i
+1, val
- 0x8000 );
1333 return ERROR_FUNCTION_FAILED
;
1338 INT ival
= MSI_RecordGetInteger( rec
, i
+ 1 );
1339 val
= ival
^ 0x80000000;
1343 r
= TABLE_set_int( tv
, row
, i
+1, val
);
1344 if ( r
!= ERROR_SUCCESS
)
1350 static UINT
table_create_new_row( struct tagMSIVIEW
*view
, UINT
*num
, BOOL temporary
)
1352 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1358 TRACE("%p %s\n", view
, temporary
? "TRUE" : "FALSE");
1361 return ERROR_INVALID_PARAMETER
;
1363 row
= msi_alloc_zero( tv
->row_size
);
1365 return ERROR_NOT_ENOUGH_MEMORY
;
1369 row_count
= &tv
->table
->nonpersistent_row_count
;
1370 data_ptr
= &tv
->table
->nonpersistent_data
;
1371 *num
= tv
->table
->row_count
+ tv
->table
->nonpersistent_row_count
;
1375 row_count
= &tv
->table
->row_count
;
1376 data_ptr
= &tv
->table
->data
;
1377 *num
= tv
->table
->row_count
;
1380 sz
= (*row_count
+ 1) * sizeof (BYTE
*);
1382 p
= msi_realloc( *data_ptr
, sz
);
1384 p
= msi_alloc( sz
);
1388 return ERROR_NOT_ENOUGH_MEMORY
;
1392 (*data_ptr
)[*row_count
] = row
;
1395 return ERROR_SUCCESS
;
1398 static UINT
TABLE_execute( struct tagMSIVIEW
*view
, MSIRECORD
*record
)
1400 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1402 TRACE("%p %p\n", tv
, record
);
1404 TRACE("There are %d columns\n", tv
->num_cols
);
1406 return ERROR_SUCCESS
;
1409 static UINT
TABLE_close( struct tagMSIVIEW
*view
)
1411 TRACE("%p\n", view
);
1413 return ERROR_SUCCESS
;
1416 static UINT
TABLE_get_dimensions( struct tagMSIVIEW
*view
, UINT
*rows
, UINT
*cols
)
1418 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1420 TRACE("%p %p %p\n", view
, rows
, cols
);
1423 *cols
= tv
->num_cols
;
1427 return ERROR_INVALID_PARAMETER
;
1428 *rows
= tv
->table
->row_count
+ tv
->table
->nonpersistent_row_count
;
1431 return ERROR_SUCCESS
;
1434 static UINT
TABLE_get_column_info( struct tagMSIVIEW
*view
,
1435 UINT n
, LPWSTR
*name
, UINT
*type
)
1437 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1439 TRACE("%p %d %p %p\n", tv
, n
, name
, type
);
1441 if( ( n
== 0 ) || ( n
> tv
->num_cols
) )
1442 return ERROR_INVALID_PARAMETER
;
1446 *name
= strdupW( tv
->columns
[n
-1].colname
);
1448 return ERROR_FUNCTION_FAILED
;
1451 *type
= tv
->columns
[n
-1].type
;
1453 return ERROR_SUCCESS
;
1456 static UINT
msi_table_find_row( MSITABLEVIEW
*tv
, MSIRECORD
*rec
, UINT
*row
);
1458 static UINT
table_validate_new( MSITABLEVIEW
*tv
, MSIRECORD
*rec
)
1462 /* check there's no null values where they're not allowed */
1463 for( i
= 0; i
< tv
->num_cols
; i
++ )
1465 if ( tv
->columns
[i
].type
& MSITYPE_NULLABLE
)
1468 if ( MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
1469 TRACE("skipping binary column\n");
1470 else if ( tv
->columns
[i
].type
& MSITYPE_STRING
)
1474 str
= MSI_RecordGetString( rec
, i
+1 );
1475 if (str
== NULL
|| str
[0] == 0)
1476 return ERROR_INVALID_DATA
;
1482 n
= MSI_RecordGetInteger( rec
, i
+1 );
1483 if (n
== MSI_NULL_INTEGER
)
1484 return ERROR_INVALID_DATA
;
1488 /* check there's no duplicate keys */
1489 r
= msi_table_find_row( tv
, rec
, &row
);
1490 if (r
== ERROR_SUCCESS
)
1491 return ERROR_INVALID_DATA
;
1493 return ERROR_SUCCESS
;
1496 static UINT
TABLE_insert_row( struct tagMSIVIEW
*view
, MSIRECORD
*rec
, BOOL temporary
)
1498 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1501 TRACE("%p %p %s\n", tv
, rec
, temporary
? "TRUE" : "FALSE" );
1503 /* check that the key is unique - can we find a matching row? */
1504 r
= table_validate_new( tv
, rec
);
1505 if( r
!= ERROR_SUCCESS
)
1506 return ERROR_FUNCTION_FAILED
;
1508 r
= table_create_new_row( view
, &row
, temporary
);
1509 TRACE("insert_row returned %08x\n", r
);
1510 if( r
!= ERROR_SUCCESS
)
1513 return TABLE_set_row( view
, row
, rec
, (1<<tv
->num_cols
) - 1 );
1516 static UINT
TABLE_delete_row( struct tagMSIVIEW
*view
, UINT row
)
1518 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1519 UINT r
, num_rows
, num_cols
;
1522 TRACE("%p %d\n", tv
, row
);
1525 return ERROR_INVALID_PARAMETER
;
1527 r
= TABLE_get_dimensions( view
, &num_rows
, &num_cols
);
1528 if ( r
!= ERROR_SUCCESS
)
1531 if ( row
>= num_rows
)
1532 return ERROR_FUNCTION_FAILED
;
1534 tv
->table
->row_count
--;
1536 if ( row
== num_rows
- 1 )
1537 return ERROR_SUCCESS
;
1539 dest
= tv
->table
->data
[row
];
1540 src
= tv
->table
->data
[row
+ 1];
1541 memmove(dest
, src
, (num_rows
- row
- 1) * tv
->row_size
);
1542 return ERROR_SUCCESS
;
1545 static UINT
msi_table_update(struct tagMSIVIEW
*view
, MSIRECORD
*rec
, UINT row
)
1547 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1550 /* FIXME: MsiViewFetch should set rec index 0 to some ID that
1551 * sets the fetched record apart from other records
1555 return ERROR_INVALID_PARAMETER
;
1557 r
= msi_table_find_row(tv
, rec
, &new_row
);
1558 if (r
!= ERROR_SUCCESS
)
1560 ERR("can't find row to modify\n");
1561 return ERROR_SUCCESS
;
1564 /* the row cannot be changed */
1565 if (row
!= new_row
+ 1)
1566 return ERROR_FUNCTION_FAILED
;
1568 return TABLE_set_row(view
, new_row
, rec
, (1 << tv
->num_cols
) - 1);
1571 static UINT
TABLE_modify( struct tagMSIVIEW
*view
, MSIMODIFY eModifyMode
,
1572 MSIRECORD
*rec
, UINT row
)
1574 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1577 TRACE("%p %d %p\n", view
, eModifyMode
, rec
);
1579 switch (eModifyMode
)
1581 case MSIMODIFY_VALIDATE_NEW
:
1582 r
= table_validate_new( tv
, rec
);
1585 case MSIMODIFY_INSERT_TEMPORARY
:
1586 r
= table_validate_new( tv
, rec
);
1587 if (r
!= ERROR_SUCCESS
)
1589 r
= TABLE_insert_row( view
, rec
, TRUE
);
1592 case MSIMODIFY_UPDATE
:
1593 r
= msi_table_update( view
, rec
, row
);
1596 case MSIMODIFY_REFRESH
:
1597 case MSIMODIFY_INSERT
:
1598 case MSIMODIFY_ASSIGN
:
1599 case MSIMODIFY_REPLACE
:
1600 case MSIMODIFY_MERGE
:
1601 case MSIMODIFY_DELETE
:
1602 case MSIMODIFY_VALIDATE
:
1603 case MSIMODIFY_VALIDATE_FIELD
:
1604 case MSIMODIFY_VALIDATE_DELETE
:
1605 FIXME("%p %d %p - mode not implemented\n", view
, eModifyMode
, rec
);
1606 r
= ERROR_CALL_NOT_IMPLEMENTED
;
1610 r
= ERROR_INVALID_DATA
;
1616 static UINT
TABLE_delete( struct tagMSIVIEW
*view
)
1618 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1620 TRACE("%p\n", view
);
1628 return ERROR_SUCCESS
;
1631 static UINT
TABLE_find_matching_rows( struct tagMSIVIEW
*view
, UINT col
,
1632 UINT val
, UINT
*row
, MSIITERHANDLE
*handle
)
1634 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1635 const MSICOLUMNHASHENTRY
*entry
;
1637 TRACE("%p, %d, %u, %p\n", view
, col
, val
, *handle
);
1640 return ERROR_INVALID_PARAMETER
;
1642 if( (col
==0) || (col
> tv
->num_cols
) )
1643 return ERROR_INVALID_PARAMETER
;
1645 if( !tv
->columns
[col
-1].hash_table
)
1648 UINT num_rows
= tv
->table
->row_count
+ tv
->table
->nonpersistent_row_count
;
1649 MSICOLUMNHASHENTRY
**hash_table
;
1650 MSICOLUMNHASHENTRY
*new_entry
;
1652 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1654 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1655 ERR("%p %p\n", tv
, tv
->columns
);
1656 return ERROR_FUNCTION_FAILED
;
1659 /* allocate contiguous memory for the table and its entries so we
1660 * don't have to do an expensive cleanup */
1661 hash_table
= msi_alloc(MSITABLE_HASH_TABLE_SIZE
* sizeof(MSICOLUMNHASHENTRY
*) +
1662 num_rows
* sizeof(MSICOLUMNHASHENTRY
));
1664 return ERROR_OUTOFMEMORY
;
1666 memset(hash_table
, 0, MSITABLE_HASH_TABLE_SIZE
* sizeof(MSICOLUMNHASHENTRY
*));
1667 tv
->columns
[col
-1].hash_table
= hash_table
;
1669 new_entry
= (MSICOLUMNHASHENTRY
*)(hash_table
+ MSITABLE_HASH_TABLE_SIZE
);
1671 for (i
= 0; i
< num_rows
; i
++, new_entry
++)
1675 if (view
->ops
->fetch_int( view
, i
, col
, &row_value
) != ERROR_SUCCESS
)
1678 new_entry
->next
= NULL
;
1679 new_entry
->value
= row_value
;
1681 if (hash_table
[row_value
% MSITABLE_HASH_TABLE_SIZE
])
1683 MSICOLUMNHASHENTRY
*prev_entry
= hash_table
[row_value
% MSITABLE_HASH_TABLE_SIZE
];
1684 while (prev_entry
->next
)
1685 prev_entry
= prev_entry
->next
;
1686 prev_entry
->next
= new_entry
;
1689 hash_table
[row_value
% MSITABLE_HASH_TABLE_SIZE
] = new_entry
;
1694 entry
= tv
->columns
[col
-1].hash_table
[val
% MSITABLE_HASH_TABLE_SIZE
];
1696 entry
= (*handle
)->next
;
1698 while (entry
&& entry
->value
!= val
)
1699 entry
= entry
->next
;
1703 return ERROR_NO_MORE_ITEMS
;
1706 return ERROR_SUCCESS
;
1709 static UINT
TABLE_add_ref(struct tagMSIVIEW
*view
)
1711 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1714 TRACE("%p %d\n", view
, tv
->table
->ref_count
);
1716 for (i
= 0; i
< tv
->table
->col_count
; i
++)
1718 if (tv
->table
->colinfo
[i
].type
& MSITYPE_TEMPORARY
)
1719 InterlockedIncrement(&tv
->table
->colinfo
[i
].ref_count
);
1722 return InterlockedIncrement(&tv
->table
->ref_count
);
1725 static UINT
TABLE_remove_column(struct tagMSIVIEW
*view
, LPCWSTR table
, UINT number
)
1727 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1728 MSIRECORD
*rec
= NULL
;
1729 MSIVIEW
*columns
= NULL
;
1732 rec
= MSI_CreateRecord(2);
1734 return ERROR_OUTOFMEMORY
;
1736 MSI_RecordSetStringW(rec
, 1, table
);
1737 MSI_RecordSetInteger(rec
, 2, number
);
1739 r
= TABLE_CreateView(tv
->db
, szColumns
, &columns
);
1740 if (r
!= ERROR_SUCCESS
)
1743 r
= msi_table_find_row((MSITABLEVIEW
*)columns
, rec
, &row
);
1744 if (r
!= ERROR_SUCCESS
)
1747 r
= TABLE_delete_row(columns
, row
);
1748 if (r
!= ERROR_SUCCESS
)
1751 msi_update_table_columns(tv
->db
, table
);
1754 msiobj_release(&rec
->hdr
);
1755 if (columns
) columns
->ops
->delete(columns
);
1759 static UINT
TABLE_release(struct tagMSIVIEW
*view
)
1761 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1762 INT ref
= tv
->table
->ref_count
;
1766 TRACE("%p %d\n", view
, ref
);
1768 for (i
= 0; i
< tv
->table
->col_count
; i
++)
1770 if (tv
->table
->colinfo
[i
].type
& MSITYPE_TEMPORARY
)
1772 ref
= InterlockedDecrement(&tv
->table
->colinfo
[i
].ref_count
);
1775 r
= TABLE_remove_column(view
, tv
->table
->colinfo
[i
].tablename
,
1776 tv
->table
->colinfo
[i
].number
);
1777 if (r
!= ERROR_SUCCESS
)
1783 ref
= InterlockedDecrement(&tv
->table
->ref_count
);
1786 if (!tv
->table
->row_count
)
1788 list_remove(&tv
->table
->entry
);
1789 free_table(tv
->table
);
1797 static UINT
TABLE_add_column(struct tagMSIVIEW
*view
, LPCWSTR table
, UINT number
,
1798 LPCWSTR column
, UINT type
, BOOL hold
)
1800 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1805 rec
= MSI_CreateRecord(4);
1807 return ERROR_OUTOFMEMORY
;
1809 MSI_RecordSetStringW(rec
, 1, table
);
1810 MSI_RecordSetInteger(rec
, 2, number
);
1811 MSI_RecordSetStringW(rec
, 3, column
);
1812 MSI_RecordSetInteger(rec
, 4, type
);
1814 r
= TABLE_insert_row(&tv
->view
, rec
, FALSE
);
1815 if (r
!= ERROR_SUCCESS
)
1818 msi_update_table_columns(tv
->db
, table
);
1823 msitable
= find_cached_table(tv
->db
, table
);
1824 for (i
= 0; i
< msitable
->col_count
; i
++)
1826 if (!lstrcmpW(msitable
->colinfo
[i
].colname
, column
))
1828 InterlockedIncrement(&msitable
->colinfo
[i
].ref_count
);
1834 msiobj_release(&rec
->hdr
);
1838 static const MSIVIEWOPS table_ops
=
1848 TABLE_get_dimensions
,
1849 TABLE_get_column_info
,
1852 TABLE_find_matching_rows
,
1856 TABLE_remove_column
,
1859 UINT
TABLE_CreateView( MSIDATABASE
*db
, LPCWSTR name
, MSIVIEW
**view
)
1864 static const WCHAR Streams
[] = {'_','S','t','r','e','a','m','s',0};
1866 TRACE("%p %s %p\n", db
, debugstr_w(name
), view
);
1868 if ( !lstrcmpW( name
, Streams
) )
1869 return STREAMS_CreateView( db
, view
);
1871 sz
= sizeof *tv
+ lstrlenW(name
)*sizeof name
[0] ;
1872 tv
= msi_alloc_zero( sz
);
1874 return ERROR_FUNCTION_FAILED
;
1876 r
= get_table( db
, name
, &tv
->table
);
1877 if( r
!= ERROR_SUCCESS
)
1880 WARN("table not found\n");
1884 TRACE("table %p found with %d columns\n", tv
->table
, tv
->table
->col_count
);
1886 /* fill the structure */
1887 tv
->view
.ops
= &table_ops
;
1889 tv
->columns
= tv
->table
->colinfo
;
1890 tv
->num_cols
= tv
->table
->col_count
;
1891 tv
->row_size
= msi_table_get_row_size( tv
->table
->colinfo
, tv
->table
->col_count
);
1893 TRACE("%s one row is %d bytes\n", debugstr_w(name
), tv
->row_size
);
1895 *view
= (MSIVIEW
*) tv
;
1896 lstrcpyW( tv
->name
, name
);
1898 return ERROR_SUCCESS
;
1901 UINT
MSI_CommitTables( MSIDATABASE
*db
)
1904 MSITABLE
*table
= NULL
;
1908 r
= msi_save_string_table( db
->strings
, db
->storage
);
1909 if( r
!= ERROR_SUCCESS
)
1911 WARN("failed to save string table r=%08x\n",r
);
1915 LIST_FOR_EACH_ENTRY( table
, &db
->tables
, MSITABLE
, entry
)
1917 r
= save_table( db
, table
);
1918 if( r
!= ERROR_SUCCESS
)
1920 WARN("failed to save table %s (r=%08x)\n",
1921 debugstr_w(table
->name
), r
);
1926 /* force everything to reload next time */
1927 free_cached_tables( db
);
1929 return ERROR_SUCCESS
;
1932 MSICONDITION
MSI_DatabaseIsTablePersistent( MSIDATABASE
*db
, LPCWSTR table
)
1937 TRACE("%p %s\n", db
, debugstr_w(table
));
1940 return MSICONDITION_ERROR
;
1942 r
= get_table( db
, table
, &t
);
1943 if (r
!= ERROR_SUCCESS
)
1944 return MSICONDITION_NONE
;
1947 return MSICONDITION_TRUE
;
1949 return MSICONDITION_FALSE
;
1952 static UINT
read_raw_int(const BYTE
*data
, UINT col
, UINT bytes
)
1956 for (i
= 0; i
< bytes
; i
++)
1957 ret
+= (data
[col
+ i
] << i
* 8);
1962 static MSIRECORD
*msi_get_transform_record( const MSITABLEVIEW
*tv
, const string_table
*st
,
1963 const BYTE
*rawdata
, UINT bytes_per_strref
)
1965 UINT i
, val
, ofs
= 0;
1967 MSICOLUMNINFO
*columns
= tv
->columns
;
1970 mask
= rawdata
[0] | (rawdata
[1] << 8);
1973 rec
= MSI_CreateRecord( tv
->num_cols
);
1978 for( i
=0; i
<tv
->num_cols
; i
++ )
1980 if ( (mask
&1) && (i
>=(mask
>>8)) )
1982 /* all keys must be present */
1983 if ( (~mask
&1) && (~columns
[i
].type
& MSITYPE_KEY
) && ((1<<i
) & ~mask
) )
1986 if( (columns
[i
].type
& MSITYPE_STRING
) &&
1987 ! MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
1991 val
= read_raw_int(rawdata
, ofs
, bytes_per_strref
);
1992 sval
= msi_string_lookup_id( st
, val
);
1993 MSI_RecordSetStringW( rec
, i
+1, sval
);
1994 TRACE(" field %d [%s]\n", i
+1, debugstr_w(sval
));
1995 ofs
+= bytes_per_strref
;
1999 UINT n
= bytes_per_column( &columns
[i
] );
2003 val
= read_raw_int(rawdata
, ofs
, n
);
2005 MSI_RecordSetInteger( rec
, i
+1, val
^0x8000 );
2006 TRACE(" field %d [0x%04x]\n", i
+1, val
);
2009 val
= read_raw_int(rawdata
, ofs
, n
);
2011 MSI_RecordSetInteger( rec
, i
+1, val
^0x80000000 );
2012 TRACE(" field %d [0x%08x]\n", i
+1, val
);
2015 ERR("oops - unknown column width %d\n", n
);
2024 static void dump_record( MSIRECORD
*rec
)
2028 n
= MSI_RecordGetFieldCount( rec
);
2029 for( i
=1; i
<=n
; i
++ )
2031 LPCWSTR sval
= MSI_RecordGetString( rec
, i
);
2033 if( MSI_RecordIsNull( rec
, i
) )
2034 TRACE("row -> []\n");
2035 else if( (sval
= MSI_RecordGetString( rec
, i
)) )
2036 TRACE("row -> [%s]\n", debugstr_w(sval
));
2038 TRACE("row -> [0x%08x]\n", MSI_RecordGetInteger( rec
, i
) );
2042 static void dump_table( const string_table
*st
, const USHORT
*rawdata
, UINT rawsize
)
2047 for( i
=0; i
<(rawsize
/2); i
++ )
2049 sval
= msi_string_lookup_id( st
, rawdata
[i
] );
2050 MESSAGE(" %04x %s\n", rawdata
[i
], debugstr_w(sval
) );
2054 static UINT
* msi_record_to_row( const MSITABLEVIEW
*tv
, MSIRECORD
*rec
)
2059 data
= msi_alloc( tv
->num_cols
*sizeof (UINT
) );
2060 for( i
=0; i
<tv
->num_cols
; i
++ )
2064 if ( ~tv
->columns
[i
].type
& MSITYPE_KEY
)
2067 /* turn the transform column value into a row value */
2068 if ( ( tv
->columns
[i
].type
& MSITYPE_STRING
) &&
2069 ! MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
2071 str
= MSI_RecordGetString( rec
, i
+1 );
2072 r
= msi_string2idW( tv
->db
->strings
, str
, &data
[i
] );
2074 /* if there's no matching string in the string table,
2075 these keys can't match any record, so fail now. */
2076 if( ERROR_SUCCESS
!= r
)
2084 data
[i
] = MSI_RecordGetInteger( rec
, i
+1 );
2085 if ((tv
->columns
[i
].type
&0xff) == 2)
2088 data
[i
] += 0x80000000;
2094 static UINT
msi_row_matches( MSITABLEVIEW
*tv
, UINT row
, const UINT
*data
)
2096 UINT i
, r
, x
, ret
= ERROR_FUNCTION_FAILED
;
2098 for( i
=0; i
<tv
->num_cols
; i
++ )
2100 if ( ~tv
->columns
[i
].type
& MSITYPE_KEY
)
2103 /* turn the transform column value into a row value */
2104 r
= TABLE_fetch_int( &tv
->view
, row
, i
+1, &x
);
2105 if ( r
!= ERROR_SUCCESS
)
2107 ERR("TABLE_fetch_int shouldn't fail here\n");
2111 /* if this key matches, move to the next column */
2114 ret
= ERROR_FUNCTION_FAILED
;
2118 ret
= ERROR_SUCCESS
;
2124 static UINT
msi_table_find_row( MSITABLEVIEW
*tv
, MSIRECORD
*rec
, UINT
*row
)
2126 UINT i
, r
= ERROR_FUNCTION_FAILED
, *data
;
2128 data
= msi_record_to_row( tv
, rec
);
2131 for( i
= 0; i
< tv
->table
->row_count
+ tv
->table
->nonpersistent_row_count
; i
++ )
2133 r
= msi_row_matches( tv
, i
, data
);
2134 if( r
== ERROR_SUCCESS
)
2150 static UINT
msi_table_load_transform( MSIDATABASE
*db
, IStorage
*stg
,
2151 string_table
*st
, TRANSFORMDATA
*transform
,
2152 UINT bytes_per_strref
)
2155 BYTE
*rawdata
= NULL
;
2156 MSITABLEVIEW
*tv
= NULL
;
2157 UINT r
, n
, sz
, i
, mask
;
2158 MSIRECORD
*rec
= NULL
;
2164 return ERROR_SUCCESS
;
2166 name
= transform
->name
;
2169 TRACE("%p %p %p %s\n", db
, stg
, st
, debugstr_w(name
) );
2171 /* read the transform data */
2172 read_stream_data( stg
, name
, TRUE
, &rawdata
, &rawsize
);
2175 TRACE("table %s empty\n", debugstr_w(name
) );
2176 return ERROR_INVALID_TABLE
;
2179 /* create a table view */
2180 r
= TABLE_CreateView( db
, name
, (MSIVIEW
**) &tv
);
2181 if( r
!= ERROR_SUCCESS
)
2184 r
= tv
->view
.ops
->execute( &tv
->view
, NULL
);
2185 if( r
!= ERROR_SUCCESS
)
2188 TRACE("name = %s columns = %u row_size = %u raw size = %u\n",
2189 debugstr_w(name
), tv
->num_cols
, tv
->row_size
, rawsize
);
2191 /* interpret the data */
2193 for( n
=0; n
< rawsize
; )
2195 mask
= rawdata
[n
] | (rawdata
[n
+1] << 8);
2200 * if the low bit is set, columns are continuous and
2201 * the number of columns is specified in the high byte
2204 for( i
=0; i
<tv
->num_cols
; i
++ )
2206 if( (tv
->columns
[i
].type
& MSITYPE_STRING
) &&
2207 ! MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
2208 sz
+= bytes_per_strref
;
2210 sz
+= bytes_per_column( &tv
->columns
[i
] );
2216 * If the low bit is not set, mask is a bitmask.
2217 * Excepting for key fields, which are always present,
2218 * each bit indicates that a field is present in the transform record.
2220 * mask == 0 is a special case ... only the keys will be present
2221 * and it means that this row should be deleted.
2224 for( i
=0; i
<tv
->num_cols
; i
++ )
2226 if( (tv
->columns
[i
].type
& MSITYPE_KEY
) || ((1<<i
)&mask
))
2228 if( (tv
->columns
[i
].type
& MSITYPE_STRING
) &&
2229 ! MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
2230 sz
+= bytes_per_strref
;
2232 sz
+= bytes_per_column( &tv
->columns
[i
] );
2237 /* check we didn't run of the end of the table */
2238 if ( (n
+sz
) > rawsize
)
2241 dump_table( st
, (USHORT
*)rawdata
, rawsize
);
2245 rec
= msi_get_transform_record( tv
, st
, &rawdata
[n
], bytes_per_strref
);
2252 UINT number
= MSI_NULL_INTEGER
;
2254 TRACE("inserting record\n");
2256 if (!lstrcmpW(name
, szColumns
))
2258 MSI_RecordGetStringW( rec
, 1, table
, &sz
);
2259 number
= MSI_RecordGetInteger( rec
, 2 );
2262 * Native msi seems writes nul into the Number (2nd) column of
2263 * the _Columns table, only when the columns are from a new table
2265 if ( number
== MSI_NULL_INTEGER
)
2267 /* reset the column number on a new table */
2268 if ( lstrcmpW(coltable
, table
) )
2271 lstrcpyW( coltable
, table
);
2274 /* fix nul column numbers */
2275 MSI_RecordSetInteger( rec
, 2, ++colcol
);
2279 r
= TABLE_insert_row( &tv
->view
, rec
, FALSE
);
2280 if (r
!= ERROR_SUCCESS
)
2281 ERR("insert row failed\n");
2283 if ( number
!= MSI_NULL_INTEGER
&& !lstrcmpW(name
, szColumns
) )
2284 msi_update_table_columns( db
, table
);
2290 r
= msi_table_find_row( tv
, rec
, &row
);
2291 if (r
!= ERROR_SUCCESS
)
2292 ERR("no matching row to transform\n");
2295 TRACE("modifying row [%d]:\n", row
);
2296 TABLE_set_row( &tv
->view
, row
, rec
, mask
);
2300 TRACE("deleting row [%d]:\n", row
);
2301 TABLE_delete_row( &tv
->view
, row
);
2304 if( TRACE_ON(msidb
) ) dump_record( rec
);
2305 msiobj_release( &rec
->hdr
);
2312 /* no need to free the table, it's associated with the database */
2313 msi_free( rawdata
);
2315 tv
->view
.ops
->delete( &tv
->view
);
2317 return ERROR_SUCCESS
;
2321 * msi_table_apply_transform
2323 * Enumerate the table transforms in a transform storage and apply each one.
2325 UINT
msi_table_apply_transform( MSIDATABASE
*db
, IStorage
*stg
)
2327 struct list transforms
;
2328 IEnumSTATSTG
*stgenum
= NULL
;
2329 TRANSFORMDATA
*transform
;
2330 TRANSFORMDATA
*tables
= NULL
, *columns
= NULL
;
2333 string_table
*strings
;
2334 UINT ret
= ERROR_FUNCTION_FAILED
;
2335 UINT bytes_per_strref
;
2337 TRACE("%p %p\n", db
, stg
);
2339 strings
= msi_load_string_table( stg
, &bytes_per_strref
);
2343 r
= IStorage_EnumElements( stg
, 0, NULL
, 0, &stgenum
);
2347 list_init(&transforms
);
2351 MSITABLEVIEW
*tv
= NULL
;
2355 r
= IEnumSTATSTG_Next( stgenum
, 1, &stat
, &count
);
2356 if ( FAILED( r
) || !count
)
2359 decode_streamname( stat
.pwcsName
, name
);
2360 if ( name
[0] != 0x4840 )
2363 if ( !lstrcmpW( name
+1, szStringPool
) ||
2364 !lstrcmpW( name
+1, szStringData
) )
2367 transform
= msi_alloc_zero( sizeof(TRANSFORMDATA
) );
2371 list_add_tail( &transforms
, &transform
->entry
);
2373 transform
->name
= strdupW( name
+ 1 );
2375 if ( !lstrcmpW( transform
->name
, szTables
) )
2377 else if (!lstrcmpW( transform
->name
, szColumns
) )
2378 columns
= transform
;
2380 TRACE("transform contains stream %s\n", debugstr_w(name
));
2382 /* load the table */
2383 r
= TABLE_CreateView( db
, transform
->name
, (MSIVIEW
**) &tv
);
2384 if( r
!= ERROR_SUCCESS
)
2387 r
= tv
->view
.ops
->execute( &tv
->view
, NULL
);
2388 if( r
!= ERROR_SUCCESS
)
2390 tv
->view
.ops
->delete( &tv
->view
);
2394 tv
->view
.ops
->delete( &tv
->view
);
2398 * Apply _Tables and _Columns transforms first so that
2399 * the table metadata is correct, and empty tables exist.
2401 ret
= msi_table_load_transform( db
, stg
, strings
, tables
, bytes_per_strref
);
2402 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_INVALID_TABLE
)
2405 ret
= msi_table_load_transform( db
, stg
, strings
, columns
, bytes_per_strref
);
2406 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_INVALID_TABLE
)
2409 ret
= ERROR_SUCCESS
;
2411 while ( !list_empty( &transforms
) )
2413 transform
= LIST_ENTRY( list_head( &transforms
), TRANSFORMDATA
, entry
);
2415 if ( lstrcmpW( transform
->name
, szColumns
) &&
2416 lstrcmpW( transform
->name
, szTables
) &&
2417 ret
== ERROR_SUCCESS
)
2419 ret
= msi_table_load_transform( db
, stg
, strings
, transform
, bytes_per_strref
);
2422 list_remove( &transform
->entry
);
2423 msi_free( transform
->name
);
2424 msi_free( transform
);
2427 if ( ret
== ERROR_SUCCESS
)
2428 append_storage_to_db( db
, stg
);
2432 IEnumSTATSTG_Release( stgenum
);
2434 msi_destroy_stringtable( strings
);
2439 void append_storage_to_db( MSIDATABASE
*db
, IStorage
*stg
)
2443 t
= msi_alloc( sizeof *t
);
2445 IStorage_AddRef( stg
);
2446 list_add_tail( &db
->transforms
, &t
->entry
);
2449 void msi_free_transforms( MSIDATABASE
*db
)
2451 while( !list_empty( &db
->transforms
) )
2453 MSITRANSFORM
*t
= LIST_ENTRY( list_head( &db
->transforms
),
2454 MSITRANSFORM
, entry
);
2455 list_remove( &t
->entry
);
2456 IStorage_Release( t
->stg
);