sync with trunk r46493
[reactos.git] / dll / win32 / msi / join.c
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
3 *
4 * Copyright 2006 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 "msi.h"
27 #include "msiquery.h"
28 #include "objbase.h"
29 #include "objidl.h"
30 #include "msipriv.h"
31 #include "query.h"
32
33 #include "wine/debug.h"
34 #include "wine/unicode.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
37
38 typedef struct tagJOINTABLE
39 {
40 struct list entry;
41 MSIVIEW *view;
42 UINT columns;
43 UINT rows;
44 UINT next_rows;
45 } JOINTABLE;
46
47 typedef struct tagMSIJOINVIEW
48 {
49 MSIVIEW view;
50 MSIDATABASE *db;
51 struct list tables;
52 UINT columns;
53 UINT rows;
54 } MSIJOINVIEW;
55
56 static UINT JOIN_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
57 {
58 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
59 JOINTABLE *table;
60 UINT cols = 0;
61 UINT prev_rows = 1;
62
63 TRACE("%d, %d\n", row, col);
64
65 if (col == 0 || col > jv->columns)
66 return ERROR_FUNCTION_FAILED;
67
68 if (row >= jv->rows)
69 return ERROR_FUNCTION_FAILED;
70
71 LIST_FOR_EACH_ENTRY(table, &jv->tables, JOINTABLE, entry)
72 {
73 if (col <= cols + table->columns)
74 {
75 row = (row % (jv->rows / table->next_rows)) / prev_rows;
76 col -= cols;
77 break;
78 }
79
80 prev_rows *= table->rows;
81 cols += table->columns;
82 }
83
84 return table->view->ops->fetch_int( table->view, row, col, val );
85 }
86
87 static UINT JOIN_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm)
88 {
89 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
90 JOINTABLE *table;
91 UINT cols = 0;
92 UINT prev_rows = 1;
93
94 TRACE("%p %d %d %p\n", jv, row, col, stm );
95
96 if (col == 0 || col > jv->columns)
97 return ERROR_FUNCTION_FAILED;
98
99 if (row >= jv->rows)
100 return ERROR_FUNCTION_FAILED;
101
102 LIST_FOR_EACH_ENTRY(table, &jv->tables, JOINTABLE, entry)
103 {
104 if (col <= cols + table->columns)
105 {
106 row = (row % (jv->rows / table->next_rows)) / prev_rows;
107 col -= cols;
108 break;
109 }
110
111 prev_rows *= table->rows;
112 cols += table->columns;
113 }
114
115 return table->view->ops->fetch_stream( table->view, row, col, stm );
116 }
117
118 static UINT JOIN_get_row( struct tagMSIVIEW *view, UINT row, MSIRECORD **rec )
119 {
120 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
121
122 TRACE("%p %d %p\n", jv, row, rec);
123
124 return msi_view_get_row( jv->db, view, row, rec );
125 }
126
127 static UINT JOIN_execute( struct tagMSIVIEW *view, MSIRECORD *record )
128 {
129 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
130 JOINTABLE *table;
131 UINT r, rows;
132
133 TRACE("%p %p\n", jv, record);
134
135 LIST_FOR_EACH_ENTRY(table, &jv->tables, JOINTABLE, entry)
136 {
137 table->view->ops->execute(table->view, NULL);
138
139 r = table->view->ops->get_dimensions(table->view, &table->rows, NULL);
140 if (r != ERROR_SUCCESS)
141 {
142 ERR("failed to get table dimensions\n");
143 return r;
144 }
145
146 /* each table must have at least one row */
147 if (table->rows == 0)
148 {
149 jv->rows = 0;
150 return ERROR_SUCCESS;
151 }
152
153 if (jv->rows == 0)
154 jv->rows = table->rows;
155 else
156 jv->rows *= table->rows;
157 }
158
159 rows = jv->rows;
160 LIST_FOR_EACH_ENTRY(table, &jv->tables, JOINTABLE, entry)
161 {
162 rows /= table->rows;
163 table->next_rows = rows;
164 }
165
166 return ERROR_SUCCESS;
167 }
168
169 static UINT JOIN_close( struct tagMSIVIEW *view )
170 {
171 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
172 JOINTABLE *table;
173
174 TRACE("%p\n", jv );
175
176 LIST_FOR_EACH_ENTRY(table, &jv->tables, JOINTABLE, entry)
177 {
178 table->view->ops->close(table->view);
179 }
180
181 return ERROR_SUCCESS;
182 }
183
184 static UINT JOIN_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols )
185 {
186 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
187
188 TRACE("%p %p %p\n", jv, rows, cols );
189
190 if (cols)
191 *cols = jv->columns;
192
193 if (rows)
194 *rows = jv->rows;
195
196 return ERROR_SUCCESS;
197 }
198
199 static UINT JOIN_get_column_info( struct tagMSIVIEW *view,
200 UINT n, LPWSTR *name, UINT *type, BOOL *temporary,
201 LPWSTR *table_name )
202 {
203 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
204 JOINTABLE *table;
205 UINT cols = 0;
206
207 TRACE("%p %d %p %p %p %p\n", jv, n, name, type, temporary, table_name );
208
209 if (n == 0 || n > jv->columns)
210 return ERROR_FUNCTION_FAILED;
211
212 LIST_FOR_EACH_ENTRY(table, &jv->tables, JOINTABLE, entry)
213 {
214 if (n <= cols + table->columns)
215 return table->view->ops->get_column_info(table->view, n - cols,
216 name, type, temporary,
217 table_name);
218
219 cols += table->columns;
220 }
221
222 return ERROR_FUNCTION_FAILED;
223 }
224
225 static UINT join_find_row( MSIJOINVIEW *jv, MSIRECORD *rec, UINT *row )
226 {
227 LPCWSTR str;
228 UINT i, id, data;
229
230 str = MSI_RecordGetString( rec, 1 );
231 msi_string2idW( jv->db->strings, str, &id );
232
233 for (i = 0; i < jv->rows; i++)
234 {
235 JOIN_fetch_int( &jv->view, i, 1, &data );
236
237 if (data == id)
238 {
239 *row = i;
240 return ERROR_SUCCESS;
241 }
242 }
243
244 return ERROR_FUNCTION_FAILED;
245 }
246
247 static UINT JOIN_set_row( struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask )
248 {
249 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
250 JOINTABLE *table;
251 UINT i, reduced_mask = 0, r = ERROR_SUCCESS, offset = 0, col_count;
252 MSIRECORD *reduced;
253
254 TRACE("%p %d %p %u %08x\n", jv, row, rec, rec->count, mask );
255
256 if (mask >= 1 << jv->columns)
257 return ERROR_INVALID_PARAMETER;
258
259 LIST_FOR_EACH_ENTRY(table, &jv->tables, JOINTABLE, entry)
260 {
261 r = table->view->ops->get_dimensions( table->view, NULL, &col_count );
262 if (r != ERROR_SUCCESS)
263 return r;
264
265 reduced = MSI_CreateRecord( col_count );
266 if (!reduced)
267 return ERROR_FUNCTION_FAILED;
268
269 for (i = 0; i < col_count; i++)
270 {
271 r = MSI_RecordCopyField( rec, i + offset + 1, reduced, i + 1 );
272 if (r != ERROR_SUCCESS)
273 break;
274 }
275
276 offset += col_count;
277 reduced_mask = mask >> (jv->columns - offset) & ((1 << col_count) - 1);
278
279 if (r == ERROR_SUCCESS)
280 r = table->view->ops->set_row( table->view, row, reduced, reduced_mask );
281
282 msiobj_release( &reduced->hdr );
283 }
284
285 return r;
286 }
287
288 static UINT join_modify_update( struct tagMSIVIEW *view, MSIRECORD *rec )
289 {
290 MSIJOINVIEW *jv = (MSIJOINVIEW *)view;
291 UINT r, row;
292
293 r = join_find_row( jv, rec, &row );
294 if (r != ERROR_SUCCESS)
295 return r;
296
297 return JOIN_set_row( view, row, rec, (1 << jv->columns) - 1 );
298 }
299
300 static UINT JOIN_modify( struct tagMSIVIEW *view, MSIMODIFY mode, MSIRECORD *rec, UINT row )
301 {
302 UINT r;
303
304 TRACE("%p %d %p %u\n", view, mode, rec, row);
305
306 switch (mode)
307 {
308 case MSIMODIFY_UPDATE:
309 return join_modify_update( view, rec );
310
311 case MSIMODIFY_ASSIGN:
312 case MSIMODIFY_DELETE:
313 case MSIMODIFY_INSERT:
314 case MSIMODIFY_INSERT_TEMPORARY:
315 case MSIMODIFY_MERGE:
316 case MSIMODIFY_REPLACE:
317 case MSIMODIFY_SEEK:
318 case MSIMODIFY_VALIDATE:
319 case MSIMODIFY_VALIDATE_DELETE:
320 case MSIMODIFY_VALIDATE_FIELD:
321 case MSIMODIFY_VALIDATE_NEW:
322 r = ERROR_FUNCTION_FAILED;
323 break;
324
325 case MSIMODIFY_REFRESH:
326 r = ERROR_CALL_NOT_IMPLEMENTED;
327 break;
328
329 default:
330 WARN("%p %d %p %u - unknown mode\n", view, mode, rec, row );
331 r = ERROR_INVALID_PARAMETER;
332 break;
333 }
334
335 return r;
336 }
337
338 static UINT JOIN_delete( struct tagMSIVIEW *view )
339 {
340 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
341 struct list *item, *cursor;
342
343 TRACE("%p\n", jv );
344
345 LIST_FOR_EACH_SAFE(item, cursor, &jv->tables)
346 {
347 JOINTABLE* table = LIST_ENTRY(item, JOINTABLE, entry);
348
349 list_remove(&table->entry);
350 table->view->ops->delete(table->view);
351 table->view = NULL;
352 msi_free(table);
353 }
354
355 msi_free(jv);
356
357 return ERROR_SUCCESS;
358 }
359
360 static UINT JOIN_find_matching_rows( struct tagMSIVIEW *view, UINT col,
361 UINT val, UINT *row, MSIITERHANDLE *handle )
362 {
363 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
364 UINT i, row_value;
365
366 TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
367
368 if (col == 0 || col > jv->columns)
369 return ERROR_INVALID_PARAMETER;
370
371 for (i = PtrToUlong(*handle); i < jv->rows; i++)
372 {
373 if (view->ops->fetch_int( view, i, col, &row_value ) != ERROR_SUCCESS)
374 continue;
375
376 if (row_value == val)
377 {
378 *row = i;
379 (*(UINT *)handle) = i + 1;
380 return ERROR_SUCCESS;
381 }
382 }
383
384 return ERROR_NO_MORE_ITEMS;
385 }
386
387 static UINT JOIN_sort(struct tagMSIVIEW *view, column_info *columns)
388 {
389 MSIJOINVIEW *jv = (MSIJOINVIEW *)view;
390 JOINTABLE *table;
391 UINT r;
392
393 TRACE("%p %p\n", view, columns);
394
395 LIST_FOR_EACH_ENTRY(table, &jv->tables, JOINTABLE, entry)
396 {
397 r = table->view->ops->sort(table->view, columns);
398 if (r != ERROR_SUCCESS)
399 return r;
400 }
401
402 return ERROR_SUCCESS;
403 }
404
405 static const MSIVIEWOPS join_ops =
406 {
407 JOIN_fetch_int,
408 JOIN_fetch_stream,
409 JOIN_get_row,
410 NULL,
411 NULL,
412 NULL,
413 JOIN_execute,
414 JOIN_close,
415 JOIN_get_dimensions,
416 JOIN_get_column_info,
417 JOIN_modify,
418 JOIN_delete,
419 JOIN_find_matching_rows,
420 NULL,
421 NULL,
422 NULL,
423 NULL,
424 JOIN_sort,
425 NULL,
426 };
427
428 UINT JOIN_CreateView( MSIDATABASE *db, MSIVIEW **view, LPWSTR tables )
429 {
430 MSIJOINVIEW *jv = NULL;
431 UINT r = ERROR_SUCCESS;
432 JOINTABLE *table;
433 LPWSTR ptr;
434
435 TRACE("%p (%s)\n", jv, debugstr_w(tables) );
436
437 jv = msi_alloc_zero( sizeof *jv );
438 if( !jv )
439 return ERROR_FUNCTION_FAILED;
440
441 /* fill the structure */
442 jv->view.ops = &join_ops;
443 jv->db = db;
444 jv->columns = 0;
445 jv->rows = 0;
446
447 list_init(&jv->tables);
448
449 while (*tables)
450 {
451 if ((ptr = strchrW(tables, ' ')))
452 *ptr = '\0';
453
454 table = msi_alloc(sizeof(JOINTABLE));
455 if (!table)
456 {
457 r = ERROR_OUTOFMEMORY;
458 goto end;
459 }
460
461 r = TABLE_CreateView( db, tables, &table->view );
462 if( r != ERROR_SUCCESS )
463 {
464 WARN("can't create table: %s\n", debugstr_w(tables));
465 msi_free(table);
466 r = ERROR_BAD_QUERY_SYNTAX;
467 goto end;
468 }
469
470 r = table->view->ops->get_dimensions( table->view, NULL,
471 &table->columns );
472 if( r != ERROR_SUCCESS )
473 {
474 ERR("can't get table dimensions\n");
475 goto end;
476 }
477
478 jv->columns += table->columns;
479
480 list_add_head( &jv->tables, &table->entry );
481
482 if (!ptr)
483 break;
484
485 tables = ptr + 1;
486 }
487
488 *view = &jv->view;
489 return ERROR_SUCCESS;
490
491 end:
492 jv->view.ops->delete( &jv->view );
493
494 return r;
495 }