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, 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 );
77 column_info *column_list;
84 %token TK_ALTER TK_AND TK_BY TK_CHAR TK_COMMA TK_CREATE TK_DELETE
85 %token TK_DISTINCT TK_DOT TK_EQ TK_FREE TK_FROM TK_GE TK_GT TK_HOLD TK_ADD
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
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
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 */
118 %left TK_EQ TK_NE TK_LT TK_GT TK_LE TK_GE TK_LIKE
126 SQL_input* sql = (SQL_input*) info;
141 TK_INSERT TK_INTO table TK_LP selcollist TK_RP TK_VALUES TK_LP constlist TK_RP
143 SQL_input *sql = (SQL_input*) info;
144 MSIVIEW *insert = NULL;
147 r = INSERT_CreateView( sql->db, &insert, $3, $5, $9, FALSE );
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 );
165 TK_CREATE TK_TABLE table TK_LP table_def TK_RP
167 SQL_input* sql = (SQL_input*) info;
168 MSIVIEW *create = NULL;
172 CREATE_CreateView( sql->db, &create, $3, $5, FALSE );
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;
184 CREATE_CreateView( sql->db, &create, $3, $5, TRUE );
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 );
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 );
217 SQL_input* sql = (SQL_input*) info;
218 MSIVIEW *delete = NULL;
220 DELETE_CreateView( sql->db, &delete, $2 );
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 );
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 );
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 );
272 column_def TK_PRIMARY TK_KEY selcollist
274 if( SQL_MarkPrimaryKeys( $1, $4 ) )
282 column_def TK_COMMA column_and_type
286 for( ci = $1; ci->next; ci = ci->next )
302 $$->type = ($2 | MSITYPE_VALID);
303 $$->temporary = $2 & MSITYPE_TEMPORARY ? TRUE : FALSE;
312 | data_type_l TK_LOCALIZABLE
314 $$ = $1 | MSITYPE_LOCALIZABLE;
316 | data_type_l TK_TEMPORARY
318 $$ = $1 | MSITYPE_TEMPORARY;
325 $$ |= MSITYPE_NULLABLE;
327 | data_type TK_NOT TK_NULL
336 $$ = MSITYPE_STRING | 1;
338 | TK_CHAR TK_LP data_count TK_RP
340 $$ = MSITYPE_STRING | 0x400 | $3;
360 $$ = MSITYPE_STRING | MSITYPE_VALID;
367 if( ( $1 > 255 ) || ( $1 < 0 ) )
374 unorderedsel TK_ORDER TK_BY selcollist
376 SQL_input* sql = (SQL_input*) info;
380 ORDER_CreateView( sql->db, &$$, $1, $4 );
394 | TK_SELECT TK_DISTINCT selectfrom
396 SQL_input* sql = (SQL_input*) info;
400 r = DISTINCT_CreateView( sql->db, &$$, $3 );
401 if (r != ERROR_SUCCESS)
412 SQL_input* sql = (SQL_input*) info;
418 r = SELECT_CreateView( sql->db, &$$, $2, $1 );
419 if (r != ERROR_SUCCESS)
432 | column TK_COMMA selcollist
444 | fromtable TK_WHERE expr
446 SQL_input* sql = (SQL_input*) info;
450 r = WHERE_CreateView( sql->db, &$$, $1, $3 );
451 if( r != ERROR_SUCCESS )
453 $1->ops->delete( $1 );
462 SQL_input* sql = (SQL_input*) info;
466 r = TABLE_CreateView( sql->db, $2, &$$ );
467 if( r != ERROR_SUCCESS || !$$ )
472 SQL_input* sql = (SQL_input*) info;
475 r = JOIN_CreateView( sql->db, &$$, $2 );
477 if( r != ERROR_SUCCESS )
488 table TK_COMMA tablelist
490 $$ = parser_add_table($3, $1);
505 $$ = EXPR_complex( info, $1, OP_AND, $3 );
511 $$ = EXPR_complex( info, $1, OP_OR, $3 );
515 | column_val TK_EQ val
517 $$ = EXPR_complex( info, $1, OP_EQ, $3 );
521 | column_val TK_GT val
523 $$ = EXPR_complex( info, $1, OP_GT, $3 );
527 | column_val TK_LT val
529 $$ = EXPR_complex( info, $1, OP_LT, $3 );
533 | column_val TK_LE val
535 $$ = EXPR_complex( info, $1, OP_LE, $3 );
539 | column_val TK_GE val
541 $$ = EXPR_complex( info, $1, OP_GE, $3 );
545 | column_val TK_NE val
547 $$ = EXPR_complex( info, $1, OP_NE, $3 );
551 | column_val TK_IS TK_NULL
553 $$ = EXPR_unary( info, $1, OP_ISNULL );
557 | column_val TK_IS TK_NOT TK_NULL
559 $$ = EXPR_unary( info, $1, OP_NOTNULL );
573 $$ = parser_alloc_column( info, NULL, NULL );
578 | const_val TK_COMMA constlist
580 $$ = parser_alloc_column( info, NULL, NULL );
590 | column_assignment TK_COMMA update_assign_list
598 column TK_EQ const_val
608 $$ = EXPR_ival( info, $1 );
612 | TK_MINUS number %prec TK_NEGATION
614 $$ = EXPR_ival( info, -$2 );
620 $$ = EXPR_sval( info, &$1 );
626 $$ = EXPR_wildcard( info );
635 $$ = EXPR_column( info, $1 );
644 $$ = parser_alloc_column( info, $1, $3 );
650 $$ = parser_alloc_column( info, NULL, $1 );
666 $$ = SQL_getstring( info, &$1 );
675 $$ = SQL_getint( info );
681 static LPWSTR parser_add_table(LPWSTR list, LPWSTR table)
683 DWORD size = lstrlenW(list) + lstrlenW(table) + 2;
684 static const WCHAR space[] = {' ',0};
686 list = msi_realloc(list, size * sizeof(WCHAR));
687 if (!list) return NULL;
689 lstrcatW(list, space);
690 lstrcatW(list, table);
694 static void *parser_alloc( void *info, unsigned int sz )
696 SQL_input* sql = (SQL_input*) info;
699 mem = msi_alloc( sizeof (struct list) + sz );
700 list_add_tail( sql->mem, mem );
704 static column_info *parser_alloc_column( void *info, LPCWSTR table, LPCWSTR column )
708 col = parser_alloc( info, sizeof (*col) );
712 col->column = column;
721 static int sql_lex( void *SQL_lval, SQL_input *sql )
724 struct sql_str * str = SQL_lval;
729 if( ! sql->command[sql->n] )
730 return 0; /* end of input */
732 /* TRACE("string : %s\n", debugstr_w(&sql->command[sql->n])); */
733 sql->len = sqliteGetToken( &sql->command[sql->n], &token );
736 str->data = &sql->command[sql->n];
739 while( token == TK_SPACE );
741 /* TRACE("token : %d (%s)\n", token, debugstr_wn(&sql->command[sql->n], sql->len)); */
746 LPWSTR SQL_getstring( void *info, const struct sql_str *strdata )
748 LPCWSTR p = strdata->data;
749 UINT len = strdata->len;
752 /* if there's quotes, remove them */
753 if( ( (p[0]=='`') && (p[len-1]=='`') ) ||
754 ( (p[0]=='\'') && (p[len-1]=='\'') ) )
759 str = parser_alloc( info, (len + 1)*sizeof(WCHAR) );
762 memcpy( str, p, len*sizeof(WCHAR) );
768 INT SQL_getint( void *info )
770 SQL_input* sql = (SQL_input*) info;
771 LPCWSTR p = &sql->command[sql->n];
774 for( i=0; i<sql->len; i++ )
776 if( '0' > p[i] || '9' < p[i] )
778 ERR("should only be numbers here!\n");
781 r = (p[i]-'0') + r*10;
787 static int sql_error( const char *str )
792 static struct expr * EXPR_wildcard( void *info )
794 struct expr *e = parser_alloc( info, sizeof *e );
797 e->type = EXPR_WILDCARD;
802 static struct expr * EXPR_complex( void *info, struct expr *l, UINT op, struct expr *r )
804 struct expr *e = parser_alloc( info, sizeof *e );
807 e->type = EXPR_COMPLEX;
815 static struct expr * EXPR_unary( void *info, struct expr *l, UINT op )
817 struct expr *e = parser_alloc( info, sizeof *e );
820 e->type = EXPR_UNARY;
823 e->u.expr.right = NULL;
828 static struct expr * EXPR_column( void *info, const column_info *column )
830 struct expr *e = parser_alloc( info, sizeof *e );
833 e->type = EXPR_COLUMN;
834 e->u.sval = column->column;
839 static struct expr * EXPR_ival( void *info, int val )
841 struct expr *e = parser_alloc( info, sizeof *e );
850 static struct expr * EXPR_sval( void *info, const struct sql_str *str )
852 struct expr *e = parser_alloc( info, sizeof *e );
856 e->u.sval = SQL_getstring( info, str );
861 static BOOL SQL_MarkPrimaryKeys( column_info *cols,
867 for( k = keys; k && found; k = k->next )
872 for( c = cols; c && !found; c = c->next )
874 if( lstrcmpW( k->column, c->column ) )
876 c->type |= MSITYPE_KEY;
884 UINT MSI_ParseSQL( MSIDATABASE *db, LPCWSTR command, MSIVIEW **phview,
893 sql.command = command;
901 TRACE("Parse returned %d\n", r);
905 return ERROR_BAD_QUERY_SYNTAX;
908 return ERROR_SUCCESS;