Fix definition of LookupPrivilegeNameW and declare it.
[wine/multimedia.git] / dlls / msi / sql.y
blob18600edb49dbaed6637ba8e60aa37a364edabe2d
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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/debug.h"
34 #include "wine/unicode.h"
36 #define YYLEX_PARAM info
37 #define YYPARSE_PARAM info
39 extern 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 } SQL_input;
51 static LPWSTR SQL_getstring( struct sql_str *str );
52 static INT SQL_getint( SQL_input *sql );
53 static int SQL_lex( void *SQL_lval, SQL_input *info);
55 static MSIVIEW *do_one_select( MSIDATABASE *db, MSIVIEW *in,
56 string_list *columns );
57 static MSIVIEW *do_order_by( MSIDATABASE *db, MSIVIEW *in,
58 string_list *columns );
60 static BOOL SQL_MarkPrimaryKeys( create_col_info *cols,
61 string_list *keys);
63 static struct expr * EXPR_complex( struct expr *l, UINT op, struct expr *r );
64 static struct expr * EXPR_column( LPWSTR );
65 static struct expr * EXPR_ival( struct sql_str *);
66 static struct expr * EXPR_sval( struct sql_str *);
67 static struct expr * EXPR_wildcard();
71 %pure-parser
73 %union
75 struct sql_str str;
76 LPWSTR string;
77 string_list *column_list;
78 value_list *val_list;
79 MSIVIEW *query;
80 struct expr *expr;
81 USHORT column_type;
82 create_col_info *column_info;
83 column_assignment update_col_info;
86 %token TK_ABORT TK_AFTER TK_AGG_FUNCTION TK_ALL TK_AND TK_AS TK_ASC
87 %token TK_BEFORE TK_BEGIN TK_BETWEEN TK_BITAND TK_BITNOT TK_BITOR TK_BY
88 %token TK_CASCADE TK_CASE TK_CHAR TK_CHECK TK_CLUSTER TK_COLLATE TK_COLUMN
89 %token TK_COMMA TK_COMMENT TK_COMMIT TK_CONCAT TK_CONFLICT
90 %token TK_CONSTRAINT TK_COPY TK_CREATE
91 %token TK_DEFAULT TK_DEFERRABLE TK_DEFERRED TK_DELETE TK_DELIMITERS TK_DESC
92 %token TK_DISTINCT TK_DOT TK_DROP TK_EACH
93 %token TK_ELSE TK_END TK_END_OF_FILE TK_EQ TK_EXCEPT TK_EXPLAIN
94 %token TK_FAIL TK_FLOAT TK_FOR TK_FOREIGN TK_FROM TK_FUNCTION
95 %token TK_GE TK_GLOB TK_GROUP TK_GT
96 %token TK_HAVING TK_HOLD
97 %token TK_IGNORE TK_ILLEGAL TK_IMMEDIATE TK_IN TK_INDEX TK_INITIALLY
98 %token <str> TK_ID
99 %token TK_INSERT TK_INSTEAD TK_INT
100 %token <str> TK_INTEGER
101 %token TK_INTERSECT TK_INTO TK_IS
102 %token TK_ISNULL
103 %token TK_JOIN TK_JOIN_KW
104 %token TK_KEY
105 %token TK_LE TK_LIKE TK_LIMIT TK_LONG TK_LONGCHAR TK_LP TK_LSHIFT TK_LT
106 %token TK_LOCALIZABLE
107 %token TK_MATCH TK_MINUS
108 %token TK_NE TK_NOT TK_NOTNULL TK_NULL
109 %token TK_OBJECT TK_OF TK_OFFSET TK_ON TK_OR TK_ORACLE_OUTER_JOIN TK_ORDER
110 %token TK_PLUS TK_PRAGMA TK_PRIMARY
111 %token TK_RAISE TK_REFERENCES TK_REM TK_REPLACE TK_RESTRICT TK_ROLLBACK
112 %token TK_ROW TK_RP TK_RSHIFT
113 %token TK_SELECT TK_SEMI TK_SET TK_SHORT TK_SLASH TK_SPACE TK_STAR TK_STATEMENT
114 %token <str> TK_STRING
115 %token TK_TABLE TK_TEMP TK_THEN TK_TRANSACTION TK_TRIGGER
116 %token TK_UMINUS TK_UNCLOSED_STRING TK_UNION TK_UNIQUE
117 %token TK_UPDATE TK_UPLUS TK_USING
118 %token TK_VACUUM TK_VALUES TK_VIEW
119 %token TK_WHEN TK_WHERE TK_WILDCARD
122 * These are extra tokens used by the lexer but never seen by the
123 * parser. We put them in a rule so that the parser generator will
124 * add them to the parse.h output file.
127 %nonassoc END_OF_FILE ILLEGAL SPACE UNCLOSED_STRING COMMENT FUNCTION
128 COLUMN AGG_FUNCTION.
130 %type <string> column table string_or_id
131 %type <column_list> selcollist
132 %type <query> from unorderedsel oneselect onequery onecreate oneinsert oneupdate
133 %type <expr> expr val column_val const_val
134 %type <column_type> column_type data_type data_type_l data_count
135 %type <column_info> column_def table_def
136 %type <val_list> constlist
137 %type <update_col_info> column_assignment update_assign_list
141 onequery:
142 oneselect
144 SQL_input* sql = (SQL_input*) info;
145 *sql->view = $1;
147 | onecreate
149 SQL_input* sql = (SQL_input*) info;
150 *sql->view = $1;
152 | oneinsert
154 SQL_input* sql = (SQL_input*) info;
155 *sql->view = $1;
157 | oneupdate
159 SQL_input* sql = (SQL_input*) info;
160 *sql->view = $1;
164 oneinsert:
165 TK_INSERT TK_INTO table TK_LP selcollist TK_RP TK_VALUES TK_LP constlist TK_RP
167 SQL_input *sql = (SQL_input*) info;
168 MSIVIEW *insert = NULL;
170 INSERT_CreateView( sql->db, &insert, $3, $5, $9, FALSE );
171 $$ = insert;
173 | TK_INSERT TK_INTO table TK_LP selcollist TK_RP TK_VALUES TK_LP constlist TK_RP TK_TEMP
175 SQL_input *sql = (SQL_input*) info;
176 MSIVIEW *insert = NULL;
178 INSERT_CreateView( sql->db, &insert, $3, $5, $9, TRUE );
179 $$ = insert;
183 onecreate:
184 TK_CREATE TK_TABLE table TK_LP table_def TK_RP
186 SQL_input* sql = (SQL_input*) info;
187 MSIVIEW *create = NULL;
189 if( !$5 )
190 YYABORT;
191 CREATE_CreateView( sql->db, &create, $3, $5, FALSE );
192 $$ = create;
194 | TK_CREATE TK_TABLE table TK_LP table_def TK_RP TK_HOLD
196 SQL_input* sql = (SQL_input*) info;
197 MSIVIEW *create = NULL;
199 if( !$5 )
200 YYABORT;
201 CREATE_CreateView( sql->db, &create, $3, $5, TRUE );
202 $$ = create;
206 oneupdate:
207 TK_UPDATE table TK_SET update_assign_list TK_WHERE expr
209 SQL_input* sql = (SQL_input*) info;
210 MSIVIEW *update = NULL;
212 UPDATE_CreateView( sql->db, &update, $2, &$4, $6 );
213 $$ = update;
217 table_def:
218 column_def TK_PRIMARY TK_KEY selcollist
220 if( SQL_MarkPrimaryKeys( $1, $4 ) )
221 $$ = $1;
222 else
223 $$ = NULL;
227 column_def:
228 column_def TK_COMMA column column_type
230 create_col_info *ci;
232 for( ci = $1; ci->next; ci = ci->next )
235 ci->next = HeapAlloc( GetProcessHeap(), 0, sizeof *$$ );
236 if( !ci->next )
238 /* FIXME: free $1 */
239 YYABORT;
241 ci->next->colname = $3;
242 ci->next->type = $4;
243 ci->next->next = NULL;
245 $$ = $1;
247 | column column_type
249 $$ = HeapAlloc( GetProcessHeap(), 0, sizeof *$$ );
250 if( ! $$ )
251 YYABORT;
252 $$->colname = $1;
253 $$->type = $2;
254 $$->next = NULL;
258 column_type:
259 data_type_l
261 $$ = $1 | MSITYPE_VALID;
263 | data_type_l TK_LOCALIZABLE
265 FIXME("LOCALIZABLE ignored\n");
266 $$ = $1 | MSITYPE_VALID;
270 data_type_l:
271 data_type
273 $$ |= MSITYPE_NULLABLE;
275 | data_type TK_NOT TK_NULL
277 $$ = $1;
281 data_type:
282 TK_CHAR
284 $$ = MSITYPE_STRING | 1;
286 | TK_CHAR TK_LP data_count TK_RP
288 $$ = MSITYPE_STRING | 0x400 | $3;
290 | TK_LONGCHAR
292 $$ = 2;
294 | TK_SHORT
296 $$ = 2;
298 | TK_INT
300 $$ = 2;
302 | TK_LONG
304 $$ = 4;
306 | TK_OBJECT
308 $$ = 0;
312 data_count:
313 TK_INTEGER
315 SQL_input* sql = (SQL_input*) info;
316 int val = SQL_getint(sql);
317 if( ( val > 255 ) || ( val < 0 ) )
318 YYABORT;
319 $$ = val;
323 oneselect:
324 unorderedsel TK_ORDER TK_BY selcollist
326 SQL_input* sql = (SQL_input*) info;
328 if( !$1 )
329 YYABORT;
330 if( $4 )
331 $$ = do_order_by( sql->db, $1, $4 );
332 else
333 $$ = $1;
335 | unorderedsel
338 unorderedsel:
339 TK_SELECT selcollist from
341 SQL_input* sql = (SQL_input*) info;
342 if( !$3 )
343 YYABORT;
344 if( $2 )
346 $$ = do_one_select( sql->db, $3, $2 );
347 if( !$$ )
348 YYABORT;
350 else
351 $$ = $3;
353 | TK_SELECT TK_DISTINCT selcollist from
355 SQL_input* sql = (SQL_input*) info;
356 MSIVIEW *view = $4;
358 if( !view )
359 YYABORT;
360 if( $3 )
362 view = do_one_select( sql->db, view, $3 );
363 if( !view )
364 YYABORT;
366 DISTINCT_CreateView( sql->db, & $$, view );
370 selcollist:
371 column
373 string_list *list;
375 list = HeapAlloc( GetProcessHeap(), 0, sizeof *list );
376 if( !list )
377 YYABORT;
378 list->string = $1;
379 list->next = NULL;
381 $$ = list;
382 TRACE("Collist %s\n",debugstr_w($$->string));
384 | column TK_COMMA selcollist
386 string_list *list;
388 list = HeapAlloc( GetProcessHeap(), 0, sizeof *list );
389 if( !list )
390 YYABORT;
391 list->string = $1;
392 list->next = $3;
394 $$ = list;
395 TRACE("From table: %s\n",debugstr_w($$->string));
397 | TK_STAR
399 $$ = NULL;
403 from:
404 TK_FROM table
406 SQL_input* sql = (SQL_input*) info;
407 UINT r;
409 $$ = NULL;
410 TRACE("From table: %s\n",debugstr_w($2));
411 r = TABLE_CreateView( sql->db, $2, & $$ );
412 if( r != ERROR_SUCCESS )
413 YYABORT;
415 | TK_FROM table TK_WHERE expr
417 SQL_input* sql = (SQL_input*) info;
418 MSIVIEW *view = NULL;
419 UINT r;
421 $$ = NULL;
422 TRACE("From table: %s\n",debugstr_w($2));
423 r = TABLE_CreateView( sql->db, $2, &view );
424 if( r != ERROR_SUCCESS )
425 YYABORT;
426 r = WHERE_CreateView( sql->db, &view, view, $4 );
427 if( r != ERROR_SUCCESS )
428 YYABORT;
429 $$ = view;
433 expr:
434 TK_LP expr TK_RP
436 $$ = $2;
438 | column_val TK_EQ column_val
440 $$ = EXPR_complex( $1, OP_EQ, $3 );
442 | expr TK_AND expr
444 $$ = EXPR_complex( $1, OP_AND, $3 );
446 | expr TK_OR expr
448 $$ = EXPR_complex( $1, OP_OR, $3 );
450 | column_val TK_EQ val
452 $$ = EXPR_complex( $1, OP_EQ, $3 );
454 | column_val TK_GT val
456 $$ = EXPR_complex( $1, OP_GT, $3 );
458 | column_val TK_LT val
460 $$ = EXPR_complex( $1, OP_LT, $3 );
462 | column_val TK_LE val
464 $$ = EXPR_complex( $1, OP_LE, $3 );
466 | column_val TK_GE val
468 $$ = EXPR_complex( $1, OP_GE, $3 );
470 | column_val TK_NE val
472 $$ = EXPR_complex( $1, OP_NE, $3 );
474 | column_val TK_IS TK_NULL
476 $$ = EXPR_complex( $1, OP_ISNULL, NULL );
478 | column_val TK_IS TK_NOT TK_NULL
480 $$ = EXPR_complex( $1, OP_NOTNULL, NULL );
484 val:
485 column_val
486 | const_val
489 constlist:
490 const_val
492 value_list *vals;
494 vals = HeapAlloc( GetProcessHeap(), 0, sizeof *vals );
495 if( vals )
497 vals->val = $1;
498 vals->next = NULL;
500 $$ = vals;
502 | constlist TK_COMMA const_val
504 value_list *vals;
506 vals = HeapAlloc( GetProcessHeap(), 0, sizeof *vals );
507 if( vals )
509 vals->val = $3;
510 vals->next = NULL;
512 $1->next = vals;
513 $$ = $1;
517 update_assign_list:
518 column_assignment
519 | column_assignment TK_COMMA update_assign_list
521 $1.col_list->next = $3.col_list;
522 $1.val_list->next = $3.val_list;
523 $$ = $1;
527 column_assignment:
528 column TK_EQ const_val
530 $$.col_list = HeapAlloc( GetProcessHeap(), 0, sizeof *$$.col_list );
531 if( !$$.col_list )
532 YYABORT;
533 $$.col_list->string = $1;
534 $$.col_list->next = NULL;
535 $$.val_list = HeapAlloc( GetProcessHeap(), 0, sizeof *$$.val_list );
536 if( !$$.val_list )
537 YYABORT;
538 $$.val_list->val = $3;
539 $$.val_list->next = 0;
543 const_val:
544 TK_INTEGER
546 $$ = EXPR_ival( &$1 );
548 | TK_STRING
550 $$ = EXPR_sval( &$1 );
552 | TK_WILDCARD
554 $$ = EXPR_wildcard();
558 column_val:
559 column
561 $$ = EXPR_column( $1 );
565 column:
566 table TK_DOT string_or_id
568 $$ = $3; /* FIXME */
570 | string_or_id
572 $$ = $1;
576 table:
577 string_or_id
579 $$ = $1;
583 string_or_id:
584 TK_ID
586 $$ = SQL_getstring( &$1 );
588 | TK_STRING
590 $$ = SQL_getstring( &$1 );
596 int SQL_lex( void *SQL_lval, SQL_input *sql)
598 int token;
599 struct sql_str * str = SQL_lval;
603 sql->n += sql->len;
604 if( ! sql->command[sql->n] )
605 return 0; /* end of input */
607 TRACE("string : %s\n", debugstr_w(&sql->command[sql->n]));
608 sql->len = sqliteGetToken( &sql->command[sql->n], &token );
609 if( sql->len==0 )
610 break;
611 str->data = &sql->command[sql->n];
612 str->len = sql->len;
614 while( token == TK_SPACE );
616 TRACE("token : %d (%s)\n", token, debugstr_wn(&sql->command[sql->n], sql->len));
618 return token;
621 LPWSTR SQL_getstring( struct sql_str *strdata)
623 LPCWSTR p = strdata->data;
624 UINT len = strdata->len;
625 LPWSTR str;
627 /* if there's quotes, remove them */
628 if( ( (p[0]=='`') && (p[len-1]=='`') ) ||
629 ( (p[0]=='\'') && (p[len-1]=='\'') ) )
631 p++;
632 len -= 2;
634 str = HeapAlloc( GetProcessHeap(), 0, (len + 1)*sizeof(WCHAR));
635 if(!str )
636 return str;
637 memcpy(str, p, len*sizeof(WCHAR) );
638 str[len]=0;
640 return str;
643 INT SQL_getint( SQL_input *sql )
645 LPCWSTR p = &sql->command[sql->n];
647 return atoiW( p );
650 int SQL_error(const char *str)
652 return 0;
655 static MSIVIEW *do_one_select( MSIDATABASE *db, MSIVIEW *in,
656 string_list *columns )
658 MSIVIEW *view = NULL;
660 SELECT_CreateView( db, &view, in, columns );
661 delete_string_list( columns );
662 if( !view )
663 ERR("Error creating select query\n");
664 return view;
667 static MSIVIEW *do_order_by( MSIDATABASE *db, MSIVIEW *in,
668 string_list *columns )
670 MSIVIEW *view = NULL;
672 ORDER_CreateView( db, &view, in );
673 if( view )
675 string_list *x = columns;
677 for( x = columns; x ; x = x->next )
678 ORDER_AddColumn( view, x->string );
680 else
681 ERR("Error creating select query\n");
682 delete_string_list( columns );
683 return view;
686 static struct expr * EXPR_wildcard()
688 struct expr *e = HeapAlloc( GetProcessHeap(), 0, sizeof *e );
689 if( e )
691 e->type = EXPR_WILDCARD;
693 return e;
696 static struct expr * EXPR_complex( struct expr *l, UINT op, struct expr *r )
698 struct expr *e = HeapAlloc( GetProcessHeap(), 0, sizeof *e );
699 if( e )
701 e->type = EXPR_COMPLEX;
702 e->u.expr.left = l;
703 e->u.expr.op = op;
704 e->u.expr.right = r;
706 return e;
709 static struct expr * EXPR_column( LPWSTR str )
711 struct expr *e = HeapAlloc( GetProcessHeap(), 0, sizeof *e );
712 if( e )
714 e->type = EXPR_COLUMN;
715 e->u.sval = str;
717 return e;
720 static struct expr * EXPR_ival( struct sql_str *str )
722 struct expr *e = HeapAlloc( GetProcessHeap(), 0, sizeof *e );
723 if( e )
725 e->type = EXPR_IVAL;
726 e->u.ival = atoiW( str->data );
728 return e;
731 static struct expr * EXPR_sval( struct sql_str *str )
733 struct expr *e = HeapAlloc( GetProcessHeap(), 0, sizeof *e );
734 if( e )
736 e->type = EXPR_SVAL;
737 e->u.sval = SQL_getstring( str );
739 return e;
742 void delete_expr( struct expr *e )
744 if( !e )
745 return;
746 if( e->type == EXPR_COMPLEX )
748 delete_expr( e->u.expr.left );
749 delete_expr( e->u.expr.right );
751 else if( e->type == EXPR_UTF8 )
752 HeapFree( GetProcessHeap(), 0, e->u.utf8 );
753 else if( e->type == EXPR_SVAL )
754 HeapFree( GetProcessHeap(), 0, e->u.sval );
755 HeapFree( GetProcessHeap(), 0, e );
758 void delete_string_list( string_list *sl )
760 while( sl )
762 string_list *t = sl->next;
763 HeapFree( GetProcessHeap(), 0, sl->string );
764 HeapFree( GetProcessHeap(), 0, sl );
765 sl = t;
769 void delete_value_list( value_list *vl )
771 while( vl )
773 value_list *t = vl->next;
774 delete_expr( vl->val );
775 HeapFree( GetProcessHeap(), 0, vl );
776 vl = t;
780 static BOOL SQL_MarkPrimaryKeys( create_col_info *cols,
781 string_list *keys )
783 string_list *k;
784 BOOL found = TRUE;
786 for( k = keys; k && found; k = k->next )
788 create_col_info *c;
790 found = FALSE;
791 for( c = cols; c && !found; c = c->next )
793 if( lstrcmpW( k->string, c->colname ) )
794 continue;
795 c->type |= MSITYPE_KEY;
796 found = TRUE;
800 return found;
803 UINT MSI_ParseSQL( MSIDATABASE *db, LPCWSTR command, MSIVIEW **phview )
805 SQL_input sql;
806 int r;
808 *phview = NULL;
810 sql.db = db;
811 sql.command = command;
812 sql.n = 0;
813 sql.len = 0;
814 sql.view = phview;
816 r = SQL_parse(&sql);
818 TRACE("Parse returned %d\n", r);
819 if( r )
821 if( *sql.view )
822 (*sql.view)->ops->delete( *sql.view );
823 *sql.view = NULL;
824 return ERROR_BAD_QUERY_SYNTAX;
827 return ERROR_SUCCESS;