Release 1.1.37.
[wine/gsoc-2012-control.git] / dlls / msi / insert.c
blob61807e9534ebc96c077ade64ea9c61e10d560820
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2004 Mike McCormack for CodeWeavers
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.
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.
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
21 #include <stdarg.h>
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"
34 #include "query.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
39 /* below is the query interface to a table */
41 typedef struct tagMSIINSERTVIEW
43 MSIVIEW view;
44 MSIVIEW *table;
45 MSIDATABASE *db;
46 BOOL bIsTemp;
47 MSIVIEW *sv;
48 column_info *vals;
49 } MSIINSERTVIEW;
51 static UINT INSERT_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
53 MSIINSERTVIEW *iv = (MSIINSERTVIEW*)view;
55 TRACE("%p %d %d %p\n", iv, row, col, val );
57 return ERROR_FUNCTION_FAILED;
61 * msi_query_merge_record
63 * Merge a value_list and a record to create a second record.
64 * Replace wildcard entries in the valuelist with values from the record
66 MSIRECORD *msi_query_merge_record( UINT fields, const column_info *vl, MSIRECORD *rec )
68 MSIRECORD *merged;
69 DWORD wildcard_count = 1, i;
71 merged = MSI_CreateRecord( fields );
72 for( i=1; i <= fields; i++ )
74 if( !vl )
76 TRACE("Not enough elements in the list to insert\n");
77 goto err;
79 switch( vl->val->type )
81 case EXPR_SVAL:
82 TRACE("field %d -> %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 MSI_RecordCopyField( rec, wildcard_count, merged, i );
92 wildcard_count++;
93 break;
94 default:
95 ERR("Unknown expression type %d\n", vl->val->type);
97 vl = vl->next;
100 return merged;
101 err:
102 msiobj_release( &merged->hdr );
103 return NULL;
106 /* checks to see if the column order specified in the INSERT query
107 * matches the column order of the table
109 static BOOL msi_columns_in_order(MSIINSERTVIEW *iv, UINT col_count)
111 LPWSTR a, b = NULL;
112 UINT i;
113 int res;
115 for (i = 1; i <= col_count; i++)
117 iv->sv->ops->get_column_info(iv->sv, i, &a, NULL, NULL, NULL);
118 iv->table->ops->get_column_info(iv->table, i, &b, NULL, NULL, NULL);
120 res = lstrcmpW(a, b);
121 msi_free(a);
122 msi_free(b);
124 if (res != 0)
125 return FALSE;
128 return TRUE;
131 /* rearranges the data in the record to be inserted based on column order,
132 * and pads the record for any missing columns in the INSERT query
134 static UINT msi_arrange_record(MSIINSERTVIEW *iv, MSIRECORD **values)
136 MSIRECORD *padded;
137 UINT col_count, val_count;
138 UINT r, i, colidx;
139 LPWSTR a, b = NULL;
140 int res;
142 r = iv->table->ops->get_dimensions(iv->table, NULL, &col_count);
143 if (r != ERROR_SUCCESS)
144 return r;
146 val_count = MSI_RecordGetFieldCount(*values);
148 /* check to see if the columns are arranged already
149 * to avoid unnecessary copying
151 if (col_count == val_count && msi_columns_in_order(iv, col_count))
152 return ERROR_SUCCESS;
154 padded = MSI_CreateRecord(col_count);
155 if (!padded)
156 return ERROR_OUTOFMEMORY;
158 for (colidx = 1; colidx <= val_count; colidx++)
160 r = iv->sv->ops->get_column_info(iv->sv, colidx, &a, NULL, NULL, NULL);
161 if (r != ERROR_SUCCESS)
162 goto err;
164 for (i = 1; i <= col_count; i++)
166 r = iv->table->ops->get_column_info(iv->table, i, &b, NULL,
167 NULL, NULL);
168 if (r != ERROR_SUCCESS)
169 goto err;
171 res = lstrcmpW(a, b);
172 msi_free(b);
174 if (res == 0)
176 MSI_RecordCopyField(*values, colidx, padded, i);
177 break;
181 msi_free(a);
184 msiobj_release(&(*values)->hdr);
185 *values = padded;
187 return ERROR_SUCCESS;
189 err:
190 msiobj_release(&padded->hdr);
191 return r;
194 static BOOL row_has_null_primary_keys(MSIINSERTVIEW *iv, MSIRECORD *row)
196 UINT r, i, col_count, type;
198 r = iv->table->ops->get_dimensions( iv->table, NULL, &col_count );
199 if (r != ERROR_SUCCESS)
200 return FALSE;
202 for (i = 1; i <= col_count; i++)
204 r = iv->table->ops->get_column_info(iv->table, i, NULL, &type,
205 NULL, NULL);
206 if (r != ERROR_SUCCESS)
207 return FALSE;
209 if (!(type & MSITYPE_KEY))
210 continue;
212 if (MSI_RecordIsNull(row, i))
213 return TRUE;
216 return FALSE;
219 static UINT INSERT_execute( struct tagMSIVIEW *view, MSIRECORD *record )
221 MSIINSERTVIEW *iv = (MSIINSERTVIEW*)view;
222 UINT r, row = -1, col_count = 0;
223 MSIVIEW *sv;
224 MSIRECORD *values = NULL;
226 TRACE("%p %p\n", iv, record );
228 sv = iv->sv;
229 if( !sv )
230 return ERROR_FUNCTION_FAILED;
232 r = sv->ops->execute( sv, 0 );
233 TRACE("sv execute returned %x\n", r);
234 if( r )
235 return r;
237 r = sv->ops->get_dimensions( sv, NULL, &col_count );
238 if( r )
239 goto err;
242 * Merge the wildcard values into the list of values provided
243 * in the query, and create a record containing both.
245 values = msi_query_merge_record( col_count, iv->vals, record );
246 if( !values )
247 goto err;
249 r = msi_arrange_record( iv, &values );
250 if( r != ERROR_SUCCESS )
251 goto err;
253 /* rows with NULL primary keys are inserted at the beginning of the table */
254 if( row_has_null_primary_keys( iv, values ) )
255 row = 0;
257 r = iv->table->ops->insert_row( iv->table, values, row, iv->bIsTemp );
259 err:
260 if( values )
261 msiobj_release( &values->hdr );
263 return r;
267 static UINT INSERT_close( struct tagMSIVIEW *view )
269 MSIINSERTVIEW *iv = (MSIINSERTVIEW*)view;
270 MSIVIEW *sv;
272 TRACE("%p\n", iv);
274 sv = iv->sv;
275 if( !sv )
276 return ERROR_FUNCTION_FAILED;
278 return sv->ops->close( sv );
281 static UINT INSERT_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols )
283 MSIINSERTVIEW *iv = (MSIINSERTVIEW*)view;
284 MSIVIEW *sv;
286 TRACE("%p %p %p\n", iv, rows, cols );
288 sv = iv->sv;
289 if( !sv )
290 return ERROR_FUNCTION_FAILED;
292 return sv->ops->get_dimensions( sv, rows, cols );
295 static UINT INSERT_get_column_info( struct tagMSIVIEW *view,
296 UINT n, LPWSTR *name, UINT *type, BOOL *temporary,
297 LPWSTR *table_name)
299 MSIINSERTVIEW *iv = (MSIINSERTVIEW*)view;
300 MSIVIEW *sv;
302 TRACE("%p %d %p %p %p %p\n", iv, n, name, type, temporary, table_name );
304 sv = iv->sv;
305 if( !sv )
306 return ERROR_FUNCTION_FAILED;
308 return sv->ops->get_column_info( sv, n, name, type, temporary, table_name );
311 static UINT INSERT_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode, MSIRECORD *rec, UINT row)
313 MSIINSERTVIEW *iv = (MSIINSERTVIEW*)view;
315 TRACE("%p %d %p\n", iv, eModifyMode, rec );
317 return ERROR_FUNCTION_FAILED;
320 static UINT INSERT_delete( struct tagMSIVIEW *view )
322 MSIINSERTVIEW *iv = (MSIINSERTVIEW*)view;
323 MSIVIEW *sv;
325 TRACE("%p\n", iv );
327 sv = iv->sv;
328 if( sv )
329 sv->ops->delete( sv );
330 msiobj_release( &iv->db->hdr );
331 msi_free( iv );
333 return ERROR_SUCCESS;
336 static UINT INSERT_find_matching_rows( struct tagMSIVIEW *view, UINT col,
337 UINT val, UINT *row, MSIITERHANDLE *handle )
339 TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
341 return ERROR_FUNCTION_FAILED;
345 static const MSIVIEWOPS insert_ops =
347 INSERT_fetch_int,
348 NULL,
349 NULL,
350 NULL,
351 NULL,
352 NULL,
353 INSERT_execute,
354 INSERT_close,
355 INSERT_get_dimensions,
356 INSERT_get_column_info,
357 INSERT_modify,
358 INSERT_delete,
359 INSERT_find_matching_rows,
360 NULL,
361 NULL,
362 NULL,
363 NULL,
364 NULL,
365 NULL,
368 static UINT count_column_info( const column_info *ci )
370 UINT n = 0;
371 for ( ; ci; ci = ci->next )
372 n++;
373 return n;
376 UINT INSERT_CreateView( MSIDATABASE *db, MSIVIEW **view, LPCWSTR table,
377 column_info *columns, column_info *values, BOOL temp )
379 MSIINSERTVIEW *iv = NULL;
380 UINT r;
381 MSIVIEW *tv = NULL, *sv = NULL;
383 TRACE("%p\n", iv );
385 /* there should be one value for each column */
386 if ( count_column_info( columns ) != count_column_info(values) )
387 return ERROR_BAD_QUERY_SYNTAX;
389 r = TABLE_CreateView( db, table, &tv );
390 if( r != ERROR_SUCCESS )
391 return r;
393 r = SELECT_CreateView( db, &sv, tv, columns );
394 if( r != ERROR_SUCCESS )
396 if( tv )
397 tv->ops->delete( tv );
398 return r;
401 iv = msi_alloc_zero( sizeof *iv );
402 if( !iv )
403 return ERROR_FUNCTION_FAILED;
405 /* fill the structure */
406 iv->view.ops = &insert_ops;
407 msiobj_addref( &db->hdr );
408 iv->table = tv;
409 iv->db = db;
410 iv->vals = values;
411 iv->bIsTemp = temp;
412 iv->sv = sv;
413 *view = (MSIVIEW*) iv;
415 return ERROR_SUCCESS;