[MSI]
[reactos.git] / reactos / dll / win32 / msi / select.c
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
3 *
4 * Copyright 2002-2004 Mike McCormack for CodeWeavers
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include "msipriv.h"
22
23 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
24
25 /* below is the query interface to a table */
26
27 typedef struct tagMSISELECTVIEW
28 {
29 MSIVIEW view;
30 MSIDATABASE *db;
31 MSIVIEW *table;
32 UINT num_cols;
33 UINT max_cols;
34 UINT cols[1];
35 } MSISELECTVIEW;
36
37 static UINT SELECT_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
38 {
39 MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
40
41 TRACE("%p %d %d %p\n", sv, row, col, val );
42
43 if( !sv->table )
44 return ERROR_FUNCTION_FAILED;
45
46 if( !col || col > sv->num_cols )
47 return ERROR_FUNCTION_FAILED;
48
49 col = sv->cols[ col - 1 ];
50 if( !col )
51 {
52 *val = 0;
53 return ERROR_SUCCESS;
54 }
55 return sv->table->ops->fetch_int( sv->table, row, col, val );
56 }
57
58 static UINT SELECT_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm)
59 {
60 MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
61
62 TRACE("%p %d %d %p\n", sv, row, col, stm );
63
64 if( !sv->table )
65 return ERROR_FUNCTION_FAILED;
66
67 if( !col || col > sv->num_cols )
68 return ERROR_FUNCTION_FAILED;
69
70 col = sv->cols[ col - 1 ];
71 if( !col )
72 {
73 *stm = NULL;
74 return ERROR_SUCCESS;
75 }
76 return sv->table->ops->fetch_stream( sv->table, row, col, stm );
77 }
78
79 static UINT SELECT_get_row( struct tagMSIVIEW *view, UINT row, MSIRECORD **rec )
80 {
81 MSISELECTVIEW *sv = (MSISELECTVIEW *)view;
82
83 TRACE("%p %d %p\n", sv, row, rec );
84
85 if( !sv->table )
86 return ERROR_FUNCTION_FAILED;
87
88 return msi_view_get_row(sv->db, view, row, rec);
89 }
90
91 static UINT SELECT_set_row( struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask )
92 {
93 MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
94 UINT i, expanded_mask = 0, r = ERROR_SUCCESS, col_count = 0;
95 MSIRECORD *expanded;
96
97 TRACE("%p %d %p %08x\n", sv, row, rec, mask );
98
99 if ( !sv->table )
100 return ERROR_FUNCTION_FAILED;
101
102 /* test if any of the mask bits are invalid */
103 if ( mask >= (1<<sv->num_cols) )
104 return ERROR_INVALID_PARAMETER;
105
106 /* find the number of columns in the table below */
107 r = sv->table->ops->get_dimensions( sv->table, NULL, &col_count );
108 if( r )
109 return r;
110
111 /* expand the record to the right size for the underlying table */
112 expanded = MSI_CreateRecord( col_count );
113 if ( !expanded )
114 return ERROR_FUNCTION_FAILED;
115
116 /* move the right fields across */
117 for ( i=0; i<sv->num_cols; i++ )
118 {
119 r = MSI_RecordCopyField( rec, i+1, expanded, sv->cols[ i ] );
120 if (r != ERROR_SUCCESS)
121 break;
122 expanded_mask |= (1<<(sv->cols[i]-1));
123 }
124
125 /* set the row in the underlying table */
126 if (r == ERROR_SUCCESS)
127 r = sv->table->ops->set_row( sv->table, row, expanded, expanded_mask );
128
129 msiobj_release( &expanded->hdr );
130 return r;
131 }
132
133 static UINT SELECT_insert_row( struct tagMSIVIEW *view, MSIRECORD *record, UINT row, BOOL temporary )
134 {
135 MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
136 UINT i, table_cols, r;
137 MSIRECORD *outrec;
138
139 TRACE("%p %p\n", sv, record );
140
141 if ( !sv->table )
142 return ERROR_FUNCTION_FAILED;
143
144 /* rearrange the record to suit the table */
145 r = sv->table->ops->get_dimensions( sv->table, NULL, &table_cols );
146 if (r != ERROR_SUCCESS)
147 return r;
148
149 outrec = MSI_CreateRecord( table_cols + 1 );
150
151 for (i=0; i<sv->num_cols; i++)
152 {
153 r = MSI_RecordCopyField( record, i+1, outrec, sv->cols[i] );
154 if (r != ERROR_SUCCESS)
155 goto fail;
156 }
157
158 r = sv->table->ops->insert_row( sv->table, outrec, row, temporary );
159
160 fail:
161 msiobj_release( &outrec->hdr );
162
163 return r;
164 }
165
166 static UINT SELECT_execute( struct tagMSIVIEW *view, MSIRECORD *record )
167 {
168 MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
169
170 TRACE("%p %p\n", sv, record);
171
172 if( !sv->table )
173 return ERROR_FUNCTION_FAILED;
174
175 return sv->table->ops->execute( sv->table, record );
176 }
177
178 static UINT SELECT_close( struct tagMSIVIEW *view )
179 {
180 MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
181
182 TRACE("%p\n", sv );
183
184 if( !sv->table )
185 return ERROR_FUNCTION_FAILED;
186
187 return sv->table->ops->close( sv->table );
188 }
189
190 static UINT SELECT_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols )
191 {
192 MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
193
194 TRACE("%p %p %p\n", sv, rows, cols );
195
196 if( !sv->table )
197 return ERROR_FUNCTION_FAILED;
198
199 if( cols )
200 *cols = sv->num_cols;
201
202 return sv->table->ops->get_dimensions( sv->table, rows, NULL );
203 }
204
205 static UINT SELECT_get_column_info( struct tagMSIVIEW *view, UINT n, LPCWSTR *name,
206 UINT *type, BOOL *temporary, LPCWSTR *table_name )
207 {
208 MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
209
210 TRACE("%p %d %p %p %p %p\n", sv, n, name, type, temporary, table_name );
211
212 if( !sv->table )
213 return ERROR_FUNCTION_FAILED;
214
215 if( !n || n > sv->num_cols )
216 return ERROR_FUNCTION_FAILED;
217
218 n = sv->cols[ n - 1 ];
219 if( !n )
220 {
221 if (name) *name = szEmpty;
222 if (type) *type = MSITYPE_UNKNOWN | MSITYPE_VALID;
223 if (temporary) *temporary = FALSE;
224 if (table_name) *table_name = szEmpty;
225 return ERROR_SUCCESS;
226 }
227 return sv->table->ops->get_column_info( sv->table, n, name,
228 type, temporary, table_name );
229 }
230
231 static UINT msi_select_update(struct tagMSIVIEW *view, MSIRECORD *rec, UINT row)
232 {
233 MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
234 UINT r, i, num_columns, col, type, val;
235 LPCWSTR str;
236 MSIRECORD *mod;
237
238 r = SELECT_get_dimensions(view, NULL, &num_columns);
239 if (r != ERROR_SUCCESS)
240 return r;
241
242 r = sv->table->ops->get_row(sv->table, row - 1, &mod);
243 if (r != ERROR_SUCCESS)
244 return r;
245
246 for (i = 0; i < num_columns; i++)
247 {
248 col = sv->cols[i];
249
250 r = SELECT_get_column_info(view, i + 1, NULL, &type, NULL, NULL);
251 if (r != ERROR_SUCCESS)
252 {
253 ERR("Failed to get column information: %d\n", r);
254 goto done;
255 }
256
257 if (MSITYPE_IS_BINARY(type))
258 {
259 ERR("Cannot modify binary data!\n");
260 r = ERROR_FUNCTION_FAILED;
261 goto done;
262 }
263 else if (type & MSITYPE_STRING)
264 {
265 int len;
266 str = msi_record_get_string( rec, i + 1, &len );
267 r = msi_record_set_string( mod, col, str, len );
268 }
269 else
270 {
271 val = MSI_RecordGetInteger(rec, i + 1);
272 r = MSI_RecordSetInteger(mod, col, val);
273 }
274
275 if (r != ERROR_SUCCESS)
276 {
277 ERR("Failed to modify record: %d\n", r);
278 goto done;
279 }
280 }
281
282 r = sv->table->ops->modify(sv->table, MSIMODIFY_UPDATE, mod, row);
283
284 done:
285 msiobj_release(&mod->hdr);
286 return r;
287 }
288
289 static UINT SELECT_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
290 MSIRECORD *rec, UINT row )
291 {
292 MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
293
294 TRACE("%p %d %p %d\n", sv, eModifyMode, rec, row );
295
296 if( !sv->table )
297 return ERROR_FUNCTION_FAILED;
298
299 if (eModifyMode == MSIMODIFY_UPDATE)
300 return msi_select_update(view, rec, row);
301
302 return sv->table->ops->modify( sv->table, eModifyMode, rec, row );
303 }
304
305 static UINT SELECT_delete( struct tagMSIVIEW *view )
306 {
307 MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
308
309 TRACE("%p\n", sv );
310
311 if( sv->table )
312 sv->table->ops->delete( sv->table );
313 sv->table = NULL;
314
315 msi_free( sv );
316
317 return ERROR_SUCCESS;
318 }
319
320 static UINT SELECT_find_matching_rows( struct tagMSIVIEW *view, UINT col,
321 UINT val, UINT *row, MSIITERHANDLE *handle )
322 {
323 MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
324
325 TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
326
327 if( !sv->table )
328 return ERROR_FUNCTION_FAILED;
329
330 if( (col==0) || (col>sv->num_cols) )
331 return ERROR_FUNCTION_FAILED;
332
333 col = sv->cols[ col - 1 ];
334
335 return sv->table->ops->find_matching_rows( sv->table, col, val, row, handle );
336 }
337
338
339 static const MSIVIEWOPS select_ops =
340 {
341 SELECT_fetch_int,
342 SELECT_fetch_stream,
343 SELECT_get_row,
344 SELECT_set_row,
345 SELECT_insert_row,
346 NULL,
347 SELECT_execute,
348 SELECT_close,
349 SELECT_get_dimensions,
350 SELECT_get_column_info,
351 SELECT_modify,
352 SELECT_delete,
353 SELECT_find_matching_rows,
354 NULL,
355 NULL,
356 NULL,
357 NULL,
358 NULL,
359 NULL,
360 };
361
362 static UINT SELECT_AddColumn( MSISELECTVIEW *sv, LPCWSTR name,
363 LPCWSTR table_name )
364 {
365 UINT r, n;
366 MSIVIEW *table;
367
368 TRACE("%p adding %s.%s\n", sv, debugstr_w( table_name ),
369 debugstr_w( name ));
370
371 if( sv->view.ops != &select_ops )
372 return ERROR_FUNCTION_FAILED;
373
374 table = sv->table;
375 if( !table )
376 return ERROR_FUNCTION_FAILED;
377 if( !table->ops->get_dimensions )
378 return ERROR_FUNCTION_FAILED;
379 if( !table->ops->get_column_info )
380 return ERROR_FUNCTION_FAILED;
381
382 if( sv->num_cols >= sv->max_cols )
383 return ERROR_FUNCTION_FAILED;
384
385 if ( !name[0] ) n = 0;
386 else
387 {
388 r = VIEW_find_column( table, name, table_name, &n );
389 if( r != ERROR_SUCCESS )
390 return r;
391 }
392
393 sv->cols[sv->num_cols] = n;
394 TRACE("Translating column %s from %d -> %d\n",
395 debugstr_w( name ), sv->num_cols, n);
396
397 sv->num_cols++;
398
399 return ERROR_SUCCESS;
400 }
401
402 static int select_count_columns( const column_info *col )
403 {
404 int n;
405 for (n = 0; col; col = col->next)
406 n++;
407 return n;
408 }
409
410 UINT SELECT_CreateView( MSIDATABASE *db, MSIVIEW **view, MSIVIEW *table,
411 const column_info *columns )
412 {
413 MSISELECTVIEW *sv = NULL;
414 UINT count = 0, r = ERROR_SUCCESS;
415
416 TRACE("%p\n", sv );
417
418 count = select_count_columns( columns );
419
420 sv = msi_alloc_zero( FIELD_OFFSET( MSISELECTVIEW, cols[count] ));
421 if( !sv )
422 return ERROR_FUNCTION_FAILED;
423
424 /* fill the structure */
425 sv->view.ops = &select_ops;
426 sv->db = db;
427 sv->table = table;
428 sv->num_cols = 0;
429 sv->max_cols = count;
430
431 while( columns )
432 {
433 r = SELECT_AddColumn( sv, columns->column, columns->table );
434 if( r )
435 break;
436 columns = columns->next;
437 }
438
439 if( r == ERROR_SUCCESS )
440 *view = &sv->view;
441 else
442 msi_free( sv );
443
444 return r;
445 }