Release 1.3.7.
[wine/gsoc-2012-control.git] / dlls / msi / where.c
blob7cafa2ada4ba42ecb163539154795398bb7d3ed5
1 /*
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
21 #include <stdarg.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winerror.h"
26 #include "wine/debug.h"
27 #include "wine/unicode.h"
28 #include "msi.h"
29 #include "msiquery.h"
30 #include "objbase.h"
31 #include "objidl.h"
32 #include "msipriv.h"
33 #include "winnls.h"
35 #include "query.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
39 #define MSI_HASH_TABLE_SIZE 37
41 typedef struct tagMSIHASHENTRY
43 struct tagMSIHASHENTRY *next;
44 UINT value;
45 UINT row;
46 } MSIHASHENTRY;
48 /* below is the query interface to a table */
50 typedef struct tagMSIWHEREVIEW
52 MSIVIEW view;
53 MSIDATABASE *db;
54 MSIVIEW *table;
55 UINT row_count;
56 MSIHASHENTRY **reorder;
57 struct expr *cond;
58 UINT rec_index;
59 } MSIWHEREVIEW;
61 static void free_hash_table(MSIHASHENTRY **table)
63 MSIHASHENTRY *new, *old;
64 int i;
66 if (!table)
67 return;
69 for (i = 0; i < MSI_HASH_TABLE_SIZE; i++)
71 new = table[i];
73 while (new)
75 old = new;
76 new = old->next;
77 msi_free(old);
80 table[i] = NULL;
83 msi_free(table);
86 static UINT find_entry_in_hash(MSIHASHENTRY **table, UINT row, UINT *val)
88 MSIHASHENTRY *entry;
90 if (!table)
91 return ERROR_SUCCESS;
93 if (!(entry = table[row % MSI_HASH_TABLE_SIZE]))
95 WARN("Row not found in hash table!\n");
96 return ERROR_FUNCTION_FAILED;
99 while (entry && entry->row != row)
100 entry = entry->next;
102 if (entry) *val = entry->value;
103 return ERROR_SUCCESS;
106 static UINT add_entry_to_hash(MSIHASHENTRY **table, UINT row, UINT val)
108 MSIHASHENTRY *new = msi_alloc(sizeof(MSIHASHENTRY));
109 MSIHASHENTRY *prev;
111 if (!new)
112 return ERROR_OUTOFMEMORY;
114 new->next = NULL;
115 new->value = val;
116 new->row = row;
118 prev = table[row % MSI_HASH_TABLE_SIZE];
119 if (prev)
120 new->next = prev;
122 table[row % MSI_HASH_TABLE_SIZE] = new;
124 return ERROR_SUCCESS;
127 static UINT WHERE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
129 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
130 UINT r;
132 TRACE("%p %d %d %p\n", wv, row, col, val );
134 if( !wv->table )
135 return ERROR_FUNCTION_FAILED;
137 if( row > wv->row_count )
138 return ERROR_NO_MORE_ITEMS;
140 r = find_entry_in_hash(wv->reorder, row, &row);
141 if (r != ERROR_SUCCESS)
142 return r;
144 return wv->table->ops->fetch_int( wv->table, row, col, val );
147 static UINT WHERE_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm )
149 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
150 UINT r;
152 TRACE("%p %d %d %p\n", wv, row, col, stm );
154 if( !wv->table )
155 return ERROR_FUNCTION_FAILED;
157 if( row > wv->row_count )
158 return ERROR_NO_MORE_ITEMS;
160 r = find_entry_in_hash(wv->reorder, row, &row);
161 if (r != ERROR_SUCCESS)
162 return r;
164 return wv->table->ops->fetch_stream( wv->table, row, col, stm );
167 static UINT WHERE_get_row( struct tagMSIVIEW *view, UINT row, MSIRECORD **rec )
169 MSIWHEREVIEW *wv = (MSIWHEREVIEW *)view;
170 UINT r;
172 TRACE("%p %d %p\n", wv, row, rec );
174 if (!wv->table)
175 return ERROR_FUNCTION_FAILED;
177 if (row > wv->row_count)
178 return ERROR_NO_MORE_ITEMS;
180 r = find_entry_in_hash(wv->reorder, row, &row);
181 if (r != ERROR_SUCCESS)
182 return r;
184 return wv->table->ops->get_row(wv->table, row, rec);
187 static UINT WHERE_set_row( struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask )
189 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
190 UINT r;
192 TRACE("%p %d %p %08x\n", wv, row, rec, mask );
194 if( !wv->table )
195 return ERROR_FUNCTION_FAILED;
197 if( row > wv->row_count )
198 return ERROR_NO_MORE_ITEMS;
200 r = find_entry_in_hash(wv->reorder, row, &row);
201 if (r != ERROR_SUCCESS)
202 return r;
204 return wv->table->ops->set_row( wv->table, row, rec, mask );
207 static UINT WHERE_delete_row(struct tagMSIVIEW *view, UINT row)
209 MSIWHEREVIEW *wv = (MSIWHEREVIEW *)view;
210 UINT r;
212 TRACE("(%p %d)\n", view, row);
214 if ( !wv->table )
215 return ERROR_FUNCTION_FAILED;
217 if ( row > wv->row_count )
218 return ERROR_NO_MORE_ITEMS;
220 r = find_entry_in_hash( wv->reorder, row, &row );
221 if ( r != ERROR_SUCCESS )
222 return r;
224 return wv->table->ops->delete_row( wv->table, row );
227 static INT INT_evaluate_binary( INT lval, UINT op, INT rval )
229 switch( op )
231 case OP_EQ:
232 return ( lval == rval );
233 case OP_AND:
234 return ( lval && rval );
235 case OP_OR:
236 return ( lval || rval );
237 case OP_GT:
238 return ( lval > rval );
239 case OP_LT:
240 return ( lval < rval );
241 case OP_LE:
242 return ( lval <= rval );
243 case OP_GE:
244 return ( lval >= rval );
245 case OP_NE:
246 return ( lval != rval );
247 default:
248 ERR("Unknown operator %d\n", op );
250 return 0;
253 static INT INT_evaluate_unary( INT lval, UINT op )
255 switch( op )
257 case OP_ISNULL:
258 return ( !lval );
259 case OP_NOTNULL:
260 return ( lval );
261 default:
262 ERR("Unknown operator %d\n", op );
264 return 0;
267 static const WCHAR *STRING_evaluate( MSIWHEREVIEW *wv, UINT row,
268 const struct expr *expr,
269 const MSIRECORD *record )
271 UINT val = 0, r;
273 switch( expr->type )
275 case EXPR_COL_NUMBER_STRING:
276 r = wv->table->ops->fetch_int( wv->table, row, expr->u.col_number, &val );
277 if( r != ERROR_SUCCESS )
278 return NULL;
279 return msi_string_lookup_id( wv->db->strings, val );
281 case EXPR_SVAL:
282 return expr->u.sval;
284 case EXPR_WILDCARD:
285 return MSI_RecordGetString( record, ++wv->rec_index );
287 default:
288 ERR("Invalid expression type\n");
289 break;
291 return NULL;
294 static UINT STRCMP_Evaluate( MSIWHEREVIEW *wv, UINT row, const struct expr *cond,
295 INT *val, const MSIRECORD *record )
297 int sr;
298 const WCHAR *l_str, *r_str;
300 l_str = STRING_evaluate( wv, row, cond->u.expr.left, record );
301 r_str = STRING_evaluate( wv, row, cond->u.expr.right, record );
302 if( l_str == r_str ||
303 ((!l_str || !*l_str) && (!r_str || !*r_str)) )
304 sr = 0;
305 else if( l_str && ! r_str )
306 sr = 1;
307 else if( r_str && ! l_str )
308 sr = -1;
309 else
310 sr = strcmpW( l_str, r_str );
312 *val = ( cond->u.expr.op == OP_EQ && ( sr == 0 ) ) ||
313 ( cond->u.expr.op == OP_NE && ( sr != 0 ) );
315 return ERROR_SUCCESS;
318 static UINT WHERE_evaluate( MSIWHEREVIEW *wv, UINT row,
319 struct expr *cond, INT *val, MSIRECORD *record )
321 UINT r, tval;
322 INT lval, rval;
324 if( !cond )
325 return ERROR_SUCCESS;
327 switch( cond->type )
329 case EXPR_COL_NUMBER:
330 r = wv->table->ops->fetch_int( wv->table, row, cond->u.col_number, &tval );
331 *val = tval - 0x8000;
332 return ERROR_SUCCESS;
334 case EXPR_COL_NUMBER32:
335 r = wv->table->ops->fetch_int( wv->table, row, cond->u.col_number, &tval );
336 *val = tval - 0x80000000;
337 return r;
339 case EXPR_UVAL:
340 *val = cond->u.uval;
341 return ERROR_SUCCESS;
343 case EXPR_COMPLEX:
344 r = WHERE_evaluate( wv, row, cond->u.expr.left, &lval, record );
345 if( r != ERROR_SUCCESS )
346 return r;
347 r = WHERE_evaluate( wv, row, cond->u.expr.right, &rval, record );
348 if( r != ERROR_SUCCESS )
349 return r;
350 *val = INT_evaluate_binary( lval, cond->u.expr.op, rval );
351 return ERROR_SUCCESS;
353 case EXPR_UNARY:
354 r = wv->table->ops->fetch_int( wv->table, row, cond->u.expr.left->u.col_number, &tval );
355 if( r != ERROR_SUCCESS )
356 return r;
357 *val = INT_evaluate_unary( tval, cond->u.expr.op );
358 return ERROR_SUCCESS;
360 case EXPR_STRCMP:
361 return STRCMP_Evaluate( wv, row, cond, val, record );
363 case EXPR_WILDCARD:
364 *val = MSI_RecordGetInteger( record, ++wv->rec_index );
365 return ERROR_SUCCESS;
367 default:
368 ERR("Invalid expression type\n");
369 break;
372 return ERROR_SUCCESS;
375 static UINT WHERE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
377 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
378 UINT count = 0, r, i;
379 INT val;
380 MSIVIEW *table = wv->table;
382 TRACE("%p %p\n", wv, record);
384 if( !table )
385 return ERROR_FUNCTION_FAILED;
387 r = table->ops->execute( table, record );
388 if( r != ERROR_SUCCESS )
389 return r;
391 r = table->ops->get_dimensions( table, &count, NULL );
392 if( r != ERROR_SUCCESS )
393 return r;
395 free_hash_table(wv->reorder);
396 wv->reorder = msi_alloc_zero(MSI_HASH_TABLE_SIZE * sizeof(MSIHASHENTRY *));
397 if( !wv->reorder )
398 return ERROR_OUTOFMEMORY;
400 wv->row_count = 0;
402 if (0) /* disable optimization, there's no guarantee that strings are in the string table */
404 if (wv->cond->type == EXPR_STRCMP)
406 MSIITERHANDLE handle = NULL;
407 UINT row, value, col;
408 struct expr *col_cond = wv->cond->u.expr.left;
409 struct expr *val_cond = wv->cond->u.expr.right;
411 /* swap conditionals */
412 if (col_cond->type != EXPR_COL_NUMBER_STRING)
414 val_cond = wv->cond->u.expr.left;
415 col_cond = wv->cond->u.expr.right;
418 if ((col_cond->type == EXPR_COL_NUMBER_STRING) && (val_cond->type == EXPR_SVAL))
420 col = col_cond->u.col_number;
421 /* special case for "" - translate it into nil */
422 if (!val_cond->u.sval[0])
423 value = 0;
424 else
426 r = msi_string2idW(wv->db->strings, val_cond->u.sval, &value);
427 if (r != ERROR_SUCCESS)
429 TRACE("no id for %s, assuming it doesn't exist in the table\n", debugstr_w(wv->cond->u.expr.right->u.sval));
430 return ERROR_SUCCESS;
436 r = table->ops->find_matching_rows(table, col, value, &row, &handle);
437 if (r == ERROR_SUCCESS)
438 add_entry_to_hash(wv->reorder, wv->row_count++, row);
439 } while (r == ERROR_SUCCESS);
441 if (r == ERROR_NO_MORE_ITEMS)
442 return ERROR_SUCCESS;
443 else
444 return r;
446 /* else fallback to slow case */
450 for( i=0; i<count; i++ )
452 val = 0;
453 wv->rec_index = 0;
454 r = WHERE_evaluate( wv, i, wv->cond, &val, record );
455 if( r != ERROR_SUCCESS )
456 return r;
457 if( val )
458 add_entry_to_hash( wv->reorder, wv->row_count++, i );
461 return ERROR_SUCCESS;
464 static UINT WHERE_close( struct tagMSIVIEW *view )
466 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
468 TRACE("%p\n", wv );
470 if( !wv->table )
471 return ERROR_FUNCTION_FAILED;
473 return wv->table->ops->close( wv->table );
476 static UINT WHERE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols )
478 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
480 TRACE("%p %p %p\n", wv, rows, cols );
482 if( !wv->table )
483 return ERROR_FUNCTION_FAILED;
485 if( rows )
487 if( !wv->reorder )
488 return ERROR_FUNCTION_FAILED;
489 *rows = wv->row_count;
492 return wv->table->ops->get_dimensions( wv->table, NULL, cols );
495 static UINT WHERE_get_column_info( struct tagMSIVIEW *view,
496 UINT n, LPWSTR *name, UINT *type, BOOL *temporary,
497 LPWSTR *table_name)
499 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
501 TRACE("%p %d %p %p %p %p\n", wv, n, name, type, temporary, table_name );
503 if( !wv->table )
504 return ERROR_FUNCTION_FAILED;
506 return wv->table->ops->get_column_info( wv->table, n, name,
507 type, temporary, table_name );
510 static UINT WHERE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
511 MSIRECORD *rec, UINT row )
513 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
515 TRACE("%p %d %p\n", wv, eModifyMode, rec);
517 find_entry_in_hash(wv->reorder, row - 1, &row);
518 row++;
520 return wv->table->ops->modify( wv->table, eModifyMode, rec, row );
523 static UINT WHERE_delete( struct tagMSIVIEW *view )
525 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
527 TRACE("%p\n", wv );
529 if( wv->table )
530 wv->table->ops->delete( wv->table );
531 wv->table = 0;
533 free_hash_table(wv->reorder);
534 wv->reorder = NULL;
535 wv->row_count = 0;
537 msiobj_release( &wv->db->hdr );
538 msi_free( wv );
540 return ERROR_SUCCESS;
543 static UINT WHERE_find_matching_rows( struct tagMSIVIEW *view, UINT col,
544 UINT val, UINT *row, MSIITERHANDLE *handle )
546 MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
547 UINT r;
549 TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
551 if( !wv->table )
552 return ERROR_FUNCTION_FAILED;
554 r = wv->table->ops->find_matching_rows( wv->table, col, val, row, handle );
555 if (r != ERROR_SUCCESS)
556 return r;
558 if( *row > wv->row_count )
559 return ERROR_NO_MORE_ITEMS;
561 return find_entry_in_hash(wv->reorder, *row, row);
564 static UINT WHERE_sort(struct tagMSIVIEW *view, column_info *columns)
566 MSIWHEREVIEW *wv = (MSIWHEREVIEW *)view;
568 TRACE("%p %p\n", view, columns);
570 return wv->table->ops->sort(wv->table, columns);
573 static const MSIVIEWOPS where_ops =
575 WHERE_fetch_int,
576 WHERE_fetch_stream,
577 WHERE_get_row,
578 WHERE_set_row,
579 NULL,
580 WHERE_delete_row,
581 WHERE_execute,
582 WHERE_close,
583 WHERE_get_dimensions,
584 WHERE_get_column_info,
585 WHERE_modify,
586 WHERE_delete,
587 WHERE_find_matching_rows,
588 NULL,
589 NULL,
590 NULL,
591 NULL,
592 WHERE_sort,
593 NULL,
596 static UINT WHERE_VerifyCondition( MSIDATABASE *db, MSIVIEW *table, struct expr *cond,
597 UINT *valid )
599 UINT r, val = 0;
601 switch( cond->type )
603 case EXPR_COLUMN:
604 r = VIEW_find_column( table, cond->u.column.column,
605 cond->u.column.table, &val );
606 if( r == ERROR_SUCCESS )
608 UINT type = 0;
609 r = table->ops->get_column_info( table, val, NULL, &type,
610 NULL, NULL );
611 if( r == ERROR_SUCCESS )
613 if (type&MSITYPE_STRING)
614 cond->type = EXPR_COL_NUMBER_STRING;
615 else if ((type&0xff) == 4)
616 cond->type = EXPR_COL_NUMBER32;
617 else
618 cond->type = EXPR_COL_NUMBER;
619 cond->u.col_number = val;
620 *valid = 1;
622 else
623 *valid = 0;
625 else
627 *valid = 0;
628 WARN("Couldn't find column %s.%s\n", debugstr_w( cond->u.column.table ), debugstr_w( cond->u.column.column ) );
630 break;
631 case EXPR_COMPLEX:
632 r = WHERE_VerifyCondition( db, table, cond->u.expr.left, valid );
633 if( r != ERROR_SUCCESS )
634 return r;
635 if( !*valid )
636 return ERROR_SUCCESS;
637 r = WHERE_VerifyCondition( db, table, cond->u.expr.right, valid );
638 if( r != ERROR_SUCCESS )
639 return r;
641 /* check the type of the comparison */
642 if( ( cond->u.expr.left->type == EXPR_SVAL ) ||
643 ( cond->u.expr.left->type == EXPR_COL_NUMBER_STRING ) ||
644 ( cond->u.expr.right->type == EXPR_SVAL ) ||
645 ( cond->u.expr.right->type == EXPR_COL_NUMBER_STRING ) )
647 switch( cond->u.expr.op )
649 case OP_EQ:
650 case OP_NE:
651 break;
652 default:
653 *valid = FALSE;
654 return ERROR_INVALID_PARAMETER;
657 /* FIXME: check we're comparing a string to a column */
659 cond->type = EXPR_STRCMP;
662 break;
663 case EXPR_UNARY:
664 if ( cond->u.expr.left->type != EXPR_COLUMN )
666 *valid = FALSE;
667 return ERROR_INVALID_PARAMETER;
669 r = WHERE_VerifyCondition( db, table, cond->u.expr.left, valid );
670 if( r != ERROR_SUCCESS )
671 return r;
672 break;
673 case EXPR_IVAL:
674 *valid = 1;
675 cond->type = EXPR_UVAL;
676 cond->u.uval = cond->u.ival;
677 break;
678 case EXPR_WILDCARD:
679 *valid = 1;
680 break;
681 case EXPR_SVAL:
682 *valid = 1;
683 break;
684 default:
685 ERR("Invalid expression type\n");
686 *valid = 0;
687 break;
690 return ERROR_SUCCESS;
693 UINT WHERE_CreateView( MSIDATABASE *db, MSIVIEW **view, MSIVIEW *table,
694 struct expr *cond )
696 MSIWHEREVIEW *wv = NULL;
697 UINT count = 0, r, valid = 0;
699 TRACE("%p\n", table );
701 r = table->ops->get_dimensions( table, NULL, &count );
702 if( r != ERROR_SUCCESS )
704 ERR("can't get table dimensions\n");
705 return r;
708 if( cond )
710 r = WHERE_VerifyCondition( db, table, cond, &valid );
711 if( r != ERROR_SUCCESS )
712 return r;
713 if( !valid )
714 return ERROR_FUNCTION_FAILED;
717 wv = msi_alloc_zero( sizeof *wv );
718 if( !wv )
719 return ERROR_FUNCTION_FAILED;
721 /* fill the structure */
722 wv->view.ops = &where_ops;
723 msiobj_addref( &db->hdr );
724 wv->db = db;
725 wv->table = table;
726 wv->row_count = 0;
727 wv->reorder = NULL;
728 wv->cond = cond;
729 wv->rec_index = 0;
730 *view = (MSIVIEW*) wv;
732 return ERROR_SUCCESS;