ole32: Remove some assertions in the stuctured storage code by
[wine] / dlls / msi / create.c
1 /*
2  * Implementation of the Microsoft Installer (msi.dll)
3  *
4  * Copyright 2002-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 tagMSICREATEVIEW
42 {
43     MSIVIEW          view;
44     MSIDATABASE     *db;
45     LPWSTR           name;
46     BOOL             bIsTemp;
47     column_info     *col_info;
48 } MSICREATEVIEW;
49
50 static UINT CREATE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
51 {
52     MSICREATEVIEW *cv = (MSICREATEVIEW*)view;
53
54     TRACE("%p %d %d %p\n", cv, row, col, val );
55
56     return ERROR_FUNCTION_FAILED;
57 }
58
59 static UINT CREATE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
60 {
61     MSICREATEVIEW *cv = (MSICREATEVIEW*)view;
62     column_info *col;
63     UINT r, nField;
64     static const WCHAR szTables[] =  { '_','T','a','b','l','e','s',0 };
65     static const WCHAR szColumns[] = { '_','C','o','l','u','m','n','s',0 };
66     MSIVIEW *tv = NULL;
67     MSIRECORD *rec = NULL;
68
69     TRACE("%p Table %s (%s)\n", cv, debugstr_w(cv->name), 
70           cv->bIsTemp?"temporary":"permanent");
71
72     /* only add tables that don't exist already */
73     if( TABLE_Exists(cv->db, cv->name ) )
74         return ERROR_BAD_QUERY_SYNTAX;
75
76     r = TABLE_CreateView( cv->db, szTables, &tv );
77     TRACE("CreateView returned %x\n", r);
78     if( r )
79         return r;
80
81     r = tv->ops->execute( tv, 0 );
82     TRACE("tv execute returned %x\n", r);
83     if( r )
84         goto err;
85
86     rec = MSI_CreateRecord( 1 );
87     if( !rec )
88         goto err;
89
90     r = MSI_RecordSetStringW( rec, 1, cv->name );
91     if( r )
92         goto err;
93
94     r = tv->ops->insert_row( tv, rec );
95     TRACE("insert_row returned %x\n", r);
96     if( r )
97         goto err;
98
99     tv->ops->delete( tv );
100     tv = NULL;
101
102     msiobj_release( &rec->hdr );
103
104     /* add each column to the _Columns table */
105     r = TABLE_CreateView( cv->db, szColumns, &tv );
106     if( r )
107         return r;
108
109     r = tv->ops->execute( tv, 0 );
110     TRACE("tv execute returned %x\n", r);
111     if( r )
112         goto err;
113
114     rec = MSI_CreateRecord( 4 );
115     if( !rec )
116         goto err;
117
118     r = MSI_RecordSetStringW( rec, 1, cv->name );
119     if( r )
120         goto err;
121
122     /*
123      * need to set the table, column number, col name and type
124      * for each column we enter in the table
125      */
126     nField = 1;
127     for( col = cv->col_info; col; col = col->next )
128     {
129         r = MSI_RecordSetInteger( rec, 2, nField );
130         if( r )
131             goto err;
132
133         r = MSI_RecordSetStringW( rec, 3, col->column );
134         if( r )
135             goto err;
136
137         r = MSI_RecordSetInteger( rec, 4, col->type );
138         if( r )
139             goto err;
140
141         r = tv->ops->insert_row( tv, rec );
142         if( r )
143             goto err;
144
145         nField++;
146     }
147     if( !col )
148         r = ERROR_SUCCESS;
149
150 err:
151     if (rec)
152         msiobj_release( &rec->hdr );
153     /* FIXME: remove values from the string table on error */
154     if( tv )
155         tv->ops->delete( tv );
156     return r;
157 }
158
159 static UINT CREATE_close( struct tagMSIVIEW *view )
160 {
161     MSICREATEVIEW *cv = (MSICREATEVIEW*)view;
162
163     TRACE("%p\n", cv);
164
165     return ERROR_SUCCESS;
166 }
167
168 static UINT CREATE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols )
169 {
170     MSICREATEVIEW *cv = (MSICREATEVIEW*)view;
171
172     TRACE("%p %p %p\n", cv, rows, cols );
173
174     return ERROR_FUNCTION_FAILED;
175 }
176
177 static UINT CREATE_get_column_info( struct tagMSIVIEW *view,
178                 UINT n, LPWSTR *name, UINT *type )
179 {
180     MSICREATEVIEW *cv = (MSICREATEVIEW*)view;
181
182     TRACE("%p %d %p %p\n", cv, n, name, type );
183
184     return ERROR_FUNCTION_FAILED;
185 }
186
187 static UINT CREATE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
188                 MSIRECORD *rec)
189 {
190     MSICREATEVIEW *cv = (MSICREATEVIEW*)view;
191
192     TRACE("%p %d %p\n", cv, eModifyMode, rec );
193
194     return ERROR_FUNCTION_FAILED;
195 }
196
197 static UINT CREATE_delete( struct tagMSIVIEW *view )
198 {
199     MSICREATEVIEW *cv = (MSICREATEVIEW*)view;
200
201     TRACE("%p\n", cv );
202
203     msiobj_release( &cv->db->hdr );
204     msi_free( cv );
205
206     return ERROR_SUCCESS;
207 }
208
209
210 static const MSIVIEWOPS create_ops =
211 {
212     CREATE_fetch_int,
213     NULL,
214     NULL,
215     NULL,
216     CREATE_execute,
217     CREATE_close,
218     CREATE_get_dimensions,
219     CREATE_get_column_info,
220     CREATE_modify,
221     CREATE_delete
222 };
223
224 static UINT check_columns( column_info *col_info )
225 {
226     column_info *c1, *c2;
227
228     /* check for two columns with the same name */
229     for( c1 = col_info; c1; c1 = c1->next )
230         for( c2 = c1->next; c2; c2 = c2->next )
231             if (!lstrcmpW(c1->column, c2->column))
232                 return ERROR_BAD_QUERY_SYNTAX;
233
234     return ERROR_SUCCESS;
235 }
236
237 UINT CREATE_CreateView( MSIDATABASE *db, MSIVIEW **view, LPWSTR table,
238                         column_info *col_info, BOOL temp )
239 {
240     MSICREATEVIEW *cv = NULL;
241     UINT r;
242
243     TRACE("%p\n", cv );
244
245     r = check_columns( col_info );
246     if( r != ERROR_SUCCESS )
247         return r;
248
249     cv = msi_alloc_zero( sizeof *cv );
250     if( !cv )
251         return ERROR_FUNCTION_FAILED;
252
253     /* fill the structure */
254     cv->view.ops = &create_ops;
255     msiobj_addref( &db->hdr );
256     cv->db = db;
257     cv->name = table;
258     cv->col_info = col_info;
259     cv->bIsTemp = temp;
260     *view = (MSIVIEW*) cv;
261
262     return ERROR_SUCCESS;
263 }