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
;
1268 if ( (iField
<= 0) ||
1269 (iField
> tv
->num_cols
) ||
1270 MSI_RecordIsNull( rec
, iField
) )
1271 return ERROR_FUNCTION_FAILED
;
1273 columninfo
= tv
->columns
[ iField
- 1 ];
1275 if ( MSITYPE_IS_BINARY(columninfo
.type
) )
1277 *pvalue
= 1; /* refers to the first key column */
1279 else if ( columninfo
.type
& MSITYPE_STRING
)
1282 const WCHAR
*sval
= msi_record_get_string( rec
, iField
, &len
);
1285 r
= msi_string2id( tv
->db
->strings
, sval
, len
, pvalue
);
1286 if (r
!= ERROR_SUCCESS
)
1287 return ERROR_NOT_FOUND
;
1291 else if ( bytes_per_column( tv
->db
, &columninfo
, LONG_STR_BYTES
) == 2 )
1293 ival
= MSI_RecordGetInteger( rec
, iField
);
1294 if (ival
== 0x80000000) *pvalue
= 0x8000;
1297 *pvalue
= 0x8000 + MSI_RecordGetInteger( rec
, iField
);
1298 if (*pvalue
& 0xffff0000)
1300 ERR("field %u value %d out of range\n", iField
, *pvalue
- 0x8000);
1301 return ERROR_FUNCTION_FAILED
;
1307 ival
= MSI_RecordGetInteger( rec
, iField
);
1308 *pvalue
= ival
^ 0x80000000;
1311 return ERROR_SUCCESS
;
1314 static UINT
TABLE_set_row( struct tagMSIVIEW
*view
, UINT row
, MSIRECORD
*rec
, UINT mask
)
1316 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1317 UINT i
, val
, r
= ERROR_SUCCESS
;
1320 return ERROR_INVALID_PARAMETER
;
1322 /* test if any of the mask bits are invalid */
1323 if ( mask
>= (1<<tv
->num_cols
) )
1324 return ERROR_INVALID_PARAMETER
;
1326 for ( i
= 0; i
< tv
->num_cols
; i
++ )
1330 /* only update the fields specified in the mask */
1331 if ( !(mask
&(1<<i
)) )
1334 persistent
= (tv
->table
->persistent
!= MSICONDITION_FALSE
) &&
1335 (tv
->table
->data_persistent
[row
]);
1336 /* FIXME: should we allow updating keys? */
1339 if ( !MSI_RecordIsNull( rec
, i
+ 1 ) )
1341 r
= get_table_value_from_record (tv
, rec
, i
+ 1, &val
);
1342 if ( MSITYPE_IS_BINARY(tv
->columns
[ i
].type
) )
1347 if ( r
!= ERROR_SUCCESS
)
1348 return ERROR_FUNCTION_FAILED
;
1350 r
= MSI_RecordGetIStream( rec
, i
+ 1, &stm
);
1351 if ( r
!= ERROR_SUCCESS
)
1354 r
= msi_stream_name( tv
, row
, &stname
);
1355 if ( r
!= ERROR_SUCCESS
)
1357 IStream_Release( stm
);
1361 r
= msi_addstreamW( tv
->db
, stname
, stm
);
1362 IStream_Release( stm
);
1363 msi_free ( stname
);
1365 if ( r
!= ERROR_SUCCESS
)
1368 else if ( tv
->columns
[i
].type
& MSITYPE_STRING
)
1372 if ( r
!= ERROR_SUCCESS
)
1375 const WCHAR
*sval
= msi_record_get_string( rec
, i
+ 1, &len
);
1376 val
= msi_addstringW( tv
->db
->strings
, sval
, len
, 1,
1377 persistent
? StringPersistent
: StringNonPersistent
);
1381 TABLE_fetch_int(&tv
->view
, row
, i
+ 1, &x
);
1388 if ( r
!= ERROR_SUCCESS
)
1389 return ERROR_FUNCTION_FAILED
;
1393 r
= TABLE_set_int( tv
, row
, i
+1, val
);
1394 if ( r
!= ERROR_SUCCESS
)
1400 static UINT
table_create_new_row( struct tagMSIVIEW
*view
, UINT
*num
, BOOL temporary
)
1402 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1407 BOOL
**data_persist_ptr
;
1410 TRACE("%p %s\n", view
, temporary
? "TRUE" : "FALSE");
1413 return ERROR_INVALID_PARAMETER
;
1415 row
= msi_alloc_zero( tv
->row_size
);
1417 return ERROR_NOT_ENOUGH_MEMORY
;
1419 row_count
= &tv
->table
->row_count
;
1420 data_ptr
= &tv
->table
->data
;
1421 data_persist_ptr
= &tv
->table
->data_persistent
;
1423 *num
= tv
->table
->row_count
;
1425 sz
= (*row_count
+ 1) * sizeof (BYTE
*);
1427 p
= msi_realloc( *data_ptr
, sz
);
1429 p
= msi_alloc( sz
);
1433 return ERROR_NOT_ENOUGH_MEMORY
;
1436 sz
= (*row_count
+ 1) * sizeof (BOOL
);
1437 if( *data_persist_ptr
)
1438 b
= msi_realloc( *data_persist_ptr
, sz
);
1440 b
= msi_alloc( sz
);
1445 return ERROR_NOT_ENOUGH_MEMORY
;
1449 (*data_ptr
)[*row_count
] = row
;
1451 *data_persist_ptr
= b
;
1452 (*data_persist_ptr
)[*row_count
] = !temporary
;
1456 return ERROR_SUCCESS
;
1459 static UINT
TABLE_execute( struct tagMSIVIEW
*view
, MSIRECORD
*record
)
1461 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1463 TRACE("%p %p\n", tv
, record
);
1465 TRACE("There are %d columns\n", tv
->num_cols
);
1467 return ERROR_SUCCESS
;
1470 static UINT
TABLE_close( struct tagMSIVIEW
*view
)
1472 TRACE("%p\n", view
);
1474 return ERROR_SUCCESS
;
1477 static UINT
TABLE_get_dimensions( struct tagMSIVIEW
*view
, UINT
*rows
, UINT
*cols
)
1479 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1481 TRACE("%p %p %p\n", view
, rows
, cols
);
1484 *cols
= tv
->num_cols
;
1488 return ERROR_INVALID_PARAMETER
;
1489 *rows
= tv
->table
->row_count
;
1492 return ERROR_SUCCESS
;
1495 static UINT
TABLE_get_column_info( struct tagMSIVIEW
*view
,
1496 UINT n
, LPCWSTR
*name
, UINT
*type
, BOOL
*temporary
,
1497 LPCWSTR
*table_name
)
1499 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1501 TRACE("%p %d %p %p\n", tv
, n
, name
, type
);
1503 if( ( n
== 0 ) || ( n
> tv
->num_cols
) )
1504 return ERROR_INVALID_PARAMETER
;
1508 *name
= tv
->columns
[n
-1].colname
;
1510 return ERROR_FUNCTION_FAILED
;
1515 *table_name
= tv
->columns
[n
-1].tablename
;
1517 return ERROR_FUNCTION_FAILED
;
1521 *type
= tv
->columns
[n
-1].type
;
1524 *temporary
= tv
->columns
[n
-1].temporary
;
1526 return ERROR_SUCCESS
;
1529 static UINT
msi_table_find_row( MSITABLEVIEW
*tv
, MSIRECORD
*rec
, UINT
*row
, UINT
*column
);
1531 static UINT
table_validate_new( MSITABLEVIEW
*tv
, MSIRECORD
*rec
, UINT
*column
)
1535 /* check there's no null values where they're not allowed */
1536 for( i
= 0; i
< tv
->num_cols
; i
++ )
1538 if ( tv
->columns
[i
].type
& MSITYPE_NULLABLE
)
1541 if ( MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
1542 TRACE("skipping binary column\n");
1543 else if ( tv
->columns
[i
].type
& MSITYPE_STRING
)
1546 const WCHAR
*str
= msi_record_get_string( rec
, i
+1, &len
);
1548 if (!str
|| (!str
[0] && !len
))
1550 if (column
) *column
= i
;
1551 return ERROR_INVALID_DATA
;
1558 n
= MSI_RecordGetInteger( rec
, i
+1 );
1559 if (n
== MSI_NULL_INTEGER
)
1561 if (column
) *column
= i
;
1562 return ERROR_INVALID_DATA
;
1567 /* check there's no duplicate keys */
1568 r
= msi_table_find_row( tv
, rec
, &row
, column
);
1569 if (r
== ERROR_SUCCESS
)
1570 return ERROR_FUNCTION_FAILED
;
1572 return ERROR_SUCCESS
;
1575 static int compare_record( MSITABLEVIEW
*tv
, UINT row
, MSIRECORD
*rec
)
1577 UINT r
, i
, ivalue
, x
;
1579 for (i
= 0; i
< tv
->num_cols
; i
++ )
1581 if (!(tv
->columns
[i
].type
& MSITYPE_KEY
)) continue;
1583 r
= get_table_value_from_record( tv
, rec
, i
+ 1, &ivalue
);
1584 if (r
!= ERROR_SUCCESS
)
1587 r
= TABLE_fetch_int( &tv
->view
, row
, i
+ 1, &x
);
1588 if (r
!= ERROR_SUCCESS
)
1590 WARN("TABLE_fetch_int should not fail here %u\n", r
);
1597 else if (ivalue
== x
)
1599 if (i
< tv
->num_cols
- 1) continue;
1608 static int find_insert_index( MSITABLEVIEW
*tv
, MSIRECORD
*rec
)
1610 int idx
, c
, low
= 0, high
= tv
->table
->row_count
- 1;
1612 TRACE("%p %p\n", tv
, rec
);
1616 idx
= (low
+ high
) / 2;
1617 c
= compare_record( tv
, idx
, rec
);
1625 TRACE("found %u\n", idx
);
1629 TRACE("found %u\n", high
+ 1);
1633 static UINT
TABLE_insert_row( struct tagMSIVIEW
*view
, MSIRECORD
*rec
, UINT row
, BOOL temporary
)
1635 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1638 TRACE("%p %p %s\n", tv
, rec
, temporary
? "TRUE" : "FALSE" );
1640 /* check that the key is unique - can we find a matching row? */
1641 r
= table_validate_new( tv
, rec
, NULL
);
1642 if( r
!= ERROR_SUCCESS
)
1643 return ERROR_FUNCTION_FAILED
;
1646 row
= find_insert_index( tv
, rec
);
1648 r
= table_create_new_row( view
, &row
, temporary
);
1649 TRACE("insert_row returned %08x\n", r
);
1650 if( r
!= ERROR_SUCCESS
)
1653 /* shift the rows to make room for the new row */
1654 for (i
= tv
->table
->row_count
- 1; i
> row
; i
--)
1656 memmove(&(tv
->table
->data
[i
][0]),
1657 &(tv
->table
->data
[i
- 1][0]), tv
->row_size
);
1658 tv
->table
->data_persistent
[i
] = tv
->table
->data_persistent
[i
- 1];
1661 /* Re-set the persistence flag */
1662 tv
->table
->data_persistent
[row
] = !temporary
;
1663 return TABLE_set_row( view
, row
, rec
, (1<<tv
->num_cols
) - 1 );
1666 static UINT
TABLE_delete_row( struct tagMSIVIEW
*view
, UINT row
)
1668 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1669 UINT r
, num_rows
, num_cols
, i
;
1671 TRACE("%p %d\n", tv
, row
);
1674 return ERROR_INVALID_PARAMETER
;
1676 r
= TABLE_get_dimensions( view
, &num_rows
, &num_cols
);
1677 if ( r
!= ERROR_SUCCESS
)
1680 if ( row
>= num_rows
)
1681 return ERROR_FUNCTION_FAILED
;
1683 num_rows
= tv
->table
->row_count
;
1684 tv
->table
->row_count
--;
1686 /* reset the hash tables */
1687 for (i
= 0; i
< tv
->num_cols
; i
++)
1689 msi_free( tv
->columns
[i
].hash_table
);
1690 tv
->columns
[i
].hash_table
= NULL
;
1693 for (i
= row
+ 1; i
< num_rows
; i
++)
1695 memcpy(tv
->table
->data
[i
- 1], tv
->table
->data
[i
], tv
->row_size
);
1696 tv
->table
->data_persistent
[i
- 1] = tv
->table
->data_persistent
[i
];
1699 msi_free(tv
->table
->data
[num_rows
- 1]);
1701 return ERROR_SUCCESS
;
1704 static UINT
msi_table_update(struct tagMSIVIEW
*view
, MSIRECORD
*rec
, UINT row
)
1706 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1709 /* FIXME: MsiViewFetch should set rec index 0 to some ID that
1710 * sets the fetched record apart from other records
1714 return ERROR_INVALID_PARAMETER
;
1716 r
= msi_table_find_row(tv
, rec
, &new_row
, NULL
);
1717 if (r
!= ERROR_SUCCESS
)
1719 ERR("can't find row to modify\n");
1720 return ERROR_FUNCTION_FAILED
;
1723 /* the row cannot be changed */
1724 if (row
!= new_row
+ 1)
1725 return ERROR_FUNCTION_FAILED
;
1727 return TABLE_set_row(view
, new_row
, rec
, (1 << tv
->num_cols
) - 1);
1730 static UINT
msi_table_assign(struct tagMSIVIEW
*view
, MSIRECORD
*rec
)
1732 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1736 return ERROR_INVALID_PARAMETER
;
1738 r
= msi_table_find_row(tv
, rec
, &row
, NULL
);
1739 if (r
== ERROR_SUCCESS
)
1740 return TABLE_set_row(view
, row
, rec
, (1 << tv
->num_cols
) - 1);
1742 return TABLE_insert_row( view
, rec
, -1, FALSE
);
1745 static UINT
modify_delete_row( struct tagMSIVIEW
*view
, MSIRECORD
*rec
)
1747 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1750 r
= msi_table_find_row(tv
, rec
, &row
, NULL
);
1751 if (r
!= ERROR_SUCCESS
)
1754 return TABLE_delete_row(view
, row
);
1757 static UINT
msi_refresh_record( struct tagMSIVIEW
*view
, MSIRECORD
*rec
, UINT row
)
1762 r
= TABLE_get_row(view
, row
- 1, &curr
);
1763 if (r
!= ERROR_SUCCESS
)
1766 /* Close the original record */
1767 MSI_CloseRecord(&rec
->hdr
);
1769 count
= MSI_RecordGetFieldCount(rec
);
1770 for (i
= 0; i
< count
; i
++)
1771 MSI_RecordCopyField(curr
, i
+ 1, rec
, i
+ 1);
1773 msiobj_release(&curr
->hdr
);
1774 return ERROR_SUCCESS
;
1777 static UINT
TABLE_modify( struct tagMSIVIEW
*view
, MSIMODIFY eModifyMode
,
1778 MSIRECORD
*rec
, UINT row
)
1780 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1781 UINT r
, frow
, column
;
1783 TRACE("%p %d %p\n", view
, eModifyMode
, rec
);
1785 switch (eModifyMode
)
1787 case MSIMODIFY_DELETE
:
1788 r
= modify_delete_row( view
, rec
);
1790 case MSIMODIFY_VALIDATE_NEW
:
1791 r
= table_validate_new( tv
, rec
, &column
);
1792 if (r
!= ERROR_SUCCESS
)
1794 tv
->view
.error
= MSIDBERROR_DUPLICATEKEY
;
1795 tv
->view
.error_column
= tv
->columns
[column
].colname
;
1796 r
= ERROR_INVALID_DATA
;
1800 case MSIMODIFY_INSERT
:
1801 r
= table_validate_new( tv
, rec
, NULL
);
1802 if (r
!= ERROR_SUCCESS
)
1804 r
= TABLE_insert_row( view
, rec
, -1, FALSE
);
1807 case MSIMODIFY_INSERT_TEMPORARY
:
1808 r
= table_validate_new( tv
, rec
, NULL
);
1809 if (r
!= ERROR_SUCCESS
)
1811 r
= TABLE_insert_row( view
, rec
, -1, TRUE
);
1814 case MSIMODIFY_REFRESH
:
1815 r
= msi_refresh_record( view
, rec
, row
);
1818 case MSIMODIFY_UPDATE
:
1819 r
= msi_table_update( view
, rec
, row
);
1822 case MSIMODIFY_ASSIGN
:
1823 r
= msi_table_assign( view
, rec
);
1826 case MSIMODIFY_MERGE
:
1827 /* check row that matches this record */
1828 r
= msi_table_find_row( tv
, rec
, &frow
, &column
);
1829 if (r
!= ERROR_SUCCESS
)
1831 r
= table_validate_new( tv
, rec
, NULL
);
1832 if (r
== ERROR_SUCCESS
)
1833 r
= TABLE_insert_row( view
, rec
, -1, FALSE
);
1837 case MSIMODIFY_REPLACE
:
1838 case MSIMODIFY_VALIDATE
:
1839 case MSIMODIFY_VALIDATE_FIELD
:
1840 case MSIMODIFY_VALIDATE_DELETE
:
1841 FIXME("%p %d %p - mode not implemented\n", view
, eModifyMode
, rec
);
1842 r
= ERROR_CALL_NOT_IMPLEMENTED
;
1846 r
= ERROR_INVALID_DATA
;
1852 static UINT
TABLE_delete( struct tagMSIVIEW
*view
)
1854 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1856 TRACE("%p\n", view
);
1863 return ERROR_SUCCESS
;
1866 static UINT
TABLE_find_matching_rows( struct tagMSIVIEW
*view
, UINT col
,
1867 UINT val
, UINT
*row
, MSIITERHANDLE
*handle
)
1869 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1870 const MSICOLUMNHASHENTRY
*entry
;
1872 TRACE("%p, %d, %u, %p\n", view
, col
, val
, *handle
);
1875 return ERROR_INVALID_PARAMETER
;
1877 if( (col
==0) || (col
> tv
->num_cols
) )
1878 return ERROR_INVALID_PARAMETER
;
1880 if( !tv
->columns
[col
-1].hash_table
)
1883 UINT num_rows
= tv
->table
->row_count
;
1884 MSICOLUMNHASHENTRY
**hash_table
;
1885 MSICOLUMNHASHENTRY
*new_entry
;
1887 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1889 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1890 ERR("%p %p\n", tv
, tv
->columns
);
1891 return ERROR_FUNCTION_FAILED
;
1894 /* allocate contiguous memory for the table and its entries so we
1895 * don't have to do an expensive cleanup */
1896 hash_table
= msi_alloc(MSITABLE_HASH_TABLE_SIZE
* sizeof(MSICOLUMNHASHENTRY
*) +
1897 num_rows
* sizeof(MSICOLUMNHASHENTRY
));
1899 return ERROR_OUTOFMEMORY
;
1901 memset(hash_table
, 0, MSITABLE_HASH_TABLE_SIZE
* sizeof(MSICOLUMNHASHENTRY
*));
1902 tv
->columns
[col
-1].hash_table
= hash_table
;
1904 new_entry
= (MSICOLUMNHASHENTRY
*)(hash_table
+ MSITABLE_HASH_TABLE_SIZE
);
1906 for (i
= 0; i
< num_rows
; i
++, new_entry
++)
1910 if (view
->ops
->fetch_int( view
, i
, col
, &row_value
) != ERROR_SUCCESS
)
1913 new_entry
->next
= NULL
;
1914 new_entry
->value
= row_value
;
1916 if (hash_table
[row_value
% MSITABLE_HASH_TABLE_SIZE
])
1918 MSICOLUMNHASHENTRY
*prev_entry
= hash_table
[row_value
% MSITABLE_HASH_TABLE_SIZE
];
1919 while (prev_entry
->next
)
1920 prev_entry
= prev_entry
->next
;
1921 prev_entry
->next
= new_entry
;
1924 hash_table
[row_value
% MSITABLE_HASH_TABLE_SIZE
] = new_entry
;
1929 entry
= tv
->columns
[col
-1].hash_table
[val
% MSITABLE_HASH_TABLE_SIZE
];
1931 entry
= (*handle
)->next
;
1933 while (entry
&& entry
->value
!= val
)
1934 entry
= entry
->next
;
1938 return ERROR_NO_MORE_ITEMS
;
1942 return ERROR_SUCCESS
;
1945 static UINT
TABLE_add_ref(struct tagMSIVIEW
*view
)
1947 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1950 TRACE("%p %d\n", view
, tv
->table
->ref_count
);
1952 for (i
= 0; i
< tv
->table
->col_count
; i
++)
1954 if (tv
->table
->colinfo
[i
].type
& MSITYPE_TEMPORARY
)
1955 InterlockedIncrement(&tv
->table
->colinfo
[i
].ref_count
);
1958 return InterlockedIncrement(&tv
->table
->ref_count
);
1961 static UINT
TABLE_remove_column(struct tagMSIVIEW
*view
, LPCWSTR table
, UINT number
)
1963 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1964 MSIRECORD
*rec
= NULL
;
1965 MSIVIEW
*columns
= NULL
;
1968 rec
= MSI_CreateRecord(2);
1970 return ERROR_OUTOFMEMORY
;
1972 MSI_RecordSetStringW(rec
, 1, table
);
1973 MSI_RecordSetInteger(rec
, 2, number
);
1975 r
= TABLE_CreateView(tv
->db
, szColumns
, &columns
);
1976 if (r
!= ERROR_SUCCESS
)
1979 r
= msi_table_find_row((MSITABLEVIEW
*)columns
, rec
, &row
, NULL
);
1980 if (r
!= ERROR_SUCCESS
)
1983 r
= TABLE_delete_row(columns
, row
);
1984 if (r
!= ERROR_SUCCESS
)
1987 msi_update_table_columns(tv
->db
, table
);
1990 msiobj_release(&rec
->hdr
);
1991 columns
->ops
->delete(columns
);
1995 static UINT
TABLE_release(struct tagMSIVIEW
*view
)
1997 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1998 INT ref
= tv
->table
->ref_count
;
2001 TRACE("%p %d\n", view
, ref
);
2003 for (i
= 0; i
< tv
->table
->col_count
; i
++)
2005 if (tv
->table
->colinfo
[i
].type
& MSITYPE_TEMPORARY
)
2007 ref
= InterlockedDecrement(&tv
->table
->colinfo
[i
].ref_count
);
2010 r
= TABLE_remove_column(view
, tv
->table
->colinfo
[i
].tablename
,
2011 tv
->table
->colinfo
[i
].number
);
2012 if (r
!= ERROR_SUCCESS
)
2018 ref
= InterlockedDecrement(&tv
->table
->ref_count
);
2021 if (!tv
->table
->row_count
)
2023 list_remove(&tv
->table
->entry
);
2024 free_table(tv
->table
);
2032 static UINT
TABLE_add_column(struct tagMSIVIEW
*view
, LPCWSTR table
, UINT number
,
2033 LPCWSTR column
, UINT type
, BOOL hold
)
2035 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
2040 rec
= MSI_CreateRecord(4);
2042 return ERROR_OUTOFMEMORY
;
2044 MSI_RecordSetStringW(rec
, 1, table
);
2045 MSI_RecordSetInteger(rec
, 2, number
);
2046 MSI_RecordSetStringW(rec
, 3, column
);
2047 MSI_RecordSetInteger(rec
, 4, type
);
2049 r
= TABLE_insert_row(&tv
->view
, rec
, -1, FALSE
);
2050 if (r
!= ERROR_SUCCESS
)
2053 msi_update_table_columns(tv
->db
, table
);
2058 msitable
= find_cached_table(tv
->db
, table
);
2059 for (i
= 0; i
< msitable
->col_count
; i
++)
2061 if (!strcmpW( msitable
->colinfo
[i
].colname
, column
))
2063 InterlockedIncrement(&msitable
->colinfo
[i
].ref_count
);
2069 msiobj_release(&rec
->hdr
);
2073 static UINT
TABLE_drop(struct tagMSIVIEW
*view
)
2075 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
2076 MSIVIEW
*tables
= NULL
;
2077 MSIRECORD
*rec
= NULL
;
2081 TRACE("dropping table %s\n", debugstr_w(tv
->name
));
2083 for (i
= tv
->table
->col_count
- 1; i
>= 0; i
--)
2085 r
= TABLE_remove_column(view
, tv
->table
->colinfo
[i
].tablename
,
2086 tv
->table
->colinfo
[i
].number
);
2087 if (r
!= ERROR_SUCCESS
)
2091 rec
= MSI_CreateRecord(1);
2093 return ERROR_OUTOFMEMORY
;
2095 MSI_RecordSetStringW(rec
, 1, tv
->name
);
2097 r
= TABLE_CreateView(tv
->db
, szTables
, &tables
);
2098 if (r
!= ERROR_SUCCESS
)
2101 r
= msi_table_find_row((MSITABLEVIEW
*)tables
, rec
, &row
, NULL
);
2102 if (r
!= ERROR_SUCCESS
)
2105 r
= TABLE_delete_row(tables
, row
);
2106 if (r
!= ERROR_SUCCESS
)
2109 list_remove(&tv
->table
->entry
);
2110 free_table(tv
->table
);
2113 msiobj_release(&rec
->hdr
);
2114 tables
->ops
->delete(tables
);
2119 static const MSIVIEWOPS table_ops
=
2129 TABLE_get_dimensions
,
2130 TABLE_get_column_info
,
2133 TABLE_find_matching_rows
,
2137 TABLE_remove_column
,
2142 UINT
TABLE_CreateView( MSIDATABASE
*db
, LPCWSTR name
, MSIVIEW
**view
)
2147 TRACE("%p %s %p\n", db
, debugstr_w(name
), view
);
2149 if ( !strcmpW( name
, szStreams
) )
2150 return STREAMS_CreateView( db
, view
);
2151 else if ( !strcmpW( name
, szStorages
) )
2152 return STORAGES_CreateView( db
, view
);
2154 sz
= FIELD_OFFSET( MSITABLEVIEW
, name
[lstrlenW( name
) + 1] );
2155 tv
= msi_alloc_zero( sz
);
2157 return ERROR_FUNCTION_FAILED
;
2159 r
= get_table( db
, name
, &tv
->table
);
2160 if( r
!= ERROR_SUCCESS
)
2163 WARN("table not found\n");
2167 TRACE("table %p found with %d columns\n", tv
->table
, tv
->table
->col_count
);
2169 /* fill the structure */
2170 tv
->view
.ops
= &table_ops
;
2172 tv
->columns
= tv
->table
->colinfo
;
2173 tv
->num_cols
= tv
->table
->col_count
;
2174 tv
->row_size
= msi_table_get_row_size( db
, tv
->table
->colinfo
, tv
->table
->col_count
, LONG_STR_BYTES
);
2176 TRACE("%s one row is %d bytes\n", debugstr_w(name
), tv
->row_size
);
2178 *view
= (MSIVIEW
*) tv
;
2179 lstrcpyW( tv
->name
, name
);
2181 return ERROR_SUCCESS
;
2184 UINT
MSI_CommitTables( MSIDATABASE
*db
)
2186 UINT r
, bytes_per_strref
;
2188 MSITABLE
*table
= NULL
;
2192 r
= msi_save_string_table( db
->strings
, db
->storage
, &bytes_per_strref
);
2193 if( r
!= ERROR_SUCCESS
)
2195 WARN("failed to save string table r=%08x\n",r
);
2199 LIST_FOR_EACH_ENTRY( table
, &db
->tables
, MSITABLE
, entry
)
2201 r
= save_table( db
, table
, bytes_per_strref
);
2202 if( r
!= ERROR_SUCCESS
)
2204 WARN("failed to save table %s (r=%08x)\n",
2205 debugstr_w(table
->name
), r
);
2210 hr
= IStorage_Commit( db
->storage
, 0 );
2213 WARN("failed to commit changes 0x%08x\n", hr
);
2214 r
= ERROR_FUNCTION_FAILED
;
2219 MSICONDITION
MSI_DatabaseIsTablePersistent( MSIDATABASE
*db
, LPCWSTR table
)
2224 TRACE("%p %s\n", db
, debugstr_w(table
));
2227 return MSICONDITION_ERROR
;
2229 r
= get_table( db
, table
, &t
);
2230 if (r
!= ERROR_SUCCESS
)
2231 return MSICONDITION_NONE
;
2233 return t
->persistent
;
2236 static UINT
read_raw_int(const BYTE
*data
, UINT col
, UINT bytes
)
2240 for (i
= 0; i
< bytes
; i
++)
2241 ret
+= (data
[col
+ i
] << i
* 8);
2246 static UINT
msi_record_encoded_stream_name( const MSITABLEVIEW
*tv
, MSIRECORD
*rec
, LPWSTR
*pstname
)
2248 LPWSTR stname
= NULL
, sval
, p
;
2252 TRACE("%p %p\n", tv
, rec
);
2254 len
= lstrlenW( tv
->name
) + 1;
2255 stname
= msi_alloc( len
*sizeof(WCHAR
) );
2258 r
= ERROR_OUTOFMEMORY
;
2262 lstrcpyW( stname
, tv
->name
);
2264 for ( i
= 0; i
< tv
->num_cols
; i
++ )
2266 if ( tv
->columns
[i
].type
& MSITYPE_KEY
)
2268 sval
= msi_dup_record_field( rec
, i
+ 1 );
2271 r
= ERROR_OUTOFMEMORY
;
2275 len
+= lstrlenW( szDot
) + lstrlenW ( sval
);
2276 p
= msi_realloc ( stname
, len
*sizeof(WCHAR
) );
2279 r
= ERROR_OUTOFMEMORY
;
2285 lstrcatW( stname
, szDot
);
2286 lstrcatW( stname
, sval
);
2294 *pstname
= encode_streamname( FALSE
, stname
);
2297 return ERROR_SUCCESS
;
2300 msi_free ( stname
);
2305 static MSIRECORD
*msi_get_transform_record( const MSITABLEVIEW
*tv
, const string_table
*st
,
2307 const BYTE
*rawdata
, UINT bytes_per_strref
)
2309 UINT i
, val
, ofs
= 0;
2311 MSICOLUMNINFO
*columns
= tv
->columns
;
2314 mask
= rawdata
[0] | (rawdata
[1] << 8);
2317 rec
= MSI_CreateRecord( tv
->num_cols
);
2322 for( i
=0; i
<tv
->num_cols
; i
++ )
2324 if ( (mask
&1) && (i
>=(mask
>>8)) )
2326 /* all keys must be present */
2327 if ( (~mask
&1) && (~columns
[i
].type
& MSITYPE_KEY
) && ((1<<i
) & ~mask
) )
2330 if( MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
2333 IStream
*stm
= NULL
;
2336 ofs
+= bytes_per_column( tv
->db
, &columns
[i
], bytes_per_strref
);
2338 r
= msi_record_encoded_stream_name( tv
, rec
, &encname
);
2339 if ( r
!= ERROR_SUCCESS
)
2342 r
= IStorage_OpenStream( stg
, encname
, NULL
,
2343 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, &stm
);
2344 if ( r
!= ERROR_SUCCESS
)
2346 msi_free( encname
);
2350 MSI_RecordSetStream( rec
, i
+1, stm
);
2351 TRACE(" field %d [%s]\n", i
+1, debugstr_w(encname
));
2352 msi_free( encname
);
2354 else if( columns
[i
].type
& MSITYPE_STRING
)
2359 val
= read_raw_int(rawdata
, ofs
, bytes_per_strref
);
2360 sval
= msi_string_lookup( st
, val
, &len
);
2361 msi_record_set_string( rec
, i
+1, sval
, len
);
2362 TRACE(" field %d [%s]\n", i
+1, debugstr_wn(sval
, len
));
2363 ofs
+= bytes_per_strref
;
2367 UINT n
= bytes_per_column( tv
->db
, &columns
[i
], bytes_per_strref
);
2371 val
= read_raw_int(rawdata
, ofs
, n
);
2373 MSI_RecordSetInteger( rec
, i
+1, val
-0x8000 );
2374 TRACE(" field %d [0x%04x]\n", i
+1, val
);
2377 val
= read_raw_int(rawdata
, ofs
, n
);
2379 MSI_RecordSetInteger( rec
, i
+1, val
^0x80000000 );
2380 TRACE(" field %d [0x%08x]\n", i
+1, val
);
2383 ERR("oops - unknown column width %d\n", n
);
2392 static void dump_record( MSIRECORD
*rec
)
2396 n
= MSI_RecordGetFieldCount( rec
);
2397 for( i
=1; i
<=n
; i
++ )
2402 if( MSI_RecordIsNull( rec
, i
) )
2403 TRACE("row -> []\n");
2404 else if( (sval
= msi_record_get_string( rec
, i
, &len
)) )
2405 TRACE("row -> [%s]\n", debugstr_wn(sval
, len
));
2407 TRACE("row -> [0x%08x]\n", MSI_RecordGetInteger( rec
, i
) );
2411 static void dump_table( const string_table
*st
, const USHORT
*rawdata
, UINT rawsize
)
2414 for (i
= 0; i
< rawsize
/ 2; i
++)
2417 const WCHAR
*sval
= msi_string_lookup( st
, rawdata
[i
], &len
);
2418 MESSAGE(" %04x %s\n", rawdata
[i
], debugstr_wn(sval
, len
) );
2422 static UINT
* msi_record_to_row( const MSITABLEVIEW
*tv
, MSIRECORD
*rec
)
2426 data
= msi_alloc( tv
->num_cols
*sizeof (UINT
) );
2427 for( i
=0; i
<tv
->num_cols
; i
++ )
2431 if ( ~tv
->columns
[i
].type
& MSITYPE_KEY
)
2434 /* turn the transform column value into a row value */
2435 if ( ( tv
->columns
[i
].type
& MSITYPE_STRING
) &&
2436 ! MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
2439 const WCHAR
*str
= msi_record_get_string( rec
, i
+1, &len
);
2442 r
= msi_string2id( tv
->db
->strings
, str
, len
, &data
[i
] );
2444 /* if there's no matching string in the string table,
2445 these keys can't match any record, so fail now. */
2446 if (r
!= ERROR_SUCCESS
)
2456 data
[i
] = MSI_RecordGetInteger( rec
, i
+1 );
2458 if (data
[i
] == MSI_NULL_INTEGER
)
2460 else if ((tv
->columns
[i
].type
&0xff) == 2)
2463 data
[i
] += 0x80000000;
2469 static UINT
msi_row_matches( MSITABLEVIEW
*tv
, UINT row
, const UINT
*data
, UINT
*column
)
2471 UINT i
, r
, x
, ret
= ERROR_FUNCTION_FAILED
;
2473 for( i
=0; i
<tv
->num_cols
; i
++ )
2475 if ( ~tv
->columns
[i
].type
& MSITYPE_KEY
)
2478 /* turn the transform column value into a row value */
2479 r
= TABLE_fetch_int( &tv
->view
, row
, i
+1, &x
);
2480 if ( r
!= ERROR_SUCCESS
)
2482 ERR("TABLE_fetch_int shouldn't fail here\n");
2486 /* if this key matches, move to the next column */
2489 ret
= ERROR_FUNCTION_FAILED
;
2492 if (column
) *column
= i
;
2493 ret
= ERROR_SUCCESS
;
2498 static UINT
msi_table_find_row( MSITABLEVIEW
*tv
, MSIRECORD
*rec
, UINT
*row
, UINT
*column
)
2500 UINT i
, r
= ERROR_FUNCTION_FAILED
, *data
;
2502 data
= msi_record_to_row( tv
, rec
);
2505 for( i
= 0; i
< tv
->table
->row_count
; i
++ )
2507 r
= msi_row_matches( tv
, i
, data
, column
);
2508 if( r
== ERROR_SUCCESS
)
2524 static UINT
msi_table_load_transform( MSIDATABASE
*db
, IStorage
*stg
,
2525 string_table
*st
, TRANSFORMDATA
*transform
,
2526 UINT bytes_per_strref
)
2528 BYTE
*rawdata
= NULL
;
2529 MSITABLEVIEW
*tv
= NULL
;
2530 UINT r
, n
, sz
, i
, mask
, num_cols
, colcol
= 0, rawsize
= 0;
2531 MSIRECORD
*rec
= NULL
;
2536 return ERROR_SUCCESS
;
2538 name
= transform
->name
;
2541 TRACE("%p %p %p %s\n", db
, stg
, st
, debugstr_w(name
) );
2543 /* read the transform data */
2544 read_stream_data( stg
, name
, TRUE
, &rawdata
, &rawsize
);
2547 TRACE("table %s empty\n", debugstr_w(name
) );
2548 return ERROR_INVALID_TABLE
;
2551 /* create a table view */
2552 r
= TABLE_CreateView( db
, name
, (MSIVIEW
**) &tv
);
2553 if( r
!= ERROR_SUCCESS
)
2556 r
= tv
->view
.ops
->execute( &tv
->view
, NULL
);
2557 if( r
!= ERROR_SUCCESS
)
2560 TRACE("name = %s columns = %u row_size = %u raw size = %u\n",
2561 debugstr_w(name
), tv
->num_cols
, tv
->row_size
, rawsize
);
2563 /* interpret the data */
2564 for (n
= 0; n
< rawsize
;)
2566 mask
= rawdata
[n
] | (rawdata
[n
+ 1] << 8);
2570 * if the low bit is set, columns are continuous and
2571 * the number of columns is specified in the high byte
2574 num_cols
= mask
>> 8;
2575 for (i
= 0; i
< num_cols
; i
++)
2577 if( (tv
->columns
[i
].type
& MSITYPE_STRING
) &&
2578 ! MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
2579 sz
+= bytes_per_strref
;
2581 sz
+= bytes_per_column( tv
->db
, &tv
->columns
[i
], bytes_per_strref
);
2587 * If the low bit is not set, mask is a bitmask.
2588 * Excepting for key fields, which are always present,
2589 * each bit indicates that a field is present in the transform record.
2591 * mask == 0 is a special case ... only the keys will be present
2592 * and it means that this row should be deleted.
2595 num_cols
= tv
->num_cols
;
2596 for (i
= 0; i
< num_cols
; i
++)
2598 if ((tv
->columns
[i
].type
& MSITYPE_KEY
) || ((1 << i
) & mask
))
2600 if ((tv
->columns
[i
].type
& MSITYPE_STRING
) &&
2601 !MSITYPE_IS_BINARY(tv
->columns
[i
].type
))
2602 sz
+= bytes_per_strref
;
2604 sz
+= bytes_per_column( tv
->db
, &tv
->columns
[i
], bytes_per_strref
);
2609 /* check we didn't run of the end of the table */
2610 if (n
+ sz
> rawsize
)
2613 dump_table( st
, (USHORT
*)rawdata
, rawsize
);
2617 rec
= msi_get_transform_record( tv
, st
, stg
, &rawdata
[n
], bytes_per_strref
);
2622 UINT number
= MSI_NULL_INTEGER
;
2625 if (!strcmpW( name
, szColumns
))
2627 MSI_RecordGetStringW( rec
, 1, table
, &sz
);
2628 number
= MSI_RecordGetInteger( rec
, 2 );
2631 * Native msi seems writes nul into the Number (2nd) column of
2632 * the _Columns table, only when the columns are from a new table
2634 if ( number
== MSI_NULL_INTEGER
)
2636 /* reset the column number on a new table */
2637 if (strcmpW( coltable
, table
))
2640 lstrcpyW( coltable
, table
);
2643 /* fix nul column numbers */
2644 MSI_RecordSetInteger( rec
, 2, ++colcol
);
2648 if (TRACE_ON(msidb
)) dump_record( rec
);
2650 r
= msi_table_find_row( tv
, rec
, &row
, NULL
);
2651 if (r
== ERROR_SUCCESS
)
2655 TRACE("deleting row [%d]:\n", row
);
2656 r
= TABLE_delete_row( &tv
->view
, row
);
2657 if (r
!= ERROR_SUCCESS
)
2658 WARN("failed to delete row %u\n", r
);
2662 TRACE("modifying full row [%d]:\n", row
);
2663 r
= TABLE_set_row( &tv
->view
, row
, rec
, (1 << tv
->num_cols
) - 1 );
2664 if (r
!= ERROR_SUCCESS
)
2665 WARN("failed to modify row %u\n", r
);
2669 TRACE("modifying masked row [%d]:\n", row
);
2670 r
= TABLE_set_row( &tv
->view
, row
, rec
, mask
);
2671 if (r
!= ERROR_SUCCESS
)
2672 WARN("failed to modify row %u\n", r
);
2677 TRACE("inserting row\n");
2678 r
= TABLE_insert_row( &tv
->view
, rec
, -1, FALSE
);
2679 if (r
!= ERROR_SUCCESS
)
2680 WARN("failed to insert row %u\n", r
);
2683 if (number
!= MSI_NULL_INTEGER
&& !strcmpW( name
, szColumns
))
2684 msi_update_table_columns( db
, table
);
2686 msiobj_release( &rec
->hdr
);
2693 /* no need to free the table, it's associated with the database */
2694 msi_free( rawdata
);
2696 tv
->view
.ops
->delete( &tv
->view
);
2698 return ERROR_SUCCESS
;
2702 * msi_table_apply_transform
2704 * Enumerate the table transforms in a transform storage and apply each one.
2706 UINT
msi_table_apply_transform( MSIDATABASE
*db
, IStorage
*stg
)
2708 struct list transforms
;
2709 IEnumSTATSTG
*stgenum
= NULL
;
2710 TRANSFORMDATA
*transform
;
2711 TRANSFORMDATA
*tables
= NULL
, *columns
= NULL
;
2714 string_table
*strings
;
2715 UINT ret
= ERROR_FUNCTION_FAILED
;
2716 UINT bytes_per_strref
;
2718 TRACE("%p %p\n", db
, stg
);
2720 strings
= msi_load_string_table( stg
, &bytes_per_strref
);
2724 r
= IStorage_EnumElements( stg
, 0, NULL
, 0, &stgenum
);
2728 list_init(&transforms
);
2732 MSITABLEVIEW
*tv
= NULL
;
2736 r
= IEnumSTATSTG_Next( stgenum
, 1, &stat
, &count
);
2737 if ( FAILED( r
) || !count
)
2740 decode_streamname( stat
.pwcsName
, name
);
2741 CoTaskMemFree( stat
.pwcsName
);
2742 if ( name
[0] != 0x4840 )
2745 if ( !strcmpW( name
+1, szStringPool
) ||
2746 !strcmpW( name
+1, szStringData
) )
2749 transform
= msi_alloc_zero( sizeof(TRANSFORMDATA
) );
2753 list_add_tail( &transforms
, &transform
->entry
);
2755 transform
->name
= strdupW( name
+ 1 );
2757 if ( !strcmpW( transform
->name
, szTables
) )
2759 else if (!strcmpW( transform
->name
, szColumns
) )
2760 columns
= transform
;
2762 TRACE("transform contains stream %s\n", debugstr_w(name
));
2764 /* load the table */
2765 r
= TABLE_CreateView( db
, transform
->name
, (MSIVIEW
**) &tv
);
2766 if( r
!= ERROR_SUCCESS
)
2769 r
= tv
->view
.ops
->execute( &tv
->view
, NULL
);
2770 if( r
!= ERROR_SUCCESS
)
2772 tv
->view
.ops
->delete( &tv
->view
);
2776 tv
->view
.ops
->delete( &tv
->view
);
2780 * Apply _Tables and _Columns transforms first so that
2781 * the table metadata is correct, and empty tables exist.
2783 ret
= msi_table_load_transform( db
, stg
, strings
, tables
, bytes_per_strref
);
2784 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_INVALID_TABLE
)
2787 ret
= msi_table_load_transform( db
, stg
, strings
, columns
, bytes_per_strref
);
2788 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_INVALID_TABLE
)
2791 ret
= ERROR_SUCCESS
;
2793 while ( !list_empty( &transforms
) )
2795 transform
= LIST_ENTRY( list_head( &transforms
), TRANSFORMDATA
, entry
);
2797 if ( strcmpW( transform
->name
, szColumns
) &&
2798 strcmpW( transform
->name
, szTables
) &&
2799 ret
== ERROR_SUCCESS
)
2801 ret
= msi_table_load_transform( db
, stg
, strings
, transform
, bytes_per_strref
);
2804 list_remove( &transform
->entry
);
2805 msi_free( transform
->name
);
2806 msi_free( transform
);
2809 if ( ret
== ERROR_SUCCESS
)
2810 append_storage_to_db( db
, stg
);
2814 IEnumSTATSTG_Release( stgenum
);
2816 msi_destroy_stringtable( strings
);