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
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(msidb
);
41 #define MSITABLE_HASH_TABLE_SIZE 37
43 typedef struct tagMSICOLUMNHASHENTRY
45 struct tagMSICOLUMNHASHENTRY
*next
;
50 typedef struct tagMSICOLUMNINFO
59 MSICOLUMNHASHENTRY
**hash_table
;
65 BOOL
*data_persistent
;
68 MSICOLUMNINFO
*colinfo
;
70 MSICONDITION persistent
;
75 /* information for default tables */
76 static const WCHAR szTables
[] = {'_','T','a','b','l','e','s',0};
77 static const WCHAR szTable
[] = {'T','a','b','l','e',0};
78 static const WCHAR szColumns
[] = {'_','C','o','l','u','m','n','s',0};
79 static const WCHAR szNumber
[] = {'N','u','m','b','e','r',0};
80 static const WCHAR szType
[] = {'T','y','p','e',0};
82 static const MSICOLUMNINFO _Columns_cols
[4] = {
83 { szColumns
, 1, szTable
, MSITYPE_VALID
| MSITYPE_STRING
| MSITYPE_KEY
| 64, 0, 0, 0, NULL
},
84 { szColumns
, 2, szNumber
, MSITYPE_VALID
| MSITYPE_KEY
| 2, 2, 0, 0, NULL
},
85 { szColumns
, 3, szName
, MSITYPE_VALID
| MSITYPE_STRING
| 64, 4, 0, 0, NULL
},
86 { szColumns
, 4, szType
, MSITYPE_VALID
| 2, 6, 0, 0, NULL
},
89 static const MSICOLUMNINFO _Tables_cols
[1] = {
90 { szTables
, 1, szName
, MSITYPE_VALID
| MSITYPE_STRING
| MSITYPE_KEY
| 64, 0, 0, 0, NULL
},
93 #define MAX_STREAM_NAME 0x1f
95 static inline UINT
bytes_per_column( MSIDATABASE
*db
, const MSICOLUMNINFO
*col
, UINT bytes_per_strref
)
97 if( MSITYPE_IS_BINARY(col
->type
) )
100 if( col
->type
& MSITYPE_STRING
)
101 return bytes_per_strref
;
103 if( (col
->type
& 0xff) <= 2)
106 if( (col
->type
& 0xff) != 4 )
107 ERR("Invalid column size %u\n", col
->type
& 0xff);
112 static int utf2mime(int x
)
114 if( (x
>='0') && (x
<='9') )
116 if( (x
>='A') && (x
<='Z') )
118 if( (x
>='a') && (x
<='z') )
127 LPWSTR
encode_streamname(BOOL bTable
, LPCWSTR in
)
129 DWORD count
= MAX_STREAM_NAME
;
134 count
= lstrlenW( in
)+2;
135 if (!(out
= msi_alloc( count
*sizeof(WCHAR
) ))) return NULL
;
151 if( ( ch
< 0x80 ) && ( utf2mime(ch
) >= 0 ) )
153 ch
= utf2mime(ch
) + 0x4800;
155 if( next
&& (next
<0x80) )
157 next
= utf2mime(next
);
168 ERR("Failed to encode stream name (%s)\n",debugstr_w(in
));
173 static int mime2utf(int x
)
180 return x
- 10 - 26 + 'a';
181 if( x
== (10+26+26) )
186 BOOL
decode_streamname(LPCWSTR in
, LPWSTR out
)
191 while ( (ch
= *in
++) )
193 if( (ch
>= 0x3800 ) && (ch
< 0x4840 ) )
196 ch
= mime2utf(ch
-0x4800);
200 *out
++ = mime2utf(ch
&0x3f);
202 ch
= mime2utf((ch
>>6)&0x3f);
212 void enum_stream_names( IStorage
*stg
)
214 IEnumSTATSTG
*stgenum
= NULL
;
220 r
= IStorage_EnumElements( stg
, 0, NULL
, 0, &stgenum
);
228 r
= IEnumSTATSTG_Next( stgenum
, 1, &stat
, &count
);
229 if( FAILED( r
) || !count
)
231 decode_streamname( stat
.pwcsName
, name
);
232 TRACE("stream %2d -> %s %s\n", n
,
233 debugstr_w(stat
.pwcsName
), debugstr_w(name
) );
234 CoTaskMemFree( stat
.pwcsName
);
238 IEnumSTATSTG_Release( stgenum
);
241 UINT
read_stream_data( IStorage
*stg
, LPCWSTR stname
, BOOL table
,
242 BYTE
**pdata
, UINT
*psz
)
245 UINT ret
= ERROR_FUNCTION_FAILED
;
252 encname
= encode_streamname(table
, stname
);
254 TRACE("%s -> %s\n",debugstr_w(stname
),debugstr_w(encname
));
256 r
= IStorage_OpenStream(stg
, encname
, NULL
,
257 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, &stm
);
261 WARN("open stream failed r = %08x - empty table?\n", r
);
265 r
= IStream_Stat(stm
, &stat
, STATFLAG_NONAME
);
268 WARN("open stream failed r = %08x!\n", r
);
272 if( stat
.cbSize
.QuadPart
>> 32 )
278 sz
= stat
.cbSize
.QuadPart
;
279 data
= msi_alloc( sz
);
282 WARN("couldn't allocate memory r=%08x!\n", r
);
283 ret
= ERROR_NOT_ENOUGH_MEMORY
;
287 r
= IStream_Read(stm
, data
, sz
, &count
);
288 if( FAILED( r
) || ( count
!= sz
) )
291 WARN("read stream failed r = %08x!\n", r
);
300 IStream_Release( stm
);
305 UINT
write_stream_data( IStorage
*stg
, LPCWSTR stname
,
306 LPCVOID data
, UINT sz
, BOOL bTable
)
309 UINT ret
= ERROR_FUNCTION_FAILED
;
316 encname
= encode_streamname(bTable
, stname
);
317 r
= IStorage_OpenStream( stg
, encname
, NULL
,
318 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, &stm
);
321 r
= IStorage_CreateStream( stg
, encname
,
322 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, 0, &stm
);
327 WARN("open stream failed r = %08x\n", r
);
332 r
= IStream_SetSize( stm
, size
);
335 WARN("Failed to SetSize\n");
340 r
= IStream_Seek( stm
, pos
, STREAM_SEEK_SET
, NULL
);
343 WARN("Failed to Seek\n");
349 r
= IStream_Write(stm
, data
, sz
, &count
);
350 if( FAILED( r
) || ( count
!= sz
) )
352 WARN("Failed to Write\n");
360 IStream_Release( stm
);
365 static void msi_free_colinfo( MSICOLUMNINFO
*colinfo
, UINT count
)
368 for (i
= 0; i
< count
; i
++) msi_free( colinfo
[i
].hash_table
);
371 static void free_table( MSITABLE
*table
)
374 for( i
=0; i
<table
->row_count
; i
++ )
375 msi_free( table
->data
[i
] );
376 msi_free( table
->data
);
377 msi_free( table
->data_persistent
);
378 msi_free_colinfo( table
->colinfo
, table
->col_count
);
379 msi_free( table
->colinfo
);
383 static UINT
msi_table_get_row_size( MSIDATABASE
*db
, const MSICOLUMNINFO
*cols
, UINT count
, UINT bytes_per_strref
)
385 const MSICOLUMNINFO
*last_col
;
390 if (bytes_per_strref
!= LONG_STR_BYTES
)
393 for (i
= 0; i
< count
; i
++) size
+= bytes_per_column( db
, &cols
[i
], bytes_per_strref
);
396 last_col
= &cols
[count
- 1];
397 return last_col
->offset
+ bytes_per_column( db
, last_col
, bytes_per_strref
);
400 /* add this table to the list of cached tables in the database */
401 static UINT
read_table_from_storage( MSIDATABASE
*db
, MSITABLE
*t
, IStorage
*stg
)
403 BYTE
*rawdata
= NULL
;
404 UINT rawsize
= 0, i
, j
, row_size
, row_size_mem
;
406 TRACE("%s\n",debugstr_w(t
->name
));
408 row_size
= msi_table_get_row_size( db
, t
->colinfo
, t
->col_count
, db
->bytes_per_strref
);
409 row_size_mem
= msi_table_get_row_size( db
, t
->colinfo
, t
->col_count
, LONG_STR_BYTES
);
411 /* if we can't read the table, just assume that it's empty */
412 read_stream_data( stg
, t
->name
, TRUE
, &rawdata
, &rawsize
);
414 return ERROR_SUCCESS
;
416 TRACE("Read %d bytes\n", rawsize
);
418 if( rawsize
% row_size
)
420 WARN("Table size is invalid %d/%d\n", rawsize
, row_size
);
424 if ((t
->row_count
= rawsize
/ row_size
))
426 if (!(t
->data
= msi_alloc_zero( t
->row_count
* sizeof(USHORT
*) ))) goto err
;
427 if (!(t
->data_persistent
= msi_alloc_zero( t
->row_count
* sizeof(BOOL
) ))) goto err
;
430 /* transpose all the data */
431 TRACE("Transposing data from %d rows\n", t
->row_count
);
432 for (i
= 0; i
< t
->row_count
; i
++)
434 UINT ofs
= 0, ofs_mem
= 0;
436 t
->data
[i
] = msi_alloc( row_size_mem
);
439 t
->data_persistent
[i
] = TRUE
;
441 for (j
= 0; j
< t
->col_count
; j
++)
443 UINT m
= bytes_per_column( db
, &t
->colinfo
[j
], LONG_STR_BYTES
);
444 UINT n
= bytes_per_column( db
, &t
->colinfo
[j
], db
->bytes_per_strref
);
447 if ( n
!= 2 && n
!= 3 && n
!= 4 )
449 ERR("oops - unknown column width %d\n", n
);
452 if (t
->colinfo
[j
].type
& MSITYPE_STRING
&& n
< m
)
454 for (k
= 0; k
< m
; k
++)
457 t
->data
[i
][ofs_mem
+ k
] = rawdata
[ofs
* t
->row_count
+ i
* n
+ k
];
459 t
->data
[i
][ofs_mem
+ k
] = 0;
464 for (k
= 0; k
< n
; k
++)
465 t
->data
[i
][ofs_mem
+ k
] = rawdata
[ofs
* t
->row_count
+ i
* n
+ k
];
473 return ERROR_SUCCESS
;
476 return ERROR_FUNCTION_FAILED
;
479 void free_cached_tables( MSIDATABASE
*db
)
481 while( !list_empty( &db
->tables
) )
483 MSITABLE
*t
= LIST_ENTRY( list_head( &db
->tables
), MSITABLE
, entry
);
485 list_remove( &t
->entry
);
490 static MSITABLE
*find_cached_table( MSIDATABASE
*db
, LPCWSTR name
)
494 LIST_FOR_EACH_ENTRY( t
, &db
->tables
, MSITABLE
, entry
)
495 if( !wcscmp( name
, t
->name
) )
501 static void table_calc_column_offsets( MSIDATABASE
*db
, MSICOLUMNINFO
*colinfo
, DWORD count
)
505 for (i
= 0; colinfo
&& i
< count
; i
++)
507 assert( i
+ 1 == colinfo
[i
].number
);
508 if (i
) colinfo
[i
].offset
= colinfo
[i
- 1].offset
+
509 bytes_per_column( db
, &colinfo
[i
- 1], LONG_STR_BYTES
);
510 else colinfo
[i
].offset
= 0;
512 TRACE("column %d is [%s] with type %08x ofs %d\n",
513 colinfo
[i
].number
, debugstr_w(colinfo
[i
].colname
),
514 colinfo
[i
].type
, colinfo
[i
].offset
);
518 static UINT
get_defaulttablecolumns( MSIDATABASE
*db
, LPCWSTR name
, MSICOLUMNINFO
*colinfo
, UINT
*sz
)
520 const MSICOLUMNINFO
*p
;
523 TRACE("%s\n", debugstr_w(name
));
525 if (!wcscmp( name
, szTables
))
530 else if (!wcscmp( name
, szColumns
))
535 else return ERROR_FUNCTION_FAILED
;
537 for (i
= 0; i
< n
; i
++)
539 if (colinfo
&& i
< *sz
) colinfo
[i
] = p
[i
];
540 if (colinfo
&& i
>= *sz
) break;
542 table_calc_column_offsets( db
, colinfo
, n
);
544 return ERROR_SUCCESS
;
547 static UINT
get_tablecolumns( MSIDATABASE
*db
, LPCWSTR szTableName
, MSICOLUMNINFO
*colinfo
, UINT
*sz
);
549 static UINT
table_get_column_info( MSIDATABASE
*db
, LPCWSTR name
, MSICOLUMNINFO
**pcols
, UINT
*pcount
)
551 UINT r
, column_count
= 0;
552 MSICOLUMNINFO
*columns
;
554 /* get the number of columns in this table */
556 r
= get_tablecolumns( db
, name
, NULL
, &column_count
);
557 if (r
!= ERROR_SUCCESS
)
560 *pcount
= column_count
;
562 /* if there are no columns, there's no table */
564 return ERROR_INVALID_PARAMETER
;
566 TRACE("table %s found\n", debugstr_w(name
));
568 columns
= msi_alloc( column_count
* sizeof(MSICOLUMNINFO
) );
570 return ERROR_FUNCTION_FAILED
;
572 r
= get_tablecolumns( db
, name
, columns
, &column_count
);
573 if (r
!= ERROR_SUCCESS
)
576 return ERROR_FUNCTION_FAILED
;
582 static UINT
get_table( MSIDATABASE
*db
, LPCWSTR name
, MSITABLE
**table_ret
)
587 /* first, see if the table is cached */
588 table
= find_cached_table( db
, name
);
592 return ERROR_SUCCESS
;
595 /* nonexistent tables should be interpreted as empty tables */
596 table
= msi_alloc( sizeof(MSITABLE
) + lstrlenW( name
) * sizeof(WCHAR
) );
598 return ERROR_FUNCTION_FAILED
;
600 table
->row_count
= 0;
602 table
->data_persistent
= NULL
;
603 table
->colinfo
= NULL
;
604 table
->col_count
= 0;
605 table
->persistent
= MSICONDITION_TRUE
;
606 lstrcpyW( table
->name
, name
);
608 if (!wcscmp( name
, szTables
) || !wcscmp( name
, szColumns
))
609 table
->persistent
= MSICONDITION_NONE
;
611 r
= table_get_column_info( db
, name
, &table
->colinfo
, &table
->col_count
);
612 if (r
!= ERROR_SUCCESS
)
617 r
= read_table_from_storage( db
, table
, db
->storage
);
618 if (r
!= ERROR_SUCCESS
)
623 list_add_head( &db
->tables
, &table
->entry
);
625 return ERROR_SUCCESS
;
628 static UINT
read_table_int( BYTE
*const *data
, UINT row
, UINT col
, UINT bytes
)
632 for (i
= 0; i
< bytes
; i
++)
633 ret
+= data
[row
][col
+ i
] << i
* 8;
638 static UINT
get_tablecolumns( MSIDATABASE
*db
, LPCWSTR szTableName
, MSICOLUMNINFO
*colinfo
, UINT
*sz
)
640 UINT r
, i
, n
= 0, table_id
, count
, maxcount
= *sz
;
641 MSITABLE
*table
= NULL
;
643 TRACE("%s\n", debugstr_w(szTableName
));
645 /* first check if there is a default table with that name */
646 r
= get_defaulttablecolumns( db
, szTableName
, colinfo
, sz
);
647 if (r
== ERROR_SUCCESS
&& *sz
)
650 r
= get_table( db
, szColumns
, &table
);
651 if (r
!= ERROR_SUCCESS
)
653 ERR("couldn't load _Columns table\n");
654 return ERROR_FUNCTION_FAILED
;
657 /* convert table and column names to IDs from the string table */
658 r
= msi_string2id( db
->strings
, szTableName
, -1, &table_id
);
659 if (r
!= ERROR_SUCCESS
)
661 WARN("Couldn't find id for %s\n", debugstr_w(szTableName
));
664 TRACE("Table id is %d, row count is %d\n", table_id
, table
->row_count
);
666 /* Note: _Columns table doesn't have non-persistent data */
668 /* if maxcount is non-zero, assume it's exactly right for this table */
669 if (colinfo
) memset( colinfo
, 0, maxcount
* sizeof(*colinfo
) );
670 count
= table
->row_count
;
671 for (i
= 0; i
< count
; i
++)
673 if (read_table_int( table
->data
, i
, 0, LONG_STR_BYTES
) != table_id
) continue;
676 UINT id
= read_table_int( table
->data
, i
, table
->colinfo
[2].offset
, LONG_STR_BYTES
);
677 UINT col
= read_table_int( table
->data
, i
, table
->colinfo
[1].offset
, sizeof(USHORT
) ) - (1 << 15);
679 /* check the column number is in range */
680 if (col
< 1 || col
> maxcount
)
682 ERR("column %d out of range (maxcount: %d)\n", col
, maxcount
);
685 /* check if this column was already set */
686 if (colinfo
[col
- 1].number
)
688 ERR("duplicate column %d\n", col
);
691 colinfo
[col
- 1].tablename
= msi_string_lookup( db
->strings
, table_id
, NULL
);
692 colinfo
[col
- 1].number
= col
;
693 colinfo
[col
- 1].colname
= msi_string_lookup( db
->strings
, id
, NULL
);
694 colinfo
[col
- 1].type
= read_table_int( table
->data
, i
, table
->colinfo
[3].offset
,
695 sizeof(USHORT
) ) - (1 << 15);
696 colinfo
[col
- 1].offset
= 0;
697 colinfo
[col
- 1].ref_count
= 0;
698 colinfo
[col
- 1].hash_table
= NULL
;
702 TRACE("%s has %d columns\n", debugstr_w(szTableName
), n
);
704 if (colinfo
&& n
!= maxcount
)
706 ERR("missing column in table %s\n", debugstr_w(szTableName
));
707 msi_free_colinfo( colinfo
, maxcount
);
708 return ERROR_FUNCTION_FAILED
;
710 table_calc_column_offsets( db
, colinfo
, n
);
712 return ERROR_SUCCESS
;
715 UINT
msi_create_table( MSIDATABASE
*db
, LPCWSTR name
, column_info
*col_info
,
716 MSICONDITION persistent
)
720 MSIRECORD
*rec
= NULL
;
725 /* only add tables that don't exist already */
726 if( TABLE_Exists(db
, name
) )
728 WARN("table %s exists\n", debugstr_w(name
));
729 return ERROR_BAD_QUERY_SYNTAX
;
732 table
= msi_alloc( sizeof (MSITABLE
) + lstrlenW(name
)*sizeof (WCHAR
) );
734 return ERROR_FUNCTION_FAILED
;
736 table
->ref_count
= 1;
737 table
->row_count
= 0;
739 table
->data_persistent
= NULL
;
740 table
->colinfo
= NULL
;
741 table
->col_count
= 0;
742 table
->persistent
= persistent
;
743 lstrcpyW( table
->name
, name
);
745 for( col
= col_info
; col
; col
= col
->next
)
748 table
->colinfo
= msi_alloc( table
->col_count
* sizeof(MSICOLUMNINFO
) );
752 return ERROR_FUNCTION_FAILED
;
755 for( i
= 0, col
= col_info
; col
; i
++, col
= col
->next
)
757 UINT table_id
= msi_add_string( db
->strings
, col
->table
, -1, persistent
);
758 UINT col_id
= msi_add_string( db
->strings
, col
->column
, -1, persistent
);
760 table
->colinfo
[ i
].tablename
= msi_string_lookup( db
->strings
, table_id
, NULL
);
761 table
->colinfo
[ i
].number
= i
+ 1;
762 table
->colinfo
[ i
].colname
= msi_string_lookup( db
->strings
, col_id
, NULL
);
763 table
->colinfo
[ i
].type
= col
->type
;
764 table
->colinfo
[ i
].offset
= 0;
765 table
->colinfo
[ i
].ref_count
= 0;
766 table
->colinfo
[ i
].hash_table
= NULL
;
767 table
->colinfo
[ i
].temporary
= col
->temporary
;
769 table_calc_column_offsets( db
, table
->colinfo
, table
->col_count
);
771 r
= TABLE_CreateView( db
, szTables
, &tv
);
772 TRACE("CreateView returned %x\n", r
);
779 r
= tv
->ops
->execute( tv
, 0 );
780 TRACE("tv execute returned %x\n", r
);
784 rec
= MSI_CreateRecord( 1 );
788 r
= MSI_RecordSetStringW( rec
, 1, name
);
792 r
= tv
->ops
->insert_row( tv
, rec
, -1, persistent
== MSICONDITION_FALSE
);
793 TRACE("insert_row returned %x\n", r
);
797 tv
->ops
->delete( tv
);
800 msiobj_release( &rec
->hdr
);
803 if( persistent
!= MSICONDITION_FALSE
)
805 /* add each column to the _Columns table */
806 r
= TABLE_CreateView( db
, szColumns
, &tv
);
810 r
= tv
->ops
->execute( tv
, 0 );
811 TRACE("tv execute returned %x\n", r
);
815 rec
= MSI_CreateRecord( 4 );
819 r
= MSI_RecordSetStringW( rec
, 1, name
);
824 * need to set the table, column number, col name and type
825 * for each column we enter in the table
828 for( col
= col_info
; col
; col
= col
->next
)
830 r
= MSI_RecordSetInteger( rec
, 2, nField
);
834 r
= MSI_RecordSetStringW( rec
, 3, col
->column
);
838 r
= MSI_RecordSetInteger( rec
, 4, col
->type
);
842 r
= tv
->ops
->insert_row( tv
, rec
, -1, FALSE
);
854 msiobj_release( &rec
->hdr
);
855 /* FIXME: remove values from the string table on error */
857 tv
->ops
->delete( tv
);
859 if (r
== ERROR_SUCCESS
)
860 list_add_head( &db
->tables
, &table
->entry
);
867 static UINT
save_table( MSIDATABASE
*db
, const MSITABLE
*t
, UINT bytes_per_strref
)
869 BYTE
*rawdata
= NULL
;
870 UINT rawsize
, i
, j
, row_size
, row_count
;
871 UINT r
= ERROR_FUNCTION_FAILED
;
873 /* Nothing to do for non-persistent tables */
874 if( t
->persistent
== MSICONDITION_FALSE
)
875 return ERROR_SUCCESS
;
877 TRACE("Saving %s\n", debugstr_w( t
->name
) );
879 row_size
= msi_table_get_row_size( db
, t
->colinfo
, t
->col_count
, bytes_per_strref
);
880 row_count
= t
->row_count
;
881 for (i
= 0; i
< t
->row_count
; i
++)
883 if (!t
->data_persistent
[i
])
885 row_count
= 1; /* yes, this is bizarre */
889 rawsize
= row_count
* row_size
;
890 rawdata
= msi_alloc_zero( rawsize
);
893 r
= ERROR_NOT_ENOUGH_MEMORY
;
898 for (i
= 0; i
< row_count
; i
++)
900 UINT ofs
= 0, ofs_mem
= 0;
902 if (!t
->data_persistent
[i
]) break;
904 for (j
= 0; j
< t
->col_count
; j
++)
906 UINT m
= bytes_per_column( db
, &t
->colinfo
[j
], LONG_STR_BYTES
);
907 UINT n
= bytes_per_column( db
, &t
->colinfo
[j
], bytes_per_strref
);
910 if (n
!= 2 && n
!= 3 && n
!= 4)
912 ERR("oops - unknown column width %d\n", n
);
915 if (t
->colinfo
[j
].type
& MSITYPE_STRING
&& n
< m
)
917 UINT id
= read_table_int( t
->data
, i
, ofs_mem
, LONG_STR_BYTES
);
918 if (id
> 1 << bytes_per_strref
* 8)
920 ERR("string id %u out of range\n", id
);
924 for (k
= 0; k
< n
; k
++)
926 rawdata
[ofs
* row_count
+ i
* n
+ k
] = t
->data
[i
][ofs_mem
+ k
];
934 TRACE("writing %d bytes\n", rawsize
);
935 r
= write_stream_data( db
->storage
, t
->name
, rawdata
, rawsize
, TRUE
);
942 static void msi_update_table_columns( MSIDATABASE
*db
, LPCWSTR name
)
945 UINT size
, offset
, old_count
;
948 if (!(table
= find_cached_table( db
, name
))) return;
949 old_count
= table
->col_count
;
950 msi_free_colinfo( table
->colinfo
, table
->col_count
);
951 msi_free( table
->colinfo
);
952 table
->colinfo
= NULL
;
954 table_get_column_info( db
, name
, &table
->colinfo
, &table
->col_count
);
955 if (!table
->col_count
) return;
957 size
= msi_table_get_row_size( db
, table
->colinfo
, table
->col_count
, LONG_STR_BYTES
);
958 offset
= table
->colinfo
[table
->col_count
- 1].offset
;
960 for ( n
= 0; n
< table
->row_count
; n
++ )
962 table
->data
[n
] = msi_realloc( table
->data
[n
], size
);
963 if (old_count
< table
->col_count
)
964 memset( &table
->data
[n
][offset
], 0, size
- offset
);
968 /* try to find the table name in the _Tables table */
969 BOOL
TABLE_Exists( MSIDATABASE
*db
, LPCWSTR name
)
974 if( !wcscmp( name
, szTables
) || !wcscmp( name
, szColumns
) ||
975 !wcscmp( name
, szStreams
) || !wcscmp( name
, szStorages
) )
978 r
= msi_string2id( db
->strings
, name
, -1, &table_id
);
979 if( r
!= ERROR_SUCCESS
)
981 TRACE("Couldn't find id for %s\n", debugstr_w(name
));
985 r
= get_table( db
, szTables
, &table
);
986 if( r
!= ERROR_SUCCESS
)
988 ERR("table %s not available\n", debugstr_w(szTables
));
992 for( i
= 0; i
< table
->row_count
; i
++ )
994 if( read_table_int( table
->data
, i
, 0, LONG_STR_BYTES
) == table_id
)
1001 /* below is the query interface to a table */
1003 typedef struct tagMSITABLEVIEW
1008 MSICOLUMNINFO
*columns
;
1014 static UINT
TABLE_fetch_int( struct tagMSIVIEW
*view
, UINT row
, UINT col
, UINT
*val
)
1016 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1020 return ERROR_INVALID_PARAMETER
;
1022 if( (col
==0) || (col
>tv
->num_cols
) )
1023 return ERROR_INVALID_PARAMETER
;
1025 /* how many rows are there ? */
1026 if( row
>= tv
->table
->row_count
)
1027 return ERROR_NO_MORE_ITEMS
;
1029 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1031 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1032 ERR("%p %p\n", tv
, tv
->columns
);
1033 return ERROR_FUNCTION_FAILED
;
1036 n
= bytes_per_column( tv
->db
, &tv
->columns
[col
- 1], LONG_STR_BYTES
);
1037 if (n
!= 2 && n
!= 3 && n
!= 4)
1039 ERR("oops! what is %d bytes per column?\n", n
);
1040 return ERROR_FUNCTION_FAILED
;
1043 offset
= tv
->columns
[col
-1].offset
;
1044 *val
= read_table_int(tv
->table
->data
, row
, offset
, n
);
1046 /* TRACE("Data [%d][%d] = %d\n", row, col, *val ); */
1048 return ERROR_SUCCESS
;
1051 static UINT
get_stream_name( const MSITABLEVIEW
*tv
, UINT row
, WCHAR
**pstname
)
1053 LPWSTR p
, stname
= NULL
;
1054 UINT i
, r
, type
, ival
;
1057 MSIVIEW
*view
= (MSIVIEW
*) tv
;
1059 TRACE("%p %d\n", tv
, row
);
1061 len
= lstrlenW( tv
->name
) + 1;
1062 stname
= msi_alloc( len
*sizeof(WCHAR
) );
1065 r
= ERROR_OUTOFMEMORY
;
1069 lstrcpyW( stname
, tv
->name
);
1071 for ( i
= 0; i
< tv
->num_cols
; i
++ )
1073 type
= tv
->columns
[i
].type
;
1074 if ( type
& MSITYPE_KEY
)
1078 r
= TABLE_fetch_int( view
, row
, i
+1, &ival
);
1079 if ( r
!= ERROR_SUCCESS
)
1082 if ( tv
->columns
[i
].type
& MSITYPE_STRING
)
1084 sval
= msi_string_lookup( tv
->db
->strings
, ival
, NULL
);
1087 r
= ERROR_INVALID_PARAMETER
;
1093 static const WCHAR fmt
[] = { '%','d',0 };
1094 UINT n
= bytes_per_column( tv
->db
, &tv
->columns
[i
], LONG_STR_BYTES
);
1099 swprintf( number
, ARRAY_SIZE(number
), fmt
, ival
-0x8000 );
1102 swprintf( number
, ARRAY_SIZE(number
), fmt
, ival
^0x80000000 );
1105 ERR( "oops - unknown column width %d\n", n
);
1106 r
= ERROR_FUNCTION_FAILED
;
1112 len
+= lstrlenW( szDot
) + lstrlenW( sval
);
1113 p
= msi_realloc ( stname
, len
*sizeof(WCHAR
) );
1116 r
= ERROR_OUTOFMEMORY
;
1121 lstrcatW( stname
, szDot
);
1122 lstrcatW( stname
, sval
);
1129 return ERROR_SUCCESS
;
1138 * We need a special case for streams, as we need to reference column with
1139 * the name of the stream in the same table, and the table name
1140 * which may not be available at higher levels of the query
1142 static UINT
TABLE_fetch_stream( struct tagMSIVIEW
*view
, UINT row
, UINT col
, IStream
**stm
)
1144 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1148 if( !view
->ops
->fetch_int
)
1149 return ERROR_INVALID_PARAMETER
;
1151 r
= get_stream_name( tv
, row
, &name
);
1152 if (r
!= ERROR_SUCCESS
)
1154 ERR("fetching stream, error = %u\n", r
);
1158 r
= msi_get_stream( tv
->db
, name
, stm
);
1159 if (r
!= ERROR_SUCCESS
)
1160 ERR("fetching stream %s, error = %u\n", debugstr_w(name
), r
);
1166 /* Set a table value, i.e. preadjusted integer or string ID. */
1167 static UINT
table_set_bytes( MSITABLEVIEW
*tv
, UINT row
, UINT col
, UINT val
)
1172 return ERROR_INVALID_PARAMETER
;
1174 if( (col
==0) || (col
>tv
->num_cols
) )
1175 return ERROR_INVALID_PARAMETER
;
1177 if( row
>= tv
->table
->row_count
)
1178 return ERROR_INVALID_PARAMETER
;
1180 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1182 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1183 ERR("%p %p\n", tv
, tv
->columns
);
1184 return ERROR_FUNCTION_FAILED
;
1187 msi_free( tv
->columns
[col
-1].hash_table
);
1188 tv
->columns
[col
-1].hash_table
= NULL
;
1190 n
= bytes_per_column( tv
->db
, &tv
->columns
[col
- 1], LONG_STR_BYTES
);
1191 if ( n
!= 2 && n
!= 3 && n
!= 4 )
1193 ERR("oops! what is %d bytes per column?\n", n
);
1194 return ERROR_FUNCTION_FAILED
;
1197 offset
= tv
->columns
[col
-1].offset
;
1198 for ( i
= 0; i
< n
; i
++ )
1199 tv
->table
->data
[row
][offset
+ i
] = (val
>> i
* 8) & 0xff;
1201 return ERROR_SUCCESS
;
1204 static UINT
int_to_table_storage( const MSITABLEVIEW
*tv
, UINT col
, int val
, UINT
*ret
)
1206 if ((tv
->columns
[col
-1].type
& MSI_DATASIZEMASK
) == 2)
1208 if (val
== MSI_NULL_INTEGER
)
1210 else if ((val
+ 0x8000) & 0xffff0000)
1212 ERR("value %d out of range\n", val
);
1213 return ERROR_FUNCTION_FAILED
;
1216 *ret
= val
+ 0x8000;
1219 *ret
= val
^ 0x80000000;
1221 return ERROR_SUCCESS
;
1224 static UINT
TABLE_set_int( MSIVIEW
*view
, UINT row
, UINT col
, int val
)
1226 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1229 TRACE("row %u, col %u, val %d.\n", row
, col
, val
);
1231 if ((r
= int_to_table_storage( tv
, col
, val
, &table_int
)))
1234 if (tv
->columns
[col
-1].type
& MSITYPE_KEY
)
1238 if ((r
= TABLE_fetch_int( view
, row
, col
, &key
)))
1240 if (key
!= table_int
)
1242 ERR("Cannot modify primary key %s.%s.\n",
1243 debugstr_w(tv
->table
->name
), debugstr_w(tv
->columns
[col
-1].colname
));
1244 return ERROR_FUNCTION_FAILED
;
1248 return table_set_bytes( tv
, row
, col
, table_int
);
1251 static UINT
TABLE_set_string( MSIVIEW
*view
, UINT row
, UINT col
, const WCHAR
*val
, int len
)
1253 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1257 TRACE("row %u, col %u, val %s.\n", row
, col
, debugstr_wn(val
, len
));
1259 persistent
= (tv
->table
->persistent
!= MSICONDITION_FALSE
)
1260 && tv
->table
->data_persistent
[row
];
1264 r
= msi_string2id( tv
->db
->strings
, val
, len
, &id
);
1265 if (r
!= ERROR_SUCCESS
)
1266 id
= msi_add_string( tv
->db
->strings
, val
, len
, persistent
);
1271 if (tv
->columns
[col
-1].type
& MSITYPE_KEY
)
1275 if ((r
= TABLE_fetch_int( view
, row
, col
, &key
)))
1279 ERR("Cannot modify primary key %s.%s.\n",
1280 debugstr_w(tv
->table
->name
), debugstr_w(tv
->columns
[col
-1].colname
));
1281 return ERROR_FUNCTION_FAILED
;
1285 return table_set_bytes( tv
, row
, col
, id
);
1288 static UINT
TABLE_get_row( struct tagMSIVIEW
*view
, UINT row
, MSIRECORD
**rec
)
1290 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1293 return ERROR_INVALID_PARAMETER
;
1295 return msi_view_get_row(tv
->db
, view
, row
, rec
);
1298 static UINT
add_stream( MSIDATABASE
*db
, const WCHAR
*name
, IStream
*data
)
1300 static const WCHAR insert
[] = {
1301 'I','N','S','E','R','T',' ','I','N','T','O',' ',
1302 '`','_','S','t','r','e','a','m','s','`',' ',
1303 '(','`','N','a','m','e','`',',','`','D','a','t','a','`',')',' ',
1304 'V','A','L','U','E','S',' ','(','?',',','?',')',0};
1305 static const WCHAR update
[] = {
1306 'U','P','D','A','T','E',' ','`','_','S','t','r','e','a','m','s','`',' ',
1307 'S','E','T',' ','`','D','a','t','a','`',' ','=',' ','?',' ',
1308 'W','H','E','R','E',' ','`','N','a','m','e','`',' ','=',' ','?',0};
1313 TRACE("%p %s %p\n", db
, debugstr_w(name
), data
);
1315 if (!(rec
= MSI_CreateRecord( 2 )))
1316 return ERROR_OUTOFMEMORY
;
1318 r
= MSI_RecordSetStringW( rec
, 1, name
);
1319 if (r
!= ERROR_SUCCESS
)
1322 r
= MSI_RecordSetIStream( rec
, 2, data
);
1323 if (r
!= ERROR_SUCCESS
)
1326 r
= MSI_DatabaseOpenViewW( db
, insert
, &query
);
1327 if (r
!= ERROR_SUCCESS
)
1330 r
= MSI_ViewExecute( query
, rec
);
1331 msiobj_release( &query
->hdr
);
1332 if (r
== ERROR_SUCCESS
)
1335 msiobj_release( &rec
->hdr
);
1336 if (!(rec
= MSI_CreateRecord( 2 )))
1337 return ERROR_OUTOFMEMORY
;
1339 r
= MSI_RecordSetIStream( rec
, 1, data
);
1340 if (r
!= ERROR_SUCCESS
)
1343 r
= MSI_RecordSetStringW( rec
, 2, name
);
1344 if (r
!= ERROR_SUCCESS
)
1347 r
= MSI_DatabaseOpenViewW( db
, update
, &query
);
1348 if (r
!= ERROR_SUCCESS
)
1351 r
= MSI_ViewExecute( query
, rec
);
1352 msiobj_release( &query
->hdr
);
1355 msiobj_release( &rec
->hdr
);
1359 static UINT
TABLE_set_stream( MSIVIEW
*view
, UINT row
, UINT col
, IStream
*stream
)
1361 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1365 TRACE("row %u, col %u, stream %p.\n", row
, col
, stream
);
1367 if ((r
= get_stream_name( tv
, row
- 1, &name
)))
1370 r
= add_stream( tv
->db
, name
, stream
);
1375 static UINT
get_table_value_from_record( MSITABLEVIEW
*tv
, MSIRECORD
*rec
, UINT iField
, UINT
*pvalue
)
1377 MSICOLUMNINFO columninfo
;
1380 if (!iField
|| iField
> tv
->num_cols
|| MSI_RecordIsNull( rec
, iField
))
1381 return ERROR_FUNCTION_FAILED
;
1383 columninfo
= tv
->columns
[ iField
- 1 ];
1385 if ( MSITYPE_IS_BINARY(columninfo
.type
) )
1387 *pvalue
= 1; /* refers to the first key column */
1389 else if ( columninfo
.type
& MSITYPE_STRING
)
1392 const WCHAR
*sval
= msi_record_get_string( rec
, iField
, &len
);
1395 r
= msi_string2id( tv
->db
->strings
, sval
, len
, pvalue
);
1396 if (r
!= ERROR_SUCCESS
)
1397 return ERROR_NOT_FOUND
;
1402 return int_to_table_storage( tv
, iField
, MSI_RecordGetInteger( rec
, iField
), pvalue
);
1404 return ERROR_SUCCESS
;
1407 static UINT
TABLE_set_row( struct tagMSIVIEW
*view
, UINT row
, MSIRECORD
*rec
, UINT mask
)
1409 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1410 UINT i
, val
, r
= ERROR_SUCCESS
;
1413 return ERROR_INVALID_PARAMETER
;
1415 /* test if any of the mask bits are invalid */
1416 if ( mask
>= (1<<tv
->num_cols
) )
1417 return ERROR_INVALID_PARAMETER
;
1419 for ( i
= 0; i
< tv
->num_cols
; i
++ )
1423 /* only update the fields specified in the mask */
1424 if ( !(mask
&(1<<i
)) )
1427 persistent
= (tv
->table
->persistent
!= MSICONDITION_FALSE
) &&
1428 (tv
->table
->data_persistent
[row
]);
1429 /* FIXME: should we allow updating keys? */
1432 if ( !MSI_RecordIsNull( rec
, i
+ 1 ) )
1434 r
= get_table_value_from_record (tv
, rec
, i
+ 1, &val
);
1435 if ( MSITYPE_IS_BINARY(tv
->columns
[ i
].type
) )
1440 if ( r
!= ERROR_SUCCESS
)
1441 return ERROR_FUNCTION_FAILED
;
1443 r
= MSI_RecordGetIStream( rec
, i
+ 1, &stm
);
1444 if ( r
!= ERROR_SUCCESS
)
1447 r
= get_stream_name( tv
, row
, &stname
);
1448 if ( r
!= ERROR_SUCCESS
)
1450 IStream_Release( stm
);
1454 r
= add_stream( tv
->db
, stname
, stm
);
1455 IStream_Release( stm
);
1456 msi_free ( stname
);
1458 if ( r
!= ERROR_SUCCESS
)
1461 else if ( tv
->columns
[i
].type
& MSITYPE_STRING
)
1465 if ( r
!= ERROR_SUCCESS
)
1468 const WCHAR
*sval
= msi_record_get_string( rec
, i
+ 1, &len
);
1469 val
= msi_add_string( tv
->db
->strings
, sval
, len
, persistent
);
1473 TABLE_fetch_int(&tv
->view
, row
, i
+ 1, &x
);
1480 if ( r
!= ERROR_SUCCESS
)
1481 return ERROR_FUNCTION_FAILED
;
1485 r
= table_set_bytes( tv
, row
, i
+1, val
);
1486 if ( r
!= ERROR_SUCCESS
)
1492 static UINT
table_create_new_row( struct tagMSIVIEW
*view
, UINT
*num
, BOOL temporary
)
1494 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1499 BOOL
**data_persist_ptr
;
1502 TRACE("%p %s\n", view
, temporary
? "TRUE" : "FALSE");
1505 return ERROR_INVALID_PARAMETER
;
1507 row
= msi_alloc_zero( tv
->row_size
);
1509 return ERROR_NOT_ENOUGH_MEMORY
;
1511 row_count
= &tv
->table
->row_count
;
1512 data_ptr
= &tv
->table
->data
;
1513 data_persist_ptr
= &tv
->table
->data_persistent
;
1515 *num
= tv
->table
->row_count
;
1517 sz
= (*row_count
+ 1) * sizeof (BYTE
*);
1519 p
= msi_realloc( *data_ptr
, sz
);
1521 p
= msi_alloc( sz
);
1525 return ERROR_NOT_ENOUGH_MEMORY
;
1528 sz
= (*row_count
+ 1) * sizeof (BOOL
);
1529 if( *data_persist_ptr
)
1530 b
= msi_realloc( *data_persist_ptr
, sz
);
1532 b
= msi_alloc( sz
);
1537 return ERROR_NOT_ENOUGH_MEMORY
;
1541 (*data_ptr
)[*row_count
] = row
;
1543 *data_persist_ptr
= b
;
1544 (*data_persist_ptr
)[*row_count
] = !temporary
;
1548 return ERROR_SUCCESS
;
1551 static UINT
TABLE_execute( struct tagMSIVIEW
*view
, MSIRECORD
*record
)
1553 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1555 TRACE("%p %p\n", tv
, record
);
1557 TRACE("There are %d columns\n", tv
->num_cols
);
1559 return ERROR_SUCCESS
;
1562 static UINT
TABLE_close( struct tagMSIVIEW
*view
)
1564 TRACE("%p\n", view
);
1566 return ERROR_SUCCESS
;
1569 static UINT
TABLE_get_dimensions( struct tagMSIVIEW
*view
, UINT
*rows
, UINT
*cols
)
1571 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1573 TRACE("%p %p %p\n", view
, rows
, cols
);
1576 *cols
= tv
->num_cols
;
1580 return ERROR_INVALID_PARAMETER
;
1581 *rows
= tv
->table
->row_count
;
1584 return ERROR_SUCCESS
;
1587 static UINT
TABLE_get_column_info( struct tagMSIVIEW
*view
,
1588 UINT n
, LPCWSTR
*name
, UINT
*type
, BOOL
*temporary
,
1589 LPCWSTR
*table_name
)
1591 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1593 TRACE("%p %d %p %p\n", tv
, n
, name
, type
);
1595 if( ( n
== 0 ) || ( n
> tv
->num_cols
) )
1596 return ERROR_INVALID_PARAMETER
;
1600 *name
= tv
->columns
[n
-1].colname
;
1602 return ERROR_FUNCTION_FAILED
;
1607 *table_name
= tv
->columns
[n
-1].tablename
;
1609 return ERROR_FUNCTION_FAILED
;
1613 *type
= tv
->columns
[n
-1].type
;
1616 *temporary
= tv
->columns
[n
-1].temporary
;
1618 return ERROR_SUCCESS
;
1621 static UINT
msi_table_find_row( MSITABLEVIEW
*tv
, MSIRECORD
*rec
, UINT
*row
, UINT
*column
);
1623 static UINT
table_validate_new( MSITABLEVIEW
*tv
, MSIRECORD
*rec
, UINT
*column
)
1627 /* check there are no null values where they're not allowed */
1628 for( i
= 0; i
< tv
->num_cols
; i
++ )
1630 if ( tv
->columns
[i
].type
& MSITYPE_NULLABLE
)
1633 if ( MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
1634 TRACE("skipping binary column\n");
1635 else if ( tv
->columns
[i
].type
& MSITYPE_STRING
)
1638 const WCHAR
*str
= msi_record_get_string( rec
, i
+1, &len
);
1640 if (!str
|| (!str
[0] && !len
))
1642 if (column
) *column
= i
;
1643 return ERROR_INVALID_DATA
;
1650 n
= MSI_RecordGetInteger( rec
, i
+1 );
1651 if (n
== MSI_NULL_INTEGER
)
1653 if (column
) *column
= i
;
1654 return ERROR_INVALID_DATA
;
1659 /* check there are no duplicate keys */
1660 r
= msi_table_find_row( tv
, rec
, &row
, column
);
1661 if (r
== ERROR_SUCCESS
)
1662 return ERROR_FUNCTION_FAILED
;
1664 return ERROR_SUCCESS
;
1667 static int compare_record( MSITABLEVIEW
*tv
, UINT row
, MSIRECORD
*rec
)
1669 UINT r
, i
, ivalue
, x
;
1671 for (i
= 0; i
< tv
->num_cols
; i
++ )
1673 if (!(tv
->columns
[i
].type
& MSITYPE_KEY
)) continue;
1675 r
= get_table_value_from_record( tv
, rec
, i
+ 1, &ivalue
);
1676 if (r
!= ERROR_SUCCESS
)
1679 r
= TABLE_fetch_int( &tv
->view
, row
, i
+ 1, &x
);
1680 if (r
!= ERROR_SUCCESS
)
1682 WARN("TABLE_fetch_int should not fail here %u\n", r
);
1689 else if (ivalue
== x
)
1691 if (i
< tv
->num_cols
- 1) continue;
1700 static int find_insert_index( MSITABLEVIEW
*tv
, MSIRECORD
*rec
)
1702 int idx
, c
, low
= 0, high
= tv
->table
->row_count
- 1;
1704 TRACE("%p %p\n", tv
, rec
);
1708 idx
= (low
+ high
) / 2;
1709 c
= compare_record( tv
, idx
, rec
);
1717 TRACE("found %u\n", idx
);
1721 TRACE("found %u\n", high
+ 1);
1725 static UINT
TABLE_insert_row( struct tagMSIVIEW
*view
, MSIRECORD
*rec
, UINT row
, BOOL temporary
)
1727 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1730 TRACE("%p %p %s\n", tv
, rec
, temporary
? "TRUE" : "FALSE" );
1732 /* check that the key is unique - can we find a matching row? */
1733 r
= table_validate_new( tv
, rec
, NULL
);
1734 if( r
!= ERROR_SUCCESS
)
1735 return ERROR_FUNCTION_FAILED
;
1738 row
= find_insert_index( tv
, rec
);
1740 r
= table_create_new_row( view
, &row
, temporary
);
1741 TRACE("insert_row returned %08x\n", r
);
1742 if( r
!= ERROR_SUCCESS
)
1745 /* shift the rows to make room for the new row */
1746 for (i
= tv
->table
->row_count
- 1; i
> row
; i
--)
1748 memmove(&(tv
->table
->data
[i
][0]),
1749 &(tv
->table
->data
[i
- 1][0]), tv
->row_size
);
1750 tv
->table
->data_persistent
[i
] = tv
->table
->data_persistent
[i
- 1];
1753 /* Re-set the persistence flag */
1754 tv
->table
->data_persistent
[row
] = !temporary
;
1755 return TABLE_set_row( view
, row
, rec
, (1<<tv
->num_cols
) - 1 );
1758 static UINT
TABLE_delete_row( struct tagMSIVIEW
*view
, UINT row
)
1760 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1761 UINT r
, num_rows
, num_cols
, i
;
1763 TRACE("%p %d\n", tv
, row
);
1766 return ERROR_INVALID_PARAMETER
;
1768 r
= TABLE_get_dimensions( view
, &num_rows
, &num_cols
);
1769 if ( r
!= ERROR_SUCCESS
)
1772 if ( row
>= num_rows
)
1773 return ERROR_FUNCTION_FAILED
;
1775 num_rows
= tv
->table
->row_count
;
1776 tv
->table
->row_count
--;
1778 /* reset the hash tables */
1779 for (i
= 0; i
< tv
->num_cols
; i
++)
1781 msi_free( tv
->columns
[i
].hash_table
);
1782 tv
->columns
[i
].hash_table
= NULL
;
1785 for (i
= row
+ 1; i
< num_rows
; i
++)
1787 memcpy(tv
->table
->data
[i
- 1], tv
->table
->data
[i
], tv
->row_size
);
1788 tv
->table
->data_persistent
[i
- 1] = tv
->table
->data_persistent
[i
];
1791 msi_free(tv
->table
->data
[num_rows
- 1]);
1793 return ERROR_SUCCESS
;
1796 static UINT
msi_table_update(struct tagMSIVIEW
*view
, MSIRECORD
*rec
, UINT row
)
1798 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1801 /* FIXME: MsiViewFetch should set rec index 0 to some ID that
1802 * sets the fetched record apart from other records
1806 return ERROR_INVALID_PARAMETER
;
1808 r
= msi_table_find_row(tv
, rec
, &new_row
, NULL
);
1809 if (r
!= ERROR_SUCCESS
)
1811 ERR("can't find row to modify\n");
1812 return ERROR_FUNCTION_FAILED
;
1815 /* the row cannot be changed */
1817 return ERROR_FUNCTION_FAILED
;
1819 return TABLE_set_row(view
, new_row
, rec
, (1 << tv
->num_cols
) - 1);
1822 static UINT
msi_table_assign(struct tagMSIVIEW
*view
, MSIRECORD
*rec
)
1824 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1828 return ERROR_INVALID_PARAMETER
;
1830 r
= msi_table_find_row(tv
, rec
, &row
, NULL
);
1831 if (r
== ERROR_SUCCESS
)
1832 return TABLE_set_row(view
, row
, rec
, (1 << tv
->num_cols
) - 1);
1834 return TABLE_insert_row( view
, rec
, -1, FALSE
);
1837 static UINT
msi_refresh_record( struct tagMSIVIEW
*view
, MSIRECORD
*rec
, UINT row
)
1842 r
= TABLE_get_row(view
, row
, &curr
);
1843 if (r
!= ERROR_SUCCESS
)
1846 /* Close the original record */
1847 MSI_CloseRecord(&rec
->hdr
);
1849 count
= MSI_RecordGetFieldCount(rec
);
1850 for (i
= 0; i
< count
; i
++)
1851 MSI_RecordCopyField(curr
, i
+ 1, rec
, i
+ 1);
1853 msiobj_release(&curr
->hdr
);
1854 return ERROR_SUCCESS
;
1857 static UINT
TABLE_modify( struct tagMSIVIEW
*view
, MSIMODIFY eModifyMode
,
1858 MSIRECORD
*rec
, UINT row
)
1860 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1861 UINT r
, frow
, column
;
1863 TRACE("%p %d %p\n", view
, eModifyMode
, rec
);
1865 switch (eModifyMode
)
1867 case MSIMODIFY_DELETE
:
1868 r
= TABLE_delete_row( view
, row
);
1870 case MSIMODIFY_VALIDATE_NEW
:
1871 r
= table_validate_new( tv
, rec
, &column
);
1872 if (r
!= ERROR_SUCCESS
)
1874 tv
->view
.error
= MSIDBERROR_DUPLICATEKEY
;
1875 tv
->view
.error_column
= tv
->columns
[column
].colname
;
1876 r
= ERROR_INVALID_DATA
;
1880 case MSIMODIFY_INSERT
:
1881 r
= table_validate_new( tv
, rec
, NULL
);
1882 if (r
!= ERROR_SUCCESS
)
1884 r
= TABLE_insert_row( view
, rec
, -1, FALSE
);
1887 case MSIMODIFY_INSERT_TEMPORARY
:
1888 r
= table_validate_new( tv
, rec
, NULL
);
1889 if (r
!= ERROR_SUCCESS
)
1891 r
= TABLE_insert_row( view
, rec
, -1, TRUE
);
1894 case MSIMODIFY_REFRESH
:
1895 r
= msi_refresh_record( view
, rec
, row
);
1898 case MSIMODIFY_UPDATE
:
1899 r
= msi_table_update( view
, rec
, row
);
1902 case MSIMODIFY_ASSIGN
:
1903 r
= msi_table_assign( view
, rec
);
1906 case MSIMODIFY_MERGE
:
1907 /* check row that matches this record */
1908 r
= msi_table_find_row( tv
, rec
, &frow
, &column
);
1909 if (r
!= ERROR_SUCCESS
)
1911 r
= table_validate_new( tv
, rec
, NULL
);
1912 if (r
== ERROR_SUCCESS
)
1913 r
= TABLE_insert_row( view
, rec
, -1, FALSE
);
1917 case MSIMODIFY_REPLACE
:
1918 case MSIMODIFY_VALIDATE
:
1919 case MSIMODIFY_VALIDATE_FIELD
:
1920 case MSIMODIFY_VALIDATE_DELETE
:
1921 FIXME("%p %d %p - mode not implemented\n", view
, eModifyMode
, rec
);
1922 r
= ERROR_CALL_NOT_IMPLEMENTED
;
1926 r
= ERROR_INVALID_DATA
;
1932 static UINT
TABLE_delete( struct tagMSIVIEW
*view
)
1934 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1936 TRACE("%p\n", view
);
1943 return ERROR_SUCCESS
;
1946 static UINT
TABLE_add_ref(struct tagMSIVIEW
*view
)
1948 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1951 TRACE("%p %d\n", view
, tv
->table
->ref_count
);
1953 for (i
= 0; i
< tv
->table
->col_count
; i
++)
1955 if (tv
->table
->colinfo
[i
].type
& MSITYPE_TEMPORARY
)
1956 InterlockedIncrement(&tv
->table
->colinfo
[i
].ref_count
);
1959 return InterlockedIncrement(&tv
->table
->ref_count
);
1962 static UINT
TABLE_remove_column(struct tagMSIVIEW
*view
, LPCWSTR table
, UINT number
)
1964 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1965 MSIRECORD
*rec
= NULL
;
1966 MSIVIEW
*columns
= NULL
;
1969 rec
= MSI_CreateRecord(2);
1971 return ERROR_OUTOFMEMORY
;
1973 MSI_RecordSetStringW(rec
, 1, table
);
1974 MSI_RecordSetInteger(rec
, 2, number
);
1976 r
= TABLE_CreateView(tv
->db
, szColumns
, &columns
);
1977 if (r
!= ERROR_SUCCESS
)
1979 msiobj_release(&rec
->hdr
);
1983 r
= msi_table_find_row((MSITABLEVIEW
*)columns
, rec
, &row
, NULL
);
1984 if (r
!= ERROR_SUCCESS
)
1987 r
= TABLE_delete_row(columns
, row
);
1988 if (r
!= ERROR_SUCCESS
)
1991 msi_update_table_columns(tv
->db
, table
);
1994 msiobj_release(&rec
->hdr
);
1995 columns
->ops
->delete(columns
);
1999 static UINT
TABLE_release(struct tagMSIVIEW
*view
)
2001 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
2002 INT ref
= tv
->table
->ref_count
;
2005 TRACE("%p %d\n", view
, ref
);
2007 for (i
= 0; i
< tv
->table
->col_count
; i
++)
2009 if (tv
->table
->colinfo
[i
].type
& MSITYPE_TEMPORARY
)
2011 ref
= InterlockedDecrement(&tv
->table
->colinfo
[i
].ref_count
);
2014 r
= TABLE_remove_column(view
, tv
->table
->colinfo
[i
].tablename
,
2015 tv
->table
->colinfo
[i
].number
);
2016 if (r
!= ERROR_SUCCESS
)
2022 ref
= InterlockedDecrement(&tv
->table
->ref_count
);
2025 if (!tv
->table
->row_count
)
2027 list_remove(&tv
->table
->entry
);
2028 free_table(tv
->table
);
2036 static UINT
TABLE_add_column(struct tagMSIVIEW
*view
, LPCWSTR table
, UINT number
,
2037 LPCWSTR column
, UINT type
, BOOL hold
)
2039 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
2044 rec
= MSI_CreateRecord(4);
2046 return ERROR_OUTOFMEMORY
;
2048 MSI_RecordSetStringW(rec
, 1, table
);
2049 MSI_RecordSetInteger(rec
, 2, number
);
2050 MSI_RecordSetStringW(rec
, 3, column
);
2051 MSI_RecordSetInteger(rec
, 4, type
);
2053 r
= TABLE_insert_row(&tv
->view
, rec
, -1, FALSE
);
2054 if (r
!= ERROR_SUCCESS
)
2057 msi_update_table_columns(tv
->db
, table
);
2062 msitable
= find_cached_table(tv
->db
, table
);
2063 for (i
= 0; i
< msitable
->col_count
; i
++)
2065 if (!wcscmp( msitable
->colinfo
[i
].colname
, column
))
2067 InterlockedIncrement(&msitable
->colinfo
[i
].ref_count
);
2073 msiobj_release(&rec
->hdr
);
2077 static UINT
TABLE_drop(struct tagMSIVIEW
*view
)
2079 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
2080 MSIVIEW
*tables
= NULL
;
2081 MSIRECORD
*rec
= NULL
;
2085 TRACE("dropping table %s\n", debugstr_w(tv
->name
));
2087 for (i
= tv
->table
->col_count
- 1; i
>= 0; i
--)
2089 r
= TABLE_remove_column(view
, tv
->table
->colinfo
[i
].tablename
,
2090 tv
->table
->colinfo
[i
].number
);
2091 if (r
!= ERROR_SUCCESS
)
2095 rec
= MSI_CreateRecord(1);
2097 return ERROR_OUTOFMEMORY
;
2099 MSI_RecordSetStringW(rec
, 1, tv
->name
);
2101 r
= TABLE_CreateView(tv
->db
, szTables
, &tables
);
2102 if (r
!= ERROR_SUCCESS
)
2104 msiobj_release(&rec
->hdr
);
2108 r
= msi_table_find_row((MSITABLEVIEW
*)tables
, rec
, &row
, NULL
);
2109 if (r
!= ERROR_SUCCESS
)
2112 r
= TABLE_delete_row(tables
, row
);
2113 if (r
!= ERROR_SUCCESS
)
2116 list_remove(&tv
->table
->entry
);
2117 free_table(tv
->table
);
2120 msiobj_release(&rec
->hdr
);
2121 tables
->ops
->delete(tables
);
2126 static const MSIVIEWOPS table_ops
=
2138 TABLE_get_dimensions
,
2139 TABLE_get_column_info
,
2149 UINT
TABLE_CreateView( MSIDATABASE
*db
, LPCWSTR name
, MSIVIEW
**view
)
2154 TRACE("%p %s %p\n", db
, debugstr_w(name
), view
);
2156 if ( !wcscmp( name
, szStreams
) )
2157 return STREAMS_CreateView( db
, view
);
2158 else if ( !wcscmp( name
, szStorages
) )
2159 return STORAGES_CreateView( db
, view
);
2161 sz
= FIELD_OFFSET( MSITABLEVIEW
, name
[lstrlenW( name
) + 1] );
2162 tv
= msi_alloc_zero( sz
);
2164 return ERROR_FUNCTION_FAILED
;
2166 r
= get_table( db
, name
, &tv
->table
);
2167 if( r
!= ERROR_SUCCESS
)
2170 WARN("table not found\n");
2174 TRACE("table %p found with %d columns\n", tv
->table
, tv
->table
->col_count
);
2176 /* fill the structure */
2177 tv
->view
.ops
= &table_ops
;
2179 tv
->columns
= tv
->table
->colinfo
;
2180 tv
->num_cols
= tv
->table
->col_count
;
2181 tv
->row_size
= msi_table_get_row_size( db
, tv
->table
->colinfo
, tv
->table
->col_count
, LONG_STR_BYTES
);
2183 TRACE("%s one row is %d bytes\n", debugstr_w(name
), tv
->row_size
);
2185 *view
= (MSIVIEW
*) tv
;
2186 lstrcpyW( tv
->name
, name
);
2188 return ERROR_SUCCESS
;
2191 UINT
MSI_CommitTables( MSIDATABASE
*db
)
2193 UINT r
, bytes_per_strref
;
2195 MSITABLE
*table
= NULL
;
2199 r
= msi_save_string_table( db
->strings
, db
->storage
, &bytes_per_strref
);
2200 if( r
!= ERROR_SUCCESS
)
2202 WARN("failed to save string table r=%08x\n",r
);
2206 LIST_FOR_EACH_ENTRY( table
, &db
->tables
, MSITABLE
, entry
)
2208 r
= save_table( db
, table
, bytes_per_strref
);
2209 if( r
!= ERROR_SUCCESS
)
2211 WARN("failed to save table %s (r=%08x)\n",
2212 debugstr_w(table
->name
), r
);
2217 hr
= IStorage_Commit( db
->storage
, 0 );
2220 WARN("failed to commit changes 0x%08x\n", hr
);
2221 r
= ERROR_FUNCTION_FAILED
;
2226 MSICONDITION
MSI_DatabaseIsTablePersistent( MSIDATABASE
*db
, LPCWSTR table
)
2231 TRACE("%p %s\n", db
, debugstr_w(table
));
2234 return MSICONDITION_ERROR
;
2236 r
= get_table( db
, table
, &t
);
2237 if (r
!= ERROR_SUCCESS
)
2238 return MSICONDITION_NONE
;
2240 return t
->persistent
;
2243 static UINT
read_raw_int(const BYTE
*data
, UINT col
, UINT bytes
)
2247 for (i
= 0; i
< bytes
; i
++)
2248 ret
+= (data
[col
+ i
] << i
* 8);
2253 static UINT
msi_record_encoded_stream_name( const MSITABLEVIEW
*tv
, MSIRECORD
*rec
, LPWSTR
*pstname
)
2255 LPWSTR stname
= NULL
, sval
, p
;
2259 TRACE("%p %p\n", tv
, rec
);
2261 len
= lstrlenW( tv
->name
) + 1;
2262 stname
= msi_alloc( len
*sizeof(WCHAR
) );
2265 r
= ERROR_OUTOFMEMORY
;
2269 lstrcpyW( stname
, tv
->name
);
2271 for ( i
= 0; i
< tv
->num_cols
; i
++ )
2273 if ( tv
->columns
[i
].type
& MSITYPE_KEY
)
2275 sval
= msi_dup_record_field( rec
, i
+ 1 );
2278 r
= ERROR_OUTOFMEMORY
;
2282 len
+= lstrlenW( szDot
) + lstrlenW ( sval
);
2283 p
= msi_realloc ( stname
, len
*sizeof(WCHAR
) );
2286 r
= ERROR_OUTOFMEMORY
;
2292 lstrcatW( stname
, szDot
);
2293 lstrcatW( stname
, sval
);
2301 *pstname
= encode_streamname( FALSE
, stname
);
2304 return ERROR_SUCCESS
;
2307 msi_free ( stname
);
2312 static MSIRECORD
*msi_get_transform_record( const MSITABLEVIEW
*tv
, const string_table
*st
,
2313 IStorage
*stg
, const BYTE
*rawdata
, UINT bytes_per_strref
)
2315 UINT i
, val
, ofs
= 0;
2317 MSICOLUMNINFO
*columns
= tv
->columns
;
2320 mask
= rawdata
[0] | (rawdata
[1] << 8);
2323 rec
= MSI_CreateRecord( tv
->num_cols
);
2328 for( i
=0; i
<tv
->num_cols
; i
++ )
2330 if ( (mask
&1) && (i
>=(mask
>>8)) )
2332 /* all keys must be present */
2333 if ( (~mask
&1) && (~columns
[i
].type
& MSITYPE_KEY
) && ((1<<i
) & ~mask
) )
2336 if( MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
2339 IStream
*stm
= NULL
;
2342 ofs
+= bytes_per_column( tv
->db
, &columns
[i
], bytes_per_strref
);
2344 r
= msi_record_encoded_stream_name( tv
, rec
, &encname
);
2345 if ( r
!= ERROR_SUCCESS
)
2347 msiobj_release( &rec
->hdr
);
2350 r
= IStorage_OpenStream( stg
, encname
, NULL
, STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, &stm
);
2351 if ( r
!= ERROR_SUCCESS
)
2353 msiobj_release( &rec
->hdr
);
2354 msi_free( encname
);
2358 MSI_RecordSetStream( rec
, i
+1, stm
);
2359 TRACE(" field %d [%s]\n", i
+1, debugstr_w(encname
));
2360 msi_free( encname
);
2362 else if( columns
[i
].type
& MSITYPE_STRING
)
2367 val
= read_raw_int(rawdata
, ofs
, bytes_per_strref
);
2368 sval
= msi_string_lookup( st
, val
, &len
);
2369 msi_record_set_string( rec
, i
+1, sval
, len
);
2370 TRACE(" field %d [%s]\n", i
+1, debugstr_wn(sval
, len
));
2371 ofs
+= bytes_per_strref
;
2375 UINT n
= bytes_per_column( tv
->db
, &columns
[i
], bytes_per_strref
);
2379 val
= read_raw_int(rawdata
, ofs
, n
);
2381 MSI_RecordSetInteger( rec
, i
+1, val
-0x8000 );
2382 TRACE(" field %d [0x%04x]\n", i
+1, val
);
2385 val
= read_raw_int(rawdata
, ofs
, n
);
2387 MSI_RecordSetInteger( rec
, i
+1, val
^0x80000000 );
2388 TRACE(" field %d [0x%08x]\n", i
+1, val
);
2391 ERR("oops - unknown column width %d\n", n
);
2400 static void dump_table( const string_table
*st
, const USHORT
*rawdata
, UINT rawsize
)
2403 for (i
= 0; i
< rawsize
/ 2; i
++)
2406 const WCHAR
*sval
= msi_string_lookup( st
, rawdata
[i
], &len
);
2407 MESSAGE(" %04x %s\n", rawdata
[i
], debugstr_wn(sval
, len
) );
2411 static UINT
* msi_record_to_row( const MSITABLEVIEW
*tv
, MSIRECORD
*rec
)
2415 data
= msi_alloc( tv
->num_cols
*sizeof (UINT
) );
2416 for( i
=0; i
<tv
->num_cols
; i
++ )
2420 if ( ~tv
->columns
[i
].type
& MSITYPE_KEY
)
2423 /* turn the transform column value into a row value */
2424 if ( ( tv
->columns
[i
].type
& MSITYPE_STRING
) &&
2425 ! MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
2428 const WCHAR
*str
= msi_record_get_string( rec
, i
+1, &len
);
2431 r
= msi_string2id( tv
->db
->strings
, str
, len
, &data
[i
] );
2433 /* if there's no matching string in the string table,
2434 these keys can't match any record, so fail now. */
2435 if (r
!= ERROR_SUCCESS
)
2445 if (int_to_table_storage( tv
, i
+ 1, MSI_RecordGetInteger( rec
, i
+ 1 ), &data
[i
] ))
2455 static UINT
msi_row_matches( MSITABLEVIEW
*tv
, UINT row
, const UINT
*data
, UINT
*column
)
2457 UINT i
, r
, x
, ret
= ERROR_FUNCTION_FAILED
;
2459 for( i
=0; i
<tv
->num_cols
; i
++ )
2461 if ( ~tv
->columns
[i
].type
& MSITYPE_KEY
)
2464 /* turn the transform column value into a row value */
2465 r
= TABLE_fetch_int( &tv
->view
, row
, i
+1, &x
);
2466 if ( r
!= ERROR_SUCCESS
)
2468 ERR("TABLE_fetch_int shouldn't fail here\n");
2472 /* if this key matches, move to the next column */
2475 ret
= ERROR_FUNCTION_FAILED
;
2478 if (column
) *column
= i
;
2479 ret
= ERROR_SUCCESS
;
2484 static UINT
msi_table_find_row( MSITABLEVIEW
*tv
, MSIRECORD
*rec
, UINT
*row
, UINT
*column
)
2486 UINT i
, r
= ERROR_FUNCTION_FAILED
, *data
;
2488 data
= msi_record_to_row( tv
, rec
);
2491 for( i
= 0; i
< tv
->table
->row_count
; i
++ )
2493 r
= msi_row_matches( tv
, i
, data
, column
);
2494 if( r
== ERROR_SUCCESS
)
2510 static UINT
msi_table_load_transform( MSIDATABASE
*db
, IStorage
*stg
,
2511 string_table
*st
, TRANSFORMDATA
*transform
,
2512 UINT bytes_per_strref
)
2514 BYTE
*rawdata
= NULL
;
2515 MSITABLEVIEW
*tv
= NULL
;
2516 UINT r
, n
, sz
, i
, mask
, num_cols
, colcol
= 0, rawsize
= 0;
2517 MSIRECORD
*rec
= NULL
;
2522 return ERROR_SUCCESS
;
2524 name
= transform
->name
;
2527 TRACE("%p %p %p %s\n", db
, stg
, st
, debugstr_w(name
) );
2529 /* read the transform data */
2530 read_stream_data( stg
, name
, TRUE
, &rawdata
, &rawsize
);
2533 TRACE("table %s empty\n", debugstr_w(name
) );
2534 return ERROR_INVALID_TABLE
;
2537 /* create a table view */
2538 r
= TABLE_CreateView( db
, name
, (MSIVIEW
**) &tv
);
2539 if( r
!= ERROR_SUCCESS
)
2542 r
= tv
->view
.ops
->execute( &tv
->view
, NULL
);
2543 if( r
!= ERROR_SUCCESS
)
2546 TRACE("name = %s columns = %u row_size = %u raw size = %u\n",
2547 debugstr_w(name
), tv
->num_cols
, tv
->row_size
, rawsize
);
2549 /* interpret the data */
2550 for (n
= 0; n
< rawsize
;)
2552 mask
= rawdata
[n
] | (rawdata
[n
+ 1] << 8);
2556 * if the low bit is set, columns are continuous and
2557 * the number of columns is specified in the high byte
2560 num_cols
= mask
>> 8;
2561 if (num_cols
> tv
->num_cols
)
2563 ERR("excess columns in transform: %u > %u\n", num_cols
, tv
->num_cols
);
2567 for (i
= 0; i
< num_cols
; i
++)
2569 if( (tv
->columns
[i
].type
& MSITYPE_STRING
) &&
2570 ! MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
2571 sz
+= bytes_per_strref
;
2573 sz
+= bytes_per_column( tv
->db
, &tv
->columns
[i
], bytes_per_strref
);
2579 * If the low bit is not set, mask is a bitmask.
2580 * Excepting for key fields, which are always present,
2581 * each bit indicates that a field is present in the transform record.
2583 * mask == 0 is a special case ... only the keys will be present
2584 * and it means that this row should be deleted.
2587 num_cols
= tv
->num_cols
;
2588 for (i
= 0; i
< num_cols
; i
++)
2590 if ((tv
->columns
[i
].type
& MSITYPE_KEY
) || ((1 << i
) & mask
))
2592 if ((tv
->columns
[i
].type
& MSITYPE_STRING
) &&
2593 !MSITYPE_IS_BINARY(tv
->columns
[i
].type
))
2594 sz
+= bytes_per_strref
;
2596 sz
+= bytes_per_column( tv
->db
, &tv
->columns
[i
], bytes_per_strref
);
2601 /* check we didn't run of the end of the table */
2602 if (n
+ sz
> rawsize
)
2605 dump_table( st
, (USHORT
*)rawdata
, rawsize
);
2609 rec
= msi_get_transform_record( tv
, st
, stg
, &rawdata
[n
], bytes_per_strref
);
2614 UINT number
= MSI_NULL_INTEGER
;
2617 if (!wcscmp( name
, szColumns
))
2619 MSI_RecordGetStringW( rec
, 1, table
, &sz
);
2620 number
= MSI_RecordGetInteger( rec
, 2 );
2623 * Native msi seems writes nul into the Number (2nd) column of
2624 * the _Columns table when there are new columns
2626 if ( number
== MSI_NULL_INTEGER
)
2628 /* reset the column number on a new table */
2629 if (wcscmp( coltable
, table
))
2632 lstrcpyW( coltable
, table
);
2635 /* fix nul column numbers */
2636 MSI_RecordSetInteger( rec
, 2, ++colcol
);
2640 if (TRACE_ON(msidb
)) dump_record( rec
);
2642 r
= msi_table_find_row( tv
, rec
, &row
, NULL
);
2643 if (r
== ERROR_SUCCESS
)
2647 TRACE("deleting row [%d]:\n", row
);
2648 r
= TABLE_delete_row( &tv
->view
, row
);
2649 if (r
!= ERROR_SUCCESS
)
2650 WARN("failed to delete row %u\n", r
);
2654 TRACE("modifying full row [%d]:\n", row
);
2655 r
= TABLE_set_row( &tv
->view
, row
, rec
, (1 << tv
->num_cols
) - 1 );
2656 if (r
!= ERROR_SUCCESS
)
2657 WARN("failed to modify row %u\n", r
);
2661 TRACE("modifying masked row [%d]:\n", row
);
2662 r
= TABLE_set_row( &tv
->view
, row
, rec
, mask
);
2663 if (r
!= ERROR_SUCCESS
)
2664 WARN("failed to modify row %u\n", r
);
2669 TRACE("inserting row\n");
2670 r
= TABLE_insert_row( &tv
->view
, rec
, -1, FALSE
);
2671 if (r
!= ERROR_SUCCESS
)
2672 WARN("failed to insert row %u\n", r
);
2675 if (!wcscmp( name
, szColumns
))
2676 msi_update_table_columns( db
, table
);
2678 msiobj_release( &rec
->hdr
);
2685 /* no need to free the table, it's associated with the database */
2686 msi_free( rawdata
);
2688 tv
->view
.ops
->delete( &tv
->view
);
2690 return ERROR_SUCCESS
;
2694 * msi_table_apply_transform
2696 * Enumerate the table transforms in a transform storage and apply each one.
2698 UINT
msi_table_apply_transform( MSIDATABASE
*db
, IStorage
*stg
)
2700 struct list transforms
;
2701 IEnumSTATSTG
*stgenum
= NULL
;
2702 TRANSFORMDATA
*transform
;
2703 TRANSFORMDATA
*tables
= NULL
, *columns
= NULL
;
2706 string_table
*strings
;
2707 UINT ret
= ERROR_FUNCTION_FAILED
;
2708 UINT bytes_per_strref
;
2709 BOOL property_update
= FALSE
;
2711 TRACE("%p %p\n", db
, stg
);
2713 strings
= msi_load_string_table( stg
, &bytes_per_strref
);
2717 hr
= IStorage_EnumElements( stg
, 0, NULL
, 0, &stgenum
);
2721 list_init(&transforms
);
2725 MSITABLEVIEW
*tv
= NULL
;
2729 hr
= IEnumSTATSTG_Next( stgenum
, 1, &stat
, &count
);
2730 if (FAILED( hr
) || !count
)
2733 decode_streamname( stat
.pwcsName
, name
);
2734 CoTaskMemFree( stat
.pwcsName
);
2735 if ( name
[0] != 0x4840 )
2738 if ( !wcscmp( name
+1, szStringPool
) ||
2739 !wcscmp( name
+1, szStringData
) )
2742 transform
= msi_alloc_zero( sizeof(TRANSFORMDATA
) );
2746 list_add_tail( &transforms
, &transform
->entry
);
2748 transform
->name
= strdupW( name
+ 1 );
2750 if ( !wcscmp( transform
->name
, szTables
) )
2752 else if (!wcscmp( transform
->name
, szColumns
) )
2753 columns
= transform
;
2754 else if (!wcscmp( transform
->name
, szProperty
))
2755 property_update
= TRUE
;
2757 TRACE("transform contains stream %s\n", debugstr_w(name
));
2759 /* load the table */
2760 if (TABLE_CreateView( db
, transform
->name
, (MSIVIEW
**) &tv
) != ERROR_SUCCESS
)
2763 if (tv
->view
.ops
->execute( &tv
->view
, NULL
) != ERROR_SUCCESS
)
2765 tv
->view
.ops
->delete( &tv
->view
);
2769 tv
->view
.ops
->delete( &tv
->view
);
2773 * Apply _Tables and _Columns transforms first so that
2774 * the table metadata is correct, and empty tables exist.
2776 ret
= msi_table_load_transform( db
, stg
, strings
, tables
, bytes_per_strref
);
2777 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_INVALID_TABLE
)
2780 ret
= msi_table_load_transform( db
, stg
, strings
, columns
, bytes_per_strref
);
2781 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_INVALID_TABLE
)
2784 ret
= ERROR_SUCCESS
;
2786 while ( !list_empty( &transforms
) )
2788 transform
= LIST_ENTRY( list_head( &transforms
), TRANSFORMDATA
, entry
);
2790 if ( wcscmp( transform
->name
, szColumns
) &&
2791 wcscmp( transform
->name
, szTables
) &&
2792 ret
== ERROR_SUCCESS
)
2794 ret
= msi_table_load_transform( db
, stg
, strings
, transform
, bytes_per_strref
);
2797 list_remove( &transform
->entry
);
2798 msi_free( transform
->name
);
2799 msi_free( transform
);
2802 if ( ret
== ERROR_SUCCESS
)
2804 append_storage_to_db( db
, stg
);
2805 if (property_update
) msi_clone_properties( db
);
2810 IEnumSTATSTG_Release( stgenum
);
2812 msi_destroy_stringtable( strings
);