86cd4be4a79c477d98fd1df83f0c500e41fd8940
[reactos.git] / reactos / dll / win32 / msi / table.c
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
3 *
4 * Copyright 2002-2005 Mike McCormack for CodeWeavers
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include <stdarg.h>
22 #include <assert.h>
23
24 #define COBJMACROS
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
27
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"
38
39 #include "wine/debug.h"
40 #include "wine/unicode.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
43
44 #define MSITABLE_HASH_TABLE_SIZE 37
45 #define LONG_STR_BYTES 3
46
47 typedef struct tagMSICOLUMNHASHENTRY
48 {
49 struct tagMSICOLUMNHASHENTRY *next;
50 UINT value;
51 UINT row;
52 } MSICOLUMNHASHENTRY;
53
54 typedef struct tagMSICOLUMNINFO
55 {
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;
65
66 typedef struct tagMSIORDERINFO
67 {
68 UINT *reorder;
69 UINT num_cols;
70 UINT cols[1];
71 } MSIORDERINFO;
72
73 struct tagMSITABLE
74 {
75 BYTE **data;
76 BOOL *data_persistent;
77 UINT row_count;
78 struct list entry;
79 MSICOLUMNINFO *colinfo;
80 UINT col_count;
81 MSICONDITION persistent;
82 INT ref_count;
83 WCHAR name[1];
84 };
85
86 typedef struct tagMSITRANSFORM {
87 struct list entry;
88 IStorage *stg;
89 } MSITRANSFORM;
90
91 static const WCHAR szStringData[] = {
92 '_','S','t','r','i','n','g','D','a','t','a',0 };
93 static const WCHAR szStringPool[] = {
94 '_','S','t','r','i','n','g','P','o','o','l',0 };
95
96 /* information for default tables */
97 static WCHAR szTables[] = { '_','T','a','b','l','e','s',0 };
98 static WCHAR szTable[] = { 'T','a','b','l','e',0 };
99 static WCHAR szName[] = { 'N','a','m','e',0 };
100 static WCHAR szColumns[] = { '_','C','o','l','u','m','n','s',0 };
101 static WCHAR szNumber[] = { 'N','u','m','b','e','r',0 };
102 static WCHAR szType[] = { 'T','y','p','e',0 };
103
104 static const MSICOLUMNINFO _Columns_cols[4] = {
105 { szColumns, 1, szTable, MSITYPE_VALID | MSITYPE_STRING | MSITYPE_KEY | 64, 0, 0, 0, NULL },
106 { szColumns, 2, szNumber, MSITYPE_VALID | MSITYPE_KEY | 2, 2, 0, 0, NULL },
107 { szColumns, 3, szName, MSITYPE_VALID | MSITYPE_STRING | 64, 4, 0, 0, NULL },
108 { szColumns, 4, szType, MSITYPE_VALID | 2, 6, 0, 0, NULL },
109 };
110
111 static const MSICOLUMNINFO _Tables_cols[1] = {
112 { szTables, 1, szName, MSITYPE_VALID | MSITYPE_STRING | MSITYPE_KEY | 64, 0, 0, 0, NULL },
113 };
114
115 #define MAX_STREAM_NAME 0x1f
116
117 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name,
118 MSICOLUMNINFO **pcols, UINT *pcount );
119 static void table_calc_column_offsets( MSIDATABASE *db, MSICOLUMNINFO *colinfo,
120 DWORD count );
121 static UINT get_tablecolumns( MSIDATABASE *db,
122 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz);
123 static void msi_free_colinfo( MSICOLUMNINFO *colinfo, UINT count );
124 static UINT table_find_insert_idx (MSIVIEW *view, LPCWSTR name, INT *pidx);
125
126 static inline UINT bytes_per_column( MSIDATABASE *db, const MSICOLUMNINFO *col )
127 {
128 if( MSITYPE_IS_BINARY(col->type) )
129 return 2;
130
131 if( col->type & MSITYPE_STRING )
132 return db->bytes_per_strref;
133
134 if( (col->type & 0xff) <= 2)
135 return 2;
136
137 if( (col->type & 0xff) != 4 )
138 ERR("Invalid column size!\n");
139
140 return 4;
141 }
142
143 static int utf2mime(int x)
144 {
145 if( (x>='0') && (x<='9') )
146 return x-'0';
147 if( (x>='A') && (x<='Z') )
148 return x-'A'+10;
149 if( (x>='a') && (x<='z') )
150 return x-'a'+10+26;
151 if( x=='.' )
152 return 10+26+26;
153 if( x=='_' )
154 return 10+26+26+1;
155 return -1;
156 }
157
158 static LPWSTR encode_streamname(BOOL bTable, LPCWSTR in)
159 {
160 DWORD count = MAX_STREAM_NAME;
161 DWORD ch, next;
162 LPWSTR out, p;
163
164 if( !bTable )
165 count = lstrlenW( in )+2;
166 out = msi_alloc( count*sizeof(WCHAR) );
167 p = out;
168
169 if( bTable )
170 {
171 *p++ = 0x4840;
172 count --;
173 }
174 while( count -- )
175 {
176 ch = *in++;
177 if( !ch )
178 {
179 *p = ch;
180 return out;
181 }
182 if( ( ch < 0x80 ) && ( utf2mime(ch) >= 0 ) )
183 {
184 ch = utf2mime(ch) + 0x4800;
185 next = *in;
186 if( next && (next<0x80) )
187 {
188 next = utf2mime(next);
189 if( next != -1 )
190 {
191 next += 0x3ffffc0;
192 ch += (next<<6);
193 in++;
194 }
195 }
196 }
197 *p++ = ch;
198 }
199 ERR("Failed to encode stream name (%s)\n",debugstr_w(in));
200 msi_free( out );
201 return NULL;
202 }
203
204 static int mime2utf(int x)
205 {
206 if( x<10 )
207 return x + '0';
208 if( x<(10+26))
209 return x - 10 + 'A';
210 if( x<(10+26+26))
211 return x - 10 - 26 + 'a';
212 if( x == (10+26+26) )
213 return '.';
214 return '_';
215 }
216
217 BOOL decode_streamname(LPCWSTR in, LPWSTR out)
218 {
219 WCHAR ch;
220 DWORD count = 0;
221
222 while ( (ch = *in++) )
223 {
224 if( (ch >= 0x3800 ) && (ch < 0x4840 ) )
225 {
226 if( ch >= 0x4800 )
227 ch = mime2utf(ch-0x4800);
228 else
229 {
230 ch -= 0x3800;
231 *out++ = mime2utf(ch&0x3f);
232 count++;
233 ch = mime2utf((ch>>6)&0x3f);
234 }
235 }
236 *out++ = ch;
237 count++;
238 }
239 *out = 0;
240 return count;
241 }
242
243 void enum_stream_names( IStorage *stg )
244 {
245 IEnumSTATSTG *stgenum = NULL;
246 HRESULT r;
247 STATSTG stat;
248 ULONG n, count;
249 WCHAR name[0x40];
250
251 r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
252 if( FAILED( r ) )
253 return;
254
255 n = 0;
256 while( 1 )
257 {
258 count = 0;
259 r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
260 if( FAILED( r ) || !count )
261 break;
262 decode_streamname( stat.pwcsName, name );
263 TRACE("stream %2d -> %s %s\n", n,
264 debugstr_w(stat.pwcsName), debugstr_w(name) );
265 CoTaskMemFree( stat.pwcsName );
266 n++;
267 }
268
269 IEnumSTATSTG_Release( stgenum );
270 }
271
272 UINT read_stream_data( IStorage *stg, LPCWSTR stname, BOOL table,
273 BYTE **pdata, UINT *psz )
274 {
275 HRESULT r;
276 UINT ret = ERROR_FUNCTION_FAILED;
277 VOID *data;
278 ULONG sz, count;
279 IStream *stm = NULL;
280 STATSTG stat;
281 LPWSTR encname;
282
283 encname = encode_streamname(table, stname);
284
285 TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
286
287 r = IStorage_OpenStream(stg, encname, NULL,
288 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stm);
289 msi_free( encname );
290 if( FAILED( r ) )
291 {
292 WARN("open stream failed r = %08x - empty table?\n", r);
293 return ret;
294 }
295
296 r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
297 if( FAILED( r ) )
298 {
299 WARN("open stream failed r = %08x!\n", r);
300 goto end;
301 }
302
303 if( stat.cbSize.QuadPart >> 32 )
304 {
305 WARN("Too big!\n");
306 goto end;
307 }
308
309 sz = stat.cbSize.QuadPart;
310 data = msi_alloc( sz );
311 if( !data )
312 {
313 WARN("couldn't allocate memory r=%08x!\n", r);
314 ret = ERROR_NOT_ENOUGH_MEMORY;
315 goto end;
316 }
317
318 r = IStream_Read(stm, data, sz, &count );
319 if( FAILED( r ) || ( count != sz ) )
320 {
321 msi_free( data );
322 WARN("read stream failed r = %08x!\n", r);
323 goto end;
324 }
325
326 *pdata = data;
327 *psz = sz;
328 ret = ERROR_SUCCESS;
329
330 end:
331 IStream_Release( stm );
332
333 return ret;
334 }
335
336 static UINT db_get_raw_stream( MSIDATABASE *db, LPCWSTR stname, IStream **stm )
337 {
338 LPWSTR encname;
339 HRESULT r;
340
341 encname = encode_streamname(FALSE, stname);
342
343 TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
344
345 r = IStorage_OpenStream(db->storage, encname, NULL,
346 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, stm);
347 if( FAILED( r ) )
348 {
349 MSITRANSFORM *transform;
350
351 LIST_FOR_EACH_ENTRY( transform, &db->transforms, MSITRANSFORM, entry )
352 {
353 TRACE("looking for %s in transform storage\n", debugstr_w(stname) );
354 r = IStorage_OpenStream( transform->stg, encname, NULL,
355 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, stm );
356 if (SUCCEEDED(r))
357 break;
358 }
359 }
360
361 msi_free( encname );
362
363 return SUCCEEDED(r) ? ERROR_SUCCESS : ERROR_FUNCTION_FAILED;
364 }
365
366 UINT read_raw_stream_data( MSIDATABASE *db, LPCWSTR stname,
367 USHORT **pdata, UINT *psz )
368 {
369 HRESULT r;
370 UINT ret = ERROR_FUNCTION_FAILED;
371 VOID *data;
372 ULONG sz, count;
373 IStream *stm = NULL;
374 STATSTG stat;
375
376 r = db_get_raw_stream( db, stname, &stm );
377 if( r != ERROR_SUCCESS)
378 return ret;
379 r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
380 if( FAILED( r ) )
381 {
382 WARN("open stream failed r = %08x!\n", r);
383 goto end;
384 }
385
386 if( stat.cbSize.QuadPart >> 32 )
387 {
388 WARN("Too big!\n");
389 goto end;
390 }
391
392 sz = stat.cbSize.QuadPart;
393 data = msi_alloc( sz );
394 if( !data )
395 {
396 WARN("couldn't allocate memory r=%08x!\n", r);
397 ret = ERROR_NOT_ENOUGH_MEMORY;
398 goto end;
399 }
400
401 r = IStream_Read(stm, data, sz, &count );
402 if( FAILED( r ) || ( count != sz ) )
403 {
404 msi_free( data );
405 WARN("read stream failed r = %08x!\n", r);
406 goto end;
407 }
408
409 *pdata = data;
410 *psz = sz;
411 ret = ERROR_SUCCESS;
412
413 end:
414 IStream_Release( stm );
415
416 return ret;
417 }
418
419 UINT write_stream_data( IStorage *stg, LPCWSTR stname,
420 LPCVOID data, UINT sz, BOOL bTable )
421 {
422 HRESULT r;
423 UINT ret = ERROR_FUNCTION_FAILED;
424 ULONG count;
425 IStream *stm = NULL;
426 ULARGE_INTEGER size;
427 LARGE_INTEGER pos;
428 LPWSTR encname;
429
430 encname = encode_streamname(bTable, stname );
431 r = IStorage_OpenStream( stg, encname, NULL,
432 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, &stm);
433 if( FAILED(r) )
434 {
435 r = IStorage_CreateStream( stg, encname,
436 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
437 }
438 msi_free( encname );
439 if( FAILED( r ) )
440 {
441 WARN("open stream failed r = %08x\n", r);
442 return ret;
443 }
444
445 size.QuadPart = sz;
446 r = IStream_SetSize( stm, size );
447 if( FAILED( r ) )
448 {
449 WARN("Failed to SetSize\n");
450 goto end;
451 }
452
453 pos.QuadPart = 0;
454 r = IStream_Seek( stm, pos, STREAM_SEEK_SET, NULL );
455 if( FAILED( r ) )
456 {
457 WARN("Failed to Seek\n");
458 goto end;
459 }
460
461 if (sz)
462 {
463 r = IStream_Write(stm, data, sz, &count );
464 if( FAILED( r ) || ( count != sz ) )
465 {
466 WARN("Failed to Write\n");
467 goto end;
468 }
469 }
470
471 ret = ERROR_SUCCESS;
472
473 end:
474 IStream_Release( stm );
475
476 return ret;
477 }
478
479 static void free_table( MSITABLE *table )
480 {
481 UINT i;
482 for( i=0; i<table->row_count; i++ )
483 msi_free( table->data[i] );
484 msi_free( table->data );
485 msi_free( table->data_persistent );
486 msi_free_colinfo( table->colinfo, table->col_count );
487 msi_free( table->colinfo );
488 msi_free( table );
489 }
490
491 static UINT msi_table_get_row_size( MSIDATABASE *db,const MSICOLUMNINFO *cols,
492 UINT count )
493 {
494 const MSICOLUMNINFO *last_col = &cols[count-1];
495 if (!count)
496 return 0;
497 return last_col->offset + bytes_per_column( db, last_col );
498 }
499
500 /* add this table to the list of cached tables in the database */
501 static UINT read_table_from_storage( MSIDATABASE *db, MSITABLE *t, IStorage *stg )
502 {
503 BYTE *rawdata = NULL;
504 UINT rawsize = 0, i, j, row_size = 0;
505
506 TRACE("%s\n",debugstr_w(t->name));
507
508 row_size = msi_table_get_row_size( db, t->colinfo, t->col_count );
509
510 /* if we can't read the table, just assume that it's empty */
511 read_stream_data( stg, t->name, TRUE, &rawdata, &rawsize );
512 if( !rawdata )
513 return ERROR_SUCCESS;
514
515 TRACE("Read %d bytes\n", rawsize );
516
517 if( rawsize % row_size )
518 {
519 WARN("Table size is invalid %d/%d\n", rawsize, row_size );
520 goto err;
521 }
522
523 t->row_count = rawsize / row_size;
524 t->data = msi_alloc_zero( t->row_count * sizeof (USHORT*) );
525 if( !t->data )
526 goto err;
527 t->data_persistent = msi_alloc_zero( t->row_count * sizeof(BOOL));
528 if ( !t->data_persistent )
529 goto err;
530
531 /* transpose all the data */
532 TRACE("Transposing data from %d rows\n", t->row_count );
533 for( i=0; i<t->row_count; i++ )
534 {
535 t->data[i] = msi_alloc( row_size );
536 if( !t->data[i] )
537 goto err;
538 t->data_persistent[i] = TRUE;
539
540 for( j=0; j<t->col_count; j++ )
541 {
542 UINT ofs = t->colinfo[j].offset;
543 UINT n = bytes_per_column( db, &t->colinfo[j] );
544 UINT k;
545
546 if ( n != 2 && n != 3 && n != 4 )
547 {
548 ERR("oops - unknown column width %d\n", n);
549 goto err;
550 }
551
552 for ( k = 0; k < n; k++ )
553 t->data[i][ofs + k] = rawdata[ofs*t->row_count + i * n + k];
554 }
555 }
556
557 msi_free( rawdata );
558 return ERROR_SUCCESS;
559 err:
560 msi_free( rawdata );
561 return ERROR_FUNCTION_FAILED;
562 }
563
564 void free_cached_tables( MSIDATABASE *db )
565 {
566 while( !list_empty( &db->tables ) )
567 {
568 MSITABLE *t = LIST_ENTRY( list_head( &db->tables ), MSITABLE, entry );
569
570 list_remove( &t->entry );
571 free_table( t );
572 }
573 }
574
575 static MSITABLE *find_cached_table( MSIDATABASE *db, LPCWSTR name )
576 {
577 MSITABLE *t;
578
579 LIST_FOR_EACH_ENTRY( t, &db->tables, MSITABLE, entry )
580 if( !lstrcmpW( name, t->name ) )
581 return t;
582
583 return NULL;
584 }
585
586 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name, MSICOLUMNINFO **pcols, UINT *pcount )
587 {
588 UINT r, column_count = 0;
589 MSICOLUMNINFO *columns;
590
591 /* get the number of columns in this table */
592 column_count = 0;
593 r = get_tablecolumns( db, name, NULL, &column_count );
594 if( r != ERROR_SUCCESS )
595 return r;
596
597 *pcount = column_count;
598
599 /* if there's no columns, there's no table */
600 if( column_count == 0 )
601 return ERROR_INVALID_PARAMETER;
602
603 TRACE("Table %s found\n", debugstr_w(name) );
604
605 columns = msi_alloc( column_count*sizeof (MSICOLUMNINFO) );
606 if( !columns )
607 return ERROR_FUNCTION_FAILED;
608
609 r = get_tablecolumns( db, name, columns, &column_count );
610 if( r != ERROR_SUCCESS )
611 {
612 msi_free( columns );
613 return ERROR_FUNCTION_FAILED;
614 }
615
616 *pcols = columns;
617
618 return r;
619 }
620
621 UINT msi_create_table( MSIDATABASE *db, LPCWSTR name, column_info *col_info,
622 MSICONDITION persistent, MSITABLE **table_ret)
623 {
624 UINT r, nField;
625 MSIVIEW *tv = NULL;
626 MSIRECORD *rec = NULL;
627 column_info *col;
628 MSITABLE *table;
629 UINT i;
630 INT idx;
631
632 /* only add tables that don't exist already */
633 if( TABLE_Exists(db, name ) )
634 {
635 WARN("table %s exists\n", debugstr_w(name));
636 return ERROR_BAD_QUERY_SYNTAX;
637 }
638
639 table = msi_alloc( sizeof (MSITABLE) + lstrlenW(name)*sizeof (WCHAR) );
640 if( !table )
641 return ERROR_FUNCTION_FAILED;
642
643 table->ref_count = 1;
644 table->row_count = 0;
645 table->data = NULL;
646 table->data_persistent = NULL;
647 table->colinfo = NULL;
648 table->col_count = 0;
649 table->persistent = persistent;
650 lstrcpyW( table->name, name );
651
652 for( col = col_info; col; col = col->next )
653 table->col_count++;
654
655 table->colinfo = msi_alloc( table->col_count * sizeof(MSICOLUMNINFO) );
656 if (!table->colinfo)
657 {
658 free_table( table );
659 return ERROR_FUNCTION_FAILED;
660 }
661
662 for( i = 0, col = col_info; col; i++, col = col->next )
663 {
664 table->colinfo[ i ].tablename = strdupW( col->table );
665 table->colinfo[ i ].number = i + 1;
666 table->colinfo[ i ].colname = strdupW( col->column );
667 table->colinfo[ i ].type = col->type;
668 table->colinfo[ i ].offset = 0;
669 table->colinfo[ i ].ref_count = 0;
670 table->colinfo[ i ].hash_table = NULL;
671 table->colinfo[ i ].temporary = col->temporary;
672 }
673 table_calc_column_offsets( db, table->colinfo, table->col_count);
674
675 r = TABLE_CreateView( db, szTables, &tv );
676 TRACE("CreateView returned %x\n", r);
677 if( r )
678 {
679 free_table( table );
680 return r;
681 }
682
683 r = tv->ops->execute( tv, 0 );
684 TRACE("tv execute returned %x\n", r);
685 if( r )
686 goto err;
687
688 rec = MSI_CreateRecord( 1 );
689 if( !rec )
690 goto err;
691
692 r = MSI_RecordSetStringW( rec, 1, name );
693 if( r )
694 goto err;
695
696 r = table_find_insert_idx (tv, name, &idx);
697 if (r != ERROR_SUCCESS)
698 idx = -1;
699
700 r = tv->ops->insert_row( tv, rec, idx, persistent == MSICONDITION_FALSE );
701 TRACE("insert_row returned %x\n", r);
702 if( r )
703 goto err;
704
705 tv->ops->delete( tv );
706 tv = NULL;
707
708 msiobj_release( &rec->hdr );
709 rec = NULL;
710
711 if( persistent != MSICONDITION_FALSE )
712 {
713 /* add each column to the _Columns table */
714 r = TABLE_CreateView( db, szColumns, &tv );
715 if( r )
716 return r;
717
718 r = tv->ops->execute( tv, 0 );
719 TRACE("tv execute returned %x\n", r);
720 if( r )
721 goto err;
722
723 rec = MSI_CreateRecord( 4 );
724 if( !rec )
725 goto err;
726
727 r = MSI_RecordSetStringW( rec, 1, name );
728 if( r )
729 goto err;
730
731 /*
732 * need to set the table, column number, col name and type
733 * for each column we enter in the table
734 */
735 nField = 1;
736 for( col = col_info; col; col = col->next )
737 {
738 r = MSI_RecordSetInteger( rec, 2, nField );
739 if( r )
740 goto err;
741
742 r = MSI_RecordSetStringW( rec, 3, col->column );
743 if( r )
744 goto err;
745
746 r = MSI_RecordSetInteger( rec, 4, col->type );
747 if( r )
748 goto err;
749
750 r = table_find_insert_idx (tv, name, &idx);
751 if (r != ERROR_SUCCESS)
752 idx = -1;
753
754 r = tv->ops->insert_row( tv, rec, idx, FALSE );
755 if( r )
756 goto err;
757
758 nField++;
759 }
760 if( !col )
761 r = ERROR_SUCCESS;
762 }
763
764 err:
765 if (rec)
766 msiobj_release( &rec->hdr );
767 /* FIXME: remove values from the string table on error */
768 if( tv )
769 tv->ops->delete( tv );
770
771 if (r == ERROR_SUCCESS)
772 {
773 list_add_head( &db->tables, &table->entry );
774 *table_ret = table;
775 }
776 else
777 free_table( table );
778
779 return r;
780 }
781
782 static UINT get_table( MSIDATABASE *db, LPCWSTR name, MSITABLE **table_ret )
783 {
784 MSITABLE *table;
785 UINT r;
786
787 /* first, see if the table is cached */
788 table = find_cached_table( db, name );
789 if( table )
790 {
791 *table_ret = table;
792 return ERROR_SUCCESS;
793 }
794
795 /* nonexistent tables should be interpreted as empty tables */
796 table = msi_alloc( sizeof (MSITABLE) + lstrlenW(name)*sizeof (WCHAR) );
797 if( !table )
798 return ERROR_FUNCTION_FAILED;
799
800 table->row_count = 0;
801 table->data = NULL;
802 table->data_persistent = NULL;
803 table->colinfo = NULL;
804 table->col_count = 0;
805 table->persistent = MSICONDITION_TRUE;
806 lstrcpyW( table->name, name );
807
808 if ( !lstrcmpW(name, szTables) || !lstrcmpW(name, szColumns) )
809 table->persistent = MSICONDITION_NONE;
810
811 r = table_get_column_info( db, name, &table->colinfo, &table->col_count);
812 if (r != ERROR_SUCCESS)
813 {
814 free_table ( table );
815 return r;
816 }
817
818 r = read_table_from_storage( db, table, db->storage );
819 if( r != ERROR_SUCCESS )
820 {
821 free_table( table );
822 return r;
823 }
824
825 list_add_head( &db->tables, &table->entry );
826 *table_ret = table;
827 return ERROR_SUCCESS;
828 }
829
830 static UINT save_table( MSIDATABASE *db, const MSITABLE *t )
831 {
832 BYTE *rawdata = NULL, *p;
833 UINT rawsize, r, i, j, row_size;
834
835 /* Nothing to do for non-persistent tables */
836 if( t->persistent == MSICONDITION_FALSE )
837 return ERROR_SUCCESS;
838
839 TRACE("Saving %s\n", debugstr_w( t->name ) );
840
841 row_size = msi_table_get_row_size( db, t->colinfo, t->col_count );
842
843 rawsize = t->row_count * row_size;
844 rawdata = msi_alloc_zero( rawsize );
845 if( !rawdata )
846 {
847 r = ERROR_NOT_ENOUGH_MEMORY;
848 goto err;
849 }
850
851 rawsize = 0;
852 p = rawdata;
853 for( i=0; i<t->col_count; i++ )
854 {
855 for( j=0; j<t->row_count; j++ )
856 {
857 UINT offset = t->colinfo[i].offset;
858
859 if (!t->data_persistent[j]) continue;
860 if (i == 0)
861 rawsize += row_size;
862
863 *p++ = t->data[j][offset];
864 *p++ = t->data[j][offset + 1];
865 if( 4 == bytes_per_column( db, &t->colinfo[i] ) )
866 {
867 *p++ = t->data[j][offset + 2];
868 *p++ = t->data[j][offset + 3];
869 }
870 }
871 }
872
873 TRACE("writing %d bytes\n", rawsize);
874 r = write_stream_data( db->storage, t->name, rawdata, rawsize, TRUE );
875
876 err:
877 msi_free( rawdata );
878
879 return r;
880 }
881
882 static void table_calc_column_offsets( MSIDATABASE *db, MSICOLUMNINFO *colinfo,
883 DWORD count )
884 {
885 DWORD i;
886
887 for( i=0; colinfo && (i<count); i++ )
888 {
889 assert( (i+1) == colinfo[ i ].number );
890 if (i)
891 colinfo[i].offset = colinfo[ i - 1 ].offset
892 + bytes_per_column( db, &colinfo[ i - 1 ] );
893 else
894 colinfo[i].offset = 0;
895 TRACE("column %d is [%s] with type %08x ofs %d\n",
896 colinfo[i].number, debugstr_w(colinfo[i].colname),
897 colinfo[i].type, colinfo[i].offset);
898 }
899 }
900
901 static UINT get_defaulttablecolumns( MSIDATABASE *db, LPCWSTR name,
902 MSICOLUMNINFO *colinfo, UINT *sz)
903 {
904 const MSICOLUMNINFO *p;
905 DWORD i, n;
906
907 TRACE("%s\n", debugstr_w(name));
908
909 if (!lstrcmpW( name, szTables ))
910 {
911 p = _Tables_cols;
912 n = 1;
913 }
914 else if (!lstrcmpW( name, szColumns ))
915 {
916 p = _Columns_cols;
917 n = 4;
918 }
919 else
920 return ERROR_FUNCTION_FAILED;
921
922 /* duplicate the string data so we can free it in msi_free_colinfo */
923 for (i=0; i<n; i++)
924 {
925 if (colinfo && (i < *sz) )
926 {
927 colinfo[i] = p[i];
928 colinfo[i].tablename = strdupW( p[i].tablename );
929 colinfo[i].colname = strdupW( p[i].colname );
930 }
931 if( colinfo && (i >= *sz) )
932 break;
933 }
934 table_calc_column_offsets( db, colinfo, n );
935 *sz = n;
936 return ERROR_SUCCESS;
937 }
938
939 static void msi_free_colinfo( MSICOLUMNINFO *colinfo, UINT count )
940 {
941 UINT i;
942
943 for( i=0; i<count; i++ )
944 {
945 msi_free( colinfo[i].tablename );
946 msi_free( colinfo[i].colname );
947 msi_free( colinfo[i].hash_table );
948 }
949 }
950
951 static LPWSTR msi_makestring( const MSIDATABASE *db, UINT stringid)
952 {
953 return strdupW(msi_string_lookup_id( db->strings, stringid ));
954 }
955
956 static UINT read_table_int(BYTE *const *data, UINT row, UINT col, UINT bytes)
957 {
958 UINT ret = 0, i;
959
960 for (i = 0; i < bytes; i++)
961 ret += (data[row][col + i] << i * 8);
962
963 return ret;
964 }
965
966 static UINT get_tablecolumns( MSIDATABASE *db,
967 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz)
968 {
969 UINT r, i, n=0, table_id, count, maxcount = *sz;
970 MSITABLE *table = NULL;
971
972 TRACE("%s\n", debugstr_w(szTableName));
973
974 /* first check if there is a default table with that name */
975 r = get_defaulttablecolumns( db, szTableName, colinfo, sz );
976 if( ( r == ERROR_SUCCESS ) && *sz )
977 return r;
978
979 r = get_table( db, szColumns, &table );
980 if( r != ERROR_SUCCESS )
981 {
982 ERR("couldn't load _Columns table\n");
983 return ERROR_FUNCTION_FAILED;
984 }
985
986 /* convert table and column names to IDs from the string table */
987 r = msi_string2idW( db->strings, szTableName, &table_id );
988 if( r != ERROR_SUCCESS )
989 {
990 WARN("Couldn't find id for %s\n", debugstr_w(szTableName));
991 return r;
992 }
993
994 TRACE("Table id is %d, row count is %d\n", table_id, table->row_count);
995
996 /* Note: _Columns table doesn't have non-persistent data */
997
998 /* if maxcount is non-zero, assume it's exactly right for this table */
999 memset( colinfo, 0, maxcount*sizeof(*colinfo) );
1000 count = table->row_count;
1001 for( i=0; i<count; i++ )
1002 {
1003 if( read_table_int(table->data, i, 0, db->bytes_per_strref) != table_id )
1004 continue;
1005 if( colinfo )
1006 {
1007 UINT id = read_table_int(table->data, i, table->colinfo[2].offset, db->bytes_per_strref);
1008 UINT col = read_table_int(table->data, i, table->colinfo[1].offset, sizeof(USHORT)) - (1<<15);
1009
1010 /* check the column number is in range */
1011 if (col<1 || col>maxcount)
1012 {
1013 ERR("column %d out of range\n", col);
1014 continue;
1015 }
1016
1017 /* check if this column was already set */
1018 if (colinfo[ col - 1 ].number)
1019 {
1020 ERR("duplicate column %d\n", col);
1021 continue;
1022 }
1023
1024 colinfo[ col - 1 ].tablename = msi_makestring( db, table_id );
1025 colinfo[ col - 1 ].number = col;
1026 colinfo[ col - 1 ].colname = msi_makestring( db, id );
1027 colinfo[ col - 1 ].type = read_table_int(table->data, i,
1028 table->colinfo[3].offset,
1029 sizeof(USHORT)) - (1<<15);
1030 colinfo[ col - 1 ].offset = 0;
1031 colinfo[ col - 1 ].ref_count = 0;
1032 colinfo[ col - 1 ].hash_table = NULL;
1033 }
1034 n++;
1035 }
1036
1037 TRACE("%s has %d columns\n", debugstr_w(szTableName), n);
1038
1039 if (colinfo && n != maxcount)
1040 {
1041 ERR("missing column in table %s\n", debugstr_w(szTableName));
1042 msi_free_colinfo(colinfo, maxcount );
1043 return ERROR_FUNCTION_FAILED;
1044 }
1045
1046 table_calc_column_offsets( db, colinfo, n );
1047 *sz = n;
1048
1049 return ERROR_SUCCESS;
1050 }
1051
1052 static void msi_update_table_columns( MSIDATABASE *db, LPCWSTR name )
1053 {
1054 MSITABLE *table;
1055 UINT size, offset, old_count;
1056 UINT n;
1057
1058 table = find_cached_table( db, name );
1059 old_count = table->col_count;
1060 msi_free( table->colinfo );
1061 table->colinfo = NULL;
1062
1063 table_get_column_info( db, name, &table->colinfo, &table->col_count );
1064 if (!table->col_count)
1065 return;
1066
1067 size = msi_table_get_row_size( db, table->colinfo, table->col_count );
1068 offset = table->colinfo[table->col_count - 1].offset;
1069
1070 for ( n = 0; n < table->row_count; n++ )
1071 {
1072 table->data[n] = msi_realloc( table->data[n], size );
1073 if (old_count < table->col_count)
1074 memset( &table->data[n][offset], 0, size - offset );
1075 }
1076 }
1077
1078 /* try to find the table name in the _Tables table */
1079 BOOL TABLE_Exists( MSIDATABASE *db, LPCWSTR name )
1080 {
1081 UINT r, table_id = 0, i, count;
1082 MSITABLE *table = NULL;
1083
1084 static const WCHAR szStreams[] = {'_','S','t','r','e','a','m','s',0};
1085 static const WCHAR szStorages[] = {'_','S','t','o','r','a','g','e','s',0};
1086
1087 if( !lstrcmpW( name, szTables ) || !lstrcmpW( name, szColumns ) ||
1088 !lstrcmpW( name, szStreams ) || !lstrcmpW( name, szStorages ) )
1089 return TRUE;
1090
1091 r = msi_string2idW( db->strings, name, &table_id );
1092 if( r != ERROR_SUCCESS )
1093 {
1094 TRACE("Couldn't find id for %s\n", debugstr_w(name));
1095 return FALSE;
1096 }
1097
1098 r = get_table( db, szTables, &table );
1099 if( r != ERROR_SUCCESS )
1100 {
1101 ERR("table %s not available\n", debugstr_w(szTables));
1102 return FALSE;
1103 }
1104
1105 count = table->row_count;
1106 for( i=0; i<count; i++ )
1107 if( table->data[ i ][ 0 ] == table_id )
1108 return TRUE;
1109
1110 return FALSE;
1111 }
1112
1113 /* below is the query interface to a table */
1114
1115 typedef struct tagMSITABLEVIEW
1116 {
1117 MSIVIEW view;
1118 MSIDATABASE *db;
1119 MSITABLE *table;
1120 MSICOLUMNINFO *columns;
1121 MSIORDERINFO *order;
1122 UINT num_cols;
1123 UINT row_size;
1124 WCHAR name[1];
1125 } MSITABLEVIEW;
1126
1127 static UINT TABLE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
1128 {
1129 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1130 UINT offset, n;
1131
1132 if( !tv->table )
1133 return ERROR_INVALID_PARAMETER;
1134
1135 if( (col==0) || (col>tv->num_cols) )
1136 return ERROR_INVALID_PARAMETER;
1137
1138 /* how many rows are there ? */
1139 if( row >= tv->table->row_count )
1140 return ERROR_NO_MORE_ITEMS;
1141
1142 if( tv->columns[col-1].offset >= tv->row_size )
1143 {
1144 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1145 ERR("%p %p\n", tv, tv->columns );
1146 return ERROR_FUNCTION_FAILED;
1147 }
1148
1149 if (tv->order)
1150 row = tv->order->reorder[row];
1151
1152 n = bytes_per_column( tv->db, &tv->columns[col-1] );
1153 if (n != 2 && n != 3 && n != 4)
1154 {
1155 ERR("oops! what is %d bytes per column?\n", n );
1156 return ERROR_FUNCTION_FAILED;
1157 }
1158
1159 offset = tv->columns[col-1].offset;
1160 *val = read_table_int(tv->table->data, row, offset, n);
1161
1162 /* TRACE("Data [%d][%d] = %d\n", row, col, *val ); */
1163
1164 return ERROR_SUCCESS;
1165 }
1166
1167 static UINT msi_stream_name( const MSITABLEVIEW *tv, UINT row, LPWSTR *pstname )
1168 {
1169 LPWSTR p, stname = NULL;
1170 UINT i, r, type, ival;
1171 DWORD len;
1172 LPCWSTR sval;
1173 MSIVIEW *view = (MSIVIEW *) tv;
1174
1175 TRACE("%p %d\n", tv, row);
1176
1177 len = lstrlenW( tv->name ) + 1;
1178 stname = msi_alloc( len*sizeof(WCHAR) );
1179 if ( !stname )
1180 {
1181 r = ERROR_OUTOFMEMORY;
1182 goto err;
1183 }
1184
1185 lstrcpyW( stname, tv->name );
1186
1187 for ( i = 0; i < tv->num_cols; i++ )
1188 {
1189 type = tv->columns[i].type;
1190 if ( type & MSITYPE_KEY )
1191 {
1192 static const WCHAR szDot[] = { '.', 0 };
1193
1194 r = TABLE_fetch_int( view, row, i+1, &ival );
1195 if ( r != ERROR_SUCCESS )
1196 goto err;
1197
1198 if ( tv->columns[i].type & MSITYPE_STRING )
1199 {
1200 sval = msi_string_lookup_id( tv->db->strings, ival );
1201 if ( !sval )
1202 {
1203 r = ERROR_INVALID_PARAMETER;
1204 goto err;
1205 }
1206 }
1207 else
1208 {
1209 static const WCHAR fmt[] = { '%','d',0 };
1210 WCHAR number[0x20];
1211 UINT n = bytes_per_column( tv->db, &tv->columns[i] );
1212
1213 switch( n )
1214 {
1215 case 2:
1216 sprintfW( number, fmt, ival^0x8000 );
1217 break;
1218 case 4:
1219 sprintfW( number, fmt, ival^0x80000000 );
1220 break;
1221 default:
1222 ERR( "oops - unknown column width %d\n", n );
1223 r = ERROR_FUNCTION_FAILED;
1224 goto err;
1225 }
1226 sval = number;
1227 }
1228
1229 len += lstrlenW( szDot ) + lstrlenW( sval );
1230 p = msi_realloc ( stname, len*sizeof(WCHAR) );
1231 if ( !p )
1232 {
1233 r = ERROR_OUTOFMEMORY;
1234 goto err;
1235 }
1236 stname = p;
1237
1238 lstrcatW( stname, szDot );
1239 lstrcatW( stname, sval );
1240 }
1241 else
1242 continue;
1243 }
1244
1245 *pstname = stname;
1246 return ERROR_SUCCESS;
1247
1248 err:
1249 msi_free( stname );
1250 *pstname = NULL;
1251 return r;
1252 }
1253
1254 /*
1255 * We need a special case for streams, as we need to reference column with
1256 * the name of the stream in the same table, and the table name
1257 * which may not be available at higher levels of the query
1258 */
1259 static UINT TABLE_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm )
1260 {
1261 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1262 UINT r;
1263 LPWSTR full_name = NULL;
1264
1265 if( !view->ops->fetch_int )
1266 return ERROR_INVALID_PARAMETER;
1267
1268 r = msi_stream_name( tv, row, &full_name );
1269 if ( r != ERROR_SUCCESS )
1270 {
1271 ERR("fetching stream, error = %d\n", r);
1272 return r;
1273 }
1274
1275 r = db_get_raw_stream( tv->db, full_name, stm );
1276 if( r )
1277 ERR("fetching stream %s, error = %d\n",debugstr_w(full_name), r);
1278 msi_free( full_name );
1279
1280 return r;
1281 }
1282
1283 static UINT TABLE_set_int( MSITABLEVIEW *tv, UINT row, UINT col, UINT val )
1284 {
1285 UINT offset, n, i;
1286
1287 if( !tv->table )
1288 return ERROR_INVALID_PARAMETER;
1289
1290 if( (col==0) || (col>tv->num_cols) )
1291 return ERROR_INVALID_PARAMETER;
1292
1293 if( row >= tv->table->row_count )
1294 return ERROR_INVALID_PARAMETER;
1295
1296 if( tv->columns[col-1].offset >= tv->row_size )
1297 {
1298 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1299 ERR("%p %p\n", tv, tv->columns );
1300 return ERROR_FUNCTION_FAILED;
1301 }
1302
1303 msi_free( tv->columns[col-1].hash_table );
1304 tv->columns[col-1].hash_table = NULL;
1305
1306 n = bytes_per_column( tv->db, &tv->columns[col-1] );
1307 if ( n != 2 && n != 3 && n != 4 )
1308 {
1309 ERR("oops! what is %d bytes per column?\n", n );
1310 return ERROR_FUNCTION_FAILED;
1311 }
1312
1313 offset = tv->columns[col-1].offset;
1314 for ( i = 0; i < n; i++ )
1315 tv->table->data[row][offset + i] = (val >> i * 8) & 0xff;
1316
1317 return ERROR_SUCCESS;
1318 }
1319
1320 static UINT TABLE_get_row( struct tagMSIVIEW *view, UINT row, MSIRECORD **rec )
1321 {
1322 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1323
1324 if (!tv->table)
1325 return ERROR_INVALID_PARAMETER;
1326
1327 if (tv->order)
1328 row = tv->order->reorder[row];
1329
1330 return msi_view_get_row(tv->db, view, row, rec);
1331 }
1332
1333 static UINT msi_addstreamW( MSIDATABASE *db, LPCWSTR name, IStream *data )
1334 {
1335 UINT r;
1336 MSIQUERY *query = NULL;
1337 MSIRECORD *rec = NULL;
1338
1339 static const WCHAR insert[] = {
1340 'I','N','S','E','R','T',' ','I','N','T','O',' ',
1341 '`','_','S','t','r','e','a','m','s','`',' ',
1342 '(','`','N','a','m','e','`',',',
1343 '`','D','a','t','a','`',')',' ',
1344 'V','A','L','U','E','S',' ','(','?',',','?',')',0};
1345
1346 TRACE("%p %s %p\n", db, debugstr_w(name), data);
1347
1348 rec = MSI_CreateRecord( 2 );
1349 if ( !rec )
1350 return ERROR_OUTOFMEMORY;
1351
1352 r = MSI_RecordSetStringW( rec, 1, name );
1353 if ( r != ERROR_SUCCESS )
1354 goto err;
1355
1356 r = MSI_RecordSetIStream( rec, 2, data );
1357 if ( r != ERROR_SUCCESS )
1358 goto err;
1359
1360 r = MSI_DatabaseOpenViewW( db, insert, &query );
1361 if ( r != ERROR_SUCCESS )
1362 goto err;
1363
1364 r = MSI_ViewExecute( query, rec );
1365
1366 err:
1367 msiobj_release( &query->hdr );
1368 msiobj_release( &rec->hdr );
1369
1370 return r;
1371 }
1372
1373 static UINT TABLE_set_row( struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask )
1374 {
1375 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1376 UINT i, val, r = ERROR_SUCCESS;
1377
1378 if ( !tv->table )
1379 return ERROR_INVALID_PARAMETER;
1380
1381 /* test if any of the mask bits are invalid */
1382 if ( mask >= (1<<tv->num_cols) )
1383 return ERROR_INVALID_PARAMETER;
1384
1385 for ( i = 0; i < tv->num_cols; i++ )
1386 {
1387 BOOL persistent;
1388
1389 /* only update the fields specified in the mask */
1390 if ( !(mask&(1<<i)) )
1391 continue;
1392
1393 persistent = (tv->table->persistent != MSICONDITION_FALSE) &&
1394 (tv->table->data_persistent[row]);
1395 /* FIXME: should we allow updating keys? */
1396
1397 val = 0;
1398 if ( !MSI_RecordIsNull( rec, i + 1 ) )
1399 {
1400 if ( MSITYPE_IS_BINARY(tv->columns[ i ].type) )
1401 {
1402 IStream *stm;
1403 LPWSTR stname;
1404
1405 r = MSI_RecordGetIStream( rec, i + 1, &stm );
1406 if ( r != ERROR_SUCCESS )
1407 return r;
1408
1409 r = msi_stream_name( tv, row, &stname );
1410 if ( r != ERROR_SUCCESS )
1411 {
1412 IStream_Release( stm );
1413 return r;
1414 }
1415
1416 r = msi_addstreamW( tv->db, stname, stm );
1417 IStream_Release( stm );
1418 msi_free ( stname );
1419
1420 if ( r != ERROR_SUCCESS )
1421 return r;
1422
1423 val = 1; /* refers to the first key column */
1424 }
1425 else if ( tv->columns[i].type & MSITYPE_STRING )
1426 {
1427 LPCWSTR sval = MSI_RecordGetString( rec, i + 1 );
1428 UINT ival, x;
1429
1430 r = msi_string2idW(tv->db->strings, sval, &ival);
1431 if (r == ERROR_SUCCESS)
1432 {
1433 TABLE_fetch_int(&tv->view, row, i + 1, &x);
1434 if (ival == x)
1435 continue;
1436 }
1437
1438 val = msi_addstringW( tv->db->strings, 0, sval, -1, 1,
1439 persistent ? StringPersistent : StringNonPersistent );
1440 }
1441 else if ( 2 == bytes_per_column( tv->db, &tv->columns[ i ] ) )
1442 {
1443 val = 0x8000 + MSI_RecordGetInteger( rec, i + 1 );
1444 if ( val & 0xffff0000 )
1445 {
1446 ERR("field %u value %d out of range\n", i+1, val - 0x8000 );
1447 return ERROR_FUNCTION_FAILED;
1448 }
1449 }
1450 else
1451 {
1452 INT ival = MSI_RecordGetInteger( rec, i + 1 );
1453 val = ival ^ 0x80000000;
1454 }
1455 }
1456
1457 r = TABLE_set_int( tv, row, i+1, val );
1458 if ( r != ERROR_SUCCESS )
1459 break;
1460 }
1461 return r;
1462 }
1463
1464 static UINT table_create_new_row( struct tagMSIVIEW *view, UINT *num, BOOL temporary )
1465 {
1466 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1467 BYTE **p, *row;
1468 BOOL *b;
1469 UINT sz;
1470 BYTE ***data_ptr;
1471 BOOL **data_persist_ptr;
1472 UINT *row_count;
1473
1474 TRACE("%p %s\n", view, temporary ? "TRUE" : "FALSE");
1475
1476 if( !tv->table )
1477 return ERROR_INVALID_PARAMETER;
1478
1479 row = msi_alloc_zero( tv->row_size );
1480 if( !row )
1481 return ERROR_NOT_ENOUGH_MEMORY;
1482
1483 row_count = &tv->table->row_count;
1484 data_ptr = &tv->table->data;
1485 data_persist_ptr = &tv->table->data_persistent;
1486 if (*num == -1)
1487 *num = tv->table->row_count;
1488
1489 sz = (*row_count + 1) * sizeof (BYTE*);
1490 if( *data_ptr )
1491 p = msi_realloc( *data_ptr, sz );
1492 else
1493 p = msi_alloc( sz );
1494 if( !p )
1495 {
1496 msi_free( row );
1497 return ERROR_NOT_ENOUGH_MEMORY;
1498 }
1499
1500 sz = (*row_count + 1) * sizeof (BOOL);
1501 if( *data_persist_ptr )
1502 b = msi_realloc( *data_persist_ptr, sz );
1503 else
1504 b = msi_alloc( sz );
1505 if( !b )
1506 {
1507 msi_free( row );
1508 msi_free( p );
1509 return ERROR_NOT_ENOUGH_MEMORY;
1510 }
1511
1512 *data_ptr = p;
1513 (*data_ptr)[*row_count] = row;
1514
1515 *data_persist_ptr = b;
1516 (*data_persist_ptr)[*row_count] = !temporary;
1517
1518 (*row_count)++;
1519
1520 return ERROR_SUCCESS;
1521 }
1522
1523 static UINT TABLE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
1524 {
1525 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1526
1527 TRACE("%p %p\n", tv, record);
1528
1529 TRACE("There are %d columns\n", tv->num_cols );
1530
1531 return ERROR_SUCCESS;
1532 }
1533
1534 static UINT TABLE_close( struct tagMSIVIEW *view )
1535 {
1536 TRACE("%p\n", view );
1537
1538 return ERROR_SUCCESS;
1539 }
1540
1541 static UINT TABLE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols)
1542 {
1543 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1544
1545 TRACE("%p %p %p\n", view, rows, cols );
1546
1547 if( cols )
1548 *cols = tv->num_cols;
1549 if( rows )
1550 {
1551 if( !tv->table )
1552 return ERROR_INVALID_PARAMETER;
1553 *rows = tv->table->row_count;
1554 }
1555
1556 return ERROR_SUCCESS;
1557 }
1558
1559 static UINT TABLE_get_column_info( struct tagMSIVIEW *view,
1560 UINT n, LPWSTR *name, UINT *type, BOOL *temporary )
1561 {
1562 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1563
1564 TRACE("%p %d %p %p\n", tv, n, name, type );
1565
1566 if( ( n == 0 ) || ( n > tv->num_cols ) )
1567 return ERROR_INVALID_PARAMETER;
1568
1569 if( name )
1570 {
1571 *name = strdupW( tv->columns[n-1].colname );
1572 if( !*name )
1573 return ERROR_FUNCTION_FAILED;
1574 }
1575
1576 if( type )
1577 *type = tv->columns[n-1].type;
1578
1579 if( temporary )
1580 *temporary = tv->columns[n-1].temporary;
1581
1582 return ERROR_SUCCESS;
1583 }
1584
1585 static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row );
1586
1587 static UINT table_validate_new( MSITABLEVIEW *tv, MSIRECORD *rec )
1588 {
1589 UINT r, row, i;
1590
1591 /* check there's no null values where they're not allowed */
1592 for( i = 0; i < tv->num_cols; i++ )
1593 {
1594 if ( tv->columns[i].type & MSITYPE_NULLABLE )
1595 continue;
1596
1597 if ( MSITYPE_IS_BINARY(tv->columns[i].type) )
1598 TRACE("skipping binary column\n");
1599 else if ( tv->columns[i].type & MSITYPE_STRING )
1600 {
1601 LPCWSTR str;
1602
1603 str = MSI_RecordGetString( rec, i+1 );
1604 if (str == NULL || str[0] == 0)
1605 return ERROR_INVALID_DATA;
1606 }
1607 else
1608 {
1609 UINT n;
1610
1611 n = MSI_RecordGetInteger( rec, i+1 );
1612 if (n == MSI_NULL_INTEGER)
1613 return ERROR_INVALID_DATA;
1614 }
1615 }
1616
1617 /* check there's no duplicate keys */
1618 r = msi_table_find_row( tv, rec, &row );
1619 if (r == ERROR_SUCCESS)
1620 return ERROR_FUNCTION_FAILED;
1621
1622 return ERROR_SUCCESS;
1623 }
1624
1625 static UINT TABLE_insert_row( struct tagMSIVIEW *view, MSIRECORD *rec, UINT row, BOOL temporary )
1626 {
1627 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1628 UINT i, r;
1629
1630 TRACE("%p %p %s\n", tv, rec, temporary ? "TRUE" : "FALSE" );
1631
1632 /* check that the key is unique - can we find a matching row? */
1633 r = table_validate_new( tv, rec );
1634 if( r != ERROR_SUCCESS )
1635 return ERROR_FUNCTION_FAILED;
1636
1637 r = table_create_new_row( view, &row, temporary );
1638 TRACE("insert_row returned %08x\n", r);
1639 if( r != ERROR_SUCCESS )
1640 return r;
1641
1642 /* shift the rows to make room for the new row */
1643 for (i = tv->table->row_count - 1; i > row; i--)
1644 {
1645 memmove(&(tv->table->data[i][0]),
1646 &(tv->table->data[i - 1][0]), tv->row_size);
1647 tv->table->data_persistent[i] = tv->table->data_persistent[i - 1];
1648 }
1649
1650 /* Re-set the persistence flag */
1651 tv->table->data_persistent[row] = !temporary;
1652 return TABLE_set_row( view, row, rec, (1<<tv->num_cols) - 1 );
1653 }
1654
1655 static UINT TABLE_delete_row( struct tagMSIVIEW *view, UINT row )
1656 {
1657 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1658 UINT r, num_rows, num_cols, i;
1659
1660 TRACE("%p %d\n", tv, row);
1661
1662 if ( !tv->table )
1663 return ERROR_INVALID_PARAMETER;
1664
1665 r = TABLE_get_dimensions( view, &num_rows, &num_cols );
1666 if ( r != ERROR_SUCCESS )
1667 return r;
1668
1669 if ( row >= num_rows )
1670 return ERROR_FUNCTION_FAILED;
1671
1672 num_rows = tv->table->row_count;
1673 tv->table->row_count--;
1674
1675 /* reset the hash tables */
1676 for (i = 0; i < tv->num_cols; i++)
1677 {
1678 msi_free( tv->columns[i].hash_table );
1679 tv->columns[i].hash_table = NULL;
1680 }
1681
1682 if ( row == num_rows - 1 )
1683 return ERROR_SUCCESS;
1684
1685 for (i = row + 1; i < num_rows; i++)
1686 {
1687 memcpy(tv->table->data[i - 1], tv->table->data[i], tv->row_size);
1688 tv->table->data_persistent[i - 1] = tv->table->data_persistent[i];
1689 }
1690
1691 return ERROR_SUCCESS;
1692 }
1693
1694 static UINT msi_table_update(struct tagMSIVIEW *view, MSIRECORD *rec, UINT row)
1695 {
1696 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1697 UINT r, new_row;
1698
1699 /* FIXME: MsiViewFetch should set rec index 0 to some ID that
1700 * sets the fetched record apart from other records
1701 */
1702
1703 if (!tv->table)
1704 return ERROR_INVALID_PARAMETER;
1705
1706 r = msi_table_find_row(tv, rec, &new_row);
1707 if (r != ERROR_SUCCESS)
1708 {
1709 ERR("can't find row to modify\n");
1710 return ERROR_FUNCTION_FAILED;
1711 }
1712
1713 /* the row cannot be changed */
1714 if (row != new_row + 1)
1715 return ERROR_FUNCTION_FAILED;
1716
1717 return TABLE_set_row(view, new_row, rec, (1 << tv->num_cols) - 1);
1718 }
1719
1720 static UINT msi_table_assign(struct tagMSIVIEW *view, MSIRECORD *rec)
1721 {
1722 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1723 UINT r, row;
1724
1725 if (!tv->table)
1726 return ERROR_INVALID_PARAMETER;
1727
1728 r = msi_table_find_row(tv, rec, &row);
1729 if (r == ERROR_SUCCESS)
1730 return TABLE_set_row(view, row, rec, (1 << tv->num_cols) - 1);
1731 else
1732 return TABLE_insert_row( view, rec, -1, FALSE );
1733 }
1734
1735 static UINT modify_delete_row( struct tagMSIVIEW *view, MSIRECORD *rec )
1736 {
1737 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1738 UINT row, r;
1739
1740 r = msi_table_find_row(tv, rec, &row);
1741 if (r != ERROR_SUCCESS)
1742 return r;
1743
1744 return TABLE_delete_row(view, row);
1745 }
1746
1747 static UINT msi_refresh_record( struct tagMSIVIEW *view, MSIRECORD *rec, UINT row )
1748 {
1749 MSIRECORD *curr;
1750 UINT r, i, count;
1751
1752 r = TABLE_get_row(view, row - 1, &curr);
1753 if (r != ERROR_SUCCESS)
1754 return r;
1755
1756 count = MSI_RecordGetFieldCount(rec);
1757 for (i = 0; i < count; i++)
1758 MSI_RecordCopyField(curr, i + 1, rec, i + 1);
1759
1760 msiobj_release(&curr->hdr);
1761 return ERROR_SUCCESS;
1762 }
1763
1764 static UINT TABLE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
1765 MSIRECORD *rec, UINT row)
1766 {
1767 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1768 UINT r;
1769
1770 TRACE("%p %d %p\n", view, eModifyMode, rec );
1771
1772 switch (eModifyMode)
1773 {
1774 case MSIMODIFY_DELETE:
1775 r = modify_delete_row( view, rec );
1776 break;
1777 case MSIMODIFY_VALIDATE_NEW:
1778 r = table_validate_new( tv, rec );
1779 break;
1780
1781 case MSIMODIFY_INSERT:
1782 r = table_validate_new( tv, rec );
1783 if (r != ERROR_SUCCESS)
1784 break;
1785 r = TABLE_insert_row( view, rec, -1, FALSE );
1786 break;
1787
1788 case MSIMODIFY_INSERT_TEMPORARY:
1789 r = table_validate_new( tv, rec );
1790 if (r != ERROR_SUCCESS)
1791 break;
1792 r = TABLE_insert_row( view, rec, -1, TRUE );
1793 break;
1794
1795 case MSIMODIFY_REFRESH:
1796 r = msi_refresh_record( view, rec, row );
1797 break;
1798
1799 case MSIMODIFY_UPDATE:
1800 r = msi_table_update( view, rec, row );
1801 break;
1802
1803 case MSIMODIFY_ASSIGN:
1804 r = msi_table_assign( view, rec );
1805 break;
1806
1807 case MSIMODIFY_REPLACE:
1808 case MSIMODIFY_MERGE:
1809 case MSIMODIFY_VALIDATE:
1810 case MSIMODIFY_VALIDATE_FIELD:
1811 case MSIMODIFY_VALIDATE_DELETE:
1812 FIXME("%p %d %p - mode not implemented\n", view, eModifyMode, rec );
1813 r = ERROR_CALL_NOT_IMPLEMENTED;
1814 break;
1815
1816 default:
1817 r = ERROR_INVALID_DATA;
1818 }
1819
1820 return r;
1821 }
1822
1823 static UINT TABLE_delete( struct tagMSIVIEW *view )
1824 {
1825 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1826
1827 TRACE("%p\n", view );
1828
1829 tv->table = NULL;
1830 tv->columns = NULL;
1831
1832 if (tv->order)
1833 {
1834 msi_free( tv->order->reorder );
1835 msi_free( tv->order );
1836 tv->order = NULL;
1837 }
1838
1839 msi_free( tv );
1840
1841 return ERROR_SUCCESS;
1842 }
1843
1844 static UINT TABLE_find_matching_rows( struct tagMSIVIEW *view, UINT col,
1845 UINT val, UINT *row, MSIITERHANDLE *handle )
1846 {
1847 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1848 const MSICOLUMNHASHENTRY *entry;
1849
1850 TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
1851
1852 if( !tv->table )
1853 return ERROR_INVALID_PARAMETER;
1854
1855 if( (col==0) || (col > tv->num_cols) )
1856 return ERROR_INVALID_PARAMETER;
1857
1858 if( !tv->columns[col-1].hash_table )
1859 {
1860 UINT i;
1861 UINT num_rows = tv->table->row_count;
1862 MSICOLUMNHASHENTRY **hash_table;
1863 MSICOLUMNHASHENTRY *new_entry;
1864
1865 if( tv->columns[col-1].offset >= tv->row_size )
1866 {
1867 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1868 ERR("%p %p\n", tv, tv->columns );
1869 return ERROR_FUNCTION_FAILED;
1870 }
1871
1872 /* allocate contiguous memory for the table and its entries so we
1873 * don't have to do an expensive cleanup */
1874 hash_table = msi_alloc(MSITABLE_HASH_TABLE_SIZE * sizeof(MSICOLUMNHASHENTRY*) +
1875 num_rows * sizeof(MSICOLUMNHASHENTRY));
1876 if (!hash_table)
1877 return ERROR_OUTOFMEMORY;
1878
1879 memset(hash_table, 0, MSITABLE_HASH_TABLE_SIZE * sizeof(MSICOLUMNHASHENTRY*));
1880 tv->columns[col-1].hash_table = hash_table;
1881
1882 new_entry = (MSICOLUMNHASHENTRY *)(hash_table + MSITABLE_HASH_TABLE_SIZE);
1883
1884 for (i = 0; i < num_rows; i++, new_entry++)
1885 {
1886 UINT row_value;
1887
1888 if (view->ops->fetch_int( view, i, col, &row_value ) != ERROR_SUCCESS)
1889 continue;
1890
1891 new_entry->next = NULL;
1892 new_entry->value = row_value;
1893 new_entry->row = i;
1894 if (hash_table[row_value % MSITABLE_HASH_TABLE_SIZE])
1895 {
1896 MSICOLUMNHASHENTRY *prev_entry = hash_table[row_value % MSITABLE_HASH_TABLE_SIZE];
1897 while (prev_entry->next)
1898 prev_entry = prev_entry->next;
1899 prev_entry->next = new_entry;
1900 }
1901 else
1902 hash_table[row_value % MSITABLE_HASH_TABLE_SIZE] = new_entry;
1903 }
1904 }
1905
1906 if( !*handle )
1907 entry = tv->columns[col-1].hash_table[val % MSITABLE_HASH_TABLE_SIZE];
1908 else
1909 entry = (*handle)->next;
1910
1911 while (entry && entry->value != val)
1912 entry = entry->next;
1913
1914 *handle = entry;
1915 if (!entry)
1916 return ERROR_NO_MORE_ITEMS;
1917
1918 *row = entry->row;
1919
1920 return ERROR_SUCCESS;
1921 }
1922
1923 static UINT TABLE_add_ref(struct tagMSIVIEW *view)
1924 {
1925 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1926 UINT i;
1927
1928 TRACE("%p %d\n", view, tv->table->ref_count);
1929
1930 for (i = 0; i < tv->table->col_count; i++)
1931 {
1932 if (tv->table->colinfo[i].type & MSITYPE_TEMPORARY)
1933 InterlockedIncrement(&tv->table->colinfo[i].ref_count);
1934 }
1935
1936 return InterlockedIncrement(&tv->table->ref_count);
1937 }
1938
1939 static UINT TABLE_remove_column(struct tagMSIVIEW *view, LPCWSTR table, UINT number)
1940 {
1941 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1942 MSIRECORD *rec = NULL;
1943 MSIVIEW *columns = NULL;
1944 UINT row, r;
1945
1946 rec = MSI_CreateRecord(2);
1947 if (!rec)
1948 return ERROR_OUTOFMEMORY;
1949
1950 MSI_RecordSetStringW(rec, 1, table);
1951 MSI_RecordSetInteger(rec, 2, number);
1952
1953 r = TABLE_CreateView(tv->db, szColumns, &columns);
1954 if (r != ERROR_SUCCESS)
1955 return r;
1956
1957 r = msi_table_find_row((MSITABLEVIEW *)columns, rec, &row);
1958 if (r != ERROR_SUCCESS)
1959 goto done;
1960
1961 r = TABLE_delete_row(columns, row);
1962 if (r != ERROR_SUCCESS)
1963 goto done;
1964
1965 msi_update_table_columns(tv->db, table);
1966
1967 done:
1968 msiobj_release(&rec->hdr);
1969 columns->ops->delete(columns);
1970 return r;
1971 }
1972
1973 static UINT TABLE_release(struct tagMSIVIEW *view)
1974 {
1975 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1976 INT ref = tv->table->ref_count;
1977 UINT i, r;
1978
1979 TRACE("%p %d\n", view, ref);
1980
1981 for (i = 0; i < tv->table->col_count; i++)
1982 {
1983 if (tv->table->colinfo[i].type & MSITYPE_TEMPORARY)
1984 {
1985 ref = InterlockedDecrement(&tv->table->colinfo[i].ref_count);
1986 if (ref == 0)
1987 {
1988 r = TABLE_remove_column(view, tv->table->colinfo[i].tablename,
1989 tv->table->colinfo[i].number);
1990 if (r != ERROR_SUCCESS)
1991 break;
1992 }
1993 }
1994 }
1995
1996 ref = InterlockedDecrement(&tv->table->ref_count);
1997 if (ref == 0)
1998 {
1999 if (!tv->table->row_count)
2000 {
2001 list_remove(&tv->table->entry);
2002 free_table(tv->table);
2003 TABLE_delete(view);
2004 }
2005 }
2006
2007 return ref;
2008 }
2009
2010 static UINT TABLE_add_column(struct tagMSIVIEW *view, LPCWSTR table, UINT number,
2011 LPCWSTR column, UINT type, BOOL hold)
2012 {
2013 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
2014 MSITABLE *msitable;
2015 MSIRECORD *rec;
2016 UINT r, i;
2017
2018 rec = MSI_CreateRecord(4);
2019 if (!rec)
2020 return ERROR_OUTOFMEMORY;
2021
2022 MSI_RecordSetStringW(rec, 1, table);
2023 MSI_RecordSetInteger(rec, 2, number);
2024 MSI_RecordSetStringW(rec, 3, column);
2025 MSI_RecordSetInteger(rec, 4, type);
2026
2027 r = TABLE_insert_row(&tv->view, rec, -1, FALSE);
2028 if (r != ERROR_SUCCESS)
2029 goto done;
2030
2031 msi_update_table_columns(tv->db, table);
2032
2033 if (!hold)
2034 goto done;
2035
2036 msitable = find_cached_table(tv->db, table);
2037 for (i = 0; i < msitable->col_count; i++)
2038 {
2039 if (!lstrcmpW(msitable->colinfo[i].colname, column))
2040 {
2041 InterlockedIncrement(&msitable->colinfo[i].ref_count);
2042 break;
2043 }
2044 }
2045
2046 done:
2047 msiobj_release(&rec->hdr);
2048 return r;
2049 }
2050
2051 static UINT order_add_column(struct tagMSIVIEW *view, MSIORDERINFO *order, LPCWSTR name)
2052 {
2053 UINT n, r, count;
2054
2055 r = TABLE_get_dimensions(view, NULL, &count);
2056 if (r != ERROR_SUCCESS)
2057 return r;
2058
2059 if (order->num_cols >= count)
2060 return ERROR_FUNCTION_FAILED;
2061
2062 r = VIEW_find_column(view, name, &n);
2063 if (r != ERROR_SUCCESS)
2064 return r;
2065
2066 order->cols[order->num_cols] = n;
2067 TRACE("Ordering by column %s (%d)\n", debugstr_w(name), n);
2068
2069 order->num_cols++;
2070
2071 return ERROR_SUCCESS;
2072 }
2073
2074 static UINT order_compare(struct tagMSIVIEW *view, MSIORDERINFO *order,
2075 UINT a, UINT b, UINT *swap)
2076 {
2077 UINT r, i, a_val = 0, b_val = 0;
2078
2079 *swap = 0;
2080 for (i = 0; i < order->num_cols; i++)
2081 {
2082 r = TABLE_fetch_int(view, a, order->cols[i], &a_val);
2083 if (r != ERROR_SUCCESS)
2084 return r;
2085
2086 r = TABLE_fetch_int(view, b, order->cols[i], &b_val);
2087 if (r != ERROR_SUCCESS)
2088 return r;
2089
2090 if (a_val != b_val)
2091 {
2092 if (a_val > b_val)
2093 *swap = 1;
2094 break;
2095 }
2096 }
2097
2098 return ERROR_SUCCESS;
2099 }
2100
2101 static UINT order_mergesort(struct tagMSIVIEW *view, MSIORDERINFO *order,
2102 UINT left, UINT right)
2103 {
2104 UINT r, i, j, temp;
2105 UINT swap = 0, center = (left + right) / 2;
2106 UINT *array = order->reorder;
2107
2108 if (left == right)
2109 return ERROR_SUCCESS;
2110
2111 /* sort the left half */
2112 r = order_mergesort(view, order, left, center);
2113 if (r != ERROR_SUCCESS)
2114 return r;
2115
2116 /* sort the right half */
2117 r = order_mergesort(view, order, center + 1, right);
2118 if (r != ERROR_SUCCESS)
2119 return r;
2120
2121 for (i = left, j = center + 1; (i <= center) && (j <= right); i++)
2122 {
2123 r = order_compare(view, order, array[i], array[j], &swap);
2124 if (r != ERROR_SUCCESS)
2125 return r;
2126
2127 if (swap)
2128 {
2129 temp = array[j];
2130 memmove(&array[i + 1], &array[i], (j - i) * sizeof(UINT));
2131 array[i] = temp;
2132 j++;
2133 center++;
2134 }
2135 }
2136
2137 return ERROR_SUCCESS;
2138 }
2139
2140 static UINT order_verify(struct tagMSIVIEW *view, MSIORDERINFO *order, UINT num_rows)
2141 {
2142 UINT i, swap, r;
2143
2144 for (i = 1; i < num_rows; i++)
2145 {
2146 r = order_compare(view, order, order->reorder[i - 1],
2147 order->reorder[i], &swap);
2148 if (r != ERROR_SUCCESS)
2149 return r;
2150
2151 if (!swap)
2152 continue;
2153
2154 ERR("Bad order! %d\n", i);
2155 return ERROR_FUNCTION_FAILED;
2156 }
2157
2158 return ERROR_SUCCESS;
2159 }
2160
2161 static UINT TABLE_sort(struct tagMSIVIEW *view, column_info *columns)
2162 {
2163 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
2164 MSIORDERINFO *order;
2165 column_info *ptr;
2166 UINT r, i;
2167 UINT rows, cols;
2168
2169 TRACE("sorting table %s\n", debugstr_w(tv->name));
2170
2171 r = TABLE_get_dimensions(view, &rows, &cols);
2172 if (r != ERROR_SUCCESS)
2173 return r;
2174
2175 if (rows == 0)
2176 return ERROR_SUCCESS;
2177
2178 order = msi_alloc_zero(sizeof(MSIORDERINFO) + sizeof(UINT) * cols);
2179 if (!order)
2180 return ERROR_OUTOFMEMORY;
2181
2182 for (ptr = columns; ptr; ptr = ptr->next)
2183 order_add_column(view, order, ptr->column);
2184
2185 order->reorder = msi_alloc(rows * sizeof(UINT));
2186 if (!order->reorder)
2187 return ERROR_OUTOFMEMORY;
2188
2189 for (i = 0; i < rows; i++)
2190 order->reorder[i] = i;
2191
2192 r = order_mergesort(view, order, 0, rows - 1);
2193 if (r != ERROR_SUCCESS)
2194 return r;
2195
2196 r = order_verify(view, order, rows);
2197 if (r != ERROR_SUCCESS)
2198 return r;
2199
2200 tv->order = order;
2201
2202 return ERROR_SUCCESS;
2203 }
2204
2205 static UINT TABLE_drop(struct tagMSIVIEW *view)
2206 {
2207 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
2208 MSIVIEW *tables = NULL;
2209 MSIRECORD *rec = NULL;
2210 UINT r, row;
2211 INT i;
2212
2213 TRACE("dropping table %s\n", debugstr_w(tv->name));
2214
2215 for (i = tv->table->col_count - 1; i >= 0; i--)
2216 {
2217 r = TABLE_remove_column(view, tv->table->colinfo[i].tablename,
2218 tv->table->colinfo[i].number);
2219 if (r != ERROR_SUCCESS)
2220 return r;
2221 }
2222
2223 rec = MSI_CreateRecord(1);
2224 if (!rec)
2225 return ERROR_OUTOFMEMORY;
2226
2227 MSI_RecordSetStringW(rec, 1, tv->name);
2228
2229 r = TABLE_CreateView(tv->db, szTables, &tables);
2230 if (r != ERROR_SUCCESS)
2231 return r;
2232
2233 r = msi_table_find_row((MSITABLEVIEW *)tables, rec, &row);
2234 if (r != ERROR_SUCCESS)
2235 goto done;
2236
2237 r = TABLE_delete_row(tables, row);
2238 if (r != ERROR_SUCCESS)
2239 goto done;
2240
2241 list_remove(&tv->table->entry);
2242 free_table(tv->table);
2243 TABLE_delete(view);
2244
2245 done:
2246 msiobj_release(&rec->hdr);
2247 tables->ops->delete(tables);
2248
2249 return r;
2250 }
2251
2252 static const MSIVIEWOPS table_ops =
2253 {
2254 TABLE_fetch_int,
2255 TABLE_fetch_stream,
2256 TABLE_get_row,
2257 TABLE_set_row,
2258 TABLE_insert_row,
2259 TABLE_delete_row,
2260 TABLE_execute,
2261 TABLE_close,
2262 TABLE_get_dimensions,
2263 TABLE_get_column_info,
2264 TABLE_modify,
2265 TABLE_delete,
2266 TABLE_find_matching_rows,
2267 TABLE_add_ref,
2268 TABLE_release,
2269 TABLE_add_column,
2270 TABLE_remove_column,
2271 TABLE_sort,
2272 TABLE_drop,
2273 };
2274
2275 UINT TABLE_CreateView( MSIDATABASE *db, LPCWSTR name, MSIVIEW **view )
2276 {
2277 MSITABLEVIEW *tv ;
2278 UINT r, sz;
2279
2280 static const WCHAR Streams[] = {'_','S','t','r','e','a','m','s',0};
2281 static const WCHAR Storages[] = {'_','S','t','o','r','a','g','e','s',0};
2282
2283 TRACE("%p %s %p\n", db, debugstr_w(name), view );
2284
2285 if ( !lstrcmpW( name, Streams ) )
2286 return STREAMS_CreateView( db, view );
2287 else if ( !lstrcmpW( name, Storages ) )
2288 return STORAGES_CreateView( db, view );
2289
2290 sz = sizeof *tv + lstrlenW(name)*sizeof name[0] ;
2291 tv = msi_alloc_zero( sz );
2292 if( !tv )
2293 return ERROR_FUNCTION_FAILED;
2294
2295 r = get_table( db, name, &tv->table );
2296 if( r != ERROR_SUCCESS )
2297 {
2298 msi_free( tv );
2299 WARN("table not found\n");
2300 return r;
2301 }
2302
2303 TRACE("table %p found with %d columns\n", tv->table, tv->table->col_count);
2304
2305 /* fill the structure */
2306 tv->view.ops = &table_ops;
2307 tv->db = db;
2308 tv->columns = tv->table->colinfo;
2309 tv->num_cols = tv->table->col_count;
2310 tv->row_size = msi_table_get_row_size( db, tv->table->colinfo, tv->table->col_count );
2311
2312 TRACE("%s one row is %d bytes\n", debugstr_w(name), tv->row_size );
2313
2314 *view = (MSIVIEW*) tv;
2315 lstrcpyW( tv->name, name );
2316
2317 return ERROR_SUCCESS;
2318 }
2319
2320 UINT MSI_CommitTables( MSIDATABASE *db )
2321 {
2322 UINT r;
2323 MSITABLE *table = NULL;
2324
2325 TRACE("%p\n",db);
2326
2327 r = msi_save_string_table( db->strings, db->storage );
2328 if( r != ERROR_SUCCESS )
2329 {
2330 WARN("failed to save string table r=%08x\n",r);
2331 return r;
2332 }
2333
2334 LIST_FOR_EACH_ENTRY( table, &db->tables, MSITABLE, entry )
2335 {
2336 r = save_table( db, table );
2337 if( r != ERROR_SUCCESS )
2338 {
2339 WARN("failed to save table %s (r=%08x)\n",
2340 debugstr_w(table->name), r);
2341 return r;
2342 }
2343 }
2344
2345 /* force everything to reload next time */
2346 free_cached_tables( db );
2347
2348 return ERROR_SUCCESS;
2349 }
2350
2351 MSICONDITION MSI_DatabaseIsTablePersistent( MSIDATABASE *db, LPCWSTR table )
2352 {
2353 MSITABLE *t;
2354 UINT r;
2355
2356 TRACE("%p %s\n", db, debugstr_w(table));
2357
2358 if (!table)
2359 return MSICONDITION_ERROR;
2360
2361 r = get_table( db, table, &t );
2362 if (r != ERROR_SUCCESS)
2363 return MSICONDITION_NONE;
2364
2365 return t->persistent;
2366 }
2367
2368 static UINT read_raw_int(const BYTE *data, UINT col, UINT bytes)
2369 {
2370 UINT ret = 0, i;
2371
2372 for (i = 0; i < bytes; i++)
2373 ret += (data[col + i] << i * 8);
2374
2375 return ret;
2376 }
2377
2378 static UINT msi_record_encoded_stream_name( const MSITABLEVIEW *tv, MSIRECORD *rec, LPWSTR *pstname )
2379 {
2380 static const WCHAR szDot[] = { '.', 0 };
2381 LPWSTR stname = NULL, sval, p;
2382 DWORD len;
2383 UINT i, r;
2384
2385 TRACE("%p %p\n", tv, rec);
2386
2387 len = lstrlenW( tv->name ) + 1;
2388 stname = msi_alloc( len*sizeof(WCHAR) );
2389 if ( !stname )
2390 {
2391 r = ERROR_OUTOFMEMORY;
2392 goto err;
2393 }
2394
2395 lstrcpyW( stname, tv->name );
2396
2397 for ( i = 0; i < tv->num_cols; i++ )
2398 {
2399 if ( tv->columns[i].type & MSITYPE_KEY )
2400 {
2401 sval = msi_dup_record_field( rec, i + 1 );
2402 if ( !sval )
2403 {
2404 r = ERROR_OUTOFMEMORY;
2405 goto err;
2406 }
2407
2408 len += lstrlenW( szDot ) + lstrlenW ( sval );
2409 p = msi_realloc ( stname, len*sizeof(WCHAR) );
2410 if ( !p )
2411 {
2412 r = ERROR_OUTOFMEMORY;
2413 goto err;
2414 }
2415 stname = p;
2416
2417 lstrcatW( stname, szDot );
2418 lstrcatW( stname, sval );
2419
2420 msi_free( sval );
2421 }
2422 else
2423 continue;
2424 }
2425
2426 *pstname = encode_streamname( FALSE, stname );
2427 msi_free( stname );
2428
2429 return ERROR_SUCCESS;
2430
2431 err:
2432 msi_free ( stname );
2433 *pstname = NULL;
2434 return r;
2435 }
2436
2437 static MSIRECORD *msi_get_transform_record( const MSITABLEVIEW *tv, const string_table *st,
2438 IStorage *stg,
2439 const BYTE *rawdata, UINT bytes_per_strref )
2440 {
2441 UINT i, val, ofs = 0;
2442 USHORT mask;
2443 MSICOLUMNINFO *columns = tv->columns;
2444 MSIRECORD *rec;
2445
2446 mask = rawdata[0] | (rawdata[1] << 8);
2447 rawdata += 2;
2448
2449 rec = MSI_CreateRecord( tv->num_cols );
2450 if( !rec )
2451 return rec;
2452
2453 TRACE("row ->\n");
2454 for( i=0; i<tv->num_cols; i++ )
2455 {
2456 if ( (mask&1) && (i>=(mask>>8)) )
2457 break;
2458 /* all keys must be present */
2459 if ( (~mask&1) && (~columns[i].type & MSITYPE_KEY) && ((1<<i) & ~mask) )
2460 continue;
2461
2462 if( MSITYPE_IS_BINARY(tv->columns[i].type) )
2463 {
2464 LPWSTR encname;
2465 IStream *stm = NULL;
2466 UINT r;
2467
2468 ofs += bytes_per_column( tv->db, &columns[i] );
2469
2470 r = msi_record_encoded_stream_name( tv, rec, &encname );
2471 if ( r != ERROR_SUCCESS )
2472 return NULL;
2473
2474 r = IStorage_OpenStream( stg, encname, NULL,
2475 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stm );
2476 msi_free( encname );
2477 if ( r != ERROR_SUCCESS )
2478 return NULL;
2479
2480 MSI_RecordSetStream( rec, i+1, stm );
2481 TRACE(" field %d [%s]\n", i+1, debugstr_w(encname));
2482 }
2483 else if( columns[i].type & MSITYPE_STRING )
2484 {
2485 LPCWSTR sval;
2486
2487 val = read_raw_int(rawdata, ofs, bytes_per_strref);
2488 sval = msi_string_lookup_id( st, val );
2489 MSI_RecordSetStringW( rec, i+1, sval );
2490 TRACE(" field %d [%s]\n", i+1, debugstr_w(sval));
2491 ofs += bytes_per_strref;
2492 }
2493 else
2494 {
2495 UINT n = bytes_per_column( tv->db, &columns[i] );
2496 switch( n )
2497 {
2498 case 2:
2499 val = read_raw_int(rawdata, ofs, n);
2500 if (val)
2501 MSI_RecordSetInteger( rec, i+1, val^0x8000 );
2502 TRACE(" field %d [0x%04x]\n", i+1, val );
2503 break;
2504 case 4:
2505 val = read_raw_int(rawdata, ofs, n);
2506 if (val)
2507 MSI_RecordSetInteger( rec, i+1, val^0x80000000 );
2508 TRACE(" field %d [0x%08x]\n", i+1, val );
2509 break;
2510 default:
2511 ERR("oops - unknown column width %d\n", n);
2512 break;
2513 }
2514 ofs += n;
2515 }
2516 }
2517 return rec;
2518 }
2519
2520 static void dump_record( MSIRECORD *rec )
2521 {
2522 UINT i, n;
2523
2524 n = MSI_RecordGetFieldCount( rec );
2525 for( i=1; i<=n; i++ )
2526 {
2527 LPCWSTR sval = MSI_RecordGetString( rec, i );
2528
2529 if( MSI_RecordIsNull( rec, i ) )
2530 TRACE("row -> []\n");
2531 else if( (sval = MSI_RecordGetString( rec, i )) )
2532 TRACE("row -> [%s]\n", debugstr_w(sval));
2533 else
2534 TRACE("row -> [0x%08x]\n", MSI_RecordGetInteger( rec, i ) );
2535 }
2536 }
2537
2538 static void dump_table( const string_table *st, const USHORT *rawdata, UINT rawsize )
2539 {
2540 LPCWSTR sval;
2541 UINT i;
2542
2543 for( i=0; i<(rawsize/2); i++ )
2544 {
2545 sval = msi_string_lookup_id( st, rawdata[i] );
2546 MESSAGE(" %04x %s\n", rawdata[i], debugstr_w(sval) );
2547 }
2548 }
2549
2550 static UINT* msi_record_to_row( const MSITABLEVIEW *tv, MSIRECORD *rec )
2551 {
2552 LPCWSTR str;
2553 UINT i, r, *data;
2554
2555 data = msi_alloc( tv->num_cols *sizeof (UINT) );
2556 for( i=0; i<tv->num_cols; i++ )
2557 {
2558 data[i] = 0;
2559
2560 if ( ~tv->columns[i].type & MSITYPE_KEY )
2561 continue;
2562
2563 /* turn the transform column value into a row value */
2564 if ( ( tv->columns[i].type & MSITYPE_STRING ) &&
2565 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
2566 {
2567 str = MSI_RecordGetString( rec, i+1 );
2568 r = msi_string2idW( tv->db->strings, str, &data[i] );
2569
2570 /* if there's no matching string in the string table,
2571 these keys can't match any record, so fail now. */
2572 if( ERROR_SUCCESS != r )
2573 {
2574 msi_free( data );
2575 return NULL;
2576 }
2577 }
2578 else
2579 {
2580 data[i] = MSI_RecordGetInteger( rec, i+1 );
2581
2582 if (data[i] == MSI_NULL_INTEGER)
2583 data[i] = 0;
2584 else if ((tv->columns[i].type&0xff) == 2)
2585 data[i] += 0x8000;
2586 else
2587 data[i] += 0x80000000;
2588 }
2589 }
2590 return data;
2591 }
2592
2593 static UINT msi_row_matches( MSITABLEVIEW *tv, UINT row, const UINT *data )
2594 {
2595 UINT i, r, x, ret = ERROR_FUNCTION_FAILED;
2596
2597 for( i=0; i<tv->num_cols; i++ )
2598 {
2599 if ( ~tv->columns[i].type & MSITYPE_KEY )
2600 continue;
2601
2602 /* turn the transform column value into a row value */
2603 r = TABLE_fetch_int( &tv->view, row, i+1, &x );
2604 if ( r != ERROR_SUCCESS )
2605 {
2606 ERR("TABLE_fetch_int shouldn't fail here\n");
2607 break;
2608 }
2609
2610 /* if this key matches, move to the next column */
2611 if ( x != data[i] )
2612 {
2613 ret = ERROR_FUNCTION_FAILED;
2614 break;
2615 }
2616
2617 ret = ERROR_SUCCESS;
2618 }
2619
2620 return ret;
2621 }
2622
2623 static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row )
2624 {
2625 UINT i, r = ERROR_FUNCTION_FAILED, *data;
2626
2627 data = msi_record_to_row( tv, rec );
2628 if( !data )
2629 return r;
2630 for( i = 0; i < tv->table->row_count; i++ )
2631 {
2632 r = msi_row_matches( tv, i, data );
2633 if( r == ERROR_SUCCESS )
2634 {
2635 *row = i;
2636 break;
2637 }
2638 }
2639 msi_free( data );
2640 return r;
2641 }
2642
2643 typedef struct
2644 {
2645 struct list entry;
2646 LPWSTR name;
2647 } TRANSFORMDATA;
2648
2649 static UINT msi_table_load_transform( MSIDATABASE *db, IStorage *stg,
2650 string_table *st, TRANSFORMDATA *transform,
2651 UINT bytes_per_strref )
2652 {
2653 UINT rawsize = 0;
2654 BYTE *rawdata = NULL;
2655 MSITABLEVIEW *tv = NULL;
2656 UINT r, n, sz, i, mask;
2657 MSIRECORD *rec = NULL;
2658 UINT colcol = 0;
2659 WCHAR coltable[32];
2660 LPWSTR name;
2661
2662 if (!transform)
2663 return ERROR_SUCCESS;
2664
2665 name = transform->name;
2666
2667 coltable[0] = 0;
2668 TRACE("%p %p %p %s\n", db, stg, st, debugstr_w(name) );
2669
2670 /* read the transform data */
2671 read_stream_data( stg, name, TRUE, &rawdata, &rawsize );
2672 if ( !rawdata )
2673 {
2674 TRACE("table %s empty\n", debugstr_w(name) );
2675 return ERROR_INVALID_TABLE;
2676 }
2677
2678 /* create a table view */
2679 r = TABLE_CreateView( db, name, (MSIVIEW**) &tv );
2680 if( r != ERROR_SUCCESS )
2681 goto err;
2682
2683 r = tv->view.ops->execute( &tv->view, NULL );
2684 if( r != ERROR_SUCCESS )
2685 goto err;
2686
2687 TRACE("name = %s columns = %u row_size = %u raw size = %u\n",
2688 debugstr_w(name), tv->num_cols, tv->row_size, rawsize );
2689
2690 /* interpret the data */
2691 r = ERROR_SUCCESS;
2692 for( n=0; n < rawsize; )
2693 {
2694 mask = rawdata[n] | (rawdata[n+1] << 8);
2695
2696 if (mask&1)
2697 {
2698 /*
2699 * if the low bit is set, columns are continuous and
2700 * the number of columns is specified in the high byte
2701 */
2702 sz = 2;
2703 for( i=0; i<tv->num_cols; i++ )
2704 {
2705 if( (tv->columns[i].type & MSITYPE_STRING) &&
2706 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
2707 sz += bytes_per_strref;
2708 else
2709 sz += bytes_per_column( tv->db, &tv->columns[i] );
2710 }
2711 }
2712 else
2713 {
2714 /*
2715 * If the low bit is not set, mask is a bitmask.
2716 * Excepting for key fields, which are always present,
2717 * each bit indicates that a field is present in the transform record.
2718 *
2719 * mask == 0 is a special case ... only the keys will be present
2720 * and it means that this row should be deleted.
2721 */
2722 sz = 2;
2723 for( i=0; i<tv->num_cols; i++ )
2724 {
2725 if( (tv->columns[i].type & MSITYPE_KEY) || ((1<<i)&mask))
2726 {
2727 if( (tv->columns[i].type & MSITYPE_STRING) &&
2728 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
2729 sz += bytes_per_strref;
2730 else
2731 sz += bytes_per_column( tv->db, &tv->columns[i] );
2732 }
2733 }
2734 }
2735
2736 /* check we didn't run of the end of the table */
2737 if ( (n+sz) > rawsize )
2738 {
2739 ERR("borked.\n");
2740 dump_table( st, (USHORT *)rawdata, rawsize );
2741 break;
2742 }
2743
2744 rec = msi_get_transform_record( tv, st, stg, &rawdata[n], bytes_per_strref );
2745 if (rec)
2746 {
2747 if ( mask & 1 )
2748 {
2749 WCHAR table[32];
2750 DWORD sz = 32;
2751 UINT number = MSI_NULL_INTEGER;
2752
2753 TRACE("inserting record\n");
2754
2755 if (!lstrcmpW(name, szColumns))
2756 {
2757 MSI_RecordGetStringW( rec, 1, table, &sz );
2758 number = MSI_RecordGetInteger( rec, 2 );
2759
2760 /*
2761 * Native msi seems writes nul into the Number (2nd) column of
2762 * the _Columns table, only when the columns are from a new table
2763 */
2764 if ( number == MSI_NULL_INTEGER )
2765 {
2766 /* reset the column number on a new table */
2767 if ( lstrcmpW(coltable, table) )
2768 {
2769 colcol = 0;
2770 lstrcpyW( coltable, table );
2771 }
2772
2773 /* fix nul column numbers */
2774 MSI_RecordSetInteger( rec, 2, ++colcol );
2775 }
2776 }
2777
2778 r = TABLE_insert_row( &tv->view, rec, -1, FALSE );
2779 if (r != ERROR_SUCCESS)
2780 WARN("insert row failed\n");
2781
2782 if ( number != MSI_NULL_INTEGER && !lstrcmpW(name, szColumns) )
2783 msi_update_table_columns( db, table );
2784 }
2785 else
2786 {
2787 UINT row = 0;
2788
2789 r = msi_table_find_row( tv, rec, &row );
2790 if (r != ERROR_SUCCESS)
2791 WARN("no matching row to transform\n");
2792 else if ( mask )
2793 {
2794 TRACE("modifying row [%d]:\n", row);
2795 TABLE_set_row( &tv->view, row, rec, mask );
2796 }
2797 else
2798 {
2799 TRACE("deleting row [%d]:\n", row);
2800 TABLE_delete_row( &tv->view, row );
2801 }
2802 }
2803 if( TRACE_ON(msidb) ) dump_record( rec );
2804 msiobj_release( &rec->hdr );
2805 }
2806
2807 n += sz;
2808 }
2809
2810 err:
2811 /* no need to free the table, it's associated with the database */
2812 msi_free( rawdata );
2813 if( tv )
2814 tv->view.ops->delete( &tv->view );
2815
2816 return ERROR_SUCCESS;
2817 }
2818
2819 /*
2820 * msi_table_apply_transform
2821 *
2822 * Enumerate the table transforms in a transform storage and apply each one.
2823 */
2824 UINT msi_table_apply_transform( MSIDATABASE *db, IStorage *stg )
2825 {
2826 struct list transforms;
2827 IEnumSTATSTG *stgenum = NULL;
2828 TRANSFORMDATA *transform;
2829 TRANSFORMDATA *tables = NULL, *columns = NULL;
2830 HRESULT r;
2831 STATSTG stat;
2832 string_table *strings;
2833 UINT ret = ERROR_FUNCTION_FAILED;
2834 UINT bytes_per_strref;
2835
2836 TRACE("%p %p\n", db, stg );
2837
2838 strings = msi_load_string_table( stg, &bytes_per_strref );
2839 if( !strings )
2840 goto end;
2841
2842 r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
2843 if( FAILED( r ) )
2844 goto end;
2845
2846 list_init(&transforms);
2847
2848 while ( TRUE )
2849 {
2850 MSITABLEVIEW *tv = NULL;
2851 WCHAR name[0x40];
2852 ULONG count = 0;
2853
2854 r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
2855 if ( FAILED( r ) || !count )
2856 break;
2857
2858 decode_streamname( stat.pwcsName, name );
2859 CoTaskMemFree( stat.pwcsName );
2860 if ( name[0] != 0x4840 )
2861 continue;
2862
2863 if ( !lstrcmpW( name+1, szStringPool ) ||
2864 !lstrcmpW( name+1, szStringData ) )
2865 continue;
2866
2867 transform = msi_alloc_zero( sizeof(TRANSFORMDATA) );
2868 if ( !transform )
2869 break;
2870
2871 list_add_tail( &transforms, &transform->entry );
2872
2873 transform->name = strdupW( name + 1 );
2874
2875 if ( !lstrcmpW( transform->name, szTables ) )
2876 tables = transform;
2877 else if (!lstrcmpW( transform->name, szColumns ) )
2878 columns = transform;
2879
2880 TRACE("transform contains stream %s\n", debugstr_w(name));
2881
2882 /* load the table */
2883 r = TABLE_CreateView( db, transform->name, (MSIVIEW**) &tv );
2884 if( r != ERROR_SUCCESS )
2885 continue;
2886
2887 r = tv->view.ops->execute( &tv->view, NULL );
2888 if( r != ERROR_SUCCESS )
2889 {
2890 tv->view.ops->delete( &tv->view );
2891 continue;
2892 }
2893
2894 tv->view.ops->delete( &tv->view );
2895 }
2896
2897 /*
2898 * Apply _Tables and _Columns transforms first so that
2899 * the table metadata is correct, and empty tables exist.
2900 */
2901 ret = msi_table_load_transform( db, stg, strings, tables, bytes_per_strref );
2902 if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
2903 goto end;
2904
2905 ret = msi_table_load_transform( db, stg, strings, columns, bytes_per_strref );
2906 if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
2907 goto end;
2908
2909 ret = ERROR_SUCCESS;
2910
2911 while ( !list_empty( &transforms ) )
2912 {
2913 transform = LIST_ENTRY( list_head( &transforms ), TRANSFORMDATA, entry );
2914
2915 if ( lstrcmpW( transform->name, szColumns ) &&
2916 lstrcmpW( transform->name, szTables ) &&
2917 ret == ERROR_SUCCESS )
2918 {
2919 ret = msi_table_load_transform( db, stg, strings, transform, bytes_per_strref );
2920 }
2921
2922 list_remove( &transform->entry );
2923 msi_free( transform->name );
2924 msi_free( transform );
2925 }
2926
2927 if ( ret == ERROR_SUCCESS )
2928 append_storage_to_db( db, stg );
2929
2930 end:
2931 if ( stgenum )
2932 IEnumSTATSTG_Release( stgenum );
2933 if ( strings )
2934 msi_destroy_stringtable( strings );
2935
2936 return ret;
2937 }
2938
2939 void append_storage_to_db( MSIDATABASE *db, IStorage *stg )
2940 {
2941 MSITRANSFORM *t;
2942
2943 t = msi_alloc( sizeof *t );
2944 t->stg = stg;
2945 IStorage_AddRef( stg );
2946 list_add_tail( &db->transforms, &t->entry );
2947 }
2948
2949 void msi_free_transforms( MSIDATABASE *db )
2950 {
2951 while( !list_empty( &db->transforms ) )
2952 {
2953 MSITRANSFORM *t = LIST_ENTRY( list_head( &db->transforms ),
2954 MSITRANSFORM, entry );
2955 list_remove( &t->entry );
2956 IStorage_Release( t->stg );
2957 msi_free( t );
2958 }
2959 }
2960
2961 static UINT table_find_insert_idx (MSIVIEW *view, LPCWSTR name, INT *pidx)
2962 {
2963 UINT r, name_id, row_id;
2964 INT idx;
2965 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
2966
2967 TRACE ("%p %s\n", view, debugstr_w(name));
2968
2969 r = msi_string2idW(tv->db->strings, name, &name_id);
2970 if (r != ERROR_SUCCESS)
2971 {
2972 *pidx = -1;
2973 return r;
2974 }
2975
2976 for( idx = 0; idx < tv->table->row_count; idx++ )
2977 {
2978 r = TABLE_fetch_int( &tv->view, idx, 1, &row_id );
2979 if (row_id > name_id)
2980 break;
2981 }
2982
2983 *pidx = idx;
2984 return ERROR_SUCCESS;
2985 }