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
21 #define WIN32_NO_STATUS
23 #define COM_NO_WINDOWS_H
29 #define NONAMELESSUNION
30 #define NONAMELESSSTRUCT
33 //#include "winbase.h"
34 //#include "winerror.h"
36 //#include "msiquery.h"
37 //#include "objbase.h"
40 //#include "msipriv.h"
43 #include <wine/debug.h>
44 #include <wine/unicode.h>
46 WINE_DEFAULT_DEBUG_CHANNEL(msidb
);
48 #define MSITABLE_HASH_TABLE_SIZE 37
50 typedef struct tagMSICOLUMNHASHENTRY
52 struct tagMSICOLUMNHASHENTRY
*next
;
57 typedef struct tagMSICOLUMNINFO
66 MSICOLUMNHASHENTRY
**hash_table
;
72 BOOL
*data_persistent
;
75 MSICOLUMNINFO
*colinfo
;
77 MSICONDITION persistent
;
82 /* information for default tables */
83 static const WCHAR szTables
[] = {'_','T','a','b','l','e','s',0};
84 static const WCHAR szTable
[] = {'T','a','b','l','e',0};
85 static const WCHAR szColumns
[] = {'_','C','o','l','u','m','n','s',0};
86 static const WCHAR szNumber
[] = {'N','u','m','b','e','r',0};
87 static const WCHAR szType
[] = {'T','y','p','e',0};
89 static const MSICOLUMNINFO _Columns_cols
[4] = {
90 { szColumns
, 1, szTable
, MSITYPE_VALID
| MSITYPE_STRING
| MSITYPE_KEY
| 64, 0, 0, 0, NULL
},
91 { szColumns
, 2, szNumber
, MSITYPE_VALID
| MSITYPE_KEY
| 2, 2, 0, 0, NULL
},
92 { szColumns
, 3, szName
, MSITYPE_VALID
| MSITYPE_STRING
| 64, 4, 0, 0, NULL
},
93 { szColumns
, 4, szType
, MSITYPE_VALID
| 2, 6, 0, 0, NULL
},
96 static const MSICOLUMNINFO _Tables_cols
[1] = {
97 { szTables
, 1, szName
, MSITYPE_VALID
| MSITYPE_STRING
| MSITYPE_KEY
| 64, 0, 0, 0, NULL
},
100 #define MAX_STREAM_NAME 0x1f
102 static inline UINT
bytes_per_column( MSIDATABASE
*db
, const MSICOLUMNINFO
*col
, UINT bytes_per_strref
)
104 if( MSITYPE_IS_BINARY(col
->type
) )
107 if( col
->type
& MSITYPE_STRING
)
108 return bytes_per_strref
;
110 if( (col
->type
& 0xff) <= 2)
113 if( (col
->type
& 0xff) != 4 )
114 ERR("Invalid column size!\n");
119 static int utf2mime(int x
)
121 if( (x
>='0') && (x
<='9') )
123 if( (x
>='A') && (x
<='Z') )
125 if( (x
>='a') && (x
<='z') )
134 LPWSTR
encode_streamname(BOOL bTable
, LPCWSTR in
)
136 DWORD count
= MAX_STREAM_NAME
;
141 count
= lstrlenW( in
)+2;
142 if (!(out
= msi_alloc( count
*sizeof(WCHAR
) ))) return NULL
;
158 if( ( ch
< 0x80 ) && ( utf2mime(ch
) >= 0 ) )
160 ch
= utf2mime(ch
) + 0x4800;
162 if( next
&& (next
<0x80) )
164 next
= utf2mime(next
);
175 ERR("Failed to encode stream name (%s)\n",debugstr_w(in
));
180 static int mime2utf(int x
)
187 return x
- 10 - 26 + 'a';
188 if( x
== (10+26+26) )
193 BOOL
decode_streamname(LPCWSTR in
, LPWSTR out
)
198 while ( (ch
= *in
++) )
200 if( (ch
>= 0x3800 ) && (ch
< 0x4840 ) )
203 ch
= mime2utf(ch
-0x4800);
207 *out
++ = mime2utf(ch
&0x3f);
209 ch
= mime2utf((ch
>>6)&0x3f);
219 void enum_stream_names( IStorage
*stg
)
221 IEnumSTATSTG
*stgenum
= NULL
;
227 r
= IStorage_EnumElements( stg
, 0, NULL
, 0, &stgenum
);
235 r
= IEnumSTATSTG_Next( stgenum
, 1, &stat
, &count
);
236 if( FAILED( r
) || !count
)
238 decode_streamname( stat
.pwcsName
, name
);
239 TRACE("stream %2d -> %s %s\n", n
,
240 debugstr_w(stat
.pwcsName
), debugstr_w(name
) );
241 CoTaskMemFree( stat
.pwcsName
);
245 IEnumSTATSTG_Release( stgenum
);
248 UINT
read_stream_data( IStorage
*stg
, LPCWSTR stname
, BOOL table
,
249 BYTE
**pdata
, UINT
*psz
)
252 UINT ret
= ERROR_FUNCTION_FAILED
;
259 encname
= encode_streamname(table
, stname
);
261 TRACE("%s -> %s\n",debugstr_w(stname
),debugstr_w(encname
));
263 r
= IStorage_OpenStream(stg
, encname
, NULL
,
264 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, &stm
);
268 WARN("open stream failed r = %08x - empty table?\n", r
);
272 r
= IStream_Stat(stm
, &stat
, STATFLAG_NONAME
);
275 WARN("open stream failed r = %08x!\n", r
);
279 if( stat
.cbSize
.QuadPart
>> 32 )
285 sz
= stat
.cbSize
.QuadPart
;
286 data
= msi_alloc( sz
);
289 WARN("couldn't allocate memory r=%08x!\n", r
);
290 ret
= ERROR_NOT_ENOUGH_MEMORY
;
294 r
= IStream_Read(stm
, data
, sz
, &count
);
295 if( FAILED( r
) || ( count
!= sz
) )
298 WARN("read stream failed r = %08x!\n", r
);
307 IStream_Release( stm
);
312 UINT
write_stream_data( IStorage
*stg
, LPCWSTR stname
,
313 LPCVOID data
, UINT sz
, BOOL bTable
)
316 UINT ret
= ERROR_FUNCTION_FAILED
;
323 encname
= encode_streamname(bTable
, stname
);
324 r
= IStorage_OpenStream( stg
, encname
, NULL
,
325 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, &stm
);
328 r
= IStorage_CreateStream( stg
, encname
,
329 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, 0, &stm
);
334 WARN("open stream failed r = %08x\n", r
);
339 r
= IStream_SetSize( stm
, size
);
342 WARN("Failed to SetSize\n");
347 r
= IStream_Seek( stm
, pos
, STREAM_SEEK_SET
, NULL
);
350 WARN("Failed to Seek\n");
356 r
= IStream_Write(stm
, data
, sz
, &count
);
357 if( FAILED( r
) || ( count
!= sz
) )
359 WARN("Failed to Write\n");
367 IStream_Release( stm
);
372 static void msi_free_colinfo( MSICOLUMNINFO
*colinfo
, UINT count
)
375 for (i
= 0; i
< count
; i
++) msi_free( colinfo
[i
].hash_table
);
378 static void free_table( MSITABLE
*table
)
381 for( i
=0; i
<table
->row_count
; i
++ )
382 msi_free( table
->data
[i
] );
383 msi_free( table
->data
);
384 msi_free( table
->data_persistent
);
385 msi_free_colinfo( table
->colinfo
, table
->col_count
);
386 msi_free( table
->colinfo
);
390 static UINT
msi_table_get_row_size( MSIDATABASE
*db
, const MSICOLUMNINFO
*cols
, UINT count
, UINT bytes_per_strref
)
392 const MSICOLUMNINFO
*last_col
;
397 if (bytes_per_strref
!= LONG_STR_BYTES
)
400 for (i
= 0; i
< count
; i
++) size
+= bytes_per_column( db
, &cols
[i
], bytes_per_strref
);
403 last_col
= &cols
[count
- 1];
404 return last_col
->offset
+ bytes_per_column( db
, last_col
, bytes_per_strref
);
407 /* add this table to the list of cached tables in the database */
408 static UINT
read_table_from_storage( MSIDATABASE
*db
, MSITABLE
*t
, IStorage
*stg
)
410 BYTE
*rawdata
= NULL
;
411 UINT rawsize
= 0, i
, j
, row_size
, row_size_mem
;
413 TRACE("%s\n",debugstr_w(t
->name
));
415 row_size
= msi_table_get_row_size( db
, t
->colinfo
, t
->col_count
, db
->bytes_per_strref
);
416 row_size_mem
= msi_table_get_row_size( db
, t
->colinfo
, t
->col_count
, LONG_STR_BYTES
);
418 /* if we can't read the table, just assume that it's empty */
419 read_stream_data( stg
, t
->name
, TRUE
, &rawdata
, &rawsize
);
421 return ERROR_SUCCESS
;
423 TRACE("Read %d bytes\n", rawsize
);
425 if( rawsize
% row_size
)
427 WARN("Table size is invalid %d/%d\n", rawsize
, row_size
);
431 t
->row_count
= rawsize
/ row_size
;
432 t
->data
= msi_alloc_zero( t
->row_count
* sizeof (USHORT
*) );
435 t
->data_persistent
= msi_alloc_zero( t
->row_count
* sizeof(BOOL
));
436 if ( !t
->data_persistent
)
439 /* transpose all the data */
440 TRACE("Transposing data from %d rows\n", t
->row_count
);
441 for (i
= 0; i
< t
->row_count
; i
++)
443 UINT ofs
= 0, ofs_mem
= 0;
445 t
->data
[i
] = msi_alloc( row_size_mem
);
448 t
->data_persistent
[i
] = TRUE
;
450 for (j
= 0; j
< t
->col_count
; j
++)
452 UINT m
= bytes_per_column( db
, &t
->colinfo
[j
], LONG_STR_BYTES
);
453 UINT n
= bytes_per_column( db
, &t
->colinfo
[j
], db
->bytes_per_strref
);
456 if ( n
!= 2 && n
!= 3 && n
!= 4 )
458 ERR("oops - unknown column width %d\n", n
);
461 if (t
->colinfo
[j
].type
& MSITYPE_STRING
&& n
< m
)
463 for (k
= 0; k
< m
; k
++)
466 t
->data
[i
][ofs_mem
+ k
] = rawdata
[ofs
* t
->row_count
+ i
* n
+ k
];
468 t
->data
[i
][ofs_mem
+ k
] = 0;
473 for (k
= 0; k
< n
; k
++)
474 t
->data
[i
][ofs_mem
+ k
] = rawdata
[ofs
* t
->row_count
+ i
* n
+ k
];
482 return ERROR_SUCCESS
;
485 return ERROR_FUNCTION_FAILED
;
488 void free_cached_tables( MSIDATABASE
*db
)
490 while( !list_empty( &db
->tables
) )
492 MSITABLE
*t
= LIST_ENTRY( list_head( &db
->tables
), MSITABLE
, entry
);
494 list_remove( &t
->entry
);
499 static MSITABLE
*find_cached_table( MSIDATABASE
*db
, LPCWSTR name
)
503 LIST_FOR_EACH_ENTRY( t
, &db
->tables
, MSITABLE
, entry
)
504 if( !strcmpW( name
, t
->name
) )
510 static void table_calc_column_offsets( MSIDATABASE
*db
, MSICOLUMNINFO
*colinfo
, DWORD count
)
514 for (i
= 0; colinfo
&& i
< count
; i
++)
516 assert( i
+ 1 == colinfo
[i
].number
);
517 if (i
) colinfo
[i
].offset
= colinfo
[i
- 1].offset
+
518 bytes_per_column( db
, &colinfo
[i
- 1], LONG_STR_BYTES
);
519 else colinfo
[i
].offset
= 0;
521 TRACE("column %d is [%s] with type %08x ofs %d\n",
522 colinfo
[i
].number
, debugstr_w(colinfo
[i
].colname
),
523 colinfo
[i
].type
, colinfo
[i
].offset
);
527 static UINT
get_defaulttablecolumns( MSIDATABASE
*db
, LPCWSTR name
, MSICOLUMNINFO
*colinfo
, UINT
*sz
)
529 const MSICOLUMNINFO
*p
;
532 TRACE("%s\n", debugstr_w(name
));
534 if (!strcmpW( name
, szTables
))
539 else if (!strcmpW( name
, szColumns
))
544 else return ERROR_FUNCTION_FAILED
;
546 for (i
= 0; i
< n
; i
++)
548 if (colinfo
&& i
< *sz
) colinfo
[i
] = p
[i
];
549 if (colinfo
&& i
>= *sz
) break;
551 table_calc_column_offsets( db
, colinfo
, n
);
553 return ERROR_SUCCESS
;
556 static UINT
get_tablecolumns( MSIDATABASE
*db
, LPCWSTR szTableName
, MSICOLUMNINFO
*colinfo
, UINT
*sz
);
558 static UINT
table_get_column_info( MSIDATABASE
*db
, LPCWSTR name
, MSICOLUMNINFO
**pcols
, UINT
*pcount
)
560 UINT r
, column_count
= 0;
561 MSICOLUMNINFO
*columns
;
563 /* get the number of columns in this table */
565 r
= get_tablecolumns( db
, name
, NULL
, &column_count
);
566 if (r
!= ERROR_SUCCESS
)
569 *pcount
= column_count
;
571 /* if there's no columns, there's no table */
573 return ERROR_INVALID_PARAMETER
;
575 TRACE("table %s found\n", debugstr_w(name
));
577 columns
= msi_alloc( column_count
* sizeof(MSICOLUMNINFO
) );
579 return ERROR_FUNCTION_FAILED
;
581 r
= get_tablecolumns( db
, name
, columns
, &column_count
);
582 if (r
!= ERROR_SUCCESS
)
585 return ERROR_FUNCTION_FAILED
;
591 static UINT
get_table( MSIDATABASE
*db
, LPCWSTR name
, MSITABLE
**table_ret
)
596 /* first, see if the table is cached */
597 table
= find_cached_table( db
, name
);
601 return ERROR_SUCCESS
;
604 /* nonexistent tables should be interpreted as empty tables */
605 table
= msi_alloc( sizeof(MSITABLE
) + lstrlenW( name
) * sizeof(WCHAR
) );
607 return ERROR_FUNCTION_FAILED
;
609 table
->row_count
= 0;
611 table
->data_persistent
= NULL
;
612 table
->colinfo
= NULL
;
613 table
->col_count
= 0;
614 table
->persistent
= MSICONDITION_TRUE
;
615 lstrcpyW( table
->name
, name
);
617 if (!strcmpW( name
, szTables
) || !strcmpW( name
, szColumns
))
618 table
->persistent
= MSICONDITION_NONE
;
620 r
= table_get_column_info( db
, name
, &table
->colinfo
, &table
->col_count
);
621 if (r
!= ERROR_SUCCESS
)
626 r
= read_table_from_storage( db
, table
, db
->storage
);
627 if (r
!= ERROR_SUCCESS
)
632 list_add_head( &db
->tables
, &table
->entry
);
634 return ERROR_SUCCESS
;
637 static UINT
read_table_int( BYTE
*const *data
, UINT row
, UINT col
, UINT bytes
)
641 for (i
= 0; i
< bytes
; i
++)
642 ret
+= data
[row
][col
+ i
] << i
* 8;
647 static UINT
get_tablecolumns( MSIDATABASE
*db
, LPCWSTR szTableName
, MSICOLUMNINFO
*colinfo
, UINT
*sz
)
649 UINT r
, i
, n
= 0, table_id
, count
, maxcount
= *sz
;
650 MSITABLE
*table
= NULL
;
652 TRACE("%s\n", debugstr_w(szTableName
));
654 /* first check if there is a default table with that name */
655 r
= get_defaulttablecolumns( db
, szTableName
, colinfo
, sz
);
656 if (r
== ERROR_SUCCESS
&& *sz
)
659 r
= get_table( db
, szColumns
, &table
);
660 if (r
!= ERROR_SUCCESS
)
662 ERR("couldn't load _Columns table\n");
663 return ERROR_FUNCTION_FAILED
;
666 /* convert table and column names to IDs from the string table */
667 r
= msi_string2id( db
->strings
, szTableName
, -1, &table_id
);
668 if (r
!= ERROR_SUCCESS
)
670 WARN("Couldn't find id for %s\n", debugstr_w(szTableName
));
673 TRACE("Table id is %d, row count is %d\n", table_id
, table
->row_count
);
675 /* Note: _Columns table doesn't have non-persistent data */
677 /* if maxcount is non-zero, assume it's exactly right for this table */
678 memset( colinfo
, 0, maxcount
* sizeof(*colinfo
) );
679 count
= table
->row_count
;
680 for (i
= 0; i
< count
; i
++)
682 if (read_table_int( table
->data
, i
, 0, LONG_STR_BYTES
) != table_id
) continue;
685 UINT id
= read_table_int( table
->data
, i
, table
->colinfo
[2].offset
, LONG_STR_BYTES
);
686 UINT col
= read_table_int( table
->data
, i
, table
->colinfo
[1].offset
, sizeof(USHORT
) ) - (1 << 15);
688 /* check the column number is in range */
689 if (col
< 1 || col
> maxcount
)
691 ERR("column %d out of range\n", col
);
694 /* check if this column was already set */
695 if (colinfo
[col
- 1].number
)
697 ERR("duplicate column %d\n", col
);
700 colinfo
[col
- 1].tablename
= msi_string_lookup( db
->strings
, table_id
, NULL
);
701 colinfo
[col
- 1].number
= col
;
702 colinfo
[col
- 1].colname
= msi_string_lookup( db
->strings
, id
, NULL
);
703 colinfo
[col
- 1].type
= read_table_int( table
->data
, i
, table
->colinfo
[3].offset
,
704 sizeof(USHORT
) ) - (1 << 15);
705 colinfo
[col
- 1].offset
= 0;
706 colinfo
[col
- 1].ref_count
= 0;
707 colinfo
[col
- 1].hash_table
= NULL
;
711 TRACE("%s has %d columns\n", debugstr_w(szTableName
), n
);
713 if (colinfo
&& n
!= maxcount
)
715 ERR("missing column in table %s\n", debugstr_w(szTableName
));
716 msi_free_colinfo( colinfo
, maxcount
);
717 return ERROR_FUNCTION_FAILED
;
719 table_calc_column_offsets( db
, colinfo
, n
);
721 return ERROR_SUCCESS
;
724 UINT
msi_create_table( MSIDATABASE
*db
, LPCWSTR name
, column_info
*col_info
,
725 MSICONDITION persistent
)
727 enum StringPersistence string_persistence
= (persistent
) ? StringPersistent
: StringNonPersistent
;
730 MSIRECORD
*rec
= NULL
;
735 /* only add tables that don't exist already */
736 if( TABLE_Exists(db
, name
) )
738 WARN("table %s exists\n", debugstr_w(name
));
739 return ERROR_BAD_QUERY_SYNTAX
;
742 table
= msi_alloc( sizeof (MSITABLE
) + lstrlenW(name
)*sizeof (WCHAR
) );
744 return ERROR_FUNCTION_FAILED
;
746 table
->ref_count
= 1;
747 table
->row_count
= 0;
749 table
->data_persistent
= NULL
;
750 table
->colinfo
= NULL
;
751 table
->col_count
= 0;
752 table
->persistent
= persistent
;
753 lstrcpyW( table
->name
, name
);
755 for( col
= col_info
; col
; col
= col
->next
)
758 table
->colinfo
= msi_alloc( table
->col_count
* sizeof(MSICOLUMNINFO
) );
762 return ERROR_FUNCTION_FAILED
;
765 for( i
= 0, col
= col_info
; col
; i
++, col
= col
->next
)
767 UINT table_id
= msi_addstringW( db
->strings
, col
->table
, -1, 1, string_persistence
);
768 UINT col_id
= msi_addstringW( db
->strings
, col
->column
, -1, 1, string_persistence
);
770 table
->colinfo
[ i
].tablename
= msi_string_lookup( db
->strings
, table_id
, NULL
);
771 table
->colinfo
[ i
].number
= i
+ 1;
772 table
->colinfo
[ i
].colname
= msi_string_lookup( db
->strings
, col_id
, NULL
);
773 table
->colinfo
[ i
].type
= col
->type
;
774 table
->colinfo
[ i
].offset
= 0;
775 table
->colinfo
[ i
].ref_count
= 0;
776 table
->colinfo
[ i
].hash_table
= NULL
;
777 table
->colinfo
[ i
].temporary
= col
->temporary
;
779 table_calc_column_offsets( db
, table
->colinfo
, table
->col_count
);
781 r
= TABLE_CreateView( db
, szTables
, &tv
);
782 TRACE("CreateView returned %x\n", r
);
789 r
= tv
->ops
->execute( tv
, 0 );
790 TRACE("tv execute returned %x\n", r
);
794 rec
= MSI_CreateRecord( 1 );
798 r
= MSI_RecordSetStringW( rec
, 1, name
);
802 r
= tv
->ops
->insert_row( tv
, rec
, -1, persistent
== MSICONDITION_FALSE
);
803 TRACE("insert_row returned %x\n", r
);
807 tv
->ops
->delete( tv
);
810 msiobj_release( &rec
->hdr
);
813 if( persistent
!= MSICONDITION_FALSE
)
815 /* add each column to the _Columns table */
816 r
= TABLE_CreateView( db
, szColumns
, &tv
);
820 r
= tv
->ops
->execute( tv
, 0 );
821 TRACE("tv execute returned %x\n", r
);
825 rec
= MSI_CreateRecord( 4 );
829 r
= MSI_RecordSetStringW( rec
, 1, name
);
834 * need to set the table, column number, col name and type
835 * for each column we enter in the table
838 for( col
= col_info
; col
; col
= col
->next
)
840 r
= MSI_RecordSetInteger( rec
, 2, nField
);
844 r
= MSI_RecordSetStringW( rec
, 3, col
->column
);
848 r
= MSI_RecordSetInteger( rec
, 4, col
->type
);
852 r
= tv
->ops
->insert_row( tv
, rec
, -1, FALSE
);
864 msiobj_release( &rec
->hdr
);
865 /* FIXME: remove values from the string table on error */
867 tv
->ops
->delete( tv
);
869 if (r
== ERROR_SUCCESS
)
870 list_add_head( &db
->tables
, &table
->entry
);
877 static UINT
save_table( MSIDATABASE
*db
, const MSITABLE
*t
, UINT bytes_per_strref
)
879 BYTE
*rawdata
= NULL
;
880 UINT rawsize
, i
, j
, row_size
, row_count
;
881 UINT r
= ERROR_FUNCTION_FAILED
;
883 /* Nothing to do for non-persistent tables */
884 if( t
->persistent
== MSICONDITION_FALSE
)
885 return ERROR_SUCCESS
;
887 TRACE("Saving %s\n", debugstr_w( t
->name
) );
889 row_size
= msi_table_get_row_size( db
, t
->colinfo
, t
->col_count
, bytes_per_strref
);
890 row_count
= t
->row_count
;
891 for (i
= 0; i
< t
->row_count
; i
++)
893 if (!t
->data_persistent
[i
])
895 row_count
= 1; /* yes, this is bizarre */
899 rawsize
= row_count
* row_size
;
900 rawdata
= msi_alloc_zero( rawsize
);
903 r
= ERROR_NOT_ENOUGH_MEMORY
;
908 for (i
= 0; i
< t
->row_count
; i
++)
910 UINT ofs
= 0, ofs_mem
= 0;
912 if (!t
->data_persistent
[i
]) break;
914 for (j
= 0; j
< t
->col_count
; j
++)
916 UINT m
= bytes_per_column( db
, &t
->colinfo
[j
], LONG_STR_BYTES
);
917 UINT n
= bytes_per_column( db
, &t
->colinfo
[j
], bytes_per_strref
);
920 if (n
!= 2 && n
!= 3 && n
!= 4)
922 ERR("oops - unknown column width %d\n", n
);
925 if (t
->colinfo
[j
].type
& MSITYPE_STRING
&& n
< m
)
927 UINT id
= read_table_int( t
->data
, i
, ofs_mem
, LONG_STR_BYTES
);
928 if (id
> 1 << bytes_per_strref
* 8)
930 ERR("string id %u out of range\n", id
);
934 for (k
= 0; k
< n
; k
++)
936 rawdata
[ofs
* row_count
+ i
* n
+ k
] = t
->data
[i
][ofs_mem
+ k
];
944 TRACE("writing %d bytes\n", rawsize
);
945 r
= write_stream_data( db
->storage
, t
->name
, rawdata
, rawsize
, TRUE
);
952 static void msi_update_table_columns( MSIDATABASE
*db
, LPCWSTR name
)
955 UINT size
, offset
, old_count
;
958 table
= find_cached_table( db
, name
);
959 old_count
= table
->col_count
;
960 msi_free_colinfo( table
->colinfo
, table
->col_count
);
961 msi_free( table
->colinfo
);
962 table
->colinfo
= NULL
;
964 table_get_column_info( db
, name
, &table
->colinfo
, &table
->col_count
);
965 if (!table
->col_count
) return;
967 size
= msi_table_get_row_size( db
, table
->colinfo
, table
->col_count
, LONG_STR_BYTES
);
968 offset
= table
->colinfo
[table
->col_count
- 1].offset
;
970 for ( n
= 0; n
< table
->row_count
; n
++ )
972 table
->data
[n
] = msi_realloc( table
->data
[n
], size
);
973 if (old_count
< table
->col_count
)
974 memset( &table
->data
[n
][offset
], 0, size
- offset
);
978 /* try to find the table name in the _Tables table */
979 BOOL
TABLE_Exists( MSIDATABASE
*db
, LPCWSTR name
)
984 if( !strcmpW( name
, szTables
) || !strcmpW( name
, szColumns
) ||
985 !strcmpW( name
, szStreams
) || !strcmpW( name
, szStorages
) )
988 r
= msi_string2id( db
->strings
, name
, -1, &table_id
);
989 if( r
!= ERROR_SUCCESS
)
991 TRACE("Couldn't find id for %s\n", debugstr_w(name
));
995 r
= get_table( db
, szTables
, &table
);
996 if( r
!= ERROR_SUCCESS
)
998 ERR("table %s not available\n", debugstr_w(szTables
));
1002 for( i
= 0; i
< table
->row_count
; i
++ )
1004 if( read_table_int( table
->data
, i
, 0, LONG_STR_BYTES
) == table_id
)
1011 /* below is the query interface to a table */
1013 typedef struct tagMSITABLEVIEW
1018 MSICOLUMNINFO
*columns
;
1024 static UINT
TABLE_fetch_int( struct tagMSIVIEW
*view
, UINT row
, UINT col
, UINT
*val
)
1026 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1030 return ERROR_INVALID_PARAMETER
;
1032 if( (col
==0) || (col
>tv
->num_cols
) )
1033 return ERROR_INVALID_PARAMETER
;
1035 /* how many rows are there ? */
1036 if( row
>= tv
->table
->row_count
)
1037 return ERROR_NO_MORE_ITEMS
;
1039 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1041 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1042 ERR("%p %p\n", tv
, tv
->columns
);
1043 return ERROR_FUNCTION_FAILED
;
1046 n
= bytes_per_column( tv
->db
, &tv
->columns
[col
- 1], LONG_STR_BYTES
);
1047 if (n
!= 2 && n
!= 3 && n
!= 4)
1049 ERR("oops! what is %d bytes per column?\n", n
);
1050 return ERROR_FUNCTION_FAILED
;
1053 offset
= tv
->columns
[col
-1].offset
;
1054 *val
= read_table_int(tv
->table
->data
, row
, offset
, n
);
1056 /* TRACE("Data [%d][%d] = %d\n", row, col, *val ); */
1058 return ERROR_SUCCESS
;
1061 static UINT
msi_stream_name( const MSITABLEVIEW
*tv
, UINT row
, LPWSTR
*pstname
)
1063 LPWSTR p
, stname
= NULL
;
1064 UINT i
, r
, type
, ival
;
1067 MSIVIEW
*view
= (MSIVIEW
*) tv
;
1069 TRACE("%p %d\n", tv
, row
);
1071 len
= lstrlenW( tv
->name
) + 1;
1072 stname
= msi_alloc( len
*sizeof(WCHAR
) );
1075 r
= ERROR_OUTOFMEMORY
;
1079 lstrcpyW( stname
, tv
->name
);
1081 for ( i
= 0; i
< tv
->num_cols
; i
++ )
1083 type
= tv
->columns
[i
].type
;
1084 if ( type
& MSITYPE_KEY
)
1088 r
= TABLE_fetch_int( view
, row
, i
+1, &ival
);
1089 if ( r
!= ERROR_SUCCESS
)
1092 if ( tv
->columns
[i
].type
& MSITYPE_STRING
)
1094 sval
= msi_string_lookup( tv
->db
->strings
, ival
, NULL
);
1097 r
= ERROR_INVALID_PARAMETER
;
1103 static const WCHAR fmt
[] = { '%','d',0 };
1104 UINT n
= bytes_per_column( tv
->db
, &tv
->columns
[i
], LONG_STR_BYTES
);
1109 sprintfW( number
, fmt
, ival
-0x8000 );
1112 sprintfW( number
, fmt
, ival
^0x80000000 );
1115 ERR( "oops - unknown column width %d\n", n
);
1116 r
= ERROR_FUNCTION_FAILED
;
1122 len
+= lstrlenW( szDot
) + lstrlenW( sval
);
1123 p
= msi_realloc ( stname
, len
*sizeof(WCHAR
) );
1126 r
= ERROR_OUTOFMEMORY
;
1131 lstrcatW( stname
, szDot
);
1132 lstrcatW( stname
, sval
);
1139 return ERROR_SUCCESS
;
1148 * We need a special case for streams, as we need to reference column with
1149 * the name of the stream in the same table, and the table name
1150 * which may not be available at higher levels of the query
1152 static UINT
TABLE_fetch_stream( struct tagMSIVIEW
*view
, UINT row
, UINT col
, IStream
**stm
)
1154 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1156 LPWSTR encname
, full_name
= NULL
;
1158 if( !view
->ops
->fetch_int
)
1159 return ERROR_INVALID_PARAMETER
;
1161 r
= msi_stream_name( tv
, row
, &full_name
);
1162 if ( r
!= ERROR_SUCCESS
)
1164 ERR("fetching stream, error = %d\n", r
);
1168 encname
= encode_streamname( FALSE
, full_name
);
1169 r
= msi_get_raw_stream( tv
->db
, encname
, stm
);
1171 ERR("fetching stream %s, error = %d\n",debugstr_w(full_name
), r
);
1173 msi_free( full_name
);
1174 msi_free( encname
);
1178 static UINT
TABLE_set_int( MSITABLEVIEW
*tv
, UINT row
, UINT col
, UINT val
)
1183 return ERROR_INVALID_PARAMETER
;
1185 if( (col
==0) || (col
>tv
->num_cols
) )
1186 return ERROR_INVALID_PARAMETER
;
1188 if( row
>= tv
->table
->row_count
)
1189 return ERROR_INVALID_PARAMETER
;
1191 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1193 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1194 ERR("%p %p\n", tv
, tv
->columns
);
1195 return ERROR_FUNCTION_FAILED
;
1198 msi_free( tv
->columns
[col
-1].hash_table
);
1199 tv
->columns
[col
-1].hash_table
= NULL
;
1201 n
= bytes_per_column( tv
->db
, &tv
->columns
[col
- 1], LONG_STR_BYTES
);
1202 if ( n
!= 2 && n
!= 3 && n
!= 4 )
1204 ERR("oops! what is %d bytes per column?\n", n
);
1205 return ERROR_FUNCTION_FAILED
;
1208 offset
= tv
->columns
[col
-1].offset
;
1209 for ( i
= 0; i
< n
; i
++ )
1210 tv
->table
->data
[row
][offset
+ i
] = (val
>> i
* 8) & 0xff;
1212 return ERROR_SUCCESS
;
1215 static UINT
TABLE_get_row( struct tagMSIVIEW
*view
, UINT row
, MSIRECORD
**rec
)
1217 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1220 return ERROR_INVALID_PARAMETER
;
1222 return msi_view_get_row(tv
->db
, view
, row
, rec
);
1225 static UINT
msi_addstreamW( MSIDATABASE
*db
, LPCWSTR name
, IStream
*data
)
1227 static const WCHAR insert
[] = {
1228 'I','N','S','E','R','T',' ','I','N','T','O',' ',
1229 '`','_','S','t','r','e','a','m','s','`',' ',
1230 '(','`','N','a','m','e','`',',','`','D','a','t','a','`',')',' ',
1231 'V','A','L','U','E','S',' ','(','?',',','?',')',0};
1232 MSIQUERY
*query
= NULL
;
1236 TRACE("%p %s %p\n", db
, debugstr_w(name
), data
);
1238 rec
= MSI_CreateRecord( 2 );
1240 return ERROR_OUTOFMEMORY
;
1242 r
= MSI_RecordSetStringW( rec
, 1, name
);
1243 if ( r
!= ERROR_SUCCESS
)
1246 r
= MSI_RecordSetIStream( rec
, 2, data
);
1247 if ( r
!= ERROR_SUCCESS
)
1250 r
= MSI_DatabaseOpenViewW( db
, insert
, &query
);
1251 if ( r
!= ERROR_SUCCESS
)
1254 r
= MSI_ViewExecute( query
, rec
);
1257 msiobj_release( &query
->hdr
);
1258 msiobj_release( &rec
->hdr
);
1262 static UINT
get_table_value_from_record( MSITABLEVIEW
*tv
, MSIRECORD
*rec
, UINT iField
, UINT
*pvalue
)
1264 MSICOLUMNINFO columninfo
;
1267 if ( (iField
<= 0) ||
1268 (iField
> tv
->num_cols
) ||
1269 MSI_RecordIsNull( rec
, iField
) )
1270 return ERROR_FUNCTION_FAILED
;
1272 columninfo
= tv
->columns
[ iField
- 1 ];
1274 if ( MSITYPE_IS_BINARY(columninfo
.type
) )
1276 *pvalue
= 1; /* refers to the first key column */
1278 else if ( columninfo
.type
& MSITYPE_STRING
)
1281 const WCHAR
*sval
= msi_record_get_string( rec
, iField
, &len
);
1284 r
= msi_string2id( tv
->db
->strings
, sval
, len
, pvalue
);
1285 if (r
!= ERROR_SUCCESS
)
1286 return ERROR_NOT_FOUND
;
1290 else if ( bytes_per_column( tv
->db
, &columninfo
, LONG_STR_BYTES
) == 2 )
1292 *pvalue
= 0x8000 + MSI_RecordGetInteger( rec
, iField
);
1293 if ( *pvalue
& 0xffff0000 )
1295 ERR("field %u value %d out of range\n", iField
, *pvalue
- 0x8000);
1296 return ERROR_FUNCTION_FAILED
;
1301 INT ival
= MSI_RecordGetInteger( rec
, iField
);
1302 *pvalue
= ival
^ 0x80000000;
1305 return ERROR_SUCCESS
;
1308 static UINT
TABLE_set_row( struct tagMSIVIEW
*view
, UINT row
, MSIRECORD
*rec
, UINT mask
)
1310 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1311 UINT i
, val
, r
= ERROR_SUCCESS
;
1314 return ERROR_INVALID_PARAMETER
;
1316 /* test if any of the mask bits are invalid */
1317 if ( mask
>= (1<<tv
->num_cols
) )
1318 return ERROR_INVALID_PARAMETER
;
1320 for ( i
= 0; i
< tv
->num_cols
; i
++ )
1324 /* only update the fields specified in the mask */
1325 if ( !(mask
&(1<<i
)) )
1328 persistent
= (tv
->table
->persistent
!= MSICONDITION_FALSE
) &&
1329 (tv
->table
->data_persistent
[row
]);
1330 /* FIXME: should we allow updating keys? */
1333 if ( !MSI_RecordIsNull( rec
, i
+ 1 ) )
1335 r
= get_table_value_from_record (tv
, rec
, i
+ 1, &val
);
1336 if ( MSITYPE_IS_BINARY(tv
->columns
[ i
].type
) )
1341 if ( r
!= ERROR_SUCCESS
)
1342 return ERROR_FUNCTION_FAILED
;
1344 r
= MSI_RecordGetIStream( rec
, i
+ 1, &stm
);
1345 if ( r
!= ERROR_SUCCESS
)
1348 r
= msi_stream_name( tv
, row
, &stname
);
1349 if ( r
!= ERROR_SUCCESS
)
1351 IStream_Release( stm
);
1355 r
= msi_addstreamW( tv
->db
, stname
, stm
);
1356 IStream_Release( stm
);
1357 msi_free ( stname
);
1359 if ( r
!= ERROR_SUCCESS
)
1362 else if ( tv
->columns
[i
].type
& MSITYPE_STRING
)
1366 if ( r
!= ERROR_SUCCESS
)
1369 const WCHAR
*sval
= msi_record_get_string( rec
, i
+ 1, &len
);
1370 val
= msi_addstringW( tv
->db
->strings
, sval
, len
, 1,
1371 persistent
? StringPersistent
: StringNonPersistent
);
1375 TABLE_fetch_int(&tv
->view
, row
, i
+ 1, &x
);
1382 if ( r
!= ERROR_SUCCESS
)
1383 return ERROR_FUNCTION_FAILED
;
1387 r
= TABLE_set_int( tv
, row
, i
+1, val
);
1388 if ( r
!= ERROR_SUCCESS
)
1394 static UINT
table_create_new_row( struct tagMSIVIEW
*view
, UINT
*num
, BOOL temporary
)
1396 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1401 BOOL
**data_persist_ptr
;
1404 TRACE("%p %s\n", view
, temporary
? "TRUE" : "FALSE");
1407 return ERROR_INVALID_PARAMETER
;
1409 row
= msi_alloc_zero( tv
->row_size
);
1411 return ERROR_NOT_ENOUGH_MEMORY
;
1413 row_count
= &tv
->table
->row_count
;
1414 data_ptr
= &tv
->table
->data
;
1415 data_persist_ptr
= &tv
->table
->data_persistent
;
1417 *num
= tv
->table
->row_count
;
1419 sz
= (*row_count
+ 1) * sizeof (BYTE
*);
1421 p
= msi_realloc( *data_ptr
, sz
);
1423 p
= msi_alloc( sz
);
1427 return ERROR_NOT_ENOUGH_MEMORY
;
1430 sz
= (*row_count
+ 1) * sizeof (BOOL
);
1431 if( *data_persist_ptr
)
1432 b
= msi_realloc( *data_persist_ptr
, sz
);
1434 b
= msi_alloc( sz
);
1439 return ERROR_NOT_ENOUGH_MEMORY
;
1443 (*data_ptr
)[*row_count
] = row
;
1445 *data_persist_ptr
= b
;
1446 (*data_persist_ptr
)[*row_count
] = !temporary
;
1450 return ERROR_SUCCESS
;
1453 static UINT
TABLE_execute( struct tagMSIVIEW
*view
, MSIRECORD
*record
)
1455 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1457 TRACE("%p %p\n", tv
, record
);
1459 TRACE("There are %d columns\n", tv
->num_cols
);
1461 return ERROR_SUCCESS
;
1464 static UINT
TABLE_close( struct tagMSIVIEW
*view
)
1466 TRACE("%p\n", view
);
1468 return ERROR_SUCCESS
;
1471 static UINT
TABLE_get_dimensions( struct tagMSIVIEW
*view
, UINT
*rows
, UINT
*cols
)
1473 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1475 TRACE("%p %p %p\n", view
, rows
, cols
);
1478 *cols
= tv
->num_cols
;
1482 return ERROR_INVALID_PARAMETER
;
1483 *rows
= tv
->table
->row_count
;
1486 return ERROR_SUCCESS
;
1489 static UINT
TABLE_get_column_info( struct tagMSIVIEW
*view
,
1490 UINT n
, LPCWSTR
*name
, UINT
*type
, BOOL
*temporary
,
1491 LPCWSTR
*table_name
)
1493 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1495 TRACE("%p %d %p %p\n", tv
, n
, name
, type
);
1497 if( ( n
== 0 ) || ( n
> tv
->num_cols
) )
1498 return ERROR_INVALID_PARAMETER
;
1502 *name
= tv
->columns
[n
-1].colname
;
1504 return ERROR_FUNCTION_FAILED
;
1509 *table_name
= tv
->columns
[n
-1].tablename
;
1511 return ERROR_FUNCTION_FAILED
;
1515 *type
= tv
->columns
[n
-1].type
;
1518 *temporary
= tv
->columns
[n
-1].temporary
;
1520 return ERROR_SUCCESS
;
1523 static UINT
msi_table_find_row( MSITABLEVIEW
*tv
, MSIRECORD
*rec
, UINT
*row
, UINT
*column
);
1525 static UINT
table_validate_new( MSITABLEVIEW
*tv
, MSIRECORD
*rec
, UINT
*column
)
1529 /* check there's no null values where they're not allowed */
1530 for( i
= 0; i
< tv
->num_cols
; i
++ )
1532 if ( tv
->columns
[i
].type
& MSITYPE_NULLABLE
)
1535 if ( MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
1536 TRACE("skipping binary column\n");
1537 else if ( tv
->columns
[i
].type
& MSITYPE_STRING
)
1540 const WCHAR
*str
= msi_record_get_string( rec
, i
+1, &len
);
1542 if (!str
|| (!str
[0] && !len
))
1544 if (column
) *column
= i
;
1545 return ERROR_INVALID_DATA
;
1552 n
= MSI_RecordGetInteger( rec
, i
+1 );
1553 if (n
== MSI_NULL_INTEGER
)
1555 if (column
) *column
= i
;
1556 return ERROR_INVALID_DATA
;
1561 /* check there's no duplicate keys */
1562 r
= msi_table_find_row( tv
, rec
, &row
, column
);
1563 if (r
== ERROR_SUCCESS
)
1564 return ERROR_FUNCTION_FAILED
;
1566 return ERROR_SUCCESS
;
1569 static int compare_record( MSITABLEVIEW
*tv
, UINT row
, MSIRECORD
*rec
)
1571 UINT r
, i
, ivalue
, x
;
1573 for (i
= 0; i
< tv
->num_cols
; i
++ )
1575 if (!(tv
->columns
[i
].type
& MSITYPE_KEY
)) continue;
1577 r
= get_table_value_from_record( tv
, rec
, i
+ 1, &ivalue
);
1578 if (r
!= ERROR_SUCCESS
)
1581 r
= TABLE_fetch_int( &tv
->view
, row
, i
+ 1, &x
);
1582 if (r
!= ERROR_SUCCESS
)
1584 WARN("TABLE_fetch_int should not fail here %u\n", r
);
1591 else if (ivalue
== x
)
1593 if (i
< tv
->num_cols
- 1) continue;
1602 static int find_insert_index( MSITABLEVIEW
*tv
, MSIRECORD
*rec
)
1604 int idx
, c
, low
= 0, high
= tv
->table
->row_count
- 1;
1606 TRACE("%p %p\n", tv
, rec
);
1610 idx
= (low
+ high
) / 2;
1611 c
= compare_record( tv
, idx
, rec
);
1619 TRACE("found %u\n", idx
);
1623 TRACE("found %u\n", high
+ 1);
1627 static UINT
TABLE_insert_row( struct tagMSIVIEW
*view
, MSIRECORD
*rec
, UINT row
, BOOL temporary
)
1629 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1632 TRACE("%p %p %s\n", tv
, rec
, temporary
? "TRUE" : "FALSE" );
1634 /* check that the key is unique - can we find a matching row? */
1635 r
= table_validate_new( tv
, rec
, NULL
);
1636 if( r
!= ERROR_SUCCESS
)
1637 return ERROR_FUNCTION_FAILED
;
1640 row
= find_insert_index( tv
, rec
);
1642 r
= table_create_new_row( view
, &row
, temporary
);
1643 TRACE("insert_row returned %08x\n", r
);
1644 if( r
!= ERROR_SUCCESS
)
1647 /* shift the rows to make room for the new row */
1648 for (i
= tv
->table
->row_count
- 1; i
> row
; i
--)
1650 memmove(&(tv
->table
->data
[i
][0]),
1651 &(tv
->table
->data
[i
- 1][0]), tv
->row_size
);
1652 tv
->table
->data_persistent
[i
] = tv
->table
->data_persistent
[i
- 1];
1655 /* Re-set the persistence flag */
1656 tv
->table
->data_persistent
[row
] = !temporary
;
1657 return TABLE_set_row( view
, row
, rec
, (1<<tv
->num_cols
) - 1 );
1660 static UINT
TABLE_delete_row( struct tagMSIVIEW
*view
, UINT row
)
1662 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1663 UINT r
, num_rows
, num_cols
, i
;
1665 TRACE("%p %d\n", tv
, row
);
1668 return ERROR_INVALID_PARAMETER
;
1670 r
= TABLE_get_dimensions( view
, &num_rows
, &num_cols
);
1671 if ( r
!= ERROR_SUCCESS
)
1674 if ( row
>= num_rows
)
1675 return ERROR_FUNCTION_FAILED
;
1677 num_rows
= tv
->table
->row_count
;
1678 tv
->table
->row_count
--;
1680 /* reset the hash tables */
1681 for (i
= 0; i
< tv
->num_cols
; i
++)
1683 msi_free( tv
->columns
[i
].hash_table
);
1684 tv
->columns
[i
].hash_table
= NULL
;
1687 for (i
= row
+ 1; i
< num_rows
; i
++)
1689 memcpy(tv
->table
->data
[i
- 1], tv
->table
->data
[i
], tv
->row_size
);
1690 tv
->table
->data_persistent
[i
- 1] = tv
->table
->data_persistent
[i
];
1693 msi_free(tv
->table
->data
[num_rows
- 1]);
1695 return ERROR_SUCCESS
;
1698 static UINT
msi_table_update(struct tagMSIVIEW
*view
, MSIRECORD
*rec
, UINT row
)
1700 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1703 /* FIXME: MsiViewFetch should set rec index 0 to some ID that
1704 * sets the fetched record apart from other records
1708 return ERROR_INVALID_PARAMETER
;
1710 r
= msi_table_find_row(tv
, rec
, &new_row
, NULL
);
1711 if (r
!= ERROR_SUCCESS
)
1713 ERR("can't find row to modify\n");
1714 return ERROR_FUNCTION_FAILED
;
1717 /* the row cannot be changed */
1718 if (row
!= new_row
+ 1)
1719 return ERROR_FUNCTION_FAILED
;
1721 return TABLE_set_row(view
, new_row
, rec
, (1 << tv
->num_cols
) - 1);
1724 static UINT
msi_table_assign(struct tagMSIVIEW
*view
, MSIRECORD
*rec
)
1726 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1730 return ERROR_INVALID_PARAMETER
;
1732 r
= msi_table_find_row(tv
, rec
, &row
, NULL
);
1733 if (r
== ERROR_SUCCESS
)
1734 return TABLE_set_row(view
, row
, rec
, (1 << tv
->num_cols
) - 1);
1736 return TABLE_insert_row( view
, rec
, -1, FALSE
);
1739 static UINT
modify_delete_row( struct tagMSIVIEW
*view
, MSIRECORD
*rec
)
1741 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1744 r
= msi_table_find_row(tv
, rec
, &row
, NULL
);
1745 if (r
!= ERROR_SUCCESS
)
1748 return TABLE_delete_row(view
, row
);
1751 static UINT
msi_refresh_record( struct tagMSIVIEW
*view
, MSIRECORD
*rec
, UINT row
)
1756 r
= TABLE_get_row(view
, row
- 1, &curr
);
1757 if (r
!= ERROR_SUCCESS
)
1760 /* Close the original record */
1761 MSI_CloseRecord(&rec
->hdr
);
1763 count
= MSI_RecordGetFieldCount(rec
);
1764 for (i
= 0; i
< count
; i
++)
1765 MSI_RecordCopyField(curr
, i
+ 1, rec
, i
+ 1);
1767 msiobj_release(&curr
->hdr
);
1768 return ERROR_SUCCESS
;
1771 static UINT
TABLE_modify( struct tagMSIVIEW
*view
, MSIMODIFY eModifyMode
,
1772 MSIRECORD
*rec
, UINT row
)
1774 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1775 UINT r
, frow
, column
;
1777 TRACE("%p %d %p\n", view
, eModifyMode
, rec
);
1779 switch (eModifyMode
)
1781 case MSIMODIFY_DELETE
:
1782 r
= modify_delete_row( view
, rec
);
1784 case MSIMODIFY_VALIDATE_NEW
:
1785 r
= table_validate_new( tv
, rec
, &column
);
1786 if (r
!= ERROR_SUCCESS
)
1788 tv
->view
.error
= MSIDBERROR_DUPLICATEKEY
;
1789 tv
->view
.error_column
= tv
->columns
[column
].colname
;
1790 r
= ERROR_INVALID_DATA
;
1794 case MSIMODIFY_INSERT
:
1795 r
= table_validate_new( tv
, rec
, NULL
);
1796 if (r
!= ERROR_SUCCESS
)
1798 r
= TABLE_insert_row( view
, rec
, -1, FALSE
);
1801 case MSIMODIFY_INSERT_TEMPORARY
:
1802 r
= table_validate_new( tv
, rec
, NULL
);
1803 if (r
!= ERROR_SUCCESS
)
1805 r
= TABLE_insert_row( view
, rec
, -1, TRUE
);
1808 case MSIMODIFY_REFRESH
:
1809 r
= msi_refresh_record( view
, rec
, row
);
1812 case MSIMODIFY_UPDATE
:
1813 r
= msi_table_update( view
, rec
, row
);
1816 case MSIMODIFY_ASSIGN
:
1817 r
= msi_table_assign( view
, rec
);
1820 case MSIMODIFY_MERGE
:
1821 /* check row that matches this record */
1822 r
= msi_table_find_row( tv
, rec
, &frow
, &column
);
1823 if (r
!= ERROR_SUCCESS
)
1825 r
= table_validate_new( tv
, rec
, NULL
);
1826 if (r
== ERROR_SUCCESS
)
1827 r
= TABLE_insert_row( view
, rec
, -1, FALSE
);
1831 case MSIMODIFY_REPLACE
:
1832 case MSIMODIFY_VALIDATE
:
1833 case MSIMODIFY_VALIDATE_FIELD
:
1834 case MSIMODIFY_VALIDATE_DELETE
:
1835 FIXME("%p %d %p - mode not implemented\n", view
, eModifyMode
, rec
);
1836 r
= ERROR_CALL_NOT_IMPLEMENTED
;
1840 r
= ERROR_INVALID_DATA
;
1846 static UINT
TABLE_delete( struct tagMSIVIEW
*view
)
1848 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1850 TRACE("%p\n", view
);
1857 return ERROR_SUCCESS
;
1860 static UINT
TABLE_find_matching_rows( struct tagMSIVIEW
*view
, UINT col
,
1861 UINT val
, UINT
*row
, MSIITERHANDLE
*handle
)
1863 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1864 const MSICOLUMNHASHENTRY
*entry
;
1866 TRACE("%p, %d, %u, %p\n", view
, col
, val
, *handle
);
1869 return ERROR_INVALID_PARAMETER
;
1871 if( (col
==0) || (col
> tv
->num_cols
) )
1872 return ERROR_INVALID_PARAMETER
;
1874 if( !tv
->columns
[col
-1].hash_table
)
1877 UINT num_rows
= tv
->table
->row_count
;
1878 MSICOLUMNHASHENTRY
**hash_table
;
1879 MSICOLUMNHASHENTRY
*new_entry
;
1881 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1883 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1884 ERR("%p %p\n", tv
, tv
->columns
);
1885 return ERROR_FUNCTION_FAILED
;
1888 /* allocate contiguous memory for the table and its entries so we
1889 * don't have to do an expensive cleanup */
1890 hash_table
= msi_alloc(MSITABLE_HASH_TABLE_SIZE
* sizeof(MSICOLUMNHASHENTRY
*) +
1891 num_rows
* sizeof(MSICOLUMNHASHENTRY
));
1893 return ERROR_OUTOFMEMORY
;
1895 memset(hash_table
, 0, MSITABLE_HASH_TABLE_SIZE
* sizeof(MSICOLUMNHASHENTRY
*));
1896 tv
->columns
[col
-1].hash_table
= hash_table
;
1898 new_entry
= (MSICOLUMNHASHENTRY
*)(hash_table
+ MSITABLE_HASH_TABLE_SIZE
);
1900 for (i
= 0; i
< num_rows
; i
++, new_entry
++)
1904 if (view
->ops
->fetch_int( view
, i
, col
, &row_value
) != ERROR_SUCCESS
)
1907 new_entry
->next
= NULL
;
1908 new_entry
->value
= row_value
;
1910 if (hash_table
[row_value
% MSITABLE_HASH_TABLE_SIZE
])
1912 MSICOLUMNHASHENTRY
*prev_entry
= hash_table
[row_value
% MSITABLE_HASH_TABLE_SIZE
];
1913 while (prev_entry
->next
)
1914 prev_entry
= prev_entry
->next
;
1915 prev_entry
->next
= new_entry
;
1918 hash_table
[row_value
% MSITABLE_HASH_TABLE_SIZE
] = new_entry
;
1923 entry
= tv
->columns
[col
-1].hash_table
[val
% MSITABLE_HASH_TABLE_SIZE
];
1925 entry
= (*handle
)->next
;
1927 while (entry
&& entry
->value
!= val
)
1928 entry
= entry
->next
;
1932 return ERROR_NO_MORE_ITEMS
;
1936 return ERROR_SUCCESS
;
1939 static UINT
TABLE_add_ref(struct tagMSIVIEW
*view
)
1941 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1944 TRACE("%p %d\n", view
, tv
->table
->ref_count
);
1946 for (i
= 0; i
< tv
->table
->col_count
; i
++)
1948 if (tv
->table
->colinfo
[i
].type
& MSITYPE_TEMPORARY
)
1949 InterlockedIncrement(&tv
->table
->colinfo
[i
].ref_count
);
1952 return InterlockedIncrement(&tv
->table
->ref_count
);
1955 static UINT
TABLE_remove_column(struct tagMSIVIEW
*view
, LPCWSTR table
, UINT number
)
1957 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1958 MSIRECORD
*rec
= NULL
;
1959 MSIVIEW
*columns
= NULL
;
1962 rec
= MSI_CreateRecord(2);
1964 return ERROR_OUTOFMEMORY
;
1966 MSI_RecordSetStringW(rec
, 1, table
);
1967 MSI_RecordSetInteger(rec
, 2, number
);
1969 r
= TABLE_CreateView(tv
->db
, szColumns
, &columns
);
1970 if (r
!= ERROR_SUCCESS
)
1973 r
= msi_table_find_row((MSITABLEVIEW
*)columns
, rec
, &row
, NULL
);
1974 if (r
!= ERROR_SUCCESS
)
1977 r
= TABLE_delete_row(columns
, row
);
1978 if (r
!= ERROR_SUCCESS
)
1981 msi_update_table_columns(tv
->db
, table
);
1984 msiobj_release(&rec
->hdr
);
1985 columns
->ops
->delete(columns
);
1989 static UINT
TABLE_release(struct tagMSIVIEW
*view
)
1991 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1992 INT ref
= tv
->table
->ref_count
;
1995 TRACE("%p %d\n", view
, ref
);
1997 for (i
= 0; i
< tv
->table
->col_count
; i
++)
1999 if (tv
->table
->colinfo
[i
].type
& MSITYPE_TEMPORARY
)
2001 ref
= InterlockedDecrement(&tv
->table
->colinfo
[i
].ref_count
);
2004 r
= TABLE_remove_column(view
, tv
->table
->colinfo
[i
].tablename
,
2005 tv
->table
->colinfo
[i
].number
);
2006 if (r
!= ERROR_SUCCESS
)
2012 ref
= InterlockedDecrement(&tv
->table
->ref_count
);
2015 if (!tv
->table
->row_count
)
2017 list_remove(&tv
->table
->entry
);
2018 free_table(tv
->table
);
2026 static UINT
TABLE_add_column(struct tagMSIVIEW
*view
, LPCWSTR table
, UINT number
,
2027 LPCWSTR column
, UINT type
, BOOL hold
)
2029 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
2034 rec
= MSI_CreateRecord(4);
2036 return ERROR_OUTOFMEMORY
;
2038 MSI_RecordSetStringW(rec
, 1, table
);
2039 MSI_RecordSetInteger(rec
, 2, number
);
2040 MSI_RecordSetStringW(rec
, 3, column
);
2041 MSI_RecordSetInteger(rec
, 4, type
);
2043 r
= TABLE_insert_row(&tv
->view
, rec
, -1, FALSE
);
2044 if (r
!= ERROR_SUCCESS
)
2047 msi_update_table_columns(tv
->db
, table
);
2052 msitable
= find_cached_table(tv
->db
, table
);
2053 for (i
= 0; i
< msitable
->col_count
; i
++)
2055 if (!strcmpW( msitable
->colinfo
[i
].colname
, column
))
2057 InterlockedIncrement(&msitable
->colinfo
[i
].ref_count
);
2063 msiobj_release(&rec
->hdr
);
2067 static UINT
TABLE_drop(struct tagMSIVIEW
*view
)
2069 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
2070 MSIVIEW
*tables
= NULL
;
2071 MSIRECORD
*rec
= NULL
;
2075 TRACE("dropping table %s\n", debugstr_w(tv
->name
));
2077 for (i
= tv
->table
->col_count
- 1; i
>= 0; i
--)
2079 r
= TABLE_remove_column(view
, tv
->table
->colinfo
[i
].tablename
,
2080 tv
->table
->colinfo
[i
].number
);
2081 if (r
!= ERROR_SUCCESS
)
2085 rec
= MSI_CreateRecord(1);
2087 return ERROR_OUTOFMEMORY
;
2089 MSI_RecordSetStringW(rec
, 1, tv
->name
);
2091 r
= TABLE_CreateView(tv
->db
, szTables
, &tables
);
2092 if (r
!= ERROR_SUCCESS
)
2095 r
= msi_table_find_row((MSITABLEVIEW
*)tables
, rec
, &row
, NULL
);
2096 if (r
!= ERROR_SUCCESS
)
2099 r
= TABLE_delete_row(tables
, row
);
2100 if (r
!= ERROR_SUCCESS
)
2103 list_remove(&tv
->table
->entry
);
2104 free_table(tv
->table
);
2107 msiobj_release(&rec
->hdr
);
2108 tables
->ops
->delete(tables
);
2113 static const MSIVIEWOPS table_ops
=
2123 TABLE_get_dimensions
,
2124 TABLE_get_column_info
,
2127 TABLE_find_matching_rows
,
2131 TABLE_remove_column
,
2136 UINT
TABLE_CreateView( MSIDATABASE
*db
, LPCWSTR name
, MSIVIEW
**view
)
2141 TRACE("%p %s %p\n", db
, debugstr_w(name
), view
);
2143 if ( !strcmpW( name
, szStreams
) )
2144 return STREAMS_CreateView( db
, view
);
2145 else if ( !strcmpW( name
, szStorages
) )
2146 return STORAGES_CreateView( db
, view
);
2148 sz
= FIELD_OFFSET( MSITABLEVIEW
, name
[lstrlenW( name
) + 1] );
2149 tv
= msi_alloc_zero( sz
);
2151 return ERROR_FUNCTION_FAILED
;
2153 r
= get_table( db
, name
, &tv
->table
);
2154 if( r
!= ERROR_SUCCESS
)
2157 WARN("table not found\n");
2161 TRACE("table %p found with %d columns\n", tv
->table
, tv
->table
->col_count
);
2163 /* fill the structure */
2164 tv
->view
.ops
= &table_ops
;
2166 tv
->columns
= tv
->table
->colinfo
;
2167 tv
->num_cols
= tv
->table
->col_count
;
2168 tv
->row_size
= msi_table_get_row_size( db
, tv
->table
->colinfo
, tv
->table
->col_count
, LONG_STR_BYTES
);
2170 TRACE("%s one row is %d bytes\n", debugstr_w(name
), tv
->row_size
);
2172 *view
= (MSIVIEW
*) tv
;
2173 lstrcpyW( tv
->name
, name
);
2175 return ERROR_SUCCESS
;
2178 UINT
MSI_CommitTables( MSIDATABASE
*db
)
2180 UINT r
, bytes_per_strref
;
2182 MSITABLE
*table
= NULL
;
2186 r
= msi_save_string_table( db
->strings
, db
->storage
, &bytes_per_strref
);
2187 if( r
!= ERROR_SUCCESS
)
2189 WARN("failed to save string table r=%08x\n",r
);
2193 LIST_FOR_EACH_ENTRY( table
, &db
->tables
, MSITABLE
, entry
)
2195 r
= save_table( db
, table
, bytes_per_strref
);
2196 if( r
!= ERROR_SUCCESS
)
2198 WARN("failed to save table %s (r=%08x)\n",
2199 debugstr_w(table
->name
), r
);
2204 hr
= IStorage_Commit( db
->storage
, 0 );
2207 WARN("failed to commit changes 0x%08x\n", hr
);
2208 r
= ERROR_FUNCTION_FAILED
;
2213 MSICONDITION
MSI_DatabaseIsTablePersistent( MSIDATABASE
*db
, LPCWSTR table
)
2218 TRACE("%p %s\n", db
, debugstr_w(table
));
2221 return MSICONDITION_ERROR
;
2223 r
= get_table( db
, table
, &t
);
2224 if (r
!= ERROR_SUCCESS
)
2225 return MSICONDITION_NONE
;
2227 return t
->persistent
;
2230 static UINT
read_raw_int(const BYTE
*data
, UINT col
, UINT bytes
)
2234 for (i
= 0; i
< bytes
; i
++)
2235 ret
+= (data
[col
+ i
] << i
* 8);
2240 static UINT
msi_record_encoded_stream_name( const MSITABLEVIEW
*tv
, MSIRECORD
*rec
, LPWSTR
*pstname
)
2242 LPWSTR stname
= NULL
, sval
, p
;
2246 TRACE("%p %p\n", tv
, rec
);
2248 len
= lstrlenW( tv
->name
) + 1;
2249 stname
= msi_alloc( len
*sizeof(WCHAR
) );
2252 r
= ERROR_OUTOFMEMORY
;
2256 lstrcpyW( stname
, tv
->name
);
2258 for ( i
= 0; i
< tv
->num_cols
; i
++ )
2260 if ( tv
->columns
[i
].type
& MSITYPE_KEY
)
2262 sval
= msi_dup_record_field( rec
, i
+ 1 );
2265 r
= ERROR_OUTOFMEMORY
;
2269 len
+= lstrlenW( szDot
) + lstrlenW ( sval
);
2270 p
= msi_realloc ( stname
, len
*sizeof(WCHAR
) );
2273 r
= ERROR_OUTOFMEMORY
;
2279 lstrcatW( stname
, szDot
);
2280 lstrcatW( stname
, sval
);
2288 *pstname
= encode_streamname( FALSE
, stname
);
2291 return ERROR_SUCCESS
;
2294 msi_free ( stname
);
2299 static MSIRECORD
*msi_get_transform_record( const MSITABLEVIEW
*tv
, const string_table
*st
,
2301 const BYTE
*rawdata
, UINT bytes_per_strref
)
2303 UINT i
, val
, ofs
= 0;
2305 MSICOLUMNINFO
*columns
= tv
->columns
;
2308 mask
= rawdata
[0] | (rawdata
[1] << 8);
2311 rec
= MSI_CreateRecord( tv
->num_cols
);
2316 for( i
=0; i
<tv
->num_cols
; i
++ )
2318 if ( (mask
&1) && (i
>=(mask
>>8)) )
2320 /* all keys must be present */
2321 if ( (~mask
&1) && (~columns
[i
].type
& MSITYPE_KEY
) && ((1<<i
) & ~mask
) )
2324 if( MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
2327 IStream
*stm
= NULL
;
2330 ofs
+= bytes_per_column( tv
->db
, &columns
[i
], bytes_per_strref
);
2332 r
= msi_record_encoded_stream_name( tv
, rec
, &encname
);
2333 if ( r
!= ERROR_SUCCESS
)
2336 r
= IStorage_OpenStream( stg
, encname
, NULL
,
2337 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, &stm
);
2338 if ( r
!= ERROR_SUCCESS
)
2340 msi_free( encname
);
2344 MSI_RecordSetStream( rec
, i
+1, stm
);
2345 TRACE(" field %d [%s]\n", i
+1, debugstr_w(encname
));
2346 msi_free( encname
);
2348 else if( columns
[i
].type
& MSITYPE_STRING
)
2353 val
= read_raw_int(rawdata
, ofs
, bytes_per_strref
);
2354 sval
= msi_string_lookup( st
, val
, &len
);
2355 msi_record_set_string( rec
, i
+1, sval
, len
);
2356 TRACE(" field %d [%s]\n", i
+1, debugstr_wn(sval
, len
));
2357 ofs
+= bytes_per_strref
;
2361 UINT n
= bytes_per_column( tv
->db
, &columns
[i
], bytes_per_strref
);
2365 val
= read_raw_int(rawdata
, ofs
, n
);
2367 MSI_RecordSetInteger( rec
, i
+1, val
-0x8000 );
2368 TRACE(" field %d [0x%04x]\n", i
+1, val
);
2371 val
= read_raw_int(rawdata
, ofs
, n
);
2373 MSI_RecordSetInteger( rec
, i
+1, val
^0x80000000 );
2374 TRACE(" field %d [0x%08x]\n", i
+1, val
);
2377 ERR("oops - unknown column width %d\n", n
);
2386 static void dump_record( MSIRECORD
*rec
)
2390 n
= MSI_RecordGetFieldCount( rec
);
2391 for( i
=1; i
<=n
; i
++ )
2396 if( MSI_RecordIsNull( rec
, i
) )
2397 TRACE("row -> []\n");
2398 else if( (sval
= msi_record_get_string( rec
, i
, &len
)) )
2399 TRACE("row -> [%s]\n", debugstr_wn(sval
, len
));
2401 TRACE("row -> [0x%08x]\n", MSI_RecordGetInteger( rec
, i
) );
2405 static void dump_table( const string_table
*st
, const USHORT
*rawdata
, UINT rawsize
)
2408 for (i
= 0; i
< rawsize
/ 2; i
++)
2411 const WCHAR
*sval
= msi_string_lookup( st
, rawdata
[i
], &len
);
2412 MESSAGE(" %04x %s\n", rawdata
[i
], debugstr_wn(sval
, len
) );
2416 static UINT
* msi_record_to_row( const MSITABLEVIEW
*tv
, MSIRECORD
*rec
)
2420 data
= msi_alloc( tv
->num_cols
*sizeof (UINT
) );
2421 for( i
=0; i
<tv
->num_cols
; i
++ )
2425 if ( ~tv
->columns
[i
].type
& MSITYPE_KEY
)
2428 /* turn the transform column value into a row value */
2429 if ( ( tv
->columns
[i
].type
& MSITYPE_STRING
) &&
2430 ! MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
2433 const WCHAR
*str
= msi_record_get_string( rec
, i
+1, &len
);
2436 r
= msi_string2id( tv
->db
->strings
, str
, len
, &data
[i
] );
2438 /* if there's no matching string in the string table,
2439 these keys can't match any record, so fail now. */
2440 if (r
!= ERROR_SUCCESS
)
2450 data
[i
] = MSI_RecordGetInteger( rec
, i
+1 );
2452 if (data
[i
] == MSI_NULL_INTEGER
)
2454 else if ((tv
->columns
[i
].type
&0xff) == 2)
2457 data
[i
] += 0x80000000;
2463 static UINT
msi_row_matches( MSITABLEVIEW
*tv
, UINT row
, const UINT
*data
, UINT
*column
)
2465 UINT i
, r
, x
, ret
= ERROR_FUNCTION_FAILED
;
2467 for( i
=0; i
<tv
->num_cols
; i
++ )
2469 if ( ~tv
->columns
[i
].type
& MSITYPE_KEY
)
2472 /* turn the transform column value into a row value */
2473 r
= TABLE_fetch_int( &tv
->view
, row
, i
+1, &x
);
2474 if ( r
!= ERROR_SUCCESS
)
2476 ERR("TABLE_fetch_int shouldn't fail here\n");
2480 /* if this key matches, move to the next column */
2483 ret
= ERROR_FUNCTION_FAILED
;
2486 if (column
) *column
= i
;
2487 ret
= ERROR_SUCCESS
;
2492 static UINT
msi_table_find_row( MSITABLEVIEW
*tv
, MSIRECORD
*rec
, UINT
*row
, UINT
*column
)
2494 UINT i
, r
= ERROR_FUNCTION_FAILED
, *data
;
2496 data
= msi_record_to_row( tv
, rec
);
2499 for( i
= 0; i
< tv
->table
->row_count
; i
++ )
2501 r
= msi_row_matches( tv
, i
, data
, column
);
2502 if( r
== ERROR_SUCCESS
)
2518 static UINT
msi_table_load_transform( MSIDATABASE
*db
, IStorage
*stg
,
2519 string_table
*st
, TRANSFORMDATA
*transform
,
2520 UINT bytes_per_strref
)
2522 BYTE
*rawdata
= NULL
;
2523 MSITABLEVIEW
*tv
= NULL
;
2524 UINT r
, n
, sz
, i
, mask
, num_cols
, colcol
= 0, rawsize
= 0;
2525 MSIRECORD
*rec
= NULL
;
2530 return ERROR_SUCCESS
;
2532 name
= transform
->name
;
2535 TRACE("%p %p %p %s\n", db
, stg
, st
, debugstr_w(name
) );
2537 /* read the transform data */
2538 read_stream_data( stg
, name
, TRUE
, &rawdata
, &rawsize
);
2541 TRACE("table %s empty\n", debugstr_w(name
) );
2542 return ERROR_INVALID_TABLE
;
2545 /* create a table view */
2546 r
= TABLE_CreateView( db
, name
, (MSIVIEW
**) &tv
);
2547 if( r
!= ERROR_SUCCESS
)
2550 r
= tv
->view
.ops
->execute( &tv
->view
, NULL
);
2551 if( r
!= ERROR_SUCCESS
)
2554 TRACE("name = %s columns = %u row_size = %u raw size = %u\n",
2555 debugstr_w(name
), tv
->num_cols
, tv
->row_size
, rawsize
);
2557 /* interpret the data */
2558 for (n
= 0; n
< rawsize
;)
2560 mask
= rawdata
[n
] | (rawdata
[n
+ 1] << 8);
2564 * if the low bit is set, columns are continuous and
2565 * the number of columns is specified in the high byte
2568 num_cols
= mask
>> 8;
2569 for (i
= 0; i
< num_cols
; i
++)
2571 if( (tv
->columns
[i
].type
& MSITYPE_STRING
) &&
2572 ! MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
2573 sz
+= bytes_per_strref
;
2575 sz
+= bytes_per_column( tv
->db
, &tv
->columns
[i
], bytes_per_strref
);
2581 * If the low bit is not set, mask is a bitmask.
2582 * Excepting for key fields, which are always present,
2583 * each bit indicates that a field is present in the transform record.
2585 * mask == 0 is a special case ... only the keys will be present
2586 * and it means that this row should be deleted.
2589 num_cols
= tv
->num_cols
;
2590 for (i
= 0; i
< num_cols
; i
++)
2592 if ((tv
->columns
[i
].type
& MSITYPE_KEY
) || ((1 << i
) & mask
))
2594 if ((tv
->columns
[i
].type
& MSITYPE_STRING
) &&
2595 !MSITYPE_IS_BINARY(tv
->columns
[i
].type
))
2596 sz
+= bytes_per_strref
;
2598 sz
+= bytes_per_column( tv
->db
, &tv
->columns
[i
], bytes_per_strref
);
2603 /* check we didn't run of the end of the table */
2604 if (n
+ sz
> rawsize
)
2607 dump_table( st
, (USHORT
*)rawdata
, rawsize
);
2611 rec
= msi_get_transform_record( tv
, st
, stg
, &rawdata
[n
], bytes_per_strref
);
2616 UINT number
= MSI_NULL_INTEGER
;
2619 if (!strcmpW( name
, szColumns
))
2621 MSI_RecordGetStringW( rec
, 1, table
, &sz
);
2622 number
= MSI_RecordGetInteger( rec
, 2 );
2625 * Native msi seems writes nul into the Number (2nd) column of
2626 * the _Columns table, only when the columns are from a new table
2628 if ( number
== MSI_NULL_INTEGER
)
2630 /* reset the column number on a new table */
2631 if (strcmpW( coltable
, table
))
2634 lstrcpyW( coltable
, table
);
2637 /* fix nul column numbers */
2638 MSI_RecordSetInteger( rec
, 2, ++colcol
);
2642 if (TRACE_ON(msidb
)) dump_record( rec
);
2644 r
= msi_table_find_row( tv
, rec
, &row
, NULL
);
2645 if (r
== ERROR_SUCCESS
)
2649 TRACE("deleting row [%d]:\n", row
);
2650 r
= TABLE_delete_row( &tv
->view
, row
);
2651 if (r
!= ERROR_SUCCESS
)
2652 WARN("failed to delete row %u\n", r
);
2656 TRACE("modifying full row [%d]:\n", row
);
2657 r
= TABLE_set_row( &tv
->view
, row
, rec
, (1 << tv
->num_cols
) - 1 );
2658 if (r
!= ERROR_SUCCESS
)
2659 WARN("failed to modify row %u\n", r
);
2663 TRACE("modifying masked row [%d]:\n", row
);
2664 r
= TABLE_set_row( &tv
->view
, row
, rec
, mask
);
2665 if (r
!= ERROR_SUCCESS
)
2666 WARN("failed to modify row %u\n", r
);
2671 TRACE("inserting row\n");
2672 r
= TABLE_insert_row( &tv
->view
, rec
, -1, FALSE
);
2673 if (r
!= ERROR_SUCCESS
)
2674 WARN("failed to insert row %u\n", r
);
2677 if (number
!= MSI_NULL_INTEGER
&& !strcmpW( name
, szColumns
))
2678 msi_update_table_columns( db
, table
);
2680 msiobj_release( &rec
->hdr
);
2687 /* no need to free the table, it's associated with the database */
2688 msi_free( rawdata
);
2690 tv
->view
.ops
->delete( &tv
->view
);
2692 return ERROR_SUCCESS
;
2696 * msi_table_apply_transform
2698 * Enumerate the table transforms in a transform storage and apply each one.
2700 UINT
msi_table_apply_transform( MSIDATABASE
*db
, IStorage
*stg
)
2702 struct list transforms
;
2703 IEnumSTATSTG
*stgenum
= NULL
;
2704 TRANSFORMDATA
*transform
;
2705 TRANSFORMDATA
*tables
= NULL
, *columns
= NULL
;
2708 string_table
*strings
;
2709 UINT ret
= ERROR_FUNCTION_FAILED
;
2710 UINT bytes_per_strref
;
2712 TRACE("%p %p\n", db
, stg
);
2714 strings
= msi_load_string_table( stg
, &bytes_per_strref
);
2718 r
= IStorage_EnumElements( stg
, 0, NULL
, 0, &stgenum
);
2722 list_init(&transforms
);
2726 MSITABLEVIEW
*tv
= NULL
;
2730 r
= IEnumSTATSTG_Next( stgenum
, 1, &stat
, &count
);
2731 if ( FAILED( r
) || !count
)
2734 decode_streamname( stat
.pwcsName
, name
);
2735 CoTaskMemFree( stat
.pwcsName
);
2736 if ( name
[0] != 0x4840 )
2739 if ( !strcmpW( name
+1, szStringPool
) ||
2740 !strcmpW( name
+1, szStringData
) )
2743 transform
= msi_alloc_zero( sizeof(TRANSFORMDATA
) );
2747 list_add_tail( &transforms
, &transform
->entry
);
2749 transform
->name
= strdupW( name
+ 1 );
2751 if ( !strcmpW( transform
->name
, szTables
) )
2753 else if (!strcmpW( transform
->name
, szColumns
) )
2754 columns
= transform
;
2756 TRACE("transform contains stream %s\n", debugstr_w(name
));
2758 /* load the table */
2759 r
= TABLE_CreateView( db
, transform
->name
, (MSIVIEW
**) &tv
);
2760 if( r
!= ERROR_SUCCESS
)
2763 r
= tv
->view
.ops
->execute( &tv
->view
, NULL
);
2764 if( r
!= ERROR_SUCCESS
)
2766 tv
->view
.ops
->delete( &tv
->view
);
2770 tv
->view
.ops
->delete( &tv
->view
);
2774 * Apply _Tables and _Columns transforms first so that
2775 * the table metadata is correct, and empty tables exist.
2777 ret
= msi_table_load_transform( db
, stg
, strings
, tables
, bytes_per_strref
);
2778 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_INVALID_TABLE
)
2781 ret
= msi_table_load_transform( db
, stg
, strings
, columns
, bytes_per_strref
);
2782 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_INVALID_TABLE
)
2785 ret
= ERROR_SUCCESS
;
2787 while ( !list_empty( &transforms
) )
2789 transform
= LIST_ENTRY( list_head( &transforms
), TRANSFORMDATA
, entry
);
2791 if ( strcmpW( transform
->name
, szColumns
) &&
2792 strcmpW( transform
->name
, szTables
) &&
2793 ret
== ERROR_SUCCESS
)
2795 ret
= msi_table_load_transform( db
, stg
, strings
, transform
, bytes_per_strref
);
2798 list_remove( &transform
->entry
);
2799 msi_free( transform
->name
);
2800 msi_free( transform
);
2803 if ( ret
== ERROR_SUCCESS
)
2804 append_storage_to_db( db
, stg
);
2808 IEnumSTATSTG_Release( stgenum
);
2810 msi_destroy_stringtable( strings
);