sql.y 22.6 KB
Newer Older
1 2 3 4 5
%{

/*
 * Implementation of the Microsoft Installer (msi.dll)
 *
6
 * Copyright 2002-2004 Mike McCormack for CodeWeavers
7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * 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
20
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 22 23 24
 */


#include "config.h"
25 26

#include <stdarg.h>
27 28
#include <stdio.h>
#include <stdlib.h>
29 30 31

#include "windef.h"
#include "winbase.h"
32
#include "query.h"
33
#include "wine/list.h"
34
#include "wine/debug.h"
35
#include "wine/unicode.h"
36 37 38 39

#define YYLEX_PARAM info
#define YYPARSE_PARAM info

40
static int sql_error(const char *str);
41 42 43

WINE_DEFAULT_DEBUG_CHANNEL(msi);

44
typedef struct tag_SQL_input
45 46 47 48
{
    MSIDATABASE *db;
    LPCWSTR command;
    DWORD n, len;
49
    UINT r;
50 51 52 53
    MSIVIEW **view;  /* View structure for the resulting query.  This value
                      * tracks the view currently being created so we can free
                      * this view on syntax error.
                      */
54
    struct list *mem;
55
} SQL_input;
56

57
static UINT SQL_getstring( void *info, const struct sql_str *strdata, LPWSTR *str );
58
static INT SQL_getint( void *info );
59
static int sql_lex( void *SQL_lval, SQL_input *info );
60

61
static LPWSTR parser_add_table( void *info, LPCWSTR list, LPCWSTR table );
62
static void *parser_alloc( void *info, unsigned int sz );
63
static column_info *parser_alloc_column( void *info, LPCWSTR table, LPCWSTR column );
64

65
static BOOL SQL_MarkPrimaryKeys( column_info **cols, column_info *keys);
66 67

static struct expr * EXPR_complex( void *info, struct expr *l, UINT op, struct expr *r );
68
static struct expr * EXPR_unary( void *info, struct expr *l, UINT op );
69
static struct expr * EXPR_column( void *info, const column_info *column );
70
static struct expr * EXPR_ival( void *info, int val );
71
static struct expr * EXPR_sval( void *info, const struct sql_str *str );
72
static struct expr * EXPR_wildcard( void *info );
73

74 75 76 77
#define PARSER_BUBBLE_UP_VIEW( sql, result, current_view ) \
    *sql->view = current_view; \
    result = current_view

78 79 80 81 82 83
%}

%pure-parser

%union
{
84
    struct sql_str str;
85
    LPWSTR string;
86
    column_info *column_list;
87
    MSIVIEW *query;
88
    struct expr *expr;
89
    USHORT column_type;
90
    int integer;
91 92
}

93
%token TK_ALTER TK_AND TK_BY TK_CHAR TK_COMMA TK_CREATE TK_DELETE TK_DROP
94
%token TK_DISTINCT TK_DOT TK_EQ TK_FREE TK_FROM TK_GE TK_GT TK_HOLD TK_ADD
95 96
%token <str> TK_ID
%token TK_ILLEGAL TK_INSERT TK_INT
97
%token <str> TK_INTEGER
98 99 100 101
%token TK_INTO TK_IS TK_KEY TK_LE TK_LONG TK_LONGCHAR TK_LP TK_LT
%token TK_LOCALIZABLE TK_MINUS TK_NE TK_NOT TK_NULL
%token TK_OBJECT TK_OR TK_ORDER TK_PRIMARY TK_RP
%token TK_SELECT TK_SET TK_SHORT TK_SPACE TK_STAR
102
%token <str> TK_STRING
103
%token TK_TABLE TK_TEMPORARY TK_UPDATE TK_VALUES TK_WHERE TK_WILDCARD
104

105 106 107 108 109 110
/*
 * These are extra tokens used by the lexer but never seen by the
 * parser.  We put them in a rule so that the parser generator will
 * add them to the parse.h output file.
 *
 */
111 112 113
%nonassoc END_OF_FILE ILLEGAL SPACE UNCLOSED_STRING COMMENT FUNCTION
          COLUMN AGG_FUNCTION.

114 115
%type <string> table tablelist id string
%type <column_list> selcollist collist selcolumn column column_and_type column_def table_def
116
%type <column_list> column_assignment update_assign_list constlist
117
%type <query> query from selectfrom unorderdfrom
118
%type <query> oneupdate onedelete oneselect onequery onecreate oneinsert onealter onedrop
119
%type <expr> expr val column_val const_val
120
%type <column_type> column_type data_type data_type_l data_count
121
%type <integer> number alterop
122

