where.c 31.3 KB
Newer Older
1 2 3
/*
 * Implementation of the Microsoft Installer (msi.dll)
 *
4
 * Copyright 2002 Mike McCormack for CodeWeavers
5
 * Copyright 2011 Bernhard Loos
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 21
 */

22
#include <stdarg.h>
23
#include <assert.h>
24

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

#include "query.h"

39
WINE_DEFAULT_DEBUG_CHANNEL(msidb);
40

41
/* below is the query interface to a table */
42
typedef struct tagMSIROWENTRY
43
{
44
    struct tagMSIWHEREVIEW *wv; /* used during sorting */
45
    UINT values[1];
46
} MSIROWENTRY;
47

48 49 50 51 52 53 54 55
typedef struct tagJOINTABLE
{
    struct tagJOINTABLE *next;
    MSIVIEW *view;
    UINT col_count;
    UINT row_count;
    UINT table_index;
} JOINTABLE;
56

57 58 59 60 61 62 63
typedef struct tagMSIORDERINFO
{
    UINT col_count;
    UINT error;
    union ext_column columns[1];
} MSIORDERINFO;

64 65 66 67
typedef struct tagMSIWHEREVIEW
{
    MSIVIEW        view;
    MSIDATABASE   *db;
68
    JOINTABLE     *tables;
69
    UINT           row_count;
70 71
    UINT           col_count;
    UINT           table_count;
72 73
    MSIROWENTRY  **reorder;
    UINT           reorder_size; /* number of entries available in reorder */
74
    struct expr   *cond;
75
    UINT           rec_index;
76
    MSIORDERINFO  *order_info;
77 78
} MSIWHEREVIEW;

79 80 81
static UINT WHERE_evaluate( MSIWHEREVIEW *wv, const UINT rows[],
                            struct expr *cond, INT *val, MSIRECORD *record );

82
#define INITIAL_REORDER_SIZE 16
83

84 85
#define INVALID_ROW_INDEX (-1)

86
static void free_reorder(MSIWHEREVIEW *wv)
87
{
88
    UINT i;
89

90 91 92 93 94 95 96 97 98 99 100
    if (!wv->reorder)
        return;

    for (i = 0; i < wv->row_count; i++)
        msi_free(wv->reorder[i]);

    msi_free( wv->reorder );
    wv->reorder = NULL;
    wv->reorder_size = 0;
    wv->row_count = 0;
}
101

102 103 104 105 106 107 108 109 110 111 112 113 114 115
static UINT init_reorder(MSIWHEREVIEW *wv)
{
    MSIROWENTRY **new = msi_alloc_zero(sizeof(MSIROWENTRY *) * INITIAL_REORDER_SIZE);
    if (!new)
        return ERROR_OUTOFMEMORY;

    free_reorder(wv);

    wv->reorder = new;
    wv->reorder_size = INITIAL_REORDER_SIZE;

    return ERROR_SUCCESS;
}

116
static inline UINT find_row(MSIWHEREVIEW *wv, UINT row, UINT *(values[]))
117 118 119
{
    if (row >= wv->row_count)
        return ERROR_NO_MORE_ITEMS;
120

121
    *values = wv->reorder[row]->values;
122 123 124 125

    return ERROR_SUCCESS;
}

126
static UINT add_row(MSIWHEREVIEW *wv, UINT vals[])
127
{
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
    MSIROWENTRY *new;

    if (wv->reorder_size <= wv->row_count)
    {
        MSIROWENTRY **new_reorder;
        UINT newsize = wv->reorder_size * 2;

        new_reorder = msi_realloc_zero(wv->reorder, sizeof(MSIROWENTRY *) * newsize);
        if (!new_reorder)
            return ERROR_OUTOFMEMORY;

        wv->reorder = new_reorder;
        wv->reorder_size = newsize;
    }

143
    new = msi_alloc(FIELD_OFFSET( MSIROWENTRY, values[wv->table_count] ));
144 145 146 147

    if (!new)
        return ERROR_OUTOFMEMORY;

148
    wv->reorder[wv->row_count++] = new;
149

150
    memcpy(new->values, vals, wv->table_count * sizeof(UINT));
151
    new->wv = wv;
152 153 154 155

    return ERROR_SUCCESS;
}

156
static JOINTABLE *find_table(MSIWHEREVIEW *wv, UINT col, UINT *table_col)
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
{
    JOINTABLE *table = wv->tables;

    if(col == 0 || col > wv->col_count)
         return NULL;

    while (col > table->col_count)
    {
        col -= table->col_count;
        table = table->next;
        assert(table);
    }

    *table_col = col;
    return table;
}

