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