123 124 125
%left TK_OR
%left TK_AND
%left TK_NOT
126
%left TK_EQ TK_NE TK_LT TK_GT TK_LE TK_GE TK_LIKE
127 128
%right TK_NEGATION

129 130
%%

131 132
query:
    onequery
133 134 135 136
    {
        SQL_input* sql = (SQL_input*) info;
        *sql->view = $1;
    }
137 138 139 140
    ;

onequery:
    oneselect
141
  | onecreate
142
  | oneinsert
143
  | oneupdate
144
  | onedelete
145
  | onealter
146
  | onedrop
147 148 149
    ;

oneinsert:
150
    TK_INSERT TK_INTO table TK_LP collist TK_RP TK_VALUES TK_LP constlist TK_RP
151 152
        {
            SQL_input *sql = (SQL_input*) info;
153
            MSIVIEW *insert = NULL;
154

155
            INSERT_CreateView( sql->db, &insert, $3, $5, $9, FALSE );
156 157
            if( !insert )
                YYABORT;
158 159

            PARSER_BUBBLE_UP_VIEW( sql, $$,  insert );
160
        }
161
  | TK_INSERT TK_INTO table TK_LP collist TK_RP TK_VALUES TK_LP constlist TK_RP TK_TEMPORARY
162 163
        {
            SQL_input *sql = (SQL_input*) info;
164
            MSIVIEW *insert = NULL;
165

166
            INSERT_CreateView( sql->db, &insert, $3, $5, $9, TRUE );
167 168
            if( !insert )
                YYABORT;
169 170

            PARSER_BUBBLE_UP_VIEW( sql, $$,  insert );
171
        }
172 173 174 175 176 177
    ;

onecreate:
    TK_CREATE TK_TABLE table TK_LP table_def TK_RP
        {
            SQL_input* sql = (SQL_input*) info;
178
            MSIVIEW *create = NULL;
179
            UINT r;
180 181 182

            if( !$5 )
                YYABORT;
183
            r = CREATE_CreateView( sql->db, &create, $3, $5, FALSE );
184
            if( !create )
185 186
            {
                sql->r = r;
187
                YYABORT;
188
            }
189 190

            PARSER_BUBBLE_UP_VIEW( sql, $$,  create );
191 192 193 194
        }
  | TK_CREATE TK_TABLE table TK_LP table_def TK_RP TK_HOLD
        {
            SQL_input* sql = (SQL_input*) info;
195
            MSIVIEW *create = NULL;
196 197 198 199

            if( !$5 )
                YYABORT;
            CREATE_CreateView( sql->db, &create, $3, $5, TRUE );
200 201
            if( !create )
                YYABORT;
202 203

            PARSER_BUBBLE_UP_VIEW( sql, $$,  create );
204 205 206
        }
    ;

207 208 209 210
oneupdate:
    TK_UPDATE table TK_SET update_assign_list TK_WHERE expr
        {
            SQL_input* sql = (SQL_input*) info;
211
            MSIVIEW *update = NULL;
212

213
            UPDATE_CreateView( sql->db, &update, $2, $4, $6 );
214 215
            if( !update )
                YYABORT;
216 217

            PARSER_BUBBLE_UP_VIEW( sql, $$,  update );
218
        }
219 220 221 222 223 224 225 226
  | TK_UPDATE table TK_SET update_assign_list
        {
            SQL_input* sql = (SQL_input*) info;
            MSIVIEW *update = NULL;

            UPDATE_CreateView( sql->db, &update, $2, $4, NULL );
            if( !update )
                YYABORT;
227 228

            PARSER_BUBBLE_UP_VIEW( sql, $$,  update );
229
        }
230 231
    ;

232 233 234 235
onedelete:
    TK_DELETE from
        {
            SQL_input* sql = (SQL_input*) info;
236
            MSIVIEW *delete = NULL;
237 238

            DELETE_CreateView( sql->db, &delete, $2 );
239 240
            if( !delete )
                YYABORT;
241 242

            PARSER_BUBBLE_UP_VIEW( sql, $$, delete );
243 244 245
        }
    ;

