sync msi with wine 1.1.35
[reactos.git] / reactos / 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 FIXME("(%p, %d, %p): stub!\n", view, row, rec);
121 return ERROR_FUNCTION_FAILED;
122 }
123
124 static UINT JOIN_execute( struct tagMSIVIEW *view, MSIRECORD *record )
125 {
126 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
127 JOINTABLE *table;
128 UINT r, rows;
129
130 TRACE("%p %p\n", jv, record);
131
132 LIST_FOR_EACH_ENTRY(table, &jv->tables, JOINTABLE, entry)
133 {
134 table->view->ops->execute(table->view, NULL);
135
136 r = table->view->ops->get_dimensions(table->view, &table->rows, NULL);
137 if (r != ERROR_SUCCESS)
138 {
139 ERR("failed to get table dimensions\n");
140 return r;
141 }
142
143 /* each table must have at least one row */
144 if (table->rows == 0)
145 {
146 jv->rows = 0;
147 return ERROR_SUCCESS;
148 }
149
150 if (jv->rows == 0)
151 jv->rows = table->rows;
152 else
153 jv->rows *= table->rows;
154 }
155
156 rows = jv->rows;
157 LIST_FOR_EACH_ENTRY(table, &jv->tables, JOINTABLE, entry)
158 {
159 rows /= table->rows;
160 table->next_rows = rows;
161 }
162
163 return ERROR_SUCCESS;
164 }
165
166 static UINT JOIN_close( struct tagMSIVIEW *view )
167 {
168 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
169 JOINTABLE *table;
170
171 TRACE("%p\n", jv );
172
173 LIST_FOR_EACH_ENTRY(table, &jv->tables, JOINTABLE, entry)
174 {
175 table->view->ops->close(table->view);
176 }
177
178 return ERROR_SUCCESS;
179 }
180
181 static UINT JOIN_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols )
182 {
183 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
184
185 TRACE("%p %p %p\n", jv, rows, cols );
186
187 if (cols)
188 *cols = jv->columns;
189
190 if (rows)
191 *rows = jv->rows;
192
193 return ERROR_SUCCESS;
194 }
195
196 static UINT JOIN_get_column_info( struct tagMSIVIEW *view,
197 UINT n, LPWSTR *name, UINT *type, BOOL *temporary,
198 LPWSTR *table_name )
199 {
200 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
201 JOINTABLE *table;
202 UINT cols = 0;
203
204 TRACE("%p %d %p %p %p %p\n", jv, n, name, type, temporary, table_name );
205
206 if (n == 0 || n > jv->columns)
207 return ERROR_FUNCTION_FAILED;
208
209 LIST_FOR_EACH_ENTRY(table, &jv->tables, JOINTABLE, entry)
210 {
211 if (n <= cols + table->columns)
212 return table->view->ops->get_column_info(table->view, n - cols,
213 name, type, temporary,
214 table_name);
215
216 cols += table->columns;
217 }
218
219 return ERROR_FUNCTION_FAILED;
220 }
221
222 static UINT JOIN_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
223 MSIRECORD *rec, UINT row )
224 {
225 TRACE("%p %d %p\n", view, eModifyMode, rec);
226 return ERROR_FUNCTION_FAILED;
227 }
228
229 static UINT JOIN_delete( struct tagMSIVIEW *view )
230 {
231 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
232 struct list *item, *cursor;
233
234 TRACE("%p\n", jv );
235
236 LIST_FOR_EACH_SAFE(item, cursor, &jv->tables)
237 {
238 JOINTABLE* table = LIST_ENTRY(item, JOINTABLE, entry);
239
240 list_remove(&table->entry);
241 table->view->ops->delete(table->view);
242 table->view = NULL;
243 msi_free(table);
244 }
245
246 msi_free(jv);
247
248 return ERROR_SUCCESS;
249 }
250
251 static UINT JOIN_find_matching_rows( struct tagMSIVIEW *view, UINT col,
252 UINT val, UINT *row, MSIITERHANDLE *handle )
253 {
254 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
255 UINT i, row_value;
256
257 TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
258
259 if (col == 0 || col > jv->columns)
260 return ERROR_INVALID_PARAMETER;
261
262 for (i = (UINT)*handle; i < jv->rows; i++)
263 {
264 if (view->ops->fetch_int( view, i, col, &row_value ) != ERROR_SUCCESS)
265 continue;
266
267 if (row_value == val)
268 {
269 *row = i;
270 (*(UINT *)handle) = i + 1;
271 return ERROR_SUCCESS;
272 }
273 }
274
275 return ERROR_NO_MORE_ITEMS;
276 }
277
278 static UINT JOIN_sort(struct tagMSIVIEW *view, column_info *columns)
279 {
280 MSIJOINVIEW *jv = (MSIJOINVIEW *)view;
281 JOINTABLE *table;
282 UINT r;
283
284 TRACE("%p %p\n", view, columns);
285
286 LIST_FOR_EACH_ENTRY(table, &jv->tables, JOINTABLE, entry)
287 {
288 r = table->view->ops->sort(table->view, columns);
289 if (r != ERROR_SUCCESS)
290 return r;
291 }
292
293 return ERROR_SUCCESS;
294 }
295
296 static const MSIVIEWOPS join_ops =
297 {
298 JOIN_fetch_int,
299 JOIN_fetch_stream,
300 JOIN_get_row,
301 NULL,
302 NULL,
303 NULL,
304 JOIN_execute,
305 JOIN_close,
306 JOIN_get_dimensions,
307 JOIN_get_column_info,
308 JOIN_modify,
309 JOIN_delete,
310 JOIN_find_matching_rows,
311 NULL,
312 NULL,
313 NULL,
314 NULL,
315 JOIN_sort,
316 NULL,
317 };
318
319 UINT JOIN_CreateView( MSIDATABASE *db, MSIVIEW **view, LPWSTR tables )
320 {
321 MSIJOINVIEW *jv = NULL;
322 UINT r = ERROR_SUCCESS;
323 JOINTABLE *table;
324 LPWSTR ptr;
325
326 TRACE("%p (%s)\n", jv, debugstr_w(tables) );
327
328 jv = msi_alloc_zero( sizeof *jv );
329 if( !jv )
330 return ERROR_FUNCTION_FAILED;
331
332 /* fill the structure */
333 jv->view.ops = &join_ops;
334 jv->db = db;
335 jv->columns = 0;
336 jv->rows = 0;
337
338 list_init(&jv->tables);
339
340 while (*tables)
341 {
342 if ((ptr = strchrW(tables, ' ')))
343 *ptr = '\0';
344
345 table = msi_alloc(sizeof(JOINTABLE));
346 if (!table)
347 {
348 r = ERROR_OUTOFMEMORY;
349 goto end;
350 }
351
352 r = TABLE_CreateView( db, tables, &table->view );
353 if( r != ERROR_SUCCESS )
354 {
355 WARN("can't create table: %s\n", debugstr_w(tables));
356 msi_free(table);
357 r = ERROR_BAD_QUERY_SYNTAX;
358 goto end;
359 }
360
361 r = table->view->ops->get_dimensions( table->view, NULL,
362 &table->columns );
363 if( r != ERROR_SUCCESS )
364 {
365 ERR("can't get table dimensions\n");
366 goto end;
367 }
368
369 jv->columns += table->columns;
370
371 list_add_head( &jv->tables, &table->entry );
372
373 if (!ptr)
374 break;
375
376 tables = ptr + 1;
377 }
378
379 *view = &jv->view;
380 return ERROR_SUCCESS;
381
382 end:
383 jv->view.ops->delete( &jv->view );
384
385 return r;
386 }