where.c 18 KB
Newer Older
1 2 3
/*
 * Implementation of the Microsoft Installer (msi.dll)
 *
4
 * Copyright 2002 Mike McCormack for CodeWeavers
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
19 20
 */

21 22
#include <stdarg.h>

23 24 25 26 27 28 29 30 31 32 33 34 35
#include "windef.h"
#include "winbase.h"
#include "winerror.h"
#include "wine/debug.h"
#include "msi.h"
#include "msiquery.h"
#include "objbase.h"
#include "objidl.h"
#include "msipriv.h"
#include "winnls.h"

#include "query.h"

36
WINE_DEFAULT_DEBUG_CHANNEL(msidb);
37

38 39 40 41 42 43 44 45
#define MSI_HASH_TABLE_SIZE 37

typedef struct tagMSIHASHENTRY
{
    struct tagMSIHASHENTRY *next;
    UINT value;
    UINT row;
} MSIHASHENTRY;
46 47 48 49 50 51 52 53 54

/* below is the query interface to a table */

typedef struct tagMSIWHEREVIEW
{
    MSIVIEW        view;
    MSIDATABASE   *db;
    MSIVIEW       *table;
    UINT           row_count;
55
    MSIHASHENTRY **reorder;
56
    struct expr   *cond;
57
    UINT           rec_index;
58 59
} MSIWHEREVIEW;

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
static void free_hash_table(MSIHASHENTRY **table)
{
    MSIHASHENTRY *new, *old;
    int i;

    if (!table)
        return;

    for (i = 0; i < MSI_HASH_TABLE_SIZE; i++)
    {
        new = table[i];

        while (new)
        {
            old = new;
            new = old->next;
            msi_free(old);
        }

        table[i] = NULL;
    }

    msi_free(table);
}

static UINT find_entry_in_hash(MSIHASHENTRY **table, UINT row, UINT *val)
{
    MSIHASHENTRY *entry;

89 90 91
    if (!table)
        return ERROR_SUCCESS;

92 93
    if (!(entry = table[row % MSI_HASH_TABLE_SIZE]))
    {
94
        WARN("Row not found in hash table!\n");
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
        return ERROR_FUNCTION_FAILED;
    }

    while (entry && entry->row != row)
        entry = entry->next;

    if (entry) *val = entry->value;
    return ERROR_SUCCESS;
}

static UINT add_entry_to_hash(MSIHASHENTRY **table, UINT row, UINT val)
{
    MSIHASHENTRY *new = msi_alloc(sizeof(MSIHASHENTRY));
    MSIHASHENTRY *prev;

    if (!new)
        return ERROR_OUTOFMEMORY;

    new->next = NULL;
    new->value = val;
    new->row = row;

    prev = table[row % MSI_HASH_TABLE_SIZE];
    if (prev)
        new->next = prev;

    table[row % MSI_HASH_TABLE_SIZE] = new;

    return ERROR_SUCCESS;
}

126 127 128
static UINT WHERE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
{
    MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
129
    UINT r;
130 131 132 133 134 135 136 137 138

    TRACE("%p %d %d %p\n", wv, row, col, val );

    if( !wv->table )
        return ERROR_FUNCTION_FAILED;

    if( row > wv->row_count )
        return ERROR_NO_MORE_ITEMS;

139 140 141
    r = find_entry_in_hash(wv->reorder, row, &row);
    if (r != ERROR_SUCCESS)
        return r;
142 143 144 145

    return wv->table->ops->fetch_int( wv->table, row, col, val );
}

146 147 148
static UINT WHERE_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm )
{
    MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
149
    UINT r;
150 151 152 153 154 155 156 157 158

    TRACE("%p %d %d %p\n", wv, row, col, stm );

    if( !wv->table )
        return ERROR_FUNCTION_FAILED;

    if( row > wv->row_count )
        return ERROR_NO_MORE_ITEMS;

159 160 161
    r = find_entry_in_hash(wv->reorder, row, &row);
    if (r != ERROR_SUCCESS)
        return r;
162 163 164 165

    return wv->table->ops->fetch_stream( wv->table, row, col, stm );
}

166 167 168
static UINT WHERE_get_row( struct tagMSIVIEW *view, UINT row, MSIRECORD **rec )
{
    MSIWHEREVIEW *wv = (MSIWHEREVIEW *)view;
169
    UINT r;
170 171 172 173 174 175 176 177 178

    TRACE("%p %d %p\n", wv, row, rec );

    if (!wv->table)
        return ERROR_FUNCTION_FAILED;

    if (row > wv->row_count)
        return ERROR_NO_MORE_ITEMS;

179 180 181
    r = find_entry_in_hash(wv->reorder, row, &row);
    if (r != ERROR_SUCCESS)
        return r;
182

183
    return wv->table->ops->get_row(wv->table, row, rec);
184 185
}

