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
23 WINE_DEFAULT_DEBUG_CHANNEL(msidb
);
25 #define MSITABLE_HASH_TABLE_SIZE 37
27 typedef struct tagMSICOLUMNHASHENTRY
29 struct tagMSICOLUMNHASHENTRY
*next
;
34 typedef struct tagMSICOLUMNINFO
43 MSICOLUMNHASHENTRY
**hash_table
;
49 BOOL
*data_persistent
;
52 MSICOLUMNINFO
*colinfo
;
54 MSICONDITION persistent
;
59 /* information for default tables */
60 static const WCHAR szTables
[] = {'_','T','a','b','l','e','s',0};
61 static const WCHAR szTable
[] = {'T','a','b','l','e',0};
62 static const WCHAR szColumns
[] = {'_','C','o','l','u','m','n','s',0};
63 static const WCHAR szNumber
[] = {'N','u','m','b','e','r',0};
64 static const WCHAR szType
[] = {'T','y','p','e',0};
66 static const MSICOLUMNINFO _Columns_cols
[4] = {
67 { szColumns
, 1, szTable
, MSITYPE_VALID
| MSITYPE_STRING
| MSITYPE_KEY
| 64, 0, 0, 0, NULL
},
68 { szColumns
, 2, szNumber
, MSITYPE_VALID
| MSITYPE_KEY
| 2, 2, 0, 0, NULL
},
69 { szColumns
, 3, szName
, MSITYPE_VALID
| MSITYPE_STRING
| 64, 4, 0, 0, NULL
},
70 { szColumns
, 4, szType
, MSITYPE_VALID
| 2, 6, 0, 0, NULL
},
73 static const MSICOLUMNINFO _Tables_cols
[1] = {
74 { szTables
, 1, szName
, MSITYPE_VALID
| MSITYPE_STRING
| MSITYPE_KEY
| 64, 0, 0, 0, NULL
},
77 #define MAX_STREAM_NAME 0x1f
79 static inline UINT
bytes_per_column( MSIDATABASE
*db
, const MSICOLUMNINFO
*col
, UINT bytes_per_strref
)
81 if( MSITYPE_IS_BINARY(col
->type
) )
84 if( col
->type
& MSITYPE_STRING
)
85 return bytes_per_strref
;
87 if( (col
->type
& 0xff) <= 2)
90 if( (col
->type
& 0xff) != 4 )
91 ERR("Invalid column size %u\n", col
->type
& 0xff);
96 static int utf2mime(int x
)
98 if( (x
>='0') && (x
<='9') )
100 if( (x
>='A') && (x
<='Z') )
102 if( (x
>='a') && (x
<='z') )
111 LPWSTR
encode_streamname(BOOL bTable
, LPCWSTR in
)
113 DWORD count
= MAX_STREAM_NAME
;
118 count
= lstrlenW( in
)+2;
119 if (!(out
= msi_alloc( count
*sizeof(WCHAR
) ))) return NULL
;
135 if( ( ch
< 0x80 ) && ( utf2mime(ch
) >= 0 ) )
137 ch
= utf2mime(ch
) + 0x4800;
139 if( next
&& (next
<0x80) )
141 next
= utf2mime(next
);
152 ERR("Failed to encode stream name (%s)\n",debugstr_w(in
));
157 static int mime2utf(int x
)
164 return x
- 10 - 26 + 'a';
165 if( x
== (10+26+26) )
170 BOOL
decode_streamname(LPCWSTR in
, LPWSTR out
)
175 while ( (ch
= *in
++) )
177 if( (ch
>= 0x3800 ) && (ch
< 0x4840 ) )
180 ch
= mime2utf(ch
-0x4800);
184 *out
++ = mime2utf(ch
&0x3f);
186 ch
= mime2utf((ch
>>6)&0x3f);
196 void enum_stream_names( IStorage
*stg
)
198 IEnumSTATSTG
*stgenum
= NULL
;
204 r
= IStorage_EnumElements( stg
, 0, NULL
, 0, &stgenum
);
212 r
= IEnumSTATSTG_Next( stgenum
, 1, &stat
, &count
);
213 if( FAILED( r
) || !count
)
215 decode_streamname( stat
.pwcsName
, name
);
216 TRACE("stream %2d -> %s %s\n", n
,
217 debugstr_w(stat
.pwcsName
), debugstr_w(name
) );
218 CoTaskMemFree( stat
.pwcsName
);
222 IEnumSTATSTG_Release( stgenum
);
225 UINT
read_stream_data( IStorage
*stg
, LPCWSTR stname
, BOOL table
,
226 BYTE
**pdata
, UINT
*psz
)
229 UINT ret
= ERROR_FUNCTION_FAILED
;
236 encname
= encode_streamname(table
, stname
);
238 TRACE("%s -> %s\n",debugstr_w(stname
),debugstr_w(encname
));
240 r
= IStorage_OpenStream(stg
, encname
, NULL
,
241 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, &stm
);
245 WARN("open stream failed r = %08x - empty table?\n", r
);
249 r
= IStream_Stat(stm
, &stat
, STATFLAG_NONAME
);
252 WARN("open stream failed r = %08x!\n", r
);
256 if( stat
.cbSize
.QuadPart
>> 32 )
262 sz
= stat
.cbSize
.QuadPart
;
263 data
= msi_alloc( sz
);
266 WARN("couldn't allocate memory r=%08x!\n", r
);
267 ret
= ERROR_NOT_ENOUGH_MEMORY
;
271 r
= IStream_Read(stm
, data
, sz
, &count
);
272 if( FAILED( r
) || ( count
!= sz
) )
275 WARN("read stream failed r = %08x!\n", r
);
284 IStream_Release( stm
);
289 UINT
write_stream_data( IStorage
*stg
, LPCWSTR stname
,
290 LPCVOID data
, UINT sz
, BOOL bTable
)
293 UINT ret
= ERROR_FUNCTION_FAILED
;
300 encname
= encode_streamname(bTable
, stname
);
301 r
= IStorage_OpenStream( stg
, encname
, NULL
,
302 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, &stm
);
305 r
= IStorage_CreateStream( stg
, encname
,
306 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, 0, &stm
);
311 WARN("open stream failed r = %08x\n", r
);
316 r
= IStream_SetSize( stm
, size
);
319 WARN("Failed to SetSize\n");
324 r
= IStream_Seek( stm
, pos
, STREAM_SEEK_SET
, NULL
);
327 WARN("Failed to Seek\n");
333 r
= IStream_Write(stm
, data
, sz
, &count
);
334 if( FAILED( r
) || ( count
!= sz
) )
336 WARN("Failed to Write\n");
344 IStream_Release( stm
);
349 static void msi_free_colinfo( MSICOLUMNINFO
*colinfo
, UINT count
)
352 for (i
= 0; i
< count
; i
++) msi_free( colinfo
[i
].hash_table
);
355 static void free_table( MSITABLE
*table
)
358 for( i
=0; i
<table
->row_count
; i
++ )
359 msi_free( table
->data
[i
] );
360 msi_free( table
->data
);
361 msi_free( table
->data_persistent
);
362 msi_free_colinfo( table
->colinfo
, table
->col_count
);
363 msi_free( table
->colinfo
);
367 static UINT
msi_table_get_row_size( MSIDATABASE
*db
, const MSICOLUMNINFO
*cols
, UINT count
, UINT bytes_per_strref
)
369 const MSICOLUMNINFO
*last_col
;
374 if (bytes_per_strref
!= LONG_STR_BYTES
)
377 for (i
= 0; i
< count
; i
++) size
+= bytes_per_column( db
, &cols
[i
], bytes_per_strref
);
380 last_col
= &cols
[count
- 1];
381 return last_col
->offset
+ bytes_per_column( db
, last_col
, bytes_per_strref
);
384 /* add this table to the list of cached tables in the database */
385 static UINT
read_table_from_storage( MSIDATABASE
*db
, MSITABLE
*t
, IStorage
*stg
)
387 BYTE
*rawdata
= NULL
;
388 UINT rawsize
= 0, i
, j
, row_size
, row_size_mem
;
390 TRACE("%s\n",debugstr_w(t
->name
));
392 row_size
= msi_table_get_row_size( db
, t
->colinfo
, t
->col_count
, db
->bytes_per_strref
);
393 row_size_mem
= msi_table_get_row_size( db
, t
->colinfo
, t
->col_count
, LONG_STR_BYTES
);
395 /* if we can't read the table, just assume that it's empty */
396 read_stream_data( stg
, t
->name
, TRUE
, &rawdata
, &rawsize
);
398 return ERROR_SUCCESS
;
400 TRACE("Read %d bytes\n", rawsize
);
402 if( rawsize
% row_size
)
404 WARN("Table size is invalid %d/%d\n", rawsize
, row_size
);
408 t
->row_count
= rawsize
/ row_size
;
409 t
->data
= msi_alloc_zero( t
->row_count
* sizeof (USHORT
*) );
412 t
->data_persistent
= msi_alloc_zero( t
->row_count
* sizeof(BOOL
));
413 if ( !t
->data_persistent
)
416 /* transpose all the data */
417 TRACE("Transposing data from %d rows\n", t
->row_count
);
418 for (i
= 0; i
< t
->row_count
; i
++)
420 UINT ofs
= 0, ofs_mem
= 0;
422 t
->data
[i
] = msi_alloc( row_size_mem
);
425 t
->data_persistent
[i
] = TRUE
;
427 for (j
= 0; j
< t
->col_count
; j
++)
429 UINT m
= bytes_per_column( db
, &t
->colinfo
[j
], LONG_STR_BYTES
);
430 UINT n
= bytes_per_column( db
, &t
->colinfo
[j
], db
->bytes_per_strref
);
433 if ( n
!= 2 && n
!= 3 && n
!= 4 )
435 ERR("oops - unknown column width %d\n", n
);
438 if (t
->colinfo
[j
].type
& MSITYPE_STRING
&& n
< m
)
440 for (k
= 0; k
< m
; k
++)
443 t
->data
[i
][ofs_mem
+ k
] = rawdata
[ofs
* t
->row_count
+ i
* n
+ k
];
445 t
->data
[i
][ofs_mem
+ k
] = 0;
450 for (k
= 0; k
< n
; k
++)
451 t
->data
[i
][ofs_mem
+ k
] = rawdata
[ofs
* t
->row_count
+ i
* n
+ k
];
459 return ERROR_SUCCESS
;
462 return ERROR_FUNCTION_FAILED
;
465 void free_cached_tables( MSIDATABASE
*db
)
467 while( !list_empty( &db
->tables
) )
469 MSITABLE
*t
= LIST_ENTRY( list_head( &db
->tables
), MSITABLE
, entry
);
471 list_remove( &t
->entry
);
476 static MSITABLE
*find_cached_table( MSIDATABASE
*db
, LPCWSTR name
)
480 LIST_FOR_EACH_ENTRY( t
, &db
->tables
, MSITABLE
, entry
)
481 if( !strcmpW( name
, t
->name
) )
487 static void table_calc_column_offsets( MSIDATABASE
*db
, MSICOLUMNINFO
*colinfo
, DWORD count
)
491 for (i
= 0; colinfo
&& i
< count
; i
++)
493 assert( i
+ 1 == colinfo
[i
].number
);
494 if (i
) colinfo
[i
].offset
= colinfo
[i
- 1].offset
+
495 bytes_per_column( db
, &colinfo
[i
- 1], LONG_STR_BYTES
);
496 else colinfo
[i
].offset
= 0;
498 TRACE("column %d is [%s] with type %08x ofs %d\n",
499 colinfo
[i
].number
, debugstr_w(colinfo
[i
].colname
),
500 colinfo
[i
].type
, colinfo
[i
].offset
);
504 static UINT
get_defaulttablecolumns( MSIDATABASE
*db
, LPCWSTR name
, MSICOLUMNINFO
*colinfo
, UINT
*sz
)
506 const MSICOLUMNINFO
*p
;
509 TRACE("%s\n", debugstr_w(name
));
511 if (!strcmpW( name
, szTables
))
516 else if (!strcmpW( name
, szColumns
))
521 else return ERROR_FUNCTION_FAILED
;
523 for (i
= 0; i
< n
; i
++)
525 if (colinfo
&& i
< *sz
) colinfo
[i
] = p
[i
];
526 if (colinfo
&& i
>= *sz
) break;
528 table_calc_column_offsets( db
, colinfo
, n
);
530 return ERROR_SUCCESS
;
533 static UINT
get_tablecolumns( MSIDATABASE
*db
, LPCWSTR szTableName
, MSICOLUMNINFO
*colinfo
, UINT
*sz
);
535 static UINT
table_get_column_info( MSIDATABASE
*db
, LPCWSTR name
, MSICOLUMNINFO
**pcols
, UINT
*pcount
)
537 UINT r
, column_count
= 0;
538 MSICOLUMNINFO
*columns
;
540 /* get the number of columns in this table */
542 r
= get_tablecolumns( db
, name
, NULL
, &column_count
);
543 if (r
!= ERROR_SUCCESS
)
546 *pcount
= column_count
;
548 /* if there are no columns, there's no table */
550 return ERROR_INVALID_PARAMETER
;
552 TRACE("table %s found\n", debugstr_w(name
));
554 columns
= msi_alloc( column_count
* sizeof(MSICOLUMNINFO
) );
556 return ERROR_FUNCTION_FAILED
;
558 r
= get_tablecolumns( db
, name
, columns
, &column_count
);
559 if (r
!= ERROR_SUCCESS
)
562 return ERROR_FUNCTION_FAILED
;
568 static UINT
get_table( MSIDATABASE
*db
, LPCWSTR name
, MSITABLE
**table_ret
)
573 /* first, see if the table is cached */
574 table
= find_cached_table( db
, name
);
578 return ERROR_SUCCESS
;
581 /* nonexistent tables should be interpreted as empty tables */
582 table
= msi_alloc( sizeof(MSITABLE
) + lstrlenW( name
) * sizeof(WCHAR
) );
584 return ERROR_FUNCTION_FAILED
;
586 table
->row_count
= 0;
588 table
->data_persistent
= NULL
;
589 table
->colinfo
= NULL
;
590 table
->col_count
= 0;
591 table
->persistent
= MSICONDITION_TRUE
;
592 lstrcpyW( table
->name
, name
);
594 if (!strcmpW( name
, szTables
) || !strcmpW( name
, szColumns
))
595 table
->persistent
= MSICONDITION_NONE
;
597 r
= table_get_column_info( db
, name
, &table
->colinfo
, &table
->col_count
);
598 if (r
!= ERROR_SUCCESS
)
603 r
= read_table_from_storage( db
, table
, db
->storage
);
604 if (r
!= ERROR_SUCCESS
)
609 list_add_head( &db
->tables
, &table
->entry
);
611 return ERROR_SUCCESS
;
614 static UINT
read_table_int( BYTE
*const *data
, UINT row
, UINT col
, UINT bytes
)
618 for (i
= 0; i
< bytes
; i
++)
619 ret
+= data
[row
][col
+ i
] << i
* 8;
624 static UINT
get_tablecolumns( MSIDATABASE
*db
, LPCWSTR szTableName
, MSICOLUMNINFO
*colinfo
, UINT
*sz
)
626 UINT r
, i
, n
= 0, table_id
, count
, maxcount
= *sz
;
627 MSITABLE
*table
= NULL
;
629 TRACE("%s\n", debugstr_w(szTableName
));
631 /* first check if there is a default table with that name */
632 r
= get_defaulttablecolumns( db
, szTableName
, colinfo
, sz
);
633 if (r
== ERROR_SUCCESS
&& *sz
)
636 r
= get_table( db
, szColumns
, &table
);
637 if (r
!= ERROR_SUCCESS
)
639 ERR("couldn't load _Columns table\n");
640 return ERROR_FUNCTION_FAILED
;
643 /* convert table and column names to IDs from the string table */
644 r
= msi_string2id( db
->strings
, szTableName
, -1, &table_id
);
645 if (r
!= ERROR_SUCCESS
)
647 WARN("Couldn't find id for %s\n", debugstr_w(szTableName
));
650 TRACE("Table id is %d, row count is %d\n", table_id
, table
->row_count
);
652 /* Note: _Columns table doesn't have non-persistent data */
654 /* if maxcount is non-zero, assume it's exactly right for this table */
655 if (colinfo
) memset( colinfo
, 0, maxcount
* sizeof(*colinfo
) );
656 count
= table
->row_count
;
657 for (i
= 0; i
< count
; i
++)
659 if (read_table_int( table
->data
, i
, 0, LONG_STR_BYTES
) != table_id
) continue;
662 UINT id
= read_table_int( table
->data
, i
, table
->colinfo
[2].offset
, LONG_STR_BYTES
);
663 UINT col
= read_table_int( table
->data
, i
, table
->colinfo
[1].offset
, sizeof(USHORT
) ) - (1 << 15);
665 /* check the column number is in range */
666 if (col
< 1 || col
> maxcount
)
668 ERR("column %d out of range (maxcount: %d)\n", col
, maxcount
);
671 /* check if this column was already set */
672 if (colinfo
[col
- 1].number
)
674 ERR("duplicate column %d\n", col
);
677 colinfo
[col
- 1].tablename
= msi_string_lookup( db
->strings
, table_id
, NULL
);
678 colinfo
[col
- 1].number
= col
;
679 colinfo
[col
- 1].colname
= msi_string_lookup( db
->strings
, id
, NULL
);
680 colinfo
[col
- 1].type
= read_table_int( table
->data
, i
, table
->colinfo
[3].offset
,
681 sizeof(USHORT
) ) - (1 << 15);
682 colinfo
[col
- 1].offset
= 0;
683 colinfo
[col
- 1].ref_count
= 0;
684 colinfo
[col
- 1].hash_table
= NULL
;
688 TRACE("%s has %d columns\n", debugstr_w(szTableName
), n
);
690 if (colinfo
&& n
!= maxcount
)
692 ERR("missing column in table %s\n", debugstr_w(szTableName
));
693 msi_free_colinfo( colinfo
, maxcount
);
694 return ERROR_FUNCTION_FAILED
;
696 table_calc_column_offsets( db
, colinfo
, n
);
698 return ERROR_SUCCESS
;
701 UINT
msi_create_table( MSIDATABASE
*db
, LPCWSTR name
, column_info
*col_info
,
702 MSICONDITION persistent
)
704 enum StringPersistence string_persistence
= (persistent
) ? StringPersistent
: StringNonPersistent
;
707 MSIRECORD
*rec
= NULL
;
712 /* only add tables that don't exist already */
713 if( TABLE_Exists(db
, name
) )
715 WARN("table %s exists\n", debugstr_w(name
));
716 return ERROR_BAD_QUERY_SYNTAX
;
719 table
= msi_alloc( sizeof (MSITABLE
) + lstrlenW(name
)*sizeof (WCHAR
) );
721 return ERROR_FUNCTION_FAILED
;
723 table
->ref_count
= 1;
724 table
->row_count
= 0;
726 table
->data_persistent
= NULL
;
727 table
->colinfo
= NULL
;
728 table
->col_count
= 0;
729 table
->persistent
= persistent
;
730 lstrcpyW( table
->name
, name
);
732 for( col
= col_info
; col
; col
= col
->next
)
735 table
->colinfo
= msi_alloc( table
->col_count
* sizeof(MSICOLUMNINFO
) );
739 return ERROR_FUNCTION_FAILED
;
742 for( i
= 0, col
= col_info
; col
; i
++, col
= col
->next
)
744 UINT table_id
= msi_add_string( db
->strings
, col
->table
, -1, string_persistence
);
745 UINT col_id
= msi_add_string( db
->strings
, col
->column
, -1, string_persistence
);
747 table
->colinfo
[ i
].tablename
= msi_string_lookup( db
->strings
, table_id
, NULL
);
748 table
->colinfo
[ i
].number
= i
+ 1;
749 table
->colinfo
[ i
].colname
= msi_string_lookup( db
->strings
, col_id
, NULL
);
750 table
->colinfo
[ i
].type
= col
->type
;
751 table
->colinfo
[ i
].offset
= 0;
752 table
->colinfo
[ i
].ref_count
= 0;
753 table
->colinfo
[ i
].hash_table
= NULL
;
754 table
->colinfo
[ i
].temporary
= col
->temporary
;
756 table_calc_column_offsets( db
, table
->colinfo
, table
->col_count
);
758 r
= TABLE_CreateView( db
, szTables
, &tv
);
759 TRACE("CreateView returned %x\n", r
);
766 r
= tv
->ops
->execute( tv
, 0 );
767 TRACE("tv execute returned %x\n", r
);
771 rec
= MSI_CreateRecord( 1 );
775 r
= MSI_RecordSetStringW( rec
, 1, name
);
779 r
= tv
->ops
->insert_row( tv
, rec
, -1, persistent
== MSICONDITION_FALSE
);
780 TRACE("insert_row returned %x\n", r
);
784 tv
->ops
->delete( tv
);
787 msiobj_release( &rec
->hdr
);
790 if( persistent
!= MSICONDITION_FALSE
)
792 /* add each column to the _Columns table */
793 r
= TABLE_CreateView( db
, szColumns
, &tv
);
797 r
= tv
->ops
->execute( tv
, 0 );
798 TRACE("tv execute returned %x\n", r
);
802 rec
= MSI_CreateRecord( 4 );
806 r
= MSI_RecordSetStringW( rec
, 1, name
);
811 * need to set the table, column number, col name and type
812 * for each column we enter in the table
815 for( col
= col_info
; col
; col
= col
->next
)
817 r
= MSI_RecordSetInteger( rec
, 2, nField
);
821 r
= MSI_RecordSetStringW( rec
, 3, col
->column
);
825 r
= MSI_RecordSetInteger( rec
, 4, col
->type
);
829 r
= tv
->ops
->insert_row( tv
, rec
, -1, FALSE
);
841 msiobj_release( &rec
->hdr
);
842 /* FIXME: remove values from the string table on error */
844 tv
->ops
->delete( tv
);
846 if (r
== ERROR_SUCCESS
)
847 list_add_head( &db
->tables
, &table
->entry
);
854 static UINT
save_table( MSIDATABASE
*db
, const MSITABLE
*t
, UINT bytes_per_strref
)
856 BYTE
*rawdata
= NULL
;
857 UINT rawsize
, i
, j
, row_size
, row_count
;
858 UINT r
= ERROR_FUNCTION_FAILED
;
860 /* Nothing to do for non-persistent tables */
861 if( t
->persistent
== MSICONDITION_FALSE
)
862 return ERROR_SUCCESS
;
864 TRACE("Saving %s\n", debugstr_w( t
->name
) );
866 row_size
= msi_table_get_row_size( db
, t
->colinfo
, t
->col_count
, bytes_per_strref
);
867 row_count
= t
->row_count
;
868 for (i
= 0; i
< t
->row_count
; i
++)
870 if (!t
->data_persistent
[i
])
872 row_count
= 1; /* yes, this is bizarre */
876 rawsize
= row_count
* row_size
;
877 rawdata
= msi_alloc_zero( rawsize
);
880 r
= ERROR_NOT_ENOUGH_MEMORY
;
885 for (i
= 0; i
< row_count
; i
++)
887 UINT ofs
= 0, ofs_mem
= 0;
889 if (!t
->data_persistent
[i
]) break;
891 for (j
= 0; j
< t
->col_count
; j
++)
893 UINT m
= bytes_per_column( db
, &t
->colinfo
[j
], LONG_STR_BYTES
);
894 UINT n
= bytes_per_column( db
, &t
->colinfo
[j
], bytes_per_strref
);
897 if (n
!= 2 && n
!= 3 && n
!= 4)
899 ERR("oops - unknown column width %d\n", n
);
902 if (t
->colinfo
[j
].type
& MSITYPE_STRING
&& n
< m
)
904 UINT id
= read_table_int( t
->data
, i
, ofs_mem
, LONG_STR_BYTES
);
905 if (id
> 1 << bytes_per_strref
* 8)
907 ERR("string id %u out of range\n", id
);
911 for (k
= 0; k
< n
; k
++)
913 rawdata
[ofs
* row_count
+ i
* n
+ k
] = t
->data
[i
][ofs_mem
+ k
];
921 TRACE("writing %d bytes\n", rawsize
);
922 r
= write_stream_data( db
->storage
, t
->name
, rawdata
, rawsize
, TRUE
);
929 static void msi_update_table_columns( MSIDATABASE
*db
, LPCWSTR name
)
932 UINT size
, offset
, old_count
;
935 if (!(table
= find_cached_table( db
, name
))) return;
936 old_count
= table
->col_count
;
937 msi_free_colinfo( table
->colinfo
, table
->col_count
);
938 msi_free( table
->colinfo
);
939 table
->colinfo
= NULL
;
941 table_get_column_info( db
, name
, &table
->colinfo
, &table
->col_count
);
942 if (!table
->col_count
) return;
944 size
= msi_table_get_row_size( db
, table
->colinfo
, table
->col_count
, LONG_STR_BYTES
);
945 offset
= table
->colinfo
[table
->col_count
- 1].offset
;
947 for ( n
= 0; n
< table
->row_count
; n
++ )
949 table
->data
[n
] = msi_realloc( table
->data
[n
], size
);
950 if (old_count
< table
->col_count
)
951 memset( &table
->data
[n
][offset
], 0, size
- offset
);
955 /* try to find the table name in the _Tables table */
956 BOOL
TABLE_Exists( MSIDATABASE
*db
, LPCWSTR name
)
961 if( !strcmpW( name
, szTables
) || !strcmpW( name
, szColumns
) ||
962 !strcmpW( name
, szStreams
) || !strcmpW( name
, szStorages
) )
965 r
= msi_string2id( db
->strings
, name
, -1, &table_id
);
966 if( r
!= ERROR_SUCCESS
)
968 TRACE("Couldn't find id for %s\n", debugstr_w(name
));
972 r
= get_table( db
, szTables
, &table
);
973 if( r
!= ERROR_SUCCESS
)
975 ERR("table %s not available\n", debugstr_w(szTables
));
979 for( i
= 0; i
< table
->row_count
; i
++ )
981 if( read_table_int( table
->data
, i
, 0, LONG_STR_BYTES
) == table_id
)
988 /* below is the query interface to a table */
990 typedef struct tagMSITABLEVIEW
995 MSICOLUMNINFO
*columns
;
1001 static UINT
TABLE_fetch_int( struct tagMSIVIEW
*view
, UINT row
, UINT col
, UINT
*val
)
1003 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1007 return ERROR_INVALID_PARAMETER
;
1009 if( (col
==0) || (col
>tv
->num_cols
) )
1010 return ERROR_INVALID_PARAMETER
;
1012 /* how many rows are there ? */
1013 if( row
>= tv
->table
->row_count
)
1014 return ERROR_NO_MORE_ITEMS
;
1016 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1018 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1019 ERR("%p %p\n", tv
, tv
->columns
);
1020 return ERROR_FUNCTION_FAILED
;
1023 n
= bytes_per_column( tv
->db
, &tv
->columns
[col
- 1], LONG_STR_BYTES
);
1024 if (n
!= 2 && n
!= 3 && n
!= 4)
1026 ERR("oops! what is %d bytes per column?\n", n
);
1027 return ERROR_FUNCTION_FAILED
;
1030 offset
= tv
->columns
[col
-1].offset
;
1031 *val
= read_table_int(tv
->table
->data
, row
, offset
, n
);
1033 /* TRACE("Data [%d][%d] = %d\n", row, col, *val ); */
1035 return ERROR_SUCCESS
;
1038 static UINT
get_stream_name( const MSITABLEVIEW
*tv
, UINT row
, WCHAR
**pstname
)
1040 LPWSTR p
, stname
= NULL
;
1041 UINT i
, r
, type
, ival
;
1044 MSIVIEW
*view
= (MSIVIEW
*) tv
;
1046 TRACE("%p %d\n", tv
, row
);
1048 len
= lstrlenW( tv
->name
) + 1;
1049 stname
= msi_alloc( len
*sizeof(WCHAR
) );
1052 r
= ERROR_OUTOFMEMORY
;
1056 lstrcpyW( stname
, tv
->name
);
1058 for ( i
= 0; i
< tv
->num_cols
; i
++ )
1060 type
= tv
->columns
[i
].type
;
1061 if ( type
& MSITYPE_KEY
)
1065 r
= TABLE_fetch_int( view
, row
, i
+1, &ival
);
1066 if ( r
!= ERROR_SUCCESS
)
1069 if ( tv
->columns
[i
].type
& MSITYPE_STRING
)
1071 sval
= msi_string_lookup( tv
->db
->strings
, ival
, NULL
);
1074 r
= ERROR_INVALID_PARAMETER
;
1080 static const WCHAR fmt
[] = { '%','d',0 };
1081 UINT n
= bytes_per_column( tv
->db
, &tv
->columns
[i
], LONG_STR_BYTES
);
1086 sprintfW( number
, fmt
, ival
-0x8000 );
1089 sprintfW( number
, fmt
, ival
^0x80000000 );
1092 ERR( "oops - unknown column width %d\n", n
);
1093 r
= ERROR_FUNCTION_FAILED
;
1099 len
+= lstrlenW( szDot
) + lstrlenW( sval
);
1100 p
= msi_realloc ( stname
, len
*sizeof(WCHAR
) );
1103 r
= ERROR_OUTOFMEMORY
;
1108 lstrcatW( stname
, szDot
);
1109 lstrcatW( stname
, sval
);
1116 return ERROR_SUCCESS
;
1125 * We need a special case for streams, as we need to reference column with
1126 * the name of the stream in the same table, and the table name
1127 * which may not be available at higher levels of the query
1129 static UINT
TABLE_fetch_stream( struct tagMSIVIEW
*view
, UINT row
, UINT col
, IStream
**stm
)
1131 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1135 if( !view
->ops
->fetch_int
)
1136 return ERROR_INVALID_PARAMETER
;
1138 r
= get_stream_name( tv
, row
, &name
);
1139 if (r
!= ERROR_SUCCESS
)
1141 ERR("fetching stream, error = %u\n", r
);
1145 r
= msi_get_stream( tv
->db
, name
, stm
);
1146 if (r
!= ERROR_SUCCESS
)
1147 ERR("fetching stream %s, error = %u\n", debugstr_w(name
), r
);
1153 static UINT
TABLE_set_int( MSITABLEVIEW
*tv
, UINT row
, UINT col
, UINT val
)
1158 return ERROR_INVALID_PARAMETER
;
1160 if( (col
==0) || (col
>tv
->num_cols
) )
1161 return ERROR_INVALID_PARAMETER
;
1163 if( row
>= tv
->table
->row_count
)
1164 return ERROR_INVALID_PARAMETER
;
1166 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1168 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1169 ERR("%p %p\n", tv
, tv
->columns
);
1170 return ERROR_FUNCTION_FAILED
;
1173 msi_free( tv
->columns
[col
-1].hash_table
);
1174 tv
->columns
[col
-1].hash_table
= NULL
;
1176 n
= bytes_per_column( tv
->db
, &tv
->columns
[col
- 1], LONG_STR_BYTES
);
1177 if ( n
!= 2 && n
!= 3 && n
!= 4 )
1179 ERR("oops! what is %d bytes per column?\n", n
);
1180 return ERROR_FUNCTION_FAILED
;
1183 offset
= tv
->columns
[col
-1].offset
;
1184 for ( i
= 0; i
< n
; i
++ )
1185 tv
->table
->data
[row
][offset
+ i
] = (val
>> i
* 8) & 0xff;
1187 return ERROR_SUCCESS
;
1190 static UINT
TABLE_get_row( struct tagMSIVIEW
*view
, UINT row
, MSIRECORD
**rec
)
1192 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1195 return ERROR_INVALID_PARAMETER
;
1197 return msi_view_get_row(tv
->db
, view
, row
, rec
);
1200 static UINT
add_stream( MSIDATABASE
*db
, const WCHAR
*name
, IStream
*data
)
1202 static const WCHAR insert
[] = {
1203 'I','N','S','E','R','T',' ','I','N','T','O',' ',
1204 '`','_','S','t','r','e','a','m','s','`',' ',
1205 '(','`','N','a','m','e','`',',','`','D','a','t','a','`',')',' ',
1206 'V','A','L','U','E','S',' ','(','?',',','?',')',0};
1207 static const WCHAR update
[] = {
1208 'U','P','D','A','T','E',' ','`','_','S','t','r','e','a','m','s','`',' ',
1209 'S','E','T',' ','`','D','a','t','a','`',' ','=',' ','?',' ',
1210 'W','H','E','R','E',' ','`','N','a','m','e','`',' ','=',' ','?',0};
1215 TRACE("%p %s %p\n", db
, debugstr_w(name
), data
);
1217 if (!(rec
= MSI_CreateRecord( 2 )))
1218 return ERROR_OUTOFMEMORY
;
1220 r
= MSI_RecordSetStringW( rec
, 1, name
);
1221 if (r
!= ERROR_SUCCESS
)
1224 r
= MSI_RecordSetIStream( rec
, 2, data
);
1225 if (r
!= ERROR_SUCCESS
)
1228 r
= MSI_DatabaseOpenViewW( db
, insert
, &query
);
1229 if (r
!= ERROR_SUCCESS
)
1232 r
= MSI_ViewExecute( query
, rec
);
1233 msiobj_release( &query
->hdr
);
1234 if (r
== ERROR_SUCCESS
)
1237 msiobj_release( &rec
->hdr
);
1238 if (!(rec
= MSI_CreateRecord( 2 )))
1239 return ERROR_OUTOFMEMORY
;
1241 r
= MSI_RecordSetIStream( rec
, 1, data
);
1242 if (r
!= ERROR_SUCCESS
)
1245 r
= MSI_RecordSetStringW( rec
, 2, name
);
1246 if (r
!= ERROR_SUCCESS
)
1249 r
= MSI_DatabaseOpenViewW( db
, update
, &query
);
1250 if (r
!= ERROR_SUCCESS
)
1253 r
= MSI_ViewExecute( query
, rec
);
1254 msiobj_release( &query
->hdr
);
1257 msiobj_release( &rec
->hdr
);
1261 static UINT
get_table_value_from_record( MSITABLEVIEW
*tv
, MSIRECORD
*rec
, UINT iField
, UINT
*pvalue
)
1263 MSICOLUMNINFO columninfo
;
1267 if ( (iField
<= 0) ||
1268 (iField
> tv
->num_cols
) ||
1269 MSI_RecordIsNull( rec
, iField
) )
1270 return ERROR_FUNCTION_FAILED
;
1272 columninfo
= tv
->columns
[ iField
- 1 ];
1274 if ( MSITYPE_IS_BINARY(columninfo
.type
) )
1276 *pvalue
= 1; /* refers to the first key column */
1278 else if ( columninfo
.type
& MSITYPE_STRING
)
1281 const WCHAR
*sval
= msi_record_get_string( rec
, iField
, &len
);
1284 r
= msi_string2id( tv
->db
->strings
, sval
, len
, pvalue
);
1285 if (r
!= ERROR_SUCCESS
)
1286 return ERROR_NOT_FOUND
;
1290 else if ( bytes_per_column( tv
->db
, &columninfo
, LONG_STR_BYTES
) == 2 )
1292 ival
= MSI_RecordGetInteger( rec
, iField
);
1293 if (ival
== 0x80000000) *pvalue
= 0x8000;
1296 *pvalue
= 0x8000 + MSI_RecordGetInteger( rec
, iField
);
1297 if (*pvalue
& 0xffff0000)
1299 ERR("field %u value %d out of range\n", iField
, *pvalue
- 0x8000);
1300 return ERROR_FUNCTION_FAILED
;
1306 ival
= MSI_RecordGetInteger( rec
, iField
);
1307 *pvalue
= ival
^ 0x80000000;
1310 return ERROR_SUCCESS
;
1313 static UINT
TABLE_set_row( struct tagMSIVIEW
*view
, UINT row
, MSIRECORD
*rec
, UINT mask
)
1315 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1316 UINT i
, val
, r
= ERROR_SUCCESS
;
1319 return ERROR_INVALID_PARAMETER
;
1321 /* test if any of the mask bits are invalid */
1322 if ( mask
>= (1<<tv
->num_cols
) )
1323 return ERROR_INVALID_PARAMETER
;
1325 for ( i
= 0; i
< tv
->num_cols
; i
++ )
1329 /* only update the fields specified in the mask */
1330 if ( !(mask
&(1<<i
)) )
1333 persistent
= (tv
->table
->persistent
!= MSICONDITION_FALSE
) &&
1334 (tv
->table
->data_persistent
[row
]);
1335 /* FIXME: should we allow updating keys? */
1338 if ( !MSI_RecordIsNull( rec
, i
+ 1 ) )
1340 r
= get_table_value_from_record (tv
, rec
, i
+ 1, &val
);
1341 if ( MSITYPE_IS_BINARY(tv
->columns
[ i
].type
) )
1346 if ( r
!= ERROR_SUCCESS
)
1347 return ERROR_FUNCTION_FAILED
;
1349 r
= MSI_RecordGetIStream( rec
, i
+ 1, &stm
);
1350 if ( r
!= ERROR_SUCCESS
)
1353 r
= get_stream_name( tv
, row
, &stname
);
1354 if ( r
!= ERROR_SUCCESS
)
1356 IStream_Release( stm
);
1360 r
= add_stream( tv
->db
, stname
, stm
);
1361 IStream_Release( stm
);
1362 msi_free ( stname
);
1364 if ( r
!= ERROR_SUCCESS
)
1367 else if ( tv
->columns
[i
].type
& MSITYPE_STRING
)
1371 if ( r
!= ERROR_SUCCESS
)
1374 const WCHAR
*sval
= msi_record_get_string( rec
, i
+ 1, &len
);
1375 val
= msi_add_string( tv
->db
->strings
, sval
, len
,
1376 persistent
? StringPersistent
: StringNonPersistent
);
1380 TABLE_fetch_int(&tv
->view
, row
, i
+ 1, &x
);
1387 if ( r
!= ERROR_SUCCESS
)
1388 return ERROR_FUNCTION_FAILED
;
1392 r
= TABLE_set_int( tv
, row
, i
+1, val
);
1393 if ( r
!= ERROR_SUCCESS
)
1399 static UINT
table_create_new_row( struct tagMSIVIEW
*view
, UINT
*num
, BOOL temporary
)
1401 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1406 BOOL
**data_persist_ptr
;
1409 TRACE("%p %s\n", view
, temporary
? "TRUE" : "FALSE");
1412 return ERROR_INVALID_PARAMETER
;
1414 row
= msi_alloc_zero( tv
->row_size
);
1416 return ERROR_NOT_ENOUGH_MEMORY
;
1418 row_count
= &tv
->table
->row_count
;
1419 data_ptr
= &tv
->table
->data
;
1420 data_persist_ptr
= &tv
->table
->data_persistent
;
1422 *num
= tv
->table
->row_count
;
1424 sz
= (*row_count
+ 1) * sizeof (BYTE
*);
1426 p
= msi_realloc( *data_ptr
, sz
);
1428 p
= msi_alloc( sz
);
1432 return ERROR_NOT_ENOUGH_MEMORY
;
1435 sz
= (*row_count
+ 1) * sizeof (BOOL
);
1436 if( *data_persist_ptr
)
1437 b
= msi_realloc( *data_persist_ptr
, sz
);
1439 b
= msi_alloc( sz
);
1444 return ERROR_NOT_ENOUGH_MEMORY
;
1448 (*data_ptr
)[*row_count
] = row
;
1450 *data_persist_ptr
= b
;
1451 (*data_persist_ptr
)[*row_count
] = !temporary
;
1455 return ERROR_SUCCESS
;
1458 static UINT
TABLE_execute( struct tagMSIVIEW
*view
, MSIRECORD
*record
)
1460 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1462 TRACE("%p %p\n", tv
, record
);
1464 TRACE("There are %d columns\n", tv
->num_cols
);
1466 return ERROR_SUCCESS
;
1469 static UINT
TABLE_close( struct tagMSIVIEW
*view
)
1471 TRACE("%p\n", view
);
1473 return ERROR_SUCCESS
;
1476 static UINT
TABLE_get_dimensions( struct tagMSIVIEW
*view
, UINT
*rows
, UINT
*cols
)
1478 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1480 TRACE("%p %p %p\n", view
, rows
, cols
);
1483 *cols
= tv
->num_cols
;
1487 return ERROR_INVALID_PARAMETER
;
1488 *rows
= tv
->table
->row_count
;
1491 return ERROR_SUCCESS
;
1494 static UINT
TABLE_get_column_info( struct tagMSIVIEW
*view
,
1495 UINT n
, LPCWSTR
*name
, UINT
*type
, BOOL
*temporary
,
1496 LPCWSTR
*table_name
)
1498 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1500 TRACE("%p %d %p %p\n", tv
, n
, name
, type
);
1502 if( ( n
== 0 ) || ( n
> tv
->num_cols
) )
1503 return ERROR_INVALID_PARAMETER
;
1507 *name
= tv
->columns
[n
-1].colname
;
1509 return ERROR_FUNCTION_FAILED
;
1514 *table_name
= tv
->columns
[n
-1].tablename
;
1516 return ERROR_FUNCTION_FAILED
;
1520 *type
= tv
->columns
[n
-1].type
;
1523 *temporary
= tv
->columns
[n
-1].temporary
;
1525 return ERROR_SUCCESS
;
1528 static UINT
msi_table_find_row( MSITABLEVIEW
*tv
, MSIRECORD
*rec
, UINT
*row
, UINT
*column
);
1530 static UINT
table_validate_new( MSITABLEVIEW
*tv
, MSIRECORD
*rec
, UINT
*column
)
1534 /* check there are no null values where they're not allowed */
1535 for( i
= 0; i
< tv
->num_cols
; i
++ )
1537 if ( tv
->columns
[i
].type
& MSITYPE_NULLABLE
)
1540 if ( MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
1541 TRACE("skipping binary column\n");
1542 else if ( tv
->columns
[i
].type
& MSITYPE_STRING
)
1545 const WCHAR
*str
= msi_record_get_string( rec
, i
+1, &len
);
1547 if (!str
|| (!str
[0] && !len
))
1549 if (column
) *column
= i
;
1550 return ERROR_INVALID_DATA
;
1557 n
= MSI_RecordGetInteger( rec
, i
+1 );
1558 if (n
== MSI_NULL_INTEGER
)
1560 if (column
) *column
= i
;
1561 return ERROR_INVALID_DATA
;
1566 /* check there are no duplicate keys */
1567 r
= msi_table_find_row( tv
, rec
, &row
, column
);
1568 if (r
== ERROR_SUCCESS
)
1569 return ERROR_FUNCTION_FAILED
;
1571 return ERROR_SUCCESS
;
1574 static int compare_record( MSITABLEVIEW
*tv
, UINT row
, MSIRECORD
*rec
)
1576 UINT r
, i
, ivalue
, x
;
1578 for (i
= 0; i
< tv
->num_cols
; i
++ )
1580 if (!(tv
->columns
[i
].type
& MSITYPE_KEY
)) continue;
1582 r
= get_table_value_from_record( tv
, rec
, i
+ 1, &ivalue
);
1583 if (r
!= ERROR_SUCCESS
)
1586 r
= TABLE_fetch_int( &tv
->view
, row
, i
+ 1, &x
);
1587 if (r
!= ERROR_SUCCESS
)
1589 WARN("TABLE_fetch_int should not fail here %u\n", r
);
1596 else if (ivalue
== x
)
1598 if (i
< tv
->num_cols
- 1) continue;
1607 static int find_insert_index( MSITABLEVIEW
*tv
, MSIRECORD
*rec
)
1609 int idx
, c
, low
= 0, high
= tv
->table
->row_count
- 1;
1611 TRACE("%p %p\n", tv
, rec
);
1615 idx
= (low
+ high
) / 2;
1616 c
= compare_record( tv
, idx
, rec
);
1624 TRACE("found %u\n", idx
);
1628 TRACE("found %u\n", high
+ 1);
1632 static UINT
TABLE_insert_row( struct tagMSIVIEW
*view
, MSIRECORD
*rec
, UINT row
, BOOL temporary
)
1634 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1637 TRACE("%p %p %s\n", tv
, rec
, temporary
? "TRUE" : "FALSE" );
1639 /* check that the key is unique - can we find a matching row? */
1640 r
= table_validate_new( tv
, rec
, NULL
);
1641 if( r
!= ERROR_SUCCESS
)
1642 return ERROR_FUNCTION_FAILED
;
1645 row
= find_insert_index( tv
, rec
);
1647 r
= table_create_new_row( view
, &row
, temporary
);
1648 TRACE("insert_row returned %08x\n", r
);
1649 if( r
!= ERROR_SUCCESS
)
1652 /* shift the rows to make room for the new row */
1653 for (i
= tv
->table
->row_count
- 1; i
> row
; i
--)
1655 memmove(&(tv
->table
->data
[i
][0]),
1656 &(tv
->table
->data
[i
- 1][0]), tv
->row_size
);
1657 tv
->table
->data_persistent
[i
] = tv
->table
->data_persistent
[i
- 1];
1660 /* Re-set the persistence flag */
1661 tv
->table
->data_persistent
[row
] = !temporary
;
1662 return TABLE_set_row( view
, row
, rec
, (1<<tv
->num_cols
) - 1 );
1665 static UINT
TABLE_delete_row( struct tagMSIVIEW
*view
, UINT row
)
1667 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1668 UINT r
, num_rows
, num_cols
, i
;
1670 TRACE("%p %d\n", tv
, row
);
1673 return ERROR_INVALID_PARAMETER
;
1675 r
= TABLE_get_dimensions( view
, &num_rows
, &num_cols
);
1676 if ( r
!= ERROR_SUCCESS
)
1679 if ( row
>= num_rows
)
1680 return ERROR_FUNCTION_FAILED
;
1682 num_rows
= tv
->table
->row_count
;
1683 tv
->table
->row_count
--;
1685 /* reset the hash tables */
1686 for (i
= 0; i
< tv
->num_cols
; i
++)
1688 msi_free( tv
->columns
[i
].hash_table
);
1689 tv
->columns
[i
].hash_table
= NULL
;
1692 for (i
= row
+ 1; i
< num_rows
; i
++)
1694 memcpy(tv
->table
->data
[i
- 1], tv
->table
->data
[i
], tv
->row_size
);
1695 tv
->table
->data_persistent
[i
- 1] = tv
->table
->data_persistent
[i
];
1698 msi_free(tv
->table
->data
[num_rows
- 1]);
1700 return ERROR_SUCCESS
;
1703 static UINT
msi_table_update(struct tagMSIVIEW
*view
, MSIRECORD
*rec
, UINT row
)
1705 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1708 /* FIXME: MsiViewFetch should set rec index 0 to some ID that
1709 * sets the fetched record apart from other records
1713 return ERROR_INVALID_PARAMETER
;
1715 r
= msi_table_find_row(tv
, rec
, &new_row
, NULL
);
1716 if (r
!= ERROR_SUCCESS
)
1718 ERR("can't find row to modify\n");
1719 return ERROR_FUNCTION_FAILED
;
1722 /* the row cannot be changed */
1723 if (row
!= new_row
+ 1)
1724 return ERROR_FUNCTION_FAILED
;
1726 return TABLE_set_row(view
, new_row
, rec
, (1 << tv
->num_cols
) - 1);
1729 static UINT
msi_table_assign(struct tagMSIVIEW
*view
, MSIRECORD
*rec
)
1731 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1735 return ERROR_INVALID_PARAMETER
;
1737 r
= msi_table_find_row(tv
, rec
, &row
, NULL
);
1738 if (r
== ERROR_SUCCESS
)
1739 return TABLE_set_row(view
, row
, rec
, (1 << tv
->num_cols
) - 1);
1741 return TABLE_insert_row( view
, rec
, -1, FALSE
);
1744 static UINT
modify_delete_row( struct tagMSIVIEW
*view
, MSIRECORD
*rec
)
1746 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1749 r
= msi_table_find_row(tv
, rec
, &row
, NULL
);
1750 if (r
!= ERROR_SUCCESS
)
1753 return TABLE_delete_row(view
, row
);
1756 static UINT
msi_refresh_record( struct tagMSIVIEW
*view
, MSIRECORD
*rec
, UINT row
)
1761 r
= TABLE_get_row(view
, row
- 1, &curr
);
1762 if (r
!= ERROR_SUCCESS
)
1765 /* Close the original record */
1766 MSI_CloseRecord(&rec
->hdr
);
1768 count
= MSI_RecordGetFieldCount(rec
);
1769 for (i
= 0; i
< count
; i
++)
1770 MSI_RecordCopyField(curr
, i
+ 1, rec
, i
+ 1);
1772 msiobj_release(&curr
->hdr
);
1773 return ERROR_SUCCESS
;
1776 static UINT
TABLE_modify( struct tagMSIVIEW
*view
, MSIMODIFY eModifyMode
,
1777 MSIRECORD
*rec
, UINT row
)
1779 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1780 UINT r
, frow
, column
;
1782 TRACE("%p %d %p\n", view
, eModifyMode
, rec
);
1784 switch (eModifyMode
)
1786 case MSIMODIFY_DELETE
:
1787 r
= modify_delete_row( view
, rec
);
1789 case MSIMODIFY_VALIDATE_NEW
:
1790 r
= table_validate_new( tv
, rec
, &column
);
1791 if (r
!= ERROR_SUCCESS
)
1793 tv
->view
.error
= MSIDBERROR_DUPLICATEKEY
;
1794 tv
->view
.error_column
= tv
->columns
[column
].colname
;
1795 r
= ERROR_INVALID_DATA
;
1799 case MSIMODIFY_INSERT
:
1800 r
= table_validate_new( tv
, rec
, NULL
);
1801 if (r
!= ERROR_SUCCESS
)
1803 r
= TABLE_insert_row( view
, rec
, -1, FALSE
);
1806 case MSIMODIFY_INSERT_TEMPORARY
:
1807 r
= table_validate_new( tv
, rec
, NULL
);
1808 if (r
!= ERROR_SUCCESS
)
1810 r
= TABLE_insert_row( view
, rec
, -1, TRUE
);
1813 case MSIMODIFY_REFRESH
:
1814 r
= msi_refresh_record( view
, rec
, row
);
1817 case MSIMODIFY_UPDATE
:
1818 r
= msi_table_update( view
, rec
, row
);
1821 case MSIMODIFY_ASSIGN
:
1822 r
= msi_table_assign( view
, rec
);
1825 case MSIMODIFY_MERGE
:
1826 /* check row that matches this record */
1827 r
= msi_table_find_row( tv
, rec
, &frow
, &column
);
1828 if (r
!= ERROR_SUCCESS
)
1830 r
= table_validate_new( tv
, rec
, NULL
);
1831 if (r
== ERROR_SUCCESS
)
1832 r
= TABLE_insert_row( view
, rec
, -1, FALSE
);
1836 case MSIMODIFY_REPLACE
:
1837 case MSIMODIFY_VALIDATE
:
1838 case MSIMODIFY_VALIDATE_FIELD
:
1839 case MSIMODIFY_VALIDATE_DELETE
:
1840 FIXME("%p %d %p - mode not implemented\n", view
, eModifyMode
, rec
);
1841 r
= ERROR_CALL_NOT_IMPLEMENTED
;
1845 r
= ERROR_INVALID_DATA
;
1851 static UINT
TABLE_delete( struct tagMSIVIEW
*view
)
1853 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1855 TRACE("%p\n", view
);
1862 return ERROR_SUCCESS
;
1865 static UINT
TABLE_find_matching_rows( struct tagMSIVIEW
*view
, UINT col
,
1866 UINT val
, UINT
*row
, MSIITERHANDLE
*handle
)
1868 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1869 const MSICOLUMNHASHENTRY
*entry
;
1871 TRACE("%p, %d, %u, %p\n", view
, col
, val
, *handle
);
1874 return ERROR_INVALID_PARAMETER
;
1876 if( (col
==0) || (col
> tv
->num_cols
) )
1877 return ERROR_INVALID_PARAMETER
;
1879 if( !tv
->columns
[col
-1].hash_table
)
1882 UINT num_rows
= tv
->table
->row_count
;
1883 MSICOLUMNHASHENTRY
**hash_table
;
1884 MSICOLUMNHASHENTRY
*new_entry
;
1886 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1888 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1889 ERR("%p %p\n", tv
, tv
->columns
);
1890 return ERROR_FUNCTION_FAILED
;
1893 /* allocate contiguous memory for the table and its entries so we
1894 * don't have to do an expensive cleanup */
1895 hash_table
= msi_alloc(MSITABLE_HASH_TABLE_SIZE
* sizeof(MSICOLUMNHASHENTRY
*) +
1896 num_rows
* sizeof(MSICOLUMNHASHENTRY
));
1898 return ERROR_OUTOFMEMORY
;
1900 memset(hash_table
, 0, MSITABLE_HASH_TABLE_SIZE
* sizeof(MSICOLUMNHASHENTRY
*));
1901 tv
->columns
[col
-1].hash_table
= hash_table
;
1903 new_entry
= (MSICOLUMNHASHENTRY
*)(hash_table
+ MSITABLE_HASH_TABLE_SIZE
);
1905 for (i
= 0; i
< num_rows
; i
++, new_entry
++)
1909 if (view
->ops
->fetch_int( view
, i
, col
, &row_value
) != ERROR_SUCCESS
)
1912 new_entry
->next
= NULL
;
1913 new_entry
->value
= row_value
;
1915 if (hash_table
[row_value
% MSITABLE_HASH_TABLE_SIZE
])
1917 MSICOLUMNHASHENTRY
*prev_entry
= hash_table
[row_value
% MSITABLE_HASH_TABLE_SIZE
];
1918 while (prev_entry
->next
)
1919 prev_entry
= prev_entry
->next
;
1920 prev_entry
->next
= new_entry
;
1923 hash_table
[row_value
% MSITABLE_HASH_TABLE_SIZE
] = new_entry
;
1928 entry
= tv
->columns
[col
-1].hash_table
[val
% MSITABLE_HASH_TABLE_SIZE
];
1930 entry
= (*handle
)->next
;
1932 while (entry
&& entry
->value
!= val
)
1933 entry
= entry
->next
;
1937 return ERROR_NO_MORE_ITEMS
;
1941 return ERROR_SUCCESS
;
1944 static UINT
TABLE_add_ref(struct tagMSIVIEW
*view
)
1946 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1949 TRACE("%p %d\n", view
, tv
->table
->ref_count
);
1951 for (i
= 0; i
< tv
->table
->col_count
; i
++)
1953 if (tv
->table
->colinfo
[i
].type
& MSITYPE_TEMPORARY
)
1954 InterlockedIncrement(&tv
->table
->colinfo
[i
].ref_count
);
1957 return InterlockedIncrement(&tv
->table
->ref_count
);
1960 static UINT
TABLE_remove_column(struct tagMSIVIEW
*view
, LPCWSTR table
, UINT number
)
1962 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1963 MSIRECORD
*rec
= NULL
;
1964 MSIVIEW
*columns
= NULL
;
1967 rec
= MSI_CreateRecord(2);
1969 return ERROR_OUTOFMEMORY
;
1971 MSI_RecordSetStringW(rec
, 1, table
);
1972 MSI_RecordSetInteger(rec
, 2, number
);
1974 r
= TABLE_CreateView(tv
->db
, szColumns
, &columns
);
1975 if (r
!= ERROR_SUCCESS
)
1977 msiobj_release(&rec
->hdr
);
1981 r
= msi_table_find_row((MSITABLEVIEW
*)columns
, rec
, &row
, NULL
);
1982 if (r
!= ERROR_SUCCESS
)
1985 r
= TABLE_delete_row(columns
, row
);
1986 if (r
!= ERROR_SUCCESS
)
1989 msi_update_table_columns(tv
->db
, table
);
1992 msiobj_release(&rec
->hdr
);
1993 columns
->ops
->delete(columns
);
1997 static UINT
TABLE_release(struct tagMSIVIEW
*view
)
1999 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
2000 INT ref
= tv
->table
->ref_count
;
2003 TRACE("%p %d\n", view
, ref
);
2005 for (i
= 0; i
< tv
->table
->col_count
; i
++)
2007 if (tv
->table
->colinfo
[i
].type
& MSITYPE_TEMPORARY
)
2009 ref
= InterlockedDecrement(&tv
->table
->colinfo
[i
].ref_count
);
2012 r
= TABLE_remove_column(view
, tv
->table
->colinfo
[i
].tablename
,
2013 tv
->table
->colinfo
[i
].number
);
2014 if (r
!= ERROR_SUCCESS
)
2020 ref
= InterlockedDecrement(&tv
->table
->ref_count
);
2023 if (!tv
->table
->row_count
)
2025 list_remove(&tv
->table
->entry
);
2026 free_table(tv
->table
);
2034 static UINT
TABLE_add_column(struct tagMSIVIEW
*view
, LPCWSTR table
, UINT number
,
2035 LPCWSTR column
, UINT type
, BOOL hold
)
2037 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
2042 rec
= MSI_CreateRecord(4);
2044 return ERROR_OUTOFMEMORY
;
2046 MSI_RecordSetStringW(rec
, 1, table
);
2047 MSI_RecordSetInteger(rec
, 2, number
);
2048 MSI_RecordSetStringW(rec
, 3, column
);
2049 MSI_RecordSetInteger(rec
, 4, type
);
2051 r
= TABLE_insert_row(&tv
->view
, rec
, -1, FALSE
);
2052 if (r
!= ERROR_SUCCESS
)
2055 msi_update_table_columns(tv
->db
, table
);
2060 msitable
= find_cached_table(tv
->db
, table
);
2061 for (i
= 0; i
< msitable
->col_count
; i
++)
2063 if (!strcmpW( msitable
->colinfo
[i
].colname
, column
))
2065 InterlockedIncrement(&msitable
->colinfo
[i
].ref_count
);
2071 msiobj_release(&rec
->hdr
);
2075 static UINT
TABLE_drop(struct tagMSIVIEW
*view
)
2077 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
2078 MSIVIEW
*tables
= NULL
;
2079 MSIRECORD
*rec
= NULL
;
2083 TRACE("dropping table %s\n", debugstr_w(tv
->name
));
2085 for (i
= tv
->table
->col_count
- 1; i
>= 0; i
--)
2087 r
= TABLE_remove_column(view
, tv
->table
->colinfo
[i
].tablename
,
2088 tv
->table
->colinfo
[i
].number
);
2089 if (r
!= ERROR_SUCCESS
)
2093 rec
= MSI_CreateRecord(1);
2095 return ERROR_OUTOFMEMORY
;
2097 MSI_RecordSetStringW(rec
, 1, tv
->name
);
2099 r
= TABLE_CreateView(tv
->db
, szTables
, &tables
);
2100 if (r
!= ERROR_SUCCESS
)
2102 msiobj_release(&rec
->hdr
);
2106 r
= msi_table_find_row((MSITABLEVIEW
*)tables
, rec
, &row
, NULL
);
2107 if (r
!= ERROR_SUCCESS
)
2110 r
= TABLE_delete_row(tables
, row
);
2111 if (r
!= ERROR_SUCCESS
)
2114 list_remove(&tv
->table
->entry
);
2115 free_table(tv
->table
);
2118 msiobj_release(&rec
->hdr
);
2119 tables
->ops
->delete(tables
);
2124 static const MSIVIEWOPS table_ops
=
2134 TABLE_get_dimensions
,
2135 TABLE_get_column_info
,
2138 TABLE_find_matching_rows
,
2142 TABLE_remove_column
,
2147 UINT
TABLE_CreateView( MSIDATABASE
*db
, LPCWSTR name
, MSIVIEW
**view
)
2152 TRACE("%p %s %p\n", db
, debugstr_w(name
), view
);
2154 if ( !strcmpW( name
, szStreams
) )
2155 return STREAMS_CreateView( db
, view
);
2156 else if ( !strcmpW( name
, szStorages
) )
2157 return STORAGES_CreateView( db
, view
);
2159 sz
= FIELD_OFFSET( MSITABLEVIEW
, name
[lstrlenW( name
) + 1] );
2160 tv
= msi_alloc_zero( sz
);
2162 return ERROR_FUNCTION_FAILED
;
2164 r
= get_table( db
, name
, &tv
->table
);
2165 if( r
!= ERROR_SUCCESS
)
2168 WARN("table not found\n");
2172 TRACE("table %p found with %d columns\n", tv
->table
, tv
->table
->col_count
);
2174 /* fill the structure */
2175 tv
->view
.ops
= &table_ops
;
2177 tv
->columns
= tv
->table
->colinfo
;
2178 tv
->num_cols
= tv
->table
->col_count
;
2179 tv
->row_size
= msi_table_get_row_size( db
, tv
->table
->colinfo
, tv
->table
->col_count
, LONG_STR_BYTES
);
2181 TRACE("%s one row is %d bytes\n", debugstr_w(name
), tv
->row_size
);
2183 *view
= (MSIVIEW
*) tv
;
2184 lstrcpyW( tv
->name
, name
);
2186 return ERROR_SUCCESS
;
2189 UINT
MSI_CommitTables( MSIDATABASE
*db
)
2191 UINT r
, bytes_per_strref
;
2193 MSITABLE
*table
= NULL
;
2197 r
= msi_save_string_table( db
->strings
, db
->storage
, &bytes_per_strref
);
2198 if( r
!= ERROR_SUCCESS
)
2200 WARN("failed to save string table r=%08x\n",r
);
2204 LIST_FOR_EACH_ENTRY( table
, &db
->tables
, MSITABLE
, entry
)
2206 r
= save_table( db
, table
, bytes_per_strref
);
2207 if( r
!= ERROR_SUCCESS
)
2209 WARN("failed to save table %s (r=%08x)\n",
2210 debugstr_w(table
->name
), r
);
2215 hr
= IStorage_Commit( db
->storage
, 0 );
2218 WARN("failed to commit changes 0x%08x\n", hr
);
2219 r
= ERROR_FUNCTION_FAILED
;
2224 MSICONDITION
MSI_DatabaseIsTablePersistent( MSIDATABASE
*db
, LPCWSTR table
)
2229 TRACE("%p %s\n", db
, debugstr_w(table
));
2232 return MSICONDITION_ERROR
;
2234 r
= get_table( db
, table
, &t
);
2235 if (r
!= ERROR_SUCCESS
)
2236 return MSICONDITION_NONE
;
2238 return t
->persistent
;
2241 static UINT
read_raw_int(const BYTE
*data
, UINT col
, UINT bytes
)
2245 for (i
= 0; i
< bytes
; i
++)
2246 ret
+= (data
[col
+ i
] << i
* 8);
2251 static UINT
msi_record_encoded_stream_name( const MSITABLEVIEW
*tv
, MSIRECORD
*rec
, LPWSTR
*pstname
)
2253 LPWSTR stname
= NULL
, sval
, p
;
2257 TRACE("%p %p\n", tv
, rec
);
2259 len
= lstrlenW( tv
->name
) + 1;
2260 stname
= msi_alloc( len
*sizeof(WCHAR
) );
2263 r
= ERROR_OUTOFMEMORY
;
2267 lstrcpyW( stname
, tv
->name
);
2269 for ( i
= 0; i
< tv
->num_cols
; i
++ )
2271 if ( tv
->columns
[i
].type
& MSITYPE_KEY
)
2273 sval
= msi_dup_record_field( rec
, i
+ 1 );
2276 r
= ERROR_OUTOFMEMORY
;
2280 len
+= lstrlenW( szDot
) + lstrlenW ( sval
);
2281 p
= msi_realloc ( stname
, len
*sizeof(WCHAR
) );
2284 r
= ERROR_OUTOFMEMORY
;
2290 lstrcatW( stname
, szDot
);
2291 lstrcatW( stname
, sval
);
2299 *pstname
= encode_streamname( FALSE
, stname
);
2302 return ERROR_SUCCESS
;
2305 msi_free ( stname
);
2310 static MSIRECORD
*msi_get_transform_record( const MSITABLEVIEW
*tv
, const string_table
*st
,
2312 const BYTE
*rawdata
, UINT bytes_per_strref
)
2314 UINT i
, val
, ofs
= 0;
2316 MSICOLUMNINFO
*columns
= tv
->columns
;
2319 mask
= rawdata
[0] | (rawdata
[1] << 8);
2322 rec
= MSI_CreateRecord( tv
->num_cols
);
2327 for( i
=0; i
<tv
->num_cols
; i
++ )
2329 if ( (mask
&1) && (i
>=(mask
>>8)) )
2331 /* all keys must be present */
2332 if ( (~mask
&1) && (~columns
[i
].type
& MSITYPE_KEY
) && ((1<<i
) & ~mask
) )
2335 if( MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
2338 IStream
*stm
= NULL
;
2341 ofs
+= bytes_per_column( tv
->db
, &columns
[i
], bytes_per_strref
);
2343 r
= msi_record_encoded_stream_name( tv
, rec
, &encname
);
2344 if ( r
!= ERROR_SUCCESS
)
2347 r
= IStorage_OpenStream( stg
, encname
, NULL
,
2348 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, &stm
);
2349 if ( r
!= ERROR_SUCCESS
)
2351 msi_free( encname
);
2355 MSI_RecordSetStream( rec
, i
+1, stm
);
2356 TRACE(" field %d [%s]\n", i
+1, debugstr_w(encname
));
2357 msi_free( encname
);
2359 else if( columns
[i
].type
& MSITYPE_STRING
)
2364 val
= read_raw_int(rawdata
, ofs
, bytes_per_strref
);
2365 sval
= msi_string_lookup( st
, val
, &len
);
2366 msi_record_set_string( rec
, i
+1, sval
, len
);
2367 TRACE(" field %d [%s]\n", i
+1, debugstr_wn(sval
, len
));
2368 ofs
+= bytes_per_strref
;
2372 UINT n
= bytes_per_column( tv
->db
, &columns
[i
], bytes_per_strref
);
2376 val
= read_raw_int(rawdata
, ofs
, n
);
2378 MSI_RecordSetInteger( rec
, i
+1, val
-0x8000 );
2379 TRACE(" field %d [0x%04x]\n", i
+1, val
);
2382 val
= read_raw_int(rawdata
, ofs
, n
);
2384 MSI_RecordSetInteger( rec
, i
+1, val
^0x80000000 );
2385 TRACE(" field %d [0x%08x]\n", i
+1, val
);
2388 ERR("oops - unknown column width %d\n", n
);
2397 static void dump_record( MSIRECORD
*rec
)
2401 n
= MSI_RecordGetFieldCount( rec
);
2402 for( i
=1; i
<=n
; i
++ )
2407 if( MSI_RecordIsNull( rec
, i
) )
2408 TRACE("row -> []\n");
2409 else if( (sval
= msi_record_get_string( rec
, i
, &len
)) )
2410 TRACE("row -> [%s]\n", debugstr_wn(sval
, len
));
2412 TRACE("row -> [0x%08x]\n", MSI_RecordGetInteger( rec
, i
) );
2416 static void dump_table( const string_table
*st
, const USHORT
*rawdata
, UINT rawsize
)
2419 for (i
= 0; i
< rawsize
/ 2; i
++)
2422 const WCHAR
*sval
= msi_string_lookup( st
, rawdata
[i
], &len
);
2423 MESSAGE(" %04x %s\n", rawdata
[i
], debugstr_wn(sval
, len
) );
2427 static UINT
* msi_record_to_row( const MSITABLEVIEW
*tv
, MSIRECORD
*rec
)
2431 data
= msi_alloc( tv
->num_cols
*sizeof (UINT
) );
2432 for( i
=0; i
<tv
->num_cols
; i
++ )
2436 if ( ~tv
->columns
[i
].type
& MSITYPE_KEY
)
2439 /* turn the transform column value into a row value */
2440 if ( ( tv
->columns
[i
].type
& MSITYPE_STRING
) &&
2441 ! MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
2444 const WCHAR
*str
= msi_record_get_string( rec
, i
+1, &len
);
2447 r
= msi_string2id( tv
->db
->strings
, str
, len
, &data
[i
] );
2449 /* if there's no matching string in the string table,
2450 these keys can't match any record, so fail now. */
2451 if (r
!= ERROR_SUCCESS
)
2461 data
[i
] = MSI_RecordGetInteger( rec
, i
+1 );
2463 if (data
[i
] == MSI_NULL_INTEGER
)
2465 else if ((tv
->columns
[i
].type
&0xff) == 2)
2468 data
[i
] += 0x80000000;
2474 static UINT
msi_row_matches( MSITABLEVIEW
*tv
, UINT row
, const UINT
*data
, UINT
*column
)
2476 UINT i
, r
, x
, ret
= ERROR_FUNCTION_FAILED
;
2478 for( i
=0; i
<tv
->num_cols
; i
++ )
2480 if ( ~tv
->columns
[i
].type
& MSITYPE_KEY
)
2483 /* turn the transform column value into a row value */
2484 r
= TABLE_fetch_int( &tv
->view
, row
, i
+1, &x
);
2485 if ( r
!= ERROR_SUCCESS
)
2487 ERR("TABLE_fetch_int shouldn't fail here\n");
2491 /* if this key matches, move to the next column */
2494 ret
= ERROR_FUNCTION_FAILED
;
2497 if (column
) *column
= i
;
2498 ret
= ERROR_SUCCESS
;
2503 static UINT
msi_table_find_row( MSITABLEVIEW
*tv
, MSIRECORD
*rec
, UINT
*row
, UINT
*column
)
2505 UINT i
, r
= ERROR_FUNCTION_FAILED
, *data
;
2507 data
= msi_record_to_row( tv
, rec
);
2510 for( i
= 0; i
< tv
->table
->row_count
; i
++ )
2512 r
= msi_row_matches( tv
, i
, data
, column
);
2513 if( r
== ERROR_SUCCESS
)
2529 static UINT
msi_table_load_transform( MSIDATABASE
*db
, IStorage
*stg
,
2530 string_table
*st
, TRANSFORMDATA
*transform
,
2531 UINT bytes_per_strref
)
2533 BYTE
*rawdata
= NULL
;
2534 MSITABLEVIEW
*tv
= NULL
;
2535 UINT r
, n
, sz
, i
, mask
, num_cols
, colcol
= 0, rawsize
= 0;
2536 MSIRECORD
*rec
= NULL
;
2541 return ERROR_SUCCESS
;
2543 name
= transform
->name
;
2546 TRACE("%p %p %p %s\n", db
, stg
, st
, debugstr_w(name
) );
2548 /* read the transform data */
2549 read_stream_data( stg
, name
, TRUE
, &rawdata
, &rawsize
);
2552 TRACE("table %s empty\n", debugstr_w(name
) );
2553 return ERROR_INVALID_TABLE
;
2556 /* create a table view */
2557 r
= TABLE_CreateView( db
, name
, (MSIVIEW
**) &tv
);
2558 if( r
!= ERROR_SUCCESS
)
2561 r
= tv
->view
.ops
->execute( &tv
->view
, NULL
);
2562 if( r
!= ERROR_SUCCESS
)
2565 TRACE("name = %s columns = %u row_size = %u raw size = %u\n",
2566 debugstr_w(name
), tv
->num_cols
, tv
->row_size
, rawsize
);
2568 /* interpret the data */
2569 for (n
= 0; n
< rawsize
;)
2571 mask
= rawdata
[n
] | (rawdata
[n
+ 1] << 8);
2575 * if the low bit is set, columns are continuous and
2576 * the number of columns is specified in the high byte
2579 num_cols
= mask
>> 8;
2580 if (num_cols
> tv
->num_cols
)
2582 ERR("excess columns in transform: %u > %u\n", num_cols
, tv
->num_cols
);
2586 for (i
= 0; i
< num_cols
; i
++)
2588 if( (tv
->columns
[i
].type
& MSITYPE_STRING
) &&
2589 ! MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
2590 sz
+= bytes_per_strref
;
2592 sz
+= bytes_per_column( tv
->db
, &tv
->columns
[i
], bytes_per_strref
);
2598 * If the low bit is not set, mask is a bitmask.
2599 * Excepting for key fields, which are always present,
2600 * each bit indicates that a field is present in the transform record.
2602 * mask == 0 is a special case ... only the keys will be present
2603 * and it means that this row should be deleted.
2606 num_cols
= tv
->num_cols
;
2607 for (i
= 0; i
< num_cols
; i
++)
2609 if ((tv
->columns
[i
].type
& MSITYPE_KEY
) || ((1 << i
) & mask
))
2611 if ((tv
->columns
[i
].type
& MSITYPE_STRING
) &&
2612 !MSITYPE_IS_BINARY(tv
->columns
[i
].type
))
2613 sz
+= bytes_per_strref
;
2615 sz
+= bytes_per_column( tv
->db
, &tv
->columns
[i
], bytes_per_strref
);
2620 /* check we didn't run of the end of the table */
2621 if (n
+ sz
> rawsize
)
2624 dump_table( st
, (USHORT
*)rawdata
, rawsize
);
2628 rec
= msi_get_transform_record( tv
, st
, stg
, &rawdata
[n
], bytes_per_strref
);
2633 UINT number
= MSI_NULL_INTEGER
;
2636 if (!strcmpW( name
, szColumns
))
2638 MSI_RecordGetStringW( rec
, 1, table
, &sz
);
2639 number
= MSI_RecordGetInteger( rec
, 2 );
2642 * Native msi seems writes nul into the Number (2nd) column of
2643 * the _Columns table when there are new columns
2645 if ( number
== MSI_NULL_INTEGER
)
2647 /* reset the column number on a new table */
2648 if (strcmpW( coltable
, table
))
2651 lstrcpyW( coltable
, table
);
2654 /* fix nul column numbers */
2655 MSI_RecordSetInteger( rec
, 2, ++colcol
);
2659 if (TRACE_ON(msidb
)) dump_record( rec
);
2661 r
= msi_table_find_row( tv
, rec
, &row
, NULL
);
2662 if (r
== ERROR_SUCCESS
)
2666 TRACE("deleting row [%d]:\n", row
);
2667 r
= TABLE_delete_row( &tv
->view
, row
);
2668 if (r
!= ERROR_SUCCESS
)
2669 WARN("failed to delete row %u\n", r
);
2673 TRACE("modifying full row [%d]:\n", row
);
2674 r
= TABLE_set_row( &tv
->view
, row
, rec
, (1 << tv
->num_cols
) - 1 );
2675 if (r
!= ERROR_SUCCESS
)
2676 WARN("failed to modify row %u\n", r
);
2680 TRACE("modifying masked row [%d]:\n", row
);
2681 r
= TABLE_set_row( &tv
->view
, row
, rec
, mask
);
2682 if (r
!= ERROR_SUCCESS
)
2683 WARN("failed to modify row %u\n", r
);
2688 TRACE("inserting row\n");
2689 r
= TABLE_insert_row( &tv
->view
, rec
, -1, FALSE
);
2690 if (r
!= ERROR_SUCCESS
)
2691 WARN("failed to insert row %u\n", r
);
2694 if (!strcmpW( name
, szColumns
))
2695 msi_update_table_columns( db
, table
);
2697 msiobj_release( &rec
->hdr
);
2704 /* no need to free the table, it's associated with the database */
2705 msi_free( rawdata
);
2707 tv
->view
.ops
->delete( &tv
->view
);
2709 return ERROR_SUCCESS
;
2713 * msi_table_apply_transform
2715 * Enumerate the table transforms in a transform storage and apply each one.
2717 UINT
msi_table_apply_transform( MSIDATABASE
*db
, IStorage
*stg
)
2719 struct list transforms
;
2720 IEnumSTATSTG
*stgenum
= NULL
;
2721 TRANSFORMDATA
*transform
;
2722 TRANSFORMDATA
*tables
= NULL
, *columns
= NULL
;
2725 string_table
*strings
;
2726 UINT ret
= ERROR_FUNCTION_FAILED
;
2727 UINT bytes_per_strref
;
2728 BOOL property_update
= FALSE
;
2730 TRACE("%p %p\n", db
, stg
);
2732 strings
= msi_load_string_table( stg
, &bytes_per_strref
);
2736 r
= IStorage_EnumElements( stg
, 0, NULL
, 0, &stgenum
);
2740 list_init(&transforms
);
2744 MSITABLEVIEW
*tv
= NULL
;
2748 r
= IEnumSTATSTG_Next( stgenum
, 1, &stat
, &count
);
2749 if ( FAILED( r
) || !count
)
2752 decode_streamname( stat
.pwcsName
, name
);
2753 CoTaskMemFree( stat
.pwcsName
);
2754 if ( name
[0] != 0x4840 )
2757 if ( !strcmpW( name
+1, szStringPool
) ||
2758 !strcmpW( name
+1, szStringData
) )
2761 transform
= msi_alloc_zero( sizeof(TRANSFORMDATA
) );
2765 list_add_tail( &transforms
, &transform
->entry
);
2767 transform
->name
= strdupW( name
+ 1 );
2769 if ( !strcmpW( transform
->name
, szTables
) )
2771 else if (!strcmpW( transform
->name
, szColumns
) )
2772 columns
= transform
;
2773 else if (!strcmpW( transform
->name
, szProperty
))
2774 property_update
= TRUE
;
2776 TRACE("transform contains stream %s\n", debugstr_w(name
));
2778 /* load the table */
2779 r
= TABLE_CreateView( db
, transform
->name
, (MSIVIEW
**) &tv
);
2780 if( r
!= ERROR_SUCCESS
)
2783 r
= tv
->view
.ops
->execute( &tv
->view
, NULL
);
2784 if( r
!= ERROR_SUCCESS
)
2786 tv
->view
.ops
->delete( &tv
->view
);
2790 tv
->view
.ops
->delete( &tv
->view
);
2794 * Apply _Tables and _Columns transforms first so that
2795 * the table metadata is correct, and empty tables exist.
2797 ret
= msi_table_load_transform( db
, stg
, strings
, tables
, bytes_per_strref
);
2798 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_INVALID_TABLE
)
2801 ret
= msi_table_load_transform( db
, stg
, strings
, columns
, bytes_per_strref
);
2802 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_INVALID_TABLE
)
2805 ret
= ERROR_SUCCESS
;
2807 while ( !list_empty( &transforms
) )
2809 transform
= LIST_ENTRY( list_head( &transforms
), TRANSFORMDATA
, entry
);
2811 if ( strcmpW( transform
->name
, szColumns
) &&
2812 strcmpW( transform
->name
, szTables
) &&
2813 ret
== ERROR_SUCCESS
)
2815 ret
= msi_table_load_transform( db
, stg
, strings
, transform
, bytes_per_strref
);
2818 list_remove( &transform
->entry
);
2819 msi_free( transform
->name
);
2820 msi_free( transform
);
2823 if ( ret
== ERROR_SUCCESS
)
2825 append_storage_to_db( db
, stg
);
2826 if (property_update
) msi_clone_properties( db
);
2831 IEnumSTATSTG_Release( stgenum
);
2833 msi_destroy_stringtable( strings
);