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
);
329 | data_type_l TK_LOCALIZABLE
331 $$
= $1 | MSITYPE_LOCALIZABLE
;
333 | data_type_l TK_TEMPORARY
335 $$
= $1 | MSITYPE_TEMPORARY
;
342 $$ |
= MSITYPE_NULLABLE
;
344 | data_type TK_NOT TK_NULL
353 $$
= MSITYPE_STRING |
0x400;
355 | TK_CHAR TK_LP data_count TK_RP
357 $$
= MSITYPE_STRING |
0x400 |
$3;
361 $$
= MSITYPE_STRING |
0x400;
377 $$
= MSITYPE_STRING | MSITYPE_VALID
;
384 if
( ( $1 > 255 ) ||
( $1 < 0 ) )
395 | TK_SELECT TK_DISTINCT selectfrom
397 SQL_input
* sql
= (SQL_input
*) info
;
398 MSIVIEW
* distinct
= NULL
;
401 r
= DISTINCT_CreateView
( sql
->db
, &distinct
, $3 );
402 if
(r
!= ERROR_SUCCESS
)
405 PARSER_BUBBLE_UP_VIEW
( sql
, $$
, distinct
);
412 SQL_input
* sql
= (SQL_input
*) info
;
413 MSIVIEW
* select
= NULL
;
418 r
= SELECT_CreateView
( sql
->db
, &select
, $2, $1 );
419 if
(r
!= ERROR_SUCCESS
)
422 PARSER_BUBBLE_UP_VIEW
( sql
, $$
, select
);
431 | selcolumn TK_COMMA selcollist
443 | column TK_COMMA collist
456 SQL_input
* sql
= (SQL_input
*) info
;
457 MSIVIEW
* table
= NULL
;
460 r
= TABLE_CreateView
( sql
->db
, $2, &table
);
461 if
( r
!= ERROR_SUCCESS ||
!$$
)
464 PARSER_BUBBLE_UP_VIEW
( sql
, $$
, table
);
466 | unorderdfrom TK_ORDER TK_BY collist
472 r
= $1->ops
->sort
( $1, $4 );
473 if
( r
!= ERROR_SUCCESS
)
485 SQL_input
* sql
= (SQL_input
*) info
;
486 MSIVIEW
* where
= NULL
;
489 r
= WHERE_CreateView
( sql
->db
, &where
, $2, NULL
);
490 if
( r
!= ERROR_SUCCESS
)
493 PARSER_BUBBLE_UP_VIEW
( sql
, $$
, where
);
495 | TK_FROM tablelist TK_WHERE expr
497 SQL_input
* sql
= (SQL_input
*) info
;
498 MSIVIEW
* where
= NULL
;
501 r
= WHERE_CreateView
( sql
->db
, &where
, $2, $4 );
502 if
( r
!= ERROR_SUCCESS
)
505 PARSER_BUBBLE_UP_VIEW
( sql
, $$
, where
);
514 | table TK_COMMA tablelist
516 $$
= parser_add_table
( info
, $3, $1 );
531 $$
= EXPR_complex
( info
, $1, OP_AND
, $3 );
537 $$
= EXPR_complex
( info
, $1, OP_OR
, $3 );
541 | column_val TK_EQ val
543 $$
= EXPR_complex
( info
, $1, OP_EQ
, $3 );
547 | column_val TK_GT val
549 $$
= EXPR_complex
( info
, $1, OP_GT
, $3 );
553 | column_val TK_LT val
555 $$
= EXPR_complex
( info
, $1, OP_LT
, $3 );
559 | column_val TK_LE val
561 $$
= EXPR_complex
( info
, $1, OP_LE
, $3 );
565 | column_val TK_GE val
567 $$
= EXPR_complex
( info
, $1, OP_GE
, $3 );
571 | column_val TK_NE val
573 $$
= EXPR_complex
( info
, $1, OP_NE
, $3 );
577 | column_val TK_IS TK_NULL
579 $$
= EXPR_unary
( info
, $1, OP_ISNULL
);
583 | column_val TK_IS TK_NOT TK_NULL
585 $$
= EXPR_unary
( info
, $1, OP_NOTNULL
);
599 $$
= parser_alloc_column
( info
, NULL
, NULL
);
604 | const_val TK_COMMA constlist
606 $$
= parser_alloc_column
( info
, NULL
, NULL
);
616 | column_assignment TK_COMMA update_assign_list
624 column TK_EQ const_val
634 $$
= EXPR_ival
( info
, $1 );
638 | TK_MINUS number %prec TK_NEGATION
640 $$
= EXPR_ival
( info
, -$2 );
646 $$
= EXPR_sval
( info
, &$1 );
652 $$
= EXPR_wildcard
( info
);
658 $$
= EXPR_sval
( info
, NULL
);
667 $$
= EXPR_column
( info
, $1 );
676 $$
= parser_alloc_column
( info
, $1, $3 );
682 $$
= parser_alloc_column
( info
, NULL
, $1 );
691 $$
= parser_alloc_column
( info
, $1, $3 );
697 $$
= parser_alloc_column
( info
, NULL
, $1 );
703 $$
= parser_alloc_column
( info
, NULL
, $1 );
719 if
( SQL_getstring
( info
, &$1, &$$
) != ERROR_SUCCESS ||
!$$
)
727 if
( SQL_getstring
( info
, &$1, &$$
) != ERROR_SUCCESS ||
!$$
)
735 $$
= SQL_getint
( info
);
741 static LPWSTR parser_add_table
( void *info
, LPCWSTR list
, LPCWSTR table
)
743 DWORD len
= lstrlenW
( list
) + lstrlenW
( table
) + 2;
746 ret
= parser_alloc
( info
, len
* sizeof
(WCHAR
) );
749 lstrcpyW
( ret
, list
);
750 lstrcatW
( ret
, L
" " );
751 lstrcatW
( ret
, table
);
756 static void *parser_alloc
( void *info
, unsigned int sz
)
758 SQL_input
* sql
= (SQL_input
*) info
;
761 mem
= msi_alloc
( sizeof
(struct list
) + sz
);
762 list_add_tail
( sql
->mem
, mem
);
766 static column_info
*parser_alloc_column
( void *info
, LPCWSTR table
, LPCWSTR column
)
770 col
= parser_alloc
( info
, sizeof
(*col
) );
774 col
->column
= column
;
783 static int sql_lex
( void *SQL_lval
, SQL_input
*sql
)
786 struct sql_str
* str
= SQL_lval
;
791 if
( ! sql
->command
[sql
->n
] )
792 return
0; /* end of input */
794 /* TRACE("string : %s\n", debugstr_w(&sql->command[sql->n])); */
795 sql
->len
= sqliteGetToken
( &sql
->command
[sql
->n
], &token
, &skip
);
798 str
->data
= &sql
->command
[sql
->n
];
802 while
( token
== TK_SPACE
);
804 /* TRACE("token : %d (%s)\n", token, debugstr_wn(&sql->command[sql->n], sql->len)); */
809 UINT SQL_getstring
( void *info
, const struct sql_str
*strdata
, LPWSTR
*str
)
811 LPCWSTR p
= strdata
->data
;
812 UINT len
= strdata
->len
;
815 if
( ( (p
[0]=='`') && (p
[len
-1]!='`') ) ||
816 ( (p
[0]=='\'') && (p
[len
-1]!='\'') ) )
817 return ERROR_FUNCTION_FAILED
;
819 /* if there are quotes, remove them */
820 if
( ( (p
[0]=='`') && (p
[len
-1]=='`') ) ||
821 ( (p
[0]=='\'') && (p
[len
-1]=='\'') ) )
826 *str
= parser_alloc
( info
, (len
+ 1)*sizeof
(WCHAR
) );
828 return ERROR_OUTOFMEMORY
;
829 memcpy
( *str
, p
, len
*sizeof
(WCHAR
) );
832 return ERROR_SUCCESS
;
835 INT SQL_getint
( void *info
)
837 SQL_input
* sql
= (SQL_input
*) info
;
838 LPCWSTR p
= &sql
->command
[sql
->n
];
841 for
( i
=0; i
<sql
->len
; i
++ )
843 if
( '0' > p
[i
] ||
'9' < p
[i
] )
845 ERR
("should only be numbers here!\n");
848 r
= (p
[i
]-'0') + r
*10;
854 static int sql_error
( SQL_input
*info
, const char *str
)
859 static struct expr
* EXPR_wildcard
( void *info
)
861 struct expr
*e
= parser_alloc
( info
, sizeof
*e
);
864 e
->type
= EXPR_WILDCARD
;
869 static struct expr
* EXPR_complex
( void *info
, struct expr
*l
, UINT op
, struct expr
*r
)
871 struct expr
*e
= parser_alloc
( info
, sizeof
*e
);
874 e
->type
= EXPR_COMPLEX
;
882 static struct expr
* EXPR_unary
( void *info
, struct expr
*l
, UINT op
)
884 struct expr
*e
= parser_alloc
( info
, sizeof
*e
);
887 e
->type
= EXPR_UNARY
;
890 e
->u.expr.right
= NULL
;
895 static struct expr
* EXPR_column
( void *info
, const column_info
*column
)
897 struct expr
*e
= parser_alloc
( info
, sizeof
*e
);
900 e
->type
= EXPR_COLUMN
;
901 e
->u.column.unparsed.column
= column
->column
;
902 e
->u.column.unparsed.table
= column
->table
;
907 static struct expr
* EXPR_ival
( void *info
, int val
)
909 struct expr
*e
= parser_alloc
( info
, sizeof
*e
);
918 static struct expr
* EXPR_sval
( void *info
, const struct sql_str
*str
)
920 struct expr
*e
= parser_alloc
( info
, sizeof
*e
);
924 if
( !str
) e
->u.sval
= NULL
;
925 else if
( SQL_getstring
( info
, str
, (LPWSTR
*)&e
->u.sval
) != ERROR_SUCCESS
)
926 return NULL
; /* e will be freed by query destructor */
931 static void swap_columns
( column_info
**cols
, column_info
*A
, int idx
)
933 column_info
*preA
= NULL
, *preB
= NULL
, *B
, *ptr
;
951 if
( preB
) preB
->next
= A
;
952 if
( preA
) preA
->next
= B
;
960 static BOOL SQL_MarkPrimaryKeys
( column_info
**cols
,
967 for
( k
= keys
, count
= 0; k
&& found
; k
= k
->next
, count
++ )
973 for
( c
= *cols
, idx
= 0; c
&& !found
; c
= c
->next
, idx
++ )
975 if
( wcscmp
( k
->column
, c
->column
) )
977 c
->type |
= MSITYPE_KEY
;
980 swap_columns
( cols
, c
, count
);
987 UINT MSI_ParseSQL
( MSIDATABASE
*db
, LPCWSTR command
, MSIVIEW
**phview
,
996 sql.command
= command
;
999 sql.r
= ERROR_BAD_QUERY_SYNTAX
;
1003 r
= sql_parse
(&sql
);
1005 TRACE
("Parse returned %d\n", r
);
1010 (*sql.view
)->ops
->delete
(*sql.view
);
1016 return ERROR_SUCCESS
;