msi: Implement adding columns using the ALTER command.
[wine/multimedia.git] / dlls / msi / table.c
blob1dba764b4aa05f0c7ae093becedc46b7c94d368a
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 LPCWSTR tablename;
57 UINT number;
58 LPCWSTR colname;
59 UINT type;
60 UINT offset;
61 MSICOLUMNHASHENTRY **hash_table;
62 } MSICOLUMNINFO;
64 struct tagMSITABLE
66 BYTE **data;
67 UINT row_count;
68 BYTE **nonpersistent_data;
69 UINT nonpersistent_row_count;
70 struct list entry;
71 MSICOLUMNINFO *colinfo;
72 UINT col_count;
73 BOOL persistent;
74 INT ref_count;
75 WCHAR name[1];
78 typedef struct tagMSITRANSFORM {
79 struct list entry;
80 IStorage *stg;
81 } MSITRANSFORM;
83 static const WCHAR szStringData[] = {
84 '_','S','t','r','i','n','g','D','a','t','a',0 };
85 static const WCHAR szStringPool[] = {
86 '_','S','t','r','i','n','g','P','o','o','l',0 };
88 /* information for default tables */
89 static const WCHAR szTables[] = { '_','T','a','b','l','e','s',0 };
90 static const WCHAR szTable[] = { 'T','a','b','l','e',0 };
91 static const WCHAR szName[] = { 'N','a','m','e',0 };
92 static const WCHAR szColumns[] = { '_','C','o','l','u','m','n','s',0 };
93 static const WCHAR szNumber[] = { 'N','u','m','b','e','r',0 };
94 static const WCHAR szType[] = { 'T','y','p','e',0 };
96 /* These tables are written into (the .hash_table part).
97 * Do not mark them const.
99 static MSICOLUMNINFO _Columns_cols[4] = {
100 { szColumns, 1, szTable, MSITYPE_VALID | MSITYPE_STRING | MSITYPE_KEY | 64, 0 },
101 { szColumns, 2, szNumber, MSITYPE_VALID | MSITYPE_KEY | 2, 2 },
102 { szColumns, 3, szName, MSITYPE_VALID | MSITYPE_STRING | 64, 4 },
103 { szColumns, 4, szType, MSITYPE_VALID | 2, 6 },
105 static MSICOLUMNINFO _Tables_cols[1] = {
106 { szTables, 1, szName, MSITYPE_VALID | MSITYPE_STRING | 64, 0 },
109 #define MAX_STREAM_NAME 0x1f
111 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name,
112 MSICOLUMNINFO **pcols, UINT *pcount );
113 static void table_calc_column_offsets( MSICOLUMNINFO *colinfo, DWORD count );
114 static UINT get_tablecolumns( MSIDATABASE *db,
115 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz);
116 static void msi_free_colinfo( MSICOLUMNINFO *colinfo, UINT count );
119 void msi_table_set_strref(UINT bytes_per_strref)
121 _Columns_cols[0].offset = 0;
122 _Columns_cols[1].offset = bytes_per_strref;
123 _Columns_cols[2].offset = _Columns_cols[1].offset + sizeof(USHORT);
124 _Columns_cols[3].offset = _Columns_cols[2].offset + bytes_per_strref;
127 static inline UINT bytes_per_column( const MSICOLUMNINFO *col )
129 if( MSITYPE_IS_BINARY(col->type) )
130 return 2;
131 if( col->type & MSITYPE_STRING )
132 return _Columns_cols[1].offset;
133 if( (col->type & 0xff) > 4 )
134 ERR("Invalid column size!\n");
135 return col->type & 0xff;
138 static int utf2mime(int x)
140 if( (x>='0') && (x<='9') )
141 return x-'0';
142 if( (x>='A') && (x<='Z') )
143 return x-'A'+10;
144 if( (x>='a') && (x<='z') )
145 return x-'a'+10+26;
146 if( x=='.' )
147 return 10+26+26;
148 if( x=='_' )
149 return 10+26+26+1;
150 return -1;
153 LPWSTR encode_streamname(BOOL bTable, LPCWSTR in)
155 DWORD count = MAX_STREAM_NAME;
156 DWORD ch, next;
157 LPWSTR out, p;
159 if( !bTable )
160 count = lstrlenW( in )+2;
161 out = msi_alloc( count*sizeof(WCHAR) );
162 p = out;
164 if( bTable )
166 *p++ = 0x4840;
167 count --;
169 while( count -- )
171 ch = *in++;
172 if( !ch )
174 *p = ch;
175 return out;
177 if( ( ch < 0x80 ) && ( utf2mime(ch) >= 0 ) )
179 ch = utf2mime(ch) + 0x4800;
180 next = *in;
181 if( next && (next<0x80) )
183 next = utf2mime(next);
184 if( next >= 0 )
186 next += 0x3ffffc0;
187 ch += (next<<6);
188 in++;
192 *p++ = ch;
194 ERR("Failed to encode stream name (%s)\n",debugstr_w(in));
195 msi_free( out );
196 return NULL;
199 static int mime2utf(int x)
201 if( x<10 )
202 return x + '0';
203 if( x<(10+26))
204 return x - 10 + 'A';
205 if( x<(10+26+26))
206 return x - 10 - 26 + 'a';
207 if( x == (10+26+26) )
208 return '.';
209 return '_';
212 BOOL decode_streamname(LPCWSTR in, LPWSTR out)
214 WCHAR ch;
215 DWORD count = 0;
217 while ( (ch = *in++) )
219 if( (ch >= 0x3800 ) && (ch < 0x4840 ) )
221 if( ch >= 0x4800 )
222 ch = mime2utf(ch-0x4800);
223 else
225 ch -= 0x3800;
226 *out++ = mime2utf(ch&0x3f);
227 count++;
228 ch = mime2utf((ch>>6)&0x3f);
231 *out++ = ch;
232 count++;
234 *out = 0;
235 return count;
238 void enum_stream_names( IStorage *stg )
240 IEnumSTATSTG *stgenum = NULL;
241 HRESULT r;
242 STATSTG stat;
243 ULONG n, count;
244 WCHAR name[0x40];
246 r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
247 if( FAILED( r ) )
248 return;
250 n = 0;
251 while( 1 )
253 count = 0;
254 r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
255 if( FAILED( r ) || !count )
256 break;
257 decode_streamname( stat.pwcsName, name );
258 TRACE("stream %2d -> %s %s\n", n,
259 debugstr_w(stat.pwcsName), debugstr_w(name) );
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 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 int 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 if( (table->colinfo != _Tables_cols) &&
483 (table->colinfo != _Columns_cols) )
485 msi_free_colinfo( table->colinfo, table->col_count );
486 msi_free( table->colinfo );
488 msi_free( table );
491 static UINT msi_table_get_row_size( const MSICOLUMNINFO *cols, UINT count )
493 const MSICOLUMNINFO *last_col = &cols[count-1];
494 if (!count)
495 return 0;
496 return last_col->offset + bytes_per_column( last_col );
499 /* add this table to the list of cached tables in the database */
500 static UINT read_table_from_storage( MSITABLE *t, IStorage *stg )
502 BYTE *rawdata = NULL;
503 UINT rawsize = 0, i, j, row_size = 0;
505 TRACE("%s\n",debugstr_w(t->name));
507 row_size = msi_table_get_row_size( t->colinfo, t->col_count );
509 /* if we can't read the table, just assume that it's empty */
510 read_stream_data( stg, t->name, TRUE, &rawdata, &rawsize );
511 if( !rawdata )
512 return ERROR_SUCCESS;
514 TRACE("Read %d bytes\n", rawsize );
516 if( rawsize % row_size )
518 WARN("Table size is invalid %d/%d\n", rawsize, row_size );
519 goto err;
522 t->row_count = rawsize / row_size;
523 t->data = msi_alloc_zero( t->row_count * sizeof (USHORT*) );
524 if( !t->data )
525 goto err;
527 /* transpose all the data */
528 TRACE("Transposing data from %d rows\n", t->row_count );
529 for( i=0; i<t->row_count; i++ )
531 t->data[i] = msi_alloc( row_size );
532 if( !t->data[i] )
533 goto err;
535 for( j=0; j<t->col_count; j++ )
537 UINT ofs = t->colinfo[j].offset;
538 UINT n = bytes_per_column( &t->colinfo[j] );
539 UINT k;
541 if ( n != 2 && n != 3 && n != 4 )
543 ERR("oops - unknown column width %d\n", n);
544 goto err;
547 for ( k = 0; k < n; k++ )
548 t->data[i][ofs + k] = rawdata[ofs*t->row_count + i * n + k];
552 msi_free( rawdata );
553 return ERROR_SUCCESS;
554 err:
555 msi_free( rawdata );
556 return ERROR_FUNCTION_FAILED;
559 void free_cached_tables( MSIDATABASE *db )
561 while( !list_empty( &db->tables ) )
563 MSITABLE *t = LIST_ENTRY( list_head( &db->tables ), MSITABLE, entry );
565 list_remove( &t->entry );
566 free_table( t );
570 static MSITABLE *find_cached_table( MSIDATABASE *db, LPCWSTR name )
572 MSITABLE *t;
574 LIST_FOR_EACH_ENTRY( t, &db->tables, MSITABLE, entry )
575 if( !lstrcmpW( name, t->name ) )
576 return t;
578 return NULL;
581 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name, MSICOLUMNINFO **pcols, UINT *pcount )
583 UINT r, column_count = 0;
584 MSICOLUMNINFO *columns;
586 /* get the number of columns in this table */
587 column_count = 0;
588 r = get_tablecolumns( db, name, NULL, &column_count );
589 if( r != ERROR_SUCCESS )
590 return r;
592 /* if there's no columns, there's no table */
593 if( column_count == 0 )
594 return ERROR_INVALID_PARAMETER;
596 TRACE("Table %s found\n", debugstr_w(name) );
598 columns = msi_alloc( column_count*sizeof (MSICOLUMNINFO) );
599 if( !columns )
600 return ERROR_FUNCTION_FAILED;
602 r = get_tablecolumns( db, name, columns, &column_count );
603 if( r != ERROR_SUCCESS )
605 msi_free( columns );
606 return ERROR_FUNCTION_FAILED;
609 *pcols = columns;
610 *pcount = column_count;
612 return r;
615 UINT msi_create_table( MSIDATABASE *db, LPCWSTR name, column_info *col_info,
616 BOOL persistent, MSITABLE **table_ret)
618 UINT r, nField;
619 MSIVIEW *tv = NULL;
620 MSIRECORD *rec = NULL;
621 column_info *col;
622 MSITABLE *table;
623 UINT i;
625 /* only add tables that don't exist already */
626 if( TABLE_Exists(db, name ) )
628 WARN("table %s exists\n", debugstr_w(name));
629 return ERROR_BAD_QUERY_SYNTAX;
632 table = msi_alloc( sizeof (MSITABLE) + lstrlenW(name)*sizeof (WCHAR) );
633 if( !table )
634 return ERROR_FUNCTION_FAILED;
636 table->ref_count = 1;
637 table->row_count = 0;
638 table->data = NULL;
639 table->nonpersistent_row_count = 0;
640 table->nonpersistent_data = NULL;
641 table->colinfo = NULL;
642 table->col_count = 0;
643 table->persistent = persistent;
644 lstrcpyW( table->name, name );
646 for( col = col_info; col; col = col->next )
647 table->col_count++;
649 table->colinfo = msi_alloc( table->col_count * sizeof(MSICOLUMNINFO) );
650 if (!table->colinfo)
652 free_table( table );
653 return ERROR_FUNCTION_FAILED;
656 for( i = 0, col = col_info; col; i++, col = col->next )
658 table->colinfo[ i ].tablename = strdupW( col->table );
659 table->colinfo[ i ].number = i + 1;
660 table->colinfo[ i ].colname = strdupW( col->column );
661 table->colinfo[ i ].type = col->type;
662 table->colinfo[ i ].offset = 0;
663 table->colinfo[ i ].hash_table = NULL;
665 table_calc_column_offsets( 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, !persistent );
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 )
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, 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 = TRUE;
791 lstrcpyW( table->name, name );
793 /* these two tables are special - we know the column types already */
794 if( !lstrcmpW( name, szColumns ) )
796 table->colinfo = _Columns_cols;
797 table->col_count = sizeof(_Columns_cols)/sizeof(_Columns_cols[0]);
799 else if( !lstrcmpW( name, szTables ) )
801 table->colinfo = _Tables_cols;
802 table->col_count = sizeof(_Tables_cols)/sizeof(_Tables_cols[0]);
804 else
806 r = table_get_column_info( db, name, &table->colinfo, &table->col_count);
807 if (r != ERROR_SUCCESS)
809 free_table ( table );
810 return r;
814 r = read_table_from_storage( table, db->storage );
815 if( r != ERROR_SUCCESS )
817 free_table( table );
818 return r;
821 list_add_head( &db->tables, &table->entry );
822 *table_ret = table;
823 return ERROR_SUCCESS;
826 static UINT save_table( MSIDATABASE *db, const MSITABLE *t )
828 BYTE *rawdata = NULL, *p;
829 UINT rawsize, r, i, j, row_size;
831 /* Nothing to do for non-persistent tables */
832 if( !t->persistent )
833 return ERROR_SUCCESS;
835 TRACE("Saving %s\n", debugstr_w( t->name ) );
837 row_size = msi_table_get_row_size( t->colinfo, t->col_count );
839 rawsize = t->row_count * row_size;
840 rawdata = msi_alloc_zero( rawsize );
841 if( !rawdata )
843 r = ERROR_NOT_ENOUGH_MEMORY;
844 goto err;
847 p = rawdata;
848 for( i=0; i<t->col_count; i++ )
850 for( j=0; j<t->row_count; j++ )
852 UINT offset = t->colinfo[i].offset;
854 *p++ = t->data[j][offset];
855 *p++ = t->data[j][offset + 1];
856 if( 4 == bytes_per_column( &t->colinfo[i] ) )
858 *p++ = t->data[j][offset + 2];
859 *p++ = t->data[j][offset + 3];
864 TRACE("writing %d bytes\n", rawsize);
865 r = write_stream_data( db->storage, t->name, rawdata, rawsize, TRUE );
867 err:
868 msi_free( rawdata );
870 return r;
873 static void table_calc_column_offsets( MSICOLUMNINFO *colinfo, DWORD count )
875 DWORD i;
877 for( i=0; colinfo && (i<count); i++ )
879 assert( (i+1) == colinfo[ i ].number );
880 if (i)
881 colinfo[i].offset = colinfo[ i - 1 ].offset
882 + bytes_per_column( &colinfo[ i - 1 ] );
883 else
884 colinfo[i].offset = 0;
885 TRACE("column %d is [%s] with type %08x ofs %d\n",
886 colinfo[i].number, debugstr_w(colinfo[i].colname),
887 colinfo[i].type, colinfo[i].offset);
891 static UINT get_defaulttablecolumns( LPCWSTR name, MSICOLUMNINFO *colinfo, UINT *sz)
893 const MSICOLUMNINFO *p;
894 DWORD i, n;
896 TRACE("%s\n", debugstr_w(name));
898 if (!lstrcmpW( name, szTables ))
900 p = _Tables_cols;
901 n = 1;
903 else if (!lstrcmpW( name, szColumns ))
905 p = _Columns_cols;
906 n = 4;
908 else
909 return ERROR_FUNCTION_FAILED;
911 /* duplicate the string data so we can free it in msi_free_colinfo */
912 for (i=0; i<n; i++)
914 if (colinfo && (i < *sz) )
916 memcpy( &colinfo[i], &p[i], sizeof(MSICOLUMNINFO) );
917 colinfo[i].tablename = strdupW( p[i].tablename );
918 colinfo[i].colname = strdupW( p[i].colname );
920 if( colinfo && (i >= *sz) )
921 break;
923 table_calc_column_offsets( colinfo, n );
924 *sz = n;
925 return ERROR_SUCCESS;
928 static void msi_free_colinfo( MSICOLUMNINFO *colinfo, UINT count )
930 UINT i;
932 for( i=0; i<count; i++ )
934 msi_free( (LPWSTR) colinfo[i].tablename );
935 msi_free( (LPWSTR) colinfo[i].colname );
936 msi_free( colinfo[i].hash_table );
940 static LPWSTR msi_makestring( const MSIDATABASE *db, UINT stringid)
942 return strdupW(msi_string_lookup_id( db->strings, stringid ));
945 static UINT read_table_int(BYTE *const *data, UINT row, UINT col, UINT bytes)
947 UINT ret = 0, i;
949 for (i = 0; i < bytes; i++)
950 ret += (data[row][col + i] << i * 8);
952 return ret;
955 static UINT get_tablecolumns( MSIDATABASE *db,
956 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz)
958 UINT r, i, n=0, table_id, count, maxcount = *sz;
959 MSITABLE *table = NULL;
961 TRACE("%s\n", debugstr_w(szTableName));
963 /* first check if there is a default table with that name */
964 r = get_defaulttablecolumns( szTableName, colinfo, sz );
965 if( ( r == ERROR_SUCCESS ) && *sz )
966 return r;
968 r = get_table( db, szColumns, &table );
969 if( r != ERROR_SUCCESS )
971 ERR("couldn't load _Columns table\n");
972 return ERROR_FUNCTION_FAILED;
975 /* convert table and column names to IDs from the string table */
976 r = msi_string2idW( db->strings, szTableName, &table_id );
977 if( r != ERROR_SUCCESS )
979 WARN("Couldn't find id for %s\n", debugstr_w(szTableName));
980 return r;
983 TRACE("Table id is %d, row count is %d\n", table_id, table->row_count);
985 /* Note: _Columns table doesn't have non-persistent data */
987 /* if maxcount is non-zero, assume it's exactly right for this table */
988 memset( colinfo, 0, maxcount*sizeof(*colinfo) );
989 count = table->row_count;
990 for( i=0; i<count; i++ )
992 if( read_table_int(table->data, i, 0, db->bytes_per_strref) != table_id )
993 continue;
994 if( colinfo )
996 UINT id = read_table_int(table->data, i, _Columns_cols[2].offset, db->bytes_per_strref);
997 UINT col = read_table_int(table->data, i, _Columns_cols[1].offset, sizeof(USHORT)) - (1<<15);
999 /* check the column number is in range */
1000 if (col<1 || col>maxcount)
1002 ERR("column %d out of range\n", col);
1003 continue;
1006 /* check if this column was already set */
1007 if (colinfo[ col - 1 ].number)
1009 ERR("duplicate column %d\n", col);
1010 continue;
1013 colinfo[ col - 1 ].tablename = msi_makestring( db, table_id );
1014 colinfo[ col - 1 ].number = col;
1015 colinfo[ col - 1 ].colname = msi_makestring( db, id );
1016 colinfo[ col - 1 ].type = read_table_int(table->data, i, _Columns_cols[3].offset, sizeof(USHORT)) - (1<<15);
1017 colinfo[ col - 1 ].offset = 0;
1018 colinfo[ col - 1 ].hash_table = NULL;
1020 n++;
1023 TRACE("%s has %d columns\n", debugstr_w(szTableName), n);
1025 if (colinfo && n != maxcount)
1027 ERR("missing column in table %s\n", debugstr_w(szTableName));
1028 msi_free_colinfo(colinfo, maxcount );
1029 return ERROR_FUNCTION_FAILED;
1032 table_calc_column_offsets( colinfo, n );
1033 *sz = n;
1035 return ERROR_SUCCESS;
1038 static void msi_update_table_columns( MSIDATABASE *db, LPCWSTR name )
1040 MSITABLE *table;
1041 UINT size, offset;
1042 int n;
1044 table = find_cached_table( db, name );
1045 msi_free( table->colinfo );
1046 table_get_column_info( db, name, &table->colinfo, &table->col_count );
1048 size = msi_table_get_row_size( table->colinfo, table->col_count );
1049 offset = table->colinfo[table->col_count - 1].offset;
1051 for ( n = 0; n < table->row_count; n++ )
1053 table->data[n] = msi_realloc( table->data[n], size );
1054 table->data[n][offset] = (BYTE)MSI_NULL_INTEGER;
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 if( !lstrcmpW( name, szTables ) )
1065 return TRUE;
1066 if( !lstrcmpW( name, szColumns ) )
1067 return TRUE;
1069 r = msi_string2idW( db->strings, name, &table_id );
1070 if( r != ERROR_SUCCESS )
1072 TRACE("Couldn't find id for %s\n", debugstr_w(name));
1073 return FALSE;
1076 r = get_table( db, szTables, &table );
1077 if( r != ERROR_SUCCESS )
1079 ERR("table %s not available\n", debugstr_w(szTables));
1080 return FALSE;
1083 count = table->row_count;
1084 for( i=0; i<count; i++ )
1085 if( table->data[ i ][ 0 ] == table_id )
1086 break;
1088 if (i!=count)
1089 return TRUE;
1091 count = table->nonpersistent_row_count;
1092 for( i=0; i<count; i++ )
1093 if( table->nonpersistent_data[ i ][ 0 ] == table_id )
1094 break;
1096 if (i!=count)
1097 return TRUE;
1099 TRACE("Searched %d tables, but %d was not found\n", count, table_id );
1101 return FALSE;
1104 /* below is the query interface to a table */
1106 typedef struct tagMSITABLEVIEW
1108 MSIVIEW view;
1109 MSIDATABASE *db;
1110 MSITABLE *table;
1111 MSICOLUMNINFO *columns;
1112 UINT num_cols;
1113 UINT row_size;
1114 WCHAR name[1];
1115 } MSITABLEVIEW;
1117 static UINT TABLE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
1119 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1120 UINT offset, n;
1121 BYTE **data;
1123 if( !tv->table )
1124 return ERROR_INVALID_PARAMETER;
1126 if( (col==0) || (col>tv->num_cols) )
1127 return ERROR_INVALID_PARAMETER;
1129 /* how many rows are there ? */
1130 if( row >= tv->table->row_count + tv->table->nonpersistent_row_count )
1131 return ERROR_NO_MORE_ITEMS;
1133 if( tv->columns[col-1].offset >= tv->row_size )
1135 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1136 ERR("%p %p\n", tv, tv->columns );
1137 return ERROR_FUNCTION_FAILED;
1140 if (row >= tv->table->row_count)
1142 row -= tv->table->row_count;
1143 data = tv->table->nonpersistent_data;
1145 else
1146 data = tv->table->data;
1148 n = bytes_per_column( &tv->columns[col-1] );
1149 if (n != 2 && n != 3 && n != 4)
1151 ERR("oops! what is %d bytes per column?\n", n );
1152 return ERROR_FUNCTION_FAILED;
1155 offset = tv->columns[col-1].offset;
1156 *val = read_table_int(data, row, offset, n);
1158 /* TRACE("Data [%d][%d] = %d\n", row, col, *val ); */
1160 return ERROR_SUCCESS;
1164 * We need a special case for streams, as we need to reference column with
1165 * the name of the stream in the same table, and the table name
1166 * which may not be available at higher levels of the query
1168 static UINT TABLE_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm )
1170 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1171 UINT ival = 0, refcol = 0, r;
1172 LPCWSTR sval;
1173 LPWSTR full_name;
1174 DWORD len;
1175 static const WCHAR szDot[] = { '.', 0 };
1176 WCHAR number[0x20];
1178 if( !view->ops->fetch_int )
1179 return ERROR_INVALID_PARAMETER;
1182 * The column marked with the type stream data seems to have a single number
1183 * which references the column containing the name of the stream data
1185 * Fetch the column to reference first.
1187 r = view->ops->fetch_int( view, row, col, &ival );
1188 if( r != ERROR_SUCCESS )
1189 return r;
1191 /* check the column value is in range */
1192 if (ival < 0 || ival > tv->num_cols || ival == col)
1194 ERR("bad column ref (%u) for stream\n", ival);
1195 return ERROR_FUNCTION_FAILED;
1198 if ( tv->columns[ival - 1].type & MSITYPE_STRING )
1200 /* now get the column with the name of the stream */
1201 r = view->ops->fetch_int( view, row, ival, &refcol );
1202 if ( r != ERROR_SUCCESS )
1203 return r;
1205 /* lookup the string value from the string table */
1206 sval = msi_string_lookup_id( tv->db->strings, refcol );
1207 if ( !sval )
1208 return ERROR_INVALID_PARAMETER;
1210 else
1212 static const WCHAR fmt[] = { '%','d',0 };
1213 sprintfW( number, fmt, ival );
1214 sval = number;
1217 len = lstrlenW( tv->name ) + 2 + lstrlenW( sval );
1218 full_name = msi_alloc( len*sizeof(WCHAR) );
1219 lstrcpyW( full_name, tv->name );
1220 lstrcatW( full_name, szDot );
1221 lstrcatW( full_name, sval );
1223 r = db_get_raw_stream( tv->db, full_name, stm );
1224 if( r )
1225 ERR("fetching stream %s, error = %d\n",debugstr_w(full_name), r);
1226 msi_free( full_name );
1228 return r;
1231 static UINT TABLE_set_int( MSITABLEVIEW *tv, UINT row, UINT col, UINT val )
1233 UINT offset, n, i;
1234 BYTE **data;
1236 if( !tv->table )
1237 return ERROR_INVALID_PARAMETER;
1239 if( (col==0) || (col>tv->num_cols) )
1240 return ERROR_INVALID_PARAMETER;
1242 if( row >= tv->table->row_count + tv->table->nonpersistent_row_count )
1243 return ERROR_INVALID_PARAMETER;
1245 if( tv->columns[col-1].offset >= tv->row_size )
1247 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1248 ERR("%p %p\n", tv, tv->columns );
1249 return ERROR_FUNCTION_FAILED;
1252 msi_free( tv->columns[col-1].hash_table );
1253 tv->columns[col-1].hash_table = NULL;
1255 if (row >= tv->table->row_count)
1257 row -= tv->table->row_count;
1258 data = tv->table->nonpersistent_data;
1260 else
1261 data = tv->table->data;
1263 n = bytes_per_column( &tv->columns[col-1] );
1264 if ( n != 2 && n != 3 && n != 4 )
1266 ERR("oops! what is %d bytes per column?\n", n );
1267 return ERROR_FUNCTION_FAILED;
1270 offset = tv->columns[col-1].offset;
1271 for ( i = 0; i < n; i++ )
1272 data[row][offset + i] = (val >> i * 8) & 0xff;
1274 return ERROR_SUCCESS;
1277 static UINT TABLE_set_row( struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask )
1279 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1280 UINT i, val, r = ERROR_SUCCESS;
1282 if ( !tv->table )
1283 return ERROR_INVALID_PARAMETER;
1285 /* test if any of the mask bits are invalid */
1286 if ( mask >= (1<<tv->num_cols) )
1287 return ERROR_INVALID_PARAMETER;
1289 for ( i = 0; i < tv->num_cols; i++ )
1291 BOOL persistent;
1293 /* only update the fields specified in the mask */
1294 if ( !(mask&(1<<i)) )
1295 continue;
1297 /* if row >= tv->table->row_count then it is a non-persistent row */
1298 persistent = tv->table->persistent && (row < tv->table->row_count);
1299 /* FIXME: should we allow updating keys? */
1301 val = 0;
1302 if ( !MSI_RecordIsNull( rec, i + 1 ) )
1304 if ( MSITYPE_IS_BINARY(tv->columns[ i ].type) )
1306 val = 1; /* refers to the first key column */
1308 else if ( tv->columns[i].type & MSITYPE_STRING )
1310 LPCWSTR sval = MSI_RecordGetString( rec, i + 1 );
1311 val = msi_addstringW( tv->db->strings, 0, sval, -1, 1,
1312 persistent ? StringPersistent : StringNonPersistent );
1314 else if ( 2 == bytes_per_column( &tv->columns[ i ] ) )
1316 val = 0x8000 + MSI_RecordGetInteger( rec, i + 1 );
1317 if ( val & 0xffff0000 )
1319 ERR("field %u value %d out of range\n", i+1, val - 0x8000 );
1320 return ERROR_FUNCTION_FAILED;
1323 else
1325 INT ival = MSI_RecordGetInteger( rec, i + 1 );
1326 val = ival ^ 0x80000000;
1330 r = TABLE_set_int( tv, row, i+1, val );
1331 if ( r != ERROR_SUCCESS )
1332 break;
1334 return r;
1337 static UINT table_create_new_row( struct tagMSIVIEW *view, UINT *num, BOOL temporary )
1339 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1340 BYTE **p, *row;
1341 UINT sz;
1342 BYTE ***data_ptr;
1343 UINT *row_count;
1345 TRACE("%p %s\n", view, temporary ? "TRUE" : "FALSE");
1347 if( !tv->table )
1348 return ERROR_INVALID_PARAMETER;
1350 row = msi_alloc_zero( tv->row_size );
1351 if( !row )
1352 return ERROR_NOT_ENOUGH_MEMORY;
1354 if( temporary )
1356 row_count = &tv->table->nonpersistent_row_count;
1357 data_ptr = &tv->table->nonpersistent_data;
1358 *num = tv->table->row_count + tv->table->nonpersistent_row_count;
1360 else
1362 row_count = &tv->table->row_count;
1363 data_ptr = &tv->table->data;
1364 *num = tv->table->row_count;
1367 sz = (*row_count + 1) * sizeof (BYTE*);
1368 if( *data_ptr )
1369 p = msi_realloc( *data_ptr, sz );
1370 else
1371 p = msi_alloc( sz );
1372 if( !p )
1374 msi_free( row );
1375 return ERROR_NOT_ENOUGH_MEMORY;
1378 *data_ptr = p;
1379 (*data_ptr)[*row_count] = row;
1380 (*row_count)++;
1382 return ERROR_SUCCESS;
1385 static UINT TABLE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
1387 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1389 TRACE("%p %p\n", tv, record);
1391 TRACE("There are %d columns\n", tv->num_cols );
1393 return ERROR_SUCCESS;
1396 static UINT TABLE_close( struct tagMSIVIEW *view )
1398 TRACE("%p\n", view );
1400 return ERROR_SUCCESS;
1403 static UINT TABLE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols)
1405 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1407 TRACE("%p %p %p\n", view, rows, cols );
1409 if( cols )
1410 *cols = tv->num_cols;
1411 if( rows )
1413 if( !tv->table )
1414 return ERROR_INVALID_PARAMETER;
1415 *rows = tv->table->row_count + tv->table->nonpersistent_row_count;
1418 return ERROR_SUCCESS;
1421 static UINT TABLE_get_column_info( struct tagMSIVIEW *view,
1422 UINT n, LPWSTR *name, UINT *type )
1424 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1426 TRACE("%p %d %p %p\n", tv, n, name, type );
1428 if( ( n == 0 ) || ( n > tv->num_cols ) )
1429 return ERROR_INVALID_PARAMETER;
1431 if( name )
1433 *name = strdupW( tv->columns[n-1].colname );
1434 if( !*name )
1435 return ERROR_FUNCTION_FAILED;
1437 if( type )
1438 *type = tv->columns[n-1].type;
1440 return ERROR_SUCCESS;
1443 static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row );
1445 static UINT table_validate_new( MSITABLEVIEW *tv, MSIRECORD *rec )
1447 UINT r, row, i;
1449 /* check there's no null values where they're not allowed */
1450 for( i = 0; i < tv->num_cols; i++ )
1452 if ( tv->columns[i].type & MSITYPE_NULLABLE )
1453 continue;
1455 if ( MSITYPE_IS_BINARY(tv->columns[i].type) )
1456 TRACE("skipping binary column\n");
1457 else if ( tv->columns[i].type & MSITYPE_STRING )
1459 LPCWSTR str;
1461 str = MSI_RecordGetString( rec, i+1 );
1462 if (str == NULL || str[0] == 0)
1463 return ERROR_INVALID_DATA;
1465 else
1467 UINT n;
1469 n = MSI_RecordGetInteger( rec, i+1 );
1470 if (n == MSI_NULL_INTEGER)
1471 return ERROR_INVALID_DATA;
1475 /* check there's no duplicate keys */
1476 r = msi_table_find_row( tv, rec, &row );
1477 if (r == ERROR_SUCCESS)
1478 return ERROR_INVALID_DATA;
1480 return ERROR_SUCCESS;
1483 static UINT TABLE_insert_row( struct tagMSIVIEW *view, MSIRECORD *rec, BOOL temporary )
1485 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1486 UINT r, row = -1;
1488 TRACE("%p %p %s\n", tv, rec, temporary ? "TRUE" : "FALSE" );
1490 /* check that the key is unique - can we find a matching row? */
1491 r = table_validate_new( tv, rec );
1492 if( r != ERROR_SUCCESS )
1493 return ERROR_FUNCTION_FAILED;
1495 r = table_create_new_row( view, &row, temporary );
1496 TRACE("insert_row returned %08x\n", r);
1497 if( r != ERROR_SUCCESS )
1498 return r;
1500 return TABLE_set_row( view, row, rec, (1<<tv->num_cols) - 1 );
1503 static UINT TABLE_delete_row( struct tagMSIVIEW *view, UINT row )
1505 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1506 UINT r, num_rows, num_cols;
1507 BYTE *src, *dest;
1509 TRACE("%p %d\n", tv, row);
1511 if ( !tv->table )
1512 return ERROR_INVALID_PARAMETER;
1514 r = TABLE_get_dimensions( view, &num_rows, &num_cols );
1515 if ( r != ERROR_SUCCESS )
1516 return r;
1518 if ( row >= num_rows )
1519 return ERROR_FUNCTION_FAILED;
1521 tv->table->row_count--;
1523 if ( row == num_rows - 1 )
1524 return ERROR_SUCCESS;
1526 dest = tv->table->data[row];
1527 src = tv->table->data[row + 1];
1528 memmove(dest, src, (num_rows - row - 1) * tv->row_size);
1529 return ERROR_SUCCESS;
1532 static UINT TABLE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
1533 MSIRECORD *rec)
1535 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1536 UINT r;
1538 TRACE("%p %d %p\n", view, eModifyMode, rec );
1540 switch (eModifyMode)
1542 case MSIMODIFY_VALIDATE_NEW:
1543 r = table_validate_new( tv, rec );
1544 break;
1546 case MSIMODIFY_INSERT_TEMPORARY:
1547 r = table_validate_new( tv, rec );
1548 if (r != ERROR_SUCCESS)
1549 break;
1550 r = TABLE_insert_row( view, rec, TRUE );
1551 break;
1553 case MSIMODIFY_REFRESH:
1554 case MSIMODIFY_INSERT:
1555 case MSIMODIFY_UPDATE:
1556 case MSIMODIFY_ASSIGN:
1557 case MSIMODIFY_REPLACE:
1558 case MSIMODIFY_MERGE:
1559 case MSIMODIFY_DELETE:
1560 case MSIMODIFY_VALIDATE:
1561 case MSIMODIFY_VALIDATE_FIELD:
1562 case MSIMODIFY_VALIDATE_DELETE:
1563 FIXME("%p %d %p - mode not implemented\n", view, eModifyMode, rec );
1564 r = ERROR_CALL_NOT_IMPLEMENTED;
1565 break;
1567 default:
1568 r = ERROR_INVALID_DATA;
1571 return r;
1574 static UINT TABLE_delete( struct tagMSIVIEW *view )
1576 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1578 TRACE("%p\n", view );
1580 tv->table = NULL;
1582 tv->columns = NULL;
1584 msi_free( tv );
1586 return ERROR_SUCCESS;
1589 static UINT TABLE_find_matching_rows( struct tagMSIVIEW *view, UINT col,
1590 UINT val, UINT *row, MSIITERHANDLE *handle )
1592 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1593 const MSICOLUMNHASHENTRY *entry;
1595 TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
1597 if( !tv->table )
1598 return ERROR_INVALID_PARAMETER;
1600 if( (col==0) || (col > tv->num_cols) )
1601 return ERROR_INVALID_PARAMETER;
1603 if( !tv->columns[col-1].hash_table )
1605 UINT i;
1606 UINT num_rows = tv->table->row_count + tv->table->nonpersistent_row_count;
1607 MSICOLUMNHASHENTRY **hash_table;
1608 MSICOLUMNHASHENTRY *new_entry;
1610 if( tv->columns[col-1].offset >= tv->row_size )
1612 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1613 ERR("%p %p\n", tv, tv->columns );
1614 return ERROR_FUNCTION_FAILED;
1617 /* allocate contiguous memory for the table and its entries so we
1618 * don't have to do an expensive cleanup */
1619 hash_table = msi_alloc(MSITABLE_HASH_TABLE_SIZE * sizeof(MSICOLUMNHASHENTRY*) +
1620 num_rows * sizeof(MSICOLUMNHASHENTRY));
1621 if (!hash_table)
1622 return ERROR_OUTOFMEMORY;
1624 memset(hash_table, 0, MSITABLE_HASH_TABLE_SIZE * sizeof(MSICOLUMNHASHENTRY*));
1625 tv->columns[col-1].hash_table = hash_table;
1627 new_entry = (MSICOLUMNHASHENTRY *)(hash_table + MSITABLE_HASH_TABLE_SIZE);
1629 for (i = 0; i < num_rows; i++, new_entry++)
1631 UINT row_value;
1633 if (view->ops->fetch_int( view, i, col, &row_value ) != ERROR_SUCCESS)
1634 continue;
1636 new_entry->next = NULL;
1637 new_entry->value = row_value;
1638 new_entry->row = i;
1639 if (hash_table[row_value % MSITABLE_HASH_TABLE_SIZE])
1641 MSICOLUMNHASHENTRY *prev_entry = hash_table[row_value % MSITABLE_HASH_TABLE_SIZE];
1642 while (prev_entry->next)
1643 prev_entry = prev_entry->next;
1644 prev_entry->next = new_entry;
1646 else
1647 hash_table[row_value % MSITABLE_HASH_TABLE_SIZE] = new_entry;
1651 if( !*handle )
1652 entry = tv->columns[col-1].hash_table[val % MSITABLE_HASH_TABLE_SIZE];
1653 else
1654 entry = (*handle)->next;
1656 while (entry && entry->value != val)
1657 entry = entry->next;
1659 *handle = entry;
1660 if (!entry)
1661 return ERROR_NO_MORE_ITEMS;
1663 *row = entry->row;
1664 return ERROR_SUCCESS;
1667 static UINT TABLE_add_ref(struct tagMSIVIEW *view)
1669 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1671 TRACE("%p %d\n", view, tv->table->ref_count);
1673 return InterlockedIncrement(&tv->table->ref_count);
1676 static UINT TABLE_release(struct tagMSIVIEW *view)
1678 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1679 INT ref = tv->table->ref_count;
1681 TRACE("%p %d\n", view, ref);
1683 ref = InterlockedDecrement(&tv->table->ref_count);
1684 if (ref == 0)
1686 if (!tv->table->row_count)
1688 list_remove(&tv->table->entry);
1689 free_table(tv->table);
1690 TABLE_delete(view);
1694 return ref;
1697 static UINT TABLE_add_column(struct tagMSIVIEW *view, LPCWSTR table, UINT number, LPCWSTR column, UINT type)
1699 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1700 MSIRECORD *rec;
1701 UINT r;
1703 rec = MSI_CreateRecord(4);
1704 if (!rec)
1705 return ERROR_OUTOFMEMORY;
1707 MSI_RecordSetStringW(rec, 1, table);
1708 MSI_RecordSetInteger(rec, 2, number);
1709 MSI_RecordSetStringW(rec, 3, column);
1710 MSI_RecordSetInteger(rec, 4, type);
1712 r = TABLE_insert_row(&tv->view, rec, FALSE);
1713 if (r != ERROR_SUCCESS)
1714 goto done;
1716 msi_update_table_columns(tv->db, table);
1718 done:
1719 msiobj_release(&rec->hdr);
1720 return r;
1723 static const MSIVIEWOPS table_ops =
1725 TABLE_fetch_int,
1726 TABLE_fetch_stream,
1727 TABLE_set_row,
1728 TABLE_insert_row,
1729 TABLE_delete_row,
1730 TABLE_execute,
1731 TABLE_close,
1732 TABLE_get_dimensions,
1733 TABLE_get_column_info,
1734 TABLE_modify,
1735 TABLE_delete,
1736 TABLE_find_matching_rows,
1737 TABLE_add_ref,
1738 TABLE_release,
1739 TABLE_add_column,
1742 UINT TABLE_CreateView( MSIDATABASE *db, LPCWSTR name, MSIVIEW **view )
1744 MSITABLEVIEW *tv ;
1745 UINT r, sz;
1747 static const WCHAR Streams[] = {'_','S','t','r','e','a','m','s',0};
1749 TRACE("%p %s %p\n", db, debugstr_w(name), view );
1751 if ( !lstrcmpW( name, Streams ) )
1752 return STREAMS_CreateView( db, view );
1754 sz = sizeof *tv + lstrlenW(name)*sizeof name[0] ;
1755 tv = msi_alloc_zero( sz );
1756 if( !tv )
1757 return ERROR_FUNCTION_FAILED;
1759 r = get_table( db, name, &tv->table );
1760 if( r != ERROR_SUCCESS )
1762 msi_free( tv );
1763 WARN("table not found\n");
1764 return r;
1767 TRACE("table %p found with %d columns\n", tv->table, tv->table->col_count);
1769 /* fill the structure */
1770 tv->view.ops = &table_ops;
1771 tv->db = db;
1772 tv->columns = tv->table->colinfo;
1773 tv->num_cols = tv->table->col_count;
1774 tv->row_size = msi_table_get_row_size( tv->table->colinfo, tv->table->col_count );
1776 TRACE("%s one row is %d bytes\n", debugstr_w(name), tv->row_size );
1778 *view = (MSIVIEW*) tv;
1779 lstrcpyW( tv->name, name );
1781 return ERROR_SUCCESS;
1784 UINT MSI_CommitTables( MSIDATABASE *db )
1786 UINT r;
1787 MSITABLE *table = NULL;
1789 TRACE("%p\n",db);
1791 r = msi_save_string_table( db->strings, db->storage );
1792 if( r != ERROR_SUCCESS )
1794 WARN("failed to save string table r=%08x\n",r);
1795 return r;
1798 LIST_FOR_EACH_ENTRY( table, &db->tables, MSITABLE, entry )
1800 r = save_table( db, table );
1801 if( r != ERROR_SUCCESS )
1803 WARN("failed to save table %s (r=%08x)\n",
1804 debugstr_w(table->name), r);
1805 return r;
1809 /* force everything to reload next time */
1810 free_cached_tables( db );
1812 return ERROR_SUCCESS;
1815 MSICONDITION MSI_DatabaseIsTablePersistent( MSIDATABASE *db, LPCWSTR table )
1817 MSITABLE *t;
1818 UINT r;
1820 TRACE("%p %s\n", db, debugstr_w(table));
1822 if (!table)
1823 return MSICONDITION_ERROR;
1825 r = get_table( db, table, &t );
1826 if (r != ERROR_SUCCESS)
1827 return MSICONDITION_NONE;
1829 if (t->persistent)
1830 return MSICONDITION_TRUE;
1831 else
1832 return MSICONDITION_FALSE;
1835 static UINT read_raw_int(const BYTE *data, UINT col, UINT bytes)
1837 UINT ret = 0, i;
1839 for (i = 0; i < bytes; i++)
1840 ret += (data[col + i] << i * 8);
1842 return ret;
1845 static MSIRECORD *msi_get_transform_record( const MSITABLEVIEW *tv, const string_table *st,
1846 const BYTE *rawdata, UINT bytes_per_strref )
1848 UINT i, val, ofs = 0;
1849 USHORT mask;
1850 MSICOLUMNINFO *columns = tv->columns;
1851 MSIRECORD *rec;
1853 mask = rawdata[0] | (rawdata[1] << 8);
1854 rawdata += 2;
1856 rec = MSI_CreateRecord( tv->num_cols );
1857 if( !rec )
1858 return rec;
1860 TRACE("row ->\n");
1861 for( i=0; i<tv->num_cols; i++ )
1863 if ( (mask&1) && (i>=(mask>>8)) )
1864 break;
1865 /* all keys must be present */
1866 if ( (~mask&1) && (~columns[i].type & MSITYPE_KEY) && ((1<<i) & ~mask) )
1867 continue;
1869 if( (columns[i].type & MSITYPE_STRING) &&
1870 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
1872 LPCWSTR sval;
1874 val = read_raw_int(rawdata, ofs, bytes_per_strref);
1875 sval = msi_string_lookup_id( st, val );
1876 MSI_RecordSetStringW( rec, i+1, sval );
1877 TRACE(" field %d [%s]\n", i+1, debugstr_w(sval));
1878 ofs += bytes_per_strref;
1880 else
1882 UINT n = bytes_per_column( &columns[i] );
1883 switch( n )
1885 case 2:
1886 val = read_raw_int(rawdata, ofs, n);
1887 if (val)
1888 MSI_RecordSetInteger( rec, i+1, val^0x8000 );
1889 TRACE(" field %d [0x%04x]\n", i+1, val );
1890 break;
1891 case 4:
1892 val = read_raw_int(rawdata, ofs, n);
1893 if (val)
1894 MSI_RecordSetInteger( rec, i+1, val^0x80000000 );
1895 TRACE(" field %d [0x%08x]\n", i+1, val );
1896 break;
1897 default:
1898 ERR("oops - unknown column width %d\n", n);
1899 break;
1901 ofs += n;
1904 return rec;
1907 static void dump_record( MSIRECORD *rec )
1909 UINT i, n;
1911 n = MSI_RecordGetFieldCount( rec );
1912 for( i=1; i<=n; i++ )
1914 LPCWSTR sval = MSI_RecordGetString( rec, i );
1916 if( MSI_RecordIsNull( rec, i ) )
1917 TRACE("row -> []\n");
1918 else if( (sval = MSI_RecordGetString( rec, i )) )
1919 TRACE("row -> [%s]\n", debugstr_w(sval));
1920 else
1921 TRACE("row -> [0x%08x]\n", MSI_RecordGetInteger( rec, i ) );
1925 static void dump_table( const string_table *st, const USHORT *rawdata, UINT rawsize )
1927 LPCWSTR sval;
1928 UINT i;
1930 for( i=0; i<(rawsize/2); i++ )
1932 sval = msi_string_lookup_id( st, rawdata[i] );
1933 MESSAGE(" %04x %s\n", rawdata[i], debugstr_w(sval) );
1937 static UINT* msi_record_to_row( const MSITABLEVIEW *tv, MSIRECORD *rec )
1939 LPCWSTR str;
1940 UINT i, r, *data;
1942 data = msi_alloc( tv->num_cols *sizeof (UINT) );
1943 for( i=0; i<tv->num_cols; i++ )
1945 data[i] = 0;
1947 if ( ~tv->columns[i].type & MSITYPE_KEY )
1948 continue;
1950 /* turn the transform column value into a row value */
1951 if ( ( tv->columns[i].type & MSITYPE_STRING ) &&
1952 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
1954 str = MSI_RecordGetString( rec, i+1 );
1955 r = msi_string2idW( tv->db->strings, str, &data[i] );
1957 /* if there's no matching string in the string table,
1958 these keys can't match any record, so fail now. */
1959 if( ERROR_SUCCESS != r )
1961 msi_free( data );
1962 return NULL;
1965 else
1967 data[i] = MSI_RecordGetInteger( rec, i+1 );
1968 if ((tv->columns[i].type&0xff) == 2)
1969 data[i] += 0x8000;
1970 else
1971 data[i] += 0x80000000;
1974 return data;
1977 static UINT msi_row_matches( MSITABLEVIEW *tv, UINT row, const UINT *data )
1979 UINT i, r, x, ret = ERROR_FUNCTION_FAILED;
1981 for( i=0; i<tv->num_cols; i++ )
1983 if ( ~tv->columns[i].type & MSITYPE_KEY )
1984 continue;
1986 /* turn the transform column value into a row value */
1987 r = TABLE_fetch_int( &tv->view, row, i+1, &x );
1988 if ( r != ERROR_SUCCESS )
1990 ERR("TABLE_fetch_int shouldn't fail here\n");
1991 break;
1994 /* if this key matches, move to the next column */
1995 if ( x != data[i] )
1997 ret = ERROR_FUNCTION_FAILED;
1998 break;
2001 ret = ERROR_SUCCESS;
2004 return ret;
2007 static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row )
2009 UINT i, r = ERROR_FUNCTION_FAILED, *data;
2011 data = msi_record_to_row( tv, rec );
2012 if( !data )
2013 return r;
2014 for( i = 0; i < tv->table->row_count + tv->table->nonpersistent_row_count; i++ )
2016 r = msi_row_matches( tv, i, data );
2017 if( r == ERROR_SUCCESS )
2019 *row = i;
2020 break;
2023 msi_free( data );
2024 return r;
2027 typedef struct
2029 struct list entry;
2030 LPWSTR name;
2031 } TRANSFORMDATA;
2033 static UINT msi_table_load_transform( MSIDATABASE *db, IStorage *stg,
2034 string_table *st, TRANSFORMDATA *transform,
2035 UINT bytes_per_strref )
2037 UINT rawsize = 0;
2038 BYTE *rawdata = NULL;
2039 MSITABLEVIEW *tv = NULL;
2040 UINT r, n, sz, i, mask;
2041 MSIRECORD *rec = NULL;
2042 UINT colcol = 0;
2043 WCHAR coltable[32];
2044 LPWSTR name;
2046 if (!transform)
2047 return ERROR_SUCCESS;
2049 name = transform->name;
2051 coltable[0] = 0;
2052 TRACE("%p %p %p %s\n", db, stg, st, debugstr_w(name) );
2054 /* read the transform data */
2055 read_stream_data( stg, name, TRUE, &rawdata, &rawsize );
2056 if ( !rawdata )
2058 TRACE("table %s empty\n", debugstr_w(name) );
2059 return ERROR_INVALID_TABLE;
2062 /* create a table view */
2063 r = TABLE_CreateView( db, name, (MSIVIEW**) &tv );
2064 if( r != ERROR_SUCCESS )
2065 goto err;
2067 r = tv->view.ops->execute( &tv->view, NULL );
2068 if( r != ERROR_SUCCESS )
2069 goto err;
2071 TRACE("name = %s columns = %u row_size = %u raw size = %u\n",
2072 debugstr_w(name), tv->num_cols, tv->row_size, rawsize );
2074 /* interpret the data */
2075 r = ERROR_SUCCESS;
2076 for( n=0; n < rawsize; )
2078 mask = rawdata[n] | (rawdata[n+1] << 8);
2080 if (mask&1)
2083 * if the low bit is set, columns are continuous and
2084 * the number of columns is specified in the high byte
2086 sz = 2;
2087 for( i=0; i<tv->num_cols; i++ )
2089 if( (tv->columns[i].type & MSITYPE_STRING) &&
2090 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
2091 sz += bytes_per_strref;
2092 else
2093 sz += bytes_per_column( &tv->columns[i] );
2096 else
2099 * If the low bit is not set, mask is a bitmask.
2100 * Excepting for key fields, which are always present,
2101 * each bit indicates that a field is present in the transform record.
2103 * mask == 0 is a special case ... only the keys will be present
2104 * and it means that this row should be deleted.
2106 sz = 2;
2107 for( i=0; i<tv->num_cols; i++ )
2109 if( (tv->columns[i].type & MSITYPE_KEY) || ((1<<i)&mask))
2111 if( (tv->columns[i].type & MSITYPE_STRING) &&
2112 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
2113 sz += bytes_per_strref;
2114 else
2115 sz += bytes_per_column( &tv->columns[i] );
2120 /* check we didn't run of the end of the table */
2121 if ( (n+sz) > rawsize )
2123 ERR("borked.\n");
2124 dump_table( st, (USHORT *)rawdata, rawsize );
2125 break;
2128 rec = msi_get_transform_record( tv, st, &rawdata[n], bytes_per_strref );
2129 if (rec)
2131 if ( mask & 1 )
2133 WCHAR table[32];
2134 DWORD sz = 32;
2135 UINT number = MSI_NULL_INTEGER;
2137 TRACE("inserting record\n");
2139 if (!lstrcmpW(name, szColumns))
2141 MSI_RecordGetStringW( rec, 1, table, &sz );
2142 number = MSI_RecordGetInteger( rec, 2 );
2145 * Native msi seems writes nul into the Number (2nd) column of
2146 * the _Columns table, only when the columns are from a new table
2148 if ( number == MSI_NULL_INTEGER )
2150 /* reset the column number on a new table */
2151 if ( lstrcmpW(coltable, table) )
2153 colcol = 0;
2154 lstrcpyW( coltable, table );
2157 /* fix nul column numbers */
2158 MSI_RecordSetInteger( rec, 2, ++colcol );
2162 r = TABLE_insert_row( &tv->view, rec, FALSE );
2163 if (r != ERROR_SUCCESS)
2164 ERR("insert row failed\n");
2166 if ( number != MSI_NULL_INTEGER && !lstrcmpW(name, szColumns) )
2167 msi_update_table_columns( db, table );
2169 else
2171 UINT row = 0;
2173 r = msi_table_find_row( tv, rec, &row );
2174 if (r != ERROR_SUCCESS)
2175 ERR("no matching row to transform\n");
2176 else if ( mask )
2178 TRACE("modifying row [%d]:\n", row);
2179 TABLE_set_row( &tv->view, row, rec, mask );
2181 else
2183 TRACE("deleting row [%d]:\n", row);
2184 TABLE_delete_row( &tv->view, row );
2187 if( TRACE_ON(msidb) ) dump_record( rec );
2188 msiobj_release( &rec->hdr );
2191 n += sz;
2194 err:
2195 /* no need to free the table, it's associated with the database */
2196 msi_free( rawdata );
2197 if( tv )
2198 tv->view.ops->delete( &tv->view );
2200 return ERROR_SUCCESS;
2204 * msi_table_apply_transform
2206 * Enumerate the table transforms in a transform storage and apply each one.
2208 UINT msi_table_apply_transform( MSIDATABASE *db, IStorage *stg )
2210 struct list transforms;
2211 IEnumSTATSTG *stgenum = NULL;
2212 TRANSFORMDATA *transform;
2213 TRANSFORMDATA *tables = NULL, *columns = NULL;
2214 HRESULT r;
2215 STATSTG stat;
2216 string_table *strings;
2217 UINT ret = ERROR_FUNCTION_FAILED;
2218 UINT bytes_per_strref;
2220 TRACE("%p %p\n", db, stg );
2222 strings = msi_load_string_table( stg, &bytes_per_strref );
2223 if( !strings )
2224 goto end;
2226 r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
2227 if( FAILED( r ) )
2228 goto end;
2230 list_init(&transforms);
2232 while ( TRUE )
2234 MSITABLEVIEW *tv = NULL;
2235 WCHAR name[0x40];
2236 ULONG count = 0;
2238 r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
2239 if ( FAILED( r ) || !count )
2240 break;
2242 decode_streamname( stat.pwcsName, name );
2243 if ( name[0] != 0x4840 )
2244 continue;
2246 if ( !lstrcmpW( name+1, szStringPool ) ||
2247 !lstrcmpW( name+1, szStringData ) )
2248 continue;
2250 transform = msi_alloc_zero( sizeof(TRANSFORMDATA) );
2251 if ( !transform )
2252 break;
2254 list_add_tail( &transforms, &transform->entry );
2256 transform->name = strdupW( name + 1 );
2258 if ( !lstrcmpW( transform->name, szTables ) )
2259 tables = transform;
2260 else if (!lstrcmpW( transform->name, szColumns ) )
2261 columns = transform;
2263 TRACE("transform contains stream %s\n", debugstr_w(name));
2265 /* load the table */
2266 r = TABLE_CreateView( db, transform->name, (MSIVIEW**) &tv );
2267 if( r != ERROR_SUCCESS )
2268 continue;
2270 r = tv->view.ops->execute( &tv->view, NULL );
2271 if( r != ERROR_SUCCESS )
2273 tv->view.ops->delete( &tv->view );
2274 continue;
2277 tv->view.ops->delete( &tv->view );
2281 * Apply _Tables and _Columns transforms first so that
2282 * the table metadata is correct, and empty tables exist.
2284 ret = msi_table_load_transform( db, stg, strings, tables, bytes_per_strref );
2285 if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
2286 goto end;
2288 ret = msi_table_load_transform( db, stg, strings, columns, bytes_per_strref );
2289 if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
2290 goto end;
2292 ret = ERROR_SUCCESS;
2294 while ( !list_empty( &transforms ) )
2296 transform = LIST_ENTRY( list_head( &transforms ), TRANSFORMDATA, entry );
2298 if ( lstrcmpW( transform->name, szColumns ) &&
2299 lstrcmpW( transform->name, szTables ) &&
2300 ret == ERROR_SUCCESS )
2302 ret = msi_table_load_transform( db, stg, strings, transform, bytes_per_strref );
2305 list_remove( &transform->entry );
2306 msi_free( transform->name );
2307 msi_free( transform );
2310 if ( ret == ERROR_SUCCESS )
2311 append_storage_to_db( db, stg );
2313 end:
2314 if ( stgenum )
2315 IEnumSTATSTG_Release( stgenum );
2316 if ( strings )
2317 msi_destroy_stringtable( strings );
2319 return ret;
2322 void append_storage_to_db( MSIDATABASE *db, IStorage *stg )
2324 MSITRANSFORM *t;
2326 t = msi_alloc( sizeof *t );
2327 t->stg = stg;
2328 IStorage_AddRef( stg );
2329 list_add_tail( &db->transforms, &t->entry );
2332 void msi_free_transforms( MSIDATABASE *db )
2334 while( !list_empty( &db->transforms ) )
2336 MSITRANSFORM *t = LIST_ENTRY( list_head( &db->transforms ),
2337 MSITRANSFORM, entry );
2338 list_remove( &t->entry );
2339 IStorage_Release( t->stg );
2340 msi_free( t );