static UINT parse_column(MSIWHEREVIEW *wv, union ext_column *column,
                         UINT *column_type)
{
    JOINTABLE *table = wv->tables;
    UINT i, r;

    do
    {
        LPCWSTR table_name;

        if (column->unparsed.table)
        {
            r = table->view->ops->get_column_info(table->view, 1, NULL, NULL,
                                                  NULL, &table_name);
            if (r != ERROR_SUCCESS)
                return r;
            if (strcmpW(table_name, column->unparsed.table) != 0)
                continue;
        }

        for(i = 1; i <= table->col_count; i++)
        {
            LPCWSTR col_name;

            r = table->view->ops->get_column_info(table->view, i, &col_name, column_type,
                                                  NULL, NULL);
            if(r != ERROR_SUCCESS )
                return r;

            if(strcmpW(col_name, column->unparsed.column))
                continue;
            column->parsed.column = i;
            column->parsed.table = table;
            return ERROR_SUCCESS;
        }
    }
    while ((table = table->next));

    WARN("Couldn't find column %s.%s\n", debugstr_w( column->unparsed.table ), debugstr_w( column->unparsed.column ) );
    return ERROR_BAD_QUERY_SYNTAX;
}

216 217 218
static UINT WHERE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
{
    MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
219 220
    JOINTABLE *table;
    UINT *rows;
221
    UINT r;
222 223 224

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

225
    if( !wv->tables )
226 227
        return ERROR_FUNCTION_FAILED;

228
    r = find_row(wv, row, &rows);
229 230
    if (r != ERROR_SUCCESS)
        return r;
231

232 233 234 235 236
    table = find_table(wv, col, &col);
    if (!table)
        return ERROR_FUNCTION_FAILED;

    return table->view->ops->fetch_int(table->view, rows[table->table_index], col, val);
237 238
}

239 240 241
static UINT WHERE_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm )
{
    MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
242 243
    JOINTABLE *table;
    UINT *rows;
244
    UINT r;
245 246 247

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

248
    if( !wv->tables )
249 250
        return ERROR_FUNCTION_FAILED;

251
    r = find_row(wv, row, &rows);
252 253
    if (r != ERROR_SUCCESS)
        return r;
254

255 256 257 258 259
    table = find_table(wv, col, &col);
    if (!table)
        return ERROR_FUNCTION_FAILED;

    return table->view->ops->fetch_stream( table->view, rows[table->table_index], col, stm );
260 261
}

262 263 264 265 266 267
static UINT WHERE_get_row( struct tagMSIVIEW *view, UINT row, MSIRECORD **rec )
{
    MSIWHEREVIEW *wv = (MSIWHEREVIEW *)view;

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

268
    if (!wv->tables)
269 270
        return ERROR_FUNCTION_FAILED;

271
    return msi_view_get_row( wv->db, view, row, rec );
272 273
}

274
static UINT WHERE_set_row( struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask )
275 276
{
    MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
277
    UINT i, r, offset = 0;
278 279
    JOINTABLE *table = wv->tables;
    UINT *rows;
280
    UINT mask_copy = mask;
281

282
    TRACE("%p %d %p %08x\n", wv, row, rec, mask );
283

284
    if( !wv->tables )
285
         return ERROR_FUNCTION_FAILED;
286

287
    r = find_row(wv, row, &rows);
288 289
    if (r != ERROR_SUCCESS)
        return r;
290

291
    if (mask >= 1 << wv->col_count)
292 293
        return ERROR_INVALID_PARAMETER;

294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
    do
    {
        for (i = 0; i < table->col_count; i++) {
            UINT type;

            if (!(mask_copy & (1 << i)))
                continue;
            r = table->view->ops->get_column_info(table->view, i + 1, NULL,
                                            &type, NULL, NULL );
            if (r != ERROR_SUCCESS)
                return r;
            if (type & MSITYPE_KEY)
                return ERROR_FUNCTION_FAILED;
        }
        mask_copy >>= table->col_count;
    }
    while (mask_copy && (table = table->next));

    table = wv->tables;

314 315 316 317 318
    do
    {
        const UINT col_count = table->col_count;
        UINT i;
        MSIRECORD *reduced;
319 320 321 322 323 324 325
        UINT reduced_mask = (mask >> offset) & ((1 << col_count) - 1);

        if (!reduced_mask)
        {
            offset += col_count;
            continue;
        }
326 327 328 329 330

        reduced = MSI_CreateRecord(col_count);
        if (!reduced)
            return ERROR_FUNCTION_FAILED;

331
        for (i = 1; i <= col_count; i++)
332
        {
333
            r = MSI_RecordCopyField(rec, i + offset, reduced, i);
334 335 336 337 338 339 340 341 342 343 344 345 346
            if (r != ERROR_SUCCESS)
                break;
        }

        offset += col_count;

        if (r == ERROR_SUCCESS)
            r = table->view->ops->set_row(table->view, rows[table->table_index], reduced, reduced_mask);

        msiobj_release(&reduced->hdr);
    }
    while ((table = table->next));
    return r;
347 348
}

