Sync to trunk (r44371)
[reactos.git] / reactos / dll / win32 / 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 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 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 i, r, col_count = 0, row_count = 0;
62 MSIRECORD *values = NULL;
63 MSIRECORD *where = NULL;
64 MSIVIEW *wv;
65 UINT cols_count, where_count;
66 column_info *col = uv->vals;
67
68 TRACE("%p %p\n", uv, record );
69
70 /* extract the where markers from the record */
71 if (record)
72 {
73 r = MSI_RecordGetFieldCount(record);
74
75 for (i = 0; col; col = col->next)
76 i++;
77
78 cols_count = i;
79 where_count = r - i;
80
81 if (where_count > 0)
82 {
83 where = MSI_CreateRecord(where_count);
84
85 if (where)
86 for (i = 1; i <= where_count; i++)
87 MSI_RecordCopyField(record, cols_count + i, where, i);
88 }
89 }
90
91 wv = uv->wv;
92 if( !wv )
93 {
94 r = ERROR_FUNCTION_FAILED;
95 goto done;
96 }
97
98 r = wv->ops->execute( wv, where );
99 TRACE("tv execute returned %x\n", r);
100 if( r )
101 goto done;
102
103 r = wv->ops->get_dimensions( wv, &row_count, &col_count );
104 if( r )
105 goto done;
106
107 values = msi_query_merge_record( col_count, uv->vals, record );
108 if (!values)
109 {
110 r = ERROR_FUNCTION_FAILED;
111 goto done;
112 }
113
114 for ( i=0; i<row_count; i++ )
115 {
116 r = wv->ops->set_row( wv, i, values, (1 << col_count) - 1 );
117 if (r != ERROR_SUCCESS)
118 break;
119 }
120
121 done:
122 if ( where ) msiobj_release( &where->hdr );
123 if ( values ) msiobj_release( &values->hdr );
124
125 return r;
126 }
127
128
129 static UINT UPDATE_close( struct tagMSIVIEW *view )
130 {
131 MSIUPDATEVIEW *uv = (MSIUPDATEVIEW*)view;
132 MSIVIEW *wv;
133
134 TRACE("%p\n", uv);
135
136 wv = uv->wv;
137 if( !wv )
138 return ERROR_FUNCTION_FAILED;
139
140 return wv->ops->close( wv );
141 }
142
143 static UINT UPDATE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols )
144 {
145 MSIUPDATEVIEW *uv = (MSIUPDATEVIEW*)view;
146 MSIVIEW *wv;
147
148 TRACE("%p %p %p\n", uv, rows, cols );
149
150 wv = uv->wv;
151 if( !wv )
152 return ERROR_FUNCTION_FAILED;
153
154 return wv->ops->get_dimensions( wv, rows, cols );
155 }
156
157 static UINT UPDATE_get_column_info( struct tagMSIVIEW *view,
158 UINT n, LPWSTR *name, UINT *type, BOOL *temporary,
159 LPWSTR* table_name )
160 {
161 MSIUPDATEVIEW *uv = (MSIUPDATEVIEW*)view;
162 MSIVIEW *wv;
163
164 TRACE("%p %d %p %p %p %p\n", uv, n, name, type, temporary, table_name );
165
166 wv = uv->wv;
167 if( !wv )
168 return ERROR_FUNCTION_FAILED;
169
170 return wv->ops->get_column_info( wv, n, name, type, temporary, table_name );
171 }
172
173 static UINT UPDATE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
174 MSIRECORD *rec, UINT row )
175 {
176 MSIUPDATEVIEW *uv = (MSIUPDATEVIEW*)view;
177
178 TRACE("%p %d %p\n", uv, eModifyMode, rec );
179
180 return ERROR_FUNCTION_FAILED;
181 }
182
183 static UINT UPDATE_delete( struct tagMSIVIEW *view )
184 {
185 MSIUPDATEVIEW *uv = (MSIUPDATEVIEW*)view;
186 MSIVIEW *wv;
187
188 TRACE("%p\n", uv );
189
190 wv = uv->wv;
191 if( wv )
192 wv->ops->delete( wv );
193 msiobj_release( &uv->db->hdr );
194 msi_free( uv );
195
196 return ERROR_SUCCESS;
197 }
198
199 static UINT UPDATE_find_matching_rows( struct tagMSIVIEW *view, UINT col, UINT val, UINT *row, MSIITERHANDLE *handle )
200 {
201 TRACE("%p %d %d %p\n", view, col, val, *handle );
202
203 return ERROR_FUNCTION_FAILED;
204 }
205
206
207 static const MSIVIEWOPS update_ops =
208 {
209 UPDATE_fetch_int,
210 NULL,
211 NULL,
212 NULL,
213 NULL,
214 NULL,
215 UPDATE_execute,
216 UPDATE_close,
217 UPDATE_get_dimensions,
218 UPDATE_get_column_info,
219 UPDATE_modify,
220 UPDATE_delete,
221 UPDATE_find_matching_rows,
222 NULL,
223 NULL,
224 NULL,
225 NULL,
226 NULL,
227 };
228
229 UINT UPDATE_CreateView( MSIDATABASE *db, MSIVIEW **view, LPCWSTR table,
230 column_info *columns, struct expr *expr )
231 {
232 MSIUPDATEVIEW *uv = NULL;
233 UINT r;
234 MSIVIEW *tv = NULL, *sv = NULL, *wv = NULL;
235
236 TRACE("%p\n", uv );
237
238 r = TABLE_CreateView( db, table, &tv );
239 if( r != ERROR_SUCCESS )
240 return r;
241
242 if (expr)
243 {
244 /* add conditions first */
245 r = WHERE_CreateView( db, &wv, tv, expr );
246 if( r != ERROR_SUCCESS )
247 {
248 tv->ops->delete( tv );
249 return r;
250 }
251 }
252 else
253 wv = tv;
254
255 /* then select the columns we want */
256 r = SELECT_CreateView( db, &sv, wv, columns );
257 if( r != ERROR_SUCCESS )
258 {
259 wv->ops->delete( wv );
260 return r;
261 }
262
263 uv = msi_alloc_zero( sizeof *uv );
264 if( !uv )
265 {
266 wv->ops->delete( wv );
267 return ERROR_FUNCTION_FAILED;
268 }
269
270 /* fill the structure */
271 uv->view.ops = &update_ops;
272 msiobj_addref( &db->hdr );
273 uv->db = db;
274 uv->vals = columns;
275 uv->wv = sv;
276 *view = (MSIVIEW*) uv;
277
278 return ERROR_SUCCESS;
279 }