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