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
);
38 #define MSI_HASH_TABLE_SIZE 37
40 typedef struct tagMSIHASHENTRY
42 struct tagMSIHASHENTRY
*next
;
47 /* below is the query interface to a table */
49 typedef struct tagMSIWHEREVIEW
55 MSIHASHENTRY
**reorder
;
60 static void free_hash_table(MSIHASHENTRY
**table
)
62 MSIHASHENTRY
*new, *old
;
68 for (i
= 0; i
< MSI_HASH_TABLE_SIZE
; i
++)
85 static UINT
find_entry_in_hash(MSIHASHENTRY
**table
, UINT row
, UINT
*val
)
92 if (!(entry
= table
[row
% MSI_HASH_TABLE_SIZE
]))
94 WARN("Row not found in hash table!\n");
95 return ERROR_FUNCTION_FAILED
;
98 while (entry
&& entry
->row
!= row
)
101 if (entry
) *val
= entry
->value
;
102 return ERROR_SUCCESS
;
105 static UINT
add_entry_to_hash(MSIHASHENTRY
**table
, UINT row
, UINT val
)
107 MSIHASHENTRY
*new = msi_alloc(sizeof(MSIHASHENTRY
));
111 return ERROR_OUTOFMEMORY
;
117 prev
= table
[row
% MSI_HASH_TABLE_SIZE
];
121 table
[row
% MSI_HASH_TABLE_SIZE
] = new;
123 return ERROR_SUCCESS
;
126 static UINT
WHERE_fetch_int( struct tagMSIVIEW
*view
, UINT row
, UINT col
, UINT
*val
)
128 MSIWHEREVIEW
*wv
= (MSIWHEREVIEW
*)view
;
131 TRACE("%p %d %d %p\n", wv
, row
, col
, val
);
134 return ERROR_FUNCTION_FAILED
;
136 if( row
> wv
->row_count
)
137 return ERROR_NO_MORE_ITEMS
;
139 r
= find_entry_in_hash(wv
->reorder
, row
, &row
);
140 if (r
!= ERROR_SUCCESS
)
143 return wv
->table
->ops
->fetch_int( wv
->table
, row
, col
, val
);
146 static UINT
WHERE_fetch_stream( struct tagMSIVIEW
*view
, UINT row
, UINT col
, IStream
**stm
)
148 MSIWHEREVIEW
*wv
= (MSIWHEREVIEW
*)view
;
151 TRACE("%p %d %d %p\n", wv
, row
, col
, stm
);
154 return ERROR_FUNCTION_FAILED
;
156 if( row
> wv
->row_count
)
157 return ERROR_NO_MORE_ITEMS
;
159 r
= find_entry_in_hash(wv
->reorder
, row
, &row
);
160 if (r
!= ERROR_SUCCESS
)
163 return wv
->table
->ops
->fetch_stream( wv
->table
, row
, col
, stm
);
166 static UINT
WHERE_get_row( struct tagMSIVIEW
*view
, UINT row
, MSIRECORD
**rec
)
168 MSIWHEREVIEW
*wv
= (MSIWHEREVIEW
*)view
;
171 TRACE("%p %d %p\n", wv
, row
, rec
);
174 return ERROR_FUNCTION_FAILED
;
176 if (row
> wv
->row_count
)
177 return ERROR_NO_MORE_ITEMS
;
179 r
= find_entry_in_hash(wv
->reorder
, row
, &row
);
180 if (r
!= ERROR_SUCCESS
)
183 return wv
->table
->ops
->get_row(wv
->table
, row
, rec
);
186 static UINT
WHERE_set_row( struct tagMSIVIEW
*view
, UINT row
, MSIRECORD
*rec
, UINT mask
)
188 MSIWHEREVIEW
*wv
= (MSIWHEREVIEW
*)view
;
191 TRACE("%p %d %p %08x\n", wv
, row
, rec
, mask
);
194 return ERROR_FUNCTION_FAILED
;
196 if( row
> wv
->row_count
)
197 return ERROR_NO_MORE_ITEMS
;
199 r
= find_entry_in_hash(wv
->reorder
, row
, &row
);
200 if (r
!= ERROR_SUCCESS
)
203 return wv
->table
->ops
->set_row( wv
->table
, row
, rec
, mask
);
206 static UINT
WHERE_delete_row(struct tagMSIVIEW
*view
, UINT row
)
208 MSIWHEREVIEW
*wv
= (MSIWHEREVIEW
*)view
;
211 TRACE("(%p %d)\n", view
, row
);
214 return ERROR_FUNCTION_FAILED
;
216 if ( row
> wv
->row_count
)
217 return ERROR_NO_MORE_ITEMS
;
219 r
= find_entry_in_hash( wv
->reorder
, row
, &row
);
220 if ( r
!= ERROR_SUCCESS
)
223 return wv
->table
->ops
->delete_row( wv
->table
, row
);
226 static INT
INT_evaluate_binary( INT lval
, UINT op
, INT rval
)
231 return ( lval
== rval
);
233 return ( lval
&& rval
);
235 return ( lval
|| rval
);
237 return ( lval
> rval
);
239 return ( lval
< rval
);
241 return ( lval
<= rval
);
243 return ( lval
>= rval
);
245 return ( lval
!= rval
);
247 ERR("Unknown operator %d\n", op
);
252 static INT
INT_evaluate_unary( INT lval
, UINT op
)
261 ERR("Unknown operator %d\n", op
);
266 static const WCHAR
*STRING_evaluate( MSIWHEREVIEW
*wv
, UINT row
,
267 const struct expr
*expr
,
268 const MSIRECORD
*record
)
274 case EXPR_COL_NUMBER_STRING
:
275 r
= wv
->table
->ops
->fetch_int( wv
->table
, row
, expr
->u
.col_number
, &val
);
276 if( r
!= ERROR_SUCCESS
)
278 return msi_string_lookup_id( wv
->db
->strings
, val
);
284 return MSI_RecordGetString( record
, ++wv
->rec_index
);
287 ERR("Invalid expression type\n");
293 static UINT
STRCMP_Evaluate( MSIWHEREVIEW
*wv
, UINT row
, const struct expr
*cond
,
294 INT
*val
, const MSIRECORD
*record
)
297 const WCHAR
*l_str
, *r_str
;
299 l_str
= STRING_evaluate( wv
, row
, cond
->u
.expr
.left
, record
);
300 r_str
= STRING_evaluate( wv
, row
, cond
->u
.expr
.right
, record
);
301 if( l_str
== r_str
||
302 ((!l_str
|| !*l_str
) && (!r_str
|| !*r_str
)) )
304 else if( l_str
&& ! r_str
)
306 else if( r_str
&& ! l_str
)
309 sr
= lstrcmpW( l_str
, r_str
);
311 *val
= ( cond
->u
.expr
.op
== OP_EQ
&& ( sr
== 0 ) ) ||
312 ( cond
->u
.expr
.op
== OP_NE
&& ( sr
!= 0 ) );
314 return ERROR_SUCCESS
;
317 static UINT
WHERE_evaluate( MSIWHEREVIEW
*wv
, UINT row
,
318 struct expr
*cond
, INT
*val
, MSIRECORD
*record
)
324 return ERROR_SUCCESS
;
328 case EXPR_COL_NUMBER
:
329 r
= wv
->table
->ops
->fetch_int( wv
->table
, row
, cond
->u
.col_number
, &tval
);
330 *val
= tval
- 0x8000;
331 return ERROR_SUCCESS
;
333 case EXPR_COL_NUMBER32
:
334 r
= wv
->table
->ops
->fetch_int( wv
->table
, row
, cond
->u
.col_number
, &tval
);
335 *val
= tval
- 0x80000000;
340 return ERROR_SUCCESS
;
343 r
= WHERE_evaluate( wv
, row
, cond
->u
.expr
.left
, &lval
, record
);
344 if( r
!= ERROR_SUCCESS
)
346 r
= WHERE_evaluate( wv
, row
, cond
->u
.expr
.right
, &rval
, record
);
347 if( r
!= ERROR_SUCCESS
)
349 *val
= INT_evaluate_binary( lval
, cond
->u
.expr
.op
, rval
);
350 return ERROR_SUCCESS
;
353 r
= wv
->table
->ops
->fetch_int( wv
->table
, row
, cond
->u
.expr
.left
->u
.col_number
, &tval
);
354 if( r
!= ERROR_SUCCESS
)
356 *val
= INT_evaluate_unary( tval
, cond
->u
.expr
.op
);
357 return ERROR_SUCCESS
;
360 return STRCMP_Evaluate( wv
, row
, cond
, val
, record
);
363 *val
= MSI_RecordGetInteger( record
, ++wv
->rec_index
);
364 return ERROR_SUCCESS
;
367 ERR("Invalid expression type\n");
371 return ERROR_SUCCESS
;
374 static UINT
WHERE_execute( struct tagMSIVIEW
*view
, MSIRECORD
*record
)
376 MSIWHEREVIEW
*wv
= (MSIWHEREVIEW
*)view
;
377 UINT count
= 0, r
, i
;
379 MSIVIEW
*table
= wv
->table
;
381 TRACE("%p %p\n", wv
, record
);
384 return ERROR_FUNCTION_FAILED
;
386 r
= table
->ops
->execute( table
, record
);
387 if( r
!= ERROR_SUCCESS
)
390 r
= table
->ops
->get_dimensions( table
, &count
, NULL
);
391 if( r
!= ERROR_SUCCESS
)
394 free_hash_table(wv
->reorder
);
395 wv
->reorder
= msi_alloc_zero(MSI_HASH_TABLE_SIZE
* sizeof(MSIHASHENTRY
*));
397 return ERROR_OUTOFMEMORY
;
401 if (0) /* disable optimization, there's no guarantee that strings are in the string table */
403 if (wv
->cond
->type
== EXPR_STRCMP
)
405 MSIITERHANDLE handle
= NULL
;
406 UINT row
, value
, col
;
407 struct expr
*col_cond
= wv
->cond
->u
.expr
.left
;
408 struct expr
*val_cond
= wv
->cond
->u
.expr
.right
;
410 /* swap conditionals */
411 if (col_cond
->type
!= EXPR_COL_NUMBER_STRING
)
413 val_cond
= wv
->cond
->u
.expr
.left
;
414 col_cond
= wv
->cond
->u
.expr
.right
;
417 if ((col_cond
->type
== EXPR_COL_NUMBER_STRING
) && (val_cond
->type
== EXPR_SVAL
))
419 col
= col_cond
->u
.col_number
;
420 /* special case for "" - translate it into nil */
421 if (!val_cond
->u
.sval
[0])
425 r
= msi_string2idW(wv
->db
->strings
, val_cond
->u
.sval
, &value
);
426 if (r
!= ERROR_SUCCESS
)
428 TRACE("no id for %s, assuming it doesn't exist in the table\n", debugstr_w(wv
->cond
->u
.expr
.right
->u
.sval
));
429 return ERROR_SUCCESS
;
435 r
= table
->ops
->find_matching_rows(table
, col
, value
, &row
, &handle
);
436 if (r
== ERROR_SUCCESS
)
437 add_entry_to_hash(wv
->reorder
, wv
->row_count
++, row
);
438 } while (r
== ERROR_SUCCESS
);
440 if (r
== ERROR_NO_MORE_ITEMS
)
441 return ERROR_SUCCESS
;
445 /* else fallback to slow case */
449 for( i
=0; i
<count
; i
++ )
453 r
= WHERE_evaluate( wv
, i
, wv
->cond
, &val
, record
);
454 if( r
!= ERROR_SUCCESS
)
457 add_entry_to_hash( wv
->reorder
, wv
->row_count
++, i
);
460 return ERROR_SUCCESS
;
463 static UINT
WHERE_close( struct tagMSIVIEW
*view
)
465 MSIWHEREVIEW
*wv
= (MSIWHEREVIEW
*)view
;
470 return ERROR_FUNCTION_FAILED
;
472 return wv
->table
->ops
->close( wv
->table
);
475 static UINT
WHERE_get_dimensions( struct tagMSIVIEW
*view
, UINT
*rows
, UINT
*cols
)
477 MSIWHEREVIEW
*wv
= (MSIWHEREVIEW
*)view
;
479 TRACE("%p %p %p\n", wv
, rows
, cols
);
482 return ERROR_FUNCTION_FAILED
;
487 return ERROR_FUNCTION_FAILED
;
488 *rows
= wv
->row_count
;
491 return wv
->table
->ops
->get_dimensions( wv
->table
, NULL
, cols
);
494 static UINT
WHERE_get_column_info( struct tagMSIVIEW
*view
,
495 UINT n
, LPWSTR
*name
, UINT
*type
, BOOL
*temporary
,
498 MSIWHEREVIEW
*wv
= (MSIWHEREVIEW
*)view
;
500 TRACE("%p %d %p %p %p %p\n", wv
, n
, name
, type
, temporary
, table_name
);
503 return ERROR_FUNCTION_FAILED
;
505 return wv
->table
->ops
->get_column_info( wv
->table
, n
, name
,
506 type
, temporary
, table_name
);
509 static UINT
WHERE_modify( struct tagMSIVIEW
*view
, MSIMODIFY eModifyMode
,
510 MSIRECORD
*rec
, UINT row
)
512 MSIWHEREVIEW
*wv
= (MSIWHEREVIEW
*)view
;
514 TRACE("%p %d %p\n", wv
, eModifyMode
, rec
);
516 find_entry_in_hash(wv
->reorder
, row
- 1, &row
);
519 return wv
->table
->ops
->modify( wv
->table
, eModifyMode
, rec
, row
);
522 static UINT
WHERE_delete( struct tagMSIVIEW
*view
)
524 MSIWHEREVIEW
*wv
= (MSIWHEREVIEW
*)view
;
529 wv
->table
->ops
->delete( wv
->table
);
532 free_hash_table(wv
->reorder
);
536 msiobj_release( &wv
->db
->hdr
);
539 return ERROR_SUCCESS
;
542 static UINT
WHERE_find_matching_rows( struct tagMSIVIEW
*view
, UINT col
,
543 UINT val
, UINT
*row
, MSIITERHANDLE
*handle
)
545 MSIWHEREVIEW
*wv
= (MSIWHEREVIEW
*)view
;
548 TRACE("%p, %d, %u, %p\n", view
, col
, val
, *handle
);
551 return ERROR_FUNCTION_FAILED
;
553 r
= wv
->table
->ops
->find_matching_rows( wv
->table
, col
, val
, row
, handle
);
554 if (r
!= ERROR_SUCCESS
)
557 if( *row
> wv
->row_count
)
558 return ERROR_NO_MORE_ITEMS
;
560 return find_entry_in_hash(wv
->reorder
, *row
, row
);
563 static UINT
WHERE_sort(struct tagMSIVIEW
*view
, column_info
*columns
)
565 MSIWHEREVIEW
*wv
= (MSIWHEREVIEW
*)view
;
567 TRACE("%p %p\n", view
, columns
);
569 return wv
->table
->ops
->sort(wv
->table
, columns
);
572 static const MSIVIEWOPS where_ops
=
582 WHERE_get_dimensions
,
583 WHERE_get_column_info
,
586 WHERE_find_matching_rows
,
595 static UINT
WHERE_VerifyCondition( MSIDATABASE
*db
, MSIVIEW
*table
, struct expr
*cond
,
603 r
= VIEW_find_column( table
, cond
->u
.column
.column
,
604 cond
->u
.column
.table
, &val
);
605 if( r
== ERROR_SUCCESS
)
608 r
= table
->ops
->get_column_info( table
, val
, NULL
, &type
,
610 if( r
== ERROR_SUCCESS
)
612 if (type
&MSITYPE_STRING
)
613 cond
->type
= EXPR_COL_NUMBER_STRING
;
614 else if ((type
&0xff) == 4)
615 cond
->type
= EXPR_COL_NUMBER32
;
617 cond
->type
= EXPR_COL_NUMBER
;
618 cond
->u
.col_number
= val
;
627 WARN("Couldn't find column %s.%s\n", debugstr_w( cond
->u
.column
.table
), debugstr_w( cond
->u
.column
.column
) );
631 r
= WHERE_VerifyCondition( db
, table
, cond
->u
.expr
.left
, valid
);
632 if( r
!= ERROR_SUCCESS
)
635 return ERROR_SUCCESS
;
636 r
= WHERE_VerifyCondition( db
, table
, cond
->u
.expr
.right
, valid
);
637 if( r
!= ERROR_SUCCESS
)
640 /* check the type of the comparison */
641 if( ( cond
->u
.expr
.left
->type
== EXPR_SVAL
) ||
642 ( cond
->u
.expr
.left
->type
== EXPR_COL_NUMBER_STRING
) ||
643 ( cond
->u
.expr
.right
->type
== EXPR_SVAL
) ||
644 ( cond
->u
.expr
.right
->type
== EXPR_COL_NUMBER_STRING
) )
646 switch( cond
->u
.expr
.op
)
653 return ERROR_INVALID_PARAMETER
;
656 /* FIXME: check we're comparing a string to a column */
658 cond
->type
= EXPR_STRCMP
;
663 if ( cond
->u
.expr
.left
->type
!= EXPR_COLUMN
)
666 return ERROR_INVALID_PARAMETER
;
668 r
= WHERE_VerifyCondition( db
, table
, cond
->u
.expr
.left
, valid
);
669 if( r
!= ERROR_SUCCESS
)
674 cond
->type
= EXPR_UVAL
;
675 cond
->u
.uval
= cond
->u
.ival
;
684 ERR("Invalid expression type\n");
689 return ERROR_SUCCESS
;
692 UINT
WHERE_CreateView( MSIDATABASE
*db
, MSIVIEW
**view
, MSIVIEW
*table
,
695 MSIWHEREVIEW
*wv
= NULL
;
696 UINT count
= 0, r
, valid
= 0;
698 TRACE("%p\n", table
);
700 r
= table
->ops
->get_dimensions( table
, NULL
, &count
);
701 if( r
!= ERROR_SUCCESS
)
703 ERR("can't get table dimensions\n");
709 r
= WHERE_VerifyCondition( db
, table
, cond
, &valid
);
710 if( r
!= ERROR_SUCCESS
)
713 return ERROR_FUNCTION_FAILED
;
716 wv
= msi_alloc_zero( sizeof *wv
);
718 return ERROR_FUNCTION_FAILED
;
720 /* fill the structure */
721 wv
->view
.ops
= &where_ops
;
722 msiobj_addref( &db
->hdr
);
729 *view
= (MSIVIEW
*) wv
;
731 return ERROR_SUCCESS
;