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
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
39 #include "wine/debug.h"
40 #include "wine/unicode.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(msidb
);
44 #define MSITABLE_HASH_TABLE_SIZE 37
46 typedef struct tagMSICOLUMNHASHENTRY
48 struct tagMSICOLUMNHASHENTRY
*next
;
53 typedef struct tagMSICOLUMNINFO
62 MSICOLUMNHASHENTRY
**hash_table
;
65 typedef struct tagMSIORDERINFO
75 BOOL
*data_persistent
;
78 MSICOLUMNINFO
*colinfo
;
80 MSICONDITION persistent
;
85 static const WCHAR szStringData
[] = {
86 '_','S','t','r','i','n','g','D','a','t','a',0 };
87 static const WCHAR szStringPool
[] = {
88 '_','S','t','r','i','n','g','P','o','o','l',0 };
90 /* information for default tables */
91 static WCHAR szTables
[] = { '_','T','a','b','l','e','s',0 };
92 static WCHAR szTable
[] = { 'T','a','b','l','e',0 };
93 static WCHAR szName
[] = { 'N','a','m','e',0 };
94 static WCHAR szColumns
[] = { '_','C','o','l','u','m','n','s',0 };
95 static WCHAR szNumber
[] = { 'N','u','m','b','e','r',0 };
96 static WCHAR szType
[] = { 'T','y','p','e',0 };
98 static const MSICOLUMNINFO _Columns_cols
[4] = {
99 { szColumns
, 1, szTable
, MSITYPE_VALID
| MSITYPE_STRING
| MSITYPE_KEY
| 64, 0, 0, 0, NULL
},
100 { szColumns
, 2, szNumber
, MSITYPE_VALID
| MSITYPE_KEY
| 2, 2, 0, 0, NULL
},
101 { szColumns
, 3, szName
, MSITYPE_VALID
| MSITYPE_STRING
| 64, 4, 0, 0, NULL
},
102 { szColumns
, 4, szType
, MSITYPE_VALID
| 2, 6, 0, 0, NULL
},
105 static const MSICOLUMNINFO _Tables_cols
[1] = {
106 { szTables
, 1, szName
, MSITYPE_VALID
| MSITYPE_STRING
| MSITYPE_KEY
| 64, 0, 0, 0, NULL
},
109 #define MAX_STREAM_NAME 0x1f
111 static UINT
table_get_column_info( MSIDATABASE
*db
, LPCWSTR name
,
112 MSICOLUMNINFO
**pcols
, UINT
*pcount
);
113 static void table_calc_column_offsets( MSIDATABASE
*db
, MSICOLUMNINFO
*colinfo
,
115 static UINT
get_tablecolumns( MSIDATABASE
*db
,
116 LPCWSTR szTableName
, MSICOLUMNINFO
*colinfo
, UINT
*sz
);
117 static void msi_free_colinfo( MSICOLUMNINFO
*colinfo
, UINT count
);
119 static inline UINT
bytes_per_column( MSIDATABASE
*db
, const MSICOLUMNINFO
*col
, UINT bytes_per_strref
)
121 if( MSITYPE_IS_BINARY(col
->type
) )
124 if( col
->type
& MSITYPE_STRING
)
125 return bytes_per_strref
;
127 if( (col
->type
& 0xff) <= 2)
130 if( (col
->type
& 0xff) != 4 )
131 ERR("Invalid column size!\n");
136 static int utf2mime(int x
)
138 if( (x
>='0') && (x
<='9') )
140 if( (x
>='A') && (x
<='Z') )
142 if( (x
>='a') && (x
<='z') )
151 LPWSTR
encode_streamname(BOOL bTable
, LPCWSTR in
)
153 DWORD count
= MAX_STREAM_NAME
;
158 count
= lstrlenW( in
)+2;
159 out
= msi_alloc( count
*sizeof(WCHAR
) );
175 if( ( ch
< 0x80 ) && ( utf2mime(ch
) >= 0 ) )
177 ch
= utf2mime(ch
) + 0x4800;
179 if( next
&& (next
<0x80) )
181 next
= utf2mime(next
);
192 ERR("Failed to encode stream name (%s)\n",debugstr_w(in
));
197 static int mime2utf(int x
)
204 return x
- 10 - 26 + 'a';
205 if( x
== (10+26+26) )
210 BOOL
decode_streamname(LPCWSTR in
, LPWSTR out
)
215 while ( (ch
= *in
++) )
217 if( (ch
>= 0x3800 ) && (ch
< 0x4840 ) )
220 ch
= mime2utf(ch
-0x4800);
224 *out
++ = mime2utf(ch
&0x3f);
226 ch
= mime2utf((ch
>>6)&0x3f);
236 void enum_stream_names( IStorage
*stg
)
238 IEnumSTATSTG
*stgenum
= NULL
;
244 r
= IStorage_EnumElements( stg
, 0, NULL
, 0, &stgenum
);
252 r
= IEnumSTATSTG_Next( stgenum
, 1, &stat
, &count
);
253 if( FAILED( r
) || !count
)
255 decode_streamname( stat
.pwcsName
, name
);
256 TRACE("stream %2d -> %s %s\n", n
,
257 debugstr_w(stat
.pwcsName
), debugstr_w(name
) );
258 CoTaskMemFree( stat
.pwcsName
);
262 IEnumSTATSTG_Release( stgenum
);
265 UINT
read_stream_data( IStorage
*stg
, LPCWSTR stname
, BOOL table
,
266 BYTE
**pdata
, UINT
*psz
)
269 UINT ret
= ERROR_FUNCTION_FAILED
;
276 encname
= encode_streamname(table
, stname
);
278 TRACE("%s -> %s\n",debugstr_w(stname
),debugstr_w(encname
));
280 r
= IStorage_OpenStream(stg
, encname
, NULL
,
281 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, &stm
);
285 WARN("open stream failed r = %08x - empty table?\n", r
);
289 r
= IStream_Stat(stm
, &stat
, STATFLAG_NONAME
);
292 WARN("open stream failed r = %08x!\n", r
);
296 if( stat
.cbSize
.QuadPart
>> 32 )
302 sz
= stat
.cbSize
.QuadPart
;
303 data
= msi_alloc( sz
);
306 WARN("couldn't allocate memory r=%08x!\n", r
);
307 ret
= ERROR_NOT_ENOUGH_MEMORY
;
311 r
= IStream_Read(stm
, data
, sz
, &count
);
312 if( FAILED( r
) || ( count
!= sz
) )
315 WARN("read stream failed r = %08x!\n", r
);
324 IStream_Release( stm
);
329 UINT
write_stream_data( IStorage
*stg
, LPCWSTR stname
,
330 LPCVOID data
, UINT sz
, BOOL bTable
)
333 UINT ret
= ERROR_FUNCTION_FAILED
;
340 encname
= encode_streamname(bTable
, stname
);
341 r
= IStorage_OpenStream( stg
, encname
, NULL
,
342 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, &stm
);
345 r
= IStorage_CreateStream( stg
, encname
,
346 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, 0, &stm
);
351 WARN("open stream failed r = %08x\n", r
);
356 r
= IStream_SetSize( stm
, size
);
359 WARN("Failed to SetSize\n");
364 r
= IStream_Seek( stm
, pos
, STREAM_SEEK_SET
, NULL
);
367 WARN("Failed to Seek\n");
373 r
= IStream_Write(stm
, data
, sz
, &count
);
374 if( FAILED( r
) || ( count
!= sz
) )
376 WARN("Failed to Write\n");
384 IStream_Release( stm
);
389 static void free_table( MSITABLE
*table
)
392 for( i
=0; i
<table
->row_count
; i
++ )
393 msi_free( table
->data
[i
] );
394 msi_free( table
->data
);
395 msi_free( table
->data_persistent
);
396 msi_free_colinfo( table
->colinfo
, table
->col_count
);
397 msi_free( table
->colinfo
);
401 static UINT
msi_table_get_row_size( MSIDATABASE
*db
, const MSICOLUMNINFO
*cols
, UINT count
, UINT bytes_per_strref
)
403 const MSICOLUMNINFO
*last_col
;
408 if (bytes_per_strref
!= LONG_STR_BYTES
)
411 for (i
= 0; i
< count
; i
++) size
+= bytes_per_column( db
, &cols
[i
], bytes_per_strref
);
414 last_col
= &cols
[count
- 1];
415 return last_col
->offset
+ bytes_per_column( db
, last_col
, bytes_per_strref
);
418 /* add this table to the list of cached tables in the database */
419 static UINT
read_table_from_storage( MSIDATABASE
*db
, MSITABLE
*t
, IStorage
*stg
)
421 BYTE
*rawdata
= NULL
;
422 UINT rawsize
= 0, i
, j
, row_size
, row_size_mem
;
424 TRACE("%s\n",debugstr_w(t
->name
));
426 row_size
= msi_table_get_row_size( db
, t
->colinfo
, t
->col_count
, db
->bytes_per_strref
);
427 row_size_mem
= msi_table_get_row_size( db
, t
->colinfo
, t
->col_count
, LONG_STR_BYTES
);
429 /* if we can't read the table, just assume that it's empty */
430 read_stream_data( stg
, t
->name
, TRUE
, &rawdata
, &rawsize
);
432 return ERROR_SUCCESS
;
434 TRACE("Read %d bytes\n", rawsize
);
436 if( rawsize
% row_size
)
438 WARN("Table size is invalid %d/%d\n", rawsize
, row_size
);
442 t
->row_count
= rawsize
/ row_size
;
443 t
->data
= msi_alloc_zero( t
->row_count
* sizeof (USHORT
*) );
446 t
->data_persistent
= msi_alloc_zero( t
->row_count
* sizeof(BOOL
));
447 if ( !t
->data_persistent
)
450 /* transpose all the data */
451 TRACE("Transposing data from %d rows\n", t
->row_count
);
452 for (i
= 0; i
< t
->row_count
; i
++)
454 UINT ofs
= 0, ofs_mem
= 0;
456 t
->data
[i
] = msi_alloc( row_size_mem
);
459 t
->data_persistent
[i
] = TRUE
;
461 for (j
= 0; j
< t
->col_count
; j
++)
463 UINT m
= bytes_per_column( db
, &t
->colinfo
[j
], LONG_STR_BYTES
);
464 UINT n
= bytes_per_column( db
, &t
->colinfo
[j
], db
->bytes_per_strref
);
467 if ( n
!= 2 && n
!= 3 && n
!= 4 )
469 ERR("oops - unknown column width %d\n", n
);
472 if (t
->colinfo
[j
].type
& MSITYPE_STRING
&& n
< m
)
474 for (k
= 0; k
< m
; k
++)
477 t
->data
[i
][ofs_mem
+ k
] = rawdata
[ofs
* t
->row_count
+ i
* n
+ k
];
479 t
->data
[i
][ofs_mem
+ k
] = 0;
484 for (k
= 0; k
< n
; k
++)
485 t
->data
[i
][ofs_mem
+ k
] = rawdata
[ofs
* t
->row_count
+ i
* n
+ k
];
493 return ERROR_SUCCESS
;
496 return ERROR_FUNCTION_FAILED
;
499 void free_cached_tables( MSIDATABASE
*db
)
501 while( !list_empty( &db
->tables
) )
503 MSITABLE
*t
= LIST_ENTRY( list_head( &db
->tables
), MSITABLE
, entry
);
505 list_remove( &t
->entry
);
510 static MSITABLE
*find_cached_table( MSIDATABASE
*db
, LPCWSTR name
)
514 LIST_FOR_EACH_ENTRY( t
, &db
->tables
, MSITABLE
, entry
)
515 if( !lstrcmpW( name
, t
->name
) )
521 static UINT
table_get_column_info( MSIDATABASE
*db
, LPCWSTR name
, MSICOLUMNINFO
**pcols
, UINT
*pcount
)
523 UINT r
, column_count
= 0;
524 MSICOLUMNINFO
*columns
;
526 /* get the number of columns in this table */
528 r
= get_tablecolumns( db
, name
, NULL
, &column_count
);
529 if( r
!= ERROR_SUCCESS
)
532 *pcount
= column_count
;
534 /* if there's no columns, there's no table */
535 if( column_count
== 0 )
536 return ERROR_INVALID_PARAMETER
;
538 TRACE("Table %s found\n", debugstr_w(name
) );
540 columns
= msi_alloc( column_count
*sizeof (MSICOLUMNINFO
) );
542 return ERROR_FUNCTION_FAILED
;
544 r
= get_tablecolumns( db
, name
, columns
, &column_count
);
545 if( r
!= ERROR_SUCCESS
)
548 return ERROR_FUNCTION_FAILED
;
556 UINT
msi_create_table( MSIDATABASE
*db
, LPCWSTR name
, column_info
*col_info
,
557 MSICONDITION persistent
, MSITABLE
**table_ret
)
561 MSIRECORD
*rec
= NULL
;
566 /* only add tables that don't exist already */
567 if( TABLE_Exists(db
, name
) )
569 WARN("table %s exists\n", debugstr_w(name
));
570 return ERROR_BAD_QUERY_SYNTAX
;
573 table
= msi_alloc( sizeof (MSITABLE
) + lstrlenW(name
)*sizeof (WCHAR
) );
575 return ERROR_FUNCTION_FAILED
;
577 table
->ref_count
= 1;
578 table
->row_count
= 0;
580 table
->data_persistent
= NULL
;
581 table
->colinfo
= NULL
;
582 table
->col_count
= 0;
583 table
->persistent
= persistent
;
584 lstrcpyW( table
->name
, name
);
586 for( col
= col_info
; col
; col
= col
->next
)
589 table
->colinfo
= msi_alloc( table
->col_count
* sizeof(MSICOLUMNINFO
) );
593 return ERROR_FUNCTION_FAILED
;
596 for( i
= 0, col
= col_info
; col
; i
++, col
= col
->next
)
598 table
->colinfo
[ i
].tablename
= strdupW( col
->table
);
599 table
->colinfo
[ i
].number
= i
+ 1;
600 table
->colinfo
[ i
].colname
= strdupW( col
->column
);
601 table
->colinfo
[ i
].type
= col
->type
;
602 table
->colinfo
[ i
].offset
= 0;
603 table
->colinfo
[ i
].ref_count
= 0;
604 table
->colinfo
[ i
].hash_table
= NULL
;
605 table
->colinfo
[ i
].temporary
= col
->temporary
;
607 table_calc_column_offsets( db
, table
->colinfo
, table
->col_count
);
609 r
= TABLE_CreateView( db
, szTables
, &tv
);
610 TRACE("CreateView returned %x\n", r
);
617 r
= tv
->ops
->execute( tv
, 0 );
618 TRACE("tv execute returned %x\n", r
);
622 rec
= MSI_CreateRecord( 1 );
626 r
= MSI_RecordSetStringW( rec
, 1, name
);
630 r
= tv
->ops
->insert_row( tv
, rec
, -1, persistent
== MSICONDITION_FALSE
);
631 TRACE("insert_row returned %x\n", r
);
635 tv
->ops
->delete( tv
);
638 msiobj_release( &rec
->hdr
);
641 if( persistent
!= MSICONDITION_FALSE
)
643 /* add each column to the _Columns table */
644 r
= TABLE_CreateView( db
, szColumns
, &tv
);
648 r
= tv
->ops
->execute( tv
, 0 );
649 TRACE("tv execute returned %x\n", r
);
653 rec
= MSI_CreateRecord( 4 );
657 r
= MSI_RecordSetStringW( rec
, 1, name
);
662 * need to set the table, column number, col name and type
663 * for each column we enter in the table
666 for( col
= col_info
; col
; col
= col
->next
)
668 r
= MSI_RecordSetInteger( rec
, 2, nField
);
672 r
= MSI_RecordSetStringW( rec
, 3, col
->column
);
676 r
= MSI_RecordSetInteger( rec
, 4, col
->type
);
680 r
= tv
->ops
->insert_row( tv
, rec
, -1, FALSE
);
692 msiobj_release( &rec
->hdr
);
693 /* FIXME: remove values from the string table on error */
695 tv
->ops
->delete( tv
);
697 if (r
== ERROR_SUCCESS
)
699 list_add_head( &db
->tables
, &table
->entry
);
708 static UINT
get_table( MSIDATABASE
*db
, LPCWSTR name
, MSITABLE
**table_ret
)
713 /* first, see if the table is cached */
714 table
= find_cached_table( db
, name
);
718 return ERROR_SUCCESS
;
721 /* nonexistent tables should be interpreted as empty tables */
722 table
= msi_alloc( sizeof (MSITABLE
) + lstrlenW(name
)*sizeof (WCHAR
) );
724 return ERROR_FUNCTION_FAILED
;
726 table
->row_count
= 0;
728 table
->data_persistent
= NULL
;
729 table
->colinfo
= NULL
;
730 table
->col_count
= 0;
731 table
->persistent
= MSICONDITION_TRUE
;
732 lstrcpyW( table
->name
, name
);
734 if ( !lstrcmpW(name
, szTables
) || !lstrcmpW(name
, szColumns
) )
735 table
->persistent
= MSICONDITION_NONE
;
737 r
= table_get_column_info( db
, name
, &table
->colinfo
, &table
->col_count
);
738 if (r
!= ERROR_SUCCESS
)
740 free_table ( table
);
744 r
= read_table_from_storage( db
, table
, db
->storage
);
745 if( r
!= ERROR_SUCCESS
)
751 list_add_head( &db
->tables
, &table
->entry
);
753 return ERROR_SUCCESS
;
756 static UINT
read_table_int(BYTE
*const *data
, UINT row
, UINT col
, UINT bytes
)
760 for (i
= 0; i
< bytes
; i
++)
761 ret
+= data
[row
][col
+ i
] << i
* 8;
766 static UINT
save_table( MSIDATABASE
*db
, const MSITABLE
*t
, UINT bytes_per_strref
)
768 BYTE
*rawdata
= NULL
;
769 UINT rawsize
, r
, i
, j
, row_size
, row_count
;
771 /* Nothing to do for non-persistent tables */
772 if( t
->persistent
== MSICONDITION_FALSE
)
773 return ERROR_SUCCESS
;
775 TRACE("Saving %s\n", debugstr_w( t
->name
) );
777 row_size
= msi_table_get_row_size( db
, t
->colinfo
, t
->col_count
, bytes_per_strref
);
778 row_count
= t
->row_count
;
779 for (i
= 0; i
< t
->row_count
; i
++)
781 if (!t
->data_persistent
[i
])
783 row_count
= 1; /* yes, this is bizarre */
787 rawsize
= row_count
* row_size
;
788 rawdata
= msi_alloc_zero( rawsize
);
791 r
= ERROR_NOT_ENOUGH_MEMORY
;
796 for (i
= 0; i
< t
->row_count
; i
++)
798 UINT ofs
= 0, ofs_mem
= 0;
800 if (!t
->data_persistent
[i
]) break;
802 for (j
= 0; j
< t
->col_count
; j
++)
804 UINT m
= bytes_per_column( db
, &t
->colinfo
[j
], LONG_STR_BYTES
);
805 UINT n
= bytes_per_column( db
, &t
->colinfo
[j
], bytes_per_strref
);
808 if (n
!= 2 && n
!= 3 && n
!= 4)
810 ERR("oops - unknown column width %d\n", n
);
813 if (t
->colinfo
[j
].type
& MSITYPE_STRING
&& n
< m
)
815 UINT id
= read_table_int( t
->data
, i
, ofs_mem
, LONG_STR_BYTES
);
816 if (id
> 1 << bytes_per_strref
* 8)
818 ERR("string id %u out of range\n", id
);
819 r
= ERROR_FUNCTION_FAILED
;
823 for (k
= 0; k
< n
; k
++)
825 rawdata
[ofs
* row_count
+ i
* n
+ k
] = t
->data
[i
][ofs_mem
+ k
];
833 TRACE("writing %d bytes\n", rawsize
);
834 r
= write_stream_data( db
->storage
, t
->name
, rawdata
, rawsize
, TRUE
);
841 static void table_calc_column_offsets( MSIDATABASE
*db
, MSICOLUMNINFO
*colinfo
, DWORD count
)
845 for( i
=0; colinfo
&& (i
<count
); i
++ )
847 assert( (i
+1) == colinfo
[ i
].number
);
849 colinfo
[i
].offset
= colinfo
[ i
- 1 ].offset
850 + bytes_per_column( db
, &colinfo
[ i
- 1 ], LONG_STR_BYTES
);
852 colinfo
[i
].offset
= 0;
853 TRACE("column %d is [%s] with type %08x ofs %d\n",
854 colinfo
[i
].number
, debugstr_w(colinfo
[i
].colname
),
855 colinfo
[i
].type
, colinfo
[i
].offset
);
859 static UINT
get_defaulttablecolumns( MSIDATABASE
*db
, LPCWSTR name
,
860 MSICOLUMNINFO
*colinfo
, UINT
*sz
)
862 const MSICOLUMNINFO
*p
;
865 TRACE("%s\n", debugstr_w(name
));
867 if (!lstrcmpW( name
, szTables
))
872 else if (!lstrcmpW( name
, szColumns
))
878 return ERROR_FUNCTION_FAILED
;
880 /* duplicate the string data so we can free it in msi_free_colinfo */
883 if (colinfo
&& (i
< *sz
) )
886 colinfo
[i
].tablename
= strdupW( p
[i
].tablename
);
887 colinfo
[i
].colname
= strdupW( p
[i
].colname
);
889 if( colinfo
&& (i
>= *sz
) )
892 table_calc_column_offsets( db
, colinfo
, n
);
894 return ERROR_SUCCESS
;
897 static void msi_free_colinfo( MSICOLUMNINFO
*colinfo
, UINT count
)
901 for( i
=0; i
<count
; i
++ )
903 msi_free( colinfo
[i
].tablename
);
904 msi_free( colinfo
[i
].colname
);
905 msi_free( colinfo
[i
].hash_table
);
909 static LPWSTR
msi_makestring( const MSIDATABASE
*db
, UINT stringid
)
911 return strdupW(msi_string_lookup_id( db
->strings
, stringid
));
914 static UINT
get_tablecolumns( MSIDATABASE
*db
,
915 LPCWSTR szTableName
, MSICOLUMNINFO
*colinfo
, UINT
*sz
)
917 UINT r
, i
, n
=0, table_id
, count
, maxcount
= *sz
;
918 MSITABLE
*table
= NULL
;
920 TRACE("%s\n", debugstr_w(szTableName
));
922 /* first check if there is a default table with that name */
923 r
= get_defaulttablecolumns( db
, szTableName
, colinfo
, sz
);
924 if( ( r
== ERROR_SUCCESS
) && *sz
)
927 r
= get_table( db
, szColumns
, &table
);
928 if( r
!= ERROR_SUCCESS
)
930 ERR("couldn't load _Columns table\n");
931 return ERROR_FUNCTION_FAILED
;
934 /* convert table and column names to IDs from the string table */
935 r
= msi_string2idW( db
->strings
, szTableName
, &table_id
);
936 if( r
!= ERROR_SUCCESS
)
938 WARN("Couldn't find id for %s\n", debugstr_w(szTableName
));
942 TRACE("Table id is %d, row count is %d\n", table_id
, table
->row_count
);
944 /* Note: _Columns table doesn't have non-persistent data */
946 /* if maxcount is non-zero, assume it's exactly right for this table */
947 memset( colinfo
, 0, maxcount
*sizeof(*colinfo
) );
948 count
= table
->row_count
;
949 for( i
=0; i
<count
; i
++ )
951 if( read_table_int(table
->data
, i
, 0, LONG_STR_BYTES
) != table_id
)
955 UINT id
= read_table_int(table
->data
, i
, table
->colinfo
[2].offset
, LONG_STR_BYTES
);
956 UINT col
= read_table_int(table
->data
, i
, table
->colinfo
[1].offset
, sizeof(USHORT
)) - (1<<15);
958 /* check the column number is in range */
959 if (col
<1 || col
>maxcount
)
961 ERR("column %d out of range\n", col
);
965 /* check if this column was already set */
966 if (colinfo
[ col
- 1 ].number
)
968 ERR("duplicate column %d\n", col
);
972 colinfo
[ col
- 1 ].tablename
= msi_makestring( db
, table_id
);
973 colinfo
[ col
- 1 ].number
= col
;
974 colinfo
[ col
- 1 ].colname
= msi_makestring( db
, id
);
975 colinfo
[ col
- 1 ].type
= read_table_int(table
->data
, i
,
976 table
->colinfo
[3].offset
,
977 sizeof(USHORT
)) - (1<<15);
978 colinfo
[ col
- 1 ].offset
= 0;
979 colinfo
[ col
- 1 ].ref_count
= 0;
980 colinfo
[ col
- 1 ].hash_table
= NULL
;
985 TRACE("%s has %d columns\n", debugstr_w(szTableName
), n
);
987 if (colinfo
&& n
!= maxcount
)
989 ERR("missing column in table %s\n", debugstr_w(szTableName
));
990 msi_free_colinfo(colinfo
, maxcount
);
991 return ERROR_FUNCTION_FAILED
;
994 table_calc_column_offsets( db
, colinfo
, n
);
997 return ERROR_SUCCESS
;
1000 static void msi_update_table_columns( MSIDATABASE
*db
, LPCWSTR name
)
1004 UINT size
, offset
, old_count
;
1007 /* We may free name in msi_free_colinfo. */
1008 tablename
= strdupW( name
);
1010 table
= find_cached_table( db
, tablename
);
1011 old_count
= table
->col_count
;
1012 msi_free_colinfo( table
->colinfo
, table
->col_count
);
1013 msi_free( table
->colinfo
);
1014 table
->colinfo
= NULL
;
1016 table_get_column_info( db
, tablename
, &table
->colinfo
, &table
->col_count
);
1017 if (!table
->col_count
)
1020 size
= msi_table_get_row_size( db
, table
->colinfo
, table
->col_count
, LONG_STR_BYTES
);
1021 offset
= table
->colinfo
[table
->col_count
- 1].offset
;
1023 for ( n
= 0; n
< table
->row_count
; n
++ )
1025 table
->data
[n
] = msi_realloc( table
->data
[n
], size
);
1026 if (old_count
< table
->col_count
)
1027 memset( &table
->data
[n
][offset
], 0, size
- offset
);
1031 msi_free(tablename
);
1034 /* try to find the table name in the _Tables table */
1035 BOOL
TABLE_Exists( MSIDATABASE
*db
, LPCWSTR name
)
1037 UINT r
, table_id
, i
;
1040 static const WCHAR szStreams
[] = {'_','S','t','r','e','a','m','s',0};
1041 static const WCHAR szStorages
[] = {'_','S','t','o','r','a','g','e','s',0};
1043 if( !lstrcmpW( name
, szTables
) || !lstrcmpW( name
, szColumns
) ||
1044 !lstrcmpW( name
, szStreams
) || !lstrcmpW( name
, szStorages
) )
1047 r
= msi_string2idW( db
->strings
, name
, &table_id
);
1048 if( r
!= ERROR_SUCCESS
)
1050 TRACE("Couldn't find id for %s\n", debugstr_w(name
));
1054 r
= get_table( db
, szTables
, &table
);
1055 if( r
!= ERROR_SUCCESS
)
1057 ERR("table %s not available\n", debugstr_w(szTables
));
1061 for( i
= 0; i
< table
->row_count
; i
++ )
1063 if( read_table_int( table
->data
, i
, 0, LONG_STR_BYTES
) == table_id
)
1070 /* below is the query interface to a table */
1072 typedef struct tagMSITABLEVIEW
1077 MSICOLUMNINFO
*columns
;
1078 MSIORDERINFO
*order
;
1084 static UINT
TABLE_fetch_int( struct tagMSIVIEW
*view
, UINT row
, UINT col
, UINT
*val
)
1086 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1090 return ERROR_INVALID_PARAMETER
;
1092 if( (col
==0) || (col
>tv
->num_cols
) )
1093 return ERROR_INVALID_PARAMETER
;
1095 /* how many rows are there ? */
1096 if( row
>= tv
->table
->row_count
)
1097 return ERROR_NO_MORE_ITEMS
;
1099 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1101 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1102 ERR("%p %p\n", tv
, tv
->columns
);
1103 return ERROR_FUNCTION_FAILED
;
1107 row
= tv
->order
->reorder
[row
];
1109 n
= bytes_per_column( tv
->db
, &tv
->columns
[col
- 1], LONG_STR_BYTES
);
1110 if (n
!= 2 && n
!= 3 && n
!= 4)
1112 ERR("oops! what is %d bytes per column?\n", n
);
1113 return ERROR_FUNCTION_FAILED
;
1116 offset
= tv
->columns
[col
-1].offset
;
1117 *val
= read_table_int(tv
->table
->data
, row
, offset
, n
);
1119 /* TRACE("Data [%d][%d] = %d\n", row, col, *val ); */
1121 return ERROR_SUCCESS
;
1124 static UINT
msi_stream_name( const MSITABLEVIEW
*tv
, UINT row
, LPWSTR
*pstname
)
1126 LPWSTR p
, stname
= NULL
;
1127 UINT i
, r
, type
, ival
;
1130 MSIVIEW
*view
= (MSIVIEW
*) tv
;
1132 TRACE("%p %d\n", tv
, row
);
1134 len
= lstrlenW( tv
->name
) + 1;
1135 stname
= msi_alloc( len
*sizeof(WCHAR
) );
1138 r
= ERROR_OUTOFMEMORY
;
1142 lstrcpyW( stname
, tv
->name
);
1144 for ( i
= 0; i
< tv
->num_cols
; i
++ )
1146 type
= tv
->columns
[i
].type
;
1147 if ( type
& MSITYPE_KEY
)
1149 r
= TABLE_fetch_int( view
, row
, i
+1, &ival
);
1150 if ( r
!= ERROR_SUCCESS
)
1153 if ( tv
->columns
[i
].type
& MSITYPE_STRING
)
1155 sval
= msi_string_lookup_id( tv
->db
->strings
, ival
);
1158 r
= ERROR_INVALID_PARAMETER
;
1164 static const WCHAR fmt
[] = { '%','d',0 };
1166 UINT n
= bytes_per_column( tv
->db
, &tv
->columns
[i
], LONG_STR_BYTES
);
1171 sprintfW( number
, fmt
, ival
-0x8000 );
1174 sprintfW( number
, fmt
, ival
^0x80000000 );
1177 ERR( "oops - unknown column width %d\n", n
);
1178 r
= ERROR_FUNCTION_FAILED
;
1184 len
+= lstrlenW( szDot
) + lstrlenW( sval
);
1185 p
= msi_realloc ( stname
, len
*sizeof(WCHAR
) );
1188 r
= ERROR_OUTOFMEMORY
;
1193 lstrcatW( stname
, szDot
);
1194 lstrcatW( stname
, sval
);
1201 return ERROR_SUCCESS
;
1210 * We need a special case for streams, as we need to reference column with
1211 * the name of the stream in the same table, and the table name
1212 * which may not be available at higher levels of the query
1214 static UINT
TABLE_fetch_stream( struct tagMSIVIEW
*view
, UINT row
, UINT col
, IStream
**stm
)
1216 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1218 LPWSTR encname
, full_name
= NULL
;
1220 if( !view
->ops
->fetch_int
)
1221 return ERROR_INVALID_PARAMETER
;
1223 r
= msi_stream_name( tv
, row
, &full_name
);
1224 if ( r
!= ERROR_SUCCESS
)
1226 ERR("fetching stream, error = %d\n", r
);
1230 encname
= encode_streamname( FALSE
, full_name
);
1231 r
= db_get_raw_stream( tv
->db
, encname
, stm
);
1233 ERR("fetching stream %s, error = %d\n",debugstr_w(full_name
), r
);
1235 msi_free( full_name
);
1236 msi_free( encname
);
1240 static UINT
TABLE_set_int( MSITABLEVIEW
*tv
, UINT row
, UINT col
, UINT val
)
1245 return ERROR_INVALID_PARAMETER
;
1247 if( (col
==0) || (col
>tv
->num_cols
) )
1248 return ERROR_INVALID_PARAMETER
;
1250 if( row
>= tv
->table
->row_count
)
1251 return ERROR_INVALID_PARAMETER
;
1253 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1255 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1256 ERR("%p %p\n", tv
, tv
->columns
);
1257 return ERROR_FUNCTION_FAILED
;
1260 msi_free( tv
->columns
[col
-1].hash_table
);
1261 tv
->columns
[col
-1].hash_table
= NULL
;
1263 n
= bytes_per_column( tv
->db
, &tv
->columns
[col
- 1], LONG_STR_BYTES
);
1264 if ( n
!= 2 && n
!= 3 && n
!= 4 )
1266 ERR("oops! what is %d bytes per column?\n", n
);
1267 return ERROR_FUNCTION_FAILED
;
1270 offset
= tv
->columns
[col
-1].offset
;
1271 for ( i
= 0; i
< n
; i
++ )
1272 tv
->table
->data
[row
][offset
+ i
] = (val
>> i
* 8) & 0xff;
1274 return ERROR_SUCCESS
;
1277 static UINT
TABLE_get_row( struct tagMSIVIEW
*view
, UINT row
, MSIRECORD
**rec
)
1279 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1282 return ERROR_INVALID_PARAMETER
;
1285 row
= tv
->order
->reorder
[row
];
1287 return msi_view_get_row(tv
->db
, view
, row
, rec
);
1290 static UINT
msi_addstreamW( MSIDATABASE
*db
, LPCWSTR name
, IStream
*data
)
1293 MSIQUERY
*query
= NULL
;
1294 MSIRECORD
*rec
= NULL
;
1296 static const WCHAR insert
[] = {
1297 'I','N','S','E','R','T',' ','I','N','T','O',' ',
1298 '`','_','S','t','r','e','a','m','s','`',' ',
1299 '(','`','N','a','m','e','`',',',
1300 '`','D','a','t','a','`',')',' ',
1301 'V','A','L','U','E','S',' ','(','?',',','?',')',0};
1303 TRACE("%p %s %p\n", db
, debugstr_w(name
), data
);
1305 rec
= MSI_CreateRecord( 2 );
1307 return ERROR_OUTOFMEMORY
;
1309 r
= MSI_RecordSetStringW( rec
, 1, name
);
1310 if ( r
!= ERROR_SUCCESS
)
1313 r
= MSI_RecordSetIStream( rec
, 2, data
);
1314 if ( r
!= ERROR_SUCCESS
)
1317 r
= MSI_DatabaseOpenViewW( db
, insert
, &query
);
1318 if ( r
!= ERROR_SUCCESS
)
1321 r
= MSI_ViewExecute( query
, rec
);
1324 msiobj_release( &query
->hdr
);
1325 msiobj_release( &rec
->hdr
);
1330 static UINT
get_table_value_from_record( MSITABLEVIEW
*tv
, MSIRECORD
*rec
, UINT iField
, UINT
*pvalue
)
1332 MSICOLUMNINFO columninfo
;
1335 if ( (iField
<= 0) ||
1336 (iField
> tv
->num_cols
) ||
1337 MSI_RecordIsNull( rec
, iField
) )
1338 return ERROR_FUNCTION_FAILED
;
1340 columninfo
= tv
->columns
[ iField
- 1 ];
1342 if ( MSITYPE_IS_BINARY(columninfo
.type
) )
1344 *pvalue
= 1; /* refers to the first key column */
1346 else if ( columninfo
.type
& MSITYPE_STRING
)
1348 LPCWSTR sval
= MSI_RecordGetString( rec
, iField
);
1350 r
= msi_string2idW(tv
->db
->strings
, sval
, pvalue
);
1351 if (r
!= ERROR_SUCCESS
)
1352 return ERROR_NOT_FOUND
;
1354 else if ( bytes_per_column( tv
->db
, &columninfo
, LONG_STR_BYTES
) == 2 )
1356 *pvalue
= 0x8000 + MSI_RecordGetInteger( rec
, iField
);
1357 if ( *pvalue
& 0xffff0000 )
1359 ERR("field %u value %d out of range\n", iField
, *pvalue
- 0x8000);
1360 return ERROR_FUNCTION_FAILED
;
1365 INT ival
= MSI_RecordGetInteger( rec
, iField
);
1366 *pvalue
= ival
^ 0x80000000;
1369 return ERROR_SUCCESS
;
1372 static UINT
TABLE_set_row( struct tagMSIVIEW
*view
, UINT row
, MSIRECORD
*rec
, UINT mask
)
1374 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1375 UINT i
, val
, r
= ERROR_SUCCESS
;
1378 return ERROR_INVALID_PARAMETER
;
1380 /* test if any of the mask bits are invalid */
1381 if ( mask
>= (1<<tv
->num_cols
) )
1382 return ERROR_INVALID_PARAMETER
;
1384 for ( i
= 0; i
< tv
->num_cols
; i
++ )
1388 /* only update the fields specified in the mask */
1389 if ( !(mask
&(1<<i
)) )
1392 persistent
= (tv
->table
->persistent
!= MSICONDITION_FALSE
) &&
1393 (tv
->table
->data_persistent
[row
]);
1394 /* FIXME: should we allow updating keys? */
1397 if ( !MSI_RecordIsNull( rec
, i
+ 1 ) )
1399 r
= get_table_value_from_record (tv
, rec
, i
+ 1, &val
);
1400 if ( MSITYPE_IS_BINARY(tv
->columns
[ i
].type
) )
1405 if ( r
!= ERROR_SUCCESS
)
1406 return ERROR_FUNCTION_FAILED
;
1408 r
= MSI_RecordGetIStream( rec
, i
+ 1, &stm
);
1409 if ( r
!= ERROR_SUCCESS
)
1412 r
= msi_stream_name( tv
, row
, &stname
);
1413 if ( r
!= ERROR_SUCCESS
)
1415 IStream_Release( stm
);
1419 r
= msi_addstreamW( tv
->db
, stname
, stm
);
1420 IStream_Release( stm
);
1421 msi_free ( stname
);
1423 if ( r
!= ERROR_SUCCESS
)
1426 else if ( tv
->columns
[i
].type
& MSITYPE_STRING
)
1430 if ( r
!= ERROR_SUCCESS
)
1432 LPCWSTR sval
= MSI_RecordGetString( rec
, i
+ 1 );
1433 val
= msi_addstringW( tv
->db
->strings
, sval
, -1, 1,
1434 persistent
? StringPersistent
: StringNonPersistent
);
1439 TABLE_fetch_int(&tv
->view
, row
, i
+ 1, &x
);
1446 if ( r
!= ERROR_SUCCESS
)
1447 return ERROR_FUNCTION_FAILED
;
1451 r
= TABLE_set_int( tv
, row
, i
+1, val
);
1452 if ( r
!= ERROR_SUCCESS
)
1458 static UINT
table_create_new_row( struct tagMSIVIEW
*view
, UINT
*num
, BOOL temporary
)
1460 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1465 BOOL
**data_persist_ptr
;
1468 TRACE("%p %s\n", view
, temporary
? "TRUE" : "FALSE");
1471 return ERROR_INVALID_PARAMETER
;
1473 row
= msi_alloc_zero( tv
->row_size
);
1475 return ERROR_NOT_ENOUGH_MEMORY
;
1477 row_count
= &tv
->table
->row_count
;
1478 data_ptr
= &tv
->table
->data
;
1479 data_persist_ptr
= &tv
->table
->data_persistent
;
1481 *num
= tv
->table
->row_count
;
1483 sz
= (*row_count
+ 1) * sizeof (BYTE
*);
1485 p
= msi_realloc( *data_ptr
, sz
);
1487 p
= msi_alloc( sz
);
1491 return ERROR_NOT_ENOUGH_MEMORY
;
1494 sz
= (*row_count
+ 1) * sizeof (BOOL
);
1495 if( *data_persist_ptr
)
1496 b
= msi_realloc( *data_persist_ptr
, sz
);
1498 b
= msi_alloc( sz
);
1503 return ERROR_NOT_ENOUGH_MEMORY
;
1507 (*data_ptr
)[*row_count
] = row
;
1509 *data_persist_ptr
= b
;
1510 (*data_persist_ptr
)[*row_count
] = !temporary
;
1514 return ERROR_SUCCESS
;
1517 static UINT
TABLE_execute( struct tagMSIVIEW
*view
, MSIRECORD
*record
)
1519 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1521 TRACE("%p %p\n", tv
, record
);
1523 TRACE("There are %d columns\n", tv
->num_cols
);
1525 return ERROR_SUCCESS
;
1528 static UINT
TABLE_close( struct tagMSIVIEW
*view
)
1530 TRACE("%p\n", view
);
1532 return ERROR_SUCCESS
;
1535 static UINT
TABLE_get_dimensions( struct tagMSIVIEW
*view
, UINT
*rows
, UINT
*cols
)
1537 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1539 TRACE("%p %p %p\n", view
, rows
, cols
);
1542 *cols
= tv
->num_cols
;
1546 return ERROR_INVALID_PARAMETER
;
1547 *rows
= tv
->table
->row_count
;
1550 return ERROR_SUCCESS
;
1553 static UINT
TABLE_get_column_info( struct tagMSIVIEW
*view
,
1554 UINT n
, LPWSTR
*name
, UINT
*type
, BOOL
*temporary
,
1555 LPWSTR
*table_name
)
1557 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1559 TRACE("%p %d %p %p\n", tv
, n
, name
, type
);
1561 if( ( n
== 0 ) || ( n
> tv
->num_cols
) )
1562 return ERROR_INVALID_PARAMETER
;
1566 *name
= strdupW( tv
->columns
[n
-1].colname
);
1568 return ERROR_FUNCTION_FAILED
;
1573 *table_name
= strdupW( tv
->columns
[n
-1].tablename
);
1575 return ERROR_FUNCTION_FAILED
;
1579 *type
= tv
->columns
[n
-1].type
;
1582 *temporary
= tv
->columns
[n
-1].temporary
;
1584 return ERROR_SUCCESS
;
1587 static UINT
msi_table_find_row( MSITABLEVIEW
*tv
, MSIRECORD
*rec
, UINT
*row
);
1589 static UINT
table_validate_new( MSITABLEVIEW
*tv
, MSIRECORD
*rec
)
1593 /* check there's no null values where they're not allowed */
1594 for( i
= 0; i
< tv
->num_cols
; i
++ )
1596 if ( tv
->columns
[i
].type
& MSITYPE_NULLABLE
)
1599 if ( MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
1600 TRACE("skipping binary column\n");
1601 else if ( tv
->columns
[i
].type
& MSITYPE_STRING
)
1605 str
= MSI_RecordGetString( rec
, i
+1 );
1606 if (str
== NULL
|| str
[0] == 0)
1607 return ERROR_INVALID_DATA
;
1613 n
= MSI_RecordGetInteger( rec
, i
+1 );
1614 if (n
== MSI_NULL_INTEGER
)
1615 return ERROR_INVALID_DATA
;
1619 /* check there's no duplicate keys */
1620 r
= msi_table_find_row( tv
, rec
, &row
);
1621 if (r
== ERROR_SUCCESS
)
1622 return ERROR_FUNCTION_FAILED
;
1624 return ERROR_SUCCESS
;
1627 static int compare_record( MSITABLEVIEW
*tv
, UINT row
, MSIRECORD
*rec
)
1629 UINT r
, i
, ivalue
, x
;
1631 for (i
= 0; i
< tv
->num_cols
; i
++ )
1633 r
= get_table_value_from_record( tv
, rec
, i
+ 1, &ivalue
);
1634 if (r
!= ERROR_SUCCESS
)
1637 r
= TABLE_fetch_int( &tv
->view
, row
, i
+ 1, &x
);
1638 if (r
!= ERROR_SUCCESS
)
1640 WARN("TABLE_fetch_int should not fail here %u\n", r
);
1647 else if (ivalue
== x
)
1649 if (i
< tv
->num_cols
- 1) continue;
1658 static int find_insert_index( MSITABLEVIEW
*tv
, MSIRECORD
*rec
)
1660 int idx
, c
, low
= 0, high
= tv
->table
->row_count
- 1;
1662 TRACE("%p %p\n", tv
, rec
);
1666 idx
= (low
+ high
) / 2;
1667 c
= compare_record( tv
, idx
, rec
);
1675 TRACE("found %u\n", idx
);
1679 TRACE("found %u\n", high
+ 1);
1683 static UINT
TABLE_insert_row( struct tagMSIVIEW
*view
, MSIRECORD
*rec
, UINT row
, BOOL temporary
)
1685 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1688 TRACE("%p %p %s\n", tv
, rec
, temporary
? "TRUE" : "FALSE" );
1690 /* check that the key is unique - can we find a matching row? */
1691 r
= table_validate_new( tv
, rec
);
1692 if( r
!= ERROR_SUCCESS
)
1693 return ERROR_FUNCTION_FAILED
;
1696 row
= find_insert_index( tv
, rec
);
1698 r
= table_create_new_row( view
, &row
, temporary
);
1699 TRACE("insert_row returned %08x\n", r
);
1700 if( r
!= ERROR_SUCCESS
)
1703 /* shift the rows to make room for the new row */
1704 for (i
= tv
->table
->row_count
- 1; i
> row
; i
--)
1706 memmove(&(tv
->table
->data
[i
][0]),
1707 &(tv
->table
->data
[i
- 1][0]), tv
->row_size
);
1708 tv
->table
->data_persistent
[i
] = tv
->table
->data_persistent
[i
- 1];
1711 /* Re-set the persistence flag */
1712 tv
->table
->data_persistent
[row
] = !temporary
;
1713 return TABLE_set_row( view
, row
, rec
, (1<<tv
->num_cols
) - 1 );
1716 static UINT
TABLE_delete_row( struct tagMSIVIEW
*view
, UINT row
)
1718 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1719 UINT r
, num_rows
, num_cols
, i
;
1721 TRACE("%p %d\n", tv
, row
);
1724 return ERROR_INVALID_PARAMETER
;
1726 r
= TABLE_get_dimensions( view
, &num_rows
, &num_cols
);
1727 if ( r
!= ERROR_SUCCESS
)
1730 if ( row
>= num_rows
)
1731 return ERROR_FUNCTION_FAILED
;
1733 num_rows
= tv
->table
->row_count
;
1734 tv
->table
->row_count
--;
1736 /* reset the hash tables */
1737 for (i
= 0; i
< tv
->num_cols
; i
++)
1739 msi_free( tv
->columns
[i
].hash_table
);
1740 tv
->columns
[i
].hash_table
= NULL
;
1743 for (i
= row
+ 1; i
< num_rows
; i
++)
1745 memcpy(tv
->table
->data
[i
- 1], tv
->table
->data
[i
], tv
->row_size
);
1746 tv
->table
->data_persistent
[i
- 1] = tv
->table
->data_persistent
[i
];
1749 msi_free(tv
->table
->data
[num_rows
- 1]);
1751 return ERROR_SUCCESS
;
1754 static UINT
msi_table_update(struct tagMSIVIEW
*view
, MSIRECORD
*rec
, UINT row
)
1756 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1759 /* FIXME: MsiViewFetch should set rec index 0 to some ID that
1760 * sets the fetched record apart from other records
1764 return ERROR_INVALID_PARAMETER
;
1766 r
= msi_table_find_row(tv
, rec
, &new_row
);
1767 if (r
!= ERROR_SUCCESS
)
1769 ERR("can't find row to modify\n");
1770 return ERROR_FUNCTION_FAILED
;
1773 /* the row cannot be changed */
1774 if (row
!= new_row
+ 1)
1775 return ERROR_FUNCTION_FAILED
;
1777 return TABLE_set_row(view
, new_row
, rec
, (1 << tv
->num_cols
) - 1);
1780 static UINT
msi_table_assign(struct tagMSIVIEW
*view
, MSIRECORD
*rec
)
1782 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1786 return ERROR_INVALID_PARAMETER
;
1788 r
= msi_table_find_row(tv
, rec
, &row
);
1789 if (r
== ERROR_SUCCESS
)
1790 return TABLE_set_row(view
, row
, rec
, (1 << tv
->num_cols
) - 1);
1792 return TABLE_insert_row( view
, rec
, -1, FALSE
);
1795 static UINT
modify_delete_row( struct tagMSIVIEW
*view
, MSIRECORD
*rec
)
1797 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1800 r
= msi_table_find_row(tv
, rec
, &row
);
1801 if (r
!= ERROR_SUCCESS
)
1804 return TABLE_delete_row(view
, row
);
1807 static UINT
msi_refresh_record( struct tagMSIVIEW
*view
, MSIRECORD
*rec
, UINT row
)
1812 r
= TABLE_get_row(view
, row
- 1, &curr
);
1813 if (r
!= ERROR_SUCCESS
)
1816 /* Close the original record */
1817 MSI_CloseRecord(&rec
->hdr
);
1819 count
= MSI_RecordGetFieldCount(rec
);
1820 for (i
= 0; i
< count
; i
++)
1821 MSI_RecordCopyField(curr
, i
+ 1, rec
, i
+ 1);
1823 msiobj_release(&curr
->hdr
);
1824 return ERROR_SUCCESS
;
1827 static UINT
TABLE_modify( struct tagMSIVIEW
*view
, MSIMODIFY eModifyMode
,
1828 MSIRECORD
*rec
, UINT row
)
1830 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1833 TRACE("%p %d %p\n", view
, eModifyMode
, rec
);
1835 switch (eModifyMode
)
1837 case MSIMODIFY_DELETE
:
1838 r
= modify_delete_row( view
, rec
);
1840 case MSIMODIFY_VALIDATE_NEW
:
1841 r
= table_validate_new( tv
, rec
);
1844 case MSIMODIFY_INSERT
:
1845 r
= table_validate_new( tv
, rec
);
1846 if (r
!= ERROR_SUCCESS
)
1848 r
= TABLE_insert_row( view
, rec
, -1, FALSE
);
1851 case MSIMODIFY_INSERT_TEMPORARY
:
1852 r
= table_validate_new( tv
, rec
);
1853 if (r
!= ERROR_SUCCESS
)
1855 r
= TABLE_insert_row( view
, rec
, -1, TRUE
);
1858 case MSIMODIFY_REFRESH
:
1859 r
= msi_refresh_record( view
, rec
, row
);
1862 case MSIMODIFY_UPDATE
:
1863 r
= msi_table_update( view
, rec
, row
);
1866 case MSIMODIFY_ASSIGN
:
1867 r
= msi_table_assign( view
, rec
);
1870 case MSIMODIFY_REPLACE
:
1871 case MSIMODIFY_MERGE
:
1872 case MSIMODIFY_VALIDATE
:
1873 case MSIMODIFY_VALIDATE_FIELD
:
1874 case MSIMODIFY_VALIDATE_DELETE
:
1875 FIXME("%p %d %p - mode not implemented\n", view
, eModifyMode
, rec
);
1876 r
= ERROR_CALL_NOT_IMPLEMENTED
;
1880 r
= ERROR_INVALID_DATA
;
1886 static UINT
TABLE_delete( struct tagMSIVIEW
*view
)
1888 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1890 TRACE("%p\n", view
);
1897 msi_free( tv
->order
->reorder
);
1898 msi_free( tv
->order
);
1904 return ERROR_SUCCESS
;
1907 static UINT
TABLE_find_matching_rows( struct tagMSIVIEW
*view
, UINT col
,
1908 UINT val
, UINT
*row
, MSIITERHANDLE
*handle
)
1910 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1911 const MSICOLUMNHASHENTRY
*entry
;
1913 TRACE("%p, %d, %u, %p\n", view
, col
, val
, *handle
);
1916 return ERROR_INVALID_PARAMETER
;
1918 if( (col
==0) || (col
> tv
->num_cols
) )
1919 return ERROR_INVALID_PARAMETER
;
1921 if( !tv
->columns
[col
-1].hash_table
)
1924 UINT num_rows
= tv
->table
->row_count
;
1925 MSICOLUMNHASHENTRY
**hash_table
;
1926 MSICOLUMNHASHENTRY
*new_entry
;
1928 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1930 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1931 ERR("%p %p\n", tv
, tv
->columns
);
1932 return ERROR_FUNCTION_FAILED
;
1935 /* allocate contiguous memory for the table and its entries so we
1936 * don't have to do an expensive cleanup */
1937 hash_table
= msi_alloc(MSITABLE_HASH_TABLE_SIZE
* sizeof(MSICOLUMNHASHENTRY
*) +
1938 num_rows
* sizeof(MSICOLUMNHASHENTRY
));
1940 return ERROR_OUTOFMEMORY
;
1942 memset(hash_table
, 0, MSITABLE_HASH_TABLE_SIZE
* sizeof(MSICOLUMNHASHENTRY
*));
1943 tv
->columns
[col
-1].hash_table
= hash_table
;
1945 new_entry
= (MSICOLUMNHASHENTRY
*)(hash_table
+ MSITABLE_HASH_TABLE_SIZE
);
1947 for (i
= 0; i
< num_rows
; i
++, new_entry
++)
1951 if (view
->ops
->fetch_int( view
, i
, col
, &row_value
) != ERROR_SUCCESS
)
1954 new_entry
->next
= NULL
;
1955 new_entry
->value
= row_value
;
1957 if (hash_table
[row_value
% MSITABLE_HASH_TABLE_SIZE
])
1959 MSICOLUMNHASHENTRY
*prev_entry
= hash_table
[row_value
% MSITABLE_HASH_TABLE_SIZE
];
1960 while (prev_entry
->next
)
1961 prev_entry
= prev_entry
->next
;
1962 prev_entry
->next
= new_entry
;
1965 hash_table
[row_value
% MSITABLE_HASH_TABLE_SIZE
] = new_entry
;
1970 entry
= tv
->columns
[col
-1].hash_table
[val
% MSITABLE_HASH_TABLE_SIZE
];
1972 entry
= (*handle
)->next
;
1974 while (entry
&& entry
->value
!= val
)
1975 entry
= entry
->next
;
1979 return ERROR_NO_MORE_ITEMS
;
1983 return ERROR_SUCCESS
;
1986 static UINT
TABLE_add_ref(struct tagMSIVIEW
*view
)
1988 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1991 TRACE("%p %d\n", view
, tv
->table
->ref_count
);
1993 for (i
= 0; i
< tv
->table
->col_count
; i
++)
1995 if (tv
->table
->colinfo
[i
].type
& MSITYPE_TEMPORARY
)
1996 InterlockedIncrement(&tv
->table
->colinfo
[i
].ref_count
);
1999 return InterlockedIncrement(&tv
->table
->ref_count
);
2002 static UINT
TABLE_remove_column(struct tagMSIVIEW
*view
, LPCWSTR table
, UINT number
)
2004 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
2005 MSIRECORD
*rec
= NULL
;
2006 MSIVIEW
*columns
= NULL
;
2009 rec
= MSI_CreateRecord(2);
2011 return ERROR_OUTOFMEMORY
;
2013 MSI_RecordSetStringW(rec
, 1, table
);
2014 MSI_RecordSetInteger(rec
, 2, number
);
2016 r
= TABLE_CreateView(tv
->db
, szColumns
, &columns
);
2017 if (r
!= ERROR_SUCCESS
)
2020 r
= msi_table_find_row((MSITABLEVIEW
*)columns
, rec
, &row
);
2021 if (r
!= ERROR_SUCCESS
)
2024 r
= TABLE_delete_row(columns
, row
);
2025 if (r
!= ERROR_SUCCESS
)
2028 msi_update_table_columns(tv
->db
, table
);
2031 msiobj_release(&rec
->hdr
);
2032 columns
->ops
->delete(columns
);
2036 static UINT
TABLE_release(struct tagMSIVIEW
*view
)
2038 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
2039 INT ref
= tv
->table
->ref_count
;
2042 TRACE("%p %d\n", view
, ref
);
2044 for (i
= 0; i
< tv
->table
->col_count
; i
++)
2046 if (tv
->table
->colinfo
[i
].type
& MSITYPE_TEMPORARY
)
2048 ref
= InterlockedDecrement(&tv
->table
->colinfo
[i
].ref_count
);
2051 r
= TABLE_remove_column(view
, tv
->table
->colinfo
[i
].tablename
,
2052 tv
->table
->colinfo
[i
].number
);
2053 if (r
!= ERROR_SUCCESS
)
2059 ref
= InterlockedDecrement(&tv
->table
->ref_count
);
2062 if (!tv
->table
->row_count
)
2064 list_remove(&tv
->table
->entry
);
2065 free_table(tv
->table
);
2073 static UINT
TABLE_add_column(struct tagMSIVIEW
*view
, LPCWSTR table
, UINT number
,
2074 LPCWSTR column
, UINT type
, BOOL hold
)
2076 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
2081 rec
= MSI_CreateRecord(4);
2083 return ERROR_OUTOFMEMORY
;
2085 MSI_RecordSetStringW(rec
, 1, table
);
2086 MSI_RecordSetInteger(rec
, 2, number
);
2087 MSI_RecordSetStringW(rec
, 3, column
);
2088 MSI_RecordSetInteger(rec
, 4, type
);
2090 r
= TABLE_insert_row(&tv
->view
, rec
, -1, FALSE
);
2091 if (r
!= ERROR_SUCCESS
)
2094 msi_update_table_columns(tv
->db
, table
);
2099 msitable
= find_cached_table(tv
->db
, table
);
2100 for (i
= 0; i
< msitable
->col_count
; i
++)
2102 if (!lstrcmpW(msitable
->colinfo
[i
].colname
, column
))
2104 InterlockedIncrement(&msitable
->colinfo
[i
].ref_count
);
2110 msiobj_release(&rec
->hdr
);
2114 static UINT
order_add_column(struct tagMSIVIEW
*view
, MSIORDERINFO
*order
, LPCWSTR name
)
2117 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
2119 r
= TABLE_get_dimensions(view
, NULL
, &count
);
2120 if (r
!= ERROR_SUCCESS
)
2123 if (order
->num_cols
>= count
)
2124 return ERROR_FUNCTION_FAILED
;
2126 r
= VIEW_find_column(view
, name
, tv
->name
, &n
);
2127 if (r
!= ERROR_SUCCESS
)
2130 order
->cols
[order
->num_cols
] = n
;
2131 TRACE("Ordering by column %s (%d)\n", debugstr_w(name
), n
);
2135 return ERROR_SUCCESS
;
2138 static UINT
order_compare(struct tagMSIVIEW
*view
, MSIORDERINFO
*order
,
2139 UINT a
, UINT b
, UINT
*swap
)
2141 UINT r
, i
, a_val
= 0, b_val
= 0;
2144 for (i
= 0; i
< order
->num_cols
; i
++)
2146 r
= TABLE_fetch_int(view
, a
, order
->cols
[i
], &a_val
);
2147 if (r
!= ERROR_SUCCESS
)
2150 r
= TABLE_fetch_int(view
, b
, order
->cols
[i
], &b_val
);
2151 if (r
!= ERROR_SUCCESS
)
2162 return ERROR_SUCCESS
;
2165 static UINT
order_mergesort(struct tagMSIVIEW
*view
, MSIORDERINFO
*order
,
2166 UINT left
, UINT right
)
2169 UINT swap
= 0, center
= (left
+ right
) / 2;
2170 UINT
*array
= order
->reorder
;
2173 return ERROR_SUCCESS
;
2175 /* sort the left half */
2176 r
= order_mergesort(view
, order
, left
, center
);
2177 if (r
!= ERROR_SUCCESS
)
2180 /* sort the right half */
2181 r
= order_mergesort(view
, order
, center
+ 1, right
);
2182 if (r
!= ERROR_SUCCESS
)
2185 for (i
= left
, j
= center
+ 1; (i
<= center
) && (j
<= right
); i
++)
2187 r
= order_compare(view
, order
, array
[i
], array
[j
], &swap
);
2188 if (r
!= ERROR_SUCCESS
)
2194 memmove(&array
[i
+ 1], &array
[i
], (j
- i
) * sizeof(UINT
));
2201 return ERROR_SUCCESS
;
2204 static UINT
order_verify(struct tagMSIVIEW
*view
, MSIORDERINFO
*order
, UINT num_rows
)
2208 for (i
= 1; i
< num_rows
; i
++)
2210 r
= order_compare(view
, order
, order
->reorder
[i
- 1],
2211 order
->reorder
[i
], &swap
);
2212 if (r
!= ERROR_SUCCESS
)
2218 ERR("Bad order! %d\n", i
);
2219 return ERROR_FUNCTION_FAILED
;
2222 return ERROR_SUCCESS
;
2225 static UINT
TABLE_sort(struct tagMSIVIEW
*view
, column_info
*columns
)
2227 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
2228 MSIORDERINFO
*order
;
2233 TRACE("sorting table %s\n", debugstr_w(tv
->name
));
2235 r
= TABLE_get_dimensions(view
, &rows
, &cols
);
2236 if (r
!= ERROR_SUCCESS
)
2240 return ERROR_SUCCESS
;
2242 order
= msi_alloc_zero(sizeof(MSIORDERINFO
) + sizeof(UINT
) * cols
);
2244 return ERROR_OUTOFMEMORY
;
2246 for (ptr
= columns
; ptr
; ptr
= ptr
->next
)
2247 order_add_column(view
, order
, ptr
->column
);
2249 order
->reorder
= msi_alloc(rows
* sizeof(UINT
));
2250 if (!order
->reorder
)
2251 return ERROR_OUTOFMEMORY
;
2253 for (i
= 0; i
< rows
; i
++)
2254 order
->reorder
[i
] = i
;
2256 r
= order_mergesort(view
, order
, 0, rows
- 1);
2257 if (r
!= ERROR_SUCCESS
)
2260 r
= order_verify(view
, order
, rows
);
2261 if (r
!= ERROR_SUCCESS
)
2266 return ERROR_SUCCESS
;
2269 static UINT
TABLE_drop(struct tagMSIVIEW
*view
)
2271 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
2272 MSIVIEW
*tables
= NULL
;
2273 MSIRECORD
*rec
= NULL
;
2277 TRACE("dropping table %s\n", debugstr_w(tv
->name
));
2279 for (i
= tv
->table
->col_count
- 1; i
>= 0; i
--)
2281 r
= TABLE_remove_column(view
, tv
->table
->colinfo
[i
].tablename
,
2282 tv
->table
->colinfo
[i
].number
);
2283 if (r
!= ERROR_SUCCESS
)
2287 rec
= MSI_CreateRecord(1);
2289 return ERROR_OUTOFMEMORY
;
2291 MSI_RecordSetStringW(rec
, 1, tv
->name
);
2293 r
= TABLE_CreateView(tv
->db
, szTables
, &tables
);
2294 if (r
!= ERROR_SUCCESS
)
2297 r
= msi_table_find_row((MSITABLEVIEW
*)tables
, rec
, &row
);
2298 if (r
!= ERROR_SUCCESS
)
2301 r
= TABLE_delete_row(tables
, row
);
2302 if (r
!= ERROR_SUCCESS
)
2305 list_remove(&tv
->table
->entry
);
2306 free_table(tv
->table
);
2309 msiobj_release(&rec
->hdr
);
2310 tables
->ops
->delete(tables
);
2315 static const MSIVIEWOPS table_ops
=
2325 TABLE_get_dimensions
,
2326 TABLE_get_column_info
,
2329 TABLE_find_matching_rows
,
2333 TABLE_remove_column
,
2338 UINT
TABLE_CreateView( MSIDATABASE
*db
, LPCWSTR name
, MSIVIEW
**view
)
2343 static const WCHAR Streams
[] = {'_','S','t','r','e','a','m','s',0};
2344 static const WCHAR Storages
[] = {'_','S','t','o','r','a','g','e','s',0};
2346 TRACE("%p %s %p\n", db
, debugstr_w(name
), view
);
2348 if ( !lstrcmpW( name
, Streams
) )
2349 return STREAMS_CreateView( db
, view
);
2350 else if ( !lstrcmpW( name
, Storages
) )
2351 return STORAGES_CreateView( db
, view
);
2353 sz
= sizeof *tv
+ lstrlenW(name
)*sizeof name
[0] ;
2354 tv
= msi_alloc_zero( sz
);
2356 return ERROR_FUNCTION_FAILED
;
2358 r
= get_table( db
, name
, &tv
->table
);
2359 if( r
!= ERROR_SUCCESS
)
2362 WARN("table not found\n");
2366 TRACE("table %p found with %d columns\n", tv
->table
, tv
->table
->col_count
);
2368 /* fill the structure */
2369 tv
->view
.ops
= &table_ops
;
2371 tv
->columns
= tv
->table
->colinfo
;
2372 tv
->num_cols
= tv
->table
->col_count
;
2373 tv
->row_size
= msi_table_get_row_size( db
, tv
->table
->colinfo
, tv
->table
->col_count
, LONG_STR_BYTES
);
2375 TRACE("%s one row is %d bytes\n", debugstr_w(name
), tv
->row_size
);
2377 *view
= (MSIVIEW
*) tv
;
2378 lstrcpyW( tv
->name
, name
);
2380 return ERROR_SUCCESS
;
2383 UINT
MSI_CommitTables( MSIDATABASE
*db
)
2385 UINT r
, bytes_per_strref
;
2387 MSITABLE
*table
= NULL
;
2391 r
= msi_save_string_table( db
->strings
, db
->storage
, &bytes_per_strref
);
2392 if( r
!= ERROR_SUCCESS
)
2394 WARN("failed to save string table r=%08x\n",r
);
2398 LIST_FOR_EACH_ENTRY( table
, &db
->tables
, MSITABLE
, entry
)
2400 r
= save_table( db
, table
, bytes_per_strref
);
2401 if( r
!= ERROR_SUCCESS
)
2403 WARN("failed to save table %s (r=%08x)\n",
2404 debugstr_w(table
->name
), r
);
2409 /* force everything to reload next time */
2410 free_cached_tables( db
);
2412 hr
= IStorage_Commit( db
->storage
, 0 );
2415 WARN("failed to commit changes 0x%08x\n", hr
);
2416 r
= ERROR_FUNCTION_FAILED
;
2421 MSICONDITION
MSI_DatabaseIsTablePersistent( MSIDATABASE
*db
, LPCWSTR table
)
2426 TRACE("%p %s\n", db
, debugstr_w(table
));
2429 return MSICONDITION_ERROR
;
2431 r
= get_table( db
, table
, &t
);
2432 if (r
!= ERROR_SUCCESS
)
2433 return MSICONDITION_NONE
;
2435 return t
->persistent
;
2438 static UINT
read_raw_int(const BYTE
*data
, UINT col
, UINT bytes
)
2442 for (i
= 0; i
< bytes
; i
++)
2443 ret
+= (data
[col
+ i
] << i
* 8);
2448 static UINT
msi_record_encoded_stream_name( const MSITABLEVIEW
*tv
, MSIRECORD
*rec
, LPWSTR
*pstname
)
2450 LPWSTR stname
= NULL
, sval
, p
;
2454 TRACE("%p %p\n", tv
, rec
);
2456 len
= lstrlenW( tv
->name
) + 1;
2457 stname
= msi_alloc( len
*sizeof(WCHAR
) );
2460 r
= ERROR_OUTOFMEMORY
;
2464 lstrcpyW( stname
, tv
->name
);
2466 for ( i
= 0; i
< tv
->num_cols
; i
++ )
2468 if ( tv
->columns
[i
].type
& MSITYPE_KEY
)
2470 sval
= msi_dup_record_field( rec
, i
+ 1 );
2473 r
= ERROR_OUTOFMEMORY
;
2477 len
+= lstrlenW( szDot
) + lstrlenW ( sval
);
2478 p
= msi_realloc ( stname
, len
*sizeof(WCHAR
) );
2481 r
= ERROR_OUTOFMEMORY
;
2486 lstrcatW( stname
, szDot
);
2487 lstrcatW( stname
, sval
);
2495 *pstname
= encode_streamname( FALSE
, stname
);
2498 return ERROR_SUCCESS
;
2501 msi_free ( stname
);
2506 static MSIRECORD
*msi_get_transform_record( const MSITABLEVIEW
*tv
, const string_table
*st
,
2508 const BYTE
*rawdata
, UINT bytes_per_strref
)
2510 UINT i
, val
, ofs
= 0;
2512 MSICOLUMNINFO
*columns
= tv
->columns
;
2515 mask
= rawdata
[0] | (rawdata
[1] << 8);
2518 rec
= MSI_CreateRecord( tv
->num_cols
);
2523 for( i
=0; i
<tv
->num_cols
; i
++ )
2525 if ( (mask
&1) && (i
>=(mask
>>8)) )
2527 /* all keys must be present */
2528 if ( (~mask
&1) && (~columns
[i
].type
& MSITYPE_KEY
) && ((1<<i
) & ~mask
) )
2531 if( MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
2534 IStream
*stm
= NULL
;
2537 ofs
+= bytes_per_column( tv
->db
, &columns
[i
], bytes_per_strref
);
2539 r
= msi_record_encoded_stream_name( tv
, rec
, &encname
);
2540 if ( r
!= ERROR_SUCCESS
)
2543 r
= IStorage_OpenStream( stg
, encname
, NULL
,
2544 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, &stm
);
2545 msi_free( encname
);
2546 if ( r
!= ERROR_SUCCESS
)
2549 MSI_RecordSetStream( rec
, i
+1, stm
);
2550 TRACE(" field %d [%s]\n", i
+1, debugstr_w(encname
));
2552 else if( columns
[i
].type
& MSITYPE_STRING
)
2556 val
= read_raw_int(rawdata
, ofs
, bytes_per_strref
);
2557 sval
= msi_string_lookup_id( st
, val
);
2558 MSI_RecordSetStringW( rec
, i
+1, sval
);
2559 TRACE(" field %d [%s]\n", i
+1, debugstr_w(sval
));
2560 ofs
+= bytes_per_strref
;
2564 UINT n
= bytes_per_column( tv
->db
, &columns
[i
], bytes_per_strref
);
2568 val
= read_raw_int(rawdata
, ofs
, n
);
2570 MSI_RecordSetInteger( rec
, i
+1, val
-0x8000 );
2571 TRACE(" field %d [0x%04x]\n", i
+1, val
);
2574 val
= read_raw_int(rawdata
, ofs
, n
);
2576 MSI_RecordSetInteger( rec
, i
+1, val
^0x80000000 );
2577 TRACE(" field %d [0x%08x]\n", i
+1, val
);
2580 ERR("oops - unknown column width %d\n", n
);
2589 static void dump_record( MSIRECORD
*rec
)
2593 n
= MSI_RecordGetFieldCount( rec
);
2594 for( i
=1; i
<=n
; i
++ )
2596 LPCWSTR sval
= MSI_RecordGetString( rec
, i
);
2598 if( MSI_RecordIsNull( rec
, i
) )
2599 TRACE("row -> []\n");
2600 else if( (sval
= MSI_RecordGetString( rec
, i
)) )
2601 TRACE("row -> [%s]\n", debugstr_w(sval
));
2603 TRACE("row -> [0x%08x]\n", MSI_RecordGetInteger( rec
, i
) );
2607 static void dump_table( const string_table
*st
, const USHORT
*rawdata
, UINT rawsize
)
2612 for( i
=0; i
<(rawsize
/2); i
++ )
2614 sval
= msi_string_lookup_id( st
, rawdata
[i
] );
2615 MESSAGE(" %04x %s\n", rawdata
[i
], debugstr_w(sval
) );
2619 static UINT
* msi_record_to_row( const MSITABLEVIEW
*tv
, MSIRECORD
*rec
)
2624 data
= msi_alloc( tv
->num_cols
*sizeof (UINT
) );
2625 for( i
=0; i
<tv
->num_cols
; i
++ )
2629 if ( ~tv
->columns
[i
].type
& MSITYPE_KEY
)
2632 /* turn the transform column value into a row value */
2633 if ( ( tv
->columns
[i
].type
& MSITYPE_STRING
) &&
2634 ! MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
2636 str
= MSI_RecordGetString( rec
, i
+1 );
2637 r
= msi_string2idW( tv
->db
->strings
, str
, &data
[i
] );
2639 /* if there's no matching string in the string table,
2640 these keys can't match any record, so fail now. */
2641 if( ERROR_SUCCESS
!= r
)
2649 data
[i
] = MSI_RecordGetInteger( rec
, i
+1 );
2651 if (data
[i
] == MSI_NULL_INTEGER
)
2653 else if ((tv
->columns
[i
].type
&0xff) == 2)
2656 data
[i
] += 0x80000000;
2662 static UINT
msi_row_matches( MSITABLEVIEW
*tv
, UINT row
, const UINT
*data
)
2664 UINT i
, r
, x
, ret
= ERROR_FUNCTION_FAILED
;
2666 for( i
=0; i
<tv
->num_cols
; i
++ )
2668 if ( ~tv
->columns
[i
].type
& MSITYPE_KEY
)
2671 /* turn the transform column value into a row value */
2672 r
= TABLE_fetch_int( &tv
->view
, row
, i
+1, &x
);
2673 if ( r
!= ERROR_SUCCESS
)
2675 ERR("TABLE_fetch_int shouldn't fail here\n");
2679 /* if this key matches, move to the next column */
2682 ret
= ERROR_FUNCTION_FAILED
;
2686 ret
= ERROR_SUCCESS
;
2692 static UINT
msi_table_find_row( MSITABLEVIEW
*tv
, MSIRECORD
*rec
, UINT
*row
)
2694 UINT i
, r
= ERROR_FUNCTION_FAILED
, *data
;
2696 data
= msi_record_to_row( tv
, rec
);
2699 for( i
= 0; i
< tv
->table
->row_count
; i
++ )
2701 r
= msi_row_matches( tv
, i
, data
);
2702 if( r
== ERROR_SUCCESS
)
2718 static UINT
msi_table_load_transform( MSIDATABASE
*db
, IStorage
*stg
,
2719 string_table
*st
, TRANSFORMDATA
*transform
,
2720 UINT bytes_per_strref
)
2723 BYTE
*rawdata
= NULL
;
2724 MSITABLEVIEW
*tv
= NULL
;
2725 UINT r
, n
, sz
, i
, mask
;
2726 MSIRECORD
*rec
= NULL
;
2732 return ERROR_SUCCESS
;
2734 name
= transform
->name
;
2737 TRACE("%p %p %p %s\n", db
, stg
, st
, debugstr_w(name
) );
2739 /* read the transform data */
2740 read_stream_data( stg
, name
, TRUE
, &rawdata
, &rawsize
);
2743 TRACE("table %s empty\n", debugstr_w(name
) );
2744 return ERROR_INVALID_TABLE
;
2747 /* create a table view */
2748 r
= TABLE_CreateView( db
, name
, (MSIVIEW
**) &tv
);
2749 if( r
!= ERROR_SUCCESS
)
2752 r
= tv
->view
.ops
->execute( &tv
->view
, NULL
);
2753 if( r
!= ERROR_SUCCESS
)
2756 TRACE("name = %s columns = %u row_size = %u raw size = %u\n",
2757 debugstr_w(name
), tv
->num_cols
, tv
->row_size
, rawsize
);
2759 /* interpret the data */
2761 for( n
=0; n
< rawsize
; )
2763 mask
= rawdata
[n
] | (rawdata
[n
+1] << 8);
2768 * if the low bit is set, columns are continuous and
2769 * the number of columns is specified in the high byte
2772 for( i
=0; i
<tv
->num_cols
; i
++ )
2774 if( (tv
->columns
[i
].type
& MSITYPE_STRING
) &&
2775 ! MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
2776 sz
+= bytes_per_strref
;
2778 sz
+= bytes_per_column( tv
->db
, &tv
->columns
[i
], bytes_per_strref
);
2784 * If the low bit is not set, mask is a bitmask.
2785 * Excepting for key fields, which are always present,
2786 * each bit indicates that a field is present in the transform record.
2788 * mask == 0 is a special case ... only the keys will be present
2789 * and it means that this row should be deleted.
2792 for( i
=0; i
<tv
->num_cols
; i
++ )
2794 if( (tv
->columns
[i
].type
& MSITYPE_KEY
) || ((1<<i
)&mask
))
2796 if( (tv
->columns
[i
].type
& MSITYPE_STRING
) &&
2797 ! MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
2798 sz
+= bytes_per_strref
;
2800 sz
+= bytes_per_column( tv
->db
, &tv
->columns
[i
], bytes_per_strref
);
2805 /* check we didn't run of the end of the table */
2806 if ( (n
+sz
) > rawsize
)
2809 dump_table( st
, (USHORT
*)rawdata
, rawsize
);
2813 rec
= msi_get_transform_record( tv
, st
, stg
, &rawdata
[n
], bytes_per_strref
);
2818 UINT number
= MSI_NULL_INTEGER
;
2821 if (!lstrcmpW( name
, szColumns
))
2823 MSI_RecordGetStringW( rec
, 1, table
, &sz
);
2824 number
= MSI_RecordGetInteger( rec
, 2 );
2827 * Native msi seems writes nul into the Number (2nd) column of
2828 * the _Columns table, only when the columns are from a new table
2830 if ( number
== MSI_NULL_INTEGER
)
2832 /* reset the column number on a new table */
2833 if (lstrcmpW( coltable
, table
))
2836 lstrcpyW( coltable
, table
);
2839 /* fix nul column numbers */
2840 MSI_RecordSetInteger( rec
, 2, ++colcol
);
2844 if (TRACE_ON(msidb
)) dump_record( rec
);
2846 r
= msi_table_find_row( tv
, rec
, &row
);
2847 if (r
== ERROR_SUCCESS
)
2851 TRACE("deleting row [%d]:\n", row
);
2852 r
= TABLE_delete_row( &tv
->view
, row
);
2853 if (r
!= ERROR_SUCCESS
)
2854 WARN("failed to delete row %u\n", r
);
2858 TRACE("modifying full row [%d]:\n", row
);
2859 r
= TABLE_set_row( &tv
->view
, row
, rec
, (1 << tv
->num_cols
) - 1 );
2860 if (r
!= ERROR_SUCCESS
)
2861 WARN("failed to modify row %u\n", r
);
2865 TRACE("modifying masked row [%d]:\n", row
);
2866 r
= TABLE_set_row( &tv
->view
, row
, rec
, mask
);
2867 if (r
!= ERROR_SUCCESS
)
2868 WARN("failed to modify row %u\n", r
);
2873 TRACE("inserting row\n");
2874 r
= TABLE_insert_row( &tv
->view
, rec
, -1, FALSE
);
2875 if (r
!= ERROR_SUCCESS
)
2876 WARN("failed to insert row %u\n", r
);
2879 if (number
!= MSI_NULL_INTEGER
&& !lstrcmpW( name
, szColumns
))
2880 msi_update_table_columns( db
, table
);
2882 msiobj_release( &rec
->hdr
);
2889 /* no need to free the table, it's associated with the database */
2890 msi_free( rawdata
);
2892 tv
->view
.ops
->delete( &tv
->view
);
2894 return ERROR_SUCCESS
;
2898 * msi_table_apply_transform
2900 * Enumerate the table transforms in a transform storage and apply each one.
2902 UINT
msi_table_apply_transform( MSIDATABASE
*db
, IStorage
*stg
)
2904 struct list transforms
;
2905 IEnumSTATSTG
*stgenum
= NULL
;
2906 TRANSFORMDATA
*transform
;
2907 TRANSFORMDATA
*tables
= NULL
, *columns
= NULL
;
2910 string_table
*strings
;
2911 UINT ret
= ERROR_FUNCTION_FAILED
;
2912 UINT bytes_per_strref
;
2914 TRACE("%p %p\n", db
, stg
);
2916 strings
= msi_load_string_table( stg
, &bytes_per_strref
);
2920 r
= IStorage_EnumElements( stg
, 0, NULL
, 0, &stgenum
);
2924 list_init(&transforms
);
2928 MSITABLEVIEW
*tv
= NULL
;
2932 r
= IEnumSTATSTG_Next( stgenum
, 1, &stat
, &count
);
2933 if ( FAILED( r
) || !count
)
2936 decode_streamname( stat
.pwcsName
, name
);
2937 CoTaskMemFree( stat
.pwcsName
);
2938 if ( name
[0] != 0x4840 )
2941 if ( !lstrcmpW( name
+1, szStringPool
) ||
2942 !lstrcmpW( name
+1, szStringData
) )
2945 transform
= msi_alloc_zero( sizeof(TRANSFORMDATA
) );
2949 list_add_tail( &transforms
, &transform
->entry
);
2951 transform
->name
= strdupW( name
+ 1 );
2953 if ( !lstrcmpW( transform
->name
, szTables
) )
2955 else if (!lstrcmpW( transform
->name
, szColumns
) )
2956 columns
= transform
;
2958 TRACE("transform contains stream %s\n", debugstr_w(name
));
2960 /* load the table */
2961 r
= TABLE_CreateView( db
, transform
->name
, (MSIVIEW
**) &tv
);
2962 if( r
!= ERROR_SUCCESS
)
2965 r
= tv
->view
.ops
->execute( &tv
->view
, NULL
);
2966 if( r
!= ERROR_SUCCESS
)
2968 tv
->view
.ops
->delete( &tv
->view
);
2972 tv
->view
.ops
->delete( &tv
->view
);
2976 * Apply _Tables and _Columns transforms first so that
2977 * the table metadata is correct, and empty tables exist.
2979 ret
= msi_table_load_transform( db
, stg
, strings
, tables
, bytes_per_strref
);
2980 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_INVALID_TABLE
)
2983 ret
= msi_table_load_transform( db
, stg
, strings
, columns
, bytes_per_strref
);
2984 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_INVALID_TABLE
)
2987 ret
= ERROR_SUCCESS
;
2989 while ( !list_empty( &transforms
) )
2991 transform
= LIST_ENTRY( list_head( &transforms
), TRANSFORMDATA
, entry
);
2993 if ( lstrcmpW( transform
->name
, szColumns
) &&
2994 lstrcmpW( transform
->name
, szTables
) &&
2995 ret
== ERROR_SUCCESS
)
2997 ret
= msi_table_load_transform( db
, stg
, strings
, transform
, bytes_per_strref
);
3000 list_remove( &transform
->entry
);
3001 msi_free( transform
->name
);
3002 msi_free( transform
);
3005 if ( ret
== ERROR_SUCCESS
)
3006 append_storage_to_db( db
, stg
);
3010 IEnumSTATSTG_Release( stgenum
);
3012 msi_destroy_stringtable( strings
);