d9a4ad82db4de87c4784e0ec808c4a8a836ab2c6
[reactos.git] / dll / win32 / 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 "msipriv.h"
22
23 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
24
25 /* below is the query interface to a table */
26
27 typedef struct tagMSIINSERTVIEW
28 {
29 MSIVIEW view;
30 MSIVIEW *table;
31 MSIDATABASE *db;
32 BOOL bIsTemp;
33 MSIVIEW *sv;
34 column_info *vals;
35 } MSIINSERTVIEW;
36
37 static UINT INSERT_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
38 {
39 MSIINSERTVIEW *iv = (MSIINSERTVIEW*)view;
40
41 TRACE("%p %d %d %p\n", iv, row, col, val );
42
43 return ERROR_FUNCTION_FAILED;
44 }
45
46 /*
47 * msi_query_merge_record
48 *
49 * Merge a value_list and a record to create a second record.
50 * Replace wildcard entries in the valuelist with values from the record
51 */
52 MSIRECORD *msi_query_merge_record( UINT fields, const column_info *vl, MSIRECORD *rec )
53 {
54 MSIRECORD *merged;
55 DWORD wildcard_count = 1, i;
56
57 merged = MSI_CreateRecord( fields );
58 for( i=1; i <= fields; i++ )
59 {
60 if( !vl )
61 {
62 TRACE("Not enough elements in the list to insert\n");
63 goto err;
64 }
65 switch( vl->val->type )
66 {
67 case EXPR_SVAL:
68 TRACE("field %d -> %s\n", i, debugstr_w(vl->val->u.sval));
69 MSI_RecordSetStringW( merged, i, vl->val->u.sval );
70 break;
71 case EXPR_IVAL:
72 MSI_RecordSetInteger( merged, i, vl->val->u.ival );
73 break;
74 case EXPR_WILDCARD:
75 if( !rec )
76 goto err;
77 MSI_RecordCopyField( rec, wildcard_count, merged, i );
78 wildcard_count++;
79 break;
80 default:
81 ERR("Unknown expression type %d\n", vl->val->type);
82 }
83 vl = vl->next;
84 }
85
86 return merged;
87 err:
88 msiobj_release( &merged->hdr );
89 return NULL;
90 }
91
92 /* checks to see if the column order specified in the INSERT query
93 * matches the column order of the table
94 */
95 static BOOL msi_columns_in_order(MSIINSERTVIEW *iv, UINT col_count)
96 {
97 LPCWSTR a, b;
98 UINT i;
99
100 for (i = 1; i <= col_count; i++)
101 {
102 iv->sv->ops->get_column_info(iv->sv, i, &a, NULL, NULL, NULL);
103 iv->table->ops->get_column_info(iv->table, i, &b, NULL, NULL, NULL);
104
105 if (strcmpW( a, b )) return FALSE;
106 }
107 return TRUE;
108 }
109
110 /* rearranges the data in the record to be inserted based on column order,
111 * and pads the record for any missing columns in the INSERT query
112 */
113 static UINT msi_arrange_record(MSIINSERTVIEW *iv, MSIRECORD **values)
114 {
115 MSIRECORD *padded;
116 UINT col_count, val_count;
117 UINT r, i, colidx;
118 LPCWSTR a, b;
119
120 r = iv->table->ops->get_dimensions(iv->table, NULL, &col_count);
121 if (r != ERROR_SUCCESS)
122 return r;
123
124 val_count = MSI_RecordGetFieldCount(*values);
125
126 /* check to see if the columns are arranged already
127 * to avoid unnecessary copying
128 */
129 if (col_count == val_count && msi_columns_in_order(iv, col_count))
130 return ERROR_SUCCESS;
131
132 padded = MSI_CreateRecord(col_count);
133 if (!padded)
134 return ERROR_OUTOFMEMORY;
135
136 for (colidx = 1; colidx <= val_count; colidx++)
137 {
138 r = iv->sv->ops->get_column_info(iv->sv, colidx, &a, NULL, NULL, NULL);
139 if (r != ERROR_SUCCESS)
140 goto err;
141
142 for (i = 1; i <= col_count; i++)
143 {
144 r = iv->table->ops->get_column_info(iv->table, i, &b, NULL,
145 NULL, NULL);
146 if (r != ERROR_SUCCESS)
147 goto err;
148
149 if (!strcmpW( a, b ))
150 {
151 MSI_RecordCopyField(*values, colidx, padded, i);
152 break;
153 }
154 }
155 }
156 msiobj_release(&(*values)->hdr);
157 *values = padded;
158 return ERROR_SUCCESS;
159
160 err:
161 msiobj_release(&padded->hdr);
162 return r;
163 }
164
165 static BOOL row_has_null_primary_keys(MSIINSERTVIEW *iv, MSIRECORD *row)
166 {
167 UINT r, i, col_count, type;
168
169 r = iv->table->ops->get_dimensions( iv->table, NULL, &col_count );
170 if (r != ERROR_SUCCESS)
171 return FALSE;
172
173 for (i = 1; i <= col_count; i++)
174 {
175 r = iv->table->ops->get_column_info(iv->table, i, NULL, &type,
176 NULL, NULL);
177 if (r != ERROR_SUCCESS)
178 return FALSE;
179
180 if (!(type & MSITYPE_KEY))
181 continue;
182
183 if (MSI_RecordIsNull(row, i))
184 return TRUE;
185 }
186
187 return FALSE;
188 }
189
190 static UINT INSERT_execute( struct tagMSIVIEW *view, MSIRECORD *record )
191 {
192 MSIINSERTVIEW *iv = (MSIINSERTVIEW*)view;
193 UINT r, row = -1, col_count = 0;
194 MSIVIEW *sv;
195 MSIRECORD *values = NULL;
196
197 TRACE("%p %p\n", iv, record );
198
199 sv = iv->sv;
200 if( !sv )
201 return ERROR_FUNCTION_FAILED;
202
203 r = sv->ops->execute( sv, 0 );
204 TRACE("sv execute returned %x\n", r);
205 if( r )
206 return r;
207
208 r = sv->ops->get_dimensions( sv, NULL, &col_count );
209 if( r )
210 goto err;
211
212 /*
213 * Merge the wildcard values into the list of values provided
214 * in the query, and create a record containing both.
215 */
216 values = msi_query_merge_record( col_count, iv->vals, record );
217 if( !values )
218 goto err;
219
220 r = msi_arrange_record( iv, &values );
221 if( r != ERROR_SUCCESS )
222 goto err;
223
224 /* rows with NULL primary keys are inserted at the beginning of the table */
225 if( row_has_null_primary_keys( iv, values ) )
226 row = 0;
227
228 r = iv->table->ops->insert_row( iv->table, values, row, iv->bIsTemp );
229
230 err:
231 if( values )
232 msiobj_release( &values->hdr );
233
234 return r;
235 }
236
237
238 static UINT INSERT_close( struct tagMSIVIEW *view )
239 {
240 MSIINSERTVIEW *iv = (MSIINSERTVIEW*)view;
241 MSIVIEW *sv;
242
243 TRACE("%p\n", iv);
244
245 sv = iv->sv;
246 if( !sv )
247 return ERROR_FUNCTION_FAILED;
248
249 return sv->ops->close( sv );
250 }
251
252 static UINT INSERT_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols )
253 {
254 MSIINSERTVIEW *iv = (MSIINSERTVIEW*)view;
255 MSIVIEW *sv;
256
257 TRACE("%p %p %p\n", iv, rows, cols );
258
259 sv = iv->sv;
260 if( !sv )
261 return ERROR_FUNCTION_FAILED;
262
263 return sv->ops->get_dimensions( sv, rows, cols );
264 }
265
266 static UINT INSERT_get_column_info( struct tagMSIVIEW *view, UINT n, LPCWSTR *name,
267 UINT *type, BOOL *temporary, LPCWSTR *table_name )
268 {
269 MSIINSERTVIEW *iv = (MSIINSERTVIEW*)view;
270 MSIVIEW *sv;
271
272 TRACE("%p %d %p %p %p %p\n", iv, n, name, type, temporary, table_name );
273
274 sv = iv->sv;
275 if( !sv )
276 return ERROR_FUNCTION_FAILED;
277
278 return sv->ops->get_column_info( sv, n, name, type, temporary, table_name );
279 }
280
281 static UINT INSERT_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode, MSIRECORD *rec, UINT row)
282 {
283 MSIINSERTVIEW *iv = (MSIINSERTVIEW*)view;
284
285 TRACE("%p %d %p\n", iv, eModifyMode, rec );
286
287 return ERROR_FUNCTION_FAILED;
288 }
289
290 static UINT INSERT_delete( struct tagMSIVIEW *view )
291 {
292 MSIINSERTVIEW *iv = (MSIINSERTVIEW*)view;
293 MSIVIEW *sv;
294
295 TRACE("%p\n", iv );
296
297 sv = iv->sv;
298 if( sv )
299 sv->ops->delete( sv );
300 msiobj_release( &iv->db->hdr );
301 msi_free( iv );
302
303 return ERROR_SUCCESS;
304 }
305
306 static UINT INSERT_find_matching_rows( struct tagMSIVIEW *view, UINT col,
307 UINT val, UINT *row, MSIITERHANDLE *handle )
308 {
309 TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
310
311 return ERROR_FUNCTION_FAILED;
312 }
313
314
315 static const MSIVIEWOPS insert_ops =
316 {
317 INSERT_fetch_int,
318 NULL,
319 NULL,
320 NULL,
321 NULL,
322 NULL,
323 INSERT_execute,
324 INSERT_close,
325 INSERT_get_dimensions,
326 INSERT_get_column_info,
327 INSERT_modify,
328 INSERT_delete,
329 INSERT_find_matching_rows,
330 NULL,
331 NULL,
332 NULL,
333 NULL,
334 NULL,
335 NULL,
336 };
337
338 static UINT count_column_info( const column_info *ci )
339 {
340 UINT n = 0;
341 for ( ; ci; ci = ci->next )
342 n++;
343 return n;
344 }
345
346 UINT INSERT_CreateView( MSIDATABASE *db, MSIVIEW **view, LPCWSTR table,
347 column_info *columns, column_info *values, BOOL temp )
348 {
349 MSIINSERTVIEW *iv = NULL;
350 UINT r;
351 MSIVIEW *tv = NULL, *sv = NULL;
352
353 TRACE("%p\n", iv );
354
355 /* there should be one value for each column */
356 if ( count_column_info( columns ) != count_column_info(values) )
357 return ERROR_BAD_QUERY_SYNTAX;
358
359 r = TABLE_CreateView( db, table, &tv );
360 if( r != ERROR_SUCCESS )
361 return r;
362
363 r = SELECT_CreateView( db, &sv, tv, columns );
364 if( r != ERROR_SUCCESS )
365 {
366 if( tv )
367 tv->ops->delete( tv );
368 return r;
369 }
370
371 iv = msi_alloc_zero( sizeof *iv );
372 if( !iv )
373 return ERROR_FUNCTION_FAILED;
374
375 /* fill the structure */
376 iv->view.ops = &insert_ops;
377 msiobj_addref( &db->hdr );
378 iv->table = tv;
379 iv->db = db;
380 iv->vals = values;
381 iv->bIsTemp = temp;
382 iv->sv = sv;
383 *view = (MSIVIEW*) iv;
384
385 return ERROR_SUCCESS;
386 }