2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2002 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"
36 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
38 #define MSI_HASH_TABLE_SIZE 37
40 typedef struct tagMSIHASHENTRY
42 struct tagMSIHASHENTRY *next;
47 /* below is the query interface to a table */
49 typedef struct tagMSIWHEREVIEW
55 MSIHASHENTRY **reorder;
60 static void free_hash_table(MSIHASHENTRY **table)
62 MSIHASHENTRY *new, *old;
68 for (i = 0; i < MSI_HASH_TABLE_SIZE; i++)
85 static UINT find_entry_in_hash(MSIHASHENTRY **table, UINT row, UINT *val)
89 if (!(entry = table[row % MSI_HASH_TABLE_SIZE]))
91 ERR("Row not found in hash table!\n");
92 return ERROR_FUNCTION_FAILED;
95 while (entry && entry->row != row)
98 if (entry) *val = entry->value;
102 static UINT add_entry_to_hash(MSIHASHENTRY **table, UINT row, UINT val)
104 MSIHASHENTRY *new = msi_alloc(sizeof(MSIHASHENTRY));
108 return ERROR_OUTOFMEMORY;
114 prev = table[row % MSI_HASH_TABLE_SIZE];
118 table[row % MSI_HASH_TABLE_SIZE] = new;
120 return ERROR_SUCCESS;
123 static UINT WHERE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
125 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
128 TRACE("%p %d %d %p\n", wv, row, col, val );
131 return ERROR_FUNCTION_FAILED;
133 if( row > wv->row_count )
134 return ERROR_NO_MORE_ITEMS;
136 r = find_entry_in_hash(wv->reorder, row, &row);
137 if (r != ERROR_SUCCESS)
140 return wv->table->ops->fetch_int( wv->table, row, col, val );
143 static UINT WHERE_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm )
145 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
148 TRACE("%p %d %d %p\n", wv, row, col, stm );
151 return ERROR_FUNCTION_FAILED;
153 if( row > wv->row_count )
154 return ERROR_NO_MORE_ITEMS;
156 r = find_entry_in_hash(wv->reorder, row, &row);
157 if (r != ERROR_SUCCESS)
160 return wv->table->ops->fetch_stream( wv->table, row, col, stm );
163 static UINT WHERE_get_row( struct tagMSIVIEW *view, UINT row, MSIRECORD **rec )
165 MSIWHEREVIEW *wv = (MSIWHEREVIEW *)view;
168 TRACE("%p %d %p\n", wv, row, rec );
171 return ERROR_FUNCTION_FAILED;
173 if (row > wv->row_count)
174 return ERROR_NO_MORE_ITEMS;
176 r = find_entry_in_hash(wv->reorder, row, &row);
177 if (r != ERROR_SUCCESS)
180 return wv->table->ops->get_row(view, row, rec);
183 static UINT WHERE_set_row( struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask )
185 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
188 TRACE("%p %d %p %08x\n", wv, row, rec, mask );
191 return ERROR_FUNCTION_FAILED;
193 if( row > wv->row_count )
194 return ERROR_NO_MORE_ITEMS;
196 r = find_entry_in_hash(wv->reorder, row, &row);
197 if (r != ERROR_SUCCESS)
200 return wv->table->ops->set_row( wv->table, row, rec, mask );
203 static INT INT_evaluate_binary( INT lval, UINT op, INT rval )
208 return ( lval == rval );
210 return ( lval && rval );
212 return ( lval || rval );
214 return ( lval > rval );
216 return ( lval < rval );
218 return ( lval <= rval );
220 return ( lval >= rval );
222 return ( lval != rval );
224 ERR("Unknown operator %d\n", op );
229 static INT INT_evaluate_unary( INT lval, UINT op )
238 ERR("Unknown operator %d\n", op );
243 static const WCHAR *STRING_evaluate( MSIWHEREVIEW *wv, UINT row,
244 const struct expr *expr,
245 const MSIRECORD *record )
251 case EXPR_COL_NUMBER_STRING:
252 r = wv->table->ops->fetch_int( wv->table, row, expr->u.col_number, &val );
253 if( r != ERROR_SUCCESS )
255 return msi_string_lookup_id( wv->db->strings, val );
261 return MSI_RecordGetString( record, ++wv->rec_index );
264 ERR("Invalid expression type\n");
270 static UINT STRCMP_Evaluate( MSIWHEREVIEW *wv, UINT row, const struct expr *cond,
271 INT *val, const MSIRECORD *record )
274 const WCHAR *l_str, *r_str;
276 l_str = STRING_evaluate( wv, row, cond->u.expr.left, record );
277 r_str = STRING_evaluate( wv, row, cond->u.expr.right, record );
278 if( l_str == r_str ||
279 ((!l_str || !*l_str) && (!r_str || !*r_str)) )
281 else if( l_str && ! r_str )
283 else if( r_str && ! l_str )
286 sr = lstrcmpW( l_str, r_str );
288 *val = ( cond->u.expr.op == OP_EQ && ( sr == 0 ) ) ||
289 ( cond->u.expr.op == OP_LT && ( sr < 0 ) ) ||
290 ( cond->u.expr.op == OP_GT && ( sr > 0 ) );
292 return ERROR_SUCCESS;
295 static UINT WHERE_evaluate( MSIWHEREVIEW *wv, UINT row,
296 struct expr *cond, INT *val, MSIRECORD *record )
302 return ERROR_SUCCESS;
306 case EXPR_COL_NUMBER:
307 r = wv->table->ops->fetch_int( wv->table, row, cond->u.col_number, &tval );
308 *val = tval - 0x8000;
309 return ERROR_SUCCESS;
311 case EXPR_COL_NUMBER32:
312 r = wv->table->ops->fetch_int( wv->table, row, cond->u.col_number, &tval );
313 *val = tval - 0x80000000;
318 return ERROR_SUCCESS;
321 r = WHERE_evaluate( wv, row, cond->u.expr.left, &lval, record );
322 if( r != ERROR_SUCCESS )
324 r = WHERE_evaluate( wv, row, cond->u.expr.right, &rval, record );
325 if( r != ERROR_SUCCESS )
327 *val = INT_evaluate_binary( lval, cond->u.expr.op, rval );
328 return ERROR_SUCCESS;
331 r = wv->table->ops->fetch_int( wv->table, row, cond->u.expr.left->u.col_number, &tval );
332 if( r != ERROR_SUCCESS )
334 *val = INT_evaluate_unary( tval, cond->u.expr.op );
335 return ERROR_SUCCESS;
338 return STRCMP_Evaluate( wv, row, cond, val, record );
341 *val = MSI_RecordGetInteger( record, ++wv->rec_index );
342 return ERROR_SUCCESS;
345 ERR("Invalid expression type\n");
349 return ERROR_SUCCESS;
352 static UINT WHERE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
354 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
355 UINT count = 0, r, i;
357 MSIVIEW *table = wv->table;
359 TRACE("%p %p\n", wv, record);
362 return ERROR_FUNCTION_FAILED;
364 r = table->ops->execute( table, record );
365 if( r != ERROR_SUCCESS )
368 r = table->ops->get_dimensions( table, &count, NULL );
369 if( r != ERROR_SUCCESS )
372 free_hash_table(wv->reorder);
373 wv->reorder = msi_alloc_zero(MSI_HASH_TABLE_SIZE * sizeof(MSIHASHENTRY *));
375 return ERROR_OUTOFMEMORY;
378 if (wv->cond->type == EXPR_STRCMP)
380 MSIITERHANDLE handle = NULL;
381 UINT row, value, col;
382 struct expr *col_cond = wv->cond->u.expr.left;
383 struct expr *val_cond = wv->cond->u.expr.right;
385 /* swap conditionals */
386 if (col_cond->type != EXPR_COL_NUMBER_STRING)
388 val_cond = wv->cond->u.expr.left;
389 col_cond = wv->cond->u.expr.right;
392 if ((col_cond->type == EXPR_COL_NUMBER_STRING) && (val_cond->type == EXPR_SVAL))
394 col = col_cond->u.col_number;
395 /* special case for "" - translate it into nil */
396 if (!val_cond->u.sval[0])
400 r = msi_string2idW(wv->db->strings, val_cond->u.sval, &value);
401 if (r != ERROR_SUCCESS)
403 TRACE("no id for %s, assuming it doesn't exist in the table\n", debugstr_w(wv->cond->u.expr.right->u.sval));
404 return ERROR_SUCCESS;
410 r = table->ops->find_matching_rows(table, col, value, &row, &handle);
411 if (r == ERROR_SUCCESS)
412 add_entry_to_hash(wv->reorder, wv->row_count++, row);
413 } while (r == ERROR_SUCCESS);
415 if (r == ERROR_NO_MORE_ITEMS)
416 return ERROR_SUCCESS;
420 /* else fallback to slow case */
423 for( i=0; i<count; i++ )
427 r = WHERE_evaluate( wv, i, wv->cond, &val, record );
428 if( r != ERROR_SUCCESS )
431 add_entry_to_hash( wv->reorder, wv->row_count++, i );
434 return ERROR_SUCCESS;
437 static UINT WHERE_close( struct tagMSIVIEW *view )
439 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
444 return ERROR_FUNCTION_FAILED;
446 return wv->table->ops->close( wv->table );
449 static UINT WHERE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols )
451 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
453 TRACE("%p %p %p\n", wv, rows, cols );
456 return ERROR_FUNCTION_FAILED;
461 return ERROR_FUNCTION_FAILED;
462 *rows = wv->row_count;
465 return wv->table->ops->get_dimensions( wv->table, NULL, cols );
468 static UINT WHERE_get_column_info( struct tagMSIVIEW *view,
469 UINT n, LPWSTR *name, UINT *type )
471 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
473 TRACE("%p %d %p %p\n", wv, n, name, type );
476 return ERROR_FUNCTION_FAILED;
478 return wv->table->ops->get_column_info( wv->table, n, name, type );
481 static UINT WHERE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
482 MSIRECORD *rec, UINT row )
484 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
486 TRACE("%p %d %p\n", wv, eModifyMode, rec );
489 return ERROR_FUNCTION_FAILED;
491 return wv->table->ops->modify( wv->table, eModifyMode, rec, row );
494 static UINT WHERE_delete( struct tagMSIVIEW *view )
496 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
501 wv->table->ops->delete( wv->table );
504 free_hash_table(wv->reorder);
508 msiobj_release( &wv->db->hdr );
511 return ERROR_SUCCESS;
514 static UINT WHERE_find_matching_rows( struct tagMSIVIEW *view, UINT col,
515 UINT val, UINT *row, MSIITERHANDLE *handle )
517 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
520 TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
523 return ERROR_FUNCTION_FAILED;
525 r = wv->table->ops->find_matching_rows( wv->table, col, val, row, handle );
526 if (r != ERROR_SUCCESS)
529 if( *row > wv->row_count )
530 return ERROR_NO_MORE_ITEMS;
532 return find_entry_in_hash(wv->reorder, *row, row);
535 static UINT WHERE_sort(struct tagMSIVIEW *view, column_info *columns)
537 MSIWHEREVIEW *wv = (MSIWHEREVIEW *)view;
539 TRACE("%p %p\n", view, columns);
541 return wv->table->ops->sort(wv->table, columns);
544 static const MSIVIEWOPS where_ops =
554 WHERE_get_dimensions,
555 WHERE_get_column_info,
558 WHERE_find_matching_rows,
566 static UINT WHERE_VerifyCondition( MSIDATABASE *db, MSIVIEW *table, struct expr *cond,
574 r = VIEW_find_column( table, cond->u.column, &val );
575 if( r == ERROR_SUCCESS )
578 r = table->ops->get_column_info( table, val, NULL, &type );
579 if( r == ERROR_SUCCESS )
581 if (type&MSITYPE_STRING)
582 cond->type = EXPR_COL_NUMBER_STRING;
583 else if ((type&0xff) == 4)
584 cond->type = EXPR_COL_NUMBER32;
586 cond->type = EXPR_COL_NUMBER;
587 cond->u.col_number = val;
596 WARN("Couldn't find column %s\n", debugstr_w( cond->u.column ) );
600 r = WHERE_VerifyCondition( db, table, cond->u.expr.left, valid );
601 if( r != ERROR_SUCCESS )
604 return ERROR_SUCCESS;
605 r = WHERE_VerifyCondition( db, table, cond->u.expr.right, valid );
606 if( r != ERROR_SUCCESS )
609 /* check the type of the comparison */
610 if( ( cond->u.expr.left->type == EXPR_SVAL ) ||
611 ( cond->u.expr.left->type == EXPR_COL_NUMBER_STRING ) ||
612 ( cond->u.expr.right->type == EXPR_SVAL ) ||
613 ( cond->u.expr.right->type == EXPR_COL_NUMBER_STRING ) )
615 switch( cond->u.expr.op )
623 return ERROR_INVALID_PARAMETER;
626 /* FIXME: check we're comparing a string to a column */
628 cond->type = EXPR_STRCMP;
633 if ( cond->u.expr.left->type != EXPR_COLUMN )
636 return ERROR_INVALID_PARAMETER;
638 r = WHERE_VerifyCondition( db, table, cond->u.expr.left, valid );
639 if( r != ERROR_SUCCESS )
644 cond->type = EXPR_UVAL;
645 cond->u.uval = cond->u.ival;
654 ERR("Invalid expression type\n");
659 return ERROR_SUCCESS;
662 UINT WHERE_CreateView( MSIDATABASE *db, MSIVIEW **view, MSIVIEW *table,
665 MSIWHEREVIEW *wv = NULL;
666 UINT count = 0, r, valid = 0;
668 TRACE("%p\n", table );
670 r = table->ops->get_dimensions( table, NULL, &count );
671 if( r != ERROR_SUCCESS )
673 ERR("can't get table dimensions\n");
679 r = WHERE_VerifyCondition( db, table, cond, &valid );
680 if( r != ERROR_SUCCESS )
683 return ERROR_FUNCTION_FAILED;
686 wv = msi_alloc_zero( sizeof *wv );
688 return ERROR_FUNCTION_FAILED;
690 /* fill the structure */
691 wv->view.ops = &where_ops;
692 msiobj_addref( &db->hdr );
699 *view = (MSIVIEW*) wv;
701 return ERROR_SUCCESS;