246 247 248 249 250 251
onealter:
    TK_ALTER TK_TABLE table alterop
        {
            SQL_input* sql = (SQL_input*) info;
            MSIVIEW *alter = NULL;

252
            ALTER_CreateView( sql->db, &alter, $3, NULL, $4 );
253 254
            if( !alter )
                YYABORT;
255 256

            PARSER_BUBBLE_UP_VIEW( sql, $$, alter );
257 258 259 260 261 262 263 264 265
        }
  | TK_ALTER TK_TABLE table TK_ADD column_and_type
        {
            SQL_input *sql = (SQL_input *)info;
            MSIVIEW *alter = NULL;

            ALTER_CreateView( sql->db, &alter, $3, $5, 0 );
            if (!alter)
                YYABORT;
266 267

            PARSER_BUBBLE_UP_VIEW( sql, $$, alter );
268
        }
269 270 271 272 273 274 275 276
  | TK_ALTER TK_TABLE table TK_ADD column_and_type TK_HOLD
        {
            SQL_input *sql = (SQL_input *)info;
            MSIVIEW *alter = NULL;

            ALTER_CreateView( sql->db, &alter, $3, $5, 1 );
            if (!alter)
                YYABORT;
277 278

            PARSER_BUBBLE_UP_VIEW( sql, $$, alter );
279
        }
280 281 282 283 284 285 286 287 288 289 290 291 292
    ;

alterop:
    TK_HOLD
        {
            $$ = 1;
        }
  | TK_FREE
        {
            $$ = -1;
        }
  ;

293 294 295 296
onedrop:
    TK_DROP TK_TABLE table
        {
            SQL_input* sql = (SQL_input*) info;
297
            MSIVIEW* drop = NULL;
298 299
            UINT r;

300
            r = DROP_CreateView( sql->db, &drop, $3 );
301 302
            if( r != ERROR_SUCCESS || !$$ )
                YYABORT;
303 304

            PARSER_BUBBLE_UP_VIEW( sql, $$, drop );
305 306 307
        }
  ;

308
table_def:
309
    column_def TK_PRIMARY TK_KEY collist
310
        {
311
            if( SQL_MarkPrimaryKeys( &$1, $4 ) )
312 313 314 315 316 317 318
                $$ = $1;
            else
                $$ = NULL;
        }
    ;

column_def:
319
    column_def TK_COMMA column_and_type
320
        {
321
            column_info *ci;
322 323 324 325

            for( ci = $1; ci->next; ci = ci->next )
                ;

326
            ci->next = $3;
327
            $$ = $1;
328
        }
329
  | column_and_type
330
        {
331 332 333 334 335 336 337 338
            $$ = $1;
        }
    ;

column_and_type:
    column column_type
        {
            $$ = $1;
339
            $$->type = ($2 | MSITYPE_VALID);
340
            $$->temporary = $2 & MSITYPE_TEMPORARY ? TRUE : FALSE;
341 342 343 344
        }
    ;

column_type:
345 346
    data_type_l
        {
347
            $$ = $1;
348 349 350
        }
  | data_type_l TK_LOCALIZABLE
        {
351
            $$ = $1 | MSITYPE_LOCALIZABLE;
352
        }
353 354
  | data_type_l TK_TEMPORARY
        {
355
            $$ = $1 | MSITYPE_TEMPORARY;
356
        }
357 358 359
    ;

data_type_l:
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
    data_type
        {
            $$ |= MSITYPE_NULLABLE;
        }
  | data_type TK_NOT TK_NULL
        {
            $$ = $1;
        }
    ;

data_type:
    TK_CHAR
        {
            $$ = MSITYPE_STRING | 1;
        }
  | TK_CHAR TK_LP data_count TK_RP
        {
377
            $$ = MSITYPE_STRING | 0x400 | $3;
378 379 380
        }
  | TK_LONGCHAR
        {
381
            $$ = MSITYPE_STRING | 0x400;
382 383 384
        }
  | TK_SHORT
        {
385
            $$ = 2 | 0x400;
386 387 388
        }
  | TK_INT
        {
389
            $$ = 2 | 0x400;
390 391 392 393 394 395 396
        }
  | TK_LONG
        {
            $$ = 4;
        }
  | TK_OBJECT
        {
397
            $$ = MSITYPE_STRING | MSITYPE_VALID;
398 399 400 401
        }
    ;

data_count:
402
    number
403
        {
404
            if( ( $1 > 255 ) || ( $1 < 0 ) )
405
                YYABORT;
406
            $$ = $1;
407 408 409
        }
    ;

