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 /* information for default tables */
86 static const WCHAR szTables
[] = {'_','T','a','b','l','e','s',0};
87 static const WCHAR szTable
[] = {'T','a','b','l','e',0};
88 static const WCHAR szColumns
[] = {'_','C','o','l','u','m','n','s',0};
89 static const WCHAR szNumber
[] = {'N','u','m','b','e','r',0};
90 static const WCHAR szType
[] = {'T','y','p','e',0};
92 static const MSICOLUMNINFO _Columns_cols
[4] = {
93 { szColumns
, 1, szTable
, MSITYPE_VALID
| MSITYPE_STRING
| MSITYPE_KEY
| 64, 0, 0, 0, NULL
},
94 { szColumns
, 2, szNumber
, MSITYPE_VALID
| MSITYPE_KEY
| 2, 2, 0, 0, NULL
},
95 { szColumns
, 3, szName
, MSITYPE_VALID
| MSITYPE_STRING
| 64, 4, 0, 0, NULL
},
96 { szColumns
, 4, szType
, MSITYPE_VALID
| 2, 6, 0, 0, NULL
},
99 static const MSICOLUMNINFO _Tables_cols
[1] = {
100 { szTables
, 1, szName
, MSITYPE_VALID
| MSITYPE_STRING
| MSITYPE_KEY
| 64, 0, 0, 0, NULL
},
103 #define MAX_STREAM_NAME 0x1f
105 static inline UINT
bytes_per_column( MSIDATABASE
*db
, const MSICOLUMNINFO
*col
, UINT bytes_per_strref
)
107 if( MSITYPE_IS_BINARY(col
->type
) )
110 if( col
->type
& MSITYPE_STRING
)
111 return bytes_per_strref
;
113 if( (col
->type
& 0xff) <= 2)
116 if( (col
->type
& 0xff) != 4 )
117 ERR("Invalid column size!\n");
122 static int utf2mime(int x
)
124 if( (x
>='0') && (x
<='9') )
126 if( (x
>='A') && (x
<='Z') )
128 if( (x
>='a') && (x
<='z') )
137 LPWSTR
encode_streamname(BOOL bTable
, LPCWSTR in
)
139 DWORD count
= MAX_STREAM_NAME
;
144 count
= lstrlenW( in
)+2;
145 if (!(out
= msi_alloc( count
*sizeof(WCHAR
) ))) return NULL
;
161 if( ( ch
< 0x80 ) && ( utf2mime(ch
) >= 0 ) )
163 ch
= utf2mime(ch
) + 0x4800;
165 if( next
&& (next
<0x80) )
167 next
= utf2mime(next
);
178 ERR("Failed to encode stream name (%s)\n",debugstr_w(in
));
183 static int mime2utf(int x
)
190 return x
- 10 - 26 + 'a';
191 if( x
== (10+26+26) )
196 BOOL
decode_streamname(LPCWSTR in
, LPWSTR out
)
201 while ( (ch
= *in
++) )
203 if( (ch
>= 0x3800 ) && (ch
< 0x4840 ) )
206 ch
= mime2utf(ch
-0x4800);
210 *out
++ = mime2utf(ch
&0x3f);
212 ch
= mime2utf((ch
>>6)&0x3f);
222 void enum_stream_names( IStorage
*stg
)
224 IEnumSTATSTG
*stgenum
= NULL
;
230 r
= IStorage_EnumElements( stg
, 0, NULL
, 0, &stgenum
);
238 r
= IEnumSTATSTG_Next( stgenum
, 1, &stat
, &count
);
239 if( FAILED( r
) || !count
)
241 decode_streamname( stat
.pwcsName
, name
);
242 TRACE("stream %2d -> %s %s\n", n
,
243 debugstr_w(stat
.pwcsName
), debugstr_w(name
) );
244 CoTaskMemFree( stat
.pwcsName
);
248 IEnumSTATSTG_Release( stgenum
);
251 UINT
read_stream_data( IStorage
*stg
, LPCWSTR stname
, BOOL table
,
252 BYTE
**pdata
, UINT
*psz
)
255 UINT ret
= ERROR_FUNCTION_FAILED
;
262 encname
= encode_streamname(table
, stname
);
264 TRACE("%s -> %s\n",debugstr_w(stname
),debugstr_w(encname
));
266 r
= IStorage_OpenStream(stg
, encname
, NULL
,
267 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, &stm
);
271 WARN("open stream failed r = %08x - empty table?\n", r
);
275 r
= IStream_Stat(stm
, &stat
, STATFLAG_NONAME
);
278 WARN("open stream failed r = %08x!\n", r
);
282 if( stat
.cbSize
.QuadPart
>> 32 )
288 sz
= stat
.cbSize
.QuadPart
;
289 data
= msi_alloc( sz
);
292 WARN("couldn't allocate memory r=%08x!\n", r
);
293 ret
= ERROR_NOT_ENOUGH_MEMORY
;
297 r
= IStream_Read(stm
, data
, sz
, &count
);
298 if( FAILED( r
) || ( count
!= sz
) )
301 WARN("read stream failed r = %08x!\n", r
);
310 IStream_Release( stm
);
315 UINT
write_stream_data( IStorage
*stg
, LPCWSTR stname
,
316 LPCVOID data
, UINT sz
, BOOL bTable
)
319 UINT ret
= ERROR_FUNCTION_FAILED
;
326 encname
= encode_streamname(bTable
, stname
);
327 r
= IStorage_OpenStream( stg
, encname
, NULL
,
328 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, &stm
);
331 r
= IStorage_CreateStream( stg
, encname
,
332 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, 0, &stm
);
337 WARN("open stream failed r = %08x\n", r
);
342 r
= IStream_SetSize( stm
, size
);
345 WARN("Failed to SetSize\n");
350 r
= IStream_Seek( stm
, pos
, STREAM_SEEK_SET
, NULL
);
353 WARN("Failed to Seek\n");
359 r
= IStream_Write(stm
, data
, sz
, &count
);
360 if( FAILED( r
) || ( count
!= sz
) )
362 WARN("Failed to Write\n");
370 IStream_Release( stm
);
375 static void msi_free_colinfo( MSICOLUMNINFO
*colinfo
, UINT count
)
378 for (i
= 0; i
< count
; i
++) msi_free( colinfo
[i
].hash_table
);
381 static void free_table( MSITABLE
*table
)
384 for( i
=0; i
<table
->row_count
; i
++ )
385 msi_free( table
->data
[i
] );
386 msi_free( table
->data
);
387 msi_free( table
->data_persistent
);
388 msi_free_colinfo( table
->colinfo
, table
->col_count
);
389 msi_free( table
->colinfo
);
393 static UINT
msi_table_get_row_size( MSIDATABASE
*db
, const MSICOLUMNINFO
*cols
, UINT count
, UINT bytes_per_strref
)
395 const MSICOLUMNINFO
*last_col
;
400 if (bytes_per_strref
!= LONG_STR_BYTES
)
403 for (i
= 0; i
< count
; i
++) size
+= bytes_per_column( db
, &cols
[i
], bytes_per_strref
);
406 last_col
= &cols
[count
- 1];
407 return last_col
->offset
+ bytes_per_column( db
, last_col
, bytes_per_strref
);
410 /* add this table to the list of cached tables in the database */
411 static UINT
read_table_from_storage( MSIDATABASE
*db
, MSITABLE
*t
, IStorage
*stg
)
413 BYTE
*rawdata
= NULL
;
414 UINT rawsize
= 0, i
, j
, row_size
, row_size_mem
;
416 TRACE("%s\n",debugstr_w(t
->name
));
418 row_size
= msi_table_get_row_size( db
, t
->colinfo
, t
->col_count
, db
->bytes_per_strref
);
419 row_size_mem
= msi_table_get_row_size( db
, t
->colinfo
, t
->col_count
, LONG_STR_BYTES
);
421 /* if we can't read the table, just assume that it's empty */
422 read_stream_data( stg
, t
->name
, TRUE
, &rawdata
, &rawsize
);
424 return ERROR_SUCCESS
;
426 TRACE("Read %d bytes\n", rawsize
);
428 if( rawsize
% row_size
)
430 WARN("Table size is invalid %d/%d\n", rawsize
, row_size
);
434 t
->row_count
= rawsize
/ row_size
;
435 t
->data
= msi_alloc_zero( t
->row_count
* sizeof (USHORT
*) );
438 t
->data_persistent
= msi_alloc_zero( t
->row_count
* sizeof(BOOL
));
439 if ( !t
->data_persistent
)
442 /* transpose all the data */
443 TRACE("Transposing data from %d rows\n", t
->row_count
);
444 for (i
= 0; i
< t
->row_count
; i
++)
446 UINT ofs
= 0, ofs_mem
= 0;
448 t
->data
[i
] = msi_alloc( row_size_mem
);
451 t
->data_persistent
[i
] = TRUE
;
453 for (j
= 0; j
< t
->col_count
; j
++)
455 UINT m
= bytes_per_column( db
, &t
->colinfo
[j
], LONG_STR_BYTES
);
456 UINT n
= bytes_per_column( db
, &t
->colinfo
[j
], db
->bytes_per_strref
);
459 if ( n
!= 2 && n
!= 3 && n
!= 4 )
461 ERR("oops - unknown column width %d\n", n
);
464 if (t
->colinfo
[j
].type
& MSITYPE_STRING
&& n
< m
)
466 for (k
= 0; k
< m
; k
++)
469 t
->data
[i
][ofs_mem
+ k
] = rawdata
[ofs
* t
->row_count
+ i
* n
+ k
];
471 t
->data
[i
][ofs_mem
+ k
] = 0;
476 for (k
= 0; k
< n
; k
++)
477 t
->data
[i
][ofs_mem
+ k
] = rawdata
[ofs
* t
->row_count
+ i
* n
+ k
];
485 return ERROR_SUCCESS
;
488 return ERROR_FUNCTION_FAILED
;
491 void free_cached_tables( MSIDATABASE
*db
)
493 while( !list_empty( &db
->tables
) )
495 MSITABLE
*t
= LIST_ENTRY( list_head( &db
->tables
), MSITABLE
, entry
);
497 list_remove( &t
->entry
);
502 static MSITABLE
*find_cached_table( MSIDATABASE
*db
, LPCWSTR name
)
506 LIST_FOR_EACH_ENTRY( t
, &db
->tables
, MSITABLE
, entry
)
507 if( !strcmpW( name
, t
->name
) )
513 static void table_calc_column_offsets( MSIDATABASE
*db
, MSICOLUMNINFO
*colinfo
, DWORD count
)
517 for (i
= 0; colinfo
&& i
< count
; i
++)
519 assert( i
+ 1 == colinfo
[i
].number
);
520 if (i
) colinfo
[i
].offset
= colinfo
[i
- 1].offset
+
521 bytes_per_column( db
, &colinfo
[i
- 1], LONG_STR_BYTES
);
522 else colinfo
[i
].offset
= 0;
524 TRACE("column %d is [%s] with type %08x ofs %d\n",
525 colinfo
[i
].number
, debugstr_w(colinfo
[i
].colname
),
526 colinfo
[i
].type
, colinfo
[i
].offset
);
530 static UINT
get_defaulttablecolumns( MSIDATABASE
*db
, LPCWSTR name
, MSICOLUMNINFO
*colinfo
, UINT
*sz
)
532 const MSICOLUMNINFO
*p
;
535 TRACE("%s\n", debugstr_w(name
));
537 if (!strcmpW( name
, szTables
))
542 else if (!strcmpW( name
, szColumns
))
547 else return ERROR_FUNCTION_FAILED
;
549 for (i
= 0; i
< n
; i
++)
551 if (colinfo
&& i
< *sz
) colinfo
[i
] = p
[i
];
552 if (colinfo
&& i
>= *sz
) break;
554 table_calc_column_offsets( db
, colinfo
, n
);
556 return ERROR_SUCCESS
;
559 static UINT
get_tablecolumns( MSIDATABASE
*db
, LPCWSTR szTableName
, MSICOLUMNINFO
*colinfo
, UINT
*sz
);
561 static UINT
table_get_column_info( MSIDATABASE
*db
, LPCWSTR name
, MSICOLUMNINFO
**pcols
, UINT
*pcount
)
563 UINT r
, column_count
= 0;
564 MSICOLUMNINFO
*columns
;
566 /* get the number of columns in this table */
568 r
= get_tablecolumns( db
, name
, NULL
, &column_count
);
569 if (r
!= ERROR_SUCCESS
)
572 *pcount
= column_count
;
574 /* if there's no columns, there's no table */
576 return ERROR_INVALID_PARAMETER
;
578 TRACE("table %s found\n", debugstr_w(name
));
580 columns
= msi_alloc( column_count
* sizeof(MSICOLUMNINFO
) );
582 return ERROR_FUNCTION_FAILED
;
584 r
= get_tablecolumns( db
, name
, columns
, &column_count
);
585 if (r
!= ERROR_SUCCESS
)
588 return ERROR_FUNCTION_FAILED
;
594 static UINT
get_table( MSIDATABASE
*db
, LPCWSTR name
, MSITABLE
**table_ret
)
599 /* first, see if the table is cached */
600 table
= find_cached_table( db
, name
);
604 return ERROR_SUCCESS
;
607 /* nonexistent tables should be interpreted as empty tables */
608 table
= msi_alloc( sizeof(MSITABLE
) + lstrlenW( name
) * sizeof(WCHAR
) );
610 return ERROR_FUNCTION_FAILED
;
612 table
->row_count
= 0;
614 table
->data_persistent
= NULL
;
615 table
->colinfo
= NULL
;
616 table
->col_count
= 0;
617 table
->persistent
= MSICONDITION_TRUE
;
618 lstrcpyW( table
->name
, name
);
620 if (!strcmpW( name
, szTables
) || !strcmpW( name
, szColumns
))
621 table
->persistent
= MSICONDITION_NONE
;
623 r
= table_get_column_info( db
, name
, &table
->colinfo
, &table
->col_count
);
624 if (r
!= ERROR_SUCCESS
)
629 r
= read_table_from_storage( db
, table
, db
->storage
);
630 if (r
!= ERROR_SUCCESS
)
635 list_add_head( &db
->tables
, &table
->entry
);
637 return ERROR_SUCCESS
;
640 static UINT
read_table_int( BYTE
*const *data
, UINT row
, UINT col
, UINT bytes
)
644 for (i
= 0; i
< bytes
; i
++)
645 ret
+= data
[row
][col
+ i
] << i
* 8;
650 static UINT
get_tablecolumns( MSIDATABASE
*db
, LPCWSTR szTableName
, MSICOLUMNINFO
*colinfo
, UINT
*sz
)
652 UINT r
, i
, n
= 0, table_id
, count
, maxcount
= *sz
;
653 MSITABLE
*table
= NULL
;
655 TRACE("%s\n", debugstr_w(szTableName
));
657 /* first check if there is a default table with that name */
658 r
= get_defaulttablecolumns( db
, szTableName
, colinfo
, sz
);
659 if (r
== ERROR_SUCCESS
&& *sz
)
662 r
= get_table( db
, szColumns
, &table
);
663 if (r
!= ERROR_SUCCESS
)
665 ERR("couldn't load _Columns table\n");
666 return ERROR_FUNCTION_FAILED
;
669 /* convert table and column names to IDs from the string table */
670 r
= msi_string2idW( db
->strings
, szTableName
, &table_id
);
671 if (r
!= ERROR_SUCCESS
)
673 WARN("Couldn't find id for %s\n", debugstr_w(szTableName
));
676 TRACE("Table id is %d, row count is %d\n", table_id
, table
->row_count
);
678 /* Note: _Columns table doesn't have non-persistent data */
680 /* if maxcount is non-zero, assume it's exactly right for this table */
681 memset( colinfo
, 0, maxcount
* sizeof(*colinfo
) );
682 count
= table
->row_count
;
683 for (i
= 0; i
< count
; i
++)
685 if (read_table_int( table
->data
, i
, 0, LONG_STR_BYTES
) != table_id
) continue;
688 UINT id
= read_table_int( table
->data
, i
, table
->colinfo
[2].offset
, LONG_STR_BYTES
);
689 UINT col
= read_table_int( table
->data
, i
, table
->colinfo
[1].offset
, sizeof(USHORT
) ) - (1 << 15);
691 /* check the column number is in range */
692 if (col
< 1 || col
> maxcount
)
694 ERR("column %d out of range\n", col
);
697 /* check if this column was already set */
698 if (colinfo
[col
- 1].number
)
700 ERR("duplicate column %d\n", col
);
703 colinfo
[col
- 1].tablename
= msi_string_lookup_id( db
->strings
, table_id
);
704 colinfo
[col
- 1].number
= col
;
705 colinfo
[col
- 1].colname
= msi_string_lookup_id( db
->strings
, id
);
706 colinfo
[col
- 1].type
= read_table_int( table
->data
, i
, table
->colinfo
[3].offset
,
707 sizeof(USHORT
) ) - (1 << 15);
708 colinfo
[col
- 1].offset
= 0;
709 colinfo
[col
- 1].ref_count
= 0;
710 colinfo
[col
- 1].hash_table
= NULL
;
714 TRACE("%s has %d columns\n", debugstr_w(szTableName
), n
);
716 if (colinfo
&& n
!= maxcount
)
718 ERR("missing column in table %s\n", debugstr_w(szTableName
));
719 msi_free_colinfo( colinfo
, maxcount
);
720 return ERROR_FUNCTION_FAILED
;
722 table_calc_column_offsets( db
, colinfo
, n
);
724 return ERROR_SUCCESS
;
727 UINT
msi_create_table( MSIDATABASE
*db
, LPCWSTR name
, column_info
*col_info
,
728 MSICONDITION persistent
, MSITABLE
**table_ret
)
730 enum StringPersistence string_persistence
= (persistent
) ? StringPersistent
: StringNonPersistent
;
733 MSIRECORD
*rec
= NULL
;
738 /* only add tables that don't exist already */
739 if( TABLE_Exists(db
, name
) )
741 WARN("table %s exists\n", debugstr_w(name
));
742 return ERROR_BAD_QUERY_SYNTAX
;
745 table
= msi_alloc( sizeof (MSITABLE
) + lstrlenW(name
)*sizeof (WCHAR
) );
747 return ERROR_FUNCTION_FAILED
;
749 table
->ref_count
= 1;
750 table
->row_count
= 0;
752 table
->data_persistent
= NULL
;
753 table
->colinfo
= NULL
;
754 table
->col_count
= 0;
755 table
->persistent
= persistent
;
756 lstrcpyW( table
->name
, name
);
758 for( col
= col_info
; col
; col
= col
->next
)
761 table
->colinfo
= msi_alloc( table
->col_count
* sizeof(MSICOLUMNINFO
) );
765 return ERROR_FUNCTION_FAILED
;
768 for( i
= 0, col
= col_info
; col
; i
++, col
= col
->next
)
770 UINT table_id
= msi_addstringW( db
->strings
, col
->table
, -1, 1, string_persistence
);
771 UINT col_id
= msi_addstringW( db
->strings
, col
->column
, -1, 1, string_persistence
);
773 table
->colinfo
[ i
].tablename
= msi_string_lookup_id( db
->strings
, table_id
);
774 table
->colinfo
[ i
].number
= i
+ 1;
775 table
->colinfo
[ i
].colname
= msi_string_lookup_id( db
->strings
, col_id
);
776 table
->colinfo
[ i
].type
= col
->type
;
777 table
->colinfo
[ i
].offset
= 0;
778 table
->colinfo
[ i
].ref_count
= 0;
779 table
->colinfo
[ i
].hash_table
= NULL
;
780 table
->colinfo
[ i
].temporary
= col
->temporary
;
782 table_calc_column_offsets( db
, table
->colinfo
, table
->col_count
);
784 r
= TABLE_CreateView( db
, szTables
, &tv
);
785 TRACE("CreateView returned %x\n", r
);
792 r
= tv
->ops
->execute( tv
, 0 );
793 TRACE("tv execute returned %x\n", r
);
797 rec
= MSI_CreateRecord( 1 );
801 r
= MSI_RecordSetStringW( rec
, 1, name
);
805 r
= tv
->ops
->insert_row( tv
, rec
, -1, persistent
== MSICONDITION_FALSE
);
806 TRACE("insert_row returned %x\n", r
);
810 tv
->ops
->delete( tv
);
813 msiobj_release( &rec
->hdr
);
816 if( persistent
!= MSICONDITION_FALSE
)
818 /* add each column to the _Columns table */
819 r
= TABLE_CreateView( db
, szColumns
, &tv
);
823 r
= tv
->ops
->execute( tv
, 0 );
824 TRACE("tv execute returned %x\n", r
);
828 rec
= MSI_CreateRecord( 4 );
832 r
= MSI_RecordSetStringW( rec
, 1, name
);
837 * need to set the table, column number, col name and type
838 * for each column we enter in the table
841 for( col
= col_info
; col
; col
= col
->next
)
843 r
= MSI_RecordSetInteger( rec
, 2, nField
);
847 r
= MSI_RecordSetStringW( rec
, 3, col
->column
);
851 r
= MSI_RecordSetInteger( rec
, 4, col
->type
);
855 r
= tv
->ops
->insert_row( tv
, rec
, -1, FALSE
);
867 msiobj_release( &rec
->hdr
);
868 /* FIXME: remove values from the string table on error */
870 tv
->ops
->delete( tv
);
872 if (r
== ERROR_SUCCESS
)
874 list_add_head( &db
->tables
, &table
->entry
);
883 static UINT
save_table( MSIDATABASE
*db
, const MSITABLE
*t
, UINT bytes_per_strref
)
885 BYTE
*rawdata
= NULL
;
886 UINT rawsize
, i
, j
, row_size
, row_count
;
887 UINT r
= ERROR_FUNCTION_FAILED
;
889 /* Nothing to do for non-persistent tables */
890 if( t
->persistent
== MSICONDITION_FALSE
)
891 return ERROR_SUCCESS
;
893 TRACE("Saving %s\n", debugstr_w( t
->name
) );
895 row_size
= msi_table_get_row_size( db
, t
->colinfo
, t
->col_count
, bytes_per_strref
);
896 row_count
= t
->row_count
;
897 for (i
= 0; i
< t
->row_count
; i
++)
899 if (!t
->data_persistent
[i
])
901 row_count
= 1; /* yes, this is bizarre */
905 rawsize
= row_count
* row_size
;
906 rawdata
= msi_alloc_zero( rawsize
);
909 r
= ERROR_NOT_ENOUGH_MEMORY
;
914 for (i
= 0; i
< t
->row_count
; i
++)
916 UINT ofs
= 0, ofs_mem
= 0;
918 if (!t
->data_persistent
[i
]) break;
920 for (j
= 0; j
< t
->col_count
; j
++)
922 UINT m
= bytes_per_column( db
, &t
->colinfo
[j
], LONG_STR_BYTES
);
923 UINT n
= bytes_per_column( db
, &t
->colinfo
[j
], bytes_per_strref
);
926 if (n
!= 2 && n
!= 3 && n
!= 4)
928 ERR("oops - unknown column width %d\n", n
);
931 if (t
->colinfo
[j
].type
& MSITYPE_STRING
&& n
< m
)
933 UINT id
= read_table_int( t
->data
, i
, ofs_mem
, LONG_STR_BYTES
);
934 if (id
> 1 << bytes_per_strref
* 8)
936 ERR("string id %u out of range\n", id
);
940 for (k
= 0; k
< n
; k
++)
942 rawdata
[ofs
* row_count
+ i
* n
+ k
] = t
->data
[i
][ofs_mem
+ k
];
950 TRACE("writing %d bytes\n", rawsize
);
951 r
= write_stream_data( db
->storage
, t
->name
, rawdata
, rawsize
, TRUE
);
958 static void msi_update_table_columns( MSIDATABASE
*db
, LPCWSTR name
)
961 UINT size
, offset
, old_count
;
964 table
= find_cached_table( db
, name
);
965 old_count
= table
->col_count
;
966 msi_free_colinfo( table
->colinfo
, table
->col_count
);
967 msi_free( table
->colinfo
);
968 table
->colinfo
= NULL
;
970 table_get_column_info( db
, name
, &table
->colinfo
, &table
->col_count
);
971 if (!table
->col_count
) return;
973 size
= msi_table_get_row_size( db
, table
->colinfo
, table
->col_count
, LONG_STR_BYTES
);
974 offset
= table
->colinfo
[table
->col_count
- 1].offset
;
976 for ( n
= 0; n
< table
->row_count
; n
++ )
978 table
->data
[n
] = msi_realloc( table
->data
[n
], size
);
979 if (old_count
< table
->col_count
)
980 memset( &table
->data
[n
][offset
], 0, size
- offset
);
984 /* try to find the table name in the _Tables table */
985 BOOL
TABLE_Exists( MSIDATABASE
*db
, LPCWSTR name
)
990 if( !strcmpW( name
, szTables
) || !strcmpW( name
, szColumns
) ||
991 !strcmpW( name
, szStreams
) || !strcmpW( name
, szStorages
) )
994 r
= msi_string2idW( db
->strings
, name
, &table_id
);
995 if( r
!= ERROR_SUCCESS
)
997 TRACE("Couldn't find id for %s\n", debugstr_w(name
));
1001 r
= get_table( db
, szTables
, &table
);
1002 if( r
!= ERROR_SUCCESS
)
1004 ERR("table %s not available\n", debugstr_w(szTables
));
1008 for( i
= 0; i
< table
->row_count
; i
++ )
1010 if( read_table_int( table
->data
, i
, 0, LONG_STR_BYTES
) == table_id
)
1017 /* below is the query interface to a table */
1019 typedef struct tagMSITABLEVIEW
1024 MSICOLUMNINFO
*columns
;
1025 MSIORDERINFO
*order
;
1031 static UINT
TABLE_fetch_int( struct tagMSIVIEW
*view
, UINT row
, UINT col
, UINT
*val
)
1033 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1037 return ERROR_INVALID_PARAMETER
;
1039 if( (col
==0) || (col
>tv
->num_cols
) )
1040 return ERROR_INVALID_PARAMETER
;
1042 /* how many rows are there ? */
1043 if( row
>= tv
->table
->row_count
)
1044 return ERROR_NO_MORE_ITEMS
;
1046 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1048 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1049 ERR("%p %p\n", tv
, tv
->columns
);
1050 return ERROR_FUNCTION_FAILED
;
1054 row
= tv
->order
->reorder
[row
];
1056 n
= bytes_per_column( tv
->db
, &tv
->columns
[col
- 1], LONG_STR_BYTES
);
1057 if (n
!= 2 && n
!= 3 && n
!= 4)
1059 ERR("oops! what is %d bytes per column?\n", n
);
1060 return ERROR_FUNCTION_FAILED
;
1063 offset
= tv
->columns
[col
-1].offset
;
1064 *val
= read_table_int(tv
->table
->data
, row
, offset
, n
);
1066 /* TRACE("Data [%d][%d] = %d\n", row, col, *val ); */
1068 return ERROR_SUCCESS
;
1071 static UINT
msi_stream_name( const MSITABLEVIEW
*tv
, UINT row
, LPWSTR
*pstname
)
1073 LPWSTR p
, stname
= NULL
;
1074 UINT i
, r
, type
, ival
;
1077 MSIVIEW
*view
= (MSIVIEW
*) tv
;
1079 TRACE("%p %d\n", tv
, row
);
1081 len
= lstrlenW( tv
->name
) + 1;
1082 stname
= msi_alloc( len
*sizeof(WCHAR
) );
1085 r
= ERROR_OUTOFMEMORY
;
1089 lstrcpyW( stname
, tv
->name
);
1091 for ( i
= 0; i
< tv
->num_cols
; i
++ )
1093 type
= tv
->columns
[i
].type
;
1094 if ( type
& MSITYPE_KEY
)
1098 r
= TABLE_fetch_int( view
, row
, i
+1, &ival
);
1099 if ( r
!= ERROR_SUCCESS
)
1102 if ( tv
->columns
[i
].type
& MSITYPE_STRING
)
1104 sval
= msi_string_lookup_id( tv
->db
->strings
, ival
);
1107 r
= ERROR_INVALID_PARAMETER
;
1113 static const WCHAR fmt
[] = { '%','d',0 };
1114 UINT n
= bytes_per_column( tv
->db
, &tv
->columns
[i
], LONG_STR_BYTES
);
1119 sprintfW( number
, fmt
, ival
-0x8000 );
1122 sprintfW( number
, fmt
, ival
^0x80000000 );
1125 ERR( "oops - unknown column width %d\n", n
);
1126 r
= ERROR_FUNCTION_FAILED
;
1132 len
+= lstrlenW( szDot
) + lstrlenW( sval
);
1133 p
= msi_realloc ( stname
, len
*sizeof(WCHAR
) );
1136 r
= ERROR_OUTOFMEMORY
;
1141 lstrcatW( stname
, szDot
);
1142 lstrcatW( stname
, sval
);
1149 return ERROR_SUCCESS
;
1158 * We need a special case for streams, as we need to reference column with
1159 * the name of the stream in the same table, and the table name
1160 * which may not be available at higher levels of the query
1162 static UINT
TABLE_fetch_stream( struct tagMSIVIEW
*view
, UINT row
, UINT col
, IStream
**stm
)
1164 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1166 LPWSTR encname
, full_name
= NULL
;
1168 if( !view
->ops
->fetch_int
)
1169 return ERROR_INVALID_PARAMETER
;
1171 r
= msi_stream_name( tv
, row
, &full_name
);
1172 if ( r
!= ERROR_SUCCESS
)
1174 ERR("fetching stream, error = %d\n", r
);
1178 encname
= encode_streamname( FALSE
, full_name
);
1179 r
= msi_get_raw_stream( tv
->db
, encname
, stm
);
1181 ERR("fetching stream %s, error = %d\n",debugstr_w(full_name
), r
);
1183 msi_free( full_name
);
1184 msi_free( encname
);
1188 static UINT
TABLE_set_int( MSITABLEVIEW
*tv
, UINT row
, UINT col
, UINT val
)
1193 return ERROR_INVALID_PARAMETER
;
1195 if( (col
==0) || (col
>tv
->num_cols
) )
1196 return ERROR_INVALID_PARAMETER
;
1198 if( row
>= tv
->table
->row_count
)
1199 return ERROR_INVALID_PARAMETER
;
1201 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1203 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1204 ERR("%p %p\n", tv
, tv
->columns
);
1205 return ERROR_FUNCTION_FAILED
;
1208 msi_free( tv
->columns
[col
-1].hash_table
);
1209 tv
->columns
[col
-1].hash_table
= NULL
;
1211 n
= bytes_per_column( tv
->db
, &tv
->columns
[col
- 1], LONG_STR_BYTES
);
1212 if ( n
!= 2 && n
!= 3 && n
!= 4 )
1214 ERR("oops! what is %d bytes per column?\n", n
);
1215 return ERROR_FUNCTION_FAILED
;
1218 offset
= tv
->columns
[col
-1].offset
;
1219 for ( i
= 0; i
< n
; i
++ )
1220 tv
->table
->data
[row
][offset
+ i
] = (val
>> i
* 8) & 0xff;
1222 return ERROR_SUCCESS
;
1225 static UINT
TABLE_get_row( struct tagMSIVIEW
*view
, UINT row
, MSIRECORD
**rec
)
1227 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1230 return ERROR_INVALID_PARAMETER
;
1233 row
= tv
->order
->reorder
[row
];
1235 return msi_view_get_row(tv
->db
, view
, row
, rec
);
1238 static UINT
msi_addstreamW( MSIDATABASE
*db
, LPCWSTR name
, IStream
*data
)
1240 static const WCHAR insert
[] = {
1241 'I','N','S','E','R','T',' ','I','N','T','O',' ',
1242 '`','_','S','t','r','e','a','m','s','`',' ',
1243 '(','`','N','a','m','e','`',',','`','D','a','t','a','`',')',' ',
1244 'V','A','L','U','E','S',' ','(','?',',','?',')',0};
1245 MSIQUERY
*query
= NULL
;
1249 TRACE("%p %s %p\n", db
, debugstr_w(name
), data
);
1251 rec
= MSI_CreateRecord( 2 );
1253 return ERROR_OUTOFMEMORY
;
1255 r
= MSI_RecordSetStringW( rec
, 1, name
);
1256 if ( r
!= ERROR_SUCCESS
)
1259 r
= MSI_RecordSetIStream( rec
, 2, data
);
1260 if ( r
!= ERROR_SUCCESS
)
1263 r
= MSI_DatabaseOpenViewW( db
, insert
, &query
);
1264 if ( r
!= ERROR_SUCCESS
)
1267 r
= MSI_ViewExecute( query
, rec
);
1270 msiobj_release( &query
->hdr
);
1271 msiobj_release( &rec
->hdr
);
1275 static UINT
get_table_value_from_record( MSITABLEVIEW
*tv
, MSIRECORD
*rec
, UINT iField
, UINT
*pvalue
)
1277 MSICOLUMNINFO columninfo
;
1280 if ( (iField
<= 0) ||
1281 (iField
> tv
->num_cols
) ||
1282 MSI_RecordIsNull( rec
, iField
) )
1283 return ERROR_FUNCTION_FAILED
;
1285 columninfo
= tv
->columns
[ iField
- 1 ];
1287 if ( MSITYPE_IS_BINARY(columninfo
.type
) )
1289 *pvalue
= 1; /* refers to the first key column */
1291 else if ( columninfo
.type
& MSITYPE_STRING
)
1293 LPCWSTR sval
= MSI_RecordGetString( rec
, iField
);
1296 r
= msi_string2idW(tv
->db
->strings
, sval
, pvalue
);
1297 if (r
!= ERROR_SUCCESS
)
1298 return ERROR_NOT_FOUND
;
1302 else if ( bytes_per_column( tv
->db
, &columninfo
, LONG_STR_BYTES
) == 2 )
1304 *pvalue
= 0x8000 + MSI_RecordGetInteger( rec
, iField
);
1305 if ( *pvalue
& 0xffff0000 )
1307 ERR("field %u value %d out of range\n", iField
, *pvalue
- 0x8000);
1308 return ERROR_FUNCTION_FAILED
;
1313 INT ival
= MSI_RecordGetInteger( rec
, iField
);
1314 *pvalue
= ival
^ 0x80000000;
1317 return ERROR_SUCCESS
;
1320 static UINT
TABLE_set_row( struct tagMSIVIEW
*view
, UINT row
, MSIRECORD
*rec
, UINT mask
)
1322 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1323 UINT i
, val
, r
= ERROR_SUCCESS
;
1326 return ERROR_INVALID_PARAMETER
;
1328 /* test if any of the mask bits are invalid */
1329 if ( mask
>= (1<<tv
->num_cols
) )
1330 return ERROR_INVALID_PARAMETER
;
1332 for ( i
= 0; i
< tv
->num_cols
; i
++ )
1336 /* only update the fields specified in the mask */
1337 if ( !(mask
&(1<<i
)) )
1340 persistent
= (tv
->table
->persistent
!= MSICONDITION_FALSE
) &&
1341 (tv
->table
->data_persistent
[row
]);
1342 /* FIXME: should we allow updating keys? */
1345 if ( !MSI_RecordIsNull( rec
, i
+ 1 ) )
1347 r
= get_table_value_from_record (tv
, rec
, i
+ 1, &val
);
1348 if ( MSITYPE_IS_BINARY(tv
->columns
[ i
].type
) )
1353 if ( r
!= ERROR_SUCCESS
)
1354 return ERROR_FUNCTION_FAILED
;
1356 r
= MSI_RecordGetIStream( rec
, i
+ 1, &stm
);
1357 if ( r
!= ERROR_SUCCESS
)
1360 r
= msi_stream_name( tv
, row
, &stname
);
1361 if ( r
!= ERROR_SUCCESS
)
1363 IStream_Release( stm
);
1367 r
= msi_addstreamW( tv
->db
, stname
, stm
);
1368 IStream_Release( stm
);
1369 msi_free ( stname
);
1371 if ( r
!= ERROR_SUCCESS
)
1374 else if ( tv
->columns
[i
].type
& MSITYPE_STRING
)
1378 if ( r
!= ERROR_SUCCESS
)
1380 LPCWSTR sval
= MSI_RecordGetString( rec
, i
+ 1 );
1381 val
= msi_addstringW( tv
->db
->strings
, sval
, -1, 1,
1382 persistent
? StringPersistent
: StringNonPersistent
);
1386 TABLE_fetch_int(&tv
->view
, row
, i
+ 1, &x
);
1393 if ( r
!= ERROR_SUCCESS
)
1394 return ERROR_FUNCTION_FAILED
;
1398 r
= TABLE_set_int( tv
, row
, i
+1, val
);
1399 if ( r
!= ERROR_SUCCESS
)
1405 static UINT
table_create_new_row( struct tagMSIVIEW
*view
, UINT
*num
, BOOL temporary
)
1407 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1412 BOOL
**data_persist_ptr
;
1415 TRACE("%p %s\n", view
, temporary
? "TRUE" : "FALSE");
1418 return ERROR_INVALID_PARAMETER
;
1420 row
= msi_alloc_zero( tv
->row_size
);
1422 return ERROR_NOT_ENOUGH_MEMORY
;
1424 row_count
= &tv
->table
->row_count
;
1425 data_ptr
= &tv
->table
->data
;
1426 data_persist_ptr
= &tv
->table
->data_persistent
;
1428 *num
= tv
->table
->row_count
;
1430 sz
= (*row_count
+ 1) * sizeof (BYTE
*);
1432 p
= msi_realloc( *data_ptr
, sz
);
1434 p
= msi_alloc( sz
);
1438 return ERROR_NOT_ENOUGH_MEMORY
;
1441 sz
= (*row_count
+ 1) * sizeof (BOOL
);
1442 if( *data_persist_ptr
)
1443 b
= msi_realloc( *data_persist_ptr
, sz
);
1445 b
= msi_alloc( sz
);
1450 return ERROR_NOT_ENOUGH_MEMORY
;
1454 (*data_ptr
)[*row_count
] = row
;
1456 *data_persist_ptr
= b
;
1457 (*data_persist_ptr
)[*row_count
] = !temporary
;
1461 return ERROR_SUCCESS
;
1464 static UINT
TABLE_execute( struct tagMSIVIEW
*view
, MSIRECORD
*record
)
1466 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1468 TRACE("%p %p\n", tv
, record
);
1470 TRACE("There are %d columns\n", tv
->num_cols
);
1472 return ERROR_SUCCESS
;
1475 static UINT
TABLE_close( struct tagMSIVIEW
*view
)
1477 TRACE("%p\n", view
);
1479 return ERROR_SUCCESS
;
1482 static UINT
TABLE_get_dimensions( struct tagMSIVIEW
*view
, UINT
*rows
, UINT
*cols
)
1484 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1486 TRACE("%p %p %p\n", view
, rows
, cols
);
1489 *cols
= tv
->num_cols
;
1493 return ERROR_INVALID_PARAMETER
;
1494 *rows
= tv
->table
->row_count
;
1497 return ERROR_SUCCESS
;
1500 static UINT
TABLE_get_column_info( struct tagMSIVIEW
*view
,
1501 UINT n
, LPCWSTR
*name
, UINT
*type
, BOOL
*temporary
,
1502 LPCWSTR
*table_name
)
1504 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1506 TRACE("%p %d %p %p\n", tv
, n
, name
, type
);
1508 if( ( n
== 0 ) || ( n
> tv
->num_cols
) )
1509 return ERROR_INVALID_PARAMETER
;
1513 *name
= tv
->columns
[n
-1].colname
;
1515 return ERROR_FUNCTION_FAILED
;
1520 *table_name
= tv
->columns
[n
-1].tablename
;
1522 return ERROR_FUNCTION_FAILED
;
1526 *type
= tv
->columns
[n
-1].type
;
1529 *temporary
= tv
->columns
[n
-1].temporary
;
1531 return ERROR_SUCCESS
;
1534 static UINT
msi_table_find_row( MSITABLEVIEW
*tv
, MSIRECORD
*rec
, UINT
*row
, UINT
*column
);
1536 static UINT
table_validate_new( MSITABLEVIEW
*tv
, MSIRECORD
*rec
, UINT
*column
)
1540 /* check there's no null values where they're not allowed */
1541 for( i
= 0; i
< tv
->num_cols
; i
++ )
1543 if ( tv
->columns
[i
].type
& MSITYPE_NULLABLE
)
1546 if ( MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
1547 TRACE("skipping binary column\n");
1548 else if ( tv
->columns
[i
].type
& MSITYPE_STRING
)
1552 str
= MSI_RecordGetString( rec
, i
+1 );
1553 if (str
== NULL
|| str
[0] == 0)
1555 if (column
) *column
= i
;
1556 return ERROR_INVALID_DATA
;
1563 n
= MSI_RecordGetInteger( rec
, i
+1 );
1564 if (n
== MSI_NULL_INTEGER
)
1566 if (column
) *column
= i
;
1567 return ERROR_INVALID_DATA
;
1572 /* check there's no duplicate keys */
1573 r
= msi_table_find_row( tv
, rec
, &row
, column
);
1574 if (r
== ERROR_SUCCESS
)
1575 return ERROR_FUNCTION_FAILED
;
1577 return ERROR_SUCCESS
;
1580 static int compare_record( MSITABLEVIEW
*tv
, UINT row
, MSIRECORD
*rec
)
1582 UINT r
, i
, ivalue
, x
;
1584 for (i
= 0; i
< tv
->num_cols
; i
++ )
1586 if (!(tv
->columns
[i
].type
& MSITYPE_KEY
)) continue;
1588 r
= get_table_value_from_record( tv
, rec
, i
+ 1, &ivalue
);
1589 if (r
!= ERROR_SUCCESS
)
1592 r
= TABLE_fetch_int( &tv
->view
, row
, i
+ 1, &x
);
1593 if (r
!= ERROR_SUCCESS
)
1595 WARN("TABLE_fetch_int should not fail here %u\n", r
);
1602 else if (ivalue
== x
)
1604 if (i
< tv
->num_cols
- 1) continue;
1613 static int find_insert_index( MSITABLEVIEW
*tv
, MSIRECORD
*rec
)
1615 int idx
, c
, low
= 0, high
= tv
->table
->row_count
- 1;
1617 TRACE("%p %p\n", tv
, rec
);
1621 idx
= (low
+ high
) / 2;
1622 c
= compare_record( tv
, idx
, rec
);
1630 TRACE("found %u\n", idx
);
1634 TRACE("found %u\n", high
+ 1);
1638 static UINT
TABLE_insert_row( struct tagMSIVIEW
*view
, MSIRECORD
*rec
, UINT row
, BOOL temporary
)
1640 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1643 TRACE("%p %p %s\n", tv
, rec
, temporary
? "TRUE" : "FALSE" );
1645 /* check that the key is unique - can we find a matching row? */
1646 r
= table_validate_new( tv
, rec
, NULL
);
1647 if( r
!= ERROR_SUCCESS
)
1648 return ERROR_FUNCTION_FAILED
;
1651 row
= find_insert_index( tv
, rec
);
1653 r
= table_create_new_row( view
, &row
, temporary
);
1654 TRACE("insert_row returned %08x\n", r
);
1655 if( r
!= ERROR_SUCCESS
)
1658 /* shift the rows to make room for the new row */
1659 for (i
= tv
->table
->row_count
- 1; i
> row
; i
--)
1661 memmove(&(tv
->table
->data
[i
][0]),
1662 &(tv
->table
->data
[i
- 1][0]), tv
->row_size
);
1663 tv
->table
->data_persistent
[i
] = tv
->table
->data_persistent
[i
- 1];
1666 /* Re-set the persistence flag */
1667 tv
->table
->data_persistent
[row
] = !temporary
;
1668 return TABLE_set_row( view
, row
, rec
, (1<<tv
->num_cols
) - 1 );
1671 static UINT
TABLE_delete_row( struct tagMSIVIEW
*view
, UINT row
)
1673 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1674 UINT r
, num_rows
, num_cols
, i
;
1676 TRACE("%p %d\n", tv
, row
);
1679 return ERROR_INVALID_PARAMETER
;
1681 r
= TABLE_get_dimensions( view
, &num_rows
, &num_cols
);
1682 if ( r
!= ERROR_SUCCESS
)
1685 if ( row
>= num_rows
)
1686 return ERROR_FUNCTION_FAILED
;
1688 num_rows
= tv
->table
->row_count
;
1689 tv
->table
->row_count
--;
1691 /* reset the hash tables */
1692 for (i
= 0; i
< tv
->num_cols
; i
++)
1694 msi_free( tv
->columns
[i
].hash_table
);
1695 tv
->columns
[i
].hash_table
= NULL
;
1698 for (i
= row
+ 1; i
< num_rows
; i
++)
1700 memcpy(tv
->table
->data
[i
- 1], tv
->table
->data
[i
], tv
->row_size
);
1701 tv
->table
->data_persistent
[i
- 1] = tv
->table
->data_persistent
[i
];
1704 msi_free(tv
->table
->data
[num_rows
- 1]);
1706 return ERROR_SUCCESS
;
1709 static UINT
msi_table_update(struct tagMSIVIEW
*view
, MSIRECORD
*rec
, UINT row
)
1711 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1714 /* FIXME: MsiViewFetch should set rec index 0 to some ID that
1715 * sets the fetched record apart from other records
1719 return ERROR_INVALID_PARAMETER
;
1721 r
= msi_table_find_row(tv
, rec
, &new_row
, NULL
);
1722 if (r
!= ERROR_SUCCESS
)
1724 ERR("can't find row to modify\n");
1725 return ERROR_FUNCTION_FAILED
;
1728 /* the row cannot be changed */
1729 if (row
!= new_row
+ 1)
1730 return ERROR_FUNCTION_FAILED
;
1733 new_row
= tv
->order
->reorder
[new_row
];
1735 return TABLE_set_row(view
, new_row
, rec
, (1 << tv
->num_cols
) - 1);
1738 static UINT
msi_table_assign(struct tagMSIVIEW
*view
, MSIRECORD
*rec
)
1740 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1744 return ERROR_INVALID_PARAMETER
;
1746 r
= msi_table_find_row(tv
, rec
, &row
, NULL
);
1747 if (r
== ERROR_SUCCESS
)
1748 return TABLE_set_row(view
, row
, rec
, (1 << tv
->num_cols
) - 1);
1750 return TABLE_insert_row( view
, rec
, -1, FALSE
);
1753 static UINT
modify_delete_row( struct tagMSIVIEW
*view
, MSIRECORD
*rec
)
1755 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1758 r
= msi_table_find_row(tv
, rec
, &row
, NULL
);
1759 if (r
!= ERROR_SUCCESS
)
1762 return TABLE_delete_row(view
, row
);
1765 static UINT
msi_refresh_record( struct tagMSIVIEW
*view
, MSIRECORD
*rec
, UINT row
)
1770 r
= TABLE_get_row(view
, row
- 1, &curr
);
1771 if (r
!= ERROR_SUCCESS
)
1774 /* Close the original record */
1775 MSI_CloseRecord(&rec
->hdr
);
1777 count
= MSI_RecordGetFieldCount(rec
);
1778 for (i
= 0; i
< count
; i
++)
1779 MSI_RecordCopyField(curr
, i
+ 1, rec
, i
+ 1);
1781 msiobj_release(&curr
->hdr
);
1782 return ERROR_SUCCESS
;
1785 static UINT
TABLE_modify( struct tagMSIVIEW
*view
, MSIMODIFY eModifyMode
,
1786 MSIRECORD
*rec
, UINT row
)
1788 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1791 TRACE("%p %d %p\n", view
, eModifyMode
, rec
);
1793 switch (eModifyMode
)
1795 case MSIMODIFY_DELETE
:
1796 r
= modify_delete_row( view
, rec
);
1798 case MSIMODIFY_VALIDATE_NEW
:
1799 r
= table_validate_new( tv
, rec
, &column
);
1800 if (r
!= ERROR_SUCCESS
)
1802 tv
->view
.error
= MSIDBERROR_DUPLICATEKEY
;
1803 tv
->view
.error_column
= tv
->columns
[column
].colname
;
1804 r
= ERROR_INVALID_DATA
;
1808 case MSIMODIFY_INSERT
:
1809 r
= table_validate_new( tv
, rec
, NULL
);
1810 if (r
!= ERROR_SUCCESS
)
1812 r
= TABLE_insert_row( view
, rec
, -1, FALSE
);
1815 case MSIMODIFY_INSERT_TEMPORARY
:
1816 r
= table_validate_new( tv
, rec
, NULL
);
1817 if (r
!= ERROR_SUCCESS
)
1819 r
= TABLE_insert_row( view
, rec
, -1, TRUE
);
1822 case MSIMODIFY_REFRESH
:
1823 r
= msi_refresh_record( view
, rec
, row
);
1826 case MSIMODIFY_UPDATE
:
1827 r
= msi_table_update( view
, rec
, row
);
1830 case MSIMODIFY_ASSIGN
:
1831 r
= msi_table_assign( view
, rec
);
1834 case MSIMODIFY_REPLACE
:
1835 case MSIMODIFY_MERGE
:
1836 case MSIMODIFY_VALIDATE
:
1837 case MSIMODIFY_VALIDATE_FIELD
:
1838 case MSIMODIFY_VALIDATE_DELETE
:
1839 FIXME("%p %d %p - mode not implemented\n", view
, eModifyMode
, rec
);
1840 r
= ERROR_CALL_NOT_IMPLEMENTED
;
1844 r
= ERROR_INVALID_DATA
;
1850 static UINT
TABLE_delete( struct tagMSIVIEW
*view
)
1852 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1854 TRACE("%p\n", view
);
1861 msi_free( tv
->order
->reorder
);
1862 msi_free( tv
->order
);
1868 return ERROR_SUCCESS
;
1871 static UINT
TABLE_find_matching_rows( struct tagMSIVIEW
*view
, UINT col
,
1872 UINT val
, UINT
*row
, MSIITERHANDLE
*handle
)
1874 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1875 const MSICOLUMNHASHENTRY
*entry
;
1877 TRACE("%p, %d, %u, %p\n", view
, col
, val
, *handle
);
1880 return ERROR_INVALID_PARAMETER
;
1882 if( (col
==0) || (col
> tv
->num_cols
) )
1883 return ERROR_INVALID_PARAMETER
;
1885 if( !tv
->columns
[col
-1].hash_table
)
1888 UINT num_rows
= tv
->table
->row_count
;
1889 MSICOLUMNHASHENTRY
**hash_table
;
1890 MSICOLUMNHASHENTRY
*new_entry
;
1892 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1894 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1895 ERR("%p %p\n", tv
, tv
->columns
);
1896 return ERROR_FUNCTION_FAILED
;
1899 /* allocate contiguous memory for the table and its entries so we
1900 * don't have to do an expensive cleanup */
1901 hash_table
= msi_alloc(MSITABLE_HASH_TABLE_SIZE
* sizeof(MSICOLUMNHASHENTRY
*) +
1902 num_rows
* sizeof(MSICOLUMNHASHENTRY
));
1904 return ERROR_OUTOFMEMORY
;
1906 memset(hash_table
, 0, MSITABLE_HASH_TABLE_SIZE
* sizeof(MSICOLUMNHASHENTRY
*));
1907 tv
->columns
[col
-1].hash_table
= hash_table
;
1909 new_entry
= (MSICOLUMNHASHENTRY
*)(hash_table
+ MSITABLE_HASH_TABLE_SIZE
);
1911 for (i
= 0; i
< num_rows
; i
++, new_entry
++)
1915 if (view
->ops
->fetch_int( view
, i
, col
, &row_value
) != ERROR_SUCCESS
)
1918 new_entry
->next
= NULL
;
1919 new_entry
->value
= row_value
;
1921 if (hash_table
[row_value
% MSITABLE_HASH_TABLE_SIZE
])
1923 MSICOLUMNHASHENTRY
*prev_entry
= hash_table
[row_value
% MSITABLE_HASH_TABLE_SIZE
];
1924 while (prev_entry
->next
)
1925 prev_entry
= prev_entry
->next
;
1926 prev_entry
->next
= new_entry
;
1929 hash_table
[row_value
% MSITABLE_HASH_TABLE_SIZE
] = new_entry
;
1934 entry
= tv
->columns
[col
-1].hash_table
[val
% MSITABLE_HASH_TABLE_SIZE
];
1936 entry
= (*handle
)->next
;
1938 while (entry
&& entry
->value
!= val
)
1939 entry
= entry
->next
;
1943 return ERROR_NO_MORE_ITEMS
;
1947 return ERROR_SUCCESS
;
1950 static UINT
TABLE_add_ref(struct tagMSIVIEW
*view
)
1952 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1955 TRACE("%p %d\n", view
, tv
->table
->ref_count
);
1957 for (i
= 0; i
< tv
->table
->col_count
; i
++)
1959 if (tv
->table
->colinfo
[i
].type
& MSITYPE_TEMPORARY
)
1960 InterlockedIncrement(&tv
->table
->colinfo
[i
].ref_count
);
1963 return InterlockedIncrement(&tv
->table
->ref_count
);
1966 static UINT
TABLE_remove_column(struct tagMSIVIEW
*view
, LPCWSTR table
, UINT number
)
1968 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1969 MSIRECORD
*rec
= NULL
;
1970 MSIVIEW
*columns
= NULL
;
1973 rec
= MSI_CreateRecord(2);
1975 return ERROR_OUTOFMEMORY
;
1977 MSI_RecordSetStringW(rec
, 1, table
);
1978 MSI_RecordSetInteger(rec
, 2, number
);
1980 r
= TABLE_CreateView(tv
->db
, szColumns
, &columns
);
1981 if (r
!= ERROR_SUCCESS
)
1984 r
= msi_table_find_row((MSITABLEVIEW
*)columns
, rec
, &row
, NULL
);
1985 if (r
!= ERROR_SUCCESS
)
1988 r
= TABLE_delete_row(columns
, row
);
1989 if (r
!= ERROR_SUCCESS
)
1992 msi_update_table_columns(tv
->db
, table
);
1995 msiobj_release(&rec
->hdr
);
1996 columns
->ops
->delete(columns
);
2000 static UINT
TABLE_release(struct tagMSIVIEW
*view
)
2002 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
2003 INT ref
= tv
->table
->ref_count
;
2006 TRACE("%p %d\n", view
, ref
);
2008 for (i
= 0; i
< tv
->table
->col_count
; i
++)
2010 if (tv
->table
->colinfo
[i
].type
& MSITYPE_TEMPORARY
)
2012 ref
= InterlockedDecrement(&tv
->table
->colinfo
[i
].ref_count
);
2015 r
= TABLE_remove_column(view
, tv
->table
->colinfo
[i
].tablename
,
2016 tv
->table
->colinfo
[i
].number
);
2017 if (r
!= ERROR_SUCCESS
)
2023 ref
= InterlockedDecrement(&tv
->table
->ref_count
);
2026 if (!tv
->table
->row_count
)
2028 list_remove(&tv
->table
->entry
);
2029 free_table(tv
->table
);
2037 static UINT
TABLE_add_column(struct tagMSIVIEW
*view
, LPCWSTR table
, UINT number
,
2038 LPCWSTR column
, UINT type
, BOOL hold
)
2040 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
2045 rec
= MSI_CreateRecord(4);
2047 return ERROR_OUTOFMEMORY
;
2049 MSI_RecordSetStringW(rec
, 1, table
);
2050 MSI_RecordSetInteger(rec
, 2, number
);
2051 MSI_RecordSetStringW(rec
, 3, column
);
2052 MSI_RecordSetInteger(rec
, 4, type
);
2054 r
= TABLE_insert_row(&tv
->view
, rec
, -1, FALSE
);
2055 if (r
!= ERROR_SUCCESS
)
2058 msi_update_table_columns(tv
->db
, table
);
2063 msitable
= find_cached_table(tv
->db
, table
);
2064 for (i
= 0; i
< msitable
->col_count
; i
++)
2066 if (!strcmpW( msitable
->colinfo
[i
].colname
, column
))
2068 InterlockedIncrement(&msitable
->colinfo
[i
].ref_count
);
2074 msiobj_release(&rec
->hdr
);
2078 static UINT
order_add_column(struct tagMSIVIEW
*view
, MSIORDERINFO
*order
, LPCWSTR name
)
2081 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
2083 r
= TABLE_get_dimensions(view
, NULL
, &count
);
2084 if (r
!= ERROR_SUCCESS
)
2087 if (order
->num_cols
>= count
)
2088 return ERROR_FUNCTION_FAILED
;
2090 r
= VIEW_find_column(view
, name
, tv
->name
, &n
);
2091 if (r
!= ERROR_SUCCESS
)
2094 order
->cols
[order
->num_cols
] = n
;
2095 TRACE("Ordering by column %s (%d)\n", debugstr_w(name
), n
);
2099 return ERROR_SUCCESS
;
2102 static UINT
order_compare(struct tagMSIVIEW
*view
, MSIORDERINFO
*order
,
2103 UINT a
, UINT b
, UINT
*swap
)
2105 UINT r
, i
, a_val
= 0, b_val
= 0;
2108 for (i
= 0; i
< order
->num_cols
; i
++)
2110 r
= TABLE_fetch_int(view
, a
, order
->cols
[i
], &a_val
);
2111 if (r
!= ERROR_SUCCESS
)
2114 r
= TABLE_fetch_int(view
, b
, order
->cols
[i
], &b_val
);
2115 if (r
!= ERROR_SUCCESS
)
2126 return ERROR_SUCCESS
;
2129 static UINT
order_mergesort(struct tagMSIVIEW
*view
, MSIORDERINFO
*order
,
2130 UINT left
, UINT right
)
2133 UINT swap
= 0, center
= (left
+ right
) / 2;
2134 UINT
*array
= order
->reorder
;
2137 return ERROR_SUCCESS
;
2139 /* sort the left half */
2140 r
= order_mergesort(view
, order
, left
, center
);
2141 if (r
!= ERROR_SUCCESS
)
2144 /* sort the right half */
2145 r
= order_mergesort(view
, order
, center
+ 1, right
);
2146 if (r
!= ERROR_SUCCESS
)
2149 for (i
= left
, j
= center
+ 1; (i
<= center
) && (j
<= right
); i
++)
2151 r
= order_compare(view
, order
, array
[i
], array
[j
], &swap
);
2152 if (r
!= ERROR_SUCCESS
)
2158 memmove(&array
[i
+ 1], &array
[i
], (j
- i
) * sizeof(UINT
));
2165 return ERROR_SUCCESS
;
2168 static UINT
order_verify(struct tagMSIVIEW
*view
, MSIORDERINFO
*order
, UINT num_rows
)
2172 for (i
= 1; i
< num_rows
; i
++)
2174 r
= order_compare(view
, order
, order
->reorder
[i
- 1],
2175 order
->reorder
[i
], &swap
);
2176 if (r
!= ERROR_SUCCESS
)
2182 ERR("Bad order! %d\n", i
);
2183 return ERROR_FUNCTION_FAILED
;
2186 return ERROR_SUCCESS
;
2189 static UINT
TABLE_sort(struct tagMSIVIEW
*view
, column_info
*columns
)
2191 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
2192 MSIORDERINFO
*order
;
2197 TRACE("sorting table %s\n", debugstr_w(tv
->name
));
2199 r
= TABLE_get_dimensions(view
, &rows
, &cols
);
2200 if (r
!= ERROR_SUCCESS
)
2204 return ERROR_SUCCESS
;
2206 order
= msi_alloc_zero(sizeof(MSIORDERINFO
) + sizeof(UINT
) * cols
);
2208 return ERROR_OUTOFMEMORY
;
2210 for (ptr
= columns
; ptr
; ptr
= ptr
->next
)
2211 order_add_column(view
, order
, ptr
->column
);
2213 order
->reorder
= msi_alloc(rows
* sizeof(UINT
));
2214 if (!order
->reorder
)
2215 return ERROR_OUTOFMEMORY
;
2217 for (i
= 0; i
< rows
; i
++)
2218 order
->reorder
[i
] = i
;
2220 r
= order_mergesort(view
, order
, 0, rows
- 1);
2221 if (r
!= ERROR_SUCCESS
)
2224 r
= order_verify(view
, order
, rows
);
2225 if (r
!= ERROR_SUCCESS
)
2230 return ERROR_SUCCESS
;
2233 static UINT
TABLE_drop(struct tagMSIVIEW
*view
)
2235 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
2236 MSIVIEW
*tables
= NULL
;
2237 MSIRECORD
*rec
= NULL
;
2241 TRACE("dropping table %s\n", debugstr_w(tv
->name
));
2243 for (i
= tv
->table
->col_count
- 1; i
>= 0; i
--)
2245 r
= TABLE_remove_column(view
, tv
->table
->colinfo
[i
].tablename
,
2246 tv
->table
->colinfo
[i
].number
);
2247 if (r
!= ERROR_SUCCESS
)
2251 rec
= MSI_CreateRecord(1);
2253 return ERROR_OUTOFMEMORY
;
2255 MSI_RecordSetStringW(rec
, 1, tv
->name
);
2257 r
= TABLE_CreateView(tv
->db
, szTables
, &tables
);
2258 if (r
!= ERROR_SUCCESS
)
2261 r
= msi_table_find_row((MSITABLEVIEW
*)tables
, rec
, &row
, NULL
);
2262 if (r
!= ERROR_SUCCESS
)
2265 r
= TABLE_delete_row(tables
, row
);
2266 if (r
!= ERROR_SUCCESS
)
2269 list_remove(&tv
->table
->entry
);
2270 free_table(tv
->table
);
2273 msiobj_release(&rec
->hdr
);
2274 tables
->ops
->delete(tables
);
2279 static const MSIVIEWOPS table_ops
=
2289 TABLE_get_dimensions
,
2290 TABLE_get_column_info
,
2293 TABLE_find_matching_rows
,
2297 TABLE_remove_column
,
2302 UINT
TABLE_CreateView( MSIDATABASE
*db
, LPCWSTR name
, MSIVIEW
**view
)
2307 TRACE("%p %s %p\n", db
, debugstr_w(name
), view
);
2309 if ( !strcmpW( name
, szStreams
) )
2310 return STREAMS_CreateView( db
, view
);
2311 else if ( !strcmpW( name
, szStorages
) )
2312 return STORAGES_CreateView( db
, view
);
2314 sz
= sizeof *tv
+ lstrlenW(name
)*sizeof name
[0] ;
2315 tv
= msi_alloc_zero( sz
);
2317 return ERROR_FUNCTION_FAILED
;
2319 r
= get_table( db
, name
, &tv
->table
);
2320 if( r
!= ERROR_SUCCESS
)
2323 WARN("table not found\n");
2327 TRACE("table %p found with %d columns\n", tv
->table
, tv
->table
->col_count
);
2329 /* fill the structure */
2330 tv
->view
.ops
= &table_ops
;
2332 tv
->columns
= tv
->table
->colinfo
;
2333 tv
->num_cols
= tv
->table
->col_count
;
2334 tv
->row_size
= msi_table_get_row_size( db
, tv
->table
->colinfo
, tv
->table
->col_count
, LONG_STR_BYTES
);
2336 TRACE("%s one row is %d bytes\n", debugstr_w(name
), tv
->row_size
);
2338 *view
= (MSIVIEW
*) tv
;
2339 lstrcpyW( tv
->name
, name
);
2341 return ERROR_SUCCESS
;
2344 UINT
MSI_CommitTables( MSIDATABASE
*db
)
2346 UINT r
, bytes_per_strref
;
2348 MSITABLE
*table
= NULL
;
2352 r
= msi_save_string_table( db
->strings
, db
->storage
, &bytes_per_strref
);
2353 if( r
!= ERROR_SUCCESS
)
2355 WARN("failed to save string table r=%08x\n",r
);
2359 LIST_FOR_EACH_ENTRY( table
, &db
->tables
, MSITABLE
, entry
)
2361 r
= save_table( db
, table
, bytes_per_strref
);
2362 if( r
!= ERROR_SUCCESS
)
2364 WARN("failed to save table %s (r=%08x)\n",
2365 debugstr_w(table
->name
), r
);
2370 /* force everything to reload next time */
2371 free_cached_tables( db
);
2373 hr
= IStorage_Commit( db
->storage
, 0 );
2376 WARN("failed to commit changes 0x%08x\n", hr
);
2377 r
= ERROR_FUNCTION_FAILED
;
2382 MSICONDITION
MSI_DatabaseIsTablePersistent( MSIDATABASE
*db
, LPCWSTR table
)
2387 TRACE("%p %s\n", db
, debugstr_w(table
));
2390 return MSICONDITION_ERROR
;
2392 r
= get_table( db
, table
, &t
);
2393 if (r
!= ERROR_SUCCESS
)
2394 return MSICONDITION_NONE
;
2396 return t
->persistent
;
2399 static UINT
read_raw_int(const BYTE
*data
, UINT col
, UINT bytes
)
2403 for (i
= 0; i
< bytes
; i
++)
2404 ret
+= (data
[col
+ i
] << i
* 8);
2409 static UINT
msi_record_encoded_stream_name( const MSITABLEVIEW
*tv
, MSIRECORD
*rec
, LPWSTR
*pstname
)
2411 LPWSTR stname
= NULL
, sval
, p
;
2415 TRACE("%p %p\n", tv
, rec
);
2417 len
= lstrlenW( tv
->name
) + 1;
2418 stname
= msi_alloc( len
*sizeof(WCHAR
) );
2421 r
= ERROR_OUTOFMEMORY
;
2425 lstrcpyW( stname
, tv
->name
);
2427 for ( i
= 0; i
< tv
->num_cols
; i
++ )
2429 if ( tv
->columns
[i
].type
& MSITYPE_KEY
)
2431 sval
= msi_dup_record_field( rec
, i
+ 1 );
2434 r
= ERROR_OUTOFMEMORY
;
2438 len
+= lstrlenW( szDot
) + lstrlenW ( sval
);
2439 p
= msi_realloc ( stname
, len
*sizeof(WCHAR
) );
2442 r
= ERROR_OUTOFMEMORY
;
2447 lstrcatW( stname
, szDot
);
2448 lstrcatW( stname
, sval
);
2456 *pstname
= encode_streamname( FALSE
, stname
);
2459 return ERROR_SUCCESS
;
2462 msi_free ( stname
);
2467 static MSIRECORD
*msi_get_transform_record( const MSITABLEVIEW
*tv
, const string_table
*st
,
2469 const BYTE
*rawdata
, UINT bytes_per_strref
)
2471 UINT i
, val
, ofs
= 0;
2473 MSICOLUMNINFO
*columns
= tv
->columns
;
2476 mask
= rawdata
[0] | (rawdata
[1] << 8);
2479 rec
= MSI_CreateRecord( tv
->num_cols
);
2484 for( i
=0; i
<tv
->num_cols
; i
++ )
2486 if ( (mask
&1) && (i
>=(mask
>>8)) )
2488 /* all keys must be present */
2489 if ( (~mask
&1) && (~columns
[i
].type
& MSITYPE_KEY
) && ((1<<i
) & ~mask
) )
2492 if( MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
2495 IStream
*stm
= NULL
;
2498 ofs
+= bytes_per_column( tv
->db
, &columns
[i
], bytes_per_strref
);
2500 r
= msi_record_encoded_stream_name( tv
, rec
, &encname
);
2501 if ( r
!= ERROR_SUCCESS
)
2504 r
= IStorage_OpenStream( stg
, encname
, NULL
,
2505 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, &stm
);
2506 msi_free( encname
);
2507 if ( r
!= ERROR_SUCCESS
)
2510 MSI_RecordSetStream( rec
, i
+1, stm
);
2511 TRACE(" field %d [%s]\n", i
+1, debugstr_w(encname
));
2513 else if( columns
[i
].type
& MSITYPE_STRING
)
2517 val
= read_raw_int(rawdata
, ofs
, bytes_per_strref
);
2518 sval
= msi_string_lookup_id( st
, val
);
2519 MSI_RecordSetStringW( rec
, i
+1, sval
);
2520 TRACE(" field %d [%s]\n", i
+1, debugstr_w(sval
));
2521 ofs
+= bytes_per_strref
;
2525 UINT n
= bytes_per_column( tv
->db
, &columns
[i
], bytes_per_strref
);
2529 val
= read_raw_int(rawdata
, ofs
, n
);
2531 MSI_RecordSetInteger( rec
, i
+1, val
-0x8000 );
2532 TRACE(" field %d [0x%04x]\n", i
+1, val
);
2535 val
= read_raw_int(rawdata
, ofs
, n
);
2537 MSI_RecordSetInteger( rec
, i
+1, val
^0x80000000 );
2538 TRACE(" field %d [0x%08x]\n", i
+1, val
);
2541 ERR("oops - unknown column width %d\n", n
);
2550 static void dump_record( MSIRECORD
*rec
)
2554 n
= MSI_RecordGetFieldCount( rec
);
2555 for( i
=1; i
<=n
; i
++ )
2559 if( MSI_RecordIsNull( rec
, i
) )
2560 TRACE("row -> []\n");
2561 else if( (sval
= MSI_RecordGetString( rec
, i
)) )
2562 TRACE("row -> [%s]\n", debugstr_w(sval
));
2564 TRACE("row -> [0x%08x]\n", MSI_RecordGetInteger( rec
, i
) );
2568 static void dump_table( const string_table
*st
, const USHORT
*rawdata
, UINT rawsize
)
2573 for( i
=0; i
<(rawsize
/2); i
++ )
2575 sval
= msi_string_lookup_id( st
, rawdata
[i
] );
2576 MESSAGE(" %04x %s\n", rawdata
[i
], debugstr_w(sval
) );
2580 static UINT
* msi_record_to_row( const MSITABLEVIEW
*tv
, MSIRECORD
*rec
)
2585 data
= msi_alloc( tv
->num_cols
*sizeof (UINT
) );
2586 for( i
=0; i
<tv
->num_cols
; i
++ )
2590 if ( ~tv
->columns
[i
].type
& MSITYPE_KEY
)
2593 /* turn the transform column value into a row value */
2594 if ( ( tv
->columns
[i
].type
& MSITYPE_STRING
) &&
2595 ! MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
2597 str
= MSI_RecordGetString( rec
, i
+1 );
2600 r
= msi_string2idW( tv
->db
->strings
, str
, &data
[i
] );
2602 /* if there's no matching string in the string table,
2603 these keys can't match any record, so fail now. */
2604 if (r
!= ERROR_SUCCESS
)
2614 data
[i
] = MSI_RecordGetInteger( rec
, i
+1 );
2616 if (data
[i
] == MSI_NULL_INTEGER
)
2618 else if ((tv
->columns
[i
].type
&0xff) == 2)
2621 data
[i
] += 0x80000000;
2627 static UINT
msi_row_matches( MSITABLEVIEW
*tv
, UINT row
, const UINT
*data
, UINT
*column
)
2629 UINT i
, r
, x
, ret
= ERROR_FUNCTION_FAILED
;
2631 for( i
=0; i
<tv
->num_cols
; i
++ )
2633 if ( ~tv
->columns
[i
].type
& MSITYPE_KEY
)
2636 /* turn the transform column value into a row value */
2637 r
= TABLE_fetch_int( &tv
->view
, row
, i
+1, &x
);
2638 if ( r
!= ERROR_SUCCESS
)
2640 ERR("TABLE_fetch_int shouldn't fail here\n");
2644 /* if this key matches, move to the next column */
2647 ret
= ERROR_FUNCTION_FAILED
;
2650 if (column
) *column
= i
;
2651 ret
= ERROR_SUCCESS
;
2656 static UINT
msi_table_find_row( MSITABLEVIEW
*tv
, MSIRECORD
*rec
, UINT
*row
, UINT
*column
)
2658 UINT i
, r
= ERROR_FUNCTION_FAILED
, *data
;
2660 data
= msi_record_to_row( tv
, rec
);
2663 for( i
= 0; i
< tv
->table
->row_count
; i
++ )
2665 r
= msi_row_matches( tv
, i
, data
, column
);
2666 if( r
== ERROR_SUCCESS
)
2682 static UINT
msi_table_load_transform( MSIDATABASE
*db
, IStorage
*stg
,
2683 string_table
*st
, TRANSFORMDATA
*transform
,
2684 UINT bytes_per_strref
)
2687 BYTE
*rawdata
= NULL
;
2688 MSITABLEVIEW
*tv
= NULL
;
2689 UINT r
, n
, sz
, i
, mask
;
2690 MSIRECORD
*rec
= NULL
;
2696 return ERROR_SUCCESS
;
2698 name
= transform
->name
;
2701 TRACE("%p %p %p %s\n", db
, stg
, st
, debugstr_w(name
) );
2703 /* read the transform data */
2704 read_stream_data( stg
, name
, TRUE
, &rawdata
, &rawsize
);
2707 TRACE("table %s empty\n", debugstr_w(name
) );
2708 return ERROR_INVALID_TABLE
;
2711 /* create a table view */
2712 r
= TABLE_CreateView( db
, name
, (MSIVIEW
**) &tv
);
2713 if( r
!= ERROR_SUCCESS
)
2716 r
= tv
->view
.ops
->execute( &tv
->view
, NULL
);
2717 if( r
!= ERROR_SUCCESS
)
2720 TRACE("name = %s columns = %u row_size = %u raw size = %u\n",
2721 debugstr_w(name
), tv
->num_cols
, tv
->row_size
, rawsize
);
2723 /* interpret the data */
2724 for( n
=0; n
< rawsize
; )
2726 mask
= rawdata
[n
] | (rawdata
[n
+1] << 8);
2731 * if the low bit is set, columns are continuous and
2732 * the number of columns is specified in the high byte
2735 for( i
=0; i
<tv
->num_cols
; i
++ )
2737 if( (tv
->columns
[i
].type
& MSITYPE_STRING
) &&
2738 ! MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
2739 sz
+= bytes_per_strref
;
2741 sz
+= bytes_per_column( tv
->db
, &tv
->columns
[i
], bytes_per_strref
);
2747 * If the low bit is not set, mask is a bitmask.
2748 * Excepting for key fields, which are always present,
2749 * each bit indicates that a field is present in the transform record.
2751 * mask == 0 is a special case ... only the keys will be present
2752 * and it means that this row should be deleted.
2755 for( i
=0; i
<tv
->num_cols
; i
++ )
2757 if( (tv
->columns
[i
].type
& MSITYPE_KEY
) || ((1<<i
)&mask
))
2759 if( (tv
->columns
[i
].type
& MSITYPE_STRING
) &&
2760 ! MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
2761 sz
+= bytes_per_strref
;
2763 sz
+= bytes_per_column( tv
->db
, &tv
->columns
[i
], bytes_per_strref
);
2768 /* check we didn't run of the end of the table */
2769 if ( (n
+sz
) > rawsize
)
2772 dump_table( st
, (USHORT
*)rawdata
, rawsize
);
2776 rec
= msi_get_transform_record( tv
, st
, stg
, &rawdata
[n
], bytes_per_strref
);
2781 UINT number
= MSI_NULL_INTEGER
;
2784 if (!strcmpW( name
, szColumns
))
2786 MSI_RecordGetStringW( rec
, 1, table
, &sz
);
2787 number
= MSI_RecordGetInteger( rec
, 2 );
2790 * Native msi seems writes nul into the Number (2nd) column of
2791 * the _Columns table, only when the columns are from a new table
2793 if ( number
== MSI_NULL_INTEGER
)
2795 /* reset the column number on a new table */
2796 if (strcmpW( coltable
, table
))
2799 lstrcpyW( coltable
, table
);
2802 /* fix nul column numbers */
2803 MSI_RecordSetInteger( rec
, 2, ++colcol
);
2807 if (TRACE_ON(msidb
)) dump_record( rec
);
2809 r
= msi_table_find_row( tv
, rec
, &row
, NULL
);
2810 if (r
== ERROR_SUCCESS
)
2814 TRACE("deleting row [%d]:\n", row
);
2815 r
= TABLE_delete_row( &tv
->view
, row
);
2816 if (r
!= ERROR_SUCCESS
)
2817 WARN("failed to delete row %u\n", r
);
2821 TRACE("modifying full row [%d]:\n", row
);
2822 r
= TABLE_set_row( &tv
->view
, row
, rec
, (1 << tv
->num_cols
) - 1 );
2823 if (r
!= ERROR_SUCCESS
)
2824 WARN("failed to modify row %u\n", r
);
2828 TRACE("modifying masked row [%d]:\n", row
);
2829 r
= TABLE_set_row( &tv
->view
, row
, rec
, mask
);
2830 if (r
!= ERROR_SUCCESS
)
2831 WARN("failed to modify row %u\n", r
);
2836 TRACE("inserting row\n");
2837 r
= TABLE_insert_row( &tv
->view
, rec
, -1, FALSE
);
2838 if (r
!= ERROR_SUCCESS
)
2839 WARN("failed to insert row %u\n", r
);
2842 if (number
!= MSI_NULL_INTEGER
&& !strcmpW( name
, szColumns
))
2843 msi_update_table_columns( db
, table
);
2845 msiobj_release( &rec
->hdr
);
2852 /* no need to free the table, it's associated with the database */
2853 msi_free( rawdata
);
2855 tv
->view
.ops
->delete( &tv
->view
);
2857 return ERROR_SUCCESS
;
2861 * msi_table_apply_transform
2863 * Enumerate the table transforms in a transform storage and apply each one.
2865 UINT
msi_table_apply_transform( MSIDATABASE
*db
, IStorage
*stg
)
2867 struct list transforms
;
2868 IEnumSTATSTG
*stgenum
= NULL
;
2869 TRANSFORMDATA
*transform
;
2870 TRANSFORMDATA
*tables
= NULL
, *columns
= NULL
;
2873 string_table
*strings
;
2874 UINT ret
= ERROR_FUNCTION_FAILED
;
2875 UINT bytes_per_strref
;
2877 TRACE("%p %p\n", db
, stg
);
2879 strings
= msi_load_string_table( stg
, &bytes_per_strref
);
2883 r
= IStorage_EnumElements( stg
, 0, NULL
, 0, &stgenum
);
2887 list_init(&transforms
);
2891 MSITABLEVIEW
*tv
= NULL
;
2895 r
= IEnumSTATSTG_Next( stgenum
, 1, &stat
, &count
);
2896 if ( FAILED( r
) || !count
)
2899 decode_streamname( stat
.pwcsName
, name
);
2900 CoTaskMemFree( stat
.pwcsName
);
2901 if ( name
[0] != 0x4840 )
2904 if ( !strcmpW( name
+1, szStringPool
) ||
2905 !strcmpW( name
+1, szStringData
) )
2908 transform
= msi_alloc_zero( sizeof(TRANSFORMDATA
) );
2912 list_add_tail( &transforms
, &transform
->entry
);
2914 transform
->name
= strdupW( name
+ 1 );
2916 if ( !strcmpW( transform
->name
, szTables
) )
2918 else if (!strcmpW( transform
->name
, szColumns
) )
2919 columns
= transform
;
2921 TRACE("transform contains stream %s\n", debugstr_w(name
));
2923 /* load the table */
2924 r
= TABLE_CreateView( db
, transform
->name
, (MSIVIEW
**) &tv
);
2925 if( r
!= ERROR_SUCCESS
)
2928 r
= tv
->view
.ops
->execute( &tv
->view
, NULL
);
2929 if( r
!= ERROR_SUCCESS
)
2931 tv
->view
.ops
->delete( &tv
->view
);
2935 tv
->view
.ops
->delete( &tv
->view
);
2939 * Apply _Tables and _Columns transforms first so that
2940 * the table metadata is correct, and empty tables exist.
2942 ret
= msi_table_load_transform( db
, stg
, strings
, tables
, bytes_per_strref
);
2943 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_INVALID_TABLE
)
2946 ret
= msi_table_load_transform( db
, stg
, strings
, columns
, bytes_per_strref
);
2947 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_INVALID_TABLE
)
2950 ret
= ERROR_SUCCESS
;
2952 while ( !list_empty( &transforms
) )
2954 transform
= LIST_ENTRY( list_head( &transforms
), TRANSFORMDATA
, entry
);
2956 if ( strcmpW( transform
->name
, szColumns
) &&
2957 strcmpW( transform
->name
, szTables
) &&
2958 ret
== ERROR_SUCCESS
)
2960 ret
= msi_table_load_transform( db
, stg
, strings
, transform
, bytes_per_strref
);
2963 list_remove( &transform
->entry
);
2964 msi_free( transform
->name
);
2965 msi_free( transform
);
2968 if ( ret
== ERROR_SUCCESS
)
2969 append_storage_to_db( db
, stg
);
2973 IEnumSTATSTG_Release( stgenum
);
2975 msi_destroy_stringtable( strings
);