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 #define WIN32_NO_STATUS
23 #define COM_NO_WINDOWS_H
28 //#include "winbase.h"
29 //#include "winerror.h"
30 #include <wine/debug.h>
32 //#include "msiquery.h"
33 //#include "objbase.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(msidb
);
43 /* below is the query interface to a table */
45 typedef struct tagMSISELECTVIEW
55 static UINT
SELECT_fetch_int( struct tagMSIVIEW
*view
, UINT row
, UINT col
, UINT
*val
)
57 MSISELECTVIEW
*sv
= (MSISELECTVIEW
*)view
;
59 TRACE("%p %d %d %p\n", sv
, row
, col
, val
);
62 return ERROR_FUNCTION_FAILED
;
64 if( !col
|| col
> sv
->num_cols
)
65 return ERROR_FUNCTION_FAILED
;
67 col
= sv
->cols
[ col
- 1 ];
73 return sv
->table
->ops
->fetch_int( sv
->table
, row
, col
, val
);
76 static UINT
SELECT_fetch_stream( struct tagMSIVIEW
*view
, UINT row
, UINT col
, IStream
**stm
)
78 MSISELECTVIEW
*sv
= (MSISELECTVIEW
*)view
;
80 TRACE("%p %d %d %p\n", sv
, row
, col
, stm
);
83 return ERROR_FUNCTION_FAILED
;
85 if( !col
|| col
> sv
->num_cols
)
86 return ERROR_FUNCTION_FAILED
;
88 col
= sv
->cols
[ col
- 1 ];
94 return sv
->table
->ops
->fetch_stream( sv
->table
, row
, col
, stm
);
97 static UINT
SELECT_get_row( struct tagMSIVIEW
*view
, UINT row
, MSIRECORD
**rec
)
99 MSISELECTVIEW
*sv
= (MSISELECTVIEW
*)view
;
101 TRACE("%p %d %p\n", sv
, row
, rec
);
104 return ERROR_FUNCTION_FAILED
;
106 return msi_view_get_row(sv
->db
, view
, row
, rec
);
109 static UINT
SELECT_set_row( struct tagMSIVIEW
*view
, UINT row
, MSIRECORD
*rec
, UINT mask
)
111 MSISELECTVIEW
*sv
= (MSISELECTVIEW
*)view
;
112 UINT i
, expanded_mask
= 0, r
= ERROR_SUCCESS
, col_count
= 0;
115 TRACE("%p %d %p %08x\n", sv
, row
, rec
, mask
);
118 return ERROR_FUNCTION_FAILED
;
120 /* test if any of the mask bits are invalid */
121 if ( mask
>= (1<<sv
->num_cols
) )
122 return ERROR_INVALID_PARAMETER
;
124 /* find the number of columns in the table below */
125 r
= sv
->table
->ops
->get_dimensions( sv
->table
, NULL
, &col_count
);
129 /* expand the record to the right size for the underlying table */
130 expanded
= MSI_CreateRecord( col_count
);
132 return ERROR_FUNCTION_FAILED
;
134 /* move the right fields across */
135 for ( i
=0; i
<sv
->num_cols
; i
++ )
137 r
= MSI_RecordCopyField( rec
, i
+1, expanded
, sv
->cols
[ i
] );
138 if (r
!= ERROR_SUCCESS
)
140 expanded_mask
|= (1<<(sv
->cols
[i
]-1));
143 /* set the row in the underlying table */
144 if (r
== ERROR_SUCCESS
)
145 r
= sv
->table
->ops
->set_row( sv
->table
, row
, expanded
, expanded_mask
);
147 msiobj_release( &expanded
->hdr
);
151 static UINT
SELECT_insert_row( struct tagMSIVIEW
*view
, MSIRECORD
*record
, UINT row
, BOOL temporary
)
153 MSISELECTVIEW
*sv
= (MSISELECTVIEW
*)view
;
154 UINT i
, table_cols
, r
;
157 TRACE("%p %p\n", sv
, record
);
160 return ERROR_FUNCTION_FAILED
;
162 /* rearrange the record to suit the table */
163 r
= sv
->table
->ops
->get_dimensions( sv
->table
, NULL
, &table_cols
);
164 if (r
!= ERROR_SUCCESS
)
167 outrec
= MSI_CreateRecord( table_cols
+ 1 );
169 for (i
=0; i
<sv
->num_cols
; i
++)
171 r
= MSI_RecordCopyField( record
, i
+1, outrec
, sv
->cols
[i
] );
172 if (r
!= ERROR_SUCCESS
)
176 r
= sv
->table
->ops
->insert_row( sv
->table
, outrec
, row
, temporary
);
179 msiobj_release( &outrec
->hdr
);
184 static UINT
SELECT_execute( struct tagMSIVIEW
*view
, MSIRECORD
*record
)
186 MSISELECTVIEW
*sv
= (MSISELECTVIEW
*)view
;
188 TRACE("%p %p\n", sv
, record
);
191 return ERROR_FUNCTION_FAILED
;
193 return sv
->table
->ops
->execute( sv
->table
, record
);
196 static UINT
SELECT_close( struct tagMSIVIEW
*view
)
198 MSISELECTVIEW
*sv
= (MSISELECTVIEW
*)view
;
203 return ERROR_FUNCTION_FAILED
;
205 return sv
->table
->ops
->close( sv
->table
);
208 static UINT
SELECT_get_dimensions( struct tagMSIVIEW
*view
, UINT
*rows
, UINT
*cols
)
210 MSISELECTVIEW
*sv
= (MSISELECTVIEW
*)view
;
212 TRACE("%p %p %p\n", sv
, rows
, cols
);
215 return ERROR_FUNCTION_FAILED
;
218 *cols
= sv
->num_cols
;
220 return sv
->table
->ops
->get_dimensions( sv
->table
, rows
, NULL
);
223 static UINT
SELECT_get_column_info( struct tagMSIVIEW
*view
, UINT n
, LPCWSTR
*name
,
224 UINT
*type
, BOOL
*temporary
, LPCWSTR
*table_name
)
226 MSISELECTVIEW
*sv
= (MSISELECTVIEW
*)view
;
228 TRACE("%p %d %p %p %p %p\n", sv
, n
, name
, type
, temporary
, table_name
);
231 return ERROR_FUNCTION_FAILED
;
233 if( !n
|| n
> sv
->num_cols
)
234 return ERROR_FUNCTION_FAILED
;
236 n
= sv
->cols
[ n
- 1 ];
239 if (name
) *name
= szEmpty
;
240 if (type
) *type
= MSITYPE_UNKNOWN
| MSITYPE_VALID
;
241 if (temporary
) *temporary
= FALSE
;
242 if (table_name
) *table_name
= szEmpty
;
243 return ERROR_SUCCESS
;
245 return sv
->table
->ops
->get_column_info( sv
->table
, n
, name
,
246 type
, temporary
, table_name
);
249 static UINT
msi_select_update(struct tagMSIVIEW
*view
, MSIRECORD
*rec
, UINT row
)
251 MSISELECTVIEW
*sv
= (MSISELECTVIEW
*)view
;
252 UINT r
, i
, num_columns
, col
, type
, val
;
256 r
= SELECT_get_dimensions(view
, NULL
, &num_columns
);
257 if (r
!= ERROR_SUCCESS
)
260 r
= sv
->table
->ops
->get_row(sv
->table
, row
- 1, &mod
);
261 if (r
!= ERROR_SUCCESS
)
264 for (i
= 0; i
< num_columns
; i
++)
268 r
= SELECT_get_column_info(view
, i
+ 1, NULL
, &type
, NULL
, NULL
);
269 if (r
!= ERROR_SUCCESS
)
271 ERR("Failed to get column information: %d\n", r
);
275 if (MSITYPE_IS_BINARY(type
))
277 ERR("Cannot modify binary data!\n");
278 r
= ERROR_FUNCTION_FAILED
;
281 else if (type
& MSITYPE_STRING
)
284 str
= msi_record_get_string( rec
, i
+ 1, &len
);
285 r
= msi_record_set_string( mod
, col
, str
, len
);
289 val
= MSI_RecordGetInteger(rec
, i
+ 1);
290 r
= MSI_RecordSetInteger(mod
, col
, val
);
293 if (r
!= ERROR_SUCCESS
)
295 ERR("Failed to modify record: %d\n", r
);
300 r
= sv
->table
->ops
->modify(sv
->table
, MSIMODIFY_UPDATE
, mod
, row
);
303 msiobj_release(&mod
->hdr
);
307 static UINT
SELECT_modify( struct tagMSIVIEW
*view
, MSIMODIFY eModifyMode
,
308 MSIRECORD
*rec
, UINT row
)
310 MSISELECTVIEW
*sv
= (MSISELECTVIEW
*)view
;
312 TRACE("%p %d %p %d\n", sv
, eModifyMode
, rec
, row
);
315 return ERROR_FUNCTION_FAILED
;
317 if (eModifyMode
== MSIMODIFY_UPDATE
)
318 return msi_select_update(view
, rec
, row
);
320 return sv
->table
->ops
->modify( sv
->table
, eModifyMode
, rec
, row
);
323 static UINT
SELECT_delete( struct tagMSIVIEW
*view
)
325 MSISELECTVIEW
*sv
= (MSISELECTVIEW
*)view
;
330 sv
->table
->ops
->delete( sv
->table
);
335 return ERROR_SUCCESS
;
338 static UINT
SELECT_find_matching_rows( struct tagMSIVIEW
*view
, UINT col
,
339 UINT val
, UINT
*row
, MSIITERHANDLE
*handle
)
341 MSISELECTVIEW
*sv
= (MSISELECTVIEW
*)view
;
343 TRACE("%p, %d, %u, %p\n", view
, col
, val
, *handle
);
346 return ERROR_FUNCTION_FAILED
;
348 if( (col
==0) || (col
>sv
->num_cols
) )
349 return ERROR_FUNCTION_FAILED
;
351 col
= sv
->cols
[ col
- 1 ];
353 return sv
->table
->ops
->find_matching_rows( sv
->table
, col
, val
, row
, handle
);
357 static const MSIVIEWOPS select_ops
=
367 SELECT_get_dimensions
,
368 SELECT_get_column_info
,
371 SELECT_find_matching_rows
,
380 static UINT
SELECT_AddColumn( MSISELECTVIEW
*sv
, LPCWSTR name
,
386 TRACE("%p adding %s.%s\n", sv
, debugstr_w( table_name
),
389 if( sv
->view
.ops
!= &select_ops
)
390 return ERROR_FUNCTION_FAILED
;
394 return ERROR_FUNCTION_FAILED
;
395 if( !table
->ops
->get_dimensions
)
396 return ERROR_FUNCTION_FAILED
;
397 if( !table
->ops
->get_column_info
)
398 return ERROR_FUNCTION_FAILED
;
400 if( sv
->num_cols
>= sv
->max_cols
)
401 return ERROR_FUNCTION_FAILED
;
403 if ( !name
[0] ) n
= 0;
406 r
= VIEW_find_column( table
, name
, table_name
, &n
);
407 if( r
!= ERROR_SUCCESS
)
411 sv
->cols
[sv
->num_cols
] = n
;
412 TRACE("Translating column %s from %d -> %d\n",
413 debugstr_w( name
), sv
->num_cols
, n
);
417 return ERROR_SUCCESS
;
420 static int select_count_columns( const column_info
*col
)
423 for (n
= 0; col
; col
= col
->next
)
428 UINT
SELECT_CreateView( MSIDATABASE
*db
, MSIVIEW
**view
, MSIVIEW
*table
,
429 const column_info
*columns
)
431 MSISELECTVIEW
*sv
= NULL
;
432 UINT count
= 0, r
= ERROR_SUCCESS
;
436 count
= select_count_columns( columns
);
438 sv
= msi_alloc_zero( FIELD_OFFSET( MSISELECTVIEW
, cols
[count
] ));
440 return ERROR_FUNCTION_FAILED
;
442 /* fill the structure */
443 sv
->view
.ops
= &select_ops
;
447 sv
->max_cols
= count
;
451 r
= SELECT_AddColumn( sv
, columns
->column
, columns
->table
);
454 columns
= columns
->next
;
457 if( r
== ERROR_SUCCESS
)