msi: Test the PublishProduct action with the machine context.
[wine] / dlls / msi / insert.c
1 /*
2  * Implementation of the Microsoft Installer (msi.dll)
3  *
4  * Copyright 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 <stdarg.h>
22
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"
33
34 #include "query.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
37
38
39 /* below is the query interface to a table */
40
41 typedef struct tagMSIINSERTVIEW
42 {
43     MSIVIEW          view;
44     MSIDATABASE     *db;
45     BOOL             bIsTemp;
46     MSIVIEW         *sv;
47     column_info     *vals;
48 } MSIINSERTVIEW;
49
50 static UINT INSERT_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
51 {
52     MSIINSERTVIEW *iv = (MSIINSERTVIEW*)view;
53
54     TRACE("%p %d %d %p\n", iv, row, col, val );
55
56     return ERROR_FUNCTION_FAILED;
57 }
58
59 /*
60  * msi_query_merge_record
61  *
62  * Merge a value_list and a record to create a second record.
63  * Replace wildcard entries in the valuelist with values from the record
64  */
65 MSIRECORD *msi_query_merge_record( UINT fields, const column_info *vl, MSIRECORD *rec )
66 {
67     MSIRECORD *merged;
68     DWORD wildcard_count = 1, i;
69
70     merged = MSI_CreateRecord( fields );
71     for( i=1; i <= fields; i++ )
72     {
73         if( !vl )
74         {
75             TRACE("Not enough elements in the list to insert\n");
76             goto err;
77         }
78         switch( vl->val->type )
79         {
80         case EXPR_SVAL:
81             TRACE("field %d -> %s\n", i, debugstr_w(vl->val->u.sval));
82             MSI_RecordSetStringW( merged, i, vl->val->u.sval );
83             break;
84         case EXPR_IVAL:
85             MSI_RecordSetInteger( merged, i, vl->val->u.ival );
86             break;
87         case EXPR_WILDCARD:
88             if( !rec )
89                 goto err;
90             MSI_RecordCopyField( rec, wildcard_count, merged, i );
91             wildcard_count++;
92             break;
93         default:
94             ERR("Unknown expression type %d\n", vl->val->type);
95         }
96         vl = vl->next;
97     }
98
99     return merged;
100 err:
101     msiobj_release( &merged->hdr );
102     return NULL;
103 }
104
105 static UINT INSERT_execute( struct tagMSIVIEW *view, MSIRECORD *record )
106 {
107     MSIINSERTVIEW *iv = (MSIINSERTVIEW*)view;
108     UINT r, col_count = 0;
109     MSIVIEW *sv;
110     MSIRECORD *values = NULL;
111
112     TRACE("%p %p\n", iv, record );
113
114     sv = iv->sv;
115     if( !sv )
116         return ERROR_FUNCTION_FAILED;
117
118     r = sv->ops->execute( sv, 0 );
119     TRACE("tv execute returned %x\n", r);
120     if( r )
121         return r;
122
123     r = sv->ops->get_dimensions( sv, NULL, &col_count );
124     if( r )
125         goto err;
126
127     /*
128      * Merge the wildcard values into the list of values provided
129      * in the query, and create a record containing both.
130      */
131     values = msi_query_merge_record( col_count, iv->vals, record );
132     if( !values )
133         goto err;
134
135     r = sv->ops->insert_row( sv, values, iv->bIsTemp );
136
137 err:
138     if( values )
139         msiobj_release( &values->hdr );
140
141     return r;
142 }
143
144
145 static UINT INSERT_close( struct tagMSIVIEW *view )
146 {
147     MSIINSERTVIEW *iv = (MSIINSERTVIEW*)view;
148     MSIVIEW *sv;
149
150     TRACE("%p\n", iv);
151
152     sv = iv->sv;
153     if( !sv )
154         return ERROR_FUNCTION_FAILED;
155
156     return sv->ops->close( sv );
157 }
158
159 static UINT INSERT_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols )
160 {
161     MSIINSERTVIEW *iv = (MSIINSERTVIEW*)view;
162     MSIVIEW *sv;
163
164     TRACE("%p %p %p\n", iv, rows, cols );
165
166     sv = iv->sv;
167     if( !sv )
168         return ERROR_FUNCTION_FAILED;
169
170     return sv->ops->get_dimensions( sv, rows, cols );
171 }
172
173 static UINT INSERT_get_column_info( struct tagMSIVIEW *view,
174                 UINT n, LPWSTR *name, UINT *type )
175 {
176     MSIINSERTVIEW *iv = (MSIINSERTVIEW*)view;
177     MSIVIEW *sv;
178
179     TRACE("%p %d %p %p\n", iv, n, name, type );
180
181     sv = iv->sv;
182     if( !sv )
183         return ERROR_FUNCTION_FAILED;
184
185     return sv->ops->get_column_info( sv, n, name, type );
186 }
187
188 static UINT INSERT_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode, MSIRECORD *rec, UINT row)
189 {
190     MSIINSERTVIEW *iv = (MSIINSERTVIEW*)view;
191
192     TRACE("%p %d %p\n", iv, eModifyMode, rec );
193
194     return ERROR_FUNCTION_FAILED;
195 }
196
197 static UINT INSERT_delete( struct tagMSIVIEW *view )
198 {
199     MSIINSERTVIEW *iv = (MSIINSERTVIEW*)view;
200     MSIVIEW *sv;
201
202     TRACE("%p\n", iv );
203
204     sv = iv->sv;
205     if( sv )
206         sv->ops->delete( sv );
207     msiobj_release( &iv->db->hdr );
208     msi_free( iv );
209
210     return ERROR_SUCCESS;
211 }
212
213 static UINT INSERT_find_matching_rows( struct tagMSIVIEW *view, UINT col,
214     UINT val, UINT *row, MSIITERHANDLE *handle )
215 {
216     TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
217
218     return ERROR_FUNCTION_FAILED;
219 }
220
221
222 static const MSIVIEWOPS insert_ops =
223 {
224     INSERT_fetch_int,
225     NULL,
226     NULL,
227     NULL,
228     NULL,
229     NULL,
230     INSERT_execute,
231     INSERT_close,
232     INSERT_get_dimensions,
233     INSERT_get_column_info,
234     INSERT_modify,
235     INSERT_delete,
236     INSERT_find_matching_rows,
237     NULL,
238     NULL,
239     NULL,
240     NULL,
241     NULL,
242 };
243
244 static UINT count_column_info( const column_info *ci )
245 {
246     UINT n = 0;
247     for ( ; ci; ci = ci->next )
248         n++;
249     return n;
250 }
251
252 UINT INSERT_CreateView( MSIDATABASE *db, MSIVIEW **view, LPCWSTR table,
253                         column_info *columns, column_info *values, BOOL temp )
254 {
255     MSIINSERTVIEW *iv = NULL;
256     UINT r;
257     MSIVIEW *tv = NULL, *sv = NULL;
258
259     TRACE("%p\n", iv );
260
261     /* there should be one value for each column */
262     if ( count_column_info( columns ) != count_column_info(values) )
263         return ERROR_BAD_QUERY_SYNTAX;
264
265     r = TABLE_CreateView( db, table, &tv );
266     if( r != ERROR_SUCCESS )
267         return r;
268
269     r = SELECT_CreateView( db, &sv, tv, columns );
270     if( r != ERROR_SUCCESS )
271     {
272         if( tv )
273             tv->ops->delete( tv );
274         return r;
275     }
276
277     iv = msi_alloc_zero( sizeof *iv );
278     if( !iv )
279         return ERROR_FUNCTION_FAILED;
280
281     /* fill the structure */
282     iv->view.ops = &insert_ops;
283     msiobj_addref( &db->hdr );
284     iv->db = db;
285     iv->vals = values;
286     iv->bIsTemp = temp;
287     iv->sv = sv;
288     *view = (MSIVIEW*) iv;
289
290     return ERROR_SUCCESS;
291 }