order.c 7.94 KB
Newer Older
1 2 3
/*
 * Implementation of the Microsoft Installer (msi.dll)
 *
4
 * Copyright 2002 Mike McCormack for CodeWeavers
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

21 22
#include <stdarg.h>

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
#include "windef.h"
#include "winbase.h"
#include "winerror.h"
#include "wine/debug.h"
#include "msi.h"
#include "msiquery.h"
#include "objbase.h"
#include "objidl.h"
#include "msipriv.h"
#include "winnls.h"

#include "query.h"

WINE_DEFAULT_DEBUG_CHANNEL(msi);


/* below is the query interface to a table */

typedef struct tagMSIORDERVIEW
{
    MSIVIEW        view;
    MSIDATABASE   *db;
    MSIVIEW       *table;
    UINT          *reorder;
    UINT           num_cols;
    UINT           cols[1];
} MSIORDERVIEW;

static UINT ORDER_compare( MSIORDERVIEW *ov, UINT a, UINT b, UINT *swap )
{
    UINT r, i, a_val = 0, b_val = 0;

    *swap = 0;
    for( i=0; i<ov->num_cols; i++ )
    {
        r = ov->table->ops->fetch_int( ov->table, a, ov->cols[i], &a_val );
        if( r != ERROR_SUCCESS )
            return r;

        r = ov->table->ops->fetch_int( ov->table, b, ov->cols[i], &b_val );
        if( r != ERROR_SUCCESS )
            return r;

        if( a_val != b_val )
        {
            if( a_val > b_val )
                *swap = 1;
            break;
        }
    }

    return ERROR_SUCCESS;
}

static UINT ORDER_mergesort( MSIORDERVIEW *ov, UINT left, UINT right )
{
    UINT r, centre = (left + right)/2, temp, swap = 0, i, j;
    UINT *array = ov->reorder;

    if( left == right )
        return ERROR_SUCCESS;

    /* sort the left half */
    r = ORDER_mergesort( ov, left, centre );
    if( r != ERROR_SUCCESS )
        return r;

    /* sort the right half */
    r = ORDER_mergesort( ov, centre+1, right );
    if( r != ERROR_SUCCESS )
        return r;

    for( i=left, j=centre+1; (i<=centre) && (j<=right); i++ )
    {
        r = ORDER_compare( ov, array[i], array[j], &swap );
        if( r != ERROR_SUCCESS )
            return r;
        if( swap )
        { 
            temp = array[j];
            memmove( &array[i+1], &array[i], (j-i)*sizeof (UINT) );
            array[i] = temp;
            j++;
            centre++;
        }
    }

    return ERROR_SUCCESS;
}

static UINT ORDER_verify( MSIORDERVIEW *ov, UINT num_rows )
{
    UINT i, swap, r;

    for( i=1; i<num_rows; i++ )
    {
        r = ORDER_compare( ov, ov->reorder[i-1], ov->reorder[i], &swap );
        if( r != ERROR_SUCCESS )
            return r;
        if( !swap )
            continue;
        ERR("Bad order! %d\n", i);
        return ERROR_FUNCTION_FAILED;
    }

    return ERROR_SUCCESS;
}

static UINT ORDER_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
{
    MSIORDERVIEW *ov = (MSIORDERVIEW*)view;

    TRACE("%p %d %d %p\n", ov, row, col, val );

    if( !ov->table )
         return ERROR_FUNCTION_FAILED;

    row = ov->reorder[ row ];

    return ov->table->ops->fetch_int( ov->table, row, col, val );
}

145
static UINT ORDER_execute( struct tagMSIVIEW *view, MSIRECORD *record )
146 147 148 149
{
    MSIORDERVIEW *ov = (MSIORDERVIEW*)view;
    UINT r, num_rows = 0, i;

150
    TRACE("%p %p\n", ov, record);
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189

    if( !ov->table )
         return ERROR_FUNCTION_FAILED;

    r = ov->table->ops->execute( ov->table, record );
    if( r != ERROR_SUCCESS )
        return r;

    r = ov->table->ops->get_dimensions( ov->table, &num_rows, NULL );
    if( r != ERROR_SUCCESS )
        return r;

    ov->reorder = HeapAlloc( GetProcessHeap(), 0, num_rows*sizeof(UINT) );
    if( !ov->reorder )
        return ERROR_FUNCTION_FAILED;

    for( i=0; i<num_rows; i++ )
        ov->reorder[i] = i;

    r = ORDER_mergesort( ov, 0, num_rows - 1 );
    if( r != ERROR_SUCCESS )
        return r;

    r = ORDER_verify( ov, num_rows );
    if( r != ERROR_SUCCESS )
        return r;

    return ERROR_SUCCESS;
}