349 350 351 352
static UINT WHERE_delete_row(struct tagMSIVIEW *view, UINT row)
{
    MSIWHEREVIEW *wv = (MSIWHEREVIEW *)view;
    UINT r;
353
    UINT *rows;
354 355 356

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

357
    if (!wv->tables)
358 359
        return ERROR_FUNCTION_FAILED;

360
    r = find_row(wv, row, &rows);
361 362 363
    if ( r != ERROR_SUCCESS )
        return r;

364 365 366 367
    if (wv->table_count > 1)
        return ERROR_CALL_NOT_IMPLEMENTED;

    return wv->tables->view->ops->delete_row(wv->tables->view, rows[0]);
368 369
}

370 371
static INT INT_evaluate_binary( MSIWHEREVIEW *wv, const UINT rows[],
                                const struct complex_expr *expr, INT *val, MSIRECORD *record )
372
{
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
    UINT rl, rr;
    INT lval, rval;

    rl = WHERE_evaluate(wv, rows, expr->left, &lval, record);
    if (rl != ERROR_SUCCESS && rl != ERROR_CONTINUE)
        return rl;
    rr = WHERE_evaluate(wv, rows, expr->right, &rval, record);
    if (rr != ERROR_SUCCESS && rr != ERROR_CONTINUE)
        return rr;

    if (rl == ERROR_CONTINUE || rr == ERROR_CONTINUE)
    {
        if (rl == rr)
        {
            *val = TRUE;
            return ERROR_CONTINUE;
        }

        if (expr->op == OP_AND)
        {
            if ((rl == ERROR_CONTINUE && !rval) || (rr == ERROR_CONTINUE && !lval))
            {
                *val = FALSE;
                return ERROR_SUCCESS;
            }
        }
        else if (expr->op == OP_OR)
        {
            if ((rl == ERROR_CONTINUE && rval) || (rr == ERROR_CONTINUE && lval))
            {
                *val = TRUE;
                return ERROR_SUCCESS;
            }
        }

        *val = TRUE;
        return ERROR_CONTINUE;
    }

    switch( expr->op )
413 414
    {
    case OP_EQ:
415 416
        *val = ( lval == rval );
        break;
417
    case OP_AND:
418 419
        *val = ( lval && rval );
        break;
420
    case OP_OR:
421 422
        *val = ( lval || rval );
        break;
423
    case OP_GT:
424 425
        *val = ( lval > rval );
        break;
426
    case OP_LT:
427 428
        *val = ( lval < rval );
        break;
429
    case OP_LE:
430 431
        *val = ( lval <= rval );
        break;
432
    case OP_GE:
433 434
        *val = ( lval >= rval );
        break;
435
    case OP_NE:
436 437
        *val = ( lval != rval );
        break;
438
    default:
439 440
        ERR("Unknown operator %d\n", expr->op );
        return ERROR_FUNCTION_FAILED;
441
    }
442 443

    return ERROR_SUCCESS;
444 445
}

446 447 448 449
static inline UINT expr_fetch_value(const union ext_column *expr, const UINT rows[], UINT *val)
{
    JOINTABLE *table = expr->parsed.table;

450 451 452 453 454
    if( rows[table->table_index] == INVALID_ROW_INDEX )
    {
        *val = 1;
        return ERROR_CONTINUE;
    }
455 456 457 458 459
    return table->view->ops->fetch_int(table->view, rows[table->table_index],
                                        expr->parsed.column, val);
}


460 461
static UINT INT_evaluate_unary( MSIWHEREVIEW *wv, const UINT rows[],
                                const struct complex_expr *expr, INT *val, MSIRECORD *record )
462
{
463 464 465 466 467 468 469 470
    UINT r;
    UINT lval;

    r = expr_fetch_value(&expr->left->u.column, rows, &lval);
    if(r != ERROR_SUCCESS)
        return r;

    switch( expr->op )
471
    {
472
    case OP_ISNULL:
473 474
        *val = !lval;
        break;
475
    case OP_NOTNULL:
476 477
        *val = lval;
        break;
478
    default:
479 480
        ERR("Unknown operator %d\n", expr->op );
        return ERROR_FUNCTION_FAILED;
481
    }
482
    return ERROR_SUCCESS;
483 484
}

485
static UINT STRING_evaluate( MSIWHEREVIEW *wv, const UINT rows[],
486
                                     const struct expr *expr,
487 488
                                     const MSIRECORD *record,
                                     const WCHAR **str )
