2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2002-2005 Mike McCormack for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
39 #include "wine/debug.h"
40 #include "wine/unicode.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(msidb
);
44 #define MSITABLE_HASH_TABLE_SIZE 37
46 typedef struct tagMSICOLUMNHASHENTRY
48 struct tagMSICOLUMNHASHENTRY
*next
;
53 typedef struct tagMSICOLUMNINFO
62 MSICOLUMNHASHENTRY
**hash_table
;
68 BOOL
*data_persistent
;
71 MSICOLUMNINFO
*colinfo
;
73 MSICONDITION persistent
;
78 /* information for default tables */
79 static const WCHAR szTables
[] = {'_','T','a','b','l','e','s',0};
80 static const WCHAR szTable
[] = {'T','a','b','l','e',0};
81 static const WCHAR szColumns
[] = {'_','C','o','l','u','m','n','s',0};
82 static const WCHAR szNumber
[] = {'N','u','m','b','e','r',0};
83 static const WCHAR szType
[] = {'T','y','p','e',0};
85 static const MSICOLUMNINFO _Columns_cols
[4] = {
86 { szColumns
, 1, szTable
, MSITYPE_VALID
| MSITYPE_STRING
| MSITYPE_KEY
| 64, 0, 0, 0, NULL
},
87 { szColumns
, 2, szNumber
, MSITYPE_VALID
| MSITYPE_KEY
| 2, 2, 0, 0, NULL
},
88 { szColumns
, 3, szName
, MSITYPE_VALID
| MSITYPE_STRING
| 64, 4, 0, 0, NULL
},
89 { szColumns
, 4, szType
, MSITYPE_VALID
| 2, 6, 0, 0, NULL
},
92 static const MSICOLUMNINFO _Tables_cols
[1] = {
93 { szTables
, 1, szName
, MSITYPE_VALID
| MSITYPE_STRING
| MSITYPE_KEY
| 64, 0, 0, 0, NULL
},
96 #define MAX_STREAM_NAME 0x1f
98 static inline UINT
bytes_per_column( MSIDATABASE
*db
, const MSICOLUMNINFO
*col
, UINT bytes_per_strref
)
100 if( MSITYPE_IS_BINARY(col
->type
) )
103 if( col
->type
& MSITYPE_STRING
)
104 return bytes_per_strref
;
106 if( (col
->type
& 0xff) <= 2)
109 if( (col
->type
& 0xff) != 4 )
110 ERR("Invalid column size!\n");
115 static int utf2mime(int x
)
117 if( (x
>='0') && (x
<='9') )
119 if( (x
>='A') && (x
<='Z') )
121 if( (x
>='a') && (x
<='z') )
130 LPWSTR
encode_streamname(BOOL bTable
, LPCWSTR in
)
132 DWORD count
= MAX_STREAM_NAME
;
137 count
= lstrlenW( in
)+2;
138 if (!(out
= msi_alloc( count
*sizeof(WCHAR
) ))) return NULL
;
154 if( ( ch
< 0x80 ) && ( utf2mime(ch
) >= 0 ) )
156 ch
= utf2mime(ch
) + 0x4800;
158 if( next
&& (next
<0x80) )
160 next
= utf2mime(next
);
171 ERR("Failed to encode stream name (%s)\n",debugstr_w(in
));
176 static int mime2utf(int x
)
183 return x
- 10 - 26 + 'a';
184 if( x
== (10+26+26) )
189 BOOL
decode_streamname(LPCWSTR in
, LPWSTR out
)
194 while ( (ch
= *in
++) )
196 if( (ch
>= 0x3800 ) && (ch
< 0x4840 ) )
199 ch
= mime2utf(ch
-0x4800);
203 *out
++ = mime2utf(ch
&0x3f);
205 ch
= mime2utf((ch
>>6)&0x3f);
215 void enum_stream_names( IStorage
*stg
)
217 IEnumSTATSTG
*stgenum
= NULL
;
223 r
= IStorage_EnumElements( stg
, 0, NULL
, 0, &stgenum
);
231 r
= IEnumSTATSTG_Next( stgenum
, 1, &stat
, &count
);
232 if( FAILED( r
) || !count
)
234 decode_streamname( stat
.pwcsName
, name
);
235 TRACE("stream %2d -> %s %s\n", n
,
236 debugstr_w(stat
.pwcsName
), debugstr_w(name
) );
237 CoTaskMemFree( stat
.pwcsName
);
241 IEnumSTATSTG_Release( stgenum
);
244 UINT
read_stream_data( IStorage
*stg
, LPCWSTR stname
, BOOL table
,
245 BYTE
**pdata
, UINT
*psz
)
248 UINT ret
= ERROR_FUNCTION_FAILED
;
255 encname
= encode_streamname(table
, stname
);
257 TRACE("%s -> %s\n",debugstr_w(stname
),debugstr_w(encname
));
259 r
= IStorage_OpenStream(stg
, encname
, NULL
,
260 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, &stm
);
264 WARN("open stream failed r = %08x - empty table?\n", r
);
268 r
= IStream_Stat(stm
, &stat
, STATFLAG_NONAME
);
271 WARN("open stream failed r = %08x!\n", r
);
275 if( stat
.cbSize
.QuadPart
>> 32 )
281 sz
= stat
.cbSize
.QuadPart
;
282 data
= msi_alloc( sz
);
285 WARN("couldn't allocate memory r=%08x!\n", r
);
286 ret
= ERROR_NOT_ENOUGH_MEMORY
;
290 r
= IStream_Read(stm
, data
, sz
, &count
);
291 if( FAILED( r
) || ( count
!= sz
) )
294 WARN("read stream failed r = %08x!\n", r
);
303 IStream_Release( stm
);
308 UINT
write_stream_data( IStorage
*stg
, LPCWSTR stname
,
309 LPCVOID data
, UINT sz
, BOOL bTable
)
312 UINT ret
= ERROR_FUNCTION_FAILED
;
319 encname
= encode_streamname(bTable
, stname
);
320 r
= IStorage_OpenStream( stg
, encname
, NULL
,
321 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, &stm
);
324 r
= IStorage_CreateStream( stg
, encname
,
325 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, 0, &stm
);
330 WARN("open stream failed r = %08x\n", r
);
335 r
= IStream_SetSize( stm
, size
);
338 WARN("Failed to SetSize\n");
343 r
= IStream_Seek( stm
, pos
, STREAM_SEEK_SET
, NULL
);
346 WARN("Failed to Seek\n");
352 r
= IStream_Write(stm
, data
, sz
, &count
);
353 if( FAILED( r
) || ( count
!= sz
) )
355 WARN("Failed to Write\n");
363 IStream_Release( stm
);
368 static void msi_free_colinfo( MSICOLUMNINFO
*colinfo
, UINT count
)
371 for (i
= 0; i
< count
; i
++) msi_free( colinfo
[i
].hash_table
);
374 static void free_table( MSITABLE
*table
)
377 for( i
=0; i
<table
->row_count
; i
++ )
378 msi_free( table
->data
[i
] );
379 msi_free( table
->data
);
380 msi_free( table
->data_persistent
);
381 msi_free_colinfo( table
->colinfo
, table
->col_count
);
382 msi_free( table
->colinfo
);
386 static UINT
msi_table_get_row_size( MSIDATABASE
*db
, const MSICOLUMNINFO
*cols
, UINT count
, UINT bytes_per_strref
)
388 const MSICOLUMNINFO
*last_col
;
393 if (bytes_per_strref
!= LONG_STR_BYTES
)
396 for (i
= 0; i
< count
; i
++) size
+= bytes_per_column( db
, &cols
[i
], bytes_per_strref
);
399 last_col
= &cols
[count
- 1];
400 return last_col
->offset
+ bytes_per_column( db
, last_col
, bytes_per_strref
);
403 /* add this table to the list of cached tables in the database */
404 static UINT
read_table_from_storage( MSIDATABASE
*db
, MSITABLE
*t
, IStorage
*stg
)
406 BYTE
*rawdata
= NULL
;
407 UINT rawsize
= 0, i
, j
, row_size
, row_size_mem
;
409 TRACE("%s\n",debugstr_w(t
->name
));
411 row_size
= msi_table_get_row_size( db
, t
->colinfo
, t
->col_count
, db
->bytes_per_strref
);
412 row_size_mem
= msi_table_get_row_size( db
, t
->colinfo
, t
->col_count
, LONG_STR_BYTES
);
414 /* if we can't read the table, just assume that it's empty */
415 read_stream_data( stg
, t
->name
, TRUE
, &rawdata
, &rawsize
);
417 return ERROR_SUCCESS
;
419 TRACE("Read %d bytes\n", rawsize
);
421 if( rawsize
% row_size
)
423 WARN("Table size is invalid %d/%d\n", rawsize
, row_size
);
427 t
->row_count
= rawsize
/ row_size
;
428 t
->data
= msi_alloc_zero( t
->row_count
* sizeof (USHORT
*) );
431 t
->data_persistent
= msi_alloc_zero( t
->row_count
* sizeof(BOOL
));
432 if ( !t
->data_persistent
)
435 /* transpose all the data */
436 TRACE("Transposing data from %d rows\n", t
->row_count
);
437 for (i
= 0; i
< t
->row_count
; i
++)
439 UINT ofs
= 0, ofs_mem
= 0;
441 t
->data
[i
] = msi_alloc( row_size_mem
);
444 t
->data_persistent
[i
] = TRUE
;
446 for (j
= 0; j
< t
->col_count
; j
++)
448 UINT m
= bytes_per_column( db
, &t
->colinfo
[j
], LONG_STR_BYTES
);
449 UINT n
= bytes_per_column( db
, &t
->colinfo
[j
], db
->bytes_per_strref
);
452 if ( n
!= 2 && n
!= 3 && n
!= 4 )
454 ERR("oops - unknown column width %d\n", n
);
457 if (t
->colinfo
[j
].type
& MSITYPE_STRING
&& n
< m
)
459 for (k
= 0; k
< m
; k
++)
462 t
->data
[i
][ofs_mem
+ k
] = rawdata
[ofs
* t
->row_count
+ i
* n
+ k
];
464 t
->data
[i
][ofs_mem
+ k
] = 0;
469 for (k
= 0; k
< n
; k
++)
470 t
->data
[i
][ofs_mem
+ k
] = rawdata
[ofs
* t
->row_count
+ i
* n
+ k
];
478 return ERROR_SUCCESS
;
481 return ERROR_FUNCTION_FAILED
;
484 void free_cached_tables( MSIDATABASE
*db
)
486 while( !list_empty( &db
->tables
) )
488 MSITABLE
*t
= LIST_ENTRY( list_head( &db
->tables
), MSITABLE
, entry
);
490 list_remove( &t
->entry
);
495 static MSITABLE
*find_cached_table( MSIDATABASE
*db
, LPCWSTR name
)
499 LIST_FOR_EACH_ENTRY( t
, &db
->tables
, MSITABLE
, entry
)
500 if( !strcmpW( name
, t
->name
) )
506 static void table_calc_column_offsets( MSIDATABASE
*db
, MSICOLUMNINFO
*colinfo
, DWORD count
)
510 for (i
= 0; colinfo
&& i
< count
; i
++)
512 assert( i
+ 1 == colinfo
[i
].number
);
513 if (i
) colinfo
[i
].offset
= colinfo
[i
- 1].offset
+
514 bytes_per_column( db
, &colinfo
[i
- 1], LONG_STR_BYTES
);
515 else colinfo
[i
].offset
= 0;
517 TRACE("column %d is [%s] with type %08x ofs %d\n",
518 colinfo
[i
].number
, debugstr_w(colinfo
[i
].colname
),
519 colinfo
[i
].type
, colinfo
[i
].offset
);
523 static UINT
get_defaulttablecolumns( MSIDATABASE
*db
, LPCWSTR name
, MSICOLUMNINFO
*colinfo
, UINT
*sz
)
525 const MSICOLUMNINFO
*p
;
528 TRACE("%s\n", debugstr_w(name
));
530 if (!strcmpW( name
, szTables
))
535 else if (!strcmpW( name
, szColumns
))
540 else return ERROR_FUNCTION_FAILED
;
542 for (i
= 0; i
< n
; i
++)
544 if (colinfo
&& i
< *sz
) colinfo
[i
] = p
[i
];
545 if (colinfo
&& i
>= *sz
) break;
547 table_calc_column_offsets( db
, colinfo
, n
);
549 return ERROR_SUCCESS
;
552 static UINT
get_tablecolumns( MSIDATABASE
*db
, LPCWSTR szTableName
, MSICOLUMNINFO
*colinfo
, UINT
*sz
);
554 static UINT
table_get_column_info( MSIDATABASE
*db
, LPCWSTR name
, MSICOLUMNINFO
**pcols
, UINT
*pcount
)
556 UINT r
, column_count
= 0;
557 MSICOLUMNINFO
*columns
;
559 /* get the number of columns in this table */
561 r
= get_tablecolumns( db
, name
, NULL
, &column_count
);
562 if (r
!= ERROR_SUCCESS
)
565 *pcount
= column_count
;
567 /* if there are no columns, there's no table */
569 return ERROR_INVALID_PARAMETER
;
571 TRACE("table %s found\n", debugstr_w(name
));
573 columns
= msi_alloc( column_count
* sizeof(MSICOLUMNINFO
) );
575 return ERROR_FUNCTION_FAILED
;
577 r
= get_tablecolumns( db
, name
, columns
, &column_count
);
578 if (r
!= ERROR_SUCCESS
)
581 return ERROR_FUNCTION_FAILED
;
587 static UINT
get_table( MSIDATABASE
*db
, LPCWSTR name
, MSITABLE
**table_ret
)
592 /* first, see if the table is cached */
593 table
= find_cached_table( db
, name
);
597 return ERROR_SUCCESS
;
600 /* nonexistent tables should be interpreted as empty tables */
601 table
= msi_alloc( sizeof(MSITABLE
) + lstrlenW( name
) * sizeof(WCHAR
) );
603 return ERROR_FUNCTION_FAILED
;
605 table
->row_count
= 0;
607 table
->data_persistent
= NULL
;
608 table
->colinfo
= NULL
;
609 table
->col_count
= 0;
610 table
->persistent
= MSICONDITION_TRUE
;
611 lstrcpyW( table
->name
, name
);
613 if (!strcmpW( name
, szTables
) || !strcmpW( name
, szColumns
))
614 table
->persistent
= MSICONDITION_NONE
;
616 r
= table_get_column_info( db
, name
, &table
->colinfo
, &table
->col_count
);
617 if (r
!= ERROR_SUCCESS
)
622 r
= read_table_from_storage( db
, table
, db
->storage
);
623 if (r
!= ERROR_SUCCESS
)
628 list_add_head( &db
->tables
, &table
->entry
);
630 return ERROR_SUCCESS
;
633 static UINT
read_table_int( BYTE
*const *data
, UINT row
, UINT col
, UINT bytes
)
637 for (i
= 0; i
< bytes
; i
++)
638 ret
+= data
[row
][col
+ i
] << i
* 8;
643 static UINT
get_tablecolumns( MSIDATABASE
*db
, LPCWSTR szTableName
, MSICOLUMNINFO
*colinfo
, UINT
*sz
)
645 UINT r
, i
, n
= 0, table_id
, count
, maxcount
= *sz
;
646 MSITABLE
*table
= NULL
;
648 TRACE("%s\n", debugstr_w(szTableName
));
650 /* first check if there is a default table with that name */
651 r
= get_defaulttablecolumns( db
, szTableName
, colinfo
, sz
);
652 if (r
== ERROR_SUCCESS
&& *sz
)
655 r
= get_table( db
, szColumns
, &table
);
656 if (r
!= ERROR_SUCCESS
)
658 ERR("couldn't load _Columns table\n");
659 return ERROR_FUNCTION_FAILED
;
662 /* convert table and column names to IDs from the string table */
663 r
= msi_string2id( db
->strings
, szTableName
, -1, &table_id
);
664 if (r
!= ERROR_SUCCESS
)
666 WARN("Couldn't find id for %s\n", debugstr_w(szTableName
));
669 TRACE("Table id is %d, row count is %d\n", table_id
, table
->row_count
);
671 /* Note: _Columns table doesn't have non-persistent data */
673 /* if maxcount is non-zero, assume it's exactly right for this table */
674 memset( colinfo
, 0, maxcount
* sizeof(*colinfo
) );
675 count
= table
->row_count
;
676 for (i
= 0; i
< count
; i
++)
678 if (read_table_int( table
->data
, i
, 0, LONG_STR_BYTES
) != table_id
) continue;
681 UINT id
= read_table_int( table
->data
, i
, table
->colinfo
[2].offset
, LONG_STR_BYTES
);
682 UINT col
= read_table_int( table
->data
, i
, table
->colinfo
[1].offset
, sizeof(USHORT
) ) - (1 << 15);
684 /* check the column number is in range */
685 if (col
< 1 || col
> maxcount
)
687 ERR("column %d out of range\n", col
);
690 /* check if this column was already set */
691 if (colinfo
[col
- 1].number
)
693 ERR("duplicate column %d\n", col
);
696 colinfo
[col
- 1].tablename
= msi_string_lookup( db
->strings
, table_id
, NULL
);
697 colinfo
[col
- 1].number
= col
;
698 colinfo
[col
- 1].colname
= msi_string_lookup( db
->strings
, id
, NULL
);
699 colinfo
[col
- 1].type
= read_table_int( table
->data
, i
, table
->colinfo
[3].offset
,
700 sizeof(USHORT
) ) - (1 << 15);
701 colinfo
[col
- 1].offset
= 0;
702 colinfo
[col
- 1].ref_count
= 0;
703 colinfo
[col
- 1].hash_table
= NULL
;
707 TRACE("%s has %d columns\n", debugstr_w(szTableName
), n
);
709 if (colinfo
&& n
!= maxcount
)
711 ERR("missing column in table %s\n", debugstr_w(szTableName
));
712 msi_free_colinfo( colinfo
, maxcount
);
713 return ERROR_FUNCTION_FAILED
;
715 table_calc_column_offsets( db
, colinfo
, n
);
717 return ERROR_SUCCESS
;
720 UINT
msi_create_table( MSIDATABASE
*db
, LPCWSTR name
, column_info
*col_info
,
721 MSICONDITION persistent
)
723 enum StringPersistence string_persistence
= (persistent
) ? StringPersistent
: StringNonPersistent
;
726 MSIRECORD
*rec
= NULL
;
731 /* only add tables that don't exist already */
732 if( TABLE_Exists(db
, name
) )
734 WARN("table %s exists\n", debugstr_w(name
));
735 return ERROR_BAD_QUERY_SYNTAX
;
738 table
= msi_alloc( sizeof (MSITABLE
) + lstrlenW(name
)*sizeof (WCHAR
) );
740 return ERROR_FUNCTION_FAILED
;
742 table
->ref_count
= 1;
743 table
->row_count
= 0;
745 table
->data_persistent
= NULL
;
746 table
->colinfo
= NULL
;
747 table
->col_count
= 0;
748 table
->persistent
= persistent
;
749 lstrcpyW( table
->name
, name
);
751 for( col
= col_info
; col
; col
= col
->next
)
754 table
->colinfo
= msi_alloc( table
->col_count
* sizeof(MSICOLUMNINFO
) );
758 return ERROR_FUNCTION_FAILED
;
761 for( i
= 0, col
= col_info
; col
; i
++, col
= col
->next
)
763 UINT table_id
= msi_addstringW( db
->strings
, col
->table
, -1, 1, string_persistence
);
764 UINT col_id
= msi_addstringW( db
->strings
, col
->column
, -1, 1, string_persistence
);
766 table
->colinfo
[ i
].tablename
= msi_string_lookup( db
->strings
, table_id
, NULL
);
767 table
->colinfo
[ i
].number
= i
+ 1;
768 table
->colinfo
[ i
].colname
= msi_string_lookup( db
->strings
, col_id
, NULL
);
769 table
->colinfo
[ i
].type
= col
->type
;
770 table
->colinfo
[ i
].offset
= 0;
771 table
->colinfo
[ i
].ref_count
= 0;
772 table
->colinfo
[ i
].hash_table
= NULL
;
773 table
->colinfo
[ i
].temporary
= col
->temporary
;
775 table_calc_column_offsets( db
, table
->colinfo
, table
->col_count
);
777 r
= TABLE_CreateView( db
, szTables
, &tv
);
778 TRACE("CreateView returned %x\n", r
);
785 r
= tv
->ops
->execute( tv
, 0 );
786 TRACE("tv execute returned %x\n", r
);
790 rec
= MSI_CreateRecord( 1 );
794 r
= MSI_RecordSetStringW( rec
, 1, name
);
798 r
= tv
->ops
->insert_row( tv
, rec
, -1, persistent
== MSICONDITION_FALSE
);
799 TRACE("insert_row returned %x\n", r
);
803 tv
->ops
->delete( tv
);
806 msiobj_release( &rec
->hdr
);
809 if( persistent
!= MSICONDITION_FALSE
)
811 /* add each column to the _Columns table */
812 r
= TABLE_CreateView( db
, szColumns
, &tv
);
816 r
= tv
->ops
->execute( tv
, 0 );
817 TRACE("tv execute returned %x\n", r
);
821 rec
= MSI_CreateRecord( 4 );
825 r
= MSI_RecordSetStringW( rec
, 1, name
);
830 * need to set the table, column number, col name and type
831 * for each column we enter in the table
834 for( col
= col_info
; col
; col
= col
->next
)
836 r
= MSI_RecordSetInteger( rec
, 2, nField
);
840 r
= MSI_RecordSetStringW( rec
, 3, col
->column
);
844 r
= MSI_RecordSetInteger( rec
, 4, col
->type
);
848 r
= tv
->ops
->insert_row( tv
, rec
, -1, FALSE
);
860 msiobj_release( &rec
->hdr
);
861 /* FIXME: remove values from the string table on error */
863 tv
->ops
->delete( tv
);
865 if (r
== ERROR_SUCCESS
)
866 list_add_head( &db
->tables
, &table
->entry
);
873 static UINT
save_table( MSIDATABASE
*db
, const MSITABLE
*t
, UINT bytes_per_strref
)
875 BYTE
*rawdata
= NULL
;
876 UINT rawsize
, i
, j
, row_size
, row_count
;
877 UINT r
= ERROR_FUNCTION_FAILED
;
879 /* Nothing to do for non-persistent tables */
880 if( t
->persistent
== MSICONDITION_FALSE
)
881 return ERROR_SUCCESS
;
883 TRACE("Saving %s\n", debugstr_w( t
->name
) );
885 row_size
= msi_table_get_row_size( db
, t
->colinfo
, t
->col_count
, bytes_per_strref
);
886 row_count
= t
->row_count
;
887 for (i
= 0; i
< t
->row_count
; i
++)
889 if (!t
->data_persistent
[i
])
891 row_count
= 1; /* yes, this is bizarre */
895 rawsize
= row_count
* row_size
;
896 rawdata
= msi_alloc_zero( rawsize
);
899 r
= ERROR_NOT_ENOUGH_MEMORY
;
904 for (i
= 0; i
< t
->row_count
; i
++)
906 UINT ofs
= 0, ofs_mem
= 0;
908 if (!t
->data_persistent
[i
]) break;
910 for (j
= 0; j
< t
->col_count
; j
++)
912 UINT m
= bytes_per_column( db
, &t
->colinfo
[j
], LONG_STR_BYTES
);
913 UINT n
= bytes_per_column( db
, &t
->colinfo
[j
], bytes_per_strref
);
916 if (n
!= 2 && n
!= 3 && n
!= 4)
918 ERR("oops - unknown column width %d\n", n
);
921 if (t
->colinfo
[j
].type
& MSITYPE_STRING
&& n
< m
)
923 UINT id
= read_table_int( t
->data
, i
, ofs_mem
, LONG_STR_BYTES
);
924 if (id
> 1 << bytes_per_strref
* 8)
926 ERR("string id %u out of range\n", id
);
930 for (k
= 0; k
< n
; k
++)
932 rawdata
[ofs
* row_count
+ i
* n
+ k
] = t
->data
[i
][ofs_mem
+ k
];
940 TRACE("writing %d bytes\n", rawsize
);
941 r
= write_stream_data( db
->storage
, t
->name
, rawdata
, rawsize
, TRUE
);
948 static void msi_update_table_columns( MSIDATABASE
*db
, LPCWSTR name
)
951 UINT size
, offset
, old_count
;
954 table
= find_cached_table( db
, name
);
955 old_count
= table
->col_count
;
956 msi_free_colinfo( table
->colinfo
, table
->col_count
);
957 msi_free( table
->colinfo
);
958 table
->colinfo
= NULL
;
960 table_get_column_info( db
, name
, &table
->colinfo
, &table
->col_count
);
961 if (!table
->col_count
) return;
963 size
= msi_table_get_row_size( db
, table
->colinfo
, table
->col_count
, LONG_STR_BYTES
);
964 offset
= table
->colinfo
[table
->col_count
- 1].offset
;
966 for ( n
= 0; n
< table
->row_count
; n
++ )
968 table
->data
[n
] = msi_realloc( table
->data
[n
], size
);
969 if (old_count
< table
->col_count
)
970 memset( &table
->data
[n
][offset
], 0, size
- offset
);
974 /* try to find the table name in the _Tables table */
975 BOOL
TABLE_Exists( MSIDATABASE
*db
, LPCWSTR name
)
980 if( !strcmpW( name
, szTables
) || !strcmpW( name
, szColumns
) ||
981 !strcmpW( name
, szStreams
) || !strcmpW( name
, szStorages
) )
984 r
= msi_string2id( db
->strings
, name
, -1, &table_id
);
985 if( r
!= ERROR_SUCCESS
)
987 TRACE("Couldn't find id for %s\n", debugstr_w(name
));
991 r
= get_table( db
, szTables
, &table
);
992 if( r
!= ERROR_SUCCESS
)
994 ERR("table %s not available\n", debugstr_w(szTables
));
998 for( i
= 0; i
< table
->row_count
; i
++ )
1000 if( read_table_int( table
->data
, i
, 0, LONG_STR_BYTES
) == table_id
)
1007 /* below is the query interface to a table */
1009 typedef struct tagMSITABLEVIEW
1014 MSICOLUMNINFO
*columns
;
1020 static UINT
TABLE_fetch_int( struct tagMSIVIEW
*view
, UINT row
, UINT col
, UINT
*val
)
1022 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1026 return ERROR_INVALID_PARAMETER
;
1028 if( (col
==0) || (col
>tv
->num_cols
) )
1029 return ERROR_INVALID_PARAMETER
;
1031 /* how many rows are there ? */
1032 if( row
>= tv
->table
->row_count
)
1033 return ERROR_NO_MORE_ITEMS
;
1035 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1037 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1038 ERR("%p %p\n", tv
, tv
->columns
);
1039 return ERROR_FUNCTION_FAILED
;
1042 n
= bytes_per_column( tv
->db
, &tv
->columns
[col
- 1], LONG_STR_BYTES
);
1043 if (n
!= 2 && n
!= 3 && n
!= 4)
1045 ERR("oops! what is %d bytes per column?\n", n
);
1046 return ERROR_FUNCTION_FAILED
;
1049 offset
= tv
->columns
[col
-1].offset
;
1050 *val
= read_table_int(tv
->table
->data
, row
, offset
, n
);
1052 /* TRACE("Data [%d][%d] = %d\n", row, col, *val ); */
1054 return ERROR_SUCCESS
;
1057 static UINT
msi_stream_name( const MSITABLEVIEW
*tv
, UINT row
, LPWSTR
*pstname
)
1059 LPWSTR p
, stname
= NULL
;
1060 UINT i
, r
, type
, ival
;
1063 MSIVIEW
*view
= (MSIVIEW
*) tv
;
1065 TRACE("%p %d\n", tv
, row
);
1067 len
= lstrlenW( tv
->name
) + 1;
1068 stname
= msi_alloc( len
*sizeof(WCHAR
) );
1071 r
= ERROR_OUTOFMEMORY
;
1075 lstrcpyW( stname
, tv
->name
);
1077 for ( i
= 0; i
< tv
->num_cols
; i
++ )
1079 type
= tv
->columns
[i
].type
;
1080 if ( type
& MSITYPE_KEY
)
1084 r
= TABLE_fetch_int( view
, row
, i
+1, &ival
);
1085 if ( r
!= ERROR_SUCCESS
)
1088 if ( tv
->columns
[i
].type
& MSITYPE_STRING
)
1090 sval
= msi_string_lookup( tv
->db
->strings
, ival
, NULL
);
1093 r
= ERROR_INVALID_PARAMETER
;
1099 static const WCHAR fmt
[] = { '%','d',0 };
1100 UINT n
= bytes_per_column( tv
->db
, &tv
->columns
[i
], LONG_STR_BYTES
);
1105 sprintfW( number
, fmt
, ival
-0x8000 );
1108 sprintfW( number
, fmt
, ival
^0x80000000 );
1111 ERR( "oops - unknown column width %d\n", n
);
1112 r
= ERROR_FUNCTION_FAILED
;
1118 len
+= lstrlenW( szDot
) + lstrlenW( sval
);
1119 p
= msi_realloc ( stname
, len
*sizeof(WCHAR
) );
1122 r
= ERROR_OUTOFMEMORY
;
1127 lstrcatW( stname
, szDot
);
1128 lstrcatW( stname
, sval
);
1135 return ERROR_SUCCESS
;
1144 * We need a special case for streams, as we need to reference column with
1145 * the name of the stream in the same table, and the table name
1146 * which may not be available at higher levels of the query
1148 static UINT
TABLE_fetch_stream( struct tagMSIVIEW
*view
, UINT row
, UINT col
, IStream
**stm
)
1150 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1152 LPWSTR encname
, full_name
= NULL
;
1154 if( !view
->ops
->fetch_int
)
1155 return ERROR_INVALID_PARAMETER
;
1157 r
= msi_stream_name( tv
, row
, &full_name
);
1158 if ( r
!= ERROR_SUCCESS
)
1160 ERR("fetching stream, error = %d\n", r
);
1164 encname
= encode_streamname( FALSE
, full_name
);
1165 r
= msi_get_raw_stream( tv
->db
, encname
, stm
);
1167 ERR("fetching stream %s, error = %d\n",debugstr_w(full_name
), r
);
1169 msi_free( full_name
);
1170 msi_free( encname
);
1174 static UINT
TABLE_set_int( MSITABLEVIEW
*tv
, UINT row
, UINT col
, UINT val
)
1179 return ERROR_INVALID_PARAMETER
;
1181 if( (col
==0) || (col
>tv
->num_cols
) )
1182 return ERROR_INVALID_PARAMETER
;
1184 if( row
>= tv
->table
->row_count
)
1185 return ERROR_INVALID_PARAMETER
;
1187 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1189 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1190 ERR("%p %p\n", tv
, tv
->columns
);
1191 return ERROR_FUNCTION_FAILED
;
1194 msi_free( tv
->columns
[col
-1].hash_table
);
1195 tv
->columns
[col
-1].hash_table
= NULL
;
1197 n
= bytes_per_column( tv
->db
, &tv
->columns
[col
- 1], LONG_STR_BYTES
);
1198 if ( n
!= 2 && n
!= 3 && n
!= 4 )
1200 ERR("oops! what is %d bytes per column?\n", n
);
1201 return ERROR_FUNCTION_FAILED
;
1204 offset
= tv
->columns
[col
-1].offset
;
1205 for ( i
= 0; i
< n
; i
++ )
1206 tv
->table
->data
[row
][offset
+ i
] = (val
>> i
* 8) & 0xff;
1208 return ERROR_SUCCESS
;
1211 static UINT
TABLE_get_row( struct tagMSIVIEW
*view
, UINT row
, MSIRECORD
**rec
)
1213 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1216 return ERROR_INVALID_PARAMETER
;
1218 return msi_view_get_row(tv
->db
, view
, row
, rec
);
1221 static UINT
msi_addstreamW( MSIDATABASE
*db
, LPCWSTR name
, IStream
*data
)
1223 static const WCHAR insert
[] = {
1224 'I','N','S','E','R','T',' ','I','N','T','O',' ',
1225 '`','_','S','t','r','e','a','m','s','`',' ',
1226 '(','`','N','a','m','e','`',',','`','D','a','t','a','`',')',' ',
1227 'V','A','L','U','E','S',' ','(','?',',','?',')',0};
1228 MSIQUERY
*query
= NULL
;
1232 TRACE("%p %s %p\n", db
, debugstr_w(name
), data
);
1234 rec
= MSI_CreateRecord( 2 );
1236 return ERROR_OUTOFMEMORY
;
1238 r
= MSI_RecordSetStringW( rec
, 1, name
);
1239 if ( r
!= ERROR_SUCCESS
)
1242 r
= MSI_RecordSetIStream( rec
, 2, data
);
1243 if ( r
!= ERROR_SUCCESS
)
1246 r
= MSI_DatabaseOpenViewW( db
, insert
, &query
);
1247 if ( r
!= ERROR_SUCCESS
)
1250 r
= MSI_ViewExecute( query
, rec
);
1253 msiobj_release( &query
->hdr
);
1254 msiobj_release( &rec
->hdr
);
1258 static UINT
get_table_value_from_record( MSITABLEVIEW
*tv
, MSIRECORD
*rec
, UINT iField
, UINT
*pvalue
)
1260 MSICOLUMNINFO columninfo
;
1264 if ( (iField
<= 0) ||
1265 (iField
> tv
->num_cols
) ||
1266 MSI_RecordIsNull( rec
, iField
) )
1267 return ERROR_FUNCTION_FAILED
;
1269 columninfo
= tv
->columns
[ iField
- 1 ];
1271 if ( MSITYPE_IS_BINARY(columninfo
.type
) )
1273 *pvalue
= 1; /* refers to the first key column */
1275 else if ( columninfo
.type
& MSITYPE_STRING
)
1278 const WCHAR
*sval
= msi_record_get_string( rec
, iField
, &len
);
1281 r
= msi_string2id( tv
->db
->strings
, sval
, len
, pvalue
);
1282 if (r
!= ERROR_SUCCESS
)
1283 return ERROR_NOT_FOUND
;
1287 else if ( bytes_per_column( tv
->db
, &columninfo
, LONG_STR_BYTES
) == 2 )
1289 ival
= MSI_RecordGetInteger( rec
, iField
);
1290 if (ival
== 0x80000000) *pvalue
= 0x8000;
1293 *pvalue
= 0x8000 + MSI_RecordGetInteger( rec
, iField
);
1294 if (*pvalue
& 0xffff0000)
1296 ERR("field %u value %d out of range\n", iField
, *pvalue
- 0x8000);
1297 return ERROR_FUNCTION_FAILED
;
1303 ival
= MSI_RecordGetInteger( rec
, iField
);
1304 *pvalue
= ival
^ 0x80000000;
1307 return ERROR_SUCCESS
;
1310 static UINT
TABLE_set_row( struct tagMSIVIEW
*view
, UINT row
, MSIRECORD
*rec
, UINT mask
)
1312 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1313 UINT i
, val
, r
= ERROR_SUCCESS
;
1316 return ERROR_INVALID_PARAMETER
;
1318 /* test if any of the mask bits are invalid */
1319 if ( mask
>= (1<<tv
->num_cols
) )
1320 return ERROR_INVALID_PARAMETER
;
1322 for ( i
= 0; i
< tv
->num_cols
; i
++ )
1326 /* only update the fields specified in the mask */
1327 if ( !(mask
&(1<<i
)) )
1330 persistent
= (tv
->table
->persistent
!= MSICONDITION_FALSE
) &&
1331 (tv
->table
->data_persistent
[row
]);
1332 /* FIXME: should we allow updating keys? */
1335 if ( !MSI_RecordIsNull( rec
, i
+ 1 ) )
1337 r
= get_table_value_from_record (tv
, rec
, i
+ 1, &val
);
1338 if ( MSITYPE_IS_BINARY(tv
->columns
[ i
].type
) )
1343 if ( r
!= ERROR_SUCCESS
)
1344 return ERROR_FUNCTION_FAILED
;
1346 r
= MSI_RecordGetIStream( rec
, i
+ 1, &stm
);
1347 if ( r
!= ERROR_SUCCESS
)
1350 r
= msi_stream_name( tv
, row
, &stname
);
1351 if ( r
!= ERROR_SUCCESS
)
1353 IStream_Release( stm
);
1357 r
= msi_addstreamW( tv
->db
, stname
, stm
);
1358 IStream_Release( stm
);
1359 msi_free ( stname
);
1361 if ( r
!= ERROR_SUCCESS
)
1364 else if ( tv
->columns
[i
].type
& MSITYPE_STRING
)
1368 if ( r
!= ERROR_SUCCESS
)
1371 const WCHAR
*sval
= msi_record_get_string( rec
, i
+ 1, &len
);
1372 val
= msi_addstringW( tv
->db
->strings
, sval
, len
, 1,
1373 persistent
? StringPersistent
: StringNonPersistent
);
1377 TABLE_fetch_int(&tv
->view
, row
, i
+ 1, &x
);
1384 if ( r
!= ERROR_SUCCESS
)
1385 return ERROR_FUNCTION_FAILED
;
1389 r
= TABLE_set_int( tv
, row
, i
+1, val
);
1390 if ( r
!= ERROR_SUCCESS
)
1396 static UINT
table_create_new_row( struct tagMSIVIEW
*view
, UINT
*num
, BOOL temporary
)
1398 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1403 BOOL
**data_persist_ptr
;
1406 TRACE("%p %s\n", view
, temporary
? "TRUE" : "FALSE");
1409 return ERROR_INVALID_PARAMETER
;
1411 row
= msi_alloc_zero( tv
->row_size
);
1413 return ERROR_NOT_ENOUGH_MEMORY
;
1415 row_count
= &tv
->table
->row_count
;
1416 data_ptr
= &tv
->table
->data
;
1417 data_persist_ptr
= &tv
->table
->data_persistent
;
1419 *num
= tv
->table
->row_count
;
1421 sz
= (*row_count
+ 1) * sizeof (BYTE
*);
1423 p
= msi_realloc( *data_ptr
, sz
);
1425 p
= msi_alloc( sz
);
1429 return ERROR_NOT_ENOUGH_MEMORY
;
1432 sz
= (*row_count
+ 1) * sizeof (BOOL
);
1433 if( *data_persist_ptr
)
1434 b
= msi_realloc( *data_persist_ptr
, sz
);
1436 b
= msi_alloc( sz
);
1441 return ERROR_NOT_ENOUGH_MEMORY
;
1445 (*data_ptr
)[*row_count
] = row
;
1447 *data_persist_ptr
= b
;
1448 (*data_persist_ptr
)[*row_count
] = !temporary
;
1452 return ERROR_SUCCESS
;
1455 static UINT
TABLE_execute( struct tagMSIVIEW
*view
, MSIRECORD
*record
)
1457 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1459 TRACE("%p %p\n", tv
, record
);
1461 TRACE("There are %d columns\n", tv
->num_cols
);
1463 return ERROR_SUCCESS
;
1466 static UINT
TABLE_close( struct tagMSIVIEW
*view
)
1468 TRACE("%p\n", view
);
1470 return ERROR_SUCCESS
;
1473 static UINT
TABLE_get_dimensions( struct tagMSIVIEW
*view
, UINT
*rows
, UINT
*cols
)
1475 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1477 TRACE("%p %p %p\n", view
, rows
, cols
);
1480 *cols
= tv
->num_cols
;
1484 return ERROR_INVALID_PARAMETER
;
1485 *rows
= tv
->table
->row_count
;
1488 return ERROR_SUCCESS
;
1491 static UINT
TABLE_get_column_info( struct tagMSIVIEW
*view
,
1492 UINT n
, LPCWSTR
*name
, UINT
*type
, BOOL
*temporary
,
1493 LPCWSTR
*table_name
)
1495 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1497 TRACE("%p %d %p %p\n", tv
, n
, name
, type
);
1499 if( ( n
== 0 ) || ( n
> tv
->num_cols
) )
1500 return ERROR_INVALID_PARAMETER
;
1504 *name
= tv
->columns
[n
-1].colname
;
1506 return ERROR_FUNCTION_FAILED
;
1511 *table_name
= tv
->columns
[n
-1].tablename
;
1513 return ERROR_FUNCTION_FAILED
;
1517 *type
= tv
->columns
[n
-1].type
;
1520 *temporary
= tv
->columns
[n
-1].temporary
;
1522 return ERROR_SUCCESS
;
1525 static UINT
msi_table_find_row( MSITABLEVIEW
*tv
, MSIRECORD
*rec
, UINT
*row
, UINT
*column
);
1527 static UINT
table_validate_new( MSITABLEVIEW
*tv
, MSIRECORD
*rec
, UINT
*column
)
1531 /* check there are no null values where they're not allowed */
1532 for( i
= 0; i
< tv
->num_cols
; i
++ )
1534 if ( tv
->columns
[i
].type
& MSITYPE_NULLABLE
)
1537 if ( MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
1538 TRACE("skipping binary column\n");
1539 else if ( tv
->columns
[i
].type
& MSITYPE_STRING
)
1542 const WCHAR
*str
= msi_record_get_string( rec
, i
+1, &len
);
1544 if (!str
|| (!str
[0] && !len
))
1546 if (column
) *column
= i
;
1547 return ERROR_INVALID_DATA
;
1554 n
= MSI_RecordGetInteger( rec
, i
+1 );
1555 if (n
== MSI_NULL_INTEGER
)
1557 if (column
) *column
= i
;
1558 return ERROR_INVALID_DATA
;
1563 /* check there are no duplicate keys */
1564 r
= msi_table_find_row( tv
, rec
, &row
, column
);
1565 if (r
== ERROR_SUCCESS
)
1566 return ERROR_FUNCTION_FAILED
;
1568 return ERROR_SUCCESS
;
1571 static int compare_record( MSITABLEVIEW
*tv
, UINT row
, MSIRECORD
*rec
)
1573 UINT r
, i
, ivalue
, x
;
1575 for (i
= 0; i
< tv
->num_cols
; i
++ )
1577 if (!(tv
->columns
[i
].type
& MSITYPE_KEY
)) continue;
1579 r
= get_table_value_from_record( tv
, rec
, i
+ 1, &ivalue
);
1580 if (r
!= ERROR_SUCCESS
)
1583 r
= TABLE_fetch_int( &tv
->view
, row
, i
+ 1, &x
);
1584 if (r
!= ERROR_SUCCESS
)
1586 WARN("TABLE_fetch_int should not fail here %u\n", r
);
1593 else if (ivalue
== x
)
1595 if (i
< tv
->num_cols
- 1) continue;
1604 static int find_insert_index( MSITABLEVIEW
*tv
, MSIRECORD
*rec
)
1606 int idx
, c
, low
= 0, high
= tv
->table
->row_count
- 1;
1608 TRACE("%p %p\n", tv
, rec
);
1612 idx
= (low
+ high
) / 2;
1613 c
= compare_record( tv
, idx
, rec
);
1621 TRACE("found %u\n", idx
);
1625 TRACE("found %u\n", high
+ 1);
1629 static UINT
TABLE_insert_row( struct tagMSIVIEW
*view
, MSIRECORD
*rec
, UINT row
, BOOL temporary
)
1631 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1634 TRACE("%p %p %s\n", tv
, rec
, temporary
? "TRUE" : "FALSE" );
1636 /* check that the key is unique - can we find a matching row? */
1637 r
= table_validate_new( tv
, rec
, NULL
);
1638 if( r
!= ERROR_SUCCESS
)
1639 return ERROR_FUNCTION_FAILED
;
1642 row
= find_insert_index( tv
, rec
);
1644 r
= table_create_new_row( view
, &row
, temporary
);
1645 TRACE("insert_row returned %08x\n", r
);
1646 if( r
!= ERROR_SUCCESS
)
1649 /* shift the rows to make room for the new row */
1650 for (i
= tv
->table
->row_count
- 1; i
> row
; i
--)
1652 memmove(&(tv
->table
->data
[i
][0]),
1653 &(tv
->table
->data
[i
- 1][0]), tv
->row_size
);
1654 tv
->table
->data_persistent
[i
] = tv
->table
->data_persistent
[i
- 1];
1657 /* Re-set the persistence flag */
1658 tv
->table
->data_persistent
[row
] = !temporary
;
1659 return TABLE_set_row( view
, row
, rec
, (1<<tv
->num_cols
) - 1 );
1662 static UINT
TABLE_delete_row( struct tagMSIVIEW
*view
, UINT row
)
1664 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1665 UINT r
, num_rows
, num_cols
, i
;
1667 TRACE("%p %d\n", tv
, row
);
1670 return ERROR_INVALID_PARAMETER
;
1672 r
= TABLE_get_dimensions( view
, &num_rows
, &num_cols
);
1673 if ( r
!= ERROR_SUCCESS
)
1676 if ( row
>= num_rows
)
1677 return ERROR_FUNCTION_FAILED
;
1679 num_rows
= tv
->table
->row_count
;
1680 tv
->table
->row_count
--;
1682 /* reset the hash tables */
1683 for (i
= 0; i
< tv
->num_cols
; i
++)
1685 msi_free( tv
->columns
[i
].hash_table
);
1686 tv
->columns
[i
].hash_table
= NULL
;
1689 for (i
= row
+ 1; i
< num_rows
; i
++)
1691 memcpy(tv
->table
->data
[i
- 1], tv
->table
->data
[i
], tv
->row_size
);
1692 tv
->table
->data_persistent
[i
- 1] = tv
->table
->data_persistent
[i
];
1695 msi_free(tv
->table
->data
[num_rows
- 1]);
1697 return ERROR_SUCCESS
;
1700 static UINT
msi_table_update(struct tagMSIVIEW
*view
, MSIRECORD
*rec
, UINT row
)
1702 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1705 /* FIXME: MsiViewFetch should set rec index 0 to some ID that
1706 * sets the fetched record apart from other records
1710 return ERROR_INVALID_PARAMETER
;
1712 r
= msi_table_find_row(tv
, rec
, &new_row
, NULL
);
1713 if (r
!= ERROR_SUCCESS
)
1715 ERR("can't find row to modify\n");
1716 return ERROR_FUNCTION_FAILED
;
1719 /* the row cannot be changed */
1720 if (row
!= new_row
+ 1)
1721 return ERROR_FUNCTION_FAILED
;
1723 return TABLE_set_row(view
, new_row
, rec
, (1 << tv
->num_cols
) - 1);
1726 static UINT
msi_table_assign(struct tagMSIVIEW
*view
, MSIRECORD
*rec
)
1728 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1732 return ERROR_INVALID_PARAMETER
;
1734 r
= msi_table_find_row(tv
, rec
, &row
, NULL
);
1735 if (r
== ERROR_SUCCESS
)
1736 return TABLE_set_row(view
, row
, rec
, (1 << tv
->num_cols
) - 1);
1738 return TABLE_insert_row( view
, rec
, -1, FALSE
);
1741 static UINT
modify_delete_row( struct tagMSIVIEW
*view
, MSIRECORD
*rec
)
1743 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1746 r
= msi_table_find_row(tv
, rec
, &row
, NULL
);
1747 if (r
!= ERROR_SUCCESS
)
1750 return TABLE_delete_row(view
, row
);
1753 static UINT
msi_refresh_record( struct tagMSIVIEW
*view
, MSIRECORD
*rec
, UINT row
)
1758 r
= TABLE_get_row(view
, row
- 1, &curr
);
1759 if (r
!= ERROR_SUCCESS
)
1762 /* Close the original record */
1763 MSI_CloseRecord(&rec
->hdr
);
1765 count
= MSI_RecordGetFieldCount(rec
);
1766 for (i
= 0; i
< count
; i
++)
1767 MSI_RecordCopyField(curr
, i
+ 1, rec
, i
+ 1);
1769 msiobj_release(&curr
->hdr
);
1770 return ERROR_SUCCESS
;
1773 static UINT
TABLE_modify( struct tagMSIVIEW
*view
, MSIMODIFY eModifyMode
,
1774 MSIRECORD
*rec
, UINT row
)
1776 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1777 UINT r
, frow
, column
;
1779 TRACE("%p %d %p\n", view
, eModifyMode
, rec
);
1781 switch (eModifyMode
)
1783 case MSIMODIFY_DELETE
:
1784 r
= modify_delete_row( view
, rec
);
1786 case MSIMODIFY_VALIDATE_NEW
:
1787 r
= table_validate_new( tv
, rec
, &column
);
1788 if (r
!= ERROR_SUCCESS
)
1790 tv
->view
.error
= MSIDBERROR_DUPLICATEKEY
;
1791 tv
->view
.error_column
= tv
->columns
[column
].colname
;
1792 r
= ERROR_INVALID_DATA
;
1796 case MSIMODIFY_INSERT
:
1797 r
= table_validate_new( tv
, rec
, NULL
);
1798 if (r
!= ERROR_SUCCESS
)
1800 r
= TABLE_insert_row( view
, rec
, -1, FALSE
);
1803 case MSIMODIFY_INSERT_TEMPORARY
:
1804 r
= table_validate_new( tv
, rec
, NULL
);
1805 if (r
!= ERROR_SUCCESS
)
1807 r
= TABLE_insert_row( view
, rec
, -1, TRUE
);
1810 case MSIMODIFY_REFRESH
:
1811 r
= msi_refresh_record( view
, rec
, row
);
1814 case MSIMODIFY_UPDATE
:
1815 r
= msi_table_update( view
, rec
, row
);
1818 case MSIMODIFY_ASSIGN
:
1819 r
= msi_table_assign( view
, rec
);
1822 case MSIMODIFY_MERGE
:
1823 /* check row that matches this record */
1824 r
= msi_table_find_row( tv
, rec
, &frow
, &column
);
1825 if (r
!= ERROR_SUCCESS
)
1827 r
= table_validate_new( tv
, rec
, NULL
);
1828 if (r
== ERROR_SUCCESS
)
1829 r
= TABLE_insert_row( view
, rec
, -1, FALSE
);
1833 case MSIMODIFY_REPLACE
:
1834 case MSIMODIFY_VALIDATE
:
1835 case MSIMODIFY_VALIDATE_FIELD
:
1836 case MSIMODIFY_VALIDATE_DELETE
:
1837 FIXME("%p %d %p - mode not implemented\n", view
, eModifyMode
, rec
);
1838 r
= ERROR_CALL_NOT_IMPLEMENTED
;
1842 r
= ERROR_INVALID_DATA
;
1848 static UINT
TABLE_delete( struct tagMSIVIEW
*view
)
1850 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1852 TRACE("%p\n", view
);
1859 return ERROR_SUCCESS
;
1862 static UINT
TABLE_find_matching_rows( struct tagMSIVIEW
*view
, UINT col
,
1863 UINT val
, UINT
*row
, MSIITERHANDLE
*handle
)
1865 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1866 const MSICOLUMNHASHENTRY
*entry
;
1868 TRACE("%p, %d, %u, %p\n", view
, col
, val
, *handle
);
1871 return ERROR_INVALID_PARAMETER
;
1873 if( (col
==0) || (col
> tv
->num_cols
) )
1874 return ERROR_INVALID_PARAMETER
;
1876 if( !tv
->columns
[col
-1].hash_table
)
1879 UINT num_rows
= tv
->table
->row_count
;
1880 MSICOLUMNHASHENTRY
**hash_table
;
1881 MSICOLUMNHASHENTRY
*new_entry
;
1883 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1885 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1886 ERR("%p %p\n", tv
, tv
->columns
);
1887 return ERROR_FUNCTION_FAILED
;
1890 /* allocate contiguous memory for the table and its entries so we
1891 * don't have to do an expensive cleanup */
1892 hash_table
= msi_alloc(MSITABLE_HASH_TABLE_SIZE
* sizeof(MSICOLUMNHASHENTRY
*) +
1893 num_rows
* sizeof(MSICOLUMNHASHENTRY
));
1895 return ERROR_OUTOFMEMORY
;
1897 memset(hash_table
, 0, MSITABLE_HASH_TABLE_SIZE
* sizeof(MSICOLUMNHASHENTRY
*));
1898 tv
->columns
[col
-1].hash_table
= hash_table
;
1900 new_entry
= (MSICOLUMNHASHENTRY
*)(hash_table
+ MSITABLE_HASH_TABLE_SIZE
);
1902 for (i
= 0; i
< num_rows
; i
++, new_entry
++)
1906 if (view
->ops
->fetch_int( view
, i
, col
, &row_value
) != ERROR_SUCCESS
)
1909 new_entry
->next
= NULL
;
1910 new_entry
->value
= row_value
;
1912 if (hash_table
[row_value
% MSITABLE_HASH_TABLE_SIZE
])
1914 MSICOLUMNHASHENTRY
*prev_entry
= hash_table
[row_value
% MSITABLE_HASH_TABLE_SIZE
];
1915 while (prev_entry
->next
)
1916 prev_entry
= prev_entry
->next
;
1917 prev_entry
->next
= new_entry
;
1920 hash_table
[row_value
% MSITABLE_HASH_TABLE_SIZE
] = new_entry
;
1925 entry
= tv
->columns
[col
-1].hash_table
[val
% MSITABLE_HASH_TABLE_SIZE
];
1927 entry
= (*handle
)->next
;
1929 while (entry
&& entry
->value
!= val
)
1930 entry
= entry
->next
;
1934 return ERROR_NO_MORE_ITEMS
;
1938 return ERROR_SUCCESS
;
1941 static UINT
TABLE_add_ref(struct tagMSIVIEW
*view
)
1943 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1946 TRACE("%p %d\n", view
, tv
->table
->ref_count
);
1948 for (i
= 0; i
< tv
->table
->col_count
; i
++)
1950 if (tv
->table
->colinfo
[i
].type
& MSITYPE_TEMPORARY
)
1951 InterlockedIncrement(&tv
->table
->colinfo
[i
].ref_count
);
1954 return InterlockedIncrement(&tv
->table
->ref_count
);
1957 static UINT
TABLE_remove_column(struct tagMSIVIEW
*view
, LPCWSTR table
, UINT number
)
1959 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1960 MSIRECORD
*rec
= NULL
;
1961 MSIVIEW
*columns
= NULL
;
1964 rec
= MSI_CreateRecord(2);
1966 return ERROR_OUTOFMEMORY
;
1968 MSI_RecordSetStringW(rec
, 1, table
);
1969 MSI_RecordSetInteger(rec
, 2, number
);
1971 r
= TABLE_CreateView(tv
->db
, szColumns
, &columns
);
1972 if (r
!= ERROR_SUCCESS
)
1974 msiobj_release(&rec
->hdr
);
1978 r
= msi_table_find_row((MSITABLEVIEW
*)columns
, rec
, &row
, NULL
);
1979 if (r
!= ERROR_SUCCESS
)
1982 r
= TABLE_delete_row(columns
, row
);
1983 if (r
!= ERROR_SUCCESS
)
1986 msi_update_table_columns(tv
->db
, table
);
1989 msiobj_release(&rec
->hdr
);
1990 columns
->ops
->delete(columns
);
1994 static UINT
TABLE_release(struct tagMSIVIEW
*view
)
1996 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1997 INT ref
= tv
->table
->ref_count
;
2000 TRACE("%p %d\n", view
, ref
);
2002 for (i
= 0; i
< tv
->table
->col_count
; i
++)
2004 if (tv
->table
->colinfo
[i
].type
& MSITYPE_TEMPORARY
)
2006 ref
= InterlockedDecrement(&tv
->table
->colinfo
[i
].ref_count
);
2009 r
= TABLE_remove_column(view
, tv
->table
->colinfo
[i
].tablename
,
2010 tv
->table
->colinfo
[i
].number
);
2011 if (r
!= ERROR_SUCCESS
)
2017 ref
= InterlockedDecrement(&tv
->table
->ref_count
);
2020 if (!tv
->table
->row_count
)
2022 list_remove(&tv
->table
->entry
);
2023 free_table(tv
->table
);
2031 static UINT
TABLE_add_column(struct tagMSIVIEW
*view
, LPCWSTR table
, UINT number
,
2032 LPCWSTR column
, UINT type
, BOOL hold
)
2034 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
2039 rec
= MSI_CreateRecord(4);
2041 return ERROR_OUTOFMEMORY
;
2043 MSI_RecordSetStringW(rec
, 1, table
);
2044 MSI_RecordSetInteger(rec
, 2, number
);
2045 MSI_RecordSetStringW(rec
, 3, column
);
2046 MSI_RecordSetInteger(rec
, 4, type
);
2048 r
= TABLE_insert_row(&tv
->view
, rec
, -1, FALSE
);
2049 if (r
!= ERROR_SUCCESS
)
2052 msi_update_table_columns(tv
->db
, table
);
2057 msitable
= find_cached_table(tv
->db
, table
);
2058 for (i
= 0; i
< msitable
->col_count
; i
++)
2060 if (!strcmpW( msitable
->colinfo
[i
].colname
, column
))
2062 InterlockedIncrement(&msitable
->colinfo
[i
].ref_count
);
2068 msiobj_release(&rec
->hdr
);
2072 static UINT
TABLE_drop(struct tagMSIVIEW
*view
)
2074 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
2075 MSIVIEW
*tables
= NULL
;
2076 MSIRECORD
*rec
= NULL
;
2080 TRACE("dropping table %s\n", debugstr_w(tv
->name
));
2082 for (i
= tv
->table
->col_count
- 1; i
>= 0; i
--)
2084 r
= TABLE_remove_column(view
, tv
->table
->colinfo
[i
].tablename
,
2085 tv
->table
->colinfo
[i
].number
);
2086 if (r
!= ERROR_SUCCESS
)
2090 rec
= MSI_CreateRecord(1);
2092 return ERROR_OUTOFMEMORY
;
2094 MSI_RecordSetStringW(rec
, 1, tv
->name
);
2096 r
= TABLE_CreateView(tv
->db
, szTables
, &tables
);
2097 if (r
!= ERROR_SUCCESS
)
2099 msiobj_release(&rec
->hdr
);
2103 r
= msi_table_find_row((MSITABLEVIEW
*)tables
, rec
, &row
, NULL
);
2104 if (r
!= ERROR_SUCCESS
)
2107 r
= TABLE_delete_row(tables
, row
);
2108 if (r
!= ERROR_SUCCESS
)
2111 list_remove(&tv
->table
->entry
);
2112 free_table(tv
->table
);
2115 msiobj_release(&rec
->hdr
);
2116 tables
->ops
->delete(tables
);
2121 static const MSIVIEWOPS table_ops
=
2131 TABLE_get_dimensions
,
2132 TABLE_get_column_info
,
2135 TABLE_find_matching_rows
,
2139 TABLE_remove_column
,
2144 UINT
TABLE_CreateView( MSIDATABASE
*db
, LPCWSTR name
, MSIVIEW
**view
)
2149 TRACE("%p %s %p\n", db
, debugstr_w(name
), view
);
2151 if ( !strcmpW( name
, szStreams
) )
2152 return STREAMS_CreateView( db
, view
);
2153 else if ( !strcmpW( name
, szStorages
) )
2154 return STORAGES_CreateView( db
, view
);
2156 sz
= FIELD_OFFSET( MSITABLEVIEW
, name
[lstrlenW( name
) + 1] );
2157 tv
= msi_alloc_zero( sz
);
2159 return ERROR_FUNCTION_FAILED
;
2161 r
= get_table( db
, name
, &tv
->table
);
2162 if( r
!= ERROR_SUCCESS
)
2165 WARN("table not found\n");
2169 TRACE("table %p found with %d columns\n", tv
->table
, tv
->table
->col_count
);
2171 /* fill the structure */
2172 tv
->view
.ops
= &table_ops
;
2174 tv
->columns
= tv
->table
->colinfo
;
2175 tv
->num_cols
= tv
->table
->col_count
;
2176 tv
->row_size
= msi_table_get_row_size( db
, tv
->table
->colinfo
, tv
->table
->col_count
, LONG_STR_BYTES
);
2178 TRACE("%s one row is %d bytes\n", debugstr_w(name
), tv
->row_size
);
2180 *view
= (MSIVIEW
*) tv
;
2181 lstrcpyW( tv
->name
, name
);
2183 return ERROR_SUCCESS
;
2186 UINT
MSI_CommitTables( MSIDATABASE
*db
)
2188 UINT r
, bytes_per_strref
;
2190 MSITABLE
*table
= NULL
;
2194 r
= msi_save_string_table( db
->strings
, db
->storage
, &bytes_per_strref
);
2195 if( r
!= ERROR_SUCCESS
)
2197 WARN("failed to save string table r=%08x\n",r
);
2201 LIST_FOR_EACH_ENTRY( table
, &db
->tables
, MSITABLE
, entry
)
2203 r
= save_table( db
, table
, bytes_per_strref
);
2204 if( r
!= ERROR_SUCCESS
)
2206 WARN("failed to save table %s (r=%08x)\n",
2207 debugstr_w(table
->name
), r
);
2212 hr
= IStorage_Commit( db
->storage
, 0 );
2215 WARN("failed to commit changes 0x%08x\n", hr
);
2216 r
= ERROR_FUNCTION_FAILED
;
2221 MSICONDITION
MSI_DatabaseIsTablePersistent( MSIDATABASE
*db
, LPCWSTR table
)
2226 TRACE("%p %s\n", db
, debugstr_w(table
));
2229 return MSICONDITION_ERROR
;
2231 r
= get_table( db
, table
, &t
);
2232 if (r
!= ERROR_SUCCESS
)
2233 return MSICONDITION_NONE
;
2235 return t
->persistent
;
2238 static UINT
read_raw_int(const BYTE
*data
, UINT col
, UINT bytes
)
2242 for (i
= 0; i
< bytes
; i
++)
2243 ret
+= (data
[col
+ i
] << i
* 8);
2248 static UINT
msi_record_encoded_stream_name( const MSITABLEVIEW
*tv
, MSIRECORD
*rec
, LPWSTR
*pstname
)
2250 LPWSTR stname
= NULL
, sval
, p
;
2254 TRACE("%p %p\n", tv
, rec
);
2256 len
= lstrlenW( tv
->name
) + 1;
2257 stname
= msi_alloc( len
*sizeof(WCHAR
) );
2260 r
= ERROR_OUTOFMEMORY
;
2264 lstrcpyW( stname
, tv
->name
);
2266 for ( i
= 0; i
< tv
->num_cols
; i
++ )
2268 if ( tv
->columns
[i
].type
& MSITYPE_KEY
)
2270 sval
= msi_dup_record_field( rec
, i
+ 1 );
2273 r
= ERROR_OUTOFMEMORY
;
2277 len
+= lstrlenW( szDot
) + lstrlenW ( sval
);
2278 p
= msi_realloc ( stname
, len
*sizeof(WCHAR
) );
2281 r
= ERROR_OUTOFMEMORY
;
2287 lstrcatW( stname
, szDot
);
2288 lstrcatW( stname
, sval
);
2296 *pstname
= encode_streamname( FALSE
, stname
);
2299 return ERROR_SUCCESS
;
2302 msi_free ( stname
);
2307 static MSIRECORD
*msi_get_transform_record( const MSITABLEVIEW
*tv
, const string_table
*st
,
2309 const BYTE
*rawdata
, UINT bytes_per_strref
)
2311 UINT i
, val
, ofs
= 0;
2313 MSICOLUMNINFO
*columns
= tv
->columns
;
2316 mask
= rawdata
[0] | (rawdata
[1] << 8);
2319 rec
= MSI_CreateRecord( tv
->num_cols
);
2324 for( i
=0; i
<tv
->num_cols
; i
++ )
2326 if ( (mask
&1) && (i
>=(mask
>>8)) )
2328 /* all keys must be present */
2329 if ( (~mask
&1) && (~columns
[i
].type
& MSITYPE_KEY
) && ((1<<i
) & ~mask
) )
2332 if( MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
2335 IStream
*stm
= NULL
;
2338 ofs
+= bytes_per_column( tv
->db
, &columns
[i
], bytes_per_strref
);
2340 r
= msi_record_encoded_stream_name( tv
, rec
, &encname
);
2341 if ( r
!= ERROR_SUCCESS
)
2344 r
= IStorage_OpenStream( stg
, encname
, NULL
,
2345 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, &stm
);
2346 if ( r
!= ERROR_SUCCESS
)
2348 msi_free( encname
);
2352 MSI_RecordSetStream( rec
, i
+1, stm
);
2353 TRACE(" field %d [%s]\n", i
+1, debugstr_w(encname
));
2354 msi_free( encname
);
2356 else if( columns
[i
].type
& MSITYPE_STRING
)
2361 val
= read_raw_int(rawdata
, ofs
, bytes_per_strref
);
2362 sval
= msi_string_lookup( st
, val
, &len
);
2363 msi_record_set_string( rec
, i
+1, sval
, len
);
2364 TRACE(" field %d [%s]\n", i
+1, debugstr_wn(sval
, len
));
2365 ofs
+= bytes_per_strref
;
2369 UINT n
= bytes_per_column( tv
->db
, &columns
[i
], bytes_per_strref
);
2373 val
= read_raw_int(rawdata
, ofs
, n
);
2375 MSI_RecordSetInteger( rec
, i
+1, val
-0x8000 );
2376 TRACE(" field %d [0x%04x]\n", i
+1, val
);
2379 val
= read_raw_int(rawdata
, ofs
, n
);
2381 MSI_RecordSetInteger( rec
, i
+1, val
^0x80000000 );
2382 TRACE(" field %d [0x%08x]\n", i
+1, val
);
2385 ERR("oops - unknown column width %d\n", n
);
2394 static void dump_record( MSIRECORD
*rec
)
2398 n
= MSI_RecordGetFieldCount( rec
);
2399 for( i
=1; i
<=n
; i
++ )
2404 if( MSI_RecordIsNull( rec
, i
) )
2405 TRACE("row -> []\n");
2406 else if( (sval
= msi_record_get_string( rec
, i
, &len
)) )
2407 TRACE("row -> [%s]\n", debugstr_wn(sval
, len
));
2409 TRACE("row -> [0x%08x]\n", MSI_RecordGetInteger( rec
, i
) );
2413 static void dump_table( const string_table
*st
, const USHORT
*rawdata
, UINT rawsize
)
2416 for (i
= 0; i
< rawsize
/ 2; i
++)
2419 const WCHAR
*sval
= msi_string_lookup( st
, rawdata
[i
], &len
);
2420 MESSAGE(" %04x %s\n", rawdata
[i
], debugstr_wn(sval
, len
) );
2424 static UINT
* msi_record_to_row( const MSITABLEVIEW
*tv
, MSIRECORD
*rec
)
2428 data
= msi_alloc( tv
->num_cols
*sizeof (UINT
) );
2429 for( i
=0; i
<tv
->num_cols
; i
++ )
2433 if ( ~tv
->columns
[i
].type
& MSITYPE_KEY
)
2436 /* turn the transform column value into a row value */
2437 if ( ( tv
->columns
[i
].type
& MSITYPE_STRING
) &&
2438 ! MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
2441 const WCHAR
*str
= msi_record_get_string( rec
, i
+1, &len
);
2444 r
= msi_string2id( tv
->db
->strings
, str
, len
, &data
[i
] );
2446 /* if there's no matching string in the string table,
2447 these keys can't match any record, so fail now. */
2448 if (r
!= ERROR_SUCCESS
)
2458 data
[i
] = MSI_RecordGetInteger( rec
, i
+1 );
2460 if (data
[i
] == MSI_NULL_INTEGER
)
2462 else if ((tv
->columns
[i
].type
&0xff) == 2)
2465 data
[i
] += 0x80000000;
2471 static UINT
msi_row_matches( MSITABLEVIEW
*tv
, UINT row
, const UINT
*data
, UINT
*column
)
2473 UINT i
, r
, x
, ret
= ERROR_FUNCTION_FAILED
;
2475 for( i
=0; i
<tv
->num_cols
; i
++ )
2477 if ( ~tv
->columns
[i
].type
& MSITYPE_KEY
)
2480 /* turn the transform column value into a row value */
2481 r
= TABLE_fetch_int( &tv
->view
, row
, i
+1, &x
);
2482 if ( r
!= ERROR_SUCCESS
)
2484 ERR("TABLE_fetch_int shouldn't fail here\n");
2488 /* if this key matches, move to the next column */
2491 ret
= ERROR_FUNCTION_FAILED
;
2494 if (column
) *column
= i
;
2495 ret
= ERROR_SUCCESS
;
2500 static UINT
msi_table_find_row( MSITABLEVIEW
*tv
, MSIRECORD
*rec
, UINT
*row
, UINT
*column
)
2502 UINT i
, r
= ERROR_FUNCTION_FAILED
, *data
;
2504 data
= msi_record_to_row( tv
, rec
);
2507 for( i
= 0; i
< tv
->table
->row_count
; i
++ )
2509 r
= msi_row_matches( tv
, i
, data
, column
);
2510 if( r
== ERROR_SUCCESS
)
2526 static UINT
msi_table_load_transform( MSIDATABASE
*db
, IStorage
*stg
,
2527 string_table
*st
, TRANSFORMDATA
*transform
,
2528 UINT bytes_per_strref
)
2530 BYTE
*rawdata
= NULL
;
2531 MSITABLEVIEW
*tv
= NULL
;
2532 UINT r
, n
, sz
, i
, mask
, num_cols
, colcol
= 0, rawsize
= 0;
2533 MSIRECORD
*rec
= NULL
;
2538 return ERROR_SUCCESS
;
2540 name
= transform
->name
;
2543 TRACE("%p %p %p %s\n", db
, stg
, st
, debugstr_w(name
) );
2545 /* read the transform data */
2546 read_stream_data( stg
, name
, TRUE
, &rawdata
, &rawsize
);
2549 TRACE("table %s empty\n", debugstr_w(name
) );
2550 return ERROR_INVALID_TABLE
;
2553 /* create a table view */
2554 r
= TABLE_CreateView( db
, name
, (MSIVIEW
**) &tv
);
2555 if( r
!= ERROR_SUCCESS
)
2558 r
= tv
->view
.ops
->execute( &tv
->view
, NULL
);
2559 if( r
!= ERROR_SUCCESS
)
2562 TRACE("name = %s columns = %u row_size = %u raw size = %u\n",
2563 debugstr_w(name
), tv
->num_cols
, tv
->row_size
, rawsize
);
2565 /* interpret the data */
2566 for (n
= 0; n
< rawsize
;)
2568 mask
= rawdata
[n
] | (rawdata
[n
+ 1] << 8);
2572 * if the low bit is set, columns are continuous and
2573 * the number of columns is specified in the high byte
2576 num_cols
= mask
>> 8;
2577 for (i
= 0; i
< num_cols
; i
++)
2579 if( (tv
->columns
[i
].type
& MSITYPE_STRING
) &&
2580 ! MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
2581 sz
+= bytes_per_strref
;
2583 sz
+= bytes_per_column( tv
->db
, &tv
->columns
[i
], bytes_per_strref
);
2589 * If the low bit is not set, mask is a bitmask.
2590 * Excepting for key fields, which are always present,
2591 * each bit indicates that a field is present in the transform record.
2593 * mask == 0 is a special case ... only the keys will be present
2594 * and it means that this row should be deleted.
2597 num_cols
= tv
->num_cols
;
2598 for (i
= 0; i
< num_cols
; i
++)
2600 if ((tv
->columns
[i
].type
& MSITYPE_KEY
) || ((1 << i
) & mask
))
2602 if ((tv
->columns
[i
].type
& MSITYPE_STRING
) &&
2603 !MSITYPE_IS_BINARY(tv
->columns
[i
].type
))
2604 sz
+= bytes_per_strref
;
2606 sz
+= bytes_per_column( tv
->db
, &tv
->columns
[i
], bytes_per_strref
);
2611 /* check we didn't run of the end of the table */
2612 if (n
+ sz
> rawsize
)
2615 dump_table( st
, (USHORT
*)rawdata
, rawsize
);
2619 rec
= msi_get_transform_record( tv
, st
, stg
, &rawdata
[n
], bytes_per_strref
);
2624 UINT number
= MSI_NULL_INTEGER
;
2627 if (!strcmpW( name
, szColumns
))
2629 MSI_RecordGetStringW( rec
, 1, table
, &sz
);
2630 number
= MSI_RecordGetInteger( rec
, 2 );
2633 * Native msi seems writes nul into the Number (2nd) column of
2634 * the _Columns table, only when the columns are from a new table
2636 if ( number
== MSI_NULL_INTEGER
)
2638 /* reset the column number on a new table */
2639 if (strcmpW( coltable
, table
))
2642 lstrcpyW( coltable
, table
);
2645 /* fix nul column numbers */
2646 MSI_RecordSetInteger( rec
, 2, ++colcol
);
2650 if (TRACE_ON(msidb
)) dump_record( rec
);
2652 r
= msi_table_find_row( tv
, rec
, &row
, NULL
);
2653 if (r
== ERROR_SUCCESS
)
2657 TRACE("deleting row [%d]:\n", row
);
2658 r
= TABLE_delete_row( &tv
->view
, row
);
2659 if (r
!= ERROR_SUCCESS
)
2660 WARN("failed to delete row %u\n", r
);
2664 TRACE("modifying full row [%d]:\n", row
);
2665 r
= TABLE_set_row( &tv
->view
, row
, rec
, (1 << tv
->num_cols
) - 1 );
2666 if (r
!= ERROR_SUCCESS
)
2667 WARN("failed to modify row %u\n", r
);
2671 TRACE("modifying masked row [%d]:\n", row
);
2672 r
= TABLE_set_row( &tv
->view
, row
, rec
, mask
);
2673 if (r
!= ERROR_SUCCESS
)
2674 WARN("failed to modify row %u\n", r
);
2679 TRACE("inserting row\n");
2680 r
= TABLE_insert_row( &tv
->view
, rec
, -1, FALSE
);
2681 if (r
!= ERROR_SUCCESS
)
2682 WARN("failed to insert row %u\n", r
);
2685 if (number
!= MSI_NULL_INTEGER
&& !strcmpW( name
, szColumns
))
2686 msi_update_table_columns( db
, table
);
2688 msiobj_release( &rec
->hdr
);
2695 /* no need to free the table, it's associated with the database */
2696 msi_free( rawdata
);
2698 tv
->view
.ops
->delete( &tv
->view
);
2700 return ERROR_SUCCESS
;
2704 * msi_table_apply_transform
2706 * Enumerate the table transforms in a transform storage and apply each one.
2708 UINT
msi_table_apply_transform( MSIDATABASE
*db
, IStorage
*stg
)
2710 struct list transforms
;
2711 IEnumSTATSTG
*stgenum
= NULL
;
2712 TRANSFORMDATA
*transform
;
2713 TRANSFORMDATA
*tables
= NULL
, *columns
= NULL
;
2716 string_table
*strings
;
2717 UINT ret
= ERROR_FUNCTION_FAILED
;
2718 UINT bytes_per_strref
;
2720 TRACE("%p %p\n", db
, stg
);
2722 strings
= msi_load_string_table( stg
, &bytes_per_strref
);
2726 r
= IStorage_EnumElements( stg
, 0, NULL
, 0, &stgenum
);
2730 list_init(&transforms
);
2734 MSITABLEVIEW
*tv
= NULL
;
2738 r
= IEnumSTATSTG_Next( stgenum
, 1, &stat
, &count
);
2739 if ( FAILED( r
) || !count
)
2742 decode_streamname( stat
.pwcsName
, name
);
2743 CoTaskMemFree( stat
.pwcsName
);
2744 if ( name
[0] != 0x4840 )
2747 if ( !strcmpW( name
+1, szStringPool
) ||
2748 !strcmpW( name
+1, szStringData
) )
2751 transform
= msi_alloc_zero( sizeof(TRANSFORMDATA
) );
2755 list_add_tail( &transforms
, &transform
->entry
);
2757 transform
->name
= strdupW( name
+ 1 );
2759 if ( !strcmpW( transform
->name
, szTables
) )
2761 else if (!strcmpW( transform
->name
, szColumns
) )
2762 columns
= transform
;
2764 TRACE("transform contains stream %s\n", debugstr_w(name
));
2766 /* load the table */
2767 r
= TABLE_CreateView( db
, transform
->name
, (MSIVIEW
**) &tv
);
2768 if( r
!= ERROR_SUCCESS
)
2771 r
= tv
->view
.ops
->execute( &tv
->view
, NULL
);
2772 if( r
!= ERROR_SUCCESS
)
2774 tv
->view
.ops
->delete( &tv
->view
);
2778 tv
->view
.ops
->delete( &tv
->view
);
2782 * Apply _Tables and _Columns transforms first so that
2783 * the table metadata is correct, and empty tables exist.
2785 ret
= msi_table_load_transform( db
, stg
, strings
, tables
, bytes_per_strref
);
2786 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_INVALID_TABLE
)
2789 ret
= msi_table_load_transform( db
, stg
, strings
, columns
, bytes_per_strref
);
2790 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_INVALID_TABLE
)
2793 ret
= ERROR_SUCCESS
;
2795 while ( !list_empty( &transforms
) )
2797 transform
= LIST_ENTRY( list_head( &transforms
), TRANSFORMDATA
, entry
);
2799 if ( strcmpW( transform
->name
, szColumns
) &&
2800 strcmpW( transform
->name
, szTables
) &&
2801 ret
== ERROR_SUCCESS
)
2803 ret
= msi_table_load_transform( db
, stg
, strings
, transform
, bytes_per_strref
);
2806 list_remove( &transform
->entry
);
2807 msi_free( transform
->name
);
2808 msi_free( transform
);
2811 if ( ret
== ERROR_SUCCESS
)
2812 append_storage_to_db( db
, stg
);
2816 IEnumSTATSTG_Release( stgenum
);
2818 msi_destroy_stringtable( strings
);