186
static UINT WHERE_set_row( struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask )
187 188
{
    MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
189
    UINT r;
190

191
    TRACE("%p %d %p %08x\n", wv, row, rec, mask );
192 193 194

    if( !wv->table )
         return ERROR_FUNCTION_FAILED;
195

196 197
    if( row > wv->row_count )
        return ERROR_NO_MORE_ITEMS;
198

199 200 201
    r = find_entry_in_hash(wv->reorder, row, &row);
    if (r != ERROR_SUCCESS)
        return r;
202 203

    return wv->table->ops->set_row( wv->table, row, rec, mask );
204 205
}

206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
static UINT WHERE_delete_row(struct tagMSIVIEW *view, UINT row)
{
    MSIWHEREVIEW *wv = (MSIWHEREVIEW *)view;
    UINT r;

    TRACE("(%p %d)\n", view, row);

    if ( !wv->table )
        return ERROR_FUNCTION_FAILED;

    if ( row > wv->row_count )
        return ERROR_NO_MORE_ITEMS;

    r = find_entry_in_hash( wv->reorder, row, &row );
    if ( r != ERROR_SUCCESS )
        return r;

    return wv->table->ops->delete_row( wv->table, row );
}

226
static INT INT_evaluate_binary( INT lval, UINT op, INT rval )
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
{
    switch( op )
    {
    case OP_EQ:
        return ( lval == rval );
    case OP_AND:
        return ( lval && rval );
    case OP_OR:
        return ( lval || rval );
    case OP_GT:
        return ( lval > rval );
    case OP_LT:
        return ( lval < rval );
    case OP_LE:
        return ( lval <= rval );
    case OP_GE:
        return ( lval >= rval );
    case OP_NE:
        return ( lval != rval );
246 247 248 249 250 251 252 253 254 255
    default:
        ERR("Unknown operator %d\n", op );
    }
    return 0;
}

static INT INT_evaluate_unary( INT lval, UINT op )
{
    switch( op )
    {
256 257 258 259 260 261 262 263 264 265
    case OP_ISNULL:
        return ( !lval );
    case OP_NOTNULL:
        return ( lval );
    default:
        ERR("Unknown operator %d\n", op );
    }
    return 0;
}

266 267 268
static const WCHAR *STRING_evaluate( MSIWHEREVIEW *wv, UINT row,
                                     const struct expr *expr,
                                     const MSIRECORD *record )
269 270 271 272 273
{
    UINT val = 0, r;

    switch( expr->type )
    {
274
    case EXPR_COL_NUMBER_STRING:
275
        r = wv->table->ops->fetch_int( wv->table, row, expr->u.col_number, &val );
276 277
        if( r != ERROR_SUCCESS )
            return NULL;
278
        return msi_string_lookup_id( wv->db->strings, val );
279

280 281 282 283
    case EXPR_SVAL:
        return expr->u.sval;

    case EXPR_WILDCARD:
284
        return MSI_RecordGetString( record, ++wv->rec_index );
285 286 287 288 289 290 291 292

    default:
        ERR("Invalid expression type\n");
        break;
    }
    return NULL;
}

293 294
static UINT STRCMP_Evaluate( MSIWHEREVIEW *wv, UINT row, const struct expr *cond,
                             INT *val, const MSIRECORD *record )
295 296
{
    int sr;
297
    const WCHAR *l_str, *r_str;
298

299 300
    l_str = STRING_evaluate( wv, row, cond->u.expr.left, record );
    r_str = STRING_evaluate( wv, row, cond->u.expr.right, record );
301 302
    if( l_str == r_str ||
        ((!l_str || !*l_str) && (!r_str || !*r_str)) )
303 304 305 306 307 308
        sr = 0;
    else if( l_str && ! r_str )
        sr = 1;
    else if( r_str && ! l_str )
        sr = -1;
    else
309
        sr = lstrcmpW( l_str, r_str );
310 311

    *val = ( cond->u.expr.op == OP_EQ && ( sr == 0 ) ) ||
312
           ( cond->u.expr.op == OP_NE && ( sr != 0 ) ) ||
313 314 315 316 317 318
           ( cond->u.expr.op == OP_LT && ( sr < 0 ) ) ||
           ( cond->u.expr.op == OP_GT && ( sr > 0 ) );

    return ERROR_SUCCESS;
}