489
{
490
    UINT val = 0, r = ERROR_SUCCESS;
491 492 493

    switch( expr->type )
    {
494
    case EXPR_COL_NUMBER_STRING:
495
        r = expr_fetch_value(&expr->u.column, rows, &val);
496
        if (r == ERROR_SUCCESS)
497
            *str =  msi_string_lookup(wv->db->strings, val, NULL);
498 499 500
        else
            *str = NULL;
        break;
501

502
    case EXPR_SVAL:
503 504
        *str = expr->u.sval;
        break;
505 506

    case EXPR_WILDCARD:
507 508
        *str = MSI_RecordGetString(record, ++wv->rec_index);
        break;
509 510 511

    default:
        ERR("Invalid expression type\n");
512 513
        r = ERROR_FUNCTION_FAILED;
        *str = NULL;
514 515
        break;
    }
516
    return r;
517 518
}

519
static UINT STRCMP_Evaluate( MSIWHEREVIEW *wv, const UINT rows[], const struct complex_expr *expr,
520
                             INT *val, const MSIRECORD *record )
521 522
{
    int sr;
523
    const WCHAR *l_str, *r_str;
524 525 526 527 528 529 530 531 532
    UINT r;

    *val = TRUE;
    r = STRING_evaluate(wv, rows, expr->left, record, &l_str);
    if (r == ERROR_CONTINUE)
        return r;
    r = STRING_evaluate(wv, rows, expr->right, record, &r_str);
    if (r == ERROR_CONTINUE)
        return r;
533

534 535
    if( l_str == r_str ||
        ((!l_str || !*l_str) && (!r_str || !*r_str)) )
536 537 538 539 540 541
        sr = 0;
    else if( l_str && ! r_str )
        sr = 1;
    else if( r_str && ! l_str )
        sr = -1;
    else
542
        sr = strcmpW( l_str, r_str );
543

544 545
    *val = ( expr->op == OP_EQ && ( sr == 0 ) ) ||
           ( expr->op == OP_NE && ( sr != 0 ) );
546 547 548 549

    return ERROR_SUCCESS;
}

550
static UINT WHERE_evaluate( MSIWHEREVIEW *wv, const UINT rows[],
551
                            struct expr *cond, INT *val, MSIRECORD *record )
552
{
553
    UINT r, tval;
554 555

    if( !cond )
556 557
    {
        *val = TRUE;
558
        return ERROR_SUCCESS;
559
    }
560 561 562 563

    switch( cond->type )
    {
    case EXPR_COL_NUMBER:
564
        r = expr_fetch_value(&cond->u.column, rows, &tval);
565 566
        if( r != ERROR_SUCCESS )
            return r;
567 568 569 570
        *val = tval - 0x8000;
        return ERROR_SUCCESS;

    case EXPR_COL_NUMBER32:
571
        r = expr_fetch_value(&cond->u.column, rows, &tval);
572 573
        if( r != ERROR_SUCCESS )
            return r;
574
        *val = tval - 0x80000000;
575
        return r;
576 577 578 579 580 581

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

    case EXPR_COMPLEX:
582
        return INT_evaluate_binary(wv, rows, &cond->u.expr, val, record);
583 584

    case EXPR_UNARY:
585
        return INT_evaluate_unary( wv, rows, &cond->u.expr, val, record );
586

587
    case EXPR_STRCMP:
588
        return STRCMP_Evaluate( wv, rows, &cond->u.expr, val, record );
589 590

    case EXPR_WILDCARD:
591
        *val = MSI_RecordGetInteger( record, ++wv->rec_index );
592
        return ERROR_SUCCESS;
593

594 595
    default:
        ERR("Invalid expression type\n");
596
        return ERROR_FUNCTION_FAILED;
597
    }
598 599 600 601

    return ERROR_SUCCESS;
}

602
static UINT check_condition( MSIWHEREVIEW *wv, MSIRECORD *record, JOINTABLE **tables,
603 604 605 606 607
                             UINT table_rows[] )
{
    UINT r = ERROR_FUNCTION_FAILED;
    INT val;

608 609 610
    for (table_rows[(*tables)->table_index] = 0;
         table_rows[(*tables)->table_index] < (*tables)->row_count;
         table_rows[(*tables)->table_index]++)
611
    {
612 613 614 615 616 617
        val = 0;
        wv->rec_index = 0;
        r = WHERE_evaluate( wv, table_rows, wv->cond, &val, record );
        if (r != ERROR_SUCCESS && r != ERROR_CONTINUE)
            break;
        if (val)
618
        {
619
            if (*(tables + 1))
620
            {
621
                r = check_condition(wv, record, tables + 1, table_rows);
622 623 624 625 626 627 628 629 630
                if (r != ERROR_SUCCESS)
                    break;
            }
            else
            {
                if (r != ERROR_SUCCESS)
                    break;
                add_row (wv, table_rows);
            }
631 632
        }
    }
633
    table_rows[(*tables)->table_index] = INVALID_ROW_INDEX;
634 635 636
    return r;
}

