xref: /reactos/dll/win32/msi/where.c (revision f4be6dc3)
1c2c66affSColin Finck /*
2c2c66affSColin Finck  * Implementation of the Microsoft Installer (msi.dll)
3c2c66affSColin Finck  *
4c2c66affSColin Finck  * Copyright 2002 Mike McCormack for CodeWeavers
5c2c66affSColin Finck  * Copyright 2011 Bernhard Loos
6c2c66affSColin Finck  *
7c2c66affSColin Finck  * This library is free software; you can redistribute it and/or
8c2c66affSColin Finck  * modify it under the terms of the GNU Lesser General Public
9c2c66affSColin Finck  * License as published by the Free Software Foundation; either
10c2c66affSColin Finck  * version 2.1 of the License, or (at your option) any later version.
11c2c66affSColin Finck  *
12c2c66affSColin Finck  * This library is distributed in the hope that it will be useful,
13c2c66affSColin Finck  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14c2c66affSColin Finck  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15c2c66affSColin Finck  * Lesser General Public License for more details.
16c2c66affSColin Finck  *
17c2c66affSColin Finck  * You should have received a copy of the GNU Lesser General Public
18c2c66affSColin Finck  * License along with this library; if not, write to the Free Software
19c2c66affSColin Finck  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20c2c66affSColin Finck  */
21c2c66affSColin Finck 
22c42b133eSAmine Khaldi #include <stdarg.h>
23c42b133eSAmine Khaldi #include <assert.h>
24c42b133eSAmine Khaldi 
25c42b133eSAmine Khaldi #include "windef.h"
26c42b133eSAmine Khaldi #include "winbase.h"
27c42b133eSAmine Khaldi #include "winerror.h"
28c42b133eSAmine Khaldi #include "wine/debug.h"
29c42b133eSAmine Khaldi #include "msi.h"
30c42b133eSAmine Khaldi #include "msiquery.h"
31c42b133eSAmine Khaldi #include "objbase.h"
32c42b133eSAmine Khaldi #include "objidl.h"
33c2c66affSColin Finck #include "msipriv.h"
34c42b133eSAmine Khaldi #include "winnls.h"
35c42b133eSAmine Khaldi 
36c42b133eSAmine Khaldi #include "query.h"
37c2c66affSColin Finck 
38c2c66affSColin Finck WINE_DEFAULT_DEBUG_CHANNEL(msidb);
39c2c66affSColin Finck 
40c2c66affSColin Finck /* below is the query interface to a table */
41*f4be6dc3SMikhail struct row_entry
42c2c66affSColin Finck {
43c2c66affSColin Finck     struct tagMSIWHEREVIEW *wv; /* used during sorting */
44c2c66affSColin Finck     UINT values[1];
45*f4be6dc3SMikhail };
46c2c66affSColin Finck 
47*f4be6dc3SMikhail struct join_table
48c2c66affSColin Finck {
49*f4be6dc3SMikhail     struct join_table *next;
50c2c66affSColin Finck     MSIVIEW *view;
51c2c66affSColin Finck     UINT col_count;
52c2c66affSColin Finck     UINT row_count;
53c2c66affSColin Finck     UINT table_index;
54*f4be6dc3SMikhail };
55c2c66affSColin Finck 
56c2c66affSColin Finck typedef struct tagMSIORDERINFO
57c2c66affSColin Finck {
58c2c66affSColin Finck     UINT col_count;
59c2c66affSColin Finck     UINT error;
60c2c66affSColin Finck     union ext_column columns[1];
61c2c66affSColin Finck } MSIORDERINFO;
62c2c66affSColin Finck 
63c2c66affSColin Finck typedef struct tagMSIWHEREVIEW
64c2c66affSColin Finck {
65c2c66affSColin Finck     MSIVIEW        view;
66c2c66affSColin Finck     MSIDATABASE   *db;
67*f4be6dc3SMikhail     struct join_table *tables;
68c2c66affSColin Finck     UINT           row_count;
69c2c66affSColin Finck     UINT           col_count;
70c2c66affSColin Finck     UINT           table_count;
71*f4be6dc3SMikhail     struct row_entry **reorder;
72c2c66affSColin Finck     UINT           reorder_size; /* number of entries available in reorder */
73c2c66affSColin Finck     struct expr   *cond;
74c2c66affSColin Finck     UINT           rec_index;
75c2c66affSColin Finck     MSIORDERINFO  *order_info;
76c2c66affSColin Finck } MSIWHEREVIEW;
77c2c66affSColin Finck 
78c2c66affSColin Finck static UINT WHERE_evaluate( MSIWHEREVIEW *wv, const UINT rows[],
79c2c66affSColin Finck                             struct expr *cond, INT *val, MSIRECORD *record );
80c2c66affSColin Finck 
81c2c66affSColin Finck #define INITIAL_REORDER_SIZE 16
82c2c66affSColin Finck 
83c2c66affSColin Finck #define INVALID_ROW_INDEX (-1)
84c2c66affSColin Finck 
free_reorder(MSIWHEREVIEW * wv)85c2c66affSColin Finck static void free_reorder(MSIWHEREVIEW *wv)
86c2c66affSColin Finck {
87c2c66affSColin Finck     UINT i;
88c2c66affSColin Finck 
89c2c66affSColin Finck     if (!wv->reorder)
90c2c66affSColin Finck         return;
91c2c66affSColin Finck 
92c2c66affSColin Finck     for (i = 0; i < wv->row_count; i++)
93*f4be6dc3SMikhail         free(wv->reorder[i]);
94c2c66affSColin Finck 
95*f4be6dc3SMikhail     free(wv->reorder);
96c2c66affSColin Finck     wv->reorder = NULL;
97c2c66affSColin Finck     wv->reorder_size = 0;
98c2c66affSColin Finck     wv->row_count = 0;
99c2c66affSColin Finck }
100c2c66affSColin Finck 
init_reorder(MSIWHEREVIEW * wv)101c2c66affSColin Finck static UINT init_reorder(MSIWHEREVIEW *wv)
102c2c66affSColin Finck {
103*f4be6dc3SMikhail     struct row_entry **new = calloc(INITIAL_REORDER_SIZE, sizeof(*new));
104c2c66affSColin Finck     if (!new)
105c2c66affSColin Finck         return ERROR_OUTOFMEMORY;
106c2c66affSColin Finck 
107c2c66affSColin Finck     free_reorder(wv);
108c2c66affSColin Finck 
109c2c66affSColin Finck     wv->reorder = new;
110c2c66affSColin Finck     wv->reorder_size = INITIAL_REORDER_SIZE;
111c2c66affSColin Finck 
112c2c66affSColin Finck     return ERROR_SUCCESS;
113c2c66affSColin Finck }
114c2c66affSColin Finck 
find_row(MSIWHEREVIEW * wv,UINT row,UINT * (values[]))115c2c66affSColin Finck static inline UINT find_row(MSIWHEREVIEW *wv, UINT row, UINT *(values[]))
116c2c66affSColin Finck {
117c2c66affSColin Finck     if (row >= wv->row_count)
118c2c66affSColin Finck         return ERROR_NO_MORE_ITEMS;
119c2c66affSColin Finck 
120c2c66affSColin Finck     *values = wv->reorder[row]->values;
121c2c66affSColin Finck 
122c2c66affSColin Finck     return ERROR_SUCCESS;
123c2c66affSColin Finck }
124c2c66affSColin Finck 
add_row(MSIWHEREVIEW * wv,UINT vals[])125c2c66affSColin Finck static UINT add_row(MSIWHEREVIEW *wv, UINT vals[])
126c2c66affSColin Finck {
127*f4be6dc3SMikhail     struct row_entry *new;
128c2c66affSColin Finck 
129c2c66affSColin Finck     if (wv->reorder_size <= wv->row_count)
130c2c66affSColin Finck     {
131*f4be6dc3SMikhail         struct row_entry **new_reorder;
132c2c66affSColin Finck         UINT newsize = wv->reorder_size * 2;
133c2c66affSColin Finck 
134*f4be6dc3SMikhail         new_reorder = realloc(wv->reorder, newsize * sizeof(*new_reorder));
135c2c66affSColin Finck         if (!new_reorder)
136c2c66affSColin Finck             return ERROR_OUTOFMEMORY;
1371b18dc22Swinesync         memset(new_reorder + wv->reorder_size, 0, (newsize - wv->reorder_size) * sizeof(*new_reorder));
138c2c66affSColin Finck 
139c2c66affSColin Finck         wv->reorder = new_reorder;
140c2c66affSColin Finck         wv->reorder_size = newsize;
141c2c66affSColin Finck     }
142c2c66affSColin Finck 
143*f4be6dc3SMikhail     new = malloc(offsetof(struct row_entry, values[wv->table_count]));
144c2c66affSColin Finck 
145c2c66affSColin Finck     if (!new)
146c2c66affSColin Finck         return ERROR_OUTOFMEMORY;
147c2c66affSColin Finck 
148c2c66affSColin Finck     wv->reorder[wv->row_count++] = new;
149c2c66affSColin Finck 
150c2c66affSColin Finck     memcpy(new->values, vals, wv->table_count * sizeof(UINT));
151c2c66affSColin Finck     new->wv = wv;
152c2c66affSColin Finck 
153c2c66affSColin Finck     return ERROR_SUCCESS;
154c2c66affSColin Finck }
155c2c66affSColin Finck 
find_table(MSIWHEREVIEW * wv,UINT col,UINT * table_col)156*f4be6dc3SMikhail static struct join_table *find_table(MSIWHEREVIEW *wv, UINT col, UINT *table_col)
157c2c66affSColin Finck {
158*f4be6dc3SMikhail     struct join_table *table = wv->tables;
159c2c66affSColin Finck 
160c2c66affSColin Finck     if(col == 0 || col > wv->col_count)
161c2c66affSColin Finck          return NULL;
162c2c66affSColin Finck 
163c2c66affSColin Finck     while (col > table->col_count)
164c2c66affSColin Finck     {
165c2c66affSColin Finck         col -= table->col_count;
166c2c66affSColin Finck         table = table->next;
167c2c66affSColin Finck         assert(table);
168c2c66affSColin Finck     }
169c2c66affSColin Finck 
170c2c66affSColin Finck     *table_col = col;
171c2c66affSColin Finck     return table;
172c2c66affSColin Finck }
173c2c66affSColin Finck 
parse_column(MSIWHEREVIEW * wv,union ext_column * column,UINT * column_type)174c2c66affSColin Finck static UINT parse_column(MSIWHEREVIEW *wv, union ext_column *column,
175c2c66affSColin Finck                          UINT *column_type)
176c2c66affSColin Finck {
177*f4be6dc3SMikhail     struct join_table *table = wv->tables;
178c2c66affSColin Finck     UINT i, r;
179c2c66affSColin Finck 
180c2c66affSColin Finck     do
181c2c66affSColin Finck     {
182c2c66affSColin Finck         LPCWSTR table_name;
183c2c66affSColin Finck 
184c2c66affSColin Finck         if (column->unparsed.table)
185c2c66affSColin Finck         {
186c2c66affSColin Finck             r = table->view->ops->get_column_info(table->view, 1, NULL, NULL,
187c2c66affSColin Finck                                                   NULL, &table_name);
188c2c66affSColin Finck             if (r != ERROR_SUCCESS)
189c2c66affSColin Finck                 return r;
190958f1addSwinesync             if (wcscmp(table_name, column->unparsed.table) != 0)
191c2c66affSColin Finck                 continue;
192c2c66affSColin Finck         }
193c2c66affSColin Finck 
194c2c66affSColin Finck         for(i = 1; i <= table->col_count; i++)
195c2c66affSColin Finck         {
196c2c66affSColin Finck             LPCWSTR col_name;
197c2c66affSColin Finck 
198c2c66affSColin Finck             r = table->view->ops->get_column_info(table->view, i, &col_name, column_type,
199c2c66affSColin Finck                                                   NULL, NULL);
200c2c66affSColin Finck             if(r != ERROR_SUCCESS )
201c2c66affSColin Finck                 return r;
202c2c66affSColin Finck 
203958f1addSwinesync             if(wcscmp(col_name, column->unparsed.column))
204c2c66affSColin Finck                 continue;
205c2c66affSColin Finck             column->parsed.column = i;
206c2c66affSColin Finck             column->parsed.table = table;
207c2c66affSColin Finck             return ERROR_SUCCESS;
208c2c66affSColin Finck         }
209c2c66affSColin Finck     }
210c2c66affSColin Finck     while ((table = table->next));
211c2c66affSColin Finck 
212c2c66affSColin Finck     WARN("Couldn't find column %s.%s\n", debugstr_w( column->unparsed.table ), debugstr_w( column->unparsed.column ) );
213c2c66affSColin Finck     return ERROR_BAD_QUERY_SYNTAX;
214c2c66affSColin Finck }
215c2c66affSColin Finck 
WHERE_fetch_int(struct tagMSIVIEW * view,UINT row,UINT col,UINT * val)216c2c66affSColin Finck static UINT WHERE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
217c2c66affSColin Finck {
218c2c66affSColin Finck     MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
219*f4be6dc3SMikhail     struct join_table *table;
220c2c66affSColin Finck     UINT *rows;
221c2c66affSColin Finck     UINT r;
222c2c66affSColin Finck 
223c2c66affSColin Finck     TRACE("%p %d %d %p\n", wv, row, col, val );
224c2c66affSColin Finck 
225c2c66affSColin Finck     if( !wv->tables )
226c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
227c2c66affSColin Finck 
228c2c66affSColin Finck     r = find_row(wv, row, &rows);
229c2c66affSColin Finck     if (r != ERROR_SUCCESS)
230c2c66affSColin Finck         return r;
231c2c66affSColin Finck 
232c2c66affSColin Finck     table = find_table(wv, col, &col);
233c2c66affSColin Finck     if (!table)
234c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
235c2c66affSColin Finck 
236c2c66affSColin Finck     return table->view->ops->fetch_int(table->view, rows[table->table_index], col, val);
237c2c66affSColin Finck }
238c2c66affSColin Finck 
WHERE_fetch_stream(struct tagMSIVIEW * view,UINT row,UINT col,IStream ** stm)239c2c66affSColin Finck static UINT WHERE_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm )
240c2c66affSColin Finck {
241c2c66affSColin Finck     MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
242*f4be6dc3SMikhail     struct join_table *table;
243c2c66affSColin Finck     UINT *rows;
244c2c66affSColin Finck     UINT r;
245c2c66affSColin Finck 
246c2c66affSColin Finck     TRACE("%p %d %d %p\n", wv, row, col, stm );
247c2c66affSColin Finck 
248c2c66affSColin Finck     if( !wv->tables )
249c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
250c2c66affSColin Finck 
251c2c66affSColin Finck     r = find_row(wv, row, &rows);
252c2c66affSColin Finck     if (r != ERROR_SUCCESS)
253c2c66affSColin Finck         return r;
254c2c66affSColin Finck 
255c2c66affSColin Finck     table = find_table(wv, col, &col);
256c2c66affSColin Finck     if (!table)
257c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
258c2c66affSColin Finck 
259c2c66affSColin Finck     return table->view->ops->fetch_stream( table->view, rows[table->table_index], col, stm );
260c2c66affSColin Finck }
261c2c66affSColin Finck 
WHERE_set_int(struct tagMSIVIEW * view,UINT row,UINT col,int val)26297918c36Swinesync static UINT WHERE_set_int(struct tagMSIVIEW *view, UINT row, UINT col, int val)
26397918c36Swinesync {
26497918c36Swinesync     MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
265*f4be6dc3SMikhail     struct join_table *table;
26697918c36Swinesync     UINT *rows;
26797918c36Swinesync     UINT r;
26897918c36Swinesync 
26997918c36Swinesync     TRACE("view %p, row %u, col %u, val %d.\n", wv, row, col, val );
27097918c36Swinesync 
27197918c36Swinesync     r = find_row(wv, row, &rows);
27297918c36Swinesync     if (r != ERROR_SUCCESS)
27397918c36Swinesync         return r;
27497918c36Swinesync 
27597918c36Swinesync     table = find_table(wv, col, &col);
27697918c36Swinesync     if (!table)
27797918c36Swinesync         return ERROR_FUNCTION_FAILED;
27897918c36Swinesync 
27997918c36Swinesync     return table->view->ops->set_int(table->view, rows[table->table_index], col, val);
28097918c36Swinesync }
28197918c36Swinesync 
WHERE_set_string(struct tagMSIVIEW * view,UINT row,UINT col,const WCHAR * val,int len)28297918c36Swinesync static UINT WHERE_set_string(struct tagMSIVIEW *view, UINT row, UINT col, const WCHAR *val, int len)
28397918c36Swinesync {
28497918c36Swinesync     MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
285*f4be6dc3SMikhail     struct join_table *table;
28697918c36Swinesync     UINT *rows;
28797918c36Swinesync     UINT r;
28897918c36Swinesync 
28997918c36Swinesync     TRACE("view %p, row %u, col %u, val %s.\n", wv, row, col, debugstr_wn(val, len));
29097918c36Swinesync 
29197918c36Swinesync     r = find_row(wv, row, &rows);
29297918c36Swinesync     if (r != ERROR_SUCCESS)
29397918c36Swinesync         return r;
29497918c36Swinesync 
29597918c36Swinesync     table = find_table(wv, col, &col);
29697918c36Swinesync     if (!table)
29797918c36Swinesync         return ERROR_FUNCTION_FAILED;
29897918c36Swinesync 
29997918c36Swinesync     return table->view->ops->set_string(table->view, rows[table->table_index], col, val, len);
30097918c36Swinesync }
30197918c36Swinesync 
WHERE_set_stream(MSIVIEW * view,UINT row,UINT col,IStream * stream)302580d3e1bSwinesync static UINT WHERE_set_stream(MSIVIEW *view, UINT row, UINT col, IStream *stream)
303580d3e1bSwinesync {
304580d3e1bSwinesync     MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
305*f4be6dc3SMikhail     struct join_table *table;
306580d3e1bSwinesync     UINT *rows;
307580d3e1bSwinesync     UINT r;
308580d3e1bSwinesync 
309580d3e1bSwinesync     TRACE("view %p, row %u, col %u, stream %p.\n", wv, row, col, stream);
310580d3e1bSwinesync 
311580d3e1bSwinesync     r = find_row(wv, row, &rows);
312580d3e1bSwinesync     if (r != ERROR_SUCCESS)
313580d3e1bSwinesync         return r;
314580d3e1bSwinesync 
315580d3e1bSwinesync     table = find_table(wv, col, &col);
316580d3e1bSwinesync     if (!table)
317580d3e1bSwinesync         return ERROR_FUNCTION_FAILED;
318580d3e1bSwinesync 
319580d3e1bSwinesync     return table->view->ops->set_stream(table->view, rows[table->table_index], col, stream);
320580d3e1bSwinesync }
321580d3e1bSwinesync 
WHERE_set_row(struct tagMSIVIEW * view,UINT row,MSIRECORD * rec,UINT mask)322c2c66affSColin Finck static UINT WHERE_set_row( struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask )
323c2c66affSColin Finck {
324c2c66affSColin Finck     MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
325c2c66affSColin Finck     UINT i, r, offset = 0;
326*f4be6dc3SMikhail     struct join_table *table = wv->tables;
327c2c66affSColin Finck     UINT *rows;
328c2c66affSColin Finck     UINT mask_copy = mask;
329c2c66affSColin Finck 
330c2c66affSColin Finck     TRACE("%p %d %p %08x\n", wv, row, rec, mask );
331c2c66affSColin Finck 
332c2c66affSColin Finck     if( !wv->tables )
333c2c66affSColin Finck          return ERROR_FUNCTION_FAILED;
334c2c66affSColin Finck 
335c2c66affSColin Finck     r = find_row(wv, row, &rows);
336c2c66affSColin Finck     if (r != ERROR_SUCCESS)
337c2c66affSColin Finck         return r;
338c2c66affSColin Finck 
339c2c66affSColin Finck     if (mask >= 1 << wv->col_count)
340c2c66affSColin Finck         return ERROR_INVALID_PARAMETER;
341c2c66affSColin Finck 
342c2c66affSColin Finck     do
343c2c66affSColin Finck     {
344c2c66affSColin Finck         for (i = 0; i < table->col_count; i++) {
345c2c66affSColin Finck             UINT type;
346c2c66affSColin Finck 
347c2c66affSColin Finck             if (!(mask_copy & (1 << i)))
348c2c66affSColin Finck                 continue;
349c2c66affSColin Finck             r = table->view->ops->get_column_info(table->view, i + 1, NULL,
350c2c66affSColin Finck                                             &type, NULL, NULL );
351c2c66affSColin Finck             if (r != ERROR_SUCCESS)
352c2c66affSColin Finck                 return r;
353c2c66affSColin Finck             if (type & MSITYPE_KEY)
354c2c66affSColin Finck                 return ERROR_FUNCTION_FAILED;
355c2c66affSColin Finck         }
356c2c66affSColin Finck         mask_copy >>= table->col_count;
357c2c66affSColin Finck     }
358c2c66affSColin Finck     while (mask_copy && (table = table->next));
359c2c66affSColin Finck 
360c2c66affSColin Finck     table = wv->tables;
361c2c66affSColin Finck 
362c2c66affSColin Finck     do
363c2c66affSColin Finck     {
364c2c66affSColin Finck         const UINT col_count = table->col_count;
365c2c66affSColin Finck         UINT i;
366c2c66affSColin Finck         MSIRECORD *reduced;
367c2c66affSColin Finck         UINT reduced_mask = (mask >> offset) & ((1 << col_count) - 1);
368c2c66affSColin Finck 
369c2c66affSColin Finck         if (!reduced_mask)
370c2c66affSColin Finck         {
371c2c66affSColin Finck             offset += col_count;
372c2c66affSColin Finck             continue;
373c2c66affSColin Finck         }
374c2c66affSColin Finck 
375c2c66affSColin Finck         reduced = MSI_CreateRecord(col_count);
376c2c66affSColin Finck         if (!reduced)
377c2c66affSColin Finck             return ERROR_FUNCTION_FAILED;
378c2c66affSColin Finck 
379c2c66affSColin Finck         for (i = 1; i <= col_count; i++)
380c2c66affSColin Finck         {
381c2c66affSColin Finck             r = MSI_RecordCopyField(rec, i + offset, reduced, i);
382c2c66affSColin Finck             if (r != ERROR_SUCCESS)
383c2c66affSColin Finck                 break;
384c2c66affSColin Finck         }
385c2c66affSColin Finck 
386c2c66affSColin Finck         offset += col_count;
387c2c66affSColin Finck 
388c2c66affSColin Finck         if (r == ERROR_SUCCESS)
389c2c66affSColin Finck             r = table->view->ops->set_row(table->view, rows[table->table_index], reduced, reduced_mask);
390c2c66affSColin Finck 
391c2c66affSColin Finck         msiobj_release(&reduced->hdr);
392c2c66affSColin Finck     }
393c2c66affSColin Finck     while ((table = table->next));
394c2c66affSColin Finck     return r;
395c2c66affSColin Finck }
396c2c66affSColin Finck 
WHERE_delete_row(struct tagMSIVIEW * view,UINT row)397c2c66affSColin Finck static UINT WHERE_delete_row(struct tagMSIVIEW *view, UINT row)
398c2c66affSColin Finck {
399c2c66affSColin Finck     MSIWHEREVIEW *wv = (MSIWHEREVIEW *)view;
400c2c66affSColin Finck     UINT r;
401c2c66affSColin Finck     UINT *rows;
402c2c66affSColin Finck 
403c2c66affSColin Finck     TRACE("(%p %d)\n", view, row);
404c2c66affSColin Finck 
405c2c66affSColin Finck     if (!wv->tables)
406c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
407c2c66affSColin Finck 
408c2c66affSColin Finck     r = find_row(wv, row, &rows);
409c2c66affSColin Finck     if ( r != ERROR_SUCCESS )
410c2c66affSColin Finck         return r;
411c2c66affSColin Finck 
412c2c66affSColin Finck     if (wv->table_count > 1)
413c2c66affSColin Finck         return ERROR_CALL_NOT_IMPLEMENTED;
414c2c66affSColin Finck 
415c2c66affSColin Finck     return wv->tables->view->ops->delete_row(wv->tables->view, rows[0]);
416c2c66affSColin Finck }
417c2c66affSColin Finck 
INT_evaluate_binary(MSIWHEREVIEW * wv,const UINT rows[],const struct complex_expr * expr,INT * val,MSIRECORD * record)418c2c66affSColin Finck static INT INT_evaluate_binary( MSIWHEREVIEW *wv, const UINT rows[],
419c2c66affSColin Finck                                 const struct complex_expr *expr, INT *val, MSIRECORD *record )
420c2c66affSColin Finck {
421c2c66affSColin Finck     UINT rl, rr;
422c2c66affSColin Finck     INT lval, rval;
423c2c66affSColin Finck 
424c2c66affSColin Finck     rl = WHERE_evaluate(wv, rows, expr->left, &lval, record);
425c2c66affSColin Finck     if (rl != ERROR_SUCCESS && rl != ERROR_CONTINUE)
426c2c66affSColin Finck         return rl;
427c2c66affSColin Finck     rr = WHERE_evaluate(wv, rows, expr->right, &rval, record);
428c2c66affSColin Finck     if (rr != ERROR_SUCCESS && rr != ERROR_CONTINUE)
429c2c66affSColin Finck         return rr;
430c2c66affSColin Finck 
431c2c66affSColin Finck     if (rl == ERROR_CONTINUE || rr == ERROR_CONTINUE)
432c2c66affSColin Finck     {
433c2c66affSColin Finck         if (rl == rr)
434c2c66affSColin Finck         {
435c2c66affSColin Finck             *val = TRUE;
436c2c66affSColin Finck             return ERROR_CONTINUE;
437c2c66affSColin Finck         }
438c2c66affSColin Finck 
439c2c66affSColin Finck         if (expr->op == OP_AND)
440c2c66affSColin Finck         {
441c2c66affSColin Finck             if ((rl == ERROR_CONTINUE && !rval) || (rr == ERROR_CONTINUE && !lval))
442c2c66affSColin Finck             {
443c2c66affSColin Finck                 *val = FALSE;
444c2c66affSColin Finck                 return ERROR_SUCCESS;
445c2c66affSColin Finck             }
446c2c66affSColin Finck         }
447c2c66affSColin Finck         else if (expr->op == OP_OR)
448c2c66affSColin Finck         {
449c2c66affSColin Finck             if ((rl == ERROR_CONTINUE && rval) || (rr == ERROR_CONTINUE && lval))
450c2c66affSColin Finck             {
451c2c66affSColin Finck                 *val = TRUE;
452c2c66affSColin Finck                 return ERROR_SUCCESS;
453c2c66affSColin Finck             }
454c2c66affSColin Finck         }
455c2c66affSColin Finck 
456c2c66affSColin Finck         *val = TRUE;
457c2c66affSColin Finck         return ERROR_CONTINUE;
458c2c66affSColin Finck     }
459c2c66affSColin Finck 
460c2c66affSColin Finck     switch( expr->op )
461c2c66affSColin Finck     {
462c2c66affSColin Finck     case OP_EQ:
463c2c66affSColin Finck         *val = ( lval == rval );
464c2c66affSColin Finck         break;
465c2c66affSColin Finck     case OP_AND:
466c2c66affSColin Finck         *val = ( lval && rval );
467c2c66affSColin Finck         break;
468c2c66affSColin Finck     case OP_OR:
469c2c66affSColin Finck         *val = ( lval || rval );
470c2c66affSColin Finck         break;
471c2c66affSColin Finck     case OP_GT:
472c2c66affSColin Finck         *val = ( lval > rval );
473c2c66affSColin Finck         break;
474c2c66affSColin Finck     case OP_LT:
475c2c66affSColin Finck         *val = ( lval < rval );
476c2c66affSColin Finck         break;
477c2c66affSColin Finck     case OP_LE:
478c2c66affSColin Finck         *val = ( lval <= rval );
479c2c66affSColin Finck         break;
480c2c66affSColin Finck     case OP_GE:
481c2c66affSColin Finck         *val = ( lval >= rval );
482c2c66affSColin Finck         break;
483c2c66affSColin Finck     case OP_NE:
484c2c66affSColin Finck         *val = ( lval != rval );
485c2c66affSColin Finck         break;
486c2c66affSColin Finck     default:
487c2c66affSColin Finck         ERR("Unknown operator %d\n", expr->op );
488c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
489c2c66affSColin Finck     }
490c2c66affSColin Finck 
491c2c66affSColin Finck     return ERROR_SUCCESS;
492c2c66affSColin Finck }
493c2c66affSColin Finck 
expr_fetch_value(const union ext_column * expr,const UINT rows[],UINT * val)494c2c66affSColin Finck static inline UINT expr_fetch_value(const union ext_column *expr, const UINT rows[], UINT *val)
495c2c66affSColin Finck {
496*f4be6dc3SMikhail     struct join_table *table = expr->parsed.table;
497c2c66affSColin Finck 
498c2c66affSColin Finck     if( rows[table->table_index] == INVALID_ROW_INDEX )
499c2c66affSColin Finck     {
500c2c66affSColin Finck         *val = 1;
501c2c66affSColin Finck         return ERROR_CONTINUE;
502c2c66affSColin Finck     }
503c2c66affSColin Finck     return table->view->ops->fetch_int(table->view, rows[table->table_index],
504c2c66affSColin Finck                                         expr->parsed.column, val);
505c2c66affSColin Finck }
506c2c66affSColin Finck 
507c2c66affSColin Finck 
INT_evaluate_unary(MSIWHEREVIEW * wv,const UINT rows[],const struct complex_expr * expr,INT * val,MSIRECORD * record)508c2c66affSColin Finck static UINT INT_evaluate_unary( MSIWHEREVIEW *wv, const UINT rows[],
509c2c66affSColin Finck                                 const struct complex_expr *expr, INT *val, MSIRECORD *record )
510c2c66affSColin Finck {
511c2c66affSColin Finck     UINT r;
512c2c66affSColin Finck     UINT lval;
513c2c66affSColin Finck 
514c2c66affSColin Finck     r = expr_fetch_value(&expr->left->u.column, rows, &lval);
515c2c66affSColin Finck     if(r != ERROR_SUCCESS)
516c2c66affSColin Finck         return r;
517c2c66affSColin Finck 
518c2c66affSColin Finck     switch( expr->op )
519c2c66affSColin Finck     {
520c2c66affSColin Finck     case OP_ISNULL:
521c2c66affSColin Finck         *val = !lval;
522c2c66affSColin Finck         break;
523c2c66affSColin Finck     case OP_NOTNULL:
524c2c66affSColin Finck         *val = lval;
525c2c66affSColin Finck         break;
526c2c66affSColin Finck     default:
527c2c66affSColin Finck         ERR("Unknown operator %d\n", expr->op );
528c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
529c2c66affSColin Finck     }
530c2c66affSColin Finck     return ERROR_SUCCESS;
531c2c66affSColin Finck }
532c2c66affSColin Finck 
STRING_evaluate(MSIWHEREVIEW * wv,const UINT rows[],const struct expr * expr,const MSIRECORD * record,const WCHAR ** str)533c2c66affSColin Finck static UINT STRING_evaluate( MSIWHEREVIEW *wv, const UINT rows[],
534c2c66affSColin Finck                                      const struct expr *expr,
535c2c66affSColin Finck                                      const MSIRECORD *record,
536c2c66affSColin Finck                                      const WCHAR **str )
537c2c66affSColin Finck {
538c2c66affSColin Finck     UINT val = 0, r = ERROR_SUCCESS;
539c2c66affSColin Finck 
540c2c66affSColin Finck     switch( expr->type )
541c2c66affSColin Finck     {
542c2c66affSColin Finck     case EXPR_COL_NUMBER_STRING:
543c2c66affSColin Finck         r = expr_fetch_value(&expr->u.column, rows, &val);
544c2c66affSColin Finck         if (r == ERROR_SUCCESS)
545c2c66affSColin Finck             *str =  msi_string_lookup(wv->db->strings, val, NULL);
546c2c66affSColin Finck         else
547c2c66affSColin Finck             *str = NULL;
548c2c66affSColin Finck         break;
549c2c66affSColin Finck 
550c2c66affSColin Finck     case EXPR_SVAL:
551c2c66affSColin Finck         *str = expr->u.sval;
552c2c66affSColin Finck         break;
553c2c66affSColin Finck 
554c2c66affSColin Finck     case EXPR_WILDCARD:
555c2c66affSColin Finck         *str = MSI_RecordGetString(record, ++wv->rec_index);
556c2c66affSColin Finck         break;
557c2c66affSColin Finck 
558c2c66affSColin Finck     default:
559c2c66affSColin Finck         ERR("Invalid expression type\n");
560c2c66affSColin Finck         r = ERROR_FUNCTION_FAILED;
561c2c66affSColin Finck         *str = NULL;
562c2c66affSColin Finck         break;
563c2c66affSColin Finck     }
564c2c66affSColin Finck     return r;
565c2c66affSColin Finck }
566c2c66affSColin Finck 
STRCMP_Evaluate(MSIWHEREVIEW * wv,const UINT rows[],const struct complex_expr * expr,INT * val,const MSIRECORD * record)567c2c66affSColin Finck static UINT STRCMP_Evaluate( MSIWHEREVIEW *wv, const UINT rows[], const struct complex_expr *expr,
568c2c66affSColin Finck                              INT *val, const MSIRECORD *record )
569c2c66affSColin Finck {
570c2c66affSColin Finck     int sr;
571c2c66affSColin Finck     const WCHAR *l_str, *r_str;
572c2c66affSColin Finck     UINT r;
573c2c66affSColin Finck 
574c2c66affSColin Finck     *val = TRUE;
575c2c66affSColin Finck     r = STRING_evaluate(wv, rows, expr->left, record, &l_str);
576c2c66affSColin Finck     if (r == ERROR_CONTINUE)
577c2c66affSColin Finck         return r;
578c2c66affSColin Finck     r = STRING_evaluate(wv, rows, expr->right, record, &r_str);
579c2c66affSColin Finck     if (r == ERROR_CONTINUE)
580c2c66affSColin Finck         return r;
581c2c66affSColin Finck 
582c2c66affSColin Finck     if( l_str == r_str ||
583c2c66affSColin Finck         ((!l_str || !*l_str) && (!r_str || !*r_str)) )
584c2c66affSColin Finck         sr = 0;
585c2c66affSColin Finck     else if( l_str && ! r_str )
586c2c66affSColin Finck         sr = 1;
587c2c66affSColin Finck     else if( r_str && ! l_str )
588c2c66affSColin Finck         sr = -1;
589c2c66affSColin Finck     else
590958f1addSwinesync         sr = wcscmp( l_str, r_str );
591c2c66affSColin Finck 
592c2c66affSColin Finck     *val = ( expr->op == OP_EQ && ( sr == 0 ) ) ||
593c2c66affSColin Finck            ( expr->op == OP_NE && ( sr != 0 ) );
594c2c66affSColin Finck 
595c2c66affSColin Finck     return ERROR_SUCCESS;
596c2c66affSColin Finck }
597c2c66affSColin Finck 
WHERE_evaluate(MSIWHEREVIEW * wv,const UINT rows[],struct expr * cond,INT * val,MSIRECORD * record)598c2c66affSColin Finck static UINT WHERE_evaluate( MSIWHEREVIEW *wv, const UINT rows[],
599c2c66affSColin Finck                             struct expr *cond, INT *val, MSIRECORD *record )
600c2c66affSColin Finck {
601c2c66affSColin Finck     UINT r, tval;
602c2c66affSColin Finck 
603c2c66affSColin Finck     if( !cond )
604c2c66affSColin Finck     {
605c2c66affSColin Finck         *val = TRUE;
606c2c66affSColin Finck         return ERROR_SUCCESS;
607c2c66affSColin Finck     }
608c2c66affSColin Finck 
609c2c66affSColin Finck     switch( cond->type )
610c2c66affSColin Finck     {
611c2c66affSColin Finck     case EXPR_COL_NUMBER:
612c2c66affSColin Finck         r = expr_fetch_value(&cond->u.column, rows, &tval);
613c2c66affSColin Finck         if( r != ERROR_SUCCESS )
614c2c66affSColin Finck             return r;
615c2c66affSColin Finck         *val = tval - 0x8000;
616c2c66affSColin Finck         return ERROR_SUCCESS;
617c2c66affSColin Finck 
618c2c66affSColin Finck     case EXPR_COL_NUMBER32:
619c2c66affSColin Finck         r = expr_fetch_value(&cond->u.column, rows, &tval);
620c2c66affSColin Finck         if( r != ERROR_SUCCESS )
621c2c66affSColin Finck             return r;
622c2c66affSColin Finck         *val = tval - 0x80000000;
623c2c66affSColin Finck         return r;
624c2c66affSColin Finck 
625c2c66affSColin Finck     case EXPR_UVAL:
626c2c66affSColin Finck         *val = cond->u.uval;
627c2c66affSColin Finck         return ERROR_SUCCESS;
628c2c66affSColin Finck 
629c2c66affSColin Finck     case EXPR_COMPLEX:
630c2c66affSColin Finck         return INT_evaluate_binary(wv, rows, &cond->u.expr, val, record);
631c2c66affSColin Finck 
632c2c66affSColin Finck     case EXPR_UNARY:
633c2c66affSColin Finck         return INT_evaluate_unary( wv, rows, &cond->u.expr, val, record );
634c2c66affSColin Finck 
635c2c66affSColin Finck     case EXPR_STRCMP:
636c2c66affSColin Finck         return STRCMP_Evaluate( wv, rows, &cond->u.expr, val, record );
637c2c66affSColin Finck 
638c2c66affSColin Finck     case EXPR_WILDCARD:
639c2c66affSColin Finck         *val = MSI_RecordGetInteger( record, ++wv->rec_index );
640c2c66affSColin Finck         return ERROR_SUCCESS;
641c2c66affSColin Finck 
642c2c66affSColin Finck     default:
643c2c66affSColin Finck         ERR("Invalid expression type\n");
644c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
645c2c66affSColin Finck     }
646c2c66affSColin Finck 
647c2c66affSColin Finck     return ERROR_SUCCESS;
648c2c66affSColin Finck }
649c2c66affSColin Finck 
check_condition(MSIWHEREVIEW * wv,MSIRECORD * record,struct join_table ** tables,UINT table_rows[])650*f4be6dc3SMikhail static UINT check_condition( MSIWHEREVIEW *wv, MSIRECORD *record, struct join_table **tables,
651c2c66affSColin Finck                              UINT table_rows[] )
652c2c66affSColin Finck {
653c2c66affSColin Finck     UINT r = ERROR_FUNCTION_FAILED;
654c2c66affSColin Finck     INT val;
655c2c66affSColin Finck 
656c2c66affSColin Finck     for (table_rows[(*tables)->table_index] = 0;
657c2c66affSColin Finck          table_rows[(*tables)->table_index] < (*tables)->row_count;
658c2c66affSColin Finck          table_rows[(*tables)->table_index]++)
659c2c66affSColin Finck     {
660c2c66affSColin Finck         val = 0;
661c2c66affSColin Finck         wv->rec_index = 0;
662c2c66affSColin Finck         r = WHERE_evaluate( wv, table_rows, wv->cond, &val, record );
663c2c66affSColin Finck         if (r != ERROR_SUCCESS && r != ERROR_CONTINUE)
664c2c66affSColin Finck             break;
665c2c66affSColin Finck         if (val)
666c2c66affSColin Finck         {
667c2c66affSColin Finck             if (*(tables + 1))
668c2c66affSColin Finck             {
669c2c66affSColin Finck                 r = check_condition(wv, record, tables + 1, table_rows);
670c2c66affSColin Finck                 if (r != ERROR_SUCCESS)
671c2c66affSColin Finck                     break;
672c2c66affSColin Finck             }
673c2c66affSColin Finck             else
674c2c66affSColin Finck             {
675c2c66affSColin Finck                 if (r != ERROR_SUCCESS)
676c2c66affSColin Finck                     break;
677c2c66affSColin Finck                 add_row (wv, table_rows);
678c2c66affSColin Finck             }
679c2c66affSColin Finck         }
680c2c66affSColin Finck     }
681c2c66affSColin Finck     table_rows[(*tables)->table_index] = INVALID_ROW_INDEX;
682c2c66affSColin Finck     return r;
683c2c66affSColin Finck }
684c2c66affSColin Finck 
compare_entry(const void * left,const void * right)685958f1addSwinesync static int __cdecl compare_entry( const void *left, const void *right )
686c2c66affSColin Finck {
687*f4be6dc3SMikhail     const struct row_entry *le = *(const struct row_entry **)left;
688*f4be6dc3SMikhail     const struct row_entry *re = *(const struct row_entry **)right;
689c2c66affSColin Finck     const MSIWHEREVIEW *wv = le->wv;
690c2c66affSColin Finck     MSIORDERINFO *order = wv->order_info;
691c2c66affSColin Finck     UINT i, j, r, l_val, r_val;
692c2c66affSColin Finck 
693c2c66affSColin Finck     assert(le->wv == re->wv);
694c2c66affSColin Finck 
695c2c66affSColin Finck     if (order)
696c2c66affSColin Finck     {
697c2c66affSColin Finck         for (i = 0; i < order->col_count; i++)
698c2c66affSColin Finck         {
699c2c66affSColin Finck             const union ext_column *column = &order->columns[i];
700c2c66affSColin Finck 
701c2c66affSColin Finck             r = column->parsed.table->view->ops->fetch_int(column->parsed.table->view,
702c2c66affSColin Finck                           le->values[column->parsed.table->table_index],
703c2c66affSColin Finck                           column->parsed.column, &l_val);
704c2c66affSColin Finck             if (r != ERROR_SUCCESS)
705c2c66affSColin Finck             {
706c2c66affSColin Finck                 order->error = r;
707c2c66affSColin Finck                 return 0;
708c2c66affSColin Finck             }
709c2c66affSColin Finck 
710c2c66affSColin Finck             r = column->parsed.table->view->ops->fetch_int(column->parsed.table->view,
711c2c66affSColin Finck                           re->values[column->parsed.table->table_index],
712c2c66affSColin Finck                           column->parsed.column, &r_val);
713c2c66affSColin Finck             if (r != ERROR_SUCCESS)
714c2c66affSColin Finck             {
715c2c66affSColin Finck                 order->error = r;
716c2c66affSColin Finck                 return 0;
717c2c66affSColin Finck             }
718c2c66affSColin Finck 
719c2c66affSColin Finck             if (l_val != r_val)
720c2c66affSColin Finck                 return l_val < r_val ? -1 : 1;
721c2c66affSColin Finck         }
722c2c66affSColin Finck     }
723c2c66affSColin Finck 
724c2c66affSColin Finck     for (j = 0; j < wv->table_count; j++)
725c2c66affSColin Finck     {
726c2c66affSColin Finck         if (le->values[j] != re->values[j])
727c2c66affSColin Finck             return le->values[j] < re->values[j] ? -1 : 1;
728c2c66affSColin Finck     }
729c2c66affSColin Finck     return 0;
730c2c66affSColin Finck }
731c2c66affSColin Finck 
add_to_array(struct join_table ** array,struct join_table * elem)732*f4be6dc3SMikhail static void add_to_array( struct join_table **array, struct join_table *elem )
733c2c66affSColin Finck {
734c2c66affSColin Finck     while (*array && *array != elem)
735c2c66affSColin Finck         array++;
736c2c66affSColin Finck     if (!*array)
737c2c66affSColin Finck         *array = elem;
738c2c66affSColin Finck }
739c2c66affSColin Finck 
in_array(struct join_table ** array,struct join_table * elem)740*f4be6dc3SMikhail static BOOL in_array( struct join_table **array, struct join_table *elem )
741c2c66affSColin Finck {
742c2c66affSColin Finck     while (*array && *array != elem)
743c2c66affSColin Finck         array++;
744c2c66affSColin Finck     return *array != NULL;
745c2c66affSColin Finck }
746c2c66affSColin Finck 
747c2c66affSColin Finck #define CONST_EXPR 1 /* comparison to a constant value */
748c2c66affSColin Finck #define JOIN_TO_CONST_EXPR 0x10000 /* comparison to a table involved with
749c2c66affSColin Finck                                       a CONST_EXPR comaprison */
750c2c66affSColin Finck 
reorder_check(const struct expr * expr,struct join_table ** ordered_tables,BOOL process_joins,struct join_table ** lastused)751*f4be6dc3SMikhail static UINT reorder_check( const struct expr *expr, struct join_table **ordered_tables,
752*f4be6dc3SMikhail                            BOOL process_joins, struct join_table **lastused )
753c2c66affSColin Finck {
754c2c66affSColin Finck     UINT res = 0;
755c2c66affSColin Finck 
756c2c66affSColin Finck     switch (expr->type)
757c2c66affSColin Finck     {
758c2c66affSColin Finck         case EXPR_WILDCARD:
759c2c66affSColin Finck         case EXPR_SVAL:
760c2c66affSColin Finck         case EXPR_UVAL:
761c2c66affSColin Finck             return 0;
762c2c66affSColin Finck         case EXPR_COL_NUMBER:
763c2c66affSColin Finck         case EXPR_COL_NUMBER32:
764c2c66affSColin Finck         case EXPR_COL_NUMBER_STRING:
765c2c66affSColin Finck             if (in_array(ordered_tables, expr->u.column.parsed.table))
766c2c66affSColin Finck                 return JOIN_TO_CONST_EXPR;
767c2c66affSColin Finck             *lastused = expr->u.column.parsed.table;
768c2c66affSColin Finck             return CONST_EXPR;
769c2c66affSColin Finck         case EXPR_STRCMP:
770c2c66affSColin Finck         case EXPR_COMPLEX:
771c2c66affSColin Finck             res = reorder_check(expr->u.expr.right, ordered_tables, process_joins, lastused);
772c2c66affSColin Finck             /* fall through */
773c2c66affSColin Finck         case EXPR_UNARY:
774c2c66affSColin Finck             res += reorder_check(expr->u.expr.left, ordered_tables, process_joins, lastused);
775c2c66affSColin Finck             if (res == 0)
776c2c66affSColin Finck                 return 0;
777c2c66affSColin Finck             if (res == CONST_EXPR)
778c2c66affSColin Finck                 add_to_array(ordered_tables, *lastused);
779c2c66affSColin Finck             if (process_joins && res == JOIN_TO_CONST_EXPR + CONST_EXPR)
780c2c66affSColin Finck                 add_to_array(ordered_tables, *lastused);
781c2c66affSColin Finck             return res;
782c2c66affSColin Finck         default:
783c2c66affSColin Finck             ERR("Unknown expr type: %i\n", expr->type);
784c2c66affSColin Finck             assert(0);
785c2c66affSColin Finck             return 0x1000000;
786c2c66affSColin Finck     }
787c2c66affSColin Finck }
788c2c66affSColin Finck 
789c2c66affSColin Finck /* reorders the tablelist in a way to evaluate the condition as fast as possible */
ordertables(MSIWHEREVIEW * wv)790*f4be6dc3SMikhail static struct join_table **ordertables( MSIWHEREVIEW *wv )
791c2c66affSColin Finck {
792*f4be6dc3SMikhail     struct join_table *table, **tables;
793c2c66affSColin Finck 
794*f4be6dc3SMikhail     tables = calloc(wv->table_count + 1, sizeof(*tables));
795c2c66affSColin Finck 
796c2c66affSColin Finck     if (wv->cond)
797c2c66affSColin Finck     {
798c2c66affSColin Finck         table = NULL;
799c2c66affSColin Finck         reorder_check(wv->cond, tables, FALSE, &table);
800c2c66affSColin Finck         table = NULL;
801c2c66affSColin Finck         reorder_check(wv->cond, tables, TRUE, &table);
802c2c66affSColin Finck     }
803c2c66affSColin Finck 
804c2c66affSColin Finck     table = wv->tables;
805c2c66affSColin Finck     while (table)
806c2c66affSColin Finck     {
807c2c66affSColin Finck         add_to_array(tables, table);
808c2c66affSColin Finck         table = table->next;
809c2c66affSColin Finck     }
810c2c66affSColin Finck     return tables;
811c2c66affSColin Finck }
812c2c66affSColin Finck 
WHERE_execute(struct tagMSIVIEW * view,MSIRECORD * record)813c2c66affSColin Finck static UINT WHERE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
814c2c66affSColin Finck {
815c2c66affSColin Finck     MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
816c2c66affSColin Finck     UINT r;
817*f4be6dc3SMikhail     struct join_table *table = wv->tables;
818c2c66affSColin Finck     UINT *rows;
819*f4be6dc3SMikhail     struct join_table **ordered_tables;
820c2c66affSColin Finck     UINT i = 0;
821c2c66affSColin Finck 
822c2c66affSColin Finck     TRACE("%p %p\n", wv, record);
823c2c66affSColin Finck 
824c2c66affSColin Finck     if( !table )
825c2c66affSColin Finck          return ERROR_FUNCTION_FAILED;
826c2c66affSColin Finck 
827c2c66affSColin Finck     r = init_reorder(wv);
828c2c66affSColin Finck     if (r != ERROR_SUCCESS)
829c2c66affSColin Finck         return r;
830c2c66affSColin Finck 
831c2c66affSColin Finck     do
832c2c66affSColin Finck     {
833c2c66affSColin Finck         table->view->ops->execute(table->view, NULL);
834c2c66affSColin Finck 
835c2c66affSColin Finck         r = table->view->ops->get_dimensions(table->view, &table->row_count, NULL);
836c2c66affSColin Finck         if (r != ERROR_SUCCESS)
837c2c66affSColin Finck         {
838c2c66affSColin Finck             ERR("failed to get table dimensions\n");
839c2c66affSColin Finck             return r;
840c2c66affSColin Finck         }
841c2c66affSColin Finck 
842c2c66affSColin Finck         /* each table must have at least one row */
843c2c66affSColin Finck         if (table->row_count == 0)
844c2c66affSColin Finck             return ERROR_SUCCESS;
845c2c66affSColin Finck     }
846c2c66affSColin Finck     while ((table = table->next));
847c2c66affSColin Finck 
848c2c66affSColin Finck     ordered_tables = ordertables( wv );
849c2c66affSColin Finck 
850*f4be6dc3SMikhail     rows = malloc(wv->table_count * sizeof(*rows));
851c2c66affSColin Finck     for (i = 0; i < wv->table_count; i++)
852c2c66affSColin Finck         rows[i] = INVALID_ROW_INDEX;
853c2c66affSColin Finck 
854c2c66affSColin Finck     r =  check_condition(wv, record, ordered_tables, rows);
855c2c66affSColin Finck 
856c2c66affSColin Finck     if (wv->order_info)
857c2c66affSColin Finck         wv->order_info->error = ERROR_SUCCESS;
858c2c66affSColin Finck 
859*f4be6dc3SMikhail     qsort(wv->reorder, wv->row_count, sizeof(struct row_entry *), compare_entry);
860c2c66affSColin Finck 
861c2c66affSColin Finck     if (wv->order_info)
862c2c66affSColin Finck         r = wv->order_info->error;
863c2c66affSColin Finck 
864*f4be6dc3SMikhail     free(rows);
865*f4be6dc3SMikhail     free(ordered_tables);
866c2c66affSColin Finck     return r;
867c2c66affSColin Finck }
868c2c66affSColin Finck 
WHERE_close(struct tagMSIVIEW * view)869c2c66affSColin Finck static UINT WHERE_close( struct tagMSIVIEW *view )
870c2c66affSColin Finck {
871c2c66affSColin Finck     MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
872*f4be6dc3SMikhail     struct join_table *table = wv->tables;
873c2c66affSColin Finck 
874c2c66affSColin Finck     TRACE("%p\n", wv );
875c2c66affSColin Finck 
876c2c66affSColin Finck     if (!table)
877c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
878c2c66affSColin Finck 
879c2c66affSColin Finck     do
880c2c66affSColin Finck         table->view->ops->close(table->view);
881c2c66affSColin Finck     while ((table = table->next));
882c2c66affSColin Finck 
883c2c66affSColin Finck     return ERROR_SUCCESS;
884c2c66affSColin Finck }
885c2c66affSColin Finck 
WHERE_get_dimensions(struct tagMSIVIEW * view,UINT * rows,UINT * cols)886c2c66affSColin Finck static UINT WHERE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols )
887c2c66affSColin Finck {
888c2c66affSColin Finck     MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
889c2c66affSColin Finck 
890c2c66affSColin Finck     TRACE("%p %p %p\n", wv, rows, cols );
891c2c66affSColin Finck 
892c2c66affSColin Finck     if(!wv->tables)
893c2c66affSColin Finck          return ERROR_FUNCTION_FAILED;
894c2c66affSColin Finck 
895c2c66affSColin Finck     if (rows)
896c2c66affSColin Finck     {
897c2c66affSColin Finck         if (!wv->reorder)
898c2c66affSColin Finck             return ERROR_FUNCTION_FAILED;
899c2c66affSColin Finck         *rows = wv->row_count;
900c2c66affSColin Finck     }
901c2c66affSColin Finck 
902c2c66affSColin Finck     if (cols)
903c2c66affSColin Finck         *cols = wv->col_count;
904c2c66affSColin Finck 
905c2c66affSColin Finck     return ERROR_SUCCESS;
906c2c66affSColin Finck }
907c2c66affSColin Finck 
WHERE_get_column_info(struct tagMSIVIEW * view,UINT n,LPCWSTR * name,UINT * type,BOOL * temporary,LPCWSTR * table_name)908c2c66affSColin Finck static UINT WHERE_get_column_info( struct tagMSIVIEW *view, UINT n, LPCWSTR *name,
909c2c66affSColin Finck                                    UINT *type, BOOL *temporary, LPCWSTR *table_name )
910c2c66affSColin Finck {
911c2c66affSColin Finck     MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
912*f4be6dc3SMikhail     struct join_table *table;
913c2c66affSColin Finck 
914c2c66affSColin Finck     TRACE("%p %d %p %p %p %p\n", wv, n, name, type, temporary, table_name );
915c2c66affSColin Finck 
916c2c66affSColin Finck     if(!wv->tables)
917c2c66affSColin Finck          return ERROR_FUNCTION_FAILED;
918c2c66affSColin Finck 
919c2c66affSColin Finck     table = find_table(wv, n, &n);
920c2c66affSColin Finck     if (!table)
921c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
922c2c66affSColin Finck 
923c2c66affSColin Finck     return table->view->ops->get_column_info(table->view, n, name,
924c2c66affSColin Finck                                             type, temporary, table_name);
925c2c66affSColin Finck }
926c2c66affSColin Finck 
join_find_row(MSIWHEREVIEW * wv,MSIRECORD * rec,UINT * row)927c2c66affSColin Finck static UINT join_find_row( MSIWHEREVIEW *wv, MSIRECORD *rec, UINT *row )
928c2c66affSColin Finck {
929c2c66affSColin Finck     LPCWSTR str;
930c2c66affSColin Finck     UINT r, i, id, data;
931c2c66affSColin Finck 
932c2c66affSColin Finck     str = MSI_RecordGetString( rec, 1 );
933c2c66affSColin Finck     r = msi_string2id( wv->db->strings, str, -1, &id );
934c2c66affSColin Finck     if (r != ERROR_SUCCESS)
935c2c66affSColin Finck         return r;
936c2c66affSColin Finck 
937c2c66affSColin Finck     for (i = 0; i < wv->row_count; i++)
938c2c66affSColin Finck     {
939c2c66affSColin Finck         WHERE_fetch_int( &wv->view, i, 1, &data );
940c2c66affSColin Finck 
941c2c66affSColin Finck         if (data == id)
942c2c66affSColin Finck         {
943c2c66affSColin Finck             *row = i;
944c2c66affSColin Finck             return ERROR_SUCCESS;
945c2c66affSColin Finck         }
946c2c66affSColin Finck     }
947c2c66affSColin Finck 
948c2c66affSColin Finck     return ERROR_FUNCTION_FAILED;
949c2c66affSColin Finck }
950c2c66affSColin Finck 
join_modify_update(struct tagMSIVIEW * view,MSIRECORD * rec)951c2c66affSColin Finck static UINT join_modify_update( struct tagMSIVIEW *view, MSIRECORD *rec )
952c2c66affSColin Finck {
953c2c66affSColin Finck     MSIWHEREVIEW *wv = (MSIWHEREVIEW *)view;
954c2c66affSColin Finck     UINT r, row, i, mask = 0;
955c2c66affSColin Finck     MSIRECORD *current;
956c2c66affSColin Finck 
957c2c66affSColin Finck 
958c2c66affSColin Finck     r = join_find_row( wv, rec, &row );
959c2c66affSColin Finck     if (r != ERROR_SUCCESS)
960c2c66affSColin Finck         return r;
961c2c66affSColin Finck 
962c2c66affSColin Finck     r = msi_view_get_row( wv->db, view, row, &current );
963c2c66affSColin Finck     if (r != ERROR_SUCCESS)
964c2c66affSColin Finck         return r;
965c2c66affSColin Finck 
966c2c66affSColin Finck     assert(MSI_RecordGetFieldCount(rec) == MSI_RecordGetFieldCount(current));
967c2c66affSColin Finck 
968c2c66affSColin Finck     for (i = MSI_RecordGetFieldCount(rec); i > 0; i--)
969c2c66affSColin Finck     {
970c2c66affSColin Finck         if (!MSI_RecordsAreFieldsEqual(rec, current, i))
971c2c66affSColin Finck             mask |= 1 << (i - 1);
972c2c66affSColin Finck     }
973c2c66affSColin Finck      msiobj_release(&current->hdr);
974c2c66affSColin Finck 
975c2c66affSColin Finck     return WHERE_set_row( view, row, rec, mask );
976c2c66affSColin Finck }
977c2c66affSColin Finck 
WHERE_modify(struct tagMSIVIEW * view,MSIMODIFY eModifyMode,MSIRECORD * rec,UINT row)978c2c66affSColin Finck static UINT WHERE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
979c2c66affSColin Finck                           MSIRECORD *rec, UINT row )
980c2c66affSColin Finck {
981c2c66affSColin Finck     MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
982*f4be6dc3SMikhail     struct join_table *table = wv->tables;
983c2c66affSColin Finck     UINT r;
984c2c66affSColin Finck 
985c2c66affSColin Finck     TRACE("%p %d %p\n", wv, eModifyMode, rec);
986c2c66affSColin Finck 
987c2c66affSColin Finck     if (!table)
988c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
989c2c66affSColin Finck 
990c2c66affSColin Finck     if (!table->next)
991c2c66affSColin Finck     {
992c2c66affSColin Finck         UINT *rows;
993c2c66affSColin Finck 
994def8c91eSwinesync         if (find_row(wv, row, &rows) == ERROR_SUCCESS)
995def8c91eSwinesync             row = rows[0];
996c2c66affSColin Finck         else
997c2c66affSColin Finck             row = -1;
998c2c66affSColin Finck 
999c2c66affSColin Finck         return table->view->ops->modify(table->view, eModifyMode, rec, row);
1000c2c66affSColin Finck     }
1001c2c66affSColin Finck 
1002c2c66affSColin Finck     switch (eModifyMode)
1003c2c66affSColin Finck     {
1004c2c66affSColin Finck     case MSIMODIFY_UPDATE:
1005c2c66affSColin Finck         return join_modify_update( view, rec );
1006c2c66affSColin Finck 
1007c2c66affSColin Finck     case MSIMODIFY_ASSIGN:
1008c2c66affSColin Finck     case MSIMODIFY_DELETE:
1009c2c66affSColin Finck     case MSIMODIFY_INSERT:
1010c2c66affSColin Finck     case MSIMODIFY_INSERT_TEMPORARY:
1011c2c66affSColin Finck     case MSIMODIFY_MERGE:
1012c2c66affSColin Finck     case MSIMODIFY_REPLACE:
1013c2c66affSColin Finck     case MSIMODIFY_SEEK:
1014c2c66affSColin Finck     case MSIMODIFY_VALIDATE:
1015c2c66affSColin Finck     case MSIMODIFY_VALIDATE_DELETE:
1016c2c66affSColin Finck     case MSIMODIFY_VALIDATE_FIELD:
1017c2c66affSColin Finck     case MSIMODIFY_VALIDATE_NEW:
1018c2c66affSColin Finck         r = ERROR_FUNCTION_FAILED;
1019c2c66affSColin Finck         break;
1020c2c66affSColin Finck 
1021c2c66affSColin Finck     case MSIMODIFY_REFRESH:
1022c2c66affSColin Finck         r = ERROR_CALL_NOT_IMPLEMENTED;
1023c2c66affSColin Finck         break;
1024c2c66affSColin Finck 
1025c2c66affSColin Finck     default:
1026c2c66affSColin Finck         WARN("%p %d %p %u - unknown mode\n", view, eModifyMode, rec, row );
1027c2c66affSColin Finck         r = ERROR_INVALID_PARAMETER;
1028c2c66affSColin Finck         break;
1029c2c66affSColin Finck     }
1030c2c66affSColin Finck 
1031c2c66affSColin Finck     return r;
1032c2c66affSColin Finck }
1033c2c66affSColin Finck 
WHERE_delete(struct tagMSIVIEW * view)1034c2c66affSColin Finck static UINT WHERE_delete( struct tagMSIVIEW *view )
1035c2c66affSColin Finck {
1036c2c66affSColin Finck     MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
1037*f4be6dc3SMikhail     struct join_table *table = wv->tables;
1038c2c66affSColin Finck 
1039c2c66affSColin Finck     TRACE("%p\n", wv );
1040c2c66affSColin Finck 
1041c2c66affSColin Finck     while(table)
1042c2c66affSColin Finck     {
1043*f4be6dc3SMikhail         struct join_table *next;
1044c2c66affSColin Finck 
1045c2c66affSColin Finck         table->view->ops->delete(table->view);
1046c2c66affSColin Finck         table->view = NULL;
1047c2c66affSColin Finck         next = table->next;
1048*f4be6dc3SMikhail         free(table);
1049c2c66affSColin Finck         table = next;
1050c2c66affSColin Finck     }
1051c2c66affSColin Finck     wv->tables = NULL;
1052c2c66affSColin Finck     wv->table_count = 0;
1053c2c66affSColin Finck 
1054c2c66affSColin Finck     free_reorder(wv);
1055c2c66affSColin Finck 
1056*f4be6dc3SMikhail     free(wv->order_info);
1057c2c66affSColin Finck     wv->order_info = NULL;
1058c2c66affSColin Finck 
1059c2c66affSColin Finck     msiobj_release( &wv->db->hdr );
1060*f4be6dc3SMikhail     free(wv);
1061c2c66affSColin Finck 
1062c2c66affSColin Finck     return ERROR_SUCCESS;
1063c2c66affSColin Finck }
1064c2c66affSColin Finck 
WHERE_sort(struct tagMSIVIEW * view,column_info * columns)1065c2c66affSColin Finck static UINT WHERE_sort(struct tagMSIVIEW *view, column_info *columns)
1066c2c66affSColin Finck {
1067c2c66affSColin Finck     MSIWHEREVIEW *wv = (MSIWHEREVIEW *)view;
1068*f4be6dc3SMikhail     struct join_table *table = wv->tables;
1069c2c66affSColin Finck     column_info *column = columns;
1070c2c66affSColin Finck     MSIORDERINFO *orderinfo;
1071c2c66affSColin Finck     UINT r, count = 0;
1072c2c66affSColin Finck     UINT i;
1073c2c66affSColin Finck 
1074c2c66affSColin Finck     TRACE("%p %p\n", view, columns);
1075c2c66affSColin Finck 
1076c2c66affSColin Finck     if (!table)
1077c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
1078c2c66affSColin Finck 
1079c2c66affSColin Finck     while (column)
1080c2c66affSColin Finck     {
1081c2c66affSColin Finck         count++;
1082c2c66affSColin Finck         column = column->next;
1083c2c66affSColin Finck     }
1084c2c66affSColin Finck 
1085c2c66affSColin Finck     if (count == 0)
1086c2c66affSColin Finck         return ERROR_SUCCESS;
1087c2c66affSColin Finck 
1088*f4be6dc3SMikhail     orderinfo = malloc(offsetof(MSIORDERINFO, columns[count]));
1089c2c66affSColin Finck     if (!orderinfo)
1090c2c66affSColin Finck         return ERROR_OUTOFMEMORY;
1091c2c66affSColin Finck 
1092c2c66affSColin Finck     orderinfo->col_count = count;
1093c2c66affSColin Finck 
1094c2c66affSColin Finck     column = columns;
1095c2c66affSColin Finck 
1096c2c66affSColin Finck     for (i = 0; i < count; i++)
1097c2c66affSColin Finck     {
1098c2c66affSColin Finck         orderinfo->columns[i].unparsed.column = column->column;
1099c2c66affSColin Finck         orderinfo->columns[i].unparsed.table = column->table;
1100c2c66affSColin Finck 
1101c2c66affSColin Finck         r = parse_column(wv, &orderinfo->columns[i], NULL);
1102c2c66affSColin Finck         if (r != ERROR_SUCCESS)
1103c2c66affSColin Finck             goto error;
1104c2c66affSColin Finck     }
1105c2c66affSColin Finck 
1106c2c66affSColin Finck     wv->order_info = orderinfo;
1107c2c66affSColin Finck 
1108c2c66affSColin Finck     return ERROR_SUCCESS;
1109c2c66affSColin Finck error:
1110*f4be6dc3SMikhail     free(orderinfo);
1111c2c66affSColin Finck     return r;
1112c2c66affSColin Finck }
1113c2c66affSColin Finck 
1114c2c66affSColin Finck static const MSIVIEWOPS where_ops =
1115c2c66affSColin Finck {
1116c2c66affSColin Finck     WHERE_fetch_int,
1117c2c66affSColin Finck     WHERE_fetch_stream,
111897918c36Swinesync     WHERE_set_int,
111997918c36Swinesync     WHERE_set_string,
1120580d3e1bSwinesync     WHERE_set_stream,
1121c2c66affSColin Finck     WHERE_set_row,
1122c2c66affSColin Finck     NULL,
1123c2c66affSColin Finck     WHERE_delete_row,
1124c2c66affSColin Finck     WHERE_execute,
1125c2c66affSColin Finck     WHERE_close,
1126c2c66affSColin Finck     WHERE_get_dimensions,
1127c2c66affSColin Finck     WHERE_get_column_info,
1128c2c66affSColin Finck     WHERE_modify,
1129c2c66affSColin Finck     WHERE_delete,
1130c2c66affSColin Finck     NULL,
1131c2c66affSColin Finck     NULL,
1132c2c66affSColin Finck     NULL,
1133c2c66affSColin Finck     WHERE_sort,
1134c2c66affSColin Finck     NULL,
1135c2c66affSColin Finck };
1136c2c66affSColin Finck 
WHERE_VerifyCondition(MSIWHEREVIEW * wv,struct expr * cond,UINT * valid)1137c2c66affSColin Finck static UINT WHERE_VerifyCondition( MSIWHEREVIEW *wv, struct expr *cond,
1138c2c66affSColin Finck                                    UINT *valid )
1139c2c66affSColin Finck {
1140c2c66affSColin Finck     UINT r;
1141c2c66affSColin Finck 
1142c2c66affSColin Finck     switch( cond->type )
1143c2c66affSColin Finck     {
1144c2c66affSColin Finck     case EXPR_COLUMN:
1145c2c66affSColin Finck     {
1146c2c66affSColin Finck         UINT type;
1147c2c66affSColin Finck 
1148c2c66affSColin Finck         *valid = FALSE;
1149c2c66affSColin Finck 
1150c2c66affSColin Finck         r = parse_column(wv, &cond->u.column, &type);
1151c2c66affSColin Finck         if (r != ERROR_SUCCESS)
1152c2c66affSColin Finck             break;
1153c2c66affSColin Finck 
1154c2c66affSColin Finck         if (type&MSITYPE_STRING)
1155c2c66affSColin Finck             cond->type = EXPR_COL_NUMBER_STRING;
1156c2c66affSColin Finck         else if ((type&0xff) == 4)
1157c2c66affSColin Finck             cond->type = EXPR_COL_NUMBER32;
1158c2c66affSColin Finck         else
1159c2c66affSColin Finck             cond->type = EXPR_COL_NUMBER;
1160c2c66affSColin Finck 
1161c2c66affSColin Finck         *valid = TRUE;
1162c2c66affSColin Finck         break;
1163c2c66affSColin Finck     }
1164c2c66affSColin Finck     case EXPR_COMPLEX:
1165c2c66affSColin Finck         r = WHERE_VerifyCondition( wv, cond->u.expr.left, valid );
1166c2c66affSColin Finck         if( r != ERROR_SUCCESS )
1167c2c66affSColin Finck             return r;
1168c2c66affSColin Finck         if( !*valid )
1169c2c66affSColin Finck             return ERROR_SUCCESS;
1170c2c66affSColin Finck         r = WHERE_VerifyCondition( wv, cond->u.expr.right, valid );
1171c2c66affSColin Finck         if( r != ERROR_SUCCESS )
1172c2c66affSColin Finck             return r;
1173c2c66affSColin Finck 
1174c2c66affSColin Finck         /* check the type of the comparison */
1175c2c66affSColin Finck         if( ( cond->u.expr.left->type == EXPR_SVAL ) ||
1176c2c66affSColin Finck             ( cond->u.expr.left->type == EXPR_COL_NUMBER_STRING ) ||
1177c2c66affSColin Finck             ( cond->u.expr.right->type == EXPR_SVAL ) ||
1178c2c66affSColin Finck             ( cond->u.expr.right->type == EXPR_COL_NUMBER_STRING ) )
1179c2c66affSColin Finck         {
1180c2c66affSColin Finck             switch( cond->u.expr.op )
1181c2c66affSColin Finck             {
1182c2c66affSColin Finck             case OP_EQ:
1183c2c66affSColin Finck             case OP_NE:
1184c2c66affSColin Finck                 break;
1185c2c66affSColin Finck             default:
1186c2c66affSColin Finck                 *valid = FALSE;
1187c2c66affSColin Finck                 return ERROR_INVALID_PARAMETER;
1188c2c66affSColin Finck             }
1189c2c66affSColin Finck 
1190c2c66affSColin Finck             /* FIXME: check we're comparing a string to a column */
1191c2c66affSColin Finck 
1192c2c66affSColin Finck             cond->type = EXPR_STRCMP;
1193c2c66affSColin Finck         }
1194c2c66affSColin Finck 
1195c2c66affSColin Finck         break;
1196c2c66affSColin Finck     case EXPR_UNARY:
1197c2c66affSColin Finck         if ( cond->u.expr.left->type != EXPR_COLUMN )
1198c2c66affSColin Finck         {
1199c2c66affSColin Finck             *valid = FALSE;
1200c2c66affSColin Finck             return ERROR_INVALID_PARAMETER;
1201c2c66affSColin Finck         }
1202c2c66affSColin Finck         r = WHERE_VerifyCondition( wv, cond->u.expr.left, valid );
1203c2c66affSColin Finck         if( r != ERROR_SUCCESS )
1204c2c66affSColin Finck             return r;
1205c2c66affSColin Finck         break;
1206c2c66affSColin Finck     case EXPR_IVAL:
1207c2c66affSColin Finck         *valid = 1;
1208c2c66affSColin Finck         cond->type = EXPR_UVAL;
1209c2c66affSColin Finck         cond->u.uval = cond->u.ival;
1210c2c66affSColin Finck         break;
1211c2c66affSColin Finck     case EXPR_WILDCARD:
1212c2c66affSColin Finck         *valid = 1;
1213c2c66affSColin Finck         break;
1214c2c66affSColin Finck     case EXPR_SVAL:
1215c2c66affSColin Finck         *valid = 1;
1216c2c66affSColin Finck         break;
1217c2c66affSColin Finck     default:
1218c2c66affSColin Finck         ERR("Invalid expression type\n");
1219c2c66affSColin Finck         *valid = 0;
1220c2c66affSColin Finck         break;
1221c2c66affSColin Finck     }
1222c2c66affSColin Finck 
1223c2c66affSColin Finck     return ERROR_SUCCESS;
1224c2c66affSColin Finck }
1225c2c66affSColin Finck 
WHERE_CreateView(MSIDATABASE * db,MSIVIEW ** view,LPWSTR tables,struct expr * cond)1226c2c66affSColin Finck UINT WHERE_CreateView( MSIDATABASE *db, MSIVIEW **view, LPWSTR tables,
1227c2c66affSColin Finck                        struct expr *cond )
1228c2c66affSColin Finck {
1229c2c66affSColin Finck     MSIWHEREVIEW *wv = NULL;
1230c2c66affSColin Finck     UINT r, valid = 0;
1231c2c66affSColin Finck     WCHAR *ptr;
1232c2c66affSColin Finck 
1233c2c66affSColin Finck     TRACE("(%s)\n", debugstr_w(tables) );
1234c2c66affSColin Finck 
1235*f4be6dc3SMikhail     wv = calloc(1, sizeof *wv);
1236c2c66affSColin Finck     if( !wv )
1237c2c66affSColin Finck         return ERROR_FUNCTION_FAILED;
1238c2c66affSColin Finck 
1239c2c66affSColin Finck     /* fill the structure */
1240c2c66affSColin Finck     wv->view.ops = &where_ops;
1241c2c66affSColin Finck     msiobj_addref( &db->hdr );
1242c2c66affSColin Finck     wv->db = db;
1243c2c66affSColin Finck     wv->cond = cond;
1244c2c66affSColin Finck 
1245c2c66affSColin Finck     while (*tables)
1246c2c66affSColin Finck     {
1247*f4be6dc3SMikhail         struct join_table *table;
1248c2c66affSColin Finck 
1249958f1addSwinesync         if ((ptr = wcschr(tables, ' ')))
1250c2c66affSColin Finck             *ptr = '\0';
1251c2c66affSColin Finck 
1252*f4be6dc3SMikhail         table = malloc(sizeof(*table));
1253c2c66affSColin Finck         if (!table)
1254c2c66affSColin Finck         {
1255c2c66affSColin Finck             r = ERROR_OUTOFMEMORY;
1256c2c66affSColin Finck             goto end;
1257c2c66affSColin Finck         }
1258c2c66affSColin Finck 
1259c2c66affSColin Finck         r = TABLE_CreateView(db, tables, &table->view);
1260c2c66affSColin Finck         if (r != ERROR_SUCCESS)
1261c2c66affSColin Finck         {
1262c2c66affSColin Finck             WARN("can't create table: %s\n", debugstr_w(tables));
1263*f4be6dc3SMikhail             free(table);
1264c2c66affSColin Finck             r = ERROR_BAD_QUERY_SYNTAX;
1265c2c66affSColin Finck             goto end;
1266c2c66affSColin Finck         }
1267c2c66affSColin Finck 
1268c2c66affSColin Finck         r = table->view->ops->get_dimensions(table->view, NULL,
1269c2c66affSColin Finck                                              &table->col_count);
1270c2c66affSColin Finck         if (r != ERROR_SUCCESS)
1271c2c66affSColin Finck         {
1272c2c66affSColin Finck             ERR("can't get table dimensions\n");
1273c2c66affSColin Finck             table->view->ops->delete(table->view);
1274*f4be6dc3SMikhail             free(table);
1275c2c66affSColin Finck             goto end;
1276c2c66affSColin Finck         }
1277c2c66affSColin Finck 
1278c2c66affSColin Finck         wv->col_count += table->col_count;
1279c2c66affSColin Finck         table->table_index = wv->table_count++;
1280c2c66affSColin Finck 
1281c2c66affSColin Finck         table->next = wv->tables;
1282c2c66affSColin Finck         wv->tables = table;
1283c2c66affSColin Finck 
1284c2c66affSColin Finck         if (!ptr)
1285c2c66affSColin Finck             break;
1286c2c66affSColin Finck 
1287c2c66affSColin Finck         tables = ptr + 1;
1288c2c66affSColin Finck     }
1289c2c66affSColin Finck 
1290c2c66affSColin Finck     if( cond )
1291c2c66affSColin Finck     {
1292c2c66affSColin Finck         r = WHERE_VerifyCondition( wv, cond, &valid );
1293c2c66affSColin Finck         if( r != ERROR_SUCCESS )
1294c2c66affSColin Finck             goto end;
1295c2c66affSColin Finck         if( !valid ) {
1296c2c66affSColin Finck             r = ERROR_FUNCTION_FAILED;
1297c2c66affSColin Finck             goto end;
1298c2c66affSColin Finck         }
1299c2c66affSColin Finck     }
1300c2c66affSColin Finck 
1301c2c66affSColin Finck     *view = (MSIVIEW*) wv;
1302c2c66affSColin Finck 
1303c2c66affSColin Finck     return ERROR_SUCCESS;
1304c2c66affSColin Finck end:
1305c2c66affSColin Finck     WHERE_delete(&wv->view);
1306c2c66affSColin Finck 
1307c2c66affSColin Finck     return r;
1308c2c66affSColin Finck }
1309