319 320
static UINT WHERE_evaluate( MSIWHEREVIEW *wv, UINT row,
                            struct expr *cond, INT *val, MSIRECORD *record )
321
{
322 323
    UINT r, tval;
    INT lval, rval;
324 325 326 327 328 329 330

    if( !cond )
        return ERROR_SUCCESS;

    switch( cond->type )
    {
    case EXPR_COL_NUMBER:
331
        r = wv->table->ops->fetch_int( wv->table, row, cond->u.col_number, &tval );
332 333 334 335
        *val = tval - 0x8000;
        return ERROR_SUCCESS;

    case EXPR_COL_NUMBER32:
336
        r = wv->table->ops->fetch_int( wv->table, row, cond->u.col_number, &tval );
337
        *val = tval - 0x80000000;
338
        return r;
339 340 341 342 343 344

    case EXPR_UVAL:
        *val = cond->u.uval;
        return ERROR_SUCCESS;

    case EXPR_COMPLEX:
345
        r = WHERE_evaluate( wv, row, cond->u.expr.left, &lval, record );
346 347
        if( r != ERROR_SUCCESS )
            return r;
348
        r = WHERE_evaluate( wv, row, cond->u.expr.right, &rval, record );
349 350
        if( r != ERROR_SUCCESS )
            return r;
351 352 353 354
        *val = INT_evaluate_binary( lval, cond->u.expr.op, rval );
        return ERROR_SUCCESS;

    case EXPR_UNARY:
355
        r = wv->table->ops->fetch_int( wv->table, row, cond->u.expr.left->u.col_number, &tval );
356 357 358
        if( r != ERROR_SUCCESS )
            return r;
        *val = INT_evaluate_unary( tval, cond->u.expr.op );
359 360
        return ERROR_SUCCESS;

361
    case EXPR_STRCMP:
362
        return STRCMP_Evaluate( wv, row, cond, val, record );
363 364

    case EXPR_WILDCARD:
365
        *val = MSI_RecordGetInteger( record, ++wv->rec_index );
366
        return ERROR_SUCCESS;
367

368 369 370
    default:
        ERR("Invalid expression type\n");
        break;
371
    }
372 373 374 375

    return ERROR_SUCCESS;
}

376
static UINT WHERE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
377 378
{
    MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
379 380
    UINT count = 0, r, i;
    INT val;
381 382
    MSIVIEW *table = wv->table;

383
    TRACE("%p %p\n", wv, record);
384 385 386 387 388 389 390 391 392 393 394 395

    if( !table )
         return ERROR_FUNCTION_FAILED;

    r = table->ops->execute( table, record );
    if( r != ERROR_SUCCESS )
        return r;

    r = table->ops->get_dimensions( table, &count, NULL );
    if( r != ERROR_SUCCESS )
        return r;

396 397
    free_hash_table(wv->reorder);
    wv->reorder = msi_alloc_zero(MSI_HASH_TABLE_SIZE * sizeof(MSIHASHENTRY *));
398
    if( !wv->reorder )
399
        return ERROR_OUTOFMEMORY;
400

401
    wv->row_count = 0;
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
    if (wv->cond->type == EXPR_STRCMP)
    {
        MSIITERHANDLE handle = NULL;
        UINT row, value, col;
        struct expr *col_cond = wv->cond->u.expr.left;
        struct expr *val_cond = wv->cond->u.expr.right;

        /* swap conditionals */
        if (col_cond->type != EXPR_COL_NUMBER_STRING)
        {
            val_cond = wv->cond->u.expr.left;
            col_cond = wv->cond->u.expr.right;
        }

        if ((col_cond->type == EXPR_COL_NUMBER_STRING) && (val_cond->type == EXPR_SVAL))
        {
            col = col_cond->u.col_number;
419 420 421 422
            /* special case for "" - translate it into nil */
            if (!val_cond->u.sval[0])
                value = 0;
            else
423
            {
424 425 426 427 428 429
                r = msi_string2idW(wv->db->strings, val_cond->u.sval, &value);
                if (r != ERROR_SUCCESS)
                {
                    TRACE("no id for %s, assuming it doesn't exist in the table\n", debugstr_w(wv->cond->u.expr.right->u.sval));
                    return ERROR_SUCCESS;
                }
430 431 432 433 434 435
            }

            do
            {
                r = table->ops->find_matching_rows(table, col, value, &row, &handle);
                if (r == ERROR_SUCCESS)
436
                    add_entry_to_hash(wv->reorder, wv->row_count++, row);
437 438 439 440 441 442 443 444 445 446
            } while (r == ERROR_SUCCESS);

            if (r == ERROR_NO_MORE_ITEMS)
                return ERROR_SUCCESS;
            else
                return r;
        }
        /* else fallback to slow case */
    }

447 448 449
    for( i=0; i<count; i++ )
    {
        val = 0;
450 451
        wv->rec_index = 0;
        r = WHERE_evaluate( wv, i, wv->cond, &val, record );
452 453 454
        if( r != ERROR_SUCCESS )
            return r;
        if( val )
455
            add_entry_to_hash( wv->reorder, wv->row_count++, i );
456 457 458 459 460 461 462 463 464 465 466 467
    }

    return ERROR_SUCCESS;
}

