msvcrt: Use iob_func instead of p__iob on non-x86.
[wine/wine64.git] / dlls / msi / sql.y
blob7deae216c9d0f1a4644cf93fb96de3dd1dd59a93
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, const struct sql_str *str );
53 static INT SQL_getint( void *info );
54 static int sql_lex( void *SQL_lval, SQL_input *info );
56 static LPWSTR parser_add_table( LPWSTR list, LPWSTR table );
57 static void *parser_alloc( void *info, unsigned int sz );
58 static column_info *parser_alloc_column( void *info, LPCWSTR table, LPCWSTR column );
60 static BOOL SQL_MarkPrimaryKeys( column_info *cols, column_info *keys);
62 static struct expr * EXPR_complex( void *info, struct expr *l, UINT op, struct expr *r );
63 static struct expr * EXPR_unary( void *info, struct expr *l, UINT op );
64 static struct expr * EXPR_column( void *info, const column_info *column );
65 static struct expr * EXPR_ival( void *info, int val );
66 static struct expr * EXPR_sval( void *info, const struct sql_str *str );
67 static struct expr * EXPR_wildcard( void *info );
71 %pure-parser
73 %union
75 struct sql_str str;
76 LPWSTR string;
77 column_info *column_list;
78 MSIVIEW *query;
79 struct expr *expr;
80 USHORT column_type;
81 int integer;
84 %token TK_ALTER TK_AND TK_BY TK_CHAR TK_COMMA TK_CREATE TK_DELETE TK_DROP
85 %token TK_DISTINCT TK_DOT TK_EQ TK_FREE TK_FROM TK_GE TK_GT TK_HOLD TK_ADD
86 %token <str> TK_ID
87 %token TK_ILLEGAL TK_INSERT TK_INT
88 %token <str> TK_INTEGER
89 %token TK_INTO TK_IS TK_KEY TK_LE TK_LONG TK_LONGCHAR TK_LP TK_LT
90 %token TK_LOCALIZABLE TK_MINUS TK_NE TK_NOT TK_NULL
91 %token TK_OBJECT TK_OR TK_ORDER TK_PRIMARY TK_RP
92 %token TK_SELECT TK_SET TK_SHORT TK_SPACE TK_STAR
93 %token <str> TK_STRING
94 %token TK_TABLE TK_TEMPORARY TK_UPDATE TK_VALUES TK_WHERE TK_WILDCARD
97 * These are extra tokens used by the lexer but never seen by the
98 * parser. We put them in a rule so that the parser generator will
99 * add them to the parse.h output file.
102 %nonassoc END_OF_FILE ILLEGAL SPACE UNCLOSED_STRING COMMENT FUNCTION
103 COLUMN AGG_FUNCTION.
105 %type <string> table tablelist id
106 %type <column_list> selcollist column column_and_type column_def table_def
107 %type <column_list> column_assignment update_assign_list constlist
108 %type <query> query from fromtable selectfrom unorderedsel
109 %type <query> oneupdate onedelete oneselect onequery onecreate oneinsert onealter onedrop
110 %type <expr> expr val column_val const_val
111 %type <column_type> column_type data_type data_type_l data_count
112 %type <integer> number alterop
114 /* Reference: http://mates.ms.mff.cuni.cz/oracle/doc/ora815nt/server.815/a67779/operator.htm */
115 %left TK_OR
116 %left TK_AND
117 %left TK_NOT
118 %left TK_EQ TK_NE TK_LT TK_GT TK_LE TK_GE TK_LIKE
119 %right TK_NEGATION
123 query:
124 onequery
126 SQL_input* sql = (SQL_input*) info;
127 *sql->view = $1;
131 onequery:
132 oneselect
133 | onecreate
134 | oneinsert
135 | oneupdate
136 | onedelete
137 | onealter
138 | onedrop
141 oneinsert:
142 TK_INSERT TK_INTO table TK_LP selcollist TK_RP TK_VALUES TK_LP constlist TK_RP
144 SQL_input *sql = (SQL_input*) info;
145 MSIVIEW *insert = NULL;
147 INSERT_CreateView( sql->db, &insert, $3, $5, $9, FALSE );
148 if( !insert )
149 YYABORT;
150 $$ = insert;
152 | TK_INSERT TK_INTO table TK_LP selcollist TK_RP TK_VALUES TK_LP constlist TK_RP TK_TEMPORARY
154 SQL_input *sql = (SQL_input*) info;
155 MSIVIEW *insert = NULL;
157 INSERT_CreateView( sql->db, &insert, $3, $5, $9, TRUE );
158 if( !insert )
159 YYABORT;
160 $$ = insert;
164 onecreate:
165 TK_CREATE TK_TABLE table TK_LP table_def TK_RP
167 SQL_input* sql = (SQL_input*) info;
168 MSIVIEW *create = NULL;
170 if( !$5 )
171 YYABORT;
172 CREATE_CreateView( sql->db, &create, $3, $5, FALSE );
173 if( !create )
174 YYABORT;
175 $$ = create;
177 | TK_CREATE TK_TABLE table TK_LP table_def TK_RP TK_HOLD
179 SQL_input* sql = (SQL_input*) info;
180 MSIVIEW *create = NULL;
182 if( !$5 )
183 YYABORT;
184 CREATE_CreateView( sql->db, &create, $3, $5, TRUE );
185 if( !create )
186 YYABORT;
187 $$ = create;
191 oneupdate:
192 TK_UPDATE table TK_SET update_assign_list TK_WHERE expr
194 SQL_input* sql = (SQL_input*) info;
195 MSIVIEW *update = NULL;
197 UPDATE_CreateView( sql->db, &update, $2, $4, $6 );
198 if( !update )
199 YYABORT;
200 $$ = update;
202 | TK_UPDATE table TK_SET update_assign_list
204 SQL_input* sql = (SQL_input*) info;
205 MSIVIEW *update = NULL;
207 UPDATE_CreateView( sql->db, &update, $2, $4, NULL );
208 if( !update )
209 YYABORT;
210 $$ = update;
214 onedelete:
215 TK_DELETE from
217 SQL_input* sql = (SQL_input*) info;
218 MSIVIEW *delete = NULL;
220 DELETE_CreateView( sql->db, &delete, $2 );
221 if( !delete )
222 YYABORT;
223 $$ = delete;
227 onealter:
228 TK_ALTER TK_TABLE table alterop
230 SQL_input* sql = (SQL_input*) info;
231 MSIVIEW *alter = NULL;
233 ALTER_CreateView( sql->db, &alter, $3, NULL, $4 );
234 if( !alter )
235 YYABORT;
236 $$ = alter;
238 | TK_ALTER TK_TABLE table TK_ADD column_and_type
240 SQL_input *sql = (SQL_input *)info;
241 MSIVIEW *alter = NULL;
243 ALTER_CreateView( sql->db, &alter, $3, $5, 0 );
244 if (!alter)
245 YYABORT;
246 $$ = alter;
248 | TK_ALTER TK_TABLE table TK_ADD column_and_type TK_HOLD
250 SQL_input *sql = (SQL_input *)info;
251 MSIVIEW *alter = NULL;
253 ALTER_CreateView( sql->db, &alter, $3, $5, 1 );
254 if (!alter)
255 YYABORT;
256 $$ = alter;
260 alterop:
261 TK_HOLD
263 $$ = 1;
265 | TK_FREE
267 $$ = -1;
271 onedrop:
272 TK_DROP TK_TABLE table
274 SQL_input* sql = (SQL_input*) info;
275 UINT r;
277 $$ = NULL;
278 r = DROP_CreateView( sql->db, &$$, $3 );
279 if( r != ERROR_SUCCESS || !$$ )
280 YYABORT;
284 table_def:
285 column_def TK_PRIMARY TK_KEY selcollist
287 if( SQL_MarkPrimaryKeys( $1, $4 ) )
288 $$ = $1;
289 else
290 $$ = NULL;
294 column_def:
295 column_def TK_COMMA column_and_type
297 column_info *ci;
299 for( ci = $1; ci->next; ci = ci->next )
302 ci->next = $3;
303 $$ = $1;
305 | column_and_type
307 $$ = $1;
311 column_and_type:
312 column column_type
314 $$ = $1;
315 $$->type = ($2 | MSITYPE_VALID);
316 $$->temporary = $2 & MSITYPE_TEMPORARY ? TRUE : FALSE;
320 column_type:
321 data_type_l
323 $$ = $1;
325 | data_type_l TK_LOCALIZABLE
327 $$ = $1 | MSITYPE_LOCALIZABLE;
329 | data_type_l TK_TEMPORARY
331 $$ = $1 | MSITYPE_TEMPORARY;
335 data_type_l:
336 data_type
338 $$ |= MSITYPE_NULLABLE;
340 | data_type TK_NOT TK_NULL
342 $$ = $1;
346 data_type:
347 TK_CHAR
349 $$ = MSITYPE_STRING | 1;
351 | TK_CHAR TK_LP data_count TK_RP
353 $$ = MSITYPE_STRING | 0x400 | $3;
355 | TK_LONGCHAR
357 $$ = 2;
359 | TK_SHORT
361 $$ = 2;
363 | TK_INT
365 $$ = 2;
367 | TK_LONG
369 $$ = 4;
371 | TK_OBJECT
373 $$ = MSITYPE_STRING | MSITYPE_VALID;
377 data_count:
378 number
380 if( ( $1 > 255 ) || ( $1 < 0 ) )
381 YYABORT;
382 $$ = $1;
386 oneselect:
387 unorderedsel TK_ORDER TK_BY selcollist
389 UINT r;
391 if( $4 )
393 r = $1->ops->sort( $1, $4 );
394 if ( r != ERROR_SUCCESS)
395 YYABORT;
398 $$ = $1;
400 | unorderedsel
403 unorderedsel:
404 TK_SELECT selectfrom
406 $$ = $2;
408 | TK_SELECT TK_DISTINCT selectfrom
410 SQL_input* sql = (SQL_input*) info;
411 UINT r;
413 $$ = NULL;
414 r = DISTINCT_CreateView( sql->db, &$$, $3 );
415 if (r != ERROR_SUCCESS)
417 $3->ops->delete($3);
418 YYABORT;
423 selectfrom:
424 selcollist from
426 SQL_input* sql = (SQL_input*) info;
427 UINT r;
429 $$ = NULL;
430 if( $1 )
432 r = SELECT_CreateView( sql->db, &$$, $2, $1 );
433 if (r != ERROR_SUCCESS)
435 $2->ops->delete($2);
436 YYABORT;
439 else
440 $$ = $2;
444 selcollist:
445 column
446 | column TK_COMMA selcollist
448 $1->next = $3;
450 | TK_STAR
452 $$ = NULL;
456 from:
457 fromtable
458 | fromtable TK_WHERE expr
460 SQL_input* sql = (SQL_input*) info;
461 UINT r;
463 $$ = NULL;
464 r = WHERE_CreateView( sql->db, &$$, $1, $3 );
465 if( r != ERROR_SUCCESS )
467 $1->ops->delete( $1 );
468 YYABORT;
473 fromtable:
474 TK_FROM table
476 SQL_input* sql = (SQL_input*) info;
477 UINT r;
479 $$ = NULL;
480 r = TABLE_CreateView( sql->db, $2, &$$ );
481 if( r != ERROR_SUCCESS || !$$ )
482 YYABORT;
484 | TK_FROM tablelist
486 SQL_input* sql = (SQL_input*) info;
487 UINT r;
489 r = JOIN_CreateView( sql->db, &$$, $2 );
490 msi_free( $2 );
491 if( r != ERROR_SUCCESS )
492 YYABORT;
496 tablelist:
497 table
499 $$ = strdupW($1);
502 table TK_COMMA tablelist
504 $$ = parser_add_table($3, $1);
505 if (!$$)
506 YYABORT;
510 expr:
511 TK_LP expr TK_RP
513 $$ = $2;
514 if( !$$ )
515 YYABORT;
517 | expr TK_AND expr
519 $$ = EXPR_complex( info, $1, OP_AND, $3 );
520 if( !$$ )
521 YYABORT;
523 | expr TK_OR expr
525 $$ = EXPR_complex( info, $1, OP_OR, $3 );
526 if( !$$ )
527 YYABORT;
529 | column_val TK_EQ val
531 $$ = EXPR_complex( info, $1, OP_EQ, $3 );
532 if( !$$ )
533 YYABORT;
535 | column_val TK_GT val
537 $$ = EXPR_complex( info, $1, OP_GT, $3 );
538 if( !$$ )
539 YYABORT;
541 | column_val TK_LT val
543 $$ = EXPR_complex( info, $1, OP_LT, $3 );
544 if( !$$ )
545 YYABORT;
547 | column_val TK_LE val
549 $$ = EXPR_complex( info, $1, OP_LE, $3 );
550 if( !$$ )
551 YYABORT;
553 | column_val TK_GE val
555 $$ = EXPR_complex( info, $1, OP_GE, $3 );
556 if( !$$ )
557 YYABORT;
559 | column_val TK_NE val
561 $$ = EXPR_complex( info, $1, OP_NE, $3 );
562 if( !$$ )
563 YYABORT;
565 | column_val TK_IS TK_NULL
567 $$ = EXPR_unary( info, $1, OP_ISNULL );
568 if( !$$ )
569 YYABORT;
571 | column_val TK_IS TK_NOT TK_NULL
573 $$ = EXPR_unary( info, $1, OP_NOTNULL );
574 if( !$$ )
575 YYABORT;
579 val:
580 column_val
581 | const_val
584 constlist:
585 const_val
587 $$ = parser_alloc_column( info, NULL, NULL );
588 if( !$$ )
589 YYABORT;
590 $$->val = $1;
592 | const_val TK_COMMA constlist
594 $$ = parser_alloc_column( info, NULL, NULL );
595 if( !$$ )
596 YYABORT;
597 $$->val = $1;
598 $$->next = $3;
602 update_assign_list:
603 column_assignment
604 | column_assignment TK_COMMA update_assign_list
606 $$ = $1;
607 $$->next = $3;
611 column_assignment:
612 column TK_EQ const_val
614 $$ = $1;
615 $$->val = $3;
619 const_val:
620 number
622 $$ = EXPR_ival( info, $1 );
623 if( !$$ )
624 YYABORT;
626 | TK_MINUS number %prec TK_NEGATION
628 $$ = EXPR_ival( info, -$2 );
629 if( !$$ )
630 YYABORT;
632 | TK_STRING
634 $$ = EXPR_sval( info, &$1 );
635 if( !$$ )
636 YYABORT;
638 | TK_WILDCARD
640 $$ = EXPR_wildcard( info );
641 if( !$$ )
642 YYABORT;
646 column_val:
647 column
649 $$ = EXPR_column( info, $1 );
650 if( !$$ )
651 YYABORT;
655 column:
656 table TK_DOT id
658 $$ = parser_alloc_column( info, $1, $3 );
659 if( !$$ )
660 YYABORT;
662 | id
664 $$ = parser_alloc_column( info, NULL, $1 );
665 if( !$$ )
666 YYABORT;
670 table:
673 $$ = $1;
678 TK_ID
680 $$ = SQL_getstring( info, &$1 );
681 if( !$$ )
682 YYABORT;
686 number:
687 TK_INTEGER
689 $$ = SQL_getint( info );
695 static LPWSTR parser_add_table(LPWSTR list, LPWSTR table)
697 DWORD size = lstrlenW(list) + lstrlenW(table) + 2;
698 static const WCHAR space[] = {' ',0};
700 list = msi_realloc(list, size * sizeof(WCHAR));
701 if (!list) return NULL;
703 lstrcatW(list, space);
704 lstrcatW(list, table);
705 return list;
708 static void *parser_alloc( void *info, unsigned int sz )
710 SQL_input* sql = (SQL_input*) info;
711 struct list *mem;
713 mem = msi_alloc( sizeof (struct list) + sz );
714 list_add_tail( sql->mem, mem );
715 return &mem[1];
718 static column_info *parser_alloc_column( void *info, LPCWSTR table, LPCWSTR column )
720 column_info *col;
722 col = parser_alloc( info, sizeof (*col) );
723 if( col )
725 col->table = table;
726 col->column = column;
727 col->val = NULL;
728 col->type = 0;
729 col->next = NULL;
732 return col;
735 static int sql_lex( void *SQL_lval, SQL_input *sql )
737 int token;
738 struct sql_str * str = SQL_lval;
742 sql->n += sql->len;
743 if( ! sql->command[sql->n] )
744 return 0; /* end of input */
746 /* TRACE("string : %s\n", debugstr_w(&sql->command[sql->n])); */
747 sql->len = sqliteGetToken( &sql->command[sql->n], &token );
748 if( sql->len==0 )
749 break;
750 str->data = &sql->command[sql->n];
751 str->len = sql->len;
753 while( token == TK_SPACE );
755 /* TRACE("token : %d (%s)\n", token, debugstr_wn(&sql->command[sql->n], sql->len)); */
757 return token;
760 LPWSTR SQL_getstring( void *info, const struct sql_str *strdata )
762 LPCWSTR p = strdata->data;
763 UINT len = strdata->len;
764 LPWSTR str;
766 /* if there's quotes, remove them */
767 if( ( (p[0]=='`') && (p[len-1]=='`') ) ||
768 ( (p[0]=='\'') && (p[len-1]=='\'') ) )
770 p++;
771 len -= 2;
773 str = parser_alloc( info, (len + 1)*sizeof(WCHAR) );
774 if( !str )
775 return str;
776 memcpy( str, p, len*sizeof(WCHAR) );
777 str[len]=0;
779 return str;
782 INT SQL_getint( void *info )
784 SQL_input* sql = (SQL_input*) info;
785 LPCWSTR p = &sql->command[sql->n];
786 INT i, r = 0;
788 for( i=0; i<sql->len; i++ )
790 if( '0' > p[i] || '9' < p[i] )
792 ERR("should only be numbers here!\n");
793 break;
795 r = (p[i]-'0') + r*10;
798 return r;
801 static int sql_error( const char *str )
803 return 0;
806 static struct expr * EXPR_wildcard( void *info )
808 struct expr *e = parser_alloc( info, sizeof *e );
809 if( e )
811 e->type = EXPR_WILDCARD;
813 return e;
816 static struct expr * EXPR_complex( void *info, struct expr *l, UINT op, struct expr *r )
818 struct expr *e = parser_alloc( info, sizeof *e );
819 if( e )
821 e->type = EXPR_COMPLEX;
822 e->u.expr.left = l;
823 e->u.expr.op = op;
824 e->u.expr.right = r;
826 return e;
829 static struct expr * EXPR_unary( void *info, struct expr *l, UINT op )
831 struct expr *e = parser_alloc( info, sizeof *e );
832 if( e )
834 e->type = EXPR_UNARY;
835 e->u.expr.left = l;
836 e->u.expr.op = op;
837 e->u.expr.right = NULL;
839 return e;
842 static struct expr * EXPR_column( void *info, const column_info *column )
844 struct expr *e = parser_alloc( info, sizeof *e );
845 if( e )
847 e->type = EXPR_COLUMN;
848 e->u.sval = column->column;
850 return e;
853 static struct expr * EXPR_ival( void *info, int val )
855 struct expr *e = parser_alloc( info, sizeof *e );
856 if( e )
858 e->type = EXPR_IVAL;
859 e->u.ival = val;
861 return e;
864 static struct expr * EXPR_sval( void *info, const struct sql_str *str )
866 struct expr *e = parser_alloc( info, sizeof *e );
867 if( e )
869 e->type = EXPR_SVAL;
870 e->u.sval = SQL_getstring( info, str );
872 return e;
875 static BOOL SQL_MarkPrimaryKeys( column_info *cols,
876 column_info *keys )
878 column_info *k;
879 BOOL found = TRUE;
881 for( k = keys; k && found; k = k->next )
883 column_info *c;
885 found = FALSE;
886 for( c = cols; c && !found; c = c->next )
888 if( lstrcmpW( k->column, c->column ) )
889 continue;
890 c->type |= MSITYPE_KEY;
891 found = TRUE;
895 return found;
898 UINT MSI_ParseSQL( MSIDATABASE *db, LPCWSTR command, MSIVIEW **phview,
899 struct list *mem )
901 SQL_input sql;
902 int r;
904 *phview = NULL;
906 sql.db = db;
907 sql.command = command;
908 sql.n = 0;
909 sql.len = 0;
910 sql.view = phview;
911 sql.mem = mem;
913 r = sql_parse(&sql);
915 TRACE("Parse returned %d\n", r);
916 if( r )
918 *sql.view = NULL;
919 return ERROR_BAD_QUERY_SYNTAX;
922 return ERROR_SUCCESS;