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
msi_stream_name( const MSITABLEVIEW
*tv
, UINT row
, LPWSTR
*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
= msi_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
msi_addstreamW( MSIDATABASE
*db
, LPCWSTR 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 MSIQUERY
*query
= NULL
;
1211 TRACE("%p %s %p\n", db
, debugstr_w(name
), data
);
1213 rec
= MSI_CreateRecord( 2 );
1215 return ERROR_OUTOFMEMORY
;
1217 r
= MSI_RecordSetStringW( rec
, 1, name
);
1218 if ( r
!= ERROR_SUCCESS
)
1221 r
= MSI_RecordSetIStream( rec
, 2, data
);
1222 if ( r
!= ERROR_SUCCESS
)
1225 r
= MSI_DatabaseOpenViewW( db
, insert
, &query
);
1226 if ( r
!= ERROR_SUCCESS
)
1229 r
= MSI_ViewExecute( query
, rec
);
1232 msiobj_release( &query
->hdr
);
1233 msiobj_release( &rec
->hdr
);
1237 static UINT
get_table_value_from_record( MSITABLEVIEW
*tv
, MSIRECORD
*rec
, UINT iField
, UINT
*pvalue
)
1239 MSICOLUMNINFO columninfo
;
1243 if ( (iField
<= 0) ||
1244 (iField
> tv
->num_cols
) ||
1245 MSI_RecordIsNull( rec
, iField
) )
1246 return ERROR_FUNCTION_FAILED
;
1248 columninfo
= tv
->columns
[ iField
- 1 ];
1250 if ( MSITYPE_IS_BINARY(columninfo
.type
) )
1252 *pvalue
= 1; /* refers to the first key column */
1254 else if ( columninfo
.type
& MSITYPE_STRING
)
1257 const WCHAR
*sval
= msi_record_get_string( rec
, iField
, &len
);
1260 r
= msi_string2id( tv
->db
->strings
, sval
, len
, pvalue
);
1261 if (r
!= ERROR_SUCCESS
)
1262 return ERROR_NOT_FOUND
;
1266 else if ( bytes_per_column( tv
->db
, &columninfo
, LONG_STR_BYTES
) == 2 )
1268 ival
= MSI_RecordGetInteger( rec
, iField
);
1269 if (ival
== 0x80000000) *pvalue
= 0x8000;
1272 *pvalue
= 0x8000 + MSI_RecordGetInteger( rec
, iField
);
1273 if (*pvalue
& 0xffff0000)
1275 ERR("field %u value %d out of range\n", iField
, *pvalue
- 0x8000);
1276 return ERROR_FUNCTION_FAILED
;
1282 ival
= MSI_RecordGetInteger( rec
, iField
);
1283 *pvalue
= ival
^ 0x80000000;
1286 return ERROR_SUCCESS
;
1289 static UINT
TABLE_set_row( struct tagMSIVIEW
*view
, UINT row
, MSIRECORD
*rec
, UINT mask
)
1291 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1292 UINT i
, val
, r
= ERROR_SUCCESS
;
1295 return ERROR_INVALID_PARAMETER
;
1297 /* test if any of the mask bits are invalid */
1298 if ( mask
>= (1<<tv
->num_cols
) )
1299 return ERROR_INVALID_PARAMETER
;
1301 for ( i
= 0; i
< tv
->num_cols
; i
++ )
1305 /* only update the fields specified in the mask */
1306 if ( !(mask
&(1<<i
)) )
1309 persistent
= (tv
->table
->persistent
!= MSICONDITION_FALSE
) &&
1310 (tv
->table
->data_persistent
[row
]);
1311 /* FIXME: should we allow updating keys? */
1314 if ( !MSI_RecordIsNull( rec
, i
+ 1 ) )
1316 r
= get_table_value_from_record (tv
, rec
, i
+ 1, &val
);
1317 if ( MSITYPE_IS_BINARY(tv
->columns
[ i
].type
) )
1322 if ( r
!= ERROR_SUCCESS
)
1323 return ERROR_FUNCTION_FAILED
;
1325 r
= MSI_RecordGetIStream( rec
, i
+ 1, &stm
);
1326 if ( r
!= ERROR_SUCCESS
)
1329 r
= msi_stream_name( tv
, row
, &stname
);
1330 if ( r
!= ERROR_SUCCESS
)
1332 IStream_Release( stm
);
1336 r
= msi_addstreamW( tv
->db
, stname
, stm
);
1337 IStream_Release( stm
);
1338 msi_free ( stname
);
1340 if ( r
!= ERROR_SUCCESS
)
1343 else if ( tv
->columns
[i
].type
& MSITYPE_STRING
)
1347 if ( r
!= ERROR_SUCCESS
)
1350 const WCHAR
*sval
= msi_record_get_string( rec
, i
+ 1, &len
);
1351 val
= msi_add_string( tv
->db
->strings
, sval
, len
,
1352 persistent
? StringPersistent
: StringNonPersistent
);
1356 TABLE_fetch_int(&tv
->view
, row
, i
+ 1, &x
);
1363 if ( r
!= ERROR_SUCCESS
)
1364 return ERROR_FUNCTION_FAILED
;
1368 r
= TABLE_set_int( tv
, row
, i
+1, val
);
1369 if ( r
!= ERROR_SUCCESS
)
1375 static UINT
table_create_new_row( struct tagMSIVIEW
*view
, UINT
*num
, BOOL temporary
)
1377 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1382 BOOL
**data_persist_ptr
;
1385 TRACE("%p %s\n", view
, temporary
? "TRUE" : "FALSE");
1388 return ERROR_INVALID_PARAMETER
;
1390 row
= msi_alloc_zero( tv
->row_size
);
1392 return ERROR_NOT_ENOUGH_MEMORY
;
1394 row_count
= &tv
->table
->row_count
;
1395 data_ptr
= &tv
->table
->data
;
1396 data_persist_ptr
= &tv
->table
->data_persistent
;
1398 *num
= tv
->table
->row_count
;
1400 sz
= (*row_count
+ 1) * sizeof (BYTE
*);
1402 p
= msi_realloc( *data_ptr
, sz
);
1404 p
= msi_alloc( sz
);
1408 return ERROR_NOT_ENOUGH_MEMORY
;
1411 sz
= (*row_count
+ 1) * sizeof (BOOL
);
1412 if( *data_persist_ptr
)
1413 b
= msi_realloc( *data_persist_ptr
, sz
);
1415 b
= msi_alloc( sz
);
1420 return ERROR_NOT_ENOUGH_MEMORY
;
1424 (*data_ptr
)[*row_count
] = row
;
1426 *data_persist_ptr
= b
;
1427 (*data_persist_ptr
)[*row_count
] = !temporary
;
1431 return ERROR_SUCCESS
;
1434 static UINT
TABLE_execute( struct tagMSIVIEW
*view
, MSIRECORD
*record
)
1436 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1438 TRACE("%p %p\n", tv
, record
);
1440 TRACE("There are %d columns\n", tv
->num_cols
);
1442 return ERROR_SUCCESS
;
1445 static UINT
TABLE_close( struct tagMSIVIEW
*view
)
1447 TRACE("%p\n", view
);
1449 return ERROR_SUCCESS
;
1452 static UINT
TABLE_get_dimensions( struct tagMSIVIEW
*view
, UINT
*rows
, UINT
*cols
)
1454 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1456 TRACE("%p %p %p\n", view
, rows
, cols
);
1459 *cols
= tv
->num_cols
;
1463 return ERROR_INVALID_PARAMETER
;
1464 *rows
= tv
->table
->row_count
;
1467 return ERROR_SUCCESS
;
1470 static UINT
TABLE_get_column_info( struct tagMSIVIEW
*view
,
1471 UINT n
, LPCWSTR
*name
, UINT
*type
, BOOL
*temporary
,
1472 LPCWSTR
*table_name
)
1474 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1476 TRACE("%p %d %p %p\n", tv
, n
, name
, type
);
1478 if( ( n
== 0 ) || ( n
> tv
->num_cols
) )
1479 return ERROR_INVALID_PARAMETER
;
1483 *name
= tv
->columns
[n
-1].colname
;
1485 return ERROR_FUNCTION_FAILED
;
1490 *table_name
= tv
->columns
[n
-1].tablename
;
1492 return ERROR_FUNCTION_FAILED
;
1496 *type
= tv
->columns
[n
-1].type
;
1499 *temporary
= tv
->columns
[n
-1].temporary
;
1501 return ERROR_SUCCESS
;
1504 static UINT
msi_table_find_row( MSITABLEVIEW
*tv
, MSIRECORD
*rec
, UINT
*row
, UINT
*column
);
1506 static UINT
table_validate_new( MSITABLEVIEW
*tv
, MSIRECORD
*rec
, UINT
*column
)
1510 /* check there are no null values where they're not allowed */
1511 for( i
= 0; i
< tv
->num_cols
; i
++ )
1513 if ( tv
->columns
[i
].type
& MSITYPE_NULLABLE
)
1516 if ( MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
1517 TRACE("skipping binary column\n");
1518 else if ( tv
->columns
[i
].type
& MSITYPE_STRING
)
1521 const WCHAR
*str
= msi_record_get_string( rec
, i
+1, &len
);
1523 if (!str
|| (!str
[0] && !len
))
1525 if (column
) *column
= i
;
1526 return ERROR_INVALID_DATA
;
1533 n
= MSI_RecordGetInteger( rec
, i
+1 );
1534 if (n
== MSI_NULL_INTEGER
)
1536 if (column
) *column
= i
;
1537 return ERROR_INVALID_DATA
;
1542 /* check there are no duplicate keys */
1543 r
= msi_table_find_row( tv
, rec
, &row
, column
);
1544 if (r
== ERROR_SUCCESS
)
1545 return ERROR_FUNCTION_FAILED
;
1547 return ERROR_SUCCESS
;
1550 static int compare_record( MSITABLEVIEW
*tv
, UINT row
, MSIRECORD
*rec
)
1552 UINT r
, i
, ivalue
, x
;
1554 for (i
= 0; i
< tv
->num_cols
; i
++ )
1556 if (!(tv
->columns
[i
].type
& MSITYPE_KEY
)) continue;
1558 r
= get_table_value_from_record( tv
, rec
, i
+ 1, &ivalue
);
1559 if (r
!= ERROR_SUCCESS
)
1562 r
= TABLE_fetch_int( &tv
->view
, row
, i
+ 1, &x
);
1563 if (r
!= ERROR_SUCCESS
)
1565 WARN("TABLE_fetch_int should not fail here %u\n", r
);
1572 else if (ivalue
== x
)
1574 if (i
< tv
->num_cols
- 1) continue;
1583 static int find_insert_index( MSITABLEVIEW
*tv
, MSIRECORD
*rec
)
1585 int idx
, c
, low
= 0, high
= tv
->table
->row_count
- 1;
1587 TRACE("%p %p\n", tv
, rec
);
1591 idx
= (low
+ high
) / 2;
1592 c
= compare_record( tv
, idx
, rec
);
1600 TRACE("found %u\n", idx
);
1604 TRACE("found %u\n", high
+ 1);
1608 static UINT
TABLE_insert_row( struct tagMSIVIEW
*view
, MSIRECORD
*rec
, UINT row
, BOOL temporary
)
1610 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1613 TRACE("%p %p %s\n", tv
, rec
, temporary
? "TRUE" : "FALSE" );
1615 /* check that the key is unique - can we find a matching row? */
1616 r
= table_validate_new( tv
, rec
, NULL
);
1617 if( r
!= ERROR_SUCCESS
)
1618 return ERROR_FUNCTION_FAILED
;
1621 row
= find_insert_index( tv
, rec
);
1623 r
= table_create_new_row( view
, &row
, temporary
);
1624 TRACE("insert_row returned %08x\n", r
);
1625 if( r
!= ERROR_SUCCESS
)
1628 /* shift the rows to make room for the new row */
1629 for (i
= tv
->table
->row_count
- 1; i
> row
; i
--)
1631 memmove(&(tv
->table
->data
[i
][0]),
1632 &(tv
->table
->data
[i
- 1][0]), tv
->row_size
);
1633 tv
->table
->data_persistent
[i
] = tv
->table
->data_persistent
[i
- 1];
1636 /* Re-set the persistence flag */
1637 tv
->table
->data_persistent
[row
] = !temporary
;
1638 return TABLE_set_row( view
, row
, rec
, (1<<tv
->num_cols
) - 1 );
1641 static UINT
TABLE_delete_row( struct tagMSIVIEW
*view
, UINT row
)
1643 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1644 UINT r
, num_rows
, num_cols
, i
;
1646 TRACE("%p %d\n", tv
, row
);
1649 return ERROR_INVALID_PARAMETER
;
1651 r
= TABLE_get_dimensions( view
, &num_rows
, &num_cols
);
1652 if ( r
!= ERROR_SUCCESS
)
1655 if ( row
>= num_rows
)
1656 return ERROR_FUNCTION_FAILED
;
1658 num_rows
= tv
->table
->row_count
;
1659 tv
->table
->row_count
--;
1661 /* reset the hash tables */
1662 for (i
= 0; i
< tv
->num_cols
; i
++)
1664 msi_free( tv
->columns
[i
].hash_table
);
1665 tv
->columns
[i
].hash_table
= NULL
;
1668 for (i
= row
+ 1; i
< num_rows
; i
++)
1670 memcpy(tv
->table
->data
[i
- 1], tv
->table
->data
[i
], tv
->row_size
);
1671 tv
->table
->data_persistent
[i
- 1] = tv
->table
->data_persistent
[i
];
1674 msi_free(tv
->table
->data
[num_rows
- 1]);
1676 return ERROR_SUCCESS
;
1679 static UINT
msi_table_update(struct tagMSIVIEW
*view
, MSIRECORD
*rec
, UINT row
)
1681 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1684 /* FIXME: MsiViewFetch should set rec index 0 to some ID that
1685 * sets the fetched record apart from other records
1689 return ERROR_INVALID_PARAMETER
;
1691 r
= msi_table_find_row(tv
, rec
, &new_row
, NULL
);
1692 if (r
!= ERROR_SUCCESS
)
1694 ERR("can't find row to modify\n");
1695 return ERROR_FUNCTION_FAILED
;
1698 /* the row cannot be changed */
1699 if (row
!= new_row
+ 1)
1700 return ERROR_FUNCTION_FAILED
;
1702 return TABLE_set_row(view
, new_row
, rec
, (1 << tv
->num_cols
) - 1);
1705 static UINT
msi_table_assign(struct tagMSIVIEW
*view
, MSIRECORD
*rec
)
1707 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1711 return ERROR_INVALID_PARAMETER
;
1713 r
= msi_table_find_row(tv
, rec
, &row
, NULL
);
1714 if (r
== ERROR_SUCCESS
)
1715 return TABLE_set_row(view
, row
, rec
, (1 << tv
->num_cols
) - 1);
1717 return TABLE_insert_row( view
, rec
, -1, FALSE
);
1720 static UINT
modify_delete_row( struct tagMSIVIEW
*view
, MSIRECORD
*rec
)
1722 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1725 r
= msi_table_find_row(tv
, rec
, &row
, NULL
);
1726 if (r
!= ERROR_SUCCESS
)
1729 return TABLE_delete_row(view
, row
);
1732 static UINT
msi_refresh_record( struct tagMSIVIEW
*view
, MSIRECORD
*rec
, UINT row
)
1737 r
= TABLE_get_row(view
, row
- 1, &curr
);
1738 if (r
!= ERROR_SUCCESS
)
1741 /* Close the original record */
1742 MSI_CloseRecord(&rec
->hdr
);
1744 count
= MSI_RecordGetFieldCount(rec
);
1745 for (i
= 0; i
< count
; i
++)
1746 MSI_RecordCopyField(curr
, i
+ 1, rec
, i
+ 1);
1748 msiobj_release(&curr
->hdr
);
1749 return ERROR_SUCCESS
;
1752 static UINT
TABLE_modify( struct tagMSIVIEW
*view
, MSIMODIFY eModifyMode
,
1753 MSIRECORD
*rec
, UINT row
)
1755 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1756 UINT r
, frow
, column
;
1758 TRACE("%p %d %p\n", view
, eModifyMode
, rec
);
1760 switch (eModifyMode
)
1762 case MSIMODIFY_DELETE
:
1763 r
= modify_delete_row( view
, rec
);
1765 case MSIMODIFY_VALIDATE_NEW
:
1766 r
= table_validate_new( tv
, rec
, &column
);
1767 if (r
!= ERROR_SUCCESS
)
1769 tv
->view
.error
= MSIDBERROR_DUPLICATEKEY
;
1770 tv
->view
.error_column
= tv
->columns
[column
].colname
;
1771 r
= ERROR_INVALID_DATA
;
1775 case MSIMODIFY_INSERT
:
1776 r
= table_validate_new( tv
, rec
, NULL
);
1777 if (r
!= ERROR_SUCCESS
)
1779 r
= TABLE_insert_row( view
, rec
, -1, FALSE
);
1782 case MSIMODIFY_INSERT_TEMPORARY
:
1783 r
= table_validate_new( tv
, rec
, NULL
);
1784 if (r
!= ERROR_SUCCESS
)
1786 r
= TABLE_insert_row( view
, rec
, -1, TRUE
);
1789 case MSIMODIFY_REFRESH
:
1790 r
= msi_refresh_record( view
, rec
, row
);
1793 case MSIMODIFY_UPDATE
:
1794 r
= msi_table_update( view
, rec
, row
);
1797 case MSIMODIFY_ASSIGN
:
1798 r
= msi_table_assign( view
, rec
);
1801 case MSIMODIFY_MERGE
:
1802 /* check row that matches this record */
1803 r
= msi_table_find_row( tv
, rec
, &frow
, &column
);
1804 if (r
!= ERROR_SUCCESS
)
1806 r
= table_validate_new( tv
, rec
, NULL
);
1807 if (r
== ERROR_SUCCESS
)
1808 r
= TABLE_insert_row( view
, rec
, -1, FALSE
);
1812 case MSIMODIFY_REPLACE
:
1813 case MSIMODIFY_VALIDATE
:
1814 case MSIMODIFY_VALIDATE_FIELD
:
1815 case MSIMODIFY_VALIDATE_DELETE
:
1816 FIXME("%p %d %p - mode not implemented\n", view
, eModifyMode
, rec
);
1817 r
= ERROR_CALL_NOT_IMPLEMENTED
;
1821 r
= ERROR_INVALID_DATA
;
1827 static UINT
TABLE_delete( struct tagMSIVIEW
*view
)
1829 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1831 TRACE("%p\n", view
);
1838 return ERROR_SUCCESS
;
1841 static UINT
TABLE_find_matching_rows( struct tagMSIVIEW
*view
, UINT col
,
1842 UINT val
, UINT
*row
, MSIITERHANDLE
*handle
)
1844 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1845 const MSICOLUMNHASHENTRY
*entry
;
1847 TRACE("%p, %d, %u, %p\n", view
, col
, val
, *handle
);
1850 return ERROR_INVALID_PARAMETER
;
1852 if( (col
==0) || (col
> tv
->num_cols
) )
1853 return ERROR_INVALID_PARAMETER
;
1855 if( !tv
->columns
[col
-1].hash_table
)
1858 UINT num_rows
= tv
->table
->row_count
;
1859 MSICOLUMNHASHENTRY
**hash_table
;
1860 MSICOLUMNHASHENTRY
*new_entry
;
1862 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1864 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1865 ERR("%p %p\n", tv
, tv
->columns
);
1866 return ERROR_FUNCTION_FAILED
;
1869 /* allocate contiguous memory for the table and its entries so we
1870 * don't have to do an expensive cleanup */
1871 hash_table
= msi_alloc(MSITABLE_HASH_TABLE_SIZE
* sizeof(MSICOLUMNHASHENTRY
*) +
1872 num_rows
* sizeof(MSICOLUMNHASHENTRY
));
1874 return ERROR_OUTOFMEMORY
;
1876 memset(hash_table
, 0, MSITABLE_HASH_TABLE_SIZE
* sizeof(MSICOLUMNHASHENTRY
*));
1877 tv
->columns
[col
-1].hash_table
= hash_table
;
1879 new_entry
= (MSICOLUMNHASHENTRY
*)(hash_table
+ MSITABLE_HASH_TABLE_SIZE
);
1881 for (i
= 0; i
< num_rows
; i
++, new_entry
++)
1885 if (view
->ops
->fetch_int( view
, i
, col
, &row_value
) != ERROR_SUCCESS
)
1888 new_entry
->next
= NULL
;
1889 new_entry
->value
= row_value
;
1891 if (hash_table
[row_value
% MSITABLE_HASH_TABLE_SIZE
])
1893 MSICOLUMNHASHENTRY
*prev_entry
= hash_table
[row_value
% MSITABLE_HASH_TABLE_SIZE
];
1894 while (prev_entry
->next
)
1895 prev_entry
= prev_entry
->next
;
1896 prev_entry
->next
= new_entry
;
1899 hash_table
[row_value
% MSITABLE_HASH_TABLE_SIZE
] = new_entry
;
1904 entry
= tv
->columns
[col
-1].hash_table
[val
% MSITABLE_HASH_TABLE_SIZE
];
1906 entry
= (*handle
)->next
;
1908 while (entry
&& entry
->value
!= val
)
1909 entry
= entry
->next
;
1913 return ERROR_NO_MORE_ITEMS
;
1917 return ERROR_SUCCESS
;
1920 static UINT
TABLE_add_ref(struct tagMSIVIEW
*view
)
1922 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1925 TRACE("%p %d\n", view
, tv
->table
->ref_count
);
1927 for (i
= 0; i
< tv
->table
->col_count
; i
++)
1929 if (tv
->table
->colinfo
[i
].type
& MSITYPE_TEMPORARY
)
1930 InterlockedIncrement(&tv
->table
->colinfo
[i
].ref_count
);
1933 return InterlockedIncrement(&tv
->table
->ref_count
);
1936 static UINT
TABLE_remove_column(struct tagMSIVIEW
*view
, LPCWSTR table
, UINT number
)
1938 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1939 MSIRECORD
*rec
= NULL
;
1940 MSIVIEW
*columns
= NULL
;
1943 rec
= MSI_CreateRecord(2);
1945 return ERROR_OUTOFMEMORY
;
1947 MSI_RecordSetStringW(rec
, 1, table
);
1948 MSI_RecordSetInteger(rec
, 2, number
);
1950 r
= TABLE_CreateView(tv
->db
, szColumns
, &columns
);
1951 if (r
!= ERROR_SUCCESS
)
1953 msiobj_release(&rec
->hdr
);
1957 r
= msi_table_find_row((MSITABLEVIEW
*)columns
, rec
, &row
, NULL
);
1958 if (r
!= ERROR_SUCCESS
)
1961 r
= TABLE_delete_row(columns
, row
);
1962 if (r
!= ERROR_SUCCESS
)
1965 msi_update_table_columns(tv
->db
, table
);
1968 msiobj_release(&rec
->hdr
);
1969 columns
->ops
->delete(columns
);
1973 static UINT
TABLE_release(struct tagMSIVIEW
*view
)
1975 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1976 INT ref
= tv
->table
->ref_count
;
1979 TRACE("%p %d\n", view
, ref
);
1981 for (i
= 0; i
< tv
->table
->col_count
; i
++)
1983 if (tv
->table
->colinfo
[i
].type
& MSITYPE_TEMPORARY
)
1985 ref
= InterlockedDecrement(&tv
->table
->colinfo
[i
].ref_count
);
1988 r
= TABLE_remove_column(view
, tv
->table
->colinfo
[i
].tablename
,
1989 tv
->table
->colinfo
[i
].number
);
1990 if (r
!= ERROR_SUCCESS
)
1996 ref
= InterlockedDecrement(&tv
->table
->ref_count
);
1999 if (!tv
->table
->row_count
)
2001 list_remove(&tv
->table
->entry
);
2002 free_table(tv
->table
);
2010 static UINT
TABLE_add_column(struct tagMSIVIEW
*view
, LPCWSTR table
, UINT number
,
2011 LPCWSTR column
, UINT type
, BOOL hold
)
2013 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
2018 rec
= MSI_CreateRecord(4);
2020 return ERROR_OUTOFMEMORY
;
2022 MSI_RecordSetStringW(rec
, 1, table
);
2023 MSI_RecordSetInteger(rec
, 2, number
);
2024 MSI_RecordSetStringW(rec
, 3, column
);
2025 MSI_RecordSetInteger(rec
, 4, type
);
2027 r
= TABLE_insert_row(&tv
->view
, rec
, -1, FALSE
);
2028 if (r
!= ERROR_SUCCESS
)
2031 msi_update_table_columns(tv
->db
, table
);
2036 msitable
= find_cached_table(tv
->db
, table
);
2037 for (i
= 0; i
< msitable
->col_count
; i
++)
2039 if (!strcmpW( msitable
->colinfo
[i
].colname
, column
))
2041 InterlockedIncrement(&msitable
->colinfo
[i
].ref_count
);
2047 msiobj_release(&rec
->hdr
);
2051 static UINT
TABLE_drop(struct tagMSIVIEW
*view
)
2053 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
2054 MSIVIEW
*tables
= NULL
;
2055 MSIRECORD
*rec
= NULL
;
2059 TRACE("dropping table %s\n", debugstr_w(tv
->name
));
2061 for (i
= tv
->table
->col_count
- 1; i
>= 0; i
--)
2063 r
= TABLE_remove_column(view
, tv
->table
->colinfo
[i
].tablename
,
2064 tv
->table
->colinfo
[i
].number
);
2065 if (r
!= ERROR_SUCCESS
)
2069 rec
= MSI_CreateRecord(1);
2071 return ERROR_OUTOFMEMORY
;
2073 MSI_RecordSetStringW(rec
, 1, tv
->name
);
2075 r
= TABLE_CreateView(tv
->db
, szTables
, &tables
);
2076 if (r
!= ERROR_SUCCESS
)
2078 msiobj_release(&rec
->hdr
);
2082 r
= msi_table_find_row((MSITABLEVIEW
*)tables
, rec
, &row
, NULL
);
2083 if (r
!= ERROR_SUCCESS
)
2086 r
= TABLE_delete_row(tables
, row
);
2087 if (r
!= ERROR_SUCCESS
)
2090 list_remove(&tv
->table
->entry
);
2091 free_table(tv
->table
);
2094 msiobj_release(&rec
->hdr
);
2095 tables
->ops
->delete(tables
);
2100 static const MSIVIEWOPS table_ops
=
2110 TABLE_get_dimensions
,
2111 TABLE_get_column_info
,
2114 TABLE_find_matching_rows
,
2118 TABLE_remove_column
,
2123 UINT
TABLE_CreateView( MSIDATABASE
*db
, LPCWSTR name
, MSIVIEW
**view
)
2128 TRACE("%p %s %p\n", db
, debugstr_w(name
), view
);
2130 if ( !strcmpW( name
, szStreams
) )
2131 return STREAMS_CreateView( db
, view
);
2132 else if ( !strcmpW( name
, szStorages
) )
2133 return STORAGES_CreateView( db
, view
);
2135 sz
= FIELD_OFFSET( MSITABLEVIEW
, name
[lstrlenW( name
) + 1] );
2136 tv
= msi_alloc_zero( sz
);
2138 return ERROR_FUNCTION_FAILED
;
2140 r
= get_table( db
, name
, &tv
->table
);
2141 if( r
!= ERROR_SUCCESS
)
2144 WARN("table not found\n");
2148 TRACE("table %p found with %d columns\n", tv
->table
, tv
->table
->col_count
);
2150 /* fill the structure */
2151 tv
->view
.ops
= &table_ops
;
2153 tv
->columns
= tv
->table
->colinfo
;
2154 tv
->num_cols
= tv
->table
->col_count
;
2155 tv
->row_size
= msi_table_get_row_size( db
, tv
->table
->colinfo
, tv
->table
->col_count
, LONG_STR_BYTES
);
2157 TRACE("%s one row is %d bytes\n", debugstr_w(name
), tv
->row_size
);
2159 *view
= (MSIVIEW
*) tv
;
2160 lstrcpyW( tv
->name
, name
);
2162 return ERROR_SUCCESS
;
2165 UINT
MSI_CommitTables( MSIDATABASE
*db
)
2167 UINT r
, bytes_per_strref
;
2169 MSITABLE
*table
= NULL
;
2173 r
= msi_save_string_table( db
->strings
, db
->storage
, &bytes_per_strref
);
2174 if( r
!= ERROR_SUCCESS
)
2176 WARN("failed to save string table r=%08x\n",r
);
2180 LIST_FOR_EACH_ENTRY( table
, &db
->tables
, MSITABLE
, entry
)
2182 r
= save_table( db
, table
, bytes_per_strref
);
2183 if( r
!= ERROR_SUCCESS
)
2185 WARN("failed to save table %s (r=%08x)\n",
2186 debugstr_w(table
->name
), r
);
2191 hr
= IStorage_Commit( db
->storage
, 0 );
2194 WARN("failed to commit changes 0x%08x\n", hr
);
2195 r
= ERROR_FUNCTION_FAILED
;
2200 MSICONDITION
MSI_DatabaseIsTablePersistent( MSIDATABASE
*db
, LPCWSTR table
)
2205 TRACE("%p %s\n", db
, debugstr_w(table
));
2208 return MSICONDITION_ERROR
;
2210 r
= get_table( db
, table
, &t
);
2211 if (r
!= ERROR_SUCCESS
)
2212 return MSICONDITION_NONE
;
2214 return t
->persistent
;
2217 static UINT
read_raw_int(const BYTE
*data
, UINT col
, UINT bytes
)
2221 for (i
= 0; i
< bytes
; i
++)
2222 ret
+= (data
[col
+ i
] << i
* 8);
2227 static UINT
msi_record_encoded_stream_name( const MSITABLEVIEW
*tv
, MSIRECORD
*rec
, LPWSTR
*pstname
)
2229 LPWSTR stname
= NULL
, sval
, p
;
2233 TRACE("%p %p\n", tv
, rec
);
2235 len
= lstrlenW( tv
->name
) + 1;
2236 stname
= msi_alloc( len
*sizeof(WCHAR
) );
2239 r
= ERROR_OUTOFMEMORY
;
2243 lstrcpyW( stname
, tv
->name
);
2245 for ( i
= 0; i
< tv
->num_cols
; i
++ )
2247 if ( tv
->columns
[i
].type
& MSITYPE_KEY
)
2249 sval
= msi_dup_record_field( rec
, i
+ 1 );
2252 r
= ERROR_OUTOFMEMORY
;
2256 len
+= lstrlenW( szDot
) + lstrlenW ( sval
);
2257 p
= msi_realloc ( stname
, len
*sizeof(WCHAR
) );
2260 r
= ERROR_OUTOFMEMORY
;
2266 lstrcatW( stname
, szDot
);
2267 lstrcatW( stname
, sval
);
2275 *pstname
= encode_streamname( FALSE
, stname
);
2278 return ERROR_SUCCESS
;
2281 msi_free ( stname
);
2286 static MSIRECORD
*msi_get_transform_record( const MSITABLEVIEW
*tv
, const string_table
*st
,
2288 const BYTE
*rawdata
, UINT bytes_per_strref
)
2290 UINT i
, val
, ofs
= 0;
2292 MSICOLUMNINFO
*columns
= tv
->columns
;
2295 mask
= rawdata
[0] | (rawdata
[1] << 8);
2298 rec
= MSI_CreateRecord( tv
->num_cols
);
2303 for( i
=0; i
<tv
->num_cols
; i
++ )
2305 if ( (mask
&1) && (i
>=(mask
>>8)) )
2307 /* all keys must be present */
2308 if ( (~mask
&1) && (~columns
[i
].type
& MSITYPE_KEY
) && ((1<<i
) & ~mask
) )
2311 if( MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
2314 IStream
*stm
= NULL
;
2317 ofs
+= bytes_per_column( tv
->db
, &columns
[i
], bytes_per_strref
);
2319 r
= msi_record_encoded_stream_name( tv
, rec
, &encname
);
2320 if ( r
!= ERROR_SUCCESS
)
2323 r
= IStorage_OpenStream( stg
, encname
, NULL
,
2324 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, &stm
);
2325 if ( r
!= ERROR_SUCCESS
)
2327 msi_free( encname
);
2331 MSI_RecordSetStream( rec
, i
+1, stm
);
2332 TRACE(" field %d [%s]\n", i
+1, debugstr_w(encname
));
2333 msi_free( encname
);
2335 else if( columns
[i
].type
& MSITYPE_STRING
)
2340 val
= read_raw_int(rawdata
, ofs
, bytes_per_strref
);
2341 sval
= msi_string_lookup( st
, val
, &len
);
2342 msi_record_set_string( rec
, i
+1, sval
, len
);
2343 TRACE(" field %d [%s]\n", i
+1, debugstr_wn(sval
, len
));
2344 ofs
+= bytes_per_strref
;
2348 UINT n
= bytes_per_column( tv
->db
, &columns
[i
], bytes_per_strref
);
2352 val
= read_raw_int(rawdata
, ofs
, n
);
2354 MSI_RecordSetInteger( rec
, i
+1, val
-0x8000 );
2355 TRACE(" field %d [0x%04x]\n", i
+1, val
);
2358 val
= read_raw_int(rawdata
, ofs
, n
);
2360 MSI_RecordSetInteger( rec
, i
+1, val
^0x80000000 );
2361 TRACE(" field %d [0x%08x]\n", i
+1, val
);
2364 ERR("oops - unknown column width %d\n", n
);
2373 static void dump_record( MSIRECORD
*rec
)
2377 n
= MSI_RecordGetFieldCount( rec
);
2378 for( i
=1; i
<=n
; i
++ )
2383 if( MSI_RecordIsNull( rec
, i
) )
2384 TRACE("row -> []\n");
2385 else if( (sval
= msi_record_get_string( rec
, i
, &len
)) )
2386 TRACE("row -> [%s]\n", debugstr_wn(sval
, len
));
2388 TRACE("row -> [0x%08x]\n", MSI_RecordGetInteger( rec
, i
) );
2392 static void dump_table( const string_table
*st
, const USHORT
*rawdata
, UINT rawsize
)
2395 for (i
= 0; i
< rawsize
/ 2; i
++)
2398 const WCHAR
*sval
= msi_string_lookup( st
, rawdata
[i
], &len
);
2399 MESSAGE(" %04x %s\n", rawdata
[i
], debugstr_wn(sval
, len
) );
2403 static UINT
* msi_record_to_row( const MSITABLEVIEW
*tv
, MSIRECORD
*rec
)
2407 data
= msi_alloc( tv
->num_cols
*sizeof (UINT
) );
2408 for( i
=0; i
<tv
->num_cols
; i
++ )
2412 if ( ~tv
->columns
[i
].type
& MSITYPE_KEY
)
2415 /* turn the transform column value into a row value */
2416 if ( ( tv
->columns
[i
].type
& MSITYPE_STRING
) &&
2417 ! MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
2420 const WCHAR
*str
= msi_record_get_string( rec
, i
+1, &len
);
2423 r
= msi_string2id( tv
->db
->strings
, str
, len
, &data
[i
] );
2425 /* if there's no matching string in the string table,
2426 these keys can't match any record, so fail now. */
2427 if (r
!= ERROR_SUCCESS
)
2437 data
[i
] = MSI_RecordGetInteger( rec
, i
+1 );
2439 if (data
[i
] == MSI_NULL_INTEGER
)
2441 else if ((tv
->columns
[i
].type
&0xff) == 2)
2444 data
[i
] += 0x80000000;
2450 static UINT
msi_row_matches( MSITABLEVIEW
*tv
, UINT row
, const UINT
*data
, UINT
*column
)
2452 UINT i
, r
, x
, ret
= ERROR_FUNCTION_FAILED
;
2454 for( i
=0; i
<tv
->num_cols
; i
++ )
2456 if ( ~tv
->columns
[i
].type
& MSITYPE_KEY
)
2459 /* turn the transform column value into a row value */
2460 r
= TABLE_fetch_int( &tv
->view
, row
, i
+1, &x
);
2461 if ( r
!= ERROR_SUCCESS
)
2463 ERR("TABLE_fetch_int shouldn't fail here\n");
2467 /* if this key matches, move to the next column */
2470 ret
= ERROR_FUNCTION_FAILED
;
2473 if (column
) *column
= i
;
2474 ret
= ERROR_SUCCESS
;
2479 static UINT
msi_table_find_row( MSITABLEVIEW
*tv
, MSIRECORD
*rec
, UINT
*row
, UINT
*column
)
2481 UINT i
, r
= ERROR_FUNCTION_FAILED
, *data
;
2483 data
= msi_record_to_row( tv
, rec
);
2486 for( i
= 0; i
< tv
->table
->row_count
; i
++ )
2488 r
= msi_row_matches( tv
, i
, data
, column
);
2489 if( r
== ERROR_SUCCESS
)
2505 static UINT
msi_table_load_transform( MSIDATABASE
*db
, IStorage
*stg
,
2506 string_table
*st
, TRANSFORMDATA
*transform
,
2507 UINT bytes_per_strref
)
2509 BYTE
*rawdata
= NULL
;
2510 MSITABLEVIEW
*tv
= NULL
;
2511 UINT r
, n
, sz
, i
, mask
, num_cols
, colcol
= 0, rawsize
= 0;
2512 MSIRECORD
*rec
= NULL
;
2517 return ERROR_SUCCESS
;
2519 name
= transform
->name
;
2522 TRACE("%p %p %p %s\n", db
, stg
, st
, debugstr_w(name
) );
2524 /* read the transform data */
2525 read_stream_data( stg
, name
, TRUE
, &rawdata
, &rawsize
);
2528 TRACE("table %s empty\n", debugstr_w(name
) );
2529 return ERROR_INVALID_TABLE
;
2532 /* create a table view */
2533 r
= TABLE_CreateView( db
, name
, (MSIVIEW
**) &tv
);
2534 if( r
!= ERROR_SUCCESS
)
2537 r
= tv
->view
.ops
->execute( &tv
->view
, NULL
);
2538 if( r
!= ERROR_SUCCESS
)
2541 TRACE("name = %s columns = %u row_size = %u raw size = %u\n",
2542 debugstr_w(name
), tv
->num_cols
, tv
->row_size
, rawsize
);
2544 /* interpret the data */
2545 for (n
= 0; n
< rawsize
;)
2547 mask
= rawdata
[n
] | (rawdata
[n
+ 1] << 8);
2551 * if the low bit is set, columns are continuous and
2552 * the number of columns is specified in the high byte
2555 num_cols
= mask
>> 8;
2556 if (num_cols
> tv
->num_cols
)
2558 ERR("excess columns in transform: %u > %u\n", num_cols
, tv
->num_cols
);
2562 for (i
= 0; i
< num_cols
; i
++)
2564 if( (tv
->columns
[i
].type
& MSITYPE_STRING
) &&
2565 ! MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
2566 sz
+= bytes_per_strref
;
2568 sz
+= bytes_per_column( tv
->db
, &tv
->columns
[i
], bytes_per_strref
);
2574 * If the low bit is not set, mask is a bitmask.
2575 * Excepting for key fields, which are always present,
2576 * each bit indicates that a field is present in the transform record.
2578 * mask == 0 is a special case ... only the keys will be present
2579 * and it means that this row should be deleted.
2582 num_cols
= tv
->num_cols
;
2583 for (i
= 0; i
< num_cols
; i
++)
2585 if ((tv
->columns
[i
].type
& MSITYPE_KEY
) || ((1 << i
) & mask
))
2587 if ((tv
->columns
[i
].type
& MSITYPE_STRING
) &&
2588 !MSITYPE_IS_BINARY(tv
->columns
[i
].type
))
2589 sz
+= bytes_per_strref
;
2591 sz
+= bytes_per_column( tv
->db
, &tv
->columns
[i
], bytes_per_strref
);
2596 /* check we didn't run of the end of the table */
2597 if (n
+ sz
> rawsize
)
2600 dump_table( st
, (USHORT
*)rawdata
, rawsize
);
2604 rec
= msi_get_transform_record( tv
, st
, stg
, &rawdata
[n
], bytes_per_strref
);
2609 UINT number
= MSI_NULL_INTEGER
;
2612 if (!strcmpW( name
, szColumns
))
2614 MSI_RecordGetStringW( rec
, 1, table
, &sz
);
2615 number
= MSI_RecordGetInteger( rec
, 2 );
2618 * Native msi seems writes nul into the Number (2nd) column of
2619 * the _Columns table when there are new columns
2621 if ( number
== MSI_NULL_INTEGER
)
2623 /* reset the column number on a new table */
2624 if (strcmpW( coltable
, table
))
2627 lstrcpyW( coltable
, table
);
2630 /* fix nul column numbers */
2631 MSI_RecordSetInteger( rec
, 2, ++colcol
);
2635 if (TRACE_ON(msidb
)) dump_record( rec
);
2637 r
= msi_table_find_row( tv
, rec
, &row
, NULL
);
2638 if (r
== ERROR_SUCCESS
)
2642 TRACE("deleting row [%d]:\n", row
);
2643 r
= TABLE_delete_row( &tv
->view
, row
);
2644 if (r
!= ERROR_SUCCESS
)
2645 WARN("failed to delete row %u\n", r
);
2649 TRACE("modifying full row [%d]:\n", row
);
2650 r
= TABLE_set_row( &tv
->view
, row
, rec
, (1 << tv
->num_cols
) - 1 );
2651 if (r
!= ERROR_SUCCESS
)
2652 WARN("failed to modify row %u\n", r
);
2656 TRACE("modifying masked row [%d]:\n", row
);
2657 r
= TABLE_set_row( &tv
->view
, row
, rec
, mask
);
2658 if (r
!= ERROR_SUCCESS
)
2659 WARN("failed to modify row %u\n", r
);
2664 TRACE("inserting row\n");
2665 r
= TABLE_insert_row( &tv
->view
, rec
, -1, FALSE
);
2666 if (r
!= ERROR_SUCCESS
)
2667 WARN("failed to insert row %u\n", r
);
2670 if (!strcmpW( name
, szColumns
))
2671 msi_update_table_columns( db
, table
);
2673 msiobj_release( &rec
->hdr
);
2680 /* no need to free the table, it's associated with the database */
2681 msi_free( rawdata
);
2683 tv
->view
.ops
->delete( &tv
->view
);
2685 return ERROR_SUCCESS
;
2689 * msi_table_apply_transform
2691 * Enumerate the table transforms in a transform storage and apply each one.
2693 UINT
msi_table_apply_transform( MSIDATABASE
*db
, IStorage
*stg
)
2695 struct list transforms
;
2696 IEnumSTATSTG
*stgenum
= NULL
;
2697 TRANSFORMDATA
*transform
;
2698 TRANSFORMDATA
*tables
= NULL
, *columns
= NULL
;
2701 string_table
*strings
;
2702 UINT ret
= ERROR_FUNCTION_FAILED
;
2703 UINT bytes_per_strref
;
2704 BOOL property_update
= FALSE
;
2706 TRACE("%p %p\n", db
, stg
);
2708 strings
= msi_load_string_table( stg
, &bytes_per_strref
);
2712 r
= IStorage_EnumElements( stg
, 0, NULL
, 0, &stgenum
);
2716 list_init(&transforms
);
2720 MSITABLEVIEW
*tv
= NULL
;
2724 r
= IEnumSTATSTG_Next( stgenum
, 1, &stat
, &count
);
2725 if ( FAILED( r
) || !count
)
2728 decode_streamname( stat
.pwcsName
, name
);
2729 CoTaskMemFree( stat
.pwcsName
);
2730 if ( name
[0] != 0x4840 )
2733 if ( !strcmpW( name
+1, szStringPool
) ||
2734 !strcmpW( name
+1, szStringData
) )
2737 transform
= msi_alloc_zero( sizeof(TRANSFORMDATA
) );
2741 list_add_tail( &transforms
, &transform
->entry
);
2743 transform
->name
= strdupW( name
+ 1 );
2745 if ( !strcmpW( transform
->name
, szTables
) )
2747 else if (!strcmpW( transform
->name
, szColumns
) )
2748 columns
= transform
;
2749 else if (!strcmpW( transform
->name
, szProperty
))
2750 property_update
= TRUE
;
2752 TRACE("transform contains stream %s\n", debugstr_w(name
));
2754 /* load the table */
2755 r
= TABLE_CreateView( db
, transform
->name
, (MSIVIEW
**) &tv
);
2756 if( r
!= ERROR_SUCCESS
)
2759 r
= tv
->view
.ops
->execute( &tv
->view
, NULL
);
2760 if( r
!= ERROR_SUCCESS
)
2762 tv
->view
.ops
->delete( &tv
->view
);
2766 tv
->view
.ops
->delete( &tv
->view
);
2770 * Apply _Tables and _Columns transforms first so that
2771 * the table metadata is correct, and empty tables exist.
2773 ret
= msi_table_load_transform( db
, stg
, strings
, tables
, bytes_per_strref
);
2774 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_INVALID_TABLE
)
2777 ret
= msi_table_load_transform( db
, stg
, strings
, columns
, bytes_per_strref
);
2778 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_INVALID_TABLE
)
2781 ret
= ERROR_SUCCESS
;
2783 while ( !list_empty( &transforms
) )
2785 transform
= LIST_ENTRY( list_head( &transforms
), TRANSFORMDATA
, entry
);
2787 if ( strcmpW( transform
->name
, szColumns
) &&
2788 strcmpW( transform
->name
, szTables
) &&
2789 ret
== ERROR_SUCCESS
)
2791 ret
= msi_table_load_transform( db
, stg
, strings
, transform
, bytes_per_strref
);
2794 list_remove( &transform
->entry
);
2795 msi_free( transform
->name
);
2796 msi_free( transform
);
2799 if ( ret
== ERROR_SUCCESS
)
2801 append_storage_to_db( db
, stg
);
2802 if (property_update
) msi_clone_properties( db
);
2807 IEnumSTATSTG_Release( stgenum
);
2809 msi_destroy_stringtable( strings
);