msi: Honor msidbFeatureAttributesFollowParent.
[wine] / dlls / msi / where.c
1 /*
2  * Implementation of the Microsoft Installer (msi.dll)
3  *
4  * Copyright 2002 Mike McCormack for CodeWeavers
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdarg.h>
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winerror.h"
26 #include "wine/debug.h"
27 #include "wine/unicode.h"
28 #include "msi.h"
29 #include "msiquery.h"
30 #include "objbase.h"
31 #include "objidl.h"
32 #include "msipriv.h"
33 #include "winnls.h"
34
35 #include "query.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
38
39 #define MSI_HASH_TABLE_SIZE 37
40
41 typedef struct tagMSIHASHENTRY
42 {
43     struct tagMSIHASHENTRY *next;
44     UINT value;
45     UINT row;
46 } MSIHASHENTRY;
47
48 /* below is the query interface to a table */
49
50 typedef struct tagMSIWHEREVIEW
51 {
52     MSIVIEW        view;
53     MSIDATABASE   *db;
54     MSIVIEW       *table;
55     UINT           row_count;
56     MSIHASHENTRY **reorder;
57     struct expr   *cond;
58     UINT           rec_index;
59 } MSIWHEREVIEW;
60
61 static void free_hash_table(MSIHASHENTRY **table)
62 {
63     MSIHASHENTRY *new, *old;
64     int i;
65
66     if (!table)
67         return;
68
69     for (i = 0; i < MSI_HASH_TABLE_SIZE; i++)
70     {
71         new = table[i];
72
73         while (new)
74         {
75             old = new;
76             new = old->next;
77             msi_free(old);
78         }
79
80         table[i] = NULL;
81     }
82
83     msi_free(table);
84 }
85
86 static UINT find_entry_in_hash(MSIHASHENTRY **table, UINT row, UINT *val)
87 {
88     MSIHASHENTRY *entry;
89
90     if (!table)
91         return ERROR_SUCCESS;
92
93     if (!(entry = table[row % MSI_HASH_TABLE_SIZE]))
94     {
95         WARN("Row not found in hash table!\n");
96         return ERROR_FUNCTION_FAILED;
97     }
98
99     while (entry && entry->row != row)
100         entry = entry->next;
101
102     if (entry) *val = entry->value;
103     return ERROR_SUCCESS;
104 }
105
106 static UINT add_entry_to_hash(MSIHASHENTRY **table, UINT row, UINT val)
107 {
108     MSIHASHENTRY *new = msi_alloc(sizeof(MSIHASHENTRY));
109     MSIHASHENTRY *prev;
110
111     if (!new)
112         return ERROR_OUTOFMEMORY;
113
114     new->next = NULL;
115     new->value = val;
116     new->row = row;
117
118     prev = table[row % MSI_HASH_TABLE_SIZE];
119     if (prev)
120         new->next = prev;
121
122     table[row % MSI_HASH_TABLE_SIZE] = new;
123
124     return ERROR_SUCCESS;
125 }
126
127 static UINT WHERE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
128 {
129     MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
130     UINT r;
131
132     TRACE("%p %d %d %p\n", wv, row, col, val );
133
134     if( !wv->table )
135         return ERROR_FUNCTION_FAILED;
136
137     if( row > wv->row_count )
138         return ERROR_NO_MORE_ITEMS;
139
140     r = find_entry_in_hash(wv->reorder, row, &row);
141     if (r != ERROR_SUCCESS)
142         return r;
143
144     return wv->table->ops->fetch_int( wv->table, row, col, val );
145 }
146
147 static UINT WHERE_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm )
148 {
149     MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
150     UINT r;
151
152     TRACE("%p %d %d %p\n", wv, row, col, stm );
153
154     if( !wv->table )
155         return ERROR_FUNCTION_FAILED;
156
157     if( row > wv->row_count )
158         return ERROR_NO_MORE_ITEMS;
159
160     r = find_entry_in_hash(wv->reorder, row, &row);
161     if (r != ERROR_SUCCESS)
162         return r;
163
164     return wv->table->ops->fetch_stream( wv->table, row, col, stm );
165 }
166
167 static UINT WHERE_get_row( struct tagMSIVIEW *view, UINT row, MSIRECORD **rec )
168 {
169     MSIWHEREVIEW *wv = (MSIWHEREVIEW *)view;
170     UINT r;
171
172     TRACE("%p %d %p\n", wv, row, rec );
173
174     if (!wv->table)
175         return ERROR_FUNCTION_FAILED;
176
177     if (row > wv->row_count)
178         return ERROR_NO_MORE_ITEMS;
179
180     r = find_entry_in_hash(wv->reorder, row, &row);
181     if (r != ERROR_SUCCESS)
182         return r;
183
184     return wv->table->ops->get_row(wv->table, row, rec);
185 }
186
187 static UINT WHERE_set_row( struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask )
188 {
189     MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
190     UINT r;
191
192     TRACE("%p %d %p %08x\n", wv, row, rec, mask );
193
194     if( !wv->table )
195          return ERROR_FUNCTION_FAILED;
196
197     if( row > wv->row_count )
198         return ERROR_NO_MORE_ITEMS;
199
200     r = find_entry_in_hash(wv->reorder, row, &row);
201     if (r != ERROR_SUCCESS)
202         return r;
203
204     return wv->table->ops->set_row( wv->table, row, rec, mask );
205 }
206
207 static UINT WHERE_delete_row(struct tagMSIVIEW *view, UINT row)
208 {
209     MSIWHEREVIEW *wv = (MSIWHEREVIEW *)view;
210     UINT r;
211
212     TRACE("(%p %d)\n", view, row);
213
214     if ( !wv->table )
215         return ERROR_FUNCTION_FAILED;
216
217     if ( row > wv->row_count )
218         return ERROR_NO_MORE_ITEMS;
219
220     r = find_entry_in_hash( wv->reorder, row, &row );
221     if ( r != ERROR_SUCCESS )
222         return r;
223
224     return wv->table->ops->delete_row( wv->table, row );
225 }
226
227 static INT INT_evaluate_binary( INT lval, UINT op, INT rval )
228 {
229     switch( op )
230     {
231     case OP_EQ:
232         return ( lval == rval );
233     case OP_AND:
234         return ( lval && rval );
235     case OP_OR:
236         return ( lval || rval );
237     case OP_GT:
238         return ( lval > rval );
239     case OP_LT:
240         return ( lval < rval );
241     case OP_LE:
242         return ( lval <= rval );
243     case OP_GE:
244         return ( lval >= rval );
245     case OP_NE:
246         return ( lval != rval );
247     default:
248         ERR("Unknown operator %d\n", op );
249     }
250     return 0;
251 }
252
253 static INT INT_evaluate_unary( INT lval, UINT op )
254 {
255     switch( op )
256     {
257     case OP_ISNULL:
258         return ( !lval );
259     case OP_NOTNULL:
260         return ( lval );
261     default:
262         ERR("Unknown operator %d\n", op );
263     }
264     return 0;
265 }
266
267 static const WCHAR *STRING_evaluate( MSIWHEREVIEW *wv, UINT row,
268                                      const struct expr *expr,
269                                      const MSIRECORD *record )
270 {
271     UINT val = 0, r;
272
273     switch( expr->type )
274     {
275     case EXPR_COL_NUMBER_STRING:
276         r = wv->table->ops->fetch_int( wv->table, row, expr->u.col_number, &val );
277         if( r != ERROR_SUCCESS )
278             return NULL;
279         return msi_string_lookup_id( wv->db->strings, val );
280
281     case EXPR_SVAL:
282         return expr->u.sval;
283
284     case EXPR_WILDCARD:
285         return MSI_RecordGetString( record, ++wv->rec_index );
286
287     default:
288         ERR("Invalid expression type\n");
289         break;
290     }
291     return NULL;
292 }
293
294 static UINT STRCMP_Evaluate( MSIWHEREVIEW *wv, UINT row, const struct expr *cond,
295                              INT *val, const MSIRECORD *record )
296 {
297     int sr;
298     const WCHAR *l_str, *r_str;
299
300     l_str = STRING_evaluate( wv, row, cond->u.expr.left, record );
301     r_str = STRING_evaluate( wv, row, cond->u.expr.right, record );
302     if( l_str == r_str ||
303         ((!l_str || !*l_str) && (!r_str || !*r_str)) )
304         sr = 0;
305     else if( l_str && ! r_str )
306         sr = 1;
307     else if( r_str && ! l_str )
308         sr = -1;
309     else
310         sr = strcmpW( l_str, r_str );
311
312     *val = ( cond->u.expr.op == OP_EQ && ( sr == 0 ) ) ||
313            ( cond->u.expr.op == OP_NE && ( sr != 0 ) );
314
315     return ERROR_SUCCESS;
316 }
317
318 static UINT WHERE_evaluate( MSIWHEREVIEW *wv, UINT row,
319                             struct expr *cond, INT *val, MSIRECORD *record )
320 {
321     UINT r, tval;
322     INT lval, rval;
323
324     if( !cond )
325         return ERROR_SUCCESS;
326
327     switch( cond->type )
328     {
329     case EXPR_COL_NUMBER:
330         r = wv->table->ops->fetch_int( wv->table, row, cond->u.col_number, &tval );
331         if( r != ERROR_SUCCESS )
332             return r;
333         *val = tval - 0x8000;
334         return ERROR_SUCCESS;
335
336     case EXPR_COL_NUMBER32:
337         r = wv->table->ops->fetch_int( wv->table, row, cond->u.col_number, &tval );
338         if( r != ERROR_SUCCESS )
339             return r;
340         *val = tval - 0x80000000;
341         return r;
342
343     case EXPR_UVAL:
344         *val = cond->u.uval;
345         return ERROR_SUCCESS;
346
347     case EXPR_COMPLEX:
348         r = WHERE_evaluate( wv, row, cond->u.expr.left, &lval, record );
349         if( r != ERROR_SUCCESS )
350             return r;
351         r = WHERE_evaluate( wv, row, cond->u.expr.right, &rval, record );
352         if( r != ERROR_SUCCESS )
353             return r;
354         *val = INT_evaluate_binary( lval, cond->u.expr.op, rval );
355         return ERROR_SUCCESS;
356
357     case EXPR_UNARY:
358         r = wv->table->ops->fetch_int( wv->table, row, cond->u.expr.left->u.col_number, &tval );
359         if( r != ERROR_SUCCESS )
360             return r;
361         *val = INT_evaluate_unary( tval, cond->u.expr.op );
362         return ERROR_SUCCESS;
363
364     case EXPR_STRCMP:
365         return STRCMP_Evaluate( wv, row, cond, val, record );
366
367     case EXPR_WILDCARD:
368         *val = MSI_RecordGetInteger( record, ++wv->rec_index );
369         return ERROR_SUCCESS;
370
371     default:
372         ERR("Invalid expression type\n");
373         break;
374     }
375
376     return ERROR_SUCCESS;
377 }
378
379 static UINT WHERE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
380 {
381     MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
382     UINT count = 0, r, i;
383     INT val;
384     MSIVIEW *table = wv->table;
385
386     TRACE("%p %p\n", wv, record);
387
388     if( !table )
389          return ERROR_FUNCTION_FAILED;
390
391     r = table->ops->execute( table, record );
392     if( r != ERROR_SUCCESS )
393         return r;
394
395     r = table->ops->get_dimensions( table, &count, NULL );
396     if( r != ERROR_SUCCESS )
397         return r;
398
399     free_hash_table(wv->reorder);
400     wv->reorder = msi_alloc_zero(MSI_HASH_TABLE_SIZE * sizeof(MSIHASHENTRY *));
401     if( !wv->reorder )
402         return ERROR_OUTOFMEMORY;
403
404     wv->row_count = 0;
405
406 if (0) /* disable optimization, there's no guarantee that strings are in the string table */
407 {
408     if (wv->cond->type == EXPR_STRCMP)
409     {
410         MSIITERHANDLE handle = NULL;
411         UINT row, value, col;
412         struct expr *col_cond = wv->cond->u.expr.left;
413         struct expr *val_cond = wv->cond->u.expr.right;
414
415         /* swap conditionals */
416         if (col_cond->type != EXPR_COL_NUMBER_STRING)
417         {
418             val_cond = wv->cond->u.expr.left;
419             col_cond = wv->cond->u.expr.right;
420         }
421
422         if ((col_cond->type == EXPR_COL_NUMBER_STRING) && (val_cond->type == EXPR_SVAL))
423         {
424             col = col_cond->u.col_number;
425             /* special case for "" - translate it into nil */
426             if (!val_cond->u.sval[0])
427                 value = 0;
428             else
429             {
430                 r = msi_string2idW(wv->db->strings, val_cond->u.sval, &value);
431                 if (r != ERROR_SUCCESS)
432                 {
433                     TRACE("no id for %s, assuming it doesn't exist in the table\n", debugstr_w(wv->cond->u.expr.right->u.sval));
434                     return ERROR_SUCCESS;
435                 }
436             }
437
438             do
439             {
440                 r = table->ops->find_matching_rows(table, col, value, &row, &handle);
441                 if (r == ERROR_SUCCESS)
442                     add_entry_to_hash(wv->reorder, wv->row_count++, row);
443             } while (r == ERROR_SUCCESS);
444
445             if (r == ERROR_NO_MORE_ITEMS)
446                 return ERROR_SUCCESS;
447             else
448                 return r;
449         }
450         /* else fallback to slow case */
451     }
452 }
453
454     for( i=0; i<count; i++ )
455     {
456         val = 0;
457         wv->rec_index = 0;
458         r = WHERE_evaluate( wv, i, wv->cond, &val, record );
459         if( r != ERROR_SUCCESS )
460             return r;
461         if( val )
462             add_entry_to_hash( wv->reorder, wv->row_count++, i );
463     }
464
465     return ERROR_SUCCESS;
466 }
467
468 static UINT WHERE_close( struct tagMSIVIEW *view )
469 {
470     MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
471
472     TRACE("%p\n", wv );
473
474     if( !wv->table )
475         return ERROR_FUNCTION_FAILED;
476
477     return wv->table->ops->close( wv->table );
478 }
479
480 static UINT WHERE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols )
481 {
482     MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
483
484     TRACE("%p %p %p\n", wv, rows, cols );
485
486     if( !wv->table )
487          return ERROR_FUNCTION_FAILED;
488
489     if( rows )
490     {
491         if( !wv->reorder )
492             return ERROR_FUNCTION_FAILED;
493         *rows = wv->row_count;
494     }
495
496     return wv->table->ops->get_dimensions( wv->table, NULL, cols );
497 }
498
499 static UINT WHERE_get_column_info( struct tagMSIVIEW *view,
500                 UINT n, LPWSTR *name, UINT *type, BOOL *temporary,
501                 LPWSTR *table_name)
502 {
503     MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
504
505     TRACE("%p %d %p %p %p %p\n", wv, n, name, type, temporary, table_name );
506
507     if( !wv->table )
508          return ERROR_FUNCTION_FAILED;
509
510     return wv->table->ops->get_column_info( wv->table, n, name,
511                                             type, temporary, table_name );
512 }
513
514 static UINT WHERE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
515                           MSIRECORD *rec, UINT row )
516 {
517     MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
518
519     TRACE("%p %d %p\n", wv, eModifyMode, rec);
520
521     find_entry_in_hash(wv->reorder, row - 1, &row);
522     row++;
523
524     return wv->table->ops->modify( wv->table, eModifyMode, rec, row );
525 }
526
527 static UINT WHERE_delete( struct tagMSIVIEW *view )
528 {
529     MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
530
531     TRACE("%p\n", wv );
532
533     if( wv->table )
534         wv->table->ops->delete( wv->table );
535     wv->table = 0;
536
537     free_hash_table(wv->reorder);
538     wv->reorder = NULL;
539     wv->row_count = 0;
540
541     msiobj_release( &wv->db->hdr );
542     msi_free( wv );
543
544     return ERROR_SUCCESS;
545 }
546
547 static UINT WHERE_find_matching_rows( struct tagMSIVIEW *view, UINT col,
548     UINT val, UINT *row, MSIITERHANDLE *handle )
549 {
550     MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
551     UINT r;
552
553     TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
554
555     if( !wv->table )
556          return ERROR_FUNCTION_FAILED;
557
558     r = wv->table->ops->find_matching_rows( wv->table, col, val, row, handle );
559     if (r != ERROR_SUCCESS)
560         return r;
561
562     if( *row > wv->row_count )
563         return ERROR_NO_MORE_ITEMS;
564
565     return find_entry_in_hash(wv->reorder, *row, row);
566 }
567
568 static UINT WHERE_sort(struct tagMSIVIEW *view, column_info *columns)
569 {
570     MSIWHEREVIEW *wv = (MSIWHEREVIEW *)view;
571
572     TRACE("%p %p\n", view, columns);
573
574     return wv->table->ops->sort(wv->table, columns);
575 }
576
577 static const MSIVIEWOPS where_ops =
578 {
579     WHERE_fetch_int,
580     WHERE_fetch_stream,
581     WHERE_get_row,
582     WHERE_set_row,
583     NULL,
584     WHERE_delete_row,
585     WHERE_execute,
586     WHERE_close,
587     WHERE_get_dimensions,
588     WHERE_get_column_info,
589     WHERE_modify,
590     WHERE_delete,
591     WHERE_find_matching_rows,
592     NULL,
593     NULL,
594     NULL,
595     NULL,
596     WHERE_sort,
597     NULL,
598 };
599
600 static UINT WHERE_VerifyCondition( MSIDATABASE *db, MSIVIEW *table, struct expr *cond,
601                                    UINT *valid )
602 {
603     UINT r, val = 0;
604
605     switch( cond->type )
606     {
607     case EXPR_COLUMN:
608         r = VIEW_find_column( table, cond->u.column.column,
609                               cond->u.column.table, &val );
610         if( r == ERROR_SUCCESS )
611         {
612             UINT type = 0;
613             r = table->ops->get_column_info( table, val, NULL, &type,
614                                              NULL, NULL );
615             if( r == ERROR_SUCCESS )
616             {
617                 if (type&MSITYPE_STRING)
618                     cond->type = EXPR_COL_NUMBER_STRING;
619                 else if ((type&0xff) == 4)
620                     cond->type = EXPR_COL_NUMBER32;
621                 else
622                     cond->type = EXPR_COL_NUMBER;
623                 cond->u.col_number = val;
624                 *valid = 1;
625             }
626             else
627                 *valid = 0;
628         }
629         else
630         {
631             *valid = 0;
632             WARN("Couldn't find column %s.%s\n", debugstr_w( cond->u.column.table ), debugstr_w( cond->u.column.column ) );
633         }
634         break;
635     case EXPR_COMPLEX:
636         r = WHERE_VerifyCondition( db, table, cond->u.expr.left, valid );
637         if( r != ERROR_SUCCESS )
638             return r;
639         if( !*valid )
640             return ERROR_SUCCESS;
641         r = WHERE_VerifyCondition( db, table, cond->u.expr.right, valid );
642         if( r != ERROR_SUCCESS )
643             return r;
644
645         /* check the type of the comparison */
646         if( ( cond->u.expr.left->type == EXPR_SVAL ) ||
647             ( cond->u.expr.left->type == EXPR_COL_NUMBER_STRING ) ||
648             ( cond->u.expr.right->type == EXPR_SVAL ) ||
649             ( cond->u.expr.right->type == EXPR_COL_NUMBER_STRING ) )
650         {
651             switch( cond->u.expr.op )
652             {
653             case OP_EQ:
654             case OP_NE:
655                 break;
656             default:
657                 *valid = FALSE;
658                 return ERROR_INVALID_PARAMETER;
659             }
660
661             /* FIXME: check we're comparing a string to a column */
662
663             cond->type = EXPR_STRCMP;
664         }
665
666         break;
667     case EXPR_UNARY:
668         if ( cond->u.expr.left->type != EXPR_COLUMN )
669         {
670             *valid = FALSE;
671             return ERROR_INVALID_PARAMETER;
672         }
673         r = WHERE_VerifyCondition( db, table, cond->u.expr.left, valid );
674         if( r != ERROR_SUCCESS )
675             return r;
676         break;
677     case EXPR_IVAL:
678         *valid = 1;
679         cond->type = EXPR_UVAL;
680         cond->u.uval = cond->u.ival;
681         break;
682     case EXPR_WILDCARD:
683         *valid = 1;
684         break;
685     case EXPR_SVAL:
686         *valid = 1;
687         break;
688     default:
689         ERR("Invalid expression type\n");
690         *valid = 0;
691         break;
692     }
693
694     return ERROR_SUCCESS;
695 }
696
697 UINT WHERE_CreateView( MSIDATABASE *db, MSIVIEW **view, MSIVIEW *table,
698                        struct expr *cond )
699 {
700     MSIWHEREVIEW *wv = NULL;
701     UINT count = 0, r, valid = 0;
702
703     TRACE("%p\n", table );
704
705     r = table->ops->get_dimensions( table, NULL, &count );
706     if( r != ERROR_SUCCESS )
707     {
708         ERR("can't get table dimensions\n");
709         return r;
710     }
711
712     if( cond )
713     {
714         r = WHERE_VerifyCondition( db, table, cond, &valid );
715         if( r != ERROR_SUCCESS )
716             return r;
717         if( !valid )
718             return ERROR_FUNCTION_FAILED;
719     }
720
721     wv = msi_alloc_zero( sizeof *wv );
722     if( !wv )
723         return ERROR_FUNCTION_FAILED;
724     
725     /* fill the structure */
726     wv->view.ops = &where_ops;
727     msiobj_addref( &db->hdr );
728     wv->db = db;
729     wv->table = table;
730     wv->row_count = 0;
731     wv->reorder = NULL;
732     wv->cond = cond;
733     wv->rec_index = 0;
734     *view = (MSIVIEW*) wv;
735
736     return ERROR_SUCCESS;
737 }