410
oneselect:
411 412 413 414 415
    TK_SELECT selectfrom
        {
            $$ = $2;
        }
  | TK_SELECT TK_DISTINCT selectfrom
416
        {
417
            SQL_input* sql = (SQL_input*) info;
418
            MSIVIEW* distinct = NULL;
419
            UINT r;
420

421
            r = DISTINCT_CreateView( sql->db, &distinct, $3 );
422
            if (r != ERROR_SUCCESS)
423
                YYABORT;
424 425

            PARSER_BUBBLE_UP_VIEW( sql, $$, distinct );
426
        }
427 428 429
    ;

selectfrom:
430
    selcollist from
431
        {
432
            SQL_input* sql = (SQL_input*) info;
433
            MSIVIEW* select = NULL;
434
            UINT r;
435

436
            if( $1 )
437
            {
438
                r = SELECT_CreateView( sql->db, &select, $2, $1 );
439 440
                if (r != ERROR_SUCCESS)
                    YYABORT;
441 442

                PARSER_BUBBLE_UP_VIEW( sql, $$, select );
443
            }
444 445
            else
                $$ = $2;
446 447 448 449
        }
    ;

selcollist:
450 451 452 453 454 455 456 457 458 459 460 461
    selcolumn
  | selcolumn TK_COMMA selcollist
        {
            $1->next = $3;
        }
  | TK_STAR
        {
            $$ = NULL;
        }
    ;

collist:
462
    column
463
  | column TK_COMMA collist
464
        {
465
            $1->next = $3;
466 467 468 469 470 471 472 473
        }
  | TK_STAR
        {
            $$ = NULL;
        }
    ;

from:
474
    TK_FROM table
475
        {
476
            SQL_input* sql = (SQL_input*) info;
477
            MSIVIEW* table = NULL;
478
            UINT r;
479

480 481
            r = TABLE_CreateView( sql->db, $2, &table );
            if( r != ERROR_SUCCESS || !$$ )
482
                YYABORT;
483

484 485
            PARSER_BUBBLE_UP_VIEW( sql, $$, table );
        }
486
  | unorderdfrom TK_ORDER TK_BY collist
487 488 489 490 491 492 493 494 495 496 497
        {
            UINT r;

            if( $4 )
            {
                r = $1->ops->sort( $1, $4 );
                if ( r != ERROR_SUCCESS)
                    YYABORT;
            }

            $$ = $1;
498
        }
499 500 501 502 503
  | unorderdfrom
  ;

unorderdfrom:
    TK_FROM tablelist
504 505 506 507 508 509 510 511 512
        {
            SQL_input* sql = (SQL_input*) info;
            MSIVIEW* where = NULL;
            UINT r;

            r = WHERE_CreateView( sql->db, &where, $2, NULL );
            if( r != ERROR_SUCCESS )
                YYABORT;

513
            PARSER_BUBBLE_UP_VIEW( sql, $$, where );
514
        }
515
  | TK_FROM tablelist TK_WHERE expr
516
        {
517
            SQL_input* sql = (SQL_input*) info;
518
            MSIVIEW* where = NULL;
519 520
            UINT r;

521 522
            r = WHERE_CreateView( sql->db, &where, $2, $4 );
            if( r != ERROR_SUCCESS )
523
                YYABORT;
524

525
            PARSER_BUBBLE_UP_VIEW( sql, $$, where );
526 527 528
        }
    ;

529 530 531
tablelist:
    table
        {
532
            $$ = $1;
533
        }
534
  | table TK_COMMA tablelist
535
        {
536
            $$ = parser_add_table( info, $3, $1 );
537 538 539 540 541
            if (!$$)
                YYABORT;
        }
    ;

