push c6bab2db4fc296bd36abf8f7d9cf443d4a73048e
[wine/hacks.git] / dlls / msi / table.c
blobe7e2e68c38fcffe4d9891b389146134ad0091a2a
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2002-2005 Mike McCormack for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
22 #include <assert.h>
24 #define COBJMACROS
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winerror.h"
31 #include "msi.h"
32 #include "msiquery.h"
33 #include "objbase.h"
34 #include "objidl.h"
35 #include "winnls.h"
36 #include "msipriv.h"
37 #include "query.h"
39 #include "wine/debug.h"
40 #include "wine/unicode.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
44 #define MSITABLE_HASH_TABLE_SIZE 37
45 #define LONG_STR_BYTES 3
47 typedef struct tagMSICOLUMNHASHENTRY
49 struct tagMSICOLUMNHASHENTRY *next;
50 UINT value;
51 UINT row;
52 } MSICOLUMNHASHENTRY;
54 typedef struct tagMSICOLUMNINFO
56 LPWSTR tablename;
57 UINT number;
58 LPWSTR colname;
59 UINT type;
60 UINT offset;
61 INT ref_count;
62 BOOL temporary;
63 MSICOLUMNHASHENTRY **hash_table;
64 } MSICOLUMNINFO;
66 typedef struct tagMSIORDERINFO
68 UINT *reorder;
69 UINT num_cols;
70 UINT cols[1];
71 } MSIORDERINFO;
73 struct tagMSITABLE
75 BYTE **data;
76 UINT row_count;
77 BYTE **nonpersistent_data;
78 UINT nonpersistent_row_count;
79 struct list entry;
80 MSICOLUMNINFO *colinfo;
81 UINT col_count;
82 MSICONDITION persistent;
83 INT ref_count;
84 WCHAR name[1];
87 typedef struct tagMSITRANSFORM {
88 struct list entry;
89 IStorage *stg;
90 } MSITRANSFORM;
92 static const WCHAR szStringData[] = {
93 '_','S','t','r','i','n','g','D','a','t','a',0 };
94 static const WCHAR szStringPool[] = {
95 '_','S','t','r','i','n','g','P','o','o','l',0 };
97 /* information for default tables */
98 static WCHAR szTables[] = { '_','T','a','b','l','e','s',0 };
99 static WCHAR szTable[] = { 'T','a','b','l','e',0 };
100 static WCHAR szName[] = { 'N','a','m','e',0 };
101 static WCHAR szColumns[] = { '_','C','o','l','u','m','n','s',0 };
102 static WCHAR szNumber[] = { 'N','u','m','b','e','r',0 };
103 static WCHAR szType[] = { 'T','y','p','e',0 };
105 static const MSICOLUMNINFO _Columns_cols[4] = {
106 { szColumns, 1, szTable, MSITYPE_VALID | MSITYPE_STRING | MSITYPE_KEY | 64, 0, 0, 0, NULL },
107 { szColumns, 2, szNumber, MSITYPE_VALID | MSITYPE_KEY | 2, 2, 0, 0, NULL },
108 { szColumns, 3, szName, MSITYPE_VALID | MSITYPE_STRING | 64, 4, 0, 0, NULL },
109 { szColumns, 4, szType, MSITYPE_VALID | 2, 6, 0, 0, NULL },
112 static const MSICOLUMNINFO _Tables_cols[1] = {
113 { szTables, 1, szName, MSITYPE_VALID | MSITYPE_STRING | MSITYPE_KEY | 64, 0, 0, 0, NULL },
116 #define MAX_STREAM_NAME 0x1f
118 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name,
119 MSICOLUMNINFO **pcols, UINT *pcount );
120 static void table_calc_column_offsets( MSIDATABASE *db, MSICOLUMNINFO *colinfo,
121 DWORD count );
122 static UINT get_tablecolumns( MSIDATABASE *db,
123 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz);
124 static void msi_free_colinfo( MSICOLUMNINFO *colinfo, UINT count );
126 static inline UINT bytes_per_column( MSIDATABASE *db, const MSICOLUMNINFO *col )
128 if( MSITYPE_IS_BINARY(col->type) )
129 return 2;
130 if( col->type & MSITYPE_STRING )
131 return db->bytes_per_strref;
132 if( (col->type & 0xff) > 4 )
133 ERR("Invalid column size!\n");
134 return col->type & 0xff;
137 static int utf2mime(int x)
139 if( (x>='0') && (x<='9') )
140 return x-'0';
141 if( (x>='A') && (x<='Z') )
142 return x-'A'+10;
143 if( (x>='a') && (x<='z') )
144 return x-'a'+10+26;
145 if( x=='.' )
146 return 10+26+26;
147 if( x=='_' )
148 return 10+26+26+1;
149 return -1;
152 static LPWSTR encode_streamname(BOOL bTable, LPCWSTR in)
154 DWORD count = MAX_STREAM_NAME;
155 DWORD ch, next;
156 LPWSTR out, p;
158 if( !bTable )
159 count = lstrlenW( in )+2;
160 out = msi_alloc( count*sizeof(WCHAR) );
161 p = out;
163 if( bTable )
165 *p++ = 0x4840;
166 count --;
168 while( count -- )
170 ch = *in++;
171 if( !ch )
173 *p = ch;
174 return out;
176 if( ( ch < 0x80 ) && ( utf2mime(ch) >= 0 ) )
178 ch = utf2mime(ch) + 0x4800;
179 next = *in;
180 if( next && (next<0x80) )
182 next = utf2mime(next);
183 if( next != -1 )
185 next += 0x3ffffc0;
186 ch += (next<<6);
187 in++;
191 *p++ = ch;
193 ERR("Failed to encode stream name (%s)\n",debugstr_w(in));
194 msi_free( out );
195 return NULL;
198 static int mime2utf(int x)
200 if( x<10 )
201 return x + '0';
202 if( x<(10+26))
203 return x - 10 + 'A';
204 if( x<(10+26+26))
205 return x - 10 - 26 + 'a';
206 if( x == (10+26+26) )
207 return '.';
208 return '_';
211 BOOL decode_streamname(LPCWSTR in, LPWSTR out)
213 WCHAR ch;
214 DWORD count = 0;
216 while ( (ch = *in++) )
218 if( (ch >= 0x3800 ) && (ch < 0x4840 ) )
220 if( ch >= 0x4800 )
221 ch = mime2utf(ch-0x4800);
222 else
224 ch -= 0x3800;
225 *out++ = mime2utf(ch&0x3f);
226 count++;
227 ch = mime2utf((ch>>6)&0x3f);
230 *out++ = ch;
231 count++;
233 *out = 0;
234 return count;
237 void enum_stream_names( IStorage *stg )
239 IEnumSTATSTG *stgenum = NULL;
240 HRESULT r;
241 STATSTG stat;
242 ULONG n, count;
243 WCHAR name[0x40];
245 r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
246 if( FAILED( r ) )
247 return;
249 n = 0;
250 while( 1 )
252 count = 0;
253 r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
254 if( FAILED( r ) || !count )
255 break;
256 decode_streamname( stat.pwcsName, name );
257 TRACE("stream %2d -> %s %s\n", n,
258 debugstr_w(stat.pwcsName), debugstr_w(name) );
259 CoTaskMemFree( stat.pwcsName );
260 n++;
263 IEnumSTATSTG_Release( stgenum );
266 UINT read_stream_data( IStorage *stg, LPCWSTR stname, BOOL table,
267 BYTE **pdata, UINT *psz )
269 HRESULT r;
270 UINT ret = ERROR_FUNCTION_FAILED;
271 VOID *data;
272 ULONG sz, count;
273 IStream *stm = NULL;
274 STATSTG stat;
275 LPWSTR encname;
277 encname = encode_streamname(table, stname);
279 TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
281 r = IStorage_OpenStream(stg, encname, NULL,
282 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stm);
283 msi_free( encname );
284 if( FAILED( r ) )
286 WARN("open stream failed r = %08x - empty table?\n", r);
287 return ret;
290 r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
291 if( FAILED( r ) )
293 WARN("open stream failed r = %08x!\n", r);
294 goto end;
297 if( stat.cbSize.QuadPart >> 32 )
299 WARN("Too big!\n");
300 goto end;
303 sz = stat.cbSize.QuadPart;
304 data = msi_alloc( sz );
305 if( !data )
307 WARN("couldn't allocate memory r=%08x!\n", r);
308 ret = ERROR_NOT_ENOUGH_MEMORY;
309 goto end;
312 r = IStream_Read(stm, data, sz, &count );
313 if( FAILED( r ) || ( count != sz ) )
315 msi_free( data );
316 WARN("read stream failed r = %08x!\n", r);
317 goto end;
320 *pdata = data;
321 *psz = sz;
322 ret = ERROR_SUCCESS;
324 end:
325 IStream_Release( stm );
327 return ret;
330 static UINT db_get_raw_stream( MSIDATABASE *db, LPCWSTR stname, IStream **stm )
332 LPWSTR encname;
333 HRESULT r;
335 encname = encode_streamname(FALSE, stname);
337 TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
339 r = IStorage_OpenStream(db->storage, encname, NULL,
340 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, stm);
341 if( FAILED( r ) )
343 MSITRANSFORM *transform;
345 LIST_FOR_EACH_ENTRY( transform, &db->transforms, MSITRANSFORM, entry )
347 TRACE("looking for %s in transform storage\n", debugstr_w(stname) );
348 r = IStorage_OpenStream( transform->stg, encname, NULL,
349 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, stm );
350 if (SUCCEEDED(r))
351 break;
355 msi_free( encname );
357 return SUCCEEDED(r) ? ERROR_SUCCESS : ERROR_FUNCTION_FAILED;
360 UINT read_raw_stream_data( MSIDATABASE *db, LPCWSTR stname,
361 USHORT **pdata, UINT *psz )
363 HRESULT r;
364 UINT ret = ERROR_FUNCTION_FAILED;
365 VOID *data;
366 ULONG sz, count;
367 IStream *stm = NULL;
368 STATSTG stat;
370 r = db_get_raw_stream( db, stname, &stm );
371 if( r != ERROR_SUCCESS)
372 return ret;
373 r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
374 if( FAILED( r ) )
376 WARN("open stream failed r = %08x!\n", r);
377 goto end;
380 if( stat.cbSize.QuadPart >> 32 )
382 WARN("Too big!\n");
383 goto end;
386 sz = stat.cbSize.QuadPart;
387 data = msi_alloc( sz );
388 if( !data )
390 WARN("couldn't allocate memory r=%08x!\n", r);
391 ret = ERROR_NOT_ENOUGH_MEMORY;
392 goto end;
395 r = IStream_Read(stm, data, sz, &count );
396 if( FAILED( r ) || ( count != sz ) )
398 msi_free( data );
399 WARN("read stream failed r = %08x!\n", r);
400 goto end;
403 *pdata = data;
404 *psz = sz;
405 ret = ERROR_SUCCESS;
407 end:
408 IStream_Release( stm );
410 return ret;
413 UINT write_stream_data( IStorage *stg, LPCWSTR stname,
414 LPCVOID data, UINT sz, BOOL bTable )
416 HRESULT r;
417 UINT ret = ERROR_FUNCTION_FAILED;
418 ULONG count;
419 IStream *stm = NULL;
420 ULARGE_INTEGER size;
421 LARGE_INTEGER pos;
422 LPWSTR encname;
424 encname = encode_streamname(bTable, stname );
425 r = IStorage_OpenStream( stg, encname, NULL,
426 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, &stm);
427 if( FAILED(r) )
429 r = IStorage_CreateStream( stg, encname,
430 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
432 msi_free( encname );
433 if( FAILED( r ) )
435 WARN("open stream failed r = %08x\n", r);
436 return ret;
439 size.QuadPart = sz;
440 r = IStream_SetSize( stm, size );
441 if( FAILED( r ) )
443 WARN("Failed to SetSize\n");
444 goto end;
447 pos.QuadPart = 0;
448 r = IStream_Seek( stm, pos, STREAM_SEEK_SET, NULL );
449 if( FAILED( r ) )
451 WARN("Failed to Seek\n");
452 goto end;
455 if (sz)
457 r = IStream_Write(stm, data, sz, &count );
458 if( FAILED( r ) || ( count != sz ) )
460 WARN("Failed to Write\n");
461 goto end;
465 ret = ERROR_SUCCESS;
467 end:
468 IStream_Release( stm );
470 return ret;
473 static void free_table( MSITABLE *table )
475 UINT i;
476 for( i=0; i<table->row_count; i++ )
477 msi_free( table->data[i] );
478 msi_free( table->data );
479 for( i=0; i<table->nonpersistent_row_count; i++ )
480 msi_free( table->nonpersistent_data[i] );
481 msi_free( table->nonpersistent_data );
482 msi_free_colinfo( table->colinfo, table->col_count );
483 msi_free( table->colinfo );
484 msi_free( table );
487 static UINT msi_table_get_row_size( MSIDATABASE *db,const MSICOLUMNINFO *cols,
488 UINT count )
490 const MSICOLUMNINFO *last_col = &cols[count-1];
491 if (!count)
492 return 0;
493 return last_col->offset + bytes_per_column( db, last_col );
496 /* add this table to the list of cached tables in the database */
497 static UINT read_table_from_storage( MSIDATABASE *db, MSITABLE *t, IStorage *stg )
499 BYTE *rawdata = NULL;
500 UINT rawsize = 0, i, j, row_size = 0;
502 TRACE("%s\n",debugstr_w(t->name));
504 row_size = msi_table_get_row_size( db, t->colinfo, t->col_count );
506 /* if we can't read the table, just assume that it's empty */
507 read_stream_data( stg, t->name, TRUE, &rawdata, &rawsize );
508 if( !rawdata )
509 return ERROR_SUCCESS;
511 TRACE("Read %d bytes\n", rawsize );
513 if( rawsize % row_size )
515 WARN("Table size is invalid %d/%d\n", rawsize, row_size );
516 goto err;
519 t->row_count = rawsize / row_size;
520 t->data = msi_alloc_zero( t->row_count * sizeof (USHORT*) );
521 if( !t->data )
522 goto err;
524 /* transpose all the data */
525 TRACE("Transposing data from %d rows\n", t->row_count );
526 for( i=0; i<t->row_count; i++ )
528 t->data[i] = msi_alloc( row_size );
529 if( !t->data[i] )
530 goto err;
532 for( j=0; j<t->col_count; j++ )
534 UINT ofs = t->colinfo[j].offset;
535 UINT n = bytes_per_column( db, &t->colinfo[j] );
536 UINT k;
538 if ( n != 2 && n != 3 && n != 4 )
540 ERR("oops - unknown column width %d\n", n);
541 goto err;
544 for ( k = 0; k < n; k++ )
545 t->data[i][ofs + k] = rawdata[ofs*t->row_count + i * n + k];
549 msi_free( rawdata );
550 return ERROR_SUCCESS;
551 err:
552 msi_free( rawdata );
553 return ERROR_FUNCTION_FAILED;
556 void free_cached_tables( MSIDATABASE *db )
558 while( !list_empty( &db->tables ) )
560 MSITABLE *t = LIST_ENTRY( list_head( &db->tables ), MSITABLE, entry );
562 list_remove( &t->entry );
563 free_table( t );
567 static MSITABLE *find_cached_table( MSIDATABASE *db, LPCWSTR name )
569 MSITABLE *t;
571 LIST_FOR_EACH_ENTRY( t, &db->tables, MSITABLE, entry )
572 if( !lstrcmpW( name, t->name ) )
573 return t;
575 return NULL;
578 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name, MSICOLUMNINFO **pcols, UINT *pcount )
580 UINT r, column_count = 0;
581 MSICOLUMNINFO *columns;
583 /* get the number of columns in this table */
584 column_count = 0;
585 r = get_tablecolumns( db, name, NULL, &column_count );
586 if( r != ERROR_SUCCESS )
587 return r;
589 *pcount = column_count;
591 /* if there's no columns, there's no table */
592 if( column_count == 0 )
593 return ERROR_INVALID_PARAMETER;
595 TRACE("Table %s found\n", debugstr_w(name) );
597 columns = msi_alloc( column_count*sizeof (MSICOLUMNINFO) );
598 if( !columns )
599 return ERROR_FUNCTION_FAILED;
601 r = get_tablecolumns( db, name, columns, &column_count );
602 if( r != ERROR_SUCCESS )
604 msi_free( columns );
605 return ERROR_FUNCTION_FAILED;
608 *pcols = columns;
610 return r;
613 UINT msi_create_table( MSIDATABASE *db, LPCWSTR name, column_info *col_info,
614 MSICONDITION persistent, MSITABLE **table_ret)
616 UINT r, nField;
617 MSIVIEW *tv = NULL;
618 MSIRECORD *rec = NULL;
619 column_info *col;
620 MSITABLE *table;
621 UINT i;
623 /* only add tables that don't exist already */
624 if( TABLE_Exists(db, name ) )
626 WARN("table %s exists\n", debugstr_w(name));
627 return ERROR_BAD_QUERY_SYNTAX;
630 table = msi_alloc( sizeof (MSITABLE) + lstrlenW(name)*sizeof (WCHAR) );
631 if( !table )
632 return ERROR_FUNCTION_FAILED;
634 table->ref_count = 1;
635 table->row_count = 0;
636 table->data = NULL;
637 table->nonpersistent_row_count = 0;
638 table->nonpersistent_data = NULL;
639 table->colinfo = NULL;
640 table->col_count = 0;
641 table->persistent = persistent;
642 lstrcpyW( table->name, name );
644 for( col = col_info; col; col = col->next )
645 table->col_count++;
647 table->colinfo = msi_alloc( table->col_count * sizeof(MSICOLUMNINFO) );
648 if (!table->colinfo)
650 free_table( table );
651 return ERROR_FUNCTION_FAILED;
654 for( i = 0, col = col_info; col; i++, col = col->next )
656 table->colinfo[ i ].tablename = strdupW( col->table );
657 table->colinfo[ i ].number = i + 1;
658 table->colinfo[ i ].colname = strdupW( col->column );
659 table->colinfo[ i ].type = col->type;
660 table->colinfo[ i ].offset = 0;
661 table->colinfo[ i ].ref_count = 0;
662 table->colinfo[ i ].hash_table = NULL;
663 table->colinfo[ i ].temporary = col->temporary;
665 table_calc_column_offsets( db, table->colinfo, table->col_count);
667 r = TABLE_CreateView( db, szTables, &tv );
668 TRACE("CreateView returned %x\n", r);
669 if( r )
671 free_table( table );
672 return r;
675 r = tv->ops->execute( tv, 0 );
676 TRACE("tv execute returned %x\n", r);
677 if( r )
678 goto err;
680 rec = MSI_CreateRecord( 1 );
681 if( !rec )
682 goto err;
684 r = MSI_RecordSetStringW( rec, 1, name );
685 if( r )
686 goto err;
688 r = tv->ops->insert_row( tv, rec, -1, persistent == MSICONDITION_FALSE );
689 TRACE("insert_row returned %x\n", r);
690 if( r )
691 goto err;
693 tv->ops->delete( tv );
694 tv = NULL;
696 msiobj_release( &rec->hdr );
697 rec = NULL;
699 if( persistent != MSICONDITION_FALSE )
701 /* add each column to the _Columns table */
702 r = TABLE_CreateView( db, szColumns, &tv );
703 if( r )
704 return r;
706 r = tv->ops->execute( tv, 0 );
707 TRACE("tv execute returned %x\n", r);
708 if( r )
709 goto err;
711 rec = MSI_CreateRecord( 4 );
712 if( !rec )
713 goto err;
715 r = MSI_RecordSetStringW( rec, 1, name );
716 if( r )
717 goto err;
720 * need to set the table, column number, col name and type
721 * for each column we enter in the table
723 nField = 1;
724 for( col = col_info; col; col = col->next )
726 r = MSI_RecordSetInteger( rec, 2, nField );
727 if( r )
728 goto err;
730 r = MSI_RecordSetStringW( rec, 3, col->column );
731 if( r )
732 goto err;
734 r = MSI_RecordSetInteger( rec, 4, col->type );
735 if( r )
736 goto err;
738 r = tv->ops->insert_row( tv, rec, -1, FALSE );
739 if( r )
740 goto err;
742 nField++;
744 if( !col )
745 r = ERROR_SUCCESS;
748 err:
749 if (rec)
750 msiobj_release( &rec->hdr );
751 /* FIXME: remove values from the string table on error */
752 if( tv )
753 tv->ops->delete( tv );
755 if (r == ERROR_SUCCESS)
757 list_add_head( &db->tables, &table->entry );
758 *table_ret = table;
760 else
761 free_table( table );
763 return r;
766 static UINT get_table( MSIDATABASE *db, LPCWSTR name, MSITABLE **table_ret )
768 MSITABLE *table;
769 UINT r;
771 /* first, see if the table is cached */
772 table = find_cached_table( db, name );
773 if( table )
775 *table_ret = table;
776 return ERROR_SUCCESS;
779 /* nonexistent tables should be interpreted as empty tables */
780 table = msi_alloc( sizeof (MSITABLE) + lstrlenW(name)*sizeof (WCHAR) );
781 if( !table )
782 return ERROR_FUNCTION_FAILED;
784 table->row_count = 0;
785 table->data = NULL;
786 table->nonpersistent_row_count = 0;
787 table->nonpersistent_data = NULL;
788 table->colinfo = NULL;
789 table->col_count = 0;
790 table->persistent = MSICONDITION_TRUE;
791 lstrcpyW( table->name, name );
793 if ( !lstrcmpW(name, szTables) || !lstrcmpW(name, szColumns) )
794 table->persistent = MSICONDITION_NONE;
796 r = table_get_column_info( db, name, &table->colinfo, &table->col_count);
797 if (r != ERROR_SUCCESS)
799 free_table ( table );
800 return r;
803 r = read_table_from_storage( db, table, db->storage );
804 if( r != ERROR_SUCCESS )
806 free_table( table );
807 return r;
810 list_add_head( &db->tables, &table->entry );
811 *table_ret = table;
812 return ERROR_SUCCESS;
815 static UINT save_table( MSIDATABASE *db, const MSITABLE *t )
817 BYTE *rawdata = NULL, *p;
818 UINT rawsize, r, i, j, row_size;
820 /* Nothing to do for non-persistent tables */
821 if( t->persistent == MSICONDITION_FALSE )
822 return ERROR_SUCCESS;
824 TRACE("Saving %s\n", debugstr_w( t->name ) );
826 row_size = msi_table_get_row_size( db, t->colinfo, t->col_count );
828 rawsize = t->row_count * row_size;
829 rawdata = msi_alloc_zero( rawsize );
830 if( !rawdata )
832 r = ERROR_NOT_ENOUGH_MEMORY;
833 goto err;
836 p = rawdata;
837 for( i=0; i<t->col_count; i++ )
839 for( j=0; j<t->row_count; j++ )
841 UINT offset = t->colinfo[i].offset;
843 *p++ = t->data[j][offset];
844 *p++ = t->data[j][offset + 1];
845 if( 4 == bytes_per_column( db, &t->colinfo[i] ) )
847 *p++ = t->data[j][offset + 2];
848 *p++ = t->data[j][offset + 3];
853 TRACE("writing %d bytes\n", rawsize);
854 r = write_stream_data( db->storage, t->name, rawdata, rawsize, TRUE );
856 err:
857 msi_free( rawdata );
859 return r;
862 static void table_calc_column_offsets( MSIDATABASE *db, MSICOLUMNINFO *colinfo,
863 DWORD count )
865 DWORD i;
867 for( i=0; colinfo && (i<count); i++ )
869 assert( (i+1) == colinfo[ i ].number );
870 if (i)
871 colinfo[i].offset = colinfo[ i - 1 ].offset
872 + bytes_per_column( db, &colinfo[ i - 1 ] );
873 else
874 colinfo[i].offset = 0;
875 TRACE("column %d is [%s] with type %08x ofs %d\n",
876 colinfo[i].number, debugstr_w(colinfo[i].colname),
877 colinfo[i].type, colinfo[i].offset);
881 static UINT get_defaulttablecolumns( MSIDATABASE *db, LPCWSTR name,
882 MSICOLUMNINFO *colinfo, UINT *sz)
884 const MSICOLUMNINFO *p;
885 DWORD i, n;
887 TRACE("%s\n", debugstr_w(name));
889 if (!lstrcmpW( name, szTables ))
891 p = _Tables_cols;
892 n = 1;
894 else if (!lstrcmpW( name, szColumns ))
896 p = _Columns_cols;
897 n = 4;
899 else
900 return ERROR_FUNCTION_FAILED;
902 /* duplicate the string data so we can free it in msi_free_colinfo */
903 for (i=0; i<n; i++)
905 if (colinfo && (i < *sz) )
907 colinfo[i] = p[i];
908 colinfo[i].tablename = strdupW( p[i].tablename );
909 colinfo[i].colname = strdupW( p[i].colname );
911 if( colinfo && (i >= *sz) )
912 break;
914 table_calc_column_offsets( db, colinfo, n );
915 *sz = n;
916 return ERROR_SUCCESS;
919 static void msi_free_colinfo( MSICOLUMNINFO *colinfo, UINT count )
921 UINT i;
923 for( i=0; i<count; i++ )
925 msi_free( colinfo[i].tablename );
926 msi_free( colinfo[i].colname );
927 msi_free( colinfo[i].hash_table );
931 static LPWSTR msi_makestring( const MSIDATABASE *db, UINT stringid)
933 return strdupW(msi_string_lookup_id( db->strings, stringid ));
936 static UINT read_table_int(BYTE *const *data, UINT row, UINT col, UINT bytes)
938 UINT ret = 0, i;
940 for (i = 0; i < bytes; i++)
941 ret += (data[row][col + i] << i * 8);
943 return ret;
946 static UINT get_tablecolumns( MSIDATABASE *db,
947 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz)
949 UINT r, i, n=0, table_id, count, maxcount = *sz;
950 MSITABLE *table = NULL;
952 TRACE("%s\n", debugstr_w(szTableName));
954 /* first check if there is a default table with that name */
955 r = get_defaulttablecolumns( db, szTableName, colinfo, sz );
956 if( ( r == ERROR_SUCCESS ) && *sz )
957 return r;
959 r = get_table( db, szColumns, &table );
960 if( r != ERROR_SUCCESS )
962 ERR("couldn't load _Columns table\n");
963 return ERROR_FUNCTION_FAILED;
966 /* convert table and column names to IDs from the string table */
967 r = msi_string2idW( db->strings, szTableName, &table_id );
968 if( r != ERROR_SUCCESS )
970 WARN("Couldn't find id for %s\n", debugstr_w(szTableName));
971 return r;
974 TRACE("Table id is %d, row count is %d\n", table_id, table->row_count);
976 /* Note: _Columns table doesn't have non-persistent data */
978 /* if maxcount is non-zero, assume it's exactly right for this table */
979 memset( colinfo, 0, maxcount*sizeof(*colinfo) );
980 count = table->row_count;
981 for( i=0; i<count; i++ )
983 if( read_table_int(table->data, i, 0, db->bytes_per_strref) != table_id )
984 continue;
985 if( colinfo )
987 UINT id = read_table_int(table->data, i, table->colinfo[2].offset, db->bytes_per_strref);
988 UINT col = read_table_int(table->data, i, table->colinfo[1].offset, sizeof(USHORT)) - (1<<15);
990 /* check the column number is in range */
991 if (col<1 || col>maxcount)
993 ERR("column %d out of range\n", col);
994 continue;
997 /* check if this column was already set */
998 if (colinfo[ col - 1 ].number)
1000 ERR("duplicate column %d\n", col);
1001 continue;
1004 colinfo[ col - 1 ].tablename = msi_makestring( db, table_id );
1005 colinfo[ col - 1 ].number = col;
1006 colinfo[ col - 1 ].colname = msi_makestring( db, id );
1007 colinfo[ col - 1 ].type = read_table_int(table->data, i,
1008 table->colinfo[3].offset,
1009 sizeof(USHORT)) - (1<<15);
1010 colinfo[ col - 1 ].offset = 0;
1011 colinfo[ col - 1 ].ref_count = 0;
1012 colinfo[ col - 1 ].hash_table = NULL;
1014 n++;
1017 TRACE("%s has %d columns\n", debugstr_w(szTableName), n);
1019 if (colinfo && n != maxcount)
1021 ERR("missing column in table %s\n", debugstr_w(szTableName));
1022 msi_free_colinfo(colinfo, maxcount );
1023 return ERROR_FUNCTION_FAILED;
1026 table_calc_column_offsets( db, colinfo, n );
1027 *sz = n;
1029 return ERROR_SUCCESS;
1032 static void msi_update_table_columns( MSIDATABASE *db, LPCWSTR name )
1034 MSITABLE *table;
1035 UINT size, offset, old_count;
1036 UINT n;
1038 table = find_cached_table( db, name );
1039 old_count = table->col_count;
1040 msi_free( table->colinfo );
1041 table->colinfo = NULL;
1043 table_get_column_info( db, name, &table->colinfo, &table->col_count );
1044 if (!table->col_count)
1045 return;
1047 size = msi_table_get_row_size( db, table->colinfo, table->col_count );
1048 offset = table->colinfo[table->col_count - 1].offset;
1050 for ( n = 0; n < table->row_count; n++ )
1052 table->data[n] = msi_realloc( table->data[n], size );
1053 if (old_count < table->col_count)
1054 memset( &table->data[n][offset], 0, size - offset );
1058 /* try to find the table name in the _Tables table */
1059 BOOL TABLE_Exists( MSIDATABASE *db, LPCWSTR name )
1061 UINT r, table_id = 0, i, count;
1062 MSITABLE *table = NULL;
1064 static const WCHAR szStreams[] = {'_','S','t','r','e','a','m','s',0};
1065 static const WCHAR szStorages[] = {'_','S','t','o','r','a','g','e','s',0};
1067 if( !lstrcmpW( name, szTables ) || !lstrcmpW( name, szColumns ) ||
1068 !lstrcmpW( name, szStreams ) || !lstrcmpW( name, szStorages ) )
1069 return TRUE;
1071 r = msi_string2idW( db->strings, name, &table_id );
1072 if( r != ERROR_SUCCESS )
1074 TRACE("Couldn't find id for %s\n", debugstr_w(name));
1075 return FALSE;
1078 r = get_table( db, szTables, &table );
1079 if( r != ERROR_SUCCESS )
1081 ERR("table %s not available\n", debugstr_w(szTables));
1082 return FALSE;
1085 count = table->row_count;
1086 for( i=0; i<count; i++ )
1087 if( table->data[ i ][ 0 ] == table_id )
1088 return TRUE;
1090 count = table->nonpersistent_row_count;
1091 for( i=0; i<count; i++ )
1092 if( table->nonpersistent_data[ i ][ 0 ] == table_id )
1093 return TRUE;
1095 return FALSE;
1098 /* below is the query interface to a table */
1100 typedef struct tagMSITABLEVIEW
1102 MSIVIEW view;
1103 MSIDATABASE *db;
1104 MSITABLE *table;
1105 MSICOLUMNINFO *columns;
1106 MSIORDERINFO *order;
1107 UINT num_cols;
1108 UINT row_size;
1109 WCHAR name[1];
1110 } MSITABLEVIEW;
1112 static UINT TABLE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
1114 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1115 UINT offset, n;
1116 BYTE **data;
1118 if( !tv->table )
1119 return ERROR_INVALID_PARAMETER;
1121 if( (col==0) || (col>tv->num_cols) )
1122 return ERROR_INVALID_PARAMETER;
1124 /* how many rows are there ? */
1125 if( row >= tv->table->row_count + tv->table->nonpersistent_row_count )
1126 return ERROR_NO_MORE_ITEMS;
1128 if( tv->columns[col-1].offset >= tv->row_size )
1130 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1131 ERR("%p %p\n", tv, tv->columns );
1132 return ERROR_FUNCTION_FAILED;
1135 if (tv->order)
1136 row = tv->order->reorder[row];
1138 if (row >= tv->table->row_count)
1140 row -= tv->table->row_count;
1141 data = tv->table->nonpersistent_data;
1143 else
1144 data = tv->table->data;
1146 n = bytes_per_column( tv->db, &tv->columns[col-1] );
1147 if (n != 2 && n != 3 && n != 4)
1149 ERR("oops! what is %d bytes per column?\n", n );
1150 return ERROR_FUNCTION_FAILED;
1153 offset = tv->columns[col-1].offset;
1154 *val = read_table_int(data, row, offset, n);
1156 /* TRACE("Data [%d][%d] = %d\n", row, col, *val ); */
1158 return ERROR_SUCCESS;
1162 * We need a special case for streams, as we need to reference column with
1163 * the name of the stream in the same table, and the table name
1164 * which may not be available at higher levels of the query
1166 static UINT TABLE_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm )
1168 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1169 UINT ival = 0, refcol = 0, r;
1170 LPCWSTR sval;
1171 LPWSTR full_name;
1172 DWORD len;
1173 static const WCHAR szDot[] = { '.', 0 };
1174 WCHAR number[0x20];
1176 if( !view->ops->fetch_int )
1177 return ERROR_INVALID_PARAMETER;
1180 * The column marked with the type stream data seems to have a single number
1181 * which references the column containing the name of the stream data
1183 * Fetch the column to reference first.
1185 r = view->ops->fetch_int( view, row, col, &ival );
1186 if( r != ERROR_SUCCESS )
1187 return r;
1189 /* check the column value is in range */
1190 if (ival > tv->num_cols || ival == col)
1192 ERR("bad column ref (%u) for stream\n", ival);
1193 return ERROR_FUNCTION_FAILED;
1196 if ( tv->columns[ival - 1].type & MSITYPE_STRING )
1198 /* now get the column with the name of the stream */
1199 r = view->ops->fetch_int( view, row, ival, &refcol );
1200 if ( r != ERROR_SUCCESS )
1201 return r;
1203 /* lookup the string value from the string table */
1204 sval = msi_string_lookup_id( tv->db->strings, refcol );
1205 if ( !sval )
1206 return ERROR_INVALID_PARAMETER;
1208 else
1210 static const WCHAR fmt[] = { '%','d',0 };
1211 sprintfW( number, fmt, ival );
1212 sval = number;
1215 len = lstrlenW( tv->name ) + 2 + lstrlenW( sval );
1216 full_name = msi_alloc( len*sizeof(WCHAR) );
1217 lstrcpyW( full_name, tv->name );
1218 lstrcatW( full_name, szDot );
1219 lstrcatW( full_name, sval );
1221 r = db_get_raw_stream( tv->db, full_name, stm );
1222 if( r )
1223 ERR("fetching stream %s, error = %d\n",debugstr_w(full_name), r);
1224 msi_free( full_name );
1226 return r;
1229 static UINT TABLE_set_int( MSITABLEVIEW *tv, UINT row, UINT col, UINT val )
1231 UINT offset, n, i;
1232 BYTE **data;
1234 if( !tv->table )
1235 return ERROR_INVALID_PARAMETER;
1237 if( (col==0) || (col>tv->num_cols) )
1238 return ERROR_INVALID_PARAMETER;
1240 if( row >= tv->table->row_count + tv->table->nonpersistent_row_count )
1241 return ERROR_INVALID_PARAMETER;
1243 if( tv->columns[col-1].offset >= tv->row_size )
1245 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1246 ERR("%p %p\n", tv, tv->columns );
1247 return ERROR_FUNCTION_FAILED;
1250 msi_free( tv->columns[col-1].hash_table );
1251 tv->columns[col-1].hash_table = NULL;
1253 if (row >= tv->table->row_count)
1255 row -= tv->table->row_count;
1256 data = tv->table->nonpersistent_data;
1258 else
1259 data = tv->table->data;
1261 n = bytes_per_column( tv->db, &tv->columns[col-1] );
1262 if ( n != 2 && n != 3 && n != 4 )
1264 ERR("oops! what is %d bytes per column?\n", n );
1265 return ERROR_FUNCTION_FAILED;
1268 offset = tv->columns[col-1].offset;
1269 for ( i = 0; i < n; i++ )
1270 data[row][offset + i] = (val >> i * 8) & 0xff;
1272 return ERROR_SUCCESS;
1275 static UINT TABLE_get_row( struct tagMSIVIEW *view, UINT row, MSIRECORD **rec )
1277 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1279 if (!tv->table)
1280 return ERROR_INVALID_PARAMETER;
1282 if (tv->order)
1283 row = tv->order->reorder[row];
1285 return msi_view_get_row(tv->db, view, row, rec);
1288 static UINT TABLE_set_row( struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask )
1290 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1291 UINT i, val, r = ERROR_SUCCESS;
1293 if ( !tv->table )
1294 return ERROR_INVALID_PARAMETER;
1296 /* test if any of the mask bits are invalid */
1297 if ( mask >= (1<<tv->num_cols) )
1298 return ERROR_INVALID_PARAMETER;
1300 for ( i = 0; i < tv->num_cols; i++ )
1302 BOOL persistent;
1304 /* only update the fields specified in the mask */
1305 if ( !(mask&(1<<i)) )
1306 continue;
1308 /* if row >= tv->table->row_count then it is a non-persistent row */
1309 persistent = (tv->table->persistent != MSICONDITION_FALSE) &&
1310 (row < tv->table->row_count);
1311 /* FIXME: should we allow updating keys? */
1313 val = 0;
1314 if ( !MSI_RecordIsNull( rec, i + 1 ) )
1316 if ( MSITYPE_IS_BINARY(tv->columns[ i ].type) )
1318 val = 1; /* refers to the first key column */
1320 else if ( tv->columns[i].type & MSITYPE_STRING )
1322 LPCWSTR sval = MSI_RecordGetString( rec, i + 1 );
1323 UINT ival, x;
1325 r = msi_string2idW(tv->db->strings, sval, &ival);
1326 if (r == ERROR_SUCCESS)
1328 TABLE_fetch_int(&tv->view, row, i + 1, &x);
1329 if (ival == x)
1330 continue;
1333 val = msi_addstringW( tv->db->strings, 0, sval, -1, 1,
1334 persistent ? StringPersistent : StringNonPersistent );
1336 else if ( 2 == bytes_per_column( tv->db, &tv->columns[ i ] ) )
1338 val = 0x8000 + MSI_RecordGetInteger( rec, i + 1 );
1339 if ( val & 0xffff0000 )
1341 ERR("field %u value %d out of range\n", i+1, val - 0x8000 );
1342 return ERROR_FUNCTION_FAILED;
1345 else
1347 INT ival = MSI_RecordGetInteger( rec, i + 1 );
1348 val = ival ^ 0x80000000;
1352 r = TABLE_set_int( tv, row, i+1, val );
1353 if ( r != ERROR_SUCCESS )
1354 break;
1356 return r;
1359 static UINT table_create_new_row( struct tagMSIVIEW *view, UINT *num, BOOL temporary )
1361 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1362 BYTE **p, *row;
1363 UINT sz;
1364 BYTE ***data_ptr;
1365 UINT *row_count;
1367 TRACE("%p %s\n", view, temporary ? "TRUE" : "FALSE");
1369 if( !tv->table )
1370 return ERROR_INVALID_PARAMETER;
1372 row = msi_alloc_zero( tv->row_size );
1373 if( !row )
1374 return ERROR_NOT_ENOUGH_MEMORY;
1376 if( temporary )
1378 row_count = &tv->table->nonpersistent_row_count;
1379 data_ptr = &tv->table->nonpersistent_data;
1380 if (*num == -1)
1381 *num = tv->table->row_count + tv->table->nonpersistent_row_count;
1383 else
1385 row_count = &tv->table->row_count;
1386 data_ptr = &tv->table->data;
1387 if (*num == -1)
1388 *num = tv->table->row_count;
1391 sz = (*row_count + 1) * sizeof (BYTE*);
1392 if( *data_ptr )
1393 p = msi_realloc( *data_ptr, sz );
1394 else
1395 p = msi_alloc( sz );
1396 if( !p )
1398 msi_free( row );
1399 return ERROR_NOT_ENOUGH_MEMORY;
1402 *data_ptr = p;
1403 (*data_ptr)[*row_count] = row;
1404 (*row_count)++;
1406 return ERROR_SUCCESS;
1409 static UINT TABLE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
1411 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1413 TRACE("%p %p\n", tv, record);
1415 TRACE("There are %d columns\n", tv->num_cols );
1417 return ERROR_SUCCESS;
1420 static UINT TABLE_close( struct tagMSIVIEW *view )
1422 TRACE("%p\n", view );
1424 return ERROR_SUCCESS;
1427 static UINT TABLE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols)
1429 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1431 TRACE("%p %p %p\n", view, rows, cols );
1433 if( cols )
1434 *cols = tv->num_cols;
1435 if( rows )
1437 if( !tv->table )
1438 return ERROR_INVALID_PARAMETER;
1439 *rows = tv->table->row_count + tv->table->nonpersistent_row_count;
1442 return ERROR_SUCCESS;
1445 static UINT TABLE_get_column_info( struct tagMSIVIEW *view,
1446 UINT n, LPWSTR *name, UINT *type, BOOL *temporary )
1448 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1450 TRACE("%p %d %p %p\n", tv, n, name, type );
1452 if( ( n == 0 ) || ( n > tv->num_cols ) )
1453 return ERROR_INVALID_PARAMETER;
1455 if( name )
1457 *name = strdupW( tv->columns[n-1].colname );
1458 if( !*name )
1459 return ERROR_FUNCTION_FAILED;
1462 if( type )
1463 *type = tv->columns[n-1].type;
1465 if( temporary )
1466 *temporary = tv->columns[n-1].temporary;
1468 return ERROR_SUCCESS;
1471 static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row );
1473 static UINT table_validate_new( MSITABLEVIEW *tv, MSIRECORD *rec )
1475 UINT r, row, i;
1477 /* check there's no null values where they're not allowed */
1478 for( i = 0; i < tv->num_cols; i++ )
1480 if ( tv->columns[i].type & MSITYPE_NULLABLE )
1481 continue;
1483 if ( MSITYPE_IS_BINARY(tv->columns[i].type) )
1484 TRACE("skipping binary column\n");
1485 else if ( tv->columns[i].type & MSITYPE_STRING )
1487 LPCWSTR str;
1489 str = MSI_RecordGetString( rec, i+1 );
1490 if (str == NULL || str[0] == 0)
1491 return ERROR_INVALID_DATA;
1493 else
1495 UINT n;
1497 n = MSI_RecordGetInteger( rec, i+1 );
1498 if (n == MSI_NULL_INTEGER)
1499 return ERROR_INVALID_DATA;
1503 /* check there's no duplicate keys */
1504 r = msi_table_find_row( tv, rec, &row );
1505 if (r == ERROR_SUCCESS)
1506 return ERROR_FUNCTION_FAILED;
1508 return ERROR_SUCCESS;
1511 static UINT TABLE_insert_row( struct tagMSIVIEW *view, MSIRECORD *rec, UINT row, BOOL temporary )
1513 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1514 UINT i, r, idx, size;
1515 BYTE **data;
1517 TRACE("%p %p %s\n", tv, rec, temporary ? "TRUE" : "FALSE" );
1519 /* check that the key is unique - can we find a matching row? */
1520 r = table_validate_new( tv, rec );
1521 if( r != ERROR_SUCCESS )
1522 return ERROR_FUNCTION_FAILED;
1524 r = table_create_new_row( view, &row, temporary );
1525 TRACE("insert_row returned %08x\n", r);
1526 if( r != ERROR_SUCCESS )
1527 return r;
1529 idx = row;
1530 if( temporary )
1532 data = tv->table->nonpersistent_data;
1533 size = tv->table->nonpersistent_row_count;
1534 idx -= tv->table->row_count;
1536 else
1538 data = tv->table->data;
1539 size = tv->table->row_count;
1542 /* shift the rows to make room for the new row */
1543 if( idx != size - 1 )
1545 for (i = 1; i < size - idx; i++)
1546 memmove(&(data[size - i][0]),
1547 &(data[size - i - 1][0]), tv->row_size);
1550 return TABLE_set_row( view, row, rec, (1<<tv->num_cols) - 1 );
1553 static UINT TABLE_delete_row( struct tagMSIVIEW *view, UINT row )
1555 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1556 UINT r, num_rows, num_cols, i;
1557 BYTE **data;
1559 TRACE("%p %d\n", tv, row);
1561 if ( !tv->table )
1562 return ERROR_INVALID_PARAMETER;
1564 r = TABLE_get_dimensions( view, &num_rows, &num_cols );
1565 if ( r != ERROR_SUCCESS )
1566 return r;
1568 if ( row >= num_rows )
1569 return ERROR_FUNCTION_FAILED;
1571 if ( row < tv->table->row_count )
1573 num_rows = tv->table->row_count;
1574 tv->table->row_count--;
1575 data = tv->table->data;
1577 else
1579 num_rows = tv->table->nonpersistent_row_count;
1580 row -= tv->table->row_count;
1581 tv->table->nonpersistent_row_count--;
1582 data = tv->table->nonpersistent_data;
1585 /* reset the hash tables */
1586 for (i = 0; i < tv->num_cols; i++)
1588 msi_free( tv->columns[i].hash_table );
1589 tv->columns[i].hash_table = NULL;
1592 if ( row == num_rows - 1 )
1593 return ERROR_SUCCESS;
1595 for (i = row + 1; i < num_rows; i++)
1596 memcpy(data[i - 1], data[i], tv->row_size);
1598 return ERROR_SUCCESS;
1601 static UINT msi_table_update(struct tagMSIVIEW *view, MSIRECORD *rec, UINT row)
1603 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1604 UINT r, new_row;
1606 /* FIXME: MsiViewFetch should set rec index 0 to some ID that
1607 * sets the fetched record apart from other records
1610 if (!tv->table)
1611 return ERROR_INVALID_PARAMETER;
1613 r = msi_table_find_row(tv, rec, &new_row);
1614 if (r != ERROR_SUCCESS)
1616 ERR("can't find row to modify\n");
1617 return ERROR_FUNCTION_FAILED;
1620 /* the row cannot be changed */
1621 if (row != new_row + 1)
1622 return ERROR_FUNCTION_FAILED;
1624 return TABLE_set_row(view, new_row, rec, (1 << tv->num_cols) - 1);
1627 static UINT modify_delete_row( struct tagMSIVIEW *view, MSIRECORD *rec )
1629 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1630 UINT row, r;
1632 r = msi_table_find_row(tv, rec, &row);
1633 if (r != ERROR_SUCCESS)
1634 return r;
1636 return TABLE_delete_row(view, row);
1639 static UINT msi_refresh_record( struct tagMSIVIEW *view, MSIRECORD *rec, UINT row )
1641 MSIRECORD *curr;
1642 UINT r, i, count;
1644 r = TABLE_get_row(view, row - 1, &curr);
1645 if (r != ERROR_SUCCESS)
1646 return r;
1648 count = MSI_RecordGetFieldCount(rec);
1649 for (i = 0; i < count; i++)
1650 MSI_RecordCopyField(curr, i + 1, rec, i + 1);
1652 msiobj_release(&curr->hdr);
1653 return ERROR_SUCCESS;
1656 static UINT TABLE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
1657 MSIRECORD *rec, UINT row)
1659 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1660 UINT r;
1662 TRACE("%p %d %p\n", view, eModifyMode, rec );
1664 switch (eModifyMode)
1666 case MSIMODIFY_DELETE:
1667 r = modify_delete_row( view, rec );
1668 break;
1669 case MSIMODIFY_VALIDATE_NEW:
1670 r = table_validate_new( tv, rec );
1671 break;
1673 case MSIMODIFY_INSERT:
1674 r = table_validate_new( tv, rec );
1675 if (r != ERROR_SUCCESS)
1676 break;
1677 r = TABLE_insert_row( view, rec, -1, FALSE );
1678 break;
1680 case MSIMODIFY_INSERT_TEMPORARY:
1681 r = table_validate_new( tv, rec );
1682 if (r != ERROR_SUCCESS)
1683 break;
1684 r = TABLE_insert_row( view, rec, -1, TRUE );
1685 break;
1687 case MSIMODIFY_REFRESH:
1688 r = msi_refresh_record( view, rec, row );
1689 break;
1691 case MSIMODIFY_UPDATE:
1692 r = msi_table_update( view, rec, row );
1693 break;
1695 case MSIMODIFY_ASSIGN:
1696 case MSIMODIFY_REPLACE:
1697 case MSIMODIFY_MERGE:
1698 case MSIMODIFY_VALIDATE:
1699 case MSIMODIFY_VALIDATE_FIELD:
1700 case MSIMODIFY_VALIDATE_DELETE:
1701 FIXME("%p %d %p - mode not implemented\n", view, eModifyMode, rec );
1702 r = ERROR_CALL_NOT_IMPLEMENTED;
1703 break;
1705 default:
1706 r = ERROR_INVALID_DATA;
1709 return r;
1712 static UINT TABLE_delete( struct tagMSIVIEW *view )
1714 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1716 TRACE("%p\n", view );
1718 tv->table = NULL;
1719 tv->columns = NULL;
1721 if (tv->order)
1723 msi_free( tv->order->reorder );
1724 msi_free( tv->order );
1725 tv->order = NULL;
1728 msi_free( tv );
1730 return ERROR_SUCCESS;
1733 static UINT TABLE_find_matching_rows( struct tagMSIVIEW *view, UINT col,
1734 UINT val, UINT *row, MSIITERHANDLE *handle )
1736 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1737 const MSICOLUMNHASHENTRY *entry;
1739 TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
1741 if( !tv->table )
1742 return ERROR_INVALID_PARAMETER;
1744 if( (col==0) || (col > tv->num_cols) )
1745 return ERROR_INVALID_PARAMETER;
1747 if( !tv->columns[col-1].hash_table )
1749 UINT i;
1750 UINT num_rows = tv->table->row_count + tv->table->nonpersistent_row_count;
1751 MSICOLUMNHASHENTRY **hash_table;
1752 MSICOLUMNHASHENTRY *new_entry;
1754 if( tv->columns[col-1].offset >= tv->row_size )
1756 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1757 ERR("%p %p\n", tv, tv->columns );
1758 return ERROR_FUNCTION_FAILED;
1761 /* allocate contiguous memory for the table and its entries so we
1762 * don't have to do an expensive cleanup */
1763 hash_table = msi_alloc(MSITABLE_HASH_TABLE_SIZE * sizeof(MSICOLUMNHASHENTRY*) +
1764 num_rows * sizeof(MSICOLUMNHASHENTRY));
1765 if (!hash_table)
1766 return ERROR_OUTOFMEMORY;
1768 memset(hash_table, 0, MSITABLE_HASH_TABLE_SIZE * sizeof(MSICOLUMNHASHENTRY*));
1769 tv->columns[col-1].hash_table = hash_table;
1771 new_entry = (MSICOLUMNHASHENTRY *)(hash_table + MSITABLE_HASH_TABLE_SIZE);
1773 for (i = 0; i < num_rows; i++, new_entry++)
1775 UINT row_value;
1777 if (view->ops->fetch_int( view, i, col, &row_value ) != ERROR_SUCCESS)
1778 continue;
1780 new_entry->next = NULL;
1781 new_entry->value = row_value;
1782 new_entry->row = i;
1783 if (hash_table[row_value % MSITABLE_HASH_TABLE_SIZE])
1785 MSICOLUMNHASHENTRY *prev_entry = hash_table[row_value % MSITABLE_HASH_TABLE_SIZE];
1786 while (prev_entry->next)
1787 prev_entry = prev_entry->next;
1788 prev_entry->next = new_entry;
1790 else
1791 hash_table[row_value % MSITABLE_HASH_TABLE_SIZE] = new_entry;
1795 if( !*handle )
1796 entry = tv->columns[col-1].hash_table[val % MSITABLE_HASH_TABLE_SIZE];
1797 else
1798 entry = (*handle)->next;
1800 while (entry && entry->value != val)
1801 entry = entry->next;
1803 *handle = entry;
1804 if (!entry)
1805 return ERROR_NO_MORE_ITEMS;
1807 *row = entry->row;
1809 return ERROR_SUCCESS;
1812 static UINT TABLE_add_ref(struct tagMSIVIEW *view)
1814 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1815 UINT i;
1817 TRACE("%p %d\n", view, tv->table->ref_count);
1819 for (i = 0; i < tv->table->col_count; i++)
1821 if (tv->table->colinfo[i].type & MSITYPE_TEMPORARY)
1822 InterlockedIncrement(&tv->table->colinfo[i].ref_count);
1825 return InterlockedIncrement(&tv->table->ref_count);
1828 static UINT TABLE_remove_column(struct tagMSIVIEW *view, LPCWSTR table, UINT number)
1830 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1831 MSIRECORD *rec = NULL;
1832 MSIVIEW *columns = NULL;
1833 UINT row, r;
1835 rec = MSI_CreateRecord(2);
1836 if (!rec)
1837 return ERROR_OUTOFMEMORY;
1839 MSI_RecordSetStringW(rec, 1, table);
1840 MSI_RecordSetInteger(rec, 2, number);
1842 r = TABLE_CreateView(tv->db, szColumns, &columns);
1843 if (r != ERROR_SUCCESS)
1844 return r;
1846 r = msi_table_find_row((MSITABLEVIEW *)columns, rec, &row);
1847 if (r != ERROR_SUCCESS)
1848 goto done;
1850 r = TABLE_delete_row(columns, row);
1851 if (r != ERROR_SUCCESS)
1852 goto done;
1854 msi_update_table_columns(tv->db, table);
1856 done:
1857 msiobj_release(&rec->hdr);
1858 columns->ops->delete(columns);
1859 return r;
1862 static UINT TABLE_release(struct tagMSIVIEW *view)
1864 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1865 INT ref = tv->table->ref_count;
1866 UINT i, r;
1868 TRACE("%p %d\n", view, ref);
1870 for (i = 0; i < tv->table->col_count; i++)
1872 if (tv->table->colinfo[i].type & MSITYPE_TEMPORARY)
1874 ref = InterlockedDecrement(&tv->table->colinfo[i].ref_count);
1875 if (ref == 0)
1877 r = TABLE_remove_column(view, tv->table->colinfo[i].tablename,
1878 tv->table->colinfo[i].number);
1879 if (r != ERROR_SUCCESS)
1880 break;
1885 ref = InterlockedDecrement(&tv->table->ref_count);
1886 if (ref == 0)
1888 if (!tv->table->row_count)
1890 list_remove(&tv->table->entry);
1891 free_table(tv->table);
1892 TABLE_delete(view);
1896 return ref;
1899 static UINT TABLE_add_column(struct tagMSIVIEW *view, LPCWSTR table, UINT number,
1900 LPCWSTR column, UINT type, BOOL hold)
1902 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1903 MSITABLE *msitable;
1904 MSIRECORD *rec;
1905 UINT r, i;
1907 rec = MSI_CreateRecord(4);
1908 if (!rec)
1909 return ERROR_OUTOFMEMORY;
1911 MSI_RecordSetStringW(rec, 1, table);
1912 MSI_RecordSetInteger(rec, 2, number);
1913 MSI_RecordSetStringW(rec, 3, column);
1914 MSI_RecordSetInteger(rec, 4, type);
1916 r = TABLE_insert_row(&tv->view, rec, -1, FALSE);
1917 if (r != ERROR_SUCCESS)
1918 goto done;
1920 msi_update_table_columns(tv->db, table);
1922 if (!hold)
1923 goto done;
1925 msitable = find_cached_table(tv->db, table);
1926 for (i = 0; i < msitable->col_count; i++)
1928 if (!lstrcmpW(msitable->colinfo[i].colname, column))
1930 InterlockedIncrement(&msitable->colinfo[i].ref_count);
1931 break;
1935 done:
1936 msiobj_release(&rec->hdr);
1937 return r;
1940 static UINT order_add_column(struct tagMSIVIEW *view, MSIORDERINFO *order, LPCWSTR name)
1942 UINT n, r, count;
1944 r = TABLE_get_dimensions(view, NULL, &count);
1945 if (r != ERROR_SUCCESS)
1946 return r;
1948 if (order->num_cols >= count)
1949 return ERROR_FUNCTION_FAILED;
1951 r = VIEW_find_column(view, name, &n);
1952 if (r != ERROR_SUCCESS)
1953 return r;
1955 order->cols[order->num_cols] = n;
1956 TRACE("Ordering by column %s (%d)\n", debugstr_w(name), n);
1958 order->num_cols++;
1960 return ERROR_SUCCESS;
1963 static UINT order_compare(struct tagMSIVIEW *view, MSIORDERINFO *order,
1964 UINT a, UINT b, UINT *swap)
1966 UINT r, i, a_val = 0, b_val = 0;
1968 *swap = 0;
1969 for (i = 0; i < order->num_cols; i++)
1971 r = TABLE_fetch_int(view, a, order->cols[i], &a_val);
1972 if (r != ERROR_SUCCESS)
1973 return r;
1975 r = TABLE_fetch_int(view, b, order->cols[i], &b_val);
1976 if (r != ERROR_SUCCESS)
1977 return r;
1979 if (a_val != b_val)
1981 if (a_val > b_val)
1982 *swap = 1;
1983 break;
1987 return ERROR_SUCCESS;
1990 static UINT order_mergesort(struct tagMSIVIEW *view, MSIORDERINFO *order,
1991 UINT left, UINT right)
1993 UINT r, i, j, temp;
1994 UINT swap = 0, center = (left + right) / 2;
1995 UINT *array = order->reorder;
1997 if (left == right)
1998 return ERROR_SUCCESS;
2000 /* sort the left half */
2001 r = order_mergesort(view, order, left, center);
2002 if (r != ERROR_SUCCESS)
2003 return r;
2005 /* sort the right half */
2006 r = order_mergesort(view, order, center + 1, right);
2007 if (r != ERROR_SUCCESS)
2008 return r;
2010 for (i = left, j = center + 1; (i <= center) && (j <= right); i++)
2012 r = order_compare(view, order, array[i], array[j], &swap);
2013 if (r != ERROR_SUCCESS)
2014 return r;
2016 if (swap)
2018 temp = array[j];
2019 memmove(&array[i + 1], &array[i], (j - i) * sizeof(UINT));
2020 array[i] = temp;
2021 j++;
2022 center++;
2026 return ERROR_SUCCESS;
2029 static UINT order_verify(struct tagMSIVIEW *view, MSIORDERINFO *order, UINT num_rows)
2031 UINT i, swap, r;
2033 for (i = 1; i < num_rows; i++)
2035 r = order_compare(view, order, order->reorder[i - 1],
2036 order->reorder[i], &swap);
2037 if (r != ERROR_SUCCESS)
2038 return r;
2040 if (!swap)
2041 continue;
2043 ERR("Bad order! %d\n", i);
2044 return ERROR_FUNCTION_FAILED;
2047 return ERROR_SUCCESS;
2050 static UINT TABLE_sort(struct tagMSIVIEW *view, column_info *columns)
2052 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
2053 MSIORDERINFO *order;
2054 column_info *ptr;
2055 UINT r, i;
2056 UINT rows, cols;
2058 TRACE("sorting table %s\n", debugstr_w(tv->name));
2060 r = TABLE_get_dimensions(view, &rows, &cols);
2061 if (r != ERROR_SUCCESS)
2062 return r;
2064 if (rows == 0)
2065 return ERROR_SUCCESS;
2067 order = msi_alloc_zero(sizeof(MSIORDERINFO) + sizeof(UINT) * cols);
2068 if (!order)
2069 return ERROR_OUTOFMEMORY;
2071 for (ptr = columns; ptr; ptr = ptr->next)
2072 order_add_column(view, order, ptr->column);
2074 order->reorder = msi_alloc(rows * sizeof(UINT));
2075 if (!order->reorder)
2076 return ERROR_OUTOFMEMORY;
2078 for (i = 0; i < rows; i++)
2079 order->reorder[i] = i;
2081 r = order_mergesort(view, order, 0, rows - 1);
2082 if (r != ERROR_SUCCESS)
2083 return r;
2085 r = order_verify(view, order, rows);
2086 if (r != ERROR_SUCCESS)
2087 return r;
2089 tv->order = order;
2091 return ERROR_SUCCESS;
2094 static UINT TABLE_drop(struct tagMSIVIEW *view)
2096 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
2097 MSIVIEW *tables = NULL;
2098 MSIRECORD *rec = NULL;
2099 UINT r, row;
2100 INT i;
2102 TRACE("dropping table %s\n", debugstr_w(tv->name));
2104 for (i = tv->table->col_count - 1; i >= 0; i--)
2106 r = TABLE_remove_column(view, tv->table->colinfo[i].tablename,
2107 tv->table->colinfo[i].number);
2108 if (r != ERROR_SUCCESS)
2109 return r;
2112 rec = MSI_CreateRecord(1);
2113 if (!rec)
2114 return ERROR_OUTOFMEMORY;
2116 MSI_RecordSetStringW(rec, 1, tv->name);
2118 r = TABLE_CreateView(tv->db, szTables, &tables);
2119 if (r != ERROR_SUCCESS)
2120 return r;
2122 r = msi_table_find_row((MSITABLEVIEW *)tables, rec, &row);
2123 if (r != ERROR_SUCCESS)
2124 goto done;
2126 r = TABLE_delete_row(tables, row);
2127 if (r != ERROR_SUCCESS)
2128 goto done;
2130 list_remove(&tv->table->entry);
2131 free_table(tv->table);
2132 TABLE_delete(view);
2134 done:
2135 msiobj_release(&rec->hdr);
2136 tables->ops->delete(tables);
2138 return r;
2141 static const MSIVIEWOPS table_ops =
2143 TABLE_fetch_int,
2144 TABLE_fetch_stream,
2145 TABLE_get_row,
2146 TABLE_set_row,
2147 TABLE_insert_row,
2148 TABLE_delete_row,
2149 TABLE_execute,
2150 TABLE_close,
2151 TABLE_get_dimensions,
2152 TABLE_get_column_info,
2153 TABLE_modify,
2154 TABLE_delete,
2155 TABLE_find_matching_rows,
2156 TABLE_add_ref,
2157 TABLE_release,
2158 TABLE_add_column,
2159 TABLE_remove_column,
2160 TABLE_sort,
2161 TABLE_drop,
2164 UINT TABLE_CreateView( MSIDATABASE *db, LPCWSTR name, MSIVIEW **view )
2166 MSITABLEVIEW *tv ;
2167 UINT r, sz;
2169 static const WCHAR Streams[] = {'_','S','t','r','e','a','m','s',0};
2170 static const WCHAR Storages[] = {'_','S','t','o','r','a','g','e','s',0};
2172 TRACE("%p %s %p\n", db, debugstr_w(name), view );
2174 if ( !lstrcmpW( name, Streams ) )
2175 return STREAMS_CreateView( db, view );
2176 else if ( !lstrcmpW( name, Storages ) )
2177 return STORAGES_CreateView( db, view );
2179 sz = sizeof *tv + lstrlenW(name)*sizeof name[0] ;
2180 tv = msi_alloc_zero( sz );
2181 if( !tv )
2182 return ERROR_FUNCTION_FAILED;
2184 r = get_table( db, name, &tv->table );
2185 if( r != ERROR_SUCCESS )
2187 msi_free( tv );
2188 WARN("table not found\n");
2189 return r;
2192 TRACE("table %p found with %d columns\n", tv->table, tv->table->col_count);
2194 /* fill the structure */
2195 tv->view.ops = &table_ops;
2196 tv->db = db;
2197 tv->columns = tv->table->colinfo;
2198 tv->num_cols = tv->table->col_count;
2199 tv->row_size = msi_table_get_row_size( db, tv->table->colinfo, tv->table->col_count );
2201 TRACE("%s one row is %d bytes\n", debugstr_w(name), tv->row_size );
2203 *view = (MSIVIEW*) tv;
2204 lstrcpyW( tv->name, name );
2206 return ERROR_SUCCESS;
2209 UINT MSI_CommitTables( MSIDATABASE *db )
2211 UINT r;
2212 MSITABLE *table = NULL;
2214 TRACE("%p\n",db);
2216 r = msi_save_string_table( db->strings, db->storage );
2217 if( r != ERROR_SUCCESS )
2219 WARN("failed to save string table r=%08x\n",r);
2220 return r;
2223 LIST_FOR_EACH_ENTRY( table, &db->tables, MSITABLE, entry )
2225 r = save_table( db, table );
2226 if( r != ERROR_SUCCESS )
2228 WARN("failed to save table %s (r=%08x)\n",
2229 debugstr_w(table->name), r);
2230 return r;
2234 /* force everything to reload next time */
2235 free_cached_tables( db );
2237 return ERROR_SUCCESS;
2240 MSICONDITION MSI_DatabaseIsTablePersistent( MSIDATABASE *db, LPCWSTR table )
2242 MSITABLE *t;
2243 UINT r;
2245 TRACE("%p %s\n", db, debugstr_w(table));
2247 if (!table)
2248 return MSICONDITION_ERROR;
2250 r = get_table( db, table, &t );
2251 if (r != ERROR_SUCCESS)
2252 return MSICONDITION_NONE;
2254 return t->persistent;
2257 static UINT read_raw_int(const BYTE *data, UINT col, UINT bytes)
2259 UINT ret = 0, i;
2261 for (i = 0; i < bytes; i++)
2262 ret += (data[col + i] << i * 8);
2264 return ret;
2267 static MSIRECORD *msi_get_transform_record( const MSITABLEVIEW *tv, const string_table *st,
2268 const BYTE *rawdata, UINT bytes_per_strref )
2270 UINT i, val, ofs = 0;
2271 USHORT mask;
2272 MSICOLUMNINFO *columns = tv->columns;
2273 MSIRECORD *rec;
2275 mask = rawdata[0] | (rawdata[1] << 8);
2276 rawdata += 2;
2278 rec = MSI_CreateRecord( tv->num_cols );
2279 if( !rec )
2280 return rec;
2282 TRACE("row ->\n");
2283 for( i=0; i<tv->num_cols; i++ )
2285 if ( (mask&1) && (i>=(mask>>8)) )
2286 break;
2287 /* all keys must be present */
2288 if ( (~mask&1) && (~columns[i].type & MSITYPE_KEY) && ((1<<i) & ~mask) )
2289 continue;
2291 if( (columns[i].type & MSITYPE_STRING) &&
2292 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
2294 LPCWSTR sval;
2296 val = read_raw_int(rawdata, ofs, bytes_per_strref);
2297 sval = msi_string_lookup_id( st, val );
2298 MSI_RecordSetStringW( rec, i+1, sval );
2299 TRACE(" field %d [%s]\n", i+1, debugstr_w(sval));
2300 ofs += bytes_per_strref;
2302 else
2304 UINT n = bytes_per_column( tv->db, &columns[i] );
2305 switch( n )
2307 case 2:
2308 val = read_raw_int(rawdata, ofs, n);
2309 if (val)
2310 MSI_RecordSetInteger( rec, i+1, val^0x8000 );
2311 TRACE(" field %d [0x%04x]\n", i+1, val );
2312 break;
2313 case 4:
2314 val = read_raw_int(rawdata, ofs, n);
2315 if (val)
2316 MSI_RecordSetInteger( rec, i+1, val^0x80000000 );
2317 TRACE(" field %d [0x%08x]\n", i+1, val );
2318 break;
2319 default:
2320 ERR("oops - unknown column width %d\n", n);
2321 break;
2323 ofs += n;
2326 return rec;
2329 static void dump_record( MSIRECORD *rec )
2331 UINT i, n;
2333 n = MSI_RecordGetFieldCount( rec );
2334 for( i=1; i<=n; i++ )
2336 LPCWSTR sval = MSI_RecordGetString( rec, i );
2338 if( MSI_RecordIsNull( rec, i ) )
2339 TRACE("row -> []\n");
2340 else if( (sval = MSI_RecordGetString( rec, i )) )
2341 TRACE("row -> [%s]\n", debugstr_w(sval));
2342 else
2343 TRACE("row -> [0x%08x]\n", MSI_RecordGetInteger( rec, i ) );
2347 static void dump_table( const string_table *st, const USHORT *rawdata, UINT rawsize )
2349 LPCWSTR sval;
2350 UINT i;
2352 for( i=0; i<(rawsize/2); i++ )
2354 sval = msi_string_lookup_id( st, rawdata[i] );
2355 MESSAGE(" %04x %s\n", rawdata[i], debugstr_w(sval) );
2359 static UINT* msi_record_to_row( const MSITABLEVIEW *tv, MSIRECORD *rec )
2361 LPCWSTR str;
2362 UINT i, r, *data;
2364 data = msi_alloc( tv->num_cols *sizeof (UINT) );
2365 for( i=0; i<tv->num_cols; i++ )
2367 data[i] = 0;
2369 if ( ~tv->columns[i].type & MSITYPE_KEY )
2370 continue;
2372 /* turn the transform column value into a row value */
2373 if ( ( tv->columns[i].type & MSITYPE_STRING ) &&
2374 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
2376 str = MSI_RecordGetString( rec, i+1 );
2377 r = msi_string2idW( tv->db->strings, str, &data[i] );
2379 /* if there's no matching string in the string table,
2380 these keys can't match any record, so fail now. */
2381 if( ERROR_SUCCESS != r )
2383 msi_free( data );
2384 return NULL;
2387 else
2389 data[i] = MSI_RecordGetInteger( rec, i+1 );
2391 if (data[i] == MSI_NULL_INTEGER)
2392 data[i] = 0;
2393 else if ((tv->columns[i].type&0xff) == 2)
2394 data[i] += 0x8000;
2395 else
2396 data[i] += 0x80000000;
2399 return data;
2402 static UINT msi_row_matches( MSITABLEVIEW *tv, UINT row, const UINT *data )
2404 UINT i, r, x, ret = ERROR_FUNCTION_FAILED;
2406 for( i=0; i<tv->num_cols; i++ )
2408 if ( ~tv->columns[i].type & MSITYPE_KEY )
2409 continue;
2411 /* turn the transform column value into a row value */
2412 r = TABLE_fetch_int( &tv->view, row, i+1, &x );
2413 if ( r != ERROR_SUCCESS )
2415 ERR("TABLE_fetch_int shouldn't fail here\n");
2416 break;
2419 /* if this key matches, move to the next column */
2420 if ( x != data[i] )
2422 ret = ERROR_FUNCTION_FAILED;
2423 break;
2426 ret = ERROR_SUCCESS;
2429 return ret;
2432 static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row )
2434 UINT i, r = ERROR_FUNCTION_FAILED, *data;
2436 data = msi_record_to_row( tv, rec );
2437 if( !data )
2438 return r;
2439 for( i = 0; i < tv->table->row_count + tv->table->nonpersistent_row_count; i++ )
2441 r = msi_row_matches( tv, i, data );
2442 if( r == ERROR_SUCCESS )
2444 *row = i;
2445 break;
2448 msi_free( data );
2449 return r;
2452 typedef struct
2454 struct list entry;
2455 LPWSTR name;
2456 } TRANSFORMDATA;
2458 static UINT msi_table_load_transform( MSIDATABASE *db, IStorage *stg,
2459 string_table *st, TRANSFORMDATA *transform,
2460 UINT bytes_per_strref )
2462 UINT rawsize = 0;
2463 BYTE *rawdata = NULL;
2464 MSITABLEVIEW *tv = NULL;
2465 UINT r, n, sz, i, mask;
2466 MSIRECORD *rec = NULL;
2467 UINT colcol = 0;
2468 WCHAR coltable[32];
2469 LPWSTR name;
2471 if (!transform)
2472 return ERROR_SUCCESS;
2474 name = transform->name;
2476 coltable[0] = 0;
2477 TRACE("%p %p %p %s\n", db, stg, st, debugstr_w(name) );
2479 /* read the transform data */
2480 read_stream_data( stg, name, TRUE, &rawdata, &rawsize );
2481 if ( !rawdata )
2483 TRACE("table %s empty\n", debugstr_w(name) );
2484 return ERROR_INVALID_TABLE;
2487 /* create a table view */
2488 r = TABLE_CreateView( db, name, (MSIVIEW**) &tv );
2489 if( r != ERROR_SUCCESS )
2490 goto err;
2492 r = tv->view.ops->execute( &tv->view, NULL );
2493 if( r != ERROR_SUCCESS )
2494 goto err;
2496 TRACE("name = %s columns = %u row_size = %u raw size = %u\n",
2497 debugstr_w(name), tv->num_cols, tv->row_size, rawsize );
2499 /* interpret the data */
2500 r = ERROR_SUCCESS;
2501 for( n=0; n < rawsize; )
2503 mask = rawdata[n] | (rawdata[n+1] << 8);
2505 if (mask&1)
2508 * if the low bit is set, columns are continuous and
2509 * the number of columns is specified in the high byte
2511 sz = 2;
2512 for( i=0; i<tv->num_cols; i++ )
2514 if( (tv->columns[i].type & MSITYPE_STRING) &&
2515 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
2516 sz += bytes_per_strref;
2517 else
2518 sz += bytes_per_column( tv->db, &tv->columns[i] );
2521 else
2524 * If the low bit is not set, mask is a bitmask.
2525 * Excepting for key fields, which are always present,
2526 * each bit indicates that a field is present in the transform record.
2528 * mask == 0 is a special case ... only the keys will be present
2529 * and it means that this row should be deleted.
2531 sz = 2;
2532 for( i=0; i<tv->num_cols; i++ )
2534 if( (tv->columns[i].type & MSITYPE_KEY) || ((1<<i)&mask))
2536 if( (tv->columns[i].type & MSITYPE_STRING) &&
2537 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
2538 sz += bytes_per_strref;
2539 else
2540 sz += bytes_per_column( tv->db, &tv->columns[i] );
2545 /* check we didn't run of the end of the table */
2546 if ( (n+sz) > rawsize )
2548 ERR("borked.\n");
2549 dump_table( st, (USHORT *)rawdata, rawsize );
2550 break;
2553 rec = msi_get_transform_record( tv, st, &rawdata[n], bytes_per_strref );
2554 if (rec)
2556 if ( mask & 1 )
2558 WCHAR table[32];
2559 DWORD sz = 32;
2560 UINT number = MSI_NULL_INTEGER;
2562 TRACE("inserting record\n");
2564 if (!lstrcmpW(name, szColumns))
2566 MSI_RecordGetStringW( rec, 1, table, &sz );
2567 number = MSI_RecordGetInteger( rec, 2 );
2570 * Native msi seems writes nul into the Number (2nd) column of
2571 * the _Columns table, only when the columns are from a new table
2573 if ( number == MSI_NULL_INTEGER )
2575 /* reset the column number on a new table */
2576 if ( lstrcmpW(coltable, table) )
2578 colcol = 0;
2579 lstrcpyW( coltable, table );
2582 /* fix nul column numbers */
2583 MSI_RecordSetInteger( rec, 2, ++colcol );
2587 r = TABLE_insert_row( &tv->view, rec, -1, FALSE );
2588 if (r != ERROR_SUCCESS)
2589 ERR("insert row failed\n");
2591 if ( number != MSI_NULL_INTEGER && !lstrcmpW(name, szColumns) )
2592 msi_update_table_columns( db, table );
2594 else
2596 UINT row = 0;
2598 r = msi_table_find_row( tv, rec, &row );
2599 if (r != ERROR_SUCCESS)
2600 ERR("no matching row to transform\n");
2601 else if ( mask )
2603 TRACE("modifying row [%d]:\n", row);
2604 TABLE_set_row( &tv->view, row, rec, mask );
2606 else
2608 TRACE("deleting row [%d]:\n", row);
2609 TABLE_delete_row( &tv->view, row );
2612 if( TRACE_ON(msidb) ) dump_record( rec );
2613 msiobj_release( &rec->hdr );
2616 n += sz;
2619 err:
2620 /* no need to free the table, it's associated with the database */
2621 msi_free( rawdata );
2622 if( tv )
2623 tv->view.ops->delete( &tv->view );
2625 return ERROR_SUCCESS;
2629 * msi_table_apply_transform
2631 * Enumerate the table transforms in a transform storage and apply each one.
2633 UINT msi_table_apply_transform( MSIDATABASE *db, IStorage *stg )
2635 struct list transforms;
2636 IEnumSTATSTG *stgenum = NULL;
2637 TRANSFORMDATA *transform;
2638 TRANSFORMDATA *tables = NULL, *columns = NULL;
2639 HRESULT r;
2640 STATSTG stat;
2641 string_table *strings;
2642 UINT ret = ERROR_FUNCTION_FAILED;
2643 UINT bytes_per_strref;
2645 TRACE("%p %p\n", db, stg );
2647 strings = msi_load_string_table( stg, &bytes_per_strref );
2648 if( !strings )
2649 goto end;
2651 r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
2652 if( FAILED( r ) )
2653 goto end;
2655 list_init(&transforms);
2657 while ( TRUE )
2659 MSITABLEVIEW *tv = NULL;
2660 WCHAR name[0x40];
2661 ULONG count = 0;
2663 r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
2664 if ( FAILED( r ) || !count )
2665 break;
2667 decode_streamname( stat.pwcsName, name );
2668 CoTaskMemFree( stat.pwcsName );
2669 if ( name[0] != 0x4840 )
2670 continue;
2672 if ( !lstrcmpW( name+1, szStringPool ) ||
2673 !lstrcmpW( name+1, szStringData ) )
2674 continue;
2676 transform = msi_alloc_zero( sizeof(TRANSFORMDATA) );
2677 if ( !transform )
2678 break;
2680 list_add_tail( &transforms, &transform->entry );
2682 transform->name = strdupW( name + 1 );
2684 if ( !lstrcmpW( transform->name, szTables ) )
2685 tables = transform;
2686 else if (!lstrcmpW( transform->name, szColumns ) )
2687 columns = transform;
2689 TRACE("transform contains stream %s\n", debugstr_w(name));
2691 /* load the table */
2692 r = TABLE_CreateView( db, transform->name, (MSIVIEW**) &tv );
2693 if( r != ERROR_SUCCESS )
2694 continue;
2696 r = tv->view.ops->execute( &tv->view, NULL );
2697 if( r != ERROR_SUCCESS )
2699 tv->view.ops->delete( &tv->view );
2700 continue;
2703 tv->view.ops->delete( &tv->view );
2707 * Apply _Tables and _Columns transforms first so that
2708 * the table metadata is correct, and empty tables exist.
2710 ret = msi_table_load_transform( db, stg, strings, tables, bytes_per_strref );
2711 if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
2712 goto end;
2714 ret = msi_table_load_transform( db, stg, strings, columns, bytes_per_strref );
2715 if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
2716 goto end;
2718 ret = ERROR_SUCCESS;
2720 while ( !list_empty( &transforms ) )
2722 transform = LIST_ENTRY( list_head( &transforms ), TRANSFORMDATA, entry );
2724 if ( lstrcmpW( transform->name, szColumns ) &&
2725 lstrcmpW( transform->name, szTables ) &&
2726 ret == ERROR_SUCCESS )
2728 ret = msi_table_load_transform( db, stg, strings, transform, bytes_per_strref );
2731 list_remove( &transform->entry );
2732 msi_free( transform->name );
2733 msi_free( transform );
2736 if ( ret == ERROR_SUCCESS )
2737 append_storage_to_db( db, stg );
2739 end:
2740 if ( stgenum )
2741 IEnumSTATSTG_Release( stgenum );
2742 if ( strings )
2743 msi_destroy_stringtable( strings );
2745 return ret;
2748 void append_storage_to_db( MSIDATABASE *db, IStorage *stg )
2750 MSITRANSFORM *t;
2752 t = msi_alloc( sizeof *t );
2753 t->stg = stg;
2754 IStorage_AddRef( stg );
2755 list_add_tail( &db->transforms, &t->entry );
2758 void msi_free_transforms( MSIDATABASE *db )
2760 while( !list_empty( &db->transforms ) )
2762 MSITRANSFORM *t = LIST_ENTRY( list_head( &db->transforms ),
2763 MSITRANSFORM, entry );
2764 list_remove( &t->entry );
2765 IStorage_Release( t->stg );
2766 msi_free( t );