static UINT ORDER_close( struct tagMSIVIEW *view )
{
    MSIORDERVIEW *ov = (MSIORDERVIEW*)view;

    TRACE("%p\n", ov );

    if( !ov->table )
         return ERROR_FUNCTION_FAILED;

190
    HeapFree( GetProcessHeap(), 0, ov->reorder );
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
    ov->reorder = NULL;

    return ov->table->ops->close( ov->table );
}

static UINT ORDER_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols )
{
    MSIORDERVIEW *ov = (MSIORDERVIEW*)view;

    TRACE("%p %p %p\n", ov, rows, cols );

    if( !ov->table )
         return ERROR_FUNCTION_FAILED;

    return ov->table->ops->get_dimensions( ov->table, rows, cols );
}

static UINT ORDER_get_column_info( struct tagMSIVIEW *view,
                UINT n, LPWSTR *name, UINT *type )
{
    MSIORDERVIEW *ov = (MSIORDERVIEW*)view;

    TRACE("%p %d %p %p\n", ov, n, name, type );

    if( !ov->table )
         return ERROR_FUNCTION_FAILED;

    return ov->table->ops->get_column_info( ov->table, n, name, type );
}

221 222
static UINT ORDER_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
                MSIRECORD *rec )
223 224 225
{
    MSIORDERVIEW *ov = (MSIORDERVIEW*)view;

226
    TRACE("%p %d %p\n", ov, eModifyMode, rec );
227 228 229 230

    if( !ov->table )
         return ERROR_FUNCTION_FAILED;

231
    return ov->table->ops->modify( ov->table, eModifyMode, rec );
232 233 234 235 236 237 238 239 240 241 242
}

static UINT ORDER_delete( struct tagMSIVIEW *view )
{
    MSIORDERVIEW *ov = (MSIORDERVIEW*)view;

    TRACE("%p\n", ov );

    if( ov->table )
        ov->table->ops->delete( ov->table );

243
    HeapFree( GetProcessHeap(), 0, ov->reorder );
244 245
    ov->reorder = NULL;

246
    msiobj_release( &ov->db->hdr );
247
    HeapFree( GetProcessHeap(), 0, ov );
248 249 250 251 252 253 254 255

    return ERROR_SUCCESS;
}


MSIVIEWOPS order_ops =
{
    ORDER_fetch_int,
256 257
    NULL,
    NULL,
258
    NULL,
259 260 261 262 263 264 265 266
    ORDER_execute,
    ORDER_close,
    ORDER_get_dimensions,
    ORDER_get_column_info,
    ORDER_modify,
    ORDER_delete
};

267
static UINT ORDER_AddColumn( MSIORDERVIEW *ov, LPCWSTR name )
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
{
    UINT n, count, r;
    MSIVIEW *table;

    TRACE("%p adding %s\n", ov, debugstr_w( name ) );

    if( ov->view.ops != &order_ops )
        return ERROR_FUNCTION_FAILED;

    table = ov->table;
    if( !table )
        return ERROR_FUNCTION_FAILED;
    if( !table->ops->get_dimensions )
        return ERROR_FUNCTION_FAILED;
    if( !table->ops->get_column_info )
        return ERROR_FUNCTION_FAILED;

    r = table->ops->get_dimensions( table, NULL, &count );
    if( r != ERROR_SUCCESS )
        return r;

    if( ov->num_cols >= count )
        return ERROR_FUNCTION_FAILED;

    r = VIEW_find_column( table, name, &n );
    if( r != ERROR_SUCCESS )
        return r;

    ov->cols[ov->num_cols] = n;
    TRACE("Ordering by column %s (%d)\n", debugstr_w( name ), n);

    ov->num_cols++;

    return ERROR_SUCCESS;
}
303 304

UINT ORDER_CreateView( MSIDATABASE *db, MSIVIEW **view, MSIVIEW *table,
305
                       column_info *columns )
306 307 308
{
    MSIORDERVIEW *ov = NULL;
    UINT count = 0, r;
309
    column_info *x;
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334

    TRACE("%p\n", ov );

    r = table->ops->get_dimensions( table, NULL, &count );
    if( r != ERROR_SUCCESS )
    {
        ERR("can't get table dimensions\n");
        return r;
    }

    ov = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 
                    sizeof *ov + sizeof (UINT) * count );
    if( !ov )
        return ERROR_FUNCTION_FAILED;
    
    /* fill the structure */
    ov->view.ops = &order_ops;
    msiobj_addref( &db->hdr );
    ov->db = db;
    ov->table = table;
    ov->reorder = NULL;
    ov->num_cols = 0;
    *view = (MSIVIEW*) ov;

    for( x = columns; x ; x = x->next )
335
        ORDER_AddColumn( ov, x->column );
336 337 338

    return ERROR_SUCCESS;
}