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
33 #include "wine/list.h"
34 #include "wine/debug.h"
35 #include "wine/unicode.h"
37 #define YYLEX_PARAM info
38 #define YYPARSE_PARAM info
40 extern int SQL_error(const char *str);
42 WINE_DEFAULT_DEBUG_CHANNEL(msi);
44 typedef struct tag_SQL_input
49 MSIVIEW **view; /* view structure for the resulting query */
53 static LPWSTR SQL_getstring( void *info, struct sql_str *str );
54 static INT SQL_getint( void *info );
55 static int SQL_lex( void *SQL_lval, SQL_input *info );
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_column( void *info, column_info *column );
64 static struct expr * EXPR_ival( void *info, struct sql_str *, int sign );
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_ABORT TK_AFTER TK_AGG_FUNCTION TK_ALL TK_AND TK_AS TK_ASC
84 %token TK_BEFORE TK_BEGIN TK_BETWEEN TK_BITAND TK_BITNOT TK_BITOR TK_BY
85 %token TK_CASCADE TK_CASE TK_CHAR TK_CHECK TK_CLUSTER TK_COLLATE TK_COLUMN
86 %token TK_COMMA TK_COMMENT TK_COMMIT TK_CONCAT TK_CONFLICT
87 %token TK_CONSTRAINT TK_COPY TK_CREATE
88 %token TK_DEFAULT TK_DEFERRABLE TK_DEFERRED TK_DELETE TK_DELIMITERS TK_DESC
89 %token TK_DISTINCT TK_DOT TK_DROP TK_EACH
90 %token TK_ELSE TK_END TK_END_OF_FILE TK_EQ TK_EXCEPT TK_EXPLAIN
91 %token TK_FAIL TK_FLOAT TK_FOR TK_FOREIGN TK_FROM TK_FUNCTION
92 %token TK_GE TK_GLOB TK_GROUP TK_GT
93 %token TK_HAVING TK_HOLD
94 %token TK_IGNORE TK_ILLEGAL TK_IMMEDIATE TK_IN TK_INDEX TK_INITIALLY
96 %token TK_INSERT TK_INSTEAD TK_INT
97 %token <str> TK_INTEGER
98 %token TK_INTERSECT TK_INTO TK_IS
100 %token TK_JOIN TK_JOIN_KW
102 %token TK_LE TK_LIKE TK_LIMIT TK_LONG TK_LONGCHAR TK_LP TK_LSHIFT TK_LT
103 %token TK_LOCALIZABLE
104 %token TK_MATCH TK_MINUS
105 %token TK_NE TK_NOT TK_NOTNULL TK_NULL
106 %token TK_OBJECT TK_OF TK_OFFSET TK_ON TK_OR TK_ORACLE_OUTER_JOIN TK_ORDER
107 %token TK_PLUS TK_PRAGMA TK_PRIMARY
108 %token TK_RAISE TK_REFERENCES TK_REM TK_REPLACE TK_RESTRICT TK_ROLLBACK
109 %token TK_ROW TK_RP TK_RSHIFT
110 %token TK_SELECT TK_SEMI TK_SET TK_SHORT TK_SLASH TK_SPACE TK_STAR TK_STATEMENT
111 %token <str> TK_STRING
112 %token TK_TABLE TK_TEMP TK_THEN TK_TRANSACTION TK_TRIGGER
113 %token TK_UMINUS TK_UNCLOSED_STRING TK_UNION TK_UNIQUE
114 %token TK_UPDATE TK_UPLUS TK_USING
115 %token TK_VACUUM TK_VALUES TK_VIEW
116 %token TK_WHEN TK_WHERE TK_WILDCARD
119 * These are extra tokens used by the lexer but never seen by the
120 * parser. We put them in a rule so that the parser generator will
121 * add them to the parse.h output file.
124 %nonassoc END_OF_FILE ILLEGAL SPACE UNCLOSED_STRING COMMENT FUNCTION
127 %type <string> table id
128 %type <column_list> selcollist column column_and_type column_def table_def
129 %type <column_list> column_assignment update_assign_list
130 %type <query> query from fromtable selectfrom unorderedsel
131 %type <query> oneupdate onedelete oneselect onequery onecreate oneinsert
132 %type <expr> expr val column_val const_val
133 %type <column_type> column_type data_type data_type_l data_count
134 %type <val_list> constlist
141 SQL_input* sql = (SQL_input*) info;
155 TK_INSERT TK_INTO table TK_LP selcollist TK_RP TK_VALUES TK_LP constlist TK_RP
157 SQL_input *sql = (SQL_input*) info;
158 MSIVIEW *insert = NULL;
161 r = INSERT_CreateView( sql->db, &insert, $3, $5, $9, FALSE );
166 | TK_INSERT TK_INTO table TK_LP selcollist TK_RP TK_VALUES TK_LP constlist TK_RP TK_TEMP
168 SQL_input *sql = (SQL_input*) info;
169 MSIVIEW *insert = NULL;
171 INSERT_CreateView( sql->db, &insert, $3, $5, $9, TRUE );
179 TK_CREATE TK_TABLE table TK_LP table_def TK_RP
181 SQL_input* sql = (SQL_input*) info;
182 MSIVIEW *create = NULL;
186 CREATE_CreateView( sql->db, &create, $3, $5, FALSE );
191 | TK_CREATE TK_TABLE table TK_LP table_def TK_RP TK_HOLD
193 SQL_input* sql = (SQL_input*) info;
194 MSIVIEW *create = NULL;
198 CREATE_CreateView( sql->db, &create, $3, $5, TRUE );
206 TK_UPDATE table TK_SET update_assign_list TK_WHERE expr
208 SQL_input* sql = (SQL_input*) info;
209 MSIVIEW *update = NULL;
211 UPDATE_CreateView( sql->db, &update, $2, $4, $6 );
221 SQL_input* sql = (SQL_input*) info;
222 MSIVIEW *delete = NULL;
224 DELETE_CreateView( sql->db, &delete, $2 );
232 column_def TK_PRIMARY TK_KEY selcollist
234 if( SQL_MarkPrimaryKeys( $1, $4 ) )
242 column_def TK_COMMA column_and_type
246 for( ci = $1; ci->next; ci = ci->next )
269 $$ = $1 | MSITYPE_VALID;
271 | data_type_l TK_LOCALIZABLE
273 FIXME("LOCALIZABLE ignored\n");
274 $$ = $1 | MSITYPE_VALID;
281 $$ |= MSITYPE_NULLABLE;
283 | data_type TK_NOT TK_NULL
292 $$ = MSITYPE_STRING | 1;
294 | TK_CHAR TK_LP data_count TK_RP
296 $$ = MSITYPE_STRING | 0x400 | $3;
323 SQL_input* sql = (SQL_input*) info;
324 int val = SQL_getint(sql);
325 if( ( val > 255 ) || ( val < 0 ) )
332 unorderedsel TK_ORDER TK_BY selcollist
334 SQL_input* sql = (SQL_input*) info;
338 ORDER_CreateView( sql->db, &$$, $1, $4 );
352 | TK_SELECT TK_DISTINCT selectfrom
354 SQL_input* sql = (SQL_input*) info;
357 DISTINCT_CreateView( sql->db, &$$, $3 );
366 SQL_input* sql = (SQL_input*) info;
370 SELECT_CreateView( sql->db, &$$, $2, $1 );
381 | column TK_COMMA selcollist
393 | fromtable TK_WHERE expr
395 SQL_input* sql = (SQL_input*) info;
399 r = WHERE_CreateView( sql->db, &$$, $1, $3 );
400 if( r != ERROR_SUCCESS || !$$ )
408 SQL_input* sql = (SQL_input*) info;
412 r = TABLE_CreateView( sql->db, $2, &$$ );
413 if( r != ERROR_SUCCESS || !$$ )
425 | column_val TK_EQ column_val
427 $$ = EXPR_complex( info, $1, OP_EQ, $3 );
433 $$ = EXPR_complex( info, $1, OP_AND, $3 );
439 $$ = EXPR_complex( info, $1, OP_OR, $3 );
443 | column_val TK_EQ val
445 $$ = EXPR_complex( info, $1, OP_EQ, $3 );
449 | column_val TK_GT val
451 $$ = EXPR_complex( info, $1, OP_GT, $3 );
455 | column_val TK_LT val
457 $$ = EXPR_complex( info, $1, OP_LT, $3 );
461 | column_val TK_LE val
463 $$ = EXPR_complex( info, $1, OP_LE, $3 );
467 | column_val TK_GE val
469 $$ = EXPR_complex( info, $1, OP_GE, $3 );
473 | column_val TK_NE val
475 $$ = EXPR_complex( info, $1, OP_NE, $3 );
479 | column_val TK_IS TK_NULL
481 $$ = EXPR_complex( info, $1, OP_ISNULL, NULL );
485 | column_val TK_IS TK_NOT TK_NULL
487 $$ = EXPR_complex( info, $1, OP_NOTNULL, NULL );
503 vals = parser_alloc( info, sizeof *vals );
510 | const_val TK_COMMA constlist
514 vals = parser_alloc( info, sizeof *vals );
525 | column_assignment TK_COMMA update_assign_list
533 column TK_EQ const_val
543 $$ = EXPR_ival( info, &$1, 1 );
547 | TK_MINUS TK_INTEGER
549 $$ = EXPR_ival( info, &$2, -1 );
555 $$ = EXPR_sval( info, &$1 );
561 $$ = EXPR_wildcard( info );
570 $$ = EXPR_column( info, $1 );
579 $$ = parser_alloc_column( info, $1, $3 );
585 $$ = parser_alloc_column( info, NULL, $1 );
601 $$ = SQL_getstring( info, &$1 );
609 static void *parser_alloc( void *info, unsigned int sz )
611 SQL_input* sql = (SQL_input*) info;
614 mem = HeapAlloc( GetProcessHeap(), 0, sizeof (struct list) + sz );
615 list_add_tail( sql->mem, mem );
619 static column_info *parser_alloc_column( void *info, LPCWSTR table, LPCWSTR column )
623 col = parser_alloc( info, sizeof (*col) );
627 col->column = column;
636 int SQL_lex( void *SQL_lval, SQL_input *sql )
639 struct sql_str * str = SQL_lval;
644 if( ! sql->command[sql->n] )
645 return 0; /* end of input */
647 TRACE("string : %s\n", debugstr_w(&sql->command[sql->n]));
648 sql->len = sqliteGetToken( &sql->command[sql->n], &token );
651 str->data = &sql->command[sql->n];
654 while( token == TK_SPACE );
656 TRACE("token : %d (%s)\n", token, debugstr_wn(&sql->command[sql->n], sql->len));
661 LPWSTR SQL_getstring( void *info, struct sql_str *strdata )
663 LPCWSTR p = strdata->data;
664 UINT len = strdata->len;
667 /* if there's quotes, remove them */
668 if( ( (p[0]=='`') && (p[len-1]=='`') ) ||
669 ( (p[0]=='\'') && (p[len-1]=='\'') ) )
674 str = parser_alloc( info, (len + 1)*sizeof(WCHAR) );
677 memcpy( str, p, len*sizeof(WCHAR) );
683 INT SQL_getint( void *info )
685 SQL_input* sql = (SQL_input*) info;
686 LPCWSTR p = &sql->command[sql->n];
691 int SQL_error( const char *str )
696 static struct expr * EXPR_wildcard( void *info )
698 struct expr *e = parser_alloc( info, sizeof *e );
701 e->type = EXPR_WILDCARD;
706 static struct expr * EXPR_complex( void *info, struct expr *l, UINT op, struct expr *r )
708 struct expr *e = parser_alloc( info, sizeof *e );
711 e->type = EXPR_COMPLEX;
719 static struct expr * EXPR_column( void *info, column_info *column )
721 struct expr *e = parser_alloc( info, sizeof *e );
724 e->type = EXPR_COLUMN;
725 e->u.sval = column->column;
730 static struct expr * EXPR_ival( void *info, struct sql_str *str, int sign )
732 struct expr *e = parser_alloc( info, sizeof *e );
736 e->u.ival = atoiW( str->data ) * sign;
741 static struct expr * EXPR_sval( void *info, struct sql_str *str )
743 struct expr *e = parser_alloc( info, sizeof *e );
747 e->u.sval = SQL_getstring( info, str );
752 static BOOL SQL_MarkPrimaryKeys( column_info *cols,
758 for( k = keys; k && found; k = k->next )
763 for( c = cols; c && !found; c = c->next )
765 if( lstrcmpW( k->column, c->column ) )
767 c->type |= MSITYPE_KEY;
775 UINT MSI_ParseSQL( MSIDATABASE *db, LPCWSTR command, MSIVIEW **phview,
784 sql.command = command;
792 TRACE("Parse returned %d\n", r);
796 (*sql.view)->ops->delete( *sql.view );
798 return ERROR_BAD_QUERY_SYNTAX;
801 return ERROR_SUCCESS;