msi: Optimise WHERE operations.
[wine] / dlls / msi / distinct.c
1 /*
2  * Implementation of the Microsoft Installer (msi.dll)
3  *
4  * Copyright 2002 Mike McCormack for CodeWeavers
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <stdarg.h>
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winerror.h"
26 #include "wine/debug.h"
27 #include "msi.h"
28 #include "msiquery.h"
29 #include "objbase.h"
30 #include "objidl.h"
31 #include "msipriv.h"
32 #include "winnls.h"
33
34 #include "query.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
37
38 typedef struct tagDISTINCTSET
39 {
40     UINT val;
41     UINT count;
42     UINT row;
43     struct tagDISTINCTSET *nextrow;
44     struct tagDISTINCTSET *nextcol;
45 } DISTINCTSET;
46
47 typedef struct tagMSIDISTINCTVIEW
48 {
49     MSIVIEW        view;
50     MSIDATABASE   *db;
51     MSIVIEW       *table;
52     UINT           row_count;
53     UINT          *translation;
54 } MSIDISTINCTVIEW;
55
56 static DISTINCTSET ** distinct_insert( DISTINCTSET **x, UINT val, UINT row )
57 {
58     /* horrible O(n) find */
59     while( *x )
60     {
61         if( (*x)->val == val )
62         {
63             (*x)->count++;
64             return x;
65         }
66         x = &(*x)->nextrow;
67     }
68
69     /* nothing found, so add one */
70     *x = msi_alloc( sizeof (DISTINCTSET) );
71     if( *x )
72     {
73         (*x)->val = val;
74         (*x)->count = 1;
75         (*x)->row = row;
76         (*x)->nextrow = NULL;
77         (*x)->nextcol = NULL;
78     }
79     return x;
80 }
81
82 static void distinct_free( DISTINCTSET *x )
83 {
84     while( x )
85     {
86         DISTINCTSET *next = x->nextrow;
87         distinct_free( x->nextcol );
88         msi_free( x );
89         x = next;
90     }
91 }
92
93 static UINT DISTINCT_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
94 {
95     MSIDISTINCTVIEW *dv = (MSIDISTINCTVIEW*)view;
96
97     TRACE("%p %d %d %p\n", dv, row, col, val );
98
99     if( !dv->table )
100         return ERROR_FUNCTION_FAILED;
101
102     if( row >= dv->row_count )
103         return ERROR_INVALID_PARAMETER;
104
105     row = dv->translation[ row ];
106
107     return dv->table->ops->fetch_int( dv->table, row, col, val );
108 }
109
110 static UINT DISTINCT_execute( struct tagMSIVIEW *view, MSIRECORD *record )
111 {
112     MSIDISTINCTVIEW *dv = (MSIDISTINCTVIEW*)view;
113     UINT r, i, j, r_count, c_count;
114     DISTINCTSET *rowset = NULL;
115
116     TRACE("%p %p\n", dv, record);
117
118     if( !dv->table )
119          return ERROR_FUNCTION_FAILED;
120
121     r = dv->table->ops->execute( dv->table, record );
122     if( r != ERROR_SUCCESS )
123         return r;
124
125     r = dv->table->ops->get_dimensions( dv->table, &r_count, &c_count );
126     if( r != ERROR_SUCCESS )
127         return r;
128
129     dv->translation = msi_alloc( r_count*sizeof(UINT) );
130     if( !dv->translation )
131         return ERROR_FUNCTION_FAILED;
132
133     /* build it */
134     for( i=0; i<r_count; i++ )
135     {
136         DISTINCTSET **x = &rowset;
137
138         for( j=1; j<=c_count; j++ )
139         {
140             UINT val = 0;
141             r = dv->table->ops->fetch_int( dv->table, i, j, &val );
142             if( r != ERROR_SUCCESS )
143             {
144                 ERR("Failed to fetch int at %d %d\n", i, j );
145                 distinct_free( rowset );
146                 return r;
147             }
148             x = distinct_insert( x, val, i );
149             if( !*x )
150             {
151                 ERR("Failed to insert at %d %d\n", i, j );
152                 distinct_free( rowset );
153                 return ERROR_FUNCTION_FAILED;
154             }
155             if( j != c_count )
156                 x = &(*x)->nextcol;
157         }
158
159         /* check if it was distinct and if so, include it */
160         if( (*x)->row == i )
161         {
162             TRACE("Row %d -> %d\n", dv->row_count, i);
163             dv->translation[dv->row_count++] = i;
164         }
165     }
166
167     distinct_free( rowset );
168
169     return ERROR_SUCCESS;
170 }
171
172 static UINT DISTINCT_close( struct tagMSIVIEW *view )
173 {
174     MSIDISTINCTVIEW *dv = (MSIDISTINCTVIEW*)view;
175
176     TRACE("%p\n", dv );
177
178     if( !dv->table )
179          return ERROR_FUNCTION_FAILED;
180
181     msi_free( dv->translation );
182     dv->translation = NULL;
183     dv->row_count = 0;
184
185     return dv->table->ops->close( dv->table );
186 }
187
188 static UINT DISTINCT_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols )
189 {
190     MSIDISTINCTVIEW *dv = (MSIDISTINCTVIEW*)view;
191
192     TRACE("%p %p %p\n", dv, rows, cols );
193
194     if( !dv->table )
195         return ERROR_FUNCTION_FAILED;
196
197     if( rows )
198     {
199         if( !dv->translation )
200             return ERROR_FUNCTION_FAILED;
201         *rows = dv->row_count;
202     }
203
204     return dv->table->ops->get_dimensions( dv->table, NULL, cols );
205 }
206
207 static UINT DISTINCT_get_column_info( struct tagMSIVIEW *view,
208                 UINT n, LPWSTR *name, UINT *type )
209 {
210     MSIDISTINCTVIEW *dv = (MSIDISTINCTVIEW*)view;
211
212     TRACE("%p %d %p %p\n", dv, n, name, type );
213
214     if( !dv->table )
215          return ERROR_FUNCTION_FAILED;
216
217     return dv->table->ops->get_column_info( dv->table, n, name, type );
218 }
219
220 static UINT DISTINCT_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
221                 MSIRECORD *rec )
222 {
223     MSIDISTINCTVIEW *dv = (MSIDISTINCTVIEW*)view;
224
225     TRACE("%p %d %p\n", dv, eModifyMode, rec );
226
227     if( !dv->table )
228          return ERROR_FUNCTION_FAILED;
229
230     return dv->table->ops->modify( dv->table, eModifyMode, rec );
231 }
232
233 static UINT DISTINCT_delete( struct tagMSIVIEW *view )
234 {
235     MSIDISTINCTVIEW *dv = (MSIDISTINCTVIEW*)view;
236
237     TRACE("%p\n", dv );
238
239     if( dv->table )
240         dv->table->ops->delete( dv->table );
241
242     msi_free( dv->translation );
243     msiobj_release( &dv->db->hdr );
244     msi_free( dv );
245
246     return ERROR_SUCCESS;
247 }
248
249 static UINT DISTINCT_find_matching_rows( struct tagMSIVIEW *view, UINT col,
250     UINT val, UINT *row, MSIITERHANDLE *handle )
251 {
252     MSIDISTINCTVIEW *dv = (MSIDISTINCTVIEW*)view;
253     UINT r;
254
255     TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
256
257     if( !dv->table )
258          return ERROR_FUNCTION_FAILED;
259
260     r = dv->table->ops->find_matching_rows( dv->table, col, val, row, handle );
261
262     if( *row > dv->row_count )
263         return ERROR_NO_MORE_ITEMS;
264
265     *row = dv->translation[ *row ];
266
267     return r;
268 }
269
270
271 MSIVIEWOPS distinct_ops =
272 {
273     DISTINCT_fetch_int,
274     NULL,
275     NULL,
276     NULL,
277     DISTINCT_execute,
278     DISTINCT_close,
279     DISTINCT_get_dimensions,
280     DISTINCT_get_column_info,
281     DISTINCT_modify,
282     DISTINCT_delete,
283     DISTINCT_find_matching_rows,
284 };
285
286 UINT DISTINCT_CreateView( MSIDATABASE *db, MSIVIEW **view, MSIVIEW *table )
287 {
288     MSIDISTINCTVIEW *dv = NULL;
289     UINT count = 0, r;
290
291     TRACE("%p\n", dv );
292
293     r = table->ops->get_dimensions( table, NULL, &count );
294     if( r != ERROR_SUCCESS )
295     {
296         ERR("can't get table dimensions\n");
297         return r;
298     }
299
300     dv = msi_alloc_zero( sizeof *dv );
301     if( !dv )
302         return ERROR_FUNCTION_FAILED;
303     
304     /* fill the structure */
305     dv->view.ops = &distinct_ops;
306     msiobj_addref( &db->hdr );
307     dv->db = db;
308     dv->table = table;
309     dv->translation = NULL;
310     dv->row_count = 0;
311     *view = (MSIVIEW*) dv;
312
313     return ERROR_SUCCESS;
314 }