static UINT WHERE_close( struct tagMSIVIEW *view )
{
    MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;

    TRACE("%p\n", wv );

    if( !wv->table )
468
        return ERROR_FUNCTION_FAILED;
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492

    return wv->table->ops->close( wv->table );
}

static UINT WHERE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols )
{
    MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;

    TRACE("%p %p %p\n", wv, rows, cols );

    if( !wv->table )
         return ERROR_FUNCTION_FAILED;

    if( rows )
    {
        if( !wv->reorder )
            return ERROR_FUNCTION_FAILED;
        *rows = wv->row_count;
    }

    return wv->table->ops->get_dimensions( wv->table, NULL, cols );
}

static UINT WHERE_get_column_info( struct tagMSIVIEW *view,
493
                UINT n, LPWSTR *name, UINT *type, BOOL *temporary )
494 495 496
{
    MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;

497
    TRACE("%p %d %p %p %p\n", wv, n, name, type, temporary );
498 499 500 501

    if( !wv->table )
         return ERROR_FUNCTION_FAILED;

502 503
    return wv->table->ops->get_column_info( wv->table, n, name,
                                            type, temporary );
504 505
}

506
static UINT WHERE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
507
                          MSIRECORD *rec, UINT row )
508 509 510
{
    MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;

511
    TRACE("%p %d %p\n", wv, eModifyMode, rec);
512

513
    find_entry_in_hash(wv->reorder, row - 1, &row);
514 515
    row++;

516
    return wv->table->ops->modify( wv->table, eModifyMode, rec, row );
517 518 519 520 521 522 523 524 525 526
}

static UINT WHERE_delete( struct tagMSIVIEW *view )
{
    MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;

    TRACE("%p\n", wv );

    if( wv->table )
        wv->table->ops->delete( wv->table );
527
    wv->table = 0;
528

529
    free_hash_table(wv->reorder);
530 531 532
    wv->reorder = NULL;
    wv->row_count = 0;

533
    msiobj_release( &wv->db->hdr );
534
    msi_free( wv );
535 536 537 538

    return ERROR_SUCCESS;
}

539 540 541 542 543 544 545 546 547 548 549 550
static UINT WHERE_find_matching_rows( struct tagMSIVIEW *view, UINT col,
    UINT val, UINT *row, MSIITERHANDLE *handle )
{
    MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
    UINT r;

    TRACE("%p, %d, %u, %p\n", view, col, val, *handle);

    if( !wv->table )
         return ERROR_FUNCTION_FAILED;

    r = wv->table->ops->find_matching_rows( wv->table, col, val, row, handle );
551 552
    if (r != ERROR_SUCCESS)
        return r;
553 554 555 556

    if( *row > wv->row_count )
        return ERROR_NO_MORE_ITEMS;

557
    return find_entry_in_hash(wv->reorder, *row, row);
558 559
}

560 561 562 563 564 565 566 567
static UINT WHERE_sort(struct tagMSIVIEW *view, column_info *columns)
{
    MSIWHEREVIEW *wv = (MSIWHEREVIEW *)view;

    TRACE("%p %p\n", view, columns);

    return wv->table->ops->sort(wv->table, columns);
}
568

569
static const MSIVIEWOPS where_ops =
570 571
{
    WHERE_fetch_int,
572
    WHERE_fetch_stream,
573
    WHERE_get_row,
574
    WHERE_set_row,
575
    NULL,
576
    WHERE_delete_row,
577 578 579 580 581
    WHERE_execute,
    WHERE_close,
    WHERE_get_dimensions,
    WHERE_get_column_info,
    WHERE_modify,
582
    WHERE_delete,
583 584 585
    WHERE_find_matching_rows,
    NULL,
    NULL,
586
    NULL,
587
    NULL,
588
    WHERE_sort,
589
    NULL,
590 591
};

