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