join.c 8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 * Implementation of the Microsoft Installer (msi.dll)
 *
 * Copyright 2006 Mike McCormack for CodeWeavers
 *
 * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include <stdarg.h>

#include "windef.h"
#include "winbase.h"
#include "winerror.h"
#include "msi.h"
#include "msiquery.h"
#include "objbase.h"
#include "objidl.h"
#include "msipriv.h"
#include "query.h"

33 34 35
#include "wine/debug.h"
#include "wine/unicode.h"

36 37
WINE_DEFAULT_DEBUG_CHANNEL(msidb);

38 39 40 41 42 43 44 45 46
typedef struct tagJOINTABLE
{
    struct list entry;
    MSIVIEW *view;
    UINT columns;
    UINT rows;
    UINT next_rows;
} JOINTABLE;

47 48 49 50
typedef struct tagMSIJOINVIEW
{
    MSIVIEW        view;
    MSIDATABASE   *db;
51 52 53
    struct list    tables;
    UINT           columns;
    UINT           rows;
54 55 56 57 58
} MSIJOINVIEW;

static UINT JOIN_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
{
    MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
59 60 61
    JOINTABLE *table;
    UINT cols = 0;
    UINT prev_rows = 1;
62

63
    TRACE("%d, %d\n", row, col);
64

65
    if (col == 0 || col > jv->columns)
66 67
         return ERROR_FUNCTION_FAILED;

68
    if (row >= jv->rows)
69 70
         return ERROR_FUNCTION_FAILED;

71
    LIST_FOR_EACH_ENTRY(table, &jv->tables, JOINTABLE, entry)
72
    {
73 74 75 76 77 78 79 80 81
        if (col <= cols + table->columns)
        {
            row = (row % (jv->rows / table->next_rows)) / prev_rows;
            col -= cols;
            break;
        }

        prev_rows = table->rows;
        cols += table->columns;
82 83
    }

84
    return table->view->ops->fetch_int( table->view, row, col, val );
85 86 87 88 89
}

static UINT JOIN_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm)
{
    MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
90 91 92
    JOINTABLE *table;
    UINT cols = 0;
    UINT prev_rows = 1;
93 94 95

    TRACE("%p %d %d %p\n", jv, row, col, stm );

96
    if (col == 0 || col > jv->columns)
97 98
         return ERROR_FUNCTION_FAILED;

99
    if (row >= jv->rows)
100 101
         return ERROR_FUNCTION_FAILED;

102
    LIST_FOR_EACH_ENTRY(table, &jv->tables, JOINTABLE, entry)
103
    {
104 105 106 107 108 109 110 111 112
        if (col <= cols + table->columns)
        {
            row = (row % (jv->rows / table->next_rows)) / prev_rows;
            col -= cols;
            break;
        }

        prev_rows = table->rows;
        cols += table->columns;
113 114
    }

115
    return table->view->ops->fetch_stream( table->view, row, col, stm );
116 117
}

118 119
static UINT JOIN_get_row( struct tagMSIVIEW *view, UINT row, MSIRECORD **rec )
{
120 121
    FIXME("(%p, %d, %p): stub!\n", view, row, rec);
    return ERROR_FUNCTION_FAILED;
122 123
}

124 125 126
static UINT JOIN_execute( struct tagMSIVIEW *view, MSIRECORD *record )
{
    MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
127 128
    JOINTABLE *table;
    UINT r, rows;
129 130 131

    TRACE("%p %p\n", jv, record);

132
    LIST_FOR_EACH_ENTRY(table, &jv->tables, JOINTABLE, entry)
133
    {
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
        table->view->ops->execute(table->view, NULL);

        r = table->view->ops->get_dimensions(table->view, &table->rows, NULL);
        if (r != ERROR_SUCCESS)
        {
            ERR("failed to get table dimensions\n");
            return r;
        }

        /* each table must have at least one row */
        if (table->rows == 0)
        {
            jv->rows = 0;
            return ERROR_SUCCESS;
        }

        if (jv->rows == 0)
            jv->rows = table->rows;
        else
            jv->rows *= table->rows;
154
    }
155

156 157
    rows = jv->rows;
    LIST_FOR_EACH_ENTRY(table, &jv->tables, JOINTABLE, entry)
158
    {
159 160
        rows /= table->rows;
        table->next_rows = rows;
161
    }
162

163
    return ERROR_SUCCESS;
164 165 166 167 168
}

static UINT JOIN_close( struct tagMSIVIEW *view )
{
    MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
169
    JOINTABLE *table;
170 171 172

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

173 174 175 176
    LIST_FOR_EACH_ENTRY(table, &jv->tables, JOINTABLE, entry)
    {
        table->view->ops->close(table->view);
    }
177 178 179 180 181 182 183 184 185 186

    return ERROR_SUCCESS;
}

static UINT JOIN_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols )
{
    MSIJOINVIEW *jv = (MSIJOINVIEW*)view;

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

187 188
    if (cols)
        *cols = jv->columns;
189

190 191
    if (rows)
        *rows = jv->rows;
192 193 194 195 196 197 198 199

    return ERROR_SUCCESS;
}

static UINT JOIN_get_column_info( struct tagMSIVIEW *view,
                UINT n, LPWSTR *name, UINT *type )
{
    MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
200 201
    JOINTABLE *table;
    UINT cols = 0;
202 203 204

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

205
    if (n == 0 || n > jv->columns)
206 207
        return ERROR_FUNCTION_FAILED;

208 209 210 211
    LIST_FOR_EACH_ENTRY(table, &jv->tables, JOINTABLE, entry)
    {
        if (n <= cols + table->columns)
            return table->view->ops->get_column_info(table->view, n - cols, name, type);
212

213 214
        cols += table->columns;
    }
215

216
    return ERROR_FUNCTION_FAILED;
217 218 219
}

static UINT JOIN_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
220
                         MSIRECORD *rec, UINT row )
221
{
222
    TRACE("%p %d %p\n", view, eModifyMode, rec);
223 224 225 226 227 228
    return ERROR_FUNCTION_FAILED;
}

static UINT JOIN_delete( struct tagMSIVIEW *view )
{
    MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
229
    JOINTABLE *table;
230 231 232

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

233 234 235 236 237
    LIST_FOR_EACH_ENTRY(table, &jv->tables, JOINTABLE, entry)
    {
        table->view->ops->delete(table->view);
        table->view = NULL;
    }
238

239
    msi_free(jv);
Mike McCormack's avatar
Mike McCormack committed
240

241 242 243 244 245 246 247
    return ERROR_SUCCESS;
}

static UINT JOIN_find_matching_rows( struct tagMSIVIEW *view, UINT col,
    UINT val, UINT *row, MSIITERHANDLE *handle )
{
    MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
248
    UINT i, row_value;
249

250
    TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
251

252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
    if (col == 0 || col > jv->columns)
        return ERROR_INVALID_PARAMETER;

    for (i = (UINT)*handle; i < jv->rows; i++)
    {
        if (view->ops->fetch_int( view, i, col, &row_value ) != ERROR_SUCCESS)
            continue;

        if (row_value == val)
        {
            *row = i;
            (*(UINT *)handle) = i + 1;
            return ERROR_SUCCESS;
        }
    }

    return ERROR_NO_MORE_ITEMS;
269 270 271 272 273 274
}

static const MSIVIEWOPS join_ops =
{
    JOIN_fetch_int,
    JOIN_fetch_stream,
275
    JOIN_get_row,
276 277
    NULL,
    NULL,
278
    NULL,
279 280 281 282 283 284
    JOIN_execute,
    JOIN_close,
    JOIN_get_dimensions,
    JOIN_get_column_info,
    JOIN_modify,
    JOIN_delete,
285 286 287
    JOIN_find_matching_rows,
    NULL,
    NULL,
288
    NULL,
289
    NULL,
290 291
};

292
UINT JOIN_CreateView( MSIDATABASE *db, MSIVIEW **view, LPWSTR tables )
293 294 295
{
    MSIJOINVIEW *jv = NULL;
    UINT r = ERROR_SUCCESS;
296 297
    JOINTABLE *table;
    LPWSTR ptr;
298

299
    TRACE("%p (%s)\n", jv, debugstr_w(tables) );
300 301 302 303 304 305 306 307

    jv = msi_alloc_zero( sizeof *jv );
    if( !jv )
        return ERROR_FUNCTION_FAILED;

    /* fill the structure */
    jv->view.ops = &join_ops;
    jv->db = db;
308 309
    jv->columns = 0;
    jv->rows = 0;
310

311
    list_init(&jv->tables);
312

313
    while (*tables)
314
    {
315 316
        if ((ptr = strchrW(tables, ' ')))
            *ptr = '\0';
317

318 319 320
        table = msi_alloc(sizeof(JOINTABLE));
        if (!table)
            return ERROR_OUTOFMEMORY;
321

322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
        r = TABLE_CreateView( db, tables, &table->view );
        if( r != ERROR_SUCCESS )
        {
            ERR("can't create table\n");
            goto end;
        }

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

        jv->columns += table->columns;

        list_add_head( &jv->tables, &table->entry );

        if (!ptr)
            break;

        tables = ptr + 1;
344 345 346 347 348 349 350 351 352 353
    }

    *view = &jv->view;
    return ERROR_SUCCESS;

end:
    jv->view.ops->delete( &jv->view );

    return r;
}