cc6321662f44448b02d92fc55e000ea069dde472
[reactos.git] / reactos / dll / win32 / 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 #define WIN32_NO_STATUS
22 #define _INC_WINDOWS
23 #define COM_NO_WINDOWS_H
24
25 //#include <stdarg.h>
26
27 //#include "windef.h"
28 //#include "winbase.h"
29 //#include "winerror.h"
30 #include <wine/debug.h>
31 //#include "msi.h"
32 //#include "msiquery.h"
33 //#include "objbase.h"
34 //#include "objidl.h"
35 #include "msipriv.h"
36 //#include "winnls.h"
37
38 //#include "query.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
41
42
43 /* below is the query interface to a table */
44
45 typedef struct tagMSISELECTVIEW
46 {
47 MSIVIEW view;
48 MSIDATABASE *db;
49 MSIVIEW *table;
50 UINT num_cols;
51 UINT max_cols;
52 UINT cols[1];
53 } MSISELECTVIEW;
54
55 static UINT SELECT_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
56 {
57 MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
58
59 TRACE("%p %d %d %p\n", sv, row, col, val );
60
61 if( !sv->table )
62 return ERROR_FUNCTION_FAILED;
63
64 if( !col || col > sv->num_cols )
65 return ERROR_FUNCTION_FAILED;
66
67 col = sv->cols[ col - 1 ];
68 if( !col )
69 {
70 *val = 0;
71 return ERROR_SUCCESS;
72 }
73 return sv->table->ops->fetch_int( sv->table, row, col, val );
74 }
75
76 static UINT SELECT_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm)
77 {
78 MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
79
80 TRACE("%p %d %d %p\n", sv, row, col, stm );
81
82 if( !sv->table )
83 return ERROR_FUNCTION_FAILED;
84
85 if( !col || col > sv->num_cols )
86 return ERROR_FUNCTION_FAILED;
87
88 col = sv->cols[ col - 1 ];
89 if( !col )
90 {
91 *stm = NULL;
92 return ERROR_SUCCESS;
93 }
94 return sv->table->ops->fetch_stream( sv->table, row, col, stm );
95 }
96
97 static UINT SELECT_get_row( struct tagMSIVIEW *view, UINT row, MSIRECORD **rec )
98 {
99 MSISELECTVIEW *sv = (MSISELECTVIEW *)view;
100
101 TRACE("%p %d %p\n", sv, row, rec );
102
103 if( !sv->table )
104 return ERROR_FUNCTION_FAILED;
105
106 return msi_view_get_row(sv->db, view, row, rec);
107 }
108
109 static UINT SELECT_set_row( struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask )
110 {
111 MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
112 UINT i, expanded_mask = 0, r = ERROR_SUCCESS, col_count = 0;
113 MSIRECORD *expanded;
114
115 TRACE("%p %d %p %08x\n", sv, row, rec, mask );
116
117 if ( !sv->table )
118 return ERROR_FUNCTION_FAILED;
119
120 /* test if any of the mask bits are invalid */
121 if ( mask >= (1<<sv->num_cols) )
122 return ERROR_INVALID_PARAMETER;
123
124 /* find the number of columns in the table below */
125 r = sv->table->ops->get_dimensions( sv->table, NULL, &col_count );
126 if( r )
127 return r;
128
129 /* expand the record to the right size for the underlying table */
130 expanded = MSI_CreateRecord( col_count );
131 if ( !expanded )
132 return ERROR_FUNCTION_FAILED;
133
134 /* move the right fields across */
135 for ( i=0; i<sv->num_cols; i++ )
136 {
137 r = MSI_RecordCopyField( rec, i+1, expanded, sv->cols[ i ] );
138 if (r != ERROR_SUCCESS)
139 break;
140 expanded_mask |= (1<<(sv->cols[i]-1));
141 }
142
143 /* set the row in the underlying table */
144 if (r == ERROR_SUCCESS)
145 r = sv->table->ops->set_row( sv->table, row, expanded, expanded_mask );
146
147 msiobj_release( &expanded->hdr );
148 return r;
149 }
150
151 static UINT SELECT_insert_row( struct tagMSIVIEW *view, MSIRECORD *record, UINT row, BOOL temporary )
152 {
153 MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
154 UINT i, table_cols, r;
155 MSIRECORD *outrec;
156
157 TRACE("%p %p\n", sv, record );
158
159 if ( !sv->table )
160 return ERROR_FUNCTION_FAILED;
161
162 /* rearrange the record to suit the table */
163 r = sv->table->ops->get_dimensions( sv->table, NULL, &table_cols );
164 if (r != ERROR_SUCCESS)
165 return r;
166
167 outrec = MSI_CreateRecord( table_cols + 1 );
168
169 for (i=0; i<sv->num_cols; i++)
170 {
171 r = MSI_RecordCopyField( record, i+1, outrec, sv->cols[i] );
172 if (r != ERROR_SUCCESS)
173 goto fail;
174 }
175
176 r = sv->table->ops->insert_row( sv->table, outrec, row, temporary );
177
178 fail:
179 msiobj_release( &outrec->hdr );
180
181 return r;
182 }
183
184 static UINT SELECT_execute( struct tagMSIVIEW *view, MSIRECORD *record )
185 {
186 MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
187
188 TRACE("%p %p\n", sv, record);
189
190 if( !sv->table )
191 return ERROR_FUNCTION_FAILED;
192
193 return sv->table->ops->execute( sv->table, record );
194 }
195
196 static UINT SELECT_close( struct tagMSIVIEW *view )
197 {
198 MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
199
200 TRACE("%p\n", sv );
201
202 if( !sv->table )
203 return ERROR_FUNCTION_FAILED;
204
205 return sv->table->ops->close( sv->table );
206 }
207
208 static UINT SELECT_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols )
209 {
210 MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
211
212 TRACE("%p %p %p\n", sv, rows, cols );
213
214 if( !sv->table )
215 return ERROR_FUNCTION_FAILED;
216
217 if( cols )
218 *cols = sv->num_cols;
219
220 return sv->table->ops->get_dimensions( sv->table, rows, NULL );
221 }
222
223 static UINT SELECT_get_column_info( struct tagMSIVIEW *view, UINT n, LPCWSTR *name,
224 UINT *type, BOOL *temporary, LPCWSTR *table_name )
225 {
226 MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
227
228 TRACE("%p %d %p %p %p %p\n", sv, n, name, type, temporary, table_name );
229
230 if( !sv->table )
231 return ERROR_FUNCTION_FAILED;
232
233 if( !n || n > sv->num_cols )
234 return ERROR_FUNCTION_FAILED;
235
236 n = sv->cols[ n - 1 ];
237 if( !n )
238 {
239 if (name) *name = szEmpty;
240 if (type) *type = MSITYPE_UNKNOWN | MSITYPE_VALID;
241 if (temporary) *temporary = FALSE;
242 if (table_name) *table_name = szEmpty;
243 return ERROR_SUCCESS;
244 }
245 return sv->table->ops->get_column_info( sv->table, n, name,
246 type, temporary, table_name );
247 }
248
249 static UINT msi_select_update(struct tagMSIVIEW *view, MSIRECORD *rec, UINT row)
250 {
251 MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
252 UINT r, i, num_columns, col, type, val;
253 LPCWSTR str;
254 MSIRECORD *mod;
255
256 r = SELECT_get_dimensions(view, NULL, &num_columns);
257 if (r != ERROR_SUCCESS)
258 return r;
259
260 r = sv->table->ops->get_row(sv->table, row - 1, &mod);
261 if (r != ERROR_SUCCESS)
262 return r;
263
264 for (i = 0; i < num_columns; i++)
265 {
266 col = sv->cols[i];
267
268 r = SELECT_get_column_info(view, i + 1, NULL, &type, NULL, NULL);
269 if (r != ERROR_SUCCESS)
270 {
271 ERR("Failed to get column information: %d\n", r);
272 goto done;
273 }
274
275 if (MSITYPE_IS_BINARY(type))
276 {
277 ERR("Cannot modify binary data!\n");
278 r = ERROR_FUNCTION_FAILED;
279 goto done;
280 }
281 else if (type & MSITYPE_STRING)
282 {
283 int len;
284 str = msi_record_get_string( rec, i + 1, &len );
285 r = msi_record_set_string( mod, col, str, len );
286 }
287 else
288 {
289 val = MSI_RecordGetInteger(rec, i + 1);
290 r = MSI_RecordSetInteger(mod, col, val);
291 }
292
293 if (r != ERROR_SUCCESS)
294 {
295 ERR("Failed to modify record: %d\n", r);
296 goto done;
297 }
298 }
299
300 r = sv->table->ops->modify(sv->table, MSIMODIFY_UPDATE, mod, row);
301
302 done:
303 msiobj_release(&mod->hdr);
304 return r;
305 }
306
307 static UINT SELECT_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
308 MSIRECORD *rec, UINT row )
309 {
310 MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
311
312 TRACE("%p %d %p %d\n", sv, eModifyMode, rec, row );
313
314 if( !sv->table )
315 return ERROR_FUNCTION_FAILED;
316
317 if (eModifyMode == MSIMODIFY_UPDATE)
318 return msi_select_update(view, rec, row);
319
320 return sv->table->ops->modify( sv->table, eModifyMode, rec, row );
321 }
322
323 static UINT SELECT_delete( struct tagMSIVIEW *view )
324 {
325 MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
326
327 TRACE("%p\n", sv );
328
329 if( sv->table )
330 sv->table->ops->delete( sv->table );
331 sv->table = NULL;
332
333 msi_free( sv );
334
335 return ERROR_SUCCESS;
336 }
337
338 static UINT SELECT_find_matching_rows( struct tagMSIVIEW *view, UINT col,
339 UINT val, UINT *row, MSIITERHANDLE *handle )
340 {
341 MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
342
343 TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
344
345 if( !sv->table )
346 return ERROR_FUNCTION_FAILED;
347
348 if( (col==0) || (col>sv->num_cols) )
349 return ERROR_FUNCTION_FAILED;
350
351 col = sv->cols[ col - 1 ];
352
353 return sv->table->ops->find_matching_rows( sv->table, col, val, row, handle );
354 }
355
356
357 static const MSIVIEWOPS select_ops =
358 {
359 SELECT_fetch_int,
360 SELECT_fetch_stream,
361 SELECT_get_row,
362 SELECT_set_row,
363 SELECT_insert_row,
364 NULL,
365 SELECT_execute,
366 SELECT_close,
367 SELECT_get_dimensions,
368 SELECT_get_column_info,
369 SELECT_modify,
370 SELECT_delete,
371 SELECT_find_matching_rows,
372 NULL,
373 NULL,
374 NULL,
375 NULL,
376 NULL,
377 NULL,
378 };
379
380 static UINT SELECT_AddColumn( MSISELECTVIEW *sv, LPCWSTR name,
381 LPCWSTR table_name )
382 {
383 UINT r, n;
384 MSIVIEW *table;
385
386 TRACE("%p adding %s.%s\n", sv, debugstr_w( table_name ),
387 debugstr_w( name ));
388
389 if( sv->view.ops != &select_ops )
390 return ERROR_FUNCTION_FAILED;
391
392 table = sv->table;
393 if( !table )
394 return ERROR_FUNCTION_FAILED;
395 if( !table->ops->get_dimensions )
396 return ERROR_FUNCTION_FAILED;
397 if( !table->ops->get_column_info )
398 return ERROR_FUNCTION_FAILED;
399
400 if( sv->num_cols >= sv->max_cols )
401 return ERROR_FUNCTION_FAILED;
402
403 if ( !name[0] ) n = 0;
404 else
405 {
406 r = VIEW_find_column( table, name, table_name, &n );
407 if( r != ERROR_SUCCESS )
408 return r;
409 }
410
411 sv->cols[sv->num_cols] = n;
412 TRACE("Translating column %s from %d -> %d\n",
413 debugstr_w( name ), sv->num_cols, n);
414
415 sv->num_cols++;
416
417 return ERROR_SUCCESS;
418 }
419
420 static int select_count_columns( const column_info *col )
421 {
422 int n;
423 for (n = 0; col; col = col->next)
424 n++;
425 return n;
426 }
427
428 UINT SELECT_CreateView( MSIDATABASE *db, MSIVIEW **view, MSIVIEW *table,
429 const column_info *columns )
430 {
431 MSISELECTVIEW *sv = NULL;
432 UINT count = 0, r = ERROR_SUCCESS;
433
434 TRACE("%p\n", sv );
435
436 count = select_count_columns( columns );
437
438 sv = msi_alloc_zero( FIELD_OFFSET( MSISELECTVIEW, cols[count] ));
439 if( !sv )
440 return ERROR_FUNCTION_FAILED;
441
442 /* fill the structure */
443 sv->view.ops = &select_ops;
444 sv->db = db;
445 sv->table = table;
446 sv->num_cols = 0;
447 sv->max_cols = count;
448
449 while( columns )
450 {
451 r = SELECT_AddColumn( sv, columns->column, columns->table );
452 if( r )
453 break;
454 columns = columns->next;
455 }
456
457 if( r == ERROR_SUCCESS )
458 *view = &sv->view;
459 else
460 msi_free( sv );
461
462 return r;
463 }