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 UINT WHERE_delete_row(struct tagMSIVIEW *view, UINT row)
205 MSIWHEREVIEW *wv = (MSIWHEREVIEW *)view;
208 TRACE("(%p %d)\n", view, row);
211 return ERROR_FUNCTION_FAILED;
213 if ( row > wv->row_count )
214 return ERROR_NO_MORE_ITEMS;
216 r = find_entry_in_hash( wv->reorder, row, &row );
217 if ( r != ERROR_SUCCESS )
220 return wv->table->ops->delete_row( wv->table, row );
223 static INT INT_evaluate_binary( INT lval, UINT op, INT rval )
228 return ( lval == rval );
230 return ( lval && rval );
232 return ( lval || rval );
234 return ( lval > rval );
236 return ( lval < rval );
238 return ( lval <= rval );
240 return ( lval >= rval );
242 return ( lval != rval );
244 ERR("Unknown operator %d\n", op );
249 static INT INT_evaluate_unary( INT lval, UINT op )
258 ERR("Unknown operator %d\n", op );
263 static const WCHAR *STRING_evaluate( MSIWHEREVIEW *wv, UINT row,
264 const struct expr *expr,
265 const MSIRECORD *record )
271 case EXPR_COL_NUMBER_STRING:
272 r = wv->table->ops->fetch_int( wv->table, row, expr->u.col_number, &val );
273 if( r != ERROR_SUCCESS )
275 return msi_string_lookup_id( wv->db->strings, val );
281 return MSI_RecordGetString( record, ++wv->rec_index );
284 ERR("Invalid expression type\n");
290 static UINT STRCMP_Evaluate( MSIWHEREVIEW *wv, UINT row, const struct expr *cond,
291 INT *val, const MSIRECORD *record )
294 const WCHAR *l_str, *r_str;
296 l_str = STRING_evaluate( wv, row, cond->u.expr.left, record );
297 r_str = STRING_evaluate( wv, row, cond->u.expr.right, record );
298 if( l_str == r_str ||
299 ((!l_str || !*l_str) && (!r_str || !*r_str)) )
301 else if( l_str && ! r_str )
303 else if( r_str && ! l_str )
306 sr = lstrcmpW( l_str, r_str );
308 *val = ( cond->u.expr.op == OP_EQ && ( sr == 0 ) ) ||
309 ( cond->u.expr.op == OP_NE && ( sr != 0 ) ) ||
310 ( cond->u.expr.op == OP_LT && ( sr < 0 ) ) ||
311 ( cond->u.expr.op == OP_GT && ( sr > 0 ) );
313 return ERROR_SUCCESS;
316 static UINT WHERE_evaluate( MSIWHEREVIEW *wv, UINT row,
317 struct expr *cond, INT *val, MSIRECORD *record )
323 return ERROR_SUCCESS;
327 case EXPR_COL_NUMBER:
328 r = wv->table->ops->fetch_int( wv->table, row, cond->u.col_number, &tval );
329 *val = tval - 0x8000;
330 return ERROR_SUCCESS;
332 case EXPR_COL_NUMBER32:
333 r = wv->table->ops->fetch_int( wv->table, row, cond->u.col_number, &tval );
334 *val = tval - 0x80000000;
339 return ERROR_SUCCESS;
342 r = WHERE_evaluate( wv, row, cond->u.expr.left, &lval, record );
343 if( r != ERROR_SUCCESS )
345 r = WHERE_evaluate( wv, row, cond->u.expr.right, &rval, record );
346 if( r != ERROR_SUCCESS )
348 *val = INT_evaluate_binary( lval, cond->u.expr.op, rval );
349 return ERROR_SUCCESS;
352 r = wv->table->ops->fetch_int( wv->table, row, cond->u.expr.left->u.col_number, &tval );
353 if( r != ERROR_SUCCESS )
355 *val = INT_evaluate_unary( tval, cond->u.expr.op );
356 return ERROR_SUCCESS;
359 return STRCMP_Evaluate( wv, row, cond, val, record );
362 *val = MSI_RecordGetInteger( record, ++wv->rec_index );
363 return ERROR_SUCCESS;
366 ERR("Invalid expression type\n");
370 return ERROR_SUCCESS;
373 static UINT WHERE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
375 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
376 UINT count = 0, r, i;
378 MSIVIEW *table = wv->table;
380 TRACE("%p %p\n", wv, record);
383 return ERROR_FUNCTION_FAILED;
385 r = table->ops->execute( table, record );
386 if( r != ERROR_SUCCESS )
389 r = table->ops->get_dimensions( table, &count, NULL );
390 if( r != ERROR_SUCCESS )
393 free_hash_table(wv->reorder);
394 wv->reorder = msi_alloc_zero(MSI_HASH_TABLE_SIZE * sizeof(MSIHASHENTRY *));
396 return ERROR_OUTOFMEMORY;
399 if (wv->cond->type == EXPR_STRCMP)
401 MSIITERHANDLE handle = NULL;
402 UINT row, value, col;
403 struct expr *col_cond = wv->cond->u.expr.left;
404 struct expr *val_cond = wv->cond->u.expr.right;
406 /* swap conditionals */
407 if (col_cond->type != EXPR_COL_NUMBER_STRING)
409 val_cond = wv->cond->u.expr.left;
410 col_cond = wv->cond->u.expr.right;
413 if ((col_cond->type == EXPR_COL_NUMBER_STRING) && (val_cond->type == EXPR_SVAL))
415 col = col_cond->u.col_number;
416 /* special case for "" - translate it into nil */
417 if (!val_cond->u.sval[0])
421 r = msi_string2idW(wv->db->strings, val_cond->u.sval, &value);
422 if (r != ERROR_SUCCESS)
424 TRACE("no id for %s, assuming it doesn't exist in the table\n", debugstr_w(wv->cond->u.expr.right->u.sval));
425 return ERROR_SUCCESS;
431 r = table->ops->find_matching_rows(table, col, value, &row, &handle);
432 if (r == ERROR_SUCCESS)
433 add_entry_to_hash(wv->reorder, wv->row_count++, row);
434 } while (r == ERROR_SUCCESS);
436 if (r == ERROR_NO_MORE_ITEMS)
437 return ERROR_SUCCESS;
441 /* else fallback to slow case */
444 for( i=0; i<count; i++ )
448 r = WHERE_evaluate( wv, i, wv->cond, &val, record );
449 if( r != ERROR_SUCCESS )
452 add_entry_to_hash( wv->reorder, wv->row_count++, i );
455 return ERROR_SUCCESS;
458 static UINT WHERE_close( struct tagMSIVIEW *view )
460 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
465 return ERROR_FUNCTION_FAILED;
467 return wv->table->ops->close( wv->table );
470 static UINT WHERE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols )
472 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
474 TRACE("%p %p %p\n", wv, rows, cols );
477 return ERROR_FUNCTION_FAILED;
482 return ERROR_FUNCTION_FAILED;
483 *rows = wv->row_count;
486 return wv->table->ops->get_dimensions( wv->table, NULL, cols );
489 static UINT WHERE_get_column_info( struct tagMSIVIEW *view,
490 UINT n, LPWSTR *name, UINT *type )
492 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
494 TRACE("%p %d %p %p\n", wv, n, name, type );
497 return ERROR_FUNCTION_FAILED;
499 return wv->table->ops->get_column_info( wv->table, n, name, type );
502 static UINT WHERE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
503 MSIRECORD *rec, UINT row )
505 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
507 TRACE("%p %d %p\n", wv, eModifyMode, rec );
510 return ERROR_FUNCTION_FAILED;
512 return wv->table->ops->modify( wv->table, eModifyMode, rec, row );
515 static UINT WHERE_delete( struct tagMSIVIEW *view )
517 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
522 wv->table->ops->delete( wv->table );
525 free_hash_table(wv->reorder);
529 msiobj_release( &wv->db->hdr );
532 return ERROR_SUCCESS;
535 static UINT WHERE_find_matching_rows( struct tagMSIVIEW *view, UINT col,
536 UINT val, UINT *row, MSIITERHANDLE *handle )
538 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
541 TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
544 return ERROR_FUNCTION_FAILED;
546 r = wv->table->ops->find_matching_rows( wv->table, col, val, row, handle );
547 if (r != ERROR_SUCCESS)
550 if( *row > wv->row_count )
551 return ERROR_NO_MORE_ITEMS;
553 return find_entry_in_hash(wv->reorder, *row, row);
556 static UINT WHERE_sort(struct tagMSIVIEW *view, column_info *columns)
558 MSIWHEREVIEW *wv = (MSIWHEREVIEW *)view;
560 TRACE("%p %p\n", view, columns);
562 return wv->table->ops->sort(wv->table, columns);
565 static const MSIVIEWOPS where_ops =
575 WHERE_get_dimensions,
576 WHERE_get_column_info,
579 WHERE_find_matching_rows,
587 static UINT WHERE_VerifyCondition( MSIDATABASE *db, MSIVIEW *table, struct expr *cond,
595 r = VIEW_find_column( table, cond->u.column, &val );
596 if( r == ERROR_SUCCESS )
599 r = table->ops->get_column_info( table, val, NULL, &type );
600 if( r == ERROR_SUCCESS )
602 if (type&MSITYPE_STRING)
603 cond->type = EXPR_COL_NUMBER_STRING;
604 else if ((type&0xff) == 4)
605 cond->type = EXPR_COL_NUMBER32;
607 cond->type = EXPR_COL_NUMBER;
608 cond->u.col_number = val;
617 WARN("Couldn't find column %s\n", debugstr_w( cond->u.column ) );
621 r = WHERE_VerifyCondition( db, table, cond->u.expr.left, valid );
622 if( r != ERROR_SUCCESS )
625 return ERROR_SUCCESS;
626 r = WHERE_VerifyCondition( db, table, cond->u.expr.right, valid );
627 if( r != ERROR_SUCCESS )
630 /* check the type of the comparison */
631 if( ( cond->u.expr.left->type == EXPR_SVAL ) ||
632 ( cond->u.expr.left->type == EXPR_COL_NUMBER_STRING ) ||
633 ( cond->u.expr.right->type == EXPR_SVAL ) ||
634 ( cond->u.expr.right->type == EXPR_COL_NUMBER_STRING ) )
636 switch( cond->u.expr.op )
645 return ERROR_INVALID_PARAMETER;
648 /* FIXME: check we're comparing a string to a column */
650 cond->type = EXPR_STRCMP;
655 if ( cond->u.expr.left->type != EXPR_COLUMN )
658 return ERROR_INVALID_PARAMETER;
660 r = WHERE_VerifyCondition( db, table, cond->u.expr.left, valid );
661 if( r != ERROR_SUCCESS )
666 cond->type = EXPR_UVAL;
667 cond->u.uval = cond->u.ival;
676 ERR("Invalid expression type\n");
681 return ERROR_SUCCESS;
684 UINT WHERE_CreateView( MSIDATABASE *db, MSIVIEW **view, MSIVIEW *table,
687 MSIWHEREVIEW *wv = NULL;
688 UINT count = 0, r, valid = 0;
690 TRACE("%p\n", table );
692 r = table->ops->get_dimensions( table, NULL, &count );
693 if( r != ERROR_SUCCESS )
695 ERR("can't get table dimensions\n");
701 r = WHERE_VerifyCondition( db, table, cond, &valid );
702 if( r != ERROR_SUCCESS )
705 return ERROR_FUNCTION_FAILED;
708 wv = msi_alloc_zero( sizeof *wv );
710 return ERROR_FUNCTION_FAILED;
712 /* fill the structure */
713 wv->view.ops = &where_ops;
714 msiobj_addref( &db->hdr );
721 *view = (MSIVIEW*) wv;
723 return ERROR_SUCCESS;