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
24 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(msi);
37 /* below is the query interface to a table */
39 typedef struct tagMSIORDERVIEW
49 static UINT ORDER_compare( MSIORDERVIEW *ov, UINT a, UINT b, UINT *swap )
51 UINT r, i, a_val = 0, b_val = 0;
54 for( i=0; i<ov->num_cols; i++ )
56 r = ov->table->ops->fetch_int( ov->table, a, ov->cols[i], &a_val );
57 if( r != ERROR_SUCCESS )
60 r = ov->table->ops->fetch_int( ov->table, b, ov->cols[i], &b_val );
61 if( r != ERROR_SUCCESS )
75 static UINT ORDER_mergesort( MSIORDERVIEW *ov, UINT left, UINT right )
77 UINT r, centre = (left + right)/2, temp, swap = 0, i, j;
78 UINT *array = ov->reorder;
83 /* sort the left half */
84 r = ORDER_mergesort( ov, left, centre );
85 if( r != ERROR_SUCCESS )
88 /* sort the right half */
89 r = ORDER_mergesort( ov, centre+1, right );
90 if( r != ERROR_SUCCESS )
93 for( i=left, j=centre+1; (i<=centre) && (j<=right); i++ )
95 r = ORDER_compare( ov, array[i], array[j], &swap );
96 if( r != ERROR_SUCCESS )
101 memmove( &array[i+1], &array[i], (j-i)*sizeof (UINT) );
108 return ERROR_SUCCESS;
111 static UINT ORDER_verify( MSIORDERVIEW *ov, UINT num_rows )
115 for( i=1; i<num_rows; i++ )
117 r = ORDER_compare( ov, ov->reorder[i-1], ov->reorder[i], &swap );
118 if( r != ERROR_SUCCESS )
122 ERR("Bad order! %d\n", i);
123 return ERROR_FUNCTION_FAILED;
126 return ERROR_SUCCESS;
129 static UINT ORDER_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
131 MSIORDERVIEW *ov = (MSIORDERVIEW*)view;
133 TRACE("%p %d %d %p\n", ov, row, col, val );
136 return ERROR_FUNCTION_FAILED;
138 row = ov->reorder[ row ];
140 return ov->table->ops->fetch_int( ov->table, row, col, val );
143 static UINT ORDER_execute( struct tagMSIVIEW *view, MSIHANDLE record )
145 MSIORDERVIEW *ov = (MSIORDERVIEW*)view;
146 UINT r, num_rows = 0, i;
148 TRACE("%p %ld\n", ov, record);
151 return ERROR_FUNCTION_FAILED;
153 r = ov->table->ops->execute( ov->table, record );
154 if( r != ERROR_SUCCESS )
157 r = ov->table->ops->get_dimensions( ov->table, &num_rows, NULL );
158 if( r != ERROR_SUCCESS )
161 ov->reorder = HeapAlloc( GetProcessHeap(), 0, num_rows*sizeof(UINT) );
163 return ERROR_FUNCTION_FAILED;
165 for( i=0; i<num_rows; i++ )
168 r = ORDER_mergesort( ov, 0, num_rows - 1 );
169 if( r != ERROR_SUCCESS )
172 r = ORDER_verify( ov, num_rows );
173 if( r != ERROR_SUCCESS )
176 return ERROR_SUCCESS;
179 static UINT ORDER_close( struct tagMSIVIEW *view )
181 MSIORDERVIEW *ov = (MSIORDERVIEW*)view;
186 return ERROR_FUNCTION_FAILED;
189 HeapFree( GetProcessHeap(), 0, ov->reorder );
192 return ov->table->ops->close( ov->table );
195 static UINT ORDER_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols )
197 MSIORDERVIEW *ov = (MSIORDERVIEW*)view;
199 TRACE("%p %p %p\n", ov, rows, cols );
202 return ERROR_FUNCTION_FAILED;
204 return ov->table->ops->get_dimensions( ov->table, rows, cols );
207 static UINT ORDER_get_column_info( struct tagMSIVIEW *view,
208 UINT n, LPWSTR *name, UINT *type )
210 MSIORDERVIEW *ov = (MSIORDERVIEW*)view;
212 TRACE("%p %d %p %p\n", ov, n, name, type );
215 return ERROR_FUNCTION_FAILED;
217 return ov->table->ops->get_column_info( ov->table, n, name, type );
220 static UINT ORDER_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode, MSIHANDLE hrec)
222 MSIORDERVIEW *ov = (MSIORDERVIEW*)view;
224 TRACE("%p %d %ld\n", ov, eModifyMode, hrec );
227 return ERROR_FUNCTION_FAILED;
229 return ov->table->ops->modify( ov->table, eModifyMode, hrec );
232 static UINT ORDER_delete( struct tagMSIVIEW *view )
234 MSIORDERVIEW *ov = (MSIORDERVIEW*)view;
239 ov->table->ops->delete( ov->table );
242 HeapFree( GetProcessHeap(), 0, ov->reorder );
245 HeapFree( GetProcessHeap(), 0, ov );
247 return ERROR_SUCCESS;
251 MSIVIEWOPS order_ops =
256 ORDER_get_dimensions,
257 ORDER_get_column_info,
262 UINT ORDER_CreateView( MSIDATABASE *db, MSIVIEW **view, MSIVIEW *table )
264 MSIORDERVIEW *ov = NULL;
269 r = table->ops->get_dimensions( table, NULL, &count );
270 if( r != ERROR_SUCCESS )
272 ERR("can't get table dimensions\n");
276 ov = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
277 sizeof *ov + sizeof (UINT) * count );
279 return ERROR_FUNCTION_FAILED;
281 /* fill the structure */
282 ov->view.ops = &order_ops;
287 *view = (MSIVIEW*) ov;
289 return ERROR_SUCCESS;
292 UINT ORDER_AddColumn( MSIVIEW *view, LPWSTR name )
294 MSIORDERVIEW *ov = (MSIORDERVIEW*)view;
298 TRACE("%p adding %s\n", ov, debugstr_w( name ) );
300 if( ov->view.ops != &order_ops )
301 return ERROR_FUNCTION_FAILED;
305 return ERROR_FUNCTION_FAILED;
306 if( !table->ops->get_dimensions )
307 return ERROR_FUNCTION_FAILED;
308 if( !table->ops->get_column_info )
309 return ERROR_FUNCTION_FAILED;
311 r = table->ops->get_dimensions( table, NULL, &count );
312 if( r != ERROR_SUCCESS )
315 if( ov->num_cols >= count )
316 return ERROR_FUNCTION_FAILED;
318 r = VIEW_find_column( table, name, &n );
319 if( r != ERROR_SUCCESS )
322 ov->cols[ov->num_cols] = n;
323 TRACE("Ordering by column %s (%d)\n", debugstr_w( name ), n);
327 return ERROR_SUCCESS;