637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
static int compare_entry( const void *left, const void *right )
{
    const MSIROWENTRY *le = *(const MSIROWENTRY**)left;
    const MSIROWENTRY *re = *(const MSIROWENTRY**)right;
    const MSIWHEREVIEW *wv = le->wv;
    MSIORDERINFO *order = wv->order_info;
    UINT i, j, r, l_val, r_val;

    assert(le->wv == re->wv);

    if (order)
    {
        for (i = 0; i < order->col_count; i++)
        {
            const union ext_column *column = &order->columns[i];

            r = column->parsed.table->view->ops->fetch_int(column->parsed.table->view,
                          le->values[column->parsed.table->table_index],
                          column->parsed.column, &l_val);
            if (r != ERROR_SUCCESS)
            {
                order->error = r;
                return 0;
            }

            r = column->parsed.table->view->ops->fetch_int(column->parsed.table->view,
                          re->values[column->parsed.table->table_index],
                          column->parsed.column, &r_val);
            if (r != ERROR_SUCCESS)
            {
                order->error = r;
                return 0;
            }

            if (l_val != r_val)
                return l_val < r_val ? -1 : 1;
        }
    }

    for (j = 0; j < wv->table_count; j++)
    {
        if (le->values[j] != re->values[j])
            return le->values[j] < re->values[j] ? -1 : 1;
    }
    return 0;
}

684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
static void add_to_array( JOINTABLE **array, JOINTABLE *elem )
{
    while (*array && *array != elem)
        array++;
    if (!*array)
        *array = elem;
}

static BOOL in_array( JOINTABLE **array, JOINTABLE *elem )
{
    while (*array && *array != elem)
        array++;
    return *array != NULL;
}

#define CONST_EXPR 1 /* comparison to a constant value */
#define JOIN_TO_CONST_EXPR 0x10000 /* comparison to a table involved with
                                      a CONST_EXPR comaprison */

static UINT reorder_check( const struct expr *expr, JOINTABLE **ordered_tables,
                           BOOL process_joins, JOINTABLE **lastused )
{
    UINT res = 0;

    switch (expr->type)
    {
        case EXPR_WILDCARD:
        case EXPR_SVAL:
        case EXPR_UVAL:
            return 0;
        case EXPR_COL_NUMBER:
        case EXPR_COL_NUMBER32:
        case EXPR_COL_NUMBER_STRING:
            if (in_array(ordered_tables, expr->u.column.parsed.table))
                return JOIN_TO_CONST_EXPR;
            *lastused = expr->u.column.parsed.table;
            return CONST_EXPR;
        case EXPR_STRCMP:
        case EXPR_COMPLEX:
            res = reorder_check(expr->u.expr.right, ordered_tables, process_joins, lastused);
            /* fall through */
        case EXPR_UNARY:
            res += reorder_check(expr->u.expr.left, ordered_tables, process_joins, lastused);
            if (res == 0)
                return 0;
            if (res == CONST_EXPR)
                add_to_array(ordered_tables, *lastused);
            if (process_joins && res == JOIN_TO_CONST_EXPR + CONST_EXPR)
                add_to_array(ordered_tables, *lastused);
            return res;
        default:
735
            ERR("Unknown expr type: %i\n", expr->type);
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
            assert(0);
            return 0x1000000;
    }
}

/* reorders the tablelist in a way to evaluate the condition as fast as possible */
static JOINTABLE **ordertables( MSIWHEREVIEW *wv )
{
    JOINTABLE *table;
    JOINTABLE **tables;

    tables = msi_alloc_zero( (wv->table_count + 1) * sizeof(*tables) );

    if (wv->cond)
    {
        table = NULL;
        reorder_check(wv->cond, tables, FALSE, &table);
        table = NULL;
        reorder_check(wv->cond, tables, TRUE, &table);
    }

    table = wv->tables;
    while (table)
    {
        add_to_array(tables, table);
        table = table->next;
    }
    return tables;
}

766
static UINT WHERE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
767 768
{
    MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
769 770
    UINT r;
    JOINTABLE *table = wv->tables;
771
    UINT *rows;
772
    JOINTABLE **ordered_tables;
773
    UINT i = 0;
774

775
    TRACE("%p %p\n", wv, record);
776 777 778 779

    if( !table )
         return ERROR_FUNCTION_FAILED;

780
    r = init_reorder(wv);
781
    if (r != ERROR_SUCCESS)
782
        return r;
783

784
    do
785
    {
786
        table->view->ops->execute(table->view, NULL);
787

788 789
        r = table->view->ops->get_dimensions(table->view, &table->row_count, NULL);
        if (r != ERROR_SUCCESS)
790
        {
791 792
            ERR("failed to get table dimensions\n");
            return r;
793 794
        }

795 796 797
        /* each table must have at least one row */
        if (table->row_count == 0)
            return ERROR_SUCCESS;
798
    }
799
    while ((table = table->next));
800

801 802
    ordered_tables = ordertables( wv );

803
    rows = msi_alloc( wv->table_count * sizeof(*rows) );
804 805 806
    for (i = 0; i < wv->table_count; i++)
        rows[i] = INVALID_ROW_INDEX;

807
    r =  check_condition(wv, record, ordered_tables, rows);
808 809 810 811 812 813 814 815

    if (wv->order_info)
        wv->order_info->error = ERROR_SUCCESS;

    qsort(wv->reorder, wv->row_count, sizeof(MSIROWENTRY *), compare_entry);

    if (wv->order_info)
        r = wv->order_info->error;
816

817
    msi_free( rows );
818
    msi_free( ordered_tables );
819
    return r;
820 821 822 823 824
}

