netstat: Initial implementation.
[wine] / dlls / msi / sql.y
1 %{
2
3 /*
4  * Implementation of the Microsoft Installer (msi.dll)
5  *
6  * Copyright 2002-2004 Mike McCormack for CodeWeavers
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * 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 #include <stdio.h>
28 #include <stdlib.h>
29
30 #include "windef.h"
31 #include "winbase.h"
32 #include "query.h"
33 #include "wine/list.h"
34 #include "wine/debug.h"
35 #include "wine/unicode.h"
36
37 #define YYLEX_PARAM info
38 #define YYPARSE_PARAM info
39
40 static int sql_error(const char *str);
41
42 WINE_DEFAULT_DEBUG_CHANNEL(msi);
43
44 typedef struct tag_SQL_input
45 {
46     MSIDATABASE *db;
47     LPCWSTR command;
48     DWORD n, len;
49     UINT r;
50     MSIVIEW **view;  /* View structure for the resulting query.  This value
51                       * tracks the view currently being created so we can free
52                       * this view on syntax error.
53                       */
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 #define PARSER_BUBBLE_UP_VIEW( sql, result, current_view ) \
75     *sql->view = current_view; \
76     result = current_view
77
78 %}
79
80 %pure-parser
81
82 %union
83 {
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 %token <str> TK_ID
96 %token TK_ILLEGAL TK_INSERT TK_INT
97 %token <str> TK_INTEGER
98 %token TK_INTO TK_IS TK_KEY TK_LE TK_LONG TK_LONGCHAR TK_LP TK_LT
99 %token TK_LOCALIZABLE TK_MINUS TK_NE TK_NOT TK_NULL
100 %token TK_OBJECT TK_OR TK_ORDER TK_PRIMARY TK_RP
101 %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  * These are extra tokens used by the lexer but never seen by the
107  * parser.  We put them in a rule so that the parser generator will
108  * add them to the parse.h output file.
109  *
110  */
111 %nonassoc END_OF_FILE ILLEGAL SPACE UNCLOSED_STRING COMMENT FUNCTION
112           COLUMN AGG_FUNCTION.
113
114 %type <string> table tablelist id string
115 %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 %left TK_OR
124 %left TK_AND
125 %left TK_NOT
126 %left TK_EQ TK_NE TK_LT TK_GT TK_LE TK_GE TK_LIKE
127 %right TK_NEGATION
128
129 %%
130
131 query:
132     onequery
133     {
134         SQL_input* sql = (SQL_input*) info;
135         *sql->view = $1;
136     }
137     ;
138
139 onequery:
140     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             if( !insert )
157                 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             if( !insert )
168                 YYABORT;
169
170             PARSER_BUBBLE_UP_VIEW( sql, $$,  insert );
171         }
172     ;
173
174 onecreate:
175     TK_CREATE TK_TABLE table TK_LP table_def TK_RP
176         {
177             SQL_input* sql = (SQL_input*) info;
178             MSIVIEW *create = NULL;
179             UINT r;
180
181             if( !$5 )
182                 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   | TK_CREATE TK_TABLE table TK_LP table_def TK_RP TK_HOLD
193         {
194             SQL_input* sql = (SQL_input*) info;
195             MSIVIEW *create = NULL;
196
197             if( !$5 )
198                 YYABORT;
199             CREATE_CreateView( sql->db, &create, $3, $5, TRUE );
200             if( !create )
201                 YYABORT;
202
203             PARSER_BUBBLE_UP_VIEW( sql, $$,  create );
204         }
205     ;
206
207 oneupdate:
208     TK_UPDATE table TK_SET update_assign_list TK_WHERE expr
209         {
210             SQL_input* sql = (SQL_input*) info;
211             MSIVIEW *update = NULL;
212
213             UPDATE_CreateView( sql->db, &update, $2, $4, $6 );
214             if( !update )
215                 YYABORT;
216
217             PARSER_BUBBLE_UP_VIEW( sql, $$,  update );
218         }
219   | TK_UPDATE table TK_SET update_assign_list
220         {
221             SQL_input* sql = (SQL_input*) info;
222             MSIVIEW *update = NULL;
223
224             UPDATE_CreateView( sql->db, &update, $2, $4, NULL );
225             if( !update )
226                 YYABORT;
227
228             PARSER_BUBBLE_UP_VIEW( sql, $$,  update );
229         }
230     ;
231
232 onedelete:
233     TK_DELETE from
234         {
235             SQL_input* sql = (SQL_input*) info;
236             MSIVIEW *delete = NULL;
237
238             DELETE_CreateView( sql->db, &delete, $2 );
239             if( !delete )
240                 YYABORT;
241
242             PARSER_BUBBLE_UP_VIEW( sql, $$, delete );
243         }
244     ;
245
246 onealter:
247     TK_ALTER TK_TABLE table alterop
248         {
249             SQL_input* sql = (SQL_input*) info;
250             MSIVIEW *alter = NULL;
251
252             ALTER_CreateView( sql->db, &alter, $3, NULL, $4 );
253             if( !alter )
254                 YYABORT;
255
256             PARSER_BUBBLE_UP_VIEW( sql, $$, alter );
257         }
258   | TK_ALTER TK_TABLE table TK_ADD column_and_type
259         {
260             SQL_input *sql = (SQL_input *)info;
261             MSIVIEW *alter = NULL;
262
263             ALTER_CreateView( sql->db, &alter, $3, $5, 0 );
264             if (!alter)
265                 YYABORT;
266
267             PARSER_BUBBLE_UP_VIEW( sql, $$, alter );
268         }
269   | TK_ALTER TK_TABLE table TK_ADD column_and_type TK_HOLD
270         {
271             SQL_input *sql = (SQL_input *)info;
272             MSIVIEW *alter = NULL;
273
274             ALTER_CreateView( sql->db, &alter, $3, $5, 1 );
275             if (!alter)
276                 YYABORT;
277
278             PARSER_BUBBLE_UP_VIEW( sql, $$, alter );
279         }
280     ;
281
282 alterop:
283     TK_HOLD
284         {
285             $$ = 1;
286         }
287   | TK_FREE
288         {
289             $$ = -1;
290         }
291   ;
292
293 onedrop:
294     TK_DROP TK_TABLE table
295         {
296             SQL_input* sql = (SQL_input*) info;
297             MSIVIEW* drop = NULL;
298             UINT r;
299
300             r = DROP_CreateView( sql->db, &drop, $3 );
301             if( r != ERROR_SUCCESS || !$$ )
302                 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                 $$ = $1;
313             else
314                 $$ = NULL;
315         }
316     ;
317
318 column_def:
319     column_def TK_COMMA column_and_type
320         {
321             column_info *ci;
322
323             for( ci = $1; ci->next; ci = ci->next )
324                 ;
325
326             ci->next = $3;
327             $$ = $1;
328         }
329   | column_and_type
330         {
331             $$ = $1;
332         }
333     ;
334
335 column_and_type:
336     column column_type
337         {
338             $$ = $1;
339             $$->type = ($2 | MSITYPE_VALID);
340             $$->temporary = $2 & MSITYPE_TEMPORARY ? TRUE : FALSE;
341         }
342     ;
343
344 column_type:
345     data_type_l
346         {
347             $$ = $1;
348         }
349   | data_type_l TK_LOCALIZABLE
350         {
351             $$ = $1 | MSITYPE_LOCALIZABLE;
352         }
353   | data_type_l TK_TEMPORARY
354         {
355             $$ = $1 | MSITYPE_TEMPORARY;
356         }
357     ;
358
359 data_type_l:
360     data_type
361         {
362             $$ |= MSITYPE_NULLABLE;
363         }
364   | data_type TK_NOT TK_NULL
365         {
366             $$ = $1;
367         }
368     ;
369
370 data_type:
371     TK_CHAR
372         {
373             $$ = MSITYPE_STRING | 1;
374         }
375   | TK_CHAR TK_LP data_count TK_RP
376         {
377             $$ = MSITYPE_STRING | 0x400 | $3;
378         }
379   | TK_LONGCHAR
380         {
381             $$ = MSITYPE_STRING | 0x400;
382         }
383   | TK_SHORT
384         {
385             $$ = 2 | 0x400;
386         }
387   | TK_INT
388         {
389             $$ = 2 | 0x400;
390         }
391   | TK_LONG
392         {
393             $$ = 4;
394         }
395   | TK_OBJECT
396         {
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     TK_SELECT selectfrom
412         {
413             $$ = $2;
414         }
415   | 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                 if (r != ERROR_SUCCESS)
440                     YYABORT;
441
442                 PARSER_BUBBLE_UP_VIEW( sql, $$, select );
443             }
444             else
445                 $$ = $2;
446         }
447     ;
448
449 selcollist:
450     selcolumn
451   | selcolumn TK_COMMA selcollist
452         {
453             $1->next = $3;
454         }
455   | TK_STAR
456         {
457             $$ = NULL;
458         }
459     ;
460
461 collist:
462     column
463   | column TK_COMMA collist
464         {
465             $1->next = $3;
466         }
467   | TK_STAR
468         {
469             $$ = NULL;
470         }
471     ;
472
473 from:
474     TK_FROM table
475         {
476             SQL_input* sql = (SQL_input*) info;
477             MSIVIEW* table = NULL;
478             UINT r;
479
480             r = TABLE_CreateView( sql->db, $2, &table );
481             if( r != ERROR_SUCCESS || !$$ )
482                 YYABORT;
483
484             PARSER_BUBBLE_UP_VIEW( sql, $$, table );
485         }
486   | unorderdfrom TK_ORDER TK_BY collist
487         {
488             UINT r;
489
490             if( $4 )
491             {
492                 r = $1->ops->sort( $1, $4 );
493                 if ( r != ERROR_SUCCESS)
494                     YYABORT;
495             }
496
497             $$ = $1;
498         }
499   | unorderdfrom
500   ;
501
502 unorderdfrom:
503     TK_FROM tablelist
504         {
505             SQL_input* sql = (SQL_input*) info;
506             MSIVIEW* where = NULL;
507             UINT r;
508
509             r = WHERE_CreateView( sql->db, &where, $2, NULL );
510             if( r != ERROR_SUCCESS )
511                 YYABORT;
512
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             UINT r;
520
521             r = WHERE_CreateView( sql->db, &where, $2, $4 );
522             if( r != ERROR_SUCCESS )
523                 YYABORT;
524
525             PARSER_BUBBLE_UP_VIEW( sql, $$, where );
526         }
527     ;
528
529 tablelist:
530     table
531         {
532             $$ = $1;
533         }
534   | table TK_COMMA tablelist
535         {
536             $$ = parser_add_table( info, $3, $1 );
537             if (!$$)
538                 YYABORT;
539         }
540     ;
541
542 expr:
543     TK_LP expr TK_RP
544         {
545             $$ = $2;
546             if( !$$ )
547                 YYABORT;
548         }
549   | expr TK_AND expr
550         {
551             $$ = EXPR_complex( info, $1, OP_AND, $3 );
552             if( !$$ )
553                 YYABORT;
554         }
555   | expr TK_OR expr
556         {
557             $$ = EXPR_complex( info, $1, OP_OR, $3 );
558             if( !$$ )
559                 YYABORT;
560         }
561   | column_val TK_EQ val
562         {
563             $$ = EXPR_complex( info, $1, OP_EQ, $3 );
564             if( !$$ )
565                 YYABORT;
566         }
567   | column_val TK_GT val
568         {
569             $$ = EXPR_complex( info, $1, OP_GT, $3 );
570             if( !$$ )
571                 YYABORT;
572         }
573   | column_val TK_LT val
574         {
575             $$ = EXPR_complex( info, $1, OP_LT, $3 );
576             if( !$$ )
577                 YYABORT;
578         }
579   | column_val TK_LE val
580         {
581             $$ = EXPR_complex( info, $1, OP_LE, $3 );
582             if( !$$ )
583                 YYABORT;
584         }
585   | column_val TK_GE val
586         {
587             $$ = EXPR_complex( info, $1, OP_GE, $3 );
588             if( !$$ )
589                 YYABORT;
590         }
591   | column_val TK_NE val
592         {
593             $$ = EXPR_complex( info, $1, OP_NE, $3 );
594             if( !$$ )
595                 YYABORT;
596         }
597   | column_val TK_IS TK_NULL
598         {
599             $$ = EXPR_unary( info, $1, OP_ISNULL );
600             if( !$$ )
601                 YYABORT;
602         }
603   | column_val TK_IS TK_NOT TK_NULL
604         {
605             $$ = EXPR_unary( info, $1, OP_NOTNULL );
606             if( !$$ )
607                 YYABORT;
608         }
609     ;
610
611 val:
612     column_val
613   | const_val
614     ;
615
616 constlist:
617     const_val
618         {
619             $$ = parser_alloc_column( info, NULL, NULL );
620             if( !$$ )
621                 YYABORT;
622             $$->val = $1;
623         }
624   | const_val TK_COMMA constlist
625         {
626             $$ = parser_alloc_column( info, NULL, NULL );
627             if( !$$ )
628                 YYABORT;
629             $$->val = $1;
630             $$->next = $3;
631         }
632     ;
633
634 update_assign_list:
635     column_assignment
636   | column_assignment TK_COMMA update_assign_list
637         {
638             $$ = $1;
639             $$->next = $3;
640         }
641     ;
642
643 column_assignment:
644     column TK_EQ const_val
645         {
646             $$ = $1;
647             $$->val = $3;
648         }
649     ;
650
651 const_val:
652     number
653         {
654             $$ = EXPR_ival( info, $1 );
655             if( !$$ )
656                 YYABORT;
657         }
658   | TK_MINUS number %prec TK_NEGATION
659         {
660             $$ = EXPR_ival( info, -$2 );
661             if( !$$ )
662                 YYABORT;
663         }
664   | TK_STRING
665         {
666             $$ = EXPR_sval( info, &$1 );
667             if( !$$ )
668                 YYABORT;
669         }
670   | TK_WILDCARD
671         {
672             $$ = EXPR_wildcard( info );
673             if( !$$ )
674                 YYABORT;
675         }
676     ;
677
678 column_val:
679     column
680         {
681             $$ = EXPR_column( info, $1 );
682             if( !$$ )
683                 YYABORT;
684         }
685     ;
686
687 column:
688     table TK_DOT id
689         {
690             $$ = parser_alloc_column( info, $1, $3 );
691             if( !$$ )
692                 YYABORT;
693         }
694   | id
695         {
696             $$ = parser_alloc_column( info, NULL, $1 );
697             if( !$$ )
698                 YYABORT;
699         }
700     ;
701
702 selcolumn:
703     table TK_DOT id
704         {
705             $$ = parser_alloc_column( info, $1, $3 );
706             if( !$$ )
707                 YYABORT;
708         }
709   | id
710         {
711             $$ = parser_alloc_column( info, NULL, $1 );
712             if( !$$ )
713                 YYABORT;
714         }
715   | string
716         {
717             $$ = parser_alloc_column( info, NULL, $1 );
718             if( !$$ )
719                 YYABORT;
720         }
721     ;
722
723 table:
724     id
725         {
726             $$ = $1;
727         }
728     ;
729
730 id:
731     TK_ID
732         {
733             if ( SQL_getstring( info, &$1, &$$ ) != ERROR_SUCCESS || !$$ )
734                 YYABORT;
735         }
736     ;
737
738 string:
739     TK_STRING
740         {
741             if ( SQL_getstring( info, &$1, &$$ ) != ERROR_SUCCESS || !$$ )
742                 YYABORT;
743         }
744     ;
745
746 number:
747     TK_INTEGER
748         {
749             $$ = SQL_getint( info );
750         }
751     ;
752
753 %%
754
755 static LPWSTR parser_add_table( void *info, LPCWSTR list, LPCWSTR table )
756 {
757     static const WCHAR space[] = {' ',0};
758     DWORD len = strlenW( list ) + strlenW( table ) + 2;
759     LPWSTR ret;
760
761     ret = parser_alloc( info, len * sizeof(WCHAR) );
762     if( ret )
763     {
764         strcpyW( ret, list );
765         strcatW( ret, space );
766         strcatW( ret, table );
767     }
768     return ret;
769 }
770
771 static void *parser_alloc( void *info, unsigned int sz )
772 {
773     SQL_input* sql = (SQL_input*) info;
774     struct list *mem;
775
776     mem = msi_alloc( sizeof (struct list) + sz );
777     list_add_tail( sql->mem, mem );
778     return &mem[1];
779 }
780
781 static column_info *parser_alloc_column( void *info, LPCWSTR table, LPCWSTR column )
782 {
783     column_info *col;
784
785     col = parser_alloc( info, sizeof (*col) );
786     if( col )
787     {
788         col->table = table;
789         col->column = column;
790         col->val = NULL;
791         col->type = 0;
792         col->next = NULL;
793     }
794
795     return col;
796 }
797
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     do
804     {
805         sql->n += sql->len;
806         if( ! sql->command[sql->n] )
807             return 0;  /* end of input */
808
809         /* TRACE("string : %s\n", debugstr_w(&sql->command[sql->n])); */
810         sql->len = sqliteGetToken( &sql->command[sql->n], &token, &skip );
811         if( sql->len==0 )
812             break;
813         str->data = &sql->command[sql->n];
814         str->len = sql->len;
815         sql->n += skip;
816     }
817     while( token == TK_SPACE );
818
819     /* TRACE("token : %d (%s)\n", token, debugstr_wn(&sql->command[sql->n], sql->len)); */
820
821     return token;
822 }
823
824 UINT SQL_getstring( void *info, const struct sql_str *strdata, LPWSTR *str )
825 {
826     LPCWSTR p = strdata->data;
827     UINT len = strdata->len;
828
829     /* match quotes */
830     if( ( (p[0]=='`') && (p[len-1]!='`') ) ||
831         ( (p[0]=='\'') && (p[len-1]!='\'') ) )
832         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         p++;
839         len -= 2;
840     }
841     *str = parser_alloc( info, (len + 1)*sizeof(WCHAR) );
842     if( !*str )
843         return ERROR_OUTOFMEMORY;
844     memcpy( *str, p, len*sizeof(WCHAR) );
845     (*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     INT i, r = 0;
855
856     for( i=0; i<sql->len; i++ )
857     {
858         if( '0' > p[i] || '9' < p[i] )
859         {
860             ERR("should only be numbers here!\n");
861             break;
862         }
863         r = (p[i]-'0') + r*10;
864     }
865
866     return r;
867 }
868
869 static int sql_error( const char *str )
870 {
871     return 0;
872 }
873
874 static struct expr * EXPR_wildcard( void *info )
875 {
876     struct expr *e = parser_alloc( info, sizeof *e );
877     if( e )
878     {
879         e->type = EXPR_WILDCARD;
880     }
881     return e;
882 }
883
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     if( e )
888     {
889         e->type = EXPR_COMPLEX;
890         e->u.expr.left = l;
891         e->u.expr.op = op;
892         e->u.expr.right = r;
893     }
894     return e;
895 }
896
897 static struct expr * EXPR_unary( void *info, struct expr *l, UINT op )
898 {
899     struct expr *e = parser_alloc( info, sizeof *e );
900     if( e )
901     {
902         e->type = EXPR_UNARY;
903         e->u.expr.left = l;
904         e->u.expr.op = op;
905         e->u.expr.right = NULL;
906     }
907     return e;
908 }
909
910 static struct expr * EXPR_column( void *info, const column_info *column )
911 {
912     struct expr *e = parser_alloc( info, sizeof *e );
913     if( e )
914     {
915         e->type = EXPR_COLUMN;
916         e->u.column.unparsed.column = column->column;
917         e->u.column.unparsed.table = column->table;
918     }
919     return e;
920 }
921
922 static struct expr * EXPR_ival( void *info, int val )
923 {
924     struct expr *e = parser_alloc( info, sizeof *e );
925     if( e )
926     {
927         e->type = EXPR_IVAL;
928         e->u.ival = val;
929     }
930     return e;
931 }
932
933 static struct expr * EXPR_sval( void *info, const struct sql_str *str )
934 {
935     struct expr *e = parser_alloc( info, sizeof *e );
936     if( e )
937     {
938         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     return e;
943 }
944
945 static void swap_columns( column_info **cols, column_info *A, int idx )
946 {
947     column_info *preA = NULL, *preB = NULL, *B, *ptr;
948     int i = 0;
949
950     B = NULL;
951     ptr = *cols;
952     while( ptr )
953     {
954         if( i++ == idx )
955             B = ptr;
956         else if( !B )
957             preB = ptr;
958
959         if( ptr->next == A )
960             preA = ptr;
961
962         ptr = ptr->next;
963     }
964
965     if( preB ) preB->next = A;
966     if( preA ) preA->next = B;
967     ptr = A->next;
968     A->next = B->next;
969     B->next = ptr;
970     if( idx == 0 )
971       *cols = A;
972 }
973
974 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         for( c = *cols, idx = 0; c && !found; c = c->next, idx++ )
988         {
989             if( strcmpW( k->column, c->column ) )
990                 continue;
991             c->type |= MSITYPE_KEY;
992             found = TRUE;
993             if (idx != count)
994                 swap_columns( cols, c, count );
995         }
996     }
997
998     return found;
999 }
1000
1001 UINT MSI_ParseSQL( MSIDATABASE *db, LPCWSTR command, MSIVIEW **phview,
1002                    struct list *mem )
1003 {
1004     SQL_input sql;
1005     int r;
1006
1007     *phview = NULL;
1008
1009     sql.db = db;
1010     sql.command = command;
1011     sql.n = 0;
1012     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     TRACE("Parse returned %d\n", r);
1020     if( r )
1021     {
1022         if (*sql.view)
1023         {
1024             (*sql.view)->ops->delete(*sql.view);
1025             *sql.view = NULL;
1026         }
1027         return sql.r;
1028     }
1029
1030     return ERROR_SUCCESS;
1031 }