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
26 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(msidb
);
39 /* below is the query interface to a table */
41 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_get_row( struct tagMSIVIEW
*view
, UINT row
, MSIRECORD
**rec
)
88 MSIWHEREVIEW
*wv
= (MSIWHEREVIEW
*)view
;
90 TRACE("%p %d %p\n", wv
, row
, rec
);
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
->get_row(view
, row
, rec
);
103 static UINT
WHERE_set_row( struct tagMSIVIEW
*view
, UINT row
, MSIRECORD
*rec
, UINT mask
)
105 MSIWHEREVIEW
*wv
= (MSIWHEREVIEW
*)view
;
107 TRACE("%p %d %p %08x\n", wv
, row
, rec
, mask
);
110 return ERROR_FUNCTION_FAILED
;
112 if( row
> wv
->row_count
)
113 return ERROR_NO_MORE_ITEMS
;
115 row
= wv
->reorder
[ row
];
117 return wv
->table
->ops
->set_row( wv
->table
, row
, rec
, mask
);
120 static INT
INT_evaluate_binary( INT lval
, UINT op
, INT rval
)
125 return ( lval
== rval
);
127 return ( lval
&& rval
);
129 return ( lval
|| rval
);
131 return ( lval
> rval
);
133 return ( lval
< rval
);
135 return ( lval
<= rval
);
137 return ( lval
>= rval
);
139 return ( lval
!= rval
);
141 ERR("Unknown operator %d\n", op
);
146 static INT
INT_evaluate_unary( INT lval
, UINT op
)
155 ERR("Unknown operator %d\n", op
);
160 static const WCHAR
*STRING_evaluate( MSIWHEREVIEW
*wv
, UINT row
,
161 const struct expr
*expr
,
162 const MSIRECORD
*record
)
168 case EXPR_COL_NUMBER_STRING
:
169 r
= wv
->table
->ops
->fetch_int( wv
->table
, row
, expr
->u
.col_number
, &val
);
170 if( r
!= ERROR_SUCCESS
)
172 return msi_string_lookup_id( wv
->db
->strings
, val
);
178 return MSI_RecordGetString( record
, ++wv
->rec_index
);
181 ERR("Invalid expression type\n");
187 static UINT
STRCMP_Evaluate( MSIWHEREVIEW
*wv
, UINT row
, const struct expr
*cond
,
188 INT
*val
, const MSIRECORD
*record
)
191 const WCHAR
*l_str
, *r_str
;
193 l_str
= STRING_evaluate( wv
, row
, cond
->u
.expr
.left
, record
);
194 r_str
= STRING_evaluate( wv
, row
, cond
->u
.expr
.right
, record
);
195 if( l_str
== r_str
||
196 ((!l_str
|| !*l_str
) && (!r_str
|| !*r_str
)) )
198 else if( l_str
&& ! r_str
)
200 else if( r_str
&& ! l_str
)
203 sr
= lstrcmpW( l_str
, r_str
);
205 *val
= ( cond
->u
.expr
.op
== OP_EQ
&& ( sr
== 0 ) ) ||
206 ( cond
->u
.expr
.op
== OP_LT
&& ( sr
< 0 ) ) ||
207 ( cond
->u
.expr
.op
== OP_GT
&& ( sr
> 0 ) );
209 return ERROR_SUCCESS
;
212 static UINT
WHERE_evaluate( MSIWHEREVIEW
*wv
, UINT row
,
213 struct expr
*cond
, INT
*val
, MSIRECORD
*record
)
219 return ERROR_SUCCESS
;
223 case EXPR_COL_NUMBER
:
224 r
= wv
->table
->ops
->fetch_int( wv
->table
, row
, cond
->u
.col_number
, &tval
);
225 *val
= tval
- 0x8000;
226 return ERROR_SUCCESS
;
228 case EXPR_COL_NUMBER32
:
229 r
= wv
->table
->ops
->fetch_int( wv
->table
, row
, cond
->u
.col_number
, &tval
);
230 *val
= tval
- 0x80000000;
235 return ERROR_SUCCESS
;
238 r
= WHERE_evaluate( wv
, row
, cond
->u
.expr
.left
, &lval
, record
);
239 if( r
!= ERROR_SUCCESS
)
241 r
= WHERE_evaluate( wv
, row
, cond
->u
.expr
.right
, &rval
, record
);
242 if( r
!= ERROR_SUCCESS
)
244 *val
= INT_evaluate_binary( lval
, cond
->u
.expr
.op
, rval
);
245 return ERROR_SUCCESS
;
248 r
= wv
->table
->ops
->fetch_int( wv
->table
, row
, cond
->u
.expr
.left
->u
.col_number
, &tval
);
249 if( r
!= ERROR_SUCCESS
)
251 *val
= INT_evaluate_unary( tval
, cond
->u
.expr
.op
);
252 return ERROR_SUCCESS
;
255 return STRCMP_Evaluate( wv
, row
, cond
, val
, record
);
258 *val
= MSI_RecordGetInteger( record
, ++wv
->rec_index
);
259 return ERROR_SUCCESS
;
262 ERR("Invalid expression type\n");
266 return ERROR_SUCCESS
;
270 static UINT
WHERE_execute( struct tagMSIVIEW
*view
, MSIRECORD
*record
)
272 MSIWHEREVIEW
*wv
= (MSIWHEREVIEW
*)view
;
273 UINT count
= 0, r
, i
;
275 MSIVIEW
*table
= wv
->table
;
277 TRACE("%p %p\n", wv
, record
);
280 return ERROR_FUNCTION_FAILED
;
282 r
= table
->ops
->execute( table
, record
);
283 if( r
!= ERROR_SUCCESS
)
286 r
= table
->ops
->get_dimensions( table
, &count
, NULL
);
287 if( r
!= ERROR_SUCCESS
)
290 msi_free( wv
->reorder
);
291 wv
->reorder
= msi_alloc( count
*sizeof(UINT
) );
293 return ERROR_FUNCTION_FAILED
;
296 if (wv
->cond
->type
== EXPR_STRCMP
)
298 MSIITERHANDLE handle
= NULL
;
299 UINT row
, value
, col
;
300 struct expr
*col_cond
= wv
->cond
->u
.expr
.left
;
301 struct expr
*val_cond
= wv
->cond
->u
.expr
.right
;
303 /* swap conditionals */
304 if (col_cond
->type
!= EXPR_COL_NUMBER_STRING
)
306 val_cond
= wv
->cond
->u
.expr
.left
;
307 col_cond
= wv
->cond
->u
.expr
.right
;
310 if ((col_cond
->type
== EXPR_COL_NUMBER_STRING
) && (val_cond
->type
== EXPR_SVAL
))
312 col
= col_cond
->u
.col_number
;
313 /* special case for "" - translate it into nil */
314 if (!val_cond
->u
.sval
[0])
318 r
= msi_string2idW(wv
->db
->strings
, val_cond
->u
.sval
, &value
);
319 if (r
!= ERROR_SUCCESS
)
321 TRACE("no id for %s, assuming it doesn't exist in the table\n", debugstr_w(wv
->cond
->u
.expr
.right
->u
.sval
));
322 return ERROR_SUCCESS
;
328 r
= table
->ops
->find_matching_rows(table
, col
, value
, &row
, &handle
);
329 if (r
== ERROR_SUCCESS
)
330 wv
->reorder
[ wv
->row_count
++ ] = row
;
331 } while (r
== ERROR_SUCCESS
);
333 if (r
== ERROR_NO_MORE_ITEMS
)
334 return ERROR_SUCCESS
;
338 /* else fallback to slow case */
341 for( i
=0; i
<count
; i
++ )
345 r
= WHERE_evaluate( wv
, i
, wv
->cond
, &val
, record
);
346 if( r
!= ERROR_SUCCESS
)
349 wv
->reorder
[ wv
->row_count
++ ] = i
;
352 return ERROR_SUCCESS
;
355 static UINT
WHERE_close( struct tagMSIVIEW
*view
)
357 MSIWHEREVIEW
*wv
= (MSIWHEREVIEW
*)view
;
362 return ERROR_FUNCTION_FAILED
;
364 msi_free( wv
->reorder
);
367 return wv
->table
->ops
->close( wv
->table
);
370 static UINT
WHERE_get_dimensions( struct tagMSIVIEW
*view
, UINT
*rows
, UINT
*cols
)
372 MSIWHEREVIEW
*wv
= (MSIWHEREVIEW
*)view
;
374 TRACE("%p %p %p\n", wv
, rows
, cols
);
377 return ERROR_FUNCTION_FAILED
;
382 return ERROR_FUNCTION_FAILED
;
383 *rows
= wv
->row_count
;
386 return wv
->table
->ops
->get_dimensions( wv
->table
, NULL
, cols
);
389 static UINT
WHERE_get_column_info( struct tagMSIVIEW
*view
,
390 UINT n
, LPWSTR
*name
, UINT
*type
)
392 MSIWHEREVIEW
*wv
= (MSIWHEREVIEW
*)view
;
394 TRACE("%p %d %p %p\n", wv
, n
, name
, type
);
397 return ERROR_FUNCTION_FAILED
;
399 return wv
->table
->ops
->get_column_info( wv
->table
, n
, name
, type
);
402 static UINT
WHERE_modify( struct tagMSIVIEW
*view
, MSIMODIFY eModifyMode
,
403 MSIRECORD
*rec
, UINT row
)
405 MSIWHEREVIEW
*wv
= (MSIWHEREVIEW
*)view
;
407 TRACE("%p %d %p\n", wv
, eModifyMode
, rec
);
410 return ERROR_FUNCTION_FAILED
;
412 return wv
->table
->ops
->modify( wv
->table
, eModifyMode
, rec
, row
);
415 static UINT
WHERE_delete( struct tagMSIVIEW
*view
)
417 MSIWHEREVIEW
*wv
= (MSIWHEREVIEW
*)view
;
422 wv
->table
->ops
->delete( wv
->table
);
425 msi_free( wv
->reorder
);
429 msiobj_release( &wv
->db
->hdr
);
432 return ERROR_SUCCESS
;
435 static UINT
WHERE_find_matching_rows( struct tagMSIVIEW
*view
, UINT col
,
436 UINT val
, UINT
*row
, MSIITERHANDLE
*handle
)
438 MSIWHEREVIEW
*wv
= (MSIWHEREVIEW
*)view
;
441 TRACE("%p, %d, %u, %p\n", view
, col
, val
, *handle
);
444 return ERROR_FUNCTION_FAILED
;
446 r
= wv
->table
->ops
->find_matching_rows( wv
->table
, col
, val
, row
, handle
);
448 if( *row
> wv
->row_count
)
449 return ERROR_NO_MORE_ITEMS
;
451 *row
= wv
->reorder
[ *row
];
457 static const MSIVIEWOPS where_ops
=
467 WHERE_get_dimensions
,
468 WHERE_get_column_info
,
471 WHERE_find_matching_rows
,
478 static UINT
WHERE_VerifyCondition( MSIDATABASE
*db
, MSIVIEW
*table
, struct expr
*cond
,
486 r
= VIEW_find_column( table
, cond
->u
.column
, &val
);
487 if( r
== ERROR_SUCCESS
)
490 r
= table
->ops
->get_column_info( table
, val
, NULL
, &type
);
491 if( r
== ERROR_SUCCESS
)
493 if (type
&MSITYPE_STRING
)
494 cond
->type
= EXPR_COL_NUMBER_STRING
;
495 else if ((type
&0xff) == 4)
496 cond
->type
= EXPR_COL_NUMBER32
;
498 cond
->type
= EXPR_COL_NUMBER
;
499 cond
->u
.col_number
= val
;
508 ERR("Couldn't find column %s\n", debugstr_w( cond
->u
.column
) );
512 r
= WHERE_VerifyCondition( db
, table
, cond
->u
.expr
.left
, valid
);
513 if( r
!= ERROR_SUCCESS
)
516 return ERROR_SUCCESS
;
517 r
= WHERE_VerifyCondition( db
, table
, cond
->u
.expr
.right
, valid
);
518 if( r
!= ERROR_SUCCESS
)
521 /* check the type of the comparison */
522 if( ( cond
->u
.expr
.left
->type
== EXPR_SVAL
) ||
523 ( cond
->u
.expr
.left
->type
== EXPR_COL_NUMBER_STRING
) ||
524 ( cond
->u
.expr
.right
->type
== EXPR_SVAL
) ||
525 ( cond
->u
.expr
.right
->type
== EXPR_COL_NUMBER_STRING
) )
527 switch( cond
->u
.expr
.op
)
535 return ERROR_INVALID_PARAMETER
;
538 /* FIXME: check we're comparing a string to a column */
540 cond
->type
= EXPR_STRCMP
;
545 if ( cond
->u
.expr
.left
->type
!= EXPR_COLUMN
)
548 return ERROR_INVALID_PARAMETER
;
550 r
= WHERE_VerifyCondition( db
, table
, cond
->u
.expr
.left
, valid
);
551 if( r
!= ERROR_SUCCESS
)
556 cond
->type
= EXPR_UVAL
;
557 cond
->u
.uval
= cond
->u
.ival
;
566 ERR("Invalid expression type\n");
571 return ERROR_SUCCESS
;
574 UINT
WHERE_CreateView( MSIDATABASE
*db
, MSIVIEW
**view
, MSIVIEW
*table
,
577 MSIWHEREVIEW
*wv
= NULL
;
578 UINT count
= 0, r
, valid
= 0;
580 TRACE("%p\n", table
);
582 r
= table
->ops
->get_dimensions( table
, NULL
, &count
);
583 if( r
!= ERROR_SUCCESS
)
585 ERR("can't get table dimensions\n");
591 r
= WHERE_VerifyCondition( db
, table
, cond
, &valid
);
592 if( r
!= ERROR_SUCCESS
)
595 return ERROR_FUNCTION_FAILED
;
598 wv
= msi_alloc_zero( sizeof *wv
);
600 return ERROR_FUNCTION_FAILED
;
602 /* fill the structure */
603 wv
->view
.ops
= &where_ops
;
604 msiobj_addref( &db
->hdr
);
611 *view
= (MSIVIEW
*) wv
;
613 return ERROR_SUCCESS
;