ntdll: Added parsing of public key token in manifests.
[wine] / dlls / msi / select.c
1 /*
2  * Implementation of the Microsoft Installer (msi.dll)
3  *
4  * Copyright 2002-2004 Mike McCormack for CodeWeavers
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdarg.h>
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winerror.h"
26 #include "wine/debug.h"
27 #include "msi.h"
28 #include "msiquery.h"
29 #include "objbase.h"
30 #include "objidl.h"
31 #include "msipriv.h"
32 #include "winnls.h"
33
34 #include "query.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
37
38
39 /* below is the query interface to a table */
40
41 typedef struct tagMSISELECTVIEW
42 {
43     MSIVIEW        view;
44     MSIDATABASE   *db;
45     MSIVIEW       *table;
46     UINT           num_cols;
47     UINT           max_cols;
48     UINT           cols[1];
49 } MSISELECTVIEW;
50
51 static UINT SELECT_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
52 {
53     MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
54
55     TRACE("%p %d %d %p\n", sv, row, col, val );
56
57     if( !sv->table )
58          return ERROR_FUNCTION_FAILED;
59
60     if( (col==0) || (col>sv->num_cols) )
61          return ERROR_FUNCTION_FAILED;
62
63     col = sv->cols[ col - 1 ];
64
65     return sv->table->ops->fetch_int( sv->table, row, col, val );
66 }
67
68 static UINT SELECT_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm)
69 {
70     MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
71
72     TRACE("%p %d %d %p\n", sv, row, col, stm );
73
74     if( !sv->table )
75          return ERROR_FUNCTION_FAILED;
76
77     if( (col==0) || (col>sv->num_cols) )
78          return ERROR_FUNCTION_FAILED;
79
80     col = sv->cols[ col - 1 ];
81
82     return sv->table->ops->fetch_stream( sv->table, row, col, stm );
83 }
84
85 static UINT SELECT_set_row( struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask )
86 {
87     MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
88     UINT i, expanded_mask = 0, r = ERROR_SUCCESS, col_count = 0;
89     MSIRECORD *expanded;
90
91     TRACE("%p %d %p %08x\n", sv, row, rec, mask );
92
93     if ( !sv->table )
94          return ERROR_FUNCTION_FAILED;
95
96     /* test if any of the mask bits are invalid */
97     if ( mask >= (1<<sv->num_cols) )
98         return ERROR_INVALID_PARAMETER;
99
100     /* find the number of columns in the table below */
101     r = sv->table->ops->get_dimensions( sv->table, NULL, &col_count );
102     if( r )
103         return r;
104
105     /* expand the record to the right size for the underlying table */
106     expanded = MSI_CreateRecord( col_count );
107     if ( !expanded )
108         return ERROR_FUNCTION_FAILED;
109
110     /* move the right fields across */
111     for ( i=0; i<sv->num_cols; i++ )
112     {
113         r = MSI_RecordCopyField( rec, i+1, expanded, sv->cols[ i ] );
114         if (r != ERROR_SUCCESS)
115             break;
116         expanded_mask |= (1<<(sv->cols[i]-1));
117     }
118
119     /* set the row in the underlying table */
120     if (r == ERROR_SUCCESS)
121         r = sv->table->ops->set_row( sv->table, row, expanded, expanded_mask );
122
123     msiobj_release( &expanded->hdr );
124     return r;
125 }
126
127 static UINT SELECT_insert_row( struct tagMSIVIEW *view, MSIRECORD *record, BOOL temporary )
128 {
129     MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
130     UINT i, table_cols, r;
131     MSIRECORD *outrec;
132
133     TRACE("%p %p\n", sv, record );
134
135     if ( !sv->table )
136         return ERROR_FUNCTION_FAILED;
137
138     /* rearrange the record to suit the table */
139     r = sv->table->ops->get_dimensions( sv->table, NULL, &table_cols );
140     if (r != ERROR_SUCCESS)
141         return r;
142
143     outrec = MSI_CreateRecord( table_cols + 1 );
144
145     for (i=0; i<sv->num_cols; i++)
146     {
147         r = MSI_RecordCopyField( record, i+1, outrec, sv->cols[i] );
148         if (r != ERROR_SUCCESS)
149             goto fail;
150     }
151
152     r = sv->table->ops->insert_row( sv->table, outrec, temporary );
153
154 fail:
155     msiobj_release( &outrec->hdr );
156
157     return r;
158 }
159
160 static UINT SELECT_execute( struct tagMSIVIEW *view, MSIRECORD *record )
161 {
162     MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
163
164     TRACE("%p %p\n", sv, record);
165
166     if( !sv->table )
167          return ERROR_FUNCTION_FAILED;
168
169     return sv->table->ops->execute( sv->table, record );
170 }
171
172 static UINT SELECT_close( struct tagMSIVIEW *view )
173 {
174     MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
175
176     TRACE("%p\n", sv );
177
178     if( !sv->table )
179          return ERROR_FUNCTION_FAILED;
180
181     return sv->table->ops->close( sv->table );
182 }
183
184 static UINT SELECT_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols )
185 {
186     MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
187
188     TRACE("%p %p %p\n", sv, rows, cols );
189
190     if( !sv->table )
191          return ERROR_FUNCTION_FAILED;
192
193     if( cols )
194         *cols = sv->num_cols;
195
196     return sv->table->ops->get_dimensions( sv->table, rows, NULL );
197 }
198
199 static UINT SELECT_get_column_info( struct tagMSIVIEW *view,
200                 UINT n, LPWSTR *name, UINT *type )
201 {
202     MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
203
204     TRACE("%p %d %p %p\n", sv, n, name, type );
205
206     if( !sv->table )
207          return ERROR_FUNCTION_FAILED;
208
209     if( (n==0) || (n>sv->num_cols) )
210          return ERROR_FUNCTION_FAILED;
211
212     n = sv->cols[ n - 1 ];
213
214     return sv->table->ops->get_column_info( sv->table, n, name, type );
215 }
216
217 static UINT SELECT_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
218                 MSIRECORD *rec )
219 {
220     MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
221
222     TRACE("%p %d %p\n", sv, eModifyMode, rec );
223
224     if( !sv->table )
225          return ERROR_FUNCTION_FAILED;
226
227     return sv->table->ops->modify( sv->table, eModifyMode, rec );
228 }
229
230 static UINT SELECT_delete( struct tagMSIVIEW *view )
231 {
232     MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
233
234     TRACE("%p\n", sv );
235
236     if( sv->table )
237         sv->table->ops->delete( sv->table );
238     sv->table = NULL;
239
240     msi_free( sv );
241
242     return ERROR_SUCCESS;
243 }
244
245 static UINT SELECT_find_matching_rows( struct tagMSIVIEW *view, UINT col,
246     UINT val, UINT *row, MSIITERHANDLE *handle )
247 {
248     MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
249
250     TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
251
252     if( !sv->table )
253          return ERROR_FUNCTION_FAILED;
254
255     if( (col==0) || (col>sv->num_cols) )
256          return ERROR_FUNCTION_FAILED;
257
258     col = sv->cols[ col - 1 ];
259
260     return sv->table->ops->find_matching_rows( sv->table, col, val, row, handle );
261 }
262
263
264 static const MSIVIEWOPS select_ops =
265 {
266     SELECT_fetch_int,
267     SELECT_fetch_stream,
268     SELECT_set_row,
269     SELECT_insert_row,
270     NULL,
271     SELECT_execute,
272     SELECT_close,
273     SELECT_get_dimensions,
274     SELECT_get_column_info,
275     SELECT_modify,
276     SELECT_delete,
277     SELECT_find_matching_rows,
278     NULL,
279     NULL,
280 };
281
282 static UINT SELECT_AddColumn( MSISELECTVIEW *sv, LPCWSTR name )
283 {
284     UINT r, n=0;
285     MSIVIEW *table;
286
287     TRACE("%p adding %s\n", sv, debugstr_w( name ) );
288
289     if( sv->view.ops != &select_ops )
290         return ERROR_FUNCTION_FAILED;
291
292     table = sv->table;
293     if( !table )
294         return ERROR_FUNCTION_FAILED;
295     if( !table->ops->get_dimensions )
296         return ERROR_FUNCTION_FAILED;
297     if( !table->ops->get_column_info )
298         return ERROR_FUNCTION_FAILED;
299
300     if( sv->num_cols >= sv->max_cols )
301         return ERROR_FUNCTION_FAILED;
302
303     r = VIEW_find_column( table, name, &n );
304     if( r != ERROR_SUCCESS )
305         return r;
306
307     sv->cols[sv->num_cols] = n;
308     TRACE("Translating column %s from %d -> %d\n", 
309           debugstr_w( name ), sv->num_cols, n);
310
311     sv->num_cols++;
312
313     return ERROR_SUCCESS;
314 }
315
316 static int select_count_columns( const column_info *col )
317 {
318     int n;
319     for (n = 0; col; col = col->next)
320         n++;
321     return n;
322 }
323
324 UINT SELECT_CreateView( MSIDATABASE *db, MSIVIEW **view, MSIVIEW *table,
325                         const column_info *columns )
326 {
327     MSISELECTVIEW *sv = NULL;
328     UINT count = 0, r = ERROR_SUCCESS;
329
330     TRACE("%p\n", sv );
331
332     count = select_count_columns( columns );
333
334     sv = msi_alloc_zero( sizeof *sv + count*sizeof (UINT) );
335     if( !sv )
336         return ERROR_FUNCTION_FAILED;
337     
338     /* fill the structure */
339     sv->view.ops = &select_ops;
340     sv->db = db;
341     sv->table = table;
342     sv->num_cols = 0;
343     sv->max_cols = count;
344
345     while( columns )
346     {
347         r = SELECT_AddColumn( sv, columns->column );
348         if( r )
349             break;
350         columns = columns->next;
351     }
352
353     if( r == ERROR_SUCCESS )
354         *view = &sv->view;
355     else
356         msi_free( sv );
357
358     return r;
359 }