553cc6dd0b5c567742f59b29c1b8e3fde0298fe8
[reactos.git] / reactos / dll / win32 / 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 WINE_DEFAULT_DEBUG_CHANNEL(msi);
38
39 static UINT SQL_getstring( void *info, const struct sql_str *strdata, LPWSTR *str );
40 static INT SQL_getint( void *info );
41 static int sql_lex( void *SQL_lval, SQL_input *info );
42 static int sql_error( SQL_input *info, const char *str);
43
44 static LPWSTR parser_add_table( void *info, LPCWSTR list, LPCWSTR table );
45 static void *parser_alloc( void *info, unsigned int sz );
46 static column_info *parser_alloc_column( void *info, LPCWSTR table, LPCWSTR column );
47
48 static BOOL SQL_MarkPrimaryKeys( column_info **cols, column_info *keys);
49
50 static struct expr * EXPR_complex( void *info, struct expr *l, UINT op, struct expr *r );
51 static struct expr * EXPR_unary( void *info, struct expr *l, UINT op );
52 static struct expr * EXPR_column( void *info, const column_info *column );
53 static struct expr * EXPR_ival( void *info, int val );
54 static struct expr * EXPR_sval( void *info, const struct sql_str *str );
55 static struct expr * EXPR_wildcard( void *info );
56
57 #define PARSER_BUBBLE_UP_VIEW( sql, result, current_view ) \
58 *sql->view = current_view; \
59 result = current_view
60
61 %}
62
63 %lex-param { SQL_input *info }
64 %parse-param { SQL_input *info }
65 %pure-parser
66
67 %union
68 {
69 struct sql_str str;
70 LPWSTR string;
71 column_info *column_list;
72 MSIVIEW *query;
73 struct expr *expr;
74 USHORT column_type;
75 int integer;
76 }
77
78 %token TK_ALTER TK_AND TK_BY TK_CHAR TK_COMMA TK_CREATE TK_DELETE TK_DROP
79 %token TK_DISTINCT TK_DOT TK_EQ TK_FREE TK_FROM TK_GE TK_GT TK_HOLD TK_ADD
80 %token <str> TK_ID
81 %token TK_ILLEGAL TK_INSERT TK_INT
82 %token <str> TK_INTEGER
83 %token TK_INTO TK_IS TK_KEY TK_LE TK_LONG TK_LONGCHAR TK_LP TK_LT
84 %token TK_LOCALIZABLE TK_MINUS TK_NE TK_NOT TK_NULL
85 %token TK_OBJECT TK_OR TK_ORDER TK_PRIMARY TK_RP
86 %token TK_SELECT TK_SET TK_SHORT TK_SPACE TK_STAR
87 %token <str> TK_STRING
88 %token TK_TABLE TK_TEMPORARY TK_UPDATE TK_VALUES TK_WHERE TK_WILDCARD
89
90 /*
91 * These are extra tokens used by the lexer but never seen by the
92 * parser. We put them in a rule so that the parser generator will
93 * add them to the parse.h output file.
94 *
95 */
96 %nonassoc END_OF_FILE ILLEGAL SPACE UNCLOSED_STRING COMMENT FUNCTION
97 COLUMN AGG_FUNCTION.
98
99 %type <string> table tablelist id string
100 %type <column_list> selcollist collist selcolumn column column_and_type column_def table_def
101 %type <column_list> column_assignment update_assign_list constlist
102 %type <query> query from selectfrom unorderdfrom
103 %type <query> oneupdate onedelete oneselect onequery onecreate oneinsert onealter onedrop
104 %type <expr> expr val column_val const_val
105 %type <column_type> column_type data_type data_type_l data_count
106 %type <integer> number alterop
107
108 %left TK_OR
109 %left TK_AND
110 %left TK_NOT
111 %left TK_EQ TK_NE TK_LT TK_GT TK_LE TK_GE TK_LIKE
112 %right TK_NEGATION
113
114 %%
115
116 query:
117 onequery
118 {
119 SQL_input* sql = (SQL_input*) info;
120 *sql->view = $1;
121 }
122 ;
123
124 onequery:
125 oneselect
126 | onecreate
127 | oneinsert
128 | oneupdate
129 | onedelete
130 | onealter
131 | onedrop
132 ;
133
134 oneinsert:
135 TK_INSERT TK_INTO table TK_LP collist TK_RP TK_VALUES TK_LP constlist TK_RP
136 {
137 SQL_input *sql = (SQL_input*) info;
138 MSIVIEW *insert = NULL;
139
140 INSERT_CreateView( sql->db, &insert, $3, $5, $9, FALSE );
141 if( !insert )
142 YYABORT;
143
144 PARSER_BUBBLE_UP_VIEW( sql, $$, insert );
145 }
146 | TK_INSERT TK_INTO table TK_LP collist TK_RP TK_VALUES TK_LP constlist TK_RP TK_TEMPORARY
147 {
148 SQL_input *sql = (SQL_input*) info;
149 MSIVIEW *insert = NULL;
150
151 INSERT_CreateView( sql->db, &insert, $3, $5, $9, TRUE );
152 if( !insert )
153 YYABORT;
154
155 PARSER_BUBBLE_UP_VIEW( sql, $$, insert );
156 }
157 ;
158
159 onecreate:
160 TK_CREATE TK_TABLE table TK_LP table_def TK_RP
161 {
162 SQL_input* sql = (SQL_input*) info;
163 MSIVIEW *create = NULL;
164 UINT r;
165
166 if( !$5 )
167 YYABORT;
168 r = CREATE_CreateView( sql->db, &create, $3, $5, FALSE );
169 if( !create )
170 {
171 sql->r = r;
172 YYABORT;
173 }
174
175 PARSER_BUBBLE_UP_VIEW( sql, $$, create );
176 }
177 | TK_CREATE TK_TABLE table TK_LP table_def TK_RP TK_HOLD
178 {
179 SQL_input* sql = (SQL_input*) info;
180 MSIVIEW *create = NULL;
181
182 if( !$5 )
183 YYABORT;
184 CREATE_CreateView( sql->db, &create, $3, $5, TRUE );
185 if( !create )
186 YYABORT;
187
188 PARSER_BUBBLE_UP_VIEW( sql, $$, create );
189 }
190 ;
191
192 oneupdate:
193 TK_UPDATE table TK_SET update_assign_list TK_WHERE expr
194 {
195 SQL_input* sql = (SQL_input*) info;
196 MSIVIEW *update = NULL;
197
198 UPDATE_CreateView( sql->db, &update, $2, $4, $6 );
199 if( !update )
200 YYABORT;
201
202 PARSER_BUBBLE_UP_VIEW( sql, $$, update );
203 }
204 | TK_UPDATE table TK_SET update_assign_list
205 {
206 SQL_input* sql = (SQL_input*) info;
207 MSIVIEW *update = NULL;
208
209 UPDATE_CreateView( sql->db, &update, $2, $4, NULL );
210 if( !update )
211 YYABORT;
212
213 PARSER_BUBBLE_UP_VIEW( sql, $$, update );
214 }
215 ;
216
217 onedelete:
218 TK_DELETE from
219 {
220 SQL_input* sql = (SQL_input*) info;
221 MSIVIEW *delete = NULL;
222
223 DELETE_CreateView( sql->db, &delete, $2 );
224 if( !delete )
225 YYABORT;
226
227 PARSER_BUBBLE_UP_VIEW( sql, $$, delete );
228 }
229 ;
230
231 onealter:
232 TK_ALTER TK_TABLE table alterop
233 {
234 SQL_input* sql = (SQL_input*) info;
235 MSIVIEW *alter = NULL;
236
237 ALTER_CreateView( sql->db, &alter, $3, NULL, $4 );
238 if( !alter )
239 YYABORT;
240
241 PARSER_BUBBLE_UP_VIEW( sql, $$, alter );
242 }
243 | TK_ALTER TK_TABLE table TK_ADD column_and_type
244 {
245 SQL_input *sql = (SQL_input *)info;
246 MSIVIEW *alter = NULL;
247
248 ALTER_CreateView( sql->db, &alter, $3, $5, 0 );
249 if (!alter)
250 YYABORT;
251
252 PARSER_BUBBLE_UP_VIEW( sql, $$, alter );
253 }
254 | TK_ALTER TK_TABLE table TK_ADD column_and_type TK_HOLD
255 {
256 SQL_input *sql = (SQL_input *)info;
257 MSIVIEW *alter = NULL;
258
259 ALTER_CreateView( sql->db, &alter, $3, $5, 1 );
260 if (!alter)
261 YYABORT;
262
263 PARSER_BUBBLE_UP_VIEW( sql, $$, alter );
264 }
265 ;
266
267 alterop:
268 TK_HOLD
269 {
270 $$ = 1;
271 }
272 | TK_FREE
273 {
274 $$ = -1;
275 }
276 ;
277
278 onedrop:
279 TK_DROP TK_TABLE table
280 {
281 SQL_input* sql = (SQL_input*) info;
282 MSIVIEW* drop = NULL;
283 UINT r;
284
285 r = DROP_CreateView( sql->db, &drop, $3 );
286 if( r != ERROR_SUCCESS || !$$ )
287 YYABORT;
288
289 PARSER_BUBBLE_UP_VIEW( sql, $$, drop );
290 }
291 ;
292
293 table_def:
294 column_def TK_PRIMARY TK_KEY collist
295 {
296 if( SQL_MarkPrimaryKeys( &$1, $4 ) )
297 $$ = $1;
298 else
299 $$ = NULL;
300 }
301 ;
302
303 column_def:
304 column_def TK_COMMA column_and_type
305 {
306 column_info *ci;
307
308 for( ci = $1; ci->next; ci = ci->next )
309 ;
310
311 ci->next = $3;
312 $$ = $1;
313 }
314 | column_and_type
315 {
316 $$ = $1;
317 }
318 ;
319
320 column_and_type:
321 column column_type
322 {
323 $$ = $1;
324 $$->type = ($2 | MSITYPE_VALID);
325 $$->temporary = $2 & MSITYPE_TEMPORARY ? TRUE : FALSE;
326 }
327 ;
328
329 column_type:
330 data_type_l
331 {
332 $$ = $1;
333 }
334 | data_type_l TK_LOCALIZABLE
335 {
336 $$ = $1 | MSITYPE_LOCALIZABLE;
337 }
338 | data_type_l TK_TEMPORARY
339 {
340 $$ = $1 | MSITYPE_TEMPORARY;
341 }
342 ;
343
344 data_type_l:
345 data_type
346 {
347 $$ |= MSITYPE_NULLABLE;
348 }
349 | data_type TK_NOT TK_NULL
350 {
351 $$ = $1;
352 }
353 ;
354
355 data_type:
356 TK_CHAR
357 {
358 $$ = MSITYPE_STRING | 1;
359 }
360 | TK_CHAR TK_LP data_count TK_RP
361 {
362 $$ = MSITYPE_STRING | 0x400 | $3;
363 }
364 | TK_LONGCHAR
365 {
366 $$ = MSITYPE_STRING | 0x400;
367 }
368 | TK_SHORT
369 {
370 $$ = 2 | 0x400;
371 }
372 | TK_INT
373 {
374 $$ = 2 | 0x400;
375 }
376 | TK_LONG
377 {
378 $$ = 4;
379 }
380 | TK_OBJECT
381 {
382 $$ = MSITYPE_STRING | MSITYPE_VALID;
383 }
384 ;
385
386 data_count:
387 number
388 {
389 if( ( $1 > 255 ) || ( $1 < 0 ) )
390 YYABORT;
391 $$ = $1;
392 }
393 ;
394
395 oneselect:
396 TK_SELECT selectfrom
397 {
398 $$ = $2;
399 }
400 | TK_SELECT TK_DISTINCT selectfrom
401 {
402 SQL_input* sql = (SQL_input*) info;
403 MSIVIEW* distinct = NULL;
404 UINT r;
405
406 r = DISTINCT_CreateView( sql->db, &distinct, $3 );
407 if (r != ERROR_SUCCESS)
408 YYABORT;
409
410 PARSER_BUBBLE_UP_VIEW( sql, $$, distinct );
411 }
412 ;
413
414 selectfrom:
415 selcollist from
416 {
417 SQL_input* sql = (SQL_input*) info;
418 MSIVIEW* select = NULL;
419 UINT r;
420
421 if( $1 )
422 {
423 r = SELECT_CreateView( sql->db, &select, $2, $1 );
424 if (r != ERROR_SUCCESS)
425 YYABORT;
426
427 PARSER_BUBBLE_UP_VIEW( sql, $$, select );
428 }
429 else
430 $$ = $2;
431 }
432 ;
433
434 selcollist:
435 selcolumn
436 | selcolumn TK_COMMA selcollist
437 {
438 $1->next = $3;
439 }
440 | TK_STAR
441 {
442 $$ = NULL;
443 }
444 ;
445
446 collist:
447 column
448 | column TK_COMMA collist
449 {
450 $1->next = $3;
451 }
452 | TK_STAR
453 {
454 $$ = NULL;
455 }
456 ;
457
458 from:
459 TK_FROM table
460 {
461 SQL_input* sql = (SQL_input*) info;
462 MSIVIEW* table = NULL;
463 UINT r;
464
465 r = TABLE_CreateView( sql->db, $2, &table );
466 if( r != ERROR_SUCCESS || !$$ )
467 YYABORT;
468
469 PARSER_BUBBLE_UP_VIEW( sql, $$, table );
470 }
471 | unorderdfrom TK_ORDER TK_BY collist
472 {
473 UINT r;
474
475 if( $4 )
476 {
477 r = $1->ops->sort( $1, $4 );
478 if ( r != ERROR_SUCCESS)
479 YYABORT;
480 }
481
482 $$ = $1;
483 }
484 | unorderdfrom
485 ;
486
487 unorderdfrom:
488 TK_FROM tablelist
489 {
490 SQL_input* sql = (SQL_input*) info;
491 MSIVIEW* where = NULL;
492 UINT r;
493
494 r = WHERE_CreateView( sql->db, &where, $2, NULL );
495 if( r != ERROR_SUCCESS )
496 YYABORT;
497
498 PARSER_BUBBLE_UP_VIEW( sql, $$, where );
499 }
500 | TK_FROM tablelist TK_WHERE expr
501 {
502 SQL_input* sql = (SQL_input*) info;
503 MSIVIEW* where = NULL;
504 UINT r;
505
506 r = WHERE_CreateView( sql->db, &where, $2, $4 );
507 if( r != ERROR_SUCCESS )
508 YYABORT;
509
510 PARSER_BUBBLE_UP_VIEW( sql, $$, where );
511 }
512 ;
513
514 tablelist:
515 table
516 {
517 $$ = $1;
518 }
519 | table TK_COMMA tablelist
520 {
521 $$ = parser_add_table( info, $3, $1 );
522 if (!$$)
523 YYABORT;
524 }
525 ;
526
527 expr:
528 TK_LP expr TK_RP
529 {
530 $$ = $2;
531 if( !$$ )
532 YYABORT;
533 }
534 | expr TK_AND expr
535 {
536 $$ = EXPR_complex( info, $1, OP_AND, $3 );
537 if( !$$ )
538 YYABORT;
539 }
540 | expr TK_OR expr
541 {
542 $$ = EXPR_complex( info, $1, OP_OR, $3 );
543 if( !$$ )
544 YYABORT;
545 }
546 | column_val TK_EQ val
547 {
548 $$ = EXPR_complex( info, $1, OP_EQ, $3 );
549 if( !$$ )
550 YYABORT;
551 }
552 | column_val TK_GT val
553 {
554 $$ = EXPR_complex( info, $1, OP_GT, $3 );
555 if( !$$ )
556 YYABORT;
557 }
558 | column_val TK_LT val
559 {
560 $$ = EXPR_complex( info, $1, OP_LT, $3 );
561 if( !$$ )
562 YYABORT;
563 }
564 | column_val TK_LE val
565 {
566 $$ = EXPR_complex( info, $1, OP_LE, $3 );
567 if( !$$ )
568 YYABORT;
569 }
570 | column_val TK_GE val
571 {
572 $$ = EXPR_complex( info, $1, OP_GE, $3 );
573 if( !$$ )
574 YYABORT;
575 }
576 | column_val TK_NE val
577 {
578 $$ = EXPR_complex( info, $1, OP_NE, $3 );
579 if( !$$ )
580 YYABORT;
581 }
582 | column_val TK_IS TK_NULL
583 {
584 $$ = EXPR_unary( info, $1, OP_ISNULL );
585 if( !$$ )
586 YYABORT;
587 }
588 | column_val TK_IS TK_NOT TK_NULL
589 {
590 $$ = EXPR_unary( info, $1, OP_NOTNULL );
591 if( !$$ )
592 YYABORT;
593 }
594 ;
595
596 val:
597 column_val
598 | const_val
599 ;
600
601 constlist:
602 const_val
603 {
604 $$ = parser_alloc_column( info, NULL, NULL );
605 if( !$$ )
606 YYABORT;
607 $$->val = $1;
608 }
609 | const_val TK_COMMA constlist
610 {
611 $$ = parser_alloc_column( info, NULL, NULL );
612 if( !$$ )
613 YYABORT;
614 $$->val = $1;
615 $$->next = $3;
616 }
617 ;
618
619 update_assign_list:
620 column_assignment
621 | column_assignment TK_COMMA update_assign_list
622 {
623 $$ = $1;
624 $$->next = $3;
625 }
626 ;
627
628 column_assignment:
629 column TK_EQ const_val
630 {
631 $$ = $1;
632 $$->val = $3;
633 }
634 ;
635
636 const_val:
637 number
638 {
639 $$ = EXPR_ival( info, $1 );
640 if( !$$ )
641 YYABORT;
642 }
643 | TK_MINUS number %prec TK_NEGATION
644 {
645 $$ = EXPR_ival( info, -$2 );
646 if( !$$ )
647 YYABORT;
648 }
649 | TK_STRING
650 {
651 $$ = EXPR_sval( info, &$1 );
652 if( !$$ )
653 YYABORT;
654 }
655 | TK_WILDCARD
656 {
657 $$ = EXPR_wildcard( info );
658 if( !$$ )
659 YYABORT;
660 }
661 ;
662
663 column_val:
664 column
665 {
666 $$ = EXPR_column( info, $1 );
667 if( !$$ )
668 YYABORT;
669 }
670 ;
671
672 column:
673 table TK_DOT id
674 {
675 $$ = parser_alloc_column( info, $1, $3 );
676 if( !$$ )
677 YYABORT;
678 }
679 | id
680 {
681 $$ = parser_alloc_column( info, NULL, $1 );
682 if( !$$ )
683 YYABORT;
684 }
685 ;
686
687 selcolumn:
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 | string
701 {
702 $$ = parser_alloc_column( info, NULL, $1 );
703 if( !$$ )
704 YYABORT;
705 }
706 ;
707
708 table:
709 id
710 {
711 $$ = $1;
712 }
713 ;
714
715 id:
716 TK_ID
717 {
718 if ( SQL_getstring( info, &$1, &$$ ) != ERROR_SUCCESS || !$$ )
719 YYABORT;
720 }
721 ;
722
723 string:
724 TK_STRING
725 {
726 if ( SQL_getstring( info, &$1, &$$ ) != ERROR_SUCCESS || !$$ )
727 YYABORT;
728 }
729 ;
730
731 number:
732 TK_INTEGER
733 {
734 $$ = SQL_getint( info );
735 }
736 ;
737
738 %%
739
740 static LPWSTR parser_add_table( void *info, LPCWSTR list, LPCWSTR table )
741 {
742 static const WCHAR space[] = {' ',0};
743 DWORD len = strlenW( list ) + strlenW( table ) + 2;
744 LPWSTR ret;
745
746 ret = parser_alloc( info, len * sizeof(WCHAR) );
747 if( ret )
748 {
749 strcpyW( ret, list );
750 strcatW( ret, space );
751 strcatW( ret, table );
752 }
753 return ret;
754 }
755
756 static void *parser_alloc( void *info, unsigned int sz )
757 {
758 SQL_input* sql = (SQL_input*) info;
759 struct list *mem;
760
761 mem = msi_alloc( sizeof (struct list) + sz );
762 list_add_tail( sql->mem, mem );
763 return &mem[1];
764 }
765
766 static column_info *parser_alloc_column( void *info, LPCWSTR table, LPCWSTR column )
767 {
768 column_info *col;
769
770 col = parser_alloc( info, sizeof (*col) );
771 if( col )
772 {
773 col->table = table;
774 col->column = column;
775 col->val = NULL;
776 col->type = 0;
777 col->next = NULL;
778 }
779
780 return col;
781 }
782
783 static int sql_lex( void *SQL_lval, SQL_input *sql )
784 {
785 int token, skip;
786 struct sql_str * str = SQL_lval;
787
788 do
789 {
790 sql->n += sql->len;
791 if( ! sql->command[sql->n] )
792 return 0; /* end of input */
793
794 /* TRACE("string : %s\n", debugstr_w(&sql->command[sql->n])); */
795 sql->len = sqliteGetToken( &sql->command[sql->n], &token, &skip );
796 if( sql->len==0 )
797 break;
798 str->data = &sql->command[sql->n];
799 str->len = sql->len;
800 sql->n += skip;
801 }
802 while( token == TK_SPACE );
803
804 /* TRACE("token : %d (%s)\n", token, debugstr_wn(&sql->command[sql->n], sql->len)); */
805
806 return token;
807 }
808
809 UINT SQL_getstring( void *info, const struct sql_str *strdata, LPWSTR *str )
810 {
811 LPCWSTR p = strdata->data;
812 UINT len = strdata->len;
813
814 /* match quotes */
815 if( ( (p[0]=='`') && (p[len-1]!='`') ) ||
816 ( (p[0]=='\'') && (p[len-1]!='\'') ) )
817 return ERROR_FUNCTION_FAILED;
818
819 /* if there are quotes, remove them */
820 if( ( (p[0]=='`') && (p[len-1]=='`') ) ||
821 ( (p[0]=='\'') && (p[len-1]=='\'') ) )
822 {
823 p++;
824 len -= 2;
825 }
826 *str = parser_alloc( info, (len + 1)*sizeof(WCHAR) );
827 if( !*str )
828 return ERROR_OUTOFMEMORY;
829 memcpy( *str, p, len*sizeof(WCHAR) );
830 (*str)[len]=0;
831
832 return ERROR_SUCCESS;
833 }
834
835 INT SQL_getint( void *info )
836 {
837 SQL_input* sql = (SQL_input*) info;
838 LPCWSTR p = &sql->command[sql->n];
839 INT i, r = 0;
840
841 for( i=0; i<sql->len; i++ )
842 {
843 if( '0' > p[i] || '9' < p[i] )
844 {
845 ERR("should only be numbers here!\n");
846 break;
847 }
848 r = (p[i]-'0') + r*10;
849 }
850
851 return r;
852 }
853
854 static int sql_error( SQL_input *info, const char *str )
855 {
856 return 0;
857 }
858
859 static struct expr * EXPR_wildcard( void *info )
860 {
861 struct expr *e = parser_alloc( info, sizeof *e );
862 if( e )
863 {
864 e->type = EXPR_WILDCARD;
865 }
866 return e;
867 }
868
869 static struct expr * EXPR_complex( void *info, struct expr *l, UINT op, struct expr *r )
870 {
871 struct expr *e = parser_alloc( info, sizeof *e );
872 if( e )
873 {
874 e->type = EXPR_COMPLEX;
875 e->u.expr.left = l;
876 e->u.expr.op = op;
877 e->u.expr.right = r;
878 }
879 return e;
880 }
881
882 static struct expr * EXPR_unary( void *info, struct expr *l, UINT op )
883 {
884 struct expr *e = parser_alloc( info, sizeof *e );
885 if( e )
886 {
887 e->type = EXPR_UNARY;
888 e->u.expr.left = l;
889 e->u.expr.op = op;
890 e->u.expr.right = NULL;
891 }
892 return e;
893 }
894
895 static struct expr * EXPR_column( void *info, const column_info *column )
896 {
897 struct expr *e = parser_alloc( info, sizeof *e );
898 if( e )
899 {
900 e->type = EXPR_COLUMN;
901 e->u.column.unparsed.column = column->column;
902 e->u.column.unparsed.table = column->table;
903 }
904 return e;
905 }
906
907 static struct expr * EXPR_ival( void *info, int val )
908 {
909 struct expr *e = parser_alloc( info, sizeof *e );
910 if( e )
911 {
912 e->type = EXPR_IVAL;
913 e->u.ival = val;
914 }
915 return e;
916 }
917
918 static struct expr * EXPR_sval( void *info, const struct sql_str *str )
919 {
920 struct expr *e = parser_alloc( info, sizeof *e );
921 if( e )
922 {
923 e->type = EXPR_SVAL;
924 if( SQL_getstring( info, str, (LPWSTR *)&e->u.sval ) != ERROR_SUCCESS )
925 return NULL; /* e will be freed by query destructor */
926 }
927 return e;
928 }
929
930 static void swap_columns( column_info **cols, column_info *A, int idx )
931 {
932 column_info *preA = NULL, *preB = NULL, *B, *ptr;
933 int i = 0;
934
935 B = NULL;
936 ptr = *cols;
937 while( ptr )
938 {
939 if( i++ == idx )
940 B = ptr;
941 else if( !B )
942 preB = ptr;
943
944 if( ptr->next == A )
945 preA = ptr;
946
947 ptr = ptr->next;
948 }
949
950 if( preB ) preB->next = A;
951 if( preA ) preA->next = B;
952 ptr = A->next;
953 A->next = B->next;
954 B->next = ptr;
955 if( idx == 0 )
956 *cols = A;
957 }
958
959 static BOOL SQL_MarkPrimaryKeys( column_info **cols,
960 column_info *keys )
961 {
962 column_info *k;
963 BOOL found = TRUE;
964 int count;
965
966 for( k = keys, count = 0; k && found; k = k->next, count++ )
967 {
968 column_info *c;
969 int idx;
970
971 found = FALSE;
972 for( c = *cols, idx = 0; c && !found; c = c->next, idx++ )
973 {
974 if( strcmpW( k->column, c->column ) )
975 continue;
976 c->type |= MSITYPE_KEY;
977 found = TRUE;
978 if (idx != count)
979 swap_columns( cols, c, count );
980 }
981 }
982
983 return found;
984 }
985
986 UINT MSI_ParseSQL( MSIDATABASE *db, LPCWSTR command, MSIVIEW **phview,
987 struct list *mem )
988 {
989 SQL_input sql;
990 int r;
991
992 *phview = NULL;
993
994 sql.db = db;
995 sql.command = command;
996 sql.n = 0;
997 sql.len = 0;
998 sql.r = ERROR_BAD_QUERY_SYNTAX;
999 sql.view = phview;
1000 sql.mem = mem;
1001
1002 r = sql_parse(&sql);
1003
1004 TRACE("Parse returned %d\n", r);
1005 if( r )
1006 {
1007 if (*sql.view)
1008 {
1009 (*sql.view)->ops->delete(*sql.view);
1010 *sql.view = NULL;
1011 }
1012 return sql.r;
1013 }
1014
1015 return ERROR_SUCCESS;
1016 }