2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2002-2005 Mike McCormack for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(msidb
);
41 #define MSITABLE_HASH_TABLE_SIZE 37
43 struct column_hash_entry
45 struct column_hash_entry
*next
;
57 struct column_hash_entry
**hash_table
;
63 BOOL
*data_persistent
;
66 struct column_info
*colinfo
;
68 MSICONDITION persistent
;
73 /* information for default tables */
74 static const struct column_info _Columns_cols
[4] =
76 { L
"_Columns", 1, L
"Table", MSITYPE_VALID
| MSITYPE_STRING
| MSITYPE_KEY
| 64, 0, NULL
},
77 { L
"_Columns", 2, L
"Number", MSITYPE_VALID
| MSITYPE_KEY
| 2, 2, NULL
},
78 { L
"_Columns", 3, L
"Name", MSITYPE_VALID
| MSITYPE_STRING
| 64, 4, NULL
},
79 { L
"_Columns", 4, L
"Type", MSITYPE_VALID
| 2, 6, NULL
},
82 static const struct column_info _Tables_cols
[1] =
84 { L
"_Tables", 1, L
"Name", MSITYPE_VALID
| MSITYPE_STRING
| MSITYPE_KEY
| 64, 0, NULL
},
87 #define MAX_STREAM_NAME 0x1f
89 static inline UINT
bytes_per_column( MSIDATABASE
*db
, const struct column_info
*col
, UINT bytes_per_strref
)
91 if( MSITYPE_IS_BINARY(col
->type
) )
94 if( col
->type
& MSITYPE_STRING
)
95 return bytes_per_strref
;
97 if( (col
->type
& 0xff) <= 2)
100 if( (col
->type
& 0xff) != 4 )
101 ERR("Invalid column size %u\n", col
->type
& 0xff);
106 static int utf2mime(int x
)
108 if( (x
>='0') && (x
<='9') )
110 if( (x
>='A') && (x
<='Z') )
112 if( (x
>='a') && (x
<='z') )
121 LPWSTR
encode_streamname(BOOL bTable
, LPCWSTR in
)
123 DWORD count
= MAX_STREAM_NAME
;
128 count
= lstrlenW( in
)+2;
129 if (!(out
= malloc( count
* sizeof(WCHAR
) ))) return NULL
;
145 if( ( ch
< 0x80 ) && ( utf2mime(ch
) >= 0 ) )
147 ch
= utf2mime(ch
) + 0x4800;
149 if( next
&& (next
<0x80) )
151 next
= utf2mime(next
);
162 ERR("Failed to encode stream name (%s)\n",debugstr_w(in
));
167 static int mime2utf(int x
)
174 return x
- 10 - 26 + 'a';
175 if( x
== (10+26+26) )
180 BOOL
decode_streamname(LPCWSTR in
, LPWSTR out
)
185 while ( (ch
= *in
++) )
187 if( (ch
>= 0x3800 ) && (ch
< 0x4840 ) )
190 ch
= mime2utf(ch
-0x4800);
194 *out
++ = mime2utf(ch
&0x3f);
196 ch
= mime2utf((ch
>>6)&0x3f);
206 void enum_stream_names( IStorage
*stg
)
208 IEnumSTATSTG
*stgenum
= NULL
;
214 r
= IStorage_EnumElements( stg
, 0, NULL
, 0, &stgenum
);
222 r
= IEnumSTATSTG_Next( stgenum
, 1, &stat
, &count
);
223 if( FAILED( r
) || !count
)
225 decode_streamname( stat
.pwcsName
, name
);
226 TRACE( "stream %2lu -> %s %s\n", n
, debugstr_w(stat
.pwcsName
), debugstr_w(name
) );
227 CoTaskMemFree( stat
.pwcsName
);
231 IEnumSTATSTG_Release( stgenum
);
234 UINT
read_stream_data( IStorage
*stg
, LPCWSTR stname
, BOOL table
,
235 BYTE
**pdata
, UINT
*psz
)
238 UINT ret
= ERROR_FUNCTION_FAILED
;
245 encname
= encode_streamname(table
, stname
);
247 TRACE("%s -> %s\n",debugstr_w(stname
),debugstr_w(encname
));
249 r
= IStorage_OpenStream(stg
, encname
, NULL
,
250 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, &stm
);
254 WARN( "open stream failed r = %#lx - empty table?\n", r
);
258 r
= IStream_Stat(stm
, &stat
, STATFLAG_NONAME
);
261 WARN( "open stream failed r = %#lx!\n", r
);
265 if( stat
.cbSize
.QuadPart
>> 32 )
271 sz
= stat
.cbSize
.QuadPart
;
275 WARN( "couldn't allocate memory r = %#lx!\n", r
);
276 ret
= ERROR_NOT_ENOUGH_MEMORY
;
280 r
= IStream_Read(stm
, data
, sz
, &count
);
281 if( FAILED( r
) || ( count
!= sz
) )
284 WARN("read stream failed r = %#lx!\n", r
);
293 IStream_Release( stm
);
298 UINT
write_stream_data( IStorage
*stg
, LPCWSTR stname
,
299 LPCVOID data
, UINT sz
, BOOL bTable
)
302 UINT ret
= ERROR_FUNCTION_FAILED
;
309 encname
= encode_streamname(bTable
, stname
);
310 r
= IStorage_OpenStream( stg
, encname
, NULL
,
311 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, &stm
);
314 r
= IStorage_CreateStream( stg
, encname
,
315 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, 0, &stm
);
320 WARN( "open stream failed r = %#lx\n", r
);
325 r
= IStream_SetSize( stm
, size
);
328 WARN("Failed to SetSize\n");
333 r
= IStream_Seek( stm
, pos
, STREAM_SEEK_SET
, NULL
);
336 WARN("Failed to Seek\n");
342 r
= IStream_Write(stm
, data
, sz
, &count
);
343 if( FAILED( r
) || ( count
!= sz
) )
345 WARN("Failed to Write\n");
353 IStream_Release( stm
);
358 static void free_colinfo( struct column_info
*colinfo
, UINT count
)
361 for (i
= 0; i
< count
; i
++) free( colinfo
[i
].hash_table
);
364 static void free_table( MSITABLE
*table
)
367 for( i
=0; i
<table
->row_count
; i
++ )
368 free( table
->data
[i
] );
370 free( table
->data_persistent
);
371 free_colinfo( table
->colinfo
, table
->col_count
);
372 free( table
->colinfo
);
376 static UINT
table_get_row_size( MSIDATABASE
*db
, const struct column_info
*cols
, UINT count
, UINT bytes_per_strref
)
378 const struct column_info
*last_col
;
383 if (bytes_per_strref
!= LONG_STR_BYTES
)
386 for (i
= 0; i
< count
; i
++) size
+= bytes_per_column( db
, &cols
[i
], bytes_per_strref
);
389 last_col
= &cols
[count
- 1];
390 return last_col
->offset
+ bytes_per_column( db
, last_col
, bytes_per_strref
);
393 /* add this table to the list of cached tables in the database */
394 static UINT
read_table_from_storage( MSIDATABASE
*db
, MSITABLE
*t
, IStorage
*stg
)
396 BYTE
*rawdata
= NULL
;
397 UINT rawsize
= 0, i
, j
, row_size
, row_size_mem
;
399 TRACE("%s\n",debugstr_w(t
->name
));
401 row_size
= table_get_row_size( db
, t
->colinfo
, t
->col_count
, db
->bytes_per_strref
);
402 row_size_mem
= table_get_row_size( db
, t
->colinfo
, t
->col_count
, LONG_STR_BYTES
);
404 /* if we can't read the table, just assume that it's empty */
405 read_stream_data( stg
, t
->name
, TRUE
, &rawdata
, &rawsize
);
407 return ERROR_SUCCESS
;
409 TRACE("Read %d bytes\n", rawsize
);
411 if( rawsize
% row_size
)
413 WARN("Table size is invalid %d/%d\n", rawsize
, row_size
);
417 if ((t
->row_count
= rawsize
/ row_size
))
419 if (!(t
->data
= calloc( t
->row_count
, sizeof(USHORT
*) ))) goto err
;
420 if (!(t
->data_persistent
= calloc( t
->row_count
, sizeof(BOOL
) ))) goto err
;
423 /* transpose all the data */
424 TRACE("Transposing data from %d rows\n", t
->row_count
);
425 for (i
= 0; i
< t
->row_count
; i
++)
427 UINT ofs
= 0, ofs_mem
= 0;
429 t
->data
[i
] = malloc( row_size_mem
);
432 t
->data_persistent
[i
] = TRUE
;
434 for (j
= 0; j
< t
->col_count
; j
++)
436 UINT m
= bytes_per_column( db
, &t
->colinfo
[j
], LONG_STR_BYTES
);
437 UINT n
= bytes_per_column( db
, &t
->colinfo
[j
], db
->bytes_per_strref
);
440 if ( n
!= 2 && n
!= 3 && n
!= 4 )
442 ERR("oops - unknown column width %d\n", n
);
445 if (t
->colinfo
[j
].type
& MSITYPE_STRING
&& n
< m
)
447 for (k
= 0; k
< m
; k
++)
450 t
->data
[i
][ofs_mem
+ k
] = rawdata
[ofs
* t
->row_count
+ i
* n
+ k
];
452 t
->data
[i
][ofs_mem
+ k
] = 0;
457 for (k
= 0; k
< n
; k
++)
458 t
->data
[i
][ofs_mem
+ k
] = rawdata
[ofs
* t
->row_count
+ i
* n
+ k
];
466 return ERROR_SUCCESS
;
469 return ERROR_FUNCTION_FAILED
;
472 void free_cached_tables( MSIDATABASE
*db
)
474 while( !list_empty( &db
->tables
) )
476 MSITABLE
*t
= LIST_ENTRY( list_head( &db
->tables
), MSITABLE
, entry
);
478 list_remove( &t
->entry
);
483 static MSITABLE
*find_cached_table( MSIDATABASE
*db
, LPCWSTR name
)
487 LIST_FOR_EACH_ENTRY( t
, &db
->tables
, MSITABLE
, entry
)
488 if( !wcscmp( name
, t
->name
) )
494 static void table_calc_column_offsets( MSIDATABASE
*db
, struct column_info
*colinfo
, DWORD count
)
498 for (i
= 0; colinfo
&& i
< count
; i
++)
500 assert( i
+ 1 == colinfo
[i
].number
);
501 if (i
) colinfo
[i
].offset
= colinfo
[i
- 1].offset
+
502 bytes_per_column( db
, &colinfo
[i
- 1], LONG_STR_BYTES
);
503 else colinfo
[i
].offset
= 0;
505 TRACE("column %d is [%s] with type %08x ofs %d\n",
506 colinfo
[i
].number
, debugstr_w(colinfo
[i
].colname
),
507 colinfo
[i
].type
, colinfo
[i
].offset
);
511 static UINT
get_defaulttablecolumns( MSIDATABASE
*db
, const WCHAR
*name
, struct column_info
*colinfo
, UINT
*sz
)
513 const struct column_info
*p
;
516 TRACE("%s\n", debugstr_w(name
));
518 if (!wcscmp( name
, L
"_Tables" ))
523 else if (!wcscmp( name
, L
"_Columns" ))
528 else return ERROR_FUNCTION_FAILED
;
530 for (i
= 0; i
< n
; i
++)
532 if (colinfo
&& i
< *sz
) colinfo
[i
] = p
[i
];
533 if (colinfo
&& i
>= *sz
) break;
535 table_calc_column_offsets( db
, colinfo
, n
);
537 return ERROR_SUCCESS
;
540 static UINT
get_tablecolumns( MSIDATABASE
*, const WCHAR
*, struct column_info
*, UINT
* );
542 static UINT
table_get_column_info( MSIDATABASE
*db
, const WCHAR
*name
, struct column_info
**pcols
, UINT
*pcount
)
544 UINT r
, column_count
= 0;
545 struct column_info
*columns
;
547 /* get the number of columns in this table */
549 r
= get_tablecolumns( db
, name
, NULL
, &column_count
);
550 if (r
!= ERROR_SUCCESS
)
553 *pcount
= column_count
;
555 /* if there are no columns, there's no table */
557 return ERROR_INVALID_PARAMETER
;
559 TRACE("table %s found\n", debugstr_w(name
));
561 columns
= malloc( column_count
* sizeof(*columns
) );
563 return ERROR_FUNCTION_FAILED
;
565 r
= get_tablecolumns( db
, name
, columns
, &column_count
);
566 if (r
!= ERROR_SUCCESS
)
569 return ERROR_FUNCTION_FAILED
;
575 static UINT
get_table( MSIDATABASE
*db
, LPCWSTR name
, MSITABLE
**table_ret
)
580 /* first, see if the table is cached */
581 table
= find_cached_table( db
, name
);
585 return ERROR_SUCCESS
;
588 /* nonexistent tables should be interpreted as empty tables */
589 table
= malloc( sizeof(MSITABLE
) + wcslen( name
) * sizeof(WCHAR
) );
591 return ERROR_FUNCTION_FAILED
;
593 table
->row_count
= 0;
595 table
->data_persistent
= NULL
;
596 table
->colinfo
= NULL
;
597 table
->col_count
= 0;
598 table
->persistent
= MSICONDITION_TRUE
;
599 lstrcpyW( table
->name
, name
);
601 if (!wcscmp( name
, L
"_Tables" ) || !wcscmp( name
, L
"_Columns" ))
602 table
->persistent
= MSICONDITION_NONE
;
604 r
= table_get_column_info( db
, name
, &table
->colinfo
, &table
->col_count
);
605 if (r
!= ERROR_SUCCESS
)
610 r
= read_table_from_storage( db
, table
, db
->storage
);
611 if (r
!= ERROR_SUCCESS
)
616 list_add_head( &db
->tables
, &table
->entry
);
618 return ERROR_SUCCESS
;
621 static UINT
read_table_int( BYTE
*const *data
, UINT row
, UINT col
, UINT bytes
)
625 for (i
= 0; i
< bytes
; i
++)
626 ret
+= data
[row
][col
+ i
] << i
* 8;
631 static UINT
get_tablecolumns( MSIDATABASE
*db
, const WCHAR
*szTableName
, struct column_info
*colinfo
, UINT
*sz
)
633 UINT r
, i
, n
= 0, table_id
, count
, maxcount
= *sz
;
634 MSITABLE
*table
= NULL
;
636 TRACE("%s\n", debugstr_w(szTableName
));
638 /* first check if there is a default table with that name */
639 r
= get_defaulttablecolumns( db
, szTableName
, colinfo
, sz
);
640 if (r
== ERROR_SUCCESS
&& *sz
)
643 r
= get_table( db
, L
"_Columns", &table
);
644 if (r
!= ERROR_SUCCESS
)
646 ERR("couldn't load _Columns table\n");
647 return ERROR_FUNCTION_FAILED
;
650 /* convert table and column names to IDs from the string table */
651 r
= msi_string2id( db
->strings
, szTableName
, -1, &table_id
);
652 if (r
!= ERROR_SUCCESS
)
654 WARN("Couldn't find id for %s\n", debugstr_w(szTableName
));
657 TRACE("Table id is %d, row count is %d\n", table_id
, table
->row_count
);
659 /* Note: _Columns table doesn't have non-persistent data */
661 /* if maxcount is non-zero, assume it's exactly right for this table */
662 if (colinfo
) memset( colinfo
, 0, maxcount
* sizeof(*colinfo
) );
663 count
= table
->row_count
;
664 for (i
= 0; i
< count
; i
++)
666 if (read_table_int( table
->data
, i
, 0, LONG_STR_BYTES
) != table_id
) continue;
669 UINT id
= read_table_int( table
->data
, i
, table
->colinfo
[2].offset
, LONG_STR_BYTES
);
670 UINT col
= read_table_int( table
->data
, i
, table
->colinfo
[1].offset
, sizeof(USHORT
) ) - (1 << 15);
672 /* check the column number is in range */
673 if (col
< 1 || col
> maxcount
)
675 ERR("column %d out of range (maxcount: %d)\n", col
, maxcount
);
678 /* check if this column was already set */
679 if (colinfo
[col
- 1].number
)
681 ERR("duplicate column %d\n", col
);
684 colinfo
[col
- 1].tablename
= msi_string_lookup( db
->strings
, table_id
, NULL
);
685 colinfo
[col
- 1].number
= col
;
686 colinfo
[col
- 1].colname
= msi_string_lookup( db
->strings
, id
, NULL
);
687 colinfo
[col
- 1].type
= read_table_int( table
->data
, i
, table
->colinfo
[3].offset
,
688 sizeof(USHORT
) ) - (1 << 15);
689 colinfo
[col
- 1].offset
= 0;
690 colinfo
[col
- 1].hash_table
= NULL
;
694 TRACE("%s has %d columns\n", debugstr_w(szTableName
), n
);
696 if (colinfo
&& n
!= maxcount
)
698 ERR("missing column in table %s\n", debugstr_w(szTableName
));
699 free_colinfo( colinfo
, maxcount
);
700 return ERROR_FUNCTION_FAILED
;
702 table_calc_column_offsets( db
, colinfo
, n
);
704 return ERROR_SUCCESS
;
707 UINT
msi_create_table( MSIDATABASE
*db
, LPCWSTR name
, column_info
*col_info
,
708 MSICONDITION persistent
, BOOL hold
)
712 MSIRECORD
*rec
= NULL
;
717 /* only add tables that don't exist already */
718 if( TABLE_Exists(db
, name
) )
720 WARN("table %s exists\n", debugstr_w(name
));
721 return ERROR_BAD_QUERY_SYNTAX
;
724 table
= malloc( sizeof(MSITABLE
) + wcslen(name
) * sizeof(WCHAR
) );
726 return ERROR_FUNCTION_FAILED
;
728 table
->ref_count
= 0;
729 table
->row_count
= 0;
731 table
->data_persistent
= NULL
;
732 table
->colinfo
= NULL
;
733 table
->col_count
= 0;
734 table
->persistent
= persistent
;
735 lstrcpyW( table
->name
, name
);
740 for( col
= col_info
; col
; col
= col
->next
)
743 table
->colinfo
= malloc( table
->col_count
* sizeof(*table
->colinfo
) );
747 return ERROR_FUNCTION_FAILED
;
750 for( i
= 0, col
= col_info
; col
; i
++, col
= col
->next
)
752 UINT table_id
= msi_add_string( db
->strings
, col
->table
, -1, persistent
);
753 UINT col_id
= msi_add_string( db
->strings
, col
->column
, -1, persistent
);
755 table
->colinfo
[ i
].tablename
= msi_string_lookup( db
->strings
, table_id
, NULL
);
756 table
->colinfo
[ i
].number
= i
+ 1;
757 table
->colinfo
[ i
].colname
= msi_string_lookup( db
->strings
, col_id
, NULL
);
758 table
->colinfo
[ i
].type
= col
->type
;
759 table
->colinfo
[ i
].offset
= 0;
760 table
->colinfo
[ i
].hash_table
= NULL
;
762 table_calc_column_offsets( db
, table
->colinfo
, table
->col_count
);
764 r
= TABLE_CreateView( db
, L
"_Tables", &tv
);
765 TRACE("CreateView returned %x\n", r
);
772 r
= tv
->ops
->execute( tv
, 0 );
773 TRACE("tv execute returned %x\n", r
);
777 rec
= MSI_CreateRecord( 1 );
781 r
= MSI_RecordSetStringW( rec
, 1, name
);
785 r
= tv
->ops
->insert_row( tv
, rec
, -1, persistent
== MSICONDITION_FALSE
);
786 TRACE("insert_row returned %x\n", r
);
790 tv
->ops
->delete( tv
);
793 msiobj_release( &rec
->hdr
);
796 if( persistent
!= MSICONDITION_FALSE
)
798 /* add each column to the _Columns table */
799 r
= TABLE_CreateView( db
, L
"_Columns", &tv
);
803 r
= tv
->ops
->execute( tv
, 0 );
804 TRACE("tv execute returned %x\n", r
);
808 rec
= MSI_CreateRecord( 4 );
812 r
= MSI_RecordSetStringW( rec
, 1, name
);
817 * need to set the table, column number, col name and type
818 * for each column we enter in the table
821 for( col
= col_info
; col
; col
= col
->next
)
823 r
= MSI_RecordSetInteger( rec
, 2, nField
);
827 r
= MSI_RecordSetStringW( rec
, 3, col
->column
);
831 r
= MSI_RecordSetInteger( rec
, 4, col
->type
);
835 r
= tv
->ops
->insert_row( tv
, rec
, -1, FALSE
);
847 msiobj_release( &rec
->hdr
);
848 /* FIXME: remove values from the string table on error */
850 tv
->ops
->delete( tv
);
852 if (r
== ERROR_SUCCESS
)
853 list_add_head( &db
->tables
, &table
->entry
);
860 static UINT
save_table( MSIDATABASE
*db
, const MSITABLE
*t
, UINT bytes_per_strref
)
862 BYTE
*rawdata
= NULL
;
863 UINT rawsize
, i
, j
, row_size
, row_count
;
864 UINT r
= ERROR_FUNCTION_FAILED
;
866 /* Nothing to do for non-persistent tables */
867 if( t
->persistent
== MSICONDITION_FALSE
)
868 return ERROR_SUCCESS
;
870 TRACE("Saving %s\n", debugstr_w( t
->name
) );
872 row_size
= table_get_row_size( db
, t
->colinfo
, t
->col_count
, bytes_per_strref
);
873 row_count
= t
->row_count
;
874 for (i
= 0; i
< t
->row_count
; i
++)
876 if (!t
->data_persistent
[i
])
878 row_count
= 1; /* yes, this is bizarre */
882 rawsize
= row_count
* row_size
;
883 rawdata
= calloc( 1, rawsize
);
886 r
= ERROR_NOT_ENOUGH_MEMORY
;
891 for (i
= 0; i
< row_count
; i
++)
893 UINT ofs
= 0, ofs_mem
= 0;
895 if (!t
->data_persistent
[i
]) break;
897 for (j
= 0; j
< t
->col_count
; j
++)
899 UINT m
= bytes_per_column( db
, &t
->colinfo
[j
], LONG_STR_BYTES
);
900 UINT n
= bytes_per_column( db
, &t
->colinfo
[j
], bytes_per_strref
);
903 if (n
!= 2 && n
!= 3 && n
!= 4)
905 ERR("oops - unknown column width %d\n", n
);
908 if (t
->colinfo
[j
].type
& MSITYPE_STRING
&& n
< m
)
910 UINT id
= read_table_int( t
->data
, i
, ofs_mem
, LONG_STR_BYTES
);
911 if (id
> 1 << bytes_per_strref
* 8)
913 ERR("string id %u out of range\n", id
);
917 for (k
= 0; k
< n
; k
++)
919 rawdata
[ofs
* row_count
+ i
* n
+ k
] = t
->data
[i
][ofs_mem
+ k
];
927 TRACE("writing %d bytes\n", rawsize
);
928 r
= write_stream_data( db
->storage
, t
->name
, rawdata
, rawsize
, TRUE
);
935 static void update_table_columns( MSIDATABASE
*db
, const WCHAR
*name
)
938 UINT size
, offset
, old_count
;
941 if (!(table
= find_cached_table( db
, name
))) return;
942 old_count
= table
->col_count
;
943 free_colinfo( table
->colinfo
, table
->col_count
);
944 free( table
->colinfo
);
945 table
->colinfo
= NULL
;
947 table_get_column_info( db
, name
, &table
->colinfo
, &table
->col_count
);
948 if (!table
->col_count
) return;
950 size
= table_get_row_size( db
, table
->colinfo
, table
->col_count
, LONG_STR_BYTES
);
951 offset
= table
->colinfo
[table
->col_count
- 1].offset
;
953 for ( n
= 0; n
< table
->row_count
; n
++ )
955 table
->data
[n
] = realloc( table
->data
[n
], size
);
956 if (old_count
< table
->col_count
)
957 memset( &table
->data
[n
][offset
], 0, size
- offset
);
961 /* try to find the table name in the _Tables table */
962 BOOL
TABLE_Exists( MSIDATABASE
*db
, LPCWSTR name
)
967 if( !wcscmp( name
, L
"_Tables" ) || !wcscmp( name
, L
"_Columns" ) ||
968 !wcscmp( name
, L
"_Streams" ) || !wcscmp( name
, L
"_Storages" ) )
971 r
= msi_string2id( db
->strings
, name
, -1, &table_id
);
972 if( r
!= ERROR_SUCCESS
)
974 TRACE("Couldn't find id for %s\n", debugstr_w(name
));
978 r
= get_table( db
, L
"_Tables", &table
);
979 if( r
!= ERROR_SUCCESS
)
981 ERR("table _Tables not available\n");
985 for( i
= 0; i
< table
->row_count
; i
++ )
987 if( read_table_int( table
->data
, i
, 0, LONG_STR_BYTES
) == table_id
)
994 /* below is the query interface to a table */
1001 struct column_info
*columns
;
1007 static UINT
TABLE_fetch_int( struct tagMSIVIEW
*view
, UINT row
, UINT col
, UINT
*val
)
1009 struct table_view
*tv
= (struct table_view
*)view
;
1013 return ERROR_INVALID_PARAMETER
;
1015 if( (col
==0) || (col
>tv
->num_cols
) )
1016 return ERROR_INVALID_PARAMETER
;
1018 /* how many rows are there ? */
1019 if( row
>= tv
->table
->row_count
)
1020 return ERROR_NO_MORE_ITEMS
;
1022 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1024 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1025 ERR("%p %p\n", tv
, tv
->columns
);
1026 return ERROR_FUNCTION_FAILED
;
1029 n
= bytes_per_column( tv
->db
, &tv
->columns
[col
- 1], LONG_STR_BYTES
);
1030 if (n
!= 2 && n
!= 3 && n
!= 4)
1032 ERR("oops! what is %d bytes per column?\n", n
);
1033 return ERROR_FUNCTION_FAILED
;
1036 offset
= tv
->columns
[col
-1].offset
;
1037 *val
= read_table_int(tv
->table
->data
, row
, offset
, n
);
1039 /* TRACE("Data [%d][%d] = %d\n", row, col, *val ); */
1041 return ERROR_SUCCESS
;
1044 static UINT
get_stream_name( const struct table_view
*tv
, UINT row
, WCHAR
**pstname
)
1046 LPWSTR p
, stname
= NULL
;
1047 UINT i
, r
, type
, ival
;
1050 MSIVIEW
*view
= (MSIVIEW
*) tv
;
1052 TRACE("%p %d\n", tv
, row
);
1054 len
= lstrlenW( tv
->name
) + 1;
1055 stname
= malloc( len
* sizeof(WCHAR
) );
1058 r
= ERROR_OUTOFMEMORY
;
1062 lstrcpyW( stname
, tv
->name
);
1064 for ( i
= 0; i
< tv
->num_cols
; i
++ )
1066 type
= tv
->columns
[i
].type
;
1067 if ( type
& MSITYPE_KEY
)
1071 r
= TABLE_fetch_int( view
, row
, i
+1, &ival
);
1072 if ( r
!= ERROR_SUCCESS
)
1075 if ( tv
->columns
[i
].type
& MSITYPE_STRING
)
1077 sval
= msi_string_lookup( tv
->db
->strings
, ival
, NULL
);
1080 r
= ERROR_INVALID_PARAMETER
;
1086 UINT n
= bytes_per_column( tv
->db
, &tv
->columns
[i
], LONG_STR_BYTES
);
1091 swprintf( number
, ARRAY_SIZE(number
), L
"%d", ival
-0x8000 );
1094 swprintf( number
, ARRAY_SIZE(number
), L
"%d", ival
^0x80000000 );
1097 ERR( "oops - unknown column width %d\n", n
);
1098 r
= ERROR_FUNCTION_FAILED
;
1104 len
+= lstrlenW( L
"." ) + lstrlenW( sval
);
1105 p
= realloc( stname
, len
* sizeof(WCHAR
) );
1108 r
= ERROR_OUTOFMEMORY
;
1113 lstrcatW( stname
, L
"." );
1114 lstrcatW( stname
, sval
);
1121 return ERROR_SUCCESS
;
1130 * We need a special case for streams, as we need to reference column with
1131 * the name of the stream in the same table, and the table name
1132 * which may not be available at higher levels of the query
1134 static UINT
TABLE_fetch_stream( struct tagMSIVIEW
*view
, UINT row
, UINT col
, IStream
**stm
)
1136 struct table_view
*tv
= (struct table_view
*)view
;
1140 if( !view
->ops
->fetch_int
)
1141 return ERROR_INVALID_PARAMETER
;
1143 r
= get_stream_name( tv
, row
, &name
);
1144 if (r
!= ERROR_SUCCESS
)
1146 ERR("fetching stream, error = %u\n", r
);
1150 r
= msi_get_stream( tv
->db
, name
, stm
);
1151 if (r
!= ERROR_SUCCESS
)
1152 ERR("fetching stream %s, error = %u\n", debugstr_w(name
), r
);
1158 /* Set a table value, i.e. preadjusted integer or string ID. */
1159 static UINT
table_set_bytes( struct table_view
*tv
, UINT row
, UINT col
, UINT val
)
1164 return ERROR_INVALID_PARAMETER
;
1166 if( (col
==0) || (col
>tv
->num_cols
) )
1167 return ERROR_INVALID_PARAMETER
;
1169 if( row
>= tv
->table
->row_count
)
1170 return ERROR_INVALID_PARAMETER
;
1172 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1174 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1175 ERR("%p %p\n", tv
, tv
->columns
);
1176 return ERROR_FUNCTION_FAILED
;
1179 free( tv
->columns
[col
-1].hash_table
);
1180 tv
->columns
[col
-1].hash_table
= NULL
;
1182 n
= bytes_per_column( tv
->db
, &tv
->columns
[col
- 1], LONG_STR_BYTES
);
1183 if ( n
!= 2 && n
!= 3 && n
!= 4 )
1185 ERR("oops! what is %d bytes per column?\n", n
);
1186 return ERROR_FUNCTION_FAILED
;
1189 offset
= tv
->columns
[col
-1].offset
;
1190 for ( i
= 0; i
< n
; i
++ )
1191 tv
->table
->data
[row
][offset
+ i
] = (val
>> i
* 8) & 0xff;
1193 return ERROR_SUCCESS
;
1196 static UINT
int_to_table_storage( const struct table_view
*tv
, UINT col
, int val
, UINT
*ret
)
1198 if ((tv
->columns
[col
-1].type
& MSI_DATASIZEMASK
) == 2)
1200 if (val
== MSI_NULL_INTEGER
)
1202 else if ((val
+ 0x8000) & 0xffff0000)
1204 ERR("value %d out of range\n", val
);
1205 return ERROR_FUNCTION_FAILED
;
1208 *ret
= val
+ 0x8000;
1211 *ret
= val
^ 0x80000000;
1213 return ERROR_SUCCESS
;
1216 static UINT
TABLE_set_int( MSIVIEW
*view
, UINT row
, UINT col
, int val
)
1218 struct table_view
*tv
= (struct table_view
*)view
;
1221 TRACE("row %u, col %u, val %d.\n", row
, col
, val
);
1223 if ((r
= int_to_table_storage( tv
, col
, val
, &table_int
)))
1226 if (tv
->columns
[col
-1].type
& MSITYPE_KEY
)
1230 if ((r
= TABLE_fetch_int( view
, row
, col
, &key
)))
1232 if (key
!= table_int
)
1234 ERR("Cannot modify primary key %s.%s.\n",
1235 debugstr_w(tv
->table
->name
), debugstr_w(tv
->columns
[col
-1].colname
));
1236 return ERROR_FUNCTION_FAILED
;
1240 return table_set_bytes( tv
, row
, col
, table_int
);
1243 static UINT
TABLE_set_string( MSIVIEW
*view
, UINT row
, UINT col
, const WCHAR
*val
, int len
)
1245 struct table_view
*tv
= (struct table_view
*)view
;
1249 TRACE("row %u, col %u, val %s.\n", row
, col
, debugstr_wn(val
, len
));
1251 persistent
= (tv
->table
->persistent
!= MSICONDITION_FALSE
)
1252 && tv
->table
->data_persistent
[row
];
1256 r
= msi_string2id( tv
->db
->strings
, val
, len
, &id
);
1257 if (r
!= ERROR_SUCCESS
)
1258 id
= msi_add_string( tv
->db
->strings
, val
, len
, persistent
);
1263 if (tv
->columns
[col
-1].type
& MSITYPE_KEY
)
1267 if ((r
= TABLE_fetch_int( view
, row
, col
, &key
)))
1271 ERR("Cannot modify primary key %s.%s.\n",
1272 debugstr_w(tv
->table
->name
), debugstr_w(tv
->columns
[col
-1].colname
));
1273 return ERROR_FUNCTION_FAILED
;
1277 return table_set_bytes( tv
, row
, col
, id
);
1280 static UINT
TABLE_get_row( struct tagMSIVIEW
*view
, UINT row
, MSIRECORD
**rec
)
1282 struct table_view
*tv
= (struct table_view
*)view
;
1285 return ERROR_INVALID_PARAMETER
;
1287 return msi_view_get_row(tv
->db
, view
, row
, rec
);
1290 static UINT
add_stream( MSIDATABASE
*db
, const WCHAR
*name
, IStream
*data
)
1296 TRACE("%p %s %p\n", db
, debugstr_w(name
), data
);
1298 if (!(rec
= MSI_CreateRecord( 2 )))
1299 return ERROR_OUTOFMEMORY
;
1301 r
= MSI_RecordSetStringW( rec
, 1, name
);
1302 if (r
!= ERROR_SUCCESS
)
1305 r
= MSI_RecordSetIStream( rec
, 2, data
);
1306 if (r
!= ERROR_SUCCESS
)
1309 r
= MSI_DatabaseOpenViewW( db
, L
"INSERT INTO `_Streams` (`Name`,`Data`) VALUES (?,?)", &query
);
1310 if (r
!= ERROR_SUCCESS
)
1313 r
= MSI_ViewExecute( query
, rec
);
1314 msiobj_release( &query
->hdr
);
1315 if (r
== ERROR_SUCCESS
)
1318 msiobj_release( &rec
->hdr
);
1319 if (!(rec
= MSI_CreateRecord( 2 )))
1320 return ERROR_OUTOFMEMORY
;
1322 r
= MSI_RecordSetIStream( rec
, 1, data
);
1323 if (r
!= ERROR_SUCCESS
)
1326 r
= MSI_RecordSetStringW( rec
, 2, name
);
1327 if (r
!= ERROR_SUCCESS
)
1330 r
= MSI_DatabaseOpenViewW( db
, L
"UPDATE `_Streams` SET `Data` = ? WHERE `Name` = ?", &query
);
1331 if (r
!= ERROR_SUCCESS
)
1334 r
= MSI_ViewExecute( query
, rec
);
1335 msiobj_release( &query
->hdr
);
1338 msiobj_release( &rec
->hdr
);
1342 static UINT
TABLE_set_stream( MSIVIEW
*view
, UINT row
, UINT col
, IStream
*stream
)
1344 struct table_view
*tv
= (struct table_view
*)view
;
1348 TRACE("row %u, col %u, stream %p.\n", row
, col
, stream
);
1350 if ((r
= get_stream_name( tv
, row
- 1, &name
)))
1353 r
= add_stream( tv
->db
, name
, stream
);
1358 static UINT
get_table_value_from_record( struct table_view
*tv
, MSIRECORD
*rec
, UINT iField
, UINT
*pvalue
)
1360 struct column_info columninfo
;
1363 if (!iField
|| iField
> tv
->num_cols
|| MSI_RecordIsNull( rec
, iField
))
1364 return ERROR_FUNCTION_FAILED
;
1366 columninfo
= tv
->columns
[ iField
- 1 ];
1368 if ( MSITYPE_IS_BINARY(columninfo
.type
) )
1370 *pvalue
= 1; /* refers to the first key column */
1372 else if ( columninfo
.type
& MSITYPE_STRING
)
1375 const WCHAR
*sval
= msi_record_get_string( rec
, iField
, &len
);
1378 r
= msi_string2id( tv
->db
->strings
, sval
, len
, pvalue
);
1379 if (r
!= ERROR_SUCCESS
)
1380 return ERROR_NOT_FOUND
;
1385 return int_to_table_storage( tv
, iField
, MSI_RecordGetInteger( rec
, iField
), pvalue
);
1387 return ERROR_SUCCESS
;
1390 static UINT
TABLE_set_row( struct tagMSIVIEW
*view
, UINT row
, MSIRECORD
*rec
, UINT mask
)
1392 struct table_view
*tv
= (struct table_view
*)view
;
1393 UINT i
, val
, r
= ERROR_SUCCESS
;
1396 return ERROR_INVALID_PARAMETER
;
1398 /* test if any of the mask bits are invalid */
1399 if ( mask
>= (1<<tv
->num_cols
) )
1400 return ERROR_INVALID_PARAMETER
;
1402 for ( i
= 0; i
< tv
->num_cols
; i
++ )
1406 /* only update the fields specified in the mask */
1407 if ( !(mask
&(1<<i
)) )
1410 persistent
= (tv
->table
->persistent
!= MSICONDITION_FALSE
) &&
1411 (tv
->table
->data_persistent
[row
]);
1412 /* FIXME: should we allow updating keys? */
1415 if ( !MSI_RecordIsNull( rec
, i
+ 1 ) )
1417 r
= get_table_value_from_record (tv
, rec
, i
+ 1, &val
);
1418 if ( MSITYPE_IS_BINARY(tv
->columns
[ i
].type
) )
1423 if ( r
!= ERROR_SUCCESS
)
1424 return ERROR_FUNCTION_FAILED
;
1426 r
= MSI_RecordGetIStream( rec
, i
+ 1, &stm
);
1427 if ( r
!= ERROR_SUCCESS
)
1430 r
= get_stream_name( tv
, row
, &stname
);
1431 if ( r
!= ERROR_SUCCESS
)
1433 IStream_Release( stm
);
1437 r
= add_stream( tv
->db
, stname
, stm
);
1438 IStream_Release( stm
);
1441 if ( r
!= ERROR_SUCCESS
)
1444 else if ( tv
->columns
[i
].type
& MSITYPE_STRING
)
1448 if ( r
!= ERROR_SUCCESS
)
1451 const WCHAR
*sval
= msi_record_get_string( rec
, i
+ 1, &len
);
1452 val
= msi_add_string( tv
->db
->strings
, sval
, len
, persistent
);
1456 TABLE_fetch_int(&tv
->view
, row
, i
+ 1, &x
);
1463 if ( r
!= ERROR_SUCCESS
)
1464 return ERROR_FUNCTION_FAILED
;
1468 r
= table_set_bytes( tv
, row
, i
+1, val
);
1469 if ( r
!= ERROR_SUCCESS
)
1475 static UINT
table_create_new_row( struct tagMSIVIEW
*view
, UINT
*num
, BOOL temporary
)
1477 struct table_view
*tv
= (struct table_view
*)view
;
1482 BOOL
**data_persist_ptr
;
1485 TRACE("%p %s\n", view
, temporary
? "TRUE" : "FALSE");
1488 return ERROR_INVALID_PARAMETER
;
1490 row
= calloc( 1, tv
->row_size
);
1492 return ERROR_NOT_ENOUGH_MEMORY
;
1494 row_count
= &tv
->table
->row_count
;
1495 data_ptr
= &tv
->table
->data
;
1496 data_persist_ptr
= &tv
->table
->data_persistent
;
1498 *num
= tv
->table
->row_count
;
1500 sz
= (*row_count
+ 1) * sizeof (BYTE
*);
1502 p
= realloc( *data_ptr
, sz
);
1508 return ERROR_NOT_ENOUGH_MEMORY
;
1511 sz
= (*row_count
+ 1) * sizeof (BOOL
);
1512 if( *data_persist_ptr
)
1513 b
= realloc( *data_persist_ptr
, sz
);
1520 return ERROR_NOT_ENOUGH_MEMORY
;
1524 (*data_ptr
)[*row_count
] = row
;
1526 *data_persist_ptr
= b
;
1527 (*data_persist_ptr
)[*row_count
] = !temporary
;
1531 return ERROR_SUCCESS
;
1534 static UINT
TABLE_execute( struct tagMSIVIEW
*view
, MSIRECORD
*record
)
1536 struct table_view
*tv
= (struct table_view
*)view
;
1538 TRACE("%p %p\n", tv
, record
);
1540 TRACE("There are %d columns\n", tv
->num_cols
);
1542 return ERROR_SUCCESS
;
1545 static UINT
TABLE_close( struct tagMSIVIEW
*view
)
1547 TRACE("%p\n", view
);
1549 return ERROR_SUCCESS
;
1552 static UINT
TABLE_get_dimensions( struct tagMSIVIEW
*view
, UINT
*rows
, UINT
*cols
)
1554 struct table_view
*tv
= (struct table_view
*)view
;
1556 TRACE("%p %p %p\n", view
, rows
, cols
);
1559 *cols
= tv
->num_cols
;
1563 return ERROR_INVALID_PARAMETER
;
1564 *rows
= tv
->table
->row_count
;
1567 return ERROR_SUCCESS
;
1570 static UINT
TABLE_get_column_info( struct tagMSIVIEW
*view
,
1571 UINT n
, LPCWSTR
*name
, UINT
*type
, BOOL
*temporary
,
1572 LPCWSTR
*table_name
)
1574 struct table_view
*tv
= (struct table_view
*)view
;
1576 TRACE("%p %d %p %p\n", tv
, n
, name
, type
);
1578 if( ( n
== 0 ) || ( n
> tv
->num_cols
) )
1579 return ERROR_INVALID_PARAMETER
;
1583 *name
= tv
->columns
[n
-1].colname
;
1585 return ERROR_FUNCTION_FAILED
;
1590 *table_name
= tv
->columns
[n
-1].tablename
;
1592 return ERROR_FUNCTION_FAILED
;
1596 *type
= tv
->columns
[n
-1].type
;
1599 *temporary
= (tv
->columns
[n
-1].type
& MSITYPE_TEMPORARY
) != 0;
1601 return ERROR_SUCCESS
;
1604 static UINT
table_find_row( struct table_view
*, MSIRECORD
*, UINT
*, UINT
* );
1606 static UINT
table_validate_new( struct table_view
*tv
, MSIRECORD
*rec
, UINT
*column
)
1610 /* check there are no null values where they're not allowed */
1611 for( i
= 0; i
< tv
->num_cols
; i
++ )
1613 if ( tv
->columns
[i
].type
& MSITYPE_NULLABLE
)
1616 if ( MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
1617 TRACE("skipping binary column\n");
1618 else if ( tv
->columns
[i
].type
& MSITYPE_STRING
)
1621 const WCHAR
*str
= msi_record_get_string( rec
, i
+1, &len
);
1623 if (!str
|| (!str
[0] && !len
))
1625 if (column
) *column
= i
;
1626 return ERROR_INVALID_DATA
;
1633 n
= MSI_RecordGetInteger( rec
, i
+1 );
1634 if (n
== MSI_NULL_INTEGER
)
1636 if (column
) *column
= i
;
1637 return ERROR_INVALID_DATA
;
1642 /* check there are no duplicate keys */
1643 r
= table_find_row( tv
, rec
, &row
, column
);
1644 if (r
== ERROR_SUCCESS
)
1645 return ERROR_FUNCTION_FAILED
;
1647 return ERROR_SUCCESS
;
1650 static int compare_record( struct table_view
*tv
, UINT row
, MSIRECORD
*rec
)
1652 UINT r
, i
, ivalue
, x
;
1654 for (i
= 0; i
< tv
->num_cols
; i
++ )
1656 if (!(tv
->columns
[i
].type
& MSITYPE_KEY
)) continue;
1658 r
= get_table_value_from_record( tv
, rec
, i
+ 1, &ivalue
);
1659 if (r
!= ERROR_SUCCESS
)
1662 r
= TABLE_fetch_int( &tv
->view
, row
, i
+ 1, &x
);
1663 if (r
!= ERROR_SUCCESS
)
1665 WARN("TABLE_fetch_int should not fail here %u\n", r
);
1672 else if (ivalue
== x
)
1674 if (i
< tv
->num_cols
- 1) continue;
1683 static int find_insert_index( struct table_view
*tv
, MSIRECORD
*rec
)
1685 int idx
, c
, low
= 0, high
= tv
->table
->row_count
- 1;
1687 TRACE("%p %p\n", tv
, rec
);
1691 idx
= (low
+ high
) / 2;
1692 c
= compare_record( tv
, idx
, rec
);
1700 TRACE("found %u\n", idx
);
1704 TRACE("found %u\n", high
+ 1);
1708 static UINT
TABLE_insert_row( struct tagMSIVIEW
*view
, MSIRECORD
*rec
, UINT row
, BOOL temporary
)
1710 struct table_view
*tv
= (struct table_view
*)view
;
1713 TRACE("%p %p %s\n", tv
, rec
, temporary
? "TRUE" : "FALSE" );
1715 /* check that the key is unique - can we find a matching row? */
1716 r
= table_validate_new( tv
, rec
, NULL
);
1717 if( r
!= ERROR_SUCCESS
)
1718 return ERROR_FUNCTION_FAILED
;
1721 row
= find_insert_index( tv
, rec
);
1723 r
= table_create_new_row( view
, &row
, temporary
);
1724 TRACE("insert_row returned %08x\n", r
);
1725 if( r
!= ERROR_SUCCESS
)
1728 /* shift the rows to make room for the new row */
1729 for (i
= tv
->table
->row_count
- 1; i
> row
; i
--)
1731 memmove(&(tv
->table
->data
[i
][0]),
1732 &(tv
->table
->data
[i
- 1][0]), tv
->row_size
);
1733 tv
->table
->data_persistent
[i
] = tv
->table
->data_persistent
[i
- 1];
1736 /* Re-set the persistence flag */
1737 tv
->table
->data_persistent
[row
] = !temporary
;
1738 return TABLE_set_row( view
, row
, rec
, (1<<tv
->num_cols
) - 1 );
1741 static UINT
TABLE_delete_row( struct tagMSIVIEW
*view
, UINT row
)
1743 struct table_view
*tv
= (struct table_view
*)view
;
1744 UINT r
, num_rows
, num_cols
, i
;
1746 TRACE("%p %d\n", tv
, row
);
1749 return ERROR_INVALID_PARAMETER
;
1751 r
= TABLE_get_dimensions( view
, &num_rows
, &num_cols
);
1752 if ( r
!= ERROR_SUCCESS
)
1755 if ( row
>= num_rows
)
1756 return ERROR_FUNCTION_FAILED
;
1758 num_rows
= tv
->table
->row_count
;
1759 tv
->table
->row_count
--;
1761 /* reset the hash tables */
1762 for (i
= 0; i
< tv
->num_cols
; i
++)
1764 free(tv
->columns
[i
].hash_table
);
1765 tv
->columns
[i
].hash_table
= NULL
;
1768 for (i
= row
+ 1; i
< num_rows
; i
++)
1770 memcpy(tv
->table
->data
[i
- 1], tv
->table
->data
[i
], tv
->row_size
);
1771 tv
->table
->data_persistent
[i
- 1] = tv
->table
->data_persistent
[i
];
1774 free(tv
->table
->data
[num_rows
- 1]);
1776 return ERROR_SUCCESS
;
1779 static UINT
table_update(struct tagMSIVIEW
*view
, MSIRECORD
*rec
, UINT row
)
1781 struct table_view
*tv
= (struct table_view
*)view
;
1784 /* FIXME: MsiViewFetch should set rec index 0 to some ID that
1785 * sets the fetched record apart from other records
1789 return ERROR_INVALID_PARAMETER
;
1791 r
= table_find_row(tv
, rec
, &new_row
, NULL
);
1792 if (r
!= ERROR_SUCCESS
)
1794 ERR("can't find row to modify\n");
1795 return ERROR_FUNCTION_FAILED
;
1798 /* the row cannot be changed */
1800 return ERROR_FUNCTION_FAILED
;
1802 return TABLE_set_row(view
, new_row
, rec
, (1 << tv
->num_cols
) - 1);
1805 static UINT
table_assign(struct tagMSIVIEW
*view
, MSIRECORD
*rec
)
1807 struct table_view
*tv
= (struct table_view
*)view
;
1811 return ERROR_INVALID_PARAMETER
;
1813 r
= table_find_row(tv
, rec
, &row
, NULL
);
1814 if (r
== ERROR_SUCCESS
)
1815 return TABLE_set_row(view
, row
, rec
, (1 << tv
->num_cols
) - 1);
1817 return TABLE_insert_row( view
, rec
, -1, FALSE
);
1820 static UINT
refresh_record( struct tagMSIVIEW
*view
, MSIRECORD
*rec
, UINT row
)
1825 r
= TABLE_get_row(view
, row
, &curr
);
1826 if (r
!= ERROR_SUCCESS
)
1829 /* Close the original record */
1830 MSI_CloseRecord(&rec
->hdr
);
1832 count
= MSI_RecordGetFieldCount(rec
);
1833 for (i
= 0; i
< count
; i
++)
1834 MSI_RecordCopyField(curr
, i
+ 1, rec
, i
+ 1);
1836 msiobj_release(&curr
->hdr
);
1837 return ERROR_SUCCESS
;
1840 static UINT
TABLE_modify( struct tagMSIVIEW
*view
, MSIMODIFY eModifyMode
,
1841 MSIRECORD
*rec
, UINT row
)
1843 struct table_view
*tv
= (struct table_view
*)view
;
1844 UINT r
, frow
, column
;
1846 TRACE("%p %d %p\n", view
, eModifyMode
, rec
);
1848 switch (eModifyMode
)
1850 case MSIMODIFY_DELETE
:
1851 r
= TABLE_delete_row( view
, row
);
1853 case MSIMODIFY_VALIDATE_NEW
:
1854 r
= table_validate_new( tv
, rec
, &column
);
1855 if (r
!= ERROR_SUCCESS
)
1857 tv
->view
.error
= MSIDBERROR_DUPLICATEKEY
;
1858 tv
->view
.error_column
= tv
->columns
[column
].colname
;
1859 r
= ERROR_INVALID_DATA
;
1863 case MSIMODIFY_INSERT
:
1864 r
= table_validate_new( tv
, rec
, NULL
);
1865 if (r
!= ERROR_SUCCESS
)
1867 r
= TABLE_insert_row( view
, rec
, -1, FALSE
);
1870 case MSIMODIFY_INSERT_TEMPORARY
:
1871 r
= table_validate_new( tv
, rec
, NULL
);
1872 if (r
!= ERROR_SUCCESS
)
1874 r
= TABLE_insert_row( view
, rec
, -1, TRUE
);
1877 case MSIMODIFY_REFRESH
:
1878 r
= refresh_record( view
, rec
, row
);
1881 case MSIMODIFY_UPDATE
:
1882 r
= table_update( view
, rec
, row
);
1885 case MSIMODIFY_ASSIGN
:
1886 r
= table_assign( view
, rec
);
1889 case MSIMODIFY_MERGE
:
1890 /* check row that matches this record */
1891 r
= table_find_row( tv
, rec
, &frow
, &column
);
1892 if (r
!= ERROR_SUCCESS
)
1894 r
= table_validate_new( tv
, rec
, NULL
);
1895 if (r
== ERROR_SUCCESS
)
1896 r
= TABLE_insert_row( view
, rec
, -1, FALSE
);
1900 case MSIMODIFY_REPLACE
:
1901 case MSIMODIFY_VALIDATE
:
1902 case MSIMODIFY_VALIDATE_FIELD
:
1903 case MSIMODIFY_VALIDATE_DELETE
:
1904 FIXME("%p %d %p - mode not implemented\n", view
, eModifyMode
, rec
);
1905 r
= ERROR_CALL_NOT_IMPLEMENTED
;
1909 r
= ERROR_INVALID_DATA
;
1915 static UINT
TABLE_delete( struct tagMSIVIEW
*view
)
1917 struct table_view
*tv
= (struct table_view
*)view
;
1919 TRACE("%p\n", view
);
1926 return ERROR_SUCCESS
;
1929 static UINT
TABLE_add_ref(struct tagMSIVIEW
*view
)
1931 struct table_view
*tv
= (struct table_view
*)view
;
1933 TRACE( "%p, %ld\n", view
, tv
->table
->ref_count
);
1934 return InterlockedIncrement(&tv
->table
->ref_count
);
1937 static UINT
TABLE_remove_column(struct tagMSIVIEW
*view
, UINT number
)
1939 struct table_view
*tv
= (struct table_view
*)view
;
1940 MSIRECORD
*rec
= NULL
;
1941 MSIVIEW
*columns
= NULL
;
1944 if (tv
->table
->col_count
!= number
)
1945 return ERROR_BAD_QUERY_SYNTAX
;
1947 if (tv
->table
->colinfo
[number
-1].type
& MSITYPE_TEMPORARY
)
1949 UINT size
= tv
->table
->colinfo
[number
-1].offset
;
1950 tv
->table
->col_count
--;
1951 tv
->table
->colinfo
= realloc(tv
->table
->colinfo
, sizeof(*tv
->table
->colinfo
) * tv
->table
->col_count
);
1953 for (row
= 0; row
< tv
->table
->row_count
; row
++)
1954 tv
->table
->data
[row
] = realloc(tv
->table
->data
[row
], size
);
1955 return ERROR_SUCCESS
;
1958 rec
= MSI_CreateRecord(2);
1960 return ERROR_OUTOFMEMORY
;
1962 MSI_RecordSetStringW(rec
, 1, tv
->name
);
1963 MSI_RecordSetInteger(rec
, 2, number
);
1965 r
= TABLE_CreateView(tv
->db
, L
"_Columns", &columns
);
1966 if (r
!= ERROR_SUCCESS
)
1968 msiobj_release(&rec
->hdr
);
1972 r
= table_find_row((struct table_view
*)columns
, rec
, &row
, NULL
);
1973 if (r
!= ERROR_SUCCESS
)
1976 r
= TABLE_delete_row(columns
, row
);
1977 if (r
!= ERROR_SUCCESS
)
1980 update_table_columns(tv
->db
, tv
->name
);
1983 msiobj_release(&rec
->hdr
);
1984 columns
->ops
->delete(columns
);
1988 static UINT
TABLE_release(struct tagMSIVIEW
*view
)
1990 struct table_view
*tv
= (struct table_view
*)view
;
1991 INT ref
= tv
->table
->ref_count
;
1995 TRACE("%p %d\n", view
, ref
);
1997 ref
= InterlockedDecrement(&tv
->table
->ref_count
);
2000 for (i
= tv
->table
->col_count
- 1; i
>= 0; i
--)
2002 if (tv
->table
->colinfo
[i
].type
& MSITYPE_TEMPORARY
)
2004 r
= TABLE_remove_column(view
, tv
->table
->colinfo
[i
].number
);
2005 if (r
!= ERROR_SUCCESS
)
2014 if (!tv
->table
->col_count
)
2016 list_remove(&tv
->table
->entry
);
2017 free_table(tv
->table
);
2025 static UINT
TABLE_add_column(struct tagMSIVIEW
*view
, LPCWSTR column
,
2026 INT type
, BOOL hold
)
2028 UINT i
, r
, table_id
, col_id
, size
, offset
;
2029 BOOL temporary
= type
& MSITYPE_TEMPORARY
;
2030 struct table_view
*tv
= (struct table_view
*)view
;
2031 struct column_info
*colinfo
;
2033 if (temporary
&& !hold
&& !tv
->table
->ref_count
)
2034 return ERROR_SUCCESS
;
2036 if (!temporary
&& tv
->table
->col_count
&&
2037 tv
->table
->colinfo
[tv
->table
->col_count
-1].type
& MSITYPE_TEMPORARY
)
2038 return ERROR_BAD_QUERY_SYNTAX
;
2040 for (i
= 0; i
< tv
->table
->col_count
; i
++)
2042 if (!wcscmp(tv
->table
->colinfo
[i
].colname
, column
))
2043 return ERROR_BAD_QUERY_SYNTAX
;
2046 colinfo
= realloc(tv
->table
->colinfo
, sizeof(*tv
->table
->colinfo
) * (tv
->table
->col_count
+ 1));
2048 return ERROR_OUTOFMEMORY
;
2049 tv
->table
->colinfo
= colinfo
;
2051 r
= msi_string2id( tv
->db
->strings
, tv
->name
, -1, &table_id
);
2052 if (r
!= ERROR_SUCCESS
)
2054 col_id
= msi_add_string( tv
->db
->strings
, column
, -1, !temporary
);
2056 colinfo
[tv
->table
->col_count
].tablename
= msi_string_lookup( tv
->db
->strings
, table_id
, NULL
);
2057 colinfo
[tv
->table
->col_count
].number
= tv
->table
->col_count
+ 1;
2058 colinfo
[tv
->table
->col_count
].colname
= msi_string_lookup( tv
->db
->strings
, col_id
, NULL
);
2059 colinfo
[tv
->table
->col_count
].type
= type
;
2060 colinfo
[tv
->table
->col_count
].offset
= 0;
2061 colinfo
[tv
->table
->col_count
].hash_table
= NULL
;
2062 tv
->table
->col_count
++;
2064 table_calc_column_offsets( tv
->db
, tv
->table
->colinfo
, tv
->table
->col_count
);
2066 size
= table_get_row_size( tv
->db
, tv
->table
->colinfo
, tv
->table
->col_count
, LONG_STR_BYTES
);
2067 offset
= tv
->table
->colinfo
[tv
->table
->col_count
- 1].offset
;
2068 for (i
= 0; i
< tv
->table
->row_count
; i
++)
2070 BYTE
*data
= realloc(tv
->table
->data
[i
], size
);
2073 tv
->table
->col_count
--;
2074 return ERROR_OUTOFMEMORY
;
2077 tv
->table
->data
[i
] = data
;
2078 memset(data
+ offset
, 0, size
- offset
);
2086 rec
= MSI_CreateRecord(4);
2089 tv
->table
->col_count
--;
2090 return ERROR_OUTOFMEMORY
;
2093 MSI_RecordSetStringW(rec
, 1, tv
->name
);
2094 MSI_RecordSetInteger(rec
, 2, tv
->table
->col_count
);
2095 MSI_RecordSetStringW(rec
, 3, column
);
2096 MSI_RecordSetInteger(rec
, 4, type
);
2098 r
= TABLE_CreateView(tv
->db
, L
"_Columns", &columns
);
2099 if (r
!= ERROR_SUCCESS
)
2101 tv
->table
->col_count
--;
2102 msiobj_release(&rec
->hdr
);
2106 r
= TABLE_insert_row(columns
, rec
, -1, FALSE
);
2107 columns
->ops
->delete(columns
);
2108 msiobj_release(&rec
->hdr
);
2109 if (r
!= ERROR_SUCCESS
)
2111 tv
->table
->col_count
--;
2116 TABLE_add_ref(view
);
2117 return ERROR_SUCCESS
;
2120 static UINT
TABLE_drop(struct tagMSIVIEW
*view
)
2122 struct table_view
*tv
= (struct table_view
*)view
;
2123 MSIVIEW
*tables
= NULL
;
2124 MSIRECORD
*rec
= NULL
;
2128 TRACE("dropping table %s\n", debugstr_w(tv
->name
));
2130 for (i
= tv
->table
->col_count
- 1; i
>= 0; i
--)
2132 r
= TABLE_remove_column(view
, tv
->table
->colinfo
[i
].number
);
2133 if (r
!= ERROR_SUCCESS
)
2137 rec
= MSI_CreateRecord(1);
2139 return ERROR_OUTOFMEMORY
;
2141 MSI_RecordSetStringW(rec
, 1, tv
->name
);
2143 r
= TABLE_CreateView(tv
->db
, L
"_Tables", &tables
);
2144 if (r
!= ERROR_SUCCESS
)
2146 msiobj_release(&rec
->hdr
);
2150 r
= table_find_row((struct table_view
*)tables
, rec
, &row
, NULL
);
2151 if (r
!= ERROR_SUCCESS
)
2154 r
= TABLE_delete_row(tables
, row
);
2155 if (r
!= ERROR_SUCCESS
)
2158 list_remove(&tv
->table
->entry
);
2159 free_table(tv
->table
);
2162 msiobj_release(&rec
->hdr
);
2163 tables
->ops
->delete(tables
);
2168 static const MSIVIEWOPS table_ops
=
2180 TABLE_get_dimensions
,
2181 TABLE_get_column_info
,
2191 UINT
TABLE_CreateView( MSIDATABASE
*db
, LPCWSTR name
, MSIVIEW
**view
)
2193 struct table_view
*tv
;
2196 TRACE("%p %s %p\n", db
, debugstr_w(name
), view
);
2198 if ( !wcscmp( name
, L
"_Streams" ) )
2199 return STREAMS_CreateView( db
, view
);
2200 else if ( !wcscmp( name
, L
"_Storages" ) )
2201 return STORAGES_CreateView( db
, view
);
2203 sz
= FIELD_OFFSET( struct table_view
, name
[lstrlenW( name
) + 1] );
2204 tv
= calloc( 1, sz
);
2206 return ERROR_FUNCTION_FAILED
;
2208 r
= get_table( db
, name
, &tv
->table
);
2209 if( r
!= ERROR_SUCCESS
)
2212 WARN("table not found\n");
2216 TRACE("table %p found with %d columns\n", tv
->table
, tv
->table
->col_count
);
2218 /* fill the structure */
2219 tv
->view
.ops
= &table_ops
;
2221 tv
->columns
= tv
->table
->colinfo
;
2222 tv
->num_cols
= tv
->table
->col_count
;
2223 tv
->row_size
= table_get_row_size( db
, tv
->table
->colinfo
, tv
->table
->col_count
, LONG_STR_BYTES
);
2225 TRACE("%s one row is %d bytes\n", debugstr_w(name
), tv
->row_size
);
2227 *view
= (MSIVIEW
*) tv
;
2228 lstrcpyW( tv
->name
, name
);
2230 return ERROR_SUCCESS
;
2233 static WCHAR
*create_key_string(struct table_view
*tv
, MSIRECORD
*rec
)
2235 DWORD i
, p
, len
, key_len
= 0;
2238 for (i
= 0; i
< tv
->num_cols
; i
++)
2240 if (!(tv
->columns
[i
].type
& MSITYPE_KEY
))
2242 if (MSI_RecordGetStringW( rec
, i
+1, NULL
, &len
) == ERROR_SUCCESS
)
2247 key
= malloc( key_len
* sizeof(WCHAR
) );
2252 for (i
= 0; i
< tv
->num_cols
; i
++)
2254 if (!(tv
->columns
[i
].type
& MSITYPE_KEY
))
2259 if (MSI_RecordGetStringW( rec
, i
+1, key
+ p
, &len
) == ERROR_SUCCESS
)
2265 static UINT
record_stream_name( const struct table_view
*tv
, MSIRECORD
*rec
, WCHAR
*name
, DWORD
*len
)
2270 l
= wcslen( tv
->name
);
2271 if (name
&& *len
> l
)
2272 memcpy(name
, tv
->name
, l
* sizeof(WCHAR
));
2275 for ( i
= 0; i
< tv
->num_cols
; i
++ )
2277 if (!(tv
->columns
[i
].type
& MSITYPE_KEY
))
2280 if (name
&& *len
> p
+ 1)
2284 l
= (*len
> p
? *len
- p
: 0);
2285 r
= MSI_RecordGetStringW( rec
, i
+ 1, name
? name
+ p
: NULL
, &l
);
2286 if (r
!= ERROR_SUCCESS
)
2291 if (name
&& *len
> p
)
2295 return ERROR_SUCCESS
;
2298 static UINT
TransformView_fetch_int( MSIVIEW
*view
, UINT row
, UINT col
, UINT
*val
)
2300 struct table_view
*tv
= (struct table_view
*)view
;
2302 if (!tv
->table
|| col
> tv
->table
->col_count
)
2305 return ERROR_SUCCESS
;
2307 return TABLE_fetch_int( view
, row
, col
, val
);
2310 static UINT
TransformView_fetch_stream( MSIVIEW
*view
, UINT row
, UINT col
, IStream
**stm
)
2312 struct table_view
*tv
= (struct table_view
*)view
;
2314 if (!tv
->table
|| col
> tv
->table
->col_count
)
2317 return ERROR_SUCCESS
;
2319 return TABLE_fetch_stream( view
, row
, col
, stm
);
2322 static UINT
TransformView_set_row( MSIVIEW
*view
, UINT row
, MSIRECORD
*rec
, UINT mask
)
2324 static const WCHAR query_pfx
[] =
2325 L
"INSERT INTO `_TransformView` (`new`, `Table`, `Column`, `Row`, `Data`, `Current`) VALUES (1, '";
2327 struct table_view
*tv
= (struct table_view
*)view
;
2328 WCHAR buf
[256], *query
;
2335 if (!wcscmp( tv
->name
, L
"_Columns" ))
2337 ERR( "trying to modify existing column\n" );
2338 return ERROR_INSTALL_TRANSFORM_FAILURE
;
2341 if (!wcscmp( tv
->name
, L
"_Tables" ))
2343 ERR( "trying to modify existing table\n" );
2344 return ERROR_INSTALL_TRANSFORM_FAILURE
;
2347 key
= create_key_string( tv
, rec
);
2349 return ERROR_OUTOFMEMORY
;
2351 r
= msi_view_get_row( tv
->db
, view
, row
, &old_rec
);
2352 if (r
!= ERROR_SUCCESS
)
2355 for (i
= 0; i
< tv
->num_cols
; i
++)
2357 if (!(mask
& (1 << i
)))
2359 if (tv
->columns
[i
].type
& MSITYPE_KEY
)
2362 qlen
= p
= ARRAY_SIZE( query_pfx
) - 1;
2363 qlen
+= wcslen( tv
->name
) + 3; /* strlen("','") */
2364 qlen
+= wcslen( tv
->columns
[i
].colname
) + 3;
2365 qlen
+= wcslen( key
) + 3;
2366 if (MSITYPE_IS_BINARY( tv
->columns
[i
].type
))
2367 r
= record_stream_name( tv
, rec
, NULL
, &len
);
2369 r
= MSI_RecordGetStringW( rec
, i
+ 1, NULL
, &len
);
2370 if (r
!= ERROR_SUCCESS
)
2373 msiobj_release( &old_rec
->hdr
);
2378 if (old_rec
&& (r
= MSI_RecordGetStringW( old_rec
, i
+1, NULL
, &len
)))
2380 msiobj_release( &old_rec
->hdr
);
2384 qlen
+= len
+ 3; /* strlen("')") + 1 */
2386 if (qlen
> ARRAY_SIZE(buf
))
2388 query
= malloc( qlen
* sizeof(WCHAR
) );
2392 msiobj_release( &old_rec
->hdr
);
2394 return ERROR_OUTOFMEMORY
;
2402 memcpy( query
, query_pfx
, p
* sizeof(WCHAR
) );
2403 len
= wcslen( tv
->name
);
2404 memcpy( query
+ p
, tv
->name
, len
* sizeof(WCHAR
) );
2409 len
= wcslen( tv
->columns
[i
].colname
);
2410 memcpy( query
+ p
, tv
->columns
[i
].colname
, len
* sizeof(WCHAR
) );
2415 len
= wcslen( key
);
2416 memcpy( query
+ p
, key
, len
* sizeof(WCHAR
) );
2422 if (MSITYPE_IS_BINARY( tv
->columns
[i
].type
))
2423 record_stream_name( tv
, rec
, query
+ p
, &len
);
2425 MSI_RecordGetStringW( rec
, i
+ 1, query
+ p
, &len
);
2433 MSI_RecordGetStringW( old_rec
, i
+ 1, query
+ p
, &len
);
2440 r
= MSI_DatabaseOpenViewW( tv
->db
, query
, &q
);
2443 if (r
!= ERROR_SUCCESS
)
2446 msiobj_release( &old_rec
->hdr
);
2451 r
= MSI_ViewExecute( q
, NULL
);
2452 msiobj_release( &q
->hdr
);
2453 if (r
!= ERROR_SUCCESS
)
2456 msiobj_release( &old_rec
->hdr
);
2463 msiobj_release( &old_rec
->hdr
);
2465 return ERROR_SUCCESS
;
2468 static UINT
TransformView_create_table( struct table_view
*tv
, MSIRECORD
*rec
)
2470 static const WCHAR query_fmt
[] =
2471 L
"INSERT INTO `_TransformView` (`Table`, `Column`, `new`) VALUES ('%s', 'CREATE', 1)";
2473 WCHAR buf
[256], *query
= buf
;
2479 name
= msi_record_get_string( rec
, 1, &len
);
2481 return ERROR_INSTALL_TRANSFORM_FAILURE
;
2483 len
= _snwprintf( NULL
, 0, query_fmt
, name
) + 1;
2484 if (len
> ARRAY_SIZE(buf
))
2486 query
= malloc( len
* sizeof(WCHAR
) );
2488 return ERROR_OUTOFMEMORY
;
2490 swprintf( query
, len
, query_fmt
, name
);
2492 r
= MSI_DatabaseOpenViewW( tv
->db
, query
, &q
);
2495 if (r
!= ERROR_SUCCESS
)
2498 r
= MSI_ViewExecute( q
, NULL
);
2499 msiobj_release( &q
->hdr
);
2503 static UINT
TransformView_add_column( struct table_view
*tv
, MSIRECORD
*rec
)
2505 static const WCHAR query_pfx
[] =
2506 L
"INSERT INTO `_TransformView` (`new`, `Table`, `Current`, `Column`, `Data`) VALUES (1, '";
2508 WCHAR buf
[256], *query
= buf
;
2513 qlen
= p
= wcslen( query_pfx
);
2514 for (i
= 1; i
<= 4; i
++)
2516 r
= MSI_RecordGetStringW( rec
, i
, NULL
, &len
);
2517 if (r
!= ERROR_SUCCESS
)
2519 qlen
+= len
+ 3; /* strlen( "','" ) */
2522 if (qlen
> ARRAY_SIZE(buf
))
2524 query
= malloc( len
* sizeof(WCHAR
) );
2527 return ERROR_OUTOFMEMORY
;
2530 memcpy( query
, query_pfx
, p
* sizeof(WCHAR
) );
2531 for (i
= 1; i
<= 4; i
++)
2534 MSI_RecordGetStringW( rec
, i
, query
+ p
, &len
);
2546 r
= MSI_DatabaseOpenViewW( tv
->db
, query
, &q
);
2549 if (r
!= ERROR_SUCCESS
)
2552 r
= MSI_ViewExecute( q
, NULL
);
2553 msiobj_release( &q
->hdr
);
2557 static UINT
TransformView_insert_row( MSIVIEW
*view
, MSIRECORD
*rec
, UINT row
, BOOL temporary
)
2559 static const WCHAR query_fmt
[] =
2560 L
"INSERT INTO `_TransformView` (`new`, `Table`, `Column`, `Row`) VALUES (1, '%s', 'INSERT', '%s')";
2562 struct table_view
*tv
= (struct table_view
*)view
;
2563 WCHAR buf
[256], *query
= buf
;
2569 if (!wcscmp(tv
->name
, L
"_Tables"))
2570 return TransformView_create_table( tv
, rec
);
2572 if (!wcscmp(tv
->name
, L
"_Columns"))
2573 return TransformView_add_column( tv
, rec
);
2575 key
= create_key_string( tv
, rec
);
2577 return ERROR_OUTOFMEMORY
;
2579 len
= _snwprintf( NULL
, 0, query_fmt
, tv
->name
, key
) + 1;
2580 if (len
> ARRAY_SIZE(buf
))
2582 query
= malloc( len
* sizeof(WCHAR
) );
2586 return ERROR_OUTOFMEMORY
;
2589 swprintf( query
, len
, query_fmt
, tv
->name
, key
);
2592 r
= MSI_DatabaseOpenViewW( tv
->db
, query
, &q
);
2595 if (r
!= ERROR_SUCCESS
)
2598 r
= MSI_ViewExecute( q
, NULL
);
2599 msiobj_release( &q
->hdr
);
2600 if (r
!= ERROR_SUCCESS
)
2603 return TransformView_set_row( view
, row
, rec
, ~0 );
2606 static UINT
TransformView_drop_table( struct table_view
*tv
, UINT row
)
2608 static const WCHAR query_pfx
[] = L
"INSERT INTO `_TransformView` ( `new`, `Table`, `Column` ) VALUES ( 1, '";
2609 static const WCHAR query_sfx
[] = L
"', 'DROP' )";
2611 WCHAR buf
[256], *query
= buf
;
2612 UINT r
, table_id
, len
;
2617 r
= TABLE_fetch_int( &tv
->view
, row
, 1, &table_id
);
2618 if (r
!= ERROR_SUCCESS
)
2621 table
= msi_string_lookup( tv
->db
->strings
, table_id
, &table_len
);
2623 return ERROR_INSTALL_TRANSFORM_FAILURE
;
2625 len
= ARRAY_SIZE(query_pfx
) - 1 + table_len
+ ARRAY_SIZE(query_sfx
);
2626 if (len
> ARRAY_SIZE(buf
))
2628 query
= malloc( len
* sizeof(WCHAR
) );
2630 return ERROR_OUTOFMEMORY
;
2633 memcpy( query
, query_pfx
, ARRAY_SIZE(query_pfx
) * sizeof(WCHAR
) );
2634 len
= ARRAY_SIZE(query_pfx
) - 1;
2635 memcpy( query
+ len
, table
, table_len
* sizeof(WCHAR
) );
2637 memcpy( query
+ len
, query_sfx
, ARRAY_SIZE(query_sfx
) * sizeof(WCHAR
) );
2639 r
= MSI_DatabaseOpenViewW( tv
->db
, query
, &q
);
2642 if (r
!= ERROR_SUCCESS
)
2645 r
= MSI_ViewExecute( q
, NULL
);
2646 msiobj_release( &q
->hdr
);
2650 static UINT
TransformView_delete_row( MSIVIEW
*view
, UINT row
)
2652 static const WCHAR query_pfx
[] = L
"INSERT INTO `_TransformView` ( `new`, `Table`, `Column`, `Row`) VALUES ( 1, '";
2653 static const WCHAR query_column
[] = L
"', 'DELETE', '";
2654 static const WCHAR query_sfx
[] = L
"')";
2656 struct table_view
*tv
= (struct table_view
*)view
;
2657 WCHAR
*key
, buf
[256], *query
= buf
;
2658 UINT r
, len
, name_len
, key_len
;
2662 if (!wcscmp( tv
->name
, L
"_Columns" ))
2664 ERR("trying to remove column\n");
2665 return ERROR_INSTALL_TRANSFORM_FAILURE
;
2668 if (!wcscmp( tv
->name
, L
"_Tables" ))
2669 return TransformView_drop_table( tv
, row
);
2671 r
= msi_view_get_row( tv
->db
, view
, row
, &rec
);
2672 if (r
!= ERROR_SUCCESS
)
2675 key
= create_key_string( tv
, rec
);
2676 msiobj_release( &rec
->hdr
);
2678 return ERROR_OUTOFMEMORY
;
2680 name_len
= wcslen( tv
->name
);
2681 key_len
= wcslen( key
);
2682 len
= ARRAY_SIZE(query_pfx
) + name_len
+ ARRAY_SIZE(query_column
) + key_len
+ ARRAY_SIZE(query_sfx
) - 2;
2683 if (len
> ARRAY_SIZE(buf
))
2685 query
= malloc( len
* sizeof(WCHAR
) );
2690 return ERROR_OUTOFMEMORY
;
2694 memcpy( query
, query_pfx
, ARRAY_SIZE(query_pfx
) * sizeof(WCHAR
) );
2695 len
= ARRAY_SIZE(query_pfx
) - 1;
2696 memcpy( query
+ len
, tv
->name
, name_len
* sizeof(WCHAR
) );
2698 memcpy( query
+ len
, query_column
, ARRAY_SIZE(query_column
) * sizeof(WCHAR
) );
2699 len
+= ARRAY_SIZE(query_column
) - 1;
2700 memcpy( query
+ len
, key
, key_len
* sizeof(WCHAR
) );
2702 memcpy( query
+ len
, query_sfx
, ARRAY_SIZE(query_sfx
) * sizeof(WCHAR
) );
2705 r
= MSI_DatabaseOpenViewW( tv
->db
, query
, &q
);
2708 if (r
!= ERROR_SUCCESS
)
2711 r
= MSI_ViewExecute( q
, NULL
);
2712 msiobj_release( &q
->hdr
);
2716 static UINT
TransformView_execute( MSIVIEW
*view
, MSIRECORD
*record
)
2718 return ERROR_SUCCESS
;
2721 static UINT
TransformView_close( MSIVIEW
*view
)
2723 return ERROR_SUCCESS
;
2726 static UINT
TransformView_get_dimensions( MSIVIEW
*view
, UINT
*rows
, UINT
*cols
)
2728 return TABLE_get_dimensions( view
, rows
, cols
);
2731 static UINT
TransformView_get_column_info( MSIVIEW
*view
, UINT n
, LPCWSTR
*name
, UINT
*type
,
2732 BOOL
*temporary
, LPCWSTR
*table_name
)
2734 return TABLE_get_column_info( view
, n
, name
, type
, temporary
, table_name
);
2737 static UINT
TransformView_delete( MSIVIEW
*view
)
2739 struct table_view
*tv
= (struct table_view
*)view
;
2740 if (!tv
->table
|| tv
->columns
!= tv
->table
->colinfo
)
2741 free( tv
->columns
);
2742 return TABLE_delete( view
);
2745 static const MSIVIEWOPS transform_view_ops
=
2747 TransformView_fetch_int
,
2748 TransformView_fetch_stream
,
2752 TransformView_set_row
,
2753 TransformView_insert_row
,
2754 TransformView_delete_row
,
2755 TransformView_execute
,
2756 TransformView_close
,
2757 TransformView_get_dimensions
,
2758 TransformView_get_column_info
,
2760 TransformView_delete
,
2768 static UINT
TransformView_Create( MSIDATABASE
*db
, string_table
*st
, LPCWSTR name
, MSIVIEW
**view
)
2770 static const WCHAR query_pfx
[] = L
"SELECT `Column`, `Data`, `Current` FROM `_TransformView` WHERE `Table`='";
2771 static const WCHAR query_sfx
[] = L
"' AND `Row` IS NULL AND `Current` IS NOT NULL AND `new` = 1";
2773 WCHAR buf
[256], *query
= buf
;
2774 UINT r
, len
, name_len
, size
, add_col
;
2775 struct column_info
*colinfo
;
2776 struct table_view
*tv
;
2780 name_len
= wcslen( name
);
2782 r
= TABLE_CreateView( db
, name
, (MSIVIEW
**)&tv
);
2783 if (r
== ERROR_INVALID_PARAMETER
)
2785 /* table does not exist */
2786 size
= FIELD_OFFSET( struct table_view
, name
[name_len
+ 1] );
2787 tv
= calloc( 1, size
);
2789 return ERROR_OUTOFMEMORY
;
2792 memcpy( tv
->name
, name
, name_len
* sizeof(WCHAR
) );
2794 else if (r
!= ERROR_SUCCESS
)
2799 tv
->view
.ops
= &transform_view_ops
;
2801 len
= ARRAY_SIZE(query_pfx
) + name_len
+ ARRAY_SIZE(query_sfx
) - 1;
2802 if (len
> ARRAY_SIZE(buf
))
2804 query
= malloc( len
* sizeof(WCHAR
) );
2808 return ERROR_OUTOFMEMORY
;
2811 memcpy( query
, query_pfx
, ARRAY_SIZE(query_pfx
) * sizeof(WCHAR
) );
2812 len
= ARRAY_SIZE(query_pfx
) - 1;
2813 memcpy( query
+ len
, name
, name_len
* sizeof(WCHAR
) );
2815 memcpy( query
+ len
, query_sfx
, ARRAY_SIZE(query_sfx
) * sizeof(WCHAR
) );
2817 r
= MSI_DatabaseOpenViewW( tv
->db
, query
, &q
);
2820 if (r
!= ERROR_SUCCESS
)
2826 r
= MSI_ViewExecute( q
, NULL
);
2827 if (r
!= ERROR_SUCCESS
)
2833 r
= q
->view
->ops
->get_dimensions( q
->view
, &add_col
, NULL
);
2834 if (r
!= ERROR_SUCCESS
)
2837 msiobj_release( &q
->hdr
);
2844 msiobj_release( &q
->hdr
);
2845 *view
= (MSIVIEW
*)tv
;
2846 return ERROR_SUCCESS
;
2849 colinfo
= calloc( add_col
+ tv
->num_cols
, sizeof(*colinfo
) );
2853 msiobj_release( &q
->hdr
);
2855 return ERROR_OUTOFMEMORY
;
2858 while (MSI_ViewFetch( q
, &rec
) == ERROR_SUCCESS
)
2861 const WCHAR
*name
= msi_record_get_string( rec
, 1, &name_len
);
2862 const WCHAR
*type
= msi_record_get_string( rec
, 2, NULL
);
2865 idx
= _wtoi( msi_record_get_string(rec
, 3, NULL
) );
2866 colinfo
[idx
- 1].number
= idx
;
2867 colinfo
[idx
- 1].type
= _wtoi( type
);
2869 r
= msi_string2id( st
, name
, name_len
, &name_id
);
2870 if (r
== ERROR_SUCCESS
)
2871 colinfo
[idx
- 1].colname
= msi_string_lookup( st
, name_id
, NULL
);
2873 ERR( "column name %s is not defined in strings table\n", wine_dbgstr_w(name
) );
2874 msiobj_release( &rec
->hdr
);
2877 msiobj_release( &q
->hdr
);
2879 memcpy( colinfo
, tv
->columns
, tv
->num_cols
* sizeof(*colinfo
) );
2880 tv
->columns
= colinfo
;
2881 tv
->num_cols
+= add_col
;
2882 *view
= (MSIVIEW
*)tv
;
2883 return ERROR_SUCCESS
;
2886 UINT
MSI_CommitTables( MSIDATABASE
*db
)
2888 UINT r
, bytes_per_strref
;
2890 MSITABLE
*table
= NULL
;
2894 r
= msi_save_string_table( db
->strings
, db
->storage
, &bytes_per_strref
);
2895 if( r
!= ERROR_SUCCESS
)
2897 WARN("failed to save string table r=%08x\n",r
);
2901 LIST_FOR_EACH_ENTRY( table
, &db
->tables
, MSITABLE
, entry
)
2903 r
= save_table( db
, table
, bytes_per_strref
);
2904 if( r
!= ERROR_SUCCESS
)
2906 WARN("failed to save table %s (r=%08x)\n",
2907 debugstr_w(table
->name
), r
);
2912 hr
= IStorage_Commit( db
->storage
, 0 );
2915 WARN( "failed to commit changes %#lx\n", hr
);
2916 r
= ERROR_FUNCTION_FAILED
;
2921 MSICONDITION
MSI_DatabaseIsTablePersistent( MSIDATABASE
*db
, LPCWSTR table
)
2926 TRACE("%p %s\n", db
, debugstr_w(table
));
2929 return MSICONDITION_ERROR
;
2931 r
= get_table( db
, table
, &t
);
2932 if (r
!= ERROR_SUCCESS
)
2933 return MSICONDITION_NONE
;
2935 return t
->persistent
;
2938 static UINT
read_raw_int(const BYTE
*data
, UINT col
, UINT bytes
)
2942 for (i
= 0; i
< bytes
; i
++)
2943 ret
+= (data
[col
+ i
] << i
* 8);
2948 static UINT
record_encoded_stream_name( const struct table_view
*tv
, MSIRECORD
*rec
, WCHAR
**pstname
)
2954 TRACE("%p %p\n", tv
, rec
);
2956 r
= record_stream_name( tv
, rec
, NULL
, &len
);
2957 if (r
!= ERROR_SUCCESS
)
2961 name
= malloc( len
* sizeof(WCHAR
) );
2963 return ERROR_OUTOFMEMORY
;
2965 r
= record_stream_name( tv
, rec
, name
, &len
);
2966 if (r
!= ERROR_SUCCESS
)
2972 *pstname
= encode_streamname( FALSE
, name
);
2974 return ERROR_SUCCESS
;
2977 static MSIRECORD
*get_transform_record( const struct table_view
*tv
, const string_table
*st
, IStorage
*stg
,
2978 const BYTE
*rawdata
, UINT bytes_per_strref
)
2980 UINT i
, val
, ofs
= 0;
2982 struct column_info
*columns
= tv
->columns
;
2985 mask
= rawdata
[0] | (rawdata
[1] << 8);
2988 rec
= MSI_CreateRecord( tv
->num_cols
);
2993 for( i
=0; i
<tv
->num_cols
; i
++ )
2995 if ( (mask
&1) && (i
>=(mask
>>8)) )
2997 /* all keys must be present */
2998 if ( (~mask
&1) && (~columns
[i
].type
& MSITYPE_KEY
) && ((1<<i
) & ~mask
) )
3001 if( MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
3004 IStream
*stm
= NULL
;
3007 ofs
+= bytes_per_column( tv
->db
, &columns
[i
], bytes_per_strref
);
3009 r
= record_encoded_stream_name( tv
, rec
, &encname
);
3010 if ( r
!= ERROR_SUCCESS
)
3012 msiobj_release( &rec
->hdr
);
3015 r
= IStorage_OpenStream( stg
, encname
, NULL
, STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, &stm
);
3016 if ( r
!= ERROR_SUCCESS
)
3018 msiobj_release( &rec
->hdr
);
3023 MSI_RecordSetStream( rec
, i
+1, stm
);
3024 TRACE(" field %d [%s]\n", i
+1, debugstr_w(encname
));
3027 else if( columns
[i
].type
& MSITYPE_STRING
)
3032 val
= read_raw_int(rawdata
, ofs
, bytes_per_strref
);
3033 sval
= msi_string_lookup( st
, val
, &len
);
3034 msi_record_set_string( rec
, i
+1, sval
, len
);
3035 TRACE(" field %d [%s]\n", i
+1, debugstr_wn(sval
, len
));
3036 ofs
+= bytes_per_strref
;
3040 UINT n
= bytes_per_column( tv
->db
, &columns
[i
], bytes_per_strref
);
3044 val
= read_raw_int(rawdata
, ofs
, n
);
3046 MSI_RecordSetInteger( rec
, i
+1, val
-0x8000 );
3047 TRACE(" field %d [0x%04x]\n", i
+1, val
);
3050 val
= read_raw_int(rawdata
, ofs
, n
);
3052 MSI_RecordSetInteger( rec
, i
+1, val
^0x80000000 );
3053 TRACE(" field %d [0x%08x]\n", i
+1, val
);
3056 ERR("oops - unknown column width %d\n", n
);
3065 static void dump_table( const string_table
*st
, const USHORT
*rawdata
, UINT rawsize
)
3068 for (i
= 0; i
< rawsize
/ 2; i
++)
3071 const WCHAR
*sval
= msi_string_lookup( st
, rawdata
[i
], &len
);
3072 MESSAGE(" %04x %s\n", rawdata
[i
], debugstr_wn(sval
, len
) );
3076 static UINT
*record_to_row( const struct table_view
*tv
, MSIRECORD
*rec
)
3080 data
= malloc( tv
->num_cols
* sizeof (UINT
) );
3081 for( i
=0; i
<tv
->num_cols
; i
++ )
3085 if ( ~tv
->columns
[i
].type
& MSITYPE_KEY
)
3088 /* turn the transform column value into a row value */
3089 if ( ( tv
->columns
[i
].type
& MSITYPE_STRING
) &&
3090 ! MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
3093 const WCHAR
*str
= msi_record_get_string( rec
, i
+1, &len
);
3096 r
= msi_string2id( tv
->db
->strings
, str
, len
, &data
[i
] );
3098 /* if there's no matching string in the string table,
3099 these keys can't match any record, so fail now. */
3100 if (r
!= ERROR_SUCCESS
)
3110 if (int_to_table_storage( tv
, i
+ 1, MSI_RecordGetInteger( rec
, i
+ 1 ), &data
[i
] ))
3120 static UINT
row_matches( struct table_view
*tv
, UINT row
, const UINT
*data
, UINT
*column
)
3122 UINT i
, r
, x
, ret
= ERROR_FUNCTION_FAILED
;
3124 for( i
=0; i
<tv
->num_cols
; i
++ )
3126 if ( ~tv
->columns
[i
].type
& MSITYPE_KEY
)
3129 /* turn the transform column value into a row value */
3130 r
= TABLE_fetch_int( &tv
->view
, row
, i
+1, &x
);
3131 if ( r
!= ERROR_SUCCESS
)
3133 ERR("TABLE_fetch_int shouldn't fail here\n");
3137 /* if this key matches, move to the next column */
3140 ret
= ERROR_FUNCTION_FAILED
;
3143 if (column
) *column
= i
;
3144 ret
= ERROR_SUCCESS
;
3149 static UINT
table_find_row( struct table_view
*tv
, MSIRECORD
*rec
, UINT
*row
, UINT
*column
)
3151 UINT i
, r
= ERROR_FUNCTION_FAILED
, *data
;
3153 data
= record_to_row( tv
, rec
);
3156 for( i
= 0; i
< tv
->table
->row_count
; i
++ )
3158 r
= row_matches( tv
, i
, data
, column
);
3159 if( r
== ERROR_SUCCESS
)
3169 struct transform_data
3175 static UINT
table_load_transform( MSIDATABASE
*db
, IStorage
*stg
, string_table
*st
, struct transform_data
*transform
,
3176 UINT bytes_per_strref
, int err_cond
)
3178 BYTE
*rawdata
= NULL
;
3179 struct table_view
*tv
= NULL
;
3180 UINT r
, n
, sz
, i
, mask
, num_cols
, colcol
= 0, rawsize
= 0;
3181 MSIRECORD
*rec
= NULL
;
3186 return ERROR_SUCCESS
;
3188 name
= transform
->name
;
3191 TRACE("%p %p %p %s\n", db
, stg
, st
, debugstr_w(name
) );
3193 /* read the transform data */
3194 read_stream_data( stg
, name
, TRUE
, &rawdata
, &rawsize
);
3197 TRACE("table %s empty\n", debugstr_w(name
) );
3198 return ERROR_INVALID_TABLE
;
3201 /* create a table view */
3202 if ( err_cond
& MSITRANSFORM_ERROR_VIEWTRANSFORM
)
3203 r
= TransformView_Create( db
, st
, name
, (MSIVIEW
**) &tv
);
3205 r
= TABLE_CreateView( db
, name
, (MSIVIEW
**) &tv
);
3206 if( r
!= ERROR_SUCCESS
)
3209 r
= tv
->view
.ops
->execute( &tv
->view
, NULL
);
3210 if( r
!= ERROR_SUCCESS
)
3213 TRACE("name = %s columns = %u row_size = %u raw size = %u\n",
3214 debugstr_w(name
), tv
->num_cols
, tv
->row_size
, rawsize
);
3216 /* interpret the data */
3217 for (n
= 0; n
< rawsize
;)
3219 mask
= rawdata
[n
] | (rawdata
[n
+ 1] << 8);
3223 * if the low bit is set, columns are continuous and
3224 * the number of columns is specified in the high byte
3227 num_cols
= mask
>> 8;
3228 if (num_cols
> tv
->num_cols
)
3230 ERR("excess columns in transform: %u > %u\n", num_cols
, tv
->num_cols
);
3234 for (i
= 0; i
< num_cols
; i
++)
3236 if( (tv
->columns
[i
].type
& MSITYPE_STRING
) &&
3237 ! MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
3238 sz
+= bytes_per_strref
;
3240 sz
+= bytes_per_column( tv
->db
, &tv
->columns
[i
], bytes_per_strref
);
3246 * If the low bit is not set, mask is a bitmask.
3247 * Excepting for key fields, which are always present,
3248 * each bit indicates that a field is present in the transform record.
3250 * mask == 0 is a special case ... only the keys will be present
3251 * and it means that this row should be deleted.
3254 num_cols
= tv
->num_cols
;
3255 for (i
= 0; i
< num_cols
; i
++)
3257 if ((tv
->columns
[i
].type
& MSITYPE_KEY
) || ((1 << i
) & mask
))
3259 if ((tv
->columns
[i
].type
& MSITYPE_STRING
) &&
3260 !MSITYPE_IS_BINARY(tv
->columns
[i
].type
))
3261 sz
+= bytes_per_strref
;
3263 sz
+= bytes_per_column( tv
->db
, &tv
->columns
[i
], bytes_per_strref
);
3268 /* check we didn't run of the end of the table */
3269 if (n
+ sz
> rawsize
)
3272 dump_table( st
, (USHORT
*)rawdata
, rawsize
);
3276 rec
= get_transform_record( tv
, st
, stg
, &rawdata
[n
], bytes_per_strref
);
3281 UINT number
= MSI_NULL_INTEGER
;
3284 if (!wcscmp( name
, L
"_Columns" ))
3286 MSI_RecordGetStringW( rec
, 1, table
, &sz
);
3287 number
= MSI_RecordGetInteger( rec
, 2 );
3290 * Native msi seems writes nul into the Number (2nd) column of
3291 * the _Columns table when there are new columns
3293 if ( number
== MSI_NULL_INTEGER
)
3295 /* reset the column number on a new table */
3296 if (wcscmp( coltable
, table
))
3299 lstrcpyW( coltable
, table
);
3302 /* fix nul column numbers */
3303 MSI_RecordSetInteger( rec
, 2, ++colcol
);
3307 if (TRACE_ON(msidb
)) dump_record( rec
);
3310 r
= table_find_row( tv
, rec
, &row
, NULL
);
3312 r
= ERROR_FUNCTION_FAILED
;
3313 if (r
== ERROR_SUCCESS
)
3317 TRACE("deleting row [%d]:\n", row
);
3318 r
= tv
->view
.ops
->delete_row( &tv
->view
, row
);
3319 if (r
!= ERROR_SUCCESS
)
3320 WARN("failed to delete row %u\n", r
);
3324 TRACE("modifying full row [%d]:\n", row
);
3325 r
= tv
->view
.ops
->set_row( &tv
->view
, row
, rec
, (1 << tv
->num_cols
) - 1 );
3326 if (r
!= ERROR_SUCCESS
)
3327 WARN("failed to modify row %u\n", r
);
3331 TRACE("modifying masked row [%d]:\n", row
);
3332 r
= tv
->view
.ops
->set_row( &tv
->view
, row
, rec
, mask
);
3333 if (r
!= ERROR_SUCCESS
)
3334 WARN("failed to modify row %u\n", r
);
3339 TRACE("inserting row\n");
3340 r
= tv
->view
.ops
->insert_row( &tv
->view
, rec
, -1, FALSE
);
3341 if (r
!= ERROR_SUCCESS
)
3342 WARN("failed to insert row %u\n", r
);
3345 if (!(err_cond
& MSITRANSFORM_ERROR_VIEWTRANSFORM
) && !wcscmp( name
, L
"_Columns" ))
3346 update_table_columns( db
, table
);
3348 msiobj_release( &rec
->hdr
);
3355 /* no need to free the table, it's associated with the database */
3358 tv
->view
.ops
->delete( &tv
->view
);
3360 return ERROR_SUCCESS
;
3364 * msi_table_apply_transform
3366 * Enumerate the table transforms in a transform storage and apply each one.
3368 UINT
msi_table_apply_transform( MSIDATABASE
*db
, IStorage
*stg
, int err_cond
)
3370 struct list transforms
;
3371 IEnumSTATSTG
*stgenum
= NULL
;
3372 struct transform_data
*transform
;
3373 struct transform_data
*tables
= NULL
, *columns
= NULL
;
3376 string_table
*strings
;
3377 UINT ret
= ERROR_FUNCTION_FAILED
;
3378 UINT bytes_per_strref
;
3379 BOOL property_update
= FALSE
;
3380 MSIVIEW
*transform_view
= NULL
;
3382 TRACE("%p %p\n", db
, stg
);
3384 strings
= msi_load_string_table( stg
, &bytes_per_strref
);
3388 hr
= IStorage_EnumElements( stg
, 0, NULL
, 0, &stgenum
);
3392 list_init(&transforms
);
3396 struct table_view
*tv
= NULL
;
3400 hr
= IEnumSTATSTG_Next( stgenum
, 1, &stat
, &count
);
3401 if (FAILED( hr
) || !count
)
3404 decode_streamname( stat
.pwcsName
, name
);
3405 CoTaskMemFree( stat
.pwcsName
);
3406 if ( name
[0] != 0x4840 )
3409 if ( !wcscmp( name
+1, L
"_StringPool" ) ||
3410 !wcscmp( name
+1, L
"_StringData" ) )
3413 transform
= calloc( 1, sizeof(*transform
) );
3417 list_add_tail( &transforms
, &transform
->entry
);
3419 transform
->name
= wcsdup( name
+ 1 );
3421 if ( !wcscmp( transform
->name
, L
"_Tables" ) )
3423 else if (!wcscmp( transform
->name
, L
"_Columns" ) )
3424 columns
= transform
;
3425 else if (!wcscmp( transform
->name
, L
"Property" ))
3426 property_update
= TRUE
;
3428 TRACE("transform contains stream %s\n", debugstr_w(name
));
3430 /* load the table */
3431 if (TABLE_CreateView( db
, transform
->name
, (MSIVIEW
**) &tv
) != ERROR_SUCCESS
)
3434 if (tv
->view
.ops
->execute( &tv
->view
, NULL
) != ERROR_SUCCESS
)
3436 tv
->view
.ops
->delete( &tv
->view
);
3440 tv
->view
.ops
->delete( &tv
->view
);
3443 if (err_cond
& MSITRANSFORM_ERROR_VIEWTRANSFORM
)
3445 static const WCHAR create_query
[] = L
"CREATE TABLE `_TransformView` ( "
3446 L
"`Table` CHAR(0) NOT NULL TEMPORARY, `Column` CHAR(0) NOT NULL TEMPORARY, "
3447 L
"`Row` CHAR(0) TEMPORARY, `Data` CHAR(0) TEMPORARY, `Current` CHAR(0) TEMPORARY "
3448 L
"PRIMARY KEY `Table`, `Column`, `Row` ) HOLD";
3453 r
= MSI_DatabaseOpenViewW( db
, create_query
, &query
);
3454 if (r
!= ERROR_SUCCESS
)
3457 r
= MSI_ViewExecute( query
, NULL
);
3458 if (r
== ERROR_SUCCESS
)
3459 MSI_ViewClose( query
);
3460 msiobj_release( &query
->hdr
);
3461 if (r
!= ERROR_BAD_QUERY_SYNTAX
&& r
!= ERROR_SUCCESS
)
3464 if (TABLE_CreateView(db
, L
"_TransformView", &transform_view
) != ERROR_SUCCESS
)
3467 if (r
== ERROR_BAD_QUERY_SYNTAX
)
3468 transform_view
->ops
->add_ref( transform_view
);
3470 r
= transform_view
->ops
->add_column( transform_view
, L
"new",
3471 MSITYPE_TEMPORARY
| MSITYPE_NULLABLE
| 0x402 /* INT */, FALSE
);
3472 if (r
!= ERROR_SUCCESS
)
3477 * Apply _Tables and _Columns transforms first so that
3478 * the table metadata is correct, and empty tables exist.
3480 ret
= table_load_transform( db
, stg
, strings
, tables
, bytes_per_strref
, err_cond
);
3481 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_INVALID_TABLE
)
3484 ret
= table_load_transform( db
, stg
, strings
, columns
, bytes_per_strref
, err_cond
);
3485 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_INVALID_TABLE
)
3488 ret
= ERROR_SUCCESS
;
3490 while ( !list_empty( &transforms
) )
3492 transform
= LIST_ENTRY( list_head( &transforms
), struct transform_data
, entry
);
3494 if ( wcscmp( transform
->name
, L
"_Columns" ) &&
3495 wcscmp( transform
->name
, L
"_Tables" ) &&
3496 ret
== ERROR_SUCCESS
)
3498 ret
= table_load_transform( db
, stg
, strings
, transform
, bytes_per_strref
, err_cond
);
3501 list_remove( &transform
->entry
);
3502 free( transform
->name
);
3506 if ( ret
== ERROR_SUCCESS
)
3508 append_storage_to_db( db
, stg
);
3509 if (property_update
) msi_clone_properties( db
);
3514 IEnumSTATSTG_Release( stgenum
);
3516 msi_destroy_stringtable( strings
);
3519 struct tagMSITABLE
*table
= ((struct table_view
*)transform_view
)->table
;
3521 if (ret
!= ERROR_SUCCESS
)
3522 transform_view
->ops
->release( transform_view
);
3524 if (!wcscmp(table
->colinfo
[table
->col_count
- 1].colname
, L
"new"))
3525 TABLE_remove_column( transform_view
, table
->colinfo
[table
->col_count
- 1].number
);
3526 transform_view
->ops
->delete( transform_view
);