msi: Merge action.h into msipriv.h.
[wine] / dlls / msi / update.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 receuved 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 tagMSIUPDATEVIEW
42 {
43     MSIVIEW          view;
44     MSIDATABASE     *db;
45     MSIVIEW         *wv;
46     column_info     *vals;
47 } MSIUPDATEVIEW;
48
49 static UINT UPDATE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
50 {
51     MSIUPDATEVIEW *uv = (MSIUPDATEVIEW*)view;
52
53     TRACE("%p %d %d %p\n", uv, row, col, val );
54
55     return ERROR_FUNCTION_FAILED;
56 }
57
58 static UINT UPDATE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
59 {
60     MSIUPDATEVIEW *uv = (MSIUPDATEVIEW*)view;
61     UINT n, type, val, r, row, col_count = 0, row_count = 0;
62     MSIVIEW *wv;
63
64     TRACE("%p %p\n", uv, record );
65
66     if( !record )
67         return ERROR_FUNCTION_FAILED;
68
69     wv = uv->wv;
70     if( !wv )
71         return ERROR_FUNCTION_FAILED;
72
73     r = wv->ops->execute( wv, 0 );
74     TRACE("tv execute returned %x\n", r);
75     if( r )
76         return r;
77
78     r = wv->ops->get_dimensions( wv, &row_count, &col_count );
79     if( r )
80         goto err;
81
82     for( row = 0; row < row_count; row++ )
83     {
84         for( n = 1; n <= col_count; n++ )
85         {
86             r = wv->ops->get_column_info( wv, n, NULL, &type );
87             if( r )
88                 break;
89
90             if( type & MSITYPE_STRING )
91             {
92                 const WCHAR *str = MSI_RecordGetString( record, n );
93                 val = msi_addstringW( uv->db->strings, 0, str, -1, 1 );
94             }
95             else
96             {
97                 val = MSI_RecordGetInteger( record, n );
98                 val |= 0x8000;
99             }
100             r = wv->ops->set_int( wv, row, n, val );
101             if( r )
102                 break;
103         }
104     }
105
106 err:
107     return ERROR_SUCCESS;
108 }
109
110
111 static UINT UPDATE_close( struct tagMSIVIEW *view )
112 {
113     MSIUPDATEVIEW *uv = (MSIUPDATEVIEW*)view;
114     MSIVIEW *wv;
115
116     TRACE("%p\n", uv);
117
118     wv = uv->wv;
119     if( !wv )
120         return ERROR_FUNCTION_FAILED;
121
122     return wv->ops->close( wv );
123 }
124
125 static UINT UPDATE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols )
126 {
127     MSIUPDATEVIEW *uv = (MSIUPDATEVIEW*)view;
128     MSIVIEW *wv;
129
130     TRACE("%p %p %p\n", uv, rows, cols );
131
132     wv = uv->wv;
133     if( !wv )
134         return ERROR_FUNCTION_FAILED;
135
136     return wv->ops->get_dimensions( wv, rows, cols );
137 }
138
139 static UINT UPDATE_get_column_info( struct tagMSIVIEW *view,
140                 UINT n, LPWSTR *name, UINT *type )
141 {
142     MSIUPDATEVIEW *uv = (MSIUPDATEVIEW*)view;
143     MSIVIEW *wv;
144
145     TRACE("%p %d %p %p\n", uv, n, name, type );
146
147     wv = uv->wv;
148     if( !wv )
149         return ERROR_FUNCTION_FAILED;
150
151     return wv->ops->get_column_info( wv, n, name, type );
152 }
153
154 static UINT UPDATE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
155                 MSIRECORD *rec )
156 {
157     MSIUPDATEVIEW *uv = (MSIUPDATEVIEW*)view;
158
159     TRACE("%p %d %p\n", uv, eModifyMode, rec );
160
161     return ERROR_FUNCTION_FAILED;
162 }
163
164 static UINT UPDATE_delete( struct tagMSIVIEW *view )
165 {
166     MSIUPDATEVIEW *uv = (MSIUPDATEVIEW*)view;
167     MSIVIEW *wv;
168
169     TRACE("%p\n", uv );
170
171     wv = uv->wv;
172     if( wv )
173         wv->ops->delete( wv );
174     msiobj_release( &uv->db->hdr );
175     msi_free( uv );
176
177     return ERROR_SUCCESS;
178 }
179
180 static UINT UPDATE_find_matching_rows( struct tagMSIVIEW *view, UINT col, UINT val, UINT *row, MSIITERHANDLE *handle )
181 {
182     TRACE("%p %d %d %p\n", view, col, val, *handle );
183
184     return ERROR_FUNCTION_FAILED;
185 }
186
187
188 static MSIVIEWOPS update_ops =
189 {
190     UPDATE_fetch_int,
191     NULL,
192     NULL,
193     NULL,
194     UPDATE_execute,
195     UPDATE_close,
196     UPDATE_get_dimensions,
197     UPDATE_get_column_info,
198     UPDATE_modify,
199     UPDATE_delete,
200     UPDATE_find_matching_rows
201 };
202
203 UINT UPDATE_CreateView( MSIDATABASE *db, MSIVIEW **view, LPWSTR table,
204                         column_info *columns, struct expr *expr )
205 {
206     MSIUPDATEVIEW *uv = NULL;
207     UINT r;
208     MSIVIEW *tv = NULL, *sv = NULL, *wv = NULL;
209
210     TRACE("%p\n", uv );
211
212     r = TABLE_CreateView( db, table, &tv );
213     if( r != ERROR_SUCCESS )
214         return r;
215
216     /* add conditions first */
217     r = WHERE_CreateView( db, &wv, tv, expr );
218     if( r != ERROR_SUCCESS )
219     {
220         tv->ops->delete( tv );
221         return r;
222     }
223     
224     /* then select the columns we want */
225     r = SELECT_CreateView( db, &sv, wv, columns );
226     if( r != ERROR_SUCCESS )
227     {
228         wv->ops->delete( wv );
229         return r;
230     }
231
232     uv = msi_alloc_zero( sizeof *uv );
233     if( !uv )
234         return ERROR_FUNCTION_FAILED;
235
236     /* fill the structure */
237     uv->view.ops = &update_ops;
238     msiobj_addref( &db->hdr );
239     uv->db = db;
240     uv->vals = columns;
241     uv->wv = sv;
242     *view = (MSIVIEW*) uv;
243
244     return ERROR_SUCCESS;
245 }