Authors: Mike McCormack <mike@codeweavers.com>, Aric Stewart <aric@codeweavers.com>
[wine/multimedia.git] / dlls / msi / order.c
blobf73e6a0d273d6231ed3eedb85caac1935e4a5a17
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 <stdarg.h>
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"
34 #include "query.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(msi);
39 /* below is the query interface to a table */
41 typedef struct tagMSIORDERVIEW
43 MSIVIEW view;
44 MSIDATABASE *db;
45 MSIVIEW *table;
46 UINT *reorder;
47 UINT num_cols;
48 UINT cols[1];
49 } MSIORDERVIEW;
51 static UINT ORDER_compare( MSIORDERVIEW *ov, UINT a, UINT b, UINT *swap )
53 UINT r, i, a_val = 0, b_val = 0;
55 *swap = 0;
56 for( i=0; i<ov->num_cols; i++ )
58 r = ov->table->ops->fetch_int( ov->table, a, ov->cols[i], &a_val );
59 if( r != ERROR_SUCCESS )
60 return r;
62 r = ov->table->ops->fetch_int( ov->table, b, ov->cols[i], &b_val );
63 if( r != ERROR_SUCCESS )
64 return r;
66 if( a_val != b_val )
68 if( a_val > b_val )
69 *swap = 1;
70 break;
74 return ERROR_SUCCESS;
77 static UINT ORDER_mergesort( MSIORDERVIEW *ov, UINT left, UINT right )
79 UINT r, centre = (left + right)/2, temp, swap = 0, i, j;
80 UINT *array = ov->reorder;
82 if( left == right )
83 return ERROR_SUCCESS;
85 /* sort the left half */
86 r = ORDER_mergesort( ov, left, centre );
87 if( r != ERROR_SUCCESS )
88 return r;
90 /* sort the right half */
91 r = ORDER_mergesort( ov, centre+1, right );
92 if( r != ERROR_SUCCESS )
93 return r;
95 for( i=left, j=centre+1; (i<=centre) && (j<=right); i++ )
97 r = ORDER_compare( ov, array[i], array[j], &swap );
98 if( r != ERROR_SUCCESS )
99 return r;
100 if( swap )
102 temp = array[j];
103 memmove( &array[i+1], &array[i], (j-i)*sizeof (UINT) );
104 array[i] = temp;
105 j++;
106 centre++;
110 return ERROR_SUCCESS;
113 static UINT ORDER_verify( MSIORDERVIEW *ov, UINT num_rows )
115 UINT i, swap, r;
117 for( i=1; i<num_rows; i++ )
119 r = ORDER_compare( ov, ov->reorder[i-1], ov->reorder[i], &swap );
120 if( r != ERROR_SUCCESS )
121 return r;
122 if( !swap )
123 continue;
124 ERR("Bad order! %d\n", i);
125 return ERROR_FUNCTION_FAILED;
128 return ERROR_SUCCESS;
131 static UINT ORDER_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
133 MSIORDERVIEW *ov = (MSIORDERVIEW*)view;
135 TRACE("%p %d %d %p\n", ov, row, col, val );
137 if( !ov->table )
138 return ERROR_FUNCTION_FAILED;
140 row = ov->reorder[ row ];
142 return ov->table->ops->fetch_int( ov->table, row, col, val );
145 static UINT ORDER_execute( struct tagMSIVIEW *view, MSIRECORD *record )
147 MSIORDERVIEW *ov = (MSIORDERVIEW*)view;
148 UINT r, num_rows = 0, i;
150 TRACE("%p %p\n", ov, record);
152 if( !ov->table )
153 return ERROR_FUNCTION_FAILED;
155 r = ov->table->ops->execute( ov->table, record );
156 if( r != ERROR_SUCCESS )
157 return r;
159 r = ov->table->ops->get_dimensions( ov->table, &num_rows, NULL );
160 if( r != ERROR_SUCCESS )
161 return r;
163 ov->reorder = HeapAlloc( GetProcessHeap(), 0, num_rows*sizeof(UINT) );
164 if( !ov->reorder )
165 return ERROR_FUNCTION_FAILED;
167 for( i=0; i<num_rows; i++ )
168 ov->reorder[i] = i;
170 r = ORDER_mergesort( ov, 0, num_rows - 1 );
171 if( r != ERROR_SUCCESS )
172 return r;
174 r = ORDER_verify( ov, num_rows );
175 if( r != ERROR_SUCCESS )
176 return r;
178 return ERROR_SUCCESS;
181 static UINT ORDER_close( struct tagMSIVIEW *view )
183 MSIORDERVIEW *ov = (MSIORDERVIEW*)view;
185 TRACE("%p\n", ov );
187 if( !ov->table )
188 return ERROR_FUNCTION_FAILED;
190 if( ov->reorder )
191 HeapFree( GetProcessHeap(), 0, ov->reorder );
192 ov->reorder = NULL;
194 return ov->table->ops->close( ov->table );
197 static UINT ORDER_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols )
199 MSIORDERVIEW *ov = (MSIORDERVIEW*)view;
201 TRACE("%p %p %p\n", ov, rows, cols );
203 if( !ov->table )
204 return ERROR_FUNCTION_FAILED;
206 return ov->table->ops->get_dimensions( ov->table, rows, cols );
209 static UINT ORDER_get_column_info( struct tagMSIVIEW *view,
210 UINT n, LPWSTR *name, UINT *type )
212 MSIORDERVIEW *ov = (MSIORDERVIEW*)view;
214 TRACE("%p %d %p %p\n", ov, n, name, type );
216 if( !ov->table )
217 return ERROR_FUNCTION_FAILED;
219 return ov->table->ops->get_column_info( ov->table, n, name, type );
222 static UINT ORDER_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode, MSIHANDLE hrec)
224 MSIORDERVIEW *ov = (MSIORDERVIEW*)view;
226 TRACE("%p %d %ld\n", ov, eModifyMode, hrec );
228 if( !ov->table )
229 return ERROR_FUNCTION_FAILED;
231 return ov->table->ops->modify( ov->table, eModifyMode, hrec );
234 static UINT ORDER_delete( struct tagMSIVIEW *view )
236 MSIORDERVIEW *ov = (MSIORDERVIEW*)view;
238 TRACE("%p\n", ov );
240 if( ov->table )
241 ov->table->ops->delete( ov->table );
243 if( ov->reorder )
244 HeapFree( GetProcessHeap(), 0, ov->reorder );
245 ov->reorder = NULL;
247 HeapFree( GetProcessHeap(), 0, ov );
248 msiobj_release( &ov->db->hdr );
250 return ERROR_SUCCESS;
254 MSIVIEWOPS order_ops =
256 ORDER_fetch_int,
257 NULL,
258 NULL,
259 NULL,
260 ORDER_execute,
261 ORDER_close,
262 ORDER_get_dimensions,
263 ORDER_get_column_info,
264 ORDER_modify,
265 ORDER_delete
268 UINT ORDER_CreateView( MSIDATABASE *db, MSIVIEW **view, MSIVIEW *table )
270 MSIORDERVIEW *ov = NULL;
271 UINT count = 0, r;
273 TRACE("%p\n", ov );
275 r = table->ops->get_dimensions( table, NULL, &count );
276 if( r != ERROR_SUCCESS )
278 ERR("can't get table dimensions\n");
279 return r;
282 ov = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
283 sizeof *ov + sizeof (UINT) * count );
284 if( !ov )
285 return ERROR_FUNCTION_FAILED;
287 /* fill the structure */
288 ov->view.ops = &order_ops;
289 msiobj_addref( &db->hdr );
290 ov->db = db;
291 ov->table = table;
292 ov->reorder = NULL;
293 ov->num_cols = 0;
294 *view = (MSIVIEW*) ov;
296 return ERROR_SUCCESS;
299 UINT ORDER_AddColumn( MSIVIEW *view, LPWSTR name )
301 MSIORDERVIEW *ov = (MSIORDERVIEW*)view;
302 UINT n, count, r;
303 MSIVIEW *table;
305 TRACE("%p adding %s\n", ov, debugstr_w( name ) );
307 if( ov->view.ops != &order_ops )
308 return ERROR_FUNCTION_FAILED;
310 table = ov->table;
311 if( !table )
312 return ERROR_FUNCTION_FAILED;
313 if( !table->ops->get_dimensions )
314 return ERROR_FUNCTION_FAILED;
315 if( !table->ops->get_column_info )
316 return ERROR_FUNCTION_FAILED;
318 r = table->ops->get_dimensions( table, NULL, &count );
319 if( r != ERROR_SUCCESS )
320 return r;
322 if( ov->num_cols >= count )
323 return ERROR_FUNCTION_FAILED;
325 r = VIEW_find_column( table, name, &n );
326 if( r != ERROR_SUCCESS )
327 return r;
329 ov->cols[ov->num_cols] = n;
330 TRACE("Ordering by column %s (%d)\n", debugstr_w( name ), n);
332 ov->num_cols++;
334 return ERROR_SUCCESS;