msi/tests: Initialize buffer lengths before calling MsiSourceListEnumMediaDisks.
[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_get_row( struct tagMSIVIEW *view, UINT row, MSIRECORD **rec )
86 {
87     MSISELECTVIEW *sv = (MSISELECTVIEW *)view;
88
89     TRACE("%p %d %p\n", sv, row, rec );
90
91     if( !sv->table )
92          return ERROR_FUNCTION_FAILED;
93
94     return msi_view_get_row(sv->db, view, row, rec);
95 }
96
97 static UINT SELECT_set_row( struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask )
98 {
99     MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
100     UINT i, expanded_mask = 0, r = ERROR_SUCCESS, col_count = 0;
101     MSIRECORD *expanded;
102
103     TRACE("%p %d %p %08x\n", sv, row, rec, mask );
104
105     if ( !sv->table )
106          return ERROR_FUNCTION_FAILED;
107
108     /* test if any of the mask bits are invalid */
109     if ( mask >= (1<<sv->num_cols) )
110         return ERROR_INVALID_PARAMETER;
111
112     /* find the number of columns in the table below */
113     r = sv->table->ops->get_dimensions( sv->table, NULL, &col_count );
114     if( r )
115         return r;
116
117     /* expand the record to the right size for the underlying table */
118     expanded = MSI_CreateRecord( col_count );
119     if ( !expanded )
120         return ERROR_FUNCTION_FAILED;
121
122     /* move the right fields across */
123     for ( i=0; i<sv->num_cols; i++ )
124     {
125         r = MSI_RecordCopyField( rec, i+1, expanded, sv->cols[ i ] );
126         if (r != ERROR_SUCCESS)
127             break;
128         expanded_mask |= (1<<(sv->cols[i]-1));
129     }
130
131     /* set the row in the underlying table */
132     if (r == ERROR_SUCCESS)
133         r = sv->table->ops->set_row( sv->table, row, expanded, expanded_mask );
134
135     msiobj_release( &expanded->hdr );
136     return r;
137 }
138
139 static UINT SELECT_insert_row( struct tagMSIVIEW *view, MSIRECORD *record, BOOL temporary )
140 {
141     MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
142     UINT i, table_cols, r;
143     MSIRECORD *outrec;
144
145     TRACE("%p %p\n", sv, record );
146
147     if ( !sv->table )
148         return ERROR_FUNCTION_FAILED;
149
150     /* rearrange the record to suit the table */
151     r = sv->table->ops->get_dimensions( sv->table, NULL, &table_cols );
152     if (r != ERROR_SUCCESS)
153         return r;
154
155     outrec = MSI_CreateRecord( table_cols + 1 );
156
157     for (i=0; i<sv->num_cols; i++)
158     {
159         r = MSI_RecordCopyField( record, i+1, outrec, sv->cols[i] );
160         if (r != ERROR_SUCCESS)
161             goto fail;
162     }
163
164     r = sv->table->ops->insert_row( sv->table, outrec, temporary );
165
166 fail:
167     msiobj_release( &outrec->hdr );
168
169     return r;
170 }
171
172 static UINT SELECT_execute( struct tagMSIVIEW *view, MSIRECORD *record )
173 {
174     MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
175
176     TRACE("%p %p\n", sv, record);
177
178     if( !sv->table )
179          return ERROR_FUNCTION_FAILED;
180
181     return sv->table->ops->execute( sv->table, record );
182 }
183
184 static UINT SELECT_close( struct tagMSIVIEW *view )
185 {
186     MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
187
188     TRACE("%p\n", sv );
189
190     if( !sv->table )
191          return ERROR_FUNCTION_FAILED;
192
193     return sv->table->ops->close( sv->table );
194 }
195
196 static UINT SELECT_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols )
197 {
198     MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
199
200     TRACE("%p %p %p\n", sv, rows, cols );
201
202     if( !sv->table )
203          return ERROR_FUNCTION_FAILED;
204
205     if( cols )
206         *cols = sv->num_cols;
207
208     return sv->table->ops->get_dimensions( sv->table, rows, NULL );
209 }
210
211 static UINT SELECT_get_column_info( struct tagMSIVIEW *view,
212                 UINT n, LPWSTR *name, UINT *type )
213 {
214     MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
215
216     TRACE("%p %d %p %p\n", sv, n, name, type );
217
218     if( !sv->table )
219          return ERROR_FUNCTION_FAILED;
220
221     if( (n==0) || (n>sv->num_cols) )
222          return ERROR_FUNCTION_FAILED;
223
224     n = sv->cols[ n - 1 ];
225
226     return sv->table->ops->get_column_info( sv->table, n, name, type );
227 }
228
229 static UINT msi_select_update(struct tagMSIVIEW *view, MSIRECORD *rec, UINT row)
230 {
231     MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
232     UINT r, i, num_columns, col, type, val;
233     LPWSTR name;
234     LPCWSTR str;
235     MSIRECORD *mod;
236
237     r = SELECT_get_dimensions(view, NULL, &num_columns);
238     if (r != ERROR_SUCCESS)
239         return r;
240
241     r = sv->table->ops->get_row(sv->table, row - 1, &mod);
242     if (r != ERROR_SUCCESS)
243         return r;
244
245     for (i = 0; i < num_columns; i++)
246     {
247         col = sv->cols[i];
248
249         r = SELECT_get_column_info(view, i + 1, &name, &type);
250         msi_free(name);
251         if (r != ERROR_SUCCESS)
252         {
253             ERR("Failed to get column information: %d\n", r);
254             goto done;
255         }
256
257         if (MSITYPE_IS_BINARY(type))
258         {
259             ERR("Cannot modify binary data!\n");
260             r = ERROR_FUNCTION_FAILED;
261             goto done;
262         }
263         else if (type & MSITYPE_STRING)
264         {
265             str = MSI_RecordGetString(rec, i + 1);
266             r = MSI_RecordSetStringW(mod, col, str);
267         }
268         else
269         {
270             val = MSI_RecordGetInteger(rec, i + 1);
271             r = MSI_RecordSetInteger(mod, col, val);
272         }
273
274         if (r != ERROR_SUCCESS)
275         {
276             ERR("Failed to modify record: %d\n", r);
277             goto done;
278         }
279     }
280
281     r = sv->table->ops->modify(sv->table, MSIMODIFY_UPDATE, mod, row);
282
283 done:
284     msiobj_release(&mod->hdr);
285     return r;
286 }
287
288 static UINT SELECT_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
289                            MSIRECORD *rec, UINT row )
290 {
291     MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
292
293     TRACE("%p %d %p %d\n", sv, eModifyMode, rec, row );
294
295     if( !sv->table )
296          return ERROR_FUNCTION_FAILED;
297
298     if (eModifyMode == MSIMODIFY_UPDATE)
299         return msi_select_update(view, rec, row);
300
301     return sv->table->ops->modify( sv->table, eModifyMode, rec, row );
302 }
303
304 static UINT SELECT_delete( struct tagMSIVIEW *view )
305 {
306     MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
307
308     TRACE("%p\n", sv );
309
310     if( sv->table )
311         sv->table->ops->delete( sv->table );
312     sv->table = NULL;
313
314     msi_free( sv );
315
316     return ERROR_SUCCESS;
317 }
318
319 static UINT SELECT_find_matching_rows( struct tagMSIVIEW *view, UINT col,
320     UINT val, UINT *row, MSIITERHANDLE *handle )
321 {
322     MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
323
324     TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
325
326     if( !sv->table )
327          return ERROR_FUNCTION_FAILED;
328
329     if( (col==0) || (col>sv->num_cols) )
330          return ERROR_FUNCTION_FAILED;
331
332     col = sv->cols[ col - 1 ];
333
334     return sv->table->ops->find_matching_rows( sv->table, col, val, row, handle );
335 }
336
337 static UINT SELECT_sort(struct tagMSIVIEW *view, column_info *columns)
338 {
339     MSISELECTVIEW *sv = (MSISELECTVIEW *)view;
340
341     TRACE("%p %p\n", view, columns);
342
343     return sv->table->ops->sort( sv->table, columns );
344 }
345
346 static const MSIVIEWOPS select_ops =
347 {
348     SELECT_fetch_int,
349     SELECT_fetch_stream,
350     SELECT_get_row,
351     SELECT_set_row,
352     SELECT_insert_row,
353     NULL,
354     SELECT_execute,
355     SELECT_close,
356     SELECT_get_dimensions,
357     SELECT_get_column_info,
358     SELECT_modify,
359     SELECT_delete,
360     SELECT_find_matching_rows,
361     NULL,
362     NULL,
363     NULL,
364     NULL,
365     SELECT_sort,
366 };
367
368 static UINT SELECT_AddColumn( MSISELECTVIEW *sv, LPCWSTR name )
369 {
370     UINT r, n=0;
371     MSIVIEW *table;
372
373     TRACE("%p adding %s\n", sv, debugstr_w( name ) );
374
375     if( sv->view.ops != &select_ops )
376         return ERROR_FUNCTION_FAILED;
377
378     table = sv->table;
379     if( !table )
380         return ERROR_FUNCTION_FAILED;
381     if( !table->ops->get_dimensions )
382         return ERROR_FUNCTION_FAILED;
383     if( !table->ops->get_column_info )
384         return ERROR_FUNCTION_FAILED;
385
386     if( sv->num_cols >= sv->max_cols )
387         return ERROR_FUNCTION_FAILED;
388
389     r = VIEW_find_column( table, name, &n );
390     if( r != ERROR_SUCCESS )
391         return r;
392
393     sv->cols[sv->num_cols] = n;
394     TRACE("Translating column %s from %d -> %d\n", 
395           debugstr_w( name ), sv->num_cols, n);
396
397     sv->num_cols++;
398
399     return ERROR_SUCCESS;
400 }
401
402 static int select_count_columns( const column_info *col )
403 {
404     int n;
405     for (n = 0; col; col = col->next)
406         n++;
407     return n;
408 }
409
410 UINT SELECT_CreateView( MSIDATABASE *db, MSIVIEW **view, MSIVIEW *table,
411                         const column_info *columns )
412 {
413     MSISELECTVIEW *sv = NULL;
414     UINT count = 0, r = ERROR_SUCCESS;
415
416     TRACE("%p\n", sv );
417
418     count = select_count_columns( columns );
419
420     sv = msi_alloc_zero( sizeof *sv + count*sizeof (UINT) );
421     if( !sv )
422         return ERROR_FUNCTION_FAILED;
423     
424     /* fill the structure */
425     sv->view.ops = &select_ops;
426     sv->db = db;
427     sv->table = table;
428     sv->num_cols = 0;
429     sv->max_cols = count;
430
431     while( columns )
432     {
433         r = SELECT_AddColumn( sv, columns->column );
434         if( r )
435             break;
436         columns = columns->next;
437     }
438
439     if( r == ERROR_SUCCESS )
440         *view = &sv->view;
441     else
442         msi_free( sv );
443
444     return r;
445 }