stash
[wine/wine64.git] / dlls / msi / join.c
blob34de3b17fddd58cf406990819c361df95ef365e2
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2006 Mike McCormack for CodeWeavers
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.
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.
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
21 #include <stdarg.h>
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"
33 #include "wine/debug.h"
34 #include "wine/unicode.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
38 typedef struct tagJOINTABLE
40 struct list entry;
41 MSIVIEW *view;
42 UINT columns;
43 UINT rows;
44 UINT next_rows;
45 } JOINTABLE;
47 typedef struct tagMSIJOINVIEW
49 MSIVIEW view;
50 MSIDATABASE *db;
51 struct list tables;
52 UINT columns;
53 UINT rows;
54 } MSIJOINVIEW;
56 static UINT JOIN_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
58 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
59 JOINTABLE *table;
60 UINT cols = 0;
61 UINT prev_rows = 1;
63 TRACE("%d, %d\n", row, col);
65 if (col == 0 || col > jv->columns)
66 return ERROR_FUNCTION_FAILED;
68 if (row >= jv->rows)
69 return ERROR_FUNCTION_FAILED;
71 LIST_FOR_EACH_ENTRY(table, &jv->tables, JOINTABLE, entry)
73 if (col <= cols + table->columns)
75 row = (row % (jv->rows / table->next_rows)) / prev_rows;
76 col -= cols;
77 break;
80 prev_rows *= table->rows;
81 cols += table->columns;
84 return table->view->ops->fetch_int( table->view, row, col, val );
87 static UINT JOIN_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm)
89 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
90 JOINTABLE *table;
91 UINT cols = 0;
92 UINT prev_rows = 1;
94 TRACE("%p %d %d %p\n", jv, row, col, stm );
96 if (col == 0 || col > jv->columns)
97 return ERROR_FUNCTION_FAILED;
99 if (row >= jv->rows)
100 return ERROR_FUNCTION_FAILED;
102 LIST_FOR_EACH_ENTRY(table, &jv->tables, JOINTABLE, entry)
104 if (col <= cols + table->columns)
106 row = (row % (jv->rows / table->next_rows)) / prev_rows;
107 col -= cols;
108 break;
111 prev_rows *= table->rows;
112 cols += table->columns;
115 return table->view->ops->fetch_stream( table->view, row, col, stm );
118 static UINT JOIN_get_row( struct tagMSIVIEW *view, UINT row, MSIRECORD **rec )
120 FIXME("(%p, %d, %p): stub!\n", view, row, rec);
121 return ERROR_FUNCTION_FAILED;
124 static UINT JOIN_execute( struct tagMSIVIEW *view, MSIRECORD *record )
126 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
127 JOINTABLE *table;
128 UINT r, rows;
130 TRACE("%p %p\n", jv, record);
132 LIST_FOR_EACH_ENTRY(table, &jv->tables, JOINTABLE, entry)
134 table->view->ops->execute(table->view, NULL);
136 r = table->view->ops->get_dimensions(table->view, &table->rows, NULL);
137 if (r != ERROR_SUCCESS)
139 ERR("failed to get table dimensions\n");
140 return r;
143 /* each table must have at least one row */
144 if (table->rows == 0)
146 jv->rows = 0;
147 return ERROR_SUCCESS;
150 if (jv->rows == 0)
151 jv->rows = table->rows;
152 else
153 jv->rows *= table->rows;
156 rows = jv->rows;
157 LIST_FOR_EACH_ENTRY(table, &jv->tables, JOINTABLE, entry)
159 rows /= table->rows;
160 table->next_rows = rows;
163 return ERROR_SUCCESS;
166 static UINT JOIN_close( struct tagMSIVIEW *view )
168 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
169 JOINTABLE *table;
171 TRACE("%p\n", jv );
173 LIST_FOR_EACH_ENTRY(table, &jv->tables, JOINTABLE, entry)
175 table->view->ops->close(table->view);
178 return ERROR_SUCCESS;
181 static UINT JOIN_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols )
183 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
185 TRACE("%p %p %p\n", jv, rows, cols );
187 if (cols)
188 *cols = jv->columns;
190 if (rows)
191 *rows = jv->rows;
193 return ERROR_SUCCESS;
196 static UINT JOIN_get_column_info( struct tagMSIVIEW *view,
197 UINT n, LPWSTR *name, UINT *type )
199 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
200 JOINTABLE *table;
201 UINT cols = 0;
203 TRACE("%p %d %p %p\n", jv, n, name, type );
205 if (n == 0 || n > jv->columns)
206 return ERROR_FUNCTION_FAILED;
208 LIST_FOR_EACH_ENTRY(table, &jv->tables, JOINTABLE, entry)
210 if (n <= cols + table->columns)
211 return table->view->ops->get_column_info(table->view, n - cols, name, type);
213 cols += table->columns;
216 return ERROR_FUNCTION_FAILED;
219 static UINT JOIN_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
220 MSIRECORD *rec, UINT row )
222 TRACE("%p %d %p\n", view, eModifyMode, rec);
223 return ERROR_FUNCTION_FAILED;
226 static UINT JOIN_delete( struct tagMSIVIEW *view )
228 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
229 JOINTABLE *table;
231 TRACE("%p\n", jv );
233 LIST_FOR_EACH_ENTRY(table, &jv->tables, JOINTABLE, entry)
235 table->view->ops->delete(table->view);
236 table->view = NULL;
239 msi_free(jv);
241 return ERROR_SUCCESS;
244 static UINT JOIN_find_matching_rows( struct tagMSIVIEW *view, UINT col,
245 UINT val, UINT *row, MSIITERHANDLE *handle )
247 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
248 UINT i, row_value;
250 TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
252 if (col == 0 || col > jv->columns)
253 return ERROR_INVALID_PARAMETER;
255 for (i = (UINT)*handle; i < jv->rows; i++)
257 if (view->ops->fetch_int( view, i, col, &row_value ) != ERROR_SUCCESS)
258 continue;
260 if (row_value == val)
262 *row = i;
263 (*(UINT *)handle) = i + 1;
264 return ERROR_SUCCESS;
268 return ERROR_NO_MORE_ITEMS;
271 static UINT JOIN_sort(struct tagMSIVIEW *view, column_info *columns)
273 MSIJOINVIEW *jv = (MSIJOINVIEW *)view;
274 JOINTABLE *table;
275 UINT r;
277 TRACE("%p %p\n", view, columns);
279 LIST_FOR_EACH_ENTRY(table, &jv->tables, JOINTABLE, entry)
281 r = table->view->ops->sort(table->view, columns);
282 if (r != ERROR_SUCCESS)
283 return r;
286 return ERROR_SUCCESS;
289 static const MSIVIEWOPS join_ops =
291 JOIN_fetch_int,
292 JOIN_fetch_stream,
293 JOIN_get_row,
294 NULL,
295 NULL,
296 NULL,
297 JOIN_execute,
298 JOIN_close,
299 JOIN_get_dimensions,
300 JOIN_get_column_info,
301 JOIN_modify,
302 JOIN_delete,
303 JOIN_find_matching_rows,
304 NULL,
305 NULL,
306 NULL,
307 NULL,
308 JOIN_sort,
309 NULL,
312 UINT JOIN_CreateView( MSIDATABASE *db, MSIVIEW **view, LPWSTR tables )
314 MSIJOINVIEW *jv = NULL;
315 UINT r = ERROR_SUCCESS;
316 JOINTABLE *table;
317 LPWSTR ptr;
319 TRACE("%p (%s)\n", jv, debugstr_w(tables) );
321 jv = msi_alloc_zero( sizeof *jv );
322 if( !jv )
323 return ERROR_FUNCTION_FAILED;
325 /* fill the structure */
326 jv->view.ops = &join_ops;
327 jv->db = db;
328 jv->columns = 0;
329 jv->rows = 0;
331 list_init(&jv->tables);
333 while (*tables)
335 if ((ptr = strchrW(tables, ' ')))
336 *ptr = '\0';
338 table = msi_alloc(sizeof(JOINTABLE));
339 if (!table)
340 return ERROR_OUTOFMEMORY;
342 r = TABLE_CreateView( db, tables, &table->view );
343 if( r != ERROR_SUCCESS )
345 WARN("can't create table: %s\n", debugstr_w(tables));
346 r = ERROR_BAD_QUERY_SYNTAX;
347 goto end;
350 r = table->view->ops->get_dimensions( table->view, NULL, &table->columns );
351 if( r != ERROR_SUCCESS )
353 ERR("can't get table dimensions\n");
354 goto end;
357 jv->columns += table->columns;
359 list_add_head( &jv->tables, &table->entry );
361 if (!ptr)
362 break;
364 tables = ptr + 1;
367 *view = &jv->view;
368 return ERROR_SUCCESS;
370 end:
371 jv->view.ops->delete( &jv->view );
373 return r;