wgl: Fix wglGetProcAddress bug.
[wine/hacks.git] / dlls / msi / sql.y
blob17cb95714c8c8fb6b92034d8418ab1054fc47a52
1 %{
3 /*
4 * Implementation of the Microsoft Installer (msi.dll)
6 * Copyright 2002-2004 Mike McCormack for CodeWeavers
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.
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.
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
24 #include "config.h"
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
30 #include "windef.h"
31 #include "winbase.h"
32 #include "query.h"
33 #include "wine/list.h"
34 #include "wine/debug.h"
36 #define YYLEX_PARAM info
37 #define YYPARSE_PARAM info
39 static int sql_error(const char *str);
41 WINE_DEFAULT_DEBUG_CHANNEL(msi);
43 typedef struct tag_SQL_input
45 MSIDATABASE *db;
46 LPCWSTR command;
47 DWORD n, len;
48 MSIVIEW **view; /* view structure for the resulting query */
49 struct list *mem;
50 } SQL_input;
52 static LPWSTR SQL_getstring( void *info, struct sql_str *str );
53 static INT SQL_getint( void *info );
54 static int sql_lex( void *SQL_lval, SQL_input *info );
56 static void *parser_alloc( void *info, unsigned int sz );
57 static column_info *parser_alloc_column( void *info, LPCWSTR table, LPCWSTR column );
59 static BOOL SQL_MarkPrimaryKeys( column_info *cols, column_info *keys);
61 static struct expr * EXPR_complex( void *info, struct expr *l, UINT op, struct expr *r );
62 static struct expr * EXPR_unary( void *info, struct expr *l, UINT op );
63 static struct expr * EXPR_column( void *info, column_info *column );
64 static struct expr * EXPR_ival( void *info, int val );
65 static struct expr * EXPR_sval( void *info, struct sql_str * );
66 static struct expr * EXPR_wildcard( void *info );
70 %pure-parser
72 %union
74 struct sql_str str;
75 LPWSTR string;
76 column_info *column_list;
77 MSIVIEW *query;
78 struct expr *expr;
79 USHORT column_type;
80 int integer;
83 %token TK_ALTER TK_AND TK_BY TK_CHAR TK_COMMA TK_CREATE TK_DELETE
84 %token TK_DISTINCT TK_DOT TK_EQ TK_FREE TK_FROM TK_GE TK_GT TK_HOLD
85 %token <str> TK_ID
86 %token TK_ILLEGAL TK_INSERT TK_INT
87 %token <str> TK_INTEGER
88 %token TK_INTO TK_IS TK_KEY TK_LE TK_LONG TK_LONGCHAR TK_LP TK_LT
89 %token TK_LOCALIZABLE TK_MINUS TK_NE TK_NOT TK_NULL
90 %token TK_OBJECT TK_OR TK_ORDER TK_PRIMARY TK_RP
91 %token TK_SELECT TK_SET TK_SHORT TK_SPACE TK_STAR
92 %token <str> TK_STRING
93 %token TK_TABLE TK_TEMPORARY TK_UPDATE TK_VALUES TK_WHERE TK_WILDCARD
96 * These are extra tokens used by the lexer but never seen by the
97 * parser. We put them in a rule so that the parser generator will
98 * add them to the parse.h output file.
101 %nonassoc END_OF_FILE ILLEGAL SPACE UNCLOSED_STRING COMMENT FUNCTION
102 COLUMN AGG_FUNCTION.
104 %type <string> table id
105 %type <column_list> selcollist column column_and_type column_def table_def
106 %type <column_list> column_assignment update_assign_list constlist
107 %type <query> query from fromtable selectfrom unorderedsel
108 %type <query> oneupdate onedelete oneselect onequery onecreate oneinsert onealter
109 %type <expr> expr val column_val const_val
110 %type <column_type> column_type data_type data_type_l data_count
111 %type <integer> number alterop
113 /* Reference: http://mates.ms.mff.cuni.cz/oracle/doc/ora815nt/server.815/a67779/operator.htm */
114 %left TK_OR
115 %left TK_AND
116 %left TK_NOT
117 %left TK_EQ TK_NE TK_LT TK_GT TK_LE TK_GE TK_LIKE
118 %right TK_NEGATION
122 query:
123 onequery
125 SQL_input* sql = (SQL_input*) info;
126 *sql->view = $1;
130 onequery:
131 oneselect
132 | onecreate
133 | oneinsert
134 | oneupdate
135 | onedelete
136 | onealter
139 oneinsert:
140 TK_INSERT TK_INTO table TK_LP selcollist TK_RP TK_VALUES TK_LP constlist TK_RP
142 SQL_input *sql = (SQL_input*) info;
143 MSIVIEW *insert = NULL;
144 UINT r;
146 r = INSERT_CreateView( sql->db, &insert, $3, $5, $9, FALSE );
147 if( !insert )
148 YYABORT;
149 $$ = insert;
151 | TK_INSERT TK_INTO table TK_LP selcollist TK_RP TK_VALUES TK_LP constlist TK_RP TK_TEMPORARY
153 SQL_input *sql = (SQL_input*) info;
154 MSIVIEW *insert = NULL;
156 INSERT_CreateView( sql->db, &insert, $3, $5, $9, TRUE );
157 if( !insert )
158 YYABORT;
159 $$ = insert;
163 onecreate:
164 TK_CREATE TK_TABLE table TK_LP table_def TK_RP
166 SQL_input* sql = (SQL_input*) info;
167 MSIVIEW *create = NULL;
169 if( !$5 )
170 YYABORT;
171 CREATE_CreateView( sql->db, &create, $3, $5, FALSE );
172 if( !create )
173 YYABORT;
174 $$ = create;
176 | TK_CREATE TK_TABLE table TK_LP table_def TK_RP TK_HOLD
178 SQL_input* sql = (SQL_input*) info;
179 MSIVIEW *create = NULL;
181 if( !$5 )
182 YYABORT;
183 CREATE_CreateView( sql->db, &create, $3, $5, TRUE );
184 if( !create )
185 YYABORT;
186 $$ = create;
190 oneupdate:
191 TK_UPDATE table TK_SET update_assign_list TK_WHERE expr
193 SQL_input* sql = (SQL_input*) info;
194 MSIVIEW *update = NULL;
196 UPDATE_CreateView( sql->db, &update, $2, $4, $6 );
197 if( !update )
198 YYABORT;
199 $$ = update;
201 | TK_UPDATE table TK_SET update_assign_list
203 SQL_input* sql = (SQL_input*) info;
204 MSIVIEW *update = NULL;
206 UPDATE_CreateView( sql->db, &update, $2, $4, NULL );
207 if( !update )
208 YYABORT;
209 $$ = update;
213 onedelete:
214 TK_DELETE from
216 SQL_input* sql = (SQL_input*) info;
217 MSIVIEW *delete = NULL;
219 DELETE_CreateView( sql->db, &delete, $2 );
220 if( !delete )
221 YYABORT;
222 $$ = delete;
226 onealter:
227 TK_ALTER TK_TABLE table alterop
229 SQL_input* sql = (SQL_input*) info;
230 MSIVIEW *alter = NULL;
232 ALTER_CreateView( sql->db, &alter, $3, $4 );
233 if( !alter )
234 YYABORT;
235 $$ = alter;
239 alterop:
240 TK_HOLD
242 $$ = 1;
244 | TK_FREE
246 $$ = -1;
250 table_def:
251 column_def TK_PRIMARY TK_KEY selcollist
253 if( SQL_MarkPrimaryKeys( $1, $4 ) )
254 $$ = $1;
255 else
256 $$ = NULL;
260 column_def:
261 column_def TK_COMMA column_and_type
263 column_info *ci;
265 for( ci = $1; ci->next; ci = ci->next )
268 ci->next = $3;
269 $$ = $1;
271 | column_and_type
273 $$ = $1;
277 column_and_type:
278 column column_type
280 $$ = $1;
281 $$->type = $2 | MSITYPE_VALID;
285 column_type:
286 data_type_l
288 $$ = $1;
290 | data_type_l TK_LOCALIZABLE
292 $$ = $1 | MSITYPE_LOCALIZABLE;
294 | data_type_l TK_TEMPORARY
296 FIXME("temporary column\n");
300 data_type_l:
301 data_type
303 $$ |= MSITYPE_NULLABLE;
305 | data_type TK_NOT TK_NULL
307 $$ = $1;
311 data_type:
312 TK_CHAR
314 $$ = MSITYPE_STRING | 1;
316 | TK_CHAR TK_LP data_count TK_RP
318 $$ = MSITYPE_STRING | 0x400 | $3;
320 | TK_LONGCHAR
322 $$ = 2;
324 | TK_SHORT
326 $$ = 2;
328 | TK_INT
330 $$ = 2;
332 | TK_LONG
334 $$ = 4;
336 | TK_OBJECT
338 $$ = MSITYPE_STRING | MSITYPE_VALID;
342 data_count:
343 number
345 if( ( $1 > 255 ) || ( $1 < 0 ) )
346 YYABORT;
347 $$ = $1;
351 oneselect:
352 unorderedsel TK_ORDER TK_BY selcollist
354 SQL_input* sql = (SQL_input*) info;
356 $$ = NULL;
357 if( $4 )
358 ORDER_CreateView( sql->db, &$$, $1, $4 );
359 else
360 $$ = $1;
361 if( !$$ )
362 YYABORT;
364 | unorderedsel
367 unorderedsel:
368 TK_SELECT selectfrom
370 $$ = $2;
372 | TK_SELECT TK_DISTINCT selectfrom
374 SQL_input* sql = (SQL_input*) info;
375 UINT r;
377 $$ = NULL;
378 r = DISTINCT_CreateView( sql->db, &$$, $3 );
379 if (r != ERROR_SUCCESS)
381 $3->ops->delete($3);
382 YYABORT;
387 selectfrom:
388 selcollist from
390 SQL_input* sql = (SQL_input*) info;
391 UINT r;
393 $$ = NULL;
394 if( $1 )
396 r = SELECT_CreateView( sql->db, &$$, $2, $1 );
397 if (r != ERROR_SUCCESS)
399 $2->ops->delete($2);
400 YYABORT;
403 else
404 $$ = $2;
408 selcollist:
409 column
410 | column TK_COMMA selcollist
412 $1->next = $3;
414 | TK_STAR
416 $$ = NULL;
420 from:
421 fromtable
422 | fromtable TK_WHERE expr
424 SQL_input* sql = (SQL_input*) info;
425 UINT r;
427 $$ = NULL;
428 r = WHERE_CreateView( sql->db, &$$, $1, $3 );
429 if( r != ERROR_SUCCESS )
431 $1->ops->delete( $1 );
432 YYABORT;
437 fromtable:
438 TK_FROM table
440 SQL_input* sql = (SQL_input*) info;
441 UINT r;
443 $$ = NULL;
444 r = TABLE_CreateView( sql->db, $2, &$$ );
445 if( r != ERROR_SUCCESS || !$$ )
446 YYABORT;
448 | TK_FROM table TK_COMMA table
450 SQL_input* sql = (SQL_input*) info;
451 UINT r;
453 /* only support inner joins on two tables */
454 r = JOIN_CreateView( sql->db, &$$, $2, $4 );
455 if( r != ERROR_SUCCESS )
456 YYABORT;
460 expr:
461 TK_LP expr TK_RP
463 $$ = $2;
464 if( !$$ )
465 YYABORT;
467 | expr TK_AND expr
469 $$ = EXPR_complex( info, $1, OP_AND, $3 );
470 if( !$$ )
471 YYABORT;
473 | expr TK_OR expr
475 $$ = EXPR_complex( info, $1, OP_OR, $3 );
476 if( !$$ )
477 YYABORT;
479 | column_val TK_EQ val
481 $$ = EXPR_complex( info, $1, OP_EQ, $3 );
482 if( !$$ )
483 YYABORT;
485 | column_val TK_GT val
487 $$ = EXPR_complex( info, $1, OP_GT, $3 );
488 if( !$$ )
489 YYABORT;
491 | column_val TK_LT val
493 $$ = EXPR_complex( info, $1, OP_LT, $3 );
494 if( !$$ )
495 YYABORT;
497 | column_val TK_LE val
499 $$ = EXPR_complex( info, $1, OP_LE, $3 );
500 if( !$$ )
501 YYABORT;
503 | column_val TK_GE val
505 $$ = EXPR_complex( info, $1, OP_GE, $3 );
506 if( !$$ )
507 YYABORT;
509 | column_val TK_NE val
511 $$ = EXPR_complex( info, $1, OP_NE, $3 );
512 if( !$$ )
513 YYABORT;
515 | column_val TK_IS TK_NULL
517 $$ = EXPR_unary( info, $1, OP_ISNULL );
518 if( !$$ )
519 YYABORT;
521 | column_val TK_IS TK_NOT TK_NULL
523 $$ = EXPR_unary( info, $1, OP_NOTNULL );
524 if( !$$ )
525 YYABORT;
529 val:
530 column_val
531 | const_val
534 constlist:
535 const_val
537 $$ = parser_alloc_column( info, NULL, NULL );
538 if( !$$ )
539 YYABORT;
540 $$->val = $1;
542 | const_val TK_COMMA constlist
544 $$ = parser_alloc_column( info, NULL, NULL );
545 if( !$$ )
546 YYABORT;
547 $$->val = $1;
548 $$->next = $3;
552 update_assign_list:
553 column_assignment
554 | column_assignment TK_COMMA update_assign_list
556 $$ = $1;
557 $$->next = $3;
561 column_assignment:
562 column TK_EQ const_val
564 $$ = $1;
565 $$->val = $3;
569 const_val:
570 number
572 $$ = EXPR_ival( info, $1 );
573 if( !$$ )
574 YYABORT;
576 | TK_MINUS number %prec TK_NEGATION
578 $$ = EXPR_ival( info, -$2 );
579 if( !$$ )
580 YYABORT;
582 | TK_STRING
584 $$ = EXPR_sval( info, &$1 );
585 if( !$$ )
586 YYABORT;
588 | TK_WILDCARD
590 $$ = EXPR_wildcard( info );
591 if( !$$ )
592 YYABORT;
596 column_val:
597 column
599 $$ = EXPR_column( info, $1 );
600 if( !$$ )
601 YYABORT;
605 column:
606 table TK_DOT id
608 $$ = parser_alloc_column( info, $1, $3 );
609 if( !$$ )
610 YYABORT;
612 | id
614 $$ = parser_alloc_column( info, NULL, $1 );
615 if( !$$ )
616 YYABORT;
620 table:
623 $$ = $1;
628 TK_ID
630 $$ = SQL_getstring( info, &$1 );
631 if( !$$ )
632 YYABORT;
636 number:
637 TK_INTEGER
639 $$ = SQL_getint( info );
645 static void *parser_alloc( void *info, unsigned int sz )
647 SQL_input* sql = (SQL_input*) info;
648 struct list *mem;
650 mem = msi_alloc( sizeof (struct list) + sz );
651 list_add_tail( sql->mem, mem );
652 return &mem[1];
655 static column_info *parser_alloc_column( void *info, LPCWSTR table, LPCWSTR column )
657 column_info *col;
659 col = parser_alloc( info, sizeof (*col) );
660 if( col )
662 col->table = table;
663 col->column = column;
664 col->val = NULL;
665 col->type = 0;
666 col->next = NULL;
669 return col;
672 static int sql_lex( void *SQL_lval, SQL_input *sql )
674 int token;
675 struct sql_str * str = SQL_lval;
679 sql->n += sql->len;
680 if( ! sql->command[sql->n] )
681 return 0; /* end of input */
683 /* TRACE("string : %s\n", debugstr_w(&sql->command[sql->n])); */
684 sql->len = sqliteGetToken( &sql->command[sql->n], &token );
685 if( sql->len==0 )
686 break;
687 str->data = &sql->command[sql->n];
688 str->len = sql->len;
690 while( token == TK_SPACE );
692 /* TRACE("token : %d (%s)\n", token, debugstr_wn(&sql->command[sql->n], sql->len)); */
694 return token;
697 LPWSTR SQL_getstring( void *info, struct sql_str *strdata )
699 LPCWSTR p = strdata->data;
700 UINT len = strdata->len;
701 LPWSTR str;
703 /* if there's quotes, remove them */
704 if( ( (p[0]=='`') && (p[len-1]=='`') ) ||
705 ( (p[0]=='\'') && (p[len-1]=='\'') ) )
707 p++;
708 len -= 2;
710 str = parser_alloc( info, (len + 1)*sizeof(WCHAR) );
711 if( !str )
712 return str;
713 memcpy( str, p, len*sizeof(WCHAR) );
714 str[len]=0;
716 return str;
719 INT SQL_getint( void *info )
721 SQL_input* sql = (SQL_input*) info;
722 LPCWSTR p = &sql->command[sql->n];
723 INT i, r = 0;
725 for( i=0; i<sql->len; i++ )
727 if( '0' > p[i] || '9' < p[i] )
729 ERR("should only be numbers here!\n");
730 break;
732 r = (p[i]-'0') + r*10;
735 return r;
738 static int sql_error( const char *str )
740 return 0;
743 static struct expr * EXPR_wildcard( void *info )
745 struct expr *e = parser_alloc( info, sizeof *e );
746 if( e )
748 e->type = EXPR_WILDCARD;
750 return e;
753 static struct expr * EXPR_complex( void *info, struct expr *l, UINT op, struct expr *r )
755 struct expr *e = parser_alloc( info, sizeof *e );
756 if( e )
758 e->type = EXPR_COMPLEX;
759 e->u.expr.left = l;
760 e->u.expr.op = op;
761 e->u.expr.right = r;
763 return e;
766 static struct expr * EXPR_unary( void *info, struct expr *l, UINT op )
768 struct expr *e = parser_alloc( info, sizeof *e );
769 if( e )
771 e->type = EXPR_UNARY;
772 e->u.expr.left = l;
773 e->u.expr.op = op;
774 e->u.expr.right = NULL;
776 return e;
779 static struct expr * EXPR_column( void *info, column_info *column )
781 struct expr *e = parser_alloc( info, sizeof *e );
782 if( e )
784 e->type = EXPR_COLUMN;
785 e->u.sval = column->column;
787 return e;
790 static struct expr * EXPR_ival( void *info, int val )
792 struct expr *e = parser_alloc( info, sizeof *e );
793 if( e )
795 e->type = EXPR_IVAL;
796 e->u.ival = val;
798 return e;
801 static struct expr * EXPR_sval( void *info, struct sql_str *str )
803 struct expr *e = parser_alloc( info, sizeof *e );
804 if( e )
806 e->type = EXPR_SVAL;
807 e->u.sval = SQL_getstring( info, str );
809 return e;
812 static BOOL SQL_MarkPrimaryKeys( column_info *cols,
813 column_info *keys )
815 column_info *k;
816 BOOL found = TRUE;
818 for( k = keys; k && found; k = k->next )
820 column_info *c;
822 found = FALSE;
823 for( c = cols; c && !found; c = c->next )
825 if( lstrcmpW( k->column, c->column ) )
826 continue;
827 c->type |= MSITYPE_KEY;
828 found = TRUE;
832 return found;
835 UINT MSI_ParseSQL( MSIDATABASE *db, LPCWSTR command, MSIVIEW **phview,
836 struct list *mem )
838 SQL_input sql;
839 int r;
841 *phview = NULL;
843 sql.db = db;
844 sql.command = command;
845 sql.n = 0;
846 sql.len = 0;
847 sql.view = phview;
848 sql.mem = mem;
850 r = sql_parse(&sql);
852 TRACE("Parse returned %d\n", r);
853 if( r )
855 *sql.view = NULL;
856 return ERROR_BAD_QUERY_SYNTAX;
859 return ERROR_SUCCESS;