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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "wine/debug.h"
27 #include "wine/unicode.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(msi);
40 /* below is the query interface to a table */
42 typedef struct tagMSIWHEREVIEW
52 static UINT WHERE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
54 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
56 TRACE("%p %d %d %p\n", wv, row, col, val );
59 return ERROR_FUNCTION_FAILED;
61 if( row > wv->row_count )
62 return ERROR_NO_MORE_ITEMS;
64 row = wv->reorder[ row ];
66 return wv->table->ops->fetch_int( wv->table, row, col, val );
69 static UINT WHERE_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm )
71 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
73 TRACE("%p %d %d %p\n", wv, row, col, stm );
76 return ERROR_FUNCTION_FAILED;
78 if( row > wv->row_count )
79 return ERROR_NO_MORE_ITEMS;
81 row = wv->reorder[ row ];
83 return wv->table->ops->fetch_stream( wv->table, row, col, stm );
86 static UINT WHERE_set_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT val )
88 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
90 TRACE("%p %d %d %04x\n", wv, row, col, val );
93 return ERROR_FUNCTION_FAILED;
95 if( row > wv->row_count )
96 return ERROR_NO_MORE_ITEMS;
98 row = wv->reorder[ row ];
100 return wv->table->ops->set_int( wv->table, row, col, val );
103 static UINT INT_evaluate( UINT lval, UINT op, UINT rval )
108 return ( lval == rval );
110 return ( lval && rval );
112 return ( lval || rval );
114 return ( lval > rval );
116 return ( lval < rval );
118 return ( lval <= rval );
120 return ( lval >= rval );
122 return ( lval != rval );
128 ERR("Unknown operator %d\n", op );
133 static const WCHAR *STRING_evaluate( string_table *st,
134 MSIVIEW *table, UINT row, struct expr *expr, MSIRECORD *record )
140 case EXPR_COL_NUMBER_STRING:
141 r = table->ops->fetch_int( table, row, expr->u.col_number, &val );
142 if( r != ERROR_SUCCESS )
144 return msi_string_lookup_id( st, val );
150 return MSI_RecordGetString( record, 1 );
153 ERR("Invalid expression type\n");
159 static UINT STRCMP_Evaluate( string_table *st, MSIVIEW *table, UINT row,
160 struct expr *cond, UINT *val, MSIRECORD *record )
163 const WCHAR *l_str, *r_str;
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 );
169 else if( l_str && ! r_str )
171 else if( r_str && ! l_str )
174 sr = strcmpW( l_str, r_str );
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 ) );
180 return ERROR_SUCCESS;
183 static UINT WHERE_evaluate( MSIDATABASE *db, MSIVIEW *table, UINT row,
184 struct expr *cond, UINT *val, MSIRECORD *record )
189 return ERROR_SUCCESS;
193 case EXPR_COL_NUMBER_STRING:
194 case EXPR_COL_NUMBER:
195 return table->ops->fetch_int( table, row, cond->u.col_number, val );
199 return ERROR_SUCCESS;
202 r = WHERE_evaluate( db, table, row, cond->u.expr.left, &lval, record );
203 if( r != ERROR_SUCCESS )
205 r = WHERE_evaluate( db, table, row, cond->u.expr.right, &rval, record );
206 if( r != ERROR_SUCCESS )
208 *val = INT_evaluate( lval, cond->u.expr.op, rval );
209 return ERROR_SUCCESS;
212 return STRCMP_Evaluate( db->strings, table, row, cond, val, record );
215 *val = MSI_RecordGetInteger( record, 1 );
216 return ERROR_SUCCESS;
219 ERR("Invalid expression type\n");
223 return ERROR_SUCCESS;
227 static UINT WHERE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
229 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
230 UINT count = 0, r, val, i;
231 MSIVIEW *table = wv->table;
233 TRACE("%p %p\n", wv, record);
236 return ERROR_FUNCTION_FAILED;
238 r = table->ops->execute( table, record );
239 if( r != ERROR_SUCCESS )
242 r = table->ops->get_dimensions( table, &count, NULL );
243 if( r != ERROR_SUCCESS )
246 wv->reorder = HeapAlloc( GetProcessHeap(), 0, count*sizeof(UINT) );
248 return ERROR_FUNCTION_FAILED;
251 for( i=0; i<count; i++ )
254 r = WHERE_evaluate( wv->db, table, i, wv->cond, &val, record );
255 if( r != ERROR_SUCCESS )
258 wv->reorder[ wv->row_count ++ ] = i;
261 return ERROR_SUCCESS;
264 static UINT WHERE_close( struct tagMSIVIEW *view )
266 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
271 return ERROR_FUNCTION_FAILED;
273 HeapFree( GetProcessHeap(), 0, wv->reorder );
276 return wv->table->ops->close( wv->table );
279 static UINT WHERE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols )
281 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
283 TRACE("%p %p %p\n", wv, rows, cols );
286 return ERROR_FUNCTION_FAILED;
291 return ERROR_FUNCTION_FAILED;
292 *rows = wv->row_count;
295 return wv->table->ops->get_dimensions( wv->table, NULL, cols );
298 static UINT WHERE_get_column_info( struct tagMSIVIEW *view,
299 UINT n, LPWSTR *name, UINT *type )
301 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
303 TRACE("%p %d %p %p\n", wv, n, name, type );
306 return ERROR_FUNCTION_FAILED;
308 return wv->table->ops->get_column_info( wv->table, n, name, type );
311 static UINT WHERE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
314 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
316 TRACE("%p %d %p\n", wv, eModifyMode, rec );
319 return ERROR_FUNCTION_FAILED;
321 return wv->table->ops->modify( wv->table, eModifyMode, rec );
324 static UINT WHERE_delete( struct tagMSIVIEW *view )
326 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
331 wv->table->ops->delete( wv->table );
333 HeapFree( GetProcessHeap(), 0, wv->reorder );
338 delete_expr( wv->cond );
340 msiobj_release( &wv->db->hdr );
341 HeapFree( GetProcessHeap(), 0, wv );
343 return ERROR_SUCCESS;
347 MSIVIEWOPS where_ops =
355 WHERE_get_dimensions,
356 WHERE_get_column_info,
361 static UINT WHERE_VerifyCondition( MSIDATABASE *db, MSIVIEW *table, struct expr *cond,
369 r = VIEW_find_column( table, cond->u.column, &val );
370 if( r == ERROR_SUCCESS )
373 r = table->ops->get_column_info( table, val, NULL, &type );
374 if( r == ERROR_SUCCESS )
376 if (type&MSITYPE_STRING)
377 cond->type = EXPR_COL_NUMBER_STRING;
379 cond->type = EXPR_COL_NUMBER;
380 cond->u.col_number = val;
389 ERR("Couldn't find column %s\n", debugstr_w( cond->u.column ) );
393 r = WHERE_VerifyCondition( db, table, cond->u.expr.left, valid );
394 if( r != ERROR_SUCCESS )
397 return ERROR_SUCCESS;
398 r = WHERE_VerifyCondition( db, table, cond->u.expr.right, valid );
399 if( r != ERROR_SUCCESS )
402 /* check the type of the comparison */
403 if( ( cond->u.expr.left->type == EXPR_SVAL ) ||
404 ( cond->u.expr.left->type == EXPR_COL_NUMBER_STRING ) ||
405 ( cond->u.expr.right->type == EXPR_SVAL ) ||
406 ( cond->u.expr.right->type == EXPR_COL_NUMBER_STRING ) )
408 switch( cond->u.expr.op )
416 return ERROR_INVALID_PARAMETER;
419 /* FIXME: check we're comparing a string to a column */
421 cond->type = EXPR_STRCMP;
427 cond->type = EXPR_UVAL;
428 cond->u.uval = cond->u.ival + (1<<15);
437 ERR("Invalid expression type\n");
442 return ERROR_SUCCESS;
445 UINT WHERE_CreateView( MSIDATABASE *db, MSIVIEW **view, MSIVIEW *table,
448 MSIWHEREVIEW *wv = NULL;
449 UINT count = 0, r, valid = 0;
453 r = table->ops->get_dimensions( table, NULL, &count );
454 if( r != ERROR_SUCCESS )
456 ERR("can't get table dimensions\n");
462 r = WHERE_VerifyCondition( db, table, cond, &valid );
463 if( r != ERROR_SUCCESS )
466 return ERROR_FUNCTION_FAILED;
469 wv = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof *wv );
471 return ERROR_FUNCTION_FAILED;
473 /* fill the structure */
474 wv->view.ops = &where_ops;
475 msiobj_addref( &db->hdr );
481 *view = (MSIVIEW*) wv;
483 return ERROR_SUCCESS;