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 WINE_DEFAULT_DEBUG_CHANNEL
(msi
);
39 static UINT SQL_getstring
( void *info
, const struct sql_str
*strdata
, LPWSTR
*str
);
40 static INT SQL_getint
( void *info
);
41 static int sql_lex
( void *SQL_lval
, SQL_input
*info
);
42 static int sql_error
( SQL_input
*info
, const char *str
);
44 static LPWSTR parser_add_table
( void *info
, LPCWSTR list
, LPCWSTR table
);
45 static void *parser_alloc
( void *info
, unsigned int sz
);
46 static column_info
*parser_alloc_column
( void *info
, LPCWSTR table
, LPCWSTR column
);
48 static BOOL SQL_MarkPrimaryKeys
( column_info
**cols
, column_info
*keys
);
50 static struct expr
* EXPR_complex
( void *info
, struct expr
*l
, UINT op
, struct expr
*r
);
51 static struct expr
* EXPR_unary
( void *info
, struct expr
*l
, UINT op
);
52 static struct expr
* EXPR_column
( void *info
, const column_info
*column
);
53 static struct expr
* EXPR_ival
( void *info
, int val
);
54 static struct expr
* EXPR_sval
( void *info
, const struct sql_str
*str
);
55 static struct expr
* EXPR_wildcard
( void *info
);
57 #define PARSER_BUBBLE_UP_VIEW( sql, result, current_view ) \
58 *sql
->view
= current_view
; \
63 %lex
-param
{ SQL_input
*info
}
64 %parse
-param
{ SQL_input
*info
}
71 column_info
*column_list
;
78 %token TK_ALTER TK_AND TK_BY TK_CHAR TK_COMMA TK_CREATE TK_DELETE TK_DROP
79 %token TK_DISTINCT TK_DOT TK_EQ TK_FREE TK_FROM TK_GE TK_GT TK_HOLD TK_ADD
81 %token TK_ILLEGAL TK_INSERT TK_INT
82 %token
<str
> TK_INTEGER
83 %token TK_INTO TK_IS TK_KEY TK_LE TK_LONG TK_LONGCHAR TK_LP TK_LT
84 %token TK_LOCALIZABLE TK_MINUS TK_NE TK_NOT TK_NULL
85 %token TK_OBJECT TK_OR TK_ORDER TK_PRIMARY TK_RP
86 %token TK_SELECT TK_SET TK_SHORT TK_SPACE TK_STAR
87 %token
<str
> TK_STRING
88 %token TK_TABLE TK_TEMPORARY TK_UPDATE TK_VALUES TK_WHERE TK_WILDCARD
91 * These are extra tokens used by the lexer but never seen by the
92 * parser. We put them in a rule so that the parser generator will
93 * add them to the parse.h output file.
96 %nonassoc END_OF_FILE ILLEGAL SPACE UNCLOSED_STRING COMMENT FUNCTION
99 %type
<string> table tablelist id
string
100 %type
<column_list
> selcollist collist selcolumn column column_and_type column_def table_def
101 %type
<column_list
> column_assignment update_assign_list constlist
102 %type
<query
> query from selectfrom unorderdfrom
103 %type
<query
> oneupdate onedelete oneselect onequery onecreate oneinsert onealter onedrop
104 %type
<expr
> expr val column_val const_val
105 %type
<column_type
> column_type data_type data_type_l data_count
106 %type
<integer
> number alterop
111 %left TK_EQ TK_NE TK_LT TK_GT TK_LE TK_GE TK_LIKE
119 SQL_input
* sql
= (SQL_input
*) info
;
135 TK_INSERT TK_INTO table TK_LP collist TK_RP TK_VALUES TK_LP constlist TK_RP
137 SQL_input
*sql
= (SQL_input
*) info
;
138 MSIVIEW
*insert
= NULL
;
140 INSERT_CreateView
( sql
->db
, &insert
, $3, $5, $9, FALSE
);
144 PARSER_BUBBLE_UP_VIEW
( sql
, $$
, insert
);
146 | TK_INSERT TK_INTO table TK_LP collist TK_RP TK_VALUES TK_LP constlist TK_RP TK_TEMPORARY
148 SQL_input
*sql
= (SQL_input
*) info
;
149 MSIVIEW
*insert
= NULL
;
151 INSERT_CreateView
( sql
->db
, &insert
, $3, $5, $9, TRUE
);
155 PARSER_BUBBLE_UP_VIEW
( sql
, $$
, insert
);
160 TK_CREATE TK_TABLE table TK_LP table_def TK_RP
162 SQL_input
* sql
= (SQL_input
*) info
;
163 MSIVIEW
*create
= NULL
;
168 r
= CREATE_CreateView
( sql
->db
, &create
, $3, $5, FALSE
);
175 PARSER_BUBBLE_UP_VIEW
( sql
, $$
, create
);
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
);
188 PARSER_BUBBLE_UP_VIEW
( sql
, $$
, create
);
193 TK_UPDATE table TK_SET update_assign_list TK_WHERE expr
195 SQL_input
* sql
= (SQL_input
*) info
;
196 MSIVIEW
*update
= NULL
;
198 UPDATE_CreateView
( sql
->db
, &update
, $2, $4, $6 );
202 PARSER_BUBBLE_UP_VIEW
( sql
, $$
, update
);
204 | TK_UPDATE table TK_SET update_assign_list
206 SQL_input
* sql
= (SQL_input
*) info
;
207 MSIVIEW
*update
= NULL
;
209 UPDATE_CreateView
( sql
->db
, &update
, $2, $4, NULL
);
213 PARSER_BUBBLE_UP_VIEW
( sql
, $$
, update
);
220 SQL_input
* sql
= (SQL_input
*) info
;
221 MSIVIEW
*delete
= NULL
;
223 DELETE_CreateView
( sql
->db
, &delete
, $2 );
227 PARSER_BUBBLE_UP_VIEW
( sql
, $$
, delete
);
232 TK_ALTER TK_TABLE table alterop
234 SQL_input
* sql
= (SQL_input
*) info
;
235 MSIVIEW
*alter
= NULL
;
237 ALTER_CreateView
( sql
->db
, &alter
, $3, NULL
, $4 );
241 PARSER_BUBBLE_UP_VIEW
( sql
, $$
, alter
);
243 | TK_ALTER TK_TABLE table TK_ADD column_and_type
245 SQL_input
*sql
= (SQL_input
*)info
;
246 MSIVIEW
*alter
= NULL
;
248 ALTER_CreateView
( sql
->db
, &alter
, $3, $5, 0 );
252 PARSER_BUBBLE_UP_VIEW
( sql
, $$
, alter
);
254 | TK_ALTER TK_TABLE table TK_ADD column_and_type TK_HOLD
256 SQL_input
*sql
= (SQL_input
*)info
;
257 MSIVIEW
*alter
= NULL
;
259 ALTER_CreateView
( sql
->db
, &alter
, $3, $5, 1 );
263 PARSER_BUBBLE_UP_VIEW
( sql
, $$
, alter
);
279 TK_DROP TK_TABLE table
281 SQL_input
* sql
= (SQL_input
*) info
;
282 MSIVIEW
* drop
= NULL
;
285 r
= DROP_CreateView
( sql
->db
, &drop
, $3 );
286 if
( r
!= ERROR_SUCCESS ||
!$$
)
289 PARSER_BUBBLE_UP_VIEW
( sql
, $$
, drop
);
294 column_def TK_PRIMARY TK_KEY collist
296 if
( SQL_MarkPrimaryKeys
( &$1, $4 ) )
304 column_def TK_COMMA column_and_type
308 for
( ci
= $1; ci
->next
; ci
= ci
->next
)
324 $$
->type
= ($2 | MSITYPE_VALID
);
325 $$
->temporary
= $2 & MSITYPE_TEMPORARY ? TRUE
: FALSE
;
334 | data_type_l TK_LOCALIZABLE
336 $$
= $1 | MSITYPE_LOCALIZABLE
;
338 | data_type_l TK_TEMPORARY
340 $$
= $1 | MSITYPE_TEMPORARY
;
347 $$ |
= MSITYPE_NULLABLE
;
349 | data_type TK_NOT TK_NULL
358 $$
= MSITYPE_STRING |
0x400;
360 | TK_CHAR TK_LP data_count TK_RP
362 $$
= MSITYPE_STRING |
0x400 |
$3;
366 $$
= MSITYPE_STRING |
0x400;
382 $$
= MSITYPE_STRING | MSITYPE_VALID
;
389 if
( ( $1 > 255 ) ||
( $1 < 0 ) )
400 | TK_SELECT TK_DISTINCT selectfrom
402 SQL_input
* sql
= (SQL_input
*) info
;
403 MSIVIEW
* distinct
= NULL
;
406 r
= DISTINCT_CreateView
( sql
->db
, &distinct
, $3 );
407 if
(r
!= ERROR_SUCCESS
)
410 PARSER_BUBBLE_UP_VIEW
( sql
, $$
, distinct
);
417 SQL_input
* sql
= (SQL_input
*) info
;
418 MSIVIEW
* select
= NULL
;
423 r
= SELECT_CreateView
( sql
->db
, &select
, $2, $1 );
424 if
(r
!= ERROR_SUCCESS
)
427 PARSER_BUBBLE_UP_VIEW
( sql
, $$
, select
);
436 | selcolumn TK_COMMA selcollist
448 | column TK_COMMA collist
461 SQL_input
* sql
= (SQL_input
*) info
;
462 MSIVIEW
* table
= NULL
;
465 r
= TABLE_CreateView
( sql
->db
, $2, &table
);
466 if
( r
!= ERROR_SUCCESS ||
!$$
)
469 PARSER_BUBBLE_UP_VIEW
( sql
, $$
, table
);
471 | unorderdfrom TK_ORDER TK_BY collist
477 r
= $1->ops
->sort
( $1, $4 );
478 if
( r
!= ERROR_SUCCESS
)
490 SQL_input
* sql
= (SQL_input
*) info
;
491 MSIVIEW
* where
= NULL
;
494 r
= WHERE_CreateView
( sql
->db
, &where
, $2, NULL
);
495 if
( r
!= ERROR_SUCCESS
)
498 PARSER_BUBBLE_UP_VIEW
( sql
, $$
, where
);
500 | TK_FROM tablelist TK_WHERE expr
502 SQL_input
* sql
= (SQL_input
*) info
;
503 MSIVIEW
* where
= NULL
;
506 r
= WHERE_CreateView
( sql
->db
, &where
, $2, $4 );
507 if
( r
!= ERROR_SUCCESS
)
510 PARSER_BUBBLE_UP_VIEW
( sql
, $$
, where
);
519 | table TK_COMMA tablelist
521 $$
= parser_add_table
( info
, $3, $1 );
536 $$
= EXPR_complex
( info
, $1, OP_AND
, $3 );
542 $$
= EXPR_complex
( info
, $1, OP_OR
, $3 );
546 | column_val TK_EQ val
548 $$
= EXPR_complex
( info
, $1, OP_EQ
, $3 );
552 | column_val TK_GT val
554 $$
= EXPR_complex
( info
, $1, OP_GT
, $3 );
558 | column_val TK_LT val
560 $$
= EXPR_complex
( info
, $1, OP_LT
, $3 );
564 | column_val TK_LE val
566 $$
= EXPR_complex
( info
, $1, OP_LE
, $3 );
570 | column_val TK_GE val
572 $$
= EXPR_complex
( info
, $1, OP_GE
, $3 );
576 | column_val TK_NE val
578 $$
= EXPR_complex
( info
, $1, OP_NE
, $3 );
582 | column_val TK_IS TK_NULL
584 $$
= EXPR_unary
( info
, $1, OP_ISNULL
);
588 | column_val TK_IS TK_NOT TK_NULL
590 $$
= EXPR_unary
( info
, $1, OP_NOTNULL
);
604 $$
= parser_alloc_column
( info
, NULL
, NULL
);
609 | const_val TK_COMMA constlist
611 $$
= parser_alloc_column
( info
, NULL
, NULL
);
621 | column_assignment TK_COMMA update_assign_list
629 column TK_EQ const_val
639 $$
= EXPR_ival
( info
, $1 );
643 | TK_MINUS number %prec TK_NEGATION
645 $$
= EXPR_ival
( info
, -$2 );
651 $$
= EXPR_sval
( info
, &$1 );
657 $$
= EXPR_wildcard
( info
);
666 $$
= EXPR_column
( info
, $1 );
675 $$
= parser_alloc_column
( info
, $1, $3 );
681 $$
= parser_alloc_column
( info
, NULL
, $1 );
690 $$
= parser_alloc_column
( info
, $1, $3 );
696 $$
= parser_alloc_column
( info
, NULL
, $1 );
702 $$
= parser_alloc_column
( info
, NULL
, $1 );
718 if
( SQL_getstring
( info
, &$1, &$$
) != ERROR_SUCCESS ||
!$$
)
726 if
( SQL_getstring
( info
, &$1, &$$
) != ERROR_SUCCESS ||
!$$
)
734 $$
= SQL_getint
( info
);
740 static LPWSTR parser_add_table
( void *info
, LPCWSTR list
, LPCWSTR table
)
742 static const WCHAR space
[] = {' ',0};
743 DWORD len
= strlenW
( list
) + strlenW
( table
) + 2;
746 ret
= parser_alloc
( info
, len
* sizeof
(WCHAR
) );
749 strcpyW
( ret
, list
);
750 strcatW
( ret
, space
);
751 strcatW
( 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
( SQL_getstring
( info
, str
, (LPWSTR
*)&e
->u.sval
) != ERROR_SUCCESS
)
925 return NULL
; /* e will be freed by query destructor */
930 static void swap_columns
( column_info
**cols
, column_info
*A
, int idx
)
932 column_info
*preA
= NULL
, *preB
= NULL
, *B
, *ptr
;
950 if
( preB
) preB
->next
= A
;
951 if
( preA
) preA
->next
= B
;
959 static BOOL SQL_MarkPrimaryKeys
( column_info
**cols
,
966 for
( k
= keys
, count
= 0; k
&& found
; k
= k
->next
, count
++ )
972 for
( c
= *cols
, idx
= 0; c
&& !found
; c
= c
->next
, idx
++ )
974 if
( strcmpW
( k
->column
, c
->column
) )
976 c
->type |
= MSITYPE_KEY
;
979 swap_columns
( cols
, c
, count
);
986 UINT MSI_ParseSQL
( MSIDATABASE
*db
, LPCWSTR command
, MSIVIEW
**phview
,
995 sql.command
= command
;
998 sql.r
= ERROR_BAD_QUERY_SYNTAX
;
1002 r
= sql_parse
(&sql
);
1004 TRACE
("Parse returned %d\n", r
);
1009 (*sql.view
)->ops
->delete
(*sql.view
);
1015 return ERROR_SUCCESS
;