Partial implementation of the Microsoft Installer (msi.dll).
[wine/hacks.git] / dlls / msi / distinct.c
blobaaa140958bd3d6b7b5648e39d147433d07786979
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2002 Mike McCormack for Codeweavers
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.
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.
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
21 #include "windef.h"
22 #include "winbase.h"
23 #include "winerror.h"
24 #include "wine/debug.h"
25 #include "msi.h"
26 #include "msiquery.h"
27 #include "objbase.h"
28 #include "objidl.h"
29 #include "msipriv.h"
30 #include "winnls.h"
32 #include "query.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(msi);
36 typedef struct tagDISTINCTSET
38 UINT val;
39 UINT count;
40 UINT row;
41 struct tagDISTINCTSET *nextrow;
42 struct tagDISTINCTSET *nextcol;
43 } DISTINCTSET;
45 typedef struct tagMSIDISTINCTVIEW
47 MSIVIEW view;
48 MSIDATABASE *db;
49 MSIVIEW *table;
50 UINT row_count;
51 UINT *translation;
52 } MSIDISTINCTVIEW;
54 static DISTINCTSET ** distinct_insert( DISTINCTSET **x, UINT val, UINT row )
56 /* horrible O(n) find */
57 while( *x )
59 if( (*x)->val == val )
61 (*x)->count++;
62 return x;
64 x = &(*x)->nextrow;
67 /* nothing found, so add one */
68 *x = HeapAlloc( GetProcessHeap(), 0, sizeof (DISTINCTSET) );
69 if( *x )
71 (*x)->val = val;
72 (*x)->count = 1;
73 (*x)->row = row;
74 (*x)->nextrow = NULL;
75 (*x)->nextcol = NULL;
77 return x;
80 static void distinct_free( DISTINCTSET *x )
82 while( x )
84 DISTINCTSET *next = x->nextrow;
85 distinct_free( x->nextcol );
86 HeapFree( GetProcessHeap(), 0, x );
87 x = next;
91 static UINT DISTINCT_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
93 MSIDISTINCTVIEW *dv = (MSIDISTINCTVIEW*)view;
95 TRACE("%p %d %d %p\n", dv, row, col, val );
97 if( !dv->table )
98 return ERROR_FUNCTION_FAILED;
100 if( row >= dv->row_count )
101 return ERROR_INVALID_PARAMETER;
103 row = dv->translation[ row ];
105 return dv->table->ops->fetch_int( dv->table, row, col, val );
108 static UINT DISTINCT_execute( struct tagMSIVIEW *view, MSIHANDLE record )
110 MSIDISTINCTVIEW *dv = (MSIDISTINCTVIEW*)view;
111 UINT r, i, j, r_count, c_count;
112 DISTINCTSET *rowset = NULL;
114 TRACE("%p %ld\n", dv, record);
116 if( !dv->table )
117 return ERROR_FUNCTION_FAILED;
119 r = dv->table->ops->execute( dv->table, record );
120 if( r != ERROR_SUCCESS )
121 return r;
123 r = dv->table->ops->get_dimensions( dv->table, &r_count, &c_count );
124 if( r != ERROR_SUCCESS )
125 return r;
127 dv->translation = HeapAlloc( GetProcessHeap(), 0, r_count*sizeof(UINT) );
128 if( !dv->translation )
129 return ERROR_FUNCTION_FAILED;
131 /* build it */
132 for( i=0; i<r_count; i++ )
134 DISTINCTSET **x = &rowset;
136 for( j=1; j<=c_count; j++ )
138 UINT val = 0;
139 r = dv->table->ops->fetch_int( dv->table, i, j, &val );
140 if( r != ERROR_SUCCESS )
142 ERR("Failed to fetch int at %d %d\n", i, j );
143 distinct_free( rowset );
144 return r;
146 x = distinct_insert( x, val, i );
147 if( !*x )
149 ERR("Failed to insert at %d %d\n", i, j );
150 distinct_free( rowset );
151 return ERROR_FUNCTION_FAILED;
153 if( j != c_count )
154 x = &(*x)->nextcol;
157 /* check if it was distinct and if so, include it */
158 if( (*x)->row == i )
160 TRACE("Row %d -> %d\n", dv->row_count, i);
161 dv->translation[dv->row_count++] = i;
165 distinct_free( rowset );
167 return ERROR_SUCCESS;
170 static UINT DISTINCT_close( struct tagMSIVIEW *view )
172 MSIDISTINCTVIEW *dv = (MSIDISTINCTVIEW*)view;
174 TRACE("%p\n", dv );
176 if( !dv->table )
177 return ERROR_FUNCTION_FAILED;
179 if( dv->translation )
180 HeapFree( GetProcessHeap(), 0, dv->translation );
181 dv->translation = NULL;
182 dv->row_count = 0;
184 return dv->table->ops->close( dv->table );
187 static UINT DISTINCT_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols )
189 MSIDISTINCTVIEW *dv = (MSIDISTINCTVIEW*)view;
191 TRACE("%p %p %p\n", dv, rows, cols );
193 if( !dv->table )
194 return ERROR_FUNCTION_FAILED;
196 if( rows )
198 if( !dv->translation )
199 return ERROR_FUNCTION_FAILED;
200 *rows = dv->row_count;
203 return dv->table->ops->get_dimensions( dv->table, NULL, cols );
206 static UINT DISTINCT_get_column_info( struct tagMSIVIEW *view,
207 UINT n, LPWSTR *name, UINT *type )
209 MSIDISTINCTVIEW *dv = (MSIDISTINCTVIEW*)view;
211 TRACE("%p %d %p %p\n", dv, n, name, type );
213 if( !dv->table )
214 return ERROR_FUNCTION_FAILED;
216 return dv->table->ops->get_column_info( dv->table, n, name, type );
219 static UINT DISTINCT_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode, MSIHANDLE hrec)
221 MSIDISTINCTVIEW *dv = (MSIDISTINCTVIEW*)view;
223 TRACE("%p %d %ld\n", dv, eModifyMode, hrec );
225 if( !dv->table )
226 return ERROR_FUNCTION_FAILED;
228 return dv->table->ops->modify( dv->table, eModifyMode, hrec );
231 static UINT DISTINCT_delete( struct tagMSIVIEW *view )
233 MSIDISTINCTVIEW *dv = (MSIDISTINCTVIEW*)view;
235 TRACE("%p\n", dv );
237 if( dv->table )
238 dv->table->ops->delete( dv->table );
240 if( dv->translation )
241 HeapFree( GetProcessHeap(), 0, dv->translation );
242 HeapFree( GetProcessHeap(), 0, dv );
244 return ERROR_SUCCESS;
248 MSIVIEWOPS distinct_ops =
250 DISTINCT_fetch_int,
251 DISTINCT_execute,
252 DISTINCT_close,
253 DISTINCT_get_dimensions,
254 DISTINCT_get_column_info,
255 DISTINCT_modify,
256 DISTINCT_delete
259 UINT DISTINCT_CreateView( MSIDATABASE *db, MSIVIEW **view, MSIVIEW *table )
261 MSIDISTINCTVIEW *dv = NULL;
262 UINT count = 0, r;
264 TRACE("%p\n", dv );
266 r = table->ops->get_dimensions( table, NULL, &count );
267 if( r != ERROR_SUCCESS )
269 ERR("can't get table dimensions\n");
270 return r;
273 dv = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof *dv );
274 if( !dv )
275 return ERROR_FUNCTION_FAILED;
277 /* fill the structure */
278 dv->view.ops = &distinct_ops;
279 dv->db = db;
280 dv->table = table;
281 dv->translation = NULL;
282 dv->row_count = 0;
283 *view = (MSIVIEW*) dv;
285 return ERROR_SUCCESS;