592
static UINT WHERE_VerifyCondition( MSIDATABASE *db, MSIVIEW *table, struct expr *cond,
593 594
                                   UINT *valid )
{
595
    UINT r, val = 0;
596 597 598 599

    switch( cond->type )
    {
    case EXPR_COLUMN:
600
        r = VIEW_find_column( table, cond->u.column, &val );
601 602
        if( r == ERROR_SUCCESS )
        {
603
            UINT type = 0;
604
            r = table->ops->get_column_info( table, val, NULL, &type, NULL );
605 606 607 608
            if( r == ERROR_SUCCESS )
            {
                if (type&MSITYPE_STRING)
                    cond->type = EXPR_COL_NUMBER_STRING;
609 610
                else if ((type&0xff) == 4)
                    cond->type = EXPR_COL_NUMBER32;
611 612 613 614 615 616 617
                else
                    cond->type = EXPR_COL_NUMBER;
                cond->u.col_number = val;
                *valid = 1;
            }
            else
                *valid = 0;
618 619 620 621
        }
        else
        {
            *valid = 0;
622
            WARN("Couldn't find column %s\n", debugstr_w( cond->u.column ) );
623 624 625
        }
        break;
    case EXPR_COMPLEX:
626
        r = WHERE_VerifyCondition( db, table, cond->u.expr.left, valid );
627 628 629 630
        if( r != ERROR_SUCCESS )
            return r;
        if( !*valid )
            return ERROR_SUCCESS;
631
        r = WHERE_VerifyCondition( db, table, cond->u.expr.right, valid );
632 633
        if( r != ERROR_SUCCESS )
            return r;
634 635

        /* check the type of the comparison */
636
        if( ( cond->u.expr.left->type == EXPR_SVAL ) ||
637 638 639
            ( cond->u.expr.left->type == EXPR_COL_NUMBER_STRING ) ||
            ( cond->u.expr.right->type == EXPR_SVAL ) ||
            ( cond->u.expr.right->type == EXPR_COL_NUMBER_STRING ) )
640 641 642 643 644 645
        {
            switch( cond->u.expr.op )
            {
            case OP_EQ:
            case OP_GT:
            case OP_LT:
646
            case OP_NE:
647 648 649 650 651 652 653 654 655 656 657
                break;
            default:
                *valid = FALSE;
                return ERROR_INVALID_PARAMETER;
            }

            /* FIXME: check we're comparing a string to a column */

            cond->type = EXPR_STRCMP;
        }

658
        break;
659 660 661 662 663 664 665 666 667 668
    case EXPR_UNARY:
        if ( cond->u.expr.left->type != EXPR_COLUMN )
        {
            *valid = FALSE;
            return ERROR_INVALID_PARAMETER;
        }
        r = WHERE_VerifyCondition( db, table, cond->u.expr.left, valid );
        if( r != ERROR_SUCCESS )
            return r;
        break;
669 670 671
    case EXPR_IVAL:
        *valid = 1;
        cond->type = EXPR_UVAL;
672
        cond->u.uval = cond->u.ival;
673
        break;
674 675 676
    case EXPR_WILDCARD:
        *valid = 1;
        break;
677
    case EXPR_SVAL:
678
        *valid = 1;
679 680 681 682 683
        break;
    default:
        ERR("Invalid expression type\n");
        *valid = 0;
        break;
684
    }
685 686 687 688

    return ERROR_SUCCESS;
}

689 690
UINT WHERE_CreateView( MSIDATABASE *db, MSIVIEW **view, MSIVIEW *table,
                       struct expr *cond )
691
{
692 693
    MSIWHEREVIEW *wv = NULL;
    UINT count = 0, r, valid = 0;
694

695
    TRACE("%p\n", table );
696

697
    r = table->ops->get_dimensions( table, NULL, &count );
698
    if( r != ERROR_SUCCESS )
699 700 701 702
    {
        ERR("can't get table dimensions\n");
        return r;
    }
703

704
    if( cond )
705
    {
706 707 708 709 710
        r = WHERE_VerifyCondition( db, table, cond, &valid );
        if( r != ERROR_SUCCESS )
            return r;
        if( !valid )
            return ERROR_FUNCTION_FAILED;
711 712
    }

713
    wv = msi_alloc_zero( sizeof *wv );
714 715 716 717 718
    if( !wv )
        return ERROR_FUNCTION_FAILED;
    
    /* fill the structure */
    wv->view.ops = &where_ops;
719
    msiobj_addref( &db->hdr );
720 721 722 723
    wv->db = db;
    wv->table = table;
    wv->row_count = 0;
    wv->reorder = NULL;
724
    wv->cond = cond;
725
    wv->rec_index = 0;
726
    *view = (MSIVIEW*) wv;
727 728 729

    return ERROR_SUCCESS;
}