Implement MsiModifyView (MSIMODIFY_INSERT_TEMPORARY).
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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(msi);
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  * INSERT_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 static MSIRECORD *INSERT_merge_record( UINT fields, column_info *vl, MSIRECORD *rec )
66 {
67     MSIRECORD *merged;
68     DWORD wildcard_count = 1, i;
69     const WCHAR *str;
70
71     merged = MSI_CreateRecord( fields );
72     for( i=1; i <= fields; i++ )
73     {
74         if( !vl )
75         {
76             TRACE("Not enough elements in the list to insert\n");
77             goto err;
78         }
79         switch( vl->val->type )
80         {
81         case EXPR_SVAL:
82             TRACE("field %ld -> %s\n", i, debugstr_w(vl->val->u.sval));
83             MSI_RecordSetStringW( merged, i, vl->val->u.sval );
84             break;
85         case EXPR_IVAL:
86             MSI_RecordSetInteger( merged, i, vl->val->u.ival );
87             break;
88         case EXPR_WILDCARD:
89             if( !rec )
90                 goto err;
91             if( MSI_RecordIsNull( rec, wildcard_count ) )
92                 goto err;
93             str = MSI_RecordGetString( rec, wildcard_count );
94             MSI_RecordSetStringW( merged, i, str );
95             wildcard_count++;
96             break;
97         default:
98             ERR("Unknown expression type %d\n", vl->val->type);
99         }
100         vl = vl->next;
101     }
102
103     return merged;
104 err:
105     msiobj_release( &merged->hdr );
106     return NULL;
107 }
108
109 static UINT INSERT_execute( struct tagMSIVIEW *view, MSIRECORD *record )
110 {
111     MSIINSERTVIEW *iv = (MSIINSERTVIEW*)view;
112     UINT r, col_count = 0;
113     MSIVIEW *sv;
114     MSIRECORD *values = NULL;
115
116     TRACE("%p %p\n", iv, record );
117
118     sv = iv->sv;
119     if( !sv )
120         return ERROR_FUNCTION_FAILED;
121
122     r = sv->ops->execute( sv, 0 );
123     TRACE("tv execute returned %x\n", r);
124     if( r )
125         return r;
126
127     r = sv->ops->get_dimensions( sv, NULL, &col_count );
128     if( r )
129         goto err;
130
131     /*
132      * Merge the wildcard values into the list of values provided
133      * in the query, and create a record containing both.
134      */
135     values = INSERT_merge_record( col_count, iv->vals, record );
136     if( !values )
137         goto err;
138
139     r = sv->ops->insert_row( sv, values );
140
141 err:
142     if( values )
143         msiobj_release( &values->hdr );
144
145     return r;
146 }
147
148
149 static UINT INSERT_close( struct tagMSIVIEW *view )
150 {
151     MSIINSERTVIEW *iv = (MSIINSERTVIEW*)view;
152     MSIVIEW *sv;
153
154     TRACE("%p\n", iv);
155
156     sv = iv->sv;
157     if( !sv )
158         return ERROR_FUNCTION_FAILED;
159
160     return sv->ops->close( sv );
161 }
162
163 static UINT INSERT_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols )
164 {
165     MSIINSERTVIEW *iv = (MSIINSERTVIEW*)view;
166     MSIVIEW *sv;
167
168     TRACE("%p %p %p\n", iv, rows, cols );
169
170     sv = iv->sv;
171     if( !sv )
172         return ERROR_FUNCTION_FAILED;
173
174     return sv->ops->get_dimensions( sv, rows, cols );
175 }
176
177 static UINT INSERT_get_column_info( struct tagMSIVIEW *view,
178                 UINT n, LPWSTR *name, UINT *type )
179 {
180     MSIINSERTVIEW *iv = (MSIINSERTVIEW*)view;
181     MSIVIEW *sv;
182
183     TRACE("%p %d %p %p\n", iv, n, name, type );
184
185     sv = iv->sv;
186     if( !sv )
187         return ERROR_FUNCTION_FAILED;
188
189     return sv->ops->get_column_info( sv, n, name, type );
190 }
191
192 static UINT INSERT_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode, MSIRECORD *rec)
193 {
194     MSIINSERTVIEW *iv = (MSIINSERTVIEW*)view;
195
196     TRACE("%p %d %p\n", iv, eModifyMode, rec );
197
198     return ERROR_FUNCTION_FAILED;
199 }
200
201 static UINT INSERT_delete( struct tagMSIVIEW *view )
202 {
203     MSIINSERTVIEW *iv = (MSIINSERTVIEW*)view;
204     MSIVIEW *sv;
205
206     TRACE("%p\n", iv );
207
208     sv = iv->sv;
209     if( sv )
210         sv->ops->delete( sv );
211     msiobj_release( &iv->db->hdr );
212     HeapFree( GetProcessHeap(), 0, iv );
213
214     return ERROR_SUCCESS;
215 }
216
217
218 MSIVIEWOPS insert_ops =
219 {
220     INSERT_fetch_int,
221     NULL,
222     NULL,
223     NULL,
224     INSERT_execute,
225     INSERT_close,
226     INSERT_get_dimensions,
227     INSERT_get_column_info,
228     INSERT_modify,
229     INSERT_delete
230 };
231
232 UINT INSERT_CreateView( MSIDATABASE *db, MSIVIEW **view, LPWSTR table,
233                         column_info *columns, column_info *values, BOOL temp )
234 {
235     MSIINSERTVIEW *iv = NULL;
236     UINT r;
237     MSIVIEW *tv = NULL, *sv = NULL;
238
239     TRACE("%p\n", iv );
240
241     r = TABLE_CreateView( db, table, &tv );
242     if( r != ERROR_SUCCESS )
243         return r;
244
245     r = SELECT_CreateView( db, &sv, tv, columns );
246     if( r != ERROR_SUCCESS )
247     {
248         if( tv )
249             tv->ops->delete( tv );
250         return r;
251     }
252     
253     iv = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof *iv );
254     if( !iv )
255         return ERROR_FUNCTION_FAILED;
256
257     /* fill the structure */
258     iv->view.ops = &insert_ops;
259     msiobj_addref( &db->hdr );
260     iv->db = db;
261     iv->vals = values;
262     iv->bIsTemp = temp;
263     iv->sv = sv;
264     *view = (MSIVIEW*) iv;
265
266     return ERROR_SUCCESS;
267 }