40dba1a9d4f242f55929cceca0e1b2af7e43de8f
[reactos.git] / reactos / lib / 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 value_list *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, value_list *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 n, type, val, r, row, 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 row = -1;
140 r = sv->ops->insert_row( sv, &row );
141 TRACE("insert_row returned %x\n", r);
142 if( r )
143 goto err;
144
145 for( n = 1; n <= col_count; n++ )
146 {
147 r = sv->ops->get_column_info( sv, n, NULL, &type );
148 if( r )
149 break;
150
151 if( type & MSITYPE_STRING )
152 {
153 const WCHAR *str = MSI_RecordGetString( values, n );
154 val = msi_addstringW( iv->db->strings, 0, str, -1, 1 );
155 }
156 else
157 {
158 val = MSI_RecordGetInteger( values, n );
159 val |= 0x8000;
160 }
161 r = sv->ops->set_int( sv, row, n, val );
162 if( r )
163 break;
164 }
165
166 err:
167 if( values )
168 msiobj_release( &values->hdr );
169
170 return ERROR_SUCCESS;
171 }
172
173
174 static UINT INSERT_close( struct tagMSIVIEW *view )
175 {
176 MSIINSERTVIEW *iv = (MSIINSERTVIEW*)view;
177 MSIVIEW *sv;
178
179 TRACE("%p\n", iv);
180
181 sv = iv->sv;
182 if( !sv )
183 return ERROR_FUNCTION_FAILED;
184
185 return sv->ops->close( sv );
186 }
187
188 static UINT INSERT_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols )
189 {
190 MSIINSERTVIEW *iv = (MSIINSERTVIEW*)view;
191 MSIVIEW *sv;
192
193 TRACE("%p %p %p\n", iv, rows, cols );
194
195 sv = iv->sv;
196 if( !sv )
197 return ERROR_FUNCTION_FAILED;
198
199 return sv->ops->get_dimensions( sv, rows, cols );
200 }
201
202 static UINT INSERT_get_column_info( struct tagMSIVIEW *view,
203 UINT n, LPWSTR *name, UINT *type )
204 {
205 MSIINSERTVIEW *iv = (MSIINSERTVIEW*)view;
206 MSIVIEW *sv;
207
208 TRACE("%p %d %p %p\n", iv, n, name, type );
209
210 sv = iv->sv;
211 if( !sv )
212 return ERROR_FUNCTION_FAILED;
213
214 return sv->ops->get_column_info( sv, n, name, type );
215 }
216
217 static UINT INSERT_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode, MSIRECORD *rec)
218 {
219 MSIINSERTVIEW *iv = (MSIINSERTVIEW*)view;
220
221 TRACE("%p %d %p\n", iv, eModifyMode, rec );
222
223 return ERROR_FUNCTION_FAILED;
224 }
225
226 static UINT INSERT_delete( struct tagMSIVIEW *view )
227 {
228 MSIINSERTVIEW *iv = (MSIINSERTVIEW*)view;
229 MSIVIEW *sv;
230
231 TRACE("%p\n", iv );
232
233 sv = iv->sv;
234 if( sv )
235 sv->ops->delete( sv );
236 delete_value_list( iv->vals );
237 msiobj_release( &iv->db->hdr );
238 HeapFree( GetProcessHeap(), 0, iv );
239
240 return ERROR_SUCCESS;
241 }
242
243
244 MSIVIEWOPS insert_ops =
245 {
246 INSERT_fetch_int,
247 NULL,
248 NULL,
249 NULL,
250 INSERT_execute,
251 INSERT_close,
252 INSERT_get_dimensions,
253 INSERT_get_column_info,
254 INSERT_modify,
255 INSERT_delete
256 };
257
258 UINT INSERT_CreateView( MSIDATABASE *db, MSIVIEW **view, LPWSTR table,
259 string_list *columns, value_list *values, BOOL temp )
260 {
261 MSIINSERTVIEW *iv = NULL;
262 UINT r;
263 MSIVIEW *tv = NULL, *sv = NULL;
264
265 TRACE("%p\n", iv );
266
267 r = TABLE_CreateView( db, table, &tv );
268 if( r != ERROR_SUCCESS )
269 return r;
270
271 r = SELECT_CreateView( db, &sv, tv, columns );
272 if( r != ERROR_SUCCESS )
273 {
274 if( tv )
275 tv->ops->delete( tv );
276 return r;
277 }
278
279 iv = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof *iv );
280 if( !iv )
281 return ERROR_FUNCTION_FAILED;
282
283 /* fill the structure */
284 iv->view.ops = &insert_ops;
285 msiobj_addref( &db->hdr );
286 iv->db = db;
287 iv->vals = values;
288 iv->bIsTemp = temp;
289 iv->sv = sv;
290 *view = (MSIVIEW*) iv;
291
292 return ERROR_SUCCESS;
293 }