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"
36 WINE_DEFAULT_DEBUG_CHANNEL(msi);
39 /* below is the query interface to a table */
41 typedef struct tagMSIWHEREVIEW
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 );
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 );
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 );
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 )
107 return ( lval == rval );
109 return ( lval && rval );
111 return ( lval || rval );
113 return ( lval > rval );
115 return ( lval < rval );
117 return ( lval <= rval );
119 return ( lval >= rval );
121 return ( lval != rval );
127 ERR("Unknown operator %d\n", op );
132 static const WCHAR *STRING_evaluate( string_table *st,
133 MSIVIEW *table, UINT row, struct expr *expr, MSIRECORD *record )
139 case EXPR_COL_NUMBER_STRING:
140 r = table->ops->fetch_int( table, row, expr->u.col_number, &val );
141 if( r != ERROR_SUCCESS )
143 return msi_string_lookup_id( st, val );
149 return MSI_RecordGetString( record, 1 );
152 ERR("Invalid expression type\n");
158 static UINT STRCMP_Evaluate( string_table *st, MSIVIEW *table, UINT row,
159 struct expr *cond, UINT *val, MSIRECORD *record )
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 );
168 else if( l_str && ! r_str )
170 else if( r_str && ! l_str )
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 )
188 return ERROR_SUCCESS;
192 case EXPR_COL_NUMBER_STRING:
193 case EXPR_COL_NUMBER:
194 return table->ops->fetch_int( table, row, cond->u.col_number, val );
198 return ERROR_SUCCESS;
201 r = WHERE_evaluate( db, table, row, cond->u.expr.left, &lval, record );
202 if( r != ERROR_SUCCESS )
204 r = WHERE_evaluate( db, table, row, cond->u.expr.right, &rval, record );
205 if( r != ERROR_SUCCESS )
207 *val = INT_evaluate( lval, cond->u.expr.op, rval );
208 return ERROR_SUCCESS;
211 return STRCMP_Evaluate( db->strings, table, row, cond, val, record );
214 *val = MSI_RecordGetInteger( record, 1 );
215 return ERROR_SUCCESS;
218 ERR("Invalid expression type\n");
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);
235 return ERROR_FUNCTION_FAILED;
237 r = table->ops->execute( table, record );
238 if( r != ERROR_SUCCESS )
241 r = table->ops->get_dimensions( table, &count, NULL );
242 if( r != ERROR_SUCCESS )
245 wv->reorder = HeapAlloc( GetProcessHeap(), 0, count*sizeof(UINT) );
247 return ERROR_FUNCTION_FAILED;
250 for( i=0; i<count; i++ )
253 r = WHERE_evaluate( wv->db, table, i, wv->cond, &val, record );
254 if( r != ERROR_SUCCESS )
257 wv->reorder[ wv->row_count ++ ] = i;
260 return ERROR_SUCCESS;
263 static UINT WHERE_close( struct tagMSIVIEW *view )
265 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
270 return ERROR_FUNCTION_FAILED;
272 HeapFree( GetProcessHeap(), 0, wv->reorder );
275 return wv->table->ops->close( wv->table );
278 static UINT WHERE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols )
280 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
282 TRACE("%p %p %p\n", wv, rows, cols );
285 return ERROR_FUNCTION_FAILED;
290 return ERROR_FUNCTION_FAILED;
291 *rows = wv->row_count;
294 return wv->table->ops->get_dimensions( wv->table, NULL, cols );
297 static UINT WHERE_get_column_info( struct tagMSIVIEW *view,
298 UINT n, LPWSTR *name, UINT *type )
300 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
302 TRACE("%p %d %p %p\n", wv, n, name, type );
305 return ERROR_FUNCTION_FAILED;
307 return wv->table->ops->get_column_info( wv->table, n, name, type );
310 static UINT WHERE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
313 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
315 TRACE("%p %d %p\n", wv, eModifyMode, rec );
318 return ERROR_FUNCTION_FAILED;
320 return wv->table->ops->modify( wv->table, eModifyMode, rec );
323 static UINT WHERE_delete( struct tagMSIVIEW *view )
325 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
330 wv->table->ops->delete( wv->table );
332 HeapFree( GetProcessHeap(), 0, wv->reorder );
336 msiobj_release( &wv->db->hdr );
337 HeapFree( GetProcessHeap(), 0, wv );
339 return ERROR_SUCCESS;
343 MSIVIEWOPS where_ops =
351 WHERE_get_dimensions,
352 WHERE_get_column_info,
357 static UINT WHERE_VerifyCondition( MSIDATABASE *db, MSIVIEW *table, struct expr *cond,
365 r = VIEW_find_column( table, cond->u.column, &val );
366 if( r == ERROR_SUCCESS )
369 r = table->ops->get_column_info( table, val, NULL, &type );
370 if( r == ERROR_SUCCESS )
372 if (type&MSITYPE_STRING)
373 cond->type = EXPR_COL_NUMBER_STRING;
375 cond->type = EXPR_COL_NUMBER;
376 cond->u.col_number = val;
385 ERR("Couldn't find column %s\n", debugstr_w( cond->u.column ) );
389 r = WHERE_VerifyCondition( db, table, cond->u.expr.left, valid );
390 if( r != ERROR_SUCCESS )
393 return ERROR_SUCCESS;
394 r = WHERE_VerifyCondition( db, table, cond->u.expr.right, valid );
395 if( r != ERROR_SUCCESS )
398 /* check the type of the comparison */
399 if( ( cond->u.expr.left->type == EXPR_SVAL ) ||
400 ( cond->u.expr.left->type == EXPR_COL_NUMBER_STRING ) ||
401 ( cond->u.expr.right->type == EXPR_SVAL ) ||
402 ( cond->u.expr.right->type == EXPR_COL_NUMBER_STRING ) )
404 switch( cond->u.expr.op )
412 return ERROR_INVALID_PARAMETER;
415 /* FIXME: check we're comparing a string to a column */
417 cond->type = EXPR_STRCMP;
423 cond->type = EXPR_UVAL;
424 cond->u.uval = cond->u.ival + (1<<15);
433 ERR("Invalid expression type\n");
438 return ERROR_SUCCESS;
441 UINT WHERE_CreateView( MSIDATABASE *db, MSIVIEW **view, MSIVIEW *table,
444 MSIWHEREVIEW *wv = NULL;
445 UINT count = 0, r, valid = 0;
449 r = table->ops->get_dimensions( table, NULL, &count );
450 if( r != ERROR_SUCCESS )
452 ERR("can't get table dimensions\n");
458 r = WHERE_VerifyCondition( db, table, cond, &valid );
459 if( r != ERROR_SUCCESS )
462 return ERROR_FUNCTION_FAILED;
465 wv = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof *wv );
467 return ERROR_FUNCTION_FAILED;
469 /* fill the structure */
470 wv->view.ops = &where_ops;
471 msiobj_addref( &db->hdr );
477 *view = (MSIVIEW*) wv;
479 return ERROR_SUCCESS;