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_column( void *info, column_info *column );
63 static struct expr * EXPR_ival( void *info, int val );
64 static struct expr * EXPR_sval( void *info, struct sql_str * );
65 static struct expr * EXPR_wildcard( void *info );
75 column_info *column_list;
82 %token TK_ABORT TK_AFTER TK_AGG_FUNCTION TK_ALL TK_ALTER TK_AND TK_AS TK_ASC
83 %token TK_BEFORE TK_BEGIN TK_BETWEEN TK_BITAND TK_BITNOT TK_BITOR TK_BY
84 %token TK_CASCADE TK_CASE TK_CHAR TK_CHECK TK_CLUSTER TK_COLLATE TK_COLUMN
85 %token TK_COMMA TK_COMMENT TK_COMMIT TK_CONCAT TK_CONFLICT
86 %token TK_CONSTRAINT TK_COPY TK_CREATE
87 %token TK_DEFAULT TK_DEFERRABLE TK_DEFERRED TK_DELETE TK_DELIMITERS TK_DESC
88 %token TK_DISTINCT TK_DOT TK_DROP TK_EACH
89 %token TK_ELSE TK_END TK_END_OF_FILE TK_EQ TK_EXCEPT TK_EXPLAIN
90 %token TK_FAIL TK_FLOAT TK_FOR TK_FOREIGN TK_FREE TK_FROM TK_FUNCTION
91 %token TK_GE TK_GLOB TK_GROUP TK_GT
92 %token TK_HAVING TK_HOLD
93 %token TK_IGNORE TK_ILLEGAL TK_IMMEDIATE TK_IN TK_INDEX TK_INITIALLY
95 %token TK_INSERT TK_INSTEAD TK_INT
96 %token <str> TK_INTEGER
97 %token TK_INTERSECT TK_INTO TK_IS
99 %token TK_JOIN TK_JOIN_KW
101 %token TK_LE TK_LIKE TK_LIMIT TK_LONG TK_LONGCHAR TK_LP TK_LSHIFT TK_LT
102 %token TK_LOCALIZABLE
103 %token TK_MATCH TK_MINUS
104 %token TK_NE TK_NOT TK_NOTNULL TK_NULL
105 %token TK_OBJECT TK_OF TK_OFFSET TK_ON TK_OR TK_ORACLE_OUTER_JOIN TK_ORDER
106 %token TK_PLUS TK_PRAGMA TK_PRIMARY
107 %token TK_RAISE TK_REFERENCES TK_REM TK_REPLACE TK_RESTRICT TK_ROLLBACK
108 %token TK_ROW TK_RP TK_RSHIFT
109 %token TK_SELECT TK_SEMI TK_SET TK_SHORT TK_SLASH TK_SPACE TK_STAR TK_STATEMENT
110 %token <str> TK_STRING
111 %token TK_TABLE TK_TEMPORARY TK_THEN TK_TRANSACTION TK_TRIGGER
112 %token TK_UMINUS TK_UNCLOSED_STRING TK_UNION TK_UNIQUE
113 %token TK_UPDATE TK_UPLUS TK_USING
114 %token TK_VACUUM TK_VALUES TK_VIEW
115 %token TK_WHEN TK_WHERE TK_WILDCARD
118 * These are extra tokens used by the lexer but never seen by the
119 * parser. We put them in a rule so that the parser generator will
120 * add them to the parse.h output file.
123 %nonassoc END_OF_FILE ILLEGAL SPACE UNCLOSED_STRING COMMENT FUNCTION
126 %type <string> table id
127 %type <column_list> selcollist column column_and_type column_def table_def
128 %type <column_list> column_assignment update_assign_list constlist
129 %type <query> query multifrom from fromtable selectfrom unorderedsel
130 %type <query> oneupdate onedelete oneselect onequery onecreate oneinsert onealter
131 %type <expr> expr val column_val const_val
132 %type <column_type> column_type data_type data_type_l data_count
133 %type <integer> number alterop
135 /* Reference: http://mates.ms.mff.cuni.cz/oracle/doc/ora815nt/server.815/a67779/operator.htm */
139 %left TK_EQ TK_NE TK_LT TK_GT TK_LE TK_GE TK_ISNULL TK_LIKE TK_BETWEEN TK_IN
140 %left TK_PLUS TK_MINUS TK_CONCAT
148 SQL_input* sql = (SQL_input*) info;
163 TK_INSERT TK_INTO table TK_LP selcollist TK_RP TK_VALUES TK_LP constlist TK_RP
165 SQL_input *sql = (SQL_input*) info;
166 MSIVIEW *insert = NULL;
169 r = INSERT_CreateView( sql->db, &insert, $3, $5, $9, FALSE );
174 | TK_INSERT TK_INTO table TK_LP selcollist TK_RP TK_VALUES TK_LP constlist TK_RP TK_TEMPORARY
176 SQL_input *sql = (SQL_input*) info;
177 MSIVIEW *insert = NULL;
179 INSERT_CreateView( sql->db, &insert, $3, $5, $9, TRUE );
187 TK_CREATE TK_TABLE table TK_LP table_def TK_RP
189 SQL_input* sql = (SQL_input*) info;
190 MSIVIEW *create = NULL;
194 CREATE_CreateView( sql->db, &create, $3, $5, FALSE );
199 | TK_CREATE TK_TABLE table TK_LP table_def TK_RP TK_HOLD
201 SQL_input* sql = (SQL_input*) info;
202 MSIVIEW *create = NULL;
206 CREATE_CreateView( sql->db, &create, $3, $5, TRUE );
214 TK_UPDATE table TK_SET update_assign_list TK_WHERE expr
216 SQL_input* sql = (SQL_input*) info;
217 MSIVIEW *update = NULL;
219 UPDATE_CreateView( sql->db, &update, $2, $4, $6 );
229 SQL_input* sql = (SQL_input*) info;
230 MSIVIEW *delete = NULL;
232 DELETE_CreateView( sql->db, &delete, $2 );
240 TK_ALTER TK_TABLE table alterop
242 SQL_input* sql = (SQL_input*) info;
243 MSIVIEW *alter = NULL;
245 ALTER_CreateView( sql->db, &alter, $3, $4 );
264 column_def TK_PRIMARY TK_KEY selcollist
266 if( SQL_MarkPrimaryKeys( $1, $4 ) )
274 column_def TK_COMMA column_and_type
278 for( ci = $1; ci->next; ci = ci->next )
294 $$->type = $2 | MSITYPE_VALID;
303 | data_type_l TK_LOCALIZABLE
305 $$ = $1 | MSITYPE_LOCALIZABLE;
307 | data_type_l TK_TEMPORARY
309 FIXME("temporary column\n");
316 $$ |= MSITYPE_NULLABLE;
318 | data_type TK_NOT TK_NULL
327 $$ = MSITYPE_STRING | 1;
329 | TK_CHAR TK_LP data_count TK_RP
331 $$ = MSITYPE_STRING | 0x400 | $3;
351 $$ = MSITYPE_STRING | MSITYPE_VALID;
358 if( ( $1 > 255 ) || ( $1 < 0 ) )
365 unorderedsel TK_ORDER TK_BY selcollist
367 SQL_input* sql = (SQL_input*) info;
371 ORDER_CreateView( sql->db, &$$, $1, $4 );
385 | TK_SELECT TK_DISTINCT selectfrom
387 SQL_input* sql = (SQL_input*) info;
391 r = DISTINCT_CreateView( sql->db, &$$, $3 );
392 if (r != ERROR_SUCCESS)
403 SQL_input* sql = (SQL_input*) info;
409 r = SELECT_CreateView( sql->db, &$$, $2, $1 );
410 if (r != ERROR_SUCCESS)
423 | column TK_COMMA selcollist
435 | TK_FROM table TK_COMMA table TK_WHERE expr
437 SQL_input* sql = (SQL_input*) info;
440 /* only support inner joins on two tables */
441 r = JOIN_CreateView( sql->db, &$$, $2, $4, $6 );
442 if( r != ERROR_SUCCESS )
449 | fromtable TK_WHERE expr
451 SQL_input* sql = (SQL_input*) info;
455 r = WHERE_CreateView( sql->db, &$$, $1, $3 );
456 if( r != ERROR_SUCCESS )
458 $1->ops->delete( $1 );
467 SQL_input* sql = (SQL_input*) info;
471 r = TABLE_CreateView( sql->db, $2, &$$ );
472 if( r != ERROR_SUCCESS || !$$ )
486 $$ = EXPR_complex( info, $1, OP_AND, $3 );
492 $$ = EXPR_complex( info, $1, OP_OR, $3 );
496 | column_val TK_EQ val
498 $$ = EXPR_complex( info, $1, OP_EQ, $3 );
502 | column_val TK_GT val
504 $$ = EXPR_complex( info, $1, OP_GT, $3 );
508 | column_val TK_LT val
510 $$ = EXPR_complex( info, $1, OP_LT, $3 );
514 | column_val TK_LE val
516 $$ = EXPR_complex( info, $1, OP_LE, $3 );
520 | column_val TK_GE val
522 $$ = EXPR_complex( info, $1, OP_GE, $3 );
526 | column_val TK_NE val
528 $$ = EXPR_complex( info, $1, OP_NE, $3 );
532 | column_val TK_IS TK_NULL
534 $$ = EXPR_complex( info, $1, OP_ISNULL, NULL );
538 | column_val TK_IS TK_NOT TK_NULL
540 $$ = EXPR_complex( info, $1, OP_NOTNULL, NULL );
554 $$ = parser_alloc_column( info, NULL, NULL );
559 | const_val TK_COMMA constlist
561 $$ = parser_alloc_column( info, NULL, NULL );
571 | column_assignment TK_COMMA update_assign_list
579 column TK_EQ const_val
589 $$ = EXPR_ival( info, $1 );
593 | TK_MINUS number %prec TK_NEGATION
595 $$ = EXPR_ival( info, -$2 );
601 $$ = EXPR_sval( info, &$1 );
607 $$ = EXPR_wildcard( info );
616 $$ = EXPR_column( info, $1 );
625 $$ = parser_alloc_column( info, $1, $3 );
631 $$ = parser_alloc_column( info, NULL, $1 );
647 $$ = SQL_getstring( info, &$1 );
656 $$ = SQL_getint( info );
662 static void *parser_alloc( void *info, unsigned int sz )
664 SQL_input* sql = (SQL_input*) info;
667 mem = msi_alloc( sizeof (struct list) + sz );
668 list_add_tail( sql->mem, mem );
672 static column_info *parser_alloc_column( void *info, LPCWSTR table, LPCWSTR column )
676 col = parser_alloc( info, sizeof (*col) );
680 col->column = column;
689 static int sql_lex( void *SQL_lval, SQL_input *sql )
692 struct sql_str * str = SQL_lval;
697 if( ! sql->command[sql->n] )
698 return 0; /* end of input */
700 /* TRACE("string : %s\n", debugstr_w(&sql->command[sql->n])); */
701 sql->len = sqliteGetToken( &sql->command[sql->n], &token );
704 str->data = &sql->command[sql->n];
707 while( token == TK_SPACE );
709 /* TRACE("token : %d (%s)\n", token, debugstr_wn(&sql->command[sql->n], sql->len)); */
714 LPWSTR SQL_getstring( void *info, struct sql_str *strdata )
716 LPCWSTR p = strdata->data;
717 UINT len = strdata->len;
720 /* if there's quotes, remove them */
721 if( ( (p[0]=='`') && (p[len-1]=='`') ) ||
722 ( (p[0]=='\'') && (p[len-1]=='\'') ) )
727 str = parser_alloc( info, (len + 1)*sizeof(WCHAR) );
730 memcpy( str, p, len*sizeof(WCHAR) );
736 INT SQL_getint( void *info )
738 SQL_input* sql = (SQL_input*) info;
739 LPCWSTR p = &sql->command[sql->n];
742 for( i=0; i<sql->len; i++ )
744 if( '0' > p[i] || '9' < p[i] )
746 ERR("should only be numbers here!\n");
749 r = (p[i]-'0') + r*10;
755 static int sql_error( const char *str )
760 static struct expr * EXPR_wildcard( void *info )
762 struct expr *e = parser_alloc( info, sizeof *e );
765 e->type = EXPR_WILDCARD;
770 static struct expr * EXPR_complex( void *info, struct expr *l, UINT op, struct expr *r )
772 struct expr *e = parser_alloc( info, sizeof *e );
775 e->type = EXPR_COMPLEX;
783 static struct expr * EXPR_column( void *info, column_info *column )
785 struct expr *e = parser_alloc( info, sizeof *e );
788 e->type = EXPR_COLUMN;
789 e->u.sval = column->column;
794 static struct expr * EXPR_ival( void *info, int val )
796 struct expr *e = parser_alloc( info, sizeof *e );
805 static struct expr * EXPR_sval( void *info, struct sql_str *str )
807 struct expr *e = parser_alloc( info, sizeof *e );
811 e->u.sval = SQL_getstring( info, str );
816 static BOOL SQL_MarkPrimaryKeys( column_info *cols,
822 for( k = keys; k && found; k = k->next )
827 for( c = cols; c && !found; c = c->next )
829 if( lstrcmpW( k->column, c->column ) )
831 c->type |= MSITYPE_KEY;
839 UINT MSI_ParseSQL( MSIDATABASE *db, LPCWSTR command, MSIVIEW **phview,
848 sql.command = command;
856 TRACE("Parse returned %d\n", r);
860 return ERROR_BAD_QUERY_SYNTAX;
863 return ERROR_SUCCESS;