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 INT_evaluate( UINT lval, UINT op, UINT rval )
74 return ( lval == rval );
76 return ( lval && rval );
78 return ( lval || rval );
80 return ( lval > rval );
82 return ( lval < rval );
84 return ( lval <= rval );
86 return ( lval >= rval );
88 return ( lval != rval );
94 ERR("Unknown operator %d\n", op );
99 static const WCHAR *STRING_evaluate( string_table *st,
100 MSIVIEW *table, UINT row, struct expr *expr, MSIHANDLE record )
106 case EXPR_COL_NUMBER:
107 r = table->ops->fetch_int( table, row, expr->u.col_number, &val );
108 if( r != ERROR_SUCCESS )
110 return msi_string_lookup_id( st, val );
116 return MSI_RecordGetString( record, 1 );
119 ERR("Invalid expression type\n");
125 static UINT STRCMP_Evaluate( string_table *st, MSIVIEW *table, UINT row,
126 struct expr *cond, UINT *val, MSIHANDLE record )
129 const WCHAR *l_str, *r_str;
131 l_str = STRING_evaluate( st, table, row, cond->u.expr.left, record );
132 r_str = STRING_evaluate( st, table, row, cond->u.expr.right, record );
135 else if( l_str && ! r_str )
137 else if( r_str && ! l_str )
140 sr = strcmpW( l_str, r_str );
142 *val = ( cond->u.expr.op == OP_EQ && ( sr == 0 ) ) ||
143 ( cond->u.expr.op == OP_LT && ( sr < 0 ) ) ||
144 ( cond->u.expr.op == OP_GT && ( sr > 0 ) );
146 return ERROR_SUCCESS;
149 static UINT WHERE_evaluate( MSIDATABASE *db, MSIVIEW *table, UINT row,
150 struct expr *cond, UINT *val, MSIHANDLE record )
155 return ERROR_SUCCESS;
159 case EXPR_COL_NUMBER:
160 return table->ops->fetch_int( table, row, cond->u.col_number, val );
164 return ERROR_SUCCESS;
167 r = WHERE_evaluate( db, table, row, cond->u.expr.left, &lval, record );
168 if( r != ERROR_SUCCESS )
170 r = WHERE_evaluate( db, table, row, cond->u.expr.right, &rval, record );
171 if( r != ERROR_SUCCESS )
173 *val = INT_evaluate( lval, cond->u.expr.op, rval );
174 return ERROR_SUCCESS;
177 return STRCMP_Evaluate( db->strings, table, row, cond, val, record );
180 *val = MsiRecordGetInteger( record, 1 );
181 return ERROR_SUCCESS;
184 ERR("Invalid expression type\n");
188 return ERROR_SUCCESS;
192 static UINT WHERE_execute( struct tagMSIVIEW *view, MSIHANDLE record )
194 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
195 UINT count = 0, r, val, i;
196 MSIVIEW *table = wv->table;
198 TRACE("%p %ld\n", wv, record);
201 return ERROR_FUNCTION_FAILED;
203 r = table->ops->execute( table, record );
204 if( r != ERROR_SUCCESS )
207 r = table->ops->get_dimensions( table, &count, NULL );
208 if( r != ERROR_SUCCESS )
211 wv->reorder = HeapAlloc( GetProcessHeap(), 0, count*sizeof(UINT) );
213 return ERROR_FUNCTION_FAILED;
215 for( i=0; i<count; i++ )
218 r = WHERE_evaluate( wv->db, table, i, wv->cond, &val, record );
219 if( r != ERROR_SUCCESS )
222 wv->reorder[ wv->row_count ++ ] = i;
225 return ERROR_SUCCESS;
228 static UINT WHERE_close( struct tagMSIVIEW *view )
230 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
235 return ERROR_FUNCTION_FAILED;
238 HeapFree( GetProcessHeap(), 0, wv->reorder );
241 return wv->table->ops->close( wv->table );
244 static UINT WHERE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols )
246 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
248 TRACE("%p %p %p\n", wv, rows, cols );
251 return ERROR_FUNCTION_FAILED;
256 return ERROR_FUNCTION_FAILED;
257 *rows = wv->row_count;
260 return wv->table->ops->get_dimensions( wv->table, NULL, cols );
263 static UINT WHERE_get_column_info( struct tagMSIVIEW *view,
264 UINT n, LPWSTR *name, UINT *type )
266 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
268 TRACE("%p %d %p %p\n", wv, n, name, type );
271 return ERROR_FUNCTION_FAILED;
273 return wv->table->ops->get_column_info( wv->table, n, name, type );
276 static UINT WHERE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode, MSIHANDLE hrec)
278 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
280 TRACE("%p %d %ld\n", wv, eModifyMode, hrec );
283 return ERROR_FUNCTION_FAILED;
285 return wv->table->ops->modify( wv->table, eModifyMode, hrec );
288 static UINT WHERE_delete( struct tagMSIVIEW *view )
290 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
295 wv->table->ops->delete( wv->table );
298 HeapFree( GetProcessHeap(), 0, wv->reorder );
303 delete_expr( wv->cond );
305 HeapFree( GetProcessHeap(), 0, wv );
307 return ERROR_SUCCESS;
311 MSIVIEWOPS where_ops =
318 WHERE_get_dimensions,
319 WHERE_get_column_info,
324 UINT WHERE_CreateView( MSIDATABASE *db, MSIVIEW **view, MSIVIEW *table )
326 MSIWHEREVIEW *wv = NULL;
331 r = table->ops->get_dimensions( table, NULL, &count );
332 if( r != ERROR_SUCCESS )
334 ERR("can't get table dimensions\n");
338 wv = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof *wv );
340 return ERROR_FUNCTION_FAILED;
342 /* fill the structure */
343 wv->view.ops = &where_ops;
349 *view = (MSIVIEW*) wv;
351 return ERROR_SUCCESS;
354 static UINT WHERE_VerifyCondition( MSIDATABASE *db, MSIVIEW *table, struct expr *cond,
362 r = VIEW_find_column( table, cond->u.column, &val );
363 if( r == ERROR_SUCCESS )
366 cond->type = EXPR_COL_NUMBER;
367 cond->u.col_number = val;
372 ERR("Couldn't find column %s\n", debugstr_w( cond->u.column ) );
376 r = WHERE_VerifyCondition( db, table, cond->u.expr.left, valid );
377 if( r != ERROR_SUCCESS )
380 return ERROR_SUCCESS;
381 r = WHERE_VerifyCondition( db, table, cond->u.expr.right, valid );
382 if( r != ERROR_SUCCESS )
385 /* check the type of the comparison */
386 if( ( cond->u.expr.left->type == EXPR_SVAL ) ||
387 ( cond->u.expr.right->type == EXPR_SVAL ) )
389 switch( cond->u.expr.op )
397 return ERROR_INVALID_PARAMETER;
400 /* FIXME: check we're comparing a string to a column */
402 cond->type = EXPR_STRCMP;
408 cond->type = EXPR_UVAL;
409 cond->u.uval = cond->u.ival + (1<<15);
418 ERR("Invalid expression type\n");
423 return ERROR_SUCCESS;
426 UINT WHERE_AddCondition( MSIVIEW *view, struct expr *cond )
428 MSIWHEREVIEW *wv = (MSIWHEREVIEW *) view;
431 if( wv->view.ops != &where_ops )
432 return ERROR_FUNCTION_FAILED;
434 return ERROR_INVALID_PARAMETER;
437 return ERROR_SUCCESS;
439 TRACE("Adding condition\n");
441 r = WHERE_VerifyCondition( wv->db, wv->table, cond, &valid );
442 if( r != ERROR_SUCCESS )
443 ERR("condition evaluation failed\n");
445 TRACE("condition is %s\n", valid ? "valid" : "invalid" );
449 return ERROR_FUNCTION_FAILED;
454 return ERROR_SUCCESS;