static UINT WHERE_close( struct tagMSIVIEW *view )
{
    MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
825
    JOINTABLE *table = wv->tables;
826 827 828

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

829
    if (!table)
830
        return ERROR_FUNCTION_FAILED;
831

832 833 834 835 836
    do
        table->view->ops->close(table->view);
    while ((table = table->next));

    return ERROR_SUCCESS;
837 838 839 840 841 842 843 844
}

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

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

845
    if(!wv->tables)
846 847
         return ERROR_FUNCTION_FAILED;

848
    if (rows)
849
    {
850
        if (!wv->reorder)
851 852 853 854
            return ERROR_FUNCTION_FAILED;
        *rows = wv->row_count;
    }

855 856 857 858
    if (cols)
        *cols = wv->col_count;

    return ERROR_SUCCESS;
859 860
}

861 862
static UINT WHERE_get_column_info( struct tagMSIVIEW *view, UINT n, LPCWSTR *name,
                                   UINT *type, BOOL *temporary, LPCWSTR *table_name )
863 864
{
    MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
865
    JOINTABLE *table;
866

867
    TRACE("%p %d %p %p %p %p\n", wv, n, name, type, temporary, table_name );
868

869
    if(!wv->tables)
870 871
         return ERROR_FUNCTION_FAILED;

872 873 874 875 876 877 878 879 880 881 882 883 884 885
    table = find_table(wv, n, &n);
    if (!table)
        return ERROR_FUNCTION_FAILED;

    return table->view->ops->get_column_info(table->view, n, name,
                                            type, temporary, table_name);
}

static UINT join_find_row( MSIWHEREVIEW *wv, MSIRECORD *rec, UINT *row )
{
    LPCWSTR str;
    UINT r, i, id, data;

    str = MSI_RecordGetString( rec, 1 );
886
    r = msi_string2id( wv->db->strings, str, -1, &id );
887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
    if (r != ERROR_SUCCESS)
        return r;

    for (i = 0; i < wv->row_count; i++)
    {
        WHERE_fetch_int( &wv->view, i, 1, &data );

        if (data == id)
        {
            *row = i;
            return ERROR_SUCCESS;
        }
    }

    return ERROR_FUNCTION_FAILED;
}

static UINT join_modify_update( struct tagMSIVIEW *view, MSIRECORD *rec )
{
    MSIWHEREVIEW *wv = (MSIWHEREVIEW *)view;
907 908 909
    UINT r, row, i, mask = 0;
    MSIRECORD *current;

910 911 912 913 914

    r = join_find_row( wv, rec, &row );
    if (r != ERROR_SUCCESS)
        return r;

915 916 917 918 919 920 921 922 923 924 925 926 927 928
    r = msi_view_get_row( wv->db, view, row, &current );
    if (r != ERROR_SUCCESS)
        return r;

    assert(MSI_RecordGetFieldCount(rec) == MSI_RecordGetFieldCount(current));

    for (i = MSI_RecordGetFieldCount(rec); i > 0; i--)
    {
        if (!MSI_RecordsAreFieldsEqual(rec, current, i))
            mask |= 1 << (i - 1);
    }
     msiobj_release(&current->hdr);

    return WHERE_set_row( view, row, rec, mask );
929 930
}

931
static UINT WHERE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
932
                          MSIRECORD *rec, UINT row )
933 934
{
    MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
935 936
    JOINTABLE *table = wv->tables;
    UINT r;
937

938
    TRACE("%p %d %p\n", wv, eModifyMode, rec);
939

940 941 942 943 944 945 946 947 948 949 950
    if (!table)
        return ERROR_FUNCTION_FAILED;

    if (!table->next)
    {
        UINT *rows;

        if (find_row(wv, row - 1, &rows) == ERROR_SUCCESS)
            row = rows[0] + 1;
        else
            row = -1;
951

952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
        return table->view->ops->modify(table->view, eModifyMode, rec, row);
    }

    switch (eModifyMode)
    {
    case MSIMODIFY_UPDATE:
        return join_modify_update( view, rec );

    case MSIMODIFY_ASSIGN:
    case MSIMODIFY_DELETE:
    case MSIMODIFY_INSERT:
    case MSIMODIFY_INSERT_TEMPORARY:
    case MSIMODIFY_MERGE:
    case MSIMODIFY_REPLACE:
    case MSIMODIFY_SEEK:
    case MSIMODIFY_VALIDATE:
    case MSIMODIFY_VALIDATE_DELETE:
    case MSIMODIFY_VALIDATE_FIELD:
    case MSIMODIFY_VALIDATE_NEW:
        r = ERROR_FUNCTION_FAILED;
        break;

    case MSIMODIFY_REFRESH:
        r = ERROR_CALL_NOT_IMPLEMENTED;
        break;

    default:
        WARN("%p %d %p %u - unknown mode\n", view, eModifyMode, rec, row );
        r = ERROR_INVALID_PARAMETER;
        break;
    }

    return r;
985 986 987 988 989
}

