gdi32: PATH_ExtTextOut remove incorrect shift to DC origin.
[wine/hacks.git] / dlls / msi / sql.y
blob425b584ae69cb8b0ba9d64871cf4ff662ac45be4
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 UINT r;
49 MSIVIEW **view; /* view structure for the resulting query */
50 struct list *mem;
51 } SQL_input;
53 static UINT SQL_getstring( void *info, const struct sql_str *strdata, LPWSTR *str );
54 static INT SQL_getint( void *info );
55 static int sql_lex( void *SQL_lval, SQL_input *info );
57 static LPWSTR parser_add_table( LPWSTR list, LPWSTR table );
58 static void *parser_alloc( void *info, unsigned int sz );
59 static column_info *parser_alloc_column( void *info, LPCWSTR table, LPCWSTR column );
61 static BOOL SQL_MarkPrimaryKeys( column_info **cols, column_info *keys);
63 static struct expr * EXPR_complex( void *info, struct expr *l, UINT op, struct expr *r );
64 static struct expr * EXPR_unary( void *info, struct expr *l, UINT op );
65 static struct expr * EXPR_column( void *info, const column_info *column );
66 static struct expr * EXPR_ival( void *info, int val );
67 static struct expr * EXPR_sval( void *info, const struct sql_str *str );
68 static struct expr * EXPR_wildcard( void *info );
72 %pure-parser
74 %union
76 struct sql_str str;
77 LPWSTR string;
78 column_info *column_list;
79 MSIVIEW *query;
80 struct expr *expr;
81 USHORT column_type;
82 int integer;
85 %token TK_ALTER TK_AND TK_BY TK_CHAR TK_COMMA TK_CREATE TK_DELETE TK_DROP
86 %token TK_DISTINCT TK_DOT TK_EQ TK_FREE TK_FROM TK_GE TK_GT TK_HOLD TK_ADD
87 %token <str> TK_ID
88 %token TK_ILLEGAL TK_INSERT TK_INT
89 %token <str> TK_INTEGER
90 %token TK_INTO TK_IS TK_KEY TK_LE TK_LONG TK_LONGCHAR TK_LP TK_LT
91 %token TK_LOCALIZABLE TK_MINUS TK_NE TK_NOT TK_NULL
92 %token TK_OBJECT TK_OR TK_ORDER TK_PRIMARY TK_RP
93 %token TK_SELECT TK_SET TK_SHORT TK_SPACE TK_STAR
94 %token <str> TK_STRING
95 %token TK_TABLE TK_TEMPORARY TK_UPDATE TK_VALUES TK_WHERE TK_WILDCARD
98 * These are extra tokens used by the lexer but never seen by the
99 * parser. We put them in a rule so that the parser generator will
100 * add them to the parse.h output file.
103 %nonassoc END_OF_FILE ILLEGAL SPACE UNCLOSED_STRING COMMENT FUNCTION
104 COLUMN AGG_FUNCTION.
106 %type <string> table tablelist id
107 %type <column_list> selcollist column column_and_type column_def table_def
108 %type <column_list> column_assignment update_assign_list constlist
109 %type <query> query from fromtable selectfrom unorderedsel
110 %type <query> oneupdate onedelete oneselect onequery onecreate oneinsert onealter onedrop
111 %type <expr> expr val column_val const_val
112 %type <column_type> column_type data_type data_type_l data_count
113 %type <integer> number alterop
115 /* Reference: http://mates.ms.mff.cuni.cz/oracle/doc/ora815nt/server.815/a67779/operator.htm */
116 %left TK_OR
117 %left TK_AND
118 %left TK_NOT
119 %left TK_EQ TK_NE TK_LT TK_GT TK_LE TK_GE TK_LIKE
120 %right TK_NEGATION
124 query:
125 onequery
127 SQL_input* sql = (SQL_input*) info;
128 *sql->view = $1;
132 onequery:
133 oneselect
134 | onecreate
135 | oneinsert
136 | oneupdate
137 | onedelete
138 | onealter
139 | onedrop
142 oneinsert:
143 TK_INSERT TK_INTO table TK_LP selcollist TK_RP TK_VALUES TK_LP constlist TK_RP
145 SQL_input *sql = (SQL_input*) info;
146 MSIVIEW *insert = NULL;
148 INSERT_CreateView( sql->db, &insert, $3, $5, $9, FALSE );
149 if( !insert )
150 YYABORT;
151 $$ = insert;
153 | TK_INSERT TK_INTO table TK_LP selcollist TK_RP TK_VALUES TK_LP constlist TK_RP TK_TEMPORARY
155 SQL_input *sql = (SQL_input*) info;
156 MSIVIEW *insert = NULL;
158 INSERT_CreateView( sql->db, &insert, $3, $5, $9, TRUE );
159 if( !insert )
160 YYABORT;
161 $$ = insert;
165 onecreate:
166 TK_CREATE TK_TABLE table TK_LP table_def TK_RP
168 SQL_input* sql = (SQL_input*) info;
169 MSIVIEW *create = NULL;
170 UINT r;
172 if( !$5 )
173 YYABORT;
174 r = CREATE_CreateView( sql->db, &create, $3, $5, FALSE );
175 if( !create )
177 sql->r = r;
178 YYABORT;
180 $$ = create;
182 | TK_CREATE TK_TABLE table TK_LP table_def TK_RP TK_HOLD
184 SQL_input* sql = (SQL_input*) info;
185 MSIVIEW *create = NULL;
187 if( !$5 )
188 YYABORT;
189 CREATE_CreateView( sql->db, &create, $3, $5, TRUE );
190 if( !create )
191 YYABORT;
192 $$ = create;
196 oneupdate:
197 TK_UPDATE table TK_SET update_assign_list TK_WHERE expr
199 SQL_input* sql = (SQL_input*) info;
200 MSIVIEW *update = NULL;
202 UPDATE_CreateView( sql->db, &update, $2, $4, $6 );
203 if( !update )
204 YYABORT;
205 $$ = update;
207 | TK_UPDATE table TK_SET update_assign_list
209 SQL_input* sql = (SQL_input*) info;
210 MSIVIEW *update = NULL;
212 UPDATE_CreateView( sql->db, &update, $2, $4, NULL );
213 if( !update )
214 YYABORT;
215 $$ = update;
219 onedelete:
220 TK_DELETE from
222 SQL_input* sql = (SQL_input*) info;
223 MSIVIEW *delete = NULL;
225 DELETE_CreateView( sql->db, &delete, $2 );
226 if( !delete )
227 YYABORT;
228 $$ = delete;
232 onealter:
233 TK_ALTER TK_TABLE table alterop
235 SQL_input* sql = (SQL_input*) info;
236 MSIVIEW *alter = NULL;
238 ALTER_CreateView( sql->db, &alter, $3, NULL, $4 );
239 if( !alter )
240 YYABORT;
241 $$ = alter;
243 | TK_ALTER TK_TABLE table TK_ADD column_and_type
245 SQL_input *sql = (SQL_input *)info;
246 MSIVIEW *alter = NULL;
248 ALTER_CreateView( sql->db, &alter, $3, $5, 0 );
249 if (!alter)
250 YYABORT;
251 $$ = alter;
253 | TK_ALTER TK_TABLE table TK_ADD column_and_type TK_HOLD
255 SQL_input *sql = (SQL_input *)info;
256 MSIVIEW *alter = NULL;
258 ALTER_CreateView( sql->db, &alter, $3, $5, 1 );
259 if (!alter)
260 YYABORT;
261 $$ = alter;
265 alterop:
266 TK_HOLD
268 $$ = 1;
270 | TK_FREE
272 $$ = -1;
276 onedrop:
277 TK_DROP TK_TABLE table
279 SQL_input* sql = (SQL_input*) info;
280 UINT r;
282 $$ = NULL;
283 r = DROP_CreateView( sql->db, &$$, $3 );
284 if( r != ERROR_SUCCESS || !$$ )
285 YYABORT;
289 table_def:
290 column_def TK_PRIMARY TK_KEY selcollist
292 if( SQL_MarkPrimaryKeys( &$1, $4 ) )
293 $$ = $1;
294 else
295 $$ = NULL;
299 column_def:
300 column_def TK_COMMA column_and_type
302 column_info *ci;
304 for( ci = $1; ci->next; ci = ci->next )
307 ci->next = $3;
308 $$ = $1;
310 | column_and_type
312 $$ = $1;
316 column_and_type:
317 column column_type
319 $$ = $1;
320 $$->type = ($2 | MSITYPE_VALID);
321 $$->temporary = $2 & MSITYPE_TEMPORARY ? TRUE : FALSE;
325 column_type:
326 data_type_l
328 $$ = $1;
330 | data_type_l TK_LOCALIZABLE
332 $$ = $1 | MSITYPE_LOCALIZABLE;
334 | data_type_l TK_TEMPORARY
336 $$ = $1 | MSITYPE_TEMPORARY;
340 data_type_l:
341 data_type
343 $$ |= MSITYPE_NULLABLE;
345 | data_type TK_NOT TK_NULL
347 $$ = $1;
351 data_type:
352 TK_CHAR
354 $$ = MSITYPE_STRING | 1;
356 | TK_CHAR TK_LP data_count TK_RP
358 $$ = MSITYPE_STRING | 0x400 | $3;
360 | TK_LONGCHAR
362 $$ = 2;
364 | TK_SHORT
366 $$ = 2;
368 | TK_INT
370 $$ = 2;
372 | TK_LONG
374 $$ = 4;
376 | TK_OBJECT
378 $$ = MSITYPE_STRING | MSITYPE_VALID;
382 data_count:
383 number
385 if( ( $1 > 255 ) || ( $1 < 0 ) )
386 YYABORT;
387 $$ = $1;
391 oneselect:
392 unorderedsel TK_ORDER TK_BY selcollist
394 UINT r;
396 if( $4 )
398 r = $1->ops->sort( $1, $4 );
399 if ( r != ERROR_SUCCESS)
400 YYABORT;
403 $$ = $1;
405 | unorderedsel
408 unorderedsel:
409 TK_SELECT selectfrom
411 $$ = $2;
413 | TK_SELECT TK_DISTINCT selectfrom
415 SQL_input* sql = (SQL_input*) info;
416 UINT r;
418 $$ = NULL;
419 r = DISTINCT_CreateView( sql->db, &$$, $3 );
420 if (r != ERROR_SUCCESS)
422 $3->ops->delete($3);
423 YYABORT;
428 selectfrom:
429 selcollist from
431 SQL_input* sql = (SQL_input*) info;
432 UINT r;
434 $$ = NULL;
435 if( $1 )
437 r = SELECT_CreateView( sql->db, &$$, $2, $1 );
438 if (r != ERROR_SUCCESS)
440 $2->ops->delete($2);
441 YYABORT;
444 else
445 $$ = $2;
449 selcollist:
450 column
451 | column TK_COMMA selcollist
453 $1->next = $3;
455 | TK_STAR
457 $$ = NULL;
461 from:
462 fromtable
463 | fromtable TK_WHERE expr
465 SQL_input* sql = (SQL_input*) info;
466 UINT r;
468 $$ = NULL;
469 r = WHERE_CreateView( sql->db, &$$, $1, $3 );
470 if( r != ERROR_SUCCESS )
472 $1->ops->delete( $1 );
473 YYABORT;
478 fromtable:
479 TK_FROM table
481 SQL_input* sql = (SQL_input*) info;
482 UINT r;
484 $$ = NULL;
485 r = TABLE_CreateView( sql->db, $2, &$$ );
486 if( r != ERROR_SUCCESS || !$$ )
487 YYABORT;
489 | TK_FROM tablelist
491 SQL_input* sql = (SQL_input*) info;
492 UINT r;
494 r = JOIN_CreateView( sql->db, &$$, $2 );
495 msi_free( $2 );
496 if( r != ERROR_SUCCESS )
497 YYABORT;
501 tablelist:
502 table
504 $$ = strdupW($1);
507 table TK_COMMA tablelist
509 $$ = parser_add_table($3, $1);
510 if (!$$)
511 YYABORT;
515 expr:
516 TK_LP expr TK_RP
518 $$ = $2;
519 if( !$$ )
520 YYABORT;
522 | expr TK_AND expr
524 $$ = EXPR_complex( info, $1, OP_AND, $3 );
525 if( !$$ )
526 YYABORT;
528 | expr TK_OR expr
530 $$ = EXPR_complex( info, $1, OP_OR, $3 );
531 if( !$$ )
532 YYABORT;
534 | column_val TK_EQ val
536 $$ = EXPR_complex( info, $1, OP_EQ, $3 );
537 if( !$$ )
538 YYABORT;
540 | column_val TK_GT val
542 $$ = EXPR_complex( info, $1, OP_GT, $3 );
543 if( !$$ )
544 YYABORT;
546 | column_val TK_LT val
548 $$ = EXPR_complex( info, $1, OP_LT, $3 );
549 if( !$$ )
550 YYABORT;
552 | column_val TK_LE val
554 $$ = EXPR_complex( info, $1, OP_LE, $3 );
555 if( !$$ )
556 YYABORT;
558 | column_val TK_GE val
560 $$ = EXPR_complex( info, $1, OP_GE, $3 );
561 if( !$$ )
562 YYABORT;
564 | column_val TK_NE val
566 $$ = EXPR_complex( info, $1, OP_NE, $3 );
567 if( !$$ )
568 YYABORT;
570 | column_val TK_IS TK_NULL
572 $$ = EXPR_unary( info, $1, OP_ISNULL );
573 if( !$$ )
574 YYABORT;
576 | column_val TK_IS TK_NOT TK_NULL
578 $$ = EXPR_unary( info, $1, OP_NOTNULL );
579 if( !$$ )
580 YYABORT;
584 val:
585 column_val
586 | const_val
589 constlist:
590 const_val
592 $$ = parser_alloc_column( info, NULL, NULL );
593 if( !$$ )
594 YYABORT;
595 $$->val = $1;
597 | const_val TK_COMMA constlist
599 $$ = parser_alloc_column( info, NULL, NULL );
600 if( !$$ )
601 YYABORT;
602 $$->val = $1;
603 $$->next = $3;
607 update_assign_list:
608 column_assignment
609 | column_assignment TK_COMMA update_assign_list
611 $$ = $1;
612 $$->next = $3;
616 column_assignment:
617 column TK_EQ const_val
619 $$ = $1;
620 $$->val = $3;
624 const_val:
625 number
627 $$ = EXPR_ival( info, $1 );
628 if( !$$ )
629 YYABORT;
631 | TK_MINUS number %prec TK_NEGATION
633 $$ = EXPR_ival( info, -$2 );
634 if( !$$ )
635 YYABORT;
637 | TK_STRING
639 $$ = EXPR_sval( info, &$1 );
640 if( !$$ )
641 YYABORT;
643 | TK_WILDCARD
645 $$ = EXPR_wildcard( info );
646 if( !$$ )
647 YYABORT;
651 column_val:
652 column
654 $$ = EXPR_column( info, $1 );
655 if( !$$ )
656 YYABORT;
660 column:
661 table TK_DOT id
663 $$ = parser_alloc_column( info, $1, $3 );
664 if( !$$ )
665 YYABORT;
667 | id
669 $$ = parser_alloc_column( info, NULL, $1 );
670 if( !$$ )
671 YYABORT;
675 table:
678 $$ = $1;
683 TK_ID
685 if ( SQL_getstring( info, &$1, &$$ ) != ERROR_SUCCESS || !$$ )
686 YYABORT;
690 number:
691 TK_INTEGER
693 $$ = SQL_getint( info );
699 static LPWSTR parser_add_table(LPWSTR list, LPWSTR table)
701 DWORD size = lstrlenW(list) + lstrlenW(table) + 2;
702 static const WCHAR space[] = {' ',0};
704 list = msi_realloc(list, size * sizeof(WCHAR));
705 if (!list) return NULL;
707 lstrcatW(list, space);
708 lstrcatW(list, table);
709 return list;
712 static void *parser_alloc( void *info, unsigned int sz )
714 SQL_input* sql = (SQL_input*) info;
715 struct list *mem;
717 mem = msi_alloc( sizeof (struct list) + sz );
718 list_add_tail( sql->mem, mem );
719 return &mem[1];
722 static column_info *parser_alloc_column( void *info, LPCWSTR table, LPCWSTR column )
724 column_info *col;
726 col = parser_alloc( info, sizeof (*col) );
727 if( col )
729 col->table = table;
730 col->column = column;
731 col->val = NULL;
732 col->type = 0;
733 col->next = NULL;
736 return col;
739 static int sql_lex( void *SQL_lval, SQL_input *sql )
741 int token;
742 struct sql_str * str = SQL_lval;
746 sql->n += sql->len;
747 if( ! sql->command[sql->n] )
748 return 0; /* end of input */
750 /* TRACE("string : %s\n", debugstr_w(&sql->command[sql->n])); */
751 sql->len = sqliteGetToken( &sql->command[sql->n], &token );
752 if( sql->len==0 )
753 break;
754 str->data = &sql->command[sql->n];
755 str->len = sql->len;
757 while( token == TK_SPACE );
759 /* TRACE("token : %d (%s)\n", token, debugstr_wn(&sql->command[sql->n], sql->len)); */
761 return token;
764 UINT SQL_getstring( void *info, const struct sql_str *strdata, LPWSTR *str )
766 LPCWSTR p = strdata->data;
767 UINT len = strdata->len;
769 /* match quotes */
770 if( ( (p[0]=='`') && (p[len-1]!='`') ) ||
771 ( (p[0]=='\'') && (p[len-1]!='\'') ) )
772 return ERROR_FUNCTION_FAILED;
774 /* if there's quotes, remove them */
775 if( ( (p[0]=='`') && (p[len-1]=='`') ) ||
776 ( (p[0]=='\'') && (p[len-1]=='\'') ) )
778 p++;
779 len -= 2;
781 *str = parser_alloc( info, (len + 1)*sizeof(WCHAR) );
782 if( !*str )
783 return ERROR_OUTOFMEMORY;
784 memcpy( *str, p, len*sizeof(WCHAR) );
785 (*str)[len]=0;
787 return ERROR_SUCCESS;
790 INT SQL_getint( void *info )
792 SQL_input* sql = (SQL_input*) info;
793 LPCWSTR p = &sql->command[sql->n];
794 INT i, r = 0;
796 for( i=0; i<sql->len; i++ )
798 if( '0' > p[i] || '9' < p[i] )
800 ERR("should only be numbers here!\n");
801 break;
803 r = (p[i]-'0') + r*10;
806 return r;
809 static int sql_error( const char *str )
811 return 0;
814 static struct expr * EXPR_wildcard( void *info )
816 struct expr *e = parser_alloc( info, sizeof *e );
817 if( e )
819 e->type = EXPR_WILDCARD;
821 return e;
824 static struct expr * EXPR_complex( void *info, struct expr *l, UINT op, struct expr *r )
826 struct expr *e = parser_alloc( info, sizeof *e );
827 if( e )
829 e->type = EXPR_COMPLEX;
830 e->u.expr.left = l;
831 e->u.expr.op = op;
832 e->u.expr.right = r;
834 return e;
837 static struct expr * EXPR_unary( void *info, struct expr *l, UINT op )
839 struct expr *e = parser_alloc( info, sizeof *e );
840 if( e )
842 e->type = EXPR_UNARY;
843 e->u.expr.left = l;
844 e->u.expr.op = op;
845 e->u.expr.right = NULL;
847 return e;
850 static struct expr * EXPR_column( void *info, const column_info *column )
852 struct expr *e = parser_alloc( info, sizeof *e );
853 if( e )
855 e->type = EXPR_COLUMN;
856 e->u.sval = column->column;
858 return e;
861 static struct expr * EXPR_ival( void *info, int val )
863 struct expr *e = parser_alloc( info, sizeof *e );
864 if( e )
866 e->type = EXPR_IVAL;
867 e->u.ival = val;
869 return e;
872 static struct expr * EXPR_sval( void *info, const struct sql_str *str )
874 struct expr *e = parser_alloc( info, sizeof *e );
875 if( e )
877 e->type = EXPR_SVAL;
878 if( SQL_getstring( info, str, (LPWSTR *)&e->u.sval ) != ERROR_SUCCESS )
880 msi_free( e );
881 return NULL;
884 return e;
887 static void swap_columns( column_info **cols, column_info *A, int idx )
889 column_info *preA = NULL, *preB = NULL, *B, *ptr;
890 int i = 0;
892 B = NULL;
893 ptr = *cols;
894 while( ptr )
896 if( i++ == idx )
897 B = ptr;
898 else if( !B )
899 preB = ptr;
901 if( ptr->next == A )
902 preA = ptr;
904 ptr = ptr->next;
907 if( preB ) preB->next = A;
908 if( preA ) preA->next = B;
909 ptr = A->next;
910 A->next = B->next;
911 B->next = ptr;
912 if( idx == 0 )
913 *cols = A;
916 static BOOL SQL_MarkPrimaryKeys( column_info **cols,
917 column_info *keys )
919 column_info *k;
920 BOOL found = TRUE;
921 int count;
923 for( k = keys, count = 0; k && found; k = k->next, count++ )
925 column_info *c;
926 int idx;
928 found = FALSE;
929 for( c = *cols, idx = 0; c && !found; c = c->next, idx++ )
931 if( lstrcmpW( k->column, c->column ) )
932 continue;
933 c->type |= MSITYPE_KEY;
934 found = TRUE;
935 if (idx != count)
936 swap_columns( cols, c, count );
940 return found;
943 UINT MSI_ParseSQL( MSIDATABASE *db, LPCWSTR command, MSIVIEW **phview,
944 struct list *mem )
946 SQL_input sql;
947 int r;
949 *phview = NULL;
951 sql.db = db;
952 sql.command = command;
953 sql.n = 0;
954 sql.len = 0;
955 sql.r = ERROR_BAD_QUERY_SYNTAX;
956 sql.view = phview;
957 sql.mem = mem;
959 r = sql_parse(&sql);
961 TRACE("Parse returned %d\n", r);
962 if( r )
964 if (*sql.view)
966 (*sql.view)->ops->delete(*sql.view);
967 *sql.view = NULL;
969 return sql.r;
972 return ERROR_SUCCESS;