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
33 #include "wine/debug.h"
34 #include "wine/unicode.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
38 typedef struct tagJOINTABLE
47 typedef struct tagMSIJOINVIEW
56 static UINT JOIN_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
58 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
63 TRACE("%d, %d\n", row, col);
65 if (col == 0 || col > jv->columns)
66 return ERROR_FUNCTION_FAILED;
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;
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;
94 TRACE("%p %d %d %p\n", jv, row, col, stm );
96 if (col == 0 || col > jv->columns)
97 return ERROR_FUNCTION_FAILED;
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;
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 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
122 TRACE("%p %d %p\n", jv, row, rec);
124 return msi_view_get_row( jv->db, view, row, rec );
127 static UINT JOIN_execute( struct tagMSIVIEW *view, MSIRECORD *record )
129 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
133 TRACE("%p %p\n", jv, record);
135 LIST_FOR_EACH_ENTRY(table, &jv->tables, JOINTABLE, entry)
137 table->view->ops->execute(table->view, NULL);
139 r = table->view->ops->get_dimensions(table->view, &table->rows, NULL);
140 if (r != ERROR_SUCCESS)
142 ERR("failed to get table dimensions\n");
146 /* each table must have at least one row */
147 if (table->rows == 0)
150 return ERROR_SUCCESS;
154 jv->rows = table->rows;
156 jv->rows *= table->rows;
160 LIST_FOR_EACH_ENTRY(table, &jv->tables, JOINTABLE, entry)
163 table->next_rows = rows;
166 return ERROR_SUCCESS;
169 static UINT JOIN_close( struct tagMSIVIEW *view )
171 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
176 LIST_FOR_EACH_ENTRY(table, &jv->tables, JOINTABLE, entry)
178 table->view->ops->close(table->view);
181 return ERROR_SUCCESS;
184 static UINT JOIN_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols )
186 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
188 TRACE("%p %p %p\n", jv, rows, cols );
196 return ERROR_SUCCESS;
199 static UINT JOIN_get_column_info( struct tagMSIVIEW *view,
200 UINT n, LPWSTR *name, UINT *type, BOOL *temporary,
203 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
207 TRACE("%p %d %p %p %p %p\n", jv, n, name, type, temporary, table_name );
209 if (n == 0 || n > jv->columns)
210 return ERROR_FUNCTION_FAILED;
212 LIST_FOR_EACH_ENTRY(table, &jv->tables, JOINTABLE, entry)
214 if (n <= cols + table->columns)
215 return table->view->ops->get_column_info(table->view, n - cols,
216 name, type, temporary,
219 cols += table->columns;
222 return ERROR_FUNCTION_FAILED;
225 static UINT JOIN_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
226 MSIRECORD *rec, UINT row )
228 TRACE("%p %d %p\n", view, eModifyMode, rec);
229 return ERROR_FUNCTION_FAILED;
232 static UINT JOIN_delete( struct tagMSIVIEW *view )
234 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
235 struct list *item, *cursor;
239 LIST_FOR_EACH_SAFE(item, cursor, &jv->tables)
241 JOINTABLE* table = LIST_ENTRY(item, JOINTABLE, entry);
243 list_remove(&table->entry);
244 table->view->ops->delete(table->view);
251 return ERROR_SUCCESS;
254 static UINT JOIN_find_matching_rows( struct tagMSIVIEW *view, UINT col,
255 UINT val, UINT *row, MSIITERHANDLE *handle )
257 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
260 TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
262 if (col == 0 || col > jv->columns)
263 return ERROR_INVALID_PARAMETER;
265 for (i = PtrToUlong(*handle); i < jv->rows; i++)
267 if (view->ops->fetch_int( view, i, col, &row_value ) != ERROR_SUCCESS)
270 if (row_value == val)
273 (*(UINT *)handle) = i + 1;
274 return ERROR_SUCCESS;
278 return ERROR_NO_MORE_ITEMS;
281 static UINT JOIN_sort(struct tagMSIVIEW *view, column_info *columns)
283 MSIJOINVIEW *jv = (MSIJOINVIEW *)view;
287 TRACE("%p %p\n", view, columns);
289 LIST_FOR_EACH_ENTRY(table, &jv->tables, JOINTABLE, entry)
291 r = table->view->ops->sort(table->view, columns);
292 if (r != ERROR_SUCCESS)
296 return ERROR_SUCCESS;
299 static const MSIVIEWOPS join_ops =
310 JOIN_get_column_info,
313 JOIN_find_matching_rows,
322 UINT JOIN_CreateView( MSIDATABASE *db, MSIVIEW **view, LPWSTR tables )
324 MSIJOINVIEW *jv = NULL;
325 UINT r = ERROR_SUCCESS;
329 TRACE("%p (%s)\n", jv, debugstr_w(tables) );
331 jv = msi_alloc_zero( sizeof *jv );
333 return ERROR_FUNCTION_FAILED;
335 /* fill the structure */
336 jv->view.ops = &join_ops;
341 list_init(&jv->tables);
345 if ((ptr = strchrW(tables, ' ')))
348 table = msi_alloc(sizeof(JOINTABLE));
351 r = ERROR_OUTOFMEMORY;
355 r = TABLE_CreateView( db, tables, &table->view );
356 if( r != ERROR_SUCCESS )
358 WARN("can't create table: %s\n", debugstr_w(tables));
360 r = ERROR_BAD_QUERY_SYNTAX;
364 r = table->view->ops->get_dimensions( table->view, NULL,
366 if( r != ERROR_SUCCESS )
368 ERR("can't get table dimensions\n");
372 jv->columns += table->columns;
374 list_add_head( &jv->tables, &table->entry );
383 return ERROR_SUCCESS;
386 jv->view.ops->delete( &jv->view );