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"
35 #include "wine/unicode.h"
37 #define YYLEX_PARAM info
38 #define YYPARSE_PARAM info
40 static int sql_error(const char *str);
42 WINE_DEFAULT_DEBUG_CHANNEL(msi);
44 typedef struct tag_SQL_input
50 MSIVIEW **view; /* View structure for the resulting query. This value
51 * tracks the view currently being created so we can free
52 * this view on syntax error.
57 static UINT SQL_getstring( void *info, const struct sql_str *strdata, LPWSTR *str );
58 static INT SQL_getint( void *info );
59 static int sql_lex( void *SQL_lval, SQL_input *info );
61 static LPWSTR parser_add_table( void *info, LPCWSTR list, LPCWSTR table );
62 static void *parser_alloc( void *info, unsigned int sz );
63 static column_info *parser_alloc_column( void *info, LPCWSTR table, LPCWSTR column );
65 static BOOL SQL_MarkPrimaryKeys( column_info **cols, column_info *keys);
67 static struct expr * EXPR_complex( void *info, struct expr *l, UINT op, struct expr *r );
68 static struct expr * EXPR_unary( void *info, struct expr *l, UINT op );
69 static struct expr * EXPR_column( void *info, const column_info *column );
70 static struct expr * EXPR_ival( void *info, int val );
71 static struct expr * EXPR_sval( void *info, const struct sql_str *str );
72 static struct expr * EXPR_wildcard( void *info );
74 #define PARSER_BUBBLE_UP_VIEW( sql, result, current_view ) \
75 *sql->view = current_view; \
86 column_info *column_list;
93 %token TK_ALTER TK_AND TK_BY TK_CHAR TK_COMMA TK_CREATE TK_DELETE TK_DROP
94 %token TK_DISTINCT TK_DOT TK_EQ TK_FREE TK_FROM TK_GE TK_GT TK_HOLD TK_ADD
96 %token TK_ILLEGAL TK_INSERT TK_INT
97 %token <str> TK_INTEGER
98 %token TK_INTO TK_IS TK_KEY TK_LE TK_LONG TK_LONGCHAR TK_LP TK_LT
99 %token TK_LOCALIZABLE TK_MINUS TK_NE TK_NOT TK_NULL
100 %token TK_OBJECT TK_OR TK_ORDER TK_PRIMARY TK_RP
101 %token TK_SELECT TK_SET TK_SHORT TK_SPACE TK_STAR
102 %token <str> TK_STRING
103 %token TK_TABLE TK_TEMPORARY TK_UPDATE TK_VALUES TK_WHERE TK_WILDCARD
106 * These are extra tokens used by the lexer but never seen by the
107 * parser. We put them in a rule so that the parser generator will
108 * add them to the parse.h output file.
111 %nonassoc END_OF_FILE ILLEGAL SPACE UNCLOSED_STRING COMMENT FUNCTION
114 %type <string> table tablelist id string
115 %type <column_list> selcollist collist selcolumn column column_and_type column_def table_def
116 %type <column_list> column_assignment update_assign_list constlist
117 %type <query> query from selectfrom unorderdfrom
118 %type <query> oneupdate onedelete oneselect onequery onecreate oneinsert onealter onedrop
119 %type <expr> expr val column_val const_val
120 %type <column_type> column_type data_type data_type_l data_count
121 %type <integer> number alterop
126 %left TK_EQ TK_NE TK_LT TK_GT TK_LE TK_GE TK_LIKE
134 SQL_input* sql = (SQL_input*) info;
150 TK_INSERT TK_INTO table TK_LP collist TK_RP TK_VALUES TK_LP constlist TK_RP
152 SQL_input *sql = (SQL_input*) info;
153 MSIVIEW *insert = NULL;
155 INSERT_CreateView( sql->db, &insert, $3, $5, $9, FALSE );
159 PARSER_BUBBLE_UP_VIEW( sql, $$, insert );
161 | TK_INSERT TK_INTO table TK_LP collist TK_RP TK_VALUES TK_LP constlist TK_RP TK_TEMPORARY
163 SQL_input *sql = (SQL_input*) info;
164 MSIVIEW *insert = NULL;
166 INSERT_CreateView( sql->db, &insert, $3, $5, $9, TRUE );
170 PARSER_BUBBLE_UP_VIEW( sql, $$, insert );
175 TK_CREATE TK_TABLE table TK_LP table_def TK_RP
177 SQL_input* sql = (SQL_input*) info;
178 MSIVIEW *create = NULL;
183 r = CREATE_CreateView( sql->db, &create, $3, $5, FALSE );
190 PARSER_BUBBLE_UP_VIEW( sql, $$, create );
192 | TK_CREATE TK_TABLE table TK_LP table_def TK_RP TK_HOLD
194 SQL_input* sql = (SQL_input*) info;
195 MSIVIEW *create = NULL;
199 CREATE_CreateView( sql->db, &create, $3, $5, TRUE );
203 PARSER_BUBBLE_UP_VIEW( sql, $$, create );
208 TK_UPDATE table TK_SET update_assign_list TK_WHERE expr
210 SQL_input* sql = (SQL_input*) info;
211 MSIVIEW *update = NULL;
213 UPDATE_CreateView( sql->db, &update, $2, $4, $6 );
217 PARSER_BUBBLE_UP_VIEW( sql, $$, update );
219 | TK_UPDATE table TK_SET update_assign_list
221 SQL_input* sql = (SQL_input*) info;
222 MSIVIEW *update = NULL;
224 UPDATE_CreateView( sql->db, &update, $2, $4, NULL );
228 PARSER_BUBBLE_UP_VIEW( sql, $$, update );
235 SQL_input* sql = (SQL_input*) info;
236 MSIVIEW *delete = NULL;
238 DELETE_CreateView( sql->db, &delete, $2 );
242 PARSER_BUBBLE_UP_VIEW( sql, $$, delete );
247 TK_ALTER TK_TABLE table alterop
249 SQL_input* sql = (SQL_input*) info;
250 MSIVIEW *alter = NULL;
252 ALTER_CreateView( sql->db, &alter, $3, NULL, $4 );
256 PARSER_BUBBLE_UP_VIEW( sql, $$, alter );
258 | TK_ALTER TK_TABLE table TK_ADD column_and_type
260 SQL_input *sql = (SQL_input *)info;
261 MSIVIEW *alter = NULL;
263 ALTER_CreateView( sql->db, &alter, $3, $5, 0 );
267 PARSER_BUBBLE_UP_VIEW( sql, $$, alter );
269 | TK_ALTER TK_TABLE table TK_ADD column_and_type TK_HOLD
271 SQL_input *sql = (SQL_input *)info;
272 MSIVIEW *alter = NULL;
274 ALTER_CreateView( sql->db, &alter, $3, $5, 1 );
278 PARSER_BUBBLE_UP_VIEW( sql, $$, alter );
294 TK_DROP TK_TABLE table
296 SQL_input* sql = (SQL_input*) info;
297 MSIVIEW* drop = NULL;
300 r = DROP_CreateView( sql->db, &drop, $3 );
301 if( r != ERROR_SUCCESS || !$$ )
304 PARSER_BUBBLE_UP_VIEW( sql, $$, drop );
309 column_def TK_PRIMARY TK_KEY collist
311 if( SQL_MarkPrimaryKeys( &$1, $4 ) )
319 column_def TK_COMMA column_and_type
323 for( ci = $1; ci->next; ci = ci->next )
339 $$->type = ($2 | MSITYPE_VALID);
340 $$->temporary = $2 & MSITYPE_TEMPORARY ? TRUE : FALSE;
349 | data_type_l TK_LOCALIZABLE
351 $$ = $1 | MSITYPE_LOCALIZABLE;
353 | data_type_l TK_TEMPORARY
355 $$ = $1 | MSITYPE_TEMPORARY;
362 $$ |= MSITYPE_NULLABLE;
364 | data_type TK_NOT TK_NULL
373 $$ = MSITYPE_STRING | 1;
375 | TK_CHAR TK_LP data_count TK_RP
377 $$ = MSITYPE_STRING | 0x400 | $3;
381 $$ = MSITYPE_STRING | 0x400;
397 $$ = MSITYPE_STRING | MSITYPE_VALID;
404 if( ( $1 > 255 ) || ( $1 < 0 ) )
415 | TK_SELECT TK_DISTINCT selectfrom
417 SQL_input* sql = (SQL_input*) info;
418 MSIVIEW* distinct = NULL;
421 r = DISTINCT_CreateView( sql->db, &distinct, $3 );
422 if (r != ERROR_SUCCESS)
425 PARSER_BUBBLE_UP_VIEW( sql, $$, distinct );
432 SQL_input* sql = (SQL_input*) info;
433 MSIVIEW* select = NULL;
438 r = SELECT_CreateView( sql->db, &select, $2, $1 );
439 if (r != ERROR_SUCCESS)
442 PARSER_BUBBLE_UP_VIEW( sql, $$, select );
451 | selcolumn TK_COMMA selcollist
463 | column TK_COMMA collist
476 SQL_input* sql = (SQL_input*) info;
477 MSIVIEW* table = NULL;
480 r = TABLE_CreateView( sql->db, $2, &table );
481 if( r != ERROR_SUCCESS || !$$ )
484 PARSER_BUBBLE_UP_VIEW( sql, $$, table );
486 | unorderdfrom TK_ORDER TK_BY collist
492 r = $1->ops->sort( $1, $4 );
493 if ( r != ERROR_SUCCESS)
505 SQL_input* sql = (SQL_input*) info;
506 MSIVIEW* where = NULL;
509 r = WHERE_CreateView( sql->db, &where, $2, NULL );
510 if( r != ERROR_SUCCESS )
513 PARSER_BUBBLE_UP_VIEW( sql, $$, where );
515 | TK_FROM tablelist TK_WHERE expr
517 SQL_input* sql = (SQL_input*) info;
518 MSIVIEW* where = NULL;
521 r = WHERE_CreateView( sql->db, &where, $2, $4 );
522 if( r != ERROR_SUCCESS )
525 PARSER_BUBBLE_UP_VIEW( sql, $$, where );
534 | table TK_COMMA tablelist
536 $$ = parser_add_table( info, $3, $1 );
551 $$ = EXPR_complex( info, $1, OP_AND, $3 );
557 $$ = EXPR_complex( info, $1, OP_OR, $3 );
561 | column_val TK_EQ val
563 $$ = EXPR_complex( info, $1, OP_EQ, $3 );
567 | column_val TK_GT val
569 $$ = EXPR_complex( info, $1, OP_GT, $3 );
573 | column_val TK_LT val
575 $$ = EXPR_complex( info, $1, OP_LT, $3 );
579 | column_val TK_LE val
581 $$ = EXPR_complex( info, $1, OP_LE, $3 );
585 | column_val TK_GE val
587 $$ = EXPR_complex( info, $1, OP_GE, $3 );
591 | column_val TK_NE val
593 $$ = EXPR_complex( info, $1, OP_NE, $3 );
597 | column_val TK_IS TK_NULL
599 $$ = EXPR_unary( info, $1, OP_ISNULL );
603 | column_val TK_IS TK_NOT TK_NULL
605 $$ = EXPR_unary( info, $1, OP_NOTNULL );
619 $$ = parser_alloc_column( info, NULL, NULL );
624 | const_val TK_COMMA constlist
626 $$ = parser_alloc_column( info, NULL, NULL );
636 | column_assignment TK_COMMA update_assign_list
644 column TK_EQ const_val
654 $$ = EXPR_ival( info, $1 );
658 | TK_MINUS number %prec TK_NEGATION
660 $$ = EXPR_ival( info, -$2 );
666 $$ = EXPR_sval( info, &$1 );
672 $$ = EXPR_wildcard( info );
681 $$ = EXPR_column( info, $1 );
690 $$ = parser_alloc_column( info, $1, $3 );
696 $$ = parser_alloc_column( info, NULL, $1 );
705 $$ = parser_alloc_column( info, $1, $3 );
711 $$ = parser_alloc_column( info, NULL, $1 );
717 $$ = parser_alloc_column( info, NULL, $1 );
733 if ( SQL_getstring( info, &$1, &$$ ) != ERROR_SUCCESS || !$$ )
741 if ( SQL_getstring( info, &$1, &$$ ) != ERROR_SUCCESS || !$$ )
749 $$ = SQL_getint( info );
755 static LPWSTR parser_add_table( void *info, LPCWSTR list, LPCWSTR table )
757 static const WCHAR space[] = {' ',0};
758 DWORD len = strlenW( list ) + strlenW( table ) + 2;
761 ret = parser_alloc( info, len * sizeof(WCHAR) );
764 strcpyW( ret, list );
765 strcatW( ret, space );
766 strcatW( ret, table );
771 static void *parser_alloc( void *info, unsigned int sz )
773 SQL_input* sql = (SQL_input*) info;
776 mem = msi_alloc( sizeof (struct list) + sz );
777 list_add_tail( sql->mem, mem );
781 static column_info *parser_alloc_column( void *info, LPCWSTR table, LPCWSTR column )
785 col = parser_alloc( info, sizeof (*col) );
789 col->column = column;
798 static int sql_lex( void *SQL_lval, SQL_input *sql )
801 struct sql_str * str = SQL_lval;
806 if( ! sql->command[sql->n] )
807 return 0; /* end of input */
809 /* TRACE("string : %s\n", debugstr_w(&sql->command[sql->n])); */
810 sql->len = sqliteGetToken( &sql->command[sql->n], &token, &skip );
813 str->data = &sql->command[sql->n];
817 while( token == TK_SPACE );
819 /* TRACE("token : %d (%s)\n", token, debugstr_wn(&sql->command[sql->n], sql->len)); */
824 UINT SQL_getstring( void *info, const struct sql_str *strdata, LPWSTR *str )
826 LPCWSTR p = strdata->data;
827 UINT len = strdata->len;
830 if( ( (p[0]=='`') && (p[len-1]!='`') ) ||
831 ( (p[0]=='\'') && (p[len-1]!='\'') ) )
832 return ERROR_FUNCTION_FAILED;
834 /* if there's quotes, remove them */
835 if( ( (p[0]=='`') && (p[len-1]=='`') ) ||
836 ( (p[0]=='\'') && (p[len-1]=='\'') ) )
841 *str = parser_alloc( info, (len + 1)*sizeof(WCHAR) );
843 return ERROR_OUTOFMEMORY;
844 memcpy( *str, p, len*sizeof(WCHAR) );
847 return ERROR_SUCCESS;
850 INT SQL_getint( void *info )
852 SQL_input* sql = (SQL_input*) info;
853 LPCWSTR p = &sql->command[sql->n];
856 for( i=0; i<sql->len; i++ )
858 if( '0' > p[i] || '9' < p[i] )
860 ERR("should only be numbers here!\n");
863 r = (p[i]-'0') + r*10;
869 static int sql_error( const char *str )
874 static struct expr * EXPR_wildcard( void *info )
876 struct expr *e = parser_alloc( info, sizeof *e );
879 e->type = EXPR_WILDCARD;
884 static struct expr * EXPR_complex( void *info, struct expr *l, UINT op, struct expr *r )
886 struct expr *e = parser_alloc( info, sizeof *e );
889 e->type = EXPR_COMPLEX;
897 static struct expr * EXPR_unary( void *info, struct expr *l, UINT op )
899 struct expr *e = parser_alloc( info, sizeof *e );
902 e->type = EXPR_UNARY;
905 e->u.expr.right = NULL;
910 static struct expr * EXPR_column( void *info, const column_info *column )
912 struct expr *e = parser_alloc( info, sizeof *e );
915 e->type = EXPR_COLUMN;
916 e->u.column.unparsed.column = column->column;
917 e->u.column.unparsed.table = column->table;
922 static struct expr * EXPR_ival( void *info, int val )
924 struct expr *e = parser_alloc( info, sizeof *e );
933 static struct expr * EXPR_sval( void *info, const struct sql_str *str )
935 struct expr *e = parser_alloc( info, sizeof *e );
939 if( SQL_getstring( info, str, (LPWSTR *)&e->u.sval ) != ERROR_SUCCESS )
940 return NULL; /* e will be freed by query destructor */
945 static void swap_columns( column_info **cols, column_info *A, int idx )
947 column_info *preA = NULL, *preB = NULL, *B, *ptr;
965 if( preB ) preB->next = A;
966 if( preA ) preA->next = B;
974 static BOOL SQL_MarkPrimaryKeys( column_info **cols,
981 for( k = keys, count = 0; k && found; k = k->next, count++ )
987 for( c = *cols, idx = 0; c && !found; c = c->next, idx++ )
989 if( strcmpW( k->column, c->column ) )
991 c->type |= MSITYPE_KEY;
994 swap_columns( cols, c, count );
1001 UINT MSI_ParseSQL( MSIDATABASE *db, LPCWSTR command, MSIVIEW **phview,
1010 sql.command = command;
1013 sql.r = ERROR_BAD_QUERY_SYNTAX;
1017 r = sql_parse(&sql);
1019 TRACE("Parse returned %d\n", r);
1024 (*sql.view)->ops->delete(*sql.view);
1030 return ERROR_SUCCESS;