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 TK_DROP
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 onedrop
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
;
142 TK_INSERT TK_INTO table TK_LP selcollist TK_RP TK_VALUES TK_LP constlist TK_RP
144 SQL_input
*sql
= (SQL_input
*) info
;
145 MSIVIEW
*insert
= NULL
;
147 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 TK_DROP TK_TABLE table
274 SQL_input
* sql
= (SQL_input
*) info
;
278 r
= DROP_CreateView
( sql
->db
, &$$
, $3 );
279 if
( r
!= ERROR_SUCCESS ||
!$$
)
285 column_def TK_PRIMARY TK_KEY selcollist
287 if
( SQL_MarkPrimaryKeys
( $1, $4 ) )
295 column_def TK_COMMA column_and_type
299 for
( ci
= $1; ci
->next
; ci
= ci
->next
)
315 $$
->type
= ($2 | MSITYPE_VALID
);
316 $$
->temporary
= $2 & MSITYPE_TEMPORARY ? TRUE
: FALSE
;
325 | data_type_l TK_LOCALIZABLE
327 $$
= $1 | MSITYPE_LOCALIZABLE
;
329 | data_type_l TK_TEMPORARY
331 $$
= $1 | MSITYPE_TEMPORARY
;
338 $$ |
= MSITYPE_NULLABLE
;
340 | data_type TK_NOT TK_NULL
349 $$
= MSITYPE_STRING |
1;
351 | TK_CHAR TK_LP data_count TK_RP
353 $$
= MSITYPE_STRING |
0x400 |
$3;
373 $$
= MSITYPE_STRING | MSITYPE_VALID
;
380 if
( ( $1 > 255 ) ||
( $1 < 0 ) )
387 unorderedsel TK_ORDER TK_BY selcollist
393 r
= $1->ops
->sort
( $1, $4 );
394 if
( r
!= ERROR_SUCCESS
)
408 | TK_SELECT TK_DISTINCT selectfrom
410 SQL_input
* sql
= (SQL_input
*) info
;
414 r
= DISTINCT_CreateView
( sql
->db
, &$$
, $3 );
415 if
(r
!= ERROR_SUCCESS
)
426 SQL_input
* sql
= (SQL_input
*) info
;
432 r
= SELECT_CreateView
( sql
->db
, &$$
, $2, $1 );
433 if
(r
!= ERROR_SUCCESS
)
446 | column TK_COMMA selcollist
458 | fromtable TK_WHERE expr
460 SQL_input
* sql
= (SQL_input
*) info
;
464 r
= WHERE_CreateView
( sql
->db
, &$$
, $1, $3 );
465 if
( r
!= ERROR_SUCCESS
)
467 $1->ops
->delete
( $1 );
476 SQL_input
* sql
= (SQL_input
*) info
;
480 r
= TABLE_CreateView
( sql
->db
, $2, &$$
);
481 if
( r
!= ERROR_SUCCESS ||
!$$
)
486 SQL_input
* sql
= (SQL_input
*) info
;
489 r
= JOIN_CreateView
( sql
->db
, &$$
, $2 );
491 if
( r
!= ERROR_SUCCESS
)
502 table TK_COMMA tablelist
504 $$
= parser_add_table
($3, $1);
519 $$
= EXPR_complex
( info
, $1, OP_AND
, $3 );
525 $$
= EXPR_complex
( info
, $1, OP_OR
, $3 );
529 | column_val TK_EQ val
531 $$
= EXPR_complex
( info
, $1, OP_EQ
, $3 );
535 | column_val TK_GT val
537 $$
= EXPR_complex
( info
, $1, OP_GT
, $3 );
541 | column_val TK_LT val
543 $$
= EXPR_complex
( info
, $1, OP_LT
, $3 );
547 | column_val TK_LE val
549 $$
= EXPR_complex
( info
, $1, OP_LE
, $3 );
553 | column_val TK_GE val
555 $$
= EXPR_complex
( info
, $1, OP_GE
, $3 );
559 | column_val TK_NE val
561 $$
= EXPR_complex
( info
, $1, OP_NE
, $3 );
565 | column_val TK_IS TK_NULL
567 $$
= EXPR_unary
( info
, $1, OP_ISNULL
);
571 | column_val TK_IS TK_NOT TK_NULL
573 $$
= EXPR_unary
( info
, $1, OP_NOTNULL
);
587 $$
= parser_alloc_column
( info
, NULL
, NULL
);
592 | const_val TK_COMMA constlist
594 $$
= parser_alloc_column
( info
, NULL
, NULL
);
604 | column_assignment TK_COMMA update_assign_list
612 column TK_EQ const_val
622 $$
= EXPR_ival
( info
, $1 );
626 | TK_MINUS number %prec TK_NEGATION
628 $$
= EXPR_ival
( info
, -$2 );
634 $$
= EXPR_sval
( info
, &$1 );
640 $$
= EXPR_wildcard
( info
);
649 $$
= EXPR_column
( info
, $1 );
658 $$
= parser_alloc_column
( info
, $1, $3 );
664 $$
= parser_alloc_column
( info
, NULL
, $1 );
680 $$
= SQL_getstring
( info
, &$1 );
689 $$
= SQL_getint
( info
);
695 static LPWSTR parser_add_table
(LPWSTR list
, LPWSTR table
)
697 DWORD size
= lstrlenW
(list
) + lstrlenW
(table
) + 2;
698 static const WCHAR space
[] = {' ',0};
700 list
= msi_realloc
(list
, size
* sizeof
(WCHAR
));
701 if
(!list
) return NULL
;
703 lstrcatW
(list
, space
);
704 lstrcatW
(list
, table
);
708 static void *parser_alloc
( void *info
, unsigned int sz
)
710 SQL_input
* sql
= (SQL_input
*) info
;
713 mem
= msi_alloc
( sizeof
(struct list
) + sz
);
714 list_add_tail
( sql
->mem
, mem
);
718 static column_info
*parser_alloc_column
( void *info
, LPCWSTR table
, LPCWSTR column
)
722 col
= parser_alloc
( info
, sizeof
(*col
) );
726 col
->column
= column
;
735 static int sql_lex
( void *SQL_lval
, SQL_input
*sql
)
738 struct sql_str
* str
= SQL_lval
;
743 if
( ! sql
->command
[sql
->n
] )
744 return
0; /* end of input */
746 /* TRACE("string : %s\n", debugstr_w(&sql->command[sql->n])); */
747 sql
->len
= sqliteGetToken
( &sql
->command
[sql
->n
], &token
);
750 str
->data
= &sql
->command
[sql
->n
];
753 while
( token
== TK_SPACE
);
755 /* TRACE("token : %d (%s)\n", token, debugstr_wn(&sql->command[sql->n], sql->len)); */
760 LPWSTR SQL_getstring
( void *info
, const struct sql_str
*strdata
)
762 LPCWSTR p
= strdata
->data
;
763 UINT len
= strdata
->len
;
766 /* if there's quotes, remove them */
767 if
( ( (p
[0]=='`') && (p
[len
-1]=='`') ) ||
768 ( (p
[0]=='\'') && (p
[len
-1]=='\'') ) )
773 str
= parser_alloc
( info
, (len
+ 1)*sizeof
(WCHAR
) );
776 memcpy
( str
, p
, len
*sizeof
(WCHAR
) );
782 INT SQL_getint
( void *info
)
784 SQL_input
* sql
= (SQL_input
*) info
;
785 LPCWSTR p
= &sql
->command
[sql
->n
];
788 for
( i
=0; i
<sql
->len
; i
++ )
790 if
( '0' > p
[i
] ||
'9' < p
[i
] )
792 ERR
("should only be numbers here!\n");
795 r
= (p
[i
]-'0') + r
*10;
801 static int sql_error
( const char *str
)
806 static struct expr
* EXPR_wildcard
( void *info
)
808 struct expr
*e
= parser_alloc
( info
, sizeof
*e
);
811 e
->type
= EXPR_WILDCARD
;
816 static struct expr
* EXPR_complex
( void *info
, struct expr
*l
, UINT op
, struct expr
*r
)
818 struct expr
*e
= parser_alloc
( info
, sizeof
*e
);
821 e
->type
= EXPR_COMPLEX
;
829 static struct expr
* EXPR_unary
( void *info
, struct expr
*l
, UINT op
)
831 struct expr
*e
= parser_alloc
( info
, sizeof
*e
);
834 e
->type
= EXPR_UNARY
;
837 e
->u.expr.right
= NULL
;
842 static struct expr
* EXPR_column
( void *info
, const column_info
*column
)
844 struct expr
*e
= parser_alloc
( info
, sizeof
*e
);
847 e
->type
= EXPR_COLUMN
;
848 e
->u.sval
= column
->column
;
853 static struct expr
* EXPR_ival
( void *info
, int val
)
855 struct expr
*e
= parser_alloc
( info
, sizeof
*e
);
864 static struct expr
* EXPR_sval
( void *info
, const struct sql_str
*str
)
866 struct expr
*e
= parser_alloc
( info
, sizeof
*e
);
870 e
->u.sval
= SQL_getstring
( info
, str
);
875 static BOOL SQL_MarkPrimaryKeys
( column_info
*cols
,
881 for
( k
= keys
; k
&& found
; k
= k
->next
)
886 for
( c
= cols
; c
&& !found
; c
= c
->next
)
888 if
( lstrcmpW
( k
->column
, c
->column
) )
890 c
->type |
= MSITYPE_KEY
;
898 UINT MSI_ParseSQL
( MSIDATABASE
*db
, LPCWSTR command
, MSIVIEW
**phview
,
907 sql.command
= command
;
915 TRACE
("Parse returned %d\n", r
);
919 return ERROR_BAD_QUERY_SYNTAX
;
922 return ERROR_SUCCESS
;