542 543 544 545
expr:
    TK_LP expr TK_RP
        {
            $$ = $2;
546 547
            if( !$$ )
                YYABORT;
548 549 550
        }
  | expr TK_AND expr
        {
551
            $$ = EXPR_complex( info, $1, OP_AND, $3 );
552 553
            if( !$$ )
                YYABORT;
554 555 556
        }
  | expr TK_OR expr
        {
557
            $$ = EXPR_complex( info, $1, OP_OR, $3 );
558 559
            if( !$$ )
                YYABORT;
560 561 562
        }
  | column_val TK_EQ val
        {
563
            $$ = EXPR_complex( info, $1, OP_EQ, $3 );
564 565
            if( !$$ )
                YYABORT;
566 567 568
        }
  | column_val TK_GT val
        {
569
            $$ = EXPR_complex( info, $1, OP_GT, $3 );
570 571
            if( !$$ )
                YYABORT;
572 573 574
        }
  | column_val TK_LT val
        {
575
            $$ = EXPR_complex( info, $1, OP_LT, $3 );
576 577
            if( !$$ )
                YYABORT;
578 579 580
        }
  | column_val TK_LE val
        {
581
            $$ = EXPR_complex( info, $1, OP_LE, $3 );
582 583
            if( !$$ )
                YYABORT;
584 585 586
        }
  | column_val TK_GE val
        {
587
            $$ = EXPR_complex( info, $1, OP_GE, $3 );
588 589
            if( !$$ )
                YYABORT;
590 591 592
        }
  | column_val TK_NE val
        {
593
            $$ = EXPR_complex( info, $1, OP_NE, $3 );
594 595
            if( !$$ )
                YYABORT;
596 597 598
        }
  | column_val TK_IS TK_NULL
        {
599
            $$ = EXPR_unary( info, $1, OP_ISNULL );
600 601
            if( !$$ )
                YYABORT;
602 603 604
        }
  | column_val TK_IS TK_NOT TK_NULL
        {
605
            $$ = EXPR_unary( info, $1, OP_NOTNULL );
606 607
            if( !$$ )
                YYABORT;
608 609 610 611 612
        }
    ;

val:
    column_val
613
  | const_val
614
    ;
615 616 617 618

constlist:
    const_val
        {
619 620
            $$ = parser_alloc_column( info, NULL, NULL );
            if( !$$ )
621
                YYABORT;
622
            $$->val = $1;
623
        }
624
  | const_val TK_COMMA constlist
625
        {
626 627
            $$ = parser_alloc_column( info, NULL, NULL );
            if( !$$ )
628
                YYABORT;
629 630
            $$->val = $1;
            $$->next = $3;
631
        }
632 633 634 635 636 637 638
    ;

update_assign_list:
    column_assignment
  | column_assignment TK_COMMA update_assign_list
        {
            $$ = $1;
639
            $$->next = $3;
640 641 642 643 644 645
        }
    ;

column_assignment:
    column TK_EQ const_val
        {
646 647
            $$ = $1;
            $$->val = $3;
648 649
        }
    ;
650 651

const_val:
652
    number
653
        {
654
            $$ = EXPR_ival( info, $1 );
655 656
            if( !$$ )
                YYABORT;
657
        }
658
  | TK_MINUS number %prec TK_NEGATION
659
        {
660
            $$ = EXPR_ival( info, -$2 );
661 662
            if( !$$ )
                YYABORT;
663 664 665
        }
  | TK_STRING
        {
666
            $$ = EXPR_sval( info, &$1 );
667 668
            if( !$$ )
                YYABORT;
669
        }
Mike McCormack's avatar
Mike McCormack committed
670 671
  | TK_WILDCARD
        {
672
            $$ = EXPR_wildcard( info );
673 674
            if( !$$ )
                YYABORT;
Mike McCormack's avatar
Mike McCormack committed
675
        }
676 677 678
    ;

column_val:
679
    column
680
        {
681
            $$ = EXPR_column( info, $1 );
682 683
            if( !$$ )
                YYABORT;
684 685 686 687
        }
    ;

column:
688
    table TK_DOT id
689
        {
690 691 692
            $$ = parser_alloc_column( info, $1, $3 );
            if( !$$ )
                YYABORT;
693
        }
694
  | id
695
        {
696 697 698
            $$ = parser_alloc_column( info, NULL, $1 );
            if( !$$ )
                YYABORT;
699 700 701
        }
    ;

702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
selcolumn:
    table TK_DOT id
        {
            $$ = parser_alloc_column( info, $1, $3 );
            if( !$$ )
                YYABORT;
        }
  | id
        {
            $$ = parser_alloc_column( info, NULL, $1 );
            if( !$$ )
                YYABORT;
        }
  | string
        {
            $$ = parser_alloc_column( info, NULL, $1 );
            if( !$$ )
                YYABORT;
        }
    ;

723
table:
724
    id
725 726 727 728 729
        {
            $$ = $1;
        }
    ;

730
id:
731 732
    TK_ID
        {
733
            if ( SQL_getstring( info, &$1, &$$ ) != ERROR_SUCCESS || !$$ )
734
                YYABORT;
735
        }
