xref: /reactos/dll/win32/msi/table.c (revision 27a430bd)
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 
43c2c66affSColin Finck typedef struct tagMSICOLUMNHASHENTRY
44c2c66affSColin Finck {
45c2c66affSColin Finck     struct tagMSICOLUMNHASHENTRY *next;
46c2c66affSColin Finck     UINT value;
47c2c66affSColin Finck     UINT row;
48c2c66affSColin Finck } MSICOLUMNHASHENTRY;
49c2c66affSColin Finck 
50c2c66affSColin Finck typedef struct tagMSICOLUMNINFO
51c2c66affSColin Finck {
52c2c66affSColin Finck     LPCWSTR tablename;
53c2c66affSColin Finck     UINT    number;
54c2c66affSColin Finck     LPCWSTR colname;
55c2c66affSColin Finck     UINT    type;
56c2c66affSColin Finck     UINT    offset;
57c2c66affSColin Finck     INT     ref_count;
58c2c66affSColin Finck     BOOL    temporary;
59c2c66affSColin Finck     MSICOLUMNHASHENTRY **hash_table;
60c2c66affSColin Finck } MSICOLUMNINFO;
61c2c66affSColin Finck 
62c2c66affSColin Finck struct tagMSITABLE
63c2c66affSColin Finck {
64c2c66affSColin Finck     BYTE **data;
65c2c66affSColin Finck     BOOL *data_persistent;
66c2c66affSColin Finck     UINT row_count;
67c2c66affSColin Finck     struct list entry;
68c2c66affSColin Finck     MSICOLUMNINFO *colinfo;
69c2c66affSColin Finck     UINT col_count;
70c2c66affSColin Finck     MSICONDITION persistent;
71c2c66affSColin Finck     INT ref_count;
72c2c66affSColin Finck     WCHAR name[1];
73c2c66affSColin Finck };
74c2c66affSColin Finck 
75c2c66affSColin Finck /* information for default tables */
76c2c66affSColin Finck static const WCHAR szTables[]  = {'_','T','a','b','l','e','s',0};
77c2c66affSColin Finck static const WCHAR szTable[]   = {'T','a','b','l','e',0};
78c2c66affSColin Finck static const WCHAR szColumns[] = {'_','C','o','l','u','m','n','s',0};
79c2c66affSColin Finck static const WCHAR szNumber[]  = {'N','u','m','b','e','r',0};
80c2c66affSColin Finck static const WCHAR szType[]    = {'T','y','p','e',0};
81c2c66affSColin Finck 
82c2c66affSColin Finck static const MSICOLUMNINFO _Columns_cols[4] = {
83c2c66affSColin Finck     { szColumns, 1, szTable,  MSITYPE_VALID | MSITYPE_STRING | MSITYPE_KEY | 64, 0, 0, 0, NULL },
84c2c66affSColin Finck     { szColumns, 2, szNumber, MSITYPE_VALID | MSITYPE_KEY | 2,     2, 0, 0, NULL },
85c2c66affSColin Finck     { szColumns, 3, szName,   MSITYPE_VALID | MSITYPE_STRING | 64, 4, 0, 0, NULL },
86c2c66affSColin Finck     { szColumns, 4, szType,   MSITYPE_VALID | 2,                   6, 0, 0, NULL },
87c2c66affSColin Finck };
88c2c66affSColin Finck 
89c2c66affSColin Finck static const MSICOLUMNINFO _Tables_cols[1] = {
90c2c66affSColin Finck     { szTables,  1, szName,   MSITYPE_VALID | MSITYPE_STRING | MSITYPE_KEY | 64, 0, 0, 0, NULL },
91c2c66affSColin Finck };
92c2c66affSColin Finck 
93c2c66affSColin Finck #define MAX_STREAM_NAME 0x1f
94c2c66affSColin Finck 
95c2c66affSColin Finck static inline UINT bytes_per_column( MSIDATABASE *db, const MSICOLUMNINFO *col, UINT bytes_per_strref )
96c2c66affSColin Finck {
97c2c66affSColin Finck     if( MSITYPE_IS_BINARY(col->type) )
98c2c66affSColin Finck         return 2;
99c2c66affSColin Finck 
100c2c66affSColin Finck     if( col->type & MSITYPE_STRING )
101c2c66affSColin Finck         return bytes_per_strref;
102c2c66affSColin Finck 
103c2c66affSColin Finck     if( (col->type & 0xff) <= 2)
104c2c66affSColin Finck         return 2;
105c2c66affSColin Finck 
106c2c66affSColin Finck     if( (col->type & 0xff) != 4 )
107c2c66affSColin Finck         ERR("Invalid column size %u\n", col->type & 0xff);
108c2c66affSColin Finck 
109c2c66affSColin Finck     return 4;
110c2c66affSColin Finck }
111c2c66affSColin Finck 
112c2c66affSColin Finck static int utf2mime(int x)
113c2c66affSColin Finck {
114c2c66affSColin Finck     if( (x>='0') && (x<='9') )
115c2c66affSColin Finck         return x-'0';
116c2c66affSColin Finck     if( (x>='A') && (x<='Z') )
117c2c66affSColin Finck         return x-'A'+10;
118c2c66affSColin Finck     if( (x>='a') && (x<='z') )
119c2c66affSColin Finck         return x-'a'+10+26;
120c2c66affSColin Finck     if( x=='.' )
121c2c66affSColin Finck         return 10+26+26;
122c2c66affSColin Finck     if( x=='_' )
123c2c66affSColin Finck         return 10+26+26+1;
124c2c66affSColin Finck     return -1;
125c2c66affSColin Finck }
126c2c66affSColin Finck 
127c2c66affSColin Finck LPWSTR encode_streamname(BOOL bTable, LPCWSTR in)
128c2c66affSColin Finck {
129c2c66affSColin Finck     DWORD count = MAX_STREAM_NAME;
130c2c66affSColin Finck     DWORD ch, next;
131c2c66affSColin Finck     LPWSTR out, p;
132c2c66affSColin Finck 
133c2c66affSColin Finck     if( !bTable )
134c2c66affSColin Finck         count = lstrlenW( in )+2;
135c2c66affSColin Finck     if (!(out = msi_alloc( count*sizeof(WCHAR) ))) return NULL;
136c2c66affSColin Finck     p = out;
137c2c66affSColin Finck 
138c2c66affSColin Finck     if( bTable )
139c2c66affSColin Finck     {
140c2c66affSColin Finck          *p++ = 0x4840;
141c2c66affSColin Finck          count --;
142c2c66affSColin Finck     }
143c2c66affSColin Finck     while( count -- )
144c2c66affSColin Finck     {
145c2c66affSColin Finck         ch = *in++;
146c2c66affSColin Finck         if( !ch )
147c2c66affSColin Finck         {
148c2c66affSColin Finck             *p = ch;
149c2c66affSColin Finck             return out;
150c2c66affSColin Finck         }
151c2c66affSColin Finck         if( ( ch < 0x80 ) && ( utf2mime(ch) >= 0 ) )
152c2c66affSColin Finck         {
153c2c66affSColin Finck             ch = utf2mime(ch) + 0x4800;
154c2c66affSColin Finck             next = *in;
155c2c66affSColin Finck             if( next && (next<0x80) )
156c2c66affSColin Finck             {
157c2c66affSColin Finck                 next = utf2mime(next);
158c2c66affSColin Finck                 if( next != -1 )
159c2c66affSColin Finck                 {
160c2c66affSColin Finck                      next += 0x3ffffc0;
161c2c66affSColin Finck                      ch += (next<<6);
162c2c66affSColin Finck                      in++;
163c2c66affSColin Finck                 }
164c2c66affSColin Finck             }
165c2c66affSColin Finck         }
166c2c66affSColin Finck         *p++ = ch;
167c2c66affSColin Finck     }
168c2c66affSColin Finck     ERR("Failed to encode stream name (%s)\n",debugstr_w(in));
169c2c66affSColin Finck     msi_free( out );
170c2c66affSColin Finck     return NULL;
171c2c66affSColin Finck }
172c2c66affSColin Finck 
173c2c66affSColin Finck static int mime2utf(int x)
174c2c66affSColin Finck {
175c2c66affSColin Finck     if( x<10 )
176c2c66affSColin Finck         return x + '0';
177c2c66affSColin Finck     if( x<(10+26))
178c2c66affSColin Finck         return x - 10 + 'A';
179c2c66affSColin Finck     if( x<(10+26+26))
180c2c66affSColin Finck         return x - 10 - 26 + 'a';
181c2c66affSColin Finck     if( x == (10+26+26) )
182c2c66affSColin Finck         return '.';
183c2c66affSColin Finck     return '_';
184c2c66affSColin Finck }
185c2c66affSColin Finck 
186c2c66affSColin Finck BOOL decode_streamname(LPCWSTR in, LPWSTR out)
187c2c66affSColin Finck {
188c2c66affSColin Finck     WCHAR ch;
189c2c66affSColin Finck     DWORD count = 0;
190c2c66affSColin Finck 
191c2c66affSColin Finck     while ( (ch = *in++) )
192c2c66affSColin Finck     {
193c2c66affSColin Finck         if( (ch >= 0x3800 ) && (ch < 0x4840 ) )
194c2c66affSColin Finck         {
195c2c66affSColin Finck             if( ch >= 0x4800 )
196c2c66affSColin Finck                 ch = mime2utf(ch-0x4800);
197c2c66affSColin Finck             else
198c2c66affSColin Finck             {
199c2c66affSColin Finck                 ch -= 0x3800;
200c2c66affSColin Finck                 *out++ = mime2utf(ch&0x3f);
201c2c66affSColin Finck                 count++;
202c2c66affSColin Finck                 ch = mime2utf((ch>>6)&0x3f);
203c2c66affSColin Finck             }
204c2c66affSColin Finck         }
205c2c66affSColin Finck         *out++ = ch;
206c2c66affSColin Finck         count++;
207c2c66affSColin Finck     }
208c2c66affSColin Finck     *out = 0;
209c2c66affSColin Finck     return count;
210c2c66affSColin Finck }
211c2c66affSColin Finck 
212c2c66affSColin Finck void enum_stream_names( IStorage *stg )
213c2c66affSColin Finck {
214c2c66affSColin Finck     IEnumSTATSTG *stgenum = NULL;
215c2c66affSColin Finck     HRESULT r;
216c2c66affSColin Finck     STATSTG stat;
217c2c66affSColin Finck     ULONG n, count;
218c2c66affSColin Finck     WCHAR name[0x40];
219c2c66affSColin Finck 
220c2c66affSColin Finck     r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
221c2c66affSColin Finck     if( FAILED( r ) )
222c2c66affSColin Finck         return;
223c2c66affSColin Finck 
224c2c66affSColin Finck     n = 0;
225c2c66affSColin Finck     while( 1 )
226c2c66affSColin Finck     {
227c2c66affSColin Finck         count = 0;
228c2c66affSColin Finck         r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
229c2c66affSColin Finck         if( FAILED( r ) || !count )
230c2c66affSColin Finck             break;
231c2c66affSColin Finck         decode_streamname( stat.pwcsName, name );
232c2c66affSColin Finck         TRACE("stream %2d -> %s %s\n", n,
233c2c66affSColin Finck               debugstr_w(stat.pwcsName), debugstr_w(name) );
234c2c66affSColin Finck         CoTaskMemFree( stat.pwcsName );
235c2c66affSColin Finck         n++;
236c2c66affSColin Finck     }
237c2c66affSColin Finck 
238c2c66affSColin Finck     IEnumSTATSTG_Release( stgenum );
239c2c66affSColin Finck }
240c2c66affSColin Finck 
241c2c66affSColin Finck UINT read_stream_data( IStorage *stg, LPCWSTR stname, BOOL table,
242c2c66affSColin Finck                        BYTE **pdata, UINT *psz )
243c2c66affSColin Finck {
244c2c66affSColin Finck     HRESULT r;
245c2c66affSColin Finck     UINT ret = ERROR_FUNCTION_FAILED;
246c2c66affSColin Finck     VOID *data;
247c2c66affSColin Finck     ULONG sz, count;
248c2c66affSColin Finck     IStream *stm = NULL;
249c2c66affSColin Finck     STATSTG stat;
250c2c66affSColin Finck     LPWSTR encname;
251c2c66affSColin Finck 
252c2c66affSColin Finck     encname = encode_streamname(table, stname);
253c2c66affSColin Finck 
254c2c66affSColin Finck     TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
255c2c66affSColin Finck 
256c2c66affSColin Finck     r = IStorage_OpenStream(stg, encname, NULL,
257c2c66affSColin Finck             STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stm);
258c2c66affSColin Finck     msi_free( encname );
259c2c66affSColin Finck     if( FAILED( r ) )
260c2c66affSColin Finck     {
261c2c66affSColin Finck         WARN("open stream failed r = %08x - empty table?\n", r);
262c2c66affSColin Finck         return ret;
263c2c66affSColin Finck     }
264c2c66affSColin Finck 
265c2c66affSColin Finck     r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
266c2c66affSColin Finck     if( FAILED( r ) )
267c2c66affSColin Finck     {
268c2c66affSColin Finck         WARN("open stream failed r = %08x!\n", r);
269c2c66affSColin Finck         goto end;
270c2c66affSColin Finck     }
271c2c66affSColin Finck 
272c2c66affSColin Finck     if( stat.cbSize.QuadPart >> 32 )
273c2c66affSColin Finck     {
274c2c66affSColin Finck         WARN("Too big!\n");
275c2c66affSColin Finck         goto end;
276c2c66affSColin Finck     }
277c2c66affSColin Finck 
278c2c66affSColin Finck     sz = stat.cbSize.QuadPart;
279c2c66affSColin Finck     data = msi_alloc( sz );
280c2c66affSColin Finck     if( !data )
281c2c66affSColin Finck     {
282c2c66affSColin Finck         WARN("couldn't allocate memory r=%08x!\n", r);
283c2c66affSColin Finck         ret = ERROR_NOT_ENOUGH_MEMORY;
284c2c66affSColin Finck         goto end;
285c2c66affSColin Finck     }
286c2c66affSColin Finck 
287c2c66affSColin Finck     r = IStream_Read(stm, data, sz, &count );
288c2c66affSColin Finck     if( FAILED( r ) || ( count != sz ) )
289c2c66affSColin Finck     {
290c2c66affSColin Finck         msi_free( data );
291c2c66affSColin Finck         WARN("read stream failed r = %08x!\n", r);
292c2c66affSColin Finck         goto end;
293c2c66affSColin Finck     }
294c2c66affSColin Finck 
295c2c66affSColin Finck     *pdata = data;
296c2c66affSColin Finck     *psz = sz;
297c2c66affSColin Finck     ret = ERROR_SUCCESS;
298c2c66affSColin Finck 
299c2c66affSColin Finck end:
300c2c66affSColin Finck     IStream_Release( stm );
301c2c66affSColin Finck 
302c2c66affSColin Finck     return ret;
303c2c66affSColin Finck }
304c2c66affSColin Finck 
305c2c66affSColin Finck UINT write_stream_data( IStorage *stg, LPCWSTR stname,
306c2c66affSColin Finck                         LPCVOID data, UINT sz, BOOL bTable )
307c2c66affSColin Finck {
308c2c66affSColin Finck     HRESULT r;
309c2c66affSColin Finck     UINT ret = ERROR_FUNCTION_FAILED;
310c2c66affSColin Finck     ULONG count;
311c2c66affSColin Finck     IStream *stm = NULL;
312c2c66affSColin Finck     ULARGE_INTEGER size;
313c2c66affSColin Finck     LARGE_INTEGER pos;
314c2c66affSColin Finck     LPWSTR encname;
315c2c66affSColin Finck 
316c2c66affSColin Finck     encname = encode_streamname(bTable, stname );
317c2c66affSColin Finck     r = IStorage_OpenStream( stg, encname, NULL,
318c2c66affSColin Finck             STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, &stm);
319c2c66affSColin Finck     if( FAILED(r) )
320c2c66affSColin Finck     {
321c2c66affSColin Finck         r = IStorage_CreateStream( stg, encname,
322c2c66affSColin Finck                 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
323c2c66affSColin Finck     }
324c2c66affSColin Finck     msi_free( encname );
325c2c66affSColin Finck     if( FAILED( r ) )
326c2c66affSColin Finck     {
327c2c66affSColin Finck         WARN("open stream failed r = %08x\n", r);
328c2c66affSColin Finck         return ret;
329c2c66affSColin Finck     }
330c2c66affSColin Finck 
331c2c66affSColin Finck     size.QuadPart = sz;
332c2c66affSColin Finck     r = IStream_SetSize( stm, size );
333c2c66affSColin Finck     if( FAILED( r ) )
334c2c66affSColin Finck     {
335c2c66affSColin Finck         WARN("Failed to SetSize\n");
336c2c66affSColin Finck         goto end;
337c2c66affSColin Finck     }
338c2c66affSColin Finck 
339c2c66affSColin Finck     pos.QuadPart = 0;
340c2c66affSColin Finck     r = IStream_Seek( stm, pos, STREAM_SEEK_SET, NULL );
341c2c66affSColin Finck     if( FAILED( r ) )
342c2c66affSColin Finck     {
343c2c66affSColin Finck         WARN("Failed to Seek\n");
344c2c66affSColin Finck         goto end;
345c2c66affSColin Finck     }
346c2c66affSColin Finck 
347c2c66affSColin Finck     if (sz)
348c2c66affSColin Finck     {
349c2c66affSColin Finck         r = IStream_Write(stm, data, sz, &count );
350c2c66affSColin Finck         if( FAILED( r ) || ( count != sz ) )
351c2c66affSColin Finck         {
352c2c66affSColin Finck             WARN("Failed to Write\n");
353c2c66affSColin Finck             goto end;
354c2c66affSColin Finck         }
355c2c66affSColin Finck     }
356c2c66affSColin Finck 
357c2c66affSColin Finck     ret = ERROR_SUCCESS;
358c2c66affSColin Finck 
359c2c66affSColin Finck end:
360c2c66affSColin Finck     IStream_Release( stm );
361c2c66affSColin Finck 
362c2c66affSColin Finck     return ret;
363c2c66affSColin Finck }
364c2c66affSColin Finck 
365c2c66affSColin Finck static void msi_free_colinfo( MSICOLUMNINFO *colinfo, UINT count )
366c2c66affSColin Finck {
367c2c66affSColin Finck     UINT i;
368c2c66affSColin Finck     for (i = 0; i < count; i++) msi_free( colinfo[i].hash_table );
369c2c66affSColin Finck }
370c2c66affSColin Finck 
371c2c66affSColin Finck static void free_table( MSITABLE *table )
372c2c66affSColin Finck {
373c2c66affSColin Finck     UINT i;
374c2c66affSColin Finck     for( i=0; i<table->row_count; i++ )
375c2c66affSColin Finck         msi_free( table->data[i] );
376c2c66affSColin Finck     msi_free( table->data );
377c2c66affSColin Finck     msi_free( table->data_persistent );
378c2c66affSColin Finck     msi_free_colinfo( table->colinfo, table->col_count );
379c2c66affSColin Finck     msi_free( table->colinfo );
380c2c66affSColin Finck     msi_free( table );
381c2c66affSColin Finck }
382c2c66affSColin Finck 
383c2c66affSColin Finck static UINT msi_table_get_row_size( MSIDATABASE *db, const MSICOLUMNINFO *cols, UINT count, UINT bytes_per_strref )
384c2c66affSColin Finck {
385c2c66affSColin Finck     const MSICOLUMNINFO *last_col;
386c2c66affSColin Finck 
387c2c66affSColin Finck     if (!count)
388c2c66affSColin Finck         return 0;
389c2c66affSColin Finck 
390c2c66affSColin Finck     if (bytes_per_strref != LONG_STR_BYTES)
391c2c66affSColin Finck     {
392c2c66affSColin Finck         UINT i, size = 0;
393c2c66affSColin Finck         for (i = 0; i < count; i++) size += bytes_per_column( db, &cols[i], bytes_per_strref );
394c2c66affSColin Finck         return size;
395c2c66affSColin Finck     }
396c2c66affSColin Finck     last_col = &cols[count - 1];
397c2c66affSColin Finck     return last_col->offset + bytes_per_column( db, last_col, bytes_per_strref );
398c2c66affSColin Finck }
399c2c66affSColin Finck 
400c2c66affSColin Finck /* add this table to the list of cached tables in the database */
401c2c66affSColin Finck static UINT read_table_from_storage( MSIDATABASE *db, MSITABLE *t, IStorage *stg )
402c2c66affSColin Finck {
403c2c66affSColin Finck     BYTE *rawdata = NULL;
404c2c66affSColin Finck     UINT rawsize = 0, i, j, row_size, row_size_mem;
405c2c66affSColin Finck 
406c2c66affSColin Finck     TRACE("%s\n",debugstr_w(t->name));
407c2c66affSColin Finck 
408c2c66affSColin Finck     row_size = msi_table_get_row_size( db, t->colinfo, t->col_count, db->bytes_per_strref );
409c2c66affSColin Finck     row_size_mem = msi_table_get_row_size( db, t->colinfo, t->col_count, LONG_STR_BYTES );
410c2c66affSColin Finck 
411c2c66affSColin Finck     /* if we can't read the table, just assume that it's empty */
412c2c66affSColin Finck     read_stream_data( stg, t->name, TRUE, &rawdata, &rawsize );
413c2c66affSColin Finck     if( !rawdata )
414c2c66affSColin Finck         return ERROR_SUCCESS;
415c2c66affSColin Finck 
416c2c66affSColin Finck     TRACE("Read %d bytes\n", rawsize );
417c2c66affSColin Finck 
418c2c66affSColin Finck     if( rawsize % row_size )
419c2c66affSColin Finck     {
420c2c66affSColin Finck         WARN("Table size is invalid %d/%d\n", rawsize, row_size );
421c2c66affSColin Finck         goto err;
422c2c66affSColin Finck     }
423c2c66affSColin Finck 
424c2c66affSColin Finck     if ((t->row_count = rawsize / row_size))
425c2c66affSColin Finck     {
426c2c66affSColin Finck         if (!(t->data = msi_alloc_zero( t->row_count * sizeof(USHORT *) ))) goto err;
427c2c66affSColin Finck         if (!(t->data_persistent = msi_alloc_zero( t->row_count * sizeof(BOOL) ))) goto err;
428c2c66affSColin Finck     }
429c2c66affSColin Finck 
430c2c66affSColin Finck     /* transpose all the data */
431c2c66affSColin Finck     TRACE("Transposing data from %d rows\n", t->row_count );
432c2c66affSColin Finck     for (i = 0; i < t->row_count; i++)
433c2c66affSColin Finck     {
434c2c66affSColin Finck         UINT ofs = 0, ofs_mem = 0;
435c2c66affSColin Finck 
436c2c66affSColin Finck         t->data[i] = msi_alloc( row_size_mem );
437c2c66affSColin Finck         if( !t->data[i] )
438c2c66affSColin Finck             goto err;
439c2c66affSColin Finck         t->data_persistent[i] = TRUE;
440c2c66affSColin Finck 
441c2c66affSColin Finck         for (j = 0; j < t->col_count; j++)
442c2c66affSColin Finck         {
443c2c66affSColin Finck             UINT m = bytes_per_column( db, &t->colinfo[j], LONG_STR_BYTES );
444c2c66affSColin Finck             UINT n = bytes_per_column( db, &t->colinfo[j], db->bytes_per_strref );
445c2c66affSColin Finck             UINT k;
446c2c66affSColin Finck 
447c2c66affSColin Finck             if ( n != 2 && n != 3 && n != 4 )
448c2c66affSColin Finck             {
449c2c66affSColin Finck                 ERR("oops - unknown column width %d\n", n);
450c2c66affSColin Finck                 goto err;
451c2c66affSColin Finck             }
452c2c66affSColin Finck             if (t->colinfo[j].type & MSITYPE_STRING && n < m)
453c2c66affSColin Finck             {
454c2c66affSColin Finck                 for (k = 0; k < m; k++)
455c2c66affSColin Finck                 {
456c2c66affSColin Finck                     if (k < n)
457c2c66affSColin Finck                         t->data[i][ofs_mem + k] = rawdata[ofs * t->row_count + i * n + k];
458c2c66affSColin Finck                     else
459c2c66affSColin Finck                         t->data[i][ofs_mem + k] = 0;
460c2c66affSColin Finck                 }
461c2c66affSColin Finck             }
462c2c66affSColin Finck             else
463c2c66affSColin Finck             {
464c2c66affSColin Finck                 for (k = 0; k < n; k++)
465c2c66affSColin Finck                     t->data[i][ofs_mem + k] = rawdata[ofs * t->row_count + i * n + k];
466c2c66affSColin Finck             }
467c2c66affSColin Finck             ofs_mem += m;
468c2c66affSColin Finck             ofs += n;
469c2c66affSColin Finck         }
470c2c66affSColin Finck     }
471c2c66affSColin Finck 
472c2c66affSColin Finck     msi_free( rawdata );
473c2c66affSColin Finck     return ERROR_SUCCESS;
474c2c66affSColin Finck err:
475c2c66affSColin Finck     msi_free( rawdata );
476c2c66affSColin Finck     return ERROR_FUNCTION_FAILED;
477c2c66affSColin Finck }
478c2c66affSColin Finck 
479c2c66affSColin Finck void free_cached_tables( MSIDATABASE *db )
480c2c66affSColin Finck {
481c2c66affSColin Finck     while( !list_empty( &db->tables ) )
482c2c66affSColin Finck     {
483c2c66affSColin Finck         MSITABLE *t = LIST_ENTRY( list_head( &db->tables ), MSITABLE, entry );
484c2c66affSColin Finck 
485c2c66affSColin Finck         list_remove( &t->entry );
486c2c66affSColin Finck         free_table( t );
487c2c66affSColin Finck     }
488c2c66affSColin Finck }
489c2c66affSColin Finck 
490c2c66affSColin Finck static MSITABLE *find_cached_table( MSIDATABASE *db, LPCWSTR name )
491c2c66affSColin Finck {
492c2c66affSColin Finck     MSITABLE *t;
493c2c66affSColin Finck 
494c2c66affSColin Finck     LIST_FOR_EACH_ENTRY( t, &db->tables, MSITABLE, entry )
495958f1addSwinesync         if( !wcscmp( name, t->name ) )
496c2c66affSColin Finck             return t;
497c2c66affSColin Finck 
498c2c66affSColin Finck     return NULL;
499c2c66affSColin Finck }
500c2c66affSColin Finck 
501c2c66affSColin Finck static void table_calc_column_offsets( MSIDATABASE *db, MSICOLUMNINFO *colinfo, DWORD count )
502c2c66affSColin Finck {
503c2c66affSColin Finck     DWORD i;
504c2c66affSColin Finck 
505c2c66affSColin Finck     for (i = 0; colinfo && i < count; i++)
506c2c66affSColin Finck     {
507c2c66affSColin Finck          assert( i + 1 == colinfo[i].number );
508c2c66affSColin Finck          if (i) colinfo[i].offset = colinfo[i - 1].offset +
509c2c66affSColin Finck                                     bytes_per_column( db, &colinfo[i - 1], LONG_STR_BYTES );
510c2c66affSColin Finck          else colinfo[i].offset = 0;
511c2c66affSColin Finck 
512c2c66affSColin Finck          TRACE("column %d is [%s] with type %08x ofs %d\n",
513c2c66affSColin Finck                colinfo[i].number, debugstr_w(colinfo[i].colname),
514c2c66affSColin Finck                colinfo[i].type, colinfo[i].offset);
515c2c66affSColin Finck     }
516c2c66affSColin Finck }
517c2c66affSColin Finck 
518c2c66affSColin Finck static UINT get_defaulttablecolumns( MSIDATABASE *db, LPCWSTR name, MSICOLUMNINFO *colinfo, UINT *sz )
519c2c66affSColin Finck {
520c2c66affSColin Finck     const MSICOLUMNINFO *p;
521c2c66affSColin Finck     DWORD i, n;
522c2c66affSColin Finck 
523c2c66affSColin Finck     TRACE("%s\n", debugstr_w(name));
524c2c66affSColin Finck 
525958f1addSwinesync     if (!wcscmp( name, szTables ))
526c2c66affSColin Finck     {
527c2c66affSColin Finck         p = _Tables_cols;
528c2c66affSColin Finck         n = 1;
529c2c66affSColin Finck     }
530958f1addSwinesync     else if (!wcscmp( name, szColumns ))
531c2c66affSColin Finck     {
532c2c66affSColin Finck         p = _Columns_cols;
533c2c66affSColin Finck         n = 4;
534c2c66affSColin Finck     }
535c2c66affSColin Finck     else return ERROR_FUNCTION_FAILED;
536c2c66affSColin Finck 
537c2c66affSColin Finck     for (i = 0; i < n; i++)
538c2c66affSColin Finck     {
539c2c66affSColin Finck         if (colinfo && i < *sz) colinfo[i] = p[i];
540c2c66affSColin Finck         if (colinfo && i >= *sz) break;
541c2c66affSColin Finck     }
542c2c66affSColin Finck     table_calc_column_offsets( db, colinfo, n );
543c2c66affSColin Finck     *sz = n;
544c2c66affSColin Finck     return ERROR_SUCCESS;
545c2c66affSColin Finck }
546c2c66affSColin Finck 
547c2c66affSColin Finck static UINT get_tablecolumns( MSIDATABASE *db, LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz );
548c2c66affSColin Finck 
549c2c66affSColin Finck static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name, MSICOLUMNINFO **pcols, UINT *pcount )
550c2c66affSColin Finck {
551c2c66affSColin Finck     UINT r, column_count = 0;
552c2c66affSColin Finck     MSICOLUMNINFO *columns;
553c2c66affSColin Finck 
554c2c66affSColin Finck     /* get the number of columns in this table */
555c2c66affSColin Finck     column_count = 0;
556c2c66affSColin Finck     r = get_tablecolumns( db, name, NULL, &column_count );
557c2c66affSColin Finck     if (r != ERROR_SUCCESS)
558c2c66affSColin Finck         return r;
559c2c66affSColin Finck 
560c2c66affSColin Finck     *pcount = column_count;
561c2c66affSColin Finck 
562c2c66affSColin Finck     /* if there are no columns, there's no table */
563c2c66affSColin Finck     if (!column_count)
564c2c66affSColin Finck         return ERROR_INVALID_PARAMETER;
565c2c66affSColin Finck 
566c2c66affSColin Finck     TRACE("table %s found\n", debugstr_w(name));
567c2c66affSColin Finck 
568c2c66affSColin Finck     columns = msi_alloc( column_count * sizeof(MSICOLUMNINFO) );
569c2c66affSColin Finck     if (!columns)
570c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
571c2c66affSColin Finck 
572c2c66affSColin Finck     r = get_tablecolumns( db, name, columns, &column_count );
573c2c66affSColin Finck     if (r != ERROR_SUCCESS)
574c2c66affSColin Finck     {
575c2c66affSColin Finck         msi_free( columns );
576c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
577c2c66affSColin Finck     }
578c2c66affSColin Finck     *pcols = columns;
579c2c66affSColin Finck     return r;
580c2c66affSColin Finck }
581c2c66affSColin Finck 
582c2c66affSColin Finck static UINT get_table( MSIDATABASE *db, LPCWSTR name, MSITABLE **table_ret )
583c2c66affSColin Finck {
584c2c66affSColin Finck     MSITABLE *table;
585c2c66affSColin Finck     UINT r;
586c2c66affSColin Finck 
587c2c66affSColin Finck     /* first, see if the table is cached */
588c2c66affSColin Finck     table = find_cached_table( db, name );
589c2c66affSColin Finck     if (table)
590c2c66affSColin Finck     {
591c2c66affSColin Finck         *table_ret = table;
592c2c66affSColin Finck         return ERROR_SUCCESS;
593c2c66affSColin Finck     }
594c2c66affSColin Finck 
595c2c66affSColin Finck     /* nonexistent tables should be interpreted as empty tables */
596c2c66affSColin Finck     table = msi_alloc( sizeof(MSITABLE) + lstrlenW( name ) * sizeof(WCHAR) );
597c2c66affSColin Finck     if (!table)
598c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
599c2c66affSColin Finck 
600c2c66affSColin Finck     table->row_count = 0;
601c2c66affSColin Finck     table->data = NULL;
602c2c66affSColin Finck     table->data_persistent = NULL;
603c2c66affSColin Finck     table->colinfo = NULL;
604c2c66affSColin Finck     table->col_count = 0;
605c2c66affSColin Finck     table->persistent = MSICONDITION_TRUE;
606c2c66affSColin Finck     lstrcpyW( table->name, name );
607c2c66affSColin Finck 
608958f1addSwinesync     if (!wcscmp( name, szTables ) || !wcscmp( name, szColumns ))
609c2c66affSColin Finck         table->persistent = MSICONDITION_NONE;
610c2c66affSColin Finck 
611c2c66affSColin Finck     r = table_get_column_info( db, name, &table->colinfo, &table->col_count );
612c2c66affSColin Finck     if (r != ERROR_SUCCESS)
613c2c66affSColin Finck     {
614c2c66affSColin Finck         free_table( table );
615c2c66affSColin Finck         return r;
616c2c66affSColin Finck     }
617c2c66affSColin Finck     r = read_table_from_storage( db, table, db->storage );
618c2c66affSColin Finck     if (r != ERROR_SUCCESS)
619c2c66affSColin Finck     {
620c2c66affSColin Finck         free_table( table );
621c2c66affSColin Finck         return r;
622c2c66affSColin Finck     }
623c2c66affSColin Finck     list_add_head( &db->tables, &table->entry );
624c2c66affSColin Finck     *table_ret = table;
625c2c66affSColin Finck     return ERROR_SUCCESS;
626c2c66affSColin Finck }
627c2c66affSColin Finck 
628c2c66affSColin Finck static UINT read_table_int( BYTE *const *data, UINT row, UINT col, UINT bytes )
629c2c66affSColin Finck {
630c2c66affSColin Finck     UINT ret = 0, i;
631c2c66affSColin Finck 
632c2c66affSColin Finck     for (i = 0; i < bytes; i++)
633c2c66affSColin Finck         ret += data[row][col + i] << i * 8;
634c2c66affSColin Finck 
635c2c66affSColin Finck     return ret;
636c2c66affSColin Finck }
637c2c66affSColin Finck 
638c2c66affSColin Finck static UINT get_tablecolumns( MSIDATABASE *db, LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz )
639c2c66affSColin Finck {
640c2c66affSColin Finck     UINT r, i, n = 0, table_id, count, maxcount = *sz;
641c2c66affSColin Finck     MSITABLE *table = NULL;
642c2c66affSColin Finck 
643c2c66affSColin Finck     TRACE("%s\n", debugstr_w(szTableName));
644c2c66affSColin Finck 
645c2c66affSColin Finck     /* first check if there is a default table with that name */
646c2c66affSColin Finck     r = get_defaulttablecolumns( db, szTableName, colinfo, sz );
647c2c66affSColin Finck     if (r == ERROR_SUCCESS && *sz)
648c2c66affSColin Finck         return r;
649c2c66affSColin Finck 
650c2c66affSColin Finck     r = get_table( db, szColumns, &table );
651c2c66affSColin Finck     if (r != ERROR_SUCCESS)
652c2c66affSColin Finck     {
653c2c66affSColin Finck         ERR("couldn't load _Columns table\n");
654c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
655c2c66affSColin Finck     }
656c2c66affSColin Finck 
657c2c66affSColin Finck     /* convert table and column names to IDs from the string table */
658c2c66affSColin Finck     r = msi_string2id( db->strings, szTableName, -1, &table_id );
659c2c66affSColin Finck     if (r != ERROR_SUCCESS)
660c2c66affSColin Finck     {
661c2c66affSColin Finck         WARN("Couldn't find id for %s\n", debugstr_w(szTableName));
662c2c66affSColin Finck         return r;
663c2c66affSColin Finck     }
664c2c66affSColin Finck     TRACE("Table id is %d, row count is %d\n", table_id, table->row_count);
665c2c66affSColin Finck 
666c2c66affSColin Finck     /* Note: _Columns table doesn't have non-persistent data */
667c2c66affSColin Finck 
668c2c66affSColin Finck     /* if maxcount is non-zero, assume it's exactly right for this table */
669c2c66affSColin Finck     if (colinfo) memset( colinfo, 0, maxcount * sizeof(*colinfo) );
670c2c66affSColin Finck     count = table->row_count;
671c2c66affSColin Finck     for (i = 0; i < count; i++)
672c2c66affSColin Finck     {
673c2c66affSColin Finck         if (read_table_int( table->data, i, 0, LONG_STR_BYTES) != table_id) continue;
674c2c66affSColin Finck         if (colinfo)
675c2c66affSColin Finck         {
676c2c66affSColin Finck             UINT id = read_table_int( table->data, i, table->colinfo[2].offset, LONG_STR_BYTES );
677c2c66affSColin Finck             UINT col = read_table_int( table->data, i, table->colinfo[1].offset, sizeof(USHORT) ) - (1 << 15);
678c2c66affSColin Finck 
679c2c66affSColin Finck             /* check the column number is in range */
680c2c66affSColin Finck             if (col < 1 || col > maxcount)
681c2c66affSColin Finck             {
682c2c66affSColin Finck                 ERR("column %d out of range (maxcount: %d)\n", col, maxcount);
683c2c66affSColin Finck                 continue;
684c2c66affSColin Finck             }
685c2c66affSColin Finck             /* check if this column was already set */
686c2c66affSColin Finck             if (colinfo[col - 1].number)
687c2c66affSColin Finck             {
688c2c66affSColin Finck                 ERR("duplicate column %d\n", col);
689c2c66affSColin Finck                 continue;
690c2c66affSColin Finck             }
691c2c66affSColin Finck             colinfo[col - 1].tablename = msi_string_lookup( db->strings, table_id, NULL );
692c2c66affSColin Finck             colinfo[col - 1].number = col;
693c2c66affSColin Finck             colinfo[col - 1].colname = msi_string_lookup( db->strings, id, NULL );
694c2c66affSColin Finck             colinfo[col - 1].type = read_table_int( table->data, i, table->colinfo[3].offset,
695c2c66affSColin Finck                                                     sizeof(USHORT) ) - (1 << 15);
696c2c66affSColin Finck             colinfo[col - 1].offset = 0;
697c2c66affSColin Finck             colinfo[col - 1].ref_count = 0;
698c2c66affSColin Finck             colinfo[col - 1].hash_table = NULL;
699c2c66affSColin Finck         }
700c2c66affSColin Finck         n++;
701c2c66affSColin Finck     }
702c2c66affSColin Finck     TRACE("%s has %d columns\n", debugstr_w(szTableName), n);
703c2c66affSColin Finck 
704c2c66affSColin Finck     if (colinfo && n != maxcount)
705c2c66affSColin Finck     {
706c2c66affSColin Finck         ERR("missing column in table %s\n", debugstr_w(szTableName));
707c2c66affSColin Finck         msi_free_colinfo( colinfo, maxcount );
708c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
709c2c66affSColin Finck     }
710c2c66affSColin Finck     table_calc_column_offsets( db, colinfo, n );
711c2c66affSColin Finck     *sz = n;
712c2c66affSColin Finck     return ERROR_SUCCESS;
713c2c66affSColin Finck }
714c2c66affSColin Finck 
715c2c66affSColin Finck UINT msi_create_table( MSIDATABASE *db, LPCWSTR name, column_info *col_info,
716c2c66affSColin Finck                        MSICONDITION persistent )
717c2c66affSColin Finck {
718c2c66affSColin Finck     UINT r, nField;
719c2c66affSColin Finck     MSIVIEW *tv = NULL;
720c2c66affSColin Finck     MSIRECORD *rec = NULL;
721c2c66affSColin Finck     column_info *col;
722c2c66affSColin Finck     MSITABLE *table;
723c2c66affSColin Finck     UINT i;
724c2c66affSColin Finck 
725c2c66affSColin Finck     /* only add tables that don't exist already */
726c2c66affSColin Finck     if( TABLE_Exists(db, name ) )
727c2c66affSColin Finck     {
728c2c66affSColin Finck         WARN("table %s exists\n", debugstr_w(name));
729c2c66affSColin Finck         return ERROR_BAD_QUERY_SYNTAX;
730c2c66affSColin Finck     }
731c2c66affSColin Finck 
732c2c66affSColin Finck     table = msi_alloc( sizeof (MSITABLE) + lstrlenW(name)*sizeof (WCHAR) );
733c2c66affSColin Finck     if( !table )
734c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
735c2c66affSColin Finck 
736c2c66affSColin Finck     table->ref_count = 1;
737c2c66affSColin Finck     table->row_count = 0;
738c2c66affSColin Finck     table->data = NULL;
739c2c66affSColin Finck     table->data_persistent = NULL;
740c2c66affSColin Finck     table->colinfo = NULL;
741c2c66affSColin Finck     table->col_count = 0;
742c2c66affSColin Finck     table->persistent = persistent;
743c2c66affSColin Finck     lstrcpyW( table->name, name );
744c2c66affSColin Finck 
745c2c66affSColin Finck     for( col = col_info; col; col = col->next )
746c2c66affSColin Finck         table->col_count++;
747c2c66affSColin Finck 
748c2c66affSColin Finck     table->colinfo = msi_alloc( table->col_count * sizeof(MSICOLUMNINFO) );
749c2c66affSColin Finck     if (!table->colinfo)
750c2c66affSColin Finck     {
751c2c66affSColin Finck         free_table( table );
752c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
753c2c66affSColin Finck     }
754c2c66affSColin Finck 
755c2c66affSColin Finck     for( i = 0, col = col_info; col; i++, col = col->next )
756c2c66affSColin Finck     {
757211c4104Swinesync         UINT table_id = msi_add_string( db->strings, col->table, -1, persistent );
758211c4104Swinesync         UINT col_id = msi_add_string( db->strings, col->column, -1, persistent );
759c2c66affSColin Finck 
760c2c66affSColin Finck         table->colinfo[ i ].tablename = msi_string_lookup( db->strings, table_id, NULL );
761c2c66affSColin Finck         table->colinfo[ i ].number = i + 1;
762c2c66affSColin Finck         table->colinfo[ i ].colname = msi_string_lookup( db->strings, col_id, NULL );
763c2c66affSColin Finck         table->colinfo[ i ].type = col->type;
764c2c66affSColin Finck         table->colinfo[ i ].offset = 0;
765c2c66affSColin Finck         table->colinfo[ i ].ref_count = 0;
766c2c66affSColin Finck         table->colinfo[ i ].hash_table = NULL;
767c2c66affSColin Finck         table->colinfo[ i ].temporary = col->temporary;
768c2c66affSColin Finck     }
769c2c66affSColin Finck     table_calc_column_offsets( db, table->colinfo, table->col_count);
770c2c66affSColin Finck 
771c2c66affSColin Finck     r = TABLE_CreateView( db, szTables, &tv );
772c2c66affSColin Finck     TRACE("CreateView returned %x\n", r);
773c2c66affSColin Finck     if( r )
774c2c66affSColin Finck     {
775c2c66affSColin Finck         free_table( table );
776c2c66affSColin Finck         return r;
777c2c66affSColin Finck     }
778c2c66affSColin Finck 
779c2c66affSColin Finck     r = tv->ops->execute( tv, 0 );
780c2c66affSColin Finck     TRACE("tv execute returned %x\n", r);
781c2c66affSColin Finck     if( r )
782c2c66affSColin Finck         goto err;
783c2c66affSColin Finck 
784c2c66affSColin Finck     rec = MSI_CreateRecord( 1 );
785c2c66affSColin Finck     if( !rec )
786c2c66affSColin Finck         goto err;
787c2c66affSColin Finck 
788c2c66affSColin Finck     r = MSI_RecordSetStringW( rec, 1, name );
789c2c66affSColin Finck     if( r )
790c2c66affSColin Finck         goto err;
791c2c66affSColin Finck 
792c2c66affSColin Finck     r = tv->ops->insert_row( tv, rec, -1, persistent == MSICONDITION_FALSE );
793c2c66affSColin Finck     TRACE("insert_row returned %x\n", r);
794c2c66affSColin Finck     if( r )
795c2c66affSColin Finck         goto err;
796c2c66affSColin Finck 
797c2c66affSColin Finck     tv->ops->delete( tv );
798c2c66affSColin Finck     tv = NULL;
799c2c66affSColin Finck 
800c2c66affSColin Finck     msiobj_release( &rec->hdr );
801c2c66affSColin Finck     rec = NULL;
802c2c66affSColin Finck 
803c2c66affSColin Finck     if( persistent != MSICONDITION_FALSE )
804c2c66affSColin Finck     {
805c2c66affSColin Finck         /* add each column to the _Columns table */
806c2c66affSColin Finck         r = TABLE_CreateView( db, szColumns, &tv );
807c2c66affSColin Finck         if( r )
808c2c66affSColin Finck             goto err;
809c2c66affSColin Finck 
810c2c66affSColin Finck         r = tv->ops->execute( tv, 0 );
811c2c66affSColin Finck         TRACE("tv execute returned %x\n", r);
812c2c66affSColin Finck         if( r )
813c2c66affSColin Finck             goto err;
814c2c66affSColin Finck 
815c2c66affSColin Finck         rec = MSI_CreateRecord( 4 );
816c2c66affSColin Finck         if( !rec )
817c2c66affSColin Finck             goto err;
818c2c66affSColin Finck 
819c2c66affSColin Finck         r = MSI_RecordSetStringW( rec, 1, name );
820c2c66affSColin Finck         if( r )
821c2c66affSColin Finck             goto err;
822c2c66affSColin Finck 
823c2c66affSColin Finck         /*
824c2c66affSColin Finck          * need to set the table, column number, col name and type
825c2c66affSColin Finck          * for each column we enter in the table
826c2c66affSColin Finck          */
827c2c66affSColin Finck         nField = 1;
828c2c66affSColin Finck         for( col = col_info; col; col = col->next )
829c2c66affSColin Finck         {
830c2c66affSColin Finck             r = MSI_RecordSetInteger( rec, 2, nField );
831c2c66affSColin Finck             if( r )
832c2c66affSColin Finck                 goto err;
833c2c66affSColin Finck 
834c2c66affSColin Finck             r = MSI_RecordSetStringW( rec, 3, col->column );
835c2c66affSColin Finck             if( r )
836c2c66affSColin Finck                 goto err;
837c2c66affSColin Finck 
838c2c66affSColin Finck             r = MSI_RecordSetInteger( rec, 4, col->type );
839c2c66affSColin Finck             if( r )
840c2c66affSColin Finck                 goto err;
841c2c66affSColin Finck 
842c2c66affSColin Finck             r = tv->ops->insert_row( tv, rec, -1, FALSE );
843c2c66affSColin Finck             if( r )
844c2c66affSColin Finck                 goto err;
845c2c66affSColin Finck 
846c2c66affSColin Finck             nField++;
847c2c66affSColin Finck         }
848c2c66affSColin Finck         if( !col )
849c2c66affSColin Finck             r = ERROR_SUCCESS;
850c2c66affSColin Finck     }
851c2c66affSColin Finck 
852c2c66affSColin Finck err:
853c2c66affSColin Finck     if (rec)
854c2c66affSColin Finck         msiobj_release( &rec->hdr );
855c2c66affSColin Finck     /* FIXME: remove values from the string table on error */
856c2c66affSColin Finck     if( tv )
857c2c66affSColin Finck         tv->ops->delete( tv );
858c2c66affSColin Finck 
859c2c66affSColin Finck     if (r == ERROR_SUCCESS)
860c2c66affSColin Finck         list_add_head( &db->tables, &table->entry );
861c2c66affSColin Finck     else
862c2c66affSColin Finck         free_table( table );
863c2c66affSColin Finck 
864c2c66affSColin Finck     return r;
865c2c66affSColin Finck }
866c2c66affSColin Finck 
867c2c66affSColin Finck static UINT save_table( MSIDATABASE *db, const MSITABLE *t, UINT bytes_per_strref )
868c2c66affSColin Finck {
869c2c66affSColin Finck     BYTE *rawdata = NULL;
870c2c66affSColin Finck     UINT rawsize, i, j, row_size, row_count;
871c2c66affSColin Finck     UINT r = ERROR_FUNCTION_FAILED;
872c2c66affSColin Finck 
873c2c66affSColin Finck     /* Nothing to do for non-persistent tables */
874c2c66affSColin Finck     if( t->persistent == MSICONDITION_FALSE )
875c2c66affSColin Finck         return ERROR_SUCCESS;
876c2c66affSColin Finck 
877c2c66affSColin Finck     TRACE("Saving %s\n", debugstr_w( t->name ) );
878c2c66affSColin Finck 
879c2c66affSColin Finck     row_size = msi_table_get_row_size( db, t->colinfo, t->col_count, bytes_per_strref );
880c2c66affSColin Finck     row_count = t->row_count;
881c2c66affSColin Finck     for (i = 0; i < t->row_count; i++)
882c2c66affSColin Finck     {
883c2c66affSColin Finck         if (!t->data_persistent[i])
884c2c66affSColin Finck         {
885c2c66affSColin Finck             row_count = 1; /* yes, this is bizarre */
886c2c66affSColin Finck             break;
887c2c66affSColin Finck         }
888c2c66affSColin Finck     }
889c2c66affSColin Finck     rawsize = row_count * row_size;
890c2c66affSColin Finck     rawdata = msi_alloc_zero( rawsize );
891c2c66affSColin Finck     if( !rawdata )
892c2c66affSColin Finck     {
893c2c66affSColin Finck         r = ERROR_NOT_ENOUGH_MEMORY;
894c2c66affSColin Finck         goto err;
895c2c66affSColin Finck     }
896c2c66affSColin Finck 
897c2c66affSColin Finck     rawsize = 0;
898c2c66affSColin Finck     for (i = 0; i < row_count; i++)
899c2c66affSColin Finck     {
900c2c66affSColin Finck         UINT ofs = 0, ofs_mem = 0;
901c2c66affSColin Finck 
902c2c66affSColin Finck         if (!t->data_persistent[i]) break;
903c2c66affSColin Finck 
904c2c66affSColin Finck         for (j = 0; j < t->col_count; j++)
905c2c66affSColin Finck         {
906c2c66affSColin Finck             UINT m = bytes_per_column( db, &t->colinfo[j], LONG_STR_BYTES );
907c2c66affSColin Finck             UINT n = bytes_per_column( db, &t->colinfo[j], bytes_per_strref );
908c2c66affSColin Finck             UINT k;
909c2c66affSColin Finck 
910c2c66affSColin Finck             if (n != 2 && n != 3 && n != 4)
911c2c66affSColin Finck             {
912c2c66affSColin Finck                 ERR("oops - unknown column width %d\n", n);
913c2c66affSColin Finck                 goto err;
914c2c66affSColin Finck             }
915c2c66affSColin Finck             if (t->colinfo[j].type & MSITYPE_STRING && n < m)
916c2c66affSColin Finck             {
917c2c66affSColin Finck                 UINT id = read_table_int( t->data, i, ofs_mem, LONG_STR_BYTES );
918c2c66affSColin Finck                 if (id > 1 << bytes_per_strref * 8)
919c2c66affSColin Finck                 {
920c2c66affSColin Finck                     ERR("string id %u out of range\n", id);
921c2c66affSColin Finck                     goto err;
922c2c66affSColin Finck                 }
923c2c66affSColin Finck             }
924c2c66affSColin Finck             for (k = 0; k < n; k++)
925c2c66affSColin Finck             {
926c2c66affSColin Finck                 rawdata[ofs * row_count + i * n + k] = t->data[i][ofs_mem + k];
927c2c66affSColin Finck             }
928c2c66affSColin Finck             ofs_mem += m;
929c2c66affSColin Finck             ofs += n;
930c2c66affSColin Finck         }
931c2c66affSColin Finck         rawsize += row_size;
932c2c66affSColin Finck     }
933c2c66affSColin Finck 
934c2c66affSColin Finck     TRACE("writing %d bytes\n", rawsize);
935c2c66affSColin Finck     r = write_stream_data( db->storage, t->name, rawdata, rawsize, TRUE );
936c2c66affSColin Finck 
937c2c66affSColin Finck err:
938c2c66affSColin Finck     msi_free( rawdata );
939c2c66affSColin Finck     return r;
940c2c66affSColin Finck }
941c2c66affSColin Finck 
942c2c66affSColin Finck static void msi_update_table_columns( MSIDATABASE *db, LPCWSTR name )
943c2c66affSColin Finck {
944c2c66affSColin Finck     MSITABLE *table;
945c2c66affSColin Finck     UINT size, offset, old_count;
946c2c66affSColin Finck     UINT n;
947c2c66affSColin Finck 
948c2c66affSColin Finck     if (!(table = find_cached_table( db, name ))) return;
949c2c66affSColin Finck     old_count = table->col_count;
950c2c66affSColin Finck     msi_free_colinfo( table->colinfo, table->col_count );
951c2c66affSColin Finck     msi_free( table->colinfo );
952c2c66affSColin Finck     table->colinfo = NULL;
953c2c66affSColin Finck 
954c2c66affSColin Finck     table_get_column_info( db, name, &table->colinfo, &table->col_count );
955c2c66affSColin Finck     if (!table->col_count) return;
956c2c66affSColin Finck 
957c2c66affSColin Finck     size = msi_table_get_row_size( db, table->colinfo, table->col_count, LONG_STR_BYTES );
958c2c66affSColin Finck     offset = table->colinfo[table->col_count - 1].offset;
959c2c66affSColin Finck 
960c2c66affSColin Finck     for ( n = 0; n < table->row_count; n++ )
961c2c66affSColin Finck     {
962c2c66affSColin Finck         table->data[n] = msi_realloc( table->data[n], size );
963c2c66affSColin Finck         if (old_count < table->col_count)
964c2c66affSColin Finck             memset( &table->data[n][offset], 0, size - offset );
965c2c66affSColin Finck     }
966c2c66affSColin Finck }
967c2c66affSColin Finck 
968c2c66affSColin Finck /* try to find the table name in the _Tables table */
969c2c66affSColin Finck BOOL TABLE_Exists( MSIDATABASE *db, LPCWSTR name )
970c2c66affSColin Finck {
971c2c66affSColin Finck     UINT r, table_id, i;
972c2c66affSColin Finck     MSITABLE *table;
973c2c66affSColin Finck 
974958f1addSwinesync     if( !wcscmp( name, szTables ) || !wcscmp( name, szColumns ) ||
975958f1addSwinesync         !wcscmp( name, szStreams ) || !wcscmp( name, szStorages ) )
976c2c66affSColin Finck         return TRUE;
977c2c66affSColin Finck 
978c2c66affSColin Finck     r = msi_string2id( db->strings, name, -1, &table_id );
979c2c66affSColin Finck     if( r != ERROR_SUCCESS )
980c2c66affSColin Finck     {
981c2c66affSColin Finck         TRACE("Couldn't find id for %s\n", debugstr_w(name));
982c2c66affSColin Finck         return FALSE;
983c2c66affSColin Finck     }
984c2c66affSColin Finck 
985c2c66affSColin Finck     r = get_table( db, szTables, &table );
986c2c66affSColin Finck     if( r != ERROR_SUCCESS )
987c2c66affSColin Finck     {
988c2c66affSColin Finck         ERR("table %s not available\n", debugstr_w(szTables));
989c2c66affSColin Finck         return FALSE;
990c2c66affSColin Finck     }
991c2c66affSColin Finck 
992c2c66affSColin Finck     for( i = 0; i < table->row_count; i++ )
993c2c66affSColin Finck     {
994c2c66affSColin Finck         if( read_table_int( table->data, i, 0, LONG_STR_BYTES ) == table_id )
995c2c66affSColin Finck             return TRUE;
996c2c66affSColin Finck     }
997c2c66affSColin Finck 
998c2c66affSColin Finck     return FALSE;
999c2c66affSColin Finck }
1000c2c66affSColin Finck 
1001c2c66affSColin Finck /* below is the query interface to a table */
1002c2c66affSColin Finck 
1003c2c66affSColin Finck typedef struct tagMSITABLEVIEW
1004c2c66affSColin Finck {
1005c2c66affSColin Finck     MSIVIEW        view;
1006c2c66affSColin Finck     MSIDATABASE   *db;
1007c2c66affSColin Finck     MSITABLE      *table;
1008c2c66affSColin Finck     MSICOLUMNINFO *columns;
1009c2c66affSColin Finck     UINT           num_cols;
1010c2c66affSColin Finck     UINT           row_size;
1011c2c66affSColin Finck     WCHAR          name[1];
1012c2c66affSColin Finck } MSITABLEVIEW;
1013c2c66affSColin Finck 
1014c2c66affSColin Finck static UINT TABLE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
1015c2c66affSColin Finck {
1016c2c66affSColin Finck     MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1017c2c66affSColin Finck     UINT offset, n;
1018c2c66affSColin Finck 
1019c2c66affSColin Finck     if( !tv->table )
1020c2c66affSColin Finck         return ERROR_INVALID_PARAMETER;
1021c2c66affSColin Finck 
1022c2c66affSColin Finck     if( (col==0) || (col>tv->num_cols) )
1023c2c66affSColin Finck         return ERROR_INVALID_PARAMETER;
1024c2c66affSColin Finck 
1025c2c66affSColin Finck     /* how many rows are there ? */
1026c2c66affSColin Finck     if( row >= tv->table->row_count )
1027c2c66affSColin Finck         return ERROR_NO_MORE_ITEMS;
1028c2c66affSColin Finck 
1029c2c66affSColin Finck     if( tv->columns[col-1].offset >= tv->row_size )
1030c2c66affSColin Finck     {
1031c2c66affSColin Finck         ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1032c2c66affSColin Finck         ERR("%p %p\n", tv, tv->columns );
1033c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
1034c2c66affSColin Finck     }
1035c2c66affSColin Finck 
1036c2c66affSColin Finck     n = bytes_per_column( tv->db, &tv->columns[col - 1], LONG_STR_BYTES );
1037c2c66affSColin Finck     if (n != 2 && n != 3 && n != 4)
1038c2c66affSColin Finck     {
1039c2c66affSColin Finck         ERR("oops! what is %d bytes per column?\n", n );
1040c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
1041c2c66affSColin Finck     }
1042c2c66affSColin Finck 
1043c2c66affSColin Finck     offset = tv->columns[col-1].offset;
1044c2c66affSColin Finck     *val = read_table_int(tv->table->data, row, offset, n);
1045c2c66affSColin Finck 
1046c2c66affSColin Finck     /* TRACE("Data [%d][%d] = %d\n", row, col, *val ); */
1047c2c66affSColin Finck 
1048c2c66affSColin Finck     return ERROR_SUCCESS;
1049c2c66affSColin Finck }
1050c2c66affSColin Finck 
1051c2c66affSColin Finck static UINT get_stream_name( const MSITABLEVIEW *tv, UINT row, WCHAR **pstname )
1052c2c66affSColin Finck {
1053c2c66affSColin Finck     LPWSTR p, stname = NULL;
1054c2c66affSColin Finck     UINT i, r, type, ival;
1055c2c66affSColin Finck     DWORD len;
1056c2c66affSColin Finck     LPCWSTR sval;
1057c2c66affSColin Finck     MSIVIEW *view = (MSIVIEW *) tv;
1058c2c66affSColin Finck 
1059c2c66affSColin Finck     TRACE("%p %d\n", tv, row);
1060c2c66affSColin Finck 
1061c2c66affSColin Finck     len = lstrlenW( tv->name ) + 1;
1062c2c66affSColin Finck     stname = msi_alloc( len*sizeof(WCHAR) );
1063c2c66affSColin Finck     if ( !stname )
1064c2c66affSColin Finck     {
1065c2c66affSColin Finck        r = ERROR_OUTOFMEMORY;
1066c2c66affSColin Finck        goto err;
1067c2c66affSColin Finck     }
1068c2c66affSColin Finck 
1069c2c66affSColin Finck     lstrcpyW( stname, tv->name );
1070c2c66affSColin Finck 
1071c2c66affSColin Finck     for ( i = 0; i < tv->num_cols; i++ )
1072c2c66affSColin Finck     {
1073c2c66affSColin Finck         type = tv->columns[i].type;
1074c2c66affSColin Finck         if ( type & MSITYPE_KEY )
1075c2c66affSColin Finck         {
1076c2c66affSColin Finck             WCHAR number[0x20];
1077c2c66affSColin Finck 
1078c2c66affSColin Finck             r = TABLE_fetch_int( view, row, i+1, &ival );
1079c2c66affSColin Finck             if ( r != ERROR_SUCCESS )
1080c2c66affSColin Finck                 goto err;
1081c2c66affSColin Finck 
1082c2c66affSColin Finck             if ( tv->columns[i].type & MSITYPE_STRING )
1083c2c66affSColin Finck             {
1084c2c66affSColin Finck                 sval = msi_string_lookup( tv->db->strings, ival, NULL );
1085c2c66affSColin Finck                 if ( !sval )
1086c2c66affSColin Finck                 {
1087c2c66affSColin Finck                     r = ERROR_INVALID_PARAMETER;
1088c2c66affSColin Finck                     goto err;
1089c2c66affSColin Finck                 }
1090c2c66affSColin Finck             }
1091c2c66affSColin Finck             else
1092c2c66affSColin Finck             {
1093c2c66affSColin Finck                 static const WCHAR fmt[] = { '%','d',0 };
1094c2c66affSColin Finck                 UINT n = bytes_per_column( tv->db, &tv->columns[i], LONG_STR_BYTES );
1095c2c66affSColin Finck 
1096c2c66affSColin Finck                 switch( n )
1097c2c66affSColin Finck                 {
1098c2c66affSColin Finck                 case 2:
1099958f1addSwinesync                     swprintf( number, ARRAY_SIZE(number), fmt, ival-0x8000 );
1100c2c66affSColin Finck                     break;
1101c2c66affSColin Finck                 case 4:
1102958f1addSwinesync                     swprintf( number, ARRAY_SIZE(number), fmt, ival^0x80000000 );
1103c2c66affSColin Finck                     break;
1104c2c66affSColin Finck                 default:
1105c2c66affSColin Finck                     ERR( "oops - unknown column width %d\n", n );
1106c2c66affSColin Finck                     r = ERROR_FUNCTION_FAILED;
1107c2c66affSColin Finck                     goto err;
1108c2c66affSColin Finck                 }
1109c2c66affSColin Finck                 sval = number;
1110c2c66affSColin Finck             }
1111c2c66affSColin Finck 
1112c2c66affSColin Finck             len += lstrlenW( szDot ) + lstrlenW( sval );
1113c2c66affSColin Finck             p = msi_realloc ( stname, len*sizeof(WCHAR) );
1114c2c66affSColin Finck             if ( !p )
1115c2c66affSColin Finck             {
1116c2c66affSColin Finck                 r = ERROR_OUTOFMEMORY;
1117c2c66affSColin Finck                 goto err;
1118c2c66affSColin Finck             }
1119c2c66affSColin Finck             stname = p;
1120c2c66affSColin Finck 
1121c2c66affSColin Finck             lstrcatW( stname, szDot );
1122c2c66affSColin Finck             lstrcatW( stname, sval );
1123c2c66affSColin Finck         }
1124c2c66affSColin Finck         else
1125c2c66affSColin Finck            continue;
1126c2c66affSColin Finck     }
1127c2c66affSColin Finck 
1128c2c66affSColin Finck     *pstname = stname;
1129c2c66affSColin Finck     return ERROR_SUCCESS;
1130c2c66affSColin Finck 
1131c2c66affSColin Finck err:
1132c2c66affSColin Finck     msi_free( stname );
1133c2c66affSColin Finck     *pstname = NULL;
1134c2c66affSColin Finck     return r;
1135c2c66affSColin Finck }
1136c2c66affSColin Finck 
1137c2c66affSColin Finck /*
1138c2c66affSColin Finck  * We need a special case for streams, as we need to reference column with
1139c2c66affSColin Finck  * the name of the stream in the same table, and the table name
1140c2c66affSColin Finck  * which may not be available at higher levels of the query
1141c2c66affSColin Finck  */
1142c2c66affSColin Finck static UINT TABLE_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm )
1143c2c66affSColin Finck {
1144c2c66affSColin Finck     MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1145c2c66affSColin Finck     UINT r;
1146c2c66affSColin Finck     WCHAR *name;
1147c2c66affSColin Finck 
1148c2c66affSColin Finck     if( !view->ops->fetch_int )
1149c2c66affSColin Finck         return ERROR_INVALID_PARAMETER;
1150c2c66affSColin Finck 
1151c2c66affSColin Finck     r = get_stream_name( tv, row, &name );
1152c2c66affSColin Finck     if (r != ERROR_SUCCESS)
1153c2c66affSColin Finck     {
1154c2c66affSColin Finck         ERR("fetching stream, error = %u\n", r);
1155c2c66affSColin Finck         return r;
1156c2c66affSColin Finck     }
1157c2c66affSColin Finck 
1158c2c66affSColin Finck     r = msi_get_stream( tv->db, name, stm );
1159c2c66affSColin Finck     if (r != ERROR_SUCCESS)
1160c2c66affSColin Finck         ERR("fetching stream %s, error = %u\n", debugstr_w(name), r);
1161c2c66affSColin Finck 
1162c2c66affSColin Finck     msi_free( name );
1163c2c66affSColin Finck     return r;
1164c2c66affSColin Finck }
1165c2c66affSColin Finck 
116697918c36Swinesync /* Set a table value, i.e. preadjusted integer or string ID. */
116797918c36Swinesync static UINT table_set_bytes( MSITABLEVIEW *tv, UINT row, UINT col, UINT val )
1168c2c66affSColin Finck {
1169c2c66affSColin Finck     UINT offset, n, i;
1170c2c66affSColin Finck 
1171c2c66affSColin Finck     if( !tv->table )
1172c2c66affSColin Finck         return ERROR_INVALID_PARAMETER;
1173c2c66affSColin Finck 
1174c2c66affSColin Finck     if( (col==0) || (col>tv->num_cols) )
1175c2c66affSColin Finck         return ERROR_INVALID_PARAMETER;
1176c2c66affSColin Finck 
1177c2c66affSColin Finck     if( row >= tv->table->row_count )
1178c2c66affSColin Finck         return ERROR_INVALID_PARAMETER;
1179c2c66affSColin Finck 
1180c2c66affSColin Finck     if( tv->columns[col-1].offset >= tv->row_size )
1181c2c66affSColin Finck     {
1182c2c66affSColin Finck         ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1183c2c66affSColin Finck         ERR("%p %p\n", tv, tv->columns );
1184c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
1185c2c66affSColin Finck     }
1186c2c66affSColin Finck 
1187c2c66affSColin Finck     msi_free( tv->columns[col-1].hash_table );
1188c2c66affSColin Finck     tv->columns[col-1].hash_table = NULL;
1189c2c66affSColin Finck 
1190c2c66affSColin Finck     n = bytes_per_column( tv->db, &tv->columns[col - 1], LONG_STR_BYTES );
1191c2c66affSColin Finck     if ( n != 2 && n != 3 && n != 4 )
1192c2c66affSColin Finck     {
1193c2c66affSColin Finck         ERR("oops! what is %d bytes per column?\n", n );
1194c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
1195c2c66affSColin Finck     }
1196c2c66affSColin Finck 
1197c2c66affSColin Finck     offset = tv->columns[col-1].offset;
1198c2c66affSColin Finck     for ( i = 0; i < n; i++ )
1199c2c66affSColin Finck         tv->table->data[row][offset + i] = (val >> i * 8) & 0xff;
1200c2c66affSColin Finck 
1201c2c66affSColin Finck     return ERROR_SUCCESS;
1202c2c66affSColin Finck }
1203c2c66affSColin Finck 
12040e83dd03Swinesync static UINT int_to_table_storage( const MSITABLEVIEW *tv, UINT col, int val, UINT *ret )
12050e83dd03Swinesync {
12060e83dd03Swinesync     if ((tv->columns[col-1].type & MSI_DATASIZEMASK) == 2)
12070e83dd03Swinesync     {
12080e83dd03Swinesync         if (val == MSI_NULL_INTEGER)
12090e83dd03Swinesync             *ret = 0;
12100e83dd03Swinesync         else if ((val + 0x8000) & 0xffff0000)
12110e83dd03Swinesync         {
12120e83dd03Swinesync             ERR("value %d out of range\n", val);
12130e83dd03Swinesync             return ERROR_FUNCTION_FAILED;
12140e83dd03Swinesync         }
12150e83dd03Swinesync         else
12160e83dd03Swinesync             *ret = val + 0x8000;
12170e83dd03Swinesync     }
12180e83dd03Swinesync     else
12190e83dd03Swinesync         *ret = val ^ 0x80000000;
12200e83dd03Swinesync 
12210e83dd03Swinesync     return ERROR_SUCCESS;
12220e83dd03Swinesync }
12230e83dd03Swinesync 
122497918c36Swinesync static UINT TABLE_set_int( MSIVIEW *view, UINT row, UINT col, int val )
122597918c36Swinesync {
122697918c36Swinesync     MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
122797918c36Swinesync     UINT r, table_int;
122897918c36Swinesync 
122997918c36Swinesync     TRACE("row %u, col %u, val %d.\n", row, col, val);
123097918c36Swinesync 
123197918c36Swinesync     if ((r = int_to_table_storage( tv, col, val, &table_int )))
123297918c36Swinesync         return r;
123397918c36Swinesync 
123497918c36Swinesync     if (tv->columns[col-1].type & MSITYPE_KEY)
123597918c36Swinesync     {
123697918c36Swinesync         UINT key;
123797918c36Swinesync 
123897918c36Swinesync         if ((r = TABLE_fetch_int( view, row, col, &key )))
123997918c36Swinesync             return r;
124097918c36Swinesync         if (key != table_int)
124197918c36Swinesync         {
124297918c36Swinesync             ERR("Cannot modify primary key %s.%s.\n",
124397918c36Swinesync                 debugstr_w(tv->table->name), debugstr_w(tv->columns[col-1].colname));
124497918c36Swinesync             return ERROR_FUNCTION_FAILED;
124597918c36Swinesync         }
124697918c36Swinesync     }
124797918c36Swinesync 
124897918c36Swinesync     return table_set_bytes( tv, row, col, table_int );
124997918c36Swinesync }
125097918c36Swinesync 
125197918c36Swinesync static UINT TABLE_set_string( MSIVIEW *view, UINT row, UINT col, const WCHAR *val, int len )
125297918c36Swinesync {
125397918c36Swinesync     MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
125497918c36Swinesync     BOOL persistent;
125597918c36Swinesync     UINT id, r;
125697918c36Swinesync 
125797918c36Swinesync     TRACE("row %u, col %u, val %s.\n", row, col, debugstr_wn(val, len));
125897918c36Swinesync 
125997918c36Swinesync     persistent = (tv->table->persistent != MSICONDITION_FALSE)
126097918c36Swinesync                   && tv->table->data_persistent[row];
126197918c36Swinesync 
126297918c36Swinesync     if (val)
126397918c36Swinesync     {
126497918c36Swinesync         r = msi_string2id( tv->db->strings, val, len, &id );
126597918c36Swinesync         if (r != ERROR_SUCCESS)
126697918c36Swinesync             id = msi_add_string( tv->db->strings, val, len, persistent );
126797918c36Swinesync     }
126897918c36Swinesync     else
126997918c36Swinesync         id = 0;
127097918c36Swinesync 
127197918c36Swinesync     if (tv->columns[col-1].type & MSITYPE_KEY)
127297918c36Swinesync     {
127397918c36Swinesync         UINT key;
127497918c36Swinesync 
127597918c36Swinesync         if ((r = TABLE_fetch_int( view, row, col, &key )))
127697918c36Swinesync             return r;
127797918c36Swinesync         if (key != id)
127897918c36Swinesync         {
127997918c36Swinesync             ERR("Cannot modify primary key %s.%s.\n",
128097918c36Swinesync                 debugstr_w(tv->table->name), debugstr_w(tv->columns[col-1].colname));
128197918c36Swinesync             return ERROR_FUNCTION_FAILED;
128297918c36Swinesync         }
128397918c36Swinesync     }
128497918c36Swinesync 
128597918c36Swinesync     return table_set_bytes( tv, row, col, id );
128697918c36Swinesync }
128797918c36Swinesync 
1288c2c66affSColin Finck static UINT TABLE_get_row( struct tagMSIVIEW *view, UINT row, MSIRECORD **rec )
1289c2c66affSColin Finck {
1290c2c66affSColin Finck     MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1291c2c66affSColin Finck 
1292c2c66affSColin Finck     if (!tv->table)
1293c2c66affSColin Finck         return ERROR_INVALID_PARAMETER;
1294c2c66affSColin Finck 
1295c2c66affSColin Finck     return msi_view_get_row(tv->db, view, row, rec);
1296c2c66affSColin Finck }
1297c2c66affSColin Finck 
1298c2c66affSColin Finck static UINT add_stream( MSIDATABASE *db, const WCHAR *name, IStream *data )
1299c2c66affSColin Finck {
1300c2c66affSColin Finck     static const WCHAR insert[] = {
1301c2c66affSColin Finck         'I','N','S','E','R','T',' ','I','N','T','O',' ',
1302c2c66affSColin Finck         '`','_','S','t','r','e','a','m','s','`',' ',
1303c2c66affSColin Finck         '(','`','N','a','m','e','`',',','`','D','a','t','a','`',')',' ',
1304c2c66affSColin Finck         'V','A','L','U','E','S',' ','(','?',',','?',')',0};
1305c2c66affSColin Finck     static const WCHAR update[] = {
1306c2c66affSColin Finck         'U','P','D','A','T','E',' ','`','_','S','t','r','e','a','m','s','`',' ',
1307c2c66affSColin Finck         'S','E','T',' ','`','D','a','t','a','`',' ','=',' ','?',' ',
1308c2c66affSColin Finck         'W','H','E','R','E',' ','`','N','a','m','e','`',' ','=',' ','?',0};
1309c2c66affSColin Finck     MSIQUERY *query;
1310c2c66affSColin Finck     MSIRECORD *rec;
1311c2c66affSColin Finck     UINT r;
1312c2c66affSColin Finck 
1313c2c66affSColin Finck     TRACE("%p %s %p\n", db, debugstr_w(name), data);
1314c2c66affSColin Finck 
1315c2c66affSColin Finck     if (!(rec = MSI_CreateRecord( 2 )))
1316c2c66affSColin Finck         return ERROR_OUTOFMEMORY;
1317c2c66affSColin Finck 
1318c2c66affSColin Finck     r = MSI_RecordSetStringW( rec, 1, name );
1319c2c66affSColin Finck     if (r != ERROR_SUCCESS)
1320c2c66affSColin Finck        goto done;
1321c2c66affSColin Finck 
1322c2c66affSColin Finck     r = MSI_RecordSetIStream( rec, 2, data );
1323c2c66affSColin Finck     if (r != ERROR_SUCCESS)
1324c2c66affSColin Finck        goto done;
1325c2c66affSColin Finck 
1326c2c66affSColin Finck     r = MSI_DatabaseOpenViewW( db, insert, &query );
1327c2c66affSColin Finck     if (r != ERROR_SUCCESS)
1328c2c66affSColin Finck        goto done;
1329c2c66affSColin Finck 
1330c2c66affSColin Finck     r = MSI_ViewExecute( query, rec );
1331c2c66affSColin Finck     msiobj_release( &query->hdr );
1332c2c66affSColin Finck     if (r == ERROR_SUCCESS)
1333c2c66affSColin Finck         goto done;
1334c2c66affSColin Finck 
1335c2c66affSColin Finck     msiobj_release( &rec->hdr );
1336c2c66affSColin Finck     if (!(rec = MSI_CreateRecord( 2 )))
1337c2c66affSColin Finck         return ERROR_OUTOFMEMORY;
1338c2c66affSColin Finck 
1339c2c66affSColin Finck     r = MSI_RecordSetIStream( rec, 1, data );
1340c2c66affSColin Finck     if (r != ERROR_SUCCESS)
1341c2c66affSColin Finck        goto done;
1342c2c66affSColin Finck 
1343c2c66affSColin Finck     r = MSI_RecordSetStringW( rec, 2, name );
1344c2c66affSColin Finck     if (r != ERROR_SUCCESS)
1345c2c66affSColin Finck        goto done;
1346c2c66affSColin Finck 
1347c2c66affSColin Finck     r = MSI_DatabaseOpenViewW( db, update, &query );
1348c2c66affSColin Finck     if (r != ERROR_SUCCESS)
1349c2c66affSColin Finck         goto done;
1350c2c66affSColin Finck 
1351c2c66affSColin Finck     r = MSI_ViewExecute( query, rec );
1352c2c66affSColin Finck     msiobj_release( &query->hdr );
1353c2c66affSColin Finck 
1354c2c66affSColin Finck done:
1355c2c66affSColin Finck     msiobj_release( &rec->hdr );
1356c2c66affSColin Finck     return r;
1357c2c66affSColin Finck }
1358c2c66affSColin Finck 
1359580d3e1bSwinesync static UINT TABLE_set_stream( MSIVIEW *view, UINT row, UINT col, IStream *stream )
1360580d3e1bSwinesync {
1361580d3e1bSwinesync     MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1362580d3e1bSwinesync     WCHAR *name;
1363580d3e1bSwinesync     UINT r;
1364580d3e1bSwinesync 
1365580d3e1bSwinesync     TRACE("row %u, col %u, stream %p.\n", row, col, stream);
1366580d3e1bSwinesync 
1367580d3e1bSwinesync     if ((r = get_stream_name( tv, row - 1, &name )))
1368580d3e1bSwinesync         return r;
1369580d3e1bSwinesync 
1370580d3e1bSwinesync     r = add_stream( tv->db, name, stream );
1371580d3e1bSwinesync     msi_free( name );
1372580d3e1bSwinesync     return r;
1373580d3e1bSwinesync }
1374580d3e1bSwinesync 
1375c2c66affSColin Finck static UINT get_table_value_from_record( MSITABLEVIEW *tv, MSIRECORD *rec, UINT iField, UINT *pvalue )
1376c2c66affSColin Finck {
1377c2c66affSColin Finck     MSICOLUMNINFO columninfo;
1378c2c66affSColin Finck     UINT r;
1379c2c66affSColin Finck 
1380306890e8Swinesync     if (!iField || iField > tv->num_cols || MSI_RecordIsNull( rec, iField ))
1381c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
1382c2c66affSColin Finck 
1383c2c66affSColin Finck     columninfo = tv->columns[ iField - 1 ];
1384c2c66affSColin Finck 
1385c2c66affSColin Finck     if ( MSITYPE_IS_BINARY(columninfo.type) )
1386c2c66affSColin Finck     {
1387c2c66affSColin Finck         *pvalue = 1; /* refers to the first key column */
1388c2c66affSColin Finck     }
1389c2c66affSColin Finck     else if ( columninfo.type & MSITYPE_STRING )
1390c2c66affSColin Finck     {
1391c2c66affSColin Finck         int len;
1392c2c66affSColin Finck         const WCHAR *sval = msi_record_get_string( rec, iField, &len );
1393c2c66affSColin Finck         if (sval)
1394c2c66affSColin Finck         {
1395c2c66affSColin Finck             r = msi_string2id( tv->db->strings, sval, len, pvalue );
1396c2c66affSColin Finck             if (r != ERROR_SUCCESS)
1397c2c66affSColin Finck                 return ERROR_NOT_FOUND;
1398c2c66affSColin Finck         }
1399c2c66affSColin Finck         else *pvalue = 0;
1400c2c66affSColin Finck     }
1401c2c66affSColin Finck     else
14020e83dd03Swinesync         return int_to_table_storage( tv, iField, MSI_RecordGetInteger( rec, iField ), pvalue );
1403c2c66affSColin Finck 
1404c2c66affSColin Finck     return ERROR_SUCCESS;
1405c2c66affSColin Finck }
1406c2c66affSColin Finck 
1407c2c66affSColin Finck static UINT TABLE_set_row( struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask )
1408c2c66affSColin Finck {
1409c2c66affSColin Finck     MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1410c2c66affSColin Finck     UINT i, val, r = ERROR_SUCCESS;
1411c2c66affSColin Finck 
1412c2c66affSColin Finck     if ( !tv->table )
1413c2c66affSColin Finck         return ERROR_INVALID_PARAMETER;
1414c2c66affSColin Finck 
1415c2c66affSColin Finck     /* test if any of the mask bits are invalid */
1416c2c66affSColin Finck     if ( mask >= (1<<tv->num_cols) )
1417c2c66affSColin Finck         return ERROR_INVALID_PARAMETER;
1418c2c66affSColin Finck 
1419c2c66affSColin Finck     for ( i = 0; i < tv->num_cols; i++ )
1420c2c66affSColin Finck     {
1421c2c66affSColin Finck         BOOL persistent;
1422c2c66affSColin Finck 
1423c2c66affSColin Finck         /* only update the fields specified in the mask */
1424c2c66affSColin Finck         if ( !(mask&(1<<i)) )
1425c2c66affSColin Finck             continue;
1426c2c66affSColin Finck 
1427c2c66affSColin Finck         persistent = (tv->table->persistent != MSICONDITION_FALSE) &&
1428c2c66affSColin Finck                      (tv->table->data_persistent[row]);
1429c2c66affSColin Finck         /* FIXME: should we allow updating keys? */
1430c2c66affSColin Finck 
1431c2c66affSColin Finck         val = 0;
1432c2c66affSColin Finck         if ( !MSI_RecordIsNull( rec, i + 1 ) )
1433c2c66affSColin Finck         {
1434c2c66affSColin Finck             r = get_table_value_from_record (tv, rec, i + 1, &val);
1435c2c66affSColin Finck             if ( MSITYPE_IS_BINARY(tv->columns[ i ].type) )
1436c2c66affSColin Finck             {
1437c2c66affSColin Finck                 IStream *stm;
1438c2c66affSColin Finck                 LPWSTR stname;
1439c2c66affSColin Finck 
1440c2c66affSColin Finck                 if ( r != ERROR_SUCCESS )
1441c2c66affSColin Finck                     return ERROR_FUNCTION_FAILED;
1442c2c66affSColin Finck 
1443c2c66affSColin Finck                 r = MSI_RecordGetIStream( rec, i + 1, &stm );
1444c2c66affSColin Finck                 if ( r != ERROR_SUCCESS )
1445c2c66affSColin Finck                     return r;
1446c2c66affSColin Finck 
1447c2c66affSColin Finck                 r = get_stream_name( tv, row, &stname );
1448c2c66affSColin Finck                 if ( r != ERROR_SUCCESS )
1449c2c66affSColin Finck                 {
1450c2c66affSColin Finck                     IStream_Release( stm );
1451c2c66affSColin Finck                     return r;
1452c2c66affSColin Finck                 }
1453c2c66affSColin Finck 
1454c2c66affSColin Finck                 r = add_stream( tv->db, stname, stm );
1455c2c66affSColin Finck                 IStream_Release( stm );
1456c2c66affSColin Finck                 msi_free ( stname );
1457c2c66affSColin Finck 
1458c2c66affSColin Finck                 if ( r != ERROR_SUCCESS )
1459c2c66affSColin Finck                     return r;
1460c2c66affSColin Finck             }
1461c2c66affSColin Finck             else if ( tv->columns[i].type & MSITYPE_STRING )
1462c2c66affSColin Finck             {
1463c2c66affSColin Finck                 UINT x;
1464c2c66affSColin Finck 
1465c2c66affSColin Finck                 if ( r != ERROR_SUCCESS )
1466c2c66affSColin Finck                 {
1467c2c66affSColin Finck                     int len;
1468c2c66affSColin Finck                     const WCHAR *sval = msi_record_get_string( rec, i + 1, &len );
1469211c4104Swinesync                     val = msi_add_string( tv->db->strings, sval, len, persistent );
1470c2c66affSColin Finck                 }
1471c2c66affSColin Finck                 else
1472c2c66affSColin Finck                 {
1473c2c66affSColin Finck                     TABLE_fetch_int(&tv->view, row, i + 1, &x);
1474c2c66affSColin Finck                     if (val == x)
1475c2c66affSColin Finck                         continue;
1476c2c66affSColin Finck                 }
1477c2c66affSColin Finck             }
1478c2c66affSColin Finck             else
1479c2c66affSColin Finck             {
1480c2c66affSColin Finck                 if ( r != ERROR_SUCCESS )
1481c2c66affSColin Finck                     return ERROR_FUNCTION_FAILED;
1482c2c66affSColin Finck             }
1483c2c66affSColin Finck         }
1484c2c66affSColin Finck 
148597918c36Swinesync         r = table_set_bytes( tv, row, i+1, val );
1486c2c66affSColin Finck         if ( r != ERROR_SUCCESS )
1487c2c66affSColin Finck             break;
1488c2c66affSColin Finck     }
1489c2c66affSColin Finck     return r;
1490c2c66affSColin Finck }
1491c2c66affSColin Finck 
1492c2c66affSColin Finck static UINT table_create_new_row( struct tagMSIVIEW *view, UINT *num, BOOL temporary )
1493c2c66affSColin Finck {
1494c2c66affSColin Finck     MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1495c2c66affSColin Finck     BYTE **p, *row;
1496c2c66affSColin Finck     BOOL *b;
1497c2c66affSColin Finck     UINT sz;
1498c2c66affSColin Finck     BYTE ***data_ptr;
1499c2c66affSColin Finck     BOOL **data_persist_ptr;
1500c2c66affSColin Finck     UINT *row_count;
1501c2c66affSColin Finck 
1502c2c66affSColin Finck     TRACE("%p %s\n", view, temporary ? "TRUE" : "FALSE");
1503c2c66affSColin Finck 
1504c2c66affSColin Finck     if( !tv->table )
1505c2c66affSColin Finck         return ERROR_INVALID_PARAMETER;
1506c2c66affSColin Finck 
1507c2c66affSColin Finck     row = msi_alloc_zero( tv->row_size );
1508c2c66affSColin Finck     if( !row )
1509c2c66affSColin Finck         return ERROR_NOT_ENOUGH_MEMORY;
1510c2c66affSColin Finck 
1511c2c66affSColin Finck     row_count = &tv->table->row_count;
1512c2c66affSColin Finck     data_ptr = &tv->table->data;
1513c2c66affSColin Finck     data_persist_ptr = &tv->table->data_persistent;
1514c2c66affSColin Finck     if (*num == -1)
1515c2c66affSColin Finck         *num = tv->table->row_count;
1516c2c66affSColin Finck 
1517c2c66affSColin Finck     sz = (*row_count + 1) * sizeof (BYTE*);
1518c2c66affSColin Finck     if( *data_ptr )
1519c2c66affSColin Finck         p = msi_realloc( *data_ptr, sz );
1520c2c66affSColin Finck     else
1521c2c66affSColin Finck         p = msi_alloc( sz );
1522c2c66affSColin Finck     if( !p )
1523c2c66affSColin Finck     {
1524c2c66affSColin Finck         msi_free( row );
1525c2c66affSColin Finck         return ERROR_NOT_ENOUGH_MEMORY;
1526c2c66affSColin Finck     }
1527c2c66affSColin Finck 
1528c2c66affSColin Finck     sz = (*row_count + 1) * sizeof (BOOL);
1529c2c66affSColin Finck     if( *data_persist_ptr )
1530c2c66affSColin Finck         b = msi_realloc( *data_persist_ptr, sz );
1531c2c66affSColin Finck     else
1532c2c66affSColin Finck         b = msi_alloc( sz );
1533c2c66affSColin Finck     if( !b )
1534c2c66affSColin Finck     {
1535c2c66affSColin Finck         msi_free( row );
1536c2c66affSColin Finck         msi_free( p );
1537c2c66affSColin Finck         return ERROR_NOT_ENOUGH_MEMORY;
1538c2c66affSColin Finck     }
1539c2c66affSColin Finck 
1540c2c66affSColin Finck     *data_ptr = p;
1541c2c66affSColin Finck     (*data_ptr)[*row_count] = row;
1542c2c66affSColin Finck 
1543c2c66affSColin Finck     *data_persist_ptr = b;
1544c2c66affSColin Finck     (*data_persist_ptr)[*row_count] = !temporary;
1545c2c66affSColin Finck 
1546c2c66affSColin Finck     (*row_count)++;
1547c2c66affSColin Finck 
1548c2c66affSColin Finck     return ERROR_SUCCESS;
1549c2c66affSColin Finck }
1550c2c66affSColin Finck 
1551c2c66affSColin Finck static UINT TABLE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
1552c2c66affSColin Finck {
1553c2c66affSColin Finck     MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1554c2c66affSColin Finck 
1555c2c66affSColin Finck     TRACE("%p %p\n", tv, record);
1556c2c66affSColin Finck 
1557c2c66affSColin Finck     TRACE("There are %d columns\n", tv->num_cols );
1558c2c66affSColin Finck 
1559c2c66affSColin Finck     return ERROR_SUCCESS;
1560c2c66affSColin Finck }
1561c2c66affSColin Finck 
1562c2c66affSColin Finck static UINT TABLE_close( struct tagMSIVIEW *view )
1563c2c66affSColin Finck {
1564c2c66affSColin Finck     TRACE("%p\n", view );
1565c2c66affSColin Finck 
1566c2c66affSColin Finck     return ERROR_SUCCESS;
1567c2c66affSColin Finck }
1568c2c66affSColin Finck 
1569c2c66affSColin Finck static UINT TABLE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols)
1570c2c66affSColin Finck {
1571c2c66affSColin Finck     MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1572c2c66affSColin Finck 
1573c2c66affSColin Finck     TRACE("%p %p %p\n", view, rows, cols );
1574c2c66affSColin Finck 
1575c2c66affSColin Finck     if( cols )
1576c2c66affSColin Finck         *cols = tv->num_cols;
1577c2c66affSColin Finck     if( rows )
1578c2c66affSColin Finck     {
1579c2c66affSColin Finck         if( !tv->table )
1580c2c66affSColin Finck             return ERROR_INVALID_PARAMETER;
1581c2c66affSColin Finck         *rows = tv->table->row_count;
1582c2c66affSColin Finck     }
1583c2c66affSColin Finck 
1584c2c66affSColin Finck     return ERROR_SUCCESS;
1585c2c66affSColin Finck }
1586c2c66affSColin Finck 
1587c2c66affSColin Finck static UINT TABLE_get_column_info( struct tagMSIVIEW *view,
1588c2c66affSColin Finck                 UINT n, LPCWSTR *name, UINT *type, BOOL *temporary,
1589c2c66affSColin Finck                 LPCWSTR *table_name )
1590c2c66affSColin Finck {
1591c2c66affSColin Finck     MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1592c2c66affSColin Finck 
1593c2c66affSColin Finck     TRACE("%p %d %p %p\n", tv, n, name, type );
1594c2c66affSColin Finck 
1595c2c66affSColin Finck     if( ( n == 0 ) || ( n > tv->num_cols ) )
1596c2c66affSColin Finck         return ERROR_INVALID_PARAMETER;
1597c2c66affSColin Finck 
1598c2c66affSColin Finck     if( name )
1599c2c66affSColin Finck     {
1600c2c66affSColin Finck         *name = tv->columns[n-1].colname;
1601c2c66affSColin Finck         if( !*name )
1602c2c66affSColin Finck             return ERROR_FUNCTION_FAILED;
1603c2c66affSColin Finck     }
1604c2c66affSColin Finck 
1605c2c66affSColin Finck     if( table_name )
1606c2c66affSColin Finck     {
1607c2c66affSColin Finck         *table_name = tv->columns[n-1].tablename;
1608c2c66affSColin Finck         if( !*table_name )
1609c2c66affSColin Finck             return ERROR_FUNCTION_FAILED;
1610c2c66affSColin Finck     }
1611c2c66affSColin Finck 
1612c2c66affSColin Finck     if( type )
1613c2c66affSColin Finck         *type = tv->columns[n-1].type;
1614c2c66affSColin Finck 
1615c2c66affSColin Finck     if( temporary )
1616c2c66affSColin Finck         *temporary = tv->columns[n-1].temporary;
1617c2c66affSColin Finck 
1618c2c66affSColin Finck     return ERROR_SUCCESS;
1619c2c66affSColin Finck }
1620c2c66affSColin Finck 
1621c2c66affSColin Finck static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row, UINT *column );
1622c2c66affSColin Finck 
1623c2c66affSColin Finck static UINT table_validate_new( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *column )
1624c2c66affSColin Finck {
1625c2c66affSColin Finck     UINT r, row, i;
1626c2c66affSColin Finck 
1627c2c66affSColin Finck     /* check there are no null values where they're not allowed */
1628c2c66affSColin Finck     for( i = 0; i < tv->num_cols; i++ )
1629c2c66affSColin Finck     {
1630c2c66affSColin Finck         if ( tv->columns[i].type & MSITYPE_NULLABLE )
1631c2c66affSColin Finck             continue;
1632c2c66affSColin Finck 
1633c2c66affSColin Finck         if ( MSITYPE_IS_BINARY(tv->columns[i].type) )
1634c2c66affSColin Finck             TRACE("skipping binary column\n");
1635c2c66affSColin Finck         else if ( tv->columns[i].type & MSITYPE_STRING )
1636c2c66affSColin Finck         {
1637c2c66affSColin Finck             int len;
1638c2c66affSColin Finck             const WCHAR *str = msi_record_get_string( rec, i+1, &len );
1639c2c66affSColin Finck 
1640c2c66affSColin Finck             if (!str || (!str[0] && !len))
1641c2c66affSColin Finck             {
1642c2c66affSColin Finck                 if (column) *column = i;
1643c2c66affSColin Finck                 return ERROR_INVALID_DATA;
1644c2c66affSColin Finck             }
1645c2c66affSColin Finck         }
1646c2c66affSColin Finck         else
1647c2c66affSColin Finck         {
1648c2c66affSColin Finck             UINT n;
1649c2c66affSColin Finck 
1650c2c66affSColin Finck             n = MSI_RecordGetInteger( rec, i+1 );
1651c2c66affSColin Finck             if (n == MSI_NULL_INTEGER)
1652c2c66affSColin Finck             {
1653c2c66affSColin Finck                 if (column) *column = i;
1654c2c66affSColin Finck                 return ERROR_INVALID_DATA;
1655c2c66affSColin Finck             }
1656c2c66affSColin Finck         }
1657c2c66affSColin Finck     }
1658c2c66affSColin Finck 
1659c2c66affSColin Finck     /* check there are no duplicate keys */
1660c2c66affSColin Finck     r = msi_table_find_row( tv, rec, &row, column );
1661c2c66affSColin Finck     if (r == ERROR_SUCCESS)
1662c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
1663c2c66affSColin Finck 
1664c2c66affSColin Finck     return ERROR_SUCCESS;
1665c2c66affSColin Finck }
1666c2c66affSColin Finck 
1667c2c66affSColin Finck static int compare_record( MSITABLEVIEW *tv, UINT row, MSIRECORD *rec )
1668c2c66affSColin Finck {
1669c2c66affSColin Finck     UINT r, i, ivalue, x;
1670c2c66affSColin Finck 
1671c2c66affSColin Finck     for (i = 0; i < tv->num_cols; i++ )
1672c2c66affSColin Finck     {
1673c2c66affSColin Finck         if (!(tv->columns[i].type & MSITYPE_KEY)) continue;
1674c2c66affSColin Finck 
1675c2c66affSColin Finck         r = get_table_value_from_record( tv, rec, i + 1, &ivalue );
1676c2c66affSColin Finck         if (r != ERROR_SUCCESS)
1677c2c66affSColin Finck             return 1;
1678c2c66affSColin Finck 
1679c2c66affSColin Finck         r = TABLE_fetch_int( &tv->view, row, i + 1, &x );
1680c2c66affSColin Finck         if (r != ERROR_SUCCESS)
1681c2c66affSColin Finck         {
1682c2c66affSColin Finck             WARN("TABLE_fetch_int should not fail here %u\n", r);
1683c2c66affSColin Finck             return -1;
1684c2c66affSColin Finck         }
1685c2c66affSColin Finck         if (ivalue > x)
1686c2c66affSColin Finck         {
1687c2c66affSColin Finck             return 1;
1688c2c66affSColin Finck         }
1689c2c66affSColin Finck         else if (ivalue == x)
1690c2c66affSColin Finck         {
1691c2c66affSColin Finck             if (i < tv->num_cols - 1) continue;
1692c2c66affSColin Finck             return 0;
1693c2c66affSColin Finck         }
1694c2c66affSColin Finck         else
1695c2c66affSColin Finck             return -1;
1696c2c66affSColin Finck     }
1697c2c66affSColin Finck     return 1;
1698c2c66affSColin Finck }
1699c2c66affSColin Finck 
1700c2c66affSColin Finck static int find_insert_index( MSITABLEVIEW *tv, MSIRECORD *rec )
1701c2c66affSColin Finck {
1702c2c66affSColin Finck     int idx, c, low = 0, high = tv->table->row_count - 1;
1703c2c66affSColin Finck 
1704c2c66affSColin Finck     TRACE("%p %p\n", tv, rec);
1705c2c66affSColin Finck 
1706c2c66affSColin Finck     while (low <= high)
1707c2c66affSColin Finck     {
1708c2c66affSColin Finck         idx = (low + high) / 2;
1709c2c66affSColin Finck         c = compare_record( tv, idx, rec );
1710c2c66affSColin Finck 
1711c2c66affSColin Finck         if (c < 0)
1712c2c66affSColin Finck             high = idx - 1;
1713c2c66affSColin Finck         else if (c > 0)
1714c2c66affSColin Finck             low = idx + 1;
1715c2c66affSColin Finck         else
1716c2c66affSColin Finck         {
1717c2c66affSColin Finck             TRACE("found %u\n", idx);
1718c2c66affSColin Finck             return idx;
1719c2c66affSColin Finck         }
1720c2c66affSColin Finck     }
1721c2c66affSColin Finck     TRACE("found %u\n", high + 1);
1722c2c66affSColin Finck     return high + 1;
1723c2c66affSColin Finck }
1724c2c66affSColin Finck 
1725c2c66affSColin Finck static UINT TABLE_insert_row( struct tagMSIVIEW *view, MSIRECORD *rec, UINT row, BOOL temporary )
1726c2c66affSColin Finck {
1727c2c66affSColin Finck     MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1728c2c66affSColin Finck     UINT i, r;
1729c2c66affSColin Finck 
1730c2c66affSColin Finck     TRACE("%p %p %s\n", tv, rec, temporary ? "TRUE" : "FALSE" );
1731c2c66affSColin Finck 
1732c2c66affSColin Finck     /* check that the key is unique - can we find a matching row? */
1733c2c66affSColin Finck     r = table_validate_new( tv, rec, NULL );
1734c2c66affSColin Finck     if( r != ERROR_SUCCESS )
1735c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
1736c2c66affSColin Finck 
1737c2c66affSColin Finck     if (row == -1)
1738c2c66affSColin Finck         row = find_insert_index( tv, rec );
1739c2c66affSColin Finck 
1740c2c66affSColin Finck     r = table_create_new_row( view, &row, temporary );
1741c2c66affSColin Finck     TRACE("insert_row returned %08x\n", r);
1742c2c66affSColin Finck     if( r != ERROR_SUCCESS )
1743c2c66affSColin Finck         return r;
1744c2c66affSColin Finck 
1745c2c66affSColin Finck     /* shift the rows to make room for the new row */
1746c2c66affSColin Finck     for (i = tv->table->row_count - 1; i > row; i--)
1747c2c66affSColin Finck     {
1748c2c66affSColin Finck         memmove(&(tv->table->data[i][0]),
1749c2c66affSColin Finck                 &(tv->table->data[i - 1][0]), tv->row_size);
1750c2c66affSColin Finck         tv->table->data_persistent[i] = tv->table->data_persistent[i - 1];
1751c2c66affSColin Finck     }
1752c2c66affSColin Finck 
1753c2c66affSColin Finck     /* Re-set the persistence flag */
1754c2c66affSColin Finck     tv->table->data_persistent[row] = !temporary;
1755c2c66affSColin Finck     return TABLE_set_row( view, row, rec, (1<<tv->num_cols) - 1 );
1756c2c66affSColin Finck }
1757c2c66affSColin Finck 
1758c2c66affSColin Finck static UINT TABLE_delete_row( struct tagMSIVIEW *view, UINT row )
1759c2c66affSColin Finck {
1760c2c66affSColin Finck     MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1761c2c66affSColin Finck     UINT r, num_rows, num_cols, i;
1762c2c66affSColin Finck 
1763c2c66affSColin Finck     TRACE("%p %d\n", tv, row);
1764c2c66affSColin Finck 
1765c2c66affSColin Finck     if ( !tv->table )
1766c2c66affSColin Finck         return ERROR_INVALID_PARAMETER;
1767c2c66affSColin Finck 
1768c2c66affSColin Finck     r = TABLE_get_dimensions( view, &num_rows, &num_cols );
1769c2c66affSColin Finck     if ( r != ERROR_SUCCESS )
1770c2c66affSColin Finck         return r;
1771c2c66affSColin Finck 
1772c2c66affSColin Finck     if ( row >= num_rows )
1773c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
1774c2c66affSColin Finck 
1775c2c66affSColin Finck     num_rows = tv->table->row_count;
1776c2c66affSColin Finck     tv->table->row_count--;
1777c2c66affSColin Finck 
1778c2c66affSColin Finck     /* reset the hash tables */
1779c2c66affSColin Finck     for (i = 0; i < tv->num_cols; i++)
1780c2c66affSColin Finck     {
1781c2c66affSColin Finck         msi_free( tv->columns[i].hash_table );
1782c2c66affSColin Finck         tv->columns[i].hash_table = NULL;
1783c2c66affSColin Finck     }
1784c2c66affSColin Finck 
1785c2c66affSColin Finck     for (i = row + 1; i < num_rows; i++)
1786c2c66affSColin Finck     {
1787c2c66affSColin Finck         memcpy(tv->table->data[i - 1], tv->table->data[i], tv->row_size);
1788c2c66affSColin Finck         tv->table->data_persistent[i - 1] = tv->table->data_persistent[i];
1789c2c66affSColin Finck     }
1790c2c66affSColin Finck 
1791c2c66affSColin Finck     msi_free(tv->table->data[num_rows - 1]);
1792c2c66affSColin Finck 
1793c2c66affSColin Finck     return ERROR_SUCCESS;
1794c2c66affSColin Finck }
1795c2c66affSColin Finck 
1796c2c66affSColin Finck static UINT msi_table_update(struct tagMSIVIEW *view, MSIRECORD *rec, UINT row)
1797c2c66affSColin Finck {
1798c2c66affSColin Finck     MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1799c2c66affSColin Finck     UINT r, new_row;
1800c2c66affSColin Finck 
1801c2c66affSColin Finck     /* FIXME: MsiViewFetch should set rec index 0 to some ID that
1802c2c66affSColin Finck      * sets the fetched record apart from other records
1803c2c66affSColin Finck      */
1804c2c66affSColin Finck 
1805c2c66affSColin Finck     if (!tv->table)
1806c2c66affSColin Finck         return ERROR_INVALID_PARAMETER;
1807c2c66affSColin Finck 
1808c2c66affSColin Finck     r = msi_table_find_row(tv, rec, &new_row, NULL);
1809c2c66affSColin Finck     if (r != ERROR_SUCCESS)
1810c2c66affSColin Finck     {
1811c2c66affSColin Finck         ERR("can't find row to modify\n");
1812c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
1813c2c66affSColin Finck     }
1814c2c66affSColin Finck 
1815c2c66affSColin Finck     /* the row cannot be changed */
1816def8c91eSwinesync     if (row != new_row)
1817c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
1818c2c66affSColin Finck 
1819c2c66affSColin Finck     return TABLE_set_row(view, new_row, rec, (1 << tv->num_cols) - 1);
1820c2c66affSColin Finck }
1821c2c66affSColin Finck 
1822c2c66affSColin Finck static UINT msi_table_assign(struct tagMSIVIEW *view, MSIRECORD *rec)
1823c2c66affSColin Finck {
1824c2c66affSColin Finck     MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1825c2c66affSColin Finck     UINT r, row;
1826c2c66affSColin Finck 
1827c2c66affSColin Finck     if (!tv->table)
1828c2c66affSColin Finck         return ERROR_INVALID_PARAMETER;
1829c2c66affSColin Finck 
1830c2c66affSColin Finck     r = msi_table_find_row(tv, rec, &row, NULL);
1831c2c66affSColin Finck     if (r == ERROR_SUCCESS)
1832c2c66affSColin Finck         return TABLE_set_row(view, row, rec, (1 << tv->num_cols) - 1);
1833c2c66affSColin Finck     else
1834c2c66affSColin Finck         return TABLE_insert_row( view, rec, -1, FALSE );
1835c2c66affSColin Finck }
1836c2c66affSColin Finck 
1837c2c66affSColin Finck static UINT msi_refresh_record( struct tagMSIVIEW *view, MSIRECORD *rec, UINT row )
1838c2c66affSColin Finck {
1839c2c66affSColin Finck     MSIRECORD *curr;
1840c2c66affSColin Finck     UINT r, i, count;
1841c2c66affSColin Finck 
1842def8c91eSwinesync     r = TABLE_get_row(view, row, &curr);
1843c2c66affSColin Finck     if (r != ERROR_SUCCESS)
1844c2c66affSColin Finck         return r;
1845c2c66affSColin Finck 
1846c2c66affSColin Finck     /* Close the original record */
1847c2c66affSColin Finck     MSI_CloseRecord(&rec->hdr);
1848c2c66affSColin Finck 
1849c2c66affSColin Finck     count = MSI_RecordGetFieldCount(rec);
1850c2c66affSColin Finck     for (i = 0; i < count; i++)
1851c2c66affSColin Finck         MSI_RecordCopyField(curr, i + 1, rec, i + 1);
1852c2c66affSColin Finck 
1853c2c66affSColin Finck     msiobj_release(&curr->hdr);
1854c2c66affSColin Finck     return ERROR_SUCCESS;
1855c2c66affSColin Finck }
1856c2c66affSColin Finck 
1857c2c66affSColin Finck static UINT TABLE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
1858c2c66affSColin Finck                           MSIRECORD *rec, UINT row)
1859c2c66affSColin Finck {
1860c2c66affSColin Finck     MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1861c2c66affSColin Finck     UINT r, frow, column;
1862c2c66affSColin Finck 
1863c2c66affSColin Finck     TRACE("%p %d %p\n", view, eModifyMode, rec );
1864c2c66affSColin Finck 
1865c2c66affSColin Finck     switch (eModifyMode)
1866c2c66affSColin Finck     {
1867c2c66affSColin Finck     case MSIMODIFY_DELETE:
18685f1570f4Swinesync         r = TABLE_delete_row( view, row );
1869c2c66affSColin Finck         break;
1870c2c66affSColin Finck     case MSIMODIFY_VALIDATE_NEW:
1871c2c66affSColin Finck         r = table_validate_new( tv, rec, &column );
1872c2c66affSColin Finck         if (r != ERROR_SUCCESS)
1873c2c66affSColin Finck         {
1874c2c66affSColin Finck             tv->view.error = MSIDBERROR_DUPLICATEKEY;
1875c2c66affSColin Finck             tv->view.error_column = tv->columns[column].colname;
1876c2c66affSColin Finck             r = ERROR_INVALID_DATA;
1877c2c66affSColin Finck         }
1878c2c66affSColin Finck         break;
1879c2c66affSColin Finck 
1880c2c66affSColin Finck     case MSIMODIFY_INSERT:
1881c2c66affSColin Finck         r = table_validate_new( tv, rec, NULL );
1882c2c66affSColin Finck         if (r != ERROR_SUCCESS)
1883c2c66affSColin Finck             break;
1884c2c66affSColin Finck         r = TABLE_insert_row( view, rec, -1, FALSE );
1885c2c66affSColin Finck         break;
1886c2c66affSColin Finck 
1887c2c66affSColin Finck     case MSIMODIFY_INSERT_TEMPORARY:
1888c2c66affSColin Finck         r = table_validate_new( tv, rec, NULL );
1889c2c66affSColin Finck         if (r != ERROR_SUCCESS)
1890c2c66affSColin Finck             break;
1891c2c66affSColin Finck         r = TABLE_insert_row( view, rec, -1, TRUE );
1892c2c66affSColin Finck         break;
1893c2c66affSColin Finck 
1894c2c66affSColin Finck     case MSIMODIFY_REFRESH:
1895c2c66affSColin Finck         r = msi_refresh_record( view, rec, row );
1896c2c66affSColin Finck         break;
1897c2c66affSColin Finck 
1898c2c66affSColin Finck     case MSIMODIFY_UPDATE:
1899c2c66affSColin Finck         r = msi_table_update( view, rec, row );
1900c2c66affSColin Finck         break;
1901c2c66affSColin Finck 
1902c2c66affSColin Finck     case MSIMODIFY_ASSIGN:
1903c2c66affSColin Finck         r = msi_table_assign( view, rec );
1904c2c66affSColin Finck         break;
1905c2c66affSColin Finck 
1906c2c66affSColin Finck     case MSIMODIFY_MERGE:
1907c2c66affSColin Finck         /* check row that matches this record */
1908c2c66affSColin Finck         r = msi_table_find_row( tv, rec, &frow, &column );
1909c2c66affSColin Finck         if (r != ERROR_SUCCESS)
1910c2c66affSColin Finck         {
1911c2c66affSColin Finck             r = table_validate_new( tv, rec, NULL );
1912c2c66affSColin Finck             if (r == ERROR_SUCCESS)
1913c2c66affSColin Finck                 r = TABLE_insert_row( view, rec, -1, FALSE );
1914c2c66affSColin Finck         }
1915c2c66affSColin Finck         break;
1916c2c66affSColin Finck 
1917c2c66affSColin Finck     case MSIMODIFY_REPLACE:
1918c2c66affSColin Finck     case MSIMODIFY_VALIDATE:
1919c2c66affSColin Finck     case MSIMODIFY_VALIDATE_FIELD:
1920c2c66affSColin Finck     case MSIMODIFY_VALIDATE_DELETE:
1921c2c66affSColin Finck         FIXME("%p %d %p - mode not implemented\n", view, eModifyMode, rec );
1922c2c66affSColin Finck         r = ERROR_CALL_NOT_IMPLEMENTED;
1923c2c66affSColin Finck         break;
1924c2c66affSColin Finck 
1925c2c66affSColin Finck     default:
1926c2c66affSColin Finck         r = ERROR_INVALID_DATA;
1927c2c66affSColin Finck     }
1928c2c66affSColin Finck 
1929c2c66affSColin Finck     return r;
1930c2c66affSColin Finck }
1931c2c66affSColin Finck 
1932c2c66affSColin Finck static UINT TABLE_delete( struct tagMSIVIEW *view )
1933c2c66affSColin Finck {
1934c2c66affSColin Finck     MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1935c2c66affSColin Finck 
1936c2c66affSColin Finck     TRACE("%p\n", view );
1937c2c66affSColin Finck 
1938c2c66affSColin Finck     tv->table = NULL;
1939c2c66affSColin Finck     tv->columns = NULL;
1940c2c66affSColin Finck 
1941c2c66affSColin Finck     msi_free( tv );
1942c2c66affSColin Finck 
1943c2c66affSColin Finck     return ERROR_SUCCESS;
1944c2c66affSColin Finck }
1945c2c66affSColin Finck 
1946c2c66affSColin Finck static UINT TABLE_add_ref(struct tagMSIVIEW *view)
1947c2c66affSColin Finck {
1948c2c66affSColin Finck     MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1949c2c66affSColin Finck     UINT i;
1950c2c66affSColin Finck 
1951c2c66affSColin Finck     TRACE("%p %d\n", view, tv->table->ref_count);
1952c2c66affSColin Finck 
1953c2c66affSColin Finck     for (i = 0; i < tv->table->col_count; i++)
1954c2c66affSColin Finck     {
1955c2c66affSColin Finck         if (tv->table->colinfo[i].type & MSITYPE_TEMPORARY)
1956c2c66affSColin Finck             InterlockedIncrement(&tv->table->colinfo[i].ref_count);
1957c2c66affSColin Finck     }
1958c2c66affSColin Finck 
1959c2c66affSColin Finck     return InterlockedIncrement(&tv->table->ref_count);
1960c2c66affSColin Finck }
1961c2c66affSColin Finck 
1962c2c66affSColin Finck static UINT TABLE_remove_column(struct tagMSIVIEW *view, LPCWSTR table, UINT number)
1963c2c66affSColin Finck {
1964c2c66affSColin Finck     MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1965c2c66affSColin Finck     MSIRECORD *rec = NULL;
1966c2c66affSColin Finck     MSIVIEW *columns = NULL;
1967c2c66affSColin Finck     UINT row, r;
1968c2c66affSColin Finck 
1969c2c66affSColin Finck     rec = MSI_CreateRecord(2);
1970c2c66affSColin Finck     if (!rec)
1971c2c66affSColin Finck         return ERROR_OUTOFMEMORY;
1972c2c66affSColin Finck 
1973c2c66affSColin Finck     MSI_RecordSetStringW(rec, 1, table);
1974c2c66affSColin Finck     MSI_RecordSetInteger(rec, 2, number);
1975c2c66affSColin Finck 
1976c2c66affSColin Finck     r = TABLE_CreateView(tv->db, szColumns, &columns);
1977c2c66affSColin Finck     if (r != ERROR_SUCCESS)
1978c2c66affSColin Finck     {
1979c2c66affSColin Finck         msiobj_release(&rec->hdr);
1980c2c66affSColin Finck         return r;
1981c2c66affSColin Finck     }
1982c2c66affSColin Finck 
1983c2c66affSColin Finck     r = msi_table_find_row((MSITABLEVIEW *)columns, rec, &row, NULL);
1984c2c66affSColin Finck     if (r != ERROR_SUCCESS)
1985c2c66affSColin Finck         goto done;
1986c2c66affSColin Finck 
1987c2c66affSColin Finck     r = TABLE_delete_row(columns, row);
1988c2c66affSColin Finck     if (r != ERROR_SUCCESS)
1989c2c66affSColin Finck         goto done;
1990c2c66affSColin Finck 
1991c2c66affSColin Finck     msi_update_table_columns(tv->db, table);
1992c2c66affSColin Finck 
1993c2c66affSColin Finck done:
1994c2c66affSColin Finck     msiobj_release(&rec->hdr);
1995c2c66affSColin Finck     columns->ops->delete(columns);
1996c2c66affSColin Finck     return r;
1997c2c66affSColin Finck }
1998c2c66affSColin Finck 
1999c2c66affSColin Finck static UINT TABLE_release(struct tagMSIVIEW *view)
2000c2c66affSColin Finck {
2001c2c66affSColin Finck     MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
2002c2c66affSColin Finck     INT ref = tv->table->ref_count;
2003c2c66affSColin Finck     UINT i, r;
2004c2c66affSColin Finck 
2005c2c66affSColin Finck     TRACE("%p %d\n", view, ref);
2006c2c66affSColin Finck 
2007c2c66affSColin Finck     for (i = 0; i < tv->table->col_count; i++)
2008c2c66affSColin Finck     {
2009c2c66affSColin Finck         if (tv->table->colinfo[i].type & MSITYPE_TEMPORARY)
2010c2c66affSColin Finck         {
2011c2c66affSColin Finck             ref = InterlockedDecrement(&tv->table->colinfo[i].ref_count);
2012c2c66affSColin Finck             if (ref == 0)
2013c2c66affSColin Finck             {
2014c2c66affSColin Finck                 r = TABLE_remove_column(view, tv->table->colinfo[i].tablename,
2015c2c66affSColin Finck                                         tv->table->colinfo[i].number);
2016c2c66affSColin Finck                 if (r != ERROR_SUCCESS)
2017c2c66affSColin Finck                     break;
2018c2c66affSColin Finck             }
2019c2c66affSColin Finck         }
2020c2c66affSColin Finck     }
2021c2c66affSColin Finck 
2022c2c66affSColin Finck     ref = InterlockedDecrement(&tv->table->ref_count);
2023c2c66affSColin Finck     if (ref == 0)
2024c2c66affSColin Finck     {
2025c2c66affSColin Finck         if (!tv->table->row_count)
2026c2c66affSColin Finck         {
2027c2c66affSColin Finck             list_remove(&tv->table->entry);
2028c2c66affSColin Finck             free_table(tv->table);
2029c2c66affSColin Finck             TABLE_delete(view);
2030c2c66affSColin Finck         }
2031c2c66affSColin Finck     }
2032c2c66affSColin Finck 
2033c2c66affSColin Finck     return ref;
2034c2c66affSColin Finck }
2035c2c66affSColin Finck 
2036c2c66affSColin Finck static UINT TABLE_add_column(struct tagMSIVIEW *view, LPCWSTR table, UINT number,
2037c2c66affSColin Finck                              LPCWSTR column, UINT type, BOOL hold)
2038c2c66affSColin Finck {
2039c2c66affSColin Finck     MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
2040c2c66affSColin Finck     MSITABLE *msitable;
2041c2c66affSColin Finck     MSIRECORD *rec;
2042c2c66affSColin Finck     UINT r, i;
2043c2c66affSColin Finck 
2044c2c66affSColin Finck     rec = MSI_CreateRecord(4);
2045c2c66affSColin Finck     if (!rec)
2046c2c66affSColin Finck         return ERROR_OUTOFMEMORY;
2047c2c66affSColin Finck 
2048c2c66affSColin Finck     MSI_RecordSetStringW(rec, 1, table);
2049c2c66affSColin Finck     MSI_RecordSetInteger(rec, 2, number);
2050c2c66affSColin Finck     MSI_RecordSetStringW(rec, 3, column);
2051c2c66affSColin Finck     MSI_RecordSetInteger(rec, 4, type);
2052c2c66affSColin Finck 
2053c2c66affSColin Finck     r = TABLE_insert_row(&tv->view, rec, -1, FALSE);
2054c2c66affSColin Finck     if (r != ERROR_SUCCESS)
2055c2c66affSColin Finck         goto done;
2056c2c66affSColin Finck 
2057c2c66affSColin Finck     msi_update_table_columns(tv->db, table);
2058c2c66affSColin Finck 
2059c2c66affSColin Finck     if (!hold)
2060c2c66affSColin Finck         goto done;
2061c2c66affSColin Finck 
2062c2c66affSColin Finck     msitable = find_cached_table(tv->db, table);
2063c2c66affSColin Finck     for (i = 0; i < msitable->col_count; i++)
2064c2c66affSColin Finck     {
2065958f1addSwinesync         if (!wcscmp( msitable->colinfo[i].colname, column ))
2066c2c66affSColin Finck         {
2067c2c66affSColin Finck             InterlockedIncrement(&msitable->colinfo[i].ref_count);
2068c2c66affSColin Finck             break;
2069c2c66affSColin Finck         }
2070c2c66affSColin Finck     }
2071c2c66affSColin Finck 
2072c2c66affSColin Finck done:
2073c2c66affSColin Finck     msiobj_release(&rec->hdr);
2074c2c66affSColin Finck     return r;
2075c2c66affSColin Finck }
2076c2c66affSColin Finck 
2077c2c66affSColin Finck static UINT TABLE_drop(struct tagMSIVIEW *view)
2078c2c66affSColin Finck {
2079c2c66affSColin Finck     MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
2080c2c66affSColin Finck     MSIVIEW *tables = NULL;
2081c2c66affSColin Finck     MSIRECORD *rec = NULL;
2082c2c66affSColin Finck     UINT r, row;
2083c2c66affSColin Finck     INT i;
2084c2c66affSColin Finck 
2085c2c66affSColin Finck     TRACE("dropping table %s\n", debugstr_w(tv->name));
2086c2c66affSColin Finck 
2087c2c66affSColin Finck     for (i = tv->table->col_count - 1; i >= 0; i--)
2088c2c66affSColin Finck     {
2089c2c66affSColin Finck         r = TABLE_remove_column(view, tv->table->colinfo[i].tablename,
2090c2c66affSColin Finck                                 tv->table->colinfo[i].number);
2091c2c66affSColin Finck         if (r != ERROR_SUCCESS)
2092c2c66affSColin Finck             return r;
2093c2c66affSColin Finck     }
2094c2c66affSColin Finck 
2095c2c66affSColin Finck     rec = MSI_CreateRecord(1);
2096c2c66affSColin Finck     if (!rec)
2097c2c66affSColin Finck         return ERROR_OUTOFMEMORY;
2098c2c66affSColin Finck 
2099c2c66affSColin Finck     MSI_RecordSetStringW(rec, 1, tv->name);
2100c2c66affSColin Finck 
2101c2c66affSColin Finck     r = TABLE_CreateView(tv->db, szTables, &tables);
2102c2c66affSColin Finck     if (r != ERROR_SUCCESS)
2103c2c66affSColin Finck     {
2104c2c66affSColin Finck         msiobj_release(&rec->hdr);
2105c2c66affSColin Finck         return r;
2106c2c66affSColin Finck     }
2107c2c66affSColin Finck 
2108c2c66affSColin Finck     r = msi_table_find_row((MSITABLEVIEW *)tables, rec, &row, NULL);
2109c2c66affSColin Finck     if (r != ERROR_SUCCESS)
2110c2c66affSColin Finck         goto done;
2111c2c66affSColin Finck 
2112c2c66affSColin Finck     r = TABLE_delete_row(tables, row);
2113c2c66affSColin Finck     if (r != ERROR_SUCCESS)
2114c2c66affSColin Finck         goto done;
2115c2c66affSColin Finck 
2116c2c66affSColin Finck     list_remove(&tv->table->entry);
2117c2c66affSColin Finck     free_table(tv->table);
2118c2c66affSColin Finck 
2119c2c66affSColin Finck done:
2120c2c66affSColin Finck     msiobj_release(&rec->hdr);
2121c2c66affSColin Finck     tables->ops->delete(tables);
2122c2c66affSColin Finck 
2123c2c66affSColin Finck     return r;
2124c2c66affSColin Finck }
2125c2c66affSColin Finck 
2126c2c66affSColin Finck static const MSIVIEWOPS table_ops =
2127c2c66affSColin Finck {
2128c2c66affSColin Finck     TABLE_fetch_int,
2129c2c66affSColin Finck     TABLE_fetch_stream,
213097918c36Swinesync     TABLE_set_int,
213197918c36Swinesync     TABLE_set_string,
2132580d3e1bSwinesync     TABLE_set_stream,
2133c2c66affSColin Finck     TABLE_set_row,
2134c2c66affSColin Finck     TABLE_insert_row,
2135c2c66affSColin Finck     TABLE_delete_row,
2136c2c66affSColin Finck     TABLE_execute,
2137c2c66affSColin Finck     TABLE_close,
2138c2c66affSColin Finck     TABLE_get_dimensions,
2139c2c66affSColin Finck     TABLE_get_column_info,
2140c2c66affSColin Finck     TABLE_modify,
2141c2c66affSColin Finck     TABLE_delete,
2142c2c66affSColin Finck     TABLE_add_ref,
2143c2c66affSColin Finck     TABLE_release,
2144c2c66affSColin Finck     TABLE_add_column,
2145c2c66affSColin Finck     NULL,
2146c2c66affSColin Finck     TABLE_drop,
2147c2c66affSColin Finck };
2148c2c66affSColin Finck 
2149c2c66affSColin Finck UINT TABLE_CreateView( MSIDATABASE *db, LPCWSTR name, MSIVIEW **view )
2150c2c66affSColin Finck {
2151c2c66affSColin Finck     MSITABLEVIEW *tv ;
2152c2c66affSColin Finck     UINT r, sz;
2153c2c66affSColin Finck 
2154c2c66affSColin Finck     TRACE("%p %s %p\n", db, debugstr_w(name), view );
2155c2c66affSColin Finck 
2156958f1addSwinesync     if ( !wcscmp( name, szStreams ) )
2157c2c66affSColin Finck         return STREAMS_CreateView( db, view );
2158958f1addSwinesync     else if ( !wcscmp( name, szStorages ) )
2159c2c66affSColin Finck         return STORAGES_CreateView( db, view );
2160c2c66affSColin Finck 
2161c2c66affSColin Finck     sz = FIELD_OFFSET( MSITABLEVIEW, name[lstrlenW( name ) + 1] );
2162c2c66affSColin Finck     tv = msi_alloc_zero( sz );
2163c2c66affSColin Finck     if( !tv )
2164c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
2165c2c66affSColin Finck 
2166c2c66affSColin Finck     r = get_table( db, name, &tv->table );
2167c2c66affSColin Finck     if( r != ERROR_SUCCESS )
2168c2c66affSColin Finck     {
2169c2c66affSColin Finck         msi_free( tv );
2170c2c66affSColin Finck         WARN("table not found\n");
2171c2c66affSColin Finck         return r;
2172c2c66affSColin Finck     }
2173c2c66affSColin Finck 
2174c2c66affSColin Finck     TRACE("table %p found with %d columns\n", tv->table, tv->table->col_count);
2175c2c66affSColin Finck 
2176c2c66affSColin Finck     /* fill the structure */
2177c2c66affSColin Finck     tv->view.ops = &table_ops;
2178c2c66affSColin Finck     tv->db = db;
2179c2c66affSColin Finck     tv->columns = tv->table->colinfo;
2180c2c66affSColin Finck     tv->num_cols = tv->table->col_count;
2181c2c66affSColin Finck     tv->row_size = msi_table_get_row_size( db, tv->table->colinfo, tv->table->col_count, LONG_STR_BYTES );
2182c2c66affSColin Finck 
2183c2c66affSColin Finck     TRACE("%s one row is %d bytes\n", debugstr_w(name), tv->row_size );
2184c2c66affSColin Finck 
2185c2c66affSColin Finck     *view = (MSIVIEW*) tv;
2186c2c66affSColin Finck     lstrcpyW( tv->name, name );
2187c2c66affSColin Finck 
2188c2c66affSColin Finck     return ERROR_SUCCESS;
2189c2c66affSColin Finck }
2190c2c66affSColin Finck 
2191317c1f6cSwinesync static WCHAR* create_key_string(MSITABLEVIEW *tv, MSIRECORD *rec)
2192317c1f6cSwinesync {
2193317c1f6cSwinesync     DWORD i, p, len, key_len = 0;
2194317c1f6cSwinesync     WCHAR *key;
2195317c1f6cSwinesync 
2196317c1f6cSwinesync     for (i = 0; i < tv->num_cols; i++)
2197317c1f6cSwinesync     {
2198317c1f6cSwinesync         if (!(tv->columns[i].type & MSITYPE_KEY))
2199317c1f6cSwinesync             continue;
2200317c1f6cSwinesync         if (MSI_RecordGetStringW( rec, i+1, NULL, &len ) == ERROR_SUCCESS)
2201317c1f6cSwinesync             key_len += len;
2202317c1f6cSwinesync         key_len++;
2203317c1f6cSwinesync     }
2204317c1f6cSwinesync 
2205317c1f6cSwinesync     key = msi_alloc( key_len * sizeof(WCHAR) );
2206317c1f6cSwinesync     if(!key)
2207317c1f6cSwinesync         return NULL;
2208317c1f6cSwinesync 
2209317c1f6cSwinesync     p = 0;
2210317c1f6cSwinesync     for (i = 0; i < tv->num_cols; i++)
2211317c1f6cSwinesync     {
2212317c1f6cSwinesync         if (!(tv->columns[i].type & MSITYPE_KEY))
2213317c1f6cSwinesync             continue;
2214317c1f6cSwinesync         if (p)
2215317c1f6cSwinesync             key[p++] = '\t';
2216317c1f6cSwinesync         len = key_len - p;
2217317c1f6cSwinesync         if (MSI_RecordGetStringW( rec, i+1, key + p, &len ) == ERROR_SUCCESS)
2218317c1f6cSwinesync             p += len;
2219317c1f6cSwinesync     }
2220317c1f6cSwinesync     return key;
2221317c1f6cSwinesync }
2222317c1f6cSwinesync 
22238e75cb8aSwinesync static UINT msi_record_stream_name( const MSITABLEVIEW *tv, MSIRECORD *rec, LPWSTR name, UINT *len )
22248e75cb8aSwinesync {
22258e75cb8aSwinesync     UINT p = 0, l, i, r;
22268e75cb8aSwinesync 
22278e75cb8aSwinesync     l = wcslen( tv->name );
22288e75cb8aSwinesync     if (name && *len > l)
22298e75cb8aSwinesync         memcpy(name, tv->name, l * sizeof(WCHAR));
22308e75cb8aSwinesync     p += l;
22318e75cb8aSwinesync 
22328e75cb8aSwinesync     for ( i = 0; i < tv->num_cols; i++ )
22338e75cb8aSwinesync     {
22348e75cb8aSwinesync         if (!(tv->columns[i].type & MSITYPE_KEY))
22358e75cb8aSwinesync             continue;
22368e75cb8aSwinesync 
22378e75cb8aSwinesync         if (name && *len > p + 1)
22388e75cb8aSwinesync             name[p] = '.';
22398e75cb8aSwinesync         p++;
22408e75cb8aSwinesync 
22418e75cb8aSwinesync         l = (*len > p ? *len - p : 0);
22428e75cb8aSwinesync         r = MSI_RecordGetStringW( rec, i + 1, name ? name + p : NULL, &l );
22438e75cb8aSwinesync         if (r != ERROR_SUCCESS)
22448e75cb8aSwinesync             return r;
22458e75cb8aSwinesync         p += l;
22468e75cb8aSwinesync     }
22478e75cb8aSwinesync 
22488e75cb8aSwinesync     if (name && *len > p)
22498e75cb8aSwinesync         name[p] = 0;
22508e75cb8aSwinesync 
22518e75cb8aSwinesync     *len = p;
22528e75cb8aSwinesync     return ERROR_SUCCESS;
22538e75cb8aSwinesync }
22548e75cb8aSwinesync 
22551bbb87bbSwinesync static UINT TransformView_fetch_int( MSIVIEW *view, UINT row, UINT col, UINT *val )
22561bbb87bbSwinesync {
2257*27a430bdSwinesync     MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
2258*27a430bdSwinesync 
2259*27a430bdSwinesync     if (!tv->table)
2260*27a430bdSwinesync     {
2261*27a430bdSwinesync         *val = 0;
2262*27a430bdSwinesync         return ERROR_SUCCESS;
2263*27a430bdSwinesync     }
2264317c1f6cSwinesync     return TABLE_fetch_int( view, row, col, val );
22651bbb87bbSwinesync }
22661bbb87bbSwinesync 
22671bbb87bbSwinesync static UINT TransformView_fetch_stream( MSIVIEW *view, UINT row, UINT col, IStream **stm )
22681bbb87bbSwinesync {
2269*27a430bdSwinesync     MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
2270*27a430bdSwinesync 
2271*27a430bdSwinesync     if (!tv->table)
2272*27a430bdSwinesync     {
2273*27a430bdSwinesync         *stm = NULL;
2274*27a430bdSwinesync         return ERROR_SUCCESS;
2275*27a430bdSwinesync     }
2276317c1f6cSwinesync     return TABLE_fetch_stream( view, row, col, stm );
22771bbb87bbSwinesync }
22781bbb87bbSwinesync 
22791bbb87bbSwinesync static UINT TransformView_set_row( MSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask )
22801bbb87bbSwinesync {
2281317c1f6cSwinesync     static const WCHAR query_pfx[] =
2282317c1f6cSwinesync         L"INSERT INTO `_TransformView` (`Table`, `Column`, `Row`, `Data`, `Current`) VALUES ('";
2283317c1f6cSwinesync 
2284317c1f6cSwinesync     MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
2285317c1f6cSwinesync     WCHAR buf[256], *query = buf;
2286317c1f6cSwinesync     MSIRECORD *old_rec;
2287317c1f6cSwinesync     MSIQUERY *q;
2288317c1f6cSwinesync     WCHAR *key;
2289317c1f6cSwinesync     UINT i, p, r, len, qlen;
2290317c1f6cSwinesync 
2291317c1f6cSwinesync     if (!wcscmp( tv->name, szColumns ))
2292317c1f6cSwinesync     {
2293317c1f6cSwinesync         ERR( "trying to modify existing column\n" );
2294317c1f6cSwinesync         return ERROR_INSTALL_TRANSFORM_FAILURE;
2295317c1f6cSwinesync     }
2296317c1f6cSwinesync 
2297317c1f6cSwinesync     if (!wcscmp( tv->name, szTables ))
2298317c1f6cSwinesync     {
2299317c1f6cSwinesync         ERR( "trying to modify existing table\n" );
2300317c1f6cSwinesync         return ERROR_INSTALL_TRANSFORM_FAILURE;
2301317c1f6cSwinesync     }
2302317c1f6cSwinesync 
2303317c1f6cSwinesync     key = create_key_string( tv, rec );
2304317c1f6cSwinesync     if (!key)
2305317c1f6cSwinesync         return ERROR_OUTOFMEMORY;
2306317c1f6cSwinesync 
2307317c1f6cSwinesync     r = msi_view_get_row( tv->db, view, row, &old_rec );
2308317c1f6cSwinesync     if (r != ERROR_SUCCESS)
2309317c1f6cSwinesync         old_rec = NULL;
2310317c1f6cSwinesync 
2311317c1f6cSwinesync     for (i = 0; i < tv->num_cols; i++)
2312317c1f6cSwinesync     {
2313317c1f6cSwinesync         if (!(mask & (1 << i)))
2314317c1f6cSwinesync             continue;
2315317c1f6cSwinesync         if (tv->columns[i].type & MSITYPE_KEY)
2316317c1f6cSwinesync             continue;
2317317c1f6cSwinesync 
2318317c1f6cSwinesync         qlen = p = wcslen( query_pfx );
2319317c1f6cSwinesync         qlen += wcslen( tv->name ) + 3; /* strlen("','") */
2320317c1f6cSwinesync         qlen += wcslen( tv->columns[i].colname ) + 3;
2321317c1f6cSwinesync         qlen += wcslen( key ) + 3;
2322317c1f6cSwinesync         if (MSITYPE_IS_BINARY( tv->columns[i].type ))
2323317c1f6cSwinesync             r = msi_record_stream_name( tv, rec, NULL, &len );
2324317c1f6cSwinesync         else
2325317c1f6cSwinesync             r = MSI_RecordGetStringW( rec, i + 1, NULL, &len );
2326317c1f6cSwinesync         if (r != ERROR_SUCCESS)
2327317c1f6cSwinesync         {
2328317c1f6cSwinesync             if (old_rec)
2329317c1f6cSwinesync                 msiobj_release( &old_rec->hdr );
2330317c1f6cSwinesync             msi_free( key );
2331317c1f6cSwinesync             return r;
2332317c1f6cSwinesync         }
2333317c1f6cSwinesync         qlen += len + 3;
2334317c1f6cSwinesync         if (old_rec && (r = MSI_RecordGetStringW( old_rec, i+1, NULL, &len )))
2335317c1f6cSwinesync         {
2336317c1f6cSwinesync             msiobj_release( &old_rec->hdr );
2337317c1f6cSwinesync             msi_free( key );
2338317c1f6cSwinesync             return r;
2339317c1f6cSwinesync         }
2340317c1f6cSwinesync         qlen += len + 3; /* strlen("')") + 1 */
2341317c1f6cSwinesync 
2342317c1f6cSwinesync         if (qlen > ARRAY_SIZE(buf))
2343317c1f6cSwinesync         {
2344317c1f6cSwinesync             query = msi_alloc( qlen * sizeof(WCHAR) );
2345317c1f6cSwinesync             if (!query)
2346317c1f6cSwinesync             {
2347317c1f6cSwinesync                 if (old_rec)
2348317c1f6cSwinesync                     msiobj_release( &old_rec->hdr );
2349317c1f6cSwinesync                 msi_free( key );
2350317c1f6cSwinesync                 return ERROR_OUTOFMEMORY;
2351317c1f6cSwinesync             }
2352317c1f6cSwinesync         }
2353317c1f6cSwinesync 
2354317c1f6cSwinesync         memcpy( query, query_pfx, p * sizeof(WCHAR) );
2355317c1f6cSwinesync         len = wcslen( tv->name );
2356317c1f6cSwinesync         memcpy( query + p, tv->name, len * sizeof(WCHAR) );
2357317c1f6cSwinesync         p += len;
2358317c1f6cSwinesync         query[p++] = '\'';
2359317c1f6cSwinesync         query[p++] = ',';
2360317c1f6cSwinesync         query[p++] = '\'';
2361317c1f6cSwinesync         len = wcslen( tv->columns[i].colname );
2362317c1f6cSwinesync         memcpy( query + p, tv->columns[i].colname, len * sizeof(WCHAR) );
2363317c1f6cSwinesync         p += len;
2364317c1f6cSwinesync         query[p++] = '\'';
2365317c1f6cSwinesync         query[p++] = ',';
2366317c1f6cSwinesync         query[p++] = '\'';
2367317c1f6cSwinesync         len = wcslen( key );
2368317c1f6cSwinesync         memcpy( query + p, key, len * sizeof(WCHAR) );
2369317c1f6cSwinesync         p += len;
2370317c1f6cSwinesync         query[p++] = '\'';
2371317c1f6cSwinesync         query[p++] = ',';
2372317c1f6cSwinesync         query[p++] = '\'';
2373317c1f6cSwinesync         len = qlen - p;
2374317c1f6cSwinesync         if (MSITYPE_IS_BINARY( tv->columns[i].type ))
2375317c1f6cSwinesync             msi_record_stream_name( tv, rec, query + p, &len );
2376317c1f6cSwinesync         else
2377317c1f6cSwinesync             MSI_RecordGetStringW( rec, i + 1, query + p, &len );
2378317c1f6cSwinesync         p += len;
2379317c1f6cSwinesync         query[p++] = '\'';
2380317c1f6cSwinesync         query[p++] = ',';
2381317c1f6cSwinesync         query[p++] = '\'';
2382317c1f6cSwinesync         if (old_rec)
2383317c1f6cSwinesync         {
2384317c1f6cSwinesync             len = qlen - p;
2385317c1f6cSwinesync             MSI_RecordGetStringW( old_rec, i + 1, query + p, &len );
2386317c1f6cSwinesync             p += len;
2387317c1f6cSwinesync         }
2388317c1f6cSwinesync         query[p++] = '\'';
2389317c1f6cSwinesync         query[p++] = ')';
2390317c1f6cSwinesync         query[p++] = 0;
2391317c1f6cSwinesync 
2392317c1f6cSwinesync         r = MSI_DatabaseOpenViewW( tv->db, query, &q );
2393317c1f6cSwinesync         if (query != buf)
2394317c1f6cSwinesync             msi_free( query );
2395317c1f6cSwinesync         if (r != ERROR_SUCCESS)
2396317c1f6cSwinesync         {
2397317c1f6cSwinesync             if (old_rec)
2398317c1f6cSwinesync                 msiobj_release( &old_rec->hdr );
2399317c1f6cSwinesync             msi_free( key );
2400317c1f6cSwinesync             return r;
2401317c1f6cSwinesync         }
2402317c1f6cSwinesync 
2403317c1f6cSwinesync         r = MSI_ViewExecute( q, NULL );
2404317c1f6cSwinesync         msiobj_release( &q->hdr );
2405317c1f6cSwinesync         if (r != ERROR_SUCCESS)
2406317c1f6cSwinesync         {
2407317c1f6cSwinesync             if (old_rec)
2408317c1f6cSwinesync                 msiobj_release( &old_rec->hdr );
2409317c1f6cSwinesync             msi_free( key );
2410317c1f6cSwinesync             return r;
2411317c1f6cSwinesync         }
2412317c1f6cSwinesync     }
2413317c1f6cSwinesync 
2414317c1f6cSwinesync     if (old_rec)
2415317c1f6cSwinesync         msiobj_release( &old_rec->hdr );
2416317c1f6cSwinesync     msi_free( key );
2417317c1f6cSwinesync     return ERROR_SUCCESS;
24181bbb87bbSwinesync }
24191bbb87bbSwinesync 
2420*27a430bdSwinesync static UINT TransformView_create_table( MSITABLEVIEW *tv, MSIRECORD *rec )
2421*27a430bdSwinesync {
2422*27a430bdSwinesync     static const WCHAR query_fmt[] =
2423*27a430bdSwinesync         L"INSERT INTO `_TransformView` (`Table`, `Column`) VALUES ('%s', 'CREATE')";
2424*27a430bdSwinesync 
2425*27a430bdSwinesync     WCHAR buf[256], *query = buf;
2426*27a430bdSwinesync     const WCHAR *name;
2427*27a430bdSwinesync     MSIQUERY *q;
2428*27a430bdSwinesync     int len;
2429*27a430bdSwinesync     UINT r;
2430*27a430bdSwinesync 
2431*27a430bdSwinesync     name = msi_record_get_string( rec, 1, &len );
2432*27a430bdSwinesync     if (!name)
2433*27a430bdSwinesync         return ERROR_INSTALL_TRANSFORM_FAILURE;
2434*27a430bdSwinesync 
2435*27a430bdSwinesync     len = _snwprintf( NULL, 0, query_fmt, name ) + 1;
2436*27a430bdSwinesync     if (len > ARRAY_SIZE(buf))
2437*27a430bdSwinesync     {
2438*27a430bdSwinesync         query = msi_alloc( len * sizeof(WCHAR) );
2439*27a430bdSwinesync         if (!query)
2440*27a430bdSwinesync             return ERROR_OUTOFMEMORY;
2441*27a430bdSwinesync     }
2442*27a430bdSwinesync     swprintf( query, len, query_fmt, name );
2443*27a430bdSwinesync 
2444*27a430bdSwinesync     r = MSI_DatabaseOpenViewW( tv->db, query, &q );
2445*27a430bdSwinesync     if (query != buf)
2446*27a430bdSwinesync         msi_free( query );
2447*27a430bdSwinesync     if (r != ERROR_SUCCESS)
2448*27a430bdSwinesync         return r;
2449*27a430bdSwinesync 
2450*27a430bdSwinesync     r = MSI_ViewExecute( q, NULL );
2451*27a430bdSwinesync     msiobj_release( &q->hdr );
2452*27a430bdSwinesync     return r;
2453*27a430bdSwinesync }
2454*27a430bdSwinesync 
24551bbb87bbSwinesync static UINT TransformView_insert_row( MSIVIEW *view, MSIRECORD *rec, UINT row, BOOL temporary )
24561bbb87bbSwinesync {
2457*27a430bdSwinesync     MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
2458*27a430bdSwinesync 
2459*27a430bdSwinesync     if (!wcscmp(tv->name, szTables))
2460*27a430bdSwinesync         return TransformView_create_table( tv, rec );
2461*27a430bdSwinesync 
24621bbb87bbSwinesync     FIXME("\n");
24631bbb87bbSwinesync     return ERROR_CALL_NOT_IMPLEMENTED;
24641bbb87bbSwinesync }
24651bbb87bbSwinesync 
24661bbb87bbSwinesync static UINT TransformView_delete_row( MSIVIEW *view, UINT row )
24671bbb87bbSwinesync {
24681bbb87bbSwinesync     FIXME("\n");
24691bbb87bbSwinesync     return ERROR_CALL_NOT_IMPLEMENTED;
24701bbb87bbSwinesync }
24711bbb87bbSwinesync 
24721bbb87bbSwinesync static UINT TransformView_execute( MSIVIEW *view, MSIRECORD *record )
24731bbb87bbSwinesync {
24741bbb87bbSwinesync     return ERROR_SUCCESS;
24751bbb87bbSwinesync }
24761bbb87bbSwinesync 
24771bbb87bbSwinesync static UINT TransformView_close( MSIVIEW *view )
24781bbb87bbSwinesync {
24791bbb87bbSwinesync     return ERROR_SUCCESS;
24801bbb87bbSwinesync }
24811bbb87bbSwinesync 
24821bbb87bbSwinesync static UINT TransformView_get_dimensions( MSIVIEW *view, UINT *rows, UINT *cols )
24831bbb87bbSwinesync {
2484317c1f6cSwinesync     return TABLE_get_dimensions( view, rows, cols );
24851bbb87bbSwinesync }
24861bbb87bbSwinesync 
24871bbb87bbSwinesync static UINT TransformView_get_column_info( MSIVIEW *view, UINT n, LPCWSTR *name, UINT *type,
24881bbb87bbSwinesync                              BOOL *temporary, LPCWSTR *table_name )
24891bbb87bbSwinesync {
2490317c1f6cSwinesync     return TABLE_get_column_info( view, n, name, type, temporary, table_name );
24911bbb87bbSwinesync }
24921bbb87bbSwinesync 
24931bbb87bbSwinesync static UINT TransformView_delete( MSIVIEW *view )
24941bbb87bbSwinesync {
24951bbb87bbSwinesync     return TABLE_delete( view );
24961bbb87bbSwinesync }
24971bbb87bbSwinesync 
24981bbb87bbSwinesync static const MSIVIEWOPS transform_view_ops =
24991bbb87bbSwinesync {
25001bbb87bbSwinesync     TransformView_fetch_int,
25011bbb87bbSwinesync     TransformView_fetch_stream,
25021bbb87bbSwinesync     NULL,
25031bbb87bbSwinesync     NULL,
25041bbb87bbSwinesync     NULL,
25051bbb87bbSwinesync     TransformView_set_row,
25061bbb87bbSwinesync     TransformView_insert_row,
25071bbb87bbSwinesync     TransformView_delete_row,
25081bbb87bbSwinesync     TransformView_execute,
25091bbb87bbSwinesync     TransformView_close,
25101bbb87bbSwinesync     TransformView_get_dimensions,
25111bbb87bbSwinesync     TransformView_get_column_info,
25121bbb87bbSwinesync     NULL,
25131bbb87bbSwinesync     TransformView_delete,
25141bbb87bbSwinesync     NULL,
25151bbb87bbSwinesync     NULL,
25161bbb87bbSwinesync     NULL,
25171bbb87bbSwinesync     NULL,
25181bbb87bbSwinesync     NULL
25191bbb87bbSwinesync };
25201bbb87bbSwinesync 
25211bbb87bbSwinesync UINT TransformView_Create( MSIDATABASE *db, string_table *st, LPCWSTR name, MSIVIEW **view )
25221bbb87bbSwinesync {
2523*27a430bdSwinesync     UINT r, name_len, size;
25241bbb87bbSwinesync     MSITABLEVIEW *tv;
2525*27a430bdSwinesync 
2526*27a430bdSwinesync     name_len = wcslen( name );
25271bbb87bbSwinesync 
25281bbb87bbSwinesync     r = TABLE_CreateView( db, name, view );
25291bbb87bbSwinesync     if (r == ERROR_INVALID_PARAMETER)
25301bbb87bbSwinesync     {
25311bbb87bbSwinesync         /* table does not exist */
2532*27a430bdSwinesync         size = FIELD_OFFSET( MSITABLEVIEW, name[name_len + 1] );
2533*27a430bdSwinesync         tv = msi_alloc_zero( size );
2534*27a430bdSwinesync         if (!tv)
2535*27a430bdSwinesync             return ERROR_OUTOFMEMORY;
2536*27a430bdSwinesync 
2537*27a430bdSwinesync         tv->db = db;
2538*27a430bdSwinesync         memcpy( tv->name, name, name_len * sizeof(WCHAR) );
2539*27a430bdSwinesync         *view = (MSIVIEW*)tv;
25401bbb87bbSwinesync     }
25411bbb87bbSwinesync     else if (r != ERROR_SUCCESS)
25421bbb87bbSwinesync     {
25431bbb87bbSwinesync         return r;
25441bbb87bbSwinesync     }
25451bbb87bbSwinesync     else
25461bbb87bbSwinesync     {
25471bbb87bbSwinesync         tv = (MSITABLEVIEW*)*view;
25481bbb87bbSwinesync     }
25491bbb87bbSwinesync 
25501bbb87bbSwinesync     tv->view.ops = &transform_view_ops;
25511bbb87bbSwinesync     return ERROR_SUCCESS;
25521bbb87bbSwinesync }
25531bbb87bbSwinesync 
2554c2c66affSColin Finck UINT MSI_CommitTables( MSIDATABASE *db )
2555c2c66affSColin Finck {
2556c2c66affSColin Finck     UINT r, bytes_per_strref;
2557c2c66affSColin Finck     HRESULT hr;
2558c2c66affSColin Finck     MSITABLE *table = NULL;
2559c2c66affSColin Finck 
2560c2c66affSColin Finck     TRACE("%p\n",db);
2561c2c66affSColin Finck 
2562c2c66affSColin Finck     r = msi_save_string_table( db->strings, db->storage, &bytes_per_strref );
2563c2c66affSColin Finck     if( r != ERROR_SUCCESS )
2564c2c66affSColin Finck     {
2565c2c66affSColin Finck         WARN("failed to save string table r=%08x\n",r);
2566c2c66affSColin Finck         return r;
2567c2c66affSColin Finck     }
2568c2c66affSColin Finck 
2569c2c66affSColin Finck     LIST_FOR_EACH_ENTRY( table, &db->tables, MSITABLE, entry )
2570c2c66affSColin Finck     {
2571c2c66affSColin Finck         r = save_table( db, table, bytes_per_strref );
2572c2c66affSColin Finck         if( r != ERROR_SUCCESS )
2573c2c66affSColin Finck         {
2574c2c66affSColin Finck             WARN("failed to save table %s (r=%08x)\n",
2575c2c66affSColin Finck                   debugstr_w(table->name), r);
2576c2c66affSColin Finck             return r;
2577c2c66affSColin Finck         }
2578c2c66affSColin Finck     }
2579c2c66affSColin Finck 
2580c2c66affSColin Finck     hr = IStorage_Commit( db->storage, 0 );
2581c2c66affSColin Finck     if (FAILED( hr ))
2582c2c66affSColin Finck     {
2583c2c66affSColin Finck         WARN("failed to commit changes 0x%08x\n", hr);
2584c2c66affSColin Finck         r = ERROR_FUNCTION_FAILED;
2585c2c66affSColin Finck     }
2586c2c66affSColin Finck     return r;
2587c2c66affSColin Finck }
2588c2c66affSColin Finck 
2589c2c66affSColin Finck MSICONDITION MSI_DatabaseIsTablePersistent( MSIDATABASE *db, LPCWSTR table )
2590c2c66affSColin Finck {
2591c2c66affSColin Finck     MSITABLE *t;
2592c2c66affSColin Finck     UINT r;
2593c2c66affSColin Finck 
2594c2c66affSColin Finck     TRACE("%p %s\n", db, debugstr_w(table));
2595c2c66affSColin Finck 
2596c2c66affSColin Finck     if (!table)
2597c2c66affSColin Finck         return MSICONDITION_ERROR;
2598c2c66affSColin Finck 
2599c2c66affSColin Finck     r = get_table( db, table, &t );
2600c2c66affSColin Finck     if (r != ERROR_SUCCESS)
2601c2c66affSColin Finck         return MSICONDITION_NONE;
2602c2c66affSColin Finck 
2603c2c66affSColin Finck     return t->persistent;
2604c2c66affSColin Finck }
2605c2c66affSColin Finck 
2606c2c66affSColin Finck static UINT read_raw_int(const BYTE *data, UINT col, UINT bytes)
2607c2c66affSColin Finck {
2608c2c66affSColin Finck     UINT ret = 0, i;
2609c2c66affSColin Finck 
2610c2c66affSColin Finck     for (i = 0; i < bytes; i++)
2611c2c66affSColin Finck         ret += (data[col + i] << i * 8);
2612c2c66affSColin Finck 
2613c2c66affSColin Finck     return ret;
2614c2c66affSColin Finck }
2615c2c66affSColin Finck 
2616c2c66affSColin Finck static UINT msi_record_encoded_stream_name( const MSITABLEVIEW *tv, MSIRECORD *rec, LPWSTR *pstname )
2617c2c66affSColin Finck {
26188e75cb8aSwinesync     UINT r, len;
26198e75cb8aSwinesync     WCHAR *name;
2620c2c66affSColin Finck 
2621c2c66affSColin Finck     TRACE("%p %p\n", tv, rec);
2622c2c66affSColin Finck 
26238e75cb8aSwinesync     r = msi_record_stream_name( tv, rec, NULL, &len );
26248e75cb8aSwinesync     if (r != ERROR_SUCCESS)
2625c2c66affSColin Finck         return r;
26268e75cb8aSwinesync     len++;
26278e75cb8aSwinesync 
26288e75cb8aSwinesync     name = msi_alloc( len * sizeof(WCHAR) );
26298e75cb8aSwinesync     if (!name)
26308e75cb8aSwinesync         return ERROR_OUTOFMEMORY;
26318e75cb8aSwinesync 
26328e75cb8aSwinesync     r = msi_record_stream_name( tv, rec, name, &len );
26338e75cb8aSwinesync     if (r != ERROR_SUCCESS)
26348e75cb8aSwinesync     {
26358e75cb8aSwinesync         msi_free( name );
26368e75cb8aSwinesync         return r;
26378e75cb8aSwinesync     }
26388e75cb8aSwinesync 
26398e75cb8aSwinesync     *pstname = encode_streamname( FALSE, name );
26408e75cb8aSwinesync     msi_free( name );
26418e75cb8aSwinesync     return ERROR_SUCCESS;
2642c2c66affSColin Finck }
2643c2c66affSColin Finck 
2644c2c66affSColin Finck static MSIRECORD *msi_get_transform_record( const MSITABLEVIEW *tv, const string_table *st,
2645c2c66affSColin Finck                                             IStorage *stg, const BYTE *rawdata, UINT bytes_per_strref )
2646c2c66affSColin Finck {
2647c2c66affSColin Finck     UINT i, val, ofs = 0;
2648c2c66affSColin Finck     USHORT mask;
2649c2c66affSColin Finck     MSICOLUMNINFO *columns = tv->columns;
2650c2c66affSColin Finck     MSIRECORD *rec;
2651c2c66affSColin Finck 
2652c2c66affSColin Finck     mask = rawdata[0] | (rawdata[1] << 8);
2653c2c66affSColin Finck     rawdata += 2;
2654c2c66affSColin Finck 
2655c2c66affSColin Finck     rec = MSI_CreateRecord( tv->num_cols );
2656c2c66affSColin Finck     if( !rec )
2657c2c66affSColin Finck         return rec;
2658c2c66affSColin Finck 
2659c2c66affSColin Finck     TRACE("row ->\n");
2660c2c66affSColin Finck     for( i=0; i<tv->num_cols; i++ )
2661c2c66affSColin Finck     {
2662c2c66affSColin Finck         if ( (mask&1) && (i>=(mask>>8)) )
2663c2c66affSColin Finck             break;
2664c2c66affSColin Finck         /* all keys must be present */
2665c2c66affSColin Finck         if ( (~mask&1) && (~columns[i].type & MSITYPE_KEY) && ((1<<i) & ~mask) )
2666c2c66affSColin Finck             continue;
2667c2c66affSColin Finck 
2668c2c66affSColin Finck         if( MSITYPE_IS_BINARY(tv->columns[i].type) )
2669c2c66affSColin Finck         {
2670c2c66affSColin Finck             LPWSTR encname;
2671c2c66affSColin Finck             IStream *stm = NULL;
2672c2c66affSColin Finck             UINT r;
2673c2c66affSColin Finck 
2674c2c66affSColin Finck             ofs += bytes_per_column( tv->db, &columns[i], bytes_per_strref );
2675c2c66affSColin Finck 
2676c2c66affSColin Finck             r = msi_record_encoded_stream_name( tv, rec, &encname );
2677c2c66affSColin Finck             if ( r != ERROR_SUCCESS )
2678c2c66affSColin Finck             {
2679c2c66affSColin Finck                 msiobj_release( &rec->hdr );
2680c2c66affSColin Finck                 return NULL;
2681c2c66affSColin Finck             }
2682c2c66affSColin Finck             r = IStorage_OpenStream( stg, encname, NULL, STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stm );
2683c2c66affSColin Finck             if ( r != ERROR_SUCCESS )
2684c2c66affSColin Finck             {
2685c2c66affSColin Finck                 msiobj_release( &rec->hdr );
2686c2c66affSColin Finck                 msi_free( encname );
2687c2c66affSColin Finck                 return NULL;
2688c2c66affSColin Finck             }
2689c2c66affSColin Finck 
2690c2c66affSColin Finck             MSI_RecordSetStream( rec, i+1, stm );
2691c2c66affSColin Finck             TRACE(" field %d [%s]\n", i+1, debugstr_w(encname));
2692c2c66affSColin Finck             msi_free( encname );
2693c2c66affSColin Finck         }
2694c2c66affSColin Finck         else if( columns[i].type & MSITYPE_STRING )
2695c2c66affSColin Finck         {
2696c2c66affSColin Finck             int len;
2697c2c66affSColin Finck             const WCHAR *sval;
2698c2c66affSColin Finck 
2699c2c66affSColin Finck             val = read_raw_int(rawdata, ofs, bytes_per_strref);
2700c2c66affSColin Finck             sval = msi_string_lookup( st, val, &len );
2701c2c66affSColin Finck             msi_record_set_string( rec, i+1, sval, len );
2702c2c66affSColin Finck             TRACE(" field %d [%s]\n", i+1, debugstr_wn(sval, len));
2703c2c66affSColin Finck             ofs += bytes_per_strref;
2704c2c66affSColin Finck         }
2705c2c66affSColin Finck         else
2706c2c66affSColin Finck         {
2707c2c66affSColin Finck             UINT n = bytes_per_column( tv->db, &columns[i], bytes_per_strref );
2708c2c66affSColin Finck             switch( n )
2709c2c66affSColin Finck             {
2710c2c66affSColin Finck             case 2:
2711c2c66affSColin Finck                 val = read_raw_int(rawdata, ofs, n);
2712c2c66affSColin Finck                 if (val)
2713c2c66affSColin Finck                     MSI_RecordSetInteger( rec, i+1, val-0x8000 );
2714c2c66affSColin Finck                 TRACE(" field %d [0x%04x]\n", i+1, val );
2715c2c66affSColin Finck                 break;
2716c2c66affSColin Finck             case 4:
2717c2c66affSColin Finck                 val = read_raw_int(rawdata, ofs, n);
2718c2c66affSColin Finck                 if (val)
2719c2c66affSColin Finck                     MSI_RecordSetInteger( rec, i+1, val^0x80000000 );
2720c2c66affSColin Finck                 TRACE(" field %d [0x%08x]\n", i+1, val );
2721c2c66affSColin Finck                 break;
2722c2c66affSColin Finck             default:
2723c2c66affSColin Finck                 ERR("oops - unknown column width %d\n", n);
2724c2c66affSColin Finck                 break;
2725c2c66affSColin Finck             }
2726c2c66affSColin Finck             ofs += n;
2727c2c66affSColin Finck         }
2728c2c66affSColin Finck     }
2729c2c66affSColin Finck     return rec;
2730c2c66affSColin Finck }
2731c2c66affSColin Finck 
2732c2c66affSColin Finck static void dump_table( const string_table *st, const USHORT *rawdata, UINT rawsize )
2733c2c66affSColin Finck {
2734c2c66affSColin Finck     UINT i;
2735c2c66affSColin Finck     for (i = 0; i < rawsize / 2; i++)
2736c2c66affSColin Finck     {
2737c2c66affSColin Finck         int len;
2738c2c66affSColin Finck         const WCHAR *sval = msi_string_lookup( st, rawdata[i], &len );
2739c2c66affSColin Finck         MESSAGE(" %04x %s\n", rawdata[i], debugstr_wn(sval, len) );
2740c2c66affSColin Finck     }
2741c2c66affSColin Finck }
2742c2c66affSColin Finck 
2743c2c66affSColin Finck static UINT* msi_record_to_row( const MSITABLEVIEW *tv, MSIRECORD *rec )
2744c2c66affSColin Finck {
2745c2c66affSColin Finck     UINT i, r, *data;
2746c2c66affSColin Finck 
2747c2c66affSColin Finck     data = msi_alloc( tv->num_cols *sizeof (UINT) );
2748c2c66affSColin Finck     for( i=0; i<tv->num_cols; i++ )
2749c2c66affSColin Finck     {
2750c2c66affSColin Finck         data[i] = 0;
2751c2c66affSColin Finck 
2752c2c66affSColin Finck         if ( ~tv->columns[i].type & MSITYPE_KEY )
2753c2c66affSColin Finck             continue;
2754c2c66affSColin Finck 
2755c2c66affSColin Finck         /* turn the transform column value into a row value */
2756c2c66affSColin Finck         if ( ( tv->columns[i].type & MSITYPE_STRING ) &&
2757c2c66affSColin Finck              ! MSITYPE_IS_BINARY(tv->columns[i].type) )
2758c2c66affSColin Finck         {
2759c2c66affSColin Finck             int len;
2760c2c66affSColin Finck             const WCHAR *str = msi_record_get_string( rec, i+1, &len );
2761c2c66affSColin Finck             if (str)
2762c2c66affSColin Finck             {
2763c2c66affSColin Finck                 r = msi_string2id( tv->db->strings, str, len, &data[i] );
2764c2c66affSColin Finck 
2765c2c66affSColin Finck                 /* if there's no matching string in the string table,
2766c2c66affSColin Finck                    these keys can't match any record, so fail now. */
2767c2c66affSColin Finck                 if (r != ERROR_SUCCESS)
2768c2c66affSColin Finck                 {
2769c2c66affSColin Finck                     msi_free( data );
2770c2c66affSColin Finck                     return NULL;
2771c2c66affSColin Finck                 }
2772c2c66affSColin Finck             }
2773c2c66affSColin Finck             else data[i] = 0;
2774c2c66affSColin Finck         }
2775c2c66affSColin Finck         else
2776c2c66affSColin Finck         {
27770e83dd03Swinesync             if (int_to_table_storage( tv, i + 1, MSI_RecordGetInteger( rec, i + 1 ), &data[i] ))
27780e83dd03Swinesync             {
27790e83dd03Swinesync                 msi_free( data );
27800e83dd03Swinesync                 return NULL;
27810e83dd03Swinesync             }
2782c2c66affSColin Finck         }
2783c2c66affSColin Finck     }
2784c2c66affSColin Finck     return data;
2785c2c66affSColin Finck }
2786c2c66affSColin Finck 
2787c2c66affSColin Finck static UINT msi_row_matches( MSITABLEVIEW *tv, UINT row, const UINT *data, UINT *column )
2788c2c66affSColin Finck {
2789c2c66affSColin Finck     UINT i, r, x, ret = ERROR_FUNCTION_FAILED;
2790c2c66affSColin Finck 
2791c2c66affSColin Finck     for( i=0; i<tv->num_cols; i++ )
2792c2c66affSColin Finck     {
2793c2c66affSColin Finck         if ( ~tv->columns[i].type & MSITYPE_KEY )
2794c2c66affSColin Finck             continue;
2795c2c66affSColin Finck 
2796c2c66affSColin Finck         /* turn the transform column value into a row value */
2797c2c66affSColin Finck         r = TABLE_fetch_int( &tv->view, row, i+1, &x );
2798c2c66affSColin Finck         if ( r != ERROR_SUCCESS )
2799c2c66affSColin Finck         {
2800c2c66affSColin Finck             ERR("TABLE_fetch_int shouldn't fail here\n");
2801c2c66affSColin Finck             break;
2802c2c66affSColin Finck         }
2803c2c66affSColin Finck 
2804c2c66affSColin Finck         /* if this key matches, move to the next column */
2805c2c66affSColin Finck         if ( x != data[i] )
2806c2c66affSColin Finck         {
2807c2c66affSColin Finck             ret = ERROR_FUNCTION_FAILED;
2808c2c66affSColin Finck             break;
2809c2c66affSColin Finck         }
2810c2c66affSColin Finck         if (column) *column = i;
2811c2c66affSColin Finck         ret = ERROR_SUCCESS;
2812c2c66affSColin Finck     }
2813c2c66affSColin Finck     return ret;
2814c2c66affSColin Finck }
2815c2c66affSColin Finck 
2816c2c66affSColin Finck static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row, UINT *column )
2817c2c66affSColin Finck {
2818c2c66affSColin Finck     UINT i, r = ERROR_FUNCTION_FAILED, *data;
2819c2c66affSColin Finck 
2820c2c66affSColin Finck     data = msi_record_to_row( tv, rec );
2821c2c66affSColin Finck     if( !data )
2822c2c66affSColin Finck         return r;
2823c2c66affSColin Finck     for( i = 0; i < tv->table->row_count; i++ )
2824c2c66affSColin Finck     {
2825c2c66affSColin Finck         r = msi_row_matches( tv, i, data, column );
2826c2c66affSColin Finck         if( r == ERROR_SUCCESS )
2827c2c66affSColin Finck         {
2828c2c66affSColin Finck             *row = i;
2829c2c66affSColin Finck             break;
2830c2c66affSColin Finck         }
2831c2c66affSColin Finck     }
2832c2c66affSColin Finck     msi_free( data );
2833c2c66affSColin Finck     return r;
2834c2c66affSColin Finck }
2835c2c66affSColin Finck 
2836c2c66affSColin Finck typedef struct
2837c2c66affSColin Finck {
2838c2c66affSColin Finck     struct list entry;
2839c2c66affSColin Finck     LPWSTR name;
2840c2c66affSColin Finck } TRANSFORMDATA;
2841c2c66affSColin Finck 
2842c2c66affSColin Finck static UINT msi_table_load_transform( MSIDATABASE *db, IStorage *stg,
2843c2c66affSColin Finck                                       string_table *st, TRANSFORMDATA *transform,
28441bbb87bbSwinesync                                       UINT bytes_per_strref, int err_cond )
2845c2c66affSColin Finck {
2846c2c66affSColin Finck     BYTE *rawdata = NULL;
2847c2c66affSColin Finck     MSITABLEVIEW *tv = NULL;
2848c2c66affSColin Finck     UINT r, n, sz, i, mask, num_cols, colcol = 0, rawsize = 0;
2849c2c66affSColin Finck     MSIRECORD *rec = NULL;
2850c2c66affSColin Finck     WCHAR coltable[32];
2851c2c66affSColin Finck     const WCHAR *name;
2852c2c66affSColin Finck 
2853c2c66affSColin Finck     if (!transform)
2854c2c66affSColin Finck         return ERROR_SUCCESS;
2855c2c66affSColin Finck 
2856c2c66affSColin Finck     name = transform->name;
2857c2c66affSColin Finck 
2858c2c66affSColin Finck     coltable[0] = 0;
2859c2c66affSColin Finck     TRACE("%p %p %p %s\n", db, stg, st, debugstr_w(name) );
2860c2c66affSColin Finck 
2861c2c66affSColin Finck     /* read the transform data */
2862c2c66affSColin Finck     read_stream_data( stg, name, TRUE, &rawdata, &rawsize );
2863c2c66affSColin Finck     if ( !rawdata )
2864c2c66affSColin Finck     {
2865c2c66affSColin Finck         TRACE("table %s empty\n", debugstr_w(name) );
2866c2c66affSColin Finck         return ERROR_INVALID_TABLE;
2867c2c66affSColin Finck     }
2868c2c66affSColin Finck 
2869c2c66affSColin Finck     /* create a table view */
28701bbb87bbSwinesync     if ( err_cond & MSITRANSFORM_ERROR_VIEWTRANSFORM )
28711bbb87bbSwinesync         r = TransformView_Create( db, st, name, (MSIVIEW**) &tv );
28721bbb87bbSwinesync     else
2873c2c66affSColin Finck         r = TABLE_CreateView( db, name, (MSIVIEW**) &tv );
2874c2c66affSColin Finck     if( r != ERROR_SUCCESS )
2875c2c66affSColin Finck         goto err;
2876c2c66affSColin Finck 
2877c2c66affSColin Finck     r = tv->view.ops->execute( &tv->view, NULL );
2878c2c66affSColin Finck     if( r != ERROR_SUCCESS )
2879c2c66affSColin Finck         goto err;
2880c2c66affSColin Finck 
2881c2c66affSColin Finck     TRACE("name = %s columns = %u row_size = %u raw size = %u\n",
2882c2c66affSColin Finck           debugstr_w(name), tv->num_cols, tv->row_size, rawsize );
2883c2c66affSColin Finck 
2884c2c66affSColin Finck     /* interpret the data */
2885c2c66affSColin Finck     for (n = 0; n < rawsize;)
2886c2c66affSColin Finck     {
2887c2c66affSColin Finck         mask = rawdata[n] | (rawdata[n + 1] << 8);
2888c2c66affSColin Finck         if (mask & 1)
2889c2c66affSColin Finck         {
2890c2c66affSColin Finck             /*
2891c2c66affSColin Finck              * if the low bit is set, columns are continuous and
2892c2c66affSColin Finck              * the number of columns is specified in the high byte
2893c2c66affSColin Finck              */
2894c2c66affSColin Finck             sz = 2;
2895c2c66affSColin Finck             num_cols = mask >> 8;
2896c2c66affSColin Finck             if (num_cols > tv->num_cols)
2897c2c66affSColin Finck             {
2898c2c66affSColin Finck                 ERR("excess columns in transform: %u > %u\n", num_cols, tv->num_cols);
2899c2c66affSColin Finck                 break;
2900c2c66affSColin Finck             }
2901c2c66affSColin Finck 
2902c2c66affSColin Finck             for (i = 0; i < num_cols; i++)
2903c2c66affSColin Finck             {
2904c2c66affSColin Finck                 if( (tv->columns[i].type & MSITYPE_STRING) &&
2905c2c66affSColin Finck                     ! MSITYPE_IS_BINARY(tv->columns[i].type) )
2906c2c66affSColin Finck                     sz += bytes_per_strref;
2907c2c66affSColin Finck                 else
2908c2c66affSColin Finck                     sz += bytes_per_column( tv->db, &tv->columns[i], bytes_per_strref );
2909c2c66affSColin Finck             }
2910c2c66affSColin Finck         }
2911c2c66affSColin Finck         else
2912c2c66affSColin Finck         {
2913c2c66affSColin Finck             /*
2914c2c66affSColin Finck              * If the low bit is not set, mask is a bitmask.
2915c2c66affSColin Finck              * Excepting for key fields, which are always present,
2916c2c66affSColin Finck              *  each bit indicates that a field is present in the transform record.
2917c2c66affSColin Finck              *
2918c2c66affSColin Finck              * mask == 0 is a special case ... only the keys will be present
2919c2c66affSColin Finck              * and it means that this row should be deleted.
2920c2c66affSColin Finck              */
2921c2c66affSColin Finck             sz = 2;
2922c2c66affSColin Finck             num_cols = tv->num_cols;
2923c2c66affSColin Finck             for (i = 0; i < num_cols; i++)
2924c2c66affSColin Finck             {
2925c2c66affSColin Finck                 if ((tv->columns[i].type & MSITYPE_KEY) || ((1 << i) & mask))
2926c2c66affSColin Finck                 {
2927c2c66affSColin Finck                     if ((tv->columns[i].type & MSITYPE_STRING) &&
2928c2c66affSColin Finck                         !MSITYPE_IS_BINARY(tv->columns[i].type))
2929c2c66affSColin Finck                         sz += bytes_per_strref;
2930c2c66affSColin Finck                     else
2931c2c66affSColin Finck                         sz += bytes_per_column( tv->db, &tv->columns[i], bytes_per_strref );
2932c2c66affSColin Finck                 }
2933c2c66affSColin Finck             }
2934c2c66affSColin Finck         }
2935c2c66affSColin Finck 
2936c2c66affSColin Finck         /* check we didn't run of the end of the table */
2937c2c66affSColin Finck         if (n + sz > rawsize)
2938c2c66affSColin Finck         {
2939c2c66affSColin Finck             ERR("borked.\n");
2940c2c66affSColin Finck             dump_table( st, (USHORT *)rawdata, rawsize );
2941c2c66affSColin Finck             break;
2942c2c66affSColin Finck         }
2943c2c66affSColin Finck 
2944c2c66affSColin Finck         rec = msi_get_transform_record( tv, st, stg, &rawdata[n], bytes_per_strref );
2945c2c66affSColin Finck         if (rec)
2946c2c66affSColin Finck         {
2947c2c66affSColin Finck             WCHAR table[32];
2948c2c66affSColin Finck             DWORD sz = 32;
2949c2c66affSColin Finck             UINT number = MSI_NULL_INTEGER;
2950c2c66affSColin Finck             UINT row = 0;
2951c2c66affSColin Finck 
2952958f1addSwinesync             if (!wcscmp( name, szColumns ))
2953c2c66affSColin Finck             {
2954c2c66affSColin Finck                 MSI_RecordGetStringW( rec, 1, table, &sz );
2955c2c66affSColin Finck                 number = MSI_RecordGetInteger( rec, 2 );
2956c2c66affSColin Finck 
2957c2c66affSColin Finck                 /*
2958c2c66affSColin Finck                  * Native msi seems writes nul into the Number (2nd) column of
2959c2c66affSColin Finck                  * the _Columns table when there are new columns
2960c2c66affSColin Finck                  */
2961c2c66affSColin Finck                 if ( number == MSI_NULL_INTEGER )
2962c2c66affSColin Finck                 {
2963c2c66affSColin Finck                     /* reset the column number on a new table */
2964958f1addSwinesync                     if (wcscmp( coltable, table ))
2965c2c66affSColin Finck                     {
2966c2c66affSColin Finck                         colcol = 0;
2967c2c66affSColin Finck                         lstrcpyW( coltable, table );
2968c2c66affSColin Finck                     }
2969c2c66affSColin Finck 
2970c2c66affSColin Finck                     /* fix nul column numbers */
2971c2c66affSColin Finck                     MSI_RecordSetInteger( rec, 2, ++colcol );
2972c2c66affSColin Finck                 }
2973c2c66affSColin Finck             }
2974c2c66affSColin Finck 
2975c2c66affSColin Finck             if (TRACE_ON(msidb)) dump_record( rec );
2976c2c66affSColin Finck 
2977*27a430bdSwinesync             if (tv->table)
2978c2c66affSColin Finck                 r = msi_table_find_row( tv, rec, &row, NULL );
2979*27a430bdSwinesync             else
2980*27a430bdSwinesync                 r = ERROR_FUNCTION_FAILED;
2981c2c66affSColin Finck             if (r == ERROR_SUCCESS)
2982c2c66affSColin Finck             {
2983c2c66affSColin Finck                 if (!mask)
2984c2c66affSColin Finck                 {
2985c2c66affSColin Finck                     TRACE("deleting row [%d]:\n", row);
29861bbb87bbSwinesync                     r = tv->view.ops->delete_row( &tv->view, row );
2987c2c66affSColin Finck                     if (r != ERROR_SUCCESS)
2988c2c66affSColin Finck                         WARN("failed to delete row %u\n", r);
2989c2c66affSColin Finck                 }
2990c2c66affSColin Finck                 else if (mask & 1)
2991c2c66affSColin Finck                 {
2992c2c66affSColin Finck                     TRACE("modifying full row [%d]:\n", row);
29931bbb87bbSwinesync                     r = tv->view.ops->set_row( &tv->view, row, rec, (1 << tv->num_cols) - 1 );
2994c2c66affSColin Finck                     if (r != ERROR_SUCCESS)
2995c2c66affSColin Finck                         WARN("failed to modify row %u\n", r);
2996c2c66affSColin Finck                 }
2997c2c66affSColin Finck                 else
2998c2c66affSColin Finck                 {
2999c2c66affSColin Finck                     TRACE("modifying masked row [%d]:\n", row);
30001bbb87bbSwinesync                     r = tv->view.ops->set_row( &tv->view, row, rec, mask );
3001c2c66affSColin Finck                     if (r != ERROR_SUCCESS)
3002c2c66affSColin Finck                         WARN("failed to modify row %u\n", r);
3003c2c66affSColin Finck                 }
3004c2c66affSColin Finck             }
3005c2c66affSColin Finck             else
3006c2c66affSColin Finck             {
3007c2c66affSColin Finck                 TRACE("inserting row\n");
30081bbb87bbSwinesync                 r = tv->view.ops->insert_row( &tv->view, rec, -1, FALSE );
3009c2c66affSColin Finck                 if (r != ERROR_SUCCESS)
3010c2c66affSColin Finck                     WARN("failed to insert row %u\n", r);
3011c2c66affSColin Finck             }
3012c2c66affSColin Finck 
30131bbb87bbSwinesync             if (!(err_cond & MSITRANSFORM_ERROR_VIEWTRANSFORM) &&
30141bbb87bbSwinesync                     !wcscmp( name, szColumns ))
3015c2c66affSColin Finck                 msi_update_table_columns( db, table );
3016c2c66affSColin Finck 
3017c2c66affSColin Finck             msiobj_release( &rec->hdr );
3018c2c66affSColin Finck         }
3019c2c66affSColin Finck 
3020c2c66affSColin Finck         n += sz;
3021c2c66affSColin Finck     }
3022c2c66affSColin Finck 
3023c2c66affSColin Finck err:
3024c2c66affSColin Finck     /* no need to free the table, it's associated with the database */
3025c2c66affSColin Finck     msi_free( rawdata );
3026c2c66affSColin Finck     if( tv )
3027c2c66affSColin Finck         tv->view.ops->delete( &tv->view );
3028c2c66affSColin Finck 
3029c2c66affSColin Finck     return ERROR_SUCCESS;
3030c2c66affSColin Finck }
3031c2c66affSColin Finck 
3032c2c66affSColin Finck /*
3033c2c66affSColin Finck  * msi_table_apply_transform
3034c2c66affSColin Finck  *
3035c2c66affSColin Finck  * Enumerate the table transforms in a transform storage and apply each one.
3036c2c66affSColin Finck  */
30371bbb87bbSwinesync UINT msi_table_apply_transform( MSIDATABASE *db, IStorage *stg, int err_cond )
3038c2c66affSColin Finck {
3039c2c66affSColin Finck     struct list transforms;
3040c2c66affSColin Finck     IEnumSTATSTG *stgenum = NULL;
3041c2c66affSColin Finck     TRANSFORMDATA *transform;
3042c2c66affSColin Finck     TRANSFORMDATA *tables = NULL, *columns = NULL;
3043c2c66affSColin Finck     HRESULT hr;
3044c2c66affSColin Finck     STATSTG stat;
3045c2c66affSColin Finck     string_table *strings;
3046c2c66affSColin Finck     UINT ret = ERROR_FUNCTION_FAILED;
3047c2c66affSColin Finck     UINT bytes_per_strref;
3048c2c66affSColin Finck     BOOL property_update = FALSE;
30491bbb87bbSwinesync     BOOL free_transform_view = FALSE;
3050c2c66affSColin Finck 
3051c2c66affSColin Finck     TRACE("%p %p\n", db, stg );
3052c2c66affSColin Finck 
3053c2c66affSColin Finck     strings = msi_load_string_table( stg, &bytes_per_strref );
3054c2c66affSColin Finck     if( !strings )
3055c2c66affSColin Finck         goto end;
3056c2c66affSColin Finck 
3057c2c66affSColin Finck     hr = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
3058c2c66affSColin Finck     if (FAILED( hr ))
3059c2c66affSColin Finck         goto end;
3060c2c66affSColin Finck 
3061c2c66affSColin Finck     list_init(&transforms);
3062c2c66affSColin Finck 
3063c2c66affSColin Finck     while ( TRUE )
3064c2c66affSColin Finck     {
3065c2c66affSColin Finck         MSITABLEVIEW *tv = NULL;
3066c2c66affSColin Finck         WCHAR name[0x40];
3067c2c66affSColin Finck         ULONG count = 0;
3068c2c66affSColin Finck 
3069c2c66affSColin Finck         hr = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
3070c2c66affSColin Finck         if (FAILED( hr ) || !count)
3071c2c66affSColin Finck             break;
3072c2c66affSColin Finck 
3073c2c66affSColin Finck         decode_streamname( stat.pwcsName, name );
3074c2c66affSColin Finck         CoTaskMemFree( stat.pwcsName );
3075c2c66affSColin Finck         if ( name[0] != 0x4840 )
3076c2c66affSColin Finck             continue;
3077c2c66affSColin Finck 
3078958f1addSwinesync         if ( !wcscmp( name+1, szStringPool ) ||
3079958f1addSwinesync              !wcscmp( name+1, szStringData ) )
3080c2c66affSColin Finck             continue;
3081c2c66affSColin Finck 
3082c2c66affSColin Finck         transform = msi_alloc_zero( sizeof(TRANSFORMDATA) );
3083c2c66affSColin Finck         if ( !transform )
3084c2c66affSColin Finck             break;
3085c2c66affSColin Finck 
3086c2c66affSColin Finck         list_add_tail( &transforms, &transform->entry );
3087c2c66affSColin Finck 
3088c2c66affSColin Finck         transform->name = strdupW( name + 1 );
3089c2c66affSColin Finck 
3090958f1addSwinesync         if ( !wcscmp( transform->name, szTables ) )
3091c2c66affSColin Finck             tables = transform;
3092958f1addSwinesync         else if (!wcscmp( transform->name, szColumns ) )
3093c2c66affSColin Finck             columns = transform;
3094958f1addSwinesync         else if (!wcscmp( transform->name, szProperty ))
3095c2c66affSColin Finck             property_update = TRUE;
3096c2c66affSColin Finck 
3097c2c66affSColin Finck         TRACE("transform contains stream %s\n", debugstr_w(name));
3098c2c66affSColin Finck 
3099c2c66affSColin Finck         /* load the table */
3100c2c66affSColin Finck         if (TABLE_CreateView( db, transform->name, (MSIVIEW**) &tv ) != ERROR_SUCCESS)
3101c2c66affSColin Finck             continue;
3102c2c66affSColin Finck 
3103c2c66affSColin Finck         if (tv->view.ops->execute( &tv->view, NULL ) != ERROR_SUCCESS)
3104c2c66affSColin Finck         {
3105c2c66affSColin Finck             tv->view.ops->delete( &tv->view );
3106c2c66affSColin Finck             continue;
3107c2c66affSColin Finck         }
3108c2c66affSColin Finck 
3109c2c66affSColin Finck         tv->view.ops->delete( &tv->view );
3110c2c66affSColin Finck     }
3111c2c66affSColin Finck 
31121bbb87bbSwinesync     if (err_cond & MSITRANSFORM_ERROR_VIEWTRANSFORM)
31131bbb87bbSwinesync     {
31141bbb87bbSwinesync         static const WCHAR create_query[] = L"CREATE TABLE `_TransformView` ( "
31151bbb87bbSwinesync             L"`Table` CHAR(0) NOT NULL TEMPORARY, `Column` CHAR(0) NOT NULL TEMPORARY, "
31161bbb87bbSwinesync             L"`Row` CHAR(0) TEMPORARY, `Data` CHAR(0) TEMPORARY, `Current` CHAR(0) TEMPORARY "
31171bbb87bbSwinesync             L"PRIMARY KEY `Table`, `Column`, `Row` ) HOLD";
31181bbb87bbSwinesync         MSIQUERY *query;
31191bbb87bbSwinesync         UINT r;
31201bbb87bbSwinesync 
31211bbb87bbSwinesync         r = MSI_DatabaseOpenViewW( db, create_query, &query );
31221bbb87bbSwinesync         if (r != ERROR_SUCCESS)
31231bbb87bbSwinesync             goto end;
31241bbb87bbSwinesync 
31251bbb87bbSwinesync         r = MSI_ViewExecute( query, NULL );
31261bbb87bbSwinesync         if (r == ERROR_SUCCESS)
31271bbb87bbSwinesync             MSI_ViewClose( query );
31281bbb87bbSwinesync         msiobj_release( &query->hdr );
31291bbb87bbSwinesync         if (r == ERROR_BAD_QUERY_SYNTAX)
31301bbb87bbSwinesync             FIXME( "support adding to _TransformView\n" );
31311bbb87bbSwinesync         if (r != ERROR_SUCCESS)
31321bbb87bbSwinesync             goto end;
31331bbb87bbSwinesync         free_transform_view = TRUE;
31341bbb87bbSwinesync     }
31351bbb87bbSwinesync 
3136c2c66affSColin Finck     /*
3137c2c66affSColin Finck      * Apply _Tables and _Columns transforms first so that
3138c2c66affSColin Finck      * the table metadata is correct, and empty tables exist.
3139c2c66affSColin Finck      */
31401bbb87bbSwinesync     ret = msi_table_load_transform( db, stg, strings, tables, bytes_per_strref, err_cond );
3141c2c66affSColin Finck     if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
3142c2c66affSColin Finck         goto end;
3143c2c66affSColin Finck 
31441bbb87bbSwinesync     ret = msi_table_load_transform( db, stg, strings, columns, bytes_per_strref, err_cond );
3145c2c66affSColin Finck     if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
3146c2c66affSColin Finck         goto end;
3147c2c66affSColin Finck 
3148c2c66affSColin Finck     ret = ERROR_SUCCESS;
3149c2c66affSColin Finck 
3150c2c66affSColin Finck     while ( !list_empty( &transforms ) )
3151c2c66affSColin Finck     {
3152c2c66affSColin Finck         transform = LIST_ENTRY( list_head( &transforms ), TRANSFORMDATA, entry );
3153c2c66affSColin Finck 
3154958f1addSwinesync         if ( wcscmp( transform->name, szColumns ) &&
3155958f1addSwinesync              wcscmp( transform->name, szTables ) &&
3156c2c66affSColin Finck              ret == ERROR_SUCCESS )
3157c2c66affSColin Finck         {
31581bbb87bbSwinesync             ret = msi_table_load_transform( db, stg, strings, transform, bytes_per_strref, err_cond );
3159c2c66affSColin Finck         }
3160c2c66affSColin Finck 
3161c2c66affSColin Finck         list_remove( &transform->entry );
3162c2c66affSColin Finck         msi_free( transform->name );
3163c2c66affSColin Finck         msi_free( transform );
3164c2c66affSColin Finck     }
3165c2c66affSColin Finck 
3166c2c66affSColin Finck     if ( ret == ERROR_SUCCESS )
3167c2c66affSColin Finck     {
3168c2c66affSColin Finck         append_storage_to_db( db, stg );
3169c2c66affSColin Finck         if (property_update) msi_clone_properties( db );
3170c2c66affSColin Finck     }
3171c2c66affSColin Finck 
3172c2c66affSColin Finck end:
3173c2c66affSColin Finck     if ( stgenum )
3174c2c66affSColin Finck         IEnumSTATSTG_Release( stgenum );
3175c2c66affSColin Finck     if ( strings )
3176c2c66affSColin Finck         msi_destroy_stringtable( strings );
31771bbb87bbSwinesync     if (ret != ERROR_SUCCESS && free_transform_view)
31781bbb87bbSwinesync     {
31791bbb87bbSwinesync         MSIQUERY *query;
31801bbb87bbSwinesync         if (MSI_DatabaseOpenViewW( db, L"ALTER TABLE `_TransformView` FREE",
31811bbb87bbSwinesync                     &query ) == ERROR_SUCCESS)
31821bbb87bbSwinesync         {
31831bbb87bbSwinesync             if (MSI_ViewExecute( query, NULL ) == ERROR_SUCCESS)
31841bbb87bbSwinesync                 MSI_ViewClose( query );
31851bbb87bbSwinesync             msiobj_release( &query->hdr );
31861bbb87bbSwinesync         }
31871bbb87bbSwinesync     }
3188c2c66affSColin Finck 
3189c2c66affSColin Finck     return ret;
3190c2c66affSColin Finck }
3191