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_NE && ( sr != 0 ) ) ||
290 ( cond->u.expr.op == OP_LT && ( sr < 0 ) ) ||
291 ( cond->u.expr.op == OP_GT && ( sr > 0 ) );
293 return ERROR_SUCCESS;
296 static UINT WHERE_evaluate( MSIWHEREVIEW *wv, UINT row,
297 struct expr *cond, INT *val, MSIRECORD *record )
303 return ERROR_SUCCESS;
307 case EXPR_COL_NUMBER:
308 r = wv->table->ops->fetch_int( wv->table, row, cond->u.col_number, &tval );
309 *val = tval - 0x8000;
310 return ERROR_SUCCESS;
312 case EXPR_COL_NUMBER32:
313 r = wv->table->ops->fetch_int( wv->table, row, cond->u.col_number, &tval );
314 *val = tval - 0x80000000;
319 return ERROR_SUCCESS;
322 r = WHERE_evaluate( wv, row, cond->u.expr.left, &lval, record );
323 if( r != ERROR_SUCCESS )
325 r = WHERE_evaluate( wv, row, cond->u.expr.right, &rval, record );
326 if( r != ERROR_SUCCESS )
328 *val = INT_evaluate_binary( lval, cond->u.expr.op, rval );
329 return ERROR_SUCCESS;
332 r = wv->table->ops->fetch_int( wv->table, row, cond->u.expr.left->u.col_number, &tval );
333 if( r != ERROR_SUCCESS )
335 *val = INT_evaluate_unary( tval, cond->u.expr.op );
336 return ERROR_SUCCESS;
339 return STRCMP_Evaluate( wv, row, cond, val, record );
342 *val = MSI_RecordGetInteger( record, ++wv->rec_index );
343 return ERROR_SUCCESS;
346 ERR("Invalid expression type\n");
350 return ERROR_SUCCESS;
353 static UINT WHERE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
355 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
356 UINT count = 0, r, i;
358 MSIVIEW *table = wv->table;
360 TRACE("%p %p\n", wv, record);
363 return ERROR_FUNCTION_FAILED;
365 r = table->ops->execute( table, record );
366 if( r != ERROR_SUCCESS )
369 r = table->ops->get_dimensions( table, &count, NULL );
370 if( r != ERROR_SUCCESS )
373 free_hash_table(wv->reorder);
374 wv->reorder = msi_alloc_zero(MSI_HASH_TABLE_SIZE * sizeof(MSIHASHENTRY *));
376 return ERROR_OUTOFMEMORY;
379 if (wv->cond->type == EXPR_STRCMP)
381 MSIITERHANDLE handle = NULL;
382 UINT row, value, col;
383 struct expr *col_cond = wv->cond->u.expr.left;
384 struct expr *val_cond = wv->cond->u.expr.right;
386 /* swap conditionals */
387 if (col_cond->type != EXPR_COL_NUMBER_STRING)
389 val_cond = wv->cond->u.expr.left;
390 col_cond = wv->cond->u.expr.right;
393 if ((col_cond->type == EXPR_COL_NUMBER_STRING) && (val_cond->type == EXPR_SVAL))
395 col = col_cond->u.col_number;
396 /* special case for "" - translate it into nil */
397 if (!val_cond->u.sval[0])
401 r = msi_string2idW(wv->db->strings, val_cond->u.sval, &value);
402 if (r != ERROR_SUCCESS)
404 TRACE("no id for %s, assuming it doesn't exist in the table\n", debugstr_w(wv->cond->u.expr.right->u.sval));
405 return ERROR_SUCCESS;
411 r = table->ops->find_matching_rows(table, col, value, &row, &handle);
412 if (r == ERROR_SUCCESS)
413 add_entry_to_hash(wv->reorder, wv->row_count++, row);
414 } while (r == ERROR_SUCCESS);
416 if (r == ERROR_NO_MORE_ITEMS)
417 return ERROR_SUCCESS;
421 /* else fallback to slow case */
424 for( i=0; i<count; i++ )
428 r = WHERE_evaluate( wv, i, wv->cond, &val, record );
429 if( r != ERROR_SUCCESS )
432 add_entry_to_hash( wv->reorder, wv->row_count++, i );
435 return ERROR_SUCCESS;
438 static UINT WHERE_close( struct tagMSIVIEW *view )
440 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
445 return ERROR_FUNCTION_FAILED;
447 return wv->table->ops->close( wv->table );
450 static UINT WHERE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols )
452 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
454 TRACE("%p %p %p\n", wv, rows, cols );
457 return ERROR_FUNCTION_FAILED;
462 return ERROR_FUNCTION_FAILED;
463 *rows = wv->row_count;
466 return wv->table->ops->get_dimensions( wv->table, NULL, cols );
469 static UINT WHERE_get_column_info( struct tagMSIVIEW *view,
470 UINT n, LPWSTR *name, UINT *type )
472 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
474 TRACE("%p %d %p %p\n", wv, n, name, type );
477 return ERROR_FUNCTION_FAILED;
479 return wv->table->ops->get_column_info( wv->table, n, name, type );
482 static UINT WHERE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
483 MSIRECORD *rec, UINT row )
485 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
487 TRACE("%p %d %p\n", wv, eModifyMode, rec );
490 return ERROR_FUNCTION_FAILED;
492 return wv->table->ops->modify( wv->table, eModifyMode, rec, row );
495 static UINT WHERE_delete( struct tagMSIVIEW *view )
497 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
502 wv->table->ops->delete( wv->table );
505 free_hash_table(wv->reorder);
509 msiobj_release( &wv->db->hdr );
512 return ERROR_SUCCESS;
515 static UINT WHERE_find_matching_rows( struct tagMSIVIEW *view, UINT col,
516 UINT val, UINT *row, MSIITERHANDLE *handle )
518 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
521 TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
524 return ERROR_FUNCTION_FAILED;
526 r = wv->table->ops->find_matching_rows( wv->table, col, val, row, handle );
527 if (r != ERROR_SUCCESS)
530 if( *row > wv->row_count )
531 return ERROR_NO_MORE_ITEMS;
533 return find_entry_in_hash(wv->reorder, *row, row);
536 static UINT WHERE_sort(struct tagMSIVIEW *view, column_info *columns)
538 MSIWHEREVIEW *wv = (MSIWHEREVIEW *)view;
540 TRACE("%p %p\n", view, columns);
542 return wv->table->ops->sort(wv->table, columns);
545 static const MSIVIEWOPS where_ops =
555 WHERE_get_dimensions,
556 WHERE_get_column_info,
559 WHERE_find_matching_rows,
567 static UINT WHERE_VerifyCondition( MSIDATABASE *db, MSIVIEW *table, struct expr *cond,
575 r = VIEW_find_column( table, cond->u.column, &val );
576 if( r == ERROR_SUCCESS )
579 r = table->ops->get_column_info( table, val, NULL, &type );
580 if( r == ERROR_SUCCESS )
582 if (type&MSITYPE_STRING)
583 cond->type = EXPR_COL_NUMBER_STRING;
584 else if ((type&0xff) == 4)
585 cond->type = EXPR_COL_NUMBER32;
587 cond->type = EXPR_COL_NUMBER;
588 cond->u.col_number = val;
597 WARN("Couldn't find column %s\n", debugstr_w( cond->u.column ) );
601 r = WHERE_VerifyCondition( db, table, cond->u.expr.left, valid );
602 if( r != ERROR_SUCCESS )
605 return ERROR_SUCCESS;
606 r = WHERE_VerifyCondition( db, table, cond->u.expr.right, valid );
607 if( r != ERROR_SUCCESS )
610 /* check the type of the comparison */
611 if( ( cond->u.expr.left->type == EXPR_SVAL ) ||
612 ( cond->u.expr.left->type == EXPR_COL_NUMBER_STRING ) ||
613 ( cond->u.expr.right->type == EXPR_SVAL ) ||
614 ( cond->u.expr.right->type == EXPR_COL_NUMBER_STRING ) )
616 switch( cond->u.expr.op )
625 return ERROR_INVALID_PARAMETER;
628 /* FIXME: check we're comparing a string to a column */
630 cond->type = EXPR_STRCMP;
635 if ( cond->u.expr.left->type != EXPR_COLUMN )
638 return ERROR_INVALID_PARAMETER;
640 r = WHERE_VerifyCondition( db, table, cond->u.expr.left, valid );
641 if( r != ERROR_SUCCESS )
646 cond->type = EXPR_UVAL;
647 cond->u.uval = cond->u.ival;
656 ERR("Invalid expression type\n");
661 return ERROR_SUCCESS;
664 UINT WHERE_CreateView( MSIDATABASE *db, MSIVIEW **view, MSIVIEW *table,
667 MSIWHEREVIEW *wv = NULL;
668 UINT count = 0, r, valid = 0;
670 TRACE("%p\n", table );
672 r = table->ops->get_dimensions( table, NULL, &count );
673 if( r != ERROR_SUCCESS )
675 ERR("can't get table dimensions\n");
681 r = WHERE_VerifyCondition( db, table, cond, &valid );
682 if( r != ERROR_SUCCESS )
685 return ERROR_FUNCTION_FAILED;
688 wv = msi_alloc_zero( sizeof *wv );
690 return ERROR_FUNCTION_FAILED;
692 /* fill the structure */
693 wv->view.ops = &where_ops;
694 msiobj_addref( &db->hdr );
701 *view = (MSIVIEW*) wv;
703 return ERROR_SUCCESS;