wined3d: Make WINED3D_ATR macros more flexible.
[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
39 /* below is the query interface to a table */
40
41 typedef struct tagMSIWHEREVIEW
42 {
43     MSIVIEW        view;
44     MSIDATABASE   *db;
45     MSIVIEW       *table;
46     UINT           row_count;
47     UINT          *reorder;
48     struct expr   *cond;
49 } MSIWHEREVIEW;
50
51 static UINT WHERE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
52 {
53     MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
54
55     TRACE("%p %d %d %p\n", wv, row, col, val );
56
57     if( !wv->table )
58         return ERROR_FUNCTION_FAILED;
59
60     if( row > wv->row_count )
61         return ERROR_NO_MORE_ITEMS;
62
63     row = wv->reorder[ row ];
64
65     return wv->table->ops->fetch_int( wv->table, row, col, val );
66 }
67
68 static UINT WHERE_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm )
69 {
70     MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
71
72     TRACE("%p %d %d %p\n", wv, row, col, stm );
73
74     if( !wv->table )
75         return ERROR_FUNCTION_FAILED;
76
77     if( row > wv->row_count )
78         return ERROR_NO_MORE_ITEMS;
79
80     row = wv->reorder[ row ];
81
82     return wv->table->ops->fetch_stream( wv->table, row, col, stm );
83 }
84
85 static UINT WHERE_set_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT val )
86 {
87     MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
88
89     TRACE("%p %d %d %04x\n", wv, row, col, val );
90
91     if( !wv->table )
92          return ERROR_FUNCTION_FAILED;
93     
94     if( row > wv->row_count )
95         return ERROR_NO_MORE_ITEMS;
96     
97     row = wv->reorder[ row ];
98     
99     return wv->table->ops->set_int( wv->table, row, col, val );
100 }
101
102 static UINT INT_evaluate( UINT lval, UINT op, UINT rval )
103 {
104     switch( op )
105     {
106     case OP_EQ:
107         return ( lval == rval );
108     case OP_AND:
109         return ( lval && rval );
110     case OP_OR:
111         return ( lval || rval );
112     case OP_GT:
113         return ( lval > rval );
114     case OP_LT:
115         return ( lval < rval );
116     case OP_LE:
117         return ( lval <= rval );
118     case OP_GE:
119         return ( lval >= rval );
120     case OP_NE:
121         return ( lval != rval );
122     case OP_ISNULL:
123         return ( !lval );
124     case OP_NOTNULL:
125         return ( lval );
126     default:
127         ERR("Unknown operator %d\n", op );
128     }
129     return 0;
130 }
131
132 static const WCHAR *STRING_evaluate( string_table *st,
133               MSIVIEW *table, UINT row, struct expr *expr, MSIRECORD *record )
134 {
135     UINT val = 0, r;
136
137     switch( expr->type )
138     {
139     case EXPR_COL_NUMBER_STRING:
140         r = table->ops->fetch_int( table, row, expr->u.col_number, &val );
141         if( r != ERROR_SUCCESS )
142             return NULL;
143         return msi_string_lookup_id( st, val );
144
145     case EXPR_SVAL:
146         return expr->u.sval;
147
148     case EXPR_WILDCARD:
149         return MSI_RecordGetString( record, 1 );
150
151     default:
152         ERR("Invalid expression type\n");
153         break;
154     }
155     return NULL;
156 }
157
158 static UINT STRCMP_Evaluate( string_table *st, MSIVIEW *table, UINT row, 
159                              struct expr *cond, UINT *val, MSIRECORD *record )
160 {
161     int sr;
162     const WCHAR *l_str, *r_str;
163
164     l_str = STRING_evaluate( st, table, row, cond->u.expr.left, record );
165     r_str = STRING_evaluate( st, table, row, cond->u.expr.right, record );
166     if( l_str == r_str )
167         sr = 0;
168     else if( l_str && ! r_str )
169         sr = 1;
170     else if( r_str && ! l_str )
171         sr = -1;
172     else
173         sr = lstrcmpW( l_str, r_str );
174
175     *val = ( cond->u.expr.op == OP_EQ && ( sr == 0 ) ) ||
176            ( cond->u.expr.op == OP_LT && ( sr < 0 ) ) ||
177            ( cond->u.expr.op == OP_GT && ( sr > 0 ) );
178
179     return ERROR_SUCCESS;
180 }
181
182 static UINT WHERE_evaluate( MSIDATABASE *db, MSIVIEW *table, UINT row, 
183                              struct expr *cond, UINT *val, MSIRECORD *record )
184 {
185     UINT r, lval, rval;
186
187     if( !cond )
188         return ERROR_SUCCESS;
189
190     switch( cond->type )
191     {
192     case EXPR_COL_NUMBER_STRING:
193     case EXPR_COL_NUMBER:
194         return table->ops->fetch_int( table, row, cond->u.col_number, val );
195
196     case EXPR_UVAL:
197         *val = cond->u.uval;
198         return ERROR_SUCCESS;
199
200     case EXPR_COMPLEX:
201         r = WHERE_evaluate( db, table, row, cond->u.expr.left, &lval, record );
202         if( r != ERROR_SUCCESS )
203             return r;
204         r = WHERE_evaluate( db, table, row, cond->u.expr.right, &rval, record );
205         if( r != ERROR_SUCCESS )
206             return r;
207         *val = INT_evaluate( lval, cond->u.expr.op, rval );
208         return ERROR_SUCCESS;
209
210     case EXPR_STRCMP:
211         return STRCMP_Evaluate( db->strings, table, row, cond, val, record );
212
213     case EXPR_WILDCARD:
214         *val = MSI_RecordGetInteger( record, 1 );
215         return ERROR_SUCCESS;
216
217     default:
218         ERR("Invalid expression type\n");
219         break;
220     } 
221
222     return ERROR_SUCCESS;
223
224 }
225
226 static UINT WHERE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
227 {
228     MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
229     UINT count = 0, r, val, i;
230     MSIVIEW *table = wv->table;
231
232     TRACE("%p %p\n", wv, record);
233
234     if( !table )
235          return ERROR_FUNCTION_FAILED;
236
237     r = table->ops->execute( table, record );
238     if( r != ERROR_SUCCESS )
239         return r;
240
241     r = table->ops->get_dimensions( table, &count, NULL );
242     if( r != ERROR_SUCCESS )
243         return r;
244
245     msi_free( wv->reorder );
246     wv->reorder = msi_alloc( count*sizeof(UINT) );
247     if( !wv->reorder )
248         return ERROR_FUNCTION_FAILED;
249
250     wv->row_count = 0;
251     if (wv->cond->type == EXPR_STRCMP)
252     {
253         MSIITERHANDLE handle = NULL;
254         UINT row, value, col;
255         struct expr *col_cond = wv->cond->u.expr.left;
256         struct expr *val_cond = wv->cond->u.expr.right;
257
258         /* swap conditionals */
259         if (col_cond->type != EXPR_COL_NUMBER_STRING)
260         {
261             val_cond = wv->cond->u.expr.left;
262             col_cond = wv->cond->u.expr.right;
263         }
264
265         if ((col_cond->type == EXPR_COL_NUMBER_STRING) && (val_cond->type == EXPR_SVAL))
266         {
267             col = col_cond->u.col_number;
268             /* special case for "" - translate it into nil */
269             if (!val_cond->u.sval[0])
270                 value = 0;
271             else
272             {
273                 r = msi_string2idW(wv->db->strings, val_cond->u.sval, &value);
274                 if (r != ERROR_SUCCESS)
275                 {
276                     TRACE("no id for %s, assuming it doesn't exist in the table\n", debugstr_w(wv->cond->u.expr.right->u.sval));
277                     return ERROR_SUCCESS;
278                 }
279             }
280
281             do
282             {
283                 r = table->ops->find_matching_rows(table, col, value, &row, &handle);
284                 if (r == ERROR_SUCCESS)
285                     wv->reorder[ wv->row_count ++ ] = row;
286             } while (r == ERROR_SUCCESS);
287
288             if (r == ERROR_NO_MORE_ITEMS)
289                 return ERROR_SUCCESS;
290             else
291                 return r;
292         }
293         /* else fallback to slow case */
294     }
295
296     for( i=0; i<count; i++ )
297     {
298         val = 0;
299         r = WHERE_evaluate( wv->db, table, i, wv->cond, &val, record );
300         if( r != ERROR_SUCCESS )
301             return r;
302         if( val )
303             wv->reorder[ wv->row_count ++ ] = i;
304     }
305
306     return ERROR_SUCCESS;
307 }
308
309 static UINT WHERE_close( struct tagMSIVIEW *view )
310 {
311     MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
312
313     TRACE("%p\n", wv );
314
315     if( !wv->table )
316          return ERROR_FUNCTION_FAILED;
317
318     msi_free( wv->reorder );
319     wv->reorder = NULL;
320
321     return wv->table->ops->close( wv->table );
322 }
323
324 static UINT WHERE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols )
325 {
326     MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
327
328     TRACE("%p %p %p\n", wv, rows, cols );
329
330     if( !wv->table )
331          return ERROR_FUNCTION_FAILED;
332
333     if( rows )
334     {
335         if( !wv->reorder )
336             return ERROR_FUNCTION_FAILED;
337         *rows = wv->row_count;
338     }
339
340     return wv->table->ops->get_dimensions( wv->table, NULL, cols );
341 }
342
343 static UINT WHERE_get_column_info( struct tagMSIVIEW *view,
344                 UINT n, LPWSTR *name, UINT *type )
345 {
346     MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
347
348     TRACE("%p %d %p %p\n", wv, n, name, type );
349
350     if( !wv->table )
351          return ERROR_FUNCTION_FAILED;
352
353     return wv->table->ops->get_column_info( wv->table, n, name, type );
354 }
355
356 static UINT WHERE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
357                 MSIRECORD *rec )
358 {
359     MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
360
361     TRACE("%p %d %p\n", wv, eModifyMode, rec );
362
363     if( !wv->table )
364          return ERROR_FUNCTION_FAILED;
365
366     return wv->table->ops->modify( wv->table, eModifyMode, rec );
367 }
368
369 static UINT WHERE_delete( struct tagMSIVIEW *view )
370 {
371     MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
372
373     TRACE("%p\n", wv );
374
375     if( wv->table )
376         wv->table->ops->delete( wv->table );
377     wv->table = 0;
378
379     msi_free( wv->reorder );
380     wv->reorder = NULL;
381     wv->row_count = 0;
382
383     msiobj_release( &wv->db->hdr );
384     msi_free( wv );
385
386     return ERROR_SUCCESS;
387 }
388
389 static UINT WHERE_find_matching_rows( struct tagMSIVIEW *view, UINT col,
390     UINT val, UINT *row, MSIITERHANDLE *handle )
391 {
392     MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
393     UINT r;
394
395     TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
396
397     if( !wv->table )
398          return ERROR_FUNCTION_FAILED;
399
400     r = wv->table->ops->find_matching_rows( wv->table, col, val, row, handle );
401
402     if( *row > wv->row_count )
403         return ERROR_NO_MORE_ITEMS;
404
405     *row = wv->reorder[ *row ];
406
407     return r;
408 }
409
410
411 static const MSIVIEWOPS where_ops =
412 {
413     WHERE_fetch_int,
414     WHERE_fetch_stream,
415     WHERE_set_int,
416     NULL,
417     WHERE_execute,
418     WHERE_close,
419     WHERE_get_dimensions,
420     WHERE_get_column_info,
421     WHERE_modify,
422     WHERE_delete,
423     WHERE_find_matching_rows
424 };
425
426 static UINT WHERE_VerifyCondition( MSIDATABASE *db, MSIVIEW *table, struct expr *cond,
427                                    UINT *valid )
428 {
429     UINT r, val = 0;
430
431     switch( cond->type )
432     {
433     case EXPR_COLUMN:
434         r = VIEW_find_column( table, cond->u.column, &val );
435         if( r == ERROR_SUCCESS )
436         {
437             UINT type = 0;
438             r = table->ops->get_column_info( table, val, NULL, &type );
439             if( r == ERROR_SUCCESS )
440             {
441                 if (type&MSITYPE_STRING)
442                     cond->type = EXPR_COL_NUMBER_STRING;
443                 else
444                     cond->type = EXPR_COL_NUMBER;
445                 cond->u.col_number = val;
446                 *valid = 1;
447             }
448             else
449                 *valid = 0;
450         }
451         else
452         {
453             *valid = 0;
454             ERR("Couldn't find column %s\n", debugstr_w( cond->u.column ) );
455         }
456         break;
457     case EXPR_COMPLEX:
458         r = WHERE_VerifyCondition( db, table, cond->u.expr.left, valid );
459         if( r != ERROR_SUCCESS )
460             return r;
461         if( !*valid )
462             return ERROR_SUCCESS;
463         r = WHERE_VerifyCondition( db, table, cond->u.expr.right, valid );
464         if( r != ERROR_SUCCESS )
465             return r;
466
467         /* check the type of the comparison */
468         if( ( cond->u.expr.left->type == EXPR_SVAL ) ||
469             ( cond->u.expr.left->type == EXPR_COL_NUMBER_STRING ) ||
470             ( cond->u.expr.right->type == EXPR_SVAL ) ||
471             ( cond->u.expr.right->type == EXPR_COL_NUMBER_STRING ) )
472         {
473             switch( cond->u.expr.op )
474             {
475             case OP_EQ:
476             case OP_GT:
477             case OP_LT:
478                 break;
479             default:
480                 *valid = FALSE;
481                 return ERROR_INVALID_PARAMETER;
482             }
483
484             /* FIXME: check we're comparing a string to a column */
485
486             cond->type = EXPR_STRCMP;
487         }
488
489         break;
490     case EXPR_IVAL:
491         *valid = 1;
492         cond->type = EXPR_UVAL;
493         cond->u.uval = cond->u.ival + (1<<15);
494         break;
495     case EXPR_WILDCARD:
496         *valid = 1;
497         break;
498     case EXPR_SVAL:
499         *valid = 1;
500         break;
501     default:
502         ERR("Invalid expression type\n");
503         *valid = 0;
504         break;
505     } 
506
507     return ERROR_SUCCESS;
508 }
509
510 UINT WHERE_CreateView( MSIDATABASE *db, MSIVIEW **view, MSIVIEW *table,
511                        struct expr *cond )
512 {
513     MSIWHEREVIEW *wv = NULL;
514     UINT count = 0, r, valid = 0;
515
516     TRACE("%p\n", table );
517
518     r = table->ops->get_dimensions( table, NULL, &count );
519     if( r != ERROR_SUCCESS )
520     {
521         ERR("can't get table dimensions\n");
522         return r;
523     }
524
525     if( cond )
526     {
527         r = WHERE_VerifyCondition( db, table, cond, &valid );
528         if( r != ERROR_SUCCESS )
529             return r;
530         if( !valid )
531             return ERROR_FUNCTION_FAILED;
532     }
533
534     wv = msi_alloc_zero( sizeof *wv );
535     if( !wv )
536         return ERROR_FUNCTION_FAILED;
537     
538     /* fill the structure */
539     wv->view.ops = &where_ops;
540     msiobj_addref( &db->hdr );
541     wv->db = db;
542     wv->table = table;
543     wv->row_count = 0;
544     wv->reorder = NULL;
545     wv->cond = cond;
546     *view = (MSIVIEW*) wv;
547
548     return ERROR_SUCCESS;
549 }