[MSI]
[reactos.git] / reactos / dll / win32 / 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #define WIN32_NO_STATUS
22 #define _INC_WINDOWS
23 #define COM_NO_WINDOWS_H
24
25 //#include <stdarg.h>
26
27 //#include "windef.h"
28 //#include "winbase.h"
29 //#include "winerror.h"
30 #include <wine/debug.h>
31 //#include "msi.h"
32 //#include "msiquery.h"
33 //#include "objbase.h"
34 //#include "objidl.h"
35 #include "msipriv.h"
36 //#include "winnls.h"
37
38 //#include "query.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
41
42 typedef struct tagDISTINCTSET
43 {
44 UINT val;
45 UINT count;
46 UINT row;
47 struct tagDISTINCTSET *nextrow;
48 struct tagDISTINCTSET *nextcol;
49 } DISTINCTSET;
50
51 typedef struct tagMSIDISTINCTVIEW
52 {
53 MSIVIEW view;
54 MSIDATABASE *db;
55 MSIVIEW *table;
56 UINT row_count;
57 UINT *translation;
58 } MSIDISTINCTVIEW;
59
60 static DISTINCTSET ** distinct_insert( DISTINCTSET **x, UINT val, UINT row )
61 {
62 /* horrible O(n) find */
63 while( *x )
64 {
65 if( (*x)->val == val )
66 {
67 (*x)->count++;
68 return x;
69 }
70 x = &(*x)->nextrow;
71 }
72
73 /* nothing found, so add one */
74 *x = msi_alloc( sizeof (DISTINCTSET) );
75 if( *x )
76 {
77 (*x)->val = val;
78 (*x)->count = 1;
79 (*x)->row = row;
80 (*x)->nextrow = NULL;
81 (*x)->nextcol = NULL;
82 }
83 return x;
84 }
85
86 static void distinct_free( DISTINCTSET *x )
87 {
88 while( x )
89 {
90 DISTINCTSET *next = x->nextrow;
91 distinct_free( x->nextcol );
92 msi_free( x );
93 x = next;
94 }
95 }
96
97 static UINT DISTINCT_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
98 {
99 MSIDISTINCTVIEW *dv = (MSIDISTINCTVIEW*)view;
100
101 TRACE("%p %d %d %p\n", dv, row, col, val );
102
103 if( !dv->table )
104 return ERROR_FUNCTION_FAILED;
105
106 if( row >= dv->row_count )
107 return ERROR_INVALID_PARAMETER;
108
109 row = dv->translation[ row ];
110
111 return dv->table->ops->fetch_int( dv->table, row, col, val );
112 }
113
114 static UINT DISTINCT_execute( struct tagMSIVIEW *view, MSIRECORD *record )
115 {
116 MSIDISTINCTVIEW *dv = (MSIDISTINCTVIEW*)view;
117 UINT r, i, j, r_count, c_count;
118 DISTINCTSET *rowset = NULL;
119
120 TRACE("%p %p\n", dv, record);
121
122 if( !dv->table )
123 return ERROR_FUNCTION_FAILED;
124
125 r = dv->table->ops->execute( dv->table, record );
126 if( r != ERROR_SUCCESS )
127 return r;
128
129 r = dv->table->ops->get_dimensions( dv->table, &r_count, &c_count );
130 if( r != ERROR_SUCCESS )
131 return r;
132
133 dv->translation = msi_alloc( r_count*sizeof(UINT) );
134 if( !dv->translation )
135 return ERROR_FUNCTION_FAILED;
136
137 /* build it */
138 for( i=0; i<r_count; i++ )
139 {
140 DISTINCTSET **x = &rowset;
141
142 for( j=1; j<=c_count; j++ )
143 {
144 UINT val = 0;
145 r = dv->table->ops->fetch_int( dv->table, i, j, &val );
146 if( r != ERROR_SUCCESS )
147 {
148 ERR("Failed to fetch int at %d %d\n", i, j );
149 distinct_free( rowset );
150 return r;
151 }
152 x = distinct_insert( x, val, i );
153 if( !*x )
154 {
155 ERR("Failed to insert at %d %d\n", i, j );
156 distinct_free( rowset );
157 return ERROR_FUNCTION_FAILED;
158 }
159 if( j != c_count )
160 x = &(*x)->nextcol;
161 }
162
163 /* check if it was distinct and if so, include it */
164 if( (*x)->row == i )
165 {
166 TRACE("Row %d -> %d\n", dv->row_count, i);
167 dv->translation[dv->row_count++] = i;
168 }
169 }
170
171 distinct_free( rowset );
172
173 return ERROR_SUCCESS;
174 }
175
176 static UINT DISTINCT_close( struct tagMSIVIEW *view )
177 {
178 MSIDISTINCTVIEW *dv = (MSIDISTINCTVIEW*)view;
179
180 TRACE("%p\n", dv );
181
182 if( !dv->table )
183 return ERROR_FUNCTION_FAILED;
184
185 msi_free( dv->translation );
186 dv->translation = NULL;
187 dv->row_count = 0;
188
189 return dv->table->ops->close( dv->table );
190 }
191
192 static UINT DISTINCT_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols )
193 {
194 MSIDISTINCTVIEW *dv = (MSIDISTINCTVIEW*)view;
195
196 TRACE("%p %p %p\n", dv, rows, cols );
197
198 if( !dv->table )
199 return ERROR_FUNCTION_FAILED;
200
201 if( rows )
202 {
203 if( !dv->translation )
204 return ERROR_FUNCTION_FAILED;
205 *rows = dv->row_count;
206 }
207
208 return dv->table->ops->get_dimensions( dv->table, NULL, cols );
209 }
210
211 static UINT DISTINCT_get_column_info( struct tagMSIVIEW *view, UINT n, LPCWSTR *name,
212 UINT *type, BOOL *temporary, LPCWSTR *table_name )
213 {
214 MSIDISTINCTVIEW *dv = (MSIDISTINCTVIEW*)view;
215
216 TRACE("%p %d %p %p %p %p\n", dv, n, name, type, temporary, table_name );
217
218 if( !dv->table )
219 return ERROR_FUNCTION_FAILED;
220
221 return dv->table->ops->get_column_info( dv->table, n, name,
222 type, temporary, table_name );
223 }
224
225 static UINT DISTINCT_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
226 MSIRECORD *rec, UINT row )
227 {
228 MSIDISTINCTVIEW *dv = (MSIDISTINCTVIEW*)view;
229
230 TRACE("%p %d %p\n", dv, eModifyMode, rec );
231
232 if( !dv->table )
233 return ERROR_FUNCTION_FAILED;
234
235 return dv->table->ops->modify( dv->table, eModifyMode, rec, row );
236 }
237
238 static UINT DISTINCT_delete( struct tagMSIVIEW *view )
239 {
240 MSIDISTINCTVIEW *dv = (MSIDISTINCTVIEW*)view;
241
242 TRACE("%p\n", dv );
243
244 if( dv->table )
245 dv->table->ops->delete( dv->table );
246
247 msi_free( dv->translation );
248 msiobj_release( &dv->db->hdr );
249 msi_free( dv );
250
251 return ERROR_SUCCESS;
252 }
253
254 static UINT DISTINCT_find_matching_rows( struct tagMSIVIEW *view, UINT col,
255 UINT val, UINT *row, MSIITERHANDLE *handle )
256 {
257 MSIDISTINCTVIEW *dv = (MSIDISTINCTVIEW*)view;
258 UINT r;
259
260 TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
261
262 if( !dv->table )
263 return ERROR_FUNCTION_FAILED;
264
265 r = dv->table->ops->find_matching_rows( dv->table, col, val, row, handle );
266
267 if( *row > dv->row_count )
268 return ERROR_NO_MORE_ITEMS;
269
270 *row = dv->translation[ *row ];
271
272 return r;
273 }
274
275 static const MSIVIEWOPS distinct_ops =
276 {
277 DISTINCT_fetch_int,
278 NULL,
279 NULL,
280 NULL,
281 NULL,
282 NULL,
283 DISTINCT_execute,
284 DISTINCT_close,
285 DISTINCT_get_dimensions,
286 DISTINCT_get_column_info,
287 DISTINCT_modify,
288 DISTINCT_delete,
289 DISTINCT_find_matching_rows,
290 NULL,
291 NULL,
292 NULL,
293 NULL,
294 NULL,
295 NULL,
296 };
297
298 UINT DISTINCT_CreateView( MSIDATABASE *db, MSIVIEW **view, MSIVIEW *table )
299 {
300 MSIDISTINCTVIEW *dv = NULL;
301 UINT count = 0, r;
302
303 TRACE("%p\n", dv );
304
305 r = table->ops->get_dimensions( table, NULL, &count );
306 if( r != ERROR_SUCCESS )
307 {
308 ERR("can't get table dimensions\n");
309 return r;
310 }
311
312 dv = msi_alloc_zero( sizeof *dv );
313 if( !dv )
314 return ERROR_FUNCTION_FAILED;
315
316 /* fill the structure */
317 dv->view.ops = &distinct_ops;
318 msiobj_addref( &db->hdr );
319 dv->db = db;
320 dv->table = table;
321 dv->translation = NULL;
322 dv->row_count = 0;
323 *view = (MSIVIEW*) dv;
324
325 return ERROR_SUCCESS;
326 }