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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 extern
int SQL_error
(const char *str
);
42 WINE_DEFAULT_DEBUG_CHANNEL
(msi
);
44 typedef
struct tag_SQL_input
49 MSIVIEW
**view
; /* view structure for the resulting query */
53 static LPWSTR SQL_getstring
( void *info
, struct sql_str
*str
);
54 static INT SQL_getint
( void *info
);
55 static int SQL_lex
( void *SQL_lval
, SQL_input
*info
);
57 static void *parser_alloc
( void *info
, unsigned int sz
);
59 static BOOL SQL_MarkPrimaryKeys
( create_col_info
*cols
, string_list
*keys
);
61 static struct expr
* EXPR_complex
( void *info
, struct expr
*l
, UINT op
, struct expr
*r
);
62 static struct expr
* EXPR_column
( void *info
, LPWSTR column
);
63 static struct expr
* EXPR_ival
( void *info
, struct sql_str
*, int sign
);
64 static struct expr
* EXPR_sval
( void *info
, struct sql_str
* );
65 static struct expr
* EXPR_wildcard
( void *info
);
75 string_list
*column_list
;
80 create_col_info
*column_info
;
81 column_assignment update_col_info
;
84 %token TK_ABORT TK_AFTER TK_AGG_FUNCTION TK_ALL TK_AND TK_AS TK_ASC
85 %token TK_BEFORE TK_BEGIN TK_BETWEEN TK_BITAND TK_BITNOT TK_BITOR TK_BY
86 %token TK_CASCADE TK_CASE TK_CHAR TK_CHECK TK_CLUSTER TK_COLLATE TK_COLUMN
87 %token TK_COMMA TK_COMMENT TK_COMMIT TK_CONCAT TK_CONFLICT
88 %token TK_CONSTRAINT TK_COPY TK_CREATE
89 %token TK_DEFAULT TK_DEFERRABLE TK_DEFERRED TK_DELETE TK_DELIMITERS TK_DESC
90 %token TK_DISTINCT TK_DOT TK_DROP TK_EACH
91 %token TK_ELSE TK_END TK_END_OF_FILE TK_EQ TK_EXCEPT TK_EXPLAIN
92 %token TK_FAIL TK_FLOAT TK_FOR TK_FOREIGN TK_FROM TK_FUNCTION
93 %token TK_GE TK_GLOB TK_GROUP TK_GT
94 %token TK_HAVING TK_HOLD
95 %token TK_IGNORE TK_ILLEGAL TK_IMMEDIATE TK_IN TK_INDEX TK_INITIALLY
97 %token TK_INSERT TK_INSTEAD TK_INT
98 %token
<str
> TK_INTEGER
99 %token TK_INTERSECT TK_INTO TK_IS
101 %token TK_JOIN TK_JOIN_KW
103 %token TK_LE TK_LIKE TK_LIMIT TK_LONG TK_LONGCHAR TK_LP TK_LSHIFT TK_LT
104 %token TK_LOCALIZABLE
105 %token TK_MATCH TK_MINUS
106 %token TK_NE TK_NOT TK_NOTNULL TK_NULL
107 %token TK_OBJECT TK_OF TK_OFFSET TK_ON TK_OR TK_ORACLE_OUTER_JOIN TK_ORDER
108 %token TK_PLUS TK_PRAGMA TK_PRIMARY
109 %token TK_RAISE TK_REFERENCES TK_REM TK_REPLACE TK_RESTRICT TK_ROLLBACK
110 %token TK_ROW TK_RP TK_RSHIFT
111 %token TK_SELECT TK_SEMI TK_SET TK_SHORT TK_SLASH TK_SPACE TK_STAR TK_STATEMENT
112 %token
<str
> TK_STRING
113 %token TK_TABLE TK_TEMP TK_THEN TK_TRANSACTION TK_TRIGGER
114 %token TK_UMINUS TK_UNCLOSED_STRING TK_UNION TK_UNIQUE
115 %token TK_UPDATE TK_UPLUS TK_USING
116 %token TK_VACUUM TK_VALUES TK_VIEW
117 %token TK_WHEN TK_WHERE TK_WILDCARD
120 * These are extra tokens used by the lexer but never seen by the
121 * parser. We put them in a rule so that the parser generator will
122 * add them to the parse.h output file.
125 %nonassoc END_OF_FILE ILLEGAL SPACE UNCLOSED_STRING COMMENT FUNCTION
128 %type
<string> column table id
129 %type
<column_list
> selcollist
130 %type
<query
> query from fromtable unorderedsel selectfrom
131 %type
<query
> oneupdate onedelete oneselect onequery onecreate oneinsert
132 %type
<expr
> expr val column_val const_val
133 %type
<column_type
> column_type data_type data_type_l data_count
134 %type
<column_info
> column_def table_def
135 %type
<val_list
> constlist
136 %type
<update_col_info
> column_assignment update_assign_list
143 SQL_input
* sql
= (SQL_input
*) info
;
157 TK_INSERT TK_INTO table TK_LP selcollist TK_RP TK_VALUES TK_LP constlist TK_RP
159 SQL_input
*sql
= (SQL_input
*) info
;
160 MSIVIEW
*insert
= NULL
;
163 r
= INSERT_CreateView
( sql
->db
, &insert
, $3, $5, $9, FALSE
);
168 | TK_INSERT TK_INTO table TK_LP selcollist TK_RP TK_VALUES TK_LP constlist TK_RP TK_TEMP
170 SQL_input
*sql
= (SQL_input
*) info
;
171 MSIVIEW
*insert
= NULL
;
173 INSERT_CreateView
( sql
->db
, &insert
, $3, $5, $9, TRUE
);
181 TK_CREATE TK_TABLE table TK_LP table_def TK_RP
183 SQL_input
* sql
= (SQL_input
*) info
;
184 MSIVIEW
*create
= NULL
;
188 CREATE_CreateView
( sql
->db
, &create
, $3, $5, FALSE
);
193 | TK_CREATE TK_TABLE table TK_LP table_def TK_RP TK_HOLD
195 SQL_input
* sql
= (SQL_input
*) info
;
196 MSIVIEW
*create
= NULL
;
200 CREATE_CreateView
( sql
->db
, &create
, $3, $5, TRUE
);
208 TK_UPDATE table TK_SET update_assign_list TK_WHERE expr
210 SQL_input
* sql
= (SQL_input
*) info
;
211 MSIVIEW
*update
= NULL
;
213 UPDATE_CreateView
( sql
->db
, &update
, $2, &$4, $6 );
223 SQL_input
* sql
= (SQL_input
*) info
;
224 MSIVIEW
*delete
= NULL
;
226 DELETE_CreateView
( sql
->db
, &delete
, $2 );
234 column_def TK_PRIMARY TK_KEY selcollist
236 if
( SQL_MarkPrimaryKeys
( $1, $4 ) )
244 column_def TK_COMMA column column_type
248 for
( ci
= $1; ci
->next
; ci
= ci
->next
)
251 ci
->next
= HeapAlloc
( GetProcessHeap
(), 0, sizeof
*$$
);
257 ci
->next
->colname
= $3;
259 ci
->next
->next
= NULL
;
265 $$
= HeapAlloc
( GetProcessHeap
(), 0, sizeof
*$$
);
277 $$
= $1 | MSITYPE_VALID
;
279 | data_type_l TK_LOCALIZABLE
281 FIXME
("LOCALIZABLE ignored\n");
282 $$
= $1 | MSITYPE_VALID
;
289 $$ |
= MSITYPE_NULLABLE
;
291 | data_type TK_NOT TK_NULL
300 $$
= MSITYPE_STRING |
1;
302 | TK_CHAR TK_LP data_count TK_RP
304 $$
= MSITYPE_STRING |
0x400 |
$3;
331 SQL_input
* sql
= (SQL_input
*) info
;
332 int val
= SQL_getint
(sql
);
333 if
( ( val
> 255 ) ||
( val
< 0 ) )
340 unorderedsel TK_ORDER TK_BY selcollist
342 SQL_input
* sql
= (SQL_input
*) info
;
346 ORDER_CreateView
( sql
->db
, &$$
, $1, $4 );
360 | TK_SELECT TK_DISTINCT selectfrom
362 SQL_input
* sql
= (SQL_input
*) info
;
365 DISTINCT_CreateView
( sql
->db
, &$$
, $3 );
374 SQL_input
* sql
= (SQL_input
*) info
;
378 SELECT_CreateView
( sql
->db
, &$$
, $2, $1 );
392 list
= HeapAlloc
( GetProcessHeap
(), 0, sizeof
*list
);
399 TRACE
("Collist %s\n",debugstr_w
($$
->string));
401 | column TK_COMMA selcollist
405 list
= HeapAlloc
( GetProcessHeap
(), 0, sizeof
*list
);
412 TRACE
("From table: %s\n",debugstr_w
($$
->string));
422 | fromtable TK_WHERE expr
424 SQL_input
* sql
= (SQL_input
*) info
;
428 r
= WHERE_CreateView
( sql
->db
, &$$
, $1, $3 );
429 if
( r
!= ERROR_SUCCESS ||
!$$
)
437 SQL_input
* sql
= (SQL_input
*) info
;
441 r
= TABLE_CreateView
( sql
->db
, $2, &$$
);
442 if
( r
!= ERROR_SUCCESS ||
!$$
)
454 | column_val TK_EQ column_val
456 $$
= EXPR_complex
( info
, $1, OP_EQ
, $3 );
462 $$
= EXPR_complex
( info
, $1, OP_AND
, $3 );
468 $$
= EXPR_complex
( info
, $1, OP_OR
, $3 );
472 | column_val TK_EQ val
474 $$
= EXPR_complex
( info
, $1, OP_EQ
, $3 );
478 | column_val TK_GT val
480 $$
= EXPR_complex
( info
, $1, OP_GT
, $3 );
484 | column_val TK_LT val
486 $$
= EXPR_complex
( info
, $1, OP_LT
, $3 );
490 | column_val TK_LE val
492 $$
= EXPR_complex
( info
, $1, OP_LE
, $3 );
496 | column_val TK_GE val
498 $$
= EXPR_complex
( info
, $1, OP_GE
, $3 );
502 | column_val TK_NE val
504 $$
= EXPR_complex
( info
, $1, OP_NE
, $3 );
508 | column_val TK_IS TK_NULL
510 $$
= EXPR_complex
( info
, $1, OP_ISNULL
, NULL
);
514 | column_val TK_IS TK_NOT TK_NULL
516 $$
= EXPR_complex
( info
, $1, OP_NOTNULL
, NULL
);
532 vals
= parser_alloc
( info
, sizeof
*vals
);
539 | const_val TK_COMMA constlist
543 vals
= parser_alloc
( info
, sizeof
*vals
);
554 | column_assignment TK_COMMA update_assign_list
556 $1.col_list
->next
= $3.col_list
;
557 $1.val_list
->next
= $3.val_list
;
563 column TK_EQ const_val
565 $$.col_list
= HeapAlloc
( GetProcessHeap
(), 0, sizeof
*$$.col_list
);
568 $$.col_list
->string = $1;
569 $$.col_list
->next
= NULL
;
570 $$.val_list
= HeapAlloc
( GetProcessHeap
(), 0, sizeof
*$$.val_list
);
573 $$.val_list
->val
= $3;
574 $$.val_list
->next
= 0;
581 $$
= EXPR_ival
( info
, &$1, 1 );
585 | TK_MINUS TK_INTEGER
587 $$
= EXPR_ival
( info
, &$2, -1 );
593 $$
= EXPR_sval
( info
, &$1 );
599 $$
= EXPR_wildcard
( info
);
608 $$
= EXPR_column
( info
, $1 );
635 $$
= SQL_getstring
( info
, &$1 );
643 static void *parser_alloc
( void *info
, unsigned int sz
)
645 SQL_input
* sql
= (SQL_input
*) info
;
648 mem
= HeapAlloc
( GetProcessHeap
(), 0, sizeof
(struct list
) + sz
);
649 list_add_tail
( sql
->mem
, mem
);
653 int SQL_lex
( void *SQL_lval
, SQL_input
*sql
)
656 struct sql_str
* str
= SQL_lval
;
661 if
( ! sql
->command
[sql
->n
] )
662 return
0; /* end of input */
664 TRACE
("string : %s\n", debugstr_w
(&sql
->command
[sql
->n
]));
665 sql
->len
= sqliteGetToken
( &sql
->command
[sql
->n
], &token
);
668 str
->data
= &sql
->command
[sql
->n
];
671 while
( token
== TK_SPACE
);
673 TRACE
("token : %d (%s)\n", token
, debugstr_wn
(&sql
->command
[sql
->n
], sql
->len
));
678 LPWSTR SQL_getstring
( void *info
, struct sql_str
*strdata
)
680 LPCWSTR p
= strdata
->data
;
681 UINT len
= strdata
->len
;
684 /* if there's quotes, remove them */
685 if
( ( (p
[0]=='`') && (p
[len
-1]=='`') ) ||
686 ( (p
[0]=='\'') && (p
[len
-1]=='\'') ) )
691 str
= parser_alloc
( info
, (len
+ 1)*sizeof
(WCHAR
) );
694 memcpy
( str
, p
, len
*sizeof
(WCHAR
) );
700 INT SQL_getint
( void *info
)
702 SQL_input
* sql
= (SQL_input
*) info
;
703 LPCWSTR p
= &sql
->command
[sql
->n
];
708 int SQL_error
( const char *str
)
713 static struct expr
* EXPR_wildcard
( void *info
)
715 struct expr
*e
= parser_alloc
( info
, sizeof
*e
);
718 e
->type
= EXPR_WILDCARD
;
723 static struct expr
* EXPR_complex
( void *info
, struct expr
*l
, UINT op
, struct expr
*r
)
725 struct expr
*e
= parser_alloc
( info
, sizeof
*e
);
728 e
->type
= EXPR_COMPLEX
;
736 static struct expr
* EXPR_column
( void *info
, LPWSTR column
)
738 struct expr
*e
= parser_alloc
( info
, sizeof
*e
);
741 e
->type
= EXPR_COLUMN
;
747 static struct expr
* EXPR_ival
( void *info
, struct sql_str
*str
, int sign
)
749 struct expr
*e
= parser_alloc
( info
, sizeof
*e
);
753 e
->u.ival
= atoiW
( str
->data
) * sign
;
758 static struct expr
* EXPR_sval
( void *info
, struct sql_str
*str
)
760 struct expr
*e
= parser_alloc
( info
, sizeof
*e
);
764 e
->u.sval
= SQL_getstring
( info
, str
);
769 static BOOL SQL_MarkPrimaryKeys
( create_col_info
*cols
, string_list
*keys
)
774 for
( k
= keys
; k
&& found
; k
= k
->next
)
779 for
( c
= cols
; c
&& !found
; c
= c
->next
)
781 if
( lstrcmpW
( k
->string, c
->colname
) )
783 c
->type |
= MSITYPE_KEY
;
791 UINT MSI_ParseSQL
( MSIDATABASE
*db
, LPCWSTR command
, MSIVIEW
**phview
,
800 sql.command
= command
;
808 TRACE
("Parse returned %d\n", r
);
812 (*sql.view
)->ops
->delete
( *sql.view
);
814 return ERROR_BAD_QUERY_SYNTAX
;
817 return ERROR_SUCCESS
;