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 */
54 static UINT SQL_getstring
( void *info
, const struct sql_str
*strdata
, LPWSTR
*str
);
55 static INT SQL_getint
( void *info
);
56 static int sql_lex
( void *SQL_lval
, SQL_input
*info
);
58 static LPWSTR parser_add_table
( void *info
, LPCWSTR list
, LPCWSTR table
);
59 static void *parser_alloc
( void *info
, unsigned int sz
);
60 static column_info
*parser_alloc_column
( void *info
, LPCWSTR table
, LPCWSTR column
);
62 static BOOL SQL_MarkPrimaryKeys
( column_info
**cols
, column_info
*keys
);
64 static struct expr
* EXPR_complex
( void *info
, struct expr
*l
, UINT op
, struct expr
*r
);
65 static struct expr
* EXPR_unary
( void *info
, struct expr
*l
, UINT op
);
66 static struct expr
* EXPR_column
( void *info
, const column_info
*column
);
67 static struct expr
* EXPR_ival
( void *info
, int val
);
68 static struct expr
* EXPR_sval
( void *info
, const struct sql_str
*str
);
69 static struct expr
* EXPR_wildcard
( void *info
);
79 column_info
*column_list
;
86 %token TK_ALTER TK_AND TK_BY TK_CHAR TK_COMMA TK_CREATE TK_DELETE TK_DROP
87 %token TK_DISTINCT TK_DOT TK_EQ TK_FREE TK_FROM TK_GE TK_GT TK_HOLD TK_ADD
89 %token TK_ILLEGAL TK_INSERT TK_INT
90 %token
<str
> TK_INTEGER
91 %token TK_INTO TK_IS TK_KEY TK_LE TK_LONG TK_LONGCHAR TK_LP TK_LT
92 %token TK_LOCALIZABLE TK_MINUS TK_NE TK_NOT TK_NULL
93 %token TK_OBJECT TK_OR TK_ORDER TK_PRIMARY TK_RP
94 %token TK_SELECT TK_SET TK_SHORT TK_SPACE TK_STAR
95 %token
<str
> TK_STRING
96 %token TK_TABLE TK_TEMPORARY TK_UPDATE TK_VALUES TK_WHERE TK_WILDCARD
99 * These are extra tokens used by the lexer but never seen by the
100 * parser. We put them in a rule so that the parser generator will
101 * add them to the parse.h output file.
104 %nonassoc END_OF_FILE ILLEGAL SPACE UNCLOSED_STRING COMMENT FUNCTION
107 %type
<string> table tablelist id
108 %type
<column_list
> selcollist column column_and_type column_def table_def
109 %type
<column_list
> column_assignment update_assign_list constlist
110 %type
<query
> query from fromtable selectfrom unorderedsel
111 %type
<query
> oneupdate onedelete oneselect onequery onecreate oneinsert onealter onedrop
112 %type
<expr
> expr val column_val const_val
113 %type
<column_type
> column_type data_type data_type_l data_count
114 %type
<integer
> number alterop
116 /* Reference: http://mates.ms.mff.cuni.cz/oracle/doc/ora815nt/server.815/a67779/operator.htm */
120 %left TK_EQ TK_NE TK_LT TK_GT TK_LE TK_GE TK_LIKE
128 SQL_input
* sql
= (SQL_input
*) info
;
144 TK_INSERT TK_INTO table TK_LP selcollist TK_RP TK_VALUES TK_LP constlist TK_RP
146 SQL_input
*sql
= (SQL_input
*) info
;
147 MSIVIEW
*insert
= NULL
;
149 INSERT_CreateView
( sql
->db
, &insert
, $3, $5, $9, FALSE
);
154 | TK_INSERT TK_INTO table TK_LP selcollist TK_RP TK_VALUES TK_LP constlist TK_RP TK_TEMPORARY
156 SQL_input
*sql
= (SQL_input
*) info
;
157 MSIVIEW
*insert
= NULL
;
159 INSERT_CreateView
( sql
->db
, &insert
, $3, $5, $9, TRUE
);
167 TK_CREATE TK_TABLE table TK_LP table_def TK_RP
169 SQL_input
* sql
= (SQL_input
*) info
;
170 MSIVIEW
*create
= NULL
;
175 r
= CREATE_CreateView
( sql
->db
, &create
, $3, $5, FALSE
);
183 | TK_CREATE TK_TABLE table TK_LP table_def TK_RP TK_HOLD
185 SQL_input
* sql
= (SQL_input
*) info
;
186 MSIVIEW
*create
= NULL
;
190 CREATE_CreateView
( sql
->db
, &create
, $3, $5, TRUE
);
198 TK_UPDATE table TK_SET update_assign_list TK_WHERE expr
200 SQL_input
* sql
= (SQL_input
*) info
;
201 MSIVIEW
*update
= NULL
;
203 UPDATE_CreateView
( sql
->db
, &update
, $2, $4, $6 );
208 | TK_UPDATE table TK_SET update_assign_list
210 SQL_input
* sql
= (SQL_input
*) info
;
211 MSIVIEW
*update
= NULL
;
213 UPDATE_CreateView
( sql
->db
, &update
, $2, $4, NULL
);
223 SQL_input
* sql
= (SQL_input
*) info
;
224 MSIVIEW
*delete
= NULL
;
226 DELETE_CreateView
( sql
->db
, &delete
, $2 );
234 TK_ALTER TK_TABLE table alterop
236 SQL_input
* sql
= (SQL_input
*) info
;
237 MSIVIEW
*alter
= NULL
;
239 ALTER_CreateView
( sql
->db
, &alter
, $3, NULL
, $4 );
244 | TK_ALTER TK_TABLE table TK_ADD column_and_type
246 SQL_input
*sql
= (SQL_input
*)info
;
247 MSIVIEW
*alter
= NULL
;
249 ALTER_CreateView
( sql
->db
, &alter
, $3, $5, 0 );
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 );
278 TK_DROP TK_TABLE table
280 SQL_input
* sql
= (SQL_input
*) info
;
284 r
= DROP_CreateView
( sql
->db
, &$$
, $3 );
285 if
( r
!= ERROR_SUCCESS ||
!$$
)
291 column_def TK_PRIMARY TK_KEY selcollist
293 if
( SQL_MarkPrimaryKeys
( &$1, $4 ) )
301 column_def TK_COMMA column_and_type
305 for
( ci
= $1; ci
->next
; ci
= ci
->next
)
321 $$
->type
= ($2 | MSITYPE_VALID
);
322 $$
->temporary
= $2 & MSITYPE_TEMPORARY ? TRUE
: FALSE
;
331 | data_type_l TK_LOCALIZABLE
333 $$
= $1 | MSITYPE_LOCALIZABLE
;
335 | data_type_l TK_TEMPORARY
337 $$
= $1 | MSITYPE_TEMPORARY
;
344 $$ |
= MSITYPE_NULLABLE
;
346 | data_type TK_NOT TK_NULL
355 $$
= MSITYPE_STRING |
1;
357 | TK_CHAR TK_LP data_count TK_RP
359 $$
= MSITYPE_STRING |
0x400 |
$3;
363 $$
= MSITYPE_STRING |
0x400;
379 $$
= MSITYPE_STRING | MSITYPE_VALID
;
386 if
( ( $1 > 255 ) ||
( $1 < 0 ) )
393 unorderedsel TK_ORDER TK_BY selcollist
399 r
= $1->ops
->sort
( $1, $4 );
400 if
( r
!= ERROR_SUCCESS
)
414 | TK_SELECT TK_DISTINCT selectfrom
416 SQL_input
* sql
= (SQL_input
*) info
;
420 r
= DISTINCT_CreateView
( sql
->db
, &$$
, $3 );
421 if
(r
!= ERROR_SUCCESS
)
432 SQL_input
* sql
= (SQL_input
*) info
;
438 r
= SELECT_CreateView
( sql
->db
, &$$
, $2, $1 );
439 if
(r
!= ERROR_SUCCESS
)
452 | column TK_COMMA selcollist
464 | fromtable TK_WHERE expr
466 SQL_input
* sql
= (SQL_input
*) info
;
470 r
= WHERE_CreateView
( sql
->db
, &$$
, $1, $3 );
471 if
( r
!= ERROR_SUCCESS
)
473 $1->ops
->delete
( $1 );
482 SQL_input
* sql
= (SQL_input
*) info
;
486 r
= TABLE_CreateView
( sql
->db
, $2, &$$
);
487 if
( r
!= ERROR_SUCCESS ||
!$$
)
492 SQL_input
* sql
= (SQL_input
*) info
;
495 r
= JOIN_CreateView
( sql
->db
, &$$
, $2 );
496 if
( r
!= ERROR_SUCCESS
)
507 table TK_COMMA tablelist
509 $$
= parser_add_table
( info
, $3, $1 );
524 $$
= EXPR_complex
( info
, $1, OP_AND
, $3 );
530 $$
= EXPR_complex
( info
, $1, OP_OR
, $3 );
534 | column_val TK_EQ val
536 $$
= EXPR_complex
( info
, $1, OP_EQ
, $3 );
540 | column_val TK_GT val
542 $$
= EXPR_complex
( info
, $1, OP_GT
, $3 );
546 | column_val TK_LT val
548 $$
= EXPR_complex
( info
, $1, OP_LT
, $3 );
552 | column_val TK_LE val
554 $$
= EXPR_complex
( info
, $1, OP_LE
, $3 );
558 | column_val TK_GE val
560 $$
= EXPR_complex
( info
, $1, OP_GE
, $3 );
564 | column_val TK_NE val
566 $$
= EXPR_complex
( info
, $1, OP_NE
, $3 );
570 | column_val TK_IS TK_NULL
572 $$
= EXPR_unary
( info
, $1, OP_ISNULL
);
576 | column_val TK_IS TK_NOT TK_NULL
578 $$
= EXPR_unary
( info
, $1, OP_NOTNULL
);
592 $$
= parser_alloc_column
( info
, NULL
, NULL
);
597 | const_val TK_COMMA constlist
599 $$
= parser_alloc_column
( info
, NULL
, NULL
);
609 | column_assignment TK_COMMA update_assign_list
617 column TK_EQ const_val
627 $$
= EXPR_ival
( info
, $1 );
631 | TK_MINUS number %prec TK_NEGATION
633 $$
= EXPR_ival
( info
, -$2 );
639 $$
= EXPR_sval
( info
, &$1 );
645 $$
= EXPR_wildcard
( info
);
654 $$
= EXPR_column
( info
, $1 );
663 $$
= parser_alloc_column
( info
, $1, $3 );
669 $$
= parser_alloc_column
( info
, NULL
, $1 );
685 if
( SQL_getstring
( info
, &$1, &$$
) != ERROR_SUCCESS ||
!$$
)
693 $$
= SQL_getint
( info
);
699 static LPWSTR parser_add_table
( void *info
, LPCWSTR list
, LPCWSTR table
)
701 static const WCHAR space
[] = {' ',0};
702 DWORD len
= strlenW
( list
) + strlenW
( table
) + 2;
705 ret
= parser_alloc
( info
, len
* sizeof
(WCHAR
) );
708 strcpyW
( ret
, list
);
709 strcatW
( ret
, space
);
710 strcatW
( ret
, table
);
715 static void *parser_alloc
( void *info
, unsigned int sz
)
717 SQL_input
* sql
= (SQL_input
*) info
;
720 mem
= msi_alloc
( sizeof
(struct list
) + sz
);
721 list_add_tail
( sql
->mem
, mem
);
725 static column_info
*parser_alloc_column
( void *info
, LPCWSTR table
, LPCWSTR column
)
729 col
= parser_alloc
( info
, sizeof
(*col
) );
733 col
->column
= column
;
742 static int sql_lex
( void *SQL_lval
, SQL_input
*sql
)
745 struct sql_str
* str
= SQL_lval
;
750 if
( ! sql
->command
[sql
->n
] )
751 return
0; /* end of input */
753 /* TRACE("string : %s\n", debugstr_w(&sql->command[sql->n])); */
754 sql
->len
= sqliteGetToken
( &sql
->command
[sql
->n
], &token
);
757 str
->data
= &sql
->command
[sql
->n
];
760 while
( token
== TK_SPACE
);
762 /* TRACE("token : %d (%s)\n", token, debugstr_wn(&sql->command[sql->n], sql->len)); */
767 UINT SQL_getstring
( void *info
, const struct sql_str
*strdata
, LPWSTR
*str
)
769 LPCWSTR p
= strdata
->data
;
770 UINT len
= strdata
->len
;
773 if
( ( (p
[0]=='`') && (p
[len
-1]!='`') ) ||
774 ( (p
[0]=='\'') && (p
[len
-1]!='\'') ) )
775 return ERROR_FUNCTION_FAILED
;
777 /* if there's quotes, remove them */
778 if
( ( (p
[0]=='`') && (p
[len
-1]=='`') ) ||
779 ( (p
[0]=='\'') && (p
[len
-1]=='\'') ) )
784 *str
= parser_alloc
( info
, (len
+ 1)*sizeof
(WCHAR
) );
786 return ERROR_OUTOFMEMORY
;
787 memcpy
( *str
, p
, len
*sizeof
(WCHAR
) );
790 return ERROR_SUCCESS
;
793 INT SQL_getint
( void *info
)
795 SQL_input
* sql
= (SQL_input
*) info
;
796 LPCWSTR p
= &sql
->command
[sql
->n
];
799 for
( i
=0; i
<sql
->len
; i
++ )
801 if
( '0' > p
[i
] ||
'9' < p
[i
] )
803 ERR
("should only be numbers here!\n");
806 r
= (p
[i
]-'0') + r
*10;
812 static int sql_error
( const char *str
)
817 static struct expr
* EXPR_wildcard
( void *info
)
819 struct expr
*e
= parser_alloc
( info
, sizeof
*e
);
822 e
->type
= EXPR_WILDCARD
;
827 static struct expr
* EXPR_complex
( void *info
, struct expr
*l
, UINT op
, struct expr
*r
)
829 struct expr
*e
= parser_alloc
( info
, sizeof
*e
);
832 e
->type
= EXPR_COMPLEX
;
840 static struct expr
* EXPR_unary
( void *info
, struct expr
*l
, UINT op
)
842 struct expr
*e
= parser_alloc
( info
, sizeof
*e
);
845 e
->type
= EXPR_UNARY
;
848 e
->u.expr.right
= NULL
;
853 static struct expr
* EXPR_column
( void *info
, const column_info
*column
)
855 struct expr
*e
= parser_alloc
( info
, sizeof
*e
);
858 e
->type
= EXPR_COLUMN
;
859 e
->u.sval
= column
->column
;
864 static struct expr
* EXPR_ival
( void *info
, int val
)
866 struct expr
*e
= parser_alloc
( info
, sizeof
*e
);
875 static struct expr
* EXPR_sval
( void *info
, const struct sql_str
*str
)
877 struct expr
*e
= parser_alloc
( info
, sizeof
*e
);
881 if
( SQL_getstring
( info
, str
, (LPWSTR
*)&e
->u.sval
) != ERROR_SUCCESS
)
882 return NULL
; /* e will be freed by query destructor */
887 static void swap_columns
( column_info
**cols
, column_info
*A
, int idx
)
889 column_info
*preA
= NULL
, *preB
= NULL
, *B
, *ptr
;
907 if
( preB
) preB
->next
= A
;
908 if
( preA
) preA
->next
= B
;
916 static BOOL SQL_MarkPrimaryKeys
( column_info
**cols
,
923 for
( k
= keys
, count
= 0; k
&& found
; k
= k
->next
, count
++ )
929 for
( c
= *cols
, idx
= 0; c
&& !found
; c
= c
->next
, idx
++ )
931 if
( lstrcmpW
( k
->column
, c
->column
) )
933 c
->type |
= MSITYPE_KEY
;
936 swap_columns
( cols
, c
, count
);
943 UINT MSI_ParseSQL
( MSIDATABASE
*db
, LPCWSTR command
, MSIVIEW
**phview
,
952 sql.command
= command
;
955 sql.r
= ERROR_BAD_QUERY_SYNTAX
;
961 TRACE
("Parse returned %d\n", r
);
966 (*sql.view
)->ops
->delete
(*sql.view
);
972 return ERROR_SUCCESS
;