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
30 #include "wine/list.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL
(msi
);
35 static UINT SQL_getstring
( void *info
, const struct sql_str
*strdata
, LPWSTR
*str
);
36 static INT SQL_getint
( void *info
);
37 static int sql_lex
( void *SQL_lval
, SQL_input
*info
);
38 static int sql_error
( SQL_input
*info
, const char *str
);
40 static LPWSTR parser_add_table
( void *info
, LPCWSTR list
, LPCWSTR table
);
41 static void *parser_alloc
( void *info
, unsigned int sz
);
42 static column_info
*parser_alloc_column
( void *info
, LPCWSTR table
, LPCWSTR column
);
44 static BOOL SQL_MarkPrimaryKeys
( column_info
**cols
, column_info
*keys
);
46 static struct expr
* EXPR_complex
( void *info
, struct expr
*l
, UINT op
, struct expr
*r
);
47 static struct expr
* EXPR_unary
( void *info
, struct expr
*l
, UINT op
);
48 static struct expr
* EXPR_column
( void *info
, const column_info
*column
);
49 static struct expr
* EXPR_ival
( void *info
, int val
);
50 static struct expr
* EXPR_sval
( void *info
, const struct sql_str
*str
);
51 static struct expr
* EXPR_wildcard
( void *info
);
53 #define PARSER_BUBBLE_UP_VIEW( sql, result, current_view ) \
54 *sql
->view
= current_view
; \
59 %lex
-param
{ SQL_input
*info
}
60 %parse
-param
{ SQL_input
*info
}
67 column_info
*column_list
;
74 %token TK_ALTER TK_AND TK_BY TK_CHAR TK_COMMA TK_CREATE TK_DELETE TK_DROP
75 %token TK_DISTINCT TK_DOT TK_EQ TK_FREE TK_FROM TK_GE TK_GT TK_HOLD TK_ADD
77 %token TK_ILLEGAL TK_INSERT TK_INT
78 %token
<str
> TK_INTEGER
79 %token TK_INTO TK_IS TK_KEY TK_LE TK_LONG TK_LONGCHAR TK_LP TK_LT
80 %token TK_LOCALIZABLE TK_MINUS TK_NE TK_NOT TK_NULL
81 %token TK_OBJECT TK_OR TK_ORDER TK_PRIMARY TK_RP
82 %token TK_SELECT TK_SET TK_SHORT TK_SPACE TK_STAR
83 %token
<str
> TK_STRING
84 %token TK_TABLE TK_TEMPORARY TK_UPDATE TK_VALUES TK_WHERE TK_WILDCARD
87 * These are extra tokens used by the lexer but never seen by the
88 * parser. We put them in a rule so that the parser generator will
89 * add them to the parse.h output file.
92 %nonassoc END_OF_FILE ILLEGAL SPACE UNCLOSED_STRING COMMENT FUNCTION
95 %type
<string> table tablelist id
string
96 %type
<column_list
> selcollist collist selcolumn column column_and_type column_def table_def
97 %type
<column_list
> column_assignment update_assign_list constlist
98 %type
<query
> query from selectfrom unorderdfrom
99 %type
<query
> oneupdate onedelete oneselect onequery onecreate oneinsert onealter onedrop
100 %type
<expr
> expr val column_val const_val
101 %type
<column_type
> column_type data_type data_type_l data_count
102 %type
<integer
> number alterop
107 %left TK_EQ TK_NE TK_LT TK_GT TK_LE TK_GE TK_LIKE
115 SQL_input
* sql
= (SQL_input
*) info
;
131 TK_INSERT TK_INTO table TK_LP collist TK_RP TK_VALUES TK_LP constlist TK_RP
133 SQL_input
*sql
= (SQL_input
*) info
;
134 MSIVIEW
*insert
= NULL
;
136 INSERT_CreateView
( sql
->db
, &insert
, $3, $5, $9, FALSE
);
140 PARSER_BUBBLE_UP_VIEW
( sql
, $$
, insert
);
142 | TK_INSERT TK_INTO table TK_LP collist TK_RP TK_VALUES TK_LP constlist TK_RP TK_TEMPORARY
144 SQL_input
*sql
= (SQL_input
*) info
;
145 MSIVIEW
*insert
= NULL
;
147 INSERT_CreateView
( sql
->db
, &insert
, $3, $5, $9, TRUE
);
151 PARSER_BUBBLE_UP_VIEW
( sql
, $$
, insert
);
156 TK_CREATE TK_TABLE table TK_LP table_def TK_RP
158 SQL_input
* sql
= (SQL_input
*) info
;
159 MSIVIEW
*create
= NULL
;
164 r
= CREATE_CreateView
( sql
->db
, &create
, $3, $5, FALSE
);
171 PARSER_BUBBLE_UP_VIEW
( sql
, $$
, create
);
173 | TK_CREATE TK_TABLE table TK_LP table_def TK_RP TK_HOLD
175 SQL_input
* sql
= (SQL_input
*) info
;
176 MSIVIEW
*create
= NULL
;
180 CREATE_CreateView
( sql
->db
, &create
, $3, $5, TRUE
);
184 PARSER_BUBBLE_UP_VIEW
( sql
, $$
, create
);
189 TK_UPDATE table TK_SET update_assign_list TK_WHERE expr
191 SQL_input
* sql
= (SQL_input
*) info
;
192 MSIVIEW
*update
= NULL
;
194 UPDATE_CreateView
( sql
->db
, &update
, $2, $4, $6 );
198 PARSER_BUBBLE_UP_VIEW
( sql
, $$
, update
);
200 | TK_UPDATE table TK_SET update_assign_list
202 SQL_input
* sql
= (SQL_input
*) info
;
203 MSIVIEW
*update
= NULL
;
205 UPDATE_CreateView
( sql
->db
, &update
, $2, $4, NULL
);
209 PARSER_BUBBLE_UP_VIEW
( sql
, $$
, update
);
216 SQL_input
* sql
= (SQL_input
*) info
;
217 MSIVIEW
*delete
= NULL
;
219 DELETE_CreateView
( sql
->db
, &delete
, $2 );
223 PARSER_BUBBLE_UP_VIEW
( sql
, $$
, delete
);
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 );
237 PARSER_BUBBLE_UP_VIEW
( sql
, $$
, alter
);
239 | TK_ALTER TK_TABLE table TK_ADD column_and_type
241 SQL_input
*sql
= (SQL_input
*)info
;
242 MSIVIEW
*alter
= NULL
;
244 ALTER_CreateView
( sql
->db
, &alter
, $3, $5, 0 );
248 PARSER_BUBBLE_UP_VIEW
( sql
, $$
, alter
);
250 | TK_ALTER TK_TABLE table TK_ADD column_and_type TK_HOLD
252 SQL_input
*sql
= (SQL_input
*)info
;
253 MSIVIEW
*alter
= NULL
;
255 ALTER_CreateView
( sql
->db
, &alter
, $3, $5, 1 );
259 PARSER_BUBBLE_UP_VIEW
( sql
, $$
, alter
);
275 TK_DROP TK_TABLE table
277 SQL_input
* sql
= (SQL_input
*) info
;
278 MSIVIEW
* drop
= NULL
;
281 r
= DROP_CreateView
( sql
->db
, &drop
, $3 );
282 if
( r
!= ERROR_SUCCESS ||
!$$
)
285 PARSER_BUBBLE_UP_VIEW
( sql
, $$
, drop
);
290 column_def TK_PRIMARY TK_KEY collist
292 if
( SQL_MarkPrimaryKeys
( &$1, $4 ) )
300 column_def TK_COMMA column_and_type
304 for
( ci
= $1; ci
->next
; ci
= ci
->next
)
320 $$
->type
= ($2 | MSITYPE_VALID
);
321 $$
->temporary
= $2 & MSITYPE_TEMPORARY ? TRUE
: FALSE
;
330 | data_type_l TK_LOCALIZABLE
332 $$
= $1 | MSITYPE_LOCALIZABLE
;
334 | data_type_l TK_TEMPORARY
336 $$
= $1 | MSITYPE_TEMPORARY
;
343 $$ |
= MSITYPE_NULLABLE
;
345 | data_type TK_NOT TK_NULL
354 $$
= MSITYPE_STRING |
0x400;
356 | TK_CHAR TK_LP data_count TK_RP
358 $$
= MSITYPE_STRING |
0x400 |
$3;
362 $$
= MSITYPE_STRING |
0x400;
378 $$
= MSITYPE_STRING | MSITYPE_VALID
;
385 if
( ( $1 > 255 ) ||
( $1 < 0 ) )
396 | TK_SELECT TK_DISTINCT selectfrom
398 SQL_input
* sql
= (SQL_input
*) info
;
399 MSIVIEW
* distinct
= NULL
;
402 r
= DISTINCT_CreateView
( sql
->db
, &distinct
, $3 );
403 if
(r
!= ERROR_SUCCESS
)
406 PARSER_BUBBLE_UP_VIEW
( sql
, $$
, distinct
);
413 SQL_input
* sql
= (SQL_input
*) info
;
414 MSIVIEW
* select
= NULL
;
419 r
= SELECT_CreateView
( sql
->db
, &select
, $2, $1 );
420 if
(r
!= ERROR_SUCCESS
)
423 PARSER_BUBBLE_UP_VIEW
( sql
, $$
, select
);
432 | selcolumn TK_COMMA selcollist
444 | column TK_COMMA collist
457 SQL_input
* sql
= (SQL_input
*) info
;
458 MSIVIEW
* table
= NULL
;
461 r
= TABLE_CreateView
( sql
->db
, $2, &table
);
462 if
( r
!= ERROR_SUCCESS ||
!$$
)
465 PARSER_BUBBLE_UP_VIEW
( sql
, $$
, table
);
467 | unorderdfrom TK_ORDER TK_BY collist
473 r
= $1->ops
->sort
( $1, $4 );
474 if
( r
!= ERROR_SUCCESS
)
486 SQL_input
* sql
= (SQL_input
*) info
;
487 MSIVIEW
* where
= NULL
;
490 r
= WHERE_CreateView
( sql
->db
, &where
, $2, NULL
);
491 if
( r
!= ERROR_SUCCESS
)
494 PARSER_BUBBLE_UP_VIEW
( sql
, $$
, where
);
496 | TK_FROM tablelist TK_WHERE expr
498 SQL_input
* sql
= (SQL_input
*) info
;
499 MSIVIEW
* where
= NULL
;
502 r
= WHERE_CreateView
( sql
->db
, &where
, $2, $4 );
503 if
( r
!= ERROR_SUCCESS
)
506 PARSER_BUBBLE_UP_VIEW
( sql
, $$
, where
);
515 | table TK_COMMA tablelist
517 $$
= parser_add_table
( info
, $3, $1 );
532 $$
= EXPR_complex
( info
, $1, OP_AND
, $3 );
538 $$
= EXPR_complex
( info
, $1, OP_OR
, $3 );
542 | column_val TK_EQ val
544 $$
= EXPR_complex
( info
, $1, OP_EQ
, $3 );
548 | column_val TK_GT val
550 $$
= EXPR_complex
( info
, $1, OP_GT
, $3 );
554 | column_val TK_LT val
556 $$
= EXPR_complex
( info
, $1, OP_LT
, $3 );
560 | column_val TK_LE val
562 $$
= EXPR_complex
( info
, $1, OP_LE
, $3 );
566 | column_val TK_GE val
568 $$
= EXPR_complex
( info
, $1, OP_GE
, $3 );
572 | column_val TK_NE val
574 $$
= EXPR_complex
( info
, $1, OP_NE
, $3 );
578 | column_val TK_IS TK_NULL
580 $$
= EXPR_unary
( info
, $1, OP_ISNULL
);
584 | column_val TK_IS TK_NOT TK_NULL
586 $$
= EXPR_unary
( info
, $1, OP_NOTNULL
);
600 $$
= parser_alloc_column
( info
, NULL
, NULL
);
605 | const_val TK_COMMA constlist
607 $$
= parser_alloc_column
( info
, NULL
, NULL
);
617 | column_assignment TK_COMMA update_assign_list
625 column TK_EQ const_val
635 $$
= EXPR_ival
( info
, $1 );
639 | TK_MINUS number %prec TK_NEGATION
641 $$
= EXPR_ival
( info
, -$2 );
647 $$
= EXPR_sval
( info
, &$1 );
653 $$
= EXPR_wildcard
( info
);
659 $$
= EXPR_sval
( info
, NULL
);
668 $$
= EXPR_column
( info
, $1 );
677 $$
= parser_alloc_column
( info
, $1, $3 );
683 $$
= parser_alloc_column
( info
, NULL
, $1 );
692 $$
= parser_alloc_column
( info
, $1, $3 );
698 $$
= parser_alloc_column
( info
, NULL
, $1 );
704 $$
= parser_alloc_column
( info
, NULL
, $1 );
720 if
( SQL_getstring
( info
, &$1, &$$
) != ERROR_SUCCESS ||
!$$
)
728 if
( SQL_getstring
( info
, &$1, &$$
) != ERROR_SUCCESS ||
!$$
)
736 $$
= SQL_getint
( info
);
742 static LPWSTR parser_add_table
( void *info
, LPCWSTR list
, LPCWSTR table
)
744 static const WCHAR space
[] = {' ',0};
745 DWORD len
= lstrlenW
( list
) + lstrlenW
( table
) + 2;
748 ret
= parser_alloc
( info
, len
* sizeof
(WCHAR
) );
751 lstrcpyW
( ret
, list
);
752 lstrcatW
( ret
, space
);
753 lstrcatW
( ret
, table
);
758 static void *parser_alloc
( void *info
, unsigned int sz
)
760 SQL_input
* sql
= (SQL_input
*) info
;
763 mem
= msi_alloc
( sizeof
(struct list
) + sz
);
764 list_add_tail
( sql
->mem
, mem
);
768 static column_info
*parser_alloc_column
( void *info
, LPCWSTR table
, LPCWSTR column
)
772 col
= parser_alloc
( info
, sizeof
(*col
) );
776 col
->column
= column
;
785 static int sql_lex
( void *SQL_lval
, SQL_input
*sql
)
788 struct sql_str
* str
= SQL_lval
;
793 if
( ! sql
->command
[sql
->n
] )
794 return
0; /* end of input */
796 /* TRACE("string : %s\n", debugstr_w(&sql->command[sql->n])); */
797 sql
->len
= sqliteGetToken
( &sql
->command
[sql
->n
], &token
, &skip
);
800 str
->data
= &sql
->command
[sql
->n
];
804 while
( token
== TK_SPACE
);
806 /* TRACE("token : %d (%s)\n", token, debugstr_wn(&sql->command[sql->n], sql->len)); */
811 UINT SQL_getstring
( void *info
, const struct sql_str
*strdata
, LPWSTR
*str
)
813 LPCWSTR p
= strdata
->data
;
814 UINT len
= strdata
->len
;
817 if
( ( (p
[0]=='`') && (p
[len
-1]!='`') ) ||
818 ( (p
[0]=='\'') && (p
[len
-1]!='\'') ) )
819 return ERROR_FUNCTION_FAILED
;
821 /* if there are quotes, remove them */
822 if
( ( (p
[0]=='`') && (p
[len
-1]=='`') ) ||
823 ( (p
[0]=='\'') && (p
[len
-1]=='\'') ) )
828 *str
= parser_alloc
( info
, (len
+ 1)*sizeof
(WCHAR
) );
830 return ERROR_OUTOFMEMORY
;
831 memcpy
( *str
, p
, len
*sizeof
(WCHAR
) );
834 return ERROR_SUCCESS
;
837 INT SQL_getint
( void *info
)
839 SQL_input
* sql
= (SQL_input
*) info
;
840 LPCWSTR p
= &sql
->command
[sql
->n
];
843 for
( i
=0; i
<sql
->len
; i
++ )
845 if
( '0' > p
[i
] ||
'9' < p
[i
] )
847 ERR
("should only be numbers here!\n");
850 r
= (p
[i
]-'0') + r
*10;
856 static int sql_error
( SQL_input
*info
, const char *str
)
861 static struct expr
* EXPR_wildcard
( void *info
)
863 struct expr
*e
= parser_alloc
( info
, sizeof
*e
);
866 e
->type
= EXPR_WILDCARD
;
871 static struct expr
* EXPR_complex
( void *info
, struct expr
*l
, UINT op
, struct expr
*r
)
873 struct expr
*e
= parser_alloc
( info
, sizeof
*e
);
876 e
->type
= EXPR_COMPLEX
;
884 static struct expr
* EXPR_unary
( void *info
, struct expr
*l
, UINT op
)
886 struct expr
*e
= parser_alloc
( info
, sizeof
*e
);
889 e
->type
= EXPR_UNARY
;
892 e
->u.expr.right
= NULL
;
897 static struct expr
* EXPR_column
( void *info
, const column_info
*column
)
899 struct expr
*e
= parser_alloc
( info
, sizeof
*e
);
902 e
->type
= EXPR_COLUMN
;
903 e
->u.column.unparsed.column
= column
->column
;
904 e
->u.column.unparsed.table
= column
->table
;
909 static struct expr
* EXPR_ival
( void *info
, int val
)
911 struct expr
*e
= parser_alloc
( info
, sizeof
*e
);
920 static struct expr
* EXPR_sval
( void *info
, const struct sql_str
*str
)
922 struct expr
*e
= parser_alloc
( info
, sizeof
*e
);
926 if
( !str
) e
->u.sval
= NULL
;
927 else if
( SQL_getstring
( info
, str
, (LPWSTR
*)&e
->u.sval
) != ERROR_SUCCESS
)
928 return NULL
; /* e will be freed by query destructor */
933 static void swap_columns
( column_info
**cols
, column_info
*A
, int idx
)
935 column_info
*preA
= NULL
, *preB
= NULL
, *B
, *ptr
;
953 if
( preB
) preB
->next
= A
;
954 if
( preA
) preA
->next
= B
;
962 static BOOL SQL_MarkPrimaryKeys
( column_info
**cols
,
969 for
( k
= keys
, count
= 0; k
&& found
; k
= k
->next
, count
++ )
975 for
( c
= *cols
, idx
= 0; c
&& !found
; c
= c
->next
, idx
++ )
977 if
( wcscmp
( k
->column
, c
->column
) )
979 c
->type |
= MSITYPE_KEY
;
982 swap_columns
( cols
, c
, count
);
989 UINT MSI_ParseSQL
( MSIDATABASE
*db
, LPCWSTR command
, MSIVIEW
**phview
,
998 sql.command
= command
;
1001 sql.r
= ERROR_BAD_QUERY_SYNTAX
;
1005 r
= sql_parse
(&sql
);
1007 TRACE
("Parse returned %d\n", r
);
1012 (*sql.view
)->ops
->delete
(*sql.view
);
1018 return ERROR_SUCCESS
;