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 if (col == 0 || col > jv->columns)
64 return ERROR_FUNCTION_FAILED;
67 return ERROR_FUNCTION_FAILED;
69 LIST_FOR_EACH_ENTRY(table, &jv->tables, JOINTABLE, entry)
71 if (col <= cols + table->columns)
73 row = (row % (jv->rows / table->next_rows)) / prev_rows;
78 prev_rows *= table->rows;
79 cols += table->columns;
82 return table->view->ops->fetch_int( table->view, row, col, val );
85 static UINT JOIN_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm)
87 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
92 TRACE("%p %d %d %p\n", jv, row, col, stm );
94 if (col == 0 || col > jv->columns)
95 return ERROR_FUNCTION_FAILED;
98 return ERROR_FUNCTION_FAILED;
100 LIST_FOR_EACH_ENTRY(table, &jv->tables, JOINTABLE, entry)
102 if (col <= cols + table->columns)
104 row = (row % (jv->rows / table->next_rows)) / prev_rows;
109 prev_rows *= table->rows;
110 cols += table->columns;
113 return table->view->ops->fetch_stream( table->view, row, col, stm );
116 static UINT JOIN_get_row( struct tagMSIVIEW *view, UINT row, MSIRECORD **rec )
118 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
120 TRACE("%p %d %p\n", jv, row, rec);
122 return msi_view_get_row( jv->db, view, row, rec );
125 static UINT JOIN_execute( struct tagMSIVIEW *view, MSIRECORD *record )
127 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
131 TRACE("%p %p\n", jv, record);
133 LIST_FOR_EACH_ENTRY(table, &jv->tables, JOINTABLE, entry)
135 table->view->ops->execute(table->view, NULL);
137 r = table->view->ops->get_dimensions(table->view, &table->rows, NULL);
138 if (r != ERROR_SUCCESS)
140 ERR("failed to get table dimensions\n");
144 /* each table must have at least one row */
145 if (table->rows == 0)
148 return ERROR_SUCCESS;
152 jv->rows = table->rows;
154 jv->rows *= table->rows;
158 LIST_FOR_EACH_ENTRY(table, &jv->tables, JOINTABLE, entry)
161 table->next_rows = rows;
164 return ERROR_SUCCESS;
167 static UINT JOIN_close( struct tagMSIVIEW *view )
169 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
174 LIST_FOR_EACH_ENTRY(table, &jv->tables, JOINTABLE, entry)
176 table->view->ops->close(table->view);
179 return ERROR_SUCCESS;
182 static UINT JOIN_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols )
184 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
186 TRACE("%p %p %p\n", jv, rows, cols );
194 return ERROR_SUCCESS;
197 static UINT JOIN_get_column_info( struct tagMSIVIEW *view,
198 UINT n, LPWSTR *name, UINT *type, BOOL *temporary,
201 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
205 TRACE("%p %d %p %p %p %p\n", jv, n, name, type, temporary, table_name );
207 if (n == 0 || n > jv->columns)
208 return ERROR_FUNCTION_FAILED;
210 LIST_FOR_EACH_ENTRY(table, &jv->tables, JOINTABLE, entry)
212 if (n <= cols + table->columns)
213 return table->view->ops->get_column_info(table->view, n - cols,
214 name, type, temporary,
217 cols += table->columns;
220 return ERROR_FUNCTION_FAILED;
223 static UINT join_find_row( MSIJOINVIEW *jv, MSIRECORD *rec, UINT *row )
228 str = MSI_RecordGetString( rec, 1 );
229 r = msi_string2idW( jv->db->strings, str, &id );
230 if (r != ERROR_SUCCESS)
233 for (i = 0; i < jv->rows; i++)
235 JOIN_fetch_int( &jv->view, i, 1, &data );
240 return ERROR_SUCCESS;
244 return ERROR_FUNCTION_FAILED;
247 static UINT JOIN_set_row( struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask )
249 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
251 UINT i, reduced_mask = 0, r = ERROR_SUCCESS, offset = 0, col_count;
254 TRACE("%p %d %p %u %08x\n", jv, row, rec, rec->count, mask );
256 if (mask >= 1 << jv->columns)
257 return ERROR_INVALID_PARAMETER;
259 LIST_FOR_EACH_ENTRY(table, &jv->tables, JOINTABLE, entry)
261 r = table->view->ops->get_dimensions( table->view, NULL, &col_count );
262 if (r != ERROR_SUCCESS)
265 reduced = MSI_CreateRecord( col_count );
267 return ERROR_FUNCTION_FAILED;
269 for (i = 0; i < col_count; i++)
271 r = MSI_RecordCopyField( rec, i + offset + 1, reduced, i + 1 );
272 if (r != ERROR_SUCCESS)
277 reduced_mask = mask >> (jv->columns - offset) & ((1 << col_count) - 1);
279 if (r == ERROR_SUCCESS)
280 r = table->view->ops->set_row( table->view, row, reduced, reduced_mask );
282 msiobj_release( &reduced->hdr );
288 static UINT join_modify_update( struct tagMSIVIEW *view, MSIRECORD *rec )
290 MSIJOINVIEW *jv = (MSIJOINVIEW *)view;
293 r = join_find_row( jv, rec, &row );
294 if (r != ERROR_SUCCESS)
297 return JOIN_set_row( view, row, rec, (1 << jv->columns) - 1 );
300 static UINT JOIN_modify( struct tagMSIVIEW *view, MSIMODIFY mode, MSIRECORD *rec, UINT row )
304 TRACE("%p %d %p %u\n", view, mode, rec, row);
308 case MSIMODIFY_UPDATE:
309 return join_modify_update( view, rec );
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:
318 case MSIMODIFY_VALIDATE:
319 case MSIMODIFY_VALIDATE_DELETE:
320 case MSIMODIFY_VALIDATE_FIELD:
321 case MSIMODIFY_VALIDATE_NEW:
322 r = ERROR_FUNCTION_FAILED;
325 case MSIMODIFY_REFRESH:
326 r = ERROR_CALL_NOT_IMPLEMENTED;
330 WARN("%p %d %p %u - unknown mode\n", view, mode, rec, row );
331 r = ERROR_INVALID_PARAMETER;
338 static UINT JOIN_delete( struct tagMSIVIEW *view )
340 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
341 struct list *item, *cursor;
345 LIST_FOR_EACH_SAFE(item, cursor, &jv->tables)
347 JOINTABLE* table = LIST_ENTRY(item, JOINTABLE, entry);
349 list_remove(&table->entry);
350 table->view->ops->delete(table->view);
357 return ERROR_SUCCESS;
360 static UINT JOIN_find_matching_rows( struct tagMSIVIEW *view, UINT col,
361 UINT val, UINT *row, MSIITERHANDLE *handle )
363 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
366 TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
368 if (col == 0 || col > jv->columns)
369 return ERROR_INVALID_PARAMETER;
371 for (i = PtrToUlong(*handle); i < jv->rows; i++)
373 if (view->ops->fetch_int( view, i, col, &row_value ) != ERROR_SUCCESS)
376 if (row_value == val)
379 (*(UINT *)handle) = i + 1;
380 return ERROR_SUCCESS;
384 return ERROR_NO_MORE_ITEMS;
387 static UINT JOIN_sort(struct tagMSIVIEW *view, column_info *columns)
389 MSIJOINVIEW *jv = (MSIJOINVIEW *)view;
393 TRACE("%p %p\n", view, columns);
395 LIST_FOR_EACH_ENTRY(table, &jv->tables, JOINTABLE, entry)
397 r = table->view->ops->sort(table->view, columns);
398 if (r != ERROR_SUCCESS)
402 return ERROR_SUCCESS;
405 static const MSIVIEWOPS join_ops =
416 JOIN_get_column_info,
419 JOIN_find_matching_rows,
428 UINT JOIN_CreateView( MSIDATABASE *db, MSIVIEW **view, LPWSTR tables )
430 MSIJOINVIEW *jv = NULL;
431 UINT r = ERROR_SUCCESS;
435 TRACE("%p (%s)\n", jv, debugstr_w(tables) );
437 jv = msi_alloc_zero( sizeof *jv );
439 return ERROR_FUNCTION_FAILED;
441 /* fill the structure */
442 jv->view.ops = &join_ops;
447 list_init(&jv->tables);
451 if ((ptr = strchrW(tables, ' ')))
454 table = msi_alloc(sizeof(JOINTABLE));
457 r = ERROR_OUTOFMEMORY;
461 r = TABLE_CreateView( db, tables, &table->view );
462 if( r != ERROR_SUCCESS )
464 WARN("can't create table: %s\n", debugstr_w(tables));
466 r = ERROR_BAD_QUERY_SYNTAX;
470 r = table->view->ops->get_dimensions( table->view, NULL,
472 if( r != ERROR_SUCCESS )
474 ERR("can't get table dimensions\n");
478 jv->columns += table->columns;
480 list_add_head( &jv->tables, &table->entry );
489 return ERROR_SUCCESS;
492 jv->view.ops->delete( &jv->view );