gdi32: PATH_ExtTextOut remove incorrect shift to DC origin.
[wine/hacks.git] / dlls / msi / table.c
blobab86bcd10149780a33b5de7254dfb388e33be85f
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2002-2005 Mike McCormack for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
22 #include <assert.h>
24 #define COBJMACROS
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winerror.h"
31 #include "msi.h"
32 #include "msiquery.h"
33 #include "objbase.h"
34 #include "objidl.h"
35 #include "winnls.h"
36 #include "msipriv.h"
37 #include "query.h"
39 #include "wine/debug.h"
40 #include "wine/unicode.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
44 #define MSITABLE_HASH_TABLE_SIZE 37
45 #define LONG_STR_BYTES 3
47 typedef struct tagMSICOLUMNHASHENTRY
49 struct tagMSICOLUMNHASHENTRY *next;
50 UINT value;
51 UINT row;
52 } MSICOLUMNHASHENTRY;
54 typedef struct tagMSICOLUMNINFO
56 LPWSTR tablename;
57 UINT number;
58 LPWSTR colname;
59 UINT type;
60 UINT offset;
61 INT ref_count;
62 BOOL temporary;
63 MSICOLUMNHASHENTRY **hash_table;
64 } MSICOLUMNINFO;
66 typedef struct tagMSIORDERINFO
68 UINT *reorder;
69 UINT num_cols;
70 UINT cols[1];
71 } MSIORDERINFO;
73 struct tagMSITABLE
75 BYTE **data;
76 UINT row_count;
77 BYTE **nonpersistent_data;
78 UINT nonpersistent_row_count;
79 struct list entry;
80 MSICOLUMNINFO *colinfo;
81 UINT col_count;
82 MSICONDITION persistent;
83 INT ref_count;
84 WCHAR name[1];
87 typedef struct tagMSITRANSFORM {
88 struct list entry;
89 IStorage *stg;
90 } MSITRANSFORM;
92 static const WCHAR szStringData[] = {
93 '_','S','t','r','i','n','g','D','a','t','a',0 };
94 static const WCHAR szStringPool[] = {
95 '_','S','t','r','i','n','g','P','o','o','l',0 };
97 /* information for default tables */
98 static WCHAR szTables[] = { '_','T','a','b','l','e','s',0 };
99 static WCHAR szTable[] = { 'T','a','b','l','e',0 };
100 static WCHAR szName[] = { 'N','a','m','e',0 };
101 static WCHAR szColumns[] = { '_','C','o','l','u','m','n','s',0 };
102 static WCHAR szNumber[] = { 'N','u','m','b','e','r',0 };
103 static WCHAR szType[] = { 'T','y','p','e',0 };
105 static const MSICOLUMNINFO _Columns_cols[4] = {
106 { szColumns, 1, szTable, MSITYPE_VALID | MSITYPE_STRING | MSITYPE_KEY | 64, 0, 0, 0, NULL },
107 { szColumns, 2, szNumber, MSITYPE_VALID | MSITYPE_KEY | 2, 2, 0, 0, NULL },
108 { szColumns, 3, szName, MSITYPE_VALID | MSITYPE_STRING | 64, 4, 0, 0, NULL },
109 { szColumns, 4, szType, MSITYPE_VALID | 2, 6, 0, 0, NULL },
112 static const MSICOLUMNINFO _Tables_cols[1] = {
113 { szTables, 1, szName, MSITYPE_VALID | MSITYPE_STRING | MSITYPE_KEY | 64, 0, 0, 0, NULL },
116 #define MAX_STREAM_NAME 0x1f
118 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name,
119 MSICOLUMNINFO **pcols, UINT *pcount );
120 static void table_calc_column_offsets( MSIDATABASE *db, MSICOLUMNINFO *colinfo,
121 DWORD count );
122 static UINT get_tablecolumns( MSIDATABASE *db,
123 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz);
124 static void msi_free_colinfo( MSICOLUMNINFO *colinfo, UINT count );
126 static inline UINT bytes_per_column( MSIDATABASE *db, const MSICOLUMNINFO *col )
128 if( MSITYPE_IS_BINARY(col->type) )
129 return 2;
130 if( col->type & MSITYPE_STRING )
131 return db->bytes_per_strref;
132 if( (col->type & 0xff) > 4 )
133 ERR("Invalid column size!\n");
134 return col->type & 0xff;
137 static int utf2mime(int x)
139 if( (x>='0') && (x<='9') )
140 return x-'0';
141 if( (x>='A') && (x<='Z') )
142 return x-'A'+10;
143 if( (x>='a') && (x<='z') )
144 return x-'a'+10+26;
145 if( x=='.' )
146 return 10+26+26;
147 if( x=='_' )
148 return 10+26+26+1;
149 return -1;
152 static LPWSTR encode_streamname(BOOL bTable, LPCWSTR in)
154 DWORD count = MAX_STREAM_NAME;
155 DWORD ch, next;
156 LPWSTR out, p;
158 if( !bTable )
159 count = lstrlenW( in )+2;
160 out = msi_alloc( count*sizeof(WCHAR) );
161 p = out;
163 if( bTable )
165 *p++ = 0x4840;
166 count --;
168 while( count -- )
170 ch = *in++;
171 if( !ch )
173 *p = ch;
174 return out;
176 if( ( ch < 0x80 ) && ( utf2mime(ch) >= 0 ) )
178 ch = utf2mime(ch) + 0x4800;
179 next = *in;
180 if( next && (next<0x80) )
182 next = utf2mime(next);
183 if( next != -1 )
185 next += 0x3ffffc0;
186 ch += (next<<6);
187 in++;
191 *p++ = ch;
193 ERR("Failed to encode stream name (%s)\n",debugstr_w(in));
194 msi_free( out );
195 return NULL;
198 static int mime2utf(int x)
200 if( x<10 )
201 return x + '0';
202 if( x<(10+26))
203 return x - 10 + 'A';
204 if( x<(10+26+26))
205 return x - 10 - 26 + 'a';
206 if( x == (10+26+26) )
207 return '.';
208 return '_';
211 BOOL decode_streamname(LPCWSTR in, LPWSTR out)
213 WCHAR ch;
214 DWORD count = 0;
216 while ( (ch = *in++) )
218 if( (ch >= 0x3800 ) && (ch < 0x4840 ) )
220 if( ch >= 0x4800 )
221 ch = mime2utf(ch-0x4800);
222 else
224 ch -= 0x3800;
225 *out++ = mime2utf(ch&0x3f);
226 count++;
227 ch = mime2utf((ch>>6)&0x3f);
230 *out++ = ch;
231 count++;
233 *out = 0;
234 return count;
237 void enum_stream_names( IStorage *stg )
239 IEnumSTATSTG *stgenum = NULL;
240 HRESULT r;
241 STATSTG stat;
242 ULONG n, count;
243 WCHAR name[0x40];
245 r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
246 if( FAILED( r ) )
247 return;
249 n = 0;
250 while( 1 )
252 count = 0;
253 r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
254 if( FAILED( r ) || !count )
255 break;
256 decode_streamname( stat.pwcsName, name );
257 TRACE("stream %2d -> %s %s\n", n,
258 debugstr_w(stat.pwcsName), debugstr_w(name) );
259 CoTaskMemFree( stat.pwcsName );
260 n++;
263 IEnumSTATSTG_Release( stgenum );
266 UINT read_stream_data( IStorage *stg, LPCWSTR stname, BOOL table,
267 BYTE **pdata, UINT *psz )
269 HRESULT r;
270 UINT ret = ERROR_FUNCTION_FAILED;
271 VOID *data;
272 ULONG sz, count;
273 IStream *stm = NULL;
274 STATSTG stat;
275 LPWSTR encname;
277 encname = encode_streamname(table, stname);
279 TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
281 r = IStorage_OpenStream(stg, encname, NULL,
282 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stm);
283 msi_free( encname );
284 if( FAILED( r ) )
286 WARN("open stream failed r = %08x - empty table?\n", r);
287 return ret;
290 r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
291 if( FAILED( r ) )
293 WARN("open stream failed r = %08x!\n", r);
294 goto end;
297 if( stat.cbSize.QuadPart >> 32 )
299 WARN("Too big!\n");
300 goto end;
303 sz = stat.cbSize.QuadPart;
304 data = msi_alloc( sz );
305 if( !data )
307 WARN("couldn't allocate memory r=%08x!\n", r);
308 ret = ERROR_NOT_ENOUGH_MEMORY;
309 goto end;
312 r = IStream_Read(stm, data, sz, &count );
313 if( FAILED( r ) || ( count != sz ) )
315 msi_free( data );
316 WARN("read stream failed r = %08x!\n", r);
317 goto end;
320 *pdata = data;
321 *psz = sz;
322 ret = ERROR_SUCCESS;
324 end:
325 IStream_Release( stm );
327 return ret;
330 static UINT db_get_raw_stream( MSIDATABASE *db, LPCWSTR stname, IStream **stm )
332 LPWSTR encname;
333 HRESULT r;
335 encname = encode_streamname(FALSE, stname);
337 TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
339 r = IStorage_OpenStream(db->storage, encname, NULL,
340 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, stm);
341 if( FAILED( r ) )
343 MSITRANSFORM *transform;
345 LIST_FOR_EACH_ENTRY( transform, &db->transforms, MSITRANSFORM, entry )
347 TRACE("looking for %s in transform storage\n", debugstr_w(stname) );
348 r = IStorage_OpenStream( transform->stg, encname, NULL,
349 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, stm );
350 if (SUCCEEDED(r))
351 break;
355 msi_free( encname );
357 return SUCCEEDED(r) ? ERROR_SUCCESS : ERROR_FUNCTION_FAILED;
360 UINT read_raw_stream_data( MSIDATABASE *db, LPCWSTR stname,
361 USHORT **pdata, UINT *psz )
363 HRESULT r;
364 UINT ret = ERROR_FUNCTION_FAILED;
365 VOID *data;
366 ULONG sz, count;
367 IStream *stm = NULL;
368 STATSTG stat;
370 r = db_get_raw_stream( db, stname, &stm );
371 if( r != ERROR_SUCCESS)
372 return ret;
373 r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
374 if( FAILED( r ) )
376 WARN("open stream failed r = %08x!\n", r);
377 goto end;
380 if( stat.cbSize.QuadPart >> 32 )
382 WARN("Too big!\n");
383 goto end;
386 sz = stat.cbSize.QuadPart;
387 data = msi_alloc( sz );
388 if( !data )
390 WARN("couldn't allocate memory r=%08x!\n", r);
391 ret = ERROR_NOT_ENOUGH_MEMORY;
392 goto end;
395 r = IStream_Read(stm, data, sz, &count );
396 if( FAILED( r ) || ( count != sz ) )
398 msi_free( data );
399 WARN("read stream failed r = %08x!\n", r);
400 goto end;
403 *pdata = data;
404 *psz = sz;
405 ret = ERROR_SUCCESS;
407 end:
408 IStream_Release( stm );
410 return ret;
413 UINT write_stream_data( IStorage *stg, LPCWSTR stname,
414 LPCVOID data, UINT sz, BOOL bTable )
416 HRESULT r;
417 UINT ret = ERROR_FUNCTION_FAILED;
418 ULONG count;
419 IStream *stm = NULL;
420 ULARGE_INTEGER size;
421 LARGE_INTEGER pos;
422 LPWSTR encname;
424 encname = encode_streamname(bTable, stname );
425 r = IStorage_OpenStream( stg, encname, NULL,
426 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, &stm);
427 if( FAILED(r) )
429 r = IStorage_CreateStream( stg, encname,
430 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
432 msi_free( encname );
433 if( FAILED( r ) )
435 WARN("open stream failed r = %08x\n", r);
436 return ret;
439 size.QuadPart = sz;
440 r = IStream_SetSize( stm, size );
441 if( FAILED( r ) )
443 WARN("Failed to SetSize\n");
444 goto end;
447 pos.QuadPart = 0;
448 r = IStream_Seek( stm, pos, STREAM_SEEK_SET, NULL );
449 if( FAILED( r ) )
451 WARN("Failed to Seek\n");
452 goto end;
455 if (sz)
457 r = IStream_Write(stm, data, sz, &count );
458 if( FAILED( r ) || ( count != sz ) )
460 WARN("Failed to Write\n");
461 goto end;
465 ret = ERROR_SUCCESS;
467 end:
468 IStream_Release( stm );
470 return ret;
473 static void free_table( MSITABLE *table )
475 UINT i;
476 for( i=0; i<table->row_count; i++ )
477 msi_free( table->data[i] );
478 msi_free( table->data );
479 for( i=0; i<table->nonpersistent_row_count; i++ )
480 msi_free( table->nonpersistent_data[i] );
481 msi_free( table->nonpersistent_data );
482 msi_free_colinfo( table->colinfo, table->col_count );
483 msi_free( table->colinfo );
484 msi_free( table );
487 static UINT msi_table_get_row_size( MSIDATABASE *db,const MSICOLUMNINFO *cols,
488 UINT count )
490 const MSICOLUMNINFO *last_col = &cols[count-1];
491 if (!count)
492 return 0;
493 return last_col->offset + bytes_per_column( db, last_col );
496 /* add this table to the list of cached tables in the database */
497 static UINT read_table_from_storage( MSIDATABASE *db, MSITABLE *t, IStorage *stg )
499 BYTE *rawdata = NULL;
500 UINT rawsize = 0, i, j, row_size = 0;
502 TRACE("%s\n",debugstr_w(t->name));
504 row_size = msi_table_get_row_size( db, t->colinfo, t->col_count );
506 /* if we can't read the table, just assume that it's empty */
507 read_stream_data( stg, t->name, TRUE, &rawdata, &rawsize );
508 if( !rawdata )
509 return ERROR_SUCCESS;
511 TRACE("Read %d bytes\n", rawsize );
513 if( rawsize % row_size )
515 WARN("Table size is invalid %d/%d\n", rawsize, row_size );
516 goto err;
519 t->row_count = rawsize / row_size;
520 t->data = msi_alloc_zero( t->row_count * sizeof (USHORT*) );
521 if( !t->data )
522 goto err;
524 /* transpose all the data */
525 TRACE("Transposing data from %d rows\n", t->row_count );
526 for( i=0; i<t->row_count; i++ )
528 t->data[i] = msi_alloc( row_size );
529 if( !t->data[i] )
530 goto err;
532 for( j=0; j<t->col_count; j++ )
534 UINT ofs = t->colinfo[j].offset;
535 UINT n = bytes_per_column( db, &t->colinfo[j] );
536 UINT k;
538 if ( n != 2 && n != 3 && n != 4 )
540 ERR("oops - unknown column width %d\n", n);
541 goto err;
544 for ( k = 0; k < n; k++ )
545 t->data[i][ofs + k] = rawdata[ofs*t->row_count + i * n + k];
549 msi_free( rawdata );
550 return ERROR_SUCCESS;
551 err:
552 msi_free( rawdata );
553 return ERROR_FUNCTION_FAILED;
556 void free_cached_tables( MSIDATABASE *db )
558 while( !list_empty( &db->tables ) )
560 MSITABLE *t = LIST_ENTRY( list_head( &db->tables ), MSITABLE, entry );
562 list_remove( &t->entry );
563 free_table( t );
567 static MSITABLE *find_cached_table( MSIDATABASE *db, LPCWSTR name )
569 MSITABLE *t;
571 LIST_FOR_EACH_ENTRY( t, &db->tables, MSITABLE, entry )
572 if( !lstrcmpW( name, t->name ) )
573 return t;
575 return NULL;
578 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name, MSICOLUMNINFO **pcols, UINT *pcount )
580 UINT r, column_count = 0;
581 MSICOLUMNINFO *columns;
583 /* get the number of columns in this table */
584 column_count = 0;
585 r = get_tablecolumns( db, name, NULL, &column_count );
586 if( r != ERROR_SUCCESS )
587 return r;
589 *pcount = column_count;
591 /* if there's no columns, there's no table */
592 if( column_count == 0 )
593 return ERROR_INVALID_PARAMETER;
595 TRACE("Table %s found\n", debugstr_w(name) );
597 columns = msi_alloc( column_count*sizeof (MSICOLUMNINFO) );
598 if( !columns )
599 return ERROR_FUNCTION_FAILED;
601 r = get_tablecolumns( db, name, columns, &column_count );
602 if( r != ERROR_SUCCESS )
604 msi_free( columns );
605 return ERROR_FUNCTION_FAILED;
608 *pcols = columns;
610 return r;
613 UINT msi_create_table( MSIDATABASE *db, LPCWSTR name, column_info *col_info,
614 MSICONDITION persistent, MSITABLE **table_ret)
616 UINT r, nField;
617 MSIVIEW *tv = NULL;
618 MSIRECORD *rec = NULL;
619 column_info *col;
620 MSITABLE *table;
621 UINT i;
623 /* only add tables that don't exist already */
624 if( TABLE_Exists(db, name ) )
626 WARN("table %s exists\n", debugstr_w(name));
627 return ERROR_BAD_QUERY_SYNTAX;
630 table = msi_alloc( sizeof (MSITABLE) + lstrlenW(name)*sizeof (WCHAR) );
631 if( !table )
632 return ERROR_FUNCTION_FAILED;
634 table->ref_count = 1;
635 table->row_count = 0;
636 table->data = NULL;
637 table->nonpersistent_row_count = 0;
638 table->nonpersistent_data = NULL;
639 table->colinfo = NULL;
640 table->col_count = 0;
641 table->persistent = persistent;
642 lstrcpyW( table->name, name );
644 for( col = col_info; col; col = col->next )
645 table->col_count++;
647 table->colinfo = msi_alloc( table->col_count * sizeof(MSICOLUMNINFO) );
648 if (!table->colinfo)
650 free_table( table );
651 return ERROR_FUNCTION_FAILED;
654 for( i = 0, col = col_info; col; i++, col = col->next )
656 table->colinfo[ i ].tablename = strdupW( col->table );
657 table->colinfo[ i ].number = i + 1;
658 table->colinfo[ i ].colname = strdupW( col->column );
659 table->colinfo[ i ].type = col->type;
660 table->colinfo[ i ].offset = 0;
661 table->colinfo[ i ].ref_count = 0;
662 table->colinfo[ i ].hash_table = NULL;
663 table->colinfo[ i ].temporary = col->temporary;
665 table_calc_column_offsets( db, table->colinfo, table->col_count);
667 r = TABLE_CreateView( db, szTables, &tv );
668 TRACE("CreateView returned %x\n", r);
669 if( r )
671 free_table( table );
672 return r;
675 r = tv->ops->execute( tv, 0 );
676 TRACE("tv execute returned %x\n", r);
677 if( r )
678 goto err;
680 rec = MSI_CreateRecord( 1 );
681 if( !rec )
682 goto err;
684 r = MSI_RecordSetStringW( rec, 1, name );
685 if( r )
686 goto err;
688 r = tv->ops->insert_row( tv, rec, -1, persistent == MSICONDITION_FALSE );
689 TRACE("insert_row returned %x\n", r);
690 if( r )
691 goto err;
693 tv->ops->delete( tv );
694 tv = NULL;
696 msiobj_release( &rec->hdr );
697 rec = NULL;
699 if( persistent != MSICONDITION_FALSE )
701 /* add each column to the _Columns table */
702 r = TABLE_CreateView( db, szColumns, &tv );
703 if( r )
704 return r;
706 r = tv->ops->execute( tv, 0 );
707 TRACE("tv execute returned %x\n", r);
708 if( r )
709 goto err;
711 rec = MSI_CreateRecord( 4 );
712 if( !rec )
713 goto err;
715 r = MSI_RecordSetStringW( rec, 1, name );
716 if( r )
717 goto err;
720 * need to set the table, column number, col name and type
721 * for each column we enter in the table
723 nField = 1;
724 for( col = col_info; col; col = col->next )
726 r = MSI_RecordSetInteger( rec, 2, nField );
727 if( r )
728 goto err;
730 r = MSI_RecordSetStringW( rec, 3, col->column );
731 if( r )
732 goto err;
734 r = MSI_RecordSetInteger( rec, 4, col->type );
735 if( r )
736 goto err;
738 r = tv->ops->insert_row( tv, rec, -1, FALSE );
739 if( r )
740 goto err;
742 nField++;
744 if( !col )
745 r = ERROR_SUCCESS;
748 err:
749 if (rec)
750 msiobj_release( &rec->hdr );
751 /* FIXME: remove values from the string table on error */
752 if( tv )
753 tv->ops->delete( tv );
755 if (r == ERROR_SUCCESS)
757 list_add_head( &db->tables, &table->entry );
758 *table_ret = table;
760 else
761 free_table( table );
763 return r;
766 static UINT get_table( MSIDATABASE *db, LPCWSTR name, MSITABLE **table_ret )
768 MSITABLE *table;
769 UINT r;
771 /* first, see if the table is cached */
772 table = find_cached_table( db, name );
773 if( table )
775 *table_ret = table;
776 return ERROR_SUCCESS;
779 /* nonexistent tables should be interpreted as empty tables */
780 table = msi_alloc( sizeof (MSITABLE) + lstrlenW(name)*sizeof (WCHAR) );
781 if( !table )
782 return ERROR_FUNCTION_FAILED;
784 table->row_count = 0;
785 table->data = NULL;
786 table->nonpersistent_row_count = 0;
787 table->nonpersistent_data = NULL;
788 table->colinfo = NULL;
789 table->col_count = 0;
790 table->persistent = MSICONDITION_TRUE;
791 lstrcpyW( table->name, name );
793 if ( !lstrcmpW(name, szTables) || !lstrcmpW(name, szColumns) )
794 table->persistent = MSICONDITION_NONE;
796 r = table_get_column_info( db, name, &table->colinfo, &table->col_count);
797 if (r != ERROR_SUCCESS)
799 free_table ( table );
800 return r;
803 r = read_table_from_storage( db, table, db->storage );
804 if( r != ERROR_SUCCESS )
806 free_table( table );
807 return r;
810 list_add_head( &db->tables, &table->entry );
811 *table_ret = table;
812 return ERROR_SUCCESS;
815 static UINT save_table( MSIDATABASE *db, const MSITABLE *t )
817 BYTE *rawdata = NULL, *p;
818 UINT rawsize, r, i, j, row_size;
820 /* Nothing to do for non-persistent tables */
821 if( t->persistent == MSICONDITION_FALSE )
822 return ERROR_SUCCESS;
824 TRACE("Saving %s\n", debugstr_w( t->name ) );
826 row_size = msi_table_get_row_size( db, t->colinfo, t->col_count );
828 rawsize = t->row_count * row_size;
829 rawdata = msi_alloc_zero( rawsize );
830 if( !rawdata )
832 r = ERROR_NOT_ENOUGH_MEMORY;
833 goto err;
836 p = rawdata;
837 for( i=0; i<t->col_count; i++ )
839 for( j=0; j<t->row_count; j++ )
841 UINT offset = t->colinfo[i].offset;
843 *p++ = t->data[j][offset];
844 *p++ = t->data[j][offset + 1];
845 if( 4 == bytes_per_column( db, &t->colinfo[i] ) )
847 *p++ = t->data[j][offset + 2];
848 *p++ = t->data[j][offset + 3];
853 TRACE("writing %d bytes\n", rawsize);
854 r = write_stream_data( db->storage, t->name, rawdata, rawsize, TRUE );
856 err:
857 msi_free( rawdata );
859 return r;
862 static void table_calc_column_offsets( MSIDATABASE *db, MSICOLUMNINFO *colinfo,
863 DWORD count )
865 DWORD i;
867 for( i=0; colinfo && (i<count); i++ )
869 assert( (i+1) == colinfo[ i ].number );
870 if (i)
871 colinfo[i].offset = colinfo[ i - 1 ].offset
872 + bytes_per_column( db, &colinfo[ i - 1 ] );
873 else
874 colinfo[i].offset = 0;
875 TRACE("column %d is [%s] with type %08x ofs %d\n",
876 colinfo[i].number, debugstr_w(colinfo[i].colname),
877 colinfo[i].type, colinfo[i].offset);
881 static UINT get_defaulttablecolumns( MSIDATABASE *db, LPCWSTR name,
882 MSICOLUMNINFO *colinfo, UINT *sz)
884 const MSICOLUMNINFO *p;
885 DWORD i, n;
887 TRACE("%s\n", debugstr_w(name));
889 if (!lstrcmpW( name, szTables ))
891 p = _Tables_cols;
892 n = 1;
894 else if (!lstrcmpW( name, szColumns ))
896 p = _Columns_cols;
897 n = 4;
899 else
900 return ERROR_FUNCTION_FAILED;
902 /* duplicate the string data so we can free it in msi_free_colinfo */
903 for (i=0; i<n; i++)
905 if (colinfo && (i < *sz) )
907 colinfo[i] = p[i];
908 colinfo[i].tablename = strdupW( p[i].tablename );
909 colinfo[i].colname = strdupW( p[i].colname );
911 if( colinfo && (i >= *sz) )
912 break;
914 table_calc_column_offsets( db, colinfo, n );
915 *sz = n;
916 return ERROR_SUCCESS;
919 static void msi_free_colinfo( MSICOLUMNINFO *colinfo, UINT count )
921 UINT i;
923 for( i=0; i<count; i++ )
925 msi_free( colinfo[i].tablename );
926 msi_free( colinfo[i].colname );
927 msi_free( colinfo[i].hash_table );
931 static LPWSTR msi_makestring( const MSIDATABASE *db, UINT stringid)
933 return strdupW(msi_string_lookup_id( db->strings, stringid ));
936 static UINT read_table_int(BYTE *const *data, UINT row, UINT col, UINT bytes)
938 UINT ret = 0, i;
940 for (i = 0; i < bytes; i++)
941 ret += (data[row][col + i] << i * 8);
943 return ret;
946 static UINT get_tablecolumns( MSIDATABASE *db,
947 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz)
949 UINT r, i, n=0, table_id, count, maxcount = *sz;
950 MSITABLE *table = NULL;
952 TRACE("%s\n", debugstr_w(szTableName));
954 /* first check if there is a default table with that name */
955 r = get_defaulttablecolumns( db, szTableName, colinfo, sz );
956 if( ( r == ERROR_SUCCESS ) && *sz )
957 return r;
959 r = get_table( db, szColumns, &table );
960 if( r != ERROR_SUCCESS )
962 ERR("couldn't load _Columns table\n");
963 return ERROR_FUNCTION_FAILED;
966 /* convert table and column names to IDs from the string table */
967 r = msi_string2idW( db->strings, szTableName, &table_id );
968 if( r != ERROR_SUCCESS )
970 WARN("Couldn't find id for %s\n", debugstr_w(szTableName));
971 return r;
974 TRACE("Table id is %d, row count is %d\n", table_id, table->row_count);
976 /* Note: _Columns table doesn't have non-persistent data */
978 /* if maxcount is non-zero, assume it's exactly right for this table */
979 memset( colinfo, 0, maxcount*sizeof(*colinfo) );
980 count = table->row_count;
981 for( i=0; i<count; i++ )
983 if( read_table_int(table->data, i, 0, db->bytes_per_strref) != table_id )
984 continue;
985 if( colinfo )
987 UINT id = read_table_int(table->data, i, table->colinfo[2].offset, db->bytes_per_strref);
988 UINT col = read_table_int(table->data, i, table->colinfo[1].offset, sizeof(USHORT)) - (1<<15);
990 /* check the column number is in range */
991 if (col<1 || col>maxcount)
993 ERR("column %d out of range\n", col);
994 continue;
997 /* check if this column was already set */
998 if (colinfo[ col - 1 ].number)
1000 ERR("duplicate column %d\n", col);
1001 continue;
1004 colinfo[ col - 1 ].tablename = msi_makestring( db, table_id );
1005 colinfo[ col - 1 ].number = col;
1006 colinfo[ col - 1 ].colname = msi_makestring( db, id );
1007 colinfo[ col - 1 ].type = read_table_int(table->data, i,
1008 table->colinfo[3].offset,
1009 sizeof(USHORT)) - (1<<15);
1010 colinfo[ col - 1 ].offset = 0;
1011 colinfo[ col - 1 ].ref_count = 0;
1012 colinfo[ col - 1 ].hash_table = NULL;
1014 n++;
1017 TRACE("%s has %d columns\n", debugstr_w(szTableName), n);
1019 if (colinfo && n != maxcount)
1021 ERR("missing column in table %s\n", debugstr_w(szTableName));
1022 msi_free_colinfo(colinfo, maxcount );
1023 return ERROR_FUNCTION_FAILED;
1026 table_calc_column_offsets( db, colinfo, n );
1027 *sz = n;
1029 return ERROR_SUCCESS;
1032 static void msi_update_table_columns( MSIDATABASE *db, LPCWSTR name )
1034 MSITABLE *table;
1035 UINT size, offset, old_count;
1036 UINT n;
1038 table = find_cached_table( db, name );
1039 old_count = table->col_count;
1040 msi_free( table->colinfo );
1041 table_get_column_info( db, name, &table->colinfo, &table->col_count );
1043 if (!table->col_count)
1044 return;
1046 size = msi_table_get_row_size( db, table->colinfo, table->col_count );
1047 offset = table->colinfo[table->col_count - 1].offset;
1049 for ( n = 0; n < table->row_count; n++ )
1051 table->data[n] = msi_realloc( table->data[n], size );
1052 if (old_count < table->col_count)
1053 memset( &table->data[n][offset], 0, size - offset );
1057 /* try to find the table name in the _Tables table */
1058 BOOL TABLE_Exists( MSIDATABASE *db, LPCWSTR name )
1060 UINT r, table_id = 0, i, count;
1061 MSITABLE *table = NULL;
1063 static const WCHAR szStreams[] = {'_','S','t','r','e','a','m','s',0};
1064 static const WCHAR szStorages[] = {'_','S','t','o','r','a','g','e','s',0};
1066 if( !lstrcmpW( name, szTables ) || !lstrcmpW( name, szColumns ) ||
1067 !lstrcmpW( name, szStreams ) || !lstrcmpW( name, szStorages ) )
1068 return TRUE;
1070 r = msi_string2idW( db->strings, name, &table_id );
1071 if( r != ERROR_SUCCESS )
1073 TRACE("Couldn't find id for %s\n", debugstr_w(name));
1074 return FALSE;
1077 r = get_table( db, szTables, &table );
1078 if( r != ERROR_SUCCESS )
1080 ERR("table %s not available\n", debugstr_w(szTables));
1081 return FALSE;
1084 count = table->row_count;
1085 for( i=0; i<count; i++ )
1086 if( table->data[ i ][ 0 ] == table_id )
1087 return TRUE;
1089 count = table->nonpersistent_row_count;
1090 for( i=0; i<count; i++ )
1091 if( table->nonpersistent_data[ i ][ 0 ] == table_id )
1092 return TRUE;
1094 return FALSE;
1097 /* below is the query interface to a table */
1099 typedef struct tagMSITABLEVIEW
1101 MSIVIEW view;
1102 MSIDATABASE *db;
1103 MSITABLE *table;
1104 MSICOLUMNINFO *columns;
1105 MSIORDERINFO *order;
1106 UINT num_cols;
1107 UINT row_size;
1108 WCHAR name[1];
1109 } MSITABLEVIEW;
1111 static UINT TABLE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
1113 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1114 UINT offset, n;
1115 BYTE **data;
1117 if( !tv->table )
1118 return ERROR_INVALID_PARAMETER;
1120 if( (col==0) || (col>tv->num_cols) )
1121 return ERROR_INVALID_PARAMETER;
1123 /* how many rows are there ? */
1124 if( row >= tv->table->row_count + tv->table->nonpersistent_row_count )
1125 return ERROR_NO_MORE_ITEMS;
1127 if( tv->columns[col-1].offset >= tv->row_size )
1129 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1130 ERR("%p %p\n", tv, tv->columns );
1131 return ERROR_FUNCTION_FAILED;
1134 if (tv->order)
1135 row = tv->order->reorder[row];
1137 if (row >= tv->table->row_count)
1139 row -= tv->table->row_count;
1140 data = tv->table->nonpersistent_data;
1142 else
1143 data = tv->table->data;
1145 n = bytes_per_column( tv->db, &tv->columns[col-1] );
1146 if (n != 2 && n != 3 && n != 4)
1148 ERR("oops! what is %d bytes per column?\n", n );
1149 return ERROR_FUNCTION_FAILED;
1152 offset = tv->columns[col-1].offset;
1153 *val = read_table_int(data, row, offset, n);
1155 /* TRACE("Data [%d][%d] = %d\n", row, col, *val ); */
1157 return ERROR_SUCCESS;
1161 * We need a special case for streams, as we need to reference column with
1162 * the name of the stream in the same table, and the table name
1163 * which may not be available at higher levels of the query
1165 static UINT TABLE_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm )
1167 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1168 UINT ival = 0, refcol = 0, r;
1169 LPCWSTR sval;
1170 LPWSTR full_name;
1171 DWORD len;
1172 static const WCHAR szDot[] = { '.', 0 };
1173 WCHAR number[0x20];
1175 if( !view->ops->fetch_int )
1176 return ERROR_INVALID_PARAMETER;
1179 * The column marked with the type stream data seems to have a single number
1180 * which references the column containing the name of the stream data
1182 * Fetch the column to reference first.
1184 r = view->ops->fetch_int( view, row, col, &ival );
1185 if( r != ERROR_SUCCESS )
1186 return r;
1188 /* check the column value is in range */
1189 if (ival > tv->num_cols || ival == col)
1191 ERR("bad column ref (%u) for stream\n", ival);
1192 return ERROR_FUNCTION_FAILED;
1195 if ( tv->columns[ival - 1].type & MSITYPE_STRING )
1197 /* now get the column with the name of the stream */
1198 r = view->ops->fetch_int( view, row, ival, &refcol );
1199 if ( r != ERROR_SUCCESS )
1200 return r;
1202 /* lookup the string value from the string table */
1203 sval = msi_string_lookup_id( tv->db->strings, refcol );
1204 if ( !sval )
1205 return ERROR_INVALID_PARAMETER;
1207 else
1209 static const WCHAR fmt[] = { '%','d',0 };
1210 sprintfW( number, fmt, ival );
1211 sval = number;
1214 len = lstrlenW( tv->name ) + 2 + lstrlenW( sval );
1215 full_name = msi_alloc( len*sizeof(WCHAR) );
1216 lstrcpyW( full_name, tv->name );
1217 lstrcatW( full_name, szDot );
1218 lstrcatW( full_name, sval );
1220 r = db_get_raw_stream( tv->db, full_name, stm );
1221 if( r )
1222 ERR("fetching stream %s, error = %d\n",debugstr_w(full_name), r);
1223 msi_free( full_name );
1225 return r;
1228 static UINT TABLE_set_int( MSITABLEVIEW *tv, UINT row, UINT col, UINT val )
1230 UINT offset, n, i;
1231 BYTE **data;
1233 if( !tv->table )
1234 return ERROR_INVALID_PARAMETER;
1236 if( (col==0) || (col>tv->num_cols) )
1237 return ERROR_INVALID_PARAMETER;
1239 if( row >= tv->table->row_count + tv->table->nonpersistent_row_count )
1240 return ERROR_INVALID_PARAMETER;
1242 if( tv->columns[col-1].offset >= tv->row_size )
1244 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1245 ERR("%p %p\n", tv, tv->columns );
1246 return ERROR_FUNCTION_FAILED;
1249 msi_free( tv->columns[col-1].hash_table );
1250 tv->columns[col-1].hash_table = NULL;
1252 if (row >= tv->table->row_count)
1254 row -= tv->table->row_count;
1255 data = tv->table->nonpersistent_data;
1257 else
1258 data = tv->table->data;
1260 n = bytes_per_column( tv->db, &tv->columns[col-1] );
1261 if ( n != 2 && n != 3 && n != 4 )
1263 ERR("oops! what is %d bytes per column?\n", n );
1264 return ERROR_FUNCTION_FAILED;
1267 offset = tv->columns[col-1].offset;
1268 for ( i = 0; i < n; i++ )
1269 data[row][offset + i] = (val >> i * 8) & 0xff;
1271 return ERROR_SUCCESS;
1274 static UINT TABLE_get_row( struct tagMSIVIEW *view, UINT row, MSIRECORD **rec )
1276 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1278 if (!tv->table)
1279 return ERROR_INVALID_PARAMETER;
1281 if (tv->order)
1282 row = tv->order->reorder[row];
1284 return msi_view_get_row(tv->db, view, row, rec);
1287 static UINT TABLE_set_row( struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask )
1289 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1290 UINT i, val, r = ERROR_SUCCESS;
1292 if ( !tv->table )
1293 return ERROR_INVALID_PARAMETER;
1295 /* test if any of the mask bits are invalid */
1296 if ( mask >= (1<<tv->num_cols) )
1297 return ERROR_INVALID_PARAMETER;
1299 for ( i = 0; i < tv->num_cols; i++ )
1301 BOOL persistent;
1303 /* only update the fields specified in the mask */
1304 if ( !(mask&(1<<i)) )
1305 continue;
1307 /* if row >= tv->table->row_count then it is a non-persistent row */
1308 persistent = (tv->table->persistent != MSICONDITION_FALSE) &&
1309 (row < tv->table->row_count);
1310 /* FIXME: should we allow updating keys? */
1312 val = 0;
1313 if ( !MSI_RecordIsNull( rec, i + 1 ) )
1315 if ( MSITYPE_IS_BINARY(tv->columns[ i ].type) )
1317 val = 1; /* refers to the first key column */
1319 else if ( tv->columns[i].type & MSITYPE_STRING )
1321 LPCWSTR sval = MSI_RecordGetString( rec, i + 1 );
1322 UINT ival, x;
1324 r = msi_string2idW(tv->db->strings, sval, &ival);
1325 if (r == ERROR_SUCCESS)
1327 TABLE_fetch_int(&tv->view, row, i + 1, &x);
1328 if (ival == x)
1329 continue;
1332 val = msi_addstringW( tv->db->strings, 0, sval, -1, 1,
1333 persistent ? StringPersistent : StringNonPersistent );
1335 else if ( 2 == bytes_per_column( tv->db, &tv->columns[ i ] ) )
1337 val = 0x8000 + MSI_RecordGetInteger( rec, i + 1 );
1338 if ( val & 0xffff0000 )
1340 ERR("field %u value %d out of range\n", i+1, val - 0x8000 );
1341 return ERROR_FUNCTION_FAILED;
1344 else
1346 INT ival = MSI_RecordGetInteger( rec, i + 1 );
1347 val = ival ^ 0x80000000;
1351 r = TABLE_set_int( tv, row, i+1, val );
1352 if ( r != ERROR_SUCCESS )
1353 break;
1355 return r;
1358 static UINT table_create_new_row( struct tagMSIVIEW *view, UINT *num, BOOL temporary )
1360 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1361 BYTE **p, *row;
1362 UINT sz;
1363 BYTE ***data_ptr;
1364 UINT *row_count;
1366 TRACE("%p %s\n", view, temporary ? "TRUE" : "FALSE");
1368 if( !tv->table )
1369 return ERROR_INVALID_PARAMETER;
1371 row = msi_alloc_zero( tv->row_size );
1372 if( !row )
1373 return ERROR_NOT_ENOUGH_MEMORY;
1375 if( temporary )
1377 row_count = &tv->table->nonpersistent_row_count;
1378 data_ptr = &tv->table->nonpersistent_data;
1379 if (*num == -1)
1380 *num = tv->table->row_count + tv->table->nonpersistent_row_count;
1382 else
1384 row_count = &tv->table->row_count;
1385 data_ptr = &tv->table->data;
1386 if (*num == -1)
1387 *num = tv->table->row_count;
1390 sz = (*row_count + 1) * sizeof (BYTE*);
1391 if( *data_ptr )
1392 p = msi_realloc( *data_ptr, sz );
1393 else
1394 p = msi_alloc( sz );
1395 if( !p )
1397 msi_free( row );
1398 return ERROR_NOT_ENOUGH_MEMORY;
1401 *data_ptr = p;
1402 (*data_ptr)[*row_count] = row;
1403 (*row_count)++;
1405 return ERROR_SUCCESS;
1408 static UINT TABLE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
1410 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1412 TRACE("%p %p\n", tv, record);
1414 TRACE("There are %d columns\n", tv->num_cols );
1416 return ERROR_SUCCESS;
1419 static UINT TABLE_close( struct tagMSIVIEW *view )
1421 TRACE("%p\n", view );
1423 return ERROR_SUCCESS;
1426 static UINT TABLE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols)
1428 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1430 TRACE("%p %p %p\n", view, rows, cols );
1432 if( cols )
1433 *cols = tv->num_cols;
1434 if( rows )
1436 if( !tv->table )
1437 return ERROR_INVALID_PARAMETER;
1438 *rows = tv->table->row_count + tv->table->nonpersistent_row_count;
1441 return ERROR_SUCCESS;
1444 static UINT TABLE_get_column_info( struct tagMSIVIEW *view,
1445 UINT n, LPWSTR *name, UINT *type, BOOL *temporary )
1447 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1449 TRACE("%p %d %p %p\n", tv, n, name, type );
1451 if( ( n == 0 ) || ( n > tv->num_cols ) )
1452 return ERROR_INVALID_PARAMETER;
1454 if( name )
1456 *name = strdupW( tv->columns[n-1].colname );
1457 if( !*name )
1458 return ERROR_FUNCTION_FAILED;
1461 if( type )
1462 *type = tv->columns[n-1].type;
1464 if( temporary )
1465 *temporary = tv->columns[n-1].temporary;
1467 return ERROR_SUCCESS;
1470 static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row );
1472 static UINT table_validate_new( MSITABLEVIEW *tv, MSIRECORD *rec )
1474 UINT r, row, i;
1476 /* check there's no null values where they're not allowed */
1477 for( i = 0; i < tv->num_cols; i++ )
1479 if ( tv->columns[i].type & MSITYPE_NULLABLE )
1480 continue;
1482 if ( MSITYPE_IS_BINARY(tv->columns[i].type) )
1483 TRACE("skipping binary column\n");
1484 else if ( tv->columns[i].type & MSITYPE_STRING )
1486 LPCWSTR str;
1488 str = MSI_RecordGetString( rec, i+1 );
1489 if (str == NULL || str[0] == 0)
1490 return ERROR_INVALID_DATA;
1492 else
1494 UINT n;
1496 n = MSI_RecordGetInteger( rec, i+1 );
1497 if (n == MSI_NULL_INTEGER)
1498 return ERROR_INVALID_DATA;
1502 /* check there's no duplicate keys */
1503 r = msi_table_find_row( tv, rec, &row );
1504 if (r == ERROR_SUCCESS)
1505 return ERROR_FUNCTION_FAILED;
1507 return ERROR_SUCCESS;
1510 static UINT TABLE_insert_row( struct tagMSIVIEW *view, MSIRECORD *rec, UINT row, BOOL temporary )
1512 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1513 UINT i, r, idx, size;
1514 BYTE **data;
1516 TRACE("%p %p %s\n", tv, rec, temporary ? "TRUE" : "FALSE" );
1518 /* check that the key is unique - can we find a matching row? */
1519 r = table_validate_new( tv, rec );
1520 if( r != ERROR_SUCCESS )
1521 return ERROR_FUNCTION_FAILED;
1523 r = table_create_new_row( view, &row, temporary );
1524 TRACE("insert_row returned %08x\n", r);
1525 if( r != ERROR_SUCCESS )
1526 return r;
1528 idx = row;
1529 if( temporary )
1531 data = tv->table->nonpersistent_data;
1532 size = tv->table->nonpersistent_row_count;
1533 idx -= tv->table->row_count;
1535 else
1537 data = tv->table->data;
1538 size = tv->table->row_count;
1541 /* shift the rows to make room for the new row */
1542 if( idx != size - 1 )
1544 for (i = 1; i < size - idx; i++)
1545 memmove(&(data[size - i][0]),
1546 &(data[size - i - 1][0]), tv->row_size);
1549 return TABLE_set_row( view, row, rec, (1<<tv->num_cols) - 1 );
1552 static UINT TABLE_delete_row( struct tagMSIVIEW *view, UINT row )
1554 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1555 UINT r, num_rows, num_cols, i;
1556 BYTE **data;
1558 TRACE("%p %d\n", tv, row);
1560 if ( !tv->table )
1561 return ERROR_INVALID_PARAMETER;
1563 r = TABLE_get_dimensions( view, &num_rows, &num_cols );
1564 if ( r != ERROR_SUCCESS )
1565 return r;
1567 if ( row >= num_rows )
1568 return ERROR_FUNCTION_FAILED;
1570 if ( row < tv->table->row_count )
1572 num_rows = tv->table->row_count;
1573 tv->table->row_count--;
1574 data = tv->table->data;
1576 else
1578 num_rows = tv->table->nonpersistent_row_count;
1579 row -= tv->table->row_count;
1580 tv->table->nonpersistent_row_count--;
1581 data = tv->table->nonpersistent_data;
1584 /* reset the hash tables */
1585 for (i = 0; i < tv->num_cols; i++)
1587 msi_free( tv->columns[i].hash_table );
1588 tv->columns[i].hash_table = NULL;
1591 if ( row == num_rows - 1 )
1592 return ERROR_SUCCESS;
1594 for (i = row + 1; i < num_rows; i++)
1595 memcpy(data[i - 1], data[i], tv->row_size);
1597 return ERROR_SUCCESS;
1600 static UINT msi_table_update(struct tagMSIVIEW *view, MSIRECORD *rec, UINT row)
1602 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1603 UINT r, new_row;
1605 /* FIXME: MsiViewFetch should set rec index 0 to some ID that
1606 * sets the fetched record apart from other records
1609 if (!tv->table)
1610 return ERROR_INVALID_PARAMETER;
1612 r = msi_table_find_row(tv, rec, &new_row);
1613 if (r != ERROR_SUCCESS)
1615 ERR("can't find row to modify\n");
1616 return ERROR_FUNCTION_FAILED;
1619 /* the row cannot be changed */
1620 if (row != new_row + 1)
1621 return ERROR_FUNCTION_FAILED;
1623 return TABLE_set_row(view, new_row, rec, (1 << tv->num_cols) - 1);
1626 static UINT modify_delete_row( struct tagMSIVIEW *view, MSIRECORD *rec )
1628 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1629 UINT row, r;
1631 r = msi_table_find_row(tv, rec, &row);
1632 if (r != ERROR_SUCCESS)
1633 return r;
1635 return TABLE_delete_row(view, row);
1638 static UINT msi_refresh_record( struct tagMSIVIEW *view, MSIRECORD *rec, UINT row )
1640 MSIRECORD *curr;
1641 UINT r, i, count;
1643 r = TABLE_get_row(view, row - 1, &curr);
1644 if (r != ERROR_SUCCESS)
1645 return r;
1647 count = MSI_RecordGetFieldCount(rec);
1648 for (i = 0; i < count; i++)
1649 MSI_RecordCopyField(curr, i + 1, rec, i + 1);
1651 msiobj_release(&curr->hdr);
1652 return ERROR_SUCCESS;
1655 static UINT TABLE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
1656 MSIRECORD *rec, UINT row)
1658 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1659 UINT r;
1661 TRACE("%p %d %p\n", view, eModifyMode, rec );
1663 switch (eModifyMode)
1665 case MSIMODIFY_DELETE:
1666 r = modify_delete_row( view, rec );
1667 break;
1668 case MSIMODIFY_VALIDATE_NEW:
1669 r = table_validate_new( tv, rec );
1670 break;
1672 case MSIMODIFY_INSERT:
1673 r = table_validate_new( tv, rec );
1674 if (r != ERROR_SUCCESS)
1675 break;
1676 r = TABLE_insert_row( view, rec, -1, FALSE );
1677 break;
1679 case MSIMODIFY_INSERT_TEMPORARY:
1680 r = table_validate_new( tv, rec );
1681 if (r != ERROR_SUCCESS)
1682 break;
1683 r = TABLE_insert_row( view, rec, -1, TRUE );
1684 break;
1686 case MSIMODIFY_REFRESH:
1687 r = msi_refresh_record( view, rec, row );
1688 break;
1690 case MSIMODIFY_UPDATE:
1691 r = msi_table_update( view, rec, row );
1692 break;
1694 case MSIMODIFY_ASSIGN:
1695 case MSIMODIFY_REPLACE:
1696 case MSIMODIFY_MERGE:
1697 case MSIMODIFY_VALIDATE:
1698 case MSIMODIFY_VALIDATE_FIELD:
1699 case MSIMODIFY_VALIDATE_DELETE:
1700 FIXME("%p %d %p - mode not implemented\n", view, eModifyMode, rec );
1701 r = ERROR_CALL_NOT_IMPLEMENTED;
1702 break;
1704 default:
1705 r = ERROR_INVALID_DATA;
1708 return r;
1711 static UINT TABLE_delete( struct tagMSIVIEW *view )
1713 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1715 TRACE("%p\n", view );
1717 tv->table = NULL;
1718 tv->columns = NULL;
1720 if (tv->order)
1722 msi_free( tv->order->reorder );
1723 msi_free( tv->order );
1724 tv->order = NULL;
1727 msi_free( tv );
1729 return ERROR_SUCCESS;
1732 static UINT TABLE_find_matching_rows( struct tagMSIVIEW *view, UINT col,
1733 UINT val, UINT *row, MSIITERHANDLE *handle )
1735 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1736 const MSICOLUMNHASHENTRY *entry;
1738 TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
1740 if( !tv->table )
1741 return ERROR_INVALID_PARAMETER;
1743 if( (col==0) || (col > tv->num_cols) )
1744 return ERROR_INVALID_PARAMETER;
1746 if( !tv->columns[col-1].hash_table )
1748 UINT i;
1749 UINT num_rows = tv->table->row_count + tv->table->nonpersistent_row_count;
1750 MSICOLUMNHASHENTRY **hash_table;
1751 MSICOLUMNHASHENTRY *new_entry;
1753 if( tv->columns[col-1].offset >= tv->row_size )
1755 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1756 ERR("%p %p\n", tv, tv->columns );
1757 return ERROR_FUNCTION_FAILED;
1760 /* allocate contiguous memory for the table and its entries so we
1761 * don't have to do an expensive cleanup */
1762 hash_table = msi_alloc(MSITABLE_HASH_TABLE_SIZE * sizeof(MSICOLUMNHASHENTRY*) +
1763 num_rows * sizeof(MSICOLUMNHASHENTRY));
1764 if (!hash_table)
1765 return ERROR_OUTOFMEMORY;
1767 memset(hash_table, 0, MSITABLE_HASH_TABLE_SIZE * sizeof(MSICOLUMNHASHENTRY*));
1768 tv->columns[col-1].hash_table = hash_table;
1770 new_entry = (MSICOLUMNHASHENTRY *)(hash_table + MSITABLE_HASH_TABLE_SIZE);
1772 for (i = 0; i < num_rows; i++, new_entry++)
1774 UINT row_value;
1776 if (view->ops->fetch_int( view, i, col, &row_value ) != ERROR_SUCCESS)
1777 continue;
1779 new_entry->next = NULL;
1780 new_entry->value = row_value;
1781 new_entry->row = i;
1782 if (hash_table[row_value % MSITABLE_HASH_TABLE_SIZE])
1784 MSICOLUMNHASHENTRY *prev_entry = hash_table[row_value % MSITABLE_HASH_TABLE_SIZE];
1785 while (prev_entry->next)
1786 prev_entry = prev_entry->next;
1787 prev_entry->next = new_entry;
1789 else
1790 hash_table[row_value % MSITABLE_HASH_TABLE_SIZE] = new_entry;
1794 if( !*handle )
1795 entry = tv->columns[col-1].hash_table[val % MSITABLE_HASH_TABLE_SIZE];
1796 else
1797 entry = (*handle)->next;
1799 while (entry && entry->value != val)
1800 entry = entry->next;
1802 *handle = entry;
1803 if (!entry)
1804 return ERROR_NO_MORE_ITEMS;
1806 *row = entry->row;
1808 return ERROR_SUCCESS;
1811 static UINT TABLE_add_ref(struct tagMSIVIEW *view)
1813 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1814 UINT i;
1816 TRACE("%p %d\n", view, tv->table->ref_count);
1818 for (i = 0; i < tv->table->col_count; i++)
1820 if (tv->table->colinfo[i].type & MSITYPE_TEMPORARY)
1821 InterlockedIncrement(&tv->table->colinfo[i].ref_count);
1824 return InterlockedIncrement(&tv->table->ref_count);
1827 static UINT TABLE_remove_column(struct tagMSIVIEW *view, LPCWSTR table, UINT number)
1829 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1830 MSIRECORD *rec = NULL;
1831 MSIVIEW *columns = NULL;
1832 UINT row, r;
1834 rec = MSI_CreateRecord(2);
1835 if (!rec)
1836 return ERROR_OUTOFMEMORY;
1838 MSI_RecordSetStringW(rec, 1, table);
1839 MSI_RecordSetInteger(rec, 2, number);
1841 r = TABLE_CreateView(tv->db, szColumns, &columns);
1842 if (r != ERROR_SUCCESS)
1843 return r;
1845 r = msi_table_find_row((MSITABLEVIEW *)columns, rec, &row);
1846 if (r != ERROR_SUCCESS)
1847 goto done;
1849 r = TABLE_delete_row(columns, row);
1850 if (r != ERROR_SUCCESS)
1851 goto done;
1853 msi_update_table_columns(tv->db, table);
1855 done:
1856 msiobj_release(&rec->hdr);
1857 columns->ops->delete(columns);
1858 return r;
1861 static UINT TABLE_release(struct tagMSIVIEW *view)
1863 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1864 INT ref = tv->table->ref_count;
1865 UINT i, r;
1867 TRACE("%p %d\n", view, ref);
1869 for (i = 0; i < tv->table->col_count; i++)
1871 if (tv->table->colinfo[i].type & MSITYPE_TEMPORARY)
1873 ref = InterlockedDecrement(&tv->table->colinfo[i].ref_count);
1874 if (ref == 0)
1876 r = TABLE_remove_column(view, tv->table->colinfo[i].tablename,
1877 tv->table->colinfo[i].number);
1878 if (r != ERROR_SUCCESS)
1879 break;
1884 ref = InterlockedDecrement(&tv->table->ref_count);
1885 if (ref == 0)
1887 if (!tv->table->row_count)
1889 list_remove(&tv->table->entry);
1890 free_table(tv->table);
1891 TABLE_delete(view);
1895 return ref;
1898 static UINT TABLE_add_column(struct tagMSIVIEW *view, LPCWSTR table, UINT number,
1899 LPCWSTR column, UINT type, BOOL hold)
1901 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1902 MSITABLE *msitable;
1903 MSIRECORD *rec;
1904 UINT r, i;
1906 rec = MSI_CreateRecord(4);
1907 if (!rec)
1908 return ERROR_OUTOFMEMORY;
1910 MSI_RecordSetStringW(rec, 1, table);
1911 MSI_RecordSetInteger(rec, 2, number);
1912 MSI_RecordSetStringW(rec, 3, column);
1913 MSI_RecordSetInteger(rec, 4, type);
1915 r = TABLE_insert_row(&tv->view, rec, -1, FALSE);
1916 if (r != ERROR_SUCCESS)
1917 goto done;
1919 msi_update_table_columns(tv->db, table);
1921 if (!hold)
1922 goto done;
1924 msitable = find_cached_table(tv->db, table);
1925 for (i = 0; i < msitable->col_count; i++)
1927 if (!lstrcmpW(msitable->colinfo[i].colname, column))
1929 InterlockedIncrement(&msitable->colinfo[i].ref_count);
1930 break;
1934 done:
1935 msiobj_release(&rec->hdr);
1936 return r;
1939 static UINT order_add_column(struct tagMSIVIEW *view, MSIORDERINFO *order, LPCWSTR name)
1941 UINT n, r, count;
1943 r = TABLE_get_dimensions(view, NULL, &count);
1944 if (r != ERROR_SUCCESS)
1945 return r;
1947 if (order->num_cols >= count)
1948 return ERROR_FUNCTION_FAILED;
1950 r = VIEW_find_column(view, name, &n);
1951 if (r != ERROR_SUCCESS)
1952 return r;
1954 order->cols[order->num_cols] = n;
1955 TRACE("Ordering by column %s (%d)\n", debugstr_w(name), n);
1957 order->num_cols++;
1959 return ERROR_SUCCESS;
1962 static UINT order_compare(struct tagMSIVIEW *view, MSIORDERINFO *order,
1963 UINT a, UINT b, UINT *swap)
1965 UINT r, i, a_val = 0, b_val = 0;
1967 *swap = 0;
1968 for (i = 0; i < order->num_cols; i++)
1970 r = TABLE_fetch_int(view, a, order->cols[i], &a_val);
1971 if (r != ERROR_SUCCESS)
1972 return r;
1974 r = TABLE_fetch_int(view, b, order->cols[i], &b_val);
1975 if (r != ERROR_SUCCESS)
1976 return r;
1978 if (a_val != b_val)
1980 if (a_val > b_val)
1981 *swap = 1;
1982 break;
1986 return ERROR_SUCCESS;
1989 static UINT order_mergesort(struct tagMSIVIEW *view, MSIORDERINFO *order,
1990 UINT left, UINT right)
1992 UINT r, i, j, temp;
1993 UINT swap = 0, center = (left + right) / 2;
1994 UINT *array = order->reorder;
1996 if (left == right)
1997 return ERROR_SUCCESS;
1999 /* sort the left half */
2000 r = order_mergesort(view, order, left, center);
2001 if (r != ERROR_SUCCESS)
2002 return r;
2004 /* sort the right half */
2005 r = order_mergesort(view, order, center + 1, right);
2006 if (r != ERROR_SUCCESS)
2007 return r;
2009 for (i = left, j = center + 1; (i <= center) && (j <= right); i++)
2011 r = order_compare(view, order, array[i], array[j], &swap);
2012 if (r != ERROR_SUCCESS)
2013 return r;
2015 if (swap)
2017 temp = array[j];
2018 memmove(&array[i + 1], &array[i], (j - i) * sizeof(UINT));
2019 array[i] = temp;
2020 j++;
2021 center++;
2025 return ERROR_SUCCESS;
2028 static UINT order_verify(struct tagMSIVIEW *view, MSIORDERINFO *order, UINT num_rows)
2030 UINT i, swap, r;
2032 for (i = 1; i < num_rows; i++)
2034 r = order_compare(view, order, order->reorder[i - 1],
2035 order->reorder[i], &swap);
2036 if (r != ERROR_SUCCESS)
2037 return r;
2039 if (!swap)
2040 continue;
2042 ERR("Bad order! %d\n", i);
2043 return ERROR_FUNCTION_FAILED;
2046 return ERROR_SUCCESS;
2049 static UINT TABLE_sort(struct tagMSIVIEW *view, column_info *columns)
2051 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
2052 MSIORDERINFO *order;
2053 column_info *ptr;
2054 UINT r, i;
2055 UINT rows, cols;
2057 TRACE("sorting table %s\n", debugstr_w(tv->name));
2059 r = TABLE_get_dimensions(view, &rows, &cols);
2060 if (r != ERROR_SUCCESS)
2061 return r;
2063 if (rows == 0)
2064 return ERROR_SUCCESS;
2066 order = msi_alloc_zero(sizeof(MSIORDERINFO) + sizeof(UINT) * cols);
2067 if (!order)
2068 return ERROR_OUTOFMEMORY;
2070 for (ptr = columns; ptr; ptr = ptr->next)
2071 order_add_column(view, order, ptr->column);
2073 order->reorder = msi_alloc(rows * sizeof(UINT));
2074 if (!order->reorder)
2075 return ERROR_OUTOFMEMORY;
2077 for (i = 0; i < rows; i++)
2078 order->reorder[i] = i;
2080 r = order_mergesort(view, order, 0, rows - 1);
2081 if (r != ERROR_SUCCESS)
2082 return r;
2084 r = order_verify(view, order, rows);
2085 if (r != ERROR_SUCCESS)
2086 return r;
2088 tv->order = order;
2090 return ERROR_SUCCESS;
2093 static UINT TABLE_drop(struct tagMSIVIEW *view)
2095 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
2096 MSIVIEW *tables = NULL;
2097 MSIRECORD *rec = NULL;
2098 UINT r, row;
2099 INT i;
2101 TRACE("dropping table %s\n", debugstr_w(tv->name));
2103 for (i = tv->table->col_count - 1; i >= 0; i--)
2105 r = TABLE_remove_column(view, tv->table->colinfo[i].tablename,
2106 tv->table->colinfo[i].number);
2107 if (r != ERROR_SUCCESS)
2108 return r;
2111 rec = MSI_CreateRecord(1);
2112 if (!rec)
2113 return ERROR_OUTOFMEMORY;
2115 MSI_RecordSetStringW(rec, 1, tv->name);
2117 r = TABLE_CreateView(tv->db, szTables, &tables);
2118 if (r != ERROR_SUCCESS)
2119 return r;
2121 r = msi_table_find_row((MSITABLEVIEW *)tables, rec, &row);
2122 if (r != ERROR_SUCCESS)
2123 goto done;
2125 r = TABLE_delete_row(tables, row);
2126 if (r != ERROR_SUCCESS)
2127 goto done;
2129 list_remove(&tv->table->entry);
2130 free_table(tv->table);
2131 TABLE_delete(view);
2133 done:
2134 msiobj_release(&rec->hdr);
2135 tables->ops->delete(tables);
2137 return r;
2140 static const MSIVIEWOPS table_ops =
2142 TABLE_fetch_int,
2143 TABLE_fetch_stream,
2144 TABLE_get_row,
2145 TABLE_set_row,
2146 TABLE_insert_row,
2147 TABLE_delete_row,
2148 TABLE_execute,
2149 TABLE_close,
2150 TABLE_get_dimensions,
2151 TABLE_get_column_info,
2152 TABLE_modify,
2153 TABLE_delete,
2154 TABLE_find_matching_rows,
2155 TABLE_add_ref,
2156 TABLE_release,
2157 TABLE_add_column,
2158 TABLE_remove_column,
2159 TABLE_sort,
2160 TABLE_drop,
2163 UINT TABLE_CreateView( MSIDATABASE *db, LPCWSTR name, MSIVIEW **view )
2165 MSITABLEVIEW *tv ;
2166 UINT r, sz;
2168 static const WCHAR Streams[] = {'_','S','t','r','e','a','m','s',0};
2169 static const WCHAR Storages[] = {'_','S','t','o','r','a','g','e','s',0};
2171 TRACE("%p %s %p\n", db, debugstr_w(name), view );
2173 if ( !lstrcmpW( name, Streams ) )
2174 return STREAMS_CreateView( db, view );
2175 else if ( !lstrcmpW( name, Storages ) )
2176 return STORAGES_CreateView( db, view );
2178 sz = sizeof *tv + lstrlenW(name)*sizeof name[0] ;
2179 tv = msi_alloc_zero( sz );
2180 if( !tv )
2181 return ERROR_FUNCTION_FAILED;
2183 r = get_table( db, name, &tv->table );
2184 if( r != ERROR_SUCCESS )
2186 msi_free( tv );
2187 WARN("table not found\n");
2188 return r;
2191 TRACE("table %p found with %d columns\n", tv->table, tv->table->col_count);
2193 /* fill the structure */
2194 tv->view.ops = &table_ops;
2195 tv->db = db;
2196 tv->columns = tv->table->colinfo;
2197 tv->num_cols = tv->table->col_count;
2198 tv->row_size = msi_table_get_row_size( db, tv->table->colinfo, tv->table->col_count );
2200 TRACE("%s one row is %d bytes\n", debugstr_w(name), tv->row_size );
2202 *view = (MSIVIEW*) tv;
2203 lstrcpyW( tv->name, name );
2205 return ERROR_SUCCESS;
2208 UINT MSI_CommitTables( MSIDATABASE *db )
2210 UINT r;
2211 MSITABLE *table = NULL;
2213 TRACE("%p\n",db);
2215 r = msi_save_string_table( db->strings, db->storage );
2216 if( r != ERROR_SUCCESS )
2218 WARN("failed to save string table r=%08x\n",r);
2219 return r;
2222 LIST_FOR_EACH_ENTRY( table, &db->tables, MSITABLE, entry )
2224 r = save_table( db, table );
2225 if( r != ERROR_SUCCESS )
2227 WARN("failed to save table %s (r=%08x)\n",
2228 debugstr_w(table->name), r);
2229 return r;
2233 /* force everything to reload next time */
2234 free_cached_tables( db );
2236 return ERROR_SUCCESS;
2239 MSICONDITION MSI_DatabaseIsTablePersistent( MSIDATABASE *db, LPCWSTR table )
2241 MSITABLE *t;
2242 UINT r;
2244 TRACE("%p %s\n", db, debugstr_w(table));
2246 if (!table)
2247 return MSICONDITION_ERROR;
2249 r = get_table( db, table, &t );
2250 if (r != ERROR_SUCCESS)
2251 return MSICONDITION_NONE;
2253 return t->persistent;
2256 static UINT read_raw_int(const BYTE *data, UINT col, UINT bytes)
2258 UINT ret = 0, i;
2260 for (i = 0; i < bytes; i++)
2261 ret += (data[col + i] << i * 8);
2263 return ret;
2266 static MSIRECORD *msi_get_transform_record( const MSITABLEVIEW *tv, const string_table *st,
2267 const BYTE *rawdata, UINT bytes_per_strref )
2269 UINT i, val, ofs = 0;
2270 USHORT mask;
2271 MSICOLUMNINFO *columns = tv->columns;
2272 MSIRECORD *rec;
2274 mask = rawdata[0] | (rawdata[1] << 8);
2275 rawdata += 2;
2277 rec = MSI_CreateRecord( tv->num_cols );
2278 if( !rec )
2279 return rec;
2281 TRACE("row ->\n");
2282 for( i=0; i<tv->num_cols; i++ )
2284 if ( (mask&1) && (i>=(mask>>8)) )
2285 break;
2286 /* all keys must be present */
2287 if ( (~mask&1) && (~columns[i].type & MSITYPE_KEY) && ((1<<i) & ~mask) )
2288 continue;
2290 if( (columns[i].type & MSITYPE_STRING) &&
2291 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
2293 LPCWSTR sval;
2295 val = read_raw_int(rawdata, ofs, bytes_per_strref);
2296 sval = msi_string_lookup_id( st, val );
2297 MSI_RecordSetStringW( rec, i+1, sval );
2298 TRACE(" field %d [%s]\n", i+1, debugstr_w(sval));
2299 ofs += bytes_per_strref;
2301 else
2303 UINT n = bytes_per_column( tv->db, &columns[i] );
2304 switch( n )
2306 case 2:
2307 val = read_raw_int(rawdata, ofs, n);
2308 if (val)
2309 MSI_RecordSetInteger( rec, i+1, val^0x8000 );
2310 TRACE(" field %d [0x%04x]\n", i+1, val );
2311 break;
2312 case 4:
2313 val = read_raw_int(rawdata, ofs, n);
2314 if (val)
2315 MSI_RecordSetInteger( rec, i+1, val^0x80000000 );
2316 TRACE(" field %d [0x%08x]\n", i+1, val );
2317 break;
2318 default:
2319 ERR("oops - unknown column width %d\n", n);
2320 break;
2322 ofs += n;
2325 return rec;
2328 static void dump_record( MSIRECORD *rec )
2330 UINT i, n;
2332 n = MSI_RecordGetFieldCount( rec );
2333 for( i=1; i<=n; i++ )
2335 LPCWSTR sval = MSI_RecordGetString( rec, i );
2337 if( MSI_RecordIsNull( rec, i ) )
2338 TRACE("row -> []\n");
2339 else if( (sval = MSI_RecordGetString( rec, i )) )
2340 TRACE("row -> [%s]\n", debugstr_w(sval));
2341 else
2342 TRACE("row -> [0x%08x]\n", MSI_RecordGetInteger( rec, i ) );
2346 static void dump_table( const string_table *st, const USHORT *rawdata, UINT rawsize )
2348 LPCWSTR sval;
2349 UINT i;
2351 for( i=0; i<(rawsize/2); i++ )
2353 sval = msi_string_lookup_id( st, rawdata[i] );
2354 MESSAGE(" %04x %s\n", rawdata[i], debugstr_w(sval) );
2358 static UINT* msi_record_to_row( const MSITABLEVIEW *tv, MSIRECORD *rec )
2360 LPCWSTR str;
2361 UINT i, r, *data;
2363 data = msi_alloc( tv->num_cols *sizeof (UINT) );
2364 for( i=0; i<tv->num_cols; i++ )
2366 data[i] = 0;
2368 if ( ~tv->columns[i].type & MSITYPE_KEY )
2369 continue;
2371 /* turn the transform column value into a row value */
2372 if ( ( tv->columns[i].type & MSITYPE_STRING ) &&
2373 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
2375 str = MSI_RecordGetString( rec, i+1 );
2376 r = msi_string2idW( tv->db->strings, str, &data[i] );
2378 /* if there's no matching string in the string table,
2379 these keys can't match any record, so fail now. */
2380 if( ERROR_SUCCESS != r )
2382 msi_free( data );
2383 return NULL;
2386 else
2388 data[i] = MSI_RecordGetInteger( rec, i+1 );
2390 if (data[i] == MSI_NULL_INTEGER)
2391 data[i] = 0;
2392 else if ((tv->columns[i].type&0xff) == 2)
2393 data[i] += 0x8000;
2394 else
2395 data[i] += 0x80000000;
2398 return data;
2401 static UINT msi_row_matches( MSITABLEVIEW *tv, UINT row, const UINT *data )
2403 UINT i, r, x, ret = ERROR_FUNCTION_FAILED;
2405 for( i=0; i<tv->num_cols; i++ )
2407 if ( ~tv->columns[i].type & MSITYPE_KEY )
2408 continue;
2410 /* turn the transform column value into a row value */
2411 r = TABLE_fetch_int( &tv->view, row, i+1, &x );
2412 if ( r != ERROR_SUCCESS )
2414 ERR("TABLE_fetch_int shouldn't fail here\n");
2415 break;
2418 /* if this key matches, move to the next column */
2419 if ( x != data[i] )
2421 ret = ERROR_FUNCTION_FAILED;
2422 break;
2425 ret = ERROR_SUCCESS;
2428 return ret;
2431 static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row )
2433 UINT i, r = ERROR_FUNCTION_FAILED, *data;
2435 data = msi_record_to_row( tv, rec );
2436 if( !data )
2437 return r;
2438 for( i = 0; i < tv->table->row_count + tv->table->nonpersistent_row_count; i++ )
2440 r = msi_row_matches( tv, i, data );
2441 if( r == ERROR_SUCCESS )
2443 *row = i;
2444 break;
2447 msi_free( data );
2448 return r;
2451 typedef struct
2453 struct list entry;
2454 LPWSTR name;
2455 } TRANSFORMDATA;
2457 static UINT msi_table_load_transform( MSIDATABASE *db, IStorage *stg,
2458 string_table *st, TRANSFORMDATA *transform,
2459 UINT bytes_per_strref )
2461 UINT rawsize = 0;
2462 BYTE *rawdata = NULL;
2463 MSITABLEVIEW *tv = NULL;
2464 UINT r, n, sz, i, mask;
2465 MSIRECORD *rec = NULL;
2466 UINT colcol = 0;
2467 WCHAR coltable[32];
2468 LPWSTR name;
2470 if (!transform)
2471 return ERROR_SUCCESS;
2473 name = transform->name;
2475 coltable[0] = 0;
2476 TRACE("%p %p %p %s\n", db, stg, st, debugstr_w(name) );
2478 /* read the transform data */
2479 read_stream_data( stg, name, TRUE, &rawdata, &rawsize );
2480 if ( !rawdata )
2482 TRACE("table %s empty\n", debugstr_w(name) );
2483 return ERROR_INVALID_TABLE;
2486 /* create a table view */
2487 r = TABLE_CreateView( db, name, (MSIVIEW**) &tv );
2488 if( r != ERROR_SUCCESS )
2489 goto err;
2491 r = tv->view.ops->execute( &tv->view, NULL );
2492 if( r != ERROR_SUCCESS )
2493 goto err;
2495 TRACE("name = %s columns = %u row_size = %u raw size = %u\n",
2496 debugstr_w(name), tv->num_cols, tv->row_size, rawsize );
2498 /* interpret the data */
2499 r = ERROR_SUCCESS;
2500 for( n=0; n < rawsize; )
2502 mask = rawdata[n] | (rawdata[n+1] << 8);
2504 if (mask&1)
2507 * if the low bit is set, columns are continuous and
2508 * the number of columns is specified in the high byte
2510 sz = 2;
2511 for( i=0; i<tv->num_cols; i++ )
2513 if( (tv->columns[i].type & MSITYPE_STRING) &&
2514 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
2515 sz += bytes_per_strref;
2516 else
2517 sz += bytes_per_column( tv->db, &tv->columns[i] );
2520 else
2523 * If the low bit is not set, mask is a bitmask.
2524 * Excepting for key fields, which are always present,
2525 * each bit indicates that a field is present in the transform record.
2527 * mask == 0 is a special case ... only the keys will be present
2528 * and it means that this row should be deleted.
2530 sz = 2;
2531 for( i=0; i<tv->num_cols; i++ )
2533 if( (tv->columns[i].type & MSITYPE_KEY) || ((1<<i)&mask))
2535 if( (tv->columns[i].type & MSITYPE_STRING) &&
2536 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
2537 sz += bytes_per_strref;
2538 else
2539 sz += bytes_per_column( tv->db, &tv->columns[i] );
2544 /* check we didn't run of the end of the table */
2545 if ( (n+sz) > rawsize )
2547 ERR("borked.\n");
2548 dump_table( st, (USHORT *)rawdata, rawsize );
2549 break;
2552 rec = msi_get_transform_record( tv, st, &rawdata[n], bytes_per_strref );
2553 if (rec)
2555 if ( mask & 1 )
2557 WCHAR table[32];
2558 DWORD sz = 32;
2559 UINT number = MSI_NULL_INTEGER;
2561 TRACE("inserting record\n");
2563 if (!lstrcmpW(name, szColumns))
2565 MSI_RecordGetStringW( rec, 1, table, &sz );
2566 number = MSI_RecordGetInteger( rec, 2 );
2569 * Native msi seems writes nul into the Number (2nd) column of
2570 * the _Columns table, only when the columns are from a new table
2572 if ( number == MSI_NULL_INTEGER )
2574 /* reset the column number on a new table */
2575 if ( lstrcmpW(coltable, table) )
2577 colcol = 0;
2578 lstrcpyW( coltable, table );
2581 /* fix nul column numbers */
2582 MSI_RecordSetInteger( rec, 2, ++colcol );
2586 r = TABLE_insert_row( &tv->view, rec, -1, FALSE );
2587 if (r != ERROR_SUCCESS)
2588 ERR("insert row failed\n");
2590 if ( number != MSI_NULL_INTEGER && !lstrcmpW(name, szColumns) )
2591 msi_update_table_columns( db, table );
2593 else
2595 UINT row = 0;
2597 r = msi_table_find_row( tv, rec, &row );
2598 if (r != ERROR_SUCCESS)
2599 ERR("no matching row to transform\n");
2600 else if ( mask )
2602 TRACE("modifying row [%d]:\n", row);
2603 TABLE_set_row( &tv->view, row, rec, mask );
2605 else
2607 TRACE("deleting row [%d]:\n", row);
2608 TABLE_delete_row( &tv->view, row );
2611 if( TRACE_ON(msidb) ) dump_record( rec );
2612 msiobj_release( &rec->hdr );
2615 n += sz;
2618 err:
2619 /* no need to free the table, it's associated with the database */
2620 msi_free( rawdata );
2621 if( tv )
2622 tv->view.ops->delete( &tv->view );
2624 return ERROR_SUCCESS;
2628 * msi_table_apply_transform
2630 * Enumerate the table transforms in a transform storage and apply each one.
2632 UINT msi_table_apply_transform( MSIDATABASE *db, IStorage *stg )
2634 struct list transforms;
2635 IEnumSTATSTG *stgenum = NULL;
2636 TRANSFORMDATA *transform;
2637 TRANSFORMDATA *tables = NULL, *columns = NULL;
2638 HRESULT r;
2639 STATSTG stat;
2640 string_table *strings;
2641 UINT ret = ERROR_FUNCTION_FAILED;
2642 UINT bytes_per_strref;
2644 TRACE("%p %p\n", db, stg );
2646 strings = msi_load_string_table( stg, &bytes_per_strref );
2647 if( !strings )
2648 goto end;
2650 r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
2651 if( FAILED( r ) )
2652 goto end;
2654 list_init(&transforms);
2656 while ( TRUE )
2658 MSITABLEVIEW *tv = NULL;
2659 WCHAR name[0x40];
2660 ULONG count = 0;
2662 r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
2663 if ( FAILED( r ) || !count )
2664 break;
2666 decode_streamname( stat.pwcsName, name );
2667 CoTaskMemFree( stat.pwcsName );
2668 if ( name[0] != 0x4840 )
2669 continue;
2671 if ( !lstrcmpW( name+1, szStringPool ) ||
2672 !lstrcmpW( name+1, szStringData ) )
2673 continue;
2675 transform = msi_alloc_zero( sizeof(TRANSFORMDATA) );
2676 if ( !transform )
2677 break;
2679 list_add_tail( &transforms, &transform->entry );
2681 transform->name = strdupW( name + 1 );
2683 if ( !lstrcmpW( transform->name, szTables ) )
2684 tables = transform;
2685 else if (!lstrcmpW( transform->name, szColumns ) )
2686 columns = transform;
2688 TRACE("transform contains stream %s\n", debugstr_w(name));
2690 /* load the table */
2691 r = TABLE_CreateView( db, transform->name, (MSIVIEW**) &tv );
2692 if( r != ERROR_SUCCESS )
2693 continue;
2695 r = tv->view.ops->execute( &tv->view, NULL );
2696 if( r != ERROR_SUCCESS )
2698 tv->view.ops->delete( &tv->view );
2699 continue;
2702 tv->view.ops->delete( &tv->view );
2706 * Apply _Tables and _Columns transforms first so that
2707 * the table metadata is correct, and empty tables exist.
2709 ret = msi_table_load_transform( db, stg, strings, tables, bytes_per_strref );
2710 if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
2711 goto end;
2713 ret = msi_table_load_transform( db, stg, strings, columns, bytes_per_strref );
2714 if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
2715 goto end;
2717 ret = ERROR_SUCCESS;
2719 while ( !list_empty( &transforms ) )
2721 transform = LIST_ENTRY( list_head( &transforms ), TRANSFORMDATA, entry );
2723 if ( lstrcmpW( transform->name, szColumns ) &&
2724 lstrcmpW( transform->name, szTables ) &&
2725 ret == ERROR_SUCCESS )
2727 ret = msi_table_load_transform( db, stg, strings, transform, bytes_per_strref );
2730 list_remove( &transform->entry );
2731 msi_free( transform->name );
2732 msi_free( transform );
2735 if ( ret == ERROR_SUCCESS )
2736 append_storage_to_db( db, stg );
2738 end:
2739 if ( stgenum )
2740 IEnumSTATSTG_Release( stgenum );
2741 if ( strings )
2742 msi_destroy_stringtable( strings );
2744 return ret;
2747 void append_storage_to_db( MSIDATABASE *db, IStorage *stg )
2749 MSITRANSFORM *t;
2751 t = msi_alloc( sizeof *t );
2752 t->stg = stg;
2753 IStorage_AddRef( stg );
2754 list_add_tail( &db->transforms, &t->entry );
2757 void msi_free_transforms( MSIDATABASE *db )
2759 while( !list_empty( &db->transforms ) )
2761 MSITRANSFORM *t = LIST_ENTRY( list_head( &db->transforms ),
2762 MSITRANSFORM, entry );
2763 list_remove( &t->entry );
2764 IStorage_Release( t->stg );
2765 msi_free( t );