static UINT WHERE_delete( struct tagMSIVIEW *view )
{
    MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
990
    JOINTABLE *table = wv->tables;
991 992 993

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

994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
    while(table)
    {
        JOINTABLE *next;

        table->view->ops->delete(table->view);
        table->view = NULL;
        next = table->next;
        msi_free(table);
        table = next;
    }
    wv->tables = NULL;
    wv->table_count = 0;
1006

1007
    free_reorder(wv);
1008

1009 1010
    msi_free(wv->order_info);
    wv->order_info = NULL;
1011

1012
    msiobj_release( &wv->db->hdr );
1013
    msi_free( wv );
1014 1015 1016 1017

    return ERROR_SUCCESS;
}

1018 1019 1020 1021
static UINT WHERE_find_matching_rows( struct tagMSIVIEW *view, UINT col,
    UINT val, UINT *row, MSIITERHANDLE *handle )
{
    MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
1022
    UINT i, row_value;
1023 1024 1025

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

1026
    if (!wv->tables)
1027 1028
         return ERROR_FUNCTION_FAILED;

1029 1030
    if (col == 0 || col > wv->col_count)
        return ERROR_INVALID_PARAMETER;
1031

1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
    for (i = PtrToUlong(*handle); i < wv->row_count; i++)
    {
        if (view->ops->fetch_int( view, i, col, &row_value ) != ERROR_SUCCESS)
            continue;

        if (row_value == val)
        {
            *row = i;
            *handle = UlongToPtr(i + 1);
            return ERROR_SUCCESS;
        }
    }

    return ERROR_NO_MORE_ITEMS;
1046 1047
}

1048 1049 1050
static UINT WHERE_sort(struct tagMSIVIEW *view, column_info *columns)
{
    MSIWHEREVIEW *wv = (MSIWHEREVIEW *)view;
1051
    JOINTABLE *table = wv->tables;
1052 1053 1054
    column_info *column = columns;
    MSIORDERINFO *orderinfo;
    UINT r, count = 0;
1055
    UINT i;
1056 1057 1058

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

1059 1060 1061
    if (!table)
        return ERROR_FUNCTION_FAILED;

1062
    while (column)
1063
    {
1064 1065 1066 1067 1068 1069 1070
        count++;
        column = column->next;
    }

    if (count == 0)
        return ERROR_SUCCESS;

1071
    orderinfo = msi_alloc(FIELD_OFFSET(MSIORDERINFO, columns[count]));
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
    if (!orderinfo)
        return ERROR_OUTOFMEMORY;

    orderinfo->col_count = count;

    column = columns;

    for (i = 0; i < count; i++)
    {
        orderinfo->columns[i].unparsed.column = column->column;
        orderinfo->columns[i].unparsed.table = column->table;

        r = parse_column(wv, &orderinfo->columns[i], NULL);
1085
        if (r != ERROR_SUCCESS)
1086
            goto error;
1087
    }
1088 1089

    wv->order_info = orderinfo;
1090 1091

    return ERROR_SUCCESS;
1092 1093 1094
error:
    msi_free(orderinfo);
    return r;
1095
}
1096

1097
static const MSIVIEWOPS where_ops =
1098 1099
{
    WHERE_fetch_int,
1100
    WHERE_fetch_stream,
1101
    WHERE_get_row,
1102
    WHERE_set_row,
1103
    NULL,
1104
    WHERE_delete_row,
1105 1106 1107 1108 1109
    WHERE_execute,
    WHERE_close,
    WHERE_get_dimensions,
    WHERE_get_column_info,
    WHERE_modify,
1110
    WHERE_delete,
1111 1112 1113
    WHERE_find_matching_rows,
    NULL,
    NULL,
1114
    NULL,
1115
    NULL,
1116
    WHERE_sort,
1117
    NULL,
1118 1119
};

