xref: /reactos/dll/win32/msi/table.c (revision f4be6dc3)
1c2c66affSColin Finck /*
2c2c66affSColin Finck  * Implementation of the Microsoft Installer (msi.dll)
3c2c66affSColin Finck  *
4c2c66affSColin Finck  * Copyright 2002-2005 Mike McCormack for CodeWeavers
5c2c66affSColin Finck  *
6c2c66affSColin Finck  * This library is free software; you can redistribute it and/or
7c2c66affSColin Finck  * modify it under the terms of the GNU Lesser General Public
8c2c66affSColin Finck  * License as published by the Free Software Foundation; either
9c2c66affSColin Finck  * version 2.1 of the License, or (at your option) any later version.
10c2c66affSColin Finck  *
11c2c66affSColin Finck  * This library is distributed in the hope that it will be useful,
12c2c66affSColin Finck  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13c2c66affSColin Finck  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14c2c66affSColin Finck  * Lesser General Public License for more details.
15c2c66affSColin Finck  *
16c2c66affSColin Finck  * You should have received a copy of the GNU Lesser General Public
17c2c66affSColin Finck  * License along with this library; if not, write to the Free Software
18c2c66affSColin Finck  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19c2c66affSColin Finck  */
20c2c66affSColin Finck 
21c42b133eSAmine Khaldi #include <stdarg.h>
22c42b133eSAmine Khaldi #include <assert.h>
23c42b133eSAmine Khaldi 
24c42b133eSAmine Khaldi #define COBJMACROS
25c42b133eSAmine Khaldi 
26c42b133eSAmine Khaldi #include "windef.h"
27c42b133eSAmine Khaldi #include "winbase.h"
28c42b133eSAmine Khaldi #include "winerror.h"
29c42b133eSAmine Khaldi #include "msi.h"
30c42b133eSAmine Khaldi #include "msiquery.h"
31c42b133eSAmine Khaldi #include "objbase.h"
32c42b133eSAmine Khaldi #include "objidl.h"
33c42b133eSAmine Khaldi #include "winnls.h"
34c2c66affSColin Finck #include "msipriv.h"
35c42b133eSAmine Khaldi #include "query.h"
36c42b133eSAmine Khaldi 
37c42b133eSAmine Khaldi #include "wine/debug.h"
38c2c66affSColin Finck 
39c2c66affSColin Finck WINE_DEFAULT_DEBUG_CHANNEL(msidb);
40c2c66affSColin Finck 
41c2c66affSColin Finck #define MSITABLE_HASH_TABLE_SIZE 37
42c2c66affSColin Finck 
43*f4be6dc3SMikhail struct column_hash_entry
44c2c66affSColin Finck {
45*f4be6dc3SMikhail     struct column_hash_entry *next;
46c2c66affSColin Finck     UINT value;
47c2c66affSColin Finck     UINT row;
48*f4be6dc3SMikhail };
49c2c66affSColin Finck 
50*f4be6dc3SMikhail struct column_info
51c2c66affSColin Finck {
52c2c66affSColin Finck     LPCWSTR tablename;
53c2c66affSColin Finck     UINT    number;
54c2c66affSColin Finck     LPCWSTR colname;
55c2c66affSColin Finck     UINT    type;
56c2c66affSColin Finck     UINT    offset;
57*f4be6dc3SMikhail     struct column_hash_entry **hash_table;
58*f4be6dc3SMikhail };
59c2c66affSColin Finck 
60c2c66affSColin Finck struct tagMSITABLE
61c2c66affSColin Finck {
62c2c66affSColin Finck     BYTE **data;
63c2c66affSColin Finck     BOOL *data_persistent;
64c2c66affSColin Finck     UINT row_count;
65c2c66affSColin Finck     struct list entry;
66*f4be6dc3SMikhail     struct column_info *colinfo;
67c2c66affSColin Finck     UINT col_count;
68c2c66affSColin Finck     MSICONDITION persistent;
6936bf5549Swinesync     LONG ref_count;
70c2c66affSColin Finck     WCHAR name[1];
71c2c66affSColin Finck };
72c2c66affSColin Finck 
73c2c66affSColin Finck /* information for default tables */
74*f4be6dc3SMikhail static const struct column_info _Columns_cols[4] =
75*f4be6dc3SMikhail {
7674f53b4bSwinesync     { L"_Columns", 1, L"Table",  MSITYPE_VALID | MSITYPE_STRING | MSITYPE_KEY | 64, 0, NULL },
7774f53b4bSwinesync     { L"_Columns", 2, L"Number", MSITYPE_VALID | MSITYPE_KEY | 2,     2, NULL },
7874f53b4bSwinesync     { L"_Columns", 3, L"Name",   MSITYPE_VALID | MSITYPE_STRING | 64, 4, NULL },
7974f53b4bSwinesync     { L"_Columns", 4, L"Type",   MSITYPE_VALID | 2,                   6, NULL },
80c2c66affSColin Finck };
81c2c66affSColin Finck 
82*f4be6dc3SMikhail static const struct column_info _Tables_cols[1] =
83*f4be6dc3SMikhail {
8474f53b4bSwinesync     { L"_Tables",  1, L"Name",   MSITYPE_VALID | MSITYPE_STRING | MSITYPE_KEY | 64, 0, NULL },
85c2c66affSColin Finck };
86c2c66affSColin Finck 
87c2c66affSColin Finck #define MAX_STREAM_NAME 0x1f
88c2c66affSColin Finck 
bytes_per_column(MSIDATABASE * db,const struct column_info * col,UINT bytes_per_strref)89*f4be6dc3SMikhail static inline UINT bytes_per_column( MSIDATABASE *db, const struct column_info *col, UINT bytes_per_strref )
90c2c66affSColin Finck {
91c2c66affSColin Finck     if( MSITYPE_IS_BINARY(col->type) )
92c2c66affSColin Finck         return 2;
93c2c66affSColin Finck 
94c2c66affSColin Finck     if( col->type & MSITYPE_STRING )
95c2c66affSColin Finck         return bytes_per_strref;
96c2c66affSColin Finck 
97c2c66affSColin Finck     if( (col->type & 0xff) <= 2)
98c2c66affSColin Finck         return 2;
99c2c66affSColin Finck 
100c2c66affSColin Finck     if( (col->type & 0xff) != 4 )
101c2c66affSColin Finck         ERR("Invalid column size %u\n", col->type & 0xff);
102c2c66affSColin Finck 
103c2c66affSColin Finck     return 4;
104c2c66affSColin Finck }
105c2c66affSColin Finck 
utf2mime(int x)106c2c66affSColin Finck static int utf2mime(int x)
107c2c66affSColin Finck {
108c2c66affSColin Finck     if( (x>='0') && (x<='9') )
109c2c66affSColin Finck         return x-'0';
110c2c66affSColin Finck     if( (x>='A') && (x<='Z') )
111c2c66affSColin Finck         return x-'A'+10;
112c2c66affSColin Finck     if( (x>='a') && (x<='z') )
113c2c66affSColin Finck         return x-'a'+10+26;
114c2c66affSColin Finck     if( x=='.' )
115c2c66affSColin Finck         return 10+26+26;
116c2c66affSColin Finck     if( x=='_' )
117c2c66affSColin Finck         return 10+26+26+1;
118c2c66affSColin Finck     return -1;
119c2c66affSColin Finck }
120c2c66affSColin Finck 
encode_streamname(BOOL bTable,LPCWSTR in)121c2c66affSColin Finck LPWSTR encode_streamname(BOOL bTable, LPCWSTR in)
122c2c66affSColin Finck {
123c2c66affSColin Finck     DWORD count = MAX_STREAM_NAME;
124c2c66affSColin Finck     DWORD ch, next;
125c2c66affSColin Finck     LPWSTR out, p;
126c2c66affSColin Finck 
127c2c66affSColin Finck     if( !bTable )
128c2c66affSColin Finck         count = lstrlenW( in )+2;
129*f4be6dc3SMikhail     if (!(out = malloc( count * sizeof(WCHAR) ))) return NULL;
130c2c66affSColin Finck     p = out;
131c2c66affSColin Finck 
132c2c66affSColin Finck     if( bTable )
133c2c66affSColin Finck     {
134c2c66affSColin Finck          *p++ = 0x4840;
135c2c66affSColin Finck          count --;
136c2c66affSColin Finck     }
137c2c66affSColin Finck     while( count -- )
138c2c66affSColin Finck     {
139c2c66affSColin Finck         ch = *in++;
140c2c66affSColin Finck         if( !ch )
141c2c66affSColin Finck         {
142c2c66affSColin Finck             *p = ch;
143c2c66affSColin Finck             return out;
144c2c66affSColin Finck         }
145c2c66affSColin Finck         if( ( ch < 0x80 ) && ( utf2mime(ch) >= 0 ) )
146c2c66affSColin Finck         {
147c2c66affSColin Finck             ch = utf2mime(ch) + 0x4800;
148c2c66affSColin Finck             next = *in;
149c2c66affSColin Finck             if( next && (next<0x80) )
150c2c66affSColin Finck             {
151c2c66affSColin Finck                 next = utf2mime(next);
152c2c66affSColin Finck                 if( next != -1 )
153c2c66affSColin Finck                 {
154c2c66affSColin Finck                      next += 0x3ffffc0;
155c2c66affSColin Finck                      ch += (next<<6);
156c2c66affSColin Finck                      in++;
157c2c66affSColin Finck                 }
158c2c66affSColin Finck             }
159c2c66affSColin Finck         }
160c2c66affSColin Finck         *p++ = ch;
161c2c66affSColin Finck     }
162c2c66affSColin Finck     ERR("Failed to encode stream name (%s)\n",debugstr_w(in));
163*f4be6dc3SMikhail     free( out );
164c2c66affSColin Finck     return NULL;
165c2c66affSColin Finck }
166c2c66affSColin Finck 
mime2utf(int x)167c2c66affSColin Finck static int mime2utf(int x)
168c2c66affSColin Finck {
169c2c66affSColin Finck     if( x<10 )
170c2c66affSColin Finck         return x + '0';
171c2c66affSColin Finck     if( x<(10+26))
172c2c66affSColin Finck         return x - 10 + 'A';
173c2c66affSColin Finck     if( x<(10+26+26))
174c2c66affSColin Finck         return x - 10 - 26 + 'a';
175c2c66affSColin Finck     if( x == (10+26+26) )
176c2c66affSColin Finck         return '.';
177c2c66affSColin Finck     return '_';
178c2c66affSColin Finck }
179c2c66affSColin Finck 
decode_streamname(LPCWSTR in,LPWSTR out)180c2c66affSColin Finck BOOL decode_streamname(LPCWSTR in, LPWSTR out)
181c2c66affSColin Finck {
182c2c66affSColin Finck     WCHAR ch;
183c2c66affSColin Finck     DWORD count = 0;
184c2c66affSColin Finck 
185c2c66affSColin Finck     while ( (ch = *in++) )
186c2c66affSColin Finck     {
187c2c66affSColin Finck         if( (ch >= 0x3800 ) && (ch < 0x4840 ) )
188c2c66affSColin Finck         {
189c2c66affSColin Finck             if( ch >= 0x4800 )
190c2c66affSColin Finck                 ch = mime2utf(ch-0x4800);
191c2c66affSColin Finck             else
192c2c66affSColin Finck             {
193c2c66affSColin Finck                 ch -= 0x3800;
194c2c66affSColin Finck                 *out++ = mime2utf(ch&0x3f);
195c2c66affSColin Finck                 count++;
196c2c66affSColin Finck                 ch = mime2utf((ch>>6)&0x3f);
197c2c66affSColin Finck             }
198c2c66affSColin Finck         }
199c2c66affSColin Finck         *out++ = ch;
200c2c66affSColin Finck         count++;
201c2c66affSColin Finck     }
202c2c66affSColin Finck     *out = 0;
203c2c66affSColin Finck     return count;
204c2c66affSColin Finck }
205c2c66affSColin Finck 
enum_stream_names(IStorage * stg)206c2c66affSColin Finck void enum_stream_names( IStorage *stg )
207c2c66affSColin Finck {
208c2c66affSColin Finck     IEnumSTATSTG *stgenum = NULL;
209c2c66affSColin Finck     HRESULT r;
210c2c66affSColin Finck     STATSTG stat;
211c2c66affSColin Finck     ULONG n, count;
212c2c66affSColin Finck     WCHAR name[0x40];
213c2c66affSColin Finck 
214c2c66affSColin Finck     r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
215c2c66affSColin Finck     if( FAILED( r ) )
216c2c66affSColin Finck         return;
217c2c66affSColin Finck 
218c2c66affSColin Finck     n = 0;
219c2c66affSColin Finck     while( 1 )
220c2c66affSColin Finck     {
221c2c66affSColin Finck         count = 0;
222c2c66affSColin Finck         r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
223c2c66affSColin Finck         if( FAILED( r ) || !count )
224c2c66affSColin Finck             break;
225c2c66affSColin Finck         decode_streamname( stat.pwcsName, name );
22602f995b2Swinesync         TRACE( "stream %2lu -> %s %s\n", n, debugstr_w(stat.pwcsName), debugstr_w(name) );
227c2c66affSColin Finck         CoTaskMemFree( stat.pwcsName );
228c2c66affSColin Finck         n++;
229c2c66affSColin Finck     }
230c2c66affSColin Finck 
231c2c66affSColin Finck     IEnumSTATSTG_Release( stgenum );
232c2c66affSColin Finck }
233c2c66affSColin Finck 
read_stream_data(IStorage * stg,LPCWSTR stname,BOOL table,BYTE ** pdata,UINT * psz)234c2c66affSColin Finck UINT read_stream_data( IStorage *stg, LPCWSTR stname, BOOL table,
235c2c66affSColin Finck                        BYTE **pdata, UINT *psz )
236c2c66affSColin Finck {
237c2c66affSColin Finck     HRESULT r;
238c2c66affSColin Finck     UINT ret = ERROR_FUNCTION_FAILED;
239c2c66affSColin Finck     VOID *data;
240c2c66affSColin Finck     ULONG sz, count;
241c2c66affSColin Finck     IStream *stm = NULL;
242c2c66affSColin Finck     STATSTG stat;
243c2c66affSColin Finck     LPWSTR encname;
244c2c66affSColin Finck 
245c2c66affSColin Finck     encname = encode_streamname(table, stname);
246c2c66affSColin Finck 
247c2c66affSColin Finck     TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
248c2c66affSColin Finck 
249c2c66affSColin Finck     r = IStorage_OpenStream(stg, encname, NULL,
250c2c66affSColin Finck             STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stm);
251*f4be6dc3SMikhail     free(encname);
252c2c66affSColin Finck     if( FAILED( r ) )
253c2c66affSColin Finck     {
25402f995b2Swinesync         WARN( "open stream failed r = %#lx - empty table?\n", r );
255c2c66affSColin Finck         return ret;
256c2c66affSColin Finck     }
257c2c66affSColin Finck 
258c2c66affSColin Finck     r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
259c2c66affSColin Finck     if( FAILED( r ) )
260c2c66affSColin Finck     {
26102f995b2Swinesync         WARN( "open stream failed r = %#lx!\n", r );
262c2c66affSColin Finck         goto end;
263c2c66affSColin Finck     }
264c2c66affSColin Finck 
265c2c66affSColin Finck     if( stat.cbSize.QuadPart >> 32 )
266c2c66affSColin Finck     {
267c2c66affSColin Finck         WARN("Too big!\n");
268c2c66affSColin Finck         goto end;
269c2c66affSColin Finck     }
270c2c66affSColin Finck 
271c2c66affSColin Finck     sz = stat.cbSize.QuadPart;
272*f4be6dc3SMikhail     data = malloc(sz);
273c2c66affSColin Finck     if( !data )
274c2c66affSColin Finck     {
27502f995b2Swinesync         WARN( "couldn't allocate memory r = %#lx!\n", r );
276c2c66affSColin Finck         ret = ERROR_NOT_ENOUGH_MEMORY;
277c2c66affSColin Finck         goto end;
278c2c66affSColin Finck     }
279c2c66affSColin Finck 
280c2c66affSColin Finck     r = IStream_Read(stm, data, sz, &count );
281c2c66affSColin Finck     if( FAILED( r ) || ( count != sz ) )
282c2c66affSColin Finck     {
283*f4be6dc3SMikhail         free(data);
28402f995b2Swinesync         WARN("read stream failed r = %#lx!\n", r);
285c2c66affSColin Finck         goto end;
286c2c66affSColin Finck     }
287c2c66affSColin Finck 
288c2c66affSColin Finck     *pdata = data;
289c2c66affSColin Finck     *psz = sz;
290c2c66affSColin Finck     ret = ERROR_SUCCESS;
291c2c66affSColin Finck 
292c2c66affSColin Finck end:
293c2c66affSColin Finck     IStream_Release( stm );
294c2c66affSColin Finck 
295c2c66affSColin Finck     return ret;
296c2c66affSColin Finck }
297c2c66affSColin Finck 
write_stream_data(IStorage * stg,LPCWSTR stname,LPCVOID data,UINT sz,BOOL bTable)298c2c66affSColin Finck UINT write_stream_data( IStorage *stg, LPCWSTR stname,
299c2c66affSColin Finck                         LPCVOID data, UINT sz, BOOL bTable )
300c2c66affSColin Finck {
301c2c66affSColin Finck     HRESULT r;
302c2c66affSColin Finck     UINT ret = ERROR_FUNCTION_FAILED;
303c2c66affSColin Finck     ULONG count;
304c2c66affSColin Finck     IStream *stm = NULL;
305c2c66affSColin Finck     ULARGE_INTEGER size;
306c2c66affSColin Finck     LARGE_INTEGER pos;
307c2c66affSColin Finck     LPWSTR encname;
308c2c66affSColin Finck 
309c2c66affSColin Finck     encname = encode_streamname(bTable, stname );
310c2c66affSColin Finck     r = IStorage_OpenStream( stg, encname, NULL,
311c2c66affSColin Finck             STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, &stm);
312c2c66affSColin Finck     if( FAILED(r) )
313c2c66affSColin Finck     {
314c2c66affSColin Finck         r = IStorage_CreateStream( stg, encname,
315c2c66affSColin Finck                 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
316c2c66affSColin Finck     }
317*f4be6dc3SMikhail     free( encname );
318c2c66affSColin Finck     if( FAILED( r ) )
319c2c66affSColin Finck     {
32002f995b2Swinesync         WARN( "open stream failed r = %#lx\n", r );
321c2c66affSColin Finck         return ret;
322c2c66affSColin Finck     }
323c2c66affSColin Finck 
324c2c66affSColin Finck     size.QuadPart = sz;
325c2c66affSColin Finck     r = IStream_SetSize( stm, size );
326c2c66affSColin Finck     if( FAILED( r ) )
327c2c66affSColin Finck     {
328c2c66affSColin Finck         WARN("Failed to SetSize\n");
329c2c66affSColin Finck         goto end;
330c2c66affSColin Finck     }
331c2c66affSColin Finck 
332c2c66affSColin Finck     pos.QuadPart = 0;
333c2c66affSColin Finck     r = IStream_Seek( stm, pos, STREAM_SEEK_SET, NULL );
334c2c66affSColin Finck     if( FAILED( r ) )
335c2c66affSColin Finck     {
336c2c66affSColin Finck         WARN("Failed to Seek\n");
337c2c66affSColin Finck         goto end;
338c2c66affSColin Finck     }
339c2c66affSColin Finck 
340c2c66affSColin Finck     if (sz)
341c2c66affSColin Finck     {
342c2c66affSColin Finck         r = IStream_Write(stm, data, sz, &count );
343c2c66affSColin Finck         if( FAILED( r ) || ( count != sz ) )
344c2c66affSColin Finck         {
345c2c66affSColin Finck             WARN("Failed to Write\n");
346c2c66affSColin Finck             goto end;
347c2c66affSColin Finck         }
348c2c66affSColin Finck     }
349c2c66affSColin Finck 
350c2c66affSColin Finck     ret = ERROR_SUCCESS;
351c2c66affSColin Finck 
352c2c66affSColin Finck end:
353c2c66affSColin Finck     IStream_Release( stm );
354c2c66affSColin Finck 
355c2c66affSColin Finck     return ret;
356c2c66affSColin Finck }
357c2c66affSColin Finck 
free_colinfo(struct column_info * colinfo,UINT count)358*f4be6dc3SMikhail static void free_colinfo( struct column_info *colinfo, UINT count )
359c2c66affSColin Finck {
360c2c66affSColin Finck     UINT i;
361*f4be6dc3SMikhail     for (i = 0; i < count; i++) free( colinfo[i].hash_table );
362c2c66affSColin Finck }
363c2c66affSColin Finck 
free_table(MSITABLE * table)364c2c66affSColin Finck static void free_table( MSITABLE *table )
365c2c66affSColin Finck {
366c2c66affSColin Finck     UINT i;
367c2c66affSColin Finck     for( i=0; i<table->row_count; i++ )
368*f4be6dc3SMikhail         free( table->data[i] );
369*f4be6dc3SMikhail     free( table->data );
370*f4be6dc3SMikhail     free( table->data_persistent );
371*f4be6dc3SMikhail     free_colinfo( table->colinfo, table->col_count );
372*f4be6dc3SMikhail     free( table->colinfo );
373*f4be6dc3SMikhail     free( table );
374c2c66affSColin Finck }
375c2c66affSColin Finck 
table_get_row_size(MSIDATABASE * db,const struct column_info * cols,UINT count,UINT bytes_per_strref)376*f4be6dc3SMikhail static UINT table_get_row_size( MSIDATABASE *db, const struct column_info *cols, UINT count, UINT bytes_per_strref )
377c2c66affSColin Finck {
378*f4be6dc3SMikhail     const struct column_info *last_col;
379c2c66affSColin Finck 
380c2c66affSColin Finck     if (!count)
381c2c66affSColin Finck         return 0;
382c2c66affSColin Finck 
383c2c66affSColin Finck     if (bytes_per_strref != LONG_STR_BYTES)
384c2c66affSColin Finck     {
385c2c66affSColin Finck         UINT i, size = 0;
386c2c66affSColin Finck         for (i = 0; i < count; i++) size += bytes_per_column( db, &cols[i], bytes_per_strref );
387c2c66affSColin Finck         return size;
388c2c66affSColin Finck     }
389c2c66affSColin Finck     last_col = &cols[count - 1];
390c2c66affSColin Finck     return last_col->offset + bytes_per_column( db, last_col, bytes_per_strref );
391c2c66affSColin Finck }
392c2c66affSColin Finck 
393c2c66affSColin Finck /* add this table to the list of cached tables in the database */
read_table_from_storage(MSIDATABASE * db,MSITABLE * t,IStorage * stg)394c2c66affSColin Finck static UINT read_table_from_storage( MSIDATABASE *db, MSITABLE *t, IStorage *stg )
395c2c66affSColin Finck {
396c2c66affSColin Finck     BYTE *rawdata = NULL;
397c2c66affSColin Finck     UINT rawsize = 0, i, j, row_size, row_size_mem;
398c2c66affSColin Finck 
399c2c66affSColin Finck     TRACE("%s\n",debugstr_w(t->name));
400c2c66affSColin Finck 
401*f4be6dc3SMikhail     row_size = table_get_row_size( db, t->colinfo, t->col_count, db->bytes_per_strref );
402*f4be6dc3SMikhail     row_size_mem = table_get_row_size( db, t->colinfo, t->col_count, LONG_STR_BYTES );
403c2c66affSColin Finck 
404c2c66affSColin Finck     /* if we can't read the table, just assume that it's empty */
405c2c66affSColin Finck     read_stream_data( stg, t->name, TRUE, &rawdata, &rawsize );
406c2c66affSColin Finck     if( !rawdata )
407c2c66affSColin Finck         return ERROR_SUCCESS;
408c2c66affSColin Finck 
409c2c66affSColin Finck     TRACE("Read %d bytes\n", rawsize );
410c2c66affSColin Finck 
411c2c66affSColin Finck     if( rawsize % row_size )
412c2c66affSColin Finck     {
413c2c66affSColin Finck         WARN("Table size is invalid %d/%d\n", rawsize, row_size );
414c2c66affSColin Finck         goto err;
415c2c66affSColin Finck     }
416c2c66affSColin Finck 
417c2c66affSColin Finck     if ((t->row_count = rawsize / row_size))
418c2c66affSColin Finck     {
419*f4be6dc3SMikhail         if (!(t->data = calloc( t->row_count, sizeof(USHORT *) ))) goto err;
420*f4be6dc3SMikhail         if (!(t->data_persistent = calloc( t->row_count, sizeof(BOOL) ))) goto err;
421c2c66affSColin Finck     }
422c2c66affSColin Finck 
423c2c66affSColin Finck     /* transpose all the data */
424c2c66affSColin Finck     TRACE("Transposing data from %d rows\n", t->row_count );
425c2c66affSColin Finck     for (i = 0; i < t->row_count; i++)
426c2c66affSColin Finck     {
427c2c66affSColin Finck         UINT ofs = 0, ofs_mem = 0;
428c2c66affSColin Finck 
429*f4be6dc3SMikhail         t->data[i] = malloc( row_size_mem );
430c2c66affSColin Finck         if( !t->data[i] )
431c2c66affSColin Finck             goto err;
432c2c66affSColin Finck         t->data_persistent[i] = TRUE;
433c2c66affSColin Finck 
434c2c66affSColin Finck         for (j = 0; j < t->col_count; j++)
435c2c66affSColin Finck         {
436c2c66affSColin Finck             UINT m = bytes_per_column( db, &t->colinfo[j], LONG_STR_BYTES );
437c2c66affSColin Finck             UINT n = bytes_per_column( db, &t->colinfo[j], db->bytes_per_strref );
438c2c66affSColin Finck             UINT k;
439c2c66affSColin Finck 
440c2c66affSColin Finck             if ( n != 2 && n != 3 && n != 4 )
441c2c66affSColin Finck             {
442c2c66affSColin Finck                 ERR("oops - unknown column width %d\n", n);
443c2c66affSColin Finck                 goto err;
444c2c66affSColin Finck             }
445c2c66affSColin Finck             if (t->colinfo[j].type & MSITYPE_STRING && n < m)
446c2c66affSColin Finck             {
447c2c66affSColin Finck                 for (k = 0; k < m; k++)
448c2c66affSColin Finck                 {
449c2c66affSColin Finck                     if (k < n)
450c2c66affSColin Finck                         t->data[i][ofs_mem + k] = rawdata[ofs * t->row_count + i * n + k];
451c2c66affSColin Finck                     else
452c2c66affSColin Finck                         t->data[i][ofs_mem + k] = 0;
453c2c66affSColin Finck                 }
454c2c66affSColin Finck             }
455c2c66affSColin Finck             else
456c2c66affSColin Finck             {
457c2c66affSColin Finck                 for (k = 0; k < n; k++)
458c2c66affSColin Finck                     t->data[i][ofs_mem + k] = rawdata[ofs * t->row_count + i * n + k];
459c2c66affSColin Finck             }
460c2c66affSColin Finck             ofs_mem += m;
461c2c66affSColin Finck             ofs += n;
462c2c66affSColin Finck         }
463c2c66affSColin Finck     }
464c2c66affSColin Finck 
465*f4be6dc3SMikhail     free( rawdata );
466c2c66affSColin Finck     return ERROR_SUCCESS;
467c2c66affSColin Finck err:
468*f4be6dc3SMikhail     free( rawdata );
469c2c66affSColin Finck     return ERROR_FUNCTION_FAILED;
470c2c66affSColin Finck }
471c2c66affSColin Finck 
free_cached_tables(MSIDATABASE * db)472c2c66affSColin Finck void free_cached_tables( MSIDATABASE *db )
473c2c66affSColin Finck {
474c2c66affSColin Finck     while( !list_empty( &db->tables ) )
475c2c66affSColin Finck     {
476c2c66affSColin Finck         MSITABLE *t = LIST_ENTRY( list_head( &db->tables ), MSITABLE, entry );
477c2c66affSColin Finck 
478c2c66affSColin Finck         list_remove( &t->entry );
479c2c66affSColin Finck         free_table( t );
480c2c66affSColin Finck     }
481c2c66affSColin Finck }
482c2c66affSColin Finck 
find_cached_table(MSIDATABASE * db,LPCWSTR name)483c2c66affSColin Finck static MSITABLE *find_cached_table( MSIDATABASE *db, LPCWSTR name )
484c2c66affSColin Finck {
485c2c66affSColin Finck     MSITABLE *t;
486c2c66affSColin Finck 
487c2c66affSColin Finck     LIST_FOR_EACH_ENTRY( t, &db->tables, MSITABLE, entry )
488958f1addSwinesync         if( !wcscmp( name, t->name ) )
489c2c66affSColin Finck             return t;
490c2c66affSColin Finck 
491c2c66affSColin Finck     return NULL;
492c2c66affSColin Finck }
493c2c66affSColin Finck 
table_calc_column_offsets(MSIDATABASE * db,struct column_info * colinfo,DWORD count)494*f4be6dc3SMikhail static void table_calc_column_offsets( MSIDATABASE *db, struct column_info *colinfo, DWORD count )
495c2c66affSColin Finck {
496c2c66affSColin Finck     DWORD i;
497c2c66affSColin Finck 
498c2c66affSColin Finck     for (i = 0; colinfo && i < count; i++)
499c2c66affSColin Finck     {
500c2c66affSColin Finck          assert( i + 1 == colinfo[i].number );
501c2c66affSColin Finck          if (i) colinfo[i].offset = colinfo[i - 1].offset +
502c2c66affSColin Finck                                     bytes_per_column( db, &colinfo[i - 1], LONG_STR_BYTES );
503c2c66affSColin Finck          else colinfo[i].offset = 0;
504c2c66affSColin Finck 
505c2c66affSColin Finck          TRACE("column %d is [%s] with type %08x ofs %d\n",
506c2c66affSColin Finck                colinfo[i].number, debugstr_w(colinfo[i].colname),
507c2c66affSColin Finck                colinfo[i].type, colinfo[i].offset);
508c2c66affSColin Finck     }
509c2c66affSColin Finck }
510c2c66affSColin Finck 
get_defaulttablecolumns(MSIDATABASE * db,const WCHAR * name,struct column_info * colinfo,UINT * sz)511*f4be6dc3SMikhail static UINT get_defaulttablecolumns( MSIDATABASE *db, const WCHAR *name, struct column_info *colinfo, UINT *sz )
512c2c66affSColin Finck {
513*f4be6dc3SMikhail     const struct column_info *p;
514c2c66affSColin Finck     DWORD i, n;
515c2c66affSColin Finck 
516c2c66affSColin Finck     TRACE("%s\n", debugstr_w(name));
517c2c66affSColin Finck 
51874f53b4bSwinesync     if (!wcscmp( name, L"_Tables" ))
519c2c66affSColin Finck     {
520c2c66affSColin Finck         p = _Tables_cols;
521c2c66affSColin Finck         n = 1;
522c2c66affSColin Finck     }
52374f53b4bSwinesync     else if (!wcscmp( name, L"_Columns" ))
524c2c66affSColin Finck     {
525c2c66affSColin Finck         p = _Columns_cols;
526c2c66affSColin Finck         n = 4;
527c2c66affSColin Finck     }
528c2c66affSColin Finck     else return ERROR_FUNCTION_FAILED;
529c2c66affSColin Finck 
530c2c66affSColin Finck     for (i = 0; i < n; i++)
531c2c66affSColin Finck     {
532c2c66affSColin Finck         if (colinfo && i < *sz) colinfo[i] = p[i];
533c2c66affSColin Finck         if (colinfo && i >= *sz) break;
534c2c66affSColin Finck     }
535c2c66affSColin Finck     table_calc_column_offsets( db, colinfo, n );
536c2c66affSColin Finck     *sz = n;
537c2c66affSColin Finck     return ERROR_SUCCESS;
538c2c66affSColin Finck }
539c2c66affSColin Finck 
540*f4be6dc3SMikhail static UINT get_tablecolumns( MSIDATABASE *, const WCHAR *, struct column_info *, UINT * );
541c2c66affSColin Finck 
table_get_column_info(MSIDATABASE * db,const WCHAR * name,struct column_info ** pcols,UINT * pcount)542*f4be6dc3SMikhail static UINT table_get_column_info( MSIDATABASE *db, const WCHAR *name, struct column_info **pcols, UINT *pcount )
543c2c66affSColin Finck {
544c2c66affSColin Finck     UINT r, column_count = 0;
545*f4be6dc3SMikhail     struct column_info *columns;
546c2c66affSColin Finck 
547c2c66affSColin Finck     /* get the number of columns in this table */
548c2c66affSColin Finck     column_count = 0;
549c2c66affSColin Finck     r = get_tablecolumns( db, name, NULL, &column_count );
550c2c66affSColin Finck     if (r != ERROR_SUCCESS)
551c2c66affSColin Finck         return r;
552c2c66affSColin Finck 
553c2c66affSColin Finck     *pcount = column_count;
554c2c66affSColin Finck 
555c2c66affSColin Finck     /* if there are no columns, there's no table */
556c2c66affSColin Finck     if (!column_count)
557c2c66affSColin Finck         return ERROR_INVALID_PARAMETER;
558c2c66affSColin Finck 
559c2c66affSColin Finck     TRACE("table %s found\n", debugstr_w(name));
560c2c66affSColin Finck 
561*f4be6dc3SMikhail     columns = malloc( column_count * sizeof(*columns) );
562c2c66affSColin Finck     if (!columns)
563c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
564c2c66affSColin Finck 
565c2c66affSColin Finck     r = get_tablecolumns( db, name, columns, &column_count );
566c2c66affSColin Finck     if (r != ERROR_SUCCESS)
567c2c66affSColin Finck     {
568*f4be6dc3SMikhail         free( columns );
569c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
570c2c66affSColin Finck     }
571c2c66affSColin Finck     *pcols = columns;
572c2c66affSColin Finck     return r;
573c2c66affSColin Finck }
574c2c66affSColin Finck 
get_table(MSIDATABASE * db,LPCWSTR name,MSITABLE ** table_ret)575c2c66affSColin Finck static UINT get_table( MSIDATABASE *db, LPCWSTR name, MSITABLE **table_ret )
576c2c66affSColin Finck {
577c2c66affSColin Finck     MSITABLE *table;
578c2c66affSColin Finck     UINT r;
579c2c66affSColin Finck 
580c2c66affSColin Finck     /* first, see if the table is cached */
581c2c66affSColin Finck     table = find_cached_table( db, name );
582c2c66affSColin Finck     if (table)
583c2c66affSColin Finck     {
584c2c66affSColin Finck         *table_ret = table;
585c2c66affSColin Finck         return ERROR_SUCCESS;
586c2c66affSColin Finck     }
587c2c66affSColin Finck 
588c2c66affSColin Finck     /* nonexistent tables should be interpreted as empty tables */
589*f4be6dc3SMikhail     table = malloc( sizeof(MSITABLE) + wcslen( name ) * sizeof(WCHAR) );
590c2c66affSColin Finck     if (!table)
591c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
592c2c66affSColin Finck 
593c2c66affSColin Finck     table->row_count = 0;
594c2c66affSColin Finck     table->data = NULL;
595c2c66affSColin Finck     table->data_persistent = NULL;
596c2c66affSColin Finck     table->colinfo = NULL;
597c2c66affSColin Finck     table->col_count = 0;
598c2c66affSColin Finck     table->persistent = MSICONDITION_TRUE;
599c2c66affSColin Finck     lstrcpyW( table->name, name );
600c2c66affSColin Finck 
60174f53b4bSwinesync     if (!wcscmp( name, L"_Tables" ) || !wcscmp( name, L"_Columns" ))
602c2c66affSColin Finck         table->persistent = MSICONDITION_NONE;
603c2c66affSColin Finck 
604c2c66affSColin Finck     r = table_get_column_info( db, name, &table->colinfo, &table->col_count );
605c2c66affSColin Finck     if (r != ERROR_SUCCESS)
606c2c66affSColin Finck     {
607c2c66affSColin Finck         free_table( table );
608c2c66affSColin Finck         return r;
609c2c66affSColin Finck     }
610c2c66affSColin Finck     r = read_table_from_storage( db, table, db->storage );
611c2c66affSColin Finck     if (r != ERROR_SUCCESS)
612c2c66affSColin Finck     {
613c2c66affSColin Finck         free_table( table );
614c2c66affSColin Finck         return r;
615c2c66affSColin Finck     }
616c2c66affSColin Finck     list_add_head( &db->tables, &table->entry );
617c2c66affSColin Finck     *table_ret = table;
618c2c66affSColin Finck     return ERROR_SUCCESS;
619c2c66affSColin Finck }
620c2c66affSColin Finck 
read_table_int(BYTE * const * data,UINT row,UINT col,UINT bytes)621c2c66affSColin Finck static UINT read_table_int( BYTE *const *data, UINT row, UINT col, UINT bytes )
622c2c66affSColin Finck {
623c2c66affSColin Finck     UINT ret = 0, i;
624c2c66affSColin Finck 
625c2c66affSColin Finck     for (i = 0; i < bytes; i++)
626c2c66affSColin Finck         ret += data[row][col + i] << i * 8;
627c2c66affSColin Finck 
628c2c66affSColin Finck     return ret;
629c2c66affSColin Finck }
630c2c66affSColin Finck 
get_tablecolumns(MSIDATABASE * db,const WCHAR * szTableName,struct column_info * colinfo,UINT * sz)631*f4be6dc3SMikhail static UINT get_tablecolumns( MSIDATABASE *db, const WCHAR *szTableName, struct column_info *colinfo, UINT *sz )
632c2c66affSColin Finck {
633c2c66affSColin Finck     UINT r, i, n = 0, table_id, count, maxcount = *sz;
634c2c66affSColin Finck     MSITABLE *table = NULL;
635c2c66affSColin Finck 
636c2c66affSColin Finck     TRACE("%s\n", debugstr_w(szTableName));
637c2c66affSColin Finck 
638c2c66affSColin Finck     /* first check if there is a default table with that name */
639c2c66affSColin Finck     r = get_defaulttablecolumns( db, szTableName, colinfo, sz );
640c2c66affSColin Finck     if (r == ERROR_SUCCESS && *sz)
641c2c66affSColin Finck         return r;
642c2c66affSColin Finck 
64374f53b4bSwinesync     r = get_table( db, L"_Columns", &table );
644c2c66affSColin Finck     if (r != ERROR_SUCCESS)
645c2c66affSColin Finck     {
646c2c66affSColin Finck         ERR("couldn't load _Columns table\n");
647c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
648c2c66affSColin Finck     }
649c2c66affSColin Finck 
650c2c66affSColin Finck     /* convert table and column names to IDs from the string table */
651c2c66affSColin Finck     r = msi_string2id( db->strings, szTableName, -1, &table_id );
652c2c66affSColin Finck     if (r != ERROR_SUCCESS)
653c2c66affSColin Finck     {
654c2c66affSColin Finck         WARN("Couldn't find id for %s\n", debugstr_w(szTableName));
655c2c66affSColin Finck         return r;
656c2c66affSColin Finck     }
657c2c66affSColin Finck     TRACE("Table id is %d, row count is %d\n", table_id, table->row_count);
658c2c66affSColin Finck 
659c2c66affSColin Finck     /* Note: _Columns table doesn't have non-persistent data */
660c2c66affSColin Finck 
661c2c66affSColin Finck     /* if maxcount is non-zero, assume it's exactly right for this table */
662c2c66affSColin Finck     if (colinfo) memset( colinfo, 0, maxcount * sizeof(*colinfo) );
663c2c66affSColin Finck     count = table->row_count;
664c2c66affSColin Finck     for (i = 0; i < count; i++)
665c2c66affSColin Finck     {
666c2c66affSColin Finck         if (read_table_int( table->data, i, 0, LONG_STR_BYTES) != table_id) continue;
667c2c66affSColin Finck         if (colinfo)
668c2c66affSColin Finck         {
669c2c66affSColin Finck             UINT id = read_table_int( table->data, i, table->colinfo[2].offset, LONG_STR_BYTES );
670c2c66affSColin Finck             UINT col = read_table_int( table->data, i, table->colinfo[1].offset, sizeof(USHORT) ) - (1 << 15);
671c2c66affSColin Finck 
672c2c66affSColin Finck             /* check the column number is in range */
673c2c66affSColin Finck             if (col < 1 || col > maxcount)
674c2c66affSColin Finck             {
675c2c66affSColin Finck                 ERR("column %d out of range (maxcount: %d)\n", col, maxcount);
676c2c66affSColin Finck                 continue;
677c2c66affSColin Finck             }
678c2c66affSColin Finck             /* check if this column was already set */
679c2c66affSColin Finck             if (colinfo[col - 1].number)
680c2c66affSColin Finck             {
681c2c66affSColin Finck                 ERR("duplicate column %d\n", col);
682c2c66affSColin Finck                 continue;
683c2c66affSColin Finck             }
684c2c66affSColin Finck             colinfo[col - 1].tablename = msi_string_lookup( db->strings, table_id, NULL );
685c2c66affSColin Finck             colinfo[col - 1].number = col;
686c2c66affSColin Finck             colinfo[col - 1].colname = msi_string_lookup( db->strings, id, NULL );
687c2c66affSColin Finck             colinfo[col - 1].type = read_table_int( table->data, i, table->colinfo[3].offset,
688c2c66affSColin Finck                                                     sizeof(USHORT) ) - (1 << 15);
689c2c66affSColin Finck             colinfo[col - 1].offset = 0;
690c2c66affSColin Finck             colinfo[col - 1].hash_table = NULL;
691c2c66affSColin Finck         }
692c2c66affSColin Finck         n++;
693c2c66affSColin Finck     }
694c2c66affSColin Finck     TRACE("%s has %d columns\n", debugstr_w(szTableName), n);
695c2c66affSColin Finck 
696c2c66affSColin Finck     if (colinfo && n != maxcount)
697c2c66affSColin Finck     {
698c2c66affSColin Finck         ERR("missing column in table %s\n", debugstr_w(szTableName));
699*f4be6dc3SMikhail         free_colinfo( colinfo, maxcount );
700c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
701c2c66affSColin Finck     }
702c2c66affSColin Finck     table_calc_column_offsets( db, colinfo, n );
703c2c66affSColin Finck     *sz = n;
704c2c66affSColin Finck     return ERROR_SUCCESS;
705c2c66affSColin Finck }
706c2c66affSColin Finck 
msi_create_table(MSIDATABASE * db,LPCWSTR name,column_info * col_info,MSICONDITION persistent,BOOL hold)707c2c66affSColin Finck UINT msi_create_table( MSIDATABASE *db, LPCWSTR name, column_info *col_info,
708caa225a4Swinesync                        MSICONDITION persistent, BOOL hold )
709c2c66affSColin Finck {
710c2c66affSColin Finck     UINT r, nField;
711c2c66affSColin Finck     MSIVIEW *tv = NULL;
712c2c66affSColin Finck     MSIRECORD *rec = NULL;
713c2c66affSColin Finck     column_info *col;
714c2c66affSColin Finck     MSITABLE *table;
715c2c66affSColin Finck     UINT i;
716c2c66affSColin Finck 
717c2c66affSColin Finck     /* only add tables that don't exist already */
718c2c66affSColin Finck     if( TABLE_Exists(db, name ) )
719c2c66affSColin Finck     {
720c2c66affSColin Finck         WARN("table %s exists\n", debugstr_w(name));
721c2c66affSColin Finck         return ERROR_BAD_QUERY_SYNTAX;
722c2c66affSColin Finck     }
723c2c66affSColin Finck 
724*f4be6dc3SMikhail     table = malloc( sizeof(MSITABLE) + wcslen(name) * sizeof(WCHAR) );
725c2c66affSColin Finck     if( !table )
726c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
727c2c66affSColin Finck 
728caa225a4Swinesync     table->ref_count = 0;
729c2c66affSColin Finck     table->row_count = 0;
730c2c66affSColin Finck     table->data = NULL;
731c2c66affSColin Finck     table->data_persistent = NULL;
732c2c66affSColin Finck     table->colinfo = NULL;
733c2c66affSColin Finck     table->col_count = 0;
734c2c66affSColin Finck     table->persistent = persistent;
735c2c66affSColin Finck     lstrcpyW( table->name, name );
736c2c66affSColin Finck 
737caa225a4Swinesync     if( hold )
738caa225a4Swinesync         table->ref_count++;
739caa225a4Swinesync 
740c2c66affSColin Finck     for( col = col_info; col; col = col->next )
741c2c66affSColin Finck         table->col_count++;
742c2c66affSColin Finck 
743*f4be6dc3SMikhail     table->colinfo = malloc( table->col_count * sizeof(*table->colinfo) );
744c2c66affSColin Finck     if (!table->colinfo)
745c2c66affSColin Finck     {
746c2c66affSColin Finck         free_table( table );
747c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
748c2c66affSColin Finck     }
749c2c66affSColin Finck 
750c2c66affSColin Finck     for( i = 0, col = col_info; col; i++, col = col->next )
751c2c66affSColin Finck     {
752211c4104Swinesync         UINT table_id = msi_add_string( db->strings, col->table, -1, persistent );
753211c4104Swinesync         UINT col_id = msi_add_string( db->strings, col->column, -1, persistent );
754c2c66affSColin Finck 
755c2c66affSColin Finck         table->colinfo[ i ].tablename = msi_string_lookup( db->strings, table_id, NULL );
756c2c66affSColin Finck         table->colinfo[ i ].number = i + 1;
757c2c66affSColin Finck         table->colinfo[ i ].colname = msi_string_lookup( db->strings, col_id, NULL );
758c2c66affSColin Finck         table->colinfo[ i ].type = col->type;
759c2c66affSColin Finck         table->colinfo[ i ].offset = 0;
760c2c66affSColin Finck         table->colinfo[ i ].hash_table = NULL;
761c2c66affSColin Finck     }
762c2c66affSColin Finck     table_calc_column_offsets( db, table->colinfo, table->col_count);
763c2c66affSColin Finck 
76474f53b4bSwinesync     r = TABLE_CreateView( db, L"_Tables", &tv );
765c2c66affSColin Finck     TRACE("CreateView returned %x\n", r);
766c2c66affSColin Finck     if( r )
767c2c66affSColin Finck     {
768c2c66affSColin Finck         free_table( table );
769c2c66affSColin Finck         return r;
770c2c66affSColin Finck     }
771c2c66affSColin Finck 
772c2c66affSColin Finck     r = tv->ops->execute( tv, 0 );
773c2c66affSColin Finck     TRACE("tv execute returned %x\n", r);
774c2c66affSColin Finck     if( r )
775c2c66affSColin Finck         goto err;
776c2c66affSColin Finck 
777c2c66affSColin Finck     rec = MSI_CreateRecord( 1 );
778c2c66affSColin Finck     if( !rec )
779c2c66affSColin Finck         goto err;
780c2c66affSColin Finck 
781c2c66affSColin Finck     r = MSI_RecordSetStringW( rec, 1, name );
782c2c66affSColin Finck     if( r )
783c2c66affSColin Finck         goto err;
784c2c66affSColin Finck 
785c2c66affSColin Finck     r = tv->ops->insert_row( tv, rec, -1, persistent == MSICONDITION_FALSE );
786c2c66affSColin Finck     TRACE("insert_row returned %x\n", r);
787c2c66affSColin Finck     if( r )
788c2c66affSColin Finck         goto err;
789c2c66affSColin Finck 
790c2c66affSColin Finck     tv->ops->delete( tv );
791c2c66affSColin Finck     tv = NULL;
792c2c66affSColin Finck 
793c2c66affSColin Finck     msiobj_release( &rec->hdr );
794c2c66affSColin Finck     rec = NULL;
795c2c66affSColin Finck 
796c2c66affSColin Finck     if( persistent != MSICONDITION_FALSE )
797c2c66affSColin Finck     {
798c2c66affSColin Finck         /* add each column to the _Columns table */
79974f53b4bSwinesync         r = TABLE_CreateView( db, L"_Columns", &tv );
800c2c66affSColin Finck         if( r )
801c2c66affSColin Finck             goto err;
802c2c66affSColin Finck 
803c2c66affSColin Finck         r = tv->ops->execute( tv, 0 );
804c2c66affSColin Finck         TRACE("tv execute returned %x\n", r);
805c2c66affSColin Finck         if( r )
806c2c66affSColin Finck             goto err;
807c2c66affSColin Finck 
808c2c66affSColin Finck         rec = MSI_CreateRecord( 4 );
809c2c66affSColin Finck         if( !rec )
810c2c66affSColin Finck             goto err;
811c2c66affSColin Finck 
812c2c66affSColin Finck         r = MSI_RecordSetStringW( rec, 1, name );
813c2c66affSColin Finck         if( r )
814c2c66affSColin Finck             goto err;
815c2c66affSColin Finck 
816c2c66affSColin Finck         /*
817c2c66affSColin Finck          * need to set the table, column number, col name and type
818c2c66affSColin Finck          * for each column we enter in the table
819c2c66affSColin Finck          */
820c2c66affSColin Finck         nField = 1;
821c2c66affSColin Finck         for( col = col_info; col; col = col->next )
822c2c66affSColin Finck         {
823c2c66affSColin Finck             r = MSI_RecordSetInteger( rec, 2, nField );
824c2c66affSColin Finck             if( r )
825c2c66affSColin Finck                 goto err;
826c2c66affSColin Finck 
827c2c66affSColin Finck             r = MSI_RecordSetStringW( rec, 3, col->column );
828c2c66affSColin Finck             if( r )
829c2c66affSColin Finck                 goto err;
830c2c66affSColin Finck 
831c2c66affSColin Finck             r = MSI_RecordSetInteger( rec, 4, col->type );
832c2c66affSColin Finck             if( r )
833c2c66affSColin Finck                 goto err;
834c2c66affSColin Finck 
835c2c66affSColin Finck             r = tv->ops->insert_row( tv, rec, -1, FALSE );
836c2c66affSColin Finck             if( r )
837c2c66affSColin Finck                 goto err;
838c2c66affSColin Finck 
839c2c66affSColin Finck             nField++;
840c2c66affSColin Finck         }
841c2c66affSColin Finck         if( !col )
842c2c66affSColin Finck             r = ERROR_SUCCESS;
843c2c66affSColin Finck     }
844c2c66affSColin Finck 
845c2c66affSColin Finck err:
846c2c66affSColin Finck     if (rec)
847c2c66affSColin Finck         msiobj_release( &rec->hdr );
848c2c66affSColin Finck     /* FIXME: remove values from the string table on error */
849c2c66affSColin Finck     if( tv )
850c2c66affSColin Finck         tv->ops->delete( tv );
851c2c66affSColin Finck 
852c2c66affSColin Finck     if (r == ERROR_SUCCESS)
853c2c66affSColin Finck         list_add_head( &db->tables, &table->entry );
854c2c66affSColin Finck     else
855c2c66affSColin Finck         free_table( table );
856c2c66affSColin Finck 
857c2c66affSColin Finck     return r;
858c2c66affSColin Finck }
859c2c66affSColin Finck 
save_table(MSIDATABASE * db,const MSITABLE * t,UINT bytes_per_strref)860c2c66affSColin Finck static UINT save_table( MSIDATABASE *db, const MSITABLE *t, UINT bytes_per_strref )
861c2c66affSColin Finck {
862c2c66affSColin Finck     BYTE *rawdata = NULL;
863c2c66affSColin Finck     UINT rawsize, i, j, row_size, row_count;
864c2c66affSColin Finck     UINT r = ERROR_FUNCTION_FAILED;
865c2c66affSColin Finck 
866c2c66affSColin Finck     /* Nothing to do for non-persistent tables */
867c2c66affSColin Finck     if( t->persistent == MSICONDITION_FALSE )
868c2c66affSColin Finck         return ERROR_SUCCESS;
869c2c66affSColin Finck 
870c2c66affSColin Finck     TRACE("Saving %s\n", debugstr_w( t->name ) );
871c2c66affSColin Finck 
872*f4be6dc3SMikhail     row_size = table_get_row_size( db, t->colinfo, t->col_count, bytes_per_strref );
873c2c66affSColin Finck     row_count = t->row_count;
874c2c66affSColin Finck     for (i = 0; i < t->row_count; i++)
875c2c66affSColin Finck     {
876c2c66affSColin Finck         if (!t->data_persistent[i])
877c2c66affSColin Finck         {
878c2c66affSColin Finck             row_count = 1; /* yes, this is bizarre */
879c2c66affSColin Finck             break;
880c2c66affSColin Finck         }
881c2c66affSColin Finck     }
882c2c66affSColin Finck     rawsize = row_count * row_size;
883*f4be6dc3SMikhail     rawdata = calloc( 1, rawsize );
884c2c66affSColin Finck     if( !rawdata )
885c2c66affSColin Finck     {
886c2c66affSColin Finck         r = ERROR_NOT_ENOUGH_MEMORY;
887c2c66affSColin Finck         goto err;
888c2c66affSColin Finck     }
889c2c66affSColin Finck 
890c2c66affSColin Finck     rawsize = 0;
891c2c66affSColin Finck     for (i = 0; i < row_count; i++)
892c2c66affSColin Finck     {
893c2c66affSColin Finck         UINT ofs = 0, ofs_mem = 0;
894c2c66affSColin Finck 
895c2c66affSColin Finck         if (!t->data_persistent[i]) break;
896c2c66affSColin Finck 
897c2c66affSColin Finck         for (j = 0; j < t->col_count; j++)
898c2c66affSColin Finck         {
899c2c66affSColin Finck             UINT m = bytes_per_column( db, &t->colinfo[j], LONG_STR_BYTES );
900c2c66affSColin Finck             UINT n = bytes_per_column( db, &t->colinfo[j], bytes_per_strref );
901c2c66affSColin Finck             UINT k;
902c2c66affSColin Finck 
903c2c66affSColin Finck             if (n != 2 && n != 3 && n != 4)
904c2c66affSColin Finck             {
905c2c66affSColin Finck                 ERR("oops - unknown column width %d\n", n);
906c2c66affSColin Finck                 goto err;
907c2c66affSColin Finck             }
908c2c66affSColin Finck             if (t->colinfo[j].type & MSITYPE_STRING && n < m)
909c2c66affSColin Finck             {
910c2c66affSColin Finck                 UINT id = read_table_int( t->data, i, ofs_mem, LONG_STR_BYTES );
911c2c66affSColin Finck                 if (id > 1 << bytes_per_strref * 8)
912c2c66affSColin Finck                 {
913c2c66affSColin Finck                     ERR("string id %u out of range\n", id);
914c2c66affSColin Finck                     goto err;
915c2c66affSColin Finck                 }
916c2c66affSColin Finck             }
917c2c66affSColin Finck             for (k = 0; k < n; k++)
918c2c66affSColin Finck             {
919c2c66affSColin Finck                 rawdata[ofs * row_count + i * n + k] = t->data[i][ofs_mem + k];
920c2c66affSColin Finck             }
921c2c66affSColin Finck             ofs_mem += m;
922c2c66affSColin Finck             ofs += n;
923c2c66affSColin Finck         }
924c2c66affSColin Finck         rawsize += row_size;
925c2c66affSColin Finck     }
926c2c66affSColin Finck 
927c2c66affSColin Finck     TRACE("writing %d bytes\n", rawsize);
928c2c66affSColin Finck     r = write_stream_data( db->storage, t->name, rawdata, rawsize, TRUE );
929c2c66affSColin Finck 
930c2c66affSColin Finck err:
931*f4be6dc3SMikhail     free( rawdata );
932c2c66affSColin Finck     return r;
933c2c66affSColin Finck }
934c2c66affSColin Finck 
update_table_columns(MSIDATABASE * db,const WCHAR * name)935*f4be6dc3SMikhail static void update_table_columns( MSIDATABASE *db, const WCHAR *name )
936c2c66affSColin Finck {
937c2c66affSColin Finck     MSITABLE *table;
938c2c66affSColin Finck     UINT size, offset, old_count;
939c2c66affSColin Finck     UINT n;
940c2c66affSColin Finck 
941c2c66affSColin Finck     if (!(table = find_cached_table( db, name ))) return;
942c2c66affSColin Finck     old_count = table->col_count;
943*f4be6dc3SMikhail     free_colinfo( table->colinfo, table->col_count );
944*f4be6dc3SMikhail     free( table->colinfo );
945c2c66affSColin Finck     table->colinfo = NULL;
946c2c66affSColin Finck 
947c2c66affSColin Finck     table_get_column_info( db, name, &table->colinfo, &table->col_count );
948c2c66affSColin Finck     if (!table->col_count) return;
949c2c66affSColin Finck 
950*f4be6dc3SMikhail     size = table_get_row_size( db, table->colinfo, table->col_count, LONG_STR_BYTES );
951c2c66affSColin Finck     offset = table->colinfo[table->col_count - 1].offset;
952c2c66affSColin Finck 
953c2c66affSColin Finck     for ( n = 0; n < table->row_count; n++ )
954c2c66affSColin Finck     {
955*f4be6dc3SMikhail         table->data[n] = realloc( table->data[n], size );
956c2c66affSColin Finck         if (old_count < table->col_count)
957c2c66affSColin Finck             memset( &table->data[n][offset], 0, size - offset );
958c2c66affSColin Finck     }
959c2c66affSColin Finck }
960c2c66affSColin Finck 
961c2c66affSColin Finck /* try to find the table name in the _Tables table */
TABLE_Exists(MSIDATABASE * db,LPCWSTR name)962c2c66affSColin Finck BOOL TABLE_Exists( MSIDATABASE *db, LPCWSTR name )
963c2c66affSColin Finck {
964c2c66affSColin Finck     UINT r, table_id, i;
965c2c66affSColin Finck     MSITABLE *table;
966c2c66affSColin Finck 
96774f53b4bSwinesync     if( !wcscmp( name, L"_Tables" ) || !wcscmp( name, L"_Columns" ) ||
96874f53b4bSwinesync         !wcscmp( name, L"_Streams" ) || !wcscmp( name, L"_Storages" ) )
969c2c66affSColin Finck         return TRUE;
970c2c66affSColin Finck 
971c2c66affSColin Finck     r = msi_string2id( db->strings, name, -1, &table_id );
972c2c66affSColin Finck     if( r != ERROR_SUCCESS )
973c2c66affSColin Finck     {
974c2c66affSColin Finck         TRACE("Couldn't find id for %s\n", debugstr_w(name));
975c2c66affSColin Finck         return FALSE;
976c2c66affSColin Finck     }
977c2c66affSColin Finck 
97874f53b4bSwinesync     r = get_table( db, L"_Tables", &table );
979c2c66affSColin Finck     if( r != ERROR_SUCCESS )
980c2c66affSColin Finck     {
98174f53b4bSwinesync         ERR("table _Tables not available\n");
982c2c66affSColin Finck         return FALSE;
983c2c66affSColin Finck     }
984c2c66affSColin Finck 
985c2c66affSColin Finck     for( i = 0; i < table->row_count; i++ )
986c2c66affSColin Finck     {
987c2c66affSColin Finck         if( read_table_int( table->data, i, 0, LONG_STR_BYTES ) == table_id )
988c2c66affSColin Finck             return TRUE;
989c2c66affSColin Finck     }
990c2c66affSColin Finck 
991c2c66affSColin Finck     return FALSE;
992c2c66affSColin Finck }
993c2c66affSColin Finck 
994c2c66affSColin Finck /* below is the query interface to a table */
995c2c66affSColin Finck 
996*f4be6dc3SMikhail struct table_view
997c2c66affSColin Finck {
998c2c66affSColin Finck     MSIVIEW        view;
999c2c66affSColin Finck     MSIDATABASE   *db;
1000c2c66affSColin Finck     MSITABLE      *table;
1001*f4be6dc3SMikhail     struct column_info *columns;
1002c2c66affSColin Finck     UINT           num_cols;
1003c2c66affSColin Finck     UINT           row_size;
1004c2c66affSColin Finck     WCHAR          name[1];
1005*f4be6dc3SMikhail };
1006c2c66affSColin Finck 
TABLE_fetch_int(struct tagMSIVIEW * view,UINT row,UINT col,UINT * val)1007c2c66affSColin Finck static UINT TABLE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
1008c2c66affSColin Finck {
1009*f4be6dc3SMikhail     struct table_view *tv = (struct table_view *)view;
1010c2c66affSColin Finck     UINT offset, n;
1011c2c66affSColin Finck 
1012c2c66affSColin Finck     if( !tv->table )
1013c2c66affSColin Finck         return ERROR_INVALID_PARAMETER;
1014c2c66affSColin Finck 
1015c2c66affSColin Finck     if( (col==0) || (col>tv->num_cols) )
1016c2c66affSColin Finck         return ERROR_INVALID_PARAMETER;
1017c2c66affSColin Finck 
1018c2c66affSColin Finck     /* how many rows are there ? */
1019c2c66affSColin Finck     if( row >= tv->table->row_count )
1020c2c66affSColin Finck         return ERROR_NO_MORE_ITEMS;
1021c2c66affSColin Finck 
1022c2c66affSColin Finck     if( tv->columns[col-1].offset >= tv->row_size )
1023c2c66affSColin Finck     {
1024c2c66affSColin Finck         ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1025c2c66affSColin Finck         ERR("%p %p\n", tv, tv->columns );
1026c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
1027c2c66affSColin Finck     }
1028c2c66affSColin Finck 
1029c2c66affSColin Finck     n = bytes_per_column( tv->db, &tv->columns[col - 1], LONG_STR_BYTES );
1030c2c66affSColin Finck     if (n != 2 && n != 3 && n != 4)
1031c2c66affSColin Finck     {
1032c2c66affSColin Finck         ERR("oops! what is %d bytes per column?\n", n );
1033c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
1034c2c66affSColin Finck     }
1035c2c66affSColin Finck 
1036c2c66affSColin Finck     offset = tv->columns[col-1].offset;
1037c2c66affSColin Finck     *val = read_table_int(tv->table->data, row, offset, n);
1038c2c66affSColin Finck 
1039c2c66affSColin Finck     /* TRACE("Data [%d][%d] = %d\n", row, col, *val ); */
1040c2c66affSColin Finck 
1041c2c66affSColin Finck     return ERROR_SUCCESS;
1042c2c66affSColin Finck }
1043c2c66affSColin Finck 
get_stream_name(const struct table_view * tv,UINT row,WCHAR ** pstname)1044*f4be6dc3SMikhail static UINT get_stream_name( const struct table_view *tv, UINT row, WCHAR **pstname )
1045c2c66affSColin Finck {
1046c2c66affSColin Finck     LPWSTR p, stname = NULL;
1047c2c66affSColin Finck     UINT i, r, type, ival;
1048c2c66affSColin Finck     DWORD len;
1049c2c66affSColin Finck     LPCWSTR sval;
1050c2c66affSColin Finck     MSIVIEW *view = (MSIVIEW *) tv;
1051c2c66affSColin Finck 
1052c2c66affSColin Finck     TRACE("%p %d\n", tv, row);
1053c2c66affSColin Finck 
1054c2c66affSColin Finck     len = lstrlenW( tv->name ) + 1;
1055*f4be6dc3SMikhail     stname = malloc( len * sizeof(WCHAR) );
1056c2c66affSColin Finck     if ( !stname )
1057c2c66affSColin Finck     {
1058c2c66affSColin Finck        r = ERROR_OUTOFMEMORY;
1059c2c66affSColin Finck        goto err;
1060c2c66affSColin Finck     }
1061c2c66affSColin Finck 
1062c2c66affSColin Finck     lstrcpyW( stname, tv->name );
1063c2c66affSColin Finck 
1064c2c66affSColin Finck     for ( i = 0; i < tv->num_cols; i++ )
1065c2c66affSColin Finck     {
1066c2c66affSColin Finck         type = tv->columns[i].type;
1067c2c66affSColin Finck         if ( type & MSITYPE_KEY )
1068c2c66affSColin Finck         {
1069c2c66affSColin Finck             WCHAR number[0x20];
1070c2c66affSColin Finck 
1071c2c66affSColin Finck             r = TABLE_fetch_int( view, row, i+1, &ival );
1072c2c66affSColin Finck             if ( r != ERROR_SUCCESS )
1073c2c66affSColin Finck                 goto err;
1074c2c66affSColin Finck 
1075c2c66affSColin Finck             if ( tv->columns[i].type & MSITYPE_STRING )
1076c2c66affSColin Finck             {
1077c2c66affSColin Finck                 sval = msi_string_lookup( tv->db->strings, ival, NULL );
1078c2c66affSColin Finck                 if ( !sval )
1079c2c66affSColin Finck                 {
1080c2c66affSColin Finck                     r = ERROR_INVALID_PARAMETER;
1081c2c66affSColin Finck                     goto err;
1082c2c66affSColin Finck                 }
1083c2c66affSColin Finck             }
1084c2c66affSColin Finck             else
1085c2c66affSColin Finck             {
1086c2c66affSColin Finck                 UINT n = bytes_per_column( tv->db, &tv->columns[i], LONG_STR_BYTES );
1087c2c66affSColin Finck 
1088c2c66affSColin Finck                 switch( n )
1089c2c66affSColin Finck                 {
1090c2c66affSColin Finck                 case 2:
109174f53b4bSwinesync                     swprintf( number, ARRAY_SIZE(number), L"%d", ival-0x8000 );
1092c2c66affSColin Finck                     break;
1093c2c66affSColin Finck                 case 4:
109474f53b4bSwinesync                     swprintf( number, ARRAY_SIZE(number), L"%d", ival^0x80000000 );
1095c2c66affSColin Finck                     break;
1096c2c66affSColin Finck                 default:
1097c2c66affSColin Finck                     ERR( "oops - unknown column width %d\n", n );
1098c2c66affSColin Finck                     r = ERROR_FUNCTION_FAILED;
1099c2c66affSColin Finck                     goto err;
1100c2c66affSColin Finck                 }
1101c2c66affSColin Finck                 sval = number;
1102c2c66affSColin Finck             }
1103c2c66affSColin Finck 
110474f53b4bSwinesync             len += lstrlenW( L"." ) + lstrlenW( sval );
1105*f4be6dc3SMikhail             p = realloc( stname, len * sizeof(WCHAR) );
1106c2c66affSColin Finck             if ( !p )
1107c2c66affSColin Finck             {
1108c2c66affSColin Finck                 r = ERROR_OUTOFMEMORY;
1109c2c66affSColin Finck                 goto err;
1110c2c66affSColin Finck             }
1111c2c66affSColin Finck             stname = p;
1112c2c66affSColin Finck 
111374f53b4bSwinesync             lstrcatW( stname, L"." );
1114c2c66affSColin Finck             lstrcatW( stname, sval );
1115c2c66affSColin Finck         }
1116c2c66affSColin Finck         else
1117c2c66affSColin Finck            continue;
1118c2c66affSColin Finck     }
1119c2c66affSColin Finck 
1120c2c66affSColin Finck     *pstname = stname;
1121c2c66affSColin Finck     return ERROR_SUCCESS;
1122c2c66affSColin Finck 
1123c2c66affSColin Finck err:
1124*f4be6dc3SMikhail     free( stname );
1125c2c66affSColin Finck     *pstname = NULL;
1126c2c66affSColin Finck     return r;
1127c2c66affSColin Finck }
1128c2c66affSColin Finck 
1129c2c66affSColin Finck /*
1130c2c66affSColin Finck  * We need a special case for streams, as we need to reference column with
1131c2c66affSColin Finck  * the name of the stream in the same table, and the table name
1132c2c66affSColin Finck  * which may not be available at higher levels of the query
1133c2c66affSColin Finck  */
TABLE_fetch_stream(struct tagMSIVIEW * view,UINT row,UINT col,IStream ** stm)1134c2c66affSColin Finck static UINT TABLE_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm )
1135c2c66affSColin Finck {
1136*f4be6dc3SMikhail     struct table_view *tv = (struct table_view *)view;
1137c2c66affSColin Finck     UINT r;
1138c2c66affSColin Finck     WCHAR *name;
1139c2c66affSColin Finck 
1140c2c66affSColin Finck     if( !view->ops->fetch_int )
1141c2c66affSColin Finck         return ERROR_INVALID_PARAMETER;
1142c2c66affSColin Finck 
1143c2c66affSColin Finck     r = get_stream_name( tv, row, &name );
1144c2c66affSColin Finck     if (r != ERROR_SUCCESS)
1145c2c66affSColin Finck     {
1146c2c66affSColin Finck         ERR("fetching stream, error = %u\n", r);
1147c2c66affSColin Finck         return r;
1148c2c66affSColin Finck     }
1149c2c66affSColin Finck 
1150c2c66affSColin Finck     r = msi_get_stream( tv->db, name, stm );
1151c2c66affSColin Finck     if (r != ERROR_SUCCESS)
1152c2c66affSColin Finck         ERR("fetching stream %s, error = %u\n", debugstr_w(name), r);
1153c2c66affSColin Finck 
1154*f4be6dc3SMikhail     free( name );
1155c2c66affSColin Finck     return r;
1156c2c66affSColin Finck }
1157c2c66affSColin Finck 
115897918c36Swinesync /* Set a table value, i.e. preadjusted integer or string ID. */
table_set_bytes(struct table_view * tv,UINT row,UINT col,UINT val)1159*f4be6dc3SMikhail static UINT table_set_bytes( struct table_view *tv, UINT row, UINT col, UINT val )
1160c2c66affSColin Finck {
1161c2c66affSColin Finck     UINT offset, n, i;
1162c2c66affSColin Finck 
1163c2c66affSColin Finck     if( !tv->table )
1164c2c66affSColin Finck         return ERROR_INVALID_PARAMETER;
1165c2c66affSColin Finck 
1166c2c66affSColin Finck     if( (col==0) || (col>tv->num_cols) )
1167c2c66affSColin Finck         return ERROR_INVALID_PARAMETER;
1168c2c66affSColin Finck 
1169c2c66affSColin Finck     if( row >= tv->table->row_count )
1170c2c66affSColin Finck         return ERROR_INVALID_PARAMETER;
1171c2c66affSColin Finck 
1172c2c66affSColin Finck     if( tv->columns[col-1].offset >= tv->row_size )
1173c2c66affSColin Finck     {
1174c2c66affSColin Finck         ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1175c2c66affSColin Finck         ERR("%p %p\n", tv, tv->columns );
1176c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
1177c2c66affSColin Finck     }
1178c2c66affSColin Finck 
1179*f4be6dc3SMikhail     free( tv->columns[col-1].hash_table );
1180c2c66affSColin Finck     tv->columns[col-1].hash_table = NULL;
1181c2c66affSColin Finck 
1182c2c66affSColin Finck     n = bytes_per_column( tv->db, &tv->columns[col - 1], LONG_STR_BYTES );
1183c2c66affSColin Finck     if ( n != 2 && n != 3 && n != 4 )
1184c2c66affSColin Finck     {
1185c2c66affSColin Finck         ERR("oops! what is %d bytes per column?\n", n );
1186c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
1187c2c66affSColin Finck     }
1188c2c66affSColin Finck 
1189c2c66affSColin Finck     offset = tv->columns[col-1].offset;
1190c2c66affSColin Finck     for ( i = 0; i < n; i++ )
1191c2c66affSColin Finck         tv->table->data[row][offset + i] = (val >> i * 8) & 0xff;
1192c2c66affSColin Finck 
1193c2c66affSColin Finck     return ERROR_SUCCESS;
1194c2c66affSColin Finck }
1195c2c66affSColin Finck 
int_to_table_storage(const struct table_view * tv,UINT col,int val,UINT * ret)1196*f4be6dc3SMikhail static UINT int_to_table_storage( const struct table_view *tv, UINT col, int val, UINT *ret )
11970e83dd03Swinesync {
11980e83dd03Swinesync     if ((tv->columns[col-1].type & MSI_DATASIZEMASK) == 2)
11990e83dd03Swinesync     {
12000e83dd03Swinesync         if (val == MSI_NULL_INTEGER)
12010e83dd03Swinesync             *ret = 0;
12020e83dd03Swinesync         else if ((val + 0x8000) & 0xffff0000)
12030e83dd03Swinesync         {
12040e83dd03Swinesync             ERR("value %d out of range\n", val);
12050e83dd03Swinesync             return ERROR_FUNCTION_FAILED;
12060e83dd03Swinesync         }
12070e83dd03Swinesync         else
12080e83dd03Swinesync             *ret = val + 0x8000;
12090e83dd03Swinesync     }
12100e83dd03Swinesync     else
12110e83dd03Swinesync         *ret = val ^ 0x80000000;
12120e83dd03Swinesync 
12130e83dd03Swinesync     return ERROR_SUCCESS;
12140e83dd03Swinesync }
12150e83dd03Swinesync 
TABLE_set_int(MSIVIEW * view,UINT row,UINT col,int val)121697918c36Swinesync static UINT TABLE_set_int( MSIVIEW *view, UINT row, UINT col, int val )
121797918c36Swinesync {
1218*f4be6dc3SMikhail     struct table_view *tv = (struct table_view *)view;
121997918c36Swinesync     UINT r, table_int;
122097918c36Swinesync 
122197918c36Swinesync     TRACE("row %u, col %u, val %d.\n", row, col, val);
122297918c36Swinesync 
122397918c36Swinesync     if ((r = int_to_table_storage( tv, col, val, &table_int )))
122497918c36Swinesync         return r;
122597918c36Swinesync 
122697918c36Swinesync     if (tv->columns[col-1].type & MSITYPE_KEY)
122797918c36Swinesync     {
122897918c36Swinesync         UINT key;
122997918c36Swinesync 
123097918c36Swinesync         if ((r = TABLE_fetch_int( view, row, col, &key )))
123197918c36Swinesync             return r;
123297918c36Swinesync         if (key != table_int)
123397918c36Swinesync         {
123497918c36Swinesync             ERR("Cannot modify primary key %s.%s.\n",
123597918c36Swinesync                 debugstr_w(tv->table->name), debugstr_w(tv->columns[col-1].colname));
123697918c36Swinesync             return ERROR_FUNCTION_FAILED;
123797918c36Swinesync         }
123897918c36Swinesync     }
123997918c36Swinesync 
124097918c36Swinesync     return table_set_bytes( tv, row, col, table_int );
124197918c36Swinesync }
124297918c36Swinesync 
TABLE_set_string(MSIVIEW * view,UINT row,UINT col,const WCHAR * val,int len)124397918c36Swinesync static UINT TABLE_set_string( MSIVIEW *view, UINT row, UINT col, const WCHAR *val, int len )
124497918c36Swinesync {
1245*f4be6dc3SMikhail     struct table_view *tv = (struct table_view *)view;
124697918c36Swinesync     BOOL persistent;
124797918c36Swinesync     UINT id, r;
124897918c36Swinesync 
124997918c36Swinesync     TRACE("row %u, col %u, val %s.\n", row, col, debugstr_wn(val, len));
125097918c36Swinesync 
125197918c36Swinesync     persistent = (tv->table->persistent != MSICONDITION_FALSE)
125297918c36Swinesync                   && tv->table->data_persistent[row];
125397918c36Swinesync 
125497918c36Swinesync     if (val)
125597918c36Swinesync     {
125697918c36Swinesync         r = msi_string2id( tv->db->strings, val, len, &id );
125797918c36Swinesync         if (r != ERROR_SUCCESS)
125897918c36Swinesync             id = msi_add_string( tv->db->strings, val, len, persistent );
125997918c36Swinesync     }
126097918c36Swinesync     else
126197918c36Swinesync         id = 0;
126297918c36Swinesync 
126397918c36Swinesync     if (tv->columns[col-1].type & MSITYPE_KEY)
126497918c36Swinesync     {
126597918c36Swinesync         UINT key;
126697918c36Swinesync 
126797918c36Swinesync         if ((r = TABLE_fetch_int( view, row, col, &key )))
126897918c36Swinesync             return r;
126997918c36Swinesync         if (key != id)
127097918c36Swinesync         {
127197918c36Swinesync             ERR("Cannot modify primary key %s.%s.\n",
127297918c36Swinesync                 debugstr_w(tv->table->name), debugstr_w(tv->columns[col-1].colname));
127397918c36Swinesync             return ERROR_FUNCTION_FAILED;
127497918c36Swinesync         }
127597918c36Swinesync     }
127697918c36Swinesync 
127797918c36Swinesync     return table_set_bytes( tv, row, col, id );
127897918c36Swinesync }
127997918c36Swinesync 
TABLE_get_row(struct tagMSIVIEW * view,UINT row,MSIRECORD ** rec)1280c2c66affSColin Finck static UINT TABLE_get_row( struct tagMSIVIEW *view, UINT row, MSIRECORD **rec )
1281c2c66affSColin Finck {
1282*f4be6dc3SMikhail     struct table_view *tv = (struct table_view *)view;
1283c2c66affSColin Finck 
1284c2c66affSColin Finck     if (!tv->table)
1285c2c66affSColin Finck         return ERROR_INVALID_PARAMETER;
1286c2c66affSColin Finck 
1287c2c66affSColin Finck     return msi_view_get_row(tv->db, view, row, rec);
1288c2c66affSColin Finck }
1289c2c66affSColin Finck 
add_stream(MSIDATABASE * db,const WCHAR * name,IStream * data)1290c2c66affSColin Finck static UINT add_stream( MSIDATABASE *db, const WCHAR *name, IStream *data )
1291c2c66affSColin Finck {
1292c2c66affSColin Finck     MSIQUERY *query;
1293c2c66affSColin Finck     MSIRECORD *rec;
1294c2c66affSColin Finck     UINT r;
1295c2c66affSColin Finck 
1296c2c66affSColin Finck     TRACE("%p %s %p\n", db, debugstr_w(name), data);
1297c2c66affSColin Finck 
1298c2c66affSColin Finck     if (!(rec = MSI_CreateRecord( 2 )))
1299c2c66affSColin Finck         return ERROR_OUTOFMEMORY;
1300c2c66affSColin Finck 
1301c2c66affSColin Finck     r = MSI_RecordSetStringW( rec, 1, name );
1302c2c66affSColin Finck     if (r != ERROR_SUCCESS)
1303c2c66affSColin Finck        goto done;
1304c2c66affSColin Finck 
1305c2c66affSColin Finck     r = MSI_RecordSetIStream( rec, 2, data );
1306c2c66affSColin Finck     if (r != ERROR_SUCCESS)
1307c2c66affSColin Finck        goto done;
1308c2c66affSColin Finck 
130974f53b4bSwinesync     r = MSI_DatabaseOpenViewW( db, L"INSERT INTO `_Streams` (`Name`,`Data`) VALUES (?,?)", &query );
1310c2c66affSColin Finck     if (r != ERROR_SUCCESS)
1311c2c66affSColin Finck        goto done;
1312c2c66affSColin Finck 
1313c2c66affSColin Finck     r = MSI_ViewExecute( query, rec );
1314c2c66affSColin Finck     msiobj_release( &query->hdr );
1315c2c66affSColin Finck     if (r == ERROR_SUCCESS)
1316c2c66affSColin Finck         goto done;
1317c2c66affSColin Finck 
1318c2c66affSColin Finck     msiobj_release( &rec->hdr );
1319c2c66affSColin Finck     if (!(rec = MSI_CreateRecord( 2 )))
1320c2c66affSColin Finck         return ERROR_OUTOFMEMORY;
1321c2c66affSColin Finck 
1322c2c66affSColin Finck     r = MSI_RecordSetIStream( rec, 1, data );
1323c2c66affSColin Finck     if (r != ERROR_SUCCESS)
1324c2c66affSColin Finck        goto done;
1325c2c66affSColin Finck 
1326c2c66affSColin Finck     r = MSI_RecordSetStringW( rec, 2, name );
1327c2c66affSColin Finck     if (r != ERROR_SUCCESS)
1328c2c66affSColin Finck        goto done;
1329c2c66affSColin Finck 
133074f53b4bSwinesync     r = MSI_DatabaseOpenViewW( db, L"UPDATE `_Streams` SET `Data` = ? WHERE `Name` = ?", &query );
1331c2c66affSColin Finck     if (r != ERROR_SUCCESS)
1332c2c66affSColin Finck         goto done;
1333c2c66affSColin Finck 
1334c2c66affSColin Finck     r = MSI_ViewExecute( query, rec );
1335c2c66affSColin Finck     msiobj_release( &query->hdr );
1336c2c66affSColin Finck 
1337c2c66affSColin Finck done:
1338c2c66affSColin Finck     msiobj_release( &rec->hdr );
1339c2c66affSColin Finck     return r;
1340c2c66affSColin Finck }
1341c2c66affSColin Finck 
TABLE_set_stream(MSIVIEW * view,UINT row,UINT col,IStream * stream)1342580d3e1bSwinesync static UINT TABLE_set_stream( MSIVIEW *view, UINT row, UINT col, IStream *stream )
1343580d3e1bSwinesync {
1344*f4be6dc3SMikhail     struct table_view *tv = (struct table_view *)view;
1345580d3e1bSwinesync     WCHAR *name;
1346580d3e1bSwinesync     UINT r;
1347580d3e1bSwinesync 
1348580d3e1bSwinesync     TRACE("row %u, col %u, stream %p.\n", row, col, stream);
1349580d3e1bSwinesync 
1350580d3e1bSwinesync     if ((r = get_stream_name( tv, row - 1, &name )))
1351580d3e1bSwinesync         return r;
1352580d3e1bSwinesync 
1353580d3e1bSwinesync     r = add_stream( tv->db, name, stream );
1354*f4be6dc3SMikhail     free( name );
1355580d3e1bSwinesync     return r;
1356580d3e1bSwinesync }
1357580d3e1bSwinesync 
get_table_value_from_record(struct table_view * tv,MSIRECORD * rec,UINT iField,UINT * pvalue)1358*f4be6dc3SMikhail static UINT get_table_value_from_record( struct table_view *tv, MSIRECORD *rec, UINT iField, UINT *pvalue )
1359c2c66affSColin Finck {
1360*f4be6dc3SMikhail     struct column_info columninfo;
1361c2c66affSColin Finck     UINT r;
1362c2c66affSColin Finck 
1363306890e8Swinesync     if (!iField || iField > tv->num_cols || MSI_RecordIsNull( rec, iField ))
1364c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
1365c2c66affSColin Finck 
1366c2c66affSColin Finck     columninfo = tv->columns[ iField - 1 ];
1367c2c66affSColin Finck 
1368c2c66affSColin Finck     if ( MSITYPE_IS_BINARY(columninfo.type) )
1369c2c66affSColin Finck     {
1370c2c66affSColin Finck         *pvalue = 1; /* refers to the first key column */
1371c2c66affSColin Finck     }
1372c2c66affSColin Finck     else if ( columninfo.type & MSITYPE_STRING )
1373c2c66affSColin Finck     {
1374c2c66affSColin Finck         int len;
1375c2c66affSColin Finck         const WCHAR *sval = msi_record_get_string( rec, iField, &len );
1376c2c66affSColin Finck         if (sval)
1377c2c66affSColin Finck         {
1378c2c66affSColin Finck             r = msi_string2id( tv->db->strings, sval, len, pvalue );
1379c2c66affSColin Finck             if (r != ERROR_SUCCESS)
1380c2c66affSColin Finck                 return ERROR_NOT_FOUND;
1381c2c66affSColin Finck         }
1382c2c66affSColin Finck         else *pvalue = 0;
1383c2c66affSColin Finck     }
1384c2c66affSColin Finck     else
13850e83dd03Swinesync         return int_to_table_storage( tv, iField, MSI_RecordGetInteger( rec, iField ), pvalue );
1386c2c66affSColin Finck 
1387c2c66affSColin Finck     return ERROR_SUCCESS;
1388c2c66affSColin Finck }
1389c2c66affSColin Finck 
TABLE_set_row(struct tagMSIVIEW * view,UINT row,MSIRECORD * rec,UINT mask)1390c2c66affSColin Finck static UINT TABLE_set_row( struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask )
1391c2c66affSColin Finck {
1392*f4be6dc3SMikhail     struct table_view *tv = (struct table_view *)view;
1393c2c66affSColin Finck     UINT i, val, r = ERROR_SUCCESS;
1394c2c66affSColin Finck 
1395c2c66affSColin Finck     if ( !tv->table )
1396c2c66affSColin Finck         return ERROR_INVALID_PARAMETER;
1397c2c66affSColin Finck 
1398c2c66affSColin Finck     /* test if any of the mask bits are invalid */
1399c2c66affSColin Finck     if ( mask >= (1<<tv->num_cols) )
1400c2c66affSColin Finck         return ERROR_INVALID_PARAMETER;
1401c2c66affSColin Finck 
1402c2c66affSColin Finck     for ( i = 0; i < tv->num_cols; i++ )
1403c2c66affSColin Finck     {
1404c2c66affSColin Finck         BOOL persistent;
1405c2c66affSColin Finck 
1406c2c66affSColin Finck         /* only update the fields specified in the mask */
1407c2c66affSColin Finck         if ( !(mask&(1<<i)) )
1408c2c66affSColin Finck             continue;
1409c2c66affSColin Finck 
1410c2c66affSColin Finck         persistent = (tv->table->persistent != MSICONDITION_FALSE) &&
1411c2c66affSColin Finck                      (tv->table->data_persistent[row]);
1412c2c66affSColin Finck         /* FIXME: should we allow updating keys? */
1413c2c66affSColin Finck 
1414c2c66affSColin Finck         val = 0;
1415c2c66affSColin Finck         if ( !MSI_RecordIsNull( rec, i + 1 ) )
1416c2c66affSColin Finck         {
1417c2c66affSColin Finck             r = get_table_value_from_record (tv, rec, i + 1, &val);
1418c2c66affSColin Finck             if ( MSITYPE_IS_BINARY(tv->columns[ i ].type) )
1419c2c66affSColin Finck             {
1420c2c66affSColin Finck                 IStream *stm;
1421c2c66affSColin Finck                 LPWSTR stname;
1422c2c66affSColin Finck 
1423c2c66affSColin Finck                 if ( r != ERROR_SUCCESS )
1424c2c66affSColin Finck                     return ERROR_FUNCTION_FAILED;
1425c2c66affSColin Finck 
1426c2c66affSColin Finck                 r = MSI_RecordGetIStream( rec, i + 1, &stm );
1427c2c66affSColin Finck                 if ( r != ERROR_SUCCESS )
1428c2c66affSColin Finck                     return r;
1429c2c66affSColin Finck 
1430c2c66affSColin Finck                 r = get_stream_name( tv, row, &stname );
1431c2c66affSColin Finck                 if ( r != ERROR_SUCCESS )
1432c2c66affSColin Finck                 {
1433c2c66affSColin Finck                     IStream_Release( stm );
1434c2c66affSColin Finck                     return r;
1435c2c66affSColin Finck                 }
1436c2c66affSColin Finck 
1437c2c66affSColin Finck                 r = add_stream( tv->db, stname, stm );
1438c2c66affSColin Finck                 IStream_Release( stm );
1439*f4be6dc3SMikhail                 free( stname );
1440c2c66affSColin Finck 
1441c2c66affSColin Finck                 if ( r != ERROR_SUCCESS )
1442c2c66affSColin Finck                     return r;
1443c2c66affSColin Finck             }
1444c2c66affSColin Finck             else if ( tv->columns[i].type & MSITYPE_STRING )
1445c2c66affSColin Finck             {
1446c2c66affSColin Finck                 UINT x;
1447c2c66affSColin Finck 
1448c2c66affSColin Finck                 if ( r != ERROR_SUCCESS )
1449c2c66affSColin Finck                 {
1450c2c66affSColin Finck                     int len;
1451c2c66affSColin Finck                     const WCHAR *sval = msi_record_get_string( rec, i + 1, &len );
1452211c4104Swinesync                     val = msi_add_string( tv->db->strings, sval, len, persistent );
1453c2c66affSColin Finck                 }
1454c2c66affSColin Finck                 else
1455c2c66affSColin Finck                 {
1456c2c66affSColin Finck                     TABLE_fetch_int(&tv->view, row, i + 1, &x);
1457c2c66affSColin Finck                     if (val == x)
1458c2c66affSColin Finck                         continue;
1459c2c66affSColin Finck                 }
1460c2c66affSColin Finck             }
1461c2c66affSColin Finck             else
1462c2c66affSColin Finck             {
1463c2c66affSColin Finck                 if ( r != ERROR_SUCCESS )
1464c2c66affSColin Finck                     return ERROR_FUNCTION_FAILED;
1465c2c66affSColin Finck             }
1466c2c66affSColin Finck         }
1467c2c66affSColin Finck 
146897918c36Swinesync         r = table_set_bytes( tv, row, i+1, val );
1469c2c66affSColin Finck         if ( r != ERROR_SUCCESS )
1470c2c66affSColin Finck             break;
1471c2c66affSColin Finck     }
1472c2c66affSColin Finck     return r;
1473c2c66affSColin Finck }
1474c2c66affSColin Finck 
table_create_new_row(struct tagMSIVIEW * view,UINT * num,BOOL temporary)1475c2c66affSColin Finck static UINT table_create_new_row( struct tagMSIVIEW *view, UINT *num, BOOL temporary )
1476c2c66affSColin Finck {
1477*f4be6dc3SMikhail     struct table_view *tv = (struct table_view *)view;
1478c2c66affSColin Finck     BYTE **p, *row;
1479c2c66affSColin Finck     BOOL *b;
1480c2c66affSColin Finck     UINT sz;
1481c2c66affSColin Finck     BYTE ***data_ptr;
1482c2c66affSColin Finck     BOOL **data_persist_ptr;
1483c2c66affSColin Finck     UINT *row_count;
1484c2c66affSColin Finck 
1485c2c66affSColin Finck     TRACE("%p %s\n", view, temporary ? "TRUE" : "FALSE");
1486c2c66affSColin Finck 
1487c2c66affSColin Finck     if( !tv->table )
1488c2c66affSColin Finck         return ERROR_INVALID_PARAMETER;
1489c2c66affSColin Finck 
1490*f4be6dc3SMikhail     row = calloc( 1, tv->row_size );
1491c2c66affSColin Finck     if( !row )
1492c2c66affSColin Finck         return ERROR_NOT_ENOUGH_MEMORY;
1493c2c66affSColin Finck 
1494c2c66affSColin Finck     row_count = &tv->table->row_count;
1495c2c66affSColin Finck     data_ptr = &tv->table->data;
1496c2c66affSColin Finck     data_persist_ptr = &tv->table->data_persistent;
1497c2c66affSColin Finck     if (*num == -1)
1498c2c66affSColin Finck         *num = tv->table->row_count;
1499c2c66affSColin Finck 
1500c2c66affSColin Finck     sz = (*row_count + 1) * sizeof (BYTE*);
1501c2c66affSColin Finck     if( *data_ptr )
1502*f4be6dc3SMikhail         p = realloc( *data_ptr, sz );
1503c2c66affSColin Finck     else
1504*f4be6dc3SMikhail         p = malloc( sz );
1505c2c66affSColin Finck     if( !p )
1506c2c66affSColin Finck     {
1507*f4be6dc3SMikhail         free( row );
1508c2c66affSColin Finck         return ERROR_NOT_ENOUGH_MEMORY;
1509c2c66affSColin Finck     }
1510c2c66affSColin Finck 
1511c2c66affSColin Finck     sz = (*row_count + 1) * sizeof (BOOL);
1512c2c66affSColin Finck     if( *data_persist_ptr )
1513*f4be6dc3SMikhail         b = realloc( *data_persist_ptr, sz );
1514c2c66affSColin Finck     else
1515*f4be6dc3SMikhail         b = malloc( sz );
1516c2c66affSColin Finck     if( !b )
1517c2c66affSColin Finck     {
1518*f4be6dc3SMikhail         free( row );
1519*f4be6dc3SMikhail         free( p );
1520c2c66affSColin Finck         return ERROR_NOT_ENOUGH_MEMORY;
1521c2c66affSColin Finck     }
1522c2c66affSColin Finck 
1523c2c66affSColin Finck     *data_ptr = p;
1524c2c66affSColin Finck     (*data_ptr)[*row_count] = row;
1525c2c66affSColin Finck 
1526c2c66affSColin Finck     *data_persist_ptr = b;
1527c2c66affSColin Finck     (*data_persist_ptr)[*row_count] = !temporary;
1528c2c66affSColin Finck 
1529c2c66affSColin Finck     (*row_count)++;
1530c2c66affSColin Finck 
1531c2c66affSColin Finck     return ERROR_SUCCESS;
1532c2c66affSColin Finck }
1533c2c66affSColin Finck 
TABLE_execute(struct tagMSIVIEW * view,MSIRECORD * record)1534c2c66affSColin Finck static UINT TABLE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
1535c2c66affSColin Finck {
1536*f4be6dc3SMikhail     struct table_view *tv = (struct table_view *)view;
1537c2c66affSColin Finck 
1538c2c66affSColin Finck     TRACE("%p %p\n", tv, record);
1539c2c66affSColin Finck 
1540c2c66affSColin Finck     TRACE("There are %d columns\n", tv->num_cols );
1541c2c66affSColin Finck 
1542c2c66affSColin Finck     return ERROR_SUCCESS;
1543c2c66affSColin Finck }
1544c2c66affSColin Finck 
TABLE_close(struct tagMSIVIEW * view)1545c2c66affSColin Finck static UINT TABLE_close( struct tagMSIVIEW *view )
1546c2c66affSColin Finck {
1547c2c66affSColin Finck     TRACE("%p\n", view );
1548c2c66affSColin Finck 
1549c2c66affSColin Finck     return ERROR_SUCCESS;
1550c2c66affSColin Finck }
1551c2c66affSColin Finck 
TABLE_get_dimensions(struct tagMSIVIEW * view,UINT * rows,UINT * cols)1552c2c66affSColin Finck static UINT TABLE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols)
1553c2c66affSColin Finck {
1554*f4be6dc3SMikhail     struct table_view *tv = (struct table_view *)view;
1555c2c66affSColin Finck 
1556c2c66affSColin Finck     TRACE("%p %p %p\n", view, rows, cols );
1557c2c66affSColin Finck 
1558c2c66affSColin Finck     if( cols )
1559c2c66affSColin Finck         *cols = tv->num_cols;
1560c2c66affSColin Finck     if( rows )
1561c2c66affSColin Finck     {
1562c2c66affSColin Finck         if( !tv->table )
1563c2c66affSColin Finck             return ERROR_INVALID_PARAMETER;
1564c2c66affSColin Finck         *rows = tv->table->row_count;
1565c2c66affSColin Finck     }
1566c2c66affSColin Finck 
1567c2c66affSColin Finck     return ERROR_SUCCESS;
1568c2c66affSColin Finck }
1569c2c66affSColin Finck 
TABLE_get_column_info(struct tagMSIVIEW * view,UINT n,LPCWSTR * name,UINT * type,BOOL * temporary,LPCWSTR * table_name)1570c2c66affSColin Finck static UINT TABLE_get_column_info( struct tagMSIVIEW *view,
1571c2c66affSColin Finck                 UINT n, LPCWSTR *name, UINT *type, BOOL *temporary,
1572c2c66affSColin Finck                 LPCWSTR *table_name )
1573c2c66affSColin Finck {
1574*f4be6dc3SMikhail     struct table_view *tv = (struct table_view *)view;
1575c2c66affSColin Finck 
1576c2c66affSColin Finck     TRACE("%p %d %p %p\n", tv, n, name, type );
1577c2c66affSColin Finck 
1578c2c66affSColin Finck     if( ( n == 0 ) || ( n > tv->num_cols ) )
1579c2c66affSColin Finck         return ERROR_INVALID_PARAMETER;
1580c2c66affSColin Finck 
1581c2c66affSColin Finck     if( name )
1582c2c66affSColin Finck     {
1583c2c66affSColin Finck         *name = tv->columns[n-1].colname;
1584c2c66affSColin Finck         if( !*name )
1585c2c66affSColin Finck             return ERROR_FUNCTION_FAILED;
1586c2c66affSColin Finck     }
1587c2c66affSColin Finck 
1588c2c66affSColin Finck     if( table_name )
1589c2c66affSColin Finck     {
1590c2c66affSColin Finck         *table_name = tv->columns[n-1].tablename;
1591c2c66affSColin Finck         if( !*table_name )
1592c2c66affSColin Finck             return ERROR_FUNCTION_FAILED;
1593c2c66affSColin Finck     }
1594c2c66affSColin Finck 
1595c2c66affSColin Finck     if( type )
1596c2c66affSColin Finck         *type = tv->columns[n-1].type;
1597c2c66affSColin Finck 
1598c2c66affSColin Finck     if( temporary )
159961167e12Swinesync         *temporary = (tv->columns[n-1].type & MSITYPE_TEMPORARY) != 0;
1600c2c66affSColin Finck 
1601c2c66affSColin Finck     return ERROR_SUCCESS;
1602c2c66affSColin Finck }
1603c2c66affSColin Finck 
1604*f4be6dc3SMikhail static UINT table_find_row( struct table_view *, MSIRECORD *, UINT *, UINT * );
1605c2c66affSColin Finck 
table_validate_new(struct table_view * tv,MSIRECORD * rec,UINT * column)1606*f4be6dc3SMikhail static UINT table_validate_new( struct table_view *tv, MSIRECORD *rec, UINT *column )
1607c2c66affSColin Finck {
1608c2c66affSColin Finck     UINT r, row, i;
1609c2c66affSColin Finck 
1610c2c66affSColin Finck     /* check there are no null values where they're not allowed */
1611c2c66affSColin Finck     for( i = 0; i < tv->num_cols; i++ )
1612c2c66affSColin Finck     {
1613c2c66affSColin Finck         if ( tv->columns[i].type & MSITYPE_NULLABLE )
1614c2c66affSColin Finck             continue;
1615c2c66affSColin Finck 
1616c2c66affSColin Finck         if ( MSITYPE_IS_BINARY(tv->columns[i].type) )
1617c2c66affSColin Finck             TRACE("skipping binary column\n");
1618c2c66affSColin Finck         else if ( tv->columns[i].type & MSITYPE_STRING )
1619c2c66affSColin Finck         {
1620c2c66affSColin Finck             int len;
1621c2c66affSColin Finck             const WCHAR *str = msi_record_get_string( rec, i+1, &len );
1622c2c66affSColin Finck 
1623c2c66affSColin Finck             if (!str || (!str[0] && !len))
1624c2c66affSColin Finck             {
1625c2c66affSColin Finck                 if (column) *column = i;
1626c2c66affSColin Finck                 return ERROR_INVALID_DATA;
1627c2c66affSColin Finck             }
1628c2c66affSColin Finck         }
1629c2c66affSColin Finck         else
1630c2c66affSColin Finck         {
1631c2c66affSColin Finck             UINT n;
1632c2c66affSColin Finck 
1633c2c66affSColin Finck             n = MSI_RecordGetInteger( rec, i+1 );
1634c2c66affSColin Finck             if (n == MSI_NULL_INTEGER)
1635c2c66affSColin Finck             {
1636c2c66affSColin Finck                 if (column) *column = i;
1637c2c66affSColin Finck                 return ERROR_INVALID_DATA;
1638c2c66affSColin Finck             }
1639c2c66affSColin Finck         }
1640c2c66affSColin Finck     }
1641c2c66affSColin Finck 
1642c2c66affSColin Finck     /* check there are no duplicate keys */
1643*f4be6dc3SMikhail     r = table_find_row( tv, rec, &row, column );
1644c2c66affSColin Finck     if (r == ERROR_SUCCESS)
1645c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
1646c2c66affSColin Finck 
1647c2c66affSColin Finck     return ERROR_SUCCESS;
1648c2c66affSColin Finck }
1649c2c66affSColin Finck 
compare_record(struct table_view * tv,UINT row,MSIRECORD * rec)1650*f4be6dc3SMikhail static int compare_record( struct table_view *tv, UINT row, MSIRECORD *rec )
1651c2c66affSColin Finck {
1652c2c66affSColin Finck     UINT r, i, ivalue, x;
1653c2c66affSColin Finck 
1654c2c66affSColin Finck     for (i = 0; i < tv->num_cols; i++ )
1655c2c66affSColin Finck     {
1656c2c66affSColin Finck         if (!(tv->columns[i].type & MSITYPE_KEY)) continue;
1657c2c66affSColin Finck 
1658c2c66affSColin Finck         r = get_table_value_from_record( tv, rec, i + 1, &ivalue );
1659c2c66affSColin Finck         if (r != ERROR_SUCCESS)
1660c2c66affSColin Finck             return 1;
1661c2c66affSColin Finck 
1662c2c66affSColin Finck         r = TABLE_fetch_int( &tv->view, row, i + 1, &x );
1663c2c66affSColin Finck         if (r != ERROR_SUCCESS)
1664c2c66affSColin Finck         {
1665c2c66affSColin Finck             WARN("TABLE_fetch_int should not fail here %u\n", r);
1666c2c66affSColin Finck             return -1;
1667c2c66affSColin Finck         }
1668c2c66affSColin Finck         if (ivalue > x)
1669c2c66affSColin Finck         {
1670c2c66affSColin Finck             return 1;
1671c2c66affSColin Finck         }
1672c2c66affSColin Finck         else if (ivalue == x)
1673c2c66affSColin Finck         {
1674c2c66affSColin Finck             if (i < tv->num_cols - 1) continue;
1675c2c66affSColin Finck             return 0;
1676c2c66affSColin Finck         }
1677c2c66affSColin Finck         else
1678c2c66affSColin Finck             return -1;
1679c2c66affSColin Finck     }
1680c2c66affSColin Finck     return 1;
1681c2c66affSColin Finck }
1682c2c66affSColin Finck 
find_insert_index(struct table_view * tv,MSIRECORD * rec)1683*f4be6dc3SMikhail static int find_insert_index( struct table_view *tv, MSIRECORD *rec )
1684c2c66affSColin Finck {
1685c2c66affSColin Finck     int idx, c, low = 0, high = tv->table->row_count - 1;
1686c2c66affSColin Finck 
1687c2c66affSColin Finck     TRACE("%p %p\n", tv, rec);
1688c2c66affSColin Finck 
1689c2c66affSColin Finck     while (low <= high)
1690c2c66affSColin Finck     {
1691c2c66affSColin Finck         idx = (low + high) / 2;
1692c2c66affSColin Finck         c = compare_record( tv, idx, rec );
1693c2c66affSColin Finck 
1694c2c66affSColin Finck         if (c < 0)
1695c2c66affSColin Finck             high = idx - 1;
1696c2c66affSColin Finck         else if (c > 0)
1697c2c66affSColin Finck             low = idx + 1;
1698c2c66affSColin Finck         else
1699c2c66affSColin Finck         {
1700c2c66affSColin Finck             TRACE("found %u\n", idx);
1701c2c66affSColin Finck             return idx;
1702c2c66affSColin Finck         }
1703c2c66affSColin Finck     }
1704c2c66affSColin Finck     TRACE("found %u\n", high + 1);
1705c2c66affSColin Finck     return high + 1;
1706c2c66affSColin Finck }
1707c2c66affSColin Finck 
TABLE_insert_row(struct tagMSIVIEW * view,MSIRECORD * rec,UINT row,BOOL temporary)1708c2c66affSColin Finck static UINT TABLE_insert_row( struct tagMSIVIEW *view, MSIRECORD *rec, UINT row, BOOL temporary )
1709c2c66affSColin Finck {
1710*f4be6dc3SMikhail     struct table_view *tv = (struct table_view *)view;
1711c2c66affSColin Finck     UINT i, r;
1712c2c66affSColin Finck 
1713c2c66affSColin Finck     TRACE("%p %p %s\n", tv, rec, temporary ? "TRUE" : "FALSE" );
1714c2c66affSColin Finck 
1715c2c66affSColin Finck     /* check that the key is unique - can we find a matching row? */
1716c2c66affSColin Finck     r = table_validate_new( tv, rec, NULL );
1717c2c66affSColin Finck     if( r != ERROR_SUCCESS )
1718c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
1719c2c66affSColin Finck 
1720c2c66affSColin Finck     if (row == -1)
1721c2c66affSColin Finck         row = find_insert_index( tv, rec );
1722c2c66affSColin Finck 
1723c2c66affSColin Finck     r = table_create_new_row( view, &row, temporary );
1724c2c66affSColin Finck     TRACE("insert_row returned %08x\n", r);
1725c2c66affSColin Finck     if( r != ERROR_SUCCESS )
1726c2c66affSColin Finck         return r;
1727c2c66affSColin Finck 
1728c2c66affSColin Finck     /* shift the rows to make room for the new row */
1729c2c66affSColin Finck     for (i = tv->table->row_count - 1; i > row; i--)
1730c2c66affSColin Finck     {
1731c2c66affSColin Finck         memmove(&(tv->table->data[i][0]),
1732c2c66affSColin Finck                 &(tv->table->data[i - 1][0]), tv->row_size);
1733c2c66affSColin Finck         tv->table->data_persistent[i] = tv->table->data_persistent[i - 1];
1734c2c66affSColin Finck     }
1735c2c66affSColin Finck 
1736c2c66affSColin Finck     /* Re-set the persistence flag */
1737c2c66affSColin Finck     tv->table->data_persistent[row] = !temporary;
1738c2c66affSColin Finck     return TABLE_set_row( view, row, rec, (1<<tv->num_cols) - 1 );
1739c2c66affSColin Finck }
1740c2c66affSColin Finck 
TABLE_delete_row(struct tagMSIVIEW * view,UINT row)1741c2c66affSColin Finck static UINT TABLE_delete_row( struct tagMSIVIEW *view, UINT row )
1742c2c66affSColin Finck {
1743*f4be6dc3SMikhail     struct table_view *tv = (struct table_view *)view;
1744c2c66affSColin Finck     UINT r, num_rows, num_cols, i;
1745c2c66affSColin Finck 
1746c2c66affSColin Finck     TRACE("%p %d\n", tv, row);
1747c2c66affSColin Finck 
1748c2c66affSColin Finck     if ( !tv->table )
1749c2c66affSColin Finck         return ERROR_INVALID_PARAMETER;
1750c2c66affSColin Finck 
1751c2c66affSColin Finck     r = TABLE_get_dimensions( view, &num_rows, &num_cols );
1752c2c66affSColin Finck     if ( r != ERROR_SUCCESS )
1753c2c66affSColin Finck         return r;
1754c2c66affSColin Finck 
1755c2c66affSColin Finck     if ( row >= num_rows )
1756c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
1757c2c66affSColin Finck 
1758c2c66affSColin Finck     num_rows = tv->table->row_count;
1759c2c66affSColin Finck     tv->table->row_count--;
1760c2c66affSColin Finck 
1761c2c66affSColin Finck     /* reset the hash tables */
1762c2c66affSColin Finck     for (i = 0; i < tv->num_cols; i++)
1763c2c66affSColin Finck     {
1764*f4be6dc3SMikhail         free(tv->columns[i].hash_table);
1765c2c66affSColin Finck         tv->columns[i].hash_table = NULL;
1766c2c66affSColin Finck     }
1767c2c66affSColin Finck 
1768c2c66affSColin Finck     for (i = row + 1; i < num_rows; i++)
1769c2c66affSColin Finck     {
1770c2c66affSColin Finck         memcpy(tv->table->data[i - 1], tv->table->data[i], tv->row_size);
1771c2c66affSColin Finck         tv->table->data_persistent[i - 1] = tv->table->data_persistent[i];
1772c2c66affSColin Finck     }
1773c2c66affSColin Finck 
1774*f4be6dc3SMikhail     free(tv->table->data[num_rows - 1]);
1775c2c66affSColin Finck 
1776c2c66affSColin Finck     return ERROR_SUCCESS;
1777c2c66affSColin Finck }
1778c2c66affSColin Finck 
table_update(struct tagMSIVIEW * view,MSIRECORD * rec,UINT row)1779*f4be6dc3SMikhail static UINT table_update(struct tagMSIVIEW *view, MSIRECORD *rec, UINT row)
1780c2c66affSColin Finck {
1781*f4be6dc3SMikhail     struct table_view *tv = (struct table_view *)view;
1782c2c66affSColin Finck     UINT r, new_row;
1783c2c66affSColin Finck 
1784c2c66affSColin Finck     /* FIXME: MsiViewFetch should set rec index 0 to some ID that
1785c2c66affSColin Finck      * sets the fetched record apart from other records
1786c2c66affSColin Finck      */
1787c2c66affSColin Finck 
1788c2c66affSColin Finck     if (!tv->table)
1789c2c66affSColin Finck         return ERROR_INVALID_PARAMETER;
1790c2c66affSColin Finck 
1791*f4be6dc3SMikhail     r = table_find_row(tv, rec, &new_row, NULL);
1792c2c66affSColin Finck     if (r != ERROR_SUCCESS)
1793c2c66affSColin Finck     {
1794c2c66affSColin Finck         ERR("can't find row to modify\n");
1795c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
1796c2c66affSColin Finck     }
1797c2c66affSColin Finck 
1798c2c66affSColin Finck     /* the row cannot be changed */
1799def8c91eSwinesync     if (row != new_row)
1800c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
1801c2c66affSColin Finck 
1802c2c66affSColin Finck     return TABLE_set_row(view, new_row, rec, (1 << tv->num_cols) - 1);
1803c2c66affSColin Finck }
1804c2c66affSColin Finck 
table_assign(struct tagMSIVIEW * view,MSIRECORD * rec)1805*f4be6dc3SMikhail static UINT table_assign(struct tagMSIVIEW *view, MSIRECORD *rec)
1806c2c66affSColin Finck {
1807*f4be6dc3SMikhail     struct table_view *tv = (struct table_view *)view;
1808c2c66affSColin Finck     UINT r, row;
1809c2c66affSColin Finck 
1810c2c66affSColin Finck     if (!tv->table)
1811c2c66affSColin Finck         return ERROR_INVALID_PARAMETER;
1812c2c66affSColin Finck 
1813*f4be6dc3SMikhail     r = table_find_row(tv, rec, &row, NULL);
1814c2c66affSColin Finck     if (r == ERROR_SUCCESS)
1815c2c66affSColin Finck         return TABLE_set_row(view, row, rec, (1 << tv->num_cols) - 1);
1816c2c66affSColin Finck     else
1817c2c66affSColin Finck         return TABLE_insert_row( view, rec, -1, FALSE );
1818c2c66affSColin Finck }
1819c2c66affSColin Finck 
refresh_record(struct tagMSIVIEW * view,MSIRECORD * rec,UINT row)1820*f4be6dc3SMikhail static UINT refresh_record( struct tagMSIVIEW *view, MSIRECORD *rec, UINT row )
1821c2c66affSColin Finck {
1822c2c66affSColin Finck     MSIRECORD *curr;
1823c2c66affSColin Finck     UINT r, i, count;
1824c2c66affSColin Finck 
1825def8c91eSwinesync     r = TABLE_get_row(view, row, &curr);
1826c2c66affSColin Finck     if (r != ERROR_SUCCESS)
1827c2c66affSColin Finck         return r;
1828c2c66affSColin Finck 
1829c2c66affSColin Finck     /* Close the original record */
1830c2c66affSColin Finck     MSI_CloseRecord(&rec->hdr);
1831c2c66affSColin Finck 
1832c2c66affSColin Finck     count = MSI_RecordGetFieldCount(rec);
1833c2c66affSColin Finck     for (i = 0; i < count; i++)
1834c2c66affSColin Finck         MSI_RecordCopyField(curr, i + 1, rec, i + 1);
1835c2c66affSColin Finck 
1836c2c66affSColin Finck     msiobj_release(&curr->hdr);
1837c2c66affSColin Finck     return ERROR_SUCCESS;
1838c2c66affSColin Finck }
1839c2c66affSColin Finck 
TABLE_modify(struct tagMSIVIEW * view,MSIMODIFY eModifyMode,MSIRECORD * rec,UINT row)1840c2c66affSColin Finck static UINT TABLE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
1841c2c66affSColin Finck                           MSIRECORD *rec, UINT row)
1842c2c66affSColin Finck {
1843*f4be6dc3SMikhail     struct table_view *tv = (struct table_view *)view;
1844c2c66affSColin Finck     UINT r, frow, column;
1845c2c66affSColin Finck 
1846c2c66affSColin Finck     TRACE("%p %d %p\n", view, eModifyMode, rec );
1847c2c66affSColin Finck 
1848c2c66affSColin Finck     switch (eModifyMode)
1849c2c66affSColin Finck     {
1850c2c66affSColin Finck     case MSIMODIFY_DELETE:
18515f1570f4Swinesync         r = TABLE_delete_row( view, row );
1852c2c66affSColin Finck         break;
1853c2c66affSColin Finck     case MSIMODIFY_VALIDATE_NEW:
1854c2c66affSColin Finck         r = table_validate_new( tv, rec, &column );
1855c2c66affSColin Finck         if (r != ERROR_SUCCESS)
1856c2c66affSColin Finck         {
1857c2c66affSColin Finck             tv->view.error = MSIDBERROR_DUPLICATEKEY;
1858c2c66affSColin Finck             tv->view.error_column = tv->columns[column].colname;
1859c2c66affSColin Finck             r = ERROR_INVALID_DATA;
1860c2c66affSColin Finck         }
1861c2c66affSColin Finck         break;
1862c2c66affSColin Finck 
1863c2c66affSColin Finck     case MSIMODIFY_INSERT:
1864c2c66affSColin Finck         r = table_validate_new( tv, rec, NULL );
1865c2c66affSColin Finck         if (r != ERROR_SUCCESS)
1866c2c66affSColin Finck             break;
1867c2c66affSColin Finck         r = TABLE_insert_row( view, rec, -1, FALSE );
1868c2c66affSColin Finck         break;
1869c2c66affSColin Finck 
1870c2c66affSColin Finck     case MSIMODIFY_INSERT_TEMPORARY:
1871c2c66affSColin Finck         r = table_validate_new( tv, rec, NULL );
1872c2c66affSColin Finck         if (r != ERROR_SUCCESS)
1873c2c66affSColin Finck             break;
1874c2c66affSColin Finck         r = TABLE_insert_row( view, rec, -1, TRUE );
1875c2c66affSColin Finck         break;
1876c2c66affSColin Finck 
1877c2c66affSColin Finck     case MSIMODIFY_REFRESH:
1878*f4be6dc3SMikhail         r = refresh_record( view, rec, row );
1879c2c66affSColin Finck         break;
1880c2c66affSColin Finck 
1881c2c66affSColin Finck     case MSIMODIFY_UPDATE:
1882*f4be6dc3SMikhail         r = table_update( view, rec, row );
1883c2c66affSColin Finck         break;
1884c2c66affSColin Finck 
1885c2c66affSColin Finck     case MSIMODIFY_ASSIGN:
1886*f4be6dc3SMikhail         r = table_assign( view, rec );
1887c2c66affSColin Finck         break;
1888c2c66affSColin Finck 
1889c2c66affSColin Finck     case MSIMODIFY_MERGE:
1890c2c66affSColin Finck         /* check row that matches this record */
1891*f4be6dc3SMikhail         r = table_find_row( tv, rec, &frow, &column );
1892c2c66affSColin Finck         if (r != ERROR_SUCCESS)
1893c2c66affSColin Finck         {
1894c2c66affSColin Finck             r = table_validate_new( tv, rec, NULL );
1895c2c66affSColin Finck             if (r == ERROR_SUCCESS)
1896c2c66affSColin Finck                 r = TABLE_insert_row( view, rec, -1, FALSE );
1897c2c66affSColin Finck         }
1898c2c66affSColin Finck         break;
1899c2c66affSColin Finck 
1900c2c66affSColin Finck     case MSIMODIFY_REPLACE:
1901c2c66affSColin Finck     case MSIMODIFY_VALIDATE:
1902c2c66affSColin Finck     case MSIMODIFY_VALIDATE_FIELD:
1903c2c66affSColin Finck     case MSIMODIFY_VALIDATE_DELETE:
1904c2c66affSColin Finck         FIXME("%p %d %p - mode not implemented\n", view, eModifyMode, rec );
1905c2c66affSColin Finck         r = ERROR_CALL_NOT_IMPLEMENTED;
1906c2c66affSColin Finck         break;
1907c2c66affSColin Finck 
1908c2c66affSColin Finck     default:
1909c2c66affSColin Finck         r = ERROR_INVALID_DATA;
1910c2c66affSColin Finck     }
1911c2c66affSColin Finck 
1912c2c66affSColin Finck     return r;
1913c2c66affSColin Finck }
1914c2c66affSColin Finck 
TABLE_delete(struct tagMSIVIEW * view)1915c2c66affSColin Finck static UINT TABLE_delete( struct tagMSIVIEW *view )
1916c2c66affSColin Finck {
1917*f4be6dc3SMikhail     struct table_view *tv = (struct table_view *)view;
1918c2c66affSColin Finck 
1919c2c66affSColin Finck     TRACE("%p\n", view );
1920c2c66affSColin Finck 
1921c2c66affSColin Finck     tv->table = NULL;
1922c2c66affSColin Finck     tv->columns = NULL;
1923c2c66affSColin Finck 
1924*f4be6dc3SMikhail     free( tv );
1925c2c66affSColin Finck 
1926c2c66affSColin Finck     return ERROR_SUCCESS;
1927c2c66affSColin Finck }
1928c2c66affSColin Finck 
TABLE_add_ref(struct tagMSIVIEW * view)1929c2c66affSColin Finck static UINT TABLE_add_ref(struct tagMSIVIEW *view)
1930c2c66affSColin Finck {
1931*f4be6dc3SMikhail     struct table_view *tv = (struct table_view *)view;
1932c2c66affSColin Finck 
193302f995b2Swinesync     TRACE( "%p, %ld\n", view, tv->table->ref_count );
1934c2c66affSColin Finck     return InterlockedIncrement(&tv->table->ref_count);
1935c2c66affSColin Finck }
1936c2c66affSColin Finck 
TABLE_remove_column(struct tagMSIVIEW * view,UINT number)1937ed0e61b5Swinesync static UINT TABLE_remove_column(struct tagMSIVIEW *view, UINT number)
1938c2c66affSColin Finck {
1939*f4be6dc3SMikhail     struct table_view *tv = (struct table_view *)view;
1940c2c66affSColin Finck     MSIRECORD *rec = NULL;
1941c2c66affSColin Finck     MSIVIEW *columns = NULL;
1942c2c66affSColin Finck     UINT row, r;
1943c2c66affSColin Finck 
19445c718f5fSwinesync     if (tv->table->col_count != number)
19455c718f5fSwinesync         return ERROR_BAD_QUERY_SYNTAX;
19465c718f5fSwinesync 
194761167e12Swinesync     if (tv->table->colinfo[number-1].type & MSITYPE_TEMPORARY)
19485c718f5fSwinesync     {
19495c718f5fSwinesync         UINT size = tv->table->colinfo[number-1].offset;
19505c718f5fSwinesync         tv->table->col_count--;
1951*f4be6dc3SMikhail         tv->table->colinfo = realloc(tv->table->colinfo, sizeof(*tv->table->colinfo) * tv->table->col_count);
19525c718f5fSwinesync 
19535c718f5fSwinesync         for (row = 0; row < tv->table->row_count; row++)
1954*f4be6dc3SMikhail             tv->table->data[row] = realloc(tv->table->data[row], size);
19555c718f5fSwinesync         return ERROR_SUCCESS;
19565c718f5fSwinesync     }
19575c718f5fSwinesync 
1958c2c66affSColin Finck     rec = MSI_CreateRecord(2);
1959c2c66affSColin Finck     if (!rec)
1960c2c66affSColin Finck         return ERROR_OUTOFMEMORY;
1961c2c66affSColin Finck 
1962ed0e61b5Swinesync     MSI_RecordSetStringW(rec, 1, tv->name);
1963c2c66affSColin Finck     MSI_RecordSetInteger(rec, 2, number);
1964c2c66affSColin Finck 
196574f53b4bSwinesync     r = TABLE_CreateView(tv->db, L"_Columns", &columns);
1966c2c66affSColin Finck     if (r != ERROR_SUCCESS)
1967c2c66affSColin Finck     {
1968c2c66affSColin Finck         msiobj_release(&rec->hdr);
1969c2c66affSColin Finck         return r;
1970c2c66affSColin Finck     }
1971c2c66affSColin Finck 
1972*f4be6dc3SMikhail     r = table_find_row((struct table_view *)columns, rec, &row, NULL);
1973c2c66affSColin Finck     if (r != ERROR_SUCCESS)
1974c2c66affSColin Finck         goto done;
1975c2c66affSColin Finck 
1976c2c66affSColin Finck     r = TABLE_delete_row(columns, row);
1977c2c66affSColin Finck     if (r != ERROR_SUCCESS)
1978c2c66affSColin Finck         goto done;
1979c2c66affSColin Finck 
1980*f4be6dc3SMikhail     update_table_columns(tv->db, tv->name);
1981c2c66affSColin Finck 
1982c2c66affSColin Finck done:
1983c2c66affSColin Finck     msiobj_release(&rec->hdr);
1984c2c66affSColin Finck     columns->ops->delete(columns);
1985c2c66affSColin Finck     return r;
1986c2c66affSColin Finck }
1987c2c66affSColin Finck 
TABLE_release(struct tagMSIVIEW * view)1988c2c66affSColin Finck static UINT TABLE_release(struct tagMSIVIEW *view)
1989c2c66affSColin Finck {
1990*f4be6dc3SMikhail     struct table_view *tv = (struct table_view *)view;
1991c2c66affSColin Finck     INT ref = tv->table->ref_count;
19925c718f5fSwinesync     UINT r;
19935c718f5fSwinesync     INT i;
1994c2c66affSColin Finck 
1995c2c66affSColin Finck     TRACE("%p %d\n", view, ref);
1996c2c66affSColin Finck 
1997eb6788c9Swinesync     ref = InterlockedDecrement(&tv->table->ref_count);
1998eb6788c9Swinesync     if (ref == 0)
1999eb6788c9Swinesync     {
20005c718f5fSwinesync         for (i = tv->table->col_count - 1; i >= 0; i--)
2001c2c66affSColin Finck         {
2002c2c66affSColin Finck             if (tv->table->colinfo[i].type & MSITYPE_TEMPORARY)
2003c2c66affSColin Finck             {
2004ed0e61b5Swinesync                 r = TABLE_remove_column(view, tv->table->colinfo[i].number);
2005c2c66affSColin Finck                 if (r != ERROR_SUCCESS)
2006c2c66affSColin Finck                     break;
2007c2c66affSColin Finck             }
2008eb6788c9Swinesync             else
2009eb6788c9Swinesync             {
2010eb6788c9Swinesync                 break;
2011c2c66affSColin Finck             }
2012c2c66affSColin Finck         }
2013c2c66affSColin Finck 
2014eb6788c9Swinesync         if (!tv->table->col_count)
2015c2c66affSColin Finck         {
2016c2c66affSColin Finck             list_remove(&tv->table->entry);
2017c2c66affSColin Finck             free_table(tv->table);
2018c2c66affSColin Finck             TABLE_delete(view);
2019c2c66affSColin Finck         }
2020c2c66affSColin Finck     }
2021c2c66affSColin Finck 
2022c2c66affSColin Finck     return ref;
2023c2c66affSColin Finck }
2024c2c66affSColin Finck 
TABLE_add_column(struct tagMSIVIEW * view,LPCWSTR column,INT type,BOOL hold)20259cf1ecf2Swinesync static UINT TABLE_add_column(struct tagMSIVIEW *view, LPCWSTR column,
2026791734c2Swinesync                              INT type, BOOL hold)
2027c2c66affSColin Finck {
20289cf1ecf2Swinesync     UINT i, r, table_id, col_id, size, offset;
2029791734c2Swinesync     BOOL temporary = type & MSITYPE_TEMPORARY;
2030*f4be6dc3SMikhail     struct table_view *tv = (struct table_view *)view;
2031*f4be6dc3SMikhail     struct column_info *colinfo;
20329cf1ecf2Swinesync 
20339cf1ecf2Swinesync     if (temporary && !hold && !tv->table->ref_count)
20349cf1ecf2Swinesync         return ERROR_SUCCESS;
20359cf1ecf2Swinesync 
20369cf1ecf2Swinesync     if (!temporary && tv->table->col_count &&
203761167e12Swinesync             tv->table->colinfo[tv->table->col_count-1].type & MSITYPE_TEMPORARY)
20389cf1ecf2Swinesync         return ERROR_BAD_QUERY_SYNTAX;
20399cf1ecf2Swinesync 
20409cf1ecf2Swinesync     for (i = 0; i < tv->table->col_count; i++)
20419cf1ecf2Swinesync     {
20429cf1ecf2Swinesync         if (!wcscmp(tv->table->colinfo[i].colname, column))
20439cf1ecf2Swinesync             return ERROR_BAD_QUERY_SYNTAX;
20449cf1ecf2Swinesync     }
20459cf1ecf2Swinesync 
2046*f4be6dc3SMikhail     colinfo = realloc(tv->table->colinfo, sizeof(*tv->table->colinfo) * (tv->table->col_count + 1));
20479cf1ecf2Swinesync     if (!colinfo)
20489cf1ecf2Swinesync         return ERROR_OUTOFMEMORY;
20499cf1ecf2Swinesync     tv->table->colinfo = colinfo;
20509cf1ecf2Swinesync 
20519cf1ecf2Swinesync     r = msi_string2id( tv->db->strings, tv->name, -1, &table_id );
20529cf1ecf2Swinesync     if (r != ERROR_SUCCESS)
20539cf1ecf2Swinesync         return r;
20549cf1ecf2Swinesync     col_id = msi_add_string( tv->db->strings, column, -1, !temporary );
20559cf1ecf2Swinesync 
20569cf1ecf2Swinesync     colinfo[tv->table->col_count].tablename = msi_string_lookup( tv->db->strings, table_id, NULL );
20579cf1ecf2Swinesync     colinfo[tv->table->col_count].number = tv->table->col_count + 1;
20589cf1ecf2Swinesync     colinfo[tv->table->col_count].colname = msi_string_lookup( tv->db->strings, col_id, NULL );
20599cf1ecf2Swinesync     colinfo[tv->table->col_count].type = type;
20609cf1ecf2Swinesync     colinfo[tv->table->col_count].offset = 0;
20619cf1ecf2Swinesync     colinfo[tv->table->col_count].hash_table = NULL;
20629cf1ecf2Swinesync     tv->table->col_count++;
20639cf1ecf2Swinesync 
20649cf1ecf2Swinesync     table_calc_column_offsets( tv->db, tv->table->colinfo, tv->table->col_count);
20659cf1ecf2Swinesync 
2066*f4be6dc3SMikhail     size = table_get_row_size( tv->db, tv->table->colinfo, tv->table->col_count, LONG_STR_BYTES );
20679cf1ecf2Swinesync     offset = tv->table->colinfo[tv->table->col_count - 1].offset;
20689cf1ecf2Swinesync     for (i = 0; i < tv->table->row_count; i++)
20699cf1ecf2Swinesync     {
2070*f4be6dc3SMikhail         BYTE *data = realloc(tv->table->data[i], size);
20719cf1ecf2Swinesync         if (!data)
20729cf1ecf2Swinesync         {
20739cf1ecf2Swinesync             tv->table->col_count--;
20749cf1ecf2Swinesync             return ERROR_OUTOFMEMORY;
20759cf1ecf2Swinesync         }
20769cf1ecf2Swinesync 
20779cf1ecf2Swinesync         tv->table->data[i] = data;
20789cf1ecf2Swinesync         memset(data + offset, 0, size - offset);
20799cf1ecf2Swinesync     }
20809cf1ecf2Swinesync 
20819cf1ecf2Swinesync     if (!temporary)
20829cf1ecf2Swinesync     {
20839cf1ecf2Swinesync         MSIVIEW *columns;
2084c2c66affSColin Finck         MSIRECORD *rec;
2085c2c66affSColin Finck 
2086c2c66affSColin Finck         rec = MSI_CreateRecord(4);
2087c2c66affSColin Finck         if (!rec)
20889cf1ecf2Swinesync         {
20899cf1ecf2Swinesync             tv->table->col_count--;
2090c2c66affSColin Finck             return ERROR_OUTOFMEMORY;
20919cf1ecf2Swinesync         }
2092c2c66affSColin Finck 
20939cf1ecf2Swinesync         MSI_RecordSetStringW(rec, 1, tv->name);
20949cf1ecf2Swinesync         MSI_RecordSetInteger(rec, 2, tv->table->col_count);
2095c2c66affSColin Finck         MSI_RecordSetStringW(rec, 3, column);
2096c2c66affSColin Finck         MSI_RecordSetInteger(rec, 4, type);
2097c2c66affSColin Finck 
209874f53b4bSwinesync         r = TABLE_CreateView(tv->db, L"_Columns", &columns);
2099c2c66affSColin Finck         if (r != ERROR_SUCCESS)
21009cf1ecf2Swinesync         {
21019cf1ecf2Swinesync             tv->table->col_count--;
2102c2c66affSColin Finck             msiobj_release(&rec->hdr);
2103c2c66affSColin Finck             return r;
2104c2c66affSColin Finck         }
2105c2c66affSColin Finck 
21069cf1ecf2Swinesync         r = TABLE_insert_row(columns, rec, -1, FALSE);
21079cf1ecf2Swinesync         columns->ops->delete(columns);
21089cf1ecf2Swinesync         msiobj_release(&rec->hdr);
21099cf1ecf2Swinesync         if (r != ERROR_SUCCESS)
21109cf1ecf2Swinesync         {
21119cf1ecf2Swinesync             tv->table->col_count--;
21129cf1ecf2Swinesync             return r;
21139cf1ecf2Swinesync         }
21149cf1ecf2Swinesync     }
21159cf1ecf2Swinesync     if (hold)
21169cf1ecf2Swinesync         TABLE_add_ref(view);
21179cf1ecf2Swinesync     return ERROR_SUCCESS;
21189cf1ecf2Swinesync }
21199cf1ecf2Swinesync 
TABLE_drop(struct tagMSIVIEW * view)2120c2c66affSColin Finck static UINT TABLE_drop(struct tagMSIVIEW *view)
2121c2c66affSColin Finck {
2122*f4be6dc3SMikhail     struct table_view *tv = (struct table_view *)view;
2123c2c66affSColin Finck     MSIVIEW *tables = NULL;
2124c2c66affSColin Finck     MSIRECORD *rec = NULL;
2125c2c66affSColin Finck     UINT r, row;
2126c2c66affSColin Finck     INT i;
2127c2c66affSColin Finck 
2128c2c66affSColin Finck     TRACE("dropping table %s\n", debugstr_w(tv->name));
2129c2c66affSColin Finck 
2130c2c66affSColin Finck     for (i = tv->table->col_count - 1; i >= 0; i--)
2131c2c66affSColin Finck     {
2132ed0e61b5Swinesync         r = TABLE_remove_column(view, tv->table->colinfo[i].number);
2133c2c66affSColin Finck         if (r != ERROR_SUCCESS)
2134c2c66affSColin Finck             return r;
2135c2c66affSColin Finck     }
2136c2c66affSColin Finck 
2137c2c66affSColin Finck     rec = MSI_CreateRecord(1);
2138c2c66affSColin Finck     if (!rec)
2139c2c66affSColin Finck         return ERROR_OUTOFMEMORY;
2140c2c66affSColin Finck 
2141c2c66affSColin Finck     MSI_RecordSetStringW(rec, 1, tv->name);
2142c2c66affSColin Finck 
214374f53b4bSwinesync     r = TABLE_CreateView(tv->db, L"_Tables", &tables);
2144c2c66affSColin Finck     if (r != ERROR_SUCCESS)
2145c2c66affSColin Finck     {
2146c2c66affSColin Finck         msiobj_release(&rec->hdr);
2147c2c66affSColin Finck         return r;
2148c2c66affSColin Finck     }
2149c2c66affSColin Finck 
2150*f4be6dc3SMikhail     r = table_find_row((struct table_view *)tables, rec, &row, NULL);
2151c2c66affSColin Finck     if (r != ERROR_SUCCESS)
2152c2c66affSColin Finck         goto done;
2153c2c66affSColin Finck 
2154c2c66affSColin Finck     r = TABLE_delete_row(tables, row);
2155c2c66affSColin Finck     if (r != ERROR_SUCCESS)
2156c2c66affSColin Finck         goto done;
2157c2c66affSColin Finck 
2158c2c66affSColin Finck     list_remove(&tv->table->entry);
2159c2c66affSColin Finck     free_table(tv->table);
2160c2c66affSColin Finck 
2161c2c66affSColin Finck done:
2162c2c66affSColin Finck     msiobj_release(&rec->hdr);
2163c2c66affSColin Finck     tables->ops->delete(tables);
2164c2c66affSColin Finck 
2165c2c66affSColin Finck     return r;
2166c2c66affSColin Finck }
2167c2c66affSColin Finck 
2168c2c66affSColin Finck static const MSIVIEWOPS table_ops =
2169c2c66affSColin Finck {
2170c2c66affSColin Finck     TABLE_fetch_int,
2171c2c66affSColin Finck     TABLE_fetch_stream,
217297918c36Swinesync     TABLE_set_int,
217397918c36Swinesync     TABLE_set_string,
2174580d3e1bSwinesync     TABLE_set_stream,
2175c2c66affSColin Finck     TABLE_set_row,
2176c2c66affSColin Finck     TABLE_insert_row,
2177c2c66affSColin Finck     TABLE_delete_row,
2178c2c66affSColin Finck     TABLE_execute,
2179c2c66affSColin Finck     TABLE_close,
2180c2c66affSColin Finck     TABLE_get_dimensions,
2181c2c66affSColin Finck     TABLE_get_column_info,
2182c2c66affSColin Finck     TABLE_modify,
2183c2c66affSColin Finck     TABLE_delete,
2184c2c66affSColin Finck     TABLE_add_ref,
2185c2c66affSColin Finck     TABLE_release,
2186c2c66affSColin Finck     TABLE_add_column,
2187c2c66affSColin Finck     NULL,
2188c2c66affSColin Finck     TABLE_drop,
2189c2c66affSColin Finck };
2190c2c66affSColin Finck 
TABLE_CreateView(MSIDATABASE * db,LPCWSTR name,MSIVIEW ** view)2191c2c66affSColin Finck UINT TABLE_CreateView( MSIDATABASE *db, LPCWSTR name, MSIVIEW **view )
2192c2c66affSColin Finck {
2193*f4be6dc3SMikhail     struct table_view *tv ;
2194c2c66affSColin Finck     UINT r, sz;
2195c2c66affSColin Finck 
2196c2c66affSColin Finck     TRACE("%p %s %p\n", db, debugstr_w(name), view );
2197c2c66affSColin Finck 
219874f53b4bSwinesync     if ( !wcscmp( name, L"_Streams" ) )
2199c2c66affSColin Finck         return STREAMS_CreateView( db, view );
220074f53b4bSwinesync     else if ( !wcscmp( name, L"_Storages" ) )
2201c2c66affSColin Finck         return STORAGES_CreateView( db, view );
2202c2c66affSColin Finck 
2203*f4be6dc3SMikhail     sz = FIELD_OFFSET( struct table_view, name[lstrlenW( name ) + 1] );
2204*f4be6dc3SMikhail     tv = calloc( 1, sz );
2205c2c66affSColin Finck     if( !tv )
2206c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
2207c2c66affSColin Finck 
2208c2c66affSColin Finck     r = get_table( db, name, &tv->table );
2209c2c66affSColin Finck     if( r != ERROR_SUCCESS )
2210c2c66affSColin Finck     {
2211*f4be6dc3SMikhail         free( tv );
2212c2c66affSColin Finck         WARN("table not found\n");
2213c2c66affSColin Finck         return r;
2214c2c66affSColin Finck     }
2215c2c66affSColin Finck 
2216c2c66affSColin Finck     TRACE("table %p found with %d columns\n", tv->table, tv->table->col_count);
2217c2c66affSColin Finck 
2218c2c66affSColin Finck     /* fill the structure */
2219c2c66affSColin Finck     tv->view.ops = &table_ops;
2220c2c66affSColin Finck     tv->db = db;
2221c2c66affSColin Finck     tv->columns = tv->table->colinfo;
2222c2c66affSColin Finck     tv->num_cols = tv->table->col_count;
2223*f4be6dc3SMikhail     tv->row_size = table_get_row_size( db, tv->table->colinfo, tv->table->col_count, LONG_STR_BYTES );
2224c2c66affSColin Finck 
2225c2c66affSColin Finck     TRACE("%s one row is %d bytes\n", debugstr_w(name), tv->row_size );
2226c2c66affSColin Finck 
2227c2c66affSColin Finck     *view = (MSIVIEW*) tv;
2228c2c66affSColin Finck     lstrcpyW( tv->name, name );
2229c2c66affSColin Finck 
2230c2c66affSColin Finck     return ERROR_SUCCESS;
2231c2c66affSColin Finck }
2232c2c66affSColin Finck 
create_key_string(struct table_view * tv,MSIRECORD * rec)2233*f4be6dc3SMikhail static WCHAR *create_key_string(struct table_view *tv, MSIRECORD *rec)
2234317c1f6cSwinesync {
2235317c1f6cSwinesync     DWORD i, p, len, key_len = 0;
2236317c1f6cSwinesync     WCHAR *key;
2237317c1f6cSwinesync 
2238317c1f6cSwinesync     for (i = 0; i < tv->num_cols; i++)
2239317c1f6cSwinesync     {
2240317c1f6cSwinesync         if (!(tv->columns[i].type & MSITYPE_KEY))
2241317c1f6cSwinesync             continue;
2242317c1f6cSwinesync         if (MSI_RecordGetStringW( rec, i+1, NULL, &len ) == ERROR_SUCCESS)
2243317c1f6cSwinesync             key_len += len;
2244317c1f6cSwinesync         key_len++;
2245317c1f6cSwinesync     }
2246317c1f6cSwinesync 
2247*f4be6dc3SMikhail     key = malloc( key_len * sizeof(WCHAR) );
2248317c1f6cSwinesync     if(!key)
2249317c1f6cSwinesync         return NULL;
2250317c1f6cSwinesync 
2251317c1f6cSwinesync     p = 0;
2252317c1f6cSwinesync     for (i = 0; i < tv->num_cols; i++)
2253317c1f6cSwinesync     {
2254317c1f6cSwinesync         if (!(tv->columns[i].type & MSITYPE_KEY))
2255317c1f6cSwinesync             continue;
2256317c1f6cSwinesync         if (p)
2257317c1f6cSwinesync             key[p++] = '\t';
2258317c1f6cSwinesync         len = key_len - p;
2259317c1f6cSwinesync         if (MSI_RecordGetStringW( rec, i+1, key + p, &len ) == ERROR_SUCCESS)
2260317c1f6cSwinesync             p += len;
2261317c1f6cSwinesync     }
2262317c1f6cSwinesync     return key;
2263317c1f6cSwinesync }
2264317c1f6cSwinesync 
record_stream_name(const struct table_view * tv,MSIRECORD * rec,WCHAR * name,DWORD * len)2265*f4be6dc3SMikhail static UINT record_stream_name( const struct table_view *tv, MSIRECORD *rec, WCHAR *name, DWORD *len )
22668e75cb8aSwinesync {
226736bf5549Swinesync     UINT p = 0, i, r;
226836bf5549Swinesync     DWORD l;
22698e75cb8aSwinesync 
22708e75cb8aSwinesync     l = wcslen( tv->name );
22718e75cb8aSwinesync     if (name && *len > l)
22728e75cb8aSwinesync         memcpy(name, tv->name, l * sizeof(WCHAR));
22738e75cb8aSwinesync     p += l;
22748e75cb8aSwinesync 
22758e75cb8aSwinesync     for ( i = 0; i < tv->num_cols; i++ )
22768e75cb8aSwinesync     {
22778e75cb8aSwinesync         if (!(tv->columns[i].type & MSITYPE_KEY))
22788e75cb8aSwinesync             continue;
22798e75cb8aSwinesync 
22808e75cb8aSwinesync         if (name && *len > p + 1)
22818e75cb8aSwinesync             name[p] = '.';
22828e75cb8aSwinesync         p++;
22838e75cb8aSwinesync 
22848e75cb8aSwinesync         l = (*len > p ? *len - p : 0);
22858e75cb8aSwinesync         r = MSI_RecordGetStringW( rec, i + 1, name ? name + p : NULL, &l );
22868e75cb8aSwinesync         if (r != ERROR_SUCCESS)
22878e75cb8aSwinesync             return r;
22888e75cb8aSwinesync         p += l;
22898e75cb8aSwinesync     }
22908e75cb8aSwinesync 
22918e75cb8aSwinesync     if (name && *len > p)
22928e75cb8aSwinesync         name[p] = 0;
22938e75cb8aSwinesync 
22948e75cb8aSwinesync     *len = p;
22958e75cb8aSwinesync     return ERROR_SUCCESS;
22968e75cb8aSwinesync }
22978e75cb8aSwinesync 
TransformView_fetch_int(MSIVIEW * view,UINT row,UINT col,UINT * val)22981bbb87bbSwinesync static UINT TransformView_fetch_int( MSIVIEW *view, UINT row, UINT col, UINT *val )
22991bbb87bbSwinesync {
2300*f4be6dc3SMikhail     struct table_view *tv = (struct table_view *)view;
230127a430bdSwinesync 
230228c50c1aSwinesync     if (!tv->table || col > tv->table->col_count)
230327a430bdSwinesync     {
230427a430bdSwinesync         *val = 0;
230527a430bdSwinesync         return ERROR_SUCCESS;
230627a430bdSwinesync     }
2307317c1f6cSwinesync     return TABLE_fetch_int( view, row, col, val );
23081bbb87bbSwinesync }
23091bbb87bbSwinesync 
TransformView_fetch_stream(MSIVIEW * view,UINT row,UINT col,IStream ** stm)23101bbb87bbSwinesync static UINT TransformView_fetch_stream( MSIVIEW *view, UINT row, UINT col, IStream **stm )
23111bbb87bbSwinesync {
2312*f4be6dc3SMikhail     struct table_view *tv = (struct table_view *)view;
231327a430bdSwinesync 
231428c50c1aSwinesync     if (!tv->table || col > tv->table->col_count)
231527a430bdSwinesync     {
231627a430bdSwinesync         *stm = NULL;
231727a430bdSwinesync         return ERROR_SUCCESS;
231827a430bdSwinesync     }
2319317c1f6cSwinesync     return TABLE_fetch_stream( view, row, col, stm );
23201bbb87bbSwinesync }
23211bbb87bbSwinesync 
TransformView_set_row(MSIVIEW * view,UINT row,MSIRECORD * rec,UINT mask)23221bbb87bbSwinesync static UINT TransformView_set_row( MSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask )
23231bbb87bbSwinesync {
2324317c1f6cSwinesync     static const WCHAR query_pfx[] =
23251c5160dcSwinesync         L"INSERT INTO `_TransformView` (`new`, `Table`, `Column`, `Row`, `Data`, `Current`) VALUES (1, '";
2326317c1f6cSwinesync 
2327*f4be6dc3SMikhail     struct table_view *tv = (struct table_view *)view;
2328c6c4bdb2Swinesync     WCHAR buf[256], *query;
2329317c1f6cSwinesync     MSIRECORD *old_rec;
2330317c1f6cSwinesync     MSIQUERY *q;
2331317c1f6cSwinesync     WCHAR *key;
233236bf5549Swinesync     UINT i, p, r, qlen;
233336bf5549Swinesync     DWORD len;
2334317c1f6cSwinesync 
233574f53b4bSwinesync     if (!wcscmp( tv->name, L"_Columns" ))
2336317c1f6cSwinesync     {
2337317c1f6cSwinesync         ERR( "trying to modify existing column\n" );
2338317c1f6cSwinesync         return ERROR_INSTALL_TRANSFORM_FAILURE;
2339317c1f6cSwinesync     }
2340317c1f6cSwinesync 
234174f53b4bSwinesync     if (!wcscmp( tv->name, L"_Tables" ))
2342317c1f6cSwinesync     {
2343317c1f6cSwinesync         ERR( "trying to modify existing table\n" );
2344317c1f6cSwinesync         return ERROR_INSTALL_TRANSFORM_FAILURE;
2345317c1f6cSwinesync     }
2346317c1f6cSwinesync 
2347317c1f6cSwinesync     key = create_key_string( tv, rec );
2348317c1f6cSwinesync     if (!key)
2349317c1f6cSwinesync         return ERROR_OUTOFMEMORY;
2350317c1f6cSwinesync 
2351317c1f6cSwinesync     r = msi_view_get_row( tv->db, view, row, &old_rec );
2352317c1f6cSwinesync     if (r != ERROR_SUCCESS)
2353317c1f6cSwinesync         old_rec = NULL;
2354317c1f6cSwinesync 
2355317c1f6cSwinesync     for (i = 0; i < tv->num_cols; i++)
2356317c1f6cSwinesync     {
2357317c1f6cSwinesync         if (!(mask & (1 << i)))
2358317c1f6cSwinesync             continue;
2359317c1f6cSwinesync         if (tv->columns[i].type & MSITYPE_KEY)
2360317c1f6cSwinesync             continue;
2361317c1f6cSwinesync 
23621c5160dcSwinesync         qlen = p = ARRAY_SIZE( query_pfx ) - 1;
2363317c1f6cSwinesync         qlen += wcslen( tv->name ) + 3; /* strlen("','") */
2364317c1f6cSwinesync         qlen += wcslen( tv->columns[i].colname ) + 3;
2365317c1f6cSwinesync         qlen += wcslen( key ) + 3;
2366317c1f6cSwinesync         if (MSITYPE_IS_BINARY( tv->columns[i].type ))
2367*f4be6dc3SMikhail             r = record_stream_name( tv, rec, NULL, &len );
2368317c1f6cSwinesync         else
2369317c1f6cSwinesync             r = MSI_RecordGetStringW( rec, i + 1, NULL, &len );
2370317c1f6cSwinesync         if (r != ERROR_SUCCESS)
2371317c1f6cSwinesync         {
2372317c1f6cSwinesync             if (old_rec)
2373317c1f6cSwinesync                 msiobj_release( &old_rec->hdr );
2374*f4be6dc3SMikhail             free( key );
2375317c1f6cSwinesync             return r;
2376317c1f6cSwinesync         }
2377317c1f6cSwinesync         qlen += len + 3;
2378317c1f6cSwinesync         if (old_rec && (r = MSI_RecordGetStringW( old_rec, i+1, NULL, &len )))
2379317c1f6cSwinesync         {
2380317c1f6cSwinesync             msiobj_release( &old_rec->hdr );
2381*f4be6dc3SMikhail             free( key );
2382317c1f6cSwinesync             return r;
2383317c1f6cSwinesync         }
2384317c1f6cSwinesync         qlen += len + 3; /* strlen("')") + 1 */
2385317c1f6cSwinesync 
2386317c1f6cSwinesync         if (qlen > ARRAY_SIZE(buf))
2387317c1f6cSwinesync         {
2388*f4be6dc3SMikhail             query = malloc( qlen * sizeof(WCHAR) );
2389317c1f6cSwinesync             if (!query)
2390317c1f6cSwinesync             {
2391317c1f6cSwinesync                 if (old_rec)
2392317c1f6cSwinesync                     msiobj_release( &old_rec->hdr );
2393*f4be6dc3SMikhail                 free( key );
2394317c1f6cSwinesync                 return ERROR_OUTOFMEMORY;
2395317c1f6cSwinesync             }
2396317c1f6cSwinesync         }
2397c6c4bdb2Swinesync         else
2398c6c4bdb2Swinesync         {
2399c6c4bdb2Swinesync             query = buf;
2400c6c4bdb2Swinesync         }
2401317c1f6cSwinesync 
2402317c1f6cSwinesync         memcpy( query, query_pfx, p * sizeof(WCHAR) );
2403317c1f6cSwinesync         len = wcslen( tv->name );
2404317c1f6cSwinesync         memcpy( query + p, tv->name, len * sizeof(WCHAR) );
2405317c1f6cSwinesync         p += len;
2406317c1f6cSwinesync         query[p++] = '\'';
2407317c1f6cSwinesync         query[p++] = ',';
2408317c1f6cSwinesync         query[p++] = '\'';
2409317c1f6cSwinesync         len = wcslen( tv->columns[i].colname );
2410317c1f6cSwinesync         memcpy( query + p, tv->columns[i].colname, len * sizeof(WCHAR) );
2411317c1f6cSwinesync         p += len;
2412317c1f6cSwinesync         query[p++] = '\'';
2413317c1f6cSwinesync         query[p++] = ',';
2414317c1f6cSwinesync         query[p++] = '\'';
2415317c1f6cSwinesync         len = wcslen( key );
2416317c1f6cSwinesync         memcpy( query + p, key, len * sizeof(WCHAR) );
2417317c1f6cSwinesync         p += len;
2418317c1f6cSwinesync         query[p++] = '\'';
2419317c1f6cSwinesync         query[p++] = ',';
2420317c1f6cSwinesync         query[p++] = '\'';
2421317c1f6cSwinesync         len = qlen - p;
2422317c1f6cSwinesync         if (MSITYPE_IS_BINARY( tv->columns[i].type ))
2423*f4be6dc3SMikhail             record_stream_name( tv, rec, query + p, &len );
2424317c1f6cSwinesync         else
2425317c1f6cSwinesync             MSI_RecordGetStringW( rec, i + 1, query + p, &len );
2426317c1f6cSwinesync         p += len;
2427317c1f6cSwinesync         query[p++] = '\'';
2428317c1f6cSwinesync         query[p++] = ',';
2429317c1f6cSwinesync         query[p++] = '\'';
2430317c1f6cSwinesync         if (old_rec)
2431317c1f6cSwinesync         {
2432317c1f6cSwinesync             len = qlen - p;
2433317c1f6cSwinesync             MSI_RecordGetStringW( old_rec, i + 1, query + p, &len );
2434317c1f6cSwinesync             p += len;
2435317c1f6cSwinesync         }
2436317c1f6cSwinesync         query[p++] = '\'';
2437317c1f6cSwinesync         query[p++] = ')';
2438317c1f6cSwinesync         query[p++] = 0;
2439317c1f6cSwinesync 
2440317c1f6cSwinesync         r = MSI_DatabaseOpenViewW( tv->db, query, &q );
2441317c1f6cSwinesync         if (query != buf)
2442*f4be6dc3SMikhail             free( query );
2443317c1f6cSwinesync         if (r != ERROR_SUCCESS)
2444317c1f6cSwinesync         {
2445317c1f6cSwinesync             if (old_rec)
2446317c1f6cSwinesync                 msiobj_release( &old_rec->hdr );
2447*f4be6dc3SMikhail             free( key );
2448317c1f6cSwinesync             return r;
2449317c1f6cSwinesync         }
2450317c1f6cSwinesync 
2451317c1f6cSwinesync         r = MSI_ViewExecute( q, NULL );
2452317c1f6cSwinesync         msiobj_release( &q->hdr );
2453317c1f6cSwinesync         if (r != ERROR_SUCCESS)
2454317c1f6cSwinesync         {
2455317c1f6cSwinesync             if (old_rec)
2456317c1f6cSwinesync                 msiobj_release( &old_rec->hdr );
2457*f4be6dc3SMikhail             free( key );
2458317c1f6cSwinesync             return r;
2459317c1f6cSwinesync         }
2460317c1f6cSwinesync     }
2461317c1f6cSwinesync 
2462317c1f6cSwinesync     if (old_rec)
2463317c1f6cSwinesync         msiobj_release( &old_rec->hdr );
2464*f4be6dc3SMikhail     free( key );
2465317c1f6cSwinesync     return ERROR_SUCCESS;
24661bbb87bbSwinesync }
24671bbb87bbSwinesync 
TransformView_create_table(struct table_view * tv,MSIRECORD * rec)2468*f4be6dc3SMikhail static UINT TransformView_create_table( struct table_view *tv, MSIRECORD *rec )
246927a430bdSwinesync {
247027a430bdSwinesync     static const WCHAR query_fmt[] =
24711c5160dcSwinesync         L"INSERT INTO `_TransformView` (`Table`, `Column`, `new`) VALUES ('%s', 'CREATE', 1)";
247227a430bdSwinesync 
247327a430bdSwinesync     WCHAR buf[256], *query = buf;
247427a430bdSwinesync     const WCHAR *name;
247527a430bdSwinesync     MSIQUERY *q;
247627a430bdSwinesync     int len;
247727a430bdSwinesync     UINT r;
247827a430bdSwinesync 
247927a430bdSwinesync     name = msi_record_get_string( rec, 1, &len );
248027a430bdSwinesync     if (!name)
248127a430bdSwinesync         return ERROR_INSTALL_TRANSFORM_FAILURE;
248227a430bdSwinesync 
248327a430bdSwinesync     len = _snwprintf( NULL, 0, query_fmt, name ) + 1;
248427a430bdSwinesync     if (len > ARRAY_SIZE(buf))
248527a430bdSwinesync     {
2486*f4be6dc3SMikhail         query = malloc( len * sizeof(WCHAR) );
248727a430bdSwinesync         if (!query)
248827a430bdSwinesync             return ERROR_OUTOFMEMORY;
248927a430bdSwinesync     }
249027a430bdSwinesync     swprintf( query, len, query_fmt, name );
249127a430bdSwinesync 
249227a430bdSwinesync     r = MSI_DatabaseOpenViewW( tv->db, query, &q );
249327a430bdSwinesync     if (query != buf)
2494*f4be6dc3SMikhail         free( query );
249527a430bdSwinesync     if (r != ERROR_SUCCESS)
249627a430bdSwinesync         return r;
249727a430bdSwinesync 
249827a430bdSwinesync     r = MSI_ViewExecute( q, NULL );
249927a430bdSwinesync     msiobj_release( &q->hdr );
250027a430bdSwinesync     return r;
250127a430bdSwinesync }
250227a430bdSwinesync 
TransformView_add_column(struct table_view * tv,MSIRECORD * rec)2503*f4be6dc3SMikhail static UINT TransformView_add_column( struct table_view *tv, MSIRECORD *rec )
250428c50c1aSwinesync {
250528c50c1aSwinesync     static const WCHAR query_pfx[] =
25061c5160dcSwinesync         L"INSERT INTO `_TransformView` (`new`, `Table`, `Current`, `Column`, `Data`) VALUES (1, '";
250728c50c1aSwinesync 
250828c50c1aSwinesync     WCHAR buf[256], *query = buf;
250936bf5549Swinesync     UINT i, p, r, qlen;
251036bf5549Swinesync     DWORD len;
251128c50c1aSwinesync     MSIQUERY *q;
251228c50c1aSwinesync 
251328c50c1aSwinesync     qlen = p = wcslen( query_pfx );
251428c50c1aSwinesync     for (i = 1; i <= 4; i++)
251528c50c1aSwinesync     {
251628c50c1aSwinesync         r = MSI_RecordGetStringW( rec, i, NULL, &len );
251728c50c1aSwinesync         if (r != ERROR_SUCCESS)
251828c50c1aSwinesync             return r;
251928c50c1aSwinesync         qlen += len + 3; /* strlen( "','" ) */
252028c50c1aSwinesync     }
252128c50c1aSwinesync 
252228c50c1aSwinesync     if (qlen > ARRAY_SIZE(buf))
252328c50c1aSwinesync     {
2524*f4be6dc3SMikhail         query = malloc( len * sizeof(WCHAR) );
252528c50c1aSwinesync         qlen = len;
252628c50c1aSwinesync         if (!query)
252728c50c1aSwinesync             return ERROR_OUTOFMEMORY;
252828c50c1aSwinesync     }
252928c50c1aSwinesync 
253028c50c1aSwinesync     memcpy( query, query_pfx, p * sizeof(WCHAR) );
253128c50c1aSwinesync     for (i = 1; i <= 4; i++)
253228c50c1aSwinesync     {
253328c50c1aSwinesync         len = qlen - p;
253428c50c1aSwinesync         MSI_RecordGetStringW( rec, i, query + p, &len );
253528c50c1aSwinesync         p += len;
253628c50c1aSwinesync         query[p++] = '\'';
253728c50c1aSwinesync         if (i != 4)
253828c50c1aSwinesync         {
253928c50c1aSwinesync             query[p++] = ',';
254028c50c1aSwinesync             query[p++] = '\'';
254128c50c1aSwinesync         }
254228c50c1aSwinesync     }
254328c50c1aSwinesync     query[p++] = ')';
254428c50c1aSwinesync     query[p++] = 0;
254528c50c1aSwinesync 
254628c50c1aSwinesync     r = MSI_DatabaseOpenViewW( tv->db, query, &q );
254728c50c1aSwinesync     if (query != buf)
2548*f4be6dc3SMikhail         free( query );
254928c50c1aSwinesync     if (r != ERROR_SUCCESS)
255028c50c1aSwinesync         return r;
255128c50c1aSwinesync 
255228c50c1aSwinesync     r = MSI_ViewExecute( q, NULL );
255328c50c1aSwinesync     msiobj_release( &q->hdr );
255428c50c1aSwinesync     return r;
255528c50c1aSwinesync }
255628c50c1aSwinesync 
TransformView_insert_row(MSIVIEW * view,MSIRECORD * rec,UINT row,BOOL temporary)25571bbb87bbSwinesync static UINT TransformView_insert_row( MSIVIEW *view, MSIRECORD *rec, UINT row, BOOL temporary )
25581bbb87bbSwinesync {
2559e6af2a5aSwinesync     static const WCHAR query_fmt[] =
25601c5160dcSwinesync         L"INSERT INTO `_TransformView` (`new`, `Table`, `Column`, `Row`) VALUES (1, '%s', 'INSERT', '%s')";
2561e6af2a5aSwinesync 
2562*f4be6dc3SMikhail     struct table_view *tv = (struct table_view *)view;
2563e6af2a5aSwinesync     WCHAR buf[256], *query = buf;
2564e6af2a5aSwinesync     MSIQUERY *q;
2565e6af2a5aSwinesync     WCHAR *key;
2566e6af2a5aSwinesync     int len;
2567e6af2a5aSwinesync     UINT r;
256827a430bdSwinesync 
256974f53b4bSwinesync     if (!wcscmp(tv->name, L"_Tables"))
257027a430bdSwinesync         return TransformView_create_table( tv, rec );
257127a430bdSwinesync 
257274f53b4bSwinesync     if (!wcscmp(tv->name, L"_Columns"))
257328c50c1aSwinesync         return TransformView_add_column( tv, rec );
257428c50c1aSwinesync 
2575e6af2a5aSwinesync     key = create_key_string( tv, rec );
2576e6af2a5aSwinesync     if (!key)
2577e6af2a5aSwinesync         return ERROR_OUTOFMEMORY;
2578e6af2a5aSwinesync 
2579e6af2a5aSwinesync     len = _snwprintf( NULL, 0, query_fmt, tv->name, key ) + 1;
2580e6af2a5aSwinesync     if (len > ARRAY_SIZE(buf))
2581e6af2a5aSwinesync     {
2582*f4be6dc3SMikhail         query = malloc( len * sizeof(WCHAR) );
2583e6af2a5aSwinesync         if (!query)
2584e6af2a5aSwinesync         {
2585*f4be6dc3SMikhail             free( key );
2586e6af2a5aSwinesync             return ERROR_OUTOFMEMORY;
2587e6af2a5aSwinesync         }
2588e6af2a5aSwinesync     }
2589e6af2a5aSwinesync     swprintf( query, len, query_fmt, tv->name, key );
2590*f4be6dc3SMikhail     free( key );
2591e6af2a5aSwinesync 
2592e6af2a5aSwinesync     r = MSI_DatabaseOpenViewW( tv->db, query, &q );
2593e6af2a5aSwinesync     if (query != buf)
2594*f4be6dc3SMikhail         free( query );
2595e6af2a5aSwinesync     if (r != ERROR_SUCCESS)
2596e6af2a5aSwinesync         return r;
2597e6af2a5aSwinesync 
2598e6af2a5aSwinesync     r = MSI_ViewExecute( q, NULL );
2599e6af2a5aSwinesync     msiobj_release( &q->hdr );
2600e6af2a5aSwinesync     if (r != ERROR_SUCCESS)
2601e6af2a5aSwinesync         return r;
2602e6af2a5aSwinesync 
2603e6af2a5aSwinesync     return TransformView_set_row( view, row, rec, ~0 );
26041bbb87bbSwinesync }
26051bbb87bbSwinesync 
TransformView_drop_table(struct table_view * tv,UINT row)2606*f4be6dc3SMikhail static UINT TransformView_drop_table( struct table_view *tv, UINT row )
26079d793f29Swinesync {
26081c5160dcSwinesync     static const WCHAR query_pfx[] = L"INSERT INTO `_TransformView` ( `new`, `Table`, `Column` ) VALUES ( 1, '";
26099d793f29Swinesync     static const WCHAR query_sfx[] = L"', 'DROP' )";
26109d793f29Swinesync 
26119d793f29Swinesync     WCHAR buf[256], *query = buf;
26129d793f29Swinesync     UINT r, table_id, len;
26139d793f29Swinesync     const WCHAR *table;
26149d793f29Swinesync     int table_len;
26159d793f29Swinesync     MSIQUERY *q;
26169d793f29Swinesync 
26179d793f29Swinesync     r = TABLE_fetch_int( &tv->view, row, 1, &table_id );
26189d793f29Swinesync     if (r != ERROR_SUCCESS)
26199d793f29Swinesync         return r;
26209d793f29Swinesync 
26219d793f29Swinesync     table = msi_string_lookup( tv->db->strings, table_id, &table_len );
26229d793f29Swinesync     if (!table)
26239d793f29Swinesync         return ERROR_INSTALL_TRANSFORM_FAILURE;
26249d793f29Swinesync 
26259d793f29Swinesync     len = ARRAY_SIZE(query_pfx) - 1 + table_len + ARRAY_SIZE(query_sfx);
26269d793f29Swinesync     if (len > ARRAY_SIZE(buf))
26279d793f29Swinesync     {
2628*f4be6dc3SMikhail         query = malloc( len * sizeof(WCHAR) );
26299d793f29Swinesync         if (!query)
26309d793f29Swinesync             return ERROR_OUTOFMEMORY;
26319d793f29Swinesync     }
26329d793f29Swinesync 
26339d793f29Swinesync     memcpy( query, query_pfx, ARRAY_SIZE(query_pfx) * sizeof(WCHAR) );
26349d793f29Swinesync     len = ARRAY_SIZE(query_pfx) - 1;
26359d793f29Swinesync     memcpy( query + len, table, table_len * sizeof(WCHAR) );
26369d793f29Swinesync     len += table_len;
26379d793f29Swinesync     memcpy( query + len, query_sfx, ARRAY_SIZE(query_sfx) * sizeof(WCHAR) );
26389d793f29Swinesync 
26399d793f29Swinesync     r = MSI_DatabaseOpenViewW( tv->db, query, &q );
26409d793f29Swinesync     if (query != buf)
2641*f4be6dc3SMikhail         free( query );
26429d793f29Swinesync     if (r != ERROR_SUCCESS)
26439d793f29Swinesync         return r;
26449d793f29Swinesync 
26459d793f29Swinesync     r = MSI_ViewExecute( q, NULL );
26469d793f29Swinesync     msiobj_release( &q->hdr );
26479d793f29Swinesync     return r;
26489d793f29Swinesync }
26499d793f29Swinesync 
TransformView_delete_row(MSIVIEW * view,UINT row)26501bbb87bbSwinesync static UINT TransformView_delete_row( MSIVIEW *view, UINT row )
26511bbb87bbSwinesync {
26521c5160dcSwinesync     static const WCHAR query_pfx[] = L"INSERT INTO `_TransformView` ( `new`, `Table`, `Column`, `Row`) VALUES ( 1, '";
26534048c060Swinesync     static const WCHAR query_column[] = L"', 'DELETE', '";
26544048c060Swinesync     static const WCHAR query_sfx[] = L"')";
26554048c060Swinesync 
2656*f4be6dc3SMikhail     struct table_view *tv = (struct table_view *)view;
26574048c060Swinesync     WCHAR *key, buf[256], *query = buf;
26584048c060Swinesync     UINT r, len, name_len, key_len;
26594048c060Swinesync     MSIRECORD *rec;
26604048c060Swinesync     MSIQUERY *q;
26619d793f29Swinesync 
266274f53b4bSwinesync     if (!wcscmp( tv->name, L"_Columns" ))
26639d793f29Swinesync     {
26649d793f29Swinesync         ERR("trying to remove column\n");
26659d793f29Swinesync         return ERROR_INSTALL_TRANSFORM_FAILURE;
26669d793f29Swinesync     }
26679d793f29Swinesync 
266874f53b4bSwinesync     if (!wcscmp( tv->name, L"_Tables" ))
26699d793f29Swinesync         return TransformView_drop_table( tv, row );
26709d793f29Swinesync 
26714048c060Swinesync     r = msi_view_get_row( tv->db, view, row, &rec );
26724048c060Swinesync     if (r != ERROR_SUCCESS)
26734048c060Swinesync         return r;
26744048c060Swinesync 
26754048c060Swinesync     key = create_key_string( tv, rec );
26764048c060Swinesync     msiobj_release( &rec->hdr );
26774048c060Swinesync     if (!key)
26784048c060Swinesync         return ERROR_OUTOFMEMORY;
26794048c060Swinesync 
26804048c060Swinesync     name_len = wcslen( tv->name );
26814048c060Swinesync     key_len = wcslen( key );
26824048c060Swinesync     len = ARRAY_SIZE(query_pfx) + name_len + ARRAY_SIZE(query_column) + key_len + ARRAY_SIZE(query_sfx) - 2;
26834048c060Swinesync     if (len > ARRAY_SIZE(buf))
26844048c060Swinesync     {
2685*f4be6dc3SMikhail         query = malloc( len * sizeof(WCHAR) );
26864048c060Swinesync         if (!query)
26874048c060Swinesync         {
2688*f4be6dc3SMikhail             free( tv );
2689*f4be6dc3SMikhail             free( key );
26904048c060Swinesync             return ERROR_OUTOFMEMORY;
26914048c060Swinesync         }
26924048c060Swinesync     }
26934048c060Swinesync 
26944048c060Swinesync     memcpy( query, query_pfx, ARRAY_SIZE(query_pfx) * sizeof(WCHAR) );
26954048c060Swinesync     len = ARRAY_SIZE(query_pfx) - 1;
26964048c060Swinesync     memcpy( query + len, tv->name, name_len * sizeof(WCHAR) );
26974048c060Swinesync     len += name_len;
26984048c060Swinesync     memcpy( query + len, query_column, ARRAY_SIZE(query_column) * sizeof(WCHAR) );
26994048c060Swinesync     len += ARRAY_SIZE(query_column) - 1;
27004048c060Swinesync     memcpy( query + len, key, key_len * sizeof(WCHAR) );
27014048c060Swinesync     len += key_len;
27024048c060Swinesync     memcpy( query + len, query_sfx, ARRAY_SIZE(query_sfx) * sizeof(WCHAR) );
2703*f4be6dc3SMikhail     free( key );
27044048c060Swinesync 
27054048c060Swinesync     r = MSI_DatabaseOpenViewW( tv->db, query, &q );
27064048c060Swinesync     if (query != buf)
2707*f4be6dc3SMikhail         free( query );
27084048c060Swinesync     if (r != ERROR_SUCCESS)
27094048c060Swinesync         return r;
27104048c060Swinesync 
27114048c060Swinesync     r = MSI_ViewExecute( q, NULL );
27124048c060Swinesync     msiobj_release( &q->hdr );
27134048c060Swinesync     return r;
27141bbb87bbSwinesync }
27151bbb87bbSwinesync 
TransformView_execute(MSIVIEW * view,MSIRECORD * record)27161bbb87bbSwinesync static UINT TransformView_execute( MSIVIEW *view, MSIRECORD *record )
27171bbb87bbSwinesync {
27181bbb87bbSwinesync     return ERROR_SUCCESS;
27191bbb87bbSwinesync }
27201bbb87bbSwinesync 
TransformView_close(MSIVIEW * view)27211bbb87bbSwinesync static UINT TransformView_close( MSIVIEW *view )
27221bbb87bbSwinesync {
27231bbb87bbSwinesync     return ERROR_SUCCESS;
27241bbb87bbSwinesync }
27251bbb87bbSwinesync 
TransformView_get_dimensions(MSIVIEW * view,UINT * rows,UINT * cols)27261bbb87bbSwinesync static UINT TransformView_get_dimensions( MSIVIEW *view, UINT *rows, UINT *cols )
27271bbb87bbSwinesync {
2728317c1f6cSwinesync     return TABLE_get_dimensions( view, rows, cols );
27291bbb87bbSwinesync }
27301bbb87bbSwinesync 
TransformView_get_column_info(MSIVIEW * view,UINT n,LPCWSTR * name,UINT * type,BOOL * temporary,LPCWSTR * table_name)27311bbb87bbSwinesync static UINT TransformView_get_column_info( MSIVIEW *view, UINT n, LPCWSTR *name, UINT *type,
27321bbb87bbSwinesync                              BOOL *temporary, LPCWSTR *table_name )
27331bbb87bbSwinesync {
2734317c1f6cSwinesync     return TABLE_get_column_info( view, n, name, type, temporary, table_name );
27351bbb87bbSwinesync }
27361bbb87bbSwinesync 
TransformView_delete(MSIVIEW * view)27371bbb87bbSwinesync static UINT TransformView_delete( MSIVIEW *view )
27381bbb87bbSwinesync {
2739*f4be6dc3SMikhail     struct table_view *tv = (struct table_view *)view;
274028c50c1aSwinesync     if (!tv->table || tv->columns != tv->table->colinfo)
2741*f4be6dc3SMikhail         free( tv->columns );
27421bbb87bbSwinesync     return TABLE_delete( view );
27431bbb87bbSwinesync }
27441bbb87bbSwinesync 
27451bbb87bbSwinesync static const MSIVIEWOPS transform_view_ops =
27461bbb87bbSwinesync {
27471bbb87bbSwinesync     TransformView_fetch_int,
27481bbb87bbSwinesync     TransformView_fetch_stream,
27491bbb87bbSwinesync     NULL,
27501bbb87bbSwinesync     NULL,
27511bbb87bbSwinesync     NULL,
27521bbb87bbSwinesync     TransformView_set_row,
27531bbb87bbSwinesync     TransformView_insert_row,
27541bbb87bbSwinesync     TransformView_delete_row,
27551bbb87bbSwinesync     TransformView_execute,
27561bbb87bbSwinesync     TransformView_close,
27571bbb87bbSwinesync     TransformView_get_dimensions,
27581bbb87bbSwinesync     TransformView_get_column_info,
27591bbb87bbSwinesync     NULL,
27601bbb87bbSwinesync     TransformView_delete,
27611bbb87bbSwinesync     NULL,
27621bbb87bbSwinesync     NULL,
27631bbb87bbSwinesync     NULL,
27641bbb87bbSwinesync     NULL,
27651bbb87bbSwinesync     NULL
27661bbb87bbSwinesync };
27671bbb87bbSwinesync 
TransformView_Create(MSIDATABASE * db,string_table * st,LPCWSTR name,MSIVIEW ** view)2768*f4be6dc3SMikhail static UINT TransformView_Create( MSIDATABASE *db, string_table *st, LPCWSTR name, MSIVIEW **view )
27691bbb87bbSwinesync {
277028c50c1aSwinesync     static const WCHAR query_pfx[] = L"SELECT `Column`, `Data`, `Current` FROM `_TransformView` WHERE `Table`='";
27711c5160dcSwinesync     static const WCHAR query_sfx[] = L"' AND `Row` IS NULL AND `Current` IS NOT NULL AND `new` = 1";
277228c50c1aSwinesync 
277328c50c1aSwinesync     WCHAR buf[256], *query = buf;
277428c50c1aSwinesync     UINT r, len, name_len, size, add_col;
2775*f4be6dc3SMikhail     struct column_info *colinfo;
2776*f4be6dc3SMikhail     struct table_view *tv;
277728c50c1aSwinesync     MSIRECORD *rec;
277828c50c1aSwinesync     MSIQUERY *q;
277927a430bdSwinesync 
278027a430bdSwinesync     name_len = wcslen( name );
27811bbb87bbSwinesync 
2782*f4be6dc3SMikhail     r = TABLE_CreateView( db, name, (MSIVIEW **)&tv );
27831bbb87bbSwinesync     if (r == ERROR_INVALID_PARAMETER)
27841bbb87bbSwinesync     {
27851bbb87bbSwinesync         /* table does not exist */
2786*f4be6dc3SMikhail         size = FIELD_OFFSET( struct table_view, name[name_len + 1] );
2787*f4be6dc3SMikhail         tv = calloc( 1, size );
278827a430bdSwinesync         if (!tv)
278927a430bdSwinesync             return ERROR_OUTOFMEMORY;
279027a430bdSwinesync 
279127a430bdSwinesync         tv->db = db;
279227a430bdSwinesync         memcpy( tv->name, name, name_len * sizeof(WCHAR) );
27931bbb87bbSwinesync     }
27941bbb87bbSwinesync     else if (r != ERROR_SUCCESS)
27951bbb87bbSwinesync     {
27961bbb87bbSwinesync         return r;
27971bbb87bbSwinesync     }
27981bbb87bbSwinesync 
27991bbb87bbSwinesync     tv->view.ops = &transform_view_ops;
280028c50c1aSwinesync 
280128c50c1aSwinesync     len = ARRAY_SIZE(query_pfx) + name_len + ARRAY_SIZE(query_sfx) - 1;
280228c50c1aSwinesync     if (len > ARRAY_SIZE(buf))
280328c50c1aSwinesync     {
2804*f4be6dc3SMikhail         query = malloc( len * sizeof(WCHAR) );
280528c50c1aSwinesync         if (!query)
280628c50c1aSwinesync         {
2807*f4be6dc3SMikhail             free( tv );
280828c50c1aSwinesync             return ERROR_OUTOFMEMORY;
280928c50c1aSwinesync         }
281028c50c1aSwinesync     }
281128c50c1aSwinesync     memcpy( query, query_pfx, ARRAY_SIZE(query_pfx) * sizeof(WCHAR) );
281228c50c1aSwinesync     len = ARRAY_SIZE(query_pfx) - 1;
281328c50c1aSwinesync     memcpy( query + len, name, name_len * sizeof(WCHAR) );
281428c50c1aSwinesync     len += name_len;
281528c50c1aSwinesync     memcpy( query + len, query_sfx, ARRAY_SIZE(query_sfx) * sizeof(WCHAR) );
281628c50c1aSwinesync 
281728c50c1aSwinesync     r = MSI_DatabaseOpenViewW( tv->db, query, &q );
281828c50c1aSwinesync     if (query != buf)
2819*f4be6dc3SMikhail         free( query );
282028c50c1aSwinesync     if (r != ERROR_SUCCESS)
282128c50c1aSwinesync     {
2822*f4be6dc3SMikhail         free( tv );
282328c50c1aSwinesync         return r;
282428c50c1aSwinesync     }
282528c50c1aSwinesync 
282628c50c1aSwinesync     r = MSI_ViewExecute( q, NULL );
282728c50c1aSwinesync     if (r != ERROR_SUCCESS)
282828c50c1aSwinesync     {
2829*f4be6dc3SMikhail         free( tv );
283028c50c1aSwinesync         return r;
283128c50c1aSwinesync     }
283228c50c1aSwinesync 
283328c50c1aSwinesync     r = q->view->ops->get_dimensions( q->view, &add_col, NULL );
283428c50c1aSwinesync     if (r != ERROR_SUCCESS)
283528c50c1aSwinesync     {
283628c50c1aSwinesync         MSI_ViewClose( q );
283728c50c1aSwinesync         msiobj_release( &q->hdr );
2838*f4be6dc3SMikhail         free( tv );
283928c50c1aSwinesync         return r;
284028c50c1aSwinesync     }
284128c50c1aSwinesync     if (!add_col)
284228c50c1aSwinesync     {
284328c50c1aSwinesync         MSI_ViewClose( q );
284428c50c1aSwinesync         msiobj_release( &q->hdr );
2845*f4be6dc3SMikhail         *view = (MSIVIEW *)tv;
284628c50c1aSwinesync         return ERROR_SUCCESS;
284728c50c1aSwinesync     }
284828c50c1aSwinesync 
2849*f4be6dc3SMikhail     colinfo = calloc( add_col + tv->num_cols, sizeof(*colinfo) );
285028c50c1aSwinesync     if (!colinfo)
285128c50c1aSwinesync     {
285228c50c1aSwinesync         MSI_ViewClose( q );
285328c50c1aSwinesync         msiobj_release( &q->hdr );
2854*f4be6dc3SMikhail         free( tv );
2855*f4be6dc3SMikhail         return ERROR_OUTOFMEMORY;
285628c50c1aSwinesync     }
285728c50c1aSwinesync 
285828c50c1aSwinesync     while (MSI_ViewFetch( q, &rec ) == ERROR_SUCCESS)
285928c50c1aSwinesync     {
286028c50c1aSwinesync         int name_len;
286128c50c1aSwinesync         const WCHAR *name = msi_record_get_string( rec, 1, &name_len );
286228c50c1aSwinesync         const WCHAR *type = msi_record_get_string( rec, 2, NULL );
286328c50c1aSwinesync         UINT name_id, idx;
286428c50c1aSwinesync 
286528c50c1aSwinesync         idx = _wtoi( msi_record_get_string(rec, 3, NULL) );
286628c50c1aSwinesync         colinfo[idx - 1].number = idx;
286728c50c1aSwinesync         colinfo[idx - 1].type = _wtoi( type );
286828c50c1aSwinesync 
286928c50c1aSwinesync         r = msi_string2id( st, name, name_len, &name_id );
287028c50c1aSwinesync         if (r == ERROR_SUCCESS)
287128c50c1aSwinesync             colinfo[idx - 1].colname = msi_string_lookup( st, name_id, NULL );
287228c50c1aSwinesync         else
287328c50c1aSwinesync             ERR( "column name %s is not defined in strings table\n", wine_dbgstr_w(name) );
2874dac7d6f2Swinesync         msiobj_release( &rec->hdr );
287528c50c1aSwinesync     }
287628c50c1aSwinesync     MSI_ViewClose( q );
287728c50c1aSwinesync     msiobj_release( &q->hdr );
287828c50c1aSwinesync 
287928c50c1aSwinesync     memcpy( colinfo, tv->columns, tv->num_cols * sizeof(*colinfo) );
288028c50c1aSwinesync     tv->columns = colinfo;
288128c50c1aSwinesync     tv->num_cols += add_col;
2882*f4be6dc3SMikhail     *view = (MSIVIEW *)tv;
28831bbb87bbSwinesync     return ERROR_SUCCESS;
28841bbb87bbSwinesync }
28851bbb87bbSwinesync 
MSI_CommitTables(MSIDATABASE * db)2886c2c66affSColin Finck UINT MSI_CommitTables( MSIDATABASE *db )
2887c2c66affSColin Finck {
2888c2c66affSColin Finck     UINT r, bytes_per_strref;
2889c2c66affSColin Finck     HRESULT hr;
2890c2c66affSColin Finck     MSITABLE *table = NULL;
2891c2c66affSColin Finck 
2892c2c66affSColin Finck     TRACE("%p\n",db);
2893c2c66affSColin Finck 
2894c2c66affSColin Finck     r = msi_save_string_table( db->strings, db->storage, &bytes_per_strref );
2895c2c66affSColin Finck     if( r != ERROR_SUCCESS )
2896c2c66affSColin Finck     {
2897c2c66affSColin Finck         WARN("failed to save string table r=%08x\n",r);
2898c2c66affSColin Finck         return r;
2899c2c66affSColin Finck     }
2900c2c66affSColin Finck 
2901c2c66affSColin Finck     LIST_FOR_EACH_ENTRY( table, &db->tables, MSITABLE, entry )
2902c2c66affSColin Finck     {
2903c2c66affSColin Finck         r = save_table( db, table, bytes_per_strref );
2904c2c66affSColin Finck         if( r != ERROR_SUCCESS )
2905c2c66affSColin Finck         {
2906c2c66affSColin Finck             WARN("failed to save table %s (r=%08x)\n",
2907c2c66affSColin Finck                   debugstr_w(table->name), r);
2908c2c66affSColin Finck             return r;
2909c2c66affSColin Finck         }
2910c2c66affSColin Finck     }
2911c2c66affSColin Finck 
2912c2c66affSColin Finck     hr = IStorage_Commit( db->storage, 0 );
2913c2c66affSColin Finck     if (FAILED( hr ))
2914c2c66affSColin Finck     {
291502f995b2Swinesync         WARN( "failed to commit changes %#lx\n", hr );
2916c2c66affSColin Finck         r = ERROR_FUNCTION_FAILED;
2917c2c66affSColin Finck     }
2918c2c66affSColin Finck     return r;
2919c2c66affSColin Finck }
2920c2c66affSColin Finck 
MSI_DatabaseIsTablePersistent(MSIDATABASE * db,LPCWSTR table)2921c2c66affSColin Finck MSICONDITION MSI_DatabaseIsTablePersistent( MSIDATABASE *db, LPCWSTR table )
2922c2c66affSColin Finck {
2923c2c66affSColin Finck     MSITABLE *t;
2924c2c66affSColin Finck     UINT r;
2925c2c66affSColin Finck 
2926c2c66affSColin Finck     TRACE("%p %s\n", db, debugstr_w(table));
2927c2c66affSColin Finck 
2928c2c66affSColin Finck     if (!table)
2929c2c66affSColin Finck         return MSICONDITION_ERROR;
2930c2c66affSColin Finck 
2931c2c66affSColin Finck     r = get_table( db, table, &t );
2932c2c66affSColin Finck     if (r != ERROR_SUCCESS)
2933c2c66affSColin Finck         return MSICONDITION_NONE;
2934c2c66affSColin Finck 
2935c2c66affSColin Finck     return t->persistent;
2936c2c66affSColin Finck }
2937c2c66affSColin Finck 
read_raw_int(const BYTE * data,UINT col,UINT bytes)2938c2c66affSColin Finck static UINT read_raw_int(const BYTE *data, UINT col, UINT bytes)
2939c2c66affSColin Finck {
2940c2c66affSColin Finck     UINT ret = 0, i;
2941c2c66affSColin Finck 
2942c2c66affSColin Finck     for (i = 0; i < bytes; i++)
2943c2c66affSColin Finck         ret += (data[col + i] << i * 8);
2944c2c66affSColin Finck 
2945c2c66affSColin Finck     return ret;
2946c2c66affSColin Finck }
2947c2c66affSColin Finck 
record_encoded_stream_name(const struct table_view * tv,MSIRECORD * rec,WCHAR ** pstname)2948*f4be6dc3SMikhail static UINT record_encoded_stream_name( const struct table_view *tv, MSIRECORD *rec, WCHAR **pstname )
2949c2c66affSColin Finck {
295036bf5549Swinesync     UINT r;
295136bf5549Swinesync     DWORD len;
29528e75cb8aSwinesync     WCHAR *name;
2953c2c66affSColin Finck 
2954c2c66affSColin Finck     TRACE("%p %p\n", tv, rec);
2955c2c66affSColin Finck 
2956*f4be6dc3SMikhail     r = record_stream_name( tv, rec, NULL, &len );
29578e75cb8aSwinesync     if (r != ERROR_SUCCESS)
2958c2c66affSColin Finck         return r;
29598e75cb8aSwinesync     len++;
29608e75cb8aSwinesync 
2961*f4be6dc3SMikhail     name = malloc( len * sizeof(WCHAR) );
29628e75cb8aSwinesync     if (!name)
29638e75cb8aSwinesync         return ERROR_OUTOFMEMORY;
29648e75cb8aSwinesync 
2965*f4be6dc3SMikhail     r = record_stream_name( tv, rec, name, &len );
29668e75cb8aSwinesync     if (r != ERROR_SUCCESS)
29678e75cb8aSwinesync     {
2968*f4be6dc3SMikhail         free( name );
29698e75cb8aSwinesync         return r;
29708e75cb8aSwinesync     }
29718e75cb8aSwinesync 
29728e75cb8aSwinesync     *pstname = encode_streamname( FALSE, name );
2973*f4be6dc3SMikhail     free( name );
29748e75cb8aSwinesync     return ERROR_SUCCESS;
2975c2c66affSColin Finck }
2976c2c66affSColin Finck 
get_transform_record(const struct table_view * tv,const string_table * st,IStorage * stg,const BYTE * rawdata,UINT bytes_per_strref)2977*f4be6dc3SMikhail static MSIRECORD *get_transform_record( const struct table_view *tv, const string_table *st, IStorage *stg,
2978*f4be6dc3SMikhail                                         const BYTE *rawdata, UINT bytes_per_strref )
2979c2c66affSColin Finck {
2980c2c66affSColin Finck     UINT i, val, ofs = 0;
2981c2c66affSColin Finck     USHORT mask;
2982*f4be6dc3SMikhail     struct column_info *columns = tv->columns;
2983c2c66affSColin Finck     MSIRECORD *rec;
2984c2c66affSColin Finck 
2985c2c66affSColin Finck     mask = rawdata[0] | (rawdata[1] << 8);
2986c2c66affSColin Finck     rawdata += 2;
2987c2c66affSColin Finck 
2988c2c66affSColin Finck     rec = MSI_CreateRecord( tv->num_cols );
2989c2c66affSColin Finck     if( !rec )
2990c2c66affSColin Finck         return rec;
2991c2c66affSColin Finck 
2992c2c66affSColin Finck     TRACE("row ->\n");
2993c2c66affSColin Finck     for( i=0; i<tv->num_cols; i++ )
2994c2c66affSColin Finck     {
2995c2c66affSColin Finck         if ( (mask&1) && (i>=(mask>>8)) )
2996c2c66affSColin Finck             break;
2997c2c66affSColin Finck         /* all keys must be present */
2998c2c66affSColin Finck         if ( (~mask&1) && (~columns[i].type & MSITYPE_KEY) && ((1<<i) & ~mask) )
2999c2c66affSColin Finck             continue;
3000c2c66affSColin Finck 
3001c2c66affSColin Finck         if( MSITYPE_IS_BINARY(tv->columns[i].type) )
3002c2c66affSColin Finck         {
3003c2c66affSColin Finck             LPWSTR encname;
3004c2c66affSColin Finck             IStream *stm = NULL;
3005c2c66affSColin Finck             UINT r;
3006c2c66affSColin Finck 
3007c2c66affSColin Finck             ofs += bytes_per_column( tv->db, &columns[i], bytes_per_strref );
3008c2c66affSColin Finck 
3009*f4be6dc3SMikhail             r = record_encoded_stream_name( tv, rec, &encname );
3010c2c66affSColin Finck             if ( r != ERROR_SUCCESS )
3011c2c66affSColin Finck             {
3012c2c66affSColin Finck                 msiobj_release( &rec->hdr );
3013c2c66affSColin Finck                 return NULL;
3014c2c66affSColin Finck             }
3015c2c66affSColin Finck             r = IStorage_OpenStream( stg, encname, NULL, STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stm );
3016c2c66affSColin Finck             if ( r != ERROR_SUCCESS )
3017c2c66affSColin Finck             {
3018c2c66affSColin Finck                 msiobj_release( &rec->hdr );
3019*f4be6dc3SMikhail                 free( encname );
3020c2c66affSColin Finck                 return NULL;
3021c2c66affSColin Finck             }
3022c2c66affSColin Finck 
3023c2c66affSColin Finck             MSI_RecordSetStream( rec, i+1, stm );
3024c2c66affSColin Finck             TRACE(" field %d [%s]\n", i+1, debugstr_w(encname));
3025*f4be6dc3SMikhail             free( encname );
3026c2c66affSColin Finck         }
3027c2c66affSColin Finck         else if( columns[i].type & MSITYPE_STRING )
3028c2c66affSColin Finck         {
3029c2c66affSColin Finck             int len;
3030c2c66affSColin Finck             const WCHAR *sval;
3031c2c66affSColin Finck 
3032c2c66affSColin Finck             val = read_raw_int(rawdata, ofs, bytes_per_strref);
3033c2c66affSColin Finck             sval = msi_string_lookup( st, val, &len );
3034c2c66affSColin Finck             msi_record_set_string( rec, i+1, sval, len );
3035c2c66affSColin Finck             TRACE(" field %d [%s]\n", i+1, debugstr_wn(sval, len));
3036c2c66affSColin Finck             ofs += bytes_per_strref;
3037c2c66affSColin Finck         }
3038c2c66affSColin Finck         else
3039c2c66affSColin Finck         {
3040c2c66affSColin Finck             UINT n = bytes_per_column( tv->db, &columns[i], bytes_per_strref );
3041c2c66affSColin Finck             switch( n )
3042c2c66affSColin Finck             {
3043c2c66affSColin Finck             case 2:
3044c2c66affSColin Finck                 val = read_raw_int(rawdata, ofs, n);
3045c2c66affSColin Finck                 if (val)
3046c2c66affSColin Finck                     MSI_RecordSetInteger( rec, i+1, val-0x8000 );
3047c2c66affSColin Finck                 TRACE(" field %d [0x%04x]\n", i+1, val );
3048c2c66affSColin Finck                 break;
3049c2c66affSColin Finck             case 4:
3050c2c66affSColin Finck                 val = read_raw_int(rawdata, ofs, n);
3051c2c66affSColin Finck                 if (val)
3052c2c66affSColin Finck                     MSI_RecordSetInteger( rec, i+1, val^0x80000000 );
3053c2c66affSColin Finck                 TRACE(" field %d [0x%08x]\n", i+1, val );
3054c2c66affSColin Finck                 break;
3055c2c66affSColin Finck             default:
3056c2c66affSColin Finck                 ERR("oops - unknown column width %d\n", n);
3057c2c66affSColin Finck                 break;
3058c2c66affSColin Finck             }
3059c2c66affSColin Finck             ofs += n;
3060c2c66affSColin Finck         }
3061c2c66affSColin Finck     }
3062c2c66affSColin Finck     return rec;
3063c2c66affSColin Finck }
3064c2c66affSColin Finck 
dump_table(const string_table * st,const USHORT * rawdata,UINT rawsize)3065c2c66affSColin Finck static void dump_table( const string_table *st, const USHORT *rawdata, UINT rawsize )
3066c2c66affSColin Finck {
3067c2c66affSColin Finck     UINT i;
3068c2c66affSColin Finck     for (i = 0; i < rawsize / 2; i++)
3069c2c66affSColin Finck     {
3070c2c66affSColin Finck         int len;
3071c2c66affSColin Finck         const WCHAR *sval = msi_string_lookup( st, rawdata[i], &len );
3072c2c66affSColin Finck         MESSAGE(" %04x %s\n", rawdata[i], debugstr_wn(sval, len) );
3073c2c66affSColin Finck     }
3074c2c66affSColin Finck }
3075c2c66affSColin Finck 
record_to_row(const struct table_view * tv,MSIRECORD * rec)3076*f4be6dc3SMikhail static UINT *record_to_row( const struct table_view *tv, MSIRECORD *rec )
3077c2c66affSColin Finck {
3078c2c66affSColin Finck     UINT i, r, *data;
3079c2c66affSColin Finck 
3080*f4be6dc3SMikhail     data = malloc( tv->num_cols * sizeof (UINT) );
3081c2c66affSColin Finck     for( i=0; i<tv->num_cols; i++ )
3082c2c66affSColin Finck     {
3083c2c66affSColin Finck         data[i] = 0;
3084c2c66affSColin Finck 
3085c2c66affSColin Finck         if ( ~tv->columns[i].type & MSITYPE_KEY )
3086c2c66affSColin Finck             continue;
3087c2c66affSColin Finck 
3088c2c66affSColin Finck         /* turn the transform column value into a row value */
3089c2c66affSColin Finck         if ( ( tv->columns[i].type & MSITYPE_STRING ) &&
3090c2c66affSColin Finck              ! MSITYPE_IS_BINARY(tv->columns[i].type) )
3091c2c66affSColin Finck         {
3092c2c66affSColin Finck             int len;
3093c2c66affSColin Finck             const WCHAR *str = msi_record_get_string( rec, i+1, &len );
3094c2c66affSColin Finck             if (str)
3095c2c66affSColin Finck             {
3096c2c66affSColin Finck                 r = msi_string2id( tv->db->strings, str, len, &data[i] );
3097c2c66affSColin Finck 
3098c2c66affSColin Finck                 /* if there's no matching string in the string table,
3099c2c66affSColin Finck                    these keys can't match any record, so fail now. */
3100c2c66affSColin Finck                 if (r != ERROR_SUCCESS)
3101c2c66affSColin Finck                 {
3102*f4be6dc3SMikhail                     free( data );
3103c2c66affSColin Finck                     return NULL;
3104c2c66affSColin Finck                 }
3105c2c66affSColin Finck             }
3106c2c66affSColin Finck             else data[i] = 0;
3107c2c66affSColin Finck         }
3108c2c66affSColin Finck         else
3109c2c66affSColin Finck         {
31100e83dd03Swinesync             if (int_to_table_storage( tv, i + 1, MSI_RecordGetInteger( rec, i + 1 ), &data[i] ))
31110e83dd03Swinesync             {
3112*f4be6dc3SMikhail                 free( data );
31130e83dd03Swinesync                 return NULL;
31140e83dd03Swinesync             }
3115c2c66affSColin Finck         }
3116c2c66affSColin Finck     }
3117c2c66affSColin Finck     return data;
3118c2c66affSColin Finck }
3119c2c66affSColin Finck 
row_matches(struct table_view * tv,UINT row,const UINT * data,UINT * column)3120*f4be6dc3SMikhail static UINT row_matches( struct table_view *tv, UINT row, const UINT *data, UINT *column )
3121c2c66affSColin Finck {
3122c2c66affSColin Finck     UINT i, r, x, ret = ERROR_FUNCTION_FAILED;
3123c2c66affSColin Finck 
3124c2c66affSColin Finck     for( i=0; i<tv->num_cols; i++ )
3125c2c66affSColin Finck     {
3126c2c66affSColin Finck         if ( ~tv->columns[i].type & MSITYPE_KEY )
3127c2c66affSColin Finck             continue;
3128c2c66affSColin Finck 
3129c2c66affSColin Finck         /* turn the transform column value into a row value */
3130c2c66affSColin Finck         r = TABLE_fetch_int( &tv->view, row, i+1, &x );
3131c2c66affSColin Finck         if ( r != ERROR_SUCCESS )
3132c2c66affSColin Finck         {
3133c2c66affSColin Finck             ERR("TABLE_fetch_int shouldn't fail here\n");
3134c2c66affSColin Finck             break;
3135c2c66affSColin Finck         }
3136c2c66affSColin Finck 
3137c2c66affSColin Finck         /* if this key matches, move to the next column */
3138c2c66affSColin Finck         if ( x != data[i] )
3139c2c66affSColin Finck         {
3140c2c66affSColin Finck             ret = ERROR_FUNCTION_FAILED;
3141c2c66affSColin Finck             break;
3142c2c66affSColin Finck         }
3143c2c66affSColin Finck         if (column) *column = i;
3144c2c66affSColin Finck         ret = ERROR_SUCCESS;
3145c2c66affSColin Finck     }
3146c2c66affSColin Finck     return ret;
3147c2c66affSColin Finck }
3148c2c66affSColin Finck 
table_find_row(struct table_view * tv,MSIRECORD * rec,UINT * row,UINT * column)3149*f4be6dc3SMikhail static UINT table_find_row( struct table_view *tv, MSIRECORD *rec, UINT *row, UINT *column )
3150c2c66affSColin Finck {
3151c2c66affSColin Finck     UINT i, r = ERROR_FUNCTION_FAILED, *data;
3152c2c66affSColin Finck 
3153*f4be6dc3SMikhail     data = record_to_row( tv, rec );
3154c2c66affSColin Finck     if( !data )
3155c2c66affSColin Finck         return r;
3156c2c66affSColin Finck     for( i = 0; i < tv->table->row_count; i++ )
3157c2c66affSColin Finck     {
3158*f4be6dc3SMikhail         r = row_matches( tv, i, data, column );
3159c2c66affSColin Finck         if( r == ERROR_SUCCESS )
3160c2c66affSColin Finck         {
3161c2c66affSColin Finck             *row = i;
3162c2c66affSColin Finck             break;
3163c2c66affSColin Finck         }
3164c2c66affSColin Finck     }
3165*f4be6dc3SMikhail     free( data );
3166c2c66affSColin Finck     return r;
3167c2c66affSColin Finck }
3168c2c66affSColin Finck 
3169*f4be6dc3SMikhail struct transform_data
3170c2c66affSColin Finck {
3171c2c66affSColin Finck     struct list entry;
3172c2c66affSColin Finck     LPWSTR name;
3173*f4be6dc3SMikhail };
3174c2c66affSColin Finck 
table_load_transform(MSIDATABASE * db,IStorage * stg,string_table * st,struct transform_data * transform,UINT bytes_per_strref,int err_cond)3175*f4be6dc3SMikhail static UINT table_load_transform( MSIDATABASE *db, IStorage *stg, string_table *st, struct transform_data *transform,
31761bbb87bbSwinesync                                   UINT bytes_per_strref, int err_cond )
3177c2c66affSColin Finck {
3178c2c66affSColin Finck     BYTE *rawdata = NULL;
3179*f4be6dc3SMikhail     struct table_view *tv = NULL;
3180c2c66affSColin Finck     UINT r, n, sz, i, mask, num_cols, colcol = 0, rawsize = 0;
3181c2c66affSColin Finck     MSIRECORD *rec = NULL;
3182c2c66affSColin Finck     WCHAR coltable[32];
3183c2c66affSColin Finck     const WCHAR *name;
3184c2c66affSColin Finck 
3185c2c66affSColin Finck     if (!transform)
3186c2c66affSColin Finck         return ERROR_SUCCESS;
3187c2c66affSColin Finck 
3188c2c66affSColin Finck     name = transform->name;
3189c2c66affSColin Finck 
3190c2c66affSColin Finck     coltable[0] = 0;
3191c2c66affSColin Finck     TRACE("%p %p %p %s\n", db, stg, st, debugstr_w(name) );
3192c2c66affSColin Finck 
3193c2c66affSColin Finck     /* read the transform data */
3194c2c66affSColin Finck     read_stream_data( stg, name, TRUE, &rawdata, &rawsize );
3195c2c66affSColin Finck     if ( !rawdata )
3196c2c66affSColin Finck     {
3197c2c66affSColin Finck         TRACE("table %s empty\n", debugstr_w(name) );
3198c2c66affSColin Finck         return ERROR_INVALID_TABLE;
3199c2c66affSColin Finck     }
3200c2c66affSColin Finck 
3201c2c66affSColin Finck     /* create a table view */
32021bbb87bbSwinesync     if ( err_cond & MSITRANSFORM_ERROR_VIEWTRANSFORM )
32031bbb87bbSwinesync         r = TransformView_Create( db, st, name, (MSIVIEW**) &tv );
32041bbb87bbSwinesync     else
3205c2c66affSColin Finck         r = TABLE_CreateView( db, name, (MSIVIEW**) &tv );
3206c2c66affSColin Finck     if( r != ERROR_SUCCESS )
3207c2c66affSColin Finck         goto err;
3208c2c66affSColin Finck 
3209c2c66affSColin Finck     r = tv->view.ops->execute( &tv->view, NULL );
3210c2c66affSColin Finck     if( r != ERROR_SUCCESS )
3211c2c66affSColin Finck         goto err;
3212c2c66affSColin Finck 
3213c2c66affSColin Finck     TRACE("name = %s columns = %u row_size = %u raw size = %u\n",
3214c2c66affSColin Finck           debugstr_w(name), tv->num_cols, tv->row_size, rawsize );
3215c2c66affSColin Finck 
3216c2c66affSColin Finck     /* interpret the data */
3217c2c66affSColin Finck     for (n = 0; n < rawsize;)
3218c2c66affSColin Finck     {
3219c2c66affSColin Finck         mask = rawdata[n] | (rawdata[n + 1] << 8);
3220c2c66affSColin Finck         if (mask & 1)
3221c2c66affSColin Finck         {
3222c2c66affSColin Finck             /*
3223c2c66affSColin Finck              * if the low bit is set, columns are continuous and
3224c2c66affSColin Finck              * the number of columns is specified in the high byte
3225c2c66affSColin Finck              */
3226c2c66affSColin Finck             sz = 2;
3227c2c66affSColin Finck             num_cols = mask >> 8;
3228c2c66affSColin Finck             if (num_cols > tv->num_cols)
3229c2c66affSColin Finck             {
3230c2c66affSColin Finck                 ERR("excess columns in transform: %u > %u\n", num_cols, tv->num_cols);
3231c2c66affSColin Finck                 break;
3232c2c66affSColin Finck             }
3233c2c66affSColin Finck 
3234c2c66affSColin Finck             for (i = 0; i < num_cols; i++)
3235c2c66affSColin Finck             {
3236c2c66affSColin Finck                 if( (tv->columns[i].type & MSITYPE_STRING) &&
3237c2c66affSColin Finck                     ! MSITYPE_IS_BINARY(tv->columns[i].type) )
3238c2c66affSColin Finck                     sz += bytes_per_strref;
3239c2c66affSColin Finck                 else
3240c2c66affSColin Finck                     sz += bytes_per_column( tv->db, &tv->columns[i], bytes_per_strref );
3241c2c66affSColin Finck             }
3242c2c66affSColin Finck         }
3243c2c66affSColin Finck         else
3244c2c66affSColin Finck         {
3245c2c66affSColin Finck             /*
3246c2c66affSColin Finck              * If the low bit is not set, mask is a bitmask.
3247c2c66affSColin Finck              * Excepting for key fields, which are always present,
3248c2c66affSColin Finck              *  each bit indicates that a field is present in the transform record.
3249c2c66affSColin Finck              *
3250c2c66affSColin Finck              * mask == 0 is a special case ... only the keys will be present
3251c2c66affSColin Finck              * and it means that this row should be deleted.
3252c2c66affSColin Finck              */
3253c2c66affSColin Finck             sz = 2;
3254c2c66affSColin Finck             num_cols = tv->num_cols;
3255c2c66affSColin Finck             for (i = 0; i < num_cols; i++)
3256c2c66affSColin Finck             {
3257c2c66affSColin Finck                 if ((tv->columns[i].type & MSITYPE_KEY) || ((1 << i) & mask))
3258c2c66affSColin Finck                 {
3259c2c66affSColin Finck                     if ((tv->columns[i].type & MSITYPE_STRING) &&
3260c2c66affSColin Finck                         !MSITYPE_IS_BINARY(tv->columns[i].type))
3261c2c66affSColin Finck                         sz += bytes_per_strref;
3262c2c66affSColin Finck                     else
3263c2c66affSColin Finck                         sz += bytes_per_column( tv->db, &tv->columns[i], bytes_per_strref );
3264c2c66affSColin Finck                 }
3265c2c66affSColin Finck             }
3266c2c66affSColin Finck         }
3267c2c66affSColin Finck 
3268c2c66affSColin Finck         /* check we didn't run of the end of the table */
3269c2c66affSColin Finck         if (n + sz > rawsize)
3270c2c66affSColin Finck         {
3271c2c66affSColin Finck             ERR("borked.\n");
3272c2c66affSColin Finck             dump_table( st, (USHORT *)rawdata, rawsize );
3273c2c66affSColin Finck             break;
3274c2c66affSColin Finck         }
3275c2c66affSColin Finck 
3276*f4be6dc3SMikhail         rec = get_transform_record( tv, st, stg, &rawdata[n], bytes_per_strref );
3277c2c66affSColin Finck         if (rec)
3278c2c66affSColin Finck         {
3279c2c66affSColin Finck             WCHAR table[32];
3280c2c66affSColin Finck             DWORD sz = 32;
3281c2c66affSColin Finck             UINT number = MSI_NULL_INTEGER;
3282c2c66affSColin Finck             UINT row = 0;
3283c2c66affSColin Finck 
328474f53b4bSwinesync             if (!wcscmp( name, L"_Columns" ))
3285c2c66affSColin Finck             {
3286c2c66affSColin Finck                 MSI_RecordGetStringW( rec, 1, table, &sz );
3287c2c66affSColin Finck                 number = MSI_RecordGetInteger( rec, 2 );
3288c2c66affSColin Finck 
3289c2c66affSColin Finck                 /*
3290c2c66affSColin Finck                  * Native msi seems writes nul into the Number (2nd) column of
3291c2c66affSColin Finck                  * the _Columns table when there are new columns
3292c2c66affSColin Finck                  */
3293c2c66affSColin Finck                 if ( number == MSI_NULL_INTEGER )
3294c2c66affSColin Finck                 {
3295c2c66affSColin Finck                     /* reset the column number on a new table */
3296958f1addSwinesync                     if (wcscmp( coltable, table ))
3297c2c66affSColin Finck                     {
3298c2c66affSColin Finck                         colcol = 0;
3299c2c66affSColin Finck                         lstrcpyW( coltable, table );
3300c2c66affSColin Finck                     }
3301c2c66affSColin Finck 
3302c2c66affSColin Finck                     /* fix nul column numbers */
3303c2c66affSColin Finck                     MSI_RecordSetInteger( rec, 2, ++colcol );
3304c2c66affSColin Finck                 }
3305c2c66affSColin Finck             }
3306c2c66affSColin Finck 
3307c2c66affSColin Finck             if (TRACE_ON(msidb)) dump_record( rec );
3308c2c66affSColin Finck 
330927a430bdSwinesync             if (tv->table)
3310*f4be6dc3SMikhail                 r = table_find_row( tv, rec, &row, NULL );
331127a430bdSwinesync             else
331227a430bdSwinesync                 r = ERROR_FUNCTION_FAILED;
3313c2c66affSColin Finck             if (r == ERROR_SUCCESS)
3314c2c66affSColin Finck             {
3315c2c66affSColin Finck                 if (!mask)
3316c2c66affSColin Finck                 {
3317c2c66affSColin Finck                     TRACE("deleting row [%d]:\n", row);
33181bbb87bbSwinesync                     r = tv->view.ops->delete_row( &tv->view, row );
3319c2c66affSColin Finck                     if (r != ERROR_SUCCESS)
3320c2c66affSColin Finck                         WARN("failed to delete row %u\n", r);
3321c2c66affSColin Finck                 }
3322c2c66affSColin Finck                 else if (mask & 1)
3323c2c66affSColin Finck                 {
3324c2c66affSColin Finck                     TRACE("modifying full row [%d]:\n", row);
33251bbb87bbSwinesync                     r = tv->view.ops->set_row( &tv->view, row, rec, (1 << tv->num_cols) - 1 );
3326c2c66affSColin Finck                     if (r != ERROR_SUCCESS)
3327c2c66affSColin Finck                         WARN("failed to modify row %u\n", r);
3328c2c66affSColin Finck                 }
3329c2c66affSColin Finck                 else
3330c2c66affSColin Finck                 {
3331c2c66affSColin Finck                     TRACE("modifying masked row [%d]:\n", row);
33321bbb87bbSwinesync                     r = tv->view.ops->set_row( &tv->view, row, rec, mask );
3333c2c66affSColin Finck                     if (r != ERROR_SUCCESS)
3334c2c66affSColin Finck                         WARN("failed to modify row %u\n", r);
3335c2c66affSColin Finck                 }
3336c2c66affSColin Finck             }
3337c2c66affSColin Finck             else
3338c2c66affSColin Finck             {
3339c2c66affSColin Finck                 TRACE("inserting row\n");
33401bbb87bbSwinesync                 r = tv->view.ops->insert_row( &tv->view, rec, -1, FALSE );
3341c2c66affSColin Finck                 if (r != ERROR_SUCCESS)
3342c2c66affSColin Finck                     WARN("failed to insert row %u\n", r);
3343c2c66affSColin Finck             }
3344c2c66affSColin Finck 
3345*f4be6dc3SMikhail             if (!(err_cond & MSITRANSFORM_ERROR_VIEWTRANSFORM) && !wcscmp( name, L"_Columns" ))
3346*f4be6dc3SMikhail                 update_table_columns( db, table );
3347c2c66affSColin Finck 
3348c2c66affSColin Finck             msiobj_release( &rec->hdr );
3349c2c66affSColin Finck         }
3350c2c66affSColin Finck 
3351c2c66affSColin Finck         n += sz;
3352c2c66affSColin Finck     }
3353c2c66affSColin Finck 
3354c2c66affSColin Finck err:
3355c2c66affSColin Finck     /* no need to free the table, it's associated with the database */
3356*f4be6dc3SMikhail     free( rawdata );
3357c2c66affSColin Finck     if( tv )
3358c2c66affSColin Finck         tv->view.ops->delete( &tv->view );
3359c2c66affSColin Finck 
3360c2c66affSColin Finck     return ERROR_SUCCESS;
3361c2c66affSColin Finck }
3362c2c66affSColin Finck 
3363c2c66affSColin Finck /*
3364c2c66affSColin Finck  * msi_table_apply_transform
3365c2c66affSColin Finck  *
3366c2c66affSColin Finck  * Enumerate the table transforms in a transform storage and apply each one.
3367c2c66affSColin Finck  */
msi_table_apply_transform(MSIDATABASE * db,IStorage * stg,int err_cond)33681bbb87bbSwinesync UINT msi_table_apply_transform( MSIDATABASE *db, IStorage *stg, int err_cond )
3369c2c66affSColin Finck {
3370c2c66affSColin Finck     struct list transforms;
3371c2c66affSColin Finck     IEnumSTATSTG *stgenum = NULL;
3372*f4be6dc3SMikhail     struct transform_data *transform;
3373*f4be6dc3SMikhail     struct transform_data *tables = NULL, *columns = NULL;
3374c2c66affSColin Finck     HRESULT hr;
3375c2c66affSColin Finck     STATSTG stat;
3376c2c66affSColin Finck     string_table *strings;
3377c2c66affSColin Finck     UINT ret = ERROR_FUNCTION_FAILED;
3378c2c66affSColin Finck     UINT bytes_per_strref;
3379c2c66affSColin Finck     BOOL property_update = FALSE;
33801c5160dcSwinesync     MSIVIEW *transform_view = NULL;
3381c2c66affSColin Finck 
3382c2c66affSColin Finck     TRACE("%p %p\n", db, stg );
3383c2c66affSColin Finck 
3384c2c66affSColin Finck     strings = msi_load_string_table( stg, &bytes_per_strref );
3385c2c66affSColin Finck     if( !strings )
3386c2c66affSColin Finck         goto end;
3387c2c66affSColin Finck 
3388c2c66affSColin Finck     hr = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
3389c2c66affSColin Finck     if (FAILED( hr ))
3390c2c66affSColin Finck         goto end;
3391c2c66affSColin Finck 
3392c2c66affSColin Finck     list_init(&transforms);
3393c2c66affSColin Finck 
3394c2c66affSColin Finck     while ( TRUE )
3395c2c66affSColin Finck     {
3396*f4be6dc3SMikhail         struct table_view *tv = NULL;
3397c2c66affSColin Finck         WCHAR name[0x40];
3398c2c66affSColin Finck         ULONG count = 0;
3399c2c66affSColin Finck 
3400c2c66affSColin Finck         hr = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
3401c2c66affSColin Finck         if (FAILED( hr ) || !count)
3402c2c66affSColin Finck             break;
3403c2c66affSColin Finck 
3404c2c66affSColin Finck         decode_streamname( stat.pwcsName, name );
3405c2c66affSColin Finck         CoTaskMemFree( stat.pwcsName );
3406c2c66affSColin Finck         if ( name[0] != 0x4840 )
3407c2c66affSColin Finck             continue;
3408c2c66affSColin Finck 
340974f53b4bSwinesync         if ( !wcscmp( name+1, L"_StringPool" ) ||
341074f53b4bSwinesync              !wcscmp( name+1, L"_StringData" ) )
3411c2c66affSColin Finck             continue;
3412c2c66affSColin Finck 
3413*f4be6dc3SMikhail         transform = calloc( 1, sizeof(*transform) );
3414c2c66affSColin Finck         if ( !transform )
3415c2c66affSColin Finck             break;
3416c2c66affSColin Finck 
3417c2c66affSColin Finck         list_add_tail( &transforms, &transform->entry );
3418c2c66affSColin Finck 
3419*f4be6dc3SMikhail         transform->name = wcsdup( name + 1 );
3420c2c66affSColin Finck 
342174f53b4bSwinesync         if ( !wcscmp( transform->name, L"_Tables" ) )
3422c2c66affSColin Finck             tables = transform;
342374f53b4bSwinesync         else if (!wcscmp( transform->name, L"_Columns" ) )
3424c2c66affSColin Finck             columns = transform;
342574f53b4bSwinesync         else if (!wcscmp( transform->name, L"Property" ))
3426c2c66affSColin Finck             property_update = TRUE;
3427c2c66affSColin Finck 
3428c2c66affSColin Finck         TRACE("transform contains stream %s\n", debugstr_w(name));
3429c2c66affSColin Finck 
3430c2c66affSColin Finck         /* load the table */
3431c2c66affSColin Finck         if (TABLE_CreateView( db, transform->name, (MSIVIEW**) &tv ) != ERROR_SUCCESS)
3432c2c66affSColin Finck             continue;
3433c2c66affSColin Finck 
3434c2c66affSColin Finck         if (tv->view.ops->execute( &tv->view, NULL ) != ERROR_SUCCESS)
3435c2c66affSColin Finck         {
3436c2c66affSColin Finck             tv->view.ops->delete( &tv->view );
3437c2c66affSColin Finck             continue;
3438c2c66affSColin Finck         }
3439c2c66affSColin Finck 
3440c2c66affSColin Finck         tv->view.ops->delete( &tv->view );
3441c2c66affSColin Finck     }
3442c2c66affSColin Finck 
34431bbb87bbSwinesync     if (err_cond & MSITRANSFORM_ERROR_VIEWTRANSFORM)
34441bbb87bbSwinesync     {
34451bbb87bbSwinesync         static const WCHAR create_query[] = L"CREATE TABLE `_TransformView` ( "
34461bbb87bbSwinesync             L"`Table` CHAR(0) NOT NULL TEMPORARY, `Column` CHAR(0) NOT NULL TEMPORARY, "
34471bbb87bbSwinesync             L"`Row` CHAR(0) TEMPORARY, `Data` CHAR(0) TEMPORARY, `Current` CHAR(0) TEMPORARY "
34481bbb87bbSwinesync             L"PRIMARY KEY `Table`, `Column`, `Row` ) HOLD";
344974f53b4bSwinesync 
34501bbb87bbSwinesync         MSIQUERY *query;
34511bbb87bbSwinesync         UINT r;
34521bbb87bbSwinesync 
34531bbb87bbSwinesync         r = MSI_DatabaseOpenViewW( db, create_query, &query );
34541bbb87bbSwinesync         if (r != ERROR_SUCCESS)
34551bbb87bbSwinesync             goto end;
34561bbb87bbSwinesync 
34571bbb87bbSwinesync         r = MSI_ViewExecute( query, NULL );
34581bbb87bbSwinesync         if (r == ERROR_SUCCESS)
34591bbb87bbSwinesync             MSI_ViewClose( query );
34601bbb87bbSwinesync         msiobj_release( &query->hdr );
34611c5160dcSwinesync         if (r != ERROR_BAD_QUERY_SYNTAX && r != ERROR_SUCCESS)
34621c5160dcSwinesync             goto end;
34631c5160dcSwinesync 
34641c5160dcSwinesync         if (TABLE_CreateView(db, L"_TransformView", &transform_view) != ERROR_SUCCESS)
34651c5160dcSwinesync             goto end;
34661c5160dcSwinesync 
34671bbb87bbSwinesync         if (r == ERROR_BAD_QUERY_SYNTAX)
34681c5160dcSwinesync             transform_view->ops->add_ref( transform_view );
34691c5160dcSwinesync 
34701c5160dcSwinesync         r = transform_view->ops->add_column( transform_view, L"new",
34711c5160dcSwinesync                 MSITYPE_TEMPORARY | MSITYPE_NULLABLE | 0x402 /* INT */, FALSE );
34721bbb87bbSwinesync         if (r != ERROR_SUCCESS)
34731bbb87bbSwinesync             goto end;
34741bbb87bbSwinesync     }
34751bbb87bbSwinesync 
3476c2c66affSColin Finck     /*
3477c2c66affSColin Finck      * Apply _Tables and _Columns transforms first so that
3478c2c66affSColin Finck      * the table metadata is correct, and empty tables exist.
3479c2c66affSColin Finck      */
3480*f4be6dc3SMikhail     ret = table_load_transform( db, stg, strings, tables, bytes_per_strref, err_cond );
3481c2c66affSColin Finck     if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
3482c2c66affSColin Finck         goto end;
3483c2c66affSColin Finck 
3484*f4be6dc3SMikhail     ret = table_load_transform( db, stg, strings, columns, bytes_per_strref, err_cond );
3485c2c66affSColin Finck     if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
3486c2c66affSColin Finck         goto end;
3487c2c66affSColin Finck 
3488c2c66affSColin Finck     ret = ERROR_SUCCESS;
3489c2c66affSColin Finck 
3490c2c66affSColin Finck     while ( !list_empty( &transforms ) )
3491c2c66affSColin Finck     {
3492*f4be6dc3SMikhail         transform = LIST_ENTRY( list_head( &transforms ), struct transform_data, entry );
3493c2c66affSColin Finck 
349474f53b4bSwinesync         if ( wcscmp( transform->name, L"_Columns" ) &&
349574f53b4bSwinesync              wcscmp( transform->name, L"_Tables" ) &&
3496c2c66affSColin Finck              ret == ERROR_SUCCESS )
3497c2c66affSColin Finck         {
3498*f4be6dc3SMikhail             ret = table_load_transform( db, stg, strings, transform, bytes_per_strref, err_cond );
3499c2c66affSColin Finck         }
3500c2c66affSColin Finck 
3501c2c66affSColin Finck         list_remove( &transform->entry );
3502*f4be6dc3SMikhail         free( transform->name );
3503*f4be6dc3SMikhail         free( transform );
3504c2c66affSColin Finck     }
3505c2c66affSColin Finck 
3506c2c66affSColin Finck     if ( ret == ERROR_SUCCESS )
3507c2c66affSColin Finck     {
3508c2c66affSColin Finck         append_storage_to_db( db, stg );
3509c2c66affSColin Finck         if (property_update) msi_clone_properties( db );
3510c2c66affSColin Finck     }
3511c2c66affSColin Finck 
3512c2c66affSColin Finck end:
3513c2c66affSColin Finck     if ( stgenum )
3514c2c66affSColin Finck         IEnumSTATSTG_Release( stgenum );
3515c2c66affSColin Finck     if ( strings )
3516c2c66affSColin Finck         msi_destroy_stringtable( strings );
35171c5160dcSwinesync     if (transform_view)
35181bbb87bbSwinesync     {
3519*f4be6dc3SMikhail         struct tagMSITABLE *table = ((struct table_view *)transform_view)->table;
35201c5160dcSwinesync 
35211c5160dcSwinesync         if (ret != ERROR_SUCCESS)
35221c5160dcSwinesync             transform_view->ops->release( transform_view );
35231c5160dcSwinesync 
35241c5160dcSwinesync         if (!wcscmp(table->colinfo[table->col_count - 1].colname, L"new"))
35251c5160dcSwinesync             TABLE_remove_column( transform_view, table->colinfo[table->col_count - 1].number );
35261c5160dcSwinesync         transform_view->ops->delete( transform_view );
35271bbb87bbSwinesync     }
3528c2c66affSColin Finck 
3529c2c66affSColin Finck     return ret;
3530c2c66affSColin Finck }
3531