advapi32: Return a fake handle from EventRegister.
[wine/multimedia.git] / dlls / msi / select.c
blob982cf6a93697ce37e98e038dd6e065bd0facb2f7
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2002-2004 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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(msidb);
39 /* below is the query interface to a table */
41 typedef struct tagMSISELECTVIEW
43 MSIVIEW view;
44 MSIDATABASE *db;
45 MSIVIEW *table;
46 UINT num_cols;
47 UINT max_cols;
48 UINT cols[1];
49 } MSISELECTVIEW;
51 static UINT SELECT_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
53 MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
55 TRACE("%p %d %d %p\n", sv, row, col, val );
57 if( !sv->table )
58 return ERROR_FUNCTION_FAILED;
60 if( !col || col > sv->num_cols )
61 return ERROR_FUNCTION_FAILED;
63 col = sv->cols[ col - 1 ];
64 if( !col )
66 *val = 0;
67 return ERROR_SUCCESS;
69 return sv->table->ops->fetch_int( sv->table, row, col, val );
72 static UINT SELECT_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm)
74 MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
76 TRACE("%p %d %d %p\n", sv, row, col, stm );
78 if( !sv->table )
79 return ERROR_FUNCTION_FAILED;
81 if( !col || col > sv->num_cols )
82 return ERROR_FUNCTION_FAILED;
84 col = sv->cols[ col - 1 ];
85 if( !col )
87 *stm = NULL;
88 return ERROR_SUCCESS;
90 return sv->table->ops->fetch_stream( sv->table, row, col, stm );
93 static UINT SELECT_get_row( struct tagMSIVIEW *view, UINT row, MSIRECORD **rec )
95 MSISELECTVIEW *sv = (MSISELECTVIEW *)view;
97 TRACE("%p %d %p\n", sv, row, rec );
99 if( !sv->table )
100 return ERROR_FUNCTION_FAILED;
102 return msi_view_get_row(sv->db, view, row, rec);
105 static UINT SELECT_set_row( struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask )
107 MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
108 UINT i, expanded_mask = 0, r = ERROR_SUCCESS, col_count = 0;
109 MSIRECORD *expanded;
111 TRACE("%p %d %p %08x\n", sv, row, rec, mask );
113 if ( !sv->table )
114 return ERROR_FUNCTION_FAILED;
116 /* test if any of the mask bits are invalid */
117 if ( mask >= (1<<sv->num_cols) )
118 return ERROR_INVALID_PARAMETER;
120 /* find the number of columns in the table below */
121 r = sv->table->ops->get_dimensions( sv->table, NULL, &col_count );
122 if( r )
123 return r;
125 /* expand the record to the right size for the underlying table */
126 expanded = MSI_CreateRecord( col_count );
127 if ( !expanded )
128 return ERROR_FUNCTION_FAILED;
130 /* move the right fields across */
131 for ( i=0; i<sv->num_cols; i++ )
133 r = MSI_RecordCopyField( rec, i+1, expanded, sv->cols[ i ] );
134 if (r != ERROR_SUCCESS)
135 break;
136 expanded_mask |= (1<<(sv->cols[i]-1));
139 /* set the row in the underlying table */
140 if (r == ERROR_SUCCESS)
141 r = sv->table->ops->set_row( sv->table, row, expanded, expanded_mask );
143 msiobj_release( &expanded->hdr );
144 return r;
147 static UINT SELECT_insert_row( struct tagMSIVIEW *view, MSIRECORD *record, UINT row, BOOL temporary )
149 MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
150 UINT i, table_cols, r;
151 MSIRECORD *outrec;
153 TRACE("%p %p\n", sv, record );
155 if ( !sv->table )
156 return ERROR_FUNCTION_FAILED;
158 /* rearrange the record to suit the table */
159 r = sv->table->ops->get_dimensions( sv->table, NULL, &table_cols );
160 if (r != ERROR_SUCCESS)
161 return r;
163 outrec = MSI_CreateRecord( table_cols + 1 );
165 for (i=0; i<sv->num_cols; i++)
167 r = MSI_RecordCopyField( record, i+1, outrec, sv->cols[i] );
168 if (r != ERROR_SUCCESS)
169 goto fail;
172 r = sv->table->ops->insert_row( sv->table, outrec, row, temporary );
174 fail:
175 msiobj_release( &outrec->hdr );
177 return r;
180 static UINT SELECT_execute( struct tagMSIVIEW *view, MSIRECORD *record )
182 MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
184 TRACE("%p %p\n", sv, record);
186 if( !sv->table )
187 return ERROR_FUNCTION_FAILED;
189 return sv->table->ops->execute( sv->table, record );
192 static UINT SELECT_close( struct tagMSIVIEW *view )
194 MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
196 TRACE("%p\n", sv );
198 if( !sv->table )
199 return ERROR_FUNCTION_FAILED;
201 return sv->table->ops->close( sv->table );
204 static UINT SELECT_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols )
206 MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
208 TRACE("%p %p %p\n", sv, rows, cols );
210 if( !sv->table )
211 return ERROR_FUNCTION_FAILED;
213 if( cols )
214 *cols = sv->num_cols;
216 return sv->table->ops->get_dimensions( sv->table, rows, NULL );
219 static UINT SELECT_get_column_info( struct tagMSIVIEW *view, UINT n, LPCWSTR *name,
220 UINT *type, BOOL *temporary, LPCWSTR *table_name )
222 MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
224 TRACE("%p %d %p %p %p %p\n", sv, n, name, type, temporary, table_name );
226 if( !sv->table )
227 return ERROR_FUNCTION_FAILED;
229 if( !n || n > sv->num_cols )
230 return ERROR_FUNCTION_FAILED;
232 n = sv->cols[ n - 1 ];
233 if( !n )
235 if (name) *name = szEmpty;
236 if (type) *type = MSITYPE_UNKNOWN | MSITYPE_VALID;
237 if (temporary) *temporary = FALSE;
238 if (table_name) *table_name = szEmpty;
239 return ERROR_SUCCESS;
241 return sv->table->ops->get_column_info( sv->table, n, name,
242 type, temporary, table_name );
245 static UINT msi_select_update(struct tagMSIVIEW *view, MSIRECORD *rec, UINT row)
247 MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
248 UINT r, i, num_columns, col, type, val;
249 LPCWSTR str;
250 MSIRECORD *mod;
252 r = SELECT_get_dimensions(view, NULL, &num_columns);
253 if (r != ERROR_SUCCESS)
254 return r;
256 r = sv->table->ops->get_row(sv->table, row - 1, &mod);
257 if (r != ERROR_SUCCESS)
258 return r;
260 for (i = 0; i < num_columns; i++)
262 col = sv->cols[i];
264 r = SELECT_get_column_info(view, i + 1, NULL, &type, NULL, NULL);
265 if (r != ERROR_SUCCESS)
267 ERR("Failed to get column information: %d\n", r);
268 goto done;
271 if (MSITYPE_IS_BINARY(type))
273 ERR("Cannot modify binary data!\n");
274 r = ERROR_FUNCTION_FAILED;
275 goto done;
277 else if (type & MSITYPE_STRING)
279 str = MSI_RecordGetString(rec, i + 1);
280 r = MSI_RecordSetStringW(mod, col, str);
282 else
284 val = MSI_RecordGetInteger(rec, i + 1);
285 r = MSI_RecordSetInteger(mod, col, val);
288 if (r != ERROR_SUCCESS)
290 ERR("Failed to modify record: %d\n", r);
291 goto done;
295 r = sv->table->ops->modify(sv->table, MSIMODIFY_UPDATE, mod, row);
297 done:
298 msiobj_release(&mod->hdr);
299 return r;
302 static UINT SELECT_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
303 MSIRECORD *rec, UINT row )
305 MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
307 TRACE("%p %d %p %d\n", sv, eModifyMode, rec, row );
309 if( !sv->table )
310 return ERROR_FUNCTION_FAILED;
312 if (eModifyMode == MSIMODIFY_UPDATE)
313 return msi_select_update(view, rec, row);
315 return sv->table->ops->modify( sv->table, eModifyMode, rec, row );
318 static UINT SELECT_delete( struct tagMSIVIEW *view )
320 MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
322 TRACE("%p\n", sv );
324 if( sv->table )
325 sv->table->ops->delete( sv->table );
326 sv->table = NULL;
328 msi_free( sv );
330 return ERROR_SUCCESS;
333 static UINT SELECT_find_matching_rows( struct tagMSIVIEW *view, UINT col,
334 UINT val, UINT *row, MSIITERHANDLE *handle )
336 MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
338 TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
340 if( !sv->table )
341 return ERROR_FUNCTION_FAILED;
343 if( (col==0) || (col>sv->num_cols) )
344 return ERROR_FUNCTION_FAILED;
346 col = sv->cols[ col - 1 ];
348 return sv->table->ops->find_matching_rows( sv->table, col, val, row, handle );
352 static const MSIVIEWOPS select_ops =
354 SELECT_fetch_int,
355 SELECT_fetch_stream,
356 SELECT_get_row,
357 SELECT_set_row,
358 SELECT_insert_row,
359 NULL,
360 SELECT_execute,
361 SELECT_close,
362 SELECT_get_dimensions,
363 SELECT_get_column_info,
364 SELECT_modify,
365 SELECT_delete,
366 SELECT_find_matching_rows,
367 NULL,
368 NULL,
369 NULL,
370 NULL,
371 NULL,
372 NULL,
375 static UINT SELECT_AddColumn( MSISELECTVIEW *sv, LPCWSTR name,
376 LPCWSTR table_name )
378 UINT r, n;
379 MSIVIEW *table;
381 TRACE("%p adding %s.%s\n", sv, debugstr_w( table_name ),
382 debugstr_w( name ));
384 if( sv->view.ops != &select_ops )
385 return ERROR_FUNCTION_FAILED;
387 table = sv->table;
388 if( !table )
389 return ERROR_FUNCTION_FAILED;
390 if( !table->ops->get_dimensions )
391 return ERROR_FUNCTION_FAILED;
392 if( !table->ops->get_column_info )
393 return ERROR_FUNCTION_FAILED;
395 if( sv->num_cols >= sv->max_cols )
396 return ERROR_FUNCTION_FAILED;
398 if ( !name[0] ) n = 0;
399 else
401 r = VIEW_find_column( table, name, table_name, &n );
402 if( r != ERROR_SUCCESS )
403 return r;
406 sv->cols[sv->num_cols] = n;
407 TRACE("Translating column %s from %d -> %d\n",
408 debugstr_w( name ), sv->num_cols, n);
410 sv->num_cols++;
412 return ERROR_SUCCESS;
415 static int select_count_columns( const column_info *col )
417 int n;
418 for (n = 0; col; col = col->next)
419 n++;
420 return n;
423 UINT SELECT_CreateView( MSIDATABASE *db, MSIVIEW **view, MSIVIEW *table,
424 const column_info *columns )
426 MSISELECTVIEW *sv = NULL;
427 UINT count = 0, r = ERROR_SUCCESS;
429 TRACE("%p\n", sv );
431 count = select_count_columns( columns );
433 sv = msi_alloc_zero( sizeof *sv + count*sizeof (UINT) );
434 if( !sv )
435 return ERROR_FUNCTION_FAILED;
437 /* fill the structure */
438 sv->view.ops = &select_ops;
439 sv->db = db;
440 sv->table = table;
441 sv->num_cols = 0;
442 sv->max_cols = count;
444 while( columns )
446 r = SELECT_AddColumn( sv, columns->column, columns->table );
447 if( r )
448 break;
449 columns = columns->next;
452 if( r == ERROR_SUCCESS )
453 *view = &sv->view;
454 else
455 msi_free( sv );
457 return r;