wined3d: Add extension detection to the GL format template table.
[wine] / dlls / 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 <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 tagMSIINSERTVIEW
42 {
43     MSIVIEW          view;
44     MSIVIEW         *table;
45     MSIDATABASE     *db;
46     BOOL             bIsTemp;
47     MSIVIEW         *sv;
48     column_info     *vals;
49 } MSIINSERTVIEW;
50
51 static UINT INSERT_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
52 {
53     MSIINSERTVIEW *iv = (MSIINSERTVIEW*)view;
54
55     TRACE("%p %d %d %p\n", iv, row, col, val );
56
57     return ERROR_FUNCTION_FAILED;
58 }
59
60 /*
61  * msi_query_merge_record
62  *
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
65  */
66 MSIRECORD *msi_query_merge_record( UINT fields, const column_info *vl, MSIRECORD *rec )
67 {
68     MSIRECORD *merged;
69     DWORD wildcard_count = 1, i;
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 %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);
96         }
97         vl = vl->next;
98     }
99
100     return merged;
101 err:
102     msiobj_release( &merged->hdr );
103     return NULL;
104 }
105
106 /* checks to see if the column order specified in the INSERT query
107  * matches the column order of the table
108  */
109 static BOOL msi_columns_in_order(MSIINSERTVIEW *iv, UINT col_count)
110 {
111     LPWSTR a, b = NULL;
112     UINT i;
113     int res;
114
115     for (i = 1; i <= col_count; i++)
116     {
117         iv->sv->ops->get_column_info(iv->sv, i, &a, NULL, NULL);
118         iv->table->ops->get_column_info(iv->table, i, &b, NULL, NULL);
119
120         res = lstrcmpW(a, b);
121         msi_free(a);
122         msi_free(b);
123
124         if (res != 0)
125             return FALSE;
126     }
127
128     return TRUE;
129 }
130
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
133  */
134 static UINT msi_arrange_record(MSIINSERTVIEW *iv, MSIRECORD **values)
135 {
136     MSIRECORD *padded;
137     UINT col_count, val_count;
138     UINT r, i, colidx;
139     LPWSTR a, b = NULL;
140     int res;
141
142     r = iv->table->ops->get_dimensions(iv->table, NULL, &col_count);
143     if (r != ERROR_SUCCESS)
144         return r;
145
146     val_count = MSI_RecordGetFieldCount(*values);
147
148     /* check to see if the columns are arranged already
149      * to avoid unnecessary copying
150      */
151     if (col_count == val_count && msi_columns_in_order(iv, col_count))
152         return ERROR_SUCCESS;
153
154     padded = MSI_CreateRecord(col_count);
155     if (!padded)
156         return ERROR_OUTOFMEMORY;
157
158     for (colidx = 1; colidx <= val_count; colidx++)
159     {
160         r = iv->sv->ops->get_column_info(iv->sv, colidx, &a, NULL, NULL);
161         if (r != ERROR_SUCCESS)
162             goto err;
163
164         for (i = 1; i <= col_count; i++)
165         {
166             r = iv->table->ops->get_column_info(iv->table, i, &b, NULL, NULL);
167             if (r != ERROR_SUCCESS)
168                 goto err;
169
170             res = lstrcmpW(a, b);
171             msi_free(b);
172
173             if (res == 0)
174             {
175                 MSI_RecordCopyField(*values, colidx, padded, i);
176                 break;
177             }
178         }
179
180         msi_free(a);
181     }
182
183     msiobj_release(&(*values)->hdr);
184     *values = padded;
185
186     return ERROR_SUCCESS;
187
188 err:
189     msiobj_release(&padded->hdr);
190     return r;
191 }
192
193 static BOOL row_has_null_primary_keys(MSIINSERTVIEW *iv, MSIRECORD *row)
194 {
195     UINT r, i, col_count, type;
196
197     r = iv->table->ops->get_dimensions( iv->table, NULL, &col_count );
198     if (r != ERROR_SUCCESS)
199         return FALSE;
200
201     for (i = 1; i <= col_count; i++)
202     {
203         r = iv->table->ops->get_column_info(iv->table, i, NULL, &type, NULL);
204         if (r != ERROR_SUCCESS)
205             return FALSE;
206
207         if (!(type & MSITYPE_KEY))
208             continue;
209
210         if (MSI_RecordIsNull(row, i))
211             return TRUE;
212     }
213
214     return FALSE;
215 }
216
217 static UINT INSERT_execute( struct tagMSIVIEW *view, MSIRECORD *record )
218 {
219     MSIINSERTVIEW *iv = (MSIINSERTVIEW*)view;
220     UINT r, row = -1, col_count = 0;
221     MSIVIEW *sv;
222     MSIRECORD *values = NULL;
223
224     TRACE("%p %p\n", iv, record );
225
226     sv = iv->sv;
227     if( !sv )
228         return ERROR_FUNCTION_FAILED;
229
230     r = sv->ops->execute( sv, 0 );
231     TRACE("sv execute returned %x\n", r);
232     if( r )
233         return r;
234
235     r = sv->ops->get_dimensions( sv, NULL, &col_count );
236     if( r )
237         goto err;
238
239     /*
240      * Merge the wildcard values into the list of values provided
241      * in the query, and create a record containing both.
242      */
243     values = msi_query_merge_record( col_count, iv->vals, record );
244     if( !values )
245         goto err;
246
247     r = msi_arrange_record( iv, &values );
248     if( r != ERROR_SUCCESS )
249         goto err;
250
251     /* rows with NULL primary keys are inserted at the beginning of the table */
252     if( row_has_null_primary_keys( iv, values ) )
253         row = 0;
254
255     r = iv->table->ops->insert_row( iv->table, values, row, iv->bIsTemp );
256
257 err:
258     if( values )
259         msiobj_release( &values->hdr );
260
261     return r;
262 }
263
264
265 static UINT INSERT_close( struct tagMSIVIEW *view )
266 {
267     MSIINSERTVIEW *iv = (MSIINSERTVIEW*)view;
268     MSIVIEW *sv;
269
270     TRACE("%p\n", iv);
271
272     sv = iv->sv;
273     if( !sv )
274         return ERROR_FUNCTION_FAILED;
275
276     return sv->ops->close( sv );
277 }
278
279 static UINT INSERT_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols )
280 {
281     MSIINSERTVIEW *iv = (MSIINSERTVIEW*)view;
282     MSIVIEW *sv;
283
284     TRACE("%p %p %p\n", iv, rows, cols );
285
286     sv = iv->sv;
287     if( !sv )
288         return ERROR_FUNCTION_FAILED;
289
290     return sv->ops->get_dimensions( sv, rows, cols );
291 }
292
293 static UINT INSERT_get_column_info( struct tagMSIVIEW *view,
294                 UINT n, LPWSTR *name, UINT *type, BOOL *temporary )
295 {
296     MSIINSERTVIEW *iv = (MSIINSERTVIEW*)view;
297     MSIVIEW *sv;
298
299     TRACE("%p %d %p %p %p\n", iv, n, name, type, temporary );
300
301     sv = iv->sv;
302     if( !sv )
303         return ERROR_FUNCTION_FAILED;
304
305     return sv->ops->get_column_info( sv, n, name, type, temporary );
306 }
307
308 static UINT INSERT_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode, MSIRECORD *rec, UINT row)
309 {
310     MSIINSERTVIEW *iv = (MSIINSERTVIEW*)view;
311
312     TRACE("%p %d %p\n", iv, eModifyMode, rec );
313
314     return ERROR_FUNCTION_FAILED;
315 }
316
317 static UINT INSERT_delete( struct tagMSIVIEW *view )
318 {
319     MSIINSERTVIEW *iv = (MSIINSERTVIEW*)view;
320     MSIVIEW *sv;
321
322     TRACE("%p\n", iv );
323
324     sv = iv->sv;
325     if( sv )
326         sv->ops->delete( sv );
327     msiobj_release( &iv->db->hdr );
328     msi_free( iv );
329
330     return ERROR_SUCCESS;
331 }
332
333 static UINT INSERT_find_matching_rows( struct tagMSIVIEW *view, UINT col,
334     UINT val, UINT *row, MSIITERHANDLE *handle )
335 {
336     TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
337
338     return ERROR_FUNCTION_FAILED;
339 }
340
341
342 static const MSIVIEWOPS insert_ops =
343 {
344     INSERT_fetch_int,
345     NULL,
346     NULL,
347     NULL,
348     NULL,
349     NULL,
350     INSERT_execute,
351     INSERT_close,
352     INSERT_get_dimensions,
353     INSERT_get_column_info,
354     INSERT_modify,
355     INSERT_delete,
356     INSERT_find_matching_rows,
357     NULL,
358     NULL,
359     NULL,
360     NULL,
361     NULL,
362     NULL,
363 };
364
365 static UINT count_column_info( const column_info *ci )
366 {
367     UINT n = 0;
368     for ( ; ci; ci = ci->next )
369         n++;
370     return n;
371 }
372
373 UINT INSERT_CreateView( MSIDATABASE *db, MSIVIEW **view, LPCWSTR table,
374                         column_info *columns, column_info *values, BOOL temp )
375 {
376     MSIINSERTVIEW *iv = NULL;
377     UINT r;
378     MSIVIEW *tv = NULL, *sv = NULL;
379
380     TRACE("%p\n", iv );
381
382     /* there should be one value for each column */
383     if ( count_column_info( columns ) != count_column_info(values) )
384         return ERROR_BAD_QUERY_SYNTAX;
385
386     r = TABLE_CreateView( db, table, &tv );
387     if( r != ERROR_SUCCESS )
388         return r;
389
390     r = SELECT_CreateView( db, &sv, tv, columns );
391     if( r != ERROR_SUCCESS )
392     {
393         if( tv )
394             tv->ops->delete( tv );
395         return r;
396     }
397
398     iv = msi_alloc_zero( sizeof *iv );
399     if( !iv )
400         return ERROR_FUNCTION_FAILED;
401
402     /* fill the structure */
403     iv->view.ops = &insert_ops;
404     msiobj_addref( &db->hdr );
405     iv->table = tv;
406     iv->db = db;
407     iv->vals = values;
408     iv->bIsTemp = temp;
409     iv->sv = sv;
410     *view = (MSIVIEW*) iv;
411
412     return ERROR_SUCCESS;
413 }