gameux: Add declaration of IGameStatisticsMgr interface.
[wine/hacks.git] / dlls / msi / where.c
blob545d6a84dc66b26b2455e62f1b01daacc421afec
1 /*
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
21 #include <stdarg.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winerror.h"
26 #include "wine/debug.h"
27 #include "msi.h"
28 #include "msiquery.h"
29 #include "objbase.h"
30 #include "objidl.h"
31 #include "msipriv.h"
32 #include "winnls.h"
34 #include "query.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
38 #define MSI_HASH_TABLE_SIZE 37
40 typedef struct tagMSIHASHENTRY
42 struct tagMSIHASHENTRY *next;
43 UINT value;
44 UINT row;
45 } MSIHASHENTRY;
47 /* below is the query interface to a table */
49 typedef struct tagMSIWHEREVIEW
51 MSIVIEW view;
52 MSIDATABASE *db;
53 MSIVIEW *table;
54 UINT row_count;
55 MSIHASHENTRY **reorder;
56 struct expr *cond;
57 UINT rec_index;
58 } MSIWHEREVIEW;
60 static void free_hash_table(MSIHASHENTRY **table)
62 MSIHASHENTRY *new, *old;
63 int i;
65 if (!table)
66 return;
68 for (i = 0; i < MSI_HASH_TABLE_SIZE; i++)
70 new = table[i];
72 while (new)
74 old = new;
75 new = old->next;
76 msi_free(old);
79 table[i] = NULL;
82 msi_free(table);
85 static UINT find_entry_in_hash(MSIHASHENTRY **table, UINT row, UINT *val)
87 MSIHASHENTRY *entry;
89 if (!table)
90 return ERROR_SUCCESS;
92 if (!(entry = table[row % MSI_HASH_TABLE_SIZE]))
94 WARN("Row not found in hash table!\n");
95 return ERROR_FUNCTION_FAILED;
98 while (entry && entry->row != row)
99 entry = entry->next;
101 if (entry) *val = entry->value;
102 return ERROR_SUCCESS;
105 static UINT add_entry_to_hash(MSIHASHENTRY **table, UINT row, UINT val)
107 MSIHASHENTRY *new = msi_alloc(sizeof(MSIHASHENTRY));
108 MSIHASHENTRY *prev;
110 if (!new)
111 return ERROR_OUTOFMEMORY;
113 new->next = NULL;
114 new->value = val;
115 new->row = row;
117 prev = table[row % MSI_HASH_TABLE_SIZE];
118 if (prev)
119 new->next = prev;
121 table[row % MSI_HASH_TABLE_SIZE] = new;
123 return ERROR_SUCCESS;
126 static UINT WHERE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
128 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
129 UINT r;
131 TRACE("%p %d %d %p\n", wv, row, col, val );
133 if( !wv->table )
134 return ERROR_FUNCTION_FAILED;
136 if( row > wv->row_count )
137 return ERROR_NO_MORE_ITEMS;
139 r = find_entry_in_hash(wv->reorder, row, &row);
140 if (r != ERROR_SUCCESS)
141 return r;
143 return wv->table->ops->fetch_int( wv->table, row, col, val );
146 static UINT WHERE_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm )
148 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
149 UINT r;
151 TRACE("%p %d %d %p\n", wv, row, col, stm );
153 if( !wv->table )
154 return ERROR_FUNCTION_FAILED;
156 if( row > wv->row_count )
157 return ERROR_NO_MORE_ITEMS;
159 r = find_entry_in_hash(wv->reorder, row, &row);
160 if (r != ERROR_SUCCESS)
161 return r;
163 return wv->table->ops->fetch_stream( wv->table, row, col, stm );
166 static UINT WHERE_get_row( struct tagMSIVIEW *view, UINT row, MSIRECORD **rec )
168 MSIWHEREVIEW *wv = (MSIWHEREVIEW *)view;
169 UINT r;
171 TRACE("%p %d %p\n", wv, row, rec );
173 if (!wv->table)
174 return ERROR_FUNCTION_FAILED;
176 if (row > wv->row_count)
177 return ERROR_NO_MORE_ITEMS;
179 r = find_entry_in_hash(wv->reorder, row, &row);
180 if (r != ERROR_SUCCESS)
181 return r;
183 return wv->table->ops->get_row(wv->table, row, rec);
186 static UINT WHERE_set_row( struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask )
188 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
189 UINT r;
191 TRACE("%p %d %p %08x\n", wv, row, rec, mask );
193 if( !wv->table )
194 return ERROR_FUNCTION_FAILED;
196 if( row > wv->row_count )
197 return ERROR_NO_MORE_ITEMS;
199 r = find_entry_in_hash(wv->reorder, row, &row);
200 if (r != ERROR_SUCCESS)
201 return r;
203 return wv->table->ops->set_row( wv->table, row, rec, mask );
206 static UINT WHERE_delete_row(struct tagMSIVIEW *view, UINT row)
208 MSIWHEREVIEW *wv = (MSIWHEREVIEW *)view;
209 UINT r;
211 TRACE("(%p %d)\n", view, row);
213 if ( !wv->table )
214 return ERROR_FUNCTION_FAILED;
216 if ( row > wv->row_count )
217 return ERROR_NO_MORE_ITEMS;
219 r = find_entry_in_hash( wv->reorder, row, &row );
220 if ( r != ERROR_SUCCESS )
221 return r;
223 return wv->table->ops->delete_row( wv->table, row );
226 static INT INT_evaluate_binary( INT lval, UINT op, INT rval )
228 switch( op )
230 case OP_EQ:
231 return ( lval == rval );
232 case OP_AND:
233 return ( lval && rval );
234 case OP_OR:
235 return ( lval || rval );
236 case OP_GT:
237 return ( lval > rval );
238 case OP_LT:
239 return ( lval < rval );
240 case OP_LE:
241 return ( lval <= rval );
242 case OP_GE:
243 return ( lval >= rval );
244 case OP_NE:
245 return ( lval != rval );
246 default:
247 ERR("Unknown operator %d\n", op );
249 return 0;
252 static INT INT_evaluate_unary( INT lval, UINT op )
254 switch( op )
256 case OP_ISNULL:
257 return ( !lval );
258 case OP_NOTNULL:
259 return ( lval );
260 default:
261 ERR("Unknown operator %d\n", op );
263 return 0;
266 static const WCHAR *STRING_evaluate( MSIWHEREVIEW *wv, UINT row,
267 const struct expr *expr,
268 const MSIRECORD *record )
270 UINT val = 0, r;
272 switch( expr->type )
274 case EXPR_COL_NUMBER_STRING:
275 r = wv->table->ops->fetch_int( wv->table, row, expr->u.col_number, &val );
276 if( r != ERROR_SUCCESS )
277 return NULL;
278 return msi_string_lookup_id( wv->db->strings, val );
280 case EXPR_SVAL:
281 return expr->u.sval;
283 case EXPR_WILDCARD:
284 return MSI_RecordGetString( record, ++wv->rec_index );
286 default:
287 ERR("Invalid expression type\n");
288 break;
290 return NULL;
293 static UINT STRCMP_Evaluate( MSIWHEREVIEW *wv, UINT row, const struct expr *cond,
294 INT *val, const MSIRECORD *record )
296 int sr;
297 const WCHAR *l_str, *r_str;
299 l_str = STRING_evaluate( wv, row, cond->u.expr.left, record );
300 r_str = STRING_evaluate( wv, row, cond->u.expr.right, record );
301 if( l_str == r_str ||
302 ((!l_str || !*l_str) && (!r_str || !*r_str)) )
303 sr = 0;
304 else if( l_str && ! r_str )
305 sr = 1;
306 else if( r_str && ! l_str )
307 sr = -1;
308 else
309 sr = lstrcmpW( l_str, r_str );
311 *val = ( cond->u.expr.op == OP_EQ && ( sr == 0 ) ) ||
312 ( cond->u.expr.op == OP_NE && ( sr != 0 ) );
314 return ERROR_SUCCESS;
317 static UINT WHERE_evaluate( MSIWHEREVIEW *wv, UINT row,
318 struct expr *cond, INT *val, MSIRECORD *record )
320 UINT r, tval;
321 INT lval, rval;
323 if( !cond )
324 return ERROR_SUCCESS;
326 switch( cond->type )
328 case EXPR_COL_NUMBER:
329 r = wv->table->ops->fetch_int( wv->table, row, cond->u.col_number, &tval );
330 *val = tval - 0x8000;
331 return ERROR_SUCCESS;
333 case EXPR_COL_NUMBER32:
334 r = wv->table->ops->fetch_int( wv->table, row, cond->u.col_number, &tval );
335 *val = tval - 0x80000000;
336 return r;
338 case EXPR_UVAL:
339 *val = cond->u.uval;
340 return ERROR_SUCCESS;
342 case EXPR_COMPLEX:
343 r = WHERE_evaluate( wv, row, cond->u.expr.left, &lval, record );
344 if( r != ERROR_SUCCESS )
345 return r;
346 r = WHERE_evaluate( wv, row, cond->u.expr.right, &rval, record );
347 if( r != ERROR_SUCCESS )
348 return r;
349 *val = INT_evaluate_binary( lval, cond->u.expr.op, rval );
350 return ERROR_SUCCESS;
352 case EXPR_UNARY:
353 r = wv->table->ops->fetch_int( wv->table, row, cond->u.expr.left->u.col_number, &tval );
354 if( r != ERROR_SUCCESS )
355 return r;
356 *val = INT_evaluate_unary( tval, cond->u.expr.op );
357 return ERROR_SUCCESS;
359 case EXPR_STRCMP:
360 return STRCMP_Evaluate( wv, row, cond, val, record );
362 case EXPR_WILDCARD:
363 *val = MSI_RecordGetInteger( record, ++wv->rec_index );
364 return ERROR_SUCCESS;
366 default:
367 ERR("Invalid expression type\n");
368 break;
371 return ERROR_SUCCESS;
374 static UINT WHERE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
376 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
377 UINT count = 0, r, i;
378 INT val;
379 MSIVIEW *table = wv->table;
381 TRACE("%p %p\n", wv, record);
383 if( !table )
384 return ERROR_FUNCTION_FAILED;
386 r = table->ops->execute( table, record );
387 if( r != ERROR_SUCCESS )
388 return r;
390 r = table->ops->get_dimensions( table, &count, NULL );
391 if( r != ERROR_SUCCESS )
392 return r;
394 free_hash_table(wv->reorder);
395 wv->reorder = msi_alloc_zero(MSI_HASH_TABLE_SIZE * sizeof(MSIHASHENTRY *));
396 if( !wv->reorder )
397 return ERROR_OUTOFMEMORY;
399 wv->row_count = 0;
400 if (wv->cond->type == EXPR_STRCMP)
402 MSIITERHANDLE handle = NULL;
403 UINT row, value, col;
404 struct expr *col_cond = wv->cond->u.expr.left;
405 struct expr *val_cond = wv->cond->u.expr.right;
407 /* swap conditionals */
408 if (col_cond->type != EXPR_COL_NUMBER_STRING)
410 val_cond = wv->cond->u.expr.left;
411 col_cond = wv->cond->u.expr.right;
414 if ((col_cond->type == EXPR_COL_NUMBER_STRING) && (val_cond->type == EXPR_SVAL))
416 col = col_cond->u.col_number;
417 /* special case for "" - translate it into nil */
418 if (!val_cond->u.sval[0])
419 value = 0;
420 else
422 r = msi_string2idW(wv->db->strings, val_cond->u.sval, &value);
423 if (r != ERROR_SUCCESS)
425 TRACE("no id for %s, assuming it doesn't exist in the table\n", debugstr_w(wv->cond->u.expr.right->u.sval));
426 return ERROR_SUCCESS;
432 r = table->ops->find_matching_rows(table, col, value, &row, &handle);
433 if (r == ERROR_SUCCESS)
434 add_entry_to_hash(wv->reorder, wv->row_count++, row);
435 } while (r == ERROR_SUCCESS);
437 if (r == ERROR_NO_MORE_ITEMS)
438 return ERROR_SUCCESS;
439 else
440 return r;
442 /* else fallback to slow case */
445 for( i=0; i<count; i++ )
447 val = 0;
448 wv->rec_index = 0;
449 r = WHERE_evaluate( wv, i, wv->cond, &val, record );
450 if( r != ERROR_SUCCESS )
451 return r;
452 if( val )
453 add_entry_to_hash( wv->reorder, wv->row_count++, i );
456 return ERROR_SUCCESS;
459 static UINT WHERE_close( struct tagMSIVIEW *view )
461 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
463 TRACE("%p\n", wv );
465 if( !wv->table )
466 return ERROR_FUNCTION_FAILED;
468 return wv->table->ops->close( wv->table );
471 static UINT WHERE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols )
473 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
475 TRACE("%p %p %p\n", wv, rows, cols );
477 if( !wv->table )
478 return ERROR_FUNCTION_FAILED;
480 if( rows )
482 if( !wv->reorder )
483 return ERROR_FUNCTION_FAILED;
484 *rows = wv->row_count;
487 return wv->table->ops->get_dimensions( wv->table, NULL, cols );
490 static UINT WHERE_get_column_info( struct tagMSIVIEW *view,
491 UINT n, LPWSTR *name, UINT *type, BOOL *temporary,
492 LPWSTR *table_name)
494 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
496 TRACE("%p %d %p %p %p %p\n", wv, n, name, type, temporary, table_name );
498 if( !wv->table )
499 return ERROR_FUNCTION_FAILED;
501 return wv->table->ops->get_column_info( wv->table, n, name,
502 type, temporary, table_name );
505 static UINT WHERE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
506 MSIRECORD *rec, UINT row )
508 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
510 TRACE("%p %d %p\n", wv, eModifyMode, rec);
512 find_entry_in_hash(wv->reorder, row - 1, &row);
513 row++;
515 return wv->table->ops->modify( wv->table, eModifyMode, rec, row );
518 static UINT WHERE_delete( struct tagMSIVIEW *view )
520 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
522 TRACE("%p\n", wv );
524 if( wv->table )
525 wv->table->ops->delete( wv->table );
526 wv->table = 0;
528 free_hash_table(wv->reorder);
529 wv->reorder = NULL;
530 wv->row_count = 0;
532 msiobj_release( &wv->db->hdr );
533 msi_free( wv );
535 return ERROR_SUCCESS;
538 static UINT WHERE_find_matching_rows( struct tagMSIVIEW *view, UINT col,
539 UINT val, UINT *row, MSIITERHANDLE *handle )
541 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
542 UINT r;
544 TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
546 if( !wv->table )
547 return ERROR_FUNCTION_FAILED;
549 r = wv->table->ops->find_matching_rows( wv->table, col, val, row, handle );
550 if (r != ERROR_SUCCESS)
551 return r;
553 if( *row > wv->row_count )
554 return ERROR_NO_MORE_ITEMS;
556 return find_entry_in_hash(wv->reorder, *row, row);
559 static UINT WHERE_sort(struct tagMSIVIEW *view, column_info *columns)
561 MSIWHEREVIEW *wv = (MSIWHEREVIEW *)view;
563 TRACE("%p %p\n", view, columns);
565 return wv->table->ops->sort(wv->table, columns);
568 static const MSIVIEWOPS where_ops =
570 WHERE_fetch_int,
571 WHERE_fetch_stream,
572 WHERE_get_row,
573 WHERE_set_row,
574 NULL,
575 WHERE_delete_row,
576 WHERE_execute,
577 WHERE_close,
578 WHERE_get_dimensions,
579 WHERE_get_column_info,
580 WHERE_modify,
581 WHERE_delete,
582 WHERE_find_matching_rows,
583 NULL,
584 NULL,
585 NULL,
586 NULL,
587 WHERE_sort,
588 NULL,
591 static UINT WHERE_VerifyCondition( MSIDATABASE *db, MSIVIEW *table, struct expr *cond,
592 UINT *valid )
594 UINT r, val = 0;
596 switch( cond->type )
598 case EXPR_COLUMN:
599 r = VIEW_find_column( table, cond->u.column.column,
600 cond->u.column.table, &val );
601 if( r == ERROR_SUCCESS )
603 UINT type = 0;
604 r = table->ops->get_column_info( table, val, NULL, &type,
605 NULL, NULL );
606 if( r == ERROR_SUCCESS )
608 if (type&MSITYPE_STRING)
609 cond->type = EXPR_COL_NUMBER_STRING;
610 else if ((type&0xff) == 4)
611 cond->type = EXPR_COL_NUMBER32;
612 else
613 cond->type = EXPR_COL_NUMBER;
614 cond->u.col_number = val;
615 *valid = 1;
617 else
618 *valid = 0;
620 else
622 *valid = 0;
623 WARN("Couldn't find column %s.%s\n", debugstr_w( cond->u.column.table ), debugstr_w( cond->u.column.column ) );
625 break;
626 case EXPR_COMPLEX:
627 r = WHERE_VerifyCondition( db, table, cond->u.expr.left, valid );
628 if( r != ERROR_SUCCESS )
629 return r;
630 if( !*valid )
631 return ERROR_SUCCESS;
632 r = WHERE_VerifyCondition( db, table, cond->u.expr.right, valid );
633 if( r != ERROR_SUCCESS )
634 return r;
636 /* check the type of the comparison */
637 if( ( cond->u.expr.left->type == EXPR_SVAL ) ||
638 ( cond->u.expr.left->type == EXPR_COL_NUMBER_STRING ) ||
639 ( cond->u.expr.right->type == EXPR_SVAL ) ||
640 ( cond->u.expr.right->type == EXPR_COL_NUMBER_STRING ) )
642 switch( cond->u.expr.op )
644 case OP_EQ:
645 case OP_NE:
646 break;
647 default:
648 *valid = FALSE;
649 return ERROR_INVALID_PARAMETER;
652 /* FIXME: check we're comparing a string to a column */
654 cond->type = EXPR_STRCMP;
657 break;
658 case EXPR_UNARY:
659 if ( cond->u.expr.left->type != EXPR_COLUMN )
661 *valid = FALSE;
662 return ERROR_INVALID_PARAMETER;
664 r = WHERE_VerifyCondition( db, table, cond->u.expr.left, valid );
665 if( r != ERROR_SUCCESS )
666 return r;
667 break;
668 case EXPR_IVAL:
669 *valid = 1;
670 cond->type = EXPR_UVAL;
671 cond->u.uval = cond->u.ival;
672 break;
673 case EXPR_WILDCARD:
674 *valid = 1;
675 break;
676 case EXPR_SVAL:
677 *valid = 1;
678 break;
679 default:
680 ERR("Invalid expression type\n");
681 *valid = 0;
682 break;
685 return ERROR_SUCCESS;
688 UINT WHERE_CreateView( MSIDATABASE *db, MSIVIEW **view, MSIVIEW *table,
689 struct expr *cond )
691 MSIWHEREVIEW *wv = NULL;
692 UINT count = 0, r, valid = 0;
694 TRACE("%p\n", table );
696 r = table->ops->get_dimensions( table, NULL, &count );
697 if( r != ERROR_SUCCESS )
699 ERR("can't get table dimensions\n");
700 return r;
703 if( cond )
705 r = WHERE_VerifyCondition( db, table, cond, &valid );
706 if( r != ERROR_SUCCESS )
707 return r;
708 if( !valid )
709 return ERROR_FUNCTION_FAILED;
712 wv = msi_alloc_zero( sizeof *wv );
713 if( !wv )
714 return ERROR_FUNCTION_FAILED;
716 /* fill the structure */
717 wv->view.ops = &where_ops;
718 msiobj_addref( &db->hdr );
719 wv->db = db;
720 wv->table = table;
721 wv->row_count = 0;
722 wv->reorder = NULL;
723 wv->cond = cond;
724 wv->rec_index = 0;
725 *view = (MSIVIEW*) wv;
727 return ERROR_SUCCESS;