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
26 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
36 typedef struct tagMSIJOINVIEW
40 MSIVIEW *left, *right;
41 UINT left_count, right_count;
42 UINT left_key, right_key;
47 static UINT JOIN_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
49 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
52 TRACE("%p %d %d %p\n", jv, row, col, val );
54 if( !jv->left || !jv->right )
55 return ERROR_FUNCTION_FAILED;
57 if( (col==0) || (col>(jv->left_count + jv->right_count)) )
58 return ERROR_FUNCTION_FAILED;
60 if( row >= jv->pair_count )
61 return ERROR_FUNCTION_FAILED;
63 if( col <= jv->left_count )
66 row = jv->pairs[ row*2 ];
71 row = jv->pairs[ row*2 + 1 ];
72 col -= jv->left_count;
75 return table->ops->fetch_int( table, row, col, val );
78 static UINT JOIN_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm)
80 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
83 TRACE("%p %d %d %p\n", jv, row, col, stm );
85 if( !jv->left || !jv->right )
86 return ERROR_FUNCTION_FAILED;
88 if( (col==0) || (col>(jv->left_count + jv->right_count)) )
89 return ERROR_FUNCTION_FAILED;
91 if( row <= jv->left_count )
94 row = jv->pairs[ row*2 ];
99 row = jv->pairs[ row*2 + 1 ];
100 col -= jv->left_count;
103 return table->ops->fetch_stream( table, row, col, stm );
106 static UINT JOIN_set_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT val )
108 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
110 TRACE("%p %d %d %04x\n", jv, row, col, val );
112 return ERROR_FUNCTION_FAILED;
115 static UINT JOIN_insert_row( struct tagMSIVIEW *view, MSIRECORD *record )
117 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
119 TRACE("%p %p\n", jv, record );
121 return ERROR_FUNCTION_FAILED;
124 static int join_key_compare(const void *l, const void *r)
126 const UINT *left = l, *right = r;
127 if (left[1] < right[1])
129 if (left[1] == right[1])
134 static UINT join_load_key_column( MSIJOINVIEW *jv, MSIVIEW *table, UINT column,
135 UINT **pdata, UINT *pcount )
137 UINT r, i, count = 0, *data = NULL;
139 r = table->ops->get_dimensions( table, &count, NULL );
140 if( r != ERROR_SUCCESS )
146 data = msi_alloc( count * 2 * sizeof (UINT) );
148 return ERROR_SUCCESS;
150 for (i=0; i<count; i++)
153 r = table->ops->fetch_int( table, i, column, &data[i*2+1] );
154 if (r != ERROR_SUCCESS)
155 ERR("fetch data (%u,%u) failed\n", i, column);
158 qsort( data, count, 2 * sizeof (UINT), join_key_compare );
164 return ERROR_SUCCESS;
167 static UINT join_match( UINT *ldata, UINT lcount,
168 UINT *rdata, UINT rcount,
169 UINT **ppairs, UINT *ppair_count )
174 TRACE("left %u right %u\n", rcount, lcount);
176 /* there can be at most max(lcount, rcount) matches */
182 pairs = msi_alloc( n * 2 * sizeof(UINT) );
184 return ERROR_OUTOFMEMORY;
186 for (n=0, i=0, j=0; i<lcount && j<rcount; )
188 /* values match... store the row numbers */
189 if (ldata[i*2+1] == rdata[j*2+1])
191 pairs[n*2] = ldata[i*2];
192 pairs[n*2+1] = rdata[j*2];
193 i++; /* FIXME: assumes primary key on the right */
198 /* values differ... move along */
199 if (ldata[i*2+1] < rdata[j*2+1])
208 return ERROR_SUCCESS;
211 static UINT JOIN_execute( struct tagMSIVIEW *view, MSIRECORD *record )
213 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
214 UINT r, *ldata = NULL, *rdata = NULL, lcount = 0, rcount = 0;
216 TRACE("%p %p\n", jv, record);
218 if( !jv->left || !jv->right )
219 return ERROR_FUNCTION_FAILED;
221 r = jv->left->ops->execute( jv->left, NULL );
222 if (r != ERROR_SUCCESS)
225 r = jv->right->ops->execute( jv->right, NULL );
226 if (r != ERROR_SUCCESS)
229 r = join_load_key_column( jv, jv->left, jv->left_key, &ldata, &lcount );
230 if (r != ERROR_SUCCESS)
233 r = join_load_key_column( jv, jv->right, jv->right_key, &rdata, &rcount );
234 if (r != ERROR_SUCCESS)
237 r = join_match( ldata, lcount, rdata, rcount, &jv->pairs, &jv->pair_count );
246 static UINT JOIN_close( struct tagMSIVIEW *view )
248 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
252 if( !jv->left || !jv->right )
253 return ERROR_FUNCTION_FAILED;
255 jv->left->ops->close( jv->left );
256 jv->right->ops->close( jv->right );
258 return ERROR_SUCCESS;
261 static UINT JOIN_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols )
263 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
265 TRACE("%p %p %p\n", jv, rows, cols );
268 *cols = jv->left_count + jv->right_count;
272 if( !jv->left || !jv->right )
273 return ERROR_FUNCTION_FAILED;
275 *rows = jv->pair_count;
278 return ERROR_SUCCESS;
281 static UINT JOIN_get_column_info( struct tagMSIVIEW *view,
282 UINT n, LPWSTR *name, UINT *type )
284 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
286 TRACE("%p %d %p %p\n", jv, n, name, type );
288 if( !jv->left || !jv->right )
289 return ERROR_FUNCTION_FAILED;
291 if( (n==0) || (n>(jv->left_count + jv->right_count)) )
292 return ERROR_FUNCTION_FAILED;
294 if( n <= jv->left_count )
295 return jv->left->ops->get_column_info( jv->left, n, name, type );
297 n = n - jv->left_count;
299 return jv->right->ops->get_column_info( jv->right, n, name, type );
302 static UINT JOIN_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
305 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
307 TRACE("%p %d %p\n", jv, eModifyMode, rec );
309 return ERROR_FUNCTION_FAILED;
312 static UINT JOIN_delete( struct tagMSIVIEW *view )
314 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
319 jv->left->ops->delete( jv->left );
323 jv->right->ops->delete( jv->right );
326 msi_free( jv->pairs );
331 return ERROR_SUCCESS;
334 static UINT JOIN_find_matching_rows( struct tagMSIVIEW *view, UINT col,
335 UINT val, UINT *row, MSIITERHANDLE *handle )
337 MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
339 FIXME("%p, %d, %u, %p\n", jv, col, val, *handle);
341 return ERROR_FUNCTION_FAILED;
344 static const MSIVIEWOPS join_ops =
353 JOIN_get_column_info,
356 JOIN_find_matching_rows
360 * join_check_condition
362 * This is probably overly strict about what kind of condition we need
365 static UINT join_check_condition(MSIJOINVIEW *jv, struct expr *cond)
369 /* assume that we have `KeyColumn` = `SubkeyColumn` */
370 if ( cond->type != EXPR_COMPLEX )
371 return ERROR_FUNCTION_FAILED;
373 if ( cond->u.expr.op != OP_EQ )
374 return ERROR_FUNCTION_FAILED;
376 if ( cond->u.expr.left->type != EXPR_COLUMN )
377 return ERROR_FUNCTION_FAILED;
379 if ( cond->u.expr.right->type != EXPR_COLUMN )
380 return ERROR_FUNCTION_FAILED;
382 /* make sure both columns exist */
383 r = VIEW_find_column( jv->left, cond->u.expr.left->u.column, &jv->left_key );
384 if (r != ERROR_SUCCESS)
385 return ERROR_FUNCTION_FAILED;
387 r = VIEW_find_column( jv->right, cond->u.expr.right->u.column, &jv->right_key );
388 if (r != ERROR_SUCCESS)
389 return ERROR_FUNCTION_FAILED;
391 TRACE("left %s (%u) right %s (%u)\n",
392 debugstr_w(cond->u.expr.left->u.column), jv->left_key,
393 debugstr_w(cond->u.expr.right->u.column), jv->right_key);
395 return ERROR_SUCCESS;
398 UINT JOIN_CreateView( MSIDATABASE *db, MSIVIEW **view,
399 LPCWSTR left, LPCWSTR right,
402 MSIJOINVIEW *jv = NULL;
403 UINT r = ERROR_SUCCESS;
405 TRACE("%p (%s,%s)\n", jv, debugstr_w(left), debugstr_w(right) );
407 jv = msi_alloc_zero( sizeof *jv );
409 return ERROR_FUNCTION_FAILED;
411 /* fill the structure */
412 jv->view.ops = &join_ops;
415 /* create the tables to join */
416 r = TABLE_CreateView( db, left, &jv->left );
417 if( r != ERROR_SUCCESS )
419 ERR("can't create left table\n");
423 r = TABLE_CreateView( db, right, &jv->right );
424 if( r != ERROR_SUCCESS )
426 ERR("can't create right table\n");
430 /* get the number of columns in each table */
431 r = jv->left->ops->get_dimensions( jv->left, NULL, &jv->left_count );
432 if( r != ERROR_SUCCESS )
434 ERR("can't get left table dimensions\n");
438 r = jv->right->ops->get_dimensions( jv->right, NULL, &jv->right_count );
439 if( r != ERROR_SUCCESS )
441 ERR("can't get right table dimensions\n");
445 r = join_check_condition( jv, cond );
446 if( r != ERROR_SUCCESS )
448 ERR("can't get join condition\n");
453 return ERROR_SUCCESS;
456 jv->view.ops->delete( &jv->view );