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
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
48 MSIVIEW **view; /* view structure for the resulting query */
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 );
76 column_info *column_list;
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
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
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 */
117 %left TK_EQ TK_NE TK_LT TK_GT TK_LE TK_GE TK_LIKE
125 SQL_input* sql = (SQL_input*) info;
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;
146 r = INSERT_CreateView( sql->db, &insert, $3, $5, $9, FALSE );
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 );
164 TK_CREATE TK_TABLE table TK_LP table_def TK_RP
166 SQL_input* sql = (SQL_input*) info;
167 MSIVIEW *create = NULL;
171 CREATE_CreateView( sql->db, &create, $3, $5, FALSE );
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;
183 CREATE_CreateView( sql->db, &create, $3, $5, TRUE );
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 );
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 );
216 SQL_input* sql = (SQL_input*) info;
217 MSIVIEW *delete = NULL;
219 DELETE_CreateView( sql->db, &delete, $2 );
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 );
251 column_def TK_PRIMARY TK_KEY selcollist
253 if( SQL_MarkPrimaryKeys( $1, $4 ) )
261 column_def TK_COMMA column_and_type
265 for( ci = $1; ci->next; ci = ci->next )
281 $$->type = $2 | MSITYPE_VALID;
290 | data_type_l TK_LOCALIZABLE
292 $$ = $1 | MSITYPE_LOCALIZABLE;
294 | data_type_l TK_TEMPORARY
296 FIXME("temporary column\n");
303 $$ |= MSITYPE_NULLABLE;
305 | data_type TK_NOT TK_NULL
314 $$ = MSITYPE_STRING | 1;
316 | TK_CHAR TK_LP data_count TK_RP
318 $$ = MSITYPE_STRING | 0x400 | $3;
338 $$ = MSITYPE_STRING | MSITYPE_VALID;
345 if( ( $1 > 255 ) || ( $1 < 0 ) )
352 unorderedsel TK_ORDER TK_BY selcollist
354 SQL_input* sql = (SQL_input*) info;
358 ORDER_CreateView( sql->db, &$$, $1, $4 );
372 | TK_SELECT TK_DISTINCT selectfrom
374 SQL_input* sql = (SQL_input*) info;
378 r = DISTINCT_CreateView( sql->db, &$$, $3 );
379 if (r != ERROR_SUCCESS)
390 SQL_input* sql = (SQL_input*) info;
396 r = SELECT_CreateView( sql->db, &$$, $2, $1 );
397 if (r != ERROR_SUCCESS)
410 | column TK_COMMA selcollist
422 | fromtable TK_WHERE expr
424 SQL_input* sql = (SQL_input*) info;
428 r = WHERE_CreateView( sql->db, &$$, $1, $3 );
429 if( r != ERROR_SUCCESS )
431 $1->ops->delete( $1 );
440 SQL_input* sql = (SQL_input*) info;
444 r = TABLE_CreateView( sql->db, $2, &$$ );
445 if( r != ERROR_SUCCESS || !$$ )
448 | TK_FROM table TK_COMMA table
450 SQL_input* sql = (SQL_input*) info;
453 /* only support inner joins on two tables */
454 r = JOIN_CreateView( sql->db, &$$, $2, $4 );
455 if( r != ERROR_SUCCESS )
469 $$ = EXPR_complex( info, $1, OP_AND, $3 );
475 $$ = EXPR_complex( info, $1, OP_OR, $3 );
479 | column_val TK_EQ val
481 $$ = EXPR_complex( info, $1, OP_EQ, $3 );
485 | column_val TK_GT val
487 $$ = EXPR_complex( info, $1, OP_GT, $3 );
491 | column_val TK_LT val
493 $$ = EXPR_complex( info, $1, OP_LT, $3 );
497 | column_val TK_LE val
499 $$ = EXPR_complex( info, $1, OP_LE, $3 );
503 | column_val TK_GE val
505 $$ = EXPR_complex( info, $1, OP_GE, $3 );
509 | column_val TK_NE val
511 $$ = EXPR_complex( info, $1, OP_NE, $3 );
515 | column_val TK_IS TK_NULL
517 $$ = EXPR_unary( info, $1, OP_ISNULL );
521 | column_val TK_IS TK_NOT TK_NULL
523 $$ = EXPR_unary( info, $1, OP_NOTNULL );
537 $$ = parser_alloc_column( info, NULL, NULL );
542 | const_val TK_COMMA constlist
544 $$ = parser_alloc_column( info, NULL, NULL );
554 | column_assignment TK_COMMA update_assign_list
562 column TK_EQ const_val
572 $$ = EXPR_ival( info, $1 );
576 | TK_MINUS number %prec TK_NEGATION
578 $$ = EXPR_ival( info, -$2 );
584 $$ = EXPR_sval( info, &$1 );
590 $$ = EXPR_wildcard( info );
599 $$ = EXPR_column( info, $1 );
608 $$ = parser_alloc_column( info, $1, $3 );
614 $$ = parser_alloc_column( info, NULL, $1 );
630 $$ = SQL_getstring( info, &$1 );
639 $$ = SQL_getint( info );
645 static void *parser_alloc( void *info, unsigned int sz )
647 SQL_input* sql = (SQL_input*) info;
650 mem = msi_alloc( sizeof (struct list) + sz );
651 list_add_tail( sql->mem, mem );
655 static column_info *parser_alloc_column( void *info, LPCWSTR table, LPCWSTR column )
659 col = parser_alloc( info, sizeof (*col) );
663 col->column = column;
672 static int sql_lex( void *SQL_lval, SQL_input *sql )
675 struct sql_str * str = SQL_lval;
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 );
687 str->data = &sql->command[sql->n];
690 while( token == TK_SPACE );
692 /* TRACE("token : %d (%s)\n", token, debugstr_wn(&sql->command[sql->n], sql->len)); */
697 LPWSTR SQL_getstring( void *info, struct sql_str *strdata )
699 LPCWSTR p = strdata->data;
700 UINT len = strdata->len;
703 /* if there's quotes, remove them */
704 if( ( (p[0]=='`') && (p[len-1]=='`') ) ||
705 ( (p[0]=='\'') && (p[len-1]=='\'') ) )
710 str = parser_alloc( info, (len + 1)*sizeof(WCHAR) );
713 memcpy( str, p, len*sizeof(WCHAR) );
719 INT SQL_getint( void *info )
721 SQL_input* sql = (SQL_input*) info;
722 LPCWSTR p = &sql->command[sql->n];
725 for( i=0; i<sql->len; i++ )
727 if( '0' > p[i] || '9' < p[i] )
729 ERR("should only be numbers here!\n");
732 r = (p[i]-'0') + r*10;
738 static int sql_error( const char *str )
743 static struct expr * EXPR_wildcard( void *info )
745 struct expr *e = parser_alloc( info, sizeof *e );
748 e->type = EXPR_WILDCARD;
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 );
758 e->type = EXPR_COMPLEX;
766 static struct expr * EXPR_unary( void *info, struct expr *l, UINT op )
768 struct expr *e = parser_alloc( info, sizeof *e );
771 e->type = EXPR_UNARY;
774 e->u.expr.right = NULL;
779 static struct expr * EXPR_column( void *info, column_info *column )
781 struct expr *e = parser_alloc( info, sizeof *e );
784 e->type = EXPR_COLUMN;
785 e->u.sval = column->column;
790 static struct expr * EXPR_ival( void *info, int val )
792 struct expr *e = parser_alloc( info, sizeof *e );
801 static struct expr * EXPR_sval( void *info, struct sql_str *str )
803 struct expr *e = parser_alloc( info, sizeof *e );
807 e->u.sval = SQL_getstring( info, str );
812 static BOOL SQL_MarkPrimaryKeys( column_info *cols,
818 for( k = keys; k && found; k = k->next )
823 for( c = cols; c && !found; c = c->next )
825 if( lstrcmpW( k->column, c->column ) )
827 c->type |= MSITYPE_KEY;
835 UINT MSI_ParseSQL( MSIDATABASE *db, LPCWSTR command, MSIVIEW **phview,
844 sql.command = command;
852 TRACE("Parse returned %d\n", r);
856 return ERROR_BAD_QUERY_SYNTAX;
859 return ERROR_SUCCESS;