gdi32: Change an ERR to a WARN for fonts with too-long names.
[wine/multimedia.git] / dlls / msi / table.c
blobcfe5612ed14fdfd57e41125ee69783ef409daf41
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2002-2005 Mike McCormack for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
22 #include <assert.h>
24 #define COBJMACROS
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winerror.h"
31 #include "msi.h"
32 #include "msiquery.h"
33 #include "objbase.h"
34 #include "objidl.h"
35 #include "winnls.h"
36 #include "msipriv.h"
37 #include "query.h"
39 #include "wine/debug.h"
40 #include "wine/unicode.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
44 #define MSITABLE_HASH_TABLE_SIZE 37
46 typedef struct tagMSICOLUMNHASHENTRY
48 struct tagMSICOLUMNHASHENTRY *next;
49 UINT value;
50 UINT row;
51 } MSICOLUMNHASHENTRY;
53 typedef struct tagMSICOLUMNINFO
55 LPCWSTR tablename;
56 UINT number;
57 LPCWSTR colname;
58 UINT type;
59 UINT offset;
60 INT ref_count;
61 BOOL temporary;
62 MSICOLUMNHASHENTRY **hash_table;
63 } MSICOLUMNINFO;
65 struct tagMSITABLE
67 BYTE **data;
68 BOOL *data_persistent;
69 UINT row_count;
70 struct list entry;
71 MSICOLUMNINFO *colinfo;
72 UINT col_count;
73 MSICONDITION persistent;
74 INT ref_count;
75 WCHAR name[1];
78 /* information for default tables */
79 static const WCHAR szTables[] = {'_','T','a','b','l','e','s',0};
80 static const WCHAR szTable[] = {'T','a','b','l','e',0};
81 static const WCHAR szColumns[] = {'_','C','o','l','u','m','n','s',0};
82 static const WCHAR szNumber[] = {'N','u','m','b','e','r',0};
83 static const WCHAR szType[] = {'T','y','p','e',0};
85 static const MSICOLUMNINFO _Columns_cols[4] = {
86 { szColumns, 1, szTable, MSITYPE_VALID | MSITYPE_STRING | MSITYPE_KEY | 64, 0, 0, 0, NULL },
87 { szColumns, 2, szNumber, MSITYPE_VALID | MSITYPE_KEY | 2, 2, 0, 0, NULL },
88 { szColumns, 3, szName, MSITYPE_VALID | MSITYPE_STRING | 64, 4, 0, 0, NULL },
89 { szColumns, 4, szType, MSITYPE_VALID | 2, 6, 0, 0, NULL },
92 static const MSICOLUMNINFO _Tables_cols[1] = {
93 { szTables, 1, szName, MSITYPE_VALID | MSITYPE_STRING | MSITYPE_KEY | 64, 0, 0, 0, NULL },
96 #define MAX_STREAM_NAME 0x1f
98 static inline UINT bytes_per_column( MSIDATABASE *db, const MSICOLUMNINFO *col, UINT bytes_per_strref )
100 if( MSITYPE_IS_BINARY(col->type) )
101 return 2;
103 if( col->type & MSITYPE_STRING )
104 return bytes_per_strref;
106 if( (col->type & 0xff) <= 2)
107 return 2;
109 if( (col->type & 0xff) != 4 )
110 ERR("Invalid column size %u\n", col->type & 0xff);
112 return 4;
115 static int utf2mime(int x)
117 if( (x>='0') && (x<='9') )
118 return x-'0';
119 if( (x>='A') && (x<='Z') )
120 return x-'A'+10;
121 if( (x>='a') && (x<='z') )
122 return x-'a'+10+26;
123 if( x=='.' )
124 return 10+26+26;
125 if( x=='_' )
126 return 10+26+26+1;
127 return -1;
130 LPWSTR encode_streamname(BOOL bTable, LPCWSTR in)
132 DWORD count = MAX_STREAM_NAME;
133 DWORD ch, next;
134 LPWSTR out, p;
136 if( !bTable )
137 count = lstrlenW( in )+2;
138 if (!(out = msi_alloc( count*sizeof(WCHAR) ))) return NULL;
139 p = out;
141 if( bTable )
143 *p++ = 0x4840;
144 count --;
146 while( count -- )
148 ch = *in++;
149 if( !ch )
151 *p = ch;
152 return out;
154 if( ( ch < 0x80 ) && ( utf2mime(ch) >= 0 ) )
156 ch = utf2mime(ch) + 0x4800;
157 next = *in;
158 if( next && (next<0x80) )
160 next = utf2mime(next);
161 if( next != -1 )
163 next += 0x3ffffc0;
164 ch += (next<<6);
165 in++;
169 *p++ = ch;
171 ERR("Failed to encode stream name (%s)\n",debugstr_w(in));
172 msi_free( out );
173 return NULL;
176 static int mime2utf(int x)
178 if( x<10 )
179 return x + '0';
180 if( x<(10+26))
181 return x - 10 + 'A';
182 if( x<(10+26+26))
183 return x - 10 - 26 + 'a';
184 if( x == (10+26+26) )
185 return '.';
186 return '_';
189 BOOL decode_streamname(LPCWSTR in, LPWSTR out)
191 WCHAR ch;
192 DWORD count = 0;
194 while ( (ch = *in++) )
196 if( (ch >= 0x3800 ) && (ch < 0x4840 ) )
198 if( ch >= 0x4800 )
199 ch = mime2utf(ch-0x4800);
200 else
202 ch -= 0x3800;
203 *out++ = mime2utf(ch&0x3f);
204 count++;
205 ch = mime2utf((ch>>6)&0x3f);
208 *out++ = ch;
209 count++;
211 *out = 0;
212 return count;
215 void enum_stream_names( IStorage *stg )
217 IEnumSTATSTG *stgenum = NULL;
218 HRESULT r;
219 STATSTG stat;
220 ULONG n, count;
221 WCHAR name[0x40];
223 r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
224 if( FAILED( r ) )
225 return;
227 n = 0;
228 while( 1 )
230 count = 0;
231 r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
232 if( FAILED( r ) || !count )
233 break;
234 decode_streamname( stat.pwcsName, name );
235 TRACE("stream %2d -> %s %s\n", n,
236 debugstr_w(stat.pwcsName), debugstr_w(name) );
237 CoTaskMemFree( stat.pwcsName );
238 n++;
241 IEnumSTATSTG_Release( stgenum );
244 UINT read_stream_data( IStorage *stg, LPCWSTR stname, BOOL table,
245 BYTE **pdata, UINT *psz )
247 HRESULT r;
248 UINT ret = ERROR_FUNCTION_FAILED;
249 VOID *data;
250 ULONG sz, count;
251 IStream *stm = NULL;
252 STATSTG stat;
253 LPWSTR encname;
255 encname = encode_streamname(table, stname);
257 TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
259 r = IStorage_OpenStream(stg, encname, NULL,
260 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stm);
261 msi_free( encname );
262 if( FAILED( r ) )
264 WARN("open stream failed r = %08x - empty table?\n", r);
265 return ret;
268 r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
269 if( FAILED( r ) )
271 WARN("open stream failed r = %08x!\n", r);
272 goto end;
275 if( stat.cbSize.QuadPart >> 32 )
277 WARN("Too big!\n");
278 goto end;
281 sz = stat.cbSize.QuadPart;
282 data = msi_alloc( sz );
283 if( !data )
285 WARN("couldn't allocate memory r=%08x!\n", r);
286 ret = ERROR_NOT_ENOUGH_MEMORY;
287 goto end;
290 r = IStream_Read(stm, data, sz, &count );
291 if( FAILED( r ) || ( count != sz ) )
293 msi_free( data );
294 WARN("read stream failed r = %08x!\n", r);
295 goto end;
298 *pdata = data;
299 *psz = sz;
300 ret = ERROR_SUCCESS;
302 end:
303 IStream_Release( stm );
305 return ret;
308 UINT write_stream_data( IStorage *stg, LPCWSTR stname,
309 LPCVOID data, UINT sz, BOOL bTable )
311 HRESULT r;
312 UINT ret = ERROR_FUNCTION_FAILED;
313 ULONG count;
314 IStream *stm = NULL;
315 ULARGE_INTEGER size;
316 LARGE_INTEGER pos;
317 LPWSTR encname;
319 encname = encode_streamname(bTable, stname );
320 r = IStorage_OpenStream( stg, encname, NULL,
321 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, &stm);
322 if( FAILED(r) )
324 r = IStorage_CreateStream( stg, encname,
325 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
327 msi_free( encname );
328 if( FAILED( r ) )
330 WARN("open stream failed r = %08x\n", r);
331 return ret;
334 size.QuadPart = sz;
335 r = IStream_SetSize( stm, size );
336 if( FAILED( r ) )
338 WARN("Failed to SetSize\n");
339 goto end;
342 pos.QuadPart = 0;
343 r = IStream_Seek( stm, pos, STREAM_SEEK_SET, NULL );
344 if( FAILED( r ) )
346 WARN("Failed to Seek\n");
347 goto end;
350 if (sz)
352 r = IStream_Write(stm, data, sz, &count );
353 if( FAILED( r ) || ( count != sz ) )
355 WARN("Failed to Write\n");
356 goto end;
360 ret = ERROR_SUCCESS;
362 end:
363 IStream_Release( stm );
365 return ret;
368 static void msi_free_colinfo( MSICOLUMNINFO *colinfo, UINT count )
370 UINT i;
371 for (i = 0; i < count; i++) msi_free( colinfo[i].hash_table );
374 static void free_table( MSITABLE *table )
376 UINT i;
377 for( i=0; i<table->row_count; i++ )
378 msi_free( table->data[i] );
379 msi_free( table->data );
380 msi_free( table->data_persistent );
381 msi_free_colinfo( table->colinfo, table->col_count );
382 msi_free( table->colinfo );
383 msi_free( table );
386 static UINT msi_table_get_row_size( MSIDATABASE *db, const MSICOLUMNINFO *cols, UINT count, UINT bytes_per_strref )
388 const MSICOLUMNINFO *last_col;
390 if (!count)
391 return 0;
393 if (bytes_per_strref != LONG_STR_BYTES)
395 UINT i, size = 0;
396 for (i = 0; i < count; i++) size += bytes_per_column( db, &cols[i], bytes_per_strref );
397 return size;
399 last_col = &cols[count - 1];
400 return last_col->offset + bytes_per_column( db, last_col, bytes_per_strref );
403 /* add this table to the list of cached tables in the database */
404 static UINT read_table_from_storage( MSIDATABASE *db, MSITABLE *t, IStorage *stg )
406 BYTE *rawdata = NULL;
407 UINT rawsize = 0, i, j, row_size, row_size_mem;
409 TRACE("%s\n",debugstr_w(t->name));
411 row_size = msi_table_get_row_size( db, t->colinfo, t->col_count, db->bytes_per_strref );
412 row_size_mem = msi_table_get_row_size( db, t->colinfo, t->col_count, LONG_STR_BYTES );
414 /* if we can't read the table, just assume that it's empty */
415 read_stream_data( stg, t->name, TRUE, &rawdata, &rawsize );
416 if( !rawdata )
417 return ERROR_SUCCESS;
419 TRACE("Read %d bytes\n", rawsize );
421 if( rawsize % row_size )
423 WARN("Table size is invalid %d/%d\n", rawsize, row_size );
424 goto err;
427 t->row_count = rawsize / row_size;
428 t->data = msi_alloc_zero( t->row_count * sizeof (USHORT*) );
429 if( !t->data )
430 goto err;
431 t->data_persistent = msi_alloc_zero( t->row_count * sizeof(BOOL));
432 if ( !t->data_persistent )
433 goto err;
435 /* transpose all the data */
436 TRACE("Transposing data from %d rows\n", t->row_count );
437 for (i = 0; i < t->row_count; i++)
439 UINT ofs = 0, ofs_mem = 0;
441 t->data[i] = msi_alloc( row_size_mem );
442 if( !t->data[i] )
443 goto err;
444 t->data_persistent[i] = TRUE;
446 for (j = 0; j < t->col_count; j++)
448 UINT m = bytes_per_column( db, &t->colinfo[j], LONG_STR_BYTES );
449 UINT n = bytes_per_column( db, &t->colinfo[j], db->bytes_per_strref );
450 UINT k;
452 if ( n != 2 && n != 3 && n != 4 )
454 ERR("oops - unknown column width %d\n", n);
455 goto err;
457 if (t->colinfo[j].type & MSITYPE_STRING && n < m)
459 for (k = 0; k < m; k++)
461 if (k < n)
462 t->data[i][ofs_mem + k] = rawdata[ofs * t->row_count + i * n + k];
463 else
464 t->data[i][ofs_mem + k] = 0;
467 else
469 for (k = 0; k < n; k++)
470 t->data[i][ofs_mem + k] = rawdata[ofs * t->row_count + i * n + k];
472 ofs_mem += m;
473 ofs += n;
477 msi_free( rawdata );
478 return ERROR_SUCCESS;
479 err:
480 msi_free( rawdata );
481 return ERROR_FUNCTION_FAILED;
484 void free_cached_tables( MSIDATABASE *db )
486 while( !list_empty( &db->tables ) )
488 MSITABLE *t = LIST_ENTRY( list_head( &db->tables ), MSITABLE, entry );
490 list_remove( &t->entry );
491 free_table( t );
495 static MSITABLE *find_cached_table( MSIDATABASE *db, LPCWSTR name )
497 MSITABLE *t;
499 LIST_FOR_EACH_ENTRY( t, &db->tables, MSITABLE, entry )
500 if( !strcmpW( name, t->name ) )
501 return t;
503 return NULL;
506 static void table_calc_column_offsets( MSIDATABASE *db, MSICOLUMNINFO *colinfo, DWORD count )
508 DWORD i;
510 for (i = 0; colinfo && i < count; i++)
512 assert( i + 1 == colinfo[i].number );
513 if (i) colinfo[i].offset = colinfo[i - 1].offset +
514 bytes_per_column( db, &colinfo[i - 1], LONG_STR_BYTES );
515 else colinfo[i].offset = 0;
517 TRACE("column %d is [%s] with type %08x ofs %d\n",
518 colinfo[i].number, debugstr_w(colinfo[i].colname),
519 colinfo[i].type, colinfo[i].offset);
523 static UINT get_defaulttablecolumns( MSIDATABASE *db, LPCWSTR name, MSICOLUMNINFO *colinfo, UINT *sz )
525 const MSICOLUMNINFO *p;
526 DWORD i, n;
528 TRACE("%s\n", debugstr_w(name));
530 if (!strcmpW( name, szTables ))
532 p = _Tables_cols;
533 n = 1;
535 else if (!strcmpW( name, szColumns ))
537 p = _Columns_cols;
538 n = 4;
540 else return ERROR_FUNCTION_FAILED;
542 for (i = 0; i < n; i++)
544 if (colinfo && i < *sz) colinfo[i] = p[i];
545 if (colinfo && i >= *sz) break;
547 table_calc_column_offsets( db, colinfo, n );
548 *sz = n;
549 return ERROR_SUCCESS;
552 static UINT get_tablecolumns( MSIDATABASE *db, LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz );
554 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name, MSICOLUMNINFO **pcols, UINT *pcount )
556 UINT r, column_count = 0;
557 MSICOLUMNINFO *columns;
559 /* get the number of columns in this table */
560 column_count = 0;
561 r = get_tablecolumns( db, name, NULL, &column_count );
562 if (r != ERROR_SUCCESS)
563 return r;
565 *pcount = column_count;
567 /* if there are no columns, there's no table */
568 if (!column_count)
569 return ERROR_INVALID_PARAMETER;
571 TRACE("table %s found\n", debugstr_w(name));
573 columns = msi_alloc( column_count * sizeof(MSICOLUMNINFO) );
574 if (!columns)
575 return ERROR_FUNCTION_FAILED;
577 r = get_tablecolumns( db, name, columns, &column_count );
578 if (r != ERROR_SUCCESS)
580 msi_free( columns );
581 return ERROR_FUNCTION_FAILED;
583 *pcols = columns;
584 return r;
587 static UINT get_table( MSIDATABASE *db, LPCWSTR name, MSITABLE **table_ret )
589 MSITABLE *table;
590 UINT r;
592 /* first, see if the table is cached */
593 table = find_cached_table( db, name );
594 if (table)
596 *table_ret = table;
597 return ERROR_SUCCESS;
600 /* nonexistent tables should be interpreted as empty tables */
601 table = msi_alloc( sizeof(MSITABLE) + lstrlenW( name ) * sizeof(WCHAR) );
602 if (!table)
603 return ERROR_FUNCTION_FAILED;
605 table->row_count = 0;
606 table->data = NULL;
607 table->data_persistent = NULL;
608 table->colinfo = NULL;
609 table->col_count = 0;
610 table->persistent = MSICONDITION_TRUE;
611 lstrcpyW( table->name, name );
613 if (!strcmpW( name, szTables ) || !strcmpW( name, szColumns ))
614 table->persistent = MSICONDITION_NONE;
616 r = table_get_column_info( db, name, &table->colinfo, &table->col_count );
617 if (r != ERROR_SUCCESS)
619 free_table( table );
620 return r;
622 r = read_table_from_storage( db, table, db->storage );
623 if (r != ERROR_SUCCESS)
625 free_table( table );
626 return r;
628 list_add_head( &db->tables, &table->entry );
629 *table_ret = table;
630 return ERROR_SUCCESS;
633 static UINT read_table_int( BYTE *const *data, UINT row, UINT col, UINT bytes )
635 UINT ret = 0, i;
637 for (i = 0; i < bytes; i++)
638 ret += data[row][col + i] << i * 8;
640 return ret;
643 static UINT get_tablecolumns( MSIDATABASE *db, LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz )
645 UINT r, i, n = 0, table_id, count, maxcount = *sz;
646 MSITABLE *table = NULL;
648 TRACE("%s\n", debugstr_w(szTableName));
650 /* first check if there is a default table with that name */
651 r = get_defaulttablecolumns( db, szTableName, colinfo, sz );
652 if (r == ERROR_SUCCESS && *sz)
653 return r;
655 r = get_table( db, szColumns, &table );
656 if (r != ERROR_SUCCESS)
658 ERR("couldn't load _Columns table\n");
659 return ERROR_FUNCTION_FAILED;
662 /* convert table and column names to IDs from the string table */
663 r = msi_string2id( db->strings, szTableName, -1, &table_id );
664 if (r != ERROR_SUCCESS)
666 WARN("Couldn't find id for %s\n", debugstr_w(szTableName));
667 return r;
669 TRACE("Table id is %d, row count is %d\n", table_id, table->row_count);
671 /* Note: _Columns table doesn't have non-persistent data */
673 /* if maxcount is non-zero, assume it's exactly right for this table */
674 if (colinfo) memset( colinfo, 0, maxcount * sizeof(*colinfo) );
675 count = table->row_count;
676 for (i = 0; i < count; i++)
678 if (read_table_int( table->data, i, 0, LONG_STR_BYTES) != table_id) continue;
679 if (colinfo)
681 UINT id = read_table_int( table->data, i, table->colinfo[2].offset, LONG_STR_BYTES );
682 UINT col = read_table_int( table->data, i, table->colinfo[1].offset, sizeof(USHORT) ) - (1 << 15);
684 /* check the column number is in range */
685 if (col < 1 || col > maxcount)
687 ERR("column %d out of range (maxcount: %d)\n", col, maxcount);
688 continue;
690 /* check if this column was already set */
691 if (colinfo[col - 1].number)
693 ERR("duplicate column %d\n", col);
694 continue;
696 colinfo[col - 1].tablename = msi_string_lookup( db->strings, table_id, NULL );
697 colinfo[col - 1].number = col;
698 colinfo[col - 1].colname = msi_string_lookup( db->strings, id, NULL );
699 colinfo[col - 1].type = read_table_int( table->data, i, table->colinfo[3].offset,
700 sizeof(USHORT) ) - (1 << 15);
701 colinfo[col - 1].offset = 0;
702 colinfo[col - 1].ref_count = 0;
703 colinfo[col - 1].hash_table = NULL;
705 n++;
707 TRACE("%s has %d columns\n", debugstr_w(szTableName), n);
709 if (colinfo && n != maxcount)
711 ERR("missing column in table %s\n", debugstr_w(szTableName));
712 msi_free_colinfo( colinfo, maxcount );
713 return ERROR_FUNCTION_FAILED;
715 table_calc_column_offsets( db, colinfo, n );
716 *sz = n;
717 return ERROR_SUCCESS;
720 UINT msi_create_table( MSIDATABASE *db, LPCWSTR name, column_info *col_info,
721 MSICONDITION persistent )
723 enum StringPersistence string_persistence = (persistent) ? StringPersistent : StringNonPersistent;
724 UINT r, nField;
725 MSIVIEW *tv = NULL;
726 MSIRECORD *rec = NULL;
727 column_info *col;
728 MSITABLE *table;
729 UINT i;
731 /* only add tables that don't exist already */
732 if( TABLE_Exists(db, name ) )
734 WARN("table %s exists\n", debugstr_w(name));
735 return ERROR_BAD_QUERY_SYNTAX;
738 table = msi_alloc( sizeof (MSITABLE) + lstrlenW(name)*sizeof (WCHAR) );
739 if( !table )
740 return ERROR_FUNCTION_FAILED;
742 table->ref_count = 1;
743 table->row_count = 0;
744 table->data = NULL;
745 table->data_persistent = NULL;
746 table->colinfo = NULL;
747 table->col_count = 0;
748 table->persistent = persistent;
749 lstrcpyW( table->name, name );
751 for( col = col_info; col; col = col->next )
752 table->col_count++;
754 table->colinfo = msi_alloc( table->col_count * sizeof(MSICOLUMNINFO) );
755 if (!table->colinfo)
757 free_table( table );
758 return ERROR_FUNCTION_FAILED;
761 for( i = 0, col = col_info; col; i++, col = col->next )
763 UINT table_id = msi_add_string( db->strings, col->table, -1, string_persistence );
764 UINT col_id = msi_add_string( db->strings, col->column, -1, string_persistence );
766 table->colinfo[ i ].tablename = msi_string_lookup( db->strings, table_id, NULL );
767 table->colinfo[ i ].number = i + 1;
768 table->colinfo[ i ].colname = msi_string_lookup( db->strings, col_id, NULL );
769 table->colinfo[ i ].type = col->type;
770 table->colinfo[ i ].offset = 0;
771 table->colinfo[ i ].ref_count = 0;
772 table->colinfo[ i ].hash_table = NULL;
773 table->colinfo[ i ].temporary = col->temporary;
775 table_calc_column_offsets( db, table->colinfo, table->col_count);
777 r = TABLE_CreateView( db, szTables, &tv );
778 TRACE("CreateView returned %x\n", r);
779 if( r )
781 free_table( table );
782 return r;
785 r = tv->ops->execute( tv, 0 );
786 TRACE("tv execute returned %x\n", r);
787 if( r )
788 goto err;
790 rec = MSI_CreateRecord( 1 );
791 if( !rec )
792 goto err;
794 r = MSI_RecordSetStringW( rec, 1, name );
795 if( r )
796 goto err;
798 r = tv->ops->insert_row( tv, rec, -1, persistent == MSICONDITION_FALSE );
799 TRACE("insert_row returned %x\n", r);
800 if( r )
801 goto err;
803 tv->ops->delete( tv );
804 tv = NULL;
806 msiobj_release( &rec->hdr );
807 rec = NULL;
809 if( persistent != MSICONDITION_FALSE )
811 /* add each column to the _Columns table */
812 r = TABLE_CreateView( db, szColumns, &tv );
813 if( r )
814 return r;
816 r = tv->ops->execute( tv, 0 );
817 TRACE("tv execute returned %x\n", r);
818 if( r )
819 goto err;
821 rec = MSI_CreateRecord( 4 );
822 if( !rec )
823 goto err;
825 r = MSI_RecordSetStringW( rec, 1, name );
826 if( r )
827 goto err;
830 * need to set the table, column number, col name and type
831 * for each column we enter in the table
833 nField = 1;
834 for( col = col_info; col; col = col->next )
836 r = MSI_RecordSetInteger( rec, 2, nField );
837 if( r )
838 goto err;
840 r = MSI_RecordSetStringW( rec, 3, col->column );
841 if( r )
842 goto err;
844 r = MSI_RecordSetInteger( rec, 4, col->type );
845 if( r )
846 goto err;
848 r = tv->ops->insert_row( tv, rec, -1, FALSE );
849 if( r )
850 goto err;
852 nField++;
854 if( !col )
855 r = ERROR_SUCCESS;
858 err:
859 if (rec)
860 msiobj_release( &rec->hdr );
861 /* FIXME: remove values from the string table on error */
862 if( tv )
863 tv->ops->delete( tv );
865 if (r == ERROR_SUCCESS)
866 list_add_head( &db->tables, &table->entry );
867 else
868 free_table( table );
870 return r;
873 static UINT save_table( MSIDATABASE *db, const MSITABLE *t, UINT bytes_per_strref )
875 BYTE *rawdata = NULL;
876 UINT rawsize, i, j, row_size, row_count;
877 UINT r = ERROR_FUNCTION_FAILED;
879 /* Nothing to do for non-persistent tables */
880 if( t->persistent == MSICONDITION_FALSE )
881 return ERROR_SUCCESS;
883 TRACE("Saving %s\n", debugstr_w( t->name ) );
885 row_size = msi_table_get_row_size( db, t->colinfo, t->col_count, bytes_per_strref );
886 row_count = t->row_count;
887 for (i = 0; i < t->row_count; i++)
889 if (!t->data_persistent[i])
891 row_count = 1; /* yes, this is bizarre */
892 break;
895 rawsize = row_count * row_size;
896 rawdata = msi_alloc_zero( rawsize );
897 if( !rawdata )
899 r = ERROR_NOT_ENOUGH_MEMORY;
900 goto err;
903 rawsize = 0;
904 for (i = 0; i < t->row_count; i++)
906 UINT ofs = 0, ofs_mem = 0;
908 if (!t->data_persistent[i]) break;
910 for (j = 0; j < t->col_count; j++)
912 UINT m = bytes_per_column( db, &t->colinfo[j], LONG_STR_BYTES );
913 UINT n = bytes_per_column( db, &t->colinfo[j], bytes_per_strref );
914 UINT k;
916 if (n != 2 && n != 3 && n != 4)
918 ERR("oops - unknown column width %d\n", n);
919 goto err;
921 if (t->colinfo[j].type & MSITYPE_STRING && n < m)
923 UINT id = read_table_int( t->data, i, ofs_mem, LONG_STR_BYTES );
924 if (id > 1 << bytes_per_strref * 8)
926 ERR("string id %u out of range\n", id);
927 goto err;
930 for (k = 0; k < n; k++)
932 rawdata[ofs * row_count + i * n + k] = t->data[i][ofs_mem + k];
934 ofs_mem += m;
935 ofs += n;
937 rawsize += row_size;
940 TRACE("writing %d bytes\n", rawsize);
941 r = write_stream_data( db->storage, t->name, rawdata, rawsize, TRUE );
943 err:
944 msi_free( rawdata );
945 return r;
948 static void msi_update_table_columns( MSIDATABASE *db, LPCWSTR name )
950 MSITABLE *table;
951 UINT size, offset, old_count;
952 UINT n;
954 if (!(table = find_cached_table( db, name ))) return;
955 old_count = table->col_count;
956 msi_free_colinfo( table->colinfo, table->col_count );
957 msi_free( table->colinfo );
958 table->colinfo = NULL;
960 table_get_column_info( db, name, &table->colinfo, &table->col_count );
961 if (!table->col_count) return;
963 size = msi_table_get_row_size( db, table->colinfo, table->col_count, LONG_STR_BYTES );
964 offset = table->colinfo[table->col_count - 1].offset;
966 for ( n = 0; n < table->row_count; n++ )
968 table->data[n] = msi_realloc( table->data[n], size );
969 if (old_count < table->col_count)
970 memset( &table->data[n][offset], 0, size - offset );
974 /* try to find the table name in the _Tables table */
975 BOOL TABLE_Exists( MSIDATABASE *db, LPCWSTR name )
977 UINT r, table_id, i;
978 MSITABLE *table;
980 if( !strcmpW( name, szTables ) || !strcmpW( name, szColumns ) ||
981 !strcmpW( name, szStreams ) || !strcmpW( name, szStorages ) )
982 return TRUE;
984 r = msi_string2id( db->strings, name, -1, &table_id );
985 if( r != ERROR_SUCCESS )
987 TRACE("Couldn't find id for %s\n", debugstr_w(name));
988 return FALSE;
991 r = get_table( db, szTables, &table );
992 if( r != ERROR_SUCCESS )
994 ERR("table %s not available\n", debugstr_w(szTables));
995 return FALSE;
998 for( i = 0; i < table->row_count; i++ )
1000 if( read_table_int( table->data, i, 0, LONG_STR_BYTES ) == table_id )
1001 return TRUE;
1004 return FALSE;
1007 /* below is the query interface to a table */
1009 typedef struct tagMSITABLEVIEW
1011 MSIVIEW view;
1012 MSIDATABASE *db;
1013 MSITABLE *table;
1014 MSICOLUMNINFO *columns;
1015 UINT num_cols;
1016 UINT row_size;
1017 WCHAR name[1];
1018 } MSITABLEVIEW;
1020 static UINT TABLE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
1022 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1023 UINT offset, n;
1025 if( !tv->table )
1026 return ERROR_INVALID_PARAMETER;
1028 if( (col==0) || (col>tv->num_cols) )
1029 return ERROR_INVALID_PARAMETER;
1031 /* how many rows are there ? */
1032 if( row >= tv->table->row_count )
1033 return ERROR_NO_MORE_ITEMS;
1035 if( tv->columns[col-1].offset >= tv->row_size )
1037 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1038 ERR("%p %p\n", tv, tv->columns );
1039 return ERROR_FUNCTION_FAILED;
1042 n = bytes_per_column( tv->db, &tv->columns[col - 1], LONG_STR_BYTES );
1043 if (n != 2 && n != 3 && n != 4)
1045 ERR("oops! what is %d bytes per column?\n", n );
1046 return ERROR_FUNCTION_FAILED;
1049 offset = tv->columns[col-1].offset;
1050 *val = read_table_int(tv->table->data, row, offset, n);
1052 /* TRACE("Data [%d][%d] = %d\n", row, col, *val ); */
1054 return ERROR_SUCCESS;
1057 static UINT msi_stream_name( const MSITABLEVIEW *tv, UINT row, LPWSTR *pstname )
1059 LPWSTR p, stname = NULL;
1060 UINT i, r, type, ival;
1061 DWORD len;
1062 LPCWSTR sval;
1063 MSIVIEW *view = (MSIVIEW *) tv;
1065 TRACE("%p %d\n", tv, row);
1067 len = lstrlenW( tv->name ) + 1;
1068 stname = msi_alloc( len*sizeof(WCHAR) );
1069 if ( !stname )
1071 r = ERROR_OUTOFMEMORY;
1072 goto err;
1075 lstrcpyW( stname, tv->name );
1077 for ( i = 0; i < tv->num_cols; i++ )
1079 type = tv->columns[i].type;
1080 if ( type & MSITYPE_KEY )
1082 WCHAR number[0x20];
1084 r = TABLE_fetch_int( view, row, i+1, &ival );
1085 if ( r != ERROR_SUCCESS )
1086 goto err;
1088 if ( tv->columns[i].type & MSITYPE_STRING )
1090 sval = msi_string_lookup( tv->db->strings, ival, NULL );
1091 if ( !sval )
1093 r = ERROR_INVALID_PARAMETER;
1094 goto err;
1097 else
1099 static const WCHAR fmt[] = { '%','d',0 };
1100 UINT n = bytes_per_column( tv->db, &tv->columns[i], LONG_STR_BYTES );
1102 switch( n )
1104 case 2:
1105 sprintfW( number, fmt, ival-0x8000 );
1106 break;
1107 case 4:
1108 sprintfW( number, fmt, ival^0x80000000 );
1109 break;
1110 default:
1111 ERR( "oops - unknown column width %d\n", n );
1112 r = ERROR_FUNCTION_FAILED;
1113 goto err;
1115 sval = number;
1118 len += lstrlenW( szDot ) + lstrlenW( sval );
1119 p = msi_realloc ( stname, len*sizeof(WCHAR) );
1120 if ( !p )
1122 r = ERROR_OUTOFMEMORY;
1123 goto err;
1125 stname = p;
1127 lstrcatW( stname, szDot );
1128 lstrcatW( stname, sval );
1130 else
1131 continue;
1134 *pstname = stname;
1135 return ERROR_SUCCESS;
1137 err:
1138 msi_free( stname );
1139 *pstname = NULL;
1140 return r;
1144 * We need a special case for streams, as we need to reference column with
1145 * the name of the stream in the same table, and the table name
1146 * which may not be available at higher levels of the query
1148 static UINT TABLE_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm )
1150 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1151 UINT r;
1152 WCHAR *name;
1154 if( !view->ops->fetch_int )
1155 return ERROR_INVALID_PARAMETER;
1157 r = msi_stream_name( tv, row, &name );
1158 if (r != ERROR_SUCCESS)
1160 ERR("fetching stream, error = %u\n", r);
1161 return r;
1164 r = msi_get_stream( tv->db, name, stm );
1165 if (r != ERROR_SUCCESS)
1166 ERR("fetching stream %s, error = %u\n", debugstr_w(name), r);
1168 msi_free( name );
1169 return r;
1172 static UINT TABLE_set_int( MSITABLEVIEW *tv, UINT row, UINT col, UINT val )
1174 UINT offset, n, i;
1176 if( !tv->table )
1177 return ERROR_INVALID_PARAMETER;
1179 if( (col==0) || (col>tv->num_cols) )
1180 return ERROR_INVALID_PARAMETER;
1182 if( row >= tv->table->row_count )
1183 return ERROR_INVALID_PARAMETER;
1185 if( tv->columns[col-1].offset >= tv->row_size )
1187 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1188 ERR("%p %p\n", tv, tv->columns );
1189 return ERROR_FUNCTION_FAILED;
1192 msi_free( tv->columns[col-1].hash_table );
1193 tv->columns[col-1].hash_table = NULL;
1195 n = bytes_per_column( tv->db, &tv->columns[col - 1], LONG_STR_BYTES );
1196 if ( n != 2 && n != 3 && n != 4 )
1198 ERR("oops! what is %d bytes per column?\n", n );
1199 return ERROR_FUNCTION_FAILED;
1202 offset = tv->columns[col-1].offset;
1203 for ( i = 0; i < n; i++ )
1204 tv->table->data[row][offset + i] = (val >> i * 8) & 0xff;
1206 return ERROR_SUCCESS;
1209 static UINT TABLE_get_row( struct tagMSIVIEW *view, UINT row, MSIRECORD **rec )
1211 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1213 if (!tv->table)
1214 return ERROR_INVALID_PARAMETER;
1216 return msi_view_get_row(tv->db, view, row, rec);
1219 static UINT msi_addstreamW( MSIDATABASE *db, LPCWSTR name, IStream *data )
1221 static const WCHAR insert[] = {
1222 'I','N','S','E','R','T',' ','I','N','T','O',' ',
1223 '`','_','S','t','r','e','a','m','s','`',' ',
1224 '(','`','N','a','m','e','`',',','`','D','a','t','a','`',')',' ',
1225 'V','A','L','U','E','S',' ','(','?',',','?',')',0};
1226 MSIQUERY *query = NULL;
1227 MSIRECORD *rec;
1228 UINT r;
1230 TRACE("%p %s %p\n", db, debugstr_w(name), data);
1232 rec = MSI_CreateRecord( 2 );
1233 if ( !rec )
1234 return ERROR_OUTOFMEMORY;
1236 r = MSI_RecordSetStringW( rec, 1, name );
1237 if ( r != ERROR_SUCCESS )
1238 goto err;
1240 r = MSI_RecordSetIStream( rec, 2, data );
1241 if ( r != ERROR_SUCCESS )
1242 goto err;
1244 r = MSI_DatabaseOpenViewW( db, insert, &query );
1245 if ( r != ERROR_SUCCESS )
1246 goto err;
1248 r = MSI_ViewExecute( query, rec );
1250 err:
1251 msiobj_release( &query->hdr );
1252 msiobj_release( &rec->hdr );
1253 return r;
1256 static UINT get_table_value_from_record( MSITABLEVIEW *tv, MSIRECORD *rec, UINT iField, UINT *pvalue )
1258 MSICOLUMNINFO columninfo;
1259 UINT r;
1260 int ival;
1262 if ( (iField <= 0) ||
1263 (iField > tv->num_cols) ||
1264 MSI_RecordIsNull( rec, iField ) )
1265 return ERROR_FUNCTION_FAILED;
1267 columninfo = tv->columns[ iField - 1 ];
1269 if ( MSITYPE_IS_BINARY(columninfo.type) )
1271 *pvalue = 1; /* refers to the first key column */
1273 else if ( columninfo.type & MSITYPE_STRING )
1275 int len;
1276 const WCHAR *sval = msi_record_get_string( rec, iField, &len );
1277 if (sval)
1279 r = msi_string2id( tv->db->strings, sval, len, pvalue );
1280 if (r != ERROR_SUCCESS)
1281 return ERROR_NOT_FOUND;
1283 else *pvalue = 0;
1285 else if ( bytes_per_column( tv->db, &columninfo, LONG_STR_BYTES ) == 2 )
1287 ival = MSI_RecordGetInteger( rec, iField );
1288 if (ival == 0x80000000) *pvalue = 0x8000;
1289 else
1291 *pvalue = 0x8000 + MSI_RecordGetInteger( rec, iField );
1292 if (*pvalue & 0xffff0000)
1294 ERR("field %u value %d out of range\n", iField, *pvalue - 0x8000);
1295 return ERROR_FUNCTION_FAILED;
1299 else
1301 ival = MSI_RecordGetInteger( rec, iField );
1302 *pvalue = ival ^ 0x80000000;
1305 return ERROR_SUCCESS;
1308 static UINT TABLE_set_row( struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask )
1310 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1311 UINT i, val, r = ERROR_SUCCESS;
1313 if ( !tv->table )
1314 return ERROR_INVALID_PARAMETER;
1316 /* test if any of the mask bits are invalid */
1317 if ( mask >= (1<<tv->num_cols) )
1318 return ERROR_INVALID_PARAMETER;
1320 for ( i = 0; i < tv->num_cols; i++ )
1322 BOOL persistent;
1324 /* only update the fields specified in the mask */
1325 if ( !(mask&(1<<i)) )
1326 continue;
1328 persistent = (tv->table->persistent != MSICONDITION_FALSE) &&
1329 (tv->table->data_persistent[row]);
1330 /* FIXME: should we allow updating keys? */
1332 val = 0;
1333 if ( !MSI_RecordIsNull( rec, i + 1 ) )
1335 r = get_table_value_from_record (tv, rec, i + 1, &val);
1336 if ( MSITYPE_IS_BINARY(tv->columns[ i ].type) )
1338 IStream *stm;
1339 LPWSTR stname;
1341 if ( r != ERROR_SUCCESS )
1342 return ERROR_FUNCTION_FAILED;
1344 r = MSI_RecordGetIStream( rec, i + 1, &stm );
1345 if ( r != ERROR_SUCCESS )
1346 return r;
1348 r = msi_stream_name( tv, row, &stname );
1349 if ( r != ERROR_SUCCESS )
1351 IStream_Release( stm );
1352 return r;
1355 r = msi_addstreamW( tv->db, stname, stm );
1356 IStream_Release( stm );
1357 msi_free ( stname );
1359 if ( r != ERROR_SUCCESS )
1360 return r;
1362 else if ( tv->columns[i].type & MSITYPE_STRING )
1364 UINT x;
1366 if ( r != ERROR_SUCCESS )
1368 int len;
1369 const WCHAR *sval = msi_record_get_string( rec, i + 1, &len );
1370 val = msi_add_string( tv->db->strings, sval, len,
1371 persistent ? StringPersistent : StringNonPersistent );
1373 else
1375 TABLE_fetch_int(&tv->view, row, i + 1, &x);
1376 if (val == x)
1377 continue;
1380 else
1382 if ( r != ERROR_SUCCESS )
1383 return ERROR_FUNCTION_FAILED;
1387 r = TABLE_set_int( tv, row, i+1, val );
1388 if ( r != ERROR_SUCCESS )
1389 break;
1391 return r;
1394 static UINT table_create_new_row( struct tagMSIVIEW *view, UINT *num, BOOL temporary )
1396 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1397 BYTE **p, *row;
1398 BOOL *b;
1399 UINT sz;
1400 BYTE ***data_ptr;
1401 BOOL **data_persist_ptr;
1402 UINT *row_count;
1404 TRACE("%p %s\n", view, temporary ? "TRUE" : "FALSE");
1406 if( !tv->table )
1407 return ERROR_INVALID_PARAMETER;
1409 row = msi_alloc_zero( tv->row_size );
1410 if( !row )
1411 return ERROR_NOT_ENOUGH_MEMORY;
1413 row_count = &tv->table->row_count;
1414 data_ptr = &tv->table->data;
1415 data_persist_ptr = &tv->table->data_persistent;
1416 if (*num == -1)
1417 *num = tv->table->row_count;
1419 sz = (*row_count + 1) * sizeof (BYTE*);
1420 if( *data_ptr )
1421 p = msi_realloc( *data_ptr, sz );
1422 else
1423 p = msi_alloc( sz );
1424 if( !p )
1426 msi_free( row );
1427 return ERROR_NOT_ENOUGH_MEMORY;
1430 sz = (*row_count + 1) * sizeof (BOOL);
1431 if( *data_persist_ptr )
1432 b = msi_realloc( *data_persist_ptr, sz );
1433 else
1434 b = msi_alloc( sz );
1435 if( !b )
1437 msi_free( row );
1438 msi_free( p );
1439 return ERROR_NOT_ENOUGH_MEMORY;
1442 *data_ptr = p;
1443 (*data_ptr)[*row_count] = row;
1445 *data_persist_ptr = b;
1446 (*data_persist_ptr)[*row_count] = !temporary;
1448 (*row_count)++;
1450 return ERROR_SUCCESS;
1453 static UINT TABLE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
1455 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1457 TRACE("%p %p\n", tv, record);
1459 TRACE("There are %d columns\n", tv->num_cols );
1461 return ERROR_SUCCESS;
1464 static UINT TABLE_close( struct tagMSIVIEW *view )
1466 TRACE("%p\n", view );
1468 return ERROR_SUCCESS;
1471 static UINT TABLE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols)
1473 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1475 TRACE("%p %p %p\n", view, rows, cols );
1477 if( cols )
1478 *cols = tv->num_cols;
1479 if( rows )
1481 if( !tv->table )
1482 return ERROR_INVALID_PARAMETER;
1483 *rows = tv->table->row_count;
1486 return ERROR_SUCCESS;
1489 static UINT TABLE_get_column_info( struct tagMSIVIEW *view,
1490 UINT n, LPCWSTR *name, UINT *type, BOOL *temporary,
1491 LPCWSTR *table_name )
1493 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1495 TRACE("%p %d %p %p\n", tv, n, name, type );
1497 if( ( n == 0 ) || ( n > tv->num_cols ) )
1498 return ERROR_INVALID_PARAMETER;
1500 if( name )
1502 *name = tv->columns[n-1].colname;
1503 if( !*name )
1504 return ERROR_FUNCTION_FAILED;
1507 if( table_name )
1509 *table_name = tv->columns[n-1].tablename;
1510 if( !*table_name )
1511 return ERROR_FUNCTION_FAILED;
1514 if( type )
1515 *type = tv->columns[n-1].type;
1517 if( temporary )
1518 *temporary = tv->columns[n-1].temporary;
1520 return ERROR_SUCCESS;
1523 static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row, UINT *column );
1525 static UINT table_validate_new( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *column )
1527 UINT r, row, i;
1529 /* check there are no null values where they're not allowed */
1530 for( i = 0; i < tv->num_cols; i++ )
1532 if ( tv->columns[i].type & MSITYPE_NULLABLE )
1533 continue;
1535 if ( MSITYPE_IS_BINARY(tv->columns[i].type) )
1536 TRACE("skipping binary column\n");
1537 else if ( tv->columns[i].type & MSITYPE_STRING )
1539 int len;
1540 const WCHAR *str = msi_record_get_string( rec, i+1, &len );
1542 if (!str || (!str[0] && !len))
1544 if (column) *column = i;
1545 return ERROR_INVALID_DATA;
1548 else
1550 UINT n;
1552 n = MSI_RecordGetInteger( rec, i+1 );
1553 if (n == MSI_NULL_INTEGER)
1555 if (column) *column = i;
1556 return ERROR_INVALID_DATA;
1561 /* check there are no duplicate keys */
1562 r = msi_table_find_row( tv, rec, &row, column );
1563 if (r == ERROR_SUCCESS)
1564 return ERROR_FUNCTION_FAILED;
1566 return ERROR_SUCCESS;
1569 static int compare_record( MSITABLEVIEW *tv, UINT row, MSIRECORD *rec )
1571 UINT r, i, ivalue, x;
1573 for (i = 0; i < tv->num_cols; i++ )
1575 if (!(tv->columns[i].type & MSITYPE_KEY)) continue;
1577 r = get_table_value_from_record( tv, rec, i + 1, &ivalue );
1578 if (r != ERROR_SUCCESS)
1579 return 1;
1581 r = TABLE_fetch_int( &tv->view, row, i + 1, &x );
1582 if (r != ERROR_SUCCESS)
1584 WARN("TABLE_fetch_int should not fail here %u\n", r);
1585 return -1;
1587 if (ivalue > x)
1589 return 1;
1591 else if (ivalue == x)
1593 if (i < tv->num_cols - 1) continue;
1594 return 0;
1596 else
1597 return -1;
1599 return 1;
1602 static int find_insert_index( MSITABLEVIEW *tv, MSIRECORD *rec )
1604 int idx, c, low = 0, high = tv->table->row_count - 1;
1606 TRACE("%p %p\n", tv, rec);
1608 while (low <= high)
1610 idx = (low + high) / 2;
1611 c = compare_record( tv, idx, rec );
1613 if (c < 0)
1614 high = idx - 1;
1615 else if (c > 0)
1616 low = idx + 1;
1617 else
1619 TRACE("found %u\n", idx);
1620 return idx;
1623 TRACE("found %u\n", high + 1);
1624 return high + 1;
1627 static UINT TABLE_insert_row( struct tagMSIVIEW *view, MSIRECORD *rec, UINT row, BOOL temporary )
1629 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1630 UINT i, r;
1632 TRACE("%p %p %s\n", tv, rec, temporary ? "TRUE" : "FALSE" );
1634 /* check that the key is unique - can we find a matching row? */
1635 r = table_validate_new( tv, rec, NULL );
1636 if( r != ERROR_SUCCESS )
1637 return ERROR_FUNCTION_FAILED;
1639 if (row == -1)
1640 row = find_insert_index( tv, rec );
1642 r = table_create_new_row( view, &row, temporary );
1643 TRACE("insert_row returned %08x\n", r);
1644 if( r != ERROR_SUCCESS )
1645 return r;
1647 /* shift the rows to make room for the new row */
1648 for (i = tv->table->row_count - 1; i > row; i--)
1650 memmove(&(tv->table->data[i][0]),
1651 &(tv->table->data[i - 1][0]), tv->row_size);
1652 tv->table->data_persistent[i] = tv->table->data_persistent[i - 1];
1655 /* Re-set the persistence flag */
1656 tv->table->data_persistent[row] = !temporary;
1657 return TABLE_set_row( view, row, rec, (1<<tv->num_cols) - 1 );
1660 static UINT TABLE_delete_row( struct tagMSIVIEW *view, UINT row )
1662 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1663 UINT r, num_rows, num_cols, i;
1665 TRACE("%p %d\n", tv, row);
1667 if ( !tv->table )
1668 return ERROR_INVALID_PARAMETER;
1670 r = TABLE_get_dimensions( view, &num_rows, &num_cols );
1671 if ( r != ERROR_SUCCESS )
1672 return r;
1674 if ( row >= num_rows )
1675 return ERROR_FUNCTION_FAILED;
1677 num_rows = tv->table->row_count;
1678 tv->table->row_count--;
1680 /* reset the hash tables */
1681 for (i = 0; i < tv->num_cols; i++)
1683 msi_free( tv->columns[i].hash_table );
1684 tv->columns[i].hash_table = NULL;
1687 for (i = row + 1; i < num_rows; i++)
1689 memcpy(tv->table->data[i - 1], tv->table->data[i], tv->row_size);
1690 tv->table->data_persistent[i - 1] = tv->table->data_persistent[i];
1693 msi_free(tv->table->data[num_rows - 1]);
1695 return ERROR_SUCCESS;
1698 static UINT msi_table_update(struct tagMSIVIEW *view, MSIRECORD *rec, UINT row)
1700 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1701 UINT r, new_row;
1703 /* FIXME: MsiViewFetch should set rec index 0 to some ID that
1704 * sets the fetched record apart from other records
1707 if (!tv->table)
1708 return ERROR_INVALID_PARAMETER;
1710 r = msi_table_find_row(tv, rec, &new_row, NULL);
1711 if (r != ERROR_SUCCESS)
1713 ERR("can't find row to modify\n");
1714 return ERROR_FUNCTION_FAILED;
1717 /* the row cannot be changed */
1718 if (row != new_row + 1)
1719 return ERROR_FUNCTION_FAILED;
1721 return TABLE_set_row(view, new_row, rec, (1 << tv->num_cols) - 1);
1724 static UINT msi_table_assign(struct tagMSIVIEW *view, MSIRECORD *rec)
1726 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1727 UINT r, row;
1729 if (!tv->table)
1730 return ERROR_INVALID_PARAMETER;
1732 r = msi_table_find_row(tv, rec, &row, NULL);
1733 if (r == ERROR_SUCCESS)
1734 return TABLE_set_row(view, row, rec, (1 << tv->num_cols) - 1);
1735 else
1736 return TABLE_insert_row( view, rec, -1, FALSE );
1739 static UINT modify_delete_row( struct tagMSIVIEW *view, MSIRECORD *rec )
1741 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1742 UINT row, r;
1744 r = msi_table_find_row(tv, rec, &row, NULL);
1745 if (r != ERROR_SUCCESS)
1746 return r;
1748 return TABLE_delete_row(view, row);
1751 static UINT msi_refresh_record( struct tagMSIVIEW *view, MSIRECORD *rec, UINT row )
1753 MSIRECORD *curr;
1754 UINT r, i, count;
1756 r = TABLE_get_row(view, row - 1, &curr);
1757 if (r != ERROR_SUCCESS)
1758 return r;
1760 /* Close the original record */
1761 MSI_CloseRecord(&rec->hdr);
1763 count = MSI_RecordGetFieldCount(rec);
1764 for (i = 0; i < count; i++)
1765 MSI_RecordCopyField(curr, i + 1, rec, i + 1);
1767 msiobj_release(&curr->hdr);
1768 return ERROR_SUCCESS;
1771 static UINT TABLE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
1772 MSIRECORD *rec, UINT row)
1774 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1775 UINT r, frow, column;
1777 TRACE("%p %d %p\n", view, eModifyMode, rec );
1779 switch (eModifyMode)
1781 case MSIMODIFY_DELETE:
1782 r = modify_delete_row( view, rec );
1783 break;
1784 case MSIMODIFY_VALIDATE_NEW:
1785 r = table_validate_new( tv, rec, &column );
1786 if (r != ERROR_SUCCESS)
1788 tv->view.error = MSIDBERROR_DUPLICATEKEY;
1789 tv->view.error_column = tv->columns[column].colname;
1790 r = ERROR_INVALID_DATA;
1792 break;
1794 case MSIMODIFY_INSERT:
1795 r = table_validate_new( tv, rec, NULL );
1796 if (r != ERROR_SUCCESS)
1797 break;
1798 r = TABLE_insert_row( view, rec, -1, FALSE );
1799 break;
1801 case MSIMODIFY_INSERT_TEMPORARY:
1802 r = table_validate_new( tv, rec, NULL );
1803 if (r != ERROR_SUCCESS)
1804 break;
1805 r = TABLE_insert_row( view, rec, -1, TRUE );
1806 break;
1808 case MSIMODIFY_REFRESH:
1809 r = msi_refresh_record( view, rec, row );
1810 break;
1812 case MSIMODIFY_UPDATE:
1813 r = msi_table_update( view, rec, row );
1814 break;
1816 case MSIMODIFY_ASSIGN:
1817 r = msi_table_assign( view, rec );
1818 break;
1820 case MSIMODIFY_MERGE:
1821 /* check row that matches this record */
1822 r = msi_table_find_row( tv, rec, &frow, &column );
1823 if (r != ERROR_SUCCESS)
1825 r = table_validate_new( tv, rec, NULL );
1826 if (r == ERROR_SUCCESS)
1827 r = TABLE_insert_row( view, rec, -1, FALSE );
1829 break;
1831 case MSIMODIFY_REPLACE:
1832 case MSIMODIFY_VALIDATE:
1833 case MSIMODIFY_VALIDATE_FIELD:
1834 case MSIMODIFY_VALIDATE_DELETE:
1835 FIXME("%p %d %p - mode not implemented\n", view, eModifyMode, rec );
1836 r = ERROR_CALL_NOT_IMPLEMENTED;
1837 break;
1839 default:
1840 r = ERROR_INVALID_DATA;
1843 return r;
1846 static UINT TABLE_delete( struct tagMSIVIEW *view )
1848 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1850 TRACE("%p\n", view );
1852 tv->table = NULL;
1853 tv->columns = NULL;
1855 msi_free( tv );
1857 return ERROR_SUCCESS;
1860 static UINT TABLE_find_matching_rows( struct tagMSIVIEW *view, UINT col,
1861 UINT val, UINT *row, MSIITERHANDLE *handle )
1863 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1864 const MSICOLUMNHASHENTRY *entry;
1866 TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
1868 if( !tv->table )
1869 return ERROR_INVALID_PARAMETER;
1871 if( (col==0) || (col > tv->num_cols) )
1872 return ERROR_INVALID_PARAMETER;
1874 if( !tv->columns[col-1].hash_table )
1876 UINT i;
1877 UINT num_rows = tv->table->row_count;
1878 MSICOLUMNHASHENTRY **hash_table;
1879 MSICOLUMNHASHENTRY *new_entry;
1881 if( tv->columns[col-1].offset >= tv->row_size )
1883 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1884 ERR("%p %p\n", tv, tv->columns );
1885 return ERROR_FUNCTION_FAILED;
1888 /* allocate contiguous memory for the table and its entries so we
1889 * don't have to do an expensive cleanup */
1890 hash_table = msi_alloc(MSITABLE_HASH_TABLE_SIZE * sizeof(MSICOLUMNHASHENTRY*) +
1891 num_rows * sizeof(MSICOLUMNHASHENTRY));
1892 if (!hash_table)
1893 return ERROR_OUTOFMEMORY;
1895 memset(hash_table, 0, MSITABLE_HASH_TABLE_SIZE * sizeof(MSICOLUMNHASHENTRY*));
1896 tv->columns[col-1].hash_table = hash_table;
1898 new_entry = (MSICOLUMNHASHENTRY *)(hash_table + MSITABLE_HASH_TABLE_SIZE);
1900 for (i = 0; i < num_rows; i++, new_entry++)
1902 UINT row_value;
1904 if (view->ops->fetch_int( view, i, col, &row_value ) != ERROR_SUCCESS)
1905 continue;
1907 new_entry->next = NULL;
1908 new_entry->value = row_value;
1909 new_entry->row = i;
1910 if (hash_table[row_value % MSITABLE_HASH_TABLE_SIZE])
1912 MSICOLUMNHASHENTRY *prev_entry = hash_table[row_value % MSITABLE_HASH_TABLE_SIZE];
1913 while (prev_entry->next)
1914 prev_entry = prev_entry->next;
1915 prev_entry->next = new_entry;
1917 else
1918 hash_table[row_value % MSITABLE_HASH_TABLE_SIZE] = new_entry;
1922 if( !*handle )
1923 entry = tv->columns[col-1].hash_table[val % MSITABLE_HASH_TABLE_SIZE];
1924 else
1925 entry = (*handle)->next;
1927 while (entry && entry->value != val)
1928 entry = entry->next;
1930 *handle = entry;
1931 if (!entry)
1932 return ERROR_NO_MORE_ITEMS;
1934 *row = entry->row;
1936 return ERROR_SUCCESS;
1939 static UINT TABLE_add_ref(struct tagMSIVIEW *view)
1941 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1942 UINT i;
1944 TRACE("%p %d\n", view, tv->table->ref_count);
1946 for (i = 0; i < tv->table->col_count; i++)
1948 if (tv->table->colinfo[i].type & MSITYPE_TEMPORARY)
1949 InterlockedIncrement(&tv->table->colinfo[i].ref_count);
1952 return InterlockedIncrement(&tv->table->ref_count);
1955 static UINT TABLE_remove_column(struct tagMSIVIEW *view, LPCWSTR table, UINT number)
1957 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1958 MSIRECORD *rec = NULL;
1959 MSIVIEW *columns = NULL;
1960 UINT row, r;
1962 rec = MSI_CreateRecord(2);
1963 if (!rec)
1964 return ERROR_OUTOFMEMORY;
1966 MSI_RecordSetStringW(rec, 1, table);
1967 MSI_RecordSetInteger(rec, 2, number);
1969 r = TABLE_CreateView(tv->db, szColumns, &columns);
1970 if (r != ERROR_SUCCESS)
1972 msiobj_release(&rec->hdr);
1973 return r;
1976 r = msi_table_find_row((MSITABLEVIEW *)columns, rec, &row, NULL);
1977 if (r != ERROR_SUCCESS)
1978 goto done;
1980 r = TABLE_delete_row(columns, row);
1981 if (r != ERROR_SUCCESS)
1982 goto done;
1984 msi_update_table_columns(tv->db, table);
1986 done:
1987 msiobj_release(&rec->hdr);
1988 columns->ops->delete(columns);
1989 return r;
1992 static UINT TABLE_release(struct tagMSIVIEW *view)
1994 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1995 INT ref = tv->table->ref_count;
1996 UINT i, r;
1998 TRACE("%p %d\n", view, ref);
2000 for (i = 0; i < tv->table->col_count; i++)
2002 if (tv->table->colinfo[i].type & MSITYPE_TEMPORARY)
2004 ref = InterlockedDecrement(&tv->table->colinfo[i].ref_count);
2005 if (ref == 0)
2007 r = TABLE_remove_column(view, tv->table->colinfo[i].tablename,
2008 tv->table->colinfo[i].number);
2009 if (r != ERROR_SUCCESS)
2010 break;
2015 ref = InterlockedDecrement(&tv->table->ref_count);
2016 if (ref == 0)
2018 if (!tv->table->row_count)
2020 list_remove(&tv->table->entry);
2021 free_table(tv->table);
2022 TABLE_delete(view);
2026 return ref;
2029 static UINT TABLE_add_column(struct tagMSIVIEW *view, LPCWSTR table, UINT number,
2030 LPCWSTR column, UINT type, BOOL hold)
2032 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
2033 MSITABLE *msitable;
2034 MSIRECORD *rec;
2035 UINT r, i;
2037 rec = MSI_CreateRecord(4);
2038 if (!rec)
2039 return ERROR_OUTOFMEMORY;
2041 MSI_RecordSetStringW(rec, 1, table);
2042 MSI_RecordSetInteger(rec, 2, number);
2043 MSI_RecordSetStringW(rec, 3, column);
2044 MSI_RecordSetInteger(rec, 4, type);
2046 r = TABLE_insert_row(&tv->view, rec, -1, FALSE);
2047 if (r != ERROR_SUCCESS)
2048 goto done;
2050 msi_update_table_columns(tv->db, table);
2052 if (!hold)
2053 goto done;
2055 msitable = find_cached_table(tv->db, table);
2056 for (i = 0; i < msitable->col_count; i++)
2058 if (!strcmpW( msitable->colinfo[i].colname, column ))
2060 InterlockedIncrement(&msitable->colinfo[i].ref_count);
2061 break;
2065 done:
2066 msiobj_release(&rec->hdr);
2067 return r;
2070 static UINT TABLE_drop(struct tagMSIVIEW *view)
2072 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
2073 MSIVIEW *tables = NULL;
2074 MSIRECORD *rec = NULL;
2075 UINT r, row;
2076 INT i;
2078 TRACE("dropping table %s\n", debugstr_w(tv->name));
2080 for (i = tv->table->col_count - 1; i >= 0; i--)
2082 r = TABLE_remove_column(view, tv->table->colinfo[i].tablename,
2083 tv->table->colinfo[i].number);
2084 if (r != ERROR_SUCCESS)
2085 return r;
2088 rec = MSI_CreateRecord(1);
2089 if (!rec)
2090 return ERROR_OUTOFMEMORY;
2092 MSI_RecordSetStringW(rec, 1, tv->name);
2094 r = TABLE_CreateView(tv->db, szTables, &tables);
2095 if (r != ERROR_SUCCESS)
2097 msiobj_release(&rec->hdr);
2098 return r;
2101 r = msi_table_find_row((MSITABLEVIEW *)tables, rec, &row, NULL);
2102 if (r != ERROR_SUCCESS)
2103 goto done;
2105 r = TABLE_delete_row(tables, row);
2106 if (r != ERROR_SUCCESS)
2107 goto done;
2109 list_remove(&tv->table->entry);
2110 free_table(tv->table);
2112 done:
2113 msiobj_release(&rec->hdr);
2114 tables->ops->delete(tables);
2116 return r;
2119 static const MSIVIEWOPS table_ops =
2121 TABLE_fetch_int,
2122 TABLE_fetch_stream,
2123 TABLE_get_row,
2124 TABLE_set_row,
2125 TABLE_insert_row,
2126 TABLE_delete_row,
2127 TABLE_execute,
2128 TABLE_close,
2129 TABLE_get_dimensions,
2130 TABLE_get_column_info,
2131 TABLE_modify,
2132 TABLE_delete,
2133 TABLE_find_matching_rows,
2134 TABLE_add_ref,
2135 TABLE_release,
2136 TABLE_add_column,
2137 TABLE_remove_column,
2138 NULL,
2139 TABLE_drop,
2142 UINT TABLE_CreateView( MSIDATABASE *db, LPCWSTR name, MSIVIEW **view )
2144 MSITABLEVIEW *tv ;
2145 UINT r, sz;
2147 TRACE("%p %s %p\n", db, debugstr_w(name), view );
2149 if ( !strcmpW( name, szStreams ) )
2150 return STREAMS_CreateView( db, view );
2151 else if ( !strcmpW( name, szStorages ) )
2152 return STORAGES_CreateView( db, view );
2154 sz = FIELD_OFFSET( MSITABLEVIEW, name[lstrlenW( name ) + 1] );
2155 tv = msi_alloc_zero( sz );
2156 if( !tv )
2157 return ERROR_FUNCTION_FAILED;
2159 r = get_table( db, name, &tv->table );
2160 if( r != ERROR_SUCCESS )
2162 msi_free( tv );
2163 WARN("table not found\n");
2164 return r;
2167 TRACE("table %p found with %d columns\n", tv->table, tv->table->col_count);
2169 /* fill the structure */
2170 tv->view.ops = &table_ops;
2171 tv->db = db;
2172 tv->columns = tv->table->colinfo;
2173 tv->num_cols = tv->table->col_count;
2174 tv->row_size = msi_table_get_row_size( db, tv->table->colinfo, tv->table->col_count, LONG_STR_BYTES );
2176 TRACE("%s one row is %d bytes\n", debugstr_w(name), tv->row_size );
2178 *view = (MSIVIEW*) tv;
2179 lstrcpyW( tv->name, name );
2181 return ERROR_SUCCESS;
2184 UINT MSI_CommitTables( MSIDATABASE *db )
2186 UINT r, bytes_per_strref;
2187 HRESULT hr;
2188 MSITABLE *table = NULL;
2190 TRACE("%p\n",db);
2192 r = msi_save_string_table( db->strings, db->storage, &bytes_per_strref );
2193 if( r != ERROR_SUCCESS )
2195 WARN("failed to save string table r=%08x\n",r);
2196 return r;
2199 LIST_FOR_EACH_ENTRY( table, &db->tables, MSITABLE, entry )
2201 r = save_table( db, table, bytes_per_strref );
2202 if( r != ERROR_SUCCESS )
2204 WARN("failed to save table %s (r=%08x)\n",
2205 debugstr_w(table->name), r);
2206 return r;
2210 hr = IStorage_Commit( db->storage, 0 );
2211 if (FAILED( hr ))
2213 WARN("failed to commit changes 0x%08x\n", hr);
2214 r = ERROR_FUNCTION_FAILED;
2216 return r;
2219 MSICONDITION MSI_DatabaseIsTablePersistent( MSIDATABASE *db, LPCWSTR table )
2221 MSITABLE *t;
2222 UINT r;
2224 TRACE("%p %s\n", db, debugstr_w(table));
2226 if (!table)
2227 return MSICONDITION_ERROR;
2229 r = get_table( db, table, &t );
2230 if (r != ERROR_SUCCESS)
2231 return MSICONDITION_NONE;
2233 return t->persistent;
2236 static UINT read_raw_int(const BYTE *data, UINT col, UINT bytes)
2238 UINT ret = 0, i;
2240 for (i = 0; i < bytes; i++)
2241 ret += (data[col + i] << i * 8);
2243 return ret;
2246 static UINT msi_record_encoded_stream_name( const MSITABLEVIEW *tv, MSIRECORD *rec, LPWSTR *pstname )
2248 LPWSTR stname = NULL, sval, p;
2249 DWORD len;
2250 UINT i, r;
2252 TRACE("%p %p\n", tv, rec);
2254 len = lstrlenW( tv->name ) + 1;
2255 stname = msi_alloc( len*sizeof(WCHAR) );
2256 if ( !stname )
2258 r = ERROR_OUTOFMEMORY;
2259 goto err;
2262 lstrcpyW( stname, tv->name );
2264 for ( i = 0; i < tv->num_cols; i++ )
2266 if ( tv->columns[i].type & MSITYPE_KEY )
2268 sval = msi_dup_record_field( rec, i + 1 );
2269 if ( !sval )
2271 r = ERROR_OUTOFMEMORY;
2272 goto err;
2275 len += lstrlenW( szDot ) + lstrlenW ( sval );
2276 p = msi_realloc ( stname, len*sizeof(WCHAR) );
2277 if ( !p )
2279 r = ERROR_OUTOFMEMORY;
2280 msi_free(sval);
2281 goto err;
2283 stname = p;
2285 lstrcatW( stname, szDot );
2286 lstrcatW( stname, sval );
2288 msi_free( sval );
2290 else
2291 continue;
2294 *pstname = encode_streamname( FALSE, stname );
2295 msi_free( stname );
2297 return ERROR_SUCCESS;
2299 err:
2300 msi_free ( stname );
2301 *pstname = NULL;
2302 return r;
2305 static MSIRECORD *msi_get_transform_record( const MSITABLEVIEW *tv, const string_table *st,
2306 IStorage *stg,
2307 const BYTE *rawdata, UINT bytes_per_strref )
2309 UINT i, val, ofs = 0;
2310 USHORT mask;
2311 MSICOLUMNINFO *columns = tv->columns;
2312 MSIRECORD *rec;
2314 mask = rawdata[0] | (rawdata[1] << 8);
2315 rawdata += 2;
2317 rec = MSI_CreateRecord( tv->num_cols );
2318 if( !rec )
2319 return rec;
2321 TRACE("row ->\n");
2322 for( i=0; i<tv->num_cols; i++ )
2324 if ( (mask&1) && (i>=(mask>>8)) )
2325 break;
2326 /* all keys must be present */
2327 if ( (~mask&1) && (~columns[i].type & MSITYPE_KEY) && ((1<<i) & ~mask) )
2328 continue;
2330 if( MSITYPE_IS_BINARY(tv->columns[i].type) )
2332 LPWSTR encname;
2333 IStream *stm = NULL;
2334 UINT r;
2336 ofs += bytes_per_column( tv->db, &columns[i], bytes_per_strref );
2338 r = msi_record_encoded_stream_name( tv, rec, &encname );
2339 if ( r != ERROR_SUCCESS )
2340 return NULL;
2342 r = IStorage_OpenStream( stg, encname, NULL,
2343 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stm );
2344 if ( r != ERROR_SUCCESS )
2346 msi_free( encname );
2347 return NULL;
2350 MSI_RecordSetStream( rec, i+1, stm );
2351 TRACE(" field %d [%s]\n", i+1, debugstr_w(encname));
2352 msi_free( encname );
2354 else if( columns[i].type & MSITYPE_STRING )
2356 int len;
2357 const WCHAR *sval;
2359 val = read_raw_int(rawdata, ofs, bytes_per_strref);
2360 sval = msi_string_lookup( st, val, &len );
2361 msi_record_set_string( rec, i+1, sval, len );
2362 TRACE(" field %d [%s]\n", i+1, debugstr_wn(sval, len));
2363 ofs += bytes_per_strref;
2365 else
2367 UINT n = bytes_per_column( tv->db, &columns[i], bytes_per_strref );
2368 switch( n )
2370 case 2:
2371 val = read_raw_int(rawdata, ofs, n);
2372 if (val)
2373 MSI_RecordSetInteger( rec, i+1, val-0x8000 );
2374 TRACE(" field %d [0x%04x]\n", i+1, val );
2375 break;
2376 case 4:
2377 val = read_raw_int(rawdata, ofs, n);
2378 if (val)
2379 MSI_RecordSetInteger( rec, i+1, val^0x80000000 );
2380 TRACE(" field %d [0x%08x]\n", i+1, val );
2381 break;
2382 default:
2383 ERR("oops - unknown column width %d\n", n);
2384 break;
2386 ofs += n;
2389 return rec;
2392 static void dump_record( MSIRECORD *rec )
2394 UINT i, n;
2396 n = MSI_RecordGetFieldCount( rec );
2397 for( i=1; i<=n; i++ )
2399 int len;
2400 const WCHAR *sval;
2402 if( MSI_RecordIsNull( rec, i ) )
2403 TRACE("row -> []\n");
2404 else if( (sval = msi_record_get_string( rec, i, &len )) )
2405 TRACE("row -> [%s]\n", debugstr_wn(sval, len));
2406 else
2407 TRACE("row -> [0x%08x]\n", MSI_RecordGetInteger( rec, i ) );
2411 static void dump_table( const string_table *st, const USHORT *rawdata, UINT rawsize )
2413 UINT i;
2414 for (i = 0; i < rawsize / 2; i++)
2416 int len;
2417 const WCHAR *sval = msi_string_lookup( st, rawdata[i], &len );
2418 MESSAGE(" %04x %s\n", rawdata[i], debugstr_wn(sval, len) );
2422 static UINT* msi_record_to_row( const MSITABLEVIEW *tv, MSIRECORD *rec )
2424 UINT i, r, *data;
2426 data = msi_alloc( tv->num_cols *sizeof (UINT) );
2427 for( i=0; i<tv->num_cols; i++ )
2429 data[i] = 0;
2431 if ( ~tv->columns[i].type & MSITYPE_KEY )
2432 continue;
2434 /* turn the transform column value into a row value */
2435 if ( ( tv->columns[i].type & MSITYPE_STRING ) &&
2436 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
2438 int len;
2439 const WCHAR *str = msi_record_get_string( rec, i+1, &len );
2440 if (str)
2442 r = msi_string2id( tv->db->strings, str, len, &data[i] );
2444 /* if there's no matching string in the string table,
2445 these keys can't match any record, so fail now. */
2446 if (r != ERROR_SUCCESS)
2448 msi_free( data );
2449 return NULL;
2452 else data[i] = 0;
2454 else
2456 data[i] = MSI_RecordGetInteger( rec, i+1 );
2458 if (data[i] == MSI_NULL_INTEGER)
2459 data[i] = 0;
2460 else if ((tv->columns[i].type&0xff) == 2)
2461 data[i] += 0x8000;
2462 else
2463 data[i] += 0x80000000;
2466 return data;
2469 static UINT msi_row_matches( MSITABLEVIEW *tv, UINT row, const UINT *data, UINT *column )
2471 UINT i, r, x, ret = ERROR_FUNCTION_FAILED;
2473 for( i=0; i<tv->num_cols; i++ )
2475 if ( ~tv->columns[i].type & MSITYPE_KEY )
2476 continue;
2478 /* turn the transform column value into a row value */
2479 r = TABLE_fetch_int( &tv->view, row, i+1, &x );
2480 if ( r != ERROR_SUCCESS )
2482 ERR("TABLE_fetch_int shouldn't fail here\n");
2483 break;
2486 /* if this key matches, move to the next column */
2487 if ( x != data[i] )
2489 ret = ERROR_FUNCTION_FAILED;
2490 break;
2492 if (column) *column = i;
2493 ret = ERROR_SUCCESS;
2495 return ret;
2498 static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row, UINT *column )
2500 UINT i, r = ERROR_FUNCTION_FAILED, *data;
2502 data = msi_record_to_row( tv, rec );
2503 if( !data )
2504 return r;
2505 for( i = 0; i < tv->table->row_count; i++ )
2507 r = msi_row_matches( tv, i, data, column );
2508 if( r == ERROR_SUCCESS )
2510 *row = i;
2511 break;
2514 msi_free( data );
2515 return r;
2518 typedef struct
2520 struct list entry;
2521 LPWSTR name;
2522 } TRANSFORMDATA;
2524 static UINT msi_table_load_transform( MSIDATABASE *db, IStorage *stg,
2525 string_table *st, TRANSFORMDATA *transform,
2526 UINT bytes_per_strref )
2528 BYTE *rawdata = NULL;
2529 MSITABLEVIEW *tv = NULL;
2530 UINT r, n, sz, i, mask, num_cols, colcol = 0, rawsize = 0;
2531 MSIRECORD *rec = NULL;
2532 WCHAR coltable[32];
2533 const WCHAR *name;
2535 if (!transform)
2536 return ERROR_SUCCESS;
2538 name = transform->name;
2540 coltable[0] = 0;
2541 TRACE("%p %p %p %s\n", db, stg, st, debugstr_w(name) );
2543 /* read the transform data */
2544 read_stream_data( stg, name, TRUE, &rawdata, &rawsize );
2545 if ( !rawdata )
2547 TRACE("table %s empty\n", debugstr_w(name) );
2548 return ERROR_INVALID_TABLE;
2551 /* create a table view */
2552 r = TABLE_CreateView( db, name, (MSIVIEW**) &tv );
2553 if( r != ERROR_SUCCESS )
2554 goto err;
2556 r = tv->view.ops->execute( &tv->view, NULL );
2557 if( r != ERROR_SUCCESS )
2558 goto err;
2560 TRACE("name = %s columns = %u row_size = %u raw size = %u\n",
2561 debugstr_w(name), tv->num_cols, tv->row_size, rawsize );
2563 /* interpret the data */
2564 for (n = 0; n < rawsize;)
2566 mask = rawdata[n] | (rawdata[n + 1] << 8);
2567 if (mask & 1)
2570 * if the low bit is set, columns are continuous and
2571 * the number of columns is specified in the high byte
2573 sz = 2;
2574 num_cols = mask >> 8;
2575 if (num_cols > tv->num_cols)
2577 ERR("excess columns in transform: %u > %u\n", num_cols, tv->num_cols);
2578 break;
2581 for (i = 0; i < num_cols; i++)
2583 if( (tv->columns[i].type & MSITYPE_STRING) &&
2584 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
2585 sz += bytes_per_strref;
2586 else
2587 sz += bytes_per_column( tv->db, &tv->columns[i], bytes_per_strref );
2590 else
2593 * If the low bit is not set, mask is a bitmask.
2594 * Excepting for key fields, which are always present,
2595 * each bit indicates that a field is present in the transform record.
2597 * mask == 0 is a special case ... only the keys will be present
2598 * and it means that this row should be deleted.
2600 sz = 2;
2601 num_cols = tv->num_cols;
2602 for (i = 0; i < num_cols; i++)
2604 if ((tv->columns[i].type & MSITYPE_KEY) || ((1 << i) & mask))
2606 if ((tv->columns[i].type & MSITYPE_STRING) &&
2607 !MSITYPE_IS_BINARY(tv->columns[i].type))
2608 sz += bytes_per_strref;
2609 else
2610 sz += bytes_per_column( tv->db, &tv->columns[i], bytes_per_strref );
2615 /* check we didn't run of the end of the table */
2616 if (n + sz > rawsize)
2618 ERR("borked.\n");
2619 dump_table( st, (USHORT *)rawdata, rawsize );
2620 break;
2623 rec = msi_get_transform_record( tv, st, stg, &rawdata[n], bytes_per_strref );
2624 if (rec)
2626 WCHAR table[32];
2627 DWORD sz = 32;
2628 UINT number = MSI_NULL_INTEGER;
2629 UINT row = 0;
2631 if (!strcmpW( name, szColumns ))
2633 MSI_RecordGetStringW( rec, 1, table, &sz );
2634 number = MSI_RecordGetInteger( rec, 2 );
2637 * Native msi seems writes nul into the Number (2nd) column of
2638 * the _Columns table when there are new columns
2640 if ( number == MSI_NULL_INTEGER )
2642 /* reset the column number on a new table */
2643 if (strcmpW( coltable, table ))
2645 colcol = 0;
2646 lstrcpyW( coltable, table );
2649 /* fix nul column numbers */
2650 MSI_RecordSetInteger( rec, 2, ++colcol );
2654 if (TRACE_ON(msidb)) dump_record( rec );
2656 r = msi_table_find_row( tv, rec, &row, NULL );
2657 if (r == ERROR_SUCCESS)
2659 if (!mask)
2661 TRACE("deleting row [%d]:\n", row);
2662 r = TABLE_delete_row( &tv->view, row );
2663 if (r != ERROR_SUCCESS)
2664 WARN("failed to delete row %u\n", r);
2666 else if (mask & 1)
2668 TRACE("modifying full row [%d]:\n", row);
2669 r = TABLE_set_row( &tv->view, row, rec, (1 << tv->num_cols) - 1 );
2670 if (r != ERROR_SUCCESS)
2671 WARN("failed to modify row %u\n", r);
2673 else
2675 TRACE("modifying masked row [%d]:\n", row);
2676 r = TABLE_set_row( &tv->view, row, rec, mask );
2677 if (r != ERROR_SUCCESS)
2678 WARN("failed to modify row %u\n", r);
2681 else
2683 TRACE("inserting row\n");
2684 r = TABLE_insert_row( &tv->view, rec, -1, FALSE );
2685 if (r != ERROR_SUCCESS)
2686 WARN("failed to insert row %u\n", r);
2689 if (!strcmpW( name, szColumns ))
2690 msi_update_table_columns( db, table );
2692 msiobj_release( &rec->hdr );
2695 n += sz;
2698 err:
2699 /* no need to free the table, it's associated with the database */
2700 msi_free( rawdata );
2701 if( tv )
2702 tv->view.ops->delete( &tv->view );
2704 return ERROR_SUCCESS;
2708 * msi_table_apply_transform
2710 * Enumerate the table transforms in a transform storage and apply each one.
2712 UINT msi_table_apply_transform( MSIDATABASE *db, IStorage *stg )
2714 struct list transforms;
2715 IEnumSTATSTG *stgenum = NULL;
2716 TRANSFORMDATA *transform;
2717 TRANSFORMDATA *tables = NULL, *columns = NULL;
2718 HRESULT r;
2719 STATSTG stat;
2720 string_table *strings;
2721 UINT ret = ERROR_FUNCTION_FAILED;
2722 UINT bytes_per_strref;
2723 BOOL property_update = FALSE;
2725 TRACE("%p %p\n", db, stg );
2727 strings = msi_load_string_table( stg, &bytes_per_strref );
2728 if( !strings )
2729 goto end;
2731 r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
2732 if( FAILED( r ) )
2733 goto end;
2735 list_init(&transforms);
2737 while ( TRUE )
2739 MSITABLEVIEW *tv = NULL;
2740 WCHAR name[0x40];
2741 ULONG count = 0;
2743 r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
2744 if ( FAILED( r ) || !count )
2745 break;
2747 decode_streamname( stat.pwcsName, name );
2748 CoTaskMemFree( stat.pwcsName );
2749 if ( name[0] != 0x4840 )
2750 continue;
2752 if ( !strcmpW( name+1, szStringPool ) ||
2753 !strcmpW( name+1, szStringData ) )
2754 continue;
2756 transform = msi_alloc_zero( sizeof(TRANSFORMDATA) );
2757 if ( !transform )
2758 break;
2760 list_add_tail( &transforms, &transform->entry );
2762 transform->name = strdupW( name + 1 );
2764 if ( !strcmpW( transform->name, szTables ) )
2765 tables = transform;
2766 else if (!strcmpW( transform->name, szColumns ) )
2767 columns = transform;
2768 else if (!strcmpW( transform->name, szProperty ))
2769 property_update = TRUE;
2771 TRACE("transform contains stream %s\n", debugstr_w(name));
2773 /* load the table */
2774 r = TABLE_CreateView( db, transform->name, (MSIVIEW**) &tv );
2775 if( r != ERROR_SUCCESS )
2776 continue;
2778 r = tv->view.ops->execute( &tv->view, NULL );
2779 if( r != ERROR_SUCCESS )
2781 tv->view.ops->delete( &tv->view );
2782 continue;
2785 tv->view.ops->delete( &tv->view );
2789 * Apply _Tables and _Columns transforms first so that
2790 * the table metadata is correct, and empty tables exist.
2792 ret = msi_table_load_transform( db, stg, strings, tables, bytes_per_strref );
2793 if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
2794 goto end;
2796 ret = msi_table_load_transform( db, stg, strings, columns, bytes_per_strref );
2797 if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
2798 goto end;
2800 ret = ERROR_SUCCESS;
2802 while ( !list_empty( &transforms ) )
2804 transform = LIST_ENTRY( list_head( &transforms ), TRANSFORMDATA, entry );
2806 if ( strcmpW( transform->name, szColumns ) &&
2807 strcmpW( transform->name, szTables ) &&
2808 ret == ERROR_SUCCESS )
2810 ret = msi_table_load_transform( db, stg, strings, transform, bytes_per_strref );
2813 list_remove( &transform->entry );
2814 msi_free( transform->name );
2815 msi_free( transform );
2818 if ( ret == ERROR_SUCCESS )
2820 append_storage_to_db( db, stg );
2821 if (property_update) msi_clone_properties( db );
2824 end:
2825 if ( stgenum )
2826 IEnumSTATSTG_Release( stgenum );
2827 if ( strings )
2828 msi_destroy_stringtable( strings );
2830 return ret;