Update the address of the Free Software Foundation.
[wine/gsoc_dplay.git] / dlls / msi / where.c
blob6fa70a3ad29a53251f1f374d46c03cca7cb995cc
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);
39 /* below is the query interface to a table */
41 typedef struct tagMSIWHEREVIEW
43 MSIVIEW view;
44 MSIDATABASE *db;
45 MSIVIEW *table;
46 UINT row_count;
47 UINT *reorder;
48 struct expr *cond;
49 } MSIWHEREVIEW;
51 static UINT WHERE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
53 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
55 TRACE("%p %d %d %p\n", wv, row, col, val );
57 if( !wv->table )
58 return ERROR_FUNCTION_FAILED;
60 if( row > wv->row_count )
61 return ERROR_NO_MORE_ITEMS;
63 row = wv->reorder[ row ];
65 return wv->table->ops->fetch_int( wv->table, row, col, val );
68 static UINT WHERE_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm )
70 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
72 TRACE("%p %d %d %p\n", wv, row, col, stm );
74 if( !wv->table )
75 return ERROR_FUNCTION_FAILED;
77 if( row > wv->row_count )
78 return ERROR_NO_MORE_ITEMS;
80 row = wv->reorder[ row ];
82 return wv->table->ops->fetch_stream( wv->table, row, col, stm );
85 static UINT WHERE_set_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT val )
87 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
89 TRACE("%p %d %d %04x\n", wv, row, col, val );
91 if( !wv->table )
92 return ERROR_FUNCTION_FAILED;
94 if( row > wv->row_count )
95 return ERROR_NO_MORE_ITEMS;
97 row = wv->reorder[ row ];
99 return wv->table->ops->set_int( wv->table, row, col, val );
102 static UINT INT_evaluate( UINT lval, UINT op, UINT rval )
104 switch( op )
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 );
129 return 0;
132 static const WCHAR *STRING_evaluate( string_table *st,
133 MSIVIEW *table, UINT row, struct expr *expr, MSIRECORD *record )
135 UINT val = 0, r;
137 switch( expr->type )
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 );
145 case EXPR_SVAL:
146 return expr->u.sval;
148 case EXPR_WILDCARD:
149 return MSI_RecordGetString( record, 1 );
151 default:
152 ERR("Invalid expression type\n");
153 break;
155 return NULL;
158 static UINT STRCMP_Evaluate( string_table *st, MSIVIEW *table, UINT row,
159 struct expr *cond, UINT *val, MSIRECORD *record )
161 int sr;
162 const WCHAR *l_str, *r_str;
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 );
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 ) );
179 return ERROR_SUCCESS;
182 static UINT WHERE_evaluate( MSIDATABASE *db, MSIVIEW *table, UINT row,
183 struct expr *cond, UINT *val, MSIRECORD *record )
185 UINT r, lval, rval;
187 if( !cond )
188 return ERROR_SUCCESS;
190 switch( cond->type )
192 case EXPR_COL_NUMBER_STRING:
193 case EXPR_COL_NUMBER:
194 return table->ops->fetch_int( table, row, cond->u.col_number, val );
196 case EXPR_UVAL:
197 *val = cond->u.uval;
198 return ERROR_SUCCESS;
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;
210 case EXPR_STRCMP:
211 return STRCMP_Evaluate( db->strings, table, row, cond, val, record );
213 case EXPR_WILDCARD:
214 *val = MSI_RecordGetInteger( record, 1 );
215 return ERROR_SUCCESS;
217 default:
218 ERR("Invalid expression type\n");
219 break;
222 return ERROR_SUCCESS;
226 static UINT WHERE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
228 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
229 UINT count = 0, r, val, i;
230 MSIVIEW *table = wv->table;
232 TRACE("%p %p\n", wv, record);
234 if( !table )
235 return ERROR_FUNCTION_FAILED;
237 r = table->ops->execute( table, record );
238 if( r != ERROR_SUCCESS )
239 return r;
241 r = table->ops->get_dimensions( table, &count, NULL );
242 if( r != ERROR_SUCCESS )
243 return r;
245 msi_free( wv->reorder );
246 wv->reorder = msi_alloc( count*sizeof(UINT) );
247 if( !wv->reorder )
248 return ERROR_FUNCTION_FAILED;
250 wv->row_count = 0;
251 if (wv->cond->type == EXPR_STRCMP)
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;
258 /* swap conditionals */
259 if (col_cond->type != EXPR_COL_NUMBER_STRING)
261 val_cond = wv->cond->u.expr.left;
262 col_cond = wv->cond->u.expr.right;
265 if ((col_cond->type == EXPR_COL_NUMBER_STRING) && (val_cond->type == EXPR_SVAL))
267 col = col_cond->u.col_number;
268 r = msi_string2idW(wv->db->strings, val_cond->u.sval, &value);
269 if (r != ERROR_SUCCESS)
271 TRACE("no id for %s, assuming it doesn't exist in the table\n", debugstr_w(wv->cond->u.expr.right->u.sval));
272 return ERROR_SUCCESS;
277 r = table->ops->find_matching_rows(table, col, value, &row, &handle);
278 if (r == ERROR_SUCCESS)
279 wv->reorder[ wv->row_count ++ ] = row;
280 } while (r == ERROR_SUCCESS);
282 if (r == ERROR_NO_MORE_ITEMS)
283 return ERROR_SUCCESS;
284 else
285 return r;
287 /* else fallback to slow case */
290 for( i=0; i<count; i++ )
292 val = 0;
293 r = WHERE_evaluate( wv->db, table, i, wv->cond, &val, record );
294 if( r != ERROR_SUCCESS )
295 return r;
296 if( val )
297 wv->reorder[ wv->row_count ++ ] = i;
300 return ERROR_SUCCESS;
303 static UINT WHERE_close( struct tagMSIVIEW *view )
305 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
307 TRACE("%p\n", wv );
309 if( !wv->table )
310 return ERROR_FUNCTION_FAILED;
312 msi_free( wv->reorder );
313 wv->reorder = NULL;
315 return wv->table->ops->close( wv->table );
318 static UINT WHERE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols )
320 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
322 TRACE("%p %p %p\n", wv, rows, cols );
324 if( !wv->table )
325 return ERROR_FUNCTION_FAILED;
327 if( rows )
329 if( !wv->reorder )
330 return ERROR_FUNCTION_FAILED;
331 *rows = wv->row_count;
334 return wv->table->ops->get_dimensions( wv->table, NULL, cols );
337 static UINT WHERE_get_column_info( struct tagMSIVIEW *view,
338 UINT n, LPWSTR *name, UINT *type )
340 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
342 TRACE("%p %d %p %p\n", wv, n, name, type );
344 if( !wv->table )
345 return ERROR_FUNCTION_FAILED;
347 return wv->table->ops->get_column_info( wv->table, n, name, type );
350 static UINT WHERE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
351 MSIRECORD *rec )
353 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
355 TRACE("%p %d %p\n", wv, eModifyMode, rec );
357 if( !wv->table )
358 return ERROR_FUNCTION_FAILED;
360 return wv->table->ops->modify( wv->table, eModifyMode, rec );
363 static UINT WHERE_delete( struct tagMSIVIEW *view )
365 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
367 TRACE("%p\n", wv );
369 if( wv->table )
370 wv->table->ops->delete( wv->table );
371 wv->table = 0;
373 msi_free( wv->reorder );
374 wv->reorder = NULL;
375 wv->row_count = 0;
377 msiobj_release( &wv->db->hdr );
378 msi_free( wv );
380 return ERROR_SUCCESS;
383 static UINT WHERE_find_matching_rows( struct tagMSIVIEW *view, UINT col,
384 UINT val, UINT *row, MSIITERHANDLE *handle )
386 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
387 UINT r;
389 TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
391 if( !wv->table )
392 return ERROR_FUNCTION_FAILED;
394 r = wv->table->ops->find_matching_rows( wv->table, col, val, row, handle );
396 if( *row > wv->row_count )
397 return ERROR_NO_MORE_ITEMS;
399 *row = wv->reorder[ *row ];
401 return r;
405 MSIVIEWOPS where_ops =
407 WHERE_fetch_int,
408 WHERE_fetch_stream,
409 WHERE_set_int,
410 NULL,
411 WHERE_execute,
412 WHERE_close,
413 WHERE_get_dimensions,
414 WHERE_get_column_info,
415 WHERE_modify,
416 WHERE_delete,
417 WHERE_find_matching_rows
420 static UINT WHERE_VerifyCondition( MSIDATABASE *db, MSIVIEW *table, struct expr *cond,
421 UINT *valid )
423 UINT r, val = 0;
425 switch( cond->type )
427 case EXPR_COLUMN:
428 r = VIEW_find_column( table, cond->u.column, &val );
429 if( r == ERROR_SUCCESS )
431 UINT type = 0;
432 r = table->ops->get_column_info( table, val, NULL, &type );
433 if( r == ERROR_SUCCESS )
435 if (type&MSITYPE_STRING)
436 cond->type = EXPR_COL_NUMBER_STRING;
437 else
438 cond->type = EXPR_COL_NUMBER;
439 cond->u.col_number = val;
440 *valid = 1;
442 else
443 *valid = 0;
445 else
447 *valid = 0;
448 ERR("Couldn't find column %s\n", debugstr_w( cond->u.column ) );
450 break;
451 case EXPR_COMPLEX:
452 r = WHERE_VerifyCondition( db, table, cond->u.expr.left, valid );
453 if( r != ERROR_SUCCESS )
454 return r;
455 if( !*valid )
456 return ERROR_SUCCESS;
457 r = WHERE_VerifyCondition( db, table, cond->u.expr.right, valid );
458 if( r != ERROR_SUCCESS )
459 return r;
461 /* check the type of the comparison */
462 if( ( cond->u.expr.left->type == EXPR_SVAL ) ||
463 ( cond->u.expr.left->type == EXPR_COL_NUMBER_STRING ) ||
464 ( cond->u.expr.right->type == EXPR_SVAL ) ||
465 ( cond->u.expr.right->type == EXPR_COL_NUMBER_STRING ) )
467 switch( cond->u.expr.op )
469 case OP_EQ:
470 case OP_GT:
471 case OP_LT:
472 break;
473 default:
474 *valid = FALSE;
475 return ERROR_INVALID_PARAMETER;
478 /* FIXME: check we're comparing a string to a column */
480 cond->type = EXPR_STRCMP;
483 break;
484 case EXPR_IVAL:
485 *valid = 1;
486 cond->type = EXPR_UVAL;
487 cond->u.uval = cond->u.ival + (1<<15);
488 break;
489 case EXPR_WILDCARD:
490 *valid = 1;
491 break;
492 case EXPR_SVAL:
493 *valid = 1;
494 break;
495 default:
496 ERR("Invalid expression type\n");
497 *valid = 0;
498 break;
501 return ERROR_SUCCESS;
504 UINT WHERE_CreateView( MSIDATABASE *db, MSIVIEW **view, MSIVIEW *table,
505 struct expr *cond )
507 MSIWHEREVIEW *wv = NULL;
508 UINT count = 0, r, valid = 0;
510 TRACE("%p\n", table );
512 r = table->ops->get_dimensions( table, NULL, &count );
513 if( r != ERROR_SUCCESS )
515 ERR("can't get table dimensions\n");
516 return r;
519 if( cond )
521 r = WHERE_VerifyCondition( db, table, cond, &valid );
522 if( r != ERROR_SUCCESS )
523 return r;
524 if( !valid )
525 return ERROR_FUNCTION_FAILED;
528 wv = msi_alloc_zero( sizeof *wv );
529 if( !wv )
530 return ERROR_FUNCTION_FAILED;
532 /* fill the structure */
533 wv->view.ops = &where_ops;
534 msiobj_addref( &db->hdr );
535 wv->db = db;
536 wv->table = table;
537 wv->row_count = 0;
538 wv->reorder = NULL;
539 wv->cond = cond;
540 *view = (MSIVIEW*) wv;
542 return ERROR_SUCCESS;