1120
static UINT WHERE_VerifyCondition( MSIWHEREVIEW *wv, struct expr *cond,
1121 1122
                                   UINT *valid )
{
1123
    UINT r;
1124 1125 1126 1127

    switch( cond->type )
    {
    case EXPR_COLUMN:
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
    {
        UINT type;

        *valid = FALSE;

        r = parse_column(wv, &cond->u.column, &type);
        if (r != ERROR_SUCCESS)
            break;

        if (type&MSITYPE_STRING)
            cond->type = EXPR_COL_NUMBER_STRING;
        else if ((type&0xff) == 4)
            cond->type = EXPR_COL_NUMBER32;
1141
        else
1142 1143 1144
            cond->type = EXPR_COL_NUMBER;

        *valid = TRUE;
1145
        break;
1146
    }
1147
    case EXPR_COMPLEX:
1148
        r = WHERE_VerifyCondition( wv, cond->u.expr.left, valid );
1149 1150 1151 1152
        if( r != ERROR_SUCCESS )
            return r;
        if( !*valid )
            return ERROR_SUCCESS;
1153
        r = WHERE_VerifyCondition( wv, cond->u.expr.right, valid );
1154 1155
        if( r != ERROR_SUCCESS )
            return r;
1156 1157

        /* check the type of the comparison */
1158
        if( ( cond->u.expr.left->type == EXPR_SVAL ) ||
1159 1160 1161
            ( 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 ) )
1162 1163 1164 1165
        {
            switch( cond->u.expr.op )
            {
            case OP_EQ:
1166
            case OP_NE:
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
                break;
            default:
                *valid = FALSE;
                return ERROR_INVALID_PARAMETER;
            }

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

            cond->type = EXPR_STRCMP;
        }

1178
        break;
1179 1180 1181 1182 1183 1184
    case EXPR_UNARY:
        if ( cond->u.expr.left->type != EXPR_COLUMN )
        {
            *valid = FALSE;
            return ERROR_INVALID_PARAMETER;
        }
1185
        r = WHERE_VerifyCondition( wv, cond->u.expr.left, valid );
1186 1187 1188
        if( r != ERROR_SUCCESS )
            return r;
        break;
1189 1190 1191
    case EXPR_IVAL:
        *valid = 1;
        cond->type = EXPR_UVAL;
1192
        cond->u.uval = cond->u.ival;
1193
        break;
1194 1195 1196
    case EXPR_WILDCARD:
        *valid = 1;
        break;
1197
    case EXPR_SVAL:
1198
        *valid = 1;
1199 1200 1201 1202 1203
        break;
    default:
        ERR("Invalid expression type\n");
        *valid = 0;
        break;
1204
    }
1205 1206 1207 1208

    return ERROR_SUCCESS;
}

1209
UINT WHERE_CreateView( MSIDATABASE *db, MSIVIEW **view, LPWSTR tables,
1210
                       struct expr *cond )
1211
{
1212
    MSIWHEREVIEW *wv = NULL;
1213 1214
    UINT r, valid = 0;
    WCHAR *ptr;
1215

1216
    TRACE("(%s)\n", debugstr_w(tables) );
1217

1218
    wv = msi_alloc_zero( sizeof *wv );
1219 1220 1221 1222 1223
    if( !wv )
        return ERROR_FUNCTION_FAILED;
    
    /* fill the structure */
    wv->view.ops = &where_ops;
1224
    msiobj_addref( &db->hdr );
1225
    wv->db = db;
1226
    wv->cond = cond;
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255

    while (*tables)
    {
        JOINTABLE *table;

        if ((ptr = strchrW(tables, ' ')))
            *ptr = '\0';

        table = msi_alloc(sizeof(JOINTABLE));
        if (!table)
        {
            r = ERROR_OUTOFMEMORY;
            goto end;
        }

        r = TABLE_CreateView(db, tables, &table->view);
        if (r != ERROR_SUCCESS)
        {
            WARN("can't create table: %s\n", debugstr_w(tables));
            msi_free(table);
            r = ERROR_BAD_QUERY_SYNTAX;
            goto end;
        }

        r = table->view->ops->get_dimensions(table->view, NULL,
                                             &table->col_count);
        if (r != ERROR_SUCCESS)
        {
            ERR("can't get table dimensions\n");
1256 1257
            table->view->ops->delete(table->view);
            msi_free(table);
1258 1259 1260 1261 1262 1263
            goto end;
        }

        wv->col_count += table->col_count;
        table->table_index = wv->table_count++;

1264 1265
        table->next = wv->tables;
        wv->tables = table;
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283

        if (!ptr)
            break;

        tables = ptr + 1;
    }

    if( cond )
    {
        r = WHERE_VerifyCondition( wv, cond, &valid );
        if( r != ERROR_SUCCESS )
            goto end;
        if( !valid ) {
            r = ERROR_FUNCTION_FAILED;
            goto end;
        }
    }

1284
    *view = (MSIVIEW*) wv;
1285 1286

    return ERROR_SUCCESS;
1287 1288 1289 1290
end:
    WHERE_delete(&wv->view);

    return r;
1291
}