736 737 738 739 740 741 742 743
    ;

string:
    TK_STRING
        {
            if ( SQL_getstring( info, &$1, &$$ ) != ERROR_SUCCESS || !$$ )
                YYABORT;
        }
744 745
    ;

746 747 748 749 750 751 752
number:
    TK_INTEGER
        {
            $$ = SQL_getint( info );
        }
    ;

753 754
%%

755
static LPWSTR parser_add_table( void *info, LPCWSTR list, LPCWSTR table )
756 757
{
    static const WCHAR space[] = {' ',0};
758 759
    DWORD len = strlenW( list ) + strlenW( table ) + 2;
    LPWSTR ret;
760

761 762 763 764 765 766 767 768
    ret = parser_alloc( info, len * sizeof(WCHAR) );
    if( ret )
    {
        strcpyW( ret, list );
        strcatW( ret, space );
        strcatW( ret, table );
    }
    return ret;
769 770
}

771 772 773 774 775
static void *parser_alloc( void *info, unsigned int sz )
{
    SQL_input* sql = (SQL_input*) info;
    struct list *mem;

776
    mem = msi_alloc( sizeof (struct list) + sz );
777 778 779 780
    list_add_tail( sql->mem, mem );
    return &mem[1];
}

781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
static column_info *parser_alloc_column( void *info, LPCWSTR table, LPCWSTR column )
{
    column_info *col;

    col = parser_alloc( info, sizeof (*col) );
    if( col )
    {
        col->table = table;
        col->column = column;
        col->val = NULL;
        col->type = 0;
        col->next = NULL;
    }

    return col;
}

798
static int sql_lex( void *SQL_lval, SQL_input *sql )
799
{
800
    int token, skip;
801
    struct sql_str * str = SQL_lval;
802 803 804 805 806 807 808

    do
    {
        sql->n += sql->len;
        if( ! sql->command[sql->n] )
            return 0;  /* end of input */

809
        /* TRACE("string : %s\n", debugstr_w(&sql->command[sql->n])); */
810
        sql->len = sqliteGetToken( &sql->command[sql->n], &token, &skip );
811 812
        if( sql->len==0 )
            break;
813 814
        str->data = &sql->command[sql->n];
        str->len = sql->len;
815
        sql->n += skip;
816 817 818
    }
    while( token == TK_SPACE );

819
    /* TRACE("token : %d (%s)\n", token, debugstr_wn(&sql->command[sql->n], sql->len)); */
820

821 822 823
    return token;
}

824
UINT SQL_getstring( void *info, const struct sql_str *strdata, LPWSTR *str )
825
{
826 827
    LPCWSTR p = strdata->data;
    UINT len = strdata->len;
828 829 830 831 832

    /* match quotes */
    if( ( (p[0]=='`') && (p[len-1]!='`') ) ||
        ( (p[0]=='\'') && (p[len-1]!='\'') ) )
        return ERROR_FUNCTION_FAILED;
833 834

    /* if there's quotes, remove them */
835
    if( ( (p[0]=='`') && (p[len-1]=='`') ) ||
836
        ( (p[0]=='\'') && (p[len-1]=='\'') ) )
837 838 839 840
    {
        p++;
        len -= 2;
    }
841 842 843 844 845
    *str = parser_alloc( info, (len + 1)*sizeof(WCHAR) );
    if( !*str )
        return ERROR_OUTOFMEMORY;
    memcpy( *str, p, len*sizeof(WCHAR) );
    (*str)[len]=0;
846

847
    return ERROR_SUCCESS;
848 849
}

850
INT SQL_getint( void *info )
851
{
852
    SQL_input* sql = (SQL_input*) info;
853
    LPCWSTR p = &sql->command[sql->n];
854 855 856 857 858 859 860 861 862 863 864
    INT i, r = 0;

    for( i=0; i<sql->len; i++ )
    {
        if( '0' > p[i] || '9' < p[i] )
        {
            ERR("should only be numbers here!\n");
            break;
        }
        r = (p[i]-'0') + r*10;
    }
865

866
    return r;
867 868
}

869
static int sql_error( const char *str )
870 871 872 873
{
    return 0;
}

874
static struct expr * EXPR_wildcard( void *info )
875
{
876
    struct expr *e = parser_alloc( info, sizeof *e );
877 878 879 880 881 882 883
    if( e )
    {
        e->type = EXPR_WILDCARD;
    }
    return e;
}

884
static struct expr * EXPR_complex( void *info, struct expr *l, UINT op, struct expr *r )
885
{
886
    struct expr *e = parser_alloc( info, sizeof *e );
887 888 889 890 891 892 893 894 895 896
    if( e )
    {
        e->type = EXPR_COMPLEX;
        e->u.expr.left = l;
        e->u.expr.op = op;
        e->u.expr.right = r;
    }
    return e;
}

897 898 899 900 901 902 903 904 905 906 907 908 909
static struct expr * EXPR_unary( void *info, struct expr *l, UINT op )
{
    struct expr *e = parser_alloc( info, sizeof *e );
    if( e )
    {
        e->type = EXPR_UNARY;
        e->u.expr.left = l;
        e->u.expr.op = op;
        e->u.expr.right = NULL;
    }
    return e;
}

910
static struct expr * EXPR_column( void *info, const column_info *column )
911
{
912
    struct expr *e = parser_alloc( info, sizeof *e );
913 914 915
    if( e )
    {
        e->type = EXPR_COLUMN;
916 917
        e->u.column.unparsed.column = column->column;
        e->u.column.unparsed.table = column->table;
918 919 920 921
    }
    return e;
}

922
static struct expr * EXPR_ival( void *info, int val )
923
{
924
    struct expr *e = parser_alloc( info, sizeof *e );
925 926 927
    if( e )
    {
        e->type = EXPR_IVAL;
928
        e->u.ival = val;
929 930 931 932
    }
    return e;
}

933
static struct expr * EXPR_sval( void *info, const struct sql_str *str )
934
{
935
    struct expr *e = parser_alloc( info, sizeof *e );
936 937 938
    if( e )
    {
        e->type = EXPR_SVAL;
939
        if( SQL_getstring( info, str, (LPWSTR *)&e->u.sval ) != ERROR_SUCCESS )
940
            return NULL; /* e will be freed by query destructor */
941 942 943 944
    }
    return e;
}

945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974
static void swap_columns( column_info **cols, column_info *A, int idx )
{
    column_info *preA = NULL, *preB = NULL, *B, *ptr;
    int i = 0;

    B = NULL;
    ptr = *cols;
    while( ptr )
    {
        if( i++ == idx )
            B = ptr;
        else if( !B )
            preB = ptr;

        if( ptr->next == A )
            preA = ptr;

        ptr = ptr->next;
    }

    if( preB ) preB->next = A;
    if( preA ) preA->next = B;
    ptr = A->next;
    A->next = B->next;
    B->next = ptr;
    if( idx == 0 )
      *cols = A;
}

static BOOL SQL_MarkPrimaryKeys( column_info **cols,
975
                                 column_info *keys )
976
{
977
    column_info *k;
978
    BOOL found = TRUE;
979
    int count;
980

981
    for( k = keys, count = 0; k && found; k = k->next, count++ )
982
    {
983
        column_info *c;
984
        int idx;
985 986

        found = FALSE;
987 988
        for( c = *cols, idx = 0; c && !found; c = c->next, idx++ )
        {
989
            if( strcmpW( k->column, c->column ) )
990 991 992 993 994
                continue;
            c->type |= MSITYPE_KEY;
            found = TRUE;
            if (idx != count)
                swap_columns( cols, c, count );
995 996 997 998 999 1000
        }
    }

    return found;
}

1001 1002
UINT MSI_ParseSQL( MSIDATABASE *db, LPCWSTR command, MSIVIEW **phview,
                   struct list *mem )
1003
{
1004
    SQL_input sql;
1005 1006 1007 1008 1009 1010 1011 1012
    int r;

    *phview = NULL;

    sql.db = db;
    sql.command = command;
    sql.n = 0;
    sql.len = 0;
1013
    sql.r = ERROR_BAD_QUERY_SYNTAX;
1014
    sql.view = phview;
1015
    sql.mem = mem;
1016

1017
    r = sql_parse(&sql);
1018 1019 1020 1021

    TRACE("Parse returned %d\n", r);
    if( r )
    {
1022 1023 1024 1025 1026
        if (*sql.view)
        {
            (*sql.view)->ops->delete(*sql.view);
            *sql.view = NULL;
        }
1027
        return sql.r;
1028 1029 1030 1031
    }

    return ERROR_SUCCESS;
}