msi/tests: Test a return value (clang).
[wine.git] / dlls / msi / select.c
blobf6b5daef2f957544b5543013049f4e15a273197c
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==0) || (col>sv->num_cols) )
61 return ERROR_FUNCTION_FAILED;
63 col = sv->cols[ col - 1 ];
65 return sv->table->ops->fetch_int( sv->table, row, col, val );
68 static UINT SELECT_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm)
70 MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
72 TRACE("%p %d %d %p\n", sv, row, col, stm );
74 if( !sv->table )
75 return ERROR_FUNCTION_FAILED;
77 if( (col==0) || (col>sv->num_cols) )
78 return ERROR_FUNCTION_FAILED;
80 col = sv->cols[ col - 1 ];
82 return sv->table->ops->fetch_stream( sv->table, row, col, stm );
85 static UINT SELECT_get_row( struct tagMSIVIEW *view, UINT row, MSIRECORD **rec )
87 MSISELECTVIEW *sv = (MSISELECTVIEW *)view;
89 TRACE("%p %d %p\n", sv, row, rec );
91 if( !sv->table )
92 return ERROR_FUNCTION_FAILED;
94 return msi_view_get_row(sv->db, view, row, rec);
97 static UINT SELECT_set_row( struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask )
99 MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
100 UINT i, expanded_mask = 0, r = ERROR_SUCCESS, col_count = 0;
101 MSIRECORD *expanded;
103 TRACE("%p %d %p %08x\n", sv, row, rec, mask );
105 if ( !sv->table )
106 return ERROR_FUNCTION_FAILED;
108 /* test if any of the mask bits are invalid */
109 if ( mask >= (1<<sv->num_cols) )
110 return ERROR_INVALID_PARAMETER;
112 /* find the number of columns in the table below */
113 r = sv->table->ops->get_dimensions( sv->table, NULL, &col_count );
114 if( r )
115 return r;
117 /* expand the record to the right size for the underlying table */
118 expanded = MSI_CreateRecord( col_count );
119 if ( !expanded )
120 return ERROR_FUNCTION_FAILED;
122 /* move the right fields across */
123 for ( i=0; i<sv->num_cols; i++ )
125 r = MSI_RecordCopyField( rec, i+1, expanded, sv->cols[ i ] );
126 if (r != ERROR_SUCCESS)
127 break;
128 expanded_mask |= (1<<(sv->cols[i]-1));
131 /* set the row in the underlying table */
132 if (r == ERROR_SUCCESS)
133 r = sv->table->ops->set_row( sv->table, row, expanded, expanded_mask );
135 msiobj_release( &expanded->hdr );
136 return r;
139 static UINT SELECT_insert_row( struct tagMSIVIEW *view, MSIRECORD *record, UINT row, BOOL temporary )
141 MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
142 UINT i, table_cols, r;
143 MSIRECORD *outrec;
145 TRACE("%p %p\n", sv, record );
147 if ( !sv->table )
148 return ERROR_FUNCTION_FAILED;
150 /* rearrange the record to suit the table */
151 r = sv->table->ops->get_dimensions( sv->table, NULL, &table_cols );
152 if (r != ERROR_SUCCESS)
153 return r;
155 outrec = MSI_CreateRecord( table_cols + 1 );
157 for (i=0; i<sv->num_cols; i++)
159 r = MSI_RecordCopyField( record, i+1, outrec, sv->cols[i] );
160 if (r != ERROR_SUCCESS)
161 goto fail;
164 r = sv->table->ops->insert_row( sv->table, outrec, row, temporary );
166 fail:
167 msiobj_release( &outrec->hdr );
169 return r;
172 static UINT SELECT_execute( struct tagMSIVIEW *view, MSIRECORD *record )
174 MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
176 TRACE("%p %p\n", sv, record);
178 if( !sv->table )
179 return ERROR_FUNCTION_FAILED;
181 return sv->table->ops->execute( sv->table, record );
184 static UINT SELECT_close( struct tagMSIVIEW *view )
186 MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
188 TRACE("%p\n", sv );
190 if( !sv->table )
191 return ERROR_FUNCTION_FAILED;
193 return sv->table->ops->close( sv->table );
196 static UINT SELECT_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols )
198 MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
200 TRACE("%p %p %p\n", sv, rows, cols );
202 if( !sv->table )
203 return ERROR_FUNCTION_FAILED;
205 if( cols )
206 *cols = sv->num_cols;
208 return sv->table->ops->get_dimensions( sv->table, rows, NULL );
211 static UINT SELECT_get_column_info( struct tagMSIVIEW *view,
212 UINT n, LPWSTR *name, UINT *type, BOOL *temporary,
213 LPWSTR *table_name)
215 MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
217 TRACE("%p %d %p %p %p %p\n", sv, n, name, type, temporary, table_name );
219 if( !sv->table )
220 return ERROR_FUNCTION_FAILED;
222 if( (n==0) || (n>sv->num_cols) )
223 return ERROR_FUNCTION_FAILED;
225 n = sv->cols[ n - 1 ];
227 return sv->table->ops->get_column_info( sv->table, n, name,
228 type, temporary, table_name );
231 static UINT msi_select_update(struct tagMSIVIEW *view, MSIRECORD *rec, UINT row)
233 MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
234 UINT r, i, num_columns, col, type, val;
235 LPWSTR name;
236 LPCWSTR str;
237 MSIRECORD *mod;
239 r = SELECT_get_dimensions(view, NULL, &num_columns);
240 if (r != ERROR_SUCCESS)
241 return r;
243 r = sv->table->ops->get_row(sv->table, row - 1, &mod);
244 if (r != ERROR_SUCCESS)
245 return r;
247 for (i = 0; i < num_columns; i++)
249 col = sv->cols[i];
251 r = SELECT_get_column_info(view, i + 1, &name, &type, NULL, NULL);
252 msi_free(name);
253 if (r != ERROR_SUCCESS)
255 ERR("Failed to get column information: %d\n", r);
256 goto done;
259 if (MSITYPE_IS_BINARY(type))
261 ERR("Cannot modify binary data!\n");
262 r = ERROR_FUNCTION_FAILED;
263 goto done;
265 else if (type & MSITYPE_STRING)
267 str = MSI_RecordGetString(rec, i + 1);
268 r = MSI_RecordSetStringW(mod, col, str);
270 else
272 val = MSI_RecordGetInteger(rec, i + 1);
273 r = MSI_RecordSetInteger(mod, col, val);
276 if (r != ERROR_SUCCESS)
278 ERR("Failed to modify record: %d\n", r);
279 goto done;
283 r = sv->table->ops->modify(sv->table, MSIMODIFY_UPDATE, mod, row);
285 done:
286 msiobj_release(&mod->hdr);
287 return r;
290 static UINT SELECT_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
291 MSIRECORD *rec, UINT row )
293 MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
295 TRACE("%p %d %p %d\n", sv, eModifyMode, rec, row );
297 if( !sv->table )
298 return ERROR_FUNCTION_FAILED;
300 if (eModifyMode == MSIMODIFY_UPDATE)
301 return msi_select_update(view, rec, row);
303 return sv->table->ops->modify( sv->table, eModifyMode, rec, row );
306 static UINT SELECT_delete( struct tagMSIVIEW *view )
308 MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
310 TRACE("%p\n", sv );
312 if( sv->table )
313 sv->table->ops->delete( sv->table );
314 sv->table = NULL;
316 msi_free( sv );
318 return ERROR_SUCCESS;
321 static UINT SELECT_find_matching_rows( struct tagMSIVIEW *view, UINT col,
322 UINT val, UINT *row, MSIITERHANDLE *handle )
324 MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
326 TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
328 if( !sv->table )
329 return ERROR_FUNCTION_FAILED;
331 if( (col==0) || (col>sv->num_cols) )
332 return ERROR_FUNCTION_FAILED;
334 col = sv->cols[ col - 1 ];
336 return sv->table->ops->find_matching_rows( sv->table, col, val, row, handle );
339 static UINT SELECT_sort(struct tagMSIVIEW *view, column_info *columns)
341 MSISELECTVIEW *sv = (MSISELECTVIEW *)view;
343 TRACE("%p %p\n", view, columns);
345 return sv->table->ops->sort( sv->table, columns );
348 static const MSIVIEWOPS select_ops =
350 SELECT_fetch_int,
351 SELECT_fetch_stream,
352 SELECT_get_row,
353 SELECT_set_row,
354 SELECT_insert_row,
355 NULL,
356 SELECT_execute,
357 SELECT_close,
358 SELECT_get_dimensions,
359 SELECT_get_column_info,
360 SELECT_modify,
361 SELECT_delete,
362 SELECT_find_matching_rows,
363 NULL,
364 NULL,
365 NULL,
366 NULL,
367 SELECT_sort,
368 NULL,
371 static UINT SELECT_AddColumn( MSISELECTVIEW *sv, LPCWSTR name,
372 LPCWSTR table_name )
374 UINT r, n=0;
375 MSIVIEW *table;
377 TRACE("%p adding %s.%s\n", sv, debugstr_w( table_name ),
378 debugstr_w( name ));
380 if( sv->view.ops != &select_ops )
381 return ERROR_FUNCTION_FAILED;
383 table = sv->table;
384 if( !table )
385 return ERROR_FUNCTION_FAILED;
386 if( !table->ops->get_dimensions )
387 return ERROR_FUNCTION_FAILED;
388 if( !table->ops->get_column_info )
389 return ERROR_FUNCTION_FAILED;
391 if( sv->num_cols >= sv->max_cols )
392 return ERROR_FUNCTION_FAILED;
394 r = VIEW_find_column( table, name, table_name, &n );
395 if( r != ERROR_SUCCESS )
396 return r;
398 sv->cols[sv->num_cols] = n;
399 TRACE("Translating column %s from %d -> %d\n",
400 debugstr_w( name ), sv->num_cols, n);
402 sv->num_cols++;
404 return ERROR_SUCCESS;
407 static int select_count_columns( const column_info *col )
409 int n;
410 for (n = 0; col; col = col->next)
411 n++;
412 return n;
415 UINT SELECT_CreateView( MSIDATABASE *db, MSIVIEW **view, MSIVIEW *table,
416 const column_info *columns )
418 MSISELECTVIEW *sv = NULL;
419 UINT count = 0, r = ERROR_SUCCESS;
421 TRACE("%p\n", sv );
423 count = select_count_columns( columns );
425 sv = msi_alloc_zero( sizeof *sv + count*sizeof (UINT) );
426 if( !sv )
427 return ERROR_FUNCTION_FAILED;
429 /* fill the structure */
430 sv->view.ops = &select_ops;
431 sv->db = db;
432 sv->table = table;
433 sv->num_cols = 0;
434 sv->max_cols = count;
436 while( columns )
438 r = SELECT_AddColumn( sv, columns->column, columns->table );
439 if( r )
440 break;
441 columns = columns->next;
444 if( r == ERROR_SUCCESS )
445 *view = &sv->view;
446 else
447 msi_free( sv );
449 return r;