msi: Add support for adding temporary/non-persistent data to tables.
[wine/wine-gecko.git] / dlls / msi / table.c
blobddf0ee4d25865eda599990828042490b4c67b1ee
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
46 typedef struct tagMSICOLUMNHASHENTRY
48 struct tagMSICOLUMNHASHENTRY *next;
49 UINT value;
50 UINT row;
51 } MSICOLUMNHASHENTRY;
53 typedef struct tagMSICOLUMNINFO
55 LPCWSTR tablename;
56 UINT number;
57 LPCWSTR colname;
58 UINT type;
59 UINT offset;
60 MSICOLUMNHASHENTRY **hash_table;
61 } MSICOLUMNINFO;
63 struct tagMSITABLE
65 USHORT **data;
66 UINT row_count;
67 USHORT **nonpersistent_data;
68 UINT nonpersistent_row_count;
69 struct list entry;
70 MSICOLUMNINFO *colinfo;
71 UINT col_count;
72 WCHAR name[1];
75 typedef struct tagMSITRANSFORM {
76 struct list entry;
77 IStorage *stg;
78 } MSITRANSFORM;
80 static const WCHAR szStringData[] = {
81 '_','S','t','r','i','n','g','D','a','t','a',0 };
82 static const WCHAR szStringPool[] = {
83 '_','S','t','r','i','n','g','P','o','o','l',0 };
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 szName[] = { 'N','a','m','e',0 };
89 static const WCHAR szColumns[] = { '_','C','o','l','u','m','n','s',0 };
90 static const WCHAR szNumber[] = { 'N','u','m','b','e','r',0 };
91 static const WCHAR szType[] = { 'T','y','p','e',0 };
93 static const MSICOLUMNINFO _Columns_cols[4] = {
94 { szColumns, 1, szTable, MSITYPE_VALID | MSITYPE_STRING | 64, 0 },
95 { szColumns, 2, szNumber, MSITYPE_VALID | 2, 2 },
96 { szColumns, 3, szName, MSITYPE_VALID | MSITYPE_STRING | 64, 4 },
97 { szColumns, 4, szType, MSITYPE_VALID | 2, 6 },
99 static const MSICOLUMNINFO _Tables_cols[1] = {
100 { szTables, 1, szName, MSITYPE_VALID | MSITYPE_STRING | 64, 0 },
103 #define MAX_STREAM_NAME 0x1f
105 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name,
106 MSICOLUMNINFO **pcols, UINT *pcount );
107 static void table_calc_column_offsets( MSICOLUMNINFO *colinfo, DWORD count );
108 static UINT get_tablecolumns( MSIDATABASE *db,
109 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz);
110 static void msi_free_colinfo( MSICOLUMNINFO *colinfo, UINT count );
112 static inline UINT bytes_per_column( const MSICOLUMNINFO *col )
114 if( col->type & MSITYPE_STRING )
115 return 2;
116 if( (col->type & 0xff) > 4 )
117 ERR("Invalid column size!\n");
118 return col->type & 0xff;
121 static int utf2mime(int x)
123 if( (x>='0') && (x<='9') )
124 return x-'0';
125 if( (x>='A') && (x<='Z') )
126 return x-'A'+10;
127 if( (x>='a') && (x<='z') )
128 return x-'a'+10+26;
129 if( x=='.' )
130 return 10+26+26;
131 if( x=='_' )
132 return 10+26+26+1;
133 return -1;
136 static LPWSTR encode_streamname(BOOL bTable, LPCWSTR in)
138 DWORD count = MAX_STREAM_NAME;
139 DWORD ch, next;
140 LPWSTR out, p;
142 if( !bTable )
143 count = lstrlenW( in )+2;
144 out = msi_alloc( count*sizeof(WCHAR) );
145 p = out;
147 if( bTable )
149 *p++ = 0x4840;
150 count --;
152 while( count -- )
154 ch = *in++;
155 if( !ch )
157 *p = ch;
158 return out;
160 if( ( ch < 0x80 ) && ( utf2mime(ch) >= 0 ) )
162 ch = utf2mime(ch) + 0x4800;
163 next = *in;
164 if( next && (next<0x80) )
166 next = utf2mime(next);
167 if( next >= 0 )
169 next += 0x3ffffc0;
170 ch += (next<<6);
171 in++;
175 *p++ = ch;
177 ERR("Failed to encode stream name (%s)\n",debugstr_w(in));
178 msi_free( out );
179 return NULL;
182 static int mime2utf(int x)
184 if( x<10 )
185 return x + '0';
186 if( x<(10+26))
187 return x - 10 + 'A';
188 if( x<(10+26+26))
189 return x - 10 - 26 + 'a';
190 if( x == (10+26+26) )
191 return '.';
192 return '_';
195 static BOOL decode_streamname(LPWSTR in, LPWSTR out)
197 WCHAR ch;
198 DWORD count = 0;
200 while ( (ch = *in++) )
202 if( (ch >= 0x3800 ) && (ch < 0x4840 ) )
204 if( ch >= 0x4800 )
205 ch = mime2utf(ch-0x4800);
206 else
208 ch -= 0x3800;
209 *out++ = mime2utf(ch&0x3f);
210 count++;
211 ch = mime2utf((ch>>6)&0x3f);
214 *out++ = ch;
215 count++;
217 *out = 0;
218 return count;
221 void enum_stream_names( IStorage *stg )
223 IEnumSTATSTG *stgenum = NULL;
224 HRESULT r;
225 STATSTG stat;
226 ULONG n, count;
227 WCHAR name[0x40];
229 r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
230 if( FAILED( r ) )
231 return;
233 n = 0;
234 while( 1 )
236 count = 0;
237 r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
238 if( FAILED( r ) || !count )
239 break;
240 decode_streamname( stat.pwcsName, name );
241 TRACE("stream %2d -> %s %s\n", n,
242 debugstr_w(stat.pwcsName), debugstr_w(name) );
243 n++;
246 IEnumSTATSTG_Release( stgenum );
249 UINT read_stream_data( IStorage *stg, LPCWSTR stname,
250 USHORT **pdata, UINT *psz )
252 HRESULT r;
253 UINT ret = ERROR_FUNCTION_FAILED;
254 VOID *data;
255 ULONG sz, count;
256 IStream *stm = NULL;
257 STATSTG stat;
258 LPWSTR encname;
260 encname = encode_streamname(TRUE, stname);
262 TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
264 r = IStorage_OpenStream(stg, encname, NULL,
265 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stm);
266 msi_free( encname );
267 if( FAILED( r ) )
269 WARN("open stream failed r = %08x - empty table?\n", r);
270 return ret;
273 r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
274 if( FAILED( r ) )
276 WARN("open stream failed r = %08x!\n", r);
277 goto end;
280 if( stat.cbSize.QuadPart >> 32 )
282 WARN("Too big!\n");
283 goto end;
286 sz = stat.cbSize.QuadPart;
287 data = msi_alloc( sz );
288 if( !data )
290 WARN("couldn't allocate memory r=%08x!\n", r);
291 ret = ERROR_NOT_ENOUGH_MEMORY;
292 goto end;
295 r = IStream_Read(stm, data, sz, &count );
296 if( FAILED( r ) || ( count != sz ) )
298 msi_free( data );
299 WARN("read stream failed r = %08x!\n", r);
300 goto end;
303 *pdata = data;
304 *psz = sz;
305 ret = ERROR_SUCCESS;
307 end:
308 IStream_Release( stm );
310 return ret;
313 UINT db_get_raw_stream( MSIDATABASE *db, LPCWSTR stname, IStream **stm )
315 LPWSTR encname;
316 HRESULT r;
318 encname = encode_streamname(FALSE, stname);
320 TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
322 r = IStorage_OpenStream(db->storage, encname, NULL,
323 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, stm);
324 if( FAILED( r ) )
326 MSITRANSFORM *transform;
328 LIST_FOR_EACH_ENTRY( transform, &db->transforms, MSITRANSFORM, entry )
330 TRACE("looking for %s in transform storage\n", debugstr_w(stname) );
331 r = IStorage_OpenStream( transform->stg, encname, NULL,
332 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, stm );
333 if (SUCCEEDED(r))
334 break;
338 msi_free( encname );
340 return SUCCEEDED(r) ? ERROR_SUCCESS : ERROR_FUNCTION_FAILED;
343 UINT read_raw_stream_data( MSIDATABASE *db, LPCWSTR stname,
344 USHORT **pdata, UINT *psz )
346 HRESULT r;
347 UINT ret = ERROR_FUNCTION_FAILED;
348 VOID *data;
349 ULONG sz, count;
350 IStream *stm = NULL;
351 STATSTG stat;
353 r = db_get_raw_stream( db, stname, &stm );
354 if( r != ERROR_SUCCESS)
355 return ret;
356 r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
357 if( FAILED( r ) )
359 WARN("open stream failed r = %08x!\n", r);
360 goto end;
363 if( stat.cbSize.QuadPart >> 32 )
365 WARN("Too big!\n");
366 goto end;
369 sz = stat.cbSize.QuadPart;
370 data = msi_alloc( sz );
371 if( !data )
373 WARN("couldn't allocate memory r=%08x!\n", r);
374 ret = ERROR_NOT_ENOUGH_MEMORY;
375 goto end;
378 r = IStream_Read(stm, data, sz, &count );
379 if( FAILED( r ) || ( count != sz ) )
381 msi_free( data );
382 WARN("read stream failed r = %08x!\n", r);
383 goto end;
386 *pdata = data;
387 *psz = sz;
388 ret = ERROR_SUCCESS;
390 end:
391 IStream_Release( stm );
393 return ret;
396 UINT write_stream_data( IStorage *stg, LPCWSTR stname,
397 LPVOID data, UINT sz )
399 HRESULT r;
400 UINT ret = ERROR_FUNCTION_FAILED;
401 ULONG count;
402 IStream *stm = NULL;
403 ULARGE_INTEGER size;
404 LARGE_INTEGER pos;
405 LPWSTR encname;
407 encname = encode_streamname(TRUE, stname );
408 r = IStorage_OpenStream( stg, encname, NULL,
409 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, &stm);
410 if( FAILED(r) )
412 r = IStorage_CreateStream( stg, encname,
413 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
415 msi_free( encname );
416 if( FAILED( r ) )
418 WARN("open stream failed r = %08x\n", r);
419 return ret;
422 size.QuadPart = sz;
423 r = IStream_SetSize( stm, size );
424 if( FAILED( r ) )
426 WARN("Failed to SetSize\n");
427 goto end;
430 pos.QuadPart = 0;
431 r = IStream_Seek( stm, pos, STREAM_SEEK_SET, NULL );
432 if( FAILED( r ) )
434 WARN("Failed to Seek\n");
435 goto end;
438 if (sz)
440 r = IStream_Write(stm, data, sz, &count );
441 if( FAILED( r ) || ( count != sz ) )
443 WARN("Failed to Write\n");
444 goto end;
448 ret = ERROR_SUCCESS;
450 end:
451 IStream_Release( stm );
453 return ret;
456 static void free_table( MSITABLE *table )
458 int i;
459 for( i=0; i<table->row_count; i++ )
460 msi_free( table->data[i] );
461 msi_free( table->data );
462 for( i=0; i<table->nonpersistent_row_count; i++ )
463 msi_free( table->nonpersistent_data[i] );
464 msi_free( table->nonpersistent_data );
465 if( (table->colinfo != _Tables_cols) &&
466 (table->colinfo != _Columns_cols) )
468 msi_free_colinfo( table->colinfo, table->col_count );
469 msi_free( table->colinfo );
471 msi_free( table );
474 static UINT msi_table_get_row_size( const MSICOLUMNINFO *cols, UINT count )
476 const MSICOLUMNINFO *last_col = &cols[count-1];
477 if (!count)
478 return 0;
479 return last_col->offset + bytes_per_column( last_col );
482 /* add this table to the list of cached tables in the database */
483 static UINT read_table_from_storage( MSITABLE *t, IStorage *stg )
485 USHORT *rawdata = NULL;
486 UINT rawsize = 0, i, j, row_size = 0;
488 TRACE("%s\n",debugstr_w(t->name));
490 row_size = msi_table_get_row_size( t->colinfo, t->col_count );
492 /* if we can't read the table, just assume that it's empty */
493 read_stream_data( stg, t->name, &rawdata, &rawsize );
494 if( !rawdata )
495 return ERROR_SUCCESS;
497 TRACE("Read %d bytes\n", rawsize );
499 if( rawsize % row_size )
501 WARN("Table size is invalid %d/%d\n", rawsize, row_size );
502 goto err;
505 t->row_count = rawsize / row_size;
506 t->data = msi_alloc_zero( t->row_count * sizeof (USHORT*) );
507 if( !t->data )
508 goto err;
510 /* transpose all the data */
511 TRACE("Transposing data from %d rows\n", t->row_count );
512 for( i=0; i<t->row_count; i++ )
514 t->data[i] = msi_alloc( row_size );
515 if( !t->data[i] )
516 goto err;
518 for( j=0; j<t->col_count; j++ )
520 UINT ofs = t->colinfo[j].offset/2;
521 UINT n = bytes_per_column( &t->colinfo[j] );
523 switch( n )
525 case 2:
526 t->data[i][ofs] = rawdata[ofs*t->row_count + i ];
527 break;
528 case 4:
529 t->data[i][ofs] = rawdata[ofs*t->row_count + i*2 ];
530 t->data[i][ofs+1] = rawdata[ofs*t->row_count + i*2 + 1];
531 break;
532 default:
533 ERR("oops - unknown column width %d\n", n);
534 goto err;
539 msi_free( rawdata );
540 return ERROR_SUCCESS;
541 err:
542 msi_free( rawdata );
543 return ERROR_FUNCTION_FAILED;
546 void free_cached_tables( MSIDATABASE *db )
548 while( !list_empty( &db->tables ) )
550 MSITABLE *t = LIST_ENTRY( list_head( &db->tables ), MSITABLE, entry );
552 list_remove( &t->entry );
553 free_table( t );
557 static MSITABLE *find_cached_table( MSIDATABASE *db, LPCWSTR name )
559 MSITABLE *t;
561 LIST_FOR_EACH_ENTRY( t, &db->tables, MSITABLE, entry )
562 if( !lstrcmpW( name, t->name ) )
563 return t;
565 return NULL;
568 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name, MSICOLUMNINFO **pcols, UINT *pcount )
570 UINT r, column_count = 0;
571 MSICOLUMNINFO *columns;
573 /* get the number of columns in this table */
574 column_count = 0;
575 r = get_tablecolumns( db, name, NULL, &column_count );
576 if( r != ERROR_SUCCESS )
577 return r;
579 /* if there's no columns, there's no table */
580 if( column_count == 0 )
581 return ERROR_INVALID_PARAMETER;
583 TRACE("Table %s found\n", debugstr_w(name) );
585 columns = msi_alloc( column_count*sizeof (MSICOLUMNINFO) );
586 if( !columns )
587 return ERROR_FUNCTION_FAILED;
589 r = get_tablecolumns( db, name, columns, &column_count );
590 if( r != ERROR_SUCCESS )
592 msi_free( columns );
593 return ERROR_FUNCTION_FAILED;
596 *pcols = columns;
597 *pcount = column_count;
599 return r;
602 UINT msi_create_table( MSIDATABASE *db, LPCWSTR name, column_info *col_info,
603 BOOL persistent, MSITABLE **table_ret)
605 UINT r, nField;
606 MSIVIEW *tv = NULL;
607 MSIRECORD *rec = NULL;
608 column_info *col;
609 MSITABLE *table;
610 UINT i;
612 /* only add tables that don't exist already */
613 if( TABLE_Exists(db, name ) )
615 WARN("table %s exists\n", debugstr_w(name));
616 return ERROR_BAD_QUERY_SYNTAX;
619 table = msi_alloc( sizeof (MSITABLE) + lstrlenW(name)*sizeof (WCHAR) );
620 if( !table )
621 return ERROR_FUNCTION_FAILED;
623 table->row_count = 0;
624 table->data = NULL;
625 table->nonpersistent_row_count = 0;
626 table->nonpersistent_data = NULL;
627 table->colinfo = NULL;
628 table->col_count = 0;
629 lstrcpyW( table->name, name );
631 for( col = col_info; col; col = col->next )
632 table->col_count++;
634 table->colinfo = msi_alloc( table->col_count * sizeof(MSICOLUMNINFO) );
635 if (!table->colinfo)
637 free_table( table );
638 return ERROR_FUNCTION_FAILED;
641 for( i = 0, col = col_info; col; i++, col = col->next )
643 table->colinfo[ i ].tablename = strdupW( col->table );
644 table->colinfo[ i ].number = i + 1;
645 table->colinfo[ i ].colname = strdupW( col->column );
646 table->colinfo[ i ].type = col->type;
647 table->colinfo[ i ].offset = 0;
648 table->colinfo[ i ].hash_table = NULL;
650 table_calc_column_offsets( table->colinfo, table->col_count);
652 r = TABLE_CreateView( db, szTables, &tv );
653 TRACE("CreateView returned %x\n", r);
654 if( r )
656 free_table( table );
657 return r;
660 r = tv->ops->execute( tv, 0 );
661 TRACE("tv execute returned %x\n", r);
662 if( r )
663 goto err;
665 rec = MSI_CreateRecord( 1 );
666 if( !rec )
667 goto err;
669 r = MSI_RecordSetStringW( rec, 1, name );
670 if( r )
671 goto err;
673 r = tv->ops->insert_row( tv, rec, FALSE );
674 TRACE("insert_row returned %x\n", r);
675 if( r )
676 goto err;
678 tv->ops->delete( tv );
679 tv = NULL;
681 msiobj_release( &rec->hdr );
683 /* add each column to the _Columns table */
684 r = TABLE_CreateView( db, szColumns, &tv );
685 if( r )
686 return r;
688 r = tv->ops->execute( tv, 0 );
689 TRACE("tv execute returned %x\n", r);
690 if( r )
691 goto err;
693 rec = MSI_CreateRecord( 4 );
694 if( !rec )
695 goto err;
697 r = MSI_RecordSetStringW( rec, 1, name );
698 if( r )
699 goto err;
702 * need to set the table, column number, col name and type
703 * for each column we enter in the table
705 nField = 1;
706 for( col = col_info; col; col = col->next )
708 r = MSI_RecordSetInteger( rec, 2, nField );
709 if( r )
710 goto err;
712 r = MSI_RecordSetStringW( rec, 3, col->column );
713 if( r )
714 goto err;
716 r = MSI_RecordSetInteger( rec, 4, col->type );
717 if( r )
718 goto err;
720 r = tv->ops->insert_row( tv, rec, !persistent );
721 if( r )
722 goto err;
724 nField++;
726 if( !col )
727 r = ERROR_SUCCESS;
729 err:
730 if (rec)
731 msiobj_release( &rec->hdr );
732 /* FIXME: remove values from the string table on error */
733 if( tv )
734 tv->ops->delete( tv );
736 if (r == ERROR_SUCCESS)
738 list_add_head( &db->tables, &table->entry );
739 *table_ret = table;
741 else
742 free_table( table );
744 return r;
747 static UINT get_table( MSIDATABASE *db, LPCWSTR name, MSITABLE **table_ret )
749 MSITABLE *table;
750 UINT r;
752 /* first, see if the table is cached */
753 table = find_cached_table( db, name );
754 if( table )
756 *table_ret = table;
757 return ERROR_SUCCESS;
760 /* nonexistent tables should be interpreted as empty tables */
761 table = msi_alloc( sizeof (MSITABLE) + lstrlenW(name)*sizeof (WCHAR) );
762 if( !table )
763 return ERROR_FUNCTION_FAILED;
765 table->row_count = 0;
766 table->data = NULL;
767 table->nonpersistent_row_count = 0;
768 table->nonpersistent_data = NULL;
769 table->colinfo = NULL;
770 table->col_count = 0;
771 lstrcpyW( table->name, name );
773 /* these two tables are special - we know the column types already */
774 if( !lstrcmpW( name, szColumns ) )
776 table->colinfo = (MSICOLUMNINFO *)_Columns_cols;
777 table->col_count = sizeof(_Columns_cols)/sizeof(_Columns_cols[0]);
779 else if( !lstrcmpW( name, szTables ) )
781 table->colinfo = (MSICOLUMNINFO *)_Tables_cols;
782 table->col_count = sizeof(_Tables_cols)/sizeof(_Tables_cols[0]);
784 else
786 r = table_get_column_info( db, name, &table->colinfo, &table->col_count);
787 if (r != ERROR_SUCCESS)
789 free_table ( table );
790 return r;
794 r = read_table_from_storage( table, db->storage );
795 if( r != ERROR_SUCCESS )
797 free_table( table );
798 return r;
801 list_add_head( &db->tables, &table->entry );
802 *table_ret = table;
803 return ERROR_SUCCESS;
806 static UINT save_table( MSIDATABASE *db, MSITABLE *t )
808 USHORT *rawdata = NULL, *p;
809 UINT rawsize, r, i, j, row_size;
811 TRACE("Saving %s\n", debugstr_w( t->name ) );
813 row_size = msi_table_get_row_size( t->colinfo, t->col_count );
815 rawsize = t->row_count * row_size;
816 rawdata = msi_alloc_zero( rawsize );
817 if( !rawdata )
819 r = ERROR_NOT_ENOUGH_MEMORY;
820 goto err;
823 p = rawdata;
824 for( i=0; i<t->col_count; i++ )
826 for( j=0; j<t->row_count; j++ )
828 UINT offset = t->colinfo[i].offset;
830 *p++ = t->data[j][offset/2];
831 if( 4 == bytes_per_column( &t->colinfo[i] ) )
832 *p++ = t->data[j][offset/2+1];
836 TRACE("writing %d bytes\n", rawsize);
837 r = write_stream_data( db->storage, t->name, rawdata, rawsize );
839 err:
840 msi_free( rawdata );
842 return r;
845 static void table_calc_column_offsets( MSICOLUMNINFO *colinfo, DWORD count )
847 DWORD i;
849 for( i=0; colinfo && (i<count); i++ )
851 assert( (i+1) == colinfo[ i ].number );
852 if (i)
853 colinfo[i].offset = colinfo[ i - 1 ].offset
854 + bytes_per_column( &colinfo[ i - 1 ] );
855 else
856 colinfo[i].offset = 0;
857 TRACE("column %d is [%s] with type %08x ofs %d\n",
858 colinfo[i].number, debugstr_w(colinfo[i].colname),
859 colinfo[i].type, colinfo[i].offset);
863 static UINT get_defaulttablecolumns( LPCWSTR name, MSICOLUMNINFO *colinfo, UINT *sz)
865 const MSICOLUMNINFO *p;
866 DWORD i, n;
868 TRACE("%s\n", debugstr_w(name));
870 if (!lstrcmpW( name, szTables ))
872 p = _Tables_cols;
873 n = 1;
875 else if (!lstrcmpW( name, szColumns ))
877 p = _Columns_cols;
878 n = 4;
880 else
881 return ERROR_FUNCTION_FAILED;
883 /* duplicate the string data so we can free it in msi_free_colinfo */
884 for (i=0; i<n; i++)
886 if (colinfo && (i < *sz) )
888 memcpy( &colinfo[i], &p[i], sizeof(MSICOLUMNINFO) );
889 colinfo[i].tablename = strdupW( p[i].tablename );
890 colinfo[i].colname = strdupW( p[i].colname );
892 if( colinfo && (i >= *sz) )
893 break;
895 table_calc_column_offsets( colinfo, n );
896 *sz = n;
897 return ERROR_SUCCESS;
900 static void msi_free_colinfo( MSICOLUMNINFO *colinfo, UINT count )
902 UINT i;
904 for( i=0; i<count; i++ )
906 msi_free( (LPWSTR) colinfo[i].tablename );
907 msi_free( (LPWSTR) colinfo[i].colname );
908 msi_free( colinfo[i].hash_table );
912 static LPWSTR msi_makestring( MSIDATABASE *db, UINT stringid)
914 return strdupW(msi_string_lookup_id( db->strings, stringid ));
917 static UINT get_tablecolumns( MSIDATABASE *db,
918 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz)
920 UINT r, i, n=0, table_id, count, maxcount = *sz;
921 MSITABLE *table = NULL;
923 TRACE("%s\n", debugstr_w(szTableName));
925 /* first check if there is a default table with that name */
926 r = get_defaulttablecolumns( szTableName, colinfo, sz );
927 if( ( r == ERROR_SUCCESS ) && *sz )
928 return r;
930 r = get_table( db, szColumns, &table );
931 if( r != ERROR_SUCCESS )
933 ERR("couldn't load _Columns table\n");
934 return ERROR_FUNCTION_FAILED;
937 /* convert table and column names to IDs from the string table */
938 r = msi_string2idW( db->strings, szTableName, &table_id );
939 if( r != ERROR_SUCCESS )
941 WARN("Couldn't find id for %s\n", debugstr_w(szTableName));
942 return r;
945 TRACE("Table id is %d, row count is %d\n", table_id, table->row_count);
947 /* Note: _Columns table doesn't have non-persistent data */
949 /* if maxcount is non-zero, assume it's exactly right for this table */
950 memset( colinfo, 0, maxcount*sizeof(*colinfo) );
951 count = table->row_count;
952 for( i=0; i<count; i++ )
954 if( table->data[ i ][ 0 ] != table_id )
955 continue;
956 if( colinfo )
958 UINT id = table->data[ i ] [ 2 ];
959 UINT col = table->data[ i ][ 1 ] - (1<<15);
961 /* check the column number is in range */
962 if (col<1 || col>maxcount)
964 ERR("column %d out of range\n", col);
965 continue;
968 /* check if this column was already set */
969 if (colinfo[ col - 1 ].number)
971 ERR("duplicate column %d\n", col);
972 continue;
975 colinfo[ col - 1 ].tablename = msi_makestring( db, table_id );
976 colinfo[ col - 1 ].number = col;
977 colinfo[ col - 1 ].colname = msi_makestring( db, id );
978 colinfo[ col - 1 ].type = table->data[ i ] [ 3 ] - (1<<15);
979 colinfo[ col - 1 ].offset = 0;
980 colinfo[ col - 1 ].hash_table = NULL;
982 n++;
985 TRACE("%s has %d columns\n", debugstr_w(szTableName), n);
987 if (colinfo && n != maxcount)
989 ERR("missing column in table %s\n", debugstr_w(szTableName));
990 msi_free_colinfo(colinfo, maxcount );
991 return ERROR_FUNCTION_FAILED;
994 table_calc_column_offsets( colinfo, n );
995 *sz = n;
997 return ERROR_SUCCESS;
1000 /* try to find the table name in the _Tables table */
1001 BOOL TABLE_Exists( MSIDATABASE *db, LPCWSTR name )
1003 UINT r, table_id = 0, i, count;
1004 MSITABLE *table = NULL;
1006 if( !lstrcmpW( name, szTables ) )
1007 return TRUE;
1008 if( !lstrcmpW( name, szColumns ) )
1009 return TRUE;
1011 r = msi_string2idW( db->strings, name, &table_id );
1012 if( r != ERROR_SUCCESS )
1014 TRACE("Couldn't find id for %s\n", debugstr_w(name));
1015 return FALSE;
1018 r = get_table( db, szTables, &table );
1019 if( r != ERROR_SUCCESS )
1021 ERR("table %s not available\n", debugstr_w(szTables));
1022 return FALSE;
1025 count = table->row_count;
1026 for( i=0; i<count; i++ )
1027 if( table->data[ i ][ 0 ] == table_id )
1028 break;
1030 if (i!=count)
1031 return TRUE;
1033 count = table->nonpersistent_row_count;
1034 for( i=0; i<count; i++ )
1035 if( table->nonpersistent_data[ i ][ 0 ] == table_id )
1036 break;
1038 if (i!=count)
1039 return TRUE;
1041 TRACE("Searched %d tables, but %d was not found\n", count, table_id );
1043 return FALSE;
1046 /* below is the query interface to a table */
1048 typedef struct tagMSITABLEVIEW
1050 MSIVIEW view;
1051 MSIDATABASE *db;
1052 MSITABLE *table;
1053 MSICOLUMNINFO *columns;
1054 UINT num_cols;
1055 UINT row_size;
1056 WCHAR name[1];
1057 } MSITABLEVIEW;
1059 static UINT TABLE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
1061 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1062 UINT offset, n;
1063 USHORT **data;
1065 if( !tv->table )
1066 return ERROR_INVALID_PARAMETER;
1068 if( (col==0) || (col>tv->num_cols) )
1069 return ERROR_INVALID_PARAMETER;
1071 /* how many rows are there ? */
1072 if( row >= tv->table->row_count + tv->table->nonpersistent_row_count )
1073 return ERROR_NO_MORE_ITEMS;
1075 if( tv->columns[col-1].offset >= tv->row_size )
1077 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1078 ERR("%p %p\n", tv, tv->columns );
1079 return ERROR_FUNCTION_FAILED;
1082 if (row >= tv->table->row_count)
1084 row -= tv->table->row_count;
1085 data = tv->table->nonpersistent_data;
1087 else
1088 data = tv->table->data;
1089 n = bytes_per_column( &tv->columns[col-1] );
1090 switch( n )
1092 case 4:
1093 offset = tv->columns[col-1].offset/2;
1094 *val = data[row][offset] +
1095 (data[row][offset + 1] << 16);
1096 break;
1097 case 2:
1098 offset = tv->columns[col-1].offset/2;
1099 *val = data[row][offset];
1100 break;
1101 default:
1102 ERR("oops! what is %d bytes per column?\n", n );
1103 return ERROR_FUNCTION_FAILED;
1106 /* TRACE("Data [%d][%d] = %d\n", row, col, *val ); */
1108 return ERROR_SUCCESS;
1112 * We need a special case for streams, as we need to reference column with
1113 * the name of the stream in the same table, and the table name
1114 * which may not be available at higher levels of the query
1116 static UINT TABLE_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm )
1118 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1119 UINT ival = 0, refcol = 0, r;
1120 LPCWSTR sval;
1121 LPWSTR full_name;
1122 DWORD len;
1123 static const WCHAR szDot[] = { '.', 0 };
1124 WCHAR number[0x20];
1126 if( !view->ops->fetch_int )
1127 return ERROR_INVALID_PARAMETER;
1130 * The column marked with the type stream data seems to have a single number
1131 * which references the column containing the name of the stream data
1133 * Fetch the column to reference first.
1135 r = view->ops->fetch_int( view, row, col, &ival );
1136 if( r != ERROR_SUCCESS )
1137 return r;
1139 /* check the column value is in range */
1140 if (ival < 0 || ival > tv->num_cols || ival == col)
1142 ERR("bad column ref (%u) for stream\n", ival);
1143 return ERROR_FUNCTION_FAILED;
1146 if ( tv->columns[ival - 1].type & MSITYPE_STRING )
1148 /* now get the column with the name of the stream */
1149 r = view->ops->fetch_int( view, row, ival, &refcol );
1150 if ( r != ERROR_SUCCESS )
1151 return r;
1153 /* lookup the string value from the string table */
1154 sval = msi_string_lookup_id( tv->db->strings, refcol );
1155 if ( !sval )
1156 return ERROR_INVALID_PARAMETER;
1158 else
1160 static const WCHAR fmt[] = { '%','d',0 };
1161 sprintfW( number, fmt, ival );
1162 sval = number;
1165 len = lstrlenW( tv->name ) + 2 + lstrlenW( sval );
1166 full_name = msi_alloc( len*sizeof(WCHAR) );
1167 lstrcpyW( full_name, tv->name );
1168 lstrcatW( full_name, szDot );
1169 lstrcatW( full_name, sval );
1171 r = db_get_raw_stream( tv->db, full_name, stm );
1172 if( r )
1173 ERR("fetching stream %s, error = %d\n",debugstr_w(full_name), r);
1174 msi_free( full_name );
1176 return r;
1179 static UINT TABLE_set_int( MSITABLEVIEW *tv, UINT row, UINT col, UINT val )
1181 UINT offset, n;
1182 USHORT **data;
1184 if( !tv->table )
1185 return ERROR_INVALID_PARAMETER;
1187 if( (col==0) || (col>tv->num_cols) )
1188 return ERROR_INVALID_PARAMETER;
1190 if( row >= tv->table->row_count + tv->table->nonpersistent_row_count )
1191 return ERROR_INVALID_PARAMETER;
1193 if( tv->columns[col-1].offset >= tv->row_size )
1195 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1196 ERR("%p %p\n", tv, tv->columns );
1197 return ERROR_FUNCTION_FAILED;
1200 msi_free( tv->columns[col-1].hash_table );
1201 tv->columns[col-1].hash_table = NULL;
1203 if (row >= tv->table->row_count)
1205 row -= tv->table->row_count;
1206 data = tv->table->nonpersistent_data;
1208 else
1209 data = tv->table->data;
1210 n = bytes_per_column( &tv->columns[col-1] );
1211 switch( n )
1213 case 4:
1214 offset = tv->columns[col-1].offset/2;
1215 data[row][offset] = val & 0xffff;
1216 data[row][offset + 1] = (val>>16)&0xffff;
1217 break;
1218 case 2:
1219 offset = tv->columns[col-1].offset/2;
1220 data[row][offset] = val;
1221 break;
1222 default:
1223 ERR("oops! what is %d bytes per column?\n", n );
1224 return ERROR_FUNCTION_FAILED;
1226 return ERROR_SUCCESS;
1229 static UINT TABLE_set_row( struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask )
1231 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1232 UINT i, val, r = ERROR_SUCCESS;
1234 if ( !tv->table )
1235 return ERROR_INVALID_PARAMETER;
1237 /* test if any of the mask bits are invalid */
1238 if ( mask >= (1<<tv->num_cols) )
1239 return ERROR_INVALID_PARAMETER;
1241 for ( i = 0; i < tv->num_cols; i++ )
1243 /* only update the fields specified in the mask */
1244 if ( !(mask&(1<<i)) )
1245 continue;
1247 /* FIXME: should we allow updating keys? */
1249 val = 0;
1250 if ( !MSI_RecordIsNull( rec, i + 1 ) )
1252 if ( MSITYPE_IS_BINARY(tv->columns[ i ].type) )
1254 val = 1; /* refers to the first key column */
1256 else if ( tv->columns[i].type & MSITYPE_STRING )
1258 LPCWSTR sval = MSI_RecordGetString( rec, i + 1 );
1259 val = msi_addstringW( tv->db->strings, 0, sval, -1, 1, StringPersistent );
1261 else if ( 2 == bytes_per_column( &tv->columns[ i ] ) )
1263 val = 0x8000 + MSI_RecordGetInteger( rec, i + 1 );
1264 if ( val & 0xffff0000 )
1266 ERR("field %u value %d out of range\n", i+1, val - 0x8000 );
1267 return ERROR_FUNCTION_FAILED;
1270 else
1272 INT ival = MSI_RecordGetInteger( rec, i + 1 );
1273 val = ival ^ 0x80000000;
1277 r = TABLE_set_int( tv, row, i+1, val );
1278 if ( r != ERROR_SUCCESS )
1279 break;
1281 return r;
1284 static UINT table_create_new_row( struct tagMSIVIEW *view, UINT *num, BOOL temporary )
1286 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1287 USHORT **p, *row;
1288 UINT sz;
1289 USHORT ***data_ptr;
1290 UINT *row_count;
1292 TRACE("%p %s\n", view, temporary ? "TRUE" : "FALSE");
1294 if( !tv->table )
1295 return ERROR_INVALID_PARAMETER;
1297 row = msi_alloc_zero( tv->row_size );
1298 if( !row )
1299 return ERROR_NOT_ENOUGH_MEMORY;
1301 if( temporary )
1303 row_count = &tv->table->nonpersistent_row_count;
1304 data_ptr = &tv->table->nonpersistent_data;
1305 *num = tv->table->row_count + tv->table->nonpersistent_row_count;
1307 else
1309 row_count = &tv->table->row_count;
1310 data_ptr = &tv->table->data;
1311 *num = tv->table->row_count;
1314 sz = (*row_count + 1) * sizeof (UINT*);
1315 if( *data_ptr )
1316 p = msi_realloc( *data_ptr, sz );
1317 else
1318 p = msi_alloc( sz );
1319 if( !p )
1321 msi_free( row );
1322 return ERROR_NOT_ENOUGH_MEMORY;
1325 *data_ptr = p;
1326 (*data_ptr)[*row_count] = row;
1327 (*row_count)++;
1329 return ERROR_SUCCESS;
1332 static UINT TABLE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
1334 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1336 TRACE("%p %p\n", tv, record);
1338 TRACE("There are %d columns\n", tv->num_cols );
1340 return ERROR_SUCCESS;
1343 static UINT TABLE_close( struct tagMSIVIEW *view )
1345 TRACE("%p\n", view );
1347 return ERROR_SUCCESS;
1350 static UINT TABLE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols)
1352 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1354 TRACE("%p %p %p\n", view, rows, cols );
1356 if( cols )
1357 *cols = tv->num_cols;
1358 if( rows )
1360 if( !tv->table )
1361 return ERROR_INVALID_PARAMETER;
1362 *rows = tv->table->row_count + tv->table->nonpersistent_row_count;
1365 return ERROR_SUCCESS;
1368 static UINT TABLE_get_column_info( struct tagMSIVIEW *view,
1369 UINT n, LPWSTR *name, UINT *type )
1371 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1373 TRACE("%p %d %p %p\n", tv, n, name, type );
1375 if( ( n == 0 ) || ( n > tv->num_cols ) )
1376 return ERROR_INVALID_PARAMETER;
1378 if( name )
1380 *name = strdupW( tv->columns[n-1].colname );
1381 if( !*name )
1382 return ERROR_FUNCTION_FAILED;
1384 if( type )
1385 *type = tv->columns[n-1].type;
1387 return ERROR_SUCCESS;
1390 static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row );
1392 static UINT table_validate_new( MSITABLEVIEW *tv, MSIRECORD *rec )
1394 UINT r, row, i;
1396 /* check there's no null values where they're not allowed */
1397 for( i = 0; i < tv->num_cols; i++ )
1399 if ( tv->columns[i].type & MSITYPE_NULLABLE )
1400 continue;
1402 if ( MSITYPE_IS_BINARY(tv->columns[i].type) )
1403 TRACE("skipping binary column\n");
1404 else if ( tv->columns[i].type & MSITYPE_STRING )
1406 LPCWSTR str;
1408 str = MSI_RecordGetString( rec, i+1 );
1409 if (str == NULL || str[0] == 0)
1410 return ERROR_INVALID_DATA;
1412 else
1414 UINT n;
1416 n = MSI_RecordGetInteger( rec, i+1 );
1417 if (n == MSI_NULL_INTEGER)
1418 return ERROR_INVALID_DATA;
1422 /* check there's no duplicate keys */
1423 r = msi_table_find_row( tv, rec, &row );
1424 if (r == ERROR_SUCCESS)
1425 return ERROR_INVALID_DATA;
1427 return ERROR_SUCCESS;
1430 static UINT TABLE_insert_row( struct tagMSIVIEW *view, MSIRECORD *rec, BOOL temporary )
1432 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1433 UINT r, row = -1;
1435 TRACE("%p %p %s\n", tv, rec, temporary ? "TRUE" : "FALSE" );
1437 /* check that the key is unique - can we find a matching row? */
1438 r = table_validate_new( tv, rec );
1439 if( r != ERROR_SUCCESS )
1440 return ERROR_FUNCTION_FAILED;
1442 r = table_create_new_row( view, &row, temporary );
1443 TRACE("insert_row returned %08x\n", r);
1444 if( r != ERROR_SUCCESS )
1445 return r;
1447 return TABLE_set_row( view, row, rec, (1<<tv->num_cols) - 1 );
1450 static UINT TABLE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
1451 MSIRECORD *rec)
1453 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1454 UINT r;
1456 TRACE("%p %d %p\n", view, eModifyMode, rec );
1458 switch (eModifyMode)
1460 case MSIMODIFY_VALIDATE_NEW:
1461 r = table_validate_new( tv, rec );
1462 break;
1464 case MSIMODIFY_INSERT_TEMPORARY:
1465 r = table_validate_new( tv, rec );
1466 if (r != ERROR_SUCCESS)
1467 break;
1468 r = TABLE_insert_row( view, rec, TRUE );
1469 break;
1471 case MSIMODIFY_REFRESH:
1472 case MSIMODIFY_INSERT:
1473 case MSIMODIFY_UPDATE:
1474 case MSIMODIFY_ASSIGN:
1475 case MSIMODIFY_REPLACE:
1476 case MSIMODIFY_MERGE:
1477 case MSIMODIFY_DELETE:
1478 case MSIMODIFY_VALIDATE:
1479 case MSIMODIFY_VALIDATE_FIELD:
1480 case MSIMODIFY_VALIDATE_DELETE:
1481 FIXME("%p %d %p - mode not implemented\n", view, eModifyMode, rec );
1482 r = ERROR_CALL_NOT_IMPLEMENTED;
1483 break;
1485 default:
1486 r = ERROR_INVALID_DATA;
1489 return r;
1492 static UINT TABLE_delete( struct tagMSIVIEW *view )
1494 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1496 TRACE("%p\n", view );
1498 tv->table = NULL;
1500 tv->columns = NULL;
1502 msi_free( tv );
1504 return ERROR_SUCCESS;
1507 static UINT TABLE_find_matching_rows( struct tagMSIVIEW *view, UINT col,
1508 UINT val, UINT *row, MSIITERHANDLE *handle )
1510 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1511 const MSICOLUMNHASHENTRY *entry;
1513 TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
1515 if( !tv->table )
1516 return ERROR_INVALID_PARAMETER;
1518 if( (col==0) || (col > tv->num_cols) )
1519 return ERROR_INVALID_PARAMETER;
1521 if( !tv->columns[col-1].hash_table )
1523 UINT i;
1524 UINT num_rows = tv->table->row_count + tv->table->nonpersistent_row_count;
1525 MSICOLUMNHASHENTRY **hash_table;
1526 MSICOLUMNHASHENTRY *new_entry;
1528 if( tv->columns[col-1].offset >= tv->row_size )
1530 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1531 ERR("%p %p\n", tv, tv->columns );
1532 return ERROR_FUNCTION_FAILED;
1535 /* allocate contiguous memory for the table and its entries so we
1536 * don't have to do an expensive cleanup */
1537 hash_table = msi_alloc(MSITABLE_HASH_TABLE_SIZE * sizeof(MSICOLUMNHASHENTRY*) +
1538 num_rows * sizeof(MSICOLUMNHASHENTRY));
1539 if (!hash_table)
1540 return ERROR_OUTOFMEMORY;
1542 memset(hash_table, 0, MSITABLE_HASH_TABLE_SIZE * sizeof(MSICOLUMNHASHENTRY*));
1543 tv->columns[col-1].hash_table = hash_table;
1545 new_entry = (MSICOLUMNHASHENTRY *)(hash_table + MSITABLE_HASH_TABLE_SIZE);
1547 for (i = 0; i < num_rows; i++, new_entry++)
1549 UINT row_value, n;
1550 UINT offset;
1551 USHORT **data;
1552 UINT row = i;
1553 if( row >= tv->table->row_count )
1555 row -= tv->table->row_count;
1556 data = tv->table->nonpersistent_data;
1558 else
1559 data = tv->table->data;
1560 n = bytes_per_column( &tv->columns[col-1] );
1561 switch( n )
1563 case 4:
1564 offset = tv->columns[col-1].offset/2;
1565 row_value = data[row][offset] +
1566 (data[row][offset + 1] << 16);
1567 break;
1568 case 2:
1569 offset = tv->columns[col-1].offset/2;
1570 row_value = data[row][offset];
1571 break;
1572 default:
1573 ERR("oops! what is %d bytes per column?\n", n );
1574 continue;
1577 new_entry->next = NULL;
1578 new_entry->value = row_value;
1579 new_entry->row = i;
1580 if (hash_table[row_value % MSITABLE_HASH_TABLE_SIZE])
1582 MSICOLUMNHASHENTRY *prev_entry = hash_table[row_value % MSITABLE_HASH_TABLE_SIZE];
1583 while (prev_entry->next)
1584 prev_entry = prev_entry->next;
1585 prev_entry->next = new_entry;
1587 else
1588 hash_table[row_value % MSITABLE_HASH_TABLE_SIZE] = new_entry;
1592 if( !*handle )
1593 entry = tv->columns[col-1].hash_table[val % MSITABLE_HASH_TABLE_SIZE];
1594 else
1595 entry = (*handle)->next;
1597 while (entry && entry->value != val)
1598 entry = entry->next;
1600 *handle = entry;
1601 if (!entry)
1602 return ERROR_NO_MORE_ITEMS;
1604 *row = entry->row;
1605 return ERROR_SUCCESS;
1609 static const MSIVIEWOPS table_ops =
1611 TABLE_fetch_int,
1612 TABLE_fetch_stream,
1613 TABLE_set_row,
1614 TABLE_insert_row,
1615 TABLE_execute,
1616 TABLE_close,
1617 TABLE_get_dimensions,
1618 TABLE_get_column_info,
1619 TABLE_modify,
1620 TABLE_delete,
1621 TABLE_find_matching_rows
1624 UINT TABLE_CreateView( MSIDATABASE *db, LPCWSTR name, MSIVIEW **view )
1626 MSITABLEVIEW *tv ;
1627 UINT r, sz;
1629 TRACE("%p %s %p\n", db, debugstr_w(name), view );
1631 sz = sizeof *tv + lstrlenW(name)*sizeof name[0] ;
1632 tv = msi_alloc_zero( sz );
1633 if( !tv )
1634 return ERROR_FUNCTION_FAILED;
1636 r = get_table( db, name, &tv->table );
1637 if( r != ERROR_SUCCESS )
1639 msi_free( tv );
1640 WARN("table not found\n");
1641 return r;
1644 TRACE("table %p found with %d columns\n", tv->table, tv->table->col_count);
1646 /* fill the structure */
1647 tv->view.ops = &table_ops;
1648 tv->db = db;
1649 tv->columns = tv->table->colinfo;
1650 tv->num_cols = tv->table->col_count;
1651 tv->row_size = msi_table_get_row_size( tv->table->colinfo, tv->table->col_count );
1653 TRACE("%s one row is %d bytes\n", debugstr_w(name), tv->row_size );
1655 *view = (MSIVIEW*) tv;
1656 lstrcpyW( tv->name, name );
1658 return ERROR_SUCCESS;
1661 UINT MSI_CommitTables( MSIDATABASE *db )
1663 UINT r;
1664 MSITABLE *table = NULL;
1666 TRACE("%p\n",db);
1668 r = msi_save_string_table( db->strings, db->storage );
1669 if( r != ERROR_SUCCESS )
1671 WARN("failed to save string table r=%08x\n",r);
1672 return r;
1675 LIST_FOR_EACH_ENTRY( table, &db->tables, MSITABLE, entry )
1677 r = save_table( db, table );
1678 if( r != ERROR_SUCCESS )
1680 WARN("failed to save table %s (r=%08x)\n",
1681 debugstr_w(table->name), r);
1682 return r;
1686 /* force everything to reload next time */
1687 free_cached_tables( db );
1689 return ERROR_SUCCESS;
1692 MSICONDITION MSI_DatabaseIsTablePersistent( MSIDATABASE *db, LPCWSTR table )
1694 if (!table)
1695 return MSICONDITION_ERROR;
1697 if (!TABLE_Exists( db, table ))
1698 return MSICONDITION_NONE;
1700 return MSICONDITION_FALSE;
1703 static MSIRECORD *msi_get_transform_record( MSITABLEVIEW *tv, string_table *st, USHORT *rawdata )
1705 UINT i, val, ofs = 0;
1706 USHORT mask = *rawdata++;
1707 MSICOLUMNINFO *columns = tv->columns;
1708 MSIRECORD *rec;
1710 rec = MSI_CreateRecord( tv->num_cols );
1711 if( !rec )
1712 return rec;
1714 TRACE("row ->\n");
1715 for( i=0; i<tv->num_cols; i++ )
1717 UINT n = bytes_per_column( &columns[i] );
1719 if ( (mask&1) && (i>=(mask>>8)) )
1720 break;
1721 /* all keys must be present */
1722 if ( (~mask&1) && (~columns[i].type & MSITYPE_KEY) && ((1<<i) & ~mask) )
1723 continue;
1725 switch( n )
1727 case 2:
1728 val = rawdata[ofs];
1729 if( (columns[i].type & MSITYPE_STRING) &&
1730 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
1732 LPCWSTR sval = msi_string_lookup_id( st, val );
1733 MSI_RecordSetStringW( rec, i+1, sval );
1734 TRACE(" field %d [%s]\n", i+1, debugstr_w(sval));
1736 else
1738 if (val)
1739 MSI_RecordSetInteger( rec, i+1, val^0x8000 );
1740 TRACE(" field %d [0x%04x]\n", i+1, val );
1742 break;
1743 case 4:
1744 val = (rawdata[ofs] + (rawdata[ofs + 1]<<16));
1745 if (val)
1746 MSI_RecordSetInteger( rec, i+1, val^0x80000000 );
1747 TRACE(" field %d [0x%08x]\n", i+1, val );
1748 break;
1749 default:
1750 ERR("oops - unknown column width %d\n", n);
1751 break;
1753 ofs += n/2;
1755 return rec;
1758 static void dump_record( MSIRECORD *rec )
1760 UINT i, n;
1762 n = MSI_RecordGetFieldCount( rec );
1763 for( i=1; i<=n; i++ )
1765 LPCWSTR sval = MSI_RecordGetString( rec, i );
1767 if( MSI_RecordIsNull( rec, i ) )
1768 TRACE("row -> []\n");
1769 else if( (sval = MSI_RecordGetString( rec, i )) )
1770 TRACE("row -> [%s]\n", debugstr_w(sval));
1771 else
1772 TRACE("row -> [0x%08x]\n", MSI_RecordGetInteger( rec, i ) );
1776 static void dump_table( string_table *st, USHORT *rawdata, UINT rawsize )
1778 LPCWSTR sval;
1779 UINT i;
1781 for( i=0; i<(rawsize/2); i++ )
1783 sval = msi_string_lookup_id( st, rawdata[i] );
1784 MESSAGE(" %04x %s\n", rawdata[i], debugstr_w(sval) );
1788 static UINT* msi_record_to_row( MSITABLEVIEW *tv, MSIRECORD *rec )
1790 LPCWSTR str;
1791 UINT i, r, *data;
1793 data = msi_alloc( tv->num_cols *sizeof (UINT) );
1794 for( i=0; i<tv->num_cols; i++ )
1796 data[i] = 0;
1798 if ( ~tv->columns[i].type & MSITYPE_KEY )
1799 continue;
1801 /* turn the transform column value into a row value */
1802 if ( ( tv->columns[i].type & MSITYPE_STRING ) &&
1803 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
1805 str = MSI_RecordGetString( rec, i+1 );
1806 r = msi_string2idW( tv->db->strings, str, &data[i] );
1808 /* if there's no matching string in the string table,
1809 these keys can't match any record, so fail now. */
1810 if( ERROR_SUCCESS != r )
1812 msi_free( data );
1813 return NULL;
1816 else
1818 data[i] = MSI_RecordGetInteger( rec, i+1 );
1819 if ((tv->columns[i].type&0xff) == 2)
1820 data[i] += 0x8000;
1821 else
1822 data[i] += 0x80000000;
1825 return data;
1828 static UINT msi_row_matches( MSITABLEVIEW *tv, UINT row, UINT *data )
1830 UINT i, r, x, ret = ERROR_FUNCTION_FAILED;
1832 for( i=0; i<tv->num_cols; i++ )
1834 if ( ~tv->columns[i].type & MSITYPE_KEY )
1835 continue;
1837 /* turn the transform column value into a row value */
1838 r = TABLE_fetch_int( &tv->view, row, i+1, &x );
1839 if ( r != ERROR_SUCCESS )
1841 ERR("TABLE_fetch_int shouldn't fail here\n");
1842 break;
1845 /* if this key matches, move to the next column */
1846 if ( x != data[i] )
1848 ret = ERROR_FUNCTION_FAILED;
1849 break;
1852 ret = ERROR_SUCCESS;
1855 return ret;
1858 static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row )
1860 UINT i, r = ERROR_FUNCTION_FAILED, *data;
1862 data = msi_record_to_row( tv, rec );
1863 if( !data )
1864 return r;
1865 for( i = 0; i < tv->table->row_count + tv->table->nonpersistent_row_count; i++ )
1867 r = msi_row_matches( tv, i, data );
1868 if( r == ERROR_SUCCESS )
1870 *row = i;
1871 break;
1874 msi_free( data );
1875 return r;
1878 static UINT msi_delete_row( MSITABLEVIEW *tv, UINT row )
1880 UINT i;
1882 for( i=1; i<=tv->num_cols; i++ )
1883 TABLE_set_int( tv, row, i, 0 );
1884 return ERROR_SUCCESS;
1887 static UINT msi_table_load_transform( MSIDATABASE *db, IStorage *stg,
1888 string_table *st, LPCWSTR name )
1890 UINT rawsize = 0;
1891 USHORT *rawdata = NULL;
1892 MSITABLEVIEW *tv = NULL;
1893 UINT r, n, sz, i, mask;
1894 MSIRECORD *rec = NULL;
1895 UINT colcol = 0;
1896 WCHAR coltable[32];
1898 coltable[0] = 0;
1899 TRACE("%p %p %p %s\n", db, stg, st, debugstr_w(name) );
1901 /* read the transform data */
1902 read_stream_data( stg, name, &rawdata, &rawsize );
1903 if ( !rawdata )
1905 TRACE("table %s empty\n", debugstr_w(name) );
1906 return ERROR_INVALID_TABLE;
1909 /* create a table view */
1910 r = TABLE_CreateView( db, name, (MSIVIEW**) &tv );
1911 if( r != ERROR_SUCCESS )
1912 goto err;
1914 r = tv->view.ops->execute( &tv->view, NULL );
1915 if( r != ERROR_SUCCESS )
1916 goto err;
1918 TRACE("name = %s columns = %u row_size = %u raw size = %u\n",
1919 debugstr_w(name), tv->num_cols, tv->row_size, rawsize );
1921 /* interpret the data */
1922 r = ERROR_SUCCESS;
1923 for( n=0; n < (rawsize/2); )
1925 mask = rawdata[n];
1927 if (mask&1)
1930 * if the low bit is set, columns are continuous and
1931 * the number of columns is specified in the high byte
1933 sz = 2 + tv->row_size;
1935 else
1938 * If the low bit is not set, rowdata[n] is a bitmask.
1939 * Excepting for key fields, which are always present,
1940 * each bit indicates that a field is present in the transform record.
1942 * rawdata[n] == 0 is a special case ... only the keys will be present
1943 * and it means that this row should be deleted.
1945 sz = 2;
1946 for( i=0; i<tv->num_cols; i++ )
1948 if( (tv->columns[i].type & MSITYPE_KEY) || ((1<<i)&mask))
1949 sz += bytes_per_column( &tv->columns[i] );
1953 /* check we didn't run of the end of the table */
1954 if ( (n+sz) > rawsize )
1956 ERR("borked.\n");
1957 dump_table( st, rawdata, rawsize );
1958 break;
1961 rec = msi_get_transform_record( tv, st, &rawdata[n] );
1962 if (rec)
1964 if ( mask & 1 )
1966 TRACE("inserting record\n");
1969 * Native msi seems writes nul into the
1970 * Number (2nd) column of the _Columns table.
1971 * Not sure that it's deliberate...
1973 if (!lstrcmpW(name, szColumns))
1975 WCHAR table[32];
1976 DWORD sz = 32;
1978 MSI_RecordGetStringW( rec, 1, table, &sz );
1980 /* reset the column number on a new table */
1981 if ( lstrcmpW(coltable, table) )
1983 colcol = 0;
1984 lstrcpyW( coltable, table );
1987 /* fix nul column numbers */
1988 MSI_RecordSetInteger( rec, 2, ++colcol );
1991 r = TABLE_insert_row( &tv->view, rec, FALSE );
1992 if (r != ERROR_SUCCESS)
1993 ERR("insert row failed\n");
1995 else
1997 UINT row = 0;
1999 r = msi_table_find_row( tv, rec, &row );
2000 if (r != ERROR_SUCCESS)
2001 ERR("no matching row to transform\n");
2002 else if ( mask )
2004 TRACE("modifying row [%d]:\n", row);
2005 TABLE_set_row( &tv->view, row, rec, mask );
2007 else
2009 TRACE("deleting row [%d]:\n", row);
2010 msi_delete_row( tv, row );
2013 if( TRACE_ON(msidb) ) dump_record( rec );
2014 msiobj_release( &rec->hdr );
2017 n += sz/2;
2020 err:
2021 /* no need to free the table, it's associated with the database */
2022 msi_free( rawdata );
2023 if( tv )
2024 tv->view.ops->delete( &tv->view );
2026 return ERROR_SUCCESS;
2030 * msi_table_apply_transform
2032 * Enumerate the table transforms in a transform storage and apply each one.
2034 UINT msi_table_apply_transform( MSIDATABASE *db, IStorage *stg )
2036 IEnumSTATSTG *stgenum = NULL;
2037 HRESULT r;
2038 STATSTG stat;
2039 ULONG count;
2040 WCHAR name[0x40];
2041 string_table *strings;
2042 UINT ret = ERROR_FUNCTION_FAILED;
2044 TRACE("%p %p\n", db, stg );
2046 strings = msi_load_string_table( stg );
2047 if( !strings )
2048 goto end;
2050 r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
2051 if( FAILED( r ) )
2052 goto end;
2055 * Apply _Tables and _Columns transforms first so that
2056 * the table metadata is correct, and empty tables exist.
2058 ret = msi_table_load_transform( db, stg, strings, szTables );
2059 if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
2060 goto end;
2062 ret = msi_table_load_transform( db, stg, strings, szColumns );
2063 if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
2064 goto end;
2066 ret = ERROR_SUCCESS;
2068 while( r == ERROR_SUCCESS )
2070 count = 0;
2071 r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
2072 if( FAILED( r ) || !count )
2073 break;
2075 decode_streamname( stat.pwcsName, name );
2076 if ( name[0] != 0x4840 )
2077 continue;
2079 TRACE("transform contains stream %s\n", debugstr_w(name));
2081 if ( !lstrcmpW( name+1, szStringPool ) ||
2082 !lstrcmpW( name+1, szStringData ) ||
2083 !lstrcmpW( name+1, szColumns ) ||
2084 !lstrcmpW( name+1, szTables ) )
2085 continue;
2087 ret = msi_table_load_transform( db, stg, strings, name+1 );
2090 if ( ret == ERROR_SUCCESS )
2091 append_storage_to_db( db, stg );
2093 end:
2094 if ( stgenum )
2095 IEnumSTATSTG_Release( stgenum );
2096 if ( strings )
2097 msi_destroy_stringtable( strings );
2099 return ret;
2102 void append_storage_to_db( MSIDATABASE *db, IStorage *stg )
2104 MSITRANSFORM *t;
2106 t = msi_alloc( sizeof *t );
2107 t->stg = stg;
2108 IStorage_AddRef( stg );
2109 list_add_tail( &db->transforms, &t->entry );
2112 void msi_free_transforms( MSIDATABASE *db )
2114 while( !list_empty( &db->transforms ) )
2116 MSITRANSFORM *t = LIST_ENTRY( list_head( &db->transforms ),
2117 MSITRANSFORM, entry );
2118 list_remove( &t->entry );
2119 IStorage_Release( t